ASoC: SOF: Make creation of machine device from SOF core optional
[linux-2.6-microblaze.git] / sound / soc / sof / sof-of-dev.c
1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 //
3 // Copyright 2019 NXP
4 //
5 // Author: Daniel Baluta <daniel.baluta@nxp.com>
6 //
7
8 #include <linux/firmware.h>
9 #include <linux/module.h>
10 #include <linux/pm_runtime.h>
11 #include <sound/sof.h>
12
13 #include "ops.h"
14
15 extern struct snd_sof_dsp_ops sof_imx8_ops;
16
17 /* platform specific devices */
18 #if IS_ENABLED(CONFIG_SND_SOC_SOF_IMX8)
19 static struct sof_dev_desc sof_of_imx8qxp_desc = {
20         .default_fw_path = "imx/sof",
21         .default_tplg_path = "imx/sof-tplg",
22         .default_fw_filename = "sof-imx8.ri",
23         .nocodec_fw_filename = "sof-imx8.ri",
24         .nocodec_tplg_filename = "sof-imx8-nocodec.tplg",
25         .ops = &sof_imx8_ops,
26 };
27 #endif
28
29 static const struct dev_pm_ops sof_of_pm = {
30         SET_SYSTEM_SLEEP_PM_OPS(snd_sof_suspend, snd_sof_resume)
31         SET_RUNTIME_PM_OPS(snd_sof_runtime_suspend, snd_sof_runtime_resume,
32                            NULL)
33 };
34
35 static void sof_of_probe_complete(struct device *dev)
36 {
37         /* allow runtime_pm */
38         pm_runtime_set_autosuspend_delay(dev, SND_SOF_SUSPEND_DELAY_MS);
39         pm_runtime_use_autosuspend(dev);
40         pm_runtime_enable(dev);
41 }
42
43 static int sof_of_probe(struct platform_device *pdev)
44 {
45         struct device *dev = &pdev->dev;
46         const struct sof_dev_desc *desc;
47         struct snd_sof_pdata *sof_pdata;
48         const struct snd_sof_dsp_ops *ops;
49         int ret;
50
51         dev_info(&pdev->dev, "DT DSP detected");
52
53         sof_pdata = devm_kzalloc(dev, sizeof(*sof_pdata), GFP_KERNEL);
54         if (!sof_pdata)
55                 return -ENOMEM;
56
57         desc = device_get_match_data(dev);
58         if (!desc)
59                 return -ENODEV;
60
61         /* get ops for platform */
62         ops = desc->ops;
63         if (!ops) {
64                 dev_err(dev, "error: no matching DT descriptor ops\n");
65                 return -ENODEV;
66         }
67
68         sof_pdata->desc = desc;
69         sof_pdata->dev = &pdev->dev;
70         sof_pdata->fw_filename = desc->default_fw_filename;
71
72         /* TODO: read alternate fw and tplg filenames from DT */
73         sof_pdata->fw_filename_prefix = sof_pdata->desc->default_fw_path;
74         sof_pdata->tplg_filename_prefix = sof_pdata->desc->default_tplg_path;
75
76 #if IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE)
77         /* set callback to enable runtime_pm */
78         sof_pdata->sof_probe_complete = sof_of_probe_complete;
79 #endif
80          /* call sof helper for DSP hardware probe */
81         ret = snd_sof_device_probe(dev, sof_pdata);
82         if (ret) {
83                 dev_err(dev, "error: failed to probe DSP hardware\n");
84                 return ret;
85         }
86
87 #if !IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE)
88         sof_of_probe_complete(dev);
89 #endif
90
91         return ret;
92 }
93
94 static int sof_of_remove(struct platform_device *pdev)
95 {
96         pm_runtime_disable(&pdev->dev);
97
98         /* call sof helper for DSP hardware remove */
99         snd_sof_device_remove(&pdev->dev);
100
101         return 0;
102 }
103
104 static const struct of_device_id sof_of_ids[] = {
105 #if IS_ENABLED(CONFIG_SND_SOC_SOF_IMX8)
106         { .compatible = "fsl,imx8qxp-dsp", .data = &sof_of_imx8qxp_desc},
107 #endif
108         { }
109 };
110 MODULE_DEVICE_TABLE(of, sof_of_ids);
111
112 /* DT driver definition */
113 static struct platform_driver snd_sof_of_driver = {
114         .probe = sof_of_probe,
115         .remove = sof_of_remove,
116         .driver = {
117                 .name = "sof-audio-of",
118                 .pm = &sof_of_pm,
119                 .of_match_table = sof_of_ids,
120         },
121 };
122 module_platform_driver(snd_sof_of_driver);
123
124 MODULE_LICENSE("Dual BSD/GPL");