gpio-ich.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. static const u8 ichx_reglen[3] = {
  47. 0x30, 0x10, 0x10,
  48. };
  49. #define ICHX_WRITE(val, reg, base_res) outl(val, (reg) + (base_res)->start)
  50. #define ICHX_READ(reg, base_res) inl((reg) + (base_res)->start)
  51. struct ichx_desc {
  52. /* Max GPIO pins the chipset can have */
  53. uint ngpio;
  54. /* Whether the chipset has GPIO in GPE0_STS in the PM IO region */
  55. bool uses_gpe0;
  56. /* USE_SEL is bogus on some chipsets, eg 3100 */
  57. u32 use_sel_ignore[3];
  58. /* Some chipsets have quirks, let these use their own request/get */
  59. int (*request)(struct gpio_chip *chip, unsigned offset);
  60. int (*get)(struct gpio_chip *chip, unsigned offset);
  61. };
  62. static struct {
  63. spinlock_t lock;
  64. struct platform_device *dev;
  65. struct gpio_chip chip;
  66. struct resource *gpio_base; /* GPIO IO base */
  67. struct resource *pm_base; /* Power Mangagment IO base */
  68. struct ichx_desc *desc; /* Pointer to chipset-specific description */
  69. u32 orig_gpio_ctrl; /* Orig CTRL value, used to restore on exit */
  70. u8 use_gpio; /* Which GPIO groups are usable */
  71. } ichx_priv;
  72. static int modparam_gpiobase = -1; /* dynamic */
  73. module_param_named(gpiobase, modparam_gpiobase, int, 0444);
  74. MODULE_PARM_DESC(gpiobase, "The GPIO number base. -1 means dynamic, "
  75. "which is the default.");
  76. static int ichx_write_bit(int reg, unsigned nr, int val, int verify)
  77. {
  78. unsigned long flags;
  79. u32 data, tmp;
  80. int reg_nr = nr / 32;
  81. int bit = nr & 0x1f;
  82. int ret = 0;
  83. spin_lock_irqsave(&ichx_priv.lock, flags);
  84. data = ICHX_READ(ichx_regs[reg][reg_nr], ichx_priv.gpio_base);
  85. if (val)
  86. data |= 1 << bit;
  87. else
  88. data &= ~(1 << bit);
  89. ICHX_WRITE(data, ichx_regs[reg][reg_nr], ichx_priv.gpio_base);
  90. tmp = ICHX_READ(ichx_regs[reg][reg_nr], ichx_priv.gpio_base);
  91. if (verify && data != tmp)
  92. ret = -EPERM;
  93. spin_unlock_irqrestore(&ichx_priv.lock, flags);
  94. return ret;
  95. }
  96. static int ichx_read_bit(int reg, unsigned nr)
  97. {
  98. unsigned long flags;
  99. u32 data;
  100. int reg_nr = nr / 32;
  101. int bit = nr & 0x1f;
  102. spin_lock_irqsave(&ichx_priv.lock, flags);
  103. data = ICHX_READ(ichx_regs[reg][reg_nr], ichx_priv.gpio_base);
  104. spin_unlock_irqrestore(&ichx_priv.lock, flags);
  105. return data & (1 << bit) ? 1 : 0;
  106. }
  107. static int ichx_gpio_check_available(struct gpio_chip *gpio, unsigned nr)
  108. {
  109. return (ichx_priv.use_gpio & (1 << (nr / 32))) ? 0 : -ENXIO;
  110. }
  111. static int ichx_gpio_direction_input(struct gpio_chip *gpio, unsigned nr)
  112. {
  113. if (!ichx_gpio_check_available(gpio, nr))
  114. return -ENXIO;
  115. /*
  116. * Try setting pin as an input and verify it worked since many pins
  117. * are output-only.
  118. */
  119. if (ichx_write_bit(GPIO_IO_SEL, nr, 1, 1))
  120. return -EINVAL;
  121. return 0;
  122. }
  123. static int ichx_gpio_direction_output(struct gpio_chip *gpio, unsigned nr,
  124. int val)
  125. {
  126. if (!ichx_gpio_check_available(gpio, nr))
  127. return -ENXIO;
  128. /* Set GPIO output value. */
  129. ichx_write_bit(GPIO_LVL, nr, val, 0);
  130. /*
  131. * Try setting pin as an output and verify it worked since many pins
  132. * are input-only.
  133. */
  134. if (ichx_write_bit(GPIO_IO_SEL, nr, 0, 1))
  135. return -EINVAL;
  136. return 0;
  137. }
  138. static int ichx_gpio_get(struct gpio_chip *chip, unsigned nr)
  139. {
  140. if (!ichx_gpio_check_available(chip, nr))
  141. return -ENXIO;
  142. return ichx_read_bit(GPIO_LVL, nr);
  143. }
  144. static int ich6_gpio_get(struct gpio_chip *chip, unsigned nr)
  145. {
  146. unsigned long flags;
  147. u32 data;
  148. if (!ichx_gpio_check_available(chip, nr))
  149. return -ENXIO;
  150. /*
  151. * GPI 0 - 15 need to be read from the power management registers on
  152. * a ICH6/3100 bridge.
  153. */
  154. if (nr < 16) {
  155. if (!ichx_priv.pm_base)
  156. return -ENXIO;
  157. spin_lock_irqsave(&ichx_priv.lock, flags);
  158. /* GPI 0 - 15 are latched, write 1 to clear*/
  159. ICHX_WRITE(1 << (16 + nr), 0, ichx_priv.pm_base);
  160. data = ICHX_READ(0, ichx_priv.pm_base);
  161. spin_unlock_irqrestore(&ichx_priv.lock, flags);
  162. return (data >> 16) & (1 << nr) ? 1 : 0;
  163. } else {
  164. return ichx_gpio_get(chip, nr);
  165. }
  166. }
  167. static int ichx_gpio_request(struct gpio_chip *chip, unsigned nr)
  168. {
  169. /*
  170. * Note we assume the BIOS properly set a bridge's USE value. Some
  171. * chips (eg Intel 3100) have bogus USE values though, so first see if
  172. * the chipset's USE value can be trusted for this specific bit.
  173. * If it can't be trusted, assume that the pin can be used as a GPIO.
  174. */
  175. if (ichx_priv.desc->use_sel_ignore[nr / 32] & (1 << (nr & 0x1f)))
  176. return 1;
  177. return ichx_read_bit(GPIO_USE_SEL, nr) ? 0 : -ENODEV;
  178. }
  179. static int ich6_gpio_request(struct gpio_chip *chip, unsigned nr)
  180. {
  181. /*
  182. * Fixups for bits 16 and 17 are necessary on the Intel ICH6/3100
  183. * bridge as they are controlled by USE register bits 0 and 1. See
  184. * "Table 704 GPIO_USE_SEL1 register" in the i3100 datasheet for
  185. * additional info.
  186. */
  187. if (nr == 16 || nr == 17)
  188. nr -= 16;
  189. return ichx_gpio_request(chip, nr);
  190. }
  191. static void ichx_gpio_set(struct gpio_chip *chip, unsigned nr, int val)
  192. {
  193. ichx_write_bit(GPIO_LVL, nr, val, 0);
  194. }
  195. static void __devinit ichx_gpiolib_setup(struct gpio_chip *chip)
  196. {
  197. chip->owner = THIS_MODULE;
  198. chip->label = DRV_NAME;
  199. chip->dev = &ichx_priv.dev->dev;
  200. /* Allow chip-specific overrides of request()/get() */
  201. chip->request = ichx_priv.desc->request ?
  202. ichx_priv.desc->request : ichx_gpio_request;
  203. chip->get = ichx_priv.desc->get ?
  204. ichx_priv.desc->get : ichx_gpio_get;
  205. chip->set = ichx_gpio_set;
  206. chip->direction_input = ichx_gpio_direction_input;
  207. chip->direction_output = ichx_gpio_direction_output;
  208. chip->base = modparam_gpiobase;
  209. chip->ngpio = ichx_priv.desc->ngpio;
  210. chip->can_sleep = 0;
  211. chip->dbg_show = NULL;
  212. }
  213. /* ICH6-based, 631xesb-based */
  214. static struct ichx_desc ich6_desc = {
  215. /* Bridges using the ICH6 controller need fixups for GPIO 0 - 17 */
  216. .request = ich6_gpio_request,
  217. .get = ich6_gpio_get,
  218. /* GPIO 0-15 are read in the GPE0_STS PM register */
  219. .uses_gpe0 = true,
  220. .ngpio = 50,
  221. };
  222. /* Intel 3100 */
  223. static struct ichx_desc i3100_desc = {
  224. /*
  225. * Bits 16,17, 20 of USE_SEL and bit 16 of USE_SEL2 always read 0 on
  226. * the Intel 3100. See "Table 712. GPIO Summary Table" of 3100
  227. * Datasheet for more info.
  228. */
  229. .use_sel_ignore = {0x00130000, 0x00010000, 0x0},
  230. /* The 3100 needs fixups for GPIO 0 - 17 */
  231. .request = ich6_gpio_request,
  232. .get = ich6_gpio_get,
  233. /* GPIO 0-15 are read in the GPE0_STS PM register */
  234. .uses_gpe0 = true,
  235. .ngpio = 50,
  236. };
  237. /* ICH7 and ICH8-based */
  238. static struct ichx_desc ich7_desc = {
  239. .ngpio = 50,
  240. };
  241. /* ICH9-based */
  242. static struct ichx_desc ich9_desc = {
  243. .ngpio = 61,
  244. };
  245. /* ICH10-based - Consumer/corporate versions have different amount of GPIO */
  246. static struct ichx_desc ich10_cons_desc = {
  247. .ngpio = 61,
  248. };
  249. static struct ichx_desc ich10_corp_desc = {
  250. .ngpio = 72,
  251. };
  252. /* Intel 5 series, 6 series, 3400 series, and C200 series */
  253. static struct ichx_desc intel5_desc = {
  254. .ngpio = 76,
  255. };
  256. static int __devinit ichx_gpio_request_regions(struct resource *res_base,
  257. const char *name, u8 use_gpio)
  258. {
  259. int i;
  260. if (!res_base || !res_base->start || !res_base->end)
  261. return -ENODEV;
  262. for (i = 0; i < ARRAY_SIZE(ichx_regs[0]); i++) {
  263. if (!(use_gpio & (1 << i)))
  264. continue;
  265. if (!request_region(res_base->start + ichx_regs[0][i],
  266. ichx_reglen[i], name))
  267. goto request_err;
  268. }
  269. return 0;
  270. request_err:
  271. /* Clean up: release already requested regions, if any */
  272. for (i--; i >= 0; i--) {
  273. if (!(use_gpio & (1 << i)))
  274. continue;
  275. release_region(res_base->start + ichx_regs[0][i],
  276. ichx_reglen[i]);
  277. }
  278. return -EBUSY;
  279. }
  280. static void ichx_gpio_release_regions(struct resource *res_base, u8 use_gpio)
  281. {
  282. int i;
  283. for (i = 0; i < ARRAY_SIZE(ichx_regs[0]); i++) {
  284. if (!(use_gpio & (1 << i)))
  285. continue;
  286. release_region(res_base->start + ichx_regs[0][i],
  287. ichx_reglen[i]);
  288. }
  289. }
  290. static int __devinit ichx_gpio_probe(struct platform_device *pdev)
  291. {
  292. struct resource *res_base, *res_pm;
  293. int err;
  294. struct lpc_ich_info *ich_info = pdev->dev.platform_data;
  295. if (!ich_info)
  296. return -ENODEV;
  297. ichx_priv.dev = pdev;
  298. switch (ich_info->gpio_version) {
  299. case ICH_I3100_GPIO:
  300. ichx_priv.desc = &i3100_desc;
  301. break;
  302. case ICH_V5_GPIO:
  303. ichx_priv.desc = &intel5_desc;
  304. break;
  305. case ICH_V6_GPIO:
  306. ichx_priv.desc = &ich6_desc;
  307. break;
  308. case ICH_V7_GPIO:
  309. ichx_priv.desc = &ich7_desc;
  310. break;
  311. case ICH_V9_GPIO:
  312. ichx_priv.desc = &ich9_desc;
  313. break;
  314. case ICH_V10CORP_GPIO:
  315. ichx_priv.desc = &ich10_corp_desc;
  316. break;
  317. case ICH_V10CONS_GPIO:
  318. ichx_priv.desc = &ich10_cons_desc;
  319. break;
  320. default:
  321. return -ENODEV;
  322. }
  323. res_base = platform_get_resource(pdev, IORESOURCE_IO, ICH_RES_GPIO);
  324. ichx_priv.use_gpio = ich_info->use_gpio;
  325. err = ichx_gpio_request_regions(res_base, pdev->name,
  326. ichx_priv.use_gpio);
  327. if (err)
  328. return err;
  329. ichx_priv.gpio_base = res_base;
  330. /*
  331. * If necessary, determine the I/O address of ACPI/power management
  332. * registers which are needed to read the the GPE0 register for GPI pins
  333. * 0 - 15 on some chipsets.
  334. */
  335. if (!ichx_priv.desc->uses_gpe0)
  336. goto init;
  337. res_pm = platform_get_resource(pdev, IORESOURCE_IO, ICH_RES_GPE0);
  338. if (!res_pm) {
  339. pr_warn("ACPI BAR is unavailable, GPI 0 - 15 unavailable\n");
  340. goto init;
  341. }
  342. if (!request_region(res_pm->start, resource_size(res_pm),
  343. pdev->name)) {
  344. pr_warn("ACPI BAR is busy, GPI 0 - 15 unavailable\n");
  345. goto init;
  346. }
  347. ichx_priv.pm_base = res_pm;
  348. init:
  349. ichx_gpiolib_setup(&ichx_priv.chip);
  350. err = gpiochip_add(&ichx_priv.chip);
  351. if (err) {
  352. pr_err("Failed to register GPIOs\n");
  353. goto add_err;
  354. }
  355. pr_info("GPIO from %d to %d on %s\n", ichx_priv.chip.base,
  356. ichx_priv.chip.base + ichx_priv.chip.ngpio - 1, DRV_NAME);
  357. return 0;
  358. add_err:
  359. ichx_gpio_release_regions(ichx_priv.gpio_base, ichx_priv.use_gpio);
  360. if (ichx_priv.pm_base)
  361. release_region(ichx_priv.pm_base->start,
  362. resource_size(ichx_priv.pm_base));
  363. return err;
  364. }
  365. static int __devexit ichx_gpio_remove(struct platform_device *pdev)
  366. {
  367. int err;
  368. err = gpiochip_remove(&ichx_priv.chip);
  369. if (err) {
  370. dev_err(&pdev->dev, "%s failed, %d\n",
  371. "gpiochip_remove()", err);
  372. return err;
  373. }
  374. ichx_gpio_release_regions(ichx_priv.gpio_base, ichx_priv.use_gpio);
  375. if (ichx_priv.pm_base)
  376. release_region(ichx_priv.pm_base->start,
  377. resource_size(ichx_priv.pm_base));
  378. return 0;
  379. }
  380. static struct platform_driver ichx_gpio_driver = {
  381. .driver = {
  382. .owner = THIS_MODULE,
  383. .name = DRV_NAME,
  384. },
  385. .probe = ichx_gpio_probe,
  386. .remove = __devexit_p(ichx_gpio_remove),
  387. };
  388. module_platform_driver(ichx_gpio_driver);
  389. MODULE_AUTHOR("Peter Tyser <ptyser@xes-inc.com>");
  390. MODULE_DESCRIPTION("GPIO interface for Intel ICH series");
  391. MODULE_LICENSE("GPL");
  392. MODULE_ALIAS("platform:"DRV_NAME);