pinctrl-intel.c 29 KB

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