summaryrefslogtreecommitdiff
path: root/sound/soc/starfive/starfive_pdm.c
blob: df38259d6a8db4f47675f6e98f8be995fefdb239 (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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// SPDX-License-Identifier: GPL-2.0
/*
 * PDM driver for the StarFive JH7110 SoC
 *
 * Copyright (C) 2021 StarFive Technology Co., Ltd.
 */
#include <linux/clk.h>
#include <linux/device.h>
#include <linux/reset.h>
#include <linux/module.h>
#include <linux/of_irq.h>
#include <linux/of_platform.h>
#include <linux/regmap.h>
#include <linux/pm_runtime.h>
#include <sound/soc.h>
#include <sound/soc-dai.h>
#include <sound/pcm_params.h>
#include <sound/initval.h>
#include <sound/tlv.h>
#include "starfive_pdm.h"

struct sf_pdm {
	struct regmap *pdm_map;
	struct device *dev;
	struct clk *clk_pdm_apb;
	struct clk *clk_pdm_mclk;
	struct clk *clk_mclk;
	struct clk *clk_mclk_ext;
	struct reset_control *rst_pdm_dmic;
	struct reset_control *rst_pdm_apb;
	unsigned char flag_first;
	unsigned int saved_ctrl0;
	unsigned int saved_scale0;
};

static const DECLARE_TLV_DB_SCALE(volume_tlv, -9450, 150, 0);

static const struct snd_kcontrol_new sf_pdm_snd_controls[] = {
	SOC_SINGLE("DC compensation Control", PDM_DMIC_CTRL0, 30, 1, 0),
	SOC_SINGLE("High Pass Filter Control", PDM_DMIC_CTRL0, 28, 1, 0),
	SOC_SINGLE("Left Channel Volume Control", PDM_DMIC_CTRL0, 23, 1, 0),
	SOC_SINGLE("Right Channel Volume Control", PDM_DMIC_CTRL0, 22, 1, 0),
	SOC_SINGLE_TLV("Volume", PDM_DMIC_CTRL0, 16, 0x3F, 1, volume_tlv),
	SOC_SINGLE("Data MSB Shift", PDM_DMIC_CTRL0, 1, 7, 0),
	SOC_SINGLE("SCALE", PDM_DC_SCALE0, 0, 0x3F, 0),
	SOC_SINGLE("DC offset", PDM_DC_SCALE0, 8, 0xFFFFF, 0),
};

static void sf_pdm_enable(struct regmap *map)
{
	/* Left and Right Channel Volume Control Enable */
	regmap_update_bits(map, PDM_DMIC_CTRL0, PDM_DMIC_RVOL_MASK, 0);
	regmap_update_bits(map, PDM_DMIC_CTRL0, PDM_DMIC_LVOL_MASK, 0);
}

static void sf_pdm_disable(struct regmap *map)
{
	/* Left and Right Channel Volume Control Disable */
	regmap_update_bits(map, PDM_DMIC_CTRL0,
			   PDM_DMIC_RVOL_MASK, PDM_DMIC_RVOL_MASK);
	regmap_update_bits(map, PDM_DMIC_CTRL0,
			   PDM_DMIC_LVOL_MASK, PDM_DMIC_LVOL_MASK);
}

static int sf_pdm_trigger(struct snd_pcm_substream *substream, int cmd,
			   struct snd_soc_dai *dai)
{
	struct sf_pdm *priv = snd_soc_dai_get_drvdata(dai);

	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
	case SNDRV_PCM_TRIGGER_RESUME:
	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
		if (priv->flag_first) {
			priv->flag_first = 0;
			mdelay(200);
		}

		sf_pdm_enable(priv->pdm_map);
		return 0;

	case SNDRV_PCM_TRIGGER_STOP:
	case SNDRV_PCM_TRIGGER_SUSPEND:
	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
		sf_pdm_disable(priv->pdm_map);
		return 0;

	default:
		return -EINVAL;
	}
}

