summaryrefslogtreecommitdiff
path: root/v4l_experimental/dpl3518.c
blob: a9ee6bd52801b2d6794d0a36c2753394ff82f228 (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
/*
 * $Id: dpl3518.c,v 1.5 2005/07/13 17:25:25 mchehab Exp $
 *
 * programming the dpl3518a Dolby Pro Logic Processor
 *
 * WARNING: THIS DRIVER WILL LOAD WITHOUT COMPLAINTS EVEN IF A DIFFERENT
 * CHIP IS AT ADDRESS 0x84 (it relies on i2c to make sure that there is a
 * device acknowledging that address)
 *
 *  Copyright (C) 1999 Roland Jansen <roland@lut.rwth-aachen.de>
 *
 * 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 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
 * USA.
 *
 * ---
 * DOLBY and PRO LOGIC are trademarks of
 * Dolby Laboratories Licensing Corporation.
 */

/* FIXME */
#define DPL_MAJOR  127

#include "compat.h"
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/string.h>
#include <linux/timer.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/fs.h>
#include <asm/uaccess.h>

#include <media/id.h>
#include "dpl3518.h"


/* Addresses to scan */
#define I2C_DPL3518       0x84
static unsigned short normal_i2c[] = {
	I2C_DPL3518 >> 1,
	I2C_CLIENT_END};

I2C_CLIENT_INSMOD;

struct dpl3518 {
	struct i2c_client *client;
	int mode;		/* dolby mode */
};

static struct dpl3518 *dpl_device;
static struct i2c_driver driver;
static struct i2c_client client_template;

/* ---------------------------------------------------------------------- */

/* insmod parameters */
static int debug = 0;		/* debug output */
MODULE_PARM(debug, "i");

MODULE_DESCRIPTION("device driver for dpl3518a Dolby Pro Logic Processor");
MODULE_AUTHOR("Roland Jansen");
MODULE_LICENSE("GPL");

#define dprintk     if (debug) printk

/* ---------------------------------------------------------------------- */

#define I2C_DPL3518_MR    0x10	/* write address MODE_REG */
#define I2C_DPL3518_DFP   0x12	/* write address DFP */

/* ----------------------------------------------------------------------- */
/* functions for talking to the dpl3518 Sound processor                   */

static int dpl3518_reset(struct i2c_client *client)
{
	static char reset_off[3] = { 0x00, 0x80, 0x00 };
	static char reset_on[3]  = { 0x00, 0x00, 0x00 };

	i2c_master_send(client,reset_off,3);  /* ignore errors here */
	if (3 != i2c_master_send(client,reset_on, 3)) {
		printk(KERN_ERR "dpl3518: chip reset failed, penguin on i2c bus?\n");
		return -1;
	}
	return 0;
}

static int dpl3518_write(struct i2c_client *client, int dev, int addr, int val)
{
	int err;
	unsigned char buffer[5];

	buffer[0] = dev;
	buffer[1] = addr >> 8;
	buffer[2] = addr &  0xff;
	buffer[3] = val  >> 8;
	buffer[4] = val  &  0xff;

	for (err = 0; err < 3;) {
		if (5 == i2c_master_send(client, buffer, 5))
			break;
		err++;
		printk(KERN_WARNING "dpl3518: I/O error #%d (write 0x%02x/0x%02x)\n",
		       err, dev, addr);
		set_current_state(TASK_INTERRUPTIBLE);
		schedule_timeout(HZ/10);
	}
	if (3 == err) {
		printk(KERN_WARNING "dpl3518: giving up, reseting chip. Sound will go off, sorry folks :-|\n");
		dpl3518_reset(client);
		return -1;
	}
	return 0;
}

/* ----------------------------------------------------------------------- */

/* in: 0..100 out: 0x00..0x7f */
static int p100_to_7f(int i)
{
	int r;

	r = ((i & 0x7f) * 127 + 1) / 100;
	if (r > 127)
		r = 127;
	if (r < 0)
		r = 0;
	return r;
}

/* ----------------------------------------------------------------------- */

static void dpl3518_ioc_init(struct i2c_client *client)
{
	dprintk("dpl3518 init\n");
	dpl3518_write(client, I2C_DPL3518_MR, 0x0083, 0x0008); /* mode_reg */
	dpl3518_write(client, I2C_DPL3518_DFP, 0x000b, 0x0320); /*I2S1 src: RL */
	dpl3518_write(client, I2C_DPL3518_DFP, 0x0012, 0x0000); /*prescale2: off */
	dpl3518_write(client, I2C_DPL3518_DFP, 0x0016, 0x1000); /*prescale: 0 dB */
	dpl3518_write(client, I2C_DPL3518_DFP, 0x0038, 0x0420); /*I2S2 src: CS */
	dpl3518_write(client, I2C_DPL3518_DFP, 0x0040, 0x0000); /*adaptive,surround */
	dpl3518_write(client, I2C_DPL3518_DFP, 0x0041, 0x0520); /*i2s1, stereo */
	dpl3518_write(client, I2C_DPL3518_DFP, 0x0042, 0x1400); /*delay: 20ms */
	dpl3518_write(client, I2C_DPL3518_DFP, 0x0043, 0x0000); /*inp. Bal. auto */
	dpl3518_write(client, I2C_DPL3518_DFP, 0x0044, 0x4000); /*spatial 50% */
	dpl3518_write(client, I2C_DPL3518_DFP, 0x0045, 0x0000); /*panorama off */
	//dpl3518_write(client,I2C_DPL3518_DFP,0x0045,0x5400); /*panorama 66%*/
	dpl3518_write(client, I2C_DPL3518_DFP, 0x0046, 0x0000); /*reverb off */
}

static void dpl3518_prologic_init(struct i2c_client *client)
{
	dprintk("dpl3518 prologic_init\n");
	dpl3518_write(client, I2C_DPL3518_DFP, 0x000b, 0x0320); /*I2S1 src: RL */
	dpl3518_write(client, I2C_DPL3518_DFP, 0x0038, 0x0420); /*I2S2 src: CS */
	dpl3518_write(client, I2C_DPL3518_DFP, 0x0041, 0x0520); /*i2s1, stereo */
}

static void dpl3518_noise_init(struct i2c_client *client)
{
	dprintk("dpl3518 noise_init\n");
	dpl3518_write(client, I2C_DPL3518_DFP, 0x000b, 0x0320); /*I2S1 src: RL */
	dpl3518_write(client, I2C_DPL3518_DFP, 0x0038, 0x0420); /*I2S2 src: CS */
	dpl3518_write(client, I2C_DPL3518_DFP, 0x0041, 0x0100); /*NOISE */
}

static void dpl3518_through_init(struct i2c_client *client)
{
	dprintk("dpl3518 through_init\n");
	dpl3518_write(client, I2C_DPL3518_DFP, 0x000b, 0x0520); /*I2S1 src: S1 */
	dpl3518_write(client, I2C_DPL3518_DFP, 0x0038, 0x0220); /*I2S2 src: scart*/
	dpl3518_write(client, I2C_DPL3518_DFP, 0x0041, 0x0520); /*i2s1, stereo */
}

static void dpl3518_setmode(struct i2c_client *client, int mode)
{
	dprintk("dpl3518 setmode: %i\n", mode);
	dpl3518_write(client, I2C_DPL3518_DFP, 0x0040, mode << 4);
	dpl_device->mode = mode;
}

static void dpl3518_setnoisemode(struct i2c_client *client, int mode)
{
	static int noise_modes[5] = { 0x0000, 0x01a0, 0x01b0, 0x01c0, 0x01d0 };
	dprintk("dpl3518 setnoisemode: %i\n", mode);
	dpl3518_write(client, I2C_DPL3518_DFP, 0x0041, noise_modes[mode]);
}


static void dpl3518_delay(struct i2c_client *client, int delay)
{
	dprintk("dpl3518 setdelay: %i\n", delay);
	dpl3518_write(client, I2C_DPL3518_DFP, 0x0042, delay << 8);
}

static void dpl3518_reverb(struct i2c_client *client, int rev)
{
	dprintk("dpl3518 setreverb: %i\n", rev);
	dpl3518_write(client, I2C_DPL3518_DFP, 0x0046, p100_to_7f(rev) << 8);
}

static void dpl3518_panorama(struct i2c_client *client, int pan)
{
	dprintk("dpl3518 panorama: %i\n", pan);
	dpl3518_write(client, I2C_DPL3518_DFP, 0x0045, p100_to_7f(pan) << 8);
}

static void dpl3518_spatial(struct i2c_client *client, int spatial)
{
	dprintk("dpl3518 setspatial: %i\n", spatial);
	dpl3518_write(client, I2C_DPL3518_DFP, 0x0045, p100_to_7f(spatial) << 8);
}

/* ----------------------------------------------------------------------- */

static int dpl3518_attach(struct i2c_adapter *adap, int addr, int kind)
{
	struct dpl3518 *dpl;
	struct i2c_client *client;

	client = kmalloc(sizeof *client,GFP_KERNEL);
	if (!client)
		return -ENOMEM;
	memcpy(client,&client_template,sizeof(struct i2c_client));
	client->adapter = adap;
	client->addr = addr;

	dpl = kmalloc(sizeof *dpl, GFP_KERNEL);
	if (NULL == dpl)
		return -ENOMEM;
	i2c_set_clientdata(client, dpl);
	memset(dpl, 0, sizeof(struct dpl3518));
	dpl->mode = 0;		/* DOLBY_MODE_NONE */
	dpl->client = client;

	if (-1 == dpl3518_reset(client)) {
		kfree(dpl);
		kfree(client);
		dprintk("dpl3518: no chip found\n");
		return -1;
	}

	printk(KERN_INFO "dpl3518: init\n");
	dpl_device = dpl;
	dpl3518_ioc_init(client);

	/* done */
	i2c_attach_client(client);
	return 0;
}

static int dpl3518_probe(struct i2c_adapter *adap)
{
	return i2c_probe(adap, &addr_data, dpl3518_attach);
}

static int dpl3518_detach(struct i2c_client *client)
{
	struct dpl3518 *dpl = i2c_get_clientdata(client);

	dpl3518_reset(client);
	i2c_detach_client(client);

	dpl_device = NULL;
	kfree(dpl);
	kfree(client);
	return 0;
}

static int dpl3518_dev_ioctl(struct inode *inode, struct file *file,
			     unsigned int cmd, unsigned long arg)
{
//        unsigned int minor = MINOR(inode->i_rdev);
	int ret = 0;
	struct i2c_client *client = dpl_device->client;

	switch (cmd) {
	case DPL_IOC_RESET:
		dpl3518_reset(client);
		break;
	case DPL_IOC_VERSION:
		if (put_user(DPL_VERSION_CODE, (int *) arg))
			ret = -EFAULT;
		break;
	case DPL_IOC_INIT:
		dpl3518_ioc_init(client);
		break;
	case DPL_IOC_PROLOGIC_INIT:
		dpl3518_prologic_init(client);
		break;
	case DPL_IOC_NOISE_INIT:
		dpl3518_noise_init(client);
		break;
	case DPL_IOC_THROUGH_INIT:
		dpl3518_through_init(client);
		break;
	case DPL_IOC_MODE:
		dpl3518_setmode(client, (int) arg);
		break;
	case DPL_IOC_GET_MODE:
		if (put_user(dpl_device->mode, (int *) arg))
			ret = -EFAULT;
		break;
	case DPL_IOC_NOISE_MODE:
		dpl3518_setnoisemode(client, (int) arg);
		break;
	case DPL_IOC_DELAY:
		dpl3518_delay(client, (int) arg);
		break;
	case DPL_IOC_REVERB:
		dpl3518_reverb(client, (int) arg);
		break;
	case DPL_IOC_PANORAMA:
		dpl3518_panorama(client, (int) arg);
		break;
	case DPL_IOC_SPATIAL:
		dpl3518_spatial(client, (int) arg);
		break;
	default:
		dprintk("dpl3518_ioctl: default %i\n", (int) arg);
		ret = -EINVAL;
	}
	return ret;
}

static int dpl3518_dev_open(struct inode *inode, struct file *file)
{
	dprintk("dpl3518_dev_open\n");
	return 0;
}

static int dpl3518_dev_release(struct inode *inode, struct file *file)
{
	dprintk("dpl3518_dev_release\n");
	return 0;
}

/* ---------------------------------------------------------------------- */

static struct i2c_driver driver = {
	.owner          = THIS_MODULE,
	.name           = "i2c dpl3518 driver",
	.id             = I2C_DRIVERID_DPL3518,
	.flags          = I2C_DF_NOTIFY,
	.attach_adapter = dpl3518_probe,
	.detach_client  = dpl3518_detach,
};

static struct i2c_client client_template =
{
	I2C_DEVNAME("dpl3518"),
	.id         = -1,
	.driver     = &driver
};

static struct file_operations dpl3518_fops =
{
	.owner          = THIS_MODULE,
	.ioctl		= dpl3518_dev_ioctl,
	.open		= dpl3518_dev_open,
	.release	= dpl3518_dev_release,
};


static int dpl3518_init(void)
{
	if (register_chrdev(DPL_MAJOR, "dpl3518", &dpl3518_fops)) {
		printk("dpl3518: unable to get major %d\n", DPL_MAJOR);
		return -EBUSY;
	}
	i2c_add_driver(&driver);
	return 0;
}

static void dpl3518_fini(void)
{
	i2c_del_driver(&driver);
	unregister_chrdev(DPL_MAJOR, "dpl3518");
}

module_init(dpl3518_init);
module_exit(dpl3518_fini);

/*
 * Local variables:
 * c-basic-offset: 8
 * End:
 */