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