pinctrl-intel.c 29 KB

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