wm8994-regulator.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // wm8994-regulator.c -- Regulator driver for the WM8994
  4. //
  5. // Copyright 2009 Wolfson Microelectronics PLC.
  6. //
  7. // Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  8. #include <linux/module.h>
  9. #include <linux/moduleparam.h>
  10. #include <linux/init.h>
  11. #include <linux/bitops.h>
  12. #include <linux/err.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/regulator/driver.h>
  15. #include <linux/regulator/machine.h>
  16. #include <linux/gpio/consumer.h>
  17. #include <linux/slab.h>
  18. #include <linux/mfd/wm8994/core.h>
  19. #include <linux/mfd/wm8994/registers.h>
  20. #include <linux/mfd/wm8994/pdata.h>
  21. struct wm8994_ldo {
  22. struct regulator_dev *regulator;
  23. struct wm8994 *wm8994;
  24. struct regulator_consumer_supply supply;
  25. struct regulator_init_data init_data;
  26. };
  27. #define WM8994_LDO1_MAX_SELECTOR 0x7
  28. #define WM8994_LDO2_MAX_SELECTOR 0x3
  29. static const struct regulator_ops wm8994_ldo1_ops = {
  30. .list_voltage = regulator_list_voltage_linear,
  31. .map_voltage = regulator_map_voltage_linear,
  32. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  33. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  34. };
  35. static int wm8994_ldo2_list_voltage(struct regulator_dev *rdev,
  36. unsigned int selector)
  37. {
  38. struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
  39. if (selector > WM8994_LDO2_MAX_SELECTOR)
  40. return -EINVAL;
  41. switch (ldo->wm8994->type) {
  42. case WM8994:
  43. return (selector * 100000) + 900000;
  44. case WM8958:
  45. return (selector * 100000) + 1000000;
  46. case WM1811:
  47. switch (selector) {
  48. case 0:
  49. return -EINVAL;
  50. default:
  51. return (selector * 100000) + 950000;
  52. }
  53. break;
  54. default:
  55. return -EINVAL;
  56. }
  57. }
  58. static const struct regulator_ops wm8994_ldo2_ops = {
  59. .list_voltage = wm8994_ldo2_list_voltage,
  60. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  61. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  62. };
  63. static const struct regulator_desc wm8994_ldo_desc[] = {
  64. {
  65. .name = "LDO1",
  66. .id = 1,
  67. .type = REGULATOR_VOLTAGE,
  68. .n_voltages = WM8994_LDO1_MAX_SELECTOR + 1,
  69. .vsel_reg = WM8994_LDO_1,
  70. .vsel_mask = WM8994_LDO1_VSEL_MASK,
  71. .ops = &wm8994_ldo1_ops,
  72. .min_uV = 2400000,
  73. .uV_step = 100000,
  74. .enable_time = 3000,
  75. .owner = THIS_MODULE,
  76. },
  77. {
  78. .name = "LDO2",
  79. .id = 2,
  80. .type = REGULATOR_VOLTAGE,
  81. .n_voltages = WM8994_LDO2_MAX_SELECTOR + 1,
  82. .vsel_reg = WM8994_LDO_2,
  83. .vsel_mask = WM8994_LDO2_VSEL_MASK,
  84. .ops = &wm8994_ldo2_ops,
  85. .enable_time = 3000,
  86. .owner = THIS_MODULE,
  87. },
  88. };
  89. static const struct regulator_consumer_supply wm8994_ldo_consumer[] = {
  90. { .supply = "AVDD1" },
  91. { .supply = "DCVDD" },
  92. };
  93. static const struct regulator_init_data wm8994_ldo_default[] = {
  94. {
  95. .constraints = {
  96. .valid_ops_mask = REGULATOR_CHANGE_STATUS,
  97. },
  98. .num_consumer_supplies = 1,
  99. },
  100. {
  101. .constraints = {
  102. .valid_ops_mask = REGULATOR_CHANGE_STATUS,
  103. },
  104. .num_consumer_supplies = 1,
  105. },
  106. };
  107. static int wm8994_ldo_probe(struct platform_device *pdev)
  108. {
  109. struct wm8994 *wm8994 = dev_get_drvdata(pdev->dev.parent);
  110. struct wm8994_pdata *pdata = dev_get_platdata(wm8994->dev);
  111. int id = pdev->id % ARRAY_SIZE(pdata->ldo);
  112. struct regulator_config config = { };
  113. struct wm8994_ldo *ldo;
  114. struct gpio_desc *gpiod;
  115. int ret;
  116. dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
  117. ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm8994_ldo), GFP_KERNEL);
  118. if (!ldo)
  119. return -ENOMEM;
  120. ldo->wm8994 = wm8994;
  121. ldo->supply = wm8994_ldo_consumer[id];
  122. ldo->supply.dev_name = dev_name(wm8994->dev);
  123. config.dev = wm8994->dev;
  124. config.driver_data = ldo;
  125. config.regmap = wm8994->regmap;
  126. config.init_data = &ldo->init_data;
  127. /*
  128. * Look up LDO enable GPIO from the parent device node, we don't
  129. * use devm because the regulator core will free the GPIO
  130. */
  131. gpiod = gpiod_get_optional(pdev->dev.parent,
  132. id ? "wlf,ldo2ena" : "wlf,ldo1ena",
  133. GPIOD_OUT_LOW |
  134. GPIOD_FLAGS_BIT_NONEXCLUSIVE);
  135. if (IS_ERR(gpiod))
  136. return PTR_ERR(gpiod);
  137. config.ena_gpiod = gpiod;
  138. /* Use default constraints if none set up */
  139. if (!pdata || !pdata->ldo[id].init_data || wm8994->dev->of_node) {
  140. dev_dbg(wm8994->dev, "Using default init data, supply %s %s\n",
  141. ldo->supply.dev_name, ldo->supply.supply);
  142. ldo->init_data = wm8994_ldo_default[id];
  143. ldo->init_data.consumer_supplies = &ldo->supply;
  144. if (!gpiod)
  145. ldo->init_data.constraints.valid_ops_mask = 0;
  146. } else {
  147. ldo->init_data = *pdata->ldo[id].init_data;
  148. }
  149. /*
  150. * At this point the GPIO descriptor is handled over to the
  151. * regulator core and we need not worry about it on the
  152. * error path.
  153. */
  154. ldo->regulator = devm_regulator_register(&pdev->dev,
  155. &wm8994_ldo_desc[id],
  156. &config);
  157. if (IS_ERR(ldo->regulator)) {
  158. ret = PTR_ERR(ldo->regulator);
  159. dev_err(wm8994->dev, "Failed to register LDO%d: %d\n",
  160. id + 1, ret);
  161. return ret;
  162. }
  163. platform_set_drvdata(pdev, ldo);
  164. return 0;
  165. }
  166. static struct platform_driver wm8994_ldo_driver = {
  167. .probe = wm8994_ldo_probe,
  168. .driver = {
  169. .name = "wm8994-ldo",
  170. },
  171. };
  172. module_platform_driver(wm8994_ldo_driver);
  173. /* Module information */
  174. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  175. MODULE_DESCRIPTION("WM8994 LDO driver");
  176. MODULE_LICENSE("GPL");
  177. MODULE_ALIAS("platform:wm8994-ldo");