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