sch_gpio.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * sch_gpio.c - GPIO interface for Intel Poulsbo SCH
  3. *
  4. * Copyright (c) 2010 CompuLab Ltd
  5. * Author: Denis Turischev <denis@compulab.co.il>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License 2 as published
  9. * by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; see the file COPYING. If not, write to
  18. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/init.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/io.h>
  24. #include <linux/errno.h>
  25. #include <linux/acpi.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/gpio.h>
  28. static DEFINE_SPINLOCK(gpio_lock);
  29. #define CGEN (0x00)
  30. #define CGIO (0x04)
  31. #define CGLV (0x08)
  32. #define RGEN (0x20)
  33. #define RGIO (0x24)
  34. #define RGLV (0x28)
  35. static unsigned short gpio_ba;
  36. static int sch_gpio_core_direction_in(struct gpio_chip *gc, unsigned gpio_num)
  37. {
  38. u8 curr_dirs;
  39. unsigned short offset, bit;
  40. spin_lock(&gpio_lock);
  41. offset = CGIO + gpio_num / 8;
  42. bit = gpio_num % 8;
  43. curr_dirs = inb(gpio_ba + offset);
  44. if (!(curr_dirs & (1 << bit)))
  45. outb(curr_dirs | (1 << bit), gpio_ba + offset);
  46. spin_unlock(&gpio_lock);
  47. return 0;
  48. }
  49. static int sch_gpio_core_get(struct gpio_chip *gc, unsigned gpio_num)
  50. {
  51. int res;
  52. unsigned short offset, bit;
  53. offset = CGLV + gpio_num / 8;
  54. bit = gpio_num % 8;
  55. res = !!(inb(gpio_ba + offset) & (1 << bit));
  56. return res;
  57. }
  58. static void sch_gpio_core_set(struct gpio_chip *gc, unsigned gpio_num, int val)
  59. {
  60. u8 curr_vals;
  61. unsigned short offset, bit;
  62. spin_lock(&gpio_lock);
  63. offset = CGLV + gpio_num / 8;
  64. bit = gpio_num % 8;
  65. curr_vals = inb(gpio_ba + offset);
  66. if (val)
  67. outb(curr_vals | (1 << bit), gpio_ba + offset);
  68. else
  69. outb((curr_vals & ~(1 << bit)), gpio_ba + offset);
  70. spin_unlock(&gpio_lock);
  71. }
  72. static int sch_gpio_core_direction_out(struct gpio_chip *gc,
  73. unsigned gpio_num, int val)
  74. {
  75. u8 curr_dirs;
  76. unsigned short offset, bit;
  77. sch_gpio_core_set(gc, gpio_num, val);
  78. spin_lock(&gpio_lock);
  79. offset = CGIO + gpio_num / 8;
  80. bit = gpio_num % 8;
  81. curr_dirs = inb(gpio_ba + offset);
  82. if (curr_dirs & (1 << bit))
  83. outb(curr_dirs & ~(1 << bit), gpio_ba + offset);
  84. spin_unlock(&gpio_lock);
  85. return 0;
  86. }
  87. static struct gpio_chip sch_gpio_core = {
  88. .label = "sch_gpio_core",
  89. .owner = THIS_MODULE,
  90. .direction_input = sch_gpio_core_direction_in,
  91. .get = sch_gpio_core_get,
  92. .direction_output = sch_gpio_core_direction_out,
  93. .set = sch_gpio_core_set,
  94. };
  95. static int sch_gpio_resume_direction_in(struct gpio_chip *gc,
  96. unsigned gpio_num)
  97. {
  98. u8 curr_dirs;
  99. spin_lock(&gpio_lock);
  100. curr_dirs = inb(gpio_ba + RGIO);
  101. if (!(curr_dirs & (1 << gpio_num)))
  102. outb(curr_dirs | (1 << gpio_num) , gpio_ba + RGIO);
  103. spin_unlock(&gpio_lock);
  104. return 0;
  105. }
  106. static int sch_gpio_resume_get(struct gpio_chip *gc, unsigned gpio_num)
  107. {
  108. return !!(inb(gpio_ba + RGLV) & (1 << gpio_num));
  109. }
  110. static void sch_gpio_resume_set(struct gpio_chip *gc,
  111. unsigned gpio_num, int val)
  112. {
  113. u8 curr_vals;
  114. spin_lock(&gpio_lock);
  115. curr_vals = inb(gpio_ba + RGLV);
  116. if (val)
  117. outb(curr_vals | (1 << gpio_num), gpio_ba + RGLV);
  118. else
  119. outb((curr_vals & ~(1 << gpio_num)), gpio_ba + RGLV);
  120. spin_unlock(&gpio_lock);
  121. }
  122. static int sch_gpio_resume_direction_out(struct gpio_chip *gc,
  123. unsigned gpio_num, int val)
  124. {
  125. u8 curr_dirs;
  126. sch_gpio_resume_set(gc, gpio_num, val);
  127. spin_lock(&gpio_lock);
  128. curr_dirs = inb(gpio_ba + RGIO);
  129. if (curr_dirs & (1 << gpio_num))
  130. outb(curr_dirs & ~(1 << gpio_num), gpio_ba + RGIO);
  131. spin_unlock(&gpio_lock);
  132. return 0;
  133. }
  134. static struct gpio_chip sch_gpio_resume = {
  135. .label = "sch_gpio_resume",
  136. .owner = THIS_MODULE,
  137. .direction_input = sch_gpio_resume_direction_in,
  138. .get = sch_gpio_resume_get,
  139. .direction_output = sch_gpio_resume_direction_out,
  140. .set = sch_gpio_resume_set,
  141. };
  142. static int __devinit sch_gpio_probe(struct platform_device *pdev)
  143. {
  144. struct resource *res;
  145. int err;
  146. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  147. if (!res)
  148. return -EBUSY;
  149. if (!request_region(res->start, resource_size(res), pdev->name))
  150. return -EBUSY;
  151. gpio_ba = res->start;
  152. sch_gpio_core.base = 0;
  153. sch_gpio_core.ngpio = 10;
  154. sch_gpio_core.dev = &pdev->dev;
  155. sch_gpio_resume.base = 10;
  156. sch_gpio_resume.ngpio = 4;
  157. sch_gpio_resume.dev = &pdev->dev;
  158. err = gpiochip_add(&sch_gpio_core);
  159. if (err < 0)
  160. goto err_sch_gpio_core;
  161. err = gpiochip_add(&sch_gpio_resume);
  162. if (err < 0)
  163. goto err_sch_gpio_resume;
  164. /*
  165. * GPIO[6:0] enabled by default
  166. * GPIO7 is configured by the CMC as SLPIOVR
  167. * Enable GPIO[9:8] core powered gpios explicitly
  168. */
  169. outb(0x3, gpio_ba + CGEN + 1);
  170. /*
  171. * SUS_GPIO[2:0] enabled by default
  172. * Enable SUS_GPIO3 resume powered gpio explicitly
  173. */
  174. outb(0x8, gpio_ba + RGEN);
  175. return 0;
  176. err_sch_gpio_resume:
  177. err = gpiochip_remove(&sch_gpio_core);
  178. if (err)
  179. dev_err(&pdev->dev, "%s failed, %d\n",
  180. "gpiochip_remove()", err);
  181. err_sch_gpio_core:
  182. release_region(res->start, resource_size(res));
  183. gpio_ba = 0;
  184. return err;
  185. }
  186. static int __devexit sch_gpio_remove(struct platform_device *pdev)
  187. {
  188. struct resource *res;
  189. if (gpio_ba) {
  190. int err;
  191. err = gpiochip_remove(&sch_gpio_core);
  192. if (err)
  193. dev_err(&pdev->dev, "%s failed, %d\n",
  194. "gpiochip_remove()", err);
  195. err = gpiochip_remove(&sch_gpio_resume);
  196. if (err)
  197. dev_err(&pdev->dev, "%s failed, %d\n",
  198. "gpiochip_remove()", err);
  199. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  200. release_region(res->start, resource_size(res));
  201. gpio_ba = 0;
  202. return err;
  203. }
  204. return 0;
  205. }
  206. static struct platform_driver sch_gpio_driver = {
  207. .driver = {
  208. .name = "sch_gpio",
  209. .owner = THIS_MODULE,
  210. },
  211. .probe = sch_gpio_probe,
  212. .remove = __devexit_p(sch_gpio_remove),
  213. };
  214. static int __init sch_gpio_init(void)
  215. {
  216. return platform_driver_register(&sch_gpio_driver);
  217. }
  218. static void __exit sch_gpio_exit(void)
  219. {
  220. platform_driver_unregister(&sch_gpio_driver);
  221. }
  222. module_init(sch_gpio_init);
  223. module_exit(sch_gpio_exit);
  224. MODULE_AUTHOR("Denis Turischev <denis@compulab.co.il>");
  225. MODULE_DESCRIPTION("GPIO interface for Intel Poulsbo SCH");
  226. MODULE_LICENSE("GPL");
  227. MODULE_ALIAS("platform:sch_gpio");