rtc-rv8803.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*
  2. * RTC driver for the Micro Crystal RV8803
  3. *
  4. * Copyright (C) 2015 Micro Crystal SA
  5. *
  6. * Alexandre Belloni <alexandre.belloni@free-electrons.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/bcd.h>
  14. #include <linux/bitops.h>
  15. #include <linux/i2c.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/rtc.h>
  20. #define RV8803_SEC 0x00
  21. #define RV8803_MIN 0x01
  22. #define RV8803_HOUR 0x02
  23. #define RV8803_WEEK 0x03
  24. #define RV8803_DAY 0x04
  25. #define RV8803_MONTH 0x05
  26. #define RV8803_YEAR 0x06
  27. #define RV8803_RAM 0x07
  28. #define RV8803_ALARM_MIN 0x08
  29. #define RV8803_ALARM_HOUR 0x09
  30. #define RV8803_ALARM_WEEK_OR_DAY 0x0A
  31. #define RV8803_EXT 0x0D
  32. #define RV8803_FLAG 0x0E
  33. #define RV8803_CTRL 0x0F
  34. #define RV8803_EXT_WADA BIT(6)
  35. #define RV8803_FLAG_V1F BIT(0)
  36. #define RV8803_FLAG_V2F BIT(1)
  37. #define RV8803_FLAG_AF BIT(3)
  38. #define RV8803_FLAG_TF BIT(4)
  39. #define RV8803_FLAG_UF BIT(5)
  40. #define RV8803_CTRL_RESET BIT(0)
  41. #define RV8803_CTRL_EIE BIT(2)
  42. #define RV8803_CTRL_AIE BIT(3)
  43. #define RV8803_CTRL_TIE BIT(4)
  44. #define RV8803_CTRL_UIE BIT(5)
  45. struct rv8803_data {
  46. struct i2c_client *client;
  47. struct rtc_device *rtc;
  48. spinlock_t flags_lock;
  49. u8 ctrl;
  50. };
  51. static irqreturn_t rv8803_handle_irq(int irq, void *dev_id)
  52. {
  53. struct i2c_client *client = dev_id;
  54. struct rv8803_data *rv8803 = i2c_get_clientdata(client);
  55. unsigned long events = 0;
  56. u8 flags;
  57. spin_lock(&rv8803->flags_lock);
  58. flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
  59. if (flags <= 0) {
  60. spin_unlock(&rv8803->flags_lock);
  61. return IRQ_NONE;
  62. }
  63. if (flags & RV8803_FLAG_V1F)
  64. dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
  65. if (flags & RV8803_FLAG_V2F)
  66. dev_warn(&client->dev, "Voltage low, data loss detected.\n");
  67. if (flags & RV8803_FLAG_TF) {
  68. flags &= ~RV8803_FLAG_TF;
  69. rv8803->ctrl &= ~RV8803_CTRL_TIE;
  70. events |= RTC_PF;
  71. }
  72. if (flags & RV8803_FLAG_AF) {
  73. flags &= ~RV8803_FLAG_AF;
  74. rv8803->ctrl &= ~RV8803_CTRL_AIE;
  75. events |= RTC_AF;
  76. }
  77. if (flags & RV8803_FLAG_UF) {
  78. flags &= ~RV8803_FLAG_UF;
  79. rv8803->ctrl &= ~RV8803_CTRL_UIE;
  80. events |= RTC_UF;
  81. }
  82. if (events) {
  83. rtc_update_irq(rv8803->rtc, 1, events);
  84. i2c_smbus_write_byte_data(client, RV8803_FLAG, flags);
  85. i2c_smbus_write_byte_data(rv8803->client, RV8803_CTRL,
  86. rv8803->ctrl);
  87. }
  88. spin_unlock(&rv8803->flags_lock);
  89. return IRQ_HANDLED;
  90. }
  91. static int rv8803_get_time(struct device *dev, struct rtc_time *tm)
  92. {
  93. struct rv8803_data *rv8803 = dev_get_drvdata(dev);
  94. u8 date1[7];
  95. u8 date2[7];
  96. u8 *date = date1;
  97. int ret, flags;
  98. flags = i2c_smbus_read_byte_data(rv8803->client, RV8803_FLAG);
  99. if (flags < 0)
  100. return flags;
  101. if (flags & RV8803_FLAG_V2F) {
  102. dev_warn(dev, "Voltage low, data is invalid.\n");
  103. return -EINVAL;
  104. }
  105. ret = i2c_smbus_read_i2c_block_data(rv8803->client, RV8803_SEC,
  106. 7, date);
  107. if (ret != 7)
  108. return ret < 0 ? ret : -EIO;
  109. if ((date1[RV8803_SEC] & 0x7f) == bin2bcd(59)) {
  110. ret = i2c_smbus_read_i2c_block_data(rv8803->client, RV8803_SEC,
  111. 7, date2);
  112. if (ret != 7)
  113. return ret < 0 ? ret : -EIO;
  114. if ((date2[RV8803_SEC] & 0x7f) != bin2bcd(59))
  115. date = date2;
  116. }
  117. tm->tm_sec = bcd2bin(date[RV8803_SEC] & 0x7f);
  118. tm->tm_min = bcd2bin(date[RV8803_MIN] & 0x7f);
  119. tm->tm_hour = bcd2bin(date[RV8803_HOUR] & 0x3f);
  120. tm->tm_wday = ffs(date[RV8803_WEEK] & 0x7f);
  121. tm->tm_mday = bcd2bin(date[RV8803_DAY] & 0x3f);
  122. tm->tm_mon = bcd2bin(date[RV8803_MONTH] & 0x1f) - 1;
  123. tm->tm_year = bcd2bin(date[RV8803_YEAR]) + 100;
  124. return rtc_valid_tm(tm);
  125. }
  126. static int rv8803_set_time(struct device *dev, struct rtc_time *tm)
  127. {
  128. struct rv8803_data *rv8803 = dev_get_drvdata(dev);
  129. u8 date[7];
  130. int flags, ret;
  131. unsigned long irqflags;
  132. if ((tm->tm_year < 100) || (tm->tm_year > 199))
  133. return -EINVAL;
  134. date[RV8803_SEC] = bin2bcd(tm->tm_sec);
  135. date[RV8803_MIN] = bin2bcd(tm->tm_min);
  136. date[RV8803_HOUR] = bin2bcd(tm->tm_hour);
  137. date[RV8803_WEEK] = 1 << (tm->tm_wday);
  138. date[RV8803_DAY] = bin2bcd(tm->tm_mday);
  139. date[RV8803_MONTH] = bin2bcd(tm->tm_mon + 1);
  140. date[RV8803_YEAR] = bin2bcd(tm->tm_year - 100);
  141. ret = i2c_smbus_write_i2c_block_data(rv8803->client, RV8803_SEC,
  142. 7, date);
  143. if (ret < 0)
  144. return ret;
  145. spin_lock_irqsave(&rv8803->flags_lock, irqflags);
  146. flags = i2c_smbus_read_byte_data(rv8803->client, RV8803_FLAG);
  147. if (flags < 0) {
  148. spin_unlock_irqrestore(&rv8803->flags_lock, irqflags);
  149. return flags;
  150. }
  151. ret = i2c_smbus_write_byte_data(rv8803->client, RV8803_FLAG,
  152. flags & ~RV8803_FLAG_V2F);
  153. spin_unlock_irqrestore(&rv8803->flags_lock, irqflags);
  154. return ret;
  155. }
  156. static int rv8803_get_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  157. {
  158. struct rv8803_data *rv8803 = dev_get_drvdata(dev);
  159. struct i2c_client *client = rv8803->client;
  160. u8 alarmvals[3];
  161. int flags, ret;
  162. ret = i2c_smbus_read_i2c_block_data(client, RV8803_ALARM_MIN,
  163. 3, alarmvals);
  164. if (ret != 3)
  165. return ret < 0 ? ret : -EIO;
  166. flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
  167. if (flags < 0)
  168. return flags;
  169. alrm->time.tm_sec = 0;
  170. alrm->time.tm_min = bcd2bin(alarmvals[0] & 0x7f);
  171. alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f);
  172. alrm->time.tm_wday = -1;
  173. alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f);
  174. alrm->time.tm_mon = -1;
  175. alrm->time.tm_year = -1;
  176. alrm->enabled = !!(rv8803->ctrl & RV8803_CTRL_AIE);
  177. alrm->pending = (flags & RV8803_FLAG_AF) && alrm->enabled;
  178. return 0;
  179. }
  180. static int rv8803_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  181. {
  182. struct i2c_client *client = to_i2c_client(dev);
  183. struct rv8803_data *rv8803 = dev_get_drvdata(dev);
  184. u8 alarmvals[3];
  185. u8 ctrl[2];
  186. int ret, err;
  187. unsigned long irqflags;
  188. /* The alarm has no seconds, round up to nearest minute */
  189. if (alrm->time.tm_sec) {
  190. time64_t alarm_time = rtc_tm_to_time64(&alrm->time);
  191. alarm_time += 60 - alrm->time.tm_sec;
  192. rtc_time64_to_tm(alarm_time, &alrm->time);
  193. }
  194. spin_lock_irqsave(&rv8803->flags_lock, irqflags);
  195. ret = i2c_smbus_read_i2c_block_data(client, RV8803_FLAG, 2, ctrl);
  196. if (ret != 2) {
  197. spin_unlock_irqrestore(&rv8803->flags_lock, irqflags);
  198. return ret < 0 ? ret : -EIO;
  199. }
  200. alarmvals[0] = bin2bcd(alrm->time.tm_min);
  201. alarmvals[1] = bin2bcd(alrm->time.tm_hour);
  202. alarmvals[2] = bin2bcd(alrm->time.tm_mday);
  203. if (rv8803->ctrl & (RV8803_CTRL_AIE | RV8803_CTRL_UIE)) {
  204. rv8803->ctrl &= ~(RV8803_CTRL_AIE | RV8803_CTRL_UIE);
  205. err = i2c_smbus_write_byte_data(rv8803->client, RV8803_CTRL,
  206. rv8803->ctrl);
  207. if (err) {
  208. spin_unlock_irqrestore(&rv8803->flags_lock, irqflags);
  209. return err;
  210. }
  211. }
  212. ctrl[1] &= ~RV8803_FLAG_AF;
  213. err = i2c_smbus_write_byte_data(rv8803->client, RV8803_FLAG, ctrl[1]);
  214. spin_unlock_irqrestore(&rv8803->flags_lock, irqflags);
  215. if (err)
  216. return err;
  217. err = i2c_smbus_write_i2c_block_data(rv8803->client, RV8803_ALARM_MIN,
  218. 3, alarmvals);
  219. if (err)
  220. return err;
  221. if (alrm->enabled) {
  222. if (rv8803->rtc->uie_rtctimer.enabled)
  223. rv8803->ctrl |= RV8803_CTRL_UIE;
  224. if (rv8803->rtc->aie_timer.enabled)
  225. rv8803->ctrl |= RV8803_CTRL_AIE;
  226. err = i2c_smbus_write_byte_data(rv8803->client, RV8803_CTRL,
  227. rv8803->ctrl);
  228. if (err)
  229. return err;
  230. }
  231. return 0;
  232. }
  233. static int rv8803_alarm_irq_enable(struct device *dev, unsigned int enabled)
  234. {
  235. struct i2c_client *client = to_i2c_client(dev);
  236. struct rv8803_data *rv8803 = dev_get_drvdata(dev);
  237. int ctrl, flags, err;
  238. unsigned long irqflags;
  239. ctrl = rv8803->ctrl;
  240. if (enabled) {
  241. if (rv8803->rtc->uie_rtctimer.enabled)
  242. ctrl |= RV8803_CTRL_UIE;
  243. if (rv8803->rtc->aie_timer.enabled)
  244. ctrl |= RV8803_CTRL_AIE;
  245. } else {
  246. if (!rv8803->rtc->uie_rtctimer.enabled)
  247. ctrl &= ~RV8803_CTRL_UIE;
  248. if (!rv8803->rtc->aie_timer.enabled)
  249. ctrl &= ~RV8803_CTRL_AIE;
  250. }
  251. spin_lock_irqsave(&rv8803->flags_lock, irqflags);
  252. flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
  253. if (flags < 0) {
  254. spin_unlock_irqrestore(&rv8803->flags_lock, irqflags);
  255. return flags;
  256. }
  257. flags &= ~(RV8803_FLAG_AF | RV8803_FLAG_UF);
  258. err = i2c_smbus_write_byte_data(client, RV8803_FLAG, flags);
  259. spin_unlock_irqrestore(&rv8803->flags_lock, irqflags);
  260. if (err)
  261. return err;
  262. if (ctrl != rv8803->ctrl) {
  263. rv8803->ctrl = ctrl;
  264. err = i2c_smbus_write_byte_data(client, RV8803_CTRL,
  265. rv8803->ctrl);
  266. if (err)
  267. return err;
  268. }
  269. return 0;
  270. }
  271. static int rv8803_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
  272. {
  273. struct i2c_client *client = to_i2c_client(dev);
  274. struct rv8803_data *rv8803 = dev_get_drvdata(dev);
  275. int flags, ret = 0;
  276. unsigned long irqflags;
  277. switch (cmd) {
  278. case RTC_VL_READ:
  279. flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
  280. if (flags < 0)
  281. return flags;
  282. if (flags & RV8803_FLAG_V1F)
  283. dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
  284. if (flags & RV8803_FLAG_V2F)
  285. dev_warn(&client->dev, "Voltage low, data loss detected.\n");
  286. flags &= RV8803_FLAG_V1F | RV8803_FLAG_V2F;
  287. if (copy_to_user((void __user *)arg, &flags, sizeof(int)))
  288. return -EFAULT;
  289. return 0;
  290. case RTC_VL_CLR:
  291. spin_lock_irqsave(&rv8803->flags_lock, irqflags);
  292. flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
  293. if (flags < 0) {
  294. spin_unlock_irqrestore(&rv8803->flags_lock, irqflags);
  295. return flags;
  296. }
  297. flags &= ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F);
  298. ret = i2c_smbus_write_byte_data(client, RV8803_FLAG, flags);
  299. spin_unlock_irqrestore(&rv8803->flags_lock, irqflags);
  300. if (ret < 0)
  301. return ret;
  302. return 0;
  303. default:
  304. return -ENOIOCTLCMD;
  305. }
  306. }
  307. static ssize_t rv8803_nvram_write(struct file *filp, struct kobject *kobj,
  308. struct bin_attribute *attr,
  309. char *buf, loff_t off, size_t count)
  310. {
  311. struct device *dev = kobj_to_dev(kobj);
  312. struct i2c_client *client = to_i2c_client(dev);
  313. int ret;
  314. ret = i2c_smbus_write_byte_data(client, RV8803_RAM, buf[0]);
  315. if (ret < 0)
  316. return ret;
  317. return 1;
  318. }
  319. static ssize_t rv8803_nvram_read(struct file *filp, struct kobject *kobj,
  320. struct bin_attribute *attr,
  321. char *buf, loff_t off, size_t count)
  322. {
  323. struct device *dev = kobj_to_dev(kobj);
  324. struct i2c_client *client = to_i2c_client(dev);
  325. int ret;
  326. ret = i2c_smbus_read_byte_data(client, RV8803_RAM);
  327. if (ret < 0)
  328. return ret;
  329. buf[0] = ret;
  330. return 1;
  331. }
  332. static struct bin_attribute rv8803_nvram_attr = {
  333. .attr = {
  334. .name = "nvram",
  335. .mode = S_IRUGO | S_IWUSR,
  336. },
  337. .size = 1,
  338. .read = rv8803_nvram_read,
  339. .write = rv8803_nvram_write,
  340. };
  341. static struct rtc_class_ops rv8803_rtc_ops = {
  342. .read_time = rv8803_get_time,
  343. .set_time = rv8803_set_time,
  344. .ioctl = rv8803_ioctl,
  345. };
  346. static int rv8803_probe(struct i2c_client *client,
  347. const struct i2c_device_id *id)
  348. {
  349. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  350. struct rv8803_data *rv8803;
  351. int err, flags;
  352. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  353. I2C_FUNC_SMBUS_I2C_BLOCK)) {
  354. dev_err(&adapter->dev, "doesn't support I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK\n");
  355. return -EIO;
  356. }
  357. rv8803 = devm_kzalloc(&client->dev, sizeof(struct rv8803_data),
  358. GFP_KERNEL);
  359. if (!rv8803)
  360. return -ENOMEM;
  361. rv8803->client = client;
  362. i2c_set_clientdata(client, rv8803);
  363. flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
  364. if (flags < 0)
  365. return flags;
  366. if (flags & RV8803_FLAG_V1F)
  367. dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
  368. if (flags & RV8803_FLAG_V2F)
  369. dev_warn(&client->dev, "Voltage low, data loss detected.\n");
  370. if (flags & RV8803_FLAG_AF)
  371. dev_warn(&client->dev, "An alarm maybe have been missed.\n");
  372. if (client->irq > 0) {
  373. err = devm_request_threaded_irq(&client->dev, client->irq,
  374. NULL, rv8803_handle_irq,
  375. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  376. "rv8803", client);
  377. if (err) {
  378. dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n");
  379. client->irq = 0;
  380. } else {
  381. rv8803_rtc_ops.read_alarm = rv8803_get_alarm;
  382. rv8803_rtc_ops.set_alarm = rv8803_set_alarm;
  383. rv8803_rtc_ops.alarm_irq_enable = rv8803_alarm_irq_enable;
  384. }
  385. }
  386. rv8803->rtc = devm_rtc_device_register(&client->dev, client->name,
  387. &rv8803_rtc_ops, THIS_MODULE);
  388. if (IS_ERR(rv8803->rtc)) {
  389. dev_err(&client->dev, "unable to register the class device\n");
  390. return PTR_ERR(rv8803->rtc);
  391. }
  392. err = i2c_smbus_write_byte_data(rv8803->client, RV8803_EXT,
  393. RV8803_EXT_WADA);
  394. if (err)
  395. return err;
  396. err = device_create_bin_file(&client->dev, &rv8803_nvram_attr);
  397. if (err)
  398. return err;
  399. rv8803->rtc->max_user_freq = 1;
  400. return 0;
  401. }
  402. static int rv8803_remove(struct i2c_client *client)
  403. {
  404. device_remove_bin_file(&client->dev, &rv8803_nvram_attr);
  405. return 0;
  406. }
  407. static const struct i2c_device_id rv8803_id[] = {
  408. { "rv8803", 0 },
  409. { }
  410. };
  411. MODULE_DEVICE_TABLE(i2c, rv8803_id);
  412. static struct i2c_driver rv8803_driver = {
  413. .driver = {
  414. .name = "rtc-rv8803",
  415. },
  416. .probe = rv8803_probe,
  417. .remove = rv8803_remove,
  418. .id_table = rv8803_id,
  419. };
  420. module_i2c_driver(rv8803_driver);
  421. MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
  422. MODULE_DESCRIPTION("Micro Crystal RV8803 RTC driver");
  423. MODULE_LICENSE("GPL v2");