axp20x.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * axp20x.c - MFD core driver for the X-Powers' Power Management ICs
  3. *
  4. * AXP20x typically comprises an adaptive USB-Compatible PWM charger, BUCK DC-DC
  5. * converters, LDOs, multiple 12-bit ADCs of voltage, current and temperature
  6. * as well as configurable GPIOs.
  7. *
  8. * Author: Carlo Caione <carlo@caione.org>
  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. #include <linux/err.h>
  15. #include <linux/i2c.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/regmap.h>
  21. #include <linux/slab.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/mfd/axp20x.h>
  24. #include <linux/mfd/core.h>
  25. #include <linux/of_device.h>
  26. #include <linux/of_irq.h>
  27. #include <linux/acpi.h>
  28. #define AXP20X_OFF 0x80
  29. static const char const *axp20x_model_names[] = {
  30. "AXP202",
  31. "AXP209",
  32. "AXP288",
  33. };
  34. static const struct regmap_range axp20x_writeable_ranges[] = {
  35. regmap_reg_range(AXP20X_DATACACHE(0), AXP20X_IRQ5_STATE),
  36. regmap_reg_range(AXP20X_DCDC_MODE, AXP20X_FG_RES),
  37. };
  38. static const struct regmap_range axp20x_volatile_ranges[] = {
  39. regmap_reg_range(AXP20X_IRQ1_EN, AXP20X_IRQ5_STATE),
  40. };
  41. static const struct regmap_access_table axp20x_writeable_table = {
  42. .yes_ranges = axp20x_writeable_ranges,
  43. .n_yes_ranges = ARRAY_SIZE(axp20x_writeable_ranges),
  44. };
  45. static const struct regmap_access_table axp20x_volatile_table = {
  46. .yes_ranges = axp20x_volatile_ranges,
  47. .n_yes_ranges = ARRAY_SIZE(axp20x_volatile_ranges),
  48. };
  49. static const struct regmap_range axp288_writeable_ranges[] = {
  50. regmap_reg_range(AXP20X_DATACACHE(0), AXP20X_IRQ6_STATE),
  51. regmap_reg_range(AXP20X_DCDC_MODE, AXP288_FG_TUNE5),
  52. };
  53. static const struct regmap_range axp288_volatile_ranges[] = {
  54. regmap_reg_range(AXP20X_IRQ1_EN, AXP20X_IPSOUT_V_HIGH_L),
  55. };
  56. static const struct regmap_access_table axp288_writeable_table = {
  57. .yes_ranges = axp288_writeable_ranges,
  58. .n_yes_ranges = ARRAY_SIZE(axp288_writeable_ranges),
  59. };
  60. static const struct regmap_access_table axp288_volatile_table = {
  61. .yes_ranges = axp288_volatile_ranges,
  62. .n_yes_ranges = ARRAY_SIZE(axp288_volatile_ranges),
  63. };
  64. static struct resource axp20x_pek_resources[] = {
  65. {
  66. .name = "PEK_DBR",
  67. .start = AXP20X_IRQ_PEK_RIS_EDGE,
  68. .end = AXP20X_IRQ_PEK_RIS_EDGE,
  69. .flags = IORESOURCE_IRQ,
  70. }, {
  71. .name = "PEK_DBF",
  72. .start = AXP20X_IRQ_PEK_FAL_EDGE,
  73. .end = AXP20X_IRQ_PEK_FAL_EDGE,
  74. .flags = IORESOURCE_IRQ,
  75. },
  76. };
  77. static struct resource axp288_battery_resources[] = {
  78. {
  79. .start = AXP288_IRQ_QWBTU,
  80. .end = AXP288_IRQ_QWBTU,
  81. .flags = IORESOURCE_IRQ,
  82. },
  83. {
  84. .start = AXP288_IRQ_WBTU,
  85. .end = AXP288_IRQ_WBTU,
  86. .flags = IORESOURCE_IRQ,
  87. },
  88. {
  89. .start = AXP288_IRQ_QWBTO,
  90. .end = AXP288_IRQ_QWBTO,
  91. .flags = IORESOURCE_IRQ,
  92. },
  93. {
  94. .start = AXP288_IRQ_WBTO,
  95. .end = AXP288_IRQ_WBTO,
  96. .flags = IORESOURCE_IRQ,
  97. },
  98. {
  99. .start = AXP288_IRQ_WL2,
  100. .end = AXP288_IRQ_WL2,
  101. .flags = IORESOURCE_IRQ,
  102. },
  103. {
  104. .start = AXP288_IRQ_WL1,
  105. .end = AXP288_IRQ_WL1,
  106. .flags = IORESOURCE_IRQ,
  107. },
  108. };
  109. static const struct regmap_config axp20x_regmap_config = {
  110. .reg_bits = 8,
  111. .val_bits = 8,
  112. .wr_table = &axp20x_writeable_table,
  113. .volatile_table = &axp20x_volatile_table,
  114. .max_register = AXP20X_FG_RES,
  115. .cache_type = REGCACHE_RBTREE,
  116. };
  117. static const struct regmap_config axp288_regmap_config = {
  118. .reg_bits = 8,
  119. .val_bits = 8,
  120. .wr_table = &axp288_writeable_table,
  121. .volatile_table = &axp288_volatile_table,
  122. .max_register = AXP288_FG_TUNE5,
  123. .cache_type = REGCACHE_RBTREE,
  124. };
  125. #define INIT_REGMAP_IRQ(_variant, _irq, _off, _mask) \
  126. [_variant##_IRQ_##_irq] = { .reg_offset = (_off), .mask = BIT(_mask) }
  127. static const struct regmap_irq axp20x_regmap_irqs[] = {
  128. INIT_REGMAP_IRQ(AXP20X, ACIN_OVER_V, 0, 7),
  129. INIT_REGMAP_IRQ(AXP20X, ACIN_PLUGIN, 0, 6),
  130. INIT_REGMAP_IRQ(AXP20X, ACIN_REMOVAL, 0, 5),
  131. INIT_REGMAP_IRQ(AXP20X, VBUS_OVER_V, 0, 4),
  132. INIT_REGMAP_IRQ(AXP20X, VBUS_PLUGIN, 0, 3),
  133. INIT_REGMAP_IRQ(AXP20X, VBUS_REMOVAL, 0, 2),
  134. INIT_REGMAP_IRQ(AXP20X, VBUS_V_LOW, 0, 1),
  135. INIT_REGMAP_IRQ(AXP20X, BATT_PLUGIN, 1, 7),
  136. INIT_REGMAP_IRQ(AXP20X, BATT_REMOVAL, 1, 6),
  137. INIT_REGMAP_IRQ(AXP20X, BATT_ENT_ACT_MODE, 1, 5),
  138. INIT_REGMAP_IRQ(AXP20X, BATT_EXIT_ACT_MODE, 1, 4),
  139. INIT_REGMAP_IRQ(AXP20X, CHARG, 1, 3),
  140. INIT_REGMAP_IRQ(AXP20X, CHARG_DONE, 1, 2),
  141. INIT_REGMAP_IRQ(AXP20X, BATT_TEMP_HIGH, 1, 1),
  142. INIT_REGMAP_IRQ(AXP20X, BATT_TEMP_LOW, 1, 0),
  143. INIT_REGMAP_IRQ(AXP20X, DIE_TEMP_HIGH, 2, 7),
  144. INIT_REGMAP_IRQ(AXP20X, CHARG_I_LOW, 2, 6),
  145. INIT_REGMAP_IRQ(AXP20X, DCDC1_V_LONG, 2, 5),
  146. INIT_REGMAP_IRQ(AXP20X, DCDC2_V_LONG, 2, 4),
  147. INIT_REGMAP_IRQ(AXP20X, DCDC3_V_LONG, 2, 3),
  148. INIT_REGMAP_IRQ(AXP20X, PEK_SHORT, 2, 1),
  149. INIT_REGMAP_IRQ(AXP20X, PEK_LONG, 2, 0),
  150. INIT_REGMAP_IRQ(AXP20X, N_OE_PWR_ON, 3, 7),
  151. INIT_REGMAP_IRQ(AXP20X, N_OE_PWR_OFF, 3, 6),
  152. INIT_REGMAP_IRQ(AXP20X, VBUS_VALID, 3, 5),
  153. INIT_REGMAP_IRQ(AXP20X, VBUS_NOT_VALID, 3, 4),
  154. INIT_REGMAP_IRQ(AXP20X, VBUS_SESS_VALID, 3, 3),
  155. INIT_REGMAP_IRQ(AXP20X, VBUS_SESS_END, 3, 2),
  156. INIT_REGMAP_IRQ(AXP20X, LOW_PWR_LVL1, 3, 1),
  157. INIT_REGMAP_IRQ(AXP20X, LOW_PWR_LVL2, 3, 0),
  158. INIT_REGMAP_IRQ(AXP20X, TIMER, 4, 7),
  159. INIT_REGMAP_IRQ(AXP20X, PEK_RIS_EDGE, 4, 6),
  160. INIT_REGMAP_IRQ(AXP20X, PEK_FAL_EDGE, 4, 5),
  161. INIT_REGMAP_IRQ(AXP20X, GPIO3_INPUT, 4, 3),
  162. INIT_REGMAP_IRQ(AXP20X, GPIO2_INPUT, 4, 2),
  163. INIT_REGMAP_IRQ(AXP20X, GPIO1_INPUT, 4, 1),
  164. INIT_REGMAP_IRQ(AXP20X, GPIO0_INPUT, 4, 0),
  165. };
  166. /* some IRQs are compatible with axp20x models */
  167. static const struct regmap_irq axp288_regmap_irqs[] = {
  168. INIT_REGMAP_IRQ(AXP288, VBUS_FALL, 0, 2),
  169. INIT_REGMAP_IRQ(AXP288, VBUS_RISE, 0, 3),
  170. INIT_REGMAP_IRQ(AXP288, OV, 0, 4),
  171. INIT_REGMAP_IRQ(AXP288, DONE, 1, 2),
  172. INIT_REGMAP_IRQ(AXP288, CHARGING, 1, 3),
  173. INIT_REGMAP_IRQ(AXP288, SAFE_QUIT, 1, 4),
  174. INIT_REGMAP_IRQ(AXP288, SAFE_ENTER, 1, 5),
  175. INIT_REGMAP_IRQ(AXP288, ABSENT, 1, 6),
  176. INIT_REGMAP_IRQ(AXP288, APPEND, 1, 7),
  177. INIT_REGMAP_IRQ(AXP288, QWBTU, 2, 0),
  178. INIT_REGMAP_IRQ(AXP288, WBTU, 2, 1),
  179. INIT_REGMAP_IRQ(AXP288, QWBTO, 2, 2),
  180. INIT_REGMAP_IRQ(AXP288, WBTO, 2, 3),
  181. INIT_REGMAP_IRQ(AXP288, QCBTU, 2, 4),
  182. INIT_REGMAP_IRQ(AXP288, CBTU, 2, 5),
  183. INIT_REGMAP_IRQ(AXP288, QCBTO, 2, 6),
  184. INIT_REGMAP_IRQ(AXP288, CBTO, 2, 7),
  185. INIT_REGMAP_IRQ(AXP288, WL2, 3, 0),
  186. INIT_REGMAP_IRQ(AXP288, WL1, 3, 1),
  187. INIT_REGMAP_IRQ(AXP288, GPADC, 3, 2),
  188. INIT_REGMAP_IRQ(AXP288, OT, 3, 7),
  189. INIT_REGMAP_IRQ(AXP288, GPIO0, 4, 0),
  190. INIT_REGMAP_IRQ(AXP288, GPIO1, 4, 1),
  191. INIT_REGMAP_IRQ(AXP288, POKO, 4, 2),
  192. INIT_REGMAP_IRQ(AXP288, POKL, 4, 3),
  193. INIT_REGMAP_IRQ(AXP288, POKS, 4, 4),
  194. INIT_REGMAP_IRQ(AXP288, POKN, 4, 5),
  195. INIT_REGMAP_IRQ(AXP288, POKP, 4, 6),
  196. INIT_REGMAP_IRQ(AXP288, TIMER, 4, 7),
  197. INIT_REGMAP_IRQ(AXP288, MV_CHNG, 5, 0),
  198. INIT_REGMAP_IRQ(AXP288, BC_USB_CHNG, 5, 1),
  199. };
  200. static const struct of_device_id axp20x_of_match[] = {
  201. { .compatible = "x-powers,axp202", .data = (void *) AXP202_ID },
  202. { .compatible = "x-powers,axp209", .data = (void *) AXP209_ID },
  203. { },
  204. };
  205. MODULE_DEVICE_TABLE(of, axp20x_of_match);
  206. /*
  207. * This is useless for OF-enabled devices, but it is needed by I2C subsystem
  208. */
  209. static const struct i2c_device_id axp20x_i2c_id[] = {
  210. { },
  211. };
  212. MODULE_DEVICE_TABLE(i2c, axp20x_i2c_id);
  213. static const struct acpi_device_id axp20x_acpi_match[] = {
  214. {
  215. .id = "INT33F4",
  216. .driver_data = AXP288_ID,
  217. },
  218. { },
  219. };
  220. MODULE_DEVICE_TABLE(acpi, axp20x_acpi_match);
  221. static const struct regmap_irq_chip axp20x_regmap_irq_chip = {
  222. .name = "axp20x_irq_chip",
  223. .status_base = AXP20X_IRQ1_STATE,
  224. .ack_base = AXP20X_IRQ1_STATE,
  225. .mask_base = AXP20X_IRQ1_EN,
  226. .mask_invert = true,
  227. .init_ack_masked = true,
  228. .irqs = axp20x_regmap_irqs,
  229. .num_irqs = ARRAY_SIZE(axp20x_regmap_irqs),
  230. .num_regs = 5,
  231. };
  232. static const struct regmap_irq_chip axp288_regmap_irq_chip = {
  233. .name = "axp288_irq_chip",
  234. .status_base = AXP20X_IRQ1_STATE,
  235. .ack_base = AXP20X_IRQ1_STATE,
  236. .mask_base = AXP20X_IRQ1_EN,
  237. .mask_invert = true,
  238. .init_ack_masked = true,
  239. .irqs = axp288_regmap_irqs,
  240. .num_irqs = ARRAY_SIZE(axp288_regmap_irqs),
  241. .num_regs = 6,
  242. };
  243. static struct mfd_cell axp20x_cells[] = {
  244. {
  245. .name = "axp20x-pek",
  246. .num_resources = ARRAY_SIZE(axp20x_pek_resources),
  247. .resources = axp20x_pek_resources,
  248. }, {
  249. .name = "axp20x-regulator",
  250. },
  251. };
  252. static struct resource axp288_adc_resources[] = {
  253. {
  254. .name = "GPADC",
  255. .start = AXP288_IRQ_GPADC,
  256. .end = AXP288_IRQ_GPADC,
  257. .flags = IORESOURCE_IRQ,
  258. },
  259. };
  260. static struct resource axp288_charger_resources[] = {
  261. {
  262. .start = AXP288_IRQ_OV,
  263. .end = AXP288_IRQ_OV,
  264. .flags = IORESOURCE_IRQ,
  265. },
  266. {
  267. .start = AXP288_IRQ_DONE,
  268. .end = AXP288_IRQ_DONE,
  269. .flags = IORESOURCE_IRQ,
  270. },
  271. {
  272. .start = AXP288_IRQ_CHARGING,
  273. .end = AXP288_IRQ_CHARGING,
  274. .flags = IORESOURCE_IRQ,
  275. },
  276. {
  277. .start = AXP288_IRQ_SAFE_QUIT,
  278. .end = AXP288_IRQ_SAFE_QUIT,
  279. .flags = IORESOURCE_IRQ,
  280. },
  281. {
  282. .start = AXP288_IRQ_SAFE_ENTER,
  283. .end = AXP288_IRQ_SAFE_ENTER,
  284. .flags = IORESOURCE_IRQ,
  285. },
  286. {
  287. .start = AXP288_IRQ_QCBTU,
  288. .end = AXP288_IRQ_QCBTU,
  289. .flags = IORESOURCE_IRQ,
  290. },
  291. {
  292. .start = AXP288_IRQ_CBTU,
  293. .end = AXP288_IRQ_CBTU,
  294. .flags = IORESOURCE_IRQ,
  295. },
  296. {
  297. .start = AXP288_IRQ_QCBTO,
  298. .end = AXP288_IRQ_QCBTO,
  299. .flags = IORESOURCE_IRQ,
  300. },
  301. {
  302. .start = AXP288_IRQ_CBTO,
  303. .end = AXP288_IRQ_CBTO,
  304. .flags = IORESOURCE_IRQ,
  305. },
  306. };
  307. static struct mfd_cell axp288_cells[] = {
  308. {
  309. .name = "axp288_adc",
  310. .num_resources = ARRAY_SIZE(axp288_adc_resources),
  311. .resources = axp288_adc_resources,
  312. },
  313. {
  314. .name = "axp288_charger",
  315. .num_resources = ARRAY_SIZE(axp288_charger_resources),
  316. .resources = axp288_charger_resources,
  317. },
  318. {
  319. .name = "axp288_battery",
  320. .num_resources = ARRAY_SIZE(axp288_battery_resources),
  321. .resources = axp288_battery_resources,
  322. },
  323. {
  324. .name = "axp288_pmic_acpi",
  325. },
  326. };
  327. static struct axp20x_dev *axp20x_pm_power_off;
  328. static void axp20x_power_off(void)
  329. {
  330. if (axp20x_pm_power_off->variant == AXP288_ID)
  331. return;
  332. regmap_write(axp20x_pm_power_off->regmap, AXP20X_OFF_CTRL,
  333. AXP20X_OFF);
  334. }
  335. static int axp20x_match_device(struct axp20x_dev *axp20x, struct device *dev)
  336. {
  337. const struct acpi_device_id *acpi_id;
  338. const struct of_device_id *of_id;
  339. if (dev->of_node) {
  340. of_id = of_match_device(axp20x_of_match, dev);
  341. if (!of_id) {
  342. dev_err(dev, "Unable to match OF ID\n");
  343. return -ENODEV;
  344. }
  345. axp20x->variant = (long) of_id->data;
  346. } else {
  347. acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev);
  348. if (!acpi_id || !acpi_id->driver_data) {
  349. dev_err(dev, "Unable to match ACPI ID and data\n");
  350. return -ENODEV;
  351. }
  352. axp20x->variant = (long) acpi_id->driver_data;
  353. }
  354. switch (axp20x->variant) {
  355. case AXP202_ID:
  356. case AXP209_ID:
  357. axp20x->nr_cells = ARRAY_SIZE(axp20x_cells);
  358. axp20x->cells = axp20x_cells;
  359. axp20x->regmap_cfg = &axp20x_regmap_config;
  360. axp20x->regmap_irq_chip = &axp20x_regmap_irq_chip;
  361. break;
  362. case AXP288_ID:
  363. axp20x->cells = axp288_cells;
  364. axp20x->nr_cells = ARRAY_SIZE(axp288_cells);
  365. axp20x->regmap_cfg = &axp288_regmap_config;
  366. axp20x->regmap_irq_chip = &axp288_regmap_irq_chip;
  367. break;
  368. default:
  369. dev_err(dev, "unsupported AXP20X ID %lu\n", axp20x->variant);
  370. return -EINVAL;
  371. }
  372. dev_info(dev, "AXP20x variant %s found\n",
  373. axp20x_model_names[axp20x->variant]);
  374. return 0;
  375. }
  376. static int axp20x_i2c_probe(struct i2c_client *i2c,
  377. const struct i2c_device_id *id)
  378. {
  379. struct axp20x_dev *axp20x;
  380. int ret;
  381. axp20x = devm_kzalloc(&i2c->dev, sizeof(*axp20x), GFP_KERNEL);
  382. if (!axp20x)
  383. return -ENOMEM;
  384. ret = axp20x_match_device(axp20x, &i2c->dev);
  385. if (ret)
  386. return ret;
  387. axp20x->i2c_client = i2c;
  388. axp20x->dev = &i2c->dev;
  389. dev_set_drvdata(axp20x->dev, axp20x);
  390. axp20x->regmap = devm_regmap_init_i2c(i2c, axp20x->regmap_cfg);
  391. if (IS_ERR(axp20x->regmap)) {
  392. ret = PTR_ERR(axp20x->regmap);
  393. dev_err(&i2c->dev, "regmap init failed: %d\n", ret);
  394. return ret;
  395. }
  396. ret = regmap_add_irq_chip(axp20x->regmap, i2c->irq,
  397. IRQF_ONESHOT | IRQF_SHARED, -1,
  398. axp20x->regmap_irq_chip,
  399. &axp20x->regmap_irqc);
  400. if (ret) {
  401. dev_err(&i2c->dev, "failed to add irq chip: %d\n", ret);
  402. return ret;
  403. }
  404. ret = mfd_add_devices(axp20x->dev, -1, axp20x->cells,
  405. axp20x->nr_cells, NULL, 0, NULL);
  406. if (ret) {
  407. dev_err(&i2c->dev, "failed to add MFD devices: %d\n", ret);
  408. regmap_del_irq_chip(i2c->irq, axp20x->regmap_irqc);
  409. return ret;
  410. }
  411. if (!pm_power_off) {
  412. axp20x_pm_power_off = axp20x;
  413. pm_power_off = axp20x_power_off;
  414. }
  415. dev_info(&i2c->dev, "AXP20X driver loaded\n");
  416. return 0;
  417. }
  418. static int axp20x_i2c_remove(struct i2c_client *i2c)
  419. {
  420. struct axp20x_dev *axp20x = i2c_get_clientdata(i2c);
  421. if (axp20x == axp20x_pm_power_off) {
  422. axp20x_pm_power_off = NULL;
  423. pm_power_off = NULL;
  424. }
  425. mfd_remove_devices(axp20x->dev);
  426. regmap_del_irq_chip(axp20x->i2c_client->irq, axp20x->regmap_irqc);
  427. return 0;
  428. }
  429. static struct i2c_driver axp20x_i2c_driver = {
  430. .driver = {
  431. .name = "axp20x",
  432. .owner = THIS_MODULE,
  433. .of_match_table = of_match_ptr(axp20x_of_match),
  434. .acpi_match_table = ACPI_PTR(axp20x_acpi_match),
  435. },
  436. .probe = axp20x_i2c_probe,
  437. .remove = axp20x_i2c_remove,
  438. .id_table = axp20x_i2c_id,
  439. };
  440. module_i2c_driver(axp20x_i2c_driver);
  441. MODULE_DESCRIPTION("PMIC MFD core driver for AXP20X");
  442. MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
  443. MODULE_LICENSE("GPL");