fixed.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. static struct fixed_voltage_config *
  51. of_get_fixed_voltage_config(struct device *dev)
  52. {
  53. struct fixed_voltage_config *config;
  54. struct device_node *np = dev->of_node;
  55. const __be32 *delay;
  56. struct regulator_init_data *init_data;
  57. config = devm_kzalloc(dev, sizeof(struct fixed_voltage_config),
  58. GFP_KERNEL);
  59. if (!config)
  60. return ERR_PTR(-ENOMEM);
  61. config->init_data = of_get_regulator_init_data(dev, dev->of_node);
  62. if (!config->init_data)
  63. return ERR_PTR(-EINVAL);
  64. init_data = config->init_data;
  65. init_data->constraints.apply_uV = 0;
  66. config->supply_name = init_data->constraints.name;
  67. if (init_data->constraints.min_uV == init_data->constraints.max_uV) {
  68. config->microvolts = init_data->constraints.min_uV;
  69. } else {
  70. dev_err(dev,
  71. "Fixed regulator specified with variable voltages\n");
  72. return ERR_PTR(-EINVAL);
  73. }
  74. if (init_data->constraints.boot_on)
  75. config->enabled_at_boot = true;
  76. config->gpio = of_get_named_gpio(np, "gpio", 0);
  77. /*
  78. * of_get_named_gpio() currently returns ENODEV rather than
  79. * EPROBE_DEFER. This code attempts to be compatible with both
  80. * for now; the ENODEV check can be removed once the API is fixed.
  81. * of_get_named_gpio() doesn't differentiate between a missing
  82. * property (which would be fine here, since the GPIO is optional)
  83. * and some other error. Patches have been posted for both issues.
  84. * Once they are check in, we should replace this with:
  85. * if (config->gpio < 0 && config->gpio != -ENOENT)
  86. */
  87. if ((config->gpio == -ENODEV) || (config->gpio == -EPROBE_DEFER))
  88. return ERR_PTR(-EPROBE_DEFER);
  89. delay = of_get_property(np, "startup-delay-us", NULL);
  90. if (delay)
  91. config->startup_delay = be32_to_cpu(*delay);
  92. if (of_find_property(np, "enable-active-high", NULL))
  93. config->enable_high = true;
  94. if (of_find_property(np, "parent-supply", NULL))
  95. init_data->supply_regulator = "parent";
  96. return config;
  97. }
  98. static int fixed_voltage_is_enabled(struct regulator_dev *dev)
  99. {
  100. struct fixed_voltage_data *data = rdev_get_drvdata(dev);
  101. return data->is_enabled;
  102. }
  103. static int fixed_voltage_enable(struct regulator_dev *dev)
  104. {
  105. struct fixed_voltage_data *data = rdev_get_drvdata(dev);
  106. if (gpio_is_valid(data->gpio)) {
  107. gpio_set_value_cansleep(data->gpio, data->enable_high);
  108. data->is_enabled = true;
  109. }
  110. return 0;
  111. }
  112. static int fixed_voltage_disable(struct regulator_dev *dev)
  113. {
  114. struct fixed_voltage_data *data = rdev_get_drvdata(dev);
  115. if (gpio_is_valid(data->gpio)) {
  116. gpio_set_value_cansleep(data->gpio, !data->enable_high);
  117. data->is_enabled = false;
  118. }
  119. return 0;
  120. }
  121. static int fixed_voltage_enable_time(struct regulator_dev *dev)
  122. {
  123. struct fixed_voltage_data *data = rdev_get_drvdata(dev);
  124. return data->startup_delay;
  125. }
  126. static int fixed_voltage_get_voltage(struct regulator_dev *dev)
  127. {
  128. struct fixed_voltage_data *data = rdev_get_drvdata(dev);
  129. if (data->microvolts)
  130. return data->microvolts;
  131. else
  132. return -EINVAL;
  133. }
  134. static int fixed_voltage_list_voltage(struct regulator_dev *dev,
  135. unsigned selector)
  136. {
  137. struct fixed_voltage_data *data = rdev_get_drvdata(dev);
  138. if (selector != 0)
  139. return -EINVAL;
  140. return data->microvolts;
  141. }
  142. static struct regulator_ops fixed_voltage_ops = {
  143. .is_enabled = fixed_voltage_is_enabled,
  144. .enable = fixed_voltage_enable,
  145. .disable = fixed_voltage_disable,
  146. .enable_time = fixed_voltage_enable_time,
  147. .get_voltage = fixed_voltage_get_voltage,
  148. .list_voltage = fixed_voltage_list_voltage,
  149. };
  150. static int __devinit reg_fixed_voltage_probe(struct platform_device *pdev)
  151. {
  152. struct fixed_voltage_config *config;
  153. struct fixed_voltage_data *drvdata;
  154. int ret;
  155. if (pdev->dev.of_node) {
  156. config = of_get_fixed_voltage_config(&pdev->dev);
  157. if (IS_ERR(config))
  158. return PTR_ERR(config);
  159. } else {
  160. config = pdev->dev.platform_data;
  161. }
  162. if (!config)
  163. return -ENOMEM;
  164. drvdata = kzalloc(sizeof(struct fixed_voltage_data), GFP_KERNEL);
  165. if (drvdata == NULL) {
  166. dev_err(&pdev->dev, "Failed to allocate device data\n");
  167. ret = -ENOMEM;
  168. goto err;
  169. }
  170. drvdata->desc.name = kstrdup(config->supply_name, GFP_KERNEL);
  171. if (drvdata->desc.name == NULL) {
  172. dev_err(&pdev->dev, "Failed to allocate supply name\n");
  173. ret = -ENOMEM;
  174. goto err;
  175. }
  176. drvdata->desc.type = REGULATOR_VOLTAGE;
  177. drvdata->desc.owner = THIS_MODULE;
  178. drvdata->desc.ops = &fixed_voltage_ops;
  179. if (config->microvolts)
  180. drvdata->desc.n_voltages = 1;
  181. drvdata->microvolts = config->microvolts;
  182. drvdata->gpio = config->gpio;
  183. drvdata->startup_delay = config->startup_delay;
  184. if (gpio_is_valid(config->gpio)) {
  185. drvdata->enable_high = config->enable_high;
  186. /* FIXME: Remove below print warning
  187. *
  188. * config->gpio must be set to -EINVAL by platform code if
  189. * GPIO control is not required. However, early adopters
  190. * not requiring GPIO control may forget to initialize
  191. * config->gpio to -EINVAL. This will cause GPIO 0 to be used
  192. * for GPIO control.
  193. *
  194. * This warning will be removed once there are a couple of users
  195. * for this driver.
  196. */
  197. if (!config->gpio)
  198. dev_warn(&pdev->dev,
  199. "using GPIO 0 for regulator enable control\n");
  200. ret = gpio_request(config->gpio, config->supply_name);
  201. if (ret) {
  202. dev_err(&pdev->dev,
  203. "Could not obtain regulator enable GPIO %d: %d\n",
  204. config->gpio, ret);
  205. goto err_name;
  206. }
  207. /* set output direction without changing state
  208. * to prevent glitch
  209. */
  210. drvdata->is_enabled = config->enabled_at_boot;
  211. ret = drvdata->is_enabled ?
  212. config->enable_high : !config->enable_high;
  213. ret = gpio_direction_output(config->gpio, ret);
  214. if (ret) {
  215. dev_err(&pdev->dev,
  216. "Could not configure regulator enable GPIO %d direction: %d\n",
  217. config->gpio, ret);
  218. goto err_gpio;
  219. }
  220. } else {
  221. /* Regulator without GPIO control is considered
  222. * always enabled
  223. */
  224. drvdata->is_enabled = true;
  225. }
  226. drvdata->dev = regulator_register(&drvdata->desc, &pdev->dev,
  227. config->init_data, drvdata,
  228. pdev->dev.of_node);
  229. if (IS_ERR(drvdata->dev)) {
  230. ret = PTR_ERR(drvdata->dev);
  231. dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
  232. goto err_gpio;
  233. }
  234. platform_set_drvdata(pdev, drvdata);
  235. dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name,
  236. drvdata->microvolts);
  237. return 0;
  238. err_gpio:
  239. if (gpio_is_valid(config->gpio))
  240. gpio_free(config->gpio);
  241. err_name:
  242. kfree(drvdata->desc.name);
  243. err:
  244. kfree(drvdata);
  245. return ret;
  246. }
  247. static int __devexit reg_fixed_voltage_remove(struct platform_device *pdev)
  248. {
  249. struct fixed_voltage_data *drvdata = platform_get_drvdata(pdev);
  250. regulator_unregister(drvdata->dev);
  251. if (gpio_is_valid(drvdata->gpio))
  252. gpio_free(drvdata->gpio);
  253. kfree(drvdata->desc.name);
  254. kfree(drvdata);
  255. return 0;
  256. }
  257. #if defined(CONFIG_OF)
  258. static const struct of_device_id fixed_of_match[] __devinitconst = {
  259. { .compatible = "regulator-fixed", },
  260. {},
  261. };
  262. MODULE_DEVICE_TABLE(of, fixed_of_match);
  263. #else
  264. #define fixed_of_match NULL
  265. #endif
  266. static struct platform_driver regulator_fixed_voltage_driver = {
  267. .probe = reg_fixed_voltage_probe,
  268. .remove = __devexit_p(reg_fixed_voltage_remove),
  269. .driver = {
  270. .name = "reg-fixed-voltage",
  271. .owner = THIS_MODULE,
  272. .of_match_table = fixed_of_match,
  273. },
  274. };
  275. static int __init regulator_fixed_voltage_init(void)
  276. {
  277. return platform_driver_register(&regulator_fixed_voltage_driver);
  278. }
  279. subsys_initcall(regulator_fixed_voltage_init);
  280. static void __exit regulator_fixed_voltage_exit(void)
  281. {
  282. platform_driver_unregister(&regulator_fixed_voltage_driver);
  283. }
  284. module_exit(regulator_fixed_voltage_exit);
  285. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  286. MODULE_DESCRIPTION("Fixed voltage regulator");
  287. MODULE_LICENSE("GPL");
  288. MODULE_ALIAS("platform:reg-fixed-voltage");