rtc-rx8025.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /*
  2. * Driver for Epson's RTC module RX-8025 SA/NB
  3. *
  4. * Copyright (C) 2009 Wolfgang Grandegger <wg@grandegger.com>
  5. *
  6. * Copyright (C) 2005 by Digi International Inc.
  7. * All rights reserved.
  8. *
  9. * Modified by fengjh at rising.com.cn
  10. * <lm-sensors@lm-sensors.org>
  11. * 2006.11
  12. *
  13. * Code cleanup by Sergei Poselenov, <sposelenov@emcraft.com>
  14. * Converted to new style by Wolfgang Grandegger <wg@grandegger.com>
  15. * Alarm and periodic interrupt added by Dmitry Rakhchev <rda@emcraft.com>
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * version 2 as published by the Free Software Foundation.
  20. */
  21. #include <linux/bcd.h>
  22. #include <linux/bitops.h>
  23. #include <linux/i2c.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/rtc.h>
  27. /* Register definitions */
  28. #define RX8025_REG_SEC 0x00
  29. #define RX8025_REG_MIN 0x01
  30. #define RX8025_REG_HOUR 0x02
  31. #define RX8025_REG_WDAY 0x03
  32. #define RX8025_REG_MDAY 0x04
  33. #define RX8025_REG_MONTH 0x05
  34. #define RX8025_REG_YEAR 0x06
  35. #define RX8025_REG_DIGOFF 0x07
  36. #define RX8025_REG_ALWMIN 0x08
  37. #define RX8025_REG_ALWHOUR 0x09
  38. #define RX8025_REG_ALWWDAY 0x0a
  39. #define RX8025_REG_ALDMIN 0x0b
  40. #define RX8025_REG_ALDHOUR 0x0c
  41. /* 0x0d is reserved */
  42. #define RX8025_REG_CTRL1 0x0e
  43. #define RX8025_REG_CTRL2 0x0f
  44. #define RX8025_BIT_CTRL1_CT (7 << 0)
  45. /* 1 Hz periodic level irq */
  46. #define RX8025_BIT_CTRL1_CT_1HZ 4
  47. #define RX8025_BIT_CTRL1_TEST BIT(3)
  48. #define RX8025_BIT_CTRL1_1224 BIT(5)
  49. #define RX8025_BIT_CTRL1_DALE BIT(6)
  50. #define RX8025_BIT_CTRL1_WALE BIT(7)
  51. #define RX8025_BIT_CTRL2_DAFG BIT(0)
  52. #define RX8025_BIT_CTRL2_WAFG BIT(1)
  53. #define RX8025_BIT_CTRL2_CTFG BIT(2)
  54. #define RX8025_BIT_CTRL2_PON BIT(4)
  55. #define RX8025_BIT_CTRL2_XST BIT(5)
  56. #define RX8025_BIT_CTRL2_VDET BIT(6)
  57. /* Clock precision adjustment */
  58. #define RX8025_ADJ_RESOLUTION 3050 /* in ppb */
  59. #define RX8025_ADJ_DATA_MAX 62
  60. #define RX8025_ADJ_DATA_MIN -62
  61. static const struct i2c_device_id rx8025_id[] = {
  62. { "rx8025", 0 },
  63. { }
  64. };
  65. MODULE_DEVICE_TABLE(i2c, rx8025_id);
  66. struct rx8025_data {
  67. struct i2c_client *client;
  68. struct rtc_device *rtc;
  69. u8 ctrl1;
  70. };
  71. static s32 rx8025_read_reg(const struct i2c_client *client, u8 number)
  72. {
  73. return i2c_smbus_read_byte_data(client, number << 4);
  74. }
  75. static int rx8025_read_regs(const struct i2c_client *client,
  76. u8 number, u8 length, u8 *values)
  77. {
  78. int ret = i2c_smbus_read_i2c_block_data(client, number << 4, length,
  79. values);
  80. if (ret != length)
  81. return ret < 0 ? ret : -EIO;
  82. return 0;
  83. }
  84. static s32 rx8025_write_reg(const struct i2c_client *client, u8 number,
  85. u8 value)
  86. {
  87. return i2c_smbus_write_byte_data(client, number << 4, value);
  88. }
  89. static s32 rx8025_write_regs(const struct i2c_client *client,
  90. u8 number, u8 length, const u8 *values)
  91. {
  92. return i2c_smbus_write_i2c_block_data(client, number << 4,
  93. length, values);
  94. }
  95. static int rx8025_check_validity(struct device *dev)
  96. {
  97. struct rx8025_data *rx8025 = dev_get_drvdata(dev);
  98. int ctrl2;
  99. ctrl2 = rx8025_read_reg(rx8025->client, RX8025_REG_CTRL2);
  100. if (ctrl2 < 0)
  101. return ctrl2;
  102. if (ctrl2 & RX8025_BIT_CTRL2_VDET)
  103. dev_warn(dev, "power voltage drop detected\n");
  104. if (ctrl2 & RX8025_BIT_CTRL2_PON) {
  105. dev_warn(dev, "power-on reset detected, date is invalid\n");
  106. return -EINVAL;
  107. }
  108. if (!(ctrl2 & RX8025_BIT_CTRL2_XST)) {
  109. dev_warn(dev, "crystal stopped, date is invalid\n");
  110. return -EINVAL;
  111. }
  112. return 0;
  113. }
  114. static int rx8025_reset_validity(struct i2c_client *client)
  115. {
  116. int ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
  117. if (ctrl2 < 0)
  118. return ctrl2;
  119. ctrl2 &= ~(RX8025_BIT_CTRL2_PON | RX8025_BIT_CTRL2_VDET);
  120. return rx8025_write_reg(client, RX8025_REG_CTRL2,
  121. ctrl2 | RX8025_BIT_CTRL2_XST);
  122. }
  123. static irqreturn_t rx8025_handle_irq(int irq, void *dev_id)
  124. {
  125. struct i2c_client *client = dev_id;
  126. struct rx8025_data *rx8025 = i2c_get_clientdata(client);
  127. struct mutex *lock = &rx8025->rtc->ops_lock;
  128. int status;
  129. mutex_lock(lock);
  130. status = rx8025_read_reg(client, RX8025_REG_CTRL2);
  131. if (status < 0)
  132. goto out;
  133. if (!(status & RX8025_BIT_CTRL2_XST))
  134. dev_warn(&client->dev, "Oscillation stop was detected,"
  135. "you may have to readjust the clock\n");
  136. if (status & RX8025_BIT_CTRL2_CTFG) {
  137. /* periodic */
  138. status &= ~RX8025_BIT_CTRL2_CTFG;
  139. rtc_update_irq(rx8025->rtc, 1, RTC_PF | RTC_IRQF);
  140. }
  141. if (status & RX8025_BIT_CTRL2_DAFG) {
  142. /* alarm */
  143. status &= RX8025_BIT_CTRL2_DAFG;
  144. if (rx8025_write_reg(client, RX8025_REG_CTRL1,
  145. rx8025->ctrl1 & ~RX8025_BIT_CTRL1_DALE))
  146. goto out;
  147. rtc_update_irq(rx8025->rtc, 1, RTC_AF | RTC_IRQF);
  148. }
  149. out:
  150. mutex_unlock(lock);
  151. return IRQ_HANDLED;
  152. }
  153. static int rx8025_get_time(struct device *dev, struct rtc_time *dt)
  154. {
  155. struct rx8025_data *rx8025 = dev_get_drvdata(dev);
  156. u8 date[7];
  157. int err;
  158. err = rx8025_check_validity(dev);
  159. if (err)
  160. return err;
  161. err = rx8025_read_regs(rx8025->client, RX8025_REG_SEC, 7, date);
  162. if (err)
  163. return err;
  164. dev_dbg(dev, "%s: read 0x%02x 0x%02x "
  165. "0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n", __func__,
  166. date[0], date[1], date[2], date[3], date[4],
  167. date[5], date[6]);
  168. dt->tm_sec = bcd2bin(date[RX8025_REG_SEC] & 0x7f);
  169. dt->tm_min = bcd2bin(date[RX8025_REG_MIN] & 0x7f);
  170. if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
  171. dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x3f);
  172. else
  173. dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x1f) % 12
  174. + (date[RX8025_REG_HOUR] & 0x20 ? 12 : 0);
  175. dt->tm_mday = bcd2bin(date[RX8025_REG_MDAY] & 0x3f);
  176. dt->tm_mon = bcd2bin(date[RX8025_REG_MONTH] & 0x1f) - 1;
  177. dt->tm_year = bcd2bin(date[RX8025_REG_YEAR]) + 100;
  178. dev_dbg(dev, "%s: date %ds %dm %dh %dmd %dm %dy\n", __func__,
  179. dt->tm_sec, dt->tm_min, dt->tm_hour,
  180. dt->tm_mday, dt->tm_mon, dt->tm_year);
  181. return 0;
  182. }
  183. static int rx8025_set_time(struct device *dev, struct rtc_time *dt)
  184. {
  185. struct rx8025_data *rx8025 = dev_get_drvdata(dev);
  186. u8 date[7];
  187. int ret;
  188. if ((dt->tm_year < 100) || (dt->tm_year > 199))
  189. return -EINVAL;
  190. /*
  191. * Here the read-only bits are written as "0". I'm not sure if that
  192. * is sound.
  193. */
  194. date[RX8025_REG_SEC] = bin2bcd(dt->tm_sec);
  195. date[RX8025_REG_MIN] = bin2bcd(dt->tm_min);
  196. if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
  197. date[RX8025_REG_HOUR] = bin2bcd(dt->tm_hour);
  198. else
  199. date[RX8025_REG_HOUR] = (dt->tm_hour >= 12 ? 0x20 : 0)
  200. | bin2bcd((dt->tm_hour + 11) % 12 + 1);
  201. date[RX8025_REG_WDAY] = bin2bcd(dt->tm_wday);
  202. date[RX8025_REG_MDAY] = bin2bcd(dt->tm_mday);
  203. date[RX8025_REG_MONTH] = bin2bcd(dt->tm_mon + 1);
  204. date[RX8025_REG_YEAR] = bin2bcd(dt->tm_year - 100);
  205. dev_dbg(dev,
  206. "%s: write 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
  207. __func__,
  208. date[0], date[1], date[2], date[3], date[4], date[5], date[6]);
  209. ret = rx8025_write_regs(rx8025->client, RX8025_REG_SEC, 7, date);
  210. if (ret < 0)
  211. return ret;
  212. return rx8025_reset_validity(rx8025->client);
  213. }
  214. static int rx8025_init_client(struct i2c_client *client)
  215. {
  216. struct rx8025_data *rx8025 = i2c_get_clientdata(client);
  217. u8 ctrl[2], ctrl2;
  218. int need_clear = 0;
  219. int err;
  220. err = rx8025_read_regs(rx8025->client, RX8025_REG_CTRL1, 2, ctrl);
  221. if (err)
  222. goto out;
  223. /* Keep test bit zero ! */
  224. rx8025->ctrl1 = ctrl[0] & ~RX8025_BIT_CTRL1_TEST;
  225. if (ctrl[1] & (RX8025_BIT_CTRL2_DAFG | RX8025_BIT_CTRL2_WAFG)) {
  226. dev_warn(&client->dev, "Alarm was detected\n");
  227. need_clear = 1;
  228. }
  229. if (ctrl[1] & RX8025_BIT_CTRL2_CTFG)
  230. need_clear = 1;
  231. if (need_clear) {
  232. ctrl2 = ctrl[1];
  233. ctrl2 &= ~(RX8025_BIT_CTRL2_CTFG | RX8025_BIT_CTRL2_WAFG |
  234. RX8025_BIT_CTRL2_DAFG);
  235. err = rx8025_write_reg(client, RX8025_REG_CTRL2, ctrl2);
  236. }
  237. out:
  238. return err;
  239. }
  240. /* Alarm support */
  241. static int rx8025_read_alarm(struct device *dev, struct rtc_wkalrm *t)
  242. {
  243. struct rx8025_data *rx8025 = dev_get_drvdata(dev);
  244. struct i2c_client *client = rx8025->client;
  245. u8 ald[2];
  246. int ctrl2, err;
  247. if (client->irq <= 0)
  248. return -EINVAL;
  249. err = rx8025_read_regs(client, RX8025_REG_ALDMIN, 2, ald);
  250. if (err)
  251. return err;
  252. ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
  253. if (ctrl2 < 0)
  254. return ctrl2;
  255. dev_dbg(dev, "%s: read alarm 0x%02x 0x%02x ctrl2 %02x\n",
  256. __func__, ald[0], ald[1], ctrl2);
  257. /* Hardware alarms precision is 1 minute! */
  258. t->time.tm_sec = 0;
  259. t->time.tm_min = bcd2bin(ald[0] & 0x7f);
  260. if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
  261. t->time.tm_hour = bcd2bin(ald[1] & 0x3f);
  262. else
  263. t->time.tm_hour = bcd2bin(ald[1] & 0x1f) % 12
  264. + (ald[1] & 0x20 ? 12 : 0);
  265. dev_dbg(dev, "%s: date: %ds %dm %dh %dmd %dm %dy\n",
  266. __func__,
  267. t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
  268. t->time.tm_mday, t->time.tm_mon, t->time.tm_year);
  269. t->enabled = !!(rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE);
  270. t->pending = (ctrl2 & RX8025_BIT_CTRL2_DAFG) && t->enabled;
  271. return err;
  272. }
  273. static int rx8025_set_alarm(struct device *dev, struct rtc_wkalrm *t)
  274. {
  275. struct i2c_client *client = to_i2c_client(dev);
  276. struct rx8025_data *rx8025 = dev_get_drvdata(dev);
  277. u8 ald[2];
  278. int err;
  279. if (client->irq <= 0)
  280. return -EINVAL;
  281. /*
  282. * Hardware alarm precision is 1 minute!
  283. * round up to nearest minute
  284. */
  285. if (t->time.tm_sec) {
  286. time64_t alarm_time = rtc_tm_to_time64(&t->time);
  287. alarm_time += 60 - t->time.tm_sec;
  288. rtc_time64_to_tm(alarm_time, &t->time);
  289. }
  290. ald[0] = bin2bcd(t->time.tm_min);
  291. if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
  292. ald[1] = bin2bcd(t->time.tm_hour);
  293. else
  294. ald[1] = (t->time.tm_hour >= 12 ? 0x20 : 0)
  295. | bin2bcd((t->time.tm_hour + 11) % 12 + 1);
  296. dev_dbg(dev, "%s: write 0x%02x 0x%02x\n", __func__, ald[0], ald[1]);
  297. if (rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE) {
  298. rx8025->ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
  299. err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
  300. rx8025->ctrl1);
  301. if (err)
  302. return err;
  303. }
  304. err = rx8025_write_regs(rx8025->client, RX8025_REG_ALDMIN, 2, ald);
  305. if (err)
  306. return err;
  307. if (t->enabled) {
  308. rx8025->ctrl1 |= RX8025_BIT_CTRL1_DALE;
  309. err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
  310. rx8025->ctrl1);
  311. if (err)
  312. return err;
  313. }
  314. return 0;
  315. }
  316. static int rx8025_alarm_irq_enable(struct device *dev, unsigned int enabled)
  317. {
  318. struct rx8025_data *rx8025 = dev_get_drvdata(dev);
  319. u8 ctrl1;
  320. int err;
  321. ctrl1 = rx8025->ctrl1;
  322. if (enabled)
  323. ctrl1 |= RX8025_BIT_CTRL1_DALE;
  324. else
  325. ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
  326. if (ctrl1 != rx8025->ctrl1) {
  327. rx8025->ctrl1 = ctrl1;
  328. err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
  329. rx8025->ctrl1);
  330. if (err)
  331. return err;
  332. }
  333. return 0;
  334. }
  335. static const struct rtc_class_ops rx8025_rtc_ops = {
  336. .read_time = rx8025_get_time,
  337. .set_time = rx8025_set_time,
  338. .read_alarm = rx8025_read_alarm,
  339. .set_alarm = rx8025_set_alarm,
  340. .alarm_irq_enable = rx8025_alarm_irq_enable,
  341. };
  342. /*
  343. * Clock precision adjustment support
  344. *
  345. * According to the RX8025 SA/NB application manual the frequency and
  346. * temperature characteristics can be approximated using the following
  347. * equation:
  348. *
  349. * df = a * (ut - t)**2
  350. *
  351. * df: Frequency deviation in any temperature
  352. * a : Coefficient = (-35 +-5) * 10**-9
  353. * ut: Ultimate temperature in degree = +25 +-5 degree
  354. * t : Any temperature in degree
  355. *
  356. * Note that the clock adjustment in ppb must be entered (which is
  357. * the negative value of the deviation).
  358. */
  359. static int rx8025_get_clock_adjust(struct device *dev, int *adj)
  360. {
  361. struct i2c_client *client = to_i2c_client(dev);
  362. int digoff;
  363. digoff = rx8025_read_reg(client, RX8025_REG_DIGOFF);
  364. if (digoff < 0)
  365. return digoff;
  366. *adj = digoff >= 64 ? digoff - 128 : digoff;
  367. if (*adj > 0)
  368. (*adj)--;
  369. *adj *= -RX8025_ADJ_RESOLUTION;
  370. return 0;
  371. }
  372. static int rx8025_set_clock_adjust(struct device *dev, int adj)
  373. {
  374. struct i2c_client *client = to_i2c_client(dev);
  375. u8 digoff;
  376. int err;
  377. adj /= -RX8025_ADJ_RESOLUTION;
  378. if (adj > RX8025_ADJ_DATA_MAX)
  379. adj = RX8025_ADJ_DATA_MAX;
  380. else if (adj < RX8025_ADJ_DATA_MIN)
  381. adj = RX8025_ADJ_DATA_MIN;
  382. else if (adj > 0)
  383. adj++;
  384. else if (adj < 0)
  385. adj += 128;
  386. digoff = adj;
  387. err = rx8025_write_reg(client, RX8025_REG_DIGOFF, digoff);
  388. if (err)
  389. return err;
  390. dev_dbg(dev, "%s: write 0x%02x\n", __func__, digoff);
  391. return 0;
  392. }
  393. static ssize_t rx8025_sysfs_show_clock_adjust(struct device *dev,
  394. struct device_attribute *attr,
  395. char *buf)
  396. {
  397. int err, adj;
  398. err = rx8025_get_clock_adjust(dev, &adj);
  399. if (err)
  400. return err;
  401. return sprintf(buf, "%d\n", adj);
  402. }
  403. static ssize_t rx8025_sysfs_store_clock_adjust(struct device *dev,
  404. struct device_attribute *attr,
  405. const char *buf, size_t count)
  406. {
  407. int adj, err;
  408. if (sscanf(buf, "%i", &adj) != 1)
  409. return -EINVAL;
  410. err = rx8025_set_clock_adjust(dev, adj);
  411. return err ? err : count;
  412. }
  413. static DEVICE_ATTR(clock_adjust_ppb, S_IRUGO | S_IWUSR,
  414. rx8025_sysfs_show_clock_adjust,
  415. rx8025_sysfs_store_clock_adjust);
  416. static int rx8025_sysfs_register(struct device *dev)
  417. {
  418. return device_create_file(dev, &dev_attr_clock_adjust_ppb);
  419. }
  420. static void rx8025_sysfs_unregister(struct device *dev)
  421. {
  422. device_remove_file(dev, &dev_attr_clock_adjust_ppb);
  423. }
  424. static int rx8025_probe(struct i2c_client *client,
  425. const struct i2c_device_id *id)
  426. {
  427. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  428. struct rx8025_data *rx8025;
  429. int err = 0;
  430. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
  431. | I2C_FUNC_SMBUS_I2C_BLOCK)) {
  432. dev_err(&adapter->dev,
  433. "doesn't support required functionality\n");
  434. return -EIO;
  435. }
  436. rx8025 = devm_kzalloc(&client->dev, sizeof(*rx8025), GFP_KERNEL);
  437. if (!rx8025)
  438. return -ENOMEM;
  439. rx8025->client = client;
  440. i2c_set_clientdata(client, rx8025);
  441. err = rx8025_init_client(client);
  442. if (err)
  443. return err;
  444. rx8025->rtc = devm_rtc_device_register(&client->dev, client->name,
  445. &rx8025_rtc_ops, THIS_MODULE);
  446. if (IS_ERR(rx8025->rtc)) {
  447. dev_err(&client->dev, "unable to register the class device\n");
  448. return PTR_ERR(rx8025->rtc);
  449. }
  450. if (client->irq > 0) {
  451. dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
  452. err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
  453. rx8025_handle_irq,
  454. IRQF_ONESHOT,
  455. "rx8025", client);
  456. if (err) {
  457. dev_err(&client->dev, "unable to request IRQ, alarms disabled\n");
  458. client->irq = 0;
  459. }
  460. }
  461. rx8025->rtc->max_user_freq = 1;
  462. /* the rx8025 alarm only supports a minute accuracy */
  463. rx8025->rtc->uie_unsupported = 1;
  464. err = rx8025_sysfs_register(&client->dev);
  465. return err;
  466. }
  467. static int rx8025_remove(struct i2c_client *client)
  468. {
  469. rx8025_sysfs_unregister(&client->dev);
  470. return 0;
  471. }
  472. static struct i2c_driver rx8025_driver = {
  473. .driver = {
  474. .name = "rtc-rx8025",
  475. },
  476. .probe = rx8025_probe,
  477. .remove = rx8025_remove,
  478. .id_table = rx8025_id,
  479. };
  480. module_i2c_driver(rx8025_driver);
  481. MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
  482. MODULE_DESCRIPTION("RX-8025 SA/NB RTC driver");
  483. MODULE_LICENSE("GPL");