linux/sound/soc/omap/omap-twl4030.c
<<
>>
Prefs
   1/*
   2 * omap-twl4030.c  --  SoC audio for TI SoC based boards with twl4030 codec
   3 *
   4 * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com
   5 * All rights reserved.
   6 *
   7 * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
   8 *
   9 * This driver replaces the following machine drivers:
  10 * omap3beagle (Author: Steve Sakoman <steve@sakoman.com>)
  11 * omap3evm (Author: Anuj Aggarwal <anuj.aggarwal@ti.com>)
  12 * overo (Author: Steve Sakoman <steve@sakoman.com>)
  13 * igep0020 (Author: Enric Balletbo i Serra <eballetbo@iseebcn.com>)
  14 * zoom2 (Author: Misael Lopez Cruz <misael.lopez@ti.com>)
  15 * sdp3430 (Author: Misael Lopez Cruz <misael.lopez@ti.com>)
  16 *
  17 * This program is free software; you can redistribute it and/or
  18 * modify it under the terms of the GNU General Public License
  19 * version 2 as published by the Free Software Foundation.
  20 *
  21 * This program is distributed in the hope that it will be useful, but
  22 * WITHOUT ANY WARRANTY; without even the implied warranty of
  23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  24 * General Public License for more details.
  25 *
  26 * You should have received a copy of the GNU General Public License
  27 * along with this program; if not, write to the Free Software
  28 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  29 * 02110-1301 USA
  30 *
  31 */
  32
  33#include <linux/platform_device.h>
  34#include <linux/platform_data/omap-twl4030.h>
  35#include <linux/module.h>
  36#include <linux/of.h>
  37#include <linux/gpio.h>
  38#include <linux/of_gpio.h>
  39
  40#include <sound/core.h>
  41#include <sound/pcm.h>
  42#include <sound/soc.h>
  43#include <sound/jack.h>
  44
  45#include "omap-mcbsp.h"
  46
  47struct omap_twl4030 {
  48        int jack_detect;        /* board can detect jack events */
  49        struct snd_soc_jack hs_jack;
  50};
  51
  52static int omap_twl4030_hw_params(struct snd_pcm_substream *substream,
  53        struct snd_pcm_hw_params *params)
  54{
  55        struct snd_soc_pcm_runtime *rtd = substream->private_data;
  56        struct snd_soc_dai *codec_dai = rtd->codec_dai;
  57        struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  58        struct snd_soc_card *card = rtd->card;
  59        unsigned int fmt;
  60        int ret;
  61
  62        switch (params_channels(params)) {
  63        case 2: /* Stereo I2S mode */
  64                fmt =   SND_SOC_DAIFMT_I2S |
  65                        SND_SOC_DAIFMT_NB_NF |
  66                        SND_SOC_DAIFMT_CBM_CFM;
  67                break;
  68        case 4: /* Four channel TDM mode */
  69                fmt =   SND_SOC_DAIFMT_DSP_A |
  70                        SND_SOC_DAIFMT_IB_NF |
  71                        SND_SOC_DAIFMT_CBM_CFM;
  72                break;
  73        default:
  74                return -EINVAL;
  75        }
  76
  77        /* Set codec DAI configuration */
  78        ret = snd_soc_dai_set_fmt(codec_dai, fmt);
  79        if (ret < 0) {
  80                dev_err(card->dev, "can't set codec DAI configuration\n");
  81                return ret;
  82        }
  83
  84        /* Set cpu DAI configuration */
  85        ret = snd_soc_dai_set_fmt(cpu_dai, fmt);
  86        if (ret < 0) {
  87                dev_err(card->dev, "can't set cpu DAI configuration\n");
  88                return ret;
  89        }
  90
  91        return 0;
  92}
  93
  94static struct snd_soc_ops omap_twl4030_ops = {
  95        .hw_params = omap_twl4030_hw_params,
  96};
  97
  98static const struct snd_soc_dapm_widget dapm_widgets[] = {
  99        SND_SOC_DAPM_SPK("Earpiece Spk", NULL),
 100        SND_SOC_DAPM_SPK("Handsfree Spk", NULL),
 101        SND_SOC_DAPM_HP("Headset Stereophone", NULL),
 102        SND_SOC_DAPM_SPK("Ext Spk", NULL),
 103        SND_SOC_DAPM_SPK("Carkit Spk", NULL),
 104
 105        SND_SOC_DAPM_MIC("Main Mic", NULL),
 106        SND_SOC_DAPM_MIC("Sub Mic", NULL),
 107        SND_SOC_DAPM_MIC("Headset Mic", NULL),
 108        SND_SOC_DAPM_MIC("Carkit Mic", NULL),
 109        SND_SOC_DAPM_MIC("Digital0 Mic", NULL),
 110        SND_SOC_DAPM_MIC("Digital1 Mic", NULL),
 111        SND_SOC_DAPM_LINE("Line In", NULL),
 112};
 113
 114static const struct snd_soc_dapm_route audio_map[] = {
 115        /* Headset Stereophone:  HSOL, HSOR */
 116        {"Headset Stereophone", NULL, "HSOL"},
 117        {"Headset Stereophone", NULL, "HSOR"},
 118        /* External Speakers: HFL, HFR */
 119        {"Handsfree Spk", NULL, "HFL"},
 120        {"Handsfree Spk", NULL, "HFR"},
 121        /* External Speakers: PredrivL, PredrivR */
 122        {"Ext Spk", NULL, "PREDRIVEL"},
 123        {"Ext Spk", NULL, "PREDRIVER"},
 124        /* Carkit speakers:  CARKITL, CARKITR */
 125        {"Carkit Spk", NULL, "CARKITL"},
 126        {"Carkit Spk", NULL, "CARKITR"},
 127        /* Earpiece */
 128        {"Earpiece Spk", NULL, "EARPIECE"},
 129
 130        /* External Mics: MAINMIC, SUBMIC with bias */
 131        {"MAINMIC", NULL, "Main Mic"},
 132        {"Main Mic", NULL, "Mic Bias 1"},
 133        {"SUBMIC", NULL, "Sub Mic"},
 134        {"Sub Mic", NULL, "Mic Bias 2"},
 135        /* Headset Mic: HSMIC with bias */
 136        {"HSMIC", NULL, "Headset Mic"},
 137        {"Headset Mic", NULL, "Headset Mic Bias"},
 138        /* Digital Mics: DIGIMIC0, DIGIMIC1 with bias */
 139        {"DIGIMIC0", NULL, "Digital0 Mic"},
 140        {"Digital0 Mic", NULL, "Mic Bias 1"},
 141        {"DIGIMIC1", NULL, "Digital1 Mic"},
 142        {"Digital1 Mic", NULL, "Mic Bias 2"},
 143        /* Carkit In: CARKITMIC */
 144        {"CARKITMIC", NULL, "Carkit Mic"},
 145        /* Aux In: AUXL, AUXR */
 146        {"AUXL", NULL, "Line In"},
 147        {"AUXR", NULL, "Line In"},
 148};
 149
 150/* Headset jack detection DAPM pins */
 151static struct snd_soc_jack_pin hs_jack_pins[] = {
 152        {
 153                .pin = "Headset Mic",
 154                .mask = SND_JACK_MICROPHONE,
 155        },
 156        {
 157                .pin = "Headset Stereophone",
 158                .mask = SND_JACK_HEADPHONE,
 159        },
 160};
 161
 162/* Headset jack detection gpios */
 163static struct snd_soc_jack_gpio hs_jack_gpios[] = {
 164        {
 165                .name = "hsdet-gpio",
 166                .report = SND_JACK_HEADSET,
 167                .debounce_time = 200,
 168        },
 169};
 170
 171static inline void twl4030_disconnect_pin(struct snd_soc_dapm_context *dapm,
 172                                          int connected, char *pin)
 173{
 174        if (!connected)
 175                snd_soc_dapm_disable_pin(dapm, pin);
 176}
 177
 178static int omap_twl4030_init(struct snd_soc_pcm_runtime *rtd)
 179{
 180        struct snd_soc_codec *codec = rtd->codec;
 181        struct snd_soc_card *card = rtd->card;
 182        struct snd_soc_dapm_context *dapm = &codec->dapm;
 183        struct omap_tw4030_pdata *pdata = dev_get_platdata(card->dev);
 184        struct omap_twl4030 *priv = snd_soc_card_get_drvdata(card);
 185        int ret = 0;
 186
 187        /* Headset jack detection only if it is supported */
 188        if (priv->jack_detect > 0) {
 189                hs_jack_gpios[0].gpio = priv->jack_detect;
 190
 191                ret = snd_soc_jack_new(codec, "Headset Jack", SND_JACK_HEADSET,
 192                                       &priv->hs_jack);
 193                if (ret)
 194                        return ret;
 195
 196                ret = snd_soc_jack_add_pins(&priv->hs_jack,
 197                                            ARRAY_SIZE(hs_jack_pins),
 198                                            hs_jack_pins);
 199                if (ret)
 200                        return ret;
 201
 202                ret = snd_soc_jack_add_gpios(&priv->hs_jack,
 203                                             ARRAY_SIZE(hs_jack_gpios),
 204                                             hs_jack_gpios);
 205                if (ret)
 206                        return ret;
 207        }
 208
 209        /*
 210         * NULL pdata means we booted with DT. In this case the routing is
 211         * provided and the card is fully routed, no need to mark pins.
 212         */
 213        if (!pdata || !pdata->custom_routing)
 214                return ret;
 215
 216        /* Disable not connected paths if not used */
 217        twl4030_disconnect_pin(dapm, pdata->has_ear, "Earpiece Spk");
 218        twl4030_disconnect_pin(dapm, pdata->has_hf, "Handsfree Spk");
 219        twl4030_disconnect_pin(dapm, pdata->has_hs, "Headset Stereophone");
 220        twl4030_disconnect_pin(dapm, pdata->has_predriv, "Ext Spk");
 221        twl4030_disconnect_pin(dapm, pdata->has_carkit, "Carkit Spk");
 222
 223        twl4030_disconnect_pin(dapm, pdata->has_mainmic, "Main Mic");
 224        twl4030_disconnect_pin(dapm, pdata->has_submic, "Sub Mic");
 225        twl4030_disconnect_pin(dapm, pdata->has_hsmic, "Headset Mic");
 226        twl4030_disconnect_pin(dapm, pdata->has_carkitmic, "Carkit Mic");
 227        twl4030_disconnect_pin(dapm, pdata->has_digimic0, "Digital0 Mic");
 228        twl4030_disconnect_pin(dapm, pdata->has_digimic1, "Digital1 Mic");
 229        twl4030_disconnect_pin(dapm, pdata->has_linein, "Line In");
 230
 231        return ret;
 232}
 233
 234static int omap_twl4030_card_remove(struct snd_soc_card *card)
 235{
 236        struct omap_twl4030 *priv = snd_soc_card_get_drvdata(card);
 237
 238        if (priv->jack_detect > 0)
 239                snd_soc_jack_free_gpios(&priv->hs_jack,
 240                                        ARRAY_SIZE(hs_jack_gpios),
 241                                        hs_jack_gpios);
 242
 243        return 0;
 244}
 245
 246/* Digital audio interface glue - connects codec <--> CPU */
 247static struct snd_soc_dai_link omap_twl4030_dai_links[] = {
 248        {
 249                .name = "TWL4030 HiFi",
 250                .stream_name = "TWL4030 HiFi",
 251                .cpu_dai_name = "omap-mcbsp.2",
 252                .codec_dai_name = "twl4030-hifi",
 253                .platform_name = "omap-mcbsp.2",
 254                .codec_name = "twl4030-codec",
 255                .init = omap_twl4030_init,
 256                .ops = &omap_twl4030_ops,
 257        },
 258        {
 259                .name = "TWL4030 Voice",
 260                .stream_name = "TWL4030 Voice",
 261                .cpu_dai_name = "omap-mcbsp.3",
 262                .codec_dai_name = "twl4030-voice",
 263                .platform_name = "omap-mcbsp.3",
 264                .codec_name = "twl4030-codec",
 265                .dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_IB_NF |
 266                           SND_SOC_DAIFMT_CBM_CFM,
 267        },
 268};
 269
 270/* Audio machine driver */
 271static struct snd_soc_card omap_twl4030_card = {
 272        .owner = THIS_MODULE,
 273        .remove = omap_twl4030_card_remove,
 274        .dai_link = omap_twl4030_dai_links,
 275        .num_links = ARRAY_SIZE(omap_twl4030_dai_links),
 276
 277        .dapm_widgets = dapm_widgets,
 278        .num_dapm_widgets = ARRAY_SIZE(dapm_widgets),
 279        .dapm_routes = audio_map,
 280        .num_dapm_routes = ARRAY_SIZE(audio_map),
 281};
 282
 283static int omap_twl4030_probe(struct platform_device *pdev)
 284{
 285        struct omap_tw4030_pdata *pdata = dev_get_platdata(&pdev->dev);
 286        struct device_node *node = pdev->dev.of_node;
 287        struct snd_soc_card *card = &omap_twl4030_card;
 288        struct omap_twl4030 *priv;
 289        int ret = 0;
 290
 291        card->dev = &pdev->dev;
 292
 293        priv = devm_kzalloc(&pdev->dev, sizeof(struct omap_twl4030), GFP_KERNEL);
 294        if (priv == NULL)
 295                return -ENOMEM;
 296
 297        if (node) {
 298                struct device_node *dai_node;
 299                struct property *prop;
 300
 301                if (snd_soc_of_parse_card_name(card, "ti,model")) {
 302                        dev_err(&pdev->dev, "Card name is not provided\n");
 303                        return -ENODEV;
 304                }
 305
 306                dai_node = of_parse_phandle(node, "ti,mcbsp", 0);
 307                if (!dai_node) {
 308                        dev_err(&pdev->dev, "McBSP node is not provided\n");
 309                        return -EINVAL;
 310                }
 311                omap_twl4030_dai_links[0].cpu_dai_name  = NULL;
 312                omap_twl4030_dai_links[0].cpu_of_node = dai_node;
 313
 314                omap_twl4030_dai_links[0].platform_name  = NULL;
 315                omap_twl4030_dai_links[0].platform_of_node = dai_node;
 316
 317                dai_node = of_parse_phandle(node, "ti,mcbsp-voice", 0);
 318                if (!dai_node) {
 319                        card->num_links = 1;
 320                } else {
 321                        omap_twl4030_dai_links[1].cpu_dai_name  = NULL;
 322                        omap_twl4030_dai_links[1].cpu_of_node = dai_node;
 323
 324                        omap_twl4030_dai_links[1].platform_name  = NULL;
 325                        omap_twl4030_dai_links[1].platform_of_node = dai_node;
 326                }
 327
 328                priv->jack_detect = of_get_named_gpio(node,
 329                                                      "ti,jack-det-gpio", 0);
 330
 331                /* Optional: audio routing can be provided */
 332                prop = of_find_property(node, "ti,audio-routing", NULL);
 333                if (prop) {
 334                        ret = snd_soc_of_parse_audio_routing(card,
 335                                                            "ti,audio-routing");
 336                        if (ret)
 337                                return ret;
 338
 339                        card->fully_routed = 1;
 340                }
 341        } else if (pdata) {
 342                if (pdata->card_name) {
 343                        card->name = pdata->card_name;
 344                } else {
 345                        dev_err(&pdev->dev, "Card name is not provided\n");
 346                        return -ENODEV;
 347                }
 348
 349                if (!pdata->voice_connected)
 350                        card->num_links = 1;
 351
 352                priv->jack_detect = pdata->jack_detect;
 353        } else {
 354                dev_err(&pdev->dev, "Missing pdata\n");
 355                return -ENODEV;
 356        }
 357
 358        snd_soc_card_set_drvdata(card, priv);
 359        ret = devm_snd_soc_register_card(&pdev->dev, card);
 360        if (ret) {
 361                dev_err(&pdev->dev, "devm_snd_soc_register_card() failed: %d\n",
 362                        ret);
 363                return ret;
 364        }
 365
 366        return 0;
 367}
 368
 369static const struct of_device_id omap_twl4030_of_match[] = {
 370        {.compatible = "ti,omap-twl4030", },
 371        { },
 372};
 373MODULE_DEVICE_TABLE(of, omap_twl4030_of_match);
 374
 375static struct platform_driver omap_twl4030_driver = {
 376        .driver = {
 377                .name = "omap-twl4030",
 378                .pm = &snd_soc_pm_ops,
 379                .of_match_table = omap_twl4030_of_match,
 380        },
 381        .probe = omap_twl4030_probe,
 382};
 383
 384module_platform_driver(omap_twl4030_driver);
 385
 386MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
 387MODULE_DESCRIPTION("ALSA SoC for TI SoC based boards with twl4030 codec");
 388MODULE_LICENSE("GPL");
 389MODULE_ALIAS("platform:omap-twl4030");
 390