pinctrl-msm.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  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/driver.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 MAX_NR_TILES 4
  38. #define PS_HOLD_OFFSET 0x820
  39. /**
  40. * struct msm_pinctrl - state for a pinctrl-msm device
  41. * @dev: device handle.
  42. * @pctrl: pinctrl handle.
  43. * @chip: gpiochip handle.
  44. * @restart_nb: restart notifier block.
  45. * @irq: parent irq for the TLMM irq_chip.
  46. * @lock: Spinlock to protect register resources as well
  47. * as msm_pinctrl data structures.
  48. * @enabled_irqs: Bitmap of currently enabled irqs.
  49. * @dual_edge_irqs: Bitmap of irqs that need sw emulated dual edge
  50. * detection.
  51. * @soc; Reference to soc_data of platform specific data.
  52. * @regs: Base addresses for the TLMM tiles.
  53. */
  54. struct msm_pinctrl {
  55. struct device *dev;
  56. struct pinctrl_dev *pctrl;
  57. struct gpio_chip chip;
  58. struct pinctrl_desc desc;
  59. struct notifier_block restart_nb;
  60. struct irq_chip irq_chip;
  61. int irq;
  62. raw_spinlock_t lock;
  63. DECLARE_BITMAP(dual_edge_irqs, MAX_NR_GPIO);
  64. DECLARE_BITMAP(enabled_irqs, MAX_NR_GPIO);
  65. const struct msm_pinctrl_soc_data *soc;
  66. void __iomem *regs[MAX_NR_TILES];
  67. };
  68. #define MSM_ACCESSOR(name) \
  69. static u32 msm_readl_##name(struct msm_pinctrl *pctrl, \
  70. const struct msm_pingroup *g) \
  71. { \
  72. return readl(pctrl->regs[g->tile] + g->name##_reg); \
  73. } \
  74. static void msm_writel_##name(u32 val, struct msm_pinctrl *pctrl, \
  75. const struct msm_pingroup *g) \
  76. { \
  77. writel(val, pctrl->regs[g->tile] + g->name##_reg); \
  78. }
  79. MSM_ACCESSOR(ctl)
  80. MSM_ACCESSOR(io)
  81. MSM_ACCESSOR(intr_cfg)
  82. MSM_ACCESSOR(intr_status)
  83. MSM_ACCESSOR(intr_target)
  84. static int msm_get_groups_count(struct pinctrl_dev *pctldev)
  85. {
  86. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  87. return pctrl->soc->ngroups;
  88. }
  89. static const char *msm_get_group_name(struct pinctrl_dev *pctldev,
  90. unsigned group)
  91. {
  92. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  93. return pctrl->soc->groups[group].name;
  94. }
  95. static int msm_get_group_pins(struct pinctrl_dev *pctldev,
  96. unsigned group,
  97. const unsigned **pins,
  98. unsigned *num_pins)
  99. {
  100. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  101. *pins = pctrl->soc->groups[group].pins;
  102. *num_pins = pctrl->soc->groups[group].npins;
  103. return 0;
  104. }
  105. static const struct pinctrl_ops msm_pinctrl_ops = {
  106. .get_groups_count = msm_get_groups_count,
  107. .get_group_name = msm_get_group_name,
  108. .get_group_pins = msm_get_group_pins,
  109. .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
  110. .dt_free_map = pinctrl_utils_free_map,
  111. };
  112. static int msm_pinmux_request(struct pinctrl_dev *pctldev, unsigned offset)
  113. {
  114. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  115. struct gpio_chip *chip = &pctrl->chip;
  116. return gpiochip_line_is_valid(chip, offset) ? 0 : -EINVAL;
  117. }
  118. static int msm_get_functions_count(struct pinctrl_dev *pctldev)
  119. {
  120. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  121. return pctrl->soc->nfunctions;
  122. }
  123. static const char *msm_get_function_name(struct pinctrl_dev *pctldev,
  124. unsigned function)
  125. {
  126. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  127. return pctrl->soc->functions[function].name;
  128. }
  129. static int msm_get_function_groups(struct pinctrl_dev *pctldev,
  130. unsigned function,
  131. const char * const **groups,
  132. unsigned * const num_groups)
  133. {
  134. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  135. *groups = pctrl->soc->functions[function].groups;
  136. *num_groups = pctrl->soc->functions[function].ngroups;
  137. return 0;
  138. }
  139. static int msm_pinmux_set_mux(struct pinctrl_dev *pctldev,
  140. unsigned function,
  141. unsigned group)
  142. {
  143. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  144. const struct msm_pingroup *g;
  145. unsigned long flags;
  146. u32 val, mask;
  147. int i;
  148. g = &pctrl->soc->groups[group];
  149. mask = GENMASK(g->mux_bit + order_base_2(g->nfuncs) - 1, g->mux_bit);
  150. for (i = 0; i < g->nfuncs; i++) {
  151. if (g->funcs[i] == function)
  152. break;
  153. }
  154. if (WARN_ON(i == g->nfuncs))
  155. return -EINVAL;
  156. raw_spin_lock_irqsave(&pctrl->lock, flags);
  157. val = msm_readl_ctl(pctrl, g);
  158. val &= ~mask;
  159. val |= i << g->mux_bit;
  160. msm_writel_ctl(val, pctrl, g);
  161. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  162. return 0;
  163. }
  164. static int msm_pinmux_request_gpio(struct pinctrl_dev *pctldev,
  165. struct pinctrl_gpio_range *range,
  166. unsigned offset)
  167. {
  168. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  169. const struct msm_pingroup *g = &pctrl->soc->groups[offset];
  170. /* No funcs? Probably ACPI so can't do anything here */
  171. if (!g->nfuncs)
  172. return 0;
  173. /* For now assume function 0 is GPIO because it always is */
  174. return msm_pinmux_set_mux(pctldev, g->funcs[0], offset);
  175. }
  176. static const struct pinmux_ops msm_pinmux_ops = {
  177. .request = msm_pinmux_request,
  178. .get_functions_count = msm_get_functions_count,
  179. .get_function_name = msm_get_function_name,
  180. .get_function_groups = msm_get_function_groups,
  181. .gpio_request_enable = msm_pinmux_request_gpio,
  182. .set_mux = msm_pinmux_set_mux,
  183. };
  184. static int msm_config_reg(struct msm_pinctrl *pctrl,
  185. const struct msm_pingroup *g,
  186. unsigned param,
  187. unsigned *mask,
  188. unsigned *bit)
  189. {
  190. switch (param) {
  191. case PIN_CONFIG_BIAS_DISABLE:
  192. case PIN_CONFIG_BIAS_PULL_DOWN:
  193. case PIN_CONFIG_BIAS_BUS_HOLD:
  194. case PIN_CONFIG_BIAS_PULL_UP:
  195. *bit = g->pull_bit;
  196. *mask = 3;
  197. break;
  198. case PIN_CONFIG_DRIVE_STRENGTH:
  199. *bit = g->drv_bit;
  200. *mask = 7;
  201. break;
  202. case PIN_CONFIG_OUTPUT:
  203. case PIN_CONFIG_INPUT_ENABLE:
  204. *bit = g->oe_bit;
  205. *mask = 1;
  206. break;
  207. default:
  208. return -ENOTSUPP;
  209. }
  210. return 0;
  211. }
  212. #define MSM_NO_PULL 0
  213. #define MSM_PULL_DOWN 1
  214. #define MSM_KEEPER 2
  215. #define MSM_PULL_UP_NO_KEEPER 2
  216. #define MSM_PULL_UP 3
  217. static unsigned msm_regval_to_drive(u32 val)
  218. {
  219. return (val + 1) * 2;
  220. }
  221. static int msm_config_group_get(struct pinctrl_dev *pctldev,
  222. unsigned int group,
  223. unsigned long *config)
  224. {
  225. const struct msm_pingroup *g;
  226. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  227. unsigned param = pinconf_to_config_param(*config);
  228. unsigned mask;
  229. unsigned arg;
  230. unsigned bit;
  231. int ret;
  232. u32 val;
  233. g = &pctrl->soc->groups[group];
  234. ret = msm_config_reg(pctrl, g, param, &mask, &bit);
  235. if (ret < 0)
  236. return ret;
  237. val = msm_readl_ctl(pctrl, g);
  238. arg = (val >> bit) & mask;
  239. /* Convert register value to pinconf value */
  240. switch (param) {
  241. case PIN_CONFIG_BIAS_DISABLE:
  242. if (arg != MSM_NO_PULL)
  243. return -EINVAL;
  244. arg = 1;
  245. break;
  246. case PIN_CONFIG_BIAS_PULL_DOWN:
  247. if (arg != MSM_PULL_DOWN)
  248. return -EINVAL;
  249. arg = 1;
  250. break;
  251. case PIN_CONFIG_BIAS_BUS_HOLD:
  252. if (pctrl->soc->pull_no_keeper)
  253. return -ENOTSUPP;
  254. if (arg != MSM_KEEPER)
  255. return -EINVAL;
  256. arg = 1;
  257. break;
  258. case PIN_CONFIG_BIAS_PULL_UP:
  259. if (pctrl->soc->pull_no_keeper)
  260. arg = arg == MSM_PULL_UP_NO_KEEPER;
  261. else
  262. arg = arg == MSM_PULL_UP;
  263. if (!arg)
  264. return -EINVAL;
  265. break;
  266. case PIN_CONFIG_DRIVE_STRENGTH:
  267. arg = msm_regval_to_drive(arg);
  268. break;
  269. case PIN_CONFIG_OUTPUT:
  270. /* Pin is not output */
  271. if (!arg)
  272. return -EINVAL;
  273. val = msm_readl_io(pctrl, g);
  274. arg = !!(val & BIT(g->in_bit));
  275. break;
  276. case PIN_CONFIG_INPUT_ENABLE:
  277. /* Pin is output */
  278. if (arg)
  279. return -EINVAL;
  280. arg = 1;
  281. break;
  282. default:
  283. return -ENOTSUPP;
  284. }
  285. *config = pinconf_to_config_packed(param, arg);
  286. return 0;
  287. }
  288. static int msm_config_group_set(struct pinctrl_dev *pctldev,
  289. unsigned group,
  290. unsigned long *configs,
  291. unsigned num_configs)
  292. {
  293. const struct msm_pingroup *g;
  294. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  295. unsigned long flags;
  296. unsigned param;
  297. unsigned mask;
  298. unsigned arg;
  299. unsigned bit;
  300. int ret;
  301. u32 val;
  302. int i;
  303. g = &pctrl->soc->groups[group];
  304. for (i = 0; i < num_configs; i++) {
  305. param = pinconf_to_config_param(configs[i]);
  306. arg = pinconf_to_config_argument(configs[i]);
  307. ret = msm_config_reg(pctrl, g, param, &mask, &bit);
  308. if (ret < 0)
  309. return ret;
  310. /* Convert pinconf values to register values */
  311. switch (param) {
  312. case PIN_CONFIG_BIAS_DISABLE:
  313. arg = MSM_NO_PULL;
  314. break;
  315. case PIN_CONFIG_BIAS_PULL_DOWN:
  316. arg = MSM_PULL_DOWN;
  317. break;
  318. case PIN_CONFIG_BIAS_BUS_HOLD:
  319. if (pctrl->soc->pull_no_keeper)
  320. return -ENOTSUPP;
  321. arg = MSM_KEEPER;
  322. break;
  323. case PIN_CONFIG_BIAS_PULL_UP:
  324. if (pctrl->soc->pull_no_keeper)
  325. arg = MSM_PULL_UP_NO_KEEPER;
  326. else
  327. arg = MSM_PULL_UP;
  328. break;
  329. case PIN_CONFIG_DRIVE_STRENGTH:
  330. /* Check for invalid values */
  331. if (arg > 16 || arg < 2 || (arg % 2) != 0)
  332. arg = -1;
  333. else
  334. arg = (arg / 2) - 1;
  335. break;
  336. case PIN_CONFIG_OUTPUT:
  337. /* set output value */
  338. raw_spin_lock_irqsave(&pctrl->lock, flags);
  339. val = msm_readl_io(pctrl, g);
  340. if (arg)
  341. val |= BIT(g->out_bit);
  342. else
  343. val &= ~BIT(g->out_bit);
  344. msm_writel_io(val, pctrl, g);
  345. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  346. /* enable output */
  347. arg = 1;
  348. break;
  349. case PIN_CONFIG_INPUT_ENABLE:
  350. /* disable output */
  351. arg = 0;
  352. break;
  353. default:
  354. dev_err(pctrl->dev, "Unsupported config parameter: %x\n",
  355. param);
  356. return -EINVAL;
  357. }
  358. /* Range-check user-supplied value */
  359. if (arg & ~mask) {
  360. dev_err(pctrl->dev, "config %x: %x is invalid\n", param, arg);
  361. return -EINVAL;
  362. }
  363. raw_spin_lock_irqsave(&pctrl->lock, flags);
  364. val = msm_readl_ctl(pctrl, g);
  365. val &= ~(mask << bit);
  366. val |= arg << bit;
  367. msm_writel_ctl(val, pctrl, g);
  368. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  369. }
  370. return 0;
  371. }
  372. static const struct pinconf_ops msm_pinconf_ops = {
  373. .is_generic = true,
  374. .pin_config_group_get = msm_config_group_get,
  375. .pin_config_group_set = msm_config_group_set,
  376. };
  377. static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  378. {
  379. const struct msm_pingroup *g;
  380. struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
  381. unsigned long flags;
  382. u32 val;
  383. g = &pctrl->soc->groups[offset];
  384. raw_spin_lock_irqsave(&pctrl->lock, flags);
  385. val = msm_readl_ctl(pctrl, g);
  386. val &= ~BIT(g->oe_bit);
  387. msm_writel_ctl(val, pctrl, g);
  388. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  389. return 0;
  390. }
  391. static int msm_gpio_direction_output(struct gpio_chip *chip, unsigned offset, int value)
  392. {
  393. const struct msm_pingroup *g;
  394. struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
  395. unsigned long flags;
  396. u32 val;
  397. g = &pctrl->soc->groups[offset];
  398. raw_spin_lock_irqsave(&pctrl->lock, flags);
  399. val = msm_readl_io(pctrl, g);
  400. if (value)
  401. val |= BIT(g->out_bit);
  402. else
  403. val &= ~BIT(g->out_bit);
  404. msm_writel_io(val, pctrl, g);
  405. val = msm_readl_ctl(pctrl, g);
  406. val |= BIT(g->oe_bit);
  407. msm_writel_ctl(val, pctrl, g);
  408. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  409. return 0;
  410. }
  411. static int msm_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
  412. {
  413. struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
  414. const struct msm_pingroup *g;
  415. u32 val;
  416. g = &pctrl->soc->groups[offset];
  417. val = msm_readl_ctl(pctrl, g);
  418. /* 0 = output, 1 = input */
  419. return val & BIT(g->oe_bit) ? 0 : 1;
  420. }
  421. static int msm_gpio_get(struct gpio_chip *chip, unsigned offset)
  422. {
  423. const struct msm_pingroup *g;
  424. struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
  425. u32 val;
  426. g = &pctrl->soc->groups[offset];
  427. val = msm_readl_io(pctrl, g);
  428. return !!(val & BIT(g->in_bit));
  429. }
  430. static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  431. {
  432. const struct msm_pingroup *g;
  433. struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
  434. unsigned long flags;
  435. u32 val;
  436. g = &pctrl->soc->groups[offset];
  437. raw_spin_lock_irqsave(&pctrl->lock, flags);
  438. val = msm_readl_io(pctrl, g);
  439. if (value)
  440. val |= BIT(g->out_bit);
  441. else
  442. val &= ~BIT(g->out_bit);
  443. msm_writel_io(val, pctrl, g);
  444. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  445. }
  446. #ifdef CONFIG_DEBUG_FS
  447. #include <linux/seq_file.h>
  448. static void msm_gpio_dbg_show_one(struct seq_file *s,
  449. struct pinctrl_dev *pctldev,
  450. struct gpio_chip *chip,
  451. unsigned offset,
  452. unsigned gpio)
  453. {
  454. const struct msm_pingroup *g;
  455. struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
  456. unsigned func;
  457. int is_out;
  458. int drive;
  459. int pull;
  460. int val;
  461. u32 ctl_reg, io_reg;
  462. static const char * const pulls_keeper[] = {
  463. "no pull",
  464. "pull down",
  465. "keeper",
  466. "pull up"
  467. };
  468. static const char * const pulls_no_keeper[] = {
  469. "no pull",
  470. "pull down",
  471. "pull up",
  472. };
  473. if (!gpiochip_line_is_valid(chip, offset))
  474. return;
  475. g = &pctrl->soc->groups[offset];
  476. ctl_reg = msm_readl_ctl(pctrl, g);
  477. io_reg = msm_readl_io(pctrl, g);
  478. is_out = !!(ctl_reg & BIT(g->oe_bit));
  479. func = (ctl_reg >> g->mux_bit) & 7;
  480. drive = (ctl_reg >> g->drv_bit) & 7;
  481. pull = (ctl_reg >> g->pull_bit) & 3;
  482. if (is_out)
  483. val = !!(io_reg & BIT(g->out_bit));
  484. else
  485. val = !!(io_reg & BIT(g->in_bit));
  486. seq_printf(s, " %-8s: %-3s", g->name, is_out ? "out" : "in");
  487. seq_printf(s, " %-4s func%d", val ? "high" : "low", func);
  488. seq_printf(s, " %dmA", msm_regval_to_drive(drive));
  489. if (pctrl->soc->pull_no_keeper)
  490. seq_printf(s, " %s", pulls_no_keeper[pull]);
  491. else
  492. seq_printf(s, " %s", pulls_keeper[pull]);
  493. seq_puts(s, "\n");
  494. }
  495. static void msm_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  496. {
  497. unsigned gpio = chip->base;
  498. unsigned i;
  499. for (i = 0; i < chip->ngpio; i++, gpio++)
  500. msm_gpio_dbg_show_one(s, NULL, chip, i, gpio);
  501. }
  502. #else
  503. #define msm_gpio_dbg_show NULL
  504. #endif
  505. static int msm_gpio_init_valid_mask(struct gpio_chip *chip)
  506. {
  507. struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
  508. int ret;
  509. unsigned int len, i;
  510. unsigned int max_gpios = pctrl->soc->ngpios;
  511. u16 *tmp;
  512. /* The number of GPIOs in the ACPI tables */
  513. len = ret = device_property_read_u16_array(pctrl->dev, "gpios", NULL,
  514. 0);
  515. if (ret < 0)
  516. return 0;
  517. if (ret > max_gpios)
  518. return -EINVAL;
  519. tmp = kmalloc_array(len, sizeof(*tmp), GFP_KERNEL);
  520. if (!tmp)
  521. return -ENOMEM;
  522. ret = device_property_read_u16_array(pctrl->dev, "gpios", tmp, len);
  523. if (ret < 0) {
  524. dev_err(pctrl->dev, "could not read list of GPIOs\n");
  525. goto out;
  526. }
  527. bitmap_zero(chip->valid_mask, max_gpios);
  528. for (i = 0; i < len; i++)
  529. set_bit(tmp[i], chip->valid_mask);
  530. out:
  531. kfree(tmp);
  532. return ret;
  533. }
  534. static const struct gpio_chip msm_gpio_template = {
  535. .direction_input = msm_gpio_direction_input,
  536. .direction_output = msm_gpio_direction_output,
  537. .get_direction = msm_gpio_get_direction,
  538. .get = msm_gpio_get,
  539. .set = msm_gpio_set,
  540. .request = gpiochip_generic_request,
  541. .free = gpiochip_generic_free,
  542. .dbg_show = msm_gpio_dbg_show,
  543. .init_valid_mask = msm_gpio_init_valid_mask,
  544. };
  545. /* For dual-edge interrupts in software, since some hardware has no
  546. * such support:
  547. *
  548. * At appropriate moments, this function may be called to flip the polarity
  549. * settings of both-edge irq lines to try and catch the next edge.
  550. *
  551. * The attempt is considered successful if:
  552. * - the status bit goes high, indicating that an edge was caught, or
  553. * - the input value of the gpio doesn't change during the attempt.
  554. * If the value changes twice during the process, that would cause the first
  555. * test to fail but would force the second, as two opposite
  556. * transitions would cause a detection no matter the polarity setting.
  557. *
  558. * The do-loop tries to sledge-hammer closed the timing hole between
  559. * the initial value-read and the polarity-write - if the line value changes
  560. * during that window, an interrupt is lost, the new polarity setting is
  561. * incorrect, and the first success test will fail, causing a retry.
  562. *
  563. * Algorithm comes from Google's msmgpio driver.
  564. */
  565. static void msm_gpio_update_dual_edge_pos(struct msm_pinctrl *pctrl,
  566. const struct msm_pingroup *g,
  567. struct irq_data *d)
  568. {
  569. int loop_limit = 100;
  570. unsigned val, val2, intstat;
  571. unsigned pol;
  572. do {
  573. val = msm_readl_io(pctrl, g) & BIT(g->in_bit);
  574. pol = msm_readl_intr_cfg(pctrl, g);
  575. pol ^= BIT(g->intr_polarity_bit);
  576. msm_writel_intr_cfg(val, pctrl, g);
  577. val2 = msm_readl_io(pctrl, g) & BIT(g->in_bit);
  578. intstat = msm_readl_intr_status(pctrl, g);
  579. if (intstat || (val == val2))
  580. return;
  581. } while (loop_limit-- > 0);
  582. dev_err(pctrl->dev, "dual-edge irq failed to stabilize, %#08x != %#08x\n",
  583. val, val2);
  584. }
  585. static void msm_gpio_irq_mask(struct irq_data *d)
  586. {
  587. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  588. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  589. const struct msm_pingroup *g;
  590. unsigned long flags;
  591. u32 val;
  592. g = &pctrl->soc->groups[d->hwirq];
  593. raw_spin_lock_irqsave(&pctrl->lock, flags);
  594. val = msm_readl_intr_cfg(pctrl, g);
  595. /*
  596. * There are two bits that control interrupt forwarding to the CPU. The
  597. * RAW_STATUS_EN bit causes the level or edge sensed on the line to be
  598. * latched into the interrupt status register when the hardware detects
  599. * an irq that it's configured for (either edge for edge type or level
  600. * for level type irq). The 'non-raw' status enable bit causes the
  601. * hardware to assert the summary interrupt to the CPU if the latched
  602. * status bit is set. There's a bug though, the edge detection logic
  603. * seems to have a problem where toggling the RAW_STATUS_EN bit may
  604. * cause the status bit to latch spuriously when there isn't any edge
  605. * so we can't touch that bit for edge type irqs and we have to keep
  606. * the bit set anyway so that edges are latched while the line is masked.
  607. *
  608. * To make matters more complicated, leaving the RAW_STATUS_EN bit
  609. * enabled all the time causes level interrupts to re-latch into the
  610. * status register because the level is still present on the line after
  611. * we ack it. We clear the raw status enable bit during mask here and
  612. * set the bit on unmask so the interrupt can't latch into the hardware
  613. * while it's masked.
  614. */
  615. if (irqd_get_trigger_type(d) & IRQ_TYPE_LEVEL_MASK)
  616. val &= ~BIT(g->intr_raw_status_bit);
  617. val &= ~BIT(g->intr_enable_bit);
  618. msm_writel_intr_cfg(val, pctrl, g);
  619. clear_bit(d->hwirq, pctrl->enabled_irqs);
  620. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  621. }
  622. static void msm_gpio_irq_unmask(struct irq_data *d)
  623. {
  624. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  625. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  626. const struct msm_pingroup *g;
  627. unsigned long flags;
  628. u32 val;
  629. g = &pctrl->soc->groups[d->hwirq];
  630. raw_spin_lock_irqsave(&pctrl->lock, flags);
  631. val = msm_readl_intr_cfg(pctrl, g);
  632. val |= BIT(g->intr_raw_status_bit);
  633. val |= BIT(g->intr_enable_bit);
  634. msm_writel_intr_cfg(val, pctrl, g);
  635. set_bit(d->hwirq, pctrl->enabled_irqs);
  636. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  637. }
  638. static void msm_gpio_irq_ack(struct irq_data *d)
  639. {
  640. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  641. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  642. const struct msm_pingroup *g;
  643. unsigned long flags;
  644. u32 val;
  645. g = &pctrl->soc->groups[d->hwirq];
  646. raw_spin_lock_irqsave(&pctrl->lock, flags);
  647. val = msm_readl_intr_status(pctrl, g);
  648. if (g->intr_ack_high)
  649. val |= BIT(g->intr_status_bit);
  650. else
  651. val &= ~BIT(g->intr_status_bit);
  652. msm_writel_intr_status(val, pctrl, g);
  653. if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
  654. msm_gpio_update_dual_edge_pos(pctrl, g, d);
  655. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  656. }
  657. static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  658. {
  659. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  660. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  661. const struct msm_pingroup *g;
  662. unsigned long flags;
  663. u32 val;
  664. g = &pctrl->soc->groups[d->hwirq];
  665. raw_spin_lock_irqsave(&pctrl->lock, flags);
  666. /*
  667. * For hw without possibility of detecting both edges
  668. */
  669. if (g->intr_detection_width == 1 && type == IRQ_TYPE_EDGE_BOTH)
  670. set_bit(d->hwirq, pctrl->dual_edge_irqs);
  671. else
  672. clear_bit(d->hwirq, pctrl->dual_edge_irqs);
  673. /* Route interrupts to application cpu */
  674. val = msm_readl_intr_target(pctrl, g);
  675. val &= ~(7 << g->intr_target_bit);
  676. val |= g->intr_target_kpss_val << g->intr_target_bit;
  677. msm_writel_intr_target(val, pctrl, g);
  678. /* Update configuration for gpio.
  679. * RAW_STATUS_EN is left on for all gpio irqs. Due to the
  680. * internal circuitry of TLMM, toggling the RAW_STATUS
  681. * could cause the INTR_STATUS to be set for EDGE interrupts.
  682. */
  683. val = msm_readl_intr_cfg(pctrl, g);
  684. val |= BIT(g->intr_raw_status_bit);
  685. if (g->intr_detection_width == 2) {
  686. val &= ~(3 << g->intr_detection_bit);
  687. val &= ~(1 << g->intr_polarity_bit);
  688. switch (type) {
  689. case IRQ_TYPE_EDGE_RISING:
  690. val |= 1 << g->intr_detection_bit;
  691. val |= BIT(g->intr_polarity_bit);
  692. break;
  693. case IRQ_TYPE_EDGE_FALLING:
  694. val |= 2 << g->intr_detection_bit;
  695. val |= BIT(g->intr_polarity_bit);
  696. break;
  697. case IRQ_TYPE_EDGE_BOTH:
  698. val |= 3 << g->intr_detection_bit;
  699. val |= BIT(g->intr_polarity_bit);
  700. break;
  701. case IRQ_TYPE_LEVEL_LOW:
  702. break;
  703. case IRQ_TYPE_LEVEL_HIGH:
  704. val |= BIT(g->intr_polarity_bit);
  705. break;
  706. }
  707. } else if (g->intr_detection_width == 1) {
  708. val &= ~(1 << g->intr_detection_bit);
  709. val &= ~(1 << g->intr_polarity_bit);
  710. switch (type) {
  711. case IRQ_TYPE_EDGE_RISING:
  712. val |= BIT(g->intr_detection_bit);
  713. val |= BIT(g->intr_polarity_bit);
  714. break;
  715. case IRQ_TYPE_EDGE_FALLING:
  716. val |= BIT(g->intr_detection_bit);
  717. break;
  718. case IRQ_TYPE_EDGE_BOTH:
  719. val |= BIT(g->intr_detection_bit);
  720. val |= BIT(g->intr_polarity_bit);
  721. break;
  722. case IRQ_TYPE_LEVEL_LOW:
  723. break;
  724. case IRQ_TYPE_LEVEL_HIGH:
  725. val |= BIT(g->intr_polarity_bit);
  726. break;
  727. }
  728. } else {
  729. BUG();
  730. }
  731. msm_writel_intr_cfg(val, pctrl, g);
  732. if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
  733. msm_gpio_update_dual_edge_pos(pctrl, g, d);
  734. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  735. if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
  736. irq_set_handler_locked(d, handle_level_irq);
  737. else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  738. irq_set_handler_locked(d, handle_edge_irq);
  739. return 0;
  740. }
  741. static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
  742. {
  743. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  744. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  745. unsigned long flags;
  746. raw_spin_lock_irqsave(&pctrl->lock, flags);
  747. irq_set_irq_wake(pctrl->irq, on);
  748. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  749. return 0;
  750. }
  751. static int msm_gpio_irq_reqres(struct irq_data *d)
  752. {
  753. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  754. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  755. int ret;
  756. if (!try_module_get(gc->owner))
  757. return -ENODEV;
  758. ret = msm_pinmux_request_gpio(pctrl->pctrl, NULL, d->hwirq);
  759. if (ret)
  760. goto out;
  761. msm_gpio_direction_input(gc, d->hwirq);
  762. if (gpiochip_lock_as_irq(gc, d->hwirq)) {
  763. dev_err(gc->parent,
  764. "unable to lock HW IRQ %lu for IRQ\n",
  765. d->hwirq);
  766. ret = -EINVAL;
  767. goto out;
  768. }
  769. return 0;
  770. out:
  771. module_put(gc->owner);
  772. return ret;
  773. }
  774. static void msm_gpio_irq_relres(struct irq_data *d)
  775. {
  776. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  777. gpiochip_unlock_as_irq(gc, d->hwirq);
  778. module_put(gc->owner);
  779. }
  780. static void msm_gpio_irq_handler(struct irq_desc *desc)
  781. {
  782. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  783. const struct msm_pingroup *g;
  784. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  785. struct irq_chip *chip = irq_desc_get_chip(desc);
  786. int irq_pin;
  787. int handled = 0;
  788. u32 val;
  789. int i;
  790. chained_irq_enter(chip, desc);
  791. /*
  792. * Each pin has it's own IRQ status register, so use
  793. * enabled_irq bitmap to limit the number of reads.
  794. */
  795. for_each_set_bit(i, pctrl->enabled_irqs, pctrl->chip.ngpio) {
  796. g = &pctrl->soc->groups[i];
  797. val = msm_readl_intr_status(pctrl, g);
  798. if (val & BIT(g->intr_status_bit)) {
  799. irq_pin = irq_find_mapping(gc->irq.domain, i);
  800. generic_handle_irq(irq_pin);
  801. handled++;
  802. }
  803. }
  804. /* No interrupts were flagged */
  805. if (handled == 0)
  806. handle_bad_irq(desc);
  807. chained_irq_exit(chip, desc);
  808. }
  809. static bool msm_gpio_needs_valid_mask(struct msm_pinctrl *pctrl)
  810. {
  811. return device_property_read_u16_array(pctrl->dev, "gpios", NULL, 0) > 0;
  812. }
  813. static int msm_gpio_init(struct msm_pinctrl *pctrl)
  814. {
  815. struct gpio_chip *chip;
  816. int ret;
  817. unsigned ngpio = pctrl->soc->ngpios;
  818. if (WARN_ON(ngpio > MAX_NR_GPIO))
  819. return -EINVAL;
  820. chip = &pctrl->chip;
  821. chip->base = -1;
  822. chip->ngpio = ngpio;
  823. chip->label = dev_name(pctrl->dev);
  824. chip->parent = pctrl->dev;
  825. chip->owner = THIS_MODULE;
  826. chip->of_node = pctrl->dev->of_node;
  827. chip->need_valid_mask = msm_gpio_needs_valid_mask(pctrl);
  828. pctrl->irq_chip.name = "msmgpio";
  829. pctrl->irq_chip.irq_mask = msm_gpio_irq_mask;
  830. pctrl->irq_chip.irq_unmask = msm_gpio_irq_unmask;
  831. pctrl->irq_chip.irq_ack = msm_gpio_irq_ack;
  832. pctrl->irq_chip.irq_set_type = msm_gpio_irq_set_type;
  833. pctrl->irq_chip.irq_set_wake = msm_gpio_irq_set_wake;
  834. pctrl->irq_chip.irq_request_resources = msm_gpio_irq_reqres;
  835. pctrl->irq_chip.irq_release_resources = msm_gpio_irq_relres;
  836. ret = gpiochip_add_data(&pctrl->chip, pctrl);
  837. if (ret) {
  838. dev_err(pctrl->dev, "Failed register gpiochip\n");
  839. return ret;
  840. }
  841. /*
  842. * For DeviceTree-supported systems, the gpio core checks the
  843. * pinctrl's device node for the "gpio-ranges" property.
  844. * If it is present, it takes care of adding the pin ranges
  845. * for the driver. In this case the driver can skip ahead.
  846. *
  847. * In order to remain compatible with older, existing DeviceTree
  848. * files which don't set the "gpio-ranges" property or systems that
  849. * utilize ACPI the driver has to call gpiochip_add_pin_range().
  850. */
  851. if (!of_property_read_bool(pctrl->dev->of_node, "gpio-ranges")) {
  852. ret = gpiochip_add_pin_range(&pctrl->chip,
  853. dev_name(pctrl->dev), 0, 0, chip->ngpio);
  854. if (ret) {
  855. dev_err(pctrl->dev, "Failed to add pin range\n");
  856. gpiochip_remove(&pctrl->chip);
  857. return ret;
  858. }
  859. }
  860. ret = gpiochip_irqchip_add(chip,
  861. &pctrl->irq_chip,
  862. 0,
  863. handle_edge_irq,
  864. IRQ_TYPE_NONE);
  865. if (ret) {
  866. dev_err(pctrl->dev, "Failed to add irqchip to gpiochip\n");
  867. gpiochip_remove(&pctrl->chip);
  868. return -ENOSYS;
  869. }
  870. gpiochip_set_chained_irqchip(chip, &pctrl->irq_chip, pctrl->irq,
  871. msm_gpio_irq_handler);
  872. return 0;
  873. }
  874. static int msm_ps_hold_restart(struct notifier_block *nb, unsigned long action,
  875. void *data)
  876. {
  877. struct msm_pinctrl *pctrl = container_of(nb, struct msm_pinctrl, restart_nb);
  878. writel(0, pctrl->regs[0] + PS_HOLD_OFFSET);
  879. mdelay(1000);
  880. return NOTIFY_DONE;
  881. }
  882. static struct msm_pinctrl *poweroff_pctrl;
  883. static void msm_ps_hold_poweroff(void)
  884. {
  885. msm_ps_hold_restart(&poweroff_pctrl->restart_nb, 0, NULL);
  886. }
  887. static void msm_pinctrl_setup_pm_reset(struct msm_pinctrl *pctrl)
  888. {
  889. int i;
  890. const struct msm_function *func = pctrl->soc->functions;
  891. for (i = 0; i < pctrl->soc->nfunctions; i++)
  892. if (!strcmp(func[i].name, "ps_hold")) {
  893. pctrl->restart_nb.notifier_call = msm_ps_hold_restart;
  894. pctrl->restart_nb.priority = 128;
  895. if (register_restart_handler(&pctrl->restart_nb))
  896. dev_err(pctrl->dev,
  897. "failed to setup restart handler.\n");
  898. poweroff_pctrl = pctrl;
  899. pm_power_off = msm_ps_hold_poweroff;
  900. break;
  901. }
  902. }
  903. int msm_pinctrl_probe(struct platform_device *pdev,
  904. const struct msm_pinctrl_soc_data *soc_data)
  905. {
  906. struct msm_pinctrl *pctrl;
  907. struct resource *res;
  908. int ret;
  909. int i;
  910. pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
  911. if (!pctrl)
  912. return -ENOMEM;
  913. pctrl->dev = &pdev->dev;
  914. pctrl->soc = soc_data;
  915. pctrl->chip = msm_gpio_template;
  916. raw_spin_lock_init(&pctrl->lock);
  917. if (soc_data->tiles) {
  918. for (i = 0; i < soc_data->ntiles; i++) {
  919. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  920. soc_data->tiles[i]);
  921. pctrl->regs[i] = devm_ioremap_resource(&pdev->dev, res);
  922. if (IS_ERR(pctrl->regs[i]))
  923. return PTR_ERR(pctrl->regs[i]);
  924. }
  925. } else {
  926. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  927. pctrl->regs[0] = devm_ioremap_resource(&pdev->dev, res);
  928. if (IS_ERR(pctrl->regs[0]))
  929. return PTR_ERR(pctrl->regs[0]);
  930. }
  931. msm_pinctrl_setup_pm_reset(pctrl);
  932. pctrl->irq = platform_get_irq(pdev, 0);
  933. if (pctrl->irq < 0) {
  934. dev_err(&pdev->dev, "No interrupt defined for msmgpio\n");
  935. return pctrl->irq;
  936. }
  937. pctrl->desc.owner = THIS_MODULE;
  938. pctrl->desc.pctlops = &msm_pinctrl_ops;
  939. pctrl->desc.pmxops = &msm_pinmux_ops;
  940. pctrl->desc.confops = &msm_pinconf_ops;
  941. pctrl->desc.name = dev_name(&pdev->dev);
  942. pctrl->desc.pins = pctrl->soc->pins;
  943. pctrl->desc.npins = pctrl->soc->npins;
  944. pctrl->pctrl = devm_pinctrl_register(&pdev->dev, &pctrl->desc, pctrl);
  945. if (IS_ERR(pctrl->pctrl)) {
  946. dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
  947. return PTR_ERR(pctrl->pctrl);
  948. }
  949. ret = msm_gpio_init(pctrl);
  950. if (ret)
  951. return ret;
  952. platform_set_drvdata(pdev, pctrl);
  953. dev_dbg(&pdev->dev, "Probed Qualcomm pinctrl driver\n");
  954. return 0;
  955. }
  956. EXPORT_SYMBOL(msm_pinctrl_probe);
  957. int msm_pinctrl_remove(struct platform_device *pdev)
  958. {
  959. struct msm_pinctrl *pctrl = platform_get_drvdata(pdev);
  960. gpiochip_remove(&pctrl->chip);
  961. unregister_restart_handler(&pctrl->restart_nb);
  962. return 0;
  963. }
  964. EXPORT_SYMBOL(msm_pinctrl_remove);