aboutsummaryrefslogtreecommitdiff
path: root/sound/soc/codecs/timpani.c
blob: f80c0fcf4cb18d0238433ea44ddbc79502e65663 (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
/* Copyright (c) 2010, The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only 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.
 *
 */
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/platform_device.h>
#include <linux/mfd/marimba.h>
#include <linux/mfd/timpani-audio.h>
#include <sound/soc.h>
#include <sound/initval.h>
#include <sound/soc-dapm.h>
/* Debug purpose */
#include <linux/gpio.h>
#include <linux/clk.h>
#include <mach/mpp.h>
/* End of debug purpose */

#define ADIE_CODEC_MAX 2

struct adie_codec_register {
	u8 reg;
	u8 mask;
	u8 val;
};

static struct adie_codec_register dmic_on[] = {
	{0x80, 0x05, 0x05},
	{0x80, 0x05, 0x00},
	{0x83, 0x0C, 0x00},
	{0x8A, 0xF0, 0x30},
	{0x86, 0xFF, 0xAC},
	{0x87, 0xFF, 0xAC},
	{0x8A, 0xF0, 0xF0},
	{0x82, 0x1F, 0x1E},
	{0x83, 0x0C, 0x0C},
	{0x92, 0x3F, 0x21},
	{0x94, 0x3F, 0x24},
	{0xA3, 0x39, 0x01},
	{0xA8, 0x0F, 0x00},
	{0xAB, 0x3F, 0x00},
	{0x86, 0xFF, 0x00},
	{0x87, 0xFF, 0x00},
	{0x8A, 0xF0, 0xC0},
};

static struct adie_codec_register dmic_off[] = {
	{0x8A, 0xF0, 0xF0},
	{0x83, 0x0C, 0x00},
	{0x92, 0xFF, 0x00},
	{0x94, 0xFF, 0x1B},
};

static struct adie_codec_register spk_on[] = {
	{0x80, 0x02, 0x02},
	{0x80, 0x02, 0x00},
	{0x83, 0x03, 0x00},
	{0x8A, 0x0F, 0x03},
	{0xA3, 0x02, 0x02},
	{0x84, 0xFF, 0x00},
	{0x85, 0xFF, 0x00},
	{0x8A, 0x0F, 0x0C},
	{0x81, 0xFF, 0x0E},
	{0x83, 0x03, 0x03},
	{0x24, 0x6F, 0x6C},
	{0xB7, 0x01, 0x01},
	{0x31, 0x01, 0x01},
	{0x32, 0xF8, 0x08},
	{0x32, 0xF8, 0x48},
	{0x32, 0xF8, 0xF8},
	{0xE0, 0xFE, 0xAC},
	{0xE1, 0xFE, 0xAC},
	{0x3A, 0x24, 0x24},
	{0xE0, 0xFE, 0x3C},
	{0xE1, 0xFE, 0x3C},
	{0xE0, 0xFE, 0x1C},
	{0xE1, 0xFE, 0x1C},
	{0xE0, 0xFE, 0x10},
	{0xE1, 0xFE, 0x10},
};

static struct adie_codec_register spk_off[] = {
	{0x8A, 0x0F, 0x0F},
	{0xE0, 0xFE, 0x1C},
	{0xE1, 0xFE, 0x1C},
	{0xE0, 0xFE, 0x3C},
	{0xE1, 0xFE, 0x3C},
	{0xE0, 0xFC, 0xAC},
	{0xE1, 0xFC, 0xAC},
	{0x32, 0xF8, 0x00},
	{0x31, 0x05, 0x00},
	{0x3A, 0x24, 0x00},
};

static struct adie_codec_register spk_mute[] = {
	{0x84, 0xFF, 0xAC},
	{0x85, 0xFF, 0xAC},
	{0x8A, 0x0F, 0x0C},
};

static struct adie_codec_register spk_unmute[] = {
	{0x84, 0xFF, 0x00},
	{0x85, 0xFF, 0x00},
	{0x8A, 0x0F, 0x0C},
};

struct adie_codec_path {
	int rate; /* sample rate of path */
	u32 reg_owner;
};

struct timpani_drv_data { /* member undecided */
	struct snd_soc_codec codec;
	struct adie_codec_path path[ADIE_CODEC_MAX];
	u32 ref_cnt;
	struct marimba_codec_platform_data *codec_pdata;
};

static struct snd_soc_codec *timpani_codec;

enum /* regaccess blk id */
{
	RA_BLOCK_RX1 = 0,
	RA_BLOCK_RX2,
	RA_BLOCK_TX1,
	RA_BLOCK_TX2,
	RA_BLOCK_LB,
	RA_BLOCK_SHARED_RX_LB,
	RA_BLOCK_SHARED_TX,
	RA_BLOCK_TXFE1,
	RA_BLOCK_TXFE2,
	RA_BLOCK_PA_COMMON,
	RA_BLOCK_PA_EAR,
	RA_BLOCK_PA_HPH,
	RA_BLOCK_PA_LINE,
	RA_BLOCK_PA_AUX,
	RA_BLOCK_ADC,
	RA_BLOCK_DMIC,
	RA_BLOCK_TX_I2S,
	RA_BLOCK_DRV,
	RA_BLOCK_TEST,
	RA_BLOCK_RESERVED,
	RA_BLOCK_NUM,
};

enum /* regaccess onwer ID */
{
	RA_OWNER_NONE = 0,
	RA_OWNER_PATH_RX1,
	RA_OWNER_PATH_RX2,
	RA_OWNER_PATH_TX1,
	RA_OWNER_PATH_TX2,
	RA_OWNER_PATH_LB,
	RA_OWNER_DRV,
	RA_OWNER_NUM,
};

struct reg_acc_blk_cfg {
	u8 valid_owners[RA_OWNER_NUM];
};

struct timpani_regaccess {
	u8 reg_addr;
	u8 blk_mask[RA_BLOCK_NUM];
	u8 reg_mask;
	u8 reg_default;
};

static unsigned int timpani_codec_read(struct snd_soc_codec *codec,
				unsigned int reg)
{
	struct marimba *pdrv = codec->control_data;
	int rc;
	u8 val;

	rc = marimba_read(pdrv, reg, &val, 1);
	if (IS_ERR_VALUE(rc)) {
		pr_err("%s: fail to write reg %x\n", __func__, reg);
		return 0;
	}
	return val;
}

static int timpani_codec_write(struct snd_soc_codec *codec, unsigned int reg,
			unsigned int value)
{
	struct marimba *pdrv = codec->control_data;
	int rc;

	rc = marimba_write_bit_mask(pdrv, reg,  (u8 *)&value, 1, 0xFF);
	if (IS_ERR_VALUE(rc)) {
		pr_err("%s: fail to write reg %x\n", __func__, reg);
		return -EIO;
	}
	pr_debug("%s: write reg %x val %x\n", __func__, reg, value);
	return 0;
}

static void timpani_codec_bring_up(struct snd_soc_codec *codec)
{
	struct timpani_drv_data *timpani = snd_soc_codec_get_drvdata(codec);
	int rc;

	if (timpani->codec_pdata &&
	    timpani->codec_pdata->marimba_codec_power) {
		if (timpani->ref_cnt)
			return;
		/* Codec power up sequence */
		rc = timpani->codec_pdata->marimba_codec_power(1);
		if (rc)
			pr_err("%s: could not power up timpani "
				"codec\n", __func__);
		else {
			timpani_codec_write(codec, 0xFF, 0x08);
			timpani_codec_write(codec, 0xFF, 0x0A);
			timpani_codec_write(codec, 0xFF, 0x0E);
			timpani_codec_write(codec, 0xFF, 0x07);
			timpani_codec_write(codec, 0xFF, 0x17);
			timpani_codec_write(codec, TIMPANI_A_MREF, 0x22);
			msleep(15);
			timpani->ref_cnt++;
		}
	}
}

static void timpani_codec_bring_down(struct snd_soc_codec *codec)
{
	struct timpani_drv_data *timpani = snd_soc_codec_get_drvdata(codec);
	int rc;

	if (timpani->codec_pdata &&
	    timpani->codec_pdata->marimba_codec_power) {
		timpani->ref_cnt--;
		if (timpani->ref_cnt >= 1)
			return;
		timpani_codec_write(codec, TIMPANI_A_MREF, TIMPANI_MREF_POR);
		timpani_codec_write(codec, 0xFF, 0x07);
		timpani_codec_write(codec, 0xFF, 0x06);
		timpani_codec_write(codec, 0xFF, 0x0E);
		timpani_codec_write(codec, 0xFF, 0x08);
		rc = timpani->codec_pdata->marimba_codec_power(0);
		if (rc)
			pr_err("%s: could not power down timpani "
			"codec\n", __func__);
	}
}

static void timpani_dmic_config(struct snd_soc_codec *codec, int on)
{
	struct adie_codec_register *regs;
	int regs_sz, i;

	if (on) {
		regs = dmic_on;
		regs_sz = ARRAY_SIZE(dmic_on);
	} else {
		regs = dmic_off;
		regs_sz = ARRAY_SIZE(dmic_off);
	}

	for (i = 0; i < regs_sz; i++)
		timpani_codec_write(codec, regs[i].reg,
				(regs[i].mask & regs[i].val));
}

static void timpani_spk_config(struct snd_soc_codec *codec, int on)
{
	struct adie_codec_register *regs;
	int regs_sz, i;

	if (on) {
		regs = spk_on;
		regs_sz = ARRAY_SIZE(spk_on);
	} else {
		regs = spk_off;
		regs_sz = ARRAY_SIZE(spk_off);
	}

	for (i = 0; i < regs_sz; i++)
		timpani_codec_write(codec, regs[i].reg,
				(regs[i].mask & regs[i].val));
}

static int timpani_startup(struct snd_pcm_substream *substream,
		      struct snd_soc_dai *dai)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_device *socdev = rtd->socdev;
	struct snd_soc_codec *codec = socdev->card->codec;

	pr_info("%s()\n", __func__);

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
		pr_info("%s: playback\n", __func__);
		timpani_codec_bring_up(codec);
		timpani_spk_config(codec, 1);
	} else {
		pr_info("%s: Capture\n", __func__);
		timpani_codec_bring_up(codec);
		timpani_dmic_config(codec, 1);
	}
	return 0;
}

