pinctrl-msm.c 23 KB

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