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