1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
30struct max9850_priv {
31 struct regmap *regmap;
32 unsigned int sysclk;
33};
34
35
36static 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
49
50static 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
61static 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
70static const DECLARE_TLV_DB_RANGE(max9850_tlv,
71 0x18, 0x1f, TLV_DB_SCALE_ITEM(-7450, 400, 0),
72 0x20, 0x33, TLV_DB_SCALE_ITEM(-4150, 200, 0),
73 0x34, 0x37, TLV_DB_SCALE_ITEM(-150, 100, 0),
74 0x38, 0x3f, TLV_DB_SCALE_ITEM(250, 50, 0)
75);
76
77static const struct snd_kcontrol_new max9850_controls[] = {
78SOC_SINGLE_TLV("Headphone Volume", MAX9850_VOLUME, 0, 0x3f, 1, max9850_tlv),
79SOC_SINGLE("Headphone Switch", MAX9850_VOLUME, 7, 1, 1),
80SOC_SINGLE("Mono Switch", MAX9850_GENERAL_PURPOSE, 2, 1, 0),
81};
82
83static const struct snd_kcontrol_new max9850_mixer_controls[] = {
84 SOC_DAPM_SINGLE("Line In Switch", MAX9850_ENABLE, 1, 1, 0),
85};
86
87static const struct snd_soc_dapm_widget max9850_dapm_widgets[] = {
88SND_SOC_DAPM_SUPPLY("Charge Pump 1", MAX9850_ENABLE, 4, 0, NULL, 0),
89SND_SOC_DAPM_SUPPLY("Charge Pump 2", MAX9850_ENABLE, 5, 0, NULL, 0),
90SND_SOC_DAPM_SUPPLY("MCLK", MAX9850_ENABLE, 6, 0, NULL, 0),
91SND_SOC_DAPM_SUPPLY("SHDN", MAX9850_ENABLE, 7, 0, NULL, 0),
92SND_SOC_DAPM_MIXER_NAMED_CTL("Output Mixer", MAX9850_ENABLE, 2, 0,
93 &max9850_mixer_controls[0],
94 ARRAY_SIZE(max9850_mixer_controls)),
95SND_SOC_DAPM_PGA("Headphone Output", MAX9850_ENABLE, 3, 0, NULL, 0),
96SND_SOC_DAPM_DAC("DAC", "HiFi Playback", MAX9850_ENABLE, 0, 0),
97SND_SOC_DAPM_OUTPUT("OUTL"),
98SND_SOC_DAPM_OUTPUT("HPL"),
99SND_SOC_DAPM_OUTPUT("OUTR"),
100SND_SOC_DAPM_OUTPUT("HPR"),
101SND_SOC_DAPM_MIXER("Line Input", SND_SOC_NOPM, 0, 0, NULL, 0),
102SND_SOC_DAPM_INPUT("INL"),
103SND_SOC_DAPM_INPUT("INR"),
104};
105
106static const struct snd_soc_dapm_route max9850_dapm_routes[] = {
107
108 {"Output Mixer", NULL, "DAC"},
109 {"Output Mixer", "Line In Switch", "Line Input"},
110
111
112 {"Headphone Output", NULL, "Output Mixer"},
113 {"HPL", NULL, "Headphone Output"},
114 {"HPR", NULL, "Headphone Output"},
115 {"OUTL", NULL, "Output Mixer"},
116 {"OUTR", NULL, "Output Mixer"},
117
118
119 {"Line Input", NULL, "INL"},
120 {"Line Input", NULL, "INR"},
121
122
123 {"Output Mixer", NULL, "Charge Pump 1"},
124 {"Output Mixer", NULL, "Charge Pump 2"},
125 {"Output Mixer", NULL, "SHDN"},
126 {"DAC", NULL, "MCLK"},
127};
128
129static int max9850_hw_params(struct snd_pcm_substream *substream,
130 struct snd_pcm_hw_params *params,
131 struct snd_soc_dai *dai)
132{
133 struct snd_soc_codec *codec = dai->codec;
134 struct max9850_priv *max9850 = snd_soc_codec_get_drvdata(codec);
135 u64 lrclk_div;
136 u8 sf, da;
137
138 if (!max9850->sysclk)
139 return -EINVAL;
140
141
142 sf = (snd_soc_read(codec, MAX9850_CLOCK) >> 2) + 1;
143 lrclk_div = (1 << 22);
144 lrclk_div *= params_rate(params);
145 lrclk_div *= sf;
146 do_div(lrclk_div, max9850->sysclk);
147
148 snd_soc_write(codec, MAX9850_LRCLK_MSB, (lrclk_div >> 8) & 0x7f);
149 snd_soc_write(codec, MAX9850_LRCLK_LSB, lrclk_div & 0xff);
150
151 switch (params_width(params)) {
152 case 16:
153 da = 0;
154 break;
155 case 20:
156 da = 0x2;
157 break;
158 case 24:
159 da = 0x3;
160 break;
161 default:
162 return -EINVAL;
163 }
164 snd_soc_update_bits(codec, MAX9850_DIGITAL_AUDIO, 0x3, da);
165
166 return 0;
167}
168
169static int max9850_set_dai_sysclk(struct snd_soc_dai *codec_dai,
170 int clk_id, unsigned int freq, int dir)
171{
172 struct snd_soc_codec *codec = codec_dai->codec;
173 struct max9850_priv *max9850 = snd_soc_codec_get_drvdata(codec);
174
175
176 if (freq <= 13000000)
177 snd_soc_write(codec, MAX9850_CLOCK, 0x0);
178 else if (freq <= 26000000)
179 snd_soc_write(codec, MAX9850_CLOCK, 0x4);
180 else if (freq <= 40000000)
181 snd_soc_write(codec, MAX9850_CLOCK, 0x8);
182 else
183 return -EINVAL;
184
185 max9850->sysclk = freq;
186 return 0;
187}
188
189static int max9850_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
190{
191 struct snd_soc_codec *codec = codec_dai->codec;
192 u8 da = 0;
193
194
195 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
196 case SND_SOC_DAIFMT_CBM_CFM:
197 da |= MAX9850_MASTER;
198 break;
199 case SND_SOC_DAIFMT_CBS_CFS:
200 break;
201 default:
202 return -EINVAL;
203 }
204
205
206 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
207 case SND_SOC_DAIFMT_I2S:
208 da |= MAX9850_DLY;
209 break;
210 case SND_SOC_DAIFMT_RIGHT_J:
211 da |= MAX9850_RTJ;
212 break;
213 case SND_SOC_DAIFMT_LEFT_J:
214 break;
215 default:
216 return -EINVAL;
217 }
218
219
220 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
221 case SND_SOC_DAIFMT_NB_NF:
222 break;
223 case SND_SOC_DAIFMT_IB_IF:
224 da |= MAX9850_BCINV | MAX9850_INV;
225 break;
226 case SND_SOC_DAIFMT_IB_NF:
227 da |= MAX9850_BCINV;
228 break;
229 case SND_SOC_DAIFMT_NB_IF:
230 da |= MAX9850_INV;
231 break;
232 default:
233 return -EINVAL;
234 }
235
236
237 snd_soc_write(codec, MAX9850_DIGITAL_AUDIO, da);
238
239 return 0;
240}
241
242static int max9850_set_bias_level(struct snd_soc_codec *codec,
243 enum snd_soc_bias_level level)
244{
245 struct max9850_priv *max9850 = snd_soc_codec_get_drvdata(codec);
246 int ret;
247
248 switch (level) {
249 case SND_SOC_BIAS_ON:
250 break;
251 case SND_SOC_BIAS_PREPARE:
252 break;
253 case SND_SOC_BIAS_STANDBY:
254 if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
255 ret = regcache_sync(max9850->regmap);
256 if (ret) {
257 dev_err(codec->dev,
258 "Failed to sync cache: %d\n", ret);
259 return ret;
260 }
261 }
262 break;
263 case SND_SOC_BIAS_OFF:
264 break;
265 }
266 return 0;
267}
268
269#define MAX9850_RATES SNDRV_PCM_RATE_8000_48000
270
271#define MAX9850_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
272 SNDRV_PCM_FMTBIT_S24_LE)
273
274static const struct snd_soc_dai_ops max9850_dai_ops = {
275 .hw_params = max9850_hw_params,
276 .set_sysclk = max9850_set_dai_sysclk,
277 .set_fmt = max9850_set_dai_fmt,
278};
279
280static struct snd_soc_dai_driver max9850_dai = {
281 .name = "max9850-hifi",
282 .playback = {
283 .stream_name = "Playback",
284 .channels_min = 1,
285 .channels_max = 2,
286 .rates = MAX9850_RATES,
287 .formats = MAX9850_FORMATS
288 },
289 .ops = &max9850_dai_ops,
290};
291
292static int max9850_probe(struct snd_soc_codec *codec)
293{
294
295 snd_soc_update_bits(codec, MAX9850_GENERAL_PURPOSE, 1, 1);
296
297 snd_soc_update_bits(codec, MAX9850_VOLUME, 0x40, 0x40);
298
299 snd_soc_update_bits(codec, MAX9850_CHARGE_PUMP, 0xff, 0xc0);
300
301 return 0;
302}
303
304static struct snd_soc_codec_driver soc_codec_dev_max9850 = {
305 .probe = max9850_probe,
306 .set_bias_level = max9850_set_bias_level,
307 .suspend_bias_off = true,
308
309 .component_driver = {
310 .controls = max9850_controls,
311 .num_controls = ARRAY_SIZE(max9850_controls),
312 .dapm_widgets = max9850_dapm_widgets,
313 .num_dapm_widgets = ARRAY_SIZE(max9850_dapm_widgets),
314 .dapm_routes = max9850_dapm_routes,
315 .num_dapm_routes = ARRAY_SIZE(max9850_dapm_routes),
316 },
317};
318
319static 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
341static int max9850_i2c_remove(struct i2c_client *client)
342{
343 snd_soc_unregister_codec(&client->dev);
344 return 0;
345}
346
347static const struct i2c_device_id max9850_i2c_id[] = {
348 { "max9850", 0 },
349 { }
350};
351MODULE_DEVICE_TABLE(i2c, max9850_i2c_id);
352
353static struct i2c_driver max9850_i2c_driver = {
354 .driver = {
355 .name = "max9850",
356 },
357 .probe = max9850_i2c_probe,
358 .remove = max9850_i2c_remove,
359 .id_table = max9850_i2c_id,
360};
361
362module_i2c_driver(max9850_i2c_driver);
363
364MODULE_AUTHOR("Christian Glindkamp <christian.glindkamp@taskit.de>");
365MODULE_DESCRIPTION("ASoC MAX9850 codec driver");
366MODULE_LICENSE("GPL");
367