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
27struct tegra_wm9712 {
28	struct platform_device *codec;
29	struct tegra_asoc_utils_data util_data;
30};
31
32static 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
38static 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
43SND_SOC_DAILINK_DEFS(hifi,
44	DAILINK_COMP_ARRAY(COMP_EMPTY()),
45	DAILINK_COMP_ARRAY(COMP_CODEC("wm9712-codec", "wm9712-hifi")),
46	DAILINK_COMP_ARRAY(COMP_EMPTY()));
47
48static struct snd_soc_dai_link tegra_wm9712_dai = {
49	.name = "AC97 HiFi",
50	.stream_name = "AC97 HiFi",
51	.init = tegra_wm9712_init,
52	SND_SOC_DAILINK_REG(hifi),
53};
54
55static struct snd_soc_card snd_soc_tegra_wm9712 = {
56	.name = "tegra-wm9712",
57	.driver_name = "tegra",
58	.owner = THIS_MODULE,
59	.dai_link = &tegra_wm9712_dai,
60	.num_links = 1,
61
62	.dapm_widgets = tegra_wm9712_dapm_widgets,
63	.num_dapm_widgets = ARRAY_SIZE(tegra_wm9712_dapm_widgets),
64	.fully_routed = true,
65};
66
67static int tegra_wm9712_driver_probe(struct platform_device *pdev)
68{
69	struct device_node *np = pdev->dev.of_node;
70	struct snd_soc_card *card = &snd_soc_tegra_wm9712;
71	struct tegra_wm9712 *machine;
72	int ret;
73
74	machine = devm_kzalloc(&pdev->dev, sizeof(struct tegra_wm9712),
75			       GFP_KERNEL);
76	if (!machine)
77		return -ENOMEM;
78
79	card->dev = &pdev->dev;
80	snd_soc_card_set_drvdata(card, machine);
81
82	machine->codec = platform_device_alloc("wm9712-codec", -1);
83	if (!machine->codec) {
84		dev_err(&pdev->dev, "Can't allocate wm9712 platform device\n");
85		return -ENOMEM;
86	}
87
88	ret = platform_device_add(machine->codec);
89	if (ret)
90		goto codec_put;
91
92	ret = snd_soc_of_parse_card_name(card, "nvidia,model");
93	if (ret)
94		goto codec_unregister;
95
96	ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
97	if (ret)
98		goto codec_unregister;
99
100	tegra_wm9712_dai.cpus->of_node = of_parse_phandle(np,
101				       "nvidia,ac97-controller", 0);
102	if (!tegra_wm9712_dai.cpus->of_node) {
103		dev_err(&pdev->dev,
104			"Property 'nvidia,ac97-controller' missing or invalid\n");
105		ret = -EINVAL;
106		goto codec_unregister;
107	}
108
109	tegra_wm9712_dai.platforms->of_node = tegra_wm9712_dai.cpus->of_node;
110
111	ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
112	if (ret)
113		goto codec_unregister;
114
115	ret = tegra_asoc_utils_set_ac97_rate(&machine->util_data);
116	if (ret)
117		goto codec_unregister;
118
119	ret = snd_soc_register_card(card);
120	if (ret) {
121		dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
122			ret);
123		goto codec_unregister;
124	}
125
126	return 0;
127
128codec_unregister:
129	platform_device_del(machine->codec);
130codec_put:
131	platform_device_put(machine->codec);
132	return ret;
133}
134
135static int tegra_wm9712_driver_remove(struct platform_device *pdev)
136{
137	struct snd_soc_card *card = platform_get_drvdata(pdev);
138	struct tegra_wm9712 *machine = snd_soc_card_get_drvdata(card);
139
140	snd_soc_unregister_card(card);
141
142	platform_device_unregister(machine->codec);
143
144	return 0;
145}
146
147static const struct of_device_id tegra_wm9712_of_match[] = {
148	{ .compatible = "nvidia,tegra-audio-wm9712", },
149	{},
150};
151
152static struct platform_driver tegra_wm9712_driver = {
153	.driver = {
154		.name = DRV_NAME,
155		.pm = &snd_soc_pm_ops,
156		.of_match_table = tegra_wm9712_of_match,
157	},
158	.probe = tegra_wm9712_driver_probe,
159	.remove = tegra_wm9712_driver_remove,
160};
161module_platform_driver(tegra_wm9712_driver);
162
163MODULE_AUTHOR("Lucas Stach");
164MODULE_DESCRIPTION("Tegra+WM9712 machine ASoC driver");
165MODULE_LICENSE("GPL v2");
166MODULE_ALIAS("platform:" DRV_NAME);
167MODULE_DEVICE_TABLE(of, tegra_wm9712_of_match);
168