rtc-ds1374.c 17 KB

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