pinctrl-s3c64xx.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. /*
  2. * S3C64xx specific support for pinctrl-samsung driver.
  3. *
  4. * Copyright (c) 2013 Tomasz Figa <tomasz.figa@gmail.com>
  5. *
  6. * Based on pinctrl-exynos.c, please see the file for original copyrights.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This file contains the Samsung S3C64xx specific information required by the
  14. * the Samsung pinctrl/gpiolib driver. It also includes the implementation of
  15. * external gpio and wakeup interrupt support.
  16. */
  17. #include <linux/init.h>
  18. #include <linux/device.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/irqdomain.h>
  21. #include <linux/irq.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/io.h>
  24. #include <linux/irqchip/chained_irq.h>
  25. #include <linux/slab.h>
  26. #include <linux/err.h>
  27. #include "pinctrl-samsung.h"
  28. #define NUM_EINT0 28
  29. #define NUM_EINT0_IRQ 4
  30. #define EINT_MAX_PER_REG 16
  31. #define EINT_MAX_PER_GROUP 16
  32. /* External GPIO and wakeup interrupt related definitions */
  33. #define SVC_GROUP_SHIFT 4
  34. #define SVC_GROUP_MASK 0xf
  35. #define SVC_NUM_MASK 0xf
  36. #define SVC_GROUP(x) ((x >> SVC_GROUP_SHIFT) & \
  37. SVC_GROUP_MASK)
  38. #define EINT12CON_REG 0x200
  39. #define EINT12MASK_REG 0x240
  40. #define EINT12PEND_REG 0x260
  41. #define EINT_OFFS(i) ((i) % (2 * EINT_MAX_PER_GROUP))
  42. #define EINT_GROUP(i) ((i) / EINT_MAX_PER_GROUP)
  43. #define EINT_REG(g) (4 * ((g) / 2))
  44. #define EINTCON_REG(i) (EINT12CON_REG + EINT_REG(EINT_GROUP(i)))
  45. #define EINTMASK_REG(i) (EINT12MASK_REG + EINT_REG(EINT_GROUP(i)))
  46. #define EINTPEND_REG(i) (EINT12PEND_REG + EINT_REG(EINT_GROUP(i)))
  47. #define SERVICE_REG 0x284
  48. #define SERVICEPEND_REG 0x288
  49. #define EINT0CON0_REG 0x900
  50. #define EINT0MASK_REG 0x920
  51. #define EINT0PEND_REG 0x924
  52. /* S3C64xx specific external interrupt trigger types */
  53. #define EINT_LEVEL_LOW 0
  54. #define EINT_LEVEL_HIGH 1
  55. #define EINT_EDGE_FALLING 2
  56. #define EINT_EDGE_RISING 4
  57. #define EINT_EDGE_BOTH 6
  58. #define EINT_CON_MASK 0xF
  59. #define EINT_CON_LEN 4
  60. static const struct samsung_pin_bank_type bank_type_4bit_off = {
  61. .fld_width = { 4, 1, 2, 0, 2, 2, },
  62. .reg_offset = { 0x00, 0x04, 0x08, 0, 0x0c, 0x10, },
  63. };
  64. static const struct samsung_pin_bank_type bank_type_4bit_alive = {
  65. .fld_width = { 4, 1, 2, },
  66. .reg_offset = { 0x00, 0x04, 0x08, },
  67. };
  68. static const struct samsung_pin_bank_type bank_type_4bit2_off = {
  69. .fld_width = { 4, 1, 2, 0, 2, 2, },
  70. .reg_offset = { 0x00, 0x08, 0x0c, 0, 0x10, 0x14, },
  71. };
  72. static const struct samsung_pin_bank_type bank_type_4bit2_alive = {
  73. .fld_width = { 4, 1, 2, },
  74. .reg_offset = { 0x00, 0x08, 0x0c, },
  75. };
  76. static const struct samsung_pin_bank_type bank_type_2bit_off = {
  77. .fld_width = { 2, 1, 2, 0, 2, 2, },
  78. .reg_offset = { 0x00, 0x04, 0x08, 0, 0x0c, 0x10, },
  79. };
  80. static const struct samsung_pin_bank_type bank_type_2bit_alive = {
  81. .fld_width = { 2, 1, 2, },
  82. .reg_offset = { 0x00, 0x04, 0x08, },
  83. };
  84. #define PIN_BANK_4BIT(pins, reg, id) \
  85. { \
  86. .type = &bank_type_4bit_off, \
  87. .pctl_offset = reg, \
  88. .nr_pins = pins, \
  89. .eint_type = EINT_TYPE_NONE, \
  90. .name = id \
  91. }
  92. #define PIN_BANK_4BIT_EINTG(pins, reg, id, eoffs) \
  93. { \
  94. .type = &bank_type_4bit_off, \
  95. .pctl_offset = reg, \
  96. .nr_pins = pins, \
  97. .eint_type = EINT_TYPE_GPIO, \
  98. .eint_func = 7, \
  99. .eint_mask = (1 << (pins)) - 1, \
  100. .eint_offset = eoffs, \
  101. .name = id \
  102. }
  103. #define PIN_BANK_4BIT_EINTW(pins, reg, id, eoffs, emask) \
  104. { \
  105. .type = &bank_type_4bit_alive,\
  106. .pctl_offset = reg, \
  107. .nr_pins = pins, \
  108. .eint_type = EINT_TYPE_WKUP, \
  109. .eint_func = 3, \
  110. .eint_mask = emask, \
  111. .eint_offset = eoffs, \
  112. .name = id \
  113. }
  114. #define PIN_BANK_4BIT2_EINTG(pins, reg, id, eoffs) \
  115. { \
  116. .type = &bank_type_4bit2_off, \
  117. .pctl_offset = reg, \
  118. .nr_pins = pins, \
  119. .eint_type = EINT_TYPE_GPIO, \
  120. .eint_func = 7, \
  121. .eint_mask = (1 << (pins)) - 1, \
  122. .eint_offset = eoffs, \
  123. .name = id \
  124. }
  125. #define PIN_BANK_4BIT2_EINTW(pins, reg, id, eoffs, emask) \
  126. { \
  127. .type = &bank_type_4bit2_alive,\
  128. .pctl_offset = reg, \
  129. .nr_pins = pins, \
  130. .eint_type = EINT_TYPE_WKUP, \
  131. .eint_func = 3, \
  132. .eint_mask = emask, \
  133. .eint_offset = eoffs, \
  134. .name = id \
  135. }
  136. #define PIN_BANK_4BIT2_ALIVE(pins, reg, id) \
  137. { \
  138. .type = &bank_type_4bit2_alive,\
  139. .pctl_offset = reg, \
  140. .nr_pins = pins, \
  141. .eint_type = EINT_TYPE_NONE, \
  142. .name = id \
  143. }
  144. #define PIN_BANK_2BIT(pins, reg, id) \
  145. { \
  146. .type = &bank_type_2bit_off, \
  147. .pctl_offset = reg, \
  148. .nr_pins = pins, \
  149. .eint_type = EINT_TYPE_NONE, \
  150. .name = id \
  151. }
  152. #define PIN_BANK_2BIT_EINTG(pins, reg, id, eoffs, emask) \
  153. { \
  154. .type = &bank_type_2bit_off, \
  155. .pctl_offset = reg, \
  156. .nr_pins = pins, \
  157. .eint_type = EINT_TYPE_GPIO, \
  158. .eint_func = 3, \
  159. .eint_mask = emask, \
  160. .eint_offset = eoffs, \
  161. .name = id \
  162. }
  163. #define PIN_BANK_2BIT_EINTW(pins, reg, id, eoffs) \
  164. { \
  165. .type = &bank_type_2bit_alive,\
  166. .pctl_offset = reg, \
  167. .nr_pins = pins, \
  168. .eint_type = EINT_TYPE_WKUP, \
  169. .eint_func = 2, \
  170. .eint_mask = (1 << (pins)) - 1, \
  171. .eint_offset = eoffs, \
  172. .name = id \
  173. }
  174. /**
  175. * struct s3c64xx_eint0_data: EINT0 common data
  176. * @drvdata: pin controller driver data
  177. * @domains: IRQ domains of particular EINT0 interrupts
  178. * @pins: pin offsets inside of banks of particular EINT0 interrupts
  179. */
  180. struct s3c64xx_eint0_data {
  181. struct samsung_pinctrl_drv_data *drvdata;
  182. struct irq_domain *domains[NUM_EINT0];
  183. u8 pins[NUM_EINT0];
  184. };
  185. /**
  186. * struct s3c64xx_eint0_domain_data: EINT0 per-domain data
  187. * @bank: pin bank related to the domain
  188. * @eints: EINT0 interrupts related to the domain
  189. */
  190. struct s3c64xx_eint0_domain_data {
  191. struct samsung_pin_bank *bank;
  192. u8 eints[];
  193. };
  194. /**
  195. * struct s3c64xx_eint_gpio_data: GPIO EINT data
  196. * @drvdata: pin controller driver data
  197. * @domains: array of domains related to EINT interrupt groups
  198. */
  199. struct s3c64xx_eint_gpio_data {
  200. struct samsung_pinctrl_drv_data *drvdata;
  201. struct irq_domain *domains[];
  202. };
  203. /*
  204. * Common functions for S3C64xx EINT configuration
  205. */
  206. static int s3c64xx_irq_get_trigger(unsigned int type)
  207. {
  208. int trigger;
  209. switch (type) {
  210. case IRQ_TYPE_EDGE_RISING:
  211. trigger = EINT_EDGE_RISING;
  212. break;
  213. case IRQ_TYPE_EDGE_FALLING:
  214. trigger = EINT_EDGE_FALLING;
  215. break;
  216. case IRQ_TYPE_EDGE_BOTH:
  217. trigger = EINT_EDGE_BOTH;
  218. break;
  219. case IRQ_TYPE_LEVEL_HIGH:
  220. trigger = EINT_LEVEL_HIGH;
  221. break;
  222. case IRQ_TYPE_LEVEL_LOW:
  223. trigger = EINT_LEVEL_LOW;
  224. break;
  225. default:
  226. return -EINVAL;
  227. }
  228. return trigger;
  229. }
  230. static void s3c64xx_irq_set_handler(struct irq_data *d, unsigned int type)
  231. {
  232. /* Edge- and level-triggered interrupts need different handlers */
  233. if (type & IRQ_TYPE_EDGE_BOTH)
  234. irq_set_handler_locked(d, handle_edge_irq);
  235. else
  236. irq_set_handler_locked(d, handle_level_irq);
  237. }
  238. static void s3c64xx_irq_set_function(struct samsung_pinctrl_drv_data *d,
  239. struct samsung_pin_bank *bank, int pin)
  240. {
  241. const struct samsung_pin_bank_type *bank_type = bank->type;
  242. unsigned long flags;
  243. void __iomem *reg;
  244. u8 shift;
  245. u32 mask;
  246. u32 val;
  247. /* Make sure that pin is configured as interrupt */
  248. reg = d->virt_base + bank->pctl_offset;
  249. shift = pin;
  250. if (bank_type->fld_width[PINCFG_TYPE_FUNC] * shift >= 32) {
  251. /* 4-bit bank type with 2 con regs */
  252. reg += 4;
  253. shift -= 8;
  254. }
  255. shift = shift * bank_type->fld_width[PINCFG_TYPE_FUNC];
  256. mask = (1 << bank_type->fld_width[PINCFG_TYPE_FUNC]) - 1;
  257. spin_lock_irqsave(&bank->slock, flags);
  258. val = readl(reg);
  259. val &= ~(mask << shift);
  260. val |= bank->eint_func << shift;
  261. writel(val, reg);
  262. spin_unlock_irqrestore(&bank->slock, flags);
  263. }
  264. /*
  265. * Functions for EINT GPIO configuration (EINT groups 1-9)
  266. */
  267. static inline void s3c64xx_gpio_irq_set_mask(struct irq_data *irqd, bool mask)
  268. {
  269. struct samsung_pin_bank *bank = irq_data_get_irq_chip_data(irqd);
  270. struct samsung_pinctrl_drv_data *d = bank->drvdata;
  271. unsigned char index = EINT_OFFS(bank->eint_offset) + irqd->hwirq;
  272. void __iomem *reg = d->virt_base + EINTMASK_REG(bank->eint_offset);
  273. u32 val;
  274. val = readl(reg);
  275. if (mask)
  276. val |= 1 << index;
  277. else
  278. val &= ~(1 << index);
  279. writel(val, reg);
  280. }
  281. static void s3c64xx_gpio_irq_unmask(struct irq_data *irqd)
  282. {
  283. s3c64xx_gpio_irq_set_mask(irqd, false);
  284. }
  285. static void s3c64xx_gpio_irq_mask(struct irq_data *irqd)
  286. {
  287. s3c64xx_gpio_irq_set_mask(irqd, true);
  288. }
  289. static void s3c64xx_gpio_irq_ack(struct irq_data *irqd)
  290. {
  291. struct samsung_pin_bank *bank = irq_data_get_irq_chip_data(irqd);
  292. struct samsung_pinctrl_drv_data *d = bank->drvdata;
  293. unsigned char index = EINT_OFFS(bank->eint_offset) + irqd->hwirq;
  294. void __iomem *reg = d->virt_base + EINTPEND_REG(bank->eint_offset);
  295. writel(1 << index, reg);
  296. }
  297. static int s3c64xx_gpio_irq_set_type(struct irq_data *irqd, unsigned int type)
  298. {
  299. struct samsung_pin_bank *bank = irq_data_get_irq_chip_data(irqd);
  300. struct samsung_pinctrl_drv_data *d = bank->drvdata;
  301. void __iomem *reg;
  302. int trigger;
  303. u8 shift;
  304. u32 val;
  305. trigger = s3c64xx_irq_get_trigger(type);
  306. if (trigger < 0) {
  307. pr_err("unsupported external interrupt type\n");
  308. return -EINVAL;
  309. }
  310. s3c64xx_irq_set_handler(irqd, type);
  311. /* Set up interrupt trigger */
  312. reg = d->virt_base + EINTCON_REG(bank->eint_offset);
  313. shift = EINT_OFFS(bank->eint_offset) + irqd->hwirq;
  314. shift = 4 * (shift / 4); /* 4 EINTs per trigger selector */
  315. val = readl(reg);
  316. val &= ~(EINT_CON_MASK << shift);
  317. val |= trigger << shift;
  318. writel(val, reg);
  319. s3c64xx_irq_set_function(d, bank, irqd->hwirq);
  320. return 0;
  321. }
  322. /*
  323. * irq_chip for gpio interrupts.
  324. */
  325. static struct irq_chip s3c64xx_gpio_irq_chip = {
  326. .name = "GPIO",
  327. .irq_unmask = s3c64xx_gpio_irq_unmask,
  328. .irq_mask = s3c64xx_gpio_irq_mask,
  329. .irq_ack = s3c64xx_gpio_irq_ack,
  330. .irq_set_type = s3c64xx_gpio_irq_set_type,
  331. };
  332. static int s3c64xx_gpio_irq_map(struct irq_domain *h, unsigned int virq,
  333. irq_hw_number_t hw)
  334. {
  335. struct samsung_pin_bank *bank = h->host_data;
  336. if (!(bank->eint_mask & (1 << hw)))
  337. return -EINVAL;
  338. irq_set_chip_and_handler(virq,
  339. &s3c64xx_gpio_irq_chip, handle_level_irq);
  340. irq_set_chip_data(virq, bank);
  341. return 0;
  342. }
  343. /*
  344. * irq domain callbacks for external gpio interrupt controller.
  345. */
  346. static const struct irq_domain_ops s3c64xx_gpio_irqd_ops = {
  347. .map = s3c64xx_gpio_irq_map,
  348. .xlate = irq_domain_xlate_twocell,
  349. };
  350. static void s3c64xx_eint_gpio_irq(struct irq_desc *desc)
  351. {
  352. struct irq_chip *chip = irq_desc_get_chip(desc);
  353. struct s3c64xx_eint_gpio_data *data = irq_desc_get_handler_data(desc);
  354. struct samsung_pinctrl_drv_data *drvdata = data->drvdata;
  355. chained_irq_enter(chip, desc);
  356. do {
  357. unsigned int svc;
  358. unsigned int group;
  359. unsigned int pin;
  360. unsigned int virq;
  361. svc = readl(drvdata->virt_base + SERVICE_REG);
  362. group = SVC_GROUP(svc);
  363. pin = svc & SVC_NUM_MASK;
  364. if (!group)
  365. break;
  366. /* Group 1 is used for two pin banks */
  367. if (group == 1) {
  368. if (pin < 8)
  369. group = 0;
  370. else
  371. pin -= 8;
  372. }
  373. virq = irq_linear_revmap(data->domains[group], pin);
  374. /*
  375. * Something must be really wrong if an unmapped EINT
  376. * was unmasked...
  377. */
  378. BUG_ON(!virq);
  379. generic_handle_irq(virq);
  380. } while (1);
  381. chained_irq_exit(chip, desc);
  382. }
  383. /**
  384. * s3c64xx_eint_gpio_init() - setup handling of external gpio interrupts.
  385. * @d: driver data of samsung pinctrl driver.
  386. */
  387. static int s3c64xx_eint_gpio_init(struct samsung_pinctrl_drv_data *d)
  388. {
  389. struct s3c64xx_eint_gpio_data *data;
  390. struct samsung_pin_bank *bank;
  391. struct device *dev = d->dev;
  392. unsigned int nr_domains;
  393. unsigned int i;
  394. if (!d->irq) {
  395. dev_err(dev, "irq number not available\n");
  396. return -EINVAL;
  397. }
  398. nr_domains = 0;
  399. bank = d->pin_banks;
  400. for (i = 0; i < d->nr_banks; ++i, ++bank) {
  401. unsigned int nr_eints;
  402. unsigned int mask;
  403. if (bank->eint_type != EINT_TYPE_GPIO)
  404. continue;
  405. mask = bank->eint_mask;
  406. nr_eints = fls(mask);
  407. bank->irq_domain = irq_domain_add_linear(bank->of_node,
  408. nr_eints, &s3c64xx_gpio_irqd_ops, bank);
  409. if (!bank->irq_domain) {
  410. dev_err(dev, "gpio irq domain add failed\n");
  411. return -ENXIO;
  412. }
  413. ++nr_domains;
  414. }
  415. data = devm_kzalloc(dev, sizeof(*data)
  416. + nr_domains * sizeof(*data->domains), GFP_KERNEL);
  417. if (!data)
  418. return -ENOMEM;
  419. data->drvdata = d;
  420. bank = d->pin_banks;
  421. nr_domains = 0;
  422. for (i = 0; i < d->nr_banks; ++i, ++bank) {
  423. if (bank->eint_type != EINT_TYPE_GPIO)
  424. continue;
  425. data->domains[nr_domains++] = bank->irq_domain;
  426. }
  427. irq_set_chained_handler_and_data(d->irq, s3c64xx_eint_gpio_irq, data);
  428. return 0;
  429. }
  430. /*
  431. * Functions for configuration of EINT0 wake-up interrupts
  432. */
  433. static inline void s3c64xx_eint0_irq_set_mask(struct irq_data *irqd, bool mask)
  434. {
  435. struct s3c64xx_eint0_domain_data *ddata =
  436. irq_data_get_irq_chip_data(irqd);
  437. struct samsung_pinctrl_drv_data *d = ddata->bank->drvdata;
  438. u32 val;
  439. val = readl(d->virt_base + EINT0MASK_REG);
  440. if (mask)
  441. val |= 1 << ddata->eints[irqd->hwirq];
  442. else
  443. val &= ~(1 << ddata->eints[irqd->hwirq]);
  444. writel(val, d->virt_base + EINT0MASK_REG);
  445. }
  446. static void s3c64xx_eint0_irq_unmask(struct irq_data *irqd)
  447. {
  448. s3c64xx_eint0_irq_set_mask(irqd, false);
  449. }
  450. static void s3c64xx_eint0_irq_mask(struct irq_data *irqd)
  451. {
  452. s3c64xx_eint0_irq_set_mask(irqd, true);
  453. }
  454. static void s3c64xx_eint0_irq_ack(struct irq_data *irqd)
  455. {
  456. struct s3c64xx_eint0_domain_data *ddata =
  457. irq_data_get_irq_chip_data(irqd);
  458. struct samsung_pinctrl_drv_data *d = ddata->bank->drvdata;
  459. writel(1 << ddata->eints[irqd->hwirq],
  460. d->virt_base + EINT0PEND_REG);
  461. }
  462. static int s3c64xx_eint0_irq_set_type(struct irq_data *irqd, unsigned int type)
  463. {
  464. struct s3c64xx_eint0_domain_data *ddata =
  465. irq_data_get_irq_chip_data(irqd);
  466. struct samsung_pin_bank *bank = ddata->bank;
  467. struct samsung_pinctrl_drv_data *d = bank->drvdata;
  468. void __iomem *reg;
  469. int trigger;
  470. u8 shift;
  471. u32 val;
  472. trigger = s3c64xx_irq_get_trigger(type);
  473. if (trigger < 0) {
  474. pr_err("unsupported external interrupt type\n");
  475. return -EINVAL;
  476. }
  477. s3c64xx_irq_set_handler(irqd, type);
  478. /* Set up interrupt trigger */
  479. reg = d->virt_base + EINT0CON0_REG;
  480. shift = ddata->eints[irqd->hwirq];
  481. if (shift >= EINT_MAX_PER_REG) {
  482. reg += 4;
  483. shift -= EINT_MAX_PER_REG;
  484. }
  485. shift = EINT_CON_LEN * (shift / 2);
  486. val = readl(reg);
  487. val &= ~(EINT_CON_MASK << shift);
  488. val |= trigger << shift;
  489. writel(val, reg);
  490. s3c64xx_irq_set_function(d, bank, irqd->hwirq);
  491. return 0;
  492. }
  493. /*
  494. * irq_chip for wakeup interrupts
  495. */
  496. static struct irq_chip s3c64xx_eint0_irq_chip = {
  497. .name = "EINT0",
  498. .irq_unmask = s3c64xx_eint0_irq_unmask,
  499. .irq_mask = s3c64xx_eint0_irq_mask,
  500. .irq_ack = s3c64xx_eint0_irq_ack,
  501. .irq_set_type = s3c64xx_eint0_irq_set_type,
  502. };
  503. static inline void s3c64xx_irq_demux_eint(struct irq_desc *desc, u32 range)
  504. {
  505. struct irq_chip *chip = irq_desc_get_chip(desc);
  506. struct s3c64xx_eint0_data *data = irq_desc_get_handler_data(desc);
  507. struct samsung_pinctrl_drv_data *drvdata = data->drvdata;
  508. unsigned int pend, mask;
  509. chained_irq_enter(chip, desc);
  510. pend = readl(drvdata->virt_base + EINT0PEND_REG);
  511. mask = readl(drvdata->virt_base + EINT0MASK_REG);
  512. pend = pend & range & ~mask;
  513. pend &= range;
  514. while (pend) {
  515. unsigned int virq, irq;
  516. irq = fls(pend) - 1;
  517. pend &= ~(1 << irq);
  518. virq = irq_linear_revmap(data->domains[irq], data->pins[irq]);
  519. /*
  520. * Something must be really wrong if an unmapped EINT
  521. * was unmasked...
  522. */
  523. BUG_ON(!virq);
  524. generic_handle_irq(virq);
  525. }
  526. chained_irq_exit(chip, desc);
  527. }
  528. static void s3c64xx_demux_eint0_3(struct irq_desc *desc)
  529. {
  530. s3c64xx_irq_demux_eint(desc, 0xf);
  531. }
  532. static void s3c64xx_demux_eint4_11(struct irq_desc *desc)
  533. {
  534. s3c64xx_irq_demux_eint(desc, 0xff0);
  535. }
  536. static void s3c64xx_demux_eint12_19(struct irq_desc *desc)
  537. {
  538. s3c64xx_irq_demux_eint(desc, 0xff000);
  539. }
  540. static void s3c64xx_demux_eint20_27(struct irq_desc *desc)
  541. {
  542. s3c64xx_irq_demux_eint(desc, 0xff00000);
  543. }
  544. static irq_flow_handler_t s3c64xx_eint0_handlers[NUM_EINT0_IRQ] = {
  545. s3c64xx_demux_eint0_3,
  546. s3c64xx_demux_eint4_11,
  547. s3c64xx_demux_eint12_19,
  548. s3c64xx_demux_eint20_27,
  549. };
  550. static int s3c64xx_eint0_irq_map(struct irq_domain *h, unsigned int virq,
  551. irq_hw_number_t hw)
  552. {
  553. struct s3c64xx_eint0_domain_data *ddata = h->host_data;
  554. struct samsung_pin_bank *bank = ddata->bank;
  555. if (!(bank->eint_mask & (1 << hw)))
  556. return -EINVAL;
  557. irq_set_chip_and_handler(virq,
  558. &s3c64xx_eint0_irq_chip, handle_level_irq);
  559. irq_set_chip_data(virq, ddata);
  560. return 0;
  561. }
  562. /*
  563. * irq domain callbacks for external wakeup interrupt controller.
  564. */
  565. static const struct irq_domain_ops s3c64xx_eint0_irqd_ops = {
  566. .map = s3c64xx_eint0_irq_map,
  567. .xlate = irq_domain_xlate_twocell,
  568. };
  569. /* list of external wakeup controllers supported */
  570. static const struct of_device_id s3c64xx_eint0_irq_ids[] = {
  571. { .compatible = "samsung,s3c64xx-wakeup-eint", },
  572. { }
  573. };
  574. /**
  575. * s3c64xx_eint_eint0_init() - setup handling of external wakeup interrupts.
  576. * @d: driver data of samsung pinctrl driver.
  577. */
  578. static int s3c64xx_eint_eint0_init(struct samsung_pinctrl_drv_data *d)
  579. {
  580. struct device *dev = d->dev;
  581. struct device_node *eint0_np = NULL;
  582. struct device_node *np;
  583. struct samsung_pin_bank *bank;
  584. struct s3c64xx_eint0_data *data;
  585. unsigned int i;
  586. for_each_child_of_node(dev->of_node, np) {
  587. if (of_match_node(s3c64xx_eint0_irq_ids, np)) {
  588. eint0_np = np;
  589. break;
  590. }
  591. }
  592. if (!eint0_np)
  593. return -ENODEV;
  594. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  595. if (!data) {
  596. of_node_put(eint0_np);
  597. return -ENOMEM;
  598. }
  599. data->drvdata = d;
  600. for (i = 0; i < NUM_EINT0_IRQ; ++i) {
  601. unsigned int irq;
  602. irq = irq_of_parse_and_map(eint0_np, i);
  603. if (!irq) {
  604. dev_err(dev, "failed to get wakeup EINT IRQ %d\n", i);
  605. of_node_put(eint0_np);
  606. return -ENXIO;
  607. }
  608. irq_set_chained_handler_and_data(irq,
  609. s3c64xx_eint0_handlers[i],
  610. data);
  611. }
  612. of_node_put(eint0_np);
  613. bank = d->pin_banks;
  614. for (i = 0; i < d->nr_banks; ++i, ++bank) {
  615. struct s3c64xx_eint0_domain_data *ddata;
  616. unsigned int nr_eints;
  617. unsigned int mask;
  618. unsigned int irq;
  619. unsigned int pin;
  620. if (bank->eint_type != EINT_TYPE_WKUP)
  621. continue;
  622. mask = bank->eint_mask;
  623. nr_eints = fls(mask);
  624. ddata = devm_kzalloc(dev,
  625. sizeof(*ddata) + nr_eints, GFP_KERNEL);
  626. if (!ddata)
  627. return -ENOMEM;
  628. ddata->bank = bank;
  629. bank->irq_domain = irq_domain_add_linear(bank->of_node,
  630. nr_eints, &s3c64xx_eint0_irqd_ops, ddata);
  631. if (!bank->irq_domain) {
  632. dev_err(dev, "wkup irq domain add failed\n");
  633. return -ENXIO;
  634. }
  635. irq = bank->eint_offset;
  636. mask = bank->eint_mask;
  637. for (pin = 0; mask; ++pin, mask >>= 1) {
  638. if (!(mask & 1))
  639. continue;
  640. data->domains[irq] = bank->irq_domain;
  641. data->pins[irq] = pin;
  642. ddata->eints[pin] = irq;
  643. ++irq;
  644. }
  645. }
  646. return 0;
  647. }
  648. /* pin banks of s3c64xx pin-controller 0 */
  649. static const struct samsung_pin_bank_data s3c64xx_pin_banks0[] __initconst = {
  650. PIN_BANK_4BIT_EINTG(8, 0x000, "gpa", 0),
  651. PIN_BANK_4BIT_EINTG(7, 0x020, "gpb", 8),
  652. PIN_BANK_4BIT_EINTG(8, 0x040, "gpc", 16),
  653. PIN_BANK_4BIT_EINTG(5, 0x060, "gpd", 32),
  654. PIN_BANK_4BIT(5, 0x080, "gpe"),
  655. PIN_BANK_2BIT_EINTG(16, 0x0a0, "gpf", 48, 0x3fff),
  656. PIN_BANK_4BIT_EINTG(7, 0x0c0, "gpg", 64),
  657. PIN_BANK_4BIT2_EINTG(10, 0x0e0, "gph", 80),
  658. PIN_BANK_2BIT(16, 0x100, "gpi"),
  659. PIN_BANK_2BIT(12, 0x120, "gpj"),
  660. PIN_BANK_4BIT2_ALIVE(16, 0x800, "gpk"),
  661. PIN_BANK_4BIT2_EINTW(15, 0x810, "gpl", 16, 0x7f00),
  662. PIN_BANK_4BIT_EINTW(6, 0x820, "gpm", 23, 0x1f),
  663. PIN_BANK_2BIT_EINTW(16, 0x830, "gpn", 0),
  664. PIN_BANK_2BIT_EINTG(16, 0x140, "gpo", 96, 0xffff),
  665. PIN_BANK_2BIT_EINTG(15, 0x160, "gpp", 112, 0x7fff),
  666. PIN_BANK_2BIT_EINTG(9, 0x180, "gpq", 128, 0x1ff),
  667. };
  668. /*
  669. * Samsung pinctrl driver data for S3C64xx SoC. S3C64xx SoC includes
  670. * one gpio/pin-mux/pinconfig controller.
  671. */
  672. static const struct samsung_pin_ctrl s3c64xx_pin_ctrl[] __initconst = {
  673. {
  674. /* pin-controller instance 1 data */
  675. .pin_banks = s3c64xx_pin_banks0,
  676. .nr_banks = ARRAY_SIZE(s3c64xx_pin_banks0),
  677. .eint_gpio_init = s3c64xx_eint_gpio_init,
  678. .eint_wkup_init = s3c64xx_eint_eint0_init,
  679. },
  680. };
  681. const struct samsung_pinctrl_of_match_data s3c64xx_of_data __initconst = {
  682. .ctrl = s3c64xx_pin_ctrl,
  683. .num_ctrl = ARRAY_SIZE(s3c64xx_pin_ctrl),
  684. };