rtc-isl12022.c 7.1 KB

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