rtc-pcf8563.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /*
  2. * An I2C driver for the Philips PCF8563 RTC
  3. * Copyright 2005-06 Tower Technologies
  4. *
  5. * Author: Alessandro Zummo <a.zummo@towertech.it>
  6. * Maintainers: http://www.nslu2-linux.org/
  7. *
  8. * based on the other drivers in this same directory.
  9. *
  10. * http://www.semiconductors.philips.com/acrobat/datasheets/PCF8563-04.pdf
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. */
  16. #include <linux/clk-provider.h>
  17. #include <linux/i2c.h>
  18. #include <linux/bcd.h>
  19. #include <linux/rtc.h>
  20. #include <linux/slab.h>
  21. #include <linux/module.h>
  22. #include <linux/of.h>
  23. #include <linux/err.h>
  24. #define DRV_VERSION "0.4.4"
  25. #define PCF8563_REG_ST1 0x00 /* status */
  26. #define PCF8563_REG_ST2 0x01
  27. #define PCF8563_BIT_AIE (1 << 1)
  28. #define PCF8563_BIT_AF (1 << 3)
  29. #define PCF8563_BITS_ST2_N (7 << 5)
  30. #define PCF8563_REG_SC 0x02 /* datetime */
  31. #define PCF8563_REG_MN 0x03
  32. #define PCF8563_REG_HR 0x04
  33. #define PCF8563_REG_DM 0x05
  34. #define PCF8563_REG_DW 0x06
  35. #define PCF8563_REG_MO 0x07
  36. #define PCF8563_REG_YR 0x08
  37. #define PCF8563_REG_AMN 0x09 /* alarm */
  38. #define PCF8563_REG_CLKO 0x0D /* clock out */
  39. #define PCF8563_REG_CLKO_FE 0x80 /* clock out enabled */
  40. #define PCF8563_REG_CLKO_F_MASK 0x03 /* frequenc mask */
  41. #define PCF8563_REG_CLKO_F_32768HZ 0x00
  42. #define PCF8563_REG_CLKO_F_1024HZ 0x01
  43. #define PCF8563_REG_CLKO_F_32HZ 0x02
  44. #define PCF8563_REG_CLKO_F_1HZ 0x03
  45. #define PCF8563_REG_TMRC 0x0E /* timer control */
  46. #define PCF8563_TMRC_ENABLE BIT(7)
  47. #define PCF8563_TMRC_4096 0
  48. #define PCF8563_TMRC_64 1
  49. #define PCF8563_TMRC_1 2
  50. #define PCF8563_TMRC_1_60 3
  51. #define PCF8563_TMRC_MASK 3
  52. #define PCF8563_REG_TMR 0x0F /* timer */
  53. #define PCF8563_SC_LV 0x80 /* low voltage */
  54. #define PCF8563_MO_C 0x80 /* century */
  55. static struct i2c_driver pcf8563_driver;
  56. struct pcf8563 {
  57. struct rtc_device *rtc;
  58. /*
  59. * The meaning of MO_C bit varies by the chip type.
  60. * From PCF8563 datasheet: this bit is toggled when the years
  61. * register overflows from 99 to 00
  62. * 0 indicates the century is 20xx
  63. * 1 indicates the century is 19xx
  64. * From RTC8564 datasheet: this bit indicates change of
  65. * century. When the year digit data overflows from 99 to 00,
  66. * this bit is set. By presetting it to 0 while still in the
  67. * 20th century, it will be set in year 2000, ...
  68. * There seems no reliable way to know how the system use this
  69. * bit. So let's do it heuristically, assuming we are live in
  70. * 1970...2069.
  71. */
  72. int c_polarity; /* 0: MO_C=1 means 19xx, otherwise MO_C=1 means 20xx */
  73. int voltage_low; /* incicates if a low_voltage was detected */
  74. struct i2c_client *client;
  75. #ifdef CONFIG_COMMON_CLK
  76. struct clk_hw clkout_hw;
  77. #endif
  78. };
  79. static int pcf8563_read_block_data(struct i2c_client *client, unsigned char reg,
  80. unsigned char length, unsigned char *buf)
  81. {
  82. struct i2c_msg msgs[] = {
  83. {/* setup read ptr */
  84. .addr = client->addr,
  85. .len = 1,
  86. .buf = &reg,
  87. },
  88. {
  89. .addr = client->addr,
  90. .flags = I2C_M_RD,
  91. .len = length,
  92. .buf = buf
  93. },
  94. };
  95. if ((i2c_transfer(client->adapter, msgs, 2)) != 2) {
  96. dev_err(&client->dev, "%s: read error\n", __func__);
  97. return -EIO;
  98. }
  99. return 0;
  100. }
  101. static int pcf8563_write_block_data(struct i2c_client *client,
  102. unsigned char reg, unsigned char length,
  103. unsigned char *buf)
  104. {
  105. int i, err;
  106. for (i = 0; i < length; i++) {
  107. unsigned char data[2] = { reg + i, buf[i] };
  108. err = i2c_master_send(client, data, sizeof(data));
  109. if (err != sizeof(data)) {
  110. dev_err(&client->dev,
  111. "%s: err=%d addr=%02x, data=%02x\n",
  112. __func__, err, data[0], data[1]);
  113. return -EIO;
  114. }
  115. }
  116. return 0;
  117. }
  118. static int pcf8563_set_alarm_mode(struct i2c_client *client, bool on)
  119. {
  120. unsigned char buf;
  121. int err;
  122. err = pcf8563_read_block_data(client, PCF8563_REG_ST2, 1, &buf);
  123. if (err < 0)
  124. return err;
  125. if (on)
  126. buf |= PCF8563_BIT_AIE;
  127. else
  128. buf &= ~PCF8563_BIT_AIE;
  129. buf &= ~(PCF8563_BIT_AF | PCF8563_BITS_ST2_N);
  130. err = pcf8563_write_block_data(client, PCF8563_REG_ST2, 1, &buf);
  131. if (err < 0) {
  132. dev_err(&client->dev, "%s: write error\n", __func__);
  133. return -EIO;
  134. }
  135. return 0;
  136. }
  137. static int pcf8563_get_alarm_mode(struct i2c_client *client, unsigned char *en,
  138. unsigned char *pen)
  139. {
  140. unsigned char buf;
  141. int err;
  142. err = pcf8563_read_block_data(client, PCF8563_REG_ST2, 1, &buf);
  143. if (err)
  144. return err;
  145. if (en)
  146. *en = !!(buf & PCF8563_BIT_AIE);
  147. if (pen)
  148. *pen = !!(buf & PCF8563_BIT_AF);
  149. return 0;
  150. }
  151. static irqreturn_t pcf8563_irq(int irq, void *dev_id)
  152. {
  153. struct pcf8563 *pcf8563 = i2c_get_clientdata(dev_id);
  154. int err;
  155. char pending;
  156. err = pcf8563_get_alarm_mode(pcf8563->client, NULL, &pending);
  157. if (err)
  158. return IRQ_NONE;
  159. if (pending) {
  160. rtc_update_irq(pcf8563->rtc, 1, RTC_IRQF | RTC_AF);
  161. pcf8563_set_alarm_mode(pcf8563->client, 1);
  162. return IRQ_HANDLED;
  163. }
  164. return IRQ_NONE;
  165. }
  166. /*
  167. * In the routines that deal directly with the pcf8563 hardware, we use
  168. * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
  169. */
  170. static int pcf8563_get_datetime(struct i2c_client *client, struct rtc_time *tm)
  171. {
  172. struct pcf8563 *pcf8563 = i2c_get_clientdata(client);
  173. unsigned char buf[9];
  174. int err;
  175. err = pcf8563_read_block_data(client, PCF8563_REG_ST1, 9, buf);
  176. if (err)
  177. return err;
  178. if (buf[PCF8563_REG_SC] & PCF8563_SC_LV) {
  179. pcf8563->voltage_low = 1;
  180. dev_err(&client->dev,
  181. "low voltage detected, date/time is not reliable.\n");
  182. return -EINVAL;
  183. }
  184. dev_dbg(&client->dev,
  185. "%s: raw data is st1=%02x, st2=%02x, sec=%02x, min=%02x, hr=%02x, "
  186. "mday=%02x, wday=%02x, mon=%02x, year=%02x\n",
  187. __func__,
  188. buf[0], buf[1], buf[2], buf[3],
  189. buf[4], buf[5], buf[6], buf[7],
  190. buf[8]);
  191. tm->tm_sec = bcd2bin(buf[PCF8563_REG_SC] & 0x7F);
  192. tm->tm_min = bcd2bin(buf[PCF8563_REG_MN] & 0x7F);
  193. tm->tm_hour = bcd2bin(buf[PCF8563_REG_HR] & 0x3F); /* rtc hr 0-23 */
  194. tm->tm_mday = bcd2bin(buf[PCF8563_REG_DM] & 0x3F);
  195. tm->tm_wday = buf[PCF8563_REG_DW] & 0x07;
  196. tm->tm_mon = bcd2bin(buf[PCF8563_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
  197. tm->tm_year = bcd2bin(buf[PCF8563_REG_YR]);
  198. if (tm->tm_year < 70)
  199. tm->tm_year += 100; /* assume we are in 1970...2069 */
  200. /* detect the polarity heuristically. see note above. */
  201. pcf8563->c_polarity = (buf[PCF8563_REG_MO] & PCF8563_MO_C) ?
  202. (tm->tm_year >= 100) : (tm->tm_year < 100);
  203. dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
  204. "mday=%d, mon=%d, year=%d, wday=%d\n",
  205. __func__,
  206. tm->tm_sec, tm->tm_min, tm->tm_hour,
  207. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  208. return 0;
  209. }
  210. static int pcf8563_set_datetime(struct i2c_client *client, struct rtc_time *tm)
  211. {
  212. struct pcf8563 *pcf8563 = i2c_get_clientdata(client);
  213. unsigned char buf[9];
  214. dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
  215. "mday=%d, mon=%d, year=%d, wday=%d\n",
  216. __func__,
  217. tm->tm_sec, tm->tm_min, tm->tm_hour,
  218. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  219. /* hours, minutes and seconds */
  220. buf[PCF8563_REG_SC] = bin2bcd(tm->tm_sec);
  221. buf[PCF8563_REG_MN] = bin2bcd(tm->tm_min);
  222. buf[PCF8563_REG_HR] = bin2bcd(tm->tm_hour);
  223. buf[PCF8563_REG_DM] = bin2bcd(tm->tm_mday);
  224. /* month, 1 - 12 */
  225. buf[PCF8563_REG_MO] = bin2bcd(tm->tm_mon + 1);
  226. /* year and century */
  227. buf[PCF8563_REG_YR] = bin2bcd(tm->tm_year % 100);
  228. if (pcf8563->c_polarity ? (tm->tm_year >= 100) : (tm->tm_year < 100))
  229. buf[PCF8563_REG_MO] |= PCF8563_MO_C;
  230. buf[PCF8563_REG_DW] = tm->tm_wday & 0x07;
  231. return pcf8563_write_block_data(client, PCF8563_REG_SC,
  232. 9 - PCF8563_REG_SC, buf + PCF8563_REG_SC);
  233. }
  234. #ifdef CONFIG_RTC_INTF_DEV
  235. static int pcf8563_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
  236. {
  237. struct pcf8563 *pcf8563 = i2c_get_clientdata(to_i2c_client(dev));
  238. struct rtc_time tm;
  239. switch (cmd) {
  240. case RTC_VL_READ:
  241. if (pcf8563->voltage_low)
  242. dev_info(dev, "low voltage detected, date/time is not reliable.\n");
  243. if (copy_to_user((void __user *)arg, &pcf8563->voltage_low,
  244. sizeof(int)))
  245. return -EFAULT;
  246. return 0;
  247. case RTC_VL_CLR:
  248. /*
  249. * Clear the VL bit in the seconds register in case
  250. * the time has not been set already (which would
  251. * have cleared it). This does not really matter
  252. * because of the cached voltage_low value but do it
  253. * anyway for consistency.
  254. */
  255. if (pcf8563_get_datetime(to_i2c_client(dev), &tm))
  256. pcf8563_set_datetime(to_i2c_client(dev), &tm);
  257. /* Clear the cached value. */
  258. pcf8563->voltage_low = 0;
  259. return 0;
  260. default:
  261. return -ENOIOCTLCMD;
  262. }
  263. }
  264. #else
  265. #define pcf8563_rtc_ioctl NULL
  266. #endif
  267. static int pcf8563_rtc_read_time(struct device *dev, struct rtc_time *tm)
  268. {
  269. return pcf8563_get_datetime(to_i2c_client(dev), tm);
  270. }
  271. static int pcf8563_rtc_set_time(struct device *dev, struct rtc_time *tm)
  272. {
  273. return pcf8563_set_datetime(to_i2c_client(dev), tm);
  274. }
  275. static int pcf8563_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *tm)
  276. {
  277. struct i2c_client *client = to_i2c_client(dev);
  278. unsigned char buf[4];
  279. int err;
  280. err = pcf8563_read_block_data(client, PCF8563_REG_AMN, 4, buf);
  281. if (err)
  282. return err;
  283. dev_dbg(&client->dev,
  284. "%s: raw data is min=%02x, hr=%02x, mday=%02x, wday=%02x\n",
  285. __func__, buf[0], buf[1], buf[2], buf[3]);
  286. tm->time.tm_min = bcd2bin(buf[0] & 0x7F);
  287. tm->time.tm_hour = bcd2bin(buf[1] & 0x3F);
  288. tm->time.tm_mday = bcd2bin(buf[2] & 0x3F);
  289. tm->time.tm_wday = bcd2bin(buf[3] & 0x7);
  290. tm->time.tm_mon = -1;
  291. tm->time.tm_year = -1;
  292. tm->time.tm_yday = -1;
  293. tm->time.tm_isdst = -1;
  294. err = pcf8563_get_alarm_mode(client, &tm->enabled, &tm->pending);
  295. if (err < 0)
  296. return err;
  297. dev_dbg(&client->dev, "%s: tm is mins=%d, hours=%d, mday=%d, wday=%d,"
  298. " enabled=%d, pending=%d\n", __func__, tm->time.tm_min,
  299. tm->time.tm_hour, tm->time.tm_mday, tm->time.tm_wday,
  300. tm->enabled, tm->pending);
  301. return 0;
  302. }
  303. static int pcf8563_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *tm)
  304. {
  305. struct i2c_client *client = to_i2c_client(dev);
  306. unsigned char buf[4];
  307. int err;
  308. /* The alarm has no seconds, round up to nearest minute */
  309. if (tm->time.tm_sec) {
  310. time64_t alarm_time = rtc_tm_to_time64(&tm->time);
  311. alarm_time += 60 - tm->time.tm_sec;
  312. rtc_time64_to_tm(alarm_time, &tm->time);
  313. }
  314. dev_dbg(dev, "%s, min=%d hour=%d wday=%d mday=%d "
  315. "enabled=%d pending=%d\n", __func__,
  316. tm->time.tm_min, tm->time.tm_hour, tm->time.tm_wday,
  317. tm->time.tm_mday, tm->enabled, tm->pending);
  318. buf[0] = bin2bcd(tm->time.tm_min);
  319. buf[1] = bin2bcd(tm->time.tm_hour);
  320. buf[2] = bin2bcd(tm->time.tm_mday);
  321. buf[3] = tm->time.tm_wday & 0x07;
  322. err = pcf8563_write_block_data(client, PCF8563_REG_AMN, 4, buf);
  323. if (err)
  324. return err;
  325. return pcf8563_set_alarm_mode(client, 1);
  326. }
  327. static int pcf8563_irq_enable(struct device *dev, unsigned int enabled)
  328. {
  329. dev_dbg(dev, "%s: en=%d\n", __func__, enabled);
  330. return pcf8563_set_alarm_mode(to_i2c_client(dev), !!enabled);
  331. }
  332. #ifdef CONFIG_COMMON_CLK
  333. /*
  334. * Handling of the clkout
  335. */
  336. #define clkout_hw_to_pcf8563(_hw) container_of(_hw, struct pcf8563, clkout_hw)
  337. static int clkout_rates[] = {
  338. 32768,
  339. 1024,
  340. 32,
  341. 1,
  342. };
  343. static unsigned long pcf8563_clkout_recalc_rate(struct clk_hw *hw,
  344. unsigned long parent_rate)
  345. {
  346. struct pcf8563 *pcf8563 = clkout_hw_to_pcf8563(hw);
  347. struct i2c_client *client = pcf8563->client;
  348. unsigned char buf;
  349. int ret = pcf8563_read_block_data(client, PCF8563_REG_CLKO, 1, &buf);
  350. if (ret < 0)
  351. return 0;
  352. buf &= PCF8563_REG_CLKO_F_MASK;
  353. return clkout_rates[buf];
  354. }
  355. static long pcf8563_clkout_round_rate(struct clk_hw *hw, unsigned long rate,
  356. unsigned long *prate)
  357. {
  358. int i;
  359. for (i = 0; i < ARRAY_SIZE(clkout_rates); i++)
  360. if (clkout_rates[i] <= rate)
  361. return clkout_rates[i];
  362. return 0;
  363. }
  364. static int pcf8563_clkout_set_rate(struct clk_hw *hw, unsigned long rate,
  365. unsigned long parent_rate)
  366. {
  367. struct pcf8563 *pcf8563 = clkout_hw_to_pcf8563(hw);
  368. struct i2c_client *client = pcf8563->client;
  369. unsigned char buf;
  370. int ret = pcf8563_read_block_data(client, PCF8563_REG_CLKO, 1, &buf);
  371. int i;
  372. if (ret < 0)
  373. return ret;
  374. for (i = 0; i < ARRAY_SIZE(clkout_rates); i++)
  375. if (clkout_rates[i] == rate) {
  376. buf &= ~PCF8563_REG_CLKO_F_MASK;
  377. buf |= i;
  378. ret = pcf8563_write_block_data(client,
  379. PCF8563_REG_CLKO, 1,
  380. &buf);
  381. return ret;
  382. }
  383. return -EINVAL;
  384. }
  385. static int pcf8563_clkout_control(struct clk_hw *hw, bool enable)
  386. {
  387. struct pcf8563 *pcf8563 = clkout_hw_to_pcf8563(hw);
  388. struct i2c_client *client = pcf8563->client;
  389. unsigned char buf;
  390. int ret = pcf8563_read_block_data(client, PCF8563_REG_CLKO, 1, &buf);
  391. if (ret < 0)
  392. return ret;
  393. if (enable)
  394. buf |= PCF8563_REG_CLKO_FE;
  395. else
  396. buf &= ~PCF8563_REG_CLKO_FE;
  397. ret = pcf8563_write_block_data(client, PCF8563_REG_CLKO, 1, &buf);
  398. return ret;
  399. }
  400. static int pcf8563_clkout_prepare(struct clk_hw *hw)
  401. {
  402. return pcf8563_clkout_control(hw, 1);
  403. }
  404. static void pcf8563_clkout_unprepare(struct clk_hw *hw)
  405. {
  406. pcf8563_clkout_control(hw, 0);
  407. }
  408. static int pcf8563_clkout_is_prepared(struct clk_hw *hw)
  409. {
  410. struct pcf8563 *pcf8563 = clkout_hw_to_pcf8563(hw);
  411. struct i2c_client *client = pcf8563->client;
  412. unsigned char buf;
  413. int ret = pcf8563_read_block_data(client, PCF8563_REG_CLKO, 1, &buf);
  414. if (ret < 0)
  415. return ret;
  416. return !!(buf & PCF8563_REG_CLKO_FE);
  417. }
  418. static const struct clk_ops pcf8563_clkout_ops = {
  419. .prepare = pcf8563_clkout_prepare,
  420. .unprepare = pcf8563_clkout_unprepare,
  421. .is_prepared = pcf8563_clkout_is_prepared,
  422. .recalc_rate = pcf8563_clkout_recalc_rate,
  423. .round_rate = pcf8563_clkout_round_rate,
  424. .set_rate = pcf8563_clkout_set_rate,
  425. };
  426. static struct clk *pcf8563_clkout_register_clk(struct pcf8563 *pcf8563)
  427. {
  428. struct i2c_client *client = pcf8563->client;
  429. struct device_node *node = client->dev.of_node;
  430. struct clk *clk;
  431. struct clk_init_data init;
  432. int ret;
  433. unsigned char buf;
  434. /* disable the clkout output */
  435. buf = 0;
  436. ret = pcf8563_write_block_data(client, PCF8563_REG_CLKO, 1, &buf);
  437. if (ret < 0)
  438. return ERR_PTR(ret);
  439. init.name = "pcf8563-clkout";
  440. init.ops = &pcf8563_clkout_ops;
  441. init.flags = CLK_IS_ROOT;
  442. init.parent_names = NULL;
  443. init.num_parents = 0;
  444. pcf8563->clkout_hw.init = &init;
  445. /* optional override of the clockname */
  446. of_property_read_string(node, "clock-output-names", &init.name);
  447. /* register the clock */
  448. clk = devm_clk_register(&client->dev, &pcf8563->clkout_hw);
  449. if (!IS_ERR(clk))
  450. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  451. return clk;
  452. }
  453. #endif
  454. static const struct rtc_class_ops pcf8563_rtc_ops = {
  455. .ioctl = pcf8563_rtc_ioctl,
  456. .read_time = pcf8563_rtc_read_time,
  457. .set_time = pcf8563_rtc_set_time,
  458. .read_alarm = pcf8563_rtc_read_alarm,
  459. .set_alarm = pcf8563_rtc_set_alarm,
  460. .alarm_irq_enable = pcf8563_irq_enable,
  461. };
  462. static int pcf8563_probe(struct i2c_client *client,
  463. const struct i2c_device_id *id)
  464. {
  465. struct pcf8563 *pcf8563;
  466. int err;
  467. unsigned char buf;
  468. unsigned char alm_pending;
  469. dev_dbg(&client->dev, "%s\n", __func__);
  470. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  471. return -ENODEV;
  472. pcf8563 = devm_kzalloc(&client->dev, sizeof(struct pcf8563),
  473. GFP_KERNEL);
  474. if (!pcf8563)
  475. return -ENOMEM;
  476. dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n");
  477. i2c_set_clientdata(client, pcf8563);
  478. pcf8563->client = client;
  479. device_set_wakeup_capable(&client->dev, 1);
  480. /* Set timer to lowest frequency to save power (ref Haoyu datasheet) */
  481. buf = PCF8563_TMRC_1_60;
  482. err = pcf8563_write_block_data(client, PCF8563_REG_TMRC, 1, &buf);
  483. if (err < 0) {
  484. dev_err(&client->dev, "%s: write error\n", __func__);
  485. return err;
  486. }
  487. err = pcf8563_get_alarm_mode(client, NULL, &alm_pending);
  488. if (err) {
  489. dev_err(&client->dev, "%s: read error\n", __func__);
  490. return err;
  491. }
  492. if (alm_pending)
  493. pcf8563_set_alarm_mode(client, 0);
  494. pcf8563->rtc = devm_rtc_device_register(&client->dev,
  495. pcf8563_driver.driver.name,
  496. &pcf8563_rtc_ops, THIS_MODULE);
  497. if (IS_ERR(pcf8563->rtc))
  498. return PTR_ERR(pcf8563->rtc);
  499. if (client->irq > 0) {
  500. err = devm_request_threaded_irq(&client->dev, client->irq,
  501. NULL, pcf8563_irq,
  502. IRQF_SHARED|IRQF_ONESHOT|IRQF_TRIGGER_FALLING,
  503. pcf8563->rtc->name, client);
  504. if (err) {
  505. dev_err(&client->dev, "unable to request IRQ %d\n",
  506. client->irq);
  507. return err;
  508. }
  509. }
  510. #ifdef CONFIG_COMMON_CLK
  511. /* register clk in common clk framework */
  512. pcf8563_clkout_register_clk(pcf8563);
  513. #endif
  514. /* the pcf8563 alarm only supports a minute accuracy */
  515. pcf8563->rtc->uie_unsupported = 1;
  516. return 0;
  517. }
  518. static const struct i2c_device_id pcf8563_id[] = {
  519. { "pcf8563", 0 },
  520. { "rtc8564", 0 },
  521. { }
  522. };
  523. MODULE_DEVICE_TABLE(i2c, pcf8563_id);
  524. #ifdef CONFIG_OF
  525. static const struct of_device_id pcf8563_of_match[] = {
  526. { .compatible = "nxp,pcf8563" },
  527. {}
  528. };
  529. MODULE_DEVICE_TABLE(of, pcf8563_of_match);
  530. #endif
  531. static struct i2c_driver pcf8563_driver = {
  532. .driver = {
  533. .name = "rtc-pcf8563",
  534. .of_match_table = of_match_ptr(pcf8563_of_match),
  535. },
  536. .probe = pcf8563_probe,
  537. .id_table = pcf8563_id,
  538. };
  539. module_i2c_driver(pcf8563_driver);
  540. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  541. MODULE_DESCRIPTION("Philips PCF8563/Epson RTC8564 RTC driver");
  542. MODULE_LICENSE("GPL");
  543. MODULE_VERSION(DRV_VERSION);