pinctrl-msm.c 24 KB

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