1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * ac97.c  --  ALSA Soc AC97 codec support
4 *
5 * Copyright 2005 Wolfson Microelectronics PLC.
6 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
7 *
8 * Generic AC97 support.
9 */
10
11#include <linux/init.h>
12#include <linux/slab.h>
13#include <linux/kernel.h>
14#include <linux/device.h>
15#include <linux/module.h>
16#include <sound/core.h>
17#include <sound/pcm.h>
18#include <sound/ac97_codec.h>
19#include <sound/initval.h>
20#include <sound/soc.h>
21
22static const struct snd_soc_dapm_widget ac97_widgets[] = {
23	SND_SOC_DAPM_INPUT("RX"),
24	SND_SOC_DAPM_OUTPUT("TX"),
25};
26
27static const struct snd_soc_dapm_route ac97_routes[] = {
28	{ "AC97 Capture", NULL, "RX" },
29	{ "TX", NULL, "AC97 Playback" },
30};
31
32static int ac97_prepare(struct snd_pcm_substream *substream,
33			struct snd_soc_dai *dai)
34{
35	struct snd_soc_component *component = dai->component;
36	struct snd_ac97 *ac97 = snd_soc_component_get_drvdata(component);
37
38	int reg = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
39		  AC97_PCM_FRONT_DAC_RATE : AC97_PCM_LR_ADC_RATE;
40	return snd_ac97_set_rate(ac97, reg, substream->runtime->rate);
41}
42
43static const struct snd_soc_dai_ops ac97_dai_ops = {
44	.prepare	= ac97_prepare,
45};
46
47static struct snd_soc_dai_driver ac97_dai = {
48	.name = "ac97-hifi",
49	.playback = {
50		.stream_name = "AC97 Playback",
51		.channels_min = 1,
52		.channels_max = 2,
53		.rates = SNDRV_PCM_RATE_KNOT,
54		.formats = SND_SOC_STD_AC97_FMTS,},
55	.capture = {
56		.stream_name = "AC97 Capture",
57		.channels_min = 1,
58		.channels_max = 2,
59		.rates = SNDRV_PCM_RATE_KNOT,
60		.formats = SND_SOC_STD_AC97_FMTS,},
61	.ops = &ac97_dai_ops,
62};
63
64static int ac97_soc_probe(struct snd_soc_component *component)
65{
66	struct snd_ac97 *ac97;
67	struct snd_ac97_bus *ac97_bus;
68	struct snd_ac97_template ac97_template;
69	int ret;
70
71	/* add codec as bus device for standard ac97 */
72	ret = snd_ac97_bus(component->card->snd_card, 0, soc_ac97_ops,
73			   NULL, &ac97_bus);
74	if (ret < 0)
75		return ret;
76
77	memset(&ac97_template, 0, sizeof(struct snd_ac97_template));
78	ret = snd_ac97_mixer(ac97_bus, &ac97_template, &ac97);
79	if (ret < 0)
80		return ret;
81
82	snd_soc_component_set_drvdata(component, ac97);
83
84	return 0;
85}
86
87#ifdef CONFIG_PM
88static int ac97_soc_suspend(struct snd_soc_component *component)
89{
90	struct snd_ac97 *ac97 = snd_soc_component_get_drvdata(component);
91
92	snd_ac97_suspend(ac97);
93
94	return 0;
95}
96
97static int ac97_soc_resume(struct snd_soc_component *component)
98{
99
100	struct snd_ac97 *ac97 = snd_soc_component_get_drvdata(component);
101
102	snd_ac97_resume(ac97);
103
104	return 0;
105}
106#else
107#define ac97_soc_suspend NULL
108#define ac97_soc_resume NULL
109#endif
110
111static const struct snd_soc_component_driver soc_component_dev_ac97 = {
112	.probe			= ac97_soc_probe,
113	.suspend		= ac97_soc_suspend,
114	.resume			= ac97_soc_resume,
115	.dapm_widgets		= ac97_widgets,
116	.num_dapm_widgets	= ARRAY_SIZE(ac97_widgets),
117	.dapm_routes		= ac97_routes,
118	.num_dapm_routes	= ARRAY_SIZE(ac97_routes),
119	.idle_bias_on		= 1,
120	.use_pmdown_time	= 1,
121	.endianness		= 1,
122};
123
124static int ac97_probe(struct platform_device *pdev)
125{
126	return devm_snd_soc_register_component(&pdev->dev,
127			&soc_component_dev_ac97, &ac97_dai, 1);
128}
129
130static struct platform_driver ac97_codec_driver = {
131	.driver = {
132		.name = "ac97-codec",
133	},
134
135	.probe = ac97_probe,
136};
137
138module_platform_driver(ac97_codec_driver);
139
140MODULE_DESCRIPTION("Soc Generic AC97 driver");
141MODULE_AUTHOR("Liam Girdwood");
142MODULE_LICENSE("GPL");
143MODULE_ALIAS("platform:ac97-codec");
144