[alsa-devel] Developing a ti-pcm1863 (adc) codec driver

Oleksandr Müller sanj3k at gmail.com
Fri Aug 25 11:24:24 CEST 2017


Hello Mr. Brown,

 >  Use a route, that's the more modern way.
Would then this be correct?

static const struct snd_soc_dapm_widget pcm1863_dapm_widgets[] = {
     SND_SOC_DAPM_ADC("ADC1", NULL, SND_SOC_NOPM, 0, 0),
     SND_SOC_DAPM_INPUT("SE_IN_L"),
     SND_SOC_DAPM_INPUT("SE_IN_R"),
};

static const struct snd_soc_dapm_route pcm1863_dapm_routes[] = {
     { "ADC1", NULL, "SE_IN_L" },
     { "ADC1", NULL, "SE_IN_R" },
     { "SE_IN_L", NULL, "Capture" },
     { "SE_IN_R", NULL, "Capture" },
};

 >  I'm not sure I understand the question here.
I mean by default the adc takes VIN1 as an input and I need him to take 
VIN4. If I put my switch under sound controls do I have to trigger it, 
and if yes how? The corrected line should look like this?

static const struct snd_kcontrol_new pcm1863_controls[] = {
         SOC_DOUBLE_R("ADC MUX VIN4", PCM1863_ADC1_INPUT_SEL_L,
                     PCM1863_ADC1_INPUT_SEL_R, 4, 1, 0),
     //I need an 8 viewed from the 0 position so shifting an "1" 4 times
};

 > Please follow the kernel coding style.
Updated codec driver is attached, with a vim plugin it seems that this 
should be better now. I updated the missing break statements but I get 
the same error even now. Please take a look again.

 > This looks like you've got most of a driver here, probably the easiest
 > thing to do is submit it using the process outlined in
 > SubmittingPatches. The code looks mostly fine so this shouldn't be too
 > much of an issue.
I dont know this process. At first I would want to get it working with 
my hardware before I would begin with a more detailed development with a 
lot more functionalities.

Sanj3k
-------------- next part --------------
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/regmap.h>
#include <linux/init.h>
#include <sound/soc.h>
#include <sound/soc-dapm.h>
#include <sound/tlv.h>
#include <sound/initval.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/core.h>


#include "pcm1863.h"

struct pcm1863_priv {
	struct regmap *regmap;
	int rate;
	int fmt;
};

static const struct snd_kcontrol_new pcm1863_controls[] = {
        SOC_DOUBLE_R("ADC MUX VIN4", PCM1863_ADC1_INPUT_SEL_L, 
                    PCM1863_ADC1_INPUT_SEL_R, 4, 1, 0), 
    //I need an 8 viewed from the 0 position so shifting an "1" 4 times
};

static const struct snd_soc_dapm_widget pcm1863_dapm_widgets[] = {
        SND_SOC_DAPM_ADC("ADC1", NULL, SND_SOC_NOPM, 0, 0),
	SND_SOC_DAPM_INPUT("SE_IN_L"),
	SND_SOC_DAPM_INPUT("SE_IN_R"),
};

static const struct snd_soc_dapm_route pcm1863_dapm_routes[] = {
	{ "ADC1", NULL, "SE_IN_L" },
	{ "ADC1", NULL, "SE_IN_R" },
	{ "SE_IN_L", NULL, "Capture" },
	{ "SE_IN_R", NULL, "Capture" },	
};

static struct snd_soc_codec_driver soc_codec_dev_pcm1863 = {
	.component_driver = {
		.controls		= pcm1863_controls,
		.num_controls		= ARRAY_SIZE(pcm1863_controls),
		.dapm_widgets		= pcm1863_dapm_widgets,
		.num_dapm_widgets	= ARRAY_SIZE(pcm1863_dapm_widgets),
		.dapm_routes		= pcm1863_dapm_routes,
		.num_dapm_routes	= ARRAY_SIZE(pcm1863_dapm_routes),
	},
};

