]> rtime.felk.cvut.cz Git - zynq/linux.git/blob - drivers/media/platform/xilinx/xilinx-scenechange.c
v4l: xilinx: scd: Request IRQ after completing initialization
[zynq/linux.git] / drivers / media / platform / xilinx / xilinx-scenechange.c
1 //SPDX-License-Identifier: GPL-2.0
2 /*
3  * Xilinx Scene Change Detection driver
4  *
5  * Copyright (C) 2018 Xilinx, Inc.
6  *
7  * Authors: Anand Ashok Dumbre <anand.ashok.dumbre@xilinx.com>
8  *          Satish Kumar Nagireddy <satish.nagireddy.nagireddy@xilinx.com>
9  */
10
11 #include "xilinx-scenechange.h"
12
13 #define XSCD_RESET_DEASSERT     (0)
14 #define XSCD_RESET_ASSERT       (1)
15
16 static irqreturn_t xscd_irq_handler(int irq, void *data)
17 {
18         struct xscd_device *xscd = (struct xscd_device *)data;
19         unsigned int i;
20         u32 status;
21
22         status = xscd_read(xscd->iomem, XSCD_ISR_OFFSET);
23         if (!(status & XSCD_IE_AP_DONE))
24                 return IRQ_NONE;
25
26         xscd_write(xscd->iomem, XSCD_ISR_OFFSET, XSCD_IE_AP_DONE);
27
28         for (i = 0; i < xscd->num_streams; ++i)
29                 xscd_chan_irq_handler(xscd->chans[i]);
30
31         xscd_dma_irq_handler(xscd);
32
33         return IRQ_HANDLED;
34 }
35
36 static int xscd_init_resources(struct xscd_device *xscd)
37 {
38         struct platform_device *pdev = to_platform_device(xscd->dev);
39         struct resource *res;
40
41         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
42         xscd->iomem = devm_ioremap_resource(xscd->dev, res);
43         if (IS_ERR(xscd->iomem))
44                 return PTR_ERR(xscd->iomem);
45
46         xscd->irq = platform_get_irq(pdev, 0);
47         if (xscd->irq < 0) {
48                 dev_err(xscd->dev, "No valid irq found\n");
49                 return -EINVAL;
50         }
51
52         xscd->clk = devm_clk_get(xscd->dev, NULL);
53         if (IS_ERR(xscd->clk))
54                 return PTR_ERR(xscd->clk);
55
56         clk_prepare_enable(xscd->clk);
57         return 0;
58 }
59
60 static int xscd_parse_of(struct xscd_device *xscd)
61 {
62         struct device *dev = xscd->dev;
63         struct device_node *node = xscd->dev->of_node;
64         int ret;
65
66         xscd->memory_based = of_property_read_bool(node, "xlnx,memorybased");
67         xscd->rst_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
68         if (IS_ERR(xscd->rst_gpio)) {
69                 if (PTR_ERR(xscd->rst_gpio) != -EPROBE_DEFER)
70                         dev_err(dev, "Reset GPIO not setup in DT\n");
71
72                 return PTR_ERR(xscd->rst_gpio);
73         }
74
75         ret = of_property_read_u32(node, "xlnx,numstreams",
76                                    &xscd->num_streams);
77         if (ret < 0)
78                 return ret;
79
80         if (!xscd->memory_based && xscd->num_streams != 1) {
81                 dev_err(dev, "Stream-based mode only supports one stream\n");
82                 return -EINVAL;
83         }
84
85         return 0;
86 }
87
88 static int xscd_probe(struct platform_device *pdev)
89 {
90         struct xscd_device *xscd;
91         struct device_node *subdev_node;
92         unsigned int id;
93         int ret;
94
95         xscd = devm_kzalloc(&pdev->dev, sizeof(*xscd), GFP_KERNEL);
96         if (!xscd)
97                 return -ENOMEM;
98
99         xscd->dev = &pdev->dev;
100         platform_set_drvdata(pdev, xscd);
101
102         ret = xscd_parse_of(xscd);
103         if (ret < 0)
104                 return ret;
105
106         ret = xscd_init_resources(xscd);
107         if (ret < 0)
108                 return ret;
109
110         /* Reset Scene Change Detection IP */
111         gpiod_set_value_cansleep(xscd->rst_gpio, XSCD_RESET_ASSERT);
112         gpiod_set_value_cansleep(xscd->rst_gpio, XSCD_RESET_DEASSERT);
113
114         id = 0;
115         for_each_child_of_node(xscd->dev->of_node, subdev_node) {
116                 if (id >= xscd->num_streams) {
117                         dev_warn(&pdev->dev,
118                                  "Too many channels, limiting to %u\n",
119                                  xscd->num_streams);
120                         of_node_put(subdev_node);
121                         break;
122                 }
123
124                 ret = xscd_chan_init(xscd, id, subdev_node);
125                 if (ret < 0) {
126                         dev_err(&pdev->dev, "Failed to initialize channel %u\n",
127                                 id);
128                         return ret;
129                 }
130
131                 id++;
132         }
133
134         ret = xscd_dma_init(xscd);
135         if (ret < 0)
136                 dev_err(&pdev->dev, "Failed to initialize the DMA\n");
137
138         ret = devm_request_irq(xscd->dev, xscd->irq, xscd_irq_handler,
139                                IRQF_SHARED, dev_name(xscd->dev), xscd);
140         if (ret < 0)
141                 dev_err(&pdev->dev, "Failed to request IRQ\n");
142
143         dev_info(xscd->dev, "scene change detect device found!\n");
144         return 0;
145 }
146
147 static int xscd_remove(struct platform_device *pdev)
148 {
149         struct xscd_device *xscd = platform_get_drvdata(pdev);
150
151         xscd_dma_cleanup(xscd);
152         clk_disable_unprepare(xscd->clk);
153
154         return 0;
155 }
156
157 static const struct of_device_id xscd_of_id_table[] = {
158         { .compatible = "xlnx,v-scd" },
159         { }
160 };
161 MODULE_DEVICE_TABLE(of, xscd_of_id_table);
162
163 static struct platform_driver xscd_driver = {
164         .driver = {
165                 .name           = "xilinx-scd",
166                 .of_match_table = xscd_of_id_table,
167         },
168         .probe                  = xscd_probe,
169         .remove                 = xscd_remove,
170 };
171
172 module_platform_driver(xscd_driver);
173
174 MODULE_AUTHOR("Xilinx Inc.");
175 MODULE_DESCRIPTION("Xilinx Scene Change Detection");
176 MODULE_LICENSE("GPL v2");