diff options
Diffstat (limited to 'sound/soc/codecs/sta32x.c')
| -rw-r--r-- | sound/soc/codecs/sta32x.c | 247 |
1 files changed, 132 insertions, 115 deletions
diff --git a/sound/soc/codecs/sta32x.c b/sound/soc/codecs/sta32x.c index 0790ae8530d9..b9f9784f5164 100644 --- a/sound/soc/codecs/sta32x.c +++ b/sound/soc/codecs/sta32x.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* * Codec driver for ST STA32x 2.1-channel high-efficiency digital audio system * @@ -9,11 +10,6 @@ * Mark Brown <broonie@opensource.wolfsonmicro.com> * Freescale Semiconductor, Inc. * Timur Tabi <timur@freescale.com> - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation; either version 2 of the License, or (at your - * option) any later version. */ #define pr_fmt(fmt) KBUILD_MODNAME ":%s:%d: " fmt, __func__, __LINE__ @@ -21,11 +17,11 @@ #include <linux/module.h> #include <linux/moduleparam.h> #include <linux/init.h> +#include <linux/clk.h> #include <linux/delay.h> #include <linux/pm.h> #include <linux/i2c.h> -#include <linux/of_device.h> -#include <linux/of_gpio.h> +#include <linux/of.h> #include <linux/regmap.h> #include <linux/regulator/consumer.h> #include <linux/gpio/consumer.h> @@ -51,12 +47,9 @@ SNDRV_PCM_RATE_192000) #define STA32X_FORMATS \ - (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \ - SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \ - SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \ - SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \ - SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE | \ - SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE) + (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S18_3LE | \ + SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_3LE | \ + SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE) /* Power-up register defaults */ static const struct reg_default sta32x_regs[] = { @@ -142,8 +135,9 @@ static const char *sta32x_supply_names[] = { /* codec private data */ struct sta32x_priv { struct regmap *regmap; + struct clk *xti_clk; struct regulator_bulk_data supplies[ARRAY_SIZE(sta32x_supply_names)]; - struct snd_soc_codec *codec; + struct snd_soc_component *component; struct sta32x_platform_data *pdata; unsigned int mclk; @@ -270,8 +264,8 @@ static int sta32x_coefficient_info(struct snd_kcontrol *kcontrol, static int sta32x_coefficient_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { - struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol); - struct sta32x_priv *sta32x = snd_soc_codec_get_drvdata(codec); + struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); + struct sta32x_priv *sta32x = snd_soc_component_get_drvdata(component); int numcoef = kcontrol->private_value >> 16; int index = kcontrol->private_value & 0xffff; unsigned int cfud, val; @@ -312,8 +306,8 @@ exit_unlock: static int sta32x_coefficient_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { - struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol); - struct sta32x_priv *sta32x = snd_soc_codec_get_drvdata(codec); + struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); + struct sta32x_priv *sta32x = snd_soc_component_get_drvdata(component); int numcoef = kcontrol->private_value >> 16; int index = kcontrol->private_value & 0xffff; unsigned int cfud; @@ -347,9 +341,9 @@ static int sta32x_coefficient_put(struct snd_kcontrol *kcontrol, return 0; } -static int sta32x_sync_coef_shadow(struct snd_soc_codec *codec) +static int sta32x_sync_coef_shadow(struct snd_soc_component *component) { - struct sta32x_priv *sta32x = snd_soc_codec_get_drvdata(codec); + struct sta32x_priv *sta32x = snd_soc_component_get_drvdata(component); unsigned int cfud; int i; @@ -375,16 +369,16 @@ static int sta32x_sync_coef_shadow(struct snd_soc_codec *codec) return 0; } -static int sta32x_cache_sync(struct snd_soc_codec *codec) +static int sta32x_cache_sync(struct snd_soc_component *component) { - struct sta32x_priv *sta32x = snd_soc_codec_get_drvdata(codec); + struct sta32x_priv *sta32x = snd_soc_component_get_drvdata(component); unsigned int mute; int rc; /* mute during register sync */ regmap_read(sta32x->regmap, STA32X_MMUTE, &mute); regmap_write(sta32x->regmap, STA32X_MMUTE, mute | STA32X_MMUTE_MMUTE); - sta32x_sync_coef_shadow(codec); + sta32x_sync_coef_shadow(component); rc = regcache_sync(sta32x->regmap); regmap_write(sta32x->regmap, STA32X_MMUTE, mute); return rc; @@ -395,17 +389,17 @@ static void sta32x_watchdog(struct work_struct *work) { struct sta32x_priv *sta32x = container_of(work, struct sta32x_priv, watchdog_work.work); - struct snd_soc_codec *codec = sta32x->codec; + struct snd_soc_component *component = sta32x->component; unsigned int confa, confa_cached; /* check if sta32x has reset itself */ - confa_cached = snd_soc_read(codec, STA32X_CONFA); + confa_cached = snd_soc_component_read(component, STA32X_CONFA); regcache_cache_bypass(sta32x->regmap, true); - confa = snd_soc_read(codec, STA32X_CONFA); + confa = snd_soc_component_read(component, STA32X_CONFA); regcache_cache_bypass(sta32x->regmap, false); if (confa != confa_cached) { regcache_mark_dirty(sta32x->regmap); - sta32x_cache_sync(codec); + sta32x_cache_sync(component); } if (!sta32x->shutdown) @@ -582,10 +576,10 @@ static int mcs_ratio_table[3][7] = { static int sta32x_set_dai_sysclk(struct snd_soc_dai *codec_dai, int clk_id, unsigned int freq, int dir) { - struct snd_soc_codec *codec = codec_dai->codec; - struct sta32x_priv *sta32x = snd_soc_codec_get_drvdata(codec); + struct snd_soc_component *component = codec_dai->component; + struct sta32x_priv *sta32x = snd_soc_component_get_drvdata(component); - dev_dbg(codec->dev, "mclk=%u\n", freq); + dev_dbg(component->dev, "mclk=%u\n", freq); sta32x->mclk = freq; return 0; @@ -602,12 +596,12 @@ static int sta32x_set_dai_sysclk(struct snd_soc_dai *codec_dai, static int sta32x_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt) { - struct snd_soc_codec *codec = codec_dai->codec; - struct sta32x_priv *sta32x = snd_soc_codec_get_drvdata(codec); + struct snd_soc_component *component = codec_dai->component; + struct sta32x_priv *sta32x = snd_soc_component_get_drvdata(component); u8 confb = 0; - switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { - case SND_SOC_DAIFMT_CBS_CFS: + switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) { + case SND_SOC_DAIFMT_CBC_CFC: break; default: return -EINVAL; @@ -651,22 +645,22 @@ static int sta32x_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 sta32x_priv *sta32x = snd_soc_codec_get_drvdata(codec); + struct snd_soc_component *component = dai->component; + struct sta32x_priv *sta32x = snd_soc_component_get_drvdata(component); int i, mcs = -EINVAL, ir = -EINVAL; unsigned int confa, confb; unsigned int rate, ratio; int ret; if (!sta32x->mclk) { - dev_err(codec->dev, + dev_err(component->dev, "sta32x->mclk is unset. Unable to determine ratio\n"); return -EIO; } rate = params_rate(params); ratio = sta32x->mclk / rate; - dev_dbg(codec->dev, "rate: %u, ratio: %u\n", rate, ratio); + dev_dbg(component->dev, "rate: %u, ratio: %u\n", rate, ratio); for (i = 0; i < ARRAY_SIZE(interpolation_ratios); i++) { if (interpolation_ratios[i].fs == rate) { @@ -676,7 +670,7 @@ static int sta32x_hw_params(struct snd_pcm_substream *substream, } if (ir < 0) { - dev_err(codec->dev, "Unsupported samplerate: %u\n", rate); + dev_err(component->dev, "Unsupported samplerate: %u\n", rate); return -EINVAL; } @@ -688,7 +682,7 @@ static int sta32x_hw_params(struct snd_pcm_substream *substream, } if (mcs < 0) { - dev_err(codec->dev, "Unresolvable ratio: %u\n", ratio); + dev_err(component->dev, "Unresolvable ratio: %u\n", ratio); return -EINVAL; } @@ -698,10 +692,10 @@ static int sta32x_hw_params(struct snd_pcm_substream *substream, switch (params_width(params)) { case 24: - dev_dbg(codec->dev, "24bit\n"); - /* fall through */ + dev_dbg(component->dev, "24bit\n"); + fallthrough; case 32: - dev_dbg(codec->dev, "24bit or 32bit\n"); + dev_dbg(component->dev, "24bit or 32bit\n"); switch (sta32x->format) { case SND_SOC_DAIFMT_I2S: confb |= 0x0; @@ -716,7 +710,7 @@ static int sta32x_hw_params(struct snd_pcm_substream *substream, break; case 20: - dev_dbg(codec->dev, "20bit\n"); + dev_dbg(component->dev, "20bit\n"); switch (sta32x->format) { case SND_SOC_DAIFMT_I2S: confb |= 0x4; @@ -731,7 +725,7 @@ static int sta32x_hw_params(struct snd_pcm_substream *substream, break; case 18: - dev_dbg(codec->dev, "18bit\n"); + dev_dbg(component->dev, "18bit\n"); switch (sta32x->format) { case SND_SOC_DAIFMT_I2S: confb |= 0x8; @@ -746,7 +740,7 @@ static int sta32x_hw_params(struct snd_pcm_substream *substream, break; case 16: - dev_dbg(codec->dev, "16bit\n"); + dev_dbg(component->dev, "16bit\n"); switch (sta32x->format) { case SND_SOC_DAIFMT_I2S: confb |= 0x0; @@ -793,20 +787,21 @@ static int sta32x_startup_sequence(struct sta32x_priv *sta32x) /** * sta32x_set_bias_level - DAPM callback - * @codec: the codec device + * @component: the component device * @level: DAPM power level * - * This is called by ALSA to put the codec into low power mode - * or to wake it up. If the codec is powered off completely + * This is called by ALSA to put the component into low power mode + * or to wake it up. If the component is powered off completely * all registers must be restored after power on. */ -static int sta32x_set_bias_level(struct snd_soc_codec *codec, +static int sta32x_set_bias_level(struct snd_soc_component *component, enum snd_soc_bias_level level) { int ret; - struct sta32x_priv *sta32x = snd_soc_codec_get_drvdata(codec); + struct sta32x_priv *sta32x = snd_soc_component_get_drvdata(component); + struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component); - dev_dbg(codec->dev, "level = %d\n", level); + dev_dbg(component->dev, "level = %d\n", level); switch (level) { case SND_SOC_BIAS_ON: break; @@ -819,17 +814,17 @@ static int sta32x_set_bias_level(struct snd_soc_codec *codec, break; case SND_SOC_BIAS_STANDBY: - if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) { + if (snd_soc_dapm_get_bias_level(dapm) == SND_SOC_BIAS_OFF) { ret = regulator_bulk_enable(ARRAY_SIZE(sta32x->supplies), sta32x->supplies); if (ret != 0) { - dev_err(codec->dev, + dev_err(component->dev, "Failed to enable supplies: %d\n", ret); return ret; } sta32x_startup_sequence(sta32x); - sta32x_cache_sync(codec); + sta32x_cache_sync(component); sta32x_watchdog_start(sta32x); } @@ -847,8 +842,7 @@ static int sta32x_set_bias_level(struct snd_soc_codec *codec, msleep(300); sta32x_watchdog_stop(sta32x); - if (sta32x->gpiod_nreset) - gpiod_set_value(sta32x->gpiod_nreset, 0); + gpiod_set_value(sta32x->gpiod_nreset, 0); regulator_bulk_disable(ARRAY_SIZE(sta32x->supplies), sta32x->supplies); @@ -875,22 +869,35 @@ static struct snd_soc_dai_driver sta32x_dai = { .ops = &sta32x_dai_ops, }; -static int sta32x_probe(struct snd_soc_codec *codec) +static int sta32x_probe(struct snd_soc_component *component) { - struct sta32x_priv *sta32x = snd_soc_codec_get_drvdata(codec); + struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component); + struct sta32x_priv *sta32x = snd_soc_component_get_drvdata(component); struct sta32x_platform_data *pdata = sta32x->pdata; int i, ret = 0, thermal = 0; + + sta32x->component = component; + + if (sta32x->xti_clk) { + ret = clk_prepare_enable(sta32x->xti_clk); + if (ret != 0) { + dev_err(component->dev, + "Failed to enable clock: %d\n", ret); + return ret; + } + } + ret = regulator_bulk_enable(ARRAY_SIZE(sta32x->supplies), sta32x->supplies); if (ret != 0) { - dev_err(codec->dev, "Failed to enable supplies: %d\n", ret); - return ret; + dev_err(component->dev, "Failed to enable supplies: %d\n", ret); + goto err_clk_disable_unprepare; } ret = sta32x_startup_sequence(sta32x); if (ret < 0) { - dev_err(codec->dev, "Failed to startup device\n"); - return ret; + dev_err(component->dev, "Failed to startup device\n"); + goto err_regulator_bulk_disable; } /* CONFA */ @@ -969,36 +976,43 @@ static int sta32x_probe(struct snd_soc_codec *codec) if (sta32x->pdata->needs_esd_watchdog) INIT_DELAYED_WORK(&sta32x->watchdog_work, sta32x_watchdog); - snd_soc_codec_force_bias_level(codec, SND_SOC_BIAS_STANDBY); + snd_soc_dapm_force_bias_level(dapm, SND_SOC_BIAS_STANDBY); /* Bias level configuration will have done an extra enable */ regulator_bulk_disable(ARRAY_SIZE(sta32x->supplies), sta32x->supplies); return 0; + +err_regulator_bulk_disable: + regulator_bulk_disable(ARRAY_SIZE(sta32x->supplies), sta32x->supplies); +err_clk_disable_unprepare: + clk_disable_unprepare(sta32x->xti_clk); + return ret; } -static int sta32x_remove(struct snd_soc_codec *codec) +static void sta32x_remove(struct snd_soc_component *component) { - struct sta32x_priv *sta32x = snd_soc_codec_get_drvdata(codec); + struct sta32x_priv *sta32x = snd_soc_component_get_drvdata(component); sta32x_watchdog_stop(sta32x); regulator_bulk_disable(ARRAY_SIZE(sta32x->supplies), sta32x->supplies); - return 0; + clk_disable_unprepare(sta32x->xti_clk); } -static const struct snd_soc_codec_driver sta32x_codec = { - .probe = sta32x_probe, - .remove = sta32x_remove, - .set_bias_level = sta32x_set_bias_level, - .suspend_bias_off = true, - .component_driver = { - .controls = sta32x_snd_controls, - .num_controls = ARRAY_SIZE(sta32x_snd_controls), - .dapm_widgets = sta32x_dapm_widgets, - .num_dapm_widgets = ARRAY_SIZE(sta32x_dapm_widgets), - .dapm_routes = sta32x_dapm_routes, - .num_dapm_routes = ARRAY_SIZE(sta32x_dapm_routes), - }, +static const struct snd_soc_component_driver sta32x_component = { + .probe = sta32x_probe, + .remove = sta32x_remove, + .set_bias_level = sta32x_set_bias_level, + .controls = sta32x_snd_controls, + .num_controls = ARRAY_SIZE(sta32x_snd_controls), + .dapm_widgets = sta32x_dapm_widgets, + .num_dapm_widgets = ARRAY_SIZE(sta32x_dapm_widgets), + .dapm_routes = sta32x_dapm_routes, + .num_dapm_routes = ARRAY_SIZE(sta32x_dapm_routes), + .suspend_bias_off = 1, + .idle_bias_on = 1, + .use_pmdown_time = 1, + .endianness = 1, }; static const struct regmap_config sta32x_regmap = { @@ -1007,7 +1021,7 @@ static const struct regmap_config sta32x_regmap = { .max_register = STA32X_FDRC2, .reg_defaults = sta32x_regs, .num_reg_defaults = ARRAY_SIZE(sta32x_regs), - .cache_type = REGCACHE_RBTREE, + .cache_type = REGCACHE_MAPLE, .wr_table = &sta32x_write_regs, .rd_table = &sta32x_read_regs, .volatile_table = &sta32x_volatile_regs, @@ -1039,33 +1053,32 @@ static int sta32x_probe_dt(struct device *dev, struct sta32x_priv *sta32x) of_property_read_u8(np, "st,ch3-output-mapping", &pdata->ch3_output_mapping); - if (of_get_property(np, "st,thermal-warning-recovery", NULL)) - pdata->thermal_warning_recovery = 1; - if (of_get_property(np, "st,thermal-warning-adjustment", NULL)) - pdata->thermal_warning_adjustment = 1; - if (of_get_property(np, "st,needs_esd_watchdog", NULL)) - pdata->needs_esd_watchdog = 1; + pdata->fault_detect_recovery = + of_property_read_bool(np, "st,fault-detect-recovery"); + pdata->thermal_warning_recovery = + of_property_read_bool(np, "st,thermal-warning-recovery"); + pdata->thermal_warning_adjustment = + of_property_read_bool(np, "st,thermal-warning-adjustment"); + pdata->needs_esd_watchdog = + of_property_read_bool(np, "st,needs_esd_watchdog"); tmp = 140; of_property_read_u16(np, "st,drop-compensation-ns", &tmp); pdata->drop_compensation_ns = clamp_t(u16, tmp, 0, 300) / 20; /* CONFE */ - if (of_get_property(np, "st,max-power-use-mpcc", NULL)) - pdata->max_power_use_mpcc = 1; - - if (of_get_property(np, "st,max-power-correction", NULL)) - pdata->max_power_correction = 1; - - if (of_get_property(np, "st,am-reduction-mode", NULL)) - pdata->am_reduction_mode = 1; - - if (of_get_property(np, "st,odd-pwm-speed-mode", NULL)) - pdata->odd_pwm_speed_mode = 1; + pdata->max_power_use_mpcc = + of_property_read_bool(np, "st,max-power-use-mpcc"); + pdata->max_power_correction = + of_property_read_bool(np, "st,max-power-correction"); + pdata->am_reduction_mode = + of_property_read_bool(np, "st,am-reduction-mode"); + pdata->odd_pwm_speed_mode = + of_property_read_bool(np, "st,odd-pwm-speed-mode"); /* CONFF */ - if (of_get_property(np, "st,invalid-input-detect-mute", NULL)) - pdata->invalid_input_detect_mute = 1; + pdata->invalid_input_detect_mute = + of_property_read_bool(np, "st,invalid-input-detect-mute"); sta32x->pdata = pdata; @@ -1073,8 +1086,7 @@ static int sta32x_probe_dt(struct device *dev, struct sta32x_priv *sta32x) } #endif -static int sta32x_i2c_probe(struct i2c_client *i2c, - const struct i2c_device_id *id) +static int sta32x_i2c_probe(struct i2c_client *i2c) { struct device *dev = &i2c->dev; struct sta32x_priv *sta32x; @@ -1096,6 +1108,17 @@ static int sta32x_i2c_probe(struct i2c_client *i2c, } #endif + /* Clock */ + sta32x->xti_clk = devm_clk_get(dev, "xti"); + if (IS_ERR(sta32x->xti_clk)) { + ret = PTR_ERR(sta32x->xti_clk); + + if (ret == -EPROBE_DEFER) + return ret; + + sta32x->xti_clk = NULL; + } + /* GPIOs */ sta32x->gpiod_nreset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW); @@ -1122,23 +1145,18 @@ static int sta32x_i2c_probe(struct i2c_client *i2c, i2c_set_clientdata(i2c, sta32x); - ret = snd_soc_register_codec(dev, &sta32x_codec, &sta32x_dai, 1); + ret = devm_snd_soc_register_component(dev, &sta32x_component, + &sta32x_dai, 1); if (ret < 0) - dev_err(dev, "Failed to register codec (%d)\n", ret); + dev_err(dev, "Failed to register component (%d)\n", ret); return ret; } -static int sta32x_i2c_remove(struct i2c_client *client) -{ - snd_soc_unregister_codec(&client->dev); - return 0; -} - static const struct i2c_device_id sta32x_i2c_id[] = { - { "sta326", 0 }, - { "sta328", 0 }, - { "sta329", 0 }, + { "sta326" }, + { "sta328" }, + { "sta329" }, { } }; MODULE_DEVICE_TABLE(i2c, sta32x_i2c_id); @@ -1148,8 +1166,7 @@ static struct i2c_driver sta32x_i2c_driver = { .name = "sta32x", .of_match_table = of_match_ptr(st32x_dt_ids), }, - .probe = sta32x_i2c_probe, - .remove = sta32x_i2c_remove, + .probe = sta32x_i2c_probe, .id_table = sta32x_i2c_id, }; |
