gpio-vx855.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Linux GPIOlib driver for the VIA VX855 integrated southbridge GPIO
  3. *
  4. * Copyright (C) 2009 VIA Technologies, Inc.
  5. * Copyright (C) 2010 One Laptop per Child
  6. * Author: Harald Welte <HaraldWelte@viatech.com>
  7. * All rights reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. *
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/gpio.h>
  28. #include <linux/slab.h>
  29. #include <linux/device.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/pci.h>
  32. #include <linux/io.h>
  33. #define MODULE_NAME "vx855_gpio"
  34. /* The VX855 south bridge has the following GPIO pins:
  35. * GPI 0...13 General Purpose Input
  36. * GPO 0...12 General Purpose Output
  37. * GPIO 0...14 General Purpose I/O (Open-Drain)
  38. */
  39. #define NR_VX855_GPI 14
  40. #define NR_VX855_GPO 13
  41. #define NR_VX855_GPIO 15
  42. #define NR_VX855_GPInO (NR_VX855_GPI + NR_VX855_GPO)
  43. #define NR_VX855_GP (NR_VX855_GPI + NR_VX855_GPO + NR_VX855_GPIO)
  44. struct vx855_gpio {
  45. struct gpio_chip gpio;
  46. spinlock_t lock;
  47. u32 io_gpi;
  48. u32 io_gpo;
  49. };
  50. /* resolve a GPIx into the corresponding bit position */
  51. static inline u_int32_t gpi_i_bit(int i)
  52. {
  53. if (i < 10)
  54. return 1 << i;
  55. else
  56. return 1 << (i + 14);
  57. }
  58. static inline u_int32_t gpo_o_bit(int i)
  59. {
  60. if (i < 11)
  61. return 1 << i;
  62. else
  63. return 1 << (i + 14);
  64. }
  65. static inline u_int32_t gpio_i_bit(int i)
  66. {
  67. if (i < 14)
  68. return 1 << (i + 10);
  69. else
  70. return 1 << (i + 14);
  71. }
  72. static inline u_int32_t gpio_o_bit(int i)
  73. {
  74. if (i < 14)
  75. return 1 << (i + 11);
  76. else
  77. return 1 << (i + 13);
  78. }
  79. /* Mapping betwee numeric GPIO ID and the actual GPIO hardware numbering:
  80. * 0..13 GPI 0..13
  81. * 14..26 GPO 0..12
  82. * 27..41 GPIO 0..14
  83. */
  84. static int vx855gpio_direction_input(struct gpio_chip *gpio,
  85. unsigned int nr)
  86. {
  87. struct vx855_gpio *vg = gpiochip_get_data(gpio);
  88. unsigned long flags;
  89. u_int32_t reg_out;
  90. /* Real GPI bits are always in input direction */
  91. if (nr < NR_VX855_GPI)
  92. return 0;
  93. /* Real GPO bits cannot be put in output direction */
  94. if (nr < NR_VX855_GPInO)
  95. return -EINVAL;
  96. /* Open Drain GPIO have to be set to one */
  97. spin_lock_irqsave(&vg->lock, flags);
  98. reg_out = inl(vg->io_gpo);
  99. reg_out |= gpio_o_bit(nr - NR_VX855_GPInO);
  100. outl(reg_out, vg->io_gpo);
  101. spin_unlock_irqrestore(&vg->lock, flags);
  102. return 0;
  103. }
  104. static int vx855gpio_get(struct gpio_chip *gpio, unsigned int nr)
  105. {
  106. struct vx855_gpio *vg = gpiochip_get_data(gpio);
  107. u_int32_t reg_in;
  108. int ret = 0;
  109. if (nr < NR_VX855_GPI) {
  110. reg_in = inl(vg->io_gpi);
  111. if (reg_in & gpi_i_bit(nr))
  112. ret = 1;
  113. } else if (nr < NR_VX855_GPInO) {
  114. /* GPO don't have an input bit, we need to read it
  115. * back from the output register */
  116. reg_in = inl(vg->io_gpo);
  117. if (reg_in & gpo_o_bit(nr - NR_VX855_GPI))
  118. ret = 1;
  119. } else {
  120. reg_in = inl(vg->io_gpi);
  121. if (reg_in & gpio_i_bit(nr - NR_VX855_GPInO))
  122. ret = 1;
  123. }
  124. return ret;
  125. }
  126. static void vx855gpio_set(struct gpio_chip *gpio, unsigned int nr,
  127. int val)
  128. {
  129. struct vx855_gpio *vg = gpiochip_get_data(gpio);
  130. unsigned long flags;
  131. u_int32_t reg_out;
  132. /* True GPI cannot be switched to output mode */
  133. if (nr < NR_VX855_GPI)
  134. return;
  135. spin_lock_irqsave(&vg->lock, flags);
  136. reg_out = inl(vg->io_gpo);
  137. if (nr < NR_VX855_GPInO) {
  138. if (val)
  139. reg_out |= gpo_o_bit(nr - NR_VX855_GPI);
  140. else
  141. reg_out &= ~gpo_o_bit(nr - NR_VX855_GPI);
  142. } else {
  143. if (val)
  144. reg_out |= gpio_o_bit(nr - NR_VX855_GPInO);
  145. else
  146. reg_out &= ~gpio_o_bit(nr - NR_VX855_GPInO);
  147. }
  148. outl(reg_out, vg->io_gpo);
  149. spin_unlock_irqrestore(&vg->lock, flags);
  150. }
  151. static int vx855gpio_direction_output(struct gpio_chip *gpio,
  152. unsigned int nr, int val)
  153. {
  154. /* True GPI cannot be switched to output mode */
  155. if (nr < NR_VX855_GPI)
  156. return -EINVAL;
  157. /* True GPO don't need to be switched to output mode,
  158. * and GPIO are open-drain, i.e. also need no switching,
  159. * so all we do is set the level */
  160. vx855gpio_set(gpio, nr, val);
  161. return 0;
  162. }
  163. static int vx855gpio_set_single_ended(struct gpio_chip *gpio,
  164. unsigned int nr,
  165. enum single_ended_mode mode)
  166. {
  167. /* The GPI cannot be single-ended */
  168. if (nr < NR_VX855_GPI)
  169. return -EINVAL;
  170. /* The GPO's are push-pull */
  171. if (nr < NR_VX855_GPInO) {
  172. if (mode != LINE_MODE_PUSH_PULL)
  173. return -ENOTSUPP;
  174. return 0;
  175. }
  176. /* The GPIO's are open drain */
  177. if (mode != LINE_MODE_OPEN_DRAIN)
  178. return -ENOTSUPP;
  179. return 0;
  180. }
  181. static const char *vx855gpio_names[NR_VX855_GP] = {
  182. "VX855_GPI0", "VX855_GPI1", "VX855_GPI2", "VX855_GPI3", "VX855_GPI4",
  183. "VX855_GPI5", "VX855_GPI6", "VX855_GPI7", "VX855_GPI8", "VX855_GPI9",
  184. "VX855_GPI10", "VX855_GPI11", "VX855_GPI12", "VX855_GPI13",
  185. "VX855_GPO0", "VX855_GPO1", "VX855_GPO2", "VX855_GPO3", "VX855_GPO4",
  186. "VX855_GPO5", "VX855_GPO6", "VX855_GPO7", "VX855_GPO8", "VX855_GPO9",
  187. "VX855_GPO10", "VX855_GPO11", "VX855_GPO12",
  188. "VX855_GPIO0", "VX855_GPIO1", "VX855_GPIO2", "VX855_GPIO3",
  189. "VX855_GPIO4", "VX855_GPIO5", "VX855_GPIO6", "VX855_GPIO7",
  190. "VX855_GPIO8", "VX855_GPIO9", "VX855_GPIO10", "VX855_GPIO11",
  191. "VX855_GPIO12", "VX855_GPIO13", "VX855_GPIO14"
  192. };
  193. static void vx855gpio_gpio_setup(struct vx855_gpio *vg)
  194. {
  195. struct gpio_chip *c = &vg->gpio;
  196. c->label = "VX855 South Bridge";
  197. c->owner = THIS_MODULE;
  198. c->direction_input = vx855gpio_direction_input;
  199. c->direction_output = vx855gpio_direction_output;
  200. c->get = vx855gpio_get;
  201. c->set = vx855gpio_set;
  202. c->set_single_ended = vx855gpio_set_single_ended;
  203. c->dbg_show = NULL;
  204. c->base = 0;
  205. c->ngpio = NR_VX855_GP;
  206. c->can_sleep = false;
  207. c->names = vx855gpio_names;
  208. }
  209. /* This platform device is ordinarily registered by the vx855 mfd driver */
  210. static int vx855gpio_probe(struct platform_device *pdev)
  211. {
  212. struct resource *res_gpi;
  213. struct resource *res_gpo;
  214. struct vx855_gpio *vg;
  215. res_gpi = platform_get_resource(pdev, IORESOURCE_IO, 0);
  216. res_gpo = platform_get_resource(pdev, IORESOURCE_IO, 1);
  217. if (!res_gpi || !res_gpo)
  218. return -EBUSY;
  219. vg = devm_kzalloc(&pdev->dev, sizeof(*vg), GFP_KERNEL);
  220. if (!vg)
  221. return -ENOMEM;
  222. platform_set_drvdata(pdev, vg);
  223. dev_info(&pdev->dev, "found VX855 GPIO controller\n");
  224. vg->io_gpi = res_gpi->start;
  225. vg->io_gpo = res_gpo->start;
  226. spin_lock_init(&vg->lock);
  227. /*
  228. * A single byte is used to control various GPIO ports on the VX855,
  229. * and in the case of the OLPC XO-1.5, some of those ports are used
  230. * for switches that are interpreted and exposed through ACPI. ACPI
  231. * will have reserved the region, so our own reservation will not
  232. * succeed. Ignore and continue.
  233. */
  234. if (!devm_request_region(&pdev->dev, res_gpi->start,
  235. resource_size(res_gpi), MODULE_NAME "_gpi"))
  236. dev_warn(&pdev->dev,
  237. "GPI I/O resource busy, probably claimed by ACPI\n");
  238. if (!devm_request_region(&pdev->dev, res_gpo->start,
  239. resource_size(res_gpo), MODULE_NAME "_gpo"))
  240. dev_warn(&pdev->dev,
  241. "GPO I/O resource busy, probably claimed by ACPI\n");
  242. vx855gpio_gpio_setup(vg);
  243. return devm_gpiochip_add_data(&pdev->dev, &vg->gpio, vg);
  244. }
  245. static struct platform_driver vx855gpio_driver = {
  246. .driver = {
  247. .name = MODULE_NAME,
  248. },
  249. .probe = vx855gpio_probe,
  250. };
  251. module_platform_driver(vx855gpio_driver);
  252. MODULE_LICENSE("GPL");
  253. MODULE_AUTHOR("Harald Welte <HaraldWelte@viatech.com>");
  254. MODULE_DESCRIPTION("GPIO driver for the VIA VX855 chipset");
  255. MODULE_ALIAS("platform:vx855_gpio");