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> 21 #include <linux/slab.h> 22 #include <sound/pcm.h> 23 #include <sound/pcm_params.h> 24 #include <sound/soc.h> 25 #include <sound/tlv.h> 26 27 #include "max9850.h" 28 29 struct max9850_priv { 30 unsigned int sysclk; 31 }; 32 33 /* max9850 register cache */ 34 static const u8 max9850_reg[MAX9850_CACHEREGNUM] = { 35 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 36 }; 37 38 /* these registers are not used at the moment but provided for the sake of 39 * completeness */ 40 static int max9850_volatile_register(struct snd_soc_codec *codec, 41 unsigned int reg) 42 { 43 switch (reg) { 44 case MAX9850_STATUSA: 45 case MAX9850_STATUSB: 46 return 1; 47 default: 48 return 0; 49 } 50 } 51 52 static const unsigned int max9850_tlv[] = { 53 TLV_DB_RANGE_HEAD(4), 54 0x18, 0x1f, TLV_DB_SCALE_ITEM(-7450, 400, 0), 55 0x20, 0x33, TLV_DB_SCALE_ITEM(-4150, 200, 0), 56 0x34, 0x37, TLV_DB_SCALE_ITEM(-150, 100, 0), 57 0x38, 0x3f, TLV_DB_SCALE_ITEM(250, 50, 0), 58 }; 59 60 static const struct snd_kcontrol_new max9850_controls[] = { 61 SOC_SINGLE_TLV("Headphone Volume", MAX9850_VOLUME, 0, 0x3f, 1, max9850_tlv), 62 SOC_SINGLE("Headphone Switch", MAX9850_VOLUME, 7, 1, 1), 63 SOC_SINGLE("Mono Switch", MAX9850_GENERAL_PURPOSE, 2, 1, 0), 64 }; 65 66 static const struct snd_kcontrol_new max9850_mixer_controls[] = { 67 SOC_DAPM_SINGLE("Line In Switch", MAX9850_ENABLE, 1, 1, 0), 68 }; 69 70 static const struct snd_soc_dapm_widget max9850_dapm_widgets[] = { 71 SND_SOC_DAPM_SUPPLY("Charge Pump 1", MAX9850_ENABLE, 4, 0, NULL, 0), 72 SND_SOC_DAPM_SUPPLY("Charge Pump 2", MAX9850_ENABLE, 5, 0, NULL, 0), 73 SND_SOC_DAPM_SUPPLY("MCLK", MAX9850_ENABLE, 6, 0, NULL, 0), 74 SND_SOC_DAPM_SUPPLY("SHDN", MAX9850_ENABLE, 7, 0, NULL, 0), 75 SND_SOC_DAPM_MIXER_NAMED_CTL("Output Mixer", MAX9850_ENABLE, 2, 0, 76 &max9850_mixer_controls[0], 77 ARRAY_SIZE(max9850_mixer_controls)), 78 SND_SOC_DAPM_PGA("Headphone Output", MAX9850_ENABLE, 3, 0, NULL, 0), 79 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", MAX9850_ENABLE, 0, 0), 80 SND_SOC_DAPM_OUTPUT("OUTL"), 81 SND_SOC_DAPM_OUTPUT("HPL"), 82 SND_SOC_DAPM_OUTPUT("OUTR"), 83 SND_SOC_DAPM_OUTPUT("HPR"), 84 SND_SOC_DAPM_MIXER("Line Input", SND_SOC_NOPM, 0, 0, NULL, 0), 85 SND_SOC_DAPM_INPUT("INL"), 86 SND_SOC_DAPM_INPUT("INR"), 87 }; 88 89 static const struct snd_soc_dapm_route intercon[] = { 90 /* output mixer */ 91 {"Output Mixer", NULL, "DAC"}, 92 {"Output Mixer", "Line In Switch", "Line Input"}, 93 94 /* outputs */ 95 {"Headphone Output", NULL, "Output Mixer"}, 96 {"HPL", NULL, "Headphone Output"}, 97 {"HPR", NULL, "Headphone Output"}, 98 {"OUTL", NULL, "Output Mixer"}, 99 {"OUTR", NULL, "Output Mixer"}, 100 101 /* inputs */ 102 {"Line Input", NULL, "INL"}, 103 {"Line Input", NULL, "INR"}, 104 105 /* supplies */ 106 {"Output Mixer", NULL, "Charge Pump 1"}, 107 {"Output Mixer", NULL, "Charge Pump 2"}, 108 {"Output Mixer", NULL, "SHDN"}, 109 {"DAC", NULL, "MCLK"}, 110 }; 111 112 static int max9850_hw_params(struct snd_pcm_substream *substream, 113 struct snd_pcm_hw_params *params, 114 struct snd_soc_dai *dai) 115 { 116 struct snd_soc_codec *codec = dai->codec; 117 struct max9850_priv *max9850 = snd_soc_codec_get_drvdata(codec); 118 u64 lrclk_div; 119 u8 sf, da; 120 121 if (!max9850->sysclk) 122 return -EINVAL; 123 124 /* lrclk_div = 2^22 * rate / iclk with iclk = mclk / sf */ 125 sf = (snd_soc_read(codec, MAX9850_CLOCK) >> 2) + 1; 126 lrclk_div = (1 << 22); 127 lrclk_div *= params_rate(params); 128 lrclk_div *= sf; 129 do_div(lrclk_div, max9850->sysclk); 130 131 snd_soc_write(codec, MAX9850_LRCLK_MSB, (lrclk_div >> 8) & 0x7f); 132 snd_soc_write(codec, MAX9850_LRCLK_LSB, lrclk_div & 0xff); 133 134 switch (params_format(params)) { 135 case SNDRV_PCM_FORMAT_S16_LE: 136 da = 0; 137 break; 138 case SNDRV_PCM_FORMAT_S20_3LE: 139 da = 0x2; 140 break; 141 case SNDRV_PCM_FORMAT_S24_LE: 142 da = 0x3; 143 break; 144 default: 145 return -EINVAL; 146 } 147 snd_soc_update_bits(codec, MAX9850_DIGITAL_AUDIO, 0x3, da); 148 149 return 0; 150 } 151 152 static int max9850_set_dai_sysclk(struct snd_soc_dai *codec_dai, 153 int clk_id, unsigned int freq, int dir) 154 { 155 struct snd_soc_codec *codec = codec_dai->codec; 156 struct max9850_priv *max9850 = snd_soc_codec_get_drvdata(codec); 157 158 /* calculate mclk -> iclk divider */ 159 if (freq <= 13000000) 160 snd_soc_write(codec, MAX9850_CLOCK, 0x0); 161 else if (freq <= 26000000) 162 snd_soc_write(codec, MAX9850_CLOCK, 0x4); 163 else if (freq <= 40000000) 164 snd_soc_write(codec, MAX9850_CLOCK, 0x8); 165 else 166 return -EINVAL; 167 168 max9850->sysclk = freq; 169 return 0; 170 } 171 172 static int max9850_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt) 173 { 174 struct snd_soc_codec *codec = codec_dai->codec; 175 u8 da = 0; 176 177 /* set master/slave audio interface */ 178 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 179 case SND_SOC_DAIFMT_CBM_CFM: 180 da |= MAX9850_MASTER; 181 break; 182 case SND_SOC_DAIFMT_CBS_CFS: 183 break; 184 default: 185 return -EINVAL; 186 } 187 188 /* interface format */ 189 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 190 case SND_SOC_DAIFMT_I2S: 191 da |= MAX9850_DLY; 192 break; 193 case SND_SOC_DAIFMT_RIGHT_J: 194 da |= MAX9850_RTJ; 195 break; 196 case SND_SOC_DAIFMT_LEFT_J: 197 break; 198 default: 199 return -EINVAL; 200 } 201 202 /* clock inversion */ 203 switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 204 case SND_SOC_DAIFMT_NB_NF: 205 break; 206 case SND_SOC_DAIFMT_IB_IF: 207 da |= MAX9850_BCINV | MAX9850_INV; 208 break; 209 case SND_SOC_DAIFMT_IB_NF: 210 da |= MAX9850_BCINV; 211 break; 212 case SND_SOC_DAIFMT_NB_IF: 213 da |= MAX9850_INV; 214 break; 215 default: 216 return -EINVAL; 217 } 218 219 /* set da */ 220 snd_soc_write(codec, MAX9850_DIGITAL_AUDIO, da); 221 222 return 0; 223 } 224 225 static int max9850_set_bias_level(struct snd_soc_codec *codec, 226 enum snd_soc_bias_level level) 227 { 228 int ret; 229 230 switch (level) { 231 case SND_SOC_BIAS_ON: 232 break; 233 case SND_SOC_BIAS_PREPARE: 234 break; 235 case SND_SOC_BIAS_STANDBY: 236 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) { 237 ret = snd_soc_cache_sync(codec); 238 if (ret) { 239 dev_err(codec->dev, 240 "Failed to sync cache: %d\n", ret); 241 return ret; 242 } 243 } 244 break; 245 case SND_SOC_BIAS_OFF: 246 break; 247 } 248 codec->dapm.bias_level = level; 249 return 0; 250 } 251 252 #define MAX9850_RATES SNDRV_PCM_RATE_8000_48000 253 254 #define MAX9850_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\ 255 SNDRV_PCM_FMTBIT_S24_LE) 256 257 static struct snd_soc_dai_ops max9850_dai_ops = { 258 .hw_params = max9850_hw_params, 259 .set_sysclk = max9850_set_dai_sysclk, 260 .set_fmt = max9850_set_dai_fmt, 261 }; 262 263 static struct snd_soc_dai_driver max9850_dai = { 264 .name = "max9850-hifi", 265 .playback = { 266 .stream_name = "Playback", 267 .channels_min = 1, 268 .channels_max = 2, 269 .rates = MAX9850_RATES, 270 .formats = MAX9850_FORMATS 271 }, 272 .ops = &max9850_dai_ops, 273 }; 274 275 #ifdef CONFIG_PM 276 static int max9850_suspend(struct snd_soc_codec *codec, pm_message_t state) 277 { 278 max9850_set_bias_level(codec, SND_SOC_BIAS_OFF); 279 280 return 0; 281 } 282 283 static int max9850_resume(struct snd_soc_codec *codec) 284 { 285 max9850_set_bias_level(codec, SND_SOC_BIAS_STANDBY); 286 287 return 0; 288 } 289 #else 290 #define max9850_suspend NULL 291 #define max9850_resume NULL 292 #endif 293 294 static int max9850_probe(struct snd_soc_codec *codec) 295 { 296 struct snd_soc_dapm_context *dapm = &codec->dapm; 297 int ret; 298 299 ret = snd_soc_codec_set_cache_io(codec, 8, 8, SND_SOC_I2C); 300 if (ret < 0) { 301 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret); 302 return ret; 303 } 304 305 /* enable zero-detect */ 306 snd_soc_update_bits(codec, MAX9850_GENERAL_PURPOSE, 1, 1); 307 /* enable slew-rate control */ 308 snd_soc_update_bits(codec, MAX9850_VOLUME, 0x40, 0x40); 309 /* set slew-rate 125ms */ 310 snd_soc_update_bits(codec, MAX9850_CHARGE_PUMP, 0xff, 0xc0); 311 312 snd_soc_dapm_new_controls(dapm, max9850_dapm_widgets, 313 ARRAY_SIZE(max9850_dapm_widgets)); 314 snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon)); 315 316 snd_soc_add_controls(codec, max9850_controls, 317 ARRAY_SIZE(max9850_controls)); 318 319 return 0; 320 } 321 322 static struct snd_soc_codec_driver soc_codec_dev_max9850 = { 323 .probe = max9850_probe, 324 .suspend = max9850_suspend, 325 .resume = max9850_resume, 326 .set_bias_level = max9850_set_bias_level, 327 .reg_cache_size = ARRAY_SIZE(max9850_reg), 328 .reg_word_size = sizeof(u8), 329 .reg_cache_default = max9850_reg, 330 .volatile_register = max9850_volatile_register, 331 }; 332 333 static int __devinit max9850_i2c_probe(struct i2c_client *i2c, 334 const struct i2c_device_id *id) 335 { 336 struct max9850_priv *max9850; 337 int ret; 338 339 max9850 = kzalloc(sizeof(struct max9850_priv), GFP_KERNEL); 340 if (max9850 == NULL) 341 return -ENOMEM; 342 343 i2c_set_clientdata(i2c, max9850); 344 345 ret = snd_soc_register_codec(&i2c->dev, 346 &soc_codec_dev_max9850, &max9850_dai, 1); 347 if (ret < 0) 348 kfree(max9850); 349 return ret; 350 } 351 352 static __devexit int max9850_i2c_remove(struct i2c_client *client) 353 { 354 snd_soc_unregister_codec(&client->dev); 355 kfree(i2c_get_clientdata(client)); 356 return 0; 357 } 358 359 static const struct i2c_device_id max9850_i2c_id[] = { 360 { "max9850", 0 }, 361 { } 362 }; 363 MODULE_DEVICE_TABLE(i2c, max9850_i2c_id); 364 365 static struct i2c_driver max9850_i2c_driver = { 366 .driver = { 367 .name = "max9850", 368 .owner = THIS_MODULE, 369 }, 370 .probe = max9850_i2c_probe, 371 .remove = __devexit_p(max9850_i2c_remove), 372 .id_table = max9850_i2c_id, 373 }; 374 375 static int __init max9850_init(void) 376 { 377 return i2c_add_driver(&max9850_i2c_driver); 378 } 379 module_init(max9850_init); 380 381 static void __exit max9850_exit(void) 382 { 383 i2c_del_driver(&max9850_i2c_driver); 384 } 385 module_exit(max9850_exit); 386 387 MODULE_AUTHOR("Christian Glindkamp <christian.glindkamp@taskit.de>"); 388 MODULE_DESCRIPTION("ASoC MAX9850 codec driver"); 389 MODULE_LICENSE("GPL"); 390