pinctrl-intel.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  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. 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. 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. 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. 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. spin_lock_irqsave(&pctrl->lock, flags);
  300. if (!intel_pad_usable(pctrl, pin)) {
  301. 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. 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. 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. 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. 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. 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. 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. 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. 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. spin_unlock(&pctrl->lock);
  545. }
  546. static void intel_gpio_irq_mask_unmask(struct irq_data *d, bool mask)
  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. 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_offset = padno % community->gpp_size;
  558. unsigned gpp = padno / community->gpp_size;
  559. void __iomem *reg;
  560. u32 value;
  561. reg = community->regs + community->ie_offset + gpp * 4;
  562. value = readl(reg);
  563. if (mask)
  564. value &= ~BIT(gpp_offset);
  565. else
  566. value |= BIT(gpp_offset);
  567. writel(value, reg);
  568. }
  569. spin_unlock_irqrestore(&pctrl->lock, flags);
  570. }
  571. static void intel_gpio_irq_mask(struct irq_data *d)
  572. {
  573. intel_gpio_irq_mask_unmask(d, true);
  574. }
  575. static void intel_gpio_irq_unmask(struct irq_data *d)
  576. {
  577. intel_gpio_irq_mask_unmask(d, false);
  578. }
  579. static int intel_gpio_irq_type(struct irq_data *d, unsigned type)
  580. {
  581. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  582. struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
  583. unsigned pin = irqd_to_hwirq(d);
  584. unsigned long flags;
  585. void __iomem *reg;
  586. u32 value;
  587. reg = intel_get_padcfg(pctrl, pin, PADCFG0);
  588. if (!reg)
  589. return -EINVAL;
  590. /*
  591. * If the pin is in ACPI mode it is still usable as a GPIO but it
  592. * cannot be used as IRQ because GPI_IS status bit will not be
  593. * updated by the host controller hardware.
  594. */
  595. if (intel_pad_acpi_mode(pctrl, pin)) {
  596. dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin);
  597. return -EPERM;
  598. }
  599. spin_lock_irqsave(&pctrl->lock, flags);
  600. value = readl(reg);
  601. value &= ~(PADCFG0_RXEVCFG_MASK | PADCFG0_RXINV);
  602. if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
  603. value |= PADCFG0_RXEVCFG_EDGE_BOTH << PADCFG0_RXEVCFG_SHIFT;
  604. } else if (type & IRQ_TYPE_EDGE_FALLING) {
  605. value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
  606. value |= PADCFG0_RXINV;
  607. } else if (type & IRQ_TYPE_EDGE_RISING) {
  608. value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
  609. } else if (type & IRQ_TYPE_LEVEL_LOW) {
  610. value |= PADCFG0_RXINV;
  611. } else {
  612. value |= PADCFG0_RXEVCFG_DISABLED << PADCFG0_RXEVCFG_SHIFT;
  613. }
  614. writel(value, reg);
  615. if (type & IRQ_TYPE_EDGE_BOTH)
  616. irq_set_handler_locked(d, handle_edge_irq);
  617. else if (type & IRQ_TYPE_LEVEL_MASK)
  618. irq_set_handler_locked(d, handle_level_irq);
  619. spin_unlock_irqrestore(&pctrl->lock, flags);
  620. return 0;
  621. }
  622. static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on)
  623. {
  624. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  625. struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
  626. const struct intel_community *community;
  627. unsigned pin = irqd_to_hwirq(d);
  628. unsigned padno, gpp, gpp_offset;
  629. u32 gpe_en;
  630. community = intel_get_community(pctrl, pin);
  631. if (!community)
  632. return -EINVAL;
  633. padno = pin_to_padno(community, pin);
  634. gpp = padno / community->gpp_size;
  635. gpp_offset = padno % community->gpp_size;
  636. /* Clear the existing wake status */
  637. writel(BIT(gpp_offset), community->regs + GPI_GPE_STS + gpp * 4);
  638. /*
  639. * The controller will generate wake when GPE of the corresponding
  640. * pad is enabled and it is not routed to SCI (GPIROUTSCI is not
  641. * set).
  642. */
  643. gpe_en = readl(community->regs + GPI_GPE_EN + gpp * 4);
  644. if (on)
  645. gpe_en |= BIT(gpp_offset);
  646. else
  647. gpe_en &= ~BIT(gpp_offset);
  648. writel(gpe_en, community->regs + GPI_GPE_EN + gpp * 4);
  649. dev_dbg(pctrl->dev, "%sable wake for pin %u\n", on ? "en" : "dis", pin);
  650. return 0;
  651. }
  652. static irqreturn_t intel_gpio_community_irq_handler(struct intel_pinctrl *pctrl,
  653. const struct intel_community *community)
  654. {
  655. struct gpio_chip *gc = &pctrl->chip;
  656. irqreturn_t ret = IRQ_NONE;
  657. int gpp;
  658. for (gpp = 0; gpp < community->ngpps; gpp++) {
  659. unsigned long pending, enabled, gpp_offset;
  660. pending = readl(community->regs + GPI_IS + gpp * 4);
  661. enabled = readl(community->regs + community->ie_offset +
  662. gpp * 4);
  663. /* Only interrupts that are enabled */
  664. pending &= enabled;
  665. for_each_set_bit(gpp_offset, &pending, community->gpp_size) {
  666. unsigned padno, irq;
  667. /*
  668. * The last group in community can have less pins
  669. * than NPADS_IN_GPP.
  670. */
  671. padno = gpp_offset + gpp * community->gpp_size;
  672. if (padno >= community->npins)
  673. break;
  674. irq = irq_find_mapping(gc->irqdomain,
  675. community->pin_base + padno);
  676. generic_handle_irq(irq);
  677. ret |= IRQ_HANDLED;
  678. }
  679. }
  680. return ret;
  681. }
  682. static irqreturn_t intel_gpio_irq(int irq, void *data)
  683. {
  684. const struct intel_community *community;
  685. struct intel_pinctrl *pctrl = data;
  686. irqreturn_t ret = IRQ_NONE;
  687. int i;
  688. /* Need to check all communities for pending interrupts */
  689. for (i = 0; i < pctrl->ncommunities; i++) {
  690. community = &pctrl->communities[i];
  691. ret |= intel_gpio_community_irq_handler(pctrl, community);
  692. }
  693. return ret;
  694. }
  695. static struct irq_chip intel_gpio_irqchip = {
  696. .name = "intel-gpio",
  697. .irq_ack = intel_gpio_irq_ack,
  698. .irq_mask = intel_gpio_irq_mask,
  699. .irq_unmask = intel_gpio_irq_unmask,
  700. .irq_set_type = intel_gpio_irq_type,
  701. .irq_set_wake = intel_gpio_irq_wake,
  702. };
  703. static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq)
  704. {
  705. int ret;
  706. pctrl->chip = intel_gpio_chip;
  707. pctrl->chip.ngpio = pctrl->soc->npins;
  708. pctrl->chip.label = dev_name(pctrl->dev);
  709. pctrl->chip.parent = pctrl->dev;
  710. pctrl->chip.base = -1;
  711. ret = gpiochip_add_data(&pctrl->chip, pctrl);
  712. if (ret) {
  713. dev_err(pctrl->dev, "failed to register gpiochip\n");
  714. return ret;
  715. }
  716. ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev),
  717. 0, 0, pctrl->soc->npins);
  718. if (ret) {
  719. dev_err(pctrl->dev, "failed to add GPIO pin range\n");
  720. goto fail;
  721. }
  722. /*
  723. * We need to request the interrupt here (instead of providing chip
  724. * to the irq directly) because on some platforms several GPIO
  725. * controllers share the same interrupt line.
  726. */
  727. ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq, IRQF_SHARED,
  728. dev_name(pctrl->dev), pctrl);
  729. if (ret) {
  730. dev_err(pctrl->dev, "failed to request interrupt\n");
  731. goto fail;
  732. }
  733. ret = gpiochip_irqchip_add(&pctrl->chip, &intel_gpio_irqchip, 0,
  734. handle_simple_irq, IRQ_TYPE_NONE);
  735. if (ret) {
  736. dev_err(pctrl->dev, "failed to add irqchip\n");
  737. goto fail;
  738. }
  739. gpiochip_set_chained_irqchip(&pctrl->chip, &intel_gpio_irqchip, irq,
  740. NULL);
  741. return 0;
  742. fail:
  743. gpiochip_remove(&pctrl->chip);
  744. return ret;
  745. }
  746. static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl)
  747. {
  748. #ifdef CONFIG_PM_SLEEP
  749. const struct intel_pinctrl_soc_data *soc = pctrl->soc;
  750. struct intel_community_context *communities;
  751. struct intel_pad_context *pads;
  752. int i;
  753. pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL);
  754. if (!pads)
  755. return -ENOMEM;
  756. communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities,
  757. sizeof(*communities), GFP_KERNEL);
  758. if (!communities)
  759. return -ENOMEM;
  760. for (i = 0; i < pctrl->ncommunities; i++) {
  761. struct intel_community *community = &pctrl->communities[i];
  762. u32 *intmask;
  763. intmask = devm_kcalloc(pctrl->dev, community->ngpps,
  764. sizeof(*intmask), GFP_KERNEL);
  765. if (!intmask)
  766. return -ENOMEM;
  767. communities[i].intmask = intmask;
  768. }
  769. pctrl->context.pads = pads;
  770. pctrl->context.communities = communities;
  771. #endif
  772. return 0;
  773. }
  774. int intel_pinctrl_probe(struct platform_device *pdev,
  775. const struct intel_pinctrl_soc_data *soc_data)
  776. {
  777. struct intel_pinctrl *pctrl;
  778. int i, ret, irq;
  779. if (!soc_data)
  780. return -EINVAL;
  781. pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
  782. if (!pctrl)
  783. return -ENOMEM;
  784. pctrl->dev = &pdev->dev;
  785. pctrl->soc = soc_data;
  786. spin_lock_init(&pctrl->lock);
  787. /*
  788. * Make a copy of the communities which we can use to hold pointers
  789. * to the registers.
  790. */
  791. pctrl->ncommunities = pctrl->soc->ncommunities;
  792. pctrl->communities = devm_kcalloc(&pdev->dev, pctrl->ncommunities,
  793. sizeof(*pctrl->communities), GFP_KERNEL);
  794. if (!pctrl->communities)
  795. return -ENOMEM;
  796. for (i = 0; i < pctrl->ncommunities; i++) {
  797. struct intel_community *community = &pctrl->communities[i];
  798. struct resource *res;
  799. void __iomem *regs;
  800. u32 padbar;
  801. *community = pctrl->soc->communities[i];
  802. res = platform_get_resource(pdev, IORESOURCE_MEM,
  803. community->barno);
  804. regs = devm_ioremap_resource(&pdev->dev, res);
  805. if (IS_ERR(regs))
  806. return PTR_ERR(regs);
  807. /* Read offset of the pad configuration registers */
  808. padbar = readl(regs + PADBAR);
  809. community->regs = regs;
  810. community->pad_regs = regs + padbar;
  811. community->ngpps = DIV_ROUND_UP(community->npins,
  812. community->gpp_size);
  813. }
  814. irq = platform_get_irq(pdev, 0);
  815. if (irq < 0) {
  816. dev_err(&pdev->dev, "failed to get interrupt number\n");
  817. return irq;
  818. }
  819. ret = intel_pinctrl_pm_init(pctrl);
  820. if (ret)
  821. return ret;
  822. pctrl->pctldesc = intel_pinctrl_desc;
  823. pctrl->pctldesc.name = dev_name(&pdev->dev);
  824. pctrl->pctldesc.pins = pctrl->soc->pins;
  825. pctrl->pctldesc.npins = pctrl->soc->npins;
  826. pctrl->pctldev = pinctrl_register(&pctrl->pctldesc, &pdev->dev, pctrl);
  827. if (IS_ERR(pctrl->pctldev)) {
  828. dev_err(&pdev->dev, "failed to register pinctrl driver\n");
  829. return PTR_ERR(pctrl->pctldev);
  830. }
  831. ret = intel_gpio_probe(pctrl, irq);
  832. if (ret) {
  833. pinctrl_unregister(pctrl->pctldev);
  834. return ret;
  835. }
  836. platform_set_drvdata(pdev, pctrl);
  837. return 0;
  838. }
  839. EXPORT_SYMBOL_GPL(intel_pinctrl_probe);
  840. int intel_pinctrl_remove(struct platform_device *pdev)
  841. {
  842. struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
  843. gpiochip_remove(&pctrl->chip);
  844. pinctrl_unregister(pctrl->pctldev);
  845. return 0;
  846. }
  847. EXPORT_SYMBOL_GPL(intel_pinctrl_remove);
  848. #ifdef CONFIG_PM_SLEEP
  849. int intel_pinctrl_suspend(struct device *dev)
  850. {
  851. struct platform_device *pdev = to_platform_device(dev);
  852. struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
  853. struct intel_community_context *communities;
  854. struct intel_pad_context *pads;
  855. int i;
  856. pads = pctrl->context.pads;
  857. for (i = 0; i < pctrl->soc->npins; i++) {
  858. const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
  859. u32 val;
  860. if (!intel_pad_usable(pctrl, desc->number))
  861. continue;
  862. val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0));
  863. pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE;
  864. val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1));
  865. pads[i].padcfg1 = val;
  866. }
  867. communities = pctrl->context.communities;
  868. for (i = 0; i < pctrl->ncommunities; i++) {
  869. struct intel_community *community = &pctrl->communities[i];
  870. void __iomem *base;
  871. unsigned gpp;
  872. base = community->regs + community->ie_offset;
  873. for (gpp = 0; gpp < community->ngpps; gpp++)
  874. communities[i].intmask[gpp] = readl(base + gpp * 4);
  875. }
  876. return 0;
  877. }
  878. EXPORT_SYMBOL_GPL(intel_pinctrl_suspend);
  879. static void intel_gpio_irq_init(struct intel_pinctrl *pctrl)
  880. {
  881. size_t i;
  882. for (i = 0; i < pctrl->ncommunities; i++) {
  883. const struct intel_community *community;
  884. void __iomem *base;
  885. unsigned gpp;
  886. community = &pctrl->communities[i];
  887. base = community->regs;
  888. for (gpp = 0; gpp < community->ngpps; gpp++) {
  889. /* Mask and clear all interrupts */
  890. writel(0, base + community->ie_offset + gpp * 4);
  891. writel(0xffff, base + GPI_IS + gpp * 4);
  892. }
  893. }
  894. }
  895. int intel_pinctrl_resume(struct device *dev)
  896. {
  897. struct platform_device *pdev = to_platform_device(dev);
  898. struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
  899. const struct intel_community_context *communities;
  900. const struct intel_pad_context *pads;
  901. int i;
  902. /* Mask all interrupts */
  903. intel_gpio_irq_init(pctrl);
  904. pads = pctrl->context.pads;
  905. for (i = 0; i < pctrl->soc->npins; i++) {
  906. const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
  907. void __iomem *padcfg;
  908. u32 val;
  909. if (!intel_pad_usable(pctrl, desc->number))
  910. continue;
  911. padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG0);
  912. val = readl(padcfg) & ~PADCFG0_GPIORXSTATE;
  913. if (val != pads[i].padcfg0) {
  914. writel(pads[i].padcfg0, padcfg);
  915. dev_dbg(dev, "restored pin %u padcfg0 %#08x\n",
  916. desc->number, readl(padcfg));
  917. }
  918. padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG1);
  919. val = readl(padcfg);
  920. if (val != pads[i].padcfg1) {
  921. writel(pads[i].padcfg1, padcfg);
  922. dev_dbg(dev, "restored pin %u padcfg1 %#08x\n",
  923. desc->number, readl(padcfg));
  924. }
  925. }
  926. communities = pctrl->context.communities;
  927. for (i = 0; i < pctrl->ncommunities; i++) {
  928. struct intel_community *community = &pctrl->communities[i];
  929. void __iomem *base;
  930. unsigned gpp;
  931. base = community->regs + community->ie_offset;
  932. for (gpp = 0; gpp < community->ngpps; gpp++) {
  933. writel(communities[i].intmask[gpp], base + gpp * 4);
  934. dev_dbg(dev, "restored mask %d/%u %#08x\n", i, gpp,
  935. readl(base + gpp * 4));
  936. }
  937. }
  938. return 0;
  939. }
  940. EXPORT_SYMBOL_GPL(intel_pinctrl_resume);
  941. #endif
  942. MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>");
  943. MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
  944. MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
  945. MODULE_LICENSE("GPL v2");