pinctrl-msm.c 23 KB

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