blob: 1cdf01a8b338aa032eaa108ea45ed281e401d1b7 [file] [log] [blame]
Greg Kroah-Hartman866b8692008-02-15 16:53:09 -08001/*
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>
20#include <linux/version.h>
21#include <linux/i2c.h>
22#include <linux/videodev.h>
23#include <linux/video_decoder.h>
24#include <linux/ioctl.h>
25
26#include "wis-i2c.h"
27
28struct wis_tw9903 {
29 int norm;
30 int brightness;
31 int contrast;
32 int hue;
33};
34
35static u8 initial_registers[] =
36{
37 0x02, 0x44, /* input 1, composite */
38 0x03, 0x92, /* correct digital format */
39 0x04, 0x00,
40 0x05, 0x80, /* or 0x00 for PAL */
41 0x06, 0x40, /* second internal current reference */
42 0x07, 0x02, /* window */
43 0x08, 0x14, /* window */
44 0x09, 0xf0, /* window */
45 0x0a, 0x81, /* window */
46 0x0b, 0xd0, /* window */
47 0x0c, 0x8c,
48 0x0d, 0x00, /* scaling */
49 0x0e, 0x11, /* scaling */
50 0x0f, 0x00, /* scaling */
51 0x10, 0x00, /* brightness */
52 0x11, 0x60, /* contrast */
53 0x12, 0x01, /* sharpness */
54 0x13, 0x7f, /* U gain */
55 0x14, 0x5a, /* V gain */
56 0x15, 0x00, /* hue */
57 0x16, 0xc3, /* sharpness */
58 0x18, 0x00,
59 0x19, 0x58, /* vbi */
60 0x1a, 0x80,
61 0x1c, 0x0f, /* video norm */
62 0x1d, 0x7f, /* video norm */
63 0x20, 0xa0, /* clamping gain (working 0x50) */
64 0x21, 0x22,
65 0x22, 0xf0,
66 0x23, 0xfe,
67 0x24, 0x3c,
68 0x25, 0x38,
69 0x26, 0x44,
70 0x27, 0x20,
71 0x28, 0x00,
72 0x29, 0x15,
73 0x2a, 0xa0,
74 0x2b, 0x44,
75 0x2c, 0x37,
76 0x2d, 0x00,
77 0x2e, 0xa5, /* burst PLL control (working: a9) */
78 0x2f, 0xe0, /* 0xea is blue test frame -- 0xe0 for normal */
79 0x31, 0x00,
80 0x33, 0x22,
81 0x34, 0x11,
82 0x35, 0x35,
83 0x3b, 0x05,
84 0x06, 0xc0, /* reset device */
85 0x00, 0x00, /* Terminator (reg 0x00 is read-only) */
86};
87
88static int write_reg(struct i2c_client *client, u8 reg, u8 value)
89{
90 return i2c_smbus_write_byte_data(client, reg, value);
91}
92
93static int write_regs(struct i2c_client *client, u8 *regs)
94{
95 int i;
96
97 for (i = 0; regs[i] != 0x00; i += 2)
98 if (i2c_smbus_write_byte_data(client, regs[i], regs[i + 1]) < 0)
99 return -1;
100 return 0;
101}
102
103static int wis_tw9903_command(struct i2c_client *client,
104 unsigned int cmd, void *arg)
105{
106 struct wis_tw9903 *dec = i2c_get_clientdata(client);
107
108 switch (cmd) {
109 case DECODER_SET_INPUT:
110 {
111 int *input = arg;
112
113 i2c_smbus_write_byte_data(client, 0x02, 0x40 | (*input << 1));
114 break;
115 }
116#if 0 /* The scaler on this thing seems to be horribly broken */
117 case DECODER_SET_RESOLUTION:
118 {
119 struct video_decoder_resolution *res = arg;
120 /*int hscale = 256 * 720 / res->width;*/
121 int hscale = 256 * 720 / (res->width - (res->width > 704 ? 0 : 8));
122 int vscale = 256 * (dec->norm == VIDEO_MODE_NTSC ? 240 : 288)
123 / res->height;
124 u8 regs[] = {
125 0x0d, vscale & 0xff,
126 0x0f, hscale & 0xff,
127 0x0e, ((vscale & 0xf00) >> 4) | ((hscale & 0xf00) >> 8),
128 0x06, 0xc0, /* reset device */
129 0, 0,
130 };
131 printk(KERN_DEBUG "vscale is %04x, hscale is %04x\n",
132 vscale, hscale);
133 /*write_regs(client, regs);*/
134 break;
135 }
136#endif
137 case DECODER_SET_NORM:
138 {
139 int *input = arg;
140 u8 regs[] = {
141 0x05, *input == VIDEO_MODE_NTSC ? 0x80 : 0x00,
142 0x07, *input == VIDEO_MODE_NTSC ? 0x02 : 0x12,
143 0x08, *input == VIDEO_MODE_NTSC ? 0x14 : 0x18,
144 0x09, *input == VIDEO_MODE_NTSC ? 0xf0 : 0x20,
145 0, 0,
146 };
147 write_regs(client, regs);
148 dec->norm = *input;
149 break;
150 }
151 case VIDIOC_QUERYCTRL:
152 {
153 struct v4l2_queryctrl *ctrl = arg;
154
155 switch (ctrl->id) {
156 case V4L2_CID_BRIGHTNESS:
157 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
158 strncpy(ctrl->name, "Brightness", sizeof(ctrl->name));
159 ctrl->minimum = -128;
160 ctrl->maximum = 127;
161 ctrl->step = 1;
162 ctrl->default_value = 0x00;
163 ctrl->flags = 0;
164 break;
165 case V4L2_CID_CONTRAST:
166 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
167 strncpy(ctrl->name, "Contrast", sizeof(ctrl->name));
168 ctrl->minimum = 0;
169 ctrl->maximum = 255;
170 ctrl->step = 1;
171 ctrl->default_value = 0x60;
172 ctrl->flags = 0;
173 break;
174#if 0
175 /* I don't understand how the Chroma Gain registers work... */
176 case V4L2_CID_SATURATION:
177 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
178 strncpy(ctrl->name, "Saturation", sizeof(ctrl->name));
179 ctrl->minimum = 0;
180 ctrl->maximum = 127;
181 ctrl->step = 1;
182 ctrl->default_value = 64;
183 ctrl->flags = 0;
184 break;
185#endif
186 case V4L2_CID_HUE:
187 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
188 strncpy(ctrl->name, "Hue", sizeof(ctrl->name));
189 ctrl->minimum = -128;
190 ctrl->maximum = 127;
191 ctrl->step = 1;
192 ctrl->default_value = 0;
193 ctrl->flags = 0;
194 break;
195 }
196 break;
197 }
198 case VIDIOC_S_CTRL:
199 {
200 struct v4l2_control *ctrl = arg;
201
202 switch (ctrl->id) {
203 case V4L2_CID_BRIGHTNESS:
204 if (ctrl->value > 127)
205 dec->brightness = 127;
206 else if (ctrl->value < -128)
207 dec->brightness = -128;
208 else
209 dec->brightness = ctrl->value;
210 write_reg(client, 0x10, dec->brightness);
211 break;
212 case V4L2_CID_CONTRAST:
213 if (ctrl->value > 255)
214 dec->contrast = 255;
215 else if (ctrl->value < 0)
216 dec->contrast = 0;
217 else
218 dec->contrast = ctrl->value;
219 write_reg(client, 0x11, dec->contrast);
220 break;
221#if 0
222 case V4L2_CID_SATURATION:
223 if (ctrl->value > 127)
224 dec->saturation = 127;
225 else if (ctrl->value < 0)
226 dec->saturation = 0;
227 else
228 dec->saturation = ctrl->value;
229 /*write_reg(client, 0x0c, dec->saturation);*/
230 break;
231#endif
232 case V4L2_CID_HUE:
233 if (ctrl->value > 127)
234 dec->hue = 127;
235 else if (ctrl->value < -128)
236 dec->hue = -128;
237 else
238 dec->hue = ctrl->value;
239 write_reg(client, 0x15, dec->hue);
240 break;
241 }
242 break;
243 }
244 case VIDIOC_G_CTRL:
245 {
246 struct v4l2_control *ctrl = arg;
247
248 switch (ctrl->id) {
249 case V4L2_CID_BRIGHTNESS:
250 ctrl->value = dec->brightness;
251 break;
252 case V4L2_CID_CONTRAST:
253 ctrl->value = dec->contrast;
254 break;
255#if 0
256 case V4L2_CID_SATURATION:
257 ctrl->value = dec->saturation;
258 break;
259#endif
260 case V4L2_CID_HUE:
261 ctrl->value = dec->hue;
262 break;
263 }
264 break;
265 }
266 default:
267 break;
268 }
269 return 0;
270}
271
272static struct i2c_driver wis_tw9903_driver;
273
274static struct i2c_client wis_tw9903_client_templ = {
275 .name = "TW9903 (WIS)",
276 .driver = &wis_tw9903_driver,
277};
278
279static int wis_tw9903_detect(struct i2c_adapter *adapter, int addr, int kind)
280{
281 struct i2c_client *client;
282 struct wis_tw9903 *dec;
283
284 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
285 return 0;
286
287 client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
288 if (client == NULL)
289 return -ENOMEM;
290 memcpy(client, &wis_tw9903_client_templ,
291 sizeof(wis_tw9903_client_templ));
292 client->adapter = adapter;
293 client->addr = addr;
294
295 dec = kmalloc(sizeof(struct wis_tw9903), GFP_KERNEL);
296 if (dec == NULL) {
297 kfree(client);
298 return -ENOMEM;
299 }
300 dec->norm = VIDEO_MODE_NTSC;
301 dec->brightness = 0;
302 dec->contrast = 0x60;
303 dec->hue = 0;
304 i2c_set_clientdata(client, dec);
305
306 printk(KERN_DEBUG
307 "wis-tw9903: initializing TW9903 at address %d on %s\n",
308 addr, adapter->name);
309
310 if (write_regs(client, initial_registers) < 0) {
311 printk(KERN_ERR "wis-tw9903: error initializing TW9903\n");
312 kfree(client);
313 kfree(dec);
314 return 0;
315 }
316
317 i2c_attach_client(client);
318 return 0;
319}
320
321static int wis_tw9903_detach(struct i2c_client *client)
322{
323 struct wis_tw9903 *dec = i2c_get_clientdata(client);
324 int r;
325
326 r = i2c_detach_client(client);
327 if (r < 0)
328 return r;
329
330 kfree(client);
331 kfree(dec);
332 return 0;
333}
334
335static struct i2c_driver wis_tw9903_driver = {
336 .driver = {
337 .name = "WIS TW9903 I2C driver",
338 },
339 .id = I2C_DRIVERID_WIS_TW9903,
340 .detach_client = wis_tw9903_detach,
341 .command = wis_tw9903_command,
342};
343
344static int __init wis_tw9903_init(void)
345{
346 int r;
347
348 r = i2c_add_driver(&wis_tw9903_driver);
349 if (r < 0)
350 return r;
351 return wis_i2c_add_driver(wis_tw9903_driver.id, wis_tw9903_detect);
352}
353
354static void __exit wis_tw9903_cleanup(void)
355{
356 wis_i2c_del_driver(wis_tw9903_detect);
357 i2c_del_driver(&wis_tw9903_driver);
358}
359
360module_init(wis_tw9903_init);
361module_exit(wis_tw9903_cleanup);
362
363MODULE_LICENSE("GPL v2");