rtc-ds1374.c 17 KB

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