static int sf_pdm_hw_params(struct snd_pcm_substream *substream,
			     struct snd_pcm_hw_params *params,
			     struct snd_soc_dai *dai)
{
	struct sf_pdm *priv = snd_soc_dai_get_drvdata(dai);
	unsigned int sample_rate;
	unsigned int data_width;
	int ret;
	const int pdm_mul = 128;

	sample_rate = params_rate(params);
	switch (sample_rate) {
	case 8000:
	case 11025:
	case 16000:
		break;
	default:
		dev_err(priv->dev, "can't support sample rate:%d\n", sample_rate);
		return -EINVAL;
	}

	data_width = params_width(params);
	switch (data_width) {
	case 16:
	case 32:
		break;
	default:
		dev_err(priv->dev, "can't support bit width %d\n", data_width);
		return -EINVAL;
	}

	/* set pdm_mclk,  PDM MCLK = 128 * LRCLK */
	ret = clk_set_rate(priv->clk_pdm_mclk, pdm_mul * sample_rate);
	if (ret) {
		dev_err(priv->dev, "Can't set pdm_mclk: %d\n", ret);
		return ret;
	}

	return 0;
}

static const struct snd_soc_dai_ops sf_pdm_dai_ops = {
	.trigger	= sf_pdm_trigger,
	.hw_params	= sf_pdm_hw_params,
};

static void sf_pdm_module_init(struct sf_pdm *priv)
{
	/* Reset */
	regmap_update_bits(priv->pdm_map, PDM_DMIC_CTRL0,
			   PDM_SW_RST_MASK, 0x00);
	regmap_update_bits(priv->pdm_map, PDM_DMIC_CTRL0,
			   PDM_SW_RST_MASK, PDM_SW_RST_RELEASE);

	/* Make sure the device is initially disabled */
	sf_pdm_disable(priv->pdm_map);

	/* MUTE */
	regmap_update_bits(priv->pdm_map, PDM_DMIC_CTRL0,
			   PDM_DMIC_VOL_MASK, PDM_VOL_DB_MUTE);

	/* UNMUTE */
	regmap_update_bits(priv->pdm_map, PDM_DMIC_CTRL0,
			   PDM_DMIC_VOL_MASK, PDM_VOL_DB_MAX);

	/* enable high pass filter */
	regmap_update_bits(priv->pdm_map, PDM_DMIC_CTRL0,
			   PDM_DMIC_HPF_EN, PDM_DMIC_HPF_EN);

	/* i2s slave mode */
	regmap_update_bits(priv->pdm_map, PDM_DMIC_CTRL0,
			   PDM_DMIC_I2S_SLAVE, PDM_DMIC_I2S_SLAVE);

	/* disable fast mode */
	regmap_update_bits(priv->pdm_map, PDM_DMIC_CTRL0,
			   PDM_DMIC_FASTMODE_MASK, 0);

	/* dmic msb shift 0 */
	regmap_update_bits(priv->pdm_map, PDM_DMIC_CTRL0,
			   PDM_DMIC_MSB_MASK, 0);

	/* scale: 0x8 */
	regmap_update_bits(priv->pdm_map, PDM_DC_SCALE0,
			   DMIC_SCALE_MASK, DMIC_SCALE_DEF_VAL);

	regmap_update_bits(priv->pdm_map, PDM_DC_SCALE0,
			   DMIC_DCOFF1_MASK, DMIC_DCOFF1_VAL);

	regmap_update_bits(priv->pdm_map, PDM_DC_SCALE0,
			   DMIC_DCOFF3_MASK, DMIC_DCOFF3_VAL);

	/* scale: 0x3f */
	regmap_update_bits(priv->pdm_map, PDM_DC_SCALE0,
			   DMIC_SCALE_MASK, DMIC_SCALE_MASK);

	/* dmic msb shift 2 */
	regmap_update_bits(priv->pdm_map, PDM_DMIC_CTRL0,
			   PDM_DMIC_MSB_MASK, PDM_MSB_SHIFT_4);
}

