7
7
#include <linux/rtc.h>
8
8
#include <linux/types.h>
9
9
#include <linux/bcd.h>
10
- #include <linux/platform_data/rtc-ds2404.h>
11
10
#include <linux/delay.h>
12
- #include <linux/gpio.h>
11
+ #include <linux/gpio/consumer .h>
13
12
#include <linux/slab.h>
14
13
15
14
#include <linux/io.h>
27
26
#define DS2404_CLK 1
28
27
#define DS2404_DQ 2
29
28
30
- struct ds2404_gpio {
31
- const char * name ;
32
- unsigned int gpio ;
33
- };
34
-
35
29
struct ds2404 {
36
- struct ds2404_gpio * gpio ;
30
+ struct device * dev ;
31
+ struct gpio_desc * rst_gpiod ;
32
+ struct gpio_desc * clk_gpiod ;
33
+ struct gpio_desc * dq_gpiod ;
37
34
struct rtc_device * rtc ;
38
35
};
39
36
40
- static struct ds2404_gpio ds2404_gpio [] = {
41
- { "RTC RST" , 0 },
42
- { "RTC CLK" , 0 },
43
- { "RTC DQ" , 0 },
44
- };
45
-
46
- static int ds2404_gpio_map (struct ds2404 * chip , struct platform_device * pdev ,
47
- struct ds2404_platform_data * pdata )
37
+ static int ds2404_gpio_map (struct ds2404 * chip , struct platform_device * pdev )
48
38
{
49
- int i , err ;
50
-
51
- ds2404_gpio [DS2404_RST ].gpio = pdata -> gpio_rst ;
52
- ds2404_gpio [DS2404_CLK ].gpio = pdata -> gpio_clk ;
53
- ds2404_gpio [DS2404_DQ ].gpio = pdata -> gpio_dq ;
54
-
55
- for (i = 0 ; i < ARRAY_SIZE (ds2404_gpio ); i ++ ) {
56
- err = gpio_request (ds2404_gpio [i ].gpio , ds2404_gpio [i ].name );
57
- if (err ) {
58
- dev_err (& pdev -> dev , "error mapping gpio %s: %d\n" ,
59
- ds2404_gpio [i ].name , err );
60
- goto err_request ;
61
- }
62
- if (i != DS2404_DQ )
63
- gpio_direction_output (ds2404_gpio [i ].gpio , 1 );
64
- }
39
+ struct device * dev = & pdev -> dev ;
65
40
66
- chip -> gpio = ds2404_gpio ;
67
- return 0 ;
41
+ /* This will de-assert RESET, declare this GPIO as GPIOD_ACTIVE_LOW */
42
+ chip -> rst_gpiod = devm_gpiod_get (dev , "rst" , GPIOD_OUT_LOW );
43
+ if (IS_ERR (chip -> rst_gpiod ))
44
+ return PTR_ERR (chip -> rst_gpiod );
68
45
69
- err_request :
70
- while (-- i >= 0 )
71
- gpio_free (ds2404_gpio [i ].gpio );
72
- return err ;
73
- }
46
+ chip -> clk_gpiod = devm_gpiod_get (dev , "clk" , GPIOD_OUT_HIGH );
47
+ if (IS_ERR (chip -> clk_gpiod ))
48
+ return PTR_ERR (chip -> clk_gpiod );
74
49
75
- static void ds2404_gpio_unmap ( void * data )
76
- {
77
- int i ;
50
+ chip -> dq_gpiod = devm_gpiod_get ( dev , "dq" , GPIOD_ASIS );
51
+ if ( IS_ERR ( chip -> dq_gpiod ))
52
+ return PTR_ERR ( chip -> dq_gpiod ) ;
78
53
79
- for (i = 0 ; i < ARRAY_SIZE (ds2404_gpio ); i ++ )
80
- gpio_free (ds2404_gpio [i ].gpio );
54
+ return 0 ;
81
55
}
82
56
83
- static void ds2404_reset (struct device * dev )
57
+ static void ds2404_reset (struct ds2404 * chip )
84
58
{
85
- gpio_set_value ( ds2404_gpio [ DS2404_RST ]. gpio , 0 );
59
+ gpiod_set_value ( chip -> rst_gpiod , 1 );
86
60
udelay (1000 );
87
- gpio_set_value ( ds2404_gpio [ DS2404_RST ]. gpio , 1 );
88
- gpio_set_value ( ds2404_gpio [ DS2404_CLK ]. gpio , 0 );
89
- gpio_direction_output ( ds2404_gpio [ DS2404_DQ ]. gpio , 0 );
61
+ gpiod_set_value ( chip -> rst_gpiod , 0 );
62
+ gpiod_set_value ( chip -> clk_gpiod , 0 );
63
+ gpiod_direction_output ( chip -> dq_gpiod , 0 );
90
64
udelay (10 );
91
65
}
92
66
93
- static void ds2404_write_byte (struct device * dev , u8 byte )
67
+ static void ds2404_write_byte (struct ds2404 * chip , u8 byte )
94
68
{
95
69
int i ;
96
70
97
- gpio_direction_output ( ds2404_gpio [ DS2404_DQ ]. gpio , 1 );
71
+ gpiod_direction_output ( chip -> dq_gpiod , 1 );
98
72
for (i = 0 ; i < 8 ; i ++ ) {
99
- gpio_set_value ( ds2404_gpio [ DS2404_DQ ]. gpio , byte & (1 << i ));
73
+ gpiod_set_value ( chip -> dq_gpiod , byte & (1 << i ));
100
74
udelay (10 );
101
- gpio_set_value ( ds2404_gpio [ DS2404_CLK ]. gpio , 1 );
75
+ gpiod_set_value ( chip -> clk_gpiod , 1 );
102
76
udelay (10 );
103
- gpio_set_value ( ds2404_gpio [ DS2404_CLK ]. gpio , 0 );
77
+ gpiod_set_value ( chip -> clk_gpiod , 0 );
104
78
udelay (10 );
105
79
}
106
80
}
107
81
108
- static u8 ds2404_read_byte (struct device * dev )
82
+ static u8 ds2404_read_byte (struct ds2404 * chip )
109
83
{
110
84
int i ;
111
85
u8 ret = 0 ;
112
86
113
- gpio_direction_input ( ds2404_gpio [ DS2404_DQ ]. gpio );
87
+ gpiod_direction_input ( chip -> dq_gpiod );
114
88
115
89
for (i = 0 ; i < 8 ; i ++ ) {
116
- gpio_set_value ( ds2404_gpio [ DS2404_CLK ]. gpio , 0 );
90
+ gpiod_set_value ( chip -> clk_gpiod , 0 );
117
91
udelay (10 );
118
- if (gpio_get_value ( ds2404_gpio [ DS2404_DQ ]. gpio ))
92
+ if (gpiod_get_value ( chip -> dq_gpiod ))
119
93
ret |= 1 << i ;
120
- gpio_set_value ( ds2404_gpio [ DS2404_CLK ]. gpio , 1 );
94
+ gpiod_set_value ( chip -> clk_gpiod , 1 );
121
95
udelay (10 );
122
96
}
123
97
return ret ;
124
98
}
125
99
126
- static void ds2404_read_memory (struct device * dev , u16 offset ,
100
+ static void ds2404_read_memory (struct ds2404 * chip , u16 offset ,
127
101
int length , u8 * out )
128
102
{
129
- ds2404_reset (dev );
130
- ds2404_write_byte (dev , DS2404_READ_MEMORY_CMD );
131
- ds2404_write_byte (dev , offset & 0xff );
132
- ds2404_write_byte (dev , (offset >> 8 ) & 0xff );
103
+ ds2404_reset (chip );
104
+ ds2404_write_byte (chip , DS2404_READ_MEMORY_CMD );
105
+ ds2404_write_byte (chip , offset & 0xff );
106
+ ds2404_write_byte (chip , (offset >> 8 ) & 0xff );
133
107
while (length -- )
134
- * out ++ = ds2404_read_byte (dev );
108
+ * out ++ = ds2404_read_byte (chip );
135
109
}
136
110
137
- static void ds2404_write_memory (struct device * dev , u16 offset ,
111
+ static void ds2404_write_memory (struct ds2404 * chip , u16 offset ,
138
112
int length , u8 * out )
139
113
{
140
114
int i ;
141
115
u8 ta01 , ta02 , es ;
142
116
143
- ds2404_reset (dev );
144
- ds2404_write_byte (dev , DS2404_WRITE_SCRATCHPAD_CMD );
145
- ds2404_write_byte (dev , offset & 0xff );
146
- ds2404_write_byte (dev , (offset >> 8 ) & 0xff );
117
+ ds2404_reset (chip );
118
+ ds2404_write_byte (chip , DS2404_WRITE_SCRATCHPAD_CMD );
119
+ ds2404_write_byte (chip , offset & 0xff );
120
+ ds2404_write_byte (chip , (offset >> 8 ) & 0xff );
147
121
148
122
for (i = 0 ; i < length ; i ++ )
149
- ds2404_write_byte (dev , out [i ]);
123
+ ds2404_write_byte (chip , out [i ]);
150
124
151
- ds2404_reset (dev );
152
- ds2404_write_byte (dev , DS2404_READ_SCRATCHPAD_CMD );
125
+ ds2404_reset (chip );
126
+ ds2404_write_byte (chip , DS2404_READ_SCRATCHPAD_CMD );
153
127
154
- ta01 = ds2404_read_byte (dev );
155
- ta02 = ds2404_read_byte (dev );
156
- es = ds2404_read_byte (dev );
128
+ ta01 = ds2404_read_byte (chip );
129
+ ta02 = ds2404_read_byte (chip );
130
+ es = ds2404_read_byte (chip );
157
131
158
132
for (i = 0 ; i < length ; i ++ ) {
159
- if (out [i ] != ds2404_read_byte (dev )) {
160
- dev_err (dev , "read invalid data\n" );
133
+ if (out [i ] != ds2404_read_byte (chip )) {
134
+ dev_err (chip -> dev , "read invalid data\n" );
161
135
return ;
162
136
}
163
137
}
164
138
165
- ds2404_reset (dev );
166
- ds2404_write_byte (dev , DS2404_COPY_SCRATCHPAD_CMD );
167
- ds2404_write_byte (dev , ta01 );
168
- ds2404_write_byte (dev , ta02 );
169
- ds2404_write_byte (dev , es );
139
+ ds2404_reset (chip );
140
+ ds2404_write_byte (chip , DS2404_COPY_SCRATCHPAD_CMD );
141
+ ds2404_write_byte (chip , ta01 );
142
+ ds2404_write_byte (chip , ta02 );
143
+ ds2404_write_byte (chip , es );
170
144
171
- gpio_direction_input (ds2404_gpio [DS2404_DQ ].gpio );
172
- while (gpio_get_value (ds2404_gpio [DS2404_DQ ].gpio ))
145
+ while (gpiod_get_value (chip -> dq_gpiod ))
173
146
;
174
147
}
175
148
176
- static void ds2404_enable_osc (struct device * dev )
149
+ static void ds2404_enable_osc (struct ds2404 * chip )
177
150
{
178
151
u8 in [1 ] = { 0x10 }; /* enable oscillator */
179
- ds2404_write_memory (dev , 0x201 , 1 , in );
152
+
153
+ ds2404_write_memory (chip , 0x201 , 1 , in );
180
154
}
181
155
182
156
static int ds2404_read_time (struct device * dev , struct rtc_time * dt )
183
157
{
158
+ struct ds2404 * chip = dev_get_drvdata (dev );
184
159
unsigned long time = 0 ;
185
160
__le32 hw_time = 0 ;
186
161
187
- ds2404_read_memory (dev , 0x203 , 4 , (u8 * )& hw_time );
162
+ ds2404_read_memory (chip , 0x203 , 4 , (u8 * )& hw_time );
188
163
time = le32_to_cpu (hw_time );
189
164
190
165
rtc_time64_to_tm (time , dt );
@@ -193,8 +168,9 @@ static int ds2404_read_time(struct device *dev, struct rtc_time *dt)
193
168
194
169
static int ds2404_set_time (struct device * dev , struct rtc_time * dt )
195
170
{
171
+ struct ds2404 * chip = dev_get_drvdata (dev );
196
172
u32 time = cpu_to_le32 (rtc_tm_to_time64 (dt ));
197
- ds2404_write_memory (dev , 0x203 , 4 , (u8 * )& time );
173
+ ds2404_write_memory (chip , 0x203 , 4 , (u8 * )& time );
198
174
return 0 ;
199
175
}
200
176
@@ -205,30 +181,23 @@ static const struct rtc_class_ops ds2404_rtc_ops = {
205
181
206
182
static int rtc_probe (struct platform_device * pdev )
207
183
{
208
- struct ds2404_platform_data * pdata = dev_get_platdata (& pdev -> dev );
209
184
struct ds2404 * chip ;
210
185
int retval = - EBUSY ;
211
186
212
187
chip = devm_kzalloc (& pdev -> dev , sizeof (struct ds2404 ), GFP_KERNEL );
213
188
if (!chip )
214
189
return - ENOMEM ;
215
190
191
+ chip -> dev = & pdev -> dev ;
192
+
216
193
chip -> rtc = devm_rtc_allocate_device (& pdev -> dev );
217
194
if (IS_ERR (chip -> rtc ))
218
195
return PTR_ERR (chip -> rtc );
219
196
220
- retval = ds2404_gpio_map (chip , pdev , pdata );
197
+ retval = ds2404_gpio_map (chip , pdev );
221
198
if (retval )
222
199
return retval ;
223
200
224
- retval = devm_add_action_or_reset (& pdev -> dev , ds2404_gpio_unmap , chip );
225
- if (retval )
226
- return retval ;
227
-
228
- dev_info (& pdev -> dev , "using GPIOs RST:%d, CLK:%d, DQ:%d\n" ,
229
- chip -> gpio [DS2404_RST ].gpio , chip -> gpio [DS2404_CLK ].gpio ,
230
- chip -> gpio [DS2404_DQ ].gpio );
231
-
232
201
platform_set_drvdata (pdev , chip );
233
202
234
203
chip -> rtc -> ops = & ds2404_rtc_ops ;
@@ -238,7 +207,7 @@ static int rtc_probe(struct platform_device *pdev)
238
207
if (retval )
239
208
return retval ;
240
209
241
- ds2404_enable_osc (& pdev -> dev );
210
+ ds2404_enable_osc (chip );
242
211
return 0 ;
243
212
}
244
213
0 commit comments