rtc-hym8563.c 14 KB

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