Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  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 
 29 #include <linux/module.h>
 30 #include <linux/ioctl.h>
 31 #include <linux/i2c.h>
 32 
 33 #include "tea6420.h"
 34 
 35 static int debug = 0;           /* insmod parameter */
 36 module_param(debug, int, 0644);
 37 MODULE_PARM_DESC(debug, "Turn on/off device debugging (default:off).");
 38 #define dprintk(args...) \
 39             do { if (debug) { printk("%s: %s()[%d]: ",__stringify(KBUILD_MODNAME), __FUNCTION__, __LINE__); printk(args); } } while (0)
 40 
 41 /* addresses to scan, found only at 0x4c and/or 0x4d (7-Bit) */
 42 static unsigned short normal_i2c[] = { I2C_TEA6420_1, I2C_TEA6420_2, I2C_CLIENT_END };
 43 static unsigned short normal_i2c_range[] = { I2C_CLIENT_END };
 44 
 45 /* magic definition of all other variables and things */
 46 I2C_CLIENT_INSMOD;
 47 
 48 static struct i2c_driver driver;
 49 static struct i2c_client client_template;
 50 
 51 /* unique ID allocation */
 52 static int tea6420_id = 0;
 53 
 54 /* make a connection between the input 'i' and the output 'o'
 55    with gain 'g' for the tea6420-client 'client' (note: i = 6 means 'mute') */
 56 static int tea6420_switch(struct i2c_client *client, int i, int o, int g)
 57 {
 58         u8 byte = 0;
 59         int ret;
 60 
 61         dprintk("adr:0x%02x, i:%d, o:%d, g:%d\n", client->addr, i, o, g);
 62 
 63         /* check if the paramters are valid */
 64         if (i < 1 || i > 6 || o < 1 || o > 4 || g < 0 || g > 6 || g % 2 != 0)
 65                 return -1;
 66 
 67         byte  = ((o - 1) << 5);
 68         byte |= (i - 1);
 69 
 70         /* to understand this, have a look at the tea6420-specs (p.5) */
 71         switch (g) {
 72         case 0:
 73                 byte |= (3 << 3);
 74                 break;
 75         case 2:
 76                 byte |= (2 << 3);
 77                 break;
 78         case 4:
 79                 byte |= (1 << 3);
 80                 break;
 81         case 6:
 82                 break;
 83         }
 84 
 85         ret = i2c_smbus_write_byte(client, byte);
 86         if (ret) {
 87                 dprintk("i2c_smbus_write_byte() failed, ret:%d\n", ret);
 88                 return -EIO;
 89         }
 90         
 91         return 0;
 92 }
 93 
 94 /* this function is called by i2c_probe */
 95 static int tea6420_detect(struct i2c_adapter *adapter, int address, int kind)
 96 {
 97         struct i2c_client *client;
 98         int err = 0, i = 0;
 99 
100         /* let's see whether this adapter can support what we need */
101         if (0 == i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WRITE_BYTE)) {
102                 return 0;
103         }
104 
105         /* allocate memory for client structure */
106         client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
107         if (0 == client) {
108                 return -ENOMEM;
109         }
110         memset(client, 0x0, sizeof(struct i2c_client));
111 
112         /* fill client structure */
113         memcpy(client, &client_template, sizeof(struct i2c_client));
114         client->id = tea6420_id++;
115         client->addr = address;
116         client->adapter = adapter;
117 
118         /* tell the i2c layer a new client has arrived */
119         if (0 != (err = i2c_attach_client(client))) {
120                 kfree(client);
121                 return err;
122         }
123 
124         /* set initial values: set "mute"-input to all outputs at gain 0 */
125         err = 0;
126         for (i = 1; i < 5; i++) {
127                 err += tea6420_switch(client, 6, i, 0);
128         }
129         if (err) {
130                 dprintk("could not initialize tea6420\n");
131                 kfree(client);
132                 return -ENODEV;
133         }
134 
135         printk("tea6420: detected @ 0x%02x on adapter %s\n", address, &client->adapter->name[0]);
136 
137         return 0;
138 }
139 
140 static int attach(struct i2c_adapter *adapter)
141 {
142         /* let's see whether this is a know adapter we can attach to */
143         if (adapter->id != I2C_ALGO_SAA7146) {
144                 dprintk("refusing to probe on unknown adapter [name='%s',id=0x%x]\n", adapter->name, adapter->id);
145                 return -ENODEV;
146         }
147 
148         return i2c_probe(adapter, &addr_data, &tea6420_detect);
149 }
150 
151 static int detach(struct i2c_client *client)
152 {
153         int ret = i2c_detach_client(client);
154         kfree(client);
155         return ret;
156 }
157 
158 static int command(struct i2c_client *client, unsigned int cmd, void *arg)
159 {
160         struct tea6420_multiplex *a = (struct tea6420_multiplex *)arg;
161         int result = 0;
162 
163         switch (cmd) {
164         case TEA6420_SWITCH:
165                 result = tea6420_switch(client, a->in, a->out, a->gain);
166                 break;
167         default:
168                 return -ENOIOCTLCMD;
169         }
170 
171         return result;
172 }
173 
174 static struct i2c_driver driver = {
175         .owner  = THIS_MODULE,
176         .name   = "tea6420",
177         .id     = I2C_DRIVERID_TEA6420,
178         .flags  = I2C_DF_NOTIFY,
179         .attach_adapter = attach,
180         .detach_client  = detach,
181         .command        = command,
182 };
183 
184 static struct i2c_client client_template = {
185         I2C_DEVNAME("tea6420"),
186         .driver = &driver,
187 };
188 
189 static int __init this_module_init(void)
190 {
191         return i2c_add_driver(&driver);
192 }
193 
194 static void __exit this_module_exit(void)
195 {
196         i2c_del_driver(&driver);
197 }
198 
199 module_init(this_module_init);
200 module_exit(this_module_exit);
201 
202 MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
203 MODULE_DESCRIPTION("tea6420 driver");
204 MODULE_LICENSE("GPL");
205 
  This page was automatically generated by the LXR engine.