summaryrefslogtreecommitdiff
path: root/drivers/mfd/tlv320aic3xxx-core.c
blob: 5d17d9dd4f74cdadef09eec48141a679099d12ed (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
/*
 * tlv320aic3xxx-core.c  -- driver for TLV320AIC3XXX
 *
 * Author:      Mukund Navada <navada@ti.com>
 *              Mehar Bajwa <mehar.bajwa@ti.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * 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., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/err.h>
#include <linux/delay.h>
#include <linux/regmap.h>
#include <linux/mfd/core.h>
#include <linux/pm_runtime.h>
#include <linux/regulator/consumer.h>
#include <linux/regulator/machine.h>
#include <linux/gpio.h>

#include <linux/mfd/tlv320aic3xxx-core.h>
#include <linux/mfd/tlv320aic3262-registers.h>

struct aic3262_gpio {
	unsigned int reg;
	u8 mask;
	u8 shift;
};
struct aic3262_gpio aic3262_gpio_control[] = {
	{
	 .reg = AIC3262_GPIO1_IO_CNTL,
	 .mask = AIC3262_GPIO_D6_D2,
	 .shift = AIC3262_GPIO_D2_SHIFT,
	 },
	{
	 .reg = AIC3262_GPIO2_IO_CNTL,
	 .mask = AIC3262_GPIO_D6_D2,
	 .shift = AIC3262_GPIO_D2_SHIFT,
	 },
	{
	 .reg = AIC3262_GPI1_EN,
	 .mask = AIC3262_GPI1_D2_D1,
	 .shift = AIC3262_GPIO_D1_SHIFT,
	 },
	{
	 .reg = AIC3262_GPI2_EN,
	 .mask = AIC3262_GPI2_D5_D4,
	 .shift = AIC3262_GPIO_D4_SHIFT,
	 },
	{
	 .reg = AIC3262_GPO1_OUT_CNTL,
	 .mask = AIC3262_GPO1_D4_D1,
	 .shift = AIC3262_GPIO_D1_SHIFT,
	 },
};

/*Codec read count limit once*/
#define CODEC_BULK_READ_MAX 128
/*Ap read conut limit once*/
#define CODEC_BULK_READ_LIMIT 63

#define CHECK_AIC3xxx_I2C_SHUTDOWN(a) { if (a && a->shutdown_complete) { \
dev_err(a->dev, "error: i2c state is 'shutdown'\n"); return -ENODEV; } }

int set_aic3xxx_book(struct aic3xxx *aic3xxx, int book)
{
	int ret = 0;
	u8 page_buf[] = { 0x0, 0x0 };
	u8 book_buf[] = { 0x7f, 0x0 };

	ret = regmap_write(aic3xxx->regmap, page_buf[0], page_buf[1]);

	if (ret < 0)
		return ret;
	book_buf[1] = book;
	ret = regmap_write(aic3xxx->regmap, book_buf[0], book_buf[1]);

	if (ret < 0)
		return ret;
	aic3xxx->book_no = book;
	aic3xxx->page_no = 0;

	return ret;
}

int set_aic3xxx_page(struct aic3xxx *aic3xxx, int page)
{
	int ret = 0;
	u8 page_buf[] = { 0x0, 0x0 };

	page_buf[1] = page;
	ret = regmap_write(aic3xxx->regmap, page_buf[0], page_buf[1]);

	if (ret < 0)
		return ret;
	aic3xxx->page_no = page;
	return ret;
}
/**
 * aic3xxx_reg_read: Read a single TLV320AIC3262 register.
 *
 * @aic3xxx: Device to read from.
 * @reg: Register to read.
 */
