wm8994-regulator.c 5.0 KB

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