stpmic1_regulator.c 18 KB

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