Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 1 | /* |
Stephen Warren | 1ae93b9 | 2012-03-20 14:55:48 -0600 | [diff] [blame] | 2 | * tegra_alc5632.c -- Toshiba AC100(PAZ00) machine ASoC driver |
| 3 | * |
| 4 | * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.lauchpad.net> |
Stephen Warren | 518de86 | 2012-03-20 14:55:49 -0600 | [diff] [blame^] | 5 | * Copyright (C) 2012 - NVIDIA, Inc. |
Stephen Warren | 1ae93b9 | 2012-03-20 14:55:48 -0600 | [diff] [blame] | 6 | * |
| 7 | * Authors: Leon Romanovsky <leon@leon.nu> |
| 8 | * Andrey Danin <danindrey@mail.ru> |
| 9 | * Marc Dietrich <marvin24@gmx.de> |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License version 2 as |
| 13 | * published by the Free Software Foundation. |
| 14 | */ |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 15 | |
| 16 | #include <asm/mach-types.h> |
| 17 | |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/gpio.h> |
Leon Romanovsky | d559f1e | 2012-02-02 22:13:37 +0200 | [diff] [blame] | 22 | #include <linux/of_gpio.h> |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 23 | |
| 24 | #include <sound/core.h> |
| 25 | #include <sound/jack.h> |
| 26 | #include <sound/pcm.h> |
| 27 | #include <sound/pcm_params.h> |
| 28 | #include <sound/soc.h> |
| 29 | |
| 30 | #include "../codecs/alc5632.h" |
| 31 | |
| 32 | #include "tegra_das.h" |
| 33 | #include "tegra_i2s.h" |
| 34 | #include "tegra_pcm.h" |
| 35 | #include "tegra_asoc_utils.h" |
| 36 | |
| 37 | #define DRV_NAME "tegra-alc5632" |
| 38 | |
Leon Romanovsky | d559f1e | 2012-02-02 22:13:37 +0200 | [diff] [blame] | 39 | #define GPIO_HP_DET BIT(0) |
| 40 | |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 41 | struct tegra_alc5632 { |
| 42 | struct tegra_asoc_utils_data util_data; |
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, |
| 98 | .invert = 1, |
| 99 | }; |
| 100 | |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 101 | static const struct snd_soc_dapm_widget tegra_alc5632_dapm_widgets[] = { |
| 102 | SND_SOC_DAPM_SPK("Int Spk", NULL), |
| 103 | SND_SOC_DAPM_HP("Headset Stereophone", NULL), |
| 104 | SND_SOC_DAPM_MIC("Headset Mic", NULL), |
Leon Romanovsky | dd7d3a2 | 2012-02-13 21:27:36 +0200 | [diff] [blame] | 105 | SND_SOC_DAPM_MIC("Digital Mic", NULL), |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 106 | }; |
| 107 | |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 108 | static const struct snd_kcontrol_new tegra_alc5632_controls[] = { |
| 109 | SOC_DAPM_PIN_SWITCH("Int Spk"), |
| 110 | }; |
| 111 | |
| 112 | static int tegra_alc5632_asoc_init(struct snd_soc_pcm_runtime *rtd) |
| 113 | { |
| 114 | struct snd_soc_codec *codec = rtd->codec; |
| 115 | struct snd_soc_dapm_context *dapm = &codec->dapm; |
Leon Romanovsky | d559f1e | 2012-02-02 22:13:37 +0200 | [diff] [blame] | 116 | struct device_node *np = codec->card->dev->of_node; |
| 117 | struct tegra_alc5632 *machine = snd_soc_card_get_drvdata(codec->card); |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 118 | |
| 119 | snd_soc_jack_new(codec, "Headset Jack", SND_JACK_HEADSET, |
| 120 | &tegra_alc5632_hs_jack); |
| 121 | snd_soc_jack_add_pins(&tegra_alc5632_hs_jack, |
| 122 | ARRAY_SIZE(tegra_alc5632_hs_jack_pins), |
| 123 | tegra_alc5632_hs_jack_pins); |
| 124 | |
Leon Romanovsky | d559f1e | 2012-02-02 22:13:37 +0200 | [diff] [blame] | 125 | machine->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0); |
| 126 | |
| 127 | if (gpio_is_valid(machine->gpio_hp_det)) { |
| 128 | tegra_alc5632_hp_jack_gpio.gpio = machine->gpio_hp_det; |
| 129 | snd_soc_jack_add_gpios(&tegra_alc5632_hs_jack, |
| 130 | 1, |
| 131 | &tegra_alc5632_hp_jack_gpio); |
| 132 | machine->gpio_requested |= GPIO_HP_DET; |
| 133 | } |
| 134 | |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 135 | snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1"); |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | static struct snd_soc_dai_link tegra_alc5632_dai = { |
| 141 | .name = "ALC5632", |
| 142 | .stream_name = "ALC5632 PCM", |
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 | if (!(pdev->dev.of_node)) { |
| 182 | dev_err(&pdev->dev, "Must be instantiated using device tree\n"); |
| 183 | ret = -EINVAL; |
| 184 | goto err; |
| 185 | } |
| 186 | |
| 187 | ret = snd_soc_of_parse_card_name(card, "nvidia,model"); |
| 188 | if (ret) |
| 189 | goto err; |
| 190 | |
| 191 | ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing"); |
| 192 | if (ret) |
| 193 | goto err; |
| 194 | |
| 195 | tegra_alc5632_dai.codec_of_node = of_parse_phandle( |
| 196 | pdev->dev.of_node, "nvidia,audio-codec", 0); |
| 197 | |
| 198 | if (!tegra_alc5632_dai.codec_of_node) { |
| 199 | dev_err(&pdev->dev, |
| 200 | "Property 'nvidia,audio-codec' missing or invalid\n"); |
| 201 | ret = -EINVAL; |
| 202 | goto err; |
| 203 | } |
| 204 | |
| 205 | tegra_alc5632_dai.cpu_dai_of_node = of_parse_phandle( |
| 206 | pdev->dev.of_node, "nvidia,i2s-controller", 0); |
| 207 | if (!tegra_alc5632_dai.cpu_dai_of_node) { |
| 208 | dev_err(&pdev->dev, |
| 209 | "Property 'nvidia,i2s-controller' missing or invalid\n"); |
| 210 | ret = -EINVAL; |
| 211 | goto err; |
| 212 | } |
| 213 | |
Stephen Warren | 518de86 | 2012-03-20 14:55:49 -0600 | [diff] [blame^] | 214 | tegra_alc5632_dai.platform_of_node = tegra_alc5632_dai.cpu_dai_of_node; |
Leon Romanovsky | b4dc0a7 | 2012-01-31 09:26:59 +0200 | [diff] [blame] | 215 | |
| 216 | ret = tegra_asoc_utils_init(&alc5632->util_data, &pdev->dev); |
| 217 | if (ret) |
Stephen Warren | 518de86 | 2012-03-20 14:55:49 -0600 | [diff] [blame^] | 218 | goto err; |
Leon Romanovsky | b4dc0a7 | 2012-01-31 09:26:59 +0200 | [diff] [blame] | 219 | |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 220 | ret = snd_soc_register_card(card); |
| 221 | if (ret) { |
| 222 | dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", |
| 223 | ret); |
Leon Romanovsky | b4dc0a7 | 2012-01-31 09:26:59 +0200 | [diff] [blame] | 224 | goto err_fini_utils; |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | return 0; |
Leon Romanovsky | b4dc0a7 | 2012-01-31 09:26:59 +0200 | [diff] [blame] | 228 | |
| 229 | err_fini_utils: |
| 230 | tegra_asoc_utils_fini(&alc5632->util_data); |
Leon Romanovsky | b4dc0a7 | 2012-01-31 09:26:59 +0200 | [diff] [blame] | 231 | err: |
| 232 | return ret; |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | static int __devexit tegra_alc5632_remove(struct platform_device *pdev) |
| 236 | { |
| 237 | struct snd_soc_card *card = platform_get_drvdata(pdev); |
Leon Romanovsky | d559f1e | 2012-02-02 22:13:37 +0200 | [diff] [blame] | 238 | struct tegra_alc5632 *machine = snd_soc_card_get_drvdata(card); |
| 239 | |
| 240 | if (machine->gpio_requested & GPIO_HP_DET) |
| 241 | snd_soc_jack_free_gpios(&tegra_alc5632_hs_jack, |
| 242 | 1, |
| 243 | &tegra_alc5632_hp_jack_gpio); |
| 244 | machine->gpio_requested = 0; |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 245 | |
| 246 | snd_soc_unregister_card(card); |
| 247 | |
Leon Romanovsky | d559f1e | 2012-02-02 22:13:37 +0200 | [diff] [blame] | 248 | tegra_asoc_utils_fini(&machine->util_data); |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | |
Leon Romanovsky | b4dc0a7 | 2012-01-31 09:26:59 +0200 | [diff] [blame] | 253 | static const struct of_device_id tegra_alc5632_of_match[] __devinitconst = { |
| 254 | { .compatible = "nvidia,tegra-audio-alc5632", }, |
| 255 | {}, |
| 256 | }; |
| 257 | |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 258 | static struct platform_driver tegra_alc5632_driver = { |
| 259 | .driver = { |
| 260 | .name = DRV_NAME, |
| 261 | .owner = THIS_MODULE, |
| 262 | .pm = &snd_soc_pm_ops, |
Leon Romanovsky | b4dc0a7 | 2012-01-31 09:26:59 +0200 | [diff] [blame] | 263 | .of_match_table = tegra_alc5632_of_match, |
Leon Romanovsky | 58783fa | 2011-12-19 21:51:52 +0200 | [diff] [blame] | 264 | }, |
| 265 | .probe = tegra_alc5632_probe, |
| 266 | .remove = __devexit_p(tegra_alc5632_remove), |
| 267 | }; |
| 268 | module_platform_driver(tegra_alc5632_driver); |
| 269 | |
| 270 | MODULE_AUTHOR("Leon Romanovsky <leon@leon.nu>"); |
| 271 | MODULE_DESCRIPTION("Tegra+ALC5632 machine ASoC driver"); |
| 272 | MODULE_LICENSE("GPL"); |
| 273 | MODULE_ALIAS("platform:" DRV_NAME); |
Leon Romanovsky | b4dc0a7 | 2012-01-31 09:26:59 +0200 | [diff] [blame] | 274 | MODULE_DEVICE_TABLE(of, tegra_alc5632_of_match); |