sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / media / platform / atmel / atmel-isc.c
blobfa68fe912c95e1aea4c38acfa84f13205ec2d50b
1 /*
2 * Atmel Image Sensor Controller (ISC) driver
4 * Copyright (C) 2016 Atmel
6 * Author: Songjun Wu <songjun.wu@microchip.com>
8 * This program is free software; you may redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
12 * Sensor-->PFE-->WB-->CFA-->CC-->GAM-->CSC-->CBC-->SUB-->RLP-->DMA
14 * ISC video pipeline integrates the following submodules:
15 * PFE: Parallel Front End to sample the camera sensor input stream
16 * WB: Programmable white balance in the Bayer domain
17 * CFA: Color filter array interpolation module
18 * CC: Programmable color correction
19 * GAM: Gamma correction
20 * CSC: Programmable color space conversion
21 * CBC: Contrast and Brightness control
22 * SUB: This module performs YCbCr444 to YCbCr420 chrominance subsampling
23 * RLP: This module performs rounding, range limiting
24 * and packing of the incoming data
27 #include <linux/clk.h>
28 #include <linux/clkdev.h>
29 #include <linux/clk-provider.h>
30 #include <linux/delay.h>
31 #include <linux/interrupt.h>
32 #include <linux/module.h>
33 #include <linux/of.h>
34 #include <linux/platform_device.h>
35 #include <linux/pm_runtime.h>
36 #include <linux/regmap.h>
37 #include <linux/videodev2.h>
39 #include <media/v4l2-device.h>
40 #include <media/v4l2-image-sizes.h>
41 #include <media/v4l2-ioctl.h>
42 #include <media/v4l2-of.h>
43 #include <media/v4l2-subdev.h>
44 #include <media/videobuf2-dma-contig.h>
46 #include "atmel-isc-regs.h"
48 #define ATMEL_ISC_NAME "atmel_isc"
50 #define ISC_MAX_SUPPORT_WIDTH 2592
51 #define ISC_MAX_SUPPORT_HEIGHT 1944
53 #define ISC_CLK_MAX_DIV 255
55 enum isc_clk_id {
56 ISC_ISPCK = 0,
57 ISC_MCK = 1,
60 struct isc_clk {
61 struct clk_hw hw;
62 struct clk *clk;
63 struct regmap *regmap;
64 u8 id;
65 u8 parent_id;
66 u32 div;
67 struct device *dev;
70 #define to_isc_clk(hw) container_of(hw, struct isc_clk, hw)
72 struct isc_buffer {
73 struct vb2_v4l2_buffer vb;
74 struct list_head list;
77 struct isc_subdev_entity {
78 struct v4l2_subdev *sd;
79 struct v4l2_async_subdev *asd;
80 struct v4l2_async_notifier notifier;
81 struct v4l2_subdev_pad_config *config;
83 u32 pfe_cfg0;
85 struct list_head list;
89 * struct isc_format - ISC media bus format information
90 * @fourcc: Fourcc code for this format
91 * @mbus_code: V4L2 media bus format code.
92 * @bpp: Bytes per pixel (when stored in memory)
93 * @reg_bps: reg value for bits per sample
94 * (when transferred over a bus)
95 * @support: Indicates format supported by subdev
97 struct isc_format {
98 u32 fourcc;
99 u32 mbus_code;
100 u8 bpp;
102 u32 reg_bps;
103 u32 reg_rlp_mode;
104 u32 reg_dcfg_imode;
105 u32 reg_dctrl_dview;
107 bool support;
110 #define ISC_PIPE_LINE_NODE_NUM 11
112 struct isc_device {
113 struct regmap *regmap;
114 struct clk *hclock;
115 struct clk *ispck;
116 struct isc_clk isc_clks[2];
118 struct device *dev;
119 struct v4l2_device v4l2_dev;
120 struct video_device video_dev;
122 struct vb2_queue vb2_vidq;
123 spinlock_t dma_queue_lock;
124 struct list_head dma_queue;
125 struct isc_buffer *cur_frm;
126 unsigned int sequence;
127 bool stop;
128 struct completion comp;
130 struct v4l2_format fmt;
131 struct isc_format **user_formats;
132 unsigned int num_user_formats;
133 const struct isc_format *current_fmt;
135 struct mutex lock;
137 struct regmap_field *pipeline[ISC_PIPE_LINE_NODE_NUM];
139 struct isc_subdev_entity *current_subdev;
140 struct list_head subdev_entities;
143 static struct isc_format isc_formats[] = {
144 { V4L2_PIX_FMT_SBGGR8, MEDIA_BUS_FMT_SBGGR8_1X8,
145 1, ISC_PFE_CFG0_BPS_EIGHT, ISC_RLP_CFG_MODE_DAT8,
146 ISC_DCFG_IMODE_PACKED8, ISC_DCTRL_DVIEW_PACKED, false },
147 { V4L2_PIX_FMT_SGBRG8, MEDIA_BUS_FMT_SGBRG8_1X8,
148 1, ISC_PFE_CFG0_BPS_EIGHT, ISC_RLP_CFG_MODE_DAT8,
149 ISC_DCFG_IMODE_PACKED8, ISC_DCTRL_DVIEW_PACKED, false },
150 { V4L2_PIX_FMT_SGRBG8, MEDIA_BUS_FMT_SGRBG8_1X8,
151 1, ISC_PFE_CFG0_BPS_EIGHT, ISC_RLP_CFG_MODE_DAT8,
152 ISC_DCFG_IMODE_PACKED8, ISC_DCTRL_DVIEW_PACKED, false },
153 { V4L2_PIX_FMT_SRGGB8, MEDIA_BUS_FMT_SRGGB8_1X8,
154 1, ISC_PFE_CFG0_BPS_EIGHT, ISC_RLP_CFG_MODE_DAT8,
155 ISC_DCFG_IMODE_PACKED8, ISC_DCTRL_DVIEW_PACKED, false },
157 { V4L2_PIX_FMT_SBGGR10, MEDIA_BUS_FMT_SBGGR10_1X10,
158 2, ISC_PFG_CFG0_BPS_TEN, ISC_RLP_CFG_MODE_DAT10,
159 ISC_DCFG_IMODE_PACKED16, ISC_DCTRL_DVIEW_PACKED, false },
160 { V4L2_PIX_FMT_SGBRG10, MEDIA_BUS_FMT_SGBRG10_1X10,
161 2, ISC_PFG_CFG0_BPS_TEN, ISC_RLP_CFG_MODE_DAT10,
162 ISC_DCFG_IMODE_PACKED16, ISC_DCTRL_DVIEW_PACKED, false },
163 { V4L2_PIX_FMT_SGRBG10, MEDIA_BUS_FMT_SGRBG10_1X10,
164 2, ISC_PFG_CFG0_BPS_TEN, ISC_RLP_CFG_MODE_DAT10,
165 ISC_DCFG_IMODE_PACKED16, ISC_DCTRL_DVIEW_PACKED, false },
166 { V4L2_PIX_FMT_SRGGB10, MEDIA_BUS_FMT_SRGGB10_1X10,
167 2, ISC_PFG_CFG0_BPS_TEN, ISC_RLP_CFG_MODE_DAT10,
168 ISC_DCFG_IMODE_PACKED16, ISC_DCTRL_DVIEW_PACKED, false },
170 { V4L2_PIX_FMT_SBGGR12, MEDIA_BUS_FMT_SBGGR12_1X12,
171 2, ISC_PFG_CFG0_BPS_TWELVE, ISC_RLP_CFG_MODE_DAT12,
172 ISC_DCFG_IMODE_PACKED16, ISC_DCTRL_DVIEW_PACKED, false },
173 { V4L2_PIX_FMT_SGBRG12, MEDIA_BUS_FMT_SGBRG12_1X12,
174 2, ISC_PFG_CFG0_BPS_TWELVE, ISC_RLP_CFG_MODE_DAT12,
175 ISC_DCFG_IMODE_PACKED16, ISC_DCTRL_DVIEW_PACKED, false },
176 { V4L2_PIX_FMT_SGRBG12, MEDIA_BUS_FMT_SGRBG12_1X12,
177 2, ISC_PFG_CFG0_BPS_TWELVE, ISC_RLP_CFG_MODE_DAT12,
178 ISC_DCFG_IMODE_PACKED16, ISC_DCTRL_DVIEW_PACKED, false },
179 { V4L2_PIX_FMT_SRGGB12, MEDIA_BUS_FMT_SRGGB12_1X12,
180 2, ISC_PFG_CFG0_BPS_TWELVE, ISC_RLP_CFG_MODE_DAT12,
181 ISC_DCFG_IMODE_PACKED16, ISC_DCTRL_DVIEW_PACKED, false },
183 { V4L2_PIX_FMT_YUYV, MEDIA_BUS_FMT_YUYV8_2X8,
184 2, ISC_PFE_CFG0_BPS_EIGHT, ISC_RLP_CFG_MODE_DAT8,
185 ISC_DCFG_IMODE_PACKED8, ISC_DCTRL_DVIEW_PACKED, false },
188 static int isc_clk_enable(struct clk_hw *hw)
190 struct isc_clk *isc_clk = to_isc_clk(hw);
191 u32 id = isc_clk->id;
192 struct regmap *regmap = isc_clk->regmap;
194 dev_dbg(isc_clk->dev, "ISC CLK: %s, div = %d, parent id = %d\n",
195 __func__, isc_clk->div, isc_clk->parent_id);
197 regmap_update_bits(regmap, ISC_CLKCFG,
198 ISC_CLKCFG_DIV_MASK(id) | ISC_CLKCFG_SEL_MASK(id),
199 (isc_clk->div << ISC_CLKCFG_DIV_SHIFT(id)) |
200 (isc_clk->parent_id << ISC_CLKCFG_SEL_SHIFT(id)));
202 regmap_write(regmap, ISC_CLKEN, ISC_CLK(id));
204 return 0;
207 static void isc_clk_disable(struct clk_hw *hw)
209 struct isc_clk *isc_clk = to_isc_clk(hw);
210 u32 id = isc_clk->id;
212 regmap_write(isc_clk->regmap, ISC_CLKDIS, ISC_CLK(id));
215 static int isc_clk_is_enabled(struct clk_hw *hw)
217 struct isc_clk *isc_clk = to_isc_clk(hw);
218 u32 status;
220 regmap_read(isc_clk->regmap, ISC_CLKSR, &status);
222 return status & ISC_CLK(isc_clk->id) ? 1 : 0;
225 static unsigned long
226 isc_clk_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
228 struct isc_clk *isc_clk = to_isc_clk(hw);
230 return DIV_ROUND_CLOSEST(parent_rate, isc_clk->div + 1);
233 static int isc_clk_determine_rate(struct clk_hw *hw,
234 struct clk_rate_request *req)
236 struct isc_clk *isc_clk = to_isc_clk(hw);
237 long best_rate = -EINVAL;
238 int best_diff = -1;
239 unsigned int i, div;
241 for (i = 0; i < clk_hw_get_num_parents(hw); i++) {
242 struct clk_hw *parent;
243 unsigned long parent_rate;
245 parent = clk_hw_get_parent_by_index(hw, i);
246 if (!parent)
247 continue;
249 parent_rate = clk_hw_get_rate(parent);
250 if (!parent_rate)
251 continue;
253 for (div = 1; div < ISC_CLK_MAX_DIV + 2; div++) {
254 unsigned long rate;
255 int diff;
257 rate = DIV_ROUND_CLOSEST(parent_rate, div);
258 diff = abs(req->rate - rate);
260 if (best_diff < 0 || best_diff > diff) {
261 best_rate = rate;
262 best_diff = diff;
263 req->best_parent_rate = parent_rate;
264 req->best_parent_hw = parent;
267 if (!best_diff || rate < req->rate)
268 break;
271 if (!best_diff)
272 break;
275 dev_dbg(isc_clk->dev,
276 "ISC CLK: %s, best_rate = %ld, parent clk: %s @ %ld\n",
277 __func__, best_rate,
278 __clk_get_name((req->best_parent_hw)->clk),
279 req->best_parent_rate);
281 if (best_rate < 0)
282 return best_rate;
284 req->rate = best_rate;
286 return 0;
289 static int isc_clk_set_parent(struct clk_hw *hw, u8 index)
291 struct isc_clk *isc_clk = to_isc_clk(hw);
293 if (index >= clk_hw_get_num_parents(hw))
294 return -EINVAL;
296 isc_clk->parent_id = index;
298 return 0;
301 static u8 isc_clk_get_parent(struct clk_hw *hw)
303 struct isc_clk *isc_clk = to_isc_clk(hw);
305 return isc_clk->parent_id;
308 static int isc_clk_set_rate(struct clk_hw *hw,
309 unsigned long rate,
310 unsigned long parent_rate)
312 struct isc_clk *isc_clk = to_isc_clk(hw);
313 u32 div;
315 if (!rate)
316 return -EINVAL;
318 div = DIV_ROUND_CLOSEST(parent_rate, rate);
319 if (div > (ISC_CLK_MAX_DIV + 1) || !div)
320 return -EINVAL;
322 isc_clk->div = div - 1;
324 return 0;
327 static const struct clk_ops isc_clk_ops = {
328 .enable = isc_clk_enable,
329 .disable = isc_clk_disable,
330 .is_enabled = isc_clk_is_enabled,
331 .recalc_rate = isc_clk_recalc_rate,
332 .determine_rate = isc_clk_determine_rate,
333 .set_parent = isc_clk_set_parent,
334 .get_parent = isc_clk_get_parent,
335 .set_rate = isc_clk_set_rate,
338 static int isc_clk_register(struct isc_device *isc, unsigned int id)
340 struct regmap *regmap = isc->regmap;
341 struct device_node *np = isc->dev->of_node;
342 struct isc_clk *isc_clk;
343 struct clk_init_data init;
344 const char *clk_name = np->name;
345 const char *parent_names[3];
346 int num_parents;
348 num_parents = of_clk_get_parent_count(np);
349 if (num_parents < 1 || num_parents > 3)
350 return -EINVAL;
352 if (num_parents > 2 && id == ISC_ISPCK)
353 num_parents = 2;
355 of_clk_parent_fill(np, parent_names, num_parents);
357 if (id == ISC_MCK)
358 of_property_read_string(np, "clock-output-names", &clk_name);
359 else
360 clk_name = "isc-ispck";
362 init.parent_names = parent_names;
363 init.num_parents = num_parents;
364 init.name = clk_name;
365 init.ops = &isc_clk_ops;
366 init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
368 isc_clk = &isc->isc_clks[id];
369 isc_clk->hw.init = &init;
370 isc_clk->regmap = regmap;
371 isc_clk->id = id;
372 isc_clk->dev = isc->dev;
374 isc_clk->clk = clk_register(isc->dev, &isc_clk->hw);
375 if (IS_ERR(isc_clk->clk)) {
376 dev_err(isc->dev, "%s: clock register fail\n", clk_name);
377 return PTR_ERR(isc_clk->clk);
378 } else if (id == ISC_MCK)
379 of_clk_add_provider(np, of_clk_src_simple_get, isc_clk->clk);
381 return 0;
384 static int isc_clk_init(struct isc_device *isc)
386 unsigned int i;
387 int ret;
389 for (i = 0; i < ARRAY_SIZE(isc->isc_clks); i++)
390 isc->isc_clks[i].clk = ERR_PTR(-EINVAL);
392 for (i = 0; i < ARRAY_SIZE(isc->isc_clks); i++) {
393 ret = isc_clk_register(isc, i);
394 if (ret)
395 return ret;
398 return 0;
401 static void isc_clk_cleanup(struct isc_device *isc)
403 unsigned int i;
405 of_clk_del_provider(isc->dev->of_node);
407 for (i = 0; i < ARRAY_SIZE(isc->isc_clks); i++) {
408 struct isc_clk *isc_clk = &isc->isc_clks[i];
410 if (!IS_ERR(isc_clk->clk))
411 clk_unregister(isc_clk->clk);
415 static int isc_queue_setup(struct vb2_queue *vq,
416 unsigned int *nbuffers, unsigned int *nplanes,
417 unsigned int sizes[], struct device *alloc_devs[])
419 struct isc_device *isc = vb2_get_drv_priv(vq);
420 unsigned int size = isc->fmt.fmt.pix.sizeimage;
422 if (*nplanes)
423 return sizes[0] < size ? -EINVAL : 0;
425 *nplanes = 1;
426 sizes[0] = size;
428 return 0;
431 static int isc_buffer_prepare(struct vb2_buffer *vb)
433 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
434 struct isc_device *isc = vb2_get_drv_priv(vb->vb2_queue);
435 unsigned long size = isc->fmt.fmt.pix.sizeimage;
437 if (vb2_plane_size(vb, 0) < size) {
438 v4l2_err(&isc->v4l2_dev, "buffer too small (%lu < %lu)\n",
439 vb2_plane_size(vb, 0), size);
440 return -EINVAL;
443 vb2_set_plane_payload(vb, 0, size);
445 vbuf->field = isc->fmt.fmt.pix.field;
447 return 0;
450 static inline void isc_start_dma(struct regmap *regmap,
451 struct isc_buffer *frm, u32 dview)
453 dma_addr_t addr;
455 addr = vb2_dma_contig_plane_dma_addr(&frm->vb.vb2_buf, 0);
457 regmap_write(regmap, ISC_DCTRL, dview | ISC_DCTRL_IE_IS);
458 regmap_write(regmap, ISC_DAD0, addr);
459 regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_CAPTURE);
462 static void isc_set_pipeline(struct isc_device *isc, u32 pipeline)
464 u32 val;
465 unsigned int i;
467 for (i = 0; i < ISC_PIPE_LINE_NODE_NUM; i++) {
468 val = pipeline & BIT(i) ? 1 : 0;
469 regmap_field_write(isc->pipeline[i], val);
473 static int isc_configure(struct isc_device *isc)
475 struct regmap *regmap = isc->regmap;
476 const struct isc_format *current_fmt = isc->current_fmt;
477 struct isc_subdev_entity *subdev = isc->current_subdev;
478 u32 val, mask;
479 int counter = 10;
481 val = current_fmt->reg_bps | subdev->pfe_cfg0 |
482 ISC_PFE_CFG0_MODE_PROGRESSIVE;
483 mask = ISC_PFE_CFG0_BPS_MASK | ISC_PFE_CFG0_HPOL_LOW |
484 ISC_PFE_CFG0_VPOL_LOW | ISC_PFE_CFG0_PPOL_LOW |
485 ISC_PFE_CFG0_MODE_MASK;
487 regmap_update_bits(regmap, ISC_PFE_CFG0, mask, val);
489 regmap_update_bits(regmap, ISC_RLP_CFG, ISC_RLP_CFG_MODE_MASK,
490 current_fmt->reg_rlp_mode);
492 regmap_update_bits(regmap, ISC_DCFG, ISC_DCFG_IMODE_MASK,
493 current_fmt->reg_dcfg_imode);
495 /* Disable the pipeline */
496 isc_set_pipeline(isc, 0x0);
498 /* Update profile */
499 regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_UPPRO);
501 regmap_read(regmap, ISC_CTRLSR, &val);
502 while ((val & ISC_CTRL_UPPRO) && counter--) {
503 usleep_range(1000, 2000);
504 regmap_read(regmap, ISC_CTRLSR, &val);
507 if (counter < 0)
508 return -ETIMEDOUT;
510 return 0;
513 static int isc_start_streaming(struct vb2_queue *vq, unsigned int count)
515 struct isc_device *isc = vb2_get_drv_priv(vq);
516 struct regmap *regmap = isc->regmap;
517 struct isc_buffer *buf;
518 unsigned long flags;
519 int ret;
520 u32 val;
522 /* Enable stream on the sub device */
523 ret = v4l2_subdev_call(isc->current_subdev->sd, video, s_stream, 1);
524 if (ret && ret != -ENOIOCTLCMD) {
525 v4l2_err(&isc->v4l2_dev, "stream on failed in subdev\n");
526 goto err_start_stream;
529 pm_runtime_get_sync(isc->dev);
531 /* Disable all the interrupts */
532 regmap_write(isc->regmap, ISC_INTDIS, (u32)~0UL);
534 /* Clean the interrupt status register */
535 regmap_read(regmap, ISC_INTSR, &val);
537 ret = isc_configure(isc);
538 if (unlikely(ret))
539 goto err_configure;
541 /* Enable DMA interrupt */
542 regmap_write(regmap, ISC_INTEN, ISC_INT_DDONE);
544 spin_lock_irqsave(&isc->dma_queue_lock, flags);
546 isc->sequence = 0;
547 isc->stop = false;
548 reinit_completion(&isc->comp);
550 isc->cur_frm = list_first_entry(&isc->dma_queue,
551 struct isc_buffer, list);
552 list_del(&isc->cur_frm->list);
554 isc_start_dma(regmap, isc->cur_frm, isc->current_fmt->reg_dctrl_dview);
556 spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
558 return 0;
560 err_configure:
561 pm_runtime_put_sync(isc->dev);
563 v4l2_subdev_call(isc->current_subdev->sd, video, s_stream, 0);
565 err_start_stream:
566 spin_lock_irqsave(&isc->dma_queue_lock, flags);
567 list_for_each_entry(buf, &isc->dma_queue, list)
568 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
569 INIT_LIST_HEAD(&isc->dma_queue);
570 spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
572 return ret;
575 static void isc_stop_streaming(struct vb2_queue *vq)
577 struct isc_device *isc = vb2_get_drv_priv(vq);
578 unsigned long flags;
579 struct isc_buffer *buf;
580 int ret;
582 isc->stop = true;
584 /* Wait until the end of the current frame */
585 if (isc->cur_frm && !wait_for_completion_timeout(&isc->comp, 5 * HZ))
586 v4l2_err(&isc->v4l2_dev,
587 "Timeout waiting for end of the capture\n");
589 /* Disable DMA interrupt */
590 regmap_write(isc->regmap, ISC_INTDIS, ISC_INT_DDONE);
592 pm_runtime_put_sync(isc->dev);
594 /* Disable stream on the sub device */
595 ret = v4l2_subdev_call(isc->current_subdev->sd, video, s_stream, 0);
596 if (ret && ret != -ENOIOCTLCMD)
597 v4l2_err(&isc->v4l2_dev, "stream off failed in subdev\n");
599 /* Release all active buffers */
600 spin_lock_irqsave(&isc->dma_queue_lock, flags);
601 if (unlikely(isc->cur_frm)) {
602 vb2_buffer_done(&isc->cur_frm->vb.vb2_buf,
603 VB2_BUF_STATE_ERROR);
604 isc->cur_frm = NULL;
606 list_for_each_entry(buf, &isc->dma_queue, list)
607 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
608 INIT_LIST_HEAD(&isc->dma_queue);
609 spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
612 static void isc_buffer_queue(struct vb2_buffer *vb)
614 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
615 struct isc_buffer *buf = container_of(vbuf, struct isc_buffer, vb);
616 struct isc_device *isc = vb2_get_drv_priv(vb->vb2_queue);
617 unsigned long flags;
619 spin_lock_irqsave(&isc->dma_queue_lock, flags);
620 if (!isc->cur_frm && list_empty(&isc->dma_queue) &&
621 vb2_is_streaming(vb->vb2_queue)) {
622 isc->cur_frm = buf;
623 isc_start_dma(isc->regmap, isc->cur_frm,
624 isc->current_fmt->reg_dctrl_dview);
625 } else
626 list_add_tail(&buf->list, &isc->dma_queue);
627 spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
630 static struct vb2_ops isc_vb2_ops = {
631 .queue_setup = isc_queue_setup,
632 .wait_prepare = vb2_ops_wait_prepare,
633 .wait_finish = vb2_ops_wait_finish,
634 .buf_prepare = isc_buffer_prepare,
635 .start_streaming = isc_start_streaming,
636 .stop_streaming = isc_stop_streaming,
637 .buf_queue = isc_buffer_queue,
640 static int isc_querycap(struct file *file, void *priv,
641 struct v4l2_capability *cap)
643 struct isc_device *isc = video_drvdata(file);
645 strcpy(cap->driver, ATMEL_ISC_NAME);
646 strcpy(cap->card, "Atmel Image Sensor Controller");
647 snprintf(cap->bus_info, sizeof(cap->bus_info),
648 "platform:%s", isc->v4l2_dev.name);
650 return 0;
653 static int isc_enum_fmt_vid_cap(struct file *file, void *priv,
654 struct v4l2_fmtdesc *f)
656 struct isc_device *isc = video_drvdata(file);
657 u32 index = f->index;
659 if (index >= isc->num_user_formats)
660 return -EINVAL;
662 f->pixelformat = isc->user_formats[index]->fourcc;
664 return 0;
667 static int isc_g_fmt_vid_cap(struct file *file, void *priv,
668 struct v4l2_format *fmt)
670 struct isc_device *isc = video_drvdata(file);
672 *fmt = isc->fmt;
674 return 0;
677 static struct isc_format *find_format_by_fourcc(struct isc_device *isc,
678 unsigned int fourcc)
680 unsigned int num_formats = isc->num_user_formats;
681 struct isc_format *fmt;
682 unsigned int i;
684 for (i = 0; i < num_formats; i++) {
685 fmt = isc->user_formats[i];
686 if (fmt->fourcc == fourcc)
687 return fmt;
690 return NULL;
693 static int isc_try_fmt(struct isc_device *isc, struct v4l2_format *f,
694 struct isc_format **current_fmt)
696 struct isc_format *isc_fmt;
697 struct v4l2_pix_format *pixfmt = &f->fmt.pix;
698 struct v4l2_subdev_format format = {
699 .which = V4L2_SUBDEV_FORMAT_TRY,
701 int ret;
703 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
704 return -EINVAL;
706 isc_fmt = find_format_by_fourcc(isc, pixfmt->pixelformat);
707 if (!isc_fmt) {
708 v4l2_warn(&isc->v4l2_dev, "Format 0x%x not found\n",
709 pixfmt->pixelformat);
710 isc_fmt = isc->user_formats[isc->num_user_formats - 1];
711 pixfmt->pixelformat = isc_fmt->fourcc;
714 /* Limit to Atmel ISC hardware capabilities */
715 if (pixfmt->width > ISC_MAX_SUPPORT_WIDTH)
716 pixfmt->width = ISC_MAX_SUPPORT_WIDTH;
717 if (pixfmt->height > ISC_MAX_SUPPORT_HEIGHT)
718 pixfmt->height = ISC_MAX_SUPPORT_HEIGHT;
720 v4l2_fill_mbus_format(&format.format, pixfmt, isc_fmt->mbus_code);
721 ret = v4l2_subdev_call(isc->current_subdev->sd, pad, set_fmt,
722 isc->current_subdev->config, &format);
723 if (ret < 0)
724 return ret;
726 v4l2_fill_pix_format(pixfmt, &format.format);
728 pixfmt->field = V4L2_FIELD_NONE;
729 pixfmt->bytesperline = pixfmt->width * isc_fmt->bpp;
730 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
732 if (current_fmt)
733 *current_fmt = isc_fmt;
735 return 0;
738 static int isc_set_fmt(struct isc_device *isc, struct v4l2_format *f)
740 struct v4l2_subdev_format format = {
741 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
743 struct isc_format *current_fmt;
744 int ret;
746 ret = isc_try_fmt(isc, f, &current_fmt);
747 if (ret)
748 return ret;
750 v4l2_fill_mbus_format(&format.format, &f->fmt.pix,
751 current_fmt->mbus_code);
752 ret = v4l2_subdev_call(isc->current_subdev->sd, pad,
753 set_fmt, NULL, &format);
754 if (ret < 0)
755 return ret;
757 isc->fmt = *f;
758 isc->current_fmt = current_fmt;
760 return 0;
763 static int isc_s_fmt_vid_cap(struct file *file, void *priv,
764 struct v4l2_format *f)
766 struct isc_device *isc = video_drvdata(file);
768 if (vb2_is_streaming(&isc->vb2_vidq))
769 return -EBUSY;
771 return isc_set_fmt(isc, f);
774 static int isc_try_fmt_vid_cap(struct file *file, void *priv,
775 struct v4l2_format *f)
777 struct isc_device *isc = video_drvdata(file);
779 return isc_try_fmt(isc, f, NULL);
782 static int isc_enum_input(struct file *file, void *priv,
783 struct v4l2_input *inp)
785 if (inp->index != 0)
786 return -EINVAL;
788 inp->type = V4L2_INPUT_TYPE_CAMERA;
789 inp->std = 0;
790 strcpy(inp->name, "Camera");
792 return 0;
795 static int isc_g_input(struct file *file, void *priv, unsigned int *i)
797 *i = 0;
799 return 0;
802 static int isc_s_input(struct file *file, void *priv, unsigned int i)
804 if (i > 0)
805 return -EINVAL;
807 return 0;
810 static int isc_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
812 struct isc_device *isc = video_drvdata(file);
814 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
815 return -EINVAL;
817 return v4l2_subdev_call(isc->current_subdev->sd, video, g_parm, a);
820 static int isc_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
822 struct isc_device *isc = video_drvdata(file);
824 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
825 return -EINVAL;
827 return v4l2_subdev_call(isc->current_subdev->sd, video, s_parm, a);
830 static int isc_enum_framesizes(struct file *file, void *fh,
831 struct v4l2_frmsizeenum *fsize)
833 struct isc_device *isc = video_drvdata(file);
834 const struct isc_format *isc_fmt;
835 struct v4l2_subdev_frame_size_enum fse = {
836 .index = fsize->index,
837 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
839 int ret;
841 isc_fmt = find_format_by_fourcc(isc, fsize->pixel_format);
842 if (!isc_fmt)
843 return -EINVAL;
845 fse.code = isc_fmt->mbus_code;
847 ret = v4l2_subdev_call(isc->current_subdev->sd, pad, enum_frame_size,
848 NULL, &fse);
849 if (ret)
850 return ret;
852 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
853 fsize->discrete.width = fse.max_width;
854 fsize->discrete.height = fse.max_height;
856 return 0;
859 static int isc_enum_frameintervals(struct file *file, void *fh,
860 struct v4l2_frmivalenum *fival)
862 struct isc_device *isc = video_drvdata(file);
863 const struct isc_format *isc_fmt;
864 struct v4l2_subdev_frame_interval_enum fie = {
865 .index = fival->index,
866 .width = fival->width,
867 .height = fival->height,
868 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
870 int ret;
872 isc_fmt = find_format_by_fourcc(isc, fival->pixel_format);
873 if (!isc_fmt)
874 return -EINVAL;
876 fie.code = isc_fmt->mbus_code;
878 ret = v4l2_subdev_call(isc->current_subdev->sd, pad,
879 enum_frame_interval, NULL, &fie);
880 if (ret)
881 return ret;
883 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
884 fival->discrete = fie.interval;
886 return 0;
889 static const struct v4l2_ioctl_ops isc_ioctl_ops = {
890 .vidioc_querycap = isc_querycap,
891 .vidioc_enum_fmt_vid_cap = isc_enum_fmt_vid_cap,
892 .vidioc_g_fmt_vid_cap = isc_g_fmt_vid_cap,
893 .vidioc_s_fmt_vid_cap = isc_s_fmt_vid_cap,
894 .vidioc_try_fmt_vid_cap = isc_try_fmt_vid_cap,
896 .vidioc_enum_input = isc_enum_input,
897 .vidioc_g_input = isc_g_input,
898 .vidioc_s_input = isc_s_input,
900 .vidioc_reqbufs = vb2_ioctl_reqbufs,
901 .vidioc_querybuf = vb2_ioctl_querybuf,
902 .vidioc_qbuf = vb2_ioctl_qbuf,
903 .vidioc_expbuf = vb2_ioctl_expbuf,
904 .vidioc_dqbuf = vb2_ioctl_dqbuf,
905 .vidioc_create_bufs = vb2_ioctl_create_bufs,
906 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
907 .vidioc_streamon = vb2_ioctl_streamon,
908 .vidioc_streamoff = vb2_ioctl_streamoff,
910 .vidioc_g_parm = isc_g_parm,
911 .vidioc_s_parm = isc_s_parm,
912 .vidioc_enum_framesizes = isc_enum_framesizes,
913 .vidioc_enum_frameintervals = isc_enum_frameintervals,
916 static int isc_open(struct file *file)
918 struct isc_device *isc = video_drvdata(file);
919 struct v4l2_subdev *sd = isc->current_subdev->sd;
920 int ret;
922 if (mutex_lock_interruptible(&isc->lock))
923 return -ERESTARTSYS;
925 ret = v4l2_fh_open(file);
926 if (ret < 0)
927 goto unlock;
929 if (!v4l2_fh_is_singular_file(file))
930 goto unlock;
932 ret = v4l2_subdev_call(sd, core, s_power, 1);
933 if (ret < 0 && ret != -ENOIOCTLCMD) {
934 v4l2_fh_release(file);
935 goto unlock;
938 ret = isc_set_fmt(isc, &isc->fmt);
939 if (ret) {
940 v4l2_subdev_call(sd, core, s_power, 0);
941 v4l2_fh_release(file);
944 unlock:
945 mutex_unlock(&isc->lock);
946 return ret;
949 static int isc_release(struct file *file)
951 struct isc_device *isc = video_drvdata(file);
952 struct v4l2_subdev *sd = isc->current_subdev->sd;
953 bool fh_singular;
954 int ret;
956 mutex_lock(&isc->lock);
958 fh_singular = v4l2_fh_is_singular_file(file);
960 ret = _vb2_fop_release(file, NULL);
962 if (fh_singular)
963 v4l2_subdev_call(sd, core, s_power, 0);
965 mutex_unlock(&isc->lock);
967 return ret;
970 static const struct v4l2_file_operations isc_fops = {
971 .owner = THIS_MODULE,
972 .open = isc_open,
973 .release = isc_release,
974 .unlocked_ioctl = video_ioctl2,
975 .read = vb2_fop_read,
976 .mmap = vb2_fop_mmap,
977 .poll = vb2_fop_poll,
980 static irqreturn_t isc_interrupt(int irq, void *dev_id)
982 struct isc_device *isc = (struct isc_device *)dev_id;
983 struct regmap *regmap = isc->regmap;
984 u32 isc_intsr, isc_intmask, pending;
985 irqreturn_t ret = IRQ_NONE;
987 spin_lock(&isc->dma_queue_lock);
989 regmap_read(regmap, ISC_INTSR, &isc_intsr);
990 regmap_read(regmap, ISC_INTMASK, &isc_intmask);
992 pending = isc_intsr & isc_intmask;
994 if (likely(pending & ISC_INT_DDONE)) {
995 if (isc->cur_frm) {
996 struct vb2_v4l2_buffer *vbuf = &isc->cur_frm->vb;
997 struct vb2_buffer *vb = &vbuf->vb2_buf;
999 vb->timestamp = ktime_get_ns();
1000 vbuf->sequence = isc->sequence++;
1001 vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
1002 isc->cur_frm = NULL;
1005 if (!list_empty(&isc->dma_queue) && !isc->stop) {
1006 isc->cur_frm = list_first_entry(&isc->dma_queue,
1007 struct isc_buffer, list);
1008 list_del(&isc->cur_frm->list);
1010 isc_start_dma(regmap, isc->cur_frm,
1011 isc->current_fmt->reg_dctrl_dview);
1014 if (isc->stop)
1015 complete(&isc->comp);
1017 ret = IRQ_HANDLED;
1020 spin_unlock(&isc->dma_queue_lock);
1022 return ret;
1025 static int isc_async_bound(struct v4l2_async_notifier *notifier,
1026 struct v4l2_subdev *subdev,
1027 struct v4l2_async_subdev *asd)
1029 struct isc_device *isc = container_of(notifier->v4l2_dev,
1030 struct isc_device, v4l2_dev);
1031 struct isc_subdev_entity *subdev_entity =
1032 container_of(notifier, struct isc_subdev_entity, notifier);
1034 if (video_is_registered(&isc->video_dev)) {
1035 v4l2_err(&isc->v4l2_dev, "only supports one sub-device.\n");
1036 return -EBUSY;
1039 subdev_entity->sd = subdev;
1041 return 0;
1044 static void isc_async_unbind(struct v4l2_async_notifier *notifier,
1045 struct v4l2_subdev *subdev,
1046 struct v4l2_async_subdev *asd)
1048 struct isc_device *isc = container_of(notifier->v4l2_dev,
1049 struct isc_device, v4l2_dev);
1051 video_unregister_device(&isc->video_dev);
1052 if (isc->current_subdev->config)
1053 v4l2_subdev_free_pad_config(isc->current_subdev->config);
1056 static struct isc_format *find_format_by_code(unsigned int code, int *index)
1058 struct isc_format *fmt = &isc_formats[0];
1059 unsigned int i;
1061 for (i = 0; i < ARRAY_SIZE(isc_formats); i++) {
1062 if (fmt->mbus_code == code) {
1063 *index = i;
1064 return fmt;
1067 fmt++;
1070 return NULL;
1073 static int isc_formats_init(struct isc_device *isc)
1075 struct isc_format *fmt;
1076 struct v4l2_subdev *subdev = isc->current_subdev->sd;
1077 int num_fmts = 0, i, j;
1078 struct v4l2_subdev_mbus_code_enum mbus_code = {
1079 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1082 fmt = &isc_formats[0];
1083 for (i = 0; i < ARRAY_SIZE(isc_formats); i++) {
1084 fmt->support = false;
1085 fmt++;
1088 while (!v4l2_subdev_call(subdev, pad, enum_mbus_code,
1089 NULL, &mbus_code)) {
1090 mbus_code.index++;
1091 fmt = find_format_by_code(mbus_code.code, &i);
1092 if (!fmt)
1093 continue;
1095 fmt->support = true;
1096 num_fmts++;
1099 if (!num_fmts)
1100 return -ENXIO;
1102 isc->num_user_formats = num_fmts;
1103 isc->user_formats = devm_kcalloc(isc->dev,
1104 num_fmts, sizeof(struct isc_format *),
1105 GFP_KERNEL);
1106 if (!isc->user_formats) {
1107 v4l2_err(&isc->v4l2_dev, "could not allocate memory\n");
1108 return -ENOMEM;
1111 fmt = &isc_formats[0];
1112 for (i = 0, j = 0; i < ARRAY_SIZE(isc_formats); i++) {
1113 if (fmt->support)
1114 isc->user_formats[j++] = fmt;
1116 fmt++;
1119 return 0;
1122 static int isc_set_default_fmt(struct isc_device *isc)
1124 struct v4l2_format f = {
1125 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1126 .fmt.pix = {
1127 .width = VGA_WIDTH,
1128 .height = VGA_HEIGHT,
1129 .field = V4L2_FIELD_NONE,
1130 .pixelformat = isc->user_formats[0]->fourcc,
1133 int ret;
1135 ret = isc_try_fmt(isc, &f, NULL);
1136 if (ret)
1137 return ret;
1139 isc->current_fmt = isc->user_formats[0];
1140 isc->fmt = f;
1142 return 0;
1145 static int isc_async_complete(struct v4l2_async_notifier *notifier)
1147 struct isc_device *isc = container_of(notifier->v4l2_dev,
1148 struct isc_device, v4l2_dev);
1149 struct isc_subdev_entity *sd_entity;
1150 struct video_device *vdev = &isc->video_dev;
1151 struct vb2_queue *q = &isc->vb2_vidq;
1152 int ret;
1154 isc->current_subdev = container_of(notifier,
1155 struct isc_subdev_entity, notifier);
1156 sd_entity = isc->current_subdev;
1158 mutex_init(&isc->lock);
1159 init_completion(&isc->comp);
1161 /* Initialize videobuf2 queue */
1162 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1163 q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
1164 q->drv_priv = isc;
1165 q->buf_struct_size = sizeof(struct isc_buffer);
1166 q->ops = &isc_vb2_ops;
1167 q->mem_ops = &vb2_dma_contig_memops;
1168 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1169 q->lock = &isc->lock;
1170 q->min_buffers_needed = 1;
1171 q->dev = isc->dev;
1173 ret = vb2_queue_init(q);
1174 if (ret < 0) {
1175 v4l2_err(&isc->v4l2_dev,
1176 "vb2_queue_init() failed: %d\n", ret);
1177 return ret;
1180 /* Init video dma queues */
1181 INIT_LIST_HEAD(&isc->dma_queue);
1182 spin_lock_init(&isc->dma_queue_lock);
1184 sd_entity->config = v4l2_subdev_alloc_pad_config(sd_entity->sd);
1185 if (sd_entity->config == NULL)
1186 return -ENOMEM;
1188 ret = isc_formats_init(isc);
1189 if (ret < 0) {
1190 v4l2_err(&isc->v4l2_dev,
1191 "Init format failed: %d\n", ret);
1192 return ret;
1195 ret = isc_set_default_fmt(isc);
1196 if (ret) {
1197 v4l2_err(&isc->v4l2_dev, "Could not set default format\n");
1198 return ret;
1201 /* Register video device */
1202 strlcpy(vdev->name, ATMEL_ISC_NAME, sizeof(vdev->name));
1203 vdev->release = video_device_release_empty;
1204 vdev->fops = &isc_fops;
1205 vdev->ioctl_ops = &isc_ioctl_ops;
1206 vdev->v4l2_dev = &isc->v4l2_dev;
1207 vdev->vfl_dir = VFL_DIR_RX;
1208 vdev->queue = q;
1209 vdev->lock = &isc->lock;
1210 vdev->ctrl_handler = isc->current_subdev->sd->ctrl_handler;
1211 vdev->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE;
1212 video_set_drvdata(vdev, isc);
1214 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1215 if (ret < 0) {
1216 v4l2_err(&isc->v4l2_dev,
1217 "video_register_device failed: %d\n", ret);
1218 return ret;
1221 return 0;
1224 static void isc_subdev_cleanup(struct isc_device *isc)
1226 struct isc_subdev_entity *subdev_entity;
1228 list_for_each_entry(subdev_entity, &isc->subdev_entities, list)
1229 v4l2_async_notifier_unregister(&subdev_entity->notifier);
1231 INIT_LIST_HEAD(&isc->subdev_entities);
1234 static int isc_pipeline_init(struct isc_device *isc)
1236 struct device *dev = isc->dev;
1237 struct regmap *regmap = isc->regmap;
1238 struct regmap_field *regs;
1239 unsigned int i;
1241 /* WB-->CFA-->CC-->GAM-->CSC-->CBC-->SUB422-->SUB420 */
1242 const struct reg_field regfields[ISC_PIPE_LINE_NODE_NUM] = {
1243 REG_FIELD(ISC_WB_CTRL, 0, 0),
1244 REG_FIELD(ISC_CFA_CTRL, 0, 0),
1245 REG_FIELD(ISC_CC_CTRL, 0, 0),
1246 REG_FIELD(ISC_GAM_CTRL, 0, 0),
1247 REG_FIELD(ISC_GAM_CTRL, 1, 1),
1248 REG_FIELD(ISC_GAM_CTRL, 2, 2),
1249 REG_FIELD(ISC_GAM_CTRL, 3, 3),
1250 REG_FIELD(ISC_CSC_CTRL, 0, 0),
1251 REG_FIELD(ISC_CBC_CTRL, 0, 0),
1252 REG_FIELD(ISC_SUB422_CTRL, 0, 0),
1253 REG_FIELD(ISC_SUB420_CTRL, 0, 0),
1256 for (i = 0; i < ISC_PIPE_LINE_NODE_NUM; i++) {
1257 regs = devm_regmap_field_alloc(dev, regmap, regfields[i]);
1258 if (IS_ERR(regs))
1259 return PTR_ERR(regs);
1261 isc->pipeline[i] = regs;
1264 return 0;
1267 static int isc_parse_dt(struct device *dev, struct isc_device *isc)
1269 struct device_node *np = dev->of_node;
1270 struct device_node *epn = NULL, *rem;
1271 struct v4l2_of_endpoint v4l2_epn;
1272 struct isc_subdev_entity *subdev_entity;
1273 unsigned int flags;
1274 int ret;
1276 INIT_LIST_HEAD(&isc->subdev_entities);
1278 for (; ;) {
1279 epn = of_graph_get_next_endpoint(np, epn);
1280 if (!epn)
1281 break;
1283 rem = of_graph_get_remote_port_parent(epn);
1284 if (!rem) {
1285 dev_notice(dev, "Remote device at %s not found\n",
1286 of_node_full_name(epn));
1287 continue;
1290 ret = v4l2_of_parse_endpoint(epn, &v4l2_epn);
1291 if (ret) {
1292 of_node_put(rem);
1293 ret = -EINVAL;
1294 dev_err(dev, "Could not parse the endpoint\n");
1295 break;
1298 subdev_entity = devm_kzalloc(dev,
1299 sizeof(*subdev_entity), GFP_KERNEL);
1300 if (subdev_entity == NULL) {
1301 of_node_put(rem);
1302 ret = -ENOMEM;
1303 break;
1306 subdev_entity->asd = devm_kzalloc(dev,
1307 sizeof(*subdev_entity->asd), GFP_KERNEL);
1308 if (subdev_entity->asd == NULL) {
1309 of_node_put(rem);
1310 ret = -ENOMEM;
1311 break;
1314 flags = v4l2_epn.bus.parallel.flags;
1316 if (flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
1317 subdev_entity->pfe_cfg0 = ISC_PFE_CFG0_HPOL_LOW;
1319 if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
1320 subdev_entity->pfe_cfg0 |= ISC_PFE_CFG0_VPOL_LOW;
1322 if (flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
1323 subdev_entity->pfe_cfg0 |= ISC_PFE_CFG0_PPOL_LOW;
1325 subdev_entity->asd->match_type = V4L2_ASYNC_MATCH_OF;
1326 subdev_entity->asd->match.of.node = rem;
1327 list_add_tail(&subdev_entity->list, &isc->subdev_entities);
1330 of_node_put(epn);
1331 return ret;
1334 /* regmap configuration */
1335 #define ATMEL_ISC_REG_MAX 0xbfc
1336 static const struct regmap_config isc_regmap_config = {
1337 .reg_bits = 32,
1338 .reg_stride = 4,
1339 .val_bits = 32,
1340 .max_register = ATMEL_ISC_REG_MAX,
1343 static int atmel_isc_probe(struct platform_device *pdev)
1345 struct device *dev = &pdev->dev;
1346 struct isc_device *isc;
1347 struct resource *res;
1348 void __iomem *io_base;
1349 struct isc_subdev_entity *subdev_entity;
1350 int irq;
1351 int ret;
1353 isc = devm_kzalloc(dev, sizeof(*isc), GFP_KERNEL);
1354 if (!isc)
1355 return -ENOMEM;
1357 platform_set_drvdata(pdev, isc);
1358 isc->dev = dev;
1360 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1361 io_base = devm_ioremap_resource(dev, res);
1362 if (IS_ERR(io_base))
1363 return PTR_ERR(io_base);
1365 isc->regmap = devm_regmap_init_mmio(dev, io_base, &isc_regmap_config);
1366 if (IS_ERR(isc->regmap)) {
1367 ret = PTR_ERR(isc->regmap);
1368 dev_err(dev, "failed to init register map: %d\n", ret);
1369 return ret;
1372 irq = platform_get_irq(pdev, 0);
1373 if (irq < 0) {
1374 ret = irq;
1375 dev_err(dev, "failed to get irq: %d\n", ret);
1376 return ret;
1379 ret = devm_request_irq(dev, irq, isc_interrupt, 0,
1380 ATMEL_ISC_NAME, isc);
1381 if (ret < 0) {
1382 dev_err(dev, "can't register ISR for IRQ %u (ret=%i)\n",
1383 irq, ret);
1384 return ret;
1387 ret = isc_pipeline_init(isc);
1388 if (ret)
1389 return ret;
1391 isc->hclock = devm_clk_get(dev, "hclock");
1392 if (IS_ERR(isc->hclock)) {
1393 ret = PTR_ERR(isc->hclock);
1394 dev_err(dev, "failed to get hclock: %d\n", ret);
1395 return ret;
1398 ret = isc_clk_init(isc);
1399 if (ret) {
1400 dev_err(dev, "failed to init isc clock: %d\n", ret);
1401 goto clean_isc_clk;
1404 isc->ispck = isc->isc_clks[ISC_ISPCK].clk;
1406 /* ispck should be greater or equal to hclock */
1407 ret = clk_set_rate(isc->ispck, clk_get_rate(isc->hclock));
1408 if (ret) {
1409 dev_err(dev, "failed to set ispck rate: %d\n", ret);
1410 goto clean_isc_clk;
1413 ret = v4l2_device_register(dev, &isc->v4l2_dev);
1414 if (ret) {
1415 dev_err(dev, "unable to register v4l2 device.\n");
1416 goto clean_isc_clk;
1419 ret = isc_parse_dt(dev, isc);
1420 if (ret) {
1421 dev_err(dev, "fail to parse device tree\n");
1422 goto unregister_v4l2_device;
1425 if (list_empty(&isc->subdev_entities)) {
1426 dev_err(dev, "no subdev found\n");
1427 ret = -ENODEV;
1428 goto unregister_v4l2_device;
1431 list_for_each_entry(subdev_entity, &isc->subdev_entities, list) {
1432 subdev_entity->notifier.subdevs = &subdev_entity->asd;
1433 subdev_entity->notifier.num_subdevs = 1;
1434 subdev_entity->notifier.bound = isc_async_bound;
1435 subdev_entity->notifier.unbind = isc_async_unbind;
1436 subdev_entity->notifier.complete = isc_async_complete;
1438 ret = v4l2_async_notifier_register(&isc->v4l2_dev,
1439 &subdev_entity->notifier);
1440 if (ret) {
1441 dev_err(dev, "fail to register async notifier\n");
1442 goto cleanup_subdev;
1445 if (video_is_registered(&isc->video_dev))
1446 break;
1449 pm_runtime_enable(dev);
1451 return 0;
1453 cleanup_subdev:
1454 isc_subdev_cleanup(isc);
1456 unregister_v4l2_device:
1457 v4l2_device_unregister(&isc->v4l2_dev);
1459 clean_isc_clk:
1460 isc_clk_cleanup(isc);
1462 return ret;
1465 static int atmel_isc_remove(struct platform_device *pdev)
1467 struct isc_device *isc = platform_get_drvdata(pdev);
1469 pm_runtime_disable(&pdev->dev);
1471 isc_subdev_cleanup(isc);
1473 v4l2_device_unregister(&isc->v4l2_dev);
1475 isc_clk_cleanup(isc);
1477 return 0;
1480 static int __maybe_unused isc_runtime_suspend(struct device *dev)
1482 struct isc_device *isc = dev_get_drvdata(dev);
1484 clk_disable_unprepare(isc->ispck);
1485 clk_disable_unprepare(isc->hclock);
1487 return 0;
1490 static int __maybe_unused isc_runtime_resume(struct device *dev)
1492 struct isc_device *isc = dev_get_drvdata(dev);
1493 int ret;
1495 ret = clk_prepare_enable(isc->hclock);
1496 if (ret)
1497 return ret;
1499 return clk_prepare_enable(isc->ispck);
1502 static const struct dev_pm_ops atmel_isc_dev_pm_ops = {
1503 SET_RUNTIME_PM_OPS(isc_runtime_suspend, isc_runtime_resume, NULL)
1506 static const struct of_device_id atmel_isc_of_match[] = {
1507 { .compatible = "atmel,sama5d2-isc" },
1510 MODULE_DEVICE_TABLE(of, atmel_isc_of_match);
1512 static struct platform_driver atmel_isc_driver = {
1513 .probe = atmel_isc_probe,
1514 .remove = atmel_isc_remove,
1515 .driver = {
1516 .name = ATMEL_ISC_NAME,
1517 .pm = &atmel_isc_dev_pm_ops,
1518 .of_match_table = of_match_ptr(atmel_isc_of_match),
1522 module_platform_driver(atmel_isc_driver);
1524 MODULE_AUTHOR("Songjun Wu <songjun.wu@microchip.com>");
1525 MODULE_DESCRIPTION("The V4L2 driver for Atmel-ISC");
1526 MODULE_LICENSE("GPL v2");
1527 MODULE_SUPPORTED_DEVICE("video");