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