pinctrl-msm.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  1. /*
  2. * Copyright (c) 2013, Sony Mobile Communications AB.
  3. * Copyright (c) 2013, The Linux Foundation. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 and
  7. * only version 2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/err.h>
  16. #include <linux/io.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pinctrl/machine.h>
  21. #include <linux/pinctrl/pinctrl.h>
  22. #include <linux/pinctrl/pinmux.h>
  23. #include <linux/pinctrl/pinconf.h>
  24. #include <linux/pinctrl/pinconf-generic.h>
  25. #include <linux/slab.h>
  26. #include <linux/gpio.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/reboot.h>
  30. #include <linux/pm.h>
  31. #include <linux/log2.h>
  32. #include "../core.h"
  33. #include "../pinconf.h"
  34. #include "pinctrl-msm.h"
  35. #include "../pinctrl-utils.h"
  36. #define MAX_NR_GPIO 300
  37. #define PS_HOLD_OFFSET 0x820
  38. /**
  39. * struct msm_pinctrl - state for a pinctrl-msm device
  40. * @dev: device handle.
  41. * @pctrl: pinctrl handle.
  42. * @chip: gpiochip handle.
  43. * @restart_nb: restart notifier block.
  44. * @irq: parent irq for the TLMM irq_chip.
  45. * @lock: Spinlock to protect register resources as well
  46. * as msm_pinctrl data structures.
  47. * @enabled_irqs: Bitmap of currently enabled irqs.
  48. * @dual_edge_irqs: Bitmap of irqs that need sw emulated dual edge
  49. * detection.
  50. * @soc; Reference to soc_data of platform specific data.
  51. * @regs: Base address for the TLMM register map.
  52. */
  53. struct msm_pinctrl {
  54. struct device *dev;
  55. struct pinctrl_dev *pctrl;
  56. struct gpio_chip chip;
  57. struct notifier_block restart_nb;
  58. int irq;
  59. raw_spinlock_t lock;
  60. DECLARE_BITMAP(dual_edge_irqs, MAX_NR_GPIO);
  61. DECLARE_BITMAP(enabled_irqs, MAX_NR_GPIO);
  62. const struct msm_pinctrl_soc_data *soc;
  63. void __iomem *regs;
  64. };
  65. static int msm_get_groups_count(struct pinctrl_dev *pctldev)
  66. {
  67. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  68. return pctrl->soc->ngroups;
  69. }
  70. static const char *msm_get_group_name(struct pinctrl_dev *pctldev,
  71. unsigned group)
  72. {
  73. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  74. return pctrl->soc->groups[group].name;
  75. }
  76. static int msm_get_group_pins(struct pinctrl_dev *pctldev,
  77. unsigned group,
  78. const unsigned **pins,
  79. unsigned *num_pins)
  80. {
  81. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  82. *pins = pctrl->soc->groups[group].pins;
  83. *num_pins = pctrl->soc->groups[group].npins;
  84. return 0;
  85. }
  86. static const struct pinctrl_ops msm_pinctrl_ops = {
  87. .get_groups_count = msm_get_groups_count,
  88. .get_group_name = msm_get_group_name,
  89. .get_group_pins = msm_get_group_pins,
  90. .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
  91. .dt_free_map = pinctrl_utils_free_map,
  92. };
  93. static int msm_pinmux_request(struct pinctrl_dev *pctldev, unsigned offset)
  94. {
  95. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  96. struct gpio_chip *chip = &pctrl->chip;
  97. return gpiochip_line_is_valid(chip, offset) ? 0 : -EINVAL;
  98. }
  99. static int msm_get_functions_count(struct pinctrl_dev *pctldev)
  100. {
  101. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  102. return pctrl->soc->nfunctions;
  103. }
  104. static const char *msm_get_function_name(struct pinctrl_dev *pctldev,
  105. unsigned function)
  106. {
  107. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  108. return pctrl->soc->functions[function].name;
  109. }
  110. static int msm_get_function_groups(struct pinctrl_dev *pctldev,
  111. unsigned function,
  112. const char * const **groups,
  113. unsigned * const num_groups)
  114. {
  115. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  116. *groups = pctrl->soc->functions[function].groups;
  117. *num_groups = pctrl->soc->functions[function].ngroups;
  118. return 0;
  119. }
  120. static int msm_pinmux_set_mux(struct pinctrl_dev *pctldev,
  121. unsigned function,
  122. unsigned group)
  123. {
  124. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  125. const struct msm_pingroup *g;
  126. unsigned long flags;
  127. u32 val, mask;
  128. int i;
  129. g = &pctrl->soc->groups[group];
  130. mask = GENMASK(g->mux_bit + order_base_2(g->nfuncs) - 1, g->mux_bit);
  131. for (i = 0; i < g->nfuncs; i++) {
  132. if (g->funcs[i] == function)
  133. break;
  134. }
  135. if (WARN_ON(i == g->nfuncs))
  136. return -EINVAL;
  137. raw_spin_lock_irqsave(&pctrl->lock, flags);
  138. val = readl(pctrl->regs + g->ctl_reg);
  139. val &= ~mask;
  140. val |= i << g->mux_bit;
  141. writel(val, pctrl->regs + g->ctl_reg);
  142. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  143. return 0;
  144. }
  145. static const struct pinmux_ops msm_pinmux_ops = {
  146. .request = msm_pinmux_request,
  147. .get_functions_count = msm_get_functions_count,
  148. .get_function_name = msm_get_function_name,
  149. .get_function_groups = msm_get_function_groups,
  150. .set_mux = msm_pinmux_set_mux,
  151. };
  152. static int msm_config_reg(struct msm_pinctrl *pctrl,
  153. const struct msm_pingroup *g,
  154. unsigned param,
  155. unsigned *mask,
  156. unsigned *bit)
  157. {
  158. switch (param) {
  159. case PIN_CONFIG_BIAS_DISABLE:
  160. case PIN_CONFIG_BIAS_PULL_DOWN:
  161. case PIN_CONFIG_BIAS_BUS_HOLD:
  162. case PIN_CONFIG_BIAS_PULL_UP:
  163. *bit = g->pull_bit;
  164. *mask = 3;
  165. break;
  166. case PIN_CONFIG_DRIVE_STRENGTH:
  167. *bit = g->drv_bit;
  168. *mask = 7;
  169. break;
  170. case PIN_CONFIG_OUTPUT:
  171. case PIN_CONFIG_INPUT_ENABLE:
  172. *bit = g->oe_bit;
  173. *mask = 1;
  174. break;
  175. default:
  176. return -ENOTSUPP;
  177. }
  178. return 0;
  179. }
  180. #define MSM_NO_PULL 0
  181. #define MSM_PULL_DOWN 1
  182. #define MSM_KEEPER 2
  183. #define MSM_PULL_UP_NO_KEEPER 2
  184. #define MSM_PULL_UP 3
  185. static unsigned msm_regval_to_drive(u32 val)
  186. {
  187. return (val + 1) * 2;
  188. }
  189. static int msm_config_group_get(struct pinctrl_dev *pctldev,
  190. unsigned int group,
  191. unsigned long *config)
  192. {
  193. const struct msm_pingroup *g;
  194. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  195. unsigned param = pinconf_to_config_param(*config);
  196. unsigned mask;
  197. unsigned arg;
  198. unsigned bit;
  199. int ret;
  200. u32 val;
  201. g = &pctrl->soc->groups[group];
  202. ret = msm_config_reg(pctrl, g, param, &mask, &bit);
  203. if (ret < 0)
  204. return ret;
  205. val = readl(pctrl->regs + g->ctl_reg);
  206. arg = (val >> bit) & mask;
  207. /* Convert register value to pinconf value */
  208. switch (param) {
  209. case PIN_CONFIG_BIAS_DISABLE:
  210. arg = arg == MSM_NO_PULL;
  211. break;
  212. case PIN_CONFIG_BIAS_PULL_DOWN:
  213. arg = arg == MSM_PULL_DOWN;
  214. break;
  215. case PIN_CONFIG_BIAS_BUS_HOLD:
  216. if (pctrl->soc->pull_no_keeper)
  217. return -ENOTSUPP;
  218. arg = arg == MSM_KEEPER;
  219. break;
  220. case PIN_CONFIG_BIAS_PULL_UP:
  221. if (pctrl->soc->pull_no_keeper)
  222. arg = arg == MSM_PULL_UP_NO_KEEPER;
  223. else
  224. arg = arg == MSM_PULL_UP;
  225. break;
  226. case PIN_CONFIG_DRIVE_STRENGTH:
  227. arg = msm_regval_to_drive(arg);
  228. break;
  229. case PIN_CONFIG_OUTPUT:
  230. /* Pin is not output */
  231. if (!arg)
  232. return -EINVAL;
  233. val = readl(pctrl->regs + g->io_reg);
  234. arg = !!(val & BIT(g->in_bit));
  235. break;
  236. case PIN_CONFIG_INPUT_ENABLE:
  237. /* Pin is output */
  238. if (arg)
  239. return -EINVAL;
  240. arg = 1;
  241. break;
  242. default:
  243. return -ENOTSUPP;
  244. }
  245. *config = pinconf_to_config_packed(param, arg);
  246. return 0;
  247. }
  248. static int msm_config_group_set(struct pinctrl_dev *pctldev,
  249. unsigned group,
  250. unsigned long *configs,
  251. unsigned num_configs)
  252. {
  253. const struct msm_pingroup *g;
  254. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  255. unsigned long flags;
  256. unsigned param;
  257. unsigned mask;
  258. unsigned arg;
  259. unsigned bit;
  260. int ret;
  261. u32 val;
  262. int i;
  263. g = &pctrl->soc->groups[group];
  264. for (i = 0; i < num_configs; i++) {
  265. param = pinconf_to_config_param(configs[i]);
  266. arg = pinconf_to_config_argument(configs[i]);
  267. ret = msm_config_reg(pctrl, g, param, &mask, &bit);
  268. if (ret < 0)
  269. return ret;
  270. /* Convert pinconf values to register values */
  271. switch (param) {
  272. case PIN_CONFIG_BIAS_DISABLE:
  273. arg = MSM_NO_PULL;
  274. break;
  275. case PIN_CONFIG_BIAS_PULL_DOWN:
  276. arg = MSM_PULL_DOWN;
  277. break;
  278. case PIN_CONFIG_BIAS_BUS_HOLD:
  279. if (pctrl->soc->pull_no_keeper)
  280. return -ENOTSUPP;
  281. arg = MSM_KEEPER;
  282. break;
  283. case PIN_CONFIG_BIAS_PULL_UP:
  284. if (pctrl->soc->pull_no_keeper)
  285. arg = MSM_PULL_UP_NO_KEEPER;
  286. else
  287. arg = MSM_PULL_UP;
  288. break;
  289. case PIN_CONFIG_DRIVE_STRENGTH:
  290. /* Check for invalid values */
  291. if (arg > 16 || arg < 2 || (arg % 2) != 0)
  292. arg = -1;
  293. else
  294. arg = (arg / 2) - 1;
  295. break;
  296. case PIN_CONFIG_OUTPUT:
  297. /* set output value */
  298. raw_spin_lock_irqsave(&pctrl->lock, flags);
  299. val = readl(pctrl->regs + g->io_reg);
  300. if (arg)
  301. val |= BIT(g->out_bit);
  302. else
  303. val &= ~BIT(g->out_bit);
  304. writel(val, pctrl->regs + g->io_reg);
  305. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  306. /* enable output */
  307. arg = 1;
  308. break;
  309. case PIN_CONFIG_INPUT_ENABLE:
  310. /* disable output */
  311. arg = 0;
  312. break;
  313. default:
  314. dev_err(pctrl->dev, "Unsupported config parameter: %x\n",
  315. param);
  316. return -EINVAL;
  317. }
  318. /* Range-check user-supplied value */
  319. if (arg & ~mask) {
  320. dev_err(pctrl->dev, "config %x: %x is invalid\n", param, arg);
  321. return -EINVAL;
  322. }
  323. raw_spin_lock_irqsave(&pctrl->lock, flags);
  324. val = readl(pctrl->regs + g->ctl_reg);
  325. val &= ~(mask << bit);
  326. val |= arg << bit;
  327. writel(val, pctrl->regs + g->ctl_reg);
  328. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  329. }
  330. return 0;
  331. }
  332. static const struct pinconf_ops msm_pinconf_ops = {
  333. .is_generic = true,
  334. .pin_config_group_get = msm_config_group_get,
  335. .pin_config_group_set = msm_config_group_set,
  336. };
  337. static struct pinctrl_desc msm_pinctrl_desc = {
  338. .pctlops = &msm_pinctrl_ops,
  339. .pmxops = &msm_pinmux_ops,
  340. .confops = &msm_pinconf_ops,
  341. .owner = THIS_MODULE,
  342. };
  343. static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  344. {
  345. const struct msm_pingroup *g;
  346. struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
  347. unsigned long flags;
  348. u32 val;
  349. g = &pctrl->soc->groups[offset];
  350. raw_spin_lock_irqsave(&pctrl->lock, flags);
  351. val = readl(pctrl->regs + g->ctl_reg);
  352. val &= ~BIT(g->oe_bit);
  353. writel(val, pctrl->regs + g->ctl_reg);
  354. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  355. return 0;
  356. }
  357. static int msm_gpio_direction_output(struct gpio_chip *chip, unsigned offset, int value)
  358. {
  359. const struct msm_pingroup *g;
  360. struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
  361. unsigned long flags;
  362. u32 val;
  363. g = &pctrl->soc->groups[offset];
  364. raw_spin_lock_irqsave(&pctrl->lock, flags);
  365. val = readl(pctrl->regs + g->io_reg);
  366. if (value)
  367. val |= BIT(g->out_bit);
  368. else
  369. val &= ~BIT(g->out_bit);
  370. writel(val, pctrl->regs + g->io_reg);
  371. val = readl(pctrl->regs + g->ctl_reg);
  372. val |= BIT(g->oe_bit);
  373. writel(val, pctrl->regs + g->ctl_reg);
  374. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  375. return 0;
  376. }
  377. static int msm_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
  378. {
  379. struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
  380. const struct msm_pingroup *g;
  381. u32 val;
  382. g = &pctrl->soc->groups[offset];
  383. val = readl(pctrl->regs + g->ctl_reg);
  384. /* 0 = output, 1 = input */
  385. return val & BIT(g->oe_bit) ? 0 : 1;
  386. }
  387. static int msm_gpio_get(struct gpio_chip *chip, unsigned offset)
  388. {
  389. const struct msm_pingroup *g;
  390. struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
  391. u32 val;
  392. g = &pctrl->soc->groups[offset];
  393. val = readl(pctrl->regs + g->io_reg);
  394. return !!(val & BIT(g->in_bit));
  395. }
  396. static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  397. {
  398. const struct msm_pingroup *g;
  399. struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
  400. unsigned long flags;
  401. u32 val;
  402. g = &pctrl->soc->groups[offset];
  403. raw_spin_lock_irqsave(&pctrl->lock, flags);
  404. val = readl(pctrl->regs + g->io_reg);
  405. if (value)
  406. val |= BIT(g->out_bit);
  407. else
  408. val &= ~BIT(g->out_bit);
  409. writel(val, pctrl->regs + g->io_reg);
  410. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  411. }
  412. #ifdef CONFIG_DEBUG_FS
  413. #include <linux/seq_file.h>
  414. static void msm_gpio_dbg_show_one(struct seq_file *s,
  415. struct pinctrl_dev *pctldev,
  416. struct gpio_chip *chip,
  417. unsigned offset,
  418. unsigned gpio)
  419. {
  420. const struct msm_pingroup *g;
  421. struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
  422. unsigned func;
  423. int is_out;
  424. int drive;
  425. int pull;
  426. u32 ctl_reg;
  427. static const char * const pulls[] = {
  428. "no pull",
  429. "pull down",
  430. "keeper",
  431. "pull up"
  432. };
  433. if (!gpiochip_line_is_valid(chip, offset))
  434. return;
  435. g = &pctrl->soc->groups[offset];
  436. ctl_reg = readl(pctrl->regs + g->ctl_reg);
  437. is_out = !!(ctl_reg & BIT(g->oe_bit));
  438. func = (ctl_reg >> g->mux_bit) & 7;
  439. drive = (ctl_reg >> g->drv_bit) & 7;
  440. pull = (ctl_reg >> g->pull_bit) & 3;
  441. seq_printf(s, " %-8s: %-3s %d", g->name, is_out ? "out" : "in", func);
  442. seq_printf(s, " %dmA", msm_regval_to_drive(drive));
  443. seq_printf(s, " %s", pulls[pull]);
  444. seq_puts(s, "\n");
  445. }
  446. static void msm_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  447. {
  448. unsigned gpio = chip->base;
  449. unsigned i;
  450. for (i = 0; i < chip->ngpio; i++, gpio++)
  451. msm_gpio_dbg_show_one(s, NULL, chip, i, gpio);
  452. }
  453. #else
  454. #define msm_gpio_dbg_show NULL
  455. #endif
  456. static const struct gpio_chip msm_gpio_template = {
  457. .direction_input = msm_gpio_direction_input,
  458. .direction_output = msm_gpio_direction_output,
  459. .get_direction = msm_gpio_get_direction,
  460. .get = msm_gpio_get,
  461. .set = msm_gpio_set,
  462. .request = gpiochip_generic_request,
  463. .free = gpiochip_generic_free,
  464. .dbg_show = msm_gpio_dbg_show,
  465. };
  466. /* For dual-edge interrupts in software, since some hardware has no
  467. * such support:
  468. *
  469. * At appropriate moments, this function may be called to flip the polarity
  470. * settings of both-edge irq lines to try and catch the next edge.
  471. *
  472. * The attempt is considered successful if:
  473. * - the status bit goes high, indicating that an edge was caught, or
  474. * - the input value of the gpio doesn't change during the attempt.
  475. * If the value changes twice during the process, that would cause the first
  476. * test to fail but would force the second, as two opposite
  477. * transitions would cause a detection no matter the polarity setting.
  478. *
  479. * The do-loop tries to sledge-hammer closed the timing hole between
  480. * the initial value-read and the polarity-write - if the line value changes
  481. * during that window, an interrupt is lost, the new polarity setting is
  482. * incorrect, and the first success test will fail, causing a retry.
  483. *
  484. * Algorithm comes from Google's msmgpio driver.
  485. */
  486. static void msm_gpio_update_dual_edge_pos(struct msm_pinctrl *pctrl,
  487. const struct msm_pingroup *g,
  488. struct irq_data *d)
  489. {
  490. int loop_limit = 100;
  491. unsigned val, val2, intstat;
  492. unsigned pol;
  493. do {
  494. val = readl(pctrl->regs + g->io_reg) & BIT(g->in_bit);
  495. pol = readl(pctrl->regs + g->intr_cfg_reg);
  496. pol ^= BIT(g->intr_polarity_bit);
  497. writel(pol, pctrl->regs + g->intr_cfg_reg);
  498. val2 = readl(pctrl->regs + g->io_reg) & BIT(g->in_bit);
  499. intstat = readl(pctrl->regs + g->intr_status_reg);
  500. if (intstat || (val == val2))
  501. return;
  502. } while (loop_limit-- > 0);
  503. dev_err(pctrl->dev, "dual-edge irq failed to stabilize, %#08x != %#08x\n",
  504. val, val2);
  505. }
  506. static void msm_gpio_irq_mask(struct irq_data *d)
  507. {
  508. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  509. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  510. const struct msm_pingroup *g;
  511. unsigned long flags;
  512. u32 val;
  513. g = &pctrl->soc->groups[d->hwirq];
  514. raw_spin_lock_irqsave(&pctrl->lock, flags);
  515. val = readl(pctrl->regs + g->intr_cfg_reg);
  516. val &= ~BIT(g->intr_enable_bit);
  517. writel(val, pctrl->regs + g->intr_cfg_reg);
  518. clear_bit(d->hwirq, pctrl->enabled_irqs);
  519. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  520. }
  521. static void msm_gpio_irq_unmask(struct irq_data *d)
  522. {
  523. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  524. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  525. const struct msm_pingroup *g;
  526. unsigned long flags;
  527. u32 val;
  528. g = &pctrl->soc->groups[d->hwirq];
  529. raw_spin_lock_irqsave(&pctrl->lock, flags);
  530. val = readl(pctrl->regs + g->intr_cfg_reg);
  531. val |= BIT(g->intr_enable_bit);
  532. writel(val, pctrl->regs + g->intr_cfg_reg);
  533. set_bit(d->hwirq, pctrl->enabled_irqs);
  534. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  535. }
  536. static void msm_gpio_irq_ack(struct irq_data *d)
  537. {
  538. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  539. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  540. const struct msm_pingroup *g;
  541. unsigned long flags;
  542. u32 val;
  543. g = &pctrl->soc->groups[d->hwirq];
  544. raw_spin_lock_irqsave(&pctrl->lock, flags);
  545. val = readl(pctrl->regs + g->intr_status_reg);
  546. if (g->intr_ack_high)
  547. val |= BIT(g->intr_status_bit);
  548. else
  549. val &= ~BIT(g->intr_status_bit);
  550. writel(val, pctrl->regs + g->intr_status_reg);
  551. if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
  552. msm_gpio_update_dual_edge_pos(pctrl, g, d);
  553. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  554. }
  555. static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  556. {
  557. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  558. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  559. const struct msm_pingroup *g;
  560. unsigned long flags;
  561. u32 val;
  562. g = &pctrl->soc->groups[d->hwirq];
  563. raw_spin_lock_irqsave(&pctrl->lock, flags);
  564. /*
  565. * For hw without possibility of detecting both edges
  566. */
  567. if (g->intr_detection_width == 1 && type == IRQ_TYPE_EDGE_BOTH)
  568. set_bit(d->hwirq, pctrl->dual_edge_irqs);
  569. else
  570. clear_bit(d->hwirq, pctrl->dual_edge_irqs);
  571. /* Route interrupts to application cpu */
  572. val = readl(pctrl->regs + g->intr_target_reg);
  573. val &= ~(7 << g->intr_target_bit);
  574. val |= g->intr_target_kpss_val << g->intr_target_bit;
  575. writel(val, pctrl->regs + g->intr_target_reg);
  576. /* Update configuration for gpio.
  577. * RAW_STATUS_EN is left on for all gpio irqs. Due to the
  578. * internal circuitry of TLMM, toggling the RAW_STATUS
  579. * could cause the INTR_STATUS to be set for EDGE interrupts.
  580. */
  581. val = readl(pctrl->regs + g->intr_cfg_reg);
  582. val |= BIT(g->intr_raw_status_bit);
  583. if (g->intr_detection_width == 2) {
  584. val &= ~(3 << g->intr_detection_bit);
  585. val &= ~(1 << g->intr_polarity_bit);
  586. switch (type) {
  587. case IRQ_TYPE_EDGE_RISING:
  588. val |= 1 << g->intr_detection_bit;
  589. val |= BIT(g->intr_polarity_bit);
  590. break;
  591. case IRQ_TYPE_EDGE_FALLING:
  592. val |= 2 << g->intr_detection_bit;
  593. val |= BIT(g->intr_polarity_bit);
  594. break;
  595. case IRQ_TYPE_EDGE_BOTH:
  596. val |= 3 << g->intr_detection_bit;
  597. val |= BIT(g->intr_polarity_bit);
  598. break;
  599. case IRQ_TYPE_LEVEL_LOW:
  600. break;
  601. case IRQ_TYPE_LEVEL_HIGH:
  602. val |= BIT(g->intr_polarity_bit);
  603. break;
  604. }
  605. } else if (g->intr_detection_width == 1) {
  606. val &= ~(1 << g->intr_detection_bit);
  607. val &= ~(1 << g->intr_polarity_bit);
  608. switch (type) {
  609. case IRQ_TYPE_EDGE_RISING:
  610. val |= BIT(g->intr_detection_bit);
  611. val |= BIT(g->intr_polarity_bit);
  612. break;
  613. case IRQ_TYPE_EDGE_FALLING:
  614. val |= BIT(g->intr_detection_bit);
  615. break;
  616. case IRQ_TYPE_EDGE_BOTH:
  617. val |= BIT(g->intr_detection_bit);
  618. val |= BIT(g->intr_polarity_bit);
  619. break;
  620. case IRQ_TYPE_LEVEL_LOW:
  621. break;
  622. case IRQ_TYPE_LEVEL_HIGH:
  623. val |= BIT(g->intr_polarity_bit);
  624. break;
  625. }
  626. } else {
  627. BUG();
  628. }
  629. writel(val, pctrl->regs + g->intr_cfg_reg);
  630. if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
  631. msm_gpio_update_dual_edge_pos(pctrl, g, d);
  632. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  633. if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
  634. irq_set_handler_locked(d, handle_level_irq);
  635. else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  636. irq_set_handler_locked(d, handle_edge_irq);
  637. return 0;
  638. }
  639. static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
  640. {
  641. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  642. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  643. unsigned long flags;
  644. raw_spin_lock_irqsave(&pctrl->lock, flags);
  645. irq_set_irq_wake(pctrl->irq, on);
  646. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  647. return 0;
  648. }
  649. static struct irq_chip msm_gpio_irq_chip = {
  650. .name = "msmgpio",
  651. .irq_mask = msm_gpio_irq_mask,
  652. .irq_unmask = msm_gpio_irq_unmask,
  653. .irq_ack = msm_gpio_irq_ack,
  654. .irq_set_type = msm_gpio_irq_set_type,
  655. .irq_set_wake = msm_gpio_irq_set_wake,
  656. };
  657. static void msm_gpio_irq_handler(struct irq_desc *desc)
  658. {
  659. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  660. const struct msm_pingroup *g;
  661. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  662. struct irq_chip *chip = irq_desc_get_chip(desc);
  663. int irq_pin;
  664. int handled = 0;
  665. u32 val;
  666. int i;
  667. chained_irq_enter(chip, desc);
  668. /*
  669. * Each pin has it's own IRQ status register, so use
  670. * enabled_irq bitmap to limit the number of reads.
  671. */
  672. for_each_set_bit(i, pctrl->enabled_irqs, pctrl->chip.ngpio) {
  673. g = &pctrl->soc->groups[i];
  674. val = readl(pctrl->regs + g->intr_status_reg);
  675. if (val & BIT(g->intr_status_bit)) {
  676. irq_pin = irq_find_mapping(gc->irq.domain, i);
  677. generic_handle_irq(irq_pin);
  678. handled++;
  679. }
  680. }
  681. /* No interrupts were flagged */
  682. if (handled == 0)
  683. handle_bad_irq(desc);
  684. chained_irq_exit(chip, desc);
  685. }
  686. static int msm_gpio_init_valid_mask(struct gpio_chip *chip,
  687. struct msm_pinctrl *pctrl)
  688. {
  689. int ret;
  690. unsigned int len, i;
  691. unsigned int max_gpios = pctrl->soc->ngpios;
  692. u16 *tmp;
  693. /* The number of GPIOs in the ACPI tables */
  694. len = ret = device_property_read_u16_array(pctrl->dev, "gpios", NULL, 0);
  695. if (ret < 0)
  696. return 0;
  697. if (ret > max_gpios)
  698. return -EINVAL;
  699. tmp = kmalloc_array(len, sizeof(*tmp), GFP_KERNEL);
  700. if (!tmp)
  701. return -ENOMEM;
  702. ret = device_property_read_u16_array(pctrl->dev, "gpios", tmp, len);
  703. if (ret < 0) {
  704. dev_err(pctrl->dev, "could not read list of GPIOs\n");
  705. goto out;
  706. }
  707. bitmap_zero(chip->valid_mask, max_gpios);
  708. for (i = 0; i < len; i++)
  709. set_bit(tmp[i], chip->valid_mask);
  710. out:
  711. kfree(tmp);
  712. return ret;
  713. }
  714. static bool msm_gpio_needs_valid_mask(struct msm_pinctrl *pctrl)
  715. {
  716. return device_property_read_u16_array(pctrl->dev, "gpios", NULL, 0) > 0;
  717. }
  718. static int msm_gpio_init(struct msm_pinctrl *pctrl)
  719. {
  720. struct gpio_chip *chip;
  721. int ret;
  722. unsigned ngpio = pctrl->soc->ngpios;
  723. if (WARN_ON(ngpio > MAX_NR_GPIO))
  724. return -EINVAL;
  725. chip = &pctrl->chip;
  726. chip->base = -1;
  727. chip->ngpio = ngpio;
  728. chip->label = dev_name(pctrl->dev);
  729. chip->parent = pctrl->dev;
  730. chip->owner = THIS_MODULE;
  731. chip->of_node = pctrl->dev->of_node;
  732. chip->need_valid_mask = msm_gpio_needs_valid_mask(pctrl);
  733. ret = gpiochip_add_data(&pctrl->chip, pctrl);
  734. if (ret) {
  735. dev_err(pctrl->dev, "Failed register gpiochip\n");
  736. return ret;
  737. }
  738. ret = msm_gpio_init_valid_mask(chip, pctrl);
  739. if (ret) {
  740. dev_err(pctrl->dev, "Failed to setup irq valid bits\n");
  741. gpiochip_remove(&pctrl->chip);
  742. return ret;
  743. }
  744. ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev), 0, 0, chip->ngpio);
  745. if (ret) {
  746. dev_err(pctrl->dev, "Failed to add pin range\n");
  747. gpiochip_remove(&pctrl->chip);
  748. return ret;
  749. }
  750. ret = gpiochip_irqchip_add(chip,
  751. &msm_gpio_irq_chip,
  752. 0,
  753. handle_edge_irq,
  754. IRQ_TYPE_NONE);
  755. if (ret) {
  756. dev_err(pctrl->dev, "Failed to add irqchip to gpiochip\n");
  757. gpiochip_remove(&pctrl->chip);
  758. return -ENOSYS;
  759. }
  760. gpiochip_set_chained_irqchip(chip, &msm_gpio_irq_chip, pctrl->irq,
  761. msm_gpio_irq_handler);
  762. return 0;
  763. }
  764. static int msm_ps_hold_restart(struct notifier_block *nb, unsigned long action,
  765. void *data)
  766. {
  767. struct msm_pinctrl *pctrl = container_of(nb, struct msm_pinctrl, restart_nb);
  768. writel(0, pctrl->regs + PS_HOLD_OFFSET);
  769. mdelay(1000);
  770. return NOTIFY_DONE;
  771. }
  772. static struct msm_pinctrl *poweroff_pctrl;
  773. static void msm_ps_hold_poweroff(void)
  774. {
  775. msm_ps_hold_restart(&poweroff_pctrl->restart_nb, 0, NULL);
  776. }
  777. static void msm_pinctrl_setup_pm_reset(struct msm_pinctrl *pctrl)
  778. {
  779. int i;
  780. const struct msm_function *func = pctrl->soc->functions;
  781. for (i = 0; i < pctrl->soc->nfunctions; i++)
  782. if (!strcmp(func[i].name, "ps_hold")) {
  783. pctrl->restart_nb.notifier_call = msm_ps_hold_restart;
  784. pctrl->restart_nb.priority = 128;
  785. if (register_restart_handler(&pctrl->restart_nb))
  786. dev_err(pctrl->dev,
  787. "failed to setup restart handler.\n");
  788. poweroff_pctrl = pctrl;
  789. pm_power_off = msm_ps_hold_poweroff;
  790. break;
  791. }
  792. }
  793. int msm_pinctrl_probe(struct platform_device *pdev,
  794. const struct msm_pinctrl_soc_data *soc_data)
  795. {
  796. struct msm_pinctrl *pctrl;
  797. struct resource *res;
  798. int ret;
  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. pctrl->chip = msm_gpio_template;
  805. raw_spin_lock_init(&pctrl->lock);
  806. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  807. pctrl->regs = devm_ioremap_resource(&pdev->dev, res);
  808. if (IS_ERR(pctrl->regs))
  809. return PTR_ERR(pctrl->regs);
  810. msm_pinctrl_setup_pm_reset(pctrl);
  811. pctrl->irq = platform_get_irq(pdev, 0);
  812. if (pctrl->irq < 0) {
  813. dev_err(&pdev->dev, "No interrupt defined for msmgpio\n");
  814. return pctrl->irq;
  815. }
  816. msm_pinctrl_desc.name = dev_name(&pdev->dev);
  817. msm_pinctrl_desc.pins = pctrl->soc->pins;
  818. msm_pinctrl_desc.npins = pctrl->soc->npins;
  819. pctrl->pctrl = devm_pinctrl_register(&pdev->dev, &msm_pinctrl_desc,
  820. pctrl);
  821. if (IS_ERR(pctrl->pctrl)) {
  822. dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
  823. return PTR_ERR(pctrl->pctrl);
  824. }
  825. ret = msm_gpio_init(pctrl);
  826. if (ret)
  827. return ret;
  828. platform_set_drvdata(pdev, pctrl);
  829. dev_dbg(&pdev->dev, "Probed Qualcomm pinctrl driver\n");
  830. return 0;
  831. }
  832. EXPORT_SYMBOL(msm_pinctrl_probe);
  833. int msm_pinctrl_remove(struct platform_device *pdev)
  834. {
  835. struct msm_pinctrl *pctrl = platform_get_drvdata(pdev);
  836. gpiochip_remove(&pctrl->chip);
  837. unregister_restart_handler(&pctrl->restart_nb);
  838. return 0;
  839. }
  840. EXPORT_SYMBOL(msm_pinctrl_remove);