static void timpani_shutdown(struct snd_pcm_substream *substream,
			    struct snd_soc_dai *dai)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_device *socdev = rtd->socdev;
	struct snd_soc_codec *codec = socdev->card->codec;

	pr_info("%s()\n", __func__);
	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
		timpani_codec_bring_down(codec);
		timpani_spk_config(codec, 0);
	} else {
		timpani_codec_bring_down(codec);
		timpani_dmic_config(codec, 0);
	}
	return;
}

int digital_mute(struct snd_soc_dai *dai, int mute)
{
	struct snd_soc_codec *codec = dai->codec;
	struct adie_codec_register *regs;
	int regs_sz, i;

	if (mute) {
		regs = spk_mute;
		regs_sz = ARRAY_SIZE(spk_mute);
	} else {
		regs = spk_unmute;
		regs_sz = ARRAY_SIZE(spk_unmute);
	}

	for (i = 0; i < regs_sz; i++) {
		timpani_codec_write(codec, regs[i].reg,
			(regs[i].mask & regs[i].val));
		msleep(10);
	}

	return 0;
}

static struct snd_soc_dai_ops timpani_dai_ops = {
	.startup	= timpani_startup,
	.shutdown	= timpani_shutdown,
};

struct snd_soc_dai timpani_codec_dai[] = {
	{
		.name = "TIMPANI Rx",
		.playback = {
			.stream_name = "Handset Playback",
			.rates = SNDRV_PCM_RATE_8000_96000,
			.formats = SNDRV_PCM_FMTBIT_S16_LE,
			.rate_max =	96000,
			.rate_min =	8000,
			.channels_min = 1,
			.channels_max = 2,
		},
		.ops = &timpani_dai_ops,
	},
	{
		.name = "TIMPANI Tx",
		.capture = {
			.stream_name = "Handset Capture",
			.rates = SNDRV_PCM_RATE_8000_96000,
			.formats = SNDRV_PCM_FMTBIT_S16_LE,
			.rate_max =	96000,
			.rate_min =	8000,
			.channels_min = 1,
			.channels_max = 2,
		},
		.ops = &timpani_dai_ops,
	}
};

