[media] omap3isp: Remove boilerplate disclaimer and FSF address
[sfrench/cifs-2.6.git] / drivers / media / platform / omap3isp / ispccdc.c
1 /*
2  * ispccdc.c
3  *
4  * TI OMAP3 ISP - CCDC module
5  *
6  * Copyright (C) 2009-2010 Nokia Corporation
7  * Copyright (C) 2009 Texas Instruments, Inc.
8  *
9  * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
10  *           Sakari Ailus <sakari.ailus@iki.fi>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License version 2 as
14  * published by the Free Software Foundation.
15  */
16
17 #include <linux/module.h>
18 #include <linux/uaccess.h>
19 #include <linux/delay.h>
20 #include <linux/device.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/mm.h>
23 #include <linux/sched.h>
24 #include <linux/slab.h>
25 #include <media/v4l2-event.h>
26
27 #include "isp.h"
28 #include "ispreg.h"
29 #include "ispccdc.h"
30
31 #define CCDC_MIN_WIDTH          32
32 #define CCDC_MIN_HEIGHT         32
33
34 static struct v4l2_mbus_framefmt *
35 __ccdc_get_format(struct isp_ccdc_device *ccdc, struct v4l2_subdev_fh *fh,
36                   unsigned int pad, enum v4l2_subdev_format_whence which);
37
38 static const unsigned int ccdc_fmts[] = {
39         V4L2_MBUS_FMT_Y8_1X8,
40         V4L2_MBUS_FMT_Y10_1X10,
41         V4L2_MBUS_FMT_Y12_1X12,
42         V4L2_MBUS_FMT_SGRBG8_1X8,
43         V4L2_MBUS_FMT_SRGGB8_1X8,
44         V4L2_MBUS_FMT_SBGGR8_1X8,
45         V4L2_MBUS_FMT_SGBRG8_1X8,
46         V4L2_MBUS_FMT_SGRBG10_1X10,
47         V4L2_MBUS_FMT_SRGGB10_1X10,
48         V4L2_MBUS_FMT_SBGGR10_1X10,
49         V4L2_MBUS_FMT_SGBRG10_1X10,
50         V4L2_MBUS_FMT_SGRBG12_1X12,
51         V4L2_MBUS_FMT_SRGGB12_1X12,
52         V4L2_MBUS_FMT_SBGGR12_1X12,
53         V4L2_MBUS_FMT_SGBRG12_1X12,
54         V4L2_MBUS_FMT_YUYV8_2X8,
55         V4L2_MBUS_FMT_UYVY8_2X8,
56 };
57
58 /*
59  * ccdc_print_status - Print current CCDC Module register values.
60  * @ccdc: Pointer to ISP CCDC device.
61  *
62  * Also prints other debug information stored in the CCDC module.
63  */
64 #define CCDC_PRINT_REGISTER(isp, name)\
65         dev_dbg(isp->dev, "###CCDC " #name "=0x%08x\n", \
66                 isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_##name))
67
68 static void ccdc_print_status(struct isp_ccdc_device *ccdc)
69 {
70         struct isp_device *isp = to_isp_device(ccdc);
71
72         dev_dbg(isp->dev, "-------------CCDC Register dump-------------\n");
73
74         CCDC_PRINT_REGISTER(isp, PCR);
75         CCDC_PRINT_REGISTER(isp, SYN_MODE);
76         CCDC_PRINT_REGISTER(isp, HD_VD_WID);
77         CCDC_PRINT_REGISTER(isp, PIX_LINES);
78         CCDC_PRINT_REGISTER(isp, HORZ_INFO);
79         CCDC_PRINT_REGISTER(isp, VERT_START);
80         CCDC_PRINT_REGISTER(isp, VERT_LINES);
81         CCDC_PRINT_REGISTER(isp, CULLING);
82         CCDC_PRINT_REGISTER(isp, HSIZE_OFF);
83         CCDC_PRINT_REGISTER(isp, SDOFST);
84         CCDC_PRINT_REGISTER(isp, SDR_ADDR);
85         CCDC_PRINT_REGISTER(isp, CLAMP);
86         CCDC_PRINT_REGISTER(isp, DCSUB);
87         CCDC_PRINT_REGISTER(isp, COLPTN);
88         CCDC_PRINT_REGISTER(isp, BLKCMP);
89         CCDC_PRINT_REGISTER(isp, FPC);
90         CCDC_PRINT_REGISTER(isp, FPC_ADDR);
91         CCDC_PRINT_REGISTER(isp, VDINT);
92         CCDC_PRINT_REGISTER(isp, ALAW);
93         CCDC_PRINT_REGISTER(isp, REC656IF);
94         CCDC_PRINT_REGISTER(isp, CFG);
95         CCDC_PRINT_REGISTER(isp, FMTCFG);
96         CCDC_PRINT_REGISTER(isp, FMT_HORZ);
97         CCDC_PRINT_REGISTER(isp, FMT_VERT);
98         CCDC_PRINT_REGISTER(isp, PRGEVEN0);
99         CCDC_PRINT_REGISTER(isp, PRGEVEN1);
100         CCDC_PRINT_REGISTER(isp, PRGODD0);
101         CCDC_PRINT_REGISTER(isp, PRGODD1);
102         CCDC_PRINT_REGISTER(isp, VP_OUT);
103         CCDC_PRINT_REGISTER(isp, LSC_CONFIG);
104         CCDC_PRINT_REGISTER(isp, LSC_INITIAL);
105         CCDC_PRINT_REGISTER(isp, LSC_TABLE_BASE);
106         CCDC_PRINT_REGISTER(isp, LSC_TABLE_OFFSET);
107
108         dev_dbg(isp->dev, "--------------------------------------------\n");
109 }
110
111 /*
112  * omap3isp_ccdc_busy - Get busy state of the CCDC.
113  * @ccdc: Pointer to ISP CCDC device.
114  */
115 int omap3isp_ccdc_busy(struct isp_ccdc_device *ccdc)
116 {
117         struct isp_device *isp = to_isp_device(ccdc);
118
119         return isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_PCR) &
120                 ISPCCDC_PCR_BUSY;
121 }
122
123 /* -----------------------------------------------------------------------------
124  * Lens Shading Compensation
125  */
126
127 /*
128  * ccdc_lsc_validate_config - Check that LSC configuration is valid.
129  * @ccdc: Pointer to ISP CCDC device.
130  * @lsc_cfg: the LSC configuration to check.
131  *
132  * Returns 0 if the LSC configuration is valid, or -EINVAL if invalid.
133  */
134 static int ccdc_lsc_validate_config(struct isp_ccdc_device *ccdc,
135                                     struct omap3isp_ccdc_lsc_config *lsc_cfg)
136 {
137         struct isp_device *isp = to_isp_device(ccdc);
138         struct v4l2_mbus_framefmt *format;
139         unsigned int paxel_width, paxel_height;
140         unsigned int paxel_shift_x, paxel_shift_y;
141         unsigned int min_width, min_height, min_size;
142         unsigned int input_width, input_height;
143
144         paxel_shift_x = lsc_cfg->gain_mode_m;
145         paxel_shift_y = lsc_cfg->gain_mode_n;
146
147         if ((paxel_shift_x < 2) || (paxel_shift_x > 6) ||
148             (paxel_shift_y < 2) || (paxel_shift_y > 6)) {
149                 dev_dbg(isp->dev, "CCDC: LSC: Invalid paxel size\n");
150                 return -EINVAL;
151         }
152
153         if (lsc_cfg->offset & 3) {
154                 dev_dbg(isp->dev, "CCDC: LSC: Offset must be a multiple of "
155                         "4\n");
156                 return -EINVAL;
157         }
158
159         if ((lsc_cfg->initial_x & 1) || (lsc_cfg->initial_y & 1)) {
160                 dev_dbg(isp->dev, "CCDC: LSC: initial_x and y must be even\n");
161                 return -EINVAL;
162         }
163
164         format = __ccdc_get_format(ccdc, NULL, CCDC_PAD_SINK,
165                                    V4L2_SUBDEV_FORMAT_ACTIVE);
166         input_width = format->width;
167         input_height = format->height;
168
169         /* Calculate minimum bytesize for validation */
170         paxel_width = 1 << paxel_shift_x;
171         min_width = ((input_width + lsc_cfg->initial_x + paxel_width - 1)
172                      >> paxel_shift_x) + 1;
173
174         paxel_height = 1 << paxel_shift_y;
175         min_height = ((input_height + lsc_cfg->initial_y + paxel_height - 1)
176                      >> paxel_shift_y) + 1;
177
178         min_size = 4 * min_width * min_height;
179         if (min_size > lsc_cfg->size) {
180                 dev_dbg(isp->dev, "CCDC: LSC: too small table\n");
181                 return -EINVAL;
182         }
183         if (lsc_cfg->offset < (min_width * 4)) {
184                 dev_dbg(isp->dev, "CCDC: LSC: Offset is too small\n");
185                 return -EINVAL;
186         }
187         if ((lsc_cfg->size / lsc_cfg->offset) < min_height) {
188                 dev_dbg(isp->dev, "CCDC: LSC: Wrong size/offset combination\n");
189                 return -EINVAL;
190         }
191         return 0;
192 }
193
194 /*
195  * ccdc_lsc_program_table - Program Lens Shading Compensation table address.
196  * @ccdc: Pointer to ISP CCDC device.
197  */
198 static void ccdc_lsc_program_table(struct isp_ccdc_device *ccdc,
199                                    dma_addr_t addr)
200 {
201         isp_reg_writel(to_isp_device(ccdc), addr,
202                        OMAP3_ISP_IOMEM_CCDC, ISPCCDC_LSC_TABLE_BASE);
203 }
204
205 /*
206  * ccdc_lsc_setup_regs - Configures the lens shading compensation module
207  * @ccdc: Pointer to ISP CCDC device.
208  */
209 static void ccdc_lsc_setup_regs(struct isp_ccdc_device *ccdc,
210                                 struct omap3isp_ccdc_lsc_config *cfg)
211 {
212         struct isp_device *isp = to_isp_device(ccdc);
213         int reg;
214
215         isp_reg_writel(isp, cfg->offset, OMAP3_ISP_IOMEM_CCDC,
216                        ISPCCDC_LSC_TABLE_OFFSET);
217
218         reg = 0;
219         reg |= cfg->gain_mode_n << ISPCCDC_LSC_GAIN_MODE_N_SHIFT;
220         reg |= cfg->gain_mode_m << ISPCCDC_LSC_GAIN_MODE_M_SHIFT;
221         reg |= cfg->gain_format << ISPCCDC_LSC_GAIN_FORMAT_SHIFT;
222         isp_reg_writel(isp, reg, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_LSC_CONFIG);
223
224         reg = 0;
225         reg &= ~ISPCCDC_LSC_INITIAL_X_MASK;
226         reg |= cfg->initial_x << ISPCCDC_LSC_INITIAL_X_SHIFT;
227         reg &= ~ISPCCDC_LSC_INITIAL_Y_MASK;
228         reg |= cfg->initial_y << ISPCCDC_LSC_INITIAL_Y_SHIFT;
229         isp_reg_writel(isp, reg, OMAP3_ISP_IOMEM_CCDC,
230                        ISPCCDC_LSC_INITIAL);
231 }
232
233 static int ccdc_lsc_wait_prefetch(struct isp_ccdc_device *ccdc)
234 {
235         struct isp_device *isp = to_isp_device(ccdc);
236         unsigned int wait;
237
238         isp_reg_writel(isp, IRQ0STATUS_CCDC_LSC_PREF_COMP_IRQ,
239                        OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0STATUS);
240
241         /* timeout 1 ms */
242         for (wait = 0; wait < 1000; wait++) {
243                 if (isp_reg_readl(isp, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0STATUS) &
244                                   IRQ0STATUS_CCDC_LSC_PREF_COMP_IRQ) {
245                         isp_reg_writel(isp, IRQ0STATUS_CCDC_LSC_PREF_COMP_IRQ,
246                                        OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0STATUS);
247                         return 0;
248                 }
249
250                 rmb();
251                 udelay(1);
252         }
253
254         return -ETIMEDOUT;
255 }
256
257 /*
258  * __ccdc_lsc_enable - Enables/Disables the Lens Shading Compensation module.
259  * @ccdc: Pointer to ISP CCDC device.
260  * @enable: 0 Disables LSC, 1 Enables LSC.
261  */
262 static int __ccdc_lsc_enable(struct isp_ccdc_device *ccdc, int enable)
263 {
264         struct isp_device *isp = to_isp_device(ccdc);
265         const struct v4l2_mbus_framefmt *format =
266                 __ccdc_get_format(ccdc, NULL, CCDC_PAD_SINK,
267                                   V4L2_SUBDEV_FORMAT_ACTIVE);
268
269         if ((format->code != V4L2_MBUS_FMT_SGRBG10_1X10) &&
270             (format->code != V4L2_MBUS_FMT_SRGGB10_1X10) &&
271             (format->code != V4L2_MBUS_FMT_SBGGR10_1X10) &&
272             (format->code != V4L2_MBUS_FMT_SGBRG10_1X10))
273                 return -EINVAL;
274
275         if (enable)
276                 omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_CCDC_LSC_READ);
277
278         isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_LSC_CONFIG,
279                         ISPCCDC_LSC_ENABLE, enable ? ISPCCDC_LSC_ENABLE : 0);
280
281         if (enable) {
282                 if (ccdc_lsc_wait_prefetch(ccdc) < 0) {
283                         isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC,
284                                     ISPCCDC_LSC_CONFIG, ISPCCDC_LSC_ENABLE);
285                         ccdc->lsc.state = LSC_STATE_STOPPED;
286                         dev_warn(to_device(ccdc), "LSC prefetch timeout\n");
287                         return -ETIMEDOUT;
288                 }
289                 ccdc->lsc.state = LSC_STATE_RUNNING;
290         } else {
291                 ccdc->lsc.state = LSC_STATE_STOPPING;
292         }
293
294         return 0;
295 }
296
297 static int ccdc_lsc_busy(struct isp_ccdc_device *ccdc)
298 {
299         struct isp_device *isp = to_isp_device(ccdc);
300
301         return isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_LSC_CONFIG) &
302                              ISPCCDC_LSC_BUSY;
303 }
304
305 /* __ccdc_lsc_configure - Apply a new configuration to the LSC engine
306  * @ccdc: Pointer to ISP CCDC device
307  * @req: New configuration request
308  *
309  * context: in_interrupt()
310  */
311 static int __ccdc_lsc_configure(struct isp_ccdc_device *ccdc,
312                                 struct ispccdc_lsc_config_req *req)
313 {
314         if (!req->enable)
315                 return -EINVAL;
316
317         if (ccdc_lsc_validate_config(ccdc, &req->config) < 0) {
318                 dev_dbg(to_device(ccdc), "Discard LSC configuration\n");
319                 return -EINVAL;
320         }
321
322         if (ccdc_lsc_busy(ccdc))
323                 return -EBUSY;
324
325         ccdc_lsc_setup_regs(ccdc, &req->config);
326         ccdc_lsc_program_table(ccdc, req->table.dma);
327         return 0;
328 }
329
330 /*
331  * ccdc_lsc_error_handler - Handle LSC prefetch error scenario.
332  * @ccdc: Pointer to ISP CCDC device.
333  *
334  * Disables LSC, and defers enablement to shadow registers update time.
335  */
336 static void ccdc_lsc_error_handler(struct isp_ccdc_device *ccdc)
337 {
338         struct isp_device *isp = to_isp_device(ccdc);
339         /*
340          * From OMAP3 TRM: When this event is pending, the module
341          * goes into transparent mode (output =input). Normal
342          * operation can be resumed at the start of the next frame
343          * after:
344          *  1) Clearing this event
345          *  2) Disabling the LSC module
346          *  3) Enabling it
347          */
348         isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_LSC_CONFIG,
349                     ISPCCDC_LSC_ENABLE);
350         ccdc->lsc.state = LSC_STATE_STOPPED;
351 }
352
353 static void ccdc_lsc_free_request(struct isp_ccdc_device *ccdc,
354                                   struct ispccdc_lsc_config_req *req)
355 {
356         struct isp_device *isp = to_isp_device(ccdc);
357
358         if (req == NULL)
359                 return;
360
361         if (req->table.addr) {
362                 sg_free_table(&req->table.sgt);
363                 dma_free_coherent(isp->dev, req->config.size, req->table.addr,
364                                   req->table.dma);
365         }
366
367         kfree(req);
368 }
369
370 static void ccdc_lsc_free_queue(struct isp_ccdc_device *ccdc,
371                                 struct list_head *queue)
372 {
373         struct ispccdc_lsc_config_req *req, *n;
374         unsigned long flags;
375
376         spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
377         list_for_each_entry_safe(req, n, queue, list) {
378                 list_del(&req->list);
379                 spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
380                 ccdc_lsc_free_request(ccdc, req);
381                 spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
382         }
383         spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
384 }
385
386 static void ccdc_lsc_free_table_work(struct work_struct *work)
387 {
388         struct isp_ccdc_device *ccdc;
389         struct ispccdc_lsc *lsc;
390
391         lsc = container_of(work, struct ispccdc_lsc, table_work);
392         ccdc = container_of(lsc, struct isp_ccdc_device, lsc);
393
394         ccdc_lsc_free_queue(ccdc, &lsc->free_queue);
395 }
396
397 /*
398  * ccdc_lsc_config - Configure the LSC module from a userspace request
399  *
400  * Store the request LSC configuration in the LSC engine request pointer. The
401  * configuration will be applied to the hardware when the CCDC will be enabled,
402  * or at the next LSC interrupt if the CCDC is already running.
403  */
404 static int ccdc_lsc_config(struct isp_ccdc_device *ccdc,
405                            struct omap3isp_ccdc_update_config *config)
406 {
407         struct isp_device *isp = to_isp_device(ccdc);
408         struct ispccdc_lsc_config_req *req;
409         unsigned long flags;
410         u16 update;
411         int ret;
412
413         update = config->update &
414                  (OMAP3ISP_CCDC_CONFIG_LSC | OMAP3ISP_CCDC_TBL_LSC);
415         if (!update)
416                 return 0;
417
418         if (update != (OMAP3ISP_CCDC_CONFIG_LSC | OMAP3ISP_CCDC_TBL_LSC)) {
419                 dev_dbg(to_device(ccdc), "%s: Both LSC configuration and table "
420                         "need to be supplied\n", __func__);
421                 return -EINVAL;
422         }
423
424         req = kzalloc(sizeof(*req), GFP_KERNEL);
425         if (req == NULL)
426                 return -ENOMEM;
427
428         if (config->flag & OMAP3ISP_CCDC_CONFIG_LSC) {
429                 if (copy_from_user(&req->config, config->lsc_cfg,
430                                    sizeof(req->config))) {
431                         ret = -EFAULT;
432                         goto done;
433                 }
434
435                 req->enable = 1;
436
437                 req->table.addr = dma_alloc_coherent(isp->dev, req->config.size,
438                                                      &req->table.dma,
439                                                      GFP_KERNEL);
440                 if (req->table.addr == NULL) {
441                         ret = -ENOMEM;
442                         goto done;
443                 }
444
445                 ret = dma_get_sgtable(isp->dev, &req->table.sgt,
446                                       req->table.addr, req->table.dma,
447                                       req->config.size);
448                 if (ret < 0)
449                         goto done;
450
451                 dma_sync_sg_for_cpu(isp->dev, req->table.sgt.sgl,
452                                     req->table.sgt.nents, DMA_TO_DEVICE);
453
454                 if (copy_from_user(req->table.addr, config->lsc,
455                                    req->config.size)) {
456                         ret = -EFAULT;
457                         goto done;
458                 }
459
460                 dma_sync_sg_for_device(isp->dev, req->table.sgt.sgl,
461                                        req->table.sgt.nents, DMA_TO_DEVICE);
462         }
463
464         spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
465         if (ccdc->lsc.request) {
466                 list_add_tail(&ccdc->lsc.request->list, &ccdc->lsc.free_queue);
467                 schedule_work(&ccdc->lsc.table_work);
468         }
469         ccdc->lsc.request = req;
470         spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
471
472         ret = 0;
473
474 done:
475         if (ret < 0)
476                 ccdc_lsc_free_request(ccdc, req);
477
478         return ret;
479 }
480
481 static inline int ccdc_lsc_is_configured(struct isp_ccdc_device *ccdc)
482 {
483         unsigned long flags;
484
485         spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
486         if (ccdc->lsc.active) {
487                 spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
488                 return 1;
489         }
490         spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
491         return 0;
492 }
493
494 static int ccdc_lsc_enable(struct isp_ccdc_device *ccdc)
495 {
496         struct ispccdc_lsc *lsc = &ccdc->lsc;
497
498         if (lsc->state != LSC_STATE_STOPPED)
499                 return -EINVAL;
500
501         if (lsc->active) {
502                 list_add_tail(&lsc->active->list, &lsc->free_queue);
503                 lsc->active = NULL;
504         }
505
506         if (__ccdc_lsc_configure(ccdc, lsc->request) < 0) {
507                 omap3isp_sbl_disable(to_isp_device(ccdc),
508                                 OMAP3_ISP_SBL_CCDC_LSC_READ);
509                 list_add_tail(&lsc->request->list, &lsc->free_queue);
510                 lsc->request = NULL;
511                 goto done;
512         }
513
514         lsc->active = lsc->request;
515         lsc->request = NULL;
516         __ccdc_lsc_enable(ccdc, 1);
517
518 done:
519         if (!list_empty(&lsc->free_queue))
520                 schedule_work(&lsc->table_work);
521
522         return 0;
523 }
524
525 /* -----------------------------------------------------------------------------
526  * Parameters configuration
527  */
528
529 /*
530  * ccdc_configure_clamp - Configure optical-black or digital clamping
531  * @ccdc: Pointer to ISP CCDC device.
532  *
533  * The CCDC performs either optical-black or digital clamp. Configure and enable
534  * the selected clamp method.
535  */
536 static void ccdc_configure_clamp(struct isp_ccdc_device *ccdc)
537 {
538         struct isp_device *isp = to_isp_device(ccdc);
539         u32 clamp;
540
541         if (ccdc->obclamp) {
542                 clamp  = ccdc->clamp.obgain << ISPCCDC_CLAMP_OBGAIN_SHIFT;
543                 clamp |= ccdc->clamp.oblen << ISPCCDC_CLAMP_OBSLEN_SHIFT;
544                 clamp |= ccdc->clamp.oblines << ISPCCDC_CLAMP_OBSLN_SHIFT;
545                 clamp |= ccdc->clamp.obstpixel << ISPCCDC_CLAMP_OBST_SHIFT;
546                 isp_reg_writel(isp, clamp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CLAMP);
547         } else {
548                 isp_reg_writel(isp, ccdc->clamp.dcsubval,
549                                OMAP3_ISP_IOMEM_CCDC, ISPCCDC_DCSUB);
550         }
551
552         isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CLAMP,
553                         ISPCCDC_CLAMP_CLAMPEN,
554                         ccdc->obclamp ? ISPCCDC_CLAMP_CLAMPEN : 0);
555 }
556
557 /*
558  * ccdc_configure_fpc - Configure Faulty Pixel Correction
559  * @ccdc: Pointer to ISP CCDC device.
560  */
561 static void ccdc_configure_fpc(struct isp_ccdc_device *ccdc)
562 {
563         struct isp_device *isp = to_isp_device(ccdc);
564
565         isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FPC, ISPCCDC_FPC_FPCEN);
566
567         if (!ccdc->fpc_en)
568                 return;
569
570         isp_reg_writel(isp, ccdc->fpc.dma, OMAP3_ISP_IOMEM_CCDC,
571                        ISPCCDC_FPC_ADDR);
572         /* The FPNUM field must be set before enabling FPC. */
573         isp_reg_writel(isp, (ccdc->fpc.fpnum << ISPCCDC_FPC_FPNUM_SHIFT),
574                        OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FPC);
575         isp_reg_writel(isp, (ccdc->fpc.fpnum << ISPCCDC_FPC_FPNUM_SHIFT) |
576                        ISPCCDC_FPC_FPCEN, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FPC);
577 }
578
579 /*
580  * ccdc_configure_black_comp - Configure Black Level Compensation.
581  * @ccdc: Pointer to ISP CCDC device.
582  */
583 static void ccdc_configure_black_comp(struct isp_ccdc_device *ccdc)
584 {
585         struct isp_device *isp = to_isp_device(ccdc);
586         u32 blcomp;
587
588         blcomp  = ccdc->blcomp.b_mg << ISPCCDC_BLKCMP_B_MG_SHIFT;
589         blcomp |= ccdc->blcomp.gb_g << ISPCCDC_BLKCMP_GB_G_SHIFT;
590         blcomp |= ccdc->blcomp.gr_cy << ISPCCDC_BLKCMP_GR_CY_SHIFT;
591         blcomp |= ccdc->blcomp.r_ye << ISPCCDC_BLKCMP_R_YE_SHIFT;
592
593         isp_reg_writel(isp, blcomp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_BLKCMP);
594 }
595
596 /*
597  * ccdc_configure_lpf - Configure Low-Pass Filter (LPF).
598  * @ccdc: Pointer to ISP CCDC device.
599  */
600 static void ccdc_configure_lpf(struct isp_ccdc_device *ccdc)
601 {
602         struct isp_device *isp = to_isp_device(ccdc);
603
604         isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SYN_MODE,
605                         ISPCCDC_SYN_MODE_LPF,
606                         ccdc->lpf ? ISPCCDC_SYN_MODE_LPF : 0);
607 }
608
609 /*
610  * ccdc_configure_alaw - Configure A-law compression.
611  * @ccdc: Pointer to ISP CCDC device.
612  */
613 static void ccdc_configure_alaw(struct isp_ccdc_device *ccdc)
614 {
615         struct isp_device *isp = to_isp_device(ccdc);
616         const struct isp_format_info *info;
617         u32 alaw = 0;
618
619         info = omap3isp_video_format_info(ccdc->formats[CCDC_PAD_SINK].code);
620
621         switch (info->width) {
622         case 8:
623                 return;
624
625         case 10:
626                 alaw = ISPCCDC_ALAW_GWDI_9_0;
627                 break;
628         case 11:
629                 alaw = ISPCCDC_ALAW_GWDI_10_1;
630                 break;
631         case 12:
632                 alaw = ISPCCDC_ALAW_GWDI_11_2;
633                 break;
634         case 13:
635                 alaw = ISPCCDC_ALAW_GWDI_12_3;
636                 break;
637         }
638
639         if (ccdc->alaw)
640                 alaw |= ISPCCDC_ALAW_CCDTBL;
641
642         isp_reg_writel(isp, alaw, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_ALAW);
643 }
644
645 /*
646  * ccdc_config_imgattr - Configure sensor image specific attributes.
647  * @ccdc: Pointer to ISP CCDC device.
648  * @colptn: Color pattern of the sensor.
649  */
650 static void ccdc_config_imgattr(struct isp_ccdc_device *ccdc, u32 colptn)
651 {
652         struct isp_device *isp = to_isp_device(ccdc);
653
654         isp_reg_writel(isp, colptn, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_COLPTN);
655 }
656
657 /*
658  * ccdc_config - Set CCDC configuration from userspace
659  * @ccdc: Pointer to ISP CCDC device.
660  * @ccdc_struct: Structure containing CCDC configuration sent from userspace.
661  *
662  * Returns 0 if successful, -EINVAL if the pointer to the configuration
663  * structure is null, or the copy_from_user function fails to copy user space
664  * memory to kernel space memory.
665  */
666 static int ccdc_config(struct isp_ccdc_device *ccdc,
667                        struct omap3isp_ccdc_update_config *ccdc_struct)
668 {
669         struct isp_device *isp = to_isp_device(ccdc);
670         unsigned long flags;
671
672         spin_lock_irqsave(&ccdc->lock, flags);
673         ccdc->shadow_update = 1;
674         spin_unlock_irqrestore(&ccdc->lock, flags);
675
676         if (OMAP3ISP_CCDC_ALAW & ccdc_struct->update) {
677                 ccdc->alaw = !!(OMAP3ISP_CCDC_ALAW & ccdc_struct->flag);
678                 ccdc->update |= OMAP3ISP_CCDC_ALAW;
679         }
680
681         if (OMAP3ISP_CCDC_LPF & ccdc_struct->update) {
682                 ccdc->lpf = !!(OMAP3ISP_CCDC_LPF & ccdc_struct->flag);
683                 ccdc->update |= OMAP3ISP_CCDC_LPF;
684         }
685
686         if (OMAP3ISP_CCDC_BLCLAMP & ccdc_struct->update) {
687                 if (copy_from_user(&ccdc->clamp, ccdc_struct->bclamp,
688                                    sizeof(ccdc->clamp))) {
689                         ccdc->shadow_update = 0;
690                         return -EFAULT;
691                 }
692
693                 ccdc->obclamp = !!(OMAP3ISP_CCDC_BLCLAMP & ccdc_struct->flag);
694                 ccdc->update |= OMAP3ISP_CCDC_BLCLAMP;
695         }
696
697         if (OMAP3ISP_CCDC_BCOMP & ccdc_struct->update) {
698                 if (copy_from_user(&ccdc->blcomp, ccdc_struct->blcomp,
699                                    sizeof(ccdc->blcomp))) {
700                         ccdc->shadow_update = 0;
701                         return -EFAULT;
702                 }
703
704                 ccdc->update |= OMAP3ISP_CCDC_BCOMP;
705         }
706
707         ccdc->shadow_update = 0;
708
709         if (OMAP3ISP_CCDC_FPC & ccdc_struct->update) {
710                 struct omap3isp_ccdc_fpc fpc;
711                 struct ispccdc_fpc fpc_old = { .addr = NULL, };
712                 struct ispccdc_fpc fpc_new;
713                 u32 size;
714
715                 if (ccdc->state != ISP_PIPELINE_STREAM_STOPPED)
716                         return -EBUSY;
717
718                 ccdc->fpc_en = !!(OMAP3ISP_CCDC_FPC & ccdc_struct->flag);
719
720                 if (ccdc->fpc_en) {
721                         if (copy_from_user(&fpc, ccdc_struct->fpc, sizeof(fpc)))
722                                 return -EFAULT;
723
724                         size = fpc.fpnum * 4;
725
726                         /*
727                          * The table address must be 64-bytes aligned, which is
728                          * guaranteed by dma_alloc_coherent().
729                          */
730                         fpc_new.fpnum = fpc.fpnum;
731                         fpc_new.addr = dma_alloc_coherent(isp->dev, size,
732                                                           &fpc_new.dma,
733                                                           GFP_KERNEL);
734                         if (fpc_new.addr == NULL)
735                                 return -ENOMEM;
736
737                         if (copy_from_user(fpc_new.addr,
738                                            (__force void __user *)fpc.fpcaddr,
739                                            size)) {
740                                 dma_free_coherent(isp->dev, size, fpc_new.addr,
741                                                   fpc_new.dma);
742                                 return -EFAULT;
743                         }
744
745                         fpc_old = ccdc->fpc;
746                         ccdc->fpc = fpc_new;
747                 }
748
749                 ccdc_configure_fpc(ccdc);
750
751                 if (fpc_old.addr != NULL)
752                         dma_free_coherent(isp->dev, fpc_old.fpnum * 4,
753                                           fpc_old.addr, fpc_old.dma);
754         }
755
756         return ccdc_lsc_config(ccdc, ccdc_struct);
757 }
758
759 static void ccdc_apply_controls(struct isp_ccdc_device *ccdc)
760 {
761         if (ccdc->update & OMAP3ISP_CCDC_ALAW) {
762                 ccdc_configure_alaw(ccdc);
763                 ccdc->update &= ~OMAP3ISP_CCDC_ALAW;
764         }
765
766         if (ccdc->update & OMAP3ISP_CCDC_LPF) {
767                 ccdc_configure_lpf(ccdc);
768                 ccdc->update &= ~OMAP3ISP_CCDC_LPF;
769         }
770
771         if (ccdc->update & OMAP3ISP_CCDC_BLCLAMP) {
772                 ccdc_configure_clamp(ccdc);
773                 ccdc->update &= ~OMAP3ISP_CCDC_BLCLAMP;
774         }
775
776         if (ccdc->update & OMAP3ISP_CCDC_BCOMP) {
777                 ccdc_configure_black_comp(ccdc);
778                 ccdc->update &= ~OMAP3ISP_CCDC_BCOMP;
779         }
780 }
781
782 /*
783  * omap3isp_ccdc_restore_context - Restore values of the CCDC module registers
784  * @isp: Pointer to ISP device
785  */
786 void omap3isp_ccdc_restore_context(struct isp_device *isp)
787 {
788         struct isp_ccdc_device *ccdc = &isp->isp_ccdc;
789
790         isp_reg_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CFG, ISPCCDC_CFG_VDLC);
791
792         ccdc->update = OMAP3ISP_CCDC_ALAW | OMAP3ISP_CCDC_LPF
793                      | OMAP3ISP_CCDC_BLCLAMP | OMAP3ISP_CCDC_BCOMP;
794         ccdc_apply_controls(ccdc);
795         ccdc_configure_fpc(ccdc);
796 }
797
798 /* -----------------------------------------------------------------------------
799  * Format- and pipeline-related configuration helpers
800  */
801
802 /*
803  * ccdc_config_vp - Configure the Video Port.
804  * @ccdc: Pointer to ISP CCDC device.
805  */
806 static void ccdc_config_vp(struct isp_ccdc_device *ccdc)
807 {
808         struct isp_pipeline *pipe = to_isp_pipeline(&ccdc->subdev.entity);
809         struct isp_device *isp = to_isp_device(ccdc);
810         const struct isp_format_info *info;
811         unsigned long l3_ick = pipe->l3_ick;
812         unsigned int max_div = isp->revision == ISP_REVISION_15_0 ? 64 : 8;
813         unsigned int div = 0;
814         u32 fmtcfg_vp;
815
816         fmtcfg_vp = isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FMTCFG)
817                   & ~(ISPCCDC_FMTCFG_VPIN_MASK | ISPCCDC_FMTCFG_VPIF_FRQ_MASK);
818
819         info = omap3isp_video_format_info(ccdc->formats[CCDC_PAD_SINK].code);
820
821         switch (info->width) {
822         case 8:
823         case 10:
824                 fmtcfg_vp |= ISPCCDC_FMTCFG_VPIN_9_0;
825                 break;
826         case 11:
827                 fmtcfg_vp |= ISPCCDC_FMTCFG_VPIN_10_1;
828                 break;
829         case 12:
830                 fmtcfg_vp |= ISPCCDC_FMTCFG_VPIN_11_2;
831                 break;
832         case 13:
833                 fmtcfg_vp |= ISPCCDC_FMTCFG_VPIN_12_3;
834                 break;
835         }
836
837         if (pipe->input)
838                 div = DIV_ROUND_UP(l3_ick, pipe->max_rate);
839         else if (pipe->external_rate)
840                 div = l3_ick / pipe->external_rate;
841
842         div = clamp(div, 2U, max_div);
843         fmtcfg_vp |= (div - 2) << ISPCCDC_FMTCFG_VPIF_FRQ_SHIFT;
844
845         isp_reg_writel(isp, fmtcfg_vp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FMTCFG);
846 }
847
848 /*
849  * ccdc_enable_vp - Enable Video Port.
850  * @ccdc: Pointer to ISP CCDC device.
851  * @enable: 0 Disables VP, 1 Enables VP
852  *
853  * This is needed for outputting image to Preview, H3A and HIST ISP submodules.
854  */
855 static void ccdc_enable_vp(struct isp_ccdc_device *ccdc, u8 enable)
856 {
857         struct isp_device *isp = to_isp_device(ccdc);
858
859         isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FMTCFG,
860                         ISPCCDC_FMTCFG_VPEN, enable ? ISPCCDC_FMTCFG_VPEN : 0);
861 }
862
863 /*
864  * ccdc_config_outlineoffset - Configure memory saving output line offset
865  * @ccdc: Pointer to ISP CCDC device.
866  * @offset: Address offset to start a new line. Must be twice the
867  *          Output width and aligned on 32 byte boundary
868  * @oddeven: Specifies the odd/even line pattern to be chosen to store the
869  *           output.
870  * @numlines: Set the value 0-3 for +1-4lines, 4-7 for -1-4lines.
871  *
872  * - Configures the output line offset when stored in memory
873  * - Sets the odd/even line pattern to store the output
874  *    (EVENEVEN (1), ODDEVEN (2), EVENODD (3), ODDODD (4))
875  * - Configures the number of even and odd line fields in case of rearranging
876  * the lines.
877  */
878 static void ccdc_config_outlineoffset(struct isp_ccdc_device *ccdc,
879                                         u32 offset, u8 oddeven, u8 numlines)
880 {
881         struct isp_device *isp = to_isp_device(ccdc);
882
883         isp_reg_writel(isp, offset & 0xffff,
884                        OMAP3_ISP_IOMEM_CCDC, ISPCCDC_HSIZE_OFF);
885
886         isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SDOFST,
887                     ISPCCDC_SDOFST_FINV);
888
889         isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SDOFST,
890                     ISPCCDC_SDOFST_FOFST_4L);
891
892         switch (oddeven) {
893         case EVENEVEN:
894                 isp_reg_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SDOFST,
895                             (numlines & 0x7) << ISPCCDC_SDOFST_LOFST0_SHIFT);
896                 break;
897         case ODDEVEN:
898                 isp_reg_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SDOFST,
899                             (numlines & 0x7) << ISPCCDC_SDOFST_LOFST1_SHIFT);
900                 break;
901         case EVENODD:
902                 isp_reg_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SDOFST,
903                             (numlines & 0x7) << ISPCCDC_SDOFST_LOFST2_SHIFT);
904                 break;
905         case ODDODD:
906                 isp_reg_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SDOFST,
907                             (numlines & 0x7) << ISPCCDC_SDOFST_LOFST3_SHIFT);
908                 break;
909         default:
910                 break;
911         }
912 }
913
914 /*
915  * ccdc_set_outaddr - Set memory address to save output image
916  * @ccdc: Pointer to ISP CCDC device.
917  * @addr: ISP MMU Mapped 32-bit memory address aligned on 32 byte boundary.
918  *
919  * Sets the memory address where the output will be saved.
920  */
921 static void ccdc_set_outaddr(struct isp_ccdc_device *ccdc, u32 addr)
922 {
923         struct isp_device *isp = to_isp_device(ccdc);
924
925         isp_reg_writel(isp, addr, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SDR_ADDR);
926 }
927
928 /*
929  * omap3isp_ccdc_max_rate - Calculate maximum input data rate based on the input
930  * @ccdc: Pointer to ISP CCDC device.
931  * @max_rate: Maximum calculated data rate.
932  *
933  * Returns in *max_rate less value between calculated and passed
934  */
935 void omap3isp_ccdc_max_rate(struct isp_ccdc_device *ccdc,
936                             unsigned int *max_rate)
937 {
938         struct isp_pipeline *pipe = to_isp_pipeline(&ccdc->subdev.entity);
939         unsigned int rate;
940
941         if (pipe == NULL)
942                 return;
943
944         /*
945          * TRM says that for parallel sensors the maximum data rate
946          * should be 90% form L3/2 clock, otherwise just L3/2.
947          */
948         if (ccdc->input == CCDC_INPUT_PARALLEL)
949                 rate = pipe->l3_ick / 2 * 9 / 10;
950         else
951                 rate = pipe->l3_ick / 2;
952
953         *max_rate = min(*max_rate, rate);
954 }
955
956 /*
957  * ccdc_config_sync_if - Set CCDC sync interface configuration
958  * @ccdc: Pointer to ISP CCDC device.
959  * @pdata: Parallel interface platform data (may be NULL)
960  * @data_size: Data size
961  */
962 static void ccdc_config_sync_if(struct isp_ccdc_device *ccdc,
963                                 struct isp_parallel_platform_data *pdata,
964                                 unsigned int data_size)
965 {
966         struct isp_device *isp = to_isp_device(ccdc);
967         const struct v4l2_mbus_framefmt *format;
968         u32 syn_mode = ISPCCDC_SYN_MODE_VDHDEN;
969
970         format = &ccdc->formats[CCDC_PAD_SINK];
971
972         if (format->code == V4L2_MBUS_FMT_YUYV8_2X8 ||
973             format->code == V4L2_MBUS_FMT_UYVY8_2X8) {
974                 /* The bridge is enabled for YUV8 formats. Configure the input
975                  * mode accordingly.
976                  */
977                 syn_mode |= ISPCCDC_SYN_MODE_INPMOD_YCBCR16;
978         }
979
980         switch (data_size) {
981         case 8:
982                 syn_mode |= ISPCCDC_SYN_MODE_DATSIZ_8;
983                 break;
984         case 10:
985                 syn_mode |= ISPCCDC_SYN_MODE_DATSIZ_10;
986                 break;
987         case 11:
988                 syn_mode |= ISPCCDC_SYN_MODE_DATSIZ_11;
989                 break;
990         case 12:
991                 syn_mode |= ISPCCDC_SYN_MODE_DATSIZ_12;
992                 break;
993         }
994
995         if (pdata && pdata->data_pol)
996                 syn_mode |= ISPCCDC_SYN_MODE_DATAPOL;
997
998         if (pdata && pdata->hs_pol)
999                 syn_mode |= ISPCCDC_SYN_MODE_HDPOL;
1000
1001         if (pdata && pdata->vs_pol)
1002                 syn_mode |= ISPCCDC_SYN_MODE_VDPOL;
1003
1004         isp_reg_writel(isp, syn_mode, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SYN_MODE);
1005
1006         /* The CCDC_CFG.Y8POS bit is used in YCbCr8 input mode only. The
1007          * hardware seems to ignore it in all other input modes.
1008          */
1009         if (format->code == V4L2_MBUS_FMT_UYVY8_2X8)
1010                 isp_reg_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CFG,
1011                             ISPCCDC_CFG_Y8POS);
1012         else
1013                 isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CFG,
1014                             ISPCCDC_CFG_Y8POS);
1015
1016         isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_REC656IF,
1017                     ISPCCDC_REC656IF_R656ON);
1018 }
1019
1020 /* CCDC formats descriptions */
1021 static const u32 ccdc_sgrbg_pattern =
1022         ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP0PLC0_SHIFT |
1023         ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP0PLC1_SHIFT |
1024         ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP0PLC2_SHIFT |
1025         ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP0PLC3_SHIFT |
1026         ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP1PLC0_SHIFT |
1027         ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP1PLC1_SHIFT |
1028         ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP1PLC2_SHIFT |
1029         ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP1PLC3_SHIFT |
1030         ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP2PLC0_SHIFT |
1031         ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP2PLC1_SHIFT |
1032         ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP2PLC2_SHIFT |
1033         ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP2PLC3_SHIFT |
1034         ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP3PLC0_SHIFT |
1035         ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP3PLC1_SHIFT |
1036         ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP3PLC2_SHIFT |
1037         ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP3PLC3_SHIFT;
1038
1039 static const u32 ccdc_srggb_pattern =
1040         ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP0PLC0_SHIFT |
1041         ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP0PLC1_SHIFT |
1042         ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP0PLC2_SHIFT |
1043         ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP0PLC3_SHIFT |
1044         ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP1PLC0_SHIFT |
1045         ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP1PLC1_SHIFT |
1046         ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP1PLC2_SHIFT |
1047         ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP1PLC3_SHIFT |
1048         ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP2PLC0_SHIFT |
1049         ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP2PLC1_SHIFT |
1050         ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP2PLC2_SHIFT |
1051         ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP2PLC3_SHIFT |
1052         ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP3PLC0_SHIFT |
1053         ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP3PLC1_SHIFT |
1054         ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP3PLC2_SHIFT |
1055         ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP3PLC3_SHIFT;
1056
1057 static const u32 ccdc_sbggr_pattern =
1058         ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP0PLC0_SHIFT |
1059         ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP0PLC1_SHIFT |
1060         ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP0PLC2_SHIFT |
1061         ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP0PLC3_SHIFT |
1062         ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP1PLC0_SHIFT |
1063         ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP1PLC1_SHIFT |
1064         ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP1PLC2_SHIFT |
1065         ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP1PLC3_SHIFT |
1066         ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP2PLC0_SHIFT |
1067         ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP2PLC1_SHIFT |
1068         ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP2PLC2_SHIFT |
1069         ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP2PLC3_SHIFT |
1070         ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP3PLC0_SHIFT |
1071         ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP3PLC1_SHIFT |
1072         ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP3PLC2_SHIFT |
1073         ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP3PLC3_SHIFT;
1074
1075 static const u32 ccdc_sgbrg_pattern =
1076         ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP0PLC0_SHIFT |
1077         ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP0PLC1_SHIFT |
1078         ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP0PLC2_SHIFT |
1079         ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP0PLC3_SHIFT |
1080         ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP1PLC0_SHIFT |
1081         ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP1PLC1_SHIFT |
1082         ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP1PLC2_SHIFT |
1083         ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP1PLC3_SHIFT |
1084         ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP2PLC0_SHIFT |
1085         ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP2PLC1_SHIFT |
1086         ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP2PLC2_SHIFT |
1087         ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP2PLC3_SHIFT |
1088         ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP3PLC0_SHIFT |
1089         ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP3PLC1_SHIFT |
1090         ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP3PLC2_SHIFT |
1091         ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP3PLC3_SHIFT;
1092
1093 static void ccdc_configure(struct isp_ccdc_device *ccdc)
1094 {
1095         struct isp_device *isp = to_isp_device(ccdc);
1096         struct isp_parallel_platform_data *pdata = NULL;
1097         struct v4l2_subdev *sensor;
1098         struct v4l2_mbus_framefmt *format;
1099         const struct v4l2_rect *crop;
1100         const struct isp_format_info *fmt_info;
1101         struct v4l2_subdev_format fmt_src;
1102         unsigned int depth_out;
1103         unsigned int depth_in = 0;
1104         struct media_pad *pad;
1105         unsigned long flags;
1106         unsigned int bridge;
1107         unsigned int shift;
1108         u32 syn_mode;
1109         u32 ccdc_pattern;
1110
1111         pad = media_entity_remote_pad(&ccdc->pads[CCDC_PAD_SINK]);
1112         sensor = media_entity_to_v4l2_subdev(pad->entity);
1113         if (ccdc->input == CCDC_INPUT_PARALLEL)
1114                 pdata = &((struct isp_v4l2_subdevs_group *)sensor->host_priv)
1115                         ->bus.parallel;
1116
1117         /* Compute the lane shifter shift value and enable the bridge when the
1118          * input format is YUV.
1119          */
1120         fmt_src.pad = pad->index;
1121         fmt_src.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1122         if (!v4l2_subdev_call(sensor, pad, get_fmt, NULL, &fmt_src)) {
1123                 fmt_info = omap3isp_video_format_info(fmt_src.format.code);
1124                 depth_in = fmt_info->width;
1125         }
1126
1127         fmt_info = omap3isp_video_format_info
1128                 (isp->isp_ccdc.formats[CCDC_PAD_SINK].code);
1129         depth_out = fmt_info->width;
1130         shift = depth_in - depth_out;
1131
1132         if (fmt_info->code == V4L2_MBUS_FMT_YUYV8_2X8)
1133                 bridge = ISPCTRL_PAR_BRIDGE_LENDIAN;
1134         else if (fmt_info->code == V4L2_MBUS_FMT_UYVY8_2X8)
1135                 bridge = ISPCTRL_PAR_BRIDGE_BENDIAN;
1136         else
1137                 bridge = ISPCTRL_PAR_BRIDGE_DISABLE;
1138
1139         omap3isp_configure_bridge(isp, ccdc->input, pdata, shift, bridge);
1140
1141         ccdc_config_sync_if(ccdc, pdata, depth_out);
1142
1143         syn_mode = isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SYN_MODE);
1144
1145         /* Use the raw, unprocessed data when writing to memory. The H3A and
1146          * histogram modules are still fed with lens shading corrected data.
1147          */
1148         syn_mode &= ~ISPCCDC_SYN_MODE_VP2SDR;
1149
1150         if (ccdc->output & CCDC_OUTPUT_MEMORY)
1151                 syn_mode |= ISPCCDC_SYN_MODE_WEN;
1152         else
1153                 syn_mode &= ~ISPCCDC_SYN_MODE_WEN;
1154
1155         if (ccdc->output & CCDC_OUTPUT_RESIZER)
1156                 syn_mode |= ISPCCDC_SYN_MODE_SDR2RSZ;
1157         else
1158                 syn_mode &= ~ISPCCDC_SYN_MODE_SDR2RSZ;
1159
1160         /* CCDC_PAD_SINK */
1161         format = &ccdc->formats[CCDC_PAD_SINK];
1162
1163         /* Mosaic filter */
1164         switch (format->code) {
1165         case V4L2_MBUS_FMT_SRGGB10_1X10:
1166         case V4L2_MBUS_FMT_SRGGB12_1X12:
1167                 ccdc_pattern = ccdc_srggb_pattern;
1168                 break;
1169         case V4L2_MBUS_FMT_SBGGR10_1X10:
1170         case V4L2_MBUS_FMT_SBGGR12_1X12:
1171                 ccdc_pattern = ccdc_sbggr_pattern;
1172                 break;
1173         case V4L2_MBUS_FMT_SGBRG10_1X10:
1174         case V4L2_MBUS_FMT_SGBRG12_1X12:
1175                 ccdc_pattern = ccdc_sgbrg_pattern;
1176                 break;
1177         default:
1178                 /* Use GRBG */
1179                 ccdc_pattern = ccdc_sgrbg_pattern;
1180                 break;
1181         }
1182         ccdc_config_imgattr(ccdc, ccdc_pattern);
1183
1184         /* Generate VD0 on the last line of the image and VD1 on the
1185          * 2/3 height line.
1186          */
1187         isp_reg_writel(isp, ((format->height - 2) << ISPCCDC_VDINT_0_SHIFT) |
1188                        ((format->height * 2 / 3) << ISPCCDC_VDINT_1_SHIFT),
1189                        OMAP3_ISP_IOMEM_CCDC, ISPCCDC_VDINT);
1190
1191         /* CCDC_PAD_SOURCE_OF */
1192         format = &ccdc->formats[CCDC_PAD_SOURCE_OF];
1193         crop = &ccdc->crop;
1194
1195         isp_reg_writel(isp, (crop->left << ISPCCDC_HORZ_INFO_SPH_SHIFT) |
1196                        ((crop->width - 1) << ISPCCDC_HORZ_INFO_NPH_SHIFT),
1197                        OMAP3_ISP_IOMEM_CCDC, ISPCCDC_HORZ_INFO);
1198         isp_reg_writel(isp, crop->top << ISPCCDC_VERT_START_SLV0_SHIFT,
1199                        OMAP3_ISP_IOMEM_CCDC, ISPCCDC_VERT_START);
1200         isp_reg_writel(isp, (crop->height - 1)
1201                         << ISPCCDC_VERT_LINES_NLV_SHIFT,
1202                        OMAP3_ISP_IOMEM_CCDC, ISPCCDC_VERT_LINES);
1203
1204         ccdc_config_outlineoffset(ccdc, ccdc->video_out.bpl_value, 0, 0);
1205
1206         /* The CCDC outputs data in UYVY order by default. Swap bytes to get
1207          * YUYV.
1208          */
1209         if (format->code == V4L2_MBUS_FMT_YUYV8_1X16)
1210                 isp_reg_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CFG,
1211                             ISPCCDC_CFG_BSWD);
1212         else
1213                 isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CFG,
1214                             ISPCCDC_CFG_BSWD);
1215
1216         /* Use PACK8 mode for 1byte per pixel formats. */
1217         if (omap3isp_video_format_info(format->code)->width <= 8)
1218                 syn_mode |= ISPCCDC_SYN_MODE_PACK8;
1219         else
1220                 syn_mode &= ~ISPCCDC_SYN_MODE_PACK8;
1221
1222         isp_reg_writel(isp, syn_mode, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SYN_MODE);
1223
1224         /* CCDC_PAD_SOURCE_VP */
1225         format = &ccdc->formats[CCDC_PAD_SOURCE_VP];
1226
1227         isp_reg_writel(isp, (0 << ISPCCDC_FMT_HORZ_FMTSPH_SHIFT) |
1228                        (format->width << ISPCCDC_FMT_HORZ_FMTLNH_SHIFT),
1229                        OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FMT_HORZ);
1230         isp_reg_writel(isp, (0 << ISPCCDC_FMT_VERT_FMTSLV_SHIFT) |
1231                        ((format->height + 1) << ISPCCDC_FMT_VERT_FMTLNV_SHIFT),
1232                        OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FMT_VERT);
1233
1234         isp_reg_writel(isp, (format->width << ISPCCDC_VP_OUT_HORZ_NUM_SHIFT) |
1235                        (format->height << ISPCCDC_VP_OUT_VERT_NUM_SHIFT),
1236                        OMAP3_ISP_IOMEM_CCDC, ISPCCDC_VP_OUT);
1237
1238         /* Lens shading correction. */
1239         spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
1240         if (ccdc->lsc.request == NULL)
1241                 goto unlock;
1242
1243         WARN_ON(ccdc->lsc.active);
1244
1245         /* Get last good LSC configuration. If it is not supported for
1246          * the current active resolution discard it.
1247          */
1248         if (ccdc->lsc.active == NULL &&
1249             __ccdc_lsc_configure(ccdc, ccdc->lsc.request) == 0) {
1250                 ccdc->lsc.active = ccdc->lsc.request;
1251         } else {
1252                 list_add_tail(&ccdc->lsc.request->list, &ccdc->lsc.free_queue);
1253                 schedule_work(&ccdc->lsc.table_work);
1254         }
1255
1256         ccdc->lsc.request = NULL;
1257
1258 unlock:
1259         spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
1260
1261         ccdc_apply_controls(ccdc);
1262 }
1263
1264 static void __ccdc_enable(struct isp_ccdc_device *ccdc, int enable)
1265 {
1266         struct isp_device *isp = to_isp_device(ccdc);
1267
1268         isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_PCR,
1269                         ISPCCDC_PCR_EN, enable ? ISPCCDC_PCR_EN : 0);
1270 }
1271
1272 static int ccdc_disable(struct isp_ccdc_device *ccdc)
1273 {
1274         unsigned long flags;
1275         int ret = 0;
1276
1277         spin_lock_irqsave(&ccdc->lock, flags);
1278         if (ccdc->state == ISP_PIPELINE_STREAM_CONTINUOUS)
1279                 ccdc->stopping = CCDC_STOP_REQUEST;
1280         spin_unlock_irqrestore(&ccdc->lock, flags);
1281
1282         ret = wait_event_timeout(ccdc->wait,
1283                                  ccdc->stopping == CCDC_STOP_FINISHED,
1284                                  msecs_to_jiffies(2000));
1285         if (ret == 0) {
1286                 ret = -ETIMEDOUT;
1287                 dev_warn(to_device(ccdc), "CCDC stop timeout!\n");
1288         }
1289
1290         omap3isp_sbl_disable(to_isp_device(ccdc), OMAP3_ISP_SBL_CCDC_LSC_READ);
1291
1292         mutex_lock(&ccdc->ioctl_lock);
1293         ccdc_lsc_free_request(ccdc, ccdc->lsc.request);
1294         ccdc->lsc.request = ccdc->lsc.active;
1295         ccdc->lsc.active = NULL;
1296         cancel_work_sync(&ccdc->lsc.table_work);
1297         ccdc_lsc_free_queue(ccdc, &ccdc->lsc.free_queue);
1298         mutex_unlock(&ccdc->ioctl_lock);
1299
1300         ccdc->stopping = CCDC_STOP_NOT_REQUESTED;
1301
1302         return ret > 0 ? 0 : ret;
1303 }
1304
1305 static void ccdc_enable(struct isp_ccdc_device *ccdc)
1306 {
1307         if (ccdc_lsc_is_configured(ccdc))
1308                 __ccdc_lsc_enable(ccdc, 1);
1309         __ccdc_enable(ccdc, 1);
1310 }
1311
1312 /* -----------------------------------------------------------------------------
1313  * Interrupt handling
1314  */
1315
1316 /*
1317  * ccdc_sbl_busy - Poll idle state of CCDC and related SBL memory write bits
1318  * @ccdc: Pointer to ISP CCDC device.
1319  *
1320  * Returns zero if the CCDC is idle and the image has been written to
1321  * memory, too.
1322  */
1323 static int ccdc_sbl_busy(struct isp_ccdc_device *ccdc)
1324 {
1325         struct isp_device *isp = to_isp_device(ccdc);
1326
1327         return omap3isp_ccdc_busy(ccdc)
1328                 | (isp_reg_readl(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_CCDC_WR_0) &
1329                    ISPSBL_CCDC_WR_0_DATA_READY)
1330                 | (isp_reg_readl(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_CCDC_WR_1) &
1331                    ISPSBL_CCDC_WR_0_DATA_READY)
1332                 | (isp_reg_readl(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_CCDC_WR_2) &
1333                    ISPSBL_CCDC_WR_0_DATA_READY)
1334                 | (isp_reg_readl(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_CCDC_WR_3) &
1335                    ISPSBL_CCDC_WR_0_DATA_READY);
1336 }
1337
1338 /*
1339  * ccdc_sbl_wait_idle - Wait until the CCDC and related SBL are idle
1340  * @ccdc: Pointer to ISP CCDC device.
1341  * @max_wait: Max retry count in us for wait for idle/busy transition.
1342  */
1343 static int ccdc_sbl_wait_idle(struct isp_ccdc_device *ccdc,
1344                               unsigned int max_wait)
1345 {
1346         unsigned int wait = 0;
1347
1348         if (max_wait == 0)
1349                 max_wait = 10000; /* 10 ms */
1350
1351         for (wait = 0; wait <= max_wait; wait++) {
1352                 if (!ccdc_sbl_busy(ccdc))
1353                         return 0;
1354
1355                 rmb();
1356                 udelay(1);
1357         }
1358
1359         return -EBUSY;
1360 }
1361
1362 /* __ccdc_handle_stopping - Handle CCDC and/or LSC stopping sequence
1363  * @ccdc: Pointer to ISP CCDC device.
1364  * @event: Pointing which event trigger handler
1365  *
1366  * Return 1 when the event and stopping request combination is satisfied,
1367  * zero otherwise.
1368  */
1369 static int __ccdc_handle_stopping(struct isp_ccdc_device *ccdc, u32 event)
1370 {
1371         int rval = 0;
1372
1373         switch ((ccdc->stopping & 3) | event) {
1374         case CCDC_STOP_REQUEST | CCDC_EVENT_VD1:
1375                 if (ccdc->lsc.state != LSC_STATE_STOPPED)
1376                         __ccdc_lsc_enable(ccdc, 0);
1377                 __ccdc_enable(ccdc, 0);
1378                 ccdc->stopping = CCDC_STOP_EXECUTED;
1379                 return 1;
1380
1381         case CCDC_STOP_EXECUTED | CCDC_EVENT_VD0:
1382                 ccdc->stopping |= CCDC_STOP_CCDC_FINISHED;
1383                 if (ccdc->lsc.state == LSC_STATE_STOPPED)
1384                         ccdc->stopping |= CCDC_STOP_LSC_FINISHED;
1385                 rval = 1;
1386                 break;
1387
1388         case CCDC_STOP_EXECUTED | CCDC_EVENT_LSC_DONE:
1389                 ccdc->stopping |= CCDC_STOP_LSC_FINISHED;
1390                 rval = 1;
1391                 break;
1392
1393         case CCDC_STOP_EXECUTED | CCDC_EVENT_VD1:
1394                 return 1;
1395         }
1396
1397         if (ccdc->stopping == CCDC_STOP_FINISHED) {
1398                 wake_up(&ccdc->wait);
1399                 rval = 1;
1400         }
1401
1402         return rval;
1403 }
1404
1405 static void ccdc_hs_vs_isr(struct isp_ccdc_device *ccdc)
1406 {
1407         struct isp_pipeline *pipe = to_isp_pipeline(&ccdc->subdev.entity);
1408         struct video_device *vdev = ccdc->subdev.devnode;
1409         struct v4l2_event event;
1410
1411         /* Frame number propagation */
1412         atomic_inc(&pipe->frame_number);
1413
1414         memset(&event, 0, sizeof(event));
1415         event.type = V4L2_EVENT_FRAME_SYNC;
1416         event.u.frame_sync.frame_sequence = atomic_read(&pipe->frame_number);
1417
1418         v4l2_event_queue(vdev, &event);
1419 }
1420
1421 /*
1422  * ccdc_lsc_isr - Handle LSC events
1423  * @ccdc: Pointer to ISP CCDC device.
1424  * @events: LSC events
1425  */
1426 static void ccdc_lsc_isr(struct isp_ccdc_device *ccdc, u32 events)
1427 {
1428         unsigned long flags;
1429
1430         if (events & IRQ0STATUS_CCDC_LSC_PREF_ERR_IRQ) {
1431                 struct isp_pipeline *pipe =
1432                         to_isp_pipeline(&ccdc->subdev.entity);
1433
1434                 ccdc_lsc_error_handler(ccdc);
1435                 pipe->error = true;
1436                 dev_dbg(to_device(ccdc), "lsc prefetch error\n");
1437         }
1438
1439         if (!(events & IRQ0STATUS_CCDC_LSC_DONE_IRQ))
1440                 return;
1441
1442         /* LSC_DONE interrupt occur, there are two cases
1443          * 1. stopping for reconfiguration
1444          * 2. stopping because of STREAM OFF command
1445          */
1446         spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
1447
1448         if (ccdc->lsc.state == LSC_STATE_STOPPING)
1449                 ccdc->lsc.state = LSC_STATE_STOPPED;
1450
1451         if (__ccdc_handle_stopping(ccdc, CCDC_EVENT_LSC_DONE))
1452                 goto done;
1453
1454         if (ccdc->lsc.state != LSC_STATE_RECONFIG)
1455                 goto done;
1456
1457         /* LSC is in STOPPING state, change to the new state */
1458         ccdc->lsc.state = LSC_STATE_STOPPED;
1459
1460         /* This is an exception. Start of frame and LSC_DONE interrupt
1461          * have been received on the same time. Skip this event and wait
1462          * for better times.
1463          */
1464         if (events & IRQ0STATUS_HS_VS_IRQ)
1465                 goto done;
1466
1467         /* The LSC engine is stopped at this point. Enable it if there's a
1468          * pending request.
1469          */
1470         if (ccdc->lsc.request == NULL)
1471                 goto done;
1472
1473         ccdc_lsc_enable(ccdc);
1474
1475 done:
1476         spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
1477 }
1478
1479 static int ccdc_isr_buffer(struct isp_ccdc_device *ccdc)
1480 {
1481         struct isp_pipeline *pipe = to_isp_pipeline(&ccdc->subdev.entity);
1482         struct isp_device *isp = to_isp_device(ccdc);
1483         struct isp_buffer *buffer;
1484         int restart = 0;
1485
1486         /* The CCDC generates VD0 interrupts even when disabled (the datasheet
1487          * doesn't explicitly state if that's supposed to happen or not, so it
1488          * can be considered as a hardware bug or as a feature, but we have to
1489          * deal with it anyway). Disabling the CCDC when no buffer is available
1490          * would thus not be enough, we need to handle the situation explicitly.
1491          */
1492         if (list_empty(&ccdc->video_out.dmaqueue))
1493                 goto done;
1494
1495         /* We're in continuous mode, and memory writes were disabled due to a
1496          * buffer underrun. Reenable them now that we have a buffer. The buffer
1497          * address has been set in ccdc_video_queue.
1498          */
1499         if (ccdc->state == ISP_PIPELINE_STREAM_CONTINUOUS && ccdc->underrun) {
1500                 restart = 1;
1501                 ccdc->underrun = 0;
1502                 goto done;
1503         }
1504
1505         if (ccdc_sbl_wait_idle(ccdc, 1000)) {
1506                 dev_info(isp->dev, "CCDC won't become idle!\n");
1507                 isp->crashed |= 1U << ccdc->subdev.entity.id;
1508                 omap3isp_pipeline_cancel_stream(pipe);
1509                 goto done;
1510         }
1511
1512         buffer = omap3isp_video_buffer_next(&ccdc->video_out);
1513         if (buffer != NULL) {
1514                 ccdc_set_outaddr(ccdc, buffer->dma);
1515                 restart = 1;
1516         }
1517
1518         pipe->state |= ISP_PIPELINE_IDLE_OUTPUT;
1519
1520         if (ccdc->state == ISP_PIPELINE_STREAM_SINGLESHOT &&
1521             isp_pipeline_ready(pipe))
1522                 omap3isp_pipeline_set_stream(pipe,
1523                                         ISP_PIPELINE_STREAM_SINGLESHOT);
1524
1525 done:
1526         return restart;
1527 }
1528
1529 /*
1530  * ccdc_vd0_isr - Handle VD0 event
1531  * @ccdc: Pointer to ISP CCDC device.
1532  *
1533  * Executes LSC deferred enablement before next frame starts.
1534  */
1535 static void ccdc_vd0_isr(struct isp_ccdc_device *ccdc)
1536 {
1537         unsigned long flags;
1538         int restart = 0;
1539
1540         if (ccdc->output & CCDC_OUTPUT_MEMORY)
1541                 restart = ccdc_isr_buffer(ccdc);
1542
1543         spin_lock_irqsave(&ccdc->lock, flags);
1544         if (__ccdc_handle_stopping(ccdc, CCDC_EVENT_VD0)) {
1545                 spin_unlock_irqrestore(&ccdc->lock, flags);
1546                 return;
1547         }
1548
1549         if (!ccdc->shadow_update)
1550                 ccdc_apply_controls(ccdc);
1551         spin_unlock_irqrestore(&ccdc->lock, flags);
1552
1553         if (restart)
1554                 ccdc_enable(ccdc);
1555 }
1556
1557 /*
1558  * ccdc_vd1_isr - Handle VD1 event
1559  * @ccdc: Pointer to ISP CCDC device.
1560  */
1561 static void ccdc_vd1_isr(struct isp_ccdc_device *ccdc)
1562 {
1563         unsigned long flags;
1564
1565         spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
1566
1567         /*
1568          * Depending on the CCDC pipeline state, CCDC stopping should be
1569          * handled differently. In SINGLESHOT we emulate an internal CCDC
1570          * stopping because the CCDC hw works only in continuous mode.
1571          * When CONTINUOUS pipeline state is used and the CCDC writes it's
1572          * data to memory the CCDC and LSC are stopped immediately but
1573          * without change the CCDC stopping state machine. The CCDC
1574          * stopping state machine should be used only when user request
1575          * for stopping is received (SINGLESHOT is an exeption).
1576          */
1577         switch (ccdc->state) {
1578         case ISP_PIPELINE_STREAM_SINGLESHOT:
1579                 ccdc->stopping = CCDC_STOP_REQUEST;
1580                 break;
1581
1582         case ISP_PIPELINE_STREAM_CONTINUOUS:
1583                 if (ccdc->output & CCDC_OUTPUT_MEMORY) {
1584                         if (ccdc->lsc.state != LSC_STATE_STOPPED)
1585                                 __ccdc_lsc_enable(ccdc, 0);
1586                         __ccdc_enable(ccdc, 0);
1587                 }
1588                 break;
1589
1590         case ISP_PIPELINE_STREAM_STOPPED:
1591                 break;
1592         }
1593
1594         if (__ccdc_handle_stopping(ccdc, CCDC_EVENT_VD1))
1595                 goto done;
1596
1597         if (ccdc->lsc.request == NULL)
1598                 goto done;
1599
1600         /*
1601          * LSC need to be reconfigured. Stop it here and on next LSC_DONE IRQ
1602          * do the appropriate changes in registers
1603          */
1604         if (ccdc->lsc.state == LSC_STATE_RUNNING) {
1605                 __ccdc_lsc_enable(ccdc, 0);
1606                 ccdc->lsc.state = LSC_STATE_RECONFIG;
1607                 goto done;
1608         }
1609
1610         /* LSC has been in STOPPED state, enable it */
1611         if (ccdc->lsc.state == LSC_STATE_STOPPED)
1612                 ccdc_lsc_enable(ccdc);
1613
1614 done:
1615         spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
1616 }
1617
1618 /*
1619  * omap3isp_ccdc_isr - Configure CCDC during interframe time.
1620  * @ccdc: Pointer to ISP CCDC device.
1621  * @events: CCDC events
1622  */
1623 int omap3isp_ccdc_isr(struct isp_ccdc_device *ccdc, u32 events)
1624 {
1625         if (ccdc->state == ISP_PIPELINE_STREAM_STOPPED)
1626                 return 0;
1627
1628         if (events & IRQ0STATUS_CCDC_VD1_IRQ)
1629                 ccdc_vd1_isr(ccdc);
1630
1631         ccdc_lsc_isr(ccdc, events);
1632
1633         if (events & IRQ0STATUS_CCDC_VD0_IRQ)
1634                 ccdc_vd0_isr(ccdc);
1635
1636         if (events & IRQ0STATUS_HS_VS_IRQ)
1637                 ccdc_hs_vs_isr(ccdc);
1638
1639         return 0;
1640 }
1641
1642 /* -----------------------------------------------------------------------------
1643  * ISP video operations
1644  */
1645
1646 static int ccdc_video_queue(struct isp_video *video, struct isp_buffer *buffer)
1647 {
1648         struct isp_ccdc_device *ccdc = &video->isp->isp_ccdc;
1649
1650         if (!(ccdc->output & CCDC_OUTPUT_MEMORY))
1651                 return -ENODEV;
1652
1653         ccdc_set_outaddr(ccdc, buffer->dma);
1654
1655         /* We now have a buffer queued on the output, restart the pipeline
1656          * on the next CCDC interrupt if running in continuous mode (or when
1657          * starting the stream).
1658          */
1659         ccdc->underrun = 1;
1660
1661         return 0;
1662 }
1663
1664 static const struct isp_video_operations ccdc_video_ops = {
1665         .queue = ccdc_video_queue,
1666 };
1667
1668 /* -----------------------------------------------------------------------------
1669  * V4L2 subdev operations
1670  */
1671
1672 /*
1673  * ccdc_ioctl - CCDC module private ioctl's
1674  * @sd: ISP CCDC V4L2 subdevice
1675  * @cmd: ioctl command
1676  * @arg: ioctl argument
1677  *
1678  * Return 0 on success or a negative error code otherwise.
1679  */
1680 static long ccdc_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1681 {
1682         struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
1683         int ret;
1684
1685         switch (cmd) {
1686         case VIDIOC_OMAP3ISP_CCDC_CFG:
1687                 mutex_lock(&ccdc->ioctl_lock);
1688                 ret = ccdc_config(ccdc, arg);
1689                 mutex_unlock(&ccdc->ioctl_lock);
1690                 break;
1691
1692         default:
1693                 return -ENOIOCTLCMD;
1694         }
1695
1696         return ret;
1697 }
1698
1699 static int ccdc_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1700                                 struct v4l2_event_subscription *sub)
1701 {
1702         if (sub->type != V4L2_EVENT_FRAME_SYNC)
1703                 return -EINVAL;
1704
1705         /* line number is zero at frame start */
1706         if (sub->id != 0)
1707                 return -EINVAL;
1708
1709         return v4l2_event_subscribe(fh, sub, OMAP3ISP_CCDC_NEVENTS, NULL);
1710 }
1711
1712 static int ccdc_unsubscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1713                                   struct v4l2_event_subscription *sub)
1714 {
1715         return v4l2_event_unsubscribe(fh, sub);
1716 }
1717
1718 /*
1719  * ccdc_set_stream - Enable/Disable streaming on the CCDC module
1720  * @sd: ISP CCDC V4L2 subdevice
1721  * @enable: Enable/disable stream
1722  *
1723  * When writing to memory, the CCDC hardware can't be enabled without a memory
1724  * buffer to write to. As the s_stream operation is called in response to a
1725  * STREAMON call without any buffer queued yet, just update the enabled field
1726  * and return immediately. The CCDC will be enabled in ccdc_isr_buffer().
1727  *
1728  * When not writing to memory enable the CCDC immediately.
1729  */
1730 static int ccdc_set_stream(struct v4l2_subdev *sd, int enable)
1731 {
1732         struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
1733         struct isp_device *isp = to_isp_device(ccdc);
1734         int ret = 0;
1735
1736         if (ccdc->state == ISP_PIPELINE_STREAM_STOPPED) {
1737                 if (enable == ISP_PIPELINE_STREAM_STOPPED)
1738                         return 0;
1739
1740                 omap3isp_subclk_enable(isp, OMAP3_ISP_SUBCLK_CCDC);
1741                 isp_reg_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CFG,
1742                             ISPCCDC_CFG_VDLC);
1743
1744                 ccdc_configure(ccdc);
1745
1746                 /* TODO: Don't configure the video port if all of its output
1747                  * links are inactive.
1748                  */
1749                 ccdc_config_vp(ccdc);
1750                 ccdc_enable_vp(ccdc, 1);
1751                 ccdc_print_status(ccdc);
1752         }
1753
1754         switch (enable) {
1755         case ISP_PIPELINE_STREAM_CONTINUOUS:
1756                 if (ccdc->output & CCDC_OUTPUT_MEMORY)
1757                         omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_CCDC_WRITE);
1758
1759                 if (ccdc->underrun || !(ccdc->output & CCDC_OUTPUT_MEMORY))
1760                         ccdc_enable(ccdc);
1761
1762                 ccdc->underrun = 0;
1763                 break;
1764
1765         case ISP_PIPELINE_STREAM_SINGLESHOT:
1766                 if (ccdc->output & CCDC_OUTPUT_MEMORY &&
1767                     ccdc->state != ISP_PIPELINE_STREAM_SINGLESHOT)
1768                         omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_CCDC_WRITE);
1769
1770                 ccdc_enable(ccdc);
1771                 break;
1772
1773         case ISP_PIPELINE_STREAM_STOPPED:
1774                 ret = ccdc_disable(ccdc);
1775                 if (ccdc->output & CCDC_OUTPUT_MEMORY)
1776                         omap3isp_sbl_disable(isp, OMAP3_ISP_SBL_CCDC_WRITE);
1777                 omap3isp_subclk_disable(isp, OMAP3_ISP_SUBCLK_CCDC);
1778                 ccdc->underrun = 0;
1779                 break;
1780         }
1781
1782         ccdc->state = enable;
1783         return ret;
1784 }
1785
1786 static struct v4l2_mbus_framefmt *
1787 __ccdc_get_format(struct isp_ccdc_device *ccdc, struct v4l2_subdev_fh *fh,
1788                   unsigned int pad, enum v4l2_subdev_format_whence which)
1789 {
1790         if (which == V4L2_SUBDEV_FORMAT_TRY)
1791                 return v4l2_subdev_get_try_format(fh, pad);
1792         else
1793                 return &ccdc->formats[pad];
1794 }
1795
1796 static struct v4l2_rect *
1797 __ccdc_get_crop(struct isp_ccdc_device *ccdc, struct v4l2_subdev_fh *fh,
1798                 enum v4l2_subdev_format_whence which)
1799 {
1800         if (which == V4L2_SUBDEV_FORMAT_TRY)
1801                 return v4l2_subdev_get_try_crop(fh, CCDC_PAD_SOURCE_OF);
1802         else
1803                 return &ccdc->crop;
1804 }
1805
1806 /*
1807  * ccdc_try_format - Try video format on a pad
1808  * @ccdc: ISP CCDC device
1809  * @fh : V4L2 subdev file handle
1810  * @pad: Pad number
1811  * @fmt: Format
1812  */
1813 static void
1814 ccdc_try_format(struct isp_ccdc_device *ccdc, struct v4l2_subdev_fh *fh,
1815                 unsigned int pad, struct v4l2_mbus_framefmt *fmt,
1816                 enum v4l2_subdev_format_whence which)
1817 {
1818         const struct isp_format_info *info;
1819         enum v4l2_mbus_pixelcode pixelcode;
1820         unsigned int width = fmt->width;
1821         unsigned int height = fmt->height;
1822         struct v4l2_rect *crop;
1823         unsigned int i;
1824
1825         switch (pad) {
1826         case CCDC_PAD_SINK:
1827                 for (i = 0; i < ARRAY_SIZE(ccdc_fmts); i++) {
1828                         if (fmt->code == ccdc_fmts[i])
1829                                 break;
1830                 }
1831
1832                 /* If not found, use SGRBG10 as default */
1833                 if (i >= ARRAY_SIZE(ccdc_fmts))
1834                         fmt->code = V4L2_MBUS_FMT_SGRBG10_1X10;
1835
1836                 /* Clamp the input size. */
1837                 fmt->width = clamp_t(u32, width, 32, 4096);
1838                 fmt->height = clamp_t(u32, height, 32, 4096);
1839                 break;
1840
1841         case CCDC_PAD_SOURCE_OF:
1842                 pixelcode = fmt->code;
1843                 *fmt = *__ccdc_get_format(ccdc, fh, CCDC_PAD_SINK, which);
1844
1845                 /* YUV formats are converted from 2X8 to 1X16 by the bridge and
1846                  * can be byte-swapped.
1847                  */
1848                 if (fmt->code == V4L2_MBUS_FMT_YUYV8_2X8 ||
1849                     fmt->code == V4L2_MBUS_FMT_UYVY8_2X8) {
1850                         /* Use the user requested format if YUV. */
1851                         if (pixelcode == V4L2_MBUS_FMT_YUYV8_2X8 ||
1852                             pixelcode == V4L2_MBUS_FMT_UYVY8_2X8 ||
1853                             pixelcode == V4L2_MBUS_FMT_YUYV8_1X16 ||
1854                             pixelcode == V4L2_MBUS_FMT_UYVY8_1X16)
1855                                 fmt->code = pixelcode;
1856
1857                         if (fmt->code == V4L2_MBUS_FMT_YUYV8_2X8)
1858                                 fmt->code = V4L2_MBUS_FMT_YUYV8_1X16;
1859                         else if (fmt->code == V4L2_MBUS_FMT_UYVY8_2X8)
1860                                 fmt->code = V4L2_MBUS_FMT_UYVY8_1X16;
1861                 }
1862
1863                 /* Hardcode the output size to the crop rectangle size. */
1864                 crop = __ccdc_get_crop(ccdc, fh, which);
1865                 fmt->width = crop->width;
1866                 fmt->height = crop->height;
1867                 break;
1868
1869         case CCDC_PAD_SOURCE_VP:
1870                 *fmt = *__ccdc_get_format(ccdc, fh, CCDC_PAD_SINK, which);
1871
1872                 /* The video port interface truncates the data to 10 bits. */
1873                 info = omap3isp_video_format_info(fmt->code);
1874                 fmt->code = info->truncated;
1875
1876                 /* YUV formats are not supported by the video port. */
1877                 if (fmt->code == V4L2_MBUS_FMT_YUYV8_2X8 ||
1878                     fmt->code == V4L2_MBUS_FMT_UYVY8_2X8)
1879                         fmt->code = 0;
1880
1881                 /* The number of lines that can be clocked out from the video
1882                  * port output must be at least one line less than the number
1883                  * of input lines.
1884                  */
1885                 fmt->width = clamp_t(u32, width, 32, fmt->width);
1886                 fmt->height = clamp_t(u32, height, 32, fmt->height - 1);
1887                 break;
1888         }
1889
1890         /* Data is written to memory unpacked, each 10-bit or 12-bit pixel is
1891          * stored on 2 bytes.
1892          */
1893         fmt->colorspace = V4L2_COLORSPACE_SRGB;
1894         fmt->field = V4L2_FIELD_NONE;
1895 }
1896
1897 /*
1898  * ccdc_try_crop - Validate a crop rectangle
1899  * @ccdc: ISP CCDC device
1900  * @sink: format on the sink pad
1901  * @crop: crop rectangle to be validated
1902  */
1903 static void ccdc_try_crop(struct isp_ccdc_device *ccdc,
1904                           const struct v4l2_mbus_framefmt *sink,
1905                           struct v4l2_rect *crop)
1906 {
1907         const struct isp_format_info *info;
1908         unsigned int max_width;
1909
1910         /* For Bayer formats, restrict left/top and width/height to even values
1911          * to keep the Bayer pattern.
1912          */
1913         info = omap3isp_video_format_info(sink->code);
1914         if (info->flavor != V4L2_MBUS_FMT_Y8_1X8) {
1915                 crop->left &= ~1;
1916                 crop->top &= ~1;
1917         }
1918
1919         crop->left = clamp_t(u32, crop->left, 0, sink->width - CCDC_MIN_WIDTH);
1920         crop->top = clamp_t(u32, crop->top, 0, sink->height - CCDC_MIN_HEIGHT);
1921
1922         /* The data formatter truncates the number of horizontal output pixels
1923          * to a multiple of 16. To avoid clipping data, allow callers to request
1924          * an output size bigger than the input size up to the nearest multiple
1925          * of 16.
1926          */
1927         max_width = (sink->width - crop->left + 15) & ~15;
1928         crop->width = clamp_t(u32, crop->width, CCDC_MIN_WIDTH, max_width)
1929                     & ~15;
1930         crop->height = clamp_t(u32, crop->height, CCDC_MIN_HEIGHT,
1931                                sink->height - crop->top);
1932
1933         /* Odd width/height values don't make sense for Bayer formats. */
1934         if (info->flavor != V4L2_MBUS_FMT_Y8_1X8) {
1935                 crop->width &= ~1;
1936                 crop->height &= ~1;
1937         }
1938 }
1939
1940 /*
1941  * ccdc_enum_mbus_code - Handle pixel format enumeration
1942  * @sd     : pointer to v4l2 subdev structure
1943  * @fh : V4L2 subdev file handle
1944  * @code   : pointer to v4l2_subdev_mbus_code_enum structure
1945  * return -EINVAL or zero on success
1946  */
1947 static int ccdc_enum_mbus_code(struct v4l2_subdev *sd,
1948                                struct v4l2_subdev_fh *fh,
1949                                struct v4l2_subdev_mbus_code_enum *code)
1950 {
1951         struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
1952         struct v4l2_mbus_framefmt *format;
1953
1954         switch (code->pad) {
1955         case CCDC_PAD_SINK:
1956                 if (code->index >= ARRAY_SIZE(ccdc_fmts))
1957                         return -EINVAL;
1958
1959                 code->code = ccdc_fmts[code->index];
1960                 break;
1961
1962         case CCDC_PAD_SOURCE_OF:
1963                 format = __ccdc_get_format(ccdc, fh, code->pad,
1964                                            V4L2_SUBDEV_FORMAT_TRY);
1965
1966                 if (format->code == V4L2_MBUS_FMT_YUYV8_2X8 ||
1967                     format->code == V4L2_MBUS_FMT_UYVY8_2X8) {
1968                         /* In YUV mode the CCDC can swap bytes. */
1969                         if (code->index == 0)
1970                                 code->code = V4L2_MBUS_FMT_YUYV8_1X16;
1971                         else if (code->index == 1)
1972                                 code->code = V4L2_MBUS_FMT_UYVY8_1X16;
1973                         else
1974                                 return -EINVAL;
1975                 } else {
1976                         /* In raw mode, no configurable format confversion is
1977                          * available.
1978                          */
1979                         if (code->index == 0)
1980                                 code->code = format->code;
1981                         else
1982                                 return -EINVAL;
1983                 }
1984                 break;
1985
1986         case CCDC_PAD_SOURCE_VP:
1987                 /* The CCDC supports no configurable format conversion
1988                  * compatible with the video port. Enumerate a single output
1989                  * format code.
1990                  */
1991                 if (code->index != 0)
1992                         return -EINVAL;
1993
1994                 format = __ccdc_get_format(ccdc, fh, code->pad,
1995                                            V4L2_SUBDEV_FORMAT_TRY);
1996
1997                 /* A pixel code equal to 0 means that the video port doesn't
1998                  * support the input format. Don't enumerate any pixel code.
1999                  */
2000                 if (format->code == 0)
2001                         return -EINVAL;
2002
2003                 code->code = format->code;
2004                 break;
2005
2006         default:
2007                 return -EINVAL;
2008         }
2009
2010         return 0;
2011 }
2012
2013 static int ccdc_enum_frame_size(struct v4l2_subdev *sd,
2014                                 struct v4l2_subdev_fh *fh,
2015                                 struct v4l2_subdev_frame_size_enum *fse)
2016 {
2017         struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
2018         struct v4l2_mbus_framefmt format;
2019
2020         if (fse->index != 0)
2021                 return -EINVAL;
2022
2023         format.code = fse->code;
2024         format.width = 1;
2025         format.height = 1;
2026         ccdc_try_format(ccdc, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
2027         fse->min_width = format.width;
2028         fse->min_height = format.height;
2029
2030         if (format.code != fse->code)
2031                 return -EINVAL;
2032
2033         format.code = fse->code;
2034         format.width = -1;
2035         format.height = -1;
2036         ccdc_try_format(ccdc, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
2037         fse->max_width = format.width;
2038         fse->max_height = format.height;
2039
2040         return 0;
2041 }
2042
2043 /*
2044  * ccdc_get_selection - Retrieve a selection rectangle on a pad
2045  * @sd: ISP CCDC V4L2 subdevice
2046  * @fh: V4L2 subdev file handle
2047  * @sel: Selection rectangle
2048  *
2049  * The only supported rectangles are the crop rectangles on the output formatter
2050  * source pad.
2051  *
2052  * Return 0 on success or a negative error code otherwise.
2053  */
2054 static int ccdc_get_selection(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
2055                               struct v4l2_subdev_selection *sel)
2056 {
2057         struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
2058         struct v4l2_mbus_framefmt *format;
2059
2060         if (sel->pad != CCDC_PAD_SOURCE_OF)
2061                 return -EINVAL;
2062
2063         switch (sel->target) {
2064         case V4L2_SEL_TGT_CROP_BOUNDS:
2065                 sel->r.left = 0;
2066                 sel->r.top = 0;
2067                 sel->r.width = INT_MAX;
2068                 sel->r.height = INT_MAX;
2069
2070                 format = __ccdc_get_format(ccdc, fh, CCDC_PAD_SINK, sel->which);
2071                 ccdc_try_crop(ccdc, format, &sel->r);
2072                 break;
2073
2074         case V4L2_SEL_TGT_CROP:
2075                 sel->r = *__ccdc_get_crop(ccdc, fh, sel->which);
2076                 break;
2077
2078         default:
2079                 return -EINVAL;
2080         }
2081
2082         return 0;
2083 }
2084
2085 /*
2086  * ccdc_set_selection - Set a selection rectangle on a pad
2087  * @sd: ISP CCDC V4L2 subdevice
2088  * @fh: V4L2 subdev file handle
2089  * @sel: Selection rectangle
2090  *
2091  * The only supported rectangle is the actual crop rectangle on the output
2092  * formatter source pad.
2093  *
2094  * Return 0 on success or a negative error code otherwise.
2095  */
2096 static int ccdc_set_selection(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
2097                               struct v4l2_subdev_selection *sel)
2098 {
2099         struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
2100         struct v4l2_mbus_framefmt *format;
2101
2102         if (sel->target != V4L2_SEL_TGT_CROP ||
2103             sel->pad != CCDC_PAD_SOURCE_OF)
2104                 return -EINVAL;
2105
2106         /* The crop rectangle can't be changed while streaming. */
2107         if (ccdc->state != ISP_PIPELINE_STREAM_STOPPED)
2108                 return -EBUSY;
2109
2110         /* Modifying the crop rectangle always changes the format on the source
2111          * pad. If the KEEP_CONFIG flag is set, just return the current crop
2112          * rectangle.
2113          */
2114         if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) {
2115                 sel->r = *__ccdc_get_crop(ccdc, fh, sel->which);
2116                 return 0;
2117         }
2118
2119         format = __ccdc_get_format(ccdc, fh, CCDC_PAD_SINK, sel->which);
2120         ccdc_try_crop(ccdc, format, &sel->r);
2121         *__ccdc_get_crop(ccdc, fh, sel->which) = sel->r;
2122
2123         /* Update the source format. */
2124         format = __ccdc_get_format(ccdc, fh, CCDC_PAD_SOURCE_OF, sel->which);
2125         ccdc_try_format(ccdc, fh, CCDC_PAD_SOURCE_OF, format, sel->which);
2126
2127         return 0;
2128 }
2129
2130 /*
2131  * ccdc_get_format - Retrieve the video format on a pad
2132  * @sd : ISP CCDC V4L2 subdevice
2133  * @fh : V4L2 subdev file handle
2134  * @fmt: Format
2135  *
2136  * Return 0 on success or -EINVAL if the pad is invalid or doesn't correspond
2137  * to the format type.
2138  */
2139 static int ccdc_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
2140                            struct v4l2_subdev_format *fmt)
2141 {
2142         struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
2143         struct v4l2_mbus_framefmt *format;
2144
2145         format = __ccdc_get_format(ccdc, fh, fmt->pad, fmt->which);
2146         if (format == NULL)
2147                 return -EINVAL;
2148
2149         fmt->format = *format;
2150         return 0;
2151 }
2152
2153 /*
2154  * ccdc_set_format - Set the video format on a pad
2155  * @sd : ISP CCDC V4L2 subdevice
2156  * @fh : V4L2 subdev file handle
2157  * @fmt: Format
2158  *
2159  * Return 0 on success or -EINVAL if the pad is invalid or doesn't correspond
2160  * to the format type.
2161  */
2162 static int ccdc_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
2163                            struct v4l2_subdev_format *fmt)
2164 {
2165         struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
2166         struct v4l2_mbus_framefmt *format;
2167         struct v4l2_rect *crop;
2168
2169         format = __ccdc_get_format(ccdc, fh, fmt->pad, fmt->which);
2170         if (format == NULL)
2171                 return -EINVAL;
2172
2173         ccdc_try_format(ccdc, fh, fmt->pad, &fmt->format, fmt->which);
2174         *format = fmt->format;
2175
2176         /* Propagate the format from sink to source */
2177         if (fmt->pad == CCDC_PAD_SINK) {
2178                 /* Reset the crop rectangle. */
2179                 crop = __ccdc_get_crop(ccdc, fh, fmt->which);
2180                 crop->left = 0;
2181                 crop->top = 0;
2182                 crop->width = fmt->format.width;
2183                 crop->height = fmt->format.height;
2184
2185                 ccdc_try_crop(ccdc, &fmt->format, crop);
2186
2187                 /* Update the source formats. */
2188                 format = __ccdc_get_format(ccdc, fh, CCDC_PAD_SOURCE_OF,
2189                                            fmt->which);
2190                 *format = fmt->format;
2191                 ccdc_try_format(ccdc, fh, CCDC_PAD_SOURCE_OF, format,
2192                                 fmt->which);
2193
2194                 format = __ccdc_get_format(ccdc, fh, CCDC_PAD_SOURCE_VP,
2195                                            fmt->which);
2196                 *format = fmt->format;
2197                 ccdc_try_format(ccdc, fh, CCDC_PAD_SOURCE_VP, format,
2198                                 fmt->which);
2199         }
2200
2201         return 0;
2202 }
2203
2204 /*
2205  * Decide whether desired output pixel code can be obtained with
2206  * the lane shifter by shifting the input pixel code.
2207  * @in: input pixelcode to shifter
2208  * @out: output pixelcode from shifter
2209  * @additional_shift: # of bits the sensor's LSB is offset from CAMEXT[0]
2210  *
2211  * return true if the combination is possible
2212  * return false otherwise
2213  */
2214 static bool ccdc_is_shiftable(enum v4l2_mbus_pixelcode in,
2215                               enum v4l2_mbus_pixelcode out,
2216                               unsigned int additional_shift)
2217 {
2218         const struct isp_format_info *in_info, *out_info;
2219
2220         if (in == out)
2221                 return true;
2222
2223         in_info = omap3isp_video_format_info(in);
2224         out_info = omap3isp_video_format_info(out);
2225
2226         if ((in_info->flavor == 0) || (out_info->flavor == 0))
2227                 return false;
2228
2229         if (in_info->flavor != out_info->flavor)
2230                 return false;
2231
2232         return in_info->width - out_info->width + additional_shift <= 6;
2233 }
2234
2235 static int ccdc_link_validate(struct v4l2_subdev *sd,
2236                               struct media_link *link,
2237                               struct v4l2_subdev_format *source_fmt,
2238                               struct v4l2_subdev_format *sink_fmt)
2239 {
2240         struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
2241         unsigned long parallel_shift;
2242
2243         /* Check if the two ends match */
2244         if (source_fmt->format.width != sink_fmt->format.width ||
2245             source_fmt->format.height != sink_fmt->format.height)
2246                 return -EPIPE;
2247
2248         /* We've got a parallel sensor here. */
2249         if (ccdc->input == CCDC_INPUT_PARALLEL) {
2250                 struct isp_parallel_platform_data *pdata =
2251                         &((struct isp_v4l2_subdevs_group *)
2252                           media_entity_to_v4l2_subdev(link->source->entity)
2253                           ->host_priv)->bus.parallel;
2254                 parallel_shift = pdata->data_lane_shift * 2;
2255         } else {
2256                 parallel_shift = 0;
2257         }
2258
2259         /* Lane shifter may be used to drop bits on CCDC sink pad */
2260         if (!ccdc_is_shiftable(source_fmt->format.code,
2261                                sink_fmt->format.code, parallel_shift))
2262                 return -EPIPE;
2263
2264         return 0;
2265 }
2266
2267 /*
2268  * ccdc_init_formats - Initialize formats on all pads
2269  * @sd: ISP CCDC V4L2 subdevice
2270  * @fh: V4L2 subdev file handle
2271  *
2272  * Initialize all pad formats with default values. If fh is not NULL, try
2273  * formats are initialized on the file handle. Otherwise active formats are
2274  * initialized on the device.
2275  */
2276 static int ccdc_init_formats(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
2277 {
2278         struct v4l2_subdev_format format;
2279
2280         memset(&format, 0, sizeof(format));
2281         format.pad = CCDC_PAD_SINK;
2282         format.which = fh ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
2283         format.format.code = V4L2_MBUS_FMT_SGRBG10_1X10;
2284         format.format.width = 4096;
2285         format.format.height = 4096;
2286         ccdc_set_format(sd, fh, &format);
2287
2288         return 0;
2289 }
2290
2291 /* V4L2 subdev core operations */
2292 static const struct v4l2_subdev_core_ops ccdc_v4l2_core_ops = {
2293         .ioctl = ccdc_ioctl,
2294         .subscribe_event = ccdc_subscribe_event,
2295         .unsubscribe_event = ccdc_unsubscribe_event,
2296 };
2297
2298 /* V4L2 subdev video operations */
2299 static const struct v4l2_subdev_video_ops ccdc_v4l2_video_ops = {
2300         .s_stream = ccdc_set_stream,
2301 };
2302
2303 /* V4L2 subdev pad operations */
2304 static const struct v4l2_subdev_pad_ops ccdc_v4l2_pad_ops = {
2305         .enum_mbus_code = ccdc_enum_mbus_code,
2306         .enum_frame_size = ccdc_enum_frame_size,
2307         .get_fmt = ccdc_get_format,
2308         .set_fmt = ccdc_set_format,
2309         .get_selection = ccdc_get_selection,
2310         .set_selection = ccdc_set_selection,
2311         .link_validate = ccdc_link_validate,
2312 };
2313
2314 /* V4L2 subdev operations */
2315 static const struct v4l2_subdev_ops ccdc_v4l2_ops = {
2316         .core = &ccdc_v4l2_core_ops,
2317         .video = &ccdc_v4l2_video_ops,
2318         .pad = &ccdc_v4l2_pad_ops,
2319 };
2320
2321 /* V4L2 subdev internal operations */
2322 static const struct v4l2_subdev_internal_ops ccdc_v4l2_internal_ops = {
2323         .open = ccdc_init_formats,
2324 };
2325
2326 /* -----------------------------------------------------------------------------
2327  * Media entity operations
2328  */
2329
2330 /*
2331  * ccdc_link_setup - Setup CCDC connections
2332  * @entity: CCDC media entity
2333  * @local: Pad at the local end of the link
2334  * @remote: Pad at the remote end of the link
2335  * @flags: Link flags
2336  *
2337  * return -EINVAL or zero on success
2338  */
2339 static int ccdc_link_setup(struct media_entity *entity,
2340                            const struct media_pad *local,
2341                            const struct media_pad *remote, u32 flags)
2342 {
2343         struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
2344         struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
2345         struct isp_device *isp = to_isp_device(ccdc);
2346
2347         switch (local->index | media_entity_type(remote->entity)) {
2348         case CCDC_PAD_SINK | MEDIA_ENT_T_V4L2_SUBDEV:
2349                 /* Read from the sensor (parallel interface), CCP2, CSI2a or
2350                  * CSI2c.
2351                  */
2352                 if (!(flags & MEDIA_LNK_FL_ENABLED)) {
2353                         ccdc->input = CCDC_INPUT_NONE;
2354                         break;
2355                 }
2356
2357                 if (ccdc->input != CCDC_INPUT_NONE)
2358                         return -EBUSY;
2359
2360                 if (remote->entity == &isp->isp_ccp2.subdev.entity)
2361                         ccdc->input = CCDC_INPUT_CCP2B;
2362                 else if (remote->entity == &isp->isp_csi2a.subdev.entity)
2363                         ccdc->input = CCDC_INPUT_CSI2A;
2364                 else if (remote->entity == &isp->isp_csi2c.subdev.entity)
2365                         ccdc->input = CCDC_INPUT_CSI2C;
2366                 else
2367                         ccdc->input = CCDC_INPUT_PARALLEL;
2368
2369                 break;
2370
2371         /*
2372          * The ISP core doesn't support pipelines with multiple video outputs.
2373          * Revisit this when it will be implemented, and return -EBUSY for now.
2374          */
2375
2376         case CCDC_PAD_SOURCE_VP | MEDIA_ENT_T_V4L2_SUBDEV:
2377                 /* Write to preview engine, histogram and H3A. When none of
2378                  * those links are active, the video port can be disabled.
2379                  */
2380                 if (flags & MEDIA_LNK_FL_ENABLED) {
2381                         if (ccdc->output & ~CCDC_OUTPUT_PREVIEW)
2382                                 return -EBUSY;
2383                         ccdc->output |= CCDC_OUTPUT_PREVIEW;
2384                 } else {
2385                         ccdc->output &= ~CCDC_OUTPUT_PREVIEW;
2386                 }
2387                 break;
2388
2389         case CCDC_PAD_SOURCE_OF | MEDIA_ENT_T_DEVNODE:
2390                 /* Write to memory */
2391                 if (flags & MEDIA_LNK_FL_ENABLED) {
2392                         if (ccdc->output & ~CCDC_OUTPUT_MEMORY)
2393                                 return -EBUSY;
2394                         ccdc->output |= CCDC_OUTPUT_MEMORY;
2395                 } else {
2396                         ccdc->output &= ~CCDC_OUTPUT_MEMORY;
2397                 }
2398                 break;
2399
2400         case CCDC_PAD_SOURCE_OF | MEDIA_ENT_T_V4L2_SUBDEV:
2401                 /* Write to resizer */
2402                 if (flags & MEDIA_LNK_FL_ENABLED) {
2403                         if (ccdc->output & ~CCDC_OUTPUT_RESIZER)
2404                                 return -EBUSY;
2405                         ccdc->output |= CCDC_OUTPUT_RESIZER;
2406                 } else {
2407                         ccdc->output &= ~CCDC_OUTPUT_RESIZER;
2408                 }
2409                 break;
2410
2411         default:
2412                 return -EINVAL;
2413         }
2414
2415         return 0;
2416 }
2417
2418 /* media operations */
2419 static const struct media_entity_operations ccdc_media_ops = {
2420         .link_setup = ccdc_link_setup,
2421         .link_validate = v4l2_subdev_link_validate,
2422 };
2423
2424 void omap3isp_ccdc_unregister_entities(struct isp_ccdc_device *ccdc)
2425 {
2426         v4l2_device_unregister_subdev(&ccdc->subdev);
2427         omap3isp_video_unregister(&ccdc->video_out);
2428 }
2429
2430 int omap3isp_ccdc_register_entities(struct isp_ccdc_device *ccdc,
2431         struct v4l2_device *vdev)
2432 {
2433         int ret;
2434
2435         /* Register the subdev and video node. */
2436         ret = v4l2_device_register_subdev(vdev, &ccdc->subdev);
2437         if (ret < 0)
2438                 goto error;
2439
2440         ret = omap3isp_video_register(&ccdc->video_out, vdev);
2441         if (ret < 0)
2442                 goto error;
2443
2444         return 0;
2445
2446 error:
2447         omap3isp_ccdc_unregister_entities(ccdc);
2448         return ret;
2449 }
2450
2451 /* -----------------------------------------------------------------------------
2452  * ISP CCDC initialisation and cleanup
2453  */
2454
2455 /*
2456  * ccdc_init_entities - Initialize V4L2 subdev and media entity
2457  * @ccdc: ISP CCDC module
2458  *
2459  * Return 0 on success and a negative error code on failure.
2460  */
2461 static int ccdc_init_entities(struct isp_ccdc_device *ccdc)
2462 {
2463         struct v4l2_subdev *sd = &ccdc->subdev;
2464         struct media_pad *pads = ccdc->pads;
2465         struct media_entity *me = &sd->entity;
2466         int ret;
2467
2468         ccdc->input = CCDC_INPUT_NONE;
2469
2470         v4l2_subdev_init(sd, &ccdc_v4l2_ops);
2471         sd->internal_ops = &ccdc_v4l2_internal_ops;
2472         strlcpy(sd->name, "OMAP3 ISP CCDC", sizeof(sd->name));
2473         sd->grp_id = 1 << 16;   /* group ID for isp subdevs */
2474         v4l2_set_subdevdata(sd, ccdc);
2475         sd->flags |= V4L2_SUBDEV_FL_HAS_EVENTS | V4L2_SUBDEV_FL_HAS_DEVNODE;
2476
2477         pads[CCDC_PAD_SINK].flags = MEDIA_PAD_FL_SINK
2478                                     | MEDIA_PAD_FL_MUST_CONNECT;
2479         pads[CCDC_PAD_SOURCE_VP].flags = MEDIA_PAD_FL_SOURCE;
2480         pads[CCDC_PAD_SOURCE_OF].flags = MEDIA_PAD_FL_SOURCE;
2481
2482         me->ops = &ccdc_media_ops;
2483         ret = media_entity_init(me, CCDC_PADS_NUM, pads, 0);
2484         if (ret < 0)
2485                 return ret;
2486
2487         ccdc_init_formats(sd, NULL);
2488
2489         ccdc->video_out.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2490         ccdc->video_out.ops = &ccdc_video_ops;
2491         ccdc->video_out.isp = to_isp_device(ccdc);
2492         ccdc->video_out.capture_mem = PAGE_ALIGN(4096 * 4096) * 3;
2493         ccdc->video_out.bpl_alignment = 32;
2494
2495         ret = omap3isp_video_init(&ccdc->video_out, "CCDC");
2496         if (ret < 0)
2497                 goto error_video;
2498
2499         /* Connect the CCDC subdev to the video node. */
2500         ret = media_entity_create_link(&ccdc->subdev.entity, CCDC_PAD_SOURCE_OF,
2501                         &ccdc->video_out.video.entity, 0, 0);
2502         if (ret < 0)
2503                 goto error_link;
2504
2505         return 0;
2506
2507 error_link:
2508         omap3isp_video_cleanup(&ccdc->video_out);
2509 error_video:
2510         media_entity_cleanup(me);
2511         return ret;
2512 }
2513
2514 /*
2515  * omap3isp_ccdc_init - CCDC module initialization.
2516  * @isp: Device pointer specific to the OMAP3 ISP.
2517  *
2518  * TODO: Get the initialisation values from platform data.
2519  *
2520  * Return 0 on success or a negative error code otherwise.
2521  */
2522 int omap3isp_ccdc_init(struct isp_device *isp)
2523 {
2524         struct isp_ccdc_device *ccdc = &isp->isp_ccdc;
2525         int ret;
2526
2527         spin_lock_init(&ccdc->lock);
2528         init_waitqueue_head(&ccdc->wait);
2529         mutex_init(&ccdc->ioctl_lock);
2530
2531         ccdc->stopping = CCDC_STOP_NOT_REQUESTED;
2532
2533         INIT_WORK(&ccdc->lsc.table_work, ccdc_lsc_free_table_work);
2534         ccdc->lsc.state = LSC_STATE_STOPPED;
2535         INIT_LIST_HEAD(&ccdc->lsc.free_queue);
2536         spin_lock_init(&ccdc->lsc.req_lock);
2537
2538         ccdc->clamp.oblen = 0;
2539         ccdc->clamp.dcsubval = 0;
2540
2541         ccdc->update = OMAP3ISP_CCDC_BLCLAMP;
2542         ccdc_apply_controls(ccdc);
2543
2544         ret = ccdc_init_entities(ccdc);
2545         if (ret < 0) {
2546                 mutex_destroy(&ccdc->ioctl_lock);
2547                 return ret;
2548         }
2549
2550         return 0;
2551 }
2552
2553 /*
2554  * omap3isp_ccdc_cleanup - CCDC module cleanup.
2555  * @isp: Device pointer specific to the OMAP3 ISP.
2556  */
2557 void omap3isp_ccdc_cleanup(struct isp_device *isp)
2558 {
2559         struct isp_ccdc_device *ccdc = &isp->isp_ccdc;
2560
2561         omap3isp_video_cleanup(&ccdc->video_out);
2562         media_entity_cleanup(&ccdc->subdev.entity);
2563
2564         /* Free LSC requests. As the CCDC is stopped there's no active request,
2565          * so only the pending request and the free queue need to be handled.
2566          */
2567         ccdc_lsc_free_request(ccdc, ccdc->lsc.request);
2568         cancel_work_sync(&ccdc->lsc.table_work);
2569         ccdc_lsc_free_queue(ccdc, &ccdc->lsc.free_queue);
2570
2571         if (ccdc->fpc.addr != NULL)
2572                 dma_free_coherent(isp->dev, ccdc->fpc.fpnum * 4, ccdc->fpc.addr,
2573                                   ccdc->fpc.dma);
2574
2575         mutex_destroy(&ccdc->ioctl_lock);
2576 }