rtc-ds1343.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. /* rtc-ds1343.c
  2. *
  3. * Driver for Dallas Semiconductor DS1343 Low Current, SPI Compatible
  4. * Real Time Clock
  5. *
  6. * Author : Raghavendra Chandra Ganiga <ravi23ganiga@gmail.com>
  7. * Ankur Srivastava <sankurece@gmail.com> : DS1343 Nvram Support
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. */
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/device.h>
  18. #include <linux/spi/spi.h>
  19. #include <linux/regmap.h>
  20. #include <linux/rtc.h>
  21. #include <linux/bcd.h>
  22. #include <linux/pm.h>
  23. #include <linux/slab.h>
  24. #define DS1343_DRV_VERSION "01.00"
  25. #define DALLAS_MAXIM_DS1343 0
  26. #define DALLAS_MAXIM_DS1344 1
  27. /* RTC DS1343 Registers */
  28. #define DS1343_SECONDS_REG 0x00
  29. #define DS1343_MINUTES_REG 0x01
  30. #define DS1343_HOURS_REG 0x02
  31. #define DS1343_DAY_REG 0x03
  32. #define DS1343_DATE_REG 0x04
  33. #define DS1343_MONTH_REG 0x05
  34. #define DS1343_YEAR_REG 0x06
  35. #define DS1343_ALM0_SEC_REG 0x07
  36. #define DS1343_ALM0_MIN_REG 0x08
  37. #define DS1343_ALM0_HOUR_REG 0x09
  38. #define DS1343_ALM0_DAY_REG 0x0A
  39. #define DS1343_ALM1_SEC_REG 0x0B
  40. #define DS1343_ALM1_MIN_REG 0x0C
  41. #define DS1343_ALM1_HOUR_REG 0x0D
  42. #define DS1343_ALM1_DAY_REG 0x0E
  43. #define DS1343_CONTROL_REG 0x0F
  44. #define DS1343_STATUS_REG 0x10
  45. #define DS1343_TRICKLE_REG 0x11
  46. #define DS1343_NVRAM 0x20
  47. #define DS1343_NVRAM_LEN 96
  48. /* DS1343 Control Registers bits */
  49. #define DS1343_EOSC 0x80
  50. #define DS1343_DOSF 0x20
  51. #define DS1343_EGFIL 0x10
  52. #define DS1343_SQW 0x08
  53. #define DS1343_INTCN 0x04
  54. #define DS1343_A1IE 0x02
  55. #define DS1343_A0IE 0x01
  56. /* DS1343 Status Registers bits */
  57. #define DS1343_OSF 0x80
  58. #define DS1343_IRQF1 0x02
  59. #define DS1343_IRQF0 0x01
  60. /* DS1343 Trickle Charger Registers bits */
  61. #define DS1343_TRICKLE_MAGIC 0xa0
  62. #define DS1343_TRICKLE_DS1 0x08
  63. #define DS1343_TRICKLE_1K 0x01
  64. #define DS1343_TRICKLE_2K 0x02
  65. #define DS1343_TRICKLE_4K 0x03
  66. static const struct spi_device_id ds1343_id[] = {
  67. { "ds1343", DALLAS_MAXIM_DS1343 },
  68. { "ds1344", DALLAS_MAXIM_DS1344 },
  69. { }
  70. };
  71. MODULE_DEVICE_TABLE(spi, ds1343_id);
  72. struct ds1343_priv {
  73. struct spi_device *spi;
  74. struct rtc_device *rtc;
  75. struct regmap *map;
  76. struct mutex mutex;
  77. unsigned int irqen;
  78. int irq;
  79. int alarm_sec;
  80. int alarm_min;
  81. int alarm_hour;
  82. int alarm_mday;
  83. };
  84. static int ds1343_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
  85. {
  86. switch (cmd) {
  87. #ifdef RTC_SET_CHARGE
  88. case RTC_SET_CHARGE:
  89. {
  90. int val;
  91. if (copy_from_user(&val, (int __user *)arg, sizeof(int)))
  92. return -EFAULT;
  93. return regmap_write(priv->map, DS1343_TRICKLE_REG, val);
  94. }
  95. break;
  96. #endif
  97. }
  98. return -ENOIOCTLCMD;
  99. }
  100. static ssize_t ds1343_show_glitchfilter(struct device *dev,
  101. struct device_attribute *attr, char *buf)
  102. {
  103. struct ds1343_priv *priv = dev_get_drvdata(dev);
  104. int glitch_filt_status, data;
  105. regmap_read(priv->map, DS1343_CONTROL_REG, &data);
  106. glitch_filt_status = !!(data & DS1343_EGFIL);
  107. if (glitch_filt_status)
  108. return sprintf(buf, "enabled\n");
  109. else
  110. return sprintf(buf, "disabled\n");
  111. }
  112. static ssize_t ds1343_store_glitchfilter(struct device *dev,
  113. struct device_attribute *attr,
  114. const char *buf, size_t count)
  115. {
  116. struct ds1343_priv *priv = dev_get_drvdata(dev);
  117. int data;
  118. regmap_read(priv->map, DS1343_CONTROL_REG, &data);
  119. if (strncmp(buf, "enabled", 7) == 0)
  120. data |= DS1343_EGFIL;
  121. else if (strncmp(buf, "disabled", 8) == 0)
  122. data &= ~(DS1343_EGFIL);
  123. else
  124. return -EINVAL;
  125. regmap_write(priv->map, DS1343_CONTROL_REG, data);
  126. return count;
  127. }
  128. static DEVICE_ATTR(glitch_filter, S_IRUGO | S_IWUSR, ds1343_show_glitchfilter,
  129. ds1343_store_glitchfilter);
  130. static ssize_t ds1343_nvram_write(struct file *filp, struct kobject *kobj,
  131. struct bin_attribute *attr,
  132. char *buf, loff_t off, size_t count)
  133. {
  134. int ret;
  135. unsigned char address;
  136. struct device *dev = kobj_to_dev(kobj);
  137. struct ds1343_priv *priv = dev_get_drvdata(dev);
  138. address = DS1343_NVRAM + off;
  139. ret = regmap_bulk_write(priv->map, address, buf, count);
  140. if (ret < 0)
  141. dev_err(&priv->spi->dev, "Error in nvram write %d", ret);
  142. return (ret < 0) ? ret : count;
  143. }
  144. static ssize_t ds1343_nvram_read(struct file *filp, struct kobject *kobj,
  145. struct bin_attribute *attr,
  146. char *buf, loff_t off, size_t count)
  147. {
  148. int ret;
  149. unsigned char address;
  150. struct device *dev = kobj_to_dev(kobj);
  151. struct ds1343_priv *priv = dev_get_drvdata(dev);
  152. address = DS1343_NVRAM + off;
  153. ret = regmap_bulk_read(priv->map, address, buf, count);
  154. if (ret < 0)
  155. dev_err(&priv->spi->dev, "Error in nvram read %d\n", ret);
  156. return (ret < 0) ? ret : count;
  157. }
  158. static struct bin_attribute nvram_attr = {
  159. .attr.name = "nvram",
  160. .attr.mode = S_IRUGO | S_IWUSR,
  161. .read = ds1343_nvram_read,
  162. .write = ds1343_nvram_write,
  163. .size = DS1343_NVRAM_LEN,
  164. };
  165. static ssize_t ds1343_show_alarmstatus(struct device *dev,
  166. struct device_attribute *attr, char *buf)
  167. {
  168. struct ds1343_priv *priv = dev_get_drvdata(dev);
  169. int alarmstatus, data;
  170. regmap_read(priv->map, DS1343_CONTROL_REG, &data);
  171. alarmstatus = !!(data & DS1343_A0IE);
  172. if (alarmstatus)
  173. return sprintf(buf, "enabled\n");
  174. else
  175. return sprintf(buf, "disabled\n");
  176. }
  177. static DEVICE_ATTR(alarm_status, S_IRUGO, ds1343_show_alarmstatus, NULL);
  178. static ssize_t ds1343_show_alarmmode(struct device *dev,
  179. struct device_attribute *attr, char *buf)
  180. {
  181. struct ds1343_priv *priv = dev_get_drvdata(dev);
  182. int alarm_mode, data;
  183. char *alarm_str;
  184. regmap_read(priv->map, DS1343_ALM0_SEC_REG, &data);
  185. alarm_mode = (data & 0x80) >> 4;
  186. regmap_read(priv->map, DS1343_ALM0_MIN_REG, &data);
  187. alarm_mode |= (data & 0x80) >> 5;
  188. regmap_read(priv->map, DS1343_ALM0_HOUR_REG, &data);
  189. alarm_mode |= (data & 0x80) >> 6;
  190. regmap_read(priv->map, DS1343_ALM0_DAY_REG, &data);
  191. alarm_mode |= (data & 0x80) >> 7;
  192. switch (alarm_mode) {
  193. case 15:
  194. alarm_str = "each second";
  195. break;
  196. case 7:
  197. alarm_str = "seconds match";
  198. break;
  199. case 3:
  200. alarm_str = "minutes and seconds match";
  201. break;
  202. case 1:
  203. alarm_str = "hours, minutes and seconds match";
  204. break;
  205. case 0:
  206. alarm_str = "day, hours, minutes and seconds match";
  207. break;
  208. default:
  209. alarm_str = "invalid";
  210. break;
  211. }
  212. return sprintf(buf, "%s\n", alarm_str);
  213. }
  214. static DEVICE_ATTR(alarm_mode, S_IRUGO, ds1343_show_alarmmode, NULL);
  215. static ssize_t ds1343_show_tricklecharger(struct device *dev,
  216. struct device_attribute *attr, char *buf)
  217. {
  218. struct ds1343_priv *priv = dev_get_drvdata(dev);
  219. int data;
  220. char *diodes = "disabled", *resistors = " ";
  221. regmap_read(priv->map, DS1343_TRICKLE_REG, &data);
  222. if ((data & 0xf0) == DS1343_TRICKLE_MAGIC) {
  223. switch (data & 0x0c) {
  224. case DS1343_TRICKLE_DS1:
  225. diodes = "one diode,";
  226. break;
  227. default:
  228. diodes = "no diode,";
  229. break;
  230. }
  231. switch (data & 0x03) {
  232. case DS1343_TRICKLE_1K:
  233. resistors = "1k Ohm";
  234. break;
  235. case DS1343_TRICKLE_2K:
  236. resistors = "2k Ohm";
  237. break;
  238. case DS1343_TRICKLE_4K:
  239. resistors = "4k Ohm";
  240. break;
  241. default:
  242. diodes = "disabled";
  243. break;
  244. }
  245. }
  246. return sprintf(buf, "%s %s\n", diodes, resistors);
  247. }
  248. static DEVICE_ATTR(trickle_charger, S_IRUGO, ds1343_show_tricklecharger, NULL);
  249. static int ds1343_sysfs_register(struct device *dev)
  250. {
  251. struct ds1343_priv *priv = dev_get_drvdata(dev);
  252. int err;
  253. err = device_create_file(dev, &dev_attr_glitch_filter);
  254. if (err)
  255. return err;
  256. err = device_create_file(dev, &dev_attr_trickle_charger);
  257. if (err)
  258. goto error1;
  259. err = device_create_bin_file(dev, &nvram_attr);
  260. if (err)
  261. goto error2;
  262. if (priv->irq <= 0)
  263. return err;
  264. err = device_create_file(dev, &dev_attr_alarm_mode);
  265. if (err)
  266. goto error3;
  267. err = device_create_file(dev, &dev_attr_alarm_status);
  268. if (!err)
  269. return err;
  270. device_remove_file(dev, &dev_attr_alarm_mode);
  271. error3:
  272. device_remove_bin_file(dev, &nvram_attr);
  273. error2:
  274. device_remove_file(dev, &dev_attr_trickle_charger);
  275. error1:
  276. device_remove_file(dev, &dev_attr_glitch_filter);
  277. return err;
  278. }
  279. static void ds1343_sysfs_unregister(struct device *dev)
  280. {
  281. struct ds1343_priv *priv = dev_get_drvdata(dev);
  282. device_remove_file(dev, &dev_attr_glitch_filter);
  283. device_remove_file(dev, &dev_attr_trickle_charger);
  284. device_remove_bin_file(dev, &nvram_attr);
  285. if (priv->irq <= 0)
  286. return;
  287. device_remove_file(dev, &dev_attr_alarm_status);
  288. device_remove_file(dev, &dev_attr_alarm_mode);
  289. }
  290. static int ds1343_read_time(struct device *dev, struct rtc_time *dt)
  291. {
  292. struct ds1343_priv *priv = dev_get_drvdata(dev);
  293. unsigned char buf[7];
  294. int res;
  295. res = regmap_bulk_read(priv->map, DS1343_SECONDS_REG, buf, 7);
  296. if (res)
  297. return res;
  298. dt->tm_sec = bcd2bin(buf[0]);
  299. dt->tm_min = bcd2bin(buf[1]);
  300. dt->tm_hour = bcd2bin(buf[2] & 0x3F);
  301. dt->tm_wday = bcd2bin(buf[3]) - 1;
  302. dt->tm_mday = bcd2bin(buf[4]);
  303. dt->tm_mon = bcd2bin(buf[5] & 0x1F) - 1;
  304. dt->tm_year = bcd2bin(buf[6]) + 100; /* year offset from 1900 */
  305. return rtc_valid_tm(dt);
  306. }
  307. static int ds1343_set_time(struct device *dev, struct rtc_time *dt)
  308. {
  309. struct ds1343_priv *priv = dev_get_drvdata(dev);
  310. int res;
  311. res = regmap_write(priv->map, DS1343_SECONDS_REG,
  312. bin2bcd(dt->tm_sec));
  313. if (res)
  314. return res;
  315. res = regmap_write(priv->map, DS1343_MINUTES_REG,
  316. bin2bcd(dt->tm_min));
  317. if (res)
  318. return res;
  319. res = regmap_write(priv->map, DS1343_HOURS_REG,
  320. bin2bcd(dt->tm_hour) & 0x3F);
  321. if (res)
  322. return res;
  323. res = regmap_write(priv->map, DS1343_DAY_REG,
  324. bin2bcd(dt->tm_wday + 1));
  325. if (res)
  326. return res;
  327. res = regmap_write(priv->map, DS1343_DATE_REG,
  328. bin2bcd(dt->tm_mday));
  329. if (res)
  330. return res;
  331. res = regmap_write(priv->map, DS1343_MONTH_REG,
  332. bin2bcd(dt->tm_mon + 1));
  333. if (res)
  334. return res;
  335. dt->tm_year %= 100;
  336. res = regmap_write(priv->map, DS1343_YEAR_REG,
  337. bin2bcd(dt->tm_year));
  338. if (res)
  339. return res;
  340. return 0;
  341. }
  342. static int ds1343_update_alarm(struct device *dev)
  343. {
  344. struct ds1343_priv *priv = dev_get_drvdata(dev);
  345. unsigned int control, stat;
  346. unsigned char buf[4];
  347. int res = 0;
  348. res = regmap_read(priv->map, DS1343_CONTROL_REG, &control);
  349. if (res)
  350. return res;
  351. res = regmap_read(priv->map, DS1343_STATUS_REG, &stat);
  352. if (res)
  353. return res;
  354. control &= ~(DS1343_A0IE);
  355. stat &= ~(DS1343_IRQF0);
  356. res = regmap_write(priv->map, DS1343_CONTROL_REG, control);
  357. if (res)
  358. return res;
  359. res = regmap_write(priv->map, DS1343_STATUS_REG, stat);
  360. if (res)
  361. return res;
  362. buf[0] = priv->alarm_sec < 0 || (priv->irqen & RTC_UF) ?
  363. 0x80 : bin2bcd(priv->alarm_sec) & 0x7F;
  364. buf[1] = priv->alarm_min < 0 || (priv->irqen & RTC_UF) ?
  365. 0x80 : bin2bcd(priv->alarm_min) & 0x7F;
  366. buf[2] = priv->alarm_hour < 0 || (priv->irqen & RTC_UF) ?
  367. 0x80 : bin2bcd(priv->alarm_hour) & 0x3F;
  368. buf[3] = priv->alarm_mday < 0 || (priv->irqen & RTC_UF) ?
  369. 0x80 : bin2bcd(priv->alarm_mday) & 0x7F;
  370. res = regmap_bulk_write(priv->map, DS1343_ALM0_SEC_REG, buf, 4);
  371. if (res)
  372. return res;
  373. if (priv->irqen) {
  374. control |= DS1343_A0IE;
  375. res = regmap_write(priv->map, DS1343_CONTROL_REG, control);
  376. }
  377. return res;
  378. }
  379. static int ds1343_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  380. {
  381. struct ds1343_priv *priv = dev_get_drvdata(dev);
  382. int res = 0;
  383. unsigned int stat;
  384. if (priv->irq <= 0)
  385. return -EINVAL;
  386. mutex_lock(&priv->mutex);
  387. res = regmap_read(priv->map, DS1343_STATUS_REG, &stat);
  388. if (res)
  389. goto out;
  390. alarm->enabled = !!(priv->irqen & RTC_AF);
  391. alarm->pending = !!(stat & DS1343_IRQF0);
  392. alarm->time.tm_sec = priv->alarm_sec < 0 ? 0 : priv->alarm_sec;
  393. alarm->time.tm_min = priv->alarm_min < 0 ? 0 : priv->alarm_min;
  394. alarm->time.tm_hour = priv->alarm_hour < 0 ? 0 : priv->alarm_hour;
  395. alarm->time.tm_mday = priv->alarm_mday < 0 ? 0 : priv->alarm_mday;
  396. alarm->time.tm_mon = -1;
  397. alarm->time.tm_year = -1;
  398. alarm->time.tm_wday = -1;
  399. alarm->time.tm_yday = -1;
  400. alarm->time.tm_isdst = -1;
  401. out:
  402. mutex_unlock(&priv->mutex);
  403. return res;
  404. }
  405. static int ds1343_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  406. {
  407. struct ds1343_priv *priv = dev_get_drvdata(dev);
  408. int res = 0;
  409. if (priv->irq <= 0)
  410. return -EINVAL;
  411. mutex_lock(&priv->mutex);
  412. priv->alarm_sec = alarm->time.tm_sec;
  413. priv->alarm_min = alarm->time.tm_min;
  414. priv->alarm_hour = alarm->time.tm_hour;
  415. priv->alarm_mday = alarm->time.tm_mday;
  416. if (alarm->enabled)
  417. priv->irqen |= RTC_AF;
  418. res = ds1343_update_alarm(dev);
  419. mutex_unlock(&priv->mutex);
  420. return res;
  421. }
  422. static int ds1343_alarm_irq_enable(struct device *dev, unsigned int enabled)
  423. {
  424. struct ds1343_priv *priv = dev_get_drvdata(dev);
  425. int res = 0;
  426. if (priv->irq <= 0)
  427. return -EINVAL;
  428. mutex_lock(&priv->mutex);
  429. if (enabled)
  430. priv->irqen |= RTC_AF;
  431. else
  432. priv->irqen &= ~RTC_AF;
  433. res = ds1343_update_alarm(dev);
  434. mutex_unlock(&priv->mutex);
  435. return res;
  436. }
  437. static irqreturn_t ds1343_thread(int irq, void *dev_id)
  438. {
  439. struct ds1343_priv *priv = dev_id;
  440. unsigned int stat, control;
  441. int res = 0;
  442. mutex_lock(&priv->mutex);
  443. res = regmap_read(priv->map, DS1343_STATUS_REG, &stat);
  444. if (res)
  445. goto out;
  446. if (stat & DS1343_IRQF0) {
  447. stat &= ~DS1343_IRQF0;
  448. regmap_write(priv->map, DS1343_STATUS_REG, stat);
  449. res = regmap_read(priv->map, DS1343_CONTROL_REG, &control);
  450. if (res)
  451. goto out;
  452. control &= ~DS1343_A0IE;
  453. regmap_write(priv->map, DS1343_CONTROL_REG, control);
  454. rtc_update_irq(priv->rtc, 1, RTC_AF | RTC_IRQF);
  455. }
  456. out:
  457. mutex_unlock(&priv->mutex);
  458. return IRQ_HANDLED;
  459. }
  460. static const struct rtc_class_ops ds1343_rtc_ops = {
  461. .ioctl = ds1343_ioctl,
  462. .read_time = ds1343_read_time,
  463. .set_time = ds1343_set_time,
  464. .read_alarm = ds1343_read_alarm,
  465. .set_alarm = ds1343_set_alarm,
  466. .alarm_irq_enable = ds1343_alarm_irq_enable,
  467. };
  468. static int ds1343_probe(struct spi_device *spi)
  469. {
  470. struct ds1343_priv *priv;
  471. struct regmap_config config;
  472. unsigned int data;
  473. int res;
  474. memset(&config, 0, sizeof(config));
  475. config.reg_bits = 8;
  476. config.val_bits = 8;
  477. config.write_flag_mask = 0x80;
  478. priv = devm_kzalloc(&spi->dev, sizeof(struct ds1343_priv), GFP_KERNEL);
  479. if (!priv)
  480. return -ENOMEM;
  481. priv->spi = spi;
  482. mutex_init(&priv->mutex);
  483. /* RTC DS1347 works in spi mode 3 and
  484. * its chip select is active high
  485. */
  486. spi->mode = SPI_MODE_3 | SPI_CS_HIGH;
  487. spi->bits_per_word = 8;
  488. res = spi_setup(spi);
  489. if (res)
  490. return res;
  491. spi_set_drvdata(spi, priv);
  492. priv->map = devm_regmap_init_spi(spi, &config);
  493. if (IS_ERR(priv->map)) {
  494. dev_err(&spi->dev, "spi regmap init failed for rtc ds1343\n");
  495. return PTR_ERR(priv->map);
  496. }
  497. res = regmap_read(priv->map, DS1343_SECONDS_REG, &data);
  498. if (res)
  499. return res;
  500. regmap_read(priv->map, DS1343_CONTROL_REG, &data);
  501. data |= DS1343_INTCN;
  502. data &= ~(DS1343_EOSC | DS1343_A1IE | DS1343_A0IE);
  503. regmap_write(priv->map, DS1343_CONTROL_REG, data);
  504. regmap_read(priv->map, DS1343_STATUS_REG, &data);
  505. data &= ~(DS1343_OSF | DS1343_IRQF1 | DS1343_IRQF0);
  506. regmap_write(priv->map, DS1343_STATUS_REG, data);
  507. priv->rtc = devm_rtc_device_register(&spi->dev, "ds1343",
  508. &ds1343_rtc_ops, THIS_MODULE);
  509. if (IS_ERR(priv->rtc)) {
  510. dev_err(&spi->dev, "unable to register rtc ds1343\n");
  511. return PTR_ERR(priv->rtc);
  512. }
  513. priv->irq = spi->irq;
  514. if (priv->irq >= 0) {
  515. res = devm_request_threaded_irq(&spi->dev, spi->irq, NULL,
  516. ds1343_thread,
  517. IRQF_NO_SUSPEND | IRQF_ONESHOT,
  518. "ds1343", priv);
  519. if (res) {
  520. priv->irq = -1;
  521. dev_err(&spi->dev,
  522. "unable to request irq for rtc ds1343\n");
  523. } else {
  524. device_set_wakeup_capable(&spi->dev, 1);
  525. }
  526. }
  527. res = ds1343_sysfs_register(&spi->dev);
  528. if (res)
  529. dev_err(&spi->dev,
  530. "unable to create sysfs entries for rtc ds1343\n");
  531. return 0;
  532. }
  533. static int ds1343_remove(struct spi_device *spi)
  534. {
  535. struct ds1343_priv *priv = spi_get_drvdata(spi);
  536. if (spi->irq) {
  537. mutex_lock(&priv->mutex);
  538. priv->irqen &= ~RTC_AF;
  539. mutex_unlock(&priv->mutex);
  540. devm_free_irq(&spi->dev, spi->irq, priv);
  541. }
  542. spi_set_drvdata(spi, NULL);
  543. ds1343_sysfs_unregister(&spi->dev);
  544. return 0;
  545. }
  546. #ifdef CONFIG_PM_SLEEP
  547. static int ds1343_suspend(struct device *dev)
  548. {
  549. struct spi_device *spi = to_spi_device(dev);
  550. if (spi->irq >= 0 && device_may_wakeup(dev))
  551. enable_irq_wake(spi->irq);
  552. return 0;
  553. }
  554. static int ds1343_resume(struct device *dev)
  555. {
  556. struct spi_device *spi = to_spi_device(dev);
  557. if (spi->irq >= 0 && device_may_wakeup(dev))
  558. disable_irq_wake(spi->irq);
  559. return 0;
  560. }
  561. #endif
  562. static SIMPLE_DEV_PM_OPS(ds1343_pm, ds1343_suspend, ds1343_resume);
  563. static struct spi_driver ds1343_driver = {
  564. .driver = {
  565. .name = "ds1343",
  566. .owner = THIS_MODULE,
  567. .pm = &ds1343_pm,
  568. },
  569. .probe = ds1343_probe,
  570. .remove = ds1343_remove,
  571. .id_table = ds1343_id,
  572. };
  573. module_spi_driver(ds1343_driver);
  574. MODULE_DESCRIPTION("DS1343 RTC SPI Driver");
  575. MODULE_AUTHOR("Raghavendra Chandra Ganiga <ravi23ganiga@gmail.com>,"
  576. "Ankur Srivastava <sankurece@gmail.com>");
  577. MODULE_LICENSE("GPL v2");
  578. MODULE_VERSION(DS1343_DRV_VERSION);