summaryrefslogtreecommitdiffstats
path: root/sound/soc/intel/catpt/device.c
blob: a7017995979561efe7beed6e21503df31e435af9 (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
// SPDX-License-Identifier: GPL-2.0-only
//
// Copyright(c) 2020 Intel Corporation. All rights reserved.
//
// Author: Cezary Rojewski <cezary.rojewski@intel.com>
//
// Special thanks to:
//    Marcin Barlik <marcin.barlik@intel.com>
//    Piotr Papierkowski <piotr.papierkowski@intel.com>
//
// for sharing LPT-LP and WTP-LP AudioDSP architecture expertise and
// helping backtrack its historical background
//

#include <linux/acpi.h>
#include <linux/dma-mapping.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <sound/soc.h>
#include <sound/soc-acpi.h>
#include <sound/soc-acpi-intel-match.h>
#include "core.h"
#include "registers.h"

#define CREATE_TRACE_POINTS
#include "trace.h"

static int __maybe_unused catpt_suspend(struct device *dev)
{
	struct catpt_dev *cdev = dev_get_drvdata(dev);
	struct dma_chan *chan;
	int ret;

	chan = catpt_dma_request_config_chan(cdev);
	if (IS_ERR(chan))
		return PTR_ERR(chan);

	memset(&cdev->dx_ctx, 0, sizeof(cdev->dx_ctx));
	ret = catpt_ipc_enter_dxstate(cdev, CATPT_DX_STATE_D3, &cdev->dx_ctx);
	if (ret) {
		ret = CATPT_IPC_ERROR(ret);
		goto release_dma_chan;
	}

	ret = catpt_dsp_stall(cdev, true);
	if (ret)
		goto release_dma_chan;

	ret = catpt_store_memdumps(cdev, chan);
	if (ret) {
		dev_err(cdev->dev, "store memdumps failed: %d\n", ret);
		goto release_dma_chan;
	}

	ret = catpt_store_module_states(cdev, chan);
	if (ret) {
		dev_err(cdev->dev, "store module states failed: %d\n", ret);
		goto release_dma_chan;
	}

	ret = catpt_store_streams_context(cdev, chan);
	if (ret)
		dev_err(cdev->dev, "store streams ctx failed: %d\n", ret);

release_dma_chan:
	dma_release_channel(chan);
	if (ret)
		return ret;
	return cdev->spec->power_down(cdev);
}

static int __maybe_unused catpt_resume(struct device *dev)
{
	struct catpt_dev *cdev = dev_get_drvdata(dev);
	int ret, i;

	ret = cdev->spec->power_up(cdev);
	if (ret)
		return ret;

	if (!try_module_get(dev->driver->owner)) {
		dev_info(dev, "module unloading, skipping fw boot\n");
		return 0;
	}
	module_put(dev->driver->owner);

	ret = catpt_boot_firmware(cdev, true);
	if (ret) {
		dev_err(cdev->dev, "boot firmware failed: %d\n", ret);
		return ret;
	}

	/* reconfigure SSP devices after Dx transition */
	for (i = 0; i < CATPT_SSP_COUNT; i++) {
		if (cdev->devfmt[i].iface == UINT_MAX)
			continue;

		ret = catpt_ipc_set_device_format(cdev, &cdev->devfmt[i]);
		if (ret)
			return CATPT_IPC_ERROR(ret);
	}

	return 0;
}

static int __maybe_unused catpt_runtime_suspend(struct device *dev)
{
	if (!try_module_get(dev->driver->owner)) {
		dev_info(dev, "module unloading, skipping suspend\n");
		return 0;
	}
	module_put(dev->driver->owner);

	return catpt_suspend(dev);
}

static int __maybe_unused catpt_runtime_resume(struct device *dev)
{
	return catpt_resume(dev);
}

static const struct dev_pm_ops catpt_dev_pm = {
	SET_SYSTEM_SLEEP_PM_OPS(catpt_suspend, catpt_resume)
	SET_RUNTIME_PM_OPS(catpt_runtime_suspend, catpt_runtime_resume, NULL)
};

/* machine board owned by CATPT is removed with this hook */
static void board_pdev_unregister(void *data)
{
	platform_device_unregister(data);
}

static int catpt_register_board(struct catpt_dev *cdev)
{
	const struct catpt_spec *spec = cdev->spec;
	struct snd_soc_acpi_mach *mach;
	struct platform_device *board;

	mach = snd_soc_acpi_find_machine(spec->machines);
	if (!mach) {
		dev_info(cdev->dev, "no machines present\n");
		return 0;
	}

	mach->mach_params.platform = "catpt-platform";
	board = platform_device_register_data(NULL, mach->drv_name,
					PLATFORM_DEVID_NONE,
					(const void *)mach, sizeof(*mach));
	if (IS_ERR(board)) {
		dev_err(cdev->dev, "board register failed\n");
		return PTR_ERR(board);
	}

	return devm_add_action_or_reset(cdev->dev, board_pdev_unregister,
					board);
}

static int catpt_probe_components(struct catpt_dev *cdev)
{
	int ret;

	ret = cdev->spec->power_up(cdev);
	if (ret)
		return ret;

	ret = catpt_dmac_probe(cdev);
	if (ret) {
		dev_err(cdev->dev, "DMAC probe failed: %d\n", ret);
		goto err_dmac_probe;
	}

	ret = catpt_first_boot_firmware(cdev);
	if (ret) {
		dev_err(cdev->dev, "first fw boot failed: %d\n", ret);
		goto err_boot_fw;
	}

	ret = catpt_register_plat_component(cdev);
	if (ret) {
		dev_err(cdev->dev, "register plat comp failed: %d\n", ret);
		goto err_boot_fw;
	}

	ret = catpt_register_board(cdev);
	if (ret) {
		dev_err(cdev->dev, "register board failed: %d\n", ret);
		goto err_reg_board;
	}

	/* reflect actual ADSP state in pm_runtime */
	pm_runtime_set_active(cdev->dev);

	pm_runtime_set_autosuspend_delay(cdev->dev, 2000);
	pm_runtime_use_autosuspend(cdev->dev);
	pm_runtime_mark_last_busy(cdev->dev);
	pm_runtime_enable(cdev->dev);
	return 0;

err_reg_board:
	snd_soc_unregister_component(cdev->dev);
err_boot_fw:
	catpt_dmac_remove(cdev);
err_dmac_probe:
	cdev->spec->power_down(cdev);

	return ret;
}

static void catpt_dev_init(struct catpt_dev *cdev, struct device *dev,
			   const struct catpt_spec *spec)
{
	cdev->dev = dev;
	cdev->spec = spec;
	init_completion(&cdev->fw_ready);
	INIT_LIST_HEAD(&cdev->stream_list);
	spin_lock_init(&cdev->list_lock);
	mutex_init(&cdev->clk_mutex);

	/*
	 * Mark both device formats as uninitialized. Once corresponding
	 * cpu_dai's pcm is created, proper values are assigned.
	 */
	cdev->devfmt[CATPT_SSP_IFACE_0].iface = UINT_MAX;
	cdev->devfmt[CATPT_SSP_IFACE_1].iface = UINT_MAX;

	catpt_ipc_init(&cdev->ipc, dev);

	catpt_sram_init(&cdev->dram, spec->host_dram_offset,
			catpt_dram_size(cdev));
	catpt_sram_init(&cdev->iram, spec->host_iram_offset,
			catpt_iram_size(cdev));
}

static int catpt_acpi_probe(struct platform_device *pdev)
{
	const struct catpt_spec *spec;
	struct catpt_dev *cdev;
	struct device *dev = &pdev->dev;
	struct resource *res;
	int ret;

	spec = device_get_match_data(dev);
	if (!spec)
		return -ENODEV;

	cdev = devm_kzalloc(dev, sizeof(*cdev), GFP_KERNEL);
	if (!cdev)
		return -ENOMEM;

	catpt_dev_init(cdev, dev, spec);

	/* map DSP bar address */
	cdev->lpe_ba = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
	if (IS_ERR(cdev->lpe_ba))
		return PTR_ERR(cdev->lpe_ba);
	cdev->lpe_base = res->start;

	/* map PCI bar address */
	cdev->pci_ba = devm_platform_ioremap_resource(pdev, 1);
	if (IS_ERR(cdev->pci_ba))
		return PTR_ERR(cdev->pci_ba);

	/* alloc buffer for storing DRAM context during dx transitions */
	cdev->dxbuf_vaddr = dmam_alloc_coherent(dev, catpt_dram_size(cdev),
						&cdev->dxbuf_paddr, GFP_KERNEL);
	if (!cdev->dxbuf_vaddr)
		return -ENOMEM;

	ret = platform_get_irq(pdev, 0);
	if (ret < 0)
		return ret;
	cdev->irq = ret;

	platform_set_drvdata(pdev, cdev);

	ret = devm_request_threaded_irq(dev, cdev->irq, catpt_dsp_irq_handler,
					catpt_dsp_irq_thread,
					IRQF_SHARED, "AudioDSP", cdev);
	if (ret)
		return ret;

	return catpt_probe_components(cdev);
}

static int catpt_acpi_remove(struct platform_device *pdev)
{
	struct catpt_dev *cdev = platform_get_drvdata(pdev);

	pm_runtime_disable(cdev->dev);

	snd_soc_unregister_component(cdev->dev);
	catpt_dmac_remove(cdev);
	cdev->spec->power_down(cdev);

	catpt_sram_free(&cdev->iram);
	catpt_sram_free(&cdev->dram);

	return 0;
}

static struct catpt_spec lpt_desc = {
	.machines = snd_soc_acpi_intel_haswell_machines,
	.core_id = 0x01,
	.host_dram_offset = 0x000000,
	.host_iram_offset = 0x080000,
	.host_shim_offset = 0x0E7000,
	.host_dma_offset = { 0x0F0000, 0x0F8000 },
	.host_ssp_offset = { 0x0E8000, 0x0E9000 },
	.dram_mask = LPT_VDRTCTL0_DSRAMPGE_MASK,
	.iram_mask = LPT_VDRTCTL0_ISRAMPGE_MASK,
	.pll_shutdown = lpt_dsp_pll_shutdown,
	.power_up = lpt_dsp_power_up,
	.power_down = lpt_dsp_power_down,
};

static struct catpt_spec wpt_desc = {
	.machines = snd_soc_acpi_intel_broadwell_machines,
	.core_id = 0x02,
	.host_dram_offset = 0x000000,
	.host_iram_offset = 0x0A0000,
	.host_shim_offset = 0x0FB000,
	.host_dma_offset = { 0x0FE000, 0x0FF000 },
	.host_ssp_offset = { 0x0FC000, 0x0FD000 },
	.dram_mask = WPT_VDRTCTL0_DSRAMPGE_MASK,
	.iram_mask = WPT_VDRTCTL0_ISRAMPGE_MASK,
	.pll_shutdown = wpt_dsp_pll_shutdown,
	.power_up = wpt_dsp_power_up,
	.power_down = wpt_dsp_power_down,
};

static const struct acpi_device_id catpt_ids[] = {
	{ "INT33C8", (unsigned long)&lpt_desc },
	{ "INT3438", (unsigned long)&wpt_desc },
	{ }
};
MODULE_DEVICE_TABLE(acpi, catpt_ids);

static struct platform_driver catpt_acpi_driver = {
	.probe = catpt_acpi_probe,
	.remove = catpt_acpi_remove,
	.driver = {
		.name = "intel_catpt",
		.acpi_match_table = catpt_ids,
		.pm = &catpt_dev_pm,
		.dev_groups = catpt_attr_groups,
	},
};
module_platform_driver(catpt_acpi_driver);

MODULE_AUTHOR("Cezary Rojewski <cezary.rojewski@intel.com>");
MODULE_DESCRIPTION("Intel LPT/WPT AudioDSP driver");
MODULE_LICENSE("GPL v2");