Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 1 | /* |
| 2 | * tegra_alc5632.c -- Toshiba AC100(PAZ00) machine ASoC driver |
| 3 | * |
| 4 | * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.lauchpad.net> |
| 5 | * |
| 6 | * Authors: Leon Romanovsky <leon@leon.nu> |
| 7 | * Andrey Danin <danindrey@mail.ru> |
| 8 | * Marc Dietrich <marvin24@gmx.de> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | */ |
| 14 | |
| 15 | #include <asm/mach-types.h> |
| 16 | |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/gpio.h> |
Leon Romanovsky | d559f1e | 2012-02-02 22:13:37 +0200 | [diff] [blame] | 21 | #include <linux/of_gpio.h> |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 22 | |
| 23 | #include <sound/core.h> |
| 24 | #include <sound/jack.h> |
| 25 | #include <sound/pcm.h> |
| 26 | #include <sound/pcm_params.h> |
| 27 | #include <sound/soc.h> |
| 28 | |
| 29 | #include "../codecs/alc5632.h" |
| 30 | |
| 31 | #include "tegra_das.h" |
| 32 | #include "tegra_i2s.h" |
| 33 | #include "tegra_pcm.h" |
| 34 | #include "tegra_asoc_utils.h" |
| 35 | |
| 36 | #define DRV_NAME "tegra-alc5632" |
| 37 | |
Leon Romanovsky | d559f1e | 2012-02-02 22:13:37 +0200 | [diff] [blame] | 38 | #define GPIO_HP_DET BIT(0) |
| 39 | |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 40 | struct tegra_alc5632 { |
| 41 | struct tegra_asoc_utils_data util_data; |
Leon Romanovsky | b4dc0a7 | 2012-01-31 09:26:59 +0200 | [diff] [blame] | 42 | struct platform_device *pcm_dev; |
Leon Romanovsky | d559f1e | 2012-02-02 22:13:37 +0200 | [diff] [blame] | 43 | int gpio_requested; |
| 44 | int gpio_hp_det; |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | static int tegra_alc5632_asoc_hw_params(struct snd_pcm_substream *substream, |
| 48 | struct snd_pcm_hw_params *params) |
| 49 | { |
| 50 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 51 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 52 | struct snd_soc_codec *codec = rtd->codec; |
| 53 | struct snd_soc_card *card = codec->card; |
| 54 | struct tegra_alc5632 *alc5632 = snd_soc_card_get_drvdata(card); |
| 55 | int srate, mclk; |
| 56 | int err; |
| 57 | |
| 58 | srate = params_rate(params); |
| 59 | mclk = 512 * srate; |
| 60 | |
| 61 | err = tegra_asoc_utils_set_rate(&alc5632->util_data, srate, mclk); |
| 62 | if (err < 0) { |
| 63 | dev_err(card->dev, "Can't configure clocks\n"); |
| 64 | return err; |
| 65 | } |
| 66 | |
| 67 | err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk, |
| 68 | SND_SOC_CLOCK_IN); |
| 69 | if (err < 0) { |
| 70 | dev_err(card->dev, "codec_dai clock not set\n"); |
| 71 | return err; |
| 72 | } |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | static struct snd_soc_ops tegra_alc5632_asoc_ops = { |
| 78 | .hw_params = tegra_alc5632_asoc_hw_params, |
| 79 | }; |
| 80 | |
| 81 | static struct snd_soc_jack tegra_alc5632_hs_jack; |
| 82 | |
| 83 | static struct snd_soc_jack_pin tegra_alc5632_hs_jack_pins[] = { |
| 84 | { |
| 85 | .pin = "Headset Mic", |
| 86 | .mask = SND_JACK_MICROPHONE, |
| 87 | }, |
| 88 | { |
| 89 | .pin = "Headset Stereophone", |
| 90 | .mask = SND_JACK_HEADPHONE, |
| 91 | }, |
| 92 | }; |
| 93 | |
Leon Romanovsky | d559f1e | 2012-02-02 22:13:37 +0200 | [diff] [blame] | 94 | static struct snd_soc_jack_gpio tegra_alc5632_hp_jack_gpio = { |
| 95 | .name = "Headset detection", |
| 96 | .report = SND_JACK_HEADSET, |
| 97 | .debounce_time = 150, |
Leon Romanovsky | d559f1e | 2012-02-02 22:13:37 +0200 | [diff] [blame] | 98 | }; |
| 99 | |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 100 | static const struct snd_soc_dapm_widget tegra_alc5632_dapm_widgets[] = { |
| 101 | SND_SOC_DAPM_SPK("Int Spk", NULL), |
| 102 | SND_SOC_DAPM_HP("Headset Stereophone", NULL), |
| 103 | SND_SOC_DAPM_MIC("Headset Mic", NULL), |
Leon Romanovsky | dd7d3a2 | 2012-02-13 21:27:36 +0200 | [diff] [blame] | 104 | SND_SOC_DAPM_MIC("Digital Mic", NULL), |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 105 | }; |
| 106 | |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 107 | static const struct snd_kcontrol_new tegra_alc5632_controls[] = { |
| 108 | SOC_DAPM_PIN_SWITCH("Int Spk"), |
| 109 | }; |
| 110 | |
| 111 | static int tegra_alc5632_asoc_init(struct snd_soc_pcm_runtime *rtd) |
| 112 | { |
| 113 | struct snd_soc_codec *codec = rtd->codec; |
| 114 | struct snd_soc_dapm_context *dapm = &codec->dapm; |
Leon Romanovsky | d559f1e | 2012-02-02 22:13:37 +0200 | [diff] [blame] | 115 | struct device_node *np = codec->card->dev->of_node; |
| 116 | struct tegra_alc5632 *machine = snd_soc_card_get_drvdata(codec->card); |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 117 | |
| 118 | snd_soc_jack_new(codec, "Headset Jack", SND_JACK_HEADSET, |
| 119 | &tegra_alc5632_hs_jack); |
| 120 | snd_soc_jack_add_pins(&tegra_alc5632_hs_jack, |
| 121 | ARRAY_SIZE(tegra_alc5632_hs_jack_pins), |
| 122 | tegra_alc5632_hs_jack_pins); |
| 123 | |
Leon Romanovsky | d559f1e | 2012-02-02 22:13:37 +0200 | [diff] [blame] | 124 | machine->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0); |
| 125 | |
| 126 | if (gpio_is_valid(machine->gpio_hp_det)) { |
| 127 | tegra_alc5632_hp_jack_gpio.gpio = machine->gpio_hp_det; |
| 128 | snd_soc_jack_add_gpios(&tegra_alc5632_hs_jack, |
| 129 | 1, |
| 130 | &tegra_alc5632_hp_jack_gpio); |
| 131 | machine->gpio_requested |= GPIO_HP_DET; |
| 132 | } |
| 133 | |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 134 | snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1"); |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static struct snd_soc_dai_link tegra_alc5632_dai = { |
| 140 | .name = "ALC5632", |
| 141 | .stream_name = "ALC5632 PCM", |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 142 | .platform_name = "tegra-pcm-audio", |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 143 | .codec_dai_name = "alc5632-hifi", |
| 144 | .init = tegra_alc5632_asoc_init, |
| 145 | .ops = &tegra_alc5632_asoc_ops, |
| 146 | .dai_fmt = SND_SOC_DAIFMT_I2S |
| 147 | | SND_SOC_DAIFMT_NB_NF |
| 148 | | SND_SOC_DAIFMT_CBS_CFS, |
| 149 | }; |
| 150 | |
| 151 | static struct snd_soc_card snd_soc_tegra_alc5632 = { |
| 152 | .name = "tegra-alc5632", |
Axel Lin | b16eaf9 | 2011-12-22 21:23:01 +0800 | [diff] [blame] | 153 | .owner = THIS_MODULE, |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 154 | .dai_link = &tegra_alc5632_dai, |
| 155 | .num_links = 1, |
| 156 | .controls = tegra_alc5632_controls, |
| 157 | .num_controls = ARRAY_SIZE(tegra_alc5632_controls), |
| 158 | .dapm_widgets = tegra_alc5632_dapm_widgets, |
| 159 | .num_dapm_widgets = ARRAY_SIZE(tegra_alc5632_dapm_widgets), |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 160 | .fully_routed = true, |
| 161 | }; |
| 162 | |
| 163 | static __devinit int tegra_alc5632_probe(struct platform_device *pdev) |
| 164 | { |
| 165 | struct snd_soc_card *card = &snd_soc_tegra_alc5632; |
| 166 | struct tegra_alc5632 *alc5632; |
| 167 | int ret; |
| 168 | |
| 169 | alc5632 = devm_kzalloc(&pdev->dev, |
| 170 | sizeof(struct tegra_alc5632), GFP_KERNEL); |
| 171 | if (!alc5632) { |
| 172 | dev_err(&pdev->dev, "Can't allocate tegra_alc5632\n"); |
Leon Romanovsky | b4dc0a7 | 2012-01-31 09:26:59 +0200 | [diff] [blame] | 173 | ret = -ENOMEM; |
| 174 | goto err; |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 175 | } |
| 176 | |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 177 | card->dev = &pdev->dev; |
| 178 | platform_set_drvdata(pdev, card); |
| 179 | snd_soc_card_set_drvdata(card, alc5632); |
| 180 | |
Leon Romanovsky | b4dc0a7 | 2012-01-31 09:26:59 +0200 | [diff] [blame] | 181 | alc5632->pcm_dev = ERR_PTR(-EINVAL); |
| 182 | |
| 183 | if (!(pdev->dev.of_node)) { |
| 184 | dev_err(&pdev->dev, "Must be instantiated using device tree\n"); |
| 185 | ret = -EINVAL; |
| 186 | goto err; |
| 187 | } |
| 188 | |
| 189 | ret = snd_soc_of_parse_card_name(card, "nvidia,model"); |
| 190 | if (ret) |
| 191 | goto err; |
| 192 | |
| 193 | ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing"); |
| 194 | if (ret) |
| 195 | goto err; |
| 196 | |
| 197 | tegra_alc5632_dai.codec_of_node = of_parse_phandle( |
| 198 | pdev->dev.of_node, "nvidia,audio-codec", 0); |
| 199 | |
| 200 | if (!tegra_alc5632_dai.codec_of_node) { |
| 201 | dev_err(&pdev->dev, |
| 202 | "Property 'nvidia,audio-codec' missing or invalid\n"); |
| 203 | ret = -EINVAL; |
| 204 | goto err; |
| 205 | } |
| 206 | |
| 207 | tegra_alc5632_dai.cpu_dai_of_node = of_parse_phandle( |
| 208 | pdev->dev.of_node, "nvidia,i2s-controller", 0); |
| 209 | if (!tegra_alc5632_dai.cpu_dai_of_node) { |
| 210 | dev_err(&pdev->dev, |
| 211 | "Property 'nvidia,i2s-controller' missing or invalid\n"); |
| 212 | ret = -EINVAL; |
| 213 | goto err; |
| 214 | } |
| 215 | |
| 216 | alc5632->pcm_dev = platform_device_register_simple( |
| 217 | "tegra-pcm-audio", -1, NULL, 0); |
| 218 | if (IS_ERR(alc5632->pcm_dev)) { |
| 219 | dev_err(&pdev->dev, |
| 220 | "Can't instantiate tegra-pcm-audio\n"); |
| 221 | ret = PTR_ERR(alc5632->pcm_dev); |
| 222 | goto err; |
| 223 | } |
| 224 | |
| 225 | ret = tegra_asoc_utils_init(&alc5632->util_data, &pdev->dev); |
| 226 | if (ret) |
| 227 | goto err_unregister; |
| 228 | |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 229 | ret = snd_soc_register_card(card); |
| 230 | if (ret) { |
| 231 | dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", |
| 232 | ret); |
Leon Romanovsky | b4dc0a7 | 2012-01-31 09:26:59 +0200 | [diff] [blame] | 233 | goto err_fini_utils; |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | return 0; |
Leon Romanovsky | b4dc0a7 | 2012-01-31 09:26:59 +0200 | [diff] [blame] | 237 | |
| 238 | err_fini_utils: |
| 239 | tegra_asoc_utils_fini(&alc5632->util_data); |
| 240 | err_unregister: |
| 241 | if (!IS_ERR(alc5632->pcm_dev)) |
| 242 | platform_device_unregister(alc5632->pcm_dev); |
| 243 | err: |
| 244 | return ret; |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | static int __devexit tegra_alc5632_remove(struct platform_device *pdev) |
| 248 | { |
| 249 | struct snd_soc_card *card = platform_get_drvdata(pdev); |
Leon Romanovsky | d559f1e | 2012-02-02 22:13:37 +0200 | [diff] [blame] | 250 | struct tegra_alc5632 *machine = snd_soc_card_get_drvdata(card); |
| 251 | |
| 252 | if (machine->gpio_requested & GPIO_HP_DET) |
| 253 | snd_soc_jack_free_gpios(&tegra_alc5632_hs_jack, |
| 254 | 1, |
| 255 | &tegra_alc5632_hp_jack_gpio); |
| 256 | machine->gpio_requested = 0; |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 257 | |
| 258 | snd_soc_unregister_card(card); |
| 259 | |
Leon Romanovsky | d559f1e | 2012-02-02 22:13:37 +0200 | [diff] [blame] | 260 | tegra_asoc_utils_fini(&machine->util_data); |
| 261 | if (!IS_ERR(machine->pcm_dev)) |
| 262 | platform_device_unregister(machine->pcm_dev); |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
Leon Romanovsky | b4dc0a7 | 2012-01-31 09:26:59 +0200 | [diff] [blame] | 267 | static const struct of_device_id tegra_alc5632_of_match[] __devinitconst = { |
| 268 | { .compatible = "nvidia,tegra-audio-alc5632", }, |
| 269 | {}, |
| 270 | }; |
| 271 | |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 272 | static struct platform_driver tegra_alc5632_driver = { |
| 273 | .driver = { |
| 274 | .name = DRV_NAME, |
| 275 | .owner = THIS_MODULE, |
| 276 | .pm = &snd_soc_pm_ops, |
Leon Romanovsky | b4dc0a7 | 2012-01-31 09:26:59 +0200 | [diff] [blame] | 277 | .of_match_table = tegra_alc5632_of_match, |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 278 | }, |
| 279 | .probe = tegra_alc5632_probe, |
| 280 | .remove = __devexit_p(tegra_alc5632_remove), |
| 281 | }; |
| 282 | module_platform_driver(tegra_alc5632_driver); |
| 283 | |
| 284 | MODULE_AUTHOR("Leon Romanovsky <leon@leon.nu>"); |
| 285 | MODULE_DESCRIPTION("Tegra+ALC5632 machine ASoC driver"); |
| 286 | MODULE_LICENSE("GPL"); |
| 287 | MODULE_ALIAS("platform:" DRV_NAME); |
Leon Romanovsky | b4dc0a7 | 2012-01-31 09:26:59 +0200 | [diff] [blame] | 288 | MODULE_DEVICE_TABLE(of, tegra_alc5632_of_match); |