Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005-2006 Micronas USA Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License (Version 2) as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software Foundation, |
| 15 | * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/init.h> |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 20 | #include <linux/i2c.h> |
Ross Cohen | df20d69 | 2008-09-29 22:36:24 -0400 | [diff] [blame] | 21 | #include <linux/videodev2.h> |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 22 | #include <linux/ioctl.h> |
| 23 | |
| 24 | #include "wis-i2c.h" |
| 25 | |
| 26 | struct wis_tw9903 { |
| 27 | int norm; |
| 28 | int brightness; |
| 29 | int contrast; |
| 30 | int hue; |
| 31 | }; |
| 32 | |
| 33 | static u8 initial_registers[] = |
| 34 | { |
| 35 | 0x02, 0x44, /* input 1, composite */ |
| 36 | 0x03, 0x92, /* correct digital format */ |
| 37 | 0x04, 0x00, |
| 38 | 0x05, 0x80, /* or 0x00 for PAL */ |
| 39 | 0x06, 0x40, /* second internal current reference */ |
| 40 | 0x07, 0x02, /* window */ |
| 41 | 0x08, 0x14, /* window */ |
| 42 | 0x09, 0xf0, /* window */ |
| 43 | 0x0a, 0x81, /* window */ |
| 44 | 0x0b, 0xd0, /* window */ |
| 45 | 0x0c, 0x8c, |
| 46 | 0x0d, 0x00, /* scaling */ |
| 47 | 0x0e, 0x11, /* scaling */ |
| 48 | 0x0f, 0x00, /* scaling */ |
| 49 | 0x10, 0x00, /* brightness */ |
| 50 | 0x11, 0x60, /* contrast */ |
| 51 | 0x12, 0x01, /* sharpness */ |
| 52 | 0x13, 0x7f, /* U gain */ |
| 53 | 0x14, 0x5a, /* V gain */ |
| 54 | 0x15, 0x00, /* hue */ |
| 55 | 0x16, 0xc3, /* sharpness */ |
| 56 | 0x18, 0x00, |
| 57 | 0x19, 0x58, /* vbi */ |
| 58 | 0x1a, 0x80, |
| 59 | 0x1c, 0x0f, /* video norm */ |
| 60 | 0x1d, 0x7f, /* video norm */ |
| 61 | 0x20, 0xa0, /* clamping gain (working 0x50) */ |
| 62 | 0x21, 0x22, |
| 63 | 0x22, 0xf0, |
| 64 | 0x23, 0xfe, |
| 65 | 0x24, 0x3c, |
| 66 | 0x25, 0x38, |
| 67 | 0x26, 0x44, |
| 68 | 0x27, 0x20, |
| 69 | 0x28, 0x00, |
| 70 | 0x29, 0x15, |
| 71 | 0x2a, 0xa0, |
| 72 | 0x2b, 0x44, |
| 73 | 0x2c, 0x37, |
| 74 | 0x2d, 0x00, |
| 75 | 0x2e, 0xa5, /* burst PLL control (working: a9) */ |
| 76 | 0x2f, 0xe0, /* 0xea is blue test frame -- 0xe0 for normal */ |
| 77 | 0x31, 0x00, |
| 78 | 0x33, 0x22, |
| 79 | 0x34, 0x11, |
| 80 | 0x35, 0x35, |
| 81 | 0x3b, 0x05, |
| 82 | 0x06, 0xc0, /* reset device */ |
| 83 | 0x00, 0x00, /* Terminator (reg 0x00 is read-only) */ |
| 84 | }; |
| 85 | |
| 86 | static int write_reg(struct i2c_client *client, u8 reg, u8 value) |
| 87 | { |
| 88 | return i2c_smbus_write_byte_data(client, reg, value); |
| 89 | } |
| 90 | |
| 91 | static int write_regs(struct i2c_client *client, u8 *regs) |
| 92 | { |
| 93 | int i; |
| 94 | |
| 95 | for (i = 0; regs[i] != 0x00; i += 2) |
| 96 | if (i2c_smbus_write_byte_data(client, regs[i], regs[i + 1]) < 0) |
| 97 | return -1; |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static int wis_tw9903_command(struct i2c_client *client, |
| 102 | unsigned int cmd, void *arg) |
| 103 | { |
| 104 | struct wis_tw9903 *dec = i2c_get_clientdata(client); |
| 105 | |
| 106 | switch (cmd) { |
Ross Cohen | df20d69 | 2008-09-29 22:36:24 -0400 | [diff] [blame] | 107 | case VIDIOC_S_INPUT: |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 108 | { |
| 109 | int *input = arg; |
| 110 | |
| 111 | i2c_smbus_write_byte_data(client, 0x02, 0x40 | (*input << 1)); |
| 112 | break; |
| 113 | } |
| 114 | #if 0 /* The scaler on this thing seems to be horribly broken */ |
| 115 | case DECODER_SET_RESOLUTION: |
| 116 | { |
| 117 | struct video_decoder_resolution *res = arg; |
| 118 | /*int hscale = 256 * 720 / res->width;*/ |
| 119 | int hscale = 256 * 720 / (res->width - (res->width > 704 ? 0 : 8)); |
Ross Cohen | df20d69 | 2008-09-29 22:36:24 -0400 | [diff] [blame] | 120 | int vscale = 256 * (dec->norm & V4L2_STD_NTSC ? 240 : 288) |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 121 | / res->height; |
| 122 | u8 regs[] = { |
| 123 | 0x0d, vscale & 0xff, |
| 124 | 0x0f, hscale & 0xff, |
| 125 | 0x0e, ((vscale & 0xf00) >> 4) | ((hscale & 0xf00) >> 8), |
| 126 | 0x06, 0xc0, /* reset device */ |
| 127 | 0, 0, |
| 128 | }; |
| 129 | printk(KERN_DEBUG "vscale is %04x, hscale is %04x\n", |
| 130 | vscale, hscale); |
| 131 | /*write_regs(client, regs);*/ |
| 132 | break; |
| 133 | } |
| 134 | #endif |
Ross Cohen | df20d69 | 2008-09-29 22:36:24 -0400 | [diff] [blame] | 135 | case VIDIOC_S_STD: |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 136 | { |
Ross Cohen | df20d69 | 2008-09-29 22:36:24 -0400 | [diff] [blame] | 137 | v4l2_std_id *input = arg; |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 138 | u8 regs[] = { |
Ross Cohen | df20d69 | 2008-09-29 22:36:24 -0400 | [diff] [blame] | 139 | 0x05, *input & V4L2_STD_NTSC ? 0x80 : 0x00, |
| 140 | 0x07, *input & V4L2_STD_NTSC ? 0x02 : 0x12, |
| 141 | 0x08, *input & V4L2_STD_NTSC ? 0x14 : 0x18, |
| 142 | 0x09, *input & V4L2_STD_NTSC ? 0xf0 : 0x20, |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 143 | 0, 0, |
| 144 | }; |
| 145 | write_regs(client, regs); |
| 146 | dec->norm = *input; |
| 147 | break; |
| 148 | } |
| 149 | case VIDIOC_QUERYCTRL: |
| 150 | { |
| 151 | struct v4l2_queryctrl *ctrl = arg; |
| 152 | |
| 153 | switch (ctrl->id) { |
| 154 | case V4L2_CID_BRIGHTNESS: |
| 155 | ctrl->type = V4L2_CTRL_TYPE_INTEGER; |
| 156 | strncpy(ctrl->name, "Brightness", sizeof(ctrl->name)); |
| 157 | ctrl->minimum = -128; |
| 158 | ctrl->maximum = 127; |
| 159 | ctrl->step = 1; |
| 160 | ctrl->default_value = 0x00; |
| 161 | ctrl->flags = 0; |
| 162 | break; |
| 163 | case V4L2_CID_CONTRAST: |
| 164 | ctrl->type = V4L2_CTRL_TYPE_INTEGER; |
| 165 | strncpy(ctrl->name, "Contrast", sizeof(ctrl->name)); |
| 166 | ctrl->minimum = 0; |
| 167 | ctrl->maximum = 255; |
| 168 | ctrl->step = 1; |
| 169 | ctrl->default_value = 0x60; |
| 170 | ctrl->flags = 0; |
| 171 | break; |
| 172 | #if 0 |
| 173 | /* I don't understand how the Chroma Gain registers work... */ |
| 174 | case V4L2_CID_SATURATION: |
| 175 | ctrl->type = V4L2_CTRL_TYPE_INTEGER; |
| 176 | strncpy(ctrl->name, "Saturation", sizeof(ctrl->name)); |
| 177 | ctrl->minimum = 0; |
| 178 | ctrl->maximum = 127; |
| 179 | ctrl->step = 1; |
| 180 | ctrl->default_value = 64; |
| 181 | ctrl->flags = 0; |
| 182 | break; |
| 183 | #endif |
| 184 | case V4L2_CID_HUE: |
| 185 | ctrl->type = V4L2_CTRL_TYPE_INTEGER; |
| 186 | strncpy(ctrl->name, "Hue", sizeof(ctrl->name)); |
| 187 | ctrl->minimum = -128; |
| 188 | ctrl->maximum = 127; |
| 189 | ctrl->step = 1; |
| 190 | ctrl->default_value = 0; |
| 191 | ctrl->flags = 0; |
| 192 | break; |
| 193 | } |
| 194 | break; |
| 195 | } |
| 196 | case VIDIOC_S_CTRL: |
| 197 | { |
| 198 | struct v4l2_control *ctrl = arg; |
| 199 | |
| 200 | switch (ctrl->id) { |
| 201 | case V4L2_CID_BRIGHTNESS: |
| 202 | if (ctrl->value > 127) |
| 203 | dec->brightness = 127; |
| 204 | else if (ctrl->value < -128) |
| 205 | dec->brightness = -128; |
| 206 | else |
| 207 | dec->brightness = ctrl->value; |
| 208 | write_reg(client, 0x10, dec->brightness); |
| 209 | break; |
| 210 | case V4L2_CID_CONTRAST: |
| 211 | if (ctrl->value > 255) |
| 212 | dec->contrast = 255; |
| 213 | else if (ctrl->value < 0) |
| 214 | dec->contrast = 0; |
| 215 | else |
| 216 | dec->contrast = ctrl->value; |
| 217 | write_reg(client, 0x11, dec->contrast); |
| 218 | break; |
| 219 | #if 0 |
| 220 | case V4L2_CID_SATURATION: |
| 221 | if (ctrl->value > 127) |
| 222 | dec->saturation = 127; |
| 223 | else if (ctrl->value < 0) |
| 224 | dec->saturation = 0; |
| 225 | else |
| 226 | dec->saturation = ctrl->value; |
| 227 | /*write_reg(client, 0x0c, dec->saturation);*/ |
| 228 | break; |
| 229 | #endif |
| 230 | case V4L2_CID_HUE: |
| 231 | if (ctrl->value > 127) |
| 232 | dec->hue = 127; |
| 233 | else if (ctrl->value < -128) |
| 234 | dec->hue = -128; |
| 235 | else |
| 236 | dec->hue = ctrl->value; |
| 237 | write_reg(client, 0x15, dec->hue); |
| 238 | break; |
| 239 | } |
| 240 | break; |
| 241 | } |
| 242 | case VIDIOC_G_CTRL: |
| 243 | { |
| 244 | struct v4l2_control *ctrl = arg; |
| 245 | |
| 246 | switch (ctrl->id) { |
| 247 | case V4L2_CID_BRIGHTNESS: |
| 248 | ctrl->value = dec->brightness; |
| 249 | break; |
| 250 | case V4L2_CID_CONTRAST: |
| 251 | ctrl->value = dec->contrast; |
| 252 | break; |
| 253 | #if 0 |
| 254 | case V4L2_CID_SATURATION: |
| 255 | ctrl->value = dec->saturation; |
| 256 | break; |
| 257 | #endif |
| 258 | case V4L2_CID_HUE: |
| 259 | ctrl->value = dec->hue; |
| 260 | break; |
| 261 | } |
| 262 | break; |
| 263 | } |
| 264 | default: |
| 265 | break; |
| 266 | } |
| 267 | return 0; |
| 268 | } |
| 269 | |
Jean Delvare | 7400516 | 2009-04-21 21:47:22 +0200 | [diff] [blame] | 270 | static int wis_tw9903_probe(struct i2c_client *client, |
| 271 | const struct i2c_device_id *id) |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 272 | { |
Jean Delvare | 7400516 | 2009-04-21 21:47:22 +0200 | [diff] [blame] | 273 | struct i2c_adapter *adapter = client->adapter; |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 274 | struct wis_tw9903 *dec; |
| 275 | |
| 276 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
Jean Delvare | 7400516 | 2009-04-21 21:47:22 +0200 | [diff] [blame] | 277 | return -ENODEV; |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 278 | |
| 279 | dec = kmalloc(sizeof(struct wis_tw9903), GFP_KERNEL); |
Jean Delvare | 7400516 | 2009-04-21 21:47:22 +0200 | [diff] [blame] | 280 | if (dec == NULL) |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 281 | return -ENOMEM; |
Jean Delvare | 7400516 | 2009-04-21 21:47:22 +0200 | [diff] [blame] | 282 | |
Ross Cohen | df20d69 | 2008-09-29 22:36:24 -0400 | [diff] [blame] | 283 | dec->norm = V4L2_STD_NTSC; |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 284 | dec->brightness = 0; |
| 285 | dec->contrast = 0x60; |
| 286 | dec->hue = 0; |
| 287 | i2c_set_clientdata(client, dec); |
| 288 | |
| 289 | printk(KERN_DEBUG |
| 290 | "wis-tw9903: initializing TW9903 at address %d on %s\n", |
Jean Delvare | 7400516 | 2009-04-21 21:47:22 +0200 | [diff] [blame] | 291 | client->addr, adapter->name); |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 292 | |
| 293 | if (write_regs(client, initial_registers) < 0) { |
| 294 | printk(KERN_ERR "wis-tw9903: error initializing TW9903\n"); |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 295 | kfree(dec); |
Jean Delvare | 7400516 | 2009-04-21 21:47:22 +0200 | [diff] [blame] | 296 | return -ENODEV; |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 299 | return 0; |
| 300 | } |
| 301 | |
Jean Delvare | 7400516 | 2009-04-21 21:47:22 +0200 | [diff] [blame] | 302 | static int wis_tw9903_remove(struct i2c_client *client) |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 303 | { |
| 304 | struct wis_tw9903 *dec = i2c_get_clientdata(client); |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 305 | |
Jean Delvare | 7400516 | 2009-04-21 21:47:22 +0200 | [diff] [blame] | 306 | i2c_set_clientdata(client, NULL); |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 307 | kfree(dec); |
| 308 | return 0; |
| 309 | } |
| 310 | |
Jean Delvare | 7400516 | 2009-04-21 21:47:22 +0200 | [diff] [blame] | 311 | static struct i2c_device_id wis_tw9903_id[] = { |
| 312 | { "wis_tw9903", 0 }, |
| 313 | { } |
| 314 | }; |
| 315 | |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 316 | static struct i2c_driver wis_tw9903_driver = { |
| 317 | .driver = { |
| 318 | .name = "WIS TW9903 I2C driver", |
| 319 | }, |
Jean Delvare | 7400516 | 2009-04-21 21:47:22 +0200 | [diff] [blame] | 320 | .probe = wis_tw9903_probe, |
| 321 | .remove = wis_tw9903_remove, |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 322 | .command = wis_tw9903_command, |
Jean Delvare | 7400516 | 2009-04-21 21:47:22 +0200 | [diff] [blame] | 323 | .id_table = wis_tw9903_id, |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 324 | }; |
| 325 | |
| 326 | static int __init wis_tw9903_init(void) |
| 327 | { |
Jean Delvare | 7400516 | 2009-04-21 21:47:22 +0200 | [diff] [blame] | 328 | return i2c_add_driver(&wis_tw9903_driver); |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | static void __exit wis_tw9903_cleanup(void) |
| 332 | { |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 333 | i2c_del_driver(&wis_tw9903_driver); |
| 334 | } |
| 335 | |
| 336 | module_init(wis_tw9903_init); |
| 337 | module_exit(wis_tw9903_cleanup); |
| 338 | |
| 339 | MODULE_LICENSE("GPL v2"); |