gpio-ich.c 13 KB

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