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