wm8994-regulator.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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/gpio.h>
  21. #include <linux/slab.h>
  22. #include <linux/mfd/wm8994/core.h>
  23. #include <linux/mfd/wm8994/registers.h>
  24. #include <linux/mfd/wm8994/pdata.h>
  25. struct wm8994_ldo {
  26. struct regulator_dev *regulator;
  27. struct wm8994 *wm8994;
  28. };
  29. #define WM8994_LDO1_MAX_SELECTOR 0x7
  30. #define WM8994_LDO2_MAX_SELECTOR 0x3
  31. static struct regulator_ops wm8994_ldo1_ops = {
  32. .list_voltage = regulator_list_voltage_linear,
  33. .map_voltage = regulator_map_voltage_linear,
  34. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  35. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  36. };
  37. static int wm8994_ldo2_list_voltage(struct regulator_dev *rdev,
  38. unsigned int selector)
  39. {
  40. struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
  41. if (selector > WM8994_LDO2_MAX_SELECTOR)
  42. return -EINVAL;
  43. switch (ldo->wm8994->type) {
  44. case WM8994:
  45. return (selector * 100000) + 900000;
  46. case WM8958:
  47. return (selector * 100000) + 1000000;
  48. case WM1811:
  49. switch (selector) {
  50. case 0:
  51. return -EINVAL;
  52. default:
  53. return (selector * 100000) + 950000;
  54. }
  55. break;
  56. default:
  57. return -EINVAL;
  58. }
  59. }
  60. static struct regulator_ops wm8994_ldo2_ops = {
  61. .list_voltage = wm8994_ldo2_list_voltage,
  62. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  63. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  64. };
  65. static const struct regulator_desc wm8994_ldo_desc[] = {
  66. {
  67. .name = "LDO1",
  68. .id = 1,
  69. .type = REGULATOR_VOLTAGE,
  70. .n_voltages = WM8994_LDO1_MAX_SELECTOR + 1,
  71. .vsel_reg = WM8994_LDO_1,
  72. .vsel_mask = WM8994_LDO1_VSEL_MASK,
  73. .ops = &wm8994_ldo1_ops,
  74. .min_uV = 2400000,
  75. .uV_step = 100000,
  76. .enable_time = 3000,
  77. .owner = THIS_MODULE,
  78. },
  79. {
  80. .name = "LDO2",
  81. .id = 2,
  82. .type = REGULATOR_VOLTAGE,
  83. .n_voltages = WM8994_LDO2_MAX_SELECTOR + 1,
  84. .vsel_reg = WM8994_LDO_2,
  85. .vsel_mask = WM8994_LDO2_VSEL_MASK,
  86. .ops = &wm8994_ldo2_ops,
  87. .enable_time = 3000,
  88. .owner = THIS_MODULE,
  89. },
  90. };
  91. static int wm8994_ldo_probe(struct platform_device *pdev)
  92. {
  93. struct wm8994 *wm8994 = dev_get_drvdata(pdev->dev.parent);
  94. struct wm8994_pdata *pdata = wm8994->dev->platform_data;
  95. int id = pdev->id % ARRAY_SIZE(pdata->ldo);
  96. struct regulator_config config = { };
  97. struct wm8994_ldo *ldo;
  98. int ret;
  99. dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
  100. ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm8994_ldo), GFP_KERNEL);
  101. if (ldo == NULL) {
  102. dev_err(&pdev->dev, "Unable to allocate private data\n");
  103. return -ENOMEM;
  104. }
  105. ldo->wm8994 = wm8994;
  106. config.dev = wm8994->dev;
  107. config.driver_data = ldo;
  108. config.regmap = wm8994->regmap;
  109. if (pdata) {
  110. config.init_data = pdata->ldo[id].init_data;
  111. config.ena_gpio = pdata->ldo[id].enable;
  112. }
  113. ldo->regulator = regulator_register(&wm8994_ldo_desc[id], &config);
  114. if (IS_ERR(ldo->regulator)) {
  115. ret = PTR_ERR(ldo->regulator);
  116. dev_err(wm8994->dev, "Failed to register LDO%d: %d\n",
  117. id + 1, ret);
  118. goto err;
  119. }
  120. platform_set_drvdata(pdev, ldo);
  121. return 0;
  122. err:
  123. return ret;
  124. }
  125. static int wm8994_ldo_remove(struct platform_device *pdev)
  126. {
  127. struct wm8994_ldo *ldo = platform_get_drvdata(pdev);
  128. platform_set_drvdata(pdev, NULL);
  129. regulator_unregister(ldo->regulator);
  130. return 0;
  131. }
  132. static struct platform_driver wm8994_ldo_driver = {
  133. .probe = wm8994_ldo_probe,
  134. .remove = wm8994_ldo_remove,
  135. .driver = {
  136. .name = "wm8994-ldo",
  137. .owner = THIS_MODULE,
  138. },
  139. };
  140. static int __init wm8994_ldo_init(void)
  141. {
  142. int ret;
  143. ret = platform_driver_register(&wm8994_ldo_driver);
  144. if (ret != 0)
  145. pr_err("Failed to register Wm8994 GP LDO driver: %d\n", ret);
  146. return ret;
  147. }
  148. subsys_initcall(wm8994_ldo_init);
  149. static void __exit wm8994_ldo_exit(void)
  150. {
  151. platform_driver_unregister(&wm8994_ldo_driver);
  152. }
  153. module_exit(wm8994_ldo_exit);
  154. /* Module information */
  155. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  156. MODULE_DESCRIPTION("WM8994 LDO driver");
  157. MODULE_LICENSE("GPL");
  158. MODULE_ALIAS("platform:wm8994-ldo");