gpio-ich.c 12 KB

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