wm8731.c (a8035c8f04477895207b92915b908344749be336) wm8731.c (5998102b9095fdb7c67755812038612afea315c5)
1/*
2 * wm8731.c -- WM8731 ALSA SoC Audio driver
3 *
4 * Copyright 2005 Openedhand Ltd.
5 *
6 * Author: Richard Purdie <richard@openedhand.com>
7 *
8 * Based on wm8753.c by Liam Girdwood

--- 15 unchanged lines hidden (view full) ---

24#include <sound/pcm.h>
25#include <sound/pcm_params.h>
26#include <sound/soc.h>
27#include <sound/soc-dapm.h>
28#include <sound/initval.h>
29
30#include "wm8731.h"
31
1/*
2 * wm8731.c -- WM8731 ALSA SoC Audio driver
3 *
4 * Copyright 2005 Openedhand Ltd.
5 *
6 * Author: Richard Purdie <richard@openedhand.com>
7 *
8 * Based on wm8753.c by Liam Girdwood

--- 15 unchanged lines hidden (view full) ---

24#include <sound/pcm.h>
25#include <sound/pcm_params.h>
26#include <sound/soc.h>
27#include <sound/soc-dapm.h>
28#include <sound/initval.h>
29
30#include "wm8731.h"
31
32static struct snd_soc_codec *wm8731_codec;
32struct snd_soc_codec_device soc_codec_dev_wm8731;
33
34/* codec private data */
35struct wm8731_priv {
33struct snd_soc_codec_device soc_codec_dev_wm8731;
34
35/* codec private data */
36struct wm8731_priv {
37 struct snd_soc_codec codec;
38 u16 reg_cache[WM8731_CACHEREGNUM];
36 unsigned int sysclk;
37};
38
39#ifdef CONFIG_SPI_MASTER
40static int wm8731_spi_write(struct spi_device *spi, const char *data, int len);
39 unsigned int sysclk;
40};
41
42#ifdef CONFIG_SPI_MASTER
43static int wm8731_spi_write(struct spi_device *spi, const char *data, int len);
41static struct spi_driver wm8731_spi_driver;
42#endif
43
44/*
45 * wm8731 register cache
46 * We can't read the WM8731 register space when we are
47 * using 2 wire for device control, so we cache them instead.
48 * There is no point in caching the reset register
49 */

--- 430 unchanged lines hidden (view full) ---

480 data[1] = cache[i] & 0x00ff;
481 codec->hw_write(codec->control_data, data, 2);
482 }
483 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
484 wm8731_set_bias_level(codec, codec->suspend_bias_level);
485 return 0;
486}
487
44#endif
45
46/*
47 * wm8731 register cache
48 * We can't read the WM8731 register space when we are
49 * using 2 wire for device control, so we cache them instead.
50 * There is no point in caching the reset register
51 */

--- 430 unchanged lines hidden (view full) ---

