aboutsummaryrefslogtreecommitdiffstats
path: root/sound/soc/codecs/es7241.c
blob: 2344a0b03518164396c9c5f029461dd8fc1f3044 (plain) (blame)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// SPDX-License-Identifier: (GPL-2.0 OR MIT)
//
// Copyright (c) 2018 BayLibre, SAS.
// Author: Jerome Brunet <jbrunet@baylibre.com>

#include <linux/gpio/consumer.h>
#include <linux/of_platform.h>
#include <linux/module.h>
#include <sound/soc.h>

struct es7241_clock_mode {
	unsigned int rate_min;
	unsigned int rate_max;
	unsigned int *slv_mfs;
	unsigned int slv_mfs_num;
	unsigned int mst_mfs;
	unsigned int mst_m0:1;
	unsigned int mst_m1:1;
};

struct es7241_chip {
	const struct es7241_clock_mode *modes;
	unsigned int mode_num;
};

struct es7241_data {
	struct gpio_desc *reset;
	struct gpio_desc *m0;
	struct gpio_desc *m1;
	unsigned int fmt;
	unsigned int mclk;
	bool is_slave;
	const struct es7241_chip *chip;
};

static void es7241_set_mode(struct es7241_data *priv,  int m0, int m1)
{
	/* put the device in reset */
	gpiod_set_value_cansleep(priv->reset, 0);

	/* set the mode */
	gpiod_set_value_cansleep(priv->m0, m0);
	gpiod_set_value_cansleep(priv->m1, m1);

	/* take the device out of reset - datasheet does not specify a delay */
	gpiod_set_value_cansleep(priv->reset, 1);
}

static int es7241_set_slave_mode(struct es7241_data *priv,
				 const struct es7241_clock_mode *mode,
				 unsigned int mfs)
{
	int j;

	if (!mfs)
		goto out_ok;

	for (j = 0; j < mode->slv_mfs_num; j++) {
		if (mode->slv_mfs[j] == mfs)
			goto out_ok;
	}

	return -EINVAL;

out_ok:
	es7241_set_mode(priv, 1, 1);
	return 0;
}

static int es7241_set_master_mode(struct es7241_data *priv,
				  const struct es7241_clock_mode *mode,
				  unsigned int mfs)
{
	/*
	 * We can't really set clock ratio, if the mclk/lrclk is different
	 * from what we provide, then error out
	 */
	if (mfs && mfs != mode->mst_mfs)
		return -EINVAL;

	es7241_set_mode(priv, mode->mst_m0, mode->mst_m1);

	return 0;
}

static int es7241_hw_params(struct snd_pcm_substream *substream,
			    struct snd_pcm_hw_params *params,
			    struct snd_soc_dai *dai)
{
	struct es7241_data *priv = snd_soc_dai_get_drvdata(dai);
	unsigned int rate = params_rate(params);
	unsigned int mfs = priv->mclk / rate;
	int i;

	for (i = 0; i < priv->chip->mode_num; i++) {
		const struct es7241_clock_mode *mode = &priv->chip->modes[i];

		if (rate < mode->rate_min || rate >= mode->rate_max)
			continue;

		if (priv->is_slave)
			return es7241_set_slave_mode(priv, mode, mfs);
		else
			return es7241_set_master_mode(priv, mode, mfs);
	}

	/* should not happen */
	dev_err(dai->dev, "unsupported rate: %u\n", rate);
	return -EINVAL;
}

static int es7241_set_sysclk(struct snd_soc_dai *dai, int clk_id,
			     unsigned int freq, int dir)
{
	struct es7241_data *priv = snd_soc_dai_get_drvdata(dai);

	if (dir == SND_SOC_CLOCK_IN && clk_id == 0) {
		priv->mclk = freq;
		return 0;
	}

	return -ENOTSUPP;
}

static int es7241_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
{
	struct es7241_data *priv = snd_soc_dai_get_drvdata(dai);

	if ((fmt & SND_SOC_DAIFMT_INV_MASK) != SND_SOC_DAIFMT_NB_NF) {
		dev_err(dai->dev, "Unsupported dai clock inversion\n");
		return -EINVAL;
	}

	if ((fmt & SND_SOC_DAIFMT_FORMAT_MASK) != priv->fmt) {
		dev_err(dai->dev, "Invalid dai format\n");
		return -EINVAL;
	}

	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
	case SND_SOC_DAIFMT_CBS_CFS:
		priv->is_slave = true;
		break;
	case SND_SOC_DAIFMT_CBM_CFM:
		priv->is_slave = false;
		break;

	default:
		dev_err(dai->dev, "Unsupported clock configuration\n");
		return -EINVAL;
	}

	return 0;
}

static const struct snd_soc_dai_ops es7241_dai_ops = {
	.set_fmt	= es7241_set_fmt,
	.hw_params	= es7241_hw_params,
	.set_sysclk	= es7241_set_sysclk,
};

static struct snd_soc_dai_driver es7241_dai = {
	.name = "es7241-hifi",
	.capture = {
		.stream_name = "Capture",
		.channels_min = 2,
		.channels_max = 2,
		.rates = SNDRV_PCM_RATE_8000_192000,
		.formats = (SNDRV_PCM_FMTBIT_S16_LE  |
			    SNDRV_PCM_FMTBIT_S24_3LE |
			    SNDRV_PCM_FMTBIT_S24_LE),
	},
	.ops = &es7241_dai_ops,
};

