blob: 3774e42c8eab374f0950a39861ab807904ecf2e0 [file] [log] [blame]
Christian Glindkamp0e45cab2011-03-09 11:20:04 +01001/*
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>
Mark Brown06841662013-09-23 18:12:51 +010021#include <linux/regmap.h>
Christian Glindkamp0e45cab2011-03-09 11:20:04 +010022#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 {
Mark Brown06841662013-09-23 18:12:51 +010031 struct regmap *regmap;
Christian Glindkamp0e45cab2011-03-09 11:20:04 +010032 unsigned int sysclk;
33};
34
35/* max9850 register cache */
Mark Brown06841662013-09-23 18:12:51 +010036static 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 },
Christian Glindkamp0e45cab2011-03-09 11:20:04 +010046};
47
48/* these registers are not used at the moment but provided for the sake of
49 * completeness */
Mark Brown06841662013-09-23 18:12:51 +010050static bool max9850_volatile_register(struct device *dev, unsigned int reg)
Christian Glindkamp0e45cab2011-03-09 11:20:04 +010051{
52 switch (reg) {
53 case MAX9850_STATUSA:
54 case MAX9850_STATUSB:
55 return 1;
56 default:
57 return 0;
58 }
59}
60
Mark Brown06841662013-09-23 18:12:51 +010061static 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
Lars-Peter Clausene0340982015-08-02 17:19:45 +020070static const DECLARE_TLV_DB_RANGE(max9850_tlv,
Christian Glindkamp0e45cab2011-03-09 11:20:04 +010071 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),
Lars-Peter Clausene0340982015-08-02 17:19:45 +020074 0x38, 0x3f, TLV_DB_SCALE_ITEM(250, 50, 0)
75);
Christian Glindkamp0e45cab2011-03-09 11:20:04 +010076
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
Axel Lin5ee65ec2011-12-19 13:13:31 +0800106static const struct snd_soc_dapm_route max9850_dapm_routes[] = {
Christian Glindkamp0e45cab2011-03-09 11:20:04 +0100107 /* output mixer */
108 {"Output Mixer", NULL, "DAC"},
109 {"Output Mixer", "Line In Switch", "Line Input"},
110
111 /* outputs */
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 /* inputs */
119 {"Line Input", NULL, "INL"},
120 {"Line Input", NULL, "INR"},
121
122 /* supplies */
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
Mark Brown27380fb2011-03-11 12:07:31 +0000138 if (!max9850->sysclk)
Christian Glindkamp0e45cab2011-03-09 11:20:04 +0100139 return -EINVAL;
140
141 /* lrclk_div = 2^22 * rate / iclk with iclk = mclk / sf */
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
Mark Brown0058e452014-01-08 20:39:44 +0000151 switch (params_width(params)) {
152 case 16:
Christian Glindkamp0e45cab2011-03-09 11:20:04 +0100153 da = 0;
154 break;
Mark Brown0058e452014-01-08 20:39:44 +0000155 case 20:
Christian Glindkamp0e45cab2011-03-09 11:20:04 +0100156 da = 0x2;
157 break;
Mark Brown0058e452014-01-08 20:39:44 +0000158 case 24:
Christian Glindkamp0e45cab2011-03-09 11:20:04 +0100159 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 /* calculate mclk -> iclk divider */
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 /* set master/slave audio interface */
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 /* interface format */
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 /* clock inversion */
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 /* set da */
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{
Mark Brown06841662013-09-23 18:12:51 +0100245 struct max9850_priv *max9850 = snd_soc_codec_get_drvdata(codec);
Christian Glindkamp0e45cab2011-03-09 11:20:04 +0100246 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:
Lars-Peter Clausen30547162015-05-14 11:20:03 +0200254 if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
Mark Brown06841662013-09-23 18:12:51 +0100255 ret = regcache_sync(max9850->regmap);
Christian Glindkamp0e45cab2011-03-09 11:20:04 +0100256 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 }
Christian Glindkamp0e45cab2011-03-09 11:20:04 +0100266 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
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100274static const struct snd_soc_dai_ops max9850_dai_ops = {
Christian Glindkamp0e45cab2011-03-09 11:20:04 +0100275 .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
Christian Glindkamp0e45cab2011-03-09 11:20:04 +0100292static int max9850_probe(struct snd_soc_codec *codec)
293{
Christian Glindkamp0e45cab2011-03-09 11:20:04 +0100294 /* enable zero-detect */
295 snd_soc_update_bits(codec, MAX9850_GENERAL_PURPOSE, 1, 1);
296 /* enable slew-rate control */
297 snd_soc_update_bits(codec, MAX9850_VOLUME, 0x40, 0x40);
298 /* set slew-rate 125ms */
299 snd_soc_update_bits(codec, MAX9850_CHARGE_PUMP, 0xff, 0xc0);
300
Christian Glindkamp0e45cab2011-03-09 11:20:04 +0100301 return 0;
302}
303
304static struct snd_soc_codec_driver soc_codec_dev_max9850 = {
305 .probe = max9850_probe,
Christian Glindkamp0e45cab2011-03-09 11:20:04 +0100306 .set_bias_level = max9850_set_bias_level,
Lars-Peter Clausen46804122014-10-20 10:56:35 +0200307 .suspend_bias_off = true,
Axel Lin5ee65ec2011-12-19 13:13:31 +0800308
309 .controls = max9850_controls,
310 .num_controls = ARRAY_SIZE(max9850_controls),
311 .dapm_widgets = max9850_dapm_widgets,
312 .num_dapm_widgets = ARRAY_SIZE(max9850_dapm_widgets),
313 .dapm_routes = max9850_dapm_routes,
314 .num_dapm_routes = ARRAY_SIZE(max9850_dapm_routes),
Christian Glindkamp0e45cab2011-03-09 11:20:04 +0100315};
316
Bill Pemberton7a79e942012-12-07 09:26:37 -0500317static int max9850_i2c_probe(struct i2c_client *i2c,
318 const struct i2c_device_id *id)
Christian Glindkamp0e45cab2011-03-09 11:20:04 +0100319{
320 struct max9850_priv *max9850;
321 int ret;
322
Axel Linbf791bd2011-12-29 12:03:16 +0800323 max9850 = devm_kzalloc(&i2c->dev, sizeof(struct max9850_priv),
324 GFP_KERNEL);
Christian Glindkamp0e45cab2011-03-09 11:20:04 +0100325 if (max9850 == NULL)
326 return -ENOMEM;
327
Mark Brown06841662013-09-23 18:12:51 +0100328 max9850->regmap = devm_regmap_init_i2c(i2c, &max9850_regmap);
329 if (IS_ERR(max9850->regmap))
330 return PTR_ERR(max9850->regmap);
331
Christian Glindkamp0e45cab2011-03-09 11:20:04 +0100332 i2c_set_clientdata(i2c, max9850);
333
334 ret = snd_soc_register_codec(&i2c->dev,
335 &soc_codec_dev_max9850, &max9850_dai, 1);
Christian Glindkamp0e45cab2011-03-09 11:20:04 +0100336 return ret;
337}
338
Bill Pemberton7a79e942012-12-07 09:26:37 -0500339static int max9850_i2c_remove(struct i2c_client *client)
Christian Glindkamp0e45cab2011-03-09 11:20:04 +0100340{
341 snd_soc_unregister_codec(&client->dev);
Christian Glindkamp0e45cab2011-03-09 11:20:04 +0100342 return 0;
343}
344
345static const struct i2c_device_id max9850_i2c_id[] = {
346 { "max9850", 0 },
347 { }
348};
349MODULE_DEVICE_TABLE(i2c, max9850_i2c_id);
350
351static struct i2c_driver max9850_i2c_driver = {
352 .driver = {
353 .name = "max9850",
354 .owner = THIS_MODULE,
355 },
356 .probe = max9850_i2c_probe,
Bill Pemberton7a79e942012-12-07 09:26:37 -0500357 .remove = max9850_i2c_remove,
Christian Glindkamp0e45cab2011-03-09 11:20:04 +0100358 .id_table = max9850_i2c_id,
359};
360
Sachin Kamat4abdc8c2012-08-06 17:25:46 +0530361module_i2c_driver(max9850_i2c_driver);
Christian Glindkamp0e45cab2011-03-09 11:20:04 +0100362
363MODULE_AUTHOR("Christian Glindkamp <christian.glindkamp@taskit.de>");
364MODULE_DESCRIPTION("ASoC MAX9850 codec driver");
365MODULE_LICENSE("GPL");