]> nv-tegra.nvidia Code Review - linux-2.6.git/blobdiff - sound/soc/codecs/uda1380.c
bitops: rename for_each_bit() to for_each_set_bit()
[linux-2.6.git] / sound / soc / codecs / uda1380.c
index 1b10f488328c015479c65393071a01ed4f4b85b2..9cd0a66b7663a8d774201ca276fbde0c6494f758 100644 (file)
@@ -5,9 +5,7 @@
  * it under the terms of the GNU General Public License version 2 as
  * published by the Free Software Foundation.
  *
- * Copyright (c) 2007 Philipp Zabel <philipp.zabel@gmail.com>
- * Improved support for DAPM and audio routing/mixing capabilities,
- * added TLV support.
+ * Copyright (c) 2007-2009 Philipp Zabel <philipp.zabel@gmail.com>
  *
  * Modified by Richard Purdie <richard@openedhand.com> to fit into SoC
  * codec model.
 #include <linux/module.h>
 #include <linux/init.h>
 #include <linux/types.h>
-#include <linux/string.h>
 #include <linux/slab.h>
 #include <linux/errno.h>
-#include <linux/ioctl.h>
+#include <linux/gpio.h>
 #include <linux/delay.h>
 #include <linux/i2c.h>
 #include <linux/workqueue.h>
 #include <sound/core.h>
 #include <sound/control.h>
 #include <sound/initval.h>
-#include <sound/info.h>
 #include <sound/soc.h>
 #include <sound/soc-dapm.h>
 #include <sound/tlv.h>
+#include <sound/uda1380.h>
 
 #include "uda1380.h"
 
-static struct work_struct uda1380_work;
 static struct snd_soc_codec *uda1380_codec;
 
+/* codec private data */
+struct uda1380_priv {
+       struct snd_soc_codec codec;
+       u16 reg_cache[UDA1380_CACHEREGNUM];
+       unsigned int dac_clk;
+       struct work_struct work;
+};
+
 /*
  * uda1380 register cache
  */
