pinctrl-msm.c 25 KB

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