axp20x.c 21 KB

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