core.c 11.9 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
J
Johannes Berg 已提交
2 3 4
/*
 * i2sbus driver
 *
5
 * Copyright 2006-2008 Johannes Berg <johannes@sipsolutions.net>
J
Johannes Berg 已提交
6 7 8
 */

#include <linux/module.h>
9
#include <linux/slab.h>
J
Johannes Berg 已提交
10 11
#include <linux/pci.h>
#include <linux/interrupt.h>
12
#include <linux/dma-mapping.h>
13 14
#include <linux/of_address.h>
#include <linux/of_irq.h>
15

J
Johannes Berg 已提交
16
#include <sound/core.h>
17 18 19 20

#include <asm/macio.h>
#include <asm/dbdma.h>

J
Johannes Berg 已提交
21 22 23 24 25 26 27
#include "../soundbus.h"
#include "i2sbus.h"

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
MODULE_DESCRIPTION("Apple Soundbus: I2S support");

28 29 30 31 32
static int force;
module_param(force, int, 0444);
MODULE_PARM_DESC(force, "Force loading i2sbus even when"
			" no layout-id property is present");

33
static const struct of_device_id i2sbus_match[] = {
J
Johannes Berg 已提交
34 35 36 37
	{ .name = "i2s" },
	{ }
};

38 39
MODULE_DEVICE_TABLE(of, i2sbus_match);

J
Johannes Berg 已提交
40 41 42 43
static int alloc_dbdma_descriptor_ring(struct i2sbus_dev *i2sdev,
				       struct dbdma_command_mem *r,
				       int numcmds)
{
44 45
	/* one more for rounding, one for branch back, one for stop command */
	r->size = (numcmds + 3) * sizeof(struct dbdma_cmd);
J
Johannes Berg 已提交
46 47 48
	/* We use the PCI APIs for now until the generic one gets fixed
	 * enough or until we get some macio-specific versions
	 */
49 50
	r->space = dma_alloc_coherent(&macio_get_pci_dev(i2sdev->macio)->dev,
				      r->size, &r->bus_addr, GFP_KERNEL);
J
Joe Perches 已提交
51 52
	if (!r->space)
		return -ENOMEM;
J
Johannes Berg 已提交
53 54 55 56 57 58 59 60 61 62 63 64

	r->cmds = (void*)DBDMA_ALIGN(r->space);
	r->bus_cmd_start = r->bus_addr +
			   (dma_addr_t)((char*)r->cmds - (char*)r->space);

	return 0;
}

static void free_dbdma_descriptor_ring(struct i2sbus_dev *i2sdev,
				       struct dbdma_command_mem *r)
{
	if (!r->space) return;
J
Johannes Berg 已提交
65

J
Johannes Berg 已提交
66 67 68 69 70 71 72 73 74 75
	dma_free_coherent(&macio_get_pci_dev(i2sdev->macio)->dev,
			    r->size, r->space, r->bus_addr);
}

static void i2sbus_release_dev(struct device *dev)
{
	struct i2sbus_dev *i2sdev;
	int i;

	i2sdev = container_of(dev, struct i2sbus_dev, sound.ofdev.dev);
76 77 78
	iounmap(i2sdev->intfregs);
	iounmap(i2sdev->out.dbdma);
	iounmap(i2sdev->in.dbdma);
79
	for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++)
80
		release_and_free_resource(i2sdev->allocated_resource[i]);
J
Johannes Berg 已提交
81 82
	free_dbdma_descriptor_ring(i2sdev, &i2sdev->out.dbdma_ring);
	free_dbdma_descriptor_ring(i2sdev, &i2sdev->in.dbdma_ring);
83
	for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++)
J
Johannes Berg 已提交
84 85 86 87 88 89
		free_irq(i2sdev->interrupts[i], i2sdev);
	i2sbus_control_remove_dev(i2sdev->control, i2sdev);
	mutex_destroy(&i2sdev->lock);
	kfree(i2sdev);
}

90
static irqreturn_t i2sbus_bus_intr(int irq, void *devid)
J
Johannes Berg 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
{
	struct i2sbus_dev *dev = devid;
	u32 intreg;

	spin_lock(&dev->low_lock);
	intreg = in_le32(&dev->intfregs->intr_ctl);

	/* acknowledge interrupt reasons */
	out_le32(&dev->intfregs->intr_ctl, intreg);

	spin_unlock(&dev->low_lock);

	return IRQ_HANDLED;
}

106 107 108 109 110 111 112 113 114 115 116 117 118

