]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/i2c/chips/tlv320aic23.c
Merged with ../linux-2.6
[linux-2.6-omap-h63xx.git] / drivers / i2c / chips / tlv320aic23.c
1 /*
2  *   Texas Instrumens TLV320AIC23 audio codec's i2c interface.
3  *   
4  *   Copyright (c) by Kai Svahn <kai.svahn@nokia.com>
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  *
20  */
21
22 #include <linux/config.h>
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/i2c.h>
26 #include <linux/slab.h>
27 #include <asm/io.h>
28 #include <asm/arch/aic23.h>
29
30 #define TLV320AIC23_VERSION     "0.1"
31 #define TLV320AIC23_DATE        "12-Aug-2004"
32
33 /* I2C Addresses to scan */
34 static unsigned short normal_i2c[] = { TLV320AIC23ID1, TLV320AIC23ID2, I2C_CLIENT_END };
35 static unsigned short normal_i2c_range[] = { I2C_CLIENT_END };
36
37 /* This makes all addr_data:s */
38 I2C_CLIENT_INSMOD;
39
40 static struct i2c_driver tlv320aic23_driver; 
41 static struct i2c_client *new_client;
42 //static struct i2c_client *client;
43
44 static int _tlv320aic23_write_value(struct i2c_client *client, u8 reg, u16 value)
45 {
46         u8 val, wreg;
47         
48         /* TLV320AIC23 has 7 bit address and 9 bits of data
49          * so we need to switch one data bit into reg and rest
50          * of data into val
51          */
52         
53         wreg = (reg << 1);
54         val = (0x01 & (value >> 8));
55         wreg = (wreg | val);
56         val = (0x00ff & value);
57         
58         return i2c_smbus_write_byte_data(client, wreg, val);
59 }
60
61 int tlv320aic23_write_value(u8 reg, u16 value)
62 {
63         static struct i2c_client *client;
64         client = new_client;
65         _tlv320aic23_write_value(client, reg, value);
66         
67         return 0;
68 }
69
70 static int tlv320aic23_detect_client(struct i2c_adapter *adapter, int address, 
71                                      int kind)
72 {
73         int err = 0;
74         const char *client_name = "TLV320AIC23 Audio Codec";
75         
76         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA | 
77                                      I2C_FUNC_SMBUS_WRITE_BYTE)) {
78                 printk(KERN_WARNING "%s functinality check failed\n", client_name);
79                 return err;
80         }
81         
82         if (!(new_client = kmalloc(sizeof(struct i2c_client),
83                                    GFP_KERNEL))) {
84                 err = -ENOMEM;
85                 printk(KERN_WARNING "Couldn't allocate memory for %s\n", client_name);
86                 return err;
87         }
88         
89         memset(new_client, 0x00, sizeof(struct i2c_client));
90         new_client->addr = address;
91         new_client->adapter = adapter;
92         new_client->driver = &tlv320aic23_driver;
93         new_client->flags = 0;
94         strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
95         
96         if ((err = i2c_attach_client(new_client))) {
97                 printk(KERN_WARNING "Couldn't attach %s\n", client_name);
98                 kfree(new_client);
99                 return err;
100         }
101         
102         return 0;
103 }
104         
105 static int tlv320aic23_detach_client(struct i2c_client *client)
106 {
107         int err;
108         
109         if ((err = i2c_detach_client(client))) {
110                 printk("tlv320aic23.o: Client deregistration failed, client not detached.\n");
111                 return err;
112         }
113         
114         kfree(client);
115         return 0;
116 }
117
118 static int tlv320aic23_attach_adapter(struct i2c_adapter *adapter)
119 {
120         int res;
121         
122         res = i2c_probe(adapter, &addr_data, &tlv320aic23_detect_client);
123         return res;
124 }
125
126 /*-----------------------------------------------------------------------*/
127
128 static struct i2c_driver tlv320aic23_driver = {
129         .owner          = THIS_MODULE, 
130         .name           = "OMAP+TLV320AIC23 codec",
131         .id             = I2C_DRIVERID_EXP0,           /* Experimental ID */
132         .flags          = I2C_DF_NOTIFY,
133         .attach_adapter = tlv320aic23_attach_adapter,
134         .detach_client  = tlv320aic23_detach_client,
135 };
136
137 /*
138  *  INIT part
139  */
140
141 static int __init tlv320aic23_init(void)
142 {
143         int res;
144         struct i2c_client *client = client;
145         
146         if ((res = i2c_add_driver(&tlv320aic23_driver))) {
147                 printk("tlv320aic23 i2c: Driver registration failed, module not inserted.\n");
148                 return res;
149         }
150         
151         printk("TLV320AIC23 I2C version %s (%s)\n", TLV320AIC23_VERSION, 
152                TLV320AIC23_DATE);
153         
154         return 0;
155 }
156
157 static void __exit tlv320aic23_exit(void)
158 {
159         int res;
160
161         if ((res = i2c_del_driver(&tlv320aic23_driver))) 
162                 printk("tlv320aic23 i2c: Driver remove failed, module not removed.\n");
163 }
164
165 MODULE_AUTHOR("Kai Svahn <kai.svahn@nokia.com>");
166 MODULE_DESCRIPTION("I2C interface for TLV320AIC23 codec.");
167 MODULE_LICENSE("GPL");
168
169 module_init(tlv320aic23_init)
170 module_exit(tlv320aic23_exit)
171
172 EXPORT_SYMBOL(tlv320aic23_write_value);