1 // SPDX-License-Identifier: GPL-2.0-only
3 // aw87390.c -- AW87390 ALSA SoC Audio driver
5 // Copyright (c) 2023 awinic Technology CO., LTD
10 #include <linux/i2c.h>
11 #include <linux/firmware.h>
12 #include <linux/regmap.h>
13 #include <sound/soc.h>
15 #include "aw88395/aw88395_data_type.h"
16 #include "aw88395/aw88395_device.h"
18 static const struct regmap_config aw87390_remap_config = {
21 .max_register = AW87390_REG_MAX,
22 .reg_format_endian = REGMAP_ENDIAN_LITTLE,
23 .val_format_endian = REGMAP_ENDIAN_BIG,
26 static int aw87390_dev_reg_update(struct aw_device *aw_dev,
27 unsigned char *data, unsigned int len)
32 dev_err(aw_dev->dev, "data is NULL\n");
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);
42 ret = regmap_write(aw_dev->regmap, data[i], data[i + 1]);
50 static int aw87390_dev_get_prof_name(struct aw_device *aw_dev, int index, char **prof_name)
52 struct aw_prof_info *prof_info = &aw_dev->prof_info;
53 struct aw_prof_desc *prof_desc;
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);
61 prof_desc = &aw_dev->prof_info.prof_desc[index];
63 *prof_name = prof_info->prof_name_list[prof_desc->id];
68 static int aw87390_dev_get_prof_data(struct aw_device *aw_dev, int index,
69 struct aw_prof_desc **prof_desc)
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);
77 *prof_desc = &aw_dev->prof_info.prof_desc[index];
82 static int aw87390_dev_fw_update(struct aw_device *aw_dev)
84 struct aw_prof_desc *prof_index_desc;
85 struct aw_sec_data_desc *sec_desc;
89 ret = aw87390_dev_get_prof_name(aw_dev, aw_dev->prof_index, &prof_name);
91 dev_err(aw_dev->dev, "get prof name failed\n");
95 dev_dbg(aw_dev->dev, "start update %s", prof_name);
97 ret = aw87390_dev_get_prof_data(aw_dev, aw_dev->prof_index, &prof_index_desc);
99 dev_err(aw_dev->dev, "aw87390_dev_get_prof_data failed\n");
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);
108 dev_err(aw_dev->dev, "update reg failed\n");
112 aw_dev->prof_cur = aw_dev->prof_index;
117 static int aw87390_power_off(struct aw_device *aw_dev)
121 if (aw_dev->status == AW87390_DEV_PW_OFF) {
122 dev_dbg(aw_dev->dev, "already power off\n");
126 ret = regmap_write(aw_dev->regmap, AW87390_SYSCTRL_REG, AW87390_POWER_DOWN_VALUE);
129 aw_dev->status = AW87390_DEV_PW_OFF;
134 static int aw87390_power_on(struct aw_device *aw_dev)
138 if (aw_dev->status == AW87390_DEV_PW_ON) {
139 dev_dbg(aw_dev->dev, "already power on\n");
143 if (!aw_dev->fw_status) {
144 dev_err(aw_dev->dev, "fw not load\n");
148 ret = regmap_write(aw_dev->regmap, AW87390_SYSCTRL_REG, AW87390_POWER_DOWN_VALUE);
152 ret = aw87390_dev_fw_update(aw_dev);
154 dev_err(aw_dev->dev, "%s load profile failed\n", __func__);
157 aw_dev->status = AW87390_DEV_PW_ON;
162 static int aw87390_dev_set_profile_index(struct aw_device *aw_dev, int index)
164 if ((index >= aw_dev->prof_info.count) || (index < 0))
167 if (aw_dev->prof_index == index)
170 aw_dev->prof_index = index;
175 static int aw87390_profile_info(struct snd_kcontrol *kcontrol,
176 struct snd_ctl_elem_info *uinfo)
178 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
179 struct aw87390 *aw87390 = snd_soc_component_get_drvdata(codec);
180 char *prof_name, *name;
183 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
186 count = aw87390->aw_pa->prof_info.count;
188 uinfo->value.enumerated.items = 0;
192 uinfo->value.enumerated.items = count;
194 if (uinfo->value.enumerated.item >= count)
195 uinfo->value.enumerated.item = count - 1;
197 name = uinfo->value.enumerated.name;
198 count = uinfo->value.enumerated.item;
200 ret = aw87390_dev_get_prof_name(aw87390->aw_pa, count, &prof_name);
202 strscpy(uinfo->value.enumerated.name, "null",
207 strscpy(name, prof_name, sizeof(uinfo->value.enumerated.name));
212 static int aw87390_profile_get(struct snd_kcontrol *kcontrol,
213 struct snd_ctl_elem_value *ucontrol)
215 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
216 struct aw87390 *aw87390 = snd_soc_component_get_drvdata(codec);
218 ucontrol->value.integer.value[0] = aw87390->aw_pa->prof_index;
223 static int aw87390_profile_set(struct snd_kcontrol *kcontrol,
224 struct snd_ctl_elem_value *ucontrol)
226 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
227 struct aw87390 *aw87390 = snd_soc_component_get_drvdata(codec);
230 mutex_lock(&aw87390->lock);
231 ret = aw87390_dev_set_profile_index(aw87390->aw_pa, ucontrol->value.integer.value[0]);
233 dev_dbg(codec->dev, "profile index does not change\n");
234 mutex_unlock(&aw87390->lock);
238 if (aw87390->aw_pa->status == AW87390_DEV_PW_ON) {
239 aw87390_power_off(aw87390->aw_pa);
240 aw87390_power_on(aw87390->aw_pa);
243 mutex_unlock(&aw87390->lock);
248 static const struct snd_kcontrol_new aw87390_controls[] = {
249 AW87390_PROFILE_EXT("AW87390 Profile Set", aw87390_profile_info,
250 aw87390_profile_get, aw87390_profile_set),
253 static int aw87390_request_firmware_file(struct aw87390 *aw87390)
255 const struct firmware *cont = NULL;
258 aw87390->aw_pa->fw_status = AW87390_DEV_FW_FAILED;
260 ret = request_firmware(&cont, AW87390_ACF_FILE, aw87390->aw_pa->dev);
262 return dev_err_probe(aw87390->aw_pa->dev, ret,
263 "load [%s] failed!\n", AW87390_ACF_FILE);
265 dev_dbg(aw87390->aw_pa->dev, "loaded %s - size: %zu\n",
266 AW87390_ACF_FILE, cont ? cont->size : 0);
268 aw87390->aw_cfg = devm_kzalloc(aw87390->aw_pa->dev,
269 struct_size(aw87390->aw_cfg, data, cont->size), GFP_KERNEL);
270 if (!aw87390->aw_cfg) {
271 release_firmware(cont);
275 aw87390->aw_cfg->len = cont->size;
276 memcpy(aw87390->aw_cfg->data, cont->data, cont->size);
277 release_firmware(cont);
279 ret = aw88395_dev_load_acf_check(aw87390->aw_pa, aw87390->aw_cfg);
281 dev_err(aw87390->aw_pa->dev, "load [%s] failed!\n", AW87390_ACF_FILE);
285 mutex_lock(&aw87390->lock);
287 ret = aw88395_dev_cfg_load(aw87390->aw_pa, aw87390->aw_cfg);
289 dev_err(aw87390->aw_pa->dev, "aw_dev acf parse failed\n");
291 mutex_unlock(&aw87390->lock);
296 static int aw87390_drv_event(struct snd_soc_dapm_widget *w,
297 struct snd_kcontrol *kcontrol, int event)
299 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
300 struct aw87390 *aw87390 = snd_soc_component_get_drvdata(component);
301 struct aw_device *aw_dev = aw87390->aw_pa;
305 case SND_SOC_DAPM_PRE_PMU:
306 ret = aw87390_power_on(aw_dev);
308 case SND_SOC_DAPM_POST_PMD:
309 ret = aw87390_power_off(aw_dev);
312 dev_err(aw_dev->dev, "%s: invalid event %d\n", __func__, event);
319 static const struct snd_soc_dapm_widget aw87390_dapm_widgets[] = {
320 SND_SOC_DAPM_INPUT("IN"),
321 SND_SOC_DAPM_PGA_E("SPK PA", SND_SOC_NOPM, 0, 0, NULL, 0, aw87390_drv_event,
322 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
323 SND_SOC_DAPM_OUTPUT("OUT"),
326 static const struct snd_soc_dapm_route aw87390_dapm_routes[] = {
327 { "SPK PA", NULL, "IN" },
328 { "OUT", NULL, "SPK PA" },
331 static int aw87390_codec_probe(struct snd_soc_component *component)
333 struct aw87390 *aw87390 = snd_soc_component_get_drvdata(component);
336 ret = aw87390_request_firmware_file(aw87390);
338 return dev_err_probe(aw87390->aw_pa->dev, ret,
339 "aw87390_request_firmware_file failed\n");
344 static const struct snd_soc_component_driver soc_codec_dev_aw87390 = {
345 .probe = aw87390_codec_probe,
346 .dapm_widgets = aw87390_dapm_widgets,
347 .num_dapm_widgets = ARRAY_SIZE(aw87390_dapm_widgets),
348 .dapm_routes = aw87390_dapm_routes,
349 .num_dapm_routes = ARRAY_SIZE(aw87390_dapm_routes),
350 .controls = aw87390_controls,
351 .num_controls = ARRAY_SIZE(aw87390_controls),
354 static void aw87390_parse_channel_dt(struct aw87390 *aw87390)
356 struct aw_device *aw_dev = aw87390->aw_pa;
357 struct device_node *np = aw_dev->dev->of_node;
358 u32 channel_value = AW87390_DEV_DEFAULT_CH;
360 of_property_read_u32(np, "awinic,audio-channel", &channel_value);
362 aw_dev->channel = channel_value;
365 static int aw87390_init(struct aw87390 **aw87390, struct i2c_client *i2c, struct regmap *regmap)
367 struct aw_device *aw_dev;
368 unsigned int chip_id;
372 ret = regmap_read(regmap, AW87390_ID_REG, &chip_id);
374 dev_err(&i2c->dev, "%s read chipid error. ret = %d\n", __func__, ret);
378 if (chip_id != AW87390_CHIP_ID) {
379 dev_err(&i2c->dev, "unsupported device\n");
383 dev_dbg(&i2c->dev, "chip id = 0x%x\n", chip_id);
385 aw_dev = devm_kzalloc(&i2c->dev, sizeof(*aw_dev), GFP_KERNEL);
389 (*aw87390)->aw_pa = aw_dev;
391 aw_dev->regmap = regmap;
392 aw_dev->dev = &i2c->dev;
393 aw_dev->chip_id = AW87390_CHIP_ID;
395 aw_dev->prof_info.prof_desc = NULL;
396 aw_dev->prof_info.count = 0;
397 aw_dev->prof_info.prof_type = AW88395_DEV_NONE_TYPE_ID;
398 aw_dev->channel = AW87390_DEV_DEFAULT_CH;
399 aw_dev->fw_status = AW87390_DEV_FW_FAILED;
400 aw_dev->prof_index = AW87390_INIT_PROFILE;
401 aw_dev->status = AW87390_DEV_PW_OFF;
403 aw87390_parse_channel_dt(*aw87390);
408 static int aw87390_i2c_probe(struct i2c_client *i2c)
410 struct aw87390 *aw87390;
413 ret = i2c_check_functionality(i2c->adapter, I2C_FUNC_I2C);
415 return dev_err_probe(&i2c->dev, -ENXIO, "check_functionality failed\n");
417 aw87390 = devm_kzalloc(&i2c->dev, sizeof(*aw87390), GFP_KERNEL);
421 mutex_init(&aw87390->lock);
423 i2c_set_clientdata(i2c, aw87390);
425 aw87390->regmap = devm_regmap_init_i2c(i2c, &aw87390_remap_config);
426 if (IS_ERR(aw87390->regmap))
427 return dev_err_probe(&i2c->dev, PTR_ERR(aw87390->regmap),
428 "failed to init regmap\n");
431 ret = aw87390_init(&aw87390, i2c, aw87390->regmap);
435 ret = regmap_write(aw87390->regmap, AW87390_ID_REG, AW87390_SOFT_RESET_VALUE);
439 ret = devm_snd_soc_register_component(&i2c->dev,
440 &soc_codec_dev_aw87390, NULL, 0);
442 dev_err(&i2c->dev, "failed to register aw87390: %d\n", ret);
447 static const struct i2c_device_id aw87390_i2c_id[] = {
448 { AW87390_I2C_NAME, 0 },
451 MODULE_DEVICE_TABLE(i2c, aw87390_i2c_id);
453 static struct i2c_driver aw87390_i2c_driver = {
455 .name = AW87390_I2C_NAME,
457 .probe = aw87390_i2c_probe,
458 .id_table = aw87390_i2c_id,
460 module_i2c_driver(aw87390_i2c_driver);
462 MODULE_DESCRIPTION("ASoC AW87390 PA Driver");
463 MODULE_LICENSE("GPL v2");