xref: /linux/sound/pci/cs5535audio/cs5535audio.c (revision a1c3be890440a1769ed6f822376a3e3ab0d42994)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Driver for audio on multifunction CS5535/6 companion device
4  * Copyright (C) Jaya Kumar
5  *
6  * Based on Jaroslav Kysela and Takashi Iwai's examples.
7  * This work was sponsored by CIS(M) Sdn Bhd.
8  */
9 
10 #include <linux/delay.h>
11 #include <linux/interrupt.h>
12 #include <linux/init.h>
13 #include <linux/pci.h>
14 #include <linux/slab.h>
15 #include <linux/module.h>
16 #include <linux/io.h>
17 #include <sound/core.h>
18 #include <sound/control.h>
19 #include <sound/pcm.h>
20 #include <sound/rawmidi.h>
21 #include <sound/ac97_codec.h>
22 #include <sound/initval.h>
23 #include <sound/asoundef.h>
24 #include "cs5535audio.h"
25 
26 #define DRIVER_NAME "cs5535audio"
27 
28 static char *ac97_quirk;
29 module_param(ac97_quirk, charp, 0444);
30 MODULE_PARM_DESC(ac97_quirk, "AC'97 board specific workarounds.");
31 
32 static const struct ac97_quirk ac97_quirks[] = {
33 #if 0 /* Not yet confirmed if all 5536 boards are HP only */
34 	{
35 		.subvendor = PCI_VENDOR_ID_AMD,
36 		.subdevice = PCI_DEVICE_ID_AMD_CS5536_AUDIO,
37 		.name = "AMD RDK",
38 		.type = AC97_TUNE_HP_ONLY
39 	},
40 #endif
41 	{}
42 };
43 
44 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
45 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
46 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
47 
48 module_param_array(index, int, NULL, 0444);
49 MODULE_PARM_DESC(index, "Index value for " DRIVER_NAME);
50 module_param_array(id, charp, NULL, 0444);
51 MODULE_PARM_DESC(id, "ID string for " DRIVER_NAME);
52 module_param_array(enable, bool, NULL, 0444);
53 MODULE_PARM_DESC(enable, "Enable " DRIVER_NAME);
54 
55 static const struct pci_device_id snd_cs5535audio_ids[] = {
56 	{ PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_CS5535_AUDIO) },
57 	{ PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_CS5536_AUDIO) },
58 	{}
59 };
60 
61 MODULE_DEVICE_TABLE(pci, snd_cs5535audio_ids);
62 
63 static void wait_till_cmd_acked(struct cs5535audio *cs5535au, unsigned long timeout)
64 {
65 	unsigned int tmp;
66 	do {
67 		tmp = cs_readl(cs5535au, ACC_CODEC_CNTL);
68 		if (!(tmp & CMD_NEW))
69 			break;
70 		udelay(1);
71 	} while (--timeout);
72 	if (!timeout)
73 		dev_err(cs5535au->card->dev,
74 			"Failure writing to cs5535 codec\n");
75 }
76 
77 static unsigned short snd_cs5535audio_codec_read(struct cs5535audio *cs5535au,
78 						 unsigned short reg)
79 {
80 	unsigned int regdata;
81 	unsigned int timeout;
82 	unsigned int val;
83 
84 	regdata = ((unsigned int) reg) << 24;
85 	regdata |= ACC_CODEC_CNTL_RD_CMD;
86 	regdata |= CMD_NEW;
87 
88 	cs_writel(cs5535au, ACC_CODEC_CNTL, regdata);
89 	wait_till_cmd_acked(cs5535au, 50);
90 
91 	timeout = 50;
92 	do {
93 		val = cs_readl(cs5535au, ACC_CODEC_STATUS);
94 		if ((val & STS_NEW) && reg == (val >> 24))
95 			break;
96 		udelay(1);
97 	} while (--timeout);
98 	if (!timeout)
99 		dev_err(cs5535au->card->dev,
100 			"Failure reading codec reg 0x%x, Last value=0x%x\n",
101 			reg, val);
102 
103 	return (unsigned short) val;
104 }
105 
106 static void snd_cs5535audio_codec_write(struct cs5535audio *cs5535au,
107 					unsigned short reg, unsigned short val)
108 {
109 	unsigned int regdata;
110 
111 	regdata = ((unsigned int) reg) << 24;
112 	regdata |= val;
113 	regdata &= CMD_MASK;
114 	regdata |= CMD_NEW;
115 	regdata &= ACC_CODEC_CNTL_WR_CMD;
116 
117 	cs_writel(cs5535au, ACC_CODEC_CNTL, regdata);
118 	wait_till_cmd_acked(cs5535au, 50);
119 }
120 
121 static void snd_cs5535audio_ac97_codec_write(struct snd_ac97 *ac97,
122 					     unsigned short reg, unsigned short val)
123 {
124 	struct cs5535audio *cs5535au = ac97->private_data;
125 	snd_cs5535audio_codec_write(cs5535au, reg, val);
126 }
127 
128 static unsigned short snd_cs5535audio_ac97_codec_read(struct snd_ac97 *ac97,
129 						      unsigned short reg)
130 {
131 	struct cs5535audio *cs5535au = ac97->private_data;
132 	return snd_cs5535audio_codec_read(cs5535au, reg);
133 }
134 
135 static int snd_cs5535audio_mixer(struct cs5535audio *cs5535au)
136 {
137 	struct snd_card *card = cs5535au->card;
138 	struct snd_ac97_bus *pbus;
139 	struct snd_ac97_template ac97;
140 	int err;
141 	static const struct snd_ac97_bus_ops ops = {
142 		.write = snd_cs5535audio_ac97_codec_write,
143 		.read = snd_cs5535audio_ac97_codec_read,
144 	};
145 
146 	if ((err = snd_ac97_bus(card, 0, &ops, NULL, &pbus)) < 0)
147 		return err;
148 
149 	memset(&ac97, 0, sizeof(ac97));
150 	ac97.scaps = AC97_SCAP_AUDIO | AC97_SCAP_SKIP_MODEM
151 			| AC97_SCAP_POWER_SAVE;
152 	ac97.private_data = cs5535au;
153 	ac97.pci = cs5535au->pci;
154 
155 	/* set any OLPC-specific scaps */
156 	olpc_prequirks(card, &ac97);
157 
158 	if ((err = snd_ac97_mixer(pbus, &ac97, &cs5535au->ac97)) < 0) {
159 		dev_err(card->dev, "mixer failed\n");
160 		return err;
161 	}
162 
163 	snd_ac97_tune_hardware(cs5535au->ac97, ac97_quirks, ac97_quirk);
164 
165 	err = olpc_quirks(card, cs5535au->ac97);
166 	if (err < 0) {
167 		dev_err(card->dev, "olpc quirks failed\n");
168 		return err;
169 	}
170 
171 	return 0;
172 }
173 
174 static void process_bm0_irq(struct cs5535audio *cs5535au)
175 {
176 	u8 bm_stat;
177 	spin_lock(&cs5535au->reg_lock);
178 	bm_stat = cs_readb(cs5535au, ACC_BM0_STATUS);
179 	spin_unlock(&cs5535au->reg_lock);
180 	if (bm_stat & EOP) {
181 		snd_pcm_period_elapsed(cs5535au->playback_substream);
182 	} else {
183 		dev_err(cs5535au->card->dev,
184 			"unexpected bm0 irq src, bm_stat=%x\n",
185 			bm_stat);
186 	}
187 }
188 
189 static void process_bm1_irq(struct cs5535audio *cs5535au)
190 {
191 	u8 bm_stat;
192 	spin_lock(&cs5535au->reg_lock);
193 	bm_stat = cs_readb(cs5535au, ACC_BM1_STATUS);
194 	spin_unlock(&cs5535au->reg_lock);
195 	if (bm_stat & EOP)
196 		snd_pcm_period_elapsed(cs5535au->capture_substream);
197 }
198 
199 static irqreturn_t snd_cs5535audio_interrupt(int irq, void *dev_id)
200 {
201 	u16 acc_irq_stat;
202 	unsigned char count;
203 	struct cs5535audio *cs5535au = dev_id;
204 
205 	if (cs5535au == NULL)
206 		return IRQ_NONE;
207 
208 	acc_irq_stat = cs_readw(cs5535au, ACC_IRQ_STATUS);
209 
210 	if (!acc_irq_stat)
211 		return IRQ_NONE;
212 	for (count = 0; count < 4; count++) {
213 		if (acc_irq_stat & (1 << count)) {
214 			switch (count) {
215 			case IRQ_STS:
216 				cs_readl(cs5535au, ACC_GPIO_STATUS);
217 				break;
218 			case WU_IRQ_STS:
219 				cs_readl(cs5535au, ACC_GPIO_STATUS);
220 				break;
221 			case BM0_IRQ_STS:
222 				process_bm0_irq(cs5535au);
223 				break;
224 			case BM1_IRQ_STS:
225 				process_bm1_irq(cs5535au);
226 				break;
227 			default:
228 				dev_err(cs5535au->card->dev,
229 					"Unexpected irq src: 0x%x\n",
230 					acc_irq_stat);
231 				break;
232 			}
233 		}
234 	}
235 	return IRQ_HANDLED;
236 }
237 
238 static int snd_cs5535audio_free(struct cs5535audio *cs5535au)
239 {
240 	pci_set_power_state(cs5535au->pci, PCI_D3hot);
241 
242 	if (cs5535au->irq >= 0)
243 		free_irq(cs5535au->irq, cs5535au);
244 
245 	pci_release_regions(cs5535au->pci);
246 	pci_disable_device(cs5535au->pci);
247 	kfree(cs5535au);
248 	return 0;
249 }
250 
251 static int snd_cs5535audio_dev_free(struct snd_device *device)
252 {
253 	struct cs5535audio *cs5535au = device->device_data;
254 	return snd_cs5535audio_free(cs5535au);
255 }
256 
257 static int snd_cs5535audio_create(struct snd_card *card,
258 				  struct pci_dev *pci,
259 				  struct cs5535audio **rcs5535au)
260 {
261 	struct cs5535audio *cs5535au;
262 
263 	int err;
264 	static const struct snd_device_ops ops = {
265 		.dev_free =	snd_cs5535audio_dev_free,
266 	};
267 
268 	*rcs5535au = NULL;
269 	if ((err = pci_enable_device(pci)) < 0)
270 		return err;
271 
272 	if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(32))) {
273 		dev_warn(card->dev, "unable to get 32bit dma\n");
274 		err = -ENXIO;
275 		goto pcifail;
276 	}
277 
278 	cs5535au = kzalloc(sizeof(*cs5535au), GFP_KERNEL);
279 	if (cs5535au == NULL) {
280 		err = -ENOMEM;
281 		goto pcifail;
282 	}
283 
284 	spin_lock_init(&cs5535au->reg_lock);
285 	cs5535au->card = card;
286 	cs5535au->pci = pci;
287 	cs5535au->irq = -1;
288 
289 	if ((err = pci_request_regions(pci, "CS5535 Audio")) < 0) {
290 		kfree(cs5535au);
291 		goto pcifail;
292 	}
293 
294 	cs5535au->port = pci_resource_start(pci, 0);
295 
296 	if (request_irq(pci->irq, snd_cs5535audio_interrupt,
297 			IRQF_SHARED, KBUILD_MODNAME, cs5535au)) {
298 		dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
299 		err = -EBUSY;
300 		goto sndfail;
301 	}
302 
303 	cs5535au->irq = pci->irq;
304 	card->sync_irq = cs5535au->irq;
305 	pci_set_master(pci);
306 
307 	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
308 				  cs5535au, &ops)) < 0)
309 		goto sndfail;
310 
311 	*rcs5535au = cs5535au;
312 	return 0;
313 
314 sndfail: /* leave the device alive, just kill the snd */
315 	snd_cs5535audio_free(cs5535au);
316 	return err;
317 
318 pcifail:
319 	pci_disable_device(pci);
320 	return err;
321 }
322 
323 static int snd_cs5535audio_probe(struct pci_dev *pci,
324 				 const struct pci_device_id *pci_id)
325 {
326 	static int dev;
327 	struct snd_card *card;
328 	struct cs5535audio *cs5535au;
329 	int err;
330 
331 	if (dev >= SNDRV_CARDS)
332 		return -ENODEV;
333 	if (!enable[dev]) {
334 		dev++;
335 		return -ENOENT;
336 	}
337 
338 	err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
339 			   0, &card);
340 	if (err < 0)
341 		return err;
342 
343 	if ((err = snd_cs5535audio_create(card, pci, &cs5535au)) < 0)
344 		goto probefail_out;
345 
346 	card->private_data = cs5535au;
347 
348 	if ((err = snd_cs5535audio_mixer(cs5535au)) < 0)
349 		goto probefail_out;
350 
351 	if ((err = snd_cs5535audio_pcm(cs5535au)) < 0)
352 		goto probefail_out;
353 
354 	strcpy(card->driver, DRIVER_NAME);
355 
356 	strcpy(card->shortname, "CS5535 Audio");
357 	sprintf(card->longname, "%s %s at 0x%lx, irq %i",
358 		card->shortname, card->driver,
359 		cs5535au->port, cs5535au->irq);
360 
361 	if ((err = snd_card_register(card)) < 0)
362 		goto probefail_out;
363 
364 	pci_set_drvdata(pci, card);
365 	dev++;
366 	return 0;
367 
368 probefail_out:
369 	snd_card_free(card);
370 	return err;
371 }
372 
373 static void snd_cs5535audio_remove(struct pci_dev *pci)
374 {
375 	olpc_quirks_cleanup();
376 	snd_card_free(pci_get_drvdata(pci));
377 }
378 
379 static struct pci_driver cs5535audio_driver = {
380 	.name = KBUILD_MODNAME,
381 	.id_table = snd_cs5535audio_ids,
382 	.probe = snd_cs5535audio_probe,
383 	.remove = snd_cs5535audio_remove,
384 #ifdef CONFIG_PM_SLEEP
385 	.driver = {
386 		.pm = &snd_cs5535audio_pm,
387 	},
388 #endif
389 };
390 
391 module_pci_driver(cs5535audio_driver);
392 
393 MODULE_AUTHOR("Jaya Kumar");
394 MODULE_LICENSE("GPL");
395 MODULE_DESCRIPTION("CS5535 Audio");
396 MODULE_SUPPORTED_DEVICE("CS5535 Audio");
397