gpio-sch.c 5.8 KB

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