gpio-ich.c 13 KB

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