pinctrl-intel.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433
  1. /*
  2. * Intel pinctrl/GPIO core driver.
  3. *
  4. * Copyright (C) 2015, Intel Corporation
  5. * Authors: Mathias Nyman <mathias.nyman@linux.intel.com>
  6. * Mika Westerberg <mika.westerberg@linux.intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/gpio/driver.h>
  15. #include <linux/log2.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pinctrl/pinctrl.h>
  18. #include <linux/pinctrl/pinmux.h>
  19. #include <linux/pinctrl/pinconf.h>
  20. #include <linux/pinctrl/pinconf-generic.h>
  21. #include "../core.h"
  22. #include "pinctrl-intel.h"
  23. /* Offset from regs */
  24. #define REVID 0x000
  25. #define REVID_SHIFT 16
  26. #define REVID_MASK GENMASK(31, 16)
  27. #define PADBAR 0x00c
  28. #define GPI_IS 0x100
  29. #define GPI_GPE_STS 0x140
  30. #define GPI_GPE_EN 0x160
  31. #define PADOWN_BITS 4
  32. #define PADOWN_SHIFT(p) ((p) % 8 * PADOWN_BITS)
  33. #define PADOWN_MASK(p) (0xf << PADOWN_SHIFT(p))
  34. #define PADOWN_GPP(p) ((p) / 8)
  35. /* Offset from pad_regs */
  36. #define PADCFG0 0x000
  37. #define PADCFG0_RXEVCFG_SHIFT 25
  38. #define PADCFG0_RXEVCFG_MASK (3 << PADCFG0_RXEVCFG_SHIFT)
  39. #define PADCFG0_RXEVCFG_LEVEL 0
  40. #define PADCFG0_RXEVCFG_EDGE 1
  41. #define PADCFG0_RXEVCFG_DISABLED 2
  42. #define PADCFG0_RXEVCFG_EDGE_BOTH 3
  43. #define PADCFG0_PREGFRXSEL BIT(24)
  44. #define PADCFG0_RXINV BIT(23)
  45. #define PADCFG0_GPIROUTIOXAPIC BIT(20)
  46. #define PADCFG0_GPIROUTSCI BIT(19)
  47. #define PADCFG0_GPIROUTSMI BIT(18)
  48. #define PADCFG0_GPIROUTNMI BIT(17)
  49. #define PADCFG0_PMODE_SHIFT 10
  50. #define PADCFG0_PMODE_MASK (0xf << PADCFG0_PMODE_SHIFT)
  51. #define PADCFG0_GPIORXDIS BIT(9)
  52. #define PADCFG0_GPIOTXDIS BIT(8)
  53. #define PADCFG0_GPIORXSTATE BIT(1)
  54. #define PADCFG0_GPIOTXSTATE BIT(0)
  55. #define PADCFG1 0x004
  56. #define PADCFG1_TERM_UP BIT(13)
  57. #define PADCFG1_TERM_SHIFT 10
  58. #define PADCFG1_TERM_MASK (7 << PADCFG1_TERM_SHIFT)
  59. #define PADCFG1_TERM_20K 4
  60. #define PADCFG1_TERM_2K 3
  61. #define PADCFG1_TERM_5K 2
  62. #define PADCFG1_TERM_1K 1
  63. #define PADCFG2 0x008
  64. #define PADCFG2_DEBEN BIT(0)
  65. #define PADCFG2_DEBOUNCE_SHIFT 1
  66. #define PADCFG2_DEBOUNCE_MASK GENMASK(4, 1)
  67. #define DEBOUNCE_PERIOD 31250 /* ns */
  68. struct intel_pad_context {
  69. u32 padcfg0;
  70. u32 padcfg1;
  71. u32 padcfg2;
  72. };
  73. struct intel_community_context {
  74. u32 *intmask;
  75. };
  76. struct intel_pinctrl_context {
  77. struct intel_pad_context *pads;
  78. struct intel_community_context *communities;
  79. };
  80. /**
  81. * struct intel_pinctrl - Intel pinctrl private structure
  82. * @dev: Pointer to the device structure
  83. * @lock: Lock to serialize register access
  84. * @pctldesc: Pin controller description
  85. * @pctldev: Pointer to the pin controller device
  86. * @chip: GPIO chip in this pin controller
  87. * @soc: SoC/PCH specific pin configuration data
  88. * @communities: All communities in this pin controller
  89. * @ncommunities: Number of communities in this pin controller
  90. * @context: Configuration saved over system sleep
  91. * @irq: pinctrl/GPIO chip irq number
  92. */
  93. struct intel_pinctrl {
  94. struct device *dev;
  95. raw_spinlock_t lock;
  96. struct pinctrl_desc pctldesc;
  97. struct pinctrl_dev *pctldev;
  98. struct gpio_chip chip;
  99. const struct intel_pinctrl_soc_data *soc;
  100. struct intel_community *communities;
  101. size_t ncommunities;
  102. struct intel_pinctrl_context context;
  103. int irq;
  104. };
  105. #define pin_to_padno(c, p) ((p) - (c)->pin_base)
  106. #define padgroup_offset(g, p) ((p) - (g)->base)
  107. static struct intel_community *intel_get_community(struct intel_pinctrl *pctrl,
  108. unsigned pin)
  109. {
  110. struct intel_community *community;
  111. int i;
  112. for (i = 0; i < pctrl->ncommunities; i++) {
  113. community = &pctrl->communities[i];
  114. if (pin >= community->pin_base &&
  115. pin < community->pin_base + community->npins)
  116. return community;
  117. }
  118. dev_warn(pctrl->dev, "failed to find community for pin %u\n", pin);
  119. return NULL;
  120. }
  121. static const struct intel_padgroup *
  122. intel_community_get_padgroup(const struct intel_community *community,
  123. unsigned pin)
  124. {
  125. int i;
  126. for (i = 0; i < community->ngpps; i++) {
  127. const struct intel_padgroup *padgrp = &community->gpps[i];
  128. if (pin >= padgrp->base && pin < padgrp->base + padgrp->size)
  129. return padgrp;
  130. }
  131. return NULL;
  132. }
  133. static void __iomem *intel_get_padcfg(struct intel_pinctrl *pctrl, unsigned pin,
  134. unsigned reg)
  135. {
  136. const struct intel_community *community;
  137. unsigned padno;
  138. size_t nregs;
  139. community = intel_get_community(pctrl, pin);
  140. if (!community)
  141. return NULL;
  142. padno = pin_to_padno(community, pin);
  143. nregs = (community->features & PINCTRL_FEATURE_DEBOUNCE) ? 4 : 2;
  144. if (reg == PADCFG2 && !(community->features & PINCTRL_FEATURE_DEBOUNCE))
  145. return NULL;
  146. return community->pad_regs + reg + padno * nregs * 4;
  147. }
  148. static bool intel_pad_owned_by_host(struct intel_pinctrl *pctrl, unsigned pin)
  149. {
  150. const struct intel_community *community;
  151. const struct intel_padgroup *padgrp;
  152. unsigned gpp, offset, gpp_offset;
  153. void __iomem *padown;
  154. community = intel_get_community(pctrl, pin);
  155. if (!community)
  156. return false;
  157. if (!community->padown_offset)
  158. return true;
  159. padgrp = intel_community_get_padgroup(community, pin);
  160. if (!padgrp)
  161. return false;
  162. gpp_offset = padgroup_offset(padgrp, pin);
  163. gpp = PADOWN_GPP(gpp_offset);
  164. offset = community->padown_offset + padgrp->padown_num * 4 + gpp * 4;
  165. padown = community->regs + offset;
  166. return !(readl(padown) & PADOWN_MASK(gpp_offset));
  167. }
  168. static bool intel_pad_acpi_mode(struct intel_pinctrl *pctrl, unsigned pin)
  169. {
  170. const struct intel_community *community;
  171. const struct intel_padgroup *padgrp;
  172. unsigned offset, gpp_offset;
  173. void __iomem *hostown;
  174. community = intel_get_community(pctrl, pin);
  175. if (!community)
  176. return true;
  177. if (!community->hostown_offset)
  178. return false;
  179. padgrp = intel_community_get_padgroup(community, pin);
  180. if (!padgrp)
  181. return true;
  182. gpp_offset = padgroup_offset(padgrp, pin);
  183. offset = community->hostown_offset + padgrp->reg_num * 4;
  184. hostown = community->regs + offset;
  185. return !(readl(hostown) & BIT(gpp_offset));
  186. }
  187. static bool intel_pad_locked(struct intel_pinctrl *pctrl, unsigned pin)
  188. {
  189. struct intel_community *community;
  190. const struct intel_padgroup *padgrp;
  191. unsigned offset, gpp_offset;
  192. u32 value;
  193. community = intel_get_community(pctrl, pin);
  194. if (!community)
  195. return true;
  196. if (!community->padcfglock_offset)
  197. return false;
  198. padgrp = intel_community_get_padgroup(community, pin);
  199. if (!padgrp)
  200. return true;
  201. gpp_offset = padgroup_offset(padgrp, pin);
  202. /*
  203. * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad,
  204. * the pad is considered unlocked. Any other case means that it is
  205. * either fully or partially locked and we don't touch it.
  206. */
  207. offset = community->padcfglock_offset + padgrp->reg_num * 8;
  208. value = readl(community->regs + offset);
  209. if (value & BIT(gpp_offset))
  210. return true;
  211. offset = community->padcfglock_offset + 4 + padgrp->reg_num * 8;
  212. value = readl(community->regs + offset);
  213. if (value & BIT(gpp_offset))
  214. return true;
  215. return false;
  216. }
  217. static bool intel_pad_usable(struct intel_pinctrl *pctrl, unsigned pin)
  218. {
  219. return intel_pad_owned_by_host(pctrl, pin) &&
  220. !intel_pad_locked(pctrl, pin);
  221. }
  222. static int intel_get_groups_count(struct pinctrl_dev *pctldev)
  223. {
  224. struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  225. return pctrl->soc->ngroups;
  226. }
  227. static const char *intel_get_group_name(struct pinctrl_dev *pctldev,
  228. unsigned group)
  229. {
  230. struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  231. return pctrl->soc->groups[group].name;
  232. }
  233. static int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned group,
  234. const unsigned **pins, unsigned *npins)
  235. {
  236. struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  237. *pins = pctrl->soc->groups[group].pins;
  238. *npins = pctrl->soc->groups[group].npins;
  239. return 0;
  240. }
  241. static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
  242. unsigned pin)
  243. {
  244. struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  245. void __iomem *padcfg;
  246. u32 cfg0, cfg1, mode;
  247. bool locked, acpi;
  248. if (!intel_pad_owned_by_host(pctrl, pin)) {
  249. seq_puts(s, "not available");
  250. return;
  251. }
  252. cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
  253. cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
  254. mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
  255. if (!mode)
  256. seq_puts(s, "GPIO ");
  257. else
  258. seq_printf(s, "mode %d ", mode);
  259. seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1);
  260. /* Dump the additional PADCFG registers if available */
  261. padcfg = intel_get_padcfg(pctrl, pin, PADCFG2);
  262. if (padcfg)
  263. seq_printf(s, " 0x%08x", readl(padcfg));
  264. locked = intel_pad_locked(pctrl, pin);
  265. acpi = intel_pad_acpi_mode(pctrl, pin);
  266. if (locked || acpi) {
  267. seq_puts(s, " [");
  268. if (locked) {
  269. seq_puts(s, "LOCKED");
  270. if (acpi)
  271. seq_puts(s, ", ");
  272. }
  273. if (acpi)
  274. seq_puts(s, "ACPI");
  275. seq_puts(s, "]");
  276. }
  277. }
  278. static const struct pinctrl_ops intel_pinctrl_ops = {
  279. .get_groups_count = intel_get_groups_count,
  280. .get_group_name = intel_get_group_name,
  281. .get_group_pins = intel_get_group_pins,
  282. .pin_dbg_show = intel_pin_dbg_show,
  283. };
  284. static int intel_get_functions_count(struct pinctrl_dev *pctldev)
  285. {
  286. struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  287. return pctrl->soc->nfunctions;
  288. }
  289. static const char *intel_get_function_name(struct pinctrl_dev *pctldev,
  290. unsigned function)
  291. {
  292. struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  293. return pctrl->soc->functions[function].name;
  294. }
  295. static int intel_get_function_groups(struct pinctrl_dev *pctldev,
  296. unsigned function,
  297. const char * const **groups,
  298. unsigned * const ngroups)
  299. {
  300. struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  301. *groups = pctrl->soc->functions[function].groups;
  302. *ngroups = pctrl->soc->functions[function].ngroups;
  303. return 0;
  304. }
  305. static int intel_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned function,
  306. unsigned group)
  307. {
  308. struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  309. const struct intel_pingroup *grp = &pctrl->soc->groups[group];
  310. unsigned long flags;
  311. int i;
  312. raw_spin_lock_irqsave(&pctrl->lock, flags);
  313. /*
  314. * All pins in the groups needs to be accessible and writable
  315. * before we can enable the mux for this group.
  316. */
  317. for (i = 0; i < grp->npins; i++) {
  318. if (!intel_pad_usable(pctrl, grp->pins[i])) {
  319. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  320. return -EBUSY;
  321. }
  322. }
  323. /* Now enable the mux setting for each pin in the group */
  324. for (i = 0; i < grp->npins; i++) {
  325. void __iomem *padcfg0;
  326. u32 value;
  327. padcfg0 = intel_get_padcfg(pctrl, grp->pins[i], PADCFG0);
  328. value = readl(padcfg0);
  329. value &= ~PADCFG0_PMODE_MASK;
  330. if (grp->modes)
  331. value |= grp->modes[i] << PADCFG0_PMODE_SHIFT;
  332. else
  333. value |= grp->mode << PADCFG0_PMODE_SHIFT;
  334. writel(value, padcfg0);
  335. }
  336. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  337. return 0;
  338. }
  339. static void __intel_gpio_set_direction(void __iomem *padcfg0, bool input)
  340. {
  341. u32 value;
  342. value = readl(padcfg0);
  343. if (input) {
  344. value &= ~PADCFG0_GPIORXDIS;
  345. value |= PADCFG0_GPIOTXDIS;
  346. } else {
  347. value &= ~PADCFG0_GPIOTXDIS;
  348. value |= PADCFG0_GPIORXDIS;
  349. }
  350. writel(value, padcfg0);
  351. }
  352. static int intel_gpio_request_enable(struct pinctrl_dev *pctldev,
  353. struct pinctrl_gpio_range *range,
  354. unsigned pin)
  355. {
  356. struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  357. void __iomem *padcfg0;
  358. unsigned long flags;
  359. u32 value;
  360. raw_spin_lock_irqsave(&pctrl->lock, flags);
  361. if (!intel_pad_usable(pctrl, pin)) {
  362. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  363. return -EBUSY;
  364. }
  365. padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
  366. /* Put the pad into GPIO mode */
  367. value = readl(padcfg0) & ~PADCFG0_PMODE_MASK;
  368. /* Disable SCI/SMI/NMI generation */
  369. value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI);
  370. value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI);
  371. writel(value, padcfg0);
  372. /* Disable TX buffer and enable RX (this will be input) */
  373. __intel_gpio_set_direction(padcfg0, true);
  374. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  375. return 0;
  376. }
  377. static int intel_gpio_set_direction(struct pinctrl_dev *pctldev,
  378. struct pinctrl_gpio_range *range,
  379. unsigned pin, bool input)
  380. {
  381. struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  382. void __iomem *padcfg0;
  383. unsigned long flags;
  384. raw_spin_lock_irqsave(&pctrl->lock, flags);
  385. padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
  386. __intel_gpio_set_direction(padcfg0, input);
  387. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  388. return 0;
  389. }
  390. static const struct pinmux_ops intel_pinmux_ops = {
  391. .get_functions_count = intel_get_functions_count,
  392. .get_function_name = intel_get_function_name,
  393. .get_function_groups = intel_get_function_groups,
  394. .set_mux = intel_pinmux_set_mux,
  395. .gpio_request_enable = intel_gpio_request_enable,
  396. .gpio_set_direction = intel_gpio_set_direction,
  397. };
  398. static int intel_config_get(struct pinctrl_dev *pctldev, unsigned pin,
  399. unsigned long *config)
  400. {
  401. struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  402. enum pin_config_param param = pinconf_to_config_param(*config);
  403. const struct intel_community *community;
  404. u32 value, term;
  405. u32 arg = 0;
  406. if (!intel_pad_owned_by_host(pctrl, pin))
  407. return -ENOTSUPP;
  408. community = intel_get_community(pctrl, pin);
  409. value = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
  410. term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT;
  411. switch (param) {
  412. case PIN_CONFIG_BIAS_DISABLE:
  413. if (term)
  414. return -EINVAL;
  415. break;
  416. case PIN_CONFIG_BIAS_PULL_UP:
  417. if (!term || !(value & PADCFG1_TERM_UP))
  418. return -EINVAL;
  419. switch (term) {
  420. case PADCFG1_TERM_1K:
  421. arg = 1000;
  422. break;
  423. case PADCFG1_TERM_2K:
  424. arg = 2000;
  425. break;
  426. case PADCFG1_TERM_5K:
  427. arg = 5000;
  428. break;
  429. case PADCFG1_TERM_20K:
  430. arg = 20000;
  431. break;
  432. }
  433. break;
  434. case PIN_CONFIG_BIAS_PULL_DOWN:
  435. if (!term || value & PADCFG1_TERM_UP)
  436. return -EINVAL;
  437. switch (term) {
  438. case PADCFG1_TERM_1K:
  439. if (!(community->features & PINCTRL_FEATURE_1K_PD))
  440. return -EINVAL;
  441. arg = 1000;
  442. break;
  443. case PADCFG1_TERM_5K:
  444. arg = 5000;
  445. break;
  446. case PADCFG1_TERM_20K:
  447. arg = 20000;
  448. break;
  449. }
  450. break;
  451. case PIN_CONFIG_INPUT_DEBOUNCE: {
  452. void __iomem *padcfg2;
  453. u32 v;
  454. padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
  455. if (!padcfg2)
  456. return -ENOTSUPP;
  457. v = readl(padcfg2);
  458. if (!(v & PADCFG2_DEBEN))
  459. return -EINVAL;
  460. v = (v & PADCFG2_DEBOUNCE_MASK) >> PADCFG2_DEBOUNCE_SHIFT;
  461. arg = BIT(v) * DEBOUNCE_PERIOD / 1000;
  462. break;
  463. }
  464. default:
  465. return -ENOTSUPP;
  466. }
  467. *config = pinconf_to_config_packed(param, arg);
  468. return 0;
  469. }
  470. static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned pin,
  471. unsigned long config)
  472. {
  473. unsigned param = pinconf_to_config_param(config);
  474. unsigned arg = pinconf_to_config_argument(config);
  475. const struct intel_community *community;
  476. void __iomem *padcfg1;
  477. unsigned long flags;
  478. int ret = 0;
  479. u32 value;
  480. raw_spin_lock_irqsave(&pctrl->lock, flags);
  481. community = intel_get_community(pctrl, pin);
  482. padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
  483. value = readl(padcfg1);
  484. switch (param) {
  485. case PIN_CONFIG_BIAS_DISABLE:
  486. value &= ~(PADCFG1_TERM_MASK | PADCFG1_TERM_UP);
  487. break;
  488. case PIN_CONFIG_BIAS_PULL_UP:
  489. value &= ~PADCFG1_TERM_MASK;
  490. value |= PADCFG1_TERM_UP;
  491. switch (arg) {
  492. case 20000:
  493. value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
  494. break;
  495. case 5000:
  496. value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
  497. break;
  498. case 2000:
  499. value |= PADCFG1_TERM_2K << PADCFG1_TERM_SHIFT;
  500. break;
  501. case 1000:
  502. value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
  503. break;
  504. default:
  505. ret = -EINVAL;
  506. }
  507. break;
  508. case PIN_CONFIG_BIAS_PULL_DOWN:
  509. value &= ~(PADCFG1_TERM_UP | PADCFG1_TERM_MASK);
  510. switch (arg) {
  511. case 20000:
  512. value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
  513. break;
  514. case 5000:
  515. value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
  516. break;
  517. case 1000:
  518. if (!(community->features & PINCTRL_FEATURE_1K_PD)) {
  519. ret = -EINVAL;
  520. break;
  521. }
  522. value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
  523. break;
  524. default:
  525. ret = -EINVAL;
  526. }
  527. break;
  528. }
  529. if (!ret)
  530. writel(value, padcfg1);
  531. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  532. return ret;
  533. }
  534. static int intel_config_set_debounce(struct intel_pinctrl *pctrl, unsigned pin,
  535. unsigned debounce)
  536. {
  537. void __iomem *padcfg0, *padcfg2;
  538. unsigned long flags;
  539. u32 value0, value2;
  540. int ret = 0;
  541. padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
  542. if (!padcfg2)
  543. return -ENOTSUPP;
  544. padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
  545. raw_spin_lock_irqsave(&pctrl->lock, flags);
  546. value0 = readl(padcfg0);
  547. value2 = readl(padcfg2);
  548. /* Disable glitch filter and debouncer */
  549. value0 &= ~PADCFG0_PREGFRXSEL;
  550. value2 &= ~(PADCFG2_DEBEN | PADCFG2_DEBOUNCE_MASK);
  551. if (debounce) {
  552. unsigned long v;
  553. v = order_base_2(debounce * 1000 / DEBOUNCE_PERIOD);
  554. if (v < 3 || v > 15) {
  555. ret = -EINVAL;
  556. goto exit_unlock;
  557. } else {
  558. /* Enable glitch filter and debouncer */
  559. value0 |= PADCFG0_PREGFRXSEL;
  560. value2 |= v << PADCFG2_DEBOUNCE_SHIFT;
  561. value2 |= PADCFG2_DEBEN;
  562. }
  563. }
  564. writel(value0, padcfg0);
  565. writel(value2, padcfg2);
  566. exit_unlock:
  567. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  568. return ret;
  569. }
  570. static int intel_config_set(struct pinctrl_dev *pctldev, unsigned pin,
  571. unsigned long *configs, unsigned nconfigs)
  572. {
  573. struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  574. int i, ret;
  575. if (!intel_pad_usable(pctrl, pin))
  576. return -ENOTSUPP;
  577. for (i = 0; i < nconfigs; i++) {
  578. switch (pinconf_to_config_param(configs[i])) {
  579. case PIN_CONFIG_BIAS_DISABLE:
  580. case PIN_CONFIG_BIAS_PULL_UP:
  581. case PIN_CONFIG_BIAS_PULL_DOWN:
  582. ret = intel_config_set_pull(pctrl, pin, configs[i]);
  583. if (ret)
  584. return ret;
  585. break;
  586. case PIN_CONFIG_INPUT_DEBOUNCE:
  587. ret = intel_config_set_debounce(pctrl, pin,
  588. pinconf_to_config_argument(configs[i]));
  589. if (ret)
  590. return ret;
  591. break;
  592. default:
  593. return -ENOTSUPP;
  594. }
  595. }
  596. return 0;
  597. }
  598. static const struct pinconf_ops intel_pinconf_ops = {
  599. .is_generic = true,
  600. .pin_config_get = intel_config_get,
  601. .pin_config_set = intel_config_set,
  602. };
  603. static const struct pinctrl_desc intel_pinctrl_desc = {
  604. .pctlops = &intel_pinctrl_ops,
  605. .pmxops = &intel_pinmux_ops,
  606. .confops = &intel_pinconf_ops,
  607. .owner = THIS_MODULE,
  608. };
  609. static int intel_gpio_get(struct gpio_chip *chip, unsigned offset)
  610. {
  611. struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
  612. void __iomem *reg;
  613. u32 padcfg0;
  614. reg = intel_get_padcfg(pctrl, offset, PADCFG0);
  615. if (!reg)
  616. return -EINVAL;
  617. padcfg0 = readl(reg);
  618. if (!(padcfg0 & PADCFG0_GPIOTXDIS))
  619. return !!(padcfg0 & PADCFG0_GPIOTXSTATE);
  620. return !!(padcfg0 & PADCFG0_GPIORXSTATE);
  621. }
  622. static void intel_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  623. {
  624. struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
  625. unsigned long flags;
  626. void __iomem *reg;
  627. u32 padcfg0;
  628. reg = intel_get_padcfg(pctrl, offset, PADCFG0);
  629. if (!reg)
  630. return;
  631. raw_spin_lock_irqsave(&pctrl->lock, flags);
  632. padcfg0 = readl(reg);
  633. if (value)
  634. padcfg0 |= PADCFG0_GPIOTXSTATE;
  635. else
  636. padcfg0 &= ~PADCFG0_GPIOTXSTATE;
  637. writel(padcfg0, reg);
  638. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  639. }
  640. static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  641. {
  642. return pinctrl_gpio_direction_input(chip->base + offset);
  643. }
  644. static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  645. int value)
  646. {
  647. intel_gpio_set(chip, offset, value);
  648. return pinctrl_gpio_direction_output(chip->base + offset);
  649. }
  650. static const struct gpio_chip intel_gpio_chip = {
  651. .owner = THIS_MODULE,
  652. .request = gpiochip_generic_request,
  653. .free = gpiochip_generic_free,
  654. .direction_input = intel_gpio_direction_input,
  655. .direction_output = intel_gpio_direction_output,
  656. .get = intel_gpio_get,
  657. .set = intel_gpio_set,
  658. .set_config = gpiochip_generic_config,
  659. };
  660. static void intel_gpio_irq_ack(struct irq_data *d)
  661. {
  662. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  663. struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
  664. const struct intel_community *community;
  665. unsigned pin = irqd_to_hwirq(d);
  666. community = intel_get_community(pctrl, pin);
  667. if (community) {
  668. const struct intel_padgroup *padgrp;
  669. unsigned gpp, gpp_offset;
  670. padgrp = intel_community_get_padgroup(community, pin);
  671. if (!padgrp)
  672. return;
  673. gpp = padgrp->reg_num;
  674. gpp_offset = padgroup_offset(padgrp, pin);
  675. raw_spin_lock(&pctrl->lock);
  676. writel(BIT(gpp_offset), community->regs + GPI_IS + gpp * 4);
  677. raw_spin_unlock(&pctrl->lock);
  678. }
  679. }
  680. static void intel_gpio_irq_enable(struct irq_data *d)
  681. {
  682. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  683. struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
  684. const struct intel_community *community;
  685. unsigned pin = irqd_to_hwirq(d);
  686. community = intel_get_community(pctrl, pin);
  687. if (community) {
  688. const struct intel_padgroup *padgrp;
  689. unsigned gpp, gpp_offset;
  690. unsigned long flags;
  691. u32 value;
  692. padgrp = intel_community_get_padgroup(community, pin);
  693. if (!padgrp)
  694. return;
  695. gpp = padgrp->reg_num;
  696. gpp_offset = padgroup_offset(padgrp, pin);
  697. raw_spin_lock_irqsave(&pctrl->lock, flags);
  698. /* Clear interrupt status first to avoid unexpected interrupt */
  699. writel(BIT(gpp_offset), community->regs + GPI_IS + gpp * 4);
  700. value = readl(community->regs + community->ie_offset + gpp * 4);
  701. value |= BIT(gpp_offset);
  702. writel(value, community->regs + community->ie_offset + gpp * 4);
  703. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  704. }
  705. }
  706. static void intel_gpio_irq_mask_unmask(struct irq_data *d, bool mask)
  707. {
  708. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  709. struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
  710. const struct intel_community *community;
  711. unsigned pin = irqd_to_hwirq(d);
  712. community = intel_get_community(pctrl, pin);
  713. if (community) {
  714. const struct intel_padgroup *padgrp;
  715. unsigned gpp, gpp_offset;
  716. unsigned long flags;
  717. void __iomem *reg;
  718. u32 value;
  719. padgrp = intel_community_get_padgroup(community, pin);
  720. if (!padgrp)
  721. return;
  722. gpp = padgrp->reg_num;
  723. gpp_offset = padgroup_offset(padgrp, pin);
  724. reg = community->regs + community->ie_offset + gpp * 4;
  725. raw_spin_lock_irqsave(&pctrl->lock, flags);
  726. value = readl(reg);
  727. if (mask)
  728. value &= ~BIT(gpp_offset);
  729. else
  730. value |= BIT(gpp_offset);
  731. writel(value, reg);
  732. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  733. }
  734. }
  735. static void intel_gpio_irq_mask(struct irq_data *d)
  736. {
  737. intel_gpio_irq_mask_unmask(d, true);
  738. }
  739. static void intel_gpio_irq_unmask(struct irq_data *d)
  740. {
  741. intel_gpio_irq_mask_unmask(d, false);
  742. }
  743. static int intel_gpio_irq_type(struct irq_data *d, unsigned type)
  744. {
  745. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  746. struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
  747. unsigned pin = irqd_to_hwirq(d);
  748. unsigned long flags;
  749. void __iomem *reg;
  750. u32 value;
  751. reg = intel_get_padcfg(pctrl, pin, PADCFG0);
  752. if (!reg)
  753. return -EINVAL;
  754. /*
  755. * If the pin is in ACPI mode it is still usable as a GPIO but it
  756. * cannot be used as IRQ because GPI_IS status bit will not be
  757. * updated by the host controller hardware.
  758. */
  759. if (intel_pad_acpi_mode(pctrl, pin)) {
  760. dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin);
  761. return -EPERM;
  762. }
  763. raw_spin_lock_irqsave(&pctrl->lock, flags);
  764. value = readl(reg);
  765. value &= ~(PADCFG0_RXEVCFG_MASK | PADCFG0_RXINV);
  766. if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
  767. value |= PADCFG0_RXEVCFG_EDGE_BOTH << PADCFG0_RXEVCFG_SHIFT;
  768. } else if (type & IRQ_TYPE_EDGE_FALLING) {
  769. value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
  770. value |= PADCFG0_RXINV;
  771. } else if (type & IRQ_TYPE_EDGE_RISING) {
  772. value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
  773. } else if (type & IRQ_TYPE_LEVEL_MASK) {
  774. if (type & IRQ_TYPE_LEVEL_LOW)
  775. value |= PADCFG0_RXINV;
  776. } else {
  777. value |= PADCFG0_RXEVCFG_DISABLED << PADCFG0_RXEVCFG_SHIFT;
  778. }
  779. writel(value, reg);
  780. if (type & IRQ_TYPE_EDGE_BOTH)
  781. irq_set_handler_locked(d, handle_edge_irq);
  782. else if (type & IRQ_TYPE_LEVEL_MASK)
  783. irq_set_handler_locked(d, handle_level_irq);
  784. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  785. return 0;
  786. }
  787. static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on)
  788. {
  789. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  790. struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
  791. unsigned pin = irqd_to_hwirq(d);
  792. if (on)
  793. enable_irq_wake(pctrl->irq);
  794. else
  795. disable_irq_wake(pctrl->irq);
  796. dev_dbg(pctrl->dev, "%sable wake for pin %u\n", on ? "en" : "dis", pin);
  797. return 0;
  798. }
  799. static irqreturn_t intel_gpio_community_irq_handler(struct intel_pinctrl *pctrl,
  800. const struct intel_community *community)
  801. {
  802. struct gpio_chip *gc = &pctrl->chip;
  803. irqreturn_t ret = IRQ_NONE;
  804. int gpp;
  805. for (gpp = 0; gpp < community->ngpps; gpp++) {
  806. const struct intel_padgroup *padgrp = &community->gpps[gpp];
  807. unsigned long pending, enabled, gpp_offset;
  808. pending = readl(community->regs + GPI_IS + padgrp->reg_num * 4);
  809. enabled = readl(community->regs + community->ie_offset +
  810. padgrp->reg_num * 4);
  811. /* Only interrupts that are enabled */
  812. pending &= enabled;
  813. for_each_set_bit(gpp_offset, &pending, padgrp->size) {
  814. unsigned padno, irq;
  815. padno = padgrp->base - community->pin_base + gpp_offset;
  816. if (padno >= community->npins)
  817. break;
  818. irq = irq_find_mapping(gc->irq.domain,
  819. community->pin_base + padno);
  820. generic_handle_irq(irq);
  821. ret |= IRQ_HANDLED;
  822. }
  823. }
  824. return ret;
  825. }
  826. static irqreturn_t intel_gpio_irq(int irq, void *data)
  827. {
  828. const struct intel_community *community;
  829. struct intel_pinctrl *pctrl = data;
  830. irqreturn_t ret = IRQ_NONE;
  831. int i;
  832. /* Need to check all communities for pending interrupts */
  833. for (i = 0; i < pctrl->ncommunities; i++) {
  834. community = &pctrl->communities[i];
  835. ret |= intel_gpio_community_irq_handler(pctrl, community);
  836. }
  837. return ret;
  838. }
  839. static struct irq_chip intel_gpio_irqchip = {
  840. .name = "intel-gpio",
  841. .irq_enable = intel_gpio_irq_enable,
  842. .irq_ack = intel_gpio_irq_ack,
  843. .irq_mask = intel_gpio_irq_mask,
  844. .irq_unmask = intel_gpio_irq_unmask,
  845. .irq_set_type = intel_gpio_irq_type,
  846. .irq_set_wake = intel_gpio_irq_wake,
  847. .flags = IRQCHIP_MASK_ON_SUSPEND,
  848. };
  849. static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq)
  850. {
  851. int ret;
  852. pctrl->chip = intel_gpio_chip;
  853. pctrl->chip.ngpio = pctrl->soc->npins;
  854. pctrl->chip.label = dev_name(pctrl->dev);
  855. pctrl->chip.parent = pctrl->dev;
  856. pctrl->chip.base = -1;
  857. pctrl->irq = irq;
  858. ret = devm_gpiochip_add_data(pctrl->dev, &pctrl->chip, pctrl);
  859. if (ret) {
  860. dev_err(pctrl->dev, "failed to register gpiochip\n");
  861. return ret;
  862. }
  863. ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev),
  864. 0, 0, pctrl->soc->npins);
  865. if (ret) {
  866. dev_err(pctrl->dev, "failed to add GPIO pin range\n");
  867. return ret;
  868. }
  869. /*
  870. * We need to request the interrupt here (instead of providing chip
  871. * to the irq directly) because on some platforms several GPIO
  872. * controllers share the same interrupt line.
  873. */
  874. ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq,
  875. IRQF_SHARED | IRQF_NO_THREAD,
  876. dev_name(pctrl->dev), pctrl);
  877. if (ret) {
  878. dev_err(pctrl->dev, "failed to request interrupt\n");
  879. return ret;
  880. }
  881. ret = gpiochip_irqchip_add(&pctrl->chip, &intel_gpio_irqchip, 0,
  882. handle_bad_irq, IRQ_TYPE_NONE);
  883. if (ret) {
  884. dev_err(pctrl->dev, "failed to add irqchip\n");
  885. return ret;
  886. }
  887. gpiochip_set_chained_irqchip(&pctrl->chip, &intel_gpio_irqchip, irq,
  888. NULL);
  889. return 0;
  890. }
  891. static int intel_pinctrl_add_padgroups(struct intel_pinctrl *pctrl,
  892. struct intel_community *community)
  893. {
  894. struct intel_padgroup *gpps;
  895. unsigned npins = community->npins;
  896. unsigned padown_num = 0;
  897. size_t ngpps, i;
  898. if (community->gpps)
  899. ngpps = community->ngpps;
  900. else
  901. ngpps = DIV_ROUND_UP(community->npins, community->gpp_size);
  902. gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
  903. if (!gpps)
  904. return -ENOMEM;
  905. for (i = 0; i < ngpps; i++) {
  906. if (community->gpps) {
  907. gpps[i] = community->gpps[i];
  908. } else {
  909. unsigned gpp_size = community->gpp_size;
  910. gpps[i].reg_num = i;
  911. gpps[i].base = community->pin_base + i * gpp_size;
  912. gpps[i].size = min(gpp_size, npins);
  913. npins -= gpps[i].size;
  914. }
  915. if (gpps[i].size > 32)
  916. return -EINVAL;
  917. gpps[i].padown_num = padown_num;
  918. /*
  919. * In older hardware the number of padown registers per
  920. * group is fixed regardless of the group size.
  921. */
  922. if (community->gpp_num_padown_regs)
  923. padown_num += community->gpp_num_padown_regs;
  924. else
  925. padown_num += DIV_ROUND_UP(gpps[i].size * 4, 32);
  926. }
  927. community->ngpps = ngpps;
  928. community->gpps = gpps;
  929. return 0;
  930. }
  931. static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl)
  932. {
  933. #ifdef CONFIG_PM_SLEEP
  934. const struct intel_pinctrl_soc_data *soc = pctrl->soc;
  935. struct intel_community_context *communities;
  936. struct intel_pad_context *pads;
  937. int i;
  938. pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL);
  939. if (!pads)
  940. return -ENOMEM;
  941. communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities,
  942. sizeof(*communities), GFP_KERNEL);
  943. if (!communities)
  944. return -ENOMEM;
  945. for (i = 0; i < pctrl->ncommunities; i++) {
  946. struct intel_community *community = &pctrl->communities[i];
  947. u32 *intmask;
  948. intmask = devm_kcalloc(pctrl->dev, community->ngpps,
  949. sizeof(*intmask), GFP_KERNEL);
  950. if (!intmask)
  951. return -ENOMEM;
  952. communities[i].intmask = intmask;
  953. }
  954. pctrl->context.pads = pads;
  955. pctrl->context.communities = communities;
  956. #endif
  957. return 0;
  958. }
  959. int intel_pinctrl_probe(struct platform_device *pdev,
  960. const struct intel_pinctrl_soc_data *soc_data)
  961. {
  962. struct intel_pinctrl *pctrl;
  963. int i, ret, irq;
  964. if (!soc_data)
  965. return -EINVAL;
  966. pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
  967. if (!pctrl)
  968. return -ENOMEM;
  969. pctrl->dev = &pdev->dev;
  970. pctrl->soc = soc_data;
  971. raw_spin_lock_init(&pctrl->lock);
  972. /*
  973. * Make a copy of the communities which we can use to hold pointers
  974. * to the registers.
  975. */
  976. pctrl->ncommunities = pctrl->soc->ncommunities;
  977. pctrl->communities = devm_kcalloc(&pdev->dev, pctrl->ncommunities,
  978. sizeof(*pctrl->communities), GFP_KERNEL);
  979. if (!pctrl->communities)
  980. return -ENOMEM;
  981. for (i = 0; i < pctrl->ncommunities; i++) {
  982. struct intel_community *community = &pctrl->communities[i];
  983. struct resource *res;
  984. void __iomem *regs;
  985. u32 padbar;
  986. *community = pctrl->soc->communities[i];
  987. res = platform_get_resource(pdev, IORESOURCE_MEM,
  988. community->barno);
  989. regs = devm_ioremap_resource(&pdev->dev, res);
  990. if (IS_ERR(regs))
  991. return PTR_ERR(regs);
  992. /*
  993. * Determine community features based on the revision if
  994. * not specified already.
  995. */
  996. if (!community->features) {
  997. u32 rev;
  998. rev = (readl(regs + REVID) & REVID_MASK) >> REVID_SHIFT;
  999. if (rev >= 0x94) {
  1000. community->features |= PINCTRL_FEATURE_DEBOUNCE;
  1001. community->features |= PINCTRL_FEATURE_1K_PD;
  1002. }
  1003. }
  1004. /* Read offset of the pad configuration registers */
  1005. padbar = readl(regs + PADBAR);
  1006. community->regs = regs;
  1007. community->pad_regs = regs + padbar;
  1008. ret = intel_pinctrl_add_padgroups(pctrl, community);
  1009. if (ret)
  1010. return ret;
  1011. }
  1012. irq = platform_get_irq(pdev, 0);
  1013. if (irq < 0) {
  1014. dev_err(&pdev->dev, "failed to get interrupt number\n");
  1015. return irq;
  1016. }
  1017. ret = intel_pinctrl_pm_init(pctrl);
  1018. if (ret)
  1019. return ret;
  1020. pctrl->pctldesc = intel_pinctrl_desc;
  1021. pctrl->pctldesc.name = dev_name(&pdev->dev);
  1022. pctrl->pctldesc.pins = pctrl->soc->pins;
  1023. pctrl->pctldesc.npins = pctrl->soc->npins;
  1024. pctrl->pctldev = devm_pinctrl_register(&pdev->dev, &pctrl->pctldesc,
  1025. pctrl);
  1026. if (IS_ERR(pctrl->pctldev)) {
  1027. dev_err(&pdev->dev, "failed to register pinctrl driver\n");
  1028. return PTR_ERR(pctrl->pctldev);
  1029. }
  1030. ret = intel_gpio_probe(pctrl, irq);
  1031. if (ret)
  1032. return ret;
  1033. platform_set_drvdata(pdev, pctrl);
  1034. return 0;
  1035. }
  1036. EXPORT_SYMBOL_GPL(intel_pinctrl_probe);
  1037. #ifdef CONFIG_PM_SLEEP
  1038. static bool intel_pinctrl_should_save(struct intel_pinctrl *pctrl, unsigned pin)
  1039. {
  1040. const struct pin_desc *pd = pin_desc_get(pctrl->pctldev, pin);
  1041. if (!pd || !intel_pad_usable(pctrl, pin))
  1042. return false;
  1043. /*
  1044. * Only restore the pin if it is actually in use by the kernel (or
  1045. * by userspace). It is possible that some pins are used by the
  1046. * BIOS during resume and those are not always locked down so leave
  1047. * them alone.
  1048. */
  1049. if (pd->mux_owner || pd->gpio_owner ||
  1050. gpiochip_line_is_irq(&pctrl->chip, pin))
  1051. return true;
  1052. return false;
  1053. }
  1054. int intel_pinctrl_suspend(struct device *dev)
  1055. {
  1056. struct platform_device *pdev = to_platform_device(dev);
  1057. struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
  1058. struct intel_community_context *communities;
  1059. struct intel_pad_context *pads;
  1060. int i;
  1061. pads = pctrl->context.pads;
  1062. for (i = 0; i < pctrl->soc->npins; i++) {
  1063. const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
  1064. void __iomem *padcfg;
  1065. u32 val;
  1066. if (!intel_pinctrl_should_save(pctrl, desc->number))
  1067. continue;
  1068. val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0));
  1069. pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE;
  1070. val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1));
  1071. pads[i].padcfg1 = val;
  1072. padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
  1073. if (padcfg)
  1074. pads[i].padcfg2 = readl(padcfg);
  1075. }
  1076. communities = pctrl->context.communities;
  1077. for (i = 0; i < pctrl->ncommunities; i++) {
  1078. struct intel_community *community = &pctrl->communities[i];
  1079. void __iomem *base;
  1080. unsigned gpp;
  1081. base = community->regs + community->ie_offset;
  1082. for (gpp = 0; gpp < community->ngpps; gpp++)
  1083. communities[i].intmask[gpp] = readl(base + gpp * 4);
  1084. }
  1085. return 0;
  1086. }
  1087. EXPORT_SYMBOL_GPL(intel_pinctrl_suspend);
  1088. static void intel_gpio_irq_init(struct intel_pinctrl *pctrl)
  1089. {
  1090. size_t i;
  1091. for (i = 0; i < pctrl->ncommunities; i++) {
  1092. const struct intel_community *community;
  1093. void __iomem *base;
  1094. unsigned gpp;
  1095. community = &pctrl->communities[i];
  1096. base = community->regs;
  1097. for (gpp = 0; gpp < community->ngpps; gpp++) {
  1098. /* Mask and clear all interrupts */
  1099. writel(0, base + community->ie_offset + gpp * 4);
  1100. writel(0xffff, base + GPI_IS + gpp * 4);
  1101. }
  1102. }
  1103. }
  1104. int intel_pinctrl_resume(struct device *dev)
  1105. {
  1106. struct platform_device *pdev = to_platform_device(dev);
  1107. struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
  1108. const struct intel_community_context *communities;
  1109. const struct intel_pad_context *pads;
  1110. int i;
  1111. /* Mask all interrupts */
  1112. intel_gpio_irq_init(pctrl);
  1113. pads = pctrl->context.pads;
  1114. for (i = 0; i < pctrl->soc->npins; i++) {
  1115. const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
  1116. void __iomem *padcfg;
  1117. u32 val;
  1118. if (!intel_pinctrl_should_save(pctrl, desc->number))
  1119. continue;
  1120. padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG0);
  1121. val = readl(padcfg) & ~PADCFG0_GPIORXSTATE;
  1122. if (val != pads[i].padcfg0) {
  1123. writel(pads[i].padcfg0, padcfg);
  1124. dev_dbg(dev, "restored pin %u padcfg0 %#08x\n",
  1125. desc->number, readl(padcfg));
  1126. }
  1127. padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG1);
  1128. val = readl(padcfg);
  1129. if (val != pads[i].padcfg1) {
  1130. writel(pads[i].padcfg1, padcfg);
  1131. dev_dbg(dev, "restored pin %u padcfg1 %#08x\n",
  1132. desc->number, readl(padcfg));
  1133. }
  1134. padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
  1135. if (padcfg) {
  1136. val = readl(padcfg);
  1137. if (val != pads[i].padcfg2) {
  1138. writel(pads[i].padcfg2, padcfg);
  1139. dev_dbg(dev, "restored pin %u padcfg2 %#08x\n",
  1140. desc->number, readl(padcfg));
  1141. }
  1142. }
  1143. }
  1144. communities = pctrl->context.communities;
  1145. for (i = 0; i < pctrl->ncommunities; i++) {
  1146. struct intel_community *community = &pctrl->communities[i];
  1147. void __iomem *base;
  1148. unsigned gpp;
  1149. base = community->regs + community->ie_offset;
  1150. for (gpp = 0; gpp < community->ngpps; gpp++) {
  1151. writel(communities[i].intmask[gpp], base + gpp * 4);
  1152. dev_dbg(dev, "restored mask %d/%u %#08x\n", i, gpp,
  1153. readl(base + gpp * 4));
  1154. }
  1155. }
  1156. return 0;
  1157. }
  1158. EXPORT_SYMBOL_GPL(intel_pinctrl_resume);
  1159. #endif
  1160. MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>");
  1161. MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
  1162. MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
  1163. MODULE_LICENSE("GPL v2");