rtc-nct3018y.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2022 Nuvoton Technology Corporation
  3. #include <linux/bcd.h>
  4. #include <linux/clk-provider.h>
  5. #include <linux/err.h>
  6. #include <linux/i2c.h>
  7. #include <linux/module.h>
  8. #include <linux/of.h>
  9. #include <linux/rtc.h>
  10. #include <linux/slab.h>
  11. #define NCT3018Y_REG_SC 0x00 /* seconds */
  12. #define NCT3018Y_REG_SCA 0x01 /* alarm */
  13. #define NCT3018Y_REG_MN 0x02
  14. #define NCT3018Y_REG_MNA 0x03 /* alarm */
  15. #define NCT3018Y_REG_HR 0x04
  16. #define NCT3018Y_REG_HRA 0x05 /* alarm */
  17. #define NCT3018Y_REG_DW 0x06
  18. #define NCT3018Y_REG_DM 0x07
  19. #define NCT3018Y_REG_MO 0x08
  20. #define NCT3018Y_REG_YR 0x09
  21. #define NCT3018Y_REG_CTRL 0x0A /* timer control */
  22. #define NCT3018Y_REG_ST 0x0B /* status */
  23. #define NCT3018Y_REG_CLKO 0x0C /* clock out */
  24. #define NCT3018Y_BIT_AF BIT(7)
  25. #define NCT3018Y_BIT_ST BIT(7)
  26. #define NCT3018Y_BIT_DM BIT(6)
  27. #define NCT3018Y_BIT_HF BIT(5)
  28. #define NCT3018Y_BIT_DSM BIT(4)
  29. #define NCT3018Y_BIT_AIE BIT(3)
  30. #define NCT3018Y_BIT_OFIE BIT(2)
  31. #define NCT3018Y_BIT_CIE BIT(1)
  32. #define NCT3018Y_BIT_TWO BIT(0)
  33. #define NCT3018Y_REG_BAT_MASK 0x07
  34. #define NCT3018Y_REG_CLKO_F_MASK 0x03 /* frequenc mask */
  35. #define NCT3018Y_REG_CLKO_CKE 0x80 /* clock out enabled */
  36. struct nct3018y {
  37. struct rtc_device *rtc;
  38. struct i2c_client *client;
  39. #ifdef CONFIG_COMMON_CLK
  40. struct clk_hw clkout_hw;
  41. #endif
  42. };
  43. static int nct3018y_set_alarm_mode(struct i2c_client *client, bool on)
  44. {
  45. int err, flags;
  46. dev_dbg(&client->dev, "%s:on:%d\n", __func__, on);
  47. flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CTRL);
  48. if (flags < 0) {
  49. dev_dbg(&client->dev,
  50. "Failed to read NCT3018Y_REG_CTRL\n");
  51. return flags;
  52. }
  53. if (on)
  54. flags |= NCT3018Y_BIT_AIE;
  55. else
  56. flags &= ~NCT3018Y_BIT_AIE;
  57. flags |= NCT3018Y_BIT_CIE;
  58. err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_CTRL, flags);
  59. if (err < 0) {
  60. dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_CTRL\n");
  61. return err;
  62. }
  63. flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_ST);
  64. if (flags < 0) {
  65. dev_dbg(&client->dev,
  66. "Failed to read NCT3018Y_REG_ST\n");
  67. return flags;
  68. }
  69. flags &= ~(NCT3018Y_BIT_AF);
  70. err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_ST, flags);
  71. if (err < 0) {
  72. dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_ST\n");
  73. return err;
  74. }
  75. return 0;
  76. }
  77. static int nct3018y_get_alarm_mode(struct i2c_client *client, unsigned char *alarm_enable,
  78. unsigned char *alarm_flag)
  79. {
  80. int flags;
  81. if (alarm_enable) {
  82. dev_dbg(&client->dev, "%s:NCT3018Y_REG_CTRL\n", __func__);
  83. flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CTRL);
  84. if (flags < 0)
  85. return flags;
  86. *alarm_enable = flags & NCT3018Y_BIT_AIE;
  87. }
  88. if (alarm_flag) {
  89. dev_dbg(&client->dev, "%s:NCT3018Y_REG_ST\n", __func__);
  90. flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_ST);
  91. if (flags < 0)
  92. return flags;
  93. *alarm_flag = flags & NCT3018Y_BIT_AF;
  94. }
  95. dev_dbg(&client->dev, "%s:alarm_enable:%x alarm_flag:%x\n",
  96. __func__, *alarm_enable, *alarm_flag);
  97. return 0;
  98. }
  99. static irqreturn_t nct3018y_irq(int irq, void *dev_id)
  100. {
  101. struct nct3018y *nct3018y = i2c_get_clientdata(dev_id);
  102. struct i2c_client *client = nct3018y->client;
  103. int err;
  104. unsigned char alarm_flag;
  105. unsigned char alarm_enable;
  106. dev_dbg(&client->dev, "%s:irq:%d\n", __func__, irq);
  107. err = nct3018y_get_alarm_mode(nct3018y->client, &alarm_enable, &alarm_flag);
  108. if (err)
  109. return IRQ_NONE;
  110. if (alarm_flag) {
  111. dev_dbg(&client->dev, "%s:alarm flag:%x\n",
  112. __func__, alarm_flag);
  113. rtc_update_irq(nct3018y->rtc, 1, RTC_IRQF | RTC_AF);
  114. nct3018y_set_alarm_mode(nct3018y->client, 0);
  115. dev_dbg(&client->dev, "%s:IRQ_HANDLED\n", __func__);
  116. return IRQ_HANDLED;
  117. }
  118. return IRQ_NONE;
  119. }
  120. /*
  121. * In the routines that deal directly with the nct3018y hardware, we use
  122. * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
  123. */
  124. static int nct3018y_rtc_read_time(struct device *dev, struct rtc_time *tm)
  125. {
  126. struct i2c_client *client = to_i2c_client(dev);
  127. unsigned char buf[10];
  128. int err;
  129. err = i2c_smbus_read_i2c_block_data(client, NCT3018Y_REG_ST, 1, buf);
  130. if (err < 0)
  131. return err;
  132. if (!buf[0]) {
  133. dev_dbg(&client->dev, " voltage <=1.7, date/time is not reliable.\n");
  134. return -EINVAL;
  135. }
  136. err = i2c_smbus_read_i2c_block_data(client, NCT3018Y_REG_SC, sizeof(buf), buf);
  137. if (err < 0)
  138. return err;
  139. tm->tm_sec = bcd2bin(buf[0] & 0x7F);
  140. tm->tm_min = bcd2bin(buf[2] & 0x7F);
  141. tm->tm_hour = bcd2bin(buf[4] & 0x3F);
  142. tm->tm_wday = buf[6] & 0x07;
  143. tm->tm_mday = bcd2bin(buf[7] & 0x3F);
  144. tm->tm_mon = bcd2bin(buf[8] & 0x1F) - 1;
  145. tm->tm_year = bcd2bin(buf[9]) + 100;
  146. return 0;
  147. }
  148. static int nct3018y_rtc_set_time(struct device *dev, struct rtc_time *tm)
  149. {
  150. struct i2c_client *client = to_i2c_client(dev);
  151. unsigned char buf[4] = {0};
  152. int err;
  153. buf[0] = bin2bcd(tm->tm_sec);
  154. err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_SC, buf[0]);
  155. if (err < 0) {
  156. dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_SC\n");
  157. return err;
  158. }
  159. buf[0] = bin2bcd(tm->tm_min);
  160. err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_MN, buf[0]);
  161. if (err < 0) {
  162. dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_MN\n");
  163. return err;
  164. }
  165. buf[0] = bin2bcd(tm->tm_hour);
  166. err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_HR, buf[0]);
  167. if (err < 0) {
  168. dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_HR\n");
  169. return err;
  170. }
  171. buf[0] = tm->tm_wday & 0x07;
  172. buf[1] = bin2bcd(tm->tm_mday);
  173. buf[2] = bin2bcd(tm->tm_mon + 1);
  174. buf[3] = bin2bcd(tm->tm_year - 100);
  175. err = i2c_smbus_write_i2c_block_data(client, NCT3018Y_REG_DW,
  176. sizeof(buf), buf);
  177. if (err < 0) {
  178. dev_dbg(&client->dev, "Unable to write for day and mon and year\n");
  179. return -EIO;
  180. }
  181. return err;
  182. }
  183. static int nct3018y_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *tm)
  184. {
  185. struct i2c_client *client = to_i2c_client(dev);
  186. unsigned char buf[5];
  187. int err;
  188. err = i2c_smbus_read_i2c_block_data(client, NCT3018Y_REG_SCA,
  189. sizeof(buf), buf);
  190. if (err < 0) {
  191. dev_dbg(&client->dev, "Unable to read date\n");
  192. return -EIO;
  193. }
  194. dev_dbg(&client->dev, "%s: raw data is sec=%02x, min=%02x hr=%02x\n",
  195. __func__, buf[0], buf[2], buf[4]);
  196. tm->time.tm_sec = bcd2bin(buf[0] & 0x7F);
  197. tm->time.tm_min = bcd2bin(buf[2] & 0x7F);
  198. tm->time.tm_hour = bcd2bin(buf[4] & 0x3F);
  199. err = nct3018y_get_alarm_mode(client, &tm->enabled, &tm->pending);
  200. if (err < 0)
  201. return err;
  202. dev_dbg(&client->dev, "%s:s=%d m=%d, hr=%d, enabled=%d, pending=%d\n",
  203. __func__, tm->time.tm_sec, tm->time.tm_min,
  204. tm->time.tm_hour, tm->enabled, tm->pending);
  205. return 0;
  206. }
  207. static int nct3018y_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *tm)
  208. {
  209. struct i2c_client *client = to_i2c_client(dev);
  210. int err;
  211. dev_dbg(dev, "%s, sec=%d, min=%d hour=%d tm->enabled:%d\n",
  212. __func__, tm->time.tm_sec, tm->time.tm_min, tm->time.tm_hour,
  213. tm->enabled);
  214. err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_SCA, bin2bcd(tm->time.tm_sec));
  215. if (err < 0) {
  216. dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_SCA\n");
  217. return err;
  218. }
  219. err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_MNA, bin2bcd(tm->time.tm_min));
  220. if (err < 0) {
  221. dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_MNA\n");
  222. return err;
  223. }
  224. err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_HRA, bin2bcd(tm->time.tm_hour));
  225. if (err < 0) {
  226. dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_HRA\n");
  227. return err;
  228. }
  229. return nct3018y_set_alarm_mode(client, tm->enabled);
  230. }
  231. static int nct3018y_irq_enable(struct device *dev, unsigned int enabled)
  232. {
  233. dev_dbg(dev, "%s: alarm enable=%d\n", __func__, enabled);
  234. return nct3018y_set_alarm_mode(to_i2c_client(dev), enabled);
  235. }
  236. static int nct3018y_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
  237. {
  238. struct i2c_client *client = to_i2c_client(dev);
  239. int status, flags = 0;
  240. switch (cmd) {
  241. case RTC_VL_READ:
  242. status = i2c_smbus_read_byte_data(client, NCT3018Y_REG_ST);
  243. if (status < 0)
  244. return status;
  245. if (!(status & NCT3018Y_REG_BAT_MASK))
  246. flags |= RTC_VL_DATA_INVALID;
  247. return put_user(flags, (unsigned int __user *)arg);
  248. default:
  249. return -ENOIOCTLCMD;
  250. }
  251. }
  252. #ifdef CONFIG_COMMON_CLK
  253. /*
  254. * Handling of the clkout
  255. */
  256. #define clkout_hw_to_nct3018y(_hw) container_of(_hw, struct nct3018y, clkout_hw)
  257. static const int clkout_rates[] = {
  258. 32768,
  259. 1024,
  260. 32,
  261. 1,
  262. };
  263. static unsigned long nct3018y_clkout_recalc_rate(struct clk_hw *hw,
  264. unsigned long parent_rate)
  265. {
  266. struct nct3018y *nct3018y = clkout_hw_to_nct3018y(hw);
  267. struct i2c_client *client = nct3018y->client;
  268. int flags;
  269. flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CLKO);
  270. if (flags < 0)
  271. return 0;
  272. flags &= NCT3018Y_REG_CLKO_F_MASK;
  273. return clkout_rates[flags];
  274. }
  275. static long nct3018y_clkout_round_rate(struct clk_hw *hw, unsigned long rate,
  276. unsigned long *prate)
  277. {
  278. int i;
  279. for (i = 0; i < ARRAY_SIZE(clkout_rates); i++)
  280. if (clkout_rates[i] <= rate)
  281. return clkout_rates[i];
  282. return 0;
  283. }
  284. static int nct3018y_clkout_set_rate(struct clk_hw *hw, unsigned long rate,
  285. unsigned long parent_rate)
  286. {
  287. struct nct3018y *nct3018y = clkout_hw_to_nct3018y(hw);
  288. struct i2c_client *client = nct3018y->client;
  289. int i, flags;
  290. flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CLKO);
  291. if (flags < 0)
  292. return flags;
  293. for (i = 0; i < ARRAY_SIZE(clkout_rates); i++)
  294. if (clkout_rates[i] == rate) {
  295. flags &= ~NCT3018Y_REG_CLKO_F_MASK;
  296. flags |= i;
  297. return i2c_smbus_write_byte_data(client, NCT3018Y_REG_CLKO, flags);
  298. }
  299. return -EINVAL;
  300. }
  301. static int nct3018y_clkout_control(struct clk_hw *hw, bool enable)
  302. {
  303. struct nct3018y *nct3018y = clkout_hw_to_nct3018y(hw);
  304. struct i2c_client *client = nct3018y->client;
  305. int flags;
  306. flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CLKO);
  307. if (flags < 0)
  308. return flags;
  309. if (enable)
  310. flags |= NCT3018Y_REG_CLKO_CKE;
  311. else
  312. flags &= ~NCT3018Y_REG_CLKO_CKE;
  313. return i2c_smbus_write_byte_data(client, NCT3018Y_REG_CLKO, flags);
  314. }
  315. static int nct3018y_clkout_prepare(struct clk_hw *hw)
  316. {
  317. return nct3018y_clkout_control(hw, 1);
  318. }
  319. static void nct3018y_clkout_unprepare(struct clk_hw *hw)
  320. {
  321. nct3018y_clkout_control(hw, 0);
  322. }
  323. static int nct3018y_clkout_is_prepared(struct clk_hw *hw)
  324. {
  325. struct nct3018y *nct3018y = clkout_hw_to_nct3018y(hw);
  326. struct i2c_client *client = nct3018y->client;
  327. int flags;
  328. flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CLKO);
  329. if (flags < 0)
  330. return flags;
  331. return flags & NCT3018Y_REG_CLKO_CKE;
  332. }
  333. static const struct clk_ops nct3018y_clkout_ops = {
  334. .prepare = nct3018y_clkout_prepare,
  335. .unprepare = nct3018y_clkout_unprepare,
  336. .is_prepared = nct3018y_clkout_is_prepared,
  337. .recalc_rate = nct3018y_clkout_recalc_rate,
  338. .round_rate = nct3018y_clkout_round_rate,
  339. .set_rate = nct3018y_clkout_set_rate,
  340. };
  341. static struct clk *nct3018y_clkout_register_clk(struct nct3018y *nct3018y)
  342. {
  343. struct i2c_client *client = nct3018y->client;
  344. struct device_node *node = client->dev.of_node;
  345. struct clk *clk;
  346. struct clk_init_data init;
  347. init.name = "nct3018y-clkout";
  348. init.ops = &nct3018y_clkout_ops;
  349. init.flags = 0;
  350. init.parent_names = NULL;
  351. init.num_parents = 0;
  352. nct3018y->clkout_hw.init = &init;
  353. /* optional override of the clockname */
  354. of_property_read_string(node, "clock-output-names", &init.name);
  355. /* register the clock */
  356. clk = devm_clk_register(&client->dev, &nct3018y->clkout_hw);
  357. if (!IS_ERR(clk))
  358. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  359. return clk;
  360. }
  361. #endif
  362. static const struct rtc_class_ops nct3018y_rtc_ops = {
  363. .read_time = nct3018y_rtc_read_time,
  364. .set_time = nct3018y_rtc_set_time,
  365. .read_alarm = nct3018y_rtc_read_alarm,
  366. .set_alarm = nct3018y_rtc_set_alarm,
  367. .alarm_irq_enable = nct3018y_irq_enable,
  368. .ioctl = nct3018y_ioctl,
  369. };
  370. static int nct3018y_probe(struct i2c_client *client,
  371. const struct i2c_device_id *id)
  372. {
  373. struct nct3018y *nct3018y;
  374. int err, flags;
  375. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
  376. I2C_FUNC_SMBUS_BYTE |
  377. I2C_FUNC_SMBUS_BLOCK_DATA))
  378. return -ENODEV;
  379. nct3018y = devm_kzalloc(&client->dev, sizeof(struct nct3018y),
  380. GFP_KERNEL);
  381. if (!nct3018y)
  382. return -ENOMEM;
  383. i2c_set_clientdata(client, nct3018y);
  384. nct3018y->client = client;
  385. device_set_wakeup_capable(&client->dev, 1);
  386. flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CTRL);
  387. if (flags < 0) {
  388. dev_dbg(&client->dev, "%s: read error\n", __func__);
  389. return flags;
  390. } else if (flags & NCT3018Y_BIT_TWO) {
  391. dev_dbg(&client->dev, "%s: NCT3018Y_BIT_TWO is set\n", __func__);
  392. }
  393. flags = NCT3018Y_BIT_TWO;
  394. err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_CTRL, flags);
  395. if (err < 0) {
  396. dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_CTRL\n");
  397. return err;
  398. }
  399. flags = 0;
  400. err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_ST, flags);
  401. if (err < 0) {
  402. dev_dbg(&client->dev, "%s: write error\n", __func__);
  403. return err;
  404. }
  405. nct3018y->rtc = devm_rtc_allocate_device(&client->dev);
  406. if (IS_ERR(nct3018y->rtc))
  407. return PTR_ERR(nct3018y->rtc);
  408. nct3018y->rtc->ops = &nct3018y_rtc_ops;
  409. nct3018y->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
  410. nct3018y->rtc->range_max = RTC_TIMESTAMP_END_2099;
  411. if (client->irq > 0) {
  412. err = devm_request_threaded_irq(&client->dev, client->irq,
  413. NULL, nct3018y_irq,
  414. IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
  415. "nct3018y", client);
  416. if (err) {
  417. dev_dbg(&client->dev, "unable to request IRQ %d\n", client->irq);
  418. return err;
  419. }
  420. } else {
  421. clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, nct3018y->rtc->features);
  422. clear_bit(RTC_FEATURE_ALARM, nct3018y->rtc->features);
  423. }
  424. #ifdef CONFIG_COMMON_CLK
  425. /* register clk in common clk framework */
  426. nct3018y_clkout_register_clk(nct3018y);
  427. #endif
  428. return devm_rtc_register_device(nct3018y->rtc);
  429. }
  430. static const struct i2c_device_id nct3018y_id[] = {
  431. { "nct3018y", 0 },
  432. { }
  433. };
  434. MODULE_DEVICE_TABLE(i2c, nct3018y_id);
  435. static const struct of_device_id nct3018y_of_match[] = {
  436. { .compatible = "nuvoton,nct3018y" },
  437. {}
  438. };
  439. MODULE_DEVICE_TABLE(of, nct3018y_of_match);
  440. static struct i2c_driver nct3018y_driver = {
  441. .driver = {
  442. .name = "rtc-nct3018y",
  443. .of_match_table = of_match_ptr(nct3018y_of_match),
  444. },
  445. .probe = nct3018y_probe,
  446. .id_table = nct3018y_id,
  447. };
  448. module_i2c_driver(nct3018y_driver);
  449. MODULE_AUTHOR("Medad CChien <[email protected]>");
  450. MODULE_AUTHOR("Mia Lin <[email protected]>");
  451. MODULE_DESCRIPTION("Nuvoton NCT3018Y RTC driver");
  452. MODULE_LICENSE("GPL");