summaryrefslogtreecommitdiff
path: root/drivers/mfd/tlv320aic3256-core.c
blob: 50dbfc85e756fddb9dcf39094434a1c84f85588a (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
/*
 * tlv320aic325x-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/tlv320aic325x-core.h>
#include <linux/mfd/tlv320aic325x-registers.h>
#include <linux/mfd/tlv320aic3256-registers.h>


/**
 * set_aic325x_book: change book which we have to write/read to.
 *
 * @aic325x: Device to write/read to.
 * @book: Book to write/read to.
 */
int set_aic325x_book(struct aic325x *aic325x, int book)
{
	int ret = 0;
	u8 page_buf[] = { 0x0, 0x0 };
	u8 book_buf[] = { 0x7f, 0x0 };

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

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

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

	return ret;
}

/**
 * set_aic325x_page: change page which we have to write/read to.
 *
 * @aic325x: Device to write/read to.
 * @page: Book to write/read to.
 */
int set_aic325x_page(struct aic325x *aic325x, int page)
{
	int ret = 0;
	u8 page_buf[] = { 0x0, 0x0 };

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

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

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

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

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

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

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

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

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

	if (aic325x->page_no != page) {
		ret = set_aic325x_page(aic325x, page);
		if (ret < 0) {
			mutex_unlock(&aic325x->io_lock);
			return ret;
		}
	}
	ret = regmap_bulk_read(aic325x->regmap, offset, buf, count);
	mutex_unlock(&aic325x->io_lock);
		return ret;
}
EXPORT_SYMBOL_GPL(aic325x_bulk_read);

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

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

	mutex_lock(&aic325x->io_lock);
	if (book != aic325x->book_no) {
		ret = set_aic325x_book(aic325x, book);
		if (ret < 0) {
			mutex_unlock(&aic325x->io_lock);
			return ret;
		}
	}
	if (page != aic325x->page_no) {
		ret = set_aic325x_page(aic325x, page);
		if (ret < 0) {
			mutex_unlock(&aic325x->io_lock);
			return ret;
		}
	}
	ret = regmap_write(aic325x->regmap, offset, val);
	mutex_unlock(&aic325x->io_lock);
	return ret;

}
EXPORT_SYMBOL_GPL(aic325x_reg_write);

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

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

	mutex_lock(&aic325x->io_lock);
	if (book != aic325x->book_no) {
		ret = set_aic325x_book(aic325x, book);
		if (ret < 0) {
			mutex_unlock(&aic325x->io_lock);
			return ret;
		}
	}
	if (page != aic325x->page_no) {
		ret = set_aic325x_page(aic325x, page);
		if (ret < 0) {
			mutex_unlock(&aic325x->io_lock);
			return ret;
		}
	}
	ret = regmap_raw_write(aic325x->regmap, offset, buf, count);
	mutex_unlock(&aic325x->io_lock);
	return ret;
}
EXPORT_SYMBOL_GPL(aic325x_bulk_write);

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

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

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

}
EXPORT_SYMBOL_GPL(aic325x_set_bits);

/**
 * aic325x_wait_bits: wait for a value of a bitfield in a TLV320AIC3XXX register
 *
 * @aic325x: Device to write to.
 * @reg: Register to write to.
 * @mask: Mask of bits to set.
 * @val: Value to set (unshifted)
 * @sleep: delay value in each iteration in micro seconds
 * @count: iteration count for timeout
 */
int aic325x_wait_bits(struct aic325x *aic325x, unsigned int reg,
		      unsigned char mask, unsigned char val, int sleep,
		      int counter)
{
	int status;
	int timeout = sleep * counter;

	status = aic325x_reg_read(aic325x, reg);
	while (((status & mask) != val) && counter) {
		usleep_range(sleep, sleep + 500);
		status = aic325x_reg_read(aic325x, reg);
		counter--;
	};
	if (!counter)
		dev_err(aic325x->dev,
			"wait_bits timedout (%d millisecs). lastval 0x%x\n",
			timeout, status);
	return counter;
}
EXPORT_SYMBOL_GPL(aic325x_wait_bits);

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

/**
 * Instantiate the generic non-control parts of the device.
 */
int aic325x_device_init(struct aic325x *aic325x)
{
	const char *devname;
	int ret, i;
	u8 reset = 1;

	dev_info(aic325x->dev, "aic325x_device_init beginning\n");
	mutex_init(&aic325x->io_lock);
	dev_set_drvdata(aic325x->dev, aic325x);

	if (dev_get_platdata(aic325x->dev))
		memcpy(&aic325x->pdata, dev_get_platdata(aic325x->dev),
			sizeof(aic325x->pdata));

	/* GPIO reset for TLV320AIC3xxx codec */
	if (aic325x->pdata.gpio_reset) {
		ret = gpio_request_one(aic325x->pdata.gpio_reset,
					GPIOF_DIR_OUT | GPIOF_INIT_LOW,
					"aic325x-reset-pin");
		if (ret != 0) {
			dev_err(aic325x->dev, "not able to acquire gpio\n");
			goto err_return;
		}
	}

	/* run the codec through software reset */
	ret = aic325x_reg_write(aic325x, AIC3XXX_RESET, reset);
	if (ret < 0) {
		dev_err(aic325x->dev, "Could not write to AIC3XXX register\n");
		goto err_return;
	}

	usleep_range(10000, 10500);

	ret = aic325x_reg_read(aic325x, AIC3XXX_DEVICE_ID);
	if (ret < 0) {
		dev_err(aic325x->dev, "Failed to read ID register\n");
		goto err_return;
	}
	devname = "TLV320AIC3256";
	aic325x->type = TLV320AIC3256;

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

	for (i = 0; i < aic325x->pdata.num_gpios; i++) {
		aic325x_reg_write(aic325x, aic325x->pdata.gpio_defaults[i].reg,
			aic325x->pdata.gpio_defaults[i].value);
	}

	if (aic325x->irq) {
		ret = aic325x_irq_init(aic325x);
		if (ret < 0)
			goto err_irq;
	}


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

	switch (aic325x->type) {
	case TLV320AIC3266:
	case TLV320AIC3262:
		ret = mfd_add_devices(aic325x->dev, -1, aic3262_devs,
			      ARRAY_SIZE(aic3262_devs), NULL, 0, NULL);
		break;
	case TLV320AIC3256:
		ret = mfd_add_devices(aic325x->dev, -1, aic3256_devs,
		              ARRAY_SIZE(aic3256_devs), NULL, 0, NULL);
		break;
	default:
		dev_err(aic325x->dev, "unable to recognize codec\n");
		break;
	}
	if (ret != 0) {
		dev_err(aic325x->dev, "Failed to add children: %d\n", ret);
		goto err_mfd;
	}
	dev_info(aic325x->dev, "aic325x_device_init added mfd devices\n");

	return 0;

err_mfd:

	aic325x_irq_exit(aic325x);
err_irq:

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

	if (aic325x->pdata.gpio_reset)
		gpio_free(aic325x->pdata.gpio_reset);

	return ret;
}
EXPORT_SYMBOL_GPL(aic325x_device_init);

void aic325x_device_exit(struct aic325x *aic325x)
{

	mfd_remove_devices(aic325x->dev);
	aic325x_irq_exit(aic325x);

	if (aic325x->pdata.gpio_irq)
		gpio_free(aic325x->pdata.naudint_irq);
	if (aic325x->pdata.gpio_reset)
		gpio_free(aic325x->pdata.gpio_reset);

}
EXPORT_SYMBOL_GPL(aic325x_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");