pinctrl-msm.c 24 KB

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