ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / oss / dmasound / dac3550a.c
1 /*
2  * Driver for the i2c/i2s based DAC3550a sound chip used
3  * on some Apple iBooks. Also known as "DACA".
4  *
5  *  This file is subject to the terms and conditions of the GNU General Public
6  *  License.  See the file COPYING in the main directory of this archive
7  *  for more details.
8  */
9
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/proc_fs.h>
13 #include <linux/ioport.h>
14 #include <linux/sysctl.h>
15 #include <linux/types.h>
16 #include <linux/i2c.h>
17 #include <linux/init.h>
18 #include <asm/uaccess.h>
19 #include <asm/errno.h>
20 #include <asm/io.h>
21
22 #include "dmasound.h"
23
24 /* FYI: This code was derived from the tas3001c.c Texas/Tumbler mixer
25  * control code, as well as info derived from the AppleDACAAudio driver
26  * from Darwin CVS (main thing I derived being register numbers and 
27  * values, as well as when to make the calls). */
28
29 #define I2C_DRIVERID_DACA (0xFDCB)
30
31 #define DACA_VERSION    "0.1"
32 #define DACA_DATE "20010930"
33
34 static int cur_left_vol;
35 static int cur_right_vol;
36 static struct i2c_client *daca_client;
37
38 static int daca_attach_adapter(struct i2c_adapter *adapter);
39 static int daca_detect_client(struct i2c_adapter *adapter, int address);
40 static int daca_detach_client(struct i2c_client *client);
41
42 /* Unique ID allocation */
43 static int daca_id;
44
45 struct i2c_driver daca_driver = {  
46         .owner                  = THIS_MODULE,
47         .name                   = "DAC3550A driver  V " DACA_VERSION,
48         .id                     = I2C_DRIVERID_DACA,
49         .flags                  = I2C_DF_NOTIFY,
50         .attach_adapter         = daca_attach_adapter,
51         .detach_client          = daca_detach_client,
52 };
53
54 #define VOL_MAX ((1<<20) - 1)
55
56 void daca_get_volume(uint * left_vol, uint  *right_vol)
57 {
58         *left_vol = cur_left_vol >> 5;
59         *right_vol = cur_right_vol >> 5;
60 }
61
62 int daca_set_volume(uint left_vol, uint right_vol)
63 {
64         unsigned short voldata;
65   
66         if (!daca_client)
67                 return -1;
68
69         /* Derived from experience, not from any specific values */
70         left_vol <<= 5;
71         right_vol <<= 5;
72
73         if (left_vol > VOL_MAX)
74                 left_vol = VOL_MAX;
75         if (right_vol > VOL_MAX)
76                 right_vol = VOL_MAX;
77
78         voldata = ((left_vol >> 14)  & 0x3f) << 8;
79         voldata |= (right_vol >> 14)  & 0x3f;
80   
81         if (i2c_smbus_write_word_data(daca_client, 2, voldata) < 0) {
82                 printk("daca: failed to set volume \n");
83                 return -1; 
84         }
85
86         cur_left_vol = left_vol;
87         cur_right_vol = right_vol;
88   
89         return 0;
90 }
91
92 int daca_leave_sleep(void)
93 {
94         if (!daca_client)
95                 return -1;
96   
97         /* Do a short sleep, just to make sure I2C bus is awake and paying
98          * attention to us
99          */
100         wait_ms(20);
101         /* Write the sample rate reg the value it needs */
102         i2c_smbus_write_byte_data(daca_client, 1, 8);
103         daca_set_volume(cur_left_vol >> 5, cur_right_vol >> 5);
104         /* Another short delay, just to make sure the other I2C bus writes
105          * have taken...
106          */
107         wait_ms(20);
108         /* Write the global config reg - invert right power amp,
109          * DAC on, use 5-volt mode */
110         i2c_smbus_write_byte_data(daca_client, 3, 0x45);
111
112         return 0;
113 }
114
115 int daca_enter_sleep(void)
116 {
117         if (!daca_client)
118                 return -1;
119
120         i2c_smbus_write_byte_data(daca_client, 1, 8);
121         daca_set_volume(cur_left_vol >> 5, cur_right_vol >> 5);
122
123         /* Write the global config reg - invert right power amp,
124          * DAC on, enter low-power mode, use 5-volt mode
125          */
126         i2c_smbus_write_byte_data(daca_client, 3, 0x65);
127
128         return 0;
129 }
130
131 static int daca_attach_adapter(struct i2c_adapter *adapter)
132 {
133         if (!strncmp(adapter->name, "mac-io", 6))
134                 daca_detect_client(adapter, 0x4d);
135         return 0;
136 }
137
138 static int daca_init_client(struct i2c_client * new_client)
139 {
140         /* 
141          * Probe is not working with the current i2c-keywest
142          * driver. We try to use addr 0x4d on each adapters
143          * instead, by setting the format register.
144          * 
145          * FIXME: I'm sure that can be obtained from the
146          * device-tree. --BenH.
147          */
148   
149         /* Write the global config reg - invert right power amp,
150          * DAC on, use 5-volt mode
151          */
152         if (i2c_smbus_write_byte_data(new_client, 3, 0x45))
153                 return -1;
154
155         i2c_smbus_write_byte_data(new_client, 1, 8);
156         daca_client = new_client;
157         daca_set_volume(15000, 15000);
158
159         return 0;
160 }
161
162 static int daca_detect_client(struct i2c_adapter *adapter, int address)
163 {
164         const char *client_name = "DAC 3550A Digital Equalizer";
165         struct i2c_client *new_client;
166         int rc = -ENODEV;
167
168         new_client = kmalloc(sizeof(*new_client), GFP_KERNEL);
169         if (!new_client)
170                 return -ENOMEM;
171         memset(new_client, 0, sizeof(*new_client));
172
173         new_client->addr = address;
174         new_client->adapter = adapter;
175         new_client->driver = &daca_driver;
176         new_client->flags = 0;
177         strcpy(new_client->name, client_name);
178         new_client->id = daca_id++; /* racy... */
179
180         if (daca_init_client(new_client))
181                 goto bail;
182
183         /* Tell the i2c layer a new client has arrived */
184         if (i2c_attach_client(new_client))
185                 goto bail;
186
187         return 0;
188  bail:
189         kfree(new_client);
190         return rc;
191 }
192
193
194 static int daca_detach_client(struct i2c_client *client)
195 {
196         if (client == daca_client)
197                 daca_client = NULL;
198
199         i2c_detach_client(client);
200         kfree(client);
201         return 0;
202 }
203
204 void daca_cleanup(void)
205 {
206         i2c_del_driver(&daca_driver);
207 }
208
209 int daca_init(void)
210 {
211         printk("dac3550a driver version %s (%s)\n",DACA_VERSION,DACA_DATE);
212         return i2c_add_driver(&daca_driver);
213 }