rtc-bd70528.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. //
  3. // Copyright (C) 2018 ROHM Semiconductors
  4. //
  5. // RTC driver for ROHM BD71828 and BD71815 PMIC
  6. #include <linux/bcd.h>
  7. #include <linux/mfd/rohm-bd71815.h>
  8. #include <linux/mfd/rohm-bd71828.h>
  9. #include <linux/mfd/rohm-bd72720.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/regmap.h>
  14. #include <linux/rtc.h>
  15. /*
  16. * On BD71828 and BD71815 the ALM0 MASK is 14 bytes after the ALM0
  17. * block start
  18. */
  19. #define BD718XX_ALM_EN_OFFSET 14
  20. /*
  21. * We read regs RTC_SEC => RTC_YEAR
  22. * this struct is ordered according to chip registers.
  23. * Keep it u8 only (or packed) to avoid padding issues.
  24. */
  25. struct bd70528_rtc_day {
  26. u8 sec;
  27. u8 min;
  28. u8 hour;
  29. } __packed;
  30. struct bd70528_rtc_data {
  31. struct bd70528_rtc_day time;
  32. u8 week;
  33. u8 day;
  34. u8 month;
  35. u8 year;
  36. } __packed;
  37. struct bd71828_rtc_alm {
  38. struct bd70528_rtc_data alm0;
  39. struct bd70528_rtc_data alm1;
  40. u8 alm_mask;
  41. u8 alm1_mask;
  42. } __packed;
  43. struct bd70528_rtc {
  44. struct rohm_regmap_dev *parent;
  45. struct regmap *regmap;
  46. struct device *dev;
  47. u8 reg_time_start;
  48. u8 bd718xx_alm_block_start;
  49. };
  50. static inline void tmday2rtc(struct rtc_time *t, struct bd70528_rtc_day *d)
  51. {
  52. d->sec &= ~BD70528_MASK_RTC_SEC;
  53. d->min &= ~BD70528_MASK_RTC_MINUTE;
  54. d->hour &= ~BD70528_MASK_RTC_HOUR;
  55. d->sec |= bin2bcd(t->tm_sec);
  56. d->min |= bin2bcd(t->tm_min);
  57. d->hour |= bin2bcd(t->tm_hour);
  58. }
  59. static inline void tm2rtc(struct rtc_time *t, struct bd70528_rtc_data *r)
  60. {
  61. r->day &= ~BD70528_MASK_RTC_DAY;
  62. r->week &= ~BD70528_MASK_RTC_WEEK;
  63. r->month &= ~BD70528_MASK_RTC_MONTH;
  64. /*
  65. * PM and 24H bits are not used by Wake - thus we clear them
  66. * here and not in tmday2rtc() which is also used by wake.
  67. */
  68. r->time.hour &= ~(BD70528_MASK_RTC_HOUR_PM | BD70528_MASK_RTC_HOUR_24H);
  69. tmday2rtc(t, &r->time);
  70. /*
  71. * We do always set time in 24H mode.
  72. */
  73. r->time.hour |= BD70528_MASK_RTC_HOUR_24H;
  74. r->day |= bin2bcd(t->tm_mday);
  75. r->week |= bin2bcd(t->tm_wday);
  76. r->month |= bin2bcd(t->tm_mon + 1);
  77. r->year = bin2bcd(t->tm_year - 100);
  78. }
  79. static inline void rtc2tm(struct bd70528_rtc_data *r, struct rtc_time *t)
  80. {
  81. t->tm_sec = bcd2bin(r->time.sec & BD70528_MASK_RTC_SEC);
  82. t->tm_min = bcd2bin(r->time.min & BD70528_MASK_RTC_MINUTE);
  83. t->tm_hour = bcd2bin(r->time.hour & BD70528_MASK_RTC_HOUR);
  84. /*
  85. * If RTC is in 12H mode, then bit BD70528_MASK_RTC_HOUR_PM
  86. * is not BCD value but tells whether it is AM or PM
  87. */
  88. if (!(r->time.hour & BD70528_MASK_RTC_HOUR_24H)) {
  89. t->tm_hour %= 12;
  90. if (r->time.hour & BD70528_MASK_RTC_HOUR_PM)
  91. t->tm_hour += 12;
  92. }
  93. t->tm_mday = bcd2bin(r->day & BD70528_MASK_RTC_DAY);
  94. t->tm_mon = bcd2bin(r->month & BD70528_MASK_RTC_MONTH) - 1;
  95. t->tm_year = 100 + bcd2bin(r->year & BD70528_MASK_RTC_YEAR);
  96. t->tm_wday = bcd2bin(r->week & BD70528_MASK_RTC_WEEK);
  97. }
  98. static int bd71828_set_alarm(struct device *dev, struct rtc_wkalrm *a)
  99. {
  100. int ret;
  101. struct bd71828_rtc_alm alm;
  102. struct bd70528_rtc *r = dev_get_drvdata(dev);
  103. ret = regmap_bulk_read(r->regmap, r->bd718xx_alm_block_start, &alm,
  104. sizeof(alm));
  105. if (ret) {
  106. dev_err(dev, "Failed to read alarm regs\n");
  107. return ret;
  108. }
  109. tm2rtc(&a->time, &alm.alm0);
  110. if (!a->enabled)
  111. alm.alm_mask &= ~BD70528_MASK_ALM_EN;
  112. else
  113. alm.alm_mask |= BD70528_MASK_ALM_EN;
  114. ret = regmap_bulk_write(r->regmap, r->bd718xx_alm_block_start, &alm,
  115. sizeof(alm));
  116. if (ret)
  117. dev_err(dev, "Failed to set alarm time\n");
  118. return ret;
  119. }
  120. static int bd71828_read_alarm(struct device *dev, struct rtc_wkalrm *a)
  121. {
  122. int ret;
  123. struct bd71828_rtc_alm alm;
  124. struct bd70528_rtc *r = dev_get_drvdata(dev);
  125. ret = regmap_bulk_read(r->regmap, r->bd718xx_alm_block_start, &alm,
  126. sizeof(alm));
  127. if (ret) {
  128. dev_err(dev, "Failed to read alarm regs\n");
  129. return ret;
  130. }
  131. rtc2tm(&alm.alm0, &a->time);
  132. a->time.tm_mday = -1;
  133. a->time.tm_mon = -1;
  134. a->time.tm_year = -1;
  135. a->enabled = !!(alm.alm_mask & BD70528_MASK_ALM_EN);
  136. a->pending = 0;
  137. return 0;
  138. }
  139. static int bd71828_set_time(struct device *dev, struct rtc_time *t)
  140. {
  141. int ret;
  142. struct bd70528_rtc_data rtc_data;
  143. struct bd70528_rtc *r = dev_get_drvdata(dev);
  144. ret = regmap_bulk_read(r->regmap, r->reg_time_start, &rtc_data,
  145. sizeof(rtc_data));
  146. if (ret) {
  147. dev_err(dev, "Failed to read RTC time registers\n");
  148. return ret;
  149. }
  150. tm2rtc(t, &rtc_data);
  151. ret = regmap_bulk_write(r->regmap, r->reg_time_start, &rtc_data,
  152. sizeof(rtc_data));
  153. if (ret)
  154. dev_err(dev, "Failed to set RTC time\n");
  155. return ret;
  156. }
  157. static int bd70528_get_time(struct device *dev, struct rtc_time *t)
  158. {
  159. struct bd70528_rtc *r = dev_get_drvdata(dev);
  160. struct bd70528_rtc_data rtc_data;
  161. int ret;
  162. /* read the RTC date and time registers all at once */
  163. ret = regmap_bulk_read(r->regmap, r->reg_time_start, &rtc_data,
  164. sizeof(rtc_data));
  165. if (ret) {
  166. dev_err(dev, "Failed to read RTC time (err %d)\n", ret);
  167. return ret;
  168. }
  169. rtc2tm(&rtc_data, t);
  170. return 0;
  171. }
  172. static int bd71828_alm_enable(struct device *dev, unsigned int enabled)
  173. {
  174. int ret;
  175. struct bd70528_rtc *r = dev_get_drvdata(dev);
  176. unsigned int enableval = BD70528_MASK_ALM_EN;
  177. if (!enabled)
  178. enableval = 0;
  179. ret = regmap_update_bits(r->regmap, r->bd718xx_alm_block_start +
  180. BD718XX_ALM_EN_OFFSET, BD70528_MASK_ALM_EN,
  181. enableval);
  182. if (ret)
  183. dev_err(dev, "Failed to change alarm state\n");
  184. return ret;
  185. }
  186. static const struct rtc_class_ops bd71828_rtc_ops = {
  187. .read_time = bd70528_get_time,
  188. .set_time = bd71828_set_time,
  189. .read_alarm = bd71828_read_alarm,
  190. .set_alarm = bd71828_set_alarm,
  191. .alarm_irq_enable = bd71828_alm_enable,
  192. };
  193. static irqreturn_t alm_hndlr(int irq, void *data)
  194. {
  195. struct rtc_device *rtc = data;
  196. rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF | RTC_PF);
  197. return IRQ_HANDLED;
  198. }
  199. static int bd70528_probe(struct platform_device *pdev)
  200. {
  201. struct bd70528_rtc *bd_rtc;
  202. const struct rtc_class_ops *rtc_ops;
  203. int ret;
  204. struct rtc_device *rtc;
  205. int irq;
  206. unsigned int hr;
  207. u8 hour_reg;
  208. enum rohm_chip_type chip = platform_get_device_id(pdev)->driver_data;
  209. bd_rtc = devm_kzalloc(&pdev->dev, sizeof(*bd_rtc), GFP_KERNEL);
  210. if (!bd_rtc)
  211. return -ENOMEM;
  212. bd_rtc->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  213. if (!bd_rtc->regmap) {
  214. dev_err(&pdev->dev, "No regmap\n");
  215. return -EINVAL;
  216. }
  217. bd_rtc->dev = &pdev->dev;
  218. rtc_ops = &bd71828_rtc_ops;
  219. switch (chip) {
  220. case ROHM_CHIP_TYPE_BD71815:
  221. bd_rtc->reg_time_start = BD71815_REG_RTC_START;
  222. /*
  223. * See also BD718XX_ALM_EN_OFFSET:
  224. * This works for BD71828, BD71815, and BD72720 as they all
  225. * have same offset between the ALM0 start and the ALM0_MASK.
  226. * If new ICs are to be added this requires proper check as
  227. * the ALM0_MASK is not located at the end of ALM0 block -
  228. * but after all ALM blocks. If amount of ALMs differ, the
  229. * offset to enable/disable is likely to be incorrect and
  230. * enable/disable must be given as own reg address here.
  231. */
  232. bd_rtc->bd718xx_alm_block_start = BD71815_REG_RTC_ALM_START;
  233. hour_reg = BD71815_REG_HOUR;
  234. break;
  235. case ROHM_CHIP_TYPE_BD71828:
  236. bd_rtc->reg_time_start = BD71828_REG_RTC_START;
  237. bd_rtc->bd718xx_alm_block_start = BD71828_REG_RTC_ALM_START;
  238. hour_reg = BD71828_REG_RTC_HOUR;
  239. break;
  240. case ROHM_CHIP_TYPE_BD72720:
  241. bd_rtc->reg_time_start = BD72720_REG_RTC_START;
  242. bd_rtc->bd718xx_alm_block_start = BD72720_REG_RTC_ALM_START;
  243. hour_reg = BD72720_REG_RTC_HOUR;
  244. break;
  245. default:
  246. dev_err(&pdev->dev, "Unknown chip\n");
  247. return -ENOENT;
  248. }
  249. irq = platform_get_irq_byname(pdev, "bd70528-rtc-alm-0");
  250. if (irq < 0)
  251. return irq;
  252. platform_set_drvdata(pdev, bd_rtc);
  253. ret = regmap_read(bd_rtc->regmap, hour_reg, &hr);
  254. if (ret) {
  255. dev_err(&pdev->dev, "Failed to reag RTC clock\n");
  256. return ret;
  257. }
  258. if (!(hr & BD70528_MASK_RTC_HOUR_24H)) {
  259. struct rtc_time t;
  260. ret = rtc_ops->read_time(&pdev->dev, &t);
  261. if (!ret)
  262. ret = rtc_ops->set_time(&pdev->dev, &t);
  263. if (ret) {
  264. dev_err(&pdev->dev,
  265. "Setting 24H clock for RTC failed\n");
  266. return ret;
  267. }
  268. }
  269. device_set_wakeup_capable(&pdev->dev, true);
  270. device_wakeup_enable(&pdev->dev);
  271. rtc = devm_rtc_allocate_device(&pdev->dev);
  272. if (IS_ERR(rtc)) {
  273. dev_err(&pdev->dev, "RTC device creation failed\n");
  274. return PTR_ERR(rtc);
  275. }
  276. rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
  277. rtc->range_max = RTC_TIMESTAMP_END_2099;
  278. rtc->ops = rtc_ops;
  279. /* Request alarm IRQ prior to registerig the RTC */
  280. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, &alm_hndlr,
  281. IRQF_ONESHOT, "bd70528-rtc", rtc);
  282. if (ret)
  283. return ret;
  284. return devm_rtc_register_device(rtc);
  285. }
  286. static const struct platform_device_id bd718x7_rtc_id[] = {
  287. { "bd71828-rtc", ROHM_CHIP_TYPE_BD71828 },
  288. { "bd71815-rtc", ROHM_CHIP_TYPE_BD71815 },
  289. { "bd72720-rtc", ROHM_CHIP_TYPE_BD72720 },
  290. { },
  291. };
  292. MODULE_DEVICE_TABLE(platform, bd718x7_rtc_id);
  293. static struct platform_driver bd70528_rtc = {
  294. .driver = {
  295. .name = "bd70528-rtc"
  296. },
  297. .probe = bd70528_probe,
  298. .id_table = bd718x7_rtc_id,
  299. };
  300. module_platform_driver(bd70528_rtc);
  301. MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
  302. MODULE_DESCRIPTION("ROHM BD71828 and BD71815 PMIC RTC driver");
  303. MODULE_LICENSE("GPL");
  304. MODULE_ALIAS("platform:bd70528-rtc");