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