aboutsummaryrefslogtreecommitdiffstats
path: root/sound/soc/codecs/wm8741.c
diff options
context:
space:
mode:
Diffstat (limited to 'sound/soc/codecs/wm8741.c')
-rw-r--r--sound/soc/codecs/wm8741.c378
1 files changed, 179 insertions, 199 deletions
diff --git a/sound/soc/codecs/wm8741.c b/sound/soc/codecs/wm8741.c
index b9ea8904ad4b..90e31e9aa6f7 100644
--- a/sound/soc/codecs/wm8741.c
+++ b/sound/soc/codecs/wm8741.c
@@ -30,25 +30,21 @@
30 30
31#include "wm8741.h" 31#include "wm8741.h"
32 32
33static struct snd_soc_codec *wm8741_codec;
34struct snd_soc_codec_device soc_codec_dev_wm8741;
35
36#define WM8741_NUM_SUPPLIES 2 33#define WM8741_NUM_SUPPLIES 2
37static const char *wm8741_supply_names[WM8741_NUM_SUPPLIES] = { 34static const char *wm8741_supply_names[WM8741_NUM_SUPPLIES] = {
38 "AVDD", 35 "AVDD",
39 "DVDD", 36 "DVDD",
40}; 37};
41 38
42#define WM8741_NUM_RATES 4 39#define WM8741_NUM_RATES 6
43 40
44/* codec private data */ 41/* codec private data */
45struct wm8741_priv { 42struct wm8741_priv {
46 struct snd_soc_codec codec; 43 enum snd_soc_control_type control_type;
47 u16 reg_cache[WM8741_REGISTER_COUNT]; 44 u16 reg_cache[WM8741_REGISTER_COUNT];
48 struct regulator_bulk_data supplies[WM8741_NUM_SUPPLIES]; 45 struct regulator_bulk_data supplies[WM8741_NUM_SUPPLIES];
49 unsigned int sysclk; 46 unsigned int sysclk;
50 unsigned int rate_constraint_list[WM8741_NUM_RATES]; 47 struct snd_pcm_hw_constraint_list *sysclk_constraints;
51 struct snd_pcm_hw_constraint_list rate_constraint;
52}; 48};
53 49
54static const u16 wm8741_reg_defaults[WM8741_REGISTER_COUNT] = { 50static const u16 wm8741_reg_defaults[WM8741_REGISTER_COUNT] = {
@@ -111,10 +107,84 @@ static struct {
111 int value; 107 int value;
112 int ratio; 108 int ratio;
113} lrclk_ratios[WM8741_NUM_RATES] = { 109} lrclk_ratios[WM8741_NUM_RATES] = {
114 { 1, 256 }, 110 { 1, 128 },
115 { 2, 384 }, 111 { 2, 192 },
116 { 3, 512 }, 112 { 3, 256 },
117 { 4, 768 }, 113 { 4, 384 },
114 { 5, 512 },
115 { 6, 768 },
116};
117
118static unsigned int rates_11289[] = {
119 44100, 88235,
120};
121
122static struct snd_pcm_hw_constraint_list constraints_11289 = {
123 .count = ARRAY_SIZE(rates_11289),
124 .list = rates_11289,
125};
126
127static unsigned int rates_12288[] = {
128 32000, 48000, 96000,
129};
130
131static struct snd_pcm_hw_constraint_list constraints_12288 = {
132 .count = ARRAY_SIZE(rates_12288),
133 .list = rates_12288,
134};
135
136static unsigned int rates_16384[] = {
137 32000,
138};
139
140static struct snd_pcm_hw_constraint_list constraints_16384 = {
141 .count = ARRAY_SIZE(rates_16384),
142 .list = rates_16384,
143};
144
145static unsigned int rates_16934[] = {
146 44100, 88235,
147};
148
149static struct snd_pcm_hw_constraint_list constraints_16934 = {
150 .count = ARRAY_SIZE(rates_16934),
151 .list = rates_16934,
152};
153
154static unsigned int rates_18432[] = {
155 48000, 96000,
156};
157
158static struct snd_pcm_hw_constraint_list constraints_18432 = {
159 .count = ARRAY_SIZE(rates_18432),
160 .list = rates_18432,
161};
162
163static unsigned int rates_22579[] = {
164 44100, 88235, 1764000
165};
166
167static struct snd_pcm_hw_constraint_list constraints_22579 = {
168 .count = ARRAY_SIZE(rates_22579),
169 .list = rates_22579,
170};
171
172static unsigned int rates_24576[] = {
173 32000, 48000, 96000, 192000
174};
175
176static struct snd_pcm_hw_constraint_list constraints_24576 = {
177 .count = ARRAY_SIZE(rates_24576),
178 .list = rates_24576,
179};
180
181static unsigned int rates_36864[] = {
182 48000, 96000, 19200
183};
184
185static struct snd_pcm_hw_constraint_list constraints_36864 = {
186 .count = ARRAY_SIZE(rates_36864),
187 .list = rates_36864,
118}; 188};
119 189
120 190
@@ -135,7 +205,7 @@ static int wm8741_startup(struct snd_pcm_substream *substream,
135 205
136 snd_pcm_hw_constraint_list(substream->runtime, 0, 206 snd_pcm_hw_constraint_list(substream->runtime, 0,
137 SNDRV_PCM_HW_PARAM_RATE, 207 SNDRV_PCM_HW_PARAM_RATE,
138 &wm8741->rate_constraint); 208 wm8741->sysclk_constraints);
139 209
140 return 0; 210 return 0;
141} 211}
@@ -145,8 +215,7 @@ static int wm8741_hw_params(struct snd_pcm_substream *substream,
145 struct snd_soc_dai *dai) 215 struct snd_soc_dai *dai)
146{ 216{
147 struct snd_soc_pcm_runtime *rtd = substream->private_data; 217 struct snd_soc_pcm_runtime *rtd = substream->private_data;
148 struct snd_soc_device *socdev = rtd->socdev; 218 struct snd_soc_codec *codec = rtd->codec;
149 struct snd_soc_codec *codec = socdev->card->codec;
150 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec); 219 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
151 u16 iface = snd_soc_read(codec, WM8741_FORMAT_CONTROL) & 0x1FC; 220 u16 iface = snd_soc_read(codec, WM8741_FORMAT_CONTROL) & 0x1FC;
152 int i; 221 int i;
@@ -196,47 +265,52 @@ static int wm8741_set_dai_sysclk(struct snd_soc_dai *codec_dai,
196{ 265{
197 struct snd_soc_codec *codec = codec_dai->codec; 266 struct snd_soc_codec *codec = codec_dai->codec;
198 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec); 267 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
199 unsigned int val;
200 int i;
201 268
202 dev_dbg(codec->dev, "wm8741_set_dai_sysclk info: freq=%dHz\n", freq); 269 dev_dbg(codec->dev, "wm8741_set_dai_sysclk info: freq=%dHz\n", freq);
203 270
204 wm8741->sysclk = freq; 271 switch (freq) {
205 272 case 11289600:
206 wm8741->rate_constraint.count = 0; 273 wm8741->sysclk_constraints = &constraints_11289;
207 274 wm8741->sysclk = freq;
208 for (i = 0; i < ARRAY_SIZE(lrclk_ratios); i++) { 275 return 0;
209 dev_dbg(codec->dev, "index = %d, ratio = %d, freq = %d", 276
210 i, lrclk_ratios[i].ratio, freq); 277 case 12288000:
211 278 wm8741->sysclk_constraints = &constraints_12288;
212 val = freq / lrclk_ratios[i].ratio; 279 wm8741->sysclk = freq;
213 /* Check that it's a standard rate since core can't 280 return 0;
214 * cope with others and having the odd rates confuses 281
215 * constraint matching. 282 case 16384000:
216 */ 283 wm8741->sysclk_constraints = &constraints_16384;
217 switch (val) { 284 wm8741->sysclk = freq;
218 case 32000: 285 return 0;
219 case 44100: 286
220 case 48000: 287 case 16934400:
221 case 64000: 288 wm8741->sysclk_constraints = &constraints_16934;
222 case 88200: 289 wm8741->sysclk = freq;
223 case 96000: 290 return 0;
224 dev_dbg(codec->dev, "Supported sample rate: %dHz\n", 291
225 val); 292 case 18432000:
226 wm8741->rate_constraint_list[i] = val; 293 wm8741->sysclk_constraints = &constraints_18432;
227 wm8741->rate_constraint.count++; 294 wm8741->sysclk = freq;
228 break; 295 return 0;
229 default: 296
230 dev_dbg(codec->dev, "Skipping sample rate: %dHz\n", 297 case 22579200:
231 val); 298 case 33868800:
232 } 299 wm8741->sysclk_constraints = &constraints_22579;
300 wm8741->sysclk = freq;
301 return 0;
302
303 case 24576000:
304 wm8741->sysclk_constraints = &constraints_24576;
305 wm8741->sysclk = freq;
306 return 0;
307
308 case 36864000:
309 wm8741->sysclk_constraints = &constraints_36864;
310 wm8741->sysclk = freq;
311 return 0;
233 } 312 }
234 313 return -EINVAL;
235 /* Need at least one supported rate... */
236 if (wm8741->rate_constraint.count == 0)
237 return -EINVAL;
238
239 return 0;
240} 314}
241 315
242static int wm8741_set_dai_fmt(struct snd_soc_dai *codec_dai, 316static int wm8741_set_dai_fmt(struct snd_soc_dai *codec_dai,
@@ -314,8 +388,8 @@ static struct snd_soc_dai_ops wm8741_dai_ops = {
314 .set_fmt = wm8741_set_dai_fmt, 388 .set_fmt = wm8741_set_dai_fmt,
315}; 389};
316 390
317struct snd_soc_dai wm8741_dai = { 391static struct snd_soc_dai_driver wm8741_dai = {
318 .name = "WM8741", 392 .name = "wm8741",
319 .playback = { 393 .playback = {
320 .stream_name = "Playback", 394 .stream_name = "Playback",
321 .channels_min = 2, /* Mono modes not yet supported */ 395 .channels_min = 2, /* Mono modes not yet supported */
@@ -325,13 +399,10 @@ struct snd_soc_dai wm8741_dai = {
325 }, 399 },
326 .ops = &wm8741_dai_ops, 400 .ops = &wm8741_dai_ops,
327}; 401};
328EXPORT_SYMBOL_GPL(wm8741_dai);
329 402
330#ifdef CONFIG_PM 403#ifdef CONFIG_PM
331static int wm8741_resume(struct platform_device *pdev) 404static int wm8741_resume(struct snd_soc_codec *codec)
332{ 405{
333 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
334 struct snd_soc_codec *codec = socdev->card->codec;
335 u16 *cache = codec->reg_cache; 406 u16 *cache = codec->reg_cache;
336 int i; 407 int i;
337 408
@@ -348,189 +419,99 @@ static int wm8741_resume(struct platform_device *pdev)
348#define wm8741_resume NULL 419#define wm8741_resume NULL
349#endif 420#endif
350 421
351static int wm8741_probe(struct platform_device *pdev) 422static int wm8741_probe(struct snd_soc_codec *codec)
352{ 423{
353 struct snd_soc_device *socdev = platform_get_drvdata(pdev); 424 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
354 struct snd_soc_codec *codec;
355 int ret = 0; 425 int ret = 0;
356 426
357 if (wm8741_codec == NULL) { 427 ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8741->control_type);
358 dev_err(&pdev->dev, "Codec device not registered\n"); 428 if (ret != 0) {
359 return -ENODEV; 429 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
430 return ret;
360 } 431 }
361 432
362 socdev->card->codec = wm8741_codec; 433 ret = wm8741_reset(codec);
363 codec = wm8741_codec;
364
365 /* register pcms */
366 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
367 if (ret < 0) { 434 if (ret < 0) {
368 dev_err(codec->dev, "failed to create pcms: %d\n", ret); 435 dev_err(codec->dev, "Failed to issue reset\n");
369 goto pcm_err; 436 return ret;
370 } 437 }
371 438
439 /* Change some default settings - latch VU */
440 wm8741->reg_cache[WM8741_DACLLSB_ATTENUATION] |= WM8741_UPDATELL;
441 wm8741->reg_cache[WM8741_DACLMSB_ATTENUATION] |= WM8741_UPDATELM;
442 wm8741->reg_cache[WM8741_DACRLSB_ATTENUATION] |= WM8741_UPDATERL;
443 wm8741->reg_cache[WM8741_DACRLSB_ATTENUATION] |= WM8741_UPDATERM;
444
372 snd_soc_add_controls(codec, wm8741_snd_controls, 445 snd_soc_add_controls(codec, wm8741_snd_controls,
373 ARRAY_SIZE(wm8741_snd_controls)); 446 ARRAY_SIZE(wm8741_snd_controls));
374 wm8741_add_widgets(codec); 447 wm8741_add_widgets(codec);
375 448
376 return ret; 449 dev_dbg(codec->dev, "Successful registration\n");
377
378pcm_err:
379 return ret; 450 return ret;
380} 451}
381 452
382static int wm8741_remove(struct platform_device *pdev) 453static struct snd_soc_codec_driver soc_codec_dev_wm8741 = {
383{
384 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
385
386 snd_soc_free_pcms(socdev);
387 snd_soc_dapm_free(socdev);
388
389 return 0;
390}
391
392struct snd_soc_codec_device soc_codec_dev_wm8741 = {
393 .probe = wm8741_probe, 454 .probe = wm8741_probe,
394 .remove = wm8741_remove,
395 .resume = wm8741_resume, 455 .resume = wm8741_resume,
456 .reg_cache_size = ARRAY_SIZE(wm8741_reg_defaults),
457 .reg_word_size = sizeof(u16),
458 .reg_cache_default = &wm8741_reg_defaults,
396}; 459};
397EXPORT_SYMBOL_GPL(soc_codec_dev_wm8741);
398 460
399static int wm8741_register(struct wm8741_priv *wm8741, 461#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
400 enum snd_soc_control_type control) 462static int wm8741_i2c_probe(struct i2c_client *i2c,
463 const struct i2c_device_id *id)
401{ 464{
402 int ret; 465 struct wm8741_priv *wm8741;
403 struct snd_soc_codec *codec = &wm8741->codec; 466 int ret, i;
404 int i;
405
406 if (wm8741_codec) {
407 dev_err(codec->dev, "Another WM8741 is registered\n");
408 return -EINVAL;
409 }
410
411 mutex_init(&codec->mutex);
412 INIT_LIST_HEAD(&codec->dapm_widgets);
413 INIT_LIST_HEAD(&codec->dapm_paths);
414
415 snd_soc_codec_set_drvdata(codec, wm8741);
416 codec->name = "WM8741";
417 codec->owner = THIS_MODULE;
418 codec->bias_level = SND_SOC_BIAS_OFF;
419 codec->set_bias_level = NULL;
420 codec->dai = &wm8741_dai;
421 codec->num_dai = 1;
422 codec->reg_cache_size = WM8741_REGISTER_COUNT;
423 codec->reg_cache = &wm8741->reg_cache;
424
425 wm8741->rate_constraint.list = &wm8741->rate_constraint_list[0];
426 wm8741->rate_constraint.count =
427 ARRAY_SIZE(wm8741->rate_constraint_list);
428
429 memcpy(codec->reg_cache, wm8741_reg_defaults,
430 sizeof(wm8741->reg_cache));
431 467
432 ret = snd_soc_codec_set_cache_io(codec, 7, 9, control); 468 wm8741 = kzalloc(sizeof(struct wm8741_priv), GFP_KERNEL);
433 if (ret != 0) { 469 if (wm8741 == NULL)
434 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret); 470 return -ENOMEM;
435 goto err;
436 }
437 471
438 for (i = 0; i < ARRAY_SIZE(wm8741->supplies); i++) 472 for (i = 0; i < ARRAY_SIZE(wm8741->supplies); i++)
439 wm8741->supplies[i].supply = wm8741_supply_names[i]; 473 wm8741->supplies[i].supply = wm8741_supply_names[i];
440 474
441 ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8741->supplies), 475 ret = regulator_bulk_get(&i2c->dev, ARRAY_SIZE(wm8741->supplies),
442 wm8741->supplies); 476 wm8741->supplies);
443 if (ret != 0) { 477 if (ret != 0) {
444 dev_err(codec->dev, "Failed to request supplies: %d\n", ret); 478 dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
445 goto err; 479 goto err;
446 } 480 }
447 481
448 ret = regulator_bulk_enable(ARRAY_SIZE(wm8741->supplies), 482 ret = regulator_bulk_enable(ARRAY_SIZE(wm8741->supplies),
449 wm8741->supplies); 483 wm8741->supplies);
450 if (ret != 0) { 484 if (ret != 0) {
451 dev_err(codec->dev, "Failed to enable supplies: %d\n", ret); 485 dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret);
452 goto err_get; 486 goto err_get;
453 } 487 }
454 488
455 ret = wm8741_reset(codec); 489 i2c_set_clientdata(i2c, wm8741);
456 if (ret < 0) { 490 wm8741->control_type = SND_SOC_I2C;
457 dev_err(codec->dev, "Failed to issue reset\n");
458 goto err_enable;
459 }
460
461 wm8741_dai.dev = codec->dev;
462
463 /* Change some default settings - latch VU */
464 wm8741->reg_cache[WM8741_DACLLSB_ATTENUATION] |= WM8741_UPDATELL;
465 wm8741->reg_cache[WM8741_DACLMSB_ATTENUATION] |= WM8741_UPDATELM;
466 wm8741->reg_cache[WM8741_DACRLSB_ATTENUATION] |= WM8741_UPDATERL;
467 wm8741->reg_cache[WM8741_DACRLSB_ATTENUATION] |= WM8741_UPDATERM;
468
469 wm8741_codec = codec;
470
471 ret = snd_soc_register_codec(codec);
472 if (ret != 0) {
473 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
474 return ret;
475 }
476
477 ret = snd_soc_register_dai(&wm8741_dai);
478 if (ret != 0) {
479 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
480 snd_soc_unregister_codec(codec);
481 return ret;
482 }
483 491
484 dev_dbg(codec->dev, "Successful registration\n"); 492 ret = snd_soc_register_codec(&i2c->dev,
485 return 0; 493 &soc_codec_dev_wm8741, &wm8741_dai, 1);
494 if (ret < 0)
495 goto err_enable;
496 return ret;
486 497
487err_enable: 498err_enable:
488 regulator_bulk_disable(ARRAY_SIZE(wm8741->supplies), wm8741->supplies); 499 regulator_bulk_disable(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
489 500
490err_get: 501err_get:
491 regulator_bulk_free(ARRAY_SIZE(wm8741->supplies), wm8741->supplies); 502 regulator_bulk_free(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
492
493err: 503err:
494 kfree(wm8741); 504 kfree(wm8741);
495 return ret; 505 return ret;
496} 506}
497 507
498static void wm8741_unregister(struct wm8741_priv *wm8741) 508static int wm8741_i2c_remove(struct i2c_client *client)
499{
500 regulator_bulk_free(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
501
502 snd_soc_unregister_dai(&wm8741_dai);
503 snd_soc_unregister_codec(&wm8741->codec);
504 kfree(wm8741);
505 wm8741_codec = NULL;
506}
507
508#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
509static __devinit int wm8741_i2c_probe(struct i2c_client *i2c,
510 const struct i2c_device_id *id)
511{
512 struct wm8741_priv *wm8741;
513 struct snd_soc_codec *codec;
514
515 wm8741 = kzalloc(sizeof(struct wm8741_priv), GFP_KERNEL);
516 if (wm8741 == NULL)
517 return -ENOMEM;
518
519 codec = &wm8741->codec;
520 codec->hw_write = (hw_write_t)i2c_master_send;
521
522 i2c_set_clientdata(i2c, wm8741);
523 codec->control_data = i2c;
524
525 codec->dev = &i2c->dev;
526
527 return wm8741_register(wm8741, SND_SOC_I2C);
528}
529
530static __devexit int wm8741_i2c_remove(struct i2c_client *client)
531{ 509{
532 struct wm8741_priv *wm8741 = i2c_get_clientdata(client); 510 struct wm8741_priv *wm8741 = i2c_get_clientdata(client);
533 wm8741_unregister(wm8741); 511
512 snd_soc_unregister_codec(&client->dev);
513 regulator_bulk_free(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
514 kfree(i2c_get_clientdata(client));
534 return 0; 515 return 0;
535} 516}
536 517
@@ -540,29 +521,28 @@ static const struct i2c_device_id wm8741_i2c_id[] = {
540}; 521};
541MODULE_DEVICE_TABLE(i2c, wm8741_i2c_id); 522MODULE_DEVICE_TABLE(i2c, wm8741_i2c_id);
542 523
543
544static struct i2c_driver wm8741_i2c_driver = { 524static struct i2c_driver wm8741_i2c_driver = {
545 .driver = { 525 .driver = {
546 .name = "WM8741", 526 .name = "wm8741-codec",
547 .owner = THIS_MODULE, 527 .owner = THIS_MODULE,
548 }, 528 },
549 .probe = wm8741_i2c_probe, 529 .probe = wm8741_i2c_probe,
550 .remove = __devexit_p(wm8741_i2c_remove), 530 .remove = wm8741_i2c_remove,
551 .id_table = wm8741_i2c_id, 531 .id_table = wm8741_i2c_id,
552}; 532};
553#endif 533#endif
554 534
555static int __init wm8741_modinit(void) 535static int __init wm8741_modinit(void)
556{ 536{
557 int ret; 537 int ret = 0;
538
558#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 539#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
559 ret = i2c_add_driver(&wm8741_i2c_driver); 540 ret = i2c_add_driver(&wm8741_i2c_driver);
560 if (ret != 0) { 541 if (ret != 0)
561 printk(KERN_ERR "Failed to register WM8741 I2C driver: %d\n", 542 pr_err("Failed to register WM8741 I2C driver: %d\n", ret);
562 ret);
563 }
564#endif 543#endif
565 return 0; 544
545 return ret;
566} 546}
567module_init(wm8741_modinit); 547module_init(wm8741_modinit);
568 548