static int pcm1863_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) 
{
	struct snd_soc_codec *codec = dai->codec;
	struct pcm1863_priv *priv = snd_soc_codec_get_drvdata(codec);
		
	priv->fmt = fmt;
		
	return 0;
}

static int pcm1863_hw_params(struct snd_pcm_substream *substream,
			     struct snd_pcm_hw_params *params,
			     struct snd_soc_dai *dai)
{
	struct snd_soc_codec *codec = dai->codec;
	struct pcm1863_priv *pcm1863 = snd_soc_codec_get_drvdata(codec);
	int rxlen;
	int txlen;
	int divider;
	int ret;

	dev_dbg(codec->dev, "hw_params %u Hz, %u channels\n",
            params_rate(params),
			params_channels(params));

	switch (params_width(params)) {
	case 16:
		rxlen = PCM1863_RX_WLEN_16;
		txlen = PCM1863_TX_WLEN_16;			
		break;
	case 20:
		rxlen = PCM1863_RX_WLEN_20;
		txlen = PCM1863_TX_WLEN_20;
		break;
	case 24:
		rxlen = PCM1863_RX_WLEN_24;
		txlen = PCM1863_TX_WLEN_24;
		break;
	default:
		dev_err(codec->dev, "Bad frame size: %d\n",
                        params_width(params));
		return -EINVAL;
	}

	switch (pcm1863->fmt & SND_SOC_DAIFMT_MASTER_MASK) {
	case SND_SOC_DAIFMT_CBS_CFS: 
        //maybe not possible because it seems like the bck is only an output 
		ret = regmap_update_bits(pcm1863->regmap,
					PCM1863_CLK_SRC,
					PCM1863_MST_MODE,
					0);
		ret = regmap_update_bits(pcm1863->regmap,
					PCM1863_CLK_SRC,
					PCM1863_MST_SCK_SRC,
					1);	
		if (ret != 0) {
			dev_err(codec->dev,
                                "Failed to enable slave mode: %d\n", ret);
			return ret;
		}
		ret = regmap_update_bits(pcm1863->regmap, PCM1863_CLK_SRC,
							PCM1863_CLKDET_EN, 1);
		if (ret != 0) {
			dev_err(codec->dev,
                                "Failed to enable clock divider autoset: %d\n",
                                ret);
			return ret;
		}
        break;
	case SND_SOC_DAIFMT_CBM_CFM:	
		ret = regmap_update_bits(pcm1863->regmap,
					PCM1863_CLK_SRC,
					PCM1863_MST_MODE,
					1);	
		ret = regmap_update_bits(pcm1863->regmap,
					PCM1863_CLK_SRC,
					PCM1863_MST_SCK_SRC,
					0);	
		if (ret != 0) {
				dev_err(codec->dev,
				"Failed to enable master mode: %d\n", ret);
				return ret;
		}
		break;
	default:
		return -EINVAL;			
	}
		
	ret = regmap_update_bits(pcm1863->regmap, PCM1863_FMT_REG,
						PCM1863_RX_WLEN, rxlen);
	ret = regmap_update_bits(pcm1863->regmap, PCM1863_FMT_REG,
						PCM1863_TX_WLEN, txlen);	

	switch (pcm1863->rate) {
	case 44100:
	case 48000:
		divider = PCM1863_DIV_NUM_18;
                break;
	case 88200:
	case 96000:
		divider = PCM1863_DIV_NUM_14;
                break;
	case 192000:
		divider = PCM1863_DIV_NUM_12;	
                break;
	default:
		return -EINVAL;
	}
	ret = regmap_update_bits(pcm1863->regmap, PCM1863_SCK_DIV,
				PCM1863_DIV_NUM, divider);	
								
	return 0;
}	

static const struct snd_soc_dai_ops pcm1863_dai_ops = {
	.hw_params = pcm1863_hw_params,
	.set_fmt = pcm1863_set_fmt,
};