#define SF_PDM_RATES	(SNDRV_PCM_RATE_8000 | \
			SNDRV_PCM_RATE_11025 | \
			SNDRV_PCM_RATE_16000)

#define SF_PDM_FORMATS	(SNDRV_PCM_FMTBIT_S16_LE | \
			SNDRV_PCM_FMTBIT_S32_LE)

static struct snd_soc_dai_driver sf_pdm_dai_drv = {
	.name = "PDM",
	.id = 0,
	.capture = {
		.stream_name	= "Capture",
		.channels_min	= 2,
		.channels_max	= 2,
		.rates		= SF_PDM_RATES,
		.formats	= SF_PDM_FORMATS,
	},
	.ops = &sf_pdm_dai_ops,
	.symmetric_rate = 1,
};

static int sf_pdm_component_probe(struct snd_soc_component *component)
{
	struct sf_pdm *priv = snd_soc_component_get_drvdata(component);

	snd_soc_component_init_regmap(component, priv->pdm_map);
	snd_soc_add_component_controls(component, sf_pdm_snd_controls,
				       ARRAY_SIZE(sf_pdm_snd_controls));

	return 0;
}

static int sf_pdm_clock_enable(struct sf_pdm *priv)
{
	int ret;

	ret = clk_prepare_enable(priv->clk_pdm_mclk);
	if (ret) {
		dev_err(priv->dev, "failed to prepare enable clk_pdm_mclk\n");
		return ret;
	}

	ret = clk_prepare_enable(priv->clk_pdm_apb);
	if (ret) {
		dev_err(priv->dev, "failed to prepare enable clk_pdm_apb\n");
		goto disable_pdm_mclk;
	}

	ret = reset_control_deassert(priv->rst_pdm_dmic);
	if (ret) {
		dev_err(priv->dev, "failed to deassert pdm_dmic\n");
		goto disable_pdm_apb;
	}

	ret = reset_control_deassert(priv->rst_pdm_apb);
	if (ret) {
		dev_err(priv->dev, "failed to deassert pdm_apb\n");
		goto disable_pdm_apb;
	}

	ret = clk_set_parent(priv->clk_mclk, priv->clk_mclk_ext);
	if (ret) {
		dev_err(priv->dev, "failed to set parent clk_mclk ret=%d\n", ret);
		goto disable_pdm_apb;
	}

	return 0;

disable_pdm_apb:
	clk_disable_unprepare(priv->clk_pdm_apb);
disable_pdm_mclk:
	clk_disable_unprepare(priv->clk_pdm_mclk);

	return ret;
}

#ifdef CONFIG_PM
static int sf_pdm_runtime_suspend(struct device *dev)
{
	struct sf_pdm *priv = dev_get_drvdata(dev);

	clk_disable_unprepare(priv->clk_pdm_apb);
	clk_disable_unprepare(priv->clk_pdm_mclk);

	return 0;
}

static int sf_pdm_runtime_resume(struct device *dev)
{
	struct sf_pdm *priv = dev_get_drvdata(dev);
	int ret;

	ret = sf_pdm_clock_enable(priv);
	if (!ret)
		sf_pdm_module_init(priv);

	return ret;
}
#endif

#ifdef CONFIG_PM_SLEEP
static int sf_pdm_suspend(struct snd_soc_component *component)
{
	return pm_runtime_force_suspend(component->dev);
}

static int sf_pdm_resume(struct snd_soc_component *component)
{
	return pm_runtime_force_resume(component->dev);
}

#else
#define sf_pdm_suspend	NULL
#define sf_pdm_resume	NULL
#endif

static const struct snd_soc_component_driver sf_pdm_component_drv = {
	.name = "jh7110-pdm",
	.probe = sf_pdm_component_probe,
	.suspend = sf_pdm_suspend,
	.resume = sf_pdm_resume,
};

static const struct regmap_config sf_pdm_regmap_cfg = {
	.reg_bits	= 32,
	.val_bits	= 32,
	.reg_stride	= 4,
	.max_register	= 0x20,
};

