rtc-pcf8563.c 17 KB

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