1414#include <linux/platform_device.h>
1515#include <linux/serial.h>
1616#include <linux/serial_core.h>
17+ #include <linux/slab.h>
1718#include <linux/tty.h>
1819#include <linux/tty_flip.h>
1920#include <linux/console.h>
138139#define XUARTPS_SR_TXFULL 0x00000010 /* TX FIFO full */
139140#define XUARTPS_SR_RXTRIG 0x00000001 /* Rx Trigger */
140141
142+ /**
143+ * struct xuartps - device data
144+ * @refclk Reference clock
145+ * @aperclk APB clock
146+ */
147+ struct xuartps {
148+ struct clk * refclk ;
149+ struct clk * aperclk ;
150+ };
151+
141152/**
142153 * xuartps_isr - Interrupt handler
143154 * @irq: Irq number
@@ -936,34 +947,55 @@ static int xuartps_probe(struct platform_device *pdev)
936947 int rc ;
937948 struct uart_port * port ;
938949 struct resource * res , * res2 ;
939- struct clk * clk ;
950+ struct xuartps * xuartps_data ;
940951
941- clk = of_clk_get (pdev -> dev .of_node , 0 );
942- if (IS_ERR (clk )) {
943- dev_err (& pdev -> dev , "no clock specified\n" );
944- return PTR_ERR (clk );
952+ xuartps_data = kzalloc (sizeof (* xuartps_data ), GFP_KERNEL );
953+ if (!xuartps_data )
954+ return - ENOMEM ;
955+
956+ xuartps_data -> aperclk = clk_get (& pdev -> dev , "aper_clk" );
957+ if (IS_ERR (xuartps_data -> aperclk )) {
958+ dev_err (& pdev -> dev , "aper_clk clock not found.\n" );
959+ rc = PTR_ERR (xuartps_data -> aperclk );
960+ goto err_out_free ;
961+ }
962+ xuartps_data -> refclk = clk_get (& pdev -> dev , "ref_clk" );
963+ if (IS_ERR (xuartps_data -> refclk )) {
964+ dev_err (& pdev -> dev , "ref_clk clock not found.\n" );
965+ rc = PTR_ERR (xuartps_data -> refclk );
966+ goto err_out_clk_put_aper ;
945967 }
946968
947- rc = clk_prepare_enable (clk );
969+ rc = clk_prepare_enable (xuartps_data -> aperclk );
970+ if (rc ) {
971+ dev_err (& pdev -> dev , "Unable to enable APER clock.\n" );
972+ goto err_out_clk_put ;
973+ }
974+ rc = clk_prepare_enable (xuartps_data -> refclk );
948975 if (rc ) {
949- dev_err (& pdev -> dev , "could not enable clock\n" );
950- return - EBUSY ;
976+ dev_err (& pdev -> dev , "Unable to enable device clock. \n" );
977+ goto err_out_clk_dis_aper ;
951978 }
952979
953980 res = platform_get_resource (pdev , IORESOURCE_MEM , 0 );
954- if (!res )
955- return - ENODEV ;
981+ if (!res ) {
982+ rc = - ENODEV ;
983+ goto err_out_clk_disable ;
984+ }
956985
957986 res2 = platform_get_resource (pdev , IORESOURCE_IRQ , 0 );
958- if (!res2 )
959- return - ENODEV ;
987+ if (!res2 ) {
988+ rc = - ENODEV ;
989+ goto err_out_clk_disable ;
990+ }
960991
961992 /* Initialize the port structure */
962993 port = xuartps_get_port ();
963994
964995 if (!port ) {
965996 dev_err (& pdev -> dev , "Cannot get uart_port structure\n" );
966- return - ENODEV ;
997+ rc = - ENODEV ;
998+ goto err_out_clk_disable ;
967999 } else {
9681000 /* Register the port.
9691001 * This function also registers this device with the tty layer
@@ -972,18 +1004,31 @@ static int xuartps_probe(struct platform_device *pdev)
9721004 port -> mapbase = res -> start ;
9731005 port -> irq = res2 -> start ;
9741006 port -> dev = & pdev -> dev ;
975- port -> uartclk = clk_get_rate (clk );
976- port -> private_data = clk ;
1007+ port -> uartclk = clk_get_rate (xuartps_data -> refclk );
1008+ port -> private_data = xuartps_data ;
9771009 dev_set_drvdata (& pdev -> dev , port );
9781010 rc = uart_add_one_port (& xuartps_uart_driver , port );
9791011 if (rc ) {
9801012 dev_err (& pdev -> dev ,
9811013 "uart_add_one_port() failed; err=%i\n" , rc );
9821014 dev_set_drvdata (& pdev -> dev , NULL );
983- return rc ;
1015+ goto err_out_clk_disable ;
9841016 }
9851017 return 0 ;
9861018 }
1019+
1020+ err_out_clk_disable :
1021+ clk_disable_unprepare (xuartps_data -> refclk );
1022+ err_out_clk_dis_aper :
1023+ clk_disable_unprepare (xuartps_data -> aperclk );
1024+ err_out_clk_put :
1025+ clk_put (xuartps_data -> refclk );
1026+ err_out_clk_put_aper :
1027+ clk_put (xuartps_data -> aperclk );
1028+ err_out_free :
1029+ kfree (xuartps_data );
1030+
1031+ return rc ;
9871032}
9881033
9891034/**
@@ -995,14 +1040,18 @@ static int xuartps_probe(struct platform_device *pdev)
9951040static int xuartps_remove (struct platform_device * pdev )
9961041{
9971042 struct uart_port * port = dev_get_drvdata (& pdev -> dev );
998- struct clk * clk = port -> private_data ;
1043+ struct xuartps * xuartps_data = port -> private_data ;
9991044 int rc ;
10001045
10011046 /* Remove the xuartps port from the serial core */
10021047 rc = uart_remove_one_port (& xuartps_uart_driver , port );
10031048 dev_set_drvdata (& pdev -> dev , NULL );
10041049 port -> mapbase = 0 ;
1005- clk_disable_unprepare (clk );
1050+ clk_disable_unprepare (xuartps_data -> refclk );
1051+ clk_disable_unprepare (xuartps_data -> aperclk );
1052+ clk_put (xuartps_data -> refclk );
1053+ clk_put (xuartps_data -> aperclk );
1054+ kfree (xuartps_data );
10061055 return rc ;
10071056}
10081057
0 commit comments