blob: bb846680bcd479e4d94bc74f80b8fbfa7d9496b9 [file] [log] [blame]
Pete Eberleinb11869d2008-10-30 12:56:41 -07001/*
2 * Copyright (C) 2008 Sensoray Company Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License (Version 2) as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
Pete Eberleinb11869d2008-10-30 12:56:41 -070012 */
13
14#include <linux/module.h>
Pete Eberleinb11869d2008-10-30 12:56:41 -070015#include <linux/usb.h>
16#include <linux/i2c.h>
17#include <linux/videodev2.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Pete Eberlein832b6a82009-11-16 15:13:51 -030019#include <media/v4l2-device.h>
Pete Eberleinb11869d2008-10-30 12:56:41 -070020#include <media/v4l2-common.h>
Pete Eberlein05d76f22009-11-16 15:15:07 -030021#include <media/v4l2-subdev.h>
Pete Eberleinb11869d2008-10-30 12:56:41 -070022#include "go7007-priv.h"
Pete Eberlein832b6a82009-11-16 15:13:51 -030023
24MODULE_DESCRIPTION("Sensoray 2250/2251 i2c v4l2 subdev driver");
25MODULE_LICENSE("GPL v2");
Pete Eberleinb11869d2008-10-30 12:56:41 -070026
Hans Verkuilb20cc0732013-03-07 04:58:35 -030027/*
28 * Note: this board has two i2c devices: a vpx3226f and a tlv320aic23b.
29 * Due to the unusual way these are accessed on this device we do not
30 * reuse the i2c drivers, but instead they are implemented in this
31 * driver. It would be nice to improve on this, though.
32 */
33
Pete Eberleinb11869d2008-10-30 12:56:41 -070034#define TLV320_ADDRESS 0x34
Pete Eberleinb11869d2008-10-30 12:56:41 -070035#define VPX322_ADDR_ANALOGCONTROL1 0x02
36#define VPX322_ADDR_BRIGHTNESS0 0x0127
37#define VPX322_ADDR_BRIGHTNESS1 0x0131
38#define VPX322_ADDR_CONTRAST0 0x0128
39#define VPX322_ADDR_CONTRAST1 0x0132
40#define VPX322_ADDR_HUE 0x00dc
Mauro Carvalho Chehab028d4c92009-09-15 11:06:04 -030041#define VPX322_ADDR_SAT 0x0030
Pete Eberleinb11869d2008-10-30 12:56:41 -070042
43struct go7007_usb_board {
44 unsigned int flags;
45 struct go7007_board_info main_info;
46};
47
48struct go7007_usb {
49 struct go7007_usb_board *board;
Mauro Carvalho Chehabfd9a40d2009-09-15 11:07:59 -030050 struct mutex i2c_lock;
Pete Eberleinb11869d2008-10-30 12:56:41 -070051 struct usb_device *usbdev;
52 struct urb *video_urbs[8];
53 struct urb *audio_urbs[8];
54 struct urb *intr_urb;
55};
56
57static unsigned char aud_regs[] = {
58 0x1e, 0x00,
59 0x00, 0x17,
60 0x02, 0x17,
61 0x04, 0xf9,
62 0x06, 0xf9,
63 0x08, 0x02,
64 0x0a, 0x00,
65 0x0c, 0x00,
66 0x0a, 0x00,
67 0x0c, 0x00,
68 0x0e, 0x02,
69 0x10, 0x00,
70 0x12, 0x01,
71 0x00, 0x00,
72};
73
74
75static unsigned char vid_regs[] = {
76 0xF2, 0x0f,
77 0xAA, 0x00,
78 0xF8, 0xff,
79 0x00, 0x00,
80};
81
82static u16 vid_regs_fp[] = {
83 0x028, 0x067,
84 0x120, 0x016,
85 0x121, 0xcF2,
86 0x122, 0x0F2,
87 0x123, 0x00c,
88 0x124, 0x2d0,
89 0x125, 0x2e0,
90 0x126, 0x004,
91 0x128, 0x1E0,
92 0x12A, 0x016,
93 0x12B, 0x0F2,
94 0x12C, 0x0F2,
95 0x12D, 0x00c,
96 0x12E, 0x2d0,
97 0x12F, 0x2e0,
98 0x130, 0x004,
99 0x132, 0x1E0,
100 0x140, 0x060,
101 0x153, 0x00C,
102 0x154, 0x200,
103 0x150, 0x801,
104 0x000, 0x000
105};
106
107/* PAL specific values */
YAMANE Toshiakib91c78e2012-11-06 15:40:16 -0300108static u16 vid_regs_fp_pal[] = {
Pete Eberleinb11869d2008-10-30 12:56:41 -0700109 0x120, 0x017,
110 0x121, 0xd22,
111 0x122, 0x122,
112 0x12A, 0x017,
113 0x12B, 0x122,
114 0x12C, 0x122,
115 0x140, 0x060,
116 0x000, 0x000,
117};
118
119struct s2250 {
Pete Eberlein05d76f22009-11-16 15:15:07 -0300120 struct v4l2_subdev sd;
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300121 struct v4l2_ctrl_handler hdl;
Pete Eberlein047efc72009-09-18 22:55:13 -0300122 v4l2_std_id std;
Pete Eberleinb11869d2008-10-30 12:56:41 -0700123 int input;
124 int brightness;
125 int contrast;
126 int saturation;
127 int hue;
128 int reg12b_val;
129 int audio_input;
Jean Delvare74005162009-04-21 21:47:22 +0200130 struct i2c_client *audio;
Pete Eberleinb11869d2008-10-30 12:56:41 -0700131};
132
Pete Eberlein05d76f22009-11-16 15:15:07 -0300133static inline struct s2250 *to_state(struct v4l2_subdev *sd)
134{
135 return container_of(sd, struct s2250, sd);
136}
137
Pete Eberleinb11869d2008-10-30 12:56:41 -0700138/* from go7007-usb.c which is Copyright (C) 2005-2006 Micronas USA Inc.*/
139static int go7007_usb_vendor_request(struct go7007 *go, u16 request,
140 u16 value, u16 index, void *transfer_buffer, int length, int in)
141{
142 struct go7007_usb *usb = go->hpi_context;
143 int timeout = 5000;
144
145 if (in) {
146 return usb_control_msg(usb->usbdev,
147 usb_rcvctrlpipe(usb->usbdev, 0), request,
148 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
149 value, index, transfer_buffer, length, timeout);
150 } else {
151 return usb_control_msg(usb->usbdev,
152 usb_sndctrlpipe(usb->usbdev, 0), request,
153 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
154 value, index, transfer_buffer, length, timeout);
155 }
156}
157/* end from go7007-usb.c which is Copyright (C) 2005-2006 Micronas USA Inc.*/
158
159static int write_reg(struct i2c_client *client, u8 reg, u8 value)
160{
161 struct go7007 *go = i2c_get_adapdata(client->adapter);
Julia Lawall41e84782008-12-19 18:11:25 +0100162 struct go7007_usb *usb;
Pete Eberleinb11869d2008-10-30 12:56:41 -0700163 int rc;
Pete Eberlein986ce452010-01-13 19:15:48 -0300164 int dev_addr = client->addr << 1; /* firmware wants 8-bit address */
Pete Eberleinb11869d2008-10-30 12:56:41 -0700165 u8 *buf;
166
167 if (go == NULL)
168 return -ENODEV;
169
170 if (go->status == STATUS_SHUTDOWN)
171 return -EBUSY;
172
173 buf = kzalloc(16, GFP_KERNEL);
174 if (buf == NULL)
175 return -ENOMEM;
176
Julia Lawall41e84782008-12-19 18:11:25 +0100177 usb = go->hpi_context;
Mauro Carvalho Chehabfd9a40d2009-09-15 11:07:59 -0300178 if (mutex_lock_interruptible(&usb->i2c_lock) != 0) {
YAMANE Toshiaki5820deb2012-11-06 15:41:03 -0300179 dev_info(&client->dev, "i2c lock failed\n");
Julia Lawall253d3b12008-12-25 21:09:57 +0100180 kfree(buf);
Pete Eberleinb11869d2008-10-30 12:56:41 -0700181 return -EINTR;
182 }
183 rc = go7007_usb_vendor_request(go, 0x55, dev_addr,
184 (reg<<8 | value),
185 buf,
186 16, 1);
187
Mauro Carvalho Chehabfd9a40d2009-09-15 11:07:59 -0300188 mutex_unlock(&usb->i2c_lock);
Pete Eberleinb11869d2008-10-30 12:56:41 -0700189 kfree(buf);
190 return rc;
191}
192
193static int write_reg_fp(struct i2c_client *client, u16 addr, u16 val)
194{
195 struct go7007 *go = i2c_get_adapdata(client->adapter);
Julia Lawall41e84782008-12-19 18:11:25 +0100196 struct go7007_usb *usb;
Alexey Khoroshilov0c07aec2012-02-13 11:01:32 -0300197 int rc;
Pete Eberleinb11869d2008-10-30 12:56:41 -0700198 u8 *buf;
199 struct s2250 *dec = i2c_get_clientdata(client);
200
201 if (go == NULL)
202 return -ENODEV;
203
204 if (go->status == STATUS_SHUTDOWN)
205 return -EBUSY;
206
207 buf = kzalloc(16, GFP_KERNEL);
208
209 if (buf == NULL)
210 return -ENOMEM;
211
212
213
214 memset(buf, 0xcd, 6);
215
Julia Lawall41e84782008-12-19 18:11:25 +0100216 usb = go->hpi_context;
Mauro Carvalho Chehabfd9a40d2009-09-15 11:07:59 -0300217 if (mutex_lock_interruptible(&usb->i2c_lock) != 0) {
YAMANE Toshiaki5820deb2012-11-06 15:41:03 -0300218 dev_info(&client->dev, "i2c lock failed\n");
Pete Eberleind66ddf22009-09-18 22:05:19 -0300219 kfree(buf);
Pete Eberleinb11869d2008-10-30 12:56:41 -0700220 return -EINTR;
221 }
Alexey Khoroshilov0c07aec2012-02-13 11:01:32 -0300222 rc = go7007_usb_vendor_request(go, 0x57, addr, val, buf, 16, 1);
223 mutex_unlock(&usb->i2c_lock);
224 if (rc < 0) {
Pete Eberleind66ddf22009-09-18 22:05:19 -0300225 kfree(buf);
Alexey Khoroshilov0c07aec2012-02-13 11:01:32 -0300226 return rc;
Pete Eberleind66ddf22009-09-18 22:05:19 -0300227 }
Pete Eberleinb11869d2008-10-30 12:56:41 -0700228
Pete Eberleinb11869d2008-10-30 12:56:41 -0700229 if (buf[0] == 0) {
230 unsigned int subaddr, val_read;
231
232 subaddr = (buf[4] << 8) + buf[5];
233 val_read = (buf[2] << 8) + buf[3];
Pete Eberleind66ddf22009-09-18 22:05:19 -0300234 kfree(buf);
Pete Eberleinb11869d2008-10-30 12:56:41 -0700235 if (val_read != val) {
YAMANE Toshiaki5820deb2012-11-06 15:41:03 -0300236 dev_info(&client->dev, "invalid fp write %x %x\n",
237 val_read, val);
Pete Eberleinb11869d2008-10-30 12:56:41 -0700238 return -EFAULT;
239 }
240 if (subaddr != addr) {
YAMANE Toshiaki5820deb2012-11-06 15:41:03 -0300241 dev_info(&client->dev, "invalid fp write addr %x %x\n",
242 subaddr, addr);
Pete Eberleinb11869d2008-10-30 12:56:41 -0700243 return -EFAULT;
244 }
Pete Eberleind66ddf22009-09-18 22:05:19 -0300245 } else {
246 kfree(buf);
Pete Eberleinb11869d2008-10-30 12:56:41 -0700247 return -EFAULT;
Pete Eberleind66ddf22009-09-18 22:05:19 -0300248 }
Pete Eberleinb11869d2008-10-30 12:56:41 -0700249
250 /* save last 12b value */
251 if (addr == 0x12b)
252 dec->reg12b_val = val;
253
254 return 0;
255}
256
Pete Eberlein047efc72009-09-18 22:55:13 -0300257static int read_reg_fp(struct i2c_client *client, u16 addr, u16 *val)
258{
259 struct go7007 *go = i2c_get_adapdata(client->adapter);
260 struct go7007_usb *usb;
Alexey Khoroshilov0c07aec2012-02-13 11:01:32 -0300261 int rc;
Pete Eberlein047efc72009-09-18 22:55:13 -0300262 u8 *buf;
263
264 if (go == NULL)
265 return -ENODEV;
266
267 if (go->status == STATUS_SHUTDOWN)
268 return -EBUSY;
269
270 buf = kzalloc(16, GFP_KERNEL);
271
272 if (buf == NULL)
273 return -ENOMEM;
274
275
276
277 memset(buf, 0xcd, 6);
278 usb = go->hpi_context;
Pete Eberleine49bd722009-11-15 08:14:14 -0300279 if (mutex_lock_interruptible(&usb->i2c_lock) != 0) {
YAMANE Toshiaki5820deb2012-11-06 15:41:03 -0300280 dev_info(&client->dev, "i2c lock failed\n");
Pete Eberlein047efc72009-09-18 22:55:13 -0300281 kfree(buf);
282 return -EINTR;
283 }
Alexey Khoroshilov0c07aec2012-02-13 11:01:32 -0300284 rc = go7007_usb_vendor_request(go, 0x58, addr, 0, buf, 16, 1);
Pete Eberleine49bd722009-11-15 08:14:14 -0300285 mutex_unlock(&usb->i2c_lock);
Alexey Khoroshilov0c07aec2012-02-13 11:01:32 -0300286 if (rc < 0) {
287 kfree(buf);
288 return rc;
289 }
Pete Eberlein047efc72009-09-18 22:55:13 -0300290
291 *val = (buf[0] << 8) | buf[1];
292 kfree(buf);
293
294 return 0;
295}
296
297
Pete Eberleinb11869d2008-10-30 12:56:41 -0700298static int write_regs(struct i2c_client *client, u8 *regs)
299{
300 int i;
301
302 for (i = 0; !((regs[i] == 0x00) && (regs[i+1] == 0x00)); i += 2) {
303 if (write_reg(client, regs[i], regs[i+1]) < 0) {
YAMANE Toshiaki5820deb2012-11-06 15:41:03 -0300304 dev_info(&client->dev, "failed\n");
Pete Eberleinb11869d2008-10-30 12:56:41 -0700305 return -1;
306 }
307 }
308 return 0;
309}
310
311static int write_regs_fp(struct i2c_client *client, u16 *regs)
312{
313 int i;
314
315 for (i = 0; !((regs[i] == 0x00) && (regs[i+1] == 0x00)); i += 2) {
316 if (write_reg_fp(client, regs[i], regs[i+1]) < 0) {
YAMANE Toshiaki5820deb2012-11-06 15:41:03 -0300317 dev_info(&client->dev, "failed fp\n");
Pete Eberleinb11869d2008-10-30 12:56:41 -0700318 return -1;
319 }
320 }
321 return 0;
322}
323
324
Pete Eberlein05d76f22009-11-16 15:15:07 -0300325/* ------------------------------------------------------------------------- */
326
327static int s2250_s_video_routing(struct v4l2_subdev *sd, u32 input, u32 output,
328 u32 config)
Pete Eberleinb11869d2008-10-30 12:56:41 -0700329{
Pete Eberlein05d76f22009-11-16 15:15:07 -0300330 struct s2250 *state = to_state(sd);
331 struct i2c_client *client = v4l2_get_subdevdata(sd);
332 int vidsys;
Pete Eberleinb11869d2008-10-30 12:56:41 -0700333
Pete Eberlein05d76f22009-11-16 15:15:07 -0300334 vidsys = (state->std == V4L2_STD_NTSC) ? 0x01 : 0x00;
335 if (input == 0) {
336 /* composite */
337 write_reg_fp(client, 0x20, 0x020 | vidsys);
338 write_reg_fp(client, 0x21, 0x662);
339 write_reg_fp(client, 0x140, 0x060);
340 } else if (input == 1) {
341 /* S-Video */
342 write_reg_fp(client, 0x20, 0x040 | vidsys);
343 write_reg_fp(client, 0x21, 0x666);
344 write_reg_fp(client, 0x140, 0x060);
345 } else {
346 return -EINVAL;
347 }
348 state->input = input;
349 return 0;
350}
Pete Eberleinb11869d2008-10-30 12:56:41 -0700351
Pete Eberlein05d76f22009-11-16 15:15:07 -0300352static int s2250_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
353{
354 struct s2250 *state = to_state(sd);
355 struct i2c_client *client = v4l2_get_subdevdata(sd);
356 u16 vidsource;
357
358 vidsource = (state->input == 1) ? 0x040 : 0x020;
Hans Verkuil50deb742013-03-11 05:50:21 -0300359 if (norm & V4L2_STD_625_50) {
Pete Eberlein05d76f22009-11-16 15:15:07 -0300360 write_regs_fp(client, vid_regs_fp);
361 write_regs_fp(client, vid_regs_fp_pal);
362 write_reg_fp(client, 0x20, vidsource);
Hans Verkuil50deb742013-03-11 05:50:21 -0300363 } else {
364 write_regs_fp(client, vid_regs_fp);
365 write_reg_fp(client, 0x20, vidsource | 1);
Pete Eberlein05d76f22009-11-16 15:15:07 -0300366 }
367 state->std = norm;
368 return 0;
369}
370
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300371static int s2250_s_ctrl(struct v4l2_ctrl *ctrl)
Pete Eberlein05d76f22009-11-16 15:15:07 -0300372{
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300373 struct s2250 *state = container_of(ctrl->handler, struct s2250, hdl);
374 struct i2c_client *client = v4l2_get_subdevdata(&state->sd);
Pete Eberlein05d76f22009-11-16 15:15:07 -0300375 u16 oldvalue;
376
377 switch (ctrl->id) {
378 case V4L2_CID_BRIGHTNESS:
Pete Eberlein05d76f22009-11-16 15:15:07 -0300379 read_reg_fp(client, VPX322_ADDR_BRIGHTNESS0, &oldvalue);
380 write_reg_fp(client, VPX322_ADDR_BRIGHTNESS0,
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300381 ctrl->val | (oldvalue & ~0xff));
Pete Eberlein05d76f22009-11-16 15:15:07 -0300382 read_reg_fp(client, VPX322_ADDR_BRIGHTNESS1, &oldvalue);
383 write_reg_fp(client, VPX322_ADDR_BRIGHTNESS1,
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300384 ctrl->val | (oldvalue & ~0xff));
Pete Eberlein05d76f22009-11-16 15:15:07 -0300385 write_reg_fp(client, 0x140, 0x60);
386 break;
387 case V4L2_CID_CONTRAST:
Pete Eberlein05d76f22009-11-16 15:15:07 -0300388 read_reg_fp(client, VPX322_ADDR_CONTRAST0, &oldvalue);
389 write_reg_fp(client, VPX322_ADDR_CONTRAST0,
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300390 ctrl->val | (oldvalue & ~0x3f));
Pete Eberlein05d76f22009-11-16 15:15:07 -0300391 read_reg_fp(client, VPX322_ADDR_CONTRAST1, &oldvalue);
392 write_reg_fp(client, VPX322_ADDR_CONTRAST1,
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300393 ctrl->val | (oldvalue & ~0x3f));
Pete Eberlein05d76f22009-11-16 15:15:07 -0300394 write_reg_fp(client, 0x140, 0x60);
395 break;
396 case V4L2_CID_SATURATION:
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300397 write_reg_fp(client, VPX322_ADDR_SAT, ctrl->val);
Pete Eberlein05d76f22009-11-16 15:15:07 -0300398 break;
399 case V4L2_CID_HUE:
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300400 write_reg_fp(client, VPX322_ADDR_HUE, ctrl->val);
Pete Eberlein05d76f22009-11-16 15:15:07 -0300401 break;
402 default:
403 return -EINVAL;
404 }
405 return 0;
406}
407
Hans Verkuilb8c75ed2010-08-06 16:42:55 -0300408static int s2250_s_mbus_fmt(struct v4l2_subdev *sd,
409 struct v4l2_mbus_framefmt *fmt)
Pete Eberlein05d76f22009-11-16 15:15:07 -0300410{
411 struct s2250 *state = to_state(sd);
412 struct i2c_client *client = v4l2_get_subdevdata(sd);
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 Verkuilb8c75ed2010-08-06 16:42:55 -0300482 .s_mbus_fmt = s2250_s_mbus_fmt,
Pete Eberlein05d76f22009-11-16 15:15:07 -0300483};
484
485static const struct v4l2_subdev_ops s2250_ops = {
486 .core = &s2250_core_ops,
487 .audio = &s2250_audio_ops,
488 .video = &s2250_video_ops,
489};
490
491/* --------------------------------------------------------------------------*/
492
Jean Delvare74005162009-04-21 21:47:22 +0200493static int s2250_probe(struct i2c_client *client,
494 const struct i2c_device_id *id)
Pete Eberleinb11869d2008-10-30 12:56:41 -0700495{
Jean Delvare74005162009-04-21 21:47:22 +0200496 struct i2c_client *audio;
497 struct i2c_adapter *adapter = client->adapter;
Pete Eberlein05d76f22009-11-16 15:15:07 -0300498 struct s2250 *state;
499 struct v4l2_subdev *sd;
Pete Eberleinb11869d2008-10-30 12:56:41 -0700500 u8 *data;
501 struct go7007 *go = i2c_get_adapdata(adapter);
502 struct go7007_usb *usb = go->hpi_context;
503
Jean Delvare74005162009-04-21 21:47:22 +0200504 audio = i2c_new_dummy(adapter, TLV320_ADDRESS >> 1);
505 if (audio == NULL)
Pete Eberleinb11869d2008-10-30 12:56:41 -0700506 return -ENOMEM;
Pete Eberleinb11869d2008-10-30 12:56:41 -0700507
Hans Verkuil9b6ebf32013-03-09 09:59:16 -0300508 state = kzalloc(sizeof(struct s2250), GFP_KERNEL);
Pete Eberlein05d76f22009-11-16 15:15:07 -0300509 if (state == NULL) {
Jean Delvare74005162009-04-21 21:47:22 +0200510 i2c_unregister_device(audio);
Pete Eberleinb11869d2008-10-30 12:56:41 -0700511 return -ENOMEM;
512 }
513
Pete Eberlein05d76f22009-11-16 15:15:07 -0300514 sd = &state->sd;
515 v4l2_i2c_subdev_init(sd, client, &s2250_ops);
Pete Eberleinb11869d2008-10-30 12:56:41 -0700516
Pete Eberlein05d76f22009-11-16 15:15:07 -0300517 v4l2_info(sd, "initializing %s at address 0x%x on %s\n",
518 "Sensoray 2250/2251", client->addr, client->adapter->name);
519
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300520 v4l2_ctrl_handler_init(&state->hdl, 4);
521 v4l2_ctrl_new_std(&state->hdl, &s2250_ctrl_ops,
522 V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
523 v4l2_ctrl_new_std(&state->hdl, &s2250_ctrl_ops,
524 V4L2_CID_CONTRAST, 0, 0x3f, 1, 0x32);
525 v4l2_ctrl_new_std(&state->hdl, &s2250_ctrl_ops,
526 V4L2_CID_SATURATION, 0, 4094, 1, 2070);
527 v4l2_ctrl_new_std(&state->hdl, &s2250_ctrl_ops,
528 V4L2_CID_HUE, -512, 511, 1, 0);
529 sd->ctrl_handler = &state->hdl;
530 if (state->hdl.error) {
531 int err = state->hdl.error;
532
533 v4l2_ctrl_handler_free(&state->hdl);
534 kfree(state);
535 return err;
536 }
537
Pete Eberlein05d76f22009-11-16 15:15:07 -0300538 state->std = V4L2_STD_NTSC;
539 state->brightness = 50;
540 state->contrast = 50;
541 state->saturation = 50;
542 state->hue = 0;
543 state->audio = audio;
Pete Eberleinb11869d2008-10-30 12:56:41 -0700544
545 /* initialize the audio */
Jean Delvare74005162009-04-21 21:47:22 +0200546 if (write_regs(audio, aud_regs) < 0) {
YAMANE Toshiaki5820deb2012-11-06 15:41:03 -0300547 dev_err(&client->dev, "error initializing audio\n");
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300548 goto fail;
Pete Eberleinb11869d2008-10-30 12:56:41 -0700549 }
Pete Eberleinb11869d2008-10-30 12:56:41 -0700550
551 if (write_regs(client, vid_regs) < 0) {
YAMANE Toshiaki5820deb2012-11-06 15:41:03 -0300552 dev_err(&client->dev, "error initializing decoder\n");
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300553 goto fail;
Pete Eberleinb11869d2008-10-30 12:56:41 -0700554 }
555 if (write_regs_fp(client, vid_regs_fp) < 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 /* set default channel */
560 /* composite */
561 write_reg_fp(client, 0x20, 0x020 | 1);
562 write_reg_fp(client, 0x21, 0x662);
563 write_reg_fp(client, 0x140, 0x060);
564
565 /* set default audio input */
Pete Eberlein05d76f22009-11-16 15:15:07 -0300566 state->audio_input = 0;
Pete Eberleinb11869d2008-10-30 12:56:41 -0700567 write_reg(client, 0x08, 0x02); /* Line In */
568
Mauro Carvalho Chehabfd9a40d2009-09-15 11:07:59 -0300569 if (mutex_lock_interruptible(&usb->i2c_lock) == 0) {
Pete Eberleinb11869d2008-10-30 12:56:41 -0700570 data = kzalloc(16, GFP_KERNEL);
571 if (data != NULL) {
Hans Verkuil9707cb52014-07-17 20:40:20 -0300572 int rc = go7007_usb_vendor_request(go, 0x41, 0, 0,
Pete Eberleinb11869d2008-10-30 12:56:41 -0700573 data, 16, 1);
Hans Verkuil9707cb52014-07-17 20:40:20 -0300574
Pete Eberleinb11869d2008-10-30 12:56:41 -0700575 if (rc > 0) {
576 u8 mask;
Hans Verkuil9707cb52014-07-17 20:40:20 -0300577
Pete Eberleinb11869d2008-10-30 12:56:41 -0700578 data[0] = 0;
579 mask = 1<<5;
580 data[0] &= ~mask;
581 data[1] |= mask;
582 go7007_usb_vendor_request(go, 0x40, 0,
583 (data[1]<<8)
584 + data[1],
585 data, 16, 0);
586 }
587 kfree(data);
588 }
Mauro Carvalho Chehabfd9a40d2009-09-15 11:07:59 -0300589 mutex_unlock(&usb->i2c_lock);
Pete Eberleinb11869d2008-10-30 12:56:41 -0700590 }
591
Pete Eberlein05d76f22009-11-16 15:15:07 -0300592 v4l2_info(sd, "initialized successfully\n");
Pete Eberleinb11869d2008-10-30 12:56:41 -0700593 return 0;
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300594
595fail:
596 i2c_unregister_device(audio);
597 v4l2_ctrl_handler_free(&state->hdl);
598 kfree(state);
599 return -EIO;
Pete Eberleinb11869d2008-10-30 12:56:41 -0700600}
601
Jean Delvare74005162009-04-21 21:47:22 +0200602static int s2250_remove(struct i2c_client *client)
Pete Eberleinb11869d2008-10-30 12:56:41 -0700603{
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300604 struct s2250 *state = to_state(i2c_get_clientdata(client));
Pete Eberleinb11869d2008-10-30 12:56:41 -0700605
Hans Verkuild3b2ccd2013-03-03 10:50:34 -0300606 v4l2_device_unregister_subdev(&state->sd);
607 v4l2_ctrl_handler_free(&state->hdl);
608 kfree(state);
Pete Eberleinb11869d2008-10-30 12:56:41 -0700609 return 0;
610}
611
Németh Mártonb76a3262010-01-10 00:18:41 +0100612static const struct i2c_device_id s2250_id[] = {
Pete Eberlein832b6a82009-11-16 15:13:51 -0300613 { "s2250", 0 },
Jean Delvare74005162009-04-21 21:47:22 +0200614 { }
615};
Pete Eberlein832b6a82009-11-16 15:13:51 -0300616MODULE_DEVICE_TABLE(i2c, s2250_id);
Jean Delvare74005162009-04-21 21:47:22 +0200617
Hans Verkuil22ac5f62010-09-15 15:53:43 -0300618static struct i2c_driver s2250_driver = {
619 .driver = {
620 .owner = THIS_MODULE,
621 .name = "s2250",
622 },
623 .probe = s2250_probe,
624 .remove = s2250_remove,
625 .id_table = s2250_id,
Pete Eberleinb11869d2008-10-30 12:56:41 -0700626};
Hans Verkuil22ac5f62010-09-15 15:53:43 -0300627
Wei Yongjun74ffbf82012-10-08 08:56:11 -0300628module_i2c_driver(s2250_driver);