blob: 1ce431af8fc6f110c5f11f6354cc1fe414dd9917 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Ben Collinsfaa4fd22010-06-17 13:27:26 -04002/*
Hans Verkuildcae5da2013-03-25 05:35:17 -03003 * Copyright (C) 2010-2013 Bluecherry, LLC <http://www.bluecherrydvr.com>
4 *
5 * Original author:
6 * Ben Collins <bcollins@ubuntu.com>
7 *
8 * Additional work by:
9 * John Brooks <john.brooks@bluecherry.net>
Ben Collinsfaa4fd22010-06-17 13:27:26 -040010 */
11
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/kthread.h>
15#include <linux/freezer.h>
Hans Verkuildcae5da2013-03-25 05:35:17 -030016
Ben Collinsfaa4fd22010-06-17 13:27:26 -040017#include <media/v4l2-ioctl.h>
18#include <media/v4l2-common.h>
Hans Verkuil94160492013-03-12 18:47:03 -030019#include <media/v4l2-event.h>
Junghak Sung2d700712015-09-22 10:30:30 -030020#include <media/videobuf2-v4l2.h>
Hans Verkuila4056c2f2013-03-15 12:11:17 -030021#include <media/videobuf2-dma-contig.h>
Hans Verkuildcae5da2013-03-25 05:35:17 -030022
Krzysztof Hałasaae69b222011-02-11 13:36:27 +010023#include "solo6x10.h"
Hans Verkuildad7fab2013-03-25 05:42:46 -030024#include "solo6x10-tw28.h"
Ben Collinsfaa4fd22010-06-17 13:27:26 -040025
Hans Verkuildcae5da2013-03-25 05:35:17 -030026/* Image size is two fields, SOLO_HW_BPL is one horizontal line in hardware */
27#define SOLO_HW_BPL 2048
Ben Collinsfaa4fd22010-06-17 13:27:26 -040028#define solo_vlines(__solo) (__solo->video_vsize * 2)
29#define solo_image_size(__solo) (solo_bytesperline(__solo) * \
30 solo_vlines(__solo))
31#define solo_bytesperline(__solo) (__solo->video_hsize * 2)
32
Hans Verkuildcae5da2013-03-25 05:35:17 -030033#define MIN_VID_BUFFERS 2
Ben Collinsfaa4fd22010-06-17 13:27:26 -040034
Hans Verkuildcae5da2013-03-25 05:35:17 -030035static inline void erase_on(struct solo_dev *solo_dev)
Ben Collinsfaa4fd22010-06-17 13:27:26 -040036{
37 solo_reg_write(solo_dev, SOLO_VO_DISP_ERASE, SOLO_VO_DISP_ERASE_ON);
38 solo_dev->erasing = 1;
39 solo_dev->frame_blank = 0;
40}
41
Hans Verkuildcae5da2013-03-25 05:35:17 -030042static inline int erase_off(struct solo_dev *solo_dev)
Ben Collinsfaa4fd22010-06-17 13:27:26 -040043{
44 if (!solo_dev->erasing)
45 return 0;
46
47 /* First time around, assert erase off */
48 if (!solo_dev->frame_blank)
49 solo_reg_write(solo_dev, SOLO_VO_DISP_ERASE, 0);
50 /* Keep the erasing flag on for 8 frames minimum */
51 if (solo_dev->frame_blank++ >= 8)
52 solo_dev->erasing = 0;
53
54 return 1;
55}
56
Krzysztof Hałasadecebab2011-02-11 13:38:20 +010057void solo_video_in_isr(struct solo_dev *solo_dev)
Ben Collinsfaa4fd22010-06-17 13:27:26 -040058{
Hans Verkuildcae5da2013-03-25 05:35:17 -030059 wake_up_interruptible_all(&solo_dev->disp_thread_wait);
Ben Collinsfaa4fd22010-06-17 13:27:26 -040060}
61
Krzysztof Hałasadecebab2011-02-11 13:38:20 +010062static void solo_win_setup(struct solo_dev *solo_dev, u8 ch,
Ben Collinsfaa4fd22010-06-17 13:27:26 -040063 int sx, int sy, int ex, int ey, int scale)
64{
65 if (ch >= solo_dev->nr_chans)
66 return;
67
68 /* Here, we just keep window/channel the same */
69 solo_reg_write(solo_dev, SOLO_VI_WIN_CTRL0(ch),
70 SOLO_VI_WIN_CHANNEL(ch) |
71 SOLO_VI_WIN_SX(sx) |
72 SOLO_VI_WIN_EX(ex) |
73 SOLO_VI_WIN_SCALE(scale));
74
Ben Collinsf62de9b2010-11-04 22:51:17 -040075 solo_reg_write(solo_dev, SOLO_VI_WIN_CTRL1(ch),
Ben Collinsfaa4fd22010-06-17 13:27:26 -040076 SOLO_VI_WIN_SY(sy) |
77 SOLO_VI_WIN_EY(ey));
78}
79
Krzysztof Hałasadecebab2011-02-11 13:38:20 +010080static int solo_v4l2_ch_ext_4up(struct solo_dev *solo_dev, u8 idx, int on)
Ben Collinsfaa4fd22010-06-17 13:27:26 -040081{
82 u8 ch = idx * 4;
83
84 if (ch >= solo_dev->nr_chans)
85 return -EINVAL;
86
87 if (!on) {
88 u8 i;
Hans Verkuil1c6f3db2014-07-17 20:40:22 -030089
Ben Collinsfaa4fd22010-06-17 13:27:26 -040090 for (i = ch; i < ch + 4; i++)
91 solo_win_setup(solo_dev, i, solo_dev->video_hsize,
92 solo_vlines(solo_dev),
93 solo_dev->video_hsize,
94 solo_vlines(solo_dev), 0);
95 return 0;
96 }
97
98 /* Row 1 */
99 solo_win_setup(solo_dev, ch, 0, 0, solo_dev->video_hsize / 2,
100 solo_vlines(solo_dev) / 2, 3);
101 solo_win_setup(solo_dev, ch + 1, solo_dev->video_hsize / 2, 0,
102 solo_dev->video_hsize, solo_vlines(solo_dev) / 2, 3);
103 /* Row 2 */
104 solo_win_setup(solo_dev, ch + 2, 0, solo_vlines(solo_dev) / 2,
105 solo_dev->video_hsize / 2, solo_vlines(solo_dev), 3);
106 solo_win_setup(solo_dev, ch + 3, solo_dev->video_hsize / 2,
107 solo_vlines(solo_dev) / 2, solo_dev->video_hsize,
108 solo_vlines(solo_dev), 3);
109
110 return 0;
111}
112
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100113static int solo_v4l2_ch_ext_16up(struct solo_dev *solo_dev, int on)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400114{
115 int sy, ysize, hsize, i;
116
117 if (!on) {
118 for (i = 0; i < 16; i++)
119 solo_win_setup(solo_dev, i, solo_dev->video_hsize,
120 solo_vlines(solo_dev),
121 solo_dev->video_hsize,
122 solo_vlines(solo_dev), 0);
123 return 0;
124 }
125
126 ysize = solo_vlines(solo_dev) / 4;
127 hsize = solo_dev->video_hsize / 4;
128
129 for (sy = 0, i = 0; i < 4; i++, sy += ysize) {
130 solo_win_setup(solo_dev, i * 4, 0, sy, hsize,
131 sy + ysize, 5);
132 solo_win_setup(solo_dev, (i * 4) + 1, hsize, sy,
133 hsize * 2, sy + ysize, 5);
134 solo_win_setup(solo_dev, (i * 4) + 2, hsize * 2, sy,
135 hsize * 3, sy + ysize, 5);
136 solo_win_setup(solo_dev, (i * 4) + 3, hsize * 3, sy,
137 solo_dev->video_hsize, sy + ysize, 5);
138 }
139
140 return 0;
141}
142
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100143static int solo_v4l2_ch(struct solo_dev *solo_dev, u8 ch, int on)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400144{
145 u8 ext_ch;
146
147 if (ch < solo_dev->nr_chans) {
148 solo_win_setup(solo_dev, ch, on ? 0 : solo_dev->video_hsize,
149 on ? 0 : solo_vlines(solo_dev),
150 solo_dev->video_hsize, solo_vlines(solo_dev),
151 on ? 1 : 0);
152 return 0;
153 }
154
155 if (ch >= solo_dev->nr_chans + solo_dev->nr_ext)
156 return -EINVAL;
157
158 ext_ch = ch - solo_dev->nr_chans;
159
160 /* 4up's first */
161 if (ext_ch < 4)
162 return solo_v4l2_ch_ext_4up(solo_dev, ext_ch, on);
163
164 /* Remaining case is 16up for 16-port */
165 return solo_v4l2_ch_ext_16up(solo_dev, on);
166}
167
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100168static int solo_v4l2_set_ch(struct solo_dev *solo_dev, u8 ch)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400169{
170 if (ch >= solo_dev->nr_chans + solo_dev->nr_ext)
171 return -EINVAL;
172
173 erase_on(solo_dev);
174
175 solo_v4l2_ch(solo_dev, solo_dev->cur_disp_ch, 0);
176 solo_v4l2_ch(solo_dev, ch, 1);
177
178 solo_dev->cur_disp_ch = ch;
179
180 return 0;
181}
182
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300183static void solo_fillbuf(struct solo_dev *solo_dev,
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300184 struct vb2_buffer *vb)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400185{
Junghak Sung2d700712015-09-22 10:30:30 -0300186 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
187 dma_addr_t addr;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400188 unsigned int fdma_addr;
Hans Verkuildcae5da2013-03-25 05:35:17 -0300189 int error = -1;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400190 int i;
191
Junghak Sung2d700712015-09-22 10:30:30 -0300192 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
193 if (!addr)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400194 goto finish_buf;
195
196 if (erase_off(solo_dev)) {
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300197 void *p = vb2_plane_vaddr(vb, 0);
Hans Verkuildcae5da2013-03-25 05:35:17 -0300198 int image_size = solo_image_size(solo_dev);
Hans Verkuil1c6f3db2014-07-17 20:40:22 -0300199
Hans Verkuildcae5da2013-03-25 05:35:17 -0300200 for (i = 0; i < image_size; i += 2) {
201 ((u8 *)p)[i] = 0x80;
202 ((u8 *)p)[i + 1] = 0x00;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400203 }
204 error = 0;
Hans Verkuildcae5da2013-03-25 05:35:17 -0300205 } else {
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300206 fdma_addr = SOLO_DISP_EXT_ADDR + (solo_dev->old_write *
Hans Verkuildcae5da2013-03-25 05:35:17 -0300207 (SOLO_HW_BPL * solo_vlines(solo_dev)));
208
Junghak Sung2d700712015-09-22 10:30:30 -0300209 error = solo_p2m_dma_t(solo_dev, 0, addr, fdma_addr,
Hans Verkuildcae5da2013-03-25 05:35:17 -0300210 solo_bytesperline(solo_dev),
211 solo_vlines(solo_dev), SOLO_HW_BPL);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400212 }
213
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400214finish_buf:
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300215 if (!error) {
216 vb2_set_plane_payload(vb, 0,
217 solo_vlines(solo_dev) * solo_bytesperline(solo_dev));
Junghak Sung2d700712015-09-22 10:30:30 -0300218 vbuf->sequence = solo_dev->sequence++;
Junghak Sungd6dd6452015-11-03 08:16:37 -0200219 vb->timestamp = ktime_get_ns();
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400220 }
221
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300222 vb2_buffer_done(vb, error ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400223}
224
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300225static void solo_thread_try(struct solo_dev *solo_dev)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400226{
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300227 struct solo_vb2_buf *vb;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400228
Hans Verkuildcae5da2013-03-25 05:35:17 -0300229 /* Only "break" from this loop if slock is held, otherwise
230 * just return. */
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400231 for (;;) {
Hans Verkuildcae5da2013-03-25 05:35:17 -0300232 unsigned int cur_write;
233
234 cur_write = SOLO_VI_STATUS0_PAGE(
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300235 solo_reg_read(solo_dev, SOLO_VI_STATUS0));
236 if (cur_write == solo_dev->old_write)
Hans Verkuildcae5da2013-03-25 05:35:17 -0300237 return;
238
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300239 spin_lock(&solo_dev->slock);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400240
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300241 if (list_empty(&solo_dev->vidq_active))
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400242 break;
243
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300244 vb = list_first_entry(&solo_dev->vidq_active, struct solo_vb2_buf,
245 list);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400246
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300247 solo_dev->old_write = cur_write;
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300248 list_del(&vb->list);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400249
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300250 spin_unlock(&solo_dev->slock);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400251
Junghak Sung2d700712015-09-22 10:30:30 -0300252 solo_fillbuf(solo_dev, &vb->vb.vb2_buf);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400253 }
254
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300255 assert_spin_locked(&solo_dev->slock);
256 spin_unlock(&solo_dev->slock);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400257}
258
259static int solo_thread(void *data)
260{
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300261 struct solo_dev *solo_dev = data;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400262 DECLARE_WAITQUEUE(wait, current);
263
264 set_freezable();
265 add_wait_queue(&solo_dev->disp_thread_wait, &wait);
266
267 for (;;) {
268 long timeout = schedule_timeout_interruptible(HZ);
Hans Verkuil1c6f3db2014-07-17 20:40:22 -0300269
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400270 if (timeout == -ERESTARTSYS || kthread_should_stop())
271 break;
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300272 solo_thread_try(solo_dev);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400273 try_to_freeze();
274 }
275
276 remove_wait_queue(&solo_dev->disp_thread_wait, &wait);
277
Ben Collinsf62de9b2010-11-04 22:51:17 -0400278 return 0;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400279}
280
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300281static int solo_start_thread(struct solo_dev *solo_dev)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400282{
Hans Verkuildcae5da2013-03-25 05:35:17 -0300283 int ret = 0;
284
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300285 solo_dev->kthread = kthread_run(solo_thread, solo_dev, SOLO6X10_NAME "_disp");
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400286
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300287 if (IS_ERR(solo_dev->kthread)) {
288 ret = PTR_ERR(solo_dev->kthread);
289 solo_dev->kthread = NULL;
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300290 return ret;
Hans Verkuildcae5da2013-03-25 05:35:17 -0300291 }
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300292 solo_irq_on(solo_dev, SOLO_IRQ_VIDEO_IN);
Hans Verkuildcae5da2013-03-25 05:35:17 -0300293
294 return ret;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400295}
296
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300297static void solo_stop_thread(struct solo_dev *solo_dev)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400298{
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300299 if (!solo_dev->kthread)
Hans Verkuildcae5da2013-03-25 05:35:17 -0300300 return;
301
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300302 solo_irq_off(solo_dev, SOLO_IRQ_VIDEO_IN);
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300303 kthread_stop(solo_dev->kthread);
304 solo_dev->kthread = NULL;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400305}
306
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200307static int solo_queue_setup(struct vb2_queue *q,
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300308 unsigned int *num_buffers, unsigned int *num_planes,
Hans Verkuil36c0f8b2016-04-15 09:15:05 -0300309 unsigned int sizes[], struct device *alloc_devs[])
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400310{
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300311 struct solo_dev *solo_dev = vb2_get_drv_priv(q);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400312
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300313 sizes[0] = solo_image_size(solo_dev);
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300314 *num_planes = 1;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400315
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300316 if (*num_buffers < MIN_VID_BUFFERS)
317 *num_buffers = MIN_VID_BUFFERS;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400318
Ben Collinsf62de9b2010-11-04 22:51:17 -0400319 return 0;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400320}
321
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300322static int solo_start_streaming(struct vb2_queue *q, unsigned int count)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400323{
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300324 struct solo_dev *solo_dev = vb2_get_drv_priv(q);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400325
Hans Verkuil15513c12013-03-15 13:16:49 -0300326 solo_dev->sequence = 0;
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300327 return solo_start_thread(solo_dev);
328}
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400329
Hans Verkuile37559b2014-04-17 02:47:21 -0300330static void solo_stop_streaming(struct vb2_queue *q)
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300331{
332 struct solo_dev *solo_dev = vb2_get_drv_priv(q);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400333
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300334 solo_stop_thread(solo_dev);
Anton Sviridenko6e4c8482017-03-09 10:46:18 -0300335
336 spin_lock(&solo_dev->slock);
337 while (!list_empty(&solo_dev->vidq_active)) {
338 struct solo_vb2_buf *buf = list_entry(
339 solo_dev->vidq_active.next,
340 struct solo_vb2_buf, list);
341
342 list_del(&buf->list);
343 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
344 }
345 spin_unlock(&solo_dev->slock);
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300346 INIT_LIST_HEAD(&solo_dev->vidq_active);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400347}
348
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300349static void solo_buf_queue(struct vb2_buffer *vb)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400350{
Junghak Sung2d700712015-09-22 10:30:30 -0300351 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300352 struct vb2_queue *vq = vb->vb2_queue;
353 struct solo_dev *solo_dev = vb2_get_drv_priv(vq);
354 struct solo_vb2_buf *solo_vb =
Junghak Sung2d700712015-09-22 10:30:30 -0300355 container_of(vbuf, struct solo_vb2_buf, vb);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400356
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300357 spin_lock(&solo_dev->slock);
358 list_add_tail(&solo_vb->list, &solo_dev->vidq_active);
359 spin_unlock(&solo_dev->slock);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400360 wake_up_interruptible(&solo_dev->disp_thread_wait);
361}
362
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300363static const struct vb2_ops solo_video_qops = {
364 .queue_setup = solo_queue_setup,
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400365 .buf_queue = solo_buf_queue,
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300366 .start_streaming = solo_start_streaming,
367 .stop_streaming = solo_stop_streaming,
368 .wait_prepare = vb2_ops_wait_prepare,
369 .wait_finish = vb2_ops_wait_finish,
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400370};
371
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400372static int solo_querycap(struct file *file, void *priv,
373 struct v4l2_capability *cap)
374{
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300375 struct solo_dev *solo_dev = video_drvdata(file);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400376
Mauro Carvalho Chehabcc1e6312018-09-10 16:20:42 -0400377 strscpy(cap->driver, SOLO6X10_NAME, sizeof(cap->driver));
378 strscpy(cap->card, "Softlogic 6x10", sizeof(cap->card));
Hans Verkuil20c5f492013-03-12 18:03:20 -0300379 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400380 pci_name(solo_dev->pdev));
Hans Verkuil20c5f492013-03-12 18:03:20 -0300381 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
382 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
383 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400384 return 0;
385}
386
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100387static int solo_enum_ext_input(struct solo_dev *solo_dev,
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400388 struct v4l2_input *input)
389{
Ismael Lucenof4bca742016-05-04 13:21:21 -0300390 int ext = input->index - solo_dev->nr_chans;
391 unsigned int nup, first;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400392
Ismael Lucenof4bca742016-05-04 13:21:21 -0300393 if (ext >= solo_dev->nr_ext)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400394 return -EINVAL;
395
Ismael Lucenof4bca742016-05-04 13:21:21 -0300396 nup = (ext == 4) ? 16 : 4;
397 first = (ext & 3) << 2; /* first channel in the n-up */
398 snprintf(input->name, sizeof(input->name),
399 "Multi %d-up (cameras %d-%d)",
400 nup, first + 1, first + nup);
401 /* Possible outputs:
402 * Multi 4-up (cameras 1-4)
403 * Multi 4-up (cameras 5-8)
404 * Multi 4-up (cameras 9-12)
405 * Multi 4-up (cameras 13-16)
406 * Multi 16-up (cameras 1-16)
407 */
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400408 return 0;
409}
410
411static int solo_enum_input(struct file *file, void *priv,
412 struct v4l2_input *input)
413{
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300414 struct solo_dev *solo_dev = video_drvdata(file);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400415
416 if (input->index >= solo_dev->nr_chans) {
417 int ret = solo_enum_ext_input(solo_dev, input);
Hans Verkuil1c6f3db2014-07-17 20:40:22 -0300418
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400419 if (ret < 0)
420 return ret;
421 } else {
422 snprintf(input->name, sizeof(input->name), "Camera %d",
423 input->index + 1);
424
425 /* We can only check this for normal inputs */
426 if (!tw28_get_video_status(solo_dev, input->index))
427 input->status = V4L2_IN_ST_NO_SIGNAL;
428 }
429
430 input->type = V4L2_INPUT_TYPE_CAMERA;
Hans Verkuil4c211ed2013-03-15 12:53:17 -0300431 input->std = solo_dev->vfd->tvnorms;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400432 return 0;
433}
434
435static int solo_set_input(struct file *file, void *priv, unsigned int index)
436{
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300437 struct solo_dev *solo_dev = video_drvdata(file);
438 int ret = solo_v4l2_set_ch(solo_dev, index);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400439
Hans Verkuildcae5da2013-03-25 05:35:17 -0300440 if (!ret) {
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300441 while (erase_off(solo_dev))
Hans Verkuildcae5da2013-03-25 05:35:17 -0300442 /* Do nothing */;
443 }
444
445 return ret;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400446}
447
448static int solo_get_input(struct file *file, void *priv, unsigned int *index)
449{
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300450 struct solo_dev *solo_dev = video_drvdata(file);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400451
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300452 *index = solo_dev->cur_disp_ch;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400453
454 return 0;
455}
456
457static int solo_enum_fmt_cap(struct file *file, void *priv,
458 struct v4l2_fmtdesc *f)
459{
460 if (f->index)
461 return -EINVAL;
462
463 f->pixelformat = V4L2_PIX_FMT_UYVY;
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -0400464 strscpy(f->description, "UYUV 4:2:2 Packed", sizeof(f->description));
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400465
466 return 0;
467}
468
469static int solo_try_fmt_cap(struct file *file, void *priv,
470 struct v4l2_format *f)
471{
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300472 struct solo_dev *solo_dev = video_drvdata(file);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400473 struct v4l2_pix_format *pix = &f->fmt.pix;
474 int image_size = solo_image_size(solo_dev);
475
Hans Verkuil016afda2013-03-12 18:43:21 -0300476 if (pix->pixelformat != V4L2_PIX_FMT_UYVY)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400477 return -EINVAL;
478
Hans Verkuil016afda2013-03-12 18:43:21 -0300479 pix->width = solo_dev->video_hsize;
480 pix->height = solo_vlines(solo_dev);
481 pix->sizeimage = image_size;
482 pix->field = V4L2_FIELD_INTERLACED;
483 pix->pixelformat = V4L2_PIX_FMT_UYVY;
484 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
485 pix->priv = 0;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400486 return 0;
487}
488
489static int solo_set_fmt_cap(struct file *file, void *priv,
490 struct v4l2_format *f)
491{
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300492 struct solo_dev *solo_dev = video_drvdata(file);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400493
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300494 if (vb2_is_busy(&solo_dev->vidq))
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400495 return -EBUSY;
496
497 /* For right now, if it doesn't match our running config,
498 * then fail */
499 return solo_try_fmt_cap(file, priv, f);
500}
501
502static int solo_get_fmt_cap(struct file *file, void *priv,
503 struct v4l2_format *f)
504{
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300505 struct solo_dev *solo_dev = video_drvdata(file);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400506 struct v4l2_pix_format *pix = &f->fmt.pix;
507
508 pix->width = solo_dev->video_hsize;
509 pix->height = solo_vlines(solo_dev);
510 pix->pixelformat = V4L2_PIX_FMT_UYVY;
Hans Verkuil016afda2013-03-12 18:43:21 -0300511 pix->field = V4L2_FIELD_INTERLACED;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400512 pix->sizeimage = solo_image_size(solo_dev);
513 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
514 pix->bytesperline = solo_bytesperline(solo_dev);
Hans Verkuil016afda2013-03-12 18:43:21 -0300515 pix->priv = 0;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400516
517 return 0;
518}
519
Hans Verkuil4c211ed2013-03-15 12:53:17 -0300520static int solo_g_std(struct file *file, void *priv, v4l2_std_id *i)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400521{
Hans Verkuil4c211ed2013-03-15 12:53:17 -0300522 struct solo_dev *solo_dev = video_drvdata(file);
523
524 if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC)
525 *i = V4L2_STD_NTSC_M;
526 else
527 *i = V4L2_STD_PAL;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400528 return 0;
529}
530
Hans Verkuil429df502014-01-13 06:58:12 -0300531int solo_set_video_type(struct solo_dev *solo_dev, bool is_50hz)
Hans Verkuil4c211ed2013-03-15 12:53:17 -0300532{
533 int i;
534
535 /* Make sure all video nodes are idle */
536 if (vb2_is_busy(&solo_dev->vidq))
537 return -EBUSY;
538 for (i = 0; i < solo_dev->nr_chans; i++)
539 if (vb2_is_busy(&solo_dev->v4l2_enc[i]->vidq))
540 return -EBUSY;
Hans Verkuil429df502014-01-13 06:58:12 -0300541 solo_dev->video_type = is_50hz ? SOLO_VO_FMT_TYPE_PAL :
542 SOLO_VO_FMT_TYPE_NTSC;
Hans Verkuil4c211ed2013-03-15 12:53:17 -0300543 /* Reconfigure for the new standard */
544 solo_disp_init(solo_dev);
545 solo_enc_init(solo_dev);
546 solo_tw28_init(solo_dev);
547 for (i = 0; i < solo_dev->nr_chans; i++)
548 solo_update_mode(solo_dev->v4l2_enc[i]);
549 return solo_v4l2_set_ch(solo_dev, solo_dev->cur_disp_ch);
550}
551
552static int solo_s_std(struct file *file, void *priv, v4l2_std_id std)
553{
554 struct solo_dev *solo_dev = video_drvdata(file);
555
Hans Verkuil429df502014-01-13 06:58:12 -0300556 return solo_set_video_type(solo_dev, std & V4L2_STD_625_50);
Hans Verkuil4c211ed2013-03-15 12:53:17 -0300557}
558
Hans Verkuilc813bd32013-03-25 05:38:14 -0300559static int solo_s_ctrl(struct v4l2_ctrl *ctrl)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400560{
Hans Verkuilc813bd32013-03-25 05:38:14 -0300561 struct solo_dev *solo_dev =
562 container_of(ctrl->handler, struct solo_dev, disp_hdl);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400563
564 switch (ctrl->id) {
565 case V4L2_CID_MOTION_TRACE:
Hans Verkuilc813bd32013-03-25 05:38:14 -0300566 if (ctrl->val) {
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400567 solo_reg_write(solo_dev, SOLO_VI_MOTION_BORDER,
568 SOLO_VI_MOTION_Y_ADD |
569 SOLO_VI_MOTION_Y_VALUE(0x20) |
570 SOLO_VI_MOTION_CB_VALUE(0x10) |
571 SOLO_VI_MOTION_CR_VALUE(0x10));
572 solo_reg_write(solo_dev, SOLO_VI_MOTION_BAR,
573 SOLO_VI_MOTION_CR_ADD |
574 SOLO_VI_MOTION_Y_VALUE(0x10) |
575 SOLO_VI_MOTION_CB_VALUE(0x80) |
576 SOLO_VI_MOTION_CR_VALUE(0x10));
577 } else {
578 solo_reg_write(solo_dev, SOLO_VI_MOTION_BORDER, 0);
579 solo_reg_write(solo_dev, SOLO_VI_MOTION_BAR, 0);
580 }
581 return 0;
Hans Verkuilc813bd32013-03-25 05:38:14 -0300582 default:
583 break;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400584 }
585 return -EINVAL;
586}
587
588static const struct v4l2_file_operations solo_v4l2_fops = {
589 .owner = THIS_MODULE,
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300590 .open = v4l2_fh_open,
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300591 .release = vb2_fop_release,
592 .read = vb2_fop_read,
593 .poll = vb2_fop_poll,
594 .mmap = vb2_fop_mmap,
595 .unlocked_ioctl = video_ioctl2,
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400596};
597
598static const struct v4l2_ioctl_ops solo_v4l2_ioctl_ops = {
599 .vidioc_querycap = solo_querycap,
600 .vidioc_s_std = solo_s_std,
Hans Verkuil4c211ed2013-03-15 12:53:17 -0300601 .vidioc_g_std = solo_g_std,
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400602 /* Input callbacks */
603 .vidioc_enum_input = solo_enum_input,
604 .vidioc_s_input = solo_set_input,
605 .vidioc_g_input = solo_get_input,
606 /* Video capture format callbacks */
607 .vidioc_enum_fmt_vid_cap = solo_enum_fmt_cap,
608 .vidioc_try_fmt_vid_cap = solo_try_fmt_cap,
609 .vidioc_s_fmt_vid_cap = solo_set_fmt_cap,
610 .vidioc_g_fmt_vid_cap = solo_get_fmt_cap,
611 /* Streaming I/O */
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300612 .vidioc_reqbufs = vb2_ioctl_reqbufs,
613 .vidioc_querybuf = vb2_ioctl_querybuf,
614 .vidioc_qbuf = vb2_ioctl_qbuf,
615 .vidioc_dqbuf = vb2_ioctl_dqbuf,
616 .vidioc_streamon = vb2_ioctl_streamon,
617 .vidioc_streamoff = vb2_ioctl_streamoff,
Hans Verkuil94160492013-03-12 18:47:03 -0300618 /* Logging and events */
619 .vidioc_log_status = v4l2_ctrl_log_status,
620 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
621 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400622};
623
Bhumika Goyal507e1902017-08-26 09:08:11 -0400624static const struct video_device solo_v4l2_template = {
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100625 .name = SOLO6X10_NAME,
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400626 .fops = &solo_v4l2_fops,
627 .ioctl_ops = &solo_v4l2_ioctl_ops,
628 .minor = -1,
629 .release = video_device_release,
Hans Verkuil4c211ed2013-03-15 12:53:17 -0300630 .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL,
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400631};
632
Hans Verkuilc813bd32013-03-25 05:38:14 -0300633static const struct v4l2_ctrl_ops solo_ctrl_ops = {
634 .s_ctrl = solo_s_ctrl,
635};
636
637static const struct v4l2_ctrl_config solo_motion_trace_ctrl = {
638 .ops = &solo_ctrl_ops,
639 .id = V4L2_CID_MOTION_TRACE,
640 .name = "Motion Detection Trace",
641 .type = V4L2_CTRL_TYPE_BOOLEAN,
642 .max = 1,
643 .step = 1,
644};
645
Hans Verkuildcae5da2013-03-25 05:35:17 -0300646int solo_v4l2_init(struct solo_dev *solo_dev, unsigned nr)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400647{
648 int ret;
649 int i;
650
651 init_waitqueue_head(&solo_dev->disp_thread_wait);
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300652 spin_lock_init(&solo_dev->slock);
653 mutex_init(&solo_dev->lock);
654 INIT_LIST_HEAD(&solo_dev->vidq_active);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400655
656 solo_dev->vfd = video_device_alloc();
657 if (!solo_dev->vfd)
658 return -ENOMEM;
659
660 *solo_dev->vfd = solo_v4l2_template;
Hans Verkuild9ebd622013-03-25 05:36:44 -0300661 solo_dev->vfd->v4l2_dev = &solo_dev->v4l2_dev;
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300662 solo_dev->vfd->queue = &solo_dev->vidq;
663 solo_dev->vfd->lock = &solo_dev->lock;
Hans Verkuilc813bd32013-03-25 05:38:14 -0300664 v4l2_ctrl_handler_init(&solo_dev->disp_hdl, 1);
665 v4l2_ctrl_new_custom(&solo_dev->disp_hdl, &solo_motion_trace_ctrl, NULL);
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300666 if (solo_dev->disp_hdl.error) {
667 ret = solo_dev->disp_hdl.error;
668 goto fail;
669 }
Hans Verkuilc813bd32013-03-25 05:38:14 -0300670 solo_dev->vfd->ctrl_handler = &solo_dev->disp_hdl;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400671
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400672 video_set_drvdata(solo_dev->vfd, solo_dev);
673
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300674 solo_dev->vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
675 solo_dev->vidq.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
676 solo_dev->vidq.ops = &solo_video_qops;
677 solo_dev->vidq.mem_ops = &vb2_dma_contig_memops;
678 solo_dev->vidq.drv_priv = solo_dev;
Sakari Ailusade48682014-02-25 19:12:19 -0300679 solo_dev->vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
Mel Gormand0164ad2015-11-06 16:28:21 -0800680 solo_dev->vidq.gfp_flags = __GFP_DMA32 | __GFP_KSWAPD_RECLAIM;
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300681 solo_dev->vidq.buf_struct_size = sizeof(struct solo_vb2_buf);
682 solo_dev->vidq.lock = &solo_dev->lock;
Hans Verkuil2bc46b32016-02-15 12:37:15 -0200683 solo_dev->vidq.dev = &solo_dev->pdev->dev;
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300684 ret = vb2_queue_init(&solo_dev->vidq);
685 if (ret < 0)
686 goto fail;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400687
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400688 /* Cycle all the channels and clear */
689 for (i = 0; i < solo_dev->nr_chans; i++) {
690 solo_v4l2_set_ch(solo_dev, i);
691 while (erase_off(solo_dev))
Hans Verkuildcae5da2013-03-25 05:35:17 -0300692 /* Do nothing */;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400693 }
694
695 /* Set the default display channel */
696 solo_v4l2_set_ch(solo_dev, 0);
697 while (erase_off(solo_dev))
Hans Verkuildcae5da2013-03-25 05:35:17 -0300698 /* Do nothing */;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400699
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300700 ret = video_register_device(solo_dev->vfd, VFL_TYPE_GRABBER, nr);
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300701 if (ret < 0)
702 goto fail;
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300703
704 snprintf(solo_dev->vfd->name, sizeof(solo_dev->vfd->name), "%s (%i)",
705 SOLO6X10_NAME, solo_dev->vfd->num);
706
Mauro Carvalho Chehab3d160042016-10-18 17:44:07 -0200707 dev_info(&solo_dev->pdev->dev, "Display as /dev/video%d with %d inputs (%d extended)\n",
708 solo_dev->vfd->num,
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300709 solo_dev->nr_chans, solo_dev->nr_ext);
710
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400711 return 0;
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300712
713fail:
714 video_device_release(solo_dev->vfd);
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300715 v4l2_ctrl_handler_free(&solo_dev->disp_hdl);
716 solo_dev->vfd = NULL;
717 return ret;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400718}
719
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100720void solo_v4l2_exit(struct solo_dev *solo_dev)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400721{
Hans Verkuildcae5da2013-03-25 05:35:17 -0300722 if (solo_dev->vfd == NULL)
723 return;
724
725 video_unregister_device(solo_dev->vfd);
Hans Verkuilc813bd32013-03-25 05:38:14 -0300726 v4l2_ctrl_handler_free(&solo_dev->disp_hdl);
Hans Verkuildcae5da2013-03-25 05:35:17 -0300727 solo_dev->vfd = NULL;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400728}