rtc-abx80x.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /*
  2. * A driver for the I2C members of the Abracon AB x8xx RTC family,
  3. * and compatible: AB 1805 and AB 0805
  4. *
  5. * Copyright 2014-2015 Macq S.A.
  6. *
  7. * Author: Philippe De Muyter <phdm@macqel.be>
  8. * Author: Alexandre Belloni <alexandre.belloni@free-electrons.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. */
  15. #include <linux/bcd.h>
  16. #include <linux/i2c.h>
  17. #include <linux/module.h>
  18. #include <linux/rtc.h>
  19. #define ABX8XX_REG_HTH 0x00
  20. #define ABX8XX_REG_SC 0x01
  21. #define ABX8XX_REG_MN 0x02
  22. #define ABX8XX_REG_HR 0x03
  23. #define ABX8XX_REG_DA 0x04
  24. #define ABX8XX_REG_MO 0x05
  25. #define ABX8XX_REG_YR 0x06
  26. #define ABX8XX_REG_WD 0x07
  27. #define ABX8XX_REG_AHTH 0x08
  28. #define ABX8XX_REG_ASC 0x09
  29. #define ABX8XX_REG_AMN 0x0a
  30. #define ABX8XX_REG_AHR 0x0b
  31. #define ABX8XX_REG_ADA 0x0c
  32. #define ABX8XX_REG_AMO 0x0d
  33. #define ABX8XX_REG_AWD 0x0e
  34. #define ABX8XX_REG_STATUS 0x0f
  35. #define ABX8XX_STATUS_AF BIT(2)
  36. #define ABX8XX_REG_CTRL1 0x10
  37. #define ABX8XX_CTRL_WRITE BIT(0)
  38. #define ABX8XX_CTRL_ARST BIT(2)
  39. #define ABX8XX_CTRL_12_24 BIT(6)
  40. #define ABX8XX_REG_IRQ 0x12
  41. #define ABX8XX_IRQ_AIE BIT(2)
  42. #define ABX8XX_IRQ_IM_1_4 (0x3 << 5)
  43. #define ABX8XX_REG_CD_TIMER_CTL 0x18
  44. #define ABX8XX_REG_OSC 0x1c
  45. #define ABX8XX_OSC_FOS BIT(3)
  46. #define ABX8XX_OSC_BOS BIT(4)
  47. #define ABX8XX_OSC_ACAL_512 BIT(5)
  48. #define ABX8XX_OSC_ACAL_1024 BIT(6)
  49. #define ABX8XX_OSC_OSEL BIT(7)
  50. #define ABX8XX_REG_OSS 0x1d
  51. #define ABX8XX_OSS_OF BIT(1)
  52. #define ABX8XX_OSS_OMODE BIT(4)
  53. #define ABX8XX_REG_CFG_KEY 0x1f
  54. #define ABX8XX_CFG_KEY_OSC 0xa1
  55. #define ABX8XX_CFG_KEY_MISC 0x9d
  56. #define ABX8XX_REG_ID0 0x28
  57. #define ABX8XX_REG_TRICKLE 0x20
  58. #define ABX8XX_TRICKLE_CHARGE_ENABLE 0xa0
  59. #define ABX8XX_TRICKLE_STANDARD_DIODE 0x8
  60. #define ABX8XX_TRICKLE_SCHOTTKY_DIODE 0x4
  61. static u8 trickle_resistors[] = {0, 3, 6, 11};
  62. enum abx80x_chip {AB0801, AB0803, AB0804, AB0805,
  63. AB1801, AB1803, AB1804, AB1805, ABX80X};
  64. struct abx80x_cap {
  65. u16 pn;
  66. bool has_tc;
  67. };
  68. static struct abx80x_cap abx80x_caps[] = {
  69. [AB0801] = {.pn = 0x0801},
  70. [AB0803] = {.pn = 0x0803},
  71. [AB0804] = {.pn = 0x0804, .has_tc = true},
  72. [AB0805] = {.pn = 0x0805, .has_tc = true},
  73. [AB1801] = {.pn = 0x1801},
  74. [AB1803] = {.pn = 0x1803},
  75. [AB1804] = {.pn = 0x1804, .has_tc = true},
  76. [AB1805] = {.pn = 0x1805, .has_tc = true},
  77. [ABX80X] = {.pn = 0}
  78. };
  79. static int abx80x_is_rc_mode(struct i2c_client *client)
  80. {
  81. int flags = 0;
  82. flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS);
  83. if (flags < 0) {
  84. dev_err(&client->dev,
  85. "Failed to read autocalibration attribute\n");
  86. return flags;
  87. }
  88. return (flags & ABX8XX_OSS_OMODE) ? 1 : 0;
  89. }
  90. static int abx80x_enable_trickle_charger(struct i2c_client *client,
  91. u8 trickle_cfg)
  92. {
  93. int err;
  94. /*
  95. * Write the configuration key register to enable access to the Trickle
  96. * register
  97. */
  98. err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY,
  99. ABX8XX_CFG_KEY_MISC);
  100. if (err < 0) {
  101. dev_err(&client->dev, "Unable to write configuration key\n");
  102. return -EIO;
  103. }
  104. err = i2c_smbus_write_byte_data(client, ABX8XX_REG_TRICKLE,
  105. ABX8XX_TRICKLE_CHARGE_ENABLE |
  106. trickle_cfg);
  107. if (err < 0) {
  108. dev_err(&client->dev, "Unable to write trickle register\n");
  109. return -EIO;
  110. }
  111. return 0;
  112. }
  113. static int abx80x_rtc_read_time(struct device *dev, struct rtc_time *tm)
  114. {
  115. struct i2c_client *client = to_i2c_client(dev);
  116. unsigned char buf[8];
  117. int err, flags, rc_mode = 0;
  118. /* Read the Oscillator Failure only in XT mode */
  119. rc_mode = abx80x_is_rc_mode(client);
  120. if (rc_mode < 0)
  121. return rc_mode;
  122. if (!rc_mode) {
  123. flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS);
  124. if (flags < 0)
  125. return flags;
  126. if (flags & ABX8XX_OSS_OF) {
  127. dev_err(dev, "Oscillator failure, data is invalid.\n");
  128. return -EINVAL;
  129. }
  130. }
  131. err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_HTH,
  132. sizeof(buf), buf);
  133. if (err < 0) {
  134. dev_err(&client->dev, "Unable to read date\n");
  135. return -EIO;
  136. }
  137. tm->tm_sec = bcd2bin(buf[ABX8XX_REG_SC] & 0x7F);
  138. tm->tm_min = bcd2bin(buf[ABX8XX_REG_MN] & 0x7F);
  139. tm->tm_hour = bcd2bin(buf[ABX8XX_REG_HR] & 0x3F);
  140. tm->tm_wday = buf[ABX8XX_REG_WD] & 0x7;
  141. tm->tm_mday = bcd2bin(buf[ABX8XX_REG_DA] & 0x3F);
  142. tm->tm_mon = bcd2bin(buf[ABX8XX_REG_MO] & 0x1F) - 1;
  143. tm->tm_year = bcd2bin(buf[ABX8XX_REG_YR]) + 100;
  144. err = rtc_valid_tm(tm);
  145. if (err < 0)
  146. dev_err(&client->dev, "retrieved date/time is not valid.\n");
  147. return err;
  148. }
  149. static int abx80x_rtc_set_time(struct device *dev, struct rtc_time *tm)
  150. {
  151. struct i2c_client *client = to_i2c_client(dev);
  152. unsigned char buf[8];
  153. int err, flags;
  154. if (tm->tm_year < 100)
  155. return -EINVAL;
  156. buf[ABX8XX_REG_HTH] = 0;
  157. buf[ABX8XX_REG_SC] = bin2bcd(tm->tm_sec);
  158. buf[ABX8XX_REG_MN] = bin2bcd(tm->tm_min);
  159. buf[ABX8XX_REG_HR] = bin2bcd(tm->tm_hour);
  160. buf[ABX8XX_REG_DA] = bin2bcd(tm->tm_mday);
  161. buf[ABX8XX_REG_MO] = bin2bcd(tm->tm_mon + 1);
  162. buf[ABX8XX_REG_YR] = bin2bcd(tm->tm_year - 100);
  163. buf[ABX8XX_REG_WD] = tm->tm_wday;
  164. err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_HTH,
  165. sizeof(buf), buf);
  166. if (err < 0) {
  167. dev_err(&client->dev, "Unable to write to date registers\n");
  168. return -EIO;
  169. }
  170. /* Clear the OF bit of Oscillator Status Register */
  171. flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS);
  172. if (flags < 0)
  173. return flags;
  174. err = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSS,
  175. flags & ~ABX8XX_OSS_OF);
  176. if (err < 0) {
  177. dev_err(&client->dev, "Unable to write oscillator status register\n");
  178. return err;
  179. }
  180. return 0;
  181. }
  182. static irqreturn_t abx80x_handle_irq(int irq, void *dev_id)
  183. {
  184. struct i2c_client *client = dev_id;
  185. struct rtc_device *rtc = i2c_get_clientdata(client);
  186. int status;
  187. status = i2c_smbus_read_byte_data(client, ABX8XX_REG_STATUS);
  188. if (status < 0)
  189. return IRQ_NONE;
  190. if (status & ABX8XX_STATUS_AF)
  191. rtc_update_irq(rtc, 1, RTC_AF | RTC_IRQF);
  192. i2c_smbus_write_byte_data(client, ABX8XX_REG_STATUS, 0);
  193. return IRQ_HANDLED;
  194. }
  195. static int abx80x_read_alarm(struct device *dev, struct rtc_wkalrm *t)
  196. {
  197. struct i2c_client *client = to_i2c_client(dev);
  198. unsigned char buf[7];
  199. int irq_mask, err;
  200. if (client->irq <= 0)
  201. return -EINVAL;
  202. err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ASC,
  203. sizeof(buf), buf);
  204. if (err)
  205. return err;
  206. irq_mask = i2c_smbus_read_byte_data(client, ABX8XX_REG_IRQ);
  207. if (irq_mask < 0)
  208. return irq_mask;
  209. t->time.tm_sec = bcd2bin(buf[0] & 0x7F);
  210. t->time.tm_min = bcd2bin(buf[1] & 0x7F);
  211. t->time.tm_hour = bcd2bin(buf[2] & 0x3F);
  212. t->time.tm_mday = bcd2bin(buf[3] & 0x3F);
  213. t->time.tm_mon = bcd2bin(buf[4] & 0x1F) - 1;
  214. t->time.tm_wday = buf[5] & 0x7;
  215. t->enabled = !!(irq_mask & ABX8XX_IRQ_AIE);
  216. t->pending = (buf[6] & ABX8XX_STATUS_AF) && t->enabled;
  217. return err;
  218. }
  219. static int abx80x_set_alarm(struct device *dev, struct rtc_wkalrm *t)
  220. {
  221. struct i2c_client *client = to_i2c_client(dev);
  222. u8 alarm[6];
  223. int err;
  224. if (client->irq <= 0)
  225. return -EINVAL;
  226. alarm[0] = 0x0;
  227. alarm[1] = bin2bcd(t->time.tm_sec);
  228. alarm[2] = bin2bcd(t->time.tm_min);
  229. alarm[3] = bin2bcd(t->time.tm_hour);
  230. alarm[4] = bin2bcd(t->time.tm_mday);
  231. alarm[5] = bin2bcd(t->time.tm_mon + 1);
  232. err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_AHTH,
  233. sizeof(alarm), alarm);
  234. if (err < 0) {
  235. dev_err(&client->dev, "Unable to write alarm registers\n");
  236. return -EIO;
  237. }
  238. if (t->enabled) {
  239. err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
  240. (ABX8XX_IRQ_IM_1_4 |
  241. ABX8XX_IRQ_AIE));
  242. if (err)
  243. return err;
  244. }
  245. return 0;
  246. }
  247. static int abx80x_rtc_set_autocalibration(struct device *dev,
  248. int autocalibration)
  249. {
  250. struct i2c_client *client = to_i2c_client(dev);
  251. int retval, flags = 0;
  252. if ((autocalibration != 0) && (autocalibration != 1024) &&
  253. (autocalibration != 512)) {
  254. dev_err(dev, "autocalibration value outside permitted range\n");
  255. return -EINVAL;
  256. }
  257. flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC);
  258. if (flags < 0)
  259. return flags;
  260. if (autocalibration == 0) {
  261. flags &= ~(ABX8XX_OSC_ACAL_512 | ABX8XX_OSC_ACAL_1024);
  262. } else if (autocalibration == 1024) {
  263. /* 1024 autocalibration is 0x10 */
  264. flags |= ABX8XX_OSC_ACAL_1024;
  265. flags &= ~(ABX8XX_OSC_ACAL_512);
  266. } else {
  267. /* 512 autocalibration is 0x11 */
  268. flags |= (ABX8XX_OSC_ACAL_1024 | ABX8XX_OSC_ACAL_512);
  269. }
  270. /* Unlock write access to Oscillator Control Register */
  271. retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY,
  272. ABX8XX_CFG_KEY_OSC);
  273. if (retval < 0) {
  274. dev_err(dev, "Failed to write CONFIG_KEY register\n");
  275. return retval;
  276. }
  277. retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSC, flags);
  278. return retval;
  279. }
  280. static int abx80x_rtc_get_autocalibration(struct device *dev)
  281. {
  282. struct i2c_client *client = to_i2c_client(dev);
  283. int flags = 0, autocalibration;
  284. flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC);
  285. if (flags < 0)
  286. return flags;
  287. if (flags & ABX8XX_OSC_ACAL_512)
  288. autocalibration = 512;
  289. else if (flags & ABX8XX_OSC_ACAL_1024)
  290. autocalibration = 1024;
  291. else
  292. autocalibration = 0;
  293. return autocalibration;
  294. }
  295. static ssize_t autocalibration_store(struct device *dev,
  296. struct device_attribute *attr,
  297. const char *buf, size_t count)
  298. {
  299. int retval;
  300. unsigned long autocalibration = 0;
  301. retval = kstrtoul(buf, 10, &autocalibration);
  302. if (retval < 0) {
  303. dev_err(dev, "Failed to store RTC autocalibration attribute\n");
  304. return -EINVAL;
  305. }
  306. retval = abx80x_rtc_set_autocalibration(dev, autocalibration);
  307. return retval ? retval : count;
  308. }
  309. static ssize_t autocalibration_show(struct device *dev,
  310. struct device_attribute *attr, char *buf)
  311. {
  312. int autocalibration = 0;
  313. autocalibration = abx80x_rtc_get_autocalibration(dev);
  314. if (autocalibration < 0) {
  315. dev_err(dev, "Failed to read RTC autocalibration\n");
  316. sprintf(buf, "0\n");
  317. return autocalibration;
  318. }
  319. return sprintf(buf, "%d\n", autocalibration);
  320. }
  321. static DEVICE_ATTR_RW(autocalibration);
  322. static ssize_t oscillator_store(struct device *dev,
  323. struct device_attribute *attr,
  324. const char *buf, size_t count)
  325. {
  326. struct i2c_client *client = to_i2c_client(dev);
  327. int retval, flags, rc_mode = 0;
  328. if (strncmp(buf, "rc", 2) == 0) {
  329. rc_mode = 1;
  330. } else if (strncmp(buf, "xtal", 4) == 0) {
  331. rc_mode = 0;
  332. } else {
  333. dev_err(dev, "Oscillator selection value outside permitted ones\n");
  334. return -EINVAL;
  335. }
  336. flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC);
  337. if (flags < 0)
  338. return flags;
  339. if (rc_mode == 0)
  340. flags &= ~(ABX8XX_OSC_OSEL);
  341. else
  342. flags |= (ABX8XX_OSC_OSEL);
  343. /* Unlock write access on Oscillator Control register */
  344. retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY,
  345. ABX8XX_CFG_KEY_OSC);
  346. if (retval < 0) {
  347. dev_err(dev, "Failed to write CONFIG_KEY register\n");
  348. return retval;
  349. }
  350. retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSC, flags);
  351. if (retval < 0) {
  352. dev_err(dev, "Failed to write Oscillator Control register\n");
  353. return retval;
  354. }
  355. return retval ? retval : count;
  356. }
  357. static ssize_t oscillator_show(struct device *dev,
  358. struct device_attribute *attr, char *buf)
  359. {
  360. int rc_mode = 0;
  361. struct i2c_client *client = to_i2c_client(dev);
  362. rc_mode = abx80x_is_rc_mode(client);
  363. if (rc_mode < 0) {
  364. dev_err(dev, "Failed to read RTC oscillator selection\n");
  365. sprintf(buf, "\n");
  366. return rc_mode;
  367. }
  368. if (rc_mode)
  369. return sprintf(buf, "rc\n");
  370. else
  371. return sprintf(buf, "xtal\n");
  372. }
  373. static DEVICE_ATTR_RW(oscillator);
  374. static struct attribute *rtc_calib_attrs[] = {
  375. &dev_attr_autocalibration.attr,
  376. &dev_attr_oscillator.attr,
  377. NULL,
  378. };
  379. static const struct attribute_group rtc_calib_attr_group = {
  380. .attrs = rtc_calib_attrs,
  381. };
  382. static int abx80x_alarm_irq_enable(struct device *dev, unsigned int enabled)
  383. {
  384. struct i2c_client *client = to_i2c_client(dev);
  385. int err;
  386. if (enabled)
  387. err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
  388. (ABX8XX_IRQ_IM_1_4 |
  389. ABX8XX_IRQ_AIE));
  390. else
  391. err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
  392. ABX8XX_IRQ_IM_1_4);
  393. return err;
  394. }
  395. static const struct rtc_class_ops abx80x_rtc_ops = {
  396. .read_time = abx80x_rtc_read_time,
  397. .set_time = abx80x_rtc_set_time,
  398. .read_alarm = abx80x_read_alarm,
  399. .set_alarm = abx80x_set_alarm,
  400. .alarm_irq_enable = abx80x_alarm_irq_enable,
  401. };
  402. static int abx80x_dt_trickle_cfg(struct device_node *np)
  403. {
  404. const char *diode;
  405. int trickle_cfg = 0;
  406. int i, ret;
  407. u32 tmp;
  408. ret = of_property_read_string(np, "abracon,tc-diode", &diode);
  409. if (ret)
  410. return ret;
  411. if (!strcmp(diode, "standard"))
  412. trickle_cfg |= ABX8XX_TRICKLE_STANDARD_DIODE;
  413. else if (!strcmp(diode, "schottky"))
  414. trickle_cfg |= ABX8XX_TRICKLE_SCHOTTKY_DIODE;
  415. else
  416. return -EINVAL;
  417. ret = of_property_read_u32(np, "abracon,tc-resistor", &tmp);
  418. if (ret)
  419. return ret;
  420. for (i = 0; i < sizeof(trickle_resistors); i++)
  421. if (trickle_resistors[i] == tmp)
  422. break;
  423. if (i == sizeof(trickle_resistors))
  424. return -EINVAL;
  425. return (trickle_cfg | i);
  426. }
  427. static void rtc_calib_remove_sysfs_group(void *_dev)
  428. {
  429. struct device *dev = _dev;
  430. sysfs_remove_group(&dev->kobj, &rtc_calib_attr_group);
  431. }
  432. static int abx80x_probe(struct i2c_client *client,
  433. const struct i2c_device_id *id)
  434. {
  435. struct device_node *np = client->dev.of_node;
  436. struct rtc_device *rtc;
  437. int i, data, err, trickle_cfg = -EINVAL;
  438. char buf[7];
  439. unsigned int part = id->driver_data;
  440. unsigned int partnumber;
  441. unsigned int majrev, minrev;
  442. unsigned int lot;
  443. unsigned int wafer;
  444. unsigned int uid;
  445. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  446. return -ENODEV;
  447. err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ID0,
  448. sizeof(buf), buf);
  449. if (err < 0) {
  450. dev_err(&client->dev, "Unable to read partnumber\n");
  451. return -EIO;
  452. }
  453. partnumber = (buf[0] << 8) | buf[1];
  454. majrev = buf[2] >> 3;
  455. minrev = buf[2] & 0x7;
  456. lot = ((buf[4] & 0x80) << 2) | ((buf[6] & 0x80) << 1) | buf[3];
  457. uid = ((buf[4] & 0x7f) << 8) | buf[5];
  458. wafer = (buf[6] & 0x7c) >> 2;
  459. dev_info(&client->dev, "model %04x, revision %u.%u, lot %x, wafer %x, uid %x\n",
  460. partnumber, majrev, minrev, lot, wafer, uid);
  461. data = i2c_smbus_read_byte_data(client, ABX8XX_REG_CTRL1);
  462. if (data < 0) {
  463. dev_err(&client->dev, "Unable to read control register\n");
  464. return -EIO;
  465. }
  466. err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CTRL1,
  467. ((data & ~(ABX8XX_CTRL_12_24 |
  468. ABX8XX_CTRL_ARST)) |
  469. ABX8XX_CTRL_WRITE));
  470. if (err < 0) {
  471. dev_err(&client->dev, "Unable to write control register\n");
  472. return -EIO;
  473. }
  474. /* part autodetection */
  475. if (part == ABX80X) {
  476. for (i = 0; abx80x_caps[i].pn; i++)
  477. if (partnumber == abx80x_caps[i].pn)
  478. break;
  479. if (abx80x_caps[i].pn == 0) {
  480. dev_err(&client->dev, "Unknown part: %04x\n",
  481. partnumber);
  482. return -EINVAL;
  483. }
  484. part = i;
  485. }
  486. if (partnumber != abx80x_caps[part].pn) {
  487. dev_err(&client->dev, "partnumber mismatch %04x != %04x\n",
  488. partnumber, abx80x_caps[part].pn);
  489. return -EINVAL;
  490. }
  491. if (np && abx80x_caps[part].has_tc)
  492. trickle_cfg = abx80x_dt_trickle_cfg(np);
  493. if (trickle_cfg > 0) {
  494. dev_info(&client->dev, "Enabling trickle charger: %02x\n",
  495. trickle_cfg);
  496. abx80x_enable_trickle_charger(client, trickle_cfg);
  497. }
  498. err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CD_TIMER_CTL,
  499. BIT(2));
  500. if (err)
  501. return err;
  502. rtc = devm_rtc_device_register(&client->dev, "abx8xx",
  503. &abx80x_rtc_ops, THIS_MODULE);
  504. if (IS_ERR(rtc))
  505. return PTR_ERR(rtc);
  506. i2c_set_clientdata(client, rtc);
  507. if (client->irq > 0) {
  508. dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
  509. err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
  510. abx80x_handle_irq,
  511. IRQF_SHARED | IRQF_ONESHOT,
  512. "abx8xx",
  513. client);
  514. if (err) {
  515. dev_err(&client->dev, "unable to request IRQ, alarms disabled\n");
  516. client->irq = 0;
  517. }
  518. }
  519. /* Export sysfs entries */
  520. err = sysfs_create_group(&(&client->dev)->kobj, &rtc_calib_attr_group);
  521. if (err) {
  522. dev_err(&client->dev, "Failed to create sysfs group: %d\n",
  523. err);
  524. return err;
  525. }
  526. err = devm_add_action_or_reset(&client->dev,
  527. rtc_calib_remove_sysfs_group,
  528. &client->dev);
  529. if (err)
  530. dev_err(&client->dev,
  531. "Failed to add sysfs cleanup action: %d\n",
  532. err);
  533. return err;
  534. }
  535. static int abx80x_remove(struct i2c_client *client)
  536. {
  537. return 0;
  538. }
  539. static const struct i2c_device_id abx80x_id[] = {
  540. { "abx80x", ABX80X },
  541. { "ab0801", AB0801 },
  542. { "ab0803", AB0803 },
  543. { "ab0804", AB0804 },
  544. { "ab0805", AB0805 },
  545. { "ab1801", AB1801 },
  546. { "ab1803", AB1803 },
  547. { "ab1804", AB1804 },
  548. { "ab1805", AB1805 },
  549. { "rv1805", AB1805 },
  550. { }
  551. };
  552. MODULE_DEVICE_TABLE(i2c, abx80x_id);
  553. static struct i2c_driver abx80x_driver = {
  554. .driver = {
  555. .name = "rtc-abx80x",
  556. },
  557. .probe = abx80x_probe,
  558. .remove = abx80x_remove,
  559. .id_table = abx80x_id,
  560. };
  561. module_i2c_driver(abx80x_driver);
  562. MODULE_AUTHOR("Philippe De Muyter <phdm@macqel.be>");
  563. MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
  564. MODULE_DESCRIPTION("Abracon ABX80X RTC driver");
  565. MODULE_LICENSE("GPL v2");