pinctrl-intel.c 30 KB

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