198615454SKuninori Morimoto /* 298615454SKuninori Morimoto * DA7210 ALSA Soc codec driver 398615454SKuninori Morimoto * 498615454SKuninori Morimoto * Copyright (c) 2009 Dialog Semiconductor 598615454SKuninori Morimoto * Written by David Chen <Dajun.chen@diasemi.com> 698615454SKuninori Morimoto * 798615454SKuninori Morimoto * Copyright (C) 2009 Renesas Solutions Corp. 898615454SKuninori Morimoto * Cleanups by Kuninori Morimoto <morimoto.kuninori@renesas.com> 998615454SKuninori Morimoto * 1098615454SKuninori Morimoto * Tested on SuperH Ecovec24 board with S16/S24 LE in 48KHz using I2S 1198615454SKuninori Morimoto * 1298615454SKuninori Morimoto * This program is free software; you can redistribute it and/or modify it 1398615454SKuninori Morimoto * under the terms of the GNU General Public License as published by the 1498615454SKuninori Morimoto * Free Software Foundation; either version 2 of the License, or (at your 1598615454SKuninori Morimoto * option) any later version. 1698615454SKuninori Morimoto */ 1798615454SKuninori Morimoto 1898615454SKuninori Morimoto #include <linux/module.h> 1998615454SKuninori Morimoto #include <linux/moduleparam.h> 2098615454SKuninori Morimoto #include <linux/kernel.h> 2198615454SKuninori Morimoto #include <linux/init.h> 2298615454SKuninori Morimoto #include <linux/delay.h> 2398615454SKuninori Morimoto #include <linux/pm.h> 2498615454SKuninori Morimoto #include <linux/i2c.h> 2598615454SKuninori Morimoto #include <linux/platform_device.h> 265a0e3ad6STejun Heo #include <linux/slab.h> 2798615454SKuninori Morimoto #include <sound/core.h> 2898615454SKuninori Morimoto #include <sound/pcm.h> 2998615454SKuninori Morimoto #include <sound/pcm_params.h> 3098615454SKuninori Morimoto #include <sound/soc.h> 3198615454SKuninori Morimoto #include <sound/soc-dapm.h> 3298615454SKuninori Morimoto #include <sound/tlv.h> 3398615454SKuninori Morimoto #include <sound/initval.h> 3498615454SKuninori Morimoto #include <asm/div64.h> 3598615454SKuninori Morimoto 3698615454SKuninori Morimoto #include "da7210.h" 3798615454SKuninori Morimoto 3898615454SKuninori Morimoto /* DA7210 register space */ 3998615454SKuninori Morimoto #define DA7210_STATUS 0x02 4098615454SKuninori Morimoto #define DA7210_STARTUP1 0x03 4198615454SKuninori Morimoto #define DA7210_MIC_L 0x07 4298615454SKuninori Morimoto #define DA7210_MIC_R 0x08 4398615454SKuninori Morimoto #define DA7210_INMIX_L 0x0D 4498615454SKuninori Morimoto #define DA7210_INMIX_R 0x0E 4598615454SKuninori Morimoto #define DA7210_ADC_HPF 0x0F 4698615454SKuninori Morimoto #define DA7210_ADC 0x10 4798615454SKuninori Morimoto #define DA7210_DAC_HPF 0x14 4898615454SKuninori Morimoto #define DA7210_DAC_L 0x15 4998615454SKuninori Morimoto #define DA7210_DAC_R 0x16 5098615454SKuninori Morimoto #define DA7210_DAC_SEL 0x17 5198615454SKuninori Morimoto #define DA7210_OUTMIX_L 0x1C 5298615454SKuninori Morimoto #define DA7210_OUTMIX_R 0x1D 5398615454SKuninori Morimoto #define DA7210_HP_L_VOL 0x21 5498615454SKuninori Morimoto #define DA7210_HP_R_VOL 0x22 5598615454SKuninori Morimoto #define DA7210_HP_CFG 0x23 5698615454SKuninori Morimoto #define DA7210_DAI_SRC_SEL 0x25 5798615454SKuninori Morimoto #define DA7210_DAI_CFG1 0x26 5898615454SKuninori Morimoto #define DA7210_DAI_CFG3 0x28 5998615454SKuninori Morimoto #define DA7210_PLL_DIV3 0x2B 6098615454SKuninori Morimoto #define DA7210_PLL 0x2C 6198615454SKuninori Morimoto 6298615454SKuninori Morimoto /* STARTUP1 bit fields */ 6398615454SKuninori Morimoto #define DA7210_SC_MST_EN (1 << 0) 6498615454SKuninori Morimoto 6598615454SKuninori Morimoto /* MIC_L bit fields */ 6698615454SKuninori Morimoto #define DA7210_MICBIAS_EN (1 << 6) 6798615454SKuninori Morimoto #define DA7210_MIC_L_EN (1 << 7) 6898615454SKuninori Morimoto 6998615454SKuninori Morimoto /* MIC_R bit fields */ 7098615454SKuninori Morimoto #define DA7210_MIC_R_EN (1 << 7) 7198615454SKuninori Morimoto 7298615454SKuninori Morimoto /* INMIX_L bit fields */ 7398615454SKuninori Morimoto #define DA7210_IN_L_EN (1 << 7) 7498615454SKuninori Morimoto 7598615454SKuninori Morimoto /* INMIX_R bit fields */ 7698615454SKuninori Morimoto #define DA7210_IN_R_EN (1 << 7) 7798615454SKuninori Morimoto 7898615454SKuninori Morimoto /* ADC_HPF bit fields */ 7998615454SKuninori Morimoto #define DA7210_ADC_VOICE_EN (1 << 7) 8098615454SKuninori Morimoto 8198615454SKuninori Morimoto /* ADC bit fields */ 8298615454SKuninori Morimoto #define DA7210_ADC_L_EN (1 << 3) 8398615454SKuninori Morimoto #define DA7210_ADC_R_EN (1 << 7) 8498615454SKuninori Morimoto 85c2151433SMark Brown /* DAC_HPF fields */ 86c2151433SMark Brown #define DA7210_DAC_VOICE_EN (1 << 7) 87c2151433SMark Brown 8898615454SKuninori Morimoto /* DAC_SEL bit fields */ 8998615454SKuninori Morimoto #define DA7210_DAC_L_SRC_DAI_L (4 << 0) 9098615454SKuninori Morimoto #define DA7210_DAC_L_EN (1 << 3) 9198615454SKuninori Morimoto #define DA7210_DAC_R_SRC_DAI_R (5 << 4) 9298615454SKuninori Morimoto #define DA7210_DAC_R_EN (1 << 7) 9398615454SKuninori Morimoto 9498615454SKuninori Morimoto /* OUTMIX_L bit fields */ 9598615454SKuninori Morimoto #define DA7210_OUT_L_EN (1 << 7) 9698615454SKuninori Morimoto 9798615454SKuninori Morimoto /* OUTMIX_R bit fields */ 9898615454SKuninori Morimoto #define DA7210_OUT_R_EN (1 << 7) 9998615454SKuninori Morimoto 10098615454SKuninori Morimoto /* HP_CFG bit fields */ 10198615454SKuninori Morimoto #define DA7210_HP_2CAP_MODE (1 << 1) 10298615454SKuninori Morimoto #define DA7210_HP_SENSE_EN (1 << 2) 10398615454SKuninori Morimoto #define DA7210_HP_L_EN (1 << 3) 10498615454SKuninori Morimoto #define DA7210_HP_MODE (1 << 6) 10598615454SKuninori Morimoto #define DA7210_HP_R_EN (1 << 7) 10698615454SKuninori Morimoto 10798615454SKuninori Morimoto /* DAI_SRC_SEL bit fields */ 10898615454SKuninori Morimoto #define DA7210_DAI_OUT_L_SRC (6 << 0) 10998615454SKuninori Morimoto #define DA7210_DAI_OUT_R_SRC (7 << 4) 11098615454SKuninori Morimoto 11198615454SKuninori Morimoto /* DAI_CFG1 bit fields */ 11298615454SKuninori Morimoto #define DA7210_DAI_WORD_S16_LE (0 << 0) 11398615454SKuninori Morimoto #define DA7210_DAI_WORD_S24_LE (2 << 0) 11498615454SKuninori Morimoto #define DA7210_DAI_FLEN_64BIT (1 << 2) 11598615454SKuninori Morimoto #define DA7210_DAI_MODE_MASTER (1 << 7) 11698615454SKuninori Morimoto 11798615454SKuninori Morimoto /* DAI_CFG3 bit fields */ 11898615454SKuninori Morimoto #define DA7210_DAI_FORMAT_I2SMODE (0 << 0) 11998615454SKuninori Morimoto #define DA7210_DAI_OE (1 << 3) 12098615454SKuninori Morimoto #define DA7210_DAI_EN (1 << 7) 12198615454SKuninori Morimoto 12298615454SKuninori Morimoto /*PLL_DIV3 bit fields */ 12398615454SKuninori Morimoto #define DA7210_MCLK_RANGE_10_20_MHZ (1 << 4) 12498615454SKuninori Morimoto #define DA7210_PLL_BYP (1 << 6) 12598615454SKuninori Morimoto 12698615454SKuninori Morimoto /* PLL bit fields */ 12798615454SKuninori Morimoto #define DA7210_PLL_FS_48000 (11 << 0) 12898615454SKuninori Morimoto 12998615454SKuninori Morimoto #define DA7210_VERSION "0.0.1" 13098615454SKuninori Morimoto 13198615454SKuninori Morimoto /* Codec private data */ 13298615454SKuninori Morimoto struct da7210_priv { 13398615454SKuninori Morimoto struct snd_soc_codec codec; 13498615454SKuninori Morimoto }; 13598615454SKuninori Morimoto 13698615454SKuninori Morimoto static struct snd_soc_codec *da7210_codec; 13798615454SKuninori Morimoto 13898615454SKuninori Morimoto /* 13998615454SKuninori Morimoto * Register cache 14098615454SKuninori Morimoto */ 14198615454SKuninori Morimoto static const u8 da7210_reg[] = { 14298615454SKuninori Morimoto 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R0 - R7 */ 14398615454SKuninori Morimoto 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, /* R8 - RF */ 14498615454SKuninori Morimoto 0x00, 0x00, 0x00, 0x00, 0x08, 0x10, 0x10, 0x54, /* R10 - R17 */ 14598615454SKuninori Morimoto 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R18 - R1F */ 14698615454SKuninori Morimoto 0x00, 0x00, 0x00, 0x02, 0x00, 0x76, 0x00, 0x00, /* R20 - R27 */ 14798615454SKuninori Morimoto 0x04, 0x00, 0x00, 0x30, 0x2A, 0x00, 0x40, 0x00, /* R28 - R2F */ 14898615454SKuninori Morimoto 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, /* R30 - R37 */ 14998615454SKuninori Morimoto 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x00, 0x00, /* R38 - R3F */ 15098615454SKuninori Morimoto 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R40 - R4F */ 15198615454SKuninori Morimoto 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R48 - R4F */ 15298615454SKuninori Morimoto 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R50 - R57 */ 15398615454SKuninori Morimoto 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R58 - R5F */ 15498615454SKuninori Morimoto 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R60 - R67 */ 15598615454SKuninori Morimoto 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R68 - R6F */ 15698615454SKuninori Morimoto 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R70 - R77 */ 15798615454SKuninori Morimoto 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x54, 0x00, /* R78 - R7F */ 15898615454SKuninori Morimoto 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x00, /* R80 - R87 */ 15998615454SKuninori Morimoto 0x00, /* R88 */ 16098615454SKuninori Morimoto }; 16198615454SKuninori Morimoto 16298615454SKuninori Morimoto /* 16398615454SKuninori Morimoto * Read da7210 register cache 16498615454SKuninori Morimoto */ 16598615454SKuninori Morimoto static inline u32 da7210_read_reg_cache(struct snd_soc_codec *codec, u32 reg) 16698615454SKuninori Morimoto { 16798615454SKuninori Morimoto u8 *cache = codec->reg_cache; 16898615454SKuninori Morimoto BUG_ON(reg > ARRAY_SIZE(da7210_reg)); 16998615454SKuninori Morimoto return cache[reg]; 17098615454SKuninori Morimoto } 17198615454SKuninori Morimoto 17298615454SKuninori Morimoto /* 17398615454SKuninori Morimoto * Write to the da7210 register space 17498615454SKuninori Morimoto */ 17598615454SKuninori Morimoto static int da7210_write(struct snd_soc_codec *codec, u32 reg, u32 value) 17698615454SKuninori Morimoto { 17798615454SKuninori Morimoto u8 *cache = codec->reg_cache; 17898615454SKuninori Morimoto u8 data[2]; 17998615454SKuninori Morimoto 18098615454SKuninori Morimoto BUG_ON(codec->volatile_register); 18198615454SKuninori Morimoto 18298615454SKuninori Morimoto data[0] = reg & 0xff; 18398615454SKuninori Morimoto data[1] = value & 0xff; 18498615454SKuninori Morimoto 18598615454SKuninori Morimoto if (reg >= codec->reg_cache_size) 18698615454SKuninori Morimoto return -EIO; 18798615454SKuninori Morimoto 18898615454SKuninori Morimoto if (2 != codec->hw_write(codec->control_data, data, 2)) 18998615454SKuninori Morimoto return -EIO; 19098615454SKuninori Morimoto 19198615454SKuninori Morimoto cache[reg] = value; 19298615454SKuninori Morimoto return 0; 19398615454SKuninori Morimoto } 19498615454SKuninori Morimoto 19598615454SKuninori Morimoto /* 19698615454SKuninori Morimoto * Read from the da7210 register space. 19798615454SKuninori Morimoto */ 19898615454SKuninori Morimoto static inline u32 da7210_read(struct snd_soc_codec *codec, u32 reg) 19998615454SKuninori Morimoto { 20098615454SKuninori Morimoto if (DA7210_STATUS == reg) 20198615454SKuninori Morimoto return i2c_smbus_read_byte_data(codec->control_data, reg); 20298615454SKuninori Morimoto 20398615454SKuninori Morimoto return da7210_read_reg_cache(codec, reg); 20498615454SKuninori Morimoto } 20598615454SKuninori Morimoto 20698615454SKuninori Morimoto static int da7210_startup(struct snd_pcm_substream *substream, 20798615454SKuninori Morimoto struct snd_soc_dai *dai) 20898615454SKuninori Morimoto { 20998615454SKuninori Morimoto int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 21098615454SKuninori Morimoto struct snd_soc_codec *codec = dai->codec; 21198615454SKuninori Morimoto 21298615454SKuninori Morimoto if (is_play) { 21398615454SKuninori Morimoto /* PlayBack Volume 40 */ 21498615454SKuninori Morimoto snd_soc_update_bits(codec, DA7210_HP_L_VOL, 0x3F, 40); 21598615454SKuninori Morimoto snd_soc_update_bits(codec, DA7210_HP_R_VOL, 0x3F, 40); 21698615454SKuninori Morimoto 21798615454SKuninori Morimoto /* Enable Out */ 21898615454SKuninori Morimoto snd_soc_update_bits(codec, DA7210_OUTMIX_L, 0x1F, 0x10); 21998615454SKuninori Morimoto snd_soc_update_bits(codec, DA7210_OUTMIX_R, 0x1F, 0x10); 22098615454SKuninori Morimoto 22198615454SKuninori Morimoto } else { 22298615454SKuninori Morimoto /* Volume 7 */ 22398615454SKuninori Morimoto snd_soc_update_bits(codec, DA7210_MIC_L, 0x7, 0x7); 22498615454SKuninori Morimoto snd_soc_update_bits(codec, DA7210_MIC_R, 0x7, 0x7); 22598615454SKuninori Morimoto 22698615454SKuninori Morimoto /* Enable Mic */ 22798615454SKuninori Morimoto snd_soc_update_bits(codec, DA7210_INMIX_L, 0x1F, 0x1); 22898615454SKuninori Morimoto snd_soc_update_bits(codec, DA7210_INMIX_R, 0x1F, 0x1); 22998615454SKuninori Morimoto } 23098615454SKuninori Morimoto 23198615454SKuninori Morimoto return 0; 23298615454SKuninori Morimoto } 23398615454SKuninori Morimoto 23498615454SKuninori Morimoto /* 23598615454SKuninori Morimoto * Set PCM DAI word length. 23698615454SKuninori Morimoto */ 23798615454SKuninori Morimoto static int da7210_hw_params(struct snd_pcm_substream *substream, 23898615454SKuninori Morimoto struct snd_pcm_hw_params *params, 23998615454SKuninori Morimoto struct snd_soc_dai *dai) 24098615454SKuninori Morimoto { 24198615454SKuninori Morimoto struct snd_soc_pcm_runtime *rtd = substream->private_data; 24298615454SKuninori Morimoto struct snd_soc_device *socdev = rtd->socdev; 24398615454SKuninori Morimoto struct snd_soc_codec *codec = socdev->card->codec; 24498615454SKuninori Morimoto u32 dai_cfg1; 24598615454SKuninori Morimoto u32 reg, mask; 24698615454SKuninori Morimoto 24798615454SKuninori Morimoto /* set DAI source to Left and Right ADC */ 24898615454SKuninori Morimoto da7210_write(codec, DA7210_DAI_SRC_SEL, 24998615454SKuninori Morimoto DA7210_DAI_OUT_R_SRC | DA7210_DAI_OUT_L_SRC); 25098615454SKuninori Morimoto 25198615454SKuninori Morimoto /* Enable DAI */ 25298615454SKuninori Morimoto da7210_write(codec, DA7210_DAI_CFG3, DA7210_DAI_OE | DA7210_DAI_EN); 25398615454SKuninori Morimoto 25498615454SKuninori Morimoto dai_cfg1 = 0xFC & da7210_read(codec, DA7210_DAI_CFG1); 25598615454SKuninori Morimoto 25698615454SKuninori Morimoto switch (params_format(params)) { 25798615454SKuninori Morimoto case SNDRV_PCM_FORMAT_S16_LE: 25898615454SKuninori Morimoto dai_cfg1 |= DA7210_DAI_WORD_S16_LE; 25998615454SKuninori Morimoto break; 26098615454SKuninori Morimoto case SNDRV_PCM_FORMAT_S24_LE: 26198615454SKuninori Morimoto dai_cfg1 |= DA7210_DAI_WORD_S24_LE; 26298615454SKuninori Morimoto break; 26398615454SKuninori Morimoto default: 26498615454SKuninori Morimoto return -EINVAL; 26598615454SKuninori Morimoto } 26698615454SKuninori Morimoto 26798615454SKuninori Morimoto da7210_write(codec, DA7210_DAI_CFG1, dai_cfg1); 26898615454SKuninori Morimoto 26998615454SKuninori Morimoto /* FIXME 27098615454SKuninori Morimoto * 27198615454SKuninori Morimoto * It support 48K only now 27298615454SKuninori Morimoto */ 27398615454SKuninori Morimoto switch (params_rate(params)) { 27498615454SKuninori Morimoto case 48000: 27598615454SKuninori Morimoto if (SNDRV_PCM_STREAM_PLAYBACK == substream->stream) { 27698615454SKuninori Morimoto reg = DA7210_DAC_HPF; 27798615454SKuninori Morimoto mask = DA7210_DAC_VOICE_EN; 27898615454SKuninori Morimoto } else { 27998615454SKuninori Morimoto reg = DA7210_ADC_HPF; 28098615454SKuninori Morimoto mask = DA7210_ADC_VOICE_EN; 28198615454SKuninori Morimoto } 28298615454SKuninori Morimoto break; 28398615454SKuninori Morimoto default: 28498615454SKuninori Morimoto return -EINVAL; 28598615454SKuninori Morimoto } 28698615454SKuninori Morimoto 28798615454SKuninori Morimoto snd_soc_update_bits(codec, reg, mask, 0); 28898615454SKuninori Morimoto 28998615454SKuninori Morimoto return 0; 29098615454SKuninori Morimoto } 29198615454SKuninori Morimoto 29298615454SKuninori Morimoto /* 29398615454SKuninori Morimoto * Set DAI mode and Format 29498615454SKuninori Morimoto */ 29598615454SKuninori Morimoto static int da7210_set_dai_fmt(struct snd_soc_dai *codec_dai, u32 fmt) 29698615454SKuninori Morimoto { 29798615454SKuninori Morimoto struct snd_soc_codec *codec = codec_dai->codec; 29898615454SKuninori Morimoto u32 dai_cfg1; 29998615454SKuninori Morimoto u32 dai_cfg3; 30098615454SKuninori Morimoto 30198615454SKuninori Morimoto dai_cfg1 = 0x7f & da7210_read(codec, DA7210_DAI_CFG1); 30298615454SKuninori Morimoto dai_cfg3 = 0xfc & da7210_read(codec, DA7210_DAI_CFG3); 30398615454SKuninori Morimoto 30498615454SKuninori Morimoto switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 30598615454SKuninori Morimoto case SND_SOC_DAIFMT_CBM_CFM: 30698615454SKuninori Morimoto dai_cfg1 |= DA7210_DAI_MODE_MASTER; 30798615454SKuninori Morimoto break; 30898615454SKuninori Morimoto default: 30998615454SKuninori Morimoto return -EINVAL; 31098615454SKuninori Morimoto } 31198615454SKuninori Morimoto 31298615454SKuninori Morimoto /* FIXME 31398615454SKuninori Morimoto * 31498615454SKuninori Morimoto * It support I2S only now 31598615454SKuninori Morimoto */ 31698615454SKuninori Morimoto switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 31798615454SKuninori Morimoto case SND_SOC_DAIFMT_I2S: 31898615454SKuninori Morimoto dai_cfg3 |= DA7210_DAI_FORMAT_I2SMODE; 31998615454SKuninori Morimoto break; 32098615454SKuninori Morimoto default: 32198615454SKuninori Morimoto return -EINVAL; 32298615454SKuninori Morimoto } 32398615454SKuninori Morimoto 32498615454SKuninori Morimoto /* FIXME 32598615454SKuninori Morimoto * 32698615454SKuninori Morimoto * It support 64bit data transmission only now 32798615454SKuninori Morimoto */ 32898615454SKuninori Morimoto dai_cfg1 |= DA7210_DAI_FLEN_64BIT; 32998615454SKuninori Morimoto 33098615454SKuninori Morimoto da7210_write(codec, DA7210_DAI_CFG1, dai_cfg1); 33198615454SKuninori Morimoto da7210_write(codec, DA7210_DAI_CFG3, dai_cfg3); 33298615454SKuninori Morimoto 33398615454SKuninori Morimoto return 0; 33498615454SKuninori Morimoto } 33598615454SKuninori Morimoto 33698615454SKuninori Morimoto #define DA7210_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE) 33798615454SKuninori Morimoto 33898615454SKuninori Morimoto /* DAI operations */ 33998615454SKuninori Morimoto static struct snd_soc_dai_ops da7210_dai_ops = { 34098615454SKuninori Morimoto .startup = da7210_startup, 34198615454SKuninori Morimoto .hw_params = da7210_hw_params, 34298615454SKuninori Morimoto .set_fmt = da7210_set_dai_fmt, 34398615454SKuninori Morimoto }; 34498615454SKuninori Morimoto 34598615454SKuninori Morimoto struct snd_soc_dai da7210_dai = { 34698615454SKuninori Morimoto .name = "DA7210 IIS", 34798615454SKuninori Morimoto .id = 0, 34898615454SKuninori Morimoto /* playback capabilities */ 34998615454SKuninori Morimoto .playback = { 35098615454SKuninori Morimoto .stream_name = "Playback", 35198615454SKuninori Morimoto .channels_min = 1, 35298615454SKuninori Morimoto .channels_max = 2, 35398615454SKuninori Morimoto .rates = SNDRV_PCM_RATE_8000_96000, 35498615454SKuninori Morimoto .formats = DA7210_FORMATS, 35598615454SKuninori Morimoto }, 35698615454SKuninori Morimoto /* capture capabilities */ 35798615454SKuninori Morimoto .capture = { 35898615454SKuninori Morimoto .stream_name = "Capture", 35998615454SKuninori Morimoto .channels_min = 1, 36098615454SKuninori Morimoto .channels_max = 2, 36198615454SKuninori Morimoto .rates = SNDRV_PCM_RATE_8000_96000, 36298615454SKuninori Morimoto .formats = DA7210_FORMATS, 36398615454SKuninori Morimoto }, 36498615454SKuninori Morimoto .ops = &da7210_dai_ops, 36598615454SKuninori Morimoto }; 36698615454SKuninori Morimoto EXPORT_SYMBOL_GPL(da7210_dai); 36798615454SKuninori Morimoto 36898615454SKuninori Morimoto /* 36998615454SKuninori Morimoto * Initialize the DA7210 driver 37098615454SKuninori Morimoto * register the mixer and dsp interfaces with the kernel 37198615454SKuninori Morimoto */ 37298615454SKuninori Morimoto static int da7210_init(struct da7210_priv *da7210) 37398615454SKuninori Morimoto { 37498615454SKuninori Morimoto struct snd_soc_codec *codec = &da7210->codec; 37598615454SKuninori Morimoto int ret = 0; 37698615454SKuninori Morimoto 37798615454SKuninori Morimoto if (da7210_codec) { 37898615454SKuninori Morimoto dev_err(codec->dev, "Another da7210 is registered\n"); 37998615454SKuninori Morimoto return -EINVAL; 38098615454SKuninori Morimoto } 38198615454SKuninori Morimoto 38298615454SKuninori Morimoto mutex_init(&codec->mutex); 38398615454SKuninori Morimoto INIT_LIST_HEAD(&codec->dapm_widgets); 38498615454SKuninori Morimoto INIT_LIST_HEAD(&codec->dapm_paths); 38598615454SKuninori Morimoto 38698615454SKuninori Morimoto codec->private_data = da7210; 38798615454SKuninori Morimoto codec->name = "DA7210"; 38898615454SKuninori Morimoto codec->owner = THIS_MODULE; 38998615454SKuninori Morimoto codec->read = da7210_read; 39098615454SKuninori Morimoto codec->write = da7210_write; 39198615454SKuninori Morimoto codec->dai = &da7210_dai; 39298615454SKuninori Morimoto codec->num_dai = 1; 39398615454SKuninori Morimoto codec->hw_write = (hw_write_t)i2c_master_send; 39498615454SKuninori Morimoto codec->reg_cache_size = ARRAY_SIZE(da7210_reg); 39598615454SKuninori Morimoto codec->reg_cache = kmemdup(da7210_reg, 39698615454SKuninori Morimoto sizeof(da7210_reg), GFP_KERNEL); 39798615454SKuninori Morimoto 39898615454SKuninori Morimoto if (!codec->reg_cache) 39998615454SKuninori Morimoto return -ENOMEM; 40098615454SKuninori Morimoto 40198615454SKuninori Morimoto da7210_dai.dev = codec->dev; 40298615454SKuninori Morimoto da7210_codec = codec; 40398615454SKuninori Morimoto 40498615454SKuninori Morimoto ret = snd_soc_register_codec(codec); 40598615454SKuninori Morimoto if (ret) { 40698615454SKuninori Morimoto dev_err(codec->dev, "Failed to register CODEC: %d\n", ret); 40798615454SKuninori Morimoto goto init_err; 40898615454SKuninori Morimoto } 40998615454SKuninori Morimoto 41098615454SKuninori Morimoto ret = snd_soc_register_dai(&da7210_dai); 41198615454SKuninori Morimoto if (ret) { 41298615454SKuninori Morimoto dev_err(codec->dev, "Failed to register DAI: %d\n", ret); 41398615454SKuninori Morimoto goto init_err; 41498615454SKuninori Morimoto } 41598615454SKuninori Morimoto 41698615454SKuninori Morimoto /* FIXME 41798615454SKuninori Morimoto * 41898615454SKuninori Morimoto * This driver use fixed value here 41998615454SKuninori Morimoto */ 42098615454SKuninori Morimoto 42198615454SKuninori Morimoto /* 42298615454SKuninori Morimoto * ADC settings 42398615454SKuninori Morimoto */ 42498615454SKuninori Morimoto 42598615454SKuninori Morimoto /* Enable Left & Right MIC PGA and Mic Bias */ 42698615454SKuninori Morimoto da7210_write(codec, DA7210_MIC_L, DA7210_MIC_L_EN | DA7210_MICBIAS_EN); 42798615454SKuninori Morimoto da7210_write(codec, DA7210_MIC_R, DA7210_MIC_R_EN); 42898615454SKuninori Morimoto 42998615454SKuninori Morimoto /* Enable Left and Right input PGA */ 43098615454SKuninori Morimoto da7210_write(codec, DA7210_INMIX_L, DA7210_IN_L_EN); 43198615454SKuninori Morimoto da7210_write(codec, DA7210_INMIX_R, DA7210_IN_R_EN); 43298615454SKuninori Morimoto 43398615454SKuninori Morimoto /* Enable Left and Right ADC */ 43498615454SKuninori Morimoto da7210_write(codec, DA7210_ADC, DA7210_ADC_L_EN | DA7210_ADC_R_EN); 43598615454SKuninori Morimoto 43698615454SKuninori Morimoto /* 43798615454SKuninori Morimoto * DAC settings 43898615454SKuninori Morimoto */ 43998615454SKuninori Morimoto 44098615454SKuninori Morimoto /* Enable Left and Right DAC */ 44198615454SKuninori Morimoto da7210_write(codec, DA7210_DAC_SEL, 44298615454SKuninori Morimoto DA7210_DAC_L_SRC_DAI_L | DA7210_DAC_L_EN | 44398615454SKuninori Morimoto DA7210_DAC_R_SRC_DAI_R | DA7210_DAC_R_EN); 44498615454SKuninori Morimoto 44598615454SKuninori Morimoto /* Enable Left and Right out PGA */ 44698615454SKuninori Morimoto da7210_write(codec, DA7210_OUTMIX_L, DA7210_OUT_L_EN); 44798615454SKuninori Morimoto da7210_write(codec, DA7210_OUTMIX_R, DA7210_OUT_R_EN); 44898615454SKuninori Morimoto 44998615454SKuninori Morimoto /* Enable Left and Right HeadPhone PGA */ 45098615454SKuninori Morimoto da7210_write(codec, DA7210_HP_CFG, 45198615454SKuninori Morimoto DA7210_HP_2CAP_MODE | DA7210_HP_SENSE_EN | 45298615454SKuninori Morimoto DA7210_HP_L_EN | DA7210_HP_MODE | DA7210_HP_R_EN); 45398615454SKuninori Morimoto 45498615454SKuninori Morimoto /* Diable PLL and bypass it */ 45598615454SKuninori Morimoto da7210_write(codec, DA7210_PLL, DA7210_PLL_FS_48000); 45698615454SKuninori Morimoto 45798615454SKuninori Morimoto /* Bypass PLL and set MCLK freq rang to 10-20MHz */ 45898615454SKuninori Morimoto da7210_write(codec, DA7210_PLL_DIV3, 45998615454SKuninori Morimoto DA7210_MCLK_RANGE_10_20_MHZ | DA7210_PLL_BYP); 46098615454SKuninori Morimoto 46198615454SKuninori Morimoto /* Activate all enabled subsystem */ 46298615454SKuninori Morimoto da7210_write(codec, DA7210_STARTUP1, DA7210_SC_MST_EN); 46398615454SKuninori Morimoto 46498615454SKuninori Morimoto return ret; 46598615454SKuninori Morimoto 46698615454SKuninori Morimoto init_err: 46798615454SKuninori Morimoto kfree(codec->reg_cache); 46898615454SKuninori Morimoto codec->reg_cache = NULL; 46998615454SKuninori Morimoto 47098615454SKuninori Morimoto return ret; 47198615454SKuninori Morimoto 47298615454SKuninori Morimoto } 47398615454SKuninori Morimoto 47498615454SKuninori Morimoto #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 475735fe4cfSMark Brown static int __devinit da7210_i2c_probe(struct i2c_client *i2c, 47698615454SKuninori Morimoto const struct i2c_device_id *id) 47798615454SKuninori Morimoto { 47898615454SKuninori Morimoto struct da7210_priv *da7210; 47998615454SKuninori Morimoto struct snd_soc_codec *codec; 48098615454SKuninori Morimoto int ret; 48198615454SKuninori Morimoto 48298615454SKuninori Morimoto da7210 = kzalloc(sizeof(struct da7210_priv), GFP_KERNEL); 48398615454SKuninori Morimoto if (!da7210) 48498615454SKuninori Morimoto return -ENOMEM; 48598615454SKuninori Morimoto 48698615454SKuninori Morimoto codec = &da7210->codec; 48798615454SKuninori Morimoto codec->dev = &i2c->dev; 48898615454SKuninori Morimoto 48998615454SKuninori Morimoto i2c_set_clientdata(i2c, da7210); 49098615454SKuninori Morimoto codec->control_data = i2c; 49198615454SKuninori Morimoto 49298615454SKuninori Morimoto ret = da7210_init(da7210); 49398615454SKuninori Morimoto if (ret < 0) 49498615454SKuninori Morimoto pr_err("Failed to initialise da7210 audio codec\n"); 49598615454SKuninori Morimoto 49698615454SKuninori Morimoto return ret; 49798615454SKuninori Morimoto } 49898615454SKuninori Morimoto 499735fe4cfSMark Brown static int __devexit da7210_i2c_remove(struct i2c_client *client) 50098615454SKuninori Morimoto { 50198615454SKuninori Morimoto struct da7210_priv *da7210 = i2c_get_clientdata(client); 50298615454SKuninori Morimoto 50398615454SKuninori Morimoto snd_soc_unregister_dai(&da7210_dai); 50498615454SKuninori Morimoto kfree(da7210->codec.reg_cache); 50598615454SKuninori Morimoto kfree(da7210); 50698615454SKuninori Morimoto da7210_codec = NULL; 50798615454SKuninori Morimoto 50898615454SKuninori Morimoto return 0; 50998615454SKuninori Morimoto } 51098615454SKuninori Morimoto 51198615454SKuninori Morimoto static const struct i2c_device_id da7210_i2c_id[] = { 51298615454SKuninori Morimoto { "da7210", 0 }, 51398615454SKuninori Morimoto { } 51498615454SKuninori Morimoto }; 51598615454SKuninori Morimoto MODULE_DEVICE_TABLE(i2c, da7210_i2c_id); 51698615454SKuninori Morimoto 51798615454SKuninori Morimoto /* I2C codec control layer */ 51898615454SKuninori Morimoto static struct i2c_driver da7210_i2c_driver = { 51998615454SKuninori Morimoto .driver = { 52098615454SKuninori Morimoto .name = "DA7210 I2C Codec", 52198615454SKuninori Morimoto .owner = THIS_MODULE, 52298615454SKuninori Morimoto }, 52398615454SKuninori Morimoto .probe = da7210_i2c_probe, 52498615454SKuninori Morimoto .remove = __devexit_p(da7210_i2c_remove), 52598615454SKuninori Morimoto .id_table = da7210_i2c_id, 52698615454SKuninori Morimoto }; 52798615454SKuninori Morimoto #endif 52898615454SKuninori Morimoto 52998615454SKuninori Morimoto static int da7210_probe(struct platform_device *pdev) 53098615454SKuninori Morimoto { 53198615454SKuninori Morimoto struct snd_soc_device *socdev = platform_get_drvdata(pdev); 53298615454SKuninori Morimoto struct snd_soc_codec *codec; 53398615454SKuninori Morimoto int ret; 53498615454SKuninori Morimoto 53598615454SKuninori Morimoto if (!da7210_codec) { 53698615454SKuninori Morimoto dev_err(&pdev->dev, "Codec device not registered\n"); 53798615454SKuninori Morimoto return -ENODEV; 53898615454SKuninori Morimoto } 53998615454SKuninori Morimoto 54098615454SKuninori Morimoto socdev->card->codec = da7210_codec; 54198615454SKuninori Morimoto codec = da7210_codec; 54298615454SKuninori Morimoto 54398615454SKuninori Morimoto /* Register pcms */ 54498615454SKuninori Morimoto ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); 54598615454SKuninori Morimoto if (ret < 0) 54698615454SKuninori Morimoto goto pcm_err; 54798615454SKuninori Morimoto 54898615454SKuninori Morimoto dev_info(&pdev->dev, "DA7210 Audio Codec %s\n", DA7210_VERSION); 54998615454SKuninori Morimoto 55098615454SKuninori Morimoto pcm_err: 55198615454SKuninori Morimoto return ret; 55298615454SKuninori Morimoto } 55398615454SKuninori Morimoto 55498615454SKuninori Morimoto static int da7210_remove(struct platform_device *pdev) 55598615454SKuninori Morimoto { 55698615454SKuninori Morimoto struct snd_soc_device *socdev = platform_get_drvdata(pdev); 55798615454SKuninori Morimoto 55898615454SKuninori Morimoto snd_soc_free_pcms(socdev); 55998615454SKuninori Morimoto snd_soc_dapm_free(socdev); 56098615454SKuninori Morimoto 56198615454SKuninori Morimoto return 0; 56298615454SKuninori Morimoto } 56398615454SKuninori Morimoto 56498615454SKuninori Morimoto struct snd_soc_codec_device soc_codec_dev_da7210 = { 56598615454SKuninori Morimoto .probe = da7210_probe, 56698615454SKuninori Morimoto .remove = da7210_remove, 56798615454SKuninori Morimoto }; 56898615454SKuninori Morimoto EXPORT_SYMBOL_GPL(soc_codec_dev_da7210); 56998615454SKuninori Morimoto 57098615454SKuninori Morimoto static int __init da7210_modinit(void) 57198615454SKuninori Morimoto { 57298615454SKuninori Morimoto int ret = 0; 57398615454SKuninori Morimoto #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 57498615454SKuninori Morimoto ret = i2c_add_driver(&da7210_i2c_driver); 57598615454SKuninori Morimoto #endif 57698615454SKuninori Morimoto return ret; 57798615454SKuninori Morimoto } 57898615454SKuninori Morimoto module_init(da7210_modinit); 57998615454SKuninori Morimoto 58098615454SKuninori Morimoto static void __exit da7210_exit(void) 58198615454SKuninori Morimoto { 58298615454SKuninori Morimoto #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 58398615454SKuninori Morimoto i2c_del_driver(&da7210_i2c_driver); 58498615454SKuninori Morimoto #endif 58598615454SKuninori Morimoto } 58698615454SKuninori Morimoto module_exit(da7210_exit); 58798615454SKuninori Morimoto 58898615454SKuninori Morimoto MODULE_DESCRIPTION("ASoC DA7210 driver"); 58998615454SKuninori Morimoto MODULE_AUTHOR("David Chen, Kuninori Morimoto"); 59098615454SKuninori Morimoto MODULE_LICENSE("GPL"); 591