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 | |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 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 | |
Axel Lin | 5ee65ec | 2011-12-19 13:13:31 +0800 | [diff] [blame] | 107 | static const struct snd_soc_dapm_route max9850_dapm_routes[] = { |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 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 | |
Mark Brown | 27380fb | 2011-03-11 12:07:31 +0000 | [diff] [blame] | 139 | if (!max9850->sysclk) |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 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_format(params)) { |
| 153 | case SNDRV_PCM_FORMAT_S16_LE: |
| 154 | da = 0; |
| 155 | break; |
| 156 | case SNDRV_PCM_FORMAT_S20_3LE: |
| 157 | da = 0x2; |
| 158 | break; |
| 159 | case SNDRV_PCM_FORMAT_S24_LE: |
| 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 | { |
Mark Brown | 0684166 | 2013-09-23 18:12:51 +0100 | [diff] [blame] | 246 | struct max9850_priv *max9850 = snd_soc_codec_get_drvdata(codec); |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 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) { |
Mark Brown | 0684166 | 2013-09-23 18:12:51 +0100 | [diff] [blame] | 256 | ret = regcache_sync(max9850->regmap); |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 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 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 276 | static const struct snd_soc_dai_ops max9850_dai_ops = { |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 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 | #ifdef CONFIG_PM |
Lars-Peter Clausen | 84b315e | 2011-12-02 10:18:28 +0100 | [diff] [blame] | 295 | static int max9850_suspend(struct snd_soc_codec *codec) |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 296 | { |
| 297 | max9850_set_bias_level(codec, SND_SOC_BIAS_OFF); |
| 298 | |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | static int max9850_resume(struct snd_soc_codec *codec) |
| 303 | { |
| 304 | max9850_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
| 305 | |
| 306 | return 0; |
| 307 | } |
| 308 | #else |
| 309 | #define max9850_suspend NULL |
| 310 | #define max9850_resume NULL |
| 311 | #endif |
| 312 | |
| 313 | static int max9850_probe(struct snd_soc_codec *codec) |
| 314 | { |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 315 | int ret; |
| 316 | |
Mark Brown | 0684166 | 2013-09-23 18:12:51 +0100 | [diff] [blame] | 317 | ret = snd_soc_codec_set_cache_io(codec, 8, 8, SND_SOC_REGMAP); |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 318 | if (ret < 0) { |
| 319 | dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret); |
| 320 | return ret; |
| 321 | } |
| 322 | |
| 323 | /* enable zero-detect */ |
| 324 | snd_soc_update_bits(codec, MAX9850_GENERAL_PURPOSE, 1, 1); |
| 325 | /* enable slew-rate control */ |
| 326 | snd_soc_update_bits(codec, MAX9850_VOLUME, 0x40, 0x40); |
| 327 | /* set slew-rate 125ms */ |
| 328 | snd_soc_update_bits(codec, MAX9850_CHARGE_PUMP, 0xff, 0xc0); |
| 329 | |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | static struct snd_soc_codec_driver soc_codec_dev_max9850 = { |
| 334 | .probe = max9850_probe, |
| 335 | .suspend = max9850_suspend, |
| 336 | .resume = max9850_resume, |
| 337 | .set_bias_level = max9850_set_bias_level, |
Axel Lin | 5ee65ec | 2011-12-19 13:13:31 +0800 | [diff] [blame] | 338 | |
| 339 | .controls = max9850_controls, |
| 340 | .num_controls = ARRAY_SIZE(max9850_controls), |
| 341 | .dapm_widgets = max9850_dapm_widgets, |
| 342 | .num_dapm_widgets = ARRAY_SIZE(max9850_dapm_widgets), |
| 343 | .dapm_routes = max9850_dapm_routes, |
| 344 | .num_dapm_routes = ARRAY_SIZE(max9850_dapm_routes), |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 345 | }; |
| 346 | |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 347 | static int max9850_i2c_probe(struct i2c_client *i2c, |
| 348 | const struct i2c_device_id *id) |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 349 | { |
| 350 | struct max9850_priv *max9850; |
| 351 | int ret; |
| 352 | |
Axel Lin | bf791bd | 2011-12-29 12:03:16 +0800 | [diff] [blame] | 353 | max9850 = devm_kzalloc(&i2c->dev, sizeof(struct max9850_priv), |
| 354 | GFP_KERNEL); |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 355 | if (max9850 == NULL) |
| 356 | return -ENOMEM; |
| 357 | |
Mark Brown | 0684166 | 2013-09-23 18:12:51 +0100 | [diff] [blame] | 358 | max9850->regmap = devm_regmap_init_i2c(i2c, &max9850_regmap); |
| 359 | if (IS_ERR(max9850->regmap)) |
| 360 | return PTR_ERR(max9850->regmap); |
| 361 | |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 362 | i2c_set_clientdata(i2c, max9850); |
| 363 | |
| 364 | ret = snd_soc_register_codec(&i2c->dev, |
| 365 | &soc_codec_dev_max9850, &max9850_dai, 1); |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 366 | return ret; |
| 367 | } |
| 368 | |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 369 | static int max9850_i2c_remove(struct i2c_client *client) |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 370 | { |
| 371 | snd_soc_unregister_codec(&client->dev); |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | static const struct i2c_device_id max9850_i2c_id[] = { |
| 376 | { "max9850", 0 }, |
| 377 | { } |
| 378 | }; |
| 379 | MODULE_DEVICE_TABLE(i2c, max9850_i2c_id); |
| 380 | |
| 381 | static struct i2c_driver max9850_i2c_driver = { |
| 382 | .driver = { |
| 383 | .name = "max9850", |
| 384 | .owner = THIS_MODULE, |
| 385 | }, |
| 386 | .probe = max9850_i2c_probe, |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 387 | .remove = max9850_i2c_remove, |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 388 | .id_table = max9850_i2c_id, |
| 389 | }; |
| 390 | |
Sachin Kamat | 4abdc8c | 2012-08-06 17:25:46 +0530 | [diff] [blame] | 391 | module_i2c_driver(max9850_i2c_driver); |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 392 | |
| 393 | MODULE_AUTHOR("Christian Glindkamp <christian.glindkamp@taskit.de>"); |
| 394 | MODULE_DESCRIPTION("ASoC MAX9850 codec driver"); |
| 395 | MODULE_LICENSE("GPL"); |