summaryrefslogtreecommitdiff
path: root/sound/soc/codecs/tas5713.c
blob: a24c1da67b3dbf56c5db47364f11973fb72ae5a6 (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
/*
 * ASoC Driver for TAS5713
 *
 * Author:	Sebastian Eickhoff <basti.eickhoff@googlemail.com>
 *		Copyright 2014
 *
 * 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.
 */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/pm.h>
#include <linux/i2c.h>
#include <linux/of_device.h>
#include <linux/spi/spi.h>
#include <linux/regmap.h>
#include <linux/regulator/consumer.h>
#include <linux/slab.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include <sound/initval.h>
#include <sound/tlv.h>

#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/fs.h>
#include <asm/uaccess.h>

#include "tas5713.h"


static struct i2c_client *i2c;

struct tas5713_priv {
	struct regmap *regmap;
	int mclk_div;
	struct snd_soc_codec *codec;
};

static struct tas5713_priv *priv_data;




/*
 *    _   _    ___   _      ___         _           _
 *   /_\ | |  / __| /_\    / __|___ _ _| |_ _ _ ___| |___
 *  / _ \| |__\__ \/ _ \  | (__/ _ \ ' \  _| '_/ _ \ (_-<
 * /_/ \_\____|___/_/ \_\  \___\___/_||_\__|_| \___/_/__/
 *
 */

static const DECLARE_TLV_DB_SCALE(tas5713_vol_tlv, -10000, 50, 1);


static const struct snd_kcontrol_new tas5713_snd_controls[] = {
	SOC_SINGLE_TLV  ("Master"    , TAS5713_VOL_MASTER, 0, 248, 1, tas5713_vol_tlv),
	SOC_DOUBLE_R_TLV("Channels"  , TAS5713_VOL_CH1, TAS5713_VOL_CH2, 0, 248, 1, tas5713_vol_tlv)
};




/*
 *  __  __         _    _            ___      _
 * |  \/  |__ _ __| |_ (_)_ _  ___  |   \ _ _(_)_ _____ _ _
 * | |\/| / _` / _| ' \| | ' \/ -_) | |) | '_| \ V / -_) '_|
 * |_|  |_\__,_\__|_||_|_|_||_\___| |___/|_| |_|\_/\___|_|
 *
 */

static int tas5713_hw_params(struct snd_pcm_substream *substream,
			    struct snd_pcm_hw_params *params,
			    struct snd_soc_dai *dai)
{
	u16 blen = 0x00;

	struct snd_soc_codec *codec;
	codec = dai->codec;
	priv_data->codec = dai->codec;

	switch (params_format(params)) {
	case SNDRV_PCM_FORMAT_S16_LE:
		blen = 0x03;
		break;
	case SNDRV_PCM_FORMAT_S20_3LE:
		blen = 0x1;
		break;
	case SNDRV_PCM_FORMAT_S24_LE:
		blen = 0x04;
		break;
	case SNDRV_PCM_FORMAT_S32_LE:
		blen = 0x05;
		break;
	default:
		dev_err(dai->dev, "Unsupported word length: %u\n",
			params_format(params));
		return -EINVAL;
	}

	// set word length
	snd_soc_update_bits(codec, TAS5713_SERIAL_DATA_INTERFACE, 0x7, blen);

	return 0;
}


static int tas5713_mute_stream(struct snd_soc_dai *dai, int mute, int stream)
{
	unsigned int val = 0;

	struct tas5713_priv *tas5713;
	struct snd_soc_codec *codec = dai->codec;
	tas5713 = snd_soc_codec_get_drvdata(codec);

	if (mute) {
		val = TAS5713_SOFT_MUTE_ALL;
	}

	return regmap_write(tas5713->regmap, TAS5713_SOFT_MUTE, val);
}


static const struct snd_soc_dai_ops tas5713_dai_ops = {
	.hw_params 		= tas5713_hw_params,
	.mute_stream	= tas5713_mute_stream,
};


static struct snd_soc_dai_driver tas5713_dai = {
	.name		= "tas5713-hifi",
	.playback 	= {
		.stream_name	= "Playback",
		.channels_min	= 2,
		.channels_max	= 2,
		.rates		    = SNDRV_PCM_RATE_8000_48000,
		.formats	    = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE ),
	},
	.ops        = &tas5713_dai_ops,
};




/*
 *   ___         _          ___      _
 *  / __|___  __| |___ __  |   \ _ _(_)_ _____ _ _
 * | (__/ _ \/ _` / -_) _| | |) | '_| \ V / -_) '_|
 *  \___\___/\__,_\___\__| |___/|_| |_|\_/\___|_|
 *
 */

static int tas5713_remove(struct snd_soc_codec *codec)
{
	struct tas5713_priv *tas5713;

	tas5713 = snd_soc_codec_get_drvdata(codec);

	return 0;
}


