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