pinctrl-lynxpoint.c 23 KB

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