pinctrl-intel.c 38 KB

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