aboutsummaryrefslogtreecommitdiff
path: root/drivers/misc/tfa9887l.c
blob: 8155eaed415a0672c773d113c8efb16f19fc95e5 (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
/* driver/i2c/chip/tfa9887.c
 *
 * NXP tfa9887 Speaker Amp
 *
 * Copyright (C) 2012 HTC Corporation
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * 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.
 *
 */

#include <linux/interrupt.h>
#include <linux/i2c.h>
#include <linux/slab.h>
#include <linux/irq.h>
#include <linux/miscdevice.h>
#include <asm/uaccess.h>
#include <linux/delay.h>
#include <linux/input.h>
#include <linux/workqueue.h>
#include <linux/freezer.h>
#include <linux/tfa9887.h>
#include <linux/mutex.h>
#include <linux/debugfs.h>
#include <linux/gpio.h>
#include <linux/module.h>
#include <linux/mfd/pm8xxx/pm8921.h>

#define TPA9887_IOCTL_MAGIC 'a'
#define TPA9887_WRITE_CONFIG	_IOW(TPA9887_IOCTL_MAGIC, 0x01, unsigned int)
#define TPA9887_READ_CONFIG	_IOW(TPA9887_IOCTL_MAGIC, 0x02, unsigned int)
#define TPA9887_ENABLE_DSP	_IOW(TPA9887_IOCTL_MAGIC, 0x03, unsigned int)
#define TPA9887_KERNEL_LOCK    _IOW(TPA9887_IOCTL_MAGIC, 0x06, unsigned int)
#define DEBUG (0)

static struct i2c_client *this_client;
static struct tfa9887_platform_data *pdata;
struct mutex spk_ampl_lock;
static int tfa9887l_opened;
static int last_spkampl_state;
static int dspl_enabled;
int tfa9887l_step = -1;
EXPORT_SYMBOL(tfa9887l_step);
int tfa9887l_step_en = 0;
EXPORT_SYMBOL(tfa9887l_step_en);

static int tfa9887_i2c_write(char *txData, int length);
static int tfa9887_i2c_read(char *rxData, int length);
#ifdef CONFIG_DEBUG_FS
static struct dentry *debugfs_tpa_dent;
static struct dentry *debugfs_peek;
static struct dentry *debugfs_poke;
static unsigned char read_data;

static int get_parameters(char *buf, long int *param1, int num_of_par)
{
	char *token;
	int base, cnt;

	token = strsep(&buf, " ");

	for (cnt = 0; cnt < num_of_par; cnt++) {
		if (token != NULL) {
			if ((token[1] == 'x') || (token[1] == 'X'))
				base = 16;
			else
				base = 10;

			if (strict_strtoul(token, base, &param1[cnt]) != 0)
				return -EINVAL;

			token = strsep(&buf, " ");
			}
		else
			return -EINVAL;
	}
	return 0;
}

static int codec_debug_open(struct inode *inode, struct file *file)
{
	file->private_data = inode->i_private;
	return 0;
}

static ssize_t codec_debug_read(struct file *file, char __user *ubuf,
				size_t count, loff_t *ppos)
{
	char lbuf[8];

	snprintf(lbuf, sizeof(lbuf), "0x%x\n", read_data);
	return simple_read_from_buffer(ubuf, count, ppos, lbuf, strlen(lbuf));
}

static ssize_t codec_debug_write(struct file *filp,
	const char __user *ubuf, size_t cnt, loff_t *ppos)
{
	char *access_str = filp->private_data;
	char lbuf[32];
	unsigned char reg_idx[2] = {0x00, 0x00};
	int rc;
	long int param[5];

	if (cnt > sizeof(lbuf) - 1)
		return -EINVAL;

	rc = copy_from_user(lbuf, ubuf, cnt);
	if (rc)
		return -EFAULT;

	lbuf[cnt] = '\0';

	if (!strcmp(access_str, "poke")) {
		
		rc = get_parameters(lbuf, param, 2);
		if ((param[0] <= 0xFF) && (param[1] <= 0xFF) &&
			(rc == 0)) {
			reg_idx[0] = param[0];
			reg_idx[1] = param[1];
			tfa9887_i2c_write(reg_idx, 2);
		} else
			rc = -EINVAL;
	} else if (!strcmp(access_str, "peek")) {
		
		rc = get_parameters(lbuf, param, 1);
		if ((param[0] <= 0xFF) && (rc == 0)) {
			reg_idx[0] = param[0];
			tfa9887_i2c_read(&read_data, 1);
		} else
			rc = -EINVAL;
	}

	if (rc == 0)
		rc = cnt;
	else
		pr_err("%s: rc = %d\n", __func__, rc);

	return rc;
}

static const struct file_operations codec_debug_ops = {
	.open = codec_debug_open,
	.write = codec_debug_write,
	.read = codec_debug_read
};
#endif

unsigned char cf_dspl_bypass[3][3] = {
	{0x04, 0x88, 0x0B},
	{0x09, 0x06, 0x19},
	{0x09, 0x06, 0x18}
};

unsigned char ampl_off[1][3] = {
	{0x09, 0x06, 0x19}
};

static int tfa9887_i2c_write(char *txData, int length)
{
	int rc;
	struct i2c_msg msg[] = {
		{
			.addr = this_client->addr,
			.flags = 0,
			.len = length,
			.buf = txData,
		},
	};
	
	if (tfa9887l_step_en)
		tfa9887l_step ++;
#if DEBUG
	pr_err("%s: tfa9887l_step %d\n", __func__, tfa9887l_step);
#endif

	rc = i2c_transfer(this_client->adapter, msg, 1);
	if (rc < 0) {
		pr_err("%s: transfer error %d\n", __func__, rc);
		return rc;
	}

	
	if (tfa9887l_step_en)
		tfa9887l_step ++;

#if DEBUG
	pr_err("%s: tfa9887l_step %d\n", __func__, tfa9887l_step);
	{
		int i = 0;
		for (i = 0; i < length; i++)
			pr_info("%s: tx[%d] = %2x\n", \
				__func__, i, txData[i]);
	}
#endif

	return 0;
}

static int tfa9887_i2c_read(char *rxData, int length)
{
	int rc;
	struct i2c_msg msgs[] = {
		{
		 .addr = this_client->addr,
		 .flags = I2C_M_RD,
		 .len = length,
		 .buf = rxData,
		},
	};

	
	if (tfa9887l_step_en)
		tfa9887l_step ++;
#if DEBUG
	pr_err("%s: tfa9887l_step %d\n", __func__, tfa9887l_step);
#endif

	rc = i2c_transfer(this_client->adapter, msgs, 1);
	if (rc < 0) {
		pr_err("%s: transfer error %d\n", __func__, rc);
		return rc;
	}

	
	if (tfa9887l_step_en)
		tfa9887l_step ++;

#if DEBUG
	pr_err("%s: tfa9887l_step %d\n", __func__, tfa9887l_step);
	{
		int i = 0;
		for (i = 0; i < length; i++)
			pr_info("i2c_read %s: rx[%d] = %2x\n", __func__, i, \
				rxData[i]);
	}
#endif

	return 0;
}

static int tfa9887l_open(struct inode *inode, struct file *file)
{
	int rc = 0;

	if (tfa9887l_opened) {
		pr_info("%s: busy\n", __func__);
	}
	tfa9887l_opened = 1;

	return rc;
}

static int tfa9887l_release(struct inode *inode, struct file *file)
{
	tfa9887l_opened = 0;

	return 0;
}


int tfa9887_l_write(char *txData, int length) {
	return tfa9887_i2c_write(txData, length);
}

int tfa9887_l_read(char *rxData, int length) {
    return tfa9887_i2c_read(rxData, length);
}

void set_tfa9887l_spkamp(int en, int dsp_mode)
{
	int i =0;
        
        
        unsigned char mute_reg[1] = {0x06};
	unsigned char mute_data[3] = {0, 0, 0};
        unsigned char power_reg[1] = {0x09};
	unsigned char power_data[3] = {0, 0, 0};
	unsigned char SPK_CR[3] = {0x8, 0x8, 0};

	pr_info("%s: en = %d dsp_enabled = %d\n", __func__, en, dspl_enabled);
	mutex_lock(&spk_ampl_lock);
	
	tfa9887l_step = 0;
	tfa9887l_step_en = 1;
	if (en && !last_spkampl_state) {
		last_spkampl_state = 1;
		
		if (dspl_enabled == 0) {
			for (i=0; i <3 ; i++)
				tfa9887_i2c_write(cf_dspl_bypass[i], 3);
                
				tfa9887_i2c_write(SPK_CR,1);
				tfa9887_i2c_read(SPK_CR+1,2);
				SPK_CR[1] |= 0x4; 
				tfa9887_i2c_write(SPK_CR,3);
		} else {
			
			
			tfa9887_i2c_write(power_reg, 1);
			tfa9887_i2c_read(power_data + 1, 2);
			tfa9887_i2c_write(mute_reg, 1);
			tfa9887_i2c_read(mute_data + 1, 2);
			mute_data[0] = 0x6;
			mute_data[2] &= 0xdf;  
			power_data[0] = 0x9;
			power_data[2] &= 0xfe; 
			
			tfa9887_i2c_write(power_data, 3);
			
			tfa9887_i2c_write(mute_data, 3);
			power_data[2] |= 0x8;  
			tfa9887_i2c_write(power_data, 3);
		}
	} else if (!en && last_spkampl_state) {
		last_spkampl_state = 0;
		if (dspl_enabled == 0) {
			tfa9887_i2c_write(ampl_off[0], 3);
		} else {
			
			
			tfa9887_i2c_write(power_reg, 1);
			tfa9887_i2c_read(power_data + 1, 2);
			tfa9887_i2c_write(mute_reg, 1);
			tfa9887_i2c_read(mute_data + 1, 2);
			mute_data[0] = 0x6;
			mute_data[2] |= 0x20; 
			
			tfa9887_i2c_write(mute_data, 3);
			tfa9887_i2c_write(power_reg, 1);
			tfa9887_i2c_read(power_data + 1, 2);
			power_data[0] = 0x9;
			power_data[2] &= 0xf7;  
			tfa9887_i2c_write(power_data, 3);
			
			
			power_data[2] |= 0x1;  
			tfa9887_i2c_write(power_data, 3);
		}
	}
	
	tfa9887l_step_en = 0;
	tfa9887l_step = -1;

	mutex_unlock(&spk_ampl_lock);
}

static long tfa9887l_ioctl(struct file *file, unsigned int cmd,
	   unsigned long arg)
{
	int rc = 0;
	unsigned int reg_value[2];
	unsigned int len = 0;
	char *addr;
	void __user *argp = (void __user *)arg;

	switch (cmd) {
	case TPA9887_WRITE_CONFIG:
		pr_debug("%s: TPA9887_WRITE_CONFIG\n", __func__);
		rc = copy_from_user(reg_value, argp, sizeof(reg_value));
		if (rc) {
			pr_err("%s: copy from user failed.\n", __func__);
			goto err;
		}

		len = reg_value[0];
		addr = (char *)reg_value[1];

		
		tfa9887l_step = 0;
		tfa9887l_step_en = 2;
		tfa9887_i2c_write(addr+1, len -1);
		tfa9887l_step_en = 0;
		tfa9887l_step = -1;

		break;
	case TPA9887_READ_CONFIG:
		pr_debug("%s: TPA9887_READ_CONFIG\n", __func__);
		rc = copy_from_user(reg_value, argp, sizeof(reg_value));;
		if (rc) {
			pr_err("%s: copy from user failed.\n", __func__);
			goto err;
		}

		len = reg_value[0];
		addr = (char *)reg_value[1];

		
		tfa9887l_step = 0;
		tfa9887l_step_en = 2;
		tfa9887_i2c_read(addr, len);
		tfa9887l_step_en = 0;
		tfa9887l_step = -1;

		rc = copy_to_user(argp, reg_value, sizeof(reg_value));
		if (rc) {
			pr_err("%s: copy to user failed.\n", __func__);
			goto err;
		}
		break;
	case TPA9887_ENABLE_DSP:
		rc = copy_from_user(reg_value, argp, sizeof(reg_value));;
		if (rc) {
			pr_err("%s: copy from user failed.\n", __func__);
			goto err;
		}

		len = reg_value[0];
		dspl_enabled = reg_value[1];
		pr_info("%s: TPA9887_ENABLE_DSP(%d)\n", __func__, dspl_enabled);
		break;
	case TPA9887_KERNEL_LOCK:
		rc = copy_from_user(reg_value, argp, sizeof(reg_value));;
		if (rc) {
		   pr_err("%s: copy from user failed.\n", __func__);
		   goto err;
		}

		len = reg_value[0];
		
		if (reg_value[1]) {
		   mutex_lock(&spk_ampl_lock);
		   pr_info("TPA9887_KLOCK2 ++\n");
		} else {
		   pr_info("TPA9887_KLOCK2 --\n");
		   mutex_unlock(&spk_ampl_lock);
		}
		break;
	}
err:
	return rc;
}

static struct file_operations tfa9887l_fops = {
	.owner = THIS_MODULE,
	.open = tfa9887l_open,
	.release = tfa9887l_release,
	.unlocked_ioctl = tfa9887l_ioctl,
};

static struct miscdevice tfa9887l_device = {
	.minor = MISC_DYNAMIC_MINOR,
	.name = "tfa9887l",
	.fops = &tfa9887l_fops,
};

int tfa9887l_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
	int ret = 0;

	pdata = client->dev.platform_data;

	if (pdata == NULL) {
		pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
		if (pdata == NULL) {
			ret = -ENOMEM;
			pr_err("%s: platform data is NULL\n", __func__);
			goto err_alloc_data_failed;
		}
	}

	this_client = client;

	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
		pr_err("%s: i2c check functionality error\n", __func__);
		ret = -ENODEV;
		goto err_free_gpio_all;
	}

	ret = misc_register(&tfa9887l_device);
	if (ret) {
		pr_err("%s: tfa9887l_device register failed\n", __func__);
		goto err_free_gpio_all;
	}
