pinctrl-intel.c 39 KB

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