bd9576-regulator.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2020 ROHM Semiconductors
  3. // ROHM BD9576MUF/BD9573MUF regulator driver
  4. #include <linux/delay.h>
  5. #include <linux/err.h>
  6. #include <linux/gpio/consumer.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/kernel.h>
  9. #include <linux/mfd/rohm-bd957x.h>
  10. #include <linux/mfd/rohm-generic.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/regulator/driver.h>
  15. #include <linux/regulator/machine.h>
  16. #include <linux/regulator/of_regulator.h>
  17. #include <linux/slab.h>
  18. #define BD957X_VOUTS1_VOLT 3300000
  19. #define BD957X_VOUTS4_BASE_VOLT 1030000
  20. #define BD957X_VOUTS34_NUM_VOLT 32
  21. static int vout1_volt_table[] = {5000000, 4900000, 4800000, 4700000, 4600000,
  22. 4500000, 4500000, 4500000, 5000000, 5100000,
  23. 5200000, 5300000, 5400000, 5500000, 5500000,
  24. 5500000};
  25. static int vout2_volt_table[] = {1800000, 1780000, 1760000, 1740000, 1720000,
  26. 1700000, 1680000, 1660000, 1800000, 1820000,
  27. 1840000, 1860000, 1880000, 1900000, 1920000,
  28. 1940000};
  29. static int voutl1_volt_table[] = {2500000, 2540000, 2580000, 2620000, 2660000,
  30. 2700000, 2740000, 2780000, 2500000, 2460000,
  31. 2420000, 2380000, 2340000, 2300000, 2260000,
  32. 2220000};
  33. struct bd957x_regulator_data {
  34. struct regulator_desc desc;
  35. int base_voltage;
  36. };
  37. static int bd957x_vout34_list_voltage(struct regulator_dev *rdev,
  38. unsigned int selector)
  39. {
  40. const struct regulator_desc *desc = rdev->desc;
  41. int multiplier = selector & desc->vsel_mask & 0x7f;
  42. int tune;
  43. /* VOUT3 and 4 has 10mV step */
  44. tune = multiplier * 10000;
  45. if (!(selector & 0x80))
  46. return desc->fixed_uV - tune;
  47. return desc->fixed_uV + tune;
  48. }
  49. static int bd957x_list_voltage(struct regulator_dev *rdev,
  50. unsigned int selector)
  51. {
  52. const struct regulator_desc *desc = rdev->desc;
  53. int index = selector & desc->vsel_mask & 0x7f;
  54. if (!(selector & 0x80))
  55. index += desc->n_voltages/2;
  56. if (index >= desc->n_voltages)
  57. return -EINVAL;
  58. return desc->volt_table[index];
  59. }
  60. static const struct regulator_ops bd957x_vout34_ops = {
  61. .is_enabled = regulator_is_enabled_regmap,
  62. .list_voltage = bd957x_vout34_list_voltage,
  63. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  64. };
  65. static const struct regulator_ops bd957X_vouts1_regulator_ops = {
  66. .is_enabled = regulator_is_enabled_regmap,
  67. };
  68. static const struct regulator_ops bd957x_ops = {
  69. .is_enabled = regulator_is_enabled_regmap,
  70. .list_voltage = bd957x_list_voltage,
  71. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  72. };
  73. static struct bd957x_regulator_data bd9576_regulators[] = {
  74. {
  75. .desc = {
  76. .name = "VD50",
  77. .of_match = of_match_ptr("regulator-vd50"),
  78. .regulators_node = of_match_ptr("regulators"),
  79. .id = BD957X_VD50,
  80. .type = REGULATOR_VOLTAGE,
  81. .ops = &bd957x_ops,
  82. .volt_table = &vout1_volt_table[0],
  83. .n_voltages = ARRAY_SIZE(vout1_volt_table),
  84. .vsel_reg = BD957X_REG_VOUT1_TUNE,
  85. .vsel_mask = BD957X_MASK_VOUT1_TUNE,
  86. .enable_reg = BD957X_REG_POW_TRIGGER1,
  87. .enable_mask = BD957X_REGULATOR_EN_MASK,
  88. .enable_val = BD957X_REGULATOR_DIS_VAL,
  89. .enable_is_inverted = true,
  90. .owner = THIS_MODULE,
  91. },
  92. },
  93. {
  94. .desc = {
  95. .name = "VD18",
  96. .of_match = of_match_ptr("regulator-vd18"),
  97. .regulators_node = of_match_ptr("regulators"),
  98. .id = BD957X_VD18,
  99. .type = REGULATOR_VOLTAGE,
  100. .ops = &bd957x_ops,
  101. .volt_table = &vout2_volt_table[0],
  102. .n_voltages = ARRAY_SIZE(vout2_volt_table),
  103. .vsel_reg = BD957X_REG_VOUT2_TUNE,
  104. .vsel_mask = BD957X_MASK_VOUT2_TUNE,
  105. .enable_reg = BD957X_REG_POW_TRIGGER2,
  106. .enable_mask = BD957X_REGULATOR_EN_MASK,
  107. .enable_val = BD957X_REGULATOR_DIS_VAL,
  108. .enable_is_inverted = true,
  109. .owner = THIS_MODULE,
  110. },
  111. },
  112. {
  113. .desc = {
  114. .name = "VDDDR",
  115. .of_match = of_match_ptr("regulator-vdddr"),
  116. .regulators_node = of_match_ptr("regulators"),
  117. .id = BD957X_VDDDR,
  118. .ops = &bd957x_vout34_ops,
  119. .type = REGULATOR_VOLTAGE,
  120. .n_voltages = BD957X_VOUTS34_NUM_VOLT,
  121. .vsel_reg = BD957X_REG_VOUT3_TUNE,
  122. .vsel_mask = BD957X_MASK_VOUT3_TUNE,
  123. .enable_reg = BD957X_REG_POW_TRIGGER3,
  124. .enable_mask = BD957X_REGULATOR_EN_MASK,
  125. .enable_val = BD957X_REGULATOR_DIS_VAL,
  126. .enable_is_inverted = true,
  127. .owner = THIS_MODULE,
  128. },
  129. },
  130. {
  131. .desc = {
  132. .name = "VD10",
  133. .of_match = of_match_ptr("regulator-vd10"),
  134. .regulators_node = of_match_ptr("regulators"),
  135. .id = BD957X_VD10,
  136. .ops = &bd957x_vout34_ops,
  137. .type = REGULATOR_VOLTAGE,
  138. .fixed_uV = BD957X_VOUTS4_BASE_VOLT,
  139. .n_voltages = BD957X_VOUTS34_NUM_VOLT,
  140. .vsel_reg = BD957X_REG_VOUT4_TUNE,
  141. .vsel_mask = BD957X_MASK_VOUT4_TUNE,
  142. .enable_reg = BD957X_REG_POW_TRIGGER4,
  143. .enable_mask = BD957X_REGULATOR_EN_MASK,
  144. .enable_val = BD957X_REGULATOR_DIS_VAL,
  145. .enable_is_inverted = true,
  146. .owner = THIS_MODULE,
  147. },
  148. },
  149. {
  150. .desc = {
  151. .name = "VOUTL1",
  152. .of_match = of_match_ptr("regulator-voutl1"),
  153. .regulators_node = of_match_ptr("regulators"),
  154. .id = BD957X_VOUTL1,
  155. .ops = &bd957x_ops,
  156. .type = REGULATOR_VOLTAGE,
  157. .volt_table = &voutl1_volt_table[0],
  158. .n_voltages = ARRAY_SIZE(voutl1_volt_table),
  159. .vsel_reg = BD957X_REG_VOUTL1_TUNE,
  160. .vsel_mask = BD957X_MASK_VOUTL1_TUNE,
  161. .enable_reg = BD957X_REG_POW_TRIGGERL1,
  162. .enable_mask = BD957X_REGULATOR_EN_MASK,
  163. .enable_val = BD957X_REGULATOR_DIS_VAL,
  164. .enable_is_inverted = true,
  165. .owner = THIS_MODULE,
  166. },
  167. },
  168. {
  169. .desc = {
  170. .name = "VOUTS1",
  171. .of_match = of_match_ptr("regulator-vouts1"),
  172. .regulators_node = of_match_ptr("regulators"),
  173. .id = BD957X_VOUTS1,
  174. .ops = &bd957X_vouts1_regulator_ops,
  175. .type = REGULATOR_VOLTAGE,
  176. .n_voltages = 1,
  177. .fixed_uV = BD957X_VOUTS1_VOLT,
  178. .enable_reg = BD957X_REG_POW_TRIGGERS1,
  179. .enable_mask = BD957X_REGULATOR_EN_MASK,
  180. .enable_val = BD957X_REGULATOR_DIS_VAL,
  181. .enable_is_inverted = true,
  182. .owner = THIS_MODULE,
  183. },
  184. },
  185. };
  186. static int bd957x_probe(struct platform_device *pdev)
  187. {
  188. struct regmap *regmap;
  189. struct regulator_config config = { 0 };
  190. int i;
  191. bool vout_mode, ddr_sel;
  192. const struct bd957x_regulator_data *reg_data = &bd9576_regulators[0];
  193. unsigned int num_reg_data = ARRAY_SIZE(bd9576_regulators);
  194. enum rohm_chip_type chip = platform_get_device_id(pdev)->driver_data;
  195. regmap = dev_get_regmap(pdev->dev.parent, NULL);
  196. if (!regmap) {
  197. dev_err(&pdev->dev, "No regmap\n");
  198. return -EINVAL;
  199. }
  200. vout_mode = of_property_read_bool(pdev->dev.parent->of_node,
  201. "rohm,vout1-en-low");
  202. if (vout_mode) {
  203. struct gpio_desc *en;
  204. dev_dbg(&pdev->dev, "GPIO controlled mode\n");
  205. /* VOUT1 enable state judged by VOUT1_EN pin */
  206. /* See if we have GPIO defined */
  207. en = devm_gpiod_get_from_of_node(&pdev->dev,
  208. pdev->dev.parent->of_node,
  209. "rohm,vout1-en-gpios", 0,
  210. GPIOD_OUT_LOW, "vout1-en");
  211. if (!IS_ERR(en)) {
  212. /* VOUT1_OPS gpio ctrl */
  213. /*
  214. * Regulator core prioritizes the ena_gpio over
  215. * enable/disable/is_enabled callbacks so no need to
  216. * clear them. We can still use same ops
  217. */
  218. config.ena_gpiod = en;
  219. } else {
  220. /*
  221. * In theory it is possible someone wants to set
  222. * vout1-en LOW during OTP loading and set VOUT1 to be
  223. * controlled by GPIO - but control the GPIO from some
  224. * where else than this driver. For that to work we
  225. * should unset the is_enabled callback here.
  226. *
  227. * I believe such case where rohm,vout1-en-low is set
  228. * and vout1-en-gpios is not is likely to be a
  229. * misconfiguration. So let's just err out for now.
  230. */
  231. dev_err(&pdev->dev,
  232. "Failed to get VOUT1 control GPIO\n");
  233. return PTR_ERR(en);
  234. }
  235. }
  236. /*
  237. * If more than one PMIC needs to be controlled by same processor then
  238. * allocate the regulator data array here and use bd9576_regulators as
  239. * template. At the moment I see no such use-case so I spare some
  240. * bytes and use bd9576_regulators directly for non-constant configs
  241. * like DDR voltage selection.
  242. */
  243. ddr_sel = of_property_read_bool(pdev->dev.parent->of_node,
  244. "rohm,ddr-sel-low");
  245. if (ddr_sel)
  246. bd9576_regulators[2].desc.fixed_uV = 1350000;
  247. else
  248. bd9576_regulators[2].desc.fixed_uV = 1500000;
  249. switch (chip) {
  250. case ROHM_CHIP_TYPE_BD9576:
  251. dev_dbg(&pdev->dev, "Found BD9576MUF\n");
  252. break;
  253. case ROHM_CHIP_TYPE_BD9573:
  254. dev_dbg(&pdev->dev, "Found BD9573MUF\n");
  255. break;
  256. default:
  257. dev_err(&pdev->dev, "Unsupported chip type\n");
  258. return -EINVAL;
  259. }
  260. config.dev = pdev->dev.parent;
  261. config.regmap = regmap;
  262. for (i = 0; i < num_reg_data; i++) {
  263. const struct regulator_desc *desc;
  264. struct regulator_dev *rdev;
  265. const struct bd957x_regulator_data *r;
  266. r = &reg_data[i];
  267. desc = &r->desc;
  268. rdev = devm_regulator_register(&pdev->dev, desc, &config);
  269. if (IS_ERR(rdev)) {
  270. dev_err(&pdev->dev,
  271. "failed to register %s regulator\n",
  272. desc->name);
  273. return PTR_ERR(rdev);
  274. }
  275. /*
  276. * Clear the VOUT1 GPIO setting - rest of the regulators do not
  277. * support GPIO control
  278. */
  279. config.ena_gpiod = NULL;
  280. }
  281. return 0;
  282. }
  283. static const struct platform_device_id bd957x_pmic_id[] = {
  284. { "bd9573-pmic", ROHM_CHIP_TYPE_BD9573 },
  285. { "bd9576-pmic", ROHM_CHIP_TYPE_BD9576 },
  286. { },
  287. };
  288. MODULE_DEVICE_TABLE(platform, bd957x_pmic_id);
  289. static struct platform_driver bd957x_regulator = {
  290. .driver = {
  291. .name = "bd957x-pmic",
  292. },
  293. .probe = bd957x_probe,
  294. .id_table = bd957x_pmic_id,
  295. };
  296. module_platform_driver(bd957x_regulator);
  297. MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
  298. MODULE_DESCRIPTION("ROHM BD9576/BD9573 voltage regulator driver");
  299. MODULE_LICENSE("GPL");
  300. MODULE_ALIAS("platform:bd957x-pmic");