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/module.h>
24 #include <linux/init.h>
25 #include <linux/i2c.h>
26 #include <linux/slab.h>
27 #include <linux/device.h>
28 #include <linux/platform_device.h>
30 #include <asm/arch/aic23.h>
31 #include <asm/arch/mcbsp.h>
33 #define TLV320AIC23_VERSION "1.8"
34 #define TLV320AIC23_DATE "10-Feb-2006"
39 #define OUTPUT_VOLUME_MIN LHV_MIN
40 #define OUTPUT_VOLUME_MAX LHV_MAX
41 #define OUTPUT_VOLUME_RANGE (OUTPUT_VOLUME_MAX - OUTPUT_VOLUME_MIN)
42 #define INPUT_VOLUME_MIN LIV_MIN
43 #define INPUT_VOLUME_MAX LIV_MAX
44 #define INPUT_VOLUME_RANGE (INPUT_VOLUME_MAX - INPUT_VOLUME_MIN)
46 /* I2C Addresses to scan */
47 static unsigned short normal_i2c[] = { TLV320AIC23ID1, TLV320AIC23ID2, \
49 /*static unsigned short normal_i2c_range[] = { I2C_CLIENT_END };*/
51 /* This makes all addr_data:s */
54 static struct i2c_driver aic23_driver;
55 static struct i2c_client *new_client;
57 static struct platform_device audio_i2c_device;
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);
100 * Configures the McBSP3 which is used to send clock to the AIC23 codec.
101 * The input clock rate from DSP is 12MHz.
102 * The DSP clock must be on before this is called.
104 static int omap_mcbsp3_aic23_clock_init(void)
108 /* enable 12MHz clock to mcbsp 1 & 3 */
109 __raw_writew(__raw_readw(DSP_IDLECT2) | (1<<1), DSP_IDLECT2);
110 __raw_writew(__raw_readw(DSP_RSTCT2) | 1 | 1<<1, DSP_RSTCT2);
112 /* disable sample rate generator */
113 OMAP_MCBSP_WRITE(OMAP1610_MCBSP3_BASE, SPCR1, 0x0000);
114 OMAP_MCBSP_WRITE(OMAP1610_MCBSP3_BASE, SPCR2, 0x0000);
116 /* pin control register */
117 OMAP_MCBSP_WRITE(OMAP1610_MCBSP3_BASE, PCR0,(CLKXM | CLKXP | CLKRP));
119 /* configure srg to send 12MHz pulse from dsp peripheral clock */
120 OMAP_MCBSP_WRITE(OMAP1610_MCBSP3_BASE, SRGR1, 0x0000);
121 OMAP_MCBSP_WRITE(OMAP1610_MCBSP3_BASE, SRGR2, CLKSM);
123 /* enable sample rate generator */
124 w = OMAP_MCBSP_READ(OMAP1610_MCBSP3_BASE, SPCR2);
125 OMAP_MCBSP_WRITE(OMAP1610_MCBSP3_BASE, SPCR2, (w | FREE | GRST));
126 printk("Clock enabled to MCBSP1 & 3 \n");
131 static int aic23_detect_client(struct i2c_adapter *adapter, int address,
135 const char *client_name = "TLV320AIC23 Audio Codec";
137 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA |
138 I2C_FUNC_SMBUS_WRITE_BYTE)) {
139 printk(KERN_WARNING "%s functionality check failed\n",
144 if (!(new_client = kmalloc(sizeof(struct i2c_client),
147 printk(KERN_WARNING "Couldn't allocate memory for %s\n",
152 memset(new_client, 0x00, sizeof(struct i2c_client));
153 new_client->addr = address;
154 new_client->adapter = adapter;
155 new_client->driver = &aic23_driver;
156 new_client->flags = 0;
157 strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
159 if ((err = i2c_attach_client(new_client))) {
160 printk(KERN_WARNING "Couldn't attach %s\n", client_name);
165 if (platform_device_register(&audio_i2c_device)) {
166 printk(KERN_WARNING "Failed to register audio i2c device\n");
170 /* FIXME: Do in board-specific file */
171 omap_mcbsp3_aic23_clock_init();
173 if (!aic23_info_l.power_down)
175 aic23_info_l.initialized = 1;
180 static int aic23_detach_client(struct i2c_client *client)
184 platform_device_unregister(&audio_i2c_device);
186 if ((err = i2c_detach_client(client))) {
187 printk("aic23.o: Client deregistration failed, \
188 client not detached.\n");
195 static int aic23_attach_adapter(struct i2c_adapter *adapter)
199 res = i2c_probe(adapter, &addr_data, &aic23_detect_client);
203 static struct i2c_driver aic23_driver = {
205 .name = "OMAP+TLV320AIC23 codec",
206 /*.flags = I2C_DF_NOTIFY,*/
208 .id = I2C_DRIVERID_MISC, /* Experimental ID */
209 .attach_adapter = aic23_attach_adapter,
210 .detach_client = aic23_detach_client,
213 static void update_volume_left(int volume)
216 val = ((volume * OUTPUT_VOLUME_RANGE) / 100) + OUTPUT_VOLUME_MIN;
217 aic23_write_value(LEFT_CHANNEL_VOLUME_ADDR, val);
218 aic23_info_l.volume_reg_left = volume;
221 static void update_volume_right(int volume)
224 val = ((volume * OUTPUT_VOLUME_RANGE) / 100) + OUTPUT_VOLUME_MIN;
225 aic23_write_value(RIGHT_CHANNEL_VOLUME_ADDR, val);
226 aic23_info_l.volume_reg_right = volume;
229 static void set_mic(int mic_en)
234 aic23_info_l.power = OSC_OFF | LINE_OFF;
236 aic23_info_l.mask &= ~MICM_MUTED;
237 aic23_info_l.mask |= MICB_20DB; /* STE_ENABLED */
240 OSC_OFF | ADC_OFF | MIC_OFF | LINE_OFF;
243 DAC_SELECTED | INSEL_MIC | MICM_MUTED;
245 aic23_write_value(POWER_DOWN_CONTROL_ADDR,
247 aic23_write_value(DIGITAL_AUDIO_CONTROL_ADDR, dg_ctrl);
248 aic23_write_value(ANALOG_AUDIO_CONTROL_ADDR,
250 aic23_info_l.mic_enable = mic_en;
252 printk(KERN_INFO "aic23 mic state: %i\n", mic_en);
255 static void aic23_init_power(void)
257 aic23_write_value(RESET_CONTROL_ADDR, 0x00);
259 if (aic23_info_l.initialized == 0) {
260 aic23_write_value(LEFT_CHANNEL_VOLUME_ADDR, LHV_MIN);
261 aic23_write_value(RIGHT_CHANNEL_VOLUME_ADDR, LHV_MIN);
264 update_volume_left(aic23_info_l.volume_reg_left);
265 update_volume_right(aic23_info_l.volume_reg_right);
268 aic23_info_l.mask = DAC_SELECTED | INSEL_MIC | MICM_MUTED;
269 aic23_write_value(ANALOG_AUDIO_CONTROL_ADDR,
271 aic23_write_value(DIGITAL_AUDIO_CONTROL_ADDR, 0x00);
272 aic23_write_value(DIGITAL_AUDIO_FORMAT_ADDR, LRP_ON | FOR_DSP);
273 aic23_write_value(SAMPLE_RATE_CONTROL_ADDR, USB_CLK_ON);
274 aic23_write_value(DIGITAL_INTERFACE_ACT_ADDR, ACT_ON);
275 aic23_info_l.power = OSC_OFF | ADC_OFF | MIC_OFF | LINE_OFF;
276 aic23_write_value(POWER_DOWN_CONTROL_ADDR,
279 /* enable mic input */
280 if (aic23_info_l.mic_enable)
281 set_mic(aic23_info_l.mic_enable);
283 printk(KERN_INFO "aic23_init_power() done\n");
286 void aic23_power_down(void)
288 if (aic23_info_l.initialized) {
289 printk("aic23 powering down\n");
290 aic23_write_value(POWER_DOWN_CONTROL_ADDR, 0xff);
292 aic23_info_l.power_down = 1;
295 void aic23_power_up(void)
297 if (aic23_info_l.initialized) {
298 printk("aic23 powering up\n");
301 aic23_info_l.power_down = 0;
304 /*----------------------------------------------------------------------*/
305 /* sysfs initializations */
306 /*----------------------------------------------------------------------*/
308 static ssize_t store_volume_left(struct device *dev,
309 struct device_attribute *attr,
310 const char *buf, size_t count)
314 sscanf(buf, "%i", &volume);
316 if (volume < MIN_VOL) {
319 } else if (volume > MIN_VOL && aic23_info_l.power_down) {
320 aic23_info_l.volume_reg_left = volume;
324 if (volume > MAX_VOL)
327 update_volume_left(volume);
331 static ssize_t show_volume_left(struct device *dev,
332 struct device_attribute *attr, char *buf)
334 return sprintf(buf, "%u\n", aic23_info_l.volume_reg_left);
337 static DEVICE_ATTR(volume_left, S_IRUGO | S_IWUGO,
338 show_volume_left, store_volume_left);
340 static ssize_t store_volume_right(struct device *dev,
341 struct device_attribute *attr,
342 const char *buf, size_t count)
346 sscanf(buf, "%i", &volume);
347 if (volume < MIN_VOL) {
350 } else if (volume > MIN_VOL && aic23_info_l.power_down) {
351 aic23_info_l.volume_reg_right = volume;
355 if (volume > MAX_VOL)
358 update_volume_right(volume);
362 static ssize_t show_volume_right(struct device *dev,
363 struct device_attribute *attr, char *buf)
365 return sprintf(buf, "%u\n", aic23_info_l.volume_reg_right);
368 static DEVICE_ATTR(volume_right, S_IRUGO | S_IWUGO,
369 show_volume_right, store_volume_right);
371 static ssize_t store_gain_left(struct device *dev,
372 struct device_attribute *attr,
373 const char *buf, size_t count)
378 sscanf(buf, "%u", &gain);
382 val = ((gain * INPUT_VOLUME_RANGE) / 100) + INPUT_VOLUME_MIN;
383 aic23_write_value(LEFT_LINE_VOLUME_ADDR, val);
384 aic23_info_l.input_gain_reg_left = gain;
389 static ssize_t show_gain_left(struct device *dev,
390 struct device_attribute *attr, char *buf)
392 return sprintf(buf, "%u\n", aic23_info_l.input_gain_reg_left);
395 static DEVICE_ATTR(gain_left, S_IRUGO | S_IWUSR, show_gain_left,
398 static ssize_t store_gain_right(struct device *dev,
399 struct device_attribute *attr,
400 const char *buf, size_t count)
405 sscanf(buf, "%u", &gain);
409 val = ((gain * INPUT_VOLUME_RANGE) / 100) + INPUT_VOLUME_MIN;
410 aic23_write_value(RIGHT_LINE_VOLUME_ADDR, val);
411 aic23_info_l.input_gain_reg_right = gain;
416 static ssize_t show_gain_right(struct device *dev,
417 struct device_attribute *attr, char *buf)
419 return sprintf(buf, "%u\n", aic23_info_l.input_gain_reg_right);
422 static DEVICE_ATTR(gain_right, S_IRUGO | S_IWUSR, show_gain_right,
425 static ssize_t store_mic_loopback(struct device *dev,
426 struct device_attribute *attr,
427 const char *buf, size_t count)
431 sscanf(buf, "%i", &mic);
433 aic23_write_value(POWER_DOWN_CONTROL_ADDR, \
434 OSC_OFF | ADC_OFF | LINE_OFF);
435 aic23_info_l.mask = STE_ENABLED | DAC_SELECTED \
436 | INSEL_MIC | MICB_20DB;
437 aic23_write_value(ANALOG_AUDIO_CONTROL_ADDR,
442 aic23_write_value(POWER_DOWN_CONTROL_ADDR, \
443 OSC_OFF | ADC_OFF | MIC_OFF | LINE_OFF);
446 aic23_info_l.mic_loopback = mic;
451 static ssize_t show_mic_loopback(struct device *dev,
452 struct device_attribute *attr, char *buf)
454 return sprintf(buf, "%i\n", aic23_info_l.mic_loopback);
457 static DEVICE_ATTR(mic_loopback, S_IRUGO | S_IWUSR,
458 show_mic_loopback, store_mic_loopback);
460 static ssize_t store_st_attenuation(struct device *dev,
461 struct device_attribute *attr,
462 const char *buf, size_t count)
467 sscanf(buf, "%u", &sta);
471 tmp = aic23_info_l.mask;
474 aic23_info_l.mask = tmp | STA_REG(sta);
475 aic23_write_value(ANALOG_AUDIO_CONTROL_ADDR,
477 aic23_info_l.sta = sta;
482 static ssize_t show_st_attenuation(struct device *dev,
483 struct device_attribute *attr, char *buf)
485 return sprintf(buf, "%i\n", aic23_info_l.sta);
488 static DEVICE_ATTR(st_attenuation, S_IRUGO | S_IWUSR,
489 show_st_attenuation, store_st_attenuation);
491 static ssize_t store_mic_enable(struct device *dev,
492 struct device_attribute *attr,
493 const char *buf, size_t count)
497 sscanf(buf, "%i", &mic);
503 static ssize_t show_mic_enable(struct device *dev,
504 struct device_attribute *attr, char *buf)
506 return sprintf(buf, "%i\n", aic23_info_l.mic_enable);
509 static DEVICE_ATTR(mic_enable, S_IRUGO | S_IWUSR,
510 show_mic_enable, store_mic_enable);
512 static ssize_t show_audio_selftest(struct device *dev,
513 struct device_attribute *attr, char *buf)
515 return sprintf(buf, "%i\n", selftest);
518 static DEVICE_ATTR(audio_selftest, S_IRUGO | S_IWUSR,
519 show_audio_selftest, NULL);
521 static int audio_i2c_probe(struct platform_device *dev)
525 if ((r = device_create_file(&dev->dev, &dev_attr_volume_left)) != 0)
527 else if ((r = device_create_file(&dev->dev,
528 &dev_attr_volume_right)) != 0)
529 goto err_volume_left;
530 else if ((r = device_create_file(&dev->dev,
531 &dev_attr_gain_right)) != 0)
532 goto err_volume_right;
533 else if ((r = device_create_file(&dev->dev,
534 &dev_attr_gain_left)) != 0)
536 else if ((r = device_create_file(&dev->dev,
537 &dev_attr_mic_loopback)) != 0)
539 else if ((r = device_create_file(&dev->dev,
540 &dev_attr_mic_enable)) != 0)
541 goto err_mic_loopback;
542 else if ((r = device_create_file(&dev->dev,
543 &dev_attr_st_attenuation)) != 0)
545 else if ((r = device_create_file(&dev->dev,
546 &dev_attr_audio_selftest)) != 0)
547 goto err_st_attenuation;
552 device_remove_file(&dev->dev, &dev_attr_st_attenuation);
554 device_remove_file(&dev->dev, &dev_attr_mic_enable);
556 device_remove_file(&dev->dev, &dev_attr_mic_loopback);
558 device_remove_file(&dev->dev, &dev_attr_gain_left);
560 device_remove_file(&dev->dev, &dev_attr_gain_right);
562 device_remove_file(&dev->dev, &dev_attr_volume_right);
564 device_remove_file(&dev->dev, &dev_attr_volume_left);
569 static int audio_i2c_remove(struct platform_device *dev)
571 device_remove_file(&dev->dev, &dev_attr_st_attenuation);
572 device_remove_file(&dev->dev, &dev_attr_mic_enable);
573 device_remove_file(&dev->dev, &dev_attr_mic_loopback);
574 device_remove_file(&dev->dev, &dev_attr_gain_left);
575 device_remove_file(&dev->dev, &dev_attr_gain_right);
576 device_remove_file(&dev->dev, &dev_attr_volume_right);
577 device_remove_file(&dev->dev, &dev_attr_volume_left);
582 /*----------------------------------------------------------------*/
584 /*----------------------------------------------------------------*/
586 static void audio_i2c_shutdown(struct platform_device *dev)
588 /* Let's mute the codec before powering off to prevent
589 * glitch in the sound
591 aic23_write_value(LEFT_CHANNEL_VOLUME_ADDR, LHV_MIN);
592 aic23_write_value(RIGHT_CHANNEL_VOLUME_ADDR, LHV_MIN);
596 static int audio_i2c_suspend(struct platform_device *dev, pm_message_t state)
598 /* Let's mute the codec before powering off to prevent
599 * glitch in the sound
601 aic23_write_value(LEFT_CHANNEL_VOLUME_ADDR, LHV_MIN);
602 aic23_write_value(RIGHT_CHANNEL_VOLUME_ADDR, LHV_MIN);
608 static int audio_i2c_resume(struct platform_device *dev)
615 static struct platform_driver audio_i2c_driver = {
617 .owner = THIS_MODULE,
620 .shutdown = audio_i2c_shutdown,
621 .probe = audio_i2c_probe,
622 .remove = audio_i2c_remove,
623 .suspend = audio_i2c_suspend,
624 .resume = audio_i2c_resume,
627 static struct platform_device audio_i2c_device = {
632 /*----------------------------------------------------------------*/
634 static int __init aic23_init(void)
637 aic23_info_l.initialized = 0;
639 if (i2c_add_driver(&aic23_driver)) {
640 printk("aic23 i2c: Driver registration failed, \
641 module not inserted.\n");
646 if (platform_driver_register(&audio_i2c_driver)) {
647 printk(KERN_WARNING "Failed to register audio i2c driver\n");
652 printk("TLV320AIC23 I2C version %s (%s)\n",
653 TLV320AIC23_VERSION, TLV320AIC23_DATE);
658 static void __exit aic23_exit(void)
661 i2c_del_driver(&aic23_driver);
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);