Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * saa7110 - Philips SAA7110(A) video decoder driver |
| 3 | * |
| 4 | * Copyright (C) 1998 Pauline Middelink <middelin@polyware.nl> |
| 5 | * |
| 6 | * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net> |
| 7 | * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx> |
| 8 | * - some corrections for Pinnacle Systems Inc. DC10plus card. |
| 9 | * |
| 10 | * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net> |
| 11 | * - moved over to linux>=2.4.x i2c protocol (1/1/2003) |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
| 25 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 26 | */ |
| 27 | |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/init.h> |
| 30 | #include <linux/types.h> |
| 31 | #include <linux/delay.h> |
| 32 | #include <linux/slab.h> |
| 33 | #include <linux/wait.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <asm/uaccess.h> |
Hans Verkuil | 85ede69 | 2008-09-07 08:00:32 -0300 | [diff] [blame] | 35 | #include <linux/i2c.h> |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 36 | #include <linux/videodev2.h> |
| 37 | #include <media/v4l2-device.h> |
| 38 | #include <media/v4l2-chip-ident.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | MODULE_DESCRIPTION("Philips SAA7110 video decoder driver"); |
| 41 | MODULE_AUTHOR("Pauline Middelink"); |
| 42 | MODULE_LICENSE("GPL"); |
| 43 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 44 | |
Douglas Schilling Landgraf | ff699e6 | 2008-04-22 14:41:48 -0300 | [diff] [blame] | 45 | static int debug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | module_param(debug, int, 0); |
| 47 | MODULE_PARM_DESC(debug, "Debug level (0-1)"); |
| 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #define SAA7110_MAX_INPUT 9 /* 6 CVBS, 3 SVHS */ |
Jean Delvare | 8182ff6 | 2008-10-24 15:08:28 -0300 | [diff] [blame] | 50 | #define SAA7110_MAX_OUTPUT 1 /* 1 YUV */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #define SAA7110_NR_REG 0x35 |
| 53 | |
| 54 | struct saa7110 { |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 55 | struct v4l2_subdev sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | u8 reg[SAA7110_NR_REG]; |
| 57 | |
Hans Verkuil | 107063c | 2009-02-18 17:26:06 -0300 | [diff] [blame] | 58 | v4l2_std_id norm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | int input; |
| 60 | int enable; |
| 61 | int bright; |
| 62 | int contrast; |
| 63 | int hue; |
| 64 | int sat; |
| 65 | |
| 66 | wait_queue_head_t wq; |
| 67 | }; |
| 68 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 69 | static inline struct saa7110 *to_saa7110(struct v4l2_subdev *sd) |
| 70 | { |
| 71 | return container_of(sd, struct saa7110, sd); |
| 72 | } |
| 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | /* ----------------------------------------------------------------------- */ |
| 75 | /* I2C support functions */ |
| 76 | /* ----------------------------------------------------------------------- */ |
| 77 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 78 | static int saa7110_write(struct v4l2_subdev *sd, u8 reg, u8 value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | { |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 80 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 81 | struct saa7110 *decoder = to_saa7110(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 83 | decoder->reg[reg] = value; |
| 84 | return i2c_smbus_write_byte_data(client, reg, value); |
| 85 | } |
| 86 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 87 | static int saa7110_write_block(struct v4l2_subdev *sd, const u8 *data, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 89 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 90 | struct saa7110 *decoder = to_saa7110(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | int ret = -1; |
| 92 | u8 reg = *data; /* first register to write to */ |
| 93 | |
| 94 | /* Sanity check */ |
| 95 | if (reg + (len - 1) > SAA7110_NR_REG) |
| 96 | return ret; |
| 97 | |
| 98 | /* the saa7110 has an autoincrement function, use it if |
| 99 | * the adapter understands raw I2C */ |
| 100 | if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { |
Jean Delvare | 9aa45e3 | 2006-03-22 03:48:35 -0300 | [diff] [blame] | 101 | ret = i2c_master_send(client, data, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
| 103 | /* Cache the written data */ |
| 104 | memcpy(decoder->reg + reg, data + 1, len - 1); |
| 105 | } else { |
| 106 | for (++data, --len; len; len--) { |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 107 | ret = saa7110_write(sd, reg++, *data++); |
Hans Verkuil | 85ede69 | 2008-09-07 08:00:32 -0300 | [diff] [blame] | 108 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | break; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return ret; |
| 114 | } |
| 115 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 116 | static inline int saa7110_read(struct v4l2_subdev *sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | { |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 118 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | return i2c_smbus_read_byte(client); |
| 121 | } |
| 122 | |
| 123 | /* ----------------------------------------------------------------------- */ |
| 124 | /* SAA7110 functions */ |
| 125 | /* ----------------------------------------------------------------------- */ |
| 126 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 127 | #define FRESP_06H_COMPST 0x03 /*0x13*/ |
| 128 | #define FRESP_06H_SVIDEO 0x83 /*0xC0*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
| 130 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 131 | static int saa7110_selmux(struct v4l2_subdev *sd, int chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | { |
| 133 | static const unsigned char modes[9][8] = { |
| 134 | /* mode 0 */ |
| 135 | {FRESP_06H_COMPST, 0xD9, 0x17, 0x40, 0x03, |
| 136 | 0x44, 0x75, 0x16}, |
| 137 | /* mode 1 */ |
| 138 | {FRESP_06H_COMPST, 0xD8, 0x17, 0x40, 0x03, |
| 139 | 0x44, 0x75, 0x16}, |
| 140 | /* mode 2 */ |
| 141 | {FRESP_06H_COMPST, 0xBA, 0x07, 0x91, 0x03, |
| 142 | 0x60, 0xB5, 0x05}, |
| 143 | /* mode 3 */ |
| 144 | {FRESP_06H_COMPST, 0xB8, 0x07, 0x91, 0x03, |
| 145 | 0x60, 0xB5, 0x05}, |
| 146 | /* mode 4 */ |
| 147 | {FRESP_06H_COMPST, 0x7C, 0x07, 0xD2, 0x83, |
| 148 | 0x60, 0xB5, 0x03}, |
| 149 | /* mode 5 */ |
| 150 | {FRESP_06H_COMPST, 0x78, 0x07, 0xD2, 0x83, |
| 151 | 0x60, 0xB5, 0x03}, |
| 152 | /* mode 6 */ |
| 153 | {FRESP_06H_SVIDEO, 0x59, 0x17, 0x42, 0xA3, |
| 154 | 0x44, 0x75, 0x12}, |
| 155 | /* mode 7 */ |
| 156 | {FRESP_06H_SVIDEO, 0x9A, 0x17, 0xB1, 0x13, |
| 157 | 0x60, 0xB5, 0x14}, |
| 158 | /* mode 8 */ |
| 159 | {FRESP_06H_SVIDEO, 0x3C, 0x27, 0xC1, 0x23, |
| 160 | 0x44, 0x75, 0x21} |
| 161 | }; |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 162 | struct saa7110 *decoder = to_saa7110(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | const unsigned char *ptr = modes[chan]; |
| 164 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 165 | saa7110_write(sd, 0x06, ptr[0]); /* Luminance control */ |
| 166 | saa7110_write(sd, 0x20, ptr[1]); /* Analog Control #1 */ |
| 167 | saa7110_write(sd, 0x21, ptr[2]); /* Analog Control #2 */ |
| 168 | saa7110_write(sd, 0x22, ptr[3]); /* Mixer Control #1 */ |
| 169 | saa7110_write(sd, 0x2C, ptr[4]); /* Mixer Control #2 */ |
| 170 | saa7110_write(sd, 0x30, ptr[5]); /* ADCs gain control */ |
| 171 | saa7110_write(sd, 0x31, ptr[6]); /* Mixer Control #3 */ |
| 172 | saa7110_write(sd, 0x21, ptr[7]); /* Analog Control #2 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | decoder->input = chan; |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static const unsigned char initseq[1 + SAA7110_NR_REG] = { |
| 179 | 0, 0x4C, 0x3C, 0x0D, 0xEF, 0xBD, 0xF2, 0x03, 0x00, |
| 180 | /* 0x08 */ 0xF8, 0xF8, 0x60, 0x60, 0x00, 0x86, 0x18, 0x90, |
| 181 | /* 0x10 */ 0x00, 0x59, 0x40, 0x46, 0x42, 0x1A, 0xFF, 0xDA, |
| 182 | /* 0x18 */ 0xF2, 0x8B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 183 | /* 0x20 */ 0xD9, 0x16, 0x40, 0x41, 0x80, 0x41, 0x80, 0x4F, |
| 184 | /* 0x28 */ 0xFE, 0x01, 0xCF, 0x0F, 0x03, 0x01, 0x03, 0x0C, |
| 185 | /* 0x30 */ 0x44, 0x71, 0x02, 0x8C, 0x02 |
| 186 | }; |
| 187 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 188 | static v4l2_std_id determine_norm(struct v4l2_subdev *sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | { |
| 190 | DEFINE_WAIT(wait); |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 191 | struct saa7110 *decoder = to_saa7110(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | int status; |
| 193 | |
| 194 | /* mode changed, start automatic detection */ |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 195 | saa7110_write_block(sd, initseq, sizeof(initseq)); |
| 196 | saa7110_selmux(sd, decoder->input); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | prepare_to_wait(&decoder->wq, &wait, TASK_UNINTERRUPTIBLE); |
Mauro Carvalho Chehab | 09df5cbe | 2007-07-17 16:25:38 -0300 | [diff] [blame] | 198 | schedule_timeout(msecs_to_jiffies(250)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | finish_wait(&decoder->wq, &wait); |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 200 | status = saa7110_read(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | if (status & 0x40) { |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 202 | v4l2_dbg(1, debug, sd, "status=0x%02x (no signal)\n", status); |
| 203 | return decoder->norm; /* no change*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
| 205 | if ((status & 3) == 0) { |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 206 | saa7110_write(sd, 0x06, 0x83); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | if (status & 0x20) { |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 208 | v4l2_dbg(1, debug, sd, "status=0x%02x (NTSC/no color)\n", status); |
| 209 | /*saa7110_write(sd,0x2E,0x81);*/ |
Hans Verkuil | 107063c | 2009-02-18 17:26:06 -0300 | [diff] [blame] | 210 | return V4L2_STD_NTSC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | } |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 212 | v4l2_dbg(1, debug, sd, "status=0x%02x (PAL/no color)\n", status); |
| 213 | /*saa7110_write(sd,0x2E,0x9A);*/ |
Hans Verkuil | 107063c | 2009-02-18 17:26:06 -0300 | [diff] [blame] | 214 | return V4L2_STD_PAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | } |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 216 | /*saa7110_write(sd,0x06,0x03);*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | if (status & 0x20) { /* 60Hz */ |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 218 | v4l2_dbg(1, debug, sd, "status=0x%02x (NTSC)\n", status); |
| 219 | saa7110_write(sd, 0x0D, 0x86); |
| 220 | saa7110_write(sd, 0x0F, 0x50); |
| 221 | saa7110_write(sd, 0x11, 0x2C); |
| 222 | /*saa7110_write(sd,0x2E,0x81);*/ |
Hans Verkuil | 107063c | 2009-02-18 17:26:06 -0300 | [diff] [blame] | 223 | return V4L2_STD_NTSC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /* 50Hz -> PAL/SECAM */ |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 227 | saa7110_write(sd, 0x0D, 0x86); |
| 228 | saa7110_write(sd, 0x0F, 0x10); |
| 229 | saa7110_write(sd, 0x11, 0x59); |
| 230 | /*saa7110_write(sd,0x2E,0x9A);*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
| 232 | prepare_to_wait(&decoder->wq, &wait, TASK_UNINTERRUPTIBLE); |
Mauro Carvalho Chehab | 09df5cbe | 2007-07-17 16:25:38 -0300 | [diff] [blame] | 233 | schedule_timeout(msecs_to_jiffies(250)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | finish_wait(&decoder->wq, &wait); |
| 235 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 236 | status = saa7110_read(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | if ((status & 0x03) == 0x01) { |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 238 | v4l2_dbg(1, debug, sd, "status=0x%02x (SECAM)\n", status); |
| 239 | saa7110_write(sd, 0x0D, 0x87); |
Hans Verkuil | 107063c | 2009-02-18 17:26:06 -0300 | [diff] [blame] | 240 | return V4L2_STD_SECAM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 242 | v4l2_dbg(1, debug, sd, "status=0x%02x (PAL)\n", status); |
Hans Verkuil | 107063c | 2009-02-18 17:26:06 -0300 | [diff] [blame] | 243 | return V4L2_STD_PAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 246 | static int saa7110_g_input_status(struct v4l2_subdev *sd, u32 *pstatus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | { |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 248 | struct saa7110 *decoder = to_saa7110(sd); |
| 249 | int res = V4L2_IN_ST_NO_SIGNAL; |
| 250 | int status = saa7110_read(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 252 | v4l2_dbg(1, debug, sd, "status=0x%02x norm=%llx\n", |
Hans Verkuil | cc5cef8 | 2009-03-06 10:15:01 -0300 | [diff] [blame] | 253 | status, (unsigned long long)decoder->norm); |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 254 | if (!(status & 0x40)) |
| 255 | res = 0; |
| 256 | if (!(status & 0x03)) |
| 257 | res |= V4L2_IN_ST_NO_COLOR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 259 | *pstatus = res; |
| 260 | return 0; |
| 261 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 263 | static int saa7110_querystd(struct v4l2_subdev *sd, v4l2_std_id *std) |
| 264 | { |
| 265 | *(v4l2_std_id *)std = determine_norm(sd); |
| 266 | return 0; |
| 267 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 269 | static int saa7110_s_std(struct v4l2_subdev *sd, v4l2_std_id std) |
| 270 | { |
| 271 | struct saa7110 *decoder = to_saa7110(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 273 | if (decoder->norm != std) { |
| 274 | decoder->norm = std; |
| 275 | /*saa7110_write(sd, 0x06, 0x03);*/ |
| 276 | if (std & V4L2_STD_NTSC) { |
| 277 | saa7110_write(sd, 0x0D, 0x86); |
| 278 | saa7110_write(sd, 0x0F, 0x50); |
| 279 | saa7110_write(sd, 0x11, 0x2C); |
| 280 | /*saa7110_write(sd, 0x2E, 0x81);*/ |
| 281 | v4l2_dbg(1, debug, sd, "switched to NTSC\n"); |
| 282 | } else if (std & V4L2_STD_PAL) { |
| 283 | saa7110_write(sd, 0x0D, 0x86); |
| 284 | saa7110_write(sd, 0x0F, 0x10); |
| 285 | saa7110_write(sd, 0x11, 0x59); |
| 286 | /*saa7110_write(sd, 0x2E, 0x9A);*/ |
| 287 | v4l2_dbg(1, debug, sd, "switched to PAL\n"); |
| 288 | } else if (std & V4L2_STD_SECAM) { |
| 289 | saa7110_write(sd, 0x0D, 0x87); |
| 290 | saa7110_write(sd, 0x0F, 0x10); |
| 291 | saa7110_write(sd, 0x11, 0x59); |
| 292 | /*saa7110_write(sd, 0x2E, 0x9A);*/ |
| 293 | v4l2_dbg(1, debug, sd, "switched to SECAM\n"); |
| 294 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | return -EINVAL; |
| 296 | } |
Hans Verkuil | 85ede69 | 2008-09-07 08:00:32 -0300 | [diff] [blame] | 297 | } |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 298 | return 0; |
| 299 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
Hans Verkuil | 5325b42 | 2009-04-02 11:26:22 -0300 | [diff] [blame] | 301 | static int saa7110_s_routing(struct v4l2_subdev *sd, |
| 302 | u32 input, u32 output, u32 config) |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 303 | { |
| 304 | struct saa7110 *decoder = to_saa7110(sd); |
Hans Verkuil | 107063c | 2009-02-18 17:26:06 -0300 | [diff] [blame] | 305 | |
Roel Kluin | f14a297 | 2009-10-23 07:59:42 -0300 | [diff] [blame] | 306 | if (input >= SAA7110_MAX_INPUT) { |
Hans Verkuil | 5325b42 | 2009-04-02 11:26:22 -0300 | [diff] [blame] | 307 | v4l2_dbg(1, debug, sd, "input=%d not available\n", input); |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 308 | return -EINVAL; |
Hans Verkuil | 107063c | 2009-02-18 17:26:06 -0300 | [diff] [blame] | 309 | } |
Hans Verkuil | 5325b42 | 2009-04-02 11:26:22 -0300 | [diff] [blame] | 310 | if (decoder->input != input) { |
| 311 | saa7110_selmux(sd, input); |
| 312 | v4l2_dbg(1, debug, sd, "switched to input=%d\n", input); |
Hans Verkuil | 107063c | 2009-02-18 17:26:06 -0300 | [diff] [blame] | 313 | } |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 314 | return 0; |
| 315 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 317 | static int saa7110_s_stream(struct v4l2_subdev *sd, int enable) |
| 318 | { |
| 319 | struct saa7110 *decoder = to_saa7110(sd); |
| 320 | |
| 321 | if (decoder->enable != enable) { |
| 322 | decoder->enable = enable; |
| 323 | saa7110_write(sd, 0x0E, enable ? 0x18 : 0x80); |
| 324 | v4l2_dbg(1, debug, sd, "YUV %s\n", enable ? "on" : "off"); |
| 325 | } |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | static int saa7110_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc) |
| 330 | { |
| 331 | switch (qc->id) { |
| 332 | case V4L2_CID_BRIGHTNESS: |
| 333 | return v4l2_ctrl_query_fill(qc, 0, 255, 1, 128); |
| 334 | case V4L2_CID_CONTRAST: |
| 335 | case V4L2_CID_SATURATION: |
| 336 | return v4l2_ctrl_query_fill(qc, 0, 127, 1, 64); |
| 337 | case V4L2_CID_HUE: |
| 338 | return v4l2_ctrl_query_fill(qc, -128, 127, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | return -EINVAL; |
| 341 | } |
| 342 | return 0; |
| 343 | } |
| 344 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 345 | static int saa7110_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) |
| 346 | { |
| 347 | struct saa7110 *decoder = to_saa7110(sd); |
| 348 | |
| 349 | switch (ctrl->id) { |
| 350 | case V4L2_CID_BRIGHTNESS: |
| 351 | ctrl->value = decoder->bright; |
| 352 | break; |
| 353 | case V4L2_CID_CONTRAST: |
| 354 | ctrl->value = decoder->contrast; |
| 355 | break; |
| 356 | case V4L2_CID_SATURATION: |
| 357 | ctrl->value = decoder->sat; |
| 358 | break; |
| 359 | case V4L2_CID_HUE: |
| 360 | ctrl->value = decoder->hue; |
| 361 | break; |
| 362 | default: |
| 363 | return -EINVAL; |
| 364 | } |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | static int saa7110_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) |
| 369 | { |
| 370 | struct saa7110 *decoder = to_saa7110(sd); |
| 371 | |
| 372 | switch (ctrl->id) { |
| 373 | case V4L2_CID_BRIGHTNESS: |
| 374 | if (decoder->bright != ctrl->value) { |
| 375 | decoder->bright = ctrl->value; |
| 376 | saa7110_write(sd, 0x19, decoder->bright); |
| 377 | } |
| 378 | break; |
| 379 | case V4L2_CID_CONTRAST: |
| 380 | if (decoder->contrast != ctrl->value) { |
| 381 | decoder->contrast = ctrl->value; |
| 382 | saa7110_write(sd, 0x13, decoder->contrast); |
| 383 | } |
| 384 | break; |
| 385 | case V4L2_CID_SATURATION: |
| 386 | if (decoder->sat != ctrl->value) { |
| 387 | decoder->sat = ctrl->value; |
| 388 | saa7110_write(sd, 0x12, decoder->sat); |
| 389 | } |
| 390 | break; |
| 391 | case V4L2_CID_HUE: |
| 392 | if (decoder->hue != ctrl->value) { |
| 393 | decoder->hue = ctrl->value; |
| 394 | saa7110_write(sd, 0x07, decoder->hue); |
| 395 | } |
| 396 | break; |
| 397 | default: |
| 398 | return -EINVAL; |
| 399 | } |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | static int saa7110_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip) |
| 404 | { |
| 405 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 406 | |
| 407 | return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_SAA7110, 0); |
| 408 | } |
| 409 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | /* ----------------------------------------------------------------------- */ |
| 411 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 412 | static const struct v4l2_subdev_core_ops saa7110_core_ops = { |
| 413 | .g_chip_ident = saa7110_g_chip_ident, |
| 414 | .g_ctrl = saa7110_g_ctrl, |
| 415 | .s_ctrl = saa7110_s_ctrl, |
| 416 | .queryctrl = saa7110_queryctrl, |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 417 | .s_std = saa7110_s_std, |
| 418 | }; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 419 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 420 | static const struct v4l2_subdev_video_ops saa7110_video_ops = { |
| 421 | .s_routing = saa7110_s_routing, |
| 422 | .s_stream = saa7110_s_stream, |
| 423 | .querystd = saa7110_querystd, |
| 424 | .g_input_status = saa7110_g_input_status, |
| 425 | }; |
| 426 | |
| 427 | static const struct v4l2_subdev_ops saa7110_ops = { |
| 428 | .core = &saa7110_core_ops, |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 429 | .video = &saa7110_video_ops, |
| 430 | }; |
| 431 | |
| 432 | /* ----------------------------------------------------------------------- */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
Hans Verkuil | 85ede69 | 2008-09-07 08:00:32 -0300 | [diff] [blame] | 434 | static int saa7110_probe(struct i2c_client *client, |
| 435 | const struct i2c_device_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | struct saa7110 *decoder; |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 438 | struct v4l2_subdev *sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | int rv; |
| 440 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | /* Check if the adapter supports the needed features */ |
Hans Verkuil | 85ede69 | 2008-09-07 08:00:32 -0300 | [diff] [blame] | 442 | if (!i2c_check_functionality(client->adapter, |
| 443 | I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) |
| 444 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | |
Hans Verkuil | 85ede69 | 2008-09-07 08:00:32 -0300 | [diff] [blame] | 446 | v4l_info(client, "chip found @ 0x%x (%s)\n", |
| 447 | client->addr << 1, client->adapter->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | |
Panagiotis Issaris | 7408187 | 2006-01-11 19:40:56 -0200 | [diff] [blame] | 449 | decoder = kzalloc(sizeof(struct saa7110), GFP_KERNEL); |
Hans Verkuil | 85ede69 | 2008-09-07 08:00:32 -0300 | [diff] [blame] | 450 | if (!decoder) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | return -ENOMEM; |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 452 | sd = &decoder->sd; |
| 453 | v4l2_i2c_subdev_init(sd, client, &saa7110_ops); |
Hans Verkuil | 107063c | 2009-02-18 17:26:06 -0300 | [diff] [blame] | 454 | decoder->norm = V4L2_STD_PAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | decoder->input = 0; |
| 456 | decoder->enable = 1; |
| 457 | decoder->bright = 32768; |
| 458 | decoder->contrast = 32768; |
| 459 | decoder->hue = 32768; |
| 460 | decoder->sat = 32768; |
| 461 | init_waitqueue_head(&decoder->wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 463 | rv = saa7110_write_block(sd, initseq, sizeof(initseq)); |
Hans Verkuil | 85ede69 | 2008-09-07 08:00:32 -0300 | [diff] [blame] | 464 | if (rv < 0) { |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 465 | v4l2_dbg(1, debug, sd, "init status %d\n", rv); |
Hans Verkuil | 85ede69 | 2008-09-07 08:00:32 -0300 | [diff] [blame] | 466 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | int ver, status; |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 468 | saa7110_write(sd, 0x21, 0x10); |
| 469 | saa7110_write(sd, 0x0e, 0x18); |
| 470 | saa7110_write(sd, 0x0D, 0x04); |
| 471 | ver = saa7110_read(sd); |
| 472 | saa7110_write(sd, 0x0D, 0x06); |
| 473 | /*mdelay(150);*/ |
| 474 | status = saa7110_read(sd); |
| 475 | v4l2_dbg(1, debug, sd, "version %x, status=0x%02x\n", |
Hans Verkuil | 85ede69 | 2008-09-07 08:00:32 -0300 | [diff] [blame] | 476 | ver, status); |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 477 | saa7110_write(sd, 0x0D, 0x86); |
| 478 | saa7110_write(sd, 0x0F, 0x10); |
| 479 | saa7110_write(sd, 0x11, 0x59); |
| 480 | /*saa7110_write(sd, 0x2E, 0x9A);*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 483 | /*saa7110_selmux(sd,0);*/ |
| 484 | /*determine_norm(sd);*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | /* setup and implicit mode 0 select has been performed */ |
| 486 | |
| 487 | return 0; |
| 488 | } |
| 489 | |
Hans Verkuil | 85ede69 | 2008-09-07 08:00:32 -0300 | [diff] [blame] | 490 | static int saa7110_remove(struct i2c_client *client) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | { |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 492 | struct v4l2_subdev *sd = i2c_get_clientdata(client); |
| 493 | |
| 494 | v4l2_device_unregister_subdev(sd); |
| 495 | kfree(to_saa7110(sd)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | /* ----------------------------------------------------------------------- */ |
| 500 | |
Hans Verkuil | 85ede69 | 2008-09-07 08:00:32 -0300 | [diff] [blame] | 501 | static const struct i2c_device_id saa7110_id[] = { |
| 502 | { "saa7110", 0 }, |
| 503 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | }; |
Hans Verkuil | 85ede69 | 2008-09-07 08:00:32 -0300 | [diff] [blame] | 505 | MODULE_DEVICE_TABLE(i2c, saa7110_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | |
Hans Verkuil | ff6e542 | 2010-09-15 15:38:54 -0300 | [diff] [blame] | 507 | static struct i2c_driver saa7110_driver = { |
| 508 | .driver = { |
| 509 | .owner = THIS_MODULE, |
| 510 | .name = "saa7110", |
| 511 | }, |
| 512 | .probe = saa7110_probe, |
| 513 | .remove = saa7110_remove, |
| 514 | .id_table = saa7110_id, |
Hans Verkuil | 85ede69 | 2008-09-07 08:00:32 -0300 | [diff] [blame] | 515 | }; |
Hans Verkuil | ff6e542 | 2010-09-15 15:38:54 -0300 | [diff] [blame] | 516 | |
| 517 | static __init int init_saa7110(void) |
| 518 | { |
| 519 | return i2c_add_driver(&saa7110_driver); |
| 520 | } |
| 521 | |
| 522 | static __exit void exit_saa7110(void) |
| 523 | { |
| 524 | i2c_del_driver(&saa7110_driver); |
| 525 | } |
| 526 | |
| 527 | module_init(init_saa7110); |
| 528 | module_exit(exit_saa7110); |