blob: 4dcba5a4fff099d95a19b61391c4925e6df47f92 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001 /*
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
29#include <linux/module.h>
30#include <linux/ioctl.h>
31#include <linux/i2c.h>
32
33#include "tea6420.h"
34
35static int debug = 0; /* insmod parameter */
36module_param(debug, int, 0644);
37MODULE_PARM_DESC(debug, "Turn on/off device debugging (default:off).");
38#define dprintk(args...) \
Sam Ravnborg367cb702006-01-06 21:17:50 +010039 do { if (debug) { printk("%s: %s()[%d]: ", KBUILD_MODNAME, __FUNCTION__, __LINE__); printk(args); } } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/* addresses to scan, found only at 0x4c and/or 0x4d (7-Bit) */
42static unsigned short normal_i2c[] = { I2C_TEA6420_1, I2C_TEA6420_2, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44/* magic definition of all other variables and things */
45I2C_CLIENT_INSMOD;
46
47static struct i2c_driver driver;
48static struct i2c_client client_template;
49
50/* make a connection between the input 'i' and the output 'o'
51 with gain 'g' for the tea6420-client 'client' (note: i = 6 means 'mute') */
52static int tea6420_switch(struct i2c_client *client, int i, int o, int g)
53{
54 u8 byte = 0;
55 int ret;
56
57 dprintk("adr:0x%02x, i:%d, o:%d, g:%d\n", client->addr, i, o, g);
58
59 /* check if the paramters are valid */
60 if (i < 1 || i > 6 || o < 1 || o > 4 || g < 0 || g > 6 || g % 2 != 0)
61 return -1;
62
63 byte = ((o - 1) << 5);
64 byte |= (i - 1);
65
66 /* to understand this, have a look at the tea6420-specs (p.5) */
67 switch (g) {
68 case 0:
69 byte |= (3 << 3);
70 break;
71 case 2:
72 byte |= (2 << 3);
73 break;
74 case 4:
75 byte |= (1 << 3);
76 break;
77 case 6:
78 break;
79 }
80
81 ret = i2c_smbus_write_byte(client, byte);
82 if (ret) {
83 dprintk("i2c_smbus_write_byte() failed, ret:%d\n", ret);
84 return -EIO;
85 }
86
87 return 0;
88}
89
90/* this function is called by i2c_probe */
91static int tea6420_detect(struct i2c_adapter *adapter, int address, int kind)
92{
93 struct i2c_client *client;
94 int err = 0, i = 0;
95
96 /* let's see whether this adapter can support what we need */
97 if (0 == i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WRITE_BYTE)) {
98 return 0;
99 }
100
101 /* allocate memory for client structure */
Panagiotis Issaris74081872006-01-11 19:40:56 -0200102 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (0 == client) {
104 return -ENOMEM;
105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 /* fill client structure */
108 memcpy(client, &client_template, sizeof(struct i2c_client));
109 client->addr = address;
110 client->adapter = adapter;
111
112 /* tell the i2c layer a new client has arrived */
113 if (0 != (err = i2c_attach_client(client))) {
114 kfree(client);
115 return err;
116 }
117
118 /* set initial values: set "mute"-input to all outputs at gain 0 */
119 err = 0;
120 for (i = 1; i < 5; i++) {
121 err += tea6420_switch(client, 6, i, 0);
122 }
123 if (err) {
124 dprintk("could not initialize tea6420\n");
125 kfree(client);
126 return -ENODEV;
127 }
128
129 printk("tea6420: detected @ 0x%02x on adapter %s\n", address, &client->adapter->name[0]);
130
131 return 0;
132}
133
134static int attach(struct i2c_adapter *adapter)
135{
136 /* let's see whether this is a know adapter we can attach to */
Jean Delvare1684a9842005-08-11 23:51:10 +0200137 if (adapter->id != I2C_HW_SAA7146) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 dprintk("refusing to probe on unknown adapter [name='%s',id=0x%x]\n", adapter->name, adapter->id);
139 return -ENODEV;
140 }
141
142 return i2c_probe(adapter, &addr_data, &tea6420_detect);
143}
144
145static int detach(struct i2c_client *client)
146{
147 int ret = i2c_detach_client(client);
148 kfree(client);
149 return ret;
150}
151
152static int command(struct i2c_client *client, unsigned int cmd, void *arg)
153{
154 struct tea6420_multiplex *a = (struct tea6420_multiplex *)arg;
155 int result = 0;
156
157 switch (cmd) {
158 case TEA6420_SWITCH:
159 result = tea6420_switch(client, a->in, a->out, a->gain);
160 break;
161 default:
162 return -ENOIOCTLCMD;
163 }
164
165 return result;
166}
167
168static struct i2c_driver driver = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100169 .driver = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100170 .name = "tea6420",
171 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 .id = I2C_DRIVERID_TEA6420,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 .attach_adapter = attach,
174 .detach_client = detach,
175 .command = command,
176};
177
178static struct i2c_client client_template = {
Jean Delvarefae91e72005-08-15 19:57:04 +0200179 .name = "tea6420",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 .driver = &driver,
181};
182
183static int __init this_module_init(void)
184{
185 return i2c_add_driver(&driver);
186}
187
188static void __exit this_module_exit(void)
189{
190 i2c_del_driver(&driver);
191}
192
193module_init(this_module_init);
194module_exit(this_module_exit);
195
196MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
197MODULE_DESCRIPTION("tea6420 driver");
198MODULE_LICENSE("GPL");