fixed.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * fixed.c
  3. *
  4. * Copyright 2008 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * Copyright (c) 2009 Nokia Corporation
  9. * Roger Quadros <ext-roger.quadros@nokia.com>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This is useful for systems with mixed controllable and
  17. * non-controllable regulators, as well as for allowing testing on
  18. * systems with no controllable regulators.
  19. */
  20. #include <linux/err.h>
  21. #include <linux/mutex.h>
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/regulator/driver.h>
  25. #include <linux/regulator/fixed.h>
  26. #include <linux/gpio.h>
  27. #include <linux/delay.h>
  28. #include <linux/slab.h>
  29. #include <linux/of.h>
  30. #include <linux/of_gpio.h>
  31. #include <linux/regulator/of_regulator.h>
  32. #include <linux/regulator/machine.h>
  33. struct fixed_voltage_data {
  34. struct regulator_desc desc;
  35. struct regulator_dev *dev;
  36. int microvolts;
  37. int gpio;
  38. unsigned startup_delay;
  39. bool enable_high;
  40. bool is_enabled;
  41. };
  42. /**
  43. * of_get_fixed_voltage_config - extract fixed_voltage_config structure info
  44. * @dev: device requesting for fixed_voltage_config
  45. *
  46. * Populates fixed_voltage_config structure by extracting data from device
  47. * tree node, returns a pointer to the populated structure of NULL if memory
  48. * alloc fails.
  49. */
  50. struct fixed_voltage_config *of_get_fixed_voltage_config(struct device *dev)
  51. {
  52. struct fixed_voltage_config *config;
  53. struct device_node *np = dev->of_node;
  54. const __be32 *delay;
  55. struct regulator_init_data *init_data;
  56. config = devm_kzalloc(dev, sizeof(struct fixed_voltage_config),
  57. GFP_KERNEL);
  58. if (!config)
  59. return NULL;
  60. config->init_data = of_get_regulator_init_data(dev);
  61. if (!config->init_data)
  62. return NULL;
  63. init_data = config->init_data;
  64. config->supply_name = init_data->constraints.name;
  65. if (init_data->constraints.min_uV == init_data->constraints.max_uV) {
  66. config->microvolts = init_data->constraints.min_uV;
  67. } else {
  68. dev_err(dev,
  69. "Fixed regulator specified with variable voltages\n");
  70. return NULL;
  71. }
  72. if (init_data->constraints.boot_on)
  73. config->enabled_at_boot = true;
  74. config->gpio = of_get_named_gpio(np, "gpio", 0);
  75. delay = of_get_property(np, "startup-delay-us", NULL);
  76. if (delay)
  77. config->startup_delay = be32_to_cpu(*delay);
  78. if (of_find_property(np, "enable-active-high", NULL))
  79. config->enable_high = true;
  80. return config;
  81. }
  82. static int fixed_voltage_is_enabled(struct regulator_dev *dev)
  83. {
  84. struct fixed_voltage_data *data = rdev_get_drvdata(dev);
  85. return data->is_enabled;
  86. }
  87. static int fixed_voltage_enable(struct regulator_dev *dev)
  88. {
  89. struct fixed_voltage_data *data = rdev_get_drvdata(dev);
  90. if (gpio_is_valid(data->gpio)) {
  91. gpio_set_value_cansleep(data->gpio, data->enable_high);
  92. data->is_enabled = true;
  93. }
  94. return 0;
  95. }
  96. static int fixed_voltage_disable(struct regulator_dev *dev)
  97. {
  98. struct fixed_voltage_data *data = rdev_get_drvdata(dev);
  99. if (gpio_is_valid(data->gpio)) {
  100. gpio_set_value_cansleep(data->gpio, !data->enable_high);
  101. data->is_enabled = false;
  102. }
  103. return 0;
  104. }
  105. static int fixed_voltage_enable_time(struct regulator_dev *dev)
  106. {
  107. struct fixed_voltage_data *data = rdev_get_drvdata(dev);
  108. return data->startup_delay;
  109. }
  110. static int fixed_voltage_get_voltage(struct regulator_dev *dev)
  111. {
  112. struct fixed_voltage_data *data = rdev_get_drvdata(dev);
  113. return data->microvolts;
  114. }
  115. static int fixed_voltage_list_voltage(struct regulator_dev *dev,
  116. unsigned selector)
  117. {
  118. struct fixed_voltage_data *data = rdev_get_drvdata(dev);
  119. if (selector != 0)
  120. return -EINVAL;
  121. return data->microvolts;
  122. }
  123. static struct regulator_ops fixed_voltage_ops = {
  124. .is_enabled = fixed_voltage_is_enabled,
  125. .enable = fixed_voltage_enable,
  126. .disable = fixed_voltage_disable,
  127. .enable_time = fixed_voltage_enable_time,
  128. .get_voltage = fixed_voltage_get_voltage,
  129. .list_voltage = fixed_voltage_list_voltage,
  130. };
  131. static int __devinit reg_fixed_voltage_probe(struct platform_device *pdev)
  132. {
  133. struct fixed_voltage_config *config = pdev->dev.platform_data;
  134. struct fixed_voltage_data *drvdata;
  135. int ret;
  136. if (pdev->dev.of_node)
  137. config = of_get_fixed_voltage_config(&pdev->dev);
  138. drvdata = kzalloc(sizeof(struct fixed_voltage_data), GFP_KERNEL);
  139. if (drvdata == NULL) {
  140. dev_err(&pdev->dev, "Failed to allocate device data\n");
  141. ret = -ENOMEM;
  142. goto err;
  143. }
  144. drvdata->desc.name = kstrdup(config->supply_name, GFP_KERNEL);
  145. if (drvdata->desc.name == NULL) {
  146. dev_err(&pdev->dev, "Failed to allocate supply name\n");
  147. ret = -ENOMEM;
  148. goto err;
  149. }
  150. drvdata->desc.type = REGULATOR_VOLTAGE;
  151. drvdata->desc.owner = THIS_MODULE;
  152. drvdata->desc.ops = &fixed_voltage_ops;
  153. drvdata->desc.n_voltages = 1;
  154. drvdata->microvolts = config->microvolts;
  155. drvdata->gpio = config->gpio;
  156. drvdata->startup_delay = config->startup_delay;
  157. if (gpio_is_valid(config->gpio)) {
  158. drvdata->enable_high = config->enable_high;
  159. /* FIXME: Remove below print warning
  160. *
  161. * config->gpio must be set to -EINVAL by platform code if
  162. * GPIO control is not required. However, early adopters
  163. * not requiring GPIO control may forget to initialize
  164. * config->gpio to -EINVAL. This will cause GPIO 0 to be used
  165. * for GPIO control.
  166. *
  167. * This warning will be removed once there are a couple of users
  168. * for this driver.
  169. */
  170. if (!config->gpio)
  171. dev_warn(&pdev->dev,
  172. "using GPIO 0 for regulator enable control\n");
  173. ret = gpio_request(config->gpio, config->supply_name);
  174. if (ret) {
  175. dev_err(&pdev->dev,
  176. "Could not obtain regulator enable GPIO %d: %d\n",
  177. config->gpio, ret);
  178. goto err_name;
  179. }
  180. /* set output direction without changing state
  181. * to prevent glitch
  182. */
  183. drvdata->is_enabled = config->enabled_at_boot;
  184. ret = drvdata->is_enabled ?
  185. config->enable_high : !config->enable_high;
  186. ret = gpio_direction_output(config->gpio, ret);
  187. if (ret) {
  188. dev_err(&pdev->dev,
  189. "Could not configure regulator enable GPIO %d direction: %d\n",
  190. config->gpio, ret);
  191. goto err_gpio;
  192. }
  193. } else {
  194. /* Regulator without GPIO control is considered
  195. * always enabled
  196. */
  197. drvdata->is_enabled = true;
  198. }
  199. drvdata->dev = regulator_register(&drvdata->desc, &pdev->dev,
  200. config->init_data, drvdata, NULL);
  201. if (IS_ERR(drvdata->dev)) {
  202. ret = PTR_ERR(drvdata->dev);
  203. dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
  204. goto err_gpio;
  205. }
  206. platform_set_drvdata(pdev, drvdata);
  207. dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name,
  208. drvdata->microvolts);
  209. return 0;
  210. err_gpio:
  211. if (gpio_is_valid(config->gpio))
  212. gpio_free(config->gpio);
  213. err_name:
  214. kfree(drvdata->desc.name);
  215. err:
  216. kfree(drvdata);
  217. return ret;
  218. }
  219. static int __devexit reg_fixed_voltage_remove(struct platform_device *pdev)
  220. {
  221. struct fixed_voltage_data *drvdata = platform_get_drvdata(pdev);
  222. regulator_unregister(drvdata->dev);
  223. if (gpio_is_valid(drvdata->gpio))
  224. gpio_free(drvdata->gpio);
  225. kfree(drvdata->desc.name);
  226. kfree(drvdata);
  227. return 0;
  228. }
  229. #if defined(CONFIG_OF)
  230. static const struct of_device_id fixed_of_match[] __devinitconst = {
  231. { .compatible = "regulator-fixed", },
  232. {},
  233. };
  234. MODULE_DEVICE_TABLE(of, fixed_of_match);
  235. #else
  236. #define fixed_of_match NULL
  237. #endif
  238. static struct platform_driver regulator_fixed_voltage_driver = {
  239. .probe = reg_fixed_voltage_probe,
  240. .remove = __devexit_p(reg_fixed_voltage_remove),
  241. .driver = {
  242. .name = "reg-fixed-voltage",
  243. .owner = THIS_MODULE,
  244. .of_match_table = fixed_of_match,
  245. },
  246. };
  247. static int __init regulator_fixed_voltage_init(void)
  248. {
  249. return platform_driver_register(&regulator_fixed_voltage_driver);
  250. }
  251. subsys_initcall(regulator_fixed_voltage_init);
  252. static void __exit regulator_fixed_voltage_exit(void)
  253. {
  254. platform_driver_unregister(&regulator_fixed_voltage_driver);
  255. }
  256. module_exit(regulator_fixed_voltage_exit);
  257. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  258. MODULE_DESCRIPTION("Fixed voltage regulator");
  259. MODULE_LICENSE("GPL");
  260. MODULE_ALIAS("platform:reg-fixed-voltage");