Merge tags 'ib-mfd-clk-gpio-regulator-rtc-v5.13', 'ib-mfd-extcon-v5.13', 'ib-mfd...
[sfrench/cifs-2.6.git] / drivers / media / platform / atmel / atmel-sama5d2-isc.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Microchip Image Sensor Controller (ISC) driver
4  *
5  * Copyright (C) 2016-2019 Microchip Technology, Inc.
6  *
7  * Author: Songjun Wu
8  * Author: Eugen Hristev <eugen.hristev@microchip.com>
9  *
10  *
11  * Sensor-->PFE-->WB-->CFA-->CC-->GAM-->CSC-->CBC-->SUB-->RLP-->DMA
12  *
13  * ISC video pipeline integrates the following submodules:
14  * PFE: Parallel Front End to sample the camera sensor input stream
15  *  WB: Programmable white balance in the Bayer domain
16  * CFA: Color filter array interpolation module
17  *  CC: Programmable color correction
18  * GAM: Gamma correction
19  * CSC: Programmable color space conversion
20  * CBC: Contrast and Brightness control
21  * SUB: This module performs YCbCr444 to YCbCr420 chrominance subsampling
22  * RLP: This module performs rounding, range limiting
23  *      and packing of the incoming data
24  */
25
26 #include <linux/clk.h>
27 #include <linux/clkdev.h>
28 #include <linux/clk-provider.h>
29 #include <linux/delay.h>
30 #include <linux/interrupt.h>
31 #include <linux/math64.h>
32 #include <linux/module.h>
33 #include <linux/of.h>
34 #include <linux/of_graph.h>
35 #include <linux/platform_device.h>
36 #include <linux/pm_runtime.h>
37 #include <linux/regmap.h>
38 #include <linux/videodev2.h>
39
40 #include <media/v4l2-ctrls.h>
41 #include <media/v4l2-device.h>
42 #include <media/v4l2-event.h>
43 #include <media/v4l2-image-sizes.h>
44 #include <media/v4l2-ioctl.h>
45 #include <media/v4l2-fwnode.h>
46 #include <media/v4l2-subdev.h>
47 #include <media/videobuf2-dma-contig.h>
48
49 #include "atmel-isc-regs.h"
50 #include "atmel-isc.h"
51
52 #define ISC_MAX_SUPPORT_WIDTH   2592
53 #define ISC_MAX_SUPPORT_HEIGHT  1944
54
55 #define ISC_CLK_MAX_DIV         255
56
57 static int isc_parse_dt(struct device *dev, struct isc_device *isc)
58 {
59         struct device_node *np = dev->of_node;
60         struct device_node *epn = NULL;
61         struct isc_subdev_entity *subdev_entity;
62         unsigned int flags;
63         int ret;
64
65         INIT_LIST_HEAD(&isc->subdev_entities);
66
67         while (1) {
68                 struct v4l2_fwnode_endpoint v4l2_epn = { .bus_type = 0 };
69
70                 epn = of_graph_get_next_endpoint(np, epn);
71                 if (!epn)
72                         return 0;
73
74                 ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(epn),
75                                                  &v4l2_epn);
76                 if (ret) {
77                         ret = -EINVAL;
78                         dev_err(dev, "Could not parse the endpoint\n");
79                         break;
80                 }
81
82                 subdev_entity = devm_kzalloc(dev, sizeof(*subdev_entity),
83                                              GFP_KERNEL);
84                 if (!subdev_entity) {
85                         ret = -ENOMEM;
86                         break;
87                 }
88                 subdev_entity->epn = epn;
89
90                 flags = v4l2_epn.bus.parallel.flags;
91
92                 if (flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
93                         subdev_entity->pfe_cfg0 = ISC_PFE_CFG0_HPOL_LOW;
94
95                 if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
96                         subdev_entity->pfe_cfg0 |= ISC_PFE_CFG0_VPOL_LOW;
97
98                 if (flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
99                         subdev_entity->pfe_cfg0 |= ISC_PFE_CFG0_PPOL_LOW;
100
101                 if (v4l2_epn.bus_type == V4L2_MBUS_BT656)
102                         subdev_entity->pfe_cfg0 |= ISC_PFE_CFG0_CCIR_CRC |
103                                         ISC_PFE_CFG0_CCIR656;
104
105                 list_add_tail(&subdev_entity->list, &isc->subdev_entities);
106         }
107         of_node_put(epn);
108
109         return ret;
110 }
111
112 static int atmel_isc_probe(struct platform_device *pdev)
113 {
114         struct device *dev = &pdev->dev;
115         struct isc_device *isc;
116         struct resource *res;
117         void __iomem *io_base;
118         struct isc_subdev_entity *subdev_entity;
119         int irq;
120         int ret;
121
122         isc = devm_kzalloc(dev, sizeof(*isc), GFP_KERNEL);
123         if (!isc)
124                 return -ENOMEM;
125
126         platform_set_drvdata(pdev, isc);
127         isc->dev = dev;
128
129         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
130         io_base = devm_ioremap_resource(dev, res);
131         if (IS_ERR(io_base))
132                 return PTR_ERR(io_base);
133
134         isc->regmap = devm_regmap_init_mmio(dev, io_base, &isc_regmap_config);
135         if (IS_ERR(isc->regmap)) {
136                 ret = PTR_ERR(isc->regmap);
137                 dev_err(dev, "failed to init register map: %d\n", ret);
138                 return ret;
139         }
140
141         irq = platform_get_irq(pdev, 0);
142         if (irq < 0)
143                 return irq;
144
145         ret = devm_request_irq(dev, irq, isc_interrupt, 0,
146                                ATMEL_ISC_NAME, isc);
147         if (ret < 0) {
148                 dev_err(dev, "can't register ISR for IRQ %u (ret=%i)\n",
149                         irq, ret);
150                 return ret;
151         }
152
153         ret = isc_pipeline_init(isc);
154         if (ret)
155                 return ret;
156
157         isc->hclock = devm_clk_get(dev, "hclock");
158         if (IS_ERR(isc->hclock)) {
159                 ret = PTR_ERR(isc->hclock);
160                 dev_err(dev, "failed to get hclock: %d\n", ret);
161                 return ret;
162         }
163
164         ret = clk_prepare_enable(isc->hclock);
165         if (ret) {
166                 dev_err(dev, "failed to enable hclock: %d\n", ret);
167                 return ret;
168         }
169
170         ret = isc_clk_init(isc);
171         if (ret) {
172                 dev_err(dev, "failed to init isc clock: %d\n", ret);
173                 goto unprepare_hclk;
174         }
175
176         isc->ispck = isc->isc_clks[ISC_ISPCK].clk;
177
178         ret = clk_prepare_enable(isc->ispck);
179         if (ret) {
180                 dev_err(dev, "failed to enable ispck: %d\n", ret);
181                 goto unprepare_hclk;
182         }
183
184         /* ispck should be greater or equal to hclock */
185         ret = clk_set_rate(isc->ispck, clk_get_rate(isc->hclock));
186         if (ret) {
187                 dev_err(dev, "failed to set ispck rate: %d\n", ret);
188                 goto unprepare_clk;
189         }
190
191         ret = v4l2_device_register(dev, &isc->v4l2_dev);
192         if (ret) {
193                 dev_err(dev, "unable to register v4l2 device.\n");
194                 goto unprepare_clk;
195         }
196
197         ret = isc_parse_dt(dev, isc);
198         if (ret) {
199                 dev_err(dev, "fail to parse device tree\n");
200                 goto unregister_v4l2_device;
201         }
202
203         if (list_empty(&isc->subdev_entities)) {
204                 dev_err(dev, "no subdev found\n");
205                 ret = -ENODEV;
206                 goto unregister_v4l2_device;
207         }
208
209         list_for_each_entry(subdev_entity, &isc->subdev_entities, list) {
210                 struct v4l2_async_subdev *asd;
211
212                 v4l2_async_notifier_init(&subdev_entity->notifier);
213
214                 asd = v4l2_async_notifier_add_fwnode_remote_subdev(
215                                         &subdev_entity->notifier,
216                                         of_fwnode_handle(subdev_entity->epn),
217                                         struct v4l2_async_subdev);
218
219                 of_node_put(subdev_entity->epn);
220                 subdev_entity->epn = NULL;
221
222                 if (IS_ERR(asd)) {
223                         ret = PTR_ERR(asd);
224                         goto cleanup_subdev;
225                 }
226
227                 subdev_entity->notifier.ops = &isc_async_ops;
228
229                 ret = v4l2_async_notifier_register(&isc->v4l2_dev,
230                                                    &subdev_entity->notifier);
231                 if (ret) {
232                         dev_err(dev, "fail to register async notifier\n");
233                         goto cleanup_subdev;
234                 }
235
236                 if (video_is_registered(&isc->video_dev))
237                         break;
238         }
239
240         pm_runtime_set_active(dev);
241         pm_runtime_enable(dev);
242         pm_request_idle(dev);
243
244         return 0;
245
246 cleanup_subdev:
247         isc_subdev_cleanup(isc);
248
249 unregister_v4l2_device:
250         v4l2_device_unregister(&isc->v4l2_dev);
251
252 unprepare_clk:
253         clk_disable_unprepare(isc->ispck);
254 unprepare_hclk:
255         clk_disable_unprepare(isc->hclock);
256
257         isc_clk_cleanup(isc);
258
259         return ret;
260 }
261
262 static int atmel_isc_remove(struct platform_device *pdev)
263 {
264         struct isc_device *isc = platform_get_drvdata(pdev);
265
266         pm_runtime_disable(&pdev->dev);
267
268         isc_subdev_cleanup(isc);
269
270         v4l2_device_unregister(&isc->v4l2_dev);
271
272         clk_disable_unprepare(isc->ispck);
273         clk_disable_unprepare(isc->hclock);
274
275         isc_clk_cleanup(isc);
276
277         return 0;
278 }
279
280 static int __maybe_unused isc_runtime_suspend(struct device *dev)
281 {
282         struct isc_device *isc = dev_get_drvdata(dev);
283
284         clk_disable_unprepare(isc->ispck);
285         clk_disable_unprepare(isc->hclock);
286
287         return 0;
288 }
289
290 static int __maybe_unused isc_runtime_resume(struct device *dev)
291 {
292         struct isc_device *isc = dev_get_drvdata(dev);
293         int ret;
294
295         ret = clk_prepare_enable(isc->hclock);
296         if (ret)
297                 return ret;
298
299         ret = clk_prepare_enable(isc->ispck);
300         if (ret)
301                 clk_disable_unprepare(isc->hclock);
302
303         return ret;
304 }
305
306 static const struct dev_pm_ops atmel_isc_dev_pm_ops = {
307         SET_RUNTIME_PM_OPS(isc_runtime_suspend, isc_runtime_resume, NULL)
308 };
309
310 #if IS_ENABLED(CONFIG_OF)
311 static const struct of_device_id atmel_isc_of_match[] = {
312         { .compatible = "atmel,sama5d2-isc" },
313         { }
314 };
315 MODULE_DEVICE_TABLE(of, atmel_isc_of_match);
316 #endif
317
318 static struct platform_driver atmel_isc_driver = {
319         .probe  = atmel_isc_probe,
320         .remove = atmel_isc_remove,
321         .driver = {
322                 .name           = ATMEL_ISC_NAME,
323                 .pm             = &atmel_isc_dev_pm_ops,
324                 .of_match_table = of_match_ptr(atmel_isc_of_match),
325         },
326 };
327
328 module_platform_driver(atmel_isc_driver);
329
330 MODULE_AUTHOR("Songjun Wu");
331 MODULE_DESCRIPTION("The V4L2 driver for Atmel-ISC");
332 MODULE_LICENSE("GPL v2");