gpio-ich.c 14 KB

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