pinctrl-lynxpoint.c 26 KB

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