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 | |
Mark Brown | 0058e45 | 2014-01-08 20:39:44 +0000 | [diff] [blame] | 152 | switch (params_width(params)) { |
| 153 | case 16: |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 154 | da = 0; |
| 155 | break; |
Mark Brown | 0058e45 | 2014-01-08 20:39:44 +0000 | [diff] [blame] | 156 | case 20: |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 157 | da = 0x2; |
| 158 | break; |
Mark Brown | 0058e45 | 2014-01-08 20:39:44 +0000 | [diff] [blame] | 159 | case 24: |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 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 | } |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 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 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 275 | static const struct snd_soc_dai_ops max9850_dai_ops = { |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 276 | .hw_params = max9850_hw_params, |
| 277 | .set_sysclk = max9850_set_dai_sysclk, |
| 278 | .set_fmt = max9850_set_dai_fmt, |
| 279 | }; |
| 280 | |
| 281 | static 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 | |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 293 | static int max9850_probe(struct snd_soc_codec *codec) |
| 294 | { |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 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 | |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | static struct snd_soc_codec_driver soc_codec_dev_max9850 = { |
| 306 | .probe = max9850_probe, |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 307 | .set_bias_level = max9850_set_bias_level, |
Lars-Peter Clausen | 4680412 | 2014-10-20 10:56:35 +0200 | [diff] [blame] | 308 | .suspend_bias_off = true, |
Axel Lin | 5ee65ec | 2011-12-19 13:13:31 +0800 | [diff] [blame] | 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), |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 316 | }; |
| 317 | |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 318 | static int max9850_i2c_probe(struct i2c_client *i2c, |
| 319 | const struct i2c_device_id *id) |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 320 | { |
| 321 | struct max9850_priv *max9850; |
| 322 | int ret; |
| 323 | |
Axel Lin | bf791bd | 2011-12-29 12:03:16 +0800 | [diff] [blame] | 324 | max9850 = devm_kzalloc(&i2c->dev, sizeof(struct max9850_priv), |
| 325 | GFP_KERNEL); |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 326 | if (max9850 == NULL) |
| 327 | return -ENOMEM; |
| 328 | |
Mark Brown | 0684166 | 2013-09-23 18:12:51 +0100 | [diff] [blame] | 329 | max9850->regmap = devm_regmap_init_i2c(i2c, &max9850_regmap); |
| 330 | if (IS_ERR(max9850->regmap)) |
| 331 | return PTR_ERR(max9850->regmap); |
| 332 | |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 333 | i2c_set_clientdata(i2c, max9850); |
| 334 | |
| 335 | ret = snd_soc_register_codec(&i2c->dev, |
| 336 | &soc_codec_dev_max9850, &max9850_dai, 1); |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 337 | return ret; |
| 338 | } |
| 339 | |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 340 | static int max9850_i2c_remove(struct i2c_client *client) |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 341 | { |
| 342 | snd_soc_unregister_codec(&client->dev); |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | static const struct i2c_device_id max9850_i2c_id[] = { |
| 347 | { "max9850", 0 }, |
| 348 | { } |
| 349 | }; |
| 350 | MODULE_DEVICE_TABLE(i2c, max9850_i2c_id); |
| 351 | |
| 352 | static struct i2c_driver max9850_i2c_driver = { |
| 353 | .driver = { |
| 354 | .name = "max9850", |
| 355 | .owner = THIS_MODULE, |
| 356 | }, |
| 357 | .probe = max9850_i2c_probe, |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 358 | .remove = max9850_i2c_remove, |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 359 | .id_table = max9850_i2c_id, |
| 360 | }; |
| 361 | |
Sachin Kamat | 4abdc8c | 2012-08-06 17:25:46 +0530 | [diff] [blame] | 362 | module_i2c_driver(max9850_i2c_driver); |
Christian Glindkamp | 0e45cab | 2011-03-09 11:20:04 +0100 | [diff] [blame] | 363 | |
| 364 | MODULE_AUTHOR("Christian Glindkamp <christian.glindkamp@taskit.de>"); |
| 365 | MODULE_DESCRIPTION("ASoC MAX9850 codec driver"); |
| 366 | MODULE_LICENSE("GPL"); |