xref: /openbmc/linux/sound/pci/vx222/vx222.c (revision e481ff3f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Driver for Digigram VX222 V2/Mic PCI soundcards
4  *
5  * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
6  */
7 
8 #include <linux/init.h>
9 #include <linux/interrupt.h>
10 #include <linux/pci.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <sound/core.h>
14 #include <sound/initval.h>
15 #include <sound/tlv.h>
16 #include "vx222.h"
17 
18 #define CARD_NAME "VX222"
19 
20 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
21 MODULE_DESCRIPTION("Digigram VX222 V2/Mic");
22 MODULE_LICENSE("GPL");
23 
24 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
25 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
26 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable this card */
27 static bool mic[SNDRV_CARDS]; /* microphone */
28 static int ibl[SNDRV_CARDS]; /* microphone */
29 
30 module_param_array(index, int, NULL, 0444);
31 MODULE_PARM_DESC(index, "Index value for Digigram " CARD_NAME " soundcard.");
32 module_param_array(id, charp, NULL, 0444);
33 MODULE_PARM_DESC(id, "ID string for Digigram " CARD_NAME " soundcard.");
34 module_param_array(enable, bool, NULL, 0444);
35 MODULE_PARM_DESC(enable, "Enable Digigram " CARD_NAME " soundcard.");
36 module_param_array(mic, bool, NULL, 0444);
37 MODULE_PARM_DESC(mic, "Enable Microphone.");
38 module_param_array(ibl, int, NULL, 0444);
39 MODULE_PARM_DESC(ibl, "Capture IBL size.");
40 
41 /*
42  */
43 
44 enum {
45 	VX_PCI_VX222_OLD,
46 	VX_PCI_VX222_NEW
47 };
48 
49 static const struct pci_device_id snd_vx222_ids[] = {
50 	{ 0x10b5, 0x9050, 0x1369, PCI_ANY_ID, 0, 0, VX_PCI_VX222_OLD, },   /* PLX */
51 	{ 0x10b5, 0x9030, 0x1369, PCI_ANY_ID, 0, 0, VX_PCI_VX222_NEW, },   /* PLX */
52 	{ 0, }
53 };
54 
55 MODULE_DEVICE_TABLE(pci, snd_vx222_ids);
56 
57 
58 /*
59  */
60 
61 static const DECLARE_TLV_DB_SCALE(db_scale_old_vol, -11350, 50, 0);
62 static const DECLARE_TLV_DB_SCALE(db_scale_akm, -7350, 50, 0);
63 
64 static const struct snd_vx_hardware vx222_old_hw = {
65 
66 	.name = "VX222/Old",
67 	.type = VX_TYPE_BOARD,
68 	/* hw specs */
69 	.num_codecs = 1,
70 	.num_ins = 1,
71 	.num_outs = 1,
72 	.output_level_max = VX_ANALOG_OUT_LEVEL_MAX,
73 	.output_level_db_scale = db_scale_old_vol,
74 };
75 
76 static const struct snd_vx_hardware vx222_v2_hw = {
77 
78 	.name = "VX222/v2",
79 	.type = VX_TYPE_V2,
80 	/* hw specs */
81 	.num_codecs = 1,
82 	.num_ins = 1,
83 	.num_outs = 1,
84 	.output_level_max = VX2_AKM_LEVEL_MAX,
85 	.output_level_db_scale = db_scale_akm,
86 };
87 
88 static const struct snd_vx_hardware vx222_mic_hw = {
89 
90 	.name = "VX222/Mic",
91 	.type = VX_TYPE_MIC,
92 	/* hw specs */
93 	.num_codecs = 1,
94 	.num_ins = 1,
95 	.num_outs = 1,
96 	.output_level_max = VX2_AKM_LEVEL_MAX,
97 	.output_level_db_scale = db_scale_akm,
98 };
99 
100 
101 /*
102  */
103 static int snd_vx222_free(struct vx_core *chip)
104 {
105 	struct snd_vx222 *vx = to_vx222(chip);
106 
107 	if (chip->irq >= 0)
108 		free_irq(chip->irq, (void*)chip);
109 	if (vx->port[0])
110 		pci_release_regions(vx->pci);
111 	pci_disable_device(vx->pci);
112 	kfree(chip);
113 	return 0;
114 }
115 
116 static int snd_vx222_dev_free(struct snd_device *device)
117 {
118 	struct vx_core *chip = device->device_data;
119 	return snd_vx222_free(chip);
120 }
121 
122 
123 static int snd_vx222_create(struct snd_card *card, struct pci_dev *pci,
124 			    const struct snd_vx_hardware *hw,
125 			    struct snd_vx222 **rchip)
126 {
127 	struct vx_core *chip;
128 	struct snd_vx222 *vx;
129 	int i, err;
130 	static const struct snd_device_ops ops = {
131 		.dev_free =	snd_vx222_dev_free,
132 	};
133 	const struct snd_vx_ops *vx_ops;
134 
135 	/* enable PCI device */
136 	err = pci_enable_device(pci);
137 	if (err < 0)
138 		return err;
139 	pci_set_master(pci);
140 
141 	vx_ops = hw->type == VX_TYPE_BOARD ? &vx222_old_ops : &vx222_ops;
142 	chip = snd_vx_create(card, hw, vx_ops,
143 			     sizeof(struct snd_vx222) - sizeof(struct vx_core));
144 	if (! chip) {
145 		pci_disable_device(pci);
146 		return -ENOMEM;
147 	}
148 	vx = to_vx222(chip);
149 	vx->pci = pci;
150 
151 	err = pci_request_regions(pci, CARD_NAME);
152 	if (err < 0) {
153 		snd_vx222_free(chip);
154 		return err;
155 	}
156 	for (i = 0; i < 2; i++)
157 		vx->port[i] = pci_resource_start(pci, i + 1);
158 
159 	if (request_threaded_irq(pci->irq, snd_vx_irq_handler,
160 				 snd_vx_threaded_irq_handler, IRQF_SHARED,
161 				 KBUILD_MODNAME, chip)) {
162 		dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
163 		snd_vx222_free(chip);
164 		return -EBUSY;
165 	}
166 	chip->irq = pci->irq;
167 	card->sync_irq = chip->irq;
168 
169 	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
170 	if (err < 0) {
171 		snd_vx222_free(chip);
172 		return err;
173 	}
174 
175 	*rchip = vx;
176 	return 0;
177 }
178 
179 
180 static int snd_vx222_probe(struct pci_dev *pci,
181 			   const struct pci_device_id *pci_id)
182 {
183 	static int dev;
184 	struct snd_card *card;
185 	const struct snd_vx_hardware *hw;
186 	struct snd_vx222 *vx;
187 	int err;
188 
189 	if (dev >= SNDRV_CARDS)
190 		return -ENODEV;
191 	if (!enable[dev]) {
192 		dev++;
193 		return -ENOENT;
194 	}
195 
196 	err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
197 			   0, &card);
198 	if (err < 0)
199 		return err;
200 
201 	switch ((int)pci_id->driver_data) {
202 	case VX_PCI_VX222_OLD:
203 		hw = &vx222_old_hw;
204 		break;
205 	case VX_PCI_VX222_NEW:
206 	default:
207 		if (mic[dev])
208 			hw = &vx222_mic_hw;
209 		else
210 			hw = &vx222_v2_hw;
211 		break;
212 	}
213 	err = snd_vx222_create(card, pci, hw, &vx);
214 	if (err < 0) {
215 		snd_card_free(card);
216 		return err;
217 	}
218 	card->private_data = vx;
219 	vx->core.ibl.size = ibl[dev];
220 
221 	sprintf(card->longname, "%s at 0x%lx & 0x%lx, irq %i",
222 		card->shortname, vx->port[0], vx->port[1], vx->core.irq);
223 	dev_dbg(card->dev, "%s at 0x%lx & 0x%lx, irq %i\n",
224 		    card->shortname, vx->port[0], vx->port[1], vx->core.irq);
225 
226 #ifdef SND_VX_FW_LOADER
227 	vx->core.dev = &pci->dev;
228 #endif
229 
230 	err = snd_vx_setup_firmware(&vx->core);
231 	if (err < 0) {
232 		snd_card_free(card);
233 		return err;
234 	}
235 
236 	err = snd_card_register(card);
237 	if (err < 0) {
238 		snd_card_free(card);
239 		return err;
240 	}
241 
242 	pci_set_drvdata(pci, card);
243 	dev++;
244 	return 0;
245 }
246 
247 static void snd_vx222_remove(struct pci_dev *pci)
248 {
249 	snd_card_free(pci_get_drvdata(pci));
250 }
251 
252 #ifdef CONFIG_PM_SLEEP
253 static int snd_vx222_suspend(struct device *dev)
254 {
255 	struct snd_card *card = dev_get_drvdata(dev);
256 	struct snd_vx222 *vx = card->private_data;
257 
258 	return snd_vx_suspend(&vx->core);
259 }
260 
261 static int snd_vx222_resume(struct device *dev)
262 {
263 	struct snd_card *card = dev_get_drvdata(dev);
264 	struct snd_vx222 *vx = card->private_data;
265 
266 	return snd_vx_resume(&vx->core);
267 }
268 
269 static SIMPLE_DEV_PM_OPS(snd_vx222_pm, snd_vx222_suspend, snd_vx222_resume);
270 #define SND_VX222_PM_OPS	&snd_vx222_pm
271 #else
272 #define SND_VX222_PM_OPS	NULL
273 #endif
274 
275 static struct pci_driver vx222_driver = {
276 	.name = KBUILD_MODNAME,
277 	.id_table = snd_vx222_ids,
278 	.probe = snd_vx222_probe,
279 	.remove = snd_vx222_remove,
280 	.driver = {
281 		.pm = SND_VX222_PM_OPS,
282 	},
283 };
284 
285 module_pci_driver(vx222_driver);
286