Tim Howe | e40da86 | 2015-07-16 14:51:40 -0500 | [diff] [blame] | 1 | /* |
| 2 | * cs4349.c -- CS4349 ALSA Soc Audio driver |
| 3 | * |
| 4 | * Copyright 2015 Cirrus Logic, Inc. |
| 5 | * |
| 6 | * Authors: Tim Howe <Tim.Howe@cirrus.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/moduleparam.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/gpio.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/pm.h> |
| 21 | #include <linux/i2c.h> |
| 22 | #include <linux/of_device.h> |
| 23 | #include <linux/regmap.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <sound/core.h> |
| 26 | #include <sound/pcm.h> |
| 27 | #include <sound/pcm_params.h> |
| 28 | #include <sound/soc.h> |
| 29 | #include <sound/soc-dapm.h> |
| 30 | #include <sound/initval.h> |
| 31 | #include <sound/tlv.h> |
| 32 | #include "cs4349.h" |
| 33 | |
| 34 | |
| 35 | static const struct reg_default cs4349_reg_defaults[] = { |
| 36 | { 2, 0x00 }, /* r02 - Mode Control */ |
| 37 | { 3, 0x09 }, /* r03 - Volume, Mixing and Inversion Control */ |
| 38 | { 4, 0x81 }, /* r04 - Mute Control */ |
| 39 | { 5, 0x00 }, /* r05 - Channel A Volume Control */ |
| 40 | { 6, 0x00 }, /* r06 - Channel B Volume Control */ |
| 41 | { 7, 0xB1 }, /* r07 - Ramp and Filter Control */ |
| 42 | { 8, 0x1C }, /* r08 - Misc. Control */ |
| 43 | }; |
| 44 | |
| 45 | /* Private data for the CS4349 */ |
| 46 | struct cs4349_private { |
| 47 | struct regmap *regmap; |
| 48 | struct cs4349_platform_data pdata; |
| 49 | struct gpio_desc *reset_gpio; |
| 50 | unsigned int mode; |
| 51 | int rate; |
| 52 | }; |
| 53 | |
| 54 | static bool cs4349_readable_register(struct device *dev, unsigned int reg) |
| 55 | { |
| 56 | switch (reg) { |
Axel Lin | 0443de7 | 2015-07-19 09:14:23 +0800 | [diff] [blame^] | 57 | case CS4349_CHIPID ... CS4349_MISC: |
| 58 | return true; |
| 59 | default: |
| 60 | return false; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | static bool cs4349_writeable_register(struct device *dev, unsigned int reg) |
| 65 | { |
| 66 | switch (reg) { |
| 67 | case CS4349_MODE ... CS4349_MISC: |
Tim Howe | e40da86 | 2015-07-16 14:51:40 -0500 | [diff] [blame] | 68 | return true; |
| 69 | default: |
| 70 | return false; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | static int cs4349_set_dai_fmt(struct snd_soc_dai *codec_dai, |
| 75 | unsigned int format) |
| 76 | { |
| 77 | struct snd_soc_codec *codec = codec_dai->codec; |
| 78 | struct cs4349_private *cs4349 = snd_soc_codec_get_drvdata(codec); |
| 79 | unsigned int fmt; |
| 80 | |
| 81 | fmt = format & SND_SOC_DAIFMT_FORMAT_MASK; |
| 82 | |
| 83 | switch (fmt) { |
| 84 | case SND_SOC_DAIFMT_I2S: |
| 85 | case SND_SOC_DAIFMT_LEFT_J: |
| 86 | case SND_SOC_DAIFMT_RIGHT_J: |
| 87 | cs4349->mode = format & SND_SOC_DAIFMT_FORMAT_MASK; |
| 88 | break; |
| 89 | default: |
| 90 | return -EINVAL; |
| 91 | } |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | static int cs4349_pcm_hw_params(struct snd_pcm_substream *substream, |
| 97 | struct snd_pcm_hw_params *params, |
| 98 | struct snd_soc_dai *dai) |
| 99 | { |
Lars-Peter Clausen | dedae86 | 2015-07-19 12:15:18 +0200 | [diff] [blame] | 100 | struct snd_soc_codec *codec = dai->codec; |
Tim Howe | e40da86 | 2015-07-16 14:51:40 -0500 | [diff] [blame] | 101 | struct cs4349_private *cs4349 = snd_soc_codec_get_drvdata(codec); |
Axel Lin | da304ac | 2015-07-19 22:42:49 +0800 | [diff] [blame] | 102 | int fmt, ret; |
Tim Howe | e40da86 | 2015-07-16 14:51:40 -0500 | [diff] [blame] | 103 | |
Tim Howe | e40da86 | 2015-07-16 14:51:40 -0500 | [diff] [blame] | 104 | cs4349->rate = params_rate(params); |
| 105 | |
| 106 | switch (cs4349->mode) { |
| 107 | case SND_SOC_DAIFMT_I2S: |
Axel Lin | da304ac | 2015-07-19 22:42:49 +0800 | [diff] [blame] | 108 | fmt = DIF_I2S; |
Tim Howe | e40da86 | 2015-07-16 14:51:40 -0500 | [diff] [blame] | 109 | break; |
| 110 | case SND_SOC_DAIFMT_LEFT_J: |
Axel Lin | da304ac | 2015-07-19 22:42:49 +0800 | [diff] [blame] | 111 | fmt = DIF_LEFT_JST; |
Tim Howe | e40da86 | 2015-07-16 14:51:40 -0500 | [diff] [blame] | 112 | break; |
| 113 | case SND_SOC_DAIFMT_RIGHT_J: |
| 114 | switch (params_width(params)) { |
| 115 | case 16: |
| 116 | fmt = DIF_RGHT_JST16; |
| 117 | break; |
| 118 | case 24: |
| 119 | fmt = DIF_RGHT_JST24; |
| 120 | break; |
| 121 | default: |
| 122 | return -EINVAL; |
| 123 | } |
Tim Howe | e40da86 | 2015-07-16 14:51:40 -0500 | [diff] [blame] | 124 | break; |
| 125 | default: |
| 126 | return -EINVAL; |
| 127 | } |
| 128 | |
Axel Lin | da304ac | 2015-07-19 22:42:49 +0800 | [diff] [blame] | 129 | ret = snd_soc_update_bits(codec, CS4349_MODE, DIF_MASK, |
| 130 | MODE_FORMAT(fmt)); |
Tim Howe | e40da86 | 2015-07-16 14:51:40 -0500 | [diff] [blame] | 131 | if (ret < 0) |
| 132 | return ret; |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static int cs4349_digital_mute(struct snd_soc_dai *dai, int mute) |
| 138 | { |
| 139 | struct snd_soc_codec *codec = dai->codec; |
| 140 | int reg; |
| 141 | |
| 142 | reg = 0; |
| 143 | if (mute) |
| 144 | reg = MUTE_AB_MASK; |
| 145 | |
| 146 | return snd_soc_update_bits(codec, CS4349_MUTE, MUTE_AB_MASK, reg); |
| 147 | } |
| 148 | |
| 149 | static DECLARE_TLV_DB_SCALE(dig_tlv, -12750, 50, 0); |
| 150 | |
| 151 | static const char * const chan_mix_texts[] = { |
| 152 | "Mute", "MuteA", "MuteA SwapB", "MuteA MonoB", "SwapA MuteB", |
| 153 | "BothR", "Swap", "SwapA MonoB", "MuteB", "Normal", "BothL", |
| 154 | "MonoB", "MonoA MuteB", "MonoA", "MonoA SwapB", "Mono", |
| 155 | /*Normal == Channel A = Left, Channel B = Right*/ |
| 156 | }; |
| 157 | |
| 158 | static const char * const fm_texts[] = { |
| 159 | "Auto", "Single", "Double", "Quad", |
| 160 | }; |
| 161 | |
| 162 | static const char * const deemph_texts[] = { |
| 163 | "None", "44.1k", "48k", "32k", |
| 164 | }; |
| 165 | |
| 166 | static const char * const softr_zeroc_texts[] = { |
| 167 | "Immediate", "Zero Cross", "Soft Ramp", "SR on ZC", |
| 168 | }; |
| 169 | |
| 170 | static int deemph_values[] = { |
| 171 | 0, 4, 8, 12, |
| 172 | }; |
| 173 | |
| 174 | static int softr_zeroc_values[] = { |
| 175 | 0, 64, 128, 192, |
| 176 | }; |
| 177 | |
| 178 | static const struct soc_enum chan_mix_enum = |
| 179 | SOC_ENUM_SINGLE(CS4349_VMI, 0, |
| 180 | ARRAY_SIZE(chan_mix_texts), |
| 181 | chan_mix_texts); |
| 182 | |
| 183 | static const struct soc_enum fm_mode_enum = |
| 184 | SOC_ENUM_SINGLE(CS4349_MODE, 0, |
| 185 | ARRAY_SIZE(fm_texts), |
| 186 | fm_texts); |
| 187 | |
| 188 | static SOC_VALUE_ENUM_SINGLE_DECL(deemph_enum, CS4349_MODE, 0, DEM_MASK, |
| 189 | deemph_texts, deemph_values); |
| 190 | |
| 191 | static SOC_VALUE_ENUM_SINGLE_DECL(softr_zeroc_enum, CS4349_RMPFLT, 0, |
| 192 | SR_ZC_MASK, softr_zeroc_texts, |
| 193 | softr_zeroc_values); |
| 194 | |
| 195 | static const struct snd_kcontrol_new cs4349_snd_controls[] = { |
| 196 | SOC_DOUBLE_R_TLV("Master Playback Volume", |
| 197 | CS4349_VOLA, CS4349_VOLB, 0, 0xFF, 1, dig_tlv), |
| 198 | SOC_ENUM("Functional Mode", fm_mode_enum), |
| 199 | SOC_ENUM("De-Emphasis Control", deemph_enum), |
| 200 | SOC_ENUM("Soft Ramp Zero Cross Control", softr_zeroc_enum), |
| 201 | SOC_ENUM("Channel Mixer", chan_mix_enum), |
| 202 | SOC_SINGLE("VolA = VolB Switch", CS4349_VMI, 7, 1, 0), |
| 203 | SOC_SINGLE("InvertA Switch", CS4349_VMI, 6, 1, 0), |
| 204 | SOC_SINGLE("InvertB Switch", CS4349_VMI, 5, 1, 0), |
| 205 | SOC_SINGLE("Auto-Mute Switch", CS4349_MUTE, 7, 1, 0), |
| 206 | SOC_SINGLE("MUTEC A = B Switch", CS4349_MUTE, 5, 1, 0), |
| 207 | SOC_SINGLE("Soft Ramp Up Switch", CS4349_RMPFLT, 5, 1, 0), |
| 208 | SOC_SINGLE("Soft Ramp Down Switch", CS4349_RMPFLT, 4, 1, 0), |
| 209 | SOC_SINGLE("Slow Roll Off Filter Switch", CS4349_RMPFLT, 2, 1, 0), |
| 210 | SOC_SINGLE("Freeze Switch", CS4349_MISC, 5, 1, 0), |
| 211 | SOC_SINGLE("Popguard Switch", CS4349_MISC, 4, 1, 0), |
| 212 | }; |
| 213 | |
| 214 | static const struct snd_soc_dapm_widget cs4349_dapm_widgets[] = { |
| 215 | SND_SOC_DAPM_DAC("HiFi DAC", NULL, SND_SOC_NOPM, 0, 0), |
| 216 | |
| 217 | SND_SOC_DAPM_OUTPUT("OutputA"), |
| 218 | SND_SOC_DAPM_OUTPUT("OutputB"), |
| 219 | }; |
| 220 | |
| 221 | static const struct snd_soc_dapm_route cs4349_routes[] = { |
| 222 | {"DAC Playback", NULL, "OutputA"}, |
| 223 | {"DAC Playback", NULL, "OutputB"}, |
| 224 | |
| 225 | {"OutputA", NULL, "HiFi DAC"}, |
| 226 | {"OutputB", NULL, "HiFi DAC"}, |
| 227 | }; |
| 228 | |
| 229 | #define CS4349_PCM_FORMATS (SNDRV_PCM_FMTBIT_S8 | \ |
| 230 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \ |
| 231 | SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \ |
| 232 | SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \ |
| 233 | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \ |
| 234 | SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE | \ |
| 235 | SNDRV_PCM_FMTBIT_S32_LE) |
| 236 | |
| 237 | #define CS4349_PCM_RATES SNDRV_PCM_RATE_8000_192000 |
| 238 | |
| 239 | static const struct snd_soc_dai_ops cs4349_dai_ops = { |
| 240 | .hw_params = cs4349_pcm_hw_params, |
| 241 | .set_fmt = cs4349_set_dai_fmt, |
| 242 | .digital_mute = cs4349_digital_mute, |
| 243 | }; |
| 244 | |
| 245 | static struct snd_soc_dai_driver cs4349_dai = { |
| 246 | .name = "cs4349_hifi", |
| 247 | .playback = { |
| 248 | .stream_name = "DAC Playback", |
| 249 | .channels_min = 1, |
| 250 | .channels_max = 2, |
| 251 | .rates = CS4349_PCM_RATES, |
| 252 | .formats = CS4349_PCM_FORMATS, |
| 253 | }, |
| 254 | .ops = &cs4349_dai_ops, |
| 255 | .symmetric_rates = 1, |
| 256 | }; |
| 257 | |
| 258 | static struct snd_soc_codec_driver soc_codec_dev_cs4349 = { |
| 259 | .controls = cs4349_snd_controls, |
| 260 | .num_controls = ARRAY_SIZE(cs4349_snd_controls), |
| 261 | |
| 262 | .dapm_widgets = cs4349_dapm_widgets, |
| 263 | .num_dapm_widgets = ARRAY_SIZE(cs4349_dapm_widgets), |
| 264 | .dapm_routes = cs4349_routes, |
| 265 | .num_dapm_routes = ARRAY_SIZE(cs4349_routes), |
| 266 | }; |
| 267 | |
Axel Lin | b08b338 | 2015-07-17 23:43:02 +0800 | [diff] [blame] | 268 | static const struct regmap_config cs4349_regmap = { |
Tim Howe | e40da86 | 2015-07-16 14:51:40 -0500 | [diff] [blame] | 269 | .reg_bits = 8, |
| 270 | .val_bits = 8, |
| 271 | |
Axel Lin | dd9283e | 2015-07-17 23:38:34 +0800 | [diff] [blame] | 272 | .max_register = CS4349_MISC, |
Tim Howe | e40da86 | 2015-07-16 14:51:40 -0500 | [diff] [blame] | 273 | .reg_defaults = cs4349_reg_defaults, |
| 274 | .num_reg_defaults = ARRAY_SIZE(cs4349_reg_defaults), |
| 275 | .readable_reg = cs4349_readable_register, |
Axel Lin | 0443de7 | 2015-07-19 09:14:23 +0800 | [diff] [blame^] | 276 | .writeable_reg = cs4349_writeable_register, |
Tim Howe | e40da86 | 2015-07-16 14:51:40 -0500 | [diff] [blame] | 277 | .cache_type = REGCACHE_RBTREE, |
| 278 | }; |
| 279 | |
| 280 | static int cs4349_i2c_probe(struct i2c_client *client, |
| 281 | const struct i2c_device_id *id) |
| 282 | { |
| 283 | struct cs4349_private *cs4349; |
| 284 | struct cs4349_platform_data *pdata = dev_get_platdata(&client->dev); |
| 285 | int ret = 0; |
| 286 | |
| 287 | cs4349 = devm_kzalloc(&client->dev, sizeof(*cs4349), GFP_KERNEL); |
| 288 | if (!cs4349) |
| 289 | return -ENOMEM; |
| 290 | |
| 291 | cs4349->regmap = devm_regmap_init_i2c(client, &cs4349_regmap); |
| 292 | if (IS_ERR(cs4349->regmap)) { |
| 293 | ret = PTR_ERR(cs4349->regmap); |
| 294 | dev_err(&client->dev, "regmap_init() failed: %d\n", ret); |
| 295 | return ret; |
| 296 | } |
| 297 | |
| 298 | if (pdata) |
| 299 | cs4349->pdata = *pdata; |
| 300 | |
| 301 | /* Reset the Device */ |
| 302 | cs4349->reset_gpio = devm_gpiod_get_optional(&client->dev, |
| 303 | "reset", GPIOD_OUT_LOW); |
| 304 | if (IS_ERR(cs4349->reset_gpio)) |
| 305 | return PTR_ERR(cs4349->reset_gpio); |
| 306 | |
| 307 | if (cs4349->reset_gpio) |
| 308 | gpiod_set_value_cansleep(cs4349->reset_gpio, 1); |
| 309 | |
| 310 | i2c_set_clientdata(client, cs4349); |
| 311 | |
| 312 | return snd_soc_register_codec(&client->dev, &soc_codec_dev_cs4349, |
| 313 | &cs4349_dai, 1); |
| 314 | } |
| 315 | |
| 316 | static int cs4349_i2c_remove(struct i2c_client *client) |
| 317 | { |
| 318 | struct cs4349_private *cs4349 = i2c_get_clientdata(client); |
| 319 | |
| 320 | snd_soc_unregister_codec(&client->dev); |
| 321 | |
| 322 | /* Hold down reset */ |
| 323 | if (cs4349->reset_gpio) |
| 324 | gpiod_set_value_cansleep(cs4349->reset_gpio, 0); |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | #ifdef CONFIG_PM |
| 330 | static int cs4349_runtime_suspend(struct device *dev) |
| 331 | { |
| 332 | struct cs4349_private *cs4349 = dev_get_drvdata(dev); |
Tim Howe | e40da86 | 2015-07-16 14:51:40 -0500 | [diff] [blame] | 333 | int ret; |
| 334 | |
Lars-Peter Clausen | cb2510d | 2015-07-19 12:15:17 +0200 | [diff] [blame] | 335 | ret = regmap_update_bits(cs4349->regmap, CS4349_MISC, PWR_DWN, 1); |
Tim Howe | e40da86 | 2015-07-16 14:51:40 -0500 | [diff] [blame] | 336 | if (ret < 0) |
| 337 | return ret; |
| 338 | |
| 339 | regcache_cache_only(cs4349->regmap, true); |
| 340 | |
| 341 | /* Hold down reset */ |
| 342 | if (cs4349->reset_gpio) |
| 343 | gpiod_set_value_cansleep(cs4349->reset_gpio, 0); |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | static int cs4349_runtime_resume(struct device *dev) |
| 349 | { |
| 350 | struct cs4349_private *cs4349 = dev_get_drvdata(dev); |
Tim Howe | e40da86 | 2015-07-16 14:51:40 -0500 | [diff] [blame] | 351 | int ret; |
| 352 | |
Lars-Peter Clausen | cb2510d | 2015-07-19 12:15:17 +0200 | [diff] [blame] | 353 | ret = regmap_update_bits(cs4349->regmap, CS4349_MISC, PWR_DWN, 0); |
Tim Howe | e40da86 | 2015-07-16 14:51:40 -0500 | [diff] [blame] | 354 | if (ret < 0) |
| 355 | return ret; |
| 356 | |
| 357 | if (cs4349->reset_gpio) |
| 358 | gpiod_set_value_cansleep(cs4349->reset_gpio, 1); |
| 359 | |
| 360 | regcache_cache_only(cs4349->regmap, false); |
| 361 | regcache_sync(cs4349->regmap); |
| 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | #endif |
| 366 | |
| 367 | static const struct dev_pm_ops cs4349_runtime_pm = { |
| 368 | SET_RUNTIME_PM_OPS(cs4349_runtime_suspend, cs4349_runtime_resume, |
| 369 | NULL) |
| 370 | }; |
| 371 | |
| 372 | static const struct of_device_id cs4349_of_match[] = { |
| 373 | { .compatible = "cirrus,cs4349", }, |
| 374 | {}, |
| 375 | }; |
| 376 | |
| 377 | MODULE_DEVICE_TABLE(of, cs4349_of_match); |
| 378 | |
| 379 | static const struct i2c_device_id cs4349_i2c_id[] = { |
| 380 | {"cs4349", 0}, |
| 381 | {} |
| 382 | }; |
| 383 | |
| 384 | MODULE_DEVICE_TABLE(i2c, cs4349_i2c_id); |
| 385 | |
| 386 | static struct i2c_driver cs4349_i2c_driver = { |
| 387 | .driver = { |
| 388 | .name = "cs4349", |
| 389 | .owner = THIS_MODULE, |
| 390 | .of_match_table = cs4349_of_match, |
| 391 | }, |
| 392 | .id_table = cs4349_i2c_id, |
| 393 | .probe = cs4349_i2c_probe, |
| 394 | .remove = cs4349_i2c_remove, |
| 395 | }; |
| 396 | |
| 397 | module_i2c_driver(cs4349_i2c_driver); |
| 398 | |
| 399 | MODULE_AUTHOR("Tim Howe <tim.howe@cirrus.com>"); |
| 400 | MODULE_DESCRIPTION("Cirrus Logic CS4349 ALSA SoC Codec Driver"); |
| 401 | MODULE_LICENSE("GPL"); |