482 data[1] = cache[i] & 0x00ff;
483 codec->hw_write(codec->control_data, data, 2);
484 }
485 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
486 wm8731_set_bias_level(codec, codec->suspend_bias_level);
487 return 0;
488}
489
488/*
489 * initialise the WM8731 driver
490 * register the mixer and dsp interfaces with the kernel
491 */
492static int wm8731_init(struct snd_soc_device *socdev)
490static int wm8731_probe(struct platform_device *pdev)
493{
491{
494 struct snd_soc_codec *codec = socdev->card->codec;
495 int reg, ret = 0;
492 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
493 struct snd_soc_codec *codec;
494 int ret = 0;
496
495
497 codec->name = "WM8731";
498 codec->owner = THIS_MODULE;
499 codec->read = wm8731_read_reg_cache;
500 codec->write = wm8731_write;
501 codec->set_bias_level = wm8731_set_bias_level;
502 codec->dai = &wm8731_dai;
503 codec->num_dai = 1;
504 codec->reg_cache_size = ARRAY_SIZE(wm8731_reg);
505 codec->reg_cache = kmemdup(wm8731_reg, sizeof(wm8731_reg), GFP_KERNEL);
506 if (codec->reg_cache == NULL)
507 return -ENOMEM;
496 if (wm8731_codec == NULL) {
497 dev_err(&pdev->dev, "Codec device not registered\n");
498 return -ENODEV;
499 }
508
500
509 wm8731_reset(codec);
501 socdev->card->codec = wm8731_codec;
502 codec = wm8731_codec;
510
511 /* register pcms */
512 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
513 if (ret < 0) {
503
504 /* register pcms */
505 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
506 if (ret < 0) {
514 printk(KERN_ERR "wm8731: failed to create pcms\n");
507 dev_err(codec->dev, "failed to create pcms: %d\n", ret);
515 goto pcm_err;
516 }
517
508 goto pcm_err;
509 }
510
518 /* power on device */
519 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
520
521 /* set the update bits */
522 reg = wm8731_read_reg_cache(codec, WM8731_LOUT1V);
523 wm8731_write(codec, WM8731_LOUT1V, reg & ~0x0100);
524 reg = wm8731_read_reg_cache(codec, WM8731_ROUT1V);
525 wm8731_write(codec, WM8731_ROUT1V, reg & ~0x0100);
526 reg = wm8731_read_reg_cache(codec, WM8731_LINVOL);
527 wm8731_write(codec, WM8731_LINVOL, reg & ~0x0100);
528 reg = wm8731_read_reg_cache(codec, WM8731_RINVOL);
529 wm8731_write(codec, WM8731_RINVOL, reg & ~0x0100);
530
531 snd_soc_add_controls(codec, wm8731_snd_controls,
511 snd_soc_add_controls(codec, wm8731_snd_controls,
532 ARRAY_SIZE(wm8731_snd_controls));
512 ARRAY_SIZE(wm8731_snd_controls));
533 wm8731_add_widgets(codec);
534 ret = snd_soc_init_card(socdev);
535 if (ret < 0) {
513 wm8731_add_widgets(codec);
514 ret = snd_soc_init_card(socdev);
515 if (ret < 0) {
536 printk(KERN_ERR "wm8731: failed to register card\n");
516 dev_err(codec->dev, "failed to register card: %d\n", ret);
537 goto card_err;
538 }
539
540 return ret;
541
542card_err:
543 snd_soc_free_pcms(socdev);
544 snd_soc_dapm_free(socdev);
545pcm_err:
517 goto card_err;
518 }
519
520 return ret;
521
522card_err:
523 snd_soc_free_pcms(socdev);
524 snd_soc_dapm_free(socdev);
525pcm_err:
546 kfree(codec->reg_cache);
547 return ret;
548}
549
526 return ret;
527}
528
550static struct snd_soc_device *wm8731_socdev;
551
552
553#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
554static struct i2c_driver wm8731_i2c_driver;
555
556static int wm8731_add_i2c_device(struct platform_device *pdev,
557 const struct wm8731_setup_data *setup)
558{
559 struct i2c_board_info info;
560 struct i2c_adapter *adapter;
561 struct i2c_client *client;
562 int ret;
563
564 ret = i2c_add_driver(&wm8731_i2c_driver);
565 if (ret != 0) {
566 dev_err(&pdev->dev, "can't add i2c driver\n");
567 return ret;
568 }
569
570 memset(&info, 0, sizeof(struct i2c_board_info));
571 info.addr = setup->i2c_address;
572 strlcpy(info.type, "wm8731", I2C_NAME_SIZE);
573
574 adapter = i2c_get_adapter(setup->i2c_bus);
575 if (!adapter) {
576 dev_err(&pdev->dev, "can't get i2c adapter %d\n",
577 setup->i2c_bus);
578 goto err_driver;
579 }
580
581 client = i2c_new_device(adapter, &info);
582 i2c_put_adapter(adapter);
583 if (!client) {
584 dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
585 (unsigned int)info.addr);
586 goto err_driver;
587 }
588
589 return 0;
590
591err_driver:
592 i2c_del_driver(&wm8731_i2c_driver);
593 return -ENODEV;
594}
595#endif
596
597static int wm8731_probe(struct platform_device *pdev)
598{
599 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
600 struct wm8731_setup_data *setup;
601 struct snd_soc_codec *codec;
602 struct wm8731_priv *wm8731;
603 int ret = 0;
604
605 setup = socdev->codec_data;
606 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
607 if (codec == NULL)
608 return -ENOMEM;
609
610 wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
611 if (wm8731 == NULL) {
612 kfree(codec);
613 return -ENOMEM;
614 }
615
616 codec->private_data = wm8731;
617 socdev->card->codec = codec;
618 mutex_init(&codec->mutex);
619 INIT_LIST_HEAD(&codec->dapm_widgets);
620 INIT_LIST_HEAD(&codec->dapm_paths);
621
622 wm8731_socdev = socdev;
623 ret = -ENODEV;
624
625#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
626 if (setup->i2c_address) {
627 codec->hw_write = (hw_write_t)i2c_master_send;
628 ret = wm8731_add_i2c_device(pdev, setup);
629 }
630#endif
631#if defined(CONFIG_SPI_MASTER)
632 if (setup->spi) {
633 codec->hw_write = (hw_write_t)wm8731_spi_write;
634 ret = spi_register_driver(&wm8731_spi_driver);
635 if (ret != 0)
636 printk(KERN_ERR "can't add spi driver");
637 }
638#endif
639
640 if (ret != 0) {
641 kfree(codec->private_data);
642 kfree(codec);
643 }
644 return ret;
645}
646
647/* power down chip */
648static int wm8731_remove(struct platform_device *pdev)
649{
650 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
529/* power down chip */
530static int wm8731_remove(struct platform_device *pdev)
531{
532 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
651 struct snd_soc_codec *codec = socdev->card->codec;
652
533
653 if (codec->control_data)
654 wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
655
656 snd_soc_free_pcms(socdev);
657 snd_soc_dapm_free(socdev);
534 snd_soc_free_pcms(socdev);
535 snd_soc_dapm_free(socdev);
658#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
659 i2c_unregister_device(codec->control_data);
660 i2c_del_driver(&wm8731_i2c_driver);
661#endif
662#if defined(CONFIG_SPI_MASTER)
663 spi_unregister_driver(&wm8731_spi_driver);
664#endif
665 kfree(codec->private_data);
666 kfree(codec);
667
668 return 0;
669}
670
671struct snd_soc_codec_device soc_codec_dev_wm8731 = {
672 .probe = wm8731_probe,
673 .remove = wm8731_remove,
674 .suspend = wm8731_suspend,
675 .resume = wm8731_resume,
676};
677EXPORT_SYMBOL_GPL(soc_codec_dev_wm8731);
678
536
537 return 0;
538}
539
540struct snd_soc_codec_device soc_codec_dev_wm8731 = {
541 .probe = wm8731_probe,
542 .remove = wm8731_remove,
543 .suspend = wm8731_suspend,
544 .resume = wm8731_resume,
545};
546EXPORT_SYMBOL_GPL(soc_codec_dev_wm8731);
547
679#if defined(CONFIG_SPI_MASTER)
680static int __devinit wm8731_spi_probe(struct spi_device *spi)
548static int wm8731_register(struct wm8731_priv *wm8731)
681{
549{
682 struct snd_soc_device *socdev = wm8731_socdev;
683 struct snd_soc_codec *codec = socdev->card->codec;
684 int ret;
550 int ret;
551 struct snd_soc_codec *codec = &wm8731->codec;
552 u16 reg;
685
553
686 codec->control_data = spi;
554 if (wm8731_codec) {
555 dev_err(codec->dev, "Another WM8731 is registered\n");
556 return -EINVAL;
557 }
687
558
688 ret = wm8731_init(socdev);
689 if (ret < 0)
690 dev_err(&spi->dev, "failed to initialise WM8731\n");
559 mutex_init(&codec->mutex);
560 INIT_LIST_HEAD(&codec->dapm_widgets);
561 INIT_LIST_HEAD(&codec->dapm_paths);
691
562
692 return ret;
563 codec->private_data = wm8731;
564 codec->name = "WM8731";
565 codec->owner = THIS_MODULE;
566 codec->read = wm8731_read_reg_cache;
567 codec->write = wm8731_write;
568 codec->bias_level = SND_SOC_BIAS_OFF;
569 codec->set_bias_level = wm8731_set_bias_level;
570 codec->dai = &wm8731_dai;
571 codec->num_dai = 1;
572 codec->reg_cache_size = WM8731_CACHEREGNUM;
573 codec->reg_cache = &wm8731->reg_cache;
574
575 memcpy(codec->reg_cache, wm8731_reg, sizeof(wm8731_reg));
576
577 wm8731_dai.dev = codec->dev;
578
579 wm8731_reset(codec);
580 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
581
582 /* Latch the update bits */
583 reg = wm8731_read_reg_cache(codec, WM8731_LOUT1V);
584 wm8731_write(codec, WM8731_LOUT1V, reg & ~0x0100);
585 reg = wm8731_read_reg_cache(codec, WM8731_ROUT1V);
586 wm8731_write(codec, WM8731_ROUT1V, reg & ~0x0100);
587 reg = wm8731_read_reg_cache(codec, WM8731_LINVOL);
588 wm8731_write(codec, WM8731_LINVOL, reg & ~0x0100);
589 reg = wm8731_read_reg_cache(codec, WM8731_RINVOL);
590 wm8731_write(codec, WM8731_RINVOL, reg & ~0x0100);
591
592 wm8731_codec = codec;
593
594 ret = snd_soc_register_codec(codec);
595 if (ret != 0) {
596 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
597 return ret;
598 }
599
600 ret = snd_soc_register_dai(&wm8731_dai);
601 if (ret != 0) {
602 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
603 snd_soc_unregister_codec(codec);
604 return ret;
605 }
606
607 return 0;
693}
694
608}
609
695static int __devexit wm8731_spi_remove(struct spi_device *spi)
610static void wm8731_unregister(struct wm8731_priv *wm8731)
696{
611{
697 return 0;
612 wm8731_set_bias_level(&wm8731->codec, SND_SOC_BIAS_OFF);
613 snd_soc_unregister_dai(&wm8731_dai);
614 snd_soc_unregister_codec(&wm8731->codec);
615 kfree(wm8731);
616 wm8731_codec = NULL;
698}
699
617}
618
700static struct spi_driver wm8731_spi_driver = {
701 .driver = {
702 .name = "wm8731",
703 .bus = &spi_bus_type,
704 .owner = THIS_MODULE,
705 },
706 .probe = wm8731_spi_probe,
707 .remove = __devexit_p(wm8731_spi_remove),
708};
709
619#if defined(CONFIG_SPI_MASTER)
710static int wm8731_spi_write(struct spi_device *spi, const char *data, int len)
711{
712 struct spi_transfer t;
713 struct spi_message m;
714 u8 msg[2];
715
716 if (len <= 0)
717 return 0;

--- 7 unchanged lines hidden (view full) ---

725 t.tx_buf = &msg[0];
726 t.len = len;
727
728 spi_message_add_tail(&t, &m);
729 spi_sync(spi, &m);
730
731 return len;
732}
620static int wm8731_spi_write(struct spi_device *spi, const char *data, int len)
621{
622 struct spi_transfer t;
623 struct spi_message m;
624 u8 msg[2];
625
626 if (len <= 0)
627 return 0;

--- 7 unchanged lines hidden (view full) ---

635 t.tx_buf = &msg[0];
636 t.len = len;
637
638 spi_message_add_tail(&t, &m);
639 spi_sync(spi, &m);
640
641 return len;
642}
643
644static int __devinit wm8731_spi_probe(struct spi_device *spi)
645{
646 struct snd_soc_codec *codec;
647 struct wm8731_priv *wm8731;
648
649 wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
650 if (wm8731 == NULL)
651 return -ENOMEM;
652
653 codec = &wm8731->codec;
654 codec->control_data = spi;
655 codec->hw_write = (hw_write_t)wm8731_spi_write;
656 codec->dev = &spi->dev;
657
658 return wm8731_register(wm8731);
659}
660
661static int __devexit wm8731_spi_remove(struct spi_device *spi)
662{
663 /* FIXME: This isn't actually implemented... */
664 return 0;
665}
666
667static struct spi_driver wm8731_spi_driver = {
668 .driver = {
669 .name = "wm8731",
670 .bus = &spi_bus_type,
671 .owner = THIS_MODULE,
672 },
673 .probe = wm8731_spi_probe,
674 .remove = __devexit_p(wm8731_spi_remove),
675};
733#endif /* CONFIG_SPI_MASTER */
734
735#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
676#endif /* CONFIG_SPI_MASTER */
677
678#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
736/*
737 * WM8731 2 wire address is determined by GPIO5
738 * state during powerup.
739 * low = 0x1a
740 * high = 0x1b
741 */
742
743static int wm8731_i2c_probe(struct i2c_client *i2c,
744 const struct i2c_device_id *id)
745{
679static int wm8731_i2c_probe(struct i2c_client *i2c,
680 const struct i2c_device_id *id)
681{
746 struct snd_soc_device *socdev = wm8731_socdev;
747 struct snd_soc_codec *codec = socdev->card->codec;
748 int ret;
682 struct wm8731_priv *wm8731;
683 struct snd_soc_codec *codec;
749
684
750 i2c_set_clientdata(i2c, codec);
685 wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
686 if (wm8731 == NULL)
687 return -ENOMEM;
688
689 codec = &wm8731->codec;
690 codec->hw_write = (hw_write_t)i2c_master_send;
691
692 i2c_set_clientdata(i2c, wm8731);
751 codec->control_data = i2c;
752
693 codec->control_data = i2c;
694
753 ret = wm8731_init(socdev);
754 if (ret < 0)
755 pr_err("failed to initialise WM8731\n");
695 codec->dev = &i2c->dev;
756
696
757 return ret;
697 return wm8731_register(wm8731);
758}
759
760static int wm8731_i2c_remove(struct i2c_client *client)
761{
698}
699
700static int wm8731_i2c_remove(struct i2c_client *client)
701{
762 struct snd_soc_codec *codec = i2c_get_clientdata(client);
763 kfree(codec->reg_cache);
702 struct wm8731_priv *wm8731 = i2c_get_clientdata(client);
703 wm8731_unregister(wm8731);
764 return 0;
765}
766
767static const struct i2c_device_id wm8731_i2c_id[] = {
768 { "wm8731", 0 },
769 { }
770};
771MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id);