int aic3xxx_reg_read(struct aic3xxx *aic3xxx, unsigned int reg)
{
	unsigned int val;
	int ret;
	union aic3xxx_reg_union *aic_reg = (union aic3xxx_reg_union *) &reg;
	u8 book, page, offset;

	page = aic_reg->aic3xxx_register.page;
	book = aic_reg->aic3xxx_register.book;
	offset = aic_reg->aic3xxx_register.offset;

	mutex_lock(&aic3xxx->io_lock);
	CHECK_AIC3xxx_I2C_SHUTDOWN(aic3xxx)
	if (aic3xxx->book_no != book) {
		ret = set_aic3xxx_book(aic3xxx, book);
		if (ret < 0) {
			mutex_unlock(&aic3xxx->io_lock);
			return ret;
		}
	}

	if (aic3xxx->page_no != page) {
		ret = set_aic3xxx_page(aic3xxx, page);
		if (ret < 0) {
			mutex_unlock(&aic3xxx->io_lock);
			return ret;
		}
	}
	ret = regmap_read(aic3xxx->regmap, offset, &val);
	mutex_unlock(&aic3xxx->io_lock);

	if (ret < 0)
		return ret;
	else
		return val;
}
EXPORT_SYMBOL_GPL(aic3xxx_reg_read);

/**
 * aic3xxx_bulk_read: Read multiple TLV320AIC3262 registers
 *
 * @aic3xxx: Device to read from
 * @reg: First register
 * @count: Number of registers
 * @buf: Buffer to fill.  The data will be returned big endian.
 */
int aic3xxx_bulk_read(struct aic3xxx *aic3xxx, unsigned int reg,
		      int count, u8 *buf)
{
	int ret = 0;
	int count_temp = count;
	union aic3xxx_reg_union *aic_reg = (union aic3xxx_reg_union *) &reg;
	u8 book, page, offset;

	if (count > CODEC_BULK_READ_MAX)
		return -1;

	page = aic_reg->aic3xxx_register.page;
	book = aic_reg->aic3xxx_register.book;
	offset = aic_reg->aic3xxx_register.offset;

	mutex_lock(&aic3xxx->io_lock);
	CHECK_AIC3xxx_I2C_SHUTDOWN(aic3xxx)
	if (aic3xxx->book_no != book) {
		ret = set_aic3xxx_book(aic3xxx, book);
		if (ret < 0) {
			mutex_unlock(&aic3xxx->io_lock);
			return ret;
		}
	}

	if (aic3xxx->page_no != page) {
		ret = set_aic3xxx_page(aic3xxx, page);
		if (ret < 0) {
			mutex_unlock(&aic3xxx->io_lock);
			return ret;
		}
	}

	while (count_temp) {
		if (count_temp > CODEC_BULK_READ_LIMIT) {
			ret = regmap_bulk_read(aic3xxx->regmap, offset,
			buf, CODEC_BULK_READ_LIMIT);
			offset += CODEC_BULK_READ_LIMIT;
			buf += CODEC_BULK_READ_LIMIT;
			count_temp -= CODEC_BULK_READ_LIMIT;
		} else {
			ret = regmap_bulk_read(aic3xxx->regmap, offset,
			buf, count_temp);
			offset += count_temp;
			buf += count_temp;
			count_temp -= count_temp;
		}
	}

	mutex_unlock(&aic3xxx->io_lock);
	return ret;
}
EXPORT_SYMBOL_GPL(aic3xxx_bulk_read);

/**
 * aic3xxx_reg_write: Write a single TLV320AIC3262 register.
 *
 * @aic3xxx: Device to write to.
 * @reg: Register to write to.
 * @val: Value to write.
 */