static const struct es7241_clock_mode es7241_modes[] = {
	{
		/* Single speed mode */
		.rate_min = 8000,
		.rate_max = 50000,
		.slv_mfs = (unsigned int[]) { 256, 384, 512, 768, 1024 },
		.slv_mfs_num = 5,
		.mst_mfs = 256,
		.mst_m0 = 0,
		.mst_m1 = 0,
	}, {
		/* Double speed mode */
		.rate_min = 50000,
		.rate_max = 100000,
		.slv_mfs = (unsigned int[]) { 128, 192 },
		.slv_mfs_num = 2,
		.mst_mfs = 128,
		.mst_m0 = 1,
		.mst_m1 = 0,
	}, {
		/* Quad speed mode */
		.rate_min = 100000,
		.rate_max = 200000,
		.slv_mfs = (unsigned int[]) { 64 },
		.slv_mfs_num = 1,
		.mst_mfs = 64,
		.mst_m0 = 0,
		.mst_m1 = 1,
	},
};

static const struct es7241_chip es7241_chip __maybe_unused = {
	.modes = es7241_modes,
	.mode_num = ARRAY_SIZE(es7241_modes),
};

static const struct snd_soc_dapm_widget es7241_dapm_widgets[] = {
	SND_SOC_DAPM_INPUT("AINL"),
	SND_SOC_DAPM_INPUT("AINR"),
	SND_SOC_DAPM_DAC("ADC", "Capture", SND_SOC_NOPM, 0, 0),
	SND_SOC_DAPM_REGULATOR_SUPPLY("VDDP", 0, 0),
	SND_SOC_DAPM_REGULATOR_SUPPLY("VDDD", 0, 0),
	SND_SOC_DAPM_REGULATOR_SUPPLY("VDDA", 0, 0),
};

static const struct snd_soc_dapm_route es7241_dapm_routes[] = {
	{ "ADC", NULL, "AINL", },
	{ "ADC", NULL, "AINR", },
	{ "ADC", NULL, "VDDA", },
	{ "Capture", NULL, "VDDP", },
	{ "Capture", NULL, "VDDD", },
};

static const struct snd_soc_component_driver es7241_component_driver = {
	.dapm_widgets		= es7241_dapm_widgets,
	.num_dapm_widgets	= ARRAY_SIZE(es7241_dapm_widgets),
	.dapm_routes		= es7241_dapm_routes,
	.num_dapm_routes	= ARRAY_SIZE(es7241_dapm_routes),
	.idle_bias_on		= 1,
	.endianness		= 1,
	.non_legacy_dai_naming	= 1,
};

static void es7241_parse_fmt(struct device *dev, struct es7241_data *priv)
{
	bool is_leftj;

	/*
	 * The format is given by a pull resistor on the SDOUT pin:
	 * pull-up for i2s, pull-down for left justified.
	 */
	is_leftj = of_property_read_bool(dev->of_node,
					 "everest,sdout-pull-down");
	if (is_leftj)
		priv->fmt = SND_SOC_DAIFMT_LEFT_J;
	else
		priv->fmt = SND_SOC_DAIFMT_I2S;
}

static int es7241_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct es7241_data *priv;
	int err;

	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;
	platform_set_drvdata(pdev, priv);

	priv->chip = of_device_get_match_data(dev);
	if (!priv->chip) {
		dev_err(dev, "failed to match device\n");
		return -ENODEV;
	}

	es7241_parse_fmt(dev, priv);

	priv->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
	if (IS_ERR(priv->reset)) {
		err = PTR_ERR(priv->reset);
		if (err != -EPROBE_DEFER)
			dev_err(dev, "Failed to get 'reset' gpio: %d", err);
		return err;
	}

	priv->m0 = devm_gpiod_get_optional(dev, "m0", GPIOD_OUT_LOW);
	if (IS_ERR(priv->m0)) {
		err = PTR_ERR(priv->m0);
		if (err != -EPROBE_DEFER)
			dev_err(dev, "Failed to get 'm0' gpio: %d", err);
		return err;
	}

	priv->m1 = devm_gpiod_get_optional(dev, "m1", GPIOD_OUT_LOW);
	if (IS_ERR(priv->m1)) {
		err = PTR_ERR(priv->m1);
		if (err != -EPROBE_DEFER)
			dev_err(dev, "Failed to get 'm1' gpio: %d", err);
		return err;
	}

	return devm_snd_soc_register_component(&pdev->dev,
				      &es7241_component_driver,
				      &es7241_dai, 1);
}

#ifdef CONFIG_OF
static const struct of_device_id es7241_ids[] = {
	{ .compatible = "everest,es7241", .data = &es7241_chip },
	{ }
};
MODULE_DEVICE_TABLE(of, es7241_ids);
#endif

static struct platform_driver es7241_driver = {
	.driver = {
		.name = "es7241",
		.of_match_table = of_match_ptr(es7241_ids),
	},
	.probe = es7241_probe,
};

module_platform_driver(es7241_driver);

MODULE_DESCRIPTION("ASoC ES7241 audio codec driver");
MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
MODULE_LICENSE("GPL");