1 // SPDX-License-Identifier: GPL-2.0-only
3 // Copyright(c) 2020 Intel Corporation. All rights reserved.
5 // Author: Cezary Rojewski <cezary.rojewski@intel.com>
8 // Marcin Barlik <marcin.barlik@intel.com>
9 // Piotr Papierkowski <piotr.papierkowski@intel.com>
11 // for sharing LPT-LP and WTP-LP AudioDSP architecture expertise and
12 // helping backtrack its historical background
15 #include <linux/acpi.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/interrupt.h>
18 #include <linux/module.h>
19 #include <linux/pci.h>
20 #include <linux/platform_device.h>
21 #include <linux/pm_runtime.h>
22 #include <sound/intel-dsp-config.h>
23 #include <sound/soc.h>
24 #include <sound/soc-acpi.h>
26 #include "registers.h"
28 #define CREATE_TRACE_POINTS
31 static int __maybe_unused catpt_suspend(struct device *dev)
33 struct catpt_dev *cdev = dev_get_drvdata(dev);
34 struct dma_chan *chan;
37 chan = catpt_dma_request_config_chan(cdev);
41 memset(&cdev->dx_ctx, 0, sizeof(cdev->dx_ctx));
42 ret = catpt_ipc_enter_dxstate(cdev, CATPT_DX_STATE_D3, &cdev->dx_ctx);
44 ret = CATPT_IPC_ERROR(ret);
45 goto release_dma_chan;
48 ret = catpt_dsp_stall(cdev, true);
50 goto release_dma_chan;
52 ret = catpt_store_memdumps(cdev, chan);
54 dev_err(cdev->dev, "store memdumps failed: %d\n", ret);
55 goto release_dma_chan;
58 ret = catpt_store_module_states(cdev, chan);
60 dev_err(cdev->dev, "store module states failed: %d\n", ret);
61 goto release_dma_chan;
64 ret = catpt_store_streams_context(cdev, chan);
66 dev_err(cdev->dev, "store streams ctx failed: %d\n", ret);
69 dma_release_channel(chan);
72 return catpt_dsp_power_down(cdev);
75 static int __maybe_unused catpt_resume(struct device *dev)
77 struct catpt_dev *cdev = dev_get_drvdata(dev);
80 ret = catpt_dsp_power_up(cdev);
84 if (!try_module_get(dev->driver->owner)) {
85 dev_info(dev, "module unloading, skipping fw boot\n");
88 module_put(dev->driver->owner);
90 ret = catpt_boot_firmware(cdev, true);
92 dev_err(cdev->dev, "boot firmware failed: %d\n", ret);
96 /* reconfigure SSP devices after Dx transition */
97 for (i = 0; i < CATPT_SSP_COUNT; i++) {
98 if (cdev->devfmt[i].iface == UINT_MAX)
101 ret = catpt_ipc_set_device_format(cdev, &cdev->devfmt[i]);
103 return CATPT_IPC_ERROR(ret);
109 static int __maybe_unused catpt_runtime_suspend(struct device *dev)
111 if (!try_module_get(dev->driver->owner)) {
112 dev_info(dev, "module unloading, skipping suspend\n");
115 module_put(dev->driver->owner);
117 return catpt_suspend(dev);
120 static int __maybe_unused catpt_runtime_resume(struct device *dev)
122 return catpt_resume(dev);
125 static const struct dev_pm_ops catpt_dev_pm = {
126 SET_SYSTEM_SLEEP_PM_OPS(catpt_suspend, catpt_resume)
127 SET_RUNTIME_PM_OPS(catpt_runtime_suspend, catpt_runtime_resume, NULL)
130 /* machine board owned by CATPT is removed with this hook */
131 static void board_pdev_unregister(void *data)
133 platform_device_unregister(data);
136 static int catpt_register_board(struct catpt_dev *cdev)
138 const struct catpt_spec *spec = cdev->spec;
139 struct snd_soc_acpi_mach *mach;
140 struct platform_device *board;
142 mach = snd_soc_acpi_find_machine(spec->machines);
144 dev_info(cdev->dev, "no machines present\n");
148 mach->mach_params.platform = "catpt-platform";
149 board = platform_device_register_data(NULL, mach->drv_name,
151 (const void *)mach, sizeof(*mach));
153 dev_err(cdev->dev, "board register failed\n");
154 return PTR_ERR(board);
157 return devm_add_action_or_reset(cdev->dev, board_pdev_unregister,
161 static int catpt_probe_components(struct catpt_dev *cdev)
165 ret = catpt_dsp_power_up(cdev);
169 ret = catpt_dmac_probe(cdev);
171 dev_err(cdev->dev, "DMAC probe failed: %d\n", ret);
175 ret = catpt_first_boot_firmware(cdev);
177 dev_err(cdev->dev, "first fw boot failed: %d\n", ret);
181 ret = catpt_register_plat_component(cdev);
183 dev_err(cdev->dev, "register plat comp failed: %d\n", ret);
187 ret = catpt_register_board(cdev);
189 dev_err(cdev->dev, "register board failed: %d\n", ret);
193 /* reflect actual ADSP state in pm_runtime */
194 pm_runtime_set_active(cdev->dev);
196 pm_runtime_set_autosuspend_delay(cdev->dev, 2000);
197 pm_runtime_use_autosuspend(cdev->dev);
198 pm_runtime_mark_last_busy(cdev->dev);
199 pm_runtime_enable(cdev->dev);
203 snd_soc_unregister_component(cdev->dev);
205 catpt_dmac_remove(cdev);
207 catpt_dsp_power_down(cdev);
212 static void catpt_dev_init(struct catpt_dev *cdev, struct device *dev,
213 const struct catpt_spec *spec)
217 init_completion(&cdev->fw_ready);
218 INIT_LIST_HEAD(&cdev->stream_list);
219 spin_lock_init(&cdev->list_lock);
220 mutex_init(&cdev->clk_mutex);
223 * Mark both device formats as uninitialized. Once corresponding
224 * cpu_dai's pcm is created, proper values are assigned.
226 cdev->devfmt[CATPT_SSP_IFACE_0].iface = UINT_MAX;
227 cdev->devfmt[CATPT_SSP_IFACE_1].iface = UINT_MAX;
229 catpt_ipc_init(&cdev->ipc, dev);
231 catpt_sram_init(&cdev->dram, spec->host_dram_offset,
232 catpt_dram_size(cdev));
233 catpt_sram_init(&cdev->iram, spec->host_iram_offset,
234 catpt_iram_size(cdev));
237 static int catpt_acpi_probe(struct platform_device *pdev)
239 const struct catpt_spec *spec;
240 struct catpt_dev *cdev;
241 struct device *dev = &pdev->dev;
242 const struct acpi_device_id *id;
243 struct resource *res;
246 id = acpi_match_device(dev->driver->acpi_match_table, dev);
250 ret = snd_intel_acpi_dsp_driver_probe(dev, id->id);
251 if (ret != SND_INTEL_DSP_DRIVER_ANY && ret != SND_INTEL_DSP_DRIVER_SST) {
252 dev_dbg(dev, "CATPT ACPI driver not selected, aborting probe\n");
256 cdev = devm_kzalloc(dev, sizeof(*cdev), GFP_KERNEL);
260 spec = (const struct catpt_spec *)id->driver_data;
261 catpt_dev_init(cdev, dev, spec);
263 /* map DSP bar address */
264 cdev->lpe_ba = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
265 if (IS_ERR(cdev->lpe_ba))
266 return PTR_ERR(cdev->lpe_ba);
267 cdev->lpe_base = res->start;
269 /* map PCI bar address */
270 cdev->pci_ba = devm_platform_ioremap_resource(pdev, 1);
271 if (IS_ERR(cdev->pci_ba))
272 return PTR_ERR(cdev->pci_ba);
274 /* alloc buffer for storing DRAM context during dx transitions */
275 cdev->dxbuf_vaddr = dmam_alloc_coherent(dev, catpt_dram_size(cdev),
276 &cdev->dxbuf_paddr, GFP_KERNEL);
277 if (!cdev->dxbuf_vaddr)
280 ret = platform_get_irq(pdev, 0);
285 platform_set_drvdata(pdev, cdev);
287 ret = devm_request_threaded_irq(dev, cdev->irq, catpt_dsp_irq_handler,
288 catpt_dsp_irq_thread,
289 IRQF_SHARED, "AudioDSP", cdev);
293 return catpt_probe_components(cdev);
296 static void catpt_acpi_remove(struct platform_device *pdev)
298 struct catpt_dev *cdev = platform_get_drvdata(pdev);
300 pm_runtime_disable(cdev->dev);
302 snd_soc_unregister_component(cdev->dev);
303 catpt_dmac_remove(cdev);
304 catpt_dsp_power_down(cdev);
306 catpt_sram_free(&cdev->iram);
307 catpt_sram_free(&cdev->dram);
310 static struct snd_soc_acpi_mach lpt_machines[] = {
313 .drv_name = "hsw_rt5640",
318 static struct snd_soc_acpi_mach wpt_machines[] = {
321 .drv_name = "hsw_rt5640",
325 .drv_name = "bdw_rt286",
329 .drv_name = "bdw-rt5650",
333 .drv_name = "bdw-rt5677",
338 static struct catpt_spec lpt_desc = {
339 .machines = lpt_machines,
341 .host_dram_offset = 0x000000,
342 .host_iram_offset = 0x080000,
343 .host_shim_offset = 0x0E7000,
344 .host_dma_offset = { 0x0F0000, 0x0F8000 },
345 .host_ssp_offset = { 0x0E8000, 0x0E9000 },
346 .dram_mask = LPT_VDRTCTL0_DSRAMPGE_MASK,
347 .iram_mask = LPT_VDRTCTL0_ISRAMPGE_MASK,
348 .d3srampgd_bit = LPT_VDRTCTL0_D3SRAMPGD,
349 .d3pgd_bit = LPT_VDRTCTL0_D3PGD,
350 .pll_shutdown = lpt_dsp_pll_shutdown,
353 static struct catpt_spec wpt_desc = {
354 .machines = wpt_machines,
356 .host_dram_offset = 0x000000,
357 .host_iram_offset = 0x0A0000,
358 .host_shim_offset = 0x0FB000,
359 .host_dma_offset = { 0x0FE000, 0x0FF000 },
360 .host_ssp_offset = { 0x0FC000, 0x0FD000 },
361 .dram_mask = WPT_VDRTCTL0_DSRAMPGE_MASK,
362 .iram_mask = WPT_VDRTCTL0_ISRAMPGE_MASK,
363 .d3srampgd_bit = WPT_VDRTCTL0_D3SRAMPGD,
364 .d3pgd_bit = WPT_VDRTCTL0_D3PGD,
365 .pll_shutdown = wpt_dsp_pll_shutdown,
368 static const struct acpi_device_id catpt_ids[] = {
369 { "INT33C8", (unsigned long)&lpt_desc },
370 { "INT3438", (unsigned long)&wpt_desc },
373 MODULE_DEVICE_TABLE(acpi, catpt_ids);
375 static struct platform_driver catpt_acpi_driver = {
376 .probe = catpt_acpi_probe,
377 .remove_new = catpt_acpi_remove,
379 .name = "intel_catpt",
380 .acpi_match_table = catpt_ids,
382 .dev_groups = catpt_attr_groups,
385 module_platform_driver(catpt_acpi_driver);
387 MODULE_AUTHOR("Cezary Rojewski <cezary.rojewski@intel.com>");
388 MODULE_DESCRIPTION("Intel LPT/WPT AudioDSP driver");
389 MODULE_LICENSE("GPL v2");