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