pinctrl-msm.c 23 KB

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