/* * CS4270 ALSA SoC (ASoC) codec driver * * Author: Timur Tabi <timur@freescale.com> * * Copyright 2007-2009 Freescale Semiconductor, Inc. This file is licensed * under the terms of the GNU General Public License version 2. This * program is licensed "as is" without any warranty of any kind, whether * express or implied. * * This is an ASoC device driver for the Cirrus Logic CS4270 codec. * * Current features/limitations: * * - Software mode is supported. Stand-alone mode is not supported. * - Only I2C is supported, not SPI * - Support for master and slave mode * - The machine driver's 'startup' function must call * cs4270_set_dai_sysclk() with the value of MCLK. * - Only I2S and left-justified modes are supported * - Power management is supported
*/
/* Power-on default values for the registers * * This array contains the power-on default values of the registers, with the * exception of the "CHIPID" register (01h). The lower four bits of that * register contain the hardware revision, so it is treated as volatile.
*/ staticconststruct reg_default cs4270_reg_defaults[] = {
{ 2, 0x00 },
{ 3, 0x30 },
{ 4, 0x00 },
{ 5, 0x60 },
{ 6, 0x20 },
{ 7, 0x00 },
{ 8, 0x00 },
};
/* Private data for the CS4270 */ struct cs4270_private { struct regmap *regmap; unsignedint mclk; /* Input frequency of the MCLK pin */ unsignedint mode; /* The mode (I2S or left-justified) */ unsignedint slave_mode; unsignedint manual_mute;
/* power domain regulators */ struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)];
/** * struct cs4270_mode_ratios - clock ratio tables * @ratio: the ratio of MCLK to the sample rate * @speed_mode: the Speed Mode bits to set in the Mode Control register for * this ratio * @mclk: the Ratio Select bits to set in the Mode Control register for this * ratio * * The data for this chart is taken from Table 5 of the CS4270 reference * manual. * * This table is used to determine how to program the Mode Control register. * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling * rates the CS4270 currently supports. * * @speed_mode is the corresponding bit pattern to be written to the * MODE bits of the Mode Control Register * * @mclk is the corresponding bit pattern to be wirten to the MCLK bits of * the Mode Control Register. * * In situations where a single ratio is represented by multiple speed * modes, we favor the slowest speed. E.g, for a ratio of 128, we pick * double-speed instead of quad-speed. However, the CS4270 errata states * that divide-By-1.5 can cause failures, so we avoid that mode where * possible. * * Errata: There is an errata for the CS4270 where divide-by-1.5 does not * work if Vd is 3.3V. If this effects you, select the * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will * never select any sample rates that require divide-by-1.5.
*/ struct cs4270_mode_ratios { unsignedint ratio;
u8 speed_mode;
u8 mclk;
};
staticbool cs4270_reg_is_volatile(struct device *dev, unsignedint reg)
{ /* Unreadable registers are considered volatile */ if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG)) returntrue;
return reg == CS4270_CHIPID;
}
/** * cs4270_set_dai_sysclk - determine the CS4270 samples rates. * @codec_dai: the codec DAI * @clk_id: the clock ID (ignored) * @freq: the MCLK input frequency * @dir: the clock direction (ignored) * * This function is used to tell the codec driver what the input MCLK * frequency is. * * The value of MCLK is used to determine which sample rates are supported * by the CS4270. The ratio of MCLK / Fs must be equal to one of nine * supported values - 64, 96, 128, 192, 256, 384, 512, 768, and 1024. * * This function calculates the nine ratios and determines which ones match * a standard sample rate. If there's a match, then it is added to the list * of supported sample rates. * * This function must be called by the machine driver's 'startup' function, * otherwise the list of supported sample rates will not be available in * time for ALSA. * * For setups with variable MCLKs, pass 0 as 'freq' argument. This will cause * theoretically possible sample rates to be enabled. Call it again with a * proper value set one the external clock is set (most probably you would do * that from a machine's driver 'hw_param' hook.
*/ staticint cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai, int clk_id, unsignedint freq, int dir)
{ struct snd_soc_component *component = codec_dai->component; struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component);
cs4270->mclk = freq; return 0;
}
/** * cs4270_set_dai_fmt - configure the codec for the selected audio format * @codec_dai: the codec DAI * @format: a SND_SOC_DAIFMT_x value indicating the data format * * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the * codec accordingly. * * Currently, this function only supports SND_SOC_DAIFMT_I2S and * SND_SOC_DAIFMT_LEFT_J. The CS4270 codec also supports right-justified * data for playback only, but ASoC currently does not support different * formats for playback vs. record.
*/ staticint cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai, unsignedint format)
{ struct snd_soc_component *component = codec_dai->component; struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component);
/* set DAI format */ switch (format & SND_SOC_DAIFMT_FORMAT_MASK) { case SND_SOC_DAIFMT_I2S: case SND_SOC_DAIFMT_LEFT_J:
cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK; break; default:
dev_err(component->dev, "invalid dai format\n"); return -EINVAL;
}
/* set master/slave audio interface */ switch (format & SND_SOC_DAIFMT_MASTER_MASK) { case SND_SOC_DAIFMT_CBC_CFC:
cs4270->slave_mode = 1; break; case SND_SOC_DAIFMT_CBP_CFP:
cs4270->slave_mode = 0; break; default: /* all other modes are unsupported by the hardware */
dev_err(component->dev, "Unknown master/slave configuration\n"); return -EINVAL;
}
return 0;
}
/** * cs4270_hw_params - program the CS4270 with the given hardware parameters. * @substream: the audio stream * @params: the hardware parameters to set * @dai: the SOC DAI (ignored) * * This function programs the hardware with the values provided. * Specifically, the sample rate and the data format. * * The .ops functions are used to provide board-specific data, like input * frequencies, to this driver. This function takes that information, * combines it with the hardware parameters provided, and programs the * hardware accordingly.
*/ staticint cs4270_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
{ struct snd_soc_component *component = dai->component; struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component); int ret; unsignedint i; unsignedint rate; unsignedint ratio; int reg;
/* Figure out which MCLK/LRCK ratio to use */
rate = params_rate(params); /* Sampling rate, in Hz */
ratio = cs4270->mclk / rate; /* MCLK/LRCK ratio */
for (i = 0; i < NUM_MCLK_RATIOS; i++) { if (cs4270_mode_ratios[i].ratio == ratio) break;
}
if (i == NUM_MCLK_RATIOS) { /* We did not find a matching ratio */
dev_err(component->dev, "could not find matching ratio\n"); return -EINVAL;
}
switch (cs4270->mode) { case SND_SOC_DAIFMT_I2S:
reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S; break; case SND_SOC_DAIFMT_LEFT_J:
reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ; break; default:
dev_err(component->dev, "unknown dai format\n"); return -EINVAL;
}
ret = snd_soc_component_write(component, CS4270_FORMAT, reg); if (ret < 0) {
dev_err(component->dev, "i2c write failed\n"); return ret;
}
return ret;
}
/** * cs4270_dai_mute - enable/disable the CS4270 external mute * @dai: the SOC DAI * @mute: 0 = disable mute, 1 = enable mute * @direction: (ignored) * * This function toggles the mute bits in the MUTE register. The CS4270's * mute capability is intended for external muting circuitry, so if the * board does not have the MUTEA or MUTEB pins connected to such circuitry, * then this function will do nothing.
*/ staticint cs4270_dai_mute(struct snd_soc_dai *dai, int mute, int direction)
{ struct snd_soc_component *component = dai->component; struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component); int reg6;
/** * cs4270_soc_put_mute - put callback for the 'Master Playback switch' * alsa control. * @kcontrol: mixer control * @ucontrol: control element information * * This function basically passes the arguments on to the generic * snd_soc_put_volsw() function and saves the mute information in * our private data structure. This is because we want to prevent * cs4270_dai_mute() neglecting the user's decision to manually * mute the codec's output. * * Returns 0 for success.
*/ staticint cs4270_soc_put_mute(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
{ struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component); int left = !ucontrol->value.integer.value[0]; int right = !ucontrol->value.integer.value[1];
/** * cs4270_probe - ASoC probe function * @component: ASoC component * * This function is called when ASoC has all the pieces it needs to * instantiate a sound driver.
*/ staticint cs4270_probe(struct snd_soc_component *component)
{ struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component); int ret;
/* Disable auto-mute. This feature appears to be buggy. In some * situations, auto-mute will not deactivate when it should, so we want * this feature disabled by default. An application (e.g. alsactl) can * re-enabled it by using the controls.
*/
ret = snd_soc_component_update_bits(component, CS4270_MUTE, CS4270_MUTE_AUTO, 0); if (ret < 0) {
dev_err(component->dev, "i2c write failed\n"); return ret;
}
/* Disable automatic volume control. The hardware enables, and it * causes volume change commands to be delayed, sometimes until after * playback has started. An application (e.g. alsactl) can * re-enabled it by using the controls.
*/
ret = snd_soc_component_update_bits(component, CS4270_TRANS,
CS4270_TRANS_SOFT | CS4270_TRANS_ZERO, 0); if (ret < 0) {
dev_err(component->dev, "i2c write failed\n"); return ret;
}
ret = regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies),
cs4270->supplies);
return ret;
}
/** * cs4270_remove - ASoC remove function * @component: ASoC component * * This function is the counterpart to cs4270_probe().
*/ staticvoid cs4270_remove(struct snd_soc_component *component)
{ struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component);
/* This suspend/resume implementation can handle both - a simple standby * where the codec remains powered, and a full suspend, where the voltage * domain the codec is connected to is teared down and/or any other hardware * reset condition is asserted. * * The codec's own power saving features are enabled in the suspend callback, * and all registers are written back to the hardware when resuming.
*/
/** * cs4270_i2c_remove - deinitialize the I2C interface of the CS4270 * @i2c_client: the I2C client object * * This function puts the chip into low power mode when the i2c device * is removed.
*/ staticvoid cs4270_i2c_remove(struct i2c_client *i2c_client)
{ struct cs4270_private *cs4270 = i2c_get_clientdata(i2c_client);
/** * cs4270_i2c_probe - initialize the I2C interface of the CS4270 * @i2c_client: the I2C client object * * This function is called whenever the I2C subsystem finds a device that * matches the device ID given via a prior call to i2c_add_driver().
*/ staticint cs4270_i2c_probe(struct i2c_client *i2c_client)
{ struct cs4270_private *cs4270; unsignedint val; int ret, i;
cs4270 = devm_kzalloc(&i2c_client->dev, sizeof(struct cs4270_private),
GFP_KERNEL); if (!cs4270) return -ENOMEM;
/* get the power supply regulators */ for (i = 0; i < ARRAY_SIZE(supply_names); i++)
cs4270->supplies[i].supply = supply_names[i];
ret = devm_regulator_bulk_get(&i2c_client->dev,
ARRAY_SIZE(cs4270->supplies),
cs4270->supplies); if (ret < 0) return ret;
if (cs4270->reset_gpio) {
dev_dbg(&i2c_client->dev, "Found reset GPIO\n");
gpiod_set_value_cansleep(cs4270->reset_gpio, 1);
}
/* Sleep 500ns before i2c communications */
ndelay(500);
cs4270->regmap = devm_regmap_init_i2c(i2c_client, &cs4270_regmap); if (IS_ERR(cs4270->regmap)) return PTR_ERR(cs4270->regmap);
/* Verify that we have a CS4270 */
ret = regmap_read(cs4270->regmap, CS4270_CHIPID, &val); if (ret < 0) {
dev_err(&i2c_client->dev, "failed to read i2c at addr %X\n",
i2c_client->addr); return ret;
} /* The top four bits of the chip ID should be 1100. */ if ((val & 0xF0) != 0xC0) {
dev_err(&i2c_client->dev, "device at addr %X is not a CS4270\n",
i2c_client->addr); return -ENODEV;
}
dev_info(&i2c_client->dev, "found device at i2c address %X\n",
i2c_client->addr);
dev_info(&i2c_client->dev, "hardware revision %X\n", val & 0xF);
i2c_set_clientdata(i2c_client, cs4270);
ret = devm_snd_soc_register_component(&i2c_client->dev,
&soc_component_device_cs4270, &cs4270_dai, 1); return ret;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.