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
, *rem
;
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 rem
= of_graph_get_remote_port_parent(epn
);
76 dev_notice(dev
, "Remote device at %pOF not found\n",
81 ret
= v4l2_fwnode_endpoint_parse(of_fwnode_handle(epn
),
86 dev_err(dev
, "Could not parse the endpoint\n");
90 subdev_entity
= devm_kzalloc(dev
, sizeof(*subdev_entity
),
98 /* asd will be freed by the subsystem once it's added to the
101 subdev_entity
->asd
= kzalloc(sizeof(*subdev_entity
->asd
),
103 if (!subdev_entity
->asd
) {
109 flags
= v4l2_epn
.bus
.parallel
.flags
;
111 if (flags
& V4L2_MBUS_HSYNC_ACTIVE_LOW
)
112 subdev_entity
->pfe_cfg0
= ISC_PFE_CFG0_HPOL_LOW
;
114 if (flags
& V4L2_MBUS_VSYNC_ACTIVE_LOW
)
115 subdev_entity
->pfe_cfg0
|= ISC_PFE_CFG0_VPOL_LOW
;
117 if (flags
& V4L2_MBUS_PCLK_SAMPLE_FALLING
)
118 subdev_entity
->pfe_cfg0
|= ISC_PFE_CFG0_PPOL_LOW
;
120 if (v4l2_epn
.bus_type
== V4L2_MBUS_BT656
)
121 subdev_entity
->pfe_cfg0
|= ISC_PFE_CFG0_CCIR_CRC
|
122 ISC_PFE_CFG0_CCIR656
;
124 subdev_entity
->asd
->match_type
= V4L2_ASYNC_MATCH_FWNODE
;
125 subdev_entity
->asd
->match
.fwnode
= of_fwnode_handle(rem
);
126 list_add_tail(&subdev_entity
->list
, &isc
->subdev_entities
);
133 static int atmel_isc_probe(struct platform_device
*pdev
)
135 struct device
*dev
= &pdev
->dev
;
136 struct isc_device
*isc
;
137 struct resource
*res
;
138 void __iomem
*io_base
;
139 struct isc_subdev_entity
*subdev_entity
;
143 isc
= devm_kzalloc(dev
, sizeof(*isc
), GFP_KERNEL
);
147 platform_set_drvdata(pdev
, isc
);
150 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
151 io_base
= devm_ioremap_resource(dev
, res
);
153 return PTR_ERR(io_base
);
155 isc
->regmap
= devm_regmap_init_mmio(dev
, io_base
, &isc_regmap_config
);
156 if (IS_ERR(isc
->regmap
)) {
157 ret
= PTR_ERR(isc
->regmap
);
158 dev_err(dev
, "failed to init register map: %d\n", ret
);
162 irq
= platform_get_irq(pdev
, 0);
166 ret
= devm_request_irq(dev
, irq
, isc_interrupt
, 0,
167 ATMEL_ISC_NAME
, isc
);
169 dev_err(dev
, "can't register ISR for IRQ %u (ret=%i)\n",
174 ret
= isc_pipeline_init(isc
);
178 isc
->hclock
= devm_clk_get(dev
, "hclock");
179 if (IS_ERR(isc
->hclock
)) {
180 ret
= PTR_ERR(isc
->hclock
);
181 dev_err(dev
, "failed to get hclock: %d\n", ret
);
185 ret
= clk_prepare_enable(isc
->hclock
);
187 dev_err(dev
, "failed to enable hclock: %d\n", ret
);
191 ret
= isc_clk_init(isc
);
193 dev_err(dev
, "failed to init isc clock: %d\n", ret
);
197 isc
->ispck
= isc
->isc_clks
[ISC_ISPCK
].clk
;
199 ret
= clk_prepare_enable(isc
->ispck
);
201 dev_err(dev
, "failed to enable ispck: %d\n", ret
);
205 /* ispck should be greater or equal to hclock */
206 ret
= clk_set_rate(isc
->ispck
, clk_get_rate(isc
->hclock
));
208 dev_err(dev
, "failed to set ispck rate: %d\n", ret
);
212 ret
= v4l2_device_register(dev
, &isc
->v4l2_dev
);
214 dev_err(dev
, "unable to register v4l2 device.\n");
218 ret
= isc_parse_dt(dev
, isc
);
220 dev_err(dev
, "fail to parse device tree\n");
221 goto unregister_v4l2_device
;
224 if (list_empty(&isc
->subdev_entities
)) {
225 dev_err(dev
, "no subdev found\n");
227 goto unregister_v4l2_device
;
230 list_for_each_entry(subdev_entity
, &isc
->subdev_entities
, list
) {
231 v4l2_async_notifier_init(&subdev_entity
->notifier
);
233 ret
= v4l2_async_notifier_add_subdev(&subdev_entity
->notifier
,
236 fwnode_handle_put(subdev_entity
->asd
->match
.fwnode
);
237 kfree(subdev_entity
->asd
);
241 subdev_entity
->notifier
.ops
= &isc_async_ops
;
243 ret
= v4l2_async_notifier_register(&isc
->v4l2_dev
,
244 &subdev_entity
->notifier
);
246 dev_err(dev
, "fail to register async notifier\n");
250 if (video_is_registered(&isc
->video_dev
))
254 pm_runtime_set_active(dev
);
255 pm_runtime_enable(dev
);
256 pm_request_idle(dev
);
261 isc_subdev_cleanup(isc
);
263 unregister_v4l2_device
:
264 v4l2_device_unregister(&isc
->v4l2_dev
);
267 clk_disable_unprepare(isc
->ispck
);
269 clk_disable_unprepare(isc
->hclock
);
271 isc_clk_cleanup(isc
);
276 static int atmel_isc_remove(struct platform_device
*pdev
)
278 struct isc_device
*isc
= platform_get_drvdata(pdev
);
280 pm_runtime_disable(&pdev
->dev
);
282 isc_subdev_cleanup(isc
);
284 v4l2_device_unregister(&isc
->v4l2_dev
);
286 clk_disable_unprepare(isc
->ispck
);
287 clk_disable_unprepare(isc
->hclock
);
289 isc_clk_cleanup(isc
);
294 static int __maybe_unused
isc_runtime_suspend(struct device
*dev
)
296 struct isc_device
*isc
= dev_get_drvdata(dev
);
298 clk_disable_unprepare(isc
->ispck
);
299 clk_disable_unprepare(isc
->hclock
);
304 static int __maybe_unused
isc_runtime_resume(struct device
*dev
)
306 struct isc_device
*isc
= dev_get_drvdata(dev
);
309 ret
= clk_prepare_enable(isc
->hclock
);
313 ret
= clk_prepare_enable(isc
->ispck
);
315 clk_disable_unprepare(isc
->hclock
);
320 static const struct dev_pm_ops atmel_isc_dev_pm_ops
= {
321 SET_RUNTIME_PM_OPS(isc_runtime_suspend
, isc_runtime_resume
, NULL
)
324 #if IS_ENABLED(CONFIG_OF)
325 static const struct of_device_id atmel_isc_of_match
[] = {
326 { .compatible
= "atmel,sama5d2-isc" },
329 MODULE_DEVICE_TABLE(of
, atmel_isc_of_match
);
332 static struct platform_driver atmel_isc_driver
= {
333 .probe
= atmel_isc_probe
,
334 .remove
= atmel_isc_remove
,
336 .name
= ATMEL_ISC_NAME
,
337 .pm
= &atmel_isc_dev_pm_ops
,
338 .of_match_table
= of_match_ptr(atmel_isc_of_match
),
342 module_platform_driver(atmel_isc_driver
);
344 MODULE_AUTHOR("Songjun Wu");
345 MODULE_DESCRIPTION("The V4L2 driver for Atmel-ISC");
346 MODULE_LICENSE("GPL v2");
347 MODULE_SUPPORTED_DEVICE("video");