/*
 * XXX FIXME: We test the layout_id's here to get the proper way of
 * mapping in various registers, thanks to bugs in Apple device-trees.
 * We could instead key off the machine model and the name of the i2s
 * node (i2s-a). This we'll do when we move it all to macio_asic.c
 * and have that export items for each sub-node too.
 */
static int i2sbus_get_and_fixup_rsrc(struct device_node *np, int index,
				     int layout, struct resource *res)
{
	struct device_node *parent;
	int pindex, rc = -ENXIO;
119
	const u32 *reg;
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

	/* Machines with layout 76 and 36 (K2 based) have a weird device
	 * tree what we need to special case.
	 * Normal machines just fetch the resource from the i2s-X node.
	 * Darwin further divides normal machines into old and new layouts
	 * with a subtely different code path but that doesn't seem necessary
	 * in practice, they just bloated it. In addition, even on our K2
	 * case the i2s-modem node, if we ever want to handle it, uses the
	 * normal layout
	 */
	if (layout != 76 && layout != 36)
		return of_address_to_resource(np, index, res);

	parent = of_get_parent(np);
	pindex = (index == aoa_resource_i2smmio) ? 0 : 1;
	rc = of_address_to_resource(parent, pindex, res);
	if (rc)
		goto bail;
138
	reg = of_get_property(np, "reg", NULL);
139 140 141 142 143 144 145 146 147 148 149
	if (reg == NULL) {
		rc = -ENXIO;
		goto bail;
	}
	res->start += reg[index * 2];
	res->end = res->start + reg[index * 2 + 1] - 1;
 bail:
	of_node_put(parent);
	return rc;
}

J
Johannes Berg 已提交
150 151 152 153 154 155
/* FIXME: look at device node refcounting */
static int i2sbus_add_dev(struct macio_dev *macio,
			  struct i2sbus_control *control,
			  struct device_node *np)
{
	struct i2sbus_dev *dev;
156
	struct device_node *child, *sound = NULL;
157
	struct resource *r;
158
	int i, layout = 0, rlen, ok = force;
159 160 161 162
	char node_name[6];
	static const char *rnames[] = { "i2sbus: %pOFn (control)",
					"i2sbus: %pOFn (tx)",
					"i2sbus: %pOFn (rx)" };
T
Takashi Iwai 已提交
163
	static const irq_handler_t ints[] = {
J
Johannes Berg 已提交
164 165 166 167 168
		i2sbus_bus_intr,
		i2sbus_tx_intr,
		i2sbus_rx_intr
	};

169
	if (snprintf(node_name, sizeof(node_name), "%pOFn", np) != 5)
J
Johannes Berg 已提交
170
		return 0;
171
	if (strncmp(node_name, "i2s-", 4))
J
Johannes Berg 已提交
172 173 174 175 176 177 178
		return 0;

	dev = kzalloc(sizeof(struct i2sbus_dev), GFP_KERNEL);
	if (!dev)
		return 0;

	i = 0;
179 180
	for_each_child_of_node(np, child) {
		if (of_node_name_eq(child, "sound")) {
J
Johannes Berg 已提交
181 182 183 184 185
			i++;
			sound = child;
		}
	}
	if (i == 1) {
186 187 188 189
		const u32 *id = of_get_property(sound, "layout-id", NULL);

		if (id) {
			layout = *id;
J
Johannes Berg 已提交
190
			snprintf(dev->sound.modalias, 32,
191
				 "sound-layout-%d", layout);
192
			ok = 1;
193 194 195 196 197 198
		} else {
			id = of_get_property(sound, "device-id", NULL);
			/*
			 * We probably cannot handle all device-id machines,
			 * so restrict to those we do handle for now.
			 */
199
			if (id && (*id == 22 || *id == 14 || *id == 35 ||
200
				   *id == 31 || *id == 44)) {
201 202 203 204 205
				snprintf(dev->sound.modalias, 32,
					 "aoa-device-id-%d", *id);
				ok = 1;
				layout = -1;
			}
J
Johannes Berg 已提交
206 207 208 209 210 211 212 213
		}
	}
	/* for the time being, until we can handle non-layout-id
	 * things in some fabric, refuse to attach if there is no
	 * layout-id property or we haven't been forced to attach.
	 * When there are two i2s busses and only one has a layout-id,
	 * then this depends on the order, but that isn't important
	 * either as the second one in that case is just a modem. */
214
	if (!ok) {
J
Johannes Berg 已提交
215 216 217 218 219 220
		kfree(dev);
		return -ENODEV;
	}

