blob: 49e75a1a1f3f803dbd3839913baa02c8330d4ff4 [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001// SPDX-License-Identifier: GPL-2.0-only
Pete Eberleinb11869d2008-10-30 12:56:41 -07002/*
3 * Copyright (C) 2008 Sensoray Company Inc.
Pete Eberleinb11869d2008-10-30 12:56:41 -07004 */
5
6#include <linux/module.h>
Pete Eberleinb11869d2008-10-30 12:56:41 -07007#include <linux/usb.h>
8#include <linux/i2c.h>
9#include <linux/videodev2.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Pete Eberlein832b6a82009-11-16 15:13:51 -030011#include <media/v4l2-device.h>
Pete Eberleinb11869d2008-10-30 12:56:41 -070012#include <media/v4l2-common.h>
Pete Eberlein05d76f22009-11-16 15:15:07 -030013#include <media/v4l2-subdev.h>
Pete Eberleinb11869d2008-10-30 12:56:41 -070014#include "go7007-priv.h"
Pete Eberlein832b6a82009-11-16 15:13:51 -030015
16MODULE_DESCRIPTION("Sensoray 2250/2251 i2c v4l2 subdev driver");
17MODULE_LICENSE("GPL v2");
Pete Eberleinb11869d2008-10-30 12:56:41 -070018
Hans Verkuilb20cc0732013-03-07 04:58:35 -030019/*
20 * Note: this board has two i2c devices: a vpx3226f and a tlv320aic23b.
21 * Due to the unusual way these are accessed on this device we do not
22 * reuse the i2c drivers, but instead they are implemented in this
23 * driver. It would be nice to improve on this, though.
24 */
25
Pete Eberleinb11869d2008-10-30 12:56:41 -070026#define TLV320_ADDRESS 0x34
Pete Eberleinb11869d2008-10-30 12:56:41 -070027#define VPX322_ADDR_ANALOGCONTROL1 0x02
28#define VPX322_ADDR_BRIGHTNESS0 0x0127
29#define VPX322_ADDR_BRIGHTNESS1 0x0131
30#define VPX322_ADDR_CONTRAST0 0x0128
31#define VPX322_ADDR_CONTRAST1 0x0132
32#define VPX322_ADDR_HUE 0x00dc
Mauro Carvalho Chehab028d4c92009-09-15 11:06:04 -030033#define VPX322_ADDR_SAT 0x0030
Pete Eberleinb11869d2008-10-30 12:56:41 -070034
35struct go7007_usb_board {
36 unsigned int flags;
37 struct go7007_board_info main_info;
38};
39
40struct go7007_usb {
41 struct go7007_usb_board *board;
Mauro Carvalho Chehabfd9a40d2009-09-15 11:07:59 -030042 struct mutex i2c_lock;
Pete Eberleinb11869d2008-10-30 12:56:41 -070043 struct usb_device *usbdev;
44 struct urb *video_urbs[8];
45 struct urb *audio_urbs[8];
46 struct urb *intr_urb;
47};
48
49static unsigned char aud_regs[] = {
50 0x1e, 0x00,
51 0x00, 0x17,
52 0x02, 0x17,
53 0x04, 0xf9,
54 0x06, 0xf9,
55 0x08, 0x02,
56 0x0a, 0x00,
57 0x0c, 0x00,
58 0x0a, 0x00,
59 0x0c, 0x00,
60 0x0e, 0x02,
61 0x10, 0x00,
62 0x12, 0x01,
63 0x00, 0x00,
64};
65
66
67static unsigned char vid_regs[] = {
68 0xF2, 0x0f,
69 0xAA, 0x00,
70 0xF8, 0xff,
71 0x00, 0x00,
72};
73
74static u16 vid_regs_fp[] = {
75 0x028, 0x067,
76 0x120, 0x016,
77 0x121, 0xcF2,
78 0x122, 0x0F2,
79 0x123, 0x00c,
80 0x124, 0x2d0,
81 0x125, 0x2e0,
82 0x126, 0x004,
83 0x128, 0x1E0,
84 0x12A, 0x016,
85 0x12B, 0x0F2,
86 0x12C, 0x0F2,
87 0x12D, 0x00c,
88 0x12E, 0x2d0,
89 0x12F, 0x2e0,
90 0x130, 0x004,
91 0x132, 0x1E0,
92 0x140, 0x060,
93 0x153, 0x00C,
94 0x154, 0x200,
95 0x150, 0x801,
96 0x000, 0x000
97};
98
99/* PAL specific values */
YAMANE Toshiakib91c78e2012-11-06 15:40:16 -0300100static u16 vid_regs_fp_pal[] = {
Pete Eberleinb11869d2008-10-30 12:56:41 -0700101 0x120, 0x017,
102 0x121, 0xd22,
103 0x122, 0x122,
104 0x12A, 0x017,
105 0x12B, 0x122,
106 0x12C, 0x122,
107 0x140, 0x060,
108 0x000, 0x000,
109};
110
111struct s2250 {
Pete Eberlein05d76f22009-11-16 15:15:07 -0300112 struct v4l2_subdev sd;
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300113 struct v4l2_ctrl_handler hdl;
Pete Eberlein047efc72009-09-18 22:55:13 -0300114 v4l2_std_id std;
Pete Eberleinb11869d2008-10-30 12:56:41 -0700115 int input;
116 int brightness;
117 int contrast;
118 int saturation;
119 int hue;
120 int reg12b_val;
121 int audio_input;
Jean Delvare74005162009-04-21 21:47:22 +0200122 struct i2c_client *audio;
Pete Eberleinb11869d2008-10-30 12:56:41 -0700123};
124
Pete Eberlein05d76f22009-11-16 15:15:07 -0300125static inline struct s2250 *to_state(struct v4l2_subdev *sd)
126{
127 return container_of(sd, struct s2250, sd);
128}
129
Pete Eberleinb11869d2008-10-30 12:56:41 -0700130/* from go7007-usb.c which is Copyright (C) 2005-2006 Micronas USA Inc.*/
131static int go7007_usb_vendor_request(struct go7007 *go, u16 request,
132 u16 value, u16 index, void *transfer_buffer, int length, int in)
133{
134 struct go7007_usb *usb = go->hpi_context;
135 int timeout = 5000;
136
137 if (in) {
138 return usb_control_msg(usb->usbdev,
139 usb_rcvctrlpipe(usb->usbdev, 0), request,
140 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
141 value, index, transfer_buffer, length, timeout);
142 } else {
143 return usb_control_msg(usb->usbdev,
144 usb_sndctrlpipe(usb->usbdev, 0), request,
145 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
146 value, index, transfer_buffer, length, timeout);
147 }
148}
149/* end from go7007-usb.c which is Copyright (C) 2005-2006 Micronas USA Inc.*/
150
151static int write_reg(struct i2c_client *client, u8 reg, u8 value)
152{
153 struct go7007 *go = i2c_get_adapdata(client->adapter);
Julia Lawall41e84782008-12-19 18:11:25 +0100154 struct go7007_usb *usb;
Pete Eberleinb11869d2008-10-30 12:56:41 -0700155 int rc;
Pete Eberlein986ce452010-01-13 19:15:48 -0300156 int dev_addr = client->addr << 1; /* firmware wants 8-bit address */
Pete Eberleinb11869d2008-10-30 12:56:41 -0700157 u8 *buf;
158
159 if (go == NULL)
160 return -ENODEV;
161
162 if (go->status == STATUS_SHUTDOWN)
163 return -EBUSY;
164
165 buf = kzalloc(16, GFP_KERNEL);
166 if (buf == NULL)
167 return -ENOMEM;
168
Julia Lawall41e84782008-12-19 18:11:25 +0100169 usb = go->hpi_context;
Mauro Carvalho Chehabfd9a40d2009-09-15 11:07:59 -0300170 if (mutex_lock_interruptible(&usb->i2c_lock) != 0) {
YAMANE Toshiaki5820deb2012-11-06 15:41:03 -0300171 dev_info(&client->dev, "i2c lock failed\n");
Julia Lawall253d3b12008-12-25 21:09:57 +0100172 kfree(buf);
Pete Eberleinb11869d2008-10-30 12:56:41 -0700173 return -EINTR;
174 }
175 rc = go7007_usb_vendor_request(go, 0x55, dev_addr,
176 (reg<<8 | value),
177 buf,
178 16, 1);
179
Mauro Carvalho Chehabfd9a40d2009-09-15 11:07:59 -0300180 mutex_unlock(&usb->i2c_lock);
Pete Eberleinb11869d2008-10-30 12:56:41 -0700181 kfree(buf);
182 return rc;
183}
184
185static int write_reg_fp(struct i2c_client *client, u16 addr, u16 val)
186{
187 struct go7007 *go = i2c_get_adapdata(client->adapter);
Julia Lawall41e84782008-12-19 18:11:25 +0100188 struct go7007_usb *usb;
Alexey Khoroshilov0c07aec2012-02-13 11:01:32 -0300189 int rc;
Pete Eberleinb11869d2008-10-30 12:56:41 -0700190 u8 *buf;
191 struct s2250 *dec = i2c_get_clientdata(client);
192
193 if (go == NULL)
194 return -ENODEV;
195
196 if (go->status == STATUS_SHUTDOWN)
197 return -EBUSY;
198
199 buf = kzalloc(16, GFP_KERNEL);
200
201 if (buf == NULL)
202 return -ENOMEM;
203
204
205
206 memset(buf, 0xcd, 6);
207
Julia Lawall41e84782008-12-19 18:11:25 +0100208 usb = go->hpi_context;
Mauro Carvalho Chehabfd9a40d2009-09-15 11:07:59 -0300209 if (mutex_lock_interruptible(&usb->i2c_lock) != 0) {
YAMANE Toshiaki5820deb2012-11-06 15:41:03 -0300210 dev_info(&client->dev, "i2c lock failed\n");
Pete Eberleind66ddf22009-09-18 22:05:19 -0300211 kfree(buf);
Pete Eberleinb11869d2008-10-30 12:56:41 -0700212 return -EINTR;
213 }
Alexey Khoroshilov0c07aec2012-02-13 11:01:32 -0300214 rc = go7007_usb_vendor_request(go, 0x57, addr, val, buf, 16, 1);
215 mutex_unlock(&usb->i2c_lock);
216 if (rc < 0) {
Pete Eberleind66ddf22009-09-18 22:05:19 -0300217 kfree(buf);
Alexey Khoroshilov0c07aec2012-02-13 11:01:32 -0300218 return rc;
Pete Eberleind66ddf22009-09-18 22:05:19 -0300219 }
Pete Eberleinb11869d2008-10-30 12:56:41 -0700220
Pete Eberleinb11869d2008-10-30 12:56:41 -0700221 if (buf[0] == 0) {
222 unsigned int subaddr, val_read;
223
224 subaddr = (buf[4] << 8) + buf[5];
225 val_read = (buf[2] << 8) + buf[3];
Pete Eberleind66ddf22009-09-18 22:05:19 -0300226 kfree(buf);
Pete Eberleinb11869d2008-10-30 12:56:41 -0700227 if (val_read != val) {
YAMANE Toshiaki5820deb2012-11-06 15:41:03 -0300228 dev_info(&client->dev, "invalid fp write %x %x\n",
229 val_read, val);
Pete Eberleinb11869d2008-10-30 12:56:41 -0700230 return -EFAULT;
231 }
232 if (subaddr != addr) {
YAMANE Toshiaki5820deb2012-11-06 15:41:03 -0300233 dev_info(&client->dev, "invalid fp write addr %x %x\n",
234 subaddr, addr);
Pete Eberleinb11869d2008-10-30 12:56:41 -0700235 return -EFAULT;
236 }
Pete Eberleind66ddf22009-09-18 22:05:19 -0300237 } else {
238 kfree(buf);
Pete Eberleinb11869d2008-10-30 12:56:41 -0700239 return -EFAULT;
Pete Eberleind66ddf22009-09-18 22:05:19 -0300240 }
Pete Eberleinb11869d2008-10-30 12:56:41 -0700241
242 /* save last 12b value */
243 if (addr == 0x12b)
244 dec->reg12b_val = val;
245
246 return 0;
247}
248
Pete Eberlein047efc72009-09-18 22:55:13 -0300249static int read_reg_fp(struct i2c_client *client, u16 addr, u16 *val)
250{
251 struct go7007 *go = i2c_get_adapdata(client->adapter);
252 struct go7007_usb *usb;
Alexey Khoroshilov0c07aec2012-02-13 11:01:32 -0300253 int rc;
Pete Eberlein047efc72009-09-18 22:55:13 -0300254 u8 *buf;
255
256 if (go == NULL)
257 return -ENODEV;
258
259 if (go->status == STATUS_SHUTDOWN)
260 return -EBUSY;
261
262 buf = kzalloc(16, GFP_KERNEL);
263
264 if (buf == NULL)
265 return -ENOMEM;
266
267
268
269 memset(buf, 0xcd, 6);
270 usb = go->hpi_context;
Pete Eberleine49bd722009-11-15 08:14:14 -0300271 if (mutex_lock_interruptible(&usb->i2c_lock) != 0) {
YAMANE Toshiaki5820deb2012-11-06 15:41:03 -0300272 dev_info(&client->dev, "i2c lock failed\n");
Pete Eberlein047efc72009-09-18 22:55:13 -0300273 kfree(buf);
274 return -EINTR;
275 }
Alexey Khoroshilov0c07aec2012-02-13 11:01:32 -0300276 rc = go7007_usb_vendor_request(go, 0x58, addr, 0, buf, 16, 1);
Pete Eberleine49bd722009-11-15 08:14:14 -0300277 mutex_unlock(&usb->i2c_lock);
Alexey Khoroshilov0c07aec2012-02-13 11:01:32 -0300278 if (rc < 0) {
279 kfree(buf);
280 return rc;
281 }
Pete Eberlein047efc72009-09-18 22:55:13 -0300282
283 *val = (buf[0] << 8) | buf[1];
284 kfree(buf);
285
286 return 0;
287}
288
289
Pete Eberleinb11869d2008-10-30 12:56:41 -0700290static int write_regs(struct i2c_client *client, u8 *regs)
291{
292 int i;
293
294 for (i = 0; !((regs[i] == 0x00) && (regs[i+1] == 0x00)); i += 2) {
295 if (write_reg(client, regs[i], regs[i+1]) < 0) {
YAMANE Toshiaki5820deb2012-11-06 15:41:03 -0300296 dev_info(&client->dev, "failed\n");
Pete Eberleinb11869d2008-10-30 12:56:41 -0700297 return -1;
298 }
299 }
300 return 0;
301}
302
303static int write_regs_fp(struct i2c_client *client, u16 *regs)
304{
305 int i;
306
307 for (i = 0; !((regs[i] == 0x00) && (regs[i+1] == 0x00)); i += 2) {
308 if (write_reg_fp(client, regs[i], regs[i+1]) < 0) {
YAMANE Toshiaki5820deb2012-11-06 15:41:03 -0300309 dev_info(&client->dev, "failed fp\n");
Pete Eberleinb11869d2008-10-30 12:56:41 -0700310 return -1;
311 }
312 }
313 return 0;
314}
315
316
Pete Eberlein05d76f22009-11-16 15:15:07 -0300317/* ------------------------------------------------------------------------- */
318
319static int s2250_s_video_routing(struct v4l2_subdev *sd, u32 input, u32 output,
320 u32 config)
Pete Eberleinb11869d2008-10-30 12:56:41 -0700321{
Pete Eberlein05d76f22009-11-16 15:15:07 -0300322 struct s2250 *state = to_state(sd);
323 struct i2c_client *client = v4l2_get_subdevdata(sd);
324 int vidsys;
Pete Eberleinb11869d2008-10-30 12:56:41 -0700325
Pete Eberlein05d76f22009-11-16 15:15:07 -0300326 vidsys = (state->std == V4L2_STD_NTSC) ? 0x01 : 0x00;
327 if (input == 0) {
328 /* composite */
329 write_reg_fp(client, 0x20, 0x020 | vidsys);
330 write_reg_fp(client, 0x21, 0x662);
331 write_reg_fp(client, 0x140, 0x060);
332 } else if (input == 1) {
333 /* S-Video */
334 write_reg_fp(client, 0x20, 0x040 | vidsys);
335 write_reg_fp(client, 0x21, 0x666);
336 write_reg_fp(client, 0x140, 0x060);
337 } else {
338 return -EINVAL;
339 }
340 state->input = input;
341 return 0;
342}
Pete Eberleinb11869d2008-10-30 12:56:41 -0700343
Pete Eberlein05d76f22009-11-16 15:15:07 -0300344static int s2250_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
345{
346 struct s2250 *state = to_state(sd);
347 struct i2c_client *client = v4l2_get_subdevdata(sd);
348 u16 vidsource;
349
350 vidsource = (state->input == 1) ? 0x040 : 0x020;
Hans Verkuil50deb742013-03-11 05:50:21 -0300351 if (norm & V4L2_STD_625_50) {
Pete Eberlein05d76f22009-11-16 15:15:07 -0300352 write_regs_fp(client, vid_regs_fp);
353 write_regs_fp(client, vid_regs_fp_pal);
354 write_reg_fp(client, 0x20, vidsource);
Hans Verkuil50deb742013-03-11 05:50:21 -0300355 } else {
356 write_regs_fp(client, vid_regs_fp);
357 write_reg_fp(client, 0x20, vidsource | 1);
Pete Eberlein05d76f22009-11-16 15:15:07 -0300358 }
359 state->std = norm;
360 return 0;
361}
362
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300363static int s2250_s_ctrl(struct v4l2_ctrl *ctrl)
Pete Eberlein05d76f22009-11-16 15:15:07 -0300364{
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300365 struct s2250 *state = container_of(ctrl->handler, struct s2250, hdl);
366 struct i2c_client *client = v4l2_get_subdevdata(&state->sd);
Pete Eberlein05d76f22009-11-16 15:15:07 -0300367 u16 oldvalue;
368
369 switch (ctrl->id) {
370 case V4L2_CID_BRIGHTNESS:
Pete Eberlein05d76f22009-11-16 15:15:07 -0300371 read_reg_fp(client, VPX322_ADDR_BRIGHTNESS0, &oldvalue);
372 write_reg_fp(client, VPX322_ADDR_BRIGHTNESS0,
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300373 ctrl->val | (oldvalue & ~0xff));
Pete Eberlein05d76f22009-11-16 15:15:07 -0300374 read_reg_fp(client, VPX322_ADDR_BRIGHTNESS1, &oldvalue);
375 write_reg_fp(client, VPX322_ADDR_BRIGHTNESS1,
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300376 ctrl->val | (oldvalue & ~0xff));
Pete Eberlein05d76f22009-11-16 15:15:07 -0300377 write_reg_fp(client, 0x140, 0x60);
378 break;
379 case V4L2_CID_CONTRAST:
Pete Eberlein05d76f22009-11-16 15:15:07 -0300380 read_reg_fp(client, VPX322_ADDR_CONTRAST0, &oldvalue);
381 write_reg_fp(client, VPX322_ADDR_CONTRAST0,
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300382 ctrl->val | (oldvalue & ~0x3f));
Pete Eberlein05d76f22009-11-16 15:15:07 -0300383 read_reg_fp(client, VPX322_ADDR_CONTRAST1, &oldvalue);
384 write_reg_fp(client, VPX322_ADDR_CONTRAST1,
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300385 ctrl->val | (oldvalue & ~0x3f));
Pete Eberlein05d76f22009-11-16 15:15:07 -0300386 write_reg_fp(client, 0x140, 0x60);
387 break;
388 case V4L2_CID_SATURATION:
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300389 write_reg_fp(client, VPX322_ADDR_SAT, ctrl->val);
Pete Eberlein05d76f22009-11-16 15:15:07 -0300390 break;
391 case V4L2_CID_HUE:
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300392 write_reg_fp(client, VPX322_ADDR_HUE, ctrl->val);
Pete Eberlein05d76f22009-11-16 15:15:07 -0300393 break;
394 default:
395 return -EINVAL;
396 }
397 return 0;
398}
399
Hans Verkuil6e80c472015-03-21 09:39:09 -0300400static int s2250_set_fmt(struct v4l2_subdev *sd,
401 struct v4l2_subdev_pad_config *cfg,
402 struct v4l2_subdev_format *format)
Pete Eberlein05d76f22009-11-16 15:15:07 -0300403{
Hans Verkuil6e80c472015-03-21 09:39:09 -0300404 struct v4l2_mbus_framefmt *fmt = &format->format;
Pete Eberlein05d76f22009-11-16 15:15:07 -0300405 struct s2250 *state = to_state(sd);
406 struct i2c_client *client = v4l2_get_subdevdata(sd);
407
Hans Verkuil6e80c472015-03-21 09:39:09 -0300408 if (format->pad)
409 return -EINVAL;
410
411 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
412 return 0;
413
Hans Verkuilb8c75ed2010-08-06 16:42:55 -0300414 if (fmt->height < 640) {
Pete Eberlein05d76f22009-11-16 15:15:07 -0300415 write_reg_fp(client, 0x12b, state->reg12b_val | 0x400);
416 write_reg_fp(client, 0x140, 0x060);
417 } else {
418 write_reg_fp(client, 0x12b, state->reg12b_val & ~0x400);
419 write_reg_fp(client, 0x140, 0x060);
420 }
421 return 0;
422}
423
424static int s2250_s_audio_routing(struct v4l2_subdev *sd, u32 input, u32 output,
425 u32 config)
426{
427 struct s2250 *state = to_state(sd);
428
429 switch (input) {
430 case 0:
431 write_reg(state->audio, 0x08, 0x02); /* Line In */
432 break;
433 case 1:
434 write_reg(state->audio, 0x08, 0x04); /* Mic */
435 break;
436 case 2:
437 write_reg(state->audio, 0x08, 0x05); /* Mic Boost */
438 break;
439 default:
440 return -EINVAL;
441 }
442 state->audio_input = input;
443 return 0;
444}
445
446
447static int s2250_log_status(struct v4l2_subdev *sd)
448{
449 struct s2250 *state = to_state(sd);
450
451 v4l2_info(sd, "Standard: %s\n", state->std == V4L2_STD_NTSC ? "NTSC" :
452 state->std == V4L2_STD_PAL ? "PAL" :
453 state->std == V4L2_STD_SECAM ? "SECAM" :
454 "unknown");
455 v4l2_info(sd, "Input: %s\n", state->input == 0 ? "Composite" :
456 state->input == 1 ? "S-video" :
457 "error");
Pete Eberlein05d76f22009-11-16 15:15:07 -0300458 v4l2_info(sd, "Audio input: %s\n", state->audio_input == 0 ? "Line In" :
459 state->audio_input == 1 ? "Mic" :
460 state->audio_input == 2 ? "Mic Boost" :
461 "error");
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300462 return v4l2_ctrl_subdev_log_status(sd);
Pete Eberlein05d76f22009-11-16 15:15:07 -0300463}
464
465/* --------------------------------------------------------------------------*/
466
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300467static const struct v4l2_ctrl_ops s2250_ctrl_ops = {
468 .s_ctrl = s2250_s_ctrl,
469};
470
Pete Eberlein05d76f22009-11-16 15:15:07 -0300471static const struct v4l2_subdev_core_ops s2250_core_ops = {
472 .log_status = s2250_log_status,
Pete Eberlein05d76f22009-11-16 15:15:07 -0300473};
474
475static const struct v4l2_subdev_audio_ops s2250_audio_ops = {
476 .s_routing = s2250_s_audio_routing,
477};
478
479static const struct v4l2_subdev_video_ops s2250_video_ops = {
Laurent Pinchart8774bed2014-04-28 16:53:01 -0300480 .s_std = s2250_s_std,
Pete Eberlein05d76f22009-11-16 15:15:07 -0300481 .s_routing = s2250_s_video_routing,
Hans Verkuil6e80c472015-03-21 09:39:09 -0300482};
483
484static const struct v4l2_subdev_pad_ops s2250_pad_ops = {
485 .set_fmt = s2250_set_fmt,
Pete Eberlein05d76f22009-11-16 15:15:07 -0300486};
487
488static const struct v4l2_subdev_ops s2250_ops = {
489 .core = &s2250_core_ops,
490 .audio = &s2250_audio_ops,
491 .video = &s2250_video_ops,
Hans Verkuil6e80c472015-03-21 09:39:09 -0300492 .pad = &s2250_pad_ops,
Pete Eberlein05d76f22009-11-16 15:15:07 -0300493};
494
495/* --------------------------------------------------------------------------*/
496
Jean Delvare74005162009-04-21 21:47:22 +0200497static int s2250_probe(struct i2c_client *client,
498 const struct i2c_device_id *id)
Pete Eberleinb11869d2008-10-30 12:56:41 -0700499{
Jean Delvare74005162009-04-21 21:47:22 +0200500 struct i2c_client *audio;
501 struct i2c_adapter *adapter = client->adapter;
Pete Eberlein05d76f22009-11-16 15:15:07 -0300502 struct s2250 *state;
503 struct v4l2_subdev *sd;
Pete Eberleinb11869d2008-10-30 12:56:41 -0700504 u8 *data;
505 struct go7007 *go = i2c_get_adapdata(adapter);
506 struct go7007_usb *usb = go->hpi_context;
507
Wolfram Sang53f5d7a2019-07-22 14:26:02 -0300508 audio = i2c_new_dummy_device(adapter, TLV320_ADDRESS >> 1);
509 if (IS_ERR(audio))
510 return PTR_ERR(audio);
Pete Eberleinb11869d2008-10-30 12:56:41 -0700511
Hans Verkuil9b6ebf32013-03-09 09:59:16 -0300512 state = kzalloc(sizeof(struct s2250), GFP_KERNEL);
Pete Eberlein05d76f22009-11-16 15:15:07 -0300513 if (state == NULL) {
Jean Delvare74005162009-04-21 21:47:22 +0200514 i2c_unregister_device(audio);
Pete Eberleinb11869d2008-10-30 12:56:41 -0700515 return -ENOMEM;
516 }
517
Pete Eberlein05d76f22009-11-16 15:15:07 -0300518 sd = &state->sd;
519 v4l2_i2c_subdev_init(sd, client, &s2250_ops);
Pete Eberleinb11869d2008-10-30 12:56:41 -0700520
Pete Eberlein05d76f22009-11-16 15:15:07 -0300521 v4l2_info(sd, "initializing %s at address 0x%x on %s\n",
522 "Sensoray 2250/2251", client->addr, client->adapter->name);
523
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300524 v4l2_ctrl_handler_init(&state->hdl, 4);
525 v4l2_ctrl_new_std(&state->hdl, &s2250_ctrl_ops,
526 V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
527 v4l2_ctrl_new_std(&state->hdl, &s2250_ctrl_ops,
528 V4L2_CID_CONTRAST, 0, 0x3f, 1, 0x32);
529 v4l2_ctrl_new_std(&state->hdl, &s2250_ctrl_ops,
530 V4L2_CID_SATURATION, 0, 4094, 1, 2070);
531 v4l2_ctrl_new_std(&state->hdl, &s2250_ctrl_ops,
532 V4L2_CID_HUE, -512, 511, 1, 0);
533 sd->ctrl_handler = &state->hdl;
534 if (state->hdl.error) {
535 int err = state->hdl.error;
536
537 v4l2_ctrl_handler_free(&state->hdl);
538 kfree(state);
539 return err;
540 }
541
Pete Eberlein05d76f22009-11-16 15:15:07 -0300542 state->std = V4L2_STD_NTSC;
543 state->brightness = 50;
544 state->contrast = 50;
545 state->saturation = 50;
546 state->hue = 0;
547 state->audio = audio;
Pete Eberleinb11869d2008-10-30 12:56:41 -0700548
549 /* initialize the audio */
Jean Delvare74005162009-04-21 21:47:22 +0200550 if (write_regs(audio, aud_regs) < 0) {
YAMANE Toshiaki5820deb2012-11-06 15:41:03 -0300551 dev_err(&client->dev, "error initializing audio\n");
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300552 goto fail;
Pete Eberleinb11869d2008-10-30 12:56:41 -0700553 }
Pete Eberleinb11869d2008-10-30 12:56:41 -0700554
555 if (write_regs(client, vid_regs) < 0) {
YAMANE Toshiaki5820deb2012-11-06 15:41:03 -0300556 dev_err(&client->dev, "error initializing decoder\n");
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300557 goto fail;
Pete Eberleinb11869d2008-10-30 12:56:41 -0700558 }
559 if (write_regs_fp(client, vid_regs_fp) < 0) {
YAMANE Toshiaki5820deb2012-11-06 15:41:03 -0300560 dev_err(&client->dev, "error initializing decoder\n");
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300561 goto fail;
Pete Eberleinb11869d2008-10-30 12:56:41 -0700562 }
563 /* set default channel */
564 /* composite */
565 write_reg_fp(client, 0x20, 0x020 | 1);
566 write_reg_fp(client, 0x21, 0x662);
567 write_reg_fp(client, 0x140, 0x060);
568
569 /* set default audio input */
Pete Eberlein05d76f22009-11-16 15:15:07 -0300570 state->audio_input = 0;
Pete Eberleinb11869d2008-10-30 12:56:41 -0700571 write_reg(client, 0x08, 0x02); /* Line In */
572
Mauro Carvalho Chehabfd9a40d2009-09-15 11:07:59 -0300573 if (mutex_lock_interruptible(&usb->i2c_lock) == 0) {
Pete Eberleinb11869d2008-10-30 12:56:41 -0700574 data = kzalloc(16, GFP_KERNEL);
575 if (data != NULL) {
Hans Verkuil9707cb52014-07-17 20:40:20 -0300576 int rc = go7007_usb_vendor_request(go, 0x41, 0, 0,
Pete Eberleinb11869d2008-10-30 12:56:41 -0700577 data, 16, 1);
Hans Verkuil9707cb52014-07-17 20:40:20 -0300578
Pete Eberleinb11869d2008-10-30 12:56:41 -0700579 if (rc > 0) {
580 u8 mask;
Hans Verkuil9707cb52014-07-17 20:40:20 -0300581
Pete Eberleinb11869d2008-10-30 12:56:41 -0700582 data[0] = 0;
583 mask = 1<<5;
584 data[0] &= ~mask;
585 data[1] |= mask;
586 go7007_usb_vendor_request(go, 0x40, 0,
587 (data[1]<<8)
588 + data[1],
589 data, 16, 0);
590 }
591 kfree(data);
592 }
Mauro Carvalho Chehabfd9a40d2009-09-15 11:07:59 -0300593 mutex_unlock(&usb->i2c_lock);
Pete Eberleinb11869d2008-10-30 12:56:41 -0700594 }
595
Pete Eberlein05d76f22009-11-16 15:15:07 -0300596 v4l2_info(sd, "initialized successfully\n");
Pete Eberleinb11869d2008-10-30 12:56:41 -0700597 return 0;
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300598
599fail:
600 i2c_unregister_device(audio);
601 v4l2_ctrl_handler_free(&state->hdl);
602 kfree(state);
603 return -EIO;
Pete Eberleinb11869d2008-10-30 12:56:41 -0700604}
605
Jean Delvare74005162009-04-21 21:47:22 +0200606static int s2250_remove(struct i2c_client *client)
Pete Eberleinb11869d2008-10-30 12:56:41 -0700607{
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300608 struct s2250 *state = to_state(i2c_get_clientdata(client));
Pete Eberleinb11869d2008-10-30 12:56:41 -0700609
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300610 v4l2_device_unregister_subdev(&state->sd);
611 v4l2_ctrl_handler_free(&state->hdl);
612 kfree(state);
Pete Eberleinb11869d2008-10-30 12:56:41 -0700613 return 0;
614}
615
Németh Mártonb76a3262010-01-10 00:18:41 +0100616static const struct i2c_device_id s2250_id[] = {
Pete Eberlein832b6a82009-11-16 15:13:51 -0300617 { "s2250", 0 },
Jean Delvare74005162009-04-21 21:47:22 +0200618 { }
619};
Pete Eberlein832b6a82009-11-16 15:13:51 -0300620MODULE_DEVICE_TABLE(i2c, s2250_id);
Jean Delvare74005162009-04-21 21:47:22 +0200621
Hans Verkuil22ac5f62010-09-15 15:53:43 -0300622static struct i2c_driver s2250_driver = {
623 .driver = {
Hans Verkuil22ac5f62010-09-15 15:53:43 -0300624 .name = "s2250",
625 },
626 .probe = s2250_probe,
627 .remove = s2250_remove,
628 .id_table = s2250_id,
Pete Eberleinb11869d2008-10-30 12:56:41 -0700629};
Hans Verkuil22ac5f62010-09-15 15:53:43 -0300630
Wei Yongjun74ffbf82012-10-08 08:56:11 -0300631module_i2c_driver(s2250_driver);