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