1 | // SPDX-License-Identifier: GPL-2.0-or-later |
2 | /* |
3 | * PCM1681 ASoC codec driver |
4 | * |
5 | * Copyright (c) StreamUnlimited GmbH 2013 |
6 | * Marek Belisko <marek.belisko@streamunlimited.com> |
7 | */ |
8 | |
9 | #include <linux/module.h> |
10 | #include <linux/slab.h> |
11 | #include <linux/delay.h> |
12 | #include <linux/gpio.h> |
13 | #include <linux/i2c.h> |
14 | #include <linux/regmap.h> |
15 | #include <linux/of.h> |
16 | #include <sound/pcm.h> |
17 | #include <sound/pcm_params.h> |
18 | #include <sound/soc.h> |
19 | #include <sound/tlv.h> |
20 | |
21 | #define PCM1681_PCM_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \ |
22 | SNDRV_PCM_FMTBIT_S24_LE) |
23 | |
24 | #define PCM1681_PCM_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 | \ |
25 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \ |
26 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | \ |
27 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000) |
28 | |
29 | #define PCM1681_SOFT_MUTE_ALL 0xff |
30 | #define PCM1681_DEEMPH_RATE_MASK 0x18 |
31 | #define PCM1681_DEEMPH_MASK 0x01 |
32 | |
33 | #define PCM1681_ATT_CONTROL(X) (X <= 6 ? X : X + 9) /* Attenuation level */ |
34 | #define PCM1681_SOFT_MUTE 0x07 /* Soft mute control register */ |
35 | #define PCM1681_DAC_CONTROL 0x08 /* DAC operation control */ |
36 | #define PCM1681_FMT_CONTROL 0x09 /* Audio interface data format */ |
37 | #define PCM1681_DEEMPH_CONTROL 0x0a /* De-emphasis control */ |
38 | #define PCM1681_ZERO_DETECT_STATUS 0x0e /* Zero detect status reg */ |
39 | |
40 | static const struct reg_default pcm1681_reg_defaults[] = { |
41 | { 0x01, 0xff }, |
42 | { 0x02, 0xff }, |
43 | { 0x03, 0xff }, |
44 | { 0x04, 0xff }, |
45 | { 0x05, 0xff }, |
46 | { 0x06, 0xff }, |
47 | { 0x07, 0x00 }, |
48 | { 0x08, 0x00 }, |
49 | { 0x09, 0x06 }, |
50 | { 0x0A, 0x00 }, |
51 | { 0x0B, 0xff }, |
52 | { 0x0C, 0x0f }, |
53 | { 0x0D, 0x00 }, |
54 | { 0x10, 0xff }, |
55 | { 0x11, 0xff }, |
56 | { 0x12, 0x00 }, |
57 | { 0x13, 0x00 }, |
58 | }; |
59 | |
60 | static bool pcm1681_accessible_reg(struct device *dev, unsigned int reg) |
61 | { |
62 | return !((reg == 0x00) || (reg == 0x0f)); |
63 | } |
64 | |
65 | static bool pcm1681_writeable_reg(struct device *dev, unsigned int reg) |
66 | { |
67 | return pcm1681_accessible_reg(dev, reg) && |
68 | (reg != PCM1681_ZERO_DETECT_STATUS); |
69 | } |
70 | |
71 | struct pcm1681_private { |
72 | struct regmap *regmap; |
73 | unsigned int format; |
74 | /* Current deemphasis status */ |
75 | unsigned int deemph; |
76 | /* Current rate for deemphasis control */ |
77 | unsigned int rate; |
78 | }; |
79 | |
80 | static const int pcm1681_deemph[] = { 44100, 48000, 32000 }; |
81 | |
82 | static int pcm1681_set_deemph(struct snd_soc_component *component) |
83 | { |
84 | struct pcm1681_private *priv = snd_soc_component_get_drvdata(c: component); |
85 | int i, val = -1, enable = 0; |
86 | |
87 | if (priv->deemph) { |
88 | for (i = 0; i < ARRAY_SIZE(pcm1681_deemph); i++) { |
89 | if (pcm1681_deemph[i] == priv->rate) { |
90 | val = i; |
91 | break; |
92 | } |
93 | } |
94 | } |
95 | |
96 | if (val != -1) { |
97 | regmap_update_bits(map: priv->regmap, PCM1681_DEEMPH_CONTROL, |
98 | PCM1681_DEEMPH_RATE_MASK, val: val << 3); |
99 | enable = 1; |
100 | } else { |
101 | enable = 0; |
102 | } |
103 | |
104 | /* enable/disable deemphasis functionality */ |
105 | return regmap_update_bits(map: priv->regmap, PCM1681_DEEMPH_CONTROL, |
106 | PCM1681_DEEMPH_MASK, val: enable); |
107 | } |
108 | |
109 | static int pcm1681_get_deemph(struct snd_kcontrol *kcontrol, |
110 | struct snd_ctl_elem_value *ucontrol) |
111 | { |
112 | struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); |
113 | struct pcm1681_private *priv = snd_soc_component_get_drvdata(c: component); |
114 | |
115 | ucontrol->value.integer.value[0] = priv->deemph; |
116 | |
117 | return 0; |
118 | } |
119 | |
120 | static int pcm1681_put_deemph(struct snd_kcontrol *kcontrol, |
121 | struct snd_ctl_elem_value *ucontrol) |
122 | { |
123 | struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); |
124 | struct pcm1681_private *priv = snd_soc_component_get_drvdata(c: component); |
125 | |
126 | priv->deemph = ucontrol->value.integer.value[0]; |
127 | |
128 | return pcm1681_set_deemph(component); |
129 | } |
130 | |
131 | static int pcm1681_set_dai_fmt(struct snd_soc_dai *codec_dai, |
132 | unsigned int format) |
133 | { |
134 | struct snd_soc_component *component = codec_dai->component; |
135 | struct pcm1681_private *priv = snd_soc_component_get_drvdata(c: component); |
136 | |
137 | /* The PCM1681 can only be consumer to all clocks */ |
138 | if ((format & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) != SND_SOC_DAIFMT_CBC_CFC) { |
139 | dev_err(component->dev, "Invalid clocking mode\n" ); |
140 | return -EINVAL; |
141 | } |
142 | |
143 | priv->format = format; |
144 | |
145 | return 0; |
146 | } |
147 | |
148 | static int pcm1681_mute(struct snd_soc_dai *dai, int mute, int direction) |
149 | { |
150 | struct snd_soc_component *component = dai->component; |
151 | struct pcm1681_private *priv = snd_soc_component_get_drvdata(c: component); |
152 | int val; |
153 | |
154 | if (mute) |
155 | val = PCM1681_SOFT_MUTE_ALL; |
156 | else |
157 | val = 0; |
158 | |
159 | return regmap_write(map: priv->regmap, PCM1681_SOFT_MUTE, val); |
160 | } |
161 | |
162 | static int pcm1681_hw_params(struct snd_pcm_substream *substream, |
163 | struct snd_pcm_hw_params *params, |
164 | struct snd_soc_dai *dai) |
165 | { |
166 | struct snd_soc_component *component = dai->component; |
167 | struct pcm1681_private *priv = snd_soc_component_get_drvdata(c: component); |
168 | int val = 0, ret; |
169 | |
170 | priv->rate = params_rate(p: params); |
171 | |
172 | switch (priv->format & SND_SOC_DAIFMT_FORMAT_MASK) { |
173 | case SND_SOC_DAIFMT_RIGHT_J: |
174 | switch (params_width(p: params)) { |
175 | case 24: |
176 | val = 0; |
177 | break; |
178 | case 16: |
179 | val = 3; |
180 | break; |
181 | default: |
182 | return -EINVAL; |
183 | } |
184 | break; |
185 | case SND_SOC_DAIFMT_I2S: |
186 | val = 0x04; |
187 | break; |
188 | case SND_SOC_DAIFMT_LEFT_J: |
189 | val = 0x05; |
190 | break; |
191 | default: |
192 | dev_err(component->dev, "Invalid DAI format\n" ); |
193 | return -EINVAL; |
194 | } |
195 | |
196 | ret = regmap_update_bits(map: priv->regmap, PCM1681_FMT_CONTROL, mask: 0x0f, val); |
197 | if (ret < 0) |
198 | return ret; |
199 | |
200 | return pcm1681_set_deemph(component); |
201 | } |
202 | |
203 | static const struct snd_soc_dai_ops pcm1681_dai_ops = { |
204 | .set_fmt = pcm1681_set_dai_fmt, |
205 | .hw_params = pcm1681_hw_params, |
206 | .mute_stream = pcm1681_mute, |
207 | .no_capture_mute = 1, |
208 | }; |
209 | |
210 | static const struct snd_soc_dapm_widget pcm1681_dapm_widgets[] = { |
211 | SND_SOC_DAPM_OUTPUT("VOUT1" ), |
212 | SND_SOC_DAPM_OUTPUT("VOUT2" ), |
213 | SND_SOC_DAPM_OUTPUT("VOUT3" ), |
214 | SND_SOC_DAPM_OUTPUT("VOUT4" ), |
215 | SND_SOC_DAPM_OUTPUT("VOUT5" ), |
216 | SND_SOC_DAPM_OUTPUT("VOUT6" ), |
217 | SND_SOC_DAPM_OUTPUT("VOUT7" ), |
218 | SND_SOC_DAPM_OUTPUT("VOUT8" ), |
219 | }; |
220 | |
221 | static const struct snd_soc_dapm_route pcm1681_dapm_routes[] = { |
222 | { "VOUT1" , NULL, "Playback" }, |
223 | { "VOUT2" , NULL, "Playback" }, |
224 | { "VOUT3" , NULL, "Playback" }, |
225 | { "VOUT4" , NULL, "Playback" }, |
226 | { "VOUT5" , NULL, "Playback" }, |
227 | { "VOUT6" , NULL, "Playback" }, |
228 | { "VOUT7" , NULL, "Playback" }, |
229 | { "VOUT8" , NULL, "Playback" }, |
230 | }; |
231 | |
232 | static const DECLARE_TLV_DB_SCALE(pcm1681_dac_tlv, -6350, 50, 1); |
233 | |
234 | static const struct snd_kcontrol_new pcm1681_controls[] = { |
235 | SOC_DOUBLE_R_TLV("Channel 1/2 Playback Volume" , |
236 | PCM1681_ATT_CONTROL(1), PCM1681_ATT_CONTROL(2), 0, |
237 | 0x7f, 0, pcm1681_dac_tlv), |
238 | SOC_DOUBLE_R_TLV("Channel 3/4 Playback Volume" , |
239 | PCM1681_ATT_CONTROL(3), PCM1681_ATT_CONTROL(4), 0, |
240 | 0x7f, 0, pcm1681_dac_tlv), |
241 | SOC_DOUBLE_R_TLV("Channel 5/6 Playback Volume" , |
242 | PCM1681_ATT_CONTROL(5), PCM1681_ATT_CONTROL(6), 0, |
243 | 0x7f, 0, pcm1681_dac_tlv), |
244 | SOC_DOUBLE_R_TLV("Channel 7/8 Playback Volume" , |
245 | PCM1681_ATT_CONTROL(7), PCM1681_ATT_CONTROL(8), 0, |
246 | 0x7f, 0, pcm1681_dac_tlv), |
247 | SOC_SINGLE_BOOL_EXT("De-emphasis Switch" , 0, |
248 | pcm1681_get_deemph, pcm1681_put_deemph), |
249 | }; |
250 | |
251 | static struct snd_soc_dai_driver pcm1681_dai = { |
252 | .name = "pcm1681-hifi" , |
253 | .playback = { |
254 | .stream_name = "Playback" , |
255 | .channels_min = 2, |
256 | .channels_max = 8, |
257 | .rates = PCM1681_PCM_RATES, |
258 | .formats = PCM1681_PCM_FORMATS, |
259 | }, |
260 | .ops = &pcm1681_dai_ops, |
261 | }; |
262 | |
263 | #ifdef CONFIG_OF |
264 | static const struct of_device_id pcm1681_dt_ids[] = { |
265 | { .compatible = "ti,pcm1681" , }, |
266 | { } |
267 | }; |
268 | MODULE_DEVICE_TABLE(of, pcm1681_dt_ids); |
269 | #endif |
270 | |
271 | static const struct regmap_config pcm1681_regmap = { |
272 | .reg_bits = 8, |
273 | .val_bits = 8, |
274 | .max_register = 0x13, |
275 | .reg_defaults = pcm1681_reg_defaults, |
276 | .num_reg_defaults = ARRAY_SIZE(pcm1681_reg_defaults), |
277 | .writeable_reg = pcm1681_writeable_reg, |
278 | .readable_reg = pcm1681_accessible_reg, |
279 | }; |
280 | |
281 | static const struct snd_soc_component_driver soc_component_dev_pcm1681 = { |
282 | .controls = pcm1681_controls, |
283 | .num_controls = ARRAY_SIZE(pcm1681_controls), |
284 | .dapm_widgets = pcm1681_dapm_widgets, |
285 | .num_dapm_widgets = ARRAY_SIZE(pcm1681_dapm_widgets), |
286 | .dapm_routes = pcm1681_dapm_routes, |
287 | .num_dapm_routes = ARRAY_SIZE(pcm1681_dapm_routes), |
288 | .idle_bias_on = 1, |
289 | .use_pmdown_time = 1, |
290 | .endianness = 1, |
291 | }; |
292 | |
293 | static const struct i2c_device_id pcm1681_i2c_id[] = { |
294 | {"pcm1681" , 0}, |
295 | {} |
296 | }; |
297 | MODULE_DEVICE_TABLE(i2c, pcm1681_i2c_id); |
298 | |
299 | static int pcm1681_i2c_probe(struct i2c_client *client) |
300 | { |
301 | int ret; |
302 | struct pcm1681_private *priv; |
303 | |
304 | priv = devm_kzalloc(dev: &client->dev, size: sizeof(*priv), GFP_KERNEL); |
305 | if (!priv) |
306 | return -ENOMEM; |
307 | |
308 | priv->regmap = devm_regmap_init_i2c(client, &pcm1681_regmap); |
309 | if (IS_ERR(ptr: priv->regmap)) { |
310 | ret = PTR_ERR(ptr: priv->regmap); |
311 | dev_err(&client->dev, "Failed to create regmap: %d\n" , ret); |
312 | return ret; |
313 | } |
314 | |
315 | i2c_set_clientdata(client, data: priv); |
316 | |
317 | return devm_snd_soc_register_component(dev: &client->dev, |
318 | component_driver: &soc_component_dev_pcm1681, |
319 | dai_drv: &pcm1681_dai, num_dai: 1); |
320 | } |
321 | |
322 | static struct i2c_driver pcm1681_i2c_driver = { |
323 | .driver = { |
324 | .name = "pcm1681" , |
325 | .of_match_table = of_match_ptr(pcm1681_dt_ids), |
326 | }, |
327 | .id_table = pcm1681_i2c_id, |
328 | .probe = pcm1681_i2c_probe, |
329 | }; |
330 | |
331 | module_i2c_driver(pcm1681_i2c_driver); |
332 | |
333 | MODULE_DESCRIPTION("Texas Instruments PCM1681 ALSA SoC Codec Driver" ); |
334 | MODULE_AUTHOR("Marek Belisko <marek.belisko@streamunlimited.com>" ); |
335 | MODULE_LICENSE("GPL" ); |
336 | |