blob: 8dbb55a92fcf184a8222796a2c6b25347b0850e5 [file] [log] [blame]
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001/*
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>
Joerg Roedele74d83a2011-09-06 11:02:15 -030034#include <linux/slab.h>
Laurent Pinchartde1135d2011-02-12 18:05:06 -030035#include <media/v4l2-event.h>
36
37#include "isp.h"
38#include "ispreg.h"
39#include "ispccdc.h"
40
Laurent Pincharta64909b2012-04-20 05:47:49 -030041#define CCDC_MIN_WIDTH 32
42#define CCDC_MIN_HEIGHT 32
43
Laurent Pinchartde1135d2011-02-12 18:05:06 -030044static 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,
Michael Jones5782f972011-03-29 05:19:08 -030050 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,
Laurent Pinchartde1135d2011-02-12 18:05:06 -030056 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)
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200372 omap_iommu_vfree(isp->domain, isp->dev, req->table);
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300373 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
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200444 req->table = omap_iommu_vmalloc(isp->domain, isp->dev, 0,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300445 req->config.size, IOMMU_FLAG);
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300446 if (IS_ERR_VALUE(req->table)) {
447 req->table = 0;
448 ret = -ENOMEM;
449 goto done;
450 }
451
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200452 req->iovm = omap_find_iovm_area(isp->dev, req->table);
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300453 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
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200468 table = omap_da_to_va(isp->dev, req->table);
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300469 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
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300737 * already done by omap_iommu_vmalloc().
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300738 */
739 size = ccdc->fpc.fpnum * 4;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200740 table_new = omap_iommu_vmalloc(isp->domain, isp->dev,
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300741 0, size, IOMMU_FLAG);
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300742 if (IS_ERR_VALUE(table_new))
743 return -ENOMEM;
744
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200745 if (copy_from_user(omap_da_to_va(isp->dev, table_new),
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300746 (__force void __user *)
747 ccdc->fpc.fpcaddr, size)) {
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200748 omap_iommu_vfree(isp->domain, isp->dev,
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300749 table_new);
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300750 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)
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200759 omap_iommu_vfree(isp->domain, isp->dev, table_old);
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300760 }
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;
Peter Meerwald13eaaa72012-06-21 13:46:05 -0300838 }
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300839
840 if (pipe->input)
841 div = DIV_ROUND_UP(l3_ick, pipe->max_rate);
Sakari Ailusc6c01f92012-02-25 21:13:41 -0300842 else if (pipe->external_rate)
843 div = l3_ick / pipe->external_rate;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300844
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);
Laurent Pinchartcf7a3d92011-08-31 10:42:17 -0300970 u32 syn_mode = ISPCCDC_SYN_MODE_VDHDEN;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300971
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300972 switch (syncif->datsz) {
973 case 8:
974 syn_mode |= ISPCCDC_SYN_MODE_DATSIZ_8;
975 break;
976 case 10:
977 syn_mode |= ISPCCDC_SYN_MODE_DATSIZ_10;
978 break;
979 case 11:
980 syn_mode |= ISPCCDC_SYN_MODE_DATSIZ_11;
981 break;
982 case 12:
983 syn_mode |= ISPCCDC_SYN_MODE_DATSIZ_12;
984 break;
Peter Meerwald13eaaa72012-06-21 13:46:05 -0300985 }
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300986
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300987 if (syncif->datapol)
988 syn_mode |= ISPCCDC_SYN_MODE_DATAPOL;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300989
990 if (syncif->hdpol)
991 syn_mode |= ISPCCDC_SYN_MODE_HDPOL;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300992
993 if (syncif->vdpol)
994 syn_mode |= ISPCCDC_SYN_MODE_VDPOL;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300995
996 isp_reg_writel(isp, syn_mode, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SYN_MODE);
997
998 if (!syncif->bt_r656_en)
999 isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_REC656IF,
1000 ISPCCDC_REC656IF_R656ON);
1001}
1002
1003/* CCDC formats descriptions */
1004static const u32 ccdc_sgrbg_pattern =
1005 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP0PLC0_SHIFT |
1006 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP0PLC1_SHIFT |
1007 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP0PLC2_SHIFT |
1008 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP0PLC3_SHIFT |
1009 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP1PLC0_SHIFT |
1010 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP1PLC1_SHIFT |
1011 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP1PLC2_SHIFT |
1012 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP1PLC3_SHIFT |
1013 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP2PLC0_SHIFT |
1014 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP2PLC1_SHIFT |
1015 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP2PLC2_SHIFT |
1016 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP2PLC3_SHIFT |
1017 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP3PLC0_SHIFT |
1018 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP3PLC1_SHIFT |
1019 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP3PLC2_SHIFT |
1020 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP3PLC3_SHIFT;
1021
1022static const u32 ccdc_srggb_pattern =
1023 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP0PLC0_SHIFT |
1024 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP0PLC1_SHIFT |
1025 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP0PLC2_SHIFT |
1026 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP0PLC3_SHIFT |
1027 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP1PLC0_SHIFT |
1028 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP1PLC1_SHIFT |
1029 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP1PLC2_SHIFT |
1030 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP1PLC3_SHIFT |
1031 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP2PLC0_SHIFT |
1032 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP2PLC1_SHIFT |
1033 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP2PLC2_SHIFT |
1034 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP2PLC3_SHIFT |
1035 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP3PLC0_SHIFT |
1036 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP3PLC1_SHIFT |
1037 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP3PLC2_SHIFT |
1038 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP3PLC3_SHIFT;
1039
1040static const u32 ccdc_sbggr_pattern =
1041 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP0PLC0_SHIFT |
1042 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP0PLC1_SHIFT |
1043 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP0PLC2_SHIFT |
1044 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP0PLC3_SHIFT |
1045 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP1PLC0_SHIFT |
1046 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP1PLC1_SHIFT |
1047 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP1PLC2_SHIFT |
1048 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP1PLC3_SHIFT |
1049 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP2PLC0_SHIFT |
1050 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP2PLC1_SHIFT |
1051 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP2PLC2_SHIFT |
1052 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP2PLC3_SHIFT |
1053 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP3PLC0_SHIFT |
1054 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP3PLC1_SHIFT |
1055 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP3PLC2_SHIFT |
1056 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP3PLC3_SHIFT;
1057
1058static const u32 ccdc_sgbrg_pattern =
1059 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP0PLC0_SHIFT |
1060 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP0PLC1_SHIFT |
1061 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP0PLC2_SHIFT |
1062 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP0PLC3_SHIFT |
1063 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP1PLC0_SHIFT |
1064 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP1PLC1_SHIFT |
1065 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP1PLC2_SHIFT |
1066 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP1PLC3_SHIFT |
1067 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP2PLC0_SHIFT |
1068 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP2PLC1_SHIFT |
1069 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP2PLC2_SHIFT |
1070 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP2PLC3_SHIFT |
1071 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP3PLC0_SHIFT |
1072 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP3PLC1_SHIFT |
1073 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP3PLC2_SHIFT |
1074 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP3PLC3_SHIFT;
1075
1076static void ccdc_configure(struct isp_ccdc_device *ccdc)
1077{
1078 struct isp_device *isp = to_isp_device(ccdc);
1079 struct isp_parallel_platform_data *pdata = NULL;
1080 struct v4l2_subdev *sensor;
1081 struct v4l2_mbus_framefmt *format;
Laurent Pincharta64909b2012-04-20 05:47:49 -03001082 const struct v4l2_rect *crop;
Michael Jonesc09af042011-03-29 05:19:09 -03001083 const struct isp_format_info *fmt_info;
1084 struct v4l2_subdev_format fmt_src;
1085 unsigned int depth_out;
1086 unsigned int depth_in = 0;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001087 struct media_pad *pad;
1088 unsigned long flags;
Michael Jonesc09af042011-03-29 05:19:09 -03001089 unsigned int shift;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001090 u32 syn_mode;
1091 u32 ccdc_pattern;
1092
Michael Jonesc09af042011-03-29 05:19:09 -03001093 pad = media_entity_remote_source(&ccdc->pads[CCDC_PAD_SINK]);
1094 sensor = media_entity_to_v4l2_subdev(pad->entity);
1095 if (ccdc->input == CCDC_INPUT_PARALLEL)
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001096 pdata = &((struct isp_v4l2_subdevs_group *)sensor->host_priv)
1097 ->bus.parallel;
Michael Jonesc09af042011-03-29 05:19:09 -03001098
1099 /* Compute shift value for lane shifter to configure the bridge. */
1100 fmt_src.pad = pad->index;
1101 fmt_src.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1102 if (!v4l2_subdev_call(sensor, pad, get_fmt, NULL, &fmt_src)) {
1103 fmt_info = omap3isp_video_format_info(fmt_src.format.code);
Laurent Pinchart1697e492011-08-31 11:57:12 -03001104 depth_in = fmt_info->width;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001105 }
1106
Michael Jonesc09af042011-03-29 05:19:09 -03001107 fmt_info = omap3isp_video_format_info
1108 (isp->isp_ccdc.formats[CCDC_PAD_SINK].code);
Laurent Pinchart1697e492011-08-31 11:57:12 -03001109 depth_out = fmt_info->width;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001110
Michael Jonesc09af042011-03-29 05:19:09 -03001111 shift = depth_in - depth_out;
1112 omap3isp_configure_bridge(isp, ccdc->input, pdata, shift);
1113
1114 ccdc->syncif.datsz = depth_out;
Laurent Pinchartcf7a3d92011-08-31 10:42:17 -03001115 ccdc->syncif.datapol = 0;
Laurent Pinchart1752cd52011-05-25 04:16:28 -03001116 ccdc->syncif.hdpol = pdata ? pdata->hs_pol : 0;
1117 ccdc->syncif.vdpol = pdata ? pdata->vs_pol : 0;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001118 ccdc_config_sync_if(ccdc, &ccdc->syncif);
1119
1120 /* CCDC_PAD_SINK */
1121 format = &ccdc->formats[CCDC_PAD_SINK];
1122
1123 syn_mode = isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SYN_MODE);
1124
1125 /* Use the raw, unprocessed data when writing to memory. The H3A and
1126 * histogram modules are still fed with lens shading corrected data.
1127 */
1128 syn_mode &= ~ISPCCDC_SYN_MODE_VP2SDR;
1129
1130 if (ccdc->output & CCDC_OUTPUT_MEMORY)
1131 syn_mode |= ISPCCDC_SYN_MODE_WEN;
1132 else
1133 syn_mode &= ~ISPCCDC_SYN_MODE_WEN;
1134
1135 if (ccdc->output & CCDC_OUTPUT_RESIZER)
1136 syn_mode |= ISPCCDC_SYN_MODE_SDR2RSZ;
1137 else
1138 syn_mode &= ~ISPCCDC_SYN_MODE_SDR2RSZ;
1139
1140 /* Use PACK8 mode for 1byte per pixel formats. */
Laurent Pinchart1697e492011-08-31 11:57:12 -03001141 if (omap3isp_video_format_info(format->code)->width <= 8)
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001142 syn_mode |= ISPCCDC_SYN_MODE_PACK8;
1143 else
1144 syn_mode &= ~ISPCCDC_SYN_MODE_PACK8;
1145
1146 isp_reg_writel(isp, syn_mode, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SYN_MODE);
1147
1148 /* Mosaic filter */
1149 switch (format->code) {
1150 case V4L2_MBUS_FMT_SRGGB10_1X10:
1151 case V4L2_MBUS_FMT_SRGGB12_1X12:
1152 ccdc_pattern = ccdc_srggb_pattern;
1153 break;
1154 case V4L2_MBUS_FMT_SBGGR10_1X10:
1155 case V4L2_MBUS_FMT_SBGGR12_1X12:
1156 ccdc_pattern = ccdc_sbggr_pattern;
1157 break;
1158 case V4L2_MBUS_FMT_SGBRG10_1X10:
1159 case V4L2_MBUS_FMT_SGBRG12_1X12:
1160 ccdc_pattern = ccdc_sgbrg_pattern;
1161 break;
1162 default:
1163 /* Use GRBG */
1164 ccdc_pattern = ccdc_sgrbg_pattern;
1165 break;
1166 }
1167 ccdc_config_imgattr(ccdc, ccdc_pattern);
1168
1169 /* Generate VD0 on the last line of the image and VD1 on the
1170 * 2/3 height line.
1171 */
1172 isp_reg_writel(isp, ((format->height - 2) << ISPCCDC_VDINT_0_SHIFT) |
1173 ((format->height * 2 / 3) << ISPCCDC_VDINT_1_SHIFT),
1174 OMAP3_ISP_IOMEM_CCDC, ISPCCDC_VDINT);
1175
1176 /* CCDC_PAD_SOURCE_OF */
Laurent Pincharta64909b2012-04-20 05:47:49 -03001177 crop = &ccdc->crop;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001178
Laurent Pincharta64909b2012-04-20 05:47:49 -03001179 isp_reg_writel(isp, (crop->left << ISPCCDC_HORZ_INFO_SPH_SHIFT) |
1180 ((crop->width - 1) << ISPCCDC_HORZ_INFO_NPH_SHIFT),
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001181 OMAP3_ISP_IOMEM_CCDC, ISPCCDC_HORZ_INFO);
Laurent Pincharta64909b2012-04-20 05:47:49 -03001182 isp_reg_writel(isp, crop->top << ISPCCDC_VERT_START_SLV0_SHIFT,
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001183 OMAP3_ISP_IOMEM_CCDC, ISPCCDC_VERT_START);
Laurent Pincharta64909b2012-04-20 05:47:49 -03001184 isp_reg_writel(isp, (crop->height - 1)
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001185 << ISPCCDC_VERT_LINES_NLV_SHIFT,
1186 OMAP3_ISP_IOMEM_CCDC, ISPCCDC_VERT_LINES);
1187
1188 ccdc_config_outlineoffset(ccdc, ccdc->video_out.bpl_value, 0, 0);
1189
1190 /* CCDC_PAD_SOURCE_VP */
1191 format = &ccdc->formats[CCDC_PAD_SOURCE_VP];
1192
1193 isp_reg_writel(isp, (0 << ISPCCDC_FMT_HORZ_FMTSPH_SHIFT) |
1194 (format->width << ISPCCDC_FMT_HORZ_FMTLNH_SHIFT),
1195 OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FMT_HORZ);
1196 isp_reg_writel(isp, (0 << ISPCCDC_FMT_VERT_FMTSLV_SHIFT) |
1197 ((format->height + 1) << ISPCCDC_FMT_VERT_FMTLNV_SHIFT),
1198 OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FMT_VERT);
1199
1200 isp_reg_writel(isp, (format->width << ISPCCDC_VP_OUT_HORZ_NUM_SHIFT) |
1201 (format->height << ISPCCDC_VP_OUT_VERT_NUM_SHIFT),
1202 OMAP3_ISP_IOMEM_CCDC, ISPCCDC_VP_OUT);
1203
1204 spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
1205 if (ccdc->lsc.request == NULL)
1206 goto unlock;
1207
1208 WARN_ON(ccdc->lsc.active);
1209
1210 /* Get last good LSC configuration. If it is not supported for
1211 * the current active resolution discard it.
1212 */
1213 if (ccdc->lsc.active == NULL &&
1214 __ccdc_lsc_configure(ccdc, ccdc->lsc.request) == 0) {
1215 ccdc->lsc.active = ccdc->lsc.request;
1216 } else {
1217 list_add_tail(&ccdc->lsc.request->list, &ccdc->lsc.free_queue);
1218 schedule_work(&ccdc->lsc.table_work);
1219 }
1220
1221 ccdc->lsc.request = NULL;
1222
1223unlock:
1224 spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
1225
1226 ccdc_apply_controls(ccdc);
1227}
1228
1229static void __ccdc_enable(struct isp_ccdc_device *ccdc, int enable)
1230{
1231 struct isp_device *isp = to_isp_device(ccdc);
1232
1233 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_PCR,
1234 ISPCCDC_PCR_EN, enable ? ISPCCDC_PCR_EN : 0);
1235}
1236
1237static int ccdc_disable(struct isp_ccdc_device *ccdc)
1238{
1239 unsigned long flags;
1240 int ret = 0;
1241
1242 spin_lock_irqsave(&ccdc->lock, flags);
1243 if (ccdc->state == ISP_PIPELINE_STREAM_CONTINUOUS)
1244 ccdc->stopping = CCDC_STOP_REQUEST;
1245 spin_unlock_irqrestore(&ccdc->lock, flags);
1246
1247 ret = wait_event_timeout(ccdc->wait,
1248 ccdc->stopping == CCDC_STOP_FINISHED,
1249 msecs_to_jiffies(2000));
1250 if (ret == 0) {
1251 ret = -ETIMEDOUT;
1252 dev_warn(to_device(ccdc), "CCDC stop timeout!\n");
1253 }
1254
1255 omap3isp_sbl_disable(to_isp_device(ccdc), OMAP3_ISP_SBL_CCDC_LSC_READ);
1256
1257 mutex_lock(&ccdc->ioctl_lock);
1258 ccdc_lsc_free_request(ccdc, ccdc->lsc.request);
1259 ccdc->lsc.request = ccdc->lsc.active;
1260 ccdc->lsc.active = NULL;
1261 cancel_work_sync(&ccdc->lsc.table_work);
1262 ccdc_lsc_free_queue(ccdc, &ccdc->lsc.free_queue);
1263 mutex_unlock(&ccdc->ioctl_lock);
1264
1265 ccdc->stopping = CCDC_STOP_NOT_REQUESTED;
1266
1267 return ret > 0 ? 0 : ret;
1268}
1269
1270static void ccdc_enable(struct isp_ccdc_device *ccdc)
1271{
1272 if (ccdc_lsc_is_configured(ccdc))
1273 __ccdc_lsc_enable(ccdc, 1);
1274 __ccdc_enable(ccdc, 1);
1275}
1276
1277/* -----------------------------------------------------------------------------
1278 * Interrupt handling
1279 */
1280
1281/*
1282 * ccdc_sbl_busy - Poll idle state of CCDC and related SBL memory write bits
1283 * @ccdc: Pointer to ISP CCDC device.
1284 *
1285 * Returns zero if the CCDC is idle and the image has been written to
1286 * memory, too.
1287 */
1288static int ccdc_sbl_busy(struct isp_ccdc_device *ccdc)
1289{
1290 struct isp_device *isp = to_isp_device(ccdc);
1291
1292 return omap3isp_ccdc_busy(ccdc)
1293 | (isp_reg_readl(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_CCDC_WR_0) &
1294 ISPSBL_CCDC_WR_0_DATA_READY)
1295 | (isp_reg_readl(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_CCDC_WR_1) &
1296 ISPSBL_CCDC_WR_0_DATA_READY)
1297 | (isp_reg_readl(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_CCDC_WR_2) &
1298 ISPSBL_CCDC_WR_0_DATA_READY)
1299 | (isp_reg_readl(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_CCDC_WR_3) &
1300 ISPSBL_CCDC_WR_0_DATA_READY);
1301}
1302
1303/*
1304 * ccdc_sbl_wait_idle - Wait until the CCDC and related SBL are idle
1305 * @ccdc: Pointer to ISP CCDC device.
1306 * @max_wait: Max retry count in us for wait for idle/busy transition.
1307 */
1308static int ccdc_sbl_wait_idle(struct isp_ccdc_device *ccdc,
1309 unsigned int max_wait)
1310{
1311 unsigned int wait = 0;
1312
1313 if (max_wait == 0)
1314 max_wait = 10000; /* 10 ms */
1315
1316 for (wait = 0; wait <= max_wait; wait++) {
1317 if (!ccdc_sbl_busy(ccdc))
1318 return 0;
1319
1320 rmb();
1321 udelay(1);
1322 }
1323
1324 return -EBUSY;
1325}
1326
1327/* __ccdc_handle_stopping - Handle CCDC and/or LSC stopping sequence
1328 * @ccdc: Pointer to ISP CCDC device.
1329 * @event: Pointing which event trigger handler
1330 *
Michael Jones2d4e9d12011-02-28 08:29:03 -03001331 * Return 1 when the event and stopping request combination is satisfied,
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001332 * zero otherwise.
1333 */
1334static int __ccdc_handle_stopping(struct isp_ccdc_device *ccdc, u32 event)
1335{
1336 int rval = 0;
1337
1338 switch ((ccdc->stopping & 3) | event) {
1339 case CCDC_STOP_REQUEST | CCDC_EVENT_VD1:
1340 if (ccdc->lsc.state != LSC_STATE_STOPPED)
1341 __ccdc_lsc_enable(ccdc, 0);
1342 __ccdc_enable(ccdc, 0);
1343 ccdc->stopping = CCDC_STOP_EXECUTED;
1344 return 1;
1345
1346 case CCDC_STOP_EXECUTED | CCDC_EVENT_VD0:
1347 ccdc->stopping |= CCDC_STOP_CCDC_FINISHED;
1348 if (ccdc->lsc.state == LSC_STATE_STOPPED)
1349 ccdc->stopping |= CCDC_STOP_LSC_FINISHED;
1350 rval = 1;
1351 break;
1352
1353 case CCDC_STOP_EXECUTED | CCDC_EVENT_LSC_DONE:
1354 ccdc->stopping |= CCDC_STOP_LSC_FINISHED;
1355 rval = 1;
1356 break;
1357
1358 case CCDC_STOP_EXECUTED | CCDC_EVENT_VD1:
1359 return 1;
1360 }
1361
1362 if (ccdc->stopping == CCDC_STOP_FINISHED) {
1363 wake_up(&ccdc->wait);
1364 rval = 1;
1365 }
1366
1367 return rval;
1368}
1369
1370static void ccdc_hs_vs_isr(struct isp_ccdc_device *ccdc)
1371{
Laurent Pinchartbd0f2e62011-11-11 11:22:20 -03001372 struct isp_pipeline *pipe = to_isp_pipeline(&ccdc->subdev.entity);
Laurent Pinchart63ae37e2011-11-16 10:54:02 -03001373 struct video_device *vdev = ccdc->subdev.devnode;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001374 struct v4l2_event event;
1375
Laurent Pinchartb43883d2012-02-09 13:00:45 -03001376 /* Frame number propagation */
1377 atomic_inc(&pipe->frame_number);
1378
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001379 memset(&event, 0, sizeof(event));
Sakari Ailus69d232a2011-06-15 15:58:48 -03001380 event.type = V4L2_EVENT_FRAME_SYNC;
1381 event.u.frame_sync.frame_sequence = atomic_read(&pipe->frame_number);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001382
1383 v4l2_event_queue(vdev, &event);
1384}
1385
1386/*
1387 * ccdc_lsc_isr - Handle LSC events
1388 * @ccdc: Pointer to ISP CCDC device.
1389 * @events: LSC events
1390 */
1391static void ccdc_lsc_isr(struct isp_ccdc_device *ccdc, u32 events)
1392{
1393 unsigned long flags;
1394
1395 if (events & IRQ0STATUS_CCDC_LSC_PREF_ERR_IRQ) {
Laurent Pinchart875e2e32011-12-07 08:34:50 -03001396 struct isp_pipeline *pipe =
1397 to_isp_pipeline(&ccdc->subdev.entity);
1398
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001399 ccdc_lsc_error_handler(ccdc);
Laurent Pinchart875e2e32011-12-07 08:34:50 -03001400 pipe->error = true;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001401 dev_dbg(to_device(ccdc), "lsc prefetch error\n");
1402 }
1403
1404 if (!(events & IRQ0STATUS_CCDC_LSC_DONE_IRQ))
1405 return;
1406
1407 /* LSC_DONE interrupt occur, there are two cases
1408 * 1. stopping for reconfiguration
1409 * 2. stopping because of STREAM OFF command
1410 */
1411 spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
1412
1413 if (ccdc->lsc.state == LSC_STATE_STOPPING)
1414 ccdc->lsc.state = LSC_STATE_STOPPED;
1415
1416 if (__ccdc_handle_stopping(ccdc, CCDC_EVENT_LSC_DONE))
1417 goto done;
1418
1419 if (ccdc->lsc.state != LSC_STATE_RECONFIG)
1420 goto done;
1421
1422 /* LSC is in STOPPING state, change to the new state */
1423 ccdc->lsc.state = LSC_STATE_STOPPED;
1424
1425 /* This is an exception. Start of frame and LSC_DONE interrupt
1426 * have been received on the same time. Skip this event and wait
1427 * for better times.
1428 */
1429 if (events & IRQ0STATUS_HS_VS_IRQ)
1430 goto done;
1431
1432 /* The LSC engine is stopped at this point. Enable it if there's a
1433 * pending request.
1434 */
1435 if (ccdc->lsc.request == NULL)
1436 goto done;
1437
1438 ccdc_lsc_enable(ccdc);
1439
1440done:
1441 spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
1442}
1443
1444static int ccdc_isr_buffer(struct isp_ccdc_device *ccdc)
1445{
1446 struct isp_pipeline *pipe = to_isp_pipeline(&ccdc->subdev.entity);
1447 struct isp_device *isp = to_isp_device(ccdc);
1448 struct isp_buffer *buffer;
1449 int restart = 0;
1450
1451 /* The CCDC generates VD0 interrupts even when disabled (the datasheet
1452 * doesn't explicitly state if that's supposed to happen or not, so it
1453 * can be considered as a hardware bug or as a feature, but we have to
1454 * deal with it anyway). Disabling the CCDC when no buffer is available
1455 * would thus not be enough, we need to handle the situation explicitly.
1456 */
1457 if (list_empty(&ccdc->video_out.dmaqueue))
1458 goto done;
1459
1460 /* We're in continuous mode, and memory writes were disabled due to a
1461 * buffer underrun. Reenable them now that we have a buffer. The buffer
1462 * address has been set in ccdc_video_queue.
1463 */
1464 if (ccdc->state == ISP_PIPELINE_STREAM_CONTINUOUS && ccdc->underrun) {
1465 restart = 1;
1466 ccdc->underrun = 0;
1467 goto done;
1468 }
1469
1470 if (ccdc_sbl_wait_idle(ccdc, 1000)) {
1471 dev_info(isp->dev, "CCDC won't become idle!\n");
1472 goto done;
1473 }
1474
Laurent Pinchart875e2e32011-12-07 08:34:50 -03001475 buffer = omap3isp_video_buffer_next(&ccdc->video_out);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001476 if (buffer != NULL) {
1477 ccdc_set_outaddr(ccdc, buffer->isp_addr);
1478 restart = 1;
1479 }
1480
1481 pipe->state |= ISP_PIPELINE_IDLE_OUTPUT;
1482
1483 if (ccdc->state == ISP_PIPELINE_STREAM_SINGLESHOT &&
1484 isp_pipeline_ready(pipe))
1485 omap3isp_pipeline_set_stream(pipe,
1486 ISP_PIPELINE_STREAM_SINGLESHOT);
1487
1488done:
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001489 return restart;
1490}
1491
1492/*
1493 * ccdc_vd0_isr - Handle VD0 event
1494 * @ccdc: Pointer to ISP CCDC device.
1495 *
1496 * Executes LSC deferred enablement before next frame starts.
1497 */
1498static void ccdc_vd0_isr(struct isp_ccdc_device *ccdc)
1499{
1500 unsigned long flags;
1501 int restart = 0;
1502
1503 if (ccdc->output & CCDC_OUTPUT_MEMORY)
1504 restart = ccdc_isr_buffer(ccdc);
1505
1506 spin_lock_irqsave(&ccdc->lock, flags);
1507 if (__ccdc_handle_stopping(ccdc, CCDC_EVENT_VD0)) {
1508 spin_unlock_irqrestore(&ccdc->lock, flags);
1509 return;
1510 }
1511
1512 if (!ccdc->shadow_update)
1513 ccdc_apply_controls(ccdc);
1514 spin_unlock_irqrestore(&ccdc->lock, flags);
1515
1516 if (restart)
1517 ccdc_enable(ccdc);
1518}
1519
1520/*
1521 * ccdc_vd1_isr - Handle VD1 event
1522 * @ccdc: Pointer to ISP CCDC device.
1523 */
1524static void ccdc_vd1_isr(struct isp_ccdc_device *ccdc)
1525{
1526 unsigned long flags;
1527
1528 spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
1529
1530 /*
1531 * Depending on the CCDC pipeline state, CCDC stopping should be
1532 * handled differently. In SINGLESHOT we emulate an internal CCDC
1533 * stopping because the CCDC hw works only in continuous mode.
1534 * When CONTINUOUS pipeline state is used and the CCDC writes it's
1535 * data to memory the CCDC and LSC are stopped immediately but
1536 * without change the CCDC stopping state machine. The CCDC
1537 * stopping state machine should be used only when user request
1538 * for stopping is received (SINGLESHOT is an exeption).
1539 */
1540 switch (ccdc->state) {
1541 case ISP_PIPELINE_STREAM_SINGLESHOT:
1542 ccdc->stopping = CCDC_STOP_REQUEST;
1543 break;
1544
1545 case ISP_PIPELINE_STREAM_CONTINUOUS:
1546 if (ccdc->output & CCDC_OUTPUT_MEMORY) {
1547 if (ccdc->lsc.state != LSC_STATE_STOPPED)
1548 __ccdc_lsc_enable(ccdc, 0);
1549 __ccdc_enable(ccdc, 0);
1550 }
1551 break;
1552
1553 case ISP_PIPELINE_STREAM_STOPPED:
1554 break;
1555 }
1556
1557 if (__ccdc_handle_stopping(ccdc, CCDC_EVENT_VD1))
1558 goto done;
1559
1560 if (ccdc->lsc.request == NULL)
1561 goto done;
1562
1563 /*
1564 * LSC need to be reconfigured. Stop it here and on next LSC_DONE IRQ
1565 * do the appropriate changes in registers
1566 */
1567 if (ccdc->lsc.state == LSC_STATE_RUNNING) {
1568 __ccdc_lsc_enable(ccdc, 0);
1569 ccdc->lsc.state = LSC_STATE_RECONFIG;
1570 goto done;
1571 }
1572
1573 /* LSC has been in STOPPED state, enable it */
1574 if (ccdc->lsc.state == LSC_STATE_STOPPED)
1575 ccdc_lsc_enable(ccdc);
1576
1577done:
1578 spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
1579}
1580
1581/*
1582 * omap3isp_ccdc_isr - Configure CCDC during interframe time.
1583 * @ccdc: Pointer to ISP CCDC device.
1584 * @events: CCDC events
1585 */
1586int omap3isp_ccdc_isr(struct isp_ccdc_device *ccdc, u32 events)
1587{
1588 if (ccdc->state == ISP_PIPELINE_STREAM_STOPPED)
1589 return 0;
1590
1591 if (events & IRQ0STATUS_CCDC_VD1_IRQ)
1592 ccdc_vd1_isr(ccdc);
1593
1594 ccdc_lsc_isr(ccdc, events);
1595
1596 if (events & IRQ0STATUS_CCDC_VD0_IRQ)
1597 ccdc_vd0_isr(ccdc);
1598
1599 if (events & IRQ0STATUS_HS_VS_IRQ)
1600 ccdc_hs_vs_isr(ccdc);
1601
1602 return 0;
1603}
1604
1605/* -----------------------------------------------------------------------------
1606 * ISP video operations
1607 */
1608
1609static int ccdc_video_queue(struct isp_video *video, struct isp_buffer *buffer)
1610{
1611 struct isp_ccdc_device *ccdc = &video->isp->isp_ccdc;
1612
1613 if (!(ccdc->output & CCDC_OUTPUT_MEMORY))
1614 return -ENODEV;
1615
1616 ccdc_set_outaddr(ccdc, buffer->isp_addr);
1617
Michael Jones2d4e9d12011-02-28 08:29:03 -03001618 /* We now have a buffer queued on the output, restart the pipeline
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001619 * on the next CCDC interrupt if running in continuous mode (or when
1620 * starting the stream).
1621 */
1622 ccdc->underrun = 1;
1623
1624 return 0;
1625}
1626
1627static const struct isp_video_operations ccdc_video_ops = {
1628 .queue = ccdc_video_queue,
1629};
1630
1631/* -----------------------------------------------------------------------------
1632 * V4L2 subdev operations
1633 */
1634
1635/*
1636 * ccdc_ioctl - CCDC module private ioctl's
1637 * @sd: ISP CCDC V4L2 subdevice
1638 * @cmd: ioctl command
1639 * @arg: ioctl argument
1640 *
1641 * Return 0 on success or a negative error code otherwise.
1642 */
1643static long ccdc_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1644{
1645 struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
1646 int ret;
1647
1648 switch (cmd) {
1649 case VIDIOC_OMAP3ISP_CCDC_CFG:
1650 mutex_lock(&ccdc->ioctl_lock);
1651 ret = ccdc_config(ccdc, arg);
1652 mutex_unlock(&ccdc->ioctl_lock);
1653 break;
1654
1655 default:
1656 return -ENOIOCTLCMD;
1657 }
1658
1659 return ret;
1660}
1661
1662static int ccdc_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1663 struct v4l2_event_subscription *sub)
1664{
Sakari Ailus69d232a2011-06-15 15:58:48 -03001665 if (sub->type != V4L2_EVENT_FRAME_SYNC)
1666 return -EINVAL;
1667
1668 /* line number is zero at frame start */
1669 if (sub->id != 0)
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001670 return -EINVAL;
1671
Hans de Goedec53c2542012-04-08 12:59:46 -03001672 return v4l2_event_subscribe(fh, sub, OMAP3ISP_CCDC_NEVENTS, NULL);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001673}
1674
1675static int ccdc_unsubscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1676 struct v4l2_event_subscription *sub)
1677{
1678 return v4l2_event_unsubscribe(fh, sub);
1679}
1680
1681/*
1682 * ccdc_set_stream - Enable/Disable streaming on the CCDC module
1683 * @sd: ISP CCDC V4L2 subdevice
1684 * @enable: Enable/disable stream
1685 *
1686 * When writing to memory, the CCDC hardware can't be enabled without a memory
1687 * buffer to write to. As the s_stream operation is called in response to a
1688 * STREAMON call without any buffer queued yet, just update the enabled field
1689 * and return immediately. The CCDC will be enabled in ccdc_isr_buffer().
1690 *
1691 * When not writing to memory enable the CCDC immediately.
1692 */
1693static int ccdc_set_stream(struct v4l2_subdev *sd, int enable)
1694{
1695 struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
1696 struct isp_device *isp = to_isp_device(ccdc);
1697 int ret = 0;
1698
1699 if (ccdc->state == ISP_PIPELINE_STREAM_STOPPED) {
1700 if (enable == ISP_PIPELINE_STREAM_STOPPED)
1701 return 0;
1702
1703 omap3isp_subclk_enable(isp, OMAP3_ISP_SUBCLK_CCDC);
1704 isp_reg_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CFG,
1705 ISPCCDC_CFG_VDLC);
1706
1707 ccdc_configure(ccdc);
1708
1709 /* TODO: Don't configure the video port if all of its output
1710 * links are inactive.
1711 */
1712 ccdc_config_vp(ccdc);
1713 ccdc_enable_vp(ccdc, 1);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001714 ccdc_print_status(ccdc);
1715 }
1716
1717 switch (enable) {
1718 case ISP_PIPELINE_STREAM_CONTINUOUS:
1719 if (ccdc->output & CCDC_OUTPUT_MEMORY)
1720 omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_CCDC_WRITE);
1721
1722 if (ccdc->underrun || !(ccdc->output & CCDC_OUTPUT_MEMORY))
1723 ccdc_enable(ccdc);
1724
1725 ccdc->underrun = 0;
1726 break;
1727
1728 case ISP_PIPELINE_STREAM_SINGLESHOT:
1729 if (ccdc->output & CCDC_OUTPUT_MEMORY &&
1730 ccdc->state != ISP_PIPELINE_STREAM_SINGLESHOT)
1731 omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_CCDC_WRITE);
1732
1733 ccdc_enable(ccdc);
1734 break;
1735
1736 case ISP_PIPELINE_STREAM_STOPPED:
1737 ret = ccdc_disable(ccdc);
1738 if (ccdc->output & CCDC_OUTPUT_MEMORY)
1739 omap3isp_sbl_disable(isp, OMAP3_ISP_SBL_CCDC_WRITE);
1740 omap3isp_subclk_disable(isp, OMAP3_ISP_SUBCLK_CCDC);
1741 ccdc->underrun = 0;
1742 break;
1743 }
1744
1745 ccdc->state = enable;
1746 return ret;
1747}
1748
1749static struct v4l2_mbus_framefmt *
1750__ccdc_get_format(struct isp_ccdc_device *ccdc, struct v4l2_subdev_fh *fh,
1751 unsigned int pad, enum v4l2_subdev_format_whence which)
1752{
1753 if (which == V4L2_SUBDEV_FORMAT_TRY)
1754 return v4l2_subdev_get_try_format(fh, pad);
1755 else
1756 return &ccdc->formats[pad];
1757}
1758
Laurent Pincharta64909b2012-04-20 05:47:49 -03001759static struct v4l2_rect *
1760__ccdc_get_crop(struct isp_ccdc_device *ccdc, struct v4l2_subdev_fh *fh,
1761 enum v4l2_subdev_format_whence which)
1762{
1763 if (which == V4L2_SUBDEV_FORMAT_TRY)
1764 return v4l2_subdev_get_try_crop(fh, CCDC_PAD_SOURCE_OF);
1765 else
1766 return &ccdc->crop;
1767}
1768
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001769/*
1770 * ccdc_try_format - Try video format on a pad
1771 * @ccdc: ISP CCDC device
1772 * @fh : V4L2 subdev file handle
1773 * @pad: Pad number
1774 * @fmt: Format
1775 */
1776static void
1777ccdc_try_format(struct isp_ccdc_device *ccdc, struct v4l2_subdev_fh *fh,
1778 unsigned int pad, struct v4l2_mbus_framefmt *fmt,
1779 enum v4l2_subdev_format_whence which)
1780{
1781 struct v4l2_mbus_framefmt *format;
1782 const struct isp_format_info *info;
1783 unsigned int width = fmt->width;
1784 unsigned int height = fmt->height;
Laurent Pincharta64909b2012-04-20 05:47:49 -03001785 struct v4l2_rect *crop;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001786 unsigned int i;
1787
1788 switch (pad) {
1789 case CCDC_PAD_SINK:
1790 /* TODO: If the CCDC output formatter pad is connected directly
1791 * to the resizer, only YUV formats can be used.
1792 */
1793 for (i = 0; i < ARRAY_SIZE(ccdc_fmts); i++) {
1794 if (fmt->code == ccdc_fmts[i])
1795 break;
1796 }
1797
1798 /* If not found, use SGRBG10 as default */
1799 if (i >= ARRAY_SIZE(ccdc_fmts))
1800 fmt->code = V4L2_MBUS_FMT_SGRBG10_1X10;
1801
1802 /* Clamp the input size. */
1803 fmt->width = clamp_t(u32, width, 32, 4096);
1804 fmt->height = clamp_t(u32, height, 32, 4096);
1805 break;
1806
1807 case CCDC_PAD_SOURCE_OF:
1808 format = __ccdc_get_format(ccdc, fh, CCDC_PAD_SINK, which);
1809 memcpy(fmt, format, sizeof(*fmt));
1810
Laurent Pincharta64909b2012-04-20 05:47:49 -03001811 /* Hardcode the output size to the crop rectangle size. */
1812 crop = __ccdc_get_crop(ccdc, fh, which);
1813 fmt->width = crop->width;
1814 fmt->height = crop->height;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001815 break;
1816
1817 case CCDC_PAD_SOURCE_VP:
1818 format = __ccdc_get_format(ccdc, fh, CCDC_PAD_SINK, which);
1819 memcpy(fmt, format, sizeof(*fmt));
1820
1821 /* The video port interface truncates the data to 10 bits. */
1822 info = omap3isp_video_format_info(fmt->code);
1823 fmt->code = info->truncated;
1824
1825 /* The number of lines that can be clocked out from the video
1826 * port output must be at least one line less than the number
1827 * of input lines.
1828 */
1829 fmt->width = clamp_t(u32, width, 32, fmt->width);
1830 fmt->height = clamp_t(u32, height, 32, fmt->height - 1);
1831 break;
1832 }
1833
1834 /* Data is written to memory unpacked, each 10-bit or 12-bit pixel is
1835 * stored on 2 bytes.
1836 */
1837 fmt->colorspace = V4L2_COLORSPACE_SRGB;
1838 fmt->field = V4L2_FIELD_NONE;
1839}
1840
1841/*
Laurent Pincharta64909b2012-04-20 05:47:49 -03001842 * ccdc_try_crop - Validate a crop rectangle
1843 * @ccdc: ISP CCDC device
1844 * @sink: format on the sink pad
1845 * @crop: crop rectangle to be validated
1846 */
1847static void ccdc_try_crop(struct isp_ccdc_device *ccdc,
1848 const struct v4l2_mbus_framefmt *sink,
1849 struct v4l2_rect *crop)
1850{
1851 const struct isp_format_info *info;
1852 unsigned int max_width;
1853
1854 /* For Bayer formats, restrict left/top and width/height to even values
1855 * to keep the Bayer pattern.
1856 */
1857 info = omap3isp_video_format_info(sink->code);
1858 if (info->flavor != V4L2_MBUS_FMT_Y8_1X8) {
1859 crop->left &= ~1;
1860 crop->top &= ~1;
1861 }
1862
1863 crop->left = clamp_t(u32, crop->left, 0, sink->width - CCDC_MIN_WIDTH);
1864 crop->top = clamp_t(u32, crop->top, 0, sink->height - CCDC_MIN_HEIGHT);
1865
1866 /* The data formatter truncates the number of horizontal output pixels
1867 * to a multiple of 16. To avoid clipping data, allow callers to request
1868 * an output size bigger than the input size up to the nearest multiple
1869 * of 16.
1870 */
1871 max_width = (sink->width - crop->left + 15) & ~15;
1872 crop->width = clamp_t(u32, crop->width, CCDC_MIN_WIDTH, max_width)
1873 & ~15;
1874 crop->height = clamp_t(u32, crop->height, CCDC_MIN_HEIGHT,
1875 sink->height - crop->top);
1876
1877 /* Odd width/height values don't make sense for Bayer formats. */
1878 if (info->flavor != V4L2_MBUS_FMT_Y8_1X8) {
1879 crop->width &= ~1;
1880 crop->height &= ~1;
1881 }
1882}
1883
1884/*
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001885 * ccdc_enum_mbus_code - Handle pixel format enumeration
1886 * @sd : pointer to v4l2 subdev structure
1887 * @fh : V4L2 subdev file handle
1888 * @code : pointer to v4l2_subdev_mbus_code_enum structure
1889 * return -EINVAL or zero on success
1890 */
1891static int ccdc_enum_mbus_code(struct v4l2_subdev *sd,
1892 struct v4l2_subdev_fh *fh,
1893 struct v4l2_subdev_mbus_code_enum *code)
1894{
1895 struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
1896 struct v4l2_mbus_framefmt *format;
1897
1898 switch (code->pad) {
1899 case CCDC_PAD_SINK:
1900 if (code->index >= ARRAY_SIZE(ccdc_fmts))
1901 return -EINVAL;
1902
1903 code->code = ccdc_fmts[code->index];
1904 break;
1905
1906 case CCDC_PAD_SOURCE_OF:
1907 case CCDC_PAD_SOURCE_VP:
1908 /* No format conversion inside CCDC */
1909 if (code->index != 0)
1910 return -EINVAL;
1911
1912 format = __ccdc_get_format(ccdc, fh, CCDC_PAD_SINK,
1913 V4L2_SUBDEV_FORMAT_TRY);
1914
1915 code->code = format->code;
1916 break;
1917
1918 default:
1919 return -EINVAL;
1920 }
1921
1922 return 0;
1923}
1924
1925static int ccdc_enum_frame_size(struct v4l2_subdev *sd,
1926 struct v4l2_subdev_fh *fh,
1927 struct v4l2_subdev_frame_size_enum *fse)
1928{
1929 struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
1930 struct v4l2_mbus_framefmt format;
1931
1932 if (fse->index != 0)
1933 return -EINVAL;
1934
1935 format.code = fse->code;
1936 format.width = 1;
1937 format.height = 1;
1938 ccdc_try_format(ccdc, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
1939 fse->min_width = format.width;
1940 fse->min_height = format.height;
1941
1942 if (format.code != fse->code)
1943 return -EINVAL;
1944
1945 format.code = fse->code;
1946 format.width = -1;
1947 format.height = -1;
1948 ccdc_try_format(ccdc, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
1949 fse->max_width = format.width;
1950 fse->max_height = format.height;
1951
1952 return 0;
1953}
1954
1955/*
Laurent Pincharta64909b2012-04-20 05:47:49 -03001956 * ccdc_get_selection - Retrieve a selection rectangle on a pad
1957 * @sd: ISP CCDC V4L2 subdevice
1958 * @fh: V4L2 subdev file handle
1959 * @sel: Selection rectangle
1960 *
1961 * The only supported rectangles are the crop rectangles on the output formatter
1962 * source pad.
1963 *
1964 * Return 0 on success or a negative error code otherwise.
1965 */
1966static int ccdc_get_selection(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
1967 struct v4l2_subdev_selection *sel)
1968{
1969 struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
1970 struct v4l2_mbus_framefmt *format;
1971
1972 if (sel->pad != CCDC_PAD_SOURCE_OF)
1973 return -EINVAL;
1974
1975 switch (sel->target) {
Sakari Ailus5689b282012-05-18 09:31:18 -03001976 case V4L2_SEL_TGT_CROP_BOUNDS:
Laurent Pincharta64909b2012-04-20 05:47:49 -03001977 sel->r.left = 0;
1978 sel->r.top = 0;
1979 sel->r.width = INT_MAX;
1980 sel->r.height = INT_MAX;
1981
1982 format = __ccdc_get_format(ccdc, fh, CCDC_PAD_SINK, sel->which);
1983 ccdc_try_crop(ccdc, format, &sel->r);
1984 break;
1985
Sakari Ailus5689b282012-05-18 09:31:18 -03001986 case V4L2_SEL_TGT_CROP:
Laurent Pincharta64909b2012-04-20 05:47:49 -03001987 sel->r = *__ccdc_get_crop(ccdc, fh, sel->which);
1988 break;
1989
1990 default:
1991 return -EINVAL;
1992 }
1993
1994 return 0;
1995}
1996
1997/*
1998 * ccdc_set_selection - Set a selection rectangle on a pad
1999 * @sd: ISP CCDC V4L2 subdevice
2000 * @fh: V4L2 subdev file handle
2001 * @sel: Selection rectangle
2002 *
2003 * The only supported rectangle is the actual crop rectangle on the output
2004 * formatter source pad.
2005 *
2006 * Return 0 on success or a negative error code otherwise.
2007 */
2008static int ccdc_set_selection(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
2009 struct v4l2_subdev_selection *sel)
2010{
2011 struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
2012 struct v4l2_mbus_framefmt *format;
2013
Sakari Ailus5689b282012-05-18 09:31:18 -03002014 if (sel->target != V4L2_SEL_TGT_CROP ||
Laurent Pincharta64909b2012-04-20 05:47:49 -03002015 sel->pad != CCDC_PAD_SOURCE_OF)
2016 return -EINVAL;
2017
2018 /* The crop rectangle can't be changed while streaming. */
2019 if (ccdc->state != ISP_PIPELINE_STREAM_STOPPED)
2020 return -EBUSY;
2021
2022 /* Modifying the crop rectangle always changes the format on the source
2023 * pad. If the KEEP_CONFIG flag is set, just return the current crop
2024 * rectangle.
2025 */
Sakari Ailus563df3d2012-06-13 16:01:10 -03002026 if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) {
Laurent Pincharta64909b2012-04-20 05:47:49 -03002027 sel->r = *__ccdc_get_crop(ccdc, fh, sel->which);
2028 return 0;
2029 }
2030
2031 format = __ccdc_get_format(ccdc, fh, CCDC_PAD_SINK, sel->which);
2032 ccdc_try_crop(ccdc, format, &sel->r);
2033 *__ccdc_get_crop(ccdc, fh, sel->which) = sel->r;
2034
2035 /* Update the source format. */
2036 format = __ccdc_get_format(ccdc, fh, CCDC_PAD_SOURCE_OF, sel->which);
2037 ccdc_try_format(ccdc, fh, CCDC_PAD_SOURCE_OF, format, sel->which);
2038
2039 return 0;
2040}
2041
2042/*
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002043 * ccdc_get_format - Retrieve the video format on a pad
2044 * @sd : ISP CCDC V4L2 subdevice
2045 * @fh : V4L2 subdev file handle
2046 * @fmt: Format
2047 *
2048 * Return 0 on success or -EINVAL if the pad is invalid or doesn't correspond
2049 * to the format type.
2050 */
2051static int ccdc_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
2052 struct v4l2_subdev_format *fmt)
2053{
2054 struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
2055 struct v4l2_mbus_framefmt *format;
2056
2057 format = __ccdc_get_format(ccdc, fh, fmt->pad, fmt->which);
2058 if (format == NULL)
2059 return -EINVAL;
2060
2061 fmt->format = *format;
2062 return 0;
2063}
2064
2065/*
2066 * ccdc_set_format - Set the video format on a pad
2067 * @sd : ISP CCDC V4L2 subdevice
2068 * @fh : V4L2 subdev file handle
2069 * @fmt: Format
2070 *
2071 * Return 0 on success or -EINVAL if the pad is invalid or doesn't correspond
2072 * to the format type.
2073 */
2074static int ccdc_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
2075 struct v4l2_subdev_format *fmt)
2076{
2077 struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
2078 struct v4l2_mbus_framefmt *format;
Laurent Pincharta64909b2012-04-20 05:47:49 -03002079 struct v4l2_rect *crop;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002080
2081 format = __ccdc_get_format(ccdc, fh, fmt->pad, fmt->which);
2082 if (format == NULL)
2083 return -EINVAL;
2084
2085 ccdc_try_format(ccdc, fh, fmt->pad, &fmt->format, fmt->which);
2086 *format = fmt->format;
2087
2088 /* Propagate the format from sink to source */
2089 if (fmt->pad == CCDC_PAD_SINK) {
Laurent Pincharta64909b2012-04-20 05:47:49 -03002090 /* Reset the crop rectangle. */
2091 crop = __ccdc_get_crop(ccdc, fh, fmt->which);
2092 crop->left = 0;
2093 crop->top = 0;
2094 crop->width = fmt->format.width;
2095 crop->height = fmt->format.height;
2096
2097 ccdc_try_crop(ccdc, &fmt->format, crop);
2098
2099 /* Update the source formats. */
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002100 format = __ccdc_get_format(ccdc, fh, CCDC_PAD_SOURCE_OF,
2101 fmt->which);
2102 *format = fmt->format;
2103 ccdc_try_format(ccdc, fh, CCDC_PAD_SOURCE_OF, format,
2104 fmt->which);
2105
2106 format = __ccdc_get_format(ccdc, fh, CCDC_PAD_SOURCE_VP,
2107 fmt->which);
2108 *format = fmt->format;
2109 ccdc_try_format(ccdc, fh, CCDC_PAD_SOURCE_VP, format,
2110 fmt->which);
2111 }
2112
2113 return 0;
2114}
2115
2116/*
Sakari Ailusa6d7a622012-02-25 20:42:07 -03002117 * Decide whether desired output pixel code can be obtained with
2118 * the lane shifter by shifting the input pixel code.
2119 * @in: input pixelcode to shifter
2120 * @out: output pixelcode from shifter
2121 * @additional_shift: # of bits the sensor's LSB is offset from CAMEXT[0]
2122 *
2123 * return true if the combination is possible
2124 * return false otherwise
2125 */
2126static bool ccdc_is_shiftable(enum v4l2_mbus_pixelcode in,
2127 enum v4l2_mbus_pixelcode out,
2128 unsigned int additional_shift)
2129{
2130 const struct isp_format_info *in_info, *out_info;
2131
2132 if (in == out)
2133 return true;
2134
2135 in_info = omap3isp_video_format_info(in);
2136 out_info = omap3isp_video_format_info(out);
2137
2138 if ((in_info->flavor == 0) || (out_info->flavor == 0))
2139 return false;
2140
2141 if (in_info->flavor != out_info->flavor)
2142 return false;
2143
Laurent Pinchart1697e492011-08-31 11:57:12 -03002144 return in_info->width - out_info->width + additional_shift <= 6;
Sakari Ailusa6d7a622012-02-25 20:42:07 -03002145}
2146
2147static int ccdc_link_validate(struct v4l2_subdev *sd,
2148 struct media_link *link,
2149 struct v4l2_subdev_format *source_fmt,
2150 struct v4l2_subdev_format *sink_fmt)
2151{
2152 struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
2153 unsigned long parallel_shift;
2154
2155 /* Check if the two ends match */
2156 if (source_fmt->format.width != sink_fmt->format.width ||
2157 source_fmt->format.height != sink_fmt->format.height)
2158 return -EPIPE;
2159
2160 /* We've got a parallel sensor here. */
2161 if (ccdc->input == CCDC_INPUT_PARALLEL) {
2162 struct isp_parallel_platform_data *pdata =
2163 &((struct isp_v4l2_subdevs_group *)
2164 media_entity_to_v4l2_subdev(link->source->entity)
2165 ->host_priv)->bus.parallel;
2166 parallel_shift = pdata->data_lane_shift * 2;
2167 } else {
2168 parallel_shift = 0;
2169 }
2170
2171 /* Lane shifter may be used to drop bits on CCDC sink pad */
2172 if (!ccdc_is_shiftable(source_fmt->format.code,
2173 sink_fmt->format.code, parallel_shift))
2174 return -EPIPE;
2175
2176 return 0;
2177}
2178
2179/*
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002180 * ccdc_init_formats - Initialize formats on all pads
2181 * @sd: ISP CCDC V4L2 subdevice
2182 * @fh: V4L2 subdev file handle
2183 *
2184 * Initialize all pad formats with default values. If fh is not NULL, try
2185 * formats are initialized on the file handle. Otherwise active formats are
2186 * initialized on the device.
2187 */
2188static int ccdc_init_formats(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
2189{
2190 struct v4l2_subdev_format format;
2191
2192 memset(&format, 0, sizeof(format));
2193 format.pad = CCDC_PAD_SINK;
2194 format.which = fh ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
2195 format.format.code = V4L2_MBUS_FMT_SGRBG10_1X10;
2196 format.format.width = 4096;
2197 format.format.height = 4096;
2198 ccdc_set_format(sd, fh, &format);
2199
2200 return 0;
2201}
2202
2203/* V4L2 subdev core operations */
2204static const struct v4l2_subdev_core_ops ccdc_v4l2_core_ops = {
2205 .ioctl = ccdc_ioctl,
2206 .subscribe_event = ccdc_subscribe_event,
2207 .unsubscribe_event = ccdc_unsubscribe_event,
2208};
2209
2210/* V4L2 subdev video operations */
2211static const struct v4l2_subdev_video_ops ccdc_v4l2_video_ops = {
2212 .s_stream = ccdc_set_stream,
2213};
2214
2215/* V4L2 subdev pad operations */
2216static const struct v4l2_subdev_pad_ops ccdc_v4l2_pad_ops = {
2217 .enum_mbus_code = ccdc_enum_mbus_code,
2218 .enum_frame_size = ccdc_enum_frame_size,
2219 .get_fmt = ccdc_get_format,
2220 .set_fmt = ccdc_set_format,
Laurent Pincharta64909b2012-04-20 05:47:49 -03002221 .get_selection = ccdc_get_selection,
2222 .set_selection = ccdc_set_selection,
Sakari Ailusa6d7a622012-02-25 20:42:07 -03002223 .link_validate = ccdc_link_validate,
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002224};
2225
2226/* V4L2 subdev operations */
2227static const struct v4l2_subdev_ops ccdc_v4l2_ops = {
2228 .core = &ccdc_v4l2_core_ops,
2229 .video = &ccdc_v4l2_video_ops,
2230 .pad = &ccdc_v4l2_pad_ops,
2231};
2232
2233/* V4L2 subdev internal operations */
2234static const struct v4l2_subdev_internal_ops ccdc_v4l2_internal_ops = {
2235 .open = ccdc_init_formats,
2236};
2237
2238/* -----------------------------------------------------------------------------
2239 * Media entity operations
2240 */
2241
2242/*
2243 * ccdc_link_setup - Setup CCDC connections
2244 * @entity: CCDC media entity
2245 * @local: Pad at the local end of the link
2246 * @remote: Pad at the remote end of the link
2247 * @flags: Link flags
2248 *
2249 * return -EINVAL or zero on success
2250 */
2251static int ccdc_link_setup(struct media_entity *entity,
2252 const struct media_pad *local,
2253 const struct media_pad *remote, u32 flags)
2254{
2255 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
2256 struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
2257 struct isp_device *isp = to_isp_device(ccdc);
2258
2259 switch (local->index | media_entity_type(remote->entity)) {
2260 case CCDC_PAD_SINK | MEDIA_ENT_T_V4L2_SUBDEV:
2261 /* Read from the sensor (parallel interface), CCP2, CSI2a or
2262 * CSI2c.
2263 */
2264 if (!(flags & MEDIA_LNK_FL_ENABLED)) {
2265 ccdc->input = CCDC_INPUT_NONE;
2266 break;
2267 }
2268
2269 if (ccdc->input != CCDC_INPUT_NONE)
2270 return -EBUSY;
2271
2272 if (remote->entity == &isp->isp_ccp2.subdev.entity)
2273 ccdc->input = CCDC_INPUT_CCP2B;
2274 else if (remote->entity == &isp->isp_csi2a.subdev.entity)
2275 ccdc->input = CCDC_INPUT_CSI2A;
2276 else if (remote->entity == &isp->isp_csi2c.subdev.entity)
2277 ccdc->input = CCDC_INPUT_CSI2C;
2278 else
2279 ccdc->input = CCDC_INPUT_PARALLEL;
2280
2281 break;
2282
2283 /*
2284 * The ISP core doesn't support pipelines with multiple video outputs.
2285 * Revisit this when it will be implemented, and return -EBUSY for now.
2286 */
2287
2288 case CCDC_PAD_SOURCE_VP | MEDIA_ENT_T_V4L2_SUBDEV:
2289 /* Write to preview engine, histogram and H3A. When none of
2290 * those links are active, the video port can be disabled.
2291 */
2292 if (flags & MEDIA_LNK_FL_ENABLED) {
2293 if (ccdc->output & ~CCDC_OUTPUT_PREVIEW)
2294 return -EBUSY;
2295 ccdc->output |= CCDC_OUTPUT_PREVIEW;
2296 } else {
2297 ccdc->output &= ~CCDC_OUTPUT_PREVIEW;
2298 }
2299 break;
2300
2301 case CCDC_PAD_SOURCE_OF | MEDIA_ENT_T_DEVNODE:
2302 /* Write to memory */
2303 if (flags & MEDIA_LNK_FL_ENABLED) {
2304 if (ccdc->output & ~CCDC_OUTPUT_MEMORY)
2305 return -EBUSY;
2306 ccdc->output |= CCDC_OUTPUT_MEMORY;
2307 } else {
2308 ccdc->output &= ~CCDC_OUTPUT_MEMORY;
2309 }
2310 break;
2311
2312 case CCDC_PAD_SOURCE_OF | MEDIA_ENT_T_V4L2_SUBDEV:
2313 /* Write to resizer */
2314 if (flags & MEDIA_LNK_FL_ENABLED) {
2315 if (ccdc->output & ~CCDC_OUTPUT_RESIZER)
2316 return -EBUSY;
2317 ccdc->output |= CCDC_OUTPUT_RESIZER;
2318 } else {
2319 ccdc->output &= ~CCDC_OUTPUT_RESIZER;
2320 }
2321 break;
2322
2323 default:
2324 return -EINVAL;
2325 }
2326
2327 return 0;
2328}
2329
2330/* media operations */
2331static const struct media_entity_operations ccdc_media_ops = {
2332 .link_setup = ccdc_link_setup,
Sakari Ailusa6d7a622012-02-25 20:42:07 -03002333 .link_validate = v4l2_subdev_link_validate,
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002334};
2335
Laurent Pinchart39099d02011-09-22 16:59:26 -03002336void omap3isp_ccdc_unregister_entities(struct isp_ccdc_device *ccdc)
2337{
2338 v4l2_device_unregister_subdev(&ccdc->subdev);
2339 omap3isp_video_unregister(&ccdc->video_out);
2340}
2341
2342int omap3isp_ccdc_register_entities(struct isp_ccdc_device *ccdc,
2343 struct v4l2_device *vdev)
2344{
2345 int ret;
2346
2347 /* Register the subdev and video node. */
2348 ret = v4l2_device_register_subdev(vdev, &ccdc->subdev);
2349 if (ret < 0)
2350 goto error;
2351
2352 ret = omap3isp_video_register(&ccdc->video_out, vdev);
2353 if (ret < 0)
2354 goto error;
2355
2356 return 0;
2357
2358error:
2359 omap3isp_ccdc_unregister_entities(ccdc);
2360 return ret;
2361}
2362
2363/* -----------------------------------------------------------------------------
2364 * ISP CCDC initialisation and cleanup
2365 */
2366
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002367/*
2368 * ccdc_init_entities - Initialize V4L2 subdev and media entity
2369 * @ccdc: ISP CCDC module
2370 *
2371 * Return 0 on success and a negative error code on failure.
2372 */
2373static int ccdc_init_entities(struct isp_ccdc_device *ccdc)
2374{
2375 struct v4l2_subdev *sd = &ccdc->subdev;
2376 struct media_pad *pads = ccdc->pads;
2377 struct media_entity *me = &sd->entity;
2378 int ret;
2379
2380 ccdc->input = CCDC_INPUT_NONE;
2381
2382 v4l2_subdev_init(sd, &ccdc_v4l2_ops);
2383 sd->internal_ops = &ccdc_v4l2_internal_ops;
2384 strlcpy(sd->name, "OMAP3 ISP CCDC", sizeof(sd->name));
2385 sd->grp_id = 1 << 16; /* group ID for isp subdevs */
2386 v4l2_set_subdevdata(sd, ccdc);
2387 sd->flags |= V4L2_SUBDEV_FL_HAS_EVENTS | V4L2_SUBDEV_FL_HAS_DEVNODE;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002388
2389 pads[CCDC_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
2390 pads[CCDC_PAD_SOURCE_VP].flags = MEDIA_PAD_FL_SOURCE;
2391 pads[CCDC_PAD_SOURCE_OF].flags = MEDIA_PAD_FL_SOURCE;
2392
2393 me->ops = &ccdc_media_ops;
2394 ret = media_entity_init(me, CCDC_PADS_NUM, pads, 0);
2395 if (ret < 0)
2396 return ret;
2397
2398 ccdc_init_formats(sd, NULL);
2399
2400 ccdc->video_out.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2401 ccdc->video_out.ops = &ccdc_video_ops;
2402 ccdc->video_out.isp = to_isp_device(ccdc);
2403 ccdc->video_out.capture_mem = PAGE_ALIGN(4096 * 4096) * 3;
2404 ccdc->video_out.bpl_alignment = 32;
2405
2406 ret = omap3isp_video_init(&ccdc->video_out, "CCDC");
2407 if (ret < 0)
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03002408 goto error_video;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002409
2410 /* Connect the CCDC subdev to the video node. */
2411 ret = media_entity_create_link(&ccdc->subdev.entity, CCDC_PAD_SOURCE_OF,
2412 &ccdc->video_out.video.entity, 0, 0);
2413 if (ret < 0)
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03002414 goto error_link;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002415
2416 return 0;
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03002417
2418error_link:
2419 omap3isp_video_cleanup(&ccdc->video_out);
2420error_video:
2421 media_entity_cleanup(me);
2422 return ret;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002423}
2424
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002425/*
2426 * omap3isp_ccdc_init - CCDC module initialization.
2427 * @dev: Device pointer specific to the OMAP3 ISP.
2428 *
2429 * TODO: Get the initialisation values from platform data.
2430 *
2431 * Return 0 on success or a negative error code otherwise.
2432 */
2433int omap3isp_ccdc_init(struct isp_device *isp)
2434{
2435 struct isp_ccdc_device *ccdc = &isp->isp_ccdc;
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03002436 int ret;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002437
2438 spin_lock_init(&ccdc->lock);
2439 init_waitqueue_head(&ccdc->wait);
2440 mutex_init(&ccdc->ioctl_lock);
2441
2442 ccdc->stopping = CCDC_STOP_NOT_REQUESTED;
2443
2444 INIT_WORK(&ccdc->lsc.table_work, ccdc_lsc_free_table_work);
2445 ccdc->lsc.state = LSC_STATE_STOPPED;
2446 INIT_LIST_HEAD(&ccdc->lsc.free_queue);
2447 spin_lock_init(&ccdc->lsc.req_lock);
2448
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002449 ccdc->syncif.datsz = 0;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002450
2451 ccdc->clamp.oblen = 0;
2452 ccdc->clamp.dcsubval = 0;
2453
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002454 ccdc->update = OMAP3ISP_CCDC_BLCLAMP;
2455 ccdc_apply_controls(ccdc);
2456
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03002457 ret = ccdc_init_entities(ccdc);
2458 if (ret < 0) {
2459 mutex_destroy(&ccdc->ioctl_lock);
2460 return ret;
2461 }
2462
2463 return 0;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002464}
2465
2466/*
2467 * omap3isp_ccdc_cleanup - CCDC module cleanup.
2468 * @dev: Device pointer specific to the OMAP3 ISP.
2469 */
2470void omap3isp_ccdc_cleanup(struct isp_device *isp)
2471{
2472 struct isp_ccdc_device *ccdc = &isp->isp_ccdc;
2473
Laurent Pinchart63b4ca22011-09-22 16:54:34 -03002474 omap3isp_video_cleanup(&ccdc->video_out);
2475 media_entity_cleanup(&ccdc->subdev.entity);
2476
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002477 /* Free LSC requests. As the CCDC is stopped there's no active request,
2478 * so only the pending request and the free queue need to be handled.
2479 */
2480 ccdc_lsc_free_request(ccdc, ccdc->lsc.request);
2481 cancel_work_sync(&ccdc->lsc.table_work);
2482 ccdc_lsc_free_queue(ccdc, &ccdc->lsc.free_queue);
2483
2484 if (ccdc->fpc.fpcaddr != 0)
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02002485 omap_iommu_vfree(isp->domain, isp->dev, ccdc->fpc.fpcaddr);
Laurent Pincharted33ac82011-09-22 17:09:26 -03002486
2487 mutex_destroy(&ccdc->ioctl_lock);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002488}