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