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