pinctrl-msm.c 23 KB

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