pinctrl-lynxpoint.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Intel Lynxpoint PCH pinctrl/GPIO driver
  4. *
  5. * Copyright (c) 2012, 2019, Intel Corporation
  6. * Authors: Mathias Nyman <mathias.nyman@linux.intel.com>
  7. * Andy Shevchenko <andriy.shevchenko@linux.intel.com>
  8. */
  9. #include <linux/acpi.h>
  10. #include <linux/array_size.h>
  11. #include <linux/bitops.h>
  12. #include <linux/cleanup.h>
  13. #include <linux/gpio/driver.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/io.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pm.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/slab.h>
  21. #include <linux/types.h>
  22. #include <linux/pinctrl/consumer.h>
  23. #include <linux/pinctrl/pinconf-generic.h>
  24. #include <linux/pinctrl/pinconf.h>
  25. #include <linux/pinctrl/pinctrl.h>
  26. #include <linux/pinctrl/pinmux.h>
  27. #include "pinctrl-intel.h"
  28. #define COMMUNITY(p, n) \
  29. { \
  30. .pin_base = (p), \
  31. .npins = (n), \
  32. }
  33. static const struct pinctrl_pin_desc lptlp_pins[] = {
  34. PINCTRL_PIN(0, "GP0_UART1_RXD"),
  35. PINCTRL_PIN(1, "GP1_UART1_TXD"),
  36. PINCTRL_PIN(2, "GP2_UART1_RTSB"),
  37. PINCTRL_PIN(3, "GP3_UART1_CTSB"),
  38. PINCTRL_PIN(4, "GP4_I2C0_SDA"),
  39. PINCTRL_PIN(5, "GP5_I2C0_SCL"),
  40. PINCTRL_PIN(6, "GP6_I2C1_SDA"),
  41. PINCTRL_PIN(7, "GP7_I2C1_SCL"),
  42. PINCTRL_PIN(8, "GP8"),
  43. PINCTRL_PIN(9, "GP9"),
  44. PINCTRL_PIN(10, "GP10"),
  45. PINCTRL_PIN(11, "GP11_SMBALERTB"),
  46. PINCTRL_PIN(12, "GP12_LANPHYPC"),
  47. PINCTRL_PIN(13, "GP13"),
  48. PINCTRL_PIN(14, "GP14"),
  49. PINCTRL_PIN(15, "GP15"),
  50. PINCTRL_PIN(16, "GP16_MGPIO9"),
  51. PINCTRL_PIN(17, "GP17_MGPIO10"),
  52. PINCTRL_PIN(18, "GP18_SRC0CLKRQB"),
  53. PINCTRL_PIN(19, "GP19_SRC1CLKRQB"),
  54. PINCTRL_PIN(20, "GP20_SRC2CLKRQB"),
  55. PINCTRL_PIN(21, "GP21_SRC3CLKRQB"),
  56. PINCTRL_PIN(22, "GP22_SRC4CLKRQB_TRST2"),
  57. PINCTRL_PIN(23, "GP23_SRC5CLKRQB_TDI2"),
  58. PINCTRL_PIN(24, "GP24_MGPIO0"),
  59. PINCTRL_PIN(25, "GP25_USBWAKEOUTB"),
  60. PINCTRL_PIN(26, "GP26_MGPIO5"),
  61. PINCTRL_PIN(27, "GP27_MGPIO6"),
  62. PINCTRL_PIN(28, "GP28_MGPIO7"),
  63. PINCTRL_PIN(29, "GP29_SLP_WLANB_MGPIO3"),
  64. PINCTRL_PIN(30, "GP30_SUSWARNB_SUSPWRDNACK_MGPIO1"),
  65. PINCTRL_PIN(31, "GP31_ACPRESENT_MGPIO2"),
  66. PINCTRL_PIN(32, "GP32_CLKRUNB"),
  67. PINCTRL_PIN(33, "GP33_DEVSLP0"),
  68. PINCTRL_PIN(34, "GP34_SATA0XPCIE6L3B_SATA0GP"),
  69. PINCTRL_PIN(35, "GP35_SATA1XPCIE6L2B_SATA1GP"),
  70. PINCTRL_PIN(36, "GP36_SATA2XPCIE6L1B_SATA2GP"),
  71. PINCTRL_PIN(37, "GP37_SATA3XPCIE6L0B_SATA3GP"),
  72. PINCTRL_PIN(38, "GP38_DEVSLP1"),
  73. PINCTRL_PIN(39, "GP39_DEVSLP2"),
  74. PINCTRL_PIN(40, "GP40_OC0B"),
  75. PINCTRL_PIN(41, "GP41_OC1B"),
  76. PINCTRL_PIN(42, "GP42_OC2B"),
  77. PINCTRL_PIN(43, "GP43_OC3B"),
  78. PINCTRL_PIN(44, "GP44"),
  79. PINCTRL_PIN(45, "GP45_TMS2"),
  80. PINCTRL_PIN(46, "GP46_TDO2"),
  81. PINCTRL_PIN(47, "GP47"),
  82. PINCTRL_PIN(48, "GP48"),
  83. PINCTRL_PIN(49, "GP49"),
  84. PINCTRL_PIN(50, "GP50"),
  85. PINCTRL_PIN(51, "GP51_GSXDOUT"),
  86. PINCTRL_PIN(52, "GP52_GSXSLOAD"),
  87. PINCTRL_PIN(53, "GP53_GSXDIN"),
  88. PINCTRL_PIN(54, "GP54_GSXSRESETB"),
  89. PINCTRL_PIN(55, "GP55_GSXCLK"),
  90. PINCTRL_PIN(56, "GP56"),
  91. PINCTRL_PIN(57, "GP57"),
  92. PINCTRL_PIN(58, "GP58"),
  93. PINCTRL_PIN(59, "GP59"),
  94. PINCTRL_PIN(60, "GP60_SML0ALERTB_MGPIO4"),
  95. PINCTRL_PIN(61, "GP61_SUS_STATB"),
  96. PINCTRL_PIN(62, "GP62_SUSCLK"),
  97. PINCTRL_PIN(63, "GP63_SLP_S5B"),
  98. PINCTRL_PIN(64, "GP64_SDIO_CLK"),
  99. PINCTRL_PIN(65, "GP65_SDIO_CMD"),
  100. PINCTRL_PIN(66, "GP66_SDIO_D0"),
  101. PINCTRL_PIN(67, "GP67_SDIO_D1"),
  102. PINCTRL_PIN(68, "GP68_SDIO_D2"),
  103. PINCTRL_PIN(69, "GP69_SDIO_D3"),
  104. PINCTRL_PIN(70, "GP70_SDIO_POWER_EN"),
  105. PINCTRL_PIN(71, "GP71_MPHYPC"),
  106. PINCTRL_PIN(72, "GP72_BATLOWB"),
  107. PINCTRL_PIN(73, "GP73_SML1ALERTB_PCHHOTB_MGPIO8"),
  108. PINCTRL_PIN(74, "GP74_SML1DATA_MGPIO12"),
  109. PINCTRL_PIN(75, "GP75_SML1CLK_MGPIO11"),
  110. PINCTRL_PIN(76, "GP76_BMBUSYB"),
  111. PINCTRL_PIN(77, "GP77_PIRQAB"),
  112. PINCTRL_PIN(78, "GP78_PIRQBB"),
  113. PINCTRL_PIN(79, "GP79_PIRQCB"),
  114. PINCTRL_PIN(80, "GP80_PIRQDB"),
  115. PINCTRL_PIN(81, "GP81_SPKR"),
  116. PINCTRL_PIN(82, "GP82_RCINB"),
  117. PINCTRL_PIN(83, "GP83_GSPI0_CSB"),
  118. PINCTRL_PIN(84, "GP84_GSPI0_CLK"),
  119. PINCTRL_PIN(85, "GP85_GSPI0_MISO"),
  120. PINCTRL_PIN(86, "GP86_GSPI0_MOSI"),
  121. PINCTRL_PIN(87, "GP87_GSPI1_CSB"),
  122. PINCTRL_PIN(88, "GP88_GSPI1_CLK"),
  123. PINCTRL_PIN(89, "GP89_GSPI1_MISO"),
  124. PINCTRL_PIN(90, "GP90_GSPI1_MOSI"),
  125. PINCTRL_PIN(91, "GP91_UART0_RXD"),
  126. PINCTRL_PIN(92, "GP92_UART0_TXD"),
  127. PINCTRL_PIN(93, "GP93_UART0_RTSB"),
  128. PINCTRL_PIN(94, "GP94_UART0_CTSB"),
  129. };
  130. static const struct intel_community lptlp_communities[] = {
  131. COMMUNITY(0, 95),
  132. };
  133. static const struct intel_pinctrl_soc_data lptlp_soc_data = {
  134. .pins = lptlp_pins,
  135. .npins = ARRAY_SIZE(lptlp_pins),
  136. .communities = lptlp_communities,
  137. .ncommunities = ARRAY_SIZE(lptlp_communities),
  138. };
  139. /* LynxPoint chipset has support for 95 GPIO pins */
  140. #define LP_NUM_GPIO 95
  141. /* Bitmapped register offsets */
  142. #define LP_ACPI_OWNED 0x00 /* Bitmap, set by bios, 0: pin reserved for ACPI */
  143. #define LP_IRQ2IOXAPIC 0x10 /* Bitmap, set by bios, 1: pin routed to IOxAPIC */
  144. #define LP_GC 0x7C /* set APIC IRQ to IRQ14 or IRQ15 for all pins */
  145. #define LP_INT_STAT 0x80
  146. #define LP_INT_ENABLE 0x90
  147. /* Each pin has two 32 bit config registers, starting at 0x100 */
  148. #define LP_CONFIG1 0x100
  149. #define LP_CONFIG2 0x104
  150. /* LP_CONFIG1 reg bits */
  151. #define OUT_LVL_BIT BIT(31)
  152. #define IN_LVL_BIT BIT(30)
  153. #define TRIG_SEL_BIT BIT(4) /* 0: Edge, 1: Level */
  154. #define INT_INV_BIT BIT(3) /* Invert interrupt triggering */
  155. #define DIR_BIT BIT(2) /* 0: Output, 1: Input */
  156. #define USE_SEL_MASK GENMASK(1, 0) /* 0: Native, 1: GPIO, ... */
  157. #define USE_SEL_NATIVE (0 << 0)
  158. #define USE_SEL_GPIO (1 << 0)
  159. /* LP_CONFIG2 reg bits */
  160. #define GPINDIS_BIT BIT(2) /* disable input sensing */
  161. #define GPIWP_MASK GENMASK(1, 0) /* weak pull options */
  162. #define GPIWP_NONE 0 /* none */
  163. #define GPIWP_DOWN 1 /* weak pull down */
  164. #define GPIWP_UP 2 /* weak pull up */
  165. /*
  166. * Lynxpoint gpios are controlled through both bitmapped registers and
  167. * per gpio specific registers. The bitmapped registers are in chunks of
  168. * 3 x 32bit registers to cover all 95 GPIOs
  169. *
  170. * per gpio specific registers consist of two 32bit registers per gpio
  171. * (LP_CONFIG1 and LP_CONFIG2), with 95 GPIOs there's a total of
  172. * 190 config registers.
  173. *
  174. * A simplified view of the register layout look like this:
  175. *
  176. * LP_ACPI_OWNED[31:0] gpio ownerships for gpios 0-31 (bitmapped registers)
  177. * LP_ACPI_OWNED[63:32] gpio ownerships for gpios 32-63
  178. * LP_ACPI_OWNED[94:64] gpio ownerships for gpios 63-94
  179. * ...
  180. * LP_INT_ENABLE[31:0] ...
  181. * LP_INT_ENABLE[63:32] ...
  182. * LP_INT_ENABLE[94:64] ...
  183. * LP0_CONFIG1 (gpio 0) config1 reg for gpio 0 (per gpio registers)
  184. * LP0_CONFIG2 (gpio 0) config2 reg for gpio 0
  185. * LP1_CONFIG1 (gpio 1) config1 reg for gpio 1
  186. * LP1_CONFIG2 (gpio 1) config2 reg for gpio 1
  187. * LP2_CONFIG1 (gpio 2) ...
  188. * LP2_CONFIG2 (gpio 2) ...
  189. * ...
  190. * LP94_CONFIG1 (gpio 94) ...
  191. * LP94_CONFIG2 (gpio 94) ...
  192. *
  193. * IOxAPIC redirection map applies only for gpio 8-10, 13-14, 45-55.
  194. */
  195. static void __iomem *lp_gpio_reg(struct gpio_chip *chip, unsigned int offset,
  196. int reg)
  197. {
  198. struct intel_pinctrl *lg = gpiochip_get_data(chip);
  199. const struct intel_community *comm;
  200. int reg_offset;
  201. comm = intel_get_community(lg, offset);
  202. if (!comm)
  203. return NULL;
  204. offset -= comm->pin_base;
  205. if (reg == LP_CONFIG1 || reg == LP_CONFIG2)
  206. /* per gpio specific config registers */
  207. reg_offset = offset * 8;
  208. else
  209. /* bitmapped registers */
  210. reg_offset = (offset / 32) * 4;
  211. return comm->regs + reg_offset + reg;
  212. }
  213. static bool lp_gpio_acpi_use(struct intel_pinctrl *lg, unsigned int pin)
  214. {
  215. void __iomem *acpi_use;
  216. acpi_use = lp_gpio_reg(&lg->chip, pin, LP_ACPI_OWNED);
  217. if (!acpi_use)
  218. return true;
  219. return !(ioread32(acpi_use) & BIT(pin % 32));
  220. }
  221. static bool lp_gpio_ioxapic_use(struct gpio_chip *chip, unsigned int offset)
  222. {
  223. void __iomem *ioxapic_use = lp_gpio_reg(chip, offset, LP_IRQ2IOXAPIC);
  224. u32 value;
  225. value = ioread32(ioxapic_use);
  226. if (offset >= 8 && offset <= 10)
  227. return !!(value & BIT(offset - 8 + 0));
  228. if (offset >= 13 && offset <= 14)
  229. return !!(value & BIT(offset - 13 + 3));
  230. if (offset >= 45 && offset <= 55)
  231. return !!(value & BIT(offset - 45 + 5));
  232. return false;
  233. }
  234. static void lp_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
  235. unsigned int pin)
  236. {
  237. struct intel_pinctrl *lg = pinctrl_dev_get_drvdata(pctldev);
  238. void __iomem *reg = lp_gpio_reg(&lg->chip, pin, LP_CONFIG1);
  239. void __iomem *conf2 = lp_gpio_reg(&lg->chip, pin, LP_CONFIG2);
  240. u32 value, mode;
  241. value = ioread32(reg);
  242. mode = value & USE_SEL_MASK;
  243. if (mode == USE_SEL_GPIO)
  244. seq_puts(s, "GPIO ");
  245. else
  246. seq_printf(s, "mode %d ", mode);
  247. seq_printf(s, "0x%08x 0x%08x", value, ioread32(conf2));
  248. if (lp_gpio_acpi_use(lg, pin))
  249. seq_puts(s, " [ACPI]");
  250. }
  251. static const struct pinctrl_ops lptlp_pinctrl_ops = {
  252. .get_groups_count = intel_get_groups_count,
  253. .get_group_name = intel_get_group_name,
  254. .get_group_pins = intel_get_group_pins,
  255. .pin_dbg_show = lp_pin_dbg_show,
  256. };
  257. static int lp_pinmux_set_mux(struct pinctrl_dev *pctldev,
  258. unsigned int function, unsigned int group)
  259. {
  260. struct intel_pinctrl *lg = pinctrl_dev_get_drvdata(pctldev);
  261. const struct intel_pingroup *grp = &lg->soc->groups[group];
  262. int i;
  263. guard(raw_spinlock_irqsave)(&lg->lock);
  264. /* Now enable the mux setting for each pin in the group */
  265. for (i = 0; i < grp->grp.npins; i++) {
  266. void __iomem *reg = lp_gpio_reg(&lg->chip, grp->grp.pins[i], LP_CONFIG1);
  267. u32 value;
  268. value = ioread32(reg);
  269. value &= ~USE_SEL_MASK;
  270. if (grp->modes)
  271. value |= grp->modes[i];
  272. else
  273. value |= grp->mode;
  274. iowrite32(value, reg);
  275. }
  276. return 0;
  277. }
  278. static void lp_gpio_enable_input(void __iomem *reg)
  279. {
  280. iowrite32(ioread32(reg) & ~GPINDIS_BIT, reg);
  281. }
  282. static void lp_gpio_disable_input(void __iomem *reg)
  283. {
  284. iowrite32(ioread32(reg) | GPINDIS_BIT, reg);
  285. }
  286. static int lp_gpio_request_enable(struct pinctrl_dev *pctldev,
  287. struct pinctrl_gpio_range *range,
  288. unsigned int pin)
  289. {
  290. struct intel_pinctrl *lg = pinctrl_dev_get_drvdata(pctldev);
  291. void __iomem *reg = lp_gpio_reg(&lg->chip, pin, LP_CONFIG1);
  292. void __iomem *conf2 = lp_gpio_reg(&lg->chip, pin, LP_CONFIG2);
  293. u32 value;
  294. guard(raw_spinlock_irqsave)(&lg->lock);
  295. /*
  296. * Reconfigure pin to GPIO mode if needed and issue a warning,
  297. * since we expect firmware to configure it properly.
  298. */
  299. value = ioread32(reg);
  300. if ((value & USE_SEL_MASK) != USE_SEL_GPIO) {
  301. iowrite32((value & USE_SEL_MASK) | USE_SEL_GPIO, reg);
  302. dev_warn(lg->dev, FW_BUG "pin %u forcibly reconfigured as GPIO\n", pin);
  303. }
  304. /* Enable input sensing */
  305. lp_gpio_enable_input(conf2);
  306. return 0;
  307. }
  308. static void lp_gpio_disable_free(struct pinctrl_dev *pctldev,
  309. struct pinctrl_gpio_range *range,
  310. unsigned int pin)
  311. {
  312. struct intel_pinctrl *lg = pinctrl_dev_get_drvdata(pctldev);
  313. void __iomem *conf2 = lp_gpio_reg(&lg->chip, pin, LP_CONFIG2);
  314. guard(raw_spinlock_irqsave)(&lg->lock);
  315. /* Disable input sensing */
  316. lp_gpio_disable_input(conf2);
  317. }
  318. static int lp_gpio_set_direction(struct pinctrl_dev *pctldev,
  319. struct pinctrl_gpio_range *range,
  320. unsigned int pin, bool input)
  321. {
  322. struct intel_pinctrl *lg = pinctrl_dev_get_drvdata(pctldev);
  323. void __iomem *reg = lp_gpio_reg(&lg->chip, pin, LP_CONFIG1);
  324. u32 value;
  325. guard(raw_spinlock_irqsave)(&lg->lock);
  326. value = ioread32(reg);
  327. value &= ~DIR_BIT;
  328. if (input) {
  329. value |= DIR_BIT;
  330. } else {
  331. /*
  332. * Before making any direction modifications, do a check if GPIO
  333. * is set for direct IRQ. On Lynxpoint, setting GPIO to output
  334. * does not make sense, so let's at least warn the caller before
  335. * they shoot themselves in the foot.
  336. */
  337. WARN(lp_gpio_ioxapic_use(&lg->chip, pin),
  338. "Potential Error: Setting GPIO to output with IOxAPIC redirection");
  339. }
  340. iowrite32(value, reg);
  341. return 0;
  342. }
  343. static const struct pinmux_ops lptlp_pinmux_ops = {
  344. .get_functions_count = intel_get_functions_count,
  345. .get_function_name = intel_get_function_name,
  346. .get_function_groups = intel_get_function_groups,
  347. .set_mux = lp_pinmux_set_mux,
  348. .gpio_request_enable = lp_gpio_request_enable,
  349. .gpio_disable_free = lp_gpio_disable_free,
  350. .gpio_set_direction = lp_gpio_set_direction,
  351. };
  352. static int lp_pin_config_get(struct pinctrl_dev *pctldev, unsigned int pin,
  353. unsigned long *config)
  354. {
  355. struct intel_pinctrl *lg = pinctrl_dev_get_drvdata(pctldev);
  356. void __iomem *conf2 = lp_gpio_reg(&lg->chip, pin, LP_CONFIG2);
  357. enum pin_config_param param = pinconf_to_config_param(*config);
  358. u32 value, pull;
  359. u16 arg;
  360. scoped_guard(raw_spinlock_irqsave, &lg->lock)
  361. value = ioread32(conf2);
  362. pull = value & GPIWP_MASK;
  363. switch (param) {
  364. case PIN_CONFIG_BIAS_DISABLE:
  365. if (pull != GPIWP_NONE)
  366. return -EINVAL;
  367. arg = 0;
  368. break;
  369. case PIN_CONFIG_BIAS_PULL_DOWN:
  370. if (pull != GPIWP_DOWN)
  371. return -EINVAL;
  372. arg = 1;
  373. break;
  374. case PIN_CONFIG_BIAS_PULL_UP:
  375. if (pull != GPIWP_UP)
  376. return -EINVAL;
  377. arg = 1;
  378. break;
  379. default:
  380. return -ENOTSUPP;
  381. }
  382. *config = pinconf_to_config_packed(param, arg);
  383. return 0;
  384. }
  385. static int lp_pin_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
  386. unsigned long *configs, unsigned int num_configs)
  387. {
  388. struct intel_pinctrl *lg = pinctrl_dev_get_drvdata(pctldev);
  389. void __iomem *conf2 = lp_gpio_reg(&lg->chip, pin, LP_CONFIG2);
  390. enum pin_config_param param;
  391. unsigned int i;
  392. u32 value;
  393. guard(raw_spinlock_irqsave)(&lg->lock);
  394. value = ioread32(conf2);
  395. for (i = 0; i < num_configs; i++) {
  396. param = pinconf_to_config_param(configs[i]);
  397. switch (param) {
  398. case PIN_CONFIG_BIAS_DISABLE:
  399. value &= ~GPIWP_MASK;
  400. value |= GPIWP_NONE;
  401. break;
  402. case PIN_CONFIG_BIAS_PULL_DOWN:
  403. value &= ~GPIWP_MASK;
  404. value |= GPIWP_DOWN;
  405. break;
  406. case PIN_CONFIG_BIAS_PULL_UP:
  407. value &= ~GPIWP_MASK;
  408. value |= GPIWP_UP;
  409. break;
  410. default:
  411. return -ENOTSUPP;
  412. }
  413. }
  414. iowrite32(value, conf2);
  415. return 0;
  416. }
  417. static const struct pinconf_ops lptlp_pinconf_ops = {
  418. .is_generic = true,
  419. .pin_config_get = lp_pin_config_get,
  420. .pin_config_set = lp_pin_config_set,
  421. };
  422. static const struct pinctrl_desc lptlp_pinctrl_desc = {
  423. .pctlops = &lptlp_pinctrl_ops,
  424. .pmxops = &lptlp_pinmux_ops,
  425. .confops = &lptlp_pinconf_ops,
  426. .owner = THIS_MODULE,
  427. };
  428. static int lp_gpio_get(struct gpio_chip *chip, unsigned int offset)
  429. {
  430. void __iomem *reg = lp_gpio_reg(chip, offset, LP_CONFIG1);
  431. return !!(ioread32(reg) & IN_LVL_BIT);
  432. }
  433. static void lp_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)
  434. {
  435. struct intel_pinctrl *lg = gpiochip_get_data(chip);
  436. void __iomem *reg = lp_gpio_reg(chip, offset, LP_CONFIG1);
  437. guard(raw_spinlock_irqsave)(&lg->lock);
  438. if (value)
  439. iowrite32(ioread32(reg) | OUT_LVL_BIT, reg);
  440. else
  441. iowrite32(ioread32(reg) & ~OUT_LVL_BIT, reg);
  442. }
  443. static int lp_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
  444. {
  445. return pinctrl_gpio_direction_input(chip, offset);
  446. }
  447. static int lp_gpio_direction_output(struct gpio_chip *chip, unsigned int offset,
  448. int value)
  449. {
  450. lp_gpio_set(chip, offset, value);
  451. return pinctrl_gpio_direction_output(chip, offset);
  452. }
  453. static int lp_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
  454. {
  455. void __iomem *reg = lp_gpio_reg(chip, offset, LP_CONFIG1);
  456. if (ioread32(reg) & DIR_BIT)
  457. return GPIO_LINE_DIRECTION_IN;
  458. return GPIO_LINE_DIRECTION_OUT;
  459. }
  460. static void lp_gpio_irq_handler(struct irq_desc *desc)
  461. {
  462. struct irq_data *data = irq_desc_get_irq_data(desc);
  463. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  464. struct intel_pinctrl *lg = gpiochip_get_data(gc);
  465. struct irq_chip *chip = irq_data_get_irq_chip(data);
  466. void __iomem *reg, *ena;
  467. unsigned long pending;
  468. u32 base, pin;
  469. /* check from GPIO controller which pin triggered the interrupt */
  470. for (base = 0; base < lg->chip.ngpio; base += 32) {
  471. reg = lp_gpio_reg(&lg->chip, base, LP_INT_STAT);
  472. ena = lp_gpio_reg(&lg->chip, base, LP_INT_ENABLE);
  473. /* Only interrupts that are enabled */
  474. pending = ioread32(reg) & ioread32(ena);
  475. for_each_set_bit(pin, &pending, 32)
  476. generic_handle_domain_irq(lg->chip.irq.domain, base + pin);
  477. }
  478. chip->irq_eoi(data);
  479. }
  480. static void lp_irq_ack(struct irq_data *d)
  481. {
  482. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  483. struct intel_pinctrl *lg = gpiochip_get_data(gc);
  484. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  485. void __iomem *reg = lp_gpio_reg(&lg->chip, hwirq, LP_INT_STAT);
  486. guard(raw_spinlock_irqsave)(&lg->lock);
  487. iowrite32(BIT(hwirq % 32), reg);
  488. }
  489. static void lp_irq_unmask(struct irq_data *d)
  490. {
  491. }
  492. static void lp_irq_mask(struct irq_data *d)
  493. {
  494. }
  495. static void lp_irq_enable(struct irq_data *d)
  496. {
  497. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  498. struct intel_pinctrl *lg = gpiochip_get_data(gc);
  499. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  500. void __iomem *reg = lp_gpio_reg(&lg->chip, hwirq, LP_INT_ENABLE);
  501. gpiochip_enable_irq(gc, hwirq);
  502. scoped_guard(raw_spinlock_irqsave, &lg->lock)
  503. iowrite32(ioread32(reg) | BIT(hwirq % 32), reg);
  504. }
  505. static void lp_irq_disable(struct irq_data *d)
  506. {
  507. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  508. struct intel_pinctrl *lg = gpiochip_get_data(gc);
  509. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  510. void __iomem *reg = lp_gpio_reg(&lg->chip, hwirq, LP_INT_ENABLE);
  511. scoped_guard(raw_spinlock_irqsave, &lg->lock)
  512. iowrite32(ioread32(reg) & ~BIT(hwirq % 32), reg);
  513. gpiochip_disable_irq(gc, hwirq);
  514. }
  515. static int lp_irq_set_type(struct irq_data *d, unsigned int type)
  516. {
  517. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  518. struct intel_pinctrl *lg = gpiochip_get_data(gc);
  519. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  520. void __iomem *reg;
  521. u32 value;
  522. reg = lp_gpio_reg(&lg->chip, hwirq, LP_CONFIG1);
  523. if (!reg)
  524. return -EINVAL;
  525. /* Fail if BIOS reserved pin for ACPI use */
  526. if (lp_gpio_acpi_use(lg, hwirq)) {
  527. dev_err(lg->dev, "pin %lu can't be used as IRQ\n", hwirq);
  528. return -EBUSY;
  529. }
  530. guard(raw_spinlock_irqsave)(&lg->lock);
  531. value = ioread32(reg);
  532. /* set both TRIG_SEL and INV bits to 0 for rising edge */
  533. if (type & IRQ_TYPE_EDGE_RISING)
  534. value &= ~(TRIG_SEL_BIT | INT_INV_BIT);
  535. /* TRIG_SEL bit 0, INV bit 1 for falling edge */
  536. if (type & IRQ_TYPE_EDGE_FALLING)
  537. value = (value | INT_INV_BIT) & ~TRIG_SEL_BIT;
  538. /* TRIG_SEL bit 1, INV bit 0 for level low */
  539. if (type & IRQ_TYPE_LEVEL_LOW)
  540. value = (value | TRIG_SEL_BIT) & ~INT_INV_BIT;
  541. /* TRIG_SEL bit 1, INV bit 1 for level high */
  542. if (type & IRQ_TYPE_LEVEL_HIGH)
  543. value |= TRIG_SEL_BIT | INT_INV_BIT;
  544. iowrite32(value, reg);
  545. if (type & IRQ_TYPE_EDGE_BOTH)
  546. irq_set_handler_locked(d, handle_edge_irq);
  547. else if (type & IRQ_TYPE_LEVEL_MASK)
  548. irq_set_handler_locked(d, handle_level_irq);
  549. return 0;
  550. }
  551. static const struct irq_chip lp_irqchip = {
  552. .name = "LP-GPIO",
  553. .irq_ack = lp_irq_ack,
  554. .irq_mask = lp_irq_mask,
  555. .irq_unmask = lp_irq_unmask,
  556. .irq_enable = lp_irq_enable,
  557. .irq_disable = lp_irq_disable,
  558. .irq_set_type = lp_irq_set_type,
  559. .flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_IMMUTABLE,
  560. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  561. };
  562. static int lp_gpio_irq_init_hw(struct gpio_chip *chip)
  563. {
  564. struct intel_pinctrl *lg = gpiochip_get_data(chip);
  565. void __iomem *reg;
  566. unsigned int base;
  567. for (base = 0; base < lg->chip.ngpio; base += 32) {
  568. /* disable gpio pin interrupts */
  569. reg = lp_gpio_reg(&lg->chip, base, LP_INT_ENABLE);
  570. iowrite32(0, reg);
  571. /* Clear interrupt status register */
  572. reg = lp_gpio_reg(&lg->chip, base, LP_INT_STAT);
  573. iowrite32(0xffffffff, reg);
  574. }
  575. return 0;
  576. }
  577. static int lp_gpio_add_pin_ranges(struct gpio_chip *chip)
  578. {
  579. struct intel_pinctrl *lg = gpiochip_get_data(chip);
  580. struct device *dev = lg->dev;
  581. int ret;
  582. ret = gpiochip_add_pin_range(chip, dev_name(dev), 0, 0, lg->soc->npins);
  583. if (ret)
  584. dev_err(dev, "failed to add GPIO pin range\n");
  585. return ret;
  586. }
  587. static int lp_gpio_probe(struct platform_device *pdev)
  588. {
  589. const struct intel_pinctrl_soc_data *soc;
  590. struct intel_pinctrl *lg;
  591. struct gpio_chip *gc;
  592. struct device *dev = &pdev->dev;
  593. struct resource *io_rc;
  594. void __iomem *regs;
  595. unsigned int i;
  596. int irq, ret;
  597. soc = (const struct intel_pinctrl_soc_data *)device_get_match_data(dev);
  598. if (!soc)
  599. return -ENODEV;
  600. lg = devm_kzalloc(dev, sizeof(*lg), GFP_KERNEL);
  601. if (!lg)
  602. return -ENOMEM;
  603. lg->dev = dev;
  604. lg->soc = soc;
  605. lg->ncommunities = lg->soc->ncommunities;
  606. lg->communities = devm_kcalloc(dev, lg->ncommunities,
  607. sizeof(*lg->communities), GFP_KERNEL);
  608. if (!lg->communities)
  609. return -ENOMEM;
  610. lg->pctldesc = lptlp_pinctrl_desc;
  611. lg->pctldesc.name = dev_name(dev);
  612. lg->pctldesc.pins = lg->soc->pins;
  613. lg->pctldesc.npins = lg->soc->npins;
  614. lg->pctldev = devm_pinctrl_register(dev, &lg->pctldesc, lg);
  615. if (IS_ERR(lg->pctldev)) {
  616. dev_err(dev, "failed to register pinctrl driver\n");
  617. return PTR_ERR(lg->pctldev);
  618. }
  619. platform_set_drvdata(pdev, lg);
  620. io_rc = platform_get_resource(pdev, IORESOURCE_IO, 0);
  621. if (!io_rc) {
  622. dev_err(dev, "missing IO resources\n");
  623. return -EINVAL;
  624. }
  625. regs = devm_ioport_map(dev, io_rc->start, resource_size(io_rc));
  626. if (!regs) {
  627. dev_err(dev, "failed mapping IO region %pR\n", &io_rc);
  628. return -EBUSY;
  629. }
  630. for (i = 0; i < lg->soc->ncommunities; i++) {
  631. struct intel_community *comm = &lg->communities[i];
  632. *comm = lg->soc->communities[i];
  633. comm->regs = regs;
  634. comm->pad_regs = regs + 0x100;
  635. }
  636. raw_spin_lock_init(&lg->lock);
  637. gc = &lg->chip;
  638. gc->label = dev_name(dev);
  639. gc->owner = THIS_MODULE;
  640. gc->request = gpiochip_generic_request;
  641. gc->free = gpiochip_generic_free;
  642. gc->direction_input = lp_gpio_direction_input;
  643. gc->direction_output = lp_gpio_direction_output;
  644. gc->get = lp_gpio_get;
  645. gc->set = lp_gpio_set;
  646. gc->set_config = gpiochip_generic_config;
  647. gc->get_direction = lp_gpio_get_direction;
  648. gc->base = -1;
  649. gc->ngpio = LP_NUM_GPIO;
  650. gc->can_sleep = false;
  651. gc->add_pin_ranges = lp_gpio_add_pin_ranges;
  652. gc->parent = dev;
  653. /* set up interrupts */
  654. irq = platform_get_irq_optional(pdev, 0);
  655. if (irq > 0) {
  656. struct gpio_irq_chip *girq;
  657. girq = &gc->irq;
  658. gpio_irq_chip_set_chip(girq, &lp_irqchip);
  659. girq->init_hw = lp_gpio_irq_init_hw;
  660. girq->parent_handler = lp_gpio_irq_handler;
  661. girq->num_parents = 1;
  662. girq->parents = devm_kcalloc(dev, girq->num_parents,
  663. sizeof(*girq->parents),
  664. GFP_KERNEL);
  665. if (!girq->parents)
  666. return -ENOMEM;
  667. girq->parents[0] = irq;
  668. girq->default_type = IRQ_TYPE_NONE;
  669. girq->handler = handle_bad_irq;
  670. }
  671. ret = devm_gpiochip_add_data(dev, gc, lg);
  672. if (ret) {
  673. dev_err(dev, "failed adding lp-gpio chip\n");
  674. return ret;
  675. }
  676. return 0;
  677. }
  678. static int lp_gpio_resume(struct device *dev)
  679. {
  680. struct intel_pinctrl *lg = dev_get_drvdata(dev);
  681. struct gpio_chip *chip = &lg->chip;
  682. const char *dummy;
  683. int i;
  684. /* on some hardware suspend clears input sensing, re-enable it here */
  685. for_each_requested_gpio(chip, i, dummy)
  686. lp_gpio_enable_input(lp_gpio_reg(chip, i, LP_CONFIG2));
  687. return 0;
  688. }
  689. static DEFINE_SIMPLE_DEV_PM_OPS(lp_gpio_pm_ops, NULL, lp_gpio_resume);
  690. static const struct acpi_device_id lynxpoint_gpio_acpi_match[] = {
  691. { "INT33C7", (kernel_ulong_t)&lptlp_soc_data },
  692. { "INT3437", (kernel_ulong_t)&lptlp_soc_data },
  693. { }
  694. };
  695. MODULE_DEVICE_TABLE(acpi, lynxpoint_gpio_acpi_match);
  696. static struct platform_driver lp_gpio_driver = {
  697. .probe = lp_gpio_probe,
  698. .driver = {
  699. .name = "lp_gpio",
  700. .pm = pm_sleep_ptr(&lp_gpio_pm_ops),
  701. .acpi_match_table = lynxpoint_gpio_acpi_match,
  702. },
  703. };
  704. static int __init lp_gpio_init(void)
  705. {
  706. return platform_driver_register(&lp_gpio_driver);
  707. }
  708. subsys_initcall(lp_gpio_init);
  709. static void __exit lp_gpio_exit(void)
  710. {
  711. platform_driver_unregister(&lp_gpio_driver);
  712. }
  713. module_exit(lp_gpio_exit);
  714. MODULE_AUTHOR("Mathias Nyman (Intel)");
  715. MODULE_AUTHOR("Andy Shevchenko (Intel)");
  716. MODULE_DESCRIPTION("Intel Lynxpoint pinctrl driver");
  717. MODULE_LICENSE("GPL v2");
  718. MODULE_ALIAS("platform:lp_gpio");
  719. MODULE_IMPORT_NS(PINCTRL_INTEL);