axp20x.c 22 KB

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