1 // SPDX-License-Identifier: GPL-2.0
3 * Microchip Image Sensor Controller (ISC) driver
5 * Copyright (C) 2016-2019 Microchip Technology, Inc.
8 * Author: Eugen Hristev <eugen.hristev@microchip.com>
11 * Sensor-->PFE-->WB-->CFA-->CC-->GAM-->CSC-->CBC-->SUB-->RLP-->DMA
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
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>
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>
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>
49 #include "atmel-isc-regs.h"
50 #include "atmel-isc.h"
52 #define ISC_MAX_SUPPORT_WIDTH 2592
53 #define ISC_MAX_SUPPORT_HEIGHT 1944
55 #define ISC_CLK_MAX_DIV 255
57 static int isc_parse_dt(struct device *dev, struct isc_device *isc)
59 struct device_node *np = dev->of_node;
60 struct device_node *epn = NULL;
61 struct isc_subdev_entity *subdev_entity;
65 INIT_LIST_HEAD(&isc->subdev_entities);
68 struct v4l2_fwnode_endpoint v4l2_epn = { .bus_type = 0 };
70 epn = of_graph_get_next_endpoint(np, epn);
74 ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(epn),
78 dev_err(dev, "Could not parse the endpoint\n");
82 subdev_entity = devm_kzalloc(dev, sizeof(*subdev_entity),
88 subdev_entity->epn = epn;
90 flags = v4l2_epn.bus.parallel.flags;
92 if (flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
93 subdev_entity->pfe_cfg0 = ISC_PFE_CFG0_HPOL_LOW;
95 if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
96 subdev_entity->pfe_cfg0 |= ISC_PFE_CFG0_VPOL_LOW;
98 if (flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
99 subdev_entity->pfe_cfg0 |= ISC_PFE_CFG0_PPOL_LOW;
101 if (v4l2_epn.bus_type == V4L2_MBUS_BT656)
102 subdev_entity->pfe_cfg0 |= ISC_PFE_CFG0_CCIR_CRC |
103 ISC_PFE_CFG0_CCIR656;
105 list_add_tail(&subdev_entity->list, &isc->subdev_entities);
112 static int atmel_isc_probe(struct platform_device *pdev)
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;
122 isc = devm_kzalloc(dev, sizeof(*isc), GFP_KERNEL);
126 platform_set_drvdata(pdev, isc);
129 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
130 io_base = devm_ioremap_resource(dev, res);
132 return PTR_ERR(io_base);
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);
141 irq = platform_get_irq(pdev, 0);
145 ret = devm_request_irq(dev, irq, isc_interrupt, 0,
146 ATMEL_ISC_NAME, isc);
148 dev_err(dev, "can't register ISR for IRQ %u (ret=%i)\n",
153 ret = isc_pipeline_init(isc);
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);
164 ret = clk_prepare_enable(isc->hclock);
166 dev_err(dev, "failed to enable hclock: %d\n", ret);
170 ret = isc_clk_init(isc);
172 dev_err(dev, "failed to init isc clock: %d\n", ret);
176 isc->ispck = isc->isc_clks[ISC_ISPCK].clk;
178 ret = clk_prepare_enable(isc->ispck);
180 dev_err(dev, "failed to enable ispck: %d\n", ret);
184 /* ispck should be greater or equal to hclock */
185 ret = clk_set_rate(isc->ispck, clk_get_rate(isc->hclock));
187 dev_err(dev, "failed to set ispck rate: %d\n", ret);
191 ret = v4l2_device_register(dev, &isc->v4l2_dev);
193 dev_err(dev, "unable to register v4l2 device.\n");
197 ret = isc_parse_dt(dev, isc);
199 dev_err(dev, "fail to parse device tree\n");
200 goto unregister_v4l2_device;
203 if (list_empty(&isc->subdev_entities)) {
204 dev_err(dev, "no subdev found\n");
206 goto unregister_v4l2_device;
209 list_for_each_entry(subdev_entity, &isc->subdev_entities, list) {
210 struct v4l2_async_subdev *asd;
212 v4l2_async_notifier_init(&subdev_entity->notifier);
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);
219 of_node_put(subdev_entity->epn);
220 subdev_entity->epn = NULL;
227 subdev_entity->notifier.ops = &isc_async_ops;
229 ret = v4l2_async_notifier_register(&isc->v4l2_dev,
230 &subdev_entity->notifier);
232 dev_err(dev, "fail to register async notifier\n");
236 if (video_is_registered(&isc->video_dev))
240 pm_runtime_set_active(dev);
241 pm_runtime_enable(dev);
242 pm_request_idle(dev);
247 isc_subdev_cleanup(isc);
249 unregister_v4l2_device:
250 v4l2_device_unregister(&isc->v4l2_dev);
253 clk_disable_unprepare(isc->ispck);
255 clk_disable_unprepare(isc->hclock);
257 isc_clk_cleanup(isc);
262 static int atmel_isc_remove(struct platform_device *pdev)
264 struct isc_device *isc = platform_get_drvdata(pdev);
266 pm_runtime_disable(&pdev->dev);
268 isc_subdev_cleanup(isc);
270 v4l2_device_unregister(&isc->v4l2_dev);
272 clk_disable_unprepare(isc->ispck);
273 clk_disable_unprepare(isc->hclock);
275 isc_clk_cleanup(isc);
280 static int __maybe_unused isc_runtime_suspend(struct device *dev)
282 struct isc_device *isc = dev_get_drvdata(dev);
284 clk_disable_unprepare(isc->ispck);
285 clk_disable_unprepare(isc->hclock);
290 static int __maybe_unused isc_runtime_resume(struct device *dev)
292 struct isc_device *isc = dev_get_drvdata(dev);
295 ret = clk_prepare_enable(isc->hclock);
299 ret = clk_prepare_enable(isc->ispck);
301 clk_disable_unprepare(isc->hclock);
306 static const struct dev_pm_ops atmel_isc_dev_pm_ops = {
307 SET_RUNTIME_PM_OPS(isc_runtime_suspend, isc_runtime_resume, NULL)
310 #if IS_ENABLED(CONFIG_OF)
311 static const struct of_device_id atmel_isc_of_match[] = {
312 { .compatible = "atmel,sama5d2-isc" },
315 MODULE_DEVICE_TABLE(of, atmel_isc_of_match);
318 static struct platform_driver atmel_isc_driver = {
319 .probe = atmel_isc_probe,
320 .remove = atmel_isc_remove,
322 .name = ATMEL_ISC_NAME,
323 .pm = &atmel_isc_dev_pm_ops,
324 .of_match_table = of_match_ptr(atmel_isc_of_match),
328 module_platform_driver(atmel_isc_driver);
330 MODULE_AUTHOR("Songjun Wu");
331 MODULE_DESCRIPTION("The V4L2 driver for Atmel-ISC");
332 MODULE_LICENSE("GPL v2");