]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/i2c/chips/tlv320aic23.c
43c4bac5b53f035d8e3eb1787f98b44a77b9397d
[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
36 /* This makes all addr_data:s */
37 I2C_CLIENT_INSMOD;
38
39 static struct i2c_driver tlv320aic23_driver; 
40 static struct i2c_client *new_client;
41 //static struct i2c_client *client;
42
43 static int _tlv320aic23_write_value(struct i2c_client *client, u8 reg, u16 value)
44 {
45         u8 val, wreg;
46         
47         /* TLV320AIC23 has 7 bit address and 9 bits of data
48          * so we need to switch one data bit into reg and rest
49          * of data into val
50          */
51         
52         wreg = (reg << 1);
53         val = (0x01 & (value >> 8));
54         wreg = (wreg | val);
55         val = (0x00ff & value);
56         
57         return i2c_smbus_write_byte_data(client, wreg, val);
58 }
59
60 int tlv320aic23_write_value(u8 reg, u16 value)
61 {
62         static struct i2c_client *client;
63         client = new_client;
64         _tlv320aic23_write_value(client, reg, value);
65         
66         return 0;
67 }
68
69 static int tlv320aic23_detect_client(struct i2c_adapter *adapter, int address, 
70                                      int kind)
71 {
72         int err = 0;
73         const char *client_name = "TLV320AIC23 Audio Codec";
74         
75         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA | 
76                                      I2C_FUNC_SMBUS_WRITE_BYTE)) {
77                 printk(KERN_WARNING "%s functinality check failed\n", client_name);
78                 return err;
79         }
80         
81         if (!(new_client = kmalloc(sizeof(struct i2c_client),
82                                    GFP_KERNEL))) {
83                 err = -ENOMEM;
84                 printk(KERN_WARNING "Couldn't allocate memory for %s\n", client_name);
85                 return err;
86         }
87         
88         memset(new_client, 0x00, sizeof(struct i2c_client));
89         new_client->addr = address;
90         new_client->adapter = adapter;
91         new_client->driver = &tlv320aic23_driver;
92         new_client->flags = 0;
93         strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
94         
95         if ((err = i2c_attach_client(new_client))) {
96                 printk(KERN_WARNING "Couldn't attach %s\n", client_name);
97                 kfree(new_client);
98                 return err;
99         }
100         
101         return 0;
102 }
103         
104 static int tlv320aic23_detach_client(struct i2c_client *client)
105 {
106         int err;
107         
108         if ((err = i2c_detach_client(client))) {
109                 printk("tlv320aic23.o: Client deregistration failed, client not detached.\n");
110                 return err;
111         }
112         
113         kfree(client);
114         return 0;
115 }
116
117 static int tlv320aic23_attach_adapter(struct i2c_adapter *adapter)
118 {
119         int res;
120         
121         res = i2c_probe(adapter, &addr_data, &tlv320aic23_detect_client);
122         return res;
123 }
124
125 /*-----------------------------------------------------------------------*/
126
127 static struct i2c_driver tlv320aic23_driver = {
128         .owner          = THIS_MODULE, 
129         .name           = "OMAP+TLV320AIC23 codec",
130         .id             = I2C_DRIVERID_EXP0,           /* Experimental ID */
131         .flags          = I2C_DF_NOTIFY,
132         .attach_adapter = tlv320aic23_attach_adapter,
133         .detach_client  = tlv320aic23_detach_client,
134 };
135
136 /*
137  *  INIT part
138  */
139
140 static int __init tlv320aic23_init(void)
141 {
142         int res;
143         struct i2c_client *client = client;
144         
145         if ((res = i2c_add_driver(&tlv320aic23_driver))) {
146                 printk("tlv320aic23 i2c: Driver registration failed, module not inserted.\n");
147                 return res;
148         }
149         
150         printk("TLV320AIC23 I2C version %s (%s)\n", TLV320AIC23_VERSION, 
151                TLV320AIC23_DATE);
152         
153         return 0;
154 }
155
156 static void __exit tlv320aic23_exit(void)
157 {
158         int res;
159
160         if ((res = i2c_del_driver(&tlv320aic23_driver))) 
161                 printk("tlv320aic23 i2c: Driver remove failed, module not removed.\n");
162 }
163
164 MODULE_AUTHOR("Kai Svahn <kai.svahn@nokia.com>");
165 MODULE_DESCRIPTION("I2C interface for TLV320AIC23 codec.");
166 MODULE_LICENSE("GPL");
167
168 module_init(tlv320aic23_init)
169 module_exit(tlv320aic23_exit)
170
171 EXPORT_SYMBOL(tlv320aic23_write_value);