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