Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Multiplexed-IRQs driver for TS-4800's FPGA
0003  *
0004  * Copyright (c) 2015 - Savoir-faire Linux
0005  *
0006  * This file is licensed under the terms of the GNU General Public
0007  * License version 2. This program is licensed "as is" without any
0008  * warranty of any kind, whether express or implied.
0009  */
0010 
0011 #include <linux/interrupt.h>
0012 #include <linux/io.h>
0013 #include <linux/irq.h>
0014 #include <linux/irqchip.h>
0015 #include <linux/irqchip/chained_irq.h>
0016 #include <linux/irqdomain.h>
0017 #include <linux/module.h>
0018 #include <linux/of.h>
0019 #include <linux/of_address.h>
0020 #include <linux/of_irq.h>
0021 #include <linux/platform_device.h>
0022 #include <linux/seq_file.h>
0023 
0024 #define IRQ_MASK        0x4
0025 #define IRQ_STATUS      0x8
0026 
0027 struct ts4800_irq_data {
0028     void __iomem            *base;
0029     struct platform_device  *pdev;
0030     struct irq_domain       *domain;
0031 };
0032 
0033 static void ts4800_irq_mask(struct irq_data *d)
0034 {
0035     struct ts4800_irq_data *data = irq_data_get_irq_chip_data(d);
0036     u16 reg = readw(data->base + IRQ_MASK);
0037     u16 mask = 1 << d->hwirq;
0038 
0039     writew(reg | mask, data->base + IRQ_MASK);
0040 }
0041 
0042 static void ts4800_irq_unmask(struct irq_data *d)
0043 {
0044     struct ts4800_irq_data *data = irq_data_get_irq_chip_data(d);
0045     u16 reg = readw(data->base + IRQ_MASK);
0046     u16 mask = 1 << d->hwirq;
0047 
0048     writew(reg & ~mask, data->base + IRQ_MASK);
0049 }
0050 
0051 static void ts4800_irq_print_chip(struct irq_data *d, struct seq_file *p)
0052 {
0053     struct ts4800_irq_data *data = irq_data_get_irq_chip_data(d);
0054 
0055     seq_printf(p, "%s", dev_name(&data->pdev->dev));
0056 }
0057 
0058 static const struct irq_chip ts4800_chip = {
0059     .irq_mask   = ts4800_irq_mask,
0060     .irq_unmask = ts4800_irq_unmask,
0061     .irq_print_chip = ts4800_irq_print_chip,
0062 };
0063 
0064 static int ts4800_irqdomain_map(struct irq_domain *d, unsigned int irq,
0065                 irq_hw_number_t hwirq)
0066 {
0067     struct ts4800_irq_data *data = d->host_data;
0068 
0069     irq_set_chip_and_handler(irq, &ts4800_chip, handle_simple_irq);
0070     irq_set_chip_data(irq, data);
0071     irq_set_noprobe(irq);
0072 
0073     return 0;
0074 }
0075 
0076 static const struct irq_domain_ops ts4800_ic_ops = {
0077     .map = ts4800_irqdomain_map,
0078     .xlate = irq_domain_xlate_onecell,
0079 };
0080 
0081 static void ts4800_ic_chained_handle_irq(struct irq_desc *desc)
0082 {
0083     struct ts4800_irq_data *data = irq_desc_get_handler_data(desc);
0084     struct irq_chip *chip = irq_desc_get_chip(desc);
0085     u16 status = readw(data->base + IRQ_STATUS);
0086 
0087     chained_irq_enter(chip, desc);
0088 
0089     if (unlikely(status == 0)) {
0090         handle_bad_irq(desc);
0091         goto out;
0092     }
0093 
0094     do {
0095         unsigned int bit = __ffs(status);
0096 
0097         generic_handle_domain_irq(data->domain, bit);
0098         status &= ~(1 << bit);
0099     } while (status);
0100 
0101 out:
0102     chained_irq_exit(chip, desc);
0103 }
0104 
0105 static int ts4800_ic_probe(struct platform_device *pdev)
0106 {
0107     struct device_node *node = pdev->dev.of_node;
0108     struct ts4800_irq_data *data;
0109     int parent_irq;
0110 
0111     data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
0112     if (!data)
0113         return -ENOMEM;
0114 
0115     data->pdev = pdev;
0116     data->base = devm_platform_ioremap_resource(pdev, 0);
0117     if (IS_ERR(data->base))
0118         return PTR_ERR(data->base);
0119 
0120     writew(0xFFFF, data->base + IRQ_MASK);
0121 
0122     parent_irq = irq_of_parse_and_map(node, 0);
0123     if (!parent_irq) {
0124         dev_err(&pdev->dev, "failed to get parent IRQ\n");
0125         return -EINVAL;
0126     }
0127 
0128     data->domain = irq_domain_add_linear(node, 8, &ts4800_ic_ops, data);
0129     if (!data->domain) {
0130         dev_err(&pdev->dev, "cannot add IRQ domain\n");
0131         return -ENOMEM;
0132     }
0133 
0134     irq_set_chained_handler_and_data(parent_irq,
0135                      ts4800_ic_chained_handle_irq, data);
0136 
0137     platform_set_drvdata(pdev, data);
0138 
0139     return 0;
0140 }
0141 
0142 static int ts4800_ic_remove(struct platform_device *pdev)
0143 {
0144     struct ts4800_irq_data *data = platform_get_drvdata(pdev);
0145 
0146     irq_domain_remove(data->domain);
0147 
0148     return 0;
0149 }
0150 
0151 static const struct of_device_id ts4800_ic_of_match[] = {
0152     { .compatible = "technologic,ts4800-irqc", },
0153     {},
0154 };
0155 MODULE_DEVICE_TABLE(of, ts4800_ic_of_match);
0156 
0157 static struct platform_driver ts4800_ic_driver = {
0158     .probe  = ts4800_ic_probe,
0159     .remove = ts4800_ic_remove,
0160     .driver = {
0161         .name = "ts4800-irqc",
0162         .of_match_table = ts4800_ic_of_match,
0163     },
0164 };
0165 module_platform_driver(ts4800_ic_driver);
0166 
0167 MODULE_AUTHOR("Damien Riegel <damien.riegel@savoirfairelinux.com>");
0168 MODULE_LICENSE("GPL v2");
0169 MODULE_ALIAS("platform:ts4800_irqc");