gpio-ich.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * Intel ICH6-10, Series 5 and 6 GPIO driver
  3. *
  4. * Copyright (C) 2010 Extreme Engineering Solutions.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  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; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/module.h>
  22. #include <linux/pci.h>
  23. #include <linux/gpio.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/mfd/lpc_ich.h>
  26. #define DRV_NAME "gpio_ich"
  27. /*
  28. * GPIO register offsets in GPIO I/O space.
  29. * Each chunk of 32 GPIOs is manipulated via its own USE_SELx, IO_SELx, and
  30. * LVLx registers. Logic in the read/write functions takes a register and
  31. * an absolute bit number and determines the proper register offset and bit
  32. * number in that register. For example, to read the value of GPIO bit 50
  33. * the code would access offset ichx_regs[2(=GPIO_LVL)][1(=50/32)],
  34. * bit 18 (50%32).
  35. */
  36. enum GPIO_REG {
  37. GPIO_USE_SEL = 0,
  38. GPIO_IO_SEL,
  39. GPIO_LVL,
  40. };
  41. static const u8 ichx_regs[3][3] = {
  42. {0x00, 0x30, 0x40}, /* USE_SEL[1-3] offsets */
  43. {0x04, 0x34, 0x44}, /* IO_SEL[1-3] offsets */
  44. {0x0c, 0x38, 0x48}, /* LVL[1-3] offsets */
  45. };
  46. #define ICHX_WRITE(val, reg, base_res) outl(val, (reg) + (base_res)->start)
  47. #define ICHX_READ(reg, base_res) inl((reg) + (base_res)->start)
  48. struct ichx_desc {
  49. /* Max GPIO pins the chipset can have */
  50. uint ngpio;
  51. /* Whether the chipset has GPIO in GPE0_STS in the PM IO region */
  52. bool uses_gpe0;
  53. /* USE_SEL is bogus on some chipsets, eg 3100 */
  54. u32 use_sel_ignore[3];
  55. /* Some chipsets have quirks, let these use their own request/get */
  56. int (*request)(struct gpio_chip *chip, unsigned offset);
  57. int (*get)(struct gpio_chip *chip, unsigned offset);
  58. };
  59. static struct {
  60. spinlock_t lock;
  61. struct platform_device *dev;
  62. struct gpio_chip chip;
  63. struct resource *gpio_base; /* GPIO IO base */
  64. struct resource *pm_base; /* Power Mangagment IO base */
  65. struct ichx_desc *desc; /* Pointer to chipset-specific description */
  66. u32 orig_gpio_ctrl; /* Orig CTRL value, used to restore on exit */
  67. } ichx_priv;
  68. static int modparam_gpiobase = -1; /* dynamic */
  69. module_param_named(gpiobase, modparam_gpiobase, int, 0444);
  70. MODULE_PARM_DESC(gpiobase, "The GPIO number base. -1 means dynamic, "
  71. "which is the default.");
  72. static int ichx_write_bit(int reg, unsigned nr, int val, int verify)
  73. {
  74. unsigned long flags;
  75. u32 data, tmp;
  76. int reg_nr = nr / 32;
  77. int bit = nr & 0x1f;
  78. int ret = 0;
  79. spin_lock_irqsave(&ichx_priv.lock, flags);
  80. data = ICHX_READ(ichx_regs[reg][reg_nr], ichx_priv.gpio_base);
  81. if (val)
  82. data |= 1 << bit;
  83. else
  84. data &= ~(1 << bit);
  85. ICHX_WRITE(data, ichx_regs[reg][reg_nr], ichx_priv.gpio_base);
  86. tmp = ICHX_READ(ichx_regs[reg][reg_nr], ichx_priv.gpio_base);
  87. if (verify && data != tmp)
  88. ret = -EPERM;
  89. spin_unlock_irqrestore(&ichx_priv.lock, flags);
  90. return ret;
  91. }
  92. static int ichx_read_bit(int reg, unsigned nr)
  93. {
  94. unsigned long flags;
  95. u32 data;
  96. int reg_nr = nr / 32;
  97. int bit = nr & 0x1f;
  98. spin_lock_irqsave(&ichx_priv.lock, flags);
  99. data = ICHX_READ(ichx_regs[reg][reg_nr], ichx_priv.gpio_base);
  100. spin_unlock_irqrestore(&ichx_priv.lock, flags);
  101. return data & (1 << bit) ? 1 : 0;
  102. }
  103. static int ichx_gpio_direction_input(struct gpio_chip *gpio, unsigned nr)
  104. {
  105. /*
  106. * Try setting pin as an input and verify it worked since many pins
  107. * are output-only.
  108. */
  109. if (ichx_write_bit(GPIO_IO_SEL, nr, 1, 1))
  110. return -EINVAL;
  111. return 0;
  112. }
  113. static int ichx_gpio_direction_output(struct gpio_chip *gpio, unsigned nr,
  114. int val)
  115. {
  116. /* Set GPIO output value. */
  117. ichx_write_bit(GPIO_LVL, nr, val, 0);
  118. /*
  119. * Try setting pin as an output and verify it worked since many pins
  120. * are input-only.
  121. */
  122. if (ichx_write_bit(GPIO_IO_SEL, nr, 0, 1))
  123. return -EINVAL;
  124. return 0;
  125. }
  126. static int ichx_gpio_get(struct gpio_chip *chip, unsigned nr)
  127. {
  128. return ichx_read_bit(GPIO_LVL, nr);
  129. }
  130. static int ich6_gpio_get(struct gpio_chip *chip, unsigned nr)
  131. {
  132. unsigned long flags;
  133. u32 data;
  134. /*
  135. * GPI 0 - 15 need to be read from the power management registers on
  136. * a ICH6/3100 bridge.
  137. */
  138. if (nr < 16) {
  139. if (!ichx_priv.pm_base)
  140. return -ENXIO;
  141. spin_lock_irqsave(&ichx_priv.lock, flags);
  142. /* GPI 0 - 15 are latched, write 1 to clear*/
  143. ICHX_WRITE(1 << (16 + nr), 0, ichx_priv.pm_base);
  144. data = ICHX_READ(0, ichx_priv.pm_base);
  145. spin_unlock_irqrestore(&ichx_priv.lock, flags);
  146. return (data >> 16) & (1 << nr) ? 1 : 0;
  147. } else {
  148. return ichx_gpio_get(chip, nr);
  149. }
  150. }
  151. static int ichx_gpio_request(struct gpio_chip *chip, unsigned nr)
  152. {
  153. /*
  154. * Note we assume the BIOS properly set a bridge's USE value. Some
  155. * chips (eg Intel 3100) have bogus USE values though, so first see if
  156. * the chipset's USE value can be trusted for this specific bit.
  157. * If it can't be trusted, assume that the pin can be used as a GPIO.
  158. */
  159. if (ichx_priv.desc->use_sel_ignore[nr / 32] & (1 << (nr & 0x1f)))
  160. return 1;
  161. return ichx_read_bit(GPIO_USE_SEL, nr) ? 0 : -ENODEV;
  162. }
  163. static int ich6_gpio_request(struct gpio_chip *chip, unsigned nr)
  164. {
  165. /*
  166. * Fixups for bits 16 and 17 are necessary on the Intel ICH6/3100
  167. * bridge as they are controlled by USE register bits 0 and 1. See
  168. * "Table 704 GPIO_USE_SEL1 register" in the i3100 datasheet for
  169. * additional info.
  170. */
  171. if (nr == 16 || nr == 17)
  172. nr -= 16;
  173. return ichx_gpio_request(chip, nr);
  174. }
  175. static void ichx_gpio_set(struct gpio_chip *chip, unsigned nr, int val)
  176. {
  177. ichx_write_bit(GPIO_LVL, nr, val, 0);
  178. }
  179. static void __devinit ichx_gpiolib_setup(struct gpio_chip *chip)
  180. {
  181. chip->owner = THIS_MODULE;
  182. chip->label = DRV_NAME;
  183. chip->dev = &ichx_priv.dev->dev;
  184. /* Allow chip-specific overrides of request()/get() */
  185. chip->request = ichx_priv.desc->request ?
  186. ichx_priv.desc->request : ichx_gpio_request;
  187. chip->get = ichx_priv.desc->get ?
  188. ichx_priv.desc->get : ichx_gpio_get;
  189. chip->set = ichx_gpio_set;
  190. chip->direction_input = ichx_gpio_direction_input;
  191. chip->direction_output = ichx_gpio_direction_output;
  192. chip->base = modparam_gpiobase;
  193. chip->ngpio = ichx_priv.desc->ngpio;
  194. chip->can_sleep = 0;
  195. chip->dbg_show = NULL;
  196. }
  197. /* ICH6-based, 631xesb-based */
  198. static struct ichx_desc ich6_desc = {
  199. /* Bridges using the ICH6 controller need fixups for GPIO 0 - 17 */
  200. .request = ich6_gpio_request,
  201. .get = ich6_gpio_get,
  202. /* GPIO 0-15 are read in the GPE0_STS PM register */
  203. .uses_gpe0 = true,
  204. .ngpio = 50,
  205. };
  206. /* Intel 3100 */
  207. static struct ichx_desc i3100_desc = {
  208. /*
  209. * Bits 16,17, 20 of USE_SEL and bit 16 of USE_SEL2 always read 0 on
  210. * the Intel 3100. See "Table 712. GPIO Summary Table" of 3100
  211. * Datasheet for more info.
  212. */
  213. .use_sel_ignore = {0x00130000, 0x00010000, 0x0},
  214. /* The 3100 needs fixups for GPIO 0 - 17 */
  215. .request = ich6_gpio_request,
  216. .get = ich6_gpio_get,
  217. /* GPIO 0-15 are read in the GPE0_STS PM register */
  218. .uses_gpe0 = true,
  219. .ngpio = 50,
  220. };
  221. /* ICH7 and ICH8-based */
  222. static struct ichx_desc ich7_desc = {
  223. .ngpio = 50,
  224. };
  225. /* ICH9-based */
  226. static struct ichx_desc ich9_desc = {
  227. .ngpio = 61,
  228. };
  229. /* ICH10-based - Consumer/corporate versions have different amount of GPIO */
  230. static struct ichx_desc ich10_cons_desc = {
  231. .ngpio = 61,
  232. };
  233. static struct ichx_desc ich10_corp_desc = {
  234. .ngpio = 72,
  235. };
  236. /* Intel 5 series, 6 series, 3400 series, and C200 series */
  237. static struct ichx_desc intel5_desc = {
  238. .ngpio = 76,
  239. };
  240. static int __devinit ichx_gpio_probe(struct platform_device *pdev)
  241. {
  242. struct resource *res_base, *res_pm;
  243. int err;
  244. struct lpc_ich_info *ich_info = pdev->dev.platform_data;
  245. if (!ich_info)
  246. return -ENODEV;
  247. ichx_priv.dev = pdev;
  248. switch (ich_info->gpio_version) {
  249. case ICH_I3100_GPIO:
  250. ichx_priv.desc = &i3100_desc;
  251. break;
  252. case ICH_V5_GPIO:
  253. ichx_priv.desc = &intel5_desc;
  254. break;
  255. case ICH_V6_GPIO:
  256. ichx_priv.desc = &ich6_desc;
  257. break;
  258. case ICH_V7_GPIO:
  259. ichx_priv.desc = &ich7_desc;
  260. break;
  261. case ICH_V9_GPIO:
  262. ichx_priv.desc = &ich9_desc;
  263. break;
  264. case ICH_V10CORP_GPIO:
  265. ichx_priv.desc = &ich10_corp_desc;
  266. break;
  267. case ICH_V10CONS_GPIO:
  268. ichx_priv.desc = &ich10_cons_desc;
  269. break;
  270. default:
  271. return -ENODEV;
  272. }
  273. res_base = platform_get_resource(pdev, IORESOURCE_IO, ICH_RES_GPIO);
  274. if (!res_base || !res_base->start || !res_base->end)
  275. return -ENODEV;
  276. if (!request_region(res_base->start, resource_size(res_base),
  277. pdev->name))
  278. return -EBUSY;
  279. ichx_priv.gpio_base = res_base;
  280. /*
  281. * If necessary, determine the I/O address of ACPI/power management
  282. * registers which are needed to read the the GPE0 register for GPI pins
  283. * 0 - 15 on some chipsets.
  284. */
  285. if (!ichx_priv.desc->uses_gpe0)
  286. goto init;
  287. res_pm = platform_get_resource(pdev, IORESOURCE_IO, ICH_RES_GPE0);
  288. if (!res_pm) {
  289. pr_warn("ACPI BAR is unavailable, GPI 0 - 15 unavailable\n");
  290. goto init;
  291. }
  292. if (!request_region(res_pm->start, resource_size(res_pm),
  293. pdev->name)) {
  294. pr_warn("ACPI BAR is busy, GPI 0 - 15 unavailable\n");
  295. goto init;
  296. }
  297. ichx_priv.pm_base = res_pm;
  298. init:
  299. ichx_gpiolib_setup(&ichx_priv.chip);
  300. err = gpiochip_add(&ichx_priv.chip);
  301. if (err) {
  302. pr_err("Failed to register GPIOs\n");
  303. goto add_err;
  304. }
  305. pr_info("GPIO from %d to %d on %s\n", ichx_priv.chip.base,
  306. ichx_priv.chip.base + ichx_priv.chip.ngpio - 1, DRV_NAME);
  307. return 0;
  308. add_err:
  309. release_region(ichx_priv.gpio_base->start,
  310. resource_size(ichx_priv.gpio_base));
  311. if (ichx_priv.pm_base)
  312. release_region(ichx_priv.pm_base->start,
  313. resource_size(ichx_priv.pm_base));
  314. return err;
  315. }
  316. static int __devexit ichx_gpio_remove(struct platform_device *pdev)
  317. {
  318. int err;
  319. err = gpiochip_remove(&ichx_priv.chip);
  320. if (err) {
  321. dev_err(&pdev->dev, "%s failed, %d\n",
  322. "gpiochip_remove()", err);
  323. return err;
  324. }
  325. release_region(ichx_priv.gpio_base->start,
  326. resource_size(ichx_priv.gpio_base));
  327. if (ichx_priv.pm_base)
  328. release_region(ichx_priv.pm_base->start,
  329. resource_size(ichx_priv.pm_base));
  330. return 0;
  331. }
  332. static struct platform_driver ichx_gpio_driver = {
  333. .driver = {
  334. .owner = THIS_MODULE,
  335. .name = DRV_NAME,
  336. },
  337. .probe = ichx_gpio_probe,
  338. .remove = __devexit_p(ichx_gpio_remove),
  339. };
  340. module_platform_driver(ichx_gpio_driver);
  341. MODULE_AUTHOR("Peter Tyser <ptyser@xes-inc.com>");
  342. MODULE_DESCRIPTION("GPIO interface for Intel ICH series");
  343. MODULE_LICENSE("GPL");
  344. MODULE_ALIAS("platform:"DRV_NAME);