axp20x.c 20 KB

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