rtc-pcf85363.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * drivers/rtc/rtc-pcf85363.c
  3. *
  4. * Driver for NXP PCF85363 real-time clock.
  5. *
  6. * Copyright (C) 2017 Eric Nelson
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * Based loosely on rtc-8583 by Russell King, Wolfram Sang and Juergen Beisert
  13. */
  14. #include <linux/module.h>
  15. #include <linux/i2c.h>
  16. #include <linux/slab.h>
  17. #include <linux/rtc.h>
  18. #include <linux/init.h>
  19. #include <linux/err.h>
  20. #include <linux/errno.h>
  21. #include <linux/bcd.h>
  22. #include <linux/of.h>
  23. #include <linux/of_device.h>
  24. #include <linux/regmap.h>
  25. /*
  26. * Date/Time registers
  27. */
  28. #define DT_100THS 0x00
  29. #define DT_SECS 0x01
  30. #define DT_MINUTES 0x02
  31. #define DT_HOURS 0x03
  32. #define DT_DAYS 0x04
  33. #define DT_WEEKDAYS 0x05
  34. #define DT_MONTHS 0x06
  35. #define DT_YEARS 0x07
  36. /*
  37. * Alarm registers
  38. */
  39. #define DT_SECOND_ALM1 0x08
  40. #define DT_MINUTE_ALM1 0x09
  41. #define DT_HOUR_ALM1 0x0a
  42. #define DT_DAY_ALM1 0x0b
  43. #define DT_MONTH_ALM1 0x0c
  44. #define DT_MINUTE_ALM2 0x0d
  45. #define DT_HOUR_ALM2 0x0e
  46. #define DT_WEEKDAY_ALM2 0x0f
  47. #define DT_ALARM_EN 0x10
  48. /*
  49. * Time stamp registers
  50. */
  51. #define DT_TIMESTAMP1 0x11
  52. #define DT_TIMESTAMP2 0x17
  53. #define DT_TIMESTAMP3 0x1d
  54. #define DT_TS_MODE 0x23
  55. /*
  56. * control registers
  57. */
  58. #define CTRL_OFFSET 0x24
  59. #define CTRL_OSCILLATOR 0x25
  60. #define CTRL_BATTERY 0x26
  61. #define CTRL_PIN_IO 0x27
  62. #define CTRL_FUNCTION 0x28
  63. #define CTRL_INTA_EN 0x29
  64. #define CTRL_INTB_EN 0x2a
  65. #define CTRL_FLAGS 0x2b
  66. #define CTRL_RAMBYTE 0x2c
  67. #define CTRL_WDOG 0x2d
  68. #define CTRL_STOP_EN 0x2e
  69. #define CTRL_RESETS 0x2f
  70. #define CTRL_RAM 0x40
  71. #define NVRAM_SIZE 0x40
  72. static struct i2c_driver pcf85363_driver;
  73. struct pcf85363 {
  74. struct device *dev;
  75. struct rtc_device *rtc;
  76. struct regmap *regmap;
  77. };
  78. static int pcf85363_rtc_read_time(struct device *dev, struct rtc_time *tm)
  79. {
  80. struct pcf85363 *pcf85363 = dev_get_drvdata(dev);
  81. unsigned char buf[DT_YEARS + 1];
  82. int ret, len = sizeof(buf);
  83. /* read the RTC date and time registers all at once */
  84. ret = regmap_bulk_read(pcf85363->regmap, DT_100THS, buf, len);
  85. if (ret) {
  86. dev_err(dev, "%s: error %d\n", __func__, ret);
  87. return ret;
  88. }
  89. tm->tm_year = bcd2bin(buf[DT_YEARS]);
  90. /* adjust for 1900 base of rtc_time */
  91. tm->tm_year += 100;
  92. tm->tm_wday = buf[DT_WEEKDAYS] & 7;
  93. buf[DT_SECS] &= 0x7F;
  94. tm->tm_sec = bcd2bin(buf[DT_SECS]);
  95. buf[DT_MINUTES] &= 0x7F;
  96. tm->tm_min = bcd2bin(buf[DT_MINUTES]);
  97. tm->tm_hour = bcd2bin(buf[DT_HOURS]);
  98. tm->tm_mday = bcd2bin(buf[DT_DAYS]);
  99. tm->tm_mon = bcd2bin(buf[DT_MONTHS]) - 1;
  100. return 0;
  101. }
  102. static int pcf85363_rtc_set_time(struct device *dev, struct rtc_time *tm)
  103. {
  104. struct pcf85363 *pcf85363 = dev_get_drvdata(dev);
  105. unsigned char buf[DT_YEARS + 1];
  106. int len = sizeof(buf);
  107. buf[DT_100THS] = 0;
  108. buf[DT_SECS] = bin2bcd(tm->tm_sec);
  109. buf[DT_MINUTES] = bin2bcd(tm->tm_min);
  110. buf[DT_HOURS] = bin2bcd(tm->tm_hour);
  111. buf[DT_DAYS] = bin2bcd(tm->tm_mday);
  112. buf[DT_WEEKDAYS] = tm->tm_wday;
  113. buf[DT_MONTHS] = bin2bcd(tm->tm_mon + 1);
  114. buf[DT_YEARS] = bin2bcd(tm->tm_year % 100);
  115. return regmap_bulk_write(pcf85363->regmap, DT_100THS,
  116. buf, len);
  117. }
  118. static const struct rtc_class_ops rtc_ops = {
  119. .read_time = pcf85363_rtc_read_time,
  120. .set_time = pcf85363_rtc_set_time,
  121. };
  122. static int pcf85363_nvram_read(void *priv, unsigned int offset, void *val,
  123. size_t bytes)
  124. {
  125. struct pcf85363 *pcf85363 = priv;
  126. return regmap_bulk_read(pcf85363->regmap, CTRL_RAM + offset,
  127. val, bytes);
  128. }
  129. static int pcf85363_nvram_write(void *priv, unsigned int offset, void *val,
  130. size_t bytes)
  131. {
  132. struct pcf85363 *pcf85363 = priv;
  133. return regmap_bulk_write(pcf85363->regmap, CTRL_RAM + offset,
  134. val, bytes);
  135. }
  136. static const struct regmap_config regmap_config = {
  137. .reg_bits = 8,
  138. .val_bits = 8,
  139. .max_register = 0x7f,
  140. };
  141. static int pcf85363_probe(struct i2c_client *client,
  142. const struct i2c_device_id *id)
  143. {
  144. struct pcf85363 *pcf85363;
  145. struct nvmem_config nvmem_cfg = {
  146. .name = "pcf85363-",
  147. .word_size = 1,
  148. .stride = 1,
  149. .size = NVRAM_SIZE,
  150. .reg_read = pcf85363_nvram_read,
  151. .reg_write = pcf85363_nvram_write,
  152. };
  153. int ret;
  154. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  155. return -ENODEV;
  156. pcf85363 = devm_kzalloc(&client->dev, sizeof(struct pcf85363),
  157. GFP_KERNEL);
  158. if (!pcf85363)
  159. return -ENOMEM;
  160. pcf85363->regmap = devm_regmap_init_i2c(client, &regmap_config);
  161. if (IS_ERR(pcf85363->regmap)) {
  162. dev_err(&client->dev, "regmap allocation failed\n");
  163. return PTR_ERR(pcf85363->regmap);
  164. }
  165. pcf85363->dev = &client->dev;
  166. i2c_set_clientdata(client, pcf85363);
  167. pcf85363->rtc = devm_rtc_allocate_device(pcf85363->dev);
  168. if (IS_ERR(pcf85363->rtc))
  169. return PTR_ERR(pcf85363->rtc);
  170. pcf85363->rtc->ops = &rtc_ops;
  171. ret = rtc_register_device(pcf85363->rtc);
  172. nvmem_cfg.priv = pcf85363;
  173. rtc_nvmem_register(pcf85363->rtc, &nvmem_cfg);
  174. return ret;
  175. }
  176. static const struct of_device_id dev_ids[] = {
  177. { .compatible = "nxp,pcf85363" },
  178. {}
  179. };
  180. MODULE_DEVICE_TABLE(of, dev_ids);
  181. static struct i2c_driver pcf85363_driver = {
  182. .driver = {
  183. .name = "pcf85363",
  184. .of_match_table = of_match_ptr(dev_ids),
  185. },
  186. .probe = pcf85363_probe,
  187. };
  188. module_i2c_driver(pcf85363_driver);
  189. MODULE_AUTHOR("Eric Nelson");
  190. MODULE_DESCRIPTION("pcf85363 I2C RTC driver");
  191. MODULE_LICENSE("GPL");