Merge branch 'bugzilla-12249' into release
[linux-2.6] / drivers / staging / go7007 / s2250-board.c
1 /*
2  * Copyright (C) 2008 Sensoray Company 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/usb.h>
21 #include <linux/i2c.h>
22 #include <linux/videodev2.h>
23 #include <media/v4l2-common.h>
24 #include "go7007-priv.h"
25 #include "wis-i2c.h"
26
27 extern int s2250loader_init(void);
28 extern void s2250loader_cleanup(void);
29
30 #define TLV320_ADDRESS      0x34
31 #define VPX322_ADDR_ANALOGCONTROL1      0x02
32 #define VPX322_ADDR_BRIGHTNESS0         0x0127
33 #define VPX322_ADDR_BRIGHTNESS1         0x0131
34 #define VPX322_ADDR_CONTRAST0           0x0128
35 #define VPX322_ADDR_CONTRAST1           0x0132
36 #define VPX322_ADDR_HUE                 0x00dc
37 #define VPX322_ADDR_SAT                 0x0030
38
39 struct go7007_usb_board {
40         unsigned int flags;
41         struct go7007_board_info main_info;
42 };
43
44 struct go7007_usb {
45         struct go7007_usb_board *board;
46         struct semaphore i2c_lock;
47         struct usb_device *usbdev;
48         struct urb *video_urbs[8];
49         struct urb *audio_urbs[8];
50         struct urb *intr_urb;
51 };
52
53 static unsigned char aud_regs[] = {
54         0x1e, 0x00,
55         0x00, 0x17,
56         0x02, 0x17,
57         0x04, 0xf9,
58         0x06, 0xf9,
59         0x08, 0x02,
60         0x0a, 0x00,
61         0x0c, 0x00,
62         0x0a, 0x00,
63         0x0c, 0x00,
64         0x0e, 0x02,
65         0x10, 0x00,
66         0x12, 0x01,
67         0x00, 0x00,
68 };
69
70
71 static unsigned char vid_regs[] = {
72         0xF2, 0x0f,
73         0xAA, 0x00,
74         0xF8, 0xff,
75         0x00, 0x00,
76 };
77
78 static u16 vid_regs_fp[] = {
79         0x028, 0x067,
80         0x120, 0x016,
81         0x121, 0xcF2,
82         0x122, 0x0F2,
83         0x123, 0x00c,
84         0x124, 0x2d0,
85         0x125, 0x2e0,
86         0x126, 0x004,
87         0x128, 0x1E0,
88         0x12A, 0x016,
89         0x12B, 0x0F2,
90         0x12C, 0x0F2,
91         0x12D, 0x00c,
92         0x12E, 0x2d0,
93         0x12F, 0x2e0,
94         0x130, 0x004,
95         0x132, 0x1E0,
96         0x140, 0x060,
97         0x153, 0x00C,
98         0x154, 0x200,
99         0x150, 0x801,
100         0x000, 0x000
101 };
102
103 /* PAL specific values */
104 static u16 vid_regs_fp_pal[] =
105 {
106         0x120, 0x017,
107         0x121, 0xd22,
108         0x122, 0x122,
109         0x12A, 0x017,
110         0x12B, 0x122,
111         0x12C, 0x122,
112         0x140, 0x060,
113         0x000, 0x000,
114 };
115
116 struct s2250 {
117         int std;
118         int input;
119         int brightness;
120         int contrast;
121         int saturation;
122         int hue;
123         int reg12b_val;
124         int audio_input;
125         struct i2c_client *audio;
126 };
127
128 /* from go7007-usb.c which is Copyright (C) 2005-2006 Micronas USA Inc.*/
129 static int go7007_usb_vendor_request(struct go7007 *go, u16 request,
130         u16 value, u16 index, void *transfer_buffer, int length, int in)
131 {
132         struct go7007_usb *usb = go->hpi_context;
133         int timeout = 5000;
134
135         if (in) {
136                 return usb_control_msg(usb->usbdev,
137                                 usb_rcvctrlpipe(usb->usbdev, 0), request,
138                                 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
139                                 value, index, transfer_buffer, length, timeout);
140         } else {
141                 return usb_control_msg(usb->usbdev,
142                                 usb_sndctrlpipe(usb->usbdev, 0), request,
143                                 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
144                                 value, index, transfer_buffer, length, timeout);
145         }
146 }
147 /* end from go7007-usb.c which is Copyright (C) 2005-2006 Micronas USA Inc.*/
148
149 static int write_reg(struct i2c_client *client, u8 reg, u8 value)
150 {
151         struct go7007 *go = i2c_get_adapdata(client->adapter);
152         struct go7007_usb *usb;
153         int rc;
154         int dev_addr = client->addr;
155         u8 *buf;
156
157         if (go == NULL)
158                 return -ENODEV;
159
160         if (go->status == STATUS_SHUTDOWN)
161                 return -EBUSY;
162
163         buf = kzalloc(16, GFP_KERNEL);
164         if (buf == NULL)
165                 return -ENOMEM;
166
167         usb = go->hpi_context;
168         if (down_interruptible(&usb->i2c_lock) != 0) {
169                 printk(KERN_INFO "i2c lock failed\n");
170                 kfree(buf);
171                 return -EINTR;
172         }
173         rc = go7007_usb_vendor_request(go, 0x55, dev_addr,
174                                        (reg<<8 | value),
175                                        buf,
176                                        16, 1);
177
178         up(&usb->i2c_lock);
179         kfree(buf);
180         return rc;
181 }
182
183 static int write_reg_fp(struct i2c_client *client, u16 addr, u16 val)
184 {
185         struct go7007 *go = i2c_get_adapdata(client->adapter);
186         struct go7007_usb *usb;
187         u8 *buf;
188         struct s2250 *dec = i2c_get_clientdata(client);
189
190         if (go == NULL)
191                 return -ENODEV;
192
193         if (go->status == STATUS_SHUTDOWN)
194                 return -EBUSY;
195
196         buf = kzalloc(16, GFP_KERNEL);
197
198         if (buf == NULL)
199                 return -ENOMEM;
200
201
202
203         memset(buf, 0xcd, 6);
204
205         usb = go->hpi_context;
206         if (down_interruptible(&usb->i2c_lock) != 0) {
207                 printk(KERN_INFO "i2c lock failed\n");
208                 return -EINTR;
209         }
210         if (go7007_usb_vendor_request(go, 0x57, addr, val, buf, 16, 1) < 0)
211                 return -EFAULT;
212
213         up(&usb->i2c_lock);
214         if (buf[0] == 0) {
215                 unsigned int subaddr, val_read;
216
217                 subaddr = (buf[4] << 8) + buf[5];
218                 val_read = (buf[2] << 8) + buf[3];
219                 if (val_read != val) {
220                         printk(KERN_INFO "invalid fp write %x %x\n",
221                                val_read, val);
222                         return -EFAULT;
223                 }
224                 if (subaddr != addr) {
225                         printk(KERN_INFO "invalid fp write addr %x %x\n",
226                                subaddr, addr);
227                         return -EFAULT;
228                 }
229         } else
230                 return -EFAULT;
231
232         /* save last 12b value */
233         if (addr == 0x12b)
234                 dec->reg12b_val = val;
235
236         return 0;
237 }
238
239 static int write_regs(struct i2c_client *client, u8 *regs)
240 {
241         int i;
242
243         for (i = 0; !((regs[i] == 0x00) && (regs[i+1] == 0x00)); i += 2) {
244                 if (write_reg(client, regs[i], regs[i+1]) < 0) {
245                         printk(KERN_INFO "s2250: failed\n");
246                         return -1;
247                 }
248         }
249         return 0;
250 }
251
252 static int write_regs_fp(struct i2c_client *client, u16 *regs)
253 {
254         int i;
255
256         for (i = 0; !((regs[i] == 0x00) && (regs[i+1] == 0x00)); i += 2) {
257                 if (write_reg_fp(client, regs[i], regs[i+1]) < 0) {
258                         printk(KERN_INFO "s2250: failed fp\n");
259                         return -1;
260                 }
261         }
262         return 0;
263 }
264
265
266 static int s2250_command(struct i2c_client *client,
267                          unsigned int cmd, void *arg)
268 {
269         struct s2250 *dec = i2c_get_clientdata(client);
270
271         switch (cmd) {
272         case VIDIOC_S_INPUT:
273         {
274                 int vidsys;
275                 int *input = arg;
276
277                 vidsys = (dec->std == V4L2_STD_NTSC) ? 0x01 : 0x00;
278                 if (*input == 0) {
279                         /* composite */
280                         write_reg_fp(client, 0x20, 0x020 | vidsys);
281                         write_reg_fp(client, 0x21, 0x662);
282                         write_reg_fp(client, 0x140, 0x060);
283                 } else {
284                         /* S-Video */
285                         write_reg_fp(client, 0x20, 0x040 | vidsys);
286                         write_reg_fp(client, 0x21, 0x666);
287                         write_reg_fp(client, 0x140, 0x060);
288                 }
289                 dec->input = *input;
290                 break;
291         }
292         case VIDIOC_S_STD:
293         {
294                 v4l2_std_id *std = arg;
295                 u16 vidsource;
296
297                 vidsource = (dec->input == 1) ? 0x040 : 0x020;
298                 dec->std = *std;
299                 switch (dec->std) {
300                 case V4L2_STD_NTSC:
301                         write_regs_fp(client, vid_regs_fp);
302                         write_reg_fp(client, 0x20, vidsource | 1);
303                         break;
304                 case V4L2_STD_PAL:
305                         write_regs_fp(client, vid_regs_fp);
306                         write_regs_fp(client, vid_regs_fp_pal);
307                         write_reg_fp(client, 0x20, vidsource);
308                         break;
309                 default:
310                         return -EINVAL;
311                 }
312                 break;
313         }
314         case VIDIOC_QUERYCTRL:
315         {
316                 struct v4l2_queryctrl *ctrl = arg;
317                 static const u32 user_ctrls[] = {
318                         V4L2_CID_BRIGHTNESS,
319                         V4L2_CID_CONTRAST,
320                         V4L2_CID_SATURATION,
321                         V4L2_CID_HUE,
322                         0
323                 };
324                 static const u32 *ctrl_classes[] = {
325                         user_ctrls,
326                         NULL
327                 };
328
329                 ctrl->id = v4l2_ctrl_next(ctrl_classes, ctrl->id);
330                 switch (ctrl->id) {
331                 case V4L2_CID_BRIGHTNESS:
332                         v4l2_ctrl_query_fill(ctrl, 0, 100, 1, 50);
333                         break;
334                 case V4L2_CID_CONTRAST:
335                         v4l2_ctrl_query_fill(ctrl, 0, 100, 1, 50);
336                         break;
337                 case V4L2_CID_SATURATION:
338                         v4l2_ctrl_query_fill(ctrl, 0, 100, 1, 50);
339                         break;
340                 case V4L2_CID_HUE:
341                         v4l2_ctrl_query_fill(ctrl, -50, 50, 1, 0);
342                         break;
343                 default:
344                         ctrl->name[0] = '\0';
345                         return -EINVAL;
346                 }
347                 break;
348         }
349         case VIDIOC_S_CTRL:
350         {
351                 struct v4l2_control *ctrl = arg;
352                 int value1;
353
354                 switch (ctrl->id) {
355                 case V4L2_CID_BRIGHTNESS:
356                         printk(KERN_INFO "s2250: future setting\n");
357                         return -EINVAL;
358                 case V4L2_CID_CONTRAST:
359                         printk(KERN_INFO "s2250: future setting\n");
360                         return -EINVAL;
361                         break;
362                 case V4L2_CID_SATURATION:
363                         if (ctrl->value > 127)
364                                 dec->saturation = 127;
365                         else if (ctrl->value < 0)
366                                 dec->saturation = 0;
367                         else
368                                 dec->saturation = ctrl->value;
369
370                         value1 = dec->saturation * 4140 / 100;
371                         if (value1 > 4094)
372                                 value1 = 4094;
373                         write_reg_fp(client, VPX322_ADDR_SAT, value1);
374                         break;
375                 case V4L2_CID_HUE:
376                         if (ctrl->value > 50)
377                                 dec->hue = 50;
378                         else if (ctrl->value < -50)
379                                 dec->hue = -50;
380                         else
381                                 dec->hue = ctrl->value;
382                         /* clamp the hue range */
383                         value1 = dec->hue * 280 / 50;
384                         write_reg_fp(client, VPX322_ADDR_HUE, value1);
385                         break;
386                 }
387                 break;
388         }
389         case VIDIOC_G_CTRL:
390         {
391                 struct v4l2_control *ctrl = arg;
392
393                 switch (ctrl->id) {
394                 case V4L2_CID_BRIGHTNESS:
395                         ctrl->value = dec->brightness;
396                         break;
397                 case V4L2_CID_CONTRAST:
398                         ctrl->value = dec->contrast;
399                         break;
400                 case V4L2_CID_SATURATION:
401                         ctrl->value = dec->saturation;
402                         break;
403                 case V4L2_CID_HUE:
404                         ctrl->value = dec->hue;
405                         break;
406                 }
407                 break;
408         }
409         case VIDIOC_S_FMT:
410         {
411                 struct v4l2_format *fmt = arg;
412                 if (fmt->fmt.pix.height < 640) {
413                         write_reg_fp(client, 0x12b, dec->reg12b_val | 0x400);
414                         write_reg_fp(client, 0x140, 0x060);
415                 } else {
416                         write_reg_fp(client, 0x12b, dec->reg12b_val & ~0x400);
417                         write_reg_fp(client, 0x140, 0x060);
418                 }
419                 return 0;
420         }
421         case VIDIOC_G_AUDIO:
422         {
423                 struct v4l2_audio *audio = arg;
424
425                 memset(audio, 0, sizeof(*audio));
426                 audio->index = dec->audio_input;
427                 /* fall through */
428         }
429         case VIDIOC_ENUMAUDIO:
430         {
431                 struct v4l2_audio *audio = arg;
432
433                 switch (audio->index) {
434                 case 0:
435                         strcpy(audio->name, "Line In");
436                         break;
437                 case 1:
438                         strcpy(audio->name, "Mic");
439                         break;
440                 case 2:
441                         strcpy(audio->name, "Mic Boost");
442                         break;
443                 default:
444                         audio->name[0] = '\0';
445                         return 0;
446                 }
447                 audio->capability = V4L2_AUDCAP_STEREO;
448                 audio->mode = 0;
449                 return 0;
450         }
451         case VIDIOC_S_AUDIO:
452         {
453                 struct v4l2_audio *audio = arg;
454
455                 switch (audio->index) {
456                 case 0:
457                         write_reg(dec->audio, 0x08, 0x02); /* Line In */
458                         break;
459                 case 1:
460                         write_reg(dec->audio, 0x08, 0x04); /* Mic */
461                         break;
462                 case 2:
463                         write_reg(dec->audio, 0x08, 0x05); /* Mic Boost */
464                         break;
465                 default:
466                         return -EINVAL;
467                 }
468                 dec->audio_input = audio->index;
469                 return 0;
470         }
471
472         default:
473                 printk(KERN_INFO "s2250: unknown command 0x%x\n", cmd);
474                 break;
475         }
476         return 0;
477 }
478
479 static int s2250_probe(struct i2c_client *client,
480                        const struct i2c_device_id *id)
481 {
482         struct i2c_client *audio;
483         struct i2c_adapter *adapter = client->adapter;
484         struct s2250 *dec;
485         u8 *data;
486         struct go7007 *go = i2c_get_adapdata(adapter);
487         struct go7007_usb *usb = go->hpi_context;
488
489         audio = i2c_new_dummy(adapter, TLV320_ADDRESS >> 1);
490         if (audio == NULL)
491                 return -ENOMEM;
492
493         dec = kmalloc(sizeof(struct s2250), GFP_KERNEL);
494         if (dec == NULL) {
495                 i2c_unregister_device(audio);
496                 return -ENOMEM;
497         }
498
499         dec->std = V4L2_STD_NTSC;
500         dec->brightness = 50;
501         dec->contrast = 50;
502         dec->saturation = 50;
503         dec->hue = 0;
504         dec->audio = audio;
505         i2c_set_clientdata(client, dec);
506
507         printk(KERN_DEBUG
508                "s2250: initializing video decoder on %s\n",
509                adapter->name);
510
511         /* initialize the audio */
512         if (write_regs(audio, aud_regs) < 0) {
513                 printk(KERN_ERR
514                        "s2250: error initializing audio\n");
515                 i2c_unregister_device(audio);
516                 kfree(dec);
517                 return 0;
518         }
519
520         if (write_regs(client, vid_regs) < 0) {
521                 printk(KERN_ERR
522                        "s2250: error initializing decoder\n");
523                 i2c_unregister_device(audio);
524                 kfree(dec);
525                 return 0;
526         }
527         if (write_regs_fp(client, vid_regs_fp) < 0) {
528                 printk(KERN_ERR
529                        "s2250: error initializing decoder\n");
530                 i2c_unregister_device(audio);
531                 kfree(dec);
532                 return 0;
533         }
534         /* set default channel */
535         /* composite */
536         write_reg_fp(client, 0x20, 0x020 | 1);
537         write_reg_fp(client, 0x21, 0x662);
538         write_reg_fp(client, 0x140, 0x060);
539
540         /* set default audio input */
541         dec->audio_input = 0;
542         write_reg(client, 0x08, 0x02); /* Line In */
543
544         if (down_interruptible(&usb->i2c_lock) == 0) {
545                 data = kzalloc(16, GFP_KERNEL);
546                 if (data != NULL) {
547                         int rc;
548                         rc = go7007_usb_vendor_request(go, 0x41, 0, 0,
549                                                        data, 16, 1);
550                         if (rc > 0) {
551                                 u8 mask;
552                                 data[0] = 0;
553                                 mask = 1<<5;
554                                 data[0] &= ~mask;
555                                 data[1] |= mask;
556                                 go7007_usb_vendor_request(go, 0x40, 0,
557                                                           (data[1]<<8)
558                                                           + data[1],
559                                                           data, 16, 0);
560                         }
561                         kfree(data);
562                 }
563                 up(&usb->i2c_lock);
564         }
565
566         printk("s2250: initialized successfully\n");
567         return 0;
568 }
569
570 static int s2250_remove(struct i2c_client *client)
571 {
572         struct s2250 *dec = i2c_get_clientdata(client);
573
574         i2c_set_clientdata(client, NULL);
575         i2c_unregister_device(dec->audio);
576         kfree(dec);
577         return 0;
578 }
579
580 static struct i2c_device_id s2250_id[] = {
581         { "s2250_board", 0 },
582         { }
583 };
584
585 static struct i2c_driver s2250_driver = {
586         .driver = {
587                 .name   = "Sensoray 2250 board driver",
588         },
589         .probe          = s2250_probe,
590         .remove         = s2250_remove,
591         .command        = s2250_command,
592         .id_table       = s2250_id,
593 };
594
595 static int __init s2250_init(void)
596 {
597         int r;
598
599         r = s2250loader_init();
600         if (r < 0)
601                 return r;
602
603         r = i2c_add_driver(&s2250_driver);
604         if (r < 0)
605                 s2250loader_cleanup();
606
607         return r;
608 }
609
610 static void __exit s2250_cleanup(void)
611 {
612         i2c_del_driver(&s2250_driver);
613
614         s2250loader_cleanup();
615 }
616
617 module_init(s2250_init);
618 module_exit(s2250_cleanup);
619
620 MODULE_AUTHOR("");
621 MODULE_DESCRIPTION("Board driver for Sensoryray 2250");
622 MODULE_LICENSE("GPL v2");