File Index
Files and directories
Sign in
FILE

aw87390.c

File preview with syntax highlighting.

linux-6.19.14/sound/soc/codecs/aw87390.c C
  1// SPDX-License-Identifier: GPL-2.0-only
  2//
  3// aw87390.c  --  AW87390 ALSA SoC Audio driver
  4//
  5// Copyright (c) 2023 awinic Technology CO., LTD
  6//
  7// Author: Weidong Wang <wangweidong.a@awinic.com>
  8//
  9
 10#include <linux/i2c.h>
 11#include <linux/firmware.h>
 12#include <linux/regmap.h>
 13#include <sound/soc.h>
 14#include "aw87390.h"
 15#include "aw88395/aw88395_data_type.h"
 16#include "aw88395/aw88395_device.h"
 17
 18static const struct regmap_config aw87390_remap_config = {
 19	.val_bits = 8,
 20	.reg_bits = 8,
 21	.max_register = AW87390_REG_MAX,
 22	.reg_format_endian = REGMAP_ENDIAN_LITTLE,
 23	.val_format_endian = REGMAP_ENDIAN_BIG,
 24};
 25
 26static int aw87390_dev_reg_update(struct aw_device *aw_dev,
 27					unsigned char *data, unsigned int len)
 28{
 29	int i, ret;
 30
 31	if (!data) {
 32		dev_err(aw_dev->dev, "data is NULL\n");
 33		return -EINVAL;
 34	}
 35
 36	for (i = 0; i < len-1; i += 2) {
 37		if (data[i] == AW87390_DELAY_REG_ADDR) {
 38			usleep_range(data[i + 1] * AW87390_REG_DELAY_TIME,
 39					data[i + 1] * AW87390_REG_DELAY_TIME + 10);
 40			continue;
 41		}
 42		ret = regmap_write(aw_dev->regmap, data[i], data[i + 1]);
 43		if (ret)
 44			return ret;
 45	}
 46
 47	return 0;
 48}
 49
 50static int aw87390_dev_get_prof_name(struct aw_device *aw_dev, int index, char **prof_name)
 51{
 52	struct aw_prof_info *prof_info = &aw_dev->prof_info;
 53	struct aw_prof_desc *prof_desc;
 54
 55	if ((index >= aw_dev->prof_info.count) || (index < 0)) {
 56		dev_err(aw_dev->dev, "index[%d] overflow count[%d]\n",
 57			index, aw_dev->prof_info.count);
 58		return -EINVAL;
 59	}
 60
 61	prof_desc = &aw_dev->prof_info.prof_desc[index];
 62
 63	*prof_name = prof_info->prof_name_list[prof_desc->id];
 64
 65	return 0;
 66}
 67
 68static int aw87390_dev_get_prof_data(struct aw_device *aw_dev, int index,
 69			struct aw_prof_desc **prof_desc)
 70{
 71	if ((index >= aw_dev->prof_info.count) || (index < 0)) {
 72		dev_err(aw_dev->dev, "%s: index[%d] overflow count[%d]\n",
 73				__func__, index, aw_dev->prof_info.count);
 74		return -EINVAL;
 75	}
 76
 77	*prof_desc = &aw_dev->prof_info.prof_desc[index];
 78
 79	return 0;
 80}
 81
 82static int aw87390_dev_fw_update(struct aw_device *aw_dev)
 83{
 84	struct aw_prof_desc *prof_index_desc;
 85	struct aw_sec_data_desc *sec_desc;
 86	char *prof_name;
 87	int ret;
 88
 89	ret = aw87390_dev_get_prof_name(aw_dev, aw_dev->prof_index, &prof_name);
 90	if (ret) {
 91		dev_err(aw_dev->dev, "get prof name failed\n");
 92		return -EINVAL;
 93	}
 94
 95	dev_dbg(aw_dev->dev, "start update %s", prof_name);
 96
 97	ret = aw87390_dev_get_prof_data(aw_dev, aw_dev->prof_index, &prof_index_desc);
 98	if (ret) {
 99		dev_err(aw_dev->dev, "aw87390_dev_get_prof_data failed\n");
100		return ret;
101	}
102
103	/* update reg */
104	sec_desc = prof_index_desc->sec_desc;
105	ret = aw87390_dev_reg_update(aw_dev, sec_desc[AW88395_DATA_TYPE_REG].data,
106					sec_desc[AW88395_DATA_TYPE_REG].len);
107	if (ret) {
108		dev_err(aw_dev->dev, "update reg failed\n");
109		return ret;
110	}
111
112	aw_dev->prof_cur = aw_dev->prof_index;
113
114	return 0;
115}
116
117static int aw87390_power_off(struct aw_device *aw_dev)
118{
119	int ret;
120
121	if (aw_dev->status == AW87390_DEV_PW_OFF) {
122		dev_dbg(aw_dev->dev, "already power off\n");
123		return 0;
124	}
125
126	ret = regmap_write(aw_dev->regmap, AW87390_SYSCTRL_REG, AW87390_POWER_DOWN_VALUE);
127	if (ret)
128		return ret;
129	aw_dev->status = AW87390_DEV_PW_OFF;
130
131	return 0;
132}
133
134static int aw87390_power_on(struct aw_device *aw_dev)
135{
136	int ret;
137
138	if (aw_dev->status == AW87390_DEV_PW_ON) {
139		dev_dbg(aw_dev->dev, "already power on\n");
140		return 0;
141	}
142
143	if (!aw_dev->fw_status) {
144		dev_err(aw_dev->dev, "fw not load\n");
145		return -EINVAL;
146	}
147
148	ret = regmap_write(aw_dev->regmap, AW87390_SYSCTRL_REG, AW87390_POWER_DOWN_VALUE);
149	if (ret)
150		return ret;
151
152	ret = aw87390_dev_fw_update(aw_dev);
153	if (ret) {
154		dev_err(aw_dev->dev, "%s load profile failed\n", __func__);
155		return ret;
156	}
157	aw_dev->status = AW87390_DEV_PW_ON;
158
159	return 0;
160}
161
162static int aw87390_dev_set_profile_index(struct aw_device *aw_dev, int index)
163{
164	if ((index >= aw_dev->prof_info.count) || (index < 0))
165		return -EINVAL;
166
167	if (aw_dev->prof_index == index)
168		return -EPERM;
169
170	aw_dev->prof_index = index;
171
172	return 0;
173}
174
175static int aw87390_profile_info(struct snd_kcontrol *kcontrol,
176			 struct snd_ctl_elem_info *uinfo)
177{
178	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
179	struct aw87390 *aw87390 = snd_soc_component_get_drvdata(codec);
180	char *prof_name;
181	int count, ret;
182
183	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
184	uinfo->count = 1;
185
186	count = aw87390->aw_pa->prof_info.count;
187	if (count <= 0) {
188		uinfo->value.enumerated.items = 0;
189		return 0;
190	}
191
192	uinfo->value.enumerated.items = count;
193
194	if (uinfo->value.enumerated.item >= count)
195		uinfo->value.enumerated.item = count - 1;
196
197	count = uinfo->value.enumerated.item;
198
199	ret = aw87390_dev_get_prof_name(aw87390->aw_pa, count, &prof_name);
200	if (ret) {
201		strscpy(uinfo->value.enumerated.name, "null");
202		return 0;
203	}
204
205	strscpy(uinfo->value.enumerated.name, prof_name);
206
207	return 0;
208}
209
210static int aw87390_profile_get(struct snd_kcontrol *kcontrol,
211			struct snd_ctl_elem_value *ucontrol)
212{
213	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
214	struct aw87390 *aw87390 = snd_soc_component_get_drvdata(codec);
215
216	ucontrol->value.integer.value[0] = aw87390->aw_pa->prof_index;
217
218	return 0;
219}
220
221static int aw87390_profile_set(struct snd_kcontrol *kcontrol,
222		struct snd_ctl_elem_value *ucontrol)
223{
224	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
225	struct aw87390 *aw87390 = snd_soc_component_get_drvdata(codec);
226	int ret;
227
228	mutex_lock(&aw87390->lock);
229	ret = aw87390_dev_set_profile_index(aw87390->aw_pa, ucontrol->value.integer.value[0]);
230	if (ret) {
231		dev_dbg(codec->dev, "profile index does not change\n");
232		mutex_unlock(&aw87390->lock);
233		return 0;
234	}
235
236	if (aw87390->aw_pa->status == AW87390_DEV_PW_ON) {
237		aw87390_power_off(aw87390->aw_pa);
238		aw87390_power_on(aw87390->aw_pa);
239	}
240
241	mutex_unlock(&aw87390->lock);
242
243	return 1;
244}
245
246static const struct snd_kcontrol_new aw87390_controls[] = {
247	AW87390_PROFILE_EXT("AW87390 Profile Set", aw87390_profile_info,
248		aw87390_profile_get, aw87390_profile_set),
249};
250
251static int aw87390_request_firmware_file(struct aw87390 *aw87390)
252{
253	const struct firmware *cont = NULL;
254	int ret;
255
256	aw87390->aw_pa->fw_status = AW87390_DEV_FW_FAILED;
257
258	ret = request_firmware(&cont, AW87390_ACF_FILE, aw87390->aw_pa->dev);
259	if (ret)
260		return dev_err_probe(aw87390->aw_pa->dev, ret,
261					"load [%s] failed!\n", AW87390_ACF_FILE);
262
263	dev_dbg(aw87390->aw_pa->dev, "loaded %s - size: %zu\n",
264			AW87390_ACF_FILE, cont ? cont->size : 0);
265
266	aw87390->aw_cfg = devm_kzalloc(aw87390->aw_pa->dev,
267				struct_size(aw87390->aw_cfg, data, cont->size), GFP_KERNEL);
268	if (!aw87390->aw_cfg) {
269		release_firmware(cont);
270		return -ENOMEM;
271	}
272
273	aw87390->aw_cfg->len = cont->size;
274	memcpy(aw87390->aw_cfg->data, cont->data, cont->size);
275	release_firmware(cont);
276
277	ret = aw88395_dev_load_acf_check(aw87390->aw_pa, aw87390->aw_cfg);
278	if (ret) {
279		dev_err(aw87390->aw_pa->dev, "load [%s] failed!\n", AW87390_ACF_FILE);
280		return ret;
281	}
282
283	mutex_lock(&aw87390->lock);
284
285	ret = aw88395_dev_cfg_load(aw87390->aw_pa, aw87390->aw_cfg);
286	if (ret)
287		dev_err(aw87390->aw_pa->dev, "aw_dev acf parse failed\n");
288
289	mutex_unlock(&aw87390->lock);
290
291	return ret;
292}
293
294static int aw87390_drv_event(struct snd_soc_dapm_widget *w,
295				struct snd_kcontrol *kcontrol, int event)
296{
297	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
298	struct aw87390 *aw87390 = snd_soc_component_get_drvdata(component);
299	struct aw_device *aw_dev = aw87390->aw_pa;
300	int ret;
301
302	switch (event) {
303	case SND_SOC_DAPM_PRE_PMU:
304		ret = aw87390_power_on(aw_dev);
305		break;
306	case SND_SOC_DAPM_POST_PMD:
307		ret = aw87390_power_off(aw_dev);
308		break;
309	default:
310		dev_err(aw_dev->dev, "%s: invalid event %d\n", __func__, event);
311		ret = -EINVAL;
312	}
313
314	return ret;
315}
316
317static const struct snd_soc_dapm_widget aw87390_dapm_widgets[] = {
318	SND_SOC_DAPM_INPUT("IN"),
319	SND_SOC_DAPM_PGA_E("SPK PA", SND_SOC_NOPM, 0, 0, NULL, 0, aw87390_drv_event,
320			       SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
321	SND_SOC_DAPM_OUTPUT("OUT"),
322};
323
324static const struct snd_soc_dapm_route aw87390_dapm_routes[] = {
325	{ "SPK PA", NULL, "IN" },
326	{ "OUT", NULL, "SPK PA" },
327};
328
329static int aw87390_codec_probe(struct snd_soc_component *component)
330{
331	struct aw87390 *aw87390 = snd_soc_component_get_drvdata(component);
332	int ret;
333
334	ret = aw87390_request_firmware_file(aw87390);
335	if (ret)
336		return dev_err_probe(aw87390->aw_pa->dev, ret,
337				"aw87390_request_firmware_file failed\n");
338
339	return 0;
340}
341
342static const struct snd_soc_component_driver soc_codec_dev_aw87390 = {
343	.probe = aw87390_codec_probe,
344	.dapm_widgets = aw87390_dapm_widgets,
345	.num_dapm_widgets = ARRAY_SIZE(aw87390_dapm_widgets),
346	.dapm_routes = aw87390_dapm_routes,
347	.num_dapm_routes = ARRAY_SIZE(aw87390_dapm_routes),
348	.controls = aw87390_controls,
349	.num_controls = ARRAY_SIZE(aw87390_controls),
350};
351
352static void aw87390_parse_channel_dt(struct aw87390 *aw87390)
353{
354	struct aw_device *aw_dev = aw87390->aw_pa;
355	struct device_node *np = aw_dev->dev->of_node;
356	u32 channel_value = AW87390_DEV_DEFAULT_CH;
357
358	of_property_read_u32(np, "awinic,audio-channel", &channel_value);
359
360	aw_dev->channel = channel_value;
361}
362
363static int aw87390_init(struct aw87390 *aw87390, struct i2c_client *i2c, struct regmap *regmap)
364{
365	struct aw_device *aw_dev;
366	unsigned int chip_id;
367	int ret;
368
369	/* read chip id */
370	ret = regmap_read(regmap, AW87390_ID_REG, &chip_id);
371	if (ret) {
372		dev_err(&i2c->dev, "%s read chipid error. ret = %d\n", __func__, ret);
373		return ret;
374	}
375
376	if (chip_id != AW87390_CHIP_ID) {
377		dev_err(&i2c->dev, "unsupported device\n");
378		return -ENXIO;
379	}
380
381	dev_dbg(&i2c->dev, "chip id = 0x%x\n", chip_id);
382
383	aw_dev = devm_kzalloc(&i2c->dev, sizeof(*aw_dev), GFP_KERNEL);
384	if (!aw_dev)
385		return -ENOMEM;
386
387	aw87390->aw_pa = aw_dev;
388	aw_dev->i2c = i2c;
389	aw_dev->regmap = regmap;
390	aw_dev->dev = &i2c->dev;
391	aw_dev->chip_id = AW87390_CHIP_ID;
392	aw_dev->acf = NULL;
393	aw_dev->prof_info.prof_desc = NULL;
394	aw_dev->prof_info.count = 0;
395	aw_dev->prof_info.prof_type = AW88395_DEV_NONE_TYPE_ID;
396	aw_dev->channel = AW87390_DEV_DEFAULT_CH;
397	aw_dev->fw_status = AW87390_DEV_FW_FAILED;
398	aw_dev->prof_index = AW87390_INIT_PROFILE;
399	aw_dev->status = AW87390_DEV_PW_OFF;
400
401	aw87390_parse_channel_dt(aw87390);
402
403	return 0;
404}
405
406static int aw87390_i2c_probe(struct i2c_client *i2c)
407{
408	struct aw87390 *aw87390;
409	int ret;
410
411	ret = i2c_check_functionality(i2c->adapter, I2C_FUNC_I2C);
412	if (!ret)
413		return dev_err_probe(&i2c->dev, -ENXIO, "check_functionality failed\n");
414
415	aw87390 = devm_kzalloc(&i2c->dev, sizeof(*aw87390), GFP_KERNEL);
416	if (!aw87390)
417		return -ENOMEM;
418
419	mutex_init(&aw87390->lock);
420
421	i2c_set_clientdata(i2c, aw87390);
422
423	aw87390->regmap = devm_regmap_init_i2c(i2c, &aw87390_remap_config);
424	if (IS_ERR(aw87390->regmap))
425		return dev_err_probe(&i2c->dev, PTR_ERR(aw87390->regmap),
426					"failed to init regmap\n");
427
428	/* aw pa init */
429	ret = aw87390_init(aw87390, i2c, aw87390->regmap);
430	if (ret)
431		return ret;
432
433	ret = regmap_write(aw87390->regmap, AW87390_ID_REG, AW87390_SOFT_RESET_VALUE);
434	if (ret)
435		return ret;
436
437	ret = devm_snd_soc_register_component(&i2c->dev,
438				&soc_codec_dev_aw87390, NULL, 0);
439	if (ret)
440		dev_err(&i2c->dev, "failed to register aw87390: %d\n", ret);
441
442	return ret;
443}
444
445static const struct i2c_device_id aw87390_i2c_id[] = {
446	{ AW87390_I2C_NAME },
447	{ }
448};
449MODULE_DEVICE_TABLE(i2c, aw87390_i2c_id);
450
451static struct i2c_driver aw87390_i2c_driver = {
452	.driver = {
453		.name = AW87390_I2C_NAME,
454	},
455	.probe = aw87390_i2c_probe,
456	.id_table = aw87390_i2c_id,
457};
458module_i2c_driver(aw87390_i2c_driver);
459
460MODULE_DESCRIPTION("ASoC AW87390 PA Driver");
461MODULE_LICENSE("GPL v2");
~/ Read-only file preview.