pinctrl-intel.c 38 KB

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