rtc-hym8563.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Haoyu HYM8563 RTC driver
  4. *
  5. * Copyright (C) 2013 MundoReader S.L.
  6. * Author: Heiko Stuebner <[email protected]>
  7. *
  8. * based on rtc-HYM8563
  9. * Copyright (C) 2010 ROCKCHIP, Inc.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/clk-provider.h>
  13. #include <linux/i2c.h>
  14. #include <linux/bcd.h>
  15. #include <linux/rtc.h>
  16. #define HYM8563_CTL1 0x00
  17. #define HYM8563_CTL1_TEST BIT(7)
  18. #define HYM8563_CTL1_STOP BIT(5)
  19. #define HYM8563_CTL1_TESTC BIT(3)
  20. #define HYM8563_CTL2 0x01
  21. #define HYM8563_CTL2_TI_TP BIT(4)
  22. #define HYM8563_CTL2_AF BIT(3)
  23. #define HYM8563_CTL2_TF BIT(2)
  24. #define HYM8563_CTL2_AIE BIT(1)
  25. #define HYM8563_CTL2_TIE BIT(0)
  26. #define HYM8563_SEC 0x02
  27. #define HYM8563_SEC_VL BIT(7)
  28. #define HYM8563_SEC_MASK 0x7f
  29. #define HYM8563_MIN 0x03
  30. #define HYM8563_MIN_MASK 0x7f
  31. #define HYM8563_HOUR 0x04
  32. #define HYM8563_HOUR_MASK 0x3f
  33. #define HYM8563_DAY 0x05
  34. #define HYM8563_DAY_MASK 0x3f
  35. #define HYM8563_WEEKDAY 0x06
  36. #define HYM8563_WEEKDAY_MASK 0x07
  37. #define HYM8563_MONTH 0x07
  38. #define HYM8563_MONTH_CENTURY BIT(7)
  39. #define HYM8563_MONTH_MASK 0x1f
  40. #define HYM8563_YEAR 0x08
  41. #define HYM8563_ALM_MIN 0x09
  42. #define HYM8563_ALM_HOUR 0x0a
  43. #define HYM8563_ALM_DAY 0x0b
  44. #define HYM8563_ALM_WEEK 0x0c
  45. /* Each alarm check can be disabled by setting this bit in the register */
  46. #define HYM8563_ALM_BIT_DISABLE BIT(7)
  47. #define HYM8563_CLKOUT 0x0d
  48. #define HYM8563_CLKOUT_ENABLE BIT(7)
  49. #define HYM8563_CLKOUT_32768 0
  50. #define HYM8563_CLKOUT_1024 1
  51. #define HYM8563_CLKOUT_32 2
  52. #define HYM8563_CLKOUT_1 3
  53. #define HYM8563_CLKOUT_MASK 3
  54. #define HYM8563_TMR_CTL 0x0e
  55. #define HYM8563_TMR_CTL_ENABLE BIT(7)
  56. #define HYM8563_TMR_CTL_4096 0
  57. #define HYM8563_TMR_CTL_64 1
  58. #define HYM8563_TMR_CTL_1 2
  59. #define HYM8563_TMR_CTL_1_60 3
  60. #define HYM8563_TMR_CTL_MASK 3
  61. #define HYM8563_TMR_CNT 0x0f
  62. struct hym8563 {
  63. struct i2c_client *client;
  64. struct rtc_device *rtc;
  65. #ifdef CONFIG_COMMON_CLK
  66. struct clk_hw clkout_hw;
  67. #endif
  68. };
  69. /*
  70. * RTC handling
  71. */
  72. static int hym8563_rtc_read_time(struct device *dev, struct rtc_time *tm)
  73. {
  74. struct i2c_client *client = to_i2c_client(dev);
  75. u8 buf[7];
  76. int ret;
  77. ret = i2c_smbus_read_i2c_block_data(client, HYM8563_SEC, 7, buf);
  78. if (ret < 0)
  79. return ret;
  80. if (buf[0] & HYM8563_SEC_VL) {
  81. dev_warn(&client->dev,
  82. "no valid clock/calendar values available\n");
  83. return -EINVAL;
  84. }
  85. tm->tm_sec = bcd2bin(buf[0] & HYM8563_SEC_MASK);
  86. tm->tm_min = bcd2bin(buf[1] & HYM8563_MIN_MASK);
  87. tm->tm_hour = bcd2bin(buf[2] & HYM8563_HOUR_MASK);
  88. tm->tm_mday = bcd2bin(buf[3] & HYM8563_DAY_MASK);
  89. tm->tm_wday = bcd2bin(buf[4] & HYM8563_WEEKDAY_MASK); /* 0 = Sun */
  90. tm->tm_mon = bcd2bin(buf[5] & HYM8563_MONTH_MASK) - 1; /* 0 = Jan */
  91. tm->tm_year = bcd2bin(buf[6]) + 100;
  92. return 0;
  93. }
  94. static int hym8563_rtc_set_time(struct device *dev, struct rtc_time *tm)
  95. {
  96. struct i2c_client *client = to_i2c_client(dev);
  97. u8 buf[7];
  98. int ret;
  99. /* Years >= 2100 are to far in the future, 19XX is to early */
  100. if (tm->tm_year < 100 || tm->tm_year >= 200)
  101. return -EINVAL;
  102. buf[0] = bin2bcd(tm->tm_sec);
  103. buf[1] = bin2bcd(tm->tm_min);
  104. buf[2] = bin2bcd(tm->tm_hour);
  105. buf[3] = bin2bcd(tm->tm_mday);
  106. buf[4] = bin2bcd(tm->tm_wday);
  107. buf[5] = bin2bcd(tm->tm_mon + 1);
  108. /*
  109. * While the HYM8563 has a century flag in the month register,
  110. * it does not seem to carry it over a subsequent write/read.
  111. * So we'll limit ourself to 100 years, starting at 2000 for now.
  112. */
  113. buf[6] = bin2bcd(tm->tm_year - 100);
  114. /*
  115. * CTL1 only contains TEST-mode bits apart from stop,
  116. * so no need to read the value first
  117. */
  118. ret = i2c_smbus_write_byte_data(client, HYM8563_CTL1,
  119. HYM8563_CTL1_STOP);
  120. if (ret < 0)
  121. return ret;
  122. ret = i2c_smbus_write_i2c_block_data(client, HYM8563_SEC, 7, buf);
  123. if (ret < 0)
  124. return ret;
  125. ret = i2c_smbus_write_byte_data(client, HYM8563_CTL1, 0);
  126. if (ret < 0)
  127. return ret;
  128. return 0;
  129. }
  130. static int hym8563_rtc_alarm_irq_enable(struct device *dev,
  131. unsigned int enabled)
  132. {
  133. struct i2c_client *client = to_i2c_client(dev);
  134. int data;
  135. data = i2c_smbus_read_byte_data(client, HYM8563_CTL2);
  136. if (data < 0)
  137. return data;
  138. if (enabled)
  139. data |= HYM8563_CTL2_AIE;
  140. else
  141. data &= ~HYM8563_CTL2_AIE;
  142. return i2c_smbus_write_byte_data(client, HYM8563_CTL2, data);
  143. };
  144. static int hym8563_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
  145. {
  146. struct i2c_client *client = to_i2c_client(dev);
  147. struct rtc_time *alm_tm = &alm->time;
  148. u8 buf[4];
  149. int ret;
  150. ret = i2c_smbus_read_i2c_block_data(client, HYM8563_ALM_MIN, 4, buf);
  151. if (ret < 0)
  152. return ret;
  153. /* The alarm only has a minute accuracy */
  154. alm_tm->tm_sec = 0;
  155. alm_tm->tm_min = (buf[0] & HYM8563_ALM_BIT_DISABLE) ?
  156. -1 :
  157. bcd2bin(buf[0] & HYM8563_MIN_MASK);
  158. alm_tm->tm_hour = (buf[1] & HYM8563_ALM_BIT_DISABLE) ?
  159. -1 :
  160. bcd2bin(buf[1] & HYM8563_HOUR_MASK);
  161. alm_tm->tm_mday = (buf[2] & HYM8563_ALM_BIT_DISABLE) ?
  162. -1 :
  163. bcd2bin(buf[2] & HYM8563_DAY_MASK);
  164. alm_tm->tm_wday = (buf[3] & HYM8563_ALM_BIT_DISABLE) ?
  165. -1 :
  166. bcd2bin(buf[3] & HYM8563_WEEKDAY_MASK);
  167. ret = i2c_smbus_read_byte_data(client, HYM8563_CTL2);
  168. if (ret < 0)
  169. return ret;
  170. if (ret & HYM8563_CTL2_AIE)
  171. alm->enabled = 1;
  172. return 0;
  173. }
  174. static int hym8563_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
  175. {
  176. struct i2c_client *client = to_i2c_client(dev);
  177. struct rtc_time *alm_tm = &alm->time;
  178. u8 buf[4];
  179. int ret;
  180. ret = i2c_smbus_read_byte_data(client, HYM8563_CTL2);
  181. if (ret < 0)
  182. return ret;
  183. ret &= ~HYM8563_CTL2_AIE;
  184. ret = i2c_smbus_write_byte_data(client, HYM8563_CTL2, ret);
  185. if (ret < 0)
  186. return ret;
  187. buf[0] = (alm_tm->tm_min < 60 && alm_tm->tm_min >= 0) ?
  188. bin2bcd(alm_tm->tm_min) : HYM8563_ALM_BIT_DISABLE;
  189. buf[1] = (alm_tm->tm_hour < 24 && alm_tm->tm_hour >= 0) ?
  190. bin2bcd(alm_tm->tm_hour) : HYM8563_ALM_BIT_DISABLE;
  191. buf[2] = (alm_tm->tm_mday <= 31 && alm_tm->tm_mday >= 1) ?
  192. bin2bcd(alm_tm->tm_mday) : HYM8563_ALM_BIT_DISABLE;
  193. buf[3] = (alm_tm->tm_wday < 7 && alm_tm->tm_wday >= 0) ?
  194. bin2bcd(alm_tm->tm_wday) : HYM8563_ALM_BIT_DISABLE;
  195. ret = i2c_smbus_write_i2c_block_data(client, HYM8563_ALM_MIN, 4, buf);
  196. if (ret < 0)
  197. return ret;
  198. return hym8563_rtc_alarm_irq_enable(dev, alm->enabled);
  199. }
  200. static const struct rtc_class_ops hym8563_rtc_ops = {
  201. .read_time = hym8563_rtc_read_time,
  202. .set_time = hym8563_rtc_set_time,
  203. .alarm_irq_enable = hym8563_rtc_alarm_irq_enable,
  204. .read_alarm = hym8563_rtc_read_alarm,
  205. .set_alarm = hym8563_rtc_set_alarm,
  206. };
  207. /*
  208. * Handling of the clkout
  209. */
  210. #ifdef CONFIG_COMMON_CLK
  211. #define clkout_hw_to_hym8563(_hw) container_of(_hw, struct hym8563, clkout_hw)
  212. static int clkout_rates[] = {
  213. 32768,
  214. 1024,
  215. 32,
  216. 1,
  217. };
  218. static unsigned long hym8563_clkout_recalc_rate(struct clk_hw *hw,
  219. unsigned long parent_rate)
  220. {
  221. struct hym8563 *hym8563 = clkout_hw_to_hym8563(hw);
  222. struct i2c_client *client = hym8563->client;
  223. int ret = i2c_smbus_read_byte_data(client, HYM8563_CLKOUT);
  224. if (ret < 0)
  225. return 0;
  226. ret &= HYM8563_CLKOUT_MASK;
  227. return clkout_rates[ret];
  228. }
  229. static long hym8563_clkout_round_rate(struct clk_hw *hw, unsigned long rate,
  230. unsigned long *prate)
  231. {
  232. int i;
  233. for (i = 0; i < ARRAY_SIZE(clkout_rates); i++)
  234. if (clkout_rates[i] <= rate)
  235. return clkout_rates[i];
  236. return 0;
  237. }
  238. static int hym8563_clkout_set_rate(struct clk_hw *hw, unsigned long rate,
  239. unsigned long parent_rate)
  240. {
  241. struct hym8563 *hym8563 = clkout_hw_to_hym8563(hw);
  242. struct i2c_client *client = hym8563->client;
  243. int ret = i2c_smbus_read_byte_data(client, HYM8563_CLKOUT);
  244. int i;
  245. if (ret < 0)
  246. return ret;
  247. for (i = 0; i < ARRAY_SIZE(clkout_rates); i++)
  248. if (clkout_rates[i] == rate) {
  249. ret &= ~HYM8563_CLKOUT_MASK;
  250. ret |= i;
  251. return i2c_smbus_write_byte_data(client,
  252. HYM8563_CLKOUT, ret);
  253. }
  254. return -EINVAL;
  255. }
  256. static int hym8563_clkout_control(struct clk_hw *hw, bool enable)
  257. {
  258. struct hym8563 *hym8563 = clkout_hw_to_hym8563(hw);
  259. struct i2c_client *client = hym8563->client;
  260. int ret = i2c_smbus_read_byte_data(client, HYM8563_CLKOUT);
  261. if (ret < 0)
  262. return ret;
  263. if (enable)
  264. ret |= HYM8563_CLKOUT_ENABLE;
  265. else
  266. ret &= ~HYM8563_CLKOUT_ENABLE;
  267. return i2c_smbus_write_byte_data(client, HYM8563_CLKOUT, ret);
  268. }
  269. static int hym8563_clkout_prepare(struct clk_hw *hw)
  270. {
  271. return hym8563_clkout_control(hw, 1);
  272. }
  273. static void hym8563_clkout_unprepare(struct clk_hw *hw)
  274. {
  275. hym8563_clkout_control(hw, 0);
  276. }
  277. static int hym8563_clkout_is_prepared(struct clk_hw *hw)
  278. {
  279. struct hym8563 *hym8563 = clkout_hw_to_hym8563(hw);
  280. struct i2c_client *client = hym8563->client;
  281. int ret = i2c_smbus_read_byte_data(client, HYM8563_CLKOUT);
  282. if (ret < 0)
  283. return ret;
  284. return !!(ret & HYM8563_CLKOUT_ENABLE);
  285. }
  286. static const struct clk_ops hym8563_clkout_ops = {
  287. .prepare = hym8563_clkout_prepare,
  288. .unprepare = hym8563_clkout_unprepare,
  289. .is_prepared = hym8563_clkout_is_prepared,
  290. .recalc_rate = hym8563_clkout_recalc_rate,
  291. .round_rate = hym8563_clkout_round_rate,
  292. .set_rate = hym8563_clkout_set_rate,
  293. };
  294. static struct clk *hym8563_clkout_register_clk(struct hym8563 *hym8563)
  295. {
  296. struct i2c_client *client = hym8563->client;
  297. struct device_node *node = client->dev.of_node;
  298. struct clk *clk;
  299. struct clk_init_data init;
  300. int ret;
  301. ret = i2c_smbus_write_byte_data(client, HYM8563_CLKOUT,
  302. 0);
  303. if (ret < 0)
  304. return ERR_PTR(ret);
  305. init.name = "hym8563-clkout";
  306. init.ops = &hym8563_clkout_ops;
  307. init.flags = 0;
  308. init.parent_names = NULL;
  309. init.num_parents = 0;
  310. hym8563->clkout_hw.init = &init;
  311. /* optional override of the clockname */
  312. of_property_read_string(node, "clock-output-names", &init.name);
  313. /* register the clock */
  314. clk = clk_register(&client->dev, &hym8563->clkout_hw);
  315. if (!IS_ERR(clk))
  316. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  317. return clk;
  318. }
  319. #endif
  320. /*
  321. * The alarm interrupt is implemented as a level-low interrupt in the
  322. * hym8563, while the timer interrupt uses a falling edge.
  323. * We don't use the timer at all, so the interrupt is requested to
  324. * use the level-low trigger.
  325. */
  326. static irqreturn_t hym8563_irq(int irq, void *dev_id)
  327. {
  328. struct hym8563 *hym8563 = (struct hym8563 *)dev_id;
  329. struct i2c_client *client = hym8563->client;
  330. int data, ret;
  331. rtc_lock(hym8563->rtc);
  332. /* Clear the alarm flag */
  333. data = i2c_smbus_read_byte_data(client, HYM8563_CTL2);
  334. if (data < 0) {
  335. dev_err(&client->dev, "%s: error reading i2c data %d\n",
  336. __func__, data);
  337. goto out;
  338. }
  339. data &= ~HYM8563_CTL2_AF;
  340. ret = i2c_smbus_write_byte_data(client, HYM8563_CTL2, data);
  341. if (ret < 0) {
  342. dev_err(&client->dev, "%s: error writing i2c data %d\n",
  343. __func__, ret);
  344. }
  345. out:
  346. rtc_unlock(hym8563->rtc);
  347. return IRQ_HANDLED;
  348. }
  349. static int hym8563_init_device(struct i2c_client *client)
  350. {
  351. int ret;
  352. /* Clear stop flag if present */
  353. ret = i2c_smbus_write_byte_data(client, HYM8563_CTL1, 0);
  354. if (ret < 0)
  355. return ret;
  356. ret = i2c_smbus_read_byte_data(client, HYM8563_CTL2);
  357. if (ret < 0)
  358. return ret;
  359. /* Disable alarm and timer interrupts */
  360. ret &= ~HYM8563_CTL2_AIE;
  361. ret &= ~HYM8563_CTL2_TIE;
  362. /* Clear any pending alarm and timer flags */
  363. if (ret & HYM8563_CTL2_AF)
  364. ret &= ~HYM8563_CTL2_AF;
  365. if (ret & HYM8563_CTL2_TF)
  366. ret &= ~HYM8563_CTL2_TF;
  367. ret &= ~HYM8563_CTL2_TI_TP;
  368. return i2c_smbus_write_byte_data(client, HYM8563_CTL2, ret);
  369. }
  370. #ifdef CONFIG_PM_SLEEP
  371. static int hym8563_suspend(struct device *dev)
  372. {
  373. struct i2c_client *client = to_i2c_client(dev);
  374. int ret;
  375. if (device_may_wakeup(dev)) {
  376. ret = enable_irq_wake(client->irq);
  377. if (ret) {
  378. dev_err(dev, "enable_irq_wake failed, %d\n", ret);
  379. return ret;
  380. }
  381. }
  382. return 0;
  383. }
  384. static int hym8563_resume(struct device *dev)
  385. {
  386. struct i2c_client *client = to_i2c_client(dev);
  387. if (device_may_wakeup(dev))
  388. disable_irq_wake(client->irq);
  389. return 0;
  390. }
  391. #endif
  392. static SIMPLE_DEV_PM_OPS(hym8563_pm_ops, hym8563_suspend, hym8563_resume);
  393. static int hym8563_probe(struct i2c_client *client)
  394. {
  395. struct hym8563 *hym8563;
  396. int ret;
  397. hym8563 = devm_kzalloc(&client->dev, sizeof(*hym8563), GFP_KERNEL);
  398. if (!hym8563)
  399. return -ENOMEM;
  400. hym8563->rtc = devm_rtc_allocate_device(&client->dev);
  401. if (IS_ERR(hym8563->rtc))
  402. return PTR_ERR(hym8563->rtc);
  403. hym8563->client = client;
  404. i2c_set_clientdata(client, hym8563);
  405. ret = hym8563_init_device(client);
  406. if (ret) {
  407. dev_err(&client->dev, "could not init device, %d\n", ret);
  408. return ret;
  409. }
  410. if (client->irq > 0) {
  411. ret = devm_request_threaded_irq(&client->dev, client->irq,
  412. NULL, hym8563_irq,
  413. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  414. client->name, hym8563);
  415. if (ret < 0) {
  416. dev_err(&client->dev, "irq %d request failed, %d\n",
  417. client->irq, ret);
  418. return ret;
  419. }
  420. }
  421. if (client->irq > 0 ||
  422. device_property_read_bool(&client->dev, "wakeup-source")) {
  423. device_init_wakeup(&client->dev, true);
  424. }
  425. /* check state of calendar information */
  426. ret = i2c_smbus_read_byte_data(client, HYM8563_SEC);
  427. if (ret < 0)
  428. return ret;
  429. dev_dbg(&client->dev, "rtc information is %s\n",
  430. (ret & HYM8563_SEC_VL) ? "invalid" : "valid");
  431. hym8563->rtc->ops = &hym8563_rtc_ops;
  432. set_bit(RTC_FEATURE_ALARM_RES_MINUTE, hym8563->rtc->features);
  433. clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, hym8563->rtc->features);
  434. #ifdef CONFIG_COMMON_CLK
  435. hym8563_clkout_register_clk(hym8563);
  436. #endif
  437. return devm_rtc_register_device(hym8563->rtc);
  438. }
  439. static const struct i2c_device_id hym8563_id[] = {
  440. { "hym8563", 0 },
  441. {},
  442. };
  443. MODULE_DEVICE_TABLE(i2c, hym8563_id);
  444. static const struct of_device_id hym8563_dt_idtable[] = {
  445. { .compatible = "haoyu,hym8563" },
  446. {},
  447. };
  448. MODULE_DEVICE_TABLE(of, hym8563_dt_idtable);
  449. static struct i2c_driver hym8563_driver = {
  450. .driver = {
  451. .name = "rtc-hym8563",
  452. .pm = &hym8563_pm_ops,
  453. .of_match_table = hym8563_dt_idtable,
  454. },
  455. .probe_new = hym8563_probe,
  456. .id_table = hym8563_id,
  457. };
  458. module_i2c_driver(hym8563_driver);
  459. MODULE_AUTHOR("Heiko Stuebner <[email protected]>");
  460. MODULE_DESCRIPTION("HYM8563 RTC driver");
  461. MODULE_LICENSE("GPL");