pinctrl-intel.c 38 KB

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