gpio-pm8xxx-rpc.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Qualcomm PMIC8XXX GPIO driver based on RPC
  3. *
  4. * Copyright (c) 2011, The Linux Foundation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 and
  8. * only version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #define pr_fmt(fmt) "%s: " fmt, __func__
  16. #include <linux/platform_device.h>
  17. #include <linux/gpio.h>
  18. #include <linux/gpio-pm8xxx-rpc.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/fs.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/slab.h>
  23. #include <mach/pmic.h>
  24. struct pm8xxx_gpio_rpc_chip {
  25. struct list_head link;
  26. struct gpio_chip gpio_chip;
  27. };
  28. static LIST_HEAD(pm8xxx_gpio_rpc_chips);
  29. static DEFINE_MUTEX(pm8xxx_gpio_chips_lock);
  30. static int pm8xxx_gpio_rpc_get(struct pm8xxx_gpio_rpc_chip *pm8xxx_gpio_chip,
  31. unsigned gpio)
  32. {
  33. int rc;
  34. if (gpio >= pm8xxx_gpio_chip->gpio_chip.ngpio
  35. || pm8xxx_gpio_chip == NULL)
  36. return -EINVAL;
  37. rc = pmic_gpio_get_value(gpio);
  38. return rc;
  39. }
  40. static int pm8xxx_gpio_rpc_set(struct pm8xxx_gpio_rpc_chip *pm8xxx_gpio_chip,
  41. unsigned gpio, int value)
  42. {
  43. int rc;
  44. if (gpio >= pm8xxx_gpio_chip->gpio_chip.ngpio ||
  45. pm8xxx_gpio_chip == NULL)
  46. return -EINVAL;
  47. rc = pmic_gpio_set_value(gpio, value);
  48. return rc;
  49. }
  50. static int pm8xxx_gpio_rpc_set_direction(struct pm8xxx_gpio_rpc_chip
  51. *pm8xxx_gpio_chip, unsigned gpio, int direction)
  52. {
  53. int rc = 0;
  54. if (!direction || pm8xxx_gpio_chip == NULL)
  55. return -EINVAL;
  56. if (direction == PM_GPIO_DIR_IN)
  57. rc = pmic_gpio_direction_input(gpio);
  58. else if (direction == PM_GPIO_DIR_OUT)
  59. rc = pmic_gpio_direction_output(gpio);
  60. return rc;
  61. }
  62. static int pm8xxx_gpio_rpc_read(struct gpio_chip *gpio_chip, unsigned offset)
  63. {
  64. struct pm8xxx_gpio_rpc_chip *pm8xxx_gpio_chip =
  65. dev_get_drvdata(gpio_chip->dev);
  66. return pm8xxx_gpio_rpc_get(pm8xxx_gpio_chip, offset);
  67. }
  68. static void pm8xxx_gpio_rpc_write(struct gpio_chip *gpio_chip,
  69. unsigned offset, int val)
  70. {
  71. struct pm8xxx_gpio_rpc_chip *pm8xxx_gpio_chip =
  72. dev_get_drvdata(gpio_chip->dev);
  73. pm8xxx_gpio_rpc_set(pm8xxx_gpio_chip, offset, !!val);
  74. }
  75. static int pm8xxx_gpio_rpc_direction_input(struct gpio_chip *gpio_chip,
  76. unsigned offset)
  77. {
  78. struct pm8xxx_gpio_rpc_chip *pm8xxx_gpio_chip =
  79. dev_get_drvdata(gpio_chip->dev);
  80. return pm8xxx_gpio_rpc_set_direction(pm8xxx_gpio_chip, offset,
  81. PM_GPIO_DIR_IN);
  82. }
  83. static int pm8xxx_gpio_rpc_direction_output(struct gpio_chip *gpio_chip,
  84. unsigned offset, int val)
  85. {
  86. int ret = 0;
  87. struct pm8xxx_gpio_rpc_chip *pm8xxx_gpio_chip =
  88. dev_get_drvdata(gpio_chip->dev);
  89. ret = pm8xxx_gpio_rpc_set_direction(pm8xxx_gpio_chip, offset,
  90. PM_GPIO_DIR_OUT);
  91. if (!ret)
  92. ret = pm8xxx_gpio_rpc_set(pm8xxx_gpio_chip, offset, !!val);
  93. return ret;
  94. }
  95. static void pm8xxx_gpio_rpc_dbg_show(struct seq_file *s, struct gpio_chip
  96. *gpio_chip)
  97. {
  98. struct pm8xxx_gpio_rpc_chip *pmxx_gpio_chip =
  99. dev_get_drvdata(gpio_chip->dev);
  100. u8 state, mode;
  101. const char *label;
  102. int i;
  103. for (i = 0; i < gpio_chip->ngpio; i++) {
  104. label = gpiochip_is_requested(gpio_chip, i);
  105. state = pm8xxx_gpio_rpc_get(pmxx_gpio_chip, i);
  106. mode = pmic_gpio_get_direction(i);
  107. seq_printf(s, "gpio-%-3d (%-12.12s) %s %s",
  108. gpio_chip->base + i,
  109. label ? label : " ", mode ? "out" : "in",
  110. state ? "hi" : "lo");
  111. seq_printf(s, "\n");
  112. }
  113. }
  114. static int __devinit pm8xxx_gpio_rpc_probe(struct platform_device *pdev)
  115. {
  116. int ret;
  117. struct pm8xxx_gpio_rpc_chip *pm8xxx_gpio_chip;
  118. const struct pm8xxx_gpio_rpc_platform_data *pdata =
  119. pdev->dev.platform_data;
  120. if (!pdata) {
  121. pr_err("missing platform data\n");
  122. return -EINVAL;
  123. }
  124. pm8xxx_gpio_chip = kzalloc(sizeof(struct pm8xxx_gpio_rpc_chip),
  125. GFP_KERNEL);
  126. if (!pm8xxx_gpio_chip) {
  127. pr_err("Cannot allocate pm8xxx_gpio_chip\n");
  128. return -ENOMEM;
  129. }
  130. pm8xxx_gpio_chip->gpio_chip.label = "pm8xxx-gpio-rpc";
  131. pm8xxx_gpio_chip->gpio_chip.direction_input =
  132. pm8xxx_gpio_rpc_direction_input;
  133. pm8xxx_gpio_chip->gpio_chip.direction_output =
  134. pm8xxx_gpio_rpc_direction_output;
  135. pm8xxx_gpio_chip->gpio_chip.get = pm8xxx_gpio_rpc_read;
  136. pm8xxx_gpio_chip->gpio_chip.set = pm8xxx_gpio_rpc_write;
  137. pm8xxx_gpio_chip->gpio_chip.dbg_show = pm8xxx_gpio_rpc_dbg_show;
  138. pm8xxx_gpio_chip->gpio_chip.ngpio = pdata->ngpios;
  139. pm8xxx_gpio_chip->gpio_chip.can_sleep = 1;
  140. pm8xxx_gpio_chip->gpio_chip.dev = &pdev->dev;
  141. pm8xxx_gpio_chip->gpio_chip.base = pdata->gpio_base;
  142. mutex_lock(&pm8xxx_gpio_chips_lock);
  143. list_add(&pm8xxx_gpio_chip->link, &pm8xxx_gpio_rpc_chips);
  144. mutex_unlock(&pm8xxx_gpio_chips_lock);
  145. platform_set_drvdata(pdev, pm8xxx_gpio_chip);
  146. ret = gpiochip_add(&pm8xxx_gpio_chip->gpio_chip);
  147. if (ret) {
  148. pr_err("gpiochip_add failed ret = %d\n", ret);
  149. goto reset_drvdata;
  150. }
  151. pr_info("OK: base=%d, ngpio=%d\n", pm8xxx_gpio_chip->gpio_chip.base,
  152. pm8xxx_gpio_chip->gpio_chip.ngpio);
  153. return 0;
  154. reset_drvdata:
  155. mutex_lock(&pm8xxx_gpio_chips_lock);
  156. list_del(&pm8xxx_gpio_chip->link);
  157. mutex_unlock(&pm8xxx_gpio_chips_lock);
  158. platform_set_drvdata(pdev, NULL);
  159. kfree(pm8xxx_gpio_chip);
  160. mutex_destroy(&pm8xxx_gpio_chips_lock);
  161. return ret;
  162. }
  163. static int __devexit pm8xxx_gpio_rpc_remove(struct platform_device *pdev)
  164. {
  165. struct pm8xxx_gpio_rpc_chip *pm8xxx_gpio_chip =
  166. platform_get_drvdata(pdev);
  167. mutex_lock(&pm8xxx_gpio_chips_lock);
  168. list_del(&pm8xxx_gpio_chip->link);
  169. mutex_unlock(&pm8xxx_gpio_chips_lock);
  170. platform_set_drvdata(pdev, NULL);
  171. if (gpiochip_remove(&pm8xxx_gpio_chip->gpio_chip))
  172. pr_err("failed to remove gpio chip\n");
  173. kfree(pm8xxx_gpio_chip);
  174. mutex_destroy(&pm8xxx_gpio_chips_lock);
  175. return 0;
  176. }
  177. static struct platform_driver pm8xxx_gpio_rpc_driver = {
  178. .probe = pm8xxx_gpio_rpc_probe,
  179. .remove = __devexit_p(pm8xxx_gpio_rpc_remove),
  180. .driver = {
  181. .name = PM8XXX_GPIO_DEV_NAME,
  182. .owner = THIS_MODULE,
  183. },
  184. };
  185. static int __init pm8xxx_gpio_rpc_init(void)
  186. {
  187. return platform_driver_register(&pm8xxx_gpio_rpc_driver);
  188. }
  189. postcore_initcall(pm8xxx_gpio_rpc_init);
  190. static void __exit pm8xxx_gpio_rpc_exit(void)
  191. {
  192. platform_driver_unregister(&pm8xxx_gpio_rpc_driver);
  193. }
  194. module_exit(pm8xxx_gpio_rpc_exit);
  195. MODULE_LICENSE("GPL v2");
  196. MODULE_DESCRIPTION("PMIC GPIO driver based on RPC");
  197. MODULE_VERSION("1.0");
  198. MODULE_ALIAS("platform:" PM8XXX_GPIO_DEV_NAME);