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. return 0;
  145. }
  146. static int abx80x_rtc_set_time(struct device *dev, struct rtc_time *tm)
  147. {
  148. struct i2c_client *client = to_i2c_client(dev);
  149. unsigned char buf[8];
  150. int err, flags;
  151. if (tm->tm_year < 100)
  152. return -EINVAL;
  153. buf[ABX8XX_REG_HTH] = 0;
  154. buf[ABX8XX_REG_SC] = bin2bcd(tm->tm_sec);
  155. buf[ABX8XX_REG_MN] = bin2bcd(tm->tm_min);
  156. buf[ABX8XX_REG_HR] = bin2bcd(tm->tm_hour);
  157. buf[ABX8XX_REG_DA] = bin2bcd(tm->tm_mday);
  158. buf[ABX8XX_REG_MO] = bin2bcd(tm->tm_mon + 1);
  159. buf[ABX8XX_REG_YR] = bin2bcd(tm->tm_year - 100);
  160. buf[ABX8XX_REG_WD] = tm->tm_wday;
  161. err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_HTH,
  162. sizeof(buf), buf);
  163. if (err < 0) {
  164. dev_err(&client->dev, "Unable to write to date registers\n");
  165. return -EIO;
  166. }
  167. /* Clear the OF bit of Oscillator Status Register */
  168. flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS);
  169. if (flags < 0)
  170. return flags;
  171. err = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSS,
  172. flags & ~ABX8XX_OSS_OF);
  173. if (err < 0) {
  174. dev_err(&client->dev, "Unable to write oscillator status register\n");
  175. return err;
  176. }
  177. return 0;
  178. }
  179. static irqreturn_t abx80x_handle_irq(int irq, void *dev_id)
  180. {
  181. struct i2c_client *client = dev_id;
  182. struct rtc_device *rtc = i2c_get_clientdata(client);
  183. int status;
  184. status = i2c_smbus_read_byte_data(client, ABX8XX_REG_STATUS);
  185. if (status < 0)
  186. return IRQ_NONE;
  187. if (status & ABX8XX_STATUS_AF)
  188. rtc_update_irq(rtc, 1, RTC_AF | RTC_IRQF);
  189. i2c_smbus_write_byte_data(client, ABX8XX_REG_STATUS, 0);
  190. return IRQ_HANDLED;
  191. }
  192. static int abx80x_read_alarm(struct device *dev, struct rtc_wkalrm *t)
  193. {
  194. struct i2c_client *client = to_i2c_client(dev);
  195. unsigned char buf[7];
  196. int irq_mask, err;
  197. if (client->irq <= 0)
  198. return -EINVAL;
  199. err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ASC,
  200. sizeof(buf), buf);
  201. if (err)
  202. return err;
  203. irq_mask = i2c_smbus_read_byte_data(client, ABX8XX_REG_IRQ);
  204. if (irq_mask < 0)
  205. return irq_mask;
  206. t->time.tm_sec = bcd2bin(buf[0] & 0x7F);
  207. t->time.tm_min = bcd2bin(buf[1] & 0x7F);
  208. t->time.tm_hour = bcd2bin(buf[2] & 0x3F);
  209. t->time.tm_mday = bcd2bin(buf[3] & 0x3F);
  210. t->time.tm_mon = bcd2bin(buf[4] & 0x1F) - 1;
  211. t->time.tm_wday = buf[5] & 0x7;
  212. t->enabled = !!(irq_mask & ABX8XX_IRQ_AIE);
  213. t->pending = (buf[6] & ABX8XX_STATUS_AF) && t->enabled;
  214. return err;
  215. }
  216. static int abx80x_set_alarm(struct device *dev, struct rtc_wkalrm *t)
  217. {
  218. struct i2c_client *client = to_i2c_client(dev);
  219. u8 alarm[6];
  220. int err;
  221. if (client->irq <= 0)
  222. return -EINVAL;
  223. alarm[0] = 0x0;
  224. alarm[1] = bin2bcd(t->time.tm_sec);
  225. alarm[2] = bin2bcd(t->time.tm_min);
  226. alarm[3] = bin2bcd(t->time.tm_hour);
  227. alarm[4] = bin2bcd(t->time.tm_mday);
  228. alarm[5] = bin2bcd(t->time.tm_mon + 1);
  229. err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_AHTH,
  230. sizeof(alarm), alarm);
  231. if (err < 0) {
  232. dev_err(&client->dev, "Unable to write alarm registers\n");
  233. return -EIO;
  234. }
  235. if (t->enabled) {
  236. err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
  237. (ABX8XX_IRQ_IM_1_4 |
  238. ABX8XX_IRQ_AIE));
  239. if (err)
  240. return err;
  241. }
  242. return 0;
  243. }
  244. static int abx80x_rtc_set_autocalibration(struct device *dev,
  245. int autocalibration)
  246. {
  247. struct i2c_client *client = to_i2c_client(dev);
  248. int retval, flags = 0;
  249. if ((autocalibration != 0) && (autocalibration != 1024) &&
  250. (autocalibration != 512)) {
  251. dev_err(dev, "autocalibration value outside permitted range\n");
  252. return -EINVAL;
  253. }
  254. flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC);
  255. if (flags < 0)
  256. return flags;
  257. if (autocalibration == 0) {
  258. flags &= ~(ABX8XX_OSC_ACAL_512 | ABX8XX_OSC_ACAL_1024);
  259. } else if (autocalibration == 1024) {
  260. /* 1024 autocalibration is 0x10 */
  261. flags |= ABX8XX_OSC_ACAL_1024;
  262. flags &= ~(ABX8XX_OSC_ACAL_512);
  263. } else {
  264. /* 512 autocalibration is 0x11 */
  265. flags |= (ABX8XX_OSC_ACAL_1024 | ABX8XX_OSC_ACAL_512);
  266. }
  267. /* Unlock write access to Oscillator Control Register */
  268. retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY,
  269. ABX8XX_CFG_KEY_OSC);
  270. if (retval < 0) {
  271. dev_err(dev, "Failed to write CONFIG_KEY register\n");
  272. return retval;
  273. }
  274. retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSC, flags);
  275. return retval;
  276. }
  277. static int abx80x_rtc_get_autocalibration(struct device *dev)
  278. {
  279. struct i2c_client *client = to_i2c_client(dev);
  280. int flags = 0, autocalibration;
  281. flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC);
  282. if (flags < 0)
  283. return flags;
  284. if (flags & ABX8XX_OSC_ACAL_512)
  285. autocalibration = 512;
  286. else if (flags & ABX8XX_OSC_ACAL_1024)
  287. autocalibration = 1024;
  288. else
  289. autocalibration = 0;
  290. return autocalibration;
  291. }
  292. static ssize_t autocalibration_store(struct device *dev,
  293. struct device_attribute *attr,
  294. const char *buf, size_t count)
  295. {
  296. int retval;
  297. unsigned long autocalibration = 0;
  298. retval = kstrtoul(buf, 10, &autocalibration);
  299. if (retval < 0) {
  300. dev_err(dev, "Failed to store RTC autocalibration attribute\n");
  301. return -EINVAL;
  302. }
  303. retval = abx80x_rtc_set_autocalibration(dev, autocalibration);
  304. return retval ? retval : count;
  305. }
  306. static ssize_t autocalibration_show(struct device *dev,
  307. struct device_attribute *attr, char *buf)
  308. {
  309. int autocalibration = 0;
  310. autocalibration = abx80x_rtc_get_autocalibration(dev);
  311. if (autocalibration < 0) {
  312. dev_err(dev, "Failed to read RTC autocalibration\n");
  313. sprintf(buf, "0\n");
  314. return autocalibration;
  315. }
  316. return sprintf(buf, "%d\n", autocalibration);
  317. }
  318. static DEVICE_ATTR_RW(autocalibration);
  319. static ssize_t oscillator_store(struct device *dev,
  320. struct device_attribute *attr,
  321. const char *buf, size_t count)
  322. {
  323. struct i2c_client *client = to_i2c_client(dev);
  324. int retval, flags, rc_mode = 0;
  325. if (strncmp(buf, "rc", 2) == 0) {
  326. rc_mode = 1;
  327. } else if (strncmp(buf, "xtal", 4) == 0) {
  328. rc_mode = 0;
  329. } else {
  330. dev_err(dev, "Oscillator selection value outside permitted ones\n");
  331. return -EINVAL;
  332. }
  333. flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC);
  334. if (flags < 0)
  335. return flags;
  336. if (rc_mode == 0)
  337. flags &= ~(ABX8XX_OSC_OSEL);
  338. else
  339. flags |= (ABX8XX_OSC_OSEL);
  340. /* Unlock write access on Oscillator Control register */
  341. retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY,
  342. ABX8XX_CFG_KEY_OSC);
  343. if (retval < 0) {
  344. dev_err(dev, "Failed to write CONFIG_KEY register\n");
  345. return retval;
  346. }
  347. retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSC, flags);
  348. if (retval < 0) {
  349. dev_err(dev, "Failed to write Oscillator Control register\n");
  350. return retval;
  351. }
  352. return retval ? retval : count;
  353. }
  354. static ssize_t oscillator_show(struct device *dev,
  355. struct device_attribute *attr, char *buf)
  356. {
  357. int rc_mode = 0;
  358. struct i2c_client *client = to_i2c_client(dev);
  359. rc_mode = abx80x_is_rc_mode(client);
  360. if (rc_mode < 0) {
  361. dev_err(dev, "Failed to read RTC oscillator selection\n");
  362. sprintf(buf, "\n");
  363. return rc_mode;
  364. }
  365. if (rc_mode)
  366. return sprintf(buf, "rc\n");
  367. else
  368. return sprintf(buf, "xtal\n");
  369. }
  370. static DEVICE_ATTR_RW(oscillator);
  371. static struct attribute *rtc_calib_attrs[] = {
  372. &dev_attr_autocalibration.attr,
  373. &dev_attr_oscillator.attr,
  374. NULL,
  375. };
  376. static const struct attribute_group rtc_calib_attr_group = {
  377. .attrs = rtc_calib_attrs,
  378. };
  379. static int abx80x_alarm_irq_enable(struct device *dev, unsigned int enabled)
  380. {
  381. struct i2c_client *client = to_i2c_client(dev);
  382. int err;
  383. if (enabled)
  384. err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
  385. (ABX8XX_IRQ_IM_1_4 |
  386. ABX8XX_IRQ_AIE));
  387. else
  388. err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
  389. ABX8XX_IRQ_IM_1_4);
  390. return err;
  391. }
  392. static const struct rtc_class_ops abx80x_rtc_ops = {
  393. .read_time = abx80x_rtc_read_time,
  394. .set_time = abx80x_rtc_set_time,
  395. .read_alarm = abx80x_read_alarm,
  396. .set_alarm = abx80x_set_alarm,
  397. .alarm_irq_enable = abx80x_alarm_irq_enable,
  398. };
  399. static int abx80x_dt_trickle_cfg(struct device_node *np)
  400. {
  401. const char *diode;
  402. int trickle_cfg = 0;
  403. int i, ret;
  404. u32 tmp;
  405. ret = of_property_read_string(np, "abracon,tc-diode", &diode);
  406. if (ret)
  407. return ret;
  408. if (!strcmp(diode, "standard"))
  409. trickle_cfg |= ABX8XX_TRICKLE_STANDARD_DIODE;
  410. else if (!strcmp(diode, "schottky"))
  411. trickle_cfg |= ABX8XX_TRICKLE_SCHOTTKY_DIODE;
  412. else
  413. return -EINVAL;
  414. ret = of_property_read_u32(np, "abracon,tc-resistor", &tmp);
  415. if (ret)
  416. return ret;
  417. for (i = 0; i < sizeof(trickle_resistors); i++)
  418. if (trickle_resistors[i] == tmp)
  419. break;
  420. if (i == sizeof(trickle_resistors))
  421. return -EINVAL;
  422. return (trickle_cfg | i);
  423. }
  424. static void rtc_calib_remove_sysfs_group(void *_dev)
  425. {
  426. struct device *dev = _dev;
  427. sysfs_remove_group(&dev->kobj, &rtc_calib_attr_group);
  428. }
  429. static int abx80x_probe(struct i2c_client *client,
  430. const struct i2c_device_id *id)
  431. {
  432. struct device_node *np = client->dev.of_node;
  433. struct rtc_device *rtc;
  434. int i, data, err, trickle_cfg = -EINVAL;
  435. char buf[7];
  436. unsigned int part = id->driver_data;
  437. unsigned int partnumber;
  438. unsigned int majrev, minrev;
  439. unsigned int lot;
  440. unsigned int wafer;
  441. unsigned int uid;
  442. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  443. return -ENODEV;
  444. err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ID0,
  445. sizeof(buf), buf);
  446. if (err < 0) {
  447. dev_err(&client->dev, "Unable to read partnumber\n");
  448. return -EIO;
  449. }
  450. partnumber = (buf[0] << 8) | buf[1];
  451. majrev = buf[2] >> 3;
  452. minrev = buf[2] & 0x7;
  453. lot = ((buf[4] & 0x80) << 2) | ((buf[6] & 0x80) << 1) | buf[3];
  454. uid = ((buf[4] & 0x7f) << 8) | buf[5];
  455. wafer = (buf[6] & 0x7c) >> 2;
  456. dev_info(&client->dev, "model %04x, revision %u.%u, lot %x, wafer %x, uid %x\n",
  457. partnumber, majrev, minrev, lot, wafer, uid);
  458. data = i2c_smbus_read_byte_data(client, ABX8XX_REG_CTRL1);
  459. if (data < 0) {
  460. dev_err(&client->dev, "Unable to read control register\n");
  461. return -EIO;
  462. }
  463. err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CTRL1,
  464. ((data & ~(ABX8XX_CTRL_12_24 |
  465. ABX8XX_CTRL_ARST)) |
  466. ABX8XX_CTRL_WRITE));
  467. if (err < 0) {
  468. dev_err(&client->dev, "Unable to write control register\n");
  469. return -EIO;
  470. }
  471. /* part autodetection */
  472. if (part == ABX80X) {
  473. for (i = 0; abx80x_caps[i].pn; i++)
  474. if (partnumber == abx80x_caps[i].pn)
  475. break;
  476. if (abx80x_caps[i].pn == 0) {
  477. dev_err(&client->dev, "Unknown part: %04x\n",
  478. partnumber);
  479. return -EINVAL;
  480. }
  481. part = i;
  482. }
  483. if (partnumber != abx80x_caps[part].pn) {
  484. dev_err(&client->dev, "partnumber mismatch %04x != %04x\n",
  485. partnumber, abx80x_caps[part].pn);
  486. return -EINVAL;
  487. }
  488. if (np && abx80x_caps[part].has_tc)
  489. trickle_cfg = abx80x_dt_trickle_cfg(np);
  490. if (trickle_cfg > 0) {
  491. dev_info(&client->dev, "Enabling trickle charger: %02x\n",
  492. trickle_cfg);
  493. abx80x_enable_trickle_charger(client, trickle_cfg);
  494. }
  495. err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CD_TIMER_CTL,
  496. BIT(2));
  497. if (err)
  498. return err;
  499. rtc = devm_rtc_allocate_device(&client->dev);
  500. if (IS_ERR(rtc))
  501. return PTR_ERR(rtc);
  502. rtc->ops = &abx80x_rtc_ops;
  503. i2c_set_clientdata(client, rtc);
  504. if (client->irq > 0) {
  505. dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
  506. err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
  507. abx80x_handle_irq,
  508. IRQF_SHARED | IRQF_ONESHOT,
  509. "abx8xx",
  510. client);
  511. if (err) {
  512. dev_err(&client->dev, "unable to request IRQ, alarms disabled\n");
  513. client->irq = 0;
  514. }
  515. }
  516. /* Export sysfs entries */
  517. err = sysfs_create_group(&(&client->dev)->kobj, &rtc_calib_attr_group);
  518. if (err) {
  519. dev_err(&client->dev, "Failed to create sysfs group: %d\n",
  520. err);
  521. return err;
  522. }
  523. err = devm_add_action_or_reset(&client->dev,
  524. rtc_calib_remove_sysfs_group,
  525. &client->dev);
  526. if (err) {
  527. dev_err(&client->dev,
  528. "Failed to add sysfs cleanup action: %d\n",
  529. err);
  530. return err;
  531. }
  532. err = rtc_register_device(rtc);
  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");