pinctrl-msm.c 24 KB

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