gpio-etraxfs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. #include <linux/kernel.h>
  2. #include <linux/init.h>
  3. #include <linux/gpio.h>
  4. #include <linux/gpio/driver.h>
  5. #include <linux/of_gpio.h>
  6. #include <linux/io.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/basic_mmio_gpio.h>
  10. #define ETRAX_FS_rw_pa_dout 0
  11. #define ETRAX_FS_r_pa_din 4
  12. #define ETRAX_FS_rw_pa_oe 8
  13. #define ETRAX_FS_rw_intr_cfg 12
  14. #define ETRAX_FS_rw_intr_mask 16
  15. #define ETRAX_FS_rw_ack_intr 20
  16. #define ETRAX_FS_r_intr 24
  17. #define ETRAX_FS_r_masked_intr 28
  18. #define ETRAX_FS_rw_pb_dout 32
  19. #define ETRAX_FS_r_pb_din 36
  20. #define ETRAX_FS_rw_pb_oe 40
  21. #define ETRAX_FS_rw_pc_dout 48
  22. #define ETRAX_FS_r_pc_din 52
  23. #define ETRAX_FS_rw_pc_oe 56
  24. #define ETRAX_FS_rw_pd_dout 64
  25. #define ETRAX_FS_r_pd_din 68
  26. #define ETRAX_FS_rw_pd_oe 72
  27. #define ETRAX_FS_rw_pe_dout 80
  28. #define ETRAX_FS_r_pe_din 84
  29. #define ETRAX_FS_rw_pe_oe 88
  30. #define ARTPEC3_r_pa_din 0
  31. #define ARTPEC3_rw_pa_dout 4
  32. #define ARTPEC3_rw_pa_oe 8
  33. #define ARTPEC3_r_pb_din 44
  34. #define ARTPEC3_rw_pb_dout 48
  35. #define ARTPEC3_rw_pb_oe 52
  36. #define ARTPEC3_r_pc_din 88
  37. #define ARTPEC3_rw_pc_dout 92
  38. #define ARTPEC3_rw_pc_oe 96
  39. #define ARTPEC3_r_pd_din 116
  40. #define ARTPEC3_rw_intr_cfg 120
  41. #define ARTPEC3_rw_intr_pins 124
  42. #define ARTPEC3_rw_intr_mask 128
  43. #define ARTPEC3_rw_ack_intr 132
  44. #define ARTPEC3_r_masked_intr 140
  45. #define GIO_CFG_OFF 0
  46. #define GIO_CFG_HI 1
  47. #define GIO_CFG_LO 2
  48. #define GIO_CFG_SET 3
  49. #define GIO_CFG_POSEDGE 5
  50. #define GIO_CFG_NEGEDGE 6
  51. #define GIO_CFG_ANYEDGE 7
  52. struct etraxfs_gpio_info;
  53. struct etraxfs_gpio_block {
  54. spinlock_t lock;
  55. u32 mask;
  56. u32 cfg;
  57. u32 pins;
  58. unsigned int group[8];
  59. void __iomem *regs;
  60. const struct etraxfs_gpio_info *info;
  61. };
  62. struct etraxfs_gpio_chip {
  63. struct bgpio_chip bgc;
  64. struct etraxfs_gpio_block *block;
  65. };
  66. struct etraxfs_gpio_port {
  67. const char *label;
  68. unsigned int oe;
  69. unsigned int dout;
  70. unsigned int din;
  71. unsigned int ngpio;
  72. };
  73. struct etraxfs_gpio_info {
  74. unsigned int num_ports;
  75. const struct etraxfs_gpio_port *ports;
  76. unsigned int rw_ack_intr;
  77. unsigned int rw_intr_mask;
  78. unsigned int rw_intr_cfg;
  79. unsigned int rw_intr_pins;
  80. unsigned int r_masked_intr;
  81. };
  82. static const struct etraxfs_gpio_port etraxfs_gpio_etraxfs_ports[] = {
  83. {
  84. .label = "A",
  85. .ngpio = 8,
  86. .oe = ETRAX_FS_rw_pa_oe,
  87. .dout = ETRAX_FS_rw_pa_dout,
  88. .din = ETRAX_FS_r_pa_din,
  89. },
  90. {
  91. .label = "B",
  92. .ngpio = 18,
  93. .oe = ETRAX_FS_rw_pb_oe,
  94. .dout = ETRAX_FS_rw_pb_dout,
  95. .din = ETRAX_FS_r_pb_din,
  96. },
  97. {
  98. .label = "C",
  99. .ngpio = 18,
  100. .oe = ETRAX_FS_rw_pc_oe,
  101. .dout = ETRAX_FS_rw_pc_dout,
  102. .din = ETRAX_FS_r_pc_din,
  103. },
  104. {
  105. .label = "D",
  106. .ngpio = 18,
  107. .oe = ETRAX_FS_rw_pd_oe,
  108. .dout = ETRAX_FS_rw_pd_dout,
  109. .din = ETRAX_FS_r_pd_din,
  110. },
  111. {
  112. .label = "E",
  113. .ngpio = 18,
  114. .oe = ETRAX_FS_rw_pe_oe,
  115. .dout = ETRAX_FS_rw_pe_dout,
  116. .din = ETRAX_FS_r_pe_din,
  117. },
  118. };
  119. static const struct etraxfs_gpio_info etraxfs_gpio_etraxfs = {
  120. .num_ports = ARRAY_SIZE(etraxfs_gpio_etraxfs_ports),
  121. .ports = etraxfs_gpio_etraxfs_ports,
  122. .rw_ack_intr = ETRAX_FS_rw_ack_intr,
  123. .rw_intr_mask = ETRAX_FS_rw_intr_mask,
  124. .rw_intr_cfg = ETRAX_FS_rw_intr_cfg,
  125. .r_masked_intr = ETRAX_FS_r_masked_intr,
  126. };
  127. static const struct etraxfs_gpio_port etraxfs_gpio_artpec3_ports[] = {
  128. {
  129. .label = "A",
  130. .ngpio = 32,
  131. .oe = ARTPEC3_rw_pa_oe,
  132. .dout = ARTPEC3_rw_pa_dout,
  133. .din = ARTPEC3_r_pa_din,
  134. },
  135. {
  136. .label = "B",
  137. .ngpio = 32,
  138. .oe = ARTPEC3_rw_pb_oe,
  139. .dout = ARTPEC3_rw_pb_dout,
  140. .din = ARTPEC3_r_pb_din,
  141. },
  142. {
  143. .label = "C",
  144. .ngpio = 16,
  145. .oe = ARTPEC3_rw_pc_oe,
  146. .dout = ARTPEC3_rw_pc_dout,
  147. .din = ARTPEC3_r_pc_din,
  148. },
  149. {
  150. .label = "D",
  151. .ngpio = 32,
  152. .din = ARTPEC3_r_pd_din,
  153. },
  154. };
  155. static const struct etraxfs_gpio_info etraxfs_gpio_artpec3 = {
  156. .num_ports = ARRAY_SIZE(etraxfs_gpio_artpec3_ports),
  157. .ports = etraxfs_gpio_artpec3_ports,
  158. .rw_ack_intr = ARTPEC3_rw_ack_intr,
  159. .rw_intr_mask = ARTPEC3_rw_intr_mask,
  160. .rw_intr_cfg = ARTPEC3_rw_intr_cfg,
  161. .r_masked_intr = ARTPEC3_r_masked_intr,
  162. .rw_intr_pins = ARTPEC3_rw_intr_pins,
  163. };
  164. static struct etraxfs_gpio_chip *to_etraxfs(struct gpio_chip *gc)
  165. {
  166. return container_of(gc, struct etraxfs_gpio_chip, bgc.gc);
  167. }
  168. static unsigned int etraxfs_gpio_chip_to_port(struct gpio_chip *gc)
  169. {
  170. return gc->label[0] - 'A';
  171. }
  172. static int etraxfs_gpio_of_xlate(struct gpio_chip *gc,
  173. const struct of_phandle_args *gpiospec,
  174. u32 *flags)
  175. {
  176. /*
  177. * Port numbers are A to E, and the properties are integers, so we
  178. * specify them as 0xA - 0xE.
  179. */
  180. if (etraxfs_gpio_chip_to_port(gc) + 0xA != gpiospec->args[2])
  181. return -EINVAL;
  182. return of_gpio_simple_xlate(gc, gpiospec, flags);
  183. }
  184. static const struct of_device_id etraxfs_gpio_of_table[] = {
  185. {
  186. .compatible = "axis,etraxfs-gio",
  187. .data = &etraxfs_gpio_etraxfs,
  188. },
  189. {
  190. .compatible = "axis,artpec3-gio",
  191. .data = &etraxfs_gpio_artpec3,
  192. },
  193. {},
  194. };
  195. static unsigned int etraxfs_gpio_to_group_irq(unsigned int gpio)
  196. {
  197. return gpio % 8;
  198. }
  199. static unsigned int etraxfs_gpio_to_group_pin(struct etraxfs_gpio_chip *chip,
  200. unsigned int gpio)
  201. {
  202. return 4 * etraxfs_gpio_chip_to_port(&chip->bgc.gc) + gpio / 8;
  203. }
  204. static void etraxfs_gpio_irq_ack(struct irq_data *d)
  205. {
  206. struct etraxfs_gpio_chip *chip =
  207. to_etraxfs(irq_data_get_irq_chip_data(d));
  208. struct etraxfs_gpio_block *block = chip->block;
  209. unsigned int grpirq = etraxfs_gpio_to_group_irq(d->hwirq);
  210. writel(BIT(grpirq), block->regs + block->info->rw_ack_intr);
  211. }
  212. static void etraxfs_gpio_irq_mask(struct irq_data *d)
  213. {
  214. struct etraxfs_gpio_chip *chip =
  215. to_etraxfs(irq_data_get_irq_chip_data(d));
  216. struct etraxfs_gpio_block *block = chip->block;
  217. unsigned int grpirq = etraxfs_gpio_to_group_irq(d->hwirq);
  218. spin_lock(&block->lock);
  219. block->mask &= ~BIT(grpirq);
  220. writel(block->mask, block->regs + block->info->rw_intr_mask);
  221. spin_unlock(&block->lock);
  222. }
  223. static void etraxfs_gpio_irq_unmask(struct irq_data *d)
  224. {
  225. struct etraxfs_gpio_chip *chip =
  226. to_etraxfs(irq_data_get_irq_chip_data(d));
  227. struct etraxfs_gpio_block *block = chip->block;
  228. unsigned int grpirq = etraxfs_gpio_to_group_irq(d->hwirq);
  229. spin_lock(&block->lock);
  230. block->mask |= BIT(grpirq);
  231. writel(block->mask, block->regs + block->info->rw_intr_mask);
  232. spin_unlock(&block->lock);
  233. }
  234. static int etraxfs_gpio_irq_set_type(struct irq_data *d, u32 type)
  235. {
  236. struct etraxfs_gpio_chip *chip =
  237. to_etraxfs(irq_data_get_irq_chip_data(d));
  238. struct etraxfs_gpio_block *block = chip->block;
  239. unsigned int grpirq = etraxfs_gpio_to_group_irq(d->hwirq);
  240. u32 cfg;
  241. switch (type) {
  242. case IRQ_TYPE_EDGE_RISING:
  243. cfg = GIO_CFG_POSEDGE;
  244. break;
  245. case IRQ_TYPE_EDGE_FALLING:
  246. cfg = GIO_CFG_NEGEDGE;
  247. break;
  248. case IRQ_TYPE_EDGE_BOTH:
  249. cfg = GIO_CFG_ANYEDGE;
  250. break;
  251. case IRQ_TYPE_LEVEL_LOW:
  252. cfg = GIO_CFG_LO;
  253. break;
  254. case IRQ_TYPE_LEVEL_HIGH:
  255. cfg = GIO_CFG_HI;
  256. break;
  257. default:
  258. return -EINVAL;
  259. }
  260. spin_lock(&block->lock);
  261. block->cfg &= ~(0x7 << (grpirq * 3));
  262. block->cfg |= (cfg << (grpirq * 3));
  263. writel(block->cfg, block->regs + block->info->rw_intr_cfg);
  264. spin_unlock(&block->lock);
  265. return 0;
  266. }
  267. static int etraxfs_gpio_irq_request_resources(struct irq_data *d)
  268. {
  269. struct etraxfs_gpio_chip *chip =
  270. to_etraxfs(irq_data_get_irq_chip_data(d));
  271. struct etraxfs_gpio_block *block = chip->block;
  272. unsigned int grpirq = etraxfs_gpio_to_group_irq(d->hwirq);
  273. int ret = -EBUSY;
  274. spin_lock(&block->lock);
  275. if (block->group[grpirq])
  276. goto out;
  277. ret = gpiochip_lock_as_irq(&chip->bgc.gc, d->hwirq);
  278. if (ret)
  279. goto out;
  280. block->group[grpirq] = d->irq;
  281. if (block->info->rw_intr_pins) {
  282. unsigned int pin = etraxfs_gpio_to_group_pin(chip, d->hwirq);
  283. block->pins &= ~(0xf << (grpirq * 4));
  284. block->pins |= (pin << (grpirq * 4));
  285. writel(block->pins, block->regs + block->info->rw_intr_pins);
  286. }
  287. out:
  288. spin_unlock(&block->lock);
  289. return ret;
  290. }
  291. static void etraxfs_gpio_irq_release_resources(struct irq_data *d)
  292. {
  293. struct etraxfs_gpio_chip *chip =
  294. to_etraxfs(irq_data_get_irq_chip_data(d));
  295. struct etraxfs_gpio_block *block = chip->block;
  296. unsigned int grpirq = etraxfs_gpio_to_group_irq(d->hwirq);
  297. spin_lock(&block->lock);
  298. block->group[grpirq] = 0;
  299. gpiochip_unlock_as_irq(&chip->bgc.gc, d->hwirq);
  300. spin_unlock(&block->lock);
  301. }
  302. static struct irq_chip etraxfs_gpio_irq_chip = {
  303. .name = "gpio-etraxfs",
  304. .irq_ack = etraxfs_gpio_irq_ack,
  305. .irq_mask = etraxfs_gpio_irq_mask,
  306. .irq_unmask = etraxfs_gpio_irq_unmask,
  307. .irq_set_type = etraxfs_gpio_irq_set_type,
  308. .irq_request_resources = etraxfs_gpio_irq_request_resources,
  309. .irq_release_resources = etraxfs_gpio_irq_release_resources,
  310. };
  311. static irqreturn_t etraxfs_gpio_interrupt(int irq, void *dev_id)
  312. {
  313. struct etraxfs_gpio_block *block = dev_id;
  314. unsigned long intr = readl(block->regs + block->info->r_masked_intr);
  315. int bit;
  316. for_each_set_bit(bit, &intr, 8)
  317. generic_handle_irq(block->group[bit]);
  318. return IRQ_RETVAL(intr & 0xff);
  319. }
  320. static int etraxfs_gpio_probe(struct platform_device *pdev)
  321. {
  322. struct device *dev = &pdev->dev;
  323. const struct etraxfs_gpio_info *info;
  324. const struct of_device_id *match;
  325. struct etraxfs_gpio_block *block;
  326. struct etraxfs_gpio_chip *chips;
  327. struct resource *res, *irq;
  328. bool allportsirq = false;
  329. void __iomem *regs;
  330. int ret;
  331. int i;
  332. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  333. regs = devm_ioremap_resource(dev, res);
  334. if (IS_ERR(regs))
  335. return PTR_ERR(regs);
  336. match = of_match_node(etraxfs_gpio_of_table, dev->of_node);
  337. if (!match)
  338. return -EINVAL;
  339. info = match->data;
  340. chips = devm_kzalloc(dev, sizeof(*chips) * info->num_ports, GFP_KERNEL);
  341. if (!chips)
  342. return -ENOMEM;
  343. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  344. if (!irq)
  345. return -EINVAL;
  346. block = devm_kzalloc(dev, sizeof(*block), GFP_KERNEL);
  347. if (!block)
  348. return -ENOMEM;
  349. spin_lock_init(&block->lock);
  350. block->regs = regs;
  351. block->info = info;
  352. writel(0, block->regs + info->rw_intr_mask);
  353. writel(0, block->regs + info->rw_intr_cfg);
  354. if (info->rw_intr_pins) {
  355. allportsirq = true;
  356. writel(0, block->regs + info->rw_intr_pins);
  357. }
  358. ret = devm_request_irq(dev, irq->start, etraxfs_gpio_interrupt,
  359. IRQF_SHARED, dev_name(dev), block);
  360. if (ret) {
  361. dev_err(dev, "Unable to request irq %d\n", ret);
  362. return ret;
  363. }
  364. for (i = 0; i < info->num_ports; i++) {
  365. struct etraxfs_gpio_chip *chip = &chips[i];
  366. struct bgpio_chip *bgc = &chip->bgc;
  367. const struct etraxfs_gpio_port *port = &info->ports[i];
  368. unsigned long flags = BGPIOF_READ_OUTPUT_REG_SET;
  369. void __iomem *dat = regs + port->din;
  370. void __iomem *set = regs + port->dout;
  371. void __iomem *dirout = regs + port->oe;
  372. chip->block = block;
  373. if (dirout == set) {
  374. dirout = set = NULL;
  375. flags = BGPIOF_NO_OUTPUT;
  376. }
  377. ret = bgpio_init(bgc, dev, 4,
  378. dat, set, NULL, dirout, NULL,
  379. flags);
  380. if (ret) {
  381. dev_err(dev, "Unable to init port %s\n",
  382. port->label);
  383. continue;
  384. }
  385. bgc->gc.ngpio = port->ngpio;
  386. bgc->gc.label = port->label;
  387. bgc->gc.of_node = dev->of_node;
  388. bgc->gc.of_gpio_n_cells = 3;
  389. bgc->gc.of_xlate = etraxfs_gpio_of_xlate;
  390. ret = gpiochip_add(&bgc->gc);
  391. if (ret) {
  392. dev_err(dev, "Unable to register port %s\n",
  393. bgc->gc.label);
  394. continue;
  395. }
  396. if (i > 0 && !allportsirq)
  397. continue;
  398. ret = gpiochip_irqchip_add(&bgc->gc, &etraxfs_gpio_irq_chip, 0,
  399. handle_level_irq, IRQ_TYPE_NONE);
  400. if (ret) {
  401. dev_err(dev, "Unable to add irqchip to port %s\n",
  402. bgc->gc.label);
  403. }
  404. }
  405. return 0;
  406. }
  407. static struct platform_driver etraxfs_gpio_driver = {
  408. .driver = {
  409. .name = "etraxfs-gpio",
  410. .of_match_table = of_match_ptr(etraxfs_gpio_of_table),
  411. },
  412. .probe = etraxfs_gpio_probe,
  413. };
  414. static int __init etraxfs_gpio_init(void)
  415. {
  416. return platform_driver_register(&etraxfs_gpio_driver);
  417. }
  418. device_initcall(etraxfs_gpio_init);