Merge tag 'acpica-4.2-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael...
[deliverable/linux.git] / sound / soc / codecs / max9850.c
CommitLineData
0e45cab6
CG
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>
06841662 21#include <linux/regmap.h>
0e45cab6
CG
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 {
06841662 31 struct regmap *regmap;
0e45cab6
CG
32 unsigned int sysclk;
33};
34
35/* max9850 register cache */
06841662
MB
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 },
0e45cab6
CG
46};
47
48/* these registers are not used at the moment but provided for the sake of
49 * completeness */
06841662 50static bool max9850_volatile_register(struct device *dev, unsigned int reg)
0e45cab6
CG
51{
52 switch (reg) {
53 case MAX9850_STATUSA:
54 case MAX9850_STATUSB:
55 return 1;
56 default:
57 return 0;
58 }
59}
60
06841662
MB
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
0e45cab6
CG
70static 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
78static const struct snd_kcontrol_new max9850_controls[] = {
79SOC_SINGLE_TLV("Headphone Volume", MAX9850_VOLUME, 0, 0x3f, 1, max9850_tlv),
80SOC_SINGLE("Headphone Switch", MAX9850_VOLUME, 7, 1, 1),
81SOC_SINGLE("Mono Switch", MAX9850_GENERAL_PURPOSE, 2, 1, 0),
82};
83
84static const struct snd_kcontrol_new max9850_mixer_controls[] = {
85 SOC_DAPM_SINGLE("Line In Switch", MAX9850_ENABLE, 1, 1, 0),
86};
87
88static const struct snd_soc_dapm_widget max9850_dapm_widgets[] = {
89SND_SOC_DAPM_SUPPLY("Charge Pump 1", MAX9850_ENABLE, 4, 0, NULL, 0),
90SND_SOC_DAPM_SUPPLY("Charge Pump 2", MAX9850_ENABLE, 5, 0, NULL, 0),
91SND_SOC_DAPM_SUPPLY("MCLK", MAX9850_ENABLE, 6, 0, NULL, 0),
92SND_SOC_DAPM_SUPPLY("SHDN", MAX9850_ENABLE, 7, 0, NULL, 0),
93SND_SOC_DAPM_MIXER_NAMED_CTL("Output Mixer", MAX9850_ENABLE, 2, 0,
94 &max9850_mixer_controls[0],
95 ARRAY_SIZE(max9850_mixer_controls)),
96SND_SOC_DAPM_PGA("Headphone Output", MAX9850_ENABLE, 3, 0, NULL, 0),
97SND_SOC_DAPM_DAC("DAC", "HiFi Playback", MAX9850_ENABLE, 0, 0),
98SND_SOC_DAPM_OUTPUT("OUTL"),
99SND_SOC_DAPM_OUTPUT("HPL"),
100SND_SOC_DAPM_OUTPUT("OUTR"),
101SND_SOC_DAPM_OUTPUT("HPR"),
102SND_SOC_DAPM_MIXER("Line Input", SND_SOC_NOPM, 0, 0, NULL, 0),
103SND_SOC_DAPM_INPUT("INL"),
104SND_SOC_DAPM_INPUT("INR"),
105};
106
5ee65ec6 107static const struct snd_soc_dapm_route max9850_dapm_routes[] = {
0e45cab6
CG
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
130static 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
27380fb8 139 if (!max9850->sysclk)
0e45cab6
CG
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
0058e459
MB
152 switch (params_width(params)) {
153 case 16:
0e45cab6
CG
154 da = 0;
155 break;
0058e459 156 case 20:
0e45cab6
CG
157 da = 0x2;
158 break;
0058e459 159 case 24:
0e45cab6
CG
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
170static 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
190static 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
243static int max9850_set_bias_level(struct snd_soc_codec *codec,
244 enum snd_soc_bias_level level)
245{
06841662 246 struct max9850_priv *max9850 = snd_soc_codec_get_drvdata(codec);
0e45cab6
CG
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:
3054716d 255 if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
06841662 256 ret = regcache_sync(max9850->regmap);
0e45cab6
CG
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 }
0e45cab6
CG
267 return 0;
268}
269
270#define MAX9850_RATES SNDRV_PCM_RATE_8000_48000
271
272#define MAX9850_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
273 SNDRV_PCM_FMTBIT_S24_LE)
274
85e7652d 275static const struct snd_soc_dai_ops max9850_dai_ops = {
0e45cab6
CG
276 .hw_params = max9850_hw_params,
277 .set_sysclk = max9850_set_dai_sysclk,
278 .set_fmt = max9850_set_dai_fmt,
279};
280
281static struct snd_soc_dai_driver max9850_dai = {
282 .name = "max9850-hifi",
283 .playback = {
284 .stream_name = "Playback",
285 .channels_min = 1,
286 .channels_max = 2,
287 .rates = MAX9850_RATES,
288 .formats = MAX9850_FORMATS
289 },
290 .ops = &max9850_dai_ops,
291};
292
0e45cab6
CG
293static int max9850_probe(struct snd_soc_codec *codec)
294{
0e45cab6
CG
295 /* enable zero-detect */
296 snd_soc_update_bits(codec, MAX9850_GENERAL_PURPOSE, 1, 1);
297 /* enable slew-rate control */
298 snd_soc_update_bits(codec, MAX9850_VOLUME, 0x40, 0x40);
299 /* set slew-rate 125ms */
300 snd_soc_update_bits(codec, MAX9850_CHARGE_PUMP, 0xff, 0xc0);
301
0e45cab6
CG
302 return 0;
303}
304
305static struct snd_soc_codec_driver soc_codec_dev_max9850 = {
306 .probe = max9850_probe,
0e45cab6 307 .set_bias_level = max9850_set_bias_level,
46804120 308 .suspend_bias_off = true,
5ee65ec6
AL
309
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),
0e45cab6
CG
316};
317
7a79e94e
BP
318static int max9850_i2c_probe(struct i2c_client *i2c,
319 const struct i2c_device_id *id)
0e45cab6
CG
320{
321 struct max9850_priv *max9850;
322 int ret;
323
bf791bdb
AL
324 max9850 = devm_kzalloc(&i2c->dev, sizeof(struct max9850_priv),
325 GFP_KERNEL);
0e45cab6
CG
326 if (max9850 == NULL)
327 return -ENOMEM;
328
06841662
MB
329 max9850->regmap = devm_regmap_init_i2c(i2c, &max9850_regmap);
330 if (IS_ERR(max9850->regmap))
331 return PTR_ERR(max9850->regmap);
332
0e45cab6
CG
333 i2c_set_clientdata(i2c, max9850);
334
335 ret = snd_soc_register_codec(&i2c->dev,
336 &soc_codec_dev_max9850, &max9850_dai, 1);
0e45cab6
CG
337 return ret;
338}
339
7a79e94e 340static int max9850_i2c_remove(struct i2c_client *client)
0e45cab6
CG
341{
342 snd_soc_unregister_codec(&client->dev);
0e45cab6
CG
343 return 0;
344}
345
346static const struct i2c_device_id max9850_i2c_id[] = {
347 { "max9850", 0 },
348 { }
349};
350MODULE_DEVICE_TABLE(i2c, max9850_i2c_id);
351
352static struct i2c_driver max9850_i2c_driver = {
353 .driver = {
354 .name = "max9850",
355 .owner = THIS_MODULE,
356 },
357 .probe = max9850_i2c_probe,
7a79e94e 358 .remove = max9850_i2c_remove,
0e45cab6
CG
359 .id_table = max9850_i2c_id,
360};
361
4abdc8c8 362module_i2c_driver(max9850_i2c_driver);
0e45cab6
CG
363
364MODULE_AUTHOR("Christian Glindkamp <christian.glindkamp@taskit.de>");
365MODULE_DESCRIPTION("ASoC MAX9850 codec driver");
366MODULE_LICENSE("GPL");
This page took 0.197761 seconds and 5 git commands to generate.