rtc-isl12022.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * An I2C driver for the Intersil ISL 12022
  3. *
  4. * Author: Roman Fietze <roman.fietze@telemotive.de>
  5. *
  6. * Based on the Philips PCF8563 RTC
  7. * by Alessandro Zummo <a.zummo@towertech.it>.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License version
  11. * 2 as published by the Free Software Foundation.
  12. */
  13. #include <linux/i2c.h>
  14. #include <linux/bcd.h>
  15. #include <linux/rtc.h>
  16. #include <linux/slab.h>
  17. #include <linux/module.h>
  18. #include <linux/err.h>
  19. #include <linux/of.h>
  20. #include <linux/of_device.h>
  21. /* ISL register offsets */
  22. #define ISL12022_REG_SC 0x00
  23. #define ISL12022_REG_MN 0x01
  24. #define ISL12022_REG_HR 0x02
  25. #define ISL12022_REG_DT 0x03
  26. #define ISL12022_REG_MO 0x04
  27. #define ISL12022_REG_YR 0x05
  28. #define ISL12022_REG_DW 0x06
  29. #define ISL12022_REG_SR 0x07
  30. #define ISL12022_REG_INT 0x08
  31. /* ISL register bits */
  32. #define ISL12022_HR_MIL (1 << 7) /* military or 24 hour time */
  33. #define ISL12022_SR_LBAT85 (1 << 2)
  34. #define ISL12022_SR_LBAT75 (1 << 1)
  35. #define ISL12022_INT_WRTC (1 << 6)
  36. static struct i2c_driver isl12022_driver;
  37. struct isl12022 {
  38. struct rtc_device *rtc;
  39. bool write_enabled; /* true if write enable is set */
  40. };
  41. static int isl12022_read_regs(struct i2c_client *client, uint8_t reg,
  42. uint8_t *data, size_t n)
  43. {
  44. struct i2c_msg msgs[] = {
  45. {
  46. .addr = client->addr,
  47. .flags = 0,
  48. .len = 1,
  49. .buf = data
  50. }, /* setup read ptr */
  51. {
  52. .addr = client->addr,
  53. .flags = I2C_M_RD,
  54. .len = n,
  55. .buf = data
  56. }
  57. };
  58. int ret;
  59. data[0] = reg;
  60. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  61. if (ret != ARRAY_SIZE(msgs)) {
  62. dev_err(&client->dev, "%s: read error, ret=%d\n",
  63. __func__, ret);
  64. return -EIO;
  65. }
  66. return 0;
  67. }
  68. static int isl12022_write_reg(struct i2c_client *client,
  69. uint8_t reg, uint8_t val)
  70. {
  71. uint8_t data[2] = { reg, val };
  72. int err;
  73. err = i2c_master_send(client, data, sizeof(data));
  74. if (err != sizeof(data)) {
  75. dev_err(&client->dev,
  76. "%s: err=%d addr=%02x, data=%02x\n",
  77. __func__, err, data[0], data[1]);
  78. return -EIO;
  79. }
  80. return 0;
  81. }
  82. /*
  83. * In the routines that deal directly with the isl12022 hardware, we use
  84. * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
  85. */
  86. static int isl12022_get_datetime(struct i2c_client *client, struct rtc_time *tm)
  87. {
  88. uint8_t buf[ISL12022_REG_INT + 1];
  89. int ret;
  90. ret = isl12022_read_regs(client, ISL12022_REG_SC, buf, sizeof(buf));
  91. if (ret)
  92. return ret;
  93. if (buf[ISL12022_REG_SR] & (ISL12022_SR_LBAT85 | ISL12022_SR_LBAT75)) {
  94. dev_warn(&client->dev,
  95. "voltage dropped below %u%%, "
  96. "date and time is not reliable.\n",
  97. buf[ISL12022_REG_SR] & ISL12022_SR_LBAT85 ? 85 : 75);
  98. }
  99. dev_dbg(&client->dev,
  100. "%s: raw data is sec=%02x, min=%02x, hr=%02x, "
  101. "mday=%02x, mon=%02x, year=%02x, wday=%02x, "
  102. "sr=%02x, int=%02x",
  103. __func__,
  104. buf[ISL12022_REG_SC],
  105. buf[ISL12022_REG_MN],
  106. buf[ISL12022_REG_HR],
  107. buf[ISL12022_REG_DT],
  108. buf[ISL12022_REG_MO],
  109. buf[ISL12022_REG_YR],
  110. buf[ISL12022_REG_DW],
  111. buf[ISL12022_REG_SR],
  112. buf[ISL12022_REG_INT]);
  113. tm->tm_sec = bcd2bin(buf[ISL12022_REG_SC] & 0x7F);
  114. tm->tm_min = bcd2bin(buf[ISL12022_REG_MN] & 0x7F);
  115. tm->tm_hour = bcd2bin(buf[ISL12022_REG_HR] & 0x3F);
  116. tm->tm_mday = bcd2bin(buf[ISL12022_REG_DT] & 0x3F);
  117. tm->tm_wday = buf[ISL12022_REG_DW] & 0x07;
  118. tm->tm_mon = bcd2bin(buf[ISL12022_REG_MO] & 0x1F) - 1;
  119. tm->tm_year = bcd2bin(buf[ISL12022_REG_YR]) + 100;
  120. dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
  121. "mday=%d, mon=%d, year=%d, wday=%d\n",
  122. __func__,
  123. tm->tm_sec, tm->tm_min, tm->tm_hour,
  124. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  125. return rtc_valid_tm(tm);
  126. }
  127. static int isl12022_set_datetime(struct i2c_client *client, struct rtc_time *tm)
  128. {
  129. struct isl12022 *isl12022 = i2c_get_clientdata(client);
  130. size_t i;
  131. int ret;
  132. uint8_t buf[ISL12022_REG_DW + 1];
  133. dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
  134. "mday=%d, mon=%d, year=%d, wday=%d\n",
  135. __func__,
  136. tm->tm_sec, tm->tm_min, tm->tm_hour,
  137. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  138. if (!isl12022->write_enabled) {
  139. ret = isl12022_read_regs(client, ISL12022_REG_INT, buf, 1);
  140. if (ret)
  141. return ret;
  142. /* Check if WRTC (write rtc enable) is set factory default is
  143. * 0 (not set) */
  144. if (!(buf[0] & ISL12022_INT_WRTC)) {
  145. dev_info(&client->dev,
  146. "init write enable and 24 hour format\n");
  147. /* Set the write enable bit. */
  148. ret = isl12022_write_reg(client,
  149. ISL12022_REG_INT,
  150. buf[0] | ISL12022_INT_WRTC);
  151. if (ret)
  152. return ret;
  153. /* Write to any RTC register to start RTC, we use the
  154. * HR register, setting the MIL bit to use the 24 hour
  155. * format. */
  156. ret = isl12022_read_regs(client, ISL12022_REG_HR,
  157. buf, 1);
  158. if (ret)
  159. return ret;
  160. ret = isl12022_write_reg(client,
  161. ISL12022_REG_HR,
  162. buf[0] | ISL12022_HR_MIL);
  163. if (ret)
  164. return ret;
  165. }
  166. isl12022->write_enabled = 1;
  167. }
  168. /* hours, minutes and seconds */
  169. buf[ISL12022_REG_SC] = bin2bcd(tm->tm_sec);
  170. buf[ISL12022_REG_MN] = bin2bcd(tm->tm_min);
  171. buf[ISL12022_REG_HR] = bin2bcd(tm->tm_hour) | ISL12022_HR_MIL;
  172. buf[ISL12022_REG_DT] = bin2bcd(tm->tm_mday);
  173. /* month, 1 - 12 */
  174. buf[ISL12022_REG_MO] = bin2bcd(tm->tm_mon + 1);
  175. /* year and century */
  176. buf[ISL12022_REG_YR] = bin2bcd(tm->tm_year % 100);
  177. buf[ISL12022_REG_DW] = tm->tm_wday & 0x07;
  178. /* write register's data */
  179. for (i = 0; i < ARRAY_SIZE(buf); i++) {
  180. ret = isl12022_write_reg(client, ISL12022_REG_SC + i,
  181. buf[ISL12022_REG_SC + i]);
  182. if (ret)
  183. return -EIO;
  184. }
  185. return 0;
  186. }
  187. static int isl12022_rtc_read_time(struct device *dev, struct rtc_time *tm)
  188. {
  189. return isl12022_get_datetime(to_i2c_client(dev), tm);
  190. }
  191. static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm)
  192. {
  193. return isl12022_set_datetime(to_i2c_client(dev), tm);
  194. }
  195. static const struct rtc_class_ops isl12022_rtc_ops = {
  196. .read_time = isl12022_rtc_read_time,
  197. .set_time = isl12022_rtc_set_time,
  198. };
  199. static int isl12022_probe(struct i2c_client *client,
  200. const struct i2c_device_id *id)
  201. {
  202. struct isl12022 *isl12022;
  203. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  204. return -ENODEV;
  205. isl12022 = devm_kzalloc(&client->dev, sizeof(struct isl12022),
  206. GFP_KERNEL);
  207. if (!isl12022)
  208. return -ENOMEM;
  209. i2c_set_clientdata(client, isl12022);
  210. isl12022->rtc = devm_rtc_device_register(&client->dev,
  211. isl12022_driver.driver.name,
  212. &isl12022_rtc_ops, THIS_MODULE);
  213. return PTR_ERR_OR_ZERO(isl12022->rtc);
  214. }
  215. #ifdef CONFIG_OF
  216. static const struct of_device_id isl12022_dt_match[] = {
  217. { .compatible = "isl,isl12022" }, /* for backward compat., don't use */
  218. { .compatible = "isil,isl12022" },
  219. { },
  220. };
  221. MODULE_DEVICE_TABLE(of, isl12022_dt_match);
  222. #endif
  223. static const struct i2c_device_id isl12022_id[] = {
  224. { "isl12022", 0 },
  225. { }
  226. };
  227. MODULE_DEVICE_TABLE(i2c, isl12022_id);
  228. static struct i2c_driver isl12022_driver = {
  229. .driver = {
  230. .name = "rtc-isl12022",
  231. #ifdef CONFIG_OF
  232. .of_match_table = of_match_ptr(isl12022_dt_match),
  233. #endif
  234. },
  235. .probe = isl12022_probe,
  236. .id_table = isl12022_id,
  237. };
  238. module_i2c_driver(isl12022_driver);
  239. MODULE_AUTHOR("roman.fietze@telemotive.de");
  240. MODULE_DESCRIPTION("ISL 12022 RTC driver");
  241. MODULE_LICENSE("GPL");