pinctrl-intel.c 28 KB

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