Janusz Krzysztofik | 459dc35 | 2009-07-22 05:22:28 +0200 | [diff] [blame] | 1 | /* |
| 2 | * cx20442.c -- CX20442 ALSA Soc Audio driver |
| 3 | * |
| 4 | * Copyright 2009 Janusz Krzysztofik <jkrzyszt@tis.icnet.pl> |
| 5 | * |
| 6 | * Initially based on sound/soc/codecs/wm8400.c |
| 7 | * Copyright 2008, 2009 Wolfson Microelectronics PLC. |
| 8 | * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify it |
| 11 | * under the terms of the GNU General Public License as published by the |
| 12 | * Free Software Foundation; either version 2 of the License, or (at your |
| 13 | * option) any later version. |
| 14 | */ |
| 15 | |
| 16 | #include <sound/core.h> |
| 17 | #include <sound/initval.h> |
| 18 | #include <sound/soc-dapm.h> |
| 19 | |
| 20 | #include "cx20442.h" |
| 21 | |
| 22 | |
| 23 | struct cx20442_priv { |
| 24 | struct snd_soc_codec codec; |
| 25 | u8 reg_cache[1]; |
| 26 | }; |
| 27 | |
| 28 | #define CX20442_PM 0x0 |
| 29 | |
| 30 | #define CX20442_TELIN 0 |
| 31 | #define CX20442_TELOUT 1 |
| 32 | #define CX20442_MIC 2 |
| 33 | #define CX20442_SPKOUT 3 |
| 34 | #define CX20442_AGC 4 |
| 35 | |
| 36 | static const struct snd_soc_dapm_widget cx20442_dapm_widgets[] = { |
| 37 | SND_SOC_DAPM_OUTPUT("TELOUT"), |
| 38 | SND_SOC_DAPM_OUTPUT("SPKOUT"), |
| 39 | SND_SOC_DAPM_OUTPUT("AGCOUT"), |
| 40 | |
| 41 | SND_SOC_DAPM_MIXER("SPKOUT Mixer", SND_SOC_NOPM, 0, 0, NULL, 0), |
| 42 | |
| 43 | SND_SOC_DAPM_PGA("TELOUT Amp", CX20442_PM, CX20442_TELOUT, 0, NULL, 0), |
| 44 | SND_SOC_DAPM_PGA("SPKOUT Amp", CX20442_PM, CX20442_SPKOUT, 0, NULL, 0), |
| 45 | SND_SOC_DAPM_PGA("SPKOUT AGC", CX20442_PM, CX20442_AGC, 0, NULL, 0), |
| 46 | |
| 47 | SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0), |
| 48 | SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0), |
| 49 | |
| 50 | SND_SOC_DAPM_MIXER("Input Mixer", SND_SOC_NOPM, 0, 0, NULL, 0), |
| 51 | |
| 52 | SND_SOC_DAPM_MICBIAS("TELIN Bias", CX20442_PM, CX20442_TELIN, 0), |
| 53 | SND_SOC_DAPM_MICBIAS("MIC Bias", CX20442_PM, CX20442_MIC, 0), |
| 54 | |
| 55 | SND_SOC_DAPM_PGA("MIC AGC", CX20442_PM, CX20442_AGC, 0, NULL, 0), |
| 56 | |
| 57 | SND_SOC_DAPM_INPUT("TELIN"), |
| 58 | SND_SOC_DAPM_INPUT("MIC"), |
| 59 | SND_SOC_DAPM_INPUT("AGCIN"), |
| 60 | }; |
| 61 | |
| 62 | static const struct snd_soc_dapm_route cx20442_audio_map[] = { |
| 63 | {"TELOUT", NULL, "TELOUT Amp"}, |
| 64 | |
| 65 | {"SPKOUT", NULL, "SPKOUT Mixer"}, |
| 66 | {"SPKOUT Mixer", NULL, "SPKOUT Amp"}, |
| 67 | |
| 68 | {"TELOUT Amp", NULL, "DAC"}, |
| 69 | {"SPKOUT Amp", NULL, "DAC"}, |
| 70 | |
| 71 | {"SPKOUT Mixer", NULL, "SPKOUT AGC"}, |
| 72 | {"SPKOUT AGC", NULL, "AGCIN"}, |
| 73 | |
| 74 | {"AGCOUT", NULL, "MIC AGC"}, |
| 75 | {"MIC AGC", NULL, "MIC"}, |
| 76 | |
| 77 | {"MIC Bias", NULL, "MIC"}, |
| 78 | {"Input Mixer", NULL, "MIC Bias"}, |
| 79 | |
| 80 | {"TELIN Bias", NULL, "TELIN"}, |
| 81 | {"Input Mixer", NULL, "TELIN Bias"}, |
| 82 | |
| 83 | {"ADC", NULL, "Input Mixer"}, |
| 84 | }; |
| 85 | |
| 86 | static int cx20442_add_widgets(struct snd_soc_codec *codec) |
| 87 | { |
| 88 | snd_soc_dapm_new_controls(codec, cx20442_dapm_widgets, |
| 89 | ARRAY_SIZE(cx20442_dapm_widgets)); |
| 90 | |
| 91 | snd_soc_dapm_add_routes(codec, cx20442_audio_map, |
| 92 | ARRAY_SIZE(cx20442_audio_map)); |
| 93 | |
| 94 | snd_soc_dapm_new_widgets(codec); |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | static unsigned int cx20442_read_reg_cache(struct snd_soc_codec *codec, |
| 99 | unsigned int reg) |
| 100 | { |
| 101 | u8 *reg_cache = codec->reg_cache; |
| 102 | |
| 103 | if (reg >= codec->reg_cache_size) |
| 104 | return -EINVAL; |
| 105 | |
| 106 | return reg_cache[reg]; |
| 107 | } |
| 108 | |
| 109 | enum v253_vls { |
| 110 | V253_VLS_NONE = 0, |
| 111 | V253_VLS_T, |
| 112 | V253_VLS_L, |
| 113 | V253_VLS_LT, |
| 114 | V253_VLS_S, |
| 115 | V253_VLS_ST, |
| 116 | V253_VLS_M, |
| 117 | V253_VLS_MST, |
| 118 | V253_VLS_S1, |
| 119 | V253_VLS_S1T, |
| 120 | V253_VLS_MS1T, |
| 121 | V253_VLS_M1, |
| 122 | V253_VLS_M1ST, |
| 123 | V253_VLS_M1S1T, |
| 124 | V253_VLS_H, |
| 125 | V253_VLS_HT, |
| 126 | V253_VLS_MS, |
| 127 | V253_VLS_MS1, |
| 128 | V253_VLS_M1S, |
| 129 | V253_VLS_M1S1, |
| 130 | V253_VLS_TEST, |
| 131 | }; |
| 132 | |
| 133 | static int cx20442_pm_to_v253_vls(u8 value) |
| 134 | { |
Janusz Krzysztofik | b84eab0 | 2009-07-28 20:24:12 +0200 | [diff] [blame^] | 135 | switch (value & ~(1 << CX20442_AGC)) { |
Janusz Krzysztofik | 459dc35 | 2009-07-22 05:22:28 +0200 | [diff] [blame] | 136 | case 0: |
| 137 | return V253_VLS_T; |
| 138 | case (1 << CX20442_SPKOUT): |
| 139 | return V253_VLS_S1; |
| 140 | case (1 << CX20442_MIC): |
| 141 | return V253_VLS_M1; |
| 142 | case (1 << CX20442_SPKOUT) | (1 << CX20442_MIC): |
| 143 | return V253_VLS_M1S1; |
| 144 | case (1 << CX20442_TELOUT): |
| 145 | case (1 << CX20442_TELIN): |
| 146 | case (1 << CX20442_TELOUT) | (1 << CX20442_TELIN): |
| 147 | return V253_VLS_L; |
| 148 | case (1 << CX20442_TELOUT) | (1 << CX20442_MIC): |
| 149 | return V253_VLS_NONE; |
| 150 | } |
| 151 | return -EINVAL; |
| 152 | } |
| 153 | static int cx20442_pm_to_v253_vsp(u8 value) |
| 154 | { |
Janusz Krzysztofik | b84eab0 | 2009-07-28 20:24:12 +0200 | [diff] [blame^] | 155 | switch (value & ~(1 << CX20442_AGC)) { |
Janusz Krzysztofik | 459dc35 | 2009-07-22 05:22:28 +0200 | [diff] [blame] | 156 | case (1 << CX20442_SPKOUT): |
| 157 | case (1 << CX20442_MIC): |
| 158 | case (1 << CX20442_SPKOUT) | (1 << CX20442_MIC): |
| 159 | return (bool)(value & (1 << CX20442_AGC)); |
| 160 | } |
| 161 | return (value & (1 << CX20442_AGC)) ? -EINVAL : 0; |
| 162 | } |
| 163 | |
| 164 | static int cx20442_write(struct snd_soc_codec *codec, unsigned int reg, |
| 165 | unsigned int value) |
| 166 | { |
| 167 | u8 *reg_cache = codec->reg_cache; |
| 168 | int vls, vsp, old, len; |
| 169 | char buf[18]; |
| 170 | |
| 171 | if (reg >= codec->reg_cache_size) |
| 172 | return -EINVAL; |
| 173 | |
Janusz Krzysztofik | b84eab0 | 2009-07-28 20:24:12 +0200 | [diff] [blame^] | 174 | /* hw_write and control_data pointers required for talking to the modem |
| 175 | * are expected to be set by the machine driver's line discipline |
| 176 | * initialization code */ |
Janusz Krzysztofik | 459dc35 | 2009-07-22 05:22:28 +0200 | [diff] [blame] | 177 | if (!codec->hw_write || !codec->control_data) |
| 178 | return -EIO; |
| 179 | |
| 180 | old = reg_cache[reg]; |
| 181 | reg_cache[reg] = value; |
| 182 | |
| 183 | vls = cx20442_pm_to_v253_vls(value); |
| 184 | if (vls < 0) |
| 185 | return vls; |
| 186 | |
| 187 | vsp = cx20442_pm_to_v253_vsp(value); |
Janusz Krzysztofik | b84eab0 | 2009-07-28 20:24:12 +0200 | [diff] [blame^] | 188 | if (vsp < 0) |
Janusz Krzysztofik | 459dc35 | 2009-07-22 05:22:28 +0200 | [diff] [blame] | 189 | return vsp; |
| 190 | |
| 191 | if ((vls == V253_VLS_T) || |
| 192 | (vls == cx20442_pm_to_v253_vls(old))) { |
| 193 | if (vsp == cx20442_pm_to_v253_vsp(old)) |
| 194 | return 0; |
| 195 | len = snprintf(buf, ARRAY_SIZE(buf), "at+vsp=%d\r", vsp); |
| 196 | } else if (vsp == cx20442_pm_to_v253_vsp(old)) |
| 197 | len = snprintf(buf, ARRAY_SIZE(buf), "at+vls=%d\r", vls); |
| 198 | else |
| 199 | len = snprintf(buf, ARRAY_SIZE(buf), |
| 200 | "at+vls=%d;+vsp=%d\r", vls, vsp); |
| 201 | |
| 202 | if (unlikely(len > (ARRAY_SIZE(buf) - 1))) |
| 203 | return -ENOMEM; |
| 204 | |
| 205 | if (codec->hw_write(codec->control_data, buf, len) != len) |
| 206 | return -EIO; |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | struct snd_soc_dai cx20442_dai = { |
| 212 | .name = "CX20442", |
| 213 | .playback = { |
| 214 | .stream_name = "Playback", |
| 215 | .channels_min = 1, |
| 216 | .channels_max = 1, |
| 217 | .rates = SNDRV_PCM_RATE_8000, |
| 218 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 219 | }, |
| 220 | .capture = { |
| 221 | .stream_name = "Capture", |
| 222 | .channels_min = 1, |
| 223 | .channels_max = 1, |
| 224 | .rates = SNDRV_PCM_RATE_8000, |
| 225 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 226 | }, |
| 227 | }; |
| 228 | EXPORT_SYMBOL_GPL(cx20442_dai); |
| 229 | |
| 230 | static struct snd_soc_codec *cx20442_codec; |
| 231 | |
| 232 | static int cx20442_codec_probe(struct platform_device *pdev) |
| 233 | { |
| 234 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 235 | struct snd_soc_codec *codec; |
| 236 | int ret; |
| 237 | |
Janusz Krzysztofik | b84eab0 | 2009-07-28 20:24:12 +0200 | [diff] [blame^] | 238 | if (!cx20442_codec) { |
Janusz Krzysztofik | 459dc35 | 2009-07-22 05:22:28 +0200 | [diff] [blame] | 239 | dev_err(&pdev->dev, "cx20442 not yet discovered\n"); |
| 240 | return -ENODEV; |
| 241 | } |
| 242 | codec = cx20442_codec; |
| 243 | |
| 244 | socdev->card->codec = codec; |
| 245 | |
| 246 | /* register pcms */ |
| 247 | ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); |
| 248 | if (ret < 0) { |
| 249 | dev_err(&pdev->dev, "failed to create pcms\n"); |
| 250 | goto pcm_err; |
| 251 | } |
| 252 | |
| 253 | cx20442_add_widgets(codec); |
| 254 | |
| 255 | ret = snd_soc_init_card(socdev); |
| 256 | if (ret < 0) { |
| 257 | dev_err(&pdev->dev, "failed to register card\n"); |
| 258 | goto card_err; |
| 259 | } |
| 260 | |
| 261 | return ret; |
| 262 | |
| 263 | card_err: |
| 264 | snd_soc_free_pcms(socdev); |
| 265 | snd_soc_dapm_free(socdev); |
| 266 | pcm_err: |
| 267 | return ret; |
| 268 | } |
| 269 | |
| 270 | /* power down chip */ |
| 271 | static int cx20442_codec_remove(struct platform_device *pdev) |
| 272 | { |
| 273 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 274 | |
| 275 | snd_soc_free_pcms(socdev); |
| 276 | snd_soc_dapm_free(socdev); |
| 277 | |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | struct snd_soc_codec_device cx20442_codec_dev = { |
| 282 | .probe = cx20442_codec_probe, |
| 283 | .remove = cx20442_codec_remove, |
| 284 | }; |
| 285 | EXPORT_SYMBOL_GPL(cx20442_codec_dev); |
| 286 | |
| 287 | static int cx20442_register(struct cx20442_priv *cx20442) |
| 288 | { |
| 289 | struct snd_soc_codec *codec = &cx20442->codec; |
| 290 | int ret; |
| 291 | |
| 292 | mutex_init(&codec->mutex); |
| 293 | INIT_LIST_HEAD(&codec->dapm_widgets); |
| 294 | INIT_LIST_HEAD(&codec->dapm_paths); |
| 295 | |
| 296 | codec->name = "CX20442"; |
| 297 | codec->owner = THIS_MODULE; |
| 298 | codec->private_data = cx20442; |
| 299 | |
| 300 | codec->dai = &cx20442_dai; |
| 301 | codec->num_dai = 1; |
| 302 | |
| 303 | codec->reg_cache = &cx20442->reg_cache; |
| 304 | codec->reg_cache_size = ARRAY_SIZE(cx20442->reg_cache); |
| 305 | codec->read = cx20442_read_reg_cache; |
| 306 | codec->write = cx20442_write; |
| 307 | |
| 308 | codec->bias_level = SND_SOC_BIAS_OFF; |
| 309 | |
| 310 | cx20442_dai.dev = codec->dev; |
| 311 | |
| 312 | cx20442_codec = codec; |
| 313 | |
| 314 | ret = snd_soc_register_codec(codec); |
| 315 | if (ret != 0) { |
Janusz Krzysztofik | b84eab0 | 2009-07-28 20:24:12 +0200 | [diff] [blame^] | 316 | dev_err(&codec->dev, "Failed to register codec: %d\n", ret); |
Janusz Krzysztofik | 459dc35 | 2009-07-22 05:22:28 +0200 | [diff] [blame] | 317 | goto err; |
| 318 | } |
| 319 | |
| 320 | ret = snd_soc_register_dai(&cx20442_dai); |
| 321 | if (ret != 0) { |
Janusz Krzysztofik | b84eab0 | 2009-07-28 20:24:12 +0200 | [diff] [blame^] | 322 | dev_err(&codec->dev, "Failed to register DAI: %d\n", ret); |
Janusz Krzysztofik | 459dc35 | 2009-07-22 05:22:28 +0200 | [diff] [blame] | 323 | goto err_codec; |
| 324 | } |
| 325 | |
| 326 | return 0; |
| 327 | |
| 328 | err_codec: |
| 329 | snd_soc_unregister_codec(codec); |
| 330 | err: |
| 331 | cx20442_codec = NULL; |
| 332 | kfree(cx20442); |
| 333 | return ret; |
| 334 | } |
| 335 | |
| 336 | static void cx20442_unregister(struct cx20442_priv *cx20442) |
| 337 | { |
| 338 | snd_soc_unregister_dai(&cx20442_dai); |
| 339 | snd_soc_unregister_codec(&cx20442->codec); |
| 340 | |
| 341 | cx20442_codec = NULL; |
| 342 | kfree(cx20442); |
| 343 | } |
| 344 | |
| 345 | static int cx20442_platform_probe(struct platform_device *pdev) |
| 346 | { |
| 347 | struct cx20442_priv *cx20442; |
| 348 | struct snd_soc_codec *codec; |
| 349 | |
| 350 | cx20442 = kzalloc(sizeof(struct cx20442_priv), GFP_KERNEL); |
| 351 | if (cx20442 == NULL) |
| 352 | return -ENOMEM; |
| 353 | |
| 354 | codec = &cx20442->codec; |
| 355 | |
| 356 | codec->control_data = NULL; |
| 357 | codec->hw_write = NULL; |
| 358 | codec->pop_time = 0; |
| 359 | |
| 360 | codec->dev = &pdev->dev; |
| 361 | platform_set_drvdata(pdev, cx20442); |
| 362 | |
| 363 | return cx20442_register(cx20442); |
| 364 | } |
| 365 | |
| 366 | static int __exit cx20442_platform_remove(struct platform_device *pdev) |
| 367 | { |
| 368 | struct cx20442_priv *cx20442 = platform_get_drvdata(pdev); |
| 369 | |
| 370 | cx20442_unregister(cx20442); |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | static struct platform_driver cx20442_platform_driver = { |
| 375 | .driver = { |
| 376 | .name = "cx20442", |
| 377 | .owner = THIS_MODULE, |
| 378 | }, |
| 379 | .probe = cx20442_platform_probe, |
| 380 | .remove = __exit_p(cx20442_platform_remove), |
| 381 | }; |
| 382 | |
| 383 | static int __init cx20442_init(void) |
| 384 | { |
| 385 | return platform_driver_register(&cx20442_platform_driver); |
| 386 | } |
| 387 | module_init(cx20442_init); |
| 388 | |
| 389 | static void __exit cx20442_exit(void) |
| 390 | { |
| 391 | platform_driver_unregister(&cx20442_platform_driver); |
| 392 | } |
| 393 | module_exit(cx20442_exit); |
| 394 | |
| 395 | MODULE_DESCRIPTION("ASoC CX20442-11 voice modem codec driver"); |
| 396 | MODULE_AUTHOR("Janusz Krzysztofik"); |
| 397 | MODULE_LICENSE("GPL"); |
Janusz Krzysztofik | b84eab0 | 2009-07-28 20:24:12 +0200 | [diff] [blame^] | 398 | MODULE_ALIAS("platform:cx20442"); |