2 * Texas Instrumens TLV320AIC23 audio codec's i2c interface.
4 * Copyright (c) by Kai Svahn <kai.svahn@nokia.com>
5 * Copyright (c) by Jussi Laako <jussi.laako@nokia.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/config.h>
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/i2c.h>
27 #include <linux/slab.h>
28 #include <linux/device.h>
29 #include <linux/platform_device.h>
31 #include <asm/arch/aic23.h>
32 #include <asm/arch/mcbsp.h>
34 #define TLV320AIC23_VERSION "1.8"
35 #define TLV320AIC23_DATE "10-Feb-2006"
40 #define OUTPUT_VOLUME_MIN LHV_MIN
41 #define OUTPUT_VOLUME_MAX LHV_MAX
42 #define OUTPUT_VOLUME_RANGE (OUTPUT_VOLUME_MAX - OUTPUT_VOLUME_MIN)
43 #define INPUT_VOLUME_MIN LIV_MIN
44 #define INPUT_VOLUME_MAX LIV_MAX
45 #define INPUT_VOLUME_RANGE (INPUT_VOLUME_MAX - INPUT_VOLUME_MIN)
47 /* I2C Addresses to scan */
48 static unsigned short normal_i2c[] = { TLV320AIC23ID1, TLV320AIC23ID2, \
50 /*static unsigned short normal_i2c_range[] = { I2C_CLIENT_END };*/
52 /* This makes all addr_data:s */
55 static struct i2c_driver aic23_driver;
56 static struct i2c_client *new_client;
59 static struct aic23_info {
62 u16 input_gain_reg_left;
63 u16 input_gain_reg_right;
64 u16 power; /* For POWER_DOWN_CONTROL_ADDR */
65 u16 mask; /* For ANALOG_AUDIO_CONTROL_ADDR */
73 static int _aic23_write_value(struct i2c_client *client, u8 reg, u16 value)
77 /* TLV320AIC23 has 7 bit address and 9 bits of data
78 * so we need to switch one data bit into reg and rest
83 val = (0x01 & (value >> 8));
85 val = (0x00ff & value);
87 return i2c_smbus_write_byte_data(client, wreg, val);
90 int aic23_write_value(u8 reg, u16 value)
92 static struct i2c_client *client;
94 _aic23_write_value(client, reg, value);
99 static int aic23_detect_client(struct i2c_adapter *adapter, int address,
103 const char *client_name = "TLV320AIC23 Audio Codec";
105 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA |
106 I2C_FUNC_SMBUS_WRITE_BYTE)) {
107 printk(KERN_WARNING "%s functinality check failed\n",
112 if (!(new_client = kmalloc(sizeof(struct i2c_client),
115 printk(KERN_WARNING "Couldn't allocate memory for %s\n",
120 memset(new_client, 0x00, sizeof(struct i2c_client));
121 new_client->addr = address;
122 new_client->adapter = adapter;
123 new_client->driver = &aic23_driver;
124 new_client->flags = 0;
125 strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
127 if ((err = i2c_attach_client(new_client))) {
128 printk(KERN_WARNING "Couldn't attach %s\n", client_name);
135 static int aic23_detach_client(struct i2c_client *client)
139 if ((err = i2c_detach_client(client))) {
140 printk("aic23.o: Client deregistration failed, \
141 client not detached.\n");
148 static int aic23_attach_adapter(struct i2c_adapter *adapter)
152 res = i2c_probe(adapter, &addr_data, &aic23_detect_client);
156 static struct i2c_driver aic23_driver = {
158 .name = "OMAP+TLV320AIC23 codec",
159 /*.flags = I2C_DF_NOTIFY,*/
161 .id = I2C_DRIVERID_MISC, /* Experimental ID */
162 .attach_adapter = aic23_attach_adapter,
163 .detach_client = aic23_detach_client,
167 * Configures the McBSP3 which is used to send clock to the AIC23 codec.
168 * The input clock rate from DSP is 12MHz.
169 * The DSP clock must be on before this is called.
171 static int omap_mcbsp3_aic23_clock_init(void)
175 /* enable 12MHz clock to mcbsp 1 & 3 */
176 __raw_writew(__raw_readw(DSP_IDLECT2) | (1<<1), DSP_IDLECT2);
177 __raw_writew(__raw_readw(DSP_RSTCT2) | 1 | 1<<1, DSP_RSTCT2);
179 /* disable sample rate generator */
180 OMAP_MCBSP_WRITE(OMAP1610_MCBSP3_BASE, SPCR1, 0x0000);
181 OMAP_MCBSP_WRITE(OMAP1610_MCBSP3_BASE, SPCR2, 0x0000);
183 /* pin control register */
184 OMAP_MCBSP_WRITE(OMAP1610_MCBSP3_BASE, PCR0,(CLKXM | CLKXP | CLKRP));
186 /* configure srg to send 12MHz pulse from dsp peripheral clock */
187 OMAP_MCBSP_WRITE(OMAP1610_MCBSP3_BASE, SRGR1, 0x0000);
188 OMAP_MCBSP_WRITE(OMAP1610_MCBSP3_BASE, SRGR2, CLKSM);
190 /* enable sample rate generator */
191 w = OMAP_MCBSP_READ(OMAP1610_MCBSP3_BASE, SPCR2);
192 OMAP_MCBSP_WRITE(OMAP1610_MCBSP3_BASE, SPCR2, (w | FREE | GRST));
193 printk("Clock enabled to MCBSP1 & 3 \n");
198 static void update_volume_left(int volume)
201 val = ((volume * OUTPUT_VOLUME_RANGE) / 100) + OUTPUT_VOLUME_MIN;
202 aic23_write_value(LEFT_CHANNEL_VOLUME_ADDR, val);
203 aic23_info_l.volume_reg_left = volume;
206 static void update_volume_right(int volume)
209 val = ((volume * OUTPUT_VOLUME_RANGE) / 100) + OUTPUT_VOLUME_MIN;
210 aic23_write_value(RIGHT_CHANNEL_VOLUME_ADDR, val);
211 aic23_info_l.volume_reg_right = volume;
214 static void set_mic(int mic_en)
219 aic23_info_l.power = OSC_OFF | LINE_OFF;
221 aic23_info_l.mask &= ~MICM_MUTED;
222 aic23_info_l.mask |= MICB_20DB; /* STE_ENABLED */
225 OSC_OFF | ADC_OFF | MIC_OFF | LINE_OFF;
228 DAC_SELECTED | INSEL_MIC | MICM_MUTED;
230 aic23_write_value(POWER_DOWN_CONTROL_ADDR,
232 aic23_write_value(DIGITAL_AUDIO_CONTROL_ADDR, dg_ctrl);
233 aic23_write_value(ANALOG_AUDIO_CONTROL_ADDR,
235 aic23_info_l.mic_enable = mic_en;
237 printk(KERN_INFO "aic23 mic state: %i\n", mic_en);
240 static void aic23_init_power(void)
242 aic23_write_value(RESET_CONTROL_ADDR, 0x00);
244 if (aic23_info_l.initialized == 0) {
245 aic23_write_value(LEFT_CHANNEL_VOLUME_ADDR, LHV_MIN);
246 aic23_write_value(RIGHT_CHANNEL_VOLUME_ADDR, LHV_MIN);
249 update_volume_left(aic23_info_l.volume_reg_left);
250 update_volume_right(aic23_info_l.volume_reg_right);
253 aic23_info_l.mask = DAC_SELECTED | INSEL_MIC | MICM_MUTED;
254 aic23_write_value(ANALOG_AUDIO_CONTROL_ADDR,
256 aic23_write_value(DIGITAL_AUDIO_CONTROL_ADDR, 0x00);
257 aic23_write_value(DIGITAL_AUDIO_FORMAT_ADDR, LRP_ON | FOR_DSP);
258 aic23_write_value(SAMPLE_RATE_CONTROL_ADDR, USB_CLK_ON);
259 aic23_write_value(DIGITAL_INTERFACE_ACT_ADDR, ACT_ON);
260 aic23_info_l.power = OSC_OFF | ADC_OFF | MIC_OFF | LINE_OFF;
261 aic23_write_value(POWER_DOWN_CONTROL_ADDR,
264 /* enable mic input */
265 if (aic23_info_l.mic_enable)
266 set_mic(aic23_info_l.mic_enable);
268 printk(KERN_INFO "aic23_init_power() done\n");
271 void aic23_power_down(void)
273 if (aic23_info_l.initialized) {
274 printk("aic23 powering down\n");
275 aic23_write_value(POWER_DOWN_CONTROL_ADDR, 0xff);
277 aic23_info_l.power_down = 1;
280 void aic23_power_up(void)
282 if (aic23_info_l.initialized) {
283 printk("aic23 powering up\n");
286 aic23_info_l.power_down = 0;
289 /*----------------------------------------------------------------------*/
290 /* sysfs initializations */
291 /*----------------------------------------------------------------------*/
293 static ssize_t store_volume_left(struct device *dev,
294 struct device_attribute *attr,
295 const char *buf, size_t count)
299 sscanf(buf, "%i", &volume);
301 if (volume < MIN_VOL) {
304 } else if (volume > MIN_VOL && aic23_info_l.power_down) {
305 aic23_info_l.volume_reg_left = volume;
309 if (volume > MAX_VOL)
312 update_volume_left(volume);
316 static ssize_t show_volume_left(struct device *dev,
317 struct device_attribute *attr, char *buf)
319 return sprintf(buf, "%u\n", aic23_info_l.volume_reg_left);
322 static DEVICE_ATTR(volume_left, S_IRUGO | S_IWUGO,
323 show_volume_left, store_volume_left);
325 static ssize_t store_volume_right(struct device *dev,
326 struct device_attribute *attr,
327 const char *buf, size_t count)
331 sscanf(buf, "%i", &volume);
332 if (volume < MIN_VOL) {
335 } else if (volume > MIN_VOL && aic23_info_l.power_down) {
336 aic23_info_l.volume_reg_right = volume;
340 if (volume > MAX_VOL)
343 update_volume_right(volume);
347 static ssize_t show_volume_right(struct device *dev,
348 struct device_attribute *attr, char *buf)
350 return sprintf(buf, "%u\n", aic23_info_l.volume_reg_right);
353 static DEVICE_ATTR(volume_right, S_IRUGO | S_IWUGO,
354 show_volume_right, store_volume_right);
356 static ssize_t store_gain_left(struct device *dev,
357 struct device_attribute *attr,
358 const char *buf, size_t count)
363 sscanf(buf, "%u", &gain);
367 val = ((gain * INPUT_VOLUME_RANGE) / 100) + INPUT_VOLUME_MIN;
368 aic23_write_value(LEFT_LINE_VOLUME_ADDR, val);
369 aic23_info_l.input_gain_reg_left = gain;
374 static ssize_t show_gain_left(struct device *dev,
375 struct device_attribute *attr, char *buf)
377 return sprintf(buf, "%u\n", aic23_info_l.input_gain_reg_left);
380 static DEVICE_ATTR(gain_left, S_IRUGO | S_IWUSR, show_gain_left,
383 static ssize_t store_gain_right(struct device *dev,
384 struct device_attribute *attr,
385 const char *buf, size_t count)
390 sscanf(buf, "%u", &gain);
394 val = ((gain * INPUT_VOLUME_RANGE) / 100) + INPUT_VOLUME_MIN;
395 aic23_write_value(RIGHT_LINE_VOLUME_ADDR, val);
396 aic23_info_l.input_gain_reg_right = gain;
401 static ssize_t show_gain_right(struct device *dev,
402 struct device_attribute *attr, char *buf)
404 return sprintf(buf, "%u\n", aic23_info_l.input_gain_reg_right);
407 static DEVICE_ATTR(gain_right, S_IRUGO | S_IWUSR, show_gain_right,
410 static ssize_t store_mic_loopback(struct device *dev,
411 struct device_attribute *attr,
412 const char *buf, size_t count)
416 sscanf(buf, "%i", &mic);
418 aic23_write_value(POWER_DOWN_CONTROL_ADDR, \
419 OSC_OFF | ADC_OFF | LINE_OFF);
420 aic23_info_l.mask = STE_ENABLED | DAC_SELECTED \
421 | INSEL_MIC | MICB_20DB;
422 aic23_write_value(ANALOG_AUDIO_CONTROL_ADDR,
427 aic23_write_value(POWER_DOWN_CONTROL_ADDR, \
428 OSC_OFF | ADC_OFF | MIC_OFF | LINE_OFF);
431 aic23_info_l.mic_loopback = mic;
436 static ssize_t show_mic_loopback(struct device *dev,
437 struct device_attribute *attr, char *buf)
439 return sprintf(buf, "%i\n", aic23_info_l.mic_loopback);
442 static DEVICE_ATTR(mic_loopback, S_IRUGO | S_IWUSR,
443 show_mic_loopback, store_mic_loopback);
445 static ssize_t store_st_attenuation(struct device *dev,
446 struct device_attribute *attr,
447 const char *buf, size_t count)
452 sscanf(buf, "%u", &sta);
456 tmp = aic23_info_l.mask;
459 aic23_info_l.mask = tmp | STA_REG(sta);
460 aic23_write_value(ANALOG_AUDIO_CONTROL_ADDR,
462 aic23_info_l.sta = sta;
467 static ssize_t show_st_attenuation(struct device *dev,
468 struct device_attribute *attr, char *buf)
470 return sprintf(buf, "%i\n", aic23_info_l.sta);
473 static DEVICE_ATTR(st_attenuation, S_IRUGO | S_IWUSR,
474 show_st_attenuation, store_st_attenuation);
476 static ssize_t store_mic_enable(struct device *dev,
477 struct device_attribute *attr,
478 const char *buf, size_t count)
482 sscanf(buf, "%i", &mic);
488 static ssize_t show_mic_enable(struct device *dev,
489 struct device_attribute *attr, char *buf)
491 return sprintf(buf, "%i\n", aic23_info_l.mic_enable);
494 static DEVICE_ATTR(mic_enable, S_IRUGO | S_IWUSR,
495 show_mic_enable, store_mic_enable);
497 static ssize_t show_audio_selftest(struct device *dev,
498 struct device_attribute *attr, char *buf)
500 return sprintf(buf, "%i\n", selftest);
503 static DEVICE_ATTR(audio_selftest, S_IRUGO | S_IWUSR,
504 show_audio_selftest, NULL);
506 static int audio_i2c_probe(struct platform_device *dev)
510 if ((r = device_create_file(&dev->dev, &dev_attr_volume_left)) != 0)
512 else if ((r = device_create_file(&dev->dev,
513 &dev_attr_volume_right)) != 0)
514 goto err_volume_left;
515 else if ((r = device_create_file(&dev->dev,
516 &dev_attr_gain_right)) != 0)
517 goto err_volume_right;
518 else if ((r = device_create_file(&dev->dev,
519 &dev_attr_gain_left)) != 0)
521 else if ((r = device_create_file(&dev->dev,
522 &dev_attr_mic_loopback)) != 0)
524 else if ((r = device_create_file(&dev->dev,
525 &dev_attr_mic_enable)) != 0)
526 goto err_mic_loopback;
527 else if ((r = device_create_file(&dev->dev,
528 &dev_attr_st_attenuation)) != 0)
530 else if ((r = device_create_file(&dev->dev,
531 &dev_attr_audio_selftest)) != 0)
532 goto err_st_attenuation;
537 device_remove_file(&dev->dev, &dev_attr_st_attenuation);
539 device_remove_file(&dev->dev, &dev_attr_mic_enable);
541 device_remove_file(&dev->dev, &dev_attr_mic_loopback);
543 device_remove_file(&dev->dev, &dev_attr_gain_left);
545 device_remove_file(&dev->dev, &dev_attr_gain_right);
547 device_remove_file(&dev->dev, &dev_attr_volume_right);
549 device_remove_file(&dev->dev, &dev_attr_volume_left);
554 static int audio_i2c_remove(struct platform_device *dev)
556 device_remove_file(&dev->dev, &dev_attr_st_attenuation);
557 device_remove_file(&dev->dev, &dev_attr_mic_enable);
558 device_remove_file(&dev->dev, &dev_attr_mic_loopback);
559 device_remove_file(&dev->dev, &dev_attr_gain_left);
560 device_remove_file(&dev->dev, &dev_attr_gain_right);
561 device_remove_file(&dev->dev, &dev_attr_volume_right);
562 device_remove_file(&dev->dev, &dev_attr_volume_left);
567 /*----------------------------------------------------------------*/
569 /*----------------------------------------------------------------*/
571 static void audio_i2c_shutdown(struct platform_device *dev)
573 /* Let's mute the codec before powering off to prevent
574 * glitch in the sound
576 aic23_write_value(LEFT_CHANNEL_VOLUME_ADDR, LHV_MIN);
577 aic23_write_value(RIGHT_CHANNEL_VOLUME_ADDR, LHV_MIN);
581 static int audio_i2c_suspend(struct platform_device *dev, pm_message_t state)
583 /* Let's mute the codec before powering off to prevent
584 * glitch in the sound
586 aic23_write_value(LEFT_CHANNEL_VOLUME_ADDR, LHV_MIN);
587 aic23_write_value(RIGHT_CHANNEL_VOLUME_ADDR, LHV_MIN);
593 static int audio_i2c_resume(struct platform_device *dev)
600 static struct platform_driver audio_i2c_driver = {
602 .owner = THIS_MODULE,
605 .shutdown = audio_i2c_shutdown,
606 .probe = audio_i2c_probe,
607 .remove = audio_i2c_remove,
608 .suspend = audio_i2c_suspend,
609 .resume = audio_i2c_resume,
612 static struct platform_device audio_i2c_device = {
617 /*----------------------------------------------------------------*/
619 static int __init aic23_init(void)
622 aic23_info_l.initialized = 0;
624 if (i2c_add_driver(&aic23_driver)) {
625 printk("aic23 i2c: Driver registration failed, \
626 module not inserted.\n");
631 if (platform_driver_register(&audio_i2c_driver)) {
632 printk(KERN_WARNING "Failed to register audio i2c driver\n");
637 if (platform_device_register(&audio_i2c_device)) {
638 printk(KERN_WARNING "Failed to register audio i2c device\n");
639 platform_driver_unregister(&audio_i2c_driver);
643 /* FIXME: Do in board-specific file */
644 omap_mcbsp3_aic23_clock_init();
645 if (!aic23_info_l.power_down)
647 aic23_info_l.initialized = 1;
648 printk("TLV320AIC23 I2C version %s (%s)\n",
649 TLV320AIC23_VERSION, TLV320AIC23_DATE);
654 static void __exit aic23_exit(void)
659 if ((res = i2c_del_driver(&aic23_driver)))
660 printk("aic23 i2c: Driver remove failed, module not removed.\n");
662 platform_device_unregister(&audio_i2c_device);
663 platform_driver_unregister(&audio_i2c_driver);
666 MODULE_AUTHOR("Kai Svahn <kai.svahn@nokia.com>");
667 MODULE_DESCRIPTION("I2C interface for TLV320AIC23 codec.");
668 MODULE_LICENSE("GPL");
670 module_init(aic23_init)
671 module_exit(aic23_exit)
673 EXPORT_SYMBOL(aic23_write_value);
674 EXPORT_SYMBOL(aic23_power_up);
675 EXPORT_SYMBOL(aic23_power_down);