irq-aspeed-i2c-ic.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Aspeed 24XX/25XX I2C Interrupt Controller.
  3. *
  4. * Copyright (C) 2012-2017 ASPEED Technology Inc.
  5. * Copyright 2017 IBM Corporation
  6. * Copyright 2017 Google, Inc.
  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 version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/irq.h>
  13. #include <linux/irqchip.h>
  14. #include <linux/irqchip/chained_irq.h>
  15. #include <linux/irqdomain.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/io.h>
  19. #define ASPEED_I2C_IC_NUM_BUS 14
  20. struct aspeed_i2c_ic {
  21. void __iomem *base;
  22. int parent_irq;
  23. struct irq_domain *irq_domain;
  24. };
  25. /*
  26. * The aspeed chip provides a single hardware interrupt for all of the I2C
  27. * busses, so we use a dummy interrupt chip to translate this single interrupt
  28. * into multiple interrupts, each associated with a single I2C bus.
  29. */
  30. static void aspeed_i2c_ic_irq_handler(struct irq_desc *desc)
  31. {
  32. struct aspeed_i2c_ic *i2c_ic = irq_desc_get_handler_data(desc);
  33. struct irq_chip *chip = irq_desc_get_chip(desc);
  34. unsigned long bit, status;
  35. unsigned int bus_irq;
  36. chained_irq_enter(chip, desc);
  37. status = readl(i2c_ic->base);
  38. for_each_set_bit(bit, &status, ASPEED_I2C_IC_NUM_BUS) {
  39. bus_irq = irq_find_mapping(i2c_ic->irq_domain, bit);
  40. generic_handle_irq(bus_irq);
  41. }
  42. chained_irq_exit(chip, desc);
  43. }
  44. /*
  45. * Set simple handler and mark IRQ as valid. Nothing interesting to do here
  46. * since we are using a dummy interrupt chip.
  47. */
  48. static int aspeed_i2c_ic_map_irq_domain(struct irq_domain *domain,
  49. unsigned int irq, irq_hw_number_t hwirq)
  50. {
  51. irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
  52. irq_set_chip_data(irq, domain->host_data);
  53. return 0;
  54. }
  55. static const struct irq_domain_ops aspeed_i2c_ic_irq_domain_ops = {
  56. .map = aspeed_i2c_ic_map_irq_domain,
  57. };
  58. static int __init aspeed_i2c_ic_of_init(struct device_node *node,
  59. struct device_node *parent)
  60. {
  61. struct aspeed_i2c_ic *i2c_ic;
  62. int ret = 0;
  63. i2c_ic = kzalloc(sizeof(*i2c_ic), GFP_KERNEL);
  64. if (!i2c_ic)
  65. return -ENOMEM;
  66. i2c_ic->base = of_iomap(node, 0);
  67. if (!i2c_ic->base) {
  68. ret = -ENOMEM;
  69. goto err_free_ic;
  70. }
  71. i2c_ic->parent_irq = irq_of_parse_and_map(node, 0);
  72. if (i2c_ic->parent_irq < 0) {
  73. ret = i2c_ic->parent_irq;
  74. goto err_iounmap;
  75. }
  76. i2c_ic->irq_domain = irq_domain_add_linear(node, ASPEED_I2C_IC_NUM_BUS,
  77. &aspeed_i2c_ic_irq_domain_ops,
  78. NULL);
  79. if (!i2c_ic->irq_domain) {
  80. ret = -ENOMEM;
  81. goto err_iounmap;
  82. }
  83. i2c_ic->irq_domain->name = "aspeed-i2c-domain";
  84. irq_set_chained_handler_and_data(i2c_ic->parent_irq,
  85. aspeed_i2c_ic_irq_handler, i2c_ic);
  86. pr_info("i2c controller registered, irq %d\n", i2c_ic->parent_irq);
  87. return 0;
  88. err_iounmap:
  89. iounmap(i2c_ic->base);
  90. err_free_ic:
  91. kfree(i2c_ic);
  92. return ret;
  93. }
  94. IRQCHIP_DECLARE(ast2400_i2c_ic, "aspeed,ast2400-i2c-ic", aspeed_i2c_ic_of_init);
  95. IRQCHIP_DECLARE(ast2500_i2c_ic, "aspeed,ast2500-i2c-ic", aspeed_i2c_ic_of_init);