]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - drivers/media/platform/soc_camera/tegra_camera/common.c
4b08514c459f0caaf9d1414ab46686de1285c35e
[sojka/nv-tegra/linux-3.10.git] / drivers / media / platform / soc_camera / tegra_camera / common.c
1 /*
2  * Copyright (c) 2013-2014, NVIDIA CORPORATION.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16
17 #include <linux/delay.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/nvhost.h>
22 #include <linux/of.h>
23 #include <linux/of_device.h>
24 #include <linux/of_platform.h>
25 #include <linux/tegra_pm_domains.h>
26
27 #include <mach/powergate.h>
28
29 #include <media/soc_camera.h>
30 #include <media/soc_mediabus.h>
31 #include <media/videobuf2-dma-contig.h>
32 #include <media/tegra_v4l2_camera.h>
33
34 #include "dev.h"
35 #include "bus_client.h"
36 #include "nvhost_acm.h"
37 #include "t124/t124.h"
38
39 #include "common.h"
40
41 static int tpg_mode;
42 module_param(tpg_mode, int, 0644);
43
44 #define TEGRA_CAM_DRV_NAME "vi"
45 #define TEGRA_CAM_VERSION_CODE KERNEL_VERSION(0, 0, 5)
46
47 #define TEGRA_SYNCPT_RETRY_COUNT        10
48
49 static const struct soc_mbus_pixelfmt tegra_camera_yuv_formats[] = {
50         {
51                 .fourcc                 = V4L2_PIX_FMT_UYVY,
52                 .name                   = "YUV422 (UYVY) packed",
53                 .bits_per_sample        = 16,
54                 .packing                = SOC_MBUS_PACKING_NONE,
55                 .order                  = SOC_MBUS_ORDER_LE,
56         },
57         {
58                 .fourcc                 = V4L2_PIX_FMT_VYUY,
59                 .name                   = "YUV422 (VYUY) packed",
60                 .bits_per_sample        = 16,
61                 .packing                = SOC_MBUS_PACKING_NONE,
62                 .order                  = SOC_MBUS_ORDER_LE,
63         },
64         {
65                 .fourcc                 = V4L2_PIX_FMT_YUYV,
66                 .name                   = "YUV422 (YUYV) packed",
67                 .bits_per_sample        = 16,
68                 .packing                = SOC_MBUS_PACKING_NONE,
69                 .order                  = SOC_MBUS_ORDER_LE,
70         },
71         {
72                 .fourcc                 = V4L2_PIX_FMT_YVYU,
73                 .name                   = "YUV422 (YVYU) packed",
74                 .bits_per_sample        = 16,
75                 .packing                = SOC_MBUS_PACKING_NONE,
76                 .order                  = SOC_MBUS_ORDER_LE,
77         },
78         {
79                 .fourcc                 = V4L2_PIX_FMT_YUV420,
80                 .name                   = "YUV420 (YU12) planar",
81                 .bits_per_sample        = 12,
82                 .packing                = SOC_MBUS_PACKING_NONE,
83                 .order                  = SOC_MBUS_ORDER_LE,
84         },
85         {
86                 .fourcc                 = V4L2_PIX_FMT_YVU420,
87                 .name                   = "YVU420 (YV12) planar",
88                 .bits_per_sample        = 12,
89                 .packing                = SOC_MBUS_PACKING_NONE,
90                 .order                  = SOC_MBUS_ORDER_LE,
91         },
92 };
93
94 static const struct soc_mbus_pixelfmt tegra_camera_bayer_formats[] = {
95         {
96                 .fourcc                 = V4L2_PIX_FMT_SBGGR8,
97                 .name                   = "Bayer 8 BGBG.. GRGR..",
98                 .bits_per_sample        = 8,
99                 .packing                = SOC_MBUS_PACKING_NONE,
100                 .order                  = SOC_MBUS_ORDER_LE,
101         },
102         {
103                 .fourcc                 = V4L2_PIX_FMT_SGBRG8,
104                 .name                   = "Bayer 8 GBGB.. RGRG..",
105                 .bits_per_sample        = 8,
106                 .packing                = SOC_MBUS_PACKING_NONE,
107                 .order                  = SOC_MBUS_ORDER_LE,
108         },
109         {
110                 .fourcc                 = V4L2_PIX_FMT_SBGGR10,
111                 .name                   = "Bayer 10 BGBG.. GRGR..",
112                 .bits_per_sample        = 16,
113                 .packing                = SOC_MBUS_PACKING_EXTEND16,
114                 .order                  = SOC_MBUS_ORDER_LE,
115         },
116         {
117                 .fourcc                 = V4L2_PIX_FMT_SRGGB10,
118                 .name                   = "Bayer 10 RGRG.. GBGB..",
119                 .bits_per_sample        = 16,
120                 .packing                = SOC_MBUS_PACKING_EXTEND16,
121                 .order                  = SOC_MBUS_ORDER_LE,
122         },
123 };
124
125 static const struct soc_mbus_pixelfmt tegra_camera_rgb_formats[] = {
126         {
127                 .fourcc                 = V4L2_PIX_FMT_RGB32,
128                 .name                   = "RGBA 8-8-8-8",
129                 .bits_per_sample        = 32,
130                 .packing                = SOC_MBUS_PACKING_NONE,
131                 .order                  = SOC_MBUS_ORDER_LE,
132         },
133 };
134
135 static int tegra_camera_activate(struct tegra_camera_dev *cam,
136                                  struct soc_camera_device *icd)
137 {
138         struct tegra_camera_ops *cam_ops = cam->ops;
139         struct soc_camera_subdev_desc *ssdesc = &icd->sdesc->subdev_desc;
140         struct tegra_camera_platform_data *pdata = ssdesc->drv_priv;
141         int port = pdata->port;
142         int ret;
143
144         ret = nvhost_module_busy_ext(cam->ndev);
145         if (ret) {
146                 dev_err(&cam->ndev->dev, "nvhost module is busy\n");
147                 return ret;
148         }
149
150         /* Enable external power */
151         if (cam->reg) {
152                 ret = regulator_enable(cam->reg);
153                 if (ret)
154                         dev_err(&cam->ndev->dev, "enabling regulator failed\n");
155         }
156
157         if (cam_ops->activate)
158                 cam_ops->activate(cam);
159
160         /* Unpowergate VE */
161         tegra_unpowergate_partition(TEGRA_POWERGATE_VENC);
162
163         /* Init Clocks */
164         if (cam_ops->clks_init)
165                 cam_ops->clks_init(cam, port);
166
167         if (cam_ops->clks_enable)
168                 cam_ops->clks_enable(cam);
169
170         if (cam_ops->capture_clean)
171                 cam_ops->capture_clean(cam);
172
173         cam->sof = 1;
174
175         return 0;
176 }
177
178 static void tegra_camera_deactivate(struct tegra_camera_dev *cam)
179 {
180         struct tegra_camera_ops *cam_ops = cam->ops;
181
182
183         if (cam_ops->clks_disable)
184                 cam_ops->clks_disable(cam);
185
186         if (cam->ops->clks_deinit)
187                 cam->ops->clks_deinit(cam);
188
189         if (cam_ops->deactivate)
190                 cam_ops->deactivate(cam);
191
192         /* Powergate VE */
193         tegra_powergate_partition(TEGRA_POWERGATE_VENC);
194
195         /* Disable external power */
196         if (cam->reg)
197                 regulator_disable(cam->reg);
198
199         nvhost_module_idle_ext(cam->ndev);
200
201         cam->sof = 0;
202 }
203
204 static int tegra_camera_capture_frame(struct tegra_camera_dev *cam)
205 {
206         struct vb2_buffer *vb = cam->active;
207         struct tegra_camera_buffer *buf = to_tegra_vb(vb);
208         struct soc_camera_device *icd = buf->icd;
209         struct soc_camera_subdev_desc *ssdesc = &icd->sdesc->subdev_desc;
210         struct tegra_camera_platform_data *pdata = ssdesc->drv_priv;
211         int port = pdata->port;
212         int retry = TEGRA_SYNCPT_RETRY_COUNT;
213         int err;
214
215         /* Setup capture registers */
216         cam->ops->capture_setup(cam);
217
218         cam->ops->incr_syncpts(cam);
219
220         while (retry) {
221                 err = cam->ops->capture_start(cam, buf);
222                 /* Capturing succeed, stop capturing */
223                 cam->ops->capture_stop(cam, port);
224                 if (err) {
225                         retry--;
226
227                         cam->ops->incr_syncpts(cam);
228                         if (cam->ops->save_syncpts)
229                                 cam->ops->save_syncpts(cam);
230
231                         continue;
232                 }
233                 break;
234         }
235
236         /* Reset hardware for too many errors */
237         if (!retry) {
238                 tegra_camera_deactivate(cam);
239                 mdelay(5);
240                 tegra_camera_activate(cam, icd);
241                 if (cam->active)
242                         cam->ops->capture_setup(cam);
243         }
244
245         spin_lock_irq(&cam->videobuf_queue_lock);
246
247         vb = cam->active;
248         do_gettimeofday(&vb->v4l2_buf.timestamp);
249         vb->v4l2_buf.field = cam->field;
250         if (port == TEGRA_CAMERA_PORT_CSI_A)
251                 vb->v4l2_buf.sequence = cam->sequence_a++;
252         else if (port == TEGRA_CAMERA_PORT_CSI_B)
253                 vb->v4l2_buf.sequence = cam->sequence_b++;
254
255         vb2_buffer_done(vb, err < 0 ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
256         list_del_init(&buf->queue);
257
258         cam->num_frames++;
259
260         spin_unlock_irq(&cam->videobuf_queue_lock);
261
262         return err;
263 }
264
265 static void tegra_camera_work(struct work_struct *work)
266 {
267         struct tegra_camera_dev *cam =
268                 container_of(work, struct tegra_camera_dev, work);
269         struct tegra_camera_buffer *buf;
270
271         while (1) {
272                 mutex_lock(&cam->work_mutex);
273
274                 spin_lock_irq(&cam->videobuf_queue_lock);
275                 if (list_empty(&cam->capture)) {
276                         cam->active = NULL;
277                         spin_unlock_irq(&cam->videobuf_queue_lock);
278                         mutex_unlock(&cam->work_mutex);
279                         return;
280                 }
281
282                 buf = list_entry(cam->capture.next, struct tegra_camera_buffer,
283                                 queue);
284                 cam->active = &buf->vb;
285                 spin_unlock_irq(&cam->videobuf_queue_lock);
286
287                 tegra_camera_capture_frame(cam);
288
289                 mutex_unlock(&cam->work_mutex);
290         }
291 }
292
293 static int tegra_camera_init_buffer(struct tegra_camera_buffer *buf)
294 {
295         struct soc_camera_device *icd = buf->icd;
296         int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
297                                                 icd->current_fmt->host_fmt);
298         struct soc_camera_subdev_desc *ssdesc = &icd->sdesc->subdev_desc;
299         struct tegra_camera_platform_data *pdata = ssdesc->drv_priv;
300
301         switch (icd->current_fmt->host_fmt->fourcc) {
302         case V4L2_PIX_FMT_UYVY:
303         case V4L2_PIX_FMT_VYUY:
304         case V4L2_PIX_FMT_YUYV:
305         case V4L2_PIX_FMT_YVYU:
306         case V4L2_PIX_FMT_SBGGR8:
307         case V4L2_PIX_FMT_SGBRG8:
308         case V4L2_PIX_FMT_SBGGR10:
309         case V4L2_PIX_FMT_SRGGB10:
310         case V4L2_PIX_FMT_RGB32:
311                 buf->buffer_addr = vb2_dma_contig_plane_dma_addr(&buf->vb, 0);
312                 buf->start_addr = buf->buffer_addr;
313
314                 if (pdata->flip_v)
315                         buf->start_addr += bytes_per_line *
316                                            (icd->user_height-1);
317
318                 if (pdata->flip_h)
319                         buf->start_addr += bytes_per_line - 1;
320
321                 break;
322
323         case V4L2_PIX_FMT_YUV420:
324         case V4L2_PIX_FMT_YVU420:
325                 buf->buffer_addr = vb2_dma_contig_plane_dma_addr(&buf->vb, 0);
326                 buf->buffer_addr_u = buf->buffer_addr +
327                                      icd->user_width * icd->user_height;
328                 buf->buffer_addr_v = buf->buffer_addr_u +
329                                      (icd->user_width * icd->user_height) / 4;
330
331                 /* For YVU420, we swap the locations of the U and V planes. */
332                 if (icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_YVU420) {
333                         dma_addr_t temp = buf->buffer_addr_u;
334                         buf->buffer_addr_u = buf->buffer_addr_v;
335                         buf->buffer_addr_v = temp;
336                 }
337
338                 buf->start_addr = buf->buffer_addr;
339                 buf->start_addr_u = buf->buffer_addr_u;
340                 buf->start_addr_v = buf->buffer_addr_v;
341
342                 if (pdata->flip_v) {
343                         buf->start_addr += icd->user_width *
344                                            (icd->user_height - 1);
345
346                         buf->start_addr_u += ((icd->user_width/2) *
347                                               ((icd->user_height/2) - 1));
348
349                         buf->start_addr_v += ((icd->user_width/2) *
350                                               ((icd->user_height/2) - 1));
351                 }
352
353                 if (pdata->flip_h) {
354                         buf->start_addr += icd->user_width - 1;
355
356                         buf->start_addr_u += (icd->user_width/2) - 1;
357
358                         buf->start_addr_v += (icd->user_width/2) - 1;
359                 }
360
361                 break;
362
363         default:
364                 dev_err(icd->parent, "Wrong host format %d\n",
365                         icd->current_fmt->host_fmt->fourcc);
366                 return -EINVAL;
367         }
368
369         return 0;
370 }
371
372 /*
373  *  Videobuf operations
374  */
375 static int tegra_camera_videobuf_setup(struct vb2_queue *vq,
376                                        const struct v4l2_format *fmt,
377                                        unsigned int *num_buffers,
378                                        unsigned int *num_planes,
379                                        unsigned int sizes[],
380                                        void *alloc_ctxs[])
381 {
382         struct soc_camera_device *icd = container_of(vq,
383                                                      struct soc_camera_device,
384                                                      vb2_vidq);
385         struct soc_camera_subdev_desc *ssdesc = &icd->sdesc->subdev_desc;
386         struct tegra_camera_platform_data *pdata = ssdesc->drv_priv;
387         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
388         struct tegra_camera_dev *cam = ici->priv;
389         int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
390                                                 icd->current_fmt->host_fmt);
391         if (bytes_per_line < 0)
392                 return bytes_per_line;
393
394         *num_planes = 1;
395
396         if (pdata->port == TEGRA_CAMERA_PORT_CSI_A)
397                 cam->sequence_a = 0;
398         else if (pdata->port == TEGRA_CAMERA_PORT_CSI_B)
399                 cam->sequence_b = 0;
400         sizes[0] = bytes_per_line * icd->user_height;
401         alloc_ctxs[0] = cam->alloc_ctx;
402
403         if (!*num_buffers)
404                 *num_buffers = 2;
405
406         return 0;
407 }
408
409 static int tegra_camera_videobuf_prepare(struct vb2_buffer *vb)
410 {
411         struct soc_camera_device *icd = container_of(vb->vb2_queue,
412                                                      struct soc_camera_device,
413                                                      vb2_vidq);
414         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
415         struct tegra_camera_dev *cam = ici->priv;
416         struct tegra_camera_buffer *buf = to_tegra_vb(vb);
417         struct soc_camera_subdev_desc *ssdesc = &icd->sdesc->subdev_desc;
418         struct tegra_camera_platform_data *pdata = ssdesc->drv_priv;
419         int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
420                                                 icd->current_fmt->host_fmt);
421         unsigned long size;
422
423         if (bytes_per_line < 0)
424                 return bytes_per_line;
425
426         buf->icd = icd;
427
428         if (!pdata) {
429                 dev_err(icd->parent, "No platform data for this device!\n");
430                 return -EINVAL;
431         }
432
433         if (!cam->ops->port_is_valid(pdata->port)) {
434                 dev_err(icd->parent,
435                         "Invalid camera port %d in platform data\n",
436                         pdata->port);
437                 return -EINVAL;
438         }
439
440 #ifdef PREFILL_BUFFER
441         dev_info(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
442                 vb, vb2_plane_vaddr(vb, 0), vb2_plane_size(vb, 0));
443
444         /*
445          * This can be useful if you want to see if we actually fill
446          * the buffer with something
447          */
448         if (vb2_plane_vaddr(vb, 0))
449                 memset(vb2_plane_vaddr(vb, 0), 0xbd, vb2_plane_size(vb, 0));
450 #endif
451
452         if (!icd->current_fmt) {
453                 dev_err(icd->parent, "%s NULL format point\n", __func__);
454                 return -EINVAL;
455         }
456
457         size = icd->user_height * bytes_per_line;
458
459         if (vb2_plane_size(vb, 0) < size) {
460                 dev_err(icd->parent, "Buffer too small (%lu < %lu)\n",
461                         vb2_plane_size(vb, 0), size);
462                 return -ENOBUFS;
463         }
464
465         vb2_set_plane_payload(vb, 0, size);
466
467         return tegra_camera_init_buffer(buf);
468 }
469
470 static void tegra_camera_videobuf_queue(struct vb2_buffer *vb)
471 {
472         struct soc_camera_device *icd = container_of(vb->vb2_queue,
473                                                      struct soc_camera_device,
474                                                      vb2_vidq);
475         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
476         struct tegra_camera_dev *cam = ici->priv;
477         struct tegra_camera_buffer *buf = to_tegra_vb(vb);
478
479         spin_lock_irq(&cam->videobuf_queue_lock);
480         list_add_tail(&buf->queue, &cam->capture);
481         schedule_work(&cam->work);
482         spin_unlock_irq(&cam->videobuf_queue_lock);
483 }
484
485 static void tegra_camera_videobuf_release(struct vb2_buffer *vb)
486 {
487         struct soc_camera_device *icd = container_of(vb->vb2_queue,
488                                                      struct soc_camera_device,
489                                                      vb2_vidq);
490         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
491         struct tegra_camera_buffer *buf = to_tegra_vb(vb);
492         struct tegra_camera_dev *cam = ici->priv;
493
494         mutex_lock(&cam->work_mutex);
495
496         spin_lock_irq(&cam->videobuf_queue_lock);
497
498         if (cam->active == vb)
499                 cam->active = NULL;
500
501         /*
502          * Doesn't hurt also if the list is empty, but it hurts, if queuing the
503          * buffer failed, and .buf_init() hasn't been called
504          */
505         if (buf->queue.next)
506                 list_del_init(&buf->queue);
507
508         spin_unlock_irq(&cam->videobuf_queue_lock);
509
510         mutex_unlock(&cam->work_mutex);
511 }
512
513 static int tegra_camera_videobuf_init(struct vb2_buffer *vb)
514 {
515         /* This is for locking debugging only */
516         INIT_LIST_HEAD(&to_tegra_vb(vb)->queue);
517
518         return 0;
519 }
520
521 static int tegra_camera_stop_streaming(struct vb2_queue *q)
522 {
523         struct soc_camera_device *icd = container_of(q,
524                                                      struct soc_camera_device,
525                                                      vb2_vidq);
526         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
527         struct tegra_camera_dev *cam = ici->priv;
528         struct list_head *buf_head, *tmp;
529
530
531         mutex_lock(&cam->work_mutex);
532
533         spin_lock_irq(&cam->videobuf_queue_lock);
534         list_for_each_safe(buf_head, tmp, &cam->capture) {
535                 struct tegra_camera_buffer *buf = container_of(buf_head,
536                                 struct tegra_camera_buffer,
537                                 queue);
538                 if (buf->icd == icd)
539                         list_del_init(buf_head);
540         }
541         spin_unlock_irq(&cam->videobuf_queue_lock);
542
543         if (cam->active) {
544                 struct tegra_camera_buffer *buf = to_tegra_vb(cam->active);
545                 if (buf->icd == icd)
546                         cam->active = NULL;
547         }
548
549         mutex_unlock(&cam->work_mutex);
550
551         return 0;
552 }
553
554 static struct vb2_ops tegra_camera_videobuf_ops = {
555         .queue_setup    = tegra_camera_videobuf_setup,
556         .buf_prepare    = tegra_camera_videobuf_prepare,
557         .buf_queue      = tegra_camera_videobuf_queue,
558         .buf_cleanup    = tegra_camera_videobuf_release,
559         .buf_init       = tegra_camera_videobuf_init,
560         .wait_prepare   = soc_camera_unlock,
561         .wait_finish    = soc_camera_lock,
562         .stop_streaming = tegra_camera_stop_streaming,
563 };
564
565 /*
566  *  SOC camera host operations
567  */
568 static int tegra_camera_init_videobuf(struct vb2_queue *q,
569                                       struct soc_camera_device *icd)
570 {
571         q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
572         q->io_modes = VB2_MMAP | VB2_USERPTR;
573         q->drv_priv = icd;
574         q->ops = &tegra_camera_videobuf_ops;
575         q->mem_ops = &vb2_dma_contig_memops;
576         q->buf_struct_size = sizeof(struct tegra_camera_buffer);
577         q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
578
579         return vb2_queue_init(q);
580 }
581
582 /*
583  * Called with .video_lock held
584  */
585 static int tegra_camera_add_device(struct soc_camera_device *icd)
586 {
587         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
588         struct tegra_camera_dev *cam = ici->priv;
589         int ret;
590
591         if (!cam->enable_refcnt) {
592                 ret = tegra_camera_activate(cam, icd);
593                 if (ret)
594                         return ret;
595                 cam->num_frames = 0;
596         }
597         cam->enable_refcnt++;
598
599         return 0;
600 }
601
602 /* Called with .video_lock held */
603 static void tegra_camera_remove_device(struct soc_camera_device *icd)
604 {
605         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
606         struct tegra_camera_dev *cam = ici->priv;
607
608         cam->enable_refcnt--;
609         if (!cam->enable_refcnt) {
610                 cancel_work_sync(&cam->work);
611                 tegra_camera_deactivate(cam);
612         }
613 }
614
615 static int tegra_camera_set_bus_param(struct soc_camera_device *icd)
616 {
617         return 0;
618 }
619
620 static int tegra_camera_get_formats(struct soc_camera_device *icd,
621                                     unsigned int idx,
622                                     struct soc_camera_format_xlate *xlate)
623 {
624         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
625         struct device *dev = icd->parent;
626         struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
627         struct tegra_camera_dev *cam = ici->priv;
628         int num_formats;
629         const struct soc_mbus_pixelfmt *formats;
630         int ret;
631         enum v4l2_mbus_pixelcode code;
632         int k;
633
634         /*
635          * If we're in test pattern mode, ignore the subdev's formats, and
636          * pick a format that the test pattern mode can handle.
637          */
638         if (!cam->tpg_mode) {
639                 ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
640                 if (ret != 0)
641                         /* No more formats */
642                         return 0;
643         } else
644                 code = V4L2_MBUS_FMT_RGBA8888_4X8_LE;
645
646         switch (code) {
647         case V4L2_MBUS_FMT_UYVY8_2X8:
648         case V4L2_MBUS_FMT_VYUY8_2X8:
649         case V4L2_MBUS_FMT_YUYV8_2X8:
650         case V4L2_MBUS_FMT_YVYU8_2X8:
651                 formats = tegra_camera_yuv_formats;
652                 num_formats = ARRAY_SIZE(tegra_camera_yuv_formats);
653                 break;
654         case V4L2_MBUS_FMT_SBGGR8_1X8:
655         case V4L2_MBUS_FMT_SGBRG8_1X8:
656         case V4L2_MBUS_FMT_SBGGR10_1X10:
657         case V4L2_MBUS_FMT_SRGGB10_1X10:
658                 formats = tegra_camera_bayer_formats;
659                 num_formats = ARRAY_SIZE(tegra_camera_bayer_formats);
660                 break;
661         case V4L2_MBUS_FMT_RGBA8888_4X8_LE:
662                 formats = tegra_camera_rgb_formats;
663                 num_formats = ARRAY_SIZE(tegra_camera_rgb_formats);
664                 break;
665         default:
666                 dev_notice(dev, "Not supporting mbus format code 0x%04x\n",
667                            code);
668                 formats = NULL;
669                 num_formats = 0;
670         }
671
672         for (k = 0; xlate && (k < num_formats); k++) {
673                 xlate->host_fmt = &formats[k];
674                 xlate->code     = code;
675                 xlate++;
676
677                 dev_notice(dev, "Supporting mbus format code 0x%04x using %s\n",
678                            code, formats[k].name);
679         }
680
681         return num_formats;
682 }
683
684 static void tegra_camera_put_formats(struct soc_camera_device *icd)
685 {
686         kfree(icd->host_priv);
687         icd->host_priv = NULL;
688 }
689
690 static int tegra_camera_set_fmt(struct soc_camera_device *icd,
691                               struct v4l2_format *f)
692 {
693         struct device *dev = icd->parent;
694         struct soc_camera_host *ici = to_soc_camera_host(dev);
695         struct tegra_camera_dev *cam = ici->priv;
696         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
697         const struct soc_camera_format_xlate *xlate = NULL;
698         struct v4l2_pix_format *pix = &f->fmt.pix;
699         struct v4l2_mbus_framefmt mf;
700         int ret = 0;
701
702         xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
703         if (!xlate) {
704                 dev_warn(dev, "Format %x not found\n", pix->pixelformat);
705                 return -EINVAL;
706         }
707
708         mf.width        = pix->width;
709         mf.height       = pix->height;
710         mf.field        = pix->field;
711         mf.colorspace   = pix->colorspace;
712         mf.code         = xlate->code;
713
714         if (!cam->tpg_mode) {
715                 ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
716                 if (IS_ERR_VALUE(ret)) {
717                         dev_warn(dev, "Failed to configure for format %x\n",
718                                  pix->pixelformat);
719                         return ret;
720                 }
721
722                 if (mf.code != xlate->code) {
723                         dev_warn(dev,
724                                  "mf.code = 0x%04x, xlate->code = 0x%04x, "
725                                  "mismatch\n", mf.code, xlate->code);
726                         return -EINVAL;
727                 }
728         }
729
730         icd->user_width         = mf.width;
731         icd->user_height        = mf.height;
732         icd->current_fmt        = xlate;
733
734         cam->field = pix->field;
735
736         return ret;
737 }
738
739 static int tegra_camera_try_fmt(struct soc_camera_device *icd,
740                                 struct v4l2_format *f)
741 {
742         struct device *dev = icd->parent;
743         struct soc_camera_host *ici = to_soc_camera_host(dev);
744         struct tegra_camera_dev *cam = ici->priv;
745         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
746         const struct soc_camera_format_xlate *xlate;
747         struct v4l2_pix_format *pix = &f->fmt.pix;
748         struct v4l2_mbus_framefmt mf;
749         __u32 pixfmt = pix->pixelformat;
750         int ret = 0;
751
752         xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
753         if (!xlate) {
754                 dev_warn(icd->parent, "Format %x not found\n", pixfmt);
755                 return -EINVAL;
756         }
757
758         pix->bytesperline = soc_mbus_bytes_per_line(pix->width,
759                                                     xlate->host_fmt);
760         if (pix->bytesperline < 0)
761                 return pix->bytesperline;
762         pix->sizeimage = pix->height * pix->bytesperline;
763
764         /* limit to sensor capabilities */
765         mf.width        = pix->width;
766         mf.height       = pix->height;
767         mf.field        = pix->field;
768         mf.colorspace   = pix->colorspace;
769         mf.code         = xlate->code;
770
771         if (!cam->tpg_mode) {
772                 ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
773                 if (IS_ERR_VALUE(ret))
774                         return ret;
775         }
776
777         pix->width      = mf.width;
778         pix->height     = mf.height;
779         pix->colorspace = mf.colorspace;
780         /*
781          * width and height could have been changed, therefore update the
782          * bytesperline and sizeimage here.
783          */
784         pix->bytesperline = soc_mbus_bytes_per_line(pix->width,
785                                                     xlate->host_fmt);
786         pix->sizeimage = pix->height * pix->bytesperline;
787
788         switch (mf.field) {
789         case V4L2_FIELD_ANY:
790         case V4L2_FIELD_NONE:
791                 pix->field      = V4L2_FIELD_NONE;
792                 break;
793         default:
794                 /* TODO: support interlaced at least in pass-through mode */
795                 dev_err(icd->parent, "Field type %d unsupported.\n",
796                         mf.field);
797                 return -EINVAL;
798         }
799
800         return ret;
801 }
802
803 static int tegra_camera_reqbufs(struct soc_camera_device *icd,
804                                 struct v4l2_requestbuffers *p)
805 {
806         return 0;
807 }
808
809 static unsigned int tegra_camera_poll(struct file *file, poll_table *pt)
810 {
811         struct soc_camera_device *icd = file->private_data;
812
813         return vb2_poll(&icd->vb2_vidq, file, pt);
814 }
815
816 static int tegra_camera_querycap(struct soc_camera_host *ici,
817                                  struct v4l2_capability *cap)
818 {
819         strlcpy(cap->card, TEGRA_CAM_DRV_NAME, sizeof(cap->card));
820         cap->version = TEGRA_CAM_VERSION_CODE;
821         cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
822
823         return 0;
824 }
825
826 static struct soc_camera_host_ops tegra_soc_camera_host_ops = {
827         .owner          = THIS_MODULE,
828         .init_videobuf2 = tegra_camera_init_videobuf,
829         .add            = tegra_camera_add_device,
830         .remove         = tegra_camera_remove_device,
831         .set_bus_param  = tegra_camera_set_bus_param,
832         .get_formats    = tegra_camera_get_formats,
833         .put_formats    = tegra_camera_put_formats,
834         .set_fmt        = tegra_camera_set_fmt,
835         .try_fmt        = tegra_camera_try_fmt,
836         .reqbufs        = tegra_camera_reqbufs,
837         .poll           = tegra_camera_poll,
838         .querycap       = tegra_camera_querycap,
839 };
840
841 static struct of_device_id tegra_vi_of_match[] = {
842 #ifdef TEGRA_12X_OR_HIGHER_CONFIG
843         { .compatible = "nvidia,tegra124-vi",
844                 .data = (struct nvhost_device_data *)&t124_vi_info },
845 #endif
846         { },
847 };
848
849 static int tegra_camera_probe(struct platform_device *pdev)
850 {
851         struct tegra_camera_dev *cam;
852         struct nvhost_device_data *ndata = NULL;
853         int err = 0;
854
855         if (pdev->dev.of_node) {
856                 const struct of_device_id *match;
857
858                 match = of_match_device(tegra_vi_of_match, &pdev->dev);
859                 if (match) {
860                         ndata = (struct nvhost_device_data *) match->data;
861                         pdev->dev.platform_data = ndata;
862                 }
863
864                 /*
865                  * Device Tree will initialize this ID as -1
866                  * Set it to the right value for future usage
867                  */
868                 pdev->id = pdev->dev.id;
869         } else
870                 ndata = pdev->dev.platform_data;
871
872         if (!ndata) {
873                 dev_err(&pdev->dev, "No nvhost device data!\n");
874                 err = -EINVAL;
875                 goto exit;
876         }
877
878         /* vi.1 has to wait vi.0 initialized, so defer probing */
879         if (pdev->id && ndata->master) {
880                 struct nvhost_device_data *master_ndata =
881                         ndata->master->dev.platform_data;
882                 if (master_ndata == platform_get_drvdata(ndata->master))
883                         return -EPROBE_DEFER;
884         }
885
886         cam = devm_kzalloc(&pdev->dev, sizeof(struct tegra_camera_dev),
887                            GFP_KERNEL);
888         if (!cam) {
889                 dev_err(&pdev->dev, "couldn't allocate cam\n");
890                 err = -ENOMEM;
891                 goto exit;
892         }
893
894         cam->ndata = ndata;
895         cam->ndev = pdev;
896
897         cam->ici.priv = cam;
898         cam->ici.v4l2_dev.dev = &pdev->dev;
899         cam->ici.nr = pdev->id;
900         cam->ici.drv_name = dev_name(&pdev->dev);
901         cam->ici.ops = &tegra_soc_camera_host_ops;
902
903         cam->tpg_mode = tpg_mode;
904
905         INIT_LIST_HEAD(&cam->capture);
906         INIT_WORK(&cam->work, tegra_camera_work);
907         spin_lock_init(&cam->videobuf_queue_lock);
908         mutex_init(&cam->work_mutex);
909
910         if (pdev->dev.of_node) {
911                 int cplen;
912                 const char *compat;
913                 compat = of_get_property(pdev->dev.of_node,
914                                          "compatible", &cplen);
915
916                 if (!strcmp(compat, "nvidia,tegra124-vi"))
917                         vi2_register(cam);
918                 else
919                         vi_register(cam);
920         } else {
921 #ifdef TEGRA_12X_OR_HIGHER_CONFIG
922         /* Register VI/CSI or VI2/CSI2 structs */
923                 vi2_register(cam);
924 #else
925                 vi_register(cam);
926 #endif
927         }
928
929         /* Init Regulator */
930         cam->reg = devm_regulator_get(&pdev->dev, cam->regulator_name);
931         if (IS_ERR_OR_NULL(cam->reg)) {
932                 dev_err(&pdev->dev, "%s: couldn't get regulator %s, err %ld\n",
933                         __func__, cam->regulator_name, PTR_ERR(cam->reg));
934                 cam->reg = NULL;
935                 goto exit;
936         }
937
938         mutex_init(&ndata->lock);
939         platform_set_drvdata(pdev, ndata);
940         err = nvhost_client_device_get_resources(pdev);
941         if (err) {
942                 dev_err(&pdev->dev, "%s: nvhost get resources failed %d\n",
943                                 __func__, err);
944                 goto exit;
945         }
946
947         /* Init syncpts */
948         cam->ops->init_syncpts(cam);
949
950         if (!ndata->aperture[0]) {
951                 if (ndata->master) {
952                         struct nvhost_device_data *master_ndata =
953                                 ndata->master->dev.platform_data;
954                         ndata->aperture[0] = master_ndata->aperture[0];
955                 } else {
956                         dev_err(&pdev->dev, "%s: failed to map register base\n",
957                                 __func__);
958                         err = -ENXIO;
959                         goto exit_free_syncpts;
960                 }
961         }
962         cam->reg_base = ndata->aperture[0];
963
964         /* Match the nvhost_module_init VENC powergating */
965         tegra_unpowergate_partition(TEGRA_POWERGATE_VENC);
966         nvhost_module_init(pdev);
967
968         err = nvhost_client_device_init(pdev);
969         if (err) {
970                 dev_err(&pdev->dev, "%s: nvhost init failed %d\n",
971                                 __func__, err);
972                 goto exit_free_syncpts;
973         }
974
975         cam->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
976         if (IS_ERR(cam->alloc_ctx)) {
977                 err = PTR_ERR(cam->alloc_ctx);
978                 goto exit_free_syncpts;
979         }
980
981         platform_set_drvdata(pdev, cam);
982         err = soc_camera_host_register(&cam->ici);
983         if (IS_ERR_VALUE(err))
984                 goto exit_cleanup_alloc_ctx;
985
986         dev_notice(&pdev->dev, "Tegra camera driver loaded.\n");
987
988         return err;
989
990 exit_cleanup_alloc_ctx:
991         platform_set_drvdata(pdev, cam->ndata);
992         vb2_dma_contig_cleanup_ctx(cam->alloc_ctx);
993 exit_free_syncpts:
994         cam->ops->free_syncpts(cam);
995 exit:
996         return err;
997 }
998
999 static int tegra_camera_remove(struct platform_device *pdev)
1000 {
1001         struct soc_camera_host *ici = to_soc_camera_host(&pdev->dev);
1002         struct tegra_camera_dev *cam = container_of(ici,
1003                                         struct tegra_camera_dev, ici);
1004
1005         soc_camera_host_unregister(ici);
1006
1007         platform_set_drvdata(pdev, cam->ndata);
1008         nvhost_client_device_release(pdev);
1009         cam->ndata->aperture[0] = NULL;
1010
1011         vb2_dma_contig_cleanup_ctx(cam->alloc_ctx);
1012
1013         if (cam->ops)
1014                 cam->ops->free_syncpts(cam);
1015
1016         dev_notice(&pdev->dev, "Tegra camera host driver unloaded\n");
1017
1018         return 0;
1019 }
1020
1021 static struct platform_driver tegra_camera_driver = {
1022         .driver = {
1023                 .name   = TEGRA_CAM_DRV_NAME,
1024                 .owner  = THIS_MODULE,
1025 #ifdef CONFIG_OF
1026                 .of_match_table = tegra_vi_of_match,
1027 #endif
1028         },
1029         .probe          = tegra_camera_probe,
1030         .remove         = tegra_camera_remove,
1031 };
1032
1033 static int __init tegra_camera_init(void)
1034 {
1035         return platform_driver_register(&tegra_camera_driver);
1036 }
1037
1038 static void __exit tegra_camera_exit(void)
1039 {
1040         platform_driver_unregister(&tegra_camera_driver);
1041 }
1042
1043 module_init(tegra_camera_init);
1044 module_exit(tegra_camera_exit);
1045
1046 MODULE_DESCRIPTION("TEGRA SoC Camera Host driver");
1047 MODULE_AUTHOR("Bryan Wu <pengw@nvidia.com>");
1048 MODULE_LICENSE("GPL v2");
1049 MODULE_ALIAS("nvhost:" TEGRA_CAM_DRV_NAME);