aboutsummaryrefslogtreecommitdiffstats
path: root/sound/soc/xilinx/xilinx-dp-codec.c
blob: af6e6b08c415e0309c7dd90bdfa7c7a606fa1042 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * Xilinx DisplayPort Sound Codec support
 *
 *  Copyright (C) 2015 Xilinx, Inc.
 *
 *  Author: Hyun Woo Kwon <hyunk@xilinx.com>
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/clk.h>
#include <linux/device.h>
#include <linux/module.h>
#include <linux/platform_device.h>

#include <sound/soc.h>

/**
 * struct xilinx_dp_codec - DisplayPort codec
 * @aud_clk: audio clock
 */
struct xilinx_dp_codec {
	struct clk *aud_clk;
};

struct xilinx_dp_codec_fmt {
	unsigned long rate;
	unsigned int snd_rate;
};

static struct snd_soc_dai_driver xilinx_dp_codec_dai = {
	.name		= "xilinx-dp-snd-codec-dai",
	.playback	= {
		.channels_min	= 2,
		.channels_max	= 2,
		.rates		= SNDRV_PCM_RATE_44100,
		.formats	= SNDRV_PCM_FMTBIT_S16_LE,
	},
};

static const struct xilinx_dp_codec_fmt rates[] = {
	{
		.rate	= 48000 * 512,
		.snd_rate = SNDRV_PCM_RATE_48000
	},
	{
		.rate	= 44100 * 512,
		.snd_rate = SNDRV_PCM_RATE_44100
	}
};

static const struct snd_soc_component_driver xilinx_dp_component_driver = {
	.idle_bias_on		= 1,
	.use_pmdown_time	= 1,
	.endianness		= 1,
	.non_legacy_dai_naming	= 1,
};

static int xilinx_dp_codec_probe(struct platform_device *pdev)
{
	struct xilinx_dp_codec *codec;
	unsigned int i;
	unsigned long rate;
	int ret;

	codec = devm_kzalloc(&pdev->dev, sizeof(*codec), GFP_KERNEL);
	if (!codec)
		return -ENOMEM;

	codec->aud_clk = devm_clk_get(&pdev->dev, NULL);
	if (IS_ERR(codec->aud_clk))
		return PTR_ERR(codec->aud_clk);

	ret = clk_prepare_enable(codec->aud_clk);
	if (ret) {
		dev_err(&pdev->dev, "failed to enable the aud_clk\n");
		return ret;
	}

	for (i = 0; i < ARRAY_SIZE(rates); i++) {
		clk_disable_unprepare(codec->aud_clk);
		ret = clk_set_rate(codec->aud_clk, rates[i].rate);
		clk_prepare_enable(codec->aud_clk);
		if (ret)
			continue;

		rate = clk_get_rate(codec->aud_clk);
		/* Ignore some offset +- 10 */
		if (abs(rates[i].rate - rate) < 10) {
			xilinx_dp_codec_dai.playback.rates = rates[i].snd_rate;
			break;
		}
		ret = -EINVAL;
	}

	if (ret) {
		dev_err(&pdev->dev, "Failed to get required clock freq\n");
		goto error_clk;
	}

	ret = devm_snd_soc_register_component(&pdev->dev,
					      &xilinx_dp_component_driver,
					      &xilinx_dp_codec_dai, 1);
	if (ret)
		goto error_clk;

	platform_set_drvdata(pdev, codec);

	dev_info(&pdev->dev, "Xilinx DisplayPort Sound Codec probed\n");

	return 0;

error_clk:
	clk_disable_unprepare(codec->aud_clk);
	return ret;
}

static int xilinx_dp_codec_dev_remove(struct platform_device *pdev)
{
	struct xilinx_dp_codec *codec = platform_get_drvdata(pdev);

	clk_disable_unprepare(codec->aud_clk);

	return 0;
}

static int __maybe_unused xilinx_dp_codec_pm_suspend(struct device *dev)
{
	struct xilinx_dp_codec *codec = dev_get_drvdata(dev);

	clk_disable_unprepare(codec->aud_clk);

	return 0;
}

static int __maybe_unused xilinx_dp_codec_pm_resume(struct device *dev)
{
	struct xilinx_dp_codec *codec = dev_get_drvdata(dev);
	int ret;

	ret = clk_prepare_enable(codec->aud_clk);
	if (ret)
		dev_err(dev, "failed to enable the aud_clk\n");

	return ret;
}

static const struct dev_pm_ops xilinx_dp_codec_pm_ops = {
	SET_SYSTEM_SLEEP_PM_OPS(xilinx_dp_codec_pm_suspend,
				xilinx_dp_codec_pm_resume)
};

static const struct of_device_id xilinx_dp_codec_of_match[] = {
	{ .compatible = "xlnx,dp-snd-codec", },
	{ /* end of table */ },
};
MODULE_DEVICE_TABLE(of, xilinx_dp_codec_of_match);

static struct platform_driver xilinx_dp_codec_driver = {
	.driver	= {
		.name		= "xilinx-dp-snd-codec",
		.of_match_table	= xilinx_dp_codec_of_match,
		.pm		= &xilinx_dp_codec_pm_ops,
	},
	.probe	= xilinx_dp_codec_probe,
	.remove	= xilinx_dp_codec_dev_remove,
};
module_platform_driver(xilinx_dp_codec_driver);

MODULE_DESCRIPTION("Xilinx DisplayPort Sound Codec module");
MODULE_LICENSE("GPL v2");