rtc-ds1374.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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. *
  7. * Copyright (C) 2014 Rose Technology
  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
  17. * recommened in .../Documentation/i2c/writing-clients section
  18. * "Sending and receiving", using SMBus level communication is preferred.
  19. */
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/i2c.h>
  25. #include <linux/rtc.h>
  26. #include <linux/bcd.h>
  27. #include <linux/workqueue.h>
  28. #include <linux/slab.h>
  29. #include <linux/pm.h>
  30. #ifdef CONFIG_RTC_DRV_DS1374_WDT
  31. #include <linux/fs.h>
  32. #include <linux/ioctl.h>
  33. #include <linux/miscdevice.h>
  34. #include <linux/reboot.h>
  35. #include <linux/watchdog.h>
  36. #endif
  37. #define DS1374_REG_TOD0 0x00 /* Time of Day */
  38. #define DS1374_REG_TOD1 0x01
  39. #define DS1374_REG_TOD2 0x02
  40. #define DS1374_REG_TOD3 0x03
  41. #define DS1374_REG_WDALM0 0x04 /* Watchdog/Alarm */
  42. #define DS1374_REG_WDALM1 0x05
  43. #define DS1374_REG_WDALM2 0x06
  44. #define DS1374_REG_CR 0x07 /* Control */
  45. #define DS1374_REG_CR_AIE 0x01 /* Alarm Int. Enable */
  46. #define DS1374_REG_CR_WDALM 0x20 /* 1=Watchdog, 0=Alarm */
  47. #define DS1374_REG_CR_WACE 0x40 /* WD/Alarm counter enable */
  48. #define DS1374_REG_SR 0x08 /* Status */
  49. #define DS1374_REG_SR_OSF 0x80 /* Oscillator Stop Flag */
  50. #define DS1374_REG_SR_AF 0x01 /* Alarm Flag */
  51. #define DS1374_REG_TCR 0x09 /* Trickle Charge */
  52. static const struct i2c_device_id ds1374_id[] = {
  53. { "ds1374", 0 },
  54. { }
  55. };
  56. MODULE_DEVICE_TABLE(i2c, ds1374_id);
  57. #ifdef CONFIG_OF
  58. static const struct of_device_id ds1374_of_match[] = {
  59. { .compatible = "dallas,ds1374" },
  60. { }
  61. };
  62. MODULE_DEVICE_TABLE(of, ds1374_of_match);
  63. #endif
  64. struct ds1374 {
  65. struct i2c_client *client;
  66. struct rtc_device *rtc;
  67. struct work_struct work;
  68. /* The mutex protects alarm operations, and prevents a race
  69. * between the enable_irq() in the workqueue and the free_irq()
  70. * in the remove function.
  71. */
  72. struct mutex mutex;
  73. int exiting;
  74. };
  75. static struct i2c_driver ds1374_driver;
  76. static int ds1374_read_rtc(struct i2c_client *client, u32 *time,
  77. int reg, int nbytes)
  78. {
  79. u8 buf[4];
  80. int ret;
  81. int i;
  82. if (WARN_ON(nbytes > 4))
  83. return -EINVAL;
  84. ret = i2c_smbus_read_i2c_block_data(client, reg, nbytes, buf);
  85. if (ret < 0)
  86. return ret;
  87. if (ret < nbytes)
  88. return -EIO;
  89. for (i = nbytes - 1, *time = 0; i >= 0; i--)
  90. *time = (*time << 8) | buf[i];
  91. return 0;
  92. }
  93. static int ds1374_write_rtc(struct i2c_client *client, u32 time,
  94. int reg, int nbytes)
  95. {
  96. u8 buf[4];
  97. int i;
  98. if (nbytes > 4) {
  99. WARN_ON(1);
  100. return -EINVAL;
  101. }
  102. for (i = 0; i < nbytes; i++) {
  103. buf[i] = time & 0xff;
  104. time >>= 8;
  105. }
  106. return i2c_smbus_write_i2c_block_data(client, reg, nbytes, buf);
  107. }
  108. static int ds1374_check_rtc_status(struct i2c_client *client)
  109. {
  110. int ret = 0;
  111. int control, stat;
  112. stat = i2c_smbus_read_byte_data(client, DS1374_REG_SR);
  113. if (stat < 0)
  114. return stat;
  115. if (stat & DS1374_REG_SR_OSF)
  116. dev_warn(&client->dev,
  117. "oscillator discontinuity flagged, time unreliable\n");
  118. stat &= ~(DS1374_REG_SR_OSF | DS1374_REG_SR_AF);
  119. ret = i2c_smbus_write_byte_data(client, DS1374_REG_SR, stat);
  120. if (ret < 0)
  121. return ret;
  122. /* If the alarm is pending, clear it before requesting
  123. * the interrupt, so an interrupt event isn't reported
  124. * before everything is initialized.
  125. */
  126. control = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
  127. if (control < 0)
  128. return control;
  129. control &= ~(DS1374_REG_CR_WACE | DS1374_REG_CR_AIE);
  130. return i2c_smbus_write_byte_data(client, DS1374_REG_CR, control);
  131. }
  132. static int ds1374_read_time(struct device *dev, struct rtc_time *time)
  133. {
  134. struct i2c_client *client = to_i2c_client(dev);
  135. u32 itime;
  136. int ret;
  137. ret = ds1374_read_rtc(client, &itime, DS1374_REG_TOD0, 4);
  138. if (!ret)
  139. rtc_time_to_tm(itime, time);
  140. return ret;
  141. }
  142. static int ds1374_set_time(struct device *dev, struct rtc_time *time)
  143. {
  144. struct i2c_client *client = to_i2c_client(dev);
  145. unsigned long itime;
  146. rtc_tm_to_time(time, &itime);
  147. return ds1374_write_rtc(client, itime, DS1374_REG_TOD0, 4);
  148. }
  149. #ifndef CONFIG_RTC_DRV_DS1374_WDT
  150. /* The ds1374 has a decrementer for an alarm, rather than a comparator.
  151. * If the time of day is changed, then the alarm will need to be
  152. * reset.
  153. */
  154. static int ds1374_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  155. {
  156. struct i2c_client *client = to_i2c_client(dev);
  157. struct ds1374 *ds1374 = i2c_get_clientdata(client);
  158. u32 now, cur_alarm;
  159. int cr, sr;
  160. int ret = 0;
  161. if (client->irq <= 0)
  162. return -EINVAL;
  163. mutex_lock(&ds1374->mutex);
  164. cr = ret = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
  165. if (ret < 0)
  166. goto out;
  167. sr = ret = i2c_smbus_read_byte_data(client, DS1374_REG_SR);
  168. if (ret < 0)
  169. goto out;
  170. ret = ds1374_read_rtc(client, &now, DS1374_REG_TOD0, 4);
  171. if (ret)
  172. goto out;
  173. ret = ds1374_read_rtc(client, &cur_alarm, DS1374_REG_WDALM0, 3);
  174. if (ret)
  175. goto out;
  176. rtc_time_to_tm(now + cur_alarm, &alarm->time);
  177. alarm->enabled = !!(cr & DS1374_REG_CR_WACE);
  178. alarm->pending = !!(sr & DS1374_REG_SR_AF);
  179. out:
  180. mutex_unlock(&ds1374->mutex);
  181. return ret;
  182. }
  183. static int ds1374_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  184. {
  185. struct i2c_client *client = to_i2c_client(dev);
  186. struct ds1374 *ds1374 = i2c_get_clientdata(client);
  187. struct rtc_time now;
  188. unsigned long new_alarm, itime;
  189. int cr;
  190. int ret = 0;
  191. if (client->irq <= 0)
  192. return -EINVAL;
  193. ret = ds1374_read_time(dev, &now);
  194. if (ret < 0)
  195. return ret;
  196. rtc_tm_to_time(&alarm->time, &new_alarm);
  197. rtc_tm_to_time(&now, &itime);
  198. /* This can happen due to races, in addition to dates that are
  199. * truly in the past. To avoid requiring the caller to check for
  200. * races, dates in the past are assumed to be in the recent past
  201. * (i.e. not something that we'd rather the caller know about via
  202. * an error), and the alarm is set to go off as soon as possible.
  203. */
  204. if (time_before_eq(new_alarm, itime))
  205. new_alarm = 1;
  206. else
  207. new_alarm -= itime;
  208. mutex_lock(&ds1374->mutex);
  209. ret = cr = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
  210. if (ret < 0)
  211. goto out;
  212. /* Disable any existing alarm before setting the new one
  213. * (or lack thereof). */
  214. cr &= ~DS1374_REG_CR_WACE;
  215. ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
  216. if (ret < 0)
  217. goto out;
  218. ret = ds1374_write_rtc(client, new_alarm, DS1374_REG_WDALM0, 3);
  219. if (ret)
  220. goto out;
  221. if (alarm->enabled) {
  222. cr |= DS1374_REG_CR_WACE | DS1374_REG_CR_AIE;
  223. cr &= ~DS1374_REG_CR_WDALM;
  224. ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
  225. }
  226. out:
  227. mutex_unlock(&ds1374->mutex);
  228. return ret;
  229. }
  230. #endif
  231. static irqreturn_t ds1374_irq(int irq, void *dev_id)
  232. {
  233. struct i2c_client *client = dev_id;
  234. struct ds1374 *ds1374 = i2c_get_clientdata(client);
  235. disable_irq_nosync(irq);
  236. schedule_work(&ds1374->work);
  237. return IRQ_HANDLED;
  238. }
  239. static void ds1374_work(struct work_struct *work)
  240. {
  241. struct ds1374 *ds1374 = container_of(work, struct ds1374, work);
  242. struct i2c_client *client = ds1374->client;
  243. int stat, control;
  244. mutex_lock(&ds1374->mutex);
  245. stat = i2c_smbus_read_byte_data(client, DS1374_REG_SR);
  246. if (stat < 0)
  247. goto unlock;
  248. if (stat & DS1374_REG_SR_AF) {
  249. stat &= ~DS1374_REG_SR_AF;
  250. i2c_smbus_write_byte_data(client, DS1374_REG_SR, stat);
  251. control = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
  252. if (control < 0)
  253. goto out;
  254. control &= ~(DS1374_REG_CR_WACE | DS1374_REG_CR_AIE);
  255. i2c_smbus_write_byte_data(client, DS1374_REG_CR, control);
  256. rtc_update_irq(ds1374->rtc, 1, RTC_AF | RTC_IRQF);
  257. }
  258. out:
  259. if (!ds1374->exiting)
  260. enable_irq(client->irq);
  261. unlock:
  262. mutex_unlock(&ds1374->mutex);
  263. }
  264. #ifndef CONFIG_RTC_DRV_DS1374_WDT
  265. static int ds1374_alarm_irq_enable(struct device *dev, unsigned int enabled)
  266. {
  267. struct i2c_client *client = to_i2c_client(dev);
  268. struct ds1374 *ds1374 = i2c_get_clientdata(client);
  269. int ret;
  270. mutex_lock(&ds1374->mutex);
  271. ret = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
  272. if (ret < 0)
  273. goto out;
  274. if (enabled) {
  275. ret |= DS1374_REG_CR_WACE | DS1374_REG_CR_AIE;
  276. ret &= ~DS1374_REG_CR_WDALM;
  277. } else {
  278. ret &= ~DS1374_REG_CR_WACE;
  279. }
  280. ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, ret);
  281. out:
  282. mutex_unlock(&ds1374->mutex);
  283. return ret;
  284. }
  285. #endif
  286. static const struct rtc_class_ops ds1374_rtc_ops = {
  287. .read_time = ds1374_read_time,
  288. .set_time = ds1374_set_time,
  289. #ifndef CONFIG_RTC_DRV_DS1374_WDT
  290. .read_alarm = ds1374_read_alarm,
  291. .set_alarm = ds1374_set_alarm,
  292. .alarm_irq_enable = ds1374_alarm_irq_enable,
  293. #endif
  294. };
  295. #ifdef CONFIG_RTC_DRV_DS1374_WDT
  296. /*
  297. *****************************************************************************
  298. *
  299. * Watchdog Driver
  300. *
  301. *****************************************************************************
  302. */
  303. static struct i2c_client *save_client;
  304. /* Default margin */
  305. #define WD_TIMO 131762
  306. #define DRV_NAME "DS1374 Watchdog"
  307. static int wdt_margin = WD_TIMO;
  308. static unsigned long wdt_is_open;
  309. module_param(wdt_margin, int, 0);
  310. MODULE_PARM_DESC(wdt_margin, "Watchdog timeout in seconds (default 32s)");
  311. static const struct watchdog_info ds1374_wdt_info = {
  312. .identity = "DS1374 WTD",
  313. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
  314. WDIOF_MAGICCLOSE,
  315. };
  316. static int ds1374_wdt_settimeout(unsigned int timeout)
  317. {
  318. int ret = -ENOIOCTLCMD;
  319. int cr;
  320. ret = cr = i2c_smbus_read_byte_data(save_client, DS1374_REG_CR);
  321. if (ret < 0)
  322. goto out;
  323. /* Disable any existing watchdog/alarm before setting the new one */
  324. cr &= ~DS1374_REG_CR_WACE;
  325. ret = i2c_smbus_write_byte_data(save_client, DS1374_REG_CR, cr);
  326. if (ret < 0)
  327. goto out;
  328. /* Set new watchdog time */
  329. ret = ds1374_write_rtc(save_client, timeout, DS1374_REG_WDALM0, 3);
  330. if (ret) {
  331. pr_info("couldn't set new watchdog time\n");
  332. goto out;
  333. }
  334. /* Enable watchdog timer */
  335. cr |= DS1374_REG_CR_WACE | DS1374_REG_CR_WDALM;
  336. cr &= ~DS1374_REG_CR_AIE;
  337. ret = i2c_smbus_write_byte_data(save_client, DS1374_REG_CR, cr);
  338. if (ret < 0)
  339. goto out;
  340. return 0;
  341. out:
  342. return ret;
  343. }
  344. /*
  345. * Reload the watchdog timer. (ie, pat the watchdog)
  346. */
  347. static void ds1374_wdt_ping(void)
  348. {
  349. u32 val;
  350. int ret = 0;
  351. ret = ds1374_read_rtc(save_client, &val, DS1374_REG_WDALM0, 3);
  352. if (ret)
  353. pr_info("WD TICK FAIL!!!!!!!!!! %i\n", ret);
  354. }
  355. static void ds1374_wdt_disable(void)
  356. {
  357. int ret = -ENOIOCTLCMD;
  358. int cr;
  359. cr = i2c_smbus_read_byte_data(save_client, DS1374_REG_CR);
  360. /* Disable watchdog timer */
  361. cr &= ~DS1374_REG_CR_WACE;
  362. ret = i2c_smbus_write_byte_data(save_client, DS1374_REG_CR, cr);
  363. }
  364. /*
  365. * Watchdog device is opened, and watchdog starts running.
  366. */
  367. static int ds1374_wdt_open(struct inode *inode, struct file *file)
  368. {
  369. struct ds1374 *ds1374 = i2c_get_clientdata(save_client);
  370. if (MINOR(inode->i_rdev) == WATCHDOG_MINOR) {
  371. mutex_lock(&ds1374->mutex);
  372. if (test_and_set_bit(0, &wdt_is_open)) {
  373. mutex_unlock(&ds1374->mutex);
  374. return -EBUSY;
  375. }
  376. /*
  377. * Activate
  378. */
  379. wdt_is_open = 1;
  380. mutex_unlock(&ds1374->mutex);
  381. return nonseekable_open(inode, file);
  382. }
  383. return -ENODEV;
  384. }
  385. /*
  386. * Close the watchdog device.
  387. */
  388. static int ds1374_wdt_release(struct inode *inode, struct file *file)
  389. {
  390. if (MINOR(inode->i_rdev) == WATCHDOG_MINOR)
  391. clear_bit(0, &wdt_is_open);
  392. return 0;
  393. }
  394. /*
  395. * Pat the watchdog whenever device is written to.
  396. */
  397. static ssize_t ds1374_wdt_write(struct file *file, const char __user *data,
  398. size_t len, loff_t *ppos)
  399. {
  400. if (len) {
  401. ds1374_wdt_ping();
  402. return 1;
  403. }
  404. return 0;
  405. }
  406. static ssize_t ds1374_wdt_read(struct file *file, char __user *data,
  407. size_t len, loff_t *ppos)
  408. {
  409. return 0;
  410. }
  411. /*
  412. * Handle commands from user-space.
  413. */
  414. static long ds1374_wdt_ioctl(struct file *file, unsigned int cmd,
  415. unsigned long arg)
  416. {
  417. int new_margin, options;
  418. switch (cmd) {
  419. case WDIOC_GETSUPPORT:
  420. return copy_to_user((struct watchdog_info __user *)arg,
  421. &ds1374_wdt_info, sizeof(ds1374_wdt_info)) ? -EFAULT : 0;
  422. case WDIOC_GETSTATUS:
  423. case WDIOC_GETBOOTSTATUS:
  424. return put_user(0, (int __user *)arg);
  425. case WDIOC_KEEPALIVE:
  426. ds1374_wdt_ping();
  427. return 0;
  428. case WDIOC_SETTIMEOUT:
  429. if (get_user(new_margin, (int __user *)arg))
  430. return -EFAULT;
  431. /* the hardware's tick rate is 4096 Hz, so
  432. * the counter value needs to be scaled accordingly
  433. */
  434. new_margin <<= 12;
  435. if (new_margin < 1 || new_margin > 16777216)
  436. return -EINVAL;
  437. wdt_margin = new_margin;
  438. ds1374_wdt_settimeout(new_margin);
  439. ds1374_wdt_ping();
  440. /* fallthrough */
  441. case WDIOC_GETTIMEOUT:
  442. /* when returning ... inverse is true */
  443. return put_user((wdt_margin >> 12), (int __user *)arg);
  444. case WDIOC_SETOPTIONS:
  445. if (copy_from_user(&options, (int __user *)arg, sizeof(int)))
  446. return -EFAULT;
  447. if (options & WDIOS_DISABLECARD) {
  448. pr_info("disable watchdog\n");
  449. ds1374_wdt_disable();
  450. return 0;
  451. }
  452. if (options & WDIOS_ENABLECARD) {
  453. pr_info("enable watchdog\n");
  454. ds1374_wdt_settimeout(wdt_margin);
  455. ds1374_wdt_ping();
  456. return 0;
  457. }
  458. return -EINVAL;
  459. }
  460. return -ENOTTY;
  461. }
  462. static long ds1374_wdt_unlocked_ioctl(struct file *file, unsigned int cmd,
  463. unsigned long arg)
  464. {
  465. int ret;
  466. struct ds1374 *ds1374 = i2c_get_clientdata(save_client);
  467. mutex_lock(&ds1374->mutex);
  468. ret = ds1374_wdt_ioctl(file, cmd, arg);
  469. mutex_unlock(&ds1374->mutex);
  470. return ret;
  471. }
  472. static int ds1374_wdt_notify_sys(struct notifier_block *this,
  473. unsigned long code, void *unused)
  474. {
  475. if (code == SYS_DOWN || code == SYS_HALT)
  476. /* Disable Watchdog */
  477. ds1374_wdt_disable();
  478. return NOTIFY_DONE;
  479. }
  480. static const struct file_operations ds1374_wdt_fops = {
  481. .owner = THIS_MODULE,
  482. .read = ds1374_wdt_read,
  483. .unlocked_ioctl = ds1374_wdt_unlocked_ioctl,
  484. .write = ds1374_wdt_write,
  485. .open = ds1374_wdt_open,
  486. .release = ds1374_wdt_release,
  487. .llseek = no_llseek,
  488. };
  489. static struct miscdevice ds1374_miscdev = {
  490. .minor = WATCHDOG_MINOR,
  491. .name = "watchdog",
  492. .fops = &ds1374_wdt_fops,
  493. };
  494. static struct notifier_block ds1374_wdt_notifier = {
  495. .notifier_call = ds1374_wdt_notify_sys,
  496. };
  497. #endif /*CONFIG_RTC_DRV_DS1374_WDT*/
  498. /*
  499. *****************************************************************************
  500. *
  501. * Driver Interface
  502. *
  503. *****************************************************************************
  504. */
  505. static int ds1374_probe(struct i2c_client *client,
  506. const struct i2c_device_id *id)
  507. {
  508. struct ds1374 *ds1374;
  509. int ret;
  510. ds1374 = devm_kzalloc(&client->dev, sizeof(struct ds1374), GFP_KERNEL);
  511. if (!ds1374)
  512. return -ENOMEM;
  513. ds1374->client = client;
  514. i2c_set_clientdata(client, ds1374);
  515. INIT_WORK(&ds1374->work, ds1374_work);
  516. mutex_init(&ds1374->mutex);
  517. ret = ds1374_check_rtc_status(client);
  518. if (ret)
  519. return ret;
  520. if (client->irq > 0) {
  521. ret = devm_request_irq(&client->dev, client->irq, ds1374_irq, 0,
  522. "ds1374", client);
  523. if (ret) {
  524. dev_err(&client->dev, "unable to request IRQ\n");
  525. return ret;
  526. }
  527. device_set_wakeup_capable(&client->dev, 1);
  528. }
  529. ds1374->rtc = devm_rtc_device_register(&client->dev, client->name,
  530. &ds1374_rtc_ops, THIS_MODULE);
  531. if (IS_ERR(ds1374->rtc)) {
  532. dev_err(&client->dev, "unable to register the class device\n");
  533. return PTR_ERR(ds1374->rtc);
  534. }
  535. #ifdef CONFIG_RTC_DRV_DS1374_WDT
  536. save_client = client;
  537. ret = misc_register(&ds1374_miscdev);
  538. if (ret)
  539. return ret;
  540. ret = register_reboot_notifier(&ds1374_wdt_notifier);
  541. if (ret) {
  542. misc_deregister(&ds1374_miscdev);
  543. return ret;
  544. }
  545. ds1374_wdt_settimeout(131072);
  546. #endif
  547. return 0;
  548. }
  549. static int ds1374_remove(struct i2c_client *client)
  550. {
  551. struct ds1374 *ds1374 = i2c_get_clientdata(client);
  552. #ifdef CONFIG_RTC_DRV_DS1374_WDT
  553. misc_deregister(&ds1374_miscdev);
  554. ds1374_miscdev.parent = NULL;
  555. unregister_reboot_notifier(&ds1374_wdt_notifier);
  556. #endif
  557. if (client->irq > 0) {
  558. mutex_lock(&ds1374->mutex);
  559. ds1374->exiting = 1;
  560. mutex_unlock(&ds1374->mutex);
  561. devm_free_irq(&client->dev, client->irq, client);
  562. cancel_work_sync(&ds1374->work);
  563. }
  564. return 0;
  565. }
  566. #ifdef CONFIG_PM_SLEEP
  567. static int ds1374_suspend(struct device *dev)
  568. {
  569. struct i2c_client *client = to_i2c_client(dev);
  570. if (client->irq > 0 && device_may_wakeup(&client->dev))
  571. enable_irq_wake(client->irq);
  572. return 0;
  573. }
  574. static int ds1374_resume(struct device *dev)
  575. {
  576. struct i2c_client *client = to_i2c_client(dev);
  577. if (client->irq > 0 && device_may_wakeup(&client->dev))
  578. disable_irq_wake(client->irq);
  579. return 0;
  580. }
  581. #endif
  582. static SIMPLE_DEV_PM_OPS(ds1374_pm, ds1374_suspend, ds1374_resume);
  583. static struct i2c_driver ds1374_driver = {
  584. .driver = {
  585. .name = "rtc-ds1374",
  586. .of_match_table = of_match_ptr(ds1374_of_match),
  587. .pm = &ds1374_pm,
  588. },
  589. .probe = ds1374_probe,
  590. .remove = ds1374_remove,
  591. .id_table = ds1374_id,
  592. };
  593. module_i2c_driver(ds1374_driver);
  594. MODULE_AUTHOR("Scott Wood <scottwood@freescale.com>");
  595. MODULE_DESCRIPTION("Maxim/Dallas DS1374 RTC Driver");
  596. MODULE_LICENSE("GPL");