pinctrl-intel.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930
  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/cleanup.h>
  11. #include <linux/gpio/driver.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/log2.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/property.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/string_helpers.h>
  19. #include <linux/time.h>
  20. #include <linux/pinctrl/consumer.h>
  21. #include <linux/pinctrl/pinconf.h>
  22. #include <linux/pinctrl/pinconf-generic.h>
  23. #include <linux/pinctrl/pinctrl.h>
  24. #include <linux/pinctrl/pinmux.h>
  25. #include <linux/platform_data/x86/pwm-lpss.h>
  26. #include "../core.h"
  27. #include "pinctrl-intel.h"
  28. /* Offset from regs */
  29. #define REVID 0x000
  30. #define REVID_SHIFT 16
  31. #define REVID_MASK GENMASK(31, 16)
  32. #define CAPLIST 0x004
  33. #define CAPLIST_ID_SHIFT 16
  34. #define CAPLIST_ID_MASK GENMASK(23, 16)
  35. #define CAPLIST_ID_GPIO_HW_INFO 1
  36. #define CAPLIST_ID_PWM 2
  37. #define CAPLIST_ID_BLINK 3
  38. #define CAPLIST_ID_EXP 4
  39. #define CAPLIST_NEXT_SHIFT 0
  40. #define CAPLIST_NEXT_MASK GENMASK(15, 0)
  41. #define PADBAR 0x00c
  42. #define PADOWN_BITS 4
  43. #define PADOWN_SHIFT(p) ((p) % 8 * PADOWN_BITS)
  44. #define PADOWN_MASK(p) (GENMASK(3, 0) << PADOWN_SHIFT(p))
  45. #define PADOWN_GPP(p) ((p) / 8)
  46. #define PWMC 0x204
  47. /* Offset from pad_regs */
  48. #define PADCFG0 0x000
  49. #define PADCFG0_RXEVCFG_MASK GENMASK(26, 25)
  50. #define PADCFG0_RXEVCFG_LEVEL (0 << 25)
  51. #define PADCFG0_RXEVCFG_EDGE (1 << 25)
  52. #define PADCFG0_RXEVCFG_DISABLED (2 << 25)
  53. #define PADCFG0_RXEVCFG_EDGE_BOTH (3 << 25)
  54. #define PADCFG0_PREGFRXSEL BIT(24)
  55. #define PADCFG0_RXINV BIT(23)
  56. #define PADCFG0_GPIROUTIOXAPIC BIT(20)
  57. #define PADCFG0_GPIROUTSCI BIT(19)
  58. #define PADCFG0_GPIROUTSMI BIT(18)
  59. #define PADCFG0_GPIROUTNMI BIT(17)
  60. #define PADCFG0_PMODE_SHIFT 10
  61. #define PADCFG0_PMODE_MASK GENMASK(13, 10)
  62. #define PADCFG0_PMODE_GPIO 0
  63. #define PADCFG0_GPIODIS_SHIFT 8
  64. #define PADCFG0_GPIODIS_MASK GENMASK(9, 8)
  65. #define PADCFG0_GPIODIS_NONE 0
  66. #define PADCFG0_GPIODIS_OUTPUT 1
  67. #define PADCFG0_GPIODIS_INPUT 2
  68. #define PADCFG0_GPIODIS_FULL 3
  69. #define PADCFG0_GPIORXDIS BIT(9)
  70. #define PADCFG0_GPIOTXDIS BIT(8)
  71. #define PADCFG0_GPIORXSTATE BIT(1)
  72. #define PADCFG0_GPIOTXSTATE BIT(0)
  73. #define PADCFG1 0x004
  74. #define PADCFG1_TERM_UP BIT(13)
  75. #define PADCFG1_TERM_SHIFT 10
  76. #define PADCFG1_TERM_MASK GENMASK(12, 10)
  77. #define PADCFG1_TERM_20K BIT(2)
  78. #define PADCFG1_TERM_5K BIT(1)
  79. #define PADCFG1_TERM_4K (BIT(2) | BIT(1))
  80. #define PADCFG1_TERM_1K BIT(0)
  81. #define PADCFG1_TERM_952 (BIT(2) | BIT(0))
  82. #define PADCFG1_TERM_833 (BIT(1) | BIT(0))
  83. #define PADCFG1_TERM_800 (BIT(2) | BIT(1) | BIT(0))
  84. #define PADCFG2 0x008
  85. #define PADCFG2_DEBOUNCE_SHIFT 1
  86. #define PADCFG2_DEBOUNCE_MASK GENMASK(4, 1)
  87. #define PADCFG2_DEBEN BIT(0)
  88. #define DEBOUNCE_PERIOD_NSEC 31250
  89. struct intel_pad_context {
  90. u32 padcfg0;
  91. u32 padcfg1;
  92. u32 padcfg2;
  93. };
  94. struct intel_community_context {
  95. u32 *intmask;
  96. u32 *hostown;
  97. };
  98. #define pin_to_padno(c, p) ((p) - (c)->pin_base)
  99. #define padgroup_offset(g, p) ((p) - (g)->base)
  100. #define for_each_intel_pin_community(pctrl, community) \
  101. for (unsigned int __ci = 0; \
  102. __ci < pctrl->ncommunities && (community = &pctrl->communities[__ci]); \
  103. __ci++) \
  104. #define for_each_intel_community_pad_group(community, grp) \
  105. for (unsigned int __gi = 0; \
  106. __gi < community->ngpps && (grp = &community->gpps[__gi]); \
  107. __gi++) \
  108. #define for_each_intel_pad_group(pctrl, community, grp) \
  109. for_each_intel_pin_community(pctrl, community) \
  110. for_each_intel_community_pad_group(community, grp)
  111. #define for_each_intel_gpio_group(pctrl, community, grp) \
  112. for_each_intel_pad_group(pctrl, community, grp) \
  113. if (grp->gpio_base == INTEL_GPIO_BASE_NOMAP) {} else
  114. const struct intel_community *intel_get_community(const struct intel_pinctrl *pctrl,
  115. unsigned int pin)
  116. {
  117. const struct intel_community *community;
  118. for_each_intel_pin_community(pctrl, community) {
  119. if (pin >= community->pin_base &&
  120. pin < community->pin_base + community->npins)
  121. return community;
  122. }
  123. dev_warn(pctrl->dev, "failed to find community for pin %u\n", pin);
  124. return NULL;
  125. }
  126. EXPORT_SYMBOL_NS_GPL(intel_get_community, PINCTRL_INTEL);
  127. static const struct intel_padgroup *
  128. intel_community_get_padgroup(const struct intel_community *community,
  129. unsigned int pin)
  130. {
  131. const struct intel_padgroup *padgrp;
  132. for_each_intel_community_pad_group(community, padgrp) {
  133. if (pin >= padgrp->base && pin < padgrp->base + padgrp->size)
  134. return padgrp;
  135. }
  136. return NULL;
  137. }
  138. static void __iomem *intel_get_padcfg(struct intel_pinctrl *pctrl,
  139. unsigned int pin, unsigned int reg)
  140. {
  141. const struct intel_community *community;
  142. unsigned int padno;
  143. size_t nregs;
  144. community = intel_get_community(pctrl, pin);
  145. if (!community)
  146. return NULL;
  147. padno = pin_to_padno(community, pin);
  148. nregs = (community->features & PINCTRL_FEATURE_DEBOUNCE) ? 4 : 2;
  149. if (reg >= nregs * 4)
  150. return NULL;
  151. return community->pad_regs + reg + padno * nregs * 4;
  152. }
  153. static bool intel_pad_owned_by_host(const struct intel_pinctrl *pctrl, unsigned int pin)
  154. {
  155. const struct intel_community *community;
  156. const struct intel_padgroup *padgrp;
  157. unsigned int gpp, offset, gpp_offset;
  158. void __iomem *padown;
  159. community = intel_get_community(pctrl, pin);
  160. if (!community)
  161. return false;
  162. if (!community->padown_offset)
  163. return true;
  164. padgrp = intel_community_get_padgroup(community, pin);
  165. if (!padgrp)
  166. return false;
  167. gpp_offset = padgroup_offset(padgrp, pin);
  168. gpp = PADOWN_GPP(gpp_offset);
  169. offset = community->padown_offset + padgrp->padown_num * 4 + gpp * 4;
  170. padown = community->regs + offset;
  171. return !(readl(padown) & PADOWN_MASK(gpp_offset));
  172. }
  173. static bool intel_pad_acpi_mode(const struct intel_pinctrl *pctrl, unsigned int pin)
  174. {
  175. const struct intel_community *community;
  176. const struct intel_padgroup *padgrp;
  177. unsigned int offset, gpp_offset;
  178. void __iomem *hostown;
  179. community = intel_get_community(pctrl, pin);
  180. if (!community)
  181. return true;
  182. if (!community->hostown_offset)
  183. return false;
  184. padgrp = intel_community_get_padgroup(community, pin);
  185. if (!padgrp)
  186. return true;
  187. gpp_offset = padgroup_offset(padgrp, pin);
  188. offset = community->hostown_offset + padgrp->reg_num * 4;
  189. hostown = community->regs + offset;
  190. return !(readl(hostown) & BIT(gpp_offset));
  191. }
  192. /**
  193. * enum - Locking variants of the pad configuration
  194. * @PAD_UNLOCKED: pad is fully controlled by the configuration registers
  195. * @PAD_LOCKED: pad configuration registers, except TX state, are locked
  196. * @PAD_LOCKED_TX: pad configuration TX state is locked
  197. * @PAD_LOCKED_FULL: pad configuration registers are locked completely
  198. *
  199. * Locking is considered as read-only mode for corresponding registers and
  200. * their respective fields. That said, TX state bit is locked separately from
  201. * the main locking scheme.
  202. */
  203. enum {
  204. PAD_UNLOCKED = 0,
  205. PAD_LOCKED = 1,
  206. PAD_LOCKED_TX = 2,
  207. PAD_LOCKED_FULL = PAD_LOCKED | PAD_LOCKED_TX,
  208. };
  209. static int intel_pad_locked(const struct intel_pinctrl *pctrl, unsigned int pin)
  210. {
  211. const struct intel_community *community;
  212. const struct intel_padgroup *padgrp;
  213. unsigned int offset, gpp_offset;
  214. u32 value;
  215. int ret = PAD_UNLOCKED;
  216. community = intel_get_community(pctrl, pin);
  217. if (!community)
  218. return PAD_LOCKED_FULL;
  219. if (!community->padcfglock_offset)
  220. return PAD_UNLOCKED;
  221. padgrp = intel_community_get_padgroup(community, pin);
  222. if (!padgrp)
  223. return PAD_LOCKED_FULL;
  224. gpp_offset = padgroup_offset(padgrp, pin);
  225. /*
  226. * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad,
  227. * the pad is considered unlocked. Any other case means that it is
  228. * either fully or partially locked.
  229. */
  230. offset = community->padcfglock_offset + 0 + padgrp->reg_num * 8;
  231. value = readl(community->regs + offset);
  232. if (value & BIT(gpp_offset))
  233. ret |= PAD_LOCKED;
  234. offset = community->padcfglock_offset + 4 + padgrp->reg_num * 8;
  235. value = readl(community->regs + offset);
  236. if (value & BIT(gpp_offset))
  237. ret |= PAD_LOCKED_TX;
  238. return ret;
  239. }
  240. static bool intel_pad_is_unlocked(const struct intel_pinctrl *pctrl, unsigned int pin)
  241. {
  242. return (intel_pad_locked(pctrl, pin) & PAD_LOCKED) == PAD_UNLOCKED;
  243. }
  244. static bool intel_pad_usable(const struct intel_pinctrl *pctrl, unsigned int pin)
  245. {
  246. return intel_pad_owned_by_host(pctrl, pin) && intel_pad_is_unlocked(pctrl, pin);
  247. }
  248. int intel_get_groups_count(struct pinctrl_dev *pctldev)
  249. {
  250. const struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  251. return pctrl->soc->ngroups;
  252. }
  253. EXPORT_SYMBOL_NS_GPL(intel_get_groups_count, PINCTRL_INTEL);
  254. const char *intel_get_group_name(struct pinctrl_dev *pctldev, unsigned int group)
  255. {
  256. const struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  257. return pctrl->soc->groups[group].grp.name;
  258. }
  259. EXPORT_SYMBOL_NS_GPL(intel_get_group_name, PINCTRL_INTEL);
  260. int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned int group,
  261. const unsigned int **pins, unsigned int *npins)
  262. {
  263. const struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  264. *pins = pctrl->soc->groups[group].grp.pins;
  265. *npins = pctrl->soc->groups[group].grp.npins;
  266. return 0;
  267. }
  268. EXPORT_SYMBOL_NS_GPL(intel_get_group_pins, PINCTRL_INTEL);
  269. static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
  270. unsigned int pin)
  271. {
  272. struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  273. void __iomem *padcfg;
  274. u32 cfg0, cfg1, mode;
  275. int locked;
  276. bool acpi;
  277. if (!intel_pad_owned_by_host(pctrl, pin)) {
  278. seq_puts(s, "not available");
  279. return;
  280. }
  281. cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
  282. cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
  283. mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
  284. if (mode == PADCFG0_PMODE_GPIO)
  285. seq_puts(s, "GPIO ");
  286. else
  287. seq_printf(s, "mode %d ", mode);
  288. seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1);
  289. /* Dump the additional PADCFG registers if available */
  290. padcfg = intel_get_padcfg(pctrl, pin, PADCFG2);
  291. if (padcfg)
  292. seq_printf(s, " 0x%08x", readl(padcfg));
  293. locked = intel_pad_locked(pctrl, pin);
  294. acpi = intel_pad_acpi_mode(pctrl, pin);
  295. if (locked || acpi) {
  296. seq_puts(s, " [");
  297. if (locked)
  298. seq_puts(s, "LOCKED");
  299. if ((locked & PAD_LOCKED_FULL) == PAD_LOCKED_TX)
  300. seq_puts(s, " tx");
  301. else if ((locked & PAD_LOCKED_FULL) == PAD_LOCKED_FULL)
  302. seq_puts(s, " full");
  303. if (locked && acpi)
  304. seq_puts(s, ", ");
  305. if (acpi)
  306. seq_puts(s, "ACPI");
  307. seq_puts(s, "]");
  308. }
  309. }
  310. static const struct pinctrl_ops intel_pinctrl_ops = {
  311. .get_groups_count = intel_get_groups_count,
  312. .get_group_name = intel_get_group_name,
  313. .get_group_pins = intel_get_group_pins,
  314. .pin_dbg_show = intel_pin_dbg_show,
  315. };
  316. int intel_get_functions_count(struct pinctrl_dev *pctldev)
  317. {
  318. const struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  319. return pctrl->soc->nfunctions;
  320. }
  321. EXPORT_SYMBOL_NS_GPL(intel_get_functions_count, PINCTRL_INTEL);
  322. const char *intel_get_function_name(struct pinctrl_dev *pctldev, unsigned int function)
  323. {
  324. const struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  325. return pctrl->soc->functions[function].func.name;
  326. }
  327. EXPORT_SYMBOL_NS_GPL(intel_get_function_name, PINCTRL_INTEL);
  328. int intel_get_function_groups(struct pinctrl_dev *pctldev, unsigned int function,
  329. const char * const **groups, unsigned int * const ngroups)
  330. {
  331. const struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  332. *groups = pctrl->soc->functions[function].func.groups;
  333. *ngroups = pctrl->soc->functions[function].func.ngroups;
  334. return 0;
  335. }
  336. EXPORT_SYMBOL_NS_GPL(intel_get_function_groups, PINCTRL_INTEL);
  337. static int intel_pinmux_set_mux(struct pinctrl_dev *pctldev,
  338. unsigned int function, unsigned int group)
  339. {
  340. struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  341. const struct intel_pingroup *grp = &pctrl->soc->groups[group];
  342. int i;
  343. guard(raw_spinlock_irqsave)(&pctrl->lock);
  344. /*
  345. * All pins in the groups needs to be accessible and writable
  346. * before we can enable the mux for this group.
  347. */
  348. for (i = 0; i < grp->grp.npins; i++) {
  349. if (!intel_pad_usable(pctrl, grp->grp.pins[i]))
  350. return -EBUSY;
  351. }
  352. /* Now enable the mux setting for each pin in the group */
  353. for (i = 0; i < grp->grp.npins; i++) {
  354. void __iomem *padcfg0;
  355. u32 value, pmode;
  356. padcfg0 = intel_get_padcfg(pctrl, grp->grp.pins[i], PADCFG0);
  357. value = readl(padcfg0);
  358. value &= ~PADCFG0_PMODE_MASK;
  359. if (grp->modes)
  360. pmode = grp->modes[i];
  361. else
  362. pmode = grp->mode;
  363. value |= pmode << PADCFG0_PMODE_SHIFT;
  364. writel(value, padcfg0);
  365. }
  366. return 0;
  367. }
  368. /**
  369. * enum - Possible pad physical connections
  370. * @PAD_CONNECT_NONE: pad is fully disconnected
  371. * @PAD_CONNECT_INPUT: pad is in input only mode
  372. * @PAD_CONNECT_OUTPUT: pad is in output only mode
  373. * @PAD_CONNECT_FULL: pad is fully connected
  374. */
  375. enum {
  376. PAD_CONNECT_NONE = 0,
  377. PAD_CONNECT_INPUT = 1,
  378. PAD_CONNECT_OUTPUT = 2,
  379. PAD_CONNECT_FULL = PAD_CONNECT_INPUT | PAD_CONNECT_OUTPUT,
  380. };
  381. static int __intel_gpio_get_direction(u32 value)
  382. {
  383. switch ((value & PADCFG0_GPIODIS_MASK) >> PADCFG0_GPIODIS_SHIFT) {
  384. case PADCFG0_GPIODIS_FULL:
  385. return PAD_CONNECT_NONE;
  386. case PADCFG0_GPIODIS_OUTPUT:
  387. return PAD_CONNECT_INPUT;
  388. case PADCFG0_GPIODIS_INPUT:
  389. return PAD_CONNECT_OUTPUT;
  390. case PADCFG0_GPIODIS_NONE:
  391. return PAD_CONNECT_FULL;
  392. default:
  393. return -ENOTSUPP;
  394. };
  395. }
  396. static u32 __intel_gpio_set_direction(u32 value, bool input, bool output)
  397. {
  398. if (input)
  399. value &= ~PADCFG0_GPIORXDIS;
  400. else
  401. value |= PADCFG0_GPIORXDIS;
  402. if (output)
  403. value &= ~PADCFG0_GPIOTXDIS;
  404. else
  405. value |= PADCFG0_GPIOTXDIS;
  406. return value;
  407. }
  408. static int __intel_gpio_get_gpio_mode(u32 value)
  409. {
  410. return (value & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
  411. }
  412. static int intel_gpio_get_gpio_mode(void __iomem *padcfg0)
  413. {
  414. return __intel_gpio_get_gpio_mode(readl(padcfg0));
  415. }
  416. static void intel_gpio_set_gpio_mode(void __iomem *padcfg0)
  417. {
  418. u32 value;
  419. value = readl(padcfg0);
  420. /* Put the pad into GPIO mode */
  421. value &= ~PADCFG0_PMODE_MASK;
  422. value |= PADCFG0_PMODE_GPIO;
  423. /* Disable TX buffer and enable RX (this will be input) */
  424. value = __intel_gpio_set_direction(value, true, false);
  425. /* Disable SCI/SMI/NMI generation */
  426. value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI);
  427. value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI);
  428. writel(value, padcfg0);
  429. }
  430. static int intel_gpio_request_enable(struct pinctrl_dev *pctldev,
  431. struct pinctrl_gpio_range *range,
  432. unsigned int pin)
  433. {
  434. struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  435. void __iomem *padcfg0;
  436. padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
  437. guard(raw_spinlock_irqsave)(&pctrl->lock);
  438. if (!intel_pad_owned_by_host(pctrl, pin))
  439. return -EBUSY;
  440. if (!intel_pad_is_unlocked(pctrl, pin))
  441. return 0;
  442. /*
  443. * If pin is already configured in GPIO mode, we assume that
  444. * firmware provides correct settings. In such case we avoid
  445. * potential glitches on the pin. Otherwise, for the pin in
  446. * alternative mode, consumer has to supply respective flags.
  447. */
  448. if (intel_gpio_get_gpio_mode(padcfg0) == PADCFG0_PMODE_GPIO)
  449. return 0;
  450. intel_gpio_set_gpio_mode(padcfg0);
  451. return 0;
  452. }
  453. static int intel_gpio_set_direction(struct pinctrl_dev *pctldev,
  454. struct pinctrl_gpio_range *range,
  455. unsigned int pin, bool input)
  456. {
  457. struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  458. void __iomem *padcfg0;
  459. u32 value;
  460. padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
  461. guard(raw_spinlock_irqsave)(&pctrl->lock);
  462. value = readl(padcfg0);
  463. if (input)
  464. value = __intel_gpio_set_direction(value, true, false);
  465. else
  466. value = __intel_gpio_set_direction(value, false, true);
  467. writel(value, padcfg0);
  468. return 0;
  469. }
  470. static const struct pinmux_ops intel_pinmux_ops = {
  471. .get_functions_count = intel_get_functions_count,
  472. .get_function_name = intel_get_function_name,
  473. .get_function_groups = intel_get_function_groups,
  474. .set_mux = intel_pinmux_set_mux,
  475. .gpio_request_enable = intel_gpio_request_enable,
  476. .gpio_set_direction = intel_gpio_set_direction,
  477. };
  478. static int intel_config_get_pull(struct intel_pinctrl *pctrl, unsigned int pin,
  479. enum pin_config_param param, u32 *arg)
  480. {
  481. void __iomem *padcfg1;
  482. u32 value, term;
  483. padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
  484. scoped_guard(raw_spinlock_irqsave, &pctrl->lock)
  485. value = readl(padcfg1);
  486. term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT;
  487. switch (param) {
  488. case PIN_CONFIG_BIAS_DISABLE:
  489. if (term)
  490. return -EINVAL;
  491. break;
  492. case PIN_CONFIG_BIAS_PULL_UP:
  493. if (!term || !(value & PADCFG1_TERM_UP))
  494. return -EINVAL;
  495. switch (term) {
  496. case PADCFG1_TERM_833:
  497. *arg = 833;
  498. break;
  499. case PADCFG1_TERM_1K:
  500. *arg = 1000;
  501. break;
  502. case PADCFG1_TERM_4K:
  503. *arg = 4000;
  504. break;
  505. case PADCFG1_TERM_5K:
  506. *arg = 5000;
  507. break;
  508. case PADCFG1_TERM_20K:
  509. *arg = 20000;
  510. break;
  511. }
  512. break;
  513. case PIN_CONFIG_BIAS_PULL_DOWN: {
  514. const struct intel_community *community = intel_get_community(pctrl, pin);
  515. if (!term || value & PADCFG1_TERM_UP)
  516. return -EINVAL;
  517. switch (term) {
  518. case PADCFG1_TERM_833:
  519. if (!(community->features & PINCTRL_FEATURE_1K_PD))
  520. return -EINVAL;
  521. *arg = 833;
  522. break;
  523. case PADCFG1_TERM_1K:
  524. if (!(community->features & PINCTRL_FEATURE_1K_PD))
  525. return -EINVAL;
  526. *arg = 1000;
  527. break;
  528. case PADCFG1_TERM_4K:
  529. *arg = 4000;
  530. break;
  531. case PADCFG1_TERM_5K:
  532. *arg = 5000;
  533. break;
  534. case PADCFG1_TERM_20K:
  535. *arg = 20000;
  536. break;
  537. }
  538. break;
  539. }
  540. default:
  541. return -EINVAL;
  542. }
  543. return 0;
  544. }
  545. static int intel_config_get_high_impedance(struct intel_pinctrl *pctrl, unsigned int pin,
  546. enum pin_config_param param, u32 *arg)
  547. {
  548. void __iomem *padcfg0;
  549. u32 value;
  550. padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
  551. scoped_guard(raw_spinlock_irqsave, &pctrl->lock)
  552. value = readl(padcfg0);
  553. if (__intel_gpio_get_direction(value) != PAD_CONNECT_NONE)
  554. return -EINVAL;
  555. return 0;
  556. }
  557. static int intel_config_get_debounce(struct intel_pinctrl *pctrl, unsigned int pin,
  558. enum pin_config_param param, u32 *arg)
  559. {
  560. void __iomem *padcfg2;
  561. unsigned long v;
  562. u32 value2;
  563. padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
  564. if (!padcfg2)
  565. return -ENOTSUPP;
  566. scoped_guard(raw_spinlock_irqsave, &pctrl->lock)
  567. value2 = readl(padcfg2);
  568. if (!(value2 & PADCFG2_DEBEN))
  569. return -EINVAL;
  570. v = (value2 & PADCFG2_DEBOUNCE_MASK) >> PADCFG2_DEBOUNCE_SHIFT;
  571. *arg = BIT(v) * DEBOUNCE_PERIOD_NSEC / NSEC_PER_USEC;
  572. return 0;
  573. }
  574. static int intel_config_get(struct pinctrl_dev *pctldev, unsigned int pin,
  575. unsigned long *config)
  576. {
  577. struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  578. enum pin_config_param param = pinconf_to_config_param(*config);
  579. u32 arg = 0;
  580. int ret;
  581. if (!intel_pad_owned_by_host(pctrl, pin))
  582. return -ENOTSUPP;
  583. switch (param) {
  584. case PIN_CONFIG_BIAS_DISABLE:
  585. case PIN_CONFIG_BIAS_PULL_UP:
  586. case PIN_CONFIG_BIAS_PULL_DOWN:
  587. ret = intel_config_get_pull(pctrl, pin, param, &arg);
  588. if (ret)
  589. return ret;
  590. break;
  591. case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
  592. ret = intel_config_get_high_impedance(pctrl, pin, param, &arg);
  593. if (ret)
  594. return ret;
  595. break;
  596. case PIN_CONFIG_INPUT_DEBOUNCE:
  597. ret = intel_config_get_debounce(pctrl, pin, param, &arg);
  598. if (ret)
  599. return ret;
  600. break;
  601. default:
  602. return -ENOTSUPP;
  603. }
  604. *config = pinconf_to_config_packed(param, arg);
  605. return 0;
  606. }
  607. static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned int pin,
  608. unsigned long config)
  609. {
  610. unsigned int param = pinconf_to_config_param(config);
  611. unsigned int arg = pinconf_to_config_argument(config);
  612. u32 term = 0, up = 0, value;
  613. void __iomem *padcfg1;
  614. switch (param) {
  615. case PIN_CONFIG_BIAS_DISABLE:
  616. break;
  617. case PIN_CONFIG_BIAS_PULL_UP:
  618. switch (arg) {
  619. case 20000:
  620. term = PADCFG1_TERM_20K;
  621. break;
  622. case 1: /* Set default strength value in case none is given */
  623. case 5000:
  624. term = PADCFG1_TERM_5K;
  625. break;
  626. case 4000:
  627. term = PADCFG1_TERM_4K;
  628. break;
  629. case 1000:
  630. term = PADCFG1_TERM_1K;
  631. break;
  632. case 833:
  633. term = PADCFG1_TERM_833;
  634. break;
  635. default:
  636. return -EINVAL;
  637. }
  638. up = PADCFG1_TERM_UP;
  639. break;
  640. case PIN_CONFIG_BIAS_PULL_DOWN: {
  641. const struct intel_community *community = intel_get_community(pctrl, pin);
  642. switch (arg) {
  643. case 20000:
  644. term = PADCFG1_TERM_20K;
  645. break;
  646. case 1: /* Set default strength value in case none is given */
  647. case 5000:
  648. term = PADCFG1_TERM_5K;
  649. break;
  650. case 4000:
  651. term = PADCFG1_TERM_4K;
  652. break;
  653. case 1000:
  654. if (!(community->features & PINCTRL_FEATURE_1K_PD))
  655. return -EINVAL;
  656. term = PADCFG1_TERM_1K;
  657. break;
  658. case 833:
  659. if (!(community->features & PINCTRL_FEATURE_1K_PD))
  660. return -EINVAL;
  661. term = PADCFG1_TERM_833;
  662. break;
  663. default:
  664. return -EINVAL;
  665. }
  666. break;
  667. }
  668. default:
  669. return -EINVAL;
  670. }
  671. padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
  672. guard(raw_spinlock_irqsave)(&pctrl->lock);
  673. value = readl(padcfg1);
  674. value = (value & ~PADCFG1_TERM_MASK) | (term << PADCFG1_TERM_SHIFT);
  675. value = (value & ~PADCFG1_TERM_UP) | up;
  676. writel(value, padcfg1);
  677. return 0;
  678. }
  679. static void intel_gpio_set_high_impedance(struct intel_pinctrl *pctrl, unsigned int pin)
  680. {
  681. void __iomem *padcfg0;
  682. u32 value;
  683. padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
  684. guard(raw_spinlock_irqsave)(&pctrl->lock);
  685. value = readl(padcfg0);
  686. value = __intel_gpio_set_direction(value, false, false);
  687. writel(value, padcfg0);
  688. }
  689. static int intel_config_set_debounce(struct intel_pinctrl *pctrl,
  690. unsigned int pin, unsigned int debounce)
  691. {
  692. void __iomem *padcfg0, *padcfg2;
  693. u32 value0, value2;
  694. unsigned long v;
  695. if (debounce) {
  696. v = order_base_2(debounce * NSEC_PER_USEC / DEBOUNCE_PERIOD_NSEC);
  697. if (v < 3 || v > 15)
  698. return -EINVAL;
  699. } else {
  700. v = 0;
  701. }
  702. padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
  703. if (!padcfg2)
  704. return -ENOTSUPP;
  705. padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
  706. guard(raw_spinlock_irqsave)(&pctrl->lock);
  707. value0 = readl(padcfg0);
  708. value2 = readl(padcfg2);
  709. value2 = (value2 & ~PADCFG2_DEBOUNCE_MASK) | (v << PADCFG2_DEBOUNCE_SHIFT);
  710. if (v) {
  711. /* Enable glitch filter and debouncer */
  712. value0 |= PADCFG0_PREGFRXSEL;
  713. value2 |= PADCFG2_DEBEN;
  714. } else {
  715. /* Disable glitch filter and debouncer */
  716. value0 &= ~PADCFG0_PREGFRXSEL;
  717. value2 &= ~PADCFG2_DEBEN;
  718. }
  719. writel(value0, padcfg0);
  720. writel(value2, padcfg2);
  721. return 0;
  722. }
  723. static int intel_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
  724. unsigned long *configs, unsigned int nconfigs)
  725. {
  726. struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  727. int i, ret;
  728. if (!intel_pad_usable(pctrl, pin))
  729. return -ENOTSUPP;
  730. for (i = 0; i < nconfigs; i++) {
  731. switch (pinconf_to_config_param(configs[i])) {
  732. case PIN_CONFIG_BIAS_DISABLE:
  733. case PIN_CONFIG_BIAS_PULL_UP:
  734. case PIN_CONFIG_BIAS_PULL_DOWN:
  735. ret = intel_config_set_pull(pctrl, pin, configs[i]);
  736. if (ret)
  737. return ret;
  738. break;
  739. case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
  740. intel_gpio_set_high_impedance(pctrl, pin);
  741. break;
  742. case PIN_CONFIG_INPUT_DEBOUNCE:
  743. ret = intel_config_set_debounce(pctrl, pin,
  744. pinconf_to_config_argument(configs[i]));
  745. if (ret)
  746. return ret;
  747. break;
  748. default:
  749. return -ENOTSUPP;
  750. }
  751. }
  752. return 0;
  753. }
  754. static const struct pinconf_ops intel_pinconf_ops = {
  755. .is_generic = true,
  756. .pin_config_get = intel_config_get,
  757. .pin_config_set = intel_config_set,
  758. };
  759. static const struct pinctrl_desc intel_pinctrl_desc = {
  760. .pctlops = &intel_pinctrl_ops,
  761. .pmxops = &intel_pinmux_ops,
  762. .confops = &intel_pinconf_ops,
  763. .owner = THIS_MODULE,
  764. };
  765. /**
  766. * intel_gpio_to_pin() - Translate from GPIO offset to pin number
  767. * @pctrl: Pinctrl structure
  768. * @offset: GPIO offset from gpiolib
  769. * @community: Community is filled here if not %NULL
  770. * @padgrp: Pad group is filled here if not %NULL
  771. *
  772. * When coming through gpiolib irqchip, the GPIO offset is not
  773. * automatically translated to pinctrl pin number. This function can be
  774. * used to find out the corresponding pinctrl pin.
  775. *
  776. * Return: a pin number and pointers to the community and pad group, which
  777. * the pin belongs to, or negative error code if translation can't be done.
  778. */
  779. static int intel_gpio_to_pin(const struct intel_pinctrl *pctrl, unsigned int offset,
  780. const struct intel_community **community,
  781. const struct intel_padgroup **padgrp)
  782. {
  783. const struct intel_community *comm;
  784. const struct intel_padgroup *grp;
  785. for_each_intel_gpio_group(pctrl, comm, grp) {
  786. if (offset >= grp->gpio_base && offset < grp->gpio_base + grp->size) {
  787. if (community)
  788. *community = comm;
  789. if (padgrp)
  790. *padgrp = grp;
  791. return grp->base + offset - grp->gpio_base;
  792. }
  793. }
  794. return -EINVAL;
  795. }
  796. /**
  797. * intel_pin_to_gpio() - Translate from pin number to GPIO offset
  798. * @pctrl: Pinctrl structure
  799. * @pin: pin number
  800. *
  801. * Translate the pin number of pinctrl to GPIO offset
  802. *
  803. * Return: a GPIO offset, or negative error code if translation can't be done.
  804. */
  805. static int intel_pin_to_gpio(const struct intel_pinctrl *pctrl, int pin)
  806. {
  807. const struct intel_community *community;
  808. const struct intel_padgroup *padgrp;
  809. community = intel_get_community(pctrl, pin);
  810. if (!community)
  811. return -EINVAL;
  812. padgrp = intel_community_get_padgroup(community, pin);
  813. if (!padgrp)
  814. return -EINVAL;
  815. return pin - padgrp->base + padgrp->gpio_base;
  816. }
  817. static int intel_gpio_get(struct gpio_chip *chip, unsigned int offset)
  818. {
  819. struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
  820. void __iomem *reg;
  821. u32 padcfg0;
  822. int pin;
  823. pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
  824. if (pin < 0)
  825. return -EINVAL;
  826. reg = intel_get_padcfg(pctrl, pin, PADCFG0);
  827. if (!reg)
  828. return -EINVAL;
  829. padcfg0 = readl(reg);
  830. if (__intel_gpio_get_direction(padcfg0) & PAD_CONNECT_OUTPUT)
  831. return !!(padcfg0 & PADCFG0_GPIOTXSTATE);
  832. return !!(padcfg0 & PADCFG0_GPIORXSTATE);
  833. }
  834. static void intel_gpio_set(struct gpio_chip *chip, unsigned int offset,
  835. int value)
  836. {
  837. struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
  838. void __iomem *reg;
  839. u32 padcfg0;
  840. int pin;
  841. pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
  842. if (pin < 0)
  843. return;
  844. reg = intel_get_padcfg(pctrl, pin, PADCFG0);
  845. if (!reg)
  846. return;
  847. guard(raw_spinlock_irqsave)(&pctrl->lock);
  848. padcfg0 = readl(reg);
  849. if (value)
  850. padcfg0 |= PADCFG0_GPIOTXSTATE;
  851. else
  852. padcfg0 &= ~PADCFG0_GPIOTXSTATE;
  853. writel(padcfg0, reg);
  854. }
  855. static int intel_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
  856. {
  857. struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
  858. void __iomem *reg;
  859. u32 padcfg0;
  860. int pin;
  861. pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
  862. if (pin < 0)
  863. return -EINVAL;
  864. reg = intel_get_padcfg(pctrl, pin, PADCFG0);
  865. if (!reg)
  866. return -EINVAL;
  867. scoped_guard(raw_spinlock_irqsave, &pctrl->lock)
  868. padcfg0 = readl(reg);
  869. if (padcfg0 & PADCFG0_PMODE_MASK)
  870. return -EINVAL;
  871. if (__intel_gpio_get_direction(padcfg0) & PAD_CONNECT_OUTPUT)
  872. return GPIO_LINE_DIRECTION_OUT;
  873. return GPIO_LINE_DIRECTION_IN;
  874. }
  875. static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
  876. {
  877. return pinctrl_gpio_direction_input(chip, offset);
  878. }
  879. static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned int offset,
  880. int value)
  881. {
  882. intel_gpio_set(chip, offset, value);
  883. return pinctrl_gpio_direction_output(chip, offset);
  884. }
  885. static const struct gpio_chip intel_gpio_chip = {
  886. .owner = THIS_MODULE,
  887. .request = gpiochip_generic_request,
  888. .free = gpiochip_generic_free,
  889. .get_direction = intel_gpio_get_direction,
  890. .direction_input = intel_gpio_direction_input,
  891. .direction_output = intel_gpio_direction_output,
  892. .get = intel_gpio_get,
  893. .set = intel_gpio_set,
  894. .set_config = gpiochip_generic_config,
  895. };
  896. static void intel_gpio_irq_ack(struct irq_data *d)
  897. {
  898. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  899. struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
  900. const struct intel_community *community;
  901. const struct intel_padgroup *padgrp;
  902. int pin;
  903. pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
  904. if (pin >= 0) {
  905. unsigned int gpp, gpp_offset;
  906. void __iomem *is;
  907. gpp = padgrp->reg_num;
  908. gpp_offset = padgroup_offset(padgrp, pin);
  909. is = community->regs + community->is_offset + gpp * 4;
  910. guard(raw_spinlock)(&pctrl->lock);
  911. writel(BIT(gpp_offset), is);
  912. }
  913. }
  914. static void intel_gpio_irq_mask_unmask(struct gpio_chip *gc, irq_hw_number_t hwirq, bool mask)
  915. {
  916. struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
  917. const struct intel_community *community;
  918. const struct intel_padgroup *padgrp;
  919. int pin;
  920. pin = intel_gpio_to_pin(pctrl, hwirq, &community, &padgrp);
  921. if (pin >= 0) {
  922. unsigned int gpp, gpp_offset;
  923. void __iomem *reg, *is;
  924. u32 value;
  925. gpp = padgrp->reg_num;
  926. gpp_offset = padgroup_offset(padgrp, pin);
  927. reg = community->regs + community->ie_offset + gpp * 4;
  928. is = community->regs + community->is_offset + gpp * 4;
  929. guard(raw_spinlock_irqsave)(&pctrl->lock);
  930. /* Clear interrupt status first to avoid unexpected interrupt */
  931. writel(BIT(gpp_offset), is);
  932. value = readl(reg);
  933. if (mask)
  934. value &= ~BIT(gpp_offset);
  935. else
  936. value |= BIT(gpp_offset);
  937. writel(value, reg);
  938. }
  939. }
  940. static void intel_gpio_irq_mask(struct irq_data *d)
  941. {
  942. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  943. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  944. intel_gpio_irq_mask_unmask(gc, hwirq, true);
  945. gpiochip_disable_irq(gc, hwirq);
  946. }
  947. static void intel_gpio_irq_unmask(struct irq_data *d)
  948. {
  949. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  950. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  951. gpiochip_enable_irq(gc, hwirq);
  952. intel_gpio_irq_mask_unmask(gc, hwirq, false);
  953. }
  954. static int intel_gpio_irq_type(struct irq_data *d, unsigned int type)
  955. {
  956. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  957. struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
  958. unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
  959. u32 rxevcfg, rxinv, value;
  960. void __iomem *reg;
  961. reg = intel_get_padcfg(pctrl, pin, PADCFG0);
  962. if (!reg)
  963. return -EINVAL;
  964. /*
  965. * If the pin is in ACPI mode it is still usable as a GPIO but it
  966. * cannot be used as IRQ because GPI_IS status bit will not be
  967. * updated by the host controller hardware.
  968. */
  969. if (intel_pad_acpi_mode(pctrl, pin)) {
  970. dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin);
  971. return -EPERM;
  972. }
  973. if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
  974. rxevcfg = PADCFG0_RXEVCFG_EDGE_BOTH;
  975. } else if (type & IRQ_TYPE_EDGE_FALLING) {
  976. rxevcfg = PADCFG0_RXEVCFG_EDGE;
  977. } else if (type & IRQ_TYPE_EDGE_RISING) {
  978. rxevcfg = PADCFG0_RXEVCFG_EDGE;
  979. } else if (type & IRQ_TYPE_LEVEL_MASK) {
  980. rxevcfg = PADCFG0_RXEVCFG_LEVEL;
  981. } else {
  982. rxevcfg = PADCFG0_RXEVCFG_DISABLED;
  983. }
  984. if (type == IRQ_TYPE_EDGE_FALLING || type == IRQ_TYPE_LEVEL_LOW)
  985. rxinv = PADCFG0_RXINV;
  986. else
  987. rxinv = 0;
  988. guard(raw_spinlock_irqsave)(&pctrl->lock);
  989. intel_gpio_set_gpio_mode(reg);
  990. value = readl(reg);
  991. value = (value & ~PADCFG0_RXEVCFG_MASK) | rxevcfg;
  992. value = (value & ~PADCFG0_RXINV) | rxinv;
  993. writel(value, reg);
  994. if (type & IRQ_TYPE_EDGE_BOTH)
  995. irq_set_handler_locked(d, handle_edge_irq);
  996. else if (type & IRQ_TYPE_LEVEL_MASK)
  997. irq_set_handler_locked(d, handle_level_irq);
  998. return 0;
  999. }
  1000. static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on)
  1001. {
  1002. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1003. struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
  1004. unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
  1005. if (on)
  1006. enable_irq_wake(pctrl->irq);
  1007. else
  1008. disable_irq_wake(pctrl->irq);
  1009. dev_dbg(pctrl->dev, "%s wake for pin %u\n", str_enable_disable(on), pin);
  1010. return 0;
  1011. }
  1012. static const struct irq_chip intel_gpio_irq_chip = {
  1013. .name = "intel-gpio",
  1014. .irq_ack = intel_gpio_irq_ack,
  1015. .irq_mask = intel_gpio_irq_mask,
  1016. .irq_unmask = intel_gpio_irq_unmask,
  1017. .irq_set_type = intel_gpio_irq_type,
  1018. .irq_set_wake = intel_gpio_irq_wake,
  1019. .flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_IMMUTABLE,
  1020. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  1021. };
  1022. static irqreturn_t intel_gpio_irq(int irq, void *data)
  1023. {
  1024. const struct intel_community *community;
  1025. const struct intel_padgroup *padgrp;
  1026. struct intel_pinctrl *pctrl = data;
  1027. int ret = 0;
  1028. /* Need to check all communities for pending interrupts */
  1029. for_each_intel_pad_group(pctrl, community, padgrp) {
  1030. struct gpio_chip *gc = &pctrl->chip;
  1031. unsigned long pending, enabled;
  1032. unsigned int gpp, gpp_offset;
  1033. void __iomem *reg, *is;
  1034. gpp = padgrp->reg_num;
  1035. reg = community->regs + community->ie_offset + gpp * 4;
  1036. is = community->regs + community->is_offset + gpp * 4;
  1037. scoped_guard(raw_spinlock, &pctrl->lock) {
  1038. pending = readl(is);
  1039. enabled = readl(reg);
  1040. }
  1041. /* Only interrupts that are enabled */
  1042. pending &= enabled;
  1043. for_each_set_bit(gpp_offset, &pending, padgrp->size)
  1044. generic_handle_domain_irq(gc->irq.domain, padgrp->gpio_base + gpp_offset);
  1045. ret += pending ? 1 : 0;
  1046. }
  1047. return IRQ_RETVAL(ret);
  1048. }
  1049. static void intel_gpio_irq_init(struct intel_pinctrl *pctrl)
  1050. {
  1051. const struct intel_community *community;
  1052. for_each_intel_pin_community(pctrl, community) {
  1053. void __iomem *reg, *is;
  1054. unsigned int gpp;
  1055. for (gpp = 0; gpp < community->ngpps; gpp++) {
  1056. reg = community->regs + community->ie_offset + gpp * 4;
  1057. is = community->regs + community->is_offset + gpp * 4;
  1058. /* Mask and clear all interrupts */
  1059. writel(0, reg);
  1060. writel(0xffff, is);
  1061. }
  1062. }
  1063. }
  1064. static int intel_gpio_irq_init_hw(struct gpio_chip *gc)
  1065. {
  1066. struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
  1067. /*
  1068. * Make sure the interrupt lines are in a proper state before
  1069. * further configuration.
  1070. */
  1071. intel_gpio_irq_init(pctrl);
  1072. return 0;
  1073. }
  1074. static int intel_gpio_add_pin_ranges(struct gpio_chip *gc)
  1075. {
  1076. struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
  1077. const struct intel_community *community;
  1078. const struct intel_padgroup *grp;
  1079. int ret;
  1080. for_each_intel_gpio_group(pctrl, community, grp) {
  1081. ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev),
  1082. grp->gpio_base, grp->base,
  1083. grp->size);
  1084. if (ret) {
  1085. dev_err(pctrl->dev, "failed to add GPIO pin range\n");
  1086. return ret;
  1087. }
  1088. }
  1089. return 0;
  1090. }
  1091. static unsigned int intel_gpio_ngpio(const struct intel_pinctrl *pctrl)
  1092. {
  1093. const struct intel_community *community;
  1094. const struct intel_padgroup *grp;
  1095. unsigned int ngpio = 0;
  1096. for_each_intel_gpio_group(pctrl, community, grp) {
  1097. if (grp->gpio_base + grp->size > ngpio)
  1098. ngpio = grp->gpio_base + grp->size;
  1099. }
  1100. return ngpio;
  1101. }
  1102. static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq)
  1103. {
  1104. int ret;
  1105. struct gpio_irq_chip *girq;
  1106. pctrl->chip = intel_gpio_chip;
  1107. /* Setup GPIO chip */
  1108. pctrl->chip.ngpio = intel_gpio_ngpio(pctrl);
  1109. pctrl->chip.label = dev_name(pctrl->dev);
  1110. pctrl->chip.parent = pctrl->dev;
  1111. pctrl->chip.base = -1;
  1112. pctrl->chip.add_pin_ranges = intel_gpio_add_pin_ranges;
  1113. pctrl->irq = irq;
  1114. /*
  1115. * On some platforms several GPIO controllers share the same interrupt
  1116. * line.
  1117. */
  1118. ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq,
  1119. IRQF_SHARED | IRQF_NO_THREAD,
  1120. dev_name(pctrl->dev), pctrl);
  1121. if (ret) {
  1122. dev_err(pctrl->dev, "failed to request interrupt\n");
  1123. return ret;
  1124. }
  1125. /* Setup IRQ chip */
  1126. girq = &pctrl->chip.irq;
  1127. gpio_irq_chip_set_chip(girq, &intel_gpio_irq_chip);
  1128. /* This will let us handle the IRQ in the driver */
  1129. girq->parent_handler = NULL;
  1130. girq->num_parents = 0;
  1131. girq->default_type = IRQ_TYPE_NONE;
  1132. girq->handler = handle_bad_irq;
  1133. girq->init_hw = intel_gpio_irq_init_hw;
  1134. ret = devm_gpiochip_add_data(pctrl->dev, &pctrl->chip, pctrl);
  1135. if (ret) {
  1136. dev_err(pctrl->dev, "failed to register gpiochip\n");
  1137. return ret;
  1138. }
  1139. return 0;
  1140. }
  1141. static int intel_pinctrl_add_padgroups_by_gpps(struct intel_pinctrl *pctrl,
  1142. struct intel_community *community)
  1143. {
  1144. struct intel_padgroup *gpps;
  1145. unsigned int padown_num = 0;
  1146. size_t i, ngpps = community->ngpps;
  1147. gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
  1148. if (!gpps)
  1149. return -ENOMEM;
  1150. for (i = 0; i < ngpps; i++) {
  1151. gpps[i] = community->gpps[i];
  1152. if (gpps[i].size > INTEL_PINCTRL_MAX_GPP_SIZE)
  1153. return -EINVAL;
  1154. /* Special treatment for GPIO base */
  1155. switch (gpps[i].gpio_base) {
  1156. case INTEL_GPIO_BASE_MATCH:
  1157. gpps[i].gpio_base = gpps[i].base;
  1158. break;
  1159. case INTEL_GPIO_BASE_ZERO:
  1160. gpps[i].gpio_base = 0;
  1161. break;
  1162. case INTEL_GPIO_BASE_NOMAP:
  1163. break;
  1164. default:
  1165. break;
  1166. }
  1167. gpps[i].padown_num = padown_num;
  1168. padown_num += DIV_ROUND_UP(gpps[i].size * 4, INTEL_PINCTRL_MAX_GPP_SIZE);
  1169. }
  1170. community->gpps = gpps;
  1171. return 0;
  1172. }
  1173. static int intel_pinctrl_add_padgroups_by_size(struct intel_pinctrl *pctrl,
  1174. struct intel_community *community)
  1175. {
  1176. struct intel_padgroup *gpps;
  1177. unsigned int npins = community->npins;
  1178. unsigned int padown_num = 0;
  1179. size_t i, ngpps = DIV_ROUND_UP(npins, community->gpp_size);
  1180. if (community->gpp_size > INTEL_PINCTRL_MAX_GPP_SIZE)
  1181. return -EINVAL;
  1182. gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
  1183. if (!gpps)
  1184. return -ENOMEM;
  1185. for (i = 0; i < ngpps; i++) {
  1186. unsigned int gpp_size = community->gpp_size;
  1187. gpps[i].reg_num = i;
  1188. gpps[i].base = community->pin_base + i * gpp_size;
  1189. gpps[i].size = min(gpp_size, npins);
  1190. npins -= gpps[i].size;
  1191. gpps[i].gpio_base = gpps[i].base;
  1192. gpps[i].padown_num = padown_num;
  1193. padown_num += community->gpp_num_padown_regs;
  1194. }
  1195. community->ngpps = ngpps;
  1196. community->gpps = gpps;
  1197. return 0;
  1198. }
  1199. static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl)
  1200. {
  1201. #ifdef CONFIG_PM_SLEEP
  1202. const struct intel_pinctrl_soc_data *soc = pctrl->soc;
  1203. struct intel_community_context *communities;
  1204. struct intel_pad_context *pads;
  1205. int i;
  1206. pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL);
  1207. if (!pads)
  1208. return -ENOMEM;
  1209. communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities,
  1210. sizeof(*communities), GFP_KERNEL);
  1211. if (!communities)
  1212. return -ENOMEM;
  1213. for (i = 0; i < pctrl->ncommunities; i++) {
  1214. struct intel_community *community = &pctrl->communities[i];
  1215. u32 *intmask, *hostown;
  1216. intmask = devm_kcalloc(pctrl->dev, community->ngpps,
  1217. sizeof(*intmask), GFP_KERNEL);
  1218. if (!intmask)
  1219. return -ENOMEM;
  1220. communities[i].intmask = intmask;
  1221. hostown = devm_kcalloc(pctrl->dev, community->ngpps,
  1222. sizeof(*hostown), GFP_KERNEL);
  1223. if (!hostown)
  1224. return -ENOMEM;
  1225. communities[i].hostown = hostown;
  1226. }
  1227. pctrl->context.pads = pads;
  1228. pctrl->context.communities = communities;
  1229. #endif
  1230. return 0;
  1231. }
  1232. static int intel_pinctrl_probe_pwm(struct intel_pinctrl *pctrl,
  1233. struct intel_community *community)
  1234. {
  1235. static const struct pwm_lpss_boardinfo info = {
  1236. .clk_rate = 19200000,
  1237. .npwm = 1,
  1238. .base_unit_bits = 22,
  1239. };
  1240. struct pwm_chip *chip;
  1241. if (!(community->features & PINCTRL_FEATURE_PWM))
  1242. return 0;
  1243. if (!IS_REACHABLE(CONFIG_PWM_LPSS))
  1244. return 0;
  1245. chip = devm_pwm_lpss_probe(pctrl->dev, community->regs + PWMC, &info);
  1246. return PTR_ERR_OR_ZERO(chip);
  1247. }
  1248. int intel_pinctrl_probe(struct platform_device *pdev,
  1249. const struct intel_pinctrl_soc_data *soc_data)
  1250. {
  1251. struct device *dev = &pdev->dev;
  1252. struct intel_pinctrl *pctrl;
  1253. int i, ret, irq;
  1254. pctrl = devm_kzalloc(dev, sizeof(*pctrl), GFP_KERNEL);
  1255. if (!pctrl)
  1256. return -ENOMEM;
  1257. pctrl->dev = dev;
  1258. pctrl->soc = soc_data;
  1259. raw_spin_lock_init(&pctrl->lock);
  1260. /*
  1261. * Make a copy of the communities which we can use to hold pointers
  1262. * to the registers.
  1263. */
  1264. pctrl->ncommunities = pctrl->soc->ncommunities;
  1265. pctrl->communities = devm_kcalloc(dev, pctrl->ncommunities,
  1266. sizeof(*pctrl->communities), GFP_KERNEL);
  1267. if (!pctrl->communities)
  1268. return -ENOMEM;
  1269. for (i = 0; i < pctrl->ncommunities; i++) {
  1270. struct intel_community *community = &pctrl->communities[i];
  1271. void __iomem *regs;
  1272. u32 offset;
  1273. u32 value;
  1274. *community = pctrl->soc->communities[i];
  1275. regs = devm_platform_ioremap_resource(pdev, community->barno);
  1276. if (IS_ERR(regs))
  1277. return PTR_ERR(regs);
  1278. /*
  1279. * Determine community features based on the revision.
  1280. * A value of all ones means the device is not present.
  1281. */
  1282. value = readl(regs + REVID);
  1283. if (value == ~0u)
  1284. return -ENODEV;
  1285. if (((value & REVID_MASK) >> REVID_SHIFT) >= 0x94) {
  1286. community->features |= PINCTRL_FEATURE_DEBOUNCE;
  1287. community->features |= PINCTRL_FEATURE_1K_PD;
  1288. }
  1289. /* Determine community features based on the capabilities */
  1290. offset = CAPLIST;
  1291. do {
  1292. value = readl(regs + offset);
  1293. switch ((value & CAPLIST_ID_MASK) >> CAPLIST_ID_SHIFT) {
  1294. case CAPLIST_ID_GPIO_HW_INFO:
  1295. community->features |= PINCTRL_FEATURE_GPIO_HW_INFO;
  1296. break;
  1297. case CAPLIST_ID_PWM:
  1298. community->features |= PINCTRL_FEATURE_PWM;
  1299. break;
  1300. case CAPLIST_ID_BLINK:
  1301. community->features |= PINCTRL_FEATURE_BLINK;
  1302. break;
  1303. case CAPLIST_ID_EXP:
  1304. community->features |= PINCTRL_FEATURE_EXP;
  1305. break;
  1306. default:
  1307. break;
  1308. }
  1309. offset = (value & CAPLIST_NEXT_MASK) >> CAPLIST_NEXT_SHIFT;
  1310. } while (offset);
  1311. dev_dbg(dev, "Community%d features: %#08x\n", i, community->features);
  1312. /* Read offset of the pad configuration registers */
  1313. offset = readl(regs + PADBAR);
  1314. community->regs = regs;
  1315. community->pad_regs = regs + offset;
  1316. if (community->gpps)
  1317. ret = intel_pinctrl_add_padgroups_by_gpps(pctrl, community);
  1318. else
  1319. ret = intel_pinctrl_add_padgroups_by_size(pctrl, community);
  1320. if (ret)
  1321. return ret;
  1322. ret = intel_pinctrl_probe_pwm(pctrl, community);
  1323. if (ret)
  1324. return ret;
  1325. }
  1326. irq = platform_get_irq(pdev, 0);
  1327. if (irq < 0)
  1328. return irq;
  1329. ret = intel_pinctrl_pm_init(pctrl);
  1330. if (ret)
  1331. return ret;
  1332. pctrl->pctldesc = intel_pinctrl_desc;
  1333. pctrl->pctldesc.name = dev_name(dev);
  1334. pctrl->pctldesc.pins = pctrl->soc->pins;
  1335. pctrl->pctldesc.npins = pctrl->soc->npins;
  1336. pctrl->pctldev = devm_pinctrl_register(dev, &pctrl->pctldesc, pctrl);
  1337. if (IS_ERR(pctrl->pctldev)) {
  1338. dev_err(dev, "failed to register pinctrl driver\n");
  1339. return PTR_ERR(pctrl->pctldev);
  1340. }
  1341. ret = intel_gpio_probe(pctrl, irq);
  1342. if (ret)
  1343. return ret;
  1344. platform_set_drvdata(pdev, pctrl);
  1345. return 0;
  1346. }
  1347. EXPORT_SYMBOL_NS_GPL(intel_pinctrl_probe, PINCTRL_INTEL);
  1348. int intel_pinctrl_probe_by_hid(struct platform_device *pdev)
  1349. {
  1350. const struct intel_pinctrl_soc_data *data;
  1351. data = device_get_match_data(&pdev->dev);
  1352. if (!data)
  1353. return -ENODATA;
  1354. return intel_pinctrl_probe(pdev, data);
  1355. }
  1356. EXPORT_SYMBOL_NS_GPL(intel_pinctrl_probe_by_hid, PINCTRL_INTEL);
  1357. int intel_pinctrl_probe_by_uid(struct platform_device *pdev)
  1358. {
  1359. const struct intel_pinctrl_soc_data *data;
  1360. data = intel_pinctrl_get_soc_data(pdev);
  1361. if (IS_ERR(data))
  1362. return PTR_ERR(data);
  1363. return intel_pinctrl_probe(pdev, data);
  1364. }
  1365. EXPORT_SYMBOL_NS_GPL(intel_pinctrl_probe_by_uid, PINCTRL_INTEL);
  1366. const struct intel_pinctrl_soc_data *intel_pinctrl_get_soc_data(struct platform_device *pdev)
  1367. {
  1368. const struct intel_pinctrl_soc_data * const *table;
  1369. const struct intel_pinctrl_soc_data *data;
  1370. struct device *dev = &pdev->dev;
  1371. table = device_get_match_data(dev);
  1372. if (table) {
  1373. struct acpi_device *adev = ACPI_COMPANION(dev);
  1374. unsigned int i;
  1375. for (i = 0; table[i]; i++) {
  1376. if (acpi_dev_uid_match(adev, table[i]->uid))
  1377. break;
  1378. }
  1379. data = table[i];
  1380. } else {
  1381. const struct platform_device_id *id;
  1382. id = platform_get_device_id(pdev);
  1383. if (!id)
  1384. return ERR_PTR(-ENODEV);
  1385. table = (const struct intel_pinctrl_soc_data * const *)id->driver_data;
  1386. data = table[pdev->id];
  1387. }
  1388. return data ?: ERR_PTR(-ENODATA);
  1389. }
  1390. EXPORT_SYMBOL_NS_GPL(intel_pinctrl_get_soc_data, PINCTRL_INTEL);
  1391. static bool __intel_gpio_is_direct_irq(u32 value)
  1392. {
  1393. return (value & PADCFG0_GPIROUTIOXAPIC) &&
  1394. (__intel_gpio_get_direction(value) == PAD_CONNECT_INPUT) &&
  1395. (__intel_gpio_get_gpio_mode(value) == PADCFG0_PMODE_GPIO);
  1396. }
  1397. static bool intel_pinctrl_should_save(struct intel_pinctrl *pctrl, unsigned int pin)
  1398. {
  1399. const struct pin_desc *pd = pin_desc_get(pctrl->pctldev, pin);
  1400. u32 value;
  1401. if (!pd || !intel_pad_usable(pctrl, pin))
  1402. return false;
  1403. /*
  1404. * Only restore the pin if it is actually in use by the kernel (or
  1405. * by userspace). It is possible that some pins are used by the
  1406. * BIOS during resume and those are not always locked down so leave
  1407. * them alone.
  1408. */
  1409. if (pd->mux_owner || pd->gpio_owner ||
  1410. gpiochip_line_is_irq(&pctrl->chip, intel_pin_to_gpio(pctrl, pin)))
  1411. return true;
  1412. /*
  1413. * The firmware on some systems may configure GPIO pins to be
  1414. * an interrupt source in so called "direct IRQ" mode. In such
  1415. * cases the GPIO controller driver has no idea if those pins
  1416. * are being used or not. At the same time, there is a known bug
  1417. * in the firmwares that don't restore the pin settings correctly
  1418. * after suspend, i.e. by an unknown reason the Rx value becomes
  1419. * inverted.
  1420. *
  1421. * Hence, let's save and restore the pins that are configured
  1422. * as GPIOs in the input mode with GPIROUTIOXAPIC bit set.
  1423. *
  1424. * See https://bugzilla.kernel.org/show_bug.cgi?id=214749.
  1425. */
  1426. value = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
  1427. if (__intel_gpio_is_direct_irq(value))
  1428. return true;
  1429. return false;
  1430. }
  1431. static int intel_pinctrl_suspend_noirq(struct device *dev)
  1432. {
  1433. struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
  1434. struct intel_community_context *communities;
  1435. struct intel_pad_context *pads;
  1436. int i;
  1437. pads = pctrl->context.pads;
  1438. for (i = 0; i < pctrl->soc->npins; i++) {
  1439. const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
  1440. void __iomem *padcfg;
  1441. u32 val;
  1442. if (!intel_pinctrl_should_save(pctrl, desc->number))
  1443. continue;
  1444. val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0));
  1445. pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE;
  1446. val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1));
  1447. pads[i].padcfg1 = val;
  1448. padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
  1449. if (padcfg)
  1450. pads[i].padcfg2 = readl(padcfg);
  1451. }
  1452. communities = pctrl->context.communities;
  1453. for (i = 0; i < pctrl->ncommunities; i++) {
  1454. struct intel_community *community = &pctrl->communities[i];
  1455. void __iomem *base;
  1456. unsigned int gpp;
  1457. base = community->regs + community->ie_offset;
  1458. for (gpp = 0; gpp < community->ngpps; gpp++)
  1459. communities[i].intmask[gpp] = readl(base + gpp * 4);
  1460. base = community->regs + community->hostown_offset;
  1461. for (gpp = 0; gpp < community->ngpps; gpp++)
  1462. communities[i].hostown[gpp] = readl(base + gpp * 4);
  1463. }
  1464. return 0;
  1465. }
  1466. static bool intel_gpio_update_reg(void __iomem *reg, u32 mask, u32 value)
  1467. {
  1468. u32 curr, updated;
  1469. curr = readl(reg);
  1470. updated = (curr & ~mask) | (value & mask);
  1471. if (curr == updated)
  1472. return false;
  1473. writel(updated, reg);
  1474. return true;
  1475. }
  1476. static void intel_restore_hostown(struct intel_pinctrl *pctrl, unsigned int c,
  1477. void __iomem *base, unsigned int gpp, u32 saved)
  1478. {
  1479. const struct intel_community *community = &pctrl->communities[c];
  1480. const struct intel_padgroup *padgrp = &community->gpps[gpp];
  1481. struct device *dev = pctrl->dev;
  1482. const char *dummy;
  1483. u32 requested = 0;
  1484. unsigned int i;
  1485. if (padgrp->gpio_base == INTEL_GPIO_BASE_NOMAP)
  1486. return;
  1487. for_each_requested_gpio_in_range(&pctrl->chip, i, padgrp->gpio_base, padgrp->size, dummy)
  1488. requested |= BIT(i);
  1489. if (!intel_gpio_update_reg(base + gpp * 4, requested, saved))
  1490. return;
  1491. dev_dbg(dev, "restored hostown %u/%u %#08x\n", c, gpp, readl(base + gpp * 4));
  1492. }
  1493. static void intel_restore_intmask(struct intel_pinctrl *pctrl, unsigned int c,
  1494. void __iomem *base, unsigned int gpp, u32 saved)
  1495. {
  1496. struct device *dev = pctrl->dev;
  1497. if (!intel_gpio_update_reg(base + gpp * 4, ~0U, saved))
  1498. return;
  1499. dev_dbg(dev, "restored mask %u/%u %#08x\n", c, gpp, readl(base + gpp * 4));
  1500. }
  1501. static void intel_restore_padcfg(struct intel_pinctrl *pctrl, unsigned int pin,
  1502. unsigned int reg, u32 saved)
  1503. {
  1504. u32 mask = (reg == PADCFG0) ? PADCFG0_GPIORXSTATE : 0;
  1505. unsigned int n = reg / sizeof(u32);
  1506. struct device *dev = pctrl->dev;
  1507. void __iomem *padcfg;
  1508. padcfg = intel_get_padcfg(pctrl, pin, reg);
  1509. if (!padcfg)
  1510. return;
  1511. if (!intel_gpio_update_reg(padcfg, ~mask, saved))
  1512. return;
  1513. dev_dbg(dev, "restored pin %u padcfg%u %#08x\n", pin, n, readl(padcfg));
  1514. }
  1515. static int intel_pinctrl_resume_noirq(struct device *dev)
  1516. {
  1517. struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
  1518. const struct intel_community_context *communities;
  1519. const struct intel_pad_context *pads;
  1520. int i;
  1521. /* Mask all interrupts */
  1522. intel_gpio_irq_init(pctrl);
  1523. pads = pctrl->context.pads;
  1524. for (i = 0; i < pctrl->soc->npins; i++) {
  1525. const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
  1526. if (!(intel_pinctrl_should_save(pctrl, desc->number) ||
  1527. /*
  1528. * If the firmware mangled the register contents too much,
  1529. * check the saved value for the Direct IRQ mode.
  1530. */
  1531. __intel_gpio_is_direct_irq(pads[i].padcfg0)))
  1532. continue;
  1533. intel_restore_padcfg(pctrl, desc->number, PADCFG0, pads[i].padcfg0);
  1534. intel_restore_padcfg(pctrl, desc->number, PADCFG1, pads[i].padcfg1);
  1535. intel_restore_padcfg(pctrl, desc->number, PADCFG2, pads[i].padcfg2);
  1536. }
  1537. communities = pctrl->context.communities;
  1538. for (i = 0; i < pctrl->ncommunities; i++) {
  1539. struct intel_community *community = &pctrl->communities[i];
  1540. void __iomem *base;
  1541. unsigned int gpp;
  1542. base = community->regs + community->ie_offset;
  1543. for (gpp = 0; gpp < community->ngpps; gpp++)
  1544. intel_restore_intmask(pctrl, i, base, gpp, communities[i].intmask[gpp]);
  1545. base = community->regs + community->hostown_offset;
  1546. for (gpp = 0; gpp < community->ngpps; gpp++)
  1547. intel_restore_hostown(pctrl, i, base, gpp, communities[i].hostown[gpp]);
  1548. }
  1549. return 0;
  1550. }
  1551. EXPORT_NS_GPL_DEV_SLEEP_PM_OPS(intel_pinctrl_pm_ops, PINCTRL_INTEL) = {
  1552. NOIRQ_SYSTEM_SLEEP_PM_OPS(intel_pinctrl_suspend_noirq, intel_pinctrl_resume_noirq)
  1553. };
  1554. MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>");
  1555. MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
  1556. MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
  1557. MODULE_LICENSE("GPL v2");