stpmic1_regulator.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) STMicroelectronics 2018
  3. // Author: Pascal Paillet <p.paillet@st.com> for STMicroelectronics.
  4. #include <linux/interrupt.h>
  5. #include <linux/mfd/stpmic1.h>
  6. #include <linux/module.h>
  7. #include <linux/of_irq.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/regmap.h>
  10. #include <linux/regulator/driver.h>
  11. #include <linux/regulator/machine.h>
  12. #include <linux/regulator/of_regulator.h>
  13. #include <dt-bindings/mfd/st,stpmic1.h>
  14. /**
  15. * stpmic1 regulator description: this structure is used as driver data
  16. * @desc: regulator framework description
  17. * @mask_reset_reg: mask reset register address
  18. * @mask_reset_mask: mask rank and mask reset register mask
  19. * @icc_reg: icc register address
  20. * @icc_mask: icc register mask
  21. */
  22. struct stpmic1_regulator_cfg {
  23. struct regulator_desc desc;
  24. u8 mask_reset_reg;
  25. u8 mask_reset_mask;
  26. u8 icc_reg;
  27. u8 icc_mask;
  28. };
  29. static int stpmic1_set_mode(struct regulator_dev *rdev, unsigned int mode);
  30. static unsigned int stpmic1_get_mode(struct regulator_dev *rdev);
  31. static int stpmic1_set_icc(struct regulator_dev *rdev);
  32. static unsigned int stpmic1_map_mode(unsigned int mode);
  33. enum {
  34. STPMIC1_BUCK1 = 0,
  35. STPMIC1_BUCK2 = 1,
  36. STPMIC1_BUCK3 = 2,
  37. STPMIC1_BUCK4 = 3,
  38. STPMIC1_LDO1 = 4,
  39. STPMIC1_LDO2 = 5,
  40. STPMIC1_LDO3 = 6,
  41. STPMIC1_LDO4 = 7,
  42. STPMIC1_LDO5 = 8,
  43. STPMIC1_LDO6 = 9,
  44. STPMIC1_VREF_DDR = 10,
  45. STPMIC1_BOOST = 11,
  46. STPMIC1_VBUS_OTG = 12,
  47. STPMIC1_SW_OUT = 13,
  48. };
  49. /* Enable time worst case is 5000mV/(2250uV/uS) */
  50. #define PMIC_ENABLE_TIME_US 2200
  51. static const struct regulator_linear_range buck1_ranges[] = {
  52. REGULATOR_LINEAR_RANGE(725000, 0, 4, 0),
  53. REGULATOR_LINEAR_RANGE(725000, 5, 36, 25000),
  54. REGULATOR_LINEAR_RANGE(1500000, 37, 63, 0),
  55. };
  56. static const struct regulator_linear_range buck2_ranges[] = {
  57. REGULATOR_LINEAR_RANGE(1000000, 0, 17, 0),
  58. REGULATOR_LINEAR_RANGE(1050000, 18, 19, 0),
  59. REGULATOR_LINEAR_RANGE(1100000, 20, 21, 0),
  60. REGULATOR_LINEAR_RANGE(1150000, 22, 23, 0),
  61. REGULATOR_LINEAR_RANGE(1200000, 24, 25, 0),
  62. REGULATOR_LINEAR_RANGE(1250000, 26, 27, 0),
  63. REGULATOR_LINEAR_RANGE(1300000, 28, 29, 0),
  64. REGULATOR_LINEAR_RANGE(1350000, 30, 31, 0),
  65. REGULATOR_LINEAR_RANGE(1400000, 32, 33, 0),
  66. REGULATOR_LINEAR_RANGE(1450000, 34, 35, 0),
  67. REGULATOR_LINEAR_RANGE(1500000, 36, 63, 0),
  68. };
  69. static const struct regulator_linear_range buck3_ranges[] = {
  70. REGULATOR_LINEAR_RANGE(1000000, 0, 19, 0),
  71. REGULATOR_LINEAR_RANGE(1100000, 20, 23, 0),
  72. REGULATOR_LINEAR_RANGE(1200000, 24, 27, 0),
  73. REGULATOR_LINEAR_RANGE(1300000, 28, 31, 0),
  74. REGULATOR_LINEAR_RANGE(1400000, 32, 35, 0),
  75. REGULATOR_LINEAR_RANGE(1500000, 36, 55, 100000),
  76. REGULATOR_LINEAR_RANGE(3400000, 56, 63, 0),
  77. };
  78. static const struct regulator_linear_range buck4_ranges[] = {
  79. REGULATOR_LINEAR_RANGE(600000, 0, 27, 25000),
  80. REGULATOR_LINEAR_RANGE(1300000, 28, 29, 0),
  81. REGULATOR_LINEAR_RANGE(1350000, 30, 31, 0),
  82. REGULATOR_LINEAR_RANGE(1400000, 32, 33, 0),
  83. REGULATOR_LINEAR_RANGE(1450000, 34, 35, 0),
  84. REGULATOR_LINEAR_RANGE(1500000, 36, 60, 100000),
  85. REGULATOR_LINEAR_RANGE(3900000, 61, 63, 0),
  86. };
  87. static const struct regulator_linear_range ldo1_ranges[] = {
  88. REGULATOR_LINEAR_RANGE(1700000, 0, 7, 0),
  89. REGULATOR_LINEAR_RANGE(1700000, 8, 24, 100000),
  90. REGULATOR_LINEAR_RANGE(3300000, 25, 31, 0),
  91. };
  92. static const struct regulator_linear_range ldo2_ranges[] = {
  93. REGULATOR_LINEAR_RANGE(1700000, 0, 7, 0),
  94. REGULATOR_LINEAR_RANGE(1700000, 8, 24, 100000),
  95. REGULATOR_LINEAR_RANGE(3300000, 25, 30, 0),
  96. };
  97. static const struct regulator_linear_range ldo3_ranges[] = {
  98. REGULATOR_LINEAR_RANGE(1700000, 0, 7, 0),
  99. REGULATOR_LINEAR_RANGE(1700000, 8, 24, 100000),
  100. REGULATOR_LINEAR_RANGE(3300000, 25, 30, 0),
  101. /* with index 31 LDO3 is in DDR mode */
  102. REGULATOR_LINEAR_RANGE(500000, 31, 31, 0),
  103. };
  104. static const struct regulator_linear_range ldo5_ranges[] = {
  105. REGULATOR_LINEAR_RANGE(1700000, 0, 7, 0),
  106. REGULATOR_LINEAR_RANGE(1700000, 8, 30, 100000),
  107. REGULATOR_LINEAR_RANGE(3900000, 31, 31, 0),
  108. };
  109. static const struct regulator_linear_range ldo6_ranges[] = {
  110. REGULATOR_LINEAR_RANGE(900000, 0, 24, 100000),
  111. REGULATOR_LINEAR_RANGE(3300000, 25, 31, 0),
  112. };
  113. static const struct regulator_ops stpmic1_ldo_ops = {
  114. .list_voltage = regulator_list_voltage_linear_range,
  115. .map_voltage = regulator_map_voltage_linear_range,
  116. .is_enabled = regulator_is_enabled_regmap,
  117. .enable = regulator_enable_regmap,
  118. .disable = regulator_disable_regmap,
  119. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  120. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  121. .set_over_current_protection = stpmic1_set_icc,
  122. };
  123. static const struct regulator_ops stpmic1_ldo3_ops = {
  124. .list_voltage = regulator_list_voltage_linear_range,
  125. .map_voltage = regulator_map_voltage_iterate,
  126. .is_enabled = regulator_is_enabled_regmap,
  127. .enable = regulator_enable_regmap,
  128. .disable = regulator_disable_regmap,
  129. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  130. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  131. .get_bypass = regulator_get_bypass_regmap,
  132. .set_bypass = regulator_set_bypass_regmap,
  133. .set_over_current_protection = stpmic1_set_icc,
  134. };
  135. static const struct regulator_ops stpmic1_ldo4_fixed_regul_ops = {
  136. .is_enabled = regulator_is_enabled_regmap,
  137. .enable = regulator_enable_regmap,
  138. .disable = regulator_disable_regmap,
  139. .set_over_current_protection = stpmic1_set_icc,
  140. };
  141. static const struct regulator_ops stpmic1_buck_ops = {
  142. .list_voltage = regulator_list_voltage_linear_range,
  143. .map_voltage = regulator_map_voltage_linear_range,
  144. .is_enabled = regulator_is_enabled_regmap,
  145. .enable = regulator_enable_regmap,
  146. .disable = regulator_disable_regmap,
  147. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  148. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  149. .set_pull_down = regulator_set_pull_down_regmap,
  150. .set_mode = stpmic1_set_mode,
  151. .get_mode = stpmic1_get_mode,
  152. .set_over_current_protection = stpmic1_set_icc,
  153. };
  154. static const struct regulator_ops stpmic1_vref_ddr_ops = {
  155. .is_enabled = regulator_is_enabled_regmap,
  156. .enable = regulator_enable_regmap,
  157. .disable = regulator_disable_regmap,
  158. };
  159. static const struct regulator_ops stpmic1_boost_regul_ops = {
  160. .is_enabled = regulator_is_enabled_regmap,
  161. .enable = regulator_enable_regmap,
  162. .disable = regulator_disable_regmap,
  163. .set_over_current_protection = stpmic1_set_icc,
  164. };
  165. static const struct regulator_ops stpmic1_switch_regul_ops = {
  166. .is_enabled = regulator_is_enabled_regmap,
  167. .enable = regulator_enable_regmap,
  168. .disable = regulator_disable_regmap,
  169. .set_over_current_protection = stpmic1_set_icc,
  170. .set_active_discharge = regulator_set_active_discharge_regmap,
  171. };
  172. #define REG_LDO(ids, base) { \
  173. .name = #ids, \
  174. .id = STPMIC1_##ids, \
  175. .n_voltages = 32, \
  176. .ops = &stpmic1_ldo_ops, \
  177. .linear_ranges = base ## _ranges, \
  178. .n_linear_ranges = ARRAY_SIZE(base ## _ranges), \
  179. .type = REGULATOR_VOLTAGE, \
  180. .owner = THIS_MODULE, \
  181. .vsel_reg = ids##_ACTIVE_CR, \
  182. .vsel_mask = LDO_VOLTAGE_MASK, \
  183. .enable_reg = ids##_ACTIVE_CR, \
  184. .enable_mask = LDO_ENABLE_MASK, \
  185. .enable_val = 1, \
  186. .disable_val = 0, \
  187. .enable_time = PMIC_ENABLE_TIME_US, \
  188. .supply_name = #base, \
  189. }
  190. #define REG_LDO3(ids, base) { \
  191. .name = #ids, \
  192. .id = STPMIC1_##ids, \
  193. .n_voltages = 32, \
  194. .ops = &stpmic1_ldo3_ops, \
  195. .linear_ranges = ldo3_ranges, \
  196. .n_linear_ranges = ARRAY_SIZE(ldo3_ranges), \
  197. .type = REGULATOR_VOLTAGE, \
  198. .owner = THIS_MODULE, \
  199. .vsel_reg = LDO3_ACTIVE_CR, \
  200. .vsel_mask = LDO_VOLTAGE_MASK, \
  201. .enable_reg = LDO3_ACTIVE_CR, \
  202. .enable_mask = LDO_ENABLE_MASK, \
  203. .enable_val = 1, \
  204. .disable_val = 0, \
  205. .enable_time = PMIC_ENABLE_TIME_US, \
  206. .bypass_reg = LDO3_ACTIVE_CR, \
  207. .bypass_mask = LDO_BYPASS_MASK, \
  208. .bypass_val_on = LDO_BYPASS_MASK, \
  209. .bypass_val_off = 0, \
  210. .supply_name = #base, \
  211. }
  212. #define REG_LDO4(ids, base) { \
  213. .name = #ids, \
  214. .id = STPMIC1_##ids, \
  215. .n_voltages = 1, \
  216. .ops = &stpmic1_ldo4_fixed_regul_ops, \
  217. .type = REGULATOR_VOLTAGE, \
  218. .owner = THIS_MODULE, \
  219. .min_uV = 3300000, \
  220. .fixed_uV = 3300000, \
  221. .enable_reg = LDO4_ACTIVE_CR, \
  222. .enable_mask = LDO_ENABLE_MASK, \
  223. .enable_val = 1, \
  224. .disable_val = 0, \
  225. .enable_time = PMIC_ENABLE_TIME_US, \
  226. .supply_name = #base, \
  227. }
  228. #define REG_BUCK(ids, base) { \
  229. .name = #ids, \
  230. .id = STPMIC1_##ids, \
  231. .ops = &stpmic1_buck_ops, \
  232. .n_voltages = 64, \
  233. .linear_ranges = base ## _ranges, \
  234. .n_linear_ranges = ARRAY_SIZE(base ## _ranges), \
  235. .type = REGULATOR_VOLTAGE, \
  236. .owner = THIS_MODULE, \
  237. .vsel_reg = ids##_ACTIVE_CR, \
  238. .vsel_mask = BUCK_VOLTAGE_MASK, \
  239. .enable_reg = ids##_ACTIVE_CR, \
  240. .enable_mask = BUCK_ENABLE_MASK, \
  241. .enable_val = 1, \
  242. .disable_val = 0, \
  243. .enable_time = PMIC_ENABLE_TIME_US, \
  244. .of_map_mode = stpmic1_map_mode, \
  245. .pull_down_reg = ids##_PULL_DOWN_REG, \
  246. .pull_down_mask = ids##_PULL_DOWN_MASK, \
  247. .supply_name = #base, \
  248. }
  249. #define REG_VREF_DDR(ids, base) { \
  250. .name = #ids, \
  251. .id = STPMIC1_##ids, \
  252. .n_voltages = 1, \
  253. .ops = &stpmic1_vref_ddr_ops, \
  254. .type = REGULATOR_VOLTAGE, \
  255. .owner = THIS_MODULE, \
  256. .min_uV = 500000, \
  257. .fixed_uV = 500000, \
  258. .enable_reg = VREF_DDR_ACTIVE_CR, \
  259. .enable_mask = BUCK_ENABLE_MASK, \
  260. .enable_val = 1, \
  261. .disable_val = 0, \
  262. .enable_time = PMIC_ENABLE_TIME_US, \
  263. .supply_name = #base, \
  264. }
  265. #define REG_BOOST(ids, base) { \
  266. .name = #ids, \
  267. .id = STPMIC1_##ids, \
  268. .n_voltages = 1, \
  269. .ops = &stpmic1_boost_regul_ops, \
  270. .type = REGULATOR_VOLTAGE, \
  271. .owner = THIS_MODULE, \
  272. .min_uV = 0, \
  273. .fixed_uV = 5000000, \
  274. .enable_reg = BST_SW_CR, \
  275. .enable_mask = BOOST_ENABLED, \
  276. .enable_val = BOOST_ENABLED, \
  277. .disable_val = 0, \
  278. .enable_time = PMIC_ENABLE_TIME_US, \
  279. .supply_name = #base, \
  280. }
  281. #define REG_VBUS_OTG(ids, base) { \
  282. .name = #ids, \
  283. .id = STPMIC1_##ids, \
  284. .n_voltages = 1, \
  285. .ops = &stpmic1_switch_regul_ops, \
  286. .type = REGULATOR_VOLTAGE, \
  287. .owner = THIS_MODULE, \
  288. .min_uV = 0, \
  289. .fixed_uV = 5000000, \
  290. .enable_reg = BST_SW_CR, \
  291. .enable_mask = USBSW_OTG_SWITCH_ENABLED, \
  292. .enable_val = USBSW_OTG_SWITCH_ENABLED, \
  293. .disable_val = 0, \
  294. .enable_time = PMIC_ENABLE_TIME_US, \
  295. .supply_name = #base, \
  296. .active_discharge_reg = BST_SW_CR, \
  297. .active_discharge_mask = VBUS_OTG_DISCHARGE, \
  298. .active_discharge_on = VBUS_OTG_DISCHARGE, \
  299. }
  300. #define REG_SW_OUT(ids, base) { \
  301. .name = #ids, \
  302. .id = STPMIC1_##ids, \
  303. .n_voltages = 1, \
  304. .ops = &stpmic1_switch_regul_ops, \
  305. .type = REGULATOR_VOLTAGE, \
  306. .owner = THIS_MODULE, \
  307. .min_uV = 0, \
  308. .fixed_uV = 5000000, \
  309. .enable_reg = BST_SW_CR, \
  310. .enable_mask = SWIN_SWOUT_ENABLED, \
  311. .enable_val = SWIN_SWOUT_ENABLED, \
  312. .disable_val = 0, \
  313. .enable_time = PMIC_ENABLE_TIME_US, \
  314. .supply_name = #base, \
  315. .active_discharge_reg = BST_SW_CR, \
  316. .active_discharge_mask = SW_OUT_DISCHARGE, \
  317. .active_discharge_on = SW_OUT_DISCHARGE, \
  318. }
  319. static const struct stpmic1_regulator_cfg stpmic1_regulator_cfgs[] = {
  320. [STPMIC1_BUCK1] = {
  321. .desc = REG_BUCK(BUCK1, buck1),
  322. .icc_reg = BUCKS_ICCTO_CR,
  323. .icc_mask = BIT(0),
  324. .mask_reset_reg = BUCKS_MASK_RESET_CR,
  325. .mask_reset_mask = BIT(0),
  326. },
  327. [STPMIC1_BUCK2] = {
  328. .desc = REG_BUCK(BUCK2, buck2),
  329. .icc_reg = BUCKS_ICCTO_CR,
  330. .icc_mask = BIT(1),
  331. .mask_reset_reg = BUCKS_MASK_RESET_CR,
  332. .mask_reset_mask = BIT(1),
  333. },
  334. [STPMIC1_BUCK3] = {
  335. .desc = REG_BUCK(BUCK3, buck3),
  336. .icc_reg = BUCKS_ICCTO_CR,
  337. .icc_mask = BIT(2),
  338. .mask_reset_reg = BUCKS_MASK_RESET_CR,
  339. .mask_reset_mask = BIT(2),
  340. },
  341. [STPMIC1_BUCK4] = {
  342. .desc = REG_BUCK(BUCK4, buck4),
  343. .icc_reg = BUCKS_ICCTO_CR,
  344. .icc_mask = BIT(3),
  345. .mask_reset_reg = BUCKS_MASK_RESET_CR,
  346. .mask_reset_mask = BIT(3),
  347. },
  348. [STPMIC1_LDO1] = {
  349. .desc = REG_LDO(LDO1, ldo1),
  350. .icc_reg = LDOS_ICCTO_CR,
  351. .icc_mask = BIT(0),
  352. .mask_reset_reg = LDOS_MASK_RESET_CR,
  353. .mask_reset_mask = BIT(0),
  354. },
  355. [STPMIC1_LDO2] = {
  356. .desc = REG_LDO(LDO2, ldo2),
  357. .icc_reg = LDOS_ICCTO_CR,
  358. .icc_mask = BIT(1),
  359. .mask_reset_reg = LDOS_MASK_RESET_CR,
  360. .mask_reset_mask = BIT(1),
  361. },
  362. [STPMIC1_LDO3] = {
  363. .desc = REG_LDO3(LDO3, ldo3),
  364. .icc_reg = LDOS_ICCTO_CR,
  365. .icc_mask = BIT(2),
  366. .mask_reset_reg = LDOS_MASK_RESET_CR,
  367. .mask_reset_mask = BIT(2),
  368. },
  369. [STPMIC1_LDO4] = {
  370. .desc = REG_LDO4(LDO4, ldo4),
  371. .icc_reg = LDOS_ICCTO_CR,
  372. .icc_mask = BIT(3),
  373. .mask_reset_reg = LDOS_MASK_RESET_CR,
  374. .mask_reset_mask = BIT(3),
  375. },
  376. [STPMIC1_LDO5] = {
  377. .desc = REG_LDO(LDO5, ldo5),
  378. .icc_reg = LDOS_ICCTO_CR,
  379. .icc_mask = BIT(4),
  380. .mask_reset_reg = LDOS_MASK_RESET_CR,
  381. .mask_reset_mask = BIT(4),
  382. },
  383. [STPMIC1_LDO6] = {
  384. .desc = REG_LDO(LDO6, ldo6),
  385. .icc_reg = LDOS_ICCTO_CR,
  386. .icc_mask = BIT(5),
  387. .mask_reset_reg = LDOS_MASK_RESET_CR,
  388. .mask_reset_mask = BIT(5),
  389. },
  390. [STPMIC1_VREF_DDR] = {
  391. .desc = REG_VREF_DDR(VREF_DDR, vref_ddr),
  392. .mask_reset_reg = LDOS_MASK_RESET_CR,
  393. .mask_reset_mask = BIT(6),
  394. },
  395. [STPMIC1_BOOST] = {
  396. .desc = REG_BOOST(BOOST, boost),
  397. .icc_reg = BUCKS_ICCTO_CR,
  398. .icc_mask = BIT(6),
  399. },
  400. [STPMIC1_VBUS_OTG] = {
  401. .desc = REG_VBUS_OTG(VBUS_OTG, pwr_sw1),
  402. .icc_reg = BUCKS_ICCTO_CR,
  403. .icc_mask = BIT(4),
  404. },
  405. [STPMIC1_SW_OUT] = {
  406. .desc = REG_SW_OUT(SW_OUT, pwr_sw2),
  407. .icc_reg = BUCKS_ICCTO_CR,
  408. .icc_mask = BIT(5),
  409. },
  410. };
  411. static unsigned int stpmic1_map_mode(unsigned int mode)
  412. {
  413. switch (mode) {
  414. case STPMIC1_BUCK_MODE_NORMAL:
  415. return REGULATOR_MODE_NORMAL;
  416. case STPMIC1_BUCK_MODE_LP:
  417. return REGULATOR_MODE_STANDBY;
  418. default:
  419. return REGULATOR_MODE_INVALID;
  420. }
  421. }
  422. static unsigned int stpmic1_get_mode(struct regulator_dev *rdev)
  423. {
  424. int value;
  425. struct regmap *regmap = rdev_get_regmap(rdev);
  426. regmap_read(regmap, rdev->desc->enable_reg, &value);
  427. if (value & STPMIC1_BUCK_MODE_LP)
  428. return REGULATOR_MODE_STANDBY;
  429. return REGULATOR_MODE_NORMAL;
  430. }
  431. static int stpmic1_set_mode(struct regulator_dev *rdev, unsigned int mode)
  432. {
  433. int value;
  434. struct regmap *regmap = rdev_get_regmap(rdev);
  435. switch (mode) {
  436. case REGULATOR_MODE_NORMAL:
  437. value = STPMIC1_BUCK_MODE_NORMAL;
  438. break;
  439. case REGULATOR_MODE_STANDBY:
  440. value = STPMIC1_BUCK_MODE_LP;
  441. break;
  442. default:
  443. return -EINVAL;
  444. }
  445. return regmap_update_bits(regmap, rdev->desc->enable_reg,
  446. STPMIC1_BUCK_MODE_LP, value);
  447. }
  448. static int stpmic1_set_icc(struct regulator_dev *rdev)
  449. {
  450. struct stpmic1_regulator_cfg *cfg = rdev_get_drvdata(rdev);
  451. struct regmap *regmap = rdev_get_regmap(rdev);
  452. /* enable switch off in case of over current */
  453. return regmap_update_bits(regmap, cfg->icc_reg, cfg->icc_mask,
  454. cfg->icc_mask);
  455. }
  456. static irqreturn_t stpmic1_curlim_irq_handler(int irq, void *data)
  457. {
  458. struct regulator_dev *rdev = (struct regulator_dev *)data;
  459. regulator_lock(rdev);
  460. /* Send an overcurrent notification */
  461. regulator_notifier_call_chain(rdev,
  462. REGULATOR_EVENT_OVER_CURRENT,
  463. NULL);
  464. regulator_unlock(rdev);
  465. return IRQ_HANDLED;
  466. }
  467. #define MATCH(_name, _id) \
  468. [STPMIC1_##_id] = { \
  469. .name = #_name, \
  470. .desc = &stpmic1_regulator_cfgs[STPMIC1_##_id].desc, \
  471. }
  472. static struct of_regulator_match stpmic1_matches[] = {
  473. MATCH(buck1, BUCK1),
  474. MATCH(buck2, BUCK2),
  475. MATCH(buck3, BUCK3),
  476. MATCH(buck4, BUCK4),
  477. MATCH(ldo1, LDO1),
  478. MATCH(ldo2, LDO2),
  479. MATCH(ldo3, LDO3),
  480. MATCH(ldo4, LDO4),
  481. MATCH(ldo5, LDO5),
  482. MATCH(ldo6, LDO6),
  483. MATCH(vref_ddr, VREF_DDR),
  484. MATCH(boost, BOOST),
  485. MATCH(pwr_sw1, VBUS_OTG),
  486. MATCH(pwr_sw2, SW_OUT),
  487. };
  488. static int stpmic1_regulator_register(struct platform_device *pdev, int id,
  489. struct of_regulator_match *match,
  490. const struct stpmic1_regulator_cfg *cfg)
  491. {
  492. struct stpmic1 *pmic_dev = dev_get_drvdata(pdev->dev.parent);
  493. struct regulator_dev *rdev;
  494. struct regulator_config config = {};
  495. int ret = 0;
  496. int irq;
  497. config.dev = &pdev->dev;
  498. config.init_data = match->init_data;
  499. config.of_node = match->of_node;
  500. config.regmap = pmic_dev->regmap;
  501. config.driver_data = (void *)cfg;
  502. rdev = devm_regulator_register(&pdev->dev, &cfg->desc, &config);
  503. if (IS_ERR(rdev)) {
  504. dev_err(&pdev->dev, "failed to register %s regulator\n",
  505. cfg->desc.name);
  506. return PTR_ERR(rdev);
  507. }
  508. /* set mask reset */
  509. if (of_get_property(config.of_node, "st,mask-reset", NULL) &&
  510. cfg->mask_reset_reg != 0) {
  511. ret = regmap_update_bits(pmic_dev->regmap,
  512. cfg->mask_reset_reg,
  513. cfg->mask_reset_mask,
  514. cfg->mask_reset_mask);
  515. if (ret) {
  516. dev_err(&pdev->dev, "set mask reset failed\n");
  517. return ret;
  518. }
  519. }
  520. /* setup an irq handler for over-current detection */
  521. irq = of_irq_get(config.of_node, 0);
  522. if (irq > 0) {
  523. ret = devm_request_threaded_irq(&pdev->dev,
  524. irq, NULL,
  525. stpmic1_curlim_irq_handler,
  526. IRQF_ONESHOT | IRQF_SHARED,
  527. pdev->name, rdev);
  528. if (ret) {
  529. dev_err(&pdev->dev, "Request IRQ failed\n");
  530. return ret;
  531. }
  532. }
  533. return 0;
  534. }
  535. static int stpmic1_regulator_probe(struct platform_device *pdev)
  536. {
  537. int i, ret;
  538. ret = of_regulator_match(&pdev->dev, pdev->dev.of_node, stpmic1_matches,
  539. ARRAY_SIZE(stpmic1_matches));
  540. if (ret < 0) {
  541. dev_err(&pdev->dev,
  542. "Error in PMIC regulator device tree node");
  543. return ret;
  544. }
  545. for (i = 0; i < ARRAY_SIZE(stpmic1_regulator_cfgs); i++) {
  546. ret = stpmic1_regulator_register(pdev, i, &stpmic1_matches[i],
  547. &stpmic1_regulator_cfgs[i]);
  548. if (ret < 0)
  549. return ret;
  550. }
  551. dev_dbg(&pdev->dev, "stpmic1_regulator driver probed\n");
  552. return 0;
  553. }
  554. static const struct of_device_id of_pmic_regulator_match[] = {
  555. { .compatible = "st,stpmic1-regulators" },
  556. { },
  557. };
  558. MODULE_DEVICE_TABLE(of, of_pmic_regulator_match);
  559. static struct platform_driver stpmic1_regulator_driver = {
  560. .driver = {
  561. .name = "stpmic1-regulator",
  562. .of_match_table = of_match_ptr(of_pmic_regulator_match),
  563. },
  564. .probe = stpmic1_regulator_probe,
  565. };
  566. module_platform_driver(stpmic1_regulator_driver);
  567. MODULE_DESCRIPTION("STPMIC1 PMIC voltage regulator driver");
  568. MODULE_AUTHOR("Pascal Paillet <p.paillet@st.com>");
  569. MODULE_LICENSE("GPL v2");