	mutex_init(&dev->lock);
	spin_lock_init(&dev->low_lock);
221
	dev->sound.ofdev.archdata.dma_mask = macio->ofdev.archdata.dma_mask;
222
	dev->sound.ofdev.dev.of_node = np;
223
	dev->sound.ofdev.dev.dma_mask = &dev->sound.ofdev.archdata.dma_mask;
J
Johannes Berg 已提交
224 225 226 227 228 229 230
	dev->sound.ofdev.dev.parent = &macio->ofdev.dev;
	dev->sound.ofdev.dev.release = i2sbus_release_dev;
	dev->sound.attach_codec = i2sbus_attach_codec;
	dev->sound.detach_codec = i2sbus_detach_codec;
	dev->sound.pcmid = -1;
	dev->macio = macio;
	dev->control = control;
231
	dev->bus_number = node_name[4] - 'a';
J
Johannes Berg 已提交
232 233
	INIT_LIST_HEAD(&dev->sound.codec_list);

234
	for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++) {
J
Johannes Berg 已提交
235
		dev->interrupts[i] = -1;
236
		snprintf(dev->rnames[i], sizeof(dev->rnames[i]),
237
			 rnames[i], np);
J
Johannes Berg 已提交
238
	}
239
	for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++) {
240 241
		int irq = irq_of_parse_and_map(np, i);
		if (request_irq(irq, ints[i], 0, dev->rnames[i], dev))
J
Johannes Berg 已提交
242
			goto err;
243
		dev->interrupts[i] = irq;
J
Johannes Berg 已提交
244 245
	}

246 247 248 249 250 251 252 253 254

	/* Resource handling is problematic as some device-trees contain
	 * useless crap (ugh ugh ugh). We work around that here by calling
	 * specific functions for calculating the appropriate resources.
	 *
	 * This will all be moved to macio_asic.c at one point
	 */
	for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++) {
		if (i2sbus_get_and_fixup_rsrc(np,i,layout,&dev->resources[i]))
J
Johannes Berg 已提交
255
			goto err;
256
		/* If only we could use our resource dev->resources[i]...
J
Johannes Berg 已提交
257
		 * but request_resource doesn't know about parents and
258 259
		 * contained resources...
		 */
J
Johannes Berg 已提交
260
		dev->allocated_resource[i] =
J
Johannes Berg 已提交
261
			request_mem_region(dev->resources[i].start,
262
					   resource_size(&dev->resources[i]),
J
Johannes Berg 已提交
263 264 265 266 267 268
					   dev->rnames[i]);
		if (!dev->allocated_resource[i]) {
			printk(KERN_ERR "i2sbus: failed to claim resource %d!\n", i);
			goto err;
		}
	}
269 270

	r = &dev->resources[aoa_resource_i2smmio];
271
	rlen = resource_size(r);
272 273 274 275 276
	if (rlen < sizeof(struct i2s_interface_regs))
		goto err;
	dev->intfregs = ioremap(r->start, rlen);

	r = &dev->resources[aoa_resource_txdbdma];
277
	rlen = resource_size(r);
278 279 280 281 282
	if (rlen < sizeof(struct dbdma_regs))
		goto err;
	dev->out.dbdma = ioremap(r->start, rlen);

	r = &dev->resources[aoa_resource_rxdbdma];
283
	rlen = resource_size(r);
284 285 286 287
	if (rlen < sizeof(struct dbdma_regs))
		goto err;
	dev->in.dbdma = ioremap(r->start, rlen);

J
Johannes Berg 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
	if (!dev->intfregs || !dev->out.dbdma || !dev->in.dbdma)
		goto err;

	if (alloc_dbdma_descriptor_ring(dev, &dev->out.dbdma_ring,
					MAX_DBDMA_COMMANDS))
		goto err;
	if (alloc_dbdma_descriptor_ring(dev, &dev->in.dbdma_ring,
					MAX_DBDMA_COMMANDS))
		goto err;

	if (i2sbus_control_add_dev(dev->control, dev)) {
		printk(KERN_ERR "i2sbus: control layer didn't like bus\n");
		goto err;
	}

	if (soundbus_add_one(&dev->sound)) {
		printk(KERN_DEBUG "i2sbus: device registration error!\n");
		goto err;
	}

	/* enable this cell */
	i2sbus_control_cell(dev->control, dev, 1);
	i2sbus_control_enable(dev->control, dev);
	i2sbus_control_clock(dev->control, dev, 1);

	return 1;
 err:
	for (i=0;i<3;i++)
		if (dev->interrupts[i] != -1)
			free_irq(dev->interrupts[i], dev);
	free_dbdma_descriptor_ring(dev, &dev->out.dbdma_ring);
	free_dbdma_descriptor_ring(dev, &dev->in.dbdma_ring);
