summaryrefslogtreecommitdiff
path: root/drivers/misc/i2s/i2s.c
blob: b4c243b7cb2b2c96cd684c7fe93544cbe0004c4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/*----------------------------------------------------------------------------*/
/*  copyright STMicroelectronics, 2007.                                       */
/*                                                                            */
/* This program is free software; you can redistribute it and/or modify it    */
/* under the terms of the GNU General Public License as published by the Free */
/* Software Foundation; either version 2.1 of the License, or (at your option)*/
/* any later version.                                                         */
/*                                                                            */
/* This program is distributed in the hope that it will be useful, but        */
/* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY */
/* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License    */
/* for more details.                                                          */
/*                                                                            */
/* You should have received a copy of the GNU General Public License          */
/* along with this program. If not, see <http://www.gnu.org/licenses/>.       */
/*----------------------------------------------------------------------------*/

#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/init.h>
#include <linux/cache.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/idr.h>
#include <linux/i2s/i2s.h>
#include <linux/platform_device.h>

/*******************************************************************************/
static DEFINE_MUTEX(core_lock);

static void i2sdev_release(struct device *dev)
{
	struct i2s_device *i2s = to_i2s_device(dev);

	if (i2s->controller)
		put_device(&(i2s->controller->dev));
	kfree(dev);
}
static ssize_t
modalias_show(struct device *dev, struct device_attribute *a, char *buf)
{
	const struct i2s_device *i2s = to_i2s_device(dev);
	return sprintf(buf, "%s\n", i2s->modalias);
}

static struct device_attribute i2s_dev_attrs[] = {
	__ATTR_RO(modalias),
	__ATTR_NULL,
};

/* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
 * and the sysfs version makes coldplug work too.
 */
static const struct i2s_device_id *i2s_match_id(const struct i2s_device_id *id,
						const struct i2s_device *device)
{
	while (id->name[0]) {
		if (strcmp(device->modalias, id->name) == 0)
			return id;
		id++;
	}
	return NULL;
}

static int i2s_match_device(struct device *dev, struct device_driver *drv)
{
	const struct i2s_device *device = to_i2s_device(dev);
	struct i2s_driver *driver = to_i2s_driver(drv);
	if (driver->id_table)
		return i2s_match_id(driver->id_table, device) != NULL;
	return 0;
}

static int i2s_uevent(struct device *dev, struct kobj_uevent_env *env)
{
	const struct i2s_device *i2s = to_i2s_device(dev);

	add_uevent_var(env, "MODALIAS=%s", i2s->modalias);
	return 0;
}

#ifdef	CONFIG_PM
static int i2s_suspend(struct device *dev, pm_message_t message)
{
	int value = 0;
	struct i2s_driver *drv = to_i2s_driver(dev->driver);

	/* suspend will stop irqs and dma; no more i/o */
	if (drv) {
		if (drv->suspend)
			value = drv->suspend(to_i2s_device(dev), message);
		else
			dev_dbg(dev, "... can't suspend\n");
	}
	return value;
}

static int i2s_resume(struct device *dev)
{
	int value = 0;
	struct i2s_driver *drv = to_i2s_driver(dev->driver);

	/* resume may restart the i/o queue */
	if (drv) {
		if (drv->resume)
			value = drv->resume(to_i2s_device(dev));
		else
			dev_dbg(dev, "... can't resume\n");
	}
	return value;
}

#else
#define i2s_suspend	NULL
#define i2s_resume	NULL
#endif

/*This bus is designed to handle various protocols supported by the MSP- ARM Primecell IP
 * such as
 * I2s, PCM, AC97, TDM .... (refer to the data sheet for the complete list.
 * Current MSP driver has the above ones coded.
 * */
struct bus_type i2s_bus_type = {
	.name = "i2s",
	.dev_attrs = i2s_dev_attrs,
	.match = i2s_match_device,
	.uevent = i2s_uevent,
	.suspend = i2s_suspend,
	.resume = i2s_resume,
};

EXPORT_SYMBOL_GPL(i2s_bus_type);

static int i2s_drv_probe(struct device *dev)
{
	const struct i2s_driver *sdrv = to_i2s_driver(dev->driver);

	return sdrv->probe(to_i2s_device(dev));
}

static int i2s_drv_remove(struct device *dev)
{
	const struct i2s_driver *sdrv = to_i2s_driver(dev->driver);

	return sdrv->remove(to_i2s_device(dev));
}

static void i2s_drv_shutdown(struct device *dev)
{
	const struct i2s_driver *sdrv = to_i2s_driver(dev->driver);

	sdrv->shutdown(to_i2s_device(dev));
}

/**
 * i2s_register_driver - register a I2S driver
 * @sdrv: the driver to register
 * Context: can sleep
 */