static int timpani_soc_probe(struct platform_device *pdev)
{
	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
	struct snd_soc_codec *codec;
	int ret = 0;

	if (!timpani_codec) {
		dev_err(&pdev->dev, "core driver not yet probed\n");
		return -ENODEV;
	}

	socdev->card->codec = timpani_codec;
	codec = timpani_codec;

	ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
	if (ret < 0)
		dev_err(codec->dev, "failed to create pcms\n");
	return ret;
}

/* power down chip */
static int timpani_soc_remove(struct platform_device *pdev)
{
	struct snd_soc_device *socdev = platform_get_drvdata(pdev);

	snd_soc_free_pcms(socdev);
	return 0;
}

struct snd_soc_codec_device soc_codec_dev_timpani = {
	.probe =	timpani_soc_probe,
	.remove =	timpani_soc_remove,
};
EXPORT_SYMBOL_GPL(soc_codec_dev_timpani);

static int timpani_codec_probe(struct platform_device *pdev)
{
	struct snd_soc_codec *codec;
	struct timpani_drv_data *priv;

	pr_info("%s()\n", __func__);
	priv = kzalloc(sizeof(struct timpani_drv_data), GFP_KERNEL);
	if (priv == NULL)
		return -ENOMEM;

	codec = &priv->codec;
	snd_soc_codec_set_drvdata(codec, priv);
	priv->codec_pdata = pdev->dev.platform_data;

	mutex_init(&codec->mutex);
	INIT_LIST_HEAD(&codec->dapm_widgets);
	INIT_LIST_HEAD(&codec->dapm_paths);

	codec->name = "TIMPANI";
	codec->owner = THIS_MODULE;
	codec->read = timpani_codec_read;
	codec->write = timpani_codec_write;
	codec->dai = timpani_codec_dai;
	codec->num_dai = ARRAY_SIZE(timpani_codec_dai);
	codec->control_data = platform_get_drvdata(pdev);
	timpani_codec = codec;

	snd_soc_register_dais(timpani_codec_dai, ARRAY_SIZE(timpani_codec_dai));
	snd_soc_register_codec(codec);

	return 0;
}

static struct platform_driver timpani_codec_driver = {
	.probe = timpani_codec_probe,
	.driver = {
		.name = "timpani_codec",
		.owner = THIS_MODULE,
	},
};

static int __init timpani_codec_init(void)
{
	return platform_driver_register(&timpani_codec_driver);
}

static void __exit timpani_codec_exit(void)
{
	platform_driver_unregister(&timpani_codec_driver);
}

module_init(timpani_codec_init);
module_exit(timpani_codec_exit);

MODULE_DESCRIPTION("Timpani codec driver");
MODULE_VERSION("1.0");
MODULE_LICENSE("GPL v2");