da9150-core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*
  2. * DA9150 Core MFD Driver
  3. *
  4. * Copyright (c) 2014 Dialog Semiconductor
  5. *
  6. * Author: Adam Thomson <Adam.Thomson.Opensource@diasemi.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/i2c.h>
  17. #include <linux/regmap.h>
  18. #include <linux/slab.h>
  19. #include <linux/irq.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/mfd/core.h>
  22. #include <linux/mfd/da9150/core.h>
  23. #include <linux/mfd/da9150/registers.h>
  24. /* Raw device access, used for QIF */
  25. static int da9150_i2c_read_device(struct i2c_client *client, u8 addr, int count,
  26. u8 *buf)
  27. {
  28. struct i2c_msg xfer;
  29. int ret;
  30. /*
  31. * Read is split into two transfers as device expects STOP/START rather
  32. * than repeated start to carry out this kind of access.
  33. */
  34. /* Write address */
  35. xfer.addr = client->addr;
  36. xfer.flags = 0;
  37. xfer.len = 1;
  38. xfer.buf = &addr;
  39. ret = i2c_transfer(client->adapter, &xfer, 1);
  40. if (ret != 1) {
  41. if (ret < 0)
  42. return ret;
  43. else
  44. return -EIO;
  45. }
  46. /* Read data */
  47. xfer.addr = client->addr;
  48. xfer.flags = I2C_M_RD;
  49. xfer.len = count;
  50. xfer.buf = buf;
  51. ret = i2c_transfer(client->adapter, &xfer, 1);
  52. if (ret == 1)
  53. return 0;
  54. else if (ret < 0)
  55. return ret;
  56. else
  57. return -EIO;
  58. }
  59. static int da9150_i2c_write_device(struct i2c_client *client, u8 addr,
  60. int count, const u8 *buf)
  61. {
  62. struct i2c_msg xfer;
  63. u8 *reg_data;
  64. int ret;
  65. reg_data = kzalloc(1 + count, GFP_KERNEL);
  66. if (!reg_data)
  67. return -ENOMEM;
  68. reg_data[0] = addr;
  69. memcpy(&reg_data[1], buf, count);
  70. /* Write address & data */
  71. xfer.addr = client->addr;
  72. xfer.flags = 0;
  73. xfer.len = 1 + count;
  74. xfer.buf = reg_data;
  75. ret = i2c_transfer(client->adapter, &xfer, 1);
  76. kfree(reg_data);
  77. if (ret == 1)
  78. return 0;
  79. else if (ret < 0)
  80. return ret;
  81. else
  82. return -EIO;
  83. }
  84. static bool da9150_volatile_reg(struct device *dev, unsigned int reg)
  85. {
  86. switch (reg) {
  87. case DA9150_PAGE_CON:
  88. case DA9150_STATUS_A:
  89. case DA9150_STATUS_B:
  90. case DA9150_STATUS_C:
  91. case DA9150_STATUS_D:
  92. case DA9150_STATUS_E:
  93. case DA9150_STATUS_F:
  94. case DA9150_STATUS_G:
  95. case DA9150_STATUS_H:
  96. case DA9150_STATUS_I:
  97. case DA9150_STATUS_J:
  98. case DA9150_STATUS_K:
  99. case DA9150_STATUS_L:
  100. case DA9150_STATUS_N:
  101. case DA9150_FAULT_LOG_A:
  102. case DA9150_FAULT_LOG_B:
  103. case DA9150_EVENT_E:
  104. case DA9150_EVENT_F:
  105. case DA9150_EVENT_G:
  106. case DA9150_EVENT_H:
  107. case DA9150_CONTROL_B:
  108. case DA9150_CONTROL_C:
  109. case DA9150_GPADC_MAN:
  110. case DA9150_GPADC_RES_A:
  111. case DA9150_GPADC_RES_B:
  112. case DA9150_ADETVB_CFG_C:
  113. case DA9150_ADETD_STAT:
  114. case DA9150_ADET_CMPSTAT:
  115. case DA9150_ADET_CTRL_A:
  116. case DA9150_PPR_TCTR_B:
  117. case DA9150_COREBTLD_STAT_A:
  118. case DA9150_CORE_DATA_A:
  119. case DA9150_CORE_DATA_B:
  120. case DA9150_CORE_DATA_C:
  121. case DA9150_CORE_DATA_D:
  122. case DA9150_CORE2WIRE_STAT_A:
  123. case DA9150_FW_CTRL_C:
  124. case DA9150_FG_CTRL_B:
  125. case DA9150_FW_CTRL_B:
  126. case DA9150_GPADC_CMAN:
  127. case DA9150_GPADC_CRES_A:
  128. case DA9150_GPADC_CRES_B:
  129. case DA9150_CC_ICHG_RES_A:
  130. case DA9150_CC_ICHG_RES_B:
  131. case DA9150_CC_IAVG_RES_A:
  132. case DA9150_CC_IAVG_RES_B:
  133. case DA9150_TAUX_CTRL_A:
  134. case DA9150_TAUX_VALUE_H:
  135. case DA9150_TAUX_VALUE_L:
  136. case DA9150_TBAT_RES_A:
  137. case DA9150_TBAT_RES_B:
  138. return true;
  139. default:
  140. return false;
  141. }
  142. }
  143. static const struct regmap_range_cfg da9150_range_cfg[] = {
  144. {
  145. .range_min = DA9150_PAGE_CON,
  146. .range_max = DA9150_TBAT_RES_B,
  147. .selector_reg = DA9150_PAGE_CON,
  148. .selector_mask = DA9150_I2C_PAGE_MASK,
  149. .selector_shift = DA9150_I2C_PAGE_SHIFT,
  150. .window_start = 0,
  151. .window_len = 256,
  152. },
  153. };
  154. static const struct regmap_config da9150_regmap_config = {
  155. .reg_bits = 8,
  156. .val_bits = 8,
  157. .ranges = da9150_range_cfg,
  158. .num_ranges = ARRAY_SIZE(da9150_range_cfg),
  159. .max_register = DA9150_TBAT_RES_B,
  160. .cache_type = REGCACHE_RBTREE,
  161. .volatile_reg = da9150_volatile_reg,
  162. };
  163. void da9150_read_qif(struct da9150 *da9150, u8 addr, int count, u8 *buf)
  164. {
  165. int ret;
  166. ret = da9150_i2c_read_device(da9150->core_qif, addr, count, buf);
  167. if (ret < 0)
  168. dev_err(da9150->dev, "Failed to read from QIF 0x%x: %d\n",
  169. addr, ret);
  170. }
  171. EXPORT_SYMBOL_GPL(da9150_read_qif);
  172. void da9150_write_qif(struct da9150 *da9150, u8 addr, int count, const u8 *buf)
  173. {
  174. int ret;
  175. ret = da9150_i2c_write_device(da9150->core_qif, addr, count, buf);
  176. if (ret < 0)
  177. dev_err(da9150->dev, "Failed to write to QIF 0x%x: %d\n",
  178. addr, ret);
  179. }
  180. EXPORT_SYMBOL_GPL(da9150_write_qif);
  181. u8 da9150_reg_read(struct da9150 *da9150, u16 reg)
  182. {
  183. int val, ret;
  184. ret = regmap_read(da9150->regmap, reg, &val);
  185. if (ret)
  186. dev_err(da9150->dev, "Failed to read from reg 0x%x: %d\n",
  187. reg, ret);
  188. return (u8) val;
  189. }
  190. EXPORT_SYMBOL_GPL(da9150_reg_read);
  191. void da9150_reg_write(struct da9150 *da9150, u16 reg, u8 val)
  192. {
  193. int ret;
  194. ret = regmap_write(da9150->regmap, reg, val);
  195. if (ret)
  196. dev_err(da9150->dev, "Failed to write to reg 0x%x: %d\n",
  197. reg, ret);
  198. }
  199. EXPORT_SYMBOL_GPL(da9150_reg_write);
  200. void da9150_set_bits(struct da9150 *da9150, u16 reg, u8 mask, u8 val)
  201. {
  202. int ret;
  203. ret = regmap_update_bits(da9150->regmap, reg, mask, val);
  204. if (ret)
  205. dev_err(da9150->dev, "Failed to set bits in reg 0x%x: %d\n",
  206. reg, ret);
  207. }
  208. EXPORT_SYMBOL_GPL(da9150_set_bits);
  209. void da9150_bulk_read(struct da9150 *da9150, u16 reg, int count, u8 *buf)
  210. {
  211. int ret;
  212. ret = regmap_bulk_read(da9150->regmap, reg, buf, count);
  213. if (ret)
  214. dev_err(da9150->dev, "Failed to bulk read from reg 0x%x: %d\n",
  215. reg, ret);
  216. }
  217. EXPORT_SYMBOL_GPL(da9150_bulk_read);
  218. void da9150_bulk_write(struct da9150 *da9150, u16 reg, int count, const u8 *buf)
  219. {
  220. int ret;
  221. ret = regmap_raw_write(da9150->regmap, reg, buf, count);
  222. if (ret)
  223. dev_err(da9150->dev, "Failed to bulk write to reg 0x%x %d\n",
  224. reg, ret);
  225. }
  226. EXPORT_SYMBOL_GPL(da9150_bulk_write);
  227. static const struct regmap_irq da9150_irqs[] = {
  228. [DA9150_IRQ_VBUS] = {
  229. .reg_offset = 0,
  230. .mask = DA9150_E_VBUS_MASK,
  231. },
  232. [DA9150_IRQ_CHG] = {
  233. .reg_offset = 0,
  234. .mask = DA9150_E_CHG_MASK,
  235. },
  236. [DA9150_IRQ_TCLASS] = {
  237. .reg_offset = 0,
  238. .mask = DA9150_E_TCLASS_MASK,
  239. },
  240. [DA9150_IRQ_TJUNC] = {
  241. .reg_offset = 0,
  242. .mask = DA9150_E_TJUNC_MASK,
  243. },
  244. [DA9150_IRQ_VFAULT] = {
  245. .reg_offset = 0,
  246. .mask = DA9150_E_VFAULT_MASK,
  247. },
  248. [DA9150_IRQ_CONF] = {
  249. .reg_offset = 1,
  250. .mask = DA9150_E_CONF_MASK,
  251. },
  252. [DA9150_IRQ_DAT] = {
  253. .reg_offset = 1,
  254. .mask = DA9150_E_DAT_MASK,
  255. },
  256. [DA9150_IRQ_DTYPE] = {
  257. .reg_offset = 1,
  258. .mask = DA9150_E_DTYPE_MASK,
  259. },
  260. [DA9150_IRQ_ID] = {
  261. .reg_offset = 1,
  262. .mask = DA9150_E_ID_MASK,
  263. },
  264. [DA9150_IRQ_ADP] = {
  265. .reg_offset = 1,
  266. .mask = DA9150_E_ADP_MASK,
  267. },
  268. [DA9150_IRQ_SESS_END] = {
  269. .reg_offset = 1,
  270. .mask = DA9150_E_SESS_END_MASK,
  271. },
  272. [DA9150_IRQ_SESS_VLD] = {
  273. .reg_offset = 1,
  274. .mask = DA9150_E_SESS_VLD_MASK,
  275. },
  276. [DA9150_IRQ_FG] = {
  277. .reg_offset = 2,
  278. .mask = DA9150_E_FG_MASK,
  279. },
  280. [DA9150_IRQ_GP] = {
  281. .reg_offset = 2,
  282. .mask = DA9150_E_GP_MASK,
  283. },
  284. [DA9150_IRQ_TBAT] = {
  285. .reg_offset = 2,
  286. .mask = DA9150_E_TBAT_MASK,
  287. },
  288. [DA9150_IRQ_GPIOA] = {
  289. .reg_offset = 2,
  290. .mask = DA9150_E_GPIOA_MASK,
  291. },
  292. [DA9150_IRQ_GPIOB] = {
  293. .reg_offset = 2,
  294. .mask = DA9150_E_GPIOB_MASK,
  295. },
  296. [DA9150_IRQ_GPIOC] = {
  297. .reg_offset = 2,
  298. .mask = DA9150_E_GPIOC_MASK,
  299. },
  300. [DA9150_IRQ_GPIOD] = {
  301. .reg_offset = 2,
  302. .mask = DA9150_E_GPIOD_MASK,
  303. },
  304. [DA9150_IRQ_GPADC] = {
  305. .reg_offset = 2,
  306. .mask = DA9150_E_GPADC_MASK,
  307. },
  308. [DA9150_IRQ_WKUP] = {
  309. .reg_offset = 3,
  310. .mask = DA9150_E_WKUP_MASK,
  311. },
  312. };
  313. static const struct regmap_irq_chip da9150_regmap_irq_chip = {
  314. .name = "da9150_irq",
  315. .status_base = DA9150_EVENT_E,
  316. .mask_base = DA9150_IRQ_MASK_E,
  317. .ack_base = DA9150_EVENT_E,
  318. .num_regs = DA9150_NUM_IRQ_REGS,
  319. .irqs = da9150_irqs,
  320. .num_irqs = ARRAY_SIZE(da9150_irqs),
  321. };
  322. static struct resource da9150_gpadc_resources[] = {
  323. DEFINE_RES_IRQ_NAMED(DA9150_IRQ_GPADC, "GPADC"),
  324. };
  325. static struct resource da9150_charger_resources[] = {
  326. DEFINE_RES_IRQ_NAMED(DA9150_IRQ_CHG, "CHG_STATUS"),
  327. DEFINE_RES_IRQ_NAMED(DA9150_IRQ_TJUNC, "CHG_TJUNC"),
  328. DEFINE_RES_IRQ_NAMED(DA9150_IRQ_VFAULT, "CHG_VFAULT"),
  329. DEFINE_RES_IRQ_NAMED(DA9150_IRQ_VBUS, "CHG_VBUS"),
  330. };
  331. static struct resource da9150_fg_resources[] = {
  332. DEFINE_RES_IRQ_NAMED(DA9150_IRQ_FG, "FG"),
  333. };
  334. enum da9150_dev_idx {
  335. DA9150_GPADC_IDX = 0,
  336. DA9150_CHARGER_IDX,
  337. DA9150_FG_IDX,
  338. };
  339. static struct mfd_cell da9150_devs[] = {
  340. [DA9150_GPADC_IDX] = {
  341. .name = "da9150-gpadc",
  342. .of_compatible = "dlg,da9150-gpadc",
  343. .resources = da9150_gpadc_resources,
  344. .num_resources = ARRAY_SIZE(da9150_gpadc_resources),
  345. },
  346. [DA9150_CHARGER_IDX] = {
  347. .name = "da9150-charger",
  348. .of_compatible = "dlg,da9150-charger",
  349. .resources = da9150_charger_resources,
  350. .num_resources = ARRAY_SIZE(da9150_charger_resources),
  351. },
  352. [DA9150_FG_IDX] = {
  353. .name = "da9150-fuel-gauge",
  354. .of_compatible = "dlg,da9150-fuel-gauge",
  355. .resources = da9150_fg_resources,
  356. .num_resources = ARRAY_SIZE(da9150_fg_resources),
  357. },
  358. };
  359. static int da9150_probe(struct i2c_client *client,
  360. const struct i2c_device_id *id)
  361. {
  362. struct da9150 *da9150;
  363. struct da9150_pdata *pdata = dev_get_platdata(&client->dev);
  364. int qif_addr;
  365. int ret;
  366. da9150 = devm_kzalloc(&client->dev, sizeof(*da9150), GFP_KERNEL);
  367. if (!da9150)
  368. return -ENOMEM;
  369. da9150->dev = &client->dev;
  370. da9150->irq = client->irq;
  371. i2c_set_clientdata(client, da9150);
  372. da9150->regmap = devm_regmap_init_i2c(client, &da9150_regmap_config);
  373. if (IS_ERR(da9150->regmap)) {
  374. ret = PTR_ERR(da9150->regmap);
  375. dev_err(da9150->dev, "Failed to allocate register map: %d\n",
  376. ret);
  377. return ret;
  378. }
  379. /* Setup secondary I2C interface for QIF access */
  380. qif_addr = da9150_reg_read(da9150, DA9150_CORE2WIRE_CTRL_A);
  381. qif_addr = (qif_addr & DA9150_CORE_BASE_ADDR_MASK) >> 1;
  382. qif_addr |= DA9150_QIF_I2C_ADDR_LSB;
  383. da9150->core_qif = i2c_new_dummy(client->adapter, qif_addr);
  384. if (!da9150->core_qif) {
  385. dev_err(da9150->dev, "Failed to attach QIF client\n");
  386. return -ENODEV;
  387. }
  388. i2c_set_clientdata(da9150->core_qif, da9150);
  389. if (pdata) {
  390. da9150->irq_base = pdata->irq_base;
  391. da9150_devs[DA9150_FG_IDX].platform_data = pdata->fg_pdata;
  392. da9150_devs[DA9150_FG_IDX].pdata_size =
  393. sizeof(struct da9150_fg_pdata);
  394. } else {
  395. da9150->irq_base = -1;
  396. }
  397. ret = regmap_add_irq_chip(da9150->regmap, da9150->irq,
  398. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  399. da9150->irq_base, &da9150_regmap_irq_chip,
  400. &da9150->regmap_irq_data);
  401. if (ret) {
  402. dev_err(da9150->dev, "Failed to add regmap irq chip: %d\n",
  403. ret);
  404. goto regmap_irq_fail;
  405. }
  406. da9150->irq_base = regmap_irq_chip_get_base(da9150->regmap_irq_data);
  407. enable_irq_wake(da9150->irq);
  408. ret = mfd_add_devices(da9150->dev, -1, da9150_devs,
  409. ARRAY_SIZE(da9150_devs), NULL,
  410. da9150->irq_base, NULL);
  411. if (ret) {
  412. dev_err(da9150->dev, "Failed to add child devices: %d\n", ret);
  413. goto mfd_fail;
  414. }
  415. return 0;
  416. mfd_fail:
  417. regmap_del_irq_chip(da9150->irq, da9150->regmap_irq_data);
  418. regmap_irq_fail:
  419. i2c_unregister_device(da9150->core_qif);
  420. return ret;
  421. }
  422. static int da9150_remove(struct i2c_client *client)
  423. {
  424. struct da9150 *da9150 = i2c_get_clientdata(client);
  425. regmap_del_irq_chip(da9150->irq, da9150->regmap_irq_data);
  426. mfd_remove_devices(da9150->dev);
  427. i2c_unregister_device(da9150->core_qif);
  428. return 0;
  429. }
  430. static void da9150_shutdown(struct i2c_client *client)
  431. {
  432. struct da9150 *da9150 = i2c_get_clientdata(client);
  433. /* Make sure we have a wakup source for the device */
  434. da9150_set_bits(da9150, DA9150_CONFIG_D,
  435. DA9150_WKUP_PM_EN_MASK,
  436. DA9150_WKUP_PM_EN_MASK);
  437. /* Set device to DISABLED mode */
  438. da9150_set_bits(da9150, DA9150_CONTROL_C,
  439. DA9150_DISABLE_MASK, DA9150_DISABLE_MASK);
  440. }
  441. static const struct i2c_device_id da9150_i2c_id[] = {
  442. { "da9150", },
  443. { }
  444. };
  445. MODULE_DEVICE_TABLE(i2c, da9150_i2c_id);
  446. static const struct of_device_id da9150_of_match[] = {
  447. { .compatible = "dlg,da9150", },
  448. { }
  449. };
  450. MODULE_DEVICE_TABLE(of, da9150_of_match);
  451. static struct i2c_driver da9150_driver = {
  452. .driver = {
  453. .name = "da9150",
  454. .of_match_table = of_match_ptr(da9150_of_match),
  455. },
  456. .probe = da9150_probe,
  457. .remove = da9150_remove,
  458. .shutdown = da9150_shutdown,
  459. .id_table = da9150_i2c_id,
  460. };
  461. module_i2c_driver(da9150_driver);
  462. MODULE_DESCRIPTION("MFD Core Driver for DA9150");
  463. MODULE_AUTHOR("Adam Thomson <Adam.Thomson.Opensource@diasemi.com>");
  464. MODULE_LICENSE("GPL");