pinctrl-msm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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_cfg_reg);
  501. val |= BIT(g->intr_enable_bit);
  502. writel(val, pctrl->regs + g->intr_cfg_reg);
  503. set_bit(d->hwirq, pctrl->enabled_irqs);
  504. spin_unlock_irqrestore(&pctrl->lock, flags);
  505. }
  506. static void msm_gpio_irq_ack(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. spin_lock_irqsave(&pctrl->lock, flags);
  515. val = readl(pctrl->regs + g->intr_status_reg);
  516. if (g->intr_ack_high)
  517. val |= BIT(g->intr_status_bit);
  518. else
  519. val &= ~BIT(g->intr_status_bit);
  520. writel(val, pctrl->regs + g->intr_status_reg);
  521. if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
  522. msm_gpio_update_dual_edge_pos(pctrl, g, d);
  523. spin_unlock_irqrestore(&pctrl->lock, flags);
  524. }
  525. static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  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. spin_lock_irqsave(&pctrl->lock, flags);
  534. /*
  535. * For hw without possibility of detecting both edges
  536. */
  537. if (g->intr_detection_width == 1 && type == IRQ_TYPE_EDGE_BOTH)
  538. set_bit(d->hwirq, pctrl->dual_edge_irqs);
  539. else
  540. clear_bit(d->hwirq, pctrl->dual_edge_irqs);
  541. /* Route interrupts to application cpu */
  542. val = readl(pctrl->regs + g->intr_target_reg);
  543. val &= ~(7 << g->intr_target_bit);
  544. val |= g->intr_target_kpss_val << g->intr_target_bit;
  545. writel(val, pctrl->regs + g->intr_target_reg);
  546. /* Update configuration for gpio.
  547. * RAW_STATUS_EN is left on for all gpio irqs. Due to the
  548. * internal circuitry of TLMM, toggling the RAW_STATUS
  549. * could cause the INTR_STATUS to be set for EDGE interrupts.
  550. */
  551. val = readl(pctrl->regs + g->intr_cfg_reg);
  552. val |= BIT(g->intr_raw_status_bit);
  553. if (g->intr_detection_width == 2) {
  554. val &= ~(3 << g->intr_detection_bit);
  555. val &= ~(1 << g->intr_polarity_bit);
  556. switch (type) {
  557. case IRQ_TYPE_EDGE_RISING:
  558. val |= 1 << g->intr_detection_bit;
  559. val |= BIT(g->intr_polarity_bit);
  560. break;
  561. case IRQ_TYPE_EDGE_FALLING:
  562. val |= 2 << g->intr_detection_bit;
  563. val |= BIT(g->intr_polarity_bit);
  564. break;
  565. case IRQ_TYPE_EDGE_BOTH:
  566. val |= 3 << g->intr_detection_bit;
  567. val |= BIT(g->intr_polarity_bit);
  568. break;
  569. case IRQ_TYPE_LEVEL_LOW:
  570. break;
  571. case IRQ_TYPE_LEVEL_HIGH:
  572. val |= BIT(g->intr_polarity_bit);
  573. break;
  574. }
  575. } else if (g->intr_detection_width == 1) {
  576. val &= ~(1 << g->intr_detection_bit);
  577. val &= ~(1 << g->intr_polarity_bit);
  578. switch (type) {
  579. case IRQ_TYPE_EDGE_RISING:
  580. val |= BIT(g->intr_detection_bit);
  581. val |= BIT(g->intr_polarity_bit);
  582. break;
  583. case IRQ_TYPE_EDGE_FALLING:
  584. val |= BIT(g->intr_detection_bit);
  585. break;
  586. case IRQ_TYPE_EDGE_BOTH:
  587. val |= BIT(g->intr_detection_bit);
  588. val |= BIT(g->intr_polarity_bit);
  589. break;
  590. case IRQ_TYPE_LEVEL_LOW:
  591. break;
  592. case IRQ_TYPE_LEVEL_HIGH:
  593. val |= BIT(g->intr_polarity_bit);
  594. break;
  595. }
  596. } else {
  597. BUG();
  598. }
  599. writel(val, pctrl->regs + g->intr_cfg_reg);
  600. if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
  601. msm_gpio_update_dual_edge_pos(pctrl, g, d);
  602. spin_unlock_irqrestore(&pctrl->lock, flags);
  603. if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
  604. irq_set_handler_locked(d, handle_level_irq);
  605. else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  606. irq_set_handler_locked(d, handle_edge_irq);
  607. return 0;
  608. }
  609. static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
  610. {
  611. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  612. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  613. unsigned long flags;
  614. spin_lock_irqsave(&pctrl->lock, flags);
  615. irq_set_irq_wake(pctrl->irq, on);
  616. spin_unlock_irqrestore(&pctrl->lock, flags);
  617. return 0;
  618. }
  619. static struct irq_chip msm_gpio_irq_chip = {
  620. .name = "msmgpio",
  621. .irq_mask = msm_gpio_irq_mask,
  622. .irq_unmask = msm_gpio_irq_unmask,
  623. .irq_ack = msm_gpio_irq_ack,
  624. .irq_set_type = msm_gpio_irq_set_type,
  625. .irq_set_wake = msm_gpio_irq_set_wake,
  626. };
  627. static void msm_gpio_irq_handler(struct irq_desc *desc)
  628. {
  629. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  630. const struct msm_pingroup *g;
  631. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  632. struct irq_chip *chip = irq_desc_get_chip(desc);
  633. int irq_pin;
  634. int handled = 0;
  635. u32 val;
  636. int i;
  637. chained_irq_enter(chip, desc);
  638. /*
  639. * Each pin has it's own IRQ status register, so use
  640. * enabled_irq bitmap to limit the number of reads.
  641. */
  642. for_each_set_bit(i, pctrl->enabled_irqs, pctrl->chip.ngpio) {
  643. g = &pctrl->soc->groups[i];
  644. val = readl(pctrl->regs + g->intr_status_reg);
  645. if (val & BIT(g->intr_status_bit)) {
  646. irq_pin = irq_find_mapping(gc->irqdomain, i);
  647. generic_handle_irq(irq_pin);
  648. handled++;
  649. }
  650. }
  651. /* No interrupts were flagged */
  652. if (handled == 0)
  653. handle_bad_irq(desc);
  654. chained_irq_exit(chip, desc);
  655. }
  656. static int msm_gpio_init(struct msm_pinctrl *pctrl)
  657. {
  658. struct gpio_chip *chip;
  659. int ret;
  660. unsigned ngpio = pctrl->soc->ngpios;
  661. if (WARN_ON(ngpio > MAX_NR_GPIO))
  662. return -EINVAL;
  663. chip = &pctrl->chip;
  664. chip->base = 0;
  665. chip->ngpio = ngpio;
  666. chip->label = dev_name(pctrl->dev);
  667. chip->parent = pctrl->dev;
  668. chip->owner = THIS_MODULE;
  669. chip->of_node = pctrl->dev->of_node;
  670. ret = gpiochip_add_data(&pctrl->chip, pctrl);
  671. if (ret) {
  672. dev_err(pctrl->dev, "Failed register gpiochip\n");
  673. return ret;
  674. }
  675. ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev), 0, 0, chip->ngpio);
  676. if (ret) {
  677. dev_err(pctrl->dev, "Failed to add pin range\n");
  678. gpiochip_remove(&pctrl->chip);
  679. return ret;
  680. }
  681. ret = gpiochip_irqchip_add(chip,
  682. &msm_gpio_irq_chip,
  683. 0,
  684. handle_edge_irq,
  685. IRQ_TYPE_NONE);
  686. if (ret) {
  687. dev_err(pctrl->dev, "Failed to add irqchip to gpiochip\n");
  688. gpiochip_remove(&pctrl->chip);
  689. return -ENOSYS;
  690. }
  691. gpiochip_set_chained_irqchip(chip, &msm_gpio_irq_chip, pctrl->irq,
  692. msm_gpio_irq_handler);
  693. return 0;
  694. }
  695. static int msm_ps_hold_restart(struct notifier_block *nb, unsigned long action,
  696. void *data)
  697. {
  698. struct msm_pinctrl *pctrl = container_of(nb, struct msm_pinctrl, restart_nb);
  699. writel(0, pctrl->regs + PS_HOLD_OFFSET);
  700. mdelay(1000);
  701. return NOTIFY_DONE;
  702. }
  703. static struct msm_pinctrl *poweroff_pctrl;
  704. static void msm_ps_hold_poweroff(void)
  705. {
  706. msm_ps_hold_restart(&poweroff_pctrl->restart_nb, 0, NULL);
  707. }
  708. static void msm_pinctrl_setup_pm_reset(struct msm_pinctrl *pctrl)
  709. {
  710. int i;
  711. const struct msm_function *func = pctrl->soc->functions;
  712. for (i = 0; i < pctrl->soc->nfunctions; i++)
  713. if (!strcmp(func[i].name, "ps_hold")) {
  714. pctrl->restart_nb.notifier_call = msm_ps_hold_restart;
  715. pctrl->restart_nb.priority = 128;
  716. if (register_restart_handler(&pctrl->restart_nb))
  717. dev_err(pctrl->dev,
  718. "failed to setup restart handler.\n");
  719. poweroff_pctrl = pctrl;
  720. pm_power_off = msm_ps_hold_poweroff;
  721. break;
  722. }
  723. }
  724. int msm_pinctrl_probe(struct platform_device *pdev,
  725. const struct msm_pinctrl_soc_data *soc_data)
  726. {
  727. struct msm_pinctrl *pctrl;
  728. struct resource *res;
  729. int ret;
  730. pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
  731. if (!pctrl) {
  732. dev_err(&pdev->dev, "Can't allocate msm_pinctrl\n");
  733. return -ENOMEM;
  734. }
  735. pctrl->dev = &pdev->dev;
  736. pctrl->soc = soc_data;
  737. pctrl->chip = msm_gpio_template;
  738. spin_lock_init(&pctrl->lock);
  739. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  740. pctrl->regs = devm_ioremap_resource(&pdev->dev, res);
  741. if (IS_ERR(pctrl->regs))
  742. return PTR_ERR(pctrl->regs);
  743. msm_pinctrl_setup_pm_reset(pctrl);
  744. pctrl->irq = platform_get_irq(pdev, 0);
  745. if (pctrl->irq < 0) {
  746. dev_err(&pdev->dev, "No interrupt defined for msmgpio\n");
  747. return pctrl->irq;
  748. }
  749. msm_pinctrl_desc.name = dev_name(&pdev->dev);
  750. msm_pinctrl_desc.pins = pctrl->soc->pins;
  751. msm_pinctrl_desc.npins = pctrl->soc->npins;
  752. pctrl->pctrl = devm_pinctrl_register(&pdev->dev, &msm_pinctrl_desc,
  753. pctrl);
  754. if (IS_ERR(pctrl->pctrl)) {
  755. dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
  756. return PTR_ERR(pctrl->pctrl);
  757. }
  758. ret = msm_gpio_init(pctrl);
  759. if (ret)
  760. return ret;
  761. platform_set_drvdata(pdev, pctrl);
  762. dev_dbg(&pdev->dev, "Probed Qualcomm pinctrl driver\n");
  763. return 0;
  764. }
  765. EXPORT_SYMBOL(msm_pinctrl_probe);
  766. int msm_pinctrl_remove(struct platform_device *pdev)
  767. {
  768. struct msm_pinctrl *pctrl = platform_get_drvdata(pdev);
  769. gpiochip_remove(&pctrl->chip);
  770. unregister_restart_handler(&pctrl->restart_nb);
  771. return 0;
  772. }
  773. EXPORT_SYMBOL(msm_pinctrl_remove);