int i2s_register_driver(struct i2s_driver *sdrv)
{
	sdrv->driver.bus = &i2s_bus_type;
	if (sdrv->probe)
		sdrv->driver.probe = i2s_drv_probe;
	if (sdrv->remove)
		sdrv->driver.remove = i2s_drv_remove;
	if (sdrv->shutdown)
		sdrv->driver.shutdown = i2s_drv_shutdown;
	return driver_register(&sdrv->driver);
}

EXPORT_SYMBOL_GPL(i2s_register_driver);

/******************************************************************************/
struct board_i2s_combined_info {
	struct i2s_board_info board_info;
	struct i2s_device *i2s_dev_p;
};
struct boardinfo {
	struct list_head list;
	unsigned n_board_info;
	struct board_i2s_combined_info board_i2s_info[0];
};

static LIST_HEAD(board_list);
static DEFINE_MUTEX(board_lock);

/*
 * Get an i2s device. Used in MSP LTP tests.
 */
struct i2s_device *i2s_get_device_from_boardinfo(int chip_select)
{
	struct boardinfo *bi;
	struct i2s_device *i2s_dev_p = NULL;

	mutex_lock(&board_lock);
	list_for_each_entry(bi, &board_list, list) {
		struct board_i2s_combined_info *chip = bi->board_i2s_info;
		unsigned n;

		for (n = bi->n_board_info; n > 0; n--, chip++)
			if (chip->board_info.chip_select == chip_select) {
				i2s_dev_p = chip->i2s_dev_p;
				break;
			}
		if (i2s_dev_p != NULL)
			break;
	}
	mutex_unlock(&board_lock);

	return i2s_dev_p;
}

EXPORT_SYMBOL_GPL(i2s_get_device_from_boardinfo);

/* I2S devices should normally not be created by I2S device drivers; that
 * would make them board-specific.  Similarly with I2S master drivers.
 * Device registration normally goes into like arch/.../mach.../board-YYY.c
 * with other readonly (flashable) information about mainboard devices.
 */
struct i2s_device *i2s_alloc_device(struct device *device)
{
	struct i2s_device *i2s;
	struct device *dev = device->parent;

	get_device(device);
	i2s = kzalloc(sizeof *i2s, GFP_KERNEL);
	if (!i2s) {
		dev_err(dev, "cannot alloc i2s_device\n");
		return NULL;
	}

	i2s->dev.parent = dev;
	i2s->dev.bus = &i2s_bus_type;
	i2s->dev.release = i2sdev_release;
	device_initialize(&i2s->dev);
	return i2s;
}

EXPORT_SYMBOL_GPL(i2s_alloc_device);

/**
 * i2s_add_device - Add i2s_device allocated with i2s_alloc_device
 * @i2s: i2s_device to register
 *
 * Companion function to i2s_alloc_device.  Devices allocated with
 * i2s_alloc_device can be added onto the i2s bus with this function.
 *
 * Returns 0 on success; negative errno on failure
 */
int i2s_add_device(struct i2s_device *i2s)
{
	static DEFINE_MUTEX(i2s_add_lock);
	struct device *dev = i2s->dev.parent;
	int status;

	dev_set_name(&i2s->dev, "%s.%u", "i2s", i2s->chip_select);

	mutex_lock(&i2s_add_lock);

	if (bus_find_device_by_name(&i2s_bus_type, NULL, dev_name(&i2s->dev))
	    != NULL) {
		dev_err(dev, "chipselect %d already in use\n",
			i2s->chip_select);
		status = -EBUSY;
		goto done;
	}

	/* Device may be bound to an active driver when this returns */
	status = device_add(&i2s->dev);
	if (status < 0)
		dev_err(dev, "can't %s %s, status %d\n",
			"add", dev_name(&i2s->dev), status);
	else
		dev_dbg(dev, "registered child %s\n", dev_name(&i2s->dev));

      done:
	mutex_unlock(&i2s_add_lock);
	return status;
}

EXPORT_SYMBOL_GPL(i2s_add_device);

/**
 * i2s_new_device - instantiate one new I2S device
 * @i2s_cont: Controller to which device is connected
 * @chip: Describes the I2S device
 * Context: can sleep
 *
 * On typical mainboards, this is purely internal; and it's not needed
 * after board init creates the hard-wired devices.  Some development
 * platforms may not be able to use i2s_register_board_info though, and
 * this is exported so that driver could add devices (which it would
 * learn about out-of-band).
 *
 * Returns the new device, or NULL.
 */
struct i2s_device *i2s_new_device(struct i2s_controller *i2s_cont,
				  struct i2s_board_info *chip)
{
	struct i2s_device *proxy;
	int status;

	/* NOTE:  caller did any chip->bus_num checks necessary.
	 *
	 * Also, unless we change the return value convention to use
	 * error-or-pointer (not NULL-or-pointer), troubleshootability
	 * suggests syslogged diagnostics are best here (ugh).
	 */

