pinctrl-exynos.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // Exynos specific support for Samsung pinctrl/gpiolib driver with eint support.
  4. //
  5. // Copyright (c) 2012 Samsung Electronics Co., Ltd.
  6. // http://www.samsung.com
  7. // Copyright (c) 2012 Linaro Ltd
  8. // http://www.linaro.org
  9. //
  10. // Author: Thomas Abraham <thomas.ab@samsung.com>
  11. //
  12. // This file contains the Samsung Exynos specific information required by the
  13. // the Samsung pinctrl/gpiolib driver. It also includes the implementation of
  14. // external gpio and wakeup interrupt support.
  15. #include <linux/device.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/irqdomain.h>
  18. #include <linux/irq.h>
  19. #include <linux/irqchip/chained_irq.h>
  20. #include <linux/of.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/slab.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/regmap.h>
  25. #include <linux/err.h>
  26. #include <linux/soc/samsung/exynos-pmu.h>
  27. #include <linux/soc/samsung/exynos-regs-pmu.h>
  28. #include <dt-bindings/pinctrl/samsung.h>
  29. #include "pinctrl-samsung.h"
  30. #include "pinctrl-exynos.h"
  31. struct exynos_irq_chip {
  32. struct irq_chip chip;
  33. u32 eint_con;
  34. u32 eint_mask;
  35. u32 eint_pend;
  36. u32 eint_wake_mask_value;
  37. u32 eint_wake_mask_reg;
  38. void (*set_eint_wakeup_mask)(struct samsung_pinctrl_drv_data *drvdata,
  39. struct exynos_irq_chip *irq_chip);
  40. };
  41. static inline struct exynos_irq_chip *to_exynos_irq_chip(struct irq_chip *chip)
  42. {
  43. return container_of(chip, struct exynos_irq_chip, chip);
  44. }
  45. static void exynos_irq_mask(struct irq_data *irqd)
  46. {
  47. struct irq_chip *chip = irq_data_get_irq_chip(irqd);
  48. struct exynos_irq_chip *our_chip = to_exynos_irq_chip(chip);
  49. struct samsung_pin_bank *bank = irq_data_get_irq_chip_data(irqd);
  50. unsigned long reg_mask = our_chip->eint_mask + bank->eint_offset;
  51. unsigned int mask;
  52. unsigned long flags;
  53. spin_lock_irqsave(&bank->slock, flags);
  54. mask = readl(bank->eint_base + reg_mask);
  55. mask |= 1 << irqd->hwirq;
  56. writel(mask, bank->eint_base + reg_mask);
  57. spin_unlock_irqrestore(&bank->slock, flags);
  58. }
  59. static void exynos_irq_ack(struct irq_data *irqd)
  60. {
  61. struct irq_chip *chip = irq_data_get_irq_chip(irqd);
  62. struct exynos_irq_chip *our_chip = to_exynos_irq_chip(chip);
  63. struct samsung_pin_bank *bank = irq_data_get_irq_chip_data(irqd);
  64. unsigned long reg_pend = our_chip->eint_pend + bank->eint_offset;
  65. writel(1 << irqd->hwirq, bank->eint_base + reg_pend);
  66. }
  67. static void exynos_irq_unmask(struct irq_data *irqd)
  68. {
  69. struct irq_chip *chip = irq_data_get_irq_chip(irqd);
  70. struct exynos_irq_chip *our_chip = to_exynos_irq_chip(chip);
  71. struct samsung_pin_bank *bank = irq_data_get_irq_chip_data(irqd);
  72. unsigned long reg_mask = our_chip->eint_mask + bank->eint_offset;
  73. unsigned int mask;
  74. unsigned long flags;
  75. /*
  76. * Ack level interrupts right before unmask
  77. *
  78. * If we don't do this we'll get a double-interrupt. Level triggered
  79. * interrupts must not fire an interrupt if the level is not
  80. * _currently_ active, even if it was active while the interrupt was
  81. * masked.
  82. */
  83. if (irqd_get_trigger_type(irqd) & IRQ_TYPE_LEVEL_MASK)
  84. exynos_irq_ack(irqd);
  85. spin_lock_irqsave(&bank->slock, flags);
  86. mask = readl(bank->eint_base + reg_mask);
  87. mask &= ~(1 << irqd->hwirq);
  88. writel(mask, bank->eint_base + reg_mask);
  89. spin_unlock_irqrestore(&bank->slock, flags);
  90. }
  91. static int exynos_irq_set_type(struct irq_data *irqd, unsigned int type)
  92. {
  93. struct irq_chip *chip = irq_data_get_irq_chip(irqd);
  94. struct exynos_irq_chip *our_chip = to_exynos_irq_chip(chip);
  95. struct samsung_pin_bank *bank = irq_data_get_irq_chip_data(irqd);
  96. unsigned int shift = EXYNOS_EINT_CON_LEN * irqd->hwirq;
  97. unsigned int con, trig_type;
  98. unsigned long reg_con = our_chip->eint_con + bank->eint_offset;
  99. switch (type) {
  100. case IRQ_TYPE_EDGE_RISING:
  101. trig_type = EXYNOS_EINT_EDGE_RISING;
  102. break;
  103. case IRQ_TYPE_EDGE_FALLING:
  104. trig_type = EXYNOS_EINT_EDGE_FALLING;
  105. break;
  106. case IRQ_TYPE_EDGE_BOTH:
  107. trig_type = EXYNOS_EINT_EDGE_BOTH;
  108. break;
  109. case IRQ_TYPE_LEVEL_HIGH:
  110. trig_type = EXYNOS_EINT_LEVEL_HIGH;
  111. break;
  112. case IRQ_TYPE_LEVEL_LOW:
  113. trig_type = EXYNOS_EINT_LEVEL_LOW;
  114. break;
  115. default:
  116. pr_err("unsupported external interrupt type\n");
  117. return -EINVAL;
  118. }
  119. if (type & IRQ_TYPE_EDGE_BOTH)
  120. irq_set_handler_locked(irqd, handle_edge_irq);
  121. else
  122. irq_set_handler_locked(irqd, handle_level_irq);
  123. con = readl(bank->eint_base + reg_con);
  124. con &= ~(EXYNOS_EINT_CON_MASK << shift);
  125. con |= trig_type << shift;
  126. writel(con, bank->eint_base + reg_con);
  127. return 0;
  128. }
  129. static int exynos_irq_request_resources(struct irq_data *irqd)
  130. {
  131. struct samsung_pin_bank *bank = irq_data_get_irq_chip_data(irqd);
  132. const struct samsung_pin_bank_type *bank_type = bank->type;
  133. unsigned long reg_con, flags;
  134. unsigned int shift, mask, con;
  135. int ret;
  136. ret = gpiochip_lock_as_irq(&bank->gpio_chip, irqd->hwirq);
  137. if (ret) {
  138. dev_err(bank->gpio_chip.parent,
  139. "unable to lock pin %s-%lu IRQ\n",
  140. bank->name, irqd->hwirq);
  141. return ret;
  142. }
  143. reg_con = bank->pctl_offset + bank_type->reg_offset[PINCFG_TYPE_FUNC];
  144. shift = irqd->hwirq * bank_type->fld_width[PINCFG_TYPE_FUNC];
  145. mask = (1 << bank_type->fld_width[PINCFG_TYPE_FUNC]) - 1;
  146. spin_lock_irqsave(&bank->slock, flags);
  147. con = readl(bank->pctl_base + reg_con);
  148. con &= ~(mask << shift);
  149. con |= EXYNOS_PIN_FUNC_EINT << shift;
  150. writel(con, bank->pctl_base + reg_con);
  151. spin_unlock_irqrestore(&bank->slock, flags);
  152. return 0;
  153. }
  154. static void exynos_irq_release_resources(struct irq_data *irqd)
  155. {
  156. struct samsung_pin_bank *bank = irq_data_get_irq_chip_data(irqd);
  157. const struct samsung_pin_bank_type *bank_type = bank->type;
  158. unsigned long reg_con, flags;
  159. unsigned int shift, mask, con;
  160. reg_con = bank->pctl_offset + bank_type->reg_offset[PINCFG_TYPE_FUNC];
  161. shift = irqd->hwirq * bank_type->fld_width[PINCFG_TYPE_FUNC];
  162. mask = (1 << bank_type->fld_width[PINCFG_TYPE_FUNC]) - 1;
  163. spin_lock_irqsave(&bank->slock, flags);
  164. con = readl(bank->pctl_base + reg_con);
  165. con &= ~(mask << shift);
  166. con |= EXYNOS_PIN_FUNC_INPUT << shift;
  167. writel(con, bank->pctl_base + reg_con);
  168. spin_unlock_irqrestore(&bank->slock, flags);
  169. gpiochip_unlock_as_irq(&bank->gpio_chip, irqd->hwirq);
  170. }
  171. /*
  172. * irq_chip for gpio interrupts.
  173. */
  174. static struct exynos_irq_chip exynos_gpio_irq_chip = {
  175. .chip = {
  176. .name = "exynos_gpio_irq_chip",
  177. .irq_unmask = exynos_irq_unmask,
  178. .irq_mask = exynos_irq_mask,
  179. .irq_ack = exynos_irq_ack,
  180. .irq_set_type = exynos_irq_set_type,
  181. .irq_request_resources = exynos_irq_request_resources,
  182. .irq_release_resources = exynos_irq_release_resources,
  183. },
  184. .eint_con = EXYNOS_GPIO_ECON_OFFSET,
  185. .eint_mask = EXYNOS_GPIO_EMASK_OFFSET,
  186. .eint_pend = EXYNOS_GPIO_EPEND_OFFSET,
  187. /* eint_wake_mask_value not used */
  188. };
  189. static int exynos_eint_irq_map(struct irq_domain *h, unsigned int virq,
  190. irq_hw_number_t hw)
  191. {
  192. struct samsung_pin_bank *b = h->host_data;
  193. irq_set_chip_data(virq, b);
  194. irq_set_chip_and_handler(virq, &b->irq_chip->chip,
  195. handle_level_irq);
  196. return 0;
  197. }
  198. /*
  199. * irq domain callbacks for external gpio and wakeup interrupt controllers.
  200. */
  201. static const struct irq_domain_ops exynos_eint_irqd_ops = {
  202. .map = exynos_eint_irq_map,
  203. .xlate = irq_domain_xlate_twocell,
  204. };
  205. static irqreturn_t exynos_eint_gpio_irq(int irq, void *data)
  206. {
  207. struct samsung_pinctrl_drv_data *d = data;
  208. struct samsung_pin_bank *bank = d->pin_banks;
  209. unsigned int svc, group, pin, virq;
  210. svc = readl(bank->eint_base + EXYNOS_SVC_OFFSET);
  211. group = EXYNOS_SVC_GROUP(svc);
  212. pin = svc & EXYNOS_SVC_NUM_MASK;
  213. if (!group)
  214. return IRQ_HANDLED;
  215. bank += (group - 1);
  216. virq = irq_linear_revmap(bank->irq_domain, pin);
  217. if (!virq)
  218. return IRQ_NONE;
  219. generic_handle_irq(virq);
  220. return IRQ_HANDLED;
  221. }
  222. struct exynos_eint_gpio_save {
  223. u32 eint_con;
  224. u32 eint_fltcon0;
  225. u32 eint_fltcon1;
  226. u32 eint_mask;
  227. };
  228. /*
  229. * exynos_eint_gpio_init() - setup handling of external gpio interrupts.
  230. * @d: driver data of samsung pinctrl driver.
  231. */
  232. int exynos_eint_gpio_init(struct samsung_pinctrl_drv_data *d)
  233. {
  234. struct samsung_pin_bank *bank;
  235. struct device *dev = d->dev;
  236. int ret;
  237. int i;
  238. if (!d->irq) {
  239. dev_err(dev, "irq number not available\n");
  240. return -EINVAL;
  241. }
  242. ret = devm_request_irq(dev, d->irq, exynos_eint_gpio_irq,
  243. 0, dev_name(dev), d);
  244. if (ret) {
  245. dev_err(dev, "irq request failed\n");
  246. return -ENXIO;
  247. }
  248. bank = d->pin_banks;
  249. for (i = 0; i < d->nr_banks; ++i, ++bank) {
  250. if (bank->eint_type != EINT_TYPE_GPIO)
  251. continue;
  252. bank->irq_domain = irq_domain_add_linear(bank->of_node,
  253. bank->nr_pins, &exynos_eint_irqd_ops, bank);
  254. if (!bank->irq_domain) {
  255. dev_err(dev, "gpio irq domain add failed\n");
  256. ret = -ENXIO;
  257. goto err_domains;
  258. }
  259. bank->soc_priv = devm_kzalloc(d->dev,
  260. sizeof(struct exynos_eint_gpio_save), GFP_KERNEL);
  261. if (!bank->soc_priv) {
  262. irq_domain_remove(bank->irq_domain);
  263. ret = -ENOMEM;
  264. goto err_domains;
  265. }
  266. bank->irq_chip = &exynos_gpio_irq_chip;
  267. }
  268. return 0;
  269. err_domains:
  270. for (--i, --bank; i >= 0; --i, --bank) {
  271. if (bank->eint_type != EINT_TYPE_GPIO)
  272. continue;
  273. irq_domain_remove(bank->irq_domain);
  274. }
  275. return ret;
  276. }
  277. static u32 exynos_eint_wake_mask = 0xffffffff;
  278. u32 exynos_get_eint_wake_mask(void)
  279. {
  280. return exynos_eint_wake_mask;
  281. }
  282. static int exynos_wkup_irq_set_wake(struct irq_data *irqd, unsigned int on)
  283. {
  284. struct irq_chip *chip = irq_data_get_irq_chip(irqd);
  285. struct exynos_irq_chip *our_chip = to_exynos_irq_chip(chip);
  286. struct samsung_pin_bank *bank = irq_data_get_irq_chip_data(irqd);
  287. unsigned long bit = 1UL << (2 * bank->eint_offset + irqd->hwirq);
  288. pr_info("wake %s for irq %d\n", on ? "enabled" : "disabled", irqd->irq);
  289. if (!on)
  290. exynos_eint_wake_mask |= bit;
  291. else
  292. exynos_eint_wake_mask &= ~bit;
  293. our_chip->eint_wake_mask_value = exynos_eint_wake_mask;
  294. return 0;
  295. }
  296. static void
  297. exynos_pinctrl_set_eint_wakeup_mask(struct samsung_pinctrl_drv_data *drvdata,
  298. struct exynos_irq_chip *irq_chip)
  299. {
  300. struct regmap *pmu_regs;
  301. if (!drvdata->retention_ctrl || !drvdata->retention_ctrl->priv) {
  302. dev_warn(drvdata->dev,
  303. "No retention data configured bank with external wakeup interrupt. Wake-up mask will not be set.\n");
  304. return;
  305. }
  306. pmu_regs = drvdata->retention_ctrl->priv;
  307. dev_info(drvdata->dev,
  308. "Setting external wakeup interrupt mask: 0x%x\n",
  309. irq_chip->eint_wake_mask_value);
  310. regmap_write(pmu_regs, irq_chip->eint_wake_mask_reg,
  311. irq_chip->eint_wake_mask_value);
  312. }
  313. static void
  314. s5pv210_pinctrl_set_eint_wakeup_mask(struct samsung_pinctrl_drv_data *drvdata,
  315. struct exynos_irq_chip *irq_chip)
  316. {
  317. void __iomem *clk_base;
  318. if (!drvdata->retention_ctrl || !drvdata->retention_ctrl->priv) {
  319. dev_warn(drvdata->dev,
  320. "No retention data configured bank with external wakeup interrupt. Wake-up mask will not be set.\n");
  321. return;
  322. }
  323. clk_base = (void __iomem *) drvdata->retention_ctrl->priv;
  324. __raw_writel(irq_chip->eint_wake_mask_value,
  325. clk_base + irq_chip->eint_wake_mask_reg);
  326. }
  327. /*
  328. * irq_chip for wakeup interrupts
  329. */
  330. static const struct exynos_irq_chip s5pv210_wkup_irq_chip __initconst = {
  331. .chip = {
  332. .name = "s5pv210_wkup_irq_chip",
  333. .irq_unmask = exynos_irq_unmask,
  334. .irq_mask = exynos_irq_mask,
  335. .irq_ack = exynos_irq_ack,
  336. .irq_set_type = exynos_irq_set_type,
  337. .irq_set_wake = exynos_wkup_irq_set_wake,
  338. .irq_request_resources = exynos_irq_request_resources,
  339. .irq_release_resources = exynos_irq_release_resources,
  340. },
  341. .eint_con = EXYNOS_WKUP_ECON_OFFSET,
  342. .eint_mask = EXYNOS_WKUP_EMASK_OFFSET,
  343. .eint_pend = EXYNOS_WKUP_EPEND_OFFSET,
  344. .eint_wake_mask_value = EXYNOS_EINT_WAKEUP_MASK_DISABLED,
  345. /* Only differences with exynos4210_wkup_irq_chip: */
  346. .eint_wake_mask_reg = S5PV210_EINT_WAKEUP_MASK,
  347. .set_eint_wakeup_mask = s5pv210_pinctrl_set_eint_wakeup_mask,
  348. };
  349. static const struct exynos_irq_chip exynos4210_wkup_irq_chip __initconst = {
  350. .chip = {
  351. .name = "exynos4210_wkup_irq_chip",
  352. .irq_unmask = exynos_irq_unmask,
  353. .irq_mask = exynos_irq_mask,
  354. .irq_ack = exynos_irq_ack,
  355. .irq_set_type = exynos_irq_set_type,
  356. .irq_set_wake = exynos_wkup_irq_set_wake,
  357. .irq_request_resources = exynos_irq_request_resources,
  358. .irq_release_resources = exynos_irq_release_resources,
  359. },
  360. .eint_con = EXYNOS_WKUP_ECON_OFFSET,
  361. .eint_mask = EXYNOS_WKUP_EMASK_OFFSET,
  362. .eint_pend = EXYNOS_WKUP_EPEND_OFFSET,
  363. .eint_wake_mask_value = EXYNOS_EINT_WAKEUP_MASK_DISABLED,
  364. .eint_wake_mask_reg = EXYNOS_EINT_WAKEUP_MASK,
  365. .set_eint_wakeup_mask = exynos_pinctrl_set_eint_wakeup_mask,
  366. };
  367. static const struct exynos_irq_chip exynos7_wkup_irq_chip __initconst = {
  368. .chip = {
  369. .name = "exynos7_wkup_irq_chip",
  370. .irq_unmask = exynos_irq_unmask,
  371. .irq_mask = exynos_irq_mask,
  372. .irq_ack = exynos_irq_ack,
  373. .irq_set_type = exynos_irq_set_type,
  374. .irq_set_wake = exynos_wkup_irq_set_wake,
  375. .irq_request_resources = exynos_irq_request_resources,
  376. .irq_release_resources = exynos_irq_release_resources,
  377. },
  378. .eint_con = EXYNOS7_WKUP_ECON_OFFSET,
  379. .eint_mask = EXYNOS7_WKUP_EMASK_OFFSET,
  380. .eint_pend = EXYNOS7_WKUP_EPEND_OFFSET,
  381. .eint_wake_mask_value = EXYNOS_EINT_WAKEUP_MASK_DISABLED,
  382. .eint_wake_mask_reg = EXYNOS5433_EINT_WAKEUP_MASK,
  383. .set_eint_wakeup_mask = exynos_pinctrl_set_eint_wakeup_mask,
  384. };
  385. /* list of external wakeup controllers supported */
  386. static const struct of_device_id exynos_wkup_irq_ids[] = {
  387. { .compatible = "samsung,s5pv210-wakeup-eint",
  388. .data = &s5pv210_wkup_irq_chip },
  389. { .compatible = "samsung,exynos4210-wakeup-eint",
  390. .data = &exynos4210_wkup_irq_chip },
  391. { .compatible = "samsung,exynos7-wakeup-eint",
  392. .data = &exynos7_wkup_irq_chip },
  393. { }
  394. };
  395. /* interrupt handler for wakeup interrupts 0..15 */
  396. static void exynos_irq_eint0_15(struct irq_desc *desc)
  397. {
  398. struct exynos_weint_data *eintd = irq_desc_get_handler_data(desc);
  399. struct samsung_pin_bank *bank = eintd->bank;
  400. struct irq_chip *chip = irq_desc_get_chip(desc);
  401. int eint_irq;
  402. chained_irq_enter(chip, desc);
  403. eint_irq = irq_linear_revmap(bank->irq_domain, eintd->irq);
  404. generic_handle_irq(eint_irq);
  405. chained_irq_exit(chip, desc);
  406. }
  407. static inline void exynos_irq_demux_eint(unsigned int pend,
  408. struct irq_domain *domain)
  409. {
  410. unsigned int irq;
  411. while (pend) {
  412. irq = fls(pend) - 1;
  413. generic_handle_irq(irq_find_mapping(domain, irq));
  414. pend &= ~(1 << irq);
  415. }
  416. }
  417. /* interrupt handler for wakeup interrupt 16 */
  418. static void exynos_irq_demux_eint16_31(struct irq_desc *desc)
  419. {
  420. struct irq_chip *chip = irq_desc_get_chip(desc);
  421. struct exynos_muxed_weint_data *eintd = irq_desc_get_handler_data(desc);
  422. unsigned int pend;
  423. unsigned int mask;
  424. int i;
  425. chained_irq_enter(chip, desc);
  426. for (i = 0; i < eintd->nr_banks; ++i) {
  427. struct samsung_pin_bank *b = eintd->banks[i];
  428. pend = readl(b->eint_base + b->irq_chip->eint_pend
  429. + b->eint_offset);
  430. mask = readl(b->eint_base + b->irq_chip->eint_mask
  431. + b->eint_offset);
  432. exynos_irq_demux_eint(pend & ~mask, b->irq_domain);
  433. }
  434. chained_irq_exit(chip, desc);
  435. }
  436. /*
  437. * exynos_eint_wkup_init() - setup handling of external wakeup interrupts.
  438. * @d: driver data of samsung pinctrl driver.
  439. */
  440. int exynos_eint_wkup_init(struct samsung_pinctrl_drv_data *d)
  441. {
  442. struct device *dev = d->dev;
  443. struct device_node *wkup_np = NULL;
  444. struct device_node *np;
  445. struct samsung_pin_bank *bank;
  446. struct exynos_weint_data *weint_data;
  447. struct exynos_muxed_weint_data *muxed_data;
  448. struct exynos_irq_chip *irq_chip;
  449. unsigned int muxed_banks = 0;
  450. unsigned int i;
  451. int idx, irq;
  452. for_each_child_of_node(dev->of_node, np) {
  453. const struct of_device_id *match;
  454. match = of_match_node(exynos_wkup_irq_ids, np);
  455. if (match) {
  456. irq_chip = kmemdup(match->data,
  457. sizeof(*irq_chip), GFP_KERNEL);
  458. if (!irq_chip) {
  459. of_node_put(np);
  460. return -ENOMEM;
  461. }
  462. wkup_np = np;
  463. break;
  464. }
  465. }
  466. if (!wkup_np)
  467. return -ENODEV;
  468. bank = d->pin_banks;
  469. for (i = 0; i < d->nr_banks; ++i, ++bank) {
  470. if (bank->eint_type != EINT_TYPE_WKUP)
  471. continue;
  472. bank->irq_domain = irq_domain_add_linear(bank->of_node,
  473. bank->nr_pins, &exynos_eint_irqd_ops, bank);
  474. if (!bank->irq_domain) {
  475. dev_err(dev, "wkup irq domain add failed\n");
  476. of_node_put(wkup_np);
  477. return -ENXIO;
  478. }
  479. bank->irq_chip = irq_chip;
  480. if (!of_find_property(bank->of_node, "interrupts", NULL)) {
  481. bank->eint_type = EINT_TYPE_WKUP_MUX;
  482. ++muxed_banks;
  483. continue;
  484. }
  485. weint_data = devm_kcalloc(dev,
  486. bank->nr_pins, sizeof(*weint_data),
  487. GFP_KERNEL);
  488. if (!weint_data) {
  489. of_node_put(wkup_np);
  490. return -ENOMEM;
  491. }
  492. for (idx = 0; idx < bank->nr_pins; ++idx) {
  493. irq = irq_of_parse_and_map(bank->of_node, idx);
  494. if (!irq) {
  495. dev_err(dev, "irq number for eint-%s-%d not found\n",
  496. bank->name, idx);
  497. continue;
  498. }
  499. weint_data[idx].irq = idx;
  500. weint_data[idx].bank = bank;
  501. irq_set_chained_handler_and_data(irq,
  502. exynos_irq_eint0_15,
  503. &weint_data[idx]);
  504. }
  505. }
  506. if (!muxed_banks) {
  507. of_node_put(wkup_np);
  508. return 0;
  509. }
  510. irq = irq_of_parse_and_map(wkup_np, 0);
  511. of_node_put(wkup_np);
  512. if (!irq) {
  513. dev_err(dev, "irq number for muxed EINTs not found\n");
  514. return 0;
  515. }
  516. muxed_data = devm_kzalloc(dev, sizeof(*muxed_data)
  517. + muxed_banks*sizeof(struct samsung_pin_bank *), GFP_KERNEL);
  518. if (!muxed_data)
  519. return -ENOMEM;
  520. irq_set_chained_handler_and_data(irq, exynos_irq_demux_eint16_31,
  521. muxed_data);
  522. bank = d->pin_banks;
  523. idx = 0;
  524. for (i = 0; i < d->nr_banks; ++i, ++bank) {
  525. if (bank->eint_type != EINT_TYPE_WKUP_MUX)
  526. continue;
  527. muxed_data->banks[idx++] = bank;
  528. }
  529. muxed_data->nr_banks = muxed_banks;
  530. return 0;
  531. }
  532. static void exynos_pinctrl_suspend_bank(
  533. struct samsung_pinctrl_drv_data *drvdata,
  534. struct samsung_pin_bank *bank)
  535. {
  536. struct exynos_eint_gpio_save *save = bank->soc_priv;
  537. void __iomem *regs = bank->eint_base;
  538. save->eint_con = readl(regs + EXYNOS_GPIO_ECON_OFFSET
  539. + bank->eint_offset);
  540. save->eint_fltcon0 = readl(regs + EXYNOS_GPIO_EFLTCON_OFFSET
  541. + 2 * bank->eint_offset);
  542. save->eint_fltcon1 = readl(regs + EXYNOS_GPIO_EFLTCON_OFFSET
  543. + 2 * bank->eint_offset + 4);
  544. save->eint_mask = readl(regs + bank->irq_chip->eint_mask
  545. + bank->eint_offset);
  546. pr_debug("%s: save con %#010x\n", bank->name, save->eint_con);
  547. pr_debug("%s: save fltcon0 %#010x\n", bank->name, save->eint_fltcon0);
  548. pr_debug("%s: save fltcon1 %#010x\n", bank->name, save->eint_fltcon1);
  549. pr_debug("%s: save mask %#010x\n", bank->name, save->eint_mask);
  550. }
  551. void exynos_pinctrl_suspend(struct samsung_pinctrl_drv_data *drvdata)
  552. {
  553. struct samsung_pin_bank *bank = drvdata->pin_banks;
  554. struct exynos_irq_chip *irq_chip = NULL;
  555. int i;
  556. for (i = 0; i < drvdata->nr_banks; ++i, ++bank) {
  557. if (bank->eint_type == EINT_TYPE_GPIO)
  558. exynos_pinctrl_suspend_bank(drvdata, bank);
  559. else if (bank->eint_type == EINT_TYPE_WKUP) {
  560. if (!irq_chip) {
  561. irq_chip = bank->irq_chip;
  562. irq_chip->set_eint_wakeup_mask(drvdata,
  563. irq_chip);
  564. } else if (bank->irq_chip != irq_chip) {
  565. dev_warn(drvdata->dev,
  566. "More than one external wakeup interrupt chip configured (bank: %s). This is not supported by hardware nor by driver.\n",
  567. bank->name);
  568. }
  569. }
  570. }
  571. }
  572. static void exynos_pinctrl_resume_bank(
  573. struct samsung_pinctrl_drv_data *drvdata,
  574. struct samsung_pin_bank *bank)
  575. {
  576. struct exynos_eint_gpio_save *save = bank->soc_priv;
  577. void __iomem *regs = bank->eint_base;
  578. pr_debug("%s: con %#010x => %#010x\n", bank->name,
  579. readl(regs + EXYNOS_GPIO_ECON_OFFSET
  580. + bank->eint_offset), save->eint_con);
  581. pr_debug("%s: fltcon0 %#010x => %#010x\n", bank->name,
  582. readl(regs + EXYNOS_GPIO_EFLTCON_OFFSET
  583. + 2 * bank->eint_offset), save->eint_fltcon0);
  584. pr_debug("%s: fltcon1 %#010x => %#010x\n", bank->name,
  585. readl(regs + EXYNOS_GPIO_EFLTCON_OFFSET
  586. + 2 * bank->eint_offset + 4), save->eint_fltcon1);
  587. pr_debug("%s: mask %#010x => %#010x\n", bank->name,
  588. readl(regs + bank->irq_chip->eint_mask
  589. + bank->eint_offset), save->eint_mask);
  590. writel(save->eint_con, regs + EXYNOS_GPIO_ECON_OFFSET
  591. + bank->eint_offset);
  592. writel(save->eint_fltcon0, regs + EXYNOS_GPIO_EFLTCON_OFFSET
  593. + 2 * bank->eint_offset);
  594. writel(save->eint_fltcon1, regs + EXYNOS_GPIO_EFLTCON_OFFSET
  595. + 2 * bank->eint_offset + 4);
  596. writel(save->eint_mask, regs + bank->irq_chip->eint_mask
  597. + bank->eint_offset);
  598. }
  599. void exynos_pinctrl_resume(struct samsung_pinctrl_drv_data *drvdata)
  600. {
  601. struct samsung_pin_bank *bank = drvdata->pin_banks;
  602. int i;
  603. for (i = 0; i < drvdata->nr_banks; ++i, ++bank)
  604. if (bank->eint_type == EINT_TYPE_GPIO)
  605. exynos_pinctrl_resume_bank(drvdata, bank);
  606. }
  607. static void exynos_retention_enable(struct samsung_pinctrl_drv_data *drvdata)
  608. {
  609. if (drvdata->retention_ctrl->refcnt)
  610. atomic_inc(drvdata->retention_ctrl->refcnt);
  611. }
  612. static void exynos_retention_disable(struct samsung_pinctrl_drv_data *drvdata)
  613. {
  614. struct samsung_retention_ctrl *ctrl = drvdata->retention_ctrl;
  615. struct regmap *pmu_regs = ctrl->priv;
  616. int i;
  617. if (ctrl->refcnt && !atomic_dec_and_test(ctrl->refcnt))
  618. return;
  619. for (i = 0; i < ctrl->nr_regs; i++)
  620. regmap_write(pmu_regs, ctrl->regs[i], ctrl->value);
  621. }
  622. struct samsung_retention_ctrl *
  623. exynos_retention_init(struct samsung_pinctrl_drv_data *drvdata,
  624. const struct samsung_retention_data *data)
  625. {
  626. struct samsung_retention_ctrl *ctrl;
  627. struct regmap *pmu_regs;
  628. int i;
  629. ctrl = devm_kzalloc(drvdata->dev, sizeof(*ctrl), GFP_KERNEL);
  630. if (!ctrl)
  631. return ERR_PTR(-ENOMEM);
  632. pmu_regs = exynos_get_pmu_regmap();
  633. if (IS_ERR(pmu_regs))
  634. return ERR_CAST(pmu_regs);
  635. ctrl->priv = pmu_regs;
  636. ctrl->regs = data->regs;
  637. ctrl->nr_regs = data->nr_regs;
  638. ctrl->value = data->value;
  639. ctrl->refcnt = data->refcnt;
  640. ctrl->enable = exynos_retention_enable;
  641. ctrl->disable = exynos_retention_disable;
  642. /* Ensure that retention is disabled on driver init */
  643. for (i = 0; i < ctrl->nr_regs; i++)
  644. regmap_write(pmu_regs, ctrl->regs[i], ctrl->value);
  645. return ctrl;
  646. }