Staging: add the go7007 video driver
[linux-2.6] / drivers / staging / go7007 / wis-uda1342.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/version.h>
21 #include <linux/i2c.h>
22 #include <linux/videodev.h>
23 #include <media/tvaudio.h>
24 #include <media/v4l2-common.h>
25
26 #include "wis-i2c.h"
27
28 static int write_reg(struct i2c_client *client, int reg, int value)
29 {
30         /* UDA1342 wants MSB first, but SMBus sends LSB first */
31         i2c_smbus_write_word_data(client, reg, swab16(value));
32         return 0;
33 }
34
35 static int wis_uda1342_command(struct i2c_client *client,
36                                 unsigned int cmd, void *arg)
37 {
38         switch (cmd) {
39         case VIDIOC_S_AUDIO:
40         {
41                 int *inp = arg;
42
43                 switch (*inp) {
44                 case TVAUDIO_INPUT_TUNER:
45                         write_reg(client, 0x00, 0x1441); /* select input 2 */
46                         break;
47                 case TVAUDIO_INPUT_EXTERN:
48                         write_reg(client, 0x00, 0x1241); /* select input 1 */
49                         break;
50                 default:
51                         printk(KERN_ERR "wis-uda1342: input %d not supported\n",
52                                         *inp);
53                         break;
54                 }
55                 break;
56         }
57         default:
58                 break;
59         }
60         return 0;
61 }
62
63 static struct i2c_driver wis_uda1342_driver;
64
65 static struct i2c_client wis_uda1342_client_templ = {
66         .name           = "UDA1342 (WIS)",
67         .driver         = &wis_uda1342_driver,
68 };
69
70 static int wis_uda1342_detect(struct i2c_adapter *adapter, int addr, int kind)
71 {
72         struct i2c_client *client;
73
74         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
75                 return 0;
76
77         client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
78         if (client == NULL)
79                 return -ENOMEM;
80         memcpy(client, &wis_uda1342_client_templ,
81                         sizeof(wis_uda1342_client_templ));
82         client->adapter = adapter;
83         client->addr = addr;
84
85         printk(KERN_DEBUG
86                 "wis-uda1342: initializing UDA1342 at address %d on %s\n",
87                 addr, adapter->name);
88
89         write_reg(client, 0x00, 0x8000); /* reset registers */
90         write_reg(client, 0x00, 0x1241); /* select input 1 */
91
92         i2c_attach_client(client);
93         return 0;
94 }
95
96 static int wis_uda1342_detach(struct i2c_client *client)
97 {
98         int r;
99
100         r = i2c_detach_client(client);
101         if (r < 0)
102                 return r;
103
104         kfree(client);
105         return 0;
106 }
107
108 static struct i2c_driver wis_uda1342_driver = {
109         .driver = {
110                 .name   = "WIS UDA1342 I2C driver",
111         },
112         .id             = I2C_DRIVERID_WIS_UDA1342,
113         .detach_client  = wis_uda1342_detach,
114         .command        = wis_uda1342_command,
115 };
116
117 static int __init wis_uda1342_init(void)
118 {
119         int r;
120
121         r = i2c_add_driver(&wis_uda1342_driver);
122         if (r < 0)
123                 return r;
124         return wis_i2c_add_driver(wis_uda1342_driver.id, wis_uda1342_detect);
125 }
126
127 static void __exit wis_uda1342_cleanup(void)
128 {
129         wis_i2c_del_driver(wis_uda1342_detect);
130         i2c_del_driver(&wis_uda1342_driver);
131 }
132
133 module_init(wis_uda1342_init);
134 module_exit(wis_uda1342_cleanup);
135
136 MODULE_LICENSE("GPL v2");