	proxy = i2s_alloc_device(&i2s_cont->dev);
	if (!proxy)
		return NULL;

	WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));

	proxy->chip_select = chip->chip_select;
	strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
	proxy->dev.platform_data = (void *)chip->platform_data;
	proxy->controller = i2s_cont;

	status = i2s_add_device(proxy);
	if (status < 0) {
		kfree(proxy);
		return NULL;
	}

	return proxy;
}

EXPORT_SYMBOL_GPL(i2s_new_device);

/**
 * i2s_register_board_info - register I2S devices for a given board
 * @info: array of chip descriptors
 * @n: how many descriptors are provided
 * Context: can sleep
 *
 * Board-specific early init code calls this (probably during arch_initcall)
 * with segments of the I2S device table.  Any device nodes are created later,
 * after the relevant parent I2S controller (id) is defined.  We keep
 * this table of devices forever, so that reloading a controller driver will
 * not make Linux forget about these hard-wired devices.
 *
 */
int __init
i2s_register_board_info(struct i2s_board_info const *info, unsigned n)
{
	int i;
	struct boardinfo *bi;

	bi = kmalloc(sizeof(*bi) + (n * sizeof(struct board_i2s_combined_info)), GFP_KERNEL);
	if (!bi)
		return -ENOMEM;
	bi->n_board_info = n;

	for (i = 0; i < n; i++)
		memcpy(&bi->board_i2s_info[i].board_info, &info[i], sizeof *info);

	mutex_lock(&board_lock);
	list_add_tail(&bi->list, &board_list);
	mutex_unlock(&board_lock);
	return 0;
}

/**
 * scan_boardinfo - Scan, creates and registered new i2s device structure.
 * @i2s_cont: i2s controller structure
 * Context: process
 *
 * It will scan the device list that may be registered statically using
 * register_board_info func in arch specific directory and call
 * i2s_new_device to create and registered i2s device over i2s bus. It is
 * called by i2s_add_controller function.
 *
 * Returns void.
 */
static void scan_boardinfo(struct i2s_controller *i2s_cont)
{
	struct boardinfo *bi;

	mutex_lock(&board_lock);
	list_for_each_entry(bi, &board_list, list) {
		struct board_i2s_combined_info *chip = bi->board_i2s_info;
		unsigned n;

		for (n = bi->n_board_info; n > 0; n--, chip++) {
			if (chip->board_info.chip_select != i2s_cont->id)
				continue;
			/* NOTE: this relies on i2s_new_device to
			 * issue diagnostics when given bogus inputs
			 */
			chip->i2s_dev_p = i2s_new_device(i2s_cont, &chip->board_info);
		}
	}
	mutex_unlock(&board_lock);
}

/******************************************************************************/
/**I2S Controller inittialization*/
static void i2s_controller_dev_release(struct device *dev)
{
	struct i2s_controller *i2s_cont;
	i2s_cont = container_of(dev, struct i2s_controller, dev);
	kfree(i2s_cont);
}

static ssize_t
show_controller_name(struct device *dev, struct device_attribute *attr,
		     char *buf)
{
	struct i2s_controller *cont = to_i2s_controller(dev);
	return sprintf(buf, "%s\n", cont->name);
}

static struct device_attribute i2s_controller_attrs[] = {
	__ATTR(name, S_IRUGO, show_controller_name, NULL),
	{},
};

static struct class i2s_controller_class = {
	.owner = THIS_MODULE,
	.name = "i2s-controller",
	.dev_attrs = i2s_controller_attrs,
};

static int i2s_register_controller(struct i2s_controller *cont)
{
	int res = 0;
	mutex_init(&cont->bus_lock);

	mutex_lock(&core_lock);

	/* Add the controller to the driver core.
	 * If the parent pointer is not set up,
	 * we add this controller to the host bus.
	 */
	if (cont->dev.parent == NULL) {
		cont->dev.parent = &platform_bus;
		pr_debug("I2S controller driver [%s] forgot to specify "
			 "physical device\n", cont->name);
	}
	dev_set_name(&cont->dev, "I2Scrlr-%d", cont->id);
	cont->dev.release = &i2s_controller_dev_release;
	cont->dev.class = &i2s_controller_class;
	res = device_register(&cont->dev);
	if (res)
		goto out_unlock;

	dev_dbg(&cont->dev, "controller [%s] registered\n", cont->name);
	scan_boardinfo(cont);
      out_unlock:
	mutex_unlock(&core_lock);
	return res;
}

/**
 * i2s_add_controller - declare i2s controller, use dynamic bus number
 * @controller: the controller to add
 * Context: can sleep
 *
 */
int i2s_add_controller(struct i2s_controller *controller)
{
	return i2s_register_controller(controller);
}

EXPORT_SYMBOL(i2s_add_controller);

