Commit | Line | Data |
---|---|---|
e40da86a TH |
1 | /* |
2 | * cs4349.c -- CS4349 ALSA Soc Audio driver | |
3 | * | |
4 | * Copyright 2015 Cirrus Logic, Inc. | |
5 | * | |
6 | * Authors: Tim Howe <Tim.Howe@cirrus.com> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License version 2 as | |
10 | * published by the Free Software Foundation. | |
11 | */ | |
12 | ||
13 | #include <linux/module.h> | |
14 | #include <linux/moduleparam.h> | |
15 | #include <linux/kernel.h> | |
16 | #include <linux/init.h> | |
17 | #include <linux/delay.h> | |
18 | #include <linux/gpio.h> | |
19 | #include <linux/platform_device.h> | |
20 | #include <linux/pm.h> | |
21 | #include <linux/i2c.h> | |
22 | #include <linux/of_device.h> | |
23 | #include <linux/regmap.h> | |
24 | #include <linux/slab.h> | |
25 | #include <sound/core.h> | |
26 | #include <sound/pcm.h> | |
27 | #include <sound/pcm_params.h> | |
28 | #include <sound/soc.h> | |
29 | #include <sound/soc-dapm.h> | |
30 | #include <sound/initval.h> | |
31 | #include <sound/tlv.h> | |
32 | #include "cs4349.h" | |
33 | ||
34 | ||
35 | static const struct reg_default cs4349_reg_defaults[] = { | |
36 | { 2, 0x00 }, /* r02 - Mode Control */ | |
37 | { 3, 0x09 }, /* r03 - Volume, Mixing and Inversion Control */ | |
38 | { 4, 0x81 }, /* r04 - Mute Control */ | |
39 | { 5, 0x00 }, /* r05 - Channel A Volume Control */ | |
40 | { 6, 0x00 }, /* r06 - Channel B Volume Control */ | |
41 | { 7, 0xB1 }, /* r07 - Ramp and Filter Control */ | |
42 | { 8, 0x1C }, /* r08 - Misc. Control */ | |
43 | }; | |
44 | ||
45 | /* Private data for the CS4349 */ | |
46 | struct cs4349_private { | |
47 | struct regmap *regmap; | |
e40da86a TH |
48 | struct gpio_desc *reset_gpio; |
49 | unsigned int mode; | |
50 | int rate; | |
51 | }; | |
52 | ||
53 | static bool cs4349_readable_register(struct device *dev, unsigned int reg) | |
54 | { | |
55 | switch (reg) { | |
0443de7e AL |
56 | case CS4349_CHIPID ... CS4349_MISC: |
57 | return true; | |
58 | default: | |
59 | return false; | |
60 | } | |
61 | } | |
62 | ||
63 | static bool cs4349_writeable_register(struct device *dev, unsigned int reg) | |
64 | { | |
65 | switch (reg) { | |
66 | case CS4349_MODE ... CS4349_MISC: | |
e40da86a TH |
67 | return true; |
68 | default: | |
69 | return false; | |
70 | } | |
71 | } | |
72 | ||
73 | static int cs4349_set_dai_fmt(struct snd_soc_dai *codec_dai, | |
74 | unsigned int format) | |
75 | { | |
76 | struct snd_soc_codec *codec = codec_dai->codec; | |
77 | struct cs4349_private *cs4349 = snd_soc_codec_get_drvdata(codec); | |
78 | unsigned int fmt; | |
79 | ||
80 | fmt = format & SND_SOC_DAIFMT_FORMAT_MASK; | |
81 | ||
82 | switch (fmt) { | |
83 | case SND_SOC_DAIFMT_I2S: | |
84 | case SND_SOC_DAIFMT_LEFT_J: | |
85 | case SND_SOC_DAIFMT_RIGHT_J: | |
86 | cs4349->mode = format & SND_SOC_DAIFMT_FORMAT_MASK; | |
87 | break; | |
88 | default: | |
89 | return -EINVAL; | |
90 | } | |
91 | ||
92 | return 0; | |
93 | } | |
94 | ||
95 | static int cs4349_pcm_hw_params(struct snd_pcm_substream *substream, | |
96 | struct snd_pcm_hw_params *params, | |
97 | struct snd_soc_dai *dai) | |
98 | { | |
dedae86d | 99 | struct snd_soc_codec *codec = dai->codec; |
e40da86a | 100 | struct cs4349_private *cs4349 = snd_soc_codec_get_drvdata(codec); |
da304ac3 | 101 | int fmt, ret; |
e40da86a | 102 | |
e40da86a TH |
103 | cs4349->rate = params_rate(params); |
104 | ||
105 | switch (cs4349->mode) { | |
106 | case SND_SOC_DAIFMT_I2S: | |
da304ac3 | 107 | fmt = DIF_I2S; |
e40da86a TH |
108 | break; |
109 | case SND_SOC_DAIFMT_LEFT_J: | |
da304ac3 | 110 | fmt = DIF_LEFT_JST; |
e40da86a TH |
111 | break; |
112 | case SND_SOC_DAIFMT_RIGHT_J: | |
113 | switch (params_width(params)) { | |
114 | case 16: | |
115 | fmt = DIF_RGHT_JST16; | |
116 | break; | |
117 | case 24: | |
118 | fmt = DIF_RGHT_JST24; | |
119 | break; | |
120 | default: | |
121 | return -EINVAL; | |
122 | } | |
e40da86a TH |
123 | break; |
124 | default: | |
125 | return -EINVAL; | |
126 | } | |
127 | ||
da304ac3 AL |
128 | ret = snd_soc_update_bits(codec, CS4349_MODE, DIF_MASK, |
129 | MODE_FORMAT(fmt)); | |
e40da86a TH |
130 | if (ret < 0) |
131 | return ret; | |
132 | ||
133 | return 0; | |
134 | } | |
135 | ||
136 | static int cs4349_digital_mute(struct snd_soc_dai *dai, int mute) | |
137 | { | |
138 | struct snd_soc_codec *codec = dai->codec; | |
139 | int reg; | |
140 | ||
141 | reg = 0; | |
142 | if (mute) | |
143 | reg = MUTE_AB_MASK; | |
144 | ||
145 | return snd_soc_update_bits(codec, CS4349_MUTE, MUTE_AB_MASK, reg); | |
146 | } | |
147 | ||
148 | static DECLARE_TLV_DB_SCALE(dig_tlv, -12750, 50, 0); | |
149 | ||
150 | static const char * const chan_mix_texts[] = { | |
151 | "Mute", "MuteA", "MuteA SwapB", "MuteA MonoB", "SwapA MuteB", | |
152 | "BothR", "Swap", "SwapA MonoB", "MuteB", "Normal", "BothL", | |
153 | "MonoB", "MonoA MuteB", "MonoA", "MonoA SwapB", "Mono", | |
154 | /*Normal == Channel A = Left, Channel B = Right*/ | |
155 | }; | |
156 | ||
157 | static const char * const fm_texts[] = { | |
158 | "Auto", "Single", "Double", "Quad", | |
159 | }; | |
160 | ||
161 | static const char * const deemph_texts[] = { | |
162 | "None", "44.1k", "48k", "32k", | |
163 | }; | |
164 | ||
165 | static const char * const softr_zeroc_texts[] = { | |
166 | "Immediate", "Zero Cross", "Soft Ramp", "SR on ZC", | |
167 | }; | |
168 | ||
169 | static int deemph_values[] = { | |
170 | 0, 4, 8, 12, | |
171 | }; | |
172 | ||
173 | static int softr_zeroc_values[] = { | |
174 | 0, 64, 128, 192, | |
175 | }; | |
176 | ||
177 | static const struct soc_enum chan_mix_enum = | |
178 | SOC_ENUM_SINGLE(CS4349_VMI, 0, | |
179 | ARRAY_SIZE(chan_mix_texts), | |
180 | chan_mix_texts); | |
181 | ||
182 | static const struct soc_enum fm_mode_enum = | |
183 | SOC_ENUM_SINGLE(CS4349_MODE, 0, | |
184 | ARRAY_SIZE(fm_texts), | |
185 | fm_texts); | |
186 | ||
187 | static SOC_VALUE_ENUM_SINGLE_DECL(deemph_enum, CS4349_MODE, 0, DEM_MASK, | |
188 | deemph_texts, deemph_values); | |
189 | ||
190 | static SOC_VALUE_ENUM_SINGLE_DECL(softr_zeroc_enum, CS4349_RMPFLT, 0, | |
191 | SR_ZC_MASK, softr_zeroc_texts, | |
192 | softr_zeroc_values); | |
193 | ||
194 | static const struct snd_kcontrol_new cs4349_snd_controls[] = { | |
195 | SOC_DOUBLE_R_TLV("Master Playback Volume", | |
196 | CS4349_VOLA, CS4349_VOLB, 0, 0xFF, 1, dig_tlv), | |
197 | SOC_ENUM("Functional Mode", fm_mode_enum), | |
198 | SOC_ENUM("De-Emphasis Control", deemph_enum), | |
199 | SOC_ENUM("Soft Ramp Zero Cross Control", softr_zeroc_enum), | |
200 | SOC_ENUM("Channel Mixer", chan_mix_enum), | |
201 | SOC_SINGLE("VolA = VolB Switch", CS4349_VMI, 7, 1, 0), | |
202 | SOC_SINGLE("InvertA Switch", CS4349_VMI, 6, 1, 0), | |
203 | SOC_SINGLE("InvertB Switch", CS4349_VMI, 5, 1, 0), | |
204 | SOC_SINGLE("Auto-Mute Switch", CS4349_MUTE, 7, 1, 0), | |
205 | SOC_SINGLE("MUTEC A = B Switch", CS4349_MUTE, 5, 1, 0), | |
206 | SOC_SINGLE("Soft Ramp Up Switch", CS4349_RMPFLT, 5, 1, 0), | |
207 | SOC_SINGLE("Soft Ramp Down Switch", CS4349_RMPFLT, 4, 1, 0), | |
208 | SOC_SINGLE("Slow Roll Off Filter Switch", CS4349_RMPFLT, 2, 1, 0), | |
209 | SOC_SINGLE("Freeze Switch", CS4349_MISC, 5, 1, 0), | |
210 | SOC_SINGLE("Popguard Switch", CS4349_MISC, 4, 1, 0), | |
211 | }; | |
212 | ||
213 | static const struct snd_soc_dapm_widget cs4349_dapm_widgets[] = { | |
214 | SND_SOC_DAPM_DAC("HiFi DAC", NULL, SND_SOC_NOPM, 0, 0), | |
215 | ||
216 | SND_SOC_DAPM_OUTPUT("OutputA"), | |
217 | SND_SOC_DAPM_OUTPUT("OutputB"), | |
218 | }; | |
219 | ||
220 | static const struct snd_soc_dapm_route cs4349_routes[] = { | |
221 | {"DAC Playback", NULL, "OutputA"}, | |
222 | {"DAC Playback", NULL, "OutputB"}, | |
223 | ||
224 | {"OutputA", NULL, "HiFi DAC"}, | |
225 | {"OutputB", NULL, "HiFi DAC"}, | |
226 | }; | |
227 | ||
228 | #define CS4349_PCM_FORMATS (SNDRV_PCM_FMTBIT_S8 | \ | |
229 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \ | |
230 | SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \ | |
231 | SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \ | |
232 | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \ | |
233 | SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE | \ | |
234 | SNDRV_PCM_FMTBIT_S32_LE) | |
235 | ||
236 | #define CS4349_PCM_RATES SNDRV_PCM_RATE_8000_192000 | |
237 | ||
238 | static const struct snd_soc_dai_ops cs4349_dai_ops = { | |
239 | .hw_params = cs4349_pcm_hw_params, | |
240 | .set_fmt = cs4349_set_dai_fmt, | |
241 | .digital_mute = cs4349_digital_mute, | |
242 | }; | |
243 | ||
244 | static struct snd_soc_dai_driver cs4349_dai = { | |
245 | .name = "cs4349_hifi", | |
246 | .playback = { | |
247 | .stream_name = "DAC Playback", | |
248 | .channels_min = 1, | |
249 | .channels_max = 2, | |
250 | .rates = CS4349_PCM_RATES, | |
251 | .formats = CS4349_PCM_FORMATS, | |
252 | }, | |
253 | .ops = &cs4349_dai_ops, | |
254 | .symmetric_rates = 1, | |
255 | }; | |
256 | ||
257 | static struct snd_soc_codec_driver soc_codec_dev_cs4349 = { | |
258 | .controls = cs4349_snd_controls, | |
259 | .num_controls = ARRAY_SIZE(cs4349_snd_controls), | |
260 | ||
261 | .dapm_widgets = cs4349_dapm_widgets, | |
262 | .num_dapm_widgets = ARRAY_SIZE(cs4349_dapm_widgets), | |
263 | .dapm_routes = cs4349_routes, | |
264 | .num_dapm_routes = ARRAY_SIZE(cs4349_routes), | |
265 | }; | |
266 | ||
b08b3382 | 267 | static const struct regmap_config cs4349_regmap = { |
e40da86a TH |
268 | .reg_bits = 8, |
269 | .val_bits = 8, | |
270 | ||
dd9283e2 | 271 | .max_register = CS4349_MISC, |
e40da86a TH |
272 | .reg_defaults = cs4349_reg_defaults, |
273 | .num_reg_defaults = ARRAY_SIZE(cs4349_reg_defaults), | |
274 | .readable_reg = cs4349_readable_register, | |
0443de7e | 275 | .writeable_reg = cs4349_writeable_register, |
e40da86a TH |
276 | .cache_type = REGCACHE_RBTREE, |
277 | }; | |
278 | ||
279 | static int cs4349_i2c_probe(struct i2c_client *client, | |
280 | const struct i2c_device_id *id) | |
281 | { | |
282 | struct cs4349_private *cs4349; | |
44251551 | 283 | int ret; |
e40da86a TH |
284 | |
285 | cs4349 = devm_kzalloc(&client->dev, sizeof(*cs4349), GFP_KERNEL); | |
286 | if (!cs4349) | |
287 | return -ENOMEM; | |
288 | ||
289 | cs4349->regmap = devm_regmap_init_i2c(client, &cs4349_regmap); | |
290 | if (IS_ERR(cs4349->regmap)) { | |
291 | ret = PTR_ERR(cs4349->regmap); | |
292 | dev_err(&client->dev, "regmap_init() failed: %d\n", ret); | |
293 | return ret; | |
294 | } | |
295 | ||
e40da86a TH |
296 | /* Reset the Device */ |
297 | cs4349->reset_gpio = devm_gpiod_get_optional(&client->dev, | |
298 | "reset", GPIOD_OUT_LOW); | |
299 | if (IS_ERR(cs4349->reset_gpio)) | |
300 | return PTR_ERR(cs4349->reset_gpio); | |
301 | ||
302 | if (cs4349->reset_gpio) | |
303 | gpiod_set_value_cansleep(cs4349->reset_gpio, 1); | |
304 | ||
305 | i2c_set_clientdata(client, cs4349); | |
306 | ||
307 | return snd_soc_register_codec(&client->dev, &soc_codec_dev_cs4349, | |
308 | &cs4349_dai, 1); | |
309 | } | |
310 | ||
311 | static int cs4349_i2c_remove(struct i2c_client *client) | |
312 | { | |
313 | struct cs4349_private *cs4349 = i2c_get_clientdata(client); | |
314 | ||
315 | snd_soc_unregister_codec(&client->dev); | |
316 | ||
317 | /* Hold down reset */ | |
318 | if (cs4349->reset_gpio) | |
319 | gpiod_set_value_cansleep(cs4349->reset_gpio, 0); | |
320 | ||
321 | return 0; | |
322 | } | |
323 | ||
324 | #ifdef CONFIG_PM | |
325 | static int cs4349_runtime_suspend(struct device *dev) | |
326 | { | |
327 | struct cs4349_private *cs4349 = dev_get_drvdata(dev); | |
e40da86a TH |
328 | int ret; |
329 | ||
cb2510da | 330 | ret = regmap_update_bits(cs4349->regmap, CS4349_MISC, PWR_DWN, 1); |
e40da86a TH |
331 | if (ret < 0) |
332 | return ret; | |
333 | ||
334 | regcache_cache_only(cs4349->regmap, true); | |
335 | ||
336 | /* Hold down reset */ | |
337 | if (cs4349->reset_gpio) | |
338 | gpiod_set_value_cansleep(cs4349->reset_gpio, 0); | |
339 | ||
340 | return 0; | |
341 | } | |
342 | ||
343 | static int cs4349_runtime_resume(struct device *dev) | |
344 | { | |
345 | struct cs4349_private *cs4349 = dev_get_drvdata(dev); | |
e40da86a TH |
346 | int ret; |
347 | ||
cb2510da | 348 | ret = regmap_update_bits(cs4349->regmap, CS4349_MISC, PWR_DWN, 0); |
e40da86a TH |
349 | if (ret < 0) |
350 | return ret; | |
351 | ||
352 | if (cs4349->reset_gpio) | |
353 | gpiod_set_value_cansleep(cs4349->reset_gpio, 1); | |
354 | ||
355 | regcache_cache_only(cs4349->regmap, false); | |
356 | regcache_sync(cs4349->regmap); | |
357 | ||
358 | return 0; | |
359 | } | |
360 | #endif | |
361 | ||
362 | static const struct dev_pm_ops cs4349_runtime_pm = { | |
363 | SET_RUNTIME_PM_OPS(cs4349_runtime_suspend, cs4349_runtime_resume, | |
364 | NULL) | |
365 | }; | |
366 | ||
367 | static const struct of_device_id cs4349_of_match[] = { | |
368 | { .compatible = "cirrus,cs4349", }, | |
369 | {}, | |
370 | }; | |
371 | ||
372 | MODULE_DEVICE_TABLE(of, cs4349_of_match); | |
373 | ||
374 | static const struct i2c_device_id cs4349_i2c_id[] = { | |
375 | {"cs4349", 0}, | |
376 | {} | |
377 | }; | |
378 | ||
379 | MODULE_DEVICE_TABLE(i2c, cs4349_i2c_id); | |
380 | ||
381 | static struct i2c_driver cs4349_i2c_driver = { | |
382 | .driver = { | |
383 | .name = "cs4349", | |
384 | .owner = THIS_MODULE, | |
385 | .of_match_table = cs4349_of_match, | |
386 | }, | |
387 | .id_table = cs4349_i2c_id, | |
388 | .probe = cs4349_i2c_probe, | |
389 | .remove = cs4349_i2c_remove, | |
390 | }; | |
391 | ||
392 | module_i2c_driver(cs4349_i2c_driver); | |
393 | ||
394 | MODULE_AUTHOR("Tim Howe <tim.howe@cirrus.com>"); | |
395 | MODULE_DESCRIPTION("Cirrus Logic CS4349 ALSA SoC Codec Driver"); | |
396 | MODULE_LICENSE("GPL"); |