1 /*
2 * Driver for the i2c/i2s based TA3001C sound chip used
3 * on some Apple hardware. Also known as "tumbler".
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 * Modified by Christopher C. Chimelis <chris@debian.org>:
10 *
11 * TODO:
12 * -----
13 * * Enable DRC since the TiBook speakers are less than good
14 * * Enable control over input line 2 (is this connected?)
15 * * Play with the dual six-stage cascading biquad filtering to see how
16 * we can use it to our advantage (currently not implemented)
17 * * Reorganise driver a bit to make it cleaner and easier to work with
18 * (read: use the header file more :-P)
19 * * Implement sleep support
20 *
21 * Version 0.4:
22 * ------------
23 * * Balance control finally works (can someone document OSS better please?)
24 * * Moved to a struct for common values referenced in the driver
25 * * Put stubs in for sleep/wake-up support for now. This will take some
26 * experimentation to make sure that the timing is right, since the
27 * TAS hardware requires specific timing while enabling low-power mode.
28 * I may cheat for now and just reset the chip on wake-up, but I'd rather
29 * not if I don't have to.
30 *
31 * Version 0.3:
32 * ------------
33 * * Fixed volume control
34 * * Added bass and treble control
35 * * Added PCM line level control (mixer 1 in the TAS manual)
36 *
37 */
38
39 #include <linux/version.h>
40 #include <linux/module.h>
41 #include <linux/slab.h>
42 #include <linux/proc_fs.h>
43 #include <linux/ioport.h>
44 #include <linux/sysctl.h>
45 #include <linux/types.h>
46 #include <linux/i2c.h>
47 #include <linux/init.h>
48 #include <asm/uaccess.h>
49 #include <asm/errno.h>
50 #include <asm/io.h>
51 #include <asm/prom.h>
52
53 #include "dmasound.h"
54 #include "tas3001c.h"
55
56 #define I2C_DRIVERID_TAS (0xFEBA)
57
58 #define TAS_VERSION "0.3"
59 #define TAS_DATE "20011214"
60
61 #define TAS_SETTING_MAX 100
62
63 #define VOL_DEFAULT (((((TAS_SETTING_MAX*4)/5)<<0)<<8) | (((TAS_SETTING_MAX*4)/5)<<0))
64 #define INPUT_DEFAULT (((TAS_SETTING_MAX*4)/5)<<0)
65 #define BASS_DEFAULT ((TAS_SETTING_MAX/2)<<0)
66 #define TREBLE_DEFAULT ((TAS_SETTING_MAX/2)<<0)
67
68 static struct i2c_client * tumbler_client = NULL;
69
70 int tumbler_enter_sleep(void);
71 int tumbler_leave_sleep(void);
72
73 static int tas_attach_adapter(struct i2c_adapter *adapter);
74 static int tas_detect_client(struct i2c_adapter *adapter, int address);
75 static int tas_detach_client(struct i2c_client *client);
76
77 /* Unique ID allocation */
78 static int tas_id;
79 static int tas_initialized;
80
81 static struct device_node* tas_node;
82 static u8 tas_i2c_address = 0x34;
83
84 struct tas_data_t {
85 uint left_vol; /* left volume */
86 uint right_vol; /* right volume */
87 uint treble; /* treble */
88 uint bass; /* bass */
89 uint pcm_level; /* pcm level */
90 };
91
92 struct i2c_driver tas_driver = {
93 name: "TAS3001C driver V 0.3",
94 id: I2C_DRIVERID_TAS,
95 flags: I2C_DF_NOTIFY,
96 attach_adapter: &tas_attach_adapter,
97 detach_client: &tas_detach_client,
98 command: NULL,
99 inc_use: NULL, /* &tas_inc_use, */
100 dec_use: NULL /* &tas_dev_use */
101 };
102
103 int
tumbler_get_volume(uint * left_vol,uint * right_vol)104 tumbler_get_volume(uint * left_vol, uint *right_vol)
105 {
106 struct tas_data_t *data;
107
108 if (!tumbler_client)
109 return -1;
110
111 data = (struct tas_data_t *) (tumbler_client->data);
112 *left_vol = data->left_vol;
113 *right_vol = data->right_vol;
114
115 return 0;
116 }
117
118 int
tumbler_set_register(uint reg,uint size,char * block)119 tumbler_set_register(uint reg, uint size, char *block)
120 {
121 if (i2c_smbus_write_block_data(tumbler_client, reg, size, block) < 0) {
122 printk("tas3001c: I2C write failed \n");
123 return -1;
124 }
125 return 0;
126 }
127
128 int
tumbler_get_pcm_lvl(uint * pcm_lvl)129 tumbler_get_pcm_lvl(uint *pcm_lvl)
130 {
131 struct tas_data_t *data;
132
133 if (!tumbler_client)
134 return -1;
135
136 data = (struct tas_data_t *) (tumbler_client->data);
137 *pcm_lvl = data->pcm_level;
138
139 return 0;
140 }
141
142 int
tumbler_get_treble(uint * treble)143 tumbler_get_treble(uint *treble)
144 {
145 struct tas_data_t *data;
146
147 if (!tumbler_client)
148 return -1;
149
150 data = (struct tas_data_t *) (tumbler_client->data);
151 *treble = data->treble;
152
153 return 0;
154 }
155
156 int
tumbler_get_bass(uint * bass)157 tumbler_get_bass(uint *bass)
158 {
159 struct tas_data_t *data;
160
161 if (!tumbler_client)
162 return -1;
163
164 data = (struct tas_data_t *) (tumbler_client->data);
165 *bass = data->bass;
166
167 return 0;
168 }
169
170 int
tumbler_set_bass(uint bass)171 tumbler_set_bass(uint bass)
172 {
173 uint cur_bass_pers = bass;
174 char block;
175 struct tas_data_t *data;
176
177 if (!tumbler_client)
178 return -1;
179
180 data = (struct tas_data_t *) (tumbler_client->data);
181
182 bass &= 0xff;
183 if (bass > TAS_SETTING_MAX)
184 bass = TAS_SETTING_MAX;
185 bass = ((bass * 72) / TAS_SETTING_MAX) << 0;
186 bass = tas_bass_table[bass];
187 block = (bass >> 0) & 0xff;
188
189 if (tumbler_set_register(TAS_SET_BASS, &block) < 0) {
190 printk("tas3001c: failed to set bass \n");
191 return -1;
192 }
193 data->bass = cur_bass_pers;
194 return 0;
195 }
196
197 int
tumbler_set_treble(uint treble)198 tumbler_set_treble(uint treble)
199 {
200 uint cur_treble_pers = treble;
201 char block;
202 struct tas_data_t *data;
203
204 if (!tumbler_client)
205 return -1;
206
207 data = (struct tas_data_t *) (tumbler_client->data);
208
209 treble &= 0xff;
210 if (treble > TAS_SETTING_MAX)
211 treble = TAS_SETTING_MAX;
212 treble = ((treble * 72) / TAS_SETTING_MAX) << 0;
213 treble = tas_treble_table[treble];
214 block = (treble >> 0) & 0xff;
215
216 if (tumbler_set_register(TAS_SET_TREBLE, &block) < 0) {
217 printk("tas3001c: failed to set treble \n");
218 return -1;
219 }
220 data->treble = cur_treble_pers;
221 return 0;
222 }
223
224 int
tumbler_set_pcm_lvl(uint pcm_lvl)225 tumbler_set_pcm_lvl(uint pcm_lvl)
226 {
227 uint pcm_lvl_pers = pcm_lvl;
228 unsigned char block[3];
229 struct tas_data_t *data;
230
231 if (!tumbler_client)
232 return -1;
233
234 data = (struct tas_data_t *) (tumbler_client->data);
235
236 pcm_lvl &= 0xff;
237 if (pcm_lvl > TAS_SETTING_MAX)
238 pcm_lvl = TAS_SETTING_MAX;
239 pcm_lvl = ((pcm_lvl * 176) / TAS_SETTING_MAX) << 0;
240
241 pcm_lvl = tas_input_table[pcm_lvl];
242
243 block[0] = (pcm_lvl >> 16) & 0xff;
244 block[1] = (pcm_lvl >> 8) & 0xff;
245 block[2] = (pcm_lvl >> 0) & 0xff;
246
247 if (tumbler_set_register(TAS_SET_MIXER1, block) < 0) {
248 printk("tas3001c: failed to set input level \n");
249 return -1;
250 }
251 data->pcm_level = pcm_lvl_pers;
252
253 return 0;
254 }
255
256 int
tumbler_set_volume(uint left_vol,uint right_vol)257 tumbler_set_volume(uint left_vol, uint right_vol)
258 {
259 uint left_vol_pers = left_vol;
260 uint right_vol_pers = right_vol;
261 unsigned char block[6];
262 struct tas_data_t *data;
263
264 if (!tumbler_client)
265 return -1;
266
267 data = (struct tas_data_t *) (tumbler_client->data);
268
269 left_vol &= 0xff;
270 if (left_vol > TAS_SETTING_MAX)
271 left_vol = TAS_SETTING_MAX;
272
273 right_vol = (right_vol >> 8) & 0xff;
274 if (right_vol > TAS_SETTING_MAX)
275 right_vol = TAS_SETTING_MAX;
276
277 left_vol = ((left_vol * 176) / TAS_SETTING_MAX) << 0;
278 right_vol = ((right_vol * 176) / TAS_SETTING_MAX) << 0;
279
280 left_vol = tas_volume_table[left_vol];
281 right_vol = tas_volume_table[right_vol];
282
283 block[0] = (left_vol >> 16) & 0xff;
284 block[1] = (left_vol >> 8) & 0xff;
285 block[2] = (left_vol >> 0) & 0xff;
286
287 block[3] = (right_vol >> 16) & 0xff;
288 block[4] = (right_vol >> 8) & 0xff;
289 block[5] = (right_vol >> 0) & 0xff;
290
291 if (tumbler_set_register(TAS_SET_VOLUME, block) < 0) {
292 printk("tas3001c: failed to set volume \n");
293 return -1;
294 }
295 data->left_vol = left_vol_pers;
296 data->right_vol = right_vol_pers;
297
298 return 0;
299 }
300
301 int
tumbler_leave_sleep(void)302 tumbler_leave_sleep(void)
303 {
304 /* Stub for now, but I have the details on low-power mode */
305 if (!tumbler_client)
306 return -1;
307
308 return 0;
309 }
310
311 int
tumbler_enter_sleep(void)312 tumbler_enter_sleep(void)
313 {
314 /* Stub for now, but I have the details on low-power mode */
315 if (!tumbler_client)
316 return -1;
317
318 return 0;
319 }
320
321 static int
tas_attach_adapter(struct i2c_adapter * adapter)322 tas_attach_adapter(struct i2c_adapter *adapter)
323 {
324 if (!strncmp(adapter->name, "mac-io", 6))
325 tas_detect_client(adapter, tas_i2c_address);
326
327 return 0;
328 }
329
330 static int
tas_init_client(struct i2c_client * new_client)331 tas_init_client(struct i2c_client * new_client)
332 {
333 /* Make sure something answers on the i2c bus
334 */
335
336 if (i2c_smbus_write_byte_data(new_client, 1, (1<<6)+(2<<4)+(2<<2)+0) < 0)
337 return -1;
338
339 tumbler_client = new_client;
340
341 tumbler_set_volume(VOL_DEFAULT, VOL_DEFAULT);
342 tumbler_set_pcm_lvl(INPUT_DEFAULT);
343 tumbler_set_bass(BASS_DEFAULT);
344 tumbler_set_treble(TREBLE_DEFAULT);
345
346 return 0;
347 }
348
349 static int
tas_detect_client(struct i2c_adapter * adapter,int address)350 tas_detect_client(struct i2c_adapter *adapter, int address)
351 {
352 int rc = 0;
353 struct i2c_client *new_client;
354 struct tas_data_t *data;
355 const char *client_name = "tas 3001c Digital Equalizer";
356
357 new_client = kmalloc(
358 sizeof(struct i2c_client) + sizeof(struct tas_data_t),
359 GFP_KERNEL);
360 if (!new_client) {
361 rc = -ENOMEM;
362 goto bail;
363 }
364
365 /* This is tricky, but it will set the data to the right value. */
366 new_client->data = new_client + 1;
367 data = (struct tas_data_t *) (new_client->data);
368
369 new_client->addr = address;
370 new_client->data = data;
371 new_client->adapter = adapter;
372 new_client->driver = &tas_driver;
373 new_client->flags = 0;
374
375 strcpy(new_client->name,client_name);
376
377 new_client->id = tas_id++; /* Automatically unique */
378
379 if (tas_init_client(new_client)) {
380 rc = -ENODEV;
381 goto bail;
382 }
383
384 /* Tell the i2c layer a new client has arrived */
385 if (i2c_attach_client(new_client)) {
386 rc = -ENODEV;
387 goto bail;
388 }
389 bail:
390 if (rc && new_client)
391 kfree(new_client);
392 return rc;
393 }
394
395 static int
tas_detach_client(struct i2c_client * client)396 tas_detach_client(struct i2c_client *client)
397 {
398 if (client == tumbler_client)
399 tumbler_client = NULL;
400
401 i2c_detach_client(client);
402 kfree(client);
403
404 return 0;
405 }
406
407 int
tas_cleanup(void)408 tas_cleanup(void)
409 {
410 if (!tas_initialized)
411 return -ENODEV;
412 i2c_del_driver(&tas_driver);
413 tas_initialized = 0;
414
415 return 0;
416 }
417
418 int
tas_init(void)419 tas_init(void)
420 {
421 int rc;
422 u32* paddr;
423
424 if (tas_initialized)
425 return 0;
426
427 tas_node = find_devices("deq");
428 if (tas_node == NULL)
429 return -ENODEV;
430
431 printk(KERN_INFO "tas3001c driver version %s (%s)\n",TAS_VERSION,TAS_DATE);
432 paddr = (u32 *)get_property(tas_node, "i2c-address", NULL);
433 if (paddr) {
434 tas_i2c_address = (*paddr) >> 1;
435 printk(KERN_INFO "using i2c address: 0x%x from device-tree\n",
436 tas_i2c_address);
437 } else
438 printk(KERN_INFO "using i2c address: 0x%x (default)\n", tas_i2c_address);
439
440 if ((rc = i2c_add_driver(&tas_driver))) {
441 printk("tas3001c: Driver registration failed, module not inserted.\n");
442 tas_cleanup();
443 return rc;
444 }
445 tas_initialized = 1;
446 return 0;
447 }
448