rtc-pcf8523.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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_voltage_low(struct i2c_client *client)
  68. {
  69. u8 value;
  70. int err;
  71. err = pcf8523_read(client, REG_CONTROL3, &value);
  72. if (err < 0)
  73. return err;
  74. return !!(value & REG_CONTROL3_BLF);
  75. }
  76. static int pcf8523_load_capacitance(struct i2c_client *client)
  77. {
  78. u32 load;
  79. u8 value;
  80. int err;
  81. err = pcf8523_read(client, REG_CONTROL1, &value);
  82. if (err < 0)
  83. return err;
  84. load = 12500;
  85. of_property_read_u32(client->dev.of_node, "quartz-load-femtofarads",
  86. &load);
  87. switch (load) {
  88. default:
  89. dev_warn(&client->dev, "Unknown quartz-load-femtofarads value: %d. Assuming 12500",
  90. load);
  91. /* fall through */
  92. case 12500:
  93. value |= REG_CONTROL1_CAP_SEL;
  94. break;
  95. case 7000:
  96. value &= ~REG_CONTROL1_CAP_SEL;
  97. break;
  98. }
  99. err = pcf8523_write(client, REG_CONTROL1, value);
  100. return err;
  101. }
  102. static int pcf8523_set_pm(struct i2c_client *client, u8 pm)
  103. {
  104. u8 value;
  105. int err;
  106. err = pcf8523_read(client, REG_CONTROL3, &value);
  107. if (err < 0)
  108. return err;
  109. value = (value & ~REG_CONTROL3_PM_MASK) | pm;
  110. err = pcf8523_write(client, REG_CONTROL3, value);
  111. if (err < 0)
  112. return err;
  113. return 0;
  114. }
  115. static int pcf8523_stop_rtc(struct i2c_client *client)
  116. {
  117. u8 value;
  118. int err;
  119. err = pcf8523_read(client, REG_CONTROL1, &value);
  120. if (err < 0)
  121. return err;
  122. value |= REG_CONTROL1_STOP;
  123. err = pcf8523_write(client, REG_CONTROL1, value);
  124. if (err < 0)
  125. return err;
  126. return 0;
  127. }
  128. static int pcf8523_start_rtc(struct i2c_client *client)
  129. {
  130. u8 value;
  131. int err;
  132. err = pcf8523_read(client, REG_CONTROL1, &value);
  133. if (err < 0)
  134. return err;
  135. value &= ~REG_CONTROL1_STOP;
  136. err = pcf8523_write(client, REG_CONTROL1, value);
  137. if (err < 0)
  138. return err;
  139. return 0;
  140. }
  141. static int pcf8523_rtc_read_time(struct device *dev, struct rtc_time *tm)
  142. {
  143. struct i2c_client *client = to_i2c_client(dev);
  144. u8 start = REG_SECONDS, regs[7];
  145. struct i2c_msg msgs[2];
  146. int err;
  147. err = pcf8523_voltage_low(client);
  148. if (err < 0) {
  149. return err;
  150. } else if (err > 0) {
  151. dev_err(dev, "low voltage detected, time is unreliable\n");
  152. return -EINVAL;
  153. }
  154. msgs[0].addr = client->addr;
  155. msgs[0].flags = 0;
  156. msgs[0].len = 1;
  157. msgs[0].buf = &start;
  158. msgs[1].addr = client->addr;
  159. msgs[1].flags = I2C_M_RD;
  160. msgs[1].len = sizeof(regs);
  161. msgs[1].buf = regs;
  162. err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  163. if (err < 0)
  164. return err;
  165. if (regs[0] & REG_SECONDS_OS)
  166. return -EINVAL;
  167. tm->tm_sec = bcd2bin(regs[0] & 0x7f);
  168. tm->tm_min = bcd2bin(regs[1] & 0x7f);
  169. tm->tm_hour = bcd2bin(regs[2] & 0x3f);
  170. tm->tm_mday = bcd2bin(regs[3] & 0x3f);
  171. tm->tm_wday = regs[4] & 0x7;
  172. tm->tm_mon = bcd2bin(regs[5] & 0x1f) - 1;
  173. tm->tm_year = bcd2bin(regs[6]) + 100;
  174. return rtc_valid_tm(tm);
  175. }
  176. static int pcf8523_rtc_set_time(struct device *dev, struct rtc_time *tm)
  177. {
  178. struct i2c_client *client = to_i2c_client(dev);
  179. struct i2c_msg msg;
  180. u8 regs[8];
  181. int err;
  182. /*
  183. * The hardware can only store values between 0 and 99 in it's YEAR
  184. * register (with 99 overflowing to 0 on increment).
  185. * After 2100-02-28 we could start interpreting the year to be in the
  186. * interval [2100, 2199], but there is no path to switch in a smooth way
  187. * because the chip handles YEAR=0x00 (and the out-of-spec
  188. * YEAR=0xa0) as a leap year, but 2100 isn't.
  189. */
  190. if (tm->tm_year < 100 || tm->tm_year >= 200)
  191. return -EINVAL;
  192. err = pcf8523_stop_rtc(client);
  193. if (err < 0)
  194. return err;
  195. regs[0] = REG_SECONDS;
  196. /* This will purposely overwrite REG_SECONDS_OS */
  197. regs[1] = bin2bcd(tm->tm_sec);
  198. regs[2] = bin2bcd(tm->tm_min);
  199. regs[3] = bin2bcd(tm->tm_hour);
  200. regs[4] = bin2bcd(tm->tm_mday);
  201. regs[5] = tm->tm_wday;
  202. regs[6] = bin2bcd(tm->tm_mon + 1);
  203. regs[7] = bin2bcd(tm->tm_year - 100);
  204. msg.addr = client->addr;
  205. msg.flags = 0;
  206. msg.len = sizeof(regs);
  207. msg.buf = regs;
  208. err = i2c_transfer(client->adapter, &msg, 1);
  209. if (err < 0) {
  210. /*
  211. * If the time cannot be set, restart the RTC anyway. Note
  212. * that errors are ignored if the RTC cannot be started so
  213. * that we have a chance to propagate the original error.
  214. */
  215. pcf8523_start_rtc(client);
  216. return err;
  217. }
  218. return pcf8523_start_rtc(client);
  219. }
  220. #ifdef CONFIG_RTC_INTF_DEV
  221. static int pcf8523_rtc_ioctl(struct device *dev, unsigned int cmd,
  222. unsigned long arg)
  223. {
  224. struct i2c_client *client = to_i2c_client(dev);
  225. int ret;
  226. switch (cmd) {
  227. case RTC_VL_READ:
  228. ret = pcf8523_voltage_low(client);
  229. if (ret < 0)
  230. return ret;
  231. if (copy_to_user((void __user *)arg, &ret, sizeof(int)))
  232. return -EFAULT;
  233. return 0;
  234. default:
  235. return -ENOIOCTLCMD;
  236. }
  237. }
  238. #else
  239. #define pcf8523_rtc_ioctl NULL
  240. #endif
  241. static const struct rtc_class_ops pcf8523_rtc_ops = {
  242. .read_time = pcf8523_rtc_read_time,
  243. .set_time = pcf8523_rtc_set_time,
  244. .ioctl = pcf8523_rtc_ioctl,
  245. };
  246. static int pcf8523_probe(struct i2c_client *client,
  247. const struct i2c_device_id *id)
  248. {
  249. struct pcf8523 *pcf;
  250. int err;
  251. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  252. return -ENODEV;
  253. pcf = devm_kzalloc(&client->dev, sizeof(*pcf), GFP_KERNEL);
  254. if (!pcf)
  255. return -ENOMEM;
  256. err = pcf8523_load_capacitance(client);
  257. if (err < 0)
  258. dev_warn(&client->dev, "failed to set xtal load capacitance: %d",
  259. err);
  260. err = pcf8523_set_pm(client, 0);
  261. if (err < 0)
  262. return err;
  263. pcf->rtc = devm_rtc_device_register(&client->dev, DRIVER_NAME,
  264. &pcf8523_rtc_ops, THIS_MODULE);
  265. if (IS_ERR(pcf->rtc))
  266. return PTR_ERR(pcf->rtc);
  267. i2c_set_clientdata(client, pcf);
  268. return 0;
  269. }
  270. static const struct i2c_device_id pcf8523_id[] = {
  271. { "pcf8523", 0 },
  272. { }
  273. };
  274. MODULE_DEVICE_TABLE(i2c, pcf8523_id);
  275. #ifdef CONFIG_OF
  276. static const struct of_device_id pcf8523_of_match[] = {
  277. { .compatible = "nxp,pcf8523" },
  278. { }
  279. };
  280. MODULE_DEVICE_TABLE(of, pcf8523_of_match);
  281. #endif
  282. static struct i2c_driver pcf8523_driver = {
  283. .driver = {
  284. .name = DRIVER_NAME,
  285. .of_match_table = of_match_ptr(pcf8523_of_match),
  286. },
  287. .probe = pcf8523_probe,
  288. .id_table = pcf8523_id,
  289. };
  290. module_i2c_driver(pcf8523_driver);
  291. MODULE_AUTHOR("Thierry Reding <[email protected]>");
  292. MODULE_DESCRIPTION("NXP PCF8523 RTC driver");
  293. MODULE_LICENSE("GPL v2");