int aic3xxx_reg_write(struct aic3xxx *aic3xxx, unsigned int reg,
		      unsigned char val)
{
	union aic3xxx_reg_union *aic_reg = (union aic3xxx_reg_union *) &reg;
	int ret = 0;
	u8 page, book, offset;

	page = aic_reg->aic3xxx_register.page;
	book = aic_reg->aic3xxx_register.book;
	offset = aic_reg->aic3xxx_register.offset;

	mutex_lock(&aic3xxx->io_lock);
	CHECK_AIC3xxx_I2C_SHUTDOWN(aic3xxx)
	if (book != aic3xxx->book_no) {
		ret = set_aic3xxx_book(aic3xxx, book);
		if (ret < 0) {
			mutex_unlock(&aic3xxx->io_lock);
			return ret;
		}
	}
	if (page != aic3xxx->page_no) {
		ret = set_aic3xxx_page(aic3xxx, page);
		if (ret < 0) {
			mutex_unlock(&aic3xxx->io_lock);
			return ret;
		}
	}
	ret = regmap_write(aic3xxx->regmap, offset, val);
	mutex_unlock(&aic3xxx->io_lock);
	return ret;

}
EXPORT_SYMBOL_GPL(aic3xxx_reg_write);

/**
 * aic3xxx_bulk_write: Write multiple TLV320AIC3262 registers
 *
 * @aic3xxx: Device to write to
 * @reg: First register
 * @count: Number of registers
 * @buf: Buffer to write from.  Data must be big-endian formatted.
 */
int aic3xxx_bulk_write(struct aic3xxx *aic3xxx, unsigned int reg,
		       int count, const u8 *buf)
{
	union aic3xxx_reg_union *aic_reg = (union aic3xxx_reg_union *) &reg;
	int ret = 0;
	u8 page, book, offset;

	page = aic_reg->aic3xxx_register.page;
	book = aic_reg->aic3xxx_register.book;
	offset = aic_reg->aic3xxx_register.offset;

	mutex_lock(&aic3xxx->io_lock);
	CHECK_AIC3xxx_I2C_SHUTDOWN(aic3xxx)
	if (book != aic3xxx->book_no) {
		ret = set_aic3xxx_book(aic3xxx, book);
		if (ret < 0) {
			mutex_unlock(&aic3xxx->io_lock);
			return ret;
		}
	}
	if (page != aic3xxx->page_no) {
		ret = set_aic3xxx_page(aic3xxx, page);
		if (ret < 0) {
			mutex_unlock(&aic3xxx->io_lock);
			return ret;
		}
	}
	ret = regmap_raw_write(aic3xxx->regmap, offset, buf, count);
	mutex_unlock(&aic3xxx->io_lock);
	return ret;
}
EXPORT_SYMBOL_GPL(aic3xxx_bulk_write);

/**
 * aic3xxx_set_bits: Set the value of a bitfield in a TLV320AIC3262 register
 *
 * @aic3xxx: Device to write to.
 * @reg: Register to write to.
 * @mask: Mask of bits to set.
 * @val: Value to set (unshifted)
 */
int aic3xxx_set_bits(struct aic3xxx *aic3xxx, unsigned int reg,
		     unsigned char mask, unsigned char val)
{
	union aic3xxx_reg_union *aic_reg = (union aic3xxx_reg_union *) &reg;
	int ret = 0;
	u8 page, book, offset;

	page = aic_reg->aic3xxx_register.page;
	book = aic_reg->aic3xxx_register.book;
	offset = aic_reg->aic3xxx_register.offset;

	mutex_lock(&aic3xxx->io_lock);
	CHECK_AIC3xxx_I2C_SHUTDOWN(aic3xxx)
	if (book != aic3xxx->book_no) {
		ret = set_aic3xxx_book(aic3xxx, book);
		if (ret < 0) {
			mutex_unlock(&aic3xxx->io_lock);
			return ret;
		}
	}
	if (page != aic3xxx->page_no) {
		ret = set_aic3xxx_page(aic3xxx, page);
		if (ret < 0) {
			mutex_unlock(&aic3xxx->io_lock);
			return ret;
		}
	}
	ret = regmap_update_bits(aic3xxx->regmap, offset, mask, val);
	mutex_unlock(&aic3xxx->io_lock);
	return ret;

}
EXPORT_SYMBOL_GPL(aic3xxx_set_bits);

