gpio-etraxfs.c 11 KB

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