Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 1 | /* |
| 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 Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 12 | #include <linux/delay.h> |
| 13 | #include <linux/errno.h> |
| 14 | #include <linux/fs.h> |
Ladislav Michl | 495515b | 2005-09-23 10:52:27 +0000 | [diff] [blame] | 15 | #include <linux/init.h> |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 16 | #include <linux/kernel.h> |
| 17 | #include <linux/major.h> |
Ladislav Michl | 495515b | 2005-09-23 10:52:27 +0000 | [diff] [blame] | 18 | #include <linux/module.h> |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 19 | #include <linux/mm.h> |
Ladislav Michl | 495515b | 2005-09-23 10:52:27 +0000 | [diff] [blame] | 20 | #include <linux/slab.h> |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 21 | |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 22 | #include <linux/videodev2.h> |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 23 | #include <linux/i2c.h> |
Hans Verkuil | babb7dc | 2009-02-13 07:31:05 -0300 | [diff] [blame] | 24 | #include <media/v4l2-common.h> |
| 25 | #include <media/v4l2-i2c-drv-legacy.h> |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 26 | |
| 27 | #include "saa7191.h" |
| 28 | |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 29 | #define SAA7191_MODULE_VERSION "0.0.5" |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 30 | |
| 31 | MODULE_DESCRIPTION("Philips SAA7191 video decoder driver"); |
| 32 | MODULE_VERSION(SAA7191_MODULE_VERSION); |
| 33 | MODULE_AUTHOR("Mikael Nousiainen <tmnousia@cc.hut.fi>"); |
| 34 | MODULE_LICENSE("GPL"); |
| 35 | |
Hans Verkuil | babb7dc | 2009-02-13 07:31:05 -0300 | [diff] [blame] | 36 | static unsigned short normal_i2c[] = { 0x8a >> 1, 0x8e >> 1, I2C_CLIENT_END }; |
| 37 | |
| 38 | I2C_CLIENT_INSMOD; |
| 39 | |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 40 | // #define SAA7191_DEBUG |
| 41 | |
| 42 | #ifdef SAA7191_DEBUG |
| 43 | #define dprintk(x...) printk("SAA7191: " x); |
| 44 | #else |
| 45 | #define dprintk(x...) |
| 46 | #endif |
| 47 | |
| 48 | #define SAA7191_SYNC_COUNT 30 |
| 49 | #define SAA7191_SYNC_DELAY 100 /* milliseconds */ |
| 50 | |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 51 | struct saa7191 { |
| 52 | struct i2c_client *client; |
| 53 | |
| 54 | /* the register values are stored here as the actual |
| 55 | * I2C-registers are write-only */ |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 56 | u8 reg[25]; |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 57 | |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 58 | int input; |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 59 | v4l2_std_id norm; |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 62 | static const u8 initseq[] = { |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 63 | 0, /* Subaddress */ |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 64 | |
| 65 | 0x50, /* (0x50) SAA7191_REG_IDEL */ |
| 66 | |
| 67 | /* 50 Hz signal timing */ |
| 68 | 0x30, /* (0x30) SAA7191_REG_HSYB */ |
| 69 | 0x00, /* (0x00) SAA7191_REG_HSYS */ |
| 70 | 0xe8, /* (0xe8) SAA7191_REG_HCLB */ |
| 71 | 0xb6, /* (0xb6) SAA7191_REG_HCLS */ |
| 72 | 0xf4, /* (0xf4) SAA7191_REG_HPHI */ |
| 73 | |
| 74 | /* control */ |
| 75 | SAA7191_LUMA_APER_1, /* (0x01) SAA7191_REG_LUMA - CVBS mode */ |
| 76 | 0x00, /* (0x00) SAA7191_REG_HUEC */ |
| 77 | 0xf8, /* (0xf8) SAA7191_REG_CKTQ */ |
| 78 | 0xf8, /* (0xf8) SAA7191_REG_CKTS */ |
| 79 | 0x90, /* (0x90) SAA7191_REG_PLSE */ |
| 80 | 0x90, /* (0x90) SAA7191_REG_SESE */ |
| 81 | 0x00, /* (0x00) SAA7191_REG_GAIN */ |
| 82 | SAA7191_STDC_NFEN | SAA7191_STDC_HRMV, /* (0x0c) SAA7191_REG_STDC |
| 83 | * - not SECAM, |
| 84 | * slow time constant */ |
| 85 | SAA7191_IOCK_OEDC | SAA7191_IOCK_OEHS | SAA7191_IOCK_OEVS |
| 86 | | SAA7191_IOCK_OEDY, /* (0x78) SAA7191_REG_IOCK |
| 87 | * - chroma from CVBS, GPSW1 & 2 off */ |
| 88 | SAA7191_CTL3_AUFD | SAA7191_CTL3_SCEN | SAA7191_CTL3_OFTS |
| 89 | | SAA7191_CTL3_YDEL0, /* (0x99) SAA7191_REG_CTL3 |
| 90 | * - automatic field detection */ |
| 91 | 0x00, /* (0x00) SAA7191_REG_CTL4 */ |
| 92 | 0x2c, /* (0x2c) SAA7191_REG_CHCV - PAL nominal value */ |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 93 | 0x00, /* unused */ |
| 94 | 0x00, /* unused */ |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 95 | |
| 96 | /* 60 Hz signal timing */ |
| 97 | 0x34, /* (0x34) SAA7191_REG_HS6B */ |
| 98 | 0x0a, /* (0x0a) SAA7191_REG_HS6S */ |
| 99 | 0xf4, /* (0xf4) SAA7191_REG_HC6B */ |
| 100 | 0xce, /* (0xce) SAA7191_REG_HC6S */ |
| 101 | 0xf4, /* (0xf4) SAA7191_REG_HP6I */ |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | /* SAA7191 register handling */ |
| 105 | |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 106 | static u8 saa7191_read_reg(struct i2c_client *client, |
| 107 | u8 reg) |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 108 | { |
| 109 | return ((struct saa7191 *)i2c_get_clientdata(client))->reg[reg]; |
| 110 | } |
| 111 | |
| 112 | static int saa7191_read_status(struct i2c_client *client, |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 113 | u8 *value) |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 114 | { |
| 115 | int ret; |
| 116 | |
| 117 | ret = i2c_master_recv(client, value, 1); |
| 118 | if (ret < 0) { |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 119 | printk(KERN_ERR "SAA7191: saa7191_read_status(): read failed\n"); |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 120 | return ret; |
| 121 | } |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 127 | static int saa7191_write_reg(struct i2c_client *client, u8 reg, |
| 128 | u8 value) |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 129 | { |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 130 | ((struct saa7191 *)i2c_get_clientdata(client))->reg[reg] = value; |
| 131 | return i2c_smbus_write_byte_data(client, reg, value); |
| 132 | } |
| 133 | |
| 134 | /* the first byte of data must be the first subaddress number (register) */ |
| 135 | static int saa7191_write_block(struct i2c_client *client, |
Mauro Carvalho Chehab | f5ab272 | 2007-09-03 12:01:51 -0300 | [diff] [blame] | 136 | u8 length, const u8 *data) |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 137 | { |
| 138 | int i; |
| 139 | int ret; |
| 140 | |
| 141 | struct saa7191 *decoder = (struct saa7191 *)i2c_get_clientdata(client); |
| 142 | for (i = 0; i < (length - 1); i++) { |
| 143 | decoder->reg[data[0] + i] = data[i + 1]; |
| 144 | } |
| 145 | |
| 146 | ret = i2c_master_send(client, data, length); |
| 147 | if (ret < 0) { |
| 148 | printk(KERN_ERR "SAA7191: saa7191_write_block(): " |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 149 | "write failed\n"); |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | /* Helper functions */ |
| 157 | |
| 158 | static int saa7191_set_input(struct i2c_client *client, int input) |
| 159 | { |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 160 | struct saa7191 *decoder = i2c_get_clientdata(client); |
| 161 | u8 luma = saa7191_read_reg(client, SAA7191_REG_LUMA); |
| 162 | u8 iock = saa7191_read_reg(client, SAA7191_REG_IOCK); |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 163 | int err; |
| 164 | |
| 165 | switch (input) { |
| 166 | case SAA7191_INPUT_COMPOSITE: /* Set Composite input */ |
| 167 | iock &= ~(SAA7191_IOCK_CHRS | SAA7191_IOCK_GPSW1 |
| 168 | | SAA7191_IOCK_GPSW2); |
| 169 | /* Chrominance trap active */ |
| 170 | luma &= ~SAA7191_LUMA_BYPS; |
| 171 | break; |
| 172 | case SAA7191_INPUT_SVIDEO: /* Set S-Video input */ |
| 173 | iock |= SAA7191_IOCK_CHRS | SAA7191_IOCK_GPSW2; |
| 174 | /* Chrominance trap bypassed */ |
| 175 | luma |= SAA7191_LUMA_BYPS; |
| 176 | break; |
| 177 | default: |
| 178 | return -EINVAL; |
| 179 | } |
| 180 | |
| 181 | err = saa7191_write_reg(client, SAA7191_REG_LUMA, luma); |
| 182 | if (err) |
| 183 | return -EIO; |
| 184 | err = saa7191_write_reg(client, SAA7191_REG_IOCK, iock); |
| 185 | if (err) |
| 186 | return -EIO; |
| 187 | |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 188 | decoder->input = input; |
| 189 | |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 190 | return 0; |
| 191 | } |
| 192 | |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 193 | static int saa7191_set_norm(struct i2c_client *client, v4l2_std_id norm) |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 194 | { |
| 195 | struct saa7191 *decoder = i2c_get_clientdata(client); |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 196 | u8 stdc = saa7191_read_reg(client, SAA7191_REG_STDC); |
| 197 | u8 ctl3 = saa7191_read_reg(client, SAA7191_REG_CTL3); |
| 198 | u8 chcv = saa7191_read_reg(client, SAA7191_REG_CHCV); |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 199 | int err; |
| 200 | |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 201 | if (norm & V4L2_STD_PAL) { |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 202 | stdc &= ~SAA7191_STDC_SECS; |
| 203 | ctl3 &= ~(SAA7191_CTL3_AUFD | SAA7191_CTL3_FSEL); |
| 204 | chcv = SAA7191_CHCV_PAL; |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 205 | } else if (norm & V4L2_STD_NTSC) { |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 206 | stdc &= ~SAA7191_STDC_SECS; |
| 207 | ctl3 &= ~SAA7191_CTL3_AUFD; |
| 208 | ctl3 |= SAA7191_CTL3_FSEL; |
| 209 | chcv = SAA7191_CHCV_NTSC; |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 210 | } else if (norm & V4L2_STD_SECAM) { |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 211 | stdc |= SAA7191_STDC_SECS; |
| 212 | ctl3 &= ~(SAA7191_CTL3_AUFD | SAA7191_CTL3_FSEL); |
| 213 | chcv = SAA7191_CHCV_PAL; |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 214 | } else { |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 215 | return -EINVAL; |
| 216 | } |
| 217 | |
| 218 | err = saa7191_write_reg(client, SAA7191_REG_CTL3, ctl3); |
| 219 | if (err) |
| 220 | return -EIO; |
| 221 | err = saa7191_write_reg(client, SAA7191_REG_STDC, stdc); |
| 222 | if (err) |
| 223 | return -EIO; |
| 224 | err = saa7191_write_reg(client, SAA7191_REG_CHCV, chcv); |
| 225 | if (err) |
| 226 | return -EIO; |
| 227 | |
| 228 | decoder->norm = norm; |
| 229 | |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 230 | dprintk("ctl3: %02x stdc: %02x chcv: %02x\n", ctl3, |
| 231 | stdc, chcv); |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 232 | dprintk("norm: %llx\n", norm); |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 233 | |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 234 | return 0; |
| 235 | } |
| 236 | |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 237 | static int saa7191_wait_for_signal(struct i2c_client *client, u8 *status) |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 238 | { |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 239 | int i = 0; |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 240 | |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 241 | dprintk("Checking for signal...\n"); |
| 242 | |
| 243 | for (i = 0; i < SAA7191_SYNC_COUNT; i++) { |
| 244 | if (saa7191_read_status(client, status)) |
| 245 | return -EIO; |
| 246 | |
| 247 | if (((*status) & SAA7191_STATUS_HLCK) == 0) { |
| 248 | dprintk("Signal found\n"); |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | msleep(SAA7191_SYNC_DELAY); |
| 253 | } |
| 254 | |
| 255 | dprintk("No signal\n"); |
| 256 | |
| 257 | return -EBUSY; |
| 258 | } |
| 259 | |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 260 | static int saa7191_autodetect_norm_extended(struct i2c_client *client, |
| 261 | v4l2_std_id *norm) |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 262 | { |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 263 | struct saa7191 *decoder = i2c_get_clientdata(client); |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 264 | u8 stdc = saa7191_read_reg(client, SAA7191_REG_STDC); |
| 265 | u8 ctl3 = saa7191_read_reg(client, SAA7191_REG_CTL3); |
| 266 | u8 status; |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 267 | v4l2_std_id old_norm = decoder->norm; |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 268 | int err = 0; |
| 269 | |
| 270 | dprintk("SAA7191 extended signal auto-detection...\n"); |
| 271 | |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 272 | *norm = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM; |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 273 | stdc &= ~SAA7191_STDC_SECS; |
| 274 | ctl3 &= ~(SAA7191_CTL3_FSEL); |
| 275 | |
| 276 | err = saa7191_write_reg(client, SAA7191_REG_STDC, stdc); |
| 277 | if (err) { |
| 278 | err = -EIO; |
| 279 | goto out; |
| 280 | } |
| 281 | err = saa7191_write_reg(client, SAA7191_REG_CTL3, ctl3); |
| 282 | if (err) { |
| 283 | err = -EIO; |
| 284 | goto out; |
| 285 | } |
| 286 | |
| 287 | ctl3 |= SAA7191_CTL3_AUFD; |
| 288 | err = saa7191_write_reg(client, SAA7191_REG_CTL3, ctl3); |
| 289 | if (err) { |
| 290 | err = -EIO; |
| 291 | goto out; |
| 292 | } |
| 293 | |
| 294 | msleep(SAA7191_SYNC_DELAY); |
| 295 | |
| 296 | err = saa7191_wait_for_signal(client, &status); |
| 297 | if (err) |
| 298 | goto out; |
| 299 | |
| 300 | if (status & SAA7191_STATUS_FIDT) { |
| 301 | /* 60Hz signal -> NTSC */ |
| 302 | dprintk("60Hz signal: NTSC\n"); |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 303 | *norm = V4L2_STD_NTSC; |
| 304 | return 0; |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | /* 50Hz signal */ |
| 308 | dprintk("50Hz signal: Trying PAL...\n"); |
| 309 | |
| 310 | /* try PAL first */ |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 311 | err = saa7191_set_norm(client, V4L2_STD_PAL); |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 312 | if (err) |
| 313 | goto out; |
| 314 | |
| 315 | msleep(SAA7191_SYNC_DELAY); |
| 316 | |
| 317 | err = saa7191_wait_for_signal(client, &status); |
| 318 | if (err) |
| 319 | goto out; |
| 320 | |
| 321 | /* not 50Hz ? */ |
| 322 | if (status & SAA7191_STATUS_FIDT) { |
| 323 | dprintk("No 50Hz signal\n"); |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 324 | saa7191_set_norm(client, old_norm); |
| 325 | return -EAGAIN; |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | if (status & SAA7191_STATUS_CODE) { |
| 329 | dprintk("PAL\n"); |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 330 | *norm = V4L2_STD_PAL; |
| 331 | return saa7191_set_norm(client, old_norm); |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | dprintk("No color detected with PAL - Trying SECAM...\n"); |
| 335 | |
| 336 | /* no color detected ? -> try SECAM */ |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 337 | err = saa7191_set_norm(client, V4L2_STD_SECAM); |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 338 | if (err) |
| 339 | goto out; |
| 340 | |
| 341 | msleep(SAA7191_SYNC_DELAY); |
| 342 | |
| 343 | err = saa7191_wait_for_signal(client, &status); |
| 344 | if (err) |
| 345 | goto out; |
| 346 | |
| 347 | /* not 50Hz ? */ |
| 348 | if (status & SAA7191_STATUS_FIDT) { |
| 349 | dprintk("No 50Hz signal\n"); |
| 350 | err = -EAGAIN; |
| 351 | goto out; |
| 352 | } |
| 353 | |
| 354 | if (status & SAA7191_STATUS_CODE) { |
| 355 | /* Color detected -> SECAM */ |
| 356 | dprintk("SECAM\n"); |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 357 | *norm = V4L2_STD_SECAM; |
| 358 | return saa7191_set_norm(client, old_norm); |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | dprintk("No color detected with SECAM - Going back to PAL.\n"); |
| 362 | |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 363 | out: |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 364 | return saa7191_set_norm(client, old_norm); |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | static int saa7191_autodetect_norm(struct i2c_client *client) |
| 368 | { |
| 369 | u8 status; |
| 370 | |
| 371 | dprintk("SAA7191 signal auto-detection...\n"); |
| 372 | |
| 373 | dprintk("Reading status...\n"); |
| 374 | |
| 375 | if (saa7191_read_status(client, &status)) |
| 376 | return -EIO; |
| 377 | |
| 378 | dprintk("Checking for signal...\n"); |
| 379 | |
| 380 | /* no signal ? */ |
| 381 | if (status & SAA7191_STATUS_HLCK) { |
| 382 | dprintk("No signal\n"); |
| 383 | return -EBUSY; |
| 384 | } |
| 385 | |
| 386 | dprintk("Signal found\n"); |
| 387 | |
| 388 | if (status & SAA7191_STATUS_FIDT) { |
| 389 | /* 60hz signal -> NTSC */ |
| 390 | dprintk("NTSC\n"); |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 391 | return saa7191_set_norm(client, V4L2_STD_NTSC); |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 392 | } else { |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 393 | /* 50hz signal -> PAL */ |
| 394 | dprintk("PAL\n"); |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 395 | return saa7191_set_norm(client, V4L2_STD_PAL); |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 396 | } |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 399 | static int saa7191_get_control(struct i2c_client *client, |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 400 | struct v4l2_control *ctrl) |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 401 | { |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 402 | u8 reg; |
| 403 | int ret = 0; |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 404 | |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 405 | switch (ctrl->id) { |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 406 | case SAA7191_CONTROL_BANDPASS: |
| 407 | case SAA7191_CONTROL_BANDPASS_WEIGHT: |
| 408 | case SAA7191_CONTROL_CORING: |
| 409 | reg = saa7191_read_reg(client, SAA7191_REG_LUMA); |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 410 | switch (ctrl->id) { |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 411 | case SAA7191_CONTROL_BANDPASS: |
| 412 | ctrl->value = ((s32)reg & SAA7191_LUMA_BPSS_MASK) |
| 413 | >> SAA7191_LUMA_BPSS_SHIFT; |
| 414 | break; |
| 415 | case SAA7191_CONTROL_BANDPASS_WEIGHT: |
| 416 | ctrl->value = ((s32)reg & SAA7191_LUMA_APER_MASK) |
| 417 | >> SAA7191_LUMA_APER_SHIFT; |
| 418 | break; |
| 419 | case SAA7191_CONTROL_CORING: |
| 420 | ctrl->value = ((s32)reg & SAA7191_LUMA_CORI_MASK) |
| 421 | >> SAA7191_LUMA_CORI_SHIFT; |
| 422 | break; |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 423 | } |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 424 | break; |
| 425 | case SAA7191_CONTROL_FORCE_COLOUR: |
| 426 | case SAA7191_CONTROL_CHROMA_GAIN: |
| 427 | reg = saa7191_read_reg(client, SAA7191_REG_GAIN); |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 428 | if (ctrl->id == SAA7191_CONTROL_FORCE_COLOUR) |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 429 | ctrl->value = ((s32)reg & SAA7191_GAIN_COLO) ? 1 : 0; |
| 430 | else |
| 431 | ctrl->value = ((s32)reg & SAA7191_GAIN_LFIS_MASK) |
| 432 | >> SAA7191_GAIN_LFIS_SHIFT; |
| 433 | break; |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 434 | case V4L2_CID_HUE: |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 435 | reg = saa7191_read_reg(client, SAA7191_REG_HUEC); |
| 436 | if (reg < 0x80) |
| 437 | reg += 0x80; |
| 438 | else |
| 439 | reg -= 0x80; |
| 440 | ctrl->value = (s32)reg; |
| 441 | break; |
| 442 | case SAA7191_CONTROL_VTRC: |
| 443 | reg = saa7191_read_reg(client, SAA7191_REG_STDC); |
| 444 | ctrl->value = ((s32)reg & SAA7191_STDC_VTRC) ? 1 : 0; |
| 445 | break; |
| 446 | case SAA7191_CONTROL_LUMA_DELAY: |
| 447 | reg = saa7191_read_reg(client, SAA7191_REG_CTL3); |
| 448 | ctrl->value = ((s32)reg & SAA7191_CTL3_YDEL_MASK) |
| 449 | >> SAA7191_CTL3_YDEL_SHIFT; |
| 450 | if (ctrl->value >= 4) |
| 451 | ctrl->value -= 8; |
| 452 | break; |
| 453 | case SAA7191_CONTROL_VNR: |
| 454 | reg = saa7191_read_reg(client, SAA7191_REG_CTL4); |
| 455 | ctrl->value = ((s32)reg & SAA7191_CTL4_VNOI_MASK) |
| 456 | >> SAA7191_CTL4_VNOI_SHIFT; |
| 457 | break; |
| 458 | default: |
| 459 | ret = -EINVAL; |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 462 | return ret; |
| 463 | } |
| 464 | |
| 465 | static int saa7191_set_control(struct i2c_client *client, |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 466 | struct v4l2_control *ctrl) |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 467 | { |
| 468 | u8 reg; |
| 469 | int ret = 0; |
| 470 | |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 471 | switch (ctrl->id) { |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 472 | case SAA7191_CONTROL_BANDPASS: |
| 473 | case SAA7191_CONTROL_BANDPASS_WEIGHT: |
| 474 | case SAA7191_CONTROL_CORING: |
| 475 | reg = saa7191_read_reg(client, SAA7191_REG_LUMA); |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 476 | switch (ctrl->id) { |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 477 | case SAA7191_CONTROL_BANDPASS: |
| 478 | reg &= ~SAA7191_LUMA_BPSS_MASK; |
| 479 | reg |= (ctrl->value << SAA7191_LUMA_BPSS_SHIFT) |
| 480 | & SAA7191_LUMA_BPSS_MASK; |
| 481 | break; |
| 482 | case SAA7191_CONTROL_BANDPASS_WEIGHT: |
| 483 | reg &= ~SAA7191_LUMA_APER_MASK; |
| 484 | reg |= (ctrl->value << SAA7191_LUMA_APER_SHIFT) |
| 485 | & SAA7191_LUMA_APER_MASK; |
| 486 | break; |
| 487 | case SAA7191_CONTROL_CORING: |
| 488 | reg &= ~SAA7191_LUMA_CORI_MASK; |
| 489 | reg |= (ctrl->value << SAA7191_LUMA_CORI_SHIFT) |
| 490 | & SAA7191_LUMA_CORI_MASK; |
| 491 | break; |
| 492 | } |
| 493 | ret = saa7191_write_reg(client, SAA7191_REG_LUMA, reg); |
| 494 | break; |
| 495 | case SAA7191_CONTROL_FORCE_COLOUR: |
| 496 | case SAA7191_CONTROL_CHROMA_GAIN: |
| 497 | reg = saa7191_read_reg(client, SAA7191_REG_GAIN); |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 498 | if (ctrl->id == SAA7191_CONTROL_FORCE_COLOUR) { |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 499 | if (ctrl->value) |
| 500 | reg |= SAA7191_GAIN_COLO; |
| 501 | else |
| 502 | reg &= ~SAA7191_GAIN_COLO; |
| 503 | } else { |
| 504 | reg &= ~SAA7191_GAIN_LFIS_MASK; |
| 505 | reg |= (ctrl->value << SAA7191_GAIN_LFIS_SHIFT) |
| 506 | & SAA7191_GAIN_LFIS_MASK; |
| 507 | } |
| 508 | ret = saa7191_write_reg(client, SAA7191_REG_GAIN, reg); |
| 509 | break; |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 510 | case V4L2_CID_HUE: |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 511 | reg = ctrl->value & 0xff; |
| 512 | if (reg < 0x80) |
| 513 | reg += 0x80; |
| 514 | else |
| 515 | reg -= 0x80; |
| 516 | ret = saa7191_write_reg(client, SAA7191_REG_HUEC, reg); |
| 517 | break; |
| 518 | case SAA7191_CONTROL_VTRC: |
| 519 | reg = saa7191_read_reg(client, SAA7191_REG_STDC); |
| 520 | if (ctrl->value) |
| 521 | reg |= SAA7191_STDC_VTRC; |
| 522 | else |
| 523 | reg &= ~SAA7191_STDC_VTRC; |
| 524 | ret = saa7191_write_reg(client, SAA7191_REG_STDC, reg); |
| 525 | break; |
| 526 | case SAA7191_CONTROL_LUMA_DELAY: { |
| 527 | s32 value = ctrl->value; |
| 528 | if (value < 0) |
| 529 | value += 8; |
| 530 | reg = saa7191_read_reg(client, SAA7191_REG_CTL3); |
| 531 | reg &= ~SAA7191_CTL3_YDEL_MASK; |
| 532 | reg |= (value << SAA7191_CTL3_YDEL_SHIFT) |
| 533 | & SAA7191_CTL3_YDEL_MASK; |
| 534 | ret = saa7191_write_reg(client, SAA7191_REG_CTL3, reg); |
| 535 | break; |
| 536 | } |
| 537 | case SAA7191_CONTROL_VNR: |
| 538 | reg = saa7191_read_reg(client, SAA7191_REG_CTL4); |
| 539 | reg &= ~SAA7191_CTL4_VNOI_MASK; |
| 540 | reg |= (ctrl->value << SAA7191_CTL4_VNOI_SHIFT) |
| 541 | & SAA7191_CTL4_VNOI_MASK; |
| 542 | ret = saa7191_write_reg(client, SAA7191_REG_CTL4, reg); |
| 543 | break; |
| 544 | default: |
| 545 | ret = -EINVAL; |
| 546 | } |
| 547 | |
| 548 | return ret; |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | /* I2C-interface */ |
| 552 | |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 553 | static int saa7191_command(struct i2c_client *client, unsigned int cmd, |
| 554 | void *arg) |
| 555 | { |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 556 | switch (cmd) { |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 557 | case VIDIOC_INT_G_INPUT_STATUS: { |
| 558 | u32 *iarg = arg; |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 559 | u8 status; |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 560 | int res = V4L2_IN_ST_NO_SIGNAL; |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 561 | |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 562 | if (saa7191_read_status(client, &status)) |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 563 | return -EIO; |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 564 | if ((status & SAA7191_STATUS_HLCK) == 0) |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 565 | res = 0; |
| 566 | if (!(status & SAA7191_STATUS_CODE)) |
| 567 | res |= V4L2_IN_ST_NO_COLOR; |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 568 | *iarg = res; |
| 569 | break; |
| 570 | } |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 571 | |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 572 | case VIDIOC_QUERYSTD: |
| 573 | return saa7191_autodetect_norm_extended(client, arg); |
| 574 | |
| 575 | case VIDIOC_S_STD: { |
| 576 | v4l2_std_id *istd = arg; |
| 577 | |
| 578 | return saa7191_set_norm(client, *istd); |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 579 | } |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 580 | case VIDIOC_INT_S_VIDEO_ROUTING: { |
| 581 | struct v4l2_routing *route = arg; |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 582 | |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 583 | return saa7191_set_input(client, route->input); |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 584 | } |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 585 | |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 586 | case VIDIOC_G_CTRL: |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 587 | return saa7191_get_control(client, arg); |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 588 | |
| 589 | case VIDIOC_S_CTRL: |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 590 | return saa7191_set_control(client, arg); |
Hans Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 591 | |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 592 | default: |
| 593 | return -EINVAL; |
| 594 | } |
| 595 | |
| 596 | return 0; |
| 597 | } |
| 598 | |
Hans Verkuil | babb7dc | 2009-02-13 07:31:05 -0300 | [diff] [blame] | 599 | static int saa7191_probe(struct i2c_client *client, |
| 600 | const struct i2c_device_id *id) |
| 601 | { |
| 602 | int err = 0; |
| 603 | struct saa7191 *decoder; |
| 604 | |
| 605 | v4l_info(client, "chip found @ 0x%x (%s)\n", |
| 606 | client->addr << 1, client->adapter->name); |
| 607 | |
| 608 | decoder = kzalloc(sizeof(*decoder), GFP_KERNEL); |
| 609 | if (!decoder) |
| 610 | return -ENOMEM; |
| 611 | |
| 612 | i2c_set_clientdata(client, decoder); |
| 613 | |
| 614 | decoder->client = client; |
| 615 | |
| 616 | err = saa7191_write_block(client, sizeof(initseq), initseq); |
| 617 | 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 Verkuil | cf4e948 | 2009-02-27 09:05:10 -0300 | [diff] [blame^] | 626 | decoder->norm = V4L2_STD_PAL; |
Hans Verkuil | babb7dc | 2009-02-13 07:31:05 -0300 | [diff] [blame] | 627 | |
| 628 | err = saa7191_autodetect_norm(client); |
| 629 | if (err && (err != -EBUSY)) |
| 630 | printk(KERN_ERR "SAA7191: Signal auto-detection failed\n"); |
| 631 | |
| 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | static int saa7191_remove(struct i2c_client *client) |
| 636 | { |
| 637 | struct saa7191 *decoder = i2c_get_clientdata(client); |
| 638 | |
| 639 | kfree(decoder); |
| 640 | return 0; |
| 641 | } |
| 642 | |
| 643 | static int saa7191_legacy_probe(struct i2c_adapter *adapter) |
| 644 | { |
| 645 | return adapter->id == I2C_HW_SGI_VINO; |
| 646 | } |
| 647 | |
| 648 | static const struct i2c_device_id saa7191_id[] = { |
| 649 | { "saa7191", 0 }, |
| 650 | { } |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 651 | }; |
Hans Verkuil | babb7dc | 2009-02-13 07:31:05 -0300 | [diff] [blame] | 652 | MODULE_DEVICE_TABLE(i2c, saa7191_id); |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 653 | |
Hans Verkuil | babb7dc | 2009-02-13 07:31:05 -0300 | [diff] [blame] | 654 | static struct v4l2_i2c_driver_data v4l2_i2c_data = { |
| 655 | .name = "saa7191", |
| 656 | .driverid = I2C_DRIVERID_SAA7191, |
| 657 | .command = saa7191_command, |
| 658 | .probe = saa7191_probe, |
| 659 | .remove = saa7191_remove, |
| 660 | .legacy_probe = saa7191_legacy_probe, |
| 661 | .id_table = saa7191_id, |
| 662 | }; |