#ifdef CONFIG_DEBUG_FS
	debugfs_tpa_dent = debugfs_create_dir("tfa9887", 0);
	if (!IS_ERR(debugfs_tpa_dent)) {
		debugfs_peek = debugfs_create_file("peek",
		S_IFREG | S_IRUGO, debugfs_tpa_dent,
		(void *) "peek", &codec_debug_ops);

		debugfs_poke = debugfs_create_file("poke",
		S_IFREG | S_IRUGO, debugfs_tpa_dent,
		(void *) "poke", &codec_debug_ops);

	}
#endif
	return 0;

err_free_gpio_all:
	return ret;
err_alloc_data_failed:
	return ret;
}

static int tfa9887l_remove(struct i2c_client *client)
{
	struct tfa9887_platform_data *p9887data = i2c_get_clientdata(client);
	kfree(p9887data);

	return 0;
}

static int tfa9887l_suspend(struct i2c_client *client, pm_message_t mesg)
{
	return 0;
}

static int tfa9887l_resume(struct i2c_client *client)
{
	return 0;
}

static const struct i2c_device_id tfa9887l_id[] = {
	{ TFA9887L_I2C_NAME, 0 },
	{ }
};

static struct i2c_driver tfa9887l_driver = {
	.probe = tfa9887l_probe,
	.remove = tfa9887l_remove,
	.suspend = tfa9887l_suspend,
	.resume = tfa9887l_resume,
	.id_table = tfa9887l_id,
	.driver = {
		.name = TFA9887L_I2C_NAME,
	},
};

static int __init tfa9887l_init(void)
{
	pr_info("%s\n", __func__);
	mutex_init(&spk_ampl_lock);
        dspl_enabled = 0;
	return i2c_add_driver(&tfa9887l_driver);
}

static void __exit tfa9887l_exit(void)
{
#ifdef CONFIG_DEBUG_FS
	debugfs_remove(debugfs_peek);
	debugfs_remove(debugfs_poke);
	debugfs_remove(debugfs_tpa_dent);
#endif
	i2c_del_driver(&tfa9887l_driver);
}

module_init(tfa9887l_init);
module_exit(tfa9887l_exit);

MODULE_DESCRIPTION("tfa9887 L Speaker Amp driver");
MODULE_LICENSE("GPL");