--- 6 unchanged lines hidden (view full) ---

778 .probe = wm8731_i2c_probe,
779 .remove = wm8731_i2c_remove,
780 .id_table = wm8731_i2c_id,
781};
782#endif
783
784static int __init wm8731_modinit(void)
785{
704 return 0;
705}
706
707static const struct i2c_device_id wm8731_i2c_id[] = {
708 { "wm8731", 0 },
709 { }
710};
711MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id);

--- 6 unchanged lines hidden (view full) ---

718 .probe = wm8731_i2c_probe,
719 .remove = wm8731_i2c_remove,
720 .id_table = wm8731_i2c_id,
721};
722#endif
723
724static int __init wm8731_modinit(void)
725{
786 return snd_soc_register_dai(&wm8731_dai);
726 int ret;
727#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
728 ret = i2c_add_driver(&wm8731_i2c_driver);
729 if (ret != 0) {
730 printk(KERN_ERR "Failed to register WM8731 I2C driver: %d\n",
731 ret);
732 }
733#endif
734#if defined(CONFIG_SPI_MASTER)
735 ret = spi_register_driver(&wm8731_spi_driver);
736 if (ret != 0) {
737 printk(KERN_ERR "Failed to register WM8731 SPI driver: %d\n",
738 ret);
739 }
740#endif
741 return 0;
787}
788module_init(wm8731_modinit);
789
790static void __exit wm8731_exit(void)
791{
742}
743module_init(wm8731_modinit);
744
745static void __exit wm8731_exit(void)
746{
792 snd_soc_unregister_dai(&wm8731_dai);
747#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
748 i2c_del_driver(&wm8731_i2c_driver);
749#endif
750#if defined(CONFIG_SPI_MASTER)
751 spi_unregister_driver(&wm8731_spi_driver);
752#endif
793}
794module_exit(wm8731_exit);
795
796MODULE_DESCRIPTION("ASoC WM8731 driver");
797MODULE_AUTHOR("Richard Purdie");
798MODULE_LICENSE("GPL");
753}
754module_exit(wm8731_exit);
755
756MODULE_DESCRIPTION("ASoC WM8731 driver");
757MODULE_AUTHOR("Richard Purdie");
758MODULE_LICENSE("GPL");