ASoC: add RT286 CODEC driver
Subsystems (3)
PATCH v5 1/1 ASoC: add RT286 CODEC driver
From: Bard Liao <[email protected]>
This patch adds the ALC286 codec driver.
ALC286 is a dual mode codec, which can run as HD-A or I2S mode.
It is controlled by HD-A verb commands via I2C protocol.
The following is the I/O difference between ALC286 and general I2S codecs.
1. A HD-A verb command contains three parts, NID, VID, and PID.
And an I2S command contains only two parts: address and data.
2. Not only the register address is written, but the read command also includes the entire write command.
3. rt286 uses different registers for read and write the same bits. As a result, standard regmap is difficult to be used on ALC286.
We don't request a standard I/O by snd_soc_codec_set_cache_io anymore. Now we have ,reg_write and .reg_read functions for ALC286's I/O. And we don't use cache due to item 3 above.
Some dummy registers (address <= 0xff) are defined for dapm routing. Thhe dummy registers are cache only.
Due to item 2 above, HD-A verb commands are put into the address part of regmap. When we issue HD-A verb write commands, the data part of regmap is zero.
---
The jack detection function is done by Lewandowski Gustaw. So I add Gustaw's Signed-off-by line in this patch
The difference between this version and previous version is listed below.
* Include <sound/hda_verbs.h> instead of hda_codec.h
* Fix rt286_update_bits doesn't write the new value to codec issue.
* Apply different settings for system clock = 19.2MHz and 24MHz.
* Use .set_bclk_ratio instead of .set_dfs.
* Change ACPI number from INT33CA to INT343A.
* Other minor changes.
--- include/sound/rt286.h | 19 + sound/soc/codecs/Kconfig | 4 + sound/soc/codecs/Makefile | 2 + sound/soc/codecs/rt286.c | 1379 +++++++++++++++++++++++++++++++++++++++++++++ sound/soc/codecs/rt286.h | 159 ++++++ 5 files changed, 1563 insertions(+) create mode 100644 include/sound/rt286.h create mode 100644 sound/soc/codecs/rt286.c create mode 100644 sound/soc/codecs/rt286.h
include/sound/rt286.h new file +19 -0
@@ -0,0 +1,19 @@ +/* +* linux/sound/rt286.h -- Platform data for RT286 +* +* Copyright 2013 Realtek Microelectronics +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#ifndef __LINUX_SND_RT286_H +#define __LINUX_SND_RT286_H + +struct rt286_platform_data { +bool cbj_en;/*combo jack enable*/ +bool gpio2_en;/*GPIO2 enable*/ +}; + +#endif
sound/soc/codecs/Kconfig +4 -0
@@ -68,6 +68,7 @@ config SND_SOC_ALL_CODECS select SND_SOC_PCM3008 select SND_SOC_PCM512x_I2C if I2C select SND_SOC_PCM512x_SPI if SPI_MASTER + select SND_SOC_RT286 if I2C select SND_SOC_RT5631 if I2C select SND_SOC_RT5640 if I2C select SND_SOC_SGTL5000 if I2C
@@ -379,6 +380,9 @@ config SND_SOC_PCM512x_SPI select SND_SOC_PCM512x select REGMAP_SPI +config SND_SOC_RT286 + tristate + config SND_SOC_RT5631 tristate
sound/soc/codecs/Makefile +2 -0
@@ -56,6 +56,7 @@ snd-soc-pcm3008-objs := pcm3008.o snd-soc-pcm512x-objs := pcm512x.o snd-soc-pcm512x-i2c-objs := pcm512x-i2c.o snd-soc-pcm512x-spi-objs := pcm512x-spi.o +snd-soc-rt286-objs := rt286.o snd-soc-rt5631-objs := rt5631.o snd-soc-rt5640-objs := rt5640.o snd-soc-sgtl5000-objs := sgtl5000.o
@@ -204,6 +205,7 @@ obj-$(CONFIG_SND_SOC_PCM3008) += snd-soc-pcm3008.o obj-$(CONFIG_SND_SOC_PCM512x) += snd-soc-pcm512x.o obj-$(CONFIG_SND_SOC_PCM512x_I2C) += snd-soc-pcm512x-i2c.o obj-$(CONFIG_SND_SOC_PCM512x_SPI) += snd-soc-pcm512x-spi.o +obj-$(CONFIG_SND_SOC_RT286) += snd-soc-rt286.o obj-$(CONFIG_SND_SOC_RT5631) += snd-soc-rt5631.o obj-$(CONFIG_SND_SOC_RT5640) += snd-soc-rt5640.o obj-$(CONFIG_SND_SOC_SGTL5000) += snd-soc-sgtl5000.o
sound/soc/codecs/rt286.c new file +1379 -0
1,379 lines not on this page · raw message
sound/soc/codecs/rt286.h new file +159 -0
159 lines not on this page · raw message
Signature · 1 line
1.8.1.1.439.g50a6b54
On 03/11/2014 08:11 AM, [email protected] wrote:
[...]
Quoted 21 lines
sound/soc/codecs/rt286.c new file +21
-0+static unsigned int rt286_reg_cache[] = { + [RT286_AUDIO_FUNCTION_GROUP ] =0x0000 , + [RT286_DAC_OUT1 ] =0x7f7f , + [RT286_DAC_OUT2 ] =0x7f7f , + [RT286_SPDIF ] =0x0000 , + [RT286_ADC_IN1 ] =0x4343 , + [RT286_ADC_IN2 ] =0x4343 , + [RT286_MIC1 ] =0x0000 , + [RT286_MIXER_IN ] =0x000b , + [RT286_MIXER_OUT1 ] =0x0002 , + [RT286_MIXER_OUT2 ] =0x0000 , + [RT286_DMIC1 ] =0x0000 , + [RT286_DMIC2 ] =0x0000 , + [RT286_LINE1 ] =0x0000 , + [RT286_BEEP ] =0x0000 , + [RT286_VENDOR_REGISTERS ] =0x0000 , + [RT286_SPK_OUT ] =0x8080 , + [RT286_HP_OUT ] =0x8080 , + [RT286_MIXER_IN1 ] =0x0000 , + [RT286_MIXER_IN2 ] =0x0000 , +};
sound/soc/codecs/rt286.c line 73
Quoted 10 lines
sound/soc/codecs/rt286.c new file +10
-0+ +static int rt286_hw_read (void *context,unsigned int reg,unsigned int *value) +{ +struct i2c_client *client = context; +struct i2c_msg xfer[2 ]; +int ret; +unsigned int buf =0x0 ; + +if (reg <=0xff ) {/*read cache*/ + *value = rt286_reg_cache[reg];
Any reason particular reason to not use regmap caching? Also your cache is driver global which means different device instances will use the same cache, which will probably have very weired effects. Also you cache has less than 0xff entries, so the check is completely bogus.
sound/soc/codecs/rt286.c line 76
Quoted 3 lines
sound/soc/codecs/rt286.c new file +3
-0+return 0 ; + } + reg =cpu_to_be32 (reg);
Don't store values of different endianness in the same variable, all the static code checkers will complain about this. Use a __be32 type variable to hold the result of cpu_to_be32.
Quoted 23 lines
sound/soc/codecs/rt286.c new file +23
-0+/* Write register */ + xfer[0 ].addr = client->addr; + xfer[0 ].flags =0 ; + xfer[0 ].len =4 ; + xfer[0 ].buf = (u8 *)® + +/* Read data */ + xfer[1 ].addr = client->addr; + xfer[1 ].flags =I2C_M_RD ; + xfer[1 ].len =4 ; + xfer[1 ].buf = (u8 *)&buf; + + ret =i2c_transfer (client->adapter, xfer,2 ); +if (ret <0 ) +return ret; +else if (ret !=2 ) +return -EIO ; + + *value =be32_to_cpu (buf); + +return 0 ; +} +
[...]
Quoted 12 lines
sound/soc/codecs/rt286.c new file +12
-0+static int rt286_update_bits (struct snd_soc_codec *codec,unsigned int vid, +unsigned int nid,unsigned int data, +unsigned int mask,unsigned int value) +{ +struct rt286_priv *rt286 =snd_soc_codec_get_drvdata (codec); +unsigned int old, new, verb; +int change, ret; + + verb =VERB_CMD ((vid |0x800 ), nid, data); +regmap_read (rt286->regmap, verb, &old); + new = (old & ~mask) | (value & mask); + change = old != new;
sound/soc/codecs/rt286.c line 145
Quoted 10 lines
sound/soc/codecs/rt286.c new file +10
-0+ +if (change) { + verb =VERB_CMD (vid, nid, new); + ret =regmap_write (rt286->regmap, verb,0 ); +if (ret <0 ) { +dev_err (codec->dev, +"Failed to write private reg: %d\n" , ret); + goto err; + } + }
Can't this use regmap_update_bits()?
Quoted 5 lines
sound/soc/codecs/rt286.c new file +5
-0+return change; + +err: +return ret; +}
[...]
Quoted 9 lines
sound/soc/codecs/rt286.c new file +9
-0+static int rt286_index_update_bits (struct snd_soc_codec *codec, +unsigned int wid,unsigned int index, +unsigned int mask,unsigned int data) +{ +unsigned int old, new; +int change, ret; + + old =rt286_index_read (codec, wid, index); + new = (old & ~mask) | (data & mask);
sound/soc/codecs/rt286.c line 215
Quoted 10 lines
sound/soc/codecs/rt286.c new file +10
-0+ change = old != new; + +if (change) { + ret =rt286_index_write (codec, wid, index, new); +if (ret <0 ) { +dev_err (codec->dev, +"Failed to write private reg: %d\n" , ret); + goto err; + } + }
Same here.
Quoted 5 lines
sound/soc/codecs/rt286.c new file +5
-0+return change; + +err: +return ret; +}
[...]
Quoted 1 line
+static int rt286_adc_values[] = {
const
Quoted 2 lines
(quoted diff) +2
-0+ 0, 5, +};
[...]
sound/soc/codecs/rt286.c line 1151
Quoted 6 lines
sound/soc/codecs/rt286.c new file +6
-0+static int rt286_set_bclk_ratio (struct snd_soc_dai *dai,unsigned int ratio) +{ +struct snd_soc_codec *codec = dai->codec; + +dev_dbg (codec->dev,"%s ratio=%d\n" , __func__, ratio); +if (50 == ratio)
The ratio is the number of bit-clock cycles per lr-clock cycle.
Quoted 10 lines
sound/soc/codecs/rt286.c new file +10
-0+rt286_index_update_bits (codec,RT286_VENDOR_REGISTERS , +RT286_I2S_CTRL1 ,0x1000 ,0x1000 ); + else + rt286_index_update_bits(codec, RT286_VENDOR_REGISTERS, + RT286_I2S_CTRL1, 0x1000, 0x0); + + +return 0 ; +} +
[...]
sound/soc/codecs/rt286.c line 1253
Quoted 6 lines
sound/soc/codecs/rt286.c new file +6
-0+ +if (rt286->i2c->irq) { +rt286_index_update_bits (codec, +RT286_VENDOR_REGISTERS ,RT286_IRQ_CTRL ,0x2 ,0x2 ); + ret =request_threaded_irq (rt286->i2c->irq,NULL , rt286_irq, +IRQF_TRIGGER_HIGH |IRQF_ONESHOT ,"rt286" , rt286);
This IRQ is never freed.
Quoted 5 lines
sound/soc/codecs/rt286.c new file +5
-0+if (ret !=0 ) { +dev_err (codec->dev, +"Failed to reguest IRQ: %d\n" , ret); +return ret; + }
sound/soc/codecs/rt286.c line 1268
Quoted 10 lines
sound/soc/codecs/rt286.c new file +10
-0+ } + +return 0 ; +} + +#define RT286_STEREO_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000) +#define RT286_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \ + SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8) + +struct snd_soc_dai_ops rt286_aif_dai_ops = {
const
Quoted 16 lines
sound/soc/codecs/rt286.c new file +16
-0+ .hw_params = rt286_hw_params, + .set_fmt = rt286_set_dai_fmt, + .set_sysclk = rt286_set_dai_sysclk, + .set_bclk_ratio = rt286_set_bclk_ratio, +}; + +struct snd_soc_dai_driver rt286_dai[] = { + { + .name ="rt286-aif1" , + .id =RT286_AIF1 , + .playback = { + .stream_name ="AIF1 Playback" , + .channels_min =1 , + .channels_max =2 , + .rates =RT286_STEREO_RATES , + .formats =RT286_FORMATS ,
sound/soc/codecs/rt286.c line 1294
Quoted 10 lines
sound/soc/codecs/rt286.c new file +10
-0+ }, + .capture = { + .stream_name = "AIF1 Capture", + .channels_min = 1, + .channels_max = 2, + .rates = RT286_STEREO_RATES, + .formats =RT286_FORMATS , + }, + .ops = &rt286_aif_dai_ops, + .symmetric_rates = 1,
Indention is a bit strange here.
Quoted 2 lines
(quoted diff) +2
-0+ }, +};
[...]
sound/soc/codecs/rt286.c line 1325
Quoted 7 lines
sound/soc/codecs/rt286.c new file +7
-0+static const struct i2c_device_id rt286_i2c_id[] = { + {"rt286" ,0 }, + {} +}; +MODULE_DEVICE_TABLE (i2c, rt286_i2c_id); + +static struct acpi_device_id rt286_acpi_match[] = {
const
Quoted 5 lines
sound/soc/codecs/rt286.c new file +5
-0+ { "INT343A", 0 }, + {}, +}; +MODULE_DEVICE_TABLE (acpi, rt286_acpi_match); +
sound/soc/codecs/rt286.c line 1340
Quoted 10 lines
sound/soc/codecs/rt286.c new file +10
-0+static int rt286_i2c_probe (struct i2c_client *i2c, +const struct i2c_device_id *id) +{ +struct rt286_platform_data *pdata =dev_get_platdata (&i2c->dev); +struct rt286_priv *rt286; +struct device *dev = &i2c->dev; +int ret; + + rt286 = devm_kzalloc(&i2c->dev, + sizeof(struct rt286_priv),
sizeof(*rt286) is prefered kernel style
Quoted 10 lines
sound/soc/codecs/rt286.c new file +10
-0+ GFP_KERNEL); +if (NULL == rt286) +return -ENOMEM ; + + rt286->regmap =devm_regmap_init (dev,NULL , i2c, &rt286_regmap); +if (IS_ERR (rt286->regmap)) { + ret =PTR_ERR (rt286->regmap); +dev_err (&i2c->dev,"Failed to allocate register map: %d\n" , + ret); +return ret;
sound/soc/codecs/rt286.c line 1360
Quoted 10 lines
sound/soc/codecs/rt286.c new file +10
-0+ } + + rt286->i2c = i2c; +i2c_set_clientdata (i2c, rt286); + +if (pdata) + rt286->pdata = *pdata; + + ret =devm_snd_soc_register_codec (&i2c->dev, &soc_codec_dev_rt286, + rt286_dai,ARRAY_SIZE (rt286_dai));
There is no such thing as devm_snd_soc_register_codec().
Quoted 3 lines
sound/soc/codecs/rt286.c new file +3
-0+ +return ret; +}
Quoted 38 lines
-----Original Message-----
From: Lars-Peter Clausen [mailto:[email protected]]
Sent: Thursday, March 13, 2014 5:16 AM
To: Bard Liao
Cc: [email protected]; [email protected]; Oder Chiou; [email protected]; Gustaw Lewandowski; Flove
Subject: Re: [alsa-devel] [PATCH v5] ASoC: add RT286 CODEC driverOn 03/11/2014 08:11 AM, [email protected] wrote:
[...]Quoted 28 lines
sound/soc/codecs/rt286.c new file +28
-0+static unsigned int rt286_reg_cache[] = { + [RT286_AUDIO_FUNCTION_GROUP ] =0x0000 , + [RT286_DAC_OUT1 ] =0x7f7f , + [RT286_DAC_OUT2 ] =0x7f7f , + [RT286_SPDIF ] =0x0000 , + [RT286_ADC_IN1 ] =0x4343 , + [RT286_ADC_IN2 ] =0x4343 , + [RT286_MIC1 ] =0x0000 , + [RT286_MIXER_IN ] =0x000b , + [RT286_MIXER_OUT1 ] =0x0002 , + [RT286_MIXER_OUT2 ] =0x0000 , + [RT286_DMIC1 ] =0x0000 , + [RT286_DMIC2 ] =0x0000 , + [RT286_LINE1 ] =0x0000 , + [RT286_BEEP ] =0x0000 , + [RT286_VENDOR_REGISTERS ] =0x0000 , + [RT286_SPK_OUT ] =0x8080 , + [RT286_HP_OUT ] =0x8080 , + [RT286_MIXER_IN1 ] =0x0000 , + [RT286_MIXER_IN2 ] =0x0000 , +}; + +static int rt286_hw_read (void *context,unsigned int reg,unsigned +int *value) { +struct i2c_client *client = context; +struct i2c_msg xfer[2 ]; +int ret; +unsigned int buf =0x0 ;
Quoted 8 lines
sound/soc/codecs/rt286.c line 73
Quoted 3 lines
sound/soc/codecs/rt286.c new file +3
-0+ +if (reg <=0xff ) {/*read cache*/ + *value = rt286_reg_cache[reg];Any reason particular reason to not use regmap caching? Also your cache is driver global which means different device instances will use the same cache, which will probably have very weired effects. Also you cache has less than
0xffentries, so the check is completely bogus.
Because rt286 uses different registers for read and write the same bits. So, I can't read the real value from cache.
The cache I am using is for dummy registers actually.
These dummy registers are mainly used for dapm to get the route information. We don't touch the codec when read/write these dummy registers. I will try to put the cache into rt286_priv.
And will change if (reg <= 0xff) to if (reg <= RT286_MIXER_IN2).
Quoted 40 lines
Quoted 24 lines
sound/soc/codecs/rt286.c new file +23
-0+/* Write register */ + xfer[0 ].addr = client->addr; + xfer[0 ].flags =0 ; + xfer[0 ].len =4 ; + xfer[0 ].buf = (u8 *)® + +/* Read data */ + xfer[1 ].addr = client->addr; + xfer[1 ].flags =I2C_M_RD ; + xfer[1 ].len =4 ; + xfer[1 ].buf = (u8 *)&buf; + + ret =i2c_transfer (client->adapter, xfer,2 ); +if (ret <0 ) +return ret; +else if (ret !=2 ) +return -EIO ; + + *value =be32_to_cpu (buf); + +return 0 ; +} +[...]
Quoted 15 lines
sound/soc/codecs/rt286.c new file +15
-0+static int rt286_update_bits (struct snd_soc_codec *codec,unsigned int vid, +unsigned int nid,unsigned int data, +unsigned int mask,unsigned int value) { +struct rt286_priv *rt286 =snd_soc_codec_get_drvdata (codec); +unsigned int old, new, verb; +int change, ret; + + verb =VERB_CMD ((vid |0x800 ), nid, data); +regmap_read (rt286->regmap, verb, &old); + new = (old & ~mask) | (value & mask); + change = old != new; + +if (change) { + verb =VERB_CMD (vid, nid, new); + ret =regmap_write (rt286->regmap, verb,0 );
Quoted 8 lines
sound/soc/codecs/rt286.c line 145
Quoted 6 lines
sound/soc/codecs/rt286.c new file +6
-0+if (ret <0 ) { +dev_err (codec->dev, +"Failed to write private reg: %d\n" , ret); + goto err; + } + }Can't this use
regmap_update_bits()?
rt286 use different data length for read/write protocol. Also it uses different registers for read/write the same bit.
verb = VERB_CMD((vid | 0x800), nid, data);regmap_read(rt286->regmap, verb, &old);
...
verb = VERB_CMD(vid, nid, new);
ret = regmap_write(rt286->regmap, verb, 0);
I use different reg(verb) for regmap_read and regmap_write. And set regmap_write's val variable to 0.
I think regmap_update_bits() will do things like regmap_read(rt286->regmap, verb, &old); regmap_write(rt286->regmap, verb, new);
with the same verb value.
Quoted 19 lines
Quoted 6 lines
sound/soc/codecs/rt286.c new file +5
-0+return change; + +err: +return ret; +}[...]
Quoted 12 lines
sound/soc/codecs/rt286.c new file +12
-0+static int rt286_index_update_bits (struct snd_soc_codec *codec, +unsigned int wid,unsigned int index, +unsigned int mask,unsigned int data) { +unsigned int old, new; +int change, ret; + + old =rt286_index_read (codec, wid, index); + new = (old & ~mask) | (data & mask); + change = old != new; + +if (change) { + ret =rt286_index_write (codec, wid, index, new);
Quoted 8 lines
sound/soc/codecs/rt286.c line 145
Quoted 6 lines
sound/soc/codecs/rt286.c new file +6
-0+if (ret <0 ) { +dev_err (codec->dev, +"Failed to write private reg: %d\n" , ret); + goto err; + } + }Same here.
Same reason as above.
Quoted 4 lines
Quoted 3 lines
(quoted diff) +2
-0+ 0, 5, +};[...]
Quoted 8 lines
sound/soc/codecs/rt286.c line 1151
Quoted 6 lines
sound/soc/codecs/rt286.c new file +6
-0+static int rt286_set_bclk_ratio (struct snd_soc_dai *dai,unsigned int +ratio) { +struct snd_soc_codec *codec = dai->codec; + +dev_dbg (codec->dev,"%s ratio=%d\n" , __func__, ratio); +if (50 == ratio)The ratio is the number of bit-clock cycles per lr-clock cycle.
That is exactly the information we need to know.
Quoted 15 lines
Quoted 15 lines
sound/soc/codecs/rt286.c new file +14
-0+ GFP_KERNEL); +if (NULL == rt286) +return -ENOMEM ; + + rt286->regmap =devm_regmap_init (dev,NULL , i2c, &rt286_regmap); +if (IS_ERR (rt286->regmap)) { + ret =PTR_ERR (rt286->regmap); +dev_err (&i2c->dev,"Failed to allocate register map: %d\n" , + ret); +return ret; + } + + rt286->i2c = i2c; +i2c_set_clientdata (i2c, rt286);
Quoted 8 lines
sound/soc/codecs/rt286.c line 1360
Quoted 6 lines
sound/soc/codecs/rt286.c new file +6
-0+ +if (pdata) + rt286->pdata = *pdata; + + ret =devm_snd_soc_register_codec (&i2c->dev, &soc_codec_dev_rt286, + rt286_dai,ARRAY_SIZE (rt286_dai));There is no such thing as
devm_snd_soc_register_codec().
That is Mark's suggestion.
I suppose devm_snd_soc_register_codec() will be upstreaming soon. Should I use snd_soc_register_codec() in this patch?
Quoted 7 lines
sound/soc/codecs/rt286.c line 98
Quoted 4 lines
sound/soc/codecs/rt286.c new file +3
-0+ +return ret; +}------Please consider the environment before printing this e-mail.
Quoted 26 lines · Bard Liao
Quoted 17 lines
Quoted 1 line
+
[...]
Quoted 15 lines
sound/soc/codecs/rt286.c new file +15
-0+static int rt286_update_bits (struct snd_soc_codec *codec,unsigned int vid, +unsigned int nid,unsigned int data, +unsigned int mask,unsigned int value) { +struct rt286_priv *rt286 =snd_soc_codec_get_drvdata (codec); +unsigned int old, new, verb; +int change, ret; + + verb =VERB_CMD ((vid |0x800 ), nid, data); +regmap_read (rt286->regmap, verb, &old); + new = (old & ~mask) | (value & mask); + change = old != new; + +if (change) { + verb =VERB_CMD (vid, nid, new); + ret =regmap_write (rt286->regmap, verb,0 );Quoted 8 lines
sound/soc/codecs/rt286.c line 145
Quoted 6 lines
sound/soc/codecs/rt286.c new file +6
-0+if (ret <0 ) { +dev_err (codec->dev, +"Failed to write private reg: %d\n" , ret); + goto err; + } + }Can't this use
regmap_update_bits()?
Quoted 8 lines
rt286 use different data length for read/write protocol. Also it uses different registers for read/write the same bit.
verb =
VERB_CMD((vid | 0x800), nid, data);regmap_read(rt286->regmap, verb, &old);
...
verb =VERB_CMD(vid, nid, new);
ret =regmap_write(rt286->regmap, verb, 0);
You need to differentiate between logical and physical addresses. If your device uses different physical addresses for read and write then your read and write functions should do the proper translation from the logical address to the physical address. Looking at include/sound/hda_verbs.h it seems that the GET verbs are always the same as the SET verbs but additionally set bit 11. So bit 11 is your read bit that always needs to be set when reading a register. This is nothing special to the rt286, in fact it is so common that regmap as support for this in the core. See the read_flag_mask for the regmap_config struct.
Quoted 3 lines
I use different reg(verb) for
regmap_readandregmap_write. And set regmap_write's val variable to 0.
So you changed the regmap semantics and do pass the value of the register write via address and set value always to zero. In that it would probably been better to not use regmap at all. But as I said, there doesn't seem to be anything special about the device, you just need to implement the read and write callbacks correctly, then it is no problem to use regmap_update_bits and also regmap level caching.
Quoted 33 lines
I think
regmap_update_bits()will do things likeregmap_read(rt286->regmap, verb, &old);regmap_write(rt286->regmap, verb, new);
with the same verb value.Quoted 19 lines
Quoted 6 lines
sound/soc/codecs/rt286.c new file +5
-0+return change; + +err: +return ret; +}[...]
Quoted 12 lines
sound/soc/codecs/rt286.c new file +12
-0+static int rt286_index_update_bits (struct snd_soc_codec *codec, +unsigned int wid,unsigned int index, +unsigned int mask,unsigned int data) { +unsigned int old, new; +int change, ret; + + old =rt286_index_read (codec, wid, index); + new = (old & ~mask) | (data & mask); + change = old != new; + +if (change) { + ret =rt286_index_write (codec, wid, index, new);Quoted 8 lines
sound/soc/codecs/rt286.c line 145
Quoted 6 lines
sound/soc/codecs/rt286.c new file +6
-0+if (ret <0 ) { +dev_err (codec->dev, +"Failed to write private reg: %d\n" , ret); + goto err; + } + }Same here.
Quoted 1 line
Same reason as above.
rt286_index_read and rt286_index_write seem to implement a paging mechanism. regmap has native support for paging, see regmap_range_cfg. If you can't use regmap for paging implement the paging mechanism in your read/write callbacks.
Quoted 41 lines
Quoted 5 lines
Quoted 4 lines
(quoted diff) +2
-0+ 0, 5, +};[...]
Quoted 8 lines
sound/soc/codecs/rt286.c line 1151
Quoted 6 lines
sound/soc/codecs/rt286.c new file +6
-0+static int rt286_set_bclk_ratio (struct snd_soc_dai *dai,unsigned int +ratio) { +struct snd_soc_codec *codec = dai->codec; + +dev_dbg (codec->dev,"%s ratio=%d\n" , __func__, ratio); +if (50 == ratio)The ratio is the number of bit-clock cycles per lr-clock cycle.
That is exactly the information we need to know.
Quoted 15 lines
Quoted 15 lines
sound/soc/codecs/rt286.c new file +14
-0+ GFP_KERNEL); +if (NULL == rt286) +return -ENOMEM ; + + rt286->regmap =devm_regmap_init (dev,NULL , i2c, &rt286_regmap); +if (IS_ERR (rt286->regmap)) { + ret =PTR_ERR (rt286->regmap); +dev_err (&i2c->dev,"Failed to allocate register map: %d\n" , + ret); +return ret; + } + + rt286->i2c = i2c; +i2c_set_clientdata (i2c, rt286);Quoted 8 lines
sound/soc/codecs/rt286.c line 1360
Quoted 6 lines
sound/soc/codecs/rt286.c new file +6
-0+ +if (pdata) + rt286->pdata = *pdata; + + ret =devm_snd_soc_register_codec (&i2c->dev, &soc_codec_dev_rt286, + rt286_dai,ARRAY_SIZE (rt286_dai));There is no such thing as
devm_snd_soc_register_codec().
Quoted 3 lines
That is Mark's suggestion.
I supposedevm_snd_soc_register_codec()will be upstreaming soon. Should I usesnd_soc_register_codec()in this patch?
Yes, otherwise the driver won't compile.
At Thu, 13 Mar 2014 09:35:40 +0100,
Quoted 37 lines · Lars-Peter Clausen
Quoted 26 lines · Bard Liao
Quoted 17 lines
Quoted 1 line
+
[...]
Quoted 15 lines
sound/soc/codecs/rt286.c new file +15
-0+static int rt286_update_bits (struct snd_soc_codec *codec,unsigned int vid, +unsigned int nid,unsigned int data, +unsigned int mask,unsigned int value) { +struct rt286_priv *rt286 =snd_soc_codec_get_drvdata (codec); +unsigned int old, new, verb; +int change, ret; + + verb =VERB_CMD ((vid |0x800 ), nid, data); +regmap_read (rt286->regmap, verb, &old); + new = (old & ~mask) | (value & mask); + change = old != new; + +if (change) { + verb =VERB_CMD (vid, nid, new); + ret =regmap_write (rt286->regmap, verb,0 );Quoted 8 lines
sound/soc/codecs/rt286.c line 145
Quoted 6 lines
sound/soc/codecs/rt286.c new file +6
-0+if (ret <0 ) { +dev_err (codec->dev, +"Failed to write private reg: %d\n" , ret); + goto err; + } + }Can't this use
regmap_update_bits()?Quoted 8 lines
rt286 use different data length for read/write protocol. Also it uses different registers for read/write the same bit.
verb =
VERB_CMD((vid | 0x800), nid, data);regmap_read(rt286->regmap, verb, &old);
...
verb =VERB_CMD(vid, nid, new);
ret =regmap_write(rt286->regmap, verb, 0);
Quoted 9 lines
You need to differentiate between logical and physical addresses. If your device uses different physical addresses for read and write then your read and write functions should do the proper translation from the logical address to the physical address. Looking at
include/sound/hda_verbs.hit seems that the GET verbs are always the same as the SET verbs but additionally set bit 11. So bit 11 is your read bit that always needs to be set when reading a register. This is nothing special to the rt286, in fact it is so common that regmap as support for this in the core. See theread_flag_maskfor theregmap_configstruct.
Note that get and set aren't always symmetrical for HD-audio. You can get up to 32bit, but set only up to 8 bit, thus some verbs are split to multiple different set verbs. Also, some verbs expect different values for get and set. And, yet more tricky part is the amplifier verbs. These aren't suitable with regmap caching, unfortunately.
Takashi
Mark Brown
maintainerQuoted 3 lines · Takashi Iwai
different values for get and set. And, yet more tricky part is the amplifier verbs. These aren't suitable with regmap caching, unfortunately.
We don't really *need* the cache, it just makes it easier to do resume and gives a bit of a performance boost.
At Thu, 13 Mar 2014 19:29:02 +0000,
Quoted 9 lines · Mark Brown
Quoted 3 lines · Takashi Iwai
different values for get and set. And, yet more tricky part is the amplifier verbs. These aren't suitable with regmap caching, unfortunately.
We don't really *need* the cache, it just makes it easier to do resume and gives a bit of a performance boost.
Well, the caching isn't just a bit. Without caching, you'd have to whip the sound hardware powered up/down at each time querying the current mixer value.
My point is only that the design of HD-audio is so plain ugly :)
Takashi
Mark Brown
maintainerQuoted 8 lines · Takashi Iwai
Quoted 2 lines · Mark Brown
We don't really *need* the cache, it just makes it easier to do resume and gives a bit of a performance boost.
Well, the caching isn't just a bit. Without caching, you'd have to whip the sound hardware powered up/down at each time querying the current mixer value.
Realistically most hardware can't actually do that - there's a couple of the idle_bias_off devices that have built in LDOs which can but it's relatively unusual to be able to remove enough power to loose register I/O (and in any case you wind up needing to cache the values per control or something in order to do system suspend).
Quoted 18 lines
-----Original Message-----
From: Mark Brown [mailto:[email protected]]
Sent: Friday, March 14, 2014 4:43 AM
To: Takashi Iwai
Cc: Lars-Peter Clausen; Bard Liao; Oder Chiou; [email protected]; [email protected]; Gustaw Lewandowski; Flove
Subject: Re: [alsa-devel] [PATCH v5] ASoC: add RT286 CODEC driverQuoted 8 lines · Takashi Iwai
Quoted 2 lines · Mark Brown
We don't really *need* the cache, it just makes it easier to do resume and gives a bit of a performance boost.
Well, the caching isn't just a bit. Without caching, you'd have to whip the sound hardware powered up/down at each time querying the current mixer value.
Quoted 5 lines
Realistically most hardware can't actually do that - there's a couple of the
idle_bias_offdevices that have built in LDOs which can but it's relatively unusual to be able to remove enough power to loose register I/O (and in any case you wind up needing to cache the values per control or something in order to do system suspend).
Mark,
What's your suggestion of the I/O part of rt286 codec driver? Can I keep using regmap without using its cache?
Or it is better not using regmap in this driver?
Thanks.
Quoted 2 lines
------Please consider the environment before printing this e-mail.
Mark Brown
maintainerQuoted 3 lines · Bard Liao
What's your suggestion of the I/O part of rt286 codec driver? Can I keep using regmap without using its cache?
Or it is better not using regmap in this driver?
I think we need to understand what the code is actually trying to do - I'm intending to go through the driver today or before the weekend but from the quick look at the I/O code I'm just really confused about what is supposed to be happening. I'll try to follow up before Monday.
Mark Brown
maintainerQuoted 6 lines · On Tue, Mar 11, 2014 at 03:11:36PM +0800, [email protected] wrote:
From: Bard Liao <[email protected]>
This patch adds the ALC286 codec driver.
ALC286 is a dual mode codec, which can run as HD-A or I2S mode. It is controlled by HD-A verb commands via I2C protocol.
Some or all of this documentation needs to end up in the code, we need to be able to understand and maintain the code going forwards and while the commit message is going to be kept it's still useful if the code can be followed. Right now that's extremely hard.
Quoted 3 lines
The following is the I/O difference between ALC286 and general I2S codecs.
1. A HD-A verb command contains three parts, NID, VID, and PID. And an I2S command contains only two parts: address and data.
It'd probably help to explain waht these are.
Quoted 9 lines
2. Not only the register address is written, but the read command also includes the entire write command.
3. rt286 uses different registers for read and write the same bits. As a result, standard regmap is difficult to be used on ALC286. We don't request a standard I/O bysnd_soc_codec_set_cache_ioanymore. Now we have ,reg_write and .reg_read functions for ALC286's I/O. And we don't use cache due to item 3 above.
Some dummy registers (address <=0xff) are defined for dapm routing. Thhe dummy registers are cache only.
Don't use dummy registers, DAPM already has virtual controls of various kinds - if you need more let's extend them. Storing data in virtual registers just makes things confusing and fragile. Some older CODEC drivers did it and they're harder to work with now than they should be.
Quoted 2 lines
Due to item 2 above, HD-A verb commands are put into the address part of regmap. When we issue HD-A verb write commands, the data part of regmap is zero.
I'm having a really hard time understanding how this follows. We do have some other devices that do things like have multi-level register addresses with registers collected in pages. By analogy here what I'd expect to see is something which combines the various pieces of addressing information into a bitfields within a single number that can be used as a register address.
Or perhaps this just isn't anything like a register map at all? Whatever is going on something that ignores the value part of the regmap interfaces doesn't seem like it is a very good fit.
Quoted 13 lines
-----Original Message-----
From: Mark Brown [mailto:[email protected]]
Sent: Saturday, March 15, 2014 3:13 AM
To: Bard Liao
Cc: [email protected]; [email protected]; Flove; Oder Chiou; Gustaw Lewandowski
Subject: Re: [PATCH v5] ASoC: add RT286 CODEC driverQuoted 4 lines · On Tue, Mar 11, 2014 at 03:11:36PM +0800, [email protected] wrote:
From: Bard Liao <[email protected]>
This patch adds the ALC286 codec driver.
Quoted 7 lines
Quoted 2 lines
ALC286 is a dual mode codec, which can run as HD-A or I2S mode. It is controlled by HD-A verb commands via I2C protocol.
Some or all of this documentation needs to end up in the code, we need to be able to understand and maintain the code going forwards and while the commit message is going to be kept it's still useful if the code can be followed. Right now that's extremely hard.
I will put these description in the code.
Quoted 6 lines
Quoted 4 lines
The following is the I/O difference between ALC286 and general I2S codecs.
1. A HD-A verb command contains three parts, NID, VID, and PID. And an I2S command contains only two parts: address and data.It'd probably help to explain waht these are.
RT286 use verb commands to control the codec.
A verb command contains three parts, NID, VID, and PID. For example, if I want to unmute headphone, I should set:
VID = 3'h (Set Amplifier Gain)
NID = 22'h (headphone)
PID = b000'h (Set left, right output unmute)
Below is the description in rt286 datasheet.
In SOC mode CODEC can ONLY be access via I2C, and CODEC works as slave mode and slave ID is 38'h. therefore HD-A verb needs to be translated via I2C protocol, no matter Read or Write HD-A command, 32 bits(8*4) command code must be issued by I2C master to compose 32 bits HD-A verb, CODEC will decode the verb, if write verb(ex: HD-A verb: 2h) was decoded, the target NID will be set to target setting, if Read verb(ex: HD-A verb: Ah) was decoded, CODEC will response the response 32 bits(8*4) response via normal I2C protocol, detail protocol please refer below.
I draw a picture below to show the I2C protocol of rt286.
I2C Write:
|1| 7 | 1 |1| 4 | 4 |1| 4 | 4 |1| 8 |1| 8 |1|1 |S|Device Address|W|A|Reserved|NID[7:4]|A|NID[3:0]|VID[11:8]|A|VID[7:0]|A|Payload[7:0]|A|P
I2C Read:
|1| 7 |1 |1| 4 | 4 |1| 4 | 4 |1| 8 |1| 8 |1|1|
|S|Device Address|W|A|Reserved|NID[7:4]|A|NID[3:0]|VID[11:8]|A|VID[7:0]|A|Payload[7:0]|A|S|->
8 |1| 8 |1| 8 |1| 8 |1 | 8 |1 |1
->Device Address|R|RD_DATA[31:24]|A|RD_DATA[23:16]|A|RD_DATA[15:8]|A|RD_DATA7:0]|NA|PQuoted 11 lines
Quoted 10 lines
2. Not only the register address is written, but the read command also includes the entire write command.
3. rt286 uses different registers for read and write the same bits. As a result, standard regmap is difficult to be used on ALC286. We don't request a standard I/O bysnd_soc_codec_set_cache_ioanymore. Now we have ,reg_write and .reg_read functions for ALC286's I/O. And we don't use cache due to item 3 above.
Some dummy registers (address <=0xff) are defined for dapm routing. Thhe dummy registers are cache only.
Quoted 4 lines
Don't use dummy registers, DAPM already has virtual controls of various kinds
- if you need more let's extend them. Storing data in virtual registers just makes things confusing and fragile. Some older CODEC drivers did it and they're harder to work with now than they should be.
Use SOC_DAPM_SINGLE_VIRT?
Quoted 11 lines
Quoted 2 lines
Due to item 2 above, HD-A verb commands are put into the address part of
regmap.
Quoted 1 line
When we issue HD-A verb write commands, the data part of regmap is zero.
I'm having a really hard time understanding how this follows. We do have some other devices that do things like have multi-level register addresses with registers collected in pages. By analogy here what I'd expect to see is something which combines the various pieces of addressing information into a bitfields within a single number that can be used as a register address.
Quoted 3 lines
Or perhaps this just isn't anything like a register map at all? Whatever is going on something that ignores the value part of the regmap interfaces doesn't seem like it is a very good fit.
I draw a general I2C protocol below.
I2C Write:
S|Device Address|W|A|Register Address|A|Data Byte High|A|Data Byte Low|A|P
I2C Read:
S|Device Address|W|A|Register Address|A|S|Device Address|R|A|Data Byte High|A|Data Byte Low|A|P
Usually, we call the data before " S|Device Address|R|" register address.
But in rt286's i2c read protocol, we have an entire write command before " S|Device Address|R|".
If I combine NID and VID to be used as a register address, and let PID as register value, it will be very difficult to define(or use) a _update_btis function.
Because I need to put all NID, VID and PID in the register address area when I issue a I2C read command. So, I put all NID, VID and PID in the register address area no matter read or write command is issued. As a result, we don't use the register value area when we use regmap_write.
That's why the register value area is always nothing(zero) when we call regmap_write.
Quoted 2 lines
------Please consider the environment before printing this e-mail.
Mark Brown
maintainerOn Tue, Mar 18, 2014 at 12:41:41PM +0000, Bard Liao wrote:
Please fix your mailer to word wrap within paragraphs, this will make your mail more legible.
Quoted 5 lines
A verb command contains three parts, NID, VID, and PID. For example, if I want to unmute headphone, I should set:
VID = 3'h (Set Amplifier Gain)
NID = 22'h (headphone)
PID = b000'h (Set left, right output unmute)
So VID+NID is an address and PID is a value?
Quoted 5 lines
Quoted 3 lines
- if you need more let's extend them. Storing data in virtual registers just makes things confusing and fragile. Some older CODEC drivers did it and they're harder to work with now than they should be.
Use
SOC_DAPM_SINGLE_VIRT?
Yes, that'd be a much better fit.
Quoted 3 lines
If I combine NID and VID to be used as a register address, and let PID as register value, it will be very difficult to define(or use) a
_update_btisfunction.
Because I need to put all NID, VID and PID in the register address area when I issue a I2C read command.
I don't see how this follows at all - all update_bits() is doing is a read/modify/write so if you can implement write and read then it comes along for free. It sounds like you need to do a cache read to get the current PID to do a read but it's not at all obvious what that has to do with update_bits() or how putting the value into the address avoids having to know the current value to do a read?
I think what you're saying above is that your read needs to have a cache read in it to function if you use normal register values?
Quoted 8 lines
-----Original Message-----
From: Mark Brown [mailto:[email protected]]
Sent: Tuesday, March 18, 2014 9:02 PM
To: Bard Liao
Cc: [email protected]; [email protected]; Flove; Oder Chiou; Gustaw Lewandowski
Subject: Re: [PATCH v5] ASoC: add RT286 CODEC driver
Quoted 4 lines
On Tue, Mar 18, 2014 at 12:41:41PM +0000, Bard Liao wrote:
Please fix your mailer to word wrap within paragraphs, this will make your mail more legible.
Now I set 80 characters to wrap the text. Hope it can work.
Quoted 8 lines
Quoted 6 lines
A verb command contains three parts, NID, VID, and PID. For example, if I want to unmute headphone, I should set:
VID = 3'h (Set Amplifier Gain)
NID = 22'h (headphone)
PID = b000'h (Set left, right output unmute)So VID+NID is an address and PID is a value?
For I2C write, yes.
But for I2C read, I think PID is also a part of address. Because PID is meaningful in the I2C read protocol. For example, if we need to get the mute/unmute status of headphone, we need to set:
VID = b'h (Get Amplifier Gain)
NID = 22'h (Headhoone)
PID = a000'h (Left output amplifier gain is requested)
And we can get response from rt286.
I am trying to implement my reg_write and reg_read to:reg_write: address: VID+NID, value: PIDreg_read: address: VID+NID+PID, value: the responding data from rt286.
Thanks.
Mark Brown
maintainerQuoted 4 lines · Bard Liao
Quoted 2 lines
Please fix your mailer to word wrap within paragraphs, this will make your mail more legible.
Now I set 80 characters to wrap the text. Hope it can work.
Doesn't seem to have taken I'm afraid :(
Quoted 10 lines
Quoted 1 line
So VID+NID is an address and PID is a value?
For I2C write, yes.
But for I2C read, I think PID is also a part of address. Because PID is meaningful in the I2C read protocol. For example, if we need to get the mute/unmute status of headphone, we need to set:
VID = b'h (Get Amplifier Gain)
NID = 22'h (Headhoone)
PID = a000'h (Left output amplifier gain is requested)
And we can get response from rt286.
Quoted 3 lines
I am trying to implement my
reg_writeandreg_readto:reg_write: address: VID+NID, value: PIDreg_read: address: VID+NID+PID, value: the responding data from rt286.
Right, that's why I said you'll need to read the cache as part of your read function. Unless the PID there is not the last value that was set in which case it probably isn't actually a register value...