PageRenderTime 25ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/rtc/rtc-isl12022.c

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