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/regmap.h> 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 { 31 struct regmap *regmap; 32 unsigned int sysclk; 33 }; 34 35 /* max9850 register cache */ 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 }, 46 }; 47 48 /* these registers are not used at the moment but provided for the sake of 49 * completeness */ 50 static bool max9850_volatile_register(struct device *dev, unsigned int reg) 51 { 52 switch (reg) { 53 case MAX9850_STATUSA: 54 case MAX9850_STATUSB: 55 return true; 56 default: 57 return false; 58 } 59 } 60 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 70 static const DECLARE_TLV_DB_RANGE(max9850_tlv, 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), 74 0x38, 0x3f, TLV_DB_SCALE_ITEM(250, 50, 0) 75 ); 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 106 static const struct snd_soc_dapm_route max9850_dapm_routes[] = { 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_component *component = dai->component; 134 struct max9850_priv *max9850 = snd_soc_component_get_drvdata(component); 135 u64 lrclk_div; 136 u8 sf, da; 137 138 if (!max9850->sysclk) 139 return -EINVAL; 140 141 /* lrclk_div = 2^22 * rate / iclk with iclk = mclk / sf */ 142 sf = (snd_soc_component_read32(component, 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_component_write(component, MAX9850_LRCLK_MSB, (lrclk_div >> 8) & 0x7f); 149 snd_soc_component_write(component, MAX9850_LRCLK_LSB, lrclk_div & 0xff); 150 151 switch (params_width(params)) { 152 case 16: 153 da = 0; 154 break; 155 case 20: 156 da = 0x2; 157 break; 158 case 24: 159 da = 0x3; 160 break; 161 default: 162 return -EINVAL; 163 } 164 snd_soc_component_update_bits(component, 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_component *component = codec_dai->component; 173 struct max9850_priv *max9850 = snd_soc_component_get_drvdata(component); 174 175 /* calculate mclk -> iclk divider */ 176 if (freq <= 13000000) 177 snd_soc_component_write(component, MAX9850_CLOCK, 0x0); 178 else if (freq <= 26000000) 179 snd_soc_component_write(component, MAX9850_CLOCK, 0x4); 180 else if (freq <= 40000000) 181 snd_soc_component_write(component, 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_component *component = codec_dai->component; 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_component_write(component, MAX9850_DIGITAL_AUDIO, da); 238 239 return 0; 240 } 241 242 static int max9850_set_bias_level(struct snd_soc_component *component, 243 enum snd_soc_bias_level level) 244 { 245 struct max9850_priv *max9850 = snd_soc_component_get_drvdata(component); 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: 254 if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) { 255 ret = regcache_sync(max9850->regmap); 256 if (ret) { 257 dev_err(component->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 } 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 274 static const struct snd_soc_dai_ops max9850_dai_ops = { 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 292 static int max9850_probe(struct snd_soc_component *component) 293 { 294 /* enable zero-detect */ 295 snd_soc_component_update_bits(component, MAX9850_GENERAL_PURPOSE, 1, 1); 296 /* enable slew-rate control */ 297 snd_soc_component_update_bits(component, MAX9850_VOLUME, 0x40, 0x40); 298 /* set slew-rate 125ms */ 299 snd_soc_component_update_bits(component, MAX9850_CHARGE_PUMP, 0xff, 0xc0); 300 301 return 0; 302 } 303 304 static const struct snd_soc_component_driver soc_component_dev_max9850 = { 305 .probe = max9850_probe, 306 .set_bias_level = max9850_set_bias_level, 307 .controls = max9850_controls, 308 .num_controls = ARRAY_SIZE(max9850_controls), 309 .dapm_widgets = max9850_dapm_widgets, 310 .num_dapm_widgets = ARRAY_SIZE(max9850_dapm_widgets), 311 .dapm_routes = max9850_dapm_routes, 312 .num_dapm_routes = ARRAY_SIZE(max9850_dapm_routes), 313 .suspend_bias_off = 1, 314 .idle_bias_on = 1, 315 .use_pmdown_time = 1, 316 .endianness = 1, 317 .non_legacy_dai_naming = 1, 318 }; 319 320 static int max9850_i2c_probe(struct i2c_client *i2c, 321 const struct i2c_device_id *id) 322 { 323 struct max9850_priv *max9850; 324 int ret; 325 326 max9850 = devm_kzalloc(&i2c->dev, sizeof(struct max9850_priv), 327 GFP_KERNEL); 328 if (max9850 == NULL) 329 return -ENOMEM; 330 331 max9850->regmap = devm_regmap_init_i2c(i2c, &max9850_regmap); 332 if (IS_ERR(max9850->regmap)) 333 return PTR_ERR(max9850->regmap); 334 335 i2c_set_clientdata(i2c, max9850); 336 337 ret = devm_snd_soc_register_component(&i2c->dev, 338 &soc_component_dev_max9850, &max9850_dai, 1); 339 return ret; 340 } 341 342 static const struct i2c_device_id max9850_i2c_id[] = { 343 { "max9850", 0 }, 344 { } 345 }; 346 MODULE_DEVICE_TABLE(i2c, max9850_i2c_id); 347 348 static struct i2c_driver max9850_i2c_driver = { 349 .driver = { 350 .name = "max9850", 351 }, 352 .probe = max9850_i2c_probe, 353 .id_table = max9850_i2c_id, 354 }; 355 356 module_i2c_driver(max9850_i2c_driver); 357 358 MODULE_AUTHOR("Christian Glindkamp <christian.glindkamp@taskit.de>"); 359 MODULE_DESCRIPTION("ASoC MAX9850 codec driver"); 360 MODULE_LICENSE("GPL"); 361