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