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