gpio-sch.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. #define GEN 0x00
  30. #define GIO 0x04
  31. #define GLV 0x08
  32. struct sch_gpio {
  33. struct gpio_chip chip;
  34. spinlock_t lock;
  35. unsigned short iobase;
  36. unsigned short core_base;
  37. unsigned short resume_base;
  38. };
  39. #define to_sch_gpio(gc) container_of(gc, struct sch_gpio, chip)
  40. static unsigned sch_gpio_offset(struct sch_gpio *sch, unsigned gpio,
  41. unsigned reg)
  42. {
  43. unsigned base = 0;
  44. if (gpio >= sch->resume_base) {
  45. gpio -= sch->resume_base;
  46. base += 0x20;
  47. }
  48. return base + reg + gpio / 8;
  49. }
  50. static unsigned sch_gpio_bit(struct sch_gpio *sch, unsigned gpio)
  51. {
  52. if (gpio >= sch->resume_base)
  53. gpio -= sch->resume_base;
  54. return gpio % 8;
  55. }
  56. static int sch_gpio_reg_get(struct gpio_chip *gc, unsigned gpio, unsigned reg)
  57. {
  58. struct sch_gpio *sch = to_sch_gpio(gc);
  59. unsigned short offset, bit;
  60. u8 reg_val;
  61. offset = sch_gpio_offset(sch, gpio, reg);
  62. bit = sch_gpio_bit(sch, gpio);
  63. reg_val = !!(inb(sch->iobase + offset) & BIT(bit));
  64. return reg_val;
  65. }
  66. static void sch_gpio_reg_set(struct gpio_chip *gc, unsigned gpio, unsigned reg,
  67. int val)
  68. {
  69. struct sch_gpio *sch = to_sch_gpio(gc);
  70. unsigned short offset, bit;
  71. u8 reg_val;
  72. offset = sch_gpio_offset(sch, gpio, reg);
  73. bit = sch_gpio_bit(sch, gpio);
  74. reg_val = inb(sch->iobase + offset);
  75. if (val)
  76. outb(reg_val | BIT(bit), sch->iobase + offset);
  77. else
  78. outb((reg_val & ~BIT(bit)), sch->iobase + offset);
  79. }
  80. static int sch_gpio_direction_in(struct gpio_chip *gc, unsigned gpio_num)
  81. {
  82. struct sch_gpio *sch = to_sch_gpio(gc);
  83. spin_lock(&sch->lock);
  84. sch_gpio_reg_set(gc, gpio_num, GIO, 1);
  85. spin_unlock(&sch->lock);
  86. return 0;
  87. }
  88. static int sch_gpio_get(struct gpio_chip *gc, unsigned gpio_num)
  89. {
  90. return sch_gpio_reg_get(gc, gpio_num, GLV);
  91. }
  92. static void sch_gpio_set(struct gpio_chip *gc, unsigned gpio_num, int val)
  93. {
  94. struct sch_gpio *sch = to_sch_gpio(gc);
  95. spin_lock(&sch->lock);
  96. sch_gpio_reg_set(gc, gpio_num, GLV, val);
  97. spin_unlock(&sch->lock);
  98. }
  99. static int sch_gpio_direction_out(struct gpio_chip *gc, unsigned gpio_num,
  100. int val)
  101. {
  102. struct sch_gpio *sch = to_sch_gpio(gc);
  103. spin_lock(&sch->lock);
  104. sch_gpio_reg_set(gc, gpio_num, GIO, 0);
  105. spin_unlock(&sch->lock);
  106. /*
  107. * according to the datasheet, writing to the level register has no
  108. * effect when GPIO is programmed as input.
  109. * Actually the the level register is read-only when configured as input.
  110. * Thus presetting the output level before switching to output is _NOT_ possible.
  111. * Hence we set the level after configuring the GPIO as output.
  112. * But we cannot prevent a short low pulse if direction is set to high
  113. * and an external pull-up is connected.
  114. */
  115. sch_gpio_set(gc, gpio_num, val);
  116. return 0;
  117. }
  118. static struct gpio_chip sch_gpio_chip = {
  119. .label = "sch_gpio",
  120. .owner = THIS_MODULE,
  121. .direction_input = sch_gpio_direction_in,
  122. .get = sch_gpio_get,
  123. .direction_output = sch_gpio_direction_out,
  124. .set = sch_gpio_set,
  125. };
  126. static int sch_gpio_probe(struct platform_device *pdev)
  127. {
  128. struct sch_gpio *sch;
  129. struct resource *res;
  130. sch = devm_kzalloc(&pdev->dev, sizeof(*sch), GFP_KERNEL);
  131. if (!sch)
  132. return -ENOMEM;
  133. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  134. if (!res)
  135. return -EBUSY;
  136. if (!devm_request_region(&pdev->dev, res->start, resource_size(res),
  137. pdev->name))
  138. return -EBUSY;
  139. spin_lock_init(&sch->lock);
  140. sch->iobase = res->start;
  141. sch->chip = sch_gpio_chip;
  142. sch->chip.label = dev_name(&pdev->dev);
  143. sch->chip.dev = &pdev->dev;
  144. switch (pdev->id) {
  145. case PCI_DEVICE_ID_INTEL_SCH_LPC:
  146. sch->core_base = 0;
  147. sch->resume_base = 10;
  148. sch->chip.ngpio = 14;
  149. /*
  150. * GPIO[6:0] enabled by default
  151. * GPIO7 is configured by the CMC as SLPIOVR
  152. * Enable GPIO[9:8] core powered gpios explicitly
  153. */
  154. sch_gpio_reg_set(&sch->chip, 8, GEN, 1);
  155. sch_gpio_reg_set(&sch->chip, 9, GEN, 1);
  156. /*
  157. * SUS_GPIO[2:0] enabled by default
  158. * Enable SUS_GPIO3 resume powered gpio explicitly
  159. */
  160. sch_gpio_reg_set(&sch->chip, 13, GEN, 1);
  161. break;
  162. case PCI_DEVICE_ID_INTEL_ITC_LPC:
  163. sch->core_base = 0;
  164. sch->resume_base = 5;
  165. sch->chip.ngpio = 14;
  166. break;
  167. case PCI_DEVICE_ID_INTEL_CENTERTON_ILB:
  168. sch->core_base = 0;
  169. sch->resume_base = 21;
  170. sch->chip.ngpio = 30;
  171. break;
  172. case PCI_DEVICE_ID_INTEL_QUARK_X1000_ILB:
  173. sch->core_base = 0;
  174. sch->resume_base = 2;
  175. sch->chip.ngpio = 8;
  176. break;
  177. default:
  178. return -ENODEV;
  179. }
  180. platform_set_drvdata(pdev, sch);
  181. return gpiochip_add(&sch->chip);
  182. }
  183. static int sch_gpio_remove(struct platform_device *pdev)
  184. {
  185. struct sch_gpio *sch = platform_get_drvdata(pdev);
  186. gpiochip_remove(&sch->chip);
  187. return 0;
  188. }
  189. static struct platform_driver sch_gpio_driver = {
  190. .driver = {
  191. .name = "sch_gpio",
  192. },
  193. .probe = sch_gpio_probe,
  194. .remove = sch_gpio_remove,
  195. };
  196. module_platform_driver(sch_gpio_driver);
  197. MODULE_AUTHOR("Denis Turischev <denis@compulab.co.il>");
  198. MODULE_DESCRIPTION("GPIO interface for Intel Poulsbo SCH");
  199. MODULE_LICENSE("GPL");
  200. MODULE_ALIAS("platform:sch_gpio");