blob: ea16e3cf1793150afe2369cee69428d46610cf43 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * saa7110 - Philips SAA7110(A) video decoder driver
3 *
4 * Copyright (C) 1998 Pauline Middelink <middelin@polyware.nl>
5 *
6 * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
7 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
8 * - some corrections for Pinnacle Systems Inc. DC10plus card.
9 *
10 * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
11 * - moved over to linux>=2.4.x i2c protocol (1/1/2003)
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/types.h>
31#include <linux/delay.h>
32#include <linux/slab.h>
33#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/uaccess.h>
Hans Verkuil85ede692008-09-07 08:00:32 -030035#include <linux/i2c.h>
Hans Verkuile7946842009-02-19 12:24:27 -030036#include <linux/videodev2.h>
37#include <media/v4l2-device.h>
38#include <media/v4l2-chip-ident.h>
Hans Verkuil85ede692008-09-07 08:00:32 -030039#include <media/v4l2-i2c-drv-legacy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41MODULE_DESCRIPTION("Philips SAA7110 video decoder driver");
42MODULE_AUTHOR("Pauline Middelink");
43MODULE_LICENSE("GPL");
44
Hans Verkuile7946842009-02-19 12:24:27 -030045static unsigned short normal_i2c[] = { 0x9c >> 1, 0x9e >> 1, I2C_CLIENT_END };
46
47I2C_CLIENT_INSMOD;
48
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030049static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050module_param(debug, int, 0);
51MODULE_PARM_DESC(debug, "Debug level (0-1)");
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define SAA7110_MAX_INPUT 9 /* 6 CVBS, 3 SVHS */
Jean Delvare8182ff62008-10-24 15:08:28 -030054#define SAA7110_MAX_OUTPUT 1 /* 1 YUV */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#define SAA7110_NR_REG 0x35
57
58struct saa7110 {
Hans Verkuile7946842009-02-19 12:24:27 -030059 struct v4l2_subdev sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 u8 reg[SAA7110_NR_REG];
61
Hans Verkuil107063c2009-02-18 17:26:06 -030062 v4l2_std_id norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 int input;
64 int enable;
65 int bright;
66 int contrast;
67 int hue;
68 int sat;
69
70 wait_queue_head_t wq;
71};
72
Hans Verkuile7946842009-02-19 12:24:27 -030073static inline struct saa7110 *to_saa7110(struct v4l2_subdev *sd)
74{
75 return container_of(sd, struct saa7110, sd);
76}
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/* ----------------------------------------------------------------------- */
79/* I2C support functions */
80/* ----------------------------------------------------------------------- */
81
Hans Verkuile7946842009-02-19 12:24:27 -030082static int saa7110_write(struct v4l2_subdev *sd, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Hans Verkuile7946842009-02-19 12:24:27 -030084 struct i2c_client *client = v4l2_get_subdevdata(sd);
85 struct saa7110 *decoder = to_saa7110(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 decoder->reg[reg] = value;
88 return i2c_smbus_write_byte_data(client, reg, value);
89}
90
Hans Verkuile7946842009-02-19 12:24:27 -030091static int saa7110_write_block(struct v4l2_subdev *sd, const u8 *data, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Hans Verkuile7946842009-02-19 12:24:27 -030093 struct i2c_client *client = v4l2_get_subdevdata(sd);
94 struct saa7110 *decoder = to_saa7110(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 int ret = -1;
96 u8 reg = *data; /* first register to write to */
97
98 /* Sanity check */
99 if (reg + (len - 1) > SAA7110_NR_REG)
100 return ret;
101
102 /* the saa7110 has an autoincrement function, use it if
103 * the adapter understands raw I2C */
104 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
Jean Delvare9aa45e32006-03-22 03:48:35 -0300105 ret = i2c_master_send(client, data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 /* Cache the written data */
108 memcpy(decoder->reg + reg, data + 1, len - 1);
109 } else {
110 for (++data, --len; len; len--) {
Hans Verkuile7946842009-02-19 12:24:27 -0300111 ret = saa7110_write(sd, reg++, *data++);
Hans Verkuil85ede692008-09-07 08:00:32 -0300112 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 break;
114 }
115 }
116
117 return ret;
118}
119
Hans Verkuile7946842009-02-19 12:24:27 -0300120static inline int saa7110_read(struct v4l2_subdev *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Hans Verkuile7946842009-02-19 12:24:27 -0300122 struct i2c_client *client = v4l2_get_subdevdata(sd);
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return i2c_smbus_read_byte(client);
125}
126
127/* ----------------------------------------------------------------------- */
128/* SAA7110 functions */
129/* ----------------------------------------------------------------------- */
130
Hans Verkuile7946842009-02-19 12:24:27 -0300131#define FRESP_06H_COMPST 0x03 /*0x13*/
132#define FRESP_06H_SVIDEO 0x83 /*0xC0*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134
Hans Verkuile7946842009-02-19 12:24:27 -0300135static int saa7110_selmux(struct v4l2_subdev *sd, int chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 static const unsigned char modes[9][8] = {
138 /* mode 0 */
139 {FRESP_06H_COMPST, 0xD9, 0x17, 0x40, 0x03,
140 0x44, 0x75, 0x16},
141 /* mode 1 */
142 {FRESP_06H_COMPST, 0xD8, 0x17, 0x40, 0x03,
143 0x44, 0x75, 0x16},
144 /* mode 2 */
145 {FRESP_06H_COMPST, 0xBA, 0x07, 0x91, 0x03,
146 0x60, 0xB5, 0x05},
147 /* mode 3 */
148 {FRESP_06H_COMPST, 0xB8, 0x07, 0x91, 0x03,
149 0x60, 0xB5, 0x05},
150 /* mode 4 */
151 {FRESP_06H_COMPST, 0x7C, 0x07, 0xD2, 0x83,
152 0x60, 0xB5, 0x03},
153 /* mode 5 */
154 {FRESP_06H_COMPST, 0x78, 0x07, 0xD2, 0x83,
155 0x60, 0xB5, 0x03},
156 /* mode 6 */
157 {FRESP_06H_SVIDEO, 0x59, 0x17, 0x42, 0xA3,
158 0x44, 0x75, 0x12},
159 /* mode 7 */
160 {FRESP_06H_SVIDEO, 0x9A, 0x17, 0xB1, 0x13,
161 0x60, 0xB5, 0x14},
162 /* mode 8 */
163 {FRESP_06H_SVIDEO, 0x3C, 0x27, 0xC1, 0x23,
164 0x44, 0x75, 0x21}
165 };
Hans Verkuile7946842009-02-19 12:24:27 -0300166 struct saa7110 *decoder = to_saa7110(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 const unsigned char *ptr = modes[chan];
168
Hans Verkuile7946842009-02-19 12:24:27 -0300169 saa7110_write(sd, 0x06, ptr[0]); /* Luminance control */
170 saa7110_write(sd, 0x20, ptr[1]); /* Analog Control #1 */
171 saa7110_write(sd, 0x21, ptr[2]); /* Analog Control #2 */
172 saa7110_write(sd, 0x22, ptr[3]); /* Mixer Control #1 */
173 saa7110_write(sd, 0x2C, ptr[4]); /* Mixer Control #2 */
174 saa7110_write(sd, 0x30, ptr[5]); /* ADCs gain control */
175 saa7110_write(sd, 0x31, ptr[6]); /* Mixer Control #3 */
176 saa7110_write(sd, 0x21, ptr[7]); /* Analog Control #2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 decoder->input = chan;
178
179 return 0;
180}
181
182static const unsigned char initseq[1 + SAA7110_NR_REG] = {
183 0, 0x4C, 0x3C, 0x0D, 0xEF, 0xBD, 0xF2, 0x03, 0x00,
184 /* 0x08 */ 0xF8, 0xF8, 0x60, 0x60, 0x00, 0x86, 0x18, 0x90,
185 /* 0x10 */ 0x00, 0x59, 0x40, 0x46, 0x42, 0x1A, 0xFF, 0xDA,
186 /* 0x18 */ 0xF2, 0x8B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
187 /* 0x20 */ 0xD9, 0x16, 0x40, 0x41, 0x80, 0x41, 0x80, 0x4F,
188 /* 0x28 */ 0xFE, 0x01, 0xCF, 0x0F, 0x03, 0x01, 0x03, 0x0C,
189 /* 0x30 */ 0x44, 0x71, 0x02, 0x8C, 0x02
190};
191
Hans Verkuile7946842009-02-19 12:24:27 -0300192static v4l2_std_id determine_norm(struct v4l2_subdev *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 DEFINE_WAIT(wait);
Hans Verkuile7946842009-02-19 12:24:27 -0300195 struct saa7110 *decoder = to_saa7110(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 int status;
197
198 /* mode changed, start automatic detection */
Hans Verkuile7946842009-02-19 12:24:27 -0300199 saa7110_write_block(sd, initseq, sizeof(initseq));
200 saa7110_selmux(sd, decoder->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 prepare_to_wait(&decoder->wq, &wait, TASK_UNINTERRUPTIBLE);
Mauro Carvalho Chehab09df5cb2007-07-17 16:25:38 -0300202 schedule_timeout(msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 finish_wait(&decoder->wq, &wait);
Hans Verkuile7946842009-02-19 12:24:27 -0300204 status = saa7110_read(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (status & 0x40) {
Hans Verkuile7946842009-02-19 12:24:27 -0300206 v4l2_dbg(1, debug, sd, "status=0x%02x (no signal)\n", status);
207 return decoder->norm; /* no change*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
209 if ((status & 3) == 0) {
Hans Verkuile7946842009-02-19 12:24:27 -0300210 saa7110_write(sd, 0x06, 0x83);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (status & 0x20) {
Hans Verkuile7946842009-02-19 12:24:27 -0300212 v4l2_dbg(1, debug, sd, "status=0x%02x (NTSC/no color)\n", status);
213 /*saa7110_write(sd,0x2E,0x81);*/
Hans Verkuil107063c2009-02-18 17:26:06 -0300214 return V4L2_STD_NTSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
Hans Verkuile7946842009-02-19 12:24:27 -0300216 v4l2_dbg(1, debug, sd, "status=0x%02x (PAL/no color)\n", status);
217 /*saa7110_write(sd,0x2E,0x9A);*/
Hans Verkuil107063c2009-02-18 17:26:06 -0300218 return V4L2_STD_PAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
Hans Verkuile7946842009-02-19 12:24:27 -0300220 /*saa7110_write(sd,0x06,0x03);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (status & 0x20) { /* 60Hz */
Hans Verkuile7946842009-02-19 12:24:27 -0300222 v4l2_dbg(1, debug, sd, "status=0x%02x (NTSC)\n", status);
223 saa7110_write(sd, 0x0D, 0x86);
224 saa7110_write(sd, 0x0F, 0x50);
225 saa7110_write(sd, 0x11, 0x2C);
226 /*saa7110_write(sd,0x2E,0x81);*/
Hans Verkuil107063c2009-02-18 17:26:06 -0300227 return V4L2_STD_NTSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229
230 /* 50Hz -> PAL/SECAM */
Hans Verkuile7946842009-02-19 12:24:27 -0300231 saa7110_write(sd, 0x0D, 0x86);
232 saa7110_write(sd, 0x0F, 0x10);
233 saa7110_write(sd, 0x11, 0x59);
234 /*saa7110_write(sd,0x2E,0x9A);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 prepare_to_wait(&decoder->wq, &wait, TASK_UNINTERRUPTIBLE);
Mauro Carvalho Chehab09df5cb2007-07-17 16:25:38 -0300237 schedule_timeout(msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 finish_wait(&decoder->wq, &wait);
239
Hans Verkuile7946842009-02-19 12:24:27 -0300240 status = saa7110_read(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if ((status & 0x03) == 0x01) {
Hans Verkuile7946842009-02-19 12:24:27 -0300242 v4l2_dbg(1, debug, sd, "status=0x%02x (SECAM)\n", status);
243 saa7110_write(sd, 0x0D, 0x87);
Hans Verkuil107063c2009-02-18 17:26:06 -0300244 return V4L2_STD_SECAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
Hans Verkuile7946842009-02-19 12:24:27 -0300246 v4l2_dbg(1, debug, sd, "status=0x%02x (PAL)\n", status);
Hans Verkuil107063c2009-02-18 17:26:06 -0300247 return V4L2_STD_PAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
Hans Verkuile7946842009-02-19 12:24:27 -0300250static int saa7110_g_input_status(struct v4l2_subdev *sd, u32 *pstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Hans Verkuile7946842009-02-19 12:24:27 -0300252 struct saa7110 *decoder = to_saa7110(sd);
253 int res = V4L2_IN_ST_NO_SIGNAL;
254 int status = saa7110_read(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Hans Verkuile7946842009-02-19 12:24:27 -0300256 v4l2_dbg(1, debug, sd, "status=0x%02x norm=%llx\n",
257 status, decoder->norm);
258 if (!(status & 0x40))
259 res = 0;
260 if (!(status & 0x03))
261 res |= V4L2_IN_ST_NO_COLOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Hans Verkuile7946842009-02-19 12:24:27 -0300263 *pstatus = res;
264 return 0;
265}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Hans Verkuile7946842009-02-19 12:24:27 -0300267static int saa7110_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
268{
269 *(v4l2_std_id *)std = determine_norm(sd);
270 return 0;
271}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Hans Verkuile7946842009-02-19 12:24:27 -0300273static int saa7110_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
274{
275 struct saa7110 *decoder = to_saa7110(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Hans Verkuile7946842009-02-19 12:24:27 -0300277 if (decoder->norm != std) {
278 decoder->norm = std;
279 /*saa7110_write(sd, 0x06, 0x03);*/
280 if (std & V4L2_STD_NTSC) {
281 saa7110_write(sd, 0x0D, 0x86);
282 saa7110_write(sd, 0x0F, 0x50);
283 saa7110_write(sd, 0x11, 0x2C);
284 /*saa7110_write(sd, 0x2E, 0x81);*/
285 v4l2_dbg(1, debug, sd, "switched to NTSC\n");
286 } else if (std & V4L2_STD_PAL) {
287 saa7110_write(sd, 0x0D, 0x86);
288 saa7110_write(sd, 0x0F, 0x10);
289 saa7110_write(sd, 0x11, 0x59);
290 /*saa7110_write(sd, 0x2E, 0x9A);*/
291 v4l2_dbg(1, debug, sd, "switched to PAL\n");
292 } else if (std & V4L2_STD_SECAM) {
293 saa7110_write(sd, 0x0D, 0x87);
294 saa7110_write(sd, 0x0F, 0x10);
295 saa7110_write(sd, 0x11, 0x59);
296 /*saa7110_write(sd, 0x2E, 0x9A);*/
297 v4l2_dbg(1, debug, sd, "switched to SECAM\n");
298 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return -EINVAL;
300 }
Hans Verkuil85ede692008-09-07 08:00:32 -0300301 }
Hans Verkuile7946842009-02-19 12:24:27 -0300302 return 0;
303}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Hans Verkuile7946842009-02-19 12:24:27 -0300305static int saa7110_s_routing(struct v4l2_subdev *sd, const struct v4l2_routing *route)
306{
307 struct saa7110 *decoder = to_saa7110(sd);
Hans Verkuil107063c2009-02-18 17:26:06 -0300308
Hans Verkuile7946842009-02-19 12:24:27 -0300309 if (route->input < 0 || route->input >= SAA7110_MAX_INPUT) {
310 v4l2_dbg(1, debug, sd, "input=%d not available\n", route->input);
311 return -EINVAL;
Hans Verkuil107063c2009-02-18 17:26:06 -0300312 }
Hans Verkuile7946842009-02-19 12:24:27 -0300313 if (decoder->input != route->input) {
314 saa7110_selmux(sd, route->input);
315 v4l2_dbg(1, debug, sd, "switched to input=%d\n", route->input);
Hans Verkuil107063c2009-02-18 17:26:06 -0300316 }
Hans Verkuile7946842009-02-19 12:24:27 -0300317 return 0;
318}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Hans Verkuile7946842009-02-19 12:24:27 -0300320static int saa7110_s_stream(struct v4l2_subdev *sd, int enable)
321{
322 struct saa7110 *decoder = to_saa7110(sd);
323
324 if (decoder->enable != enable) {
325 decoder->enable = enable;
326 saa7110_write(sd, 0x0E, enable ? 0x18 : 0x80);
327 v4l2_dbg(1, debug, sd, "YUV %s\n", enable ? "on" : "off");
328 }
329 return 0;
330}
331
332static int saa7110_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
333{
334 switch (qc->id) {
335 case V4L2_CID_BRIGHTNESS:
336 return v4l2_ctrl_query_fill(qc, 0, 255, 1, 128);
337 case V4L2_CID_CONTRAST:
338 case V4L2_CID_SATURATION:
339 return v4l2_ctrl_query_fill(qc, 0, 127, 1, 64);
340 case V4L2_CID_HUE:
341 return v4l2_ctrl_query_fill(qc, -128, 127, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return -EINVAL;
344 }
345 return 0;
346}
347
Hans Verkuile7946842009-02-19 12:24:27 -0300348static int saa7110_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
349{
350 struct saa7110 *decoder = to_saa7110(sd);
351
352 switch (ctrl->id) {
353 case V4L2_CID_BRIGHTNESS:
354 ctrl->value = decoder->bright;
355 break;
356 case V4L2_CID_CONTRAST:
357 ctrl->value = decoder->contrast;
358 break;
359 case V4L2_CID_SATURATION:
360 ctrl->value = decoder->sat;
361 break;
362 case V4L2_CID_HUE:
363 ctrl->value = decoder->hue;
364 break;
365 default:
366 return -EINVAL;
367 }
368 return 0;
369}
370
371static int saa7110_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
372{
373 struct saa7110 *decoder = to_saa7110(sd);
374
375 switch (ctrl->id) {
376 case V4L2_CID_BRIGHTNESS:
377 if (decoder->bright != ctrl->value) {
378 decoder->bright = ctrl->value;
379 saa7110_write(sd, 0x19, decoder->bright);
380 }
381 break;
382 case V4L2_CID_CONTRAST:
383 if (decoder->contrast != ctrl->value) {
384 decoder->contrast = ctrl->value;
385 saa7110_write(sd, 0x13, decoder->contrast);
386 }
387 break;
388 case V4L2_CID_SATURATION:
389 if (decoder->sat != ctrl->value) {
390 decoder->sat = ctrl->value;
391 saa7110_write(sd, 0x12, decoder->sat);
392 }
393 break;
394 case V4L2_CID_HUE:
395 if (decoder->hue != ctrl->value) {
396 decoder->hue = ctrl->value;
397 saa7110_write(sd, 0x07, decoder->hue);
398 }
399 break;
400 default:
401 return -EINVAL;
402 }
403 return 0;
404}
405
406static int saa7110_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
407{
408 struct i2c_client *client = v4l2_get_subdevdata(sd);
409
410 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_SAA7110, 0);
411}
412
413static int saa7110_command(struct i2c_client *client, unsigned cmd, void *arg)
414{
415 return v4l2_subdev_command(i2c_get_clientdata(client), cmd, arg);
416}
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418/* ----------------------------------------------------------------------- */
419
Hans Verkuile7946842009-02-19 12:24:27 -0300420static const struct v4l2_subdev_core_ops saa7110_core_ops = {
421 .g_chip_ident = saa7110_g_chip_ident,
422 .g_ctrl = saa7110_g_ctrl,
423 .s_ctrl = saa7110_s_ctrl,
424 .queryctrl = saa7110_queryctrl,
425};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Hans Verkuile7946842009-02-19 12:24:27 -0300427static const struct v4l2_subdev_tuner_ops saa7110_tuner_ops = {
428 .s_std = saa7110_s_std,
429};
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300430
Hans Verkuile7946842009-02-19 12:24:27 -0300431static const struct v4l2_subdev_video_ops saa7110_video_ops = {
432 .s_routing = saa7110_s_routing,
433 .s_stream = saa7110_s_stream,
434 .querystd = saa7110_querystd,
435 .g_input_status = saa7110_g_input_status,
436};
437
438static const struct v4l2_subdev_ops saa7110_ops = {
439 .core = &saa7110_core_ops,
440 .tuner = &saa7110_tuner_ops,
441 .video = &saa7110_video_ops,
442};
443
444/* ----------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Hans Verkuil85ede692008-09-07 08:00:32 -0300446static int saa7110_probe(struct i2c_client *client,
447 const struct i2c_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 struct saa7110 *decoder;
Hans Verkuile7946842009-02-19 12:24:27 -0300450 struct v4l2_subdev *sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 int rv;
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 /* Check if the adapter supports the needed features */
Hans Verkuil85ede692008-09-07 08:00:32 -0300454 if (!i2c_check_functionality(client->adapter,
455 I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
456 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Hans Verkuil85ede692008-09-07 08:00:32 -0300458 v4l_info(client, "chip found @ 0x%x (%s)\n",
459 client->addr << 1, client->adapter->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Panagiotis Issaris74081872006-01-11 19:40:56 -0200461 decoder = kzalloc(sizeof(struct saa7110), GFP_KERNEL);
Hans Verkuil85ede692008-09-07 08:00:32 -0300462 if (!decoder)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return -ENOMEM;
Hans Verkuile7946842009-02-19 12:24:27 -0300464 sd = &decoder->sd;
465 v4l2_i2c_subdev_init(sd, client, &saa7110_ops);
Hans Verkuil107063c2009-02-18 17:26:06 -0300466 decoder->norm = V4L2_STD_PAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 decoder->input = 0;
468 decoder->enable = 1;
469 decoder->bright = 32768;
470 decoder->contrast = 32768;
471 decoder->hue = 32768;
472 decoder->sat = 32768;
473 init_waitqueue_head(&decoder->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Hans Verkuile7946842009-02-19 12:24:27 -0300475 rv = saa7110_write_block(sd, initseq, sizeof(initseq));
Hans Verkuil85ede692008-09-07 08:00:32 -0300476 if (rv < 0) {
Hans Verkuile7946842009-02-19 12:24:27 -0300477 v4l2_dbg(1, debug, sd, "init status %d\n", rv);
Hans Verkuil85ede692008-09-07 08:00:32 -0300478 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 int ver, status;
Hans Verkuile7946842009-02-19 12:24:27 -0300480 saa7110_write(sd, 0x21, 0x10);
481 saa7110_write(sd, 0x0e, 0x18);
482 saa7110_write(sd, 0x0D, 0x04);
483 ver = saa7110_read(sd);
484 saa7110_write(sd, 0x0D, 0x06);
485 /*mdelay(150);*/
486 status = saa7110_read(sd);
487 v4l2_dbg(1, debug, sd, "version %x, status=0x%02x\n",
Hans Verkuil85ede692008-09-07 08:00:32 -0300488 ver, status);
Hans Verkuile7946842009-02-19 12:24:27 -0300489 saa7110_write(sd, 0x0D, 0x86);
490 saa7110_write(sd, 0x0F, 0x10);
491 saa7110_write(sd, 0x11, 0x59);
492 /*saa7110_write(sd, 0x2E, 0x9A);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
494
Hans Verkuile7946842009-02-19 12:24:27 -0300495 /*saa7110_selmux(sd,0);*/
496 /*determine_norm(sd);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 /* setup and implicit mode 0 select has been performed */
498
499 return 0;
500}
501
Hans Verkuil85ede692008-09-07 08:00:32 -0300502static int saa7110_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Hans Verkuile7946842009-02-19 12:24:27 -0300504 struct v4l2_subdev *sd = i2c_get_clientdata(client);
505
506 v4l2_device_unregister_subdev(sd);
507 kfree(to_saa7110(sd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return 0;
509}
510
511/* ----------------------------------------------------------------------- */
512
Hans Verkuil85ede692008-09-07 08:00:32 -0300513static const struct i2c_device_id saa7110_id[] = {
514 { "saa7110", 0 },
515 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516};
Hans Verkuil85ede692008-09-07 08:00:32 -0300517MODULE_DEVICE_TABLE(i2c, saa7110_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Hans Verkuil85ede692008-09-07 08:00:32 -0300519static struct v4l2_i2c_driver_data v4l2_i2c_data = {
520 .name = "saa7110",
521 .driverid = I2C_DRIVERID_SAA7110,
522 .command = saa7110_command,
523 .probe = saa7110_probe,
524 .remove = saa7110_remove,
525 .id_table = saa7110_id,
526};