rtc-pcf8523.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Copyright (C) 2012 Avionic Design GmbH
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/bcd.h>
  9. #include <linux/i2c.h>
  10. #include <linux/module.h>
  11. #include <linux/rtc.h>
  12. #include <linux/of.h>
  13. #define DRIVER_NAME "rtc-pcf8523"
  14. #define REG_CONTROL1 0x00
  15. #define REG_CONTROL1_CAP_SEL (1 << 7)
  16. #define REG_CONTROL1_STOP (1 << 5)
  17. #define REG_CONTROL3 0x02
  18. #define REG_CONTROL3_PM_BLD (1 << 7) /* battery low detection disabled */
  19. #define REG_CONTROL3_PM_VDD (1 << 6) /* switch-over disabled */
  20. #define REG_CONTROL3_PM_DSM (1 << 5) /* direct switching mode */
  21. #define REG_CONTROL3_PM_MASK 0xe0
  22. #define REG_CONTROL3_BLF (1 << 2) /* battery low bit, read-only */
  23. #define REG_SECONDS 0x03
  24. #define REG_SECONDS_OS (1 << 7)
  25. #define REG_MINUTES 0x04
  26. #define REG_HOURS 0x05
  27. #define REG_DAYS 0x06
  28. #define REG_WEEKDAYS 0x07
  29. #define REG_MONTHS 0x08
  30. #define REG_YEARS 0x09
  31. struct pcf8523 {
  32. struct rtc_device *rtc;
  33. };
  34. static int pcf8523_read(struct i2c_client *client, u8 reg, u8 *valuep)
  35. {
  36. struct i2c_msg msgs[2];
  37. u8 value = 0;
  38. int err;
  39. msgs[0].addr = client->addr;
  40. msgs[0].flags = 0;
  41. msgs[0].len = sizeof(reg);
  42. msgs[0].buf = &reg;
  43. msgs[1].addr = client->addr;
  44. msgs[1].flags = I2C_M_RD;
  45. msgs[1].len = sizeof(value);
  46. msgs[1].buf = &value;
  47. err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  48. if (err < 0)
  49. return err;
  50. *valuep = value;
  51. return 0;
  52. }
  53. static int pcf8523_write(struct i2c_client *client, u8 reg, u8 value)
  54. {
  55. u8 buffer[2] = { reg, value };
  56. struct i2c_msg msg;
  57. int err;
  58. msg.addr = client->addr;
  59. msg.flags = 0;
  60. msg.len = sizeof(buffer);
  61. msg.buf = buffer;
  62. err = i2c_transfer(client->adapter, &msg, 1);
  63. if (err < 0)
  64. return err;
  65. return 0;
  66. }
  67. static int pcf8523_select_capacitance(struct i2c_client *client, bool high)
  68. {
  69. u8 value;
  70. int err;
  71. err = pcf8523_read(client, REG_CONTROL1, &value);
  72. if (err < 0)
  73. return err;
  74. if (!high)
  75. value &= ~REG_CONTROL1_CAP_SEL;
  76. else
  77. value |= REG_CONTROL1_CAP_SEL;
  78. err = pcf8523_write(client, REG_CONTROL1, value);
  79. if (err < 0)
  80. return err;
  81. return err;
  82. }
  83. static int pcf8523_set_pm(struct i2c_client *client, u8 pm)
  84. {
  85. u8 value;
  86. int err;
  87. err = pcf8523_read(client, REG_CONTROL3, &value);
  88. if (err < 0)
  89. return err;
  90. value = (value & ~REG_CONTROL3_PM_MASK) | pm;
  91. err = pcf8523_write(client, REG_CONTROL3, value);
  92. if (err < 0)
  93. return err;
  94. return 0;
  95. }
  96. static int pcf8523_stop_rtc(struct i2c_client *client)
  97. {
  98. u8 value;
  99. int err;
  100. err = pcf8523_read(client, REG_CONTROL1, &value);
  101. if (err < 0)
  102. return err;
  103. value |= REG_CONTROL1_STOP;
  104. err = pcf8523_write(client, REG_CONTROL1, value);
  105. if (err < 0)
  106. return err;
  107. return 0;
  108. }
  109. static int pcf8523_start_rtc(struct i2c_client *client)
  110. {
  111. u8 value;
  112. int err;
  113. err = pcf8523_read(client, REG_CONTROL1, &value);
  114. if (err < 0)
  115. return err;
  116. value &= ~REG_CONTROL1_STOP;
  117. err = pcf8523_write(client, REG_CONTROL1, value);
  118. if (err < 0)
  119. return err;
  120. return 0;
  121. }
  122. static int pcf8523_rtc_read_time(struct device *dev, struct rtc_time *tm)
  123. {
  124. struct i2c_client *client = to_i2c_client(dev);
  125. u8 start = REG_SECONDS, regs[7];
  126. struct i2c_msg msgs[2];
  127. int err;
  128. msgs[0].addr = client->addr;
  129. msgs[0].flags = 0;
  130. msgs[0].len = 1;
  131. msgs[0].buf = &start;
  132. msgs[1].addr = client->addr;
  133. msgs[1].flags = I2C_M_RD;
  134. msgs[1].len = sizeof(regs);
  135. msgs[1].buf = regs;
  136. err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  137. if (err < 0)
  138. return err;
  139. if (regs[0] & REG_SECONDS_OS)
  140. return -EINVAL;
  141. tm->tm_sec = bcd2bin(regs[0] & 0x7f);
  142. tm->tm_min = bcd2bin(regs[1] & 0x7f);
  143. tm->tm_hour = bcd2bin(regs[2] & 0x3f);
  144. tm->tm_mday = bcd2bin(regs[3] & 0x3f);
  145. tm->tm_wday = regs[4] & 0x7;
  146. tm->tm_mon = bcd2bin(regs[5] & 0x1f) - 1;
  147. tm->tm_year = bcd2bin(regs[6]) + 100;
  148. return rtc_valid_tm(tm);
  149. }
  150. static int pcf8523_rtc_set_time(struct device *dev, struct rtc_time *tm)
  151. {
  152. struct i2c_client *client = to_i2c_client(dev);
  153. struct i2c_msg msg;
  154. u8 regs[8];
  155. int err;
  156. /*
  157. * The hardware can only store values between 0 and 99 in it's YEAR
  158. * register (with 99 overflowing to 0 on increment).
  159. * After 2100-02-28 we could start interpreting the year to be in the
  160. * interval [2100, 2199], but there is no path to switch in a smooth way
  161. * because the chip handles YEAR=0x00 (and the out-of-spec
  162. * YEAR=0xa0) as a leap year, but 2100 isn't.
  163. */
  164. if (tm->tm_year < 100 || tm->tm_year >= 200)
  165. return -EINVAL;
  166. err = pcf8523_stop_rtc(client);
  167. if (err < 0)
  168. return err;
  169. regs[0] = REG_SECONDS;
  170. /* This will purposely overwrite REG_SECONDS_OS */
  171. regs[1] = bin2bcd(tm->tm_sec);
  172. regs[2] = bin2bcd(tm->tm_min);
  173. regs[3] = bin2bcd(tm->tm_hour);
  174. regs[4] = bin2bcd(tm->tm_mday);
  175. regs[5] = tm->tm_wday;
  176. regs[6] = bin2bcd(tm->tm_mon + 1);
  177. regs[7] = bin2bcd(tm->tm_year - 100);
  178. msg.addr = client->addr;
  179. msg.flags = 0;
  180. msg.len = sizeof(regs);
  181. msg.buf = regs;
  182. err = i2c_transfer(client->adapter, &msg, 1);
  183. if (err < 0) {
  184. /*
  185. * If the time cannot be set, restart the RTC anyway. Note
  186. * that errors are ignored if the RTC cannot be started so
  187. * that we have a chance to propagate the original error.
  188. */
  189. pcf8523_start_rtc(client);
  190. return err;
  191. }
  192. return pcf8523_start_rtc(client);
  193. }
  194. #ifdef CONFIG_RTC_INTF_DEV
  195. static int pcf8523_rtc_ioctl(struct device *dev, unsigned int cmd,
  196. unsigned long arg)
  197. {
  198. struct i2c_client *client = to_i2c_client(dev);
  199. u8 value;
  200. int ret = 0, err;
  201. switch (cmd) {
  202. case RTC_VL_READ:
  203. err = pcf8523_read(client, REG_CONTROL3, &value);
  204. if (err < 0)
  205. return err;
  206. if (value & REG_CONTROL3_BLF)
  207. ret = 1;
  208. if (copy_to_user((void __user *)arg, &ret, sizeof(int)))
  209. return -EFAULT;
  210. return 0;
  211. default:
  212. return -ENOIOCTLCMD;
  213. }
  214. }
  215. #else
  216. #define pcf8523_rtc_ioctl NULL
  217. #endif
  218. static const struct rtc_class_ops pcf8523_rtc_ops = {
  219. .read_time = pcf8523_rtc_read_time,
  220. .set_time = pcf8523_rtc_set_time,
  221. .ioctl = pcf8523_rtc_ioctl,
  222. };
  223. static int pcf8523_probe(struct i2c_client *client,
  224. const struct i2c_device_id *id)
  225. {
  226. struct pcf8523 *pcf;
  227. int err;
  228. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  229. return -ENODEV;
  230. pcf = devm_kzalloc(&client->dev, sizeof(*pcf), GFP_KERNEL);
  231. if (!pcf)
  232. return -ENOMEM;
  233. err = pcf8523_select_capacitance(client, true);
  234. if (err < 0)
  235. return err;
  236. err = pcf8523_set_pm(client, 0);
  237. if (err < 0)
  238. return err;
  239. pcf->rtc = devm_rtc_device_register(&client->dev, DRIVER_NAME,
  240. &pcf8523_rtc_ops, THIS_MODULE);
  241. if (IS_ERR(pcf->rtc))
  242. return PTR_ERR(pcf->rtc);
  243. i2c_set_clientdata(client, pcf);
  244. return 0;
  245. }
  246. static const struct i2c_device_id pcf8523_id[] = {
  247. { "pcf8523", 0 },
  248. { }
  249. };
  250. MODULE_DEVICE_TABLE(i2c, pcf8523_id);
  251. #ifdef CONFIG_OF
  252. static const struct of_device_id pcf8523_of_match[] = {
  253. { .compatible = "nxp,pcf8523" },
  254. { }
  255. };
  256. MODULE_DEVICE_TABLE(of, pcf8523_of_match);
  257. #endif
  258. static struct i2c_driver pcf8523_driver = {
  259. .driver = {
  260. .name = DRIVER_NAME,
  261. .of_match_table = of_match_ptr(pcf8523_of_match),
  262. },
  263. .probe = pcf8523_probe,
  264. .id_table = pcf8523_id,
  265. };
  266. module_i2c_driver(pcf8523_driver);
  267. MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
  268. MODULE_DESCRIPTION("NXP PCF8523 RTC driver");
  269. MODULE_LICENSE("GPL v2");