msm-gpio-regulator.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #define pr_fmt(fmt) "%s: " fmt, __func__
  14. #include <linux/module.h>
  15. #include <linux/err.h>
  16. #include <linux/kernel.h>
  17. #include <linux/init.h>
  18. #include <linux/slab.h>
  19. #include <linux/string.h>
  20. #include <linux/gpio.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/regulator/driver.h>
  23. #include <linux/regulator/machine.h>
  24. #include <linux/regulator/msm-gpio-regulator.h>
  25. struct gpio_vreg {
  26. struct regulator_desc desc;
  27. struct regulator_dev *rdev;
  28. char *gpio_label;
  29. char *name;
  30. unsigned gpio;
  31. int active_low;
  32. bool gpio_requested;
  33. };
  34. static int gpio_vreg_request_gpio(struct gpio_vreg *vreg)
  35. {
  36. int rc = 0;
  37. /* Request GPIO now if it hasn't been requested before. */
  38. if (!vreg->gpio_requested) {
  39. rc = gpio_request(vreg->gpio, vreg->gpio_label);
  40. if (rc < 0) {
  41. pr_err("failed to request gpio %u (%s), rc=%d\n",
  42. vreg->gpio, vreg->gpio_label, rc);
  43. return rc;
  44. } else {
  45. vreg->gpio_requested = true;
  46. }
  47. rc = gpio_sysfs_set_active_low(vreg->gpio, vreg->active_low);
  48. if (rc < 0)
  49. pr_err("active_low=%d failed for gpio %u, rc=%d\n",
  50. vreg->active_low, vreg->gpio, rc);
  51. }
  52. return rc;
  53. }
  54. static int gpio_vreg_is_enabled(struct regulator_dev *rdev)
  55. {
  56. struct gpio_vreg *vreg = rdev_get_drvdata(rdev);
  57. int rc;
  58. rc = gpio_vreg_request_gpio(vreg);
  59. if (rc < 0)
  60. return rc;
  61. return (gpio_get_value_cansleep(vreg->gpio) ? 1 : 0) ^ vreg->active_low;
  62. }
  63. static int gpio_vreg_enable(struct regulator_dev *rdev)
  64. {
  65. struct gpio_vreg *vreg = rdev_get_drvdata(rdev);
  66. int rc;
  67. rc = gpio_vreg_request_gpio(vreg);
  68. if (rc < 0)
  69. return rc;
  70. return gpio_direction_output(vreg->gpio, !vreg->active_low);
  71. }
  72. static int gpio_vreg_disable(struct regulator_dev *rdev)
  73. {
  74. struct gpio_vreg *vreg = rdev_get_drvdata(rdev);
  75. int rc;
  76. rc = gpio_vreg_request_gpio(vreg);
  77. if (rc < 0)
  78. return rc;
  79. return gpio_direction_output(vreg->gpio, vreg->active_low);
  80. }
  81. static struct regulator_ops gpio_vreg_ops = {
  82. .enable = gpio_vreg_enable,
  83. .disable = gpio_vreg_disable,
  84. .is_enabled = gpio_vreg_is_enabled,
  85. };
  86. static int __devinit gpio_vreg_probe(struct platform_device *pdev)
  87. {
  88. const struct gpio_regulator_platform_data *pdata;
  89. struct gpio_vreg *vreg;
  90. int rc = 0;
  91. pdata = pdev->dev.platform_data;
  92. if (!pdata) {
  93. pr_err("platform data required.\n");
  94. return -EINVAL;
  95. }
  96. if (!pdata->gpio_label) {
  97. pr_err("gpio_label required.\n");
  98. return -EINVAL;
  99. }
  100. if (!pdata->regulator_name) {
  101. pr_err("regulator_name required.\n");
  102. return -EINVAL;
  103. }
  104. vreg = kzalloc(sizeof(struct gpio_vreg), GFP_KERNEL);
  105. if (!vreg) {
  106. pr_err("kzalloc failed.\n");
  107. return -ENOMEM;
  108. }
  109. vreg->name = kstrdup(pdata->regulator_name, GFP_KERNEL);
  110. if (!vreg->name) {
  111. pr_err("kzalloc failed.\n");
  112. rc = -ENOMEM;
  113. goto free_vreg;
  114. }
  115. vreg->gpio_label = kstrdup(pdata->gpio_label, GFP_KERNEL);
  116. if (!vreg->gpio_label) {
  117. pr_err("kzalloc failed.\n");
  118. rc = -ENOMEM;
  119. goto free_name;
  120. }
  121. vreg->gpio = pdata->gpio;
  122. vreg->active_low = (pdata->active_low ? 1 : 0);
  123. vreg->gpio_requested = false;
  124. vreg->desc.name = vreg->name;
  125. vreg->desc.id = pdev->id;
  126. vreg->desc.ops = &gpio_vreg_ops;
  127. vreg->desc.type = REGULATOR_VOLTAGE;
  128. vreg->desc.owner = THIS_MODULE;
  129. vreg->rdev = regulator_register(&vreg->desc, &pdev->dev,
  130. &pdata->init_data, vreg, NULL);
  131. if (IS_ERR(vreg->rdev)) {
  132. rc = PTR_ERR(vreg->rdev);
  133. pr_err("%s: regulator_register failed, rc=%d.\n", vreg->name,
  134. rc);
  135. goto free_gpio_label;
  136. }
  137. platform_set_drvdata(pdev, vreg);
  138. pr_info("id=%d, name=%s, gpio=%u, gpio_label=%s\n", pdev->id,
  139. vreg->name, vreg->gpio, vreg->gpio_label);
  140. return rc;
  141. free_gpio_label:
  142. kfree(vreg->gpio_label);
  143. free_name:
  144. kfree(vreg->name);
  145. free_vreg:
  146. kfree(vreg);
  147. return rc;
  148. }
  149. static int __devexit gpio_vreg_remove(struct platform_device *pdev)
  150. {
  151. struct gpio_vreg *vreg = platform_get_drvdata(pdev);
  152. if (vreg->gpio_requested)
  153. gpio_free(vreg->gpio);
  154. regulator_unregister(vreg->rdev);
  155. kfree(vreg->name);
  156. kfree(vreg->gpio_label);
  157. kfree(vreg);
  158. platform_set_drvdata(pdev, NULL);
  159. return 0;
  160. }
  161. static struct platform_driver gpio_vreg_driver = {
  162. .probe = gpio_vreg_probe,
  163. .remove = __devexit_p(gpio_vreg_remove),
  164. .driver = {
  165. .name = GPIO_REGULATOR_DEV_NAME,
  166. .owner = THIS_MODULE,
  167. },
  168. };
  169. static int __init gpio_vreg_init(void)
  170. {
  171. return platform_driver_register(&gpio_vreg_driver);
  172. }
  173. static void __exit gpio_vreg_exit(void)
  174. {
  175. platform_driver_unregister(&gpio_vreg_driver);
  176. }
  177. postcore_initcall(gpio_vreg_init);
  178. module_exit(gpio_vreg_exit);
  179. MODULE_LICENSE("GPL v2");
  180. MODULE_DESCRIPTION("GPIO regulator driver");
  181. MODULE_VERSION("1.0");
  182. MODULE_ALIAS("platform:" GPIO_REGULATOR_DEV_NAME);