Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 2 | /* |
Michael Trimarchi | 823733b | 2016-01-10 00:38:03 +0100 | [diff] [blame] | 3 | * PCM179X ASoC codec driver |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 4 | * |
| 5 | * Copyright (c) Amarula Solutions B.V. 2013 |
| 6 | * |
| 7 | * Michael Trimarchi <michael@amarulasolutions.com> |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/device.h> |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 14 | |
| 15 | #include <sound/core.h> |
| 16 | #include <sound/pcm.h> |
| 17 | #include <sound/pcm_params.h> |
| 18 | #include <sound/initval.h> |
| 19 | #include <sound/soc.h> |
| 20 | #include <sound/tlv.h> |
Sachin Kamat | 4b2fa51 | 2013-10-11 17:23:58 +0530 | [diff] [blame] | 21 | #include <linux/of.h> |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 22 | |
Michael Trimarchi | 823733b | 2016-01-10 00:38:03 +0100 | [diff] [blame] | 23 | #include "pcm179x.h" |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 24 | |
Michael Trimarchi | 823733b | 2016-01-10 00:38:03 +0100 | [diff] [blame] | 25 | #define PCM179X_DAC_VOL_LEFT 0x10 |
| 26 | #define PCM179X_DAC_VOL_RIGHT 0x11 |
| 27 | #define PCM179X_FMT_CONTROL 0x12 |
| 28 | #define PCM179X_MODE_CONTROL 0x13 |
| 29 | #define PCM179X_SOFT_MUTE PCM179X_FMT_CONTROL |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 30 | |
Michael Trimarchi | 823733b | 2016-01-10 00:38:03 +0100 | [diff] [blame] | 31 | #define PCM179X_FMT_MASK 0x70 |
| 32 | #define PCM179X_FMT_SHIFT 4 |
| 33 | #define PCM179X_MUTE_MASK 0x01 |
| 34 | #define PCM179X_MUTE_SHIFT 0 |
| 35 | #define PCM179X_ATLD_ENABLE (1 << 7) |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 36 | |
Michael Trimarchi | 823733b | 2016-01-10 00:38:03 +0100 | [diff] [blame] | 37 | static const struct reg_default pcm179x_reg_defaults[] = { |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 38 | { 0x10, 0xff }, |
| 39 | { 0x11, 0xff }, |
| 40 | { 0x12, 0x50 }, |
| 41 | { 0x13, 0x00 }, |
| 42 | { 0x14, 0x00 }, |
| 43 | { 0x15, 0x01 }, |
| 44 | { 0x16, 0x00 }, |
| 45 | { 0x17, 0x00 }, |
| 46 | }; |
| 47 | |
Michael Trimarchi | 823733b | 2016-01-10 00:38:03 +0100 | [diff] [blame] | 48 | static bool pcm179x_accessible_reg(struct device *dev, unsigned int reg) |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 49 | { |
| 50 | return reg >= 0x10 && reg <= 0x17; |
| 51 | } |
| 52 | |
Arnd Bergmann | a074ae0 | 2016-06-13 17:39:45 +0200 | [diff] [blame] | 53 | static bool pcm179x_writeable_reg(struct device *dev, unsigned int reg) |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 54 | { |
| 55 | bool accessible; |
| 56 | |
Michael Trimarchi | 823733b | 2016-01-10 00:38:03 +0100 | [diff] [blame] | 57 | accessible = pcm179x_accessible_reg(dev, reg); |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 58 | |
| 59 | return accessible && reg != 0x16 && reg != 0x17; |
| 60 | } |
| 61 | |
Michael Trimarchi | 823733b | 2016-01-10 00:38:03 +0100 | [diff] [blame] | 62 | struct pcm179x_private { |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 63 | struct regmap *regmap; |
| 64 | unsigned int format; |
| 65 | unsigned int rate; |
| 66 | }; |
| 67 | |
Michael Trimarchi | 823733b | 2016-01-10 00:38:03 +0100 | [diff] [blame] | 68 | static int pcm179x_set_dai_fmt(struct snd_soc_dai *codec_dai, |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 69 | unsigned int format) |
| 70 | { |
Kuninori Morimoto | db0a11d | 2018-01-29 04:16:56 +0000 | [diff] [blame] | 71 | struct snd_soc_component *component = codec_dai->component; |
| 72 | struct pcm179x_private *priv = snd_soc_component_get_drvdata(component); |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 73 | |
| 74 | priv->format = format; |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
Michael Trimarchi | 823733b | 2016-01-10 00:38:03 +0100 | [diff] [blame] | 79 | static int pcm179x_digital_mute(struct snd_soc_dai *dai, int mute) |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 80 | { |
Kuninori Morimoto | db0a11d | 2018-01-29 04:16:56 +0000 | [diff] [blame] | 81 | struct snd_soc_component *component = dai->component; |
| 82 | struct pcm179x_private *priv = snd_soc_component_get_drvdata(component); |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 83 | int ret; |
| 84 | |
Michael Trimarchi | 823733b | 2016-01-10 00:38:03 +0100 | [diff] [blame] | 85 | ret = regmap_update_bits(priv->regmap, PCM179X_SOFT_MUTE, |
| 86 | PCM179X_MUTE_MASK, !!mute); |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 87 | if (ret < 0) |
| 88 | return ret; |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
Michael Trimarchi | 823733b | 2016-01-10 00:38:03 +0100 | [diff] [blame] | 93 | static int pcm179x_hw_params(struct snd_pcm_substream *substream, |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 94 | struct snd_pcm_hw_params *params, |
| 95 | struct snd_soc_dai *dai) |
| 96 | { |
Kuninori Morimoto | db0a11d | 2018-01-29 04:16:56 +0000 | [diff] [blame] | 97 | struct snd_soc_component *component = dai->component; |
| 98 | struct pcm179x_private *priv = snd_soc_component_get_drvdata(component); |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 99 | int val = 0, ret; |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 100 | |
| 101 | priv->rate = params_rate(params); |
| 102 | |
| 103 | switch (priv->format & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 104 | case SND_SOC_DAIFMT_RIGHT_J: |
Mark Brown | e48c6d7 | 2014-01-24 16:07:16 +0000 | [diff] [blame] | 105 | switch (params_width(params)) { |
| 106 | case 24: |
| 107 | case 32: |
| 108 | val = 2; |
| 109 | break; |
| 110 | case 16: |
| 111 | val = 0; |
| 112 | break; |
| 113 | default: |
| 114 | return -EINVAL; |
| 115 | } |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 116 | break; |
| 117 | case SND_SOC_DAIFMT_I2S: |
Mark Brown | e48c6d7 | 2014-01-24 16:07:16 +0000 | [diff] [blame] | 118 | switch (params_width(params)) { |
| 119 | case 24: |
| 120 | case 32: |
| 121 | val = 5; |
| 122 | break; |
| 123 | case 16: |
| 124 | val = 4; |
| 125 | break; |
| 126 | default: |
| 127 | return -EINVAL; |
| 128 | } |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 129 | break; |
| 130 | default: |
Kuninori Morimoto | db0a11d | 2018-01-29 04:16:56 +0000 | [diff] [blame] | 131 | dev_err(component->dev, "Invalid DAI format\n"); |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 132 | return -EINVAL; |
| 133 | } |
| 134 | |
Michael Trimarchi | 823733b | 2016-01-10 00:38:03 +0100 | [diff] [blame] | 135 | val = val << PCM179X_FMT_SHIFT | PCM179X_ATLD_ENABLE; |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 136 | |
Michael Trimarchi | 823733b | 2016-01-10 00:38:03 +0100 | [diff] [blame] | 137 | ret = regmap_update_bits(priv->regmap, PCM179X_FMT_CONTROL, |
| 138 | PCM179X_FMT_MASK | PCM179X_ATLD_ENABLE, val); |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 139 | if (ret < 0) |
| 140 | return ret; |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
Michael Trimarchi | 823733b | 2016-01-10 00:38:03 +0100 | [diff] [blame] | 145 | static const struct snd_soc_dai_ops pcm179x_dai_ops = { |
| 146 | .set_fmt = pcm179x_set_dai_fmt, |
| 147 | .hw_params = pcm179x_hw_params, |
| 148 | .digital_mute = pcm179x_digital_mute, |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 149 | }; |
| 150 | |
Michael Trimarchi | 823733b | 2016-01-10 00:38:03 +0100 | [diff] [blame] | 151 | static const DECLARE_TLV_DB_SCALE(pcm179x_dac_tlv, -12000, 50, 1); |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 152 | |
Michael Trimarchi | 823733b | 2016-01-10 00:38:03 +0100 | [diff] [blame] | 153 | static const struct snd_kcontrol_new pcm179x_controls[] = { |
| 154 | SOC_DOUBLE_R_RANGE_TLV("DAC Playback Volume", PCM179X_DAC_VOL_LEFT, |
| 155 | PCM179X_DAC_VOL_RIGHT, 0, 0xf, 0xff, 0, |
| 156 | pcm179x_dac_tlv), |
| 157 | SOC_SINGLE("DAC Invert Output Switch", PCM179X_MODE_CONTROL, 7, 1, 0), |
| 158 | SOC_SINGLE("DAC Rolloff Filter Switch", PCM179X_MODE_CONTROL, 1, 1, 0), |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 159 | }; |
| 160 | |
Michael Trimarchi | 823733b | 2016-01-10 00:38:03 +0100 | [diff] [blame] | 161 | static const struct snd_soc_dapm_widget pcm179x_dapm_widgets[] = { |
Mark Brown | e7a5cb4 | 2013-08-13 18:30:00 +0100 | [diff] [blame] | 162 | SND_SOC_DAPM_OUTPUT("IOUTL+"), |
| 163 | SND_SOC_DAPM_OUTPUT("IOUTL-"), |
| 164 | SND_SOC_DAPM_OUTPUT("IOUTR+"), |
| 165 | SND_SOC_DAPM_OUTPUT("IOUTR-"), |
| 166 | }; |
| 167 | |
Michael Trimarchi | 823733b | 2016-01-10 00:38:03 +0100 | [diff] [blame] | 168 | static const struct snd_soc_dapm_route pcm179x_dapm_routes[] = { |
Mark Brown | e7a5cb4 | 2013-08-13 18:30:00 +0100 | [diff] [blame] | 169 | { "IOUTL+", NULL, "Playback" }, |
| 170 | { "IOUTL-", NULL, "Playback" }, |
| 171 | { "IOUTR+", NULL, "Playback" }, |
| 172 | { "IOUTR-", NULL, "Playback" }, |
| 173 | }; |
| 174 | |
Michael Trimarchi | 823733b | 2016-01-10 00:38:03 +0100 | [diff] [blame] | 175 | static struct snd_soc_dai_driver pcm179x_dai = { |
| 176 | .name = "pcm179x-hifi", |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 177 | .playback = { |
| 178 | .stream_name = "Playback", |
| 179 | .channels_min = 2, |
| 180 | .channels_max = 2, |
Jacob Siverskog | e0236f54 | 2016-01-22 13:39:55 +0100 | [diff] [blame] | 181 | .rates = SNDRV_PCM_RATE_CONTINUOUS, |
| 182 | .rate_min = 10000, |
| 183 | .rate_max = 200000, |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 184 | .formats = PCM1792A_FORMATS, }, |
Michael Trimarchi | 823733b | 2016-01-10 00:38:03 +0100 | [diff] [blame] | 185 | .ops = &pcm179x_dai_ops, |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 186 | }; |
| 187 | |
Jacob Siverskog | 1b347b6 | 2016-01-22 13:39:53 +0100 | [diff] [blame] | 188 | const struct regmap_config pcm179x_regmap_config = { |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 189 | .reg_bits = 8, |
| 190 | .val_bits = 8, |
Axel Lin | c92f66e | 2013-10-12 17:26:49 +0800 | [diff] [blame] | 191 | .max_register = 23, |
Michael Trimarchi | 823733b | 2016-01-10 00:38:03 +0100 | [diff] [blame] | 192 | .reg_defaults = pcm179x_reg_defaults, |
| 193 | .num_reg_defaults = ARRAY_SIZE(pcm179x_reg_defaults), |
| 194 | .writeable_reg = pcm179x_writeable_reg, |
| 195 | .readable_reg = pcm179x_accessible_reg, |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 196 | }; |
Jacob Siverskog | 1b347b6 | 2016-01-22 13:39:53 +0100 | [diff] [blame] | 197 | EXPORT_SYMBOL_GPL(pcm179x_regmap_config); |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 198 | |
Kuninori Morimoto | db0a11d | 2018-01-29 04:16:56 +0000 | [diff] [blame] | 199 | static const struct snd_soc_component_driver soc_component_dev_pcm179x = { |
| 200 | .controls = pcm179x_controls, |
| 201 | .num_controls = ARRAY_SIZE(pcm179x_controls), |
| 202 | .dapm_widgets = pcm179x_dapm_widgets, |
| 203 | .num_dapm_widgets = ARRAY_SIZE(pcm179x_dapm_widgets), |
| 204 | .dapm_routes = pcm179x_dapm_routes, |
| 205 | .num_dapm_routes = ARRAY_SIZE(pcm179x_dapm_routes), |
| 206 | .idle_bias_on = 1, |
| 207 | .use_pmdown_time = 1, |
| 208 | .endianness = 1, |
| 209 | .non_legacy_dai_naming = 1, |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 210 | }; |
| 211 | |
Jacob Siverskog | 1b347b6 | 2016-01-22 13:39:53 +0100 | [diff] [blame] | 212 | int pcm179x_common_init(struct device *dev, struct regmap *regmap) |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 213 | { |
Michael Trimarchi | 823733b | 2016-01-10 00:38:03 +0100 | [diff] [blame] | 214 | struct pcm179x_private *pcm179x; |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 215 | |
Jacob Siverskog | 1b347b6 | 2016-01-22 13:39:53 +0100 | [diff] [blame] | 216 | pcm179x = devm_kzalloc(dev, sizeof(struct pcm179x_private), |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 217 | GFP_KERNEL); |
Michael Trimarchi | 823733b | 2016-01-10 00:38:03 +0100 | [diff] [blame] | 218 | if (!pcm179x) |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 219 | return -ENOMEM; |
| 220 | |
Jacob Siverskog | 1b347b6 | 2016-01-22 13:39:53 +0100 | [diff] [blame] | 221 | pcm179x->regmap = regmap; |
| 222 | dev_set_drvdata(dev, pcm179x); |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 223 | |
Kuninori Morimoto | db0a11d | 2018-01-29 04:16:56 +0000 | [diff] [blame] | 224 | return devm_snd_soc_register_component(dev, |
| 225 | &soc_component_dev_pcm179x, &pcm179x_dai, 1); |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 226 | } |
Jacob Siverskog | 1b347b6 | 2016-01-22 13:39:53 +0100 | [diff] [blame] | 227 | EXPORT_SYMBOL_GPL(pcm179x_common_init); |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 228 | |
Michael Trimarchi | 823733b | 2016-01-10 00:38:03 +0100 | [diff] [blame] | 229 | MODULE_DESCRIPTION("ASoC PCM179X driver"); |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 230 | MODULE_AUTHOR("Michael Trimarchi <michael@amarulasolutions.com>"); |
| 231 | MODULE_LICENSE("GPL"); |