pinctrl-exynos.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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/clk.h>
  16. #include <linux/device.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/irqdomain.h>
  19. #include <linux/irq.h>
  20. #include <linux/irqchip/chained_irq.h>
  21. #include <linux/of.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/slab.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/regmap.h>
  26. #include <linux/err.h>
  27. #include <linux/soc/samsung/exynos-pmu.h>
  28. #include <linux/soc/samsung/exynos-regs-pmu.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;
  51. unsigned int mask;
  52. unsigned long flags;
  53. if (bank->eint_mask_offset)
  54. reg_mask = bank->pctl_offset + bank->eint_mask_offset;
  55. else
  56. reg_mask = our_chip->eint_mask + bank->eint_offset;
  57. if (clk_enable(bank->drvdata->pclk)) {
  58. dev_err(bank->gpio_chip.parent,
  59. "unable to enable clock for masking IRQ\n");
  60. return;
  61. }
  62. raw_spin_lock_irqsave(&bank->slock, flags);
  63. mask = readl(bank->eint_base + reg_mask);
  64. mask |= 1 << irqd->hwirq;
  65. writel(mask, bank->eint_base + reg_mask);
  66. raw_spin_unlock_irqrestore(&bank->slock, flags);
  67. clk_disable(bank->drvdata->pclk);
  68. }
  69. static void exynos_irq_ack(struct irq_data *irqd)
  70. {
  71. struct irq_chip *chip = irq_data_get_irq_chip(irqd);
  72. struct exynos_irq_chip *our_chip = to_exynos_irq_chip(chip);
  73. struct samsung_pin_bank *bank = irq_data_get_irq_chip_data(irqd);
  74. unsigned long reg_pend;
  75. if (bank->eint_pend_offset)
  76. reg_pend = bank->pctl_offset + bank->eint_pend_offset;
  77. else
  78. reg_pend = our_chip->eint_pend + bank->eint_offset;
  79. if (clk_enable(bank->drvdata->pclk)) {
  80. dev_err(bank->gpio_chip.parent,
  81. "unable to enable clock to ack IRQ\n");
  82. return;
  83. }
  84. writel(1 << irqd->hwirq, bank->eint_base + reg_pend);
  85. clk_disable(bank->drvdata->pclk);
  86. }
  87. static void exynos_irq_unmask(struct irq_data *irqd)
  88. {
  89. struct irq_chip *chip = irq_data_get_irq_chip(irqd);
  90. struct exynos_irq_chip *our_chip = to_exynos_irq_chip(chip);
  91. struct samsung_pin_bank *bank = irq_data_get_irq_chip_data(irqd);
  92. unsigned long reg_mask;
  93. unsigned int mask;
  94. unsigned long flags;
  95. /*
  96. * Ack level interrupts right before unmask
  97. *
  98. * If we don't do this we'll get a double-interrupt. Level triggered
  99. * interrupts must not fire an interrupt if the level is not
  100. * _currently_ active, even if it was active while the interrupt was
  101. * masked.
  102. */
  103. if (irqd_get_trigger_type(irqd) & IRQ_TYPE_LEVEL_MASK)
  104. exynos_irq_ack(irqd);
  105. if (bank->eint_mask_offset)
  106. reg_mask = bank->pctl_offset + bank->eint_mask_offset;
  107. else
  108. reg_mask = our_chip->eint_mask + bank->eint_offset;
  109. if (clk_enable(bank->drvdata->pclk)) {
  110. dev_err(bank->gpio_chip.parent,
  111. "unable to enable clock for unmasking IRQ\n");
  112. return;
  113. }
  114. raw_spin_lock_irqsave(&bank->slock, flags);
  115. mask = readl(bank->eint_base + reg_mask);
  116. mask &= ~(1 << irqd->hwirq);
  117. writel(mask, bank->eint_base + reg_mask);
  118. raw_spin_unlock_irqrestore(&bank->slock, flags);
  119. clk_disable(bank->drvdata->pclk);
  120. }
  121. static int exynos_irq_set_type(struct irq_data *irqd, unsigned int type)
  122. {
  123. struct irq_chip *chip = irq_data_get_irq_chip(irqd);
  124. struct exynos_irq_chip *our_chip = to_exynos_irq_chip(chip);
  125. struct samsung_pin_bank *bank = irq_data_get_irq_chip_data(irqd);
  126. unsigned int shift = EXYNOS_EINT_CON_LEN * irqd->hwirq;
  127. unsigned int con, trig_type;
  128. unsigned long reg_con;
  129. int ret;
  130. switch (type) {
  131. case IRQ_TYPE_EDGE_RISING:
  132. trig_type = EXYNOS_EINT_EDGE_RISING;
  133. break;
  134. case IRQ_TYPE_EDGE_FALLING:
  135. trig_type = EXYNOS_EINT_EDGE_FALLING;
  136. break;
  137. case IRQ_TYPE_EDGE_BOTH:
  138. trig_type = EXYNOS_EINT_EDGE_BOTH;
  139. break;
  140. case IRQ_TYPE_LEVEL_HIGH:
  141. trig_type = EXYNOS_EINT_LEVEL_HIGH;
  142. break;
  143. case IRQ_TYPE_LEVEL_LOW:
  144. trig_type = EXYNOS_EINT_LEVEL_LOW;
  145. break;
  146. default:
  147. pr_err("unsupported external interrupt type\n");
  148. return -EINVAL;
  149. }
  150. if (type & IRQ_TYPE_EDGE_BOTH)
  151. irq_set_handler_locked(irqd, handle_edge_irq);
  152. else
  153. irq_set_handler_locked(irqd, handle_level_irq);
  154. if (bank->eint_con_offset)
  155. reg_con = bank->pctl_offset + bank->eint_con_offset;
  156. else
  157. reg_con = our_chip->eint_con + bank->eint_offset;
  158. ret = clk_enable(bank->drvdata->pclk);
  159. if (ret) {
  160. dev_err(bank->gpio_chip.parent,
  161. "unable to enable clock for configuring IRQ type\n");
  162. return ret;
  163. }
  164. con = readl(bank->eint_base + reg_con);
  165. con &= ~(EXYNOS_EINT_CON_MASK << shift);
  166. con |= trig_type << shift;
  167. writel(con, bank->eint_base + reg_con);
  168. clk_disable(bank->drvdata->pclk);
  169. return 0;
  170. }
  171. static int exynos_irq_set_affinity(struct irq_data *irqd,
  172. const struct cpumask *dest, bool force)
  173. {
  174. struct samsung_pin_bank *bank = irq_data_get_irq_chip_data(irqd);
  175. struct samsung_pinctrl_drv_data *d = bank->drvdata;
  176. struct irq_data *parent = irq_get_irq_data(d->irq);
  177. if (parent)
  178. return parent->chip->irq_set_affinity(parent, dest, force);
  179. return -EINVAL;
  180. }
  181. static int exynos_irq_request_resources(struct irq_data *irqd)
  182. {
  183. struct samsung_pin_bank *bank = irq_data_get_irq_chip_data(irqd);
  184. const struct samsung_pin_bank_type *bank_type = bank->type;
  185. unsigned long reg_con, flags;
  186. unsigned int shift, mask, con;
  187. int ret;
  188. ret = gpiochip_lock_as_irq(&bank->gpio_chip, irqd->hwirq);
  189. if (ret) {
  190. dev_err(bank->gpio_chip.parent,
  191. "unable to lock pin %s-%lu IRQ\n",
  192. bank->name, irqd->hwirq);
  193. return ret;
  194. }
  195. reg_con = bank->pctl_offset + bank_type->reg_offset[PINCFG_TYPE_FUNC];
  196. shift = irqd->hwirq * bank_type->fld_width[PINCFG_TYPE_FUNC];
  197. mask = (1 << bank_type->fld_width[PINCFG_TYPE_FUNC]) - 1;
  198. ret = clk_enable(bank->drvdata->pclk);
  199. if (ret) {
  200. dev_err(bank->gpio_chip.parent,
  201. "unable to enable clock for configuring pin %s-%lu\n",
  202. bank->name, irqd->hwirq);
  203. return ret;
  204. }
  205. raw_spin_lock_irqsave(&bank->slock, flags);
  206. con = readl(bank->pctl_base + reg_con);
  207. con &= ~(mask << shift);
  208. con |= EXYNOS_PIN_CON_FUNC_EINT << shift;
  209. writel(con, bank->pctl_base + reg_con);
  210. raw_spin_unlock_irqrestore(&bank->slock, flags);
  211. clk_disable(bank->drvdata->pclk);
  212. return 0;
  213. }
  214. static void exynos_irq_release_resources(struct irq_data *irqd)
  215. {
  216. struct samsung_pin_bank *bank = irq_data_get_irq_chip_data(irqd);
  217. const struct samsung_pin_bank_type *bank_type = bank->type;
  218. unsigned long reg_con, flags;
  219. unsigned int shift, mask, con;
  220. reg_con = bank->pctl_offset + bank_type->reg_offset[PINCFG_TYPE_FUNC];
  221. shift = irqd->hwirq * bank_type->fld_width[PINCFG_TYPE_FUNC];
  222. mask = (1 << bank_type->fld_width[PINCFG_TYPE_FUNC]) - 1;
  223. if (clk_enable(bank->drvdata->pclk)) {
  224. dev_err(bank->gpio_chip.parent,
  225. "unable to enable clock for deconfiguring pin %s-%lu\n",
  226. bank->name, irqd->hwirq);
  227. return;
  228. }
  229. raw_spin_lock_irqsave(&bank->slock, flags);
  230. con = readl(bank->pctl_base + reg_con);
  231. con &= ~(mask << shift);
  232. con |= PIN_CON_FUNC_INPUT << shift;
  233. writel(con, bank->pctl_base + reg_con);
  234. raw_spin_unlock_irqrestore(&bank->slock, flags);
  235. clk_disable(bank->drvdata->pclk);
  236. gpiochip_unlock_as_irq(&bank->gpio_chip, irqd->hwirq);
  237. }
  238. /*
  239. * irq_chip for gpio interrupts.
  240. */
  241. static const struct exynos_irq_chip exynos_gpio_irq_chip __initconst = {
  242. .chip = {
  243. .name = "exynos_gpio_irq_chip",
  244. .irq_unmask = exynos_irq_unmask,
  245. .irq_mask = exynos_irq_mask,
  246. .irq_ack = exynos_irq_ack,
  247. .irq_set_type = exynos_irq_set_type,
  248. .irq_set_affinity = exynos_irq_set_affinity,
  249. .irq_request_resources = exynos_irq_request_resources,
  250. .irq_release_resources = exynos_irq_release_resources,
  251. },
  252. .eint_con = EXYNOS_GPIO_ECON_OFFSET,
  253. .eint_mask = EXYNOS_GPIO_EMASK_OFFSET,
  254. .eint_pend = EXYNOS_GPIO_EPEND_OFFSET,
  255. /* eint_wake_mask_value not used */
  256. };
  257. static int exynos_eint_irq_map(struct irq_domain *h, unsigned int virq,
  258. irq_hw_number_t hw)
  259. {
  260. struct samsung_pin_bank *b = h->host_data;
  261. irq_set_chip_data(virq, b);
  262. irq_set_chip_and_handler(virq, &b->irq_chip->chip,
  263. handle_level_irq);
  264. return 0;
  265. }
  266. /*
  267. * irq domain callbacks for external gpio and wakeup interrupt controllers.
  268. */
  269. static const struct irq_domain_ops exynos_eint_irqd_ops = {
  270. .map = exynos_eint_irq_map,
  271. .xlate = irq_domain_xlate_twocell,
  272. };
  273. static irqreturn_t exynos_eint_gpio_irq(int irq, void *data)
  274. {
  275. struct samsung_pinctrl_drv_data *d = data;
  276. struct samsung_pin_bank *bank = d->pin_banks;
  277. unsigned int svc, group, pin;
  278. int ret;
  279. if (clk_enable(bank->drvdata->pclk)) {
  280. dev_err(bank->gpio_chip.parent,
  281. "unable to enable clock for handling IRQ\n");
  282. return IRQ_NONE;
  283. }
  284. if (bank->eint_con_offset)
  285. svc = readl(bank->eint_base + EXYNOSAUTO_SVC_OFFSET);
  286. else
  287. svc = readl(bank->eint_base + EXYNOS_SVC_OFFSET);
  288. clk_disable(bank->drvdata->pclk);
  289. group = EXYNOS_SVC_GROUP(svc);
  290. pin = svc & EXYNOS_SVC_NUM_MASK;
  291. if (!group)
  292. return IRQ_HANDLED;
  293. bank += (group - 1);
  294. ret = generic_handle_domain_irq(bank->irq_domain, pin);
  295. if (ret)
  296. return IRQ_NONE;
  297. return IRQ_HANDLED;
  298. }
  299. struct exynos_eint_gpio_save {
  300. u32 eint_con;
  301. u32 eint_fltcon0;
  302. u32 eint_fltcon1;
  303. u32 eint_mask;
  304. };
  305. /*
  306. * exynos_eint_gpio_init() - setup handling of external gpio interrupts.
  307. * @d: driver data of samsung pinctrl driver.
  308. */
  309. __init int exynos_eint_gpio_init(struct samsung_pinctrl_drv_data *d)
  310. {
  311. struct samsung_pin_bank *bank;
  312. struct device *dev = d->dev;
  313. int ret;
  314. int i;
  315. if (!d->irq) {
  316. dev_err(dev, "irq number not available\n");
  317. return -EINVAL;
  318. }
  319. ret = devm_request_irq(dev, d->irq, exynos_eint_gpio_irq,
  320. 0, dev_name(dev), d);
  321. if (ret) {
  322. dev_err(dev, "irq request failed\n");
  323. return -ENXIO;
  324. }
  325. bank = d->pin_banks;
  326. for (i = 0; i < d->nr_banks; ++i, ++bank) {
  327. if (bank->eint_type != EINT_TYPE_GPIO)
  328. continue;
  329. bank->irq_chip = devm_kmemdup(dev, &exynos_gpio_irq_chip,
  330. sizeof(*bank->irq_chip), GFP_KERNEL);
  331. if (!bank->irq_chip) {
  332. ret = -ENOMEM;
  333. goto err_domains;
  334. }
  335. bank->irq_chip->chip.name = bank->name;
  336. bank->irq_domain = irq_domain_create_linear(bank->fwnode,
  337. bank->nr_pins, &exynos_eint_irqd_ops, bank);
  338. if (!bank->irq_domain) {
  339. dev_err(dev, "gpio irq domain add failed\n");
  340. ret = -ENXIO;
  341. goto err_domains;
  342. }
  343. bank->soc_priv = devm_kzalloc(d->dev,
  344. sizeof(struct exynos_eint_gpio_save), GFP_KERNEL);
  345. if (!bank->soc_priv) {
  346. irq_domain_remove(bank->irq_domain);
  347. ret = -ENOMEM;
  348. goto err_domains;
  349. }
  350. }
  351. return 0;
  352. err_domains:
  353. for (--i, --bank; i >= 0; --i, --bank) {
  354. if (bank->eint_type != EINT_TYPE_GPIO)
  355. continue;
  356. irq_domain_remove(bank->irq_domain);
  357. }
  358. return ret;
  359. }
  360. static int exynos_wkup_irq_set_wake(struct irq_data *irqd, unsigned int on)
  361. {
  362. struct irq_chip *chip = irq_data_get_irq_chip(irqd);
  363. struct exynos_irq_chip *our_chip = to_exynos_irq_chip(chip);
  364. struct samsung_pin_bank *bank = irq_data_get_irq_chip_data(irqd);
  365. unsigned long bit = 1UL << (2 * bank->eint_offset + irqd->hwirq);
  366. pr_info("wake %s for irq %u (%s-%lu)\n", on ? "enabled" : "disabled",
  367. irqd->irq, bank->name, irqd->hwirq);
  368. if (!on)
  369. *our_chip->eint_wake_mask_value |= bit;
  370. else
  371. *our_chip->eint_wake_mask_value &= ~bit;
  372. return 0;
  373. }
  374. static void
  375. exynos_pinctrl_set_eint_wakeup_mask(struct samsung_pinctrl_drv_data *drvdata,
  376. struct exynos_irq_chip *irq_chip)
  377. {
  378. struct regmap *pmu_regs;
  379. if (!drvdata->retention_ctrl || !drvdata->retention_ctrl->priv) {
  380. dev_warn(drvdata->dev,
  381. "No retention data configured bank with external wakeup interrupt. Wake-up mask will not be set.\n");
  382. return;
  383. }
  384. pmu_regs = drvdata->retention_ctrl->priv;
  385. dev_info(drvdata->dev,
  386. "Setting external wakeup interrupt mask: 0x%x\n",
  387. *irq_chip->eint_wake_mask_value);
  388. regmap_write(pmu_regs, irq_chip->eint_wake_mask_reg,
  389. *irq_chip->eint_wake_mask_value);
  390. }
  391. static void
  392. s5pv210_pinctrl_set_eint_wakeup_mask(struct samsung_pinctrl_drv_data *drvdata,
  393. struct exynos_irq_chip *irq_chip)
  394. {
  395. void __iomem *clk_base;
  396. if (!drvdata->retention_ctrl || !drvdata->retention_ctrl->priv) {
  397. dev_warn(drvdata->dev,
  398. "No retention data configured bank with external wakeup interrupt. Wake-up mask will not be set.\n");
  399. return;
  400. }
  401. clk_base = (void __iomem *) drvdata->retention_ctrl->priv;
  402. __raw_writel(*irq_chip->eint_wake_mask_value,
  403. clk_base + irq_chip->eint_wake_mask_reg);
  404. }
  405. static u32 eint_wake_mask_value = EXYNOS_EINT_WAKEUP_MASK_DISABLED;
  406. /*
  407. * irq_chip for wakeup interrupts
  408. */
  409. static const struct exynos_irq_chip s5pv210_wkup_irq_chip __initconst = {
  410. .chip = {
  411. .name = "s5pv210_wkup_irq_chip",
  412. .irq_unmask = exynos_irq_unmask,
  413. .irq_mask = exynos_irq_mask,
  414. .irq_ack = exynos_irq_ack,
  415. .irq_set_type = exynos_irq_set_type,
  416. .irq_set_wake = exynos_wkup_irq_set_wake,
  417. .irq_request_resources = exynos_irq_request_resources,
  418. .irq_release_resources = exynos_irq_release_resources,
  419. },
  420. .eint_con = EXYNOS_WKUP_ECON_OFFSET,
  421. .eint_mask = EXYNOS_WKUP_EMASK_OFFSET,
  422. .eint_pend = EXYNOS_WKUP_EPEND_OFFSET,
  423. .eint_wake_mask_value = &eint_wake_mask_value,
  424. /* Only differences with exynos4210_wkup_irq_chip: */
  425. .eint_wake_mask_reg = S5PV210_EINT_WAKEUP_MASK,
  426. .set_eint_wakeup_mask = s5pv210_pinctrl_set_eint_wakeup_mask,
  427. };
  428. static const struct exynos_irq_chip exynos4210_wkup_irq_chip __initconst = {
  429. .chip = {
  430. .name = "exynos4210_wkup_irq_chip",
  431. .irq_unmask = exynos_irq_unmask,
  432. .irq_mask = exynos_irq_mask,
  433. .irq_ack = exynos_irq_ack,
  434. .irq_set_type = exynos_irq_set_type,
  435. .irq_set_wake = exynos_wkup_irq_set_wake,
  436. .irq_request_resources = exynos_irq_request_resources,
  437. .irq_release_resources = exynos_irq_release_resources,
  438. },
  439. .eint_con = EXYNOS_WKUP_ECON_OFFSET,
  440. .eint_mask = EXYNOS_WKUP_EMASK_OFFSET,
  441. .eint_pend = EXYNOS_WKUP_EPEND_OFFSET,
  442. .eint_wake_mask_value = &eint_wake_mask_value,
  443. .eint_wake_mask_reg = EXYNOS_EINT_WAKEUP_MASK,
  444. .set_eint_wakeup_mask = exynos_pinctrl_set_eint_wakeup_mask,
  445. };
  446. static const struct exynos_irq_chip exynos7_wkup_irq_chip __initconst = {
  447. .chip = {
  448. .name = "exynos7_wkup_irq_chip",
  449. .irq_unmask = exynos_irq_unmask,
  450. .irq_mask = exynos_irq_mask,
  451. .irq_ack = exynos_irq_ack,
  452. .irq_set_type = exynos_irq_set_type,
  453. .irq_set_wake = exynos_wkup_irq_set_wake,
  454. .irq_request_resources = exynos_irq_request_resources,
  455. .irq_release_resources = exynos_irq_release_resources,
  456. },
  457. .eint_con = EXYNOS7_WKUP_ECON_OFFSET,
  458. .eint_mask = EXYNOS7_WKUP_EMASK_OFFSET,
  459. .eint_pend = EXYNOS7_WKUP_EPEND_OFFSET,
  460. .eint_wake_mask_value = &eint_wake_mask_value,
  461. .eint_wake_mask_reg = EXYNOS5433_EINT_WAKEUP_MASK,
  462. .set_eint_wakeup_mask = exynos_pinctrl_set_eint_wakeup_mask,
  463. };
  464. static const struct exynos_irq_chip exynosautov920_wkup_irq_chip __initconst = {
  465. .chip = {
  466. .name = "exynosautov920_wkup_irq_chip",
  467. .irq_unmask = exynos_irq_unmask,
  468. .irq_mask = exynos_irq_mask,
  469. .irq_ack = exynos_irq_ack,
  470. .irq_set_type = exynos_irq_set_type,
  471. .irq_set_wake = exynos_wkup_irq_set_wake,
  472. .irq_request_resources = exynos_irq_request_resources,
  473. .irq_release_resources = exynos_irq_release_resources,
  474. },
  475. .eint_wake_mask_value = &eint_wake_mask_value,
  476. .eint_wake_mask_reg = EXYNOS5433_EINT_WAKEUP_MASK,
  477. .set_eint_wakeup_mask = exynos_pinctrl_set_eint_wakeup_mask,
  478. };
  479. /* list of external wakeup controllers supported */
  480. static const struct of_device_id exynos_wkup_irq_ids[] = {
  481. { .compatible = "samsung,s5pv210-wakeup-eint",
  482. .data = &s5pv210_wkup_irq_chip },
  483. { .compatible = "samsung,exynos4210-wakeup-eint",
  484. .data = &exynos4210_wkup_irq_chip },
  485. { .compatible = "samsung,exynos7-wakeup-eint",
  486. .data = &exynos7_wkup_irq_chip },
  487. { .compatible = "samsung,exynos850-wakeup-eint",
  488. .data = &exynos7_wkup_irq_chip },
  489. { .compatible = "samsung,exynosautov9-wakeup-eint",
  490. .data = &exynos7_wkup_irq_chip },
  491. { .compatible = "samsung,exynosautov920-wakeup-eint",
  492. .data = &exynosautov920_wkup_irq_chip },
  493. { }
  494. };
  495. /* interrupt handler for wakeup interrupts 0..15 */
  496. static void exynos_irq_eint0_15(struct irq_desc *desc)
  497. {
  498. struct exynos_weint_data *eintd = irq_desc_get_handler_data(desc);
  499. struct samsung_pin_bank *bank = eintd->bank;
  500. struct irq_chip *chip = irq_desc_get_chip(desc);
  501. chained_irq_enter(chip, desc);
  502. generic_handle_domain_irq(bank->irq_domain, eintd->irq);
  503. chained_irq_exit(chip, desc);
  504. }
  505. static inline void exynos_irq_demux_eint(unsigned int pend,
  506. struct irq_domain *domain)
  507. {
  508. unsigned int irq;
  509. while (pend) {
  510. irq = fls(pend) - 1;
  511. generic_handle_domain_irq(domain, irq);
  512. pend &= ~(1 << irq);
  513. }
  514. }
  515. /* interrupt handler for wakeup interrupt 16 */
  516. static void exynos_irq_demux_eint16_31(struct irq_desc *desc)
  517. {
  518. struct irq_chip *chip = irq_desc_get_chip(desc);
  519. struct exynos_muxed_weint_data *eintd = irq_desc_get_handler_data(desc);
  520. unsigned int pend;
  521. unsigned int mask;
  522. int i;
  523. chained_irq_enter(chip, desc);
  524. /*
  525. * just enable the clock once here, to avoid an enable/disable dance for
  526. * each bank.
  527. */
  528. if (eintd->nr_banks) {
  529. struct samsung_pin_bank *b = eintd->banks[0];
  530. if (clk_enable(b->drvdata->pclk)) {
  531. dev_err(b->gpio_chip.parent,
  532. "unable to enable clock for pending IRQs\n");
  533. goto out;
  534. }
  535. }
  536. for (i = 0; i < eintd->nr_banks; ++i) {
  537. struct samsung_pin_bank *b = eintd->banks[i];
  538. pend = readl(b->eint_base + b->irq_chip->eint_pend
  539. + b->eint_offset);
  540. mask = readl(b->eint_base + b->irq_chip->eint_mask
  541. + b->eint_offset);
  542. exynos_irq_demux_eint(pend & ~mask, b->irq_domain);
  543. }
  544. if (eintd->nr_banks)
  545. clk_disable(eintd->banks[0]->drvdata->pclk);
  546. out:
  547. chained_irq_exit(chip, desc);
  548. }
  549. /*
  550. * exynos_eint_wkup_init() - setup handling of external wakeup interrupts.
  551. * @d: driver data of samsung pinctrl driver.
  552. */
  553. __init int exynos_eint_wkup_init(struct samsung_pinctrl_drv_data *d)
  554. {
  555. struct device *dev = d->dev;
  556. struct device_node *wkup_np __free(device_node) = NULL;
  557. struct device_node *np;
  558. struct samsung_pin_bank *bank;
  559. struct exynos_weint_data *weint_data;
  560. struct exynos_muxed_weint_data *muxed_data;
  561. const struct exynos_irq_chip *irq_chip;
  562. unsigned int muxed_banks = 0;
  563. unsigned int i;
  564. int idx, irq;
  565. for_each_child_of_node(dev->of_node, np) {
  566. const struct of_device_id *match;
  567. match = of_match_node(exynos_wkup_irq_ids, np);
  568. if (match) {
  569. irq_chip = match->data;
  570. wkup_np = np;
  571. break;
  572. }
  573. }
  574. if (!wkup_np)
  575. return -ENODEV;
  576. bank = d->pin_banks;
  577. for (i = 0; i < d->nr_banks; ++i, ++bank) {
  578. if (bank->eint_type != EINT_TYPE_WKUP)
  579. continue;
  580. bank->irq_chip = devm_kmemdup(dev, irq_chip, sizeof(*irq_chip),
  581. GFP_KERNEL);
  582. if (!bank->irq_chip)
  583. return -ENOMEM;
  584. bank->irq_chip->chip.name = bank->name;
  585. bank->irq_domain = irq_domain_create_linear(bank->fwnode,
  586. bank->nr_pins, &exynos_eint_irqd_ops, bank);
  587. if (!bank->irq_domain) {
  588. dev_err(dev, "wkup irq domain add failed\n");
  589. return -ENXIO;
  590. }
  591. if (!fwnode_property_present(bank->fwnode, "interrupts")) {
  592. bank->eint_type = EINT_TYPE_WKUP_MUX;
  593. ++muxed_banks;
  594. continue;
  595. }
  596. weint_data = devm_kcalloc(dev,
  597. bank->nr_pins, sizeof(*weint_data),
  598. GFP_KERNEL);
  599. if (!weint_data)
  600. return -ENOMEM;
  601. for (idx = 0; idx < bank->nr_pins; ++idx) {
  602. irq = irq_of_parse_and_map(to_of_node(bank->fwnode), idx);
  603. if (!irq) {
  604. dev_err(dev, "irq number for eint-%s-%d not found\n",
  605. bank->name, idx);
  606. continue;
  607. }
  608. weint_data[idx].irq = idx;
  609. weint_data[idx].bank = bank;
  610. irq_set_chained_handler_and_data(irq,
  611. exynos_irq_eint0_15,
  612. &weint_data[idx]);
  613. }
  614. }
  615. if (!muxed_banks)
  616. return 0;
  617. irq = irq_of_parse_and_map(wkup_np, 0);
  618. if (!irq) {
  619. dev_err(dev, "irq number for muxed EINTs not found\n");
  620. return 0;
  621. }
  622. muxed_data = devm_kzalloc(dev, sizeof(*muxed_data)
  623. + muxed_banks*sizeof(struct samsung_pin_bank *), GFP_KERNEL);
  624. if (!muxed_data)
  625. return -ENOMEM;
  626. muxed_data->nr_banks = muxed_banks;
  627. irq_set_chained_handler_and_data(irq, exynos_irq_demux_eint16_31,
  628. muxed_data);
  629. bank = d->pin_banks;
  630. idx = 0;
  631. for (i = 0; i < d->nr_banks; ++i, ++bank) {
  632. if (bank->eint_type != EINT_TYPE_WKUP_MUX)
  633. continue;
  634. muxed_data->banks[idx++] = bank;
  635. }
  636. return 0;
  637. }
  638. static void exynos_set_wakeup(struct samsung_pin_bank *bank)
  639. {
  640. struct exynos_irq_chip *irq_chip;
  641. if (bank->irq_chip) {
  642. irq_chip = bank->irq_chip;
  643. irq_chip->set_eint_wakeup_mask(bank->drvdata, irq_chip);
  644. }
  645. }
  646. void exynos_pinctrl_suspend(struct samsung_pin_bank *bank)
  647. {
  648. struct exynos_eint_gpio_save *save = bank->soc_priv;
  649. const void __iomem *regs = bank->eint_base;
  650. if (bank->eint_type == EINT_TYPE_GPIO) {
  651. save->eint_con = readl(regs + EXYNOS_GPIO_ECON_OFFSET
  652. + bank->eint_offset);
  653. save->eint_fltcon0 = readl(regs + EXYNOS_GPIO_EFLTCON_OFFSET
  654. + 2 * bank->eint_offset);
  655. save->eint_fltcon1 = readl(regs + EXYNOS_GPIO_EFLTCON_OFFSET
  656. + 2 * bank->eint_offset + 4);
  657. save->eint_mask = readl(regs + bank->irq_chip->eint_mask
  658. + bank->eint_offset);
  659. pr_debug("%s: save con %#010x\n",
  660. bank->name, save->eint_con);
  661. pr_debug("%s: save fltcon0 %#010x\n",
  662. bank->name, save->eint_fltcon0);
  663. pr_debug("%s: save fltcon1 %#010x\n",
  664. bank->name, save->eint_fltcon1);
  665. pr_debug("%s: save mask %#010x\n",
  666. bank->name, save->eint_mask);
  667. } else if (bank->eint_type == EINT_TYPE_WKUP) {
  668. exynos_set_wakeup(bank);
  669. }
  670. }
  671. void gs101_pinctrl_suspend(struct samsung_pin_bank *bank)
  672. {
  673. struct exynos_eint_gpio_save *save = bank->soc_priv;
  674. const void __iomem *regs = bank->eint_base;
  675. if (bank->eint_type == EINT_TYPE_GPIO) {
  676. save->eint_con = readl(regs + EXYNOS_GPIO_ECON_OFFSET
  677. + bank->eint_offset);
  678. save->eint_fltcon0 = readl(regs + EXYNOS_GPIO_EFLTCON_OFFSET
  679. + bank->eint_fltcon_offset);
  680. /* fltcon1 register only exists for pins 4-7 */
  681. if (bank->nr_pins > 4)
  682. save->eint_fltcon1 = readl(regs +
  683. EXYNOS_GPIO_EFLTCON_OFFSET
  684. + bank->eint_fltcon_offset + 4);
  685. save->eint_mask = readl(regs + bank->irq_chip->eint_mask
  686. + bank->eint_offset);
  687. pr_debug("%s: save con %#010x\n",
  688. bank->name, save->eint_con);
  689. pr_debug("%s: save fltcon0 %#010x\n",
  690. bank->name, save->eint_fltcon0);
  691. if (bank->nr_pins > 4)
  692. pr_debug("%s: save fltcon1 %#010x\n",
  693. bank->name, save->eint_fltcon1);
  694. pr_debug("%s: save mask %#010x\n",
  695. bank->name, save->eint_mask);
  696. } else if (bank->eint_type == EINT_TYPE_WKUP) {
  697. exynos_set_wakeup(bank);
  698. }
  699. }
  700. void exynosautov920_pinctrl_suspend(struct samsung_pin_bank *bank)
  701. {
  702. struct exynos_eint_gpio_save *save = bank->soc_priv;
  703. const void __iomem *regs = bank->eint_base;
  704. if (bank->eint_type == EINT_TYPE_GPIO) {
  705. save->eint_con = readl(regs + bank->pctl_offset +
  706. bank->eint_con_offset);
  707. save->eint_mask = readl(regs + bank->pctl_offset +
  708. bank->eint_mask_offset);
  709. pr_debug("%s: save con %#010x\n",
  710. bank->name, save->eint_con);
  711. pr_debug("%s: save mask %#010x\n",
  712. bank->name, save->eint_mask);
  713. } else if (bank->eint_type == EINT_TYPE_WKUP) {
  714. exynos_set_wakeup(bank);
  715. }
  716. }
  717. void gs101_pinctrl_resume(struct samsung_pin_bank *bank)
  718. {
  719. struct exynos_eint_gpio_save *save = bank->soc_priv;
  720. void __iomem *regs = bank->eint_base;
  721. void __iomem *eint_fltcfg0 = regs + EXYNOS_GPIO_EFLTCON_OFFSET
  722. + bank->eint_fltcon_offset;
  723. if (bank->eint_type == EINT_TYPE_GPIO) {
  724. pr_debug("%s: con %#010x => %#010x\n", bank->name,
  725. readl(regs + EXYNOS_GPIO_ECON_OFFSET
  726. + bank->eint_offset), save->eint_con);
  727. pr_debug("%s: fltcon0 %#010x => %#010x\n", bank->name,
  728. readl(eint_fltcfg0), save->eint_fltcon0);
  729. /* fltcon1 register only exists for pins 4-7 */
  730. if (bank->nr_pins > 4)
  731. pr_debug("%s: fltcon1 %#010x => %#010x\n", bank->name,
  732. readl(eint_fltcfg0 + 4), save->eint_fltcon1);
  733. pr_debug("%s: mask %#010x => %#010x\n", bank->name,
  734. readl(regs + bank->irq_chip->eint_mask
  735. + bank->eint_offset), save->eint_mask);
  736. writel(save->eint_con, regs + EXYNOS_GPIO_ECON_OFFSET
  737. + bank->eint_offset);
  738. writel(save->eint_fltcon0, eint_fltcfg0);
  739. if (bank->nr_pins > 4)
  740. writel(save->eint_fltcon1, eint_fltcfg0 + 4);
  741. writel(save->eint_mask, regs + bank->irq_chip->eint_mask
  742. + bank->eint_offset);
  743. }
  744. }
  745. void exynos_pinctrl_resume(struct samsung_pin_bank *bank)
  746. {
  747. struct exynos_eint_gpio_save *save = bank->soc_priv;
  748. void __iomem *regs = bank->eint_base;
  749. if (bank->eint_type == EINT_TYPE_GPIO) {
  750. pr_debug("%s: con %#010x => %#010x\n", bank->name,
  751. readl(regs + EXYNOS_GPIO_ECON_OFFSET
  752. + bank->eint_offset), save->eint_con);
  753. pr_debug("%s: fltcon0 %#010x => %#010x\n", bank->name,
  754. readl(regs + EXYNOS_GPIO_EFLTCON_OFFSET
  755. + 2 * bank->eint_offset), save->eint_fltcon0);
  756. pr_debug("%s: fltcon1 %#010x => %#010x\n", bank->name,
  757. readl(regs + EXYNOS_GPIO_EFLTCON_OFFSET
  758. + 2 * bank->eint_offset + 4),
  759. save->eint_fltcon1);
  760. pr_debug("%s: mask %#010x => %#010x\n", bank->name,
  761. readl(regs + bank->irq_chip->eint_mask
  762. + bank->eint_offset), save->eint_mask);
  763. writel(save->eint_con, regs + EXYNOS_GPIO_ECON_OFFSET
  764. + bank->eint_offset);
  765. writel(save->eint_fltcon0, regs + EXYNOS_GPIO_EFLTCON_OFFSET
  766. + 2 * bank->eint_offset);
  767. writel(save->eint_fltcon1, regs + EXYNOS_GPIO_EFLTCON_OFFSET
  768. + 2 * bank->eint_offset + 4);
  769. writel(save->eint_mask, regs + bank->irq_chip->eint_mask
  770. + bank->eint_offset);
  771. }
  772. }
  773. void exynosautov920_pinctrl_resume(struct samsung_pin_bank *bank)
  774. {
  775. struct exynos_eint_gpio_save *save = bank->soc_priv;
  776. void __iomem *regs = bank->eint_base;
  777. if (bank->eint_type == EINT_TYPE_GPIO) {
  778. /* exynosautov920 has eint_con_offset for all but one bank */
  779. if (!bank->eint_con_offset)
  780. exynos_pinctrl_resume(bank);
  781. pr_debug("%s: con %#010x => %#010x\n", bank->name,
  782. readl(regs + bank->pctl_offset + bank->eint_con_offset),
  783. save->eint_con);
  784. pr_debug("%s: mask %#010x => %#010x\n", bank->name,
  785. readl(regs + bank->pctl_offset +
  786. bank->eint_mask_offset), save->eint_mask);
  787. writel(save->eint_con,
  788. regs + bank->pctl_offset + bank->eint_con_offset);
  789. writel(save->eint_mask,
  790. regs + bank->pctl_offset + bank->eint_mask_offset);
  791. }
  792. }
  793. static void exynos_retention_enable(struct samsung_pinctrl_drv_data *drvdata)
  794. {
  795. if (drvdata->retention_ctrl->refcnt)
  796. atomic_inc(drvdata->retention_ctrl->refcnt);
  797. }
  798. static void exynos_retention_disable(struct samsung_pinctrl_drv_data *drvdata)
  799. {
  800. struct samsung_retention_ctrl *ctrl = drvdata->retention_ctrl;
  801. struct regmap *pmu_regs = ctrl->priv;
  802. int i;
  803. if (ctrl->refcnt && !atomic_dec_and_test(ctrl->refcnt))
  804. return;
  805. for (i = 0; i < ctrl->nr_regs; i++)
  806. regmap_write(pmu_regs, ctrl->regs[i], ctrl->value);
  807. }
  808. struct samsung_retention_ctrl *
  809. exynos_retention_init(struct samsung_pinctrl_drv_data *drvdata,
  810. const struct samsung_retention_data *data)
  811. {
  812. struct samsung_retention_ctrl *ctrl;
  813. struct regmap *pmu_regs;
  814. int i;
  815. ctrl = devm_kzalloc(drvdata->dev, sizeof(*ctrl), GFP_KERNEL);
  816. if (!ctrl)
  817. return ERR_PTR(-ENOMEM);
  818. pmu_regs = exynos_get_pmu_regmap();
  819. if (IS_ERR(pmu_regs))
  820. return ERR_CAST(pmu_regs);
  821. ctrl->priv = pmu_regs;
  822. ctrl->regs = data->regs;
  823. ctrl->nr_regs = data->nr_regs;
  824. ctrl->value = data->value;
  825. ctrl->refcnt = data->refcnt;
  826. ctrl->enable = exynos_retention_enable;
  827. ctrl->disable = exynos_retention_disable;
  828. /* Ensure that retention is disabled on driver init */
  829. for (i = 0; i < ctrl->nr_regs; i++)
  830. regmap_write(pmu_regs, ctrl->regs[i], ctrl->value);
  831. return ctrl;
  832. }