Commit | Line | Data |
---|---|---|
3c2b9075 WG |
1 | /* |
2 | * Driver for Epson's RTC module RX-8025 SA/NB | |
3 | * | |
4 | * Copyright (C) 2009 Wolfgang Grandegger <wg@grandegger.com> | |
5 | * | |
6 | * Copyright (C) 2005 by Digi International Inc. | |
7 | * All rights reserved. | |
8 | * | |
9 | * Modified by fengjh at rising.com.cn | |
10 | * <http://lists.lm-sensors.org/mailman/listinfo/lm-sensors> | |
11 | * 2006.11 | |
12 | * | |
13 | * Code cleanup by Sergei Poselenov, <sposelenov@emcraft.com> | |
14 | * Converted to new style by Wolfgang Grandegger <wg@grandegger.com> | |
15 | * Alarm and periodic interrupt added by Dmitry Rakhchev <rda@emcraft.com> | |
16 | * | |
17 | * This program is free software; you can redistribute it and/or | |
18 | * modify it under the terms of the GNU General Public License | |
19 | * version 2 as published by the Free Software Foundation. | |
20 | */ | |
3c2b9075 | 21 | #include <linux/bcd.h> |
2ddd1869 | 22 | #include <linux/bitops.h> |
3c2b9075 | 23 | #include <linux/i2c.h> |
15d3bdc2 AB |
24 | #include <linux/kernel.h> |
25 | #include <linux/module.h> | |
3c2b9075 WG |
26 | #include <linux/rtc.h> |
27 | ||
28 | /* Register definitions */ | |
29 | #define RX8025_REG_SEC 0x00 | |
30 | #define RX8025_REG_MIN 0x01 | |
31 | #define RX8025_REG_HOUR 0x02 | |
32 | #define RX8025_REG_WDAY 0x03 | |
33 | #define RX8025_REG_MDAY 0x04 | |
34 | #define RX8025_REG_MONTH 0x05 | |
35 | #define RX8025_REG_YEAR 0x06 | |
36 | #define RX8025_REG_DIGOFF 0x07 | |
37 | #define RX8025_REG_ALWMIN 0x08 | |
38 | #define RX8025_REG_ALWHOUR 0x09 | |
39 | #define RX8025_REG_ALWWDAY 0x0a | |
40 | #define RX8025_REG_ALDMIN 0x0b | |
41 | #define RX8025_REG_ALDHOUR 0x0c | |
42 | /* 0x0d is reserved */ | |
43 | #define RX8025_REG_CTRL1 0x0e | |
44 | #define RX8025_REG_CTRL2 0x0f | |
45 | ||
46 | #define RX8025_BIT_CTRL1_CT (7 << 0) | |
47 | /* 1 Hz periodic level irq */ | |
48 | #define RX8025_BIT_CTRL1_CT_1HZ 4 | |
2ddd1869 AB |
49 | #define RX8025_BIT_CTRL1_TEST BIT(3) |
50 | #define RX8025_BIT_CTRL1_1224 BIT(5) | |
51 | #define RX8025_BIT_CTRL1_DALE BIT(6) | |
52 | #define RX8025_BIT_CTRL1_WALE BIT(7) | |
3c2b9075 | 53 | |
2ddd1869 AB |
54 | #define RX8025_BIT_CTRL2_DAFG BIT(0) |
55 | #define RX8025_BIT_CTRL2_WAFG BIT(1) | |
56 | #define RX8025_BIT_CTRL2_CTFG BIT(2) | |
57 | #define RX8025_BIT_CTRL2_PON BIT(4) | |
58 | #define RX8025_BIT_CTRL2_XST BIT(5) | |
59 | #define RX8025_BIT_CTRL2_VDET BIT(6) | |
3c2b9075 WG |
60 | |
61 | /* Clock precision adjustment */ | |
62 | #define RX8025_ADJ_RESOLUTION 3050 /* in ppb */ | |
63 | #define RX8025_ADJ_DATA_MAX 62 | |
64 | #define RX8025_ADJ_DATA_MIN -62 | |
65 | ||
66 | static const struct i2c_device_id rx8025_id[] = { | |
67 | { "rx8025", 0 }, | |
b1f9d790 | 68 | { "rv8803", 1 }, |
3c2b9075 WG |
69 | { } |
70 | }; | |
71 | MODULE_DEVICE_TABLE(i2c, rx8025_id); | |
72 | ||
73 | struct rx8025_data { | |
74 | struct i2c_client *client; | |
75 | struct rtc_device *rtc; | |
3c2b9075 | 76 | u8 ctrl1; |
3c2b9075 WG |
77 | }; |
78 | ||
fd9061fb | 79 | static s32 rx8025_read_reg(const struct i2c_client *client, u8 number) |
3c2b9075 | 80 | { |
fd9061fb | 81 | return i2c_smbus_read_byte_data(client, number << 4); |
3c2b9075 WG |
82 | } |
83 | ||
fd9061fb AB |
84 | static int rx8025_read_regs(const struct i2c_client *client, |
85 | u8 number, u8 length, u8 *values) | |
3c2b9075 | 86 | { |
fd9061fb AB |
87 | int ret = i2c_smbus_read_i2c_block_data(client, number << 4, length, |
88 | values); | |
89 | if (ret != length) | |
3c2b9075 | 90 | return ret < 0 ? ret : -EIO; |
3c2b9075 WG |
91 | |
92 | return 0; | |
93 | } | |
94 | ||
fd9061fb AB |
95 | static s32 rx8025_write_reg(const struct i2c_client *client, u8 number, |
96 | u8 value) | |
3c2b9075 | 97 | { |
fd9061fb | 98 | return i2c_smbus_write_byte_data(client, number << 4, value); |
3c2b9075 WG |
99 | } |
100 | ||
fd9061fb AB |
101 | static s32 rx8025_write_regs(const struct i2c_client *client, |
102 | u8 number, u8 length, const u8 *values) | |
3c2b9075 | 103 | { |
fd9061fb AB |
104 | return i2c_smbus_write_i2c_block_data(client, number << 4, |
105 | length, values); | |
3c2b9075 WG |
106 | } |
107 | ||
efbbb4fd AB |
108 | static int rx8025_check_validity(struct device *dev) |
109 | { | |
110 | struct rx8025_data *rx8025 = dev_get_drvdata(dev); | |
111 | int ctrl2; | |
112 | ||
113 | ctrl2 = rx8025_read_reg(rx8025->client, RX8025_REG_CTRL2); | |
114 | if (ctrl2 < 0) | |
115 | return ctrl2; | |
116 | ||
117 | if (ctrl2 & RX8025_BIT_CTRL2_VDET) | |
118 | dev_warn(dev, "power voltage drop detected\n"); | |
119 | ||
120 | if (ctrl2 & RX8025_BIT_CTRL2_PON) { | |
121 | dev_warn(dev, "power-on reset detected, date is invalid\n"); | |
122 | return -EINVAL; | |
123 | } | |
124 | ||
125 | if (!(ctrl2 & RX8025_BIT_CTRL2_XST)) { | |
126 | dev_warn(dev, "crystal stopped, date is invalid\n"); | |
127 | return -EINVAL; | |
128 | } | |
129 | ||
130 | return 0; | |
131 | } | |
132 | ||
8c4a4467 AB |
133 | static int rx8025_reset_validity(struct i2c_client *client) |
134 | { | |
135 | int ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2); | |
136 | ||
137 | if (ctrl2 < 0) | |
138 | return ctrl2; | |
139 | ||
140 | ctrl2 &= ~(RX8025_BIT_CTRL2_PON | RX8025_BIT_CTRL2_VDET); | |
141 | ||
142 | return rx8025_write_reg(client, RX8025_REG_CTRL2, | |
143 | ctrl2 | RX8025_BIT_CTRL2_XST); | |
144 | } | |
145 | ||
b6a57c95 | 146 | static irqreturn_t rx8025_handle_irq(int irq, void *dev_id) |
3c2b9075 WG |
147 | { |
148 | struct i2c_client *client = dev_id; | |
149 | struct rx8025_data *rx8025 = i2c_get_clientdata(client); | |
fd9061fb | 150 | int status; |
3c2b9075 | 151 | |
fd9061fb AB |
152 | status = rx8025_read_reg(client, RX8025_REG_CTRL2); |
153 | if (status < 0) | |
3c2b9075 WG |
154 | goto out; |
155 | ||
156 | if (!(status & RX8025_BIT_CTRL2_XST)) | |
157 | dev_warn(&client->dev, "Oscillation stop was detected," | |
158 | "you may have to readjust the clock\n"); | |
159 | ||
160 | if (status & RX8025_BIT_CTRL2_CTFG) { | |
161 | /* periodic */ | |
162 | status &= ~RX8025_BIT_CTRL2_CTFG; | |
3c2b9075 | 163 | rtc_update_irq(rx8025->rtc, 1, RTC_PF | RTC_IRQF); |
3c2b9075 WG |
164 | } |
165 | ||
166 | if (status & RX8025_BIT_CTRL2_DAFG) { | |
167 | /* alarm */ | |
168 | status &= RX8025_BIT_CTRL2_DAFG; | |
169 | if (rx8025_write_reg(client, RX8025_REG_CTRL1, | |
170 | rx8025->ctrl1 & ~RX8025_BIT_CTRL1_DALE)) | |
171 | goto out; | |
3c2b9075 | 172 | rtc_update_irq(rx8025->rtc, 1, RTC_AF | RTC_IRQF); |
3c2b9075 WG |
173 | } |
174 | ||
3c2b9075 | 175 | out: |
b6a57c95 | 176 | return IRQ_HANDLED; |
3c2b9075 WG |
177 | } |
178 | ||
179 | static int rx8025_get_time(struct device *dev, struct rtc_time *dt) | |
180 | { | |
181 | struct rx8025_data *rx8025 = dev_get_drvdata(dev); | |
fd9061fb | 182 | u8 date[7]; |
efbbb4fd | 183 | int err; |
6f0a8cfe | 184 | |
efbbb4fd AB |
185 | err = rx8025_check_validity(dev); |
186 | if (err) | |
187 | return err; | |
6f0a8cfe | 188 | |
3c2b9075 WG |
189 | err = rx8025_read_regs(rx8025->client, RX8025_REG_SEC, 7, date); |
190 | if (err) | |
191 | return err; | |
192 | ||
193 | dev_dbg(dev, "%s: read 0x%02x 0x%02x " | |
194 | "0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n", __func__, | |
195 | date[0], date[1], date[2], date[3], date[4], | |
196 | date[5], date[6]); | |
197 | ||
198 | dt->tm_sec = bcd2bin(date[RX8025_REG_SEC] & 0x7f); | |
199 | dt->tm_min = bcd2bin(date[RX8025_REG_MIN] & 0x7f); | |
200 | if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224) | |
201 | dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x3f); | |
202 | else | |
203 | dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x1f) % 12 | |
204 | + (date[RX8025_REG_HOUR] & 0x20 ? 12 : 0); | |
205 | ||
206 | dt->tm_mday = bcd2bin(date[RX8025_REG_MDAY] & 0x3f); | |
207 | dt->tm_mon = bcd2bin(date[RX8025_REG_MONTH] & 0x1f) - 1; | |
32672c55 | 208 | dt->tm_year = bcd2bin(date[RX8025_REG_YEAR]) + 100; |
3c2b9075 WG |
209 | |
210 | dev_dbg(dev, "%s: date %ds %dm %dh %dmd %dm %dy\n", __func__, | |
211 | dt->tm_sec, dt->tm_min, dt->tm_hour, | |
212 | dt->tm_mday, dt->tm_mon, dt->tm_year); | |
213 | ||
214 | return rtc_valid_tm(dt); | |
215 | } | |
216 | ||
217 | static int rx8025_set_time(struct device *dev, struct rtc_time *dt) | |
218 | { | |
219 | struct rx8025_data *rx8025 = dev_get_drvdata(dev); | |
220 | u8 date[7]; | |
8c4a4467 | 221 | int ret; |
3c2b9075 | 222 | |
32672c55 AB |
223 | if ((dt->tm_year < 100) || (dt->tm_year > 199)) |
224 | return -EINVAL; | |
3c2b9075 WG |
225 | |
226 | /* | |
227 | * Here the read-only bits are written as "0". I'm not sure if that | |
228 | * is sound. | |
229 | */ | |
230 | date[RX8025_REG_SEC] = bin2bcd(dt->tm_sec); | |
231 | date[RX8025_REG_MIN] = bin2bcd(dt->tm_min); | |
232 | if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224) | |
233 | date[RX8025_REG_HOUR] = bin2bcd(dt->tm_hour); | |
234 | else | |
235 | date[RX8025_REG_HOUR] = (dt->tm_hour >= 12 ? 0x20 : 0) | |
236 | | bin2bcd((dt->tm_hour + 11) % 12 + 1); | |
237 | ||
238 | date[RX8025_REG_WDAY] = bin2bcd(dt->tm_wday); | |
239 | date[RX8025_REG_MDAY] = bin2bcd(dt->tm_mday); | |
240 | date[RX8025_REG_MONTH] = bin2bcd(dt->tm_mon + 1); | |
32672c55 | 241 | date[RX8025_REG_YEAR] = bin2bcd(dt->tm_year - 100); |
3c2b9075 WG |
242 | |
243 | dev_dbg(dev, | |
244 | "%s: write 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n", | |
245 | __func__, | |
246 | date[0], date[1], date[2], date[3], date[4], date[5], date[6]); | |
247 | ||
8c4a4467 AB |
248 | ret = rx8025_write_regs(rx8025->client, RX8025_REG_SEC, 7, date); |
249 | if (ret < 0) | |
250 | return ret; | |
251 | ||
252 | return rx8025_reset_validity(rx8025->client); | |
3c2b9075 WG |
253 | } |
254 | ||
6f0a8cfe | 255 | static int rx8025_init_client(struct i2c_client *client) |
3c2b9075 WG |
256 | { |
257 | struct rx8025_data *rx8025 = i2c_get_clientdata(client); | |
258 | u8 ctrl[2], ctrl2; | |
259 | int need_clear = 0; | |
260 | int err; | |
261 | ||
262 | err = rx8025_read_regs(rx8025->client, RX8025_REG_CTRL1, 2, ctrl); | |
263 | if (err) | |
264 | goto out; | |
265 | ||
266 | /* Keep test bit zero ! */ | |
267 | rx8025->ctrl1 = ctrl[0] & ~RX8025_BIT_CTRL1_TEST; | |
268 | ||
3c2b9075 WG |
269 | if (ctrl[1] & (RX8025_BIT_CTRL2_DAFG | RX8025_BIT_CTRL2_WAFG)) { |
270 | dev_warn(&client->dev, "Alarm was detected\n"); | |
271 | need_clear = 1; | |
272 | } | |
273 | ||
5c66e1e0 | 274 | if (ctrl[1] & RX8025_BIT_CTRL2_CTFG) |
3c2b9075 WG |
275 | need_clear = 1; |
276 | ||
6f0a8cfe | 277 | if (need_clear) { |
a27c7bf6 | 278 | ctrl2 = ctrl[1]; |
8c4a4467 | 279 | ctrl2 &= ~(RX8025_BIT_CTRL2_CTFG | RX8025_BIT_CTRL2_WAFG | |
3c2b9075 | 280 | RX8025_BIT_CTRL2_DAFG); |
3c2b9075 WG |
281 | |
282 | err = rx8025_write_reg(client, RX8025_REG_CTRL2, ctrl2); | |
283 | } | |
284 | out: | |
285 | return err; | |
286 | } | |
287 | ||
288 | /* Alarm support */ | |
289 | static int rx8025_read_alarm(struct device *dev, struct rtc_wkalrm *t) | |
290 | { | |
291 | struct rx8025_data *rx8025 = dev_get_drvdata(dev); | |
292 | struct i2c_client *client = rx8025->client; | |
fd9061fb AB |
293 | u8 ald[2]; |
294 | int ctrl2, err; | |
3c2b9075 WG |
295 | |
296 | if (client->irq <= 0) | |
297 | return -EINVAL; | |
298 | ||
299 | err = rx8025_read_regs(client, RX8025_REG_ALDMIN, 2, ald); | |
300 | if (err) | |
301 | return err; | |
302 | ||
fd9061fb AB |
303 | ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2); |
304 | if (ctrl2 < 0) | |
305 | return ctrl2; | |
3c2b9075 WG |
306 | |
307 | dev_dbg(dev, "%s: read alarm 0x%02x 0x%02x ctrl2 %02x\n", | |
308 | __func__, ald[0], ald[1], ctrl2); | |
309 | ||
310 | /* Hardware alarms precision is 1 minute! */ | |
311 | t->time.tm_sec = 0; | |
312 | t->time.tm_min = bcd2bin(ald[0] & 0x7f); | |
313 | if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224) | |
314 | t->time.tm_hour = bcd2bin(ald[1] & 0x3f); | |
315 | else | |
316 | t->time.tm_hour = bcd2bin(ald[1] & 0x1f) % 12 | |
317 | + (ald[1] & 0x20 ? 12 : 0); | |
318 | ||
319 | t->time.tm_wday = -1; | |
320 | t->time.tm_mday = -1; | |
321 | t->time.tm_mon = -1; | |
322 | t->time.tm_year = -1; | |
323 | ||
324 | dev_dbg(dev, "%s: date: %ds %dm %dh %dmd %dm %dy\n", | |
325 | __func__, | |
326 | t->time.tm_sec, t->time.tm_min, t->time.tm_hour, | |
327 | t->time.tm_mday, t->time.tm_mon, t->time.tm_year); | |
328 | t->enabled = !!(rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE); | |
329 | t->pending = (ctrl2 & RX8025_BIT_CTRL2_DAFG) && t->enabled; | |
330 | ||
331 | return err; | |
332 | } | |
333 | ||
334 | static int rx8025_set_alarm(struct device *dev, struct rtc_wkalrm *t) | |
335 | { | |
336 | struct i2c_client *client = to_i2c_client(dev); | |
337 | struct rx8025_data *rx8025 = dev_get_drvdata(dev); | |
338 | u8 ald[2]; | |
339 | int err; | |
340 | ||
341 | if (client->irq <= 0) | |
342 | return -EINVAL; | |
343 | ||
344 | /* Hardware alarm precision is 1 minute! */ | |
345 | ald[0] = bin2bcd(t->time.tm_min); | |
346 | if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224) | |
347 | ald[1] = bin2bcd(t->time.tm_hour); | |
348 | else | |
349 | ald[1] = (t->time.tm_hour >= 12 ? 0x20 : 0) | |
350 | | bin2bcd((t->time.tm_hour + 11) % 12 + 1); | |
351 | ||
352 | dev_dbg(dev, "%s: write 0x%02x 0x%02x\n", __func__, ald[0], ald[1]); | |
353 | ||
354 | if (rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE) { | |
355 | rx8025->ctrl1 &= ~RX8025_BIT_CTRL1_DALE; | |
356 | err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1, | |
357 | rx8025->ctrl1); | |
358 | if (err) | |
359 | return err; | |
360 | } | |
361 | err = rx8025_write_regs(rx8025->client, RX8025_REG_ALDMIN, 2, ald); | |
362 | if (err) | |
363 | return err; | |
364 | ||
365 | if (t->enabled) { | |
366 | rx8025->ctrl1 |= RX8025_BIT_CTRL1_DALE; | |
367 | err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1, | |
368 | rx8025->ctrl1); | |
369 | if (err) | |
370 | return err; | |
371 | } | |
372 | ||
373 | return 0; | |
374 | } | |
375 | ||
376 | static int rx8025_alarm_irq_enable(struct device *dev, unsigned int enabled) | |
377 | { | |
378 | struct rx8025_data *rx8025 = dev_get_drvdata(dev); | |
379 | u8 ctrl1; | |
380 | int err; | |
381 | ||
382 | ctrl1 = rx8025->ctrl1; | |
383 | if (enabled) | |
384 | ctrl1 |= RX8025_BIT_CTRL1_DALE; | |
385 | else | |
386 | ctrl1 &= ~RX8025_BIT_CTRL1_DALE; | |
387 | ||
388 | if (ctrl1 != rx8025->ctrl1) { | |
389 | rx8025->ctrl1 = ctrl1; | |
390 | err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1, | |
391 | rx8025->ctrl1); | |
392 | if (err) | |
393 | return err; | |
394 | } | |
395 | return 0; | |
396 | } | |
397 | ||
3c2b9075 WG |
398 | static struct rtc_class_ops rx8025_rtc_ops = { |
399 | .read_time = rx8025_get_time, | |
400 | .set_time = rx8025_set_time, | |
401 | .read_alarm = rx8025_read_alarm, | |
402 | .set_alarm = rx8025_set_alarm, | |
403 | .alarm_irq_enable = rx8025_alarm_irq_enable, | |
3c2b9075 WG |
404 | }; |
405 | ||
406 | /* | |
407 | * Clock precision adjustment support | |
408 | * | |
409 | * According to the RX8025 SA/NB application manual the frequency and | |
b770ffd4 | 410 | * temperature characteristics can be approximated using the following |
3c2b9075 WG |
411 | * equation: |
412 | * | |
413 | * df = a * (ut - t)**2 | |
414 | * | |
415 | * df: Frequency deviation in any temperature | |
416 | * a : Coefficient = (-35 +-5) * 10**-9 | |
417 | * ut: Ultimate temperature in degree = +25 +-5 degree | |
418 | * t : Any temperature in degree | |
419 | * | |
420 | * Note that the clock adjustment in ppb must be entered (which is | |
421 | * the negative value of the deviation). | |
422 | */ | |
423 | static int rx8025_get_clock_adjust(struct device *dev, int *adj) | |
424 | { | |
425 | struct i2c_client *client = to_i2c_client(dev); | |
fd9061fb | 426 | int digoff; |
3c2b9075 | 427 | |
fd9061fb AB |
428 | digoff = rx8025_read_reg(client, RX8025_REG_DIGOFF); |
429 | if (digoff < 0) | |
430 | return digoff; | |
3c2b9075 WG |
431 | |
432 | *adj = digoff >= 64 ? digoff - 128 : digoff; | |
433 | if (*adj > 0) | |
434 | (*adj)--; | |
435 | *adj *= -RX8025_ADJ_RESOLUTION; | |
436 | ||
437 | return 0; | |
438 | } | |
439 | ||
440 | static int rx8025_set_clock_adjust(struct device *dev, int adj) | |
441 | { | |
442 | struct i2c_client *client = to_i2c_client(dev); | |
443 | u8 digoff; | |
444 | int err; | |
445 | ||
446 | adj /= -RX8025_ADJ_RESOLUTION; | |
447 | if (adj > RX8025_ADJ_DATA_MAX) | |
448 | adj = RX8025_ADJ_DATA_MAX; | |
449 | else if (adj < RX8025_ADJ_DATA_MIN) | |
450 | adj = RX8025_ADJ_DATA_MIN; | |
451 | else if (adj > 0) | |
452 | adj++; | |
453 | else if (adj < 0) | |
454 | adj += 128; | |
455 | digoff = adj; | |
456 | ||
457 | err = rx8025_write_reg(client, RX8025_REG_DIGOFF, digoff); | |
458 | if (err) | |
459 | return err; | |
460 | ||
461 | dev_dbg(dev, "%s: write 0x%02x\n", __func__, digoff); | |
462 | ||
463 | return 0; | |
464 | } | |
465 | ||
466 | static ssize_t rx8025_sysfs_show_clock_adjust(struct device *dev, | |
467 | struct device_attribute *attr, | |
468 | char *buf) | |
469 | { | |
470 | int err, adj; | |
471 | ||
472 | err = rx8025_get_clock_adjust(dev, &adj); | |
473 | if (err) | |
474 | return err; | |
475 | ||
476 | return sprintf(buf, "%d\n", adj); | |
477 | } | |
478 | ||
479 | static ssize_t rx8025_sysfs_store_clock_adjust(struct device *dev, | |
480 | struct device_attribute *attr, | |
481 | const char *buf, size_t count) | |
482 | { | |
483 | int adj, err; | |
484 | ||
485 | if (sscanf(buf, "%i", &adj) != 1) | |
486 | return -EINVAL; | |
487 | ||
488 | err = rx8025_set_clock_adjust(dev, adj); | |
489 | ||
490 | return err ? err : count; | |
491 | } | |
492 | ||
493 | static DEVICE_ATTR(clock_adjust_ppb, S_IRUGO | S_IWUSR, | |
494 | rx8025_sysfs_show_clock_adjust, | |
495 | rx8025_sysfs_store_clock_adjust); | |
496 | ||
497 | static int rx8025_sysfs_register(struct device *dev) | |
498 | { | |
499 | return device_create_file(dev, &dev_attr_clock_adjust_ppb); | |
500 | } | |
501 | ||
502 | static void rx8025_sysfs_unregister(struct device *dev) | |
503 | { | |
504 | device_remove_file(dev, &dev_attr_clock_adjust_ppb); | |
505 | } | |
506 | ||
5a167f45 GKH |
507 | static int rx8025_probe(struct i2c_client *client, |
508 | const struct i2c_device_id *id) | |
3c2b9075 WG |
509 | { |
510 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); | |
511 | struct rx8025_data *rx8025; | |
6f0a8cfe | 512 | int err = 0; |
3c2b9075 WG |
513 | |
514 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | |
515 | | I2C_FUNC_SMBUS_I2C_BLOCK)) { | |
516 | dev_err(&adapter->dev, | |
517 | "doesn't support required functionality\n"); | |
dbcce7cf | 518 | return -EIO; |
3c2b9075 WG |
519 | } |
520 | ||
fac42b41 | 521 | rx8025 = devm_kzalloc(&client->dev, sizeof(*rx8025), GFP_KERNEL); |
b1f9d790 | 522 | if (!rx8025) |
dbcce7cf | 523 | return -ENOMEM; |
3c2b9075 WG |
524 | |
525 | rx8025->client = client; | |
526 | i2c_set_clientdata(client, rx8025); | |
3c2b9075 | 527 | |
6f0a8cfe | 528 | err = rx8025_init_client(client); |
3c2b9075 | 529 | if (err) |
dbcce7cf | 530 | return err; |
3c2b9075 | 531 | |
fac42b41 | 532 | rx8025->rtc = devm_rtc_device_register(&client->dev, client->name, |
3c2b9075 WG |
533 | &rx8025_rtc_ops, THIS_MODULE); |
534 | if (IS_ERR(rx8025->rtc)) { | |
3c2b9075 | 535 | dev_err(&client->dev, "unable to register the class device\n"); |
dbcce7cf | 536 | return PTR_ERR(rx8025->rtc); |
3c2b9075 WG |
537 | } |
538 | ||
539 | if (client->irq > 0) { | |
540 | dev_info(&client->dev, "IRQ %d supplied\n", client->irq); | |
f0b63a1d AB |
541 | err = devm_request_threaded_irq(&client->dev, client->irq, NULL, |
542 | rx8025_handle_irq, 0, "rx8025", | |
543 | client); | |
3c2b9075 | 544 | if (err) { |
8a06513d AB |
545 | dev_err(&client->dev, "unable to request IRQ, alarms disabled\n"); |
546 | client->irq = 0; | |
3c2b9075 WG |
547 | } |
548 | } | |
549 | ||
3c2b9075 WG |
550 | rx8025->rtc->max_user_freq = 1; |
551 | ||
552 | err = rx8025_sysfs_register(&client->dev); | |
3c2b9075 WG |
553 | return err; |
554 | } | |
555 | ||
5a167f45 | 556 | static int rx8025_remove(struct i2c_client *client) |
3c2b9075 | 557 | { |
3c2b9075 | 558 | rx8025_sysfs_unregister(&client->dev); |
3c2b9075 WG |
559 | return 0; |
560 | } | |
561 | ||
562 | static struct i2c_driver rx8025_driver = { | |
563 | .driver = { | |
564 | .name = "rtc-rx8025", | |
3c2b9075 WG |
565 | }, |
566 | .probe = rx8025_probe, | |
5a167f45 | 567 | .remove = rx8025_remove, |
3c2b9075 WG |
568 | .id_table = rx8025_id, |
569 | }; | |
570 | ||
0abc9201 | 571 | module_i2c_driver(rx8025_driver); |
3c2b9075 WG |
572 | |
573 | MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>"); | |
574 | MODULE_DESCRIPTION("RX-8025 SA/NB RTC driver"); | |
575 | MODULE_LICENSE("GPL"); |