Merge branch 'nvme-5.2-rc-next' of git://git.infradead.org/nvme into for-linus
[sfrench/cifs-2.6.git] / sound / soc / tegra / tegra_wm9712.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * tegra20_wm9712.c - Tegra machine ASoC driver for boards using WM9712 codec.
4  *
5  * Copyright 2012 Lucas Stach <dev@lynxeye.de>
6  *
7  * Partly based on code copyright/by:
8  * Copyright 2011,2012 Toradex Inc.
9  */
10
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/slab.h>
14 #include <linux/gpio.h>
15 #include <linux/of_gpio.h>
16
17 #include <sound/core.h>
18 #include <sound/jack.h>
19 #include <sound/pcm.h>
20 #include <sound/pcm_params.h>
21 #include <sound/soc.h>
22
23 #include "tegra_asoc_utils.h"
24
25 #define DRV_NAME "tegra-snd-wm9712"
26
27 struct tegra_wm9712 {
28         struct platform_device *codec;
29         struct tegra_asoc_utils_data util_data;
30 };
31
32 static const struct snd_soc_dapm_widget tegra_wm9712_dapm_widgets[] = {
33         SND_SOC_DAPM_HP("Headphone", NULL),
34         SND_SOC_DAPM_LINE("LineIn", NULL),
35         SND_SOC_DAPM_MIC("Mic", NULL),
36 };
37
38 static int tegra_wm9712_init(struct snd_soc_pcm_runtime *rtd)
39 {
40         return snd_soc_dapm_force_enable_pin(&rtd->card->dapm, "Mic Bias");
41 }
42
43 static struct snd_soc_dai_link tegra_wm9712_dai = {
44         .name = "AC97 HiFi",
45         .stream_name = "AC97 HiFi",
46         .codec_dai_name = "wm9712-hifi",
47         .codec_name = "wm9712-codec",
48         .init = tegra_wm9712_init,
49 };
50
51 static struct snd_soc_card snd_soc_tegra_wm9712 = {
52         .name = "tegra-wm9712",
53         .owner = THIS_MODULE,
54         .dai_link = &tegra_wm9712_dai,
55         .num_links = 1,
56
57         .dapm_widgets = tegra_wm9712_dapm_widgets,
58         .num_dapm_widgets = ARRAY_SIZE(tegra_wm9712_dapm_widgets),
59         .fully_routed = true,
60 };
61
62 static int tegra_wm9712_driver_probe(struct platform_device *pdev)
63 {
64         struct device_node *np = pdev->dev.of_node;
65         struct snd_soc_card *card = &snd_soc_tegra_wm9712;
66         struct tegra_wm9712 *machine;
67         int ret;
68
69         machine = devm_kzalloc(&pdev->dev, sizeof(struct tegra_wm9712),
70                                GFP_KERNEL);
71         if (!machine)
72                 return -ENOMEM;
73
74         card->dev = &pdev->dev;
75         snd_soc_card_set_drvdata(card, machine);
76
77         machine->codec = platform_device_alloc("wm9712-codec", -1);
78         if (!machine->codec) {
79                 dev_err(&pdev->dev, "Can't allocate wm9712 platform device\n");
80                 return -ENOMEM;
81         }
82
83         ret = platform_device_add(machine->codec);
84         if (ret)
85                 goto codec_put;
86
87         ret = snd_soc_of_parse_card_name(card, "nvidia,model");
88         if (ret)
89                 goto codec_unregister;
90
91         ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
92         if (ret)
93                 goto codec_unregister;
94
95         tegra_wm9712_dai.cpu_of_node = of_parse_phandle(np,
96                                        "nvidia,ac97-controller", 0);
97         if (!tegra_wm9712_dai.cpu_of_node) {
98                 dev_err(&pdev->dev,
99                         "Property 'nvidia,ac97-controller' missing or invalid\n");
100                 ret = -EINVAL;
101                 goto codec_unregister;
102         }
103
104         tegra_wm9712_dai.platform_of_node = tegra_wm9712_dai.cpu_of_node;
105
106         ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
107         if (ret)
108                 goto codec_unregister;
109
110         ret = tegra_asoc_utils_set_ac97_rate(&machine->util_data);
111         if (ret)
112                 goto asoc_utils_fini;
113
114         ret = snd_soc_register_card(card);
115         if (ret) {
116                 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
117                         ret);
118                 goto asoc_utils_fini;
119         }
120
121         return 0;
122
123 asoc_utils_fini:
124         tegra_asoc_utils_fini(&machine->util_data);
125 codec_unregister:
126         platform_device_del(machine->codec);
127 codec_put:
128         platform_device_put(machine->codec);
129         return ret;
130 }
131
132 static int tegra_wm9712_driver_remove(struct platform_device *pdev)
133 {
134         struct snd_soc_card *card = platform_get_drvdata(pdev);
135         struct tegra_wm9712 *machine = snd_soc_card_get_drvdata(card);
136
137         snd_soc_unregister_card(card);
138
139         tegra_asoc_utils_fini(&machine->util_data);
140
141         platform_device_unregister(machine->codec);
142
143         return 0;
144 }
145
146 static const struct of_device_id tegra_wm9712_of_match[] = {
147         { .compatible = "nvidia,tegra-audio-wm9712", },
148         {},
149 };
150
151 static struct platform_driver tegra_wm9712_driver = {
152         .driver = {
153                 .name = DRV_NAME,
154                 .pm = &snd_soc_pm_ops,
155                 .of_match_table = tegra_wm9712_of_match,
156         },
157         .probe = tegra_wm9712_driver_probe,
158         .remove = tegra_wm9712_driver_remove,
159 };
160 module_platform_driver(tegra_wm9712_driver);
161
162 MODULE_AUTHOR("Lucas Stach");
163 MODULE_DESCRIPTION("Tegra+WM9712 machine ASoC driver");
164 MODULE_LICENSE("GPL v2");
165 MODULE_ALIAS("platform:" DRV_NAME);
166 MODULE_DEVICE_TABLE(of, tegra_wm9712_of_match);