]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/video/tea6420.c
V4L/DVB (8628): bttv: Add support for Encore ENLTV2-FM
[mirror_ubuntu-artful-kernel.git] / drivers / media / video / tea6420.c
CommitLineData
1da177e4
LT
1 /*
2 tea6420 - i2c-driver for the tea6420 by SGS Thomson
3
4 Copyright (C) 1998-2003 Michael Hunold <michael@mihu.de>
5
6 The tea6420 is a bus controlled audio-matrix with 5 stereo inputs,
7 4 stereo outputs and gain control for each output.
8 It is cascadable, i.e. it can be found at the adresses 0x98
9 and 0x9a on the i2c-bus.
10
11 For detailed informations download the specifications directly
12 from SGS Thomson at http://www.st.com
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 */
28
a8733ca5 29
1da177e4
LT
30#include <linux/module.h>
31#include <linux/ioctl.h>
32#include <linux/i2c.h>
33
34#include "tea6420.h"
35
ff699e6b 36static int debug; /* insmod parameter */
1da177e4
LT
37module_param(debug, int, 0644);
38MODULE_PARM_DESC(debug, "Turn on/off device debugging (default:off).");
f87086e3 39
1da177e4 40#define dprintk(args...) \
7e28adb2 41 do { if (debug) { printk("%s: %s()[%d]: ", KBUILD_MODNAME, __func__, __LINE__); printk(args); } } while (0)
1da177e4
LT
42
43/* addresses to scan, found only at 0x4c and/or 0x4d (7-Bit) */
09df1c16 44static unsigned short normal_i2c[] = { I2C_ADDR_TEA6420_1, I2C_ADDR_TEA6420_2, I2C_CLIENT_END };
1da177e4
LT
45
46/* magic definition of all other variables and things */
47I2C_CLIENT_INSMOD;
48
49static struct i2c_driver driver;
50static struct i2c_client client_template;
51
52/* make a connection between the input 'i' and the output 'o'
53 with gain 'g' for the tea6420-client 'client' (note: i = 6 means 'mute') */
54static int tea6420_switch(struct i2c_client *client, int i, int o, int g)
55{
56 u8 byte = 0;
57 int ret;
58
59 dprintk("adr:0x%02x, i:%d, o:%d, g:%d\n", client->addr, i, o, g);
60
c84e6036 61 /* check if the parameters are valid */
1da177e4
LT
62 if (i < 1 || i > 6 || o < 1 || o > 4 || g < 0 || g > 6 || g % 2 != 0)
63 return -1;
64
65 byte = ((o - 1) << 5);
66 byte |= (i - 1);
67
68 /* to understand this, have a look at the tea6420-specs (p.5) */
69 switch (g) {
70 case 0:
71 byte |= (3 << 3);
72 break;
73 case 2:
74 byte |= (2 << 3);
75 break;
76 case 4:
77 byte |= (1 << 3);
78 break;
79 case 6:
80 break;
81 }
82
83 ret = i2c_smbus_write_byte(client, byte);
84 if (ret) {
85 dprintk("i2c_smbus_write_byte() failed, ret:%d\n", ret);
86 return -EIO;
87 }
a8733ca5 88
1da177e4
LT
89 return 0;
90}
91
92/* this function is called by i2c_probe */
93static int tea6420_detect(struct i2c_adapter *adapter, int address, int kind)
94{
95 struct i2c_client *client;
96 int err = 0, i = 0;
97
98 /* let's see whether this adapter can support what we need */
99 if (0 == i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WRITE_BYTE)) {
100 return 0;
101 }
102
103 /* allocate memory for client structure */
7408187d 104 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
5fa1247a 105 if (!client) {
1da177e4
LT
106 return -ENOMEM;
107 }
1da177e4
LT
108
109 /* fill client structure */
110 memcpy(client, &client_template, sizeof(struct i2c_client));
111 client->addr = address;
112 client->adapter = adapter;
113
114 /* tell the i2c layer a new client has arrived */
115 if (0 != (err = i2c_attach_client(client))) {
116 kfree(client);
117 return err;
118 }
119
120 /* set initial values: set "mute"-input to all outputs at gain 0 */
121 err = 0;
122 for (i = 1; i < 5; i++) {
123 err += tea6420_switch(client, 6, i, 0);
124 }
125 if (err) {
126 dprintk("could not initialize tea6420\n");
127 kfree(client);
128 return -ENODEV;
129 }
130
131 printk("tea6420: detected @ 0x%02x on adapter %s\n", address, &client->adapter->name[0]);
132
133 return 0;
134}
135
136static int attach(struct i2c_adapter *adapter)
137{
138 /* let's see whether this is a know adapter we can attach to */
1684a984 139 if (adapter->id != I2C_HW_SAA7146) {
1da177e4
LT
140 dprintk("refusing to probe on unknown adapter [name='%s',id=0x%x]\n", adapter->name, adapter->id);
141 return -ENODEV;
142 }
143
144 return i2c_probe(adapter, &addr_data, &tea6420_detect);
145}
146
147static int detach(struct i2c_client *client)
148{
149 int ret = i2c_detach_client(client);
150 kfree(client);
151 return ret;
152}
153
154static int command(struct i2c_client *client, unsigned int cmd, void *arg)
155{
156 struct tea6420_multiplex *a = (struct tea6420_multiplex *)arg;
157 int result = 0;
158
159 switch (cmd) {
160 case TEA6420_SWITCH:
161 result = tea6420_switch(client, a->in, a->out, a->gain);
162 break;
163 default:
164 return -ENOIOCTLCMD;
165 }
166
167 return result;
168}
169
170static struct i2c_driver driver = {
604f28e2 171 .driver = {
a8733ca5 172 .name = "tea6420",
604f28e2 173 },
1da177e4 174 .id = I2C_DRIVERID_TEA6420,
1da177e4
LT
175 .attach_adapter = attach,
176 .detach_client = detach,
177 .command = command,
178};
179
180static struct i2c_client client_template = {
fae91e72 181 .name = "tea6420",
1da177e4
LT
182 .driver = &driver,
183};
184
185static int __init this_module_init(void)
186{
187 return i2c_add_driver(&driver);
188}
189
190static void __exit this_module_exit(void)
191{
192 i2c_del_driver(&driver);
193}
194
195module_init(this_module_init);
196module_exit(this_module_exit);
197
198MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
199MODULE_DESCRIPTION("tea6420 driver");
200MODULE_LICENSE("GPL");