static struct snd_soc_dai_driver pcm1863_dai = {
	.name = "pcm1863",
	.capture = {
		.stream_name = "Capture",
		.channels_min = 2,
		.channels_max = 2,
		.rates = SNDRV_PCM_RATE_CONTINUOUS,
                .rate_min = 8000,
		.rate_max = 384000,	
		.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE
		},
		.ops = &pcm1863_dai_ops,
};

static const struct regmap_range_cfg pcm1863_range = {
	.name = "Pages", .range_min = 0x000,
	.range_max = 0xFD00,
	.selector_reg = 0,
	.selector_mask = 0xff,
	.window_start = 0, .window_len = 0x100,
};

const struct regmap_config pcm1863_regmap = {
	.reg_bits = 8,
	.val_bits = 8,

	.ranges = &pcm1863_range,
	.num_ranges = 1,

	.max_register = 0xFD00,
	.cache_type = REGCACHE_RBTREE,
};
EXPORT_SYMBOL_GPL(pcm1863_regmap);


int pcm1863_probe(struct device *dev, struct regmap *regmap)
{
	int ret;
	struct pcm1863_priv *pcm1863;

	if (IS_ERR(regmap))
		return PTR_ERR(regmap);

	pcm1863 = devm_kzalloc(dev, sizeof(*pcm1863), GFP_KERNEL);
	if (pcm1863 == NULL)
		return -ENOMEM;

	pcm1863->regmap = regmap;

	dev_set_drvdata(dev, pcm1863);

	ret = snd_soc_register_codec(dev, &soc_codec_dev_pcm1863,
		&pcm1863_dai, 1);

	return ret;
}
EXPORT_SYMBOL_GPL(pcm1863_probe);

void pcm1863_remove(struct device *dev)
{
	snd_soc_unregister_codec(dev);
}
EXPORT_SYMBOL_GPL(pcm1863_remove);

MODULE_DESCRIPTION("ASoC pcm1863 driver");
MODULE_AUTHOR("Dummy Name <test at gmail.com>");
MODULE_LICENSE("GPL v2");
-------------- next part --------------
#include <linux/init.h>
#include <linux/module.h>
#include <linux/i2c.h>

#include "pcm1863.h"

static int pcm1863_i2c_probe(struct i2c_client *i2c,
			     const struct i2c_device_id *id)
{
	struct regmap *regmap;
	struct regmap_config config = pcm1863_regmap;

	config.read_flag_mask = 0x80;
	config.write_flag_mask = 0x80;

	regmap = devm_regmap_init_i2c(i2c, &config);
	if (IS_ERR(regmap))
		return PTR_ERR(regmap);

	return pcm1863_probe(&i2c->dev, regmap);
}

static int pcm1863_i2c_remove(struct i2c_client *i2c)
{
	pcm1863_remove(&i2c->dev);
	return 0;
}

static const struct i2c_device_id pcm1863_i2c_id[] = {
	{ "pcm1863", },
	{ }
};
MODULE_DEVICE_TABLE(i2c, pcm1863_i2c_id);

/*static const struct of_device_id pcm1863_of_match[] = {
	{ .compatible = "ti,pcm1863", },
	{ }
};
MODULE_DEVICE_TABLE(of, pcm1863_of_match);*/

static struct i2c_driver pcm1863_i2c_driver = {
	.probe 		= pcm1863_i2c_probe,
	.remove 	= pcm1863_i2c_remove,
	.id_table	= pcm1863_i2c_id,
	.driver		= {
		.name	= "pcm1863",
		.of_match_table = pcm1863_of_match,
		.pm     = &pcm1863_pm_ops,
	},
};

module_i2c_driver(pcm1863_i2c_driver);

MODULE_DESCRIPTION("ASoC pcm1863 codec driver - I2C");
MODULE_AUTHOR("Dummy Name <test at gmail.com>");
MODULE_LICENSE("GPL v2");


More information about the Alsa-devel mailing list