Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 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> |
Mark Brown | 0684166 | 2013-09-23 18:12:51 +0100 | [diff] [blame] | 21 | #include <linux/regmap.h> |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 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 { |
Mark Brown | 0684166 | 2013-09-23 18:12:51 +0100 | [diff] [blame] | 31 | struct regmap *regmap; |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 32 | unsigned int sysclk; |
| 33 | }; |
| 34 | |
| 35 | /* max9850 register cache */ |
Mark Brown | 0684166 | 2013-09-23 18:12:51 +0100 | [diff] [blame] | 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 }, |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | /* these registers are not used at the moment but provided for the sake of |
| 49 | * completeness */ |
Mark Brown | 0684166 | 2013-09-23 18:12:51 +0100 | [diff] [blame] | 50 | static bool max9850_volatile_register(struct device *dev, unsigned int reg) |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 51 | { |
| 52 | switch (reg) { |
| 53 | case MAX9850_STATUSA: |
| 54 | case MAX9850_STATUSB: |
| 55 | return 1; |
| 56 | default: |
| 57 | return 0; |
| 58 | } |
| 59 | } |
| 60 | |
Mark Brown | 0684166 | 2013-09-23 18:12:51 +0100 | [diff] [blame] | 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 | |
Lars-Peter Clausen | e034098 | 2015-08-02 17:19:45 +0200 | [diff] [blame] | 70 | static const DECLARE_TLV_DB_RANGE(max9850_tlv, |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 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), |
Lars-Peter Clausen | e034098 | 2015-08-02 17:19:45 +0200 | [diff] [blame] | 74 | 0x38, 0x3f, TLV_DB_SCALE_ITEM(250, 50, 0) |
| 75 | ); |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 76 | |
| 77 | static const struct snd_kcontrol_new max9850_controls[] = { |
| 78 | SOC_SINGLE_TLV("Headphone Volume", MAX9850_VOLUME, 0, 0x3f, 1, max9850_tlv), |
| 79 | SOC_SINGLE("Headphone Switch", MAX9850_VOLUME, 7, 1, 1), |
| 80 | SOC_SINGLE("Mono Switch", MAX9850_GENERAL_PURPOSE, 2, 1, 0), |
| 81 | }; |
| 82 | |
| 83 | static const struct snd_kcontrol_new max9850_mixer_controls[] = { |
| 84 | SOC_DAPM_SINGLE("Line In Switch", MAX9850_ENABLE, 1, 1, 0), |
| 85 | }; |
| 86 | |
| 87 | static const struct snd_soc_dapm_widget max9850_dapm_widgets[] = { |
| 88 | SND_SOC_DAPM_SUPPLY("Charge Pump 1", MAX9850_ENABLE, 4, 0, NULL, 0), |
| 89 | SND_SOC_DAPM_SUPPLY("Charge Pump 2", MAX9850_ENABLE, 5, 0, NULL, 0), |
| 90 | SND_SOC_DAPM_SUPPLY("MCLK", MAX9850_ENABLE, 6, 0, NULL, 0), |
| 91 | SND_SOC_DAPM_SUPPLY("SHDN", MAX9850_ENABLE, 7, 0, NULL, 0), |
| 92 | SND_SOC_DAPM_MIXER_NAMED_CTL("Output Mixer", MAX9850_ENABLE, 2, 0, |
| 93 | &max9850_mixer_controls[0], |
| 94 | ARRAY_SIZE(max9850_mixer_controls)), |
| 95 | SND_SOC_DAPM_PGA("Headphone Output", MAX9850_ENABLE, 3, 0, NULL, 0), |
| 96 | SND_SOC_DAPM_DAC("DAC", "HiFi Playback", MAX9850_ENABLE, 0, 0), |
| 97 | SND_SOC_DAPM_OUTPUT("OUTL"), |
| 98 | SND_SOC_DAPM_OUTPUT("HPL"), |
| 99 | SND_SOC_DAPM_OUTPUT("OUTR"), |
| 100 | SND_SOC_DAPM_OUTPUT("HPR"), |
| 101 | SND_SOC_DAPM_MIXER("Line Input", SND_SOC_NOPM, 0, 0, NULL, 0), |
| 102 | SND_SOC_DAPM_INPUT("INL"), |
| 103 | SND_SOC_DAPM_INPUT("INR"), |
| 104 | }; |
| 105 | |
Axel Lin | 5ee65ec | 2011-12-19 13:13:31 +0800 | [diff] [blame] | 106 | static const struct snd_soc_dapm_route max9850_dapm_routes[] = { |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 107 | /* 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 | |
| 129 | static 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 Brown | 27380fb | 2011-03-11 12:07:31 +0000 | [diff] [blame] | 138 | if (!max9850->sysclk) |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 139 | 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 Brown | 0058e45 | 2014-01-08 20:39:44 +0000 | [diff] [blame] | 151 | switch (params_width(params)) { |
| 152 | case 16: |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 153 | da = 0; |
| 154 | break; |
Mark Brown | 0058e45 | 2014-01-08 20:39:44 +0000 | [diff] [blame] | 155 | case 20: |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 156 | da = 0x2; |
| 157 | break; |
Mark Brown | 0058e45 | 2014-01-08 20:39:44 +0000 | [diff] [blame] | 158 | case 24: |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 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 | |
| 169 | static 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 | |
| 189 | static 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 | |
| 242 | static int max9850_set_bias_level(struct snd_soc_codec *codec, |
| 243 | enum snd_soc_bias_level level) |
| 244 | { |
Mark Brown | 0684166 | 2013-09-23 18:12:51 +0100 | [diff] [blame] | 245 | struct max9850_priv *max9850 = snd_soc_codec_get_drvdata(codec); |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 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: |
Lars-Peter Clausen | 3054716 | 2015-05-14 11:20:03 +0200 | [diff] [blame] | 254 | if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) { |
Mark Brown | 0684166 | 2013-09-23 18:12:51 +0100 | [diff] [blame] | 255 | ret = regcache_sync(max9850->regmap); |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 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 | } |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 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 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 274 | static const struct snd_soc_dai_ops max9850_dai_ops = { |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 275 | .hw_params = max9850_hw_params, |
| 276 | .set_sysclk = max9850_set_dai_sysclk, |
| 277 | .set_fmt = max9850_set_dai_fmt, |
| 278 | }; |
| 279 | |
| 280 | static 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 Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 292 | static int max9850_probe(struct snd_soc_codec *codec) |
| 293 | { |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 294 | /* 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 Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | static struct snd_soc_codec_driver soc_codec_dev_max9850 = { |
| 305 | .probe = max9850_probe, |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 306 | .set_bias_level = max9850_set_bias_level, |
Lars-Peter Clausen | 4680412 | 2014-10-20 10:56:35 +0200 | [diff] [blame] | 307 | .suspend_bias_off = true, |
Axel Lin | 5ee65ec | 2011-12-19 13:13:31 +0800 | [diff] [blame] | 308 | |
| 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 Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 315 | }; |
| 316 | |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 317 | static int max9850_i2c_probe(struct i2c_client *i2c, |
| 318 | const struct i2c_device_id *id) |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 319 | { |
| 320 | struct max9850_priv *max9850; |
| 321 | int ret; |
| 322 | |
Axel Lin | bf791bd | 2011-12-29 12:03:16 +0800 | [diff] [blame] | 323 | max9850 = devm_kzalloc(&i2c->dev, sizeof(struct max9850_priv), |
| 324 | GFP_KERNEL); |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 325 | if (max9850 == NULL) |
| 326 | return -ENOMEM; |
| 327 | |
Mark Brown | 0684166 | 2013-09-23 18:12:51 +0100 | [diff] [blame] | 328 | max9850->regmap = devm_regmap_init_i2c(i2c, &max9850_regmap); |
| 329 | if (IS_ERR(max9850->regmap)) |
| 330 | return PTR_ERR(max9850->regmap); |
| 331 | |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 332 | i2c_set_clientdata(i2c, max9850); |
| 333 | |
| 334 | ret = snd_soc_register_codec(&i2c->dev, |
| 335 | &soc_codec_dev_max9850, &max9850_dai, 1); |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 336 | return ret; |
| 337 | } |
| 338 | |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 339 | static int max9850_i2c_remove(struct i2c_client *client) |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 340 | { |
| 341 | snd_soc_unregister_codec(&client->dev); |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | static const struct i2c_device_id max9850_i2c_id[] = { |
| 346 | { "max9850", 0 }, |
| 347 | { } |
| 348 | }; |
| 349 | MODULE_DEVICE_TABLE(i2c, max9850_i2c_id); |
| 350 | |
| 351 | static struct i2c_driver max9850_i2c_driver = { |
| 352 | .driver = { |
| 353 | .name = "max9850", |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 354 | }, |
| 355 | .probe = max9850_i2c_probe, |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 356 | .remove = max9850_i2c_remove, |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 357 | .id_table = max9850_i2c_id, |
| 358 | }; |
| 359 | |
Sachin Kamat | 4abdc8c | 2012-08-06 17:25:46 +0530 | [diff] [blame] | 360 | module_i2c_driver(max9850_i2c_driver); |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 361 | |
| 362 | MODULE_AUTHOR("Christian Glindkamp <christian.glindkamp@taskit.de>"); |
| 363 | MODULE_DESCRIPTION("ASoC MAX9850 codec driver"); |
| 364 | MODULE_LICENSE("GPL"); |