/**
 * aic3xxx_wait_bits: wait for a value of a bitfield in a TLV320AIC3262 register
 *
 * @aic3xxx: Device to write to.
 * @reg: Register to write to.
 * @mask: Mask of bits to set.
 * @val: Value to set (unshifted)
 * @mdelay: mdelay value in each iteration in milliseconds
 * @count: iteration count for timeout
 */
int aic3xxx_wait_bits(struct aic3xxx *aic3xxx, unsigned int reg,
		      unsigned char mask, unsigned char val, int sleep,
		      int counter)
{
	int status;
	int timeout = sleep * counter;

	status = aic3xxx_reg_read(aic3xxx, reg);
	while (((status & mask) != val) && counter) {
		mdelay(sleep);
		status = aic3xxx_reg_read(aic3xxx, reg);
		counter--;
	};
	if (!counter)
		dev_err(aic3xxx->dev,
			"wait_bits timedout (%d millisecs). lastval 0x%x val 0x%x\n",
			timeout, status, val);
	return counter;
}
EXPORT_SYMBOL_GPL(aic3xxx_wait_bits);

static struct mfd_cell aic3262_devs[] = {
	{
	 .name = "tlv320aic3262-codec",
	},
	{
	.name = "tlv320aic3262-gpio",
	},
};

static struct mfd_cell aic3285_devs[] = {
	{ .name = "tlv320aic3285-codec" },
	{ .name = "tlv320aic3285-extcon" },
	{ .name = "tlv320aic3285-gpio" },
};

/**
 * Instantiate the generic non-control parts of the device.
 */
