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 | |
| 22 | #include <linux/videodev.h> |
| 23 | #include <linux/video_decoder.h> |
| 24 | #include <linux/i2c.h> |
Hans Verkuil | babb7dc | 2009-02-13 07:31:05 -0300 | [diff] [blame^] | 25 | #include <media/v4l2-common.h> |
| 26 | #include <media/v4l2-i2c-drv-legacy.h> |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 27 | |
| 28 | #include "saa7191.h" |
| 29 | |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 30 | #define SAA7191_MODULE_VERSION "0.0.5" |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 31 | |
| 32 | MODULE_DESCRIPTION("Philips SAA7191 video decoder driver"); |
| 33 | MODULE_VERSION(SAA7191_MODULE_VERSION); |
| 34 | MODULE_AUTHOR("Mikael Nousiainen <tmnousia@cc.hut.fi>"); |
| 35 | MODULE_LICENSE("GPL"); |
| 36 | |
Hans Verkuil | babb7dc | 2009-02-13 07:31:05 -0300 | [diff] [blame^] | 37 | static unsigned short normal_i2c[] = { 0x8a >> 1, 0x8e >> 1, I2C_CLIENT_END }; |
| 38 | |
| 39 | I2C_CLIENT_INSMOD; |
| 40 | |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 41 | // #define SAA7191_DEBUG |
| 42 | |
| 43 | #ifdef SAA7191_DEBUG |
| 44 | #define dprintk(x...) printk("SAA7191: " x); |
| 45 | #else |
| 46 | #define dprintk(x...) |
| 47 | #endif |
| 48 | |
| 49 | #define SAA7191_SYNC_COUNT 30 |
| 50 | #define SAA7191_SYNC_DELAY 100 /* milliseconds */ |
| 51 | |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 52 | struct saa7191 { |
| 53 | struct i2c_client *client; |
| 54 | |
| 55 | /* the register values are stored here as the actual |
| 56 | * I2C-registers are write-only */ |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 57 | u8 reg[25]; |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 58 | |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 59 | int input; |
| 60 | int norm; |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 63 | static const u8 initseq[] = { |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 64 | 0, /* Subaddress */ |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 65 | |
| 66 | 0x50, /* (0x50) SAA7191_REG_IDEL */ |
| 67 | |
| 68 | /* 50 Hz signal timing */ |
| 69 | 0x30, /* (0x30) SAA7191_REG_HSYB */ |
| 70 | 0x00, /* (0x00) SAA7191_REG_HSYS */ |
| 71 | 0xe8, /* (0xe8) SAA7191_REG_HCLB */ |
| 72 | 0xb6, /* (0xb6) SAA7191_REG_HCLS */ |
| 73 | 0xf4, /* (0xf4) SAA7191_REG_HPHI */ |
| 74 | |
| 75 | /* control */ |
| 76 | SAA7191_LUMA_APER_1, /* (0x01) SAA7191_REG_LUMA - CVBS mode */ |
| 77 | 0x00, /* (0x00) SAA7191_REG_HUEC */ |
| 78 | 0xf8, /* (0xf8) SAA7191_REG_CKTQ */ |
| 79 | 0xf8, /* (0xf8) SAA7191_REG_CKTS */ |
| 80 | 0x90, /* (0x90) SAA7191_REG_PLSE */ |
| 81 | 0x90, /* (0x90) SAA7191_REG_SESE */ |
| 82 | 0x00, /* (0x00) SAA7191_REG_GAIN */ |
| 83 | SAA7191_STDC_NFEN | SAA7191_STDC_HRMV, /* (0x0c) SAA7191_REG_STDC |
| 84 | * - not SECAM, |
| 85 | * slow time constant */ |
| 86 | SAA7191_IOCK_OEDC | SAA7191_IOCK_OEHS | SAA7191_IOCK_OEVS |
| 87 | | SAA7191_IOCK_OEDY, /* (0x78) SAA7191_REG_IOCK |
| 88 | * - chroma from CVBS, GPSW1 & 2 off */ |
| 89 | SAA7191_CTL3_AUFD | SAA7191_CTL3_SCEN | SAA7191_CTL3_OFTS |
| 90 | | SAA7191_CTL3_YDEL0, /* (0x99) SAA7191_REG_CTL3 |
| 91 | * - automatic field detection */ |
| 92 | 0x00, /* (0x00) SAA7191_REG_CTL4 */ |
| 93 | 0x2c, /* (0x2c) SAA7191_REG_CHCV - PAL nominal value */ |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 94 | 0x00, /* unused */ |
| 95 | 0x00, /* unused */ |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 96 | |
| 97 | /* 60 Hz signal timing */ |
| 98 | 0x34, /* (0x34) SAA7191_REG_HS6B */ |
| 99 | 0x0a, /* (0x0a) SAA7191_REG_HS6S */ |
| 100 | 0xf4, /* (0xf4) SAA7191_REG_HC6B */ |
| 101 | 0xce, /* (0xce) SAA7191_REG_HC6S */ |
| 102 | 0xf4, /* (0xf4) SAA7191_REG_HP6I */ |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | /* SAA7191 register handling */ |
| 106 | |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 107 | static u8 saa7191_read_reg(struct i2c_client *client, |
| 108 | u8 reg) |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 109 | { |
| 110 | return ((struct saa7191 *)i2c_get_clientdata(client))->reg[reg]; |
| 111 | } |
| 112 | |
| 113 | static int saa7191_read_status(struct i2c_client *client, |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 114 | u8 *value) |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 115 | { |
| 116 | int ret; |
| 117 | |
| 118 | ret = i2c_master_recv(client, value, 1); |
| 119 | if (ret < 0) { |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 120 | printk(KERN_ERR "SAA7191: saa7191_read_status(): read failed\n"); |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 128 | static int saa7191_write_reg(struct i2c_client *client, u8 reg, |
| 129 | u8 value) |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 130 | { |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 131 | ((struct saa7191 *)i2c_get_clientdata(client))->reg[reg] = value; |
| 132 | return i2c_smbus_write_byte_data(client, reg, value); |
| 133 | } |
| 134 | |
| 135 | /* the first byte of data must be the first subaddress number (register) */ |
| 136 | static int saa7191_write_block(struct i2c_client *client, |
Mauro Carvalho Chehab | f5ab272 | 2007-09-03 12:01:51 -0300 | [diff] [blame] | 137 | u8 length, const u8 *data) |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 138 | { |
| 139 | int i; |
| 140 | int ret; |
| 141 | |
| 142 | struct saa7191 *decoder = (struct saa7191 *)i2c_get_clientdata(client); |
| 143 | for (i = 0; i < (length - 1); i++) { |
| 144 | decoder->reg[data[0] + i] = data[i + 1]; |
| 145 | } |
| 146 | |
| 147 | ret = i2c_master_send(client, data, length); |
| 148 | if (ret < 0) { |
| 149 | printk(KERN_ERR "SAA7191: saa7191_write_block(): " |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 150 | "write failed\n"); |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 151 | return ret; |
| 152 | } |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | /* Helper functions */ |
| 158 | |
| 159 | static int saa7191_set_input(struct i2c_client *client, int input) |
| 160 | { |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 161 | struct saa7191 *decoder = i2c_get_clientdata(client); |
| 162 | u8 luma = saa7191_read_reg(client, SAA7191_REG_LUMA); |
| 163 | u8 iock = saa7191_read_reg(client, SAA7191_REG_IOCK); |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 164 | int err; |
| 165 | |
| 166 | switch (input) { |
| 167 | case SAA7191_INPUT_COMPOSITE: /* Set Composite input */ |
| 168 | iock &= ~(SAA7191_IOCK_CHRS | SAA7191_IOCK_GPSW1 |
| 169 | | SAA7191_IOCK_GPSW2); |
| 170 | /* Chrominance trap active */ |
| 171 | luma &= ~SAA7191_LUMA_BYPS; |
| 172 | break; |
| 173 | case SAA7191_INPUT_SVIDEO: /* Set S-Video input */ |
| 174 | iock |= SAA7191_IOCK_CHRS | SAA7191_IOCK_GPSW2; |
| 175 | /* Chrominance trap bypassed */ |
| 176 | luma |= SAA7191_LUMA_BYPS; |
| 177 | break; |
| 178 | default: |
| 179 | return -EINVAL; |
| 180 | } |
| 181 | |
| 182 | err = saa7191_write_reg(client, SAA7191_REG_LUMA, luma); |
| 183 | if (err) |
| 184 | return -EIO; |
| 185 | err = saa7191_write_reg(client, SAA7191_REG_IOCK, iock); |
| 186 | if (err) |
| 187 | return -EIO; |
| 188 | |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 189 | decoder->input = input; |
| 190 | |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | static int saa7191_set_norm(struct i2c_client *client, int norm) |
| 195 | { |
| 196 | struct saa7191 *decoder = i2c_get_clientdata(client); |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 197 | u8 stdc = saa7191_read_reg(client, SAA7191_REG_STDC); |
| 198 | u8 ctl3 = saa7191_read_reg(client, SAA7191_REG_CTL3); |
| 199 | u8 chcv = saa7191_read_reg(client, SAA7191_REG_CHCV); |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 200 | int err; |
| 201 | |
| 202 | switch(norm) { |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 203 | case SAA7191_NORM_PAL: |
| 204 | stdc &= ~SAA7191_STDC_SECS; |
| 205 | ctl3 &= ~(SAA7191_CTL3_AUFD | SAA7191_CTL3_FSEL); |
| 206 | chcv = SAA7191_CHCV_PAL; |
| 207 | break; |
| 208 | case SAA7191_NORM_NTSC: |
| 209 | stdc &= ~SAA7191_STDC_SECS; |
| 210 | ctl3 &= ~SAA7191_CTL3_AUFD; |
| 211 | ctl3 |= SAA7191_CTL3_FSEL; |
| 212 | chcv = SAA7191_CHCV_NTSC; |
| 213 | break; |
| 214 | case SAA7191_NORM_SECAM: |
| 215 | stdc |= SAA7191_STDC_SECS; |
| 216 | ctl3 &= ~(SAA7191_CTL3_AUFD | SAA7191_CTL3_FSEL); |
| 217 | chcv = SAA7191_CHCV_PAL; |
| 218 | break; |
| 219 | default: |
| 220 | return -EINVAL; |
| 221 | } |
| 222 | |
| 223 | err = saa7191_write_reg(client, SAA7191_REG_CTL3, ctl3); |
| 224 | if (err) |
| 225 | return -EIO; |
| 226 | err = saa7191_write_reg(client, SAA7191_REG_STDC, stdc); |
| 227 | if (err) |
| 228 | return -EIO; |
| 229 | err = saa7191_write_reg(client, SAA7191_REG_CHCV, chcv); |
| 230 | if (err) |
| 231 | return -EIO; |
| 232 | |
| 233 | decoder->norm = norm; |
| 234 | |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 235 | dprintk("ctl3: %02x stdc: %02x chcv: %02x\n", ctl3, |
| 236 | stdc, chcv); |
| 237 | dprintk("norm: %d\n", norm); |
| 238 | |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 239 | return 0; |
| 240 | } |
| 241 | |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 242 | static int saa7191_wait_for_signal(struct i2c_client *client, u8 *status) |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 243 | { |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 244 | int i = 0; |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 245 | |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 246 | dprintk("Checking for signal...\n"); |
| 247 | |
| 248 | for (i = 0; i < SAA7191_SYNC_COUNT; i++) { |
| 249 | if (saa7191_read_status(client, status)) |
| 250 | 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 | |
| 265 | static int saa7191_autodetect_norm_extended(struct i2c_client *client) |
| 266 | { |
| 267 | u8 stdc = saa7191_read_reg(client, SAA7191_REG_STDC); |
| 268 | u8 ctl3 = saa7191_read_reg(client, SAA7191_REG_CTL3); |
| 269 | u8 status; |
| 270 | int err = 0; |
| 271 | |
| 272 | dprintk("SAA7191 extended signal auto-detection...\n"); |
| 273 | |
| 274 | stdc &= ~SAA7191_STDC_SECS; |
| 275 | ctl3 &= ~(SAA7191_CTL3_FSEL); |
| 276 | |
| 277 | err = saa7191_write_reg(client, SAA7191_REG_STDC, stdc); |
| 278 | if (err) { |
| 279 | err = -EIO; |
| 280 | goto out; |
| 281 | } |
| 282 | err = saa7191_write_reg(client, SAA7191_REG_CTL3, ctl3); |
| 283 | if (err) { |
| 284 | err = -EIO; |
| 285 | goto out; |
| 286 | } |
| 287 | |
| 288 | ctl3 |= SAA7191_CTL3_AUFD; |
| 289 | err = saa7191_write_reg(client, SAA7191_REG_CTL3, ctl3); |
| 290 | if (err) { |
| 291 | err = -EIO; |
| 292 | goto out; |
| 293 | } |
| 294 | |
| 295 | msleep(SAA7191_SYNC_DELAY); |
| 296 | |
| 297 | err = saa7191_wait_for_signal(client, &status); |
| 298 | if (err) |
| 299 | goto out; |
| 300 | |
| 301 | if (status & SAA7191_STATUS_FIDT) { |
| 302 | /* 60Hz signal -> NTSC */ |
| 303 | dprintk("60Hz signal: NTSC\n"); |
| 304 | return saa7191_set_norm(client, SAA7191_NORM_NTSC); |
| 305 | } |
| 306 | |
| 307 | /* 50Hz signal */ |
| 308 | dprintk("50Hz signal: Trying PAL...\n"); |
| 309 | |
| 310 | /* try PAL first */ |
| 311 | err = saa7191_set_norm(client, SAA7191_NORM_PAL); |
| 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"); |
| 324 | err = -EAGAIN; |
| 325 | goto out; |
| 326 | } |
| 327 | |
| 328 | if (status & SAA7191_STATUS_CODE) { |
| 329 | dprintk("PAL\n"); |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | dprintk("No color detected with PAL - Trying SECAM...\n"); |
| 334 | |
| 335 | /* no color detected ? -> try SECAM */ |
| 336 | err = saa7191_set_norm(client, |
| 337 | SAA7191_NORM_SECAM); |
| 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"); |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | dprintk("No color detected with SECAM - Going back to PAL.\n"); |
| 361 | |
| 362 | /* still no color detected ? |
| 363 | * -> set norm back to PAL */ |
| 364 | err = saa7191_set_norm(client, |
| 365 | SAA7191_NORM_PAL); |
| 366 | if (err) |
| 367 | goto out; |
| 368 | |
| 369 | out: |
| 370 | ctl3 = saa7191_read_reg(client, SAA7191_REG_CTL3); |
| 371 | if (ctl3 & SAA7191_CTL3_AUFD) { |
| 372 | ctl3 &= ~(SAA7191_CTL3_AUFD); |
| 373 | err = saa7191_write_reg(client, SAA7191_REG_CTL3, ctl3); |
| 374 | if (err) { |
| 375 | err = -EIO; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | return err; |
| 380 | } |
| 381 | |
| 382 | static int saa7191_autodetect_norm(struct i2c_client *client) |
| 383 | { |
| 384 | u8 status; |
| 385 | |
| 386 | dprintk("SAA7191 signal auto-detection...\n"); |
| 387 | |
| 388 | dprintk("Reading status...\n"); |
| 389 | |
| 390 | if (saa7191_read_status(client, &status)) |
| 391 | return -EIO; |
| 392 | |
| 393 | dprintk("Checking for signal...\n"); |
| 394 | |
| 395 | /* no signal ? */ |
| 396 | if (status & SAA7191_STATUS_HLCK) { |
| 397 | dprintk("No signal\n"); |
| 398 | return -EBUSY; |
| 399 | } |
| 400 | |
| 401 | dprintk("Signal found\n"); |
| 402 | |
| 403 | if (status & SAA7191_STATUS_FIDT) { |
| 404 | /* 60hz signal -> NTSC */ |
| 405 | dprintk("NTSC\n"); |
| 406 | return saa7191_set_norm(client, SAA7191_NORM_NTSC); |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 407 | } else { |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 408 | /* 50hz signal -> PAL */ |
| 409 | dprintk("PAL\n"); |
| 410 | return saa7191_set_norm(client, SAA7191_NORM_PAL); |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 411 | } |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 414 | static int saa7191_get_control(struct i2c_client *client, |
| 415 | struct saa7191_control *ctrl) |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 416 | { |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 417 | u8 reg; |
| 418 | int ret = 0; |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 419 | |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 420 | switch (ctrl->type) { |
| 421 | case SAA7191_CONTROL_BANDPASS: |
| 422 | case SAA7191_CONTROL_BANDPASS_WEIGHT: |
| 423 | case SAA7191_CONTROL_CORING: |
| 424 | reg = saa7191_read_reg(client, SAA7191_REG_LUMA); |
| 425 | switch (ctrl->type) { |
| 426 | case SAA7191_CONTROL_BANDPASS: |
| 427 | ctrl->value = ((s32)reg & SAA7191_LUMA_BPSS_MASK) |
| 428 | >> SAA7191_LUMA_BPSS_SHIFT; |
| 429 | break; |
| 430 | case SAA7191_CONTROL_BANDPASS_WEIGHT: |
| 431 | ctrl->value = ((s32)reg & SAA7191_LUMA_APER_MASK) |
| 432 | >> SAA7191_LUMA_APER_SHIFT; |
| 433 | break; |
| 434 | case SAA7191_CONTROL_CORING: |
| 435 | ctrl->value = ((s32)reg & SAA7191_LUMA_CORI_MASK) |
| 436 | >> SAA7191_LUMA_CORI_SHIFT; |
| 437 | break; |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 438 | } |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 439 | break; |
| 440 | case SAA7191_CONTROL_FORCE_COLOUR: |
| 441 | case SAA7191_CONTROL_CHROMA_GAIN: |
| 442 | reg = saa7191_read_reg(client, SAA7191_REG_GAIN); |
| 443 | if (ctrl->type == SAA7191_CONTROL_FORCE_COLOUR) |
| 444 | ctrl->value = ((s32)reg & SAA7191_GAIN_COLO) ? 1 : 0; |
| 445 | else |
| 446 | ctrl->value = ((s32)reg & SAA7191_GAIN_LFIS_MASK) |
| 447 | >> SAA7191_GAIN_LFIS_SHIFT; |
| 448 | break; |
| 449 | case SAA7191_CONTROL_HUE: |
| 450 | reg = saa7191_read_reg(client, SAA7191_REG_HUEC); |
| 451 | if (reg < 0x80) |
| 452 | reg += 0x80; |
| 453 | else |
| 454 | reg -= 0x80; |
| 455 | ctrl->value = (s32)reg; |
| 456 | break; |
| 457 | case SAA7191_CONTROL_VTRC: |
| 458 | reg = saa7191_read_reg(client, SAA7191_REG_STDC); |
| 459 | ctrl->value = ((s32)reg & SAA7191_STDC_VTRC) ? 1 : 0; |
| 460 | break; |
| 461 | case SAA7191_CONTROL_LUMA_DELAY: |
| 462 | reg = saa7191_read_reg(client, SAA7191_REG_CTL3); |
| 463 | ctrl->value = ((s32)reg & SAA7191_CTL3_YDEL_MASK) |
| 464 | >> SAA7191_CTL3_YDEL_SHIFT; |
| 465 | if (ctrl->value >= 4) |
| 466 | ctrl->value -= 8; |
| 467 | break; |
| 468 | case SAA7191_CONTROL_VNR: |
| 469 | reg = saa7191_read_reg(client, SAA7191_REG_CTL4); |
| 470 | ctrl->value = ((s32)reg & SAA7191_CTL4_VNOI_MASK) |
| 471 | >> SAA7191_CTL4_VNOI_SHIFT; |
| 472 | break; |
| 473 | default: |
| 474 | ret = -EINVAL; |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 477 | return ret; |
| 478 | } |
| 479 | |
| 480 | static int saa7191_set_control(struct i2c_client *client, |
| 481 | struct saa7191_control *ctrl) |
| 482 | { |
| 483 | u8 reg; |
| 484 | int ret = 0; |
| 485 | |
| 486 | switch (ctrl->type) { |
| 487 | case SAA7191_CONTROL_BANDPASS: |
| 488 | case SAA7191_CONTROL_BANDPASS_WEIGHT: |
| 489 | case SAA7191_CONTROL_CORING: |
| 490 | reg = saa7191_read_reg(client, SAA7191_REG_LUMA); |
| 491 | switch (ctrl->type) { |
| 492 | case SAA7191_CONTROL_BANDPASS: |
| 493 | reg &= ~SAA7191_LUMA_BPSS_MASK; |
| 494 | reg |= (ctrl->value << SAA7191_LUMA_BPSS_SHIFT) |
| 495 | & SAA7191_LUMA_BPSS_MASK; |
| 496 | break; |
| 497 | case SAA7191_CONTROL_BANDPASS_WEIGHT: |
| 498 | reg &= ~SAA7191_LUMA_APER_MASK; |
| 499 | reg |= (ctrl->value << SAA7191_LUMA_APER_SHIFT) |
| 500 | & SAA7191_LUMA_APER_MASK; |
| 501 | break; |
| 502 | case SAA7191_CONTROL_CORING: |
| 503 | reg &= ~SAA7191_LUMA_CORI_MASK; |
| 504 | reg |= (ctrl->value << SAA7191_LUMA_CORI_SHIFT) |
| 505 | & SAA7191_LUMA_CORI_MASK; |
| 506 | break; |
| 507 | } |
| 508 | ret = saa7191_write_reg(client, SAA7191_REG_LUMA, reg); |
| 509 | break; |
| 510 | case SAA7191_CONTROL_FORCE_COLOUR: |
| 511 | case SAA7191_CONTROL_CHROMA_GAIN: |
| 512 | reg = saa7191_read_reg(client, SAA7191_REG_GAIN); |
| 513 | if (ctrl->type == SAA7191_CONTROL_FORCE_COLOUR) { |
| 514 | if (ctrl->value) |
| 515 | reg |= SAA7191_GAIN_COLO; |
| 516 | else |
| 517 | reg &= ~SAA7191_GAIN_COLO; |
| 518 | } else { |
| 519 | reg &= ~SAA7191_GAIN_LFIS_MASK; |
| 520 | reg |= (ctrl->value << SAA7191_GAIN_LFIS_SHIFT) |
| 521 | & SAA7191_GAIN_LFIS_MASK; |
| 522 | } |
| 523 | ret = saa7191_write_reg(client, SAA7191_REG_GAIN, reg); |
| 524 | break; |
| 525 | case SAA7191_CONTROL_HUE: |
| 526 | reg = ctrl->value & 0xff; |
| 527 | if (reg < 0x80) |
| 528 | reg += 0x80; |
| 529 | else |
| 530 | reg -= 0x80; |
| 531 | ret = saa7191_write_reg(client, SAA7191_REG_HUEC, reg); |
| 532 | break; |
| 533 | case SAA7191_CONTROL_VTRC: |
| 534 | reg = saa7191_read_reg(client, SAA7191_REG_STDC); |
| 535 | if (ctrl->value) |
| 536 | reg |= SAA7191_STDC_VTRC; |
| 537 | else |
| 538 | reg &= ~SAA7191_STDC_VTRC; |
| 539 | ret = saa7191_write_reg(client, SAA7191_REG_STDC, reg); |
| 540 | break; |
| 541 | case SAA7191_CONTROL_LUMA_DELAY: { |
| 542 | s32 value = ctrl->value; |
| 543 | if (value < 0) |
| 544 | value += 8; |
| 545 | reg = saa7191_read_reg(client, SAA7191_REG_CTL3); |
| 546 | reg &= ~SAA7191_CTL3_YDEL_MASK; |
| 547 | reg |= (value << SAA7191_CTL3_YDEL_SHIFT) |
| 548 | & SAA7191_CTL3_YDEL_MASK; |
| 549 | ret = saa7191_write_reg(client, SAA7191_REG_CTL3, reg); |
| 550 | break; |
| 551 | } |
| 552 | case SAA7191_CONTROL_VNR: |
| 553 | reg = saa7191_read_reg(client, SAA7191_REG_CTL4); |
| 554 | reg &= ~SAA7191_CTL4_VNOI_MASK; |
| 555 | reg |= (ctrl->value << SAA7191_CTL4_VNOI_SHIFT) |
| 556 | & SAA7191_CTL4_VNOI_MASK; |
| 557 | ret = saa7191_write_reg(client, SAA7191_REG_CTL4, reg); |
| 558 | break; |
| 559 | default: |
| 560 | ret = -EINVAL; |
| 561 | } |
| 562 | |
| 563 | return ret; |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | /* I2C-interface */ |
| 567 | |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 568 | static int saa7191_command(struct i2c_client *client, unsigned int cmd, |
| 569 | void *arg) |
| 570 | { |
| 571 | struct saa7191 *decoder = i2c_get_clientdata(client); |
| 572 | |
| 573 | switch (cmd) { |
| 574 | case DECODER_GET_CAPABILITIES: { |
| 575 | struct video_decoder_capability *cap = arg; |
| 576 | |
| 577 | cap->flags = VIDEO_DECODER_PAL | VIDEO_DECODER_NTSC | |
| 578 | VIDEO_DECODER_SECAM | VIDEO_DECODER_AUTO; |
Ladislav Michl | 495515b | 2005-09-23 10:52:27 +0000 | [diff] [blame] | 579 | cap->inputs = (client->adapter->id == I2C_HW_SGI_VINO) ? 2 : 1; |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 580 | cap->outputs = 1; |
| 581 | break; |
| 582 | } |
| 583 | case DECODER_GET_STATUS: { |
| 584 | int *iarg = arg; |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 585 | u8 status; |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 586 | int res = 0; |
| 587 | |
| 588 | if (saa7191_read_status(client, &status)) { |
| 589 | return -EIO; |
| 590 | } |
| 591 | if ((status & SAA7191_STATUS_HLCK) == 0) |
| 592 | res |= DECODER_STATUS_GOOD; |
| 593 | if (status & SAA7191_STATUS_CODE) |
| 594 | res |= DECODER_STATUS_COLOR; |
| 595 | switch (decoder->norm) { |
| 596 | case SAA7191_NORM_NTSC: |
| 597 | res |= DECODER_STATUS_NTSC; |
| 598 | break; |
| 599 | case SAA7191_NORM_PAL: |
| 600 | res |= DECODER_STATUS_PAL; |
| 601 | break; |
| 602 | case SAA7191_NORM_SECAM: |
| 603 | res |= DECODER_STATUS_SECAM; |
| 604 | break; |
| 605 | case SAA7191_NORM_AUTO: |
| 606 | default: |
| 607 | if (status & SAA7191_STATUS_FIDT) |
| 608 | res |= DECODER_STATUS_NTSC; |
| 609 | else |
| 610 | res |= DECODER_STATUS_PAL; |
| 611 | break; |
| 612 | } |
| 613 | *iarg = res; |
| 614 | break; |
| 615 | } |
| 616 | case DECODER_SET_NORM: { |
| 617 | int *iarg = arg; |
| 618 | |
| 619 | switch (*iarg) { |
| 620 | case VIDEO_MODE_AUTO: |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 621 | return saa7191_autodetect_norm(client); |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 622 | case VIDEO_MODE_PAL: |
| 623 | return saa7191_set_norm(client, SAA7191_NORM_PAL); |
| 624 | case VIDEO_MODE_NTSC: |
| 625 | return saa7191_set_norm(client, SAA7191_NORM_NTSC); |
| 626 | case VIDEO_MODE_SECAM: |
| 627 | return saa7191_set_norm(client, SAA7191_NORM_SECAM); |
| 628 | default: |
| 629 | return -EINVAL; |
| 630 | } |
| 631 | break; |
| 632 | } |
| 633 | case DECODER_SET_INPUT: { |
| 634 | int *iarg = arg; |
| 635 | |
| 636 | switch (client->adapter->id) { |
Ladislav Michl | 495515b | 2005-09-23 10:52:27 +0000 | [diff] [blame] | 637 | case I2C_HW_SGI_VINO: |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 638 | return saa7191_set_input(client, *iarg); |
| 639 | default: |
| 640 | if (*iarg != 0) |
| 641 | return -EINVAL; |
| 642 | } |
| 643 | break; |
| 644 | } |
| 645 | case DECODER_SET_OUTPUT: { |
| 646 | int *iarg = arg; |
| 647 | |
| 648 | /* not much choice of outputs */ |
| 649 | if (*iarg != 0) |
| 650 | return -EINVAL; |
| 651 | break; |
| 652 | } |
| 653 | case DECODER_ENABLE_OUTPUT: { |
| 654 | /* Always enabled */ |
| 655 | break; |
| 656 | } |
| 657 | case DECODER_SET_PICTURE: { |
| 658 | struct video_picture *pic = arg; |
| 659 | unsigned val; |
| 660 | int err; |
| 661 | |
| 662 | val = (pic->hue >> 8) - 0x80; |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 663 | |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 664 | err = saa7191_write_reg(client, SAA7191_REG_HUEC, val); |
| 665 | if (err) |
| 666 | return -EIO; |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 667 | |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 668 | break; |
| 669 | } |
| 670 | case DECODER_SAA7191_GET_STATUS: { |
| 671 | struct saa7191_status *status = arg; |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 672 | u8 status_reg; |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 673 | |
| 674 | if (saa7191_read_status(client, &status_reg)) |
| 675 | return -EIO; |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 676 | |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 677 | status->signal = ((status_reg & SAA7191_STATUS_HLCK) == 0) |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 678 | ? 1 : 0; |
| 679 | status->signal_60hz = (status_reg & SAA7191_STATUS_FIDT) |
| 680 | ? 1 : 0; |
| 681 | status->color = (status_reg & SAA7191_STATUS_CODE) ? 1 : 0; |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 682 | |
| 683 | status->input = decoder->input; |
| 684 | status->norm = decoder->norm; |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 685 | |
| 686 | break; |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 687 | } |
| 688 | case DECODER_SAA7191_SET_NORM: { |
| 689 | int *norm = arg; |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 690 | |
| 691 | switch (*norm) { |
| 692 | case SAA7191_NORM_AUTO: |
| 693 | return saa7191_autodetect_norm(client); |
| 694 | case SAA7191_NORM_AUTO_EXT: |
| 695 | return saa7191_autodetect_norm_extended(client); |
| 696 | default: |
| 697 | return saa7191_set_norm(client, *norm); |
| 698 | } |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 699 | } |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 700 | case DECODER_SAA7191_GET_CONTROL: { |
| 701 | return saa7191_get_control(client, arg); |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 702 | } |
Ladislav Michl | a637a11 | 2005-09-01 15:07:34 +0000 | [diff] [blame] | 703 | case DECODER_SAA7191_SET_CONTROL: { |
| 704 | return saa7191_set_control(client, arg); |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 705 | } |
| 706 | default: |
| 707 | return -EINVAL; |
| 708 | } |
| 709 | |
| 710 | return 0; |
| 711 | } |
| 712 | |
Hans Verkuil | babb7dc | 2009-02-13 07:31:05 -0300 | [diff] [blame^] | 713 | static int saa7191_probe(struct i2c_client *client, |
| 714 | const struct i2c_device_id *id) |
| 715 | { |
| 716 | int err = 0; |
| 717 | struct saa7191 *decoder; |
| 718 | |
| 719 | v4l_info(client, "chip found @ 0x%x (%s)\n", |
| 720 | client->addr << 1, client->adapter->name); |
| 721 | |
| 722 | decoder = kzalloc(sizeof(*decoder), GFP_KERNEL); |
| 723 | if (!decoder) |
| 724 | return -ENOMEM; |
| 725 | |
| 726 | i2c_set_clientdata(client, decoder); |
| 727 | |
| 728 | decoder->client = client; |
| 729 | |
| 730 | err = saa7191_write_block(client, sizeof(initseq), initseq); |
| 731 | if (err) { |
| 732 | printk(KERN_ERR "SAA7191 initialization failed\n"); |
| 733 | kfree(decoder); |
| 734 | return err; |
| 735 | } |
| 736 | |
| 737 | printk(KERN_INFO "SAA7191 initialized\n"); |
| 738 | |
| 739 | decoder->input = SAA7191_INPUT_COMPOSITE; |
| 740 | decoder->norm = SAA7191_NORM_PAL; |
| 741 | |
| 742 | err = saa7191_autodetect_norm(client); |
| 743 | if (err && (err != -EBUSY)) |
| 744 | printk(KERN_ERR "SAA7191: Signal auto-detection failed\n"); |
| 745 | |
| 746 | return 0; |
| 747 | } |
| 748 | |
| 749 | static int saa7191_remove(struct i2c_client *client) |
| 750 | { |
| 751 | struct saa7191 *decoder = i2c_get_clientdata(client); |
| 752 | |
| 753 | kfree(decoder); |
| 754 | return 0; |
| 755 | } |
| 756 | |
| 757 | static int saa7191_legacy_probe(struct i2c_adapter *adapter) |
| 758 | { |
| 759 | return adapter->id == I2C_HW_SGI_VINO; |
| 760 | } |
| 761 | |
| 762 | static const struct i2c_device_id saa7191_id[] = { |
| 763 | { "saa7191", 0 }, |
| 764 | { } |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 765 | }; |
Hans Verkuil | babb7dc | 2009-02-13 07:31:05 -0300 | [diff] [blame^] | 766 | MODULE_DEVICE_TABLE(i2c, saa7191_id); |
Ralf Baechle | d203a7e | 2005-09-06 15:19:37 -0700 | [diff] [blame] | 767 | |
Hans Verkuil | babb7dc | 2009-02-13 07:31:05 -0300 | [diff] [blame^] | 768 | static struct v4l2_i2c_driver_data v4l2_i2c_data = { |
| 769 | .name = "saa7191", |
| 770 | .driverid = I2C_DRIVERID_SAA7191, |
| 771 | .command = saa7191_command, |
| 772 | .probe = saa7191_probe, |
| 773 | .remove = saa7191_remove, |
| 774 | .legacy_probe = saa7191_legacy_probe, |
| 775 | .id_table = saa7191_id, |
| 776 | }; |