summaryrefslogtreecommitdiff
path: root/asoc/codecs/tas256x/algo/src/tas25xx-algo-bin-utils.c
blob: 36cba8cd0164e3cf254e2553f48e9b4ab4e6ed3d (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
/*
** =============================================================================
** Copyright (c) 2016  Texas Instruments Inc.
**
** This program is free software; you can redistribute it and/or modify it under
** the terms of the GNU General Public License as published by the Free Software
** Foundation; version 2.
**
** 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.
**
** File:
**     tas25xx-algo-bin-utils.c
**
** Description:
**     Algorithm parameter's binary file utility wrapper.
**
** =============================================================================
*/
#include <linux/types.h>
#include <linux/device.h>
#include <linux/firmware.h>
#include <linux/slab.h>

#include "tas25xx-algo-bin-utils.h"

const char *binfilenames[NUM_SPK_TYPES] = {
	"tas25xx_TI_0.bin",
	"tas25xx_TI_1.bin",
	"tas25xx_TI_2.bin",
	"tas25xx_TI_3.bin"
};

static struct bin_file_data_t s_bin = {
	.uses_bin_file = 0,
	.parse_done = 0,
	.profile_id = 0,
	.spk_id = 0,
	.bytes_per_prof_per_channel = 0,
	.number_of_profiles = 0,
	.no_of_channels = 0,
	.single_profile_size = 0,
	.profile_data = NULL,
};

static struct device *s_dev;

void bin_file_set_device(struct device *dev)
{
	s_dev = dev;
}

static inline struct device *bin_file_get_device(void)
{
	return s_dev;
}

/*
 * returns 0 on success, -ve errorcode otherwise
 */
int bin_file_parse_init(void)
{
	int ret = 0;
	char metadata[SA_BIN_MDATA_SZ] = { 0 };
	char header[SA_BIN_HDR_SIZE] = { 0 };
	uint32_t *header_ptr = (uint32_t *)header;
	const struct firmware *fw_entry = NULL;
	struct device *dev;
	int32_t i = 0;
	int bin_data_size;

	int32_t *data;
	int32_t version;
	int32_t time;
	char *ddc;
	char ddcl[64] = { 0 };

	s_bin.uses_bin_file = 1;

	if (s_bin.profile_data)
		return 0;

	dev = bin_file_get_device();
	if (!dev) {
		pr_err("TI-SmartPA BIN: struct device is null\n");
		return -EINVAL;
	}

	if ((s_bin.spk_id >= 0) && (s_bin.spk_id < NUM_SPK_TYPES)) {
		printk(KERN_INFO "TI-SmartPA BIN : Speaker ID:%d,Bin file : %s\n", s_bin.spk_id, binfilenames[s_bin.spk_id]);
		ret = request_firmware(&fw_entry, binfilenames[s_bin.spk_id], dev);
		if (ret || !fw_entry || !fw_entry->data || (fw_entry->size  < (SA_BIN_MDATA_SZ+SA_BIN_HDR_SIZE))) {
			printk(KERN_ERR "TI-SmartPA BIN : request_firmware failed with error=%d, fw_entry=%p, size=%d\n", ret, fw_entry,
							fw_entry ? (int)fw_entry->size : 0);
			return ret;
		}

		memcpy(metadata, fw_entry->data, SA_BIN_MDATA_SZ);

		data = (int32_t *)metadata;
		version = data[0];
		time = data[1];

		ddc = (char *)data;
		while (((71 - i) > 8) && ddc[71 - i]) {
			ddcl[i] = ddc[71 - i];
			i++;
		}

		printk(KERN_INFO "TI-SmartPA BIN: Version = 0x%x\n", version);
		printk(KERN_INFO "TI-SmartPA BIN: Time = %d seconds from epoch time\n", time);
		printk(KERN_INFO "TI-SmartPA BIN: DDC Name = %s\n", ddcl);
		printk(KERN_INFO "TI-SmartPA BIN: metadata read !!\n");

		memcpy(header, (char *)(fw_entry->data) + SA_BIN_MDATA_SZ, SA_BIN_HDR_SIZE);
		s_bin.number_of_profiles = header_ptr[0];
		s_bin.no_of_channels = (header_ptr[1] >> 24) & 0xFF;
		s_bin.single_profile_size = header_ptr[1] & 0xFFFF;
		s_bin.bytes_per_prof_per_channel = s_bin.single_profile_size / s_bin.no_of_channels;

		printk(KERN_INFO"TI-SmartPA BIN: [BIN]: num of prof=%d, num of ch=%d, single profile sz=%d\n",
				s_bin.number_of_profiles,
				s_bin.no_of_channels,
				s_bin.single_profile_size);

		bin_data_size = s_bin.number_of_profiles * s_bin.single_profile_size;
		if (bin_data_size == (fw_entry->size - SA_BIN_MDATA_SZ - SA_BIN_HDR_SIZE)) {
			s_bin.profile_data = kmalloc(bin_data_size, GFP_KERNEL);
		} else {
			printk(KERN_ERR "TI-SmartPA BIN : size mismatch bin vs firmware\n");
			s_bin.profile_data = NULL;
		}

		if (!s_bin.profile_data) {
			printk(KERN_ERR "TI-SmartPA BIN : kmalloc failed\n");
			bin_file_parse_deinit();
			ret = -ENOMEM;
		} else {
			char *data_start = ((char *)(fw_entry->data)) + SA_BIN_MDATA_SZ + SA_BIN_HDR_SIZE;
			memcpy (s_bin.profile_data, data_start, bin_data_size);
			s_bin.parse_done = 1;
			printk(KERN_INFO "TI-SmartPA BIN: [BIN]: Read %d bytes successfully !!!\n", bin_data_size);
		}

		release_firmware(fw_entry);
	} else {
		printk(KERN_ERR "TI-SmartPA BIN : Invalid speaker ID : %d\n", s_bin.spk_id);
		return -EINVAL;
	}

	return ret;
}

/*
 * 1 is success, 0 is false
 */
bool bin_file_parse_status(void)
{
	if (s_bin.uses_bin_file && s_bin.parse_done)
		return true;

	return false;
}

/* parse profile apr */
bool bin_file_get_profile_data(char **buf, int *size)
{
	bool success = false;
	int profile_id;

	if (s_bin.profile_id < 0) {
		s_bin.profile_id = 0;
	}

	profile_id = s_bin.profile_id;

	printk(KERN_INFO "TI-SmartPA BIN: [BIN] load_profile %d", s_bin.profile_id);

	/*
	 * profile_id [0- g_number_of_profiles)
	 */
	if (!bin_file_parse_init() && (profile_id < s_bin.number_of_profiles) && buf && size) {

		int sz = s_bin.bytes_per_prof_per_channel;
		uint8_t *ptr = (uint8_t *)s_bin.profile_data;
		ptr += (profile_id * s_bin.single_profile_size);
		*buf = ptr;

		if (s_bin.no_of_channels == 2) {
			sz += s_bin.bytes_per_prof_per_channel;
		}

		*size = sz;
		success = true;
	} else {
		printk(KERN_INFO "TI-SmartPA BIN:[BIN] error parsing bin file!!!");
	}

	return success;
}

void bin_file_parse_deinit(void)
{

	bool temp = s_bin.uses_bin_file;
	int old_spk_id = s_bin.spk_id;
	int old_profile_id = s_bin.profile_id;
	if (s_bin.profile_data)
		kfree(s_bin.profile_data);

	memset(&s_bin, 0, sizeof(s_bin));

	s_bin.profile_id = old_profile_id;
	s_bin.spk_id = old_spk_id;
	s_bin.uses_bin_file = temp;
}

void bin_file_set_profile_id(int profile_id)
{
	s_bin.profile_id = profile_id;
}

void bin_file_set_spk_id(int spk_id)
{

	bin_file_parse_deinit();
	s_bin.spk_id = spk_id;
}

/*read only functions*/
int bin_file_get_profile_id(void)
{
	return s_bin.profile_id;
}

int bin_file_get_spk_id(void)
{
	return s_bin.spk_id;
}

int bin_file_get_number_of_channels(void)
{
	return s_bin.no_of_channels;
}

bool uses_bin_file(void)
{
	return s_bin.uses_bin_file;
}

int bin_file_get_per_profile_per_channel_param_count(void)
{
	return s_bin.bytes_per_prof_per_channel / sizeof(int);
}

int32_t *bin_file_get_profile_0_data_pointer(void)
{
	if (s_bin.uses_bin_file && s_bin.parse_done)
		return (int32_t *)s_bin.profile_data;

	return NULL;
}

/**
 * custom value start - size
 * custom value 1
 * custom value 2
 */
int32_t bin_file_get_custom_value_by_idx(int index_request, int *value)
{
	int custom_index_start;
	int ret = -EINVAL;

	int32_t *data = bin_file_get_profile_0_data_pointer();
	if (data && value) {
		custom_index_start = data[CUSTOM_VALUE_INDEX_PTR];
		if (custom_index_start && (index_request < data[custom_index_start])) {
			custom_index_start += 1; /* points to data - index 0 */
			*value = data[custom_index_start + index_request];
			ret = 0;
		}
	}

	return ret;
}