gpio-sch.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * 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/pci_ids.h>
  28. #include <linux/gpio.h>
  29. static DEFINE_SPINLOCK(gpio_lock);
  30. #define CGEN (0x00)
  31. #define CGIO (0x04)
  32. #define CGLV (0x08)
  33. #define RGEN (0x20)
  34. #define RGIO (0x24)
  35. #define RGLV (0x28)
  36. static unsigned short gpio_ba;
  37. static int sch_gpio_core_direction_in(struct gpio_chip *gc, unsigned gpio_num)
  38. {
  39. u8 curr_dirs;
  40. unsigned short offset, bit;
  41. spin_lock(&gpio_lock);
  42. offset = CGIO + gpio_num / 8;
  43. bit = gpio_num % 8;
  44. curr_dirs = inb(gpio_ba + offset);
  45. if (!(curr_dirs & (1 << bit)))
  46. outb(curr_dirs | (1 << bit), gpio_ba + offset);
  47. spin_unlock(&gpio_lock);
  48. return 0;
  49. }
  50. static int sch_gpio_core_get(struct gpio_chip *gc, unsigned gpio_num)
  51. {
  52. int res;
  53. unsigned short offset, bit;
  54. offset = CGLV + gpio_num / 8;
  55. bit = gpio_num % 8;
  56. res = !!(inb(gpio_ba + offset) & (1 << bit));
  57. return res;
  58. }
  59. static void sch_gpio_core_set(struct gpio_chip *gc, unsigned gpio_num, int val)
  60. {
  61. u8 curr_vals;
  62. unsigned short offset, bit;
  63. spin_lock(&gpio_lock);
  64. offset = CGLV + gpio_num / 8;
  65. bit = gpio_num % 8;
  66. curr_vals = inb(gpio_ba + offset);
  67. if (val)
  68. outb(curr_vals | (1 << bit), gpio_ba + offset);
  69. else
  70. outb((curr_vals & ~(1 << bit)), gpio_ba + offset);
  71. spin_unlock(&gpio_lock);
  72. }
  73. static int sch_gpio_core_direction_out(struct gpio_chip *gc,
  74. unsigned gpio_num, int val)
  75. {
  76. u8 curr_dirs;
  77. unsigned short offset, bit;
  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. /*
  86. * according to the datasheet, writing to the level register has no
  87. * effect when GPIO is programmed as input.
  88. * Actually the the level register is read-only when configured as input.
  89. * Thus presetting the output level before switching to output is _NOT_ possible.
  90. * Hence we set the level after configuring the GPIO as output.
  91. * But we cannot prevent a short low pulse if direction is set to high
  92. * and an external pull-up is connected.
  93. */
  94. sch_gpio_core_set(gc, gpio_num, val);
  95. return 0;
  96. }
  97. static struct gpio_chip sch_gpio_core = {
  98. .label = "sch_gpio_core",
  99. .owner = THIS_MODULE,
  100. .direction_input = sch_gpio_core_direction_in,
  101. .get = sch_gpio_core_get,
  102. .direction_output = sch_gpio_core_direction_out,
  103. .set = sch_gpio_core_set,
  104. };
  105. static int sch_gpio_resume_direction_in(struct gpio_chip *gc,
  106. unsigned gpio_num)
  107. {
  108. u8 curr_dirs;
  109. unsigned short offset, bit;
  110. spin_lock(&gpio_lock);
  111. offset = RGIO + gpio_num / 8;
  112. bit = gpio_num % 8;
  113. curr_dirs = inb(gpio_ba + offset);
  114. if (!(curr_dirs & (1 << bit)))
  115. outb(curr_dirs | (1 << bit), gpio_ba + offset);
  116. spin_unlock(&gpio_lock);
  117. return 0;
  118. }
  119. static int sch_gpio_resume_get(struct gpio_chip *gc, unsigned gpio_num)
  120. {
  121. unsigned short offset, bit;
  122. offset = RGLV + gpio_num / 8;
  123. bit = gpio_num % 8;
  124. return !!(inb(gpio_ba + offset) & (1 << bit));
  125. }
  126. static void sch_gpio_resume_set(struct gpio_chip *gc,
  127. unsigned gpio_num, int val)
  128. {
  129. u8 curr_vals;
  130. unsigned short offset, bit;
  131. spin_lock(&gpio_lock);
  132. offset = RGLV + gpio_num / 8;
  133. bit = gpio_num % 8;
  134. curr_vals = inb(gpio_ba + offset);
  135. if (val)
  136. outb(curr_vals | (1 << bit), gpio_ba + offset);
  137. else
  138. outb((curr_vals & ~(1 << bit)), gpio_ba + offset);
  139. spin_unlock(&gpio_lock);
  140. }
  141. static int sch_gpio_resume_direction_out(struct gpio_chip *gc,
  142. unsigned gpio_num, int val)
  143. {
  144. u8 curr_dirs;
  145. unsigned short offset, bit;
  146. offset = RGIO + gpio_num / 8;
  147. bit = gpio_num % 8;
  148. spin_lock(&gpio_lock);
  149. curr_dirs = inb(gpio_ba + offset);
  150. if (curr_dirs & (1 << bit))
  151. outb(curr_dirs & ~(1 << bit), gpio_ba + offset);
  152. spin_unlock(&gpio_lock);
  153. /*
  154. * according to the datasheet, writing to the level register has no
  155. * effect when GPIO is programmed as input.
  156. * Actually the the level register is read-only when configured as input.
  157. * Thus presetting the output level before switching to output is _NOT_ possible.
  158. * Hence we set the level after configuring the GPIO as output.
  159. * But we cannot prevent a short low pulse if direction is set to high
  160. * and an external pull-up is connected.
  161. */
  162. sch_gpio_resume_set(gc, gpio_num, val);
  163. return 0;
  164. }
  165. static struct gpio_chip sch_gpio_resume = {
  166. .label = "sch_gpio_resume",
  167. .owner = THIS_MODULE,
  168. .direction_input = sch_gpio_resume_direction_in,
  169. .get = sch_gpio_resume_get,
  170. .direction_output = sch_gpio_resume_direction_out,
  171. .set = sch_gpio_resume_set,
  172. };
  173. static int sch_gpio_probe(struct platform_device *pdev)
  174. {
  175. struct resource *res;
  176. int err, id;
  177. id = pdev->id;
  178. if (!id)
  179. return -ENODEV;
  180. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  181. if (!res)
  182. return -EBUSY;
  183. if (!request_region(res->start, resource_size(res), pdev->name))
  184. return -EBUSY;
  185. gpio_ba = res->start;
  186. switch (id) {
  187. case PCI_DEVICE_ID_INTEL_SCH_LPC:
  188. sch_gpio_core.base = 0;
  189. sch_gpio_core.ngpio = 10;
  190. sch_gpio_resume.base = 10;
  191. sch_gpio_resume.ngpio = 4;
  192. /*
  193. * GPIO[6:0] enabled by default
  194. * GPIO7 is configured by the CMC as SLPIOVR
  195. * Enable GPIO[9:8] core powered gpios explicitly
  196. */
  197. outb(0x3, gpio_ba + CGEN + 1);
  198. /*
  199. * SUS_GPIO[2:0] enabled by default
  200. * Enable SUS_GPIO3 resume powered gpio explicitly
  201. */
  202. outb(0x8, gpio_ba + RGEN);
  203. break;
  204. case PCI_DEVICE_ID_INTEL_ITC_LPC:
  205. sch_gpio_core.base = 0;
  206. sch_gpio_core.ngpio = 5;
  207. sch_gpio_resume.base = 5;
  208. sch_gpio_resume.ngpio = 9;
  209. break;
  210. case PCI_DEVICE_ID_INTEL_CENTERTON_ILB:
  211. sch_gpio_core.base = 0;
  212. sch_gpio_core.ngpio = 21;
  213. sch_gpio_resume.base = 21;
  214. sch_gpio_resume.ngpio = 9;
  215. break;
  216. default:
  217. err = -ENODEV;
  218. goto err_sch_gpio_core;
  219. }
  220. sch_gpio_core.dev = &pdev->dev;
  221. sch_gpio_resume.dev = &pdev->dev;
  222. err = gpiochip_add(&sch_gpio_core);
  223. if (err < 0)
  224. goto err_sch_gpio_core;
  225. err = gpiochip_add(&sch_gpio_resume);
  226. if (err < 0)
  227. goto err_sch_gpio_resume;
  228. return 0;
  229. err_sch_gpio_resume:
  230. if (gpiochip_remove(&sch_gpio_core))
  231. dev_err(&pdev->dev, "%s gpiochip_remove failed\n", __func__);
  232. err_sch_gpio_core:
  233. release_region(res->start, resource_size(res));
  234. gpio_ba = 0;
  235. return err;
  236. }
  237. static int sch_gpio_remove(struct platform_device *pdev)
  238. {
  239. struct resource *res;
  240. if (gpio_ba) {
  241. int err;
  242. err = gpiochip_remove(&sch_gpio_core);
  243. if (err)
  244. dev_err(&pdev->dev, "%s failed, %d\n",
  245. "gpiochip_remove()", err);
  246. err = gpiochip_remove(&sch_gpio_resume);
  247. if (err)
  248. dev_err(&pdev->dev, "%s failed, %d\n",
  249. "gpiochip_remove()", err);
  250. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  251. release_region(res->start, resource_size(res));
  252. gpio_ba = 0;
  253. return err;
  254. }
  255. return 0;
  256. }
  257. static struct platform_driver sch_gpio_driver = {
  258. .driver = {
  259. .name = "sch_gpio",
  260. .owner = THIS_MODULE,
  261. },
  262. .probe = sch_gpio_probe,
  263. .remove = sch_gpio_remove,
  264. };
  265. module_platform_driver(sch_gpio_driver);
  266. MODULE_AUTHOR("Denis Turischev <denis@compulab.co.il>");
  267. MODULE_DESCRIPTION("GPIO interface for Intel Poulsbo SCH");
  268. MODULE_LICENSE("GPL");
  269. MODULE_ALIAS("platform:sch_gpio");