320 321 322
	iounmap(dev->intfregs);
	iounmap(dev->out.dbdma);
	iounmap(dev->in.dbdma);
J
Johannes Berg 已提交
323
	for (i=0;i<3;i++)
324
		release_and_free_resource(dev->allocated_resource[i]);
J
Johannes Berg 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
	mutex_destroy(&dev->lock);
	kfree(dev);
	return 0;
}

static int i2sbus_probe(struct macio_dev* dev, const struct of_device_id *match)
{
	struct device_node *np = NULL;
	int got = 0, err;
	struct i2sbus_control *control = NULL;

	err = i2sbus_control_init(dev, &control);
	if (err)
		return err;
	if (!control) {
		printk(KERN_ERR "i2sbus_control_init API breakage\n");
		return -ENODEV;
	}

344
	while ((np = of_get_next_child(dev->ofdev.dev.of_node, np))) {
345 346
		if (of_device_is_compatible(np, "i2sbus") ||
		    of_device_is_compatible(np, "i2s-modem")) {
J
Johannes Berg 已提交
347 348 349 350 351 352 353 354 355 356
			got += i2sbus_add_dev(dev, control, np);
		}
	}

	if (!got) {
		/* found none, clean up */
		i2sbus_control_destroy(control);
		return -ENODEV;
	}

357
	dev_set_drvdata(&dev->ofdev.dev, control);
J
Johannes Berg 已提交
358 359 360 361 362 363

	return 0;
}

static int i2sbus_remove(struct macio_dev* dev)
{
364
	struct i2sbus_control *control = dev_get_drvdata(&dev->ofdev.dev);
J
Johannes Berg 已提交
365 366 367 368 369 370 371 372 373 374 375
	struct i2sbus_dev *i2sdev, *tmp;

	list_for_each_entry_safe(i2sdev, tmp, &control->list, item)
		soundbus_remove_one(&i2sdev->sound);

	return 0;
}

#ifdef CONFIG_PM
static int i2sbus_suspend(struct macio_dev* dev, pm_message_t state)
{
376
	struct i2sbus_control *control = dev_get_drvdata(&dev->ofdev.dev);
J
Johannes Berg 已提交
377 378 379 380 381 382 383 384 385 386 387 388 389
	struct codec_info_item *cii;
	struct i2sbus_dev* i2sdev;
	int err, ret = 0;

	list_for_each_entry(i2sdev, &control->list, item) {
		/* Notify codecs */
		list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
			err = 0;
			if (cii->codec->suspend)
				err = cii->codec->suspend(cii, state);
			if (err)
				ret = err;
		}
390 391 392

		/* wait until streams are stopped */
		i2sbus_wait_for_stop_both(i2sdev);
J
Johannes Berg 已提交
393
	}
394

J
Johannes Berg 已提交
395 396 397 398 399
	return ret;
}

static int i2sbus_resume(struct macio_dev* dev)
{
400
	struct i2sbus_control *control = dev_get_drvdata(&dev->ofdev.dev);
J
Johannes Berg 已提交
401 402 403 404 405
	struct codec_info_item *cii;
	struct i2sbus_dev* i2sdev;
	int err, ret = 0;

	list_for_each_entry(i2sdev, &control->list, item) {
406 407 408
		/* reset i2s bus format etc. */
		i2sbus_pcm_prepare_both(i2sdev);

J
Johannes Berg 已提交
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
		/* Notify codecs so they can re-initialize */
		list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
			err = 0;
			if (cii->codec->resume)
				err = cii->codec->resume(cii);
			if (err)
				ret = err;
		}
	}

	return ret;
}
#endif /* CONFIG_PM */

static int i2sbus_shutdown(struct macio_dev* dev)
{
	return 0;
}

static struct macio_driver i2sbus_drv = {
429 430 431 432 433
	.driver = {
		.name = "soundbus-i2s",
		.owner = THIS_MODULE,
		.of_match_table = i2sbus_match,
	},
J
Johannes Berg 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
	.probe = i2sbus_probe,
	.remove = i2sbus_remove,
#ifdef CONFIG_PM
	.suspend = i2sbus_suspend,
	.resume = i2sbus_resume,
#endif
	.shutdown = i2sbus_shutdown,
};

static int __init soundbus_i2sbus_init(void)
{
	return macio_register_driver(&i2sbus_drv);
}

static void __exit soundbus_i2sbus_exit(void)
{
	macio_unregister_driver(&i2sbus_drv);
}

module_init(soundbus_i2sbus_init);
module_exit(soundbus_i2sbus_exit);