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> |
Hans Verkuil | 4744ebf | 2010-12-12 08:27:48 -0300 | [diff] [blame] | 38 | #include <media/v4l2-ctrls.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; |
Hans Verkuil | 4744ebf | 2010-12-12 08:27:48 -0300 | [diff] [blame] | 56 | struct v4l2_ctrl_handler hdl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | u8 reg[SAA7110_NR_REG]; |
| 58 | |
Hans Verkuil | 107063c | 2009-02-18 17:26:06 -0300 | [diff] [blame] | 59 | v4l2_std_id norm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | int input; |
| 61 | int enable; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
| 63 | wait_queue_head_t wq; |
| 64 | }; |
| 65 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 66 | static inline struct saa7110 *to_saa7110(struct v4l2_subdev *sd) |
| 67 | { |
| 68 | return container_of(sd, struct saa7110, sd); |
| 69 | } |
| 70 | |
Hans Verkuil | 4744ebf | 2010-12-12 08:27:48 -0300 | [diff] [blame] | 71 | static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl) |
| 72 | { |
| 73 | return &container_of(ctrl->handler, struct saa7110, hdl)->sd; |
| 74 | } |
| 75 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | /* ----------------------------------------------------------------------- */ |
| 77 | /* I2C support functions */ |
| 78 | /* ----------------------------------------------------------------------- */ |
| 79 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 80 | static int saa7110_write(struct v4l2_subdev *sd, u8 reg, u8 value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | { |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 82 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 83 | struct saa7110 *decoder = to_saa7110(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
| 85 | decoder->reg[reg] = value; |
| 86 | return i2c_smbus_write_byte_data(client, reg, value); |
| 87 | } |
| 88 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 89 | 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] | 90 | { |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 91 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 92 | struct saa7110 *decoder = to_saa7110(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | int ret = -1; |
| 94 | u8 reg = *data; /* first register to write to */ |
| 95 | |
| 96 | /* Sanity check */ |
| 97 | if (reg + (len - 1) > SAA7110_NR_REG) |
| 98 | return ret; |
| 99 | |
| 100 | /* the saa7110 has an autoincrement function, use it if |
| 101 | * the adapter understands raw I2C */ |
| 102 | if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { |
Jean Delvare | 9aa45e3 | 2006-03-22 03:48:35 -0300 | [diff] [blame] | 103 | ret = i2c_master_send(client, data, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
| 105 | /* Cache the written data */ |
| 106 | memcpy(decoder->reg + reg, data + 1, len - 1); |
| 107 | } else { |
| 108 | for (++data, --len; len; len--) { |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 109 | ret = saa7110_write(sd, reg++, *data++); |
Hans Verkuil | 85ede69 | 2008-09-07 08:00:32 -0300 | [diff] [blame] | 110 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | break; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return ret; |
| 116 | } |
| 117 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 118 | static inline int saa7110_read(struct v4l2_subdev *sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 120 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | return i2c_smbus_read_byte(client); |
| 123 | } |
| 124 | |
| 125 | /* ----------------------------------------------------------------------- */ |
| 126 | /* SAA7110 functions */ |
| 127 | /* ----------------------------------------------------------------------- */ |
| 128 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 129 | #define FRESP_06H_COMPST 0x03 /*0x13*/ |
| 130 | #define FRESP_06H_SVIDEO 0x83 /*0xC0*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
| 132 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 133 | static int saa7110_selmux(struct v4l2_subdev *sd, int chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | { |
| 135 | static const unsigned char modes[9][8] = { |
| 136 | /* mode 0 */ |
| 137 | {FRESP_06H_COMPST, 0xD9, 0x17, 0x40, 0x03, |
| 138 | 0x44, 0x75, 0x16}, |
| 139 | /* mode 1 */ |
| 140 | {FRESP_06H_COMPST, 0xD8, 0x17, 0x40, 0x03, |
| 141 | 0x44, 0x75, 0x16}, |
| 142 | /* mode 2 */ |
| 143 | {FRESP_06H_COMPST, 0xBA, 0x07, 0x91, 0x03, |
| 144 | 0x60, 0xB5, 0x05}, |
| 145 | /* mode 3 */ |
| 146 | {FRESP_06H_COMPST, 0xB8, 0x07, 0x91, 0x03, |
| 147 | 0x60, 0xB5, 0x05}, |
| 148 | /* mode 4 */ |
| 149 | {FRESP_06H_COMPST, 0x7C, 0x07, 0xD2, 0x83, |
| 150 | 0x60, 0xB5, 0x03}, |
| 151 | /* mode 5 */ |
| 152 | {FRESP_06H_COMPST, 0x78, 0x07, 0xD2, 0x83, |
| 153 | 0x60, 0xB5, 0x03}, |
| 154 | /* mode 6 */ |
| 155 | {FRESP_06H_SVIDEO, 0x59, 0x17, 0x42, 0xA3, |
| 156 | 0x44, 0x75, 0x12}, |
| 157 | /* mode 7 */ |
| 158 | {FRESP_06H_SVIDEO, 0x9A, 0x17, 0xB1, 0x13, |
| 159 | 0x60, 0xB5, 0x14}, |
| 160 | /* mode 8 */ |
| 161 | {FRESP_06H_SVIDEO, 0x3C, 0x27, 0xC1, 0x23, |
| 162 | 0x44, 0x75, 0x21} |
| 163 | }; |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 164 | struct saa7110 *decoder = to_saa7110(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | const unsigned char *ptr = modes[chan]; |
| 166 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 167 | saa7110_write(sd, 0x06, ptr[0]); /* Luminance control */ |
| 168 | saa7110_write(sd, 0x20, ptr[1]); /* Analog Control #1 */ |
| 169 | saa7110_write(sd, 0x21, ptr[2]); /* Analog Control #2 */ |
| 170 | saa7110_write(sd, 0x22, ptr[3]); /* Mixer Control #1 */ |
| 171 | saa7110_write(sd, 0x2C, ptr[4]); /* Mixer Control #2 */ |
| 172 | saa7110_write(sd, 0x30, ptr[5]); /* ADCs gain control */ |
| 173 | saa7110_write(sd, 0x31, ptr[6]); /* Mixer Control #3 */ |
| 174 | saa7110_write(sd, 0x21, ptr[7]); /* Analog Control #2 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | decoder->input = chan; |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static const unsigned char initseq[1 + SAA7110_NR_REG] = { |
| 181 | 0, 0x4C, 0x3C, 0x0D, 0xEF, 0xBD, 0xF2, 0x03, 0x00, |
| 182 | /* 0x08 */ 0xF8, 0xF8, 0x60, 0x60, 0x00, 0x86, 0x18, 0x90, |
| 183 | /* 0x10 */ 0x00, 0x59, 0x40, 0x46, 0x42, 0x1A, 0xFF, 0xDA, |
| 184 | /* 0x18 */ 0xF2, 0x8B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 185 | /* 0x20 */ 0xD9, 0x16, 0x40, 0x41, 0x80, 0x41, 0x80, 0x4F, |
| 186 | /* 0x28 */ 0xFE, 0x01, 0xCF, 0x0F, 0x03, 0x01, 0x03, 0x0C, |
| 187 | /* 0x30 */ 0x44, 0x71, 0x02, 0x8C, 0x02 |
| 188 | }; |
| 189 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 190 | static v4l2_std_id determine_norm(struct v4l2_subdev *sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | { |
| 192 | DEFINE_WAIT(wait); |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 193 | struct saa7110 *decoder = to_saa7110(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | int status; |
| 195 | |
| 196 | /* mode changed, start automatic detection */ |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 197 | saa7110_write_block(sd, initseq, sizeof(initseq)); |
| 198 | saa7110_selmux(sd, decoder->input); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | prepare_to_wait(&decoder->wq, &wait, TASK_UNINTERRUPTIBLE); |
Mauro Carvalho Chehab | 09df5cbe | 2007-07-17 16:25:38 -0300 | [diff] [blame] | 200 | schedule_timeout(msecs_to_jiffies(250)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | finish_wait(&decoder->wq, &wait); |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 202 | status = saa7110_read(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | if (status & 0x40) { |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 204 | v4l2_dbg(1, debug, sd, "status=0x%02x (no signal)\n", status); |
Hans Verkuil | 416e87a | 2013-05-29 10:18:57 -0300 | [diff] [blame] | 205 | return V4L2_STD_UNKNOWN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | } |
| 207 | if ((status & 3) == 0) { |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 208 | saa7110_write(sd, 0x06, 0x83); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | if (status & 0x20) { |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 210 | v4l2_dbg(1, debug, sd, "status=0x%02x (NTSC/no color)\n", status); |
| 211 | /*saa7110_write(sd,0x2E,0x81);*/ |
Hans Verkuil | 107063c | 2009-02-18 17:26:06 -0300 | [diff] [blame] | 212 | return V4L2_STD_NTSC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 214 | v4l2_dbg(1, debug, sd, "status=0x%02x (PAL/no color)\n", status); |
| 215 | /*saa7110_write(sd,0x2E,0x9A);*/ |
Hans Verkuil | 107063c | 2009-02-18 17:26:06 -0300 | [diff] [blame] | 216 | return V4L2_STD_PAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | } |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 218 | /*saa7110_write(sd,0x06,0x03);*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | if (status & 0x20) { /* 60Hz */ |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 220 | v4l2_dbg(1, debug, sd, "status=0x%02x (NTSC)\n", status); |
| 221 | saa7110_write(sd, 0x0D, 0x86); |
| 222 | saa7110_write(sd, 0x0F, 0x50); |
| 223 | saa7110_write(sd, 0x11, 0x2C); |
| 224 | /*saa7110_write(sd,0x2E,0x81);*/ |
Hans Verkuil | 107063c | 2009-02-18 17:26:06 -0300 | [diff] [blame] | 225 | return V4L2_STD_NTSC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | /* 50Hz -> PAL/SECAM */ |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 229 | saa7110_write(sd, 0x0D, 0x86); |
| 230 | saa7110_write(sd, 0x0F, 0x10); |
| 231 | saa7110_write(sd, 0x11, 0x59); |
| 232 | /*saa7110_write(sd,0x2E,0x9A);*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
| 234 | prepare_to_wait(&decoder->wq, &wait, TASK_UNINTERRUPTIBLE); |
Mauro Carvalho Chehab | 09df5cbe | 2007-07-17 16:25:38 -0300 | [diff] [blame] | 235 | schedule_timeout(msecs_to_jiffies(250)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | finish_wait(&decoder->wq, &wait); |
| 237 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 238 | status = saa7110_read(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | if ((status & 0x03) == 0x01) { |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 240 | v4l2_dbg(1, debug, sd, "status=0x%02x (SECAM)\n", status); |
| 241 | saa7110_write(sd, 0x0D, 0x87); |
Hans Verkuil | 107063c | 2009-02-18 17:26:06 -0300 | [diff] [blame] | 242 | return V4L2_STD_SECAM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 244 | v4l2_dbg(1, debug, sd, "status=0x%02x (PAL)\n", status); |
Hans Verkuil | 107063c | 2009-02-18 17:26:06 -0300 | [diff] [blame] | 245 | return V4L2_STD_PAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 248 | static int saa7110_g_input_status(struct v4l2_subdev *sd, u32 *pstatus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | { |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 250 | struct saa7110 *decoder = to_saa7110(sd); |
| 251 | int res = V4L2_IN_ST_NO_SIGNAL; |
| 252 | int status = saa7110_read(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 254 | v4l2_dbg(1, debug, sd, "status=0x%02x norm=%llx\n", |
Hans Verkuil | cc5cef8 | 2009-03-06 10:15:01 -0300 | [diff] [blame] | 255 | status, (unsigned long long)decoder->norm); |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 256 | if (!(status & 0x40)) |
| 257 | res = 0; |
| 258 | if (!(status & 0x03)) |
| 259 | res |= V4L2_IN_ST_NO_COLOR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 261 | *pstatus = res; |
| 262 | return 0; |
| 263 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 265 | static int saa7110_querystd(struct v4l2_subdev *sd, v4l2_std_id *std) |
| 266 | { |
Hans Verkuil | 416e87a | 2013-05-29 10:18:57 -0300 | [diff] [blame] | 267 | *std &= determine_norm(sd); |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 268 | return 0; |
| 269 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 271 | static int saa7110_s_std(struct v4l2_subdev *sd, v4l2_std_id std) |
| 272 | { |
| 273 | struct saa7110 *decoder = to_saa7110(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 275 | if (decoder->norm != std) { |
| 276 | decoder->norm = std; |
| 277 | /*saa7110_write(sd, 0x06, 0x03);*/ |
| 278 | if (std & V4L2_STD_NTSC) { |
| 279 | saa7110_write(sd, 0x0D, 0x86); |
| 280 | saa7110_write(sd, 0x0F, 0x50); |
| 281 | saa7110_write(sd, 0x11, 0x2C); |
| 282 | /*saa7110_write(sd, 0x2E, 0x81);*/ |
| 283 | v4l2_dbg(1, debug, sd, "switched to NTSC\n"); |
| 284 | } else if (std & V4L2_STD_PAL) { |
| 285 | saa7110_write(sd, 0x0D, 0x86); |
| 286 | saa7110_write(sd, 0x0F, 0x10); |
| 287 | saa7110_write(sd, 0x11, 0x59); |
| 288 | /*saa7110_write(sd, 0x2E, 0x9A);*/ |
| 289 | v4l2_dbg(1, debug, sd, "switched to PAL\n"); |
| 290 | } else if (std & V4L2_STD_SECAM) { |
| 291 | saa7110_write(sd, 0x0D, 0x87); |
| 292 | saa7110_write(sd, 0x0F, 0x10); |
| 293 | saa7110_write(sd, 0x11, 0x59); |
| 294 | /*saa7110_write(sd, 0x2E, 0x9A);*/ |
| 295 | v4l2_dbg(1, debug, sd, "switched to SECAM\n"); |
| 296 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | return -EINVAL; |
| 298 | } |
Hans Verkuil | 85ede69 | 2008-09-07 08:00:32 -0300 | [diff] [blame] | 299 | } |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 300 | return 0; |
| 301 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
Hans Verkuil | 5325b42 | 2009-04-02 11:26:22 -0300 | [diff] [blame] | 303 | static int saa7110_s_routing(struct v4l2_subdev *sd, |
| 304 | u32 input, u32 output, u32 config) |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 305 | { |
| 306 | struct saa7110 *decoder = to_saa7110(sd); |
Hans Verkuil | 107063c | 2009-02-18 17:26:06 -0300 | [diff] [blame] | 307 | |
Roel Kluin | f14a297 | 2009-10-23 07:59:42 -0300 | [diff] [blame] | 308 | if (input >= SAA7110_MAX_INPUT) { |
Hans Verkuil | 5325b42 | 2009-04-02 11:26:22 -0300 | [diff] [blame] | 309 | v4l2_dbg(1, debug, sd, "input=%d not available\n", input); |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 310 | return -EINVAL; |
Hans Verkuil | 107063c | 2009-02-18 17:26:06 -0300 | [diff] [blame] | 311 | } |
Hans Verkuil | 5325b42 | 2009-04-02 11:26:22 -0300 | [diff] [blame] | 312 | if (decoder->input != input) { |
| 313 | saa7110_selmux(sd, input); |
| 314 | v4l2_dbg(1, debug, sd, "switched to input=%d\n", input); |
Hans Verkuil | 107063c | 2009-02-18 17:26:06 -0300 | [diff] [blame] | 315 | } |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 316 | return 0; |
| 317 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 319 | static int saa7110_s_stream(struct v4l2_subdev *sd, int enable) |
| 320 | { |
| 321 | struct saa7110 *decoder = to_saa7110(sd); |
| 322 | |
| 323 | if (decoder->enable != enable) { |
| 324 | decoder->enable = enable; |
| 325 | saa7110_write(sd, 0x0E, enable ? 0x18 : 0x80); |
| 326 | v4l2_dbg(1, debug, sd, "YUV %s\n", enable ? "on" : "off"); |
| 327 | } |
| 328 | return 0; |
| 329 | } |
| 330 | |
Hans Verkuil | 4744ebf | 2010-12-12 08:27:48 -0300 | [diff] [blame] | 331 | static int saa7110_s_ctrl(struct v4l2_ctrl *ctrl) |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 332 | { |
Hans Verkuil | 4744ebf | 2010-12-12 08:27:48 -0300 | [diff] [blame] | 333 | struct v4l2_subdev *sd = to_sd(ctrl); |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 334 | |
| 335 | switch (ctrl->id) { |
| 336 | case V4L2_CID_BRIGHTNESS: |
Hans Verkuil | 4744ebf | 2010-12-12 08:27:48 -0300 | [diff] [blame] | 337 | saa7110_write(sd, 0x19, ctrl->val); |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 338 | break; |
| 339 | case V4L2_CID_CONTRAST: |
Hans Verkuil | 4744ebf | 2010-12-12 08:27:48 -0300 | [diff] [blame] | 340 | saa7110_write(sd, 0x13, ctrl->val); |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 341 | break; |
| 342 | case V4L2_CID_SATURATION: |
Hans Verkuil | 4744ebf | 2010-12-12 08:27:48 -0300 | [diff] [blame] | 343 | saa7110_write(sd, 0x12, ctrl->val); |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 344 | break; |
| 345 | case V4L2_CID_HUE: |
Hans Verkuil | 4744ebf | 2010-12-12 08:27:48 -0300 | [diff] [blame] | 346 | saa7110_write(sd, 0x07, ctrl->val); |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 347 | break; |
| 348 | default: |
| 349 | return -EINVAL; |
| 350 | } |
| 351 | return 0; |
| 352 | } |
| 353 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | /* ----------------------------------------------------------------------- */ |
| 355 | |
Hans Verkuil | 4744ebf | 2010-12-12 08:27:48 -0300 | [diff] [blame] | 356 | static const struct v4l2_ctrl_ops saa7110_ctrl_ops = { |
| 357 | .s_ctrl = saa7110_s_ctrl, |
| 358 | }; |
| 359 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 360 | static const struct v4l2_subdev_video_ops saa7110_video_ops = { |
Laurent Pinchart | 8774bed | 2014-04-28 16:53:01 -0300 | [diff] [blame] | 361 | .s_std = saa7110_s_std, |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 362 | .s_routing = saa7110_s_routing, |
| 363 | .s_stream = saa7110_s_stream, |
| 364 | .querystd = saa7110_querystd, |
| 365 | .g_input_status = saa7110_g_input_status, |
| 366 | }; |
| 367 | |
| 368 | static const struct v4l2_subdev_ops saa7110_ops = { |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 369 | .video = &saa7110_video_ops, |
| 370 | }; |
| 371 | |
| 372 | /* ----------------------------------------------------------------------- */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
Hans Verkuil | 85ede69 | 2008-09-07 08:00:32 -0300 | [diff] [blame] | 374 | static int saa7110_probe(struct i2c_client *client, |
| 375 | const struct i2c_device_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | struct saa7110 *decoder; |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 378 | struct v4l2_subdev *sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | int rv; |
| 380 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | /* Check if the adapter supports the needed features */ |
Hans Verkuil | 85ede69 | 2008-09-07 08:00:32 -0300 | [diff] [blame] | 382 | if (!i2c_check_functionality(client->adapter, |
| 383 | I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) |
| 384 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | |
Hans Verkuil | 85ede69 | 2008-09-07 08:00:32 -0300 | [diff] [blame] | 386 | v4l_info(client, "chip found @ 0x%x (%s)\n", |
| 387 | client->addr << 1, client->adapter->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
Laurent Pinchart | c02b211 | 2013-05-02 08:29:43 -0300 | [diff] [blame] | 389 | decoder = devm_kzalloc(&client->dev, sizeof(*decoder), GFP_KERNEL); |
Hans Verkuil | 85ede69 | 2008-09-07 08:00:32 -0300 | [diff] [blame] | 390 | if (!decoder) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | return -ENOMEM; |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 392 | sd = &decoder->sd; |
| 393 | v4l2_i2c_subdev_init(sd, client, &saa7110_ops); |
Hans Verkuil | 107063c | 2009-02-18 17:26:06 -0300 | [diff] [blame] | 394 | decoder->norm = V4L2_STD_PAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | decoder->input = 0; |
| 396 | decoder->enable = 1; |
Hans Verkuil | 4744ebf | 2010-12-12 08:27:48 -0300 | [diff] [blame] | 397 | v4l2_ctrl_handler_init(&decoder->hdl, 2); |
| 398 | v4l2_ctrl_new_std(&decoder->hdl, &saa7110_ctrl_ops, |
| 399 | V4L2_CID_BRIGHTNESS, 0, 255, 1, 128); |
| 400 | v4l2_ctrl_new_std(&decoder->hdl, &saa7110_ctrl_ops, |
| 401 | V4L2_CID_CONTRAST, 0, 127, 1, 64); |
| 402 | v4l2_ctrl_new_std(&decoder->hdl, &saa7110_ctrl_ops, |
| 403 | V4L2_CID_SATURATION, 0, 127, 1, 64); |
| 404 | v4l2_ctrl_new_std(&decoder->hdl, &saa7110_ctrl_ops, |
| 405 | V4L2_CID_HUE, -128, 127, 1, 0); |
| 406 | sd->ctrl_handler = &decoder->hdl; |
| 407 | if (decoder->hdl.error) { |
| 408 | int err = decoder->hdl.error; |
| 409 | |
| 410 | v4l2_ctrl_handler_free(&decoder->hdl); |
Hans Verkuil | 4744ebf | 2010-12-12 08:27:48 -0300 | [diff] [blame] | 411 | return err; |
| 412 | } |
| 413 | v4l2_ctrl_handler_setup(&decoder->hdl); |
| 414 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | init_waitqueue_head(&decoder->wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 417 | rv = saa7110_write_block(sd, initseq, sizeof(initseq)); |
Hans Verkuil | 85ede69 | 2008-09-07 08:00:32 -0300 | [diff] [blame] | 418 | if (rv < 0) { |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 419 | v4l2_dbg(1, debug, sd, "init status %d\n", rv); |
Hans Verkuil | 85ede69 | 2008-09-07 08:00:32 -0300 | [diff] [blame] | 420 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | int ver, status; |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 422 | saa7110_write(sd, 0x21, 0x10); |
| 423 | saa7110_write(sd, 0x0e, 0x18); |
| 424 | saa7110_write(sd, 0x0D, 0x04); |
| 425 | ver = saa7110_read(sd); |
| 426 | saa7110_write(sd, 0x0D, 0x06); |
| 427 | /*mdelay(150);*/ |
| 428 | status = saa7110_read(sd); |
| 429 | v4l2_dbg(1, debug, sd, "version %x, status=0x%02x\n", |
Hans Verkuil | 85ede69 | 2008-09-07 08:00:32 -0300 | [diff] [blame] | 430 | ver, status); |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 431 | saa7110_write(sd, 0x0D, 0x86); |
| 432 | saa7110_write(sd, 0x0F, 0x10); |
| 433 | saa7110_write(sd, 0x11, 0x59); |
| 434 | /*saa7110_write(sd, 0x2E, 0x9A);*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 437 | /*saa7110_selmux(sd,0);*/ |
| 438 | /*determine_norm(sd);*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | /* setup and implicit mode 0 select has been performed */ |
| 440 | |
| 441 | return 0; |
| 442 | } |
| 443 | |
Hans Verkuil | 85ede69 | 2008-09-07 08:00:32 -0300 | [diff] [blame] | 444 | static int saa7110_remove(struct i2c_client *client) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | { |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 446 | struct v4l2_subdev *sd = i2c_get_clientdata(client); |
Hans Verkuil | 4744ebf | 2010-12-12 08:27:48 -0300 | [diff] [blame] | 447 | struct saa7110 *decoder = to_saa7110(sd); |
Hans Verkuil | e794684 | 2009-02-19 12:24:27 -0300 | [diff] [blame] | 448 | |
| 449 | v4l2_device_unregister_subdev(sd); |
Hans Verkuil | 4744ebf | 2010-12-12 08:27:48 -0300 | [diff] [blame] | 450 | v4l2_ctrl_handler_free(&decoder->hdl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | /* ----------------------------------------------------------------------- */ |
| 455 | |
Hans Verkuil | 85ede69 | 2008-09-07 08:00:32 -0300 | [diff] [blame] | 456 | static const struct i2c_device_id saa7110_id[] = { |
| 457 | { "saa7110", 0 }, |
| 458 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | }; |
Hans Verkuil | 85ede69 | 2008-09-07 08:00:32 -0300 | [diff] [blame] | 460 | MODULE_DEVICE_TABLE(i2c, saa7110_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | |
Hans Verkuil | ff6e542 | 2010-09-15 15:38:54 -0300 | [diff] [blame] | 462 | static struct i2c_driver saa7110_driver = { |
| 463 | .driver = { |
Hans Verkuil | ff6e542 | 2010-09-15 15:38:54 -0300 | [diff] [blame] | 464 | .name = "saa7110", |
| 465 | }, |
| 466 | .probe = saa7110_probe, |
| 467 | .remove = saa7110_remove, |
| 468 | .id_table = saa7110_id, |
Hans Verkuil | 85ede69 | 2008-09-07 08:00:32 -0300 | [diff] [blame] | 469 | }; |
Hans Verkuil | ff6e542 | 2010-09-15 15:38:54 -0300 | [diff] [blame] | 470 | |
Axel Lin | c6e8d86 | 2012-02-12 06:56:32 -0300 | [diff] [blame] | 471 | module_i2c_driver(saa7110_driver); |