blob: 69ed7bf032277dda2b68d01c7ced238604e36762 [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_tw2804 {
29 int channel;
30 int norm;
31 int brightness;
32 int contrast;
33 int saturation;
34 int hue;
35};
36
37static u8 global_registers[] =
38{
39 0x39, 0x00,
40 0x3a, 0xff,
41 0x3b, 0x84,
42 0x3c, 0x80,
43 0x3d, 0x80,
44 0x3e, 0x82,
45 0x3f, 0x82,
46 0xff, 0xff, /* Terminator (reg 0xff does not exist) */
47};
48
49static u8 channel_registers[] =
50{
51 0x01, 0xc4,
52 0x02, 0xa5,
53 0x03, 0x20,
54 0x04, 0xd0,
55 0x05, 0x20,
56 0x06, 0xd0,
57 0x07, 0x88,
58 0x08, 0x20,
59 0x09, 0x07,
60 0x0a, 0xf0,
61 0x0b, 0x07,
62 0x0c, 0xf0,
63 0x0d, 0x40,
64 0x0e, 0xd2,
65 0x0f, 0x80,
66 0x10, 0x80,
67 0x11, 0x80,
68 0x12, 0x80,
69 0x13, 0x1f,
70 0x14, 0x00,
71 0x15, 0x00,
72 0x16, 0x00,
73 0x17, 0x00,
74 0x18, 0xff,
75 0x19, 0xff,
76 0x1a, 0xff,
77 0x1b, 0xff,
78 0x1c, 0xff,
79 0x1d, 0xff,
80 0x1e, 0xff,
81 0x1f, 0xff,
82 0x20, 0x07,
83 0x21, 0x07,
84 0x22, 0x00,
85 0x23, 0x91,
86 0x24, 0x51,
87 0x25, 0x03,
88 0x26, 0x00,
89 0x27, 0x00,
90 0x28, 0x00,
91 0x29, 0x00,
92 0x2a, 0x00,
93 0x2b, 0x00,
94 0x2c, 0x00,
95 0x2d, 0x00,
96 0x2e, 0x00,
97 0x2f, 0x00,
98 0x30, 0x00,
99 0x31, 0x00,
100 0x32, 0x00,
101 0x33, 0x00,
102 0x34, 0x00,
103 0x35, 0x00,
104 0x36, 0x00,
105 0x37, 0x00,
106 0xff, 0xff, /* Terminator (reg 0xff does not exist) */
107};
108
109static int write_reg(struct i2c_client *client, u8 reg, u8 value, int channel)
110{
111 return i2c_smbus_write_byte_data(client, reg | (channel << 6), value);
112}
113
114static int write_regs(struct i2c_client *client, u8 *regs, int channel)
115{
116 int i;
117
118 for (i = 0; regs[i] != 0xff; i += 2)
119 if (i2c_smbus_write_byte_data(client,
120 regs[i] | (channel << 6), regs[i + 1]) < 0)
121 return -1;
122 return 0;
123}
124
125static int wis_tw2804_command(struct i2c_client *client,
126 unsigned int cmd, void *arg)
127{
128 struct wis_tw2804 *dec = i2c_get_clientdata(client);
129
130 if (cmd == DECODER_SET_CHANNEL) {
131 int *input = arg;
132
133 if (*input < 0 || *input > 3) {
134 printk(KERN_ERR "wis-tw2804: channel %d is not "
135 "between 0 and 3!\n", *input);
136 return 0;
137 }
138 dec->channel = *input;
139 printk(KERN_DEBUG "wis-tw2804: initializing TW2804 "
140 "channel %d\n", dec->channel);
141 if (dec->channel == 0 &&
142 write_regs(client, global_registers, 0) < 0) {
143 printk(KERN_ERR "wis-tw2804: error initializing "
144 "TW2804 global registers\n");
145 return 0;
146 }
147 if (write_regs(client, channel_registers, dec->channel) < 0) {
148 printk(KERN_ERR "wis-tw2804: error initializing "
149 "TW2804 channel %d\n", dec->channel);
150 return 0;
151 }
152 return 0;
153 }
154
155 if (dec->channel < 0) {
156 printk(KERN_DEBUG "wis-tw2804: ignoring command %08x until "
157 "channel number is set\n", cmd);
158 return 0;
159 }
160
161 switch (cmd) {
162 case DECODER_SET_NORM:
163 {
164 int *input = arg;
165 u8 regs[] = {
166 0x01, *input == VIDEO_MODE_NTSC ? 0xc4 : 0x84,
167 0x09, *input == VIDEO_MODE_NTSC ? 0x07 : 0x04,
168 0x0a, *input == VIDEO_MODE_NTSC ? 0xf0 : 0x20,
169 0x0b, *input == VIDEO_MODE_NTSC ? 0x07 : 0x04,
170 0x0c, *input == VIDEO_MODE_NTSC ? 0xf0 : 0x20,
171 0x0d, *input == VIDEO_MODE_NTSC ? 0x40 : 0x4a,
172 0x16, *input == VIDEO_MODE_NTSC ? 0x00 : 0x40,
173 0x17, *input == VIDEO_MODE_NTSC ? 0x00 : 0x40,
174 0x20, *input == VIDEO_MODE_NTSC ? 0x07 : 0x0f,
175 0x21, *input == VIDEO_MODE_NTSC ? 0x07 : 0x0f,
176 0xff, 0xff,
177 };
178 write_regs(client, regs, dec->channel);
179 dec->norm = *input;
180 break;
181 }
182 case VIDIOC_QUERYCTRL:
183 {
184 struct v4l2_queryctrl *ctrl = arg;
185
186 switch (ctrl->id) {
187 case V4L2_CID_BRIGHTNESS:
188 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
189 strncpy(ctrl->name, "Brightness", sizeof(ctrl->name));
190 ctrl->minimum = 0;
191 ctrl->maximum = 255;
192 ctrl->step = 1;
193 ctrl->default_value = 128;
194 ctrl->flags = 0;
195 break;
196 case V4L2_CID_CONTRAST:
197 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
198 strncpy(ctrl->name, "Contrast", sizeof(ctrl->name));
199 ctrl->minimum = 0;
200 ctrl->maximum = 255;
201 ctrl->step = 1;
202 ctrl->default_value = 128;
203 ctrl->flags = 0;
204 break;
205 case V4L2_CID_SATURATION:
206 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
207 strncpy(ctrl->name, "Saturation", sizeof(ctrl->name));
208 ctrl->minimum = 0;
209 ctrl->maximum = 255;
210 ctrl->step = 1;
211 ctrl->default_value = 128;
212 ctrl->flags = 0;
213 break;
214 case V4L2_CID_HUE:
215 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
216 strncpy(ctrl->name, "Hue", sizeof(ctrl->name));
217 ctrl->minimum = 0;
218 ctrl->maximum = 255;
219 ctrl->step = 1;
220 ctrl->default_value = 128;
221 ctrl->flags = 0;
222 break;
223 }
224 break;
225 }
226 case VIDIOC_S_CTRL:
227 {
228 struct v4l2_control *ctrl = arg;
229
230 switch (ctrl->id) {
231 case V4L2_CID_BRIGHTNESS:
232 if (ctrl->value > 255)
233 dec->brightness = 255;
234 else if (ctrl->value < 0)
235 dec->brightness = 0;
236 else
237 dec->brightness = ctrl->value;
238 write_reg(client, 0x12, dec->brightness, dec->channel);
239 break;
240 case V4L2_CID_CONTRAST:
241 if (ctrl->value > 255)
242 dec->contrast = 255;
243 else if (ctrl->value < 0)
244 dec->contrast = 0;
245 else
246 dec->contrast = ctrl->value;
247 write_reg(client, 0x11, dec->contrast, dec->channel);
248 break;
249 case V4L2_CID_SATURATION:
250 if (ctrl->value > 255)
251 dec->saturation = 255;
252 else if (ctrl->value < 0)
253 dec->saturation = 0;
254 else
255 dec->saturation = ctrl->value;
256 write_reg(client, 0x10, dec->saturation, dec->channel);
257 break;
258 case V4L2_CID_HUE:
259 if (ctrl->value > 255)
260 dec->hue = 255;
261 else if (ctrl->value < 0)
262 dec->hue = 0;
263 else
264 dec->hue = ctrl->value;
265 write_reg(client, 0x0f, dec->hue, dec->channel);
266 break;
267 }
268 break;
269 }
270 case VIDIOC_G_CTRL:
271 {
272 struct v4l2_control *ctrl = arg;
273
274 switch (ctrl->id) {
275 case V4L2_CID_BRIGHTNESS:
276 ctrl->value = dec->brightness;
277 break;
278 case V4L2_CID_CONTRAST:
279 ctrl->value = dec->contrast;
280 break;
281 case V4L2_CID_SATURATION:
282 ctrl->value = dec->saturation;
283 break;
284 case V4L2_CID_HUE:
285 ctrl->value = dec->hue;
286 break;
287 }
288 break;
289 }
290 default:
291 break;
292 }
293 return 0;
294}
295
296static struct i2c_driver wis_tw2804_driver;
297
298static struct i2c_client wis_tw2804_client_templ = {
299 .name = "TW2804 (WIS)",
300 .driver = &wis_tw2804_driver,
301};
302
303static int wis_tw2804_detect(struct i2c_adapter *adapter, int addr, int kind)
304{
305 struct i2c_client *client;
306 struct wis_tw2804 *dec;
307
308 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
309 return 0;
310
311 client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
312 if (client == NULL)
313 return -ENOMEM;
314 memcpy(client, &wis_tw2804_client_templ,
315 sizeof(wis_tw2804_client_templ));
316 client->adapter = adapter;
317 client->addr = addr;
318
319 dec = kmalloc(sizeof(struct wis_tw2804), GFP_KERNEL);
320 if (dec == NULL) {
321 kfree(client);
322 return -ENOMEM;
323 }
324 dec->channel = -1;
325 dec->norm = VIDEO_MODE_NTSC;
326 dec->brightness = 128;
327 dec->contrast = 128;
328 dec->saturation = 128;
329 dec->hue = 128;
330 i2c_set_clientdata(client, dec);
331
332 printk(KERN_DEBUG "wis-tw2804: creating TW2804 at address %d on %s\n",
333 addr, adapter->name);
334
335 i2c_attach_client(client);
336 return 0;
337}
338
339static int wis_tw2804_detach(struct i2c_client *client)
340{
341 struct wis_tw2804 *dec = i2c_get_clientdata(client);
342 int r;
343
344 r = i2c_detach_client(client);
345 if (r < 0)
346 return r;
347
348 kfree(client);
349 kfree(dec);
350 return 0;
351}
352
353static struct i2c_driver wis_tw2804_driver = {
354 .driver = {
355 .name = "WIS TW2804 I2C driver",
356 },
357 .id = I2C_DRIVERID_WIS_TW2804,
358 .detach_client = wis_tw2804_detach,
359 .command = wis_tw2804_command,
360};
361
362static int __init wis_tw2804_init(void)
363{
364 int r;
365
366 r = i2c_add_driver(&wis_tw2804_driver);
367 if (r < 0)
368 return r;
369 return wis_i2c_add_driver(wis_tw2804_driver.id, wis_tw2804_detect);
370}
371
372static void __exit wis_tw2804_cleanup(void)
373{
374 wis_i2c_del_driver(wis_tw2804_detect);
375 i2c_del_driver(&wis_tw2804_driver);
376}
377
378module_init(wis_tw2804_init);
379module_exit(wis_tw2804_cleanup);
380
381MODULE_LICENSE("GPL v2");