fixed.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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/slab.h>
  28. #include <linux/of.h>
  29. #include <linux/of_gpio.h>
  30. #include <linux/regulator/of_regulator.h>
  31. #include <linux/regulator/machine.h>
  32. struct fixed_voltage_data {
  33. struct regulator_desc desc;
  34. struct regulator_dev *dev;
  35. int microvolts;
  36. int gpio;
  37. unsigned startup_delay;
  38. bool enable_high;
  39. bool is_enabled;
  40. };
  41. /**
  42. * of_get_fixed_voltage_config - extract fixed_voltage_config structure info
  43. * @dev: device requesting for fixed_voltage_config
  44. *
  45. * Populates fixed_voltage_config structure by extracting data from device
  46. * tree node, returns a pointer to the populated structure of NULL if memory
  47. * alloc fails.
  48. */
  49. static struct fixed_voltage_config *
  50. 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, dev->of_node);
  61. if (!config->init_data)
  62. return NULL;
  63. init_data = config->init_data;
  64. init_data->constraints.apply_uV = 0;
  65. config->supply_name = init_data->constraints.name;
  66. if (init_data->constraints.min_uV == init_data->constraints.max_uV) {
  67. config->microvolts = init_data->constraints.min_uV;
  68. } else {
  69. dev_err(dev,
  70. "Fixed regulator specified with variable voltages\n");
  71. return NULL;
  72. }
  73. if (init_data->constraints.boot_on)
  74. config->enabled_at_boot = true;
  75. config->gpio = of_get_named_gpio(np, "gpio", 0);
  76. delay = of_get_property(np, "startup-delay-us", NULL);
  77. if (delay)
  78. config->startup_delay = be32_to_cpu(*delay);
  79. if (of_find_property(np, "enable-active-high", NULL))
  80. config->enable_high = true;
  81. if (of_find_property(np, "gpio-open-drain", NULL))
  82. config->gpio_is_open_drain = true;
  83. return config;
  84. }
  85. static int fixed_voltage_is_enabled(struct regulator_dev *dev)
  86. {
  87. struct fixed_voltage_data *data = rdev_get_drvdata(dev);
  88. return data->is_enabled;
  89. }
  90. static int fixed_voltage_enable(struct regulator_dev *dev)
  91. {
  92. struct fixed_voltage_data *data = rdev_get_drvdata(dev);
  93. gpio_set_value_cansleep(data->gpio, data->enable_high);
  94. data->is_enabled = true;
  95. return 0;
  96. }
  97. static int fixed_voltage_disable(struct regulator_dev *dev)
  98. {
  99. struct fixed_voltage_data *data = rdev_get_drvdata(dev);
  100. gpio_set_value_cansleep(data->gpio, !data->enable_high);
  101. data->is_enabled = false;
  102. return 0;
  103. }
  104. static int fixed_voltage_enable_time(struct regulator_dev *dev)
  105. {
  106. struct fixed_voltage_data *data = rdev_get_drvdata(dev);
  107. return data->startup_delay;
  108. }
  109. static int fixed_voltage_get_voltage(struct regulator_dev *dev)
  110. {
  111. struct fixed_voltage_data *data = rdev_get_drvdata(dev);
  112. if (data->microvolts)
  113. return data->microvolts;
  114. else
  115. return -EINVAL;
  116. }
  117. static int fixed_voltage_list_voltage(struct regulator_dev *dev,
  118. unsigned selector)
  119. {
  120. struct fixed_voltage_data *data = rdev_get_drvdata(dev);
  121. if (selector != 0)
  122. return -EINVAL;
  123. return data->microvolts;
  124. }
  125. static struct regulator_ops fixed_voltage_gpio_ops = {
  126. .is_enabled = fixed_voltage_is_enabled,
  127. .enable = fixed_voltage_enable,
  128. .disable = fixed_voltage_disable,
  129. .enable_time = fixed_voltage_enable_time,
  130. .get_voltage = fixed_voltage_get_voltage,
  131. .list_voltage = fixed_voltage_list_voltage,
  132. };
  133. static struct regulator_ops fixed_voltage_ops = {
  134. .get_voltage = fixed_voltage_get_voltage,
  135. .list_voltage = fixed_voltage_list_voltage,
  136. };
  137. static int __devinit reg_fixed_voltage_probe(struct platform_device *pdev)
  138. {
  139. struct fixed_voltage_config *config;
  140. struct fixed_voltage_data *drvdata;
  141. struct regulator_config cfg = { };
  142. int ret;
  143. if (pdev->dev.of_node)
  144. config = of_get_fixed_voltage_config(&pdev->dev);
  145. else
  146. config = pdev->dev.platform_data;
  147. if (!config)
  148. return -ENOMEM;
  149. drvdata = devm_kzalloc(&pdev->dev, sizeof(struct fixed_voltage_data),
  150. GFP_KERNEL);
  151. if (drvdata == NULL) {
  152. dev_err(&pdev->dev, "Failed to allocate device data\n");
  153. ret = -ENOMEM;
  154. goto err;
  155. }
  156. drvdata->desc.name = kstrdup(config->supply_name, GFP_KERNEL);
  157. if (drvdata->desc.name == NULL) {
  158. dev_err(&pdev->dev, "Failed to allocate supply name\n");
  159. ret = -ENOMEM;
  160. goto err;
  161. }
  162. drvdata->desc.type = REGULATOR_VOLTAGE;
  163. drvdata->desc.owner = THIS_MODULE;
  164. if (config->microvolts)
  165. drvdata->desc.n_voltages = 1;
  166. drvdata->microvolts = config->microvolts;
  167. drvdata->gpio = config->gpio;
  168. drvdata->startup_delay = config->startup_delay;
  169. if (gpio_is_valid(config->gpio)) {
  170. int gpio_flag;
  171. drvdata->enable_high = config->enable_high;
  172. /* FIXME: Remove below print warning
  173. *
  174. * config->gpio must be set to -EINVAL by platform code if
  175. * GPIO control is not required. However, early adopters
  176. * not requiring GPIO control may forget to initialize
  177. * config->gpio to -EINVAL. This will cause GPIO 0 to be used
  178. * for GPIO control.
  179. *
  180. * This warning will be removed once there are a couple of users
  181. * for this driver.
  182. */
  183. if (!config->gpio)
  184. dev_warn(&pdev->dev,
  185. "using GPIO 0 for regulator enable control\n");
  186. /*
  187. * set output direction without changing state
  188. * to prevent glitch
  189. */
  190. drvdata->is_enabled = config->enabled_at_boot;
  191. ret = drvdata->is_enabled ?
  192. config->enable_high : !config->enable_high;
  193. gpio_flag = ret ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
  194. if (config->gpio_is_open_drain)
  195. gpio_flag |= GPIOF_OPEN_DRAIN;
  196. ret = gpio_request_one(config->gpio, gpio_flag,
  197. config->supply_name);
  198. if (ret) {
  199. dev_err(&pdev->dev,
  200. "Could not obtain regulator enable GPIO %d: %d\n",
  201. config->gpio, ret);
  202. goto err_name;
  203. }
  204. drvdata->desc.ops = &fixed_voltage_gpio_ops;
  205. } else {
  206. drvdata->desc.ops = &fixed_voltage_ops;
  207. }
  208. cfg.dev = &pdev->dev;
  209. cfg.init_data = config->init_data;
  210. cfg.driver_data = drvdata;
  211. cfg.of_node = pdev->dev.of_node;
  212. drvdata->dev = regulator_register(&drvdata->desc, &cfg);
  213. if (IS_ERR(drvdata->dev)) {
  214. ret = PTR_ERR(drvdata->dev);
  215. dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
  216. goto err_gpio;
  217. }
  218. platform_set_drvdata(pdev, drvdata);
  219. dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name,
  220. drvdata->microvolts);
  221. return 0;
  222. err_gpio:
  223. if (gpio_is_valid(config->gpio))
  224. gpio_free(config->gpio);
  225. err_name:
  226. kfree(drvdata->desc.name);
  227. err:
  228. return ret;
  229. }
  230. static int __devexit reg_fixed_voltage_remove(struct platform_device *pdev)
  231. {
  232. struct fixed_voltage_data *drvdata = platform_get_drvdata(pdev);
  233. regulator_unregister(drvdata->dev);
  234. if (gpio_is_valid(drvdata->gpio))
  235. gpio_free(drvdata->gpio);
  236. kfree(drvdata->desc.name);
  237. return 0;
  238. }
  239. #if defined(CONFIG_OF)
  240. static const struct of_device_id fixed_of_match[] __devinitconst = {
  241. { .compatible = "regulator-fixed", },
  242. {},
  243. };
  244. MODULE_DEVICE_TABLE(of, fixed_of_match);
  245. #else
  246. #define fixed_of_match NULL
  247. #endif
  248. static struct platform_driver regulator_fixed_voltage_driver = {
  249. .probe = reg_fixed_voltage_probe,
  250. .remove = __devexit_p(reg_fixed_voltage_remove),
  251. .driver = {
  252. .name = "reg-fixed-voltage",
  253. .owner = THIS_MODULE,
  254. .of_match_table = fixed_of_match,
  255. },
  256. };
  257. static int __init regulator_fixed_voltage_init(void)
  258. {
  259. return platform_driver_register(&regulator_fixed_voltage_driver);
  260. }
  261. subsys_initcall(regulator_fixed_voltage_init);
  262. static void __exit regulator_fixed_voltage_exit(void)
  263. {
  264. platform_driver_unregister(&regulator_fixed_voltage_driver);
  265. }
  266. module_exit(regulator_fixed_voltage_exit);
  267. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  268. MODULE_DESCRIPTION("Fixed voltage regulator");
  269. MODULE_LICENSE("GPL");
  270. MODULE_ALIAS("platform:reg-fixed-voltage");