static int __unregister(struct device *dev, void *controller_dev)
{
	/* note: before about 2.6.14-rc1 this would corrupt memory: */
	if (dev != controller_dev)
		i2s_unregister_device(to_i2s_device(dev));
	return 0;
}

/**
 * i2s_del_controller - unregister I2S controller
 * @cont: the controller being unregistered
 * Context: can sleep
 *
 * This unregisters an I2S controller which was previously registered
 * by @i2s_add_controller.
 */
int i2s_del_controller(struct i2s_controller *cont)
{
	int res = 0;
	int dummy;
	mutex_lock(&core_lock);

	dummy = device_for_each_child(cont->dev.parent, &cont->dev,
				      __unregister);
	device_unregister(&cont->dev);
	mutex_unlock(&core_lock);
	return res;
}

EXPORT_SYMBOL(i2s_del_controller);

/******************************************************************************/
/*I2S interface apis*/

/**
 * i2s_transfer - Main i2s transfer function.
 * @i2s_cont: i2s controller structure passed by client driver.
 * @message: i2s message structure contains transceive info.
 * Context: process or interrupt.
 *
 * This API is called by client i2s driver as i2s_xfer funtion. It will handle
 * main i2s transfer over i2s bus. The controller should registered its own
 * functions using i2s algorithm structure.
 *
 * Returns error(-1) in case of failure or success(0).
 */
int i2s_transfer(struct i2s_controller *i2s_cont, struct i2s_message *message)
{
	return i2s_cont->algo->cont_transfer(i2s_cont, message);

}

EXPORT_SYMBOL(i2s_transfer);

/**
 * i2s_cleanup - Close the current i2s connection btw controller and client.
 * @i2s_cont: i2s controller structure
 * @flag: It indicates the functionality that needs to be disabled.
 * Context: process
 *
 * This API will disable and reset the controller's configuration. Reset the
 * controller so that i2s client driver can reconfigure with new configuration.
 * Controller should release all the necessary resources which was acquired
 * during setup.
 *
 * Returns error(-1) in case of failure or success(0).
 */
int i2s_cleanup(struct i2s_controller *i2s_cont, i2s_flag flag)
{
	int status = 0;
	status = i2s_cont->algo->cont_cleanup(i2s_cont, flag);
	if (status)
		return -1;
	else
		return 0;
}

EXPORT_SYMBOL(i2s_cleanup);

/**
 * i2s_setup - configures and enables the I2S controller.
 * @i2s_cont: i2s controller sent by i2s device.
 * @config: specifies the configuration parameters.
 *
 * This function configures the I2S controller with the client configuration.
 * Controller was already registered on I2S bus by some master controller
 * driver.
 *
 * Returns error(-1) in case of failure else success(0)
 */
int i2s_setup(struct i2s_controller *i2s_cont, void *config)
{
	return i2s_cont->algo->cont_setup(i2s_cont, config);
}

EXPORT_SYMBOL(i2s_setup);

/**
 * i2s_hw_status - Get the current hw status for the i2s controller.
 * @i2s_cont: i2s controller structure passed by client driver.
 * Context: process or interrupt.
 *
 * This API is called by client i2s driver to find out current hw status.
 * The controller should registered its own functions using i2s algorithm structure.
 *
 * Returns current hw status register.
 */
int i2s_hw_status(struct i2s_controller *i2s_cont)
{
	return i2s_cont->algo->cont_hw_status(i2s_cont);
}

/**
 * i2s_get_pointer - Get the current dma_addr_t for the i2s controller.
 * @i2s_cont: i2s controller structure passed by client driver.
 * @i2s_direction: Specifies TX or RX direction.
 * Context: process or interrupt.
 *
 * This API is called by client i2s driver to return a dma_addr_t corresponding
 * to the position of the DMA-controller.
 * The controller should registered its own functions using i2s algorithm structure.
 *
 * Returns current hw status register.
 */
dma_addr_t i2s_get_pointer(struct i2s_controller *i2s_cont,
			enum i2s_direction_t i2s_direction)
{
	return i2s_cont->algo->cont_get_pointer(i2s_cont, i2s_direction);
}

/******************************************************************************/

static int __init i2s_init(void)
{
	int status;

	status = bus_register(&i2s_bus_type);
	if (status < 0)
		goto err0;

	status = class_register(&i2s_controller_class);
	if (status < 0)
		goto err1;
	return 0;

      err1:
	bus_unregister(&i2s_bus_type);
      err0:
	return status;
}

static void __exit i2s_exit(void)
{
	class_unregister(&i2s_controller_class);
	bus_unregister(&i2s_bus_type);
}

subsys_initcall(i2s_init);
module_exit(i2s_exit);

MODULE_AUTHOR("Sandeep Kaushik, <sandeep-mmc.kaushik@st.com>");
MODULE_LICENSE("GPL");