Commit | Line | Data |
---|---|---|
bf4994d7 SW |
1 | /* |
2 | * RTC client/driver for the Maxim/Dallas DS1374 Real-Time Clock over I2C | |
3 | * | |
4 | * Based on code by Randy Vinson <rvinson@mvista.com>, | |
5 | * which was based on the m41t00.c by Mark Greer <mgreer@mvista.com>. | |
6 | * | |
920f91e5 | 7 | * Copyright (C) 2014 Rose Technology |
bf4994d7 SW |
8 | * Copyright (C) 2006-2007 Freescale Semiconductor |
9 | * | |
10 | * 2005 (c) MontaVista Software, Inc. This file is licensed under | |
11 | * the terms of the GNU General Public License version 2. This program | |
12 | * is licensed "as is" without any warranty of any kind, whether express | |
13 | * or implied. | |
14 | */ | |
15 | /* | |
16 | * It would be more efficient to use i2c msgs/i2c_transfer directly but, as | |
ccf988b6 | 17 | * recommended in .../Documentation/i2c/writing-clients.rst section |
bf4994d7 SW |
18 | * "Sending and receiving", using SMBus level communication is preferred. |
19 | */ | |
20 | ||
a737e835 JP |
21 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
22 | ||
bf4994d7 SW |
23 | #include <linux/kernel.h> |
24 | #include <linux/module.h> | |
25 | #include <linux/interrupt.h> | |
26 | #include <linux/i2c.h> | |
27 | #include <linux/rtc.h> | |
28 | #include <linux/bcd.h> | |
29 | #include <linux/workqueue.h> | |
5a0e3ad6 | 30 | #include <linux/slab.h> |
bc96ba74 | 31 | #include <linux/pm.h> |
920f91e5 SA |
32 | #ifdef CONFIG_RTC_DRV_DS1374_WDT |
33 | #include <linux/fs.h> | |
34 | #include <linux/ioctl.h> | |
35 | #include <linux/miscdevice.h> | |
36 | #include <linux/reboot.h> | |
37 | #include <linux/watchdog.h> | |
38 | #endif | |
bf4994d7 SW |
39 | |
40 | #define DS1374_REG_TOD0 0x00 /* Time of Day */ | |
41 | #define DS1374_REG_TOD1 0x01 | |
42 | #define DS1374_REG_TOD2 0x02 | |
43 | #define DS1374_REG_TOD3 0x03 | |
44 | #define DS1374_REG_WDALM0 0x04 /* Watchdog/Alarm */ | |
45 | #define DS1374_REG_WDALM1 0x05 | |
46 | #define DS1374_REG_WDALM2 0x06 | |
47 | #define DS1374_REG_CR 0x07 /* Control */ | |
48 | #define DS1374_REG_CR_AIE 0x01 /* Alarm Int. Enable */ | |
49 | #define DS1374_REG_CR_WDALM 0x20 /* 1=Watchdog, 0=Alarm */ | |
50 | #define DS1374_REG_CR_WACE 0x40 /* WD/Alarm counter enable */ | |
51 | #define DS1374_REG_SR 0x08 /* Status */ | |
52 | #define DS1374_REG_SR_OSF 0x80 /* Oscillator Stop Flag */ | |
53 | #define DS1374_REG_SR_AF 0x01 /* Alarm Flag */ | |
54 | #define DS1374_REG_TCR 0x09 /* Trickle Charge */ | |
55 | ||
3760f736 | 56 | static const struct i2c_device_id ds1374_id[] = { |
f2eb4327 | 57 | { "ds1374", 0 }, |
3760f736 JD |
58 | { } |
59 | }; | |
60 | MODULE_DEVICE_TABLE(i2c, ds1374_id); | |
61 | ||
920f91e5 SA |
62 | #ifdef CONFIG_OF |
63 | static const struct of_device_id ds1374_of_match[] = { | |
64 | { .compatible = "dallas,ds1374" }, | |
65 | { } | |
66 | }; | |
67 | MODULE_DEVICE_TABLE(of, ds1374_of_match); | |
68 | #endif | |
69 | ||
bf4994d7 SW |
70 | struct ds1374 { |
71 | struct i2c_client *client; | |
72 | struct rtc_device *rtc; | |
73 | struct work_struct work; | |
74 | ||
75 | /* The mutex protects alarm operations, and prevents a race | |
76 | * between the enable_irq() in the workqueue and the free_irq() | |
77 | * in the remove function. | |
78 | */ | |
79 | struct mutex mutex; | |
80 | int exiting; | |
81 | }; | |
82 | ||
83 | static struct i2c_driver ds1374_driver; | |
84 | ||
85 | static int ds1374_read_rtc(struct i2c_client *client, u32 *time, | |
adc7b9b6 | 86 | int reg, int nbytes) |
bf4994d7 SW |
87 | { |
88 | u8 buf[4]; | |
89 | int ret; | |
90 | int i; | |
91 | ||
01835fad | 92 | if (WARN_ON(nbytes > 4)) |
bf4994d7 | 93 | return -EINVAL; |
bf4994d7 SW |
94 | |
95 | ret = i2c_smbus_read_i2c_block_data(client, reg, nbytes, buf); | |
96 | ||
97 | if (ret < 0) | |
98 | return ret; | |
99 | if (ret < nbytes) | |
100 | return -EIO; | |
101 | ||
102 | for (i = nbytes - 1, *time = 0; i >= 0; i--) | |
103 | *time = (*time << 8) | buf[i]; | |
104 | ||
105 | return 0; | |
106 | } | |
107 | ||
108 | static int ds1374_write_rtc(struct i2c_client *client, u32 time, | |
adc7b9b6 | 109 | int reg, int nbytes) |
bf4994d7 SW |
110 | { |
111 | u8 buf[4]; | |
112 | int i; | |
113 | ||
114 | if (nbytes > 4) { | |
115 | WARN_ON(1); | |
116 | return -EINVAL; | |
117 | } | |
118 | ||
119 | for (i = 0; i < nbytes; i++) { | |
120 | buf[i] = time & 0xff; | |
121 | time >>= 8; | |
122 | } | |
123 | ||
124 | return i2c_smbus_write_i2c_block_data(client, reg, nbytes, buf); | |
125 | } | |
126 | ||
127 | static int ds1374_check_rtc_status(struct i2c_client *client) | |
128 | { | |
129 | int ret = 0; | |
130 | int control, stat; | |
131 | ||
132 | stat = i2c_smbus_read_byte_data(client, DS1374_REG_SR); | |
133 | if (stat < 0) | |
134 | return stat; | |
135 | ||
136 | if (stat & DS1374_REG_SR_OSF) | |
137 | dev_warn(&client->dev, | |
adc7b9b6 | 138 | "oscillator discontinuity flagged, time unreliable\n"); |
bf4994d7 SW |
139 | |
140 | stat &= ~(DS1374_REG_SR_OSF | DS1374_REG_SR_AF); | |
141 | ||
142 | ret = i2c_smbus_write_byte_data(client, DS1374_REG_SR, stat); | |
143 | if (ret < 0) | |
144 | return ret; | |
145 | ||
146 | /* If the alarm is pending, clear it before requesting | |
147 | * the interrupt, so an interrupt event isn't reported | |
148 | * before everything is initialized. | |
149 | */ | |
150 | ||
151 | control = i2c_smbus_read_byte_data(client, DS1374_REG_CR); | |
152 | if (control < 0) | |
153 | return control; | |
154 | ||
155 | control &= ~(DS1374_REG_CR_WACE | DS1374_REG_CR_AIE); | |
156 | return i2c_smbus_write_byte_data(client, DS1374_REG_CR, control); | |
157 | } | |
158 | ||
159 | static int ds1374_read_time(struct device *dev, struct rtc_time *time) | |
160 | { | |
161 | struct i2c_client *client = to_i2c_client(dev); | |
162 | u32 itime; | |
163 | int ret; | |
164 | ||
165 | ret = ds1374_read_rtc(client, &itime, DS1374_REG_TOD0, 4); | |
166 | if (!ret) | |
ca824be9 | 167 | rtc_time64_to_tm(itime, time); |
bf4994d7 SW |
168 | |
169 | return ret; | |
170 | } | |
171 | ||
172 | static int ds1374_set_time(struct device *dev, struct rtc_time *time) | |
173 | { | |
174 | struct i2c_client *client = to_i2c_client(dev); | |
ca824be9 | 175 | unsigned long itime = rtc_tm_to_time64(time); |
bf4994d7 | 176 | |
bf4994d7 SW |
177 | return ds1374_write_rtc(client, itime, DS1374_REG_TOD0, 4); |
178 | } | |
179 | ||
920f91e5 | 180 | #ifndef CONFIG_RTC_DRV_DS1374_WDT |
bf4994d7 SW |
181 | /* The ds1374 has a decrementer for an alarm, rather than a comparator. |
182 | * If the time of day is changed, then the alarm will need to be | |
183 | * reset. | |
184 | */ | |
185 | static int ds1374_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) | |
186 | { | |
187 | struct i2c_client *client = to_i2c_client(dev); | |
188 | struct ds1374 *ds1374 = i2c_get_clientdata(client); | |
189 | u32 now, cur_alarm; | |
190 | int cr, sr; | |
191 | int ret = 0; | |
192 | ||
b42f9317 | 193 | if (client->irq <= 0) |
bf4994d7 SW |
194 | return -EINVAL; |
195 | ||
196 | mutex_lock(&ds1374->mutex); | |
197 | ||
198 | cr = ret = i2c_smbus_read_byte_data(client, DS1374_REG_CR); | |
199 | if (ret < 0) | |
200 | goto out; | |
201 | ||
202 | sr = ret = i2c_smbus_read_byte_data(client, DS1374_REG_SR); | |
203 | if (ret < 0) | |
204 | goto out; | |
205 | ||
206 | ret = ds1374_read_rtc(client, &now, DS1374_REG_TOD0, 4); | |
207 | if (ret) | |
208 | goto out; | |
209 | ||
210 | ret = ds1374_read_rtc(client, &cur_alarm, DS1374_REG_WDALM0, 3); | |
211 | if (ret) | |
212 | goto out; | |
213 | ||
ca824be9 | 214 | rtc_time64_to_tm(now + cur_alarm, &alarm->time); |
bf4994d7 SW |
215 | alarm->enabled = !!(cr & DS1374_REG_CR_WACE); |
216 | alarm->pending = !!(sr & DS1374_REG_SR_AF); | |
217 | ||
218 | out: | |
219 | mutex_unlock(&ds1374->mutex); | |
220 | return ret; | |
221 | } | |
222 | ||
223 | static int ds1374_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) | |
224 | { | |
225 | struct i2c_client *client = to_i2c_client(dev); | |
226 | struct ds1374 *ds1374 = i2c_get_clientdata(client); | |
227 | struct rtc_time now; | |
228 | unsigned long new_alarm, itime; | |
229 | int cr; | |
230 | int ret = 0; | |
231 | ||
b42f9317 | 232 | if (client->irq <= 0) |
bf4994d7 SW |
233 | return -EINVAL; |
234 | ||
235 | ret = ds1374_read_time(dev, &now); | |
236 | if (ret < 0) | |
237 | return ret; | |
238 | ||
ca824be9 AB |
239 | new_alarm = rtc_tm_to_time64(&alarm->time); |
240 | itime = rtc_tm_to_time64(&now); | |
bf4994d7 | 241 | |
bf4994d7 SW |
242 | /* This can happen due to races, in addition to dates that are |
243 | * truly in the past. To avoid requiring the caller to check for | |
244 | * races, dates in the past are assumed to be in the recent past | |
245 | * (i.e. not something that we'd rather the caller know about via | |
246 | * an error), and the alarm is set to go off as soon as possible. | |
247 | */ | |
fa7af8b1 | 248 | if (time_before_eq(new_alarm, itime)) |
bf4994d7 | 249 | new_alarm = 1; |
fa7af8b1 RK |
250 | else |
251 | new_alarm -= itime; | |
bf4994d7 SW |
252 | |
253 | mutex_lock(&ds1374->mutex); | |
254 | ||
255 | ret = cr = i2c_smbus_read_byte_data(client, DS1374_REG_CR); | |
256 | if (ret < 0) | |
257 | goto out; | |
258 | ||
259 | /* Disable any existing alarm before setting the new one | |
260 | * (or lack thereof). */ | |
261 | cr &= ~DS1374_REG_CR_WACE; | |
262 | ||
263 | ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr); | |
264 | if (ret < 0) | |
265 | goto out; | |
266 | ||
267 | ret = ds1374_write_rtc(client, new_alarm, DS1374_REG_WDALM0, 3); | |
268 | if (ret) | |
269 | goto out; | |
270 | ||
271 | if (alarm->enabled) { | |
272 | cr |= DS1374_REG_CR_WACE | DS1374_REG_CR_AIE; | |
273 | cr &= ~DS1374_REG_CR_WDALM; | |
274 | ||
275 | ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr); | |
276 | } | |
277 | ||
278 | out: | |
279 | mutex_unlock(&ds1374->mutex); | |
280 | return ret; | |
281 | } | |
920f91e5 | 282 | #endif |
bf4994d7 SW |
283 | |
284 | static irqreturn_t ds1374_irq(int irq, void *dev_id) | |
285 | { | |
286 | struct i2c_client *client = dev_id; | |
287 | struct ds1374 *ds1374 = i2c_get_clientdata(client); | |
288 | ||
289 | disable_irq_nosync(irq); | |
290 | schedule_work(&ds1374->work); | |
291 | return IRQ_HANDLED; | |
292 | } | |
293 | ||
294 | static void ds1374_work(struct work_struct *work) | |
295 | { | |
296 | struct ds1374 *ds1374 = container_of(work, struct ds1374, work); | |
297 | struct i2c_client *client = ds1374->client; | |
298 | int stat, control; | |
299 | ||
300 | mutex_lock(&ds1374->mutex); | |
301 | ||
302 | stat = i2c_smbus_read_byte_data(client, DS1374_REG_SR); | |
303 | if (stat < 0) | |
28df30e6 | 304 | goto unlock; |
bf4994d7 SW |
305 | |
306 | if (stat & DS1374_REG_SR_AF) { | |
307 | stat &= ~DS1374_REG_SR_AF; | |
308 | i2c_smbus_write_byte_data(client, DS1374_REG_SR, stat); | |
309 | ||
310 | control = i2c_smbus_read_byte_data(client, DS1374_REG_CR); | |
311 | if (control < 0) | |
312 | goto out; | |
313 | ||
314 | control &= ~(DS1374_REG_CR_WACE | DS1374_REG_CR_AIE); | |
315 | i2c_smbus_write_byte_data(client, DS1374_REG_CR, control); | |
316 | ||
bf4994d7 | 317 | rtc_update_irq(ds1374->rtc, 1, RTC_AF | RTC_IRQF); |
bf4994d7 SW |
318 | } |
319 | ||
320 | out: | |
321 | if (!ds1374->exiting) | |
322 | enable_irq(client->irq); | |
28df30e6 | 323 | unlock: |
bf4994d7 SW |
324 | mutex_unlock(&ds1374->mutex); |
325 | } | |
326 | ||
920f91e5 | 327 | #ifndef CONFIG_RTC_DRV_DS1374_WDT |
16380c15 | 328 | static int ds1374_alarm_irq_enable(struct device *dev, unsigned int enabled) |
bf4994d7 SW |
329 | { |
330 | struct i2c_client *client = to_i2c_client(dev); | |
331 | struct ds1374 *ds1374 = i2c_get_clientdata(client); | |
16380c15 | 332 | int ret; |
bf4994d7 SW |
333 | |
334 | mutex_lock(&ds1374->mutex); | |
335 | ||
16380c15 JS |
336 | ret = i2c_smbus_read_byte_data(client, DS1374_REG_CR); |
337 | if (ret < 0) | |
338 | goto out; | |
bf4994d7 | 339 | |
16380c15 | 340 | if (enabled) { |
bf4994d7 SW |
341 | ret |= DS1374_REG_CR_WACE | DS1374_REG_CR_AIE; |
342 | ret &= ~DS1374_REG_CR_WDALM; | |
16380c15 JS |
343 | } else { |
344 | ret &= ~DS1374_REG_CR_WACE; | |
bf4994d7 | 345 | } |
16380c15 | 346 | ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, ret); |
bf4994d7 SW |
347 | |
348 | out: | |
349 | mutex_unlock(&ds1374->mutex); | |
350 | return ret; | |
351 | } | |
920f91e5 | 352 | #endif |
bf4994d7 SW |
353 | |
354 | static const struct rtc_class_ops ds1374_rtc_ops = { | |
355 | .read_time = ds1374_read_time, | |
356 | .set_time = ds1374_set_time, | |
920f91e5 | 357 | #ifndef CONFIG_RTC_DRV_DS1374_WDT |
bf4994d7 SW |
358 | .read_alarm = ds1374_read_alarm, |
359 | .set_alarm = ds1374_set_alarm, | |
16380c15 | 360 | .alarm_irq_enable = ds1374_alarm_irq_enable, |
920f91e5 SA |
361 | #endif |
362 | }; | |
363 | ||
364 | #ifdef CONFIG_RTC_DRV_DS1374_WDT | |
365 | /* | |
366 | ***************************************************************************** | |
367 | * | |
368 | * Watchdog Driver | |
369 | * | |
370 | ***************************************************************************** | |
371 | */ | |
372 | static struct i2c_client *save_client; | |
373 | /* Default margin */ | |
374 | #define WD_TIMO 131762 | |
375 | ||
376 | #define DRV_NAME "DS1374 Watchdog" | |
377 | ||
378 | static int wdt_margin = WD_TIMO; | |
379 | static unsigned long wdt_is_open; | |
380 | module_param(wdt_margin, int, 0); | |
381 | MODULE_PARM_DESC(wdt_margin, "Watchdog timeout in seconds (default 32s)"); | |
382 | ||
383 | static const struct watchdog_info ds1374_wdt_info = { | |
384 | .identity = "DS1374 WTD", | |
385 | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | | |
386 | WDIOF_MAGICCLOSE, | |
387 | }; | |
388 | ||
389 | static int ds1374_wdt_settimeout(unsigned int timeout) | |
390 | { | |
391 | int ret = -ENOIOCTLCMD; | |
392 | int cr; | |
393 | ||
394 | ret = cr = i2c_smbus_read_byte_data(save_client, DS1374_REG_CR); | |
395 | if (ret < 0) | |
396 | goto out; | |
397 | ||
398 | /* Disable any existing watchdog/alarm before setting the new one */ | |
399 | cr &= ~DS1374_REG_CR_WACE; | |
400 | ||
401 | ret = i2c_smbus_write_byte_data(save_client, DS1374_REG_CR, cr); | |
402 | if (ret < 0) | |
403 | goto out; | |
404 | ||
405 | /* Set new watchdog time */ | |
406 | ret = ds1374_write_rtc(save_client, timeout, DS1374_REG_WDALM0, 3); | |
407 | if (ret) { | |
a737e835 | 408 | pr_info("couldn't set new watchdog time\n"); |
920f91e5 SA |
409 | goto out; |
410 | } | |
411 | ||
412 | /* Enable watchdog timer */ | |
413 | cr |= DS1374_REG_CR_WACE | DS1374_REG_CR_WDALM; | |
414 | cr &= ~DS1374_REG_CR_AIE; | |
415 | ||
416 | ret = i2c_smbus_write_byte_data(save_client, DS1374_REG_CR, cr); | |
417 | if (ret < 0) | |
418 | goto out; | |
419 | ||
420 | return 0; | |
421 | out: | |
422 | return ret; | |
423 | } | |
424 | ||
425 | ||
426 | /* | |
427 | * Reload the watchdog timer. (ie, pat the watchdog) | |
428 | */ | |
429 | static void ds1374_wdt_ping(void) | |
430 | { | |
431 | u32 val; | |
432 | int ret = 0; | |
433 | ||
434 | ret = ds1374_read_rtc(save_client, &val, DS1374_REG_WDALM0, 3); | |
435 | if (ret) | |
436 | pr_info("WD TICK FAIL!!!!!!!!!! %i\n", ret); | |
437 | } | |
438 | ||
439 | static void ds1374_wdt_disable(void) | |
440 | { | |
920f91e5 SA |
441 | int cr; |
442 | ||
443 | cr = i2c_smbus_read_byte_data(save_client, DS1374_REG_CR); | |
444 | /* Disable watchdog timer */ | |
445 | cr &= ~DS1374_REG_CR_WACE; | |
446 | ||
75859ab1 | 447 | i2c_smbus_write_byte_data(save_client, DS1374_REG_CR, cr); |
920f91e5 SA |
448 | } |
449 | ||
450 | /* | |
451 | * Watchdog device is opened, and watchdog starts running. | |
452 | */ | |
453 | static int ds1374_wdt_open(struct inode *inode, struct file *file) | |
454 | { | |
455 | struct ds1374 *ds1374 = i2c_get_clientdata(save_client); | |
456 | ||
457 | if (MINOR(inode->i_rdev) == WATCHDOG_MINOR) { | |
458 | mutex_lock(&ds1374->mutex); | |
459 | if (test_and_set_bit(0, &wdt_is_open)) { | |
460 | mutex_unlock(&ds1374->mutex); | |
461 | return -EBUSY; | |
462 | } | |
463 | /* | |
464 | * Activate | |
465 | */ | |
466 | wdt_is_open = 1; | |
467 | mutex_unlock(&ds1374->mutex); | |
c5bf68fe | 468 | return stream_open(inode, file); |
920f91e5 SA |
469 | } |
470 | return -ENODEV; | |
471 | } | |
472 | ||
473 | /* | |
474 | * Close the watchdog device. | |
475 | */ | |
476 | static int ds1374_wdt_release(struct inode *inode, struct file *file) | |
477 | { | |
478 | if (MINOR(inode->i_rdev) == WATCHDOG_MINOR) | |
479 | clear_bit(0, &wdt_is_open); | |
480 | ||
481 | return 0; | |
482 | } | |
483 | ||
484 | /* | |
485 | * Pat the watchdog whenever device is written to. | |
486 | */ | |
487 | static ssize_t ds1374_wdt_write(struct file *file, const char __user *data, | |
488 | size_t len, loff_t *ppos) | |
489 | { | |
490 | if (len) { | |
491 | ds1374_wdt_ping(); | |
492 | return 1; | |
493 | } | |
494 | return 0; | |
495 | } | |
496 | ||
497 | static ssize_t ds1374_wdt_read(struct file *file, char __user *data, | |
498 | size_t len, loff_t *ppos) | |
499 | { | |
500 | return 0; | |
501 | } | |
502 | ||
503 | /* | |
504 | * Handle commands from user-space. | |
505 | */ | |
506 | static long ds1374_wdt_ioctl(struct file *file, unsigned int cmd, | |
507 | unsigned long arg) | |
508 | { | |
509 | int new_margin, options; | |
510 | ||
511 | switch (cmd) { | |
512 | case WDIOC_GETSUPPORT: | |
513 | return copy_to_user((struct watchdog_info __user *)arg, | |
514 | &ds1374_wdt_info, sizeof(ds1374_wdt_info)) ? -EFAULT : 0; | |
515 | ||
516 | case WDIOC_GETSTATUS: | |
517 | case WDIOC_GETBOOTSTATUS: | |
518 | return put_user(0, (int __user *)arg); | |
519 | case WDIOC_KEEPALIVE: | |
520 | ds1374_wdt_ping(); | |
521 | return 0; | |
522 | case WDIOC_SETTIMEOUT: | |
523 | if (get_user(new_margin, (int __user *)arg)) | |
524 | return -EFAULT; | |
525 | ||
453d0744 MF |
526 | /* the hardware's tick rate is 4096 Hz, so |
527 | * the counter value needs to be scaled accordingly | |
528 | */ | |
529 | new_margin <<= 12; | |
920f91e5 SA |
530 | if (new_margin < 1 || new_margin > 16777216) |
531 | return -EINVAL; | |
532 | ||
533 | wdt_margin = new_margin; | |
534 | ds1374_wdt_settimeout(new_margin); | |
535 | ds1374_wdt_ping(); | |
536 | /* fallthrough */ | |
537 | case WDIOC_GETTIMEOUT: | |
453d0744 MF |
538 | /* when returning ... inverse is true */ |
539 | return put_user((wdt_margin >> 12), (int __user *)arg); | |
920f91e5 SA |
540 | case WDIOC_SETOPTIONS: |
541 | if (copy_from_user(&options, (int __user *)arg, sizeof(int))) | |
542 | return -EFAULT; | |
543 | ||
544 | if (options & WDIOS_DISABLECARD) { | |
a737e835 | 545 | pr_info("disable watchdog\n"); |
920f91e5 | 546 | ds1374_wdt_disable(); |
538c08f4 | 547 | return 0; |
920f91e5 SA |
548 | } |
549 | ||
550 | if (options & WDIOS_ENABLECARD) { | |
a737e835 | 551 | pr_info("enable watchdog\n"); |
920f91e5 SA |
552 | ds1374_wdt_settimeout(wdt_margin); |
553 | ds1374_wdt_ping(); | |
538c08f4 | 554 | return 0; |
920f91e5 | 555 | } |
920f91e5 SA |
556 | return -EINVAL; |
557 | } | |
558 | return -ENOTTY; | |
559 | } | |
560 | ||
561 | static long ds1374_wdt_unlocked_ioctl(struct file *file, unsigned int cmd, | |
562 | unsigned long arg) | |
563 | { | |
564 | int ret; | |
565 | struct ds1374 *ds1374 = i2c_get_clientdata(save_client); | |
566 | ||
567 | mutex_lock(&ds1374->mutex); | |
568 | ret = ds1374_wdt_ioctl(file, cmd, arg); | |
569 | mutex_unlock(&ds1374->mutex); | |
570 | ||
571 | return ret; | |
572 | } | |
573 | ||
574 | static int ds1374_wdt_notify_sys(struct notifier_block *this, | |
575 | unsigned long code, void *unused) | |
576 | { | |
577 | if (code == SYS_DOWN || code == SYS_HALT) | |
578 | /* Disable Watchdog */ | |
579 | ds1374_wdt_disable(); | |
580 | return NOTIFY_DONE; | |
581 | } | |
582 | ||
583 | static const struct file_operations ds1374_wdt_fops = { | |
584 | .owner = THIS_MODULE, | |
585 | .read = ds1374_wdt_read, | |
586 | .unlocked_ioctl = ds1374_wdt_unlocked_ioctl, | |
b6dfb247 | 587 | .compat_ioctl = compat_ptr_ioctl, |
920f91e5 SA |
588 | .write = ds1374_wdt_write, |
589 | .open = ds1374_wdt_open, | |
590 | .release = ds1374_wdt_release, | |
591 | .llseek = no_llseek, | |
592 | }; | |
593 | ||
594 | static struct miscdevice ds1374_miscdev = { | |
595 | .minor = WATCHDOG_MINOR, | |
596 | .name = "watchdog", | |
597 | .fops = &ds1374_wdt_fops, | |
bf4994d7 SW |
598 | }; |
599 | ||
920f91e5 SA |
600 | static struct notifier_block ds1374_wdt_notifier = { |
601 | .notifier_call = ds1374_wdt_notify_sys, | |
602 | }; | |
603 | ||
604 | #endif /*CONFIG_RTC_DRV_DS1374_WDT*/ | |
605 | /* | |
606 | ***************************************************************************** | |
607 | * | |
608 | * Driver Interface | |
609 | * | |
610 | ***************************************************************************** | |
611 | */ | |
d2653e92 JD |
612 | static int ds1374_probe(struct i2c_client *client, |
613 | const struct i2c_device_id *id) | |
bf4994d7 SW |
614 | { |
615 | struct ds1374 *ds1374; | |
616 | int ret; | |
617 | ||
d1a96639 | 618 | ds1374 = devm_kzalloc(&client->dev, sizeof(struct ds1374), GFP_KERNEL); |
bf4994d7 SW |
619 | if (!ds1374) |
620 | return -ENOMEM; | |
621 | ||
c11af813 AB |
622 | ds1374->rtc = devm_rtc_allocate_device(&client->dev); |
623 | if (IS_ERR(ds1374->rtc)) | |
624 | return PTR_ERR(ds1374->rtc); | |
625 | ||
bf4994d7 SW |
626 | ds1374->client = client; |
627 | i2c_set_clientdata(client, ds1374); | |
628 | ||
629 | INIT_WORK(&ds1374->work, ds1374_work); | |
630 | mutex_init(&ds1374->mutex); | |
631 | ||
632 | ret = ds1374_check_rtc_status(client); | |
633 | if (ret) | |
d1a96639 | 634 | return ret; |
bf4994d7 | 635 | |
b42f9317 | 636 | if (client->irq > 0) { |
d1a96639 | 637 | ret = devm_request_irq(&client->dev, client->irq, ds1374_irq, 0, |
adc7b9b6 | 638 | "ds1374", client); |
bf4994d7 SW |
639 | if (ret) { |
640 | dev_err(&client->dev, "unable to request IRQ\n"); | |
d1a96639 | 641 | return ret; |
bf4994d7 | 642 | } |
26b3c01f AV |
643 | |
644 | device_set_wakeup_capable(&client->dev, 1); | |
bf4994d7 SW |
645 | } |
646 | ||
c11af813 | 647 | ds1374->rtc->ops = &ds1374_rtc_ops; |
4136ff3a | 648 | ds1374->rtc->range_max = U32_MAX; |
c11af813 AB |
649 | |
650 | ret = rtc_register_device(ds1374->rtc); | |
651 | if (ret) | |
652 | return ret; | |
bf4994d7 | 653 | |
920f91e5 SA |
654 | #ifdef CONFIG_RTC_DRV_DS1374_WDT |
655 | save_client = client; | |
656 | ret = misc_register(&ds1374_miscdev); | |
657 | if (ret) | |
658 | return ret; | |
659 | ret = register_reboot_notifier(&ds1374_wdt_notifier); | |
660 | if (ret) { | |
661 | misc_deregister(&ds1374_miscdev); | |
662 | return ret; | |
663 | } | |
664 | ds1374_wdt_settimeout(131072); | |
665 | #endif | |
666 | ||
bf4994d7 | 667 | return 0; |
bf4994d7 SW |
668 | } |
669 | ||
5a167f45 | 670 | static int ds1374_remove(struct i2c_client *client) |
bf4994d7 SW |
671 | { |
672 | struct ds1374 *ds1374 = i2c_get_clientdata(client); | |
920f91e5 | 673 | #ifdef CONFIG_RTC_DRV_DS1374_WDT |
f368ed60 GKH |
674 | misc_deregister(&ds1374_miscdev); |
675 | ds1374_miscdev.parent = NULL; | |
920f91e5 SA |
676 | unregister_reboot_notifier(&ds1374_wdt_notifier); |
677 | #endif | |
bf4994d7 | 678 | |
b42f9317 | 679 | if (client->irq > 0) { |
bf4994d7 SW |
680 | mutex_lock(&ds1374->mutex); |
681 | ds1374->exiting = 1; | |
682 | mutex_unlock(&ds1374->mutex); | |
683 | ||
d1a96639 | 684 | devm_free_irq(&client->dev, client->irq, client); |
9db8995b | 685 | cancel_work_sync(&ds1374->work); |
bf4994d7 SW |
686 | } |
687 | ||
bf4994d7 SW |
688 | return 0; |
689 | } | |
690 | ||
8b80ef64 | 691 | #ifdef CONFIG_PM_SLEEP |
bc96ba74 | 692 | static int ds1374_suspend(struct device *dev) |
986e36a5 | 693 | { |
bc96ba74 MB |
694 | struct i2c_client *client = to_i2c_client(dev); |
695 | ||
0d9030a2 | 696 | if (client->irq > 0 && device_may_wakeup(&client->dev)) |
986e36a5 MP |
697 | enable_irq_wake(client->irq); |
698 | return 0; | |
699 | } | |
700 | ||
bc96ba74 | 701 | static int ds1374_resume(struct device *dev) |
986e36a5 | 702 | { |
bc96ba74 MB |
703 | struct i2c_client *client = to_i2c_client(dev); |
704 | ||
0d9030a2 | 705 | if (client->irq > 0 && device_may_wakeup(&client->dev)) |
986e36a5 MP |
706 | disable_irq_wake(client->irq); |
707 | return 0; | |
708 | } | |
8b80ef64 | 709 | #endif |
bc96ba74 MB |
710 | |
711 | static SIMPLE_DEV_PM_OPS(ds1374_pm, ds1374_suspend, ds1374_resume); | |
712 | ||
bf4994d7 SW |
713 | static struct i2c_driver ds1374_driver = { |
714 | .driver = { | |
715 | .name = "rtc-ds1374", | |
abac12e1 | 716 | .of_match_table = of_match_ptr(ds1374_of_match), |
8b80ef64 | 717 | .pm = &ds1374_pm, |
bf4994d7 SW |
718 | }, |
719 | .probe = ds1374_probe, | |
5a167f45 | 720 | .remove = ds1374_remove, |
3760f736 | 721 | .id_table = ds1374_id, |
bf4994d7 SW |
722 | }; |
723 | ||
0abc9201 | 724 | module_i2c_driver(ds1374_driver); |
bf4994d7 SW |
725 | |
726 | MODULE_AUTHOR("Scott Wood <scottwood@freescale.com>"); | |
727 | MODULE_DESCRIPTION("Maxim/Dallas DS1374 RTC Driver"); | |
728 | MODULE_LICENSE("GPL"); |