xref: /linux/sound/soc/amd/ps/pci-ps.c (revision cdd30ebb1b9f36159d66f088b61aee264e649d7a)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * AMD Pink Sardine ACP PCI Driver
4  *
5  * Copyright 2022 Advanced Micro Devices, Inc.
6  */
7 
8 #include <linux/pci.h>
9 #include <linux/bitops.h>
10 #include <linux/module.h>
11 #include <linux/io.h>
12 #include <linux/delay.h>
13 #include <linux/platform_device.h>
14 #include <linux/acpi.h>
15 #include <linux/interrupt.h>
16 #include <sound/pcm_params.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/iopoll.h>
19 #include <linux/soundwire/sdw_amd.h>
20 #include "../mach-config.h"
21 
22 #include "acp63.h"
23 
acp63_power_on(void __iomem * acp_base)24 static int acp63_power_on(void __iomem *acp_base)
25 {
26 	u32 val;
27 
28 	val = readl(acp_base + ACP_PGFSM_STATUS);
29 
30 	if (!val)
31 		return val;
32 
33 	if ((val & ACP_PGFSM_STATUS_MASK) != ACP_POWER_ON_IN_PROGRESS)
34 		writel(ACP_PGFSM_CNTL_POWER_ON_MASK, acp_base + ACP_PGFSM_CONTROL);
35 
36 	return readl_poll_timeout(acp_base + ACP_PGFSM_STATUS, val, !val, DELAY_US, ACP_TIMEOUT);
37 }
38 
acp63_reset(void __iomem * acp_base)39 static int acp63_reset(void __iomem *acp_base)
40 {
41 	u32 val;
42 	int ret;
43 
44 	writel(1, acp_base + ACP_SOFT_RESET);
45 
46 	ret = readl_poll_timeout(acp_base + ACP_SOFT_RESET, val,
47 				 val & ACP_SOFT_RESET_SOFTRESET_AUDDONE_MASK,
48 				 DELAY_US, ACP_TIMEOUT);
49 	if (ret)
50 		return ret;
51 
52 	writel(0, acp_base + ACP_SOFT_RESET);
53 
54 	return readl_poll_timeout(acp_base + ACP_SOFT_RESET, val, !val, DELAY_US, ACP_TIMEOUT);
55 }
56 
acp63_enable_interrupts(void __iomem * acp_base)57 static void acp63_enable_interrupts(void __iomem *acp_base)
58 {
59 	writel(1, acp_base + ACP_EXTERNAL_INTR_ENB);
60 	writel(ACP_ERROR_IRQ, acp_base + ACP_EXTERNAL_INTR_CNTL);
61 }
62 
acp63_disable_interrupts(void __iomem * acp_base)63 static void acp63_disable_interrupts(void __iomem *acp_base)
64 {
65 	writel(ACP_EXT_INTR_STAT_CLEAR_MASK, acp_base + ACP_EXTERNAL_INTR_STAT);
66 	writel(0, acp_base + ACP_EXTERNAL_INTR_CNTL);
67 	writel(0, acp_base + ACP_EXTERNAL_INTR_ENB);
68 }
69 
acp63_init(void __iomem * acp_base,struct device * dev)70 static int acp63_init(void __iomem *acp_base, struct device *dev)
71 {
72 	int ret;
73 
74 	ret = acp63_power_on(acp_base);
75 	if (ret) {
76 		dev_err(dev, "ACP power on failed\n");
77 		return ret;
78 	}
79 	writel(0x01, acp_base + ACP_CONTROL);
80 	ret = acp63_reset(acp_base);
81 	if (ret) {
82 		dev_err(dev, "ACP reset failed\n");
83 		return ret;
84 	}
85 	acp63_enable_interrupts(acp_base);
86 	return 0;
87 }
88 
acp63_deinit(void __iomem * acp_base,struct device * dev)89 static int acp63_deinit(void __iomem *acp_base, struct device *dev)
90 {
91 	int ret;
92 
93 	acp63_disable_interrupts(acp_base);
94 	ret = acp63_reset(acp_base);
95 	if (ret) {
96 		dev_err(dev, "ACP reset failed\n");
97 		return ret;
98 	}
99 	writel(0, acp_base + ACP_CONTROL);
100 	return 0;
101 }
102 
acp63_irq_thread(int irq,void * context)103 static irqreturn_t acp63_irq_thread(int irq, void *context)
104 {
105 	struct sdw_dma_dev_data *sdw_dma_data;
106 	struct acp63_dev_data *adata = context;
107 	u32 stream_index;
108 
109 	sdw_dma_data = dev_get_drvdata(&adata->sdw_dma_dev->dev);
110 
111 	for (stream_index = 0; stream_index < ACP63_SDW0_DMA_MAX_STREAMS; stream_index++) {
112 		if (adata->sdw0_dma_intr_stat[stream_index]) {
113 			if (sdw_dma_data->sdw0_dma_stream[stream_index])
114 				snd_pcm_period_elapsed(sdw_dma_data->sdw0_dma_stream[stream_index]);
115 			adata->sdw0_dma_intr_stat[stream_index] = 0;
116 		}
117 	}
118 	for (stream_index = 0; stream_index < ACP63_SDW1_DMA_MAX_STREAMS; stream_index++) {
119 		if (adata->sdw1_dma_intr_stat[stream_index]) {
120 			if (sdw_dma_data->sdw1_dma_stream[stream_index])
121 				snd_pcm_period_elapsed(sdw_dma_data->sdw1_dma_stream[stream_index]);
122 			adata->sdw1_dma_intr_stat[stream_index] = 0;
123 		}
124 	}
125 	return IRQ_HANDLED;
126 }
127 
acp63_irq_handler(int irq,void * dev_id)128 static irqreturn_t acp63_irq_handler(int irq, void *dev_id)
129 {
130 	struct acp63_dev_data *adata;
131 	struct pdm_dev_data *ps_pdm_data;
132 	struct amd_sdw_manager *amd_manager;
133 	u32 ext_intr_stat, ext_intr_stat1;
134 	u32 stream_id = 0;
135 	u16 irq_flag = 0;
136 	u16 sdw_dma_irq_flag = 0;
137 	u16 index;
138 
139 	adata = dev_id;
140 	if (!adata)
141 		return IRQ_NONE;
142 	/* ACP interrupts will be cleared by reading particular bit and writing
143 	 * same value to the status register. writing zero's doesn't have any
144 	 * effect.
145 	 * Bit by bit checking of IRQ field is implemented.
146 	 */
147 	ext_intr_stat = readl(adata->acp63_base + ACP_EXTERNAL_INTR_STAT);
148 	if (ext_intr_stat & ACP_SDW0_STAT) {
149 		writel(ACP_SDW0_STAT, adata->acp63_base + ACP_EXTERNAL_INTR_STAT);
150 		amd_manager = dev_get_drvdata(&adata->sdw->pdev[0]->dev);
151 		if (amd_manager)
152 			schedule_work(&amd_manager->amd_sdw_irq_thread);
153 		irq_flag = 1;
154 	}
155 
156 	ext_intr_stat1 = readl(adata->acp63_base + ACP_EXTERNAL_INTR_STAT1);
157 	if (ext_intr_stat1 & ACP_SDW1_STAT) {
158 		writel(ACP_SDW1_STAT, adata->acp63_base + ACP_EXTERNAL_INTR_STAT1);
159 		amd_manager = dev_get_drvdata(&adata->sdw->pdev[1]->dev);
160 		if (amd_manager)
161 			schedule_work(&amd_manager->amd_sdw_irq_thread);
162 		irq_flag = 1;
163 	}
164 
165 	if (ext_intr_stat & ACP_ERROR_IRQ) {
166 		writel(ACP_ERROR_IRQ, adata->acp63_base + ACP_EXTERNAL_INTR_STAT);
167 		/* TODO: Report SoundWire Manager instance errors */
168 		writel(0, adata->acp63_base + ACP_SW0_I2S_ERROR_REASON);
169 		writel(0, adata->acp63_base + ACP_SW1_I2S_ERROR_REASON);
170 		writel(0, adata->acp63_base + ACP_ERROR_STATUS);
171 		irq_flag = 1;
172 	}
173 
174 	if (ext_intr_stat & BIT(PDM_DMA_STAT)) {
175 		ps_pdm_data = dev_get_drvdata(&adata->pdm_dev->dev);
176 		writel(BIT(PDM_DMA_STAT), adata->acp63_base + ACP_EXTERNAL_INTR_STAT);
177 		if (ps_pdm_data->capture_stream)
178 			snd_pcm_period_elapsed(ps_pdm_data->capture_stream);
179 		irq_flag = 1;
180 	}
181 	if (ext_intr_stat & ACP_SDW_DMA_IRQ_MASK) {
182 		for (index = ACP_AUDIO2_RX_THRESHOLD; index <= ACP_AUDIO0_TX_THRESHOLD; index++) {
183 			if (ext_intr_stat & BIT(index)) {
184 				writel(BIT(index), adata->acp63_base + ACP_EXTERNAL_INTR_STAT);
185 				switch (index) {
186 				case ACP_AUDIO0_TX_THRESHOLD:
187 					stream_id = ACP_SDW0_AUDIO0_TX;
188 					break;
189 				case ACP_AUDIO1_TX_THRESHOLD:
190 					stream_id = ACP_SDW0_AUDIO1_TX;
191 					break;
192 				case ACP_AUDIO2_TX_THRESHOLD:
193 					stream_id = ACP_SDW0_AUDIO2_TX;
194 					break;
195 				case ACP_AUDIO0_RX_THRESHOLD:
196 					stream_id = ACP_SDW0_AUDIO0_RX;
197 					break;
198 				case ACP_AUDIO1_RX_THRESHOLD:
199 					stream_id = ACP_SDW0_AUDIO1_RX;
200 					break;
201 				case ACP_AUDIO2_RX_THRESHOLD:
202 					stream_id = ACP_SDW0_AUDIO2_RX;
203 					break;
204 				}
205 
206 				adata->sdw0_dma_intr_stat[stream_id] = 1;
207 				sdw_dma_irq_flag = 1;
208 			}
209 		}
210 	}
211 
212 	if (ext_intr_stat1 & ACP_P1_AUDIO1_RX_THRESHOLD) {
213 		writel(ACP_P1_AUDIO1_RX_THRESHOLD,
214 		       adata->acp63_base + ACP_EXTERNAL_INTR_STAT1);
215 		adata->sdw1_dma_intr_stat[ACP_SDW1_AUDIO1_RX] = 1;
216 		sdw_dma_irq_flag = 1;
217 	}
218 
219 	if (ext_intr_stat1 & ACP_P1_AUDIO1_TX_THRESHOLD) {
220 		writel(ACP_P1_AUDIO1_TX_THRESHOLD,
221 		       adata->acp63_base + ACP_EXTERNAL_INTR_STAT1);
222 		adata->sdw1_dma_intr_stat[ACP_SDW1_AUDIO1_TX] = 1;
223 		sdw_dma_irq_flag = 1;
224 	}
225 
226 	if (sdw_dma_irq_flag)
227 		return IRQ_WAKE_THREAD;
228 
229 	if (irq_flag)
230 		return IRQ_HANDLED;
231 	else
232 		return IRQ_NONE;
233 }
234 
235 #if IS_ENABLED(CONFIG_SND_SOC_AMD_SOUNDWIRE)
acp_scan_sdw_devices(struct device * dev,u64 addr)236 static int acp_scan_sdw_devices(struct device *dev, u64 addr)
237 {
238 	struct acpi_device *sdw_dev;
239 	struct acp63_dev_data *acp_data;
240 
241 	acp_data = dev_get_drvdata(dev);
242 	if (!addr)
243 		return -ENODEV;
244 
245 	sdw_dev = acpi_find_child_device(ACPI_COMPANION(dev), addr, 0);
246 	if (!sdw_dev)
247 		return -ENODEV;
248 
249 	acp_data->info.handle = sdw_dev->handle;
250 	acp_data->info.count = AMD_SDW_MAX_MANAGERS;
251 	return amd_sdw_scan_controller(&acp_data->info);
252 }
253 
amd_sdw_probe(struct device * dev)254 static int amd_sdw_probe(struct device *dev)
255 {
256 	struct acp63_dev_data *acp_data;
257 	struct sdw_amd_res sdw_res;
258 	int ret;
259 
260 	acp_data = dev_get_drvdata(dev);
261 	memset(&sdw_res, 0, sizeof(sdw_res));
262 	sdw_res.addr = acp_data->addr;
263 	sdw_res.reg_range = acp_data->reg_range;
264 	sdw_res.handle = acp_data->info.handle;
265 	sdw_res.parent = dev;
266 	sdw_res.dev = dev;
267 	sdw_res.acp_lock = &acp_data->acp_lock;
268 	sdw_res.count = acp_data->info.count;
269 	sdw_res.mmio_base = acp_data->acp63_base;
270 	sdw_res.acp_rev = acp_data->acp_rev;
271 	sdw_res.link_mask = acp_data->info.link_mask;
272 	ret = sdw_amd_probe(&sdw_res, &acp_data->sdw);
273 	if (ret)
274 		dev_err(dev, "error: SoundWire probe failed\n");
275 	return ret;
276 }
277 
amd_sdw_exit(struct acp63_dev_data * acp_data)278 static int amd_sdw_exit(struct acp63_dev_data *acp_data)
279 {
280 	if (acp_data->sdw)
281 		sdw_amd_exit(acp_data->sdw);
282 	acp_data->sdw = NULL;
283 
284 	return 0;
285 }
286 
acp63_sdw_machine_select(struct device * dev)287 static struct snd_soc_acpi_mach *acp63_sdw_machine_select(struct device *dev)
288 {
289 	struct snd_soc_acpi_mach *mach;
290 	const struct snd_soc_acpi_link_adr *link;
291 	struct acp63_dev_data *acp_data = dev_get_drvdata(dev);
292 	int ret, i;
293 
294 	if (acp_data->info.count) {
295 		ret = sdw_amd_get_slave_info(acp_data->sdw);
296 		if (ret) {
297 			dev_dbg(dev, "failed to read slave information\n");
298 			return NULL;
299 		}
300 		for (mach = acp_data->machines; mach; mach++) {
301 			if (!mach->links)
302 				break;
303 			link = mach->links;
304 			for (i = 0; i < acp_data->info.count && link->num_adr; link++, i++) {
305 				if (!snd_soc_acpi_sdw_link_slaves_found(dev, link,
306 									acp_data->sdw->peripherals))
307 					break;
308 			}
309 			if (i == acp_data->info.count || !link->num_adr)
310 				break;
311 		}
312 		if (mach && mach->link_mask) {
313 			mach->mach_params.links = mach->links;
314 			mach->mach_params.link_mask = mach->link_mask;
315 			return mach;
316 		}
317 	}
318 	dev_dbg(dev, "No SoundWire machine driver found\n");
319 	return NULL;
320 }
321 #else
acp_scan_sdw_devices(struct device * dev,u64 addr)322 static int acp_scan_sdw_devices(struct device *dev, u64 addr)
323 {
324 	return 0;
325 }
326 
amd_sdw_probe(struct device * dev)327 static int amd_sdw_probe(struct device *dev)
328 {
329 	return 0;
330 }
331 
amd_sdw_exit(struct acp63_dev_data * acp_data)332 static int amd_sdw_exit(struct acp63_dev_data *acp_data)
333 {
334 	return 0;
335 }
336 
acp63_sdw_machine_select(struct device * dev)337 static struct snd_soc_acpi_mach *acp63_sdw_machine_select(struct device *dev)
338 {
339 	return NULL;
340 }
341 #endif
342 
acp63_machine_register(struct device * dev)343 static int acp63_machine_register(struct device *dev)
344 {
345 	struct snd_soc_acpi_mach *mach;
346 	struct acp63_dev_data *adata = dev_get_drvdata(dev);
347 	int size;
348 
349 	if (adata->is_sdw_dev && adata->is_sdw_config) {
350 		size = sizeof(*adata->machines);
351 		mach = acp63_sdw_machine_select(dev);
352 		if (mach) {
353 			adata->mach_dev = platform_device_register_data(dev, mach->drv_name,
354 									PLATFORM_DEVID_NONE, mach,
355 									size);
356 			if (IS_ERR(adata->mach_dev)) {
357 				dev_err(dev,
358 					"cannot register Machine device for SoundWire Interface\n");
359 				return PTR_ERR(adata->mach_dev);
360 			}
361 		}
362 
363 	} else if (adata->is_pdm_dev && !adata->is_sdw_dev && adata->is_pdm_config) {
364 		adata->mach_dev = platform_device_register_data(dev, "acp_ps_mach",
365 								PLATFORM_DEVID_NONE, NULL, 0);
366 		if (IS_ERR(adata->mach_dev)) {
367 			dev_err(dev, "cannot register amd_ps_mach device\n");
368 			return PTR_ERR(adata->mach_dev);
369 		}
370 	}
371 	return 0;
372 }
373 
get_acp63_device_config(struct pci_dev * pci,struct acp63_dev_data * acp_data)374 static int get_acp63_device_config(struct pci_dev *pci, struct acp63_dev_data *acp_data)
375 {
376 	struct acpi_device *pdm_dev;
377 	const union acpi_object *obj;
378 	u32 config;
379 	bool is_dmic_dev = false;
380 	bool is_sdw_dev = false;
381 	int ret;
382 
383 	config = readl(acp_data->acp63_base + ACP_PIN_CONFIG);
384 	switch (config) {
385 	case ACP_CONFIG_4:
386 	case ACP_CONFIG_5:
387 	case ACP_CONFIG_10:
388 	case ACP_CONFIG_11:
389 		acp_data->is_pdm_config = true;
390 		break;
391 	case ACP_CONFIG_2:
392 	case ACP_CONFIG_3:
393 		acp_data->is_sdw_config = true;
394 		break;
395 	case ACP_CONFIG_6:
396 	case ACP_CONFIG_7:
397 	case ACP_CONFIG_12:
398 	case ACP_CONFIG_8:
399 	case ACP_CONFIG_13:
400 	case ACP_CONFIG_14:
401 		acp_data->is_pdm_config = true;
402 		acp_data->is_sdw_config = true;
403 		break;
404 	default:
405 		break;
406 	}
407 
408 	if (acp_data->is_pdm_config) {
409 		pdm_dev = acpi_find_child_device(ACPI_COMPANION(&pci->dev), ACP63_DMIC_ADDR, 0);
410 		if (pdm_dev) {
411 			/* is_dmic_dev flag will be set when ACP PDM controller device exists */
412 			if (!acpi_dev_get_property(pdm_dev, "acp-audio-device-type",
413 						   ACPI_TYPE_INTEGER, &obj) &&
414 						   obj->integer.value == ACP_DMIC_DEV)
415 				is_dmic_dev = true;
416 		}
417 	}
418 
419 	if (acp_data->is_sdw_config) {
420 		ret = acp_scan_sdw_devices(&pci->dev, ACP63_SDW_ADDR);
421 		if (!ret && acp_data->info.link_mask)
422 			is_sdw_dev = true;
423 	}
424 
425 	acp_data->is_pdm_dev = is_dmic_dev;
426 	acp_data->is_sdw_dev = is_sdw_dev;
427 	if (!is_dmic_dev && !is_sdw_dev) {
428 		dev_dbg(&pci->dev, "No PDM or SoundWire manager devices found\n");
429 		return -ENODEV;
430 	}
431 	return 0;
432 }
433 
acp63_fill_platform_dev_info(struct platform_device_info * pdevinfo,struct device * parent,struct fwnode_handle * fw_node,char * name,unsigned int id,const struct resource * res,unsigned int num_res,const void * data,size_t size_data)434 static void acp63_fill_platform_dev_info(struct platform_device_info *pdevinfo,
435 					 struct device *parent,
436 					 struct fwnode_handle *fw_node,
437 					 char *name, unsigned int id,
438 					 const struct resource *res,
439 					 unsigned int num_res,
440 					 const void *data,
441 					 size_t size_data)
442 {
443 	pdevinfo->name = name;
444 	pdevinfo->id = id;
445 	pdevinfo->parent = parent;
446 	pdevinfo->num_res = num_res;
447 	pdevinfo->res = res;
448 	pdevinfo->data = data;
449 	pdevinfo->size_data = size_data;
450 	pdevinfo->fwnode = fw_node;
451 }
452 
create_acp63_platform_devs(struct pci_dev * pci,struct acp63_dev_data * adata,u32 addr)453 static int create_acp63_platform_devs(struct pci_dev *pci, struct acp63_dev_data *adata, u32 addr)
454 {
455 	struct platform_device_info pdevinfo;
456 	struct device *parent;
457 	int ret;
458 
459 	parent = &pci->dev;
460 
461 	if (adata->is_sdw_dev || adata->is_pdm_dev) {
462 		adata->res = devm_kzalloc(&pci->dev, sizeof(struct resource), GFP_KERNEL);
463 		if (!adata->res) {
464 			ret = -ENOMEM;
465 			goto de_init;
466 		}
467 		adata->res->flags = IORESOURCE_MEM;
468 		adata->res->start = addr;
469 		adata->res->end = addr + (ACP63_REG_END - ACP63_REG_START);
470 		memset(&pdevinfo, 0, sizeof(pdevinfo));
471 	}
472 
473 	if (adata->is_pdm_dev && adata->is_pdm_config) {
474 		acp63_fill_platform_dev_info(&pdevinfo, parent, NULL, "acp_ps_pdm_dma",
475 					     0, adata->res, 1, NULL, 0);
476 
477 		adata->pdm_dev = platform_device_register_full(&pdevinfo);
478 		if (IS_ERR(adata->pdm_dev)) {
479 			dev_err(&pci->dev,
480 				"cannot register %s device\n", pdevinfo.name);
481 			ret = PTR_ERR(adata->pdm_dev);
482 			goto de_init;
483 		}
484 		memset(&pdevinfo, 0, sizeof(pdevinfo));
485 		acp63_fill_platform_dev_info(&pdevinfo, parent, NULL, "dmic-codec",
486 					     0, NULL, 0, NULL, 0);
487 		adata->dmic_codec_dev = platform_device_register_full(&pdevinfo);
488 		if (IS_ERR(adata->dmic_codec_dev)) {
489 			dev_err(&pci->dev,
490 				"cannot register %s device\n", pdevinfo.name);
491 			ret = PTR_ERR(adata->dmic_codec_dev);
492 			goto unregister_pdm_dev;
493 		}
494 	}
495 	if (adata->is_sdw_dev && adata->is_sdw_config) {
496 		ret = amd_sdw_probe(&pci->dev);
497 		if (ret) {
498 			if (adata->is_pdm_dev)
499 				goto unregister_dmic_codec_dev;
500 			else
501 				goto de_init;
502 		}
503 		memset(&pdevinfo, 0, sizeof(pdevinfo));
504 		acp63_fill_platform_dev_info(&pdevinfo, parent, NULL, "amd_ps_sdw_dma",
505 					     0, adata->res, 1, NULL, 0);
506 
507 		adata->sdw_dma_dev = platform_device_register_full(&pdevinfo);
508 		if (IS_ERR(adata->sdw_dma_dev)) {
509 			dev_err(&pci->dev,
510 				"cannot register %s device\n", pdevinfo.name);
511 			ret = PTR_ERR(adata->sdw_dma_dev);
512 			if (adata->is_pdm_dev)
513 				goto unregister_dmic_codec_dev;
514 			else
515 				goto de_init;
516 		}
517 	}
518 
519 	return 0;
520 unregister_dmic_codec_dev:
521 		platform_device_unregister(adata->dmic_codec_dev);
522 unregister_pdm_dev:
523 		platform_device_unregister(adata->pdm_dev);
524 de_init:
525 	if (acp63_deinit(adata->acp63_base, &pci->dev))
526 		dev_err(&pci->dev, "ACP de-init failed\n");
527 	return ret;
528 }
529 
snd_acp63_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)530 static int snd_acp63_probe(struct pci_dev *pci,
531 			   const struct pci_device_id *pci_id)
532 {
533 	struct acp63_dev_data *adata;
534 	u32 addr;
535 	u32 irqflags, flag;
536 	int ret;
537 
538 	irqflags = IRQF_SHARED;
539 
540 	/* Return if acp config flag is defined */
541 	flag = snd_amd_acp_find_config(pci);
542 	if (flag)
543 		return -ENODEV;
544 
545 	/* Pink Sardine device check */
546 	switch (pci->revision) {
547 	case 0x63:
548 		break;
549 	default:
550 		dev_dbg(&pci->dev, "acp63 pci device not found\n");
551 		return -ENODEV;
552 	}
553 	if (pci_enable_device(pci)) {
554 		dev_err(&pci->dev, "pci_enable_device failed\n");
555 		return -ENODEV;
556 	}
557 
558 	ret = pci_request_regions(pci, "AMD ACP6.2 audio");
559 	if (ret < 0) {
560 		dev_err(&pci->dev, "pci_request_regions failed\n");
561 		goto disable_pci;
562 	}
563 	adata = devm_kzalloc(&pci->dev, sizeof(struct acp63_dev_data),
564 			     GFP_KERNEL);
565 	if (!adata) {
566 		ret = -ENOMEM;
567 		goto release_regions;
568 	}
569 
570 	addr = pci_resource_start(pci, 0);
571 	adata->acp63_base = devm_ioremap(&pci->dev, addr,
572 					 pci_resource_len(pci, 0));
573 	if (!adata->acp63_base) {
574 		ret = -ENOMEM;
575 		goto release_regions;
576 	}
577 	adata->addr = addr;
578 	adata->reg_range = ACP63_REG_END - ACP63_REG_START;
579 	adata->acp_rev = pci->revision;
580 	pci_set_master(pci);
581 	pci_set_drvdata(pci, adata);
582 	mutex_init(&adata->acp_lock);
583 	ret = acp63_init(adata->acp63_base, &pci->dev);
584 	if (ret)
585 		goto release_regions;
586 	ret = devm_request_threaded_irq(&pci->dev, pci->irq, acp63_irq_handler,
587 					acp63_irq_thread, irqflags, "ACP_PCI_IRQ", adata);
588 	if (ret) {
589 		dev_err(&pci->dev, "ACP PCI IRQ request failed\n");
590 		goto de_init;
591 	}
592 	ret = get_acp63_device_config(pci, adata);
593 	/* ACP PCI driver probe should be continued even PDM or SoundWire Devices are not found */
594 	if (ret) {
595 		dev_dbg(&pci->dev, "get acp device config failed:%d\n", ret);
596 		goto skip_pdev_creation;
597 	}
598 	ret = create_acp63_platform_devs(pci, adata, addr);
599 	if (ret < 0) {
600 		dev_err(&pci->dev, "ACP platform devices creation failed\n");
601 		goto de_init;
602 	}
603 	adata->machines = snd_soc_acpi_amd_acp63_sdw_machines;
604 	ret = acp63_machine_register(&pci->dev);
605 	if (ret) {
606 		dev_err(&pci->dev, "ACP machine register failed\n");
607 		goto de_init;
608 	}
609 skip_pdev_creation:
610 	device_set_wakeup_enable(&pci->dev, true);
611 	pm_runtime_set_autosuspend_delay(&pci->dev, ACP_SUSPEND_DELAY_MS);
612 	pm_runtime_use_autosuspend(&pci->dev);
613 	pm_runtime_put_noidle(&pci->dev);
614 	pm_runtime_allow(&pci->dev);
615 	return 0;
616 de_init:
617 	if (acp63_deinit(adata->acp63_base, &pci->dev))
618 		dev_err(&pci->dev, "ACP de-init failed\n");
619 release_regions:
620 	pci_release_regions(pci);
621 disable_pci:
622 	pci_disable_device(pci);
623 
624 	return ret;
625 }
626 
check_acp_sdw_enable_status(struct acp63_dev_data * adata)627 static bool check_acp_sdw_enable_status(struct acp63_dev_data *adata)
628 {
629 	u32 sdw0_en, sdw1_en;
630 
631 	sdw0_en = readl(adata->acp63_base + ACP_SW0_EN);
632 	sdw1_en = readl(adata->acp63_base + ACP_SW1_EN);
633 	return (sdw0_en || sdw1_en);
634 }
635 
handle_acp63_sdw_pme_event(struct acp63_dev_data * adata)636 static void handle_acp63_sdw_pme_event(struct acp63_dev_data *adata)
637 {
638 	u32 val;
639 
640 	val = readl(adata->acp63_base + ACP_SW0_WAKE_EN);
641 	if (val && adata->sdw->pdev[0])
642 		pm_request_resume(&adata->sdw->pdev[0]->dev);
643 
644 	val = readl(adata->acp63_base + ACP_SW1_WAKE_EN);
645 	if (val && adata->sdw->pdev[1])
646 		pm_request_resume(&adata->sdw->pdev[1]->dev);
647 }
648 
snd_acp63_suspend(struct device * dev)649 static int __maybe_unused snd_acp63_suspend(struct device *dev)
650 {
651 	struct acp63_dev_data *adata;
652 	int ret;
653 
654 	adata = dev_get_drvdata(dev);
655 	if (adata->is_sdw_dev) {
656 		adata->sdw_en_stat = check_acp_sdw_enable_status(adata);
657 		if (adata->sdw_en_stat)
658 			return 0;
659 	}
660 	ret = acp63_deinit(adata->acp63_base, dev);
661 	if (ret)
662 		dev_err(dev, "ACP de-init failed\n");
663 
664 	return ret;
665 }
666 
snd_acp63_runtime_resume(struct device * dev)667 static int __maybe_unused snd_acp63_runtime_resume(struct device *dev)
668 {
669 	struct acp63_dev_data *adata;
670 	int ret;
671 
672 	adata = dev_get_drvdata(dev);
673 	if (adata->sdw_en_stat)
674 		return 0;
675 
676 	ret = acp63_init(adata->acp63_base, dev);
677 	if (ret) {
678 		dev_err(dev, "ACP init failed\n");
679 		return ret;
680 	}
681 
682 	if (!adata->sdw_en_stat)
683 		handle_acp63_sdw_pme_event(adata);
684 	return 0;
685 }
686 
snd_acp63_resume(struct device * dev)687 static int __maybe_unused snd_acp63_resume(struct device *dev)
688 {
689 	struct acp63_dev_data *adata;
690 	int ret;
691 
692 	adata = dev_get_drvdata(dev);
693 	if (adata->sdw_en_stat)
694 		return 0;
695 
696 	ret = acp63_init(adata->acp63_base, dev);
697 	if (ret)
698 		dev_err(dev, "ACP init failed\n");
699 
700 	return ret;
701 }
702 
703 static const struct dev_pm_ops acp63_pm_ops = {
704 	SET_RUNTIME_PM_OPS(snd_acp63_suspend, snd_acp63_runtime_resume, NULL)
705 	SET_SYSTEM_SLEEP_PM_OPS(snd_acp63_suspend, snd_acp63_resume)
706 };
707 
snd_acp63_remove(struct pci_dev * pci)708 static void snd_acp63_remove(struct pci_dev *pci)
709 {
710 	struct acp63_dev_data *adata;
711 	int ret;
712 
713 	adata = pci_get_drvdata(pci);
714 	if (adata->sdw) {
715 		amd_sdw_exit(adata);
716 		platform_device_unregister(adata->sdw_dma_dev);
717 	}
718 	if (adata->is_pdm_dev) {
719 		platform_device_unregister(adata->pdm_dev);
720 		platform_device_unregister(adata->dmic_codec_dev);
721 	}
722 	if (adata->mach_dev)
723 		platform_device_unregister(adata->mach_dev);
724 	ret = acp63_deinit(adata->acp63_base, &pci->dev);
725 	if (ret)
726 		dev_err(&pci->dev, "ACP de-init failed\n");
727 	pm_runtime_forbid(&pci->dev);
728 	pm_runtime_get_noresume(&pci->dev);
729 	pci_release_regions(pci);
730 	pci_disable_device(pci);
731 }
732 
733 static const struct pci_device_id snd_acp63_ids[] = {
734 	{ PCI_DEVICE(PCI_VENDOR_ID_AMD, ACP_DEVICE_ID),
735 	.class = PCI_CLASS_MULTIMEDIA_OTHER << 8,
736 	.class_mask = 0xffffff },
737 	{ 0, },
738 };
739 MODULE_DEVICE_TABLE(pci, snd_acp63_ids);
740 
741 static struct pci_driver ps_acp63_driver  = {
742 	.name = KBUILD_MODNAME,
743 	.id_table = snd_acp63_ids,
744 	.probe = snd_acp63_probe,
745 	.remove = snd_acp63_remove,
746 	.driver = {
747 		.pm = &acp63_pm_ops,
748 	}
749 };
750 
751 module_pci_driver(ps_acp63_driver);
752 
753 MODULE_AUTHOR("Vijendar.Mukunda@amd.com");
754 MODULE_AUTHOR("Syed.SabaKareem@amd.com");
755 MODULE_DESCRIPTION("AMD ACP Pink Sardine PCI driver");
756 MODULE_IMPORT_NS("SOUNDWIRE_AMD_INIT");
757 MODULE_IMPORT_NS("SND_AMD_SOUNDWIRE_ACPI");
758 MODULE_LICENSE("GPL v2");
759