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;
58 static struct aic23_info {
61 u16 input_gain_reg_left;
62 u16 input_gain_reg_right;
63 u16 power; /* For POWER_DOWN_CONTROL_ADDR */
64 u16 mask; /* For ANALOG_AUDIO_CONTROL_ADDR */
72 static int _aic23_write_value(struct i2c_client *client, u8 reg, u16 value)
76 /* TLV320AIC23 has 7 bit address and 9 bits of data
77 * so we need to switch one data bit into reg and rest
82 val = (0x01 & (value >> 8));
84 val = (0x00ff & value);
86 return i2c_smbus_write_byte_data(client, wreg, val);
89 int aic23_write_value(u8 reg, u16 value)
91 static struct i2c_client *client;
93 _aic23_write_value(client, reg, value);
98 static int aic23_detect_client(struct i2c_adapter *adapter, int address,
102 const char *client_name = "TLV320AIC23 Audio Codec";
104 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA |
105 I2C_FUNC_SMBUS_WRITE_BYTE)) {
106 printk(KERN_WARNING "%s functinality check failed\n",
111 if (!(new_client = kmalloc(sizeof(struct i2c_client),
114 printk(KERN_WARNING "Couldn't allocate memory for %s\n",
119 memset(new_client, 0x00, sizeof(struct i2c_client));
120 new_client->addr = address;
121 new_client->adapter = adapter;
122 new_client->driver = &aic23_driver;
123 new_client->flags = 0;
124 strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
126 if ((err = i2c_attach_client(new_client))) {
127 printk(KERN_WARNING "Couldn't attach %s\n", client_name);
134 static int aic23_detach_client(struct i2c_client *client)
138 if ((err = i2c_detach_client(client))) {
139 printk("aic23.o: Client deregistration failed, \
140 client not detached.\n");
147 static int aic23_attach_adapter(struct i2c_adapter *adapter)
151 res = i2c_probe(adapter, &addr_data, &aic23_detect_client);
155 static struct i2c_driver aic23_driver = {
157 .name = "OMAP+TLV320AIC23 codec",
158 /*.flags = I2C_DF_NOTIFY,*/
160 .id = I2C_DRIVERID_MISC, /* Experimental ID */
161 .attach_adapter = aic23_attach_adapter,
162 .detach_client = aic23_detach_client,
166 * Configures the McBSP3 which is used to send clock to the AIC23 codec.
167 * The input clock rate from DSP is 12MHz.
168 * The DSP clock must be on before this is called.
170 static int omap_mcbsp3_aic23_clock_init(void)
174 /* enable 12MHz clock to mcbsp 1 & 3 */
175 __raw_writew(__raw_readw(DSP_IDLECT2) | (1<<1), DSP_IDLECT2);
176 __raw_writew(__raw_readw(DSP_RSTCT2) | 1 | 1<<1, DSP_RSTCT2);
178 /* disable sample rate generator */
179 OMAP_MCBSP_WRITE(OMAP1610_MCBSP3_BASE, SPCR1, 0x0000);
180 OMAP_MCBSP_WRITE(OMAP1610_MCBSP3_BASE, SPCR2, 0x0000);
182 /* pin control register */
183 OMAP_MCBSP_WRITE(OMAP1610_MCBSP3_BASE, PCR0,(CLKXM | CLKXP | CLKRP));
185 /* configure srg to send 12MHz pulse from dsp peripheral clock */
186 OMAP_MCBSP_WRITE(OMAP1610_MCBSP3_BASE, SRGR1, 0x0000);
187 OMAP_MCBSP_WRITE(OMAP1610_MCBSP3_BASE, SRGR2, CLKSM);
189 /* enable sample rate generator */
190 w = OMAP_MCBSP_READ(OMAP1610_MCBSP3_BASE, SPCR2);
191 OMAP_MCBSP_WRITE(OMAP1610_MCBSP3_BASE, SPCR2, (w | FREE | GRST));
192 printk("Clock enabled to MCBSP1 & 3 \n");
197 static void update_volume_left(int volume)
200 val = ((volume * OUTPUT_VOLUME_RANGE) / 100) + OUTPUT_VOLUME_MIN;
201 aic23_write_value(LEFT_CHANNEL_VOLUME_ADDR, val);
202 aic23_info_l.volume_reg_left = volume;
205 static void update_volume_right(int volume)
208 val = ((volume * OUTPUT_VOLUME_RANGE) / 100) + OUTPUT_VOLUME_MIN;
209 aic23_write_value(RIGHT_CHANNEL_VOLUME_ADDR, val);
210 aic23_info_l.volume_reg_right = volume;
213 static void set_mic(int mic_en)
218 aic23_info_l.power = OSC_OFF | LINE_OFF;
220 aic23_info_l.mask &= ~MICM_MUTED;
221 aic23_info_l.mask |= MICB_20DB; /* STE_ENABLED */
224 OSC_OFF | ADC_OFF | MIC_OFF | LINE_OFF;
227 DAC_SELECTED | INSEL_MIC | MICM_MUTED;
229 aic23_write_value(POWER_DOWN_CONTROL_ADDR,
231 aic23_write_value(DIGITAL_AUDIO_CONTROL_ADDR, dg_ctrl);
232 aic23_write_value(ANALOG_AUDIO_CONTROL_ADDR,
234 aic23_info_l.mic_enable = mic_en;
236 printk(KERN_INFO "aic23 mic state: %i\n", mic_en);
239 static void aic23_init_power(void)
241 aic23_write_value(RESET_CONTROL_ADDR, 0x00);
243 if (aic23_info_l.initialized == 0) {
244 aic23_write_value(LEFT_CHANNEL_VOLUME_ADDR, LHV_MIN);
245 aic23_write_value(RIGHT_CHANNEL_VOLUME_ADDR, LHV_MIN);
248 update_volume_left(aic23_info_l.volume_reg_left);
249 update_volume_right(aic23_info_l.volume_reg_right);
252 aic23_info_l.mask = DAC_SELECTED | INSEL_MIC | MICM_MUTED;
253 aic23_write_value(ANALOG_AUDIO_CONTROL_ADDR,
255 aic23_write_value(DIGITAL_AUDIO_CONTROL_ADDR, 0x00);
256 aic23_write_value(DIGITAL_AUDIO_FORMAT_ADDR, LRP_ON | FOR_DSP);
257 aic23_write_value(SAMPLE_RATE_CONTROL_ADDR, USB_CLK_ON);
258 aic23_write_value(DIGITAL_INTERFACE_ACT_ADDR, ACT_ON);
259 aic23_info_l.power = OSC_OFF | ADC_OFF | MIC_OFF | LINE_OFF;
260 aic23_write_value(POWER_DOWN_CONTROL_ADDR,
263 /* enable mic input */
264 if (aic23_info_l.mic_enable)
265 set_mic(aic23_info_l.mic_enable);
267 printk(KERN_INFO "aic23_init_power() done\n");
270 void aic23_power_down(void)
272 if (aic23_info_l.initialized) {
273 printk("aic23 powering down\n");
274 aic23_write_value(POWER_DOWN_CONTROL_ADDR, 0xff);
276 aic23_info_l.power_down = 1;
279 void aic23_power_up(void)
281 if (aic23_info_l.initialized) {
282 printk("aic23 powering up\n");
285 aic23_info_l.power_down = 0;
288 /*----------------------------------------------------------------------*/
289 /* sysfs initializations */
290 /*----------------------------------------------------------------------*/
292 static ssize_t store_volume_left(struct device *dev,
293 struct device_attribute *attr,
294 const char *buf, size_t count)
298 sscanf(buf, "%i", &volume);
300 if (volume < MIN_VOL) {
303 } else if (volume > MIN_VOL && aic23_info_l.power_down) {
304 aic23_info_l.volume_reg_left = volume;
308 if (volume > MAX_VOL)
311 update_volume_left(volume);
315 static ssize_t show_volume_left(struct device *dev,
316 struct device_attribute *attr, char *buf)
318 return sprintf(buf, "%u\n", aic23_info_l.volume_reg_left);
321 static DEVICE_ATTR(volume_left, S_IRUGO | S_IWUGO,
322 show_volume_left, store_volume_left);
324 static ssize_t store_volume_right(struct device *dev,
325 struct device_attribute *attr,
326 const char *buf, size_t count)
330 sscanf(buf, "%i", &volume);
331 if (volume < MIN_VOL) {
334 } else if (volume > MIN_VOL && aic23_info_l.power_down) {
335 aic23_info_l.volume_reg_right = volume;
339 if (volume > MAX_VOL)
342 update_volume_right(volume);
346 static ssize_t show_volume_right(struct device *dev,
347 struct device_attribute *attr, char *buf)
349 return sprintf(buf, "%u\n", aic23_info_l.volume_reg_right);
352 static DEVICE_ATTR(volume_right, S_IRUGO | S_IWUGO,
353 show_volume_right, store_volume_right);
355 static ssize_t store_gain_left(struct device *dev,
356 struct device_attribute *attr,
357 const char *buf, size_t count)
362 sscanf(buf, "%u", &gain);
366 val = ((gain * INPUT_VOLUME_RANGE) / 100) + INPUT_VOLUME_MIN;
367 aic23_write_value(LEFT_LINE_VOLUME_ADDR, val);
368 aic23_info_l.input_gain_reg_left = gain;
373 static ssize_t show_gain_left(struct device *dev,
374 struct device_attribute *attr, char *buf)
376 return sprintf(buf, "%u\n", aic23_info_l.input_gain_reg_left);
379 static DEVICE_ATTR(gain_left, S_IRUGO | S_IWUSR, show_gain_left,
382 static ssize_t store_gain_right(struct device *dev,
383 struct device_attribute *attr,
384 const char *buf, size_t count)
389 sscanf(buf, "%u", &gain);
393 val = ((gain * INPUT_VOLUME_RANGE) / 100) + INPUT_VOLUME_MIN;
394 aic23_write_value(RIGHT_LINE_VOLUME_ADDR, val);
395 aic23_info_l.input_gain_reg_right = gain;
400 static ssize_t show_gain_right(struct device *dev,
401 struct device_attribute *attr, char *buf)
403 return sprintf(buf, "%u\n", aic23_info_l.input_gain_reg_right);
406 static DEVICE_ATTR(gain_right, S_IRUGO | S_IWUSR, show_gain_right,
409 static ssize_t store_mic_loopback(struct device *dev,
410 struct device_attribute *attr,
411 const char *buf, size_t count)
415 sscanf(buf, "%i", &mic);
417 aic23_write_value(POWER_DOWN_CONTROL_ADDR, \
418 OSC_OFF | ADC_OFF | LINE_OFF);
419 aic23_info_l.mask = STE_ENABLED | DAC_SELECTED \
420 | INSEL_MIC | MICB_20DB;
421 aic23_write_value(ANALOG_AUDIO_CONTROL_ADDR,
426 aic23_write_value(POWER_DOWN_CONTROL_ADDR, \
427 OSC_OFF | ADC_OFF | MIC_OFF | LINE_OFF);
430 aic23_info_l.mic_loopback = mic;
435 static ssize_t show_mic_loopback(struct device *dev,
436 struct device_attribute *attr, char *buf)
438 return sprintf(buf, "%i\n", aic23_info_l.mic_loopback);
441 static DEVICE_ATTR(mic_loopback, S_IRUGO | S_IWUSR,
442 show_mic_loopback, store_mic_loopback);
444 static ssize_t store_st_attenuation(struct device *dev,
445 struct device_attribute *attr,
446 const char *buf, size_t count)
451 sscanf(buf, "%u", &sta);
455 tmp = aic23_info_l.mask;
458 aic23_info_l.mask = tmp | STA_REG(sta);
459 aic23_write_value(ANALOG_AUDIO_CONTROL_ADDR,
461 aic23_info_l.sta = sta;
466 static ssize_t show_st_attenuation(struct device *dev,
467 struct device_attribute *attr, char *buf)
469 return sprintf(buf, "%i\n", aic23_info_l.sta);
472 static DEVICE_ATTR(st_attenuation, S_IRUGO | S_IWUSR,
473 show_st_attenuation, store_st_attenuation);
475 static ssize_t store_mic_enable(struct device *dev,
476 struct device_attribute *attr,
477 const char *buf, size_t count)
481 sscanf(buf, "%i", &mic);
487 static ssize_t show_mic_enable(struct device *dev,
488 struct device_attribute *attr, char *buf)
490 return sprintf(buf, "%i\n", aic23_info_l.mic_enable);
493 static DEVICE_ATTR(mic_enable, S_IRUGO | S_IWUSR,
494 show_mic_enable, store_mic_enable);
496 static ssize_t show_audio_selftest(struct device *dev,
497 struct device_attribute *attr, char *buf)
499 return sprintf(buf, "%i\n", selftest);
502 static DEVICE_ATTR(audio_selftest, S_IRUGO | S_IWUSR,
503 show_audio_selftest, NULL);
505 static int audio_i2c_probe(struct platform_device *dev)
509 if ((r = device_create_file(&dev->dev, &dev_attr_volume_left)) != 0)
511 else if ((r = device_create_file(&dev->dev,
512 &dev_attr_volume_right)) != 0)
513 goto err_volume_left;
514 else if ((r = device_create_file(&dev->dev,
515 &dev_attr_gain_right)) != 0)
516 goto err_volume_right;
517 else if ((r = device_create_file(&dev->dev,
518 &dev_attr_gain_left)) != 0)
520 else if ((r = device_create_file(&dev->dev,
521 &dev_attr_mic_loopback)) != 0)
523 else if ((r = device_create_file(&dev->dev,
524 &dev_attr_mic_enable)) != 0)
525 goto err_mic_loopback;
526 else if ((r = device_create_file(&dev->dev,
527 &dev_attr_st_attenuation)) != 0)
529 else if ((r = device_create_file(&dev->dev,
530 &dev_attr_audio_selftest)) != 0)
531 goto err_st_attenuation;
536 device_remove_file(&dev->dev, &dev_attr_st_attenuation);
538 device_remove_file(&dev->dev, &dev_attr_mic_enable);
540 device_remove_file(&dev->dev, &dev_attr_mic_loopback);
542 device_remove_file(&dev->dev, &dev_attr_gain_left);
544 device_remove_file(&dev->dev, &dev_attr_gain_right);
546 device_remove_file(&dev->dev, &dev_attr_volume_right);
548 device_remove_file(&dev->dev, &dev_attr_volume_left);
553 static int audio_i2c_remove(struct platform_device *dev)
555 device_remove_file(&dev->dev, &dev_attr_st_attenuation);
556 device_remove_file(&dev->dev, &dev_attr_mic_enable);
557 device_remove_file(&dev->dev, &dev_attr_mic_loopback);
558 device_remove_file(&dev->dev, &dev_attr_gain_left);
559 device_remove_file(&dev->dev, &dev_attr_gain_right);
560 device_remove_file(&dev->dev, &dev_attr_volume_right);
561 device_remove_file(&dev->dev, &dev_attr_volume_left);
566 /*----------------------------------------------------------------*/
568 /*----------------------------------------------------------------*/
570 static void audio_i2c_shutdown(struct platform_device *dev)
572 /* Let's mute the codec before powering off to prevent
573 * glitch in the sound
575 aic23_write_value(LEFT_CHANNEL_VOLUME_ADDR, LHV_MIN);
576 aic23_write_value(RIGHT_CHANNEL_VOLUME_ADDR, LHV_MIN);
580 static int audio_i2c_suspend(struct platform_device *dev, pm_message_t state)
582 /* Let's mute the codec before powering off to prevent
583 * glitch in the sound
585 aic23_write_value(LEFT_CHANNEL_VOLUME_ADDR, LHV_MIN);
586 aic23_write_value(RIGHT_CHANNEL_VOLUME_ADDR, LHV_MIN);
592 static int audio_i2c_resume(struct platform_device *dev)
599 static struct platform_driver audio_i2c_driver = {
601 .owner = THIS_MODULE,
604 .shutdown = audio_i2c_shutdown,
605 .probe = audio_i2c_probe,
606 .remove = audio_i2c_remove,
607 .suspend = audio_i2c_suspend,
608 .resume = audio_i2c_resume,
611 static struct platform_device audio_i2c_device = {
616 /*----------------------------------------------------------------*/
618 static int __init aic23_init(void)
621 aic23_info_l.initialized = 0;
623 if (i2c_add_driver(&aic23_driver)) {
624 printk("aic23 i2c: Driver registration failed, \
625 module not inserted.\n");
630 if (platform_driver_register(&audio_i2c_driver)) {
631 printk(KERN_WARNING "Failed to register audio i2c driver\n");
636 if (platform_device_register(&audio_i2c_device)) {
637 printk(KERN_WARNING "Failed to register audio i2c device\n");
638 platform_driver_unregister(&audio_i2c_driver);
642 /* FIXME: Do in board-specific file */
643 omap_mcbsp3_aic23_clock_init();
644 if (!aic23_info_l.power_down)
646 aic23_info_l.initialized = 1;
647 printk("TLV320AIC23 I2C version %s (%s)\n",
648 TLV320AIC23_VERSION, TLV320AIC23_DATE);
653 static void __exit aic23_exit(void)
658 if ((res = i2c_del_driver(&aic23_driver)))
659 printk("aic23 i2c: Driver remove failed, module not removed.\n");
661 platform_device_unregister(&audio_i2c_device);
662 platform_driver_unregister(&audio_i2c_driver);
665 MODULE_AUTHOR("Kai Svahn <kai.svahn@nokia.com>");
666 MODULE_DESCRIPTION("I2C interface for TLV320AIC23 codec.");
667 MODULE_LICENSE("GPL");
669 module_init(aic23_init)
670 module_exit(aic23_exit)
672 EXPORT_SYMBOL(aic23_write_value);
673 EXPORT_SYMBOL(aic23_power_up);
674 EXPORT_SYMBOL(aic23_power_down);