diff options
Diffstat (limited to 'sound/soc/codecs/cs4271.c')
| -rw-r--r-- | sound/soc/codecs/cs4271.c | 224 |
1 files changed, 109 insertions, 115 deletions
diff --git a/sound/soc/codecs/cs4271.c b/sound/soc/codecs/cs4271.c index d8824773dc29..77dfc83a3c01 100644 --- a/sound/soc/codecs/cs4271.c +++ b/sound/soc/codecs/cs4271.c @@ -1,31 +1,21 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* * CS4271 ASoC codec driver * * Copyright (c) 2010 Alexander Sverdlin <subaparts@yandex.ru> * - * 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. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * * This driver support CS4271 codec being master or slave, working * in control port mode, connected either via SPI or I2C. * The data format accepted is I2S or left-justified. * DAPM support not implemented. */ +#include <linux/clk.h> #include <linux/module.h> #include <linux/slab.h> #include <linux/delay.h> -#include <linux/gpio.h> +#include <linux/gpio/consumer.h> #include <linux/of.h> -#include <linux/of_device.h> -#include <linux/of_gpio.h> #include <linux/regulator/consumer.h> #include <sound/pcm.h> #include <sound/soc.h> @@ -170,12 +160,11 @@ struct cs4271_private { /* Current sample rate for de-emphasis control */ int rate; /* GPIO driving Reset pin, if any */ - int gpio_nreset; - /* GPIO that disable serial bus, if any */ - int gpio_disable; + struct gpio_desc *reset; /* enable soft reset workaround */ bool enable_soft_reset; struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)]; + struct clk *clk; }; static const struct snd_soc_dapm_widget cs4271_dapm_widgets[] = { @@ -206,8 +195,8 @@ static const struct snd_soc_dapm_route cs4271_dapm_routes[] = { static int cs4271_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 cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec); + struct snd_soc_component *component = codec_dai->component; + struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component); cs4271->mclk = freq; return 0; @@ -216,21 +205,21 @@ static int cs4271_set_dai_sysclk(struct snd_soc_dai *codec_dai, static int cs4271_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int format) { - struct snd_soc_codec *codec = codec_dai->codec; - struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec); + struct snd_soc_component *component = codec_dai->component; + struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component); unsigned int val = 0; int ret; switch (format & SND_SOC_DAIFMT_MASTER_MASK) { - case SND_SOC_DAIFMT_CBS_CFS: - cs4271->master = 0; + case SND_SOC_DAIFMT_CBC_CFC: + cs4271->master = false; break; - case SND_SOC_DAIFMT_CBM_CFM: - cs4271->master = 1; + case SND_SOC_DAIFMT_CBP_CFP: + cs4271->master = true; val |= CS4271_MODE1_MASTER; break; default: - dev_err(codec->dev, "Invalid DAI format\n"); + dev_err(component->dev, "Invalid DAI format\n"); return -EINVAL; } @@ -250,7 +239,7 @@ static int cs4271_set_dai_fmt(struct snd_soc_dai *codec_dai, return ret; break; default: - dev_err(codec->dev, "Invalid DAI format\n"); + dev_err(component->dev, "Invalid DAI format\n"); return -EINVAL; } @@ -263,9 +252,9 @@ static int cs4271_set_dai_fmt(struct snd_soc_dai *codec_dai, static int cs4271_deemph[] = {0, 44100, 48000, 32000}; -static int cs4271_set_deemph(struct snd_soc_codec *codec) +static int cs4271_set_deemph(struct snd_soc_component *component) { - struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec); + struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component); int i, ret; int val = CS4271_DACCTL_DEM_DIS; @@ -289,8 +278,8 @@ static int cs4271_set_deemph(struct snd_soc_codec *codec) static int cs4271_get_deemph(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { - struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol); - struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec); + struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); + struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component); ucontrol->value.integer.value[0] = cs4271->deemph; return 0; @@ -299,11 +288,11 @@ static int cs4271_get_deemph(struct snd_kcontrol *kcontrol, static int cs4271_put_deemph(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { - struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol); - struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec); + struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); + struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component); cs4271->deemph = ucontrol->value.integer.value[0]; - return cs4271_set_deemph(codec); + return cs4271_set_deemph(component); } struct cs4271_clk_cfg { @@ -343,14 +332,14 @@ static struct cs4271_clk_cfg cs4271_clk_tab[] = { {0, CS4271_MODE1_MODE_4X, 256, CS4271_MODE1_DIV_2}, }; -#define CS4171_NR_RATIOS ARRAY_SIZE(cs4271_clk_tab) +#define CS4271_NR_RATIOS ARRAY_SIZE(cs4271_clk_tab) static int cs4271_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 cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec); + struct snd_soc_component *component = dai->component; + struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component); int i, ret; unsigned int ratio, val; @@ -365,9 +354,9 @@ static int cs4271_hw_params(struct snd_pcm_substream *substream, */ if ((substream->stream == SNDRV_PCM_STREAM_PLAYBACK && - !dai->capture_active) || + !snd_soc_dai_stream_active(dai, SNDRV_PCM_STREAM_CAPTURE)) || (substream->stream == SNDRV_PCM_STREAM_CAPTURE && - !dai->playback_active)) { + !snd_soc_dai_stream_active(dai, SNDRV_PCM_STREAM_PLAYBACK))) { ret = regmap_update_bits(cs4271->regmap, CS4271_MODE2, CS4271_MODE2_PDN, CS4271_MODE2_PDN); @@ -392,14 +381,14 @@ static int cs4271_hw_params(struct snd_pcm_substream *substream, val = CS4271_MODE1_MODE_4X; ratio = cs4271->mclk / cs4271->rate; - for (i = 0; i < CS4171_NR_RATIOS; i++) + for (i = 0; i < CS4271_NR_RATIOS; i++) if ((cs4271_clk_tab[i].master == cs4271->master) && (cs4271_clk_tab[i].speed_mode == val) && (cs4271_clk_tab[i].ratio == ratio)) break; - if (i == CS4171_NR_RATIOS) { - dev_err(codec->dev, "Invalid sample rate\n"); + if (i == CS4271_NR_RATIOS) { + dev_err(component->dev, "Invalid sample rate\n"); return -EINVAL; } @@ -410,13 +399,13 @@ static int cs4271_hw_params(struct snd_pcm_substream *substream, if (ret < 0) return ret; - return cs4271_set_deemph(codec); + return cs4271_set_deemph(component); } static int cs4271_mute_stream(struct snd_soc_dai *dai, int mute, int stream) { - struct snd_soc_codec *codec = dai->codec; - struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec); + struct snd_soc_component *component = dai->component; + struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component); int ret; int val_a = 0; int val_b = 0; @@ -490,28 +479,26 @@ static struct snd_soc_dai_driver cs4271_dai = { .formats = CS4271_PCM_FORMATS, }, .ops = &cs4271_dai_ops, - .symmetric_rates = 1, + .symmetric_rate = 1, }; -static int cs4271_reset(struct snd_soc_codec *codec) +static int cs4271_reset(struct snd_soc_component *component) { - struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec); + struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component); - if (gpio_is_valid(cs4271->gpio_nreset)) { - gpio_direction_output(cs4271->gpio_nreset, 0); - mdelay(1); - gpio_set_value(cs4271->gpio_nreset, 1); - mdelay(1); - } + gpiod_direction_output(cs4271->reset, 1); + mdelay(1); + gpiod_set_value(cs4271->reset, 0); + mdelay(1); return 0; } #ifdef CONFIG_PM -static int cs4271_soc_suspend(struct snd_soc_codec *codec) +static int cs4271_soc_suspend(struct snd_soc_component *component) { int ret; - struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec); + struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component); /* Set power-down bit */ ret = regmap_update_bits(cs4271->regmap, CS4271_MODE2, @@ -520,25 +507,32 @@ static int cs4271_soc_suspend(struct snd_soc_codec *codec) return ret; regcache_mark_dirty(cs4271->regmap); + clk_disable_unprepare(cs4271->clk); regulator_bulk_disable(ARRAY_SIZE(cs4271->supplies), cs4271->supplies); return 0; } -static int cs4271_soc_resume(struct snd_soc_codec *codec) +static int cs4271_soc_resume(struct snd_soc_component *component) { int ret; - struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec); + struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component); ret = regulator_bulk_enable(ARRAY_SIZE(cs4271->supplies), cs4271->supplies); if (ret < 0) { - dev_err(codec->dev, "Failed to enable regulators: %d\n", ret); + dev_err(component->dev, "Failed to enable regulators: %d\n", ret); + return ret; + } + + ret = clk_prepare_enable(cs4271->clk); + if (ret) { + dev_err(component->dev, "Failed to enable clk: %d\n", ret); return ret; } /* Do a proper reset after power up */ - cs4271_reset(codec); + cs4271_reset(component); /* Restore codec state */ ret = regcache_sync(cs4271->regmap); @@ -567,29 +561,22 @@ MODULE_DEVICE_TABLE(of, cs4271_dt_ids); EXPORT_SYMBOL_GPL(cs4271_dt_ids); #endif -static int cs4271_codec_probe(struct snd_soc_codec *codec) +static int cs4271_component_probe(struct snd_soc_component *component) { - struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec); - struct cs4271_platform_data *cs4271plat = codec->dev->platform_data; + struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component); + struct cs4271_platform_data *cs4271plat = component->dev->platform_data; int ret; - bool amutec_eq_bmutec = false; + bool amutec_eq_bmutec; -#ifdef CONFIG_OF - if (of_match_device(cs4271_dt_ids, codec->dev)) { - if (of_get_property(codec->dev->of_node, - "cirrus,amutec-eq-bmutec", NULL)) - amutec_eq_bmutec = true; - - if (of_get_property(codec->dev->of_node, - "cirrus,enable-soft-reset", NULL)) - cs4271->enable_soft_reset = true; - } -#endif + amutec_eq_bmutec = of_property_read_bool(component->dev->of_node, + "cirrus,amutec-eq-bmutec"); + cs4271->enable_soft_reset = of_property_read_bool(component->dev->of_node, + "cirrus,enable-soft-reset"); ret = regulator_bulk_enable(ARRAY_SIZE(cs4271->supplies), cs4271->supplies); if (ret < 0) { - dev_err(codec->dev, "Failed to enable regulators: %d\n", ret); + dev_err(component->dev, "Failed to enable regulators: %d\n", ret); return ret; } @@ -598,22 +585,30 @@ static int cs4271_codec_probe(struct snd_soc_codec *codec) cs4271->enable_soft_reset = cs4271plat->enable_soft_reset; } + ret = clk_prepare_enable(cs4271->clk); + if (ret) { + dev_err(component->dev, "Failed to enable clk: %d\n", ret); + goto err_disable_regulators; + } + /* Reset codec */ - cs4271_reset(codec); + cs4271_reset(component); ret = regcache_sync(cs4271->regmap); if (ret < 0) - return ret; + goto err_disable_clk; ret = regmap_update_bits(cs4271->regmap, CS4271_MODE2, CS4271_MODE2_PDN | CS4271_MODE2_CPEN, CS4271_MODE2_PDN | CS4271_MODE2_CPEN); if (ret < 0) - return ret; + goto err_disable_clk; + ret = regmap_update_bits(cs4271->regmap, CS4271_MODE2, CS4271_MODE2_PDN, 0); if (ret < 0) - return ret; + goto err_disable_clk; + /* Power-up sequence requires 85 uS */ udelay(85); @@ -623,42 +618,45 @@ static int cs4271_codec_probe(struct snd_soc_codec *codec) CS4271_MODE2_MUTECAEQUB); return 0; + +err_disable_clk: + clk_disable_unprepare(cs4271->clk); +err_disable_regulators: + regulator_bulk_disable(ARRAY_SIZE(cs4271->supplies), cs4271->supplies); + return ret; } -static int cs4271_codec_remove(struct snd_soc_codec *codec) +static void cs4271_component_remove(struct snd_soc_component *component) { - struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec); + struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component); - if (gpio_is_valid(cs4271->gpio_nreset)) - /* Set codec to the reset state */ - gpio_set_value(cs4271->gpio_nreset, 0); + /* Set codec to the reset state */ + gpiod_set_value(cs4271->reset, 1); regcache_mark_dirty(cs4271->regmap); regulator_bulk_disable(ARRAY_SIZE(cs4271->supplies), cs4271->supplies); - - return 0; + clk_disable_unprepare(cs4271->clk); }; -static struct snd_soc_codec_driver soc_codec_dev_cs4271 = { - .probe = cs4271_codec_probe, - .remove = cs4271_codec_remove, +static const struct snd_soc_component_driver soc_component_dev_cs4271 = { + .probe = cs4271_component_probe, + .remove = cs4271_component_remove, .suspend = cs4271_soc_suspend, .resume = cs4271_soc_resume, - - .component_driver = { - .controls = cs4271_snd_controls, - .num_controls = ARRAY_SIZE(cs4271_snd_controls), - .dapm_widgets = cs4271_dapm_widgets, - .num_dapm_widgets = ARRAY_SIZE(cs4271_dapm_widgets), - .dapm_routes = cs4271_dapm_routes, - .num_dapm_routes = ARRAY_SIZE(cs4271_dapm_routes), - }, + .controls = cs4271_snd_controls, + .num_controls = ARRAY_SIZE(cs4271_snd_controls), + .dapm_widgets = cs4271_dapm_widgets, + .num_dapm_widgets = ARRAY_SIZE(cs4271_dapm_widgets), + .dapm_routes = cs4271_dapm_routes, + .num_dapm_routes = ARRAY_SIZE(cs4271_dapm_routes), + .idle_bias_on = 1, + .use_pmdown_time = 1, + .endianness = 1, }; static int cs4271_common_probe(struct device *dev, struct cs4271_private **c) { - struct cs4271_platform_data *cs4271plat = dev->platform_data; struct cs4271_private *cs4271; int i, ret; @@ -666,19 +664,15 @@ static int cs4271_common_probe(struct device *dev, if (!cs4271) return -ENOMEM; - if (of_match_device(cs4271_dt_ids, dev)) - cs4271->gpio_nreset = - of_get_named_gpio(dev->of_node, "reset-gpio", 0); - - if (cs4271plat) - cs4271->gpio_nreset = cs4271plat->gpio_nreset; + cs4271->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS); + if (IS_ERR(cs4271->reset)) + return dev_err_probe(dev, PTR_ERR(cs4271->reset), + "error retrieving RESET GPIO\n"); + gpiod_set_consumer_name(cs4271->reset, "CS4271 Reset"); - if (gpio_is_valid(cs4271->gpio_nreset)) { - ret = devm_gpio_request(dev, cs4271->gpio_nreset, - "CS4271 Reset"); - if (ret < 0) - return ret; - } + cs4271->clk = devm_clk_get_optional(dev, "mclk"); + if (IS_ERR(cs4271->clk)) + return dev_err_probe(dev, PTR_ERR(cs4271->clk), "Failed to get mclk\n"); for (i = 0; i < ARRAY_SIZE(supply_names); i++) cs4271->supplies[i].supply = supply_names[i]; @@ -700,8 +694,8 @@ const struct regmap_config cs4271_regmap_config = { .reg_defaults = cs4271_reg_defaults, .num_reg_defaults = ARRAY_SIZE(cs4271_reg_defaults), - .cache_type = REGCACHE_RBTREE, - + .cache_type = REGCACHE_FLAT, + .val_bits = 8, .volatile_reg = cs4271_volatile_reg, }; EXPORT_SYMBOL_GPL(cs4271_regmap_config); @@ -721,8 +715,8 @@ int cs4271_probe(struct device *dev, struct regmap *regmap) dev_set_drvdata(dev, cs4271); cs4271->regmap = regmap; - return snd_soc_register_codec(dev, &soc_codec_dev_cs4271, &cs4271_dai, - 1); + return devm_snd_soc_register_component(dev, &soc_component_dev_cs4271, + &cs4271_dai, 1); } EXPORT_SYMBOL_GPL(cs4271_probe); |
