]>
Commit | Line | Data |
---|---|---|
0d3244d6 MD |
1 | /* |
2 | * V4L2 Driver for SuperH Mobile CEU interface | |
3 | * | |
4 | * Copyright (C) 2008 Magnus Damm | |
5 | * | |
6 | * Based on V4L2 Driver for PXA camera host - "pxa_camera.c", | |
7 | * | |
8 | * Copyright (C) 2006, Sascha Hauer, Pengutronix | |
9 | * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de> | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License as published by | |
13 | * the Free Software Foundation; either version 2 of the License, or | |
14 | * (at your option) any later version. | |
15 | */ | |
16 | ||
17 | #include <linux/init.h> | |
18 | #include <linux/module.h> | |
19 | #include <linux/io.h> | |
20 | #include <linux/delay.h> | |
21 | #include <linux/dma-mapping.h> | |
22 | #include <linux/errno.h> | |
23 | #include <linux/fs.h> | |
24 | #include <linux/interrupt.h> | |
25 | #include <linux/kernel.h> | |
26 | #include <linux/mm.h> | |
27 | #include <linux/moduleparam.h> | |
28 | #include <linux/time.h> | |
29 | #include <linux/version.h> | |
30 | #include <linux/device.h> | |
31 | #include <linux/platform_device.h> | |
0d3244d6 | 32 | #include <linux/videodev2.h> |
a42b6dd6 | 33 | #include <linux/clk.h> |
0d3244d6 MD |
34 | |
35 | #include <media/v4l2-common.h> | |
36 | #include <media/v4l2-dev.h> | |
37 | #include <media/soc_camera.h> | |
38 | #include <media/sh_mobile_ceu.h> | |
39 | #include <media/videobuf-dma-contig.h> | |
40 | ||
41 | /* register offsets for sh7722 / sh7723 */ | |
42 | ||
dd54203b MD |
43 | #define CAPSR 0x00 /* Capture start register */ |
44 | #define CAPCR 0x04 /* Capture control register */ | |
45 | #define CAMCR 0x08 /* Capture interface control register */ | |
46 | #define CMCYR 0x0c /* Capture interface cycle register */ | |
47 | #define CAMOR 0x10 /* Capture interface offset register */ | |
48 | #define CAPWR 0x14 /* Capture interface width register */ | |
49 | #define CAIFR 0x18 /* Capture interface input format register */ | |
50 | #define CSTCR 0x20 /* Camera strobe control register (<= sh7722) */ | |
51 | #define CSECR 0x24 /* Camera strobe emission count register (<= sh7722) */ | |
52 | #define CRCNTR 0x28 /* CEU register control register */ | |
53 | #define CRCMPR 0x2c /* CEU register forcible control register */ | |
54 | #define CFLCR 0x30 /* Capture filter control register */ | |
55 | #define CFSZR 0x34 /* Capture filter size clip register */ | |
56 | #define CDWDR 0x38 /* Capture destination width register */ | |
57 | #define CDAYR 0x3c /* Capture data address Y register */ | |
58 | #define CDACR 0x40 /* Capture data address C register */ | |
59 | #define CDBYR 0x44 /* Capture data bottom-field address Y register */ | |
60 | #define CDBCR 0x48 /* Capture data bottom-field address C register */ | |
61 | #define CBDSR 0x4c /* Capture bundle destination size register */ | |
62 | #define CFWCR 0x5c /* Firewall operation control register */ | |
63 | #define CLFCR 0x60 /* Capture low-pass filter control register */ | |
64 | #define CDOCR 0x64 /* Capture data output control register */ | |
65 | #define CDDCR 0x68 /* Capture data complexity level register */ | |
66 | #define CDDAR 0x6c /* Capture data complexity level address register */ | |
67 | #define CEIER 0x70 /* Capture event interrupt enable register */ | |
68 | #define CETCR 0x74 /* Capture event flag clear register */ | |
69 | #define CSTSR 0x7c /* Capture status register */ | |
70 | #define CSRTR 0x80 /* Capture software reset register */ | |
71 | #define CDSSR 0x84 /* Capture data size register */ | |
72 | #define CDAYR2 0x90 /* Capture data address Y register 2 */ | |
73 | #define CDACR2 0x94 /* Capture data address C register 2 */ | |
74 | #define CDBYR2 0x98 /* Capture data bottom-field address Y register 2 */ | |
75 | #define CDBCR2 0x9c /* Capture data bottom-field address C register 2 */ | |
0d3244d6 | 76 | |
0d3244d6 MD |
77 | /* per video frame buffer */ |
78 | struct sh_mobile_ceu_buffer { | |
79 | struct videobuf_buffer vb; /* v4l buffer must be first */ | |
80 | const struct soc_camera_data_format *fmt; | |
81 | }; | |
82 | ||
83 | struct sh_mobile_ceu_dev { | |
84 | struct device *dev; | |
85 | struct soc_camera_host ici; | |
86 | struct soc_camera_device *icd; | |
87 | ||
88 | unsigned int irq; | |
89 | void __iomem *base; | |
a42b6dd6 | 90 | struct clk *clk; |
0d3244d6 MD |
91 | unsigned long video_limit; |
92 | ||
c60f2b5c | 93 | /* lock used to protect videobuf */ |
0d3244d6 MD |
94 | spinlock_t lock; |
95 | struct list_head capture; | |
96 | struct videobuf_buffer *active; | |
e802967c | 97 | int is_interlaced; |
0d3244d6 MD |
98 | |
99 | struct sh_mobile_ceu_info *pdata; | |
8be65dc5 MD |
100 | |
101 | const struct soc_camera_data_format *camera_fmt; | |
0d3244d6 MD |
102 | }; |
103 | ||
c354b400 KM |
104 | static unsigned long make_bus_param(struct sh_mobile_ceu_dev *pcdev) |
105 | { | |
106 | unsigned long flags; | |
107 | ||
108 | flags = SOCAM_MASTER | | |
109 | SOCAM_PCLK_SAMPLE_RISING | | |
110 | SOCAM_HSYNC_ACTIVE_HIGH | | |
111 | SOCAM_HSYNC_ACTIVE_LOW | | |
112 | SOCAM_VSYNC_ACTIVE_HIGH | | |
113 | SOCAM_VSYNC_ACTIVE_LOW | | |
114 | SOCAM_DATA_ACTIVE_HIGH; | |
115 | ||
116 | if (pcdev->pdata->flags & SH_CEU_FLAG_USE_8BIT_BUS) | |
117 | flags |= SOCAM_DATAWIDTH_8; | |
118 | ||
119 | if (pcdev->pdata->flags & SH_CEU_FLAG_USE_16BIT_BUS) | |
120 | flags |= SOCAM_DATAWIDTH_16; | |
121 | ||
122 | if (flags & SOCAM_DATAWIDTH_MASK) | |
123 | return flags; | |
124 | ||
125 | return 0; | |
126 | } | |
127 | ||
0d3244d6 | 128 | static void ceu_write(struct sh_mobile_ceu_dev *priv, |
7a1a8164 | 129 | unsigned long reg_offs, u32 data) |
0d3244d6 MD |
130 | { |
131 | iowrite32(data, priv->base + reg_offs); | |
132 | } | |
133 | ||
7a1a8164 | 134 | static u32 ceu_read(struct sh_mobile_ceu_dev *priv, unsigned long reg_offs) |
0d3244d6 MD |
135 | { |
136 | return ioread32(priv->base + reg_offs); | |
137 | } | |
138 | ||
139 | /* | |
140 | * Videobuf operations | |
141 | */ | |
142 | static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue *vq, | |
143 | unsigned int *count, | |
144 | unsigned int *size) | |
145 | { | |
146 | struct soc_camera_device *icd = vq->priv_data; | |
147 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
148 | struct sh_mobile_ceu_dev *pcdev = ici->priv; | |
149 | int bytes_per_pixel = (icd->current_fmt->depth + 7) >> 3; | |
150 | ||
151 | *size = PAGE_ALIGN(icd->width * icd->height * bytes_per_pixel); | |
152 | ||
153 | if (0 == *count) | |
154 | *count = 2; | |
155 | ||
156 | if (pcdev->video_limit) { | |
157 | while (*size * *count > pcdev->video_limit) | |
158 | (*count)--; | |
159 | } | |
160 | ||
161 | dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size); | |
162 | ||
163 | return 0; | |
164 | } | |
165 | ||
166 | static void free_buffer(struct videobuf_queue *vq, | |
167 | struct sh_mobile_ceu_buffer *buf) | |
168 | { | |
169 | struct soc_camera_device *icd = vq->priv_data; | |
170 | ||
86751b01 | 171 | dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__, |
0d3244d6 MD |
172 | &buf->vb, buf->vb.baddr, buf->vb.bsize); |
173 | ||
174 | if (in_interrupt()) | |
175 | BUG(); | |
176 | ||
97215cbd | 177 | videobuf_waiton(&buf->vb, 0, 0); |
0d3244d6 MD |
178 | videobuf_dma_contig_free(vq, &buf->vb); |
179 | dev_dbg(&icd->dev, "%s freed\n", __func__); | |
180 | buf->vb.state = VIDEOBUF_NEEDS_INIT; | |
181 | } | |
182 | ||
7a1a8164 MD |
183 | #define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */ |
184 | #define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */ | |
185 | #define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */ | |
186 | #define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */ | |
187 | ||
188 | ||
0d3244d6 MD |
189 | static void sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev) |
190 | { | |
8be65dc5 | 191 | struct soc_camera_device *icd = pcdev->icd; |
ccab8a29 | 192 | dma_addr_t phys_addr_top, phys_addr_bottom; |
7a1a8164 MD |
193 | |
194 | /* The hardware is _very_ picky about this sequence. Especially | |
195 | * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge | |
196 | * several not-so-well documented interrupt sources in CETCR. | |
197 | */ | |
198 | ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_CPEIE); | |
199 | ceu_write(pcdev, CETCR, ~ceu_read(pcdev, CETCR) & CEU_CETCR_MAGIC); | |
200 | ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_CPEIE); | |
201 | ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP); | |
202 | ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW); | |
0d3244d6 | 203 | |
8be65dc5 MD |
204 | if (!pcdev->active) |
205 | return; | |
206 | ||
ccab8a29 KM |
207 | phys_addr_top = videobuf_to_dma_contig(pcdev->active); |
208 | ceu_write(pcdev, CDAYR, phys_addr_top); | |
e802967c | 209 | if (pcdev->is_interlaced) { |
ccab8a29 KM |
210 | phys_addr_bottom = phys_addr_top + icd->width; |
211 | ceu_write(pcdev, CDBYR, phys_addr_bottom); | |
212 | } | |
8be65dc5 MD |
213 | |
214 | switch (icd->current_fmt->fourcc) { | |
215 | case V4L2_PIX_FMT_NV12: | |
216 | case V4L2_PIX_FMT_NV21: | |
9e4a56d2 MD |
217 | case V4L2_PIX_FMT_NV16: |
218 | case V4L2_PIX_FMT_NV61: | |
ccab8a29 KM |
219 | phys_addr_top += icd->width * icd->height; |
220 | ceu_write(pcdev, CDACR, phys_addr_top); | |
e802967c | 221 | if (pcdev->is_interlaced) { |
ccab8a29 KM |
222 | phys_addr_bottom = phys_addr_top + icd->width; |
223 | ceu_write(pcdev, CDBCR, phys_addr_bottom); | |
224 | } | |
0d3244d6 | 225 | } |
8be65dc5 MD |
226 | |
227 | pcdev->active->state = VIDEOBUF_ACTIVE; | |
228 | ceu_write(pcdev, CAPSR, 0x1); /* start capture */ | |
0d3244d6 MD |
229 | } |
230 | ||
231 | static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue *vq, | |
232 | struct videobuf_buffer *vb, | |
233 | enum v4l2_field field) | |
234 | { | |
235 | struct soc_camera_device *icd = vq->priv_data; | |
236 | struct sh_mobile_ceu_buffer *buf; | |
237 | int ret; | |
238 | ||
239 | buf = container_of(vb, struct sh_mobile_ceu_buffer, vb); | |
240 | ||
86751b01 | 241 | dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__, |
0d3244d6 MD |
242 | vb, vb->baddr, vb->bsize); |
243 | ||
244 | /* Added list head initialization on alloc */ | |
245 | WARN_ON(!list_empty(&vb->queue)); | |
246 | ||
247 | #ifdef DEBUG | |
248 | /* This can be useful if you want to see if we actually fill | |
249 | * the buffer with something */ | |
250 | memset((void *)vb->baddr, 0xaa, vb->bsize); | |
251 | #endif | |
252 | ||
253 | BUG_ON(NULL == icd->current_fmt); | |
254 | ||
255 | if (buf->fmt != icd->current_fmt || | |
256 | vb->width != icd->width || | |
257 | vb->height != icd->height || | |
258 | vb->field != field) { | |
259 | buf->fmt = icd->current_fmt; | |
260 | vb->width = icd->width; | |
261 | vb->height = icd->height; | |
262 | vb->field = field; | |
263 | vb->state = VIDEOBUF_NEEDS_INIT; | |
264 | } | |
265 | ||
266 | vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3); | |
267 | if (0 != vb->baddr && vb->bsize < vb->size) { | |
268 | ret = -EINVAL; | |
269 | goto out; | |
270 | } | |
271 | ||
272 | if (vb->state == VIDEOBUF_NEEDS_INIT) { | |
273 | ret = videobuf_iolock(vq, vb, NULL); | |
274 | if (ret) | |
275 | goto fail; | |
276 | vb->state = VIDEOBUF_PREPARED; | |
277 | } | |
278 | ||
279 | return 0; | |
280 | fail: | |
281 | free_buffer(vq, buf); | |
282 | out: | |
283 | return ret; | |
284 | } | |
285 | ||
286 | static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue *vq, | |
287 | struct videobuf_buffer *vb) | |
288 | { | |
289 | struct soc_camera_device *icd = vq->priv_data; | |
290 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
291 | struct sh_mobile_ceu_dev *pcdev = ici->priv; | |
292 | unsigned long flags; | |
293 | ||
86751b01 | 294 | dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__, |
0d3244d6 MD |
295 | vb, vb->baddr, vb->bsize); |
296 | ||
51354cc3 | 297 | vb->state = VIDEOBUF_QUEUED; |
0d3244d6 MD |
298 | spin_lock_irqsave(&pcdev->lock, flags); |
299 | list_add_tail(&vb->queue, &pcdev->capture); | |
300 | ||
301 | if (!pcdev->active) { | |
302 | pcdev->active = vb; | |
303 | sh_mobile_ceu_capture(pcdev); | |
304 | } | |
305 | ||
306 | spin_unlock_irqrestore(&pcdev->lock, flags); | |
307 | } | |
308 | ||
309 | static void sh_mobile_ceu_videobuf_release(struct videobuf_queue *vq, | |
310 | struct videobuf_buffer *vb) | |
311 | { | |
312 | free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb)); | |
313 | } | |
314 | ||
315 | static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops = { | |
316 | .buf_setup = sh_mobile_ceu_videobuf_setup, | |
317 | .buf_prepare = sh_mobile_ceu_videobuf_prepare, | |
318 | .buf_queue = sh_mobile_ceu_videobuf_queue, | |
319 | .buf_release = sh_mobile_ceu_videobuf_release, | |
320 | }; | |
321 | ||
322 | static irqreturn_t sh_mobile_ceu_irq(int irq, void *data) | |
323 | { | |
324 | struct sh_mobile_ceu_dev *pcdev = data; | |
325 | struct videobuf_buffer *vb; | |
326 | unsigned long flags; | |
327 | ||
328 | spin_lock_irqsave(&pcdev->lock, flags); | |
329 | ||
330 | vb = pcdev->active; | |
331 | list_del_init(&vb->queue); | |
332 | ||
333 | if (!list_empty(&pcdev->capture)) | |
334 | pcdev->active = list_entry(pcdev->capture.next, | |
335 | struct videobuf_buffer, queue); | |
336 | else | |
337 | pcdev->active = NULL; | |
338 | ||
339 | sh_mobile_ceu_capture(pcdev); | |
340 | ||
341 | vb->state = VIDEOBUF_DONE; | |
342 | do_gettimeofday(&vb->ts); | |
343 | vb->field_count++; | |
344 | wake_up(&vb->done); | |
345 | spin_unlock_irqrestore(&pcdev->lock, flags); | |
346 | ||
347 | return IRQ_HANDLED; | |
348 | } | |
349 | ||
1c3bb743 | 350 | /* Called with .video_lock held */ |
0d3244d6 MD |
351 | static int sh_mobile_ceu_add_device(struct soc_camera_device *icd) |
352 | { | |
353 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
354 | struct sh_mobile_ceu_dev *pcdev = ici->priv; | |
355 | int ret = -EBUSY; | |
356 | ||
0d3244d6 MD |
357 | if (pcdev->icd) |
358 | goto err; | |
359 | ||
360 | dev_info(&icd->dev, | |
361 | "SuperH Mobile CEU driver attached to camera %d\n", | |
362 | icd->devnum); | |
363 | ||
0d3244d6 MD |
364 | ret = icd->ops->init(icd); |
365 | if (ret) | |
366 | goto err; | |
367 | ||
a42b6dd6 MD |
368 | clk_enable(pcdev->clk); |
369 | ||
0d3244d6 MD |
370 | ceu_write(pcdev, CAPSR, 1 << 16); /* reset */ |
371 | while (ceu_read(pcdev, CSTSR) & 1) | |
372 | msleep(1); | |
373 | ||
374 | pcdev->icd = icd; | |
375 | err: | |
0d3244d6 MD |
376 | return ret; |
377 | } | |
378 | ||
1c3bb743 | 379 | /* Called with .video_lock held */ |
0d3244d6 MD |
380 | static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd) |
381 | { | |
382 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
383 | struct sh_mobile_ceu_dev *pcdev = ici->priv; | |
51354cc3 | 384 | unsigned long flags; |
0d3244d6 MD |
385 | |
386 | BUG_ON(icd != pcdev->icd); | |
387 | ||
388 | /* disable capture, disable interrupts */ | |
389 | ceu_write(pcdev, CEIER, 0); | |
390 | ceu_write(pcdev, CAPSR, 1 << 16); /* reset */ | |
51354cc3 MD |
391 | |
392 | /* make sure active buffer is canceled */ | |
393 | spin_lock_irqsave(&pcdev->lock, flags); | |
394 | if (pcdev->active) { | |
395 | list_del(&pcdev->active->queue); | |
396 | pcdev->active->state = VIDEOBUF_ERROR; | |
397 | wake_up_all(&pcdev->active->done); | |
398 | pcdev->active = NULL; | |
399 | } | |
400 | spin_unlock_irqrestore(&pcdev->lock, flags); | |
401 | ||
a42b6dd6 MD |
402 | clk_disable(pcdev->clk); |
403 | ||
0d3244d6 | 404 | icd->ops->release(icd); |
0d3244d6 MD |
405 | |
406 | dev_info(&icd->dev, | |
407 | "SuperH Mobile CEU driver detached from camera %d\n", | |
408 | icd->devnum); | |
409 | ||
410 | pcdev->icd = NULL; | |
411 | } | |
412 | ||
413 | static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd, | |
414 | __u32 pixfmt) | |
415 | { | |
416 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
417 | struct sh_mobile_ceu_dev *pcdev = ici->priv; | |
ccab8a29 | 418 | int ret, buswidth, width, height, cfszr_width, cdwdr_width; |
0d3244d6 | 419 | unsigned long camera_flags, common_flags, value; |
8be65dc5 | 420 | int yuv_mode, yuv_lineskip; |
0d3244d6 MD |
421 | |
422 | camera_flags = icd->ops->query_bus_param(icd); | |
423 | common_flags = soc_camera_bus_param_compatible(camera_flags, | |
c354b400 | 424 | make_bus_param(pcdev)); |
0d3244d6 MD |
425 | if (!common_flags) |
426 | return -EINVAL; | |
427 | ||
428 | ret = icd->ops->set_bus_param(icd, common_flags); | |
429 | if (ret < 0) | |
430 | return ret; | |
431 | ||
432 | switch (common_flags & SOCAM_DATAWIDTH_MASK) { | |
433 | case SOCAM_DATAWIDTH_8: | |
434 | buswidth = 8; | |
435 | break; | |
436 | case SOCAM_DATAWIDTH_16: | |
437 | buswidth = 16; | |
438 | break; | |
439 | default: | |
440 | return -EINVAL; | |
441 | } | |
442 | ||
443 | ceu_write(pcdev, CRCNTR, 0); | |
444 | ceu_write(pcdev, CRCMPR, 0); | |
445 | ||
8be65dc5 MD |
446 | value = 0x00000010; /* data fetch by default */ |
447 | yuv_mode = yuv_lineskip = 0; | |
448 | ||
449 | switch (icd->current_fmt->fourcc) { | |
450 | case V4L2_PIX_FMT_NV12: | |
451 | case V4L2_PIX_FMT_NV21: | |
452 | yuv_lineskip = 1; /* skip for NV12/21, no skip for NV16/61 */ | |
9e4a56d2 MD |
453 | /* fall-through */ |
454 | case V4L2_PIX_FMT_NV16: | |
455 | case V4L2_PIX_FMT_NV61: | |
8be65dc5 MD |
456 | yuv_mode = 1; |
457 | switch (pcdev->camera_fmt->fourcc) { | |
458 | case V4L2_PIX_FMT_UYVY: | |
459 | value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */ | |
460 | break; | |
461 | case V4L2_PIX_FMT_VYUY: | |
462 | value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */ | |
463 | break; | |
464 | case V4L2_PIX_FMT_YUYV: | |
465 | value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */ | |
466 | break; | |
467 | case V4L2_PIX_FMT_YVYU: | |
468 | value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */ | |
469 | break; | |
470 | default: | |
471 | BUG(); | |
472 | } | |
473 | } | |
474 | ||
7a1a8164 MD |
475 | if (icd->current_fmt->fourcc == V4L2_PIX_FMT_NV21 || |
476 | icd->current_fmt->fourcc == V4L2_PIX_FMT_NV61) | |
9e4a56d2 | 477 | value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */ |
8be65dc5 | 478 | |
7a1a8164 MD |
479 | value |= common_flags & SOCAM_VSYNC_ACTIVE_LOW ? 1 << 1 : 0; |
480 | value |= common_flags & SOCAM_HSYNC_ACTIVE_LOW ? 1 << 0 : 0; | |
481 | value |= buswidth == 16 ? 1 << 12 : 0; | |
0d3244d6 MD |
482 | ceu_write(pcdev, CAMCR, value); |
483 | ||
484 | ceu_write(pcdev, CAPCR, 0x00300000); | |
e802967c | 485 | ceu_write(pcdev, CAIFR, pcdev->is_interlaced ? 0x101 : 0); |
0d3244d6 MD |
486 | |
487 | mdelay(1); | |
488 | ||
8be65dc5 MD |
489 | if (yuv_mode) { |
490 | width = icd->width * 2; | |
7a1a8164 | 491 | width = buswidth == 16 ? width / 2 : width; |
8be65dc5 MD |
492 | cfszr_width = cdwdr_width = icd->width; |
493 | } else { | |
494 | width = icd->width * ((icd->current_fmt->depth + 7) >> 3); | |
7a1a8164 MD |
495 | width = buswidth == 16 ? width / 2 : width; |
496 | cfszr_width = buswidth == 8 ? width / 2 : width; | |
497 | cdwdr_width = buswidth == 16 ? width * 2 : width; | |
8be65dc5 | 498 | } |
0d3244d6 | 499 | |
ccab8a29 | 500 | height = icd->height; |
e802967c | 501 | if (pcdev->is_interlaced) { |
ccab8a29 KM |
502 | height /= 2; |
503 | cdwdr_width *= 2; | |
504 | } | |
505 | ||
0d3244d6 | 506 | ceu_write(pcdev, CAMOR, 0); |
ccab8a29 | 507 | ceu_write(pcdev, CAPWR, (height << 16) | width); |
8be65dc5 | 508 | ceu_write(pcdev, CFLCR, 0); /* no scaling */ |
ccab8a29 | 509 | ceu_write(pcdev, CFSZR, (height << 16) | cfszr_width); |
8be65dc5 | 510 | ceu_write(pcdev, CLFCR, 0); /* no lowpass filter */ |
dd54203b MD |
511 | |
512 | /* A few words about byte order (observed in Big Endian mode) | |
513 | * | |
514 | * In data fetch mode bytes are received in chunks of 8 bytes. | |
515 | * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first) | |
516 | * | |
517 | * The data is however by default written to memory in reverse order: | |
518 | * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte) | |
519 | * | |
520 | * The lowest three bits of CDOCR allows us to do swapping, | |
2c0a072e MD |
521 | * using 7 we swap the data bytes to match the incoming order: |
522 | * D0, D1, D2, D3, D4, D5, D6, D7 | |
dd54203b | 523 | */ |
8be65dc5 MD |
524 | value = 0x00000017; |
525 | if (yuv_lineskip) | |
526 | value &= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */ | |
527 | ||
528 | ceu_write(pcdev, CDOCR, value); | |
0d3244d6 MD |
529 | |
530 | ceu_write(pcdev, CDWDR, cdwdr_width); | |
531 | ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */ | |
532 | ||
533 | /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */ | |
0d3244d6 MD |
534 | return 0; |
535 | } | |
536 | ||
9414de39 | 537 | static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd) |
0d3244d6 MD |
538 | { |
539 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
540 | struct sh_mobile_ceu_dev *pcdev = ici->priv; | |
541 | unsigned long camera_flags, common_flags; | |
542 | ||
543 | camera_flags = icd->ops->query_bus_param(icd); | |
544 | common_flags = soc_camera_bus_param_compatible(camera_flags, | |
c354b400 | 545 | make_bus_param(pcdev)); |
0d3244d6 MD |
546 | if (!common_flags) |
547 | return -EINVAL; | |
548 | ||
549 | return 0; | |
550 | } | |
551 | ||
8be65dc5 MD |
552 | static const struct soc_camera_data_format sh_mobile_ceu_formats[] = { |
553 | { | |
554 | .name = "NV12", | |
555 | .depth = 12, | |
556 | .fourcc = V4L2_PIX_FMT_NV12, | |
557 | .colorspace = V4L2_COLORSPACE_JPEG, | |
558 | }, | |
559 | { | |
560 | .name = "NV21", | |
561 | .depth = 12, | |
562 | .fourcc = V4L2_PIX_FMT_NV21, | |
563 | .colorspace = V4L2_COLORSPACE_JPEG, | |
564 | }, | |
9e4a56d2 MD |
565 | { |
566 | .name = "NV16", | |
567 | .depth = 16, | |
568 | .fourcc = V4L2_PIX_FMT_NV16, | |
569 | .colorspace = V4L2_COLORSPACE_JPEG, | |
570 | }, | |
571 | { | |
572 | .name = "NV61", | |
573 | .depth = 16, | |
574 | .fourcc = V4L2_PIX_FMT_NV61, | |
575 | .colorspace = V4L2_COLORSPACE_JPEG, | |
576 | }, | |
8be65dc5 MD |
577 | }; |
578 | ||
9414de39 MD |
579 | static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, int idx, |
580 | struct soc_camera_format_xlate *xlate) | |
581 | { | |
582 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
8be65dc5 | 583 | int ret, k, n; |
9414de39 MD |
584 | int formats = 0; |
585 | ||
586 | ret = sh_mobile_ceu_try_bus_param(icd); | |
587 | if (ret < 0) | |
588 | return 0; | |
589 | ||
c8329acc GL |
590 | /* Beginning of a pass */ |
591 | if (!idx) | |
592 | icd->host_priv = NULL; | |
593 | ||
9414de39 | 594 | switch (icd->formats[idx].fourcc) { |
8be65dc5 MD |
595 | case V4L2_PIX_FMT_UYVY: |
596 | case V4L2_PIX_FMT_VYUY: | |
597 | case V4L2_PIX_FMT_YUYV: | |
598 | case V4L2_PIX_FMT_YVYU: | |
c8329acc GL |
599 | if (icd->host_priv) |
600 | goto add_single_format; | |
601 | ||
602 | /* | |
603 | * Our case is simple so far: for any of the above four camera | |
604 | * formats we add all our four synthesized NV* formats, so, | |
605 | * just marking the device with a single flag suffices. If | |
606 | * the format generation rules are more complex, you would have | |
607 | * to actually hang your already added / counted formats onto | |
608 | * the host_priv pointer and check whether the format you're | |
609 | * going to add now is already there. | |
610 | */ | |
611 | icd->host_priv = (void *)sh_mobile_ceu_formats; | |
612 | ||
8be65dc5 MD |
613 | n = ARRAY_SIZE(sh_mobile_ceu_formats); |
614 | formats += n; | |
615 | for (k = 0; xlate && k < n; k++) { | |
616 | xlate->host_fmt = &sh_mobile_ceu_formats[k]; | |
617 | xlate->cam_fmt = icd->formats + idx; | |
618 | xlate->buswidth = icd->formats[idx].depth; | |
619 | xlate++; | |
620 | dev_dbg(&ici->dev, "Providing format %s using %s\n", | |
621 | sh_mobile_ceu_formats[k].name, | |
622 | icd->formats[idx].name); | |
623 | } | |
9414de39 | 624 | default: |
c8329acc | 625 | add_single_format: |
9414de39 MD |
626 | /* Generic pass-through */ |
627 | formats++; | |
628 | if (xlate) { | |
629 | xlate->host_fmt = icd->formats + idx; | |
630 | xlate->cam_fmt = icd->formats + idx; | |
631 | xlate->buswidth = icd->formats[idx].depth; | |
632 | xlate++; | |
633 | dev_dbg(&ici->dev, | |
634 | "Providing format %s in pass-through mode\n", | |
635 | icd->formats[idx].name); | |
636 | } | |
637 | } | |
638 | ||
639 | return formats; | |
640 | } | |
641 | ||
09e231b3 GL |
642 | static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd, |
643 | struct v4l2_rect *rect) | |
644 | { | |
645 | return icd->ops->set_crop(icd, rect); | |
646 | } | |
647 | ||
d8fac217 | 648 | static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd, |
09e231b3 | 649 | struct v4l2_format *f) |
0d3244d6 | 650 | { |
9414de39 | 651 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
8be65dc5 | 652 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
09e231b3 | 653 | __u32 pixfmt = f->fmt.pix.pixelformat; |
9414de39 | 654 | const struct soc_camera_format_xlate *xlate; |
09e231b3 | 655 | struct v4l2_format cam_f = *f; |
25c4d74e GL |
656 | int ret; |
657 | ||
9414de39 MD |
658 | xlate = soc_camera_xlate_by_fourcc(icd, pixfmt); |
659 | if (!xlate) { | |
660 | dev_warn(&ici->dev, "Format %x not found\n", pixfmt); | |
661 | return -EINVAL; | |
25c4d74e GL |
662 | } |
663 | ||
09e231b3 GL |
664 | cam_f.fmt.pix.pixelformat = xlate->cam_fmt->fourcc; |
665 | ret = icd->ops->set_fmt(icd, &cam_f); | |
9414de39 | 666 | |
0ad675eb | 667 | if (!ret) { |
9414de39 MD |
668 | icd->buswidth = xlate->buswidth; |
669 | icd->current_fmt = xlate->host_fmt; | |
8be65dc5 | 670 | pcdev->camera_fmt = xlate->cam_fmt; |
9414de39 | 671 | } |
25c4d74e GL |
672 | |
673 | return ret; | |
0d3244d6 MD |
674 | } |
675 | ||
d8fac217 GL |
676 | static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd, |
677 | struct v4l2_format *f) | |
0d3244d6 | 678 | { |
9414de39 | 679 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
ccab8a29 | 680 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
9414de39 MD |
681 | const struct soc_camera_format_xlate *xlate; |
682 | __u32 pixfmt = f->fmt.pix.pixelformat; | |
ccab8a29 | 683 | int ret; |
a2c8c68c | 684 | |
9414de39 MD |
685 | xlate = soc_camera_xlate_by_fourcc(icd, pixfmt); |
686 | if (!xlate) { | |
687 | dev_warn(&ici->dev, "Format %x not found\n", pixfmt); | |
25c4d74e | 688 | return -EINVAL; |
9414de39 | 689 | } |
25c4d74e | 690 | |
0d3244d6 MD |
691 | /* FIXME: calculate using depth and bus width */ |
692 | ||
693 | if (f->fmt.pix.height < 4) | |
694 | f->fmt.pix.height = 4; | |
695 | if (f->fmt.pix.height > 1920) | |
696 | f->fmt.pix.height = 1920; | |
697 | if (f->fmt.pix.width < 2) | |
698 | f->fmt.pix.width = 2; | |
699 | if (f->fmt.pix.width > 2560) | |
700 | f->fmt.pix.width = 2560; | |
701 | f->fmt.pix.width &= ~0x01; | |
702 | f->fmt.pix.height &= ~0x03; | |
703 | ||
25c4d74e | 704 | f->fmt.pix.bytesperline = f->fmt.pix.width * |
9414de39 | 705 | DIV_ROUND_UP(xlate->host_fmt->depth, 8); |
25c4d74e GL |
706 | f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline; |
707 | ||
0d3244d6 | 708 | /* limit to sensor capabilities */ |
ccab8a29 KM |
709 | ret = icd->ops->try_fmt(icd, f); |
710 | if (ret < 0) | |
711 | return ret; | |
712 | ||
713 | switch (f->fmt.pix.field) { | |
714 | case V4L2_FIELD_INTERLACED: | |
e802967c | 715 | pcdev->is_interlaced = 1; |
ccab8a29 KM |
716 | break; |
717 | case V4L2_FIELD_ANY: | |
718 | f->fmt.pix.field = V4L2_FIELD_NONE; | |
719 | /* fall-through */ | |
720 | case V4L2_FIELD_NONE: | |
e802967c | 721 | pcdev->is_interlaced = 0; |
ccab8a29 KM |
722 | break; |
723 | default: | |
724 | ret = -EINVAL; | |
725 | break; | |
726 | } | |
727 | ||
728 | return ret; | |
0d3244d6 MD |
729 | } |
730 | ||
731 | static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf, | |
732 | struct v4l2_requestbuffers *p) | |
733 | { | |
734 | int i; | |
735 | ||
736 | /* This is for locking debugging only. I removed spinlocks and now I | |
737 | * check whether .prepare is ever called on a linked buffer, or whether | |
738 | * a dma IRQ can occur for an in-work or unlinked buffer. Until now | |
739 | * it hadn't triggered */ | |
740 | for (i = 0; i < p->count; i++) { | |
741 | struct sh_mobile_ceu_buffer *buf; | |
742 | ||
743 | buf = container_of(icf->vb_vidq.bufs[i], | |
744 | struct sh_mobile_ceu_buffer, vb); | |
745 | INIT_LIST_HEAD(&buf->vb.queue); | |
746 | } | |
747 | ||
748 | return 0; | |
749 | } | |
750 | ||
751 | static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt) | |
752 | { | |
753 | struct soc_camera_file *icf = file->private_data; | |
754 | struct sh_mobile_ceu_buffer *buf; | |
755 | ||
756 | buf = list_entry(icf->vb_vidq.stream.next, | |
757 | struct sh_mobile_ceu_buffer, vb.stream); | |
758 | ||
759 | poll_wait(file, &buf->vb.done, pt); | |
760 | ||
761 | if (buf->vb.state == VIDEOBUF_DONE || | |
762 | buf->vb.state == VIDEOBUF_ERROR) | |
763 | return POLLIN|POLLRDNORM; | |
764 | ||
765 | return 0; | |
766 | } | |
767 | ||
768 | static int sh_mobile_ceu_querycap(struct soc_camera_host *ici, | |
769 | struct v4l2_capability *cap) | |
770 | { | |
771 | strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card)); | |
772 | cap->version = KERNEL_VERSION(0, 0, 5); | |
773 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING; | |
774 | return 0; | |
775 | } | |
776 | ||
777 | static void sh_mobile_ceu_init_videobuf(struct videobuf_queue *q, | |
778 | struct soc_camera_device *icd) | |
779 | { | |
780 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
781 | struct sh_mobile_ceu_dev *pcdev = ici->priv; | |
782 | ||
783 | videobuf_queue_dma_contig_init(q, | |
784 | &sh_mobile_ceu_videobuf_ops, | |
785 | &ici->dev, &pcdev->lock, | |
786 | V4L2_BUF_TYPE_VIDEO_CAPTURE, | |
e802967c GL |
787 | pcdev->is_interlaced ? |
788 | V4L2_FIELD_INTERLACED : V4L2_FIELD_NONE, | |
0d3244d6 MD |
789 | sizeof(struct sh_mobile_ceu_buffer), |
790 | icd); | |
791 | } | |
792 | ||
793 | static struct soc_camera_host_ops sh_mobile_ceu_host_ops = { | |
794 | .owner = THIS_MODULE, | |
795 | .add = sh_mobile_ceu_add_device, | |
796 | .remove = sh_mobile_ceu_remove_device, | |
9414de39 | 797 | .get_formats = sh_mobile_ceu_get_formats, |
09e231b3 | 798 | .set_crop = sh_mobile_ceu_set_crop, |
d8fac217 GL |
799 | .set_fmt = sh_mobile_ceu_set_fmt, |
800 | .try_fmt = sh_mobile_ceu_try_fmt, | |
0d3244d6 MD |
801 | .reqbufs = sh_mobile_ceu_reqbufs, |
802 | .poll = sh_mobile_ceu_poll, | |
803 | .querycap = sh_mobile_ceu_querycap, | |
0d3244d6 MD |
804 | .set_bus_param = sh_mobile_ceu_set_bus_param, |
805 | .init_videobuf = sh_mobile_ceu_init_videobuf, | |
806 | }; | |
807 | ||
808 | static int sh_mobile_ceu_probe(struct platform_device *pdev) | |
809 | { | |
810 | struct sh_mobile_ceu_dev *pcdev; | |
811 | struct resource *res; | |
812 | void __iomem *base; | |
a42b6dd6 | 813 | char clk_name[8]; |
0d3244d6 MD |
814 | unsigned int irq; |
815 | int err = 0; | |
816 | ||
817 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
818 | irq = platform_get_irq(pdev, 0); | |
819 | if (!res || !irq) { | |
820 | dev_err(&pdev->dev, "Not enough CEU platform resources.\n"); | |
821 | err = -ENODEV; | |
822 | goto exit; | |
823 | } | |
824 | ||
825 | pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL); | |
826 | if (!pcdev) { | |
827 | dev_err(&pdev->dev, "Could not allocate pcdev\n"); | |
828 | err = -ENOMEM; | |
829 | goto exit; | |
830 | } | |
831 | ||
832 | platform_set_drvdata(pdev, pcdev); | |
833 | INIT_LIST_HEAD(&pcdev->capture); | |
834 | spin_lock_init(&pcdev->lock); | |
835 | ||
836 | pcdev->pdata = pdev->dev.platform_data; | |
837 | if (!pcdev->pdata) { | |
838 | err = -EINVAL; | |
839 | dev_err(&pdev->dev, "CEU platform data not set.\n"); | |
840 | goto exit_kfree; | |
841 | } | |
842 | ||
eb6c8558 | 843 | base = ioremap_nocache(res->start, resource_size(res)); |
0d3244d6 MD |
844 | if (!base) { |
845 | err = -ENXIO; | |
846 | dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n"); | |
847 | goto exit_kfree; | |
848 | } | |
849 | ||
850 | pcdev->irq = irq; | |
851 | pcdev->base = base; | |
852 | pcdev->video_limit = 0; /* only enabled if second resource exists */ | |
853 | pcdev->dev = &pdev->dev; | |
854 | ||
855 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); | |
856 | if (res) { | |
857 | err = dma_declare_coherent_memory(&pdev->dev, res->start, | |
858 | res->start, | |
eb6c8558 | 859 | resource_size(res), |
0d3244d6 MD |
860 | DMA_MEMORY_MAP | |
861 | DMA_MEMORY_EXCLUSIVE); | |
862 | if (!err) { | |
863 | dev_err(&pdev->dev, "Unable to declare CEU memory.\n"); | |
864 | err = -ENXIO; | |
865 | goto exit_iounmap; | |
866 | } | |
867 | ||
eb6c8558 | 868 | pcdev->video_limit = resource_size(res); |
0d3244d6 MD |
869 | } |
870 | ||
871 | /* request irq */ | |
872 | err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED, | |
af128a10 | 873 | dev_name(&pdev->dev), pcdev); |
0d3244d6 MD |
874 | if (err) { |
875 | dev_err(&pdev->dev, "Unable to register CEU interrupt.\n"); | |
876 | goto exit_release_mem; | |
877 | } | |
878 | ||
a42b6dd6 MD |
879 | snprintf(clk_name, sizeof(clk_name), "ceu%d", pdev->id); |
880 | pcdev->clk = clk_get(&pdev->dev, clk_name); | |
881 | if (IS_ERR(pcdev->clk)) { | |
882 | dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name); | |
883 | err = PTR_ERR(pcdev->clk); | |
884 | goto exit_free_irq; | |
885 | } | |
886 | ||
0d3244d6 MD |
887 | pcdev->ici.priv = pcdev; |
888 | pcdev->ici.dev.parent = &pdev->dev; | |
889 | pcdev->ici.nr = pdev->id; | |
af128a10 KS |
890 | pcdev->ici.drv_name = dev_name(&pdev->dev); |
891 | pcdev->ici.ops = &sh_mobile_ceu_host_ops; | |
0d3244d6 MD |
892 | |
893 | err = soc_camera_host_register(&pcdev->ici); | |
894 | if (err) | |
a42b6dd6 | 895 | goto exit_free_clk; |
0d3244d6 MD |
896 | |
897 | return 0; | |
898 | ||
a42b6dd6 MD |
899 | exit_free_clk: |
900 | clk_put(pcdev->clk); | |
0d3244d6 MD |
901 | exit_free_irq: |
902 | free_irq(pcdev->irq, pcdev); | |
903 | exit_release_mem: | |
904 | if (platform_get_resource(pdev, IORESOURCE_MEM, 1)) | |
905 | dma_release_declared_memory(&pdev->dev); | |
906 | exit_iounmap: | |
907 | iounmap(base); | |
908 | exit_kfree: | |
909 | kfree(pcdev); | |
910 | exit: | |
911 | return err; | |
912 | } | |
913 | ||
914 | static int sh_mobile_ceu_remove(struct platform_device *pdev) | |
915 | { | |
916 | struct sh_mobile_ceu_dev *pcdev = platform_get_drvdata(pdev); | |
917 | ||
918 | soc_camera_host_unregister(&pcdev->ici); | |
a42b6dd6 | 919 | clk_put(pcdev->clk); |
0d3244d6 MD |
920 | free_irq(pcdev->irq, pcdev); |
921 | if (platform_get_resource(pdev, IORESOURCE_MEM, 1)) | |
922 | dma_release_declared_memory(&pdev->dev); | |
923 | iounmap(pcdev->base); | |
924 | kfree(pcdev); | |
925 | return 0; | |
926 | } | |
927 | ||
928 | static struct platform_driver sh_mobile_ceu_driver = { | |
929 | .driver = { | |
930 | .name = "sh_mobile_ceu", | |
931 | }, | |
932 | .probe = sh_mobile_ceu_probe, | |
933 | .remove = sh_mobile_ceu_remove, | |
934 | }; | |
935 | ||
936 | static int __init sh_mobile_ceu_init(void) | |
937 | { | |
938 | return platform_driver_register(&sh_mobile_ceu_driver); | |
939 | } | |
940 | ||
941 | static void __exit sh_mobile_ceu_exit(void) | |
942 | { | |
01c1e4ca | 943 | platform_driver_unregister(&sh_mobile_ceu_driver); |
0d3244d6 MD |
944 | } |
945 | ||
946 | module_init(sh_mobile_ceu_init); | |
947 | module_exit(sh_mobile_ceu_exit); | |
948 | ||
949 | MODULE_DESCRIPTION("SuperH Mobile CEU driver"); | |
950 | MODULE_AUTHOR("Magnus Damm"); | |
951 | MODULE_LICENSE("GPL"); |