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