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