gpio-sch.c 5.6 KB

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