@@ -133,7 +137,7 @@ static void uda1380_flush_work(struct work_struct *work)
 {
        int bit, reg;
 
-       for_each_bit(bit, &uda1380_cache_dirty, UDA1380_CACHEREGNUM - 0x10) {
+       for_each_set_bit(bit, &uda1380_cache_dirty, UDA1380_CACHEREGNUM - 0x10) {
                reg = 0x10 + bit;
                pr_debug("uda1380: flush reg %x val %x:\n", reg,
                                uda1380_read_reg_cache(uda1380_codec, reg));
@@ -374,7 +378,6 @@ static int uda1380_add_widgets(struct snd_soc_codec *codec)
 
        snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
 
-       snd_soc_dapm_new_widgets(codec);
        return 0;
 }
 
@@ -473,6 +476,7 @@ static int uda1380_trigger(struct snd_pcm_substream *substream, int cmd,
        struct snd_soc_pcm_runtime *rtd = substream->private_data;
        struct snd_soc_device *socdev = rtd->socdev;
        struct snd_soc_codec *codec = socdev->card->codec;
+       struct uda1380_priv *uda1380 = codec->private_data;
        int mixer = uda1380_read_reg_cache(codec, UDA1380_MIXER);
 
        switch (cmd) {
@@ -480,13 +484,13 @@ static int uda1380_trigger(struct snd_pcm_substream *substream, int cmd,
        case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
                uda1380_write_reg_cache(codec, UDA1380_MIXER,
                                        mixer & ~R14_SILENCE);
-               schedule_work(&uda1380_work);
+               schedule_work(&uda1380->work);
                break;
        case SNDRV_PCM_TRIGGER_STOP:
        case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
                uda1380_write_reg_cache(codec, UDA1380_MIXER,
                                        mixer | R14_SILENCE);
-               schedule_work(&uda1380_work);
+               schedule_work(&uda1380->work);
                break;
        }
        return 0;
@@ -579,6 +583,27 @@ static int uda1380_set_bias_level(struct snd_soc_codec *codec,
                       SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
                       SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
 
+static struct snd_soc_dai_ops uda1380_dai_ops = {
+       .hw_params      = uda1380_pcm_hw_params,
+       .shutdown       = uda1380_pcm_shutdown,
+       .trigger        = uda1380_trigger,
+       .set_fmt        = uda1380_set_dai_fmt_both,
+};
+
+static struct snd_soc_dai_ops uda1380_dai_ops_playback = {
+       .hw_params      = uda1380_pcm_hw_params,
+       .shutdown       = uda1380_pcm_shutdown,
+       .trigger        = uda1380_trigger,
+       .set_fmt        = uda1380_set_dai_fmt_playback,
+};
+
+static struct snd_soc_dai_ops uda1380_dai_ops_capture = {
+       .hw_params      = uda1380_pcm_hw_params,
+       .shutdown       = uda1380_pcm_shutdown,
+       .trigger        = uda1380_trigger,
+       .set_fmt        = uda1380_set_dai_fmt_capture,
+};
+
 struct snd_soc_dai uda1380_dai[] = {
 {
        .name = "UDA1380",
@@ -594,12 +619,7 @@ struct snd_soc_dai uda1380_dai[] = {
                .channels_max = 2,
                .rates = UDA1380_RATES,
                .formats = SNDRV_PCM_FMTBIT_S16_LE,},
-       .ops = {
-               .trigger = uda1380_trigger,
-               .hw_params = uda1380_pcm_hw_params,
-               .shutdown = uda1380_pcm_shutdown,
-               .set_fmt = uda1380_set_dai_fmt_both,
-       },
+       .ops = &uda1380_dai_ops,
 },
 { /* playback only - dual interface */
        .name = "UDA1380",
@@ -610,12 +630,7 @@ struct snd_soc_dai uda1380_dai[] = {
                .rates = UDA1380_RATES,
                .formats = SNDRV_PCM_FMTBIT_S16_LE,
        },
-       .ops = {
-               .trigger = uda1380_trigger,
-               .hw_params = uda1380_pcm_hw_params,
-               .shutdown = uda1380_pcm_shutdown,
-               .set_fmt = uda1380_set_dai_fmt_playback,
-       },
+       .ops = &uda1380_dai_ops_playback,
 },
 { /* capture only - dual interface*/
        .name = "UDA1380",
@@ -626,12 +641,7 @@ struct snd_soc_dai uda1380_dai[] = {
                .rates = UDA1380_RATES,
                .formats = SNDRV_PCM_FMTBIT_S16_LE,
        },
-       .ops = {
-               .trigger = uda1380_trigger,
-               .hw_params = uda1380_pcm_hw_params,
-               .shutdown = uda1380_pcm_shutdown,
-               .set_fmt = uda1380_set_dai_fmt_capture,
-       },
+       .ops = &uda1380_dai_ops_capture,
 },
 };
 EXPORT_SYMBOL_GPL(uda1380_dai);
@@ -664,44 +674,33 @@ static int uda1380_resume(struct platform_device *pdev)
        return 0;
 }
 
-/*
- * initialise the UDA1380 driver
- * register mixer and dsp interfaces with the kernel
- */
-static int uda1380_init(struct snd_soc_device *socdev, int dac_clk)
+static int uda1380_probe(struct platform_device *pdev)
 {
-       struct snd_soc_codec *codec = socdev->card->codec;
+       struct snd_soc_device *socdev = platform_get_drvdata(pdev);
+       struct snd_soc_codec *codec;
+       struct uda1380_platform_data *pdata;
        int ret = 0;
 
-       codec->name = "UDA1380";
-       codec->owner = THIS_MODULE;
-       codec->read = uda1380_read_reg_cache;
-       codec->write = uda1380_write;
-       codec->set_bias_level = uda1380_set_bias_level;
-       codec->dai = uda1380_dai;
-       codec->num_dai = ARRAY_SIZE(uda1380_dai);
-       codec->reg_cache = kmemdup(uda1380_reg, sizeof(uda1380_reg),
-                                  GFP_KERNEL);
-       if (codec->reg_cache == NULL)
-               return -ENOMEM;
-       codec->reg_cache_size = ARRAY_SIZE(uda1380_reg);
-       codec->reg_cache_step = 1;
-       uda1380_reset(codec);
+       if (uda1380_codec == NULL) {
+               dev_err(&pdev->dev, "Codec device not registered\n");
+               return -ENODEV;
+       }
 
-       uda1380_codec = codec;
-       INIT_WORK(&uda1380_work, uda1380_flush_work);
+       socdev->card->codec = uda1380_codec;
+       codec = uda1380_codec;
+       pdata = codec->dev->platform_data;
 
        /* register pcms */
        ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
        if (ret < 0) {
-               pr_err("uda1380: failed to create pcms\n");
+               dev_err(codec->dev, "failed to create pcms: %d\n", ret);
                goto pcm_err;
        }
 
        /* power on device */
        uda1380_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
        /* set clock input */
-       switch (dac_clk) {
+       switch (pdata->dac_clk) {
        case UDA1380_DAC_CLK_SYSCLK:
                uda1380_write(codec, UDA1380_CLK, 0);
                break;
@@ -710,181 +709,208 @@ static int uda1380_init(struct snd_soc_device *socdev, int dac_clk)
                break;
        }
 
-       /* uda1380 init */
        snd_soc_add_controls(codec, uda1380_snd_controls,
                                ARRAY_SIZE(uda1380_snd_controls));
        uda1380_add_widgets(codec);
-       ret = snd_soc_init_card(socdev);
-       if (ret < 0) {
-               pr_err("uda1380: failed to register card\n");
-               goto card_err;
-       }
 
        return ret;
 
-card_err:
-       snd_soc_free_pcms(socdev);
-       snd_soc_dapm_free(socdev);
 pcm_err:
-       kfree(codec->reg_cache);
        return ret;
 }
 
-static struct snd_soc_device *uda1380_socdev;
-
-#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
-
-static int uda1380_i2c_probe(struct i2c_client *i2c,
-                            const struct i2c_device_id *id)
+/* power down chip */
+static int uda1380_remove(struct platform_device *pdev)
 {
-       struct snd_soc_device *socdev = uda1380_socdev;
-       struct uda1380_setup_data *setup = socdev->codec_data;
+       struct snd_soc_device *socdev = platform_get_drvdata(pdev);
        struct snd_soc_codec *codec = socdev->card->codec;
-       int ret;
-
-       i2c_set_clientdata(i2c, codec);
-       codec->control_data = i2c;
 
-       ret = uda1380_init(socdev, setup->dac_clk);
-       if (ret < 0)
-               pr_err("uda1380: failed to initialise UDA1380\n");
+       if (codec->control_data)
+               uda1380_set_bias_level(codec, SND_SOC_BIAS_OFF);
 
-       return ret;
-}
+       snd_soc_free_pcms(socdev);
+       snd_soc_dapm_free(socdev);
 
-static int uda1380_i2c_remove(struct i2c_client *client)
-{
-       struct snd_soc_codec *codec = i2c_get_clientdata(client);
-       kfree(codec->reg_cache);
        return 0;
 }
 
-static const struct i2c_device_id uda1380_i2c_id[] = {
-       { "uda1380", 0 },
-       { }
-};
-MODULE_DEVICE_TABLE(i2c, uda1380_i2c_id);
-
-static struct i2c_driver uda1380_i2c_driver = {
-       .driver = {
-               .name =  "UDA1380 I2C Codec",
-               .owner = THIS_MODULE,
-       },
-       .probe =    uda1380_i2c_probe,
-       .remove =   uda1380_i2c_remove,
-       .id_table = uda1380_i2c_id,
+struct snd_soc_codec_device soc_codec_dev_uda1380 = {
+       .probe =        uda1380_probe,
+       .remove =       uda1380_remove,
+       .suspend =      uda1380_suspend,
+       .resume =       uda1380_resume,
 };
+EXPORT_SYMBOL_GPL(soc_codec_dev_uda1380);
 
-static int uda1380_add_i2c_device(struct platform_device *pdev,
-                                 const struct uda1380_setup_data *setup)
+static int uda1380_register(struct uda1380_priv *uda1380)
 {
-       struct i2c_board_info info;
-       struct i2c_adapter *adapter;
-       struct i2c_client *client;
-       int ret;
+       int ret, i;
+       struct snd_soc_codec *codec = &uda1380->codec;
+       struct uda1380_platform_data *pdata = codec->dev->platform_data;
 
-       ret = i2c_add_driver(&uda1380_i2c_driver);
-       if (ret != 0) {
-               dev_err(&pdev->dev, "can't add i2c driver\n");
-               return ret;
+       if (uda1380_codec) {
+               dev_err(codec->dev, "Another UDA1380 is registered\n");
+               return -EINVAL;
        }
 
-       memset(&info, 0, sizeof(struct i2c_board_info));
-       info.addr = setup->i2c_address;
-       strlcpy(info.type, "uda1380", I2C_NAME_SIZE);
+       if (!pdata || !pdata->gpio_power || !pdata->gpio_reset)
+               return -EINVAL;
+
+       ret = gpio_request(pdata->gpio_power, "uda1380 power");
+       if (ret)
+               goto err_out;
+       ret = gpio_request(pdata->gpio_reset, "uda1380 reset");
+       if (ret)
+               goto err_gpio;
+
+       gpio_direction_output(pdata->gpio_power, 1);
+
+       /* we may need to have the clock running here - pH5 */
+       gpio_direction_output(pdata->gpio_reset, 1);
+       udelay(5);
+       gpio_set_value(pdata->gpio_reset, 0);
+
+       mutex_init(&codec->mutex);
+       INIT_LIST_HEAD(&codec->dapm_widgets);
+       INIT_LIST_HEAD(&codec->dapm_paths);
 
-       adapter = i2c_get_adapter(setup->i2c_bus);
-       if (!adapter) {
-               dev_err(&pdev->dev, "can't get i2c adapter %d\n",
-                       setup->i2c_bus);
-               goto err_driver;
+       codec->private_data = uda1380;
+       codec->name = "UDA1380";
+       codec->owner = THIS_MODULE;
+       codec->read = uda1380_read_reg_cache;
+       codec->write = uda1380_write;
+       codec->bias_level = SND_SOC_BIAS_OFF;
+       codec->set_bias_level = uda1380_set_bias_level;
+       codec->dai = uda1380_dai;
+       codec->num_dai = ARRAY_SIZE(uda1380_dai);
+       codec->reg_cache_size = ARRAY_SIZE(uda1380_reg);
+       codec->reg_cache = &uda1380->reg_cache;
+       codec->reg_cache_step = 1;
+
+       memcpy(codec->reg_cache, uda1380_reg, sizeof(uda1380_reg));
+
+       ret = uda1380_reset(codec);
+       if (ret < 0) {
+               dev_err(codec->dev, "Failed to issue reset\n");
+               goto err_reset;
        }
 
-       client = i2c_new_device(adapter, &info);
-       i2c_put_adapter(adapter);
-       if (!client) {
-               dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
-                       (unsigned int)info.addr);
-               goto err_driver;
+       INIT_WORK(&uda1380->work, uda1380_flush_work);
+
+       for (i = 0; i < ARRAY_SIZE(uda1380_dai); i++)
+               uda1380_dai[i].dev = codec->dev;
+
+       uda1380_codec = codec;
+
+       ret = snd_soc_register_codec(codec);
+       if (ret != 0) {
+               dev_err(codec->dev, "Failed to register codec: %d\n", ret);
+               goto err_reset;
+       }
+
+       ret = snd_soc_register_dais(uda1380_dai, ARRAY_SIZE(uda1380_dai));
+       if (ret != 0) {
+               dev_err(codec->dev, "Failed to register DAIs: %d\n", ret);
+               goto err_dai;
        }
 
        return 0;
 
-err_driver:
-       i2c_del_driver(&uda1380_i2c_driver);
-       return -ENODEV;
+err_dai:
+       snd_soc_unregister_codec(codec);
+err_reset:
+       gpio_set_value(pdata->gpio_power, 0);
+       gpio_free(pdata->gpio_reset);
+err_gpio:
+       gpio_free(pdata->gpio_power);
+err_out:
+       return ret;
 }
-#endif
 
-static int uda1380_probe(struct platform_device *pdev)
+static void uda1380_unregister(struct uda1380_priv *uda1380)
 {
-       struct snd_soc_device *socdev = platform_get_drvdata(pdev);
-       struct uda1380_setup_data *setup;
+       struct snd_soc_codec *codec = &uda1380->codec;
+       struct uda1380_platform_data *pdata = codec->dev->platform_data;
+
+       snd_soc_unregister_dais(uda1380_dai, ARRAY_SIZE(uda1380_dai));
+       snd_soc_unregister_codec(&uda1380->codec);
+
+       gpio_set_value(pdata->gpio_power, 0);
+       gpio_free(pdata->gpio_reset);
+       gpio_free(pdata->gpio_power);
+
+       kfree(uda1380);
+       uda1380_codec = NULL;
+}
+
+#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
+static __devinit int uda1380_i2c_probe(struct i2c_client *i2c,
+                                     const struct i2c_device_id *id)
+{
+       struct uda1380_priv *uda1380;
        struct snd_soc_codec *codec;
        int ret;
 
-       setup = socdev->codec_data;
-       codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
-       if (codec == NULL)
+       uda1380 = kzalloc(sizeof(struct uda1380_priv), GFP_KERNEL);
+       if (uda1380 == NULL)
                return -ENOMEM;
 
-       socdev->card->codec = codec;
-       mutex_init(&codec->mutex);
-       INIT_LIST_HEAD(&codec->dapm_widgets);
-       INIT_LIST_HEAD(&codec->dapm_paths);
+       codec = &uda1380->codec;
+       codec->hw_write = (hw_write_t)i2c_master_send;
 
-       uda1380_socdev = socdev;
-       ret = -ENODEV;
+       i2c_set_clientdata(i2c, uda1380);
+       codec->control_data = i2c;
 
-#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
-       if (setup->i2c_address) {
-               codec->hw_write = (hw_write_t)i2c_master_send;
-               ret = uda1380_add_i2c_device(pdev, setup);
-       }
-#endif
+       codec->dev = &i2c->dev;
 
+       ret = uda1380_register(uda1380);
        if (ret != 0)
-               kfree(codec);
+               kfree(uda1380);
+
        return ret;
 }
 
-/* power down chip */
-static int uda1380_remove(struct platform_device *pdev)
+static int __devexit uda1380_i2c_remove(struct i2c_client *i2c)
 {
-       struct snd_soc_device *socdev = platform_get_drvdata(pdev);
-       struct snd_soc_codec *codec = socdev->card->codec;
-
-       if (codec->control_data)
-               uda1380_set_bias_level(codec, SND_SOC_BIAS_OFF);
-
-       snd_soc_free_pcms(socdev);
-       snd_soc_dapm_free(socdev);
-#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
-       i2c_unregister_device(codec->control_data);
-       i2c_del_driver(&uda1380_i2c_driver);
-#endif
-       kfree(codec);
-
+       struct uda1380_priv *uda1380 = i2c_get_clientdata(i2c);
+       uda1380_unregister(uda1380);
        return 0;
 }
 
-struct snd_soc_codec_device soc_codec_dev_uda1380 = {
-       .probe =        uda1380_probe,
-       .remove =       uda1380_remove,
-       .suspend =      uda1380_suspend,
-       .resume =       uda1380_resume,
+static const struct i2c_device_id uda1380_i2c_id[] = {
+       { "uda1380", 0 },
+       { }
 };
-EXPORT_SYMBOL_GPL(soc_codec_dev_uda1380);
+MODULE_DEVICE_TABLE(i2c, uda1380_i2c_id);
+
+static struct i2c_driver uda1380_i2c_driver = {
+       .driver = {
+               .name =  "UDA1380 I2C Codec",
+               .owner = THIS_MODULE,
+       },
+       .probe =    uda1380_i2c_probe,
+       .remove =   __devexit_p(uda1380_i2c_remove),
+       .id_table = uda1380_i2c_id,
+};
+#endif
 
 static int __init uda1380_modinit(void)
 {
-       return snd_soc_register_dais(uda1380_dai, ARRAY_SIZE(uda1380_dai));
+       int ret;
+#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
+       ret = i2c_add_driver(&uda1380_i2c_driver);
+       if (ret != 0)
+               pr_err("Failed to register UDA1380 I2C driver: %d\n", ret);
+#endif
+       return 0;
 }
 module_init(uda1380_modinit);
 
 static void __exit uda1380_exit(void)
 {
-       snd_soc_unregister_dais(uda1380_dai, ARRAY_SIZE(uda1380_dai));
+#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
+       i2c_del_driver(&uda1380_i2c_driver);
+#endif
 }
 module_exit(uda1380_exit);