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> | |
32 | #include <linux/mutex.h> | |
33 | #include <linux/videodev2.h> | |
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 MD |
76 | |
77 | static DEFINE_MUTEX(camera_lock); | |
78 | ||
79 | /* per video frame buffer */ | |
80 | struct sh_mobile_ceu_buffer { | |
81 | struct videobuf_buffer vb; /* v4l buffer must be first */ | |
82 | const struct soc_camera_data_format *fmt; | |
83 | }; | |
84 | ||
85 | struct sh_mobile_ceu_dev { | |
86 | struct device *dev; | |
87 | struct soc_camera_host ici; | |
88 | struct soc_camera_device *icd; | |
89 | ||
90 | unsigned int irq; | |
91 | void __iomem *base; | |
92 | unsigned long video_limit; | |
93 | ||
c60f2b5c | 94 | /* lock used to protect videobuf */ |
0d3244d6 MD |
95 | spinlock_t lock; |
96 | struct list_head capture; | |
97 | struct videobuf_buffer *active; | |
98 | ||
99 | struct sh_mobile_ceu_info *pdata; | |
100 | }; | |
101 | ||
102 | static void ceu_write(struct sh_mobile_ceu_dev *priv, | |
103 | unsigned long reg_offs, unsigned long data) | |
104 | { | |
105 | iowrite32(data, priv->base + reg_offs); | |
106 | } | |
107 | ||
108 | static unsigned long ceu_read(struct sh_mobile_ceu_dev *priv, | |
109 | unsigned long reg_offs) | |
110 | { | |
111 | return ioread32(priv->base + reg_offs); | |
112 | } | |
113 | ||
114 | /* | |
115 | * Videobuf operations | |
116 | */ | |
117 | static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue *vq, | |
118 | unsigned int *count, | |
119 | unsigned int *size) | |
120 | { | |
121 | struct soc_camera_device *icd = vq->priv_data; | |
122 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
123 | struct sh_mobile_ceu_dev *pcdev = ici->priv; | |
124 | int bytes_per_pixel = (icd->current_fmt->depth + 7) >> 3; | |
125 | ||
126 | *size = PAGE_ALIGN(icd->width * icd->height * bytes_per_pixel); | |
127 | ||
128 | if (0 == *count) | |
129 | *count = 2; | |
130 | ||
131 | if (pcdev->video_limit) { | |
132 | while (*size * *count > pcdev->video_limit) | |
133 | (*count)--; | |
134 | } | |
135 | ||
136 | dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size); | |
137 | ||
138 | return 0; | |
139 | } | |
140 | ||
141 | static void free_buffer(struct videobuf_queue *vq, | |
142 | struct sh_mobile_ceu_buffer *buf) | |
143 | { | |
144 | struct soc_camera_device *icd = vq->priv_data; | |
145 | ||
86751b01 | 146 | dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__, |
0d3244d6 MD |
147 | &buf->vb, buf->vb.baddr, buf->vb.bsize); |
148 | ||
149 | if (in_interrupt()) | |
150 | BUG(); | |
151 | ||
152 | videobuf_dma_contig_free(vq, &buf->vb); | |
153 | dev_dbg(&icd->dev, "%s freed\n", __func__); | |
154 | buf->vb.state = VIDEOBUF_NEEDS_INIT; | |
155 | } | |
156 | ||
157 | static void sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev) | |
158 | { | |
159 | ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~1); | |
160 | ceu_write(pcdev, CETCR, ~ceu_read(pcdev, CETCR) & 0x0317f313); | |
161 | ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | 1); | |
162 | ||
163 | ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~0x10000); | |
164 | ||
165 | ceu_write(pcdev, CETCR, 0x0317f313 ^ 0x10); | |
166 | ||
167 | if (pcdev->active) { | |
51354cc3 | 168 | pcdev->active->state = VIDEOBUF_ACTIVE; |
0d3244d6 MD |
169 | ceu_write(pcdev, CDAYR, videobuf_to_dma_contig(pcdev->active)); |
170 | ceu_write(pcdev, CAPSR, 0x1); /* start capture */ | |
171 | } | |
172 | } | |
173 | ||
174 | static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue *vq, | |
175 | struct videobuf_buffer *vb, | |
176 | enum v4l2_field field) | |
177 | { | |
178 | struct soc_camera_device *icd = vq->priv_data; | |
179 | struct sh_mobile_ceu_buffer *buf; | |
180 | int ret; | |
181 | ||
182 | buf = container_of(vb, struct sh_mobile_ceu_buffer, vb); | |
183 | ||
86751b01 | 184 | dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__, |
0d3244d6 MD |
185 | vb, vb->baddr, vb->bsize); |
186 | ||
187 | /* Added list head initialization on alloc */ | |
188 | WARN_ON(!list_empty(&vb->queue)); | |
189 | ||
190 | #ifdef DEBUG | |
191 | /* This can be useful if you want to see if we actually fill | |
192 | * the buffer with something */ | |
193 | memset((void *)vb->baddr, 0xaa, vb->bsize); | |
194 | #endif | |
195 | ||
196 | BUG_ON(NULL == icd->current_fmt); | |
197 | ||
198 | if (buf->fmt != icd->current_fmt || | |
199 | vb->width != icd->width || | |
200 | vb->height != icd->height || | |
201 | vb->field != field) { | |
202 | buf->fmt = icd->current_fmt; | |
203 | vb->width = icd->width; | |
204 | vb->height = icd->height; | |
205 | vb->field = field; | |
206 | vb->state = VIDEOBUF_NEEDS_INIT; | |
207 | } | |
208 | ||
209 | vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3); | |
210 | if (0 != vb->baddr && vb->bsize < vb->size) { | |
211 | ret = -EINVAL; | |
212 | goto out; | |
213 | } | |
214 | ||
215 | if (vb->state == VIDEOBUF_NEEDS_INIT) { | |
216 | ret = videobuf_iolock(vq, vb, NULL); | |
217 | if (ret) | |
218 | goto fail; | |
219 | vb->state = VIDEOBUF_PREPARED; | |
220 | } | |
221 | ||
222 | return 0; | |
223 | fail: | |
224 | free_buffer(vq, buf); | |
225 | out: | |
226 | return ret; | |
227 | } | |
228 | ||
229 | static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue *vq, | |
230 | struct videobuf_buffer *vb) | |
231 | { | |
232 | struct soc_camera_device *icd = vq->priv_data; | |
233 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
234 | struct sh_mobile_ceu_dev *pcdev = ici->priv; | |
235 | unsigned long flags; | |
236 | ||
86751b01 | 237 | dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__, |
0d3244d6 MD |
238 | vb, vb->baddr, vb->bsize); |
239 | ||
51354cc3 | 240 | vb->state = VIDEOBUF_QUEUED; |
0d3244d6 MD |
241 | spin_lock_irqsave(&pcdev->lock, flags); |
242 | list_add_tail(&vb->queue, &pcdev->capture); | |
243 | ||
244 | if (!pcdev->active) { | |
245 | pcdev->active = vb; | |
246 | sh_mobile_ceu_capture(pcdev); | |
247 | } | |
248 | ||
249 | spin_unlock_irqrestore(&pcdev->lock, flags); | |
250 | } | |
251 | ||
252 | static void sh_mobile_ceu_videobuf_release(struct videobuf_queue *vq, | |
253 | struct videobuf_buffer *vb) | |
254 | { | |
255 | free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb)); | |
256 | } | |
257 | ||
258 | static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops = { | |
259 | .buf_setup = sh_mobile_ceu_videobuf_setup, | |
260 | .buf_prepare = sh_mobile_ceu_videobuf_prepare, | |
261 | .buf_queue = sh_mobile_ceu_videobuf_queue, | |
262 | .buf_release = sh_mobile_ceu_videobuf_release, | |
263 | }; | |
264 | ||
265 | static irqreturn_t sh_mobile_ceu_irq(int irq, void *data) | |
266 | { | |
267 | struct sh_mobile_ceu_dev *pcdev = data; | |
268 | struct videobuf_buffer *vb; | |
269 | unsigned long flags; | |
270 | ||
271 | spin_lock_irqsave(&pcdev->lock, flags); | |
272 | ||
273 | vb = pcdev->active; | |
274 | list_del_init(&vb->queue); | |
275 | ||
276 | if (!list_empty(&pcdev->capture)) | |
277 | pcdev->active = list_entry(pcdev->capture.next, | |
278 | struct videobuf_buffer, queue); | |
279 | else | |
280 | pcdev->active = NULL; | |
281 | ||
282 | sh_mobile_ceu_capture(pcdev); | |
283 | ||
284 | vb->state = VIDEOBUF_DONE; | |
285 | do_gettimeofday(&vb->ts); | |
286 | vb->field_count++; | |
287 | wake_up(&vb->done); | |
288 | spin_unlock_irqrestore(&pcdev->lock, flags); | |
289 | ||
290 | return IRQ_HANDLED; | |
291 | } | |
292 | ||
293 | static int sh_mobile_ceu_add_device(struct soc_camera_device *icd) | |
294 | { | |
295 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
296 | struct sh_mobile_ceu_dev *pcdev = ici->priv; | |
297 | int ret = -EBUSY; | |
298 | ||
299 | mutex_lock(&camera_lock); | |
300 | ||
301 | if (pcdev->icd) | |
302 | goto err; | |
303 | ||
304 | dev_info(&icd->dev, | |
305 | "SuperH Mobile CEU driver attached to camera %d\n", | |
306 | icd->devnum); | |
307 | ||
0d3244d6 MD |
308 | ret = icd->ops->init(icd); |
309 | if (ret) | |
310 | goto err; | |
311 | ||
312 | ceu_write(pcdev, CAPSR, 1 << 16); /* reset */ | |
313 | while (ceu_read(pcdev, CSTSR) & 1) | |
314 | msleep(1); | |
315 | ||
316 | pcdev->icd = icd; | |
317 | err: | |
318 | mutex_unlock(&camera_lock); | |
319 | ||
320 | return ret; | |
321 | } | |
322 | ||
323 | static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd) | |
324 | { | |
325 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
326 | struct sh_mobile_ceu_dev *pcdev = ici->priv; | |
51354cc3 | 327 | unsigned long flags; |
0d3244d6 MD |
328 | |
329 | BUG_ON(icd != pcdev->icd); | |
330 | ||
331 | /* disable capture, disable interrupts */ | |
332 | ceu_write(pcdev, CEIER, 0); | |
333 | ceu_write(pcdev, CAPSR, 1 << 16); /* reset */ | |
51354cc3 MD |
334 | |
335 | /* make sure active buffer is canceled */ | |
336 | spin_lock_irqsave(&pcdev->lock, flags); | |
337 | if (pcdev->active) { | |
338 | list_del(&pcdev->active->queue); | |
339 | pcdev->active->state = VIDEOBUF_ERROR; | |
340 | wake_up_all(&pcdev->active->done); | |
341 | pcdev->active = NULL; | |
342 | } | |
343 | spin_unlock_irqrestore(&pcdev->lock, flags); | |
344 | ||
0d3244d6 | 345 | icd->ops->release(icd); |
0d3244d6 MD |
346 | |
347 | dev_info(&icd->dev, | |
348 | "SuperH Mobile CEU driver detached from camera %d\n", | |
349 | icd->devnum); | |
350 | ||
351 | pcdev->icd = NULL; | |
352 | } | |
353 | ||
354 | static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd, | |
355 | __u32 pixfmt) | |
356 | { | |
357 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
358 | struct sh_mobile_ceu_dev *pcdev = ici->priv; | |
359 | int ret, buswidth, width, cfszr_width, cdwdr_width; | |
360 | unsigned long camera_flags, common_flags, value; | |
361 | ||
362 | camera_flags = icd->ops->query_bus_param(icd); | |
363 | common_flags = soc_camera_bus_param_compatible(camera_flags, | |
364 | pcdev->pdata->flags); | |
365 | if (!common_flags) | |
366 | return -EINVAL; | |
367 | ||
368 | ret = icd->ops->set_bus_param(icd, common_flags); | |
369 | if (ret < 0) | |
370 | return ret; | |
371 | ||
372 | switch (common_flags & SOCAM_DATAWIDTH_MASK) { | |
373 | case SOCAM_DATAWIDTH_8: | |
374 | buswidth = 8; | |
375 | break; | |
376 | case SOCAM_DATAWIDTH_16: | |
377 | buswidth = 16; | |
378 | break; | |
379 | default: | |
380 | return -EINVAL; | |
381 | } | |
382 | ||
383 | ceu_write(pcdev, CRCNTR, 0); | |
384 | ceu_write(pcdev, CRCMPR, 0); | |
385 | ||
386 | value = 0x00000010; | |
387 | value |= (common_flags & SOCAM_VSYNC_ACTIVE_LOW) ? (1 << 1) : 0; | |
388 | value |= (common_flags & SOCAM_HSYNC_ACTIVE_LOW) ? (1 << 0) : 0; | |
389 | value |= (buswidth == 16) ? (1 << 12) : 0; | |
390 | ceu_write(pcdev, CAMCR, value); | |
391 | ||
392 | ceu_write(pcdev, CAPCR, 0x00300000); | |
393 | ceu_write(pcdev, CAIFR, 0); | |
394 | ||
395 | mdelay(1); | |
396 | ||
397 | width = icd->width * (icd->current_fmt->depth / 8); | |
398 | width = (buswidth == 16) ? width / 2 : width; | |
399 | cfszr_width = (buswidth == 8) ? width / 2 : width; | |
400 | cdwdr_width = (buswidth == 16) ? width * 2 : width; | |
401 | ||
402 | ceu_write(pcdev, CAMOR, 0); | |
403 | ceu_write(pcdev, CAPWR, (icd->height << 16) | width); | |
404 | ceu_write(pcdev, CFLCR, 0); /* data fetch mode - no scaling */ | |
405 | ceu_write(pcdev, CFSZR, (icd->height << 16) | cfszr_width); | |
406 | ceu_write(pcdev, CLFCR, 0); /* data fetch mode - no lowpass filter */ | |
dd54203b MD |
407 | |
408 | /* A few words about byte order (observed in Big Endian mode) | |
409 | * | |
410 | * In data fetch mode bytes are received in chunks of 8 bytes. | |
411 | * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first) | |
412 | * | |
413 | * The data is however by default written to memory in reverse order: | |
414 | * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte) | |
415 | * | |
416 | * The lowest three bits of CDOCR allows us to do swapping, | |
2c0a072e MD |
417 | * using 7 we swap the data bytes to match the incoming order: |
418 | * D0, D1, D2, D3, D4, D5, D6, D7 | |
dd54203b | 419 | */ |
2c0a072e | 420 | ceu_write(pcdev, CDOCR, 0x00000017); |
0d3244d6 MD |
421 | |
422 | ceu_write(pcdev, CDWDR, cdwdr_width); | |
423 | ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */ | |
424 | ||
425 | /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */ | |
426 | /* in data fetch mode: no need for CDACR, CDBYR, CDBCR */ | |
427 | ||
428 | return 0; | |
429 | } | |
430 | ||
431 | static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd, | |
432 | __u32 pixfmt) | |
433 | { | |
434 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
435 | struct sh_mobile_ceu_dev *pcdev = ici->priv; | |
436 | unsigned long camera_flags, common_flags; | |
437 | ||
438 | camera_flags = icd->ops->query_bus_param(icd); | |
439 | common_flags = soc_camera_bus_param_compatible(camera_flags, | |
440 | pcdev->pdata->flags); | |
441 | if (!common_flags) | |
442 | return -EINVAL; | |
443 | ||
444 | return 0; | |
445 | } | |
446 | ||
447 | static int sh_mobile_ceu_set_fmt_cap(struct soc_camera_device *icd, | |
448 | __u32 pixfmt, struct v4l2_rect *rect) | |
449 | { | |
450 | return icd->ops->set_fmt_cap(icd, pixfmt, rect); | |
451 | } | |
452 | ||
453 | static int sh_mobile_ceu_try_fmt_cap(struct soc_camera_device *icd, | |
454 | struct v4l2_format *f) | |
455 | { | |
456 | /* FIXME: calculate using depth and bus width */ | |
457 | ||
458 | if (f->fmt.pix.height < 4) | |
459 | f->fmt.pix.height = 4; | |
460 | if (f->fmt.pix.height > 1920) | |
461 | f->fmt.pix.height = 1920; | |
462 | if (f->fmt.pix.width < 2) | |
463 | f->fmt.pix.width = 2; | |
464 | if (f->fmt.pix.width > 2560) | |
465 | f->fmt.pix.width = 2560; | |
466 | f->fmt.pix.width &= ~0x01; | |
467 | f->fmt.pix.height &= ~0x03; | |
468 | ||
469 | /* limit to sensor capabilities */ | |
470 | return icd->ops->try_fmt_cap(icd, f); | |
471 | } | |
472 | ||
473 | static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf, | |
474 | struct v4l2_requestbuffers *p) | |
475 | { | |
476 | int i; | |
477 | ||
478 | /* This is for locking debugging only. I removed spinlocks and now I | |
479 | * check whether .prepare is ever called on a linked buffer, or whether | |
480 | * a dma IRQ can occur for an in-work or unlinked buffer. Until now | |
481 | * it hadn't triggered */ | |
482 | for (i = 0; i < p->count; i++) { | |
483 | struct sh_mobile_ceu_buffer *buf; | |
484 | ||
485 | buf = container_of(icf->vb_vidq.bufs[i], | |
486 | struct sh_mobile_ceu_buffer, vb); | |
487 | INIT_LIST_HEAD(&buf->vb.queue); | |
488 | } | |
489 | ||
490 | return 0; | |
491 | } | |
492 | ||
493 | static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt) | |
494 | { | |
495 | struct soc_camera_file *icf = file->private_data; | |
496 | struct sh_mobile_ceu_buffer *buf; | |
497 | ||
498 | buf = list_entry(icf->vb_vidq.stream.next, | |
499 | struct sh_mobile_ceu_buffer, vb.stream); | |
500 | ||
501 | poll_wait(file, &buf->vb.done, pt); | |
502 | ||
503 | if (buf->vb.state == VIDEOBUF_DONE || | |
504 | buf->vb.state == VIDEOBUF_ERROR) | |
505 | return POLLIN|POLLRDNORM; | |
506 | ||
507 | return 0; | |
508 | } | |
509 | ||
510 | static int sh_mobile_ceu_querycap(struct soc_camera_host *ici, | |
511 | struct v4l2_capability *cap) | |
512 | { | |
513 | strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card)); | |
514 | cap->version = KERNEL_VERSION(0, 0, 5); | |
515 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING; | |
516 | return 0; | |
517 | } | |
518 | ||
519 | static void sh_mobile_ceu_init_videobuf(struct videobuf_queue *q, | |
520 | struct soc_camera_device *icd) | |
521 | { | |
522 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
523 | struct sh_mobile_ceu_dev *pcdev = ici->priv; | |
524 | ||
525 | videobuf_queue_dma_contig_init(q, | |
526 | &sh_mobile_ceu_videobuf_ops, | |
527 | &ici->dev, &pcdev->lock, | |
528 | V4L2_BUF_TYPE_VIDEO_CAPTURE, | |
529 | V4L2_FIELD_NONE, | |
530 | sizeof(struct sh_mobile_ceu_buffer), | |
531 | icd); | |
532 | } | |
533 | ||
534 | static struct soc_camera_host_ops sh_mobile_ceu_host_ops = { | |
535 | .owner = THIS_MODULE, | |
536 | .add = sh_mobile_ceu_add_device, | |
537 | .remove = sh_mobile_ceu_remove_device, | |
538 | .set_fmt_cap = sh_mobile_ceu_set_fmt_cap, | |
539 | .try_fmt_cap = sh_mobile_ceu_try_fmt_cap, | |
540 | .reqbufs = sh_mobile_ceu_reqbufs, | |
541 | .poll = sh_mobile_ceu_poll, | |
542 | .querycap = sh_mobile_ceu_querycap, | |
543 | .try_bus_param = sh_mobile_ceu_try_bus_param, | |
544 | .set_bus_param = sh_mobile_ceu_set_bus_param, | |
545 | .init_videobuf = sh_mobile_ceu_init_videobuf, | |
546 | }; | |
547 | ||
548 | static int sh_mobile_ceu_probe(struct platform_device *pdev) | |
549 | { | |
550 | struct sh_mobile_ceu_dev *pcdev; | |
551 | struct resource *res; | |
552 | void __iomem *base; | |
553 | unsigned int irq; | |
554 | int err = 0; | |
555 | ||
556 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
557 | irq = platform_get_irq(pdev, 0); | |
558 | if (!res || !irq) { | |
559 | dev_err(&pdev->dev, "Not enough CEU platform resources.\n"); | |
560 | err = -ENODEV; | |
561 | goto exit; | |
562 | } | |
563 | ||
564 | pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL); | |
565 | if (!pcdev) { | |
566 | dev_err(&pdev->dev, "Could not allocate pcdev\n"); | |
567 | err = -ENOMEM; | |
568 | goto exit; | |
569 | } | |
570 | ||
571 | platform_set_drvdata(pdev, pcdev); | |
572 | INIT_LIST_HEAD(&pcdev->capture); | |
573 | spin_lock_init(&pcdev->lock); | |
574 | ||
575 | pcdev->pdata = pdev->dev.platform_data; | |
576 | if (!pcdev->pdata) { | |
577 | err = -EINVAL; | |
578 | dev_err(&pdev->dev, "CEU platform data not set.\n"); | |
579 | goto exit_kfree; | |
580 | } | |
581 | ||
582 | base = ioremap_nocache(res->start, res->end - res->start + 1); | |
583 | if (!base) { | |
584 | err = -ENXIO; | |
585 | dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n"); | |
586 | goto exit_kfree; | |
587 | } | |
588 | ||
589 | pcdev->irq = irq; | |
590 | pcdev->base = base; | |
591 | pcdev->video_limit = 0; /* only enabled if second resource exists */ | |
592 | pcdev->dev = &pdev->dev; | |
593 | ||
594 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); | |
595 | if (res) { | |
596 | err = dma_declare_coherent_memory(&pdev->dev, res->start, | |
597 | res->start, | |
598 | (res->end - res->start) + 1, | |
599 | DMA_MEMORY_MAP | | |
600 | DMA_MEMORY_EXCLUSIVE); | |
601 | if (!err) { | |
602 | dev_err(&pdev->dev, "Unable to declare CEU memory.\n"); | |
603 | err = -ENXIO; | |
604 | goto exit_iounmap; | |
605 | } | |
606 | ||
607 | pcdev->video_limit = (res->end - res->start) + 1; | |
608 | } | |
609 | ||
610 | /* request irq */ | |
611 | err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED, | |
612 | pdev->dev.bus_id, pcdev); | |
613 | if (err) { | |
614 | dev_err(&pdev->dev, "Unable to register CEU interrupt.\n"); | |
615 | goto exit_release_mem; | |
616 | } | |
617 | ||
618 | pcdev->ici.priv = pcdev; | |
619 | pcdev->ici.dev.parent = &pdev->dev; | |
620 | pcdev->ici.nr = pdev->id; | |
621 | pcdev->ici.drv_name = pdev->dev.bus_id, | |
622 | pcdev->ici.ops = &sh_mobile_ceu_host_ops, | |
623 | ||
624 | err = soc_camera_host_register(&pcdev->ici); | |
625 | if (err) | |
626 | goto exit_free_irq; | |
627 | ||
628 | return 0; | |
629 | ||
630 | exit_free_irq: | |
631 | free_irq(pcdev->irq, pcdev); | |
632 | exit_release_mem: | |
633 | if (platform_get_resource(pdev, IORESOURCE_MEM, 1)) | |
634 | dma_release_declared_memory(&pdev->dev); | |
635 | exit_iounmap: | |
636 | iounmap(base); | |
637 | exit_kfree: | |
638 | kfree(pcdev); | |
639 | exit: | |
640 | return err; | |
641 | } | |
642 | ||
643 | static int sh_mobile_ceu_remove(struct platform_device *pdev) | |
644 | { | |
645 | struct sh_mobile_ceu_dev *pcdev = platform_get_drvdata(pdev); | |
646 | ||
647 | soc_camera_host_unregister(&pcdev->ici); | |
648 | free_irq(pcdev->irq, pcdev); | |
649 | if (platform_get_resource(pdev, IORESOURCE_MEM, 1)) | |
650 | dma_release_declared_memory(&pdev->dev); | |
651 | iounmap(pcdev->base); | |
652 | kfree(pcdev); | |
653 | return 0; | |
654 | } | |
655 | ||
656 | static struct platform_driver sh_mobile_ceu_driver = { | |
657 | .driver = { | |
658 | .name = "sh_mobile_ceu", | |
659 | }, | |
660 | .probe = sh_mobile_ceu_probe, | |
661 | .remove = sh_mobile_ceu_remove, | |
662 | }; | |
663 | ||
664 | static int __init sh_mobile_ceu_init(void) | |
665 | { | |
666 | return platform_driver_register(&sh_mobile_ceu_driver); | |
667 | } | |
668 | ||
669 | static void __exit sh_mobile_ceu_exit(void) | |
670 | { | |
01c1e4ca | 671 | platform_driver_unregister(&sh_mobile_ceu_driver); |
0d3244d6 MD |
672 | } |
673 | ||
674 | module_init(sh_mobile_ceu_init); | |
675 | module_exit(sh_mobile_ceu_exit); | |
676 | ||
677 | MODULE_DESCRIPTION("SuperH Mobile CEU driver"); | |
678 | MODULE_AUTHOR("Magnus Damm"); | |
679 | MODULE_LICENSE("GPL"); |