static int sf_pdm_clock_get(struct platform_device *pdev, struct sf_pdm *priv)
{
	int ret;

	static struct clk_bulk_data clks[] = {
		{ .id = "pdm_mclk" },
		{ .id = "pdm_apb" },
		{ .id = "clk_mclk" },
		{ .id = "mclk_ext" },
	};

	ret = devm_clk_bulk_get(&pdev->dev, ARRAY_SIZE(clks), clks);
	if (ret) {
		dev_err(&pdev->dev, "failed to get pdm clocks\n");
		goto exit;
	}

	priv->clk_pdm_mclk = clks[0].clk;
	priv->clk_pdm_apb = clks[1].clk;
	priv->clk_mclk = clks[2].clk;
	priv->clk_mclk_ext = clks[3].clk;

	priv->rst_pdm_dmic = devm_reset_control_get_exclusive(&pdev->dev, "pdm_dmic");
	if (IS_ERR(priv->rst_pdm_dmic)) {
		dev_err(&pdev->dev, "failed to get pdm_dmic reset control\n");
		ret = PTR_ERR(priv->rst_pdm_dmic);
		goto exit;
	}

	priv->rst_pdm_apb = devm_reset_control_get_exclusive(&pdev->dev, "pdm_apb");
	if (IS_ERR(priv->rst_pdm_apb)) {
		dev_err(&pdev->dev, "failed to get pdm_apb reset control\n");
		ret = PTR_ERR(priv->rst_pdm_apb);
		goto exit;
	}

	/*
	 * pdm clock must always be enabled as hardware issue that
	 * no data in the first 4 seconds of the first recording
	 */
	ret = sf_pdm_clock_enable(priv);

exit:
	return ret;
}

static int sf_pdm_probe(struct platform_device *pdev)
{
	struct sf_pdm *priv;
	struct resource *res;
	void __iomem *regs;
	int ret;

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

	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pdm");
	regs = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(regs))
		return PTR_ERR(regs);

	priv->pdm_map = devm_regmap_init_mmio(&pdev->dev, regs, &sf_pdm_regmap_cfg);
	if (IS_ERR(priv->pdm_map)) {
		dev_err(&pdev->dev, "failed to init regmap: %ld\n",
			PTR_ERR(priv->pdm_map));
		return PTR_ERR(priv->pdm_map);
	}

	priv->dev = &pdev->dev;
	priv->flag_first = 1;

	ret = sf_pdm_clock_get(pdev, priv);
	if (ret) {
		dev_err(&pdev->dev, "failed to enable audio-pdm clock\n");
		return ret;
	}

	dev_set_drvdata(&pdev->dev, priv);

	ret = devm_snd_soc_register_component(&pdev->dev, &sf_pdm_component_drv,
					      &sf_pdm_dai_drv, 1);
	if (ret) {
		dev_err(&pdev->dev, "failed to register pdm dai\n");
		return ret;
	}
	pm_runtime_enable(&pdev->dev);

	return 0;
}

static int sf_pdm_dev_remove(struct platform_device *pdev)
{
	pm_runtime_disable(&pdev->dev);
	return 0;
}

static const struct of_device_id sf_pdm_of_match[] = {
	{.compatible = "starfive,jh7110-pdm",},
	{}
};
MODULE_DEVICE_TABLE(of, sf_pdm_of_match);

static const struct dev_pm_ops sf_pdm_pm_ops = {
	SET_RUNTIME_PM_OPS(sf_pdm_runtime_suspend,
			   sf_pdm_runtime_resume, NULL)
};

static struct platform_driver sf_pdm_driver = {
	.driver = {
		.name = "jh7110-pdm",
		.of_match_table = sf_pdm_of_match,
		.pm = &sf_pdm_pm_ops,
	},
	.probe = sf_pdm_probe,
	.remove = sf_pdm_dev_remove,
};
module_platform_driver(sf_pdm_driver);

MODULE_AUTHOR("Walker Chen <walker.chen@starfivetech.com>");
MODULE_DESCRIPTION("Starfive PDM Controller Driver");
MODULE_LICENSE("GPL v2");