Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 1 | /* |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 2 | * saa711x - Philips SAA711x video decoder driver version 0.0.1 |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 3 | * |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 4 | * To do: Now, it handles only saa7113/7114. Should be improved to |
| 5 | * handle all Philips saa711x devices. |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 6 | * |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 7 | * Based on saa7113 driver from Dave Perks <dperks@ibm.net> |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 22 | */ |
| 23 | |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/delay.h> |
| 27 | #include <linux/errno.h> |
| 28 | #include <linux/fs.h> |
| 29 | #include <linux/kernel.h> |
| 30 | #include <linux/major.h> |
| 31 | #include <linux/slab.h> |
| 32 | #include <linux/mm.h> |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 33 | #include <linux/signal.h> |
| 34 | #include <asm/io.h> |
| 35 | #include <asm/pgtable.h> |
| 36 | #include <asm/page.h> |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 37 | #include <linux/types.h> |
| 38 | #include <asm/uaccess.h> |
| 39 | #include <linux/videodev.h> |
| 40 | |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 41 | MODULE_DESCRIPTION("Philips SAA711x video decoder driver"); |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 42 | MODULE_AUTHOR("Dave Perks, Jose Ignacio Gijon, Joerg Heckenbach, Mark McClelland, Dwaine Garden"); |
| 43 | MODULE_LICENSE("GPL"); |
| 44 | |
| 45 | #include <linux/i2c.h> |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 46 | |
| 47 | #define I2C_NAME(s) (s)->name |
| 48 | |
| 49 | #include <linux/video_decoder.h> |
| 50 | |
| 51 | static int debug = 0; |
Eric Sesterhenn / snakebyte | 9b565eb | 2006-01-13 14:10:23 -0200 | [diff] [blame] | 52 | module_param(debug, int, 0644); |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 53 | MODULE_PARM_DESC(debug, " Set the default Debug level. Default: 0 (Off) - (0-1)"); |
| 54 | |
| 55 | |
| 56 | #define dprintk(num, format, args...) \ |
| 57 | do { \ |
| 58 | if (debug >= num) \ |
Mauro Carvalho Chehab | 674434c | 2005-12-12 00:37:28 -0800 | [diff] [blame] | 59 | printk(format, ##args); \ |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 60 | } while (0) |
| 61 | |
| 62 | /* ----------------------------------------------------------------------- */ |
| 63 | |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 64 | struct saa711x { |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 65 | unsigned char reg[32]; |
| 66 | |
| 67 | int norm; |
| 68 | int input; |
| 69 | int enable; |
| 70 | int bright; |
| 71 | int contrast; |
| 72 | int hue; |
| 73 | int sat; |
| 74 | }; |
| 75 | |
| 76 | #define I2C_SAA7113 0x4A |
Markus Rechberger | 791b403 | 2005-11-08 21:38:08 -0800 | [diff] [blame] | 77 | #define I2C_SAA7114 0x42 |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 78 | |
| 79 | /* ----------------------------------------------------------------------- */ |
| 80 | |
| 81 | static inline int |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 82 | saa711x_write (struct i2c_client *client, |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 83 | u8 reg, |
| 84 | u8 value) |
| 85 | { |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 86 | struct saa711x *decoder = i2c_get_clientdata(client); |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 87 | |
| 88 | decoder->reg[reg] = value; |
| 89 | return i2c_smbus_write_byte_data(client, reg, value); |
| 90 | } |
| 91 | |
| 92 | static int |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 93 | saa711x_write_block (struct i2c_client *client, |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 94 | const u8 *data, |
| 95 | unsigned int len) |
| 96 | { |
| 97 | int ret = -1; |
| 98 | u8 reg; |
| 99 | |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 100 | /* the saa711x has an autoincrement function, use it if |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 101 | * the adapter understands raw I2C */ |
| 102 | if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { |
| 103 | /* do raw I2C, not smbus compatible */ |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 104 | struct saa711x *decoder = i2c_get_clientdata(client); |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 105 | struct i2c_msg msg; |
| 106 | u8 block_data[32]; |
| 107 | |
| 108 | msg.addr = client->addr; |
| 109 | msg.flags = 0; |
| 110 | while (len >= 2) { |
| 111 | msg.buf = (char *) block_data; |
| 112 | msg.len = 0; |
| 113 | block_data[msg.len++] = reg = data[0]; |
| 114 | do { |
| 115 | block_data[msg.len++] = |
| 116 | decoder->reg[reg++] = data[1]; |
| 117 | len -= 2; |
| 118 | data += 2; |
| 119 | } while (len >= 2 && data[0] == reg && |
| 120 | msg.len < 32); |
| 121 | if ((ret = i2c_transfer(client->adapter, |
| 122 | &msg, 1)) < 0) |
| 123 | break; |
| 124 | } |
| 125 | } else { |
| 126 | /* do some slow I2C emulation kind of thing */ |
| 127 | while (len >= 2) { |
| 128 | reg = *data++; |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 129 | if ((ret = saa711x_write(client, reg, |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 130 | *data++)) < 0) |
| 131 | break; |
| 132 | len -= 2; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | static int |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 140 | saa711x_init_decoder (struct i2c_client *client, |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 141 | struct video_decoder_init *init) |
| 142 | { |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 143 | return saa711x_write_block(client, init->data, init->len); |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static inline int |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 147 | saa711x_read (struct i2c_client *client, |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 148 | u8 reg) |
| 149 | { |
| 150 | return i2c_smbus_read_byte_data(client, reg); |
| 151 | } |
| 152 | |
| 153 | /* ----------------------------------------------------------------------- */ |
| 154 | |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 155 | static const unsigned char saa711x_i2c_init[] = { |
| 156 | 0x00, 0x00, /* PH711x_CHIP_VERSION 00 - ID byte */ |
| 157 | 0x01, 0x08, /* PH711x_INCREMENT_DELAY - (1) (1) (1) (1) IDEL3 IDEL2 IDELL1 IDEL0 */ |
| 158 | 0x02, 0xc0, /* PH711x_ANALOG_INPUT_CONTR_1 - FUSE1 FUSE0 GUDL1 GUDL0 MODE3 MODE2 MODE1 MODE0 */ |
| 159 | 0x03, 0x23, /* PH711x_ANALOG_INPUT_CONTR_2 - (1) HLNRS VBSL WPOFF HOLDG GAFIX GAI28 GAI18 */ |
| 160 | 0x04, 0x00, /* PH711x_ANALOG_INPUT_CONTR_3 - GAI17 GAI16 GAI15 GAI14 GAI13 GAI12 GAI11 GAI10 */ |
| 161 | 0x05, 0x00, /* PH711x_ANALOG_INPUT_CONTR_4 - GAI27 GAI26 GAI25 GAI24 GAI23 GAI22 GAI21 GAI20 */ |
| 162 | 0x06, 0xeb, /* PH711x_HORIZONTAL_SYNC_START - HSB7 HSB6 HSB5 HSB4 HSB3 HSB2 HSB1 HSB0 */ |
| 163 | 0x07, 0xe0, /* PH711x_HORIZONTAL_SYNC_STOP - HSS7 HSS6 HSS5 HSS4 HSS3 HSS2 HSS1 HSS0 */ |
| 164 | 0x08, 0x88, /* PH711x_SYNC_CONTROL - AUFD FSEL FOET HTC1 HTC0 HPLL VNOI1 VNOI0 */ |
| 165 | 0x09, 0x00, /* PH711x_LUMINANCE_CONTROL - BYPS PREF BPSS1 BPSS0 VBLB UPTCV APER1 APER0 */ |
| 166 | 0x0a, 0x80, /* PH711x_LUMINANCE_BRIGHTNESS - BRIG7 BRIG6 BRIG5 BRIG4 BRIG3 BRIG2 BRIG1 BRIG0 */ |
| 167 | 0x0b, 0x47, /* PH711x_LUMINANCE_CONTRAST - CONT7 CONT6 CONT5 CONT4 CONT3 CONT2 CONT1 CONT0 */ |
| 168 | 0x0c, 0x40, /* PH711x_CHROMA_SATURATION - SATN7 SATN6 SATN5 SATN4 SATN3 SATN2 SATN1 SATN0 */ |
| 169 | 0x0d, 0x00, /* PH711x_CHROMA_HUE_CONTROL - HUEC7 HUEC6 HUEC5 HUEC4 HUEC3 HUEC2 HUEC1 HUEC0 */ |
| 170 | 0x0e, 0x01, /* PH711x_CHROMA_CONTROL - CDTO CSTD2 CSTD1 CSTD0 DCCF FCTC CHBW1 CHBW0 */ |
| 171 | 0x0f, 0xaa, /* PH711x_CHROMA_GAIN_CONTROL - ACGC CGAIN6 CGAIN5 CGAIN4 CGAIN3 CGAIN2 CGAIN1 CGAIN0 */ |
| 172 | 0x10, 0x00, /* PH711x_FORMAT_DELAY_CONTROL - OFTS1 OFTS0 HDEL1 HDEL0 VRLN YDEL2 YDEL1 YDEL0 */ |
| 173 | 0x11, 0x1C, /* PH711x_OUTPUT_CONTROL_1 - GPSW1 CM99 GPSW0 HLSEL OEYC OERT VIPB COLO */ |
| 174 | 0x12, 0x01, /* PH711x_OUTPUT_CONTROL_2 - RTSE13 RTSE12 RTSE11 RTSE10 RTSE03 RTSE02 RTSE01 RTSE00 */ |
| 175 | 0x13, 0x00, /* PH711x_OUTPUT_CONTROL_3 - ADLSB (1) (1) OLDSB FIDP (1) AOSL1 AOSL0 */ |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 176 | 0x14, 0x00, /* RESERVED 14 - (1) (1) (1) (1) (1) (1) (1) (1) */ |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 177 | 0x15, 0x00, /* PH711x_V_GATE1_START - VSTA7 VSTA6 VSTA5 VSTA4 VSTA3 VSTA2 VSTA1 VSTA0 */ |
| 178 | 0x16, 0x00, /* PH711x_V_GATE1_STOP - VSTO7 VSTO6 VSTO5 VSTO4 VSTO3 VSTO2 VSTO1 VSTO0 */ |
| 179 | 0x17, 0x00, /* PH711x_V_GATE1_MSB - (1) (1) (1) (1) (1) (1) VSTO8 VSTA8 */ |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | static int |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 183 | saa711x_command (struct i2c_client *client, |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 184 | unsigned int cmd, |
| 185 | void *arg) |
| 186 | { |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 187 | struct saa711x *decoder = i2c_get_clientdata(client); |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 188 | |
| 189 | switch (cmd) { |
| 190 | |
| 191 | case 0: |
| 192 | case DECODER_INIT: |
| 193 | { |
| 194 | struct video_decoder_init *init = arg; |
| 195 | if (NULL != init) |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 196 | return saa711x_init_decoder(client, init); |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 197 | else { |
| 198 | struct video_decoder_init vdi; |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 199 | vdi.data = saa711x_i2c_init; |
| 200 | vdi.len = sizeof(saa711x_i2c_init); |
| 201 | return saa711x_init_decoder(client, &vdi); |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
| 205 | case DECODER_DUMP: |
| 206 | { |
| 207 | int i; |
| 208 | |
| 209 | for (i = 0; i < 32; i += 16) { |
| 210 | int j; |
| 211 | |
| 212 | printk(KERN_DEBUG "%s: %03x", I2C_NAME(client), i); |
| 213 | for (j = 0; j < 16; ++j) { |
| 214 | printk(" %02x", |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 215 | saa711x_read(client, i + j)); |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 216 | } |
| 217 | printk("\n"); |
| 218 | } |
| 219 | } |
| 220 | break; |
| 221 | |
| 222 | case DECODER_GET_CAPABILITIES: |
| 223 | { |
| 224 | struct video_decoder_capability *cap = arg; |
| 225 | |
| 226 | cap->flags = VIDEO_DECODER_PAL | |
| 227 | VIDEO_DECODER_NTSC | |
| 228 | VIDEO_DECODER_SECAM | |
| 229 | VIDEO_DECODER_AUTO | |
| 230 | VIDEO_DECODER_CCIR; |
| 231 | cap->inputs = 8; |
| 232 | cap->outputs = 1; |
| 233 | } |
| 234 | break; |
| 235 | |
| 236 | case DECODER_GET_STATUS: |
| 237 | { |
| 238 | int *iarg = arg; |
| 239 | int status; |
| 240 | int res; |
| 241 | |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 242 | status = saa711x_read(client, 0x1f); |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 243 | dprintk(1, KERN_DEBUG "%s status: 0x%02x\n", I2C_NAME(client), |
| 244 | status); |
| 245 | res = 0; |
| 246 | if ((status & (1 << 6)) == 0) { |
| 247 | res |= DECODER_STATUS_GOOD; |
| 248 | } |
| 249 | switch (decoder->norm) { |
| 250 | case VIDEO_MODE_NTSC: |
| 251 | res |= DECODER_STATUS_NTSC; |
| 252 | break; |
| 253 | case VIDEO_MODE_PAL: |
| 254 | res |= DECODER_STATUS_PAL; |
| 255 | break; |
| 256 | case VIDEO_MODE_SECAM: |
| 257 | res |= DECODER_STATUS_SECAM; |
| 258 | break; |
| 259 | default: |
| 260 | case VIDEO_MODE_AUTO: |
| 261 | if ((status & (1 << 5)) != 0) { |
| 262 | res |= DECODER_STATUS_NTSC; |
| 263 | } else { |
| 264 | res |= DECODER_STATUS_PAL; |
| 265 | } |
| 266 | break; |
| 267 | } |
| 268 | if ((status & (1 << 0)) != 0) { |
| 269 | res |= DECODER_STATUS_COLOR; |
| 270 | } |
| 271 | *iarg = res; |
| 272 | } |
| 273 | break; |
| 274 | |
| 275 | case DECODER_SET_GPIO: |
| 276 | { |
| 277 | int *iarg = arg; |
| 278 | if (0 != *iarg) { |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 279 | saa711x_write(client, 0x11, |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 280 | (decoder->reg[0x11] | 0x80)); |
| 281 | } else { |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 282 | saa711x_write(client, 0x11, |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 283 | (decoder->reg[0x11] & 0x7f)); |
| 284 | } |
| 285 | break; |
| 286 | } |
| 287 | |
| 288 | case DECODER_SET_VBI_BYPASS: |
| 289 | { |
| 290 | int *iarg = arg; |
| 291 | if (0 != *iarg) { |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 292 | saa711x_write(client, 0x13, |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 293 | (decoder->reg[0x13] & 0xf0) | 0x0a); |
| 294 | } else { |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 295 | saa711x_write(client, 0x13, |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 296 | (decoder->reg[0x13] & 0xf0)); |
| 297 | } |
| 298 | break; |
| 299 | } |
| 300 | |
| 301 | case DECODER_SET_NORM: |
| 302 | { |
| 303 | int *iarg = arg; |
| 304 | |
| 305 | switch (*iarg) { |
| 306 | |
| 307 | case VIDEO_MODE_NTSC: |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 308 | saa711x_write(client, 0x08, |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 309 | (decoder->reg[0x08] & 0x3f) | 0x40); |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 310 | saa711x_write(client, 0x0e, |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 311 | (decoder->reg[0x0e] & 0x8f)); |
| 312 | break; |
| 313 | |
| 314 | case VIDEO_MODE_PAL: |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 315 | saa711x_write(client, 0x08, |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 316 | (decoder->reg[0x08] & 0x3f) | 0x00); |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 317 | saa711x_write(client, 0x0e, |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 318 | (decoder->reg[0x0e] & 0x8f)); |
| 319 | break; |
| 320 | |
| 321 | case VIDEO_MODE_SECAM: |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 322 | saa711x_write(client, 0x08, |
Dwaine Garden | 1bcd2a3 | 2005-12-01 00:51:37 -0800 | [diff] [blame] | 323 | (decoder->reg[0x08] & 0x3f) | 0x00); |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 324 | saa711x_write(client, 0x0e, |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 325 | (decoder->reg[0x0e] & 0x8f) | 0x50); |
| 326 | break; |
| 327 | |
| 328 | case VIDEO_MODE_AUTO: |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 329 | saa711x_write(client, 0x08, |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 330 | (decoder->reg[0x08] & 0x3f) | 0x80); |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 331 | saa711x_write(client, 0x0e, |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 332 | (decoder->reg[0x0e] & 0x8f)); |
| 333 | break; |
| 334 | |
| 335 | default: |
| 336 | return -EINVAL; |
| 337 | |
| 338 | } |
| 339 | decoder->norm = *iarg; |
| 340 | } |
| 341 | break; |
| 342 | |
| 343 | case DECODER_SET_INPUT: |
| 344 | { |
| 345 | int *iarg = arg; |
| 346 | if (*iarg < 0 || *iarg > 9) { |
| 347 | return -EINVAL; |
| 348 | } |
| 349 | if (decoder->input != *iarg) { |
| 350 | decoder->input = *iarg; |
| 351 | /* select mode */ |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 352 | saa711x_write(client, 0x02, |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 353 | (decoder->reg[0x02] & 0xf0) | decoder->input); |
| 354 | /* bypass chrominance trap for modes 4..7 */ |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 355 | saa711x_write(client, 0x09, |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 356 | (decoder->reg[0x09] & 0x7f) | ((decoder->input > 3) ? 0x80 : 0)); |
| 357 | } |
| 358 | } |
| 359 | break; |
| 360 | |
| 361 | case DECODER_SET_OUTPUT: |
| 362 | { |
| 363 | int *iarg = arg; |
| 364 | |
| 365 | /* not much choice of outputs */ |
| 366 | if (*iarg != 0) { |
| 367 | return -EINVAL; |
| 368 | } |
| 369 | } |
| 370 | break; |
| 371 | |
| 372 | case DECODER_ENABLE_OUTPUT: |
| 373 | { |
| 374 | int *iarg = arg; |
| 375 | int enable = (*iarg != 0); |
| 376 | |
| 377 | if (decoder->enable != enable) { |
| 378 | decoder->enable = enable; |
| 379 | |
| 380 | /* RJ: If output should be disabled (for |
| 381 | * playing videos), we also need a open PLL. |
| 382 | * The input is set to 0 (where no input |
| 383 | * source is connected), although this |
| 384 | * is not necessary. |
| 385 | * |
| 386 | * If output should be enabled, we have to |
| 387 | * reverse the above. |
| 388 | */ |
| 389 | |
| 390 | if (decoder->enable) { |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 391 | saa711x_write(client, 0x02, |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 392 | (decoder-> |
| 393 | reg[0x02] & 0xf8) | |
| 394 | decoder->input); |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 395 | saa711x_write(client, 0x08, |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 396 | (decoder->reg[0x08] & 0xfb)); |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 397 | saa711x_write(client, 0x11, |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 398 | (decoder-> |
| 399 | reg[0x11] & 0xf3) | 0x0c); |
| 400 | } else { |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 401 | saa711x_write(client, 0x02, |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 402 | (decoder->reg[0x02] & 0xf8)); |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 403 | saa711x_write(client, 0x08, |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 404 | (decoder-> |
| 405 | reg[0x08] & 0xfb) | 0x04); |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 406 | saa711x_write(client, 0x11, |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 407 | (decoder->reg[0x11] & 0xf3)); |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | break; |
| 412 | |
| 413 | case DECODER_SET_PICTURE: |
| 414 | { |
| 415 | struct video_picture *pic = arg; |
| 416 | |
| 417 | if (decoder->bright != pic->brightness) { |
| 418 | /* We want 0 to 255 we get 0-65535 */ |
| 419 | decoder->bright = pic->brightness; |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 420 | saa711x_write(client, 0x0a, decoder->bright >> 8); |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 421 | } |
| 422 | if (decoder->contrast != pic->contrast) { |
| 423 | /* We want 0 to 127 we get 0-65535 */ |
| 424 | decoder->contrast = pic->contrast; |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 425 | saa711x_write(client, 0x0b, |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 426 | decoder->contrast >> 9); |
| 427 | } |
| 428 | if (decoder->sat != pic->colour) { |
| 429 | /* We want 0 to 127 we get 0-65535 */ |
| 430 | decoder->sat = pic->colour; |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 431 | saa711x_write(client, 0x0c, decoder->sat >> 9); |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 432 | } |
| 433 | if (decoder->hue != pic->hue) { |
| 434 | /* We want -128 to 127 we get 0-65535 */ |
| 435 | decoder->hue = pic->hue; |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 436 | saa711x_write(client, 0x0d, |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 437 | (decoder->hue - 32768) >> 8); |
| 438 | } |
| 439 | } |
| 440 | break; |
| 441 | |
| 442 | default: |
| 443 | return -EINVAL; |
| 444 | } |
| 445 | |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | /* ----------------------------------------------------------------------- */ |
| 450 | |
| 451 | /* |
| 452 | * Generic i2c probe |
| 453 | * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1' |
| 454 | */ |
| 455 | |
| 456 | /* standard i2c insmod options */ |
| 457 | static unsigned short normal_i2c[] = { |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 458 | I2C_SAA7113>>1, /* saa7113 */ |
Markus Rechberger | 791b403 | 2005-11-08 21:38:08 -0800 | [diff] [blame] | 459 | I2C_SAA7114>>1, /* saa7114 */ |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 460 | I2C_CLIENT_END |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 461 | }; |
| 462 | |
| 463 | I2C_CLIENT_INSMOD; |
| 464 | |
| 465 | |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 466 | static struct i2c_driver i2c_driver_saa711x; |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 467 | |
| 468 | static int |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 469 | saa711x_detect_client (struct i2c_adapter *adapter, |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 470 | int address, |
| 471 | int kind) |
| 472 | { |
| 473 | int i; |
| 474 | struct i2c_client *client; |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 475 | struct saa711x *decoder; |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 476 | struct video_decoder_init vdi; |
| 477 | |
| 478 | dprintk(1, |
| 479 | KERN_INFO |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 480 | "saa711x.c: detecting saa711x client on address 0x%x\n", |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 481 | address << 1); |
| 482 | |
| 483 | /* Check if the adapter supports the needed features */ |
| 484 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 485 | return 0; |
| 486 | |
Panagiotis Issaris | 7408187 | 2006-01-11 19:40:56 -0200 | [diff] [blame] | 487 | client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL); |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 488 | if (client == 0) |
| 489 | return -ENOMEM; |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 490 | client->addr = address; |
| 491 | client->adapter = adapter; |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 492 | client->driver = &i2c_driver_saa711x; |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 493 | strlcpy(I2C_NAME(client), "saa711x", sizeof(I2C_NAME(client))); |
Panagiotis Issaris | 7408187 | 2006-01-11 19:40:56 -0200 | [diff] [blame] | 494 | decoder = kzalloc(sizeof(struct saa711x), GFP_KERNEL); |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 495 | if (decoder == NULL) { |
| 496 | kfree(client); |
| 497 | return -ENOMEM; |
| 498 | } |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 499 | decoder->norm = VIDEO_MODE_NTSC; |
| 500 | decoder->input = 0; |
| 501 | decoder->enable = 1; |
| 502 | decoder->bright = 32768; |
| 503 | decoder->contrast = 32768; |
| 504 | decoder->hue = 32768; |
| 505 | decoder->sat = 32768; |
| 506 | i2c_set_clientdata(client, decoder); |
| 507 | |
| 508 | i = i2c_attach_client(client); |
| 509 | if (i) { |
| 510 | kfree(client); |
| 511 | kfree(decoder); |
| 512 | return i; |
| 513 | } |
| 514 | |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 515 | vdi.data = saa711x_i2c_init; |
| 516 | vdi.len = sizeof(saa711x_i2c_init); |
| 517 | i = saa711x_init_decoder(client, &vdi); |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 518 | if (i < 0) { |
| 519 | dprintk(1, KERN_ERR "%s_attach error: init status %d\n", |
| 520 | I2C_NAME(client), i); |
| 521 | } else { |
| 522 | dprintk(1, |
| 523 | KERN_INFO |
| 524 | "%s_attach: chip version %x at address 0x%x\n", |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 525 | I2C_NAME(client), saa711x_read(client, 0x00) >> 4, |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 526 | client->addr << 1); |
| 527 | } |
| 528 | |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | static int |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 533 | saa711x_attach_adapter (struct i2c_adapter *adapter) |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 534 | { |
| 535 | dprintk(1, |
| 536 | KERN_INFO |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 537 | "saa711x.c: starting probe for adapter %s (0x%x)\n", |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 538 | I2C_NAME(adapter), adapter->id); |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 539 | return i2c_probe(adapter, &addr_data, &saa711x_detect_client); |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | static int |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 543 | saa711x_detach_client (struct i2c_client *client) |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 544 | { |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 545 | struct saa711x *decoder = i2c_get_clientdata(client); |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 546 | int err; |
| 547 | |
| 548 | err = i2c_detach_client(client); |
| 549 | if (err) { |
| 550 | return err; |
| 551 | } |
| 552 | |
| 553 | kfree(decoder); |
| 554 | kfree(client); |
| 555 | |
| 556 | return 0; |
| 557 | } |
| 558 | |
| 559 | /* ----------------------------------------------------------------------- */ |
| 560 | |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 561 | static struct i2c_driver i2c_driver_saa711x = { |
Laurent Riffard | 604f28e | 2005-11-26 20:43:39 +0100 | [diff] [blame] | 562 | .driver = { |
Laurent Riffard | 604f28e | 2005-11-26 20:43:39 +0100 | [diff] [blame] | 563 | .name = "saa711x", |
| 564 | }, |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 565 | .id = I2C_DRIVERID_SAA711X, |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 566 | .attach_adapter = saa711x_attach_adapter, |
| 567 | .detach_client = saa711x_detach_client, |
| 568 | .command = saa711x_command, |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 569 | }; |
| 570 | |
| 571 | static int __init |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 572 | saa711x_init (void) |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 573 | { |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 574 | return i2c_add_driver(&i2c_driver_saa711x); |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | static void __exit |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 578 | saa711x_exit (void) |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 579 | { |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 580 | i2c_del_driver(&i2c_driver_saa711x); |
Dwaine Garden | 340622c | 2005-11-08 21:37:34 -0800 | [diff] [blame] | 581 | } |
| 582 | |
Mauro Carvalho Chehab | 404b32f | 2005-11-08 21:38:29 -0800 | [diff] [blame] | 583 | module_init(saa711x_init); |
| 584 | module_exit(saa711x_exit); |