xref: /openbmc/linux/sound/soc/codecs/max9850.c (revision aa1d19f1)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * max9850.c  --  codec driver for max9850
4  *
5  * Copyright (C) 2011 taskit GmbH
6  *
7  * Author: Christian Glindkamp <christian.glindkamp@taskit.de>
8  *
9  * Initial development of this code was funded by
10  * MICRONIC Computer Systeme GmbH, http://www.mcsberlin.de/
11  */
12 
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/i2c.h>
16 #include <linux/regmap.h>
17 #include <linux/slab.h>
18 #include <sound/pcm.h>
19 #include <sound/pcm_params.h>
20 #include <sound/soc.h>
21 #include <sound/tlv.h>
22 
23 #include "max9850.h"
24 
25 struct max9850_priv {
26 	struct regmap *regmap;
27 	unsigned int sysclk;
28 };
29 
30 /* max9850 register cache */
31 static const struct reg_default max9850_reg[] = {
32 	{  2, 0x0c },
33 	{  3, 0x00 },
34 	{  4, 0x00 },
35 	{  5, 0x00 },
36 	{  6, 0x00 },
37 	{  7, 0x00 },
38 	{  8, 0x00 },
39 	{  9, 0x00 },
40 	{ 10, 0x00 },
41 };
42 
43 /* these registers are not used at the moment but provided for the sake of
44  * completeness */
45 static bool max9850_volatile_register(struct device *dev, unsigned int reg)
46 {
47 	switch (reg) {
48 	case MAX9850_STATUSA:
49 	case MAX9850_STATUSB:
50 		return true;
51 	default:
52 		return false;
53 	}
54 }
55 
56 static const struct regmap_config max9850_regmap = {
57 	.reg_bits = 8,
58 	.val_bits = 8,
59 
60 	.max_register = MAX9850_DIGITAL_AUDIO,
61 	.volatile_reg = max9850_volatile_register,
62 	.cache_type = REGCACHE_RBTREE,
63 };
64 
65 static const DECLARE_TLV_DB_RANGE(max9850_tlv,
66 	0x18, 0x1f, TLV_DB_SCALE_ITEM(-7450, 400, 0),
67 	0x20, 0x33, TLV_DB_SCALE_ITEM(-4150, 200, 0),
68 	0x34, 0x37, TLV_DB_SCALE_ITEM(-150, 100, 0),
69 	0x38, 0x3f, TLV_DB_SCALE_ITEM(250, 50, 0)
70 );
71 
72 static const struct snd_kcontrol_new max9850_controls[] = {
73 SOC_SINGLE_TLV("Headphone Volume", MAX9850_VOLUME, 0, 0x3f, 1, max9850_tlv),
74 SOC_SINGLE("Headphone Switch", MAX9850_VOLUME, 7, 1, 1),
75 SOC_SINGLE("Mono Switch", MAX9850_GENERAL_PURPOSE, 2, 1, 0),
76 };
77 
78 static const struct snd_kcontrol_new max9850_mixer_controls[] = {
79 	SOC_DAPM_SINGLE("Line In Switch", MAX9850_ENABLE, 1, 1, 0),
80 };
81 
82 static const struct snd_soc_dapm_widget max9850_dapm_widgets[] = {
83 SND_SOC_DAPM_SUPPLY("Charge Pump 1", MAX9850_ENABLE, 4, 0, NULL, 0),
84 SND_SOC_DAPM_SUPPLY("Charge Pump 2", MAX9850_ENABLE, 5, 0, NULL, 0),
85 SND_SOC_DAPM_SUPPLY("MCLK", MAX9850_ENABLE, 6, 0, NULL, 0),
86 SND_SOC_DAPM_SUPPLY("SHDN", MAX9850_ENABLE, 7, 0, NULL, 0),
87 SND_SOC_DAPM_MIXER_NAMED_CTL("Output Mixer", MAX9850_ENABLE, 2, 0,
88 		&max9850_mixer_controls[0],
89 		ARRAY_SIZE(max9850_mixer_controls)),
90 SND_SOC_DAPM_PGA("Headphone Output", MAX9850_ENABLE, 3, 0, NULL, 0),
91 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", MAX9850_ENABLE, 0, 0),
92 SND_SOC_DAPM_OUTPUT("OUTL"),
93 SND_SOC_DAPM_OUTPUT("HPL"),
94 SND_SOC_DAPM_OUTPUT("OUTR"),
95 SND_SOC_DAPM_OUTPUT("HPR"),
96 SND_SOC_DAPM_MIXER("Line Input", SND_SOC_NOPM, 0, 0, NULL, 0),
97 SND_SOC_DAPM_INPUT("INL"),
98 SND_SOC_DAPM_INPUT("INR"),
99 };
100 
101 static const struct snd_soc_dapm_route max9850_dapm_routes[] = {
102 	/* output mixer */
103 	{"Output Mixer", NULL, "DAC"},
104 	{"Output Mixer", "Line In Switch", "Line Input"},
105 
106 	/* outputs */
107 	{"Headphone Output", NULL, "Output Mixer"},
108 	{"HPL", NULL, "Headphone Output"},
109 	{"HPR", NULL, "Headphone Output"},
110 	{"OUTL", NULL, "Output Mixer"},
111 	{"OUTR", NULL, "Output Mixer"},
112 
113 	/* inputs */
114 	{"Line Input", NULL, "INL"},
115 	{"Line Input", NULL, "INR"},
116 
117 	/* supplies */
118 	{"Output Mixer", NULL, "Charge Pump 1"},
119 	{"Output Mixer", NULL, "Charge Pump 2"},
120 	{"Output Mixer", NULL, "SHDN"},
121 	{"DAC", NULL, "MCLK"},
122 };
123 
124 static int max9850_hw_params(struct snd_pcm_substream *substream,
125 			     struct snd_pcm_hw_params *params,
126 			     struct snd_soc_dai *dai)
127 {
128 	struct snd_soc_component *component = dai->component;
129 	struct max9850_priv *max9850 = snd_soc_component_get_drvdata(component);
130 	u64 lrclk_div;
131 	u8 sf, da;
132 
133 	if (!max9850->sysclk)
134 		return -EINVAL;
135 
136 	/* lrclk_div = 2^22 * rate / iclk with iclk = mclk / sf */
137 	sf = (snd_soc_component_read32(component, MAX9850_CLOCK) >> 2) + 1;
138 	lrclk_div = (1 << 22);
139 	lrclk_div *= params_rate(params);
140 	lrclk_div *= sf;
141 	do_div(lrclk_div, max9850->sysclk);
142 
143 	snd_soc_component_write(component, MAX9850_LRCLK_MSB, (lrclk_div >> 8) & 0x7f);
144 	snd_soc_component_write(component, MAX9850_LRCLK_LSB, lrclk_div & 0xff);
145 
146 	switch (params_width(params)) {
147 	case 16:
148 		da = 0;
149 		break;
150 	case 20:
151 		da = 0x2;
152 		break;
153 	case 24:
154 		da = 0x3;
155 		break;
156 	default:
157 		return -EINVAL;
158 	}
159 	snd_soc_component_update_bits(component, MAX9850_DIGITAL_AUDIO, 0x3, da);
160 
161 	return 0;
162 }
163 
164 static int max9850_set_dai_sysclk(struct snd_soc_dai *codec_dai,
165 		int clk_id, unsigned int freq, int dir)
166 {
167 	struct snd_soc_component *component = codec_dai->component;
168 	struct max9850_priv *max9850 = snd_soc_component_get_drvdata(component);
169 
170 	/* calculate mclk -> iclk divider */
171 	if (freq <= 13000000)
172 		snd_soc_component_write(component, MAX9850_CLOCK, 0x0);
173 	else if (freq <= 26000000)
174 		snd_soc_component_write(component, MAX9850_CLOCK, 0x4);
175 	else if (freq <= 40000000)
176 		snd_soc_component_write(component, MAX9850_CLOCK, 0x8);
177 	else
178 		return -EINVAL;
179 
180 	max9850->sysclk = freq;
181 	return 0;
182 }
183 
184 static int max9850_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
185 {
186 	struct snd_soc_component *component = codec_dai->component;
187 	u8 da = 0;
188 
189 	/* set master/slave audio interface */
190 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
191 	case SND_SOC_DAIFMT_CBM_CFM:
192 		da |= MAX9850_MASTER;
193 		break;
194 	case SND_SOC_DAIFMT_CBS_CFS:
195 		break;
196 	default:
197 		return -EINVAL;
198 	}
199 
200 	/* interface format */
201 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
202 	case SND_SOC_DAIFMT_I2S:
203 		da |= MAX9850_DLY;
204 		break;
205 	case SND_SOC_DAIFMT_RIGHT_J:
206 		da |= MAX9850_RTJ;
207 		break;
208 	case SND_SOC_DAIFMT_LEFT_J:
209 		break;
210 	default:
211 		return -EINVAL;
212 	}
213 
214 	/* clock inversion */
215 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
216 	case SND_SOC_DAIFMT_NB_NF:
217 		break;
218 	case SND_SOC_DAIFMT_IB_IF:
219 		da |= MAX9850_BCINV | MAX9850_INV;
220 		break;
221 	case SND_SOC_DAIFMT_IB_NF:
222 		da |= MAX9850_BCINV;
223 		break;
224 	case SND_SOC_DAIFMT_NB_IF:
225 		da |= MAX9850_INV;
226 		break;
227 	default:
228 		return -EINVAL;
229 	}
230 
231 	/* set da */
232 	snd_soc_component_write(component, MAX9850_DIGITAL_AUDIO, da);
233 
234 	return 0;
235 }
236 
237 static int max9850_set_bias_level(struct snd_soc_component *component,
238 				  enum snd_soc_bias_level level)
239 {
240 	struct max9850_priv *max9850 = snd_soc_component_get_drvdata(component);
241 	int ret;
242 
243 	switch (level) {
244 	case SND_SOC_BIAS_ON:
245 		break;
246 	case SND_SOC_BIAS_PREPARE:
247 		break;
248 	case SND_SOC_BIAS_STANDBY:
249 		if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
250 			ret = regcache_sync(max9850->regmap);
251 			if (ret) {
252 				dev_err(component->dev,
253 					"Failed to sync cache: %d\n", ret);
254 				return ret;
255 			}
256 		}
257 		break;
258 	case SND_SOC_BIAS_OFF:
259 		break;
260 	}
261 	return 0;
262 }
263 
264 #define MAX9850_RATES SNDRV_PCM_RATE_8000_48000
265 
266 #define MAX9850_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
267 	SNDRV_PCM_FMTBIT_S24_LE)
268 
269 static const struct snd_soc_dai_ops max9850_dai_ops = {
270 	.hw_params	= max9850_hw_params,
271 	.set_sysclk	= max9850_set_dai_sysclk,
272 	.set_fmt	= max9850_set_dai_fmt,
273 };
274 
275 static struct snd_soc_dai_driver max9850_dai = {
276 	.name = "max9850-hifi",
277 	.playback = {
278 		.stream_name = "Playback",
279 		.channels_min = 1,
280 		.channels_max = 2,
281 		.rates = MAX9850_RATES,
282 		.formats = MAX9850_FORMATS
283 	},
284 	.ops = &max9850_dai_ops,
285 };
286 
287 static int max9850_probe(struct snd_soc_component *component)
288 {
289 	/* enable zero-detect */
290 	snd_soc_component_update_bits(component, MAX9850_GENERAL_PURPOSE, 1, 1);
291 	/* enable slew-rate control */
292 	snd_soc_component_update_bits(component, MAX9850_VOLUME, 0x40, 0x40);
293 	/* set slew-rate 125ms */
294 	snd_soc_component_update_bits(component, MAX9850_CHARGE_PUMP, 0xff, 0xc0);
295 
296 	return 0;
297 }
298 
299 static const struct snd_soc_component_driver soc_component_dev_max9850 = {
300 	.probe			= max9850_probe,
301 	.set_bias_level		= max9850_set_bias_level,
302 	.controls		= max9850_controls,
303 	.num_controls		= ARRAY_SIZE(max9850_controls),
304 	.dapm_widgets		= max9850_dapm_widgets,
305 	.num_dapm_widgets	= ARRAY_SIZE(max9850_dapm_widgets),
306 	.dapm_routes		= max9850_dapm_routes,
307 	.num_dapm_routes	= ARRAY_SIZE(max9850_dapm_routes),
308 	.suspend_bias_off	= 1,
309 	.idle_bias_on		= 1,
310 	.use_pmdown_time	= 1,
311 	.endianness		= 1,
312 	.non_legacy_dai_naming	= 1,
313 };
314 
315 static int max9850_i2c_probe(struct i2c_client *i2c,
316 			     const struct i2c_device_id *id)
317 {
318 	struct max9850_priv *max9850;
319 	int ret;
320 
321 	max9850 = devm_kzalloc(&i2c->dev, sizeof(struct max9850_priv),
322 			       GFP_KERNEL);
323 	if (max9850 == NULL)
324 		return -ENOMEM;
325 
326 	max9850->regmap = devm_regmap_init_i2c(i2c, &max9850_regmap);
327 	if (IS_ERR(max9850->regmap))
328 		return PTR_ERR(max9850->regmap);
329 
330 	i2c_set_clientdata(i2c, max9850);
331 
332 	ret = devm_snd_soc_register_component(&i2c->dev,
333 			&soc_component_dev_max9850, &max9850_dai, 1);
334 	return ret;
335 }
336 
337 static const struct i2c_device_id max9850_i2c_id[] = {
338 	{ "max9850", 0 },
339 	{ }
340 };
341 MODULE_DEVICE_TABLE(i2c, max9850_i2c_id);
342 
343 static struct i2c_driver max9850_i2c_driver = {
344 	.driver = {
345 		.name = "max9850",
346 	},
347 	.probe = max9850_i2c_probe,
348 	.id_table = max9850_i2c_id,
349 };
350 
351 module_i2c_driver(max9850_i2c_driver);
352 
353 MODULE_AUTHOR("Christian Glindkamp <christian.glindkamp@taskit.de>");
354 MODULE_DESCRIPTION("ASoC MAX9850 codec driver");
355 MODULE_LICENSE("GPL");
356