int aic3xxx_device_init(struct aic3xxx *aic3xxx, int irq)
{
	struct aic3xxx_pdata *pdata = aic3xxx->dev->platform_data;
	const char *devname;
	int ret, i;
	u8 resetVal = 1;

	dev_info(aic3xxx->dev, "aic3xxx_device_init beginning\n");

	mutex_init(&aic3xxx->io_lock);
	dev_set_drvdata(aic3xxx->dev, aic3xxx);

	if (!pdata)
		return -EINVAL;

	/*GPIO reset for TLV320AIC3262 codec */
	if (pdata->gpio_reset) {
		ret = gpio_request(pdata->gpio_reset, "aic3xxx-reset-pin");
		if (ret != 0) {
			dev_err(aic3xxx->dev, "not able to acquire gpio\n");
			goto err_return;
		}
		gpio_direction_output(pdata->gpio_reset, 1);
		mdelay(5);
		gpio_direction_output(pdata->gpio_reset, 0);
		mdelay(5);
		gpio_direction_output(pdata->gpio_reset, 1);
		mdelay(5);
	}

	/* run the codec through software reset */
	ret = aic3xxx_reg_write(aic3xxx, AIC3262_RESET_REG, resetVal);
	if (ret < 0) {
		dev_err(aic3xxx->dev, "Could not write to AIC3262 register\n");
		goto err_return;
	}

	mdelay(10);

	ret = aic3xxx_reg_read(aic3xxx, AIC3262_DEVICE_ID);
	if (ret < 0) {
		dev_err(aic3xxx->dev, "Failed to read ID register\n");
		goto err_return;
	}

	switch (ret) {
	case 3:
		devname = "TLV320AIC3262";
		if (aic3xxx->type != TLV320AIC3262)
			dev_warn(aic3xxx->dev, "Device registered as type %d\n",
				 aic3xxx->type);
		aic3xxx->type = TLV320AIC3262;
		break;
	case 4:
		devname = "TLV320AIC3285";
		if (aic3xxx->type != TLV320AIC3285)
			dev_warn(aic3xxx->dev, "Device registered as type %d\n",
				 aic3xxx->type);
		aic3xxx->type = TLV320AIC3285;
	default:
		dev_err(aic3xxx->dev, "Device is not a TLV320AIC3262 type=%d",
			ret);
		ret = -EINVAL;
		goto err_return;
	}

	dev_info(aic3xxx->dev, "%s revision %c\n", devname, 'D' + ret);

	/*If naudint is gpio convert it to irq number */
	if (pdata->gpio_irq == 1) {
		aic3xxx->irq = gpio_to_irq(pdata->naudint_irq);
		gpio_request(pdata->naudint_irq, "aic3xxx-gpio-irq");
		gpio_direction_input(pdata->naudint_irq);
	} else
		aic3xxx->irq = pdata->naudint_irq;

	aic3xxx->irq_base = pdata->irq_base;
	for (i = 0; i < AIC3262_NUM_GPIO; i++) {
		if (pdata->gpio[i].used) {
			if (pdata->gpio[i].in) {
				aic3xxx_set_bits(aic3xxx,
						 aic3262_gpio_control[i].reg,
						 aic3262_gpio_control[i].mask,
						 0x1 << aic3262_gpio_control[i].
						 shift);
				if (pdata->gpio[i].in_reg) {
					aic3xxx_set_bits(aic3xxx,
							 pdata->gpio[i].in_reg,
							 pdata->gpio[i].
							 in_reg_bitmask,
							 pdata->gpio[i].
							 value << pdata->
							 gpio[i].in_reg_shift);
				}
			} else {
				aic3xxx_set_bits(aic3xxx,
						 aic3262_gpio_control[i].reg,
						 aic3262_gpio_control[i].mask,
						 pdata->gpio[i].
						 value <<
						 aic3262_gpio_control[i].shift);
			}
		} else
			aic3xxx_set_bits(aic3xxx, aic3262_gpio_control[i].reg,
					 aic3262_gpio_control[i].mask, 0x0);
	}
	aic3xxx->suspended = true;

	/* codec interrupt */
	if (aic3xxx->irq) {
		ret = aic3xxx_irq_init(aic3xxx);
		if (ret < 0)
			goto err_irq;
	}
	switch (aic3xxx->type) {
	case TLV320AIC3262:
		ret = mfd_add_devices(aic3xxx->dev, -1,
			      aic3262_devs, ARRAY_SIZE(aic3262_devs), NULL, 0,
			      NULL);
		break;

	case TLV320AIC3285:
		ret = mfd_add_devices(aic3xxx->dev, -1,
			      aic3285_devs, ARRAY_SIZE(aic3285_devs), NULL, 0,
			      NULL);
		break;
	case TLV320AIC3266:
		break;

	default:
		dev_err(aic3xxx->dev, "unable to recognize codec\n");
	}
	if (ret != 0) {
		dev_err(aic3xxx->dev, "Failed to add children: %d\n", ret);
		goto err_mfd;
	}
	dev_info(aic3xxx->dev, "aic3xxx_device_init added mfd devices\n");

	return 0;

err_mfd:

	aic3xxx_irq_exit(aic3xxx);
err_irq:

	if (pdata && pdata->gpio_irq)
		gpio_free(pdata->naudint_irq);
err_return:

	if (pdata && pdata->gpio_reset)
		gpio_free(pdata->gpio_reset);

	return ret;
}
EXPORT_SYMBOL_GPL(aic3xxx_device_init);

void aic3xxx_device_exit(struct aic3xxx *aic3xxx)
{
	struct aic3xxx_pdata *pdata = aic3xxx->dev->platform_data;

	pm_runtime_disable(aic3xxx->dev);
	mfd_remove_devices(aic3xxx->dev);
	aic3xxx_irq_exit(aic3xxx);

	if (pdata && pdata->gpio_irq)
		gpio_free(pdata->naudint_irq);
	if (pdata && pdata->gpio_reset)
		gpio_free(pdata->gpio_reset);

}
EXPORT_SYMBOL_GPL(aic3xxx_device_exit);

MODULE_AUTHOR("Mukund Navada <navada@ti.comm>");
MODULE_AUTHOR("Mehar Bajwa <mehar.bajwa@ti.com>");
MODULE_DESCRIPTION("Core support for the TLV320AIC3XXX audio CODEC");
MODULE_LICENSE("GPL");