rtc-pcf8523.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. /*
  141. * If the oscillator was stopped, try to clear the flag. Upon
  142. * power-up the flag is always set, but if we cannot clear it
  143. * the oscillator isn't running properly for some reason. The
  144. * sensible thing therefore is to return an error, signalling
  145. * that the clock cannot be assumed to be correct.
  146. */
  147. regs[0] &= ~REG_SECONDS_OS;
  148. err = pcf8523_write(client, REG_SECONDS, regs[0]);
  149. if (err < 0)
  150. return err;
  151. err = pcf8523_read(client, REG_SECONDS, &regs[0]);
  152. if (err < 0)
  153. return err;
  154. if (regs[0] & REG_SECONDS_OS)
  155. return -EAGAIN;
  156. }
  157. tm->tm_sec = bcd2bin(regs[0] & 0x7f);
  158. tm->tm_min = bcd2bin(regs[1] & 0x7f);
  159. tm->tm_hour = bcd2bin(regs[2] & 0x3f);
  160. tm->tm_mday = bcd2bin(regs[3] & 0x3f);
  161. tm->tm_wday = regs[4] & 0x7;
  162. tm->tm_mon = bcd2bin(regs[5] & 0x1f) - 1;
  163. tm->tm_year = bcd2bin(regs[6]) + 100;
  164. return rtc_valid_tm(tm);
  165. }
  166. static int pcf8523_rtc_set_time(struct device *dev, struct rtc_time *tm)
  167. {
  168. struct i2c_client *client = to_i2c_client(dev);
  169. struct i2c_msg msg;
  170. u8 regs[8];
  171. int err;
  172. err = pcf8523_stop_rtc(client);
  173. if (err < 0)
  174. return err;
  175. regs[0] = REG_SECONDS;
  176. regs[1] = bin2bcd(tm->tm_sec);
  177. regs[2] = bin2bcd(tm->tm_min);
  178. regs[3] = bin2bcd(tm->tm_hour);
  179. regs[4] = bin2bcd(tm->tm_mday);
  180. regs[5] = tm->tm_wday;
  181. regs[6] = bin2bcd(tm->tm_mon + 1);
  182. regs[7] = bin2bcd(tm->tm_year - 100);
  183. msg.addr = client->addr;
  184. msg.flags = 0;
  185. msg.len = sizeof(regs);
  186. msg.buf = regs;
  187. err = i2c_transfer(client->adapter, &msg, 1);
  188. if (err < 0) {
  189. /*
  190. * If the time cannot be set, restart the RTC anyway. Note
  191. * that errors are ignored if the RTC cannot be started so
  192. * that we have a chance to propagate the original error.
  193. */
  194. pcf8523_start_rtc(client);
  195. return err;
  196. }
  197. return pcf8523_start_rtc(client);
  198. }
  199. #ifdef CONFIG_RTC_INTF_DEV
  200. static int pcf8523_rtc_ioctl(struct device *dev, unsigned int cmd,
  201. unsigned long arg)
  202. {
  203. struct i2c_client *client = to_i2c_client(dev);
  204. u8 value;
  205. int ret = 0, err;
  206. switch (cmd) {
  207. case RTC_VL_READ:
  208. err = pcf8523_read(client, REG_CONTROL3, &value);
  209. if (err < 0)
  210. return err;
  211. if (value & REG_CONTROL3_BLF)
  212. ret = 1;
  213. if (copy_to_user((void __user *)arg, &ret, sizeof(int)))
  214. return -EFAULT;
  215. return 0;
  216. default:
  217. return -ENOIOCTLCMD;
  218. }
  219. }
  220. #else
  221. #define pcf8523_rtc_ioctl NULL
  222. #endif
  223. static const struct rtc_class_ops pcf8523_rtc_ops = {
  224. .read_time = pcf8523_rtc_read_time,
  225. .set_time = pcf8523_rtc_set_time,
  226. .ioctl = pcf8523_rtc_ioctl,
  227. };
  228. static int pcf8523_probe(struct i2c_client *client,
  229. const struct i2c_device_id *id)
  230. {
  231. struct pcf8523 *pcf;
  232. int err;
  233. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  234. return -ENODEV;
  235. pcf = devm_kzalloc(&client->dev, sizeof(*pcf), GFP_KERNEL);
  236. if (!pcf)
  237. return -ENOMEM;
  238. err = pcf8523_select_capacitance(client, true);
  239. if (err < 0)
  240. return err;
  241. err = pcf8523_set_pm(client, 0);
  242. if (err < 0)
  243. return err;
  244. pcf->rtc = devm_rtc_device_register(&client->dev, DRIVER_NAME,
  245. &pcf8523_rtc_ops, THIS_MODULE);
  246. if (IS_ERR(pcf->rtc))
  247. return PTR_ERR(pcf->rtc);
  248. i2c_set_clientdata(client, pcf);
  249. return 0;
  250. }
  251. static const struct i2c_device_id pcf8523_id[] = {
  252. { "pcf8523", 0 },
  253. { }
  254. };
  255. MODULE_DEVICE_TABLE(i2c, pcf8523_id);
  256. #ifdef CONFIG_OF
  257. static const struct of_device_id pcf8523_of_match[] = {
  258. { .compatible = "nxp,pcf8523" },
  259. { }
  260. };
  261. MODULE_DEVICE_TABLE(of, pcf8523_of_match);
  262. #endif
  263. static struct i2c_driver pcf8523_driver = {
  264. .driver = {
  265. .name = DRIVER_NAME,
  266. .owner = THIS_MODULE,
  267. .of_match_table = of_match_ptr(pcf8523_of_match),
  268. },
  269. .probe = pcf8523_probe,
  270. .id_table = pcf8523_id,
  271. };
  272. module_i2c_driver(pcf8523_driver);
  273. MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
  274. MODULE_DESCRIPTION("NXP PCF8523 RTC driver");
  275. MODULE_LICENSE("GPL v2");