axp20x.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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. "AXP221",
  33. "AXP288",
  34. };
  35. static const struct regmap_range axp20x_writeable_ranges[] = {
  36. regmap_reg_range(AXP20X_DATACACHE(0), AXP20X_IRQ5_STATE),
  37. regmap_reg_range(AXP20X_DCDC_MODE, AXP20X_FG_RES),
  38. };
  39. static const struct regmap_range axp20x_volatile_ranges[] = {
  40. regmap_reg_range(AXP20X_IRQ1_EN, AXP20X_IRQ5_STATE),
  41. };
  42. static const struct regmap_access_table axp20x_writeable_table = {
  43. .yes_ranges = axp20x_writeable_ranges,
  44. .n_yes_ranges = ARRAY_SIZE(axp20x_writeable_ranges),
  45. };
  46. static const struct regmap_access_table axp20x_volatile_table = {
  47. .yes_ranges = axp20x_volatile_ranges,
  48. .n_yes_ranges = ARRAY_SIZE(axp20x_volatile_ranges),
  49. };
  50. static const struct regmap_range axp22x_writeable_ranges[] = {
  51. regmap_reg_range(AXP20X_DATACACHE(0), AXP20X_IRQ5_STATE),
  52. regmap_reg_range(AXP20X_DCDC_MODE, AXP22X_BATLOW_THRES1),
  53. };
  54. static const struct regmap_range axp22x_volatile_ranges[] = {
  55. regmap_reg_range(AXP20X_IRQ1_EN, AXP20X_IRQ5_STATE),
  56. };
  57. static const struct regmap_access_table axp22x_writeable_table = {
  58. .yes_ranges = axp22x_writeable_ranges,
  59. .n_yes_ranges = ARRAY_SIZE(axp22x_writeable_ranges),
  60. };
  61. static const struct regmap_access_table axp22x_volatile_table = {
  62. .yes_ranges = axp22x_volatile_ranges,
  63. .n_yes_ranges = ARRAY_SIZE(axp22x_volatile_ranges),
  64. };
  65. static const struct regmap_range axp288_writeable_ranges[] = {
  66. regmap_reg_range(AXP20X_DATACACHE(0), AXP20X_IRQ6_STATE),
  67. regmap_reg_range(AXP20X_DCDC_MODE, AXP288_FG_TUNE5),
  68. };
  69. static const struct regmap_range axp288_volatile_ranges[] = {
  70. regmap_reg_range(AXP20X_IRQ1_EN, AXP20X_IPSOUT_V_HIGH_L),
  71. };
  72. static const struct regmap_access_table axp288_writeable_table = {
  73. .yes_ranges = axp288_writeable_ranges,
  74. .n_yes_ranges = ARRAY_SIZE(axp288_writeable_ranges),
  75. };
  76. static const struct regmap_access_table axp288_volatile_table = {
  77. .yes_ranges = axp288_volatile_ranges,
  78. .n_yes_ranges = ARRAY_SIZE(axp288_volatile_ranges),
  79. };
  80. static struct resource axp20x_pek_resources[] = {
  81. {
  82. .name = "PEK_DBR",
  83. .start = AXP20X_IRQ_PEK_RIS_EDGE,
  84. .end = AXP20X_IRQ_PEK_RIS_EDGE,
  85. .flags = IORESOURCE_IRQ,
  86. }, {
  87. .name = "PEK_DBF",
  88. .start = AXP20X_IRQ_PEK_FAL_EDGE,
  89. .end = AXP20X_IRQ_PEK_FAL_EDGE,
  90. .flags = IORESOURCE_IRQ,
  91. },
  92. };
  93. static struct resource axp22x_pek_resources[] = {
  94. {
  95. .name = "PEK_DBR",
  96. .start = AXP22X_IRQ_PEK_RIS_EDGE,
  97. .end = AXP22X_IRQ_PEK_RIS_EDGE,
  98. .flags = IORESOURCE_IRQ,
  99. }, {
  100. .name = "PEK_DBF",
  101. .start = AXP22X_IRQ_PEK_FAL_EDGE,
  102. .end = AXP22X_IRQ_PEK_FAL_EDGE,
  103. .flags = IORESOURCE_IRQ,
  104. },
  105. };
  106. static struct resource axp288_fuel_gauge_resources[] = {
  107. {
  108. .start = AXP288_IRQ_QWBTU,
  109. .end = AXP288_IRQ_QWBTU,
  110. .flags = IORESOURCE_IRQ,
  111. },
  112. {
  113. .start = AXP288_IRQ_WBTU,
  114. .end = AXP288_IRQ_WBTU,
  115. .flags = IORESOURCE_IRQ,
  116. },
  117. {
  118. .start = AXP288_IRQ_QWBTO,
  119. .end = AXP288_IRQ_QWBTO,
  120. .flags = IORESOURCE_IRQ,
  121. },
  122. {
  123. .start = AXP288_IRQ_WBTO,
  124. .end = AXP288_IRQ_WBTO,
  125. .flags = IORESOURCE_IRQ,
  126. },
  127. {
  128. .start = AXP288_IRQ_WL2,
  129. .end = AXP288_IRQ_WL2,
  130. .flags = IORESOURCE_IRQ,
  131. },
  132. {
  133. .start = AXP288_IRQ_WL1,
  134. .end = AXP288_IRQ_WL1,
  135. .flags = IORESOURCE_IRQ,
  136. },
  137. };
  138. static const struct regmap_config axp20x_regmap_config = {
  139. .reg_bits = 8,
  140. .val_bits = 8,
  141. .wr_table = &axp20x_writeable_table,
  142. .volatile_table = &axp20x_volatile_table,
  143. .max_register = AXP20X_FG_RES,
  144. .cache_type = REGCACHE_RBTREE,
  145. };
  146. static const struct regmap_config axp22x_regmap_config = {
  147. .reg_bits = 8,
  148. .val_bits = 8,
  149. .wr_table = &axp22x_writeable_table,
  150. .volatile_table = &axp22x_volatile_table,
  151. .max_register = AXP22X_BATLOW_THRES1,
  152. .cache_type = REGCACHE_RBTREE,
  153. };
  154. static const struct regmap_config axp288_regmap_config = {
  155. .reg_bits = 8,
  156. .val_bits = 8,
  157. .wr_table = &axp288_writeable_table,
  158. .volatile_table = &axp288_volatile_table,
  159. .max_register = AXP288_FG_TUNE5,
  160. .cache_type = REGCACHE_RBTREE,
  161. };
  162. #define INIT_REGMAP_IRQ(_variant, _irq, _off, _mask) \
  163. [_variant##_IRQ_##_irq] = { .reg_offset = (_off), .mask = BIT(_mask) }
  164. static const struct regmap_irq axp20x_regmap_irqs[] = {
  165. INIT_REGMAP_IRQ(AXP20X, ACIN_OVER_V, 0, 7),
  166. INIT_REGMAP_IRQ(AXP20X, ACIN_PLUGIN, 0, 6),
  167. INIT_REGMAP_IRQ(AXP20X, ACIN_REMOVAL, 0, 5),
  168. INIT_REGMAP_IRQ(AXP20X, VBUS_OVER_V, 0, 4),
  169. INIT_REGMAP_IRQ(AXP20X, VBUS_PLUGIN, 0, 3),
  170. INIT_REGMAP_IRQ(AXP20X, VBUS_REMOVAL, 0, 2),
  171. INIT_REGMAP_IRQ(AXP20X, VBUS_V_LOW, 0, 1),
  172. INIT_REGMAP_IRQ(AXP20X, BATT_PLUGIN, 1, 7),
  173. INIT_REGMAP_IRQ(AXP20X, BATT_REMOVAL, 1, 6),
  174. INIT_REGMAP_IRQ(AXP20X, BATT_ENT_ACT_MODE, 1, 5),
  175. INIT_REGMAP_IRQ(AXP20X, BATT_EXIT_ACT_MODE, 1, 4),
  176. INIT_REGMAP_IRQ(AXP20X, CHARG, 1, 3),
  177. INIT_REGMAP_IRQ(AXP20X, CHARG_DONE, 1, 2),
  178. INIT_REGMAP_IRQ(AXP20X, BATT_TEMP_HIGH, 1, 1),
  179. INIT_REGMAP_IRQ(AXP20X, BATT_TEMP_LOW, 1, 0),
  180. INIT_REGMAP_IRQ(AXP20X, DIE_TEMP_HIGH, 2, 7),
  181. INIT_REGMAP_IRQ(AXP20X, CHARG_I_LOW, 2, 6),
  182. INIT_REGMAP_IRQ(AXP20X, DCDC1_V_LONG, 2, 5),
  183. INIT_REGMAP_IRQ(AXP20X, DCDC2_V_LONG, 2, 4),
  184. INIT_REGMAP_IRQ(AXP20X, DCDC3_V_LONG, 2, 3),
  185. INIT_REGMAP_IRQ(AXP20X, PEK_SHORT, 2, 1),
  186. INIT_REGMAP_IRQ(AXP20X, PEK_LONG, 2, 0),
  187. INIT_REGMAP_IRQ(AXP20X, N_OE_PWR_ON, 3, 7),
  188. INIT_REGMAP_IRQ(AXP20X, N_OE_PWR_OFF, 3, 6),
  189. INIT_REGMAP_IRQ(AXP20X, VBUS_VALID, 3, 5),
  190. INIT_REGMAP_IRQ(AXP20X, VBUS_NOT_VALID, 3, 4),
  191. INIT_REGMAP_IRQ(AXP20X, VBUS_SESS_VALID, 3, 3),
  192. INIT_REGMAP_IRQ(AXP20X, VBUS_SESS_END, 3, 2),
  193. INIT_REGMAP_IRQ(AXP20X, LOW_PWR_LVL1, 3, 1),
  194. INIT_REGMAP_IRQ(AXP20X, LOW_PWR_LVL2, 3, 0),
  195. INIT_REGMAP_IRQ(AXP20X, TIMER, 4, 7),
  196. INIT_REGMAP_IRQ(AXP20X, PEK_RIS_EDGE, 4, 6),
  197. INIT_REGMAP_IRQ(AXP20X, PEK_FAL_EDGE, 4, 5),
  198. INIT_REGMAP_IRQ(AXP20X, GPIO3_INPUT, 4, 3),
  199. INIT_REGMAP_IRQ(AXP20X, GPIO2_INPUT, 4, 2),
  200. INIT_REGMAP_IRQ(AXP20X, GPIO1_INPUT, 4, 1),
  201. INIT_REGMAP_IRQ(AXP20X, GPIO0_INPUT, 4, 0),
  202. };
  203. static const struct regmap_irq axp22x_regmap_irqs[] = {
  204. INIT_REGMAP_IRQ(AXP22X, ACIN_OVER_V, 0, 7),
  205. INIT_REGMAP_IRQ(AXP22X, ACIN_PLUGIN, 0, 6),
  206. INIT_REGMAP_IRQ(AXP22X, ACIN_REMOVAL, 0, 5),
  207. INIT_REGMAP_IRQ(AXP22X, VBUS_OVER_V, 0, 4),
  208. INIT_REGMAP_IRQ(AXP22X, VBUS_PLUGIN, 0, 3),
  209. INIT_REGMAP_IRQ(AXP22X, VBUS_REMOVAL, 0, 2),
  210. INIT_REGMAP_IRQ(AXP22X, VBUS_V_LOW, 0, 1),
  211. INIT_REGMAP_IRQ(AXP22X, BATT_PLUGIN, 1, 7),
  212. INIT_REGMAP_IRQ(AXP22X, BATT_REMOVAL, 1, 6),
  213. INIT_REGMAP_IRQ(AXP22X, BATT_ENT_ACT_MODE, 1, 5),
  214. INIT_REGMAP_IRQ(AXP22X, BATT_EXIT_ACT_MODE, 1, 4),
  215. INIT_REGMAP_IRQ(AXP22X, CHARG, 1, 3),
  216. INIT_REGMAP_IRQ(AXP22X, CHARG_DONE, 1, 2),
  217. INIT_REGMAP_IRQ(AXP22X, BATT_TEMP_HIGH, 1, 1),
  218. INIT_REGMAP_IRQ(AXP22X, BATT_TEMP_LOW, 1, 0),
  219. INIT_REGMAP_IRQ(AXP22X, DIE_TEMP_HIGH, 2, 7),
  220. INIT_REGMAP_IRQ(AXP22X, PEK_SHORT, 2, 1),
  221. INIT_REGMAP_IRQ(AXP22X, PEK_LONG, 2, 0),
  222. INIT_REGMAP_IRQ(AXP22X, LOW_PWR_LVL1, 3, 1),
  223. INIT_REGMAP_IRQ(AXP22X, LOW_PWR_LVL2, 3, 0),
  224. INIT_REGMAP_IRQ(AXP22X, TIMER, 4, 7),
  225. INIT_REGMAP_IRQ(AXP22X, PEK_RIS_EDGE, 4, 6),
  226. INIT_REGMAP_IRQ(AXP22X, PEK_FAL_EDGE, 4, 5),
  227. INIT_REGMAP_IRQ(AXP22X, GPIO1_INPUT, 4, 1),
  228. INIT_REGMAP_IRQ(AXP22X, GPIO0_INPUT, 4, 0),
  229. };
  230. /* some IRQs are compatible with axp20x models */
  231. static const struct regmap_irq axp288_regmap_irqs[] = {
  232. INIT_REGMAP_IRQ(AXP288, VBUS_FALL, 0, 2),
  233. INIT_REGMAP_IRQ(AXP288, VBUS_RISE, 0, 3),
  234. INIT_REGMAP_IRQ(AXP288, OV, 0, 4),
  235. INIT_REGMAP_IRQ(AXP288, DONE, 1, 2),
  236. INIT_REGMAP_IRQ(AXP288, CHARGING, 1, 3),
  237. INIT_REGMAP_IRQ(AXP288, SAFE_QUIT, 1, 4),
  238. INIT_REGMAP_IRQ(AXP288, SAFE_ENTER, 1, 5),
  239. INIT_REGMAP_IRQ(AXP288, ABSENT, 1, 6),
  240. INIT_REGMAP_IRQ(AXP288, APPEND, 1, 7),
  241. INIT_REGMAP_IRQ(AXP288, QWBTU, 2, 0),
  242. INIT_REGMAP_IRQ(AXP288, WBTU, 2, 1),
  243. INIT_REGMAP_IRQ(AXP288, QWBTO, 2, 2),
  244. INIT_REGMAP_IRQ(AXP288, WBTO, 2, 3),
  245. INIT_REGMAP_IRQ(AXP288, QCBTU, 2, 4),
  246. INIT_REGMAP_IRQ(AXP288, CBTU, 2, 5),
  247. INIT_REGMAP_IRQ(AXP288, QCBTO, 2, 6),
  248. INIT_REGMAP_IRQ(AXP288, CBTO, 2, 7),
  249. INIT_REGMAP_IRQ(AXP288, WL2, 3, 0),
  250. INIT_REGMAP_IRQ(AXP288, WL1, 3, 1),
  251. INIT_REGMAP_IRQ(AXP288, GPADC, 3, 2),
  252. INIT_REGMAP_IRQ(AXP288, OT, 3, 7),
  253. INIT_REGMAP_IRQ(AXP288, GPIO0, 4, 0),
  254. INIT_REGMAP_IRQ(AXP288, GPIO1, 4, 1),
  255. INIT_REGMAP_IRQ(AXP288, POKO, 4, 2),
  256. INIT_REGMAP_IRQ(AXP288, POKL, 4, 3),
  257. INIT_REGMAP_IRQ(AXP288, POKS, 4, 4),
  258. INIT_REGMAP_IRQ(AXP288, POKN, 4, 5),
  259. INIT_REGMAP_IRQ(AXP288, POKP, 4, 6),
  260. INIT_REGMAP_IRQ(AXP288, TIMER, 4, 7),
  261. INIT_REGMAP_IRQ(AXP288, MV_CHNG, 5, 0),
  262. INIT_REGMAP_IRQ(AXP288, BC_USB_CHNG, 5, 1),
  263. };
  264. static const struct of_device_id axp20x_of_match[] = {
  265. { .compatible = "x-powers,axp202", .data = (void *) AXP202_ID },
  266. { .compatible = "x-powers,axp209", .data = (void *) AXP209_ID },
  267. { .compatible = "x-powers,axp221", .data = (void *) AXP221_ID },
  268. { },
  269. };
  270. MODULE_DEVICE_TABLE(of, axp20x_of_match);
  271. /*
  272. * This is useless for OF-enabled devices, but it is needed by I2C subsystem
  273. */
  274. static const struct i2c_device_id axp20x_i2c_id[] = {
  275. { },
  276. };
  277. MODULE_DEVICE_TABLE(i2c, axp20x_i2c_id);
  278. static const struct acpi_device_id axp20x_acpi_match[] = {
  279. {
  280. .id = "INT33F4",
  281. .driver_data = AXP288_ID,
  282. },
  283. { },
  284. };
  285. MODULE_DEVICE_TABLE(acpi, axp20x_acpi_match);
  286. static const struct regmap_irq_chip axp20x_regmap_irq_chip = {
  287. .name = "axp20x_irq_chip",
  288. .status_base = AXP20X_IRQ1_STATE,
  289. .ack_base = AXP20X_IRQ1_STATE,
  290. .mask_base = AXP20X_IRQ1_EN,
  291. .mask_invert = true,
  292. .init_ack_masked = true,
  293. .irqs = axp20x_regmap_irqs,
  294. .num_irqs = ARRAY_SIZE(axp20x_regmap_irqs),
  295. .num_regs = 5,
  296. };
  297. static const struct regmap_irq_chip axp22x_regmap_irq_chip = {
  298. .name = "axp22x_irq_chip",
  299. .status_base = AXP20X_IRQ1_STATE,
  300. .ack_base = AXP20X_IRQ1_STATE,
  301. .mask_base = AXP20X_IRQ1_EN,
  302. .mask_invert = true,
  303. .init_ack_masked = true,
  304. .irqs = axp22x_regmap_irqs,
  305. .num_irqs = ARRAY_SIZE(axp22x_regmap_irqs),
  306. .num_regs = 5,
  307. };
  308. static const struct regmap_irq_chip axp288_regmap_irq_chip = {
  309. .name = "axp288_irq_chip",
  310. .status_base = AXP20X_IRQ1_STATE,
  311. .ack_base = AXP20X_IRQ1_STATE,
  312. .mask_base = AXP20X_IRQ1_EN,
  313. .mask_invert = true,
  314. .init_ack_masked = true,
  315. .irqs = axp288_regmap_irqs,
  316. .num_irqs = ARRAY_SIZE(axp288_regmap_irqs),
  317. .num_regs = 6,
  318. };
  319. static struct mfd_cell axp20x_cells[] = {
  320. {
  321. .name = "axp20x-pek",
  322. .num_resources = ARRAY_SIZE(axp20x_pek_resources),
  323. .resources = axp20x_pek_resources,
  324. }, {
  325. .name = "axp20x-regulator",
  326. },
  327. };
  328. static struct mfd_cell axp22x_cells[] = {
  329. {
  330. .name = "axp20x-pek",
  331. .num_resources = ARRAY_SIZE(axp22x_pek_resources),
  332. .resources = axp22x_pek_resources,
  333. }, {
  334. .name = "axp20x-regulator",
  335. },
  336. };
  337. static struct resource axp288_adc_resources[] = {
  338. {
  339. .name = "GPADC",
  340. .start = AXP288_IRQ_GPADC,
  341. .end = AXP288_IRQ_GPADC,
  342. .flags = IORESOURCE_IRQ,
  343. },
  344. };
  345. static struct resource axp288_extcon_resources[] = {
  346. {
  347. .start = AXP288_IRQ_VBUS_FALL,
  348. .end = AXP288_IRQ_VBUS_FALL,
  349. .flags = IORESOURCE_IRQ,
  350. },
  351. {
  352. .start = AXP288_IRQ_VBUS_RISE,
  353. .end = AXP288_IRQ_VBUS_RISE,
  354. .flags = IORESOURCE_IRQ,
  355. },
  356. {
  357. .start = AXP288_IRQ_MV_CHNG,
  358. .end = AXP288_IRQ_MV_CHNG,
  359. .flags = IORESOURCE_IRQ,
  360. },
  361. {
  362. .start = AXP288_IRQ_BC_USB_CHNG,
  363. .end = AXP288_IRQ_BC_USB_CHNG,
  364. .flags = IORESOURCE_IRQ,
  365. },
  366. };
  367. static struct resource axp288_charger_resources[] = {
  368. {
  369. .start = AXP288_IRQ_OV,
  370. .end = AXP288_IRQ_OV,
  371. .flags = IORESOURCE_IRQ,
  372. },
  373. {
  374. .start = AXP288_IRQ_DONE,
  375. .end = AXP288_IRQ_DONE,
  376. .flags = IORESOURCE_IRQ,
  377. },
  378. {
  379. .start = AXP288_IRQ_CHARGING,
  380. .end = AXP288_IRQ_CHARGING,
  381. .flags = IORESOURCE_IRQ,
  382. },
  383. {
  384. .start = AXP288_IRQ_SAFE_QUIT,
  385. .end = AXP288_IRQ_SAFE_QUIT,
  386. .flags = IORESOURCE_IRQ,
  387. },
  388. {
  389. .start = AXP288_IRQ_SAFE_ENTER,
  390. .end = AXP288_IRQ_SAFE_ENTER,
  391. .flags = IORESOURCE_IRQ,
  392. },
  393. {
  394. .start = AXP288_IRQ_QCBTU,
  395. .end = AXP288_IRQ_QCBTU,
  396. .flags = IORESOURCE_IRQ,
  397. },
  398. {
  399. .start = AXP288_IRQ_CBTU,
  400. .end = AXP288_IRQ_CBTU,
  401. .flags = IORESOURCE_IRQ,
  402. },
  403. {
  404. .start = AXP288_IRQ_QCBTO,
  405. .end = AXP288_IRQ_QCBTO,
  406. .flags = IORESOURCE_IRQ,
  407. },
  408. {
  409. .start = AXP288_IRQ_CBTO,
  410. .end = AXP288_IRQ_CBTO,
  411. .flags = IORESOURCE_IRQ,
  412. },
  413. };
  414. static struct mfd_cell axp288_cells[] = {
  415. {
  416. .name = "axp288_adc",
  417. .num_resources = ARRAY_SIZE(axp288_adc_resources),
  418. .resources = axp288_adc_resources,
  419. },
  420. {
  421. .name = "axp288_extcon",
  422. .num_resources = ARRAY_SIZE(axp288_extcon_resources),
  423. .resources = axp288_extcon_resources,
  424. },
  425. {
  426. .name = "axp288_charger",
  427. .num_resources = ARRAY_SIZE(axp288_charger_resources),
  428. .resources = axp288_charger_resources,
  429. },
  430. {
  431. .name = "axp288_fuel_gauge",
  432. .num_resources = ARRAY_SIZE(axp288_fuel_gauge_resources),
  433. .resources = axp288_fuel_gauge_resources,
  434. },
  435. {
  436. .name = "axp288_pmic_acpi",
  437. },
  438. };
  439. static struct axp20x_dev *axp20x_pm_power_off;
  440. static void axp20x_power_off(void)
  441. {
  442. if (axp20x_pm_power_off->variant == AXP288_ID)
  443. return;
  444. regmap_write(axp20x_pm_power_off->regmap, AXP20X_OFF_CTRL,
  445. AXP20X_OFF);
  446. }
  447. static int axp20x_match_device(struct axp20x_dev *axp20x, struct device *dev)
  448. {
  449. const struct acpi_device_id *acpi_id;
  450. const struct of_device_id *of_id;
  451. if (dev->of_node) {
  452. of_id = of_match_device(axp20x_of_match, dev);
  453. if (!of_id) {
  454. dev_err(dev, "Unable to match OF ID\n");
  455. return -ENODEV;
  456. }
  457. axp20x->variant = (long) of_id->data;
  458. } else {
  459. acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev);
  460. if (!acpi_id || !acpi_id->driver_data) {
  461. dev_err(dev, "Unable to match ACPI ID and data\n");
  462. return -ENODEV;
  463. }
  464. axp20x->variant = (long) acpi_id->driver_data;
  465. }
  466. switch (axp20x->variant) {
  467. case AXP202_ID:
  468. case AXP209_ID:
  469. axp20x->nr_cells = ARRAY_SIZE(axp20x_cells);
  470. axp20x->cells = axp20x_cells;
  471. axp20x->regmap_cfg = &axp20x_regmap_config;
  472. axp20x->regmap_irq_chip = &axp20x_regmap_irq_chip;
  473. break;
  474. case AXP221_ID:
  475. axp20x->nr_cells = ARRAY_SIZE(axp22x_cells);
  476. axp20x->cells = axp22x_cells;
  477. axp20x->regmap_cfg = &axp22x_regmap_config;
  478. axp20x->regmap_irq_chip = &axp22x_regmap_irq_chip;
  479. break;
  480. case AXP288_ID:
  481. axp20x->cells = axp288_cells;
  482. axp20x->nr_cells = ARRAY_SIZE(axp288_cells);
  483. axp20x->regmap_cfg = &axp288_regmap_config;
  484. axp20x->regmap_irq_chip = &axp288_regmap_irq_chip;
  485. break;
  486. default:
  487. dev_err(dev, "unsupported AXP20X ID %lu\n", axp20x->variant);
  488. return -EINVAL;
  489. }
  490. dev_info(dev, "AXP20x variant %s found\n",
  491. axp20x_model_names[axp20x->variant]);
  492. return 0;
  493. }
  494. static int axp20x_i2c_probe(struct i2c_client *i2c,
  495. const struct i2c_device_id *id)
  496. {
  497. struct axp20x_dev *axp20x;
  498. int ret;
  499. axp20x = devm_kzalloc(&i2c->dev, sizeof(*axp20x), GFP_KERNEL);
  500. if (!axp20x)
  501. return -ENOMEM;
  502. ret = axp20x_match_device(axp20x, &i2c->dev);
  503. if (ret)
  504. return ret;
  505. axp20x->i2c_client = i2c;
  506. axp20x->dev = &i2c->dev;
  507. dev_set_drvdata(axp20x->dev, axp20x);
  508. axp20x->regmap = devm_regmap_init_i2c(i2c, axp20x->regmap_cfg);
  509. if (IS_ERR(axp20x->regmap)) {
  510. ret = PTR_ERR(axp20x->regmap);
  511. dev_err(&i2c->dev, "regmap init failed: %d\n", ret);
  512. return ret;
  513. }
  514. ret = regmap_add_irq_chip(axp20x->regmap, i2c->irq,
  515. IRQF_ONESHOT | IRQF_SHARED, -1,
  516. axp20x->regmap_irq_chip,
  517. &axp20x->regmap_irqc);
  518. if (ret) {
  519. dev_err(&i2c->dev, "failed to add irq chip: %d\n", ret);
  520. return ret;
  521. }
  522. ret = mfd_add_devices(axp20x->dev, -1, axp20x->cells,
  523. axp20x->nr_cells, NULL, 0, NULL);
  524. if (ret) {
  525. dev_err(&i2c->dev, "failed to add MFD devices: %d\n", ret);
  526. regmap_del_irq_chip(i2c->irq, axp20x->regmap_irqc);
  527. return ret;
  528. }
  529. if (!pm_power_off) {
  530. axp20x_pm_power_off = axp20x;
  531. pm_power_off = axp20x_power_off;
  532. }
  533. dev_info(&i2c->dev, "AXP20X driver loaded\n");
  534. return 0;
  535. }
  536. static int axp20x_i2c_remove(struct i2c_client *i2c)
  537. {
  538. struct axp20x_dev *axp20x = i2c_get_clientdata(i2c);
  539. if (axp20x == axp20x_pm_power_off) {
  540. axp20x_pm_power_off = NULL;
  541. pm_power_off = NULL;
  542. }
  543. mfd_remove_devices(axp20x->dev);
  544. regmap_del_irq_chip(axp20x->i2c_client->irq, axp20x->regmap_irqc);
  545. return 0;
  546. }
  547. static struct i2c_driver axp20x_i2c_driver = {
  548. .driver = {
  549. .name = "axp20x",
  550. .owner = THIS_MODULE,
  551. .of_match_table = of_match_ptr(axp20x_of_match),
  552. .acpi_match_table = ACPI_PTR(axp20x_acpi_match),
  553. },
  554. .probe = axp20x_i2c_probe,
  555. .remove = axp20x_i2c_remove,
  556. .id_table = axp20x_i2c_id,
  557. };
  558. module_i2c_driver(axp20x_i2c_driver);
  559. MODULE_DESCRIPTION("PMIC MFD core driver for AXP20X");
  560. MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
  561. MODULE_LICENSE("GPL");