blob: 69fc939fd3d9870eeb77e140e4430a5eb41c4423 [file] [log] [blame]
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001/*
Hans Verkuildcae5da2013-03-25 05:35:17 -03002 * Copyright (C) 2010-2013 Bluecherry, LLC <http://www.bluecherrydvr.com>
3 *
4 * Original author:
5 * Ben Collins <bcollins@ubuntu.com>
6 *
7 * Additional work by:
8 * John Brooks <john.brooks@bluecherry.net>
Ben Collinsfaa4fd22010-06-17 13:27:26 -04009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
Ben Collinsfaa4fd22010-06-17 13:27:26 -040019 */
20
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/kthread.h>
24#include <linux/freezer.h>
Hans Verkuildcae5da2013-03-25 05:35:17 -030025
Ben Collinsfaa4fd22010-06-17 13:27:26 -040026#include <media/v4l2-ioctl.h>
27#include <media/v4l2-common.h>
Hans Verkuil94160492013-03-12 18:47:03 -030028#include <media/v4l2-event.h>
Junghak Sung2d700712015-09-22 10:30:30 -030029#include <media/videobuf2-v4l2.h>
Hans Verkuila4056c2f2013-03-15 12:11:17 -030030#include <media/videobuf2-dma-contig.h>
Hans Verkuildcae5da2013-03-25 05:35:17 -030031
Krzysztof Hałasaae69b222011-02-11 13:36:27 +010032#include "solo6x10.h"
Hans Verkuildad7fab2013-03-25 05:42:46 -030033#include "solo6x10-tw28.h"
Ben Collinsfaa4fd22010-06-17 13:27:26 -040034
Hans Verkuildcae5da2013-03-25 05:35:17 -030035/* Image size is two fields, SOLO_HW_BPL is one horizontal line in hardware */
36#define SOLO_HW_BPL 2048
Ben Collinsfaa4fd22010-06-17 13:27:26 -040037#define solo_vlines(__solo) (__solo->video_vsize * 2)
38#define solo_image_size(__solo) (solo_bytesperline(__solo) * \
39 solo_vlines(__solo))
40#define solo_bytesperline(__solo) (__solo->video_hsize * 2)
41
Hans Verkuildcae5da2013-03-25 05:35:17 -030042#define MIN_VID_BUFFERS 2
Ben Collinsfaa4fd22010-06-17 13:27:26 -040043
Hans Verkuildcae5da2013-03-25 05:35:17 -030044static inline void erase_on(struct solo_dev *solo_dev)
Ben Collinsfaa4fd22010-06-17 13:27:26 -040045{
46 solo_reg_write(solo_dev, SOLO_VO_DISP_ERASE, SOLO_VO_DISP_ERASE_ON);
47 solo_dev->erasing = 1;
48 solo_dev->frame_blank = 0;
49}
50
Hans Verkuildcae5da2013-03-25 05:35:17 -030051static inline int erase_off(struct solo_dev *solo_dev)
Ben Collinsfaa4fd22010-06-17 13:27:26 -040052{
53 if (!solo_dev->erasing)
54 return 0;
55
56 /* First time around, assert erase off */
57 if (!solo_dev->frame_blank)
58 solo_reg_write(solo_dev, SOLO_VO_DISP_ERASE, 0);
59 /* Keep the erasing flag on for 8 frames minimum */
60 if (solo_dev->frame_blank++ >= 8)
61 solo_dev->erasing = 0;
62
63 return 1;
64}
65
Krzysztof Hałasadecebab2011-02-11 13:38:20 +010066void solo_video_in_isr(struct solo_dev *solo_dev)
Ben Collinsfaa4fd22010-06-17 13:27:26 -040067{
Hans Verkuildcae5da2013-03-25 05:35:17 -030068 wake_up_interruptible_all(&solo_dev->disp_thread_wait);
Ben Collinsfaa4fd22010-06-17 13:27:26 -040069}
70
Krzysztof Hałasadecebab2011-02-11 13:38:20 +010071static void solo_win_setup(struct solo_dev *solo_dev, u8 ch,
Ben Collinsfaa4fd22010-06-17 13:27:26 -040072 int sx, int sy, int ex, int ey, int scale)
73{
74 if (ch >= solo_dev->nr_chans)
75 return;
76
77 /* Here, we just keep window/channel the same */
78 solo_reg_write(solo_dev, SOLO_VI_WIN_CTRL0(ch),
79 SOLO_VI_WIN_CHANNEL(ch) |
80 SOLO_VI_WIN_SX(sx) |
81 SOLO_VI_WIN_EX(ex) |
82 SOLO_VI_WIN_SCALE(scale));
83
Ben Collinsf62de9b2010-11-04 22:51:17 -040084 solo_reg_write(solo_dev, SOLO_VI_WIN_CTRL1(ch),
Ben Collinsfaa4fd22010-06-17 13:27:26 -040085 SOLO_VI_WIN_SY(sy) |
86 SOLO_VI_WIN_EY(ey));
87}
88
Krzysztof Hałasadecebab2011-02-11 13:38:20 +010089static int solo_v4l2_ch_ext_4up(struct solo_dev *solo_dev, u8 idx, int on)
Ben Collinsfaa4fd22010-06-17 13:27:26 -040090{
91 u8 ch = idx * 4;
92
93 if (ch >= solo_dev->nr_chans)
94 return -EINVAL;
95
96 if (!on) {
97 u8 i;
Hans Verkuil1c6f3db2014-07-17 20:40:22 -030098
Ben Collinsfaa4fd22010-06-17 13:27:26 -040099 for (i = ch; i < ch + 4; i++)
100 solo_win_setup(solo_dev, i, solo_dev->video_hsize,
101 solo_vlines(solo_dev),
102 solo_dev->video_hsize,
103 solo_vlines(solo_dev), 0);
104 return 0;
105 }
106
107 /* Row 1 */
108 solo_win_setup(solo_dev, ch, 0, 0, solo_dev->video_hsize / 2,
109 solo_vlines(solo_dev) / 2, 3);
110 solo_win_setup(solo_dev, ch + 1, solo_dev->video_hsize / 2, 0,
111 solo_dev->video_hsize, solo_vlines(solo_dev) / 2, 3);
112 /* Row 2 */
113 solo_win_setup(solo_dev, ch + 2, 0, solo_vlines(solo_dev) / 2,
114 solo_dev->video_hsize / 2, solo_vlines(solo_dev), 3);
115 solo_win_setup(solo_dev, ch + 3, solo_dev->video_hsize / 2,
116 solo_vlines(solo_dev) / 2, solo_dev->video_hsize,
117 solo_vlines(solo_dev), 3);
118
119 return 0;
120}
121
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100122static int solo_v4l2_ch_ext_16up(struct solo_dev *solo_dev, int on)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400123{
124 int sy, ysize, hsize, i;
125
126 if (!on) {
127 for (i = 0; i < 16; i++)
128 solo_win_setup(solo_dev, i, solo_dev->video_hsize,
129 solo_vlines(solo_dev),
130 solo_dev->video_hsize,
131 solo_vlines(solo_dev), 0);
132 return 0;
133 }
134
135 ysize = solo_vlines(solo_dev) / 4;
136 hsize = solo_dev->video_hsize / 4;
137
138 for (sy = 0, i = 0; i < 4; i++, sy += ysize) {
139 solo_win_setup(solo_dev, i * 4, 0, sy, hsize,
140 sy + ysize, 5);
141 solo_win_setup(solo_dev, (i * 4) + 1, hsize, sy,
142 hsize * 2, sy + ysize, 5);
143 solo_win_setup(solo_dev, (i * 4) + 2, hsize * 2, sy,
144 hsize * 3, sy + ysize, 5);
145 solo_win_setup(solo_dev, (i * 4) + 3, hsize * 3, sy,
146 solo_dev->video_hsize, sy + ysize, 5);
147 }
148
149 return 0;
150}
151
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100152static int solo_v4l2_ch(struct solo_dev *solo_dev, u8 ch, int on)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400153{
154 u8 ext_ch;
155
156 if (ch < solo_dev->nr_chans) {
157 solo_win_setup(solo_dev, ch, on ? 0 : solo_dev->video_hsize,
158 on ? 0 : solo_vlines(solo_dev),
159 solo_dev->video_hsize, solo_vlines(solo_dev),
160 on ? 1 : 0);
161 return 0;
162 }
163
164 if (ch >= solo_dev->nr_chans + solo_dev->nr_ext)
165 return -EINVAL;
166
167 ext_ch = ch - solo_dev->nr_chans;
168
169 /* 4up's first */
170 if (ext_ch < 4)
171 return solo_v4l2_ch_ext_4up(solo_dev, ext_ch, on);
172
173 /* Remaining case is 16up for 16-port */
174 return solo_v4l2_ch_ext_16up(solo_dev, on);
175}
176
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100177static int solo_v4l2_set_ch(struct solo_dev *solo_dev, u8 ch)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400178{
179 if (ch >= solo_dev->nr_chans + solo_dev->nr_ext)
180 return -EINVAL;
181
182 erase_on(solo_dev);
183
184 solo_v4l2_ch(solo_dev, solo_dev->cur_disp_ch, 0);
185 solo_v4l2_ch(solo_dev, ch, 1);
186
187 solo_dev->cur_disp_ch = ch;
188
189 return 0;
190}
191
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300192static void solo_fillbuf(struct solo_dev *solo_dev,
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300193 struct vb2_buffer *vb)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400194{
Junghak Sung2d700712015-09-22 10:30:30 -0300195 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
196 dma_addr_t addr;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400197 unsigned int fdma_addr;
Hans Verkuildcae5da2013-03-25 05:35:17 -0300198 int error = -1;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400199 int i;
200
Junghak Sung2d700712015-09-22 10:30:30 -0300201 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
202 if (!addr)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400203 goto finish_buf;
204
205 if (erase_off(solo_dev)) {
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300206 void *p = vb2_plane_vaddr(vb, 0);
Hans Verkuildcae5da2013-03-25 05:35:17 -0300207 int image_size = solo_image_size(solo_dev);
Hans Verkuil1c6f3db2014-07-17 20:40:22 -0300208
Hans Verkuildcae5da2013-03-25 05:35:17 -0300209 for (i = 0; i < image_size; i += 2) {
210 ((u8 *)p)[i] = 0x80;
211 ((u8 *)p)[i + 1] = 0x00;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400212 }
213 error = 0;
Hans Verkuildcae5da2013-03-25 05:35:17 -0300214 } else {
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300215 fdma_addr = SOLO_DISP_EXT_ADDR + (solo_dev->old_write *
Hans Verkuildcae5da2013-03-25 05:35:17 -0300216 (SOLO_HW_BPL * solo_vlines(solo_dev)));
217
Junghak Sung2d700712015-09-22 10:30:30 -0300218 error = solo_p2m_dma_t(solo_dev, 0, addr, fdma_addr,
Hans Verkuildcae5da2013-03-25 05:35:17 -0300219 solo_bytesperline(solo_dev),
220 solo_vlines(solo_dev), SOLO_HW_BPL);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400221 }
222
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400223finish_buf:
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300224 if (!error) {
225 vb2_set_plane_payload(vb, 0,
226 solo_vlines(solo_dev) * solo_bytesperline(solo_dev));
Junghak Sung2d700712015-09-22 10:30:30 -0300227 vbuf->sequence = solo_dev->sequence++;
Junghak Sungd6dd6452015-11-03 08:16:37 -0200228 vb->timestamp = ktime_get_ns();
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400229 }
230
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300231 vb2_buffer_done(vb, error ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400232}
233
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300234static void solo_thread_try(struct solo_dev *solo_dev)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400235{
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300236 struct solo_vb2_buf *vb;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400237
Hans Verkuildcae5da2013-03-25 05:35:17 -0300238 /* Only "break" from this loop if slock is held, otherwise
239 * just return. */
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400240 for (;;) {
Hans Verkuildcae5da2013-03-25 05:35:17 -0300241 unsigned int cur_write;
242
243 cur_write = SOLO_VI_STATUS0_PAGE(
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300244 solo_reg_read(solo_dev, SOLO_VI_STATUS0));
245 if (cur_write == solo_dev->old_write)
Hans Verkuildcae5da2013-03-25 05:35:17 -0300246 return;
247
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300248 spin_lock(&solo_dev->slock);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400249
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300250 if (list_empty(&solo_dev->vidq_active))
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400251 break;
252
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300253 vb = list_first_entry(&solo_dev->vidq_active, struct solo_vb2_buf,
254 list);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400255
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300256 solo_dev->old_write = cur_write;
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300257 list_del(&vb->list);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400258
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300259 spin_unlock(&solo_dev->slock);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400260
Junghak Sung2d700712015-09-22 10:30:30 -0300261 solo_fillbuf(solo_dev, &vb->vb.vb2_buf);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400262 }
263
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300264 assert_spin_locked(&solo_dev->slock);
265 spin_unlock(&solo_dev->slock);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400266}
267
268static int solo_thread(void *data)
269{
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300270 struct solo_dev *solo_dev = data;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400271 DECLARE_WAITQUEUE(wait, current);
272
273 set_freezable();
274 add_wait_queue(&solo_dev->disp_thread_wait, &wait);
275
276 for (;;) {
277 long timeout = schedule_timeout_interruptible(HZ);
Hans Verkuil1c6f3db2014-07-17 20:40:22 -0300278
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400279 if (timeout == -ERESTARTSYS || kthread_should_stop())
280 break;
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300281 solo_thread_try(solo_dev);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400282 try_to_freeze();
283 }
284
285 remove_wait_queue(&solo_dev->disp_thread_wait, &wait);
286
Ben Collinsf62de9b2010-11-04 22:51:17 -0400287 return 0;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400288}
289
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300290static int solo_start_thread(struct solo_dev *solo_dev)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400291{
Hans Verkuildcae5da2013-03-25 05:35:17 -0300292 int ret = 0;
293
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300294 solo_dev->kthread = kthread_run(solo_thread, solo_dev, SOLO6X10_NAME "_disp");
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400295
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300296 if (IS_ERR(solo_dev->kthread)) {
297 ret = PTR_ERR(solo_dev->kthread);
298 solo_dev->kthread = NULL;
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300299 return ret;
Hans Verkuildcae5da2013-03-25 05:35:17 -0300300 }
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300301 solo_irq_on(solo_dev, SOLO_IRQ_VIDEO_IN);
Hans Verkuildcae5da2013-03-25 05:35:17 -0300302
303 return ret;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400304}
305
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300306static void solo_stop_thread(struct solo_dev *solo_dev)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400307{
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300308 if (!solo_dev->kthread)
Hans Verkuildcae5da2013-03-25 05:35:17 -0300309 return;
310
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300311 solo_irq_off(solo_dev, SOLO_IRQ_VIDEO_IN);
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300312 kthread_stop(solo_dev->kthread);
313 solo_dev->kthread = NULL;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400314}
315
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200316static int solo_queue_setup(struct vb2_queue *q,
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300317 unsigned int *num_buffers, unsigned int *num_planes,
Hans Verkuil36c0f8b2016-04-15 09:15:05 -0300318 unsigned int sizes[], struct device *alloc_devs[])
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400319{
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300320 struct solo_dev *solo_dev = vb2_get_drv_priv(q);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400321
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300322 sizes[0] = solo_image_size(solo_dev);
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300323 *num_planes = 1;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400324
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300325 if (*num_buffers < MIN_VID_BUFFERS)
326 *num_buffers = MIN_VID_BUFFERS;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400327
Ben Collinsf62de9b2010-11-04 22:51:17 -0400328 return 0;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400329}
330
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300331static int solo_start_streaming(struct vb2_queue *q, unsigned int count)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400332{
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300333 struct solo_dev *solo_dev = vb2_get_drv_priv(q);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400334
Hans Verkuil15513c12013-03-15 13:16:49 -0300335 solo_dev->sequence = 0;
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300336 return solo_start_thread(solo_dev);
337}
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400338
Hans Verkuile37559b2014-04-17 02:47:21 -0300339static void solo_stop_streaming(struct vb2_queue *q)
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300340{
341 struct solo_dev *solo_dev = vb2_get_drv_priv(q);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400342
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300343 solo_stop_thread(solo_dev);
Anton Sviridenko6e4c8482017-03-09 10:46:18 -0300344
345 spin_lock(&solo_dev->slock);
346 while (!list_empty(&solo_dev->vidq_active)) {
347 struct solo_vb2_buf *buf = list_entry(
348 solo_dev->vidq_active.next,
349 struct solo_vb2_buf, list);
350
351 list_del(&buf->list);
352 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
353 }
354 spin_unlock(&solo_dev->slock);
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300355 INIT_LIST_HEAD(&solo_dev->vidq_active);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400356}
357
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300358static void solo_buf_queue(struct vb2_buffer *vb)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400359{
Junghak Sung2d700712015-09-22 10:30:30 -0300360 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300361 struct vb2_queue *vq = vb->vb2_queue;
362 struct solo_dev *solo_dev = vb2_get_drv_priv(vq);
363 struct solo_vb2_buf *solo_vb =
Junghak Sung2d700712015-09-22 10:30:30 -0300364 container_of(vbuf, struct solo_vb2_buf, vb);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400365
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300366 spin_lock(&solo_dev->slock);
367 list_add_tail(&solo_vb->list, &solo_dev->vidq_active);
368 spin_unlock(&solo_dev->slock);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400369 wake_up_interruptible(&solo_dev->disp_thread_wait);
370}
371
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300372static const struct vb2_ops solo_video_qops = {
373 .queue_setup = solo_queue_setup,
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400374 .buf_queue = solo_buf_queue,
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300375 .start_streaming = solo_start_streaming,
376 .stop_streaming = solo_stop_streaming,
377 .wait_prepare = vb2_ops_wait_prepare,
378 .wait_finish = vb2_ops_wait_finish,
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400379};
380
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400381static int solo_querycap(struct file *file, void *priv,
382 struct v4l2_capability *cap)
383{
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300384 struct solo_dev *solo_dev = video_drvdata(file);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400385
Mauro Carvalho Chehabcc1e6312018-09-10 16:20:42 -0400386 strscpy(cap->driver, SOLO6X10_NAME, sizeof(cap->driver));
387 strscpy(cap->card, "Softlogic 6x10", sizeof(cap->card));
Hans Verkuil20c5f492013-03-12 18:03:20 -0300388 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400389 pci_name(solo_dev->pdev));
Hans Verkuil20c5f492013-03-12 18:03:20 -0300390 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
391 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
392 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400393 return 0;
394}
395
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100396static int solo_enum_ext_input(struct solo_dev *solo_dev,
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400397 struct v4l2_input *input)
398{
Ismael Lucenof4bca742016-05-04 13:21:21 -0300399 int ext = input->index - solo_dev->nr_chans;
400 unsigned int nup, first;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400401
Ismael Lucenof4bca742016-05-04 13:21:21 -0300402 if (ext >= solo_dev->nr_ext)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400403 return -EINVAL;
404
Ismael Lucenof4bca742016-05-04 13:21:21 -0300405 nup = (ext == 4) ? 16 : 4;
406 first = (ext & 3) << 2; /* first channel in the n-up */
407 snprintf(input->name, sizeof(input->name),
408 "Multi %d-up (cameras %d-%d)",
409 nup, first + 1, first + nup);
410 /* Possible outputs:
411 * Multi 4-up (cameras 1-4)
412 * Multi 4-up (cameras 5-8)
413 * Multi 4-up (cameras 9-12)
414 * Multi 4-up (cameras 13-16)
415 * Multi 16-up (cameras 1-16)
416 */
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400417 return 0;
418}
419
420static int solo_enum_input(struct file *file, void *priv,
421 struct v4l2_input *input)
422{
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300423 struct solo_dev *solo_dev = video_drvdata(file);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400424
425 if (input->index >= solo_dev->nr_chans) {
426 int ret = solo_enum_ext_input(solo_dev, input);
Hans Verkuil1c6f3db2014-07-17 20:40:22 -0300427
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400428 if (ret < 0)
429 return ret;
430 } else {
431 snprintf(input->name, sizeof(input->name), "Camera %d",
432 input->index + 1);
433
434 /* We can only check this for normal inputs */
435 if (!tw28_get_video_status(solo_dev, input->index))
436 input->status = V4L2_IN_ST_NO_SIGNAL;
437 }
438
439 input->type = V4L2_INPUT_TYPE_CAMERA;
Hans Verkuil4c211ed2013-03-15 12:53:17 -0300440 input->std = solo_dev->vfd->tvnorms;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400441 return 0;
442}
443
444static int solo_set_input(struct file *file, void *priv, unsigned int index)
445{
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300446 struct solo_dev *solo_dev = video_drvdata(file);
447 int ret = solo_v4l2_set_ch(solo_dev, index);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400448
Hans Verkuildcae5da2013-03-25 05:35:17 -0300449 if (!ret) {
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300450 while (erase_off(solo_dev))
Hans Verkuildcae5da2013-03-25 05:35:17 -0300451 /* Do nothing */;
452 }
453
454 return ret;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400455}
456
457static int solo_get_input(struct file *file, void *priv, unsigned int *index)
458{
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300459 struct solo_dev *solo_dev = video_drvdata(file);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400460
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300461 *index = solo_dev->cur_disp_ch;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400462
463 return 0;
464}
465
466static int solo_enum_fmt_cap(struct file *file, void *priv,
467 struct v4l2_fmtdesc *f)
468{
469 if (f->index)
470 return -EINVAL;
471
472 f->pixelformat = V4L2_PIX_FMT_UYVY;
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -0400473 strscpy(f->description, "UYUV 4:2:2 Packed", sizeof(f->description));
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400474
475 return 0;
476}
477
478static int solo_try_fmt_cap(struct file *file, void *priv,
479 struct v4l2_format *f)
480{
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300481 struct solo_dev *solo_dev = video_drvdata(file);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400482 struct v4l2_pix_format *pix = &f->fmt.pix;
483 int image_size = solo_image_size(solo_dev);
484
Hans Verkuil016afda2013-03-12 18:43:21 -0300485 if (pix->pixelformat != V4L2_PIX_FMT_UYVY)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400486 return -EINVAL;
487
Hans Verkuil016afda2013-03-12 18:43:21 -0300488 pix->width = solo_dev->video_hsize;
489 pix->height = solo_vlines(solo_dev);
490 pix->sizeimage = image_size;
491 pix->field = V4L2_FIELD_INTERLACED;
492 pix->pixelformat = V4L2_PIX_FMT_UYVY;
493 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
494 pix->priv = 0;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400495 return 0;
496}
497
498static int solo_set_fmt_cap(struct file *file, void *priv,
499 struct v4l2_format *f)
500{
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300501 struct solo_dev *solo_dev = video_drvdata(file);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400502
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300503 if (vb2_is_busy(&solo_dev->vidq))
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400504 return -EBUSY;
505
506 /* For right now, if it doesn't match our running config,
507 * then fail */
508 return solo_try_fmt_cap(file, priv, f);
509}
510
511static int solo_get_fmt_cap(struct file *file, void *priv,
512 struct v4l2_format *f)
513{
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300514 struct solo_dev *solo_dev = video_drvdata(file);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400515 struct v4l2_pix_format *pix = &f->fmt.pix;
516
517 pix->width = solo_dev->video_hsize;
518 pix->height = solo_vlines(solo_dev);
519 pix->pixelformat = V4L2_PIX_FMT_UYVY;
Hans Verkuil016afda2013-03-12 18:43:21 -0300520 pix->field = V4L2_FIELD_INTERLACED;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400521 pix->sizeimage = solo_image_size(solo_dev);
522 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
523 pix->bytesperline = solo_bytesperline(solo_dev);
Hans Verkuil016afda2013-03-12 18:43:21 -0300524 pix->priv = 0;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400525
526 return 0;
527}
528
Hans Verkuil4c211ed2013-03-15 12:53:17 -0300529static int solo_g_std(struct file *file, void *priv, v4l2_std_id *i)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400530{
Hans Verkuil4c211ed2013-03-15 12:53:17 -0300531 struct solo_dev *solo_dev = video_drvdata(file);
532
533 if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC)
534 *i = V4L2_STD_NTSC_M;
535 else
536 *i = V4L2_STD_PAL;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400537 return 0;
538}
539
Hans Verkuil429df502014-01-13 06:58:12 -0300540int solo_set_video_type(struct solo_dev *solo_dev, bool is_50hz)
Hans Verkuil4c211ed2013-03-15 12:53:17 -0300541{
542 int i;
543
544 /* Make sure all video nodes are idle */
545 if (vb2_is_busy(&solo_dev->vidq))
546 return -EBUSY;
547 for (i = 0; i < solo_dev->nr_chans; i++)
548 if (vb2_is_busy(&solo_dev->v4l2_enc[i]->vidq))
549 return -EBUSY;
Hans Verkuil429df502014-01-13 06:58:12 -0300550 solo_dev->video_type = is_50hz ? SOLO_VO_FMT_TYPE_PAL :
551 SOLO_VO_FMT_TYPE_NTSC;
Hans Verkuil4c211ed2013-03-15 12:53:17 -0300552 /* Reconfigure for the new standard */
553 solo_disp_init(solo_dev);
554 solo_enc_init(solo_dev);
555 solo_tw28_init(solo_dev);
556 for (i = 0; i < solo_dev->nr_chans; i++)
557 solo_update_mode(solo_dev->v4l2_enc[i]);
558 return solo_v4l2_set_ch(solo_dev, solo_dev->cur_disp_ch);
559}
560
561static int solo_s_std(struct file *file, void *priv, v4l2_std_id std)
562{
563 struct solo_dev *solo_dev = video_drvdata(file);
564
Hans Verkuil429df502014-01-13 06:58:12 -0300565 return solo_set_video_type(solo_dev, std & V4L2_STD_625_50);
Hans Verkuil4c211ed2013-03-15 12:53:17 -0300566}
567
Hans Verkuilc813bd32013-03-25 05:38:14 -0300568static int solo_s_ctrl(struct v4l2_ctrl *ctrl)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400569{
Hans Verkuilc813bd32013-03-25 05:38:14 -0300570 struct solo_dev *solo_dev =
571 container_of(ctrl->handler, struct solo_dev, disp_hdl);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400572
573 switch (ctrl->id) {
574 case V4L2_CID_MOTION_TRACE:
Hans Verkuilc813bd32013-03-25 05:38:14 -0300575 if (ctrl->val) {
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400576 solo_reg_write(solo_dev, SOLO_VI_MOTION_BORDER,
577 SOLO_VI_MOTION_Y_ADD |
578 SOLO_VI_MOTION_Y_VALUE(0x20) |
579 SOLO_VI_MOTION_CB_VALUE(0x10) |
580 SOLO_VI_MOTION_CR_VALUE(0x10));
581 solo_reg_write(solo_dev, SOLO_VI_MOTION_BAR,
582 SOLO_VI_MOTION_CR_ADD |
583 SOLO_VI_MOTION_Y_VALUE(0x10) |
584 SOLO_VI_MOTION_CB_VALUE(0x80) |
585 SOLO_VI_MOTION_CR_VALUE(0x10));
586 } else {
587 solo_reg_write(solo_dev, SOLO_VI_MOTION_BORDER, 0);
588 solo_reg_write(solo_dev, SOLO_VI_MOTION_BAR, 0);
589 }
590 return 0;
Hans Verkuilc813bd32013-03-25 05:38:14 -0300591 default:
592 break;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400593 }
594 return -EINVAL;
595}
596
597static const struct v4l2_file_operations solo_v4l2_fops = {
598 .owner = THIS_MODULE,
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300599 .open = v4l2_fh_open,
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300600 .release = vb2_fop_release,
601 .read = vb2_fop_read,
602 .poll = vb2_fop_poll,
603 .mmap = vb2_fop_mmap,
604 .unlocked_ioctl = video_ioctl2,
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400605};
606
607static const struct v4l2_ioctl_ops solo_v4l2_ioctl_ops = {
608 .vidioc_querycap = solo_querycap,
609 .vidioc_s_std = solo_s_std,
Hans Verkuil4c211ed2013-03-15 12:53:17 -0300610 .vidioc_g_std = solo_g_std,
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400611 /* Input callbacks */
612 .vidioc_enum_input = solo_enum_input,
613 .vidioc_s_input = solo_set_input,
614 .vidioc_g_input = solo_get_input,
615 /* Video capture format callbacks */
616 .vidioc_enum_fmt_vid_cap = solo_enum_fmt_cap,
617 .vidioc_try_fmt_vid_cap = solo_try_fmt_cap,
618 .vidioc_s_fmt_vid_cap = solo_set_fmt_cap,
619 .vidioc_g_fmt_vid_cap = solo_get_fmt_cap,
620 /* Streaming I/O */
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300621 .vidioc_reqbufs = vb2_ioctl_reqbufs,
622 .vidioc_querybuf = vb2_ioctl_querybuf,
623 .vidioc_qbuf = vb2_ioctl_qbuf,
624 .vidioc_dqbuf = vb2_ioctl_dqbuf,
625 .vidioc_streamon = vb2_ioctl_streamon,
626 .vidioc_streamoff = vb2_ioctl_streamoff,
Hans Verkuil94160492013-03-12 18:47:03 -0300627 /* Logging and events */
628 .vidioc_log_status = v4l2_ctrl_log_status,
629 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
630 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400631};
632
Bhumika Goyal507e1902017-08-26 09:08:11 -0400633static const struct video_device solo_v4l2_template = {
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100634 .name = SOLO6X10_NAME,
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400635 .fops = &solo_v4l2_fops,
636 .ioctl_ops = &solo_v4l2_ioctl_ops,
637 .minor = -1,
638 .release = video_device_release,
Hans Verkuil4c211ed2013-03-15 12:53:17 -0300639 .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL,
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400640};
641
Hans Verkuilc813bd32013-03-25 05:38:14 -0300642static const struct v4l2_ctrl_ops solo_ctrl_ops = {
643 .s_ctrl = solo_s_ctrl,
644};
645
646static const struct v4l2_ctrl_config solo_motion_trace_ctrl = {
647 .ops = &solo_ctrl_ops,
648 .id = V4L2_CID_MOTION_TRACE,
649 .name = "Motion Detection Trace",
650 .type = V4L2_CTRL_TYPE_BOOLEAN,
651 .max = 1,
652 .step = 1,
653};
654
Hans Verkuildcae5da2013-03-25 05:35:17 -0300655int solo_v4l2_init(struct solo_dev *solo_dev, unsigned nr)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400656{
657 int ret;
658 int i;
659
660 init_waitqueue_head(&solo_dev->disp_thread_wait);
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300661 spin_lock_init(&solo_dev->slock);
662 mutex_init(&solo_dev->lock);
663 INIT_LIST_HEAD(&solo_dev->vidq_active);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400664
665 solo_dev->vfd = video_device_alloc();
666 if (!solo_dev->vfd)
667 return -ENOMEM;
668
669 *solo_dev->vfd = solo_v4l2_template;
Hans Verkuild9ebd622013-03-25 05:36:44 -0300670 solo_dev->vfd->v4l2_dev = &solo_dev->v4l2_dev;
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300671 solo_dev->vfd->queue = &solo_dev->vidq;
672 solo_dev->vfd->lock = &solo_dev->lock;
Hans Verkuilc813bd32013-03-25 05:38:14 -0300673 v4l2_ctrl_handler_init(&solo_dev->disp_hdl, 1);
674 v4l2_ctrl_new_custom(&solo_dev->disp_hdl, &solo_motion_trace_ctrl, NULL);
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300675 if (solo_dev->disp_hdl.error) {
676 ret = solo_dev->disp_hdl.error;
677 goto fail;
678 }
Hans Verkuilc813bd32013-03-25 05:38:14 -0300679 solo_dev->vfd->ctrl_handler = &solo_dev->disp_hdl;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400680
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400681 video_set_drvdata(solo_dev->vfd, solo_dev);
682
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300683 solo_dev->vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
684 solo_dev->vidq.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
685 solo_dev->vidq.ops = &solo_video_qops;
686 solo_dev->vidq.mem_ops = &vb2_dma_contig_memops;
687 solo_dev->vidq.drv_priv = solo_dev;
Sakari Ailusade48682014-02-25 19:12:19 -0300688 solo_dev->vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
Mel Gormand0164ad2015-11-06 16:28:21 -0800689 solo_dev->vidq.gfp_flags = __GFP_DMA32 | __GFP_KSWAPD_RECLAIM;
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300690 solo_dev->vidq.buf_struct_size = sizeof(struct solo_vb2_buf);
691 solo_dev->vidq.lock = &solo_dev->lock;
Hans Verkuil2bc46b32016-02-15 12:37:15 -0200692 solo_dev->vidq.dev = &solo_dev->pdev->dev;
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300693 ret = vb2_queue_init(&solo_dev->vidq);
694 if (ret < 0)
695 goto fail;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400696
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400697 /* Cycle all the channels and clear */
698 for (i = 0; i < solo_dev->nr_chans; i++) {
699 solo_v4l2_set_ch(solo_dev, i);
700 while (erase_off(solo_dev))
Hans Verkuildcae5da2013-03-25 05:35:17 -0300701 /* Do nothing */;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400702 }
703
704 /* Set the default display channel */
705 solo_v4l2_set_ch(solo_dev, 0);
706 while (erase_off(solo_dev))
Hans Verkuildcae5da2013-03-25 05:35:17 -0300707 /* Do nothing */;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400708
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300709 ret = video_register_device(solo_dev->vfd, VFL_TYPE_GRABBER, nr);
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300710 if (ret < 0)
711 goto fail;
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300712
713 snprintf(solo_dev->vfd->name, sizeof(solo_dev->vfd->name), "%s (%i)",
714 SOLO6X10_NAME, solo_dev->vfd->num);
715
Mauro Carvalho Chehab3d160042016-10-18 17:44:07 -0200716 dev_info(&solo_dev->pdev->dev, "Display as /dev/video%d with %d inputs (%d extended)\n",
717 solo_dev->vfd->num,
Hans Verkuil6a2e65d2013-03-15 11:56:46 -0300718 solo_dev->nr_chans, solo_dev->nr_ext);
719
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400720 return 0;
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300721
722fail:
723 video_device_release(solo_dev->vfd);
Hans Verkuila4056c2f2013-03-15 12:11:17 -0300724 v4l2_ctrl_handler_free(&solo_dev->disp_hdl);
725 solo_dev->vfd = NULL;
726 return ret;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400727}
728
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100729void solo_v4l2_exit(struct solo_dev *solo_dev)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400730{
Hans Verkuildcae5da2013-03-25 05:35:17 -0300731 if (solo_dev->vfd == NULL)
732 return;
733
734 video_unregister_device(solo_dev->vfd);
Hans Verkuilc813bd32013-03-25 05:38:14 -0300735 v4l2_ctrl_handler_free(&solo_dev->disp_hdl);
Hans Verkuildcae5da2013-03-25 05:35:17 -0300736 solo_dev->vfd = NULL;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400737}