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 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 const struct gpio_chip sch_gpio_chip = {
  117. .label = "sch_gpio",
  118. .owner = THIS_MODULE,
  119. .direction_input = sch_gpio_direction_in,
  120. .get = sch_gpio_get,
  121. .direction_output = sch_gpio_direction_out,
  122. .set = sch_gpio_set,
  123. };
  124. static int sch_gpio_probe(struct platform_device *pdev)
  125. {
  126. struct sch_gpio *sch;
  127. struct resource *res;
  128. sch = devm_kzalloc(&pdev->dev, sizeof(*sch), GFP_KERNEL);
  129. if (!sch)
  130. return -ENOMEM;
  131. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  132. if (!res)
  133. return -EBUSY;
  134. if (!devm_request_region(&pdev->dev, res->start, resource_size(res),
  135. pdev->name))
  136. return -EBUSY;
  137. spin_lock_init(&sch->lock);
  138. sch->iobase = res->start;
  139. sch->chip = sch_gpio_chip;
  140. sch->chip.label = dev_name(&pdev->dev);
  141. sch->chip.parent = &pdev->dev;
  142. switch (pdev->id) {
  143. case PCI_DEVICE_ID_INTEL_SCH_LPC:
  144. sch->core_base = 0;
  145. sch->resume_base = 10;
  146. sch->chip.ngpio = 14;
  147. /*
  148. * GPIO[6:0] enabled by default
  149. * GPIO7 is configured by the CMC as SLPIOVR
  150. * Enable GPIO[9:8] core powered gpios explicitly
  151. */
  152. sch_gpio_reg_set(sch, 8, GEN, 1);
  153. sch_gpio_reg_set(sch, 9, GEN, 1);
  154. /*
  155. * SUS_GPIO[2:0] enabled by default
  156. * Enable SUS_GPIO3 resume powered gpio explicitly
  157. */
  158. sch_gpio_reg_set(sch, 13, GEN, 1);
  159. break;
  160. case PCI_DEVICE_ID_INTEL_ITC_LPC:
  161. sch->core_base = 0;
  162. sch->resume_base = 5;
  163. sch->chip.ngpio = 14;
  164. break;
  165. case PCI_DEVICE_ID_INTEL_CENTERTON_ILB:
  166. sch->core_base = 0;
  167. sch->resume_base = 21;
  168. sch->chip.ngpio = 30;
  169. break;
  170. case PCI_DEVICE_ID_INTEL_QUARK_X1000_ILB:
  171. sch->core_base = 0;
  172. sch->resume_base = 2;
  173. sch->chip.ngpio = 8;
  174. break;
  175. default:
  176. return -ENODEV;
  177. }
  178. platform_set_drvdata(pdev, sch);
  179. return devm_gpiochip_add_data(&pdev->dev, &sch->chip, sch);
  180. }
  181. static struct platform_driver sch_gpio_driver = {
  182. .driver = {
  183. .name = "sch_gpio",
  184. },
  185. .probe = sch_gpio_probe,
  186. };
  187. module_platform_driver(sch_gpio_driver);
  188. MODULE_AUTHOR("Denis Turischev <denis@compulab.co.il>");
  189. MODULE_DESCRIPTION("GPIO interface for Intel Poulsbo SCH");
  190. MODULE_LICENSE("GPL");
  191. MODULE_ALIAS("platform:sch_gpio");