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 18 static 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 26 static 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 50 static 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 68 static 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 82 static 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 117 static 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 134 static 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 162 static 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 175 static int aw87390_profile_info(struct snd_kcontrol *kcontrol, 176 struct snd_ctl_elem_info *uinfo) 177 { 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; 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 name = uinfo->value.enumerated.name; 198 count = uinfo->value.enumerated.item; 199 200 ret = aw87390_dev_get_prof_name(aw87390->aw_pa, count, &prof_name); 201 if (ret) { 202 strscpy(uinfo->value.enumerated.name, "null", 203 strlen("null") + 1); 204 return 0; 205 } 206 207 strscpy(name, prof_name, sizeof(uinfo->value.enumerated.name)); 208 209 return 0; 210 } 211 212 static int aw87390_profile_get(struct snd_kcontrol *kcontrol, 213 struct snd_ctl_elem_value *ucontrol) 214 { 215 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol); 216 struct aw87390 *aw87390 = snd_soc_component_get_drvdata(codec); 217 218 ucontrol->value.integer.value[0] = aw87390->aw_pa->prof_index; 219 220 return 0; 221 } 222 223 static int aw87390_profile_set(struct snd_kcontrol *kcontrol, 224 struct snd_ctl_elem_value *ucontrol) 225 { 226 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol); 227 struct aw87390 *aw87390 = snd_soc_component_get_drvdata(codec); 228 int ret; 229 230 mutex_lock(&aw87390->lock); 231 ret = aw87390_dev_set_profile_index(aw87390->aw_pa, ucontrol->value.integer.value[0]); 232 if (ret) { 233 dev_dbg(codec->dev, "profile index does not change\n"); 234 mutex_unlock(&aw87390->lock); 235 return 0; 236 } 237 238 if (aw87390->aw_pa->status == AW87390_DEV_PW_ON) { 239 aw87390_power_off(aw87390->aw_pa); 240 aw87390_power_on(aw87390->aw_pa); 241 } 242 243 mutex_unlock(&aw87390->lock); 244 245 return 1; 246 } 247 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), 251 }; 252 253 static int aw87390_request_firmware_file(struct aw87390 *aw87390) 254 { 255 const struct firmware *cont = NULL; 256 int ret; 257 258 aw87390->aw_pa->fw_status = AW87390_DEV_FW_FAILED; 259 260 ret = request_firmware(&cont, AW87390_ACF_FILE, aw87390->aw_pa->dev); 261 if (ret) 262 return dev_err_probe(aw87390->aw_pa->dev, ret, 263 "load [%s] failed!\n", AW87390_ACF_FILE); 264 265 dev_dbg(aw87390->aw_pa->dev, "loaded %s - size: %zu\n", 266 AW87390_ACF_FILE, cont ? cont->size : 0); 267 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); 272 return -ENOMEM; 273 } 274 275 aw87390->aw_cfg->len = cont->size; 276 memcpy(aw87390->aw_cfg->data, cont->data, cont->size); 277 release_firmware(cont); 278 279 ret = aw88395_dev_load_acf_check(aw87390->aw_pa, aw87390->aw_cfg); 280 if (ret) { 281 dev_err(aw87390->aw_pa->dev, "load [%s] failed!\n", AW87390_ACF_FILE); 282 return ret; 283 } 284 285 mutex_lock(&aw87390->lock); 286 287 ret = aw88395_dev_cfg_load(aw87390->aw_pa, aw87390->aw_cfg); 288 if (ret) 289 dev_err(aw87390->aw_pa->dev, "aw_dev acf parse failed\n"); 290 291 mutex_unlock(&aw87390->lock); 292 293 return ret; 294 } 295 296 static int aw87390_drv_event(struct snd_soc_dapm_widget *w, 297 struct snd_kcontrol *kcontrol, int event) 298 { 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; 302 int ret; 303 304 switch (event) { 305 case SND_SOC_DAPM_PRE_PMU: 306 ret = aw87390_power_on(aw_dev); 307 break; 308 case SND_SOC_DAPM_POST_PMD: 309 ret = aw87390_power_off(aw_dev); 310 break; 311 default: 312 dev_err(aw_dev->dev, "%s: invalid event %d\n", __func__, event); 313 ret = -EINVAL; 314 } 315 316 return ret; 317 } 318 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"), 324 }; 325 326 static const struct snd_soc_dapm_route aw87390_dapm_routes[] = { 327 { "SPK PA", NULL, "IN" }, 328 { "OUT", NULL, "SPK PA" }, 329 }; 330 331 static int aw87390_codec_probe(struct snd_soc_component *component) 332 { 333 struct aw87390 *aw87390 = snd_soc_component_get_drvdata(component); 334 int ret; 335 336 ret = aw87390_request_firmware_file(aw87390); 337 if (ret) 338 return dev_err_probe(aw87390->aw_pa->dev, ret, 339 "aw87390_request_firmware_file failed\n"); 340 341 return 0; 342 } 343 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), 352 }; 353 354 static void aw87390_parse_channel_dt(struct aw87390 *aw87390) 355 { 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; 359 360 of_property_read_u32(np, "awinic,audio-channel", &channel_value); 361 362 aw_dev->channel = channel_value; 363 } 364 365 static int aw87390_init(struct aw87390 **aw87390, struct i2c_client *i2c, struct regmap *regmap) 366 { 367 struct aw_device *aw_dev; 368 unsigned int chip_id; 369 int ret; 370 371 /* read chip id */ 372 ret = regmap_read(regmap, AW87390_ID_REG, &chip_id); 373 if (ret) { 374 dev_err(&i2c->dev, "%s read chipid error. ret = %d\n", __func__, ret); 375 return ret; 376 } 377 378 if (chip_id != AW87390_CHIP_ID) { 379 dev_err(&i2c->dev, "unsupported device\n"); 380 return -ENXIO; 381 } 382 383 dev_dbg(&i2c->dev, "chip id = 0x%x\n", chip_id); 384 385 aw_dev = devm_kzalloc(&i2c->dev, sizeof(*aw_dev), GFP_KERNEL); 386 if (!aw_dev) 387 return -ENOMEM; 388 389 (*aw87390)->aw_pa = aw_dev; 390 aw_dev->i2c = i2c; 391 aw_dev->regmap = regmap; 392 aw_dev->dev = &i2c->dev; 393 aw_dev->chip_id = AW87390_CHIP_ID; 394 aw_dev->acf = NULL; 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; 402 403 aw87390_parse_channel_dt(*aw87390); 404 405 return 0; 406 } 407 408 static int aw87390_i2c_probe(struct i2c_client *i2c) 409 { 410 struct aw87390 *aw87390; 411 int ret; 412 413 ret = i2c_check_functionality(i2c->adapter, I2C_FUNC_I2C); 414 if (!ret) 415 return dev_err_probe(&i2c->dev, -ENXIO, "check_functionality failed\n"); 416 417 aw87390 = devm_kzalloc(&i2c->dev, sizeof(*aw87390), GFP_KERNEL); 418 if (!aw87390) 419 return -ENOMEM; 420 421 mutex_init(&aw87390->lock); 422 423 i2c_set_clientdata(i2c, aw87390); 424 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"); 429 430 /* aw pa init */ 431 ret = aw87390_init(&aw87390, i2c, aw87390->regmap); 432 if (ret) 433 return ret; 434 435 ret = regmap_write(aw87390->regmap, AW87390_ID_REG, AW87390_SOFT_RESET_VALUE); 436 if (ret) 437 return ret; 438 439 ret = devm_snd_soc_register_component(&i2c->dev, 440 &soc_codec_dev_aw87390, NULL, 0); 441 if (ret) 442 dev_err(&i2c->dev, "failed to register aw87390: %d\n", ret); 443 444 return ret; 445 } 446 447 static const struct i2c_device_id aw87390_i2c_id[] = { 448 { AW87390_I2C_NAME, 0 }, 449 { } 450 }; 451 MODULE_DEVICE_TABLE(i2c, aw87390_i2c_id); 452 453 static struct i2c_driver aw87390_i2c_driver = { 454 .driver = { 455 .name = AW87390_I2C_NAME, 456 }, 457 .probe = aw87390_i2c_probe, 458 .id_table = aw87390_i2c_id, 459 }; 460 module_i2c_driver(aw87390_i2c_driver); 461 462 MODULE_DESCRIPTION("ASoC AW87390 PA Driver"); 463 MODULE_LICENSE("GPL v2"); 464