pinctrl-msm.c 27 KB

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