pinctrl-intel.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  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/platform_device.h>
  16. #include <linux/pinctrl/pinctrl.h>
  17. #include <linux/pinctrl/pinmux.h>
  18. #include <linux/pinctrl/pinconf.h>
  19. #include <linux/pinctrl/pinconf-generic.h>
  20. #include "pinctrl-intel.h"
  21. /* Offset from regs */
  22. #define PADBAR 0x00c
  23. #define GPI_IS 0x100
  24. #define GPI_GPE_STS 0x140
  25. #define GPI_GPE_EN 0x160
  26. #define PADOWN_BITS 4
  27. #define PADOWN_SHIFT(p) ((p) % 8 * PADOWN_BITS)
  28. #define PADOWN_MASK(p) (0xf << PADOWN_SHIFT(p))
  29. #define PADOWN_GPP(p) ((p) / 8)
  30. /* Offset from pad_regs */
  31. #define PADCFG0 0x000
  32. #define PADCFG0_RXEVCFG_SHIFT 25
  33. #define PADCFG0_RXEVCFG_MASK (3 << PADCFG0_RXEVCFG_SHIFT)
  34. #define PADCFG0_RXEVCFG_LEVEL 0
  35. #define PADCFG0_RXEVCFG_EDGE 1
  36. #define PADCFG0_RXEVCFG_DISABLED 2
  37. #define PADCFG0_RXEVCFG_EDGE_BOTH 3
  38. #define PADCFG0_RXINV BIT(23)
  39. #define PADCFG0_GPIROUTIOXAPIC BIT(20)
  40. #define PADCFG0_GPIROUTSCI BIT(19)
  41. #define PADCFG0_GPIROUTSMI BIT(18)
  42. #define PADCFG0_GPIROUTNMI BIT(17)
  43. #define PADCFG0_PMODE_SHIFT 10
  44. #define PADCFG0_PMODE_MASK (0xf << PADCFG0_PMODE_SHIFT)
  45. #define PADCFG0_GPIORXDIS BIT(9)
  46. #define PADCFG0_GPIOTXDIS BIT(8)
  47. #define PADCFG0_GPIORXSTATE BIT(1)
  48. #define PADCFG0_GPIOTXSTATE BIT(0)
  49. #define PADCFG1 0x004
  50. #define PADCFG1_TERM_UP BIT(13)
  51. #define PADCFG1_TERM_SHIFT 10
  52. #define PADCFG1_TERM_MASK (7 << PADCFG1_TERM_SHIFT)
  53. #define PADCFG1_TERM_20K 4
  54. #define PADCFG1_TERM_2K 3
  55. #define PADCFG1_TERM_5K 2
  56. #define PADCFG1_TERM_1K 1
  57. struct intel_pad_context {
  58. u32 padcfg0;
  59. u32 padcfg1;
  60. };
  61. struct intel_community_context {
  62. u32 *intmask;
  63. };
  64. struct intel_pinctrl_context {
  65. struct intel_pad_context *pads;
  66. struct intel_community_context *communities;
  67. };
  68. /**
  69. * struct intel_pinctrl - Intel pinctrl private structure
  70. * @dev: Pointer to the device structure
  71. * @lock: Lock to serialize register access
  72. * @pctldesc: Pin controller description
  73. * @pctldev: Pointer to the pin controller device
  74. * @chip: GPIO chip in this pin controller
  75. * @soc: SoC/PCH specific pin configuration data
  76. * @communities: All communities in this pin controller
  77. * @ncommunities: Number of communities in this pin controller
  78. * @context: Configuration saved over system sleep
  79. */
  80. struct intel_pinctrl {
  81. struct device *dev;
  82. raw_spinlock_t lock;
  83. struct pinctrl_desc pctldesc;
  84. struct pinctrl_dev *pctldev;
  85. struct gpio_chip chip;
  86. const struct intel_pinctrl_soc_data *soc;
  87. struct intel_community *communities;
  88. size_t ncommunities;
  89. struct intel_pinctrl_context context;
  90. };
  91. #define pin_to_padno(c, p) ((p) - (c)->pin_base)
  92. static struct intel_community *intel_get_community(struct intel_pinctrl *pctrl,
  93. unsigned pin)
  94. {
  95. struct intel_community *community;
  96. int i;
  97. for (i = 0; i < pctrl->ncommunities; i++) {
  98. community = &pctrl->communities[i];
  99. if (pin >= community->pin_base &&
  100. pin < community->pin_base + community->npins)
  101. return community;
  102. }
  103. dev_warn(pctrl->dev, "failed to find community for pin %u\n", pin);
  104. return NULL;
  105. }
  106. static void __iomem *intel_get_padcfg(struct intel_pinctrl *pctrl, unsigned pin,
  107. unsigned reg)
  108. {
  109. const struct intel_community *community;
  110. unsigned padno;
  111. community = intel_get_community(pctrl, pin);
  112. if (!community)
  113. return NULL;
  114. padno = pin_to_padno(community, pin);
  115. return community->pad_regs + reg + padno * 8;
  116. }
  117. static bool intel_pad_owned_by_host(struct intel_pinctrl *pctrl, unsigned pin)
  118. {
  119. const struct intel_community *community;
  120. unsigned padno, gpp, offset, group;
  121. void __iomem *padown;
  122. community = intel_get_community(pctrl, pin);
  123. if (!community)
  124. return false;
  125. if (!community->padown_offset)
  126. return true;
  127. padno = pin_to_padno(community, pin);
  128. group = padno / community->gpp_size;
  129. gpp = PADOWN_GPP(padno % community->gpp_size);
  130. offset = community->padown_offset + 0x10 * group + gpp * 4;
  131. padown = community->regs + offset;
  132. return !(readl(padown) & PADOWN_MASK(padno));
  133. }
  134. static bool intel_pad_acpi_mode(struct intel_pinctrl *pctrl, unsigned pin)
  135. {
  136. const struct intel_community *community;
  137. unsigned padno, gpp, offset;
  138. void __iomem *hostown;
  139. community = intel_get_community(pctrl, pin);
  140. if (!community)
  141. return true;
  142. if (!community->hostown_offset)
  143. return false;
  144. padno = pin_to_padno(community, pin);
  145. gpp = padno / community->gpp_size;
  146. offset = community->hostown_offset + gpp * 4;
  147. hostown = community->regs + offset;
  148. return !(readl(hostown) & BIT(padno % community->gpp_size));
  149. }
  150. static bool intel_pad_locked(struct intel_pinctrl *pctrl, unsigned pin)
  151. {
  152. struct intel_community *community;
  153. unsigned padno, gpp, offset;
  154. u32 value;
  155. community = intel_get_community(pctrl, pin);
  156. if (!community)
  157. return true;
  158. if (!community->padcfglock_offset)
  159. return false;
  160. padno = pin_to_padno(community, pin);
  161. gpp = padno / community->gpp_size;
  162. /*
  163. * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad,
  164. * the pad is considered unlocked. Any other case means that it is
  165. * either fully or partially locked and we don't touch it.
  166. */
  167. offset = community->padcfglock_offset + gpp * 8;
  168. value = readl(community->regs + offset);
  169. if (value & BIT(pin % community->gpp_size))
  170. return true;
  171. offset = community->padcfglock_offset + 4 + gpp * 8;
  172. value = readl(community->regs + offset);
  173. if (value & BIT(pin % community->gpp_size))
  174. return true;
  175. return false;
  176. }
  177. static bool intel_pad_usable(struct intel_pinctrl *pctrl, unsigned pin)
  178. {
  179. return intel_pad_owned_by_host(pctrl, pin) &&
  180. !intel_pad_locked(pctrl, pin);
  181. }
  182. static int intel_get_groups_count(struct pinctrl_dev *pctldev)
  183. {
  184. struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  185. return pctrl->soc->ngroups;
  186. }
  187. static const char *intel_get_group_name(struct pinctrl_dev *pctldev,
  188. unsigned group)
  189. {
  190. struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  191. return pctrl->soc->groups[group].name;
  192. }
  193. static int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned group,
  194. const unsigned **pins, unsigned *npins)
  195. {
  196. struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  197. *pins = pctrl->soc->groups[group].pins;
  198. *npins = pctrl->soc->groups[group].npins;
  199. return 0;
  200. }
  201. static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
  202. unsigned pin)
  203. {
  204. struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  205. u32 cfg0, cfg1, mode;
  206. bool locked, acpi;
  207. if (!intel_pad_owned_by_host(pctrl, pin)) {
  208. seq_puts(s, "not available");
  209. return;
  210. }
  211. cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
  212. cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
  213. mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
  214. if (!mode)
  215. seq_puts(s, "GPIO ");
  216. else
  217. seq_printf(s, "mode %d ", mode);
  218. seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1);
  219. locked = intel_pad_locked(pctrl, pin);
  220. acpi = intel_pad_acpi_mode(pctrl, pin);
  221. if (locked || acpi) {
  222. seq_puts(s, " [");
  223. if (locked) {
  224. seq_puts(s, "LOCKED");
  225. if (acpi)
  226. seq_puts(s, ", ");
  227. }
  228. if (acpi)
  229. seq_puts(s, "ACPI");
  230. seq_puts(s, "]");
  231. }
  232. }
  233. static const struct pinctrl_ops intel_pinctrl_ops = {
  234. .get_groups_count = intel_get_groups_count,
  235. .get_group_name = intel_get_group_name,
  236. .get_group_pins = intel_get_group_pins,
  237. .pin_dbg_show = intel_pin_dbg_show,
  238. };
  239. static int intel_get_functions_count(struct pinctrl_dev *pctldev)
  240. {
  241. struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  242. return pctrl->soc->nfunctions;
  243. }
  244. static const char *intel_get_function_name(struct pinctrl_dev *pctldev,
  245. unsigned function)
  246. {
  247. struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  248. return pctrl->soc->functions[function].name;
  249. }
  250. static int intel_get_function_groups(struct pinctrl_dev *pctldev,
  251. unsigned function,
  252. const char * const **groups,
  253. unsigned * const ngroups)
  254. {
  255. struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  256. *groups = pctrl->soc->functions[function].groups;
  257. *ngroups = pctrl->soc->functions[function].ngroups;
  258. return 0;
  259. }
  260. static int intel_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned function,
  261. unsigned group)
  262. {
  263. struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  264. const struct intel_pingroup *grp = &pctrl->soc->groups[group];
  265. unsigned long flags;
  266. int i;
  267. raw_spin_lock_irqsave(&pctrl->lock, flags);
  268. /*
  269. * All pins in the groups needs to be accessible and writable
  270. * before we can enable the mux for this group.
  271. */
  272. for (i = 0; i < grp->npins; i++) {
  273. if (!intel_pad_usable(pctrl, grp->pins[i])) {
  274. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  275. return -EBUSY;
  276. }
  277. }
  278. /* Now enable the mux setting for each pin in the group */
  279. for (i = 0; i < grp->npins; i++) {
  280. void __iomem *padcfg0;
  281. u32 value;
  282. padcfg0 = intel_get_padcfg(pctrl, grp->pins[i], PADCFG0);
  283. value = readl(padcfg0);
  284. value &= ~PADCFG0_PMODE_MASK;
  285. value |= grp->mode << PADCFG0_PMODE_SHIFT;
  286. writel(value, padcfg0);
  287. }
  288. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  289. return 0;
  290. }
  291. static int intel_gpio_request_enable(struct pinctrl_dev *pctldev,
  292. struct pinctrl_gpio_range *range,
  293. unsigned pin)
  294. {
  295. struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  296. void __iomem *padcfg0;
  297. unsigned long flags;
  298. u32 value;
  299. raw_spin_lock_irqsave(&pctrl->lock, flags);
  300. if (!intel_pad_usable(pctrl, pin)) {
  301. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  302. return -EBUSY;
  303. }
  304. padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
  305. /* Put the pad into GPIO mode */
  306. value = readl(padcfg0) & ~PADCFG0_PMODE_MASK;
  307. /* Disable SCI/SMI/NMI generation */
  308. value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI);
  309. value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI);
  310. /* Disable TX buffer and enable RX (this will be input) */
  311. value &= ~PADCFG0_GPIORXDIS;
  312. value |= PADCFG0_GPIOTXDIS;
  313. writel(value, padcfg0);
  314. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  315. return 0;
  316. }
  317. static int intel_gpio_set_direction(struct pinctrl_dev *pctldev,
  318. struct pinctrl_gpio_range *range,
  319. unsigned pin, bool input)
  320. {
  321. struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  322. void __iomem *padcfg0;
  323. unsigned long flags;
  324. u32 value;
  325. raw_spin_lock_irqsave(&pctrl->lock, flags);
  326. padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
  327. value = readl(padcfg0);
  328. if (input)
  329. value |= PADCFG0_GPIOTXDIS;
  330. else
  331. value &= ~PADCFG0_GPIOTXDIS;
  332. writel(value, padcfg0);
  333. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  334. return 0;
  335. }
  336. static const struct pinmux_ops intel_pinmux_ops = {
  337. .get_functions_count = intel_get_functions_count,
  338. .get_function_name = intel_get_function_name,
  339. .get_function_groups = intel_get_function_groups,
  340. .set_mux = intel_pinmux_set_mux,
  341. .gpio_request_enable = intel_gpio_request_enable,
  342. .gpio_set_direction = intel_gpio_set_direction,
  343. };
  344. static int intel_config_get(struct pinctrl_dev *pctldev, unsigned pin,
  345. unsigned long *config)
  346. {
  347. struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  348. enum pin_config_param param = pinconf_to_config_param(*config);
  349. u32 value, term;
  350. u16 arg = 0;
  351. if (!intel_pad_owned_by_host(pctrl, pin))
  352. return -ENOTSUPP;
  353. value = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
  354. term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT;
  355. switch (param) {
  356. case PIN_CONFIG_BIAS_DISABLE:
  357. if (term)
  358. return -EINVAL;
  359. break;
  360. case PIN_CONFIG_BIAS_PULL_UP:
  361. if (!term || !(value & PADCFG1_TERM_UP))
  362. return -EINVAL;
  363. switch (term) {
  364. case PADCFG1_TERM_1K:
  365. arg = 1000;
  366. break;
  367. case PADCFG1_TERM_2K:
  368. arg = 2000;
  369. break;
  370. case PADCFG1_TERM_5K:
  371. arg = 5000;
  372. break;
  373. case PADCFG1_TERM_20K:
  374. arg = 20000;
  375. break;
  376. }
  377. break;
  378. case PIN_CONFIG_BIAS_PULL_DOWN:
  379. if (!term || value & PADCFG1_TERM_UP)
  380. return -EINVAL;
  381. switch (term) {
  382. case PADCFG1_TERM_5K:
  383. arg = 5000;
  384. break;
  385. case PADCFG1_TERM_20K:
  386. arg = 20000;
  387. break;
  388. }
  389. break;
  390. default:
  391. return -ENOTSUPP;
  392. }
  393. *config = pinconf_to_config_packed(param, arg);
  394. return 0;
  395. }
  396. static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned pin,
  397. unsigned long config)
  398. {
  399. unsigned param = pinconf_to_config_param(config);
  400. unsigned arg = pinconf_to_config_argument(config);
  401. void __iomem *padcfg1;
  402. unsigned long flags;
  403. int ret = 0;
  404. u32 value;
  405. raw_spin_lock_irqsave(&pctrl->lock, flags);
  406. padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
  407. value = readl(padcfg1);
  408. switch (param) {
  409. case PIN_CONFIG_BIAS_DISABLE:
  410. value &= ~(PADCFG1_TERM_MASK | PADCFG1_TERM_UP);
  411. break;
  412. case PIN_CONFIG_BIAS_PULL_UP:
  413. value &= ~PADCFG1_TERM_MASK;
  414. value |= PADCFG1_TERM_UP;
  415. switch (arg) {
  416. case 20000:
  417. value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
  418. break;
  419. case 5000:
  420. value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
  421. break;
  422. case 2000:
  423. value |= PADCFG1_TERM_2K << PADCFG1_TERM_SHIFT;
  424. break;
  425. case 1000:
  426. value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
  427. break;
  428. default:
  429. ret = -EINVAL;
  430. }
  431. break;
  432. case PIN_CONFIG_BIAS_PULL_DOWN:
  433. value &= ~(PADCFG1_TERM_UP | PADCFG1_TERM_MASK);
  434. switch (arg) {
  435. case 20000:
  436. value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
  437. break;
  438. case 5000:
  439. value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
  440. break;
  441. default:
  442. ret = -EINVAL;
  443. }
  444. break;
  445. }
  446. if (!ret)
  447. writel(value, padcfg1);
  448. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  449. return ret;
  450. }
  451. static int intel_config_set(struct pinctrl_dev *pctldev, unsigned pin,
  452. unsigned long *configs, unsigned nconfigs)
  453. {
  454. struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  455. int i, ret;
  456. if (!intel_pad_usable(pctrl, pin))
  457. return -ENOTSUPP;
  458. for (i = 0; i < nconfigs; i++) {
  459. switch (pinconf_to_config_param(configs[i])) {
  460. case PIN_CONFIG_BIAS_DISABLE:
  461. case PIN_CONFIG_BIAS_PULL_UP:
  462. case PIN_CONFIG_BIAS_PULL_DOWN:
  463. ret = intel_config_set_pull(pctrl, pin, configs[i]);
  464. if (ret)
  465. return ret;
  466. break;
  467. default:
  468. return -ENOTSUPP;
  469. }
  470. }
  471. return 0;
  472. }
  473. static const struct pinconf_ops intel_pinconf_ops = {
  474. .is_generic = true,
  475. .pin_config_get = intel_config_get,
  476. .pin_config_set = intel_config_set,
  477. };
  478. static const struct pinctrl_desc intel_pinctrl_desc = {
  479. .pctlops = &intel_pinctrl_ops,
  480. .pmxops = &intel_pinmux_ops,
  481. .confops = &intel_pinconf_ops,
  482. .owner = THIS_MODULE,
  483. };
  484. static int intel_gpio_get(struct gpio_chip *chip, unsigned offset)
  485. {
  486. struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
  487. void __iomem *reg;
  488. reg = intel_get_padcfg(pctrl, offset, PADCFG0);
  489. if (!reg)
  490. return -EINVAL;
  491. return !!(readl(reg) & PADCFG0_GPIORXSTATE);
  492. }
  493. static void intel_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  494. {
  495. struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
  496. void __iomem *reg;
  497. reg = intel_get_padcfg(pctrl, offset, PADCFG0);
  498. if (reg) {
  499. unsigned long flags;
  500. u32 padcfg0;
  501. raw_spin_lock_irqsave(&pctrl->lock, flags);
  502. padcfg0 = readl(reg);
  503. if (value)
  504. padcfg0 |= PADCFG0_GPIOTXSTATE;
  505. else
  506. padcfg0 &= ~PADCFG0_GPIOTXSTATE;
  507. writel(padcfg0, reg);
  508. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  509. }
  510. }
  511. static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  512. {
  513. return pinctrl_gpio_direction_input(chip->base + offset);
  514. }
  515. static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  516. int value)
  517. {
  518. intel_gpio_set(chip, offset, value);
  519. return pinctrl_gpio_direction_output(chip->base + offset);
  520. }
  521. static const struct gpio_chip intel_gpio_chip = {
  522. .owner = THIS_MODULE,
  523. .request = gpiochip_generic_request,
  524. .free = gpiochip_generic_free,
  525. .direction_input = intel_gpio_direction_input,
  526. .direction_output = intel_gpio_direction_output,
  527. .get = intel_gpio_get,
  528. .set = intel_gpio_set,
  529. };
  530. static void intel_gpio_irq_ack(struct irq_data *d)
  531. {
  532. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  533. struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
  534. const struct intel_community *community;
  535. unsigned pin = irqd_to_hwirq(d);
  536. raw_spin_lock(&pctrl->lock);
  537. community = intel_get_community(pctrl, pin);
  538. if (community) {
  539. unsigned padno = pin_to_padno(community, pin);
  540. unsigned gpp_offset = padno % community->gpp_size;
  541. unsigned gpp = padno / community->gpp_size;
  542. writel(BIT(gpp_offset), community->regs + GPI_IS + gpp * 4);
  543. }
  544. raw_spin_unlock(&pctrl->lock);
  545. }
  546. static void intel_gpio_irq_enable(struct irq_data *d)
  547. {
  548. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  549. struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
  550. const struct intel_community *community;
  551. unsigned pin = irqd_to_hwirq(d);
  552. unsigned long flags;
  553. raw_spin_lock_irqsave(&pctrl->lock, flags);
  554. community = intel_get_community(pctrl, pin);
  555. if (community) {
  556. unsigned padno = pin_to_padno(community, pin);
  557. unsigned gpp_size = community->gpp_size;
  558. unsigned gpp_offset = padno % gpp_size;
  559. unsigned gpp = padno / gpp_size;
  560. u32 value;
  561. /* Clear interrupt status first to avoid unexpected interrupt */
  562. writel(BIT(gpp_offset), community->regs + GPI_IS + gpp * 4);
  563. value = readl(community->regs + community->ie_offset + gpp * 4);
  564. value |= BIT(gpp_offset);
  565. writel(value, community->regs + community->ie_offset + gpp * 4);
  566. }
  567. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  568. }
  569. static void intel_gpio_irq_mask_unmask(struct irq_data *d, bool mask)
  570. {
  571. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  572. struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
  573. const struct intel_community *community;
  574. unsigned pin = irqd_to_hwirq(d);
  575. unsigned long flags;
  576. raw_spin_lock_irqsave(&pctrl->lock, flags);
  577. community = intel_get_community(pctrl, pin);
  578. if (community) {
  579. unsigned padno = pin_to_padno(community, pin);
  580. unsigned gpp_offset = padno % community->gpp_size;
  581. unsigned gpp = padno / community->gpp_size;
  582. void __iomem *reg;
  583. u32 value;
  584. reg = community->regs + community->ie_offset + gpp * 4;
  585. value = readl(reg);
  586. if (mask)
  587. value &= ~BIT(gpp_offset);
  588. else
  589. value |= BIT(gpp_offset);
  590. writel(value, reg);
  591. }
  592. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  593. }
  594. static void intel_gpio_irq_mask(struct irq_data *d)
  595. {
  596. intel_gpio_irq_mask_unmask(d, true);
  597. }
  598. static void intel_gpio_irq_unmask(struct irq_data *d)
  599. {
  600. intel_gpio_irq_mask_unmask(d, false);
  601. }
  602. static int intel_gpio_irq_type(struct irq_data *d, unsigned type)
  603. {
  604. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  605. struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
  606. unsigned pin = irqd_to_hwirq(d);
  607. unsigned long flags;
  608. void __iomem *reg;
  609. u32 value;
  610. reg = intel_get_padcfg(pctrl, pin, PADCFG0);
  611. if (!reg)
  612. return -EINVAL;
  613. /*
  614. * If the pin is in ACPI mode it is still usable as a GPIO but it
  615. * cannot be used as IRQ because GPI_IS status bit will not be
  616. * updated by the host controller hardware.
  617. */
  618. if (intel_pad_acpi_mode(pctrl, pin)) {
  619. dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin);
  620. return -EPERM;
  621. }
  622. raw_spin_lock_irqsave(&pctrl->lock, flags);
  623. value = readl(reg);
  624. value &= ~(PADCFG0_RXEVCFG_MASK | PADCFG0_RXINV);
  625. if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
  626. value |= PADCFG0_RXEVCFG_EDGE_BOTH << PADCFG0_RXEVCFG_SHIFT;
  627. } else if (type & IRQ_TYPE_EDGE_FALLING) {
  628. value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
  629. value |= PADCFG0_RXINV;
  630. } else if (type & IRQ_TYPE_EDGE_RISING) {
  631. value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
  632. } else if (type & IRQ_TYPE_LEVEL_MASK) {
  633. if (type & IRQ_TYPE_LEVEL_LOW)
  634. value |= PADCFG0_RXINV;
  635. } else {
  636. value |= PADCFG0_RXEVCFG_DISABLED << PADCFG0_RXEVCFG_SHIFT;
  637. }
  638. writel(value, reg);
  639. if (type & IRQ_TYPE_EDGE_BOTH)
  640. irq_set_handler_locked(d, handle_edge_irq);
  641. else if (type & IRQ_TYPE_LEVEL_MASK)
  642. irq_set_handler_locked(d, handle_level_irq);
  643. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  644. return 0;
  645. }
  646. static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on)
  647. {
  648. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  649. struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
  650. const struct intel_community *community;
  651. unsigned pin = irqd_to_hwirq(d);
  652. unsigned padno, gpp, gpp_offset;
  653. unsigned long flags;
  654. u32 gpe_en;
  655. community = intel_get_community(pctrl, pin);
  656. if (!community)
  657. return -EINVAL;
  658. raw_spin_lock_irqsave(&pctrl->lock, flags);
  659. padno = pin_to_padno(community, pin);
  660. gpp = padno / community->gpp_size;
  661. gpp_offset = padno % community->gpp_size;
  662. /* Clear the existing wake status */
  663. writel(BIT(gpp_offset), community->regs + GPI_GPE_STS + gpp * 4);
  664. /*
  665. * The controller will generate wake when GPE of the corresponding
  666. * pad is enabled and it is not routed to SCI (GPIROUTSCI is not
  667. * set).
  668. */
  669. gpe_en = readl(community->regs + GPI_GPE_EN + gpp * 4);
  670. if (on)
  671. gpe_en |= BIT(gpp_offset);
  672. else
  673. gpe_en &= ~BIT(gpp_offset);
  674. writel(gpe_en, community->regs + GPI_GPE_EN + gpp * 4);
  675. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  676. dev_dbg(pctrl->dev, "%sable wake for pin %u\n", on ? "en" : "dis", pin);
  677. return 0;
  678. }
  679. static irqreturn_t intel_gpio_community_irq_handler(struct intel_pinctrl *pctrl,
  680. const struct intel_community *community)
  681. {
  682. struct gpio_chip *gc = &pctrl->chip;
  683. irqreturn_t ret = IRQ_NONE;
  684. int gpp;
  685. for (gpp = 0; gpp < community->ngpps; gpp++) {
  686. unsigned long pending, enabled, gpp_offset;
  687. pending = readl(community->regs + GPI_IS + gpp * 4);
  688. enabled = readl(community->regs + community->ie_offset +
  689. gpp * 4);
  690. /* Only interrupts that are enabled */
  691. pending &= enabled;
  692. for_each_set_bit(gpp_offset, &pending, community->gpp_size) {
  693. unsigned padno, irq;
  694. /*
  695. * The last group in community can have less pins
  696. * than NPADS_IN_GPP.
  697. */
  698. padno = gpp_offset + gpp * community->gpp_size;
  699. if (padno >= community->npins)
  700. break;
  701. irq = irq_find_mapping(gc->irqdomain,
  702. community->pin_base + padno);
  703. generic_handle_irq(irq);
  704. ret |= IRQ_HANDLED;
  705. }
  706. }
  707. return ret;
  708. }
  709. static irqreturn_t intel_gpio_irq(int irq, void *data)
  710. {
  711. const struct intel_community *community;
  712. struct intel_pinctrl *pctrl = data;
  713. irqreturn_t ret = IRQ_NONE;
  714. int i;
  715. /* Need to check all communities for pending interrupts */
  716. for (i = 0; i < pctrl->ncommunities; i++) {
  717. community = &pctrl->communities[i];
  718. ret |= intel_gpio_community_irq_handler(pctrl, community);
  719. }
  720. return ret;
  721. }
  722. static struct irq_chip intel_gpio_irqchip = {
  723. .name = "intel-gpio",
  724. .irq_enable = intel_gpio_irq_enable,
  725. .irq_ack = intel_gpio_irq_ack,
  726. .irq_mask = intel_gpio_irq_mask,
  727. .irq_unmask = intel_gpio_irq_unmask,
  728. .irq_set_type = intel_gpio_irq_type,
  729. .irq_set_wake = intel_gpio_irq_wake,
  730. };
  731. static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq)
  732. {
  733. int ret;
  734. pctrl->chip = intel_gpio_chip;
  735. pctrl->chip.ngpio = pctrl->soc->npins;
  736. pctrl->chip.label = dev_name(pctrl->dev);
  737. pctrl->chip.parent = pctrl->dev;
  738. pctrl->chip.base = -1;
  739. ret = gpiochip_add_data(&pctrl->chip, pctrl);
  740. if (ret) {
  741. dev_err(pctrl->dev, "failed to register gpiochip\n");
  742. return ret;
  743. }
  744. ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev),
  745. 0, 0, pctrl->soc->npins);
  746. if (ret) {
  747. dev_err(pctrl->dev, "failed to add GPIO pin range\n");
  748. goto fail;
  749. }
  750. /*
  751. * We need to request the interrupt here (instead of providing chip
  752. * to the irq directly) because on some platforms several GPIO
  753. * controllers share the same interrupt line.
  754. */
  755. ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq,
  756. IRQF_SHARED | IRQF_NO_THREAD,
  757. dev_name(pctrl->dev), pctrl);
  758. if (ret) {
  759. dev_err(pctrl->dev, "failed to request interrupt\n");
  760. goto fail;
  761. }
  762. ret = gpiochip_irqchip_add(&pctrl->chip, &intel_gpio_irqchip, 0,
  763. handle_simple_irq, IRQ_TYPE_NONE);
  764. if (ret) {
  765. dev_err(pctrl->dev, "failed to add irqchip\n");
  766. goto fail;
  767. }
  768. gpiochip_set_chained_irqchip(&pctrl->chip, &intel_gpio_irqchip, irq,
  769. NULL);
  770. return 0;
  771. fail:
  772. gpiochip_remove(&pctrl->chip);
  773. return ret;
  774. }
  775. static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl)
  776. {
  777. #ifdef CONFIG_PM_SLEEP
  778. const struct intel_pinctrl_soc_data *soc = pctrl->soc;
  779. struct intel_community_context *communities;
  780. struct intel_pad_context *pads;
  781. int i;
  782. pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL);
  783. if (!pads)
  784. return -ENOMEM;
  785. communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities,
  786. sizeof(*communities), GFP_KERNEL);
  787. if (!communities)
  788. return -ENOMEM;
  789. for (i = 0; i < pctrl->ncommunities; i++) {
  790. struct intel_community *community = &pctrl->communities[i];
  791. u32 *intmask;
  792. intmask = devm_kcalloc(pctrl->dev, community->ngpps,
  793. sizeof(*intmask), GFP_KERNEL);
  794. if (!intmask)
  795. return -ENOMEM;
  796. communities[i].intmask = intmask;
  797. }
  798. pctrl->context.pads = pads;
  799. pctrl->context.communities = communities;
  800. #endif
  801. return 0;
  802. }
  803. int intel_pinctrl_probe(struct platform_device *pdev,
  804. const struct intel_pinctrl_soc_data *soc_data)
  805. {
  806. struct intel_pinctrl *pctrl;
  807. int i, ret, irq;
  808. if (!soc_data)
  809. return -EINVAL;
  810. pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
  811. if (!pctrl)
  812. return -ENOMEM;
  813. pctrl->dev = &pdev->dev;
  814. pctrl->soc = soc_data;
  815. raw_spin_lock_init(&pctrl->lock);
  816. /*
  817. * Make a copy of the communities which we can use to hold pointers
  818. * to the registers.
  819. */
  820. pctrl->ncommunities = pctrl->soc->ncommunities;
  821. pctrl->communities = devm_kcalloc(&pdev->dev, pctrl->ncommunities,
  822. sizeof(*pctrl->communities), GFP_KERNEL);
  823. if (!pctrl->communities)
  824. return -ENOMEM;
  825. for (i = 0; i < pctrl->ncommunities; i++) {
  826. struct intel_community *community = &pctrl->communities[i];
  827. struct resource *res;
  828. void __iomem *regs;
  829. u32 padbar;
  830. *community = pctrl->soc->communities[i];
  831. res = platform_get_resource(pdev, IORESOURCE_MEM,
  832. community->barno);
  833. regs = devm_ioremap_resource(&pdev->dev, res);
  834. if (IS_ERR(regs))
  835. return PTR_ERR(regs);
  836. /* Read offset of the pad configuration registers */
  837. padbar = readl(regs + PADBAR);
  838. community->regs = regs;
  839. community->pad_regs = regs + padbar;
  840. community->ngpps = DIV_ROUND_UP(community->npins,
  841. community->gpp_size);
  842. }
  843. irq = platform_get_irq(pdev, 0);
  844. if (irq < 0) {
  845. dev_err(&pdev->dev, "failed to get interrupt number\n");
  846. return irq;
  847. }
  848. ret = intel_pinctrl_pm_init(pctrl);
  849. if (ret)
  850. return ret;
  851. pctrl->pctldesc = intel_pinctrl_desc;
  852. pctrl->pctldesc.name = dev_name(&pdev->dev);
  853. pctrl->pctldesc.pins = pctrl->soc->pins;
  854. pctrl->pctldesc.npins = pctrl->soc->npins;
  855. pctrl->pctldev = devm_pinctrl_register(&pdev->dev, &pctrl->pctldesc,
  856. pctrl);
  857. if (IS_ERR(pctrl->pctldev)) {
  858. dev_err(&pdev->dev, "failed to register pinctrl driver\n");
  859. return PTR_ERR(pctrl->pctldev);
  860. }
  861. ret = intel_gpio_probe(pctrl, irq);
  862. if (ret)
  863. return ret;
  864. platform_set_drvdata(pdev, pctrl);
  865. return 0;
  866. }
  867. EXPORT_SYMBOL_GPL(intel_pinctrl_probe);
  868. int intel_pinctrl_remove(struct platform_device *pdev)
  869. {
  870. struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
  871. gpiochip_remove(&pctrl->chip);
  872. return 0;
  873. }
  874. EXPORT_SYMBOL_GPL(intel_pinctrl_remove);
  875. #ifdef CONFIG_PM_SLEEP
  876. int intel_pinctrl_suspend(struct device *dev)
  877. {
  878. struct platform_device *pdev = to_platform_device(dev);
  879. struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
  880. struct intel_community_context *communities;
  881. struct intel_pad_context *pads;
  882. int i;
  883. pads = pctrl->context.pads;
  884. for (i = 0; i < pctrl->soc->npins; i++) {
  885. const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
  886. u32 val;
  887. if (!intel_pad_usable(pctrl, desc->number))
  888. continue;
  889. val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0));
  890. pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE;
  891. val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1));
  892. pads[i].padcfg1 = val;
  893. }
  894. communities = pctrl->context.communities;
  895. for (i = 0; i < pctrl->ncommunities; i++) {
  896. struct intel_community *community = &pctrl->communities[i];
  897. void __iomem *base;
  898. unsigned gpp;
  899. base = community->regs + community->ie_offset;
  900. for (gpp = 0; gpp < community->ngpps; gpp++)
  901. communities[i].intmask[gpp] = readl(base + gpp * 4);
  902. }
  903. return 0;
  904. }
  905. EXPORT_SYMBOL_GPL(intel_pinctrl_suspend);
  906. static void intel_gpio_irq_init(struct intel_pinctrl *pctrl)
  907. {
  908. size_t i;
  909. for (i = 0; i < pctrl->ncommunities; i++) {
  910. const struct intel_community *community;
  911. void __iomem *base;
  912. unsigned gpp;
  913. community = &pctrl->communities[i];
  914. base = community->regs;
  915. for (gpp = 0; gpp < community->ngpps; gpp++) {
  916. /* Mask and clear all interrupts */
  917. writel(0, base + community->ie_offset + gpp * 4);
  918. writel(0xffff, base + GPI_IS + gpp * 4);
  919. }
  920. }
  921. }
  922. int intel_pinctrl_resume(struct device *dev)
  923. {
  924. struct platform_device *pdev = to_platform_device(dev);
  925. struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
  926. const struct intel_community_context *communities;
  927. const struct intel_pad_context *pads;
  928. int i;
  929. /* Mask all interrupts */
  930. intel_gpio_irq_init(pctrl);
  931. pads = pctrl->context.pads;
  932. for (i = 0; i < pctrl->soc->npins; i++) {
  933. const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
  934. void __iomem *padcfg;
  935. u32 val;
  936. if (!intel_pad_usable(pctrl, desc->number))
  937. continue;
  938. padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG0);
  939. val = readl(padcfg) & ~PADCFG0_GPIORXSTATE;
  940. if (val != pads[i].padcfg0) {
  941. writel(pads[i].padcfg0, padcfg);
  942. dev_dbg(dev, "restored pin %u padcfg0 %#08x\n",
  943. desc->number, readl(padcfg));
  944. }
  945. padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG1);
  946. val = readl(padcfg);
  947. if (val != pads[i].padcfg1) {
  948. writel(pads[i].padcfg1, padcfg);
  949. dev_dbg(dev, "restored pin %u padcfg1 %#08x\n",
  950. desc->number, readl(padcfg));
  951. }
  952. }
  953. communities = pctrl->context.communities;
  954. for (i = 0; i < pctrl->ncommunities; i++) {
  955. struct intel_community *community = &pctrl->communities[i];
  956. void __iomem *base;
  957. unsigned gpp;
  958. base = community->regs + community->ie_offset;
  959. for (gpp = 0; gpp < community->ngpps; gpp++) {
  960. writel(communities[i].intmask[gpp], base + gpp * 4);
  961. dev_dbg(dev, "restored mask %d/%u %#08x\n", i, gpp,
  962. readl(base + gpp * 4));
  963. }
  964. }
  965. return 0;
  966. }
  967. EXPORT_SYMBOL_GPL(intel_pinctrl_resume);
  968. #endif
  969. MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>");
  970. MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
  971. MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
  972. MODULE_LICENSE("GPL v2");