static int tas5713_probe(struct snd_soc_codec *codec)
{
	struct tas5713_priv *tas5713;
	int i, ret;

	i2c = container_of(codec->dev, struct i2c_client, dev);

	tas5713 = snd_soc_codec_get_drvdata(codec);

	// Reset error
	ret = snd_soc_write(codec, TAS5713_ERROR_STATUS, 0x00);

	// Trim oscillator
    ret = snd_soc_write(codec, TAS5713_OSC_TRIM, 0x00);
	msleep(1000);

	// Reset error
	ret = snd_soc_write(codec, TAS5713_ERROR_STATUS, 0x00);

	// Clock mode: 44/48kHz, MCLK=64xfs
	ret = snd_soc_write(codec, TAS5713_CLOCK_CTRL, 0x60);

	// I2S 24bit
	ret = snd_soc_write(codec, TAS5713_SERIAL_DATA_INTERFACE, 0x05);

	// Unmute
	ret = snd_soc_write(codec, TAS5713_SYSTEM_CTRL2, 0x00);
	ret = snd_soc_write(codec, TAS5713_SOFT_MUTE, 0x00);

	// Set volume to 0db
	ret = snd_soc_write(codec, TAS5713_VOL_MASTER, 0x00);

	// Now start programming the default initialization sequence
	for (i = 0; i < ARRAY_SIZE(tas5713_init_sequence); ++i) {
		ret = i2c_master_send(i2c,
				     tas5713_init_sequence[i].data,
				     tas5713_init_sequence[i].size);

		if (ret < 0) {
			printk(KERN_INFO "TAS5713 CODEC PROBE: InitSeq returns: %d\n", ret);
		}
	}

	// Unmute
	ret = snd_soc_write(codec, TAS5713_SYSTEM_CTRL2, 0x00);


	return 0;
}


static struct snd_soc_codec_driver soc_codec_dev_tas5713 = {
	.probe = tas5713_probe,
	.remove = tas5713_remove,
	.controls = tas5713_snd_controls,
	.num_controls = ARRAY_SIZE(tas5713_snd_controls),
};




/*
 *   ___ ___ ___   ___      _
 *  |_ _|_  ) __| |   \ _ _(_)_ _____ _ _
 *   | | / / (__  | |) | '_| \ V / -_) '_|
 *  |___/___\___| |___/|_| |_|\_/\___|_|
 *
 */

static const struct reg_default tas5713_reg_defaults[] = {
	{ 0x07 ,0x80 },     // R7  - VOL_MASTER    - -40dB
	{ 0x08 ,  30 },     // R8  - VOL_CH1	   -   0dB
	{ 0x09 ,  30 },     // R9  - VOL_CH2       -   0dB
	{ 0x0A ,0x80 },     // R10 - VOL_HEADPHONE - -40dB
};


static bool tas5713_reg_volatile(struct device *dev, unsigned int reg)
{
	switch (reg) {
		case TAS5713_DEVICE_ID:
		case TAS5713_ERROR_STATUS:
			return true;
	default:
			return false;
	}
}


static const struct of_device_id tas5713_of_match[] = {
	{ .compatible = "ti,tas5713", },
	{ }
};
MODULE_DEVICE_TABLE(of, tas5713_of_match);


static struct regmap_config tas5713_regmap_config = {
	.reg_bits = 8,
	.val_bits = 8,

	.max_register = TAS5713_MAX_REGISTER,
	.volatile_reg = tas5713_reg_volatile,

	.cache_type = REGCACHE_RBTREE,
	.reg_defaults = tas5713_reg_defaults,
	.num_reg_defaults = ARRAY_SIZE(tas5713_reg_defaults),
};


static int tas5713_i2c_probe(struct i2c_client *i2c,
			    const struct i2c_device_id *id)
{
	int ret;

	priv_data = devm_kzalloc(&i2c->dev, sizeof *priv_data, GFP_KERNEL);
	if (!priv_data)
		return -ENOMEM;

	priv_data->regmap = devm_regmap_init_i2c(i2c, &tas5713_regmap_config);
	if (IS_ERR(priv_data->regmap)) {
		ret = PTR_ERR(priv_data->regmap);
		return ret;
	}

	i2c_set_clientdata(i2c, priv_data);

	ret = snd_soc_register_codec(&i2c->dev,
				     &soc_codec_dev_tas5713, &tas5713_dai, 1);

	return ret;
}


static int tas5713_i2c_remove(struct i2c_client *i2c)
{
	snd_soc_unregister_codec(&i2c->dev);
	i2c_set_clientdata(i2c, NULL);

	kfree(priv_data);

	return 0;
}


static const struct i2c_device_id tas5713_i2c_id[] = {
	{ "tas5713", 0 },
	{ }
};

MODULE_DEVICE_TABLE(i2c, tas5713_i2c_id);


static struct i2c_driver tas5713_i2c_driver = {
	.driver = {
		.name = "tas5713",
		.owner = THIS_MODULE,
		.of_match_table = tas5713_of_match,
	},
	.probe = tas5713_i2c_probe,
	.remove = tas5713_i2c_remove,
	.id_table = tas5713_i2c_id
};


static int __init tas5713_modinit(void)
{
	int ret = 0;

	ret = i2c_add_driver(&tas5713_i2c_driver);
	if (ret) {
		printk(KERN_ERR "Failed to register tas5713 I2C driver: %d\n",
		       ret);
	}

	return ret;
}
module_init(tas5713_modinit);


static void __exit tas5713_exit(void)
{
	i2c_del_driver(&tas5713_i2c_driver);
}
module_exit(tas5713_exit);


MODULE_AUTHOR("Sebastian Eickhoff <basti.eickhoff@googlemail.com>");
MODULE_DESCRIPTION("ASoC driver for TAS5713");
MODULE_LICENSE("GPL v2");