blob: a2513772196bf41279bed4c65a2af61114b8ecc7 [file] [log] [blame]
Ralf Baechled203a7e2005-09-06 15:19:37 -07001/*
2 * saa7191.c - Philips SAA7191 video decoder driver
3 *
4 * Copyright (C) 2003 Ladislav Michl <ladis@linux-mips.org>
5 * Copyright (C) 2004,2005 Mikael Nousiainen <tmnousia@cc.hut.fi>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
Ralf Baechled203a7e2005-09-06 15:19:37 -070012#include <linux/delay.h>
13#include <linux/errno.h>
14#include <linux/fs.h>
Ladislav Michl495515b2005-09-23 10:52:27 +000015#include <linux/init.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070016#include <linux/kernel.h>
17#include <linux/major.h>
Ladislav Michl495515b2005-09-23 10:52:27 +000018#include <linux/module.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070019#include <linux/mm.h>
Ladislav Michl495515b2005-09-23 10:52:27 +000020#include <linux/slab.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070021
Hans Verkuilcf4e9482009-02-27 09:05:10 -030022#include <linux/videodev2.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070023#include <linux/i2c.h>
Hans Verkuil8340ff42009-02-13 19:58:12 -030024#include <media/v4l2-device.h>
25#include <media/v4l2-chip-ident.h>
Hans Verkuila0720d32009-02-18 19:23:43 -030026#include <media/v4l2-i2c-drv.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070027
28#include "saa7191.h"
29
Ladislav Michla637a112005-09-01 15:07:34 +000030#define SAA7191_MODULE_VERSION "0.0.5"
Ralf Baechled203a7e2005-09-06 15:19:37 -070031
32MODULE_DESCRIPTION("Philips SAA7191 video decoder driver");
33MODULE_VERSION(SAA7191_MODULE_VERSION);
34MODULE_AUTHOR("Mikael Nousiainen <tmnousia@cc.hut.fi>");
35MODULE_LICENSE("GPL");
36
Hans Verkuilbabb7dc2009-02-13 07:31:05 -030037
Ladislav Michla637a112005-09-01 15:07:34 +000038// #define SAA7191_DEBUG
39
40#ifdef SAA7191_DEBUG
41#define dprintk(x...) printk("SAA7191: " x);
42#else
43#define dprintk(x...)
44#endif
45
46#define SAA7191_SYNC_COUNT 30
47#define SAA7191_SYNC_DELAY 100 /* milliseconds */
48
Ralf Baechled203a7e2005-09-06 15:19:37 -070049struct saa7191 {
Hans Verkuil8340ff42009-02-13 19:58:12 -030050 struct v4l2_subdev sd;
Ralf Baechled203a7e2005-09-06 15:19:37 -070051
52 /* the register values are stored here as the actual
53 * I2C-registers are write-only */
Ladislav Michla637a112005-09-01 15:07:34 +000054 u8 reg[25];
Ralf Baechled203a7e2005-09-06 15:19:37 -070055
Ladislav Michla637a112005-09-01 15:07:34 +000056 int input;
Hans Verkuilcf4e9482009-02-27 09:05:10 -030057 v4l2_std_id norm;
Ralf Baechled203a7e2005-09-06 15:19:37 -070058};
59
Hans Verkuil8340ff42009-02-13 19:58:12 -030060static inline struct saa7191 *to_saa7191(struct v4l2_subdev *sd)
61{
62 return container_of(sd, struct saa7191, sd);
63}
64
Ladislav Michla637a112005-09-01 15:07:34 +000065static const u8 initseq[] = {
Ralf Baechled203a7e2005-09-06 15:19:37 -070066 0, /* Subaddress */
Ladislav Michla637a112005-09-01 15:07:34 +000067
68 0x50, /* (0x50) SAA7191_REG_IDEL */
69
70 /* 50 Hz signal timing */
71 0x30, /* (0x30) SAA7191_REG_HSYB */
72 0x00, /* (0x00) SAA7191_REG_HSYS */
73 0xe8, /* (0xe8) SAA7191_REG_HCLB */
74 0xb6, /* (0xb6) SAA7191_REG_HCLS */
75 0xf4, /* (0xf4) SAA7191_REG_HPHI */
76
77 /* control */
78 SAA7191_LUMA_APER_1, /* (0x01) SAA7191_REG_LUMA - CVBS mode */
79 0x00, /* (0x00) SAA7191_REG_HUEC */
80 0xf8, /* (0xf8) SAA7191_REG_CKTQ */
81 0xf8, /* (0xf8) SAA7191_REG_CKTS */
82 0x90, /* (0x90) SAA7191_REG_PLSE */
83 0x90, /* (0x90) SAA7191_REG_SESE */
84 0x00, /* (0x00) SAA7191_REG_GAIN */
85 SAA7191_STDC_NFEN | SAA7191_STDC_HRMV, /* (0x0c) SAA7191_REG_STDC
86 * - not SECAM,
87 * slow time constant */
88 SAA7191_IOCK_OEDC | SAA7191_IOCK_OEHS | SAA7191_IOCK_OEVS
89 | SAA7191_IOCK_OEDY, /* (0x78) SAA7191_REG_IOCK
90 * - chroma from CVBS, GPSW1 & 2 off */
91 SAA7191_CTL3_AUFD | SAA7191_CTL3_SCEN | SAA7191_CTL3_OFTS
92 | SAA7191_CTL3_YDEL0, /* (0x99) SAA7191_REG_CTL3
93 * - automatic field detection */
94 0x00, /* (0x00) SAA7191_REG_CTL4 */
95 0x2c, /* (0x2c) SAA7191_REG_CHCV - PAL nominal value */
Ralf Baechled203a7e2005-09-06 15:19:37 -070096 0x00, /* unused */
97 0x00, /* unused */
Ladislav Michla637a112005-09-01 15:07:34 +000098
99 /* 60 Hz signal timing */
100 0x34, /* (0x34) SAA7191_REG_HS6B */
101 0x0a, /* (0x0a) SAA7191_REG_HS6S */
102 0xf4, /* (0xf4) SAA7191_REG_HC6B */
103 0xce, /* (0xce) SAA7191_REG_HC6S */
104 0xf4, /* (0xf4) SAA7191_REG_HP6I */
Ralf Baechled203a7e2005-09-06 15:19:37 -0700105};
106
107/* SAA7191 register handling */
108
Hans Verkuil8340ff42009-02-13 19:58:12 -0300109static u8 saa7191_read_reg(struct v4l2_subdev *sd, u8 reg)
Ralf Baechled203a7e2005-09-06 15:19:37 -0700110{
Hans Verkuil8340ff42009-02-13 19:58:12 -0300111 return to_saa7191(sd)->reg[reg];
Ralf Baechled203a7e2005-09-06 15:19:37 -0700112}
113
Hans Verkuil8340ff42009-02-13 19:58:12 -0300114static int saa7191_read_status(struct v4l2_subdev *sd, u8 *value)
Ralf Baechled203a7e2005-09-06 15:19:37 -0700115{
Hans Verkuil8340ff42009-02-13 19:58:12 -0300116 struct i2c_client *client = v4l2_get_subdevdata(sd);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700117 int ret;
118
119 ret = i2c_master_recv(client, value, 1);
120 if (ret < 0) {
Ladislav Michla637a112005-09-01 15:07:34 +0000121 printk(KERN_ERR "SAA7191: saa7191_read_status(): read failed\n");
Ralf Baechled203a7e2005-09-06 15:19:37 -0700122 return ret;
123 }
124
125 return 0;
126}
127
128
Hans Verkuil8340ff42009-02-13 19:58:12 -0300129static int saa7191_write_reg(struct v4l2_subdev *sd, u8 reg, u8 value)
Ralf Baechled203a7e2005-09-06 15:19:37 -0700130{
Hans Verkuil8340ff42009-02-13 19:58:12 -0300131 struct i2c_client *client = v4l2_get_subdevdata(sd);
132
133 to_saa7191(sd)->reg[reg] = value;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700134 return i2c_smbus_write_byte_data(client, reg, value);
135}
136
137/* the first byte of data must be the first subaddress number (register) */
Hans Verkuil8340ff42009-02-13 19:58:12 -0300138static int saa7191_write_block(struct v4l2_subdev *sd,
Mauro Carvalho Chehabf5ab2722007-09-03 12:01:51 -0300139 u8 length, const u8 *data)
Ralf Baechled203a7e2005-09-06 15:19:37 -0700140{
Hans Verkuil8340ff42009-02-13 19:58:12 -0300141 struct i2c_client *client = v4l2_get_subdevdata(sd);
142 struct saa7191 *decoder = to_saa7191(sd);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700143 int i;
144 int ret;
145
Ralf Baechled203a7e2005-09-06 15:19:37 -0700146 for (i = 0; i < (length - 1); i++) {
147 decoder->reg[data[0] + i] = data[i + 1];
148 }
149
150 ret = i2c_master_send(client, data, length);
151 if (ret < 0) {
152 printk(KERN_ERR "SAA7191: saa7191_write_block(): "
Ladislav Michla637a112005-09-01 15:07:34 +0000153 "write failed\n");
Ralf Baechled203a7e2005-09-06 15:19:37 -0700154 return ret;
155 }
156
157 return 0;
158}
159
160/* Helper functions */
161
Hans Verkuil8340ff42009-02-13 19:58:12 -0300162static int saa7191_s_routing(struct v4l2_subdev *sd,
Hans Verkuil5325b422009-04-02 11:26:22 -0300163 u32 input, u32 output, u32 config)
Ralf Baechled203a7e2005-09-06 15:19:37 -0700164{
Hans Verkuil8340ff42009-02-13 19:58:12 -0300165 struct saa7191 *decoder = to_saa7191(sd);
166 u8 luma = saa7191_read_reg(sd, SAA7191_REG_LUMA);
167 u8 iock = saa7191_read_reg(sd, SAA7191_REG_IOCK);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700168 int err;
169
Hans Verkuil5325b422009-04-02 11:26:22 -0300170 switch (input) {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700171 case SAA7191_INPUT_COMPOSITE: /* Set Composite input */
172 iock &= ~(SAA7191_IOCK_CHRS | SAA7191_IOCK_GPSW1
173 | SAA7191_IOCK_GPSW2);
174 /* Chrominance trap active */
175 luma &= ~SAA7191_LUMA_BYPS;
176 break;
177 case SAA7191_INPUT_SVIDEO: /* Set S-Video input */
178 iock |= SAA7191_IOCK_CHRS | SAA7191_IOCK_GPSW2;
179 /* Chrominance trap bypassed */
180 luma |= SAA7191_LUMA_BYPS;
181 break;
182 default:
183 return -EINVAL;
184 }
185
Hans Verkuil8340ff42009-02-13 19:58:12 -0300186 err = saa7191_write_reg(sd, SAA7191_REG_LUMA, luma);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700187 if (err)
188 return -EIO;
Hans Verkuil8340ff42009-02-13 19:58:12 -0300189 err = saa7191_write_reg(sd, SAA7191_REG_IOCK, iock);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700190 if (err)
191 return -EIO;
192
Hans Verkuil5325b422009-04-02 11:26:22 -0300193 decoder->input = input;
Ladislav Michla637a112005-09-01 15:07:34 +0000194
Ralf Baechled203a7e2005-09-06 15:19:37 -0700195 return 0;
196}
197
Hans Verkuil8340ff42009-02-13 19:58:12 -0300198static int saa7191_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
Ralf Baechled203a7e2005-09-06 15:19:37 -0700199{
Hans Verkuil8340ff42009-02-13 19:58:12 -0300200 struct saa7191 *decoder = to_saa7191(sd);
201 u8 stdc = saa7191_read_reg(sd, SAA7191_REG_STDC);
202 u8 ctl3 = saa7191_read_reg(sd, SAA7191_REG_CTL3);
203 u8 chcv = saa7191_read_reg(sd, SAA7191_REG_CHCV);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700204 int err;
205
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300206 if (norm & V4L2_STD_PAL) {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700207 stdc &= ~SAA7191_STDC_SECS;
208 ctl3 &= ~(SAA7191_CTL3_AUFD | SAA7191_CTL3_FSEL);
209 chcv = SAA7191_CHCV_PAL;
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300210 } else if (norm & V4L2_STD_NTSC) {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700211 stdc &= ~SAA7191_STDC_SECS;
212 ctl3 &= ~SAA7191_CTL3_AUFD;
213 ctl3 |= SAA7191_CTL3_FSEL;
214 chcv = SAA7191_CHCV_NTSC;
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300215 } else if (norm & V4L2_STD_SECAM) {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700216 stdc |= SAA7191_STDC_SECS;
217 ctl3 &= ~(SAA7191_CTL3_AUFD | SAA7191_CTL3_FSEL);
218 chcv = SAA7191_CHCV_PAL;
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300219 } else {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700220 return -EINVAL;
221 }
222
Hans Verkuil8340ff42009-02-13 19:58:12 -0300223 err = saa7191_write_reg(sd, SAA7191_REG_CTL3, ctl3);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700224 if (err)
225 return -EIO;
Hans Verkuil8340ff42009-02-13 19:58:12 -0300226 err = saa7191_write_reg(sd, SAA7191_REG_STDC, stdc);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700227 if (err)
228 return -EIO;
Hans Verkuil8340ff42009-02-13 19:58:12 -0300229 err = saa7191_write_reg(sd, SAA7191_REG_CHCV, chcv);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700230 if (err)
231 return -EIO;
232
233 decoder->norm = norm;
234
Ladislav Michla637a112005-09-01 15:07:34 +0000235 dprintk("ctl3: %02x stdc: %02x chcv: %02x\n", ctl3,
236 stdc, chcv);
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300237 dprintk("norm: %llx\n", norm);
Ladislav Michla637a112005-09-01 15:07:34 +0000238
Ralf Baechled203a7e2005-09-06 15:19:37 -0700239 return 0;
240}
241
Hans Verkuil8340ff42009-02-13 19:58:12 -0300242static int saa7191_wait_for_signal(struct v4l2_subdev *sd, u8 *status)
Ralf Baechled203a7e2005-09-06 15:19:37 -0700243{
Ladislav Michla637a112005-09-01 15:07:34 +0000244 int i = 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700245
Ladislav Michla637a112005-09-01 15:07:34 +0000246 dprintk("Checking for signal...\n");
247
248 for (i = 0; i < SAA7191_SYNC_COUNT; i++) {
Hans Verkuil8340ff42009-02-13 19:58:12 -0300249 if (saa7191_read_status(sd, status))
Ladislav Michla637a112005-09-01 15:07:34 +0000250 return -EIO;
251
252 if (((*status) & SAA7191_STATUS_HLCK) == 0) {
253 dprintk("Signal found\n");
254 return 0;
255 }
256
257 msleep(SAA7191_SYNC_DELAY);
258 }
259
260 dprintk("No signal\n");
261
262 return -EBUSY;
263}
264
Hans Verkuil8340ff42009-02-13 19:58:12 -0300265static int saa7191_querystd(struct v4l2_subdev *sd, v4l2_std_id *norm)
Ladislav Michla637a112005-09-01 15:07:34 +0000266{
Hans Verkuil8340ff42009-02-13 19:58:12 -0300267 struct saa7191 *decoder = to_saa7191(sd);
268 u8 stdc = saa7191_read_reg(sd, SAA7191_REG_STDC);
269 u8 ctl3 = saa7191_read_reg(sd, SAA7191_REG_CTL3);
Ladislav Michla637a112005-09-01 15:07:34 +0000270 u8 status;
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300271 v4l2_std_id old_norm = decoder->norm;
Ladislav Michla637a112005-09-01 15:07:34 +0000272 int err = 0;
273
274 dprintk("SAA7191 extended signal auto-detection...\n");
275
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300276 *norm = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM;
Ladislav Michla637a112005-09-01 15:07:34 +0000277 stdc &= ~SAA7191_STDC_SECS;
278 ctl3 &= ~(SAA7191_CTL3_FSEL);
279
Hans Verkuil8340ff42009-02-13 19:58:12 -0300280 err = saa7191_write_reg(sd, SAA7191_REG_STDC, stdc);
Ladislav Michla637a112005-09-01 15:07:34 +0000281 if (err) {
282 err = -EIO;
283 goto out;
284 }
Hans Verkuil8340ff42009-02-13 19:58:12 -0300285 err = saa7191_write_reg(sd, SAA7191_REG_CTL3, ctl3);
Ladislav Michla637a112005-09-01 15:07:34 +0000286 if (err) {
287 err = -EIO;
288 goto out;
289 }
290
291 ctl3 |= SAA7191_CTL3_AUFD;
Hans Verkuil8340ff42009-02-13 19:58:12 -0300292 err = saa7191_write_reg(sd, SAA7191_REG_CTL3, ctl3);
Ladislav Michla637a112005-09-01 15:07:34 +0000293 if (err) {
294 err = -EIO;
295 goto out;
296 }
297
298 msleep(SAA7191_SYNC_DELAY);
299
Hans Verkuil8340ff42009-02-13 19:58:12 -0300300 err = saa7191_wait_for_signal(sd, &status);
Ladislav Michla637a112005-09-01 15:07:34 +0000301 if (err)
302 goto out;
303
304 if (status & SAA7191_STATUS_FIDT) {
305 /* 60Hz signal -> NTSC */
306 dprintk("60Hz signal: NTSC\n");
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300307 *norm = V4L2_STD_NTSC;
308 return 0;
Ladislav Michla637a112005-09-01 15:07:34 +0000309 }
310
311 /* 50Hz signal */
312 dprintk("50Hz signal: Trying PAL...\n");
313
314 /* try PAL first */
Hans Verkuil8340ff42009-02-13 19:58:12 -0300315 err = saa7191_s_std(sd, V4L2_STD_PAL);
Ladislav Michla637a112005-09-01 15:07:34 +0000316 if (err)
317 goto out;
318
319 msleep(SAA7191_SYNC_DELAY);
320
Hans Verkuil8340ff42009-02-13 19:58:12 -0300321 err = saa7191_wait_for_signal(sd, &status);
Ladislav Michla637a112005-09-01 15:07:34 +0000322 if (err)
323 goto out;
324
325 /* not 50Hz ? */
326 if (status & SAA7191_STATUS_FIDT) {
327 dprintk("No 50Hz signal\n");
Hans Verkuil8340ff42009-02-13 19:58:12 -0300328 saa7191_s_std(sd, old_norm);
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300329 return -EAGAIN;
Ladislav Michla637a112005-09-01 15:07:34 +0000330 }
331
332 if (status & SAA7191_STATUS_CODE) {
333 dprintk("PAL\n");
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300334 *norm = V4L2_STD_PAL;
Hans Verkuil8340ff42009-02-13 19:58:12 -0300335 return saa7191_s_std(sd, old_norm);
Ladislav Michla637a112005-09-01 15:07:34 +0000336 }
337
338 dprintk("No color detected with PAL - Trying SECAM...\n");
339
340 /* no color detected ? -> try SECAM */
Hans Verkuil8340ff42009-02-13 19:58:12 -0300341 err = saa7191_s_std(sd, V4L2_STD_SECAM);
Ladislav Michla637a112005-09-01 15:07:34 +0000342 if (err)
343 goto out;
344
345 msleep(SAA7191_SYNC_DELAY);
346
Hans Verkuil8340ff42009-02-13 19:58:12 -0300347 err = saa7191_wait_for_signal(sd, &status);
Ladislav Michla637a112005-09-01 15:07:34 +0000348 if (err)
349 goto out;
350
351 /* not 50Hz ? */
352 if (status & SAA7191_STATUS_FIDT) {
353 dprintk("No 50Hz signal\n");
354 err = -EAGAIN;
355 goto out;
356 }
357
358 if (status & SAA7191_STATUS_CODE) {
359 /* Color detected -> SECAM */
360 dprintk("SECAM\n");
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300361 *norm = V4L2_STD_SECAM;
Hans Verkuil8340ff42009-02-13 19:58:12 -0300362 return saa7191_s_std(sd, old_norm);
Ladislav Michla637a112005-09-01 15:07:34 +0000363 }
364
365 dprintk("No color detected with SECAM - Going back to PAL.\n");
366
Ladislav Michla637a112005-09-01 15:07:34 +0000367out:
Hans Verkuil8340ff42009-02-13 19:58:12 -0300368 return saa7191_s_std(sd, old_norm);
Ladislav Michla637a112005-09-01 15:07:34 +0000369}
370
Hans Verkuil8340ff42009-02-13 19:58:12 -0300371static int saa7191_autodetect_norm(struct v4l2_subdev *sd)
Ladislav Michla637a112005-09-01 15:07:34 +0000372{
373 u8 status;
374
375 dprintk("SAA7191 signal auto-detection...\n");
376
377 dprintk("Reading status...\n");
378
Hans Verkuil8340ff42009-02-13 19:58:12 -0300379 if (saa7191_read_status(sd, &status))
Ladislav Michla637a112005-09-01 15:07:34 +0000380 return -EIO;
381
382 dprintk("Checking for signal...\n");
383
384 /* no signal ? */
385 if (status & SAA7191_STATUS_HLCK) {
386 dprintk("No signal\n");
387 return -EBUSY;
388 }
389
390 dprintk("Signal found\n");
391
392 if (status & SAA7191_STATUS_FIDT) {
393 /* 60hz signal -> NTSC */
394 dprintk("NTSC\n");
Hans Verkuil8340ff42009-02-13 19:58:12 -0300395 return saa7191_s_std(sd, V4L2_STD_NTSC);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700396 } else {
Ladislav Michla637a112005-09-01 15:07:34 +0000397 /* 50hz signal -> PAL */
398 dprintk("PAL\n");
Hans Verkuil8340ff42009-02-13 19:58:12 -0300399 return saa7191_s_std(sd, V4L2_STD_PAL);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700400 }
Ralf Baechled203a7e2005-09-06 15:19:37 -0700401}
402
Hans Verkuil8340ff42009-02-13 19:58:12 -0300403static int saa7191_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Ralf Baechled203a7e2005-09-06 15:19:37 -0700404{
Ladislav Michla637a112005-09-01 15:07:34 +0000405 u8 reg;
406 int ret = 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700407
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300408 switch (ctrl->id) {
Ladislav Michla637a112005-09-01 15:07:34 +0000409 case SAA7191_CONTROL_BANDPASS:
410 case SAA7191_CONTROL_BANDPASS_WEIGHT:
411 case SAA7191_CONTROL_CORING:
Hans Verkuil8340ff42009-02-13 19:58:12 -0300412 reg = saa7191_read_reg(sd, SAA7191_REG_LUMA);
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300413 switch (ctrl->id) {
Ladislav Michla637a112005-09-01 15:07:34 +0000414 case SAA7191_CONTROL_BANDPASS:
415 ctrl->value = ((s32)reg & SAA7191_LUMA_BPSS_MASK)
416 >> SAA7191_LUMA_BPSS_SHIFT;
417 break;
418 case SAA7191_CONTROL_BANDPASS_WEIGHT:
419 ctrl->value = ((s32)reg & SAA7191_LUMA_APER_MASK)
420 >> SAA7191_LUMA_APER_SHIFT;
421 break;
422 case SAA7191_CONTROL_CORING:
423 ctrl->value = ((s32)reg & SAA7191_LUMA_CORI_MASK)
424 >> SAA7191_LUMA_CORI_SHIFT;
425 break;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700426 }
Ladislav Michla637a112005-09-01 15:07:34 +0000427 break;
428 case SAA7191_CONTROL_FORCE_COLOUR:
429 case SAA7191_CONTROL_CHROMA_GAIN:
Hans Verkuil8340ff42009-02-13 19:58:12 -0300430 reg = saa7191_read_reg(sd, SAA7191_REG_GAIN);
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300431 if (ctrl->id == SAA7191_CONTROL_FORCE_COLOUR)
Ladislav Michla637a112005-09-01 15:07:34 +0000432 ctrl->value = ((s32)reg & SAA7191_GAIN_COLO) ? 1 : 0;
433 else
434 ctrl->value = ((s32)reg & SAA7191_GAIN_LFIS_MASK)
435 >> SAA7191_GAIN_LFIS_SHIFT;
436 break;
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300437 case V4L2_CID_HUE:
Hans Verkuil8340ff42009-02-13 19:58:12 -0300438 reg = saa7191_read_reg(sd, SAA7191_REG_HUEC);
Ladislav Michla637a112005-09-01 15:07:34 +0000439 if (reg < 0x80)
440 reg += 0x80;
441 else
442 reg -= 0x80;
443 ctrl->value = (s32)reg;
444 break;
445 case SAA7191_CONTROL_VTRC:
Hans Verkuil8340ff42009-02-13 19:58:12 -0300446 reg = saa7191_read_reg(sd, SAA7191_REG_STDC);
Ladislav Michla637a112005-09-01 15:07:34 +0000447 ctrl->value = ((s32)reg & SAA7191_STDC_VTRC) ? 1 : 0;
448 break;
449 case SAA7191_CONTROL_LUMA_DELAY:
Hans Verkuil8340ff42009-02-13 19:58:12 -0300450 reg = saa7191_read_reg(sd, SAA7191_REG_CTL3);
Ladislav Michla637a112005-09-01 15:07:34 +0000451 ctrl->value = ((s32)reg & SAA7191_CTL3_YDEL_MASK)
452 >> SAA7191_CTL3_YDEL_SHIFT;
453 if (ctrl->value >= 4)
454 ctrl->value -= 8;
455 break;
456 case SAA7191_CONTROL_VNR:
Hans Verkuil8340ff42009-02-13 19:58:12 -0300457 reg = saa7191_read_reg(sd, SAA7191_REG_CTL4);
Ladislav Michla637a112005-09-01 15:07:34 +0000458 ctrl->value = ((s32)reg & SAA7191_CTL4_VNOI_MASK)
459 >> SAA7191_CTL4_VNOI_SHIFT;
460 break;
461 default:
462 ret = -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700463 }
464
Ladislav Michla637a112005-09-01 15:07:34 +0000465 return ret;
466}
467
Hans Verkuil8340ff42009-02-13 19:58:12 -0300468static int saa7191_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Ladislav Michla637a112005-09-01 15:07:34 +0000469{
470 u8 reg;
471 int ret = 0;
472
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300473 switch (ctrl->id) {
Ladislav Michla637a112005-09-01 15:07:34 +0000474 case SAA7191_CONTROL_BANDPASS:
475 case SAA7191_CONTROL_BANDPASS_WEIGHT:
476 case SAA7191_CONTROL_CORING:
Hans Verkuil8340ff42009-02-13 19:58:12 -0300477 reg = saa7191_read_reg(sd, SAA7191_REG_LUMA);
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300478 switch (ctrl->id) {
Ladislav Michla637a112005-09-01 15:07:34 +0000479 case SAA7191_CONTROL_BANDPASS:
480 reg &= ~SAA7191_LUMA_BPSS_MASK;
481 reg |= (ctrl->value << SAA7191_LUMA_BPSS_SHIFT)
482 & SAA7191_LUMA_BPSS_MASK;
483 break;
484 case SAA7191_CONTROL_BANDPASS_WEIGHT:
485 reg &= ~SAA7191_LUMA_APER_MASK;
486 reg |= (ctrl->value << SAA7191_LUMA_APER_SHIFT)
487 & SAA7191_LUMA_APER_MASK;
488 break;
489 case SAA7191_CONTROL_CORING:
490 reg &= ~SAA7191_LUMA_CORI_MASK;
491 reg |= (ctrl->value << SAA7191_LUMA_CORI_SHIFT)
492 & SAA7191_LUMA_CORI_MASK;
493 break;
494 }
Hans Verkuil8340ff42009-02-13 19:58:12 -0300495 ret = saa7191_write_reg(sd, SAA7191_REG_LUMA, reg);
Ladislav Michla637a112005-09-01 15:07:34 +0000496 break;
497 case SAA7191_CONTROL_FORCE_COLOUR:
498 case SAA7191_CONTROL_CHROMA_GAIN:
Hans Verkuil8340ff42009-02-13 19:58:12 -0300499 reg = saa7191_read_reg(sd, SAA7191_REG_GAIN);
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300500 if (ctrl->id == SAA7191_CONTROL_FORCE_COLOUR) {
Ladislav Michla637a112005-09-01 15:07:34 +0000501 if (ctrl->value)
502 reg |= SAA7191_GAIN_COLO;
503 else
504 reg &= ~SAA7191_GAIN_COLO;
505 } else {
506 reg &= ~SAA7191_GAIN_LFIS_MASK;
507 reg |= (ctrl->value << SAA7191_GAIN_LFIS_SHIFT)
508 & SAA7191_GAIN_LFIS_MASK;
509 }
Hans Verkuil8340ff42009-02-13 19:58:12 -0300510 ret = saa7191_write_reg(sd, SAA7191_REG_GAIN, reg);
Ladislav Michla637a112005-09-01 15:07:34 +0000511 break;
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300512 case V4L2_CID_HUE:
Ladislav Michla637a112005-09-01 15:07:34 +0000513 reg = ctrl->value & 0xff;
514 if (reg < 0x80)
515 reg += 0x80;
516 else
517 reg -= 0x80;
Hans Verkuil8340ff42009-02-13 19:58:12 -0300518 ret = saa7191_write_reg(sd, SAA7191_REG_HUEC, reg);
Ladislav Michla637a112005-09-01 15:07:34 +0000519 break;
520 case SAA7191_CONTROL_VTRC:
Hans Verkuil8340ff42009-02-13 19:58:12 -0300521 reg = saa7191_read_reg(sd, SAA7191_REG_STDC);
Ladislav Michla637a112005-09-01 15:07:34 +0000522 if (ctrl->value)
523 reg |= SAA7191_STDC_VTRC;
524 else
525 reg &= ~SAA7191_STDC_VTRC;
Hans Verkuil8340ff42009-02-13 19:58:12 -0300526 ret = saa7191_write_reg(sd, SAA7191_REG_STDC, reg);
Ladislav Michla637a112005-09-01 15:07:34 +0000527 break;
528 case SAA7191_CONTROL_LUMA_DELAY: {
529 s32 value = ctrl->value;
530 if (value < 0)
531 value += 8;
Hans Verkuil8340ff42009-02-13 19:58:12 -0300532 reg = saa7191_read_reg(sd, SAA7191_REG_CTL3);
Ladislav Michla637a112005-09-01 15:07:34 +0000533 reg &= ~SAA7191_CTL3_YDEL_MASK;
534 reg |= (value << SAA7191_CTL3_YDEL_SHIFT)
535 & SAA7191_CTL3_YDEL_MASK;
Hans Verkuil8340ff42009-02-13 19:58:12 -0300536 ret = saa7191_write_reg(sd, SAA7191_REG_CTL3, reg);
Ladislav Michla637a112005-09-01 15:07:34 +0000537 break;
538 }
539 case SAA7191_CONTROL_VNR:
Hans Verkuil8340ff42009-02-13 19:58:12 -0300540 reg = saa7191_read_reg(sd, SAA7191_REG_CTL4);
Ladislav Michla637a112005-09-01 15:07:34 +0000541 reg &= ~SAA7191_CTL4_VNOI_MASK;
542 reg |= (ctrl->value << SAA7191_CTL4_VNOI_SHIFT)
543 & SAA7191_CTL4_VNOI_MASK;
Hans Verkuil8340ff42009-02-13 19:58:12 -0300544 ret = saa7191_write_reg(sd, SAA7191_REG_CTL4, reg);
Ladislav Michla637a112005-09-01 15:07:34 +0000545 break;
546 default:
547 ret = -EINVAL;
548 }
549
550 return ret;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700551}
552
553/* I2C-interface */
554
Hans Verkuil8340ff42009-02-13 19:58:12 -0300555static int saa7191_g_input_status(struct v4l2_subdev *sd, u32 *status)
Ralf Baechled203a7e2005-09-06 15:19:37 -0700556{
Hans Verkuil8340ff42009-02-13 19:58:12 -0300557 u8 status_reg;
558 int res = V4L2_IN_ST_NO_SIGNAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700559
Hans Verkuil8340ff42009-02-13 19:58:12 -0300560 if (saa7191_read_status(sd, &status_reg))
561 return -EIO;
562 if ((status_reg & SAA7191_STATUS_HLCK) == 0)
563 res = 0;
564 if (!(status_reg & SAA7191_STATUS_CODE))
565 res |= V4L2_IN_ST_NO_COLOR;
566 *status = res;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700567 return 0;
568}
569
Hans Verkuil8340ff42009-02-13 19:58:12 -0300570
571static int saa7191_g_chip_ident(struct v4l2_subdev *sd,
572 struct v4l2_dbg_chip_ident *chip)
573{
574 struct i2c_client *client = v4l2_get_subdevdata(sd);
575
576 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_SAA7191, 0);
577}
578
Hans Verkuil8340ff42009-02-13 19:58:12 -0300579/* ----------------------------------------------------------------------- */
580
581static const struct v4l2_subdev_core_ops saa7191_core_ops = {
582 .g_chip_ident = saa7191_g_chip_ident,
583 .g_ctrl = saa7191_g_ctrl,
584 .s_ctrl = saa7191_s_ctrl,
Hans Verkuil8340ff42009-02-13 19:58:12 -0300585 .s_std = saa7191_s_std,
586};
587
588static const struct v4l2_subdev_video_ops saa7191_video_ops = {
589 .s_routing = saa7191_s_routing,
590 .querystd = saa7191_querystd,
591 .g_input_status = saa7191_g_input_status,
592};
593
594static const struct v4l2_subdev_ops saa7191_ops = {
595 .core = &saa7191_core_ops,
596 .video = &saa7191_video_ops,
597};
598
Hans Verkuilbabb7dc2009-02-13 07:31:05 -0300599static int saa7191_probe(struct i2c_client *client,
600 const struct i2c_device_id *id)
601{
602 int err = 0;
603 struct saa7191 *decoder;
Hans Verkuil8340ff42009-02-13 19:58:12 -0300604 struct v4l2_subdev *sd;
Hans Verkuilbabb7dc2009-02-13 07:31:05 -0300605
606 v4l_info(client, "chip found @ 0x%x (%s)\n",
607 client->addr << 1, client->adapter->name);
608
609 decoder = kzalloc(sizeof(*decoder), GFP_KERNEL);
610 if (!decoder)
611 return -ENOMEM;
612
Hans Verkuil8340ff42009-02-13 19:58:12 -0300613 sd = &decoder->sd;
614 v4l2_i2c_subdev_init(sd, client, &saa7191_ops);
Hans Verkuilbabb7dc2009-02-13 07:31:05 -0300615
Hans Verkuil8340ff42009-02-13 19:58:12 -0300616 err = saa7191_write_block(sd, sizeof(initseq), initseq);
Hans Verkuilbabb7dc2009-02-13 07:31:05 -0300617 if (err) {
618 printk(KERN_ERR "SAA7191 initialization failed\n");
619 kfree(decoder);
620 return err;
621 }
622
623 printk(KERN_INFO "SAA7191 initialized\n");
624
625 decoder->input = SAA7191_INPUT_COMPOSITE;
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300626 decoder->norm = V4L2_STD_PAL;
Hans Verkuilbabb7dc2009-02-13 07:31:05 -0300627
Hans Verkuil8340ff42009-02-13 19:58:12 -0300628 err = saa7191_autodetect_norm(sd);
Hans Verkuilbabb7dc2009-02-13 07:31:05 -0300629 if (err && (err != -EBUSY))
630 printk(KERN_ERR "SAA7191: Signal auto-detection failed\n");
631
632 return 0;
633}
634
635static int saa7191_remove(struct i2c_client *client)
636{
Hans Verkuil8340ff42009-02-13 19:58:12 -0300637 struct v4l2_subdev *sd = i2c_get_clientdata(client);
Hans Verkuilbabb7dc2009-02-13 07:31:05 -0300638
Hans Verkuil8340ff42009-02-13 19:58:12 -0300639 v4l2_device_unregister_subdev(sd);
640 kfree(to_saa7191(sd));
Hans Verkuilbabb7dc2009-02-13 07:31:05 -0300641 return 0;
642}
643
Hans Verkuilbabb7dc2009-02-13 07:31:05 -0300644static const struct i2c_device_id saa7191_id[] = {
645 { "saa7191", 0 },
646 { }
Ralf Baechled203a7e2005-09-06 15:19:37 -0700647};
Hans Verkuilbabb7dc2009-02-13 07:31:05 -0300648MODULE_DEVICE_TABLE(i2c, saa7191_id);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700649
Hans Verkuilbabb7dc2009-02-13 07:31:05 -0300650static struct v4l2_i2c_driver_data v4l2_i2c_data = {
651 .name = "saa7191",
Hans Verkuilbabb7dc2009-02-13 07:31:05 -0300652 .probe = saa7191_probe,
653 .remove = saa7191_remove,
Hans Verkuilbabb7dc2009-02-13 07:31:05 -0300654 .id_table = saa7191_id,
655};