xref: /OK3568_Linux_fs/kernel/sound/soc/codecs/aw87xxx/aw87xxx.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * aw87xxx.c  aw87xxx pa module
3  *
4  * Copyright (c) 2021 AWINIC Technology CO., LTD
5  *
6  * Author: Barry <zhaozhongbo@awinic.com>
7  *
8  * This program is free software; you can redistribute  it and/or modify it
9  * under  the terms of  the GNU General  Public License as published by the
10  * Free Software Foundation;  either version 2 of the  License, or (at your
11  * option) any later version.
12  *
13  */
14 
15 #include <linux/i2c.h>
16 #include <sound/pcm.h>
17 #include <sound/pcm_params.h>
18 #include <linux/gpio.h>
19 #include <linux/of_gpio.h>
20 #include <linux/interrupt.h>
21 #include <linux/delay.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/device.h>
25 #include <linux/irq.h>
26 #include <linux/firmware.h>
27 #include <linux/platform_device.h>
28 #include <linux/slab.h>
29 #include <linux/fs.h>
30 #include <linux/proc_fs.h>
31 #include <linux/uaccess.h>
32 #include <linux/io.h>
33 #include <linux/init.h>
34 #include <linux/pci.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/gameport.h>
37 #include <linux/moduleparam.h>
38 #include <linux/mutex.h>
39 #include <linux/timer.h>
40 #include <linux/workqueue.h>
41 #include <linux/hrtimer.h>
42 #include <linux/ktime.h>
43 #include <linux/kthread.h>
44 #include <uapi/sound/asound.h>
45 #include <sound/control.h>
46 #include <sound/soc.h>
47 #include "aw87xxx.h"
48 #include "aw_device.h"
49 #include "aw_log.h"
50 #include "aw_monitor.h"
51 #include "aw_acf_bin.h"
52 #include "aw_bin_parse.h"
53 
54 /*****************************************************************
55 * aw87xxx marco
56 ******************************************************************/
57 #define AW87XXX_I2C_NAME	"aw87xxx_pa"
58 #define AW87XXX_DRIVER_VERSION	"v2.2.0"
59 #define AW87XXX_FW_BIN_NAME	"aw87xxx_acf.bin"
60 
61 /*************************************************************************
62  * aw87xxx variable
63  ************************************************************************/
64 static LIST_HEAD(g_aw87xxx_list);
65 static DEFINE_MUTEX(g_aw87xxx_mutex_lock);
66 unsigned int g_aw87xxx_dev_cnt = 0;
67 
68 #ifdef AW_KERNEL_VER_OVER_4_19_1
69 static struct aw_componet_codec_ops aw_componet_codec_ops = {
70 	.add_codec_controls = snd_soc_add_component_controls,
71 	.unregister_codec = snd_soc_unregister_component,
72 };
73 #else
74 static struct aw_componet_codec_ops aw_componet_codec_ops = {
75 	.add_codec_controls = snd_soc_add_codec_controls,
76 	.unregister_codec = snd_soc_unregister_codec,
77 };
78 #endif
79 
80 
81 /************************************************************************
82  *
83  * aw87xxx device update profile
84  *
85  ************************************************************************/
aw87xxx_update_off_prof(struct aw87xxx * aw87xxx,char * profile)86 static int aw87xxx_update_off_prof(struct aw87xxx *aw87xxx, char *profile)
87 {
88 	int ret = 0;
89 	struct aw_prof_desc *prof_desc = NULL;
90 	struct aw_data_container *data_container = NULL;
91 	struct aw_device *aw_dev = &aw87xxx->aw_dev;
92 
93 	AW_DEV_LOGD(aw87xxx->dev, "enter");
94 
95 	mutex_lock(&aw87xxx->reg_lock);
96 
97 	aw_monitor_stop(&aw87xxx->monitor);
98 
99 	prof_desc = aw_acf_get_prof_desc_form_name(aw87xxx->dev, &aw87xxx->acf_info, profile);
100 	if (prof_desc == NULL)
101 		goto no_bin_pwr_off;
102 
103 	if (!prof_desc->prof_st)
104 		goto no_bin_pwr_off;
105 
106 
107 	data_container = &prof_desc->data_container;
108 	AW_DEV_LOGD(aw87xxx->dev, "get profile[%s] data len [%d]",
109 			profile, data_container->len);
110 
111 	if (aw_dev->hwen_status == AW_DEV_HWEN_OFF) {
112 		AW_DEV_LOGI(aw87xxx->dev, "profile[%s] has already load ", profile);
113 	} else {
114 		if (aw_dev->ops.pwr_off_func) {
115 			ret = aw_dev->ops.pwr_off_func(aw_dev, data_container);
116 			if (ret < 0) {
117 				AW_DEV_LOGE(aw87xxx->dev, "load profile[%s] failed ", profile);
118 				goto pwr_off_failed;
119 			}
120 		} else {
121 			ret = aw_dev_default_pwr_off(aw_dev, data_container);
122 			if (ret < 0) {
123 				AW_DEV_LOGE(aw87xxx->dev, "load profile[%s] failed ", profile);
124 				goto pwr_off_failed;
125 			}
126 		}
127 	}
128 
129 	aw87xxx->current_profile = prof_desc->prof_name;
130 	mutex_unlock(&aw87xxx->reg_lock);
131 
132 	return 0;
133 
134 pwr_off_failed:
135 no_bin_pwr_off:
136 	aw_dev_hw_pwr_ctrl(&aw87xxx->aw_dev, false);
137 	aw87xxx->current_profile = aw87xxx->prof_off_name;
138 	mutex_unlock(&aw87xxx->reg_lock);
139 	return ret;
140 }
141 
aw87xxx_update_profile(struct aw87xxx * aw87xxx,char * profile)142 int aw87xxx_update_profile(struct aw87xxx *aw87xxx, char *profile)
143 {
144 	int ret = -EINVAL;
145 	struct aw_prof_desc *prof_desc = NULL;
146 	struct aw_prof_info *prof_info = &aw87xxx->acf_info.prof_info;
147 	struct aw_data_container *data_container = NULL;
148 	struct aw_device *aw_dev = &aw87xxx->aw_dev;
149 
150 	AW_DEV_LOGD(aw87xxx->dev, "enter");
151 
152 	if (!prof_info->status) {
153 		AW_DEV_LOGE(aw87xxx->dev, "profile_cfg not load");
154 		return -EINVAL;
155 	}
156 
157 	if (0 == strncmp(profile, aw87xxx->prof_off_name, AW_PROFILE_STR_MAX))
158 		return aw87xxx_update_off_prof(aw87xxx, profile);
159 
160 	mutex_lock(&aw87xxx->reg_lock);
161 
162 	prof_desc = aw_acf_get_prof_desc_form_name(aw87xxx->dev, &aw87xxx->acf_info, profile);
163 	if (prof_desc == NULL) {
164 		AW_DEV_LOGE(aw87xxx->dev, "not found [%s] parameter", profile);
165 		mutex_unlock(&aw87xxx->reg_lock);
166 		return -EINVAL;
167 	}
168 
169 	if (!prof_desc->prof_st) {
170 		AW_DEV_LOGE(aw87xxx->dev, "not found data container");
171 		mutex_unlock(&aw87xxx->reg_lock);
172 		return -EINVAL;
173 	}
174 
175 	data_container = &prof_desc->data_container;
176 	AW_DEV_LOGD(aw87xxx->dev, "get profile[%s] data len [%d]",
177 			profile, data_container->len);
178 
179 	aw_monitor_stop(&aw87xxx->monitor);
180 
181 	if (aw_dev->ops.pwr_on_func) {
182 		ret = aw_dev->ops.pwr_on_func(aw_dev, data_container);
183 		if (ret < 0) {
184 			AW_DEV_LOGE(aw87xxx->dev, "load profile[%s] failed ",
185 				profile);
186 			mutex_unlock(&aw87xxx->reg_lock);
187 			return aw87xxx_update_off_prof(aw87xxx, aw87xxx->prof_off_name);
188 		}
189 	} else {
190 		ret = aw_dev_default_pwr_on(aw_dev, data_container);
191 		if (ret < 0) {
192 			AW_DEV_LOGE(aw87xxx->dev, "load profile[%s] failed ",
193 				profile);
194 			mutex_unlock(&aw87xxx->reg_lock);
195 			return aw87xxx_update_off_prof(aw87xxx, aw87xxx->prof_off_name);
196 		}
197 	}
198 
199 	aw87xxx->current_profile = prof_desc->prof_name;
200 	aw_monitor_start(&aw87xxx->monitor);
201 	mutex_unlock(&aw87xxx->reg_lock);
202 
203 	AW_DEV_LOGD(aw87xxx->dev, "load profile[%s] succeed", profile);
204 
205 	return 0;
206 }
207 
aw87xxx_show_current_profile(int dev_index)208 char *aw87xxx_show_current_profile(int dev_index)
209 {
210 	struct list_head *pos = NULL;
211 	struct aw87xxx *aw87xxx = NULL;
212 
213 	list_for_each(pos, &g_aw87xxx_list) {
214 		aw87xxx = list_entry(pos, struct aw87xxx, list);
215 		if (aw87xxx == NULL) {
216 			AW_LOGE("struct aw87xxx not ready");
217 			return NULL;
218 		}
219 
220 		if (aw87xxx->dev_index == dev_index) {
221 			AW_DEV_LOGI(aw87xxx->dev, "current profile is [%s]",
222 				aw87xxx->current_profile);
223 			return aw87xxx->current_profile;
224 		}
225 	}
226 
227 	AW_LOGE("not found struct aw87xxx, dev_index = [%d]", dev_index);
228 	return NULL;
229 }
230 EXPORT_SYMBOL(aw87xxx_show_current_profile);
231 
aw87xxx_set_profile(int dev_index,char * profile)232 int aw87xxx_set_profile(int dev_index, char *profile)
233 {
234 	struct list_head *pos = NULL;
235 	struct aw87xxx *aw87xxx = NULL;
236 
237 	list_for_each(pos, &g_aw87xxx_list) {
238 		aw87xxx = list_entry(pos, struct aw87xxx, list);
239 		if (aw87xxx == NULL) {
240 			AW_LOGE("struct aw87xxx not ready");
241 			return -EINVAL;
242 		}
243 
244 		if (profile && aw87xxx->dev_index == dev_index)
245 			return aw87xxx_update_profile(aw87xxx, profile);
246 	}
247 
248 	AW_LOGE("not found struct aw87xxx, dev_index = [%d]", dev_index);
249 	return -EINVAL;
250 }
251 EXPORT_SYMBOL(aw87xxx_set_profile);
252 
253 /************************************************************************
254  *
255  * aw87xxx esd update profile
256  *
257  ************************************************************************/
aw87xxx_esd_update_off_prof(struct aw87xxx * aw87xxx,char * profile)258 static int aw87xxx_esd_update_off_prof(struct aw87xxx *aw87xxx, char *profile)
259 {
260 	int ret = 0;
261 	struct aw_prof_desc *prof_desc = NULL;
262 	struct aw_data_container *data_container = NULL;
263 	struct aw_device *aw_dev = &aw87xxx->aw_dev;
264 
265 	AW_DEV_LOGD(aw87xxx->dev, "enter");
266 	prof_desc = aw_acf_get_prof_desc_form_name(aw87xxx->dev, &aw87xxx->acf_info, profile);
267 	if (prof_desc == NULL)
268 		goto no_bin_pwr_off;
269 
270 	if (!prof_desc->prof_st)
271 		goto no_bin_pwr_off;
272 
273 	data_container = &prof_desc->data_container;
274 	AW_DEV_LOGD(aw87xxx->dev, "get profile[%s] data len [%d]",
275 			profile, data_container->len);
276 
277 	if (aw_dev->hwen_status == AW_DEV_HWEN_OFF) {
278 		AW_DEV_LOGI(aw87xxx->dev, "profile[%s] has already load ", profile);
279 	} else {
280 		if (aw_dev->ops.pwr_off_func) {
281 			ret = aw_dev->ops.pwr_off_func(aw_dev, data_container);
282 			if (ret < 0) {
283 				AW_DEV_LOGE(aw87xxx->dev, "load profile[%s] failed ", profile);
284 				goto pwr_off_failed;
285 			}
286 		} else {
287 			ret = aw_dev_default_pwr_off(aw_dev, data_container);
288 			if (ret < 0) {
289 				AW_DEV_LOGE(aw87xxx->dev, "load profile[%s] failed ", profile);
290 				goto pwr_off_failed;
291 			}
292 		}
293 	}
294 
295 	aw87xxx->current_profile = prof_desc->prof_name;
296 	return 0;
297 
298 pwr_off_failed:
299 no_bin_pwr_off:
300 	aw_dev_hw_pwr_ctrl(&aw87xxx->aw_dev, false);
301 	aw87xxx->current_profile = aw87xxx->prof_off_name;
302 	return ret;
303 }
304 
aw87xxx_esd_update_profile(struct aw87xxx * aw87xxx,char * profile)305 int aw87xxx_esd_update_profile(struct aw87xxx *aw87xxx, char *profile)
306 {
307 	int ret = -EINVAL;
308 	struct aw_prof_desc *prof_desc = NULL;
309 	struct aw_prof_info *prof_info = &aw87xxx->acf_info.prof_info;
310 	struct aw_data_container *data_container = NULL;
311 	struct aw_device *aw_dev = &aw87xxx->aw_dev;
312 
313 	AW_DEV_LOGD(aw87xxx->dev, "enter");
314 
315 	if (!prof_info->status) {
316 		AW_DEV_LOGE(aw87xxx->dev, "profile_cfg not load");
317 		return -EINVAL;
318 	}
319 
320 	if (0 == strncmp(profile, aw87xxx->prof_off_name, AW_PROFILE_STR_MAX))
321 		return aw87xxx_esd_update_off_prof(aw87xxx, profile);
322 
323 	prof_desc = aw_acf_get_prof_desc_form_name(aw87xxx->dev, &aw87xxx->acf_info,
324 					profile);
325 	if (prof_desc == NULL) {
326 		AW_DEV_LOGE(aw87xxx->dev, "not found [%s] parameter", profile);
327 		return -EINVAL;
328 	}
329 
330 	if (!prof_desc->prof_st) {
331 		AW_DEV_LOGE(aw87xxx->dev, "not found data container");
332 		return -EINVAL;
333 	}
334 
335 	data_container = &prof_desc->data_container;
336 	AW_DEV_LOGD(aw87xxx->dev, "get profile[%s] data len [%d]",
337 			profile, data_container->len);
338 
339 	if (aw_dev->ops.pwr_on_func) {
340 		ret = aw_dev->ops.pwr_on_func(aw_dev, data_container);
341 		if (ret < 0) {
342 			AW_DEV_LOGE(aw87xxx->dev, "load profile[%s] failed ",
343 				profile);
344 			return ret;
345 		}
346 	} else {
347 		ret = aw_dev_default_pwr_on(aw_dev, data_container);
348 		if (ret < 0) {
349 			AW_DEV_LOGE(aw87xxx->dev, "load profile[%s] failed ",
350 				profile);
351 			return ret;
352 		}
353 	}
354 	aw87xxx->current_profile = prof_desc->prof_name;
355 	AW_DEV_LOGD(aw87xxx->dev, "recover load profile[%s] succeed", profile);
356 
357 	return 0;
358 }
359 
360 /****************************************************************************
361  *
362  * aw87xxx Kcontrols
363  *
364  ****************************************************************************/
aw87xxx_profile_switch_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)365 static int aw87xxx_profile_switch_info(struct snd_kcontrol *kcontrol,
366 			struct snd_ctl_elem_info *uinfo)
367 {
368 	int count = 0;
369 	char *name = NULL;
370 	char *profile_name = NULL;
371 	struct aw87xxx *aw87xxx = (struct aw87xxx *)kcontrol->private_value;
372 
373 	if (aw87xxx == NULL) {
374 		AW_LOGE("get struct aw87xxx failed");
375 		return -EINVAL;
376 	}
377 
378 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
379 	uinfo->count = 1;
380 
381 	/*make sure have prof */
382 	count = aw_acf_get_profile_count(aw87xxx->dev, &aw87xxx->acf_info);
383 	if (count <= 0) {
384 		uinfo->value.enumerated.items = 0;
385 		AW_DEV_LOGE(aw87xxx->dev, "get count[%d] failed", count);
386 		return 0;
387 	}
388 
389 	uinfo->value.enumerated.items = count;
390 	if (uinfo->value.enumerated.item >= count)
391 		uinfo->value.enumerated.item = count - 1;
392 
393 	name = uinfo->value.enumerated.name;
394 	count = uinfo->value.enumerated.item;
395 	profile_name = aw_acf_get_prof_name_form_index(aw87xxx->dev,
396 		&aw87xxx->acf_info, count);
397 	if (profile_name == NULL) {
398 		strlcpy(uinfo->value.enumerated.name, "NULL",
399 			strlen("NULL") + 1);
400 		return 0;
401 	}
402 
403 	strlcpy(name, profile_name, sizeof(uinfo->value.enumerated.name));
404 
405 	return 0;
406 }
407 
aw87xxx_profile_switch_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)408 static int aw87xxx_profile_switch_put(struct snd_kcontrol *kcontrol,
409 			struct snd_ctl_elem_value *ucontrol)
410 {
411 	int ret = -1;
412 	char *profile_name = NULL;
413 	int index = ucontrol->value.integer.value[0];
414 	struct aw87xxx *aw87xxx = (struct aw87xxx *)kcontrol->private_value;
415 	struct acf_bin_info *acf_info = &aw87xxx->acf_info;
416 
417 	if (aw87xxx == NULL) {
418 		AW_LOGE("get struct aw87xxx failed");
419 		return -EINVAL;
420 	}
421 
422 	profile_name = aw_acf_get_prof_name_form_index(aw87xxx->dev, acf_info, index);
423 	if (!profile_name) {
424 		AW_DEV_LOGE(aw87xxx->dev, "not found profile name,index=[%d]",
425 				index);
426 		return -EINVAL;
427 	}
428 
429 	AW_DEV_LOGI(aw87xxx->dev, "set profile [%s]", profile_name);
430 
431 	ret = aw87xxx_update_profile(aw87xxx, profile_name);
432 	if (ret < 0) {
433 		AW_DEV_LOGE(aw87xxx->dev, "set dev_index[%d] profile failed, profile = %s",
434 			aw87xxx->dev_index, profile_name);
435 		return ret;
436 	}
437 
438 	return 0;
439 }
440 
aw87xxx_profile_switch_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)441 static int aw87xxx_profile_switch_get(struct snd_kcontrol *kcontrol,
442 			struct snd_ctl_elem_value *ucontrol)
443 {
444 	int index = 0;
445 	char *profile;
446 	struct aw87xxx *aw87xxx = (struct aw87xxx *)kcontrol->private_value;
447 
448 	if (aw87xxx == NULL) {
449 		AW_LOGE("get struct aw87xxx failed");
450 		return -EINVAL;
451 	}
452 
453 	if (!aw87xxx->current_profile) {
454 		AW_DEV_LOGE(aw87xxx->dev, "profile not init");
455 		return -EINVAL;
456 	}
457 
458 	profile = aw87xxx->current_profile;
459 	AW_DEV_LOGI(aw87xxx->dev, "current profile:[%s]",
460 		aw87xxx->current_profile);
461 
462 
463 	index = aw_acf_get_prof_index_form_name(aw87xxx->dev,
464 		&aw87xxx->acf_info, aw87xxx->current_profile);
465 	if (index < 0) {
466 		AW_DEV_LOGE(aw87xxx->dev, "get profile index failed");
467 		return index;
468 	}
469 
470 	ucontrol->value.integer.value[0] = index;
471 
472 	return 0;
473 }
474 
aw87xxx_vmax_get_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)475 static int aw87xxx_vmax_get_info(struct snd_kcontrol *kcontrol,
476 			struct snd_ctl_elem_info *uinfo)
477 {
478 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
479 	uinfo->count = 1;
480 	uinfo->value.integer.min = INT_MIN;
481 	uinfo->value.integer.max = AW_VMAX_MAX;
482 
483 	return 0;
484 }
485 
aw87xxxx_vmax_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)486 static int aw87xxxx_vmax_get(struct snd_kcontrol *kcontrol,
487 			struct snd_ctl_elem_value *ucontrol)
488 {
489 	int ret = -1;
490 	int vmax_val = 0;
491 	struct aw87xxx *aw87xxx = (struct aw87xxx *)kcontrol->private_value;
492 
493 	if (aw87xxx == NULL) {
494 		AW_LOGE("get struct aw87xxx failed");
495 		return -EINVAL;
496 	}
497 
498 	ret = aw_monitor_no_dsp_get_vmax(&aw87xxx->monitor, &vmax_val);
499 	if (ret < 0)
500 		return ret;
501 
502 	ucontrol->value.integer.value[0] = vmax_val;
503 	AW_DEV_LOGI(aw87xxx->dev, "get vmax = [0x%x]", vmax_val);
504 
505 	return 0;
506 }
507 
aw87xxx_kcontrol_dynamic_create(struct aw87xxx * aw87xxx,void * codec)508 static int aw87xxx_kcontrol_dynamic_create(struct aw87xxx *aw87xxx,
509 						void *codec)
510 {
511 	struct snd_kcontrol_new *aw87xxx_kcontrol = NULL;
512 	aw_snd_soc_codec_t *soc_codec = (aw_snd_soc_codec_t *)codec;
513 	char *kctl_name[AW87XXX_KCONTROL_NUM];
514 	int kcontrol_num = AW87XXX_KCONTROL_NUM;
515 	int ret = -1;
516 
517 	AW_DEV_LOGD(aw87xxx->dev, "enter");
518 	aw87xxx->codec = soc_codec;
519 
520 	aw87xxx_kcontrol = devm_kzalloc(aw87xxx->dev,
521 			sizeof(struct snd_kcontrol_new) * kcontrol_num,
522 			GFP_KERNEL);
523 	if (aw87xxx_kcontrol == NULL) {
524 		AW_DEV_LOGE(aw87xxx->dev, "aw87xxx_kcontrol devm_kzalloc failed");
525 		return -ENOMEM;
526 	}
527 
528 	kctl_name[0] = devm_kzalloc(aw87xxx->dev, AW_NAME_BUF_MAX,
529 			GFP_KERNEL);
530 	if (kctl_name[0] == NULL)
531 		return -ENOMEM;
532 
533 	snprintf(kctl_name[0], AW_NAME_BUF_MAX, "aw87xxx_profile_switch_%d",
534 			aw87xxx->dev_index);
535 
536 	aw87xxx_kcontrol[0].name = kctl_name[0];
537 	aw87xxx_kcontrol[0].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
538 	aw87xxx_kcontrol[0].info = aw87xxx_profile_switch_info;
539 	aw87xxx_kcontrol[0].get = aw87xxx_profile_switch_get;
540 	aw87xxx_kcontrol[0].put = aw87xxx_profile_switch_put;
541 	aw87xxx_kcontrol[0].private_value = (unsigned long)aw87xxx;
542 
543 	kctl_name[1] = devm_kzalloc(aw87xxx->codec->dev, AW_NAME_BUF_MAX,
544 			GFP_KERNEL);
545 	if (kctl_name[1] == NULL)
546 		return -ENOMEM;
547 
548 	snprintf(kctl_name[1], AW_NAME_BUF_MAX, "aw87xxx_vmax_get_%d",
549 			aw87xxx->dev_index);
550 
551 	aw87xxx_kcontrol[1].name = kctl_name[1];
552 	aw87xxx_kcontrol[1].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
553 	aw87xxx_kcontrol[1].access = SNDRV_CTL_ELEM_ACCESS_READ;
554 	aw87xxx_kcontrol[1].info = aw87xxx_vmax_get_info;
555 	aw87xxx_kcontrol[1].get = aw87xxxx_vmax_get;
556 	aw87xxx_kcontrol[1].private_value = (unsigned long)aw87xxx;
557 
558 	ret = aw_componet_codec_ops.add_codec_controls(aw87xxx->codec,
559 				aw87xxx_kcontrol, kcontrol_num);
560 	if (ret < 0) {
561 		AW_DEV_LOGE(aw87xxx->dev, "add codec controls failed, ret = %d",
562 			ret);
563 		return ret;
564 	}
565 
566 	AW_DEV_LOGI(aw87xxx->dev, "add codec controls[%s,%s]",
567 		aw87xxx_kcontrol[0].name,
568 		aw87xxx_kcontrol[1].name);
569 
570 	return 0;
571 }
572 
573 /****************************************************************************
574  *
575  *aw87xxx kcontrol create
576  *
577  ****************************************************************************/
aw87xxx_add_codec_controls(void * codec)578 int aw87xxx_add_codec_controls(void *codec)
579 {
580 	struct list_head *pos = NULL;
581 	struct aw87xxx *aw87xxx = NULL;
582 	int ret = -1;
583 
584 	list_for_each(pos, &g_aw87xxx_list) {
585 		aw87xxx = list_entry(pos, struct aw87xxx, list);
586 		if (aw87xxx == NULL) {
587 			AW_LOGE("struct aw87xxx not ready");
588 			return ret;
589 		}
590 
591 		ret = aw87xxx_kcontrol_dynamic_create(aw87xxx, codec);
592 		if (ret < 0)
593 			return ret;
594 	}
595 
596 	return 0;
597 }
598 EXPORT_SYMBOL(aw87xxx_add_codec_controls);
599 
600 
601 /****************************************************************************
602  *
603  * aw87xxx firmware cfg load
604  *
605  ***************************************************************************/
aw87xxx_fw_cfg_free(struct aw87xxx * aw87xxx)606 static void aw87xxx_fw_cfg_free(struct aw87xxx *aw87xxx)
607 {
608 	AW_DEV_LOGD(aw87xxx->dev, "enter");
609 	aw_acf_profile_free(aw87xxx->dev, &aw87xxx->acf_info);
610 	aw_monitor_cfg_free(&aw87xxx->monitor);
611 }
612 
aw87xxx_init_default_prof(struct aw87xxx * aw87xxx)613 static int aw87xxx_init_default_prof(struct aw87xxx *aw87xxx)
614 {
615 	char *profile = NULL;
616 
617 	profile = aw_acf_get_prof_off_name(aw87xxx->dev, &aw87xxx->acf_info);
618 	if (profile == NULL) {
619 		AW_DEV_LOGE(aw87xxx->dev, "get profile off name failed");
620 		return -EINVAL;
621 	}
622 
623 	snprintf(aw87xxx->prof_off_name, AW_PROFILE_STR_MAX, "%s", profile);
624 	aw87xxx->current_profile = profile;
625 	AW_DEV_LOGI(aw87xxx->dev, "init profile name [%s]",
626 		aw87xxx->current_profile);
627 
628 	return 0;
629 }
630 
aw87xxx_fw_load_retry(struct aw87xxx * aw87xxx)631 static void aw87xxx_fw_load_retry(struct aw87xxx *aw87xxx)
632 {
633 	struct acf_bin_info *acf_info = &aw87xxx->acf_info;
634 	int ram_timer_val = 2000;
635 
636 	AW_DEV_LOGD(aw87xxx->dev, "failed to read [%s]",
637 			aw87xxx->fw_name);
638 
639 	if (acf_info->load_count < AW_LOAD_FW_RETRIES) {
640 		AW_DEV_LOGD(aw87xxx->dev,
641 			"restart hrtimer to load firmware");
642 		schedule_delayed_work(&aw87xxx->fw_load_work,
643 			msecs_to_jiffies(ram_timer_val));
644 	} else {
645 		acf_info->load_count = 0;
646 		AW_DEV_LOGE(aw87xxx->dev,
647 			"can not load firmware,please check name or file exists");
648 		return;
649 	}
650 	acf_info->load_count++;
651 }
652 
aw87xxx_fw_load(const struct firmware * fw,void * context)653 static void aw87xxx_fw_load(const struct firmware *fw, void *context)
654 {
655 	int ret = -1;
656 	struct aw87xxx *aw87xxx = context;
657 	struct acf_bin_info *acf_info = &aw87xxx->acf_info;
658 
659 	AW_DEV_LOGD(aw87xxx->dev, "enter");
660 
661 	if (!fw) {
662 		aw87xxx_fw_load_retry(aw87xxx);
663 		return;
664 	}
665 
666 	AW_DEV_LOGD(aw87xxx->dev, "loaded %s - size: %ld",
667 		aw87xxx->fw_name, (u_long)(fw ? fw->size : 0));
668 
669 	mutex_lock(&aw87xxx->reg_lock);
670 	acf_info->fw_data = vmalloc(fw->size);
671 	if (!acf_info->fw_data) {
672 		AW_DEV_LOGE(aw87xxx->dev, "fw_data kzalloc memory failed");
673 		goto exit_vmalloc_failed;
674 	}
675 	memset(acf_info->fw_data, 0, fw->size);
676 	memcpy(acf_info->fw_data, fw->data, fw->size);
677 	acf_info->fw_size = fw->size;
678 
679 	ret = aw_acf_parse(aw87xxx->dev, &aw87xxx->acf_info);
680 	if (ret < 0) {
681 		AW_DEV_LOGE(aw87xxx->dev, "fw_data parse failed");
682 		goto exit_acf_parse_failed;
683 	}
684 
685 	ret = aw87xxx_init_default_prof(aw87xxx);
686 	if (ret < 0) {
687 		aw87xxx_fw_cfg_free(aw87xxx);
688 		goto exit_acf_parse_failed;
689 	}
690 
691 	AW_DEV_LOGI(aw87xxx->dev, "acf parse succeed");
692 	mutex_unlock(&aw87xxx->reg_lock);
693 	release_firmware(fw);
694 	return;
695 
696 exit_acf_parse_failed:
697 exit_vmalloc_failed:
698 	release_firmware(fw);
699 	mutex_unlock(&aw87xxx->reg_lock);
700 }
701 
aw87xxx_fw_load_work_routine(struct work_struct * work)702 static void aw87xxx_fw_load_work_routine(struct work_struct *work)
703 {
704 	struct aw87xxx *aw87xxx = container_of(work,
705 			struct aw87xxx, fw_load_work.work);
706 	struct aw_prof_info *prof_info = &aw87xxx->acf_info.prof_info;
707 
708 	AW_DEV_LOGD(aw87xxx->dev, "enter");
709 
710 	if (prof_info->status == AW_ACF_WAIT) {
711 		const struct firmware *fw;
712 		int ret;
713 
714 		ret = request_firmware(&fw, aw87xxx->fw_name, aw87xxx->dev);
715 		if (!ret) {
716 			AW_DEV_LOGD(aw87xxx->dev, "loader firmware %s success\n", aw87xxx->fw_name);
717 			aw87xxx_fw_load(fw, aw87xxx);
718 		}
719 	}
720 }
721 
aw87xxx_fw_load_init(struct aw87xxx * aw87xxx)722 static void aw87xxx_fw_load_init(struct aw87xxx *aw87xxx)
723 {
724 #ifdef AW_CFG_UPDATE_DELAY
725 	int cfg_timer_val = AW_CFG_UPDATE_DELAY_TIMER;
726 #else
727 	int cfg_timer_val = 0;
728 #endif
729 	AW_DEV_LOGI(aw87xxx->dev, "enter");
730 	snprintf(aw87xxx->fw_name, AW87XXX_FW_NAME_MAX, "%s", AW87XXX_FW_BIN_NAME);
731 	aw_acf_init(&aw87xxx->aw_dev, &aw87xxx->acf_info, aw87xxx->dev_index);
732 
733 	INIT_DELAYED_WORK(&aw87xxx->fw_load_work, aw87xxx_fw_load_work_routine);
734 	schedule_delayed_work(&aw87xxx->fw_load_work,
735 			msecs_to_jiffies(cfg_timer_val));
736 }
737 
738 /****************************************************************************
739  *
740  *aw87xxx attribute node
741  *
742  ****************************************************************************/
aw87xxx_attr_get_reg(struct device * dev,struct device_attribute * attr,char * buf)743 static ssize_t aw87xxx_attr_get_reg(struct device *dev,
744 			struct device_attribute *attr, char *buf)
745 {
746 	ssize_t len = 0;
747 	int ret = 0;
748 	unsigned int i = 0;
749 	unsigned char reg_val = 0;
750 	struct aw87xxx *aw87xxx = dev_get_drvdata(dev);
751 	struct aw_device *aw_dev = &aw87xxx->aw_dev;
752 
753 	mutex_lock(&aw87xxx->reg_lock);
754 	for (i = 0; i < aw_dev->reg_max_addr; i++) {
755 		if (!(aw_dev->reg_access[i] & AW_DEV_REG_RD_ACCESS))
756 			continue;
757 		ret = aw_dev_i2c_read_byte(&aw87xxx->aw_dev, i, &reg_val);
758 		if (ret < 0) {
759 			len += snprintf(buf + len, PAGE_SIZE - len,
760 					"read reg [0x%x] failed\n", i);
761 			AW_DEV_LOGE(aw87xxx->dev, "read reg [0x%x] failed", i);
762 		} else {
763 			len += snprintf(buf + len, PAGE_SIZE - len,
764 					"reg:0x%02X=0x%02X\n", i, reg_val);
765 			AW_DEV_LOGD(aw87xxx->dev, "reg:0x%02X=0x%02X",
766 					i, reg_val);
767 		}
768 	}
769 	mutex_unlock(&aw87xxx->reg_lock);
770 
771 	return len;
772 }
773 
aw87xxx_attr_set_reg(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)774 static ssize_t aw87xxx_attr_set_reg(struct device *dev,
775 			struct device_attribute *attr, const char *buf,
776 			size_t len)
777 {
778 	unsigned int databuf[2] = { 0 };
779 	int ret = 0;
780 	struct aw87xxx *aw87xxx = dev_get_drvdata(dev);
781 
782 	mutex_lock(&aw87xxx->reg_lock);
783 	if (sscanf(buf, "0x%x 0x%x", &databuf[0], &databuf[1]) == 2) {
784 		if (databuf[0] >= aw87xxx->aw_dev.reg_max_addr) {
785 			AW_DEV_LOGE(aw87xxx->dev, "set reg[0x%x] error,is out of reg_addr_max[0x%x]",
786 				databuf[0], aw87xxx->aw_dev.reg_max_addr);
787 			mutex_unlock(&aw87xxx->reg_lock);
788 			return -EINVAL;
789 		}
790 
791 		ret = aw_dev_i2c_write_byte(&aw87xxx->aw_dev,
792 					databuf[0], databuf[1]);
793 		if (ret < 0)
794 			AW_DEV_LOGE(aw87xxx->dev, "set [0x%x]=0x%x failed",
795 				databuf[0], databuf[1]);
796 		else
797 			AW_DEV_LOGD(aw87xxx->dev, "set [0x%x]=0x%x succeed",
798 				databuf[0], databuf[1]);
799 	} else {
800 		AW_DEV_LOGE(aw87xxx->dev, "i2c write cmd input error");
801 	}
802 	mutex_unlock(&aw87xxx->reg_lock);
803 
804 	return len;
805 }
806 
aw87xxx_attr_get_profile(struct device * dev,struct device_attribute * attr,char * buf)807 static ssize_t aw87xxx_attr_get_profile(struct device *dev,
808 			struct device_attribute *attr, char *buf)
809 {
810 	ssize_t len = 0;
811 	unsigned int i = 0;
812 	struct aw87xxx *aw87xxx = dev_get_drvdata(dev);
813 	struct aw_prof_info *prof_info = &aw87xxx->acf_info.prof_info;
814 
815 	if (!prof_info->status) {
816 		len += snprintf(buf + len, PAGE_SIZE - len,
817 				"profile_cfg not load\n");
818 		return len;
819 	}
820 
821 	AW_DEV_LOGI(aw87xxx->dev, "current profile:[%s]", aw87xxx->current_profile);
822 
823 	for (i = 0; i < prof_info->count; i++) {
824 		if (!strncmp(aw87xxx->current_profile, prof_info->prof_name_list[i],
825 				AW_PROFILE_STR_MAX))
826 			len += snprintf(buf + len, PAGE_SIZE - len,
827 				">%s\n", prof_info->prof_name_list[i]);
828 		else
829 			len += snprintf(buf + len, PAGE_SIZE - len,
830 				" %s\n", prof_info->prof_name_list[i]);
831 	}
832 
833 	return len;
834 }
835 
aw87xxx_attr_set_profile(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)836 static ssize_t aw87xxx_attr_set_profile(struct device *dev,
837 			struct device_attribute *attr, const char *buf,
838 			size_t len)
839 {
840 	char profile[AW_PROFILE_STR_MAX] = {0};
841 	int ret = 0;
842 	struct aw87xxx *aw87xxx = dev_get_drvdata(dev);
843 
844 	if (sscanf(buf, "%s", profile) == 1) {
845 		AW_DEV_LOGD(aw87xxx->dev, "set profile [%s]", profile);
846 		ret = aw87xxx_update_profile(aw87xxx, profile);
847 		if (ret < 0) {
848 			AW_DEV_LOGE(aw87xxx->dev, "set profile[%s] failed",
849 				profile);
850 			return ret;
851 		}
852 	}
853 
854 	return len;
855 }
856 
aw87xxx_attr_get_hwen(struct device * dev,struct device_attribute * attr,char * buf)857 static ssize_t aw87xxx_attr_get_hwen(struct device *dev,
858 				struct device_attribute *attr, char *buf)
859 {
860 	ssize_t len = 0;
861 	struct aw87xxx *aw87xxx = dev_get_drvdata(dev);
862 	int hwen = aw87xxx->aw_dev.hwen_status;
863 
864 	if (hwen >= AW_DEV_HWEN_INVALID)
865 		len += snprintf(buf + len, PAGE_SIZE - len, "hwen_status: invalid\n");
866 	else if (hwen == AW_DEV_HWEN_ON)
867 		len += snprintf(buf + len, PAGE_SIZE - len, "hwen_status: on\n");
868 	else if (hwen == AW_DEV_HWEN_OFF)
869 		len += snprintf(buf + len, PAGE_SIZE - len, "hwen_status: off\n");
870 
871 	return len;
872 }
873 
aw87xxx_attr_set_hwen(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)874 static ssize_t aw87xxx_attr_set_hwen(struct device *dev,
875 				struct device_attribute *attr, const char *buf,
876 				size_t len)
877 {
878 	int ret = -1;
879 	unsigned int state;
880 	struct aw87xxx *aw87xxx = dev_get_drvdata(dev);
881 
882 	ret = kstrtouint(buf, 0, &state);
883 	if (ret) {
884 		AW_DEV_LOGE(aw87xxx->dev, "fail to channelge str to int");
885 		return ret;
886 	}
887 
888 	mutex_lock(&aw87xxx->reg_lock);
889 	if (state == AW_DEV_HWEN_OFF)
890 		aw_dev_hw_pwr_ctrl(&aw87xxx->aw_dev, false); /*OFF*/
891 	else if (state == AW_DEV_HWEN_ON)
892 		aw_dev_hw_pwr_ctrl(&aw87xxx->aw_dev, true); /*ON*/
893 	else
894 		AW_DEV_LOGE(aw87xxx->dev, "input [%d] error, hwen_on=[%d],hwen_off=[%d]",
895 			state, AW_DEV_HWEN_ON, AW_DEV_HWEN_OFF);
896 	mutex_unlock(&aw87xxx->reg_lock);
897 	return len;
898 }
899 
aw87xxx_awrw_write(struct aw87xxx * aw87xxx,const char * buf,size_t count)900 int aw87xxx_awrw_write(struct aw87xxx *aw87xxx,
901 			const char *buf, size_t count)
902 {
903 	int i = 0, ret = -1;
904 	char *data_buf = NULL;
905 	int buf_len = 0;
906 	int temp_data = 0;
907 	int data_str_size = 0;
908 	char *reg_data;
909 	struct aw_i2c_packet *packet = &aw87xxx->i2c_packet;
910 
911 	AW_DEV_LOGD(aw87xxx->dev, "enter");
912 	/* one addr or one data string Composition of Contains two bytes of symbol(0X)*/
913 	/* and two byte of hexadecimal data*/
914 	data_str_size = 2 + 2 * AWRW_DATA_BYTES;
915 
916 	/* The buf includes the first address of the register to be written and all data */
917 	buf_len = AWRW_ADDR_BYTES + packet->reg_num * AWRW_DATA_BYTES;
918 	AW_DEV_LOGI(aw87xxx->dev, "buf_len = %d,reg_num = %d", buf_len, packet->reg_num);
919 	data_buf = vmalloc(buf_len);
920 	if (data_buf == NULL) {
921 		AW_DEV_LOGE(aw87xxx->dev, "alloc memory failed");
922 		return -ENOMEM;
923 	}
924 	memset(data_buf, 0, buf_len);
925 
926 	data_buf[0] = packet->reg_addr;
927 	reg_data = data_buf + 1;
928 
929 	AW_DEV_LOGD(aw87xxx->dev, "reg_addr: 0x%02x", data_buf[0]);
930 
931 	/*ag:0x00 0x01 0x01 0x01 0x01 0x00\x0a*/
932 	for (i = 0; i < packet->reg_num; i++) {
933 		ret = sscanf(buf + AWRW_HDR_LEN + 1 + i * (data_str_size + 1),
934 			"0x%x", &temp_data);
935 		if (ret != 1) {
936 			AW_DEV_LOGE(aw87xxx->dev, "sscanf failed,ret=%d", ret);
937 			return ret;
938 		}
939 		reg_data[i] = temp_data;
940 		AW_DEV_LOGD(aw87xxx->dev, "[%d] : 0x%02x", i, reg_data[i]);
941 	}
942 
943 	mutex_lock(&aw87xxx->reg_lock);
944 	ret = i2c_master_send(aw87xxx->aw_dev.i2c, data_buf, buf_len);
945 	if (ret < 0) {
946 		AW_DEV_LOGE(aw87xxx->dev, "write failed");
947 		vfree(data_buf);
948 		data_buf = NULL;
949 		mutex_unlock(&aw87xxx->reg_lock);
950 		return -EFAULT;
951 	}
952 	mutex_unlock(&aw87xxx->reg_lock);
953 
954 	vfree(data_buf);
955 	data_buf = NULL;
956 
957 	AW_DEV_LOGD(aw87xxx->dev, "down");
958 	return 0;
959 }
960 
aw87xxx_awrw_data_check(struct aw87xxx * aw87xxx,int * data,size_t count)961 static int aw87xxx_awrw_data_check(struct aw87xxx *aw87xxx,
962 			int *data, size_t count)
963 {
964 	struct aw_i2c_packet *packet = &aw87xxx->i2c_packet;
965 	int req_data_len = 0;
966 	int act_data_len = 0;
967 	int data_str_size = 0;
968 
969 	if ((data[AWRW_HDR_ADDR_BYTES] != AWRW_ADDR_BYTES) ||
970 		(data[AWRW_HDR_DATA_BYTES] != AWRW_DATA_BYTES)) {
971 		AW_DEV_LOGE(aw87xxx->dev, "addr_bytes [%d] or data_bytes [%d] unsupport",
972 			data[AWRW_HDR_ADDR_BYTES], data[AWRW_HDR_DATA_BYTES]);
973 		return -EINVAL;
974 	}
975 
976 	/* one data string Composition of Contains two bytes of symbol(0x)*/
977 	/* and two byte of hexadecimal data*/
978 	data_str_size = 2 + 2 * AWRW_DATA_BYTES;
979 	act_data_len = count - AWRW_HDR_LEN - 1;
980 
981 	/* There is a comma(,) or space between each piece of data */
982 	if (data[AWRW_HDR_WR_FLAG] == AWRW_FLAG_WRITE) {
983 		/*ag:0x00 0x01 0x01 0x01 0x01 0x00\x0a*/
984 		req_data_len = (data_str_size + 1) * packet->reg_num;
985 		if (req_data_len > act_data_len) {
986 			AW_DEV_LOGE(aw87xxx->dev, "data_len checkfailed,requeset data_len [%d],actaul data_len [%d]",
987 				req_data_len, act_data_len);
988 			return -EINVAL;
989 		}
990 	}
991 
992 	return 0;
993 }
994 
995 /* flag addr_bytes data_bytes reg_num reg_addr*/
aw87xxx_awrw_parse_buf(struct aw87xxx * aw87xxx,const char * buf,size_t count,int * wr_status)996 static int aw87xxx_awrw_parse_buf(struct aw87xxx *aw87xxx,
997 			const char *buf, size_t count, int *wr_status)
998 {
999 	int data[AWRW_HDR_MAX] = {0};
1000 	struct aw_i2c_packet *packet = &aw87xxx->i2c_packet;
1001 	int ret = -1;
1002 
1003 	if (sscanf(buf, "0x%02x 0x%02x 0x%02x 0x%02x 0x%02x",
1004 		&data[AWRW_HDR_WR_FLAG], &data[AWRW_HDR_ADDR_BYTES],
1005 		&data[AWRW_HDR_DATA_BYTES], &data[AWRW_HDR_REG_NUM],
1006 		&data[AWRW_HDR_REG_ADDR]) == 5) {
1007 
1008 		packet->reg_addr = data[AWRW_HDR_REG_ADDR];
1009 		packet->reg_num = data[AWRW_HDR_REG_NUM];
1010 		*wr_status = data[AWRW_HDR_WR_FLAG];
1011 		ret = aw87xxx_awrw_data_check(aw87xxx, data, count);
1012 		if (ret < 0)
1013 			return ret;
1014 
1015 		return 0;
1016 	}
1017 
1018 	return -EINVAL;
1019 }
1020 
aw87xxx_attr_awrw_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1021 static ssize_t aw87xxx_attr_awrw_store(struct device *dev,
1022 	struct device_attribute *attr, const char *buf, size_t count)
1023 {
1024 	struct aw87xxx *aw87xxx = dev_get_drvdata(dev);
1025 	struct aw_i2c_packet *packet = &aw87xxx->i2c_packet;
1026 	int wr_status = 0;
1027 	int ret = -1;
1028 
1029 	if (count < AWRW_HDR_LEN) {
1030 		AW_DEV_LOGE(aw87xxx->dev, "data count too smaller, please check write format");
1031 		AW_DEV_LOGE(aw87xxx->dev, "string %s,count=%ld",
1032 			buf, (u_long)count);
1033 		return -EINVAL;
1034 	}
1035 
1036 	AW_DEV_LOGI(aw87xxx->dev, "string:[%s],count=%ld", buf, (u_long)count);
1037 	ret = aw87xxx_awrw_parse_buf(aw87xxx, buf, count, &wr_status);
1038 	if (ret < 0) {
1039 		AW_DEV_LOGE(aw87xxx->dev, "can not parse string");
1040 		return ret;
1041 	}
1042 
1043 	if (wr_status == AWRW_FLAG_WRITE) {
1044 		ret = aw87xxx_awrw_write(aw87xxx, buf, count);
1045 		if (ret < 0)
1046 			return ret;
1047 	} else if (wr_status == AWRW_FLAG_READ) {
1048 		packet->status = AWRW_I2C_ST_READ;
1049 		AW_DEV_LOGI(aw87xxx->dev, "read_cmd:reg_addr[0x%02x], reg_num[%d]",
1050 			packet->reg_addr, packet->reg_num);
1051 	} else {
1052 		AW_DEV_LOGE(aw87xxx->dev, "please check str format, unsupport read_write_status: %d",
1053 			wr_status);
1054 		return -EINVAL;
1055 	}
1056 
1057 	return count;
1058 }
1059 
aw87xxx_attr_awrw_show(struct device * dev,struct device_attribute * attr,char * buf)1060 static ssize_t aw87xxx_attr_awrw_show(struct device *dev,
1061 	struct device_attribute *attr, char *buf)
1062 {
1063 	struct aw87xxx *aw87xxx = dev_get_drvdata(dev);
1064 	struct aw_i2c_packet *packet = &aw87xxx->i2c_packet;
1065 	int data_len = 0;
1066 	size_t len = 0;
1067 	int ret = -1, i = 0;
1068 	char *reg_data = NULL;
1069 
1070 	if (packet->status != AWRW_I2C_ST_READ) {
1071 		AW_DEV_LOGE(aw87xxx->dev, "please write read cmd first");
1072 		return -EINVAL;
1073 	}
1074 
1075 	data_len = AWRW_DATA_BYTES * packet->reg_num;
1076 	reg_data = (char *)vmalloc(data_len);
1077 	if (reg_data == NULL) {
1078 		AW_DEV_LOGE(aw87xxx->dev, "memory alloc failed");
1079 		ret = -EINVAL;
1080 		goto exit;
1081 	}
1082 
1083 	mutex_lock(&aw87xxx->reg_lock);
1084 	ret = aw_dev_i2c_read_msg(&aw87xxx->aw_dev, packet->reg_addr,
1085 				(char *)reg_data, data_len);
1086 	if (ret < 0) {
1087 		ret = -EFAULT;
1088 		mutex_unlock(&aw87xxx->reg_lock);
1089 		goto exit;
1090 	}
1091 	mutex_unlock(&aw87xxx->reg_lock);
1092 
1093 	AW_DEV_LOGI(aw87xxx->dev, "reg_addr 0x%02x, reg_num %d",
1094 		packet->reg_addr, packet->reg_num);
1095 
1096 	for (i = 0; i < data_len; i++) {
1097 		len += snprintf(buf + len, PAGE_SIZE - len,
1098 			"0x%02x,", reg_data[i]);
1099 		AW_DEV_LOGI(aw87xxx->dev, "0x%02x", reg_data[i]);
1100 	}
1101 
1102 	ret = len;
1103 
1104 exit:
1105 	if (reg_data) {
1106 		vfree(reg_data);
1107 		reg_data = NULL;
1108 	}
1109 	packet->status = AWRW_I2C_ST_NONE;
1110 	return ret;
1111 }
1112 
aw87xxx_drv_ver_show(struct device * dev,struct device_attribute * attr,char * buf)1113 static ssize_t aw87xxx_drv_ver_show(struct device *dev,
1114 	struct device_attribute *attr, char *buf)
1115 {
1116 	ssize_t len = 0;
1117 
1118 	len += snprintf(buf + len, PAGE_SIZE - len,
1119 		"driver_ver: %s \n", AW87XXX_DRIVER_VERSION);
1120 
1121 	return len;
1122 }
1123 
1124 static DEVICE_ATTR(reg, S_IWUSR | S_IRUGO,
1125 		aw87xxx_attr_get_reg, aw87xxx_attr_set_reg);
1126 static DEVICE_ATTR(profile, S_IWUSR | S_IRUGO,
1127 		aw87xxx_attr_get_profile, aw87xxx_attr_set_profile);
1128 static DEVICE_ATTR(hwen, S_IWUSR | S_IRUGO,
1129 		aw87xxx_attr_get_hwen, aw87xxx_attr_set_hwen);
1130 static DEVICE_ATTR(awrw, S_IWUSR | S_IRUGO,
1131 	aw87xxx_attr_awrw_show, aw87xxx_attr_awrw_store);
1132 static DEVICE_ATTR(drv_ver, S_IRUGO, aw87xxx_drv_ver_show, NULL);
1133 
1134 static struct attribute *aw87xxx_attributes[] = {
1135 	&dev_attr_reg.attr,
1136 	&dev_attr_profile.attr,
1137 	&dev_attr_hwen.attr,
1138 	&dev_attr_awrw.attr,
1139 	&dev_attr_drv_ver.attr,
1140 	NULL
1141 };
1142 
1143 static struct attribute_group aw87xxx_attribute_group = {
1144 	.attrs = aw87xxx_attributes
1145 };
1146 
1147 /****************************************************************************
1148  *
1149  *aw87xxx device probe
1150  *
1151  ****************************************************************************/
1152 
aw87xxx_dtsi_dev_index_check(struct aw87xxx * cur_aw87xxx)1153 int aw87xxx_dtsi_dev_index_check(struct aw87xxx *cur_aw87xxx)
1154 {
1155 	struct list_head *pos = NULL;
1156 	struct aw87xxx *list_aw87xxx = NULL;
1157 
1158 	list_for_each(pos, &g_aw87xxx_list) {
1159 		list_aw87xxx = list_entry(pos, struct aw87xxx, list);
1160 		if (list_aw87xxx == NULL)
1161 			continue;
1162 
1163 		if (list_aw87xxx->dev_index == cur_aw87xxx->dev_index) {
1164 			AW_DEV_LOGE(cur_aw87xxx->dev, "dev_index has already existing,check failed");
1165 			return -EINVAL;
1166 		}
1167 	}
1168 
1169 	return 0;
1170 }
1171 
aw87xxx_dtsi_parse(struct aw87xxx * aw87xxx,struct device_node * dev_node)1172 static int aw87xxx_dtsi_parse(struct aw87xxx *aw87xxx,
1173 				struct device_node *dev_node)
1174 {
1175 	int ret = -1;
1176 	int32_t dev_index = -EINVAL;
1177 
1178 	ret = of_property_read_u32(dev_node, "dev_index", &dev_index);
1179 	if (ret < 0) {
1180 		AW_DEV_LOGI(aw87xxx->dev, "dev_index parse failed, user default[%d], ret=%d",
1181 				g_aw87xxx_dev_cnt, ret);
1182 		aw87xxx->dev_index = g_aw87xxx_dev_cnt;
1183 	} else {
1184 		aw87xxx->dev_index = dev_index;
1185 		AW_DEV_LOGI(aw87xxx->dev, "parse dev_index=[%d]",
1186 				aw87xxx->dev_index);
1187 	}
1188 
1189 	ret = aw87xxx_dtsi_dev_index_check(aw87xxx);
1190 	if (ret < 0)
1191 		return ret;
1192 
1193 	ret = of_get_named_gpio(dev_node, "reset-gpio", 0);
1194 	if (ret < 0) {
1195 		AW_DEV_LOGI(aw87xxx->dev, "no reset gpio provided, hardware reset unavailable");
1196 		aw87xxx->aw_dev.rst_gpio = AW_NO_RESET_GPIO;
1197 		aw87xxx->aw_dev.hwen_status = AW_DEV_HWEN_INVALID;
1198 	} else {
1199 		aw87xxx->aw_dev.rst_gpio = ret;
1200 		aw87xxx->aw_dev.hwen_status = AW_DEV_HWEN_OFF;
1201 		AW_DEV_LOGI(aw87xxx->dev, "reset gpio[%d] parse succeed", ret);
1202 		if (gpio_is_valid(aw87xxx->aw_dev.rst_gpio)) {
1203 			ret = devm_gpio_request_one(aw87xxx->dev,
1204 					aw87xxx->aw_dev.rst_gpio,
1205 					GPIOF_OUT_INIT_LOW, "aw87xxx_reset");
1206 			if (ret < 0) {
1207 				AW_DEV_LOGE(aw87xxx->dev, "reset request failed");
1208 				return ret;
1209 			}
1210 		}
1211 		return 0;
1212 	}
1213 
1214 	/* In order to compatible with stereo share only one reset gpio */
1215 	ret = of_get_named_gpio(dev_node, "reset-shared-gpio", 0);
1216 	if (ret < 0) {
1217 		AW_DEV_LOGI(aw87xxx->dev, "no reset shared gpio provided, hardware reset unavailable");
1218 		aw87xxx->aw_dev.rst_shared_gpio = AW_NO_RESET_GPIO;
1219 	} else {
1220 		aw87xxx->aw_dev.rst_shared_gpio = ret;
1221 		AW_DEV_LOGI(aw87xxx->dev, "reset shared gpio[%d] parse succeed", ret);
1222 		if (gpio_is_valid(aw87xxx->aw_dev.rst_shared_gpio)) {
1223 			ret = devm_gpio_request_one(aw87xxx->dev,
1224 					aw87xxx->aw_dev.rst_shared_gpio,
1225 					GPIOF_OUT_INIT_LOW, "aw87xxx_reset-shared");
1226 			if (ret < 0) {
1227 				AW_DEV_LOGE(aw87xxx->dev, "aw87xxx_reset-shared reset request failed");
1228 				return ret;
1229 			}
1230 			msleep(20);
1231 			gpio_set_value_cansleep(aw87xxx->aw_dev.rst_shared_gpio, AW_GPIO_HIGHT_LEVEL);
1232 		}
1233 	}
1234 	return 0;
1235 }
1236 
aw87xxx_malloc_init(struct i2c_client * client)1237 static struct aw87xxx *aw87xxx_malloc_init(struct i2c_client *client)
1238 {
1239 	struct aw87xxx *aw87xxx = NULL;
1240 
1241 	aw87xxx = devm_kzalloc(&client->dev, sizeof(struct aw87xxx),
1242 			GFP_KERNEL);
1243 	if (aw87xxx == NULL) {
1244 		AW_DEV_LOGE(&client->dev, "failed to devm_kzalloc aw87xxx");
1245 		return NULL;
1246 	}
1247 	memset(aw87xxx, 0, sizeof(struct aw87xxx));
1248 
1249 	aw87xxx->dev = &client->dev;
1250 	aw87xxx->aw_dev.dev = &client->dev;
1251 	aw87xxx->aw_dev.i2c_bus = client->adapter->nr;
1252 	aw87xxx->aw_dev.i2c_addr = client->addr;
1253 	aw87xxx->aw_dev.i2c = client;
1254 	aw87xxx->aw_dev.hwen_status = false;
1255 	aw87xxx->aw_dev.reg_access = NULL;
1256 	aw87xxx->aw_dev.hwen_status = AW_DEV_HWEN_INVALID;
1257 	aw87xxx->off_bin_status = AW87XXX_NO_OFF_BIN;
1258 	aw87xxx->codec = NULL;
1259 	aw87xxx->current_profile = aw87xxx->prof_off_name;
1260 
1261 	mutex_init(&aw87xxx->reg_lock);
1262 
1263 	AW_DEV_LOGI(&client->dev, "struct aw87xxx devm_kzalloc and init down");
1264 	return aw87xxx;
1265 }
1266 
aw87xxx_probe(struct snd_soc_component * component)1267 static int aw87xxx_probe(struct snd_soc_component *component)
1268 {
1269 	struct aw87xxx *aw87xxx = dev_get_drvdata(component->dev);
1270 	int ret;
1271 
1272 	ret = aw87xxx_kcontrol_dynamic_create(aw87xxx, component);
1273 	if (ret < 0) {
1274 		dev_err(component->dev, "%s: aw87xxx_add_codec_controls failed, ret= %d\n",
1275 			__func__, ret);
1276 		return ret;
1277 	};
1278 	return 0;
1279 }
1280 
1281 static const struct snd_soc_component_driver aw87xxx_component_driver = {
1282 	.probe = aw87xxx_probe,
1283 };
1284 
aw87xxx_i2c_probe(struct i2c_client * client,const struct i2c_device_id * id)1285 static int aw87xxx_i2c_probe(struct i2c_client *client,
1286 				const struct i2c_device_id *id)
1287 {
1288 	struct device_node *dev_node = client->dev.of_node;
1289 	struct aw87xxx *aw87xxx = NULL;
1290 	int ret = -1;
1291 
1292 	dev_info(&client->dev, "%s\n", __func__);
1293 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1294 		AW_DEV_LOGE(&client->dev, "check_functionality failed");
1295 		return -ENODEV;
1296 	}
1297 
1298 	/* aw87xxx i2c_dev struct init */
1299 	aw87xxx = aw87xxx_malloc_init(client);
1300 	if (aw87xxx == NULL)
1301 		return -ENOMEM;
1302 
1303 	i2c_set_clientdata(client, aw87xxx);
1304 
1305 	/* aw87xxx dev_node parse */
1306 	ret = aw87xxx_dtsi_parse(aw87xxx, dev_node);
1307 	if (ret < 0)
1308 		goto exit;
1309 
1310 	/*hw power on PA*/
1311 	aw_dev_hw_pwr_ctrl(&aw87xxx->aw_dev, true);
1312 
1313 	/* aw87xxx devices private attributes init */
1314 	ret = aw_dev_init(&aw87xxx->aw_dev);
1315 	if (ret < 0)
1316 		goto exit;
1317 
1318 	ret = devm_snd_soc_register_component(aw87xxx->dev, &aw87xxx_component_driver,
1319 					      NULL, 0);
1320 	if (ret < 0) {
1321 		dev_err(aw87xxx->dev, "%s() register codec error %d\n",
1322 			__func__, ret);
1323 		goto exit;
1324 	}
1325 
1326 	/*product register reset */
1327 	aw_dev_soft_reset(&aw87xxx->aw_dev);
1328 
1329 	/*hw power off */
1330 	aw_dev_hw_pwr_ctrl(&aw87xxx->aw_dev, false);
1331 
1332 	/* create debug attrbute nodes */
1333 	ret = sysfs_create_group(&aw87xxx->dev->kobj, &aw87xxx_attribute_group);
1334 	if (ret < 0)
1335 		AW_DEV_LOGE(aw87xxx->dev, "failed to create sysfs nodes, will not allowed to use");
1336 
1337 	/* cfg_load init */
1338 	aw87xxx_fw_load_init(aw87xxx);
1339 
1340 	/*monitor init*/
1341 	aw_monitor_init(aw87xxx->dev, &aw87xxx->monitor, dev_node);
1342 
1343 	/*add device to total list */
1344 	mutex_lock(&g_aw87xxx_mutex_lock);
1345 	g_aw87xxx_dev_cnt++;
1346 	list_add(&aw87xxx->list, &g_aw87xxx_list);
1347 	mutex_unlock(&g_aw87xxx_mutex_lock);
1348 
1349 	AW_DEV_LOGI(aw87xxx->dev, "succeed");
1350 
1351 	return 0;
1352 exit:
1353 	AW_DEV_LOGE(aw87xxx->dev, "pa init failed");
1354 	return ret;
1355 }
1356 
aw87xxx_i2c_remove(struct i2c_client * client)1357 static int aw87xxx_i2c_remove(struct i2c_client *client)
1358 {
1359 	struct aw87xxx *aw87xxx = i2c_get_clientdata(client);
1360 
1361 	aw_monitor_exit(&aw87xxx->monitor);
1362 
1363 	/*rm attr node*/
1364 	sysfs_remove_group(&aw87xxx->dev->kobj, &aw87xxx_attribute_group);
1365 
1366 	aw87xxx_fw_cfg_free(aw87xxx);
1367 
1368 	mutex_lock(&g_aw87xxx_mutex_lock);
1369 	g_aw87xxx_dev_cnt--;
1370 	list_del(&aw87xxx->list);
1371 	mutex_unlock(&g_aw87xxx_mutex_lock);
1372 
1373 	return 0;
1374 }
1375 
aw87xxx_i2c_shutdown(struct i2c_client * client)1376 static void aw87xxx_i2c_shutdown(struct i2c_client *client)
1377 {
1378 	struct aw87xxx *aw87xxx = i2c_get_clientdata(client);
1379 
1380 	AW_DEV_LOGI(&client->dev, "enter");
1381 
1382 	/*soft and hw power off*/
1383 	aw87xxx_update_profile(aw87xxx, aw87xxx->prof_off_name);
1384 }
1385 
1386 
1387 static const struct i2c_device_id aw87xxx_i2c_id[] = {
1388 	{AW87XXX_I2C_NAME, 0},
1389 	{},
1390 };
1391 
1392 static const struct of_device_id extpa_of_match[] = {
1393 	{.compatible = "awinic,aw87xxx_pa"},
1394 	{},
1395 };
1396 
1397 static struct i2c_driver aw87xxx_i2c_driver = {
1398 	.driver = {
1399 		.owner = THIS_MODULE,
1400 		.name = AW87XXX_I2C_NAME,
1401 		.of_match_table = extpa_of_match,
1402 		},
1403 	.probe = aw87xxx_i2c_probe,
1404 	.remove = aw87xxx_i2c_remove,
1405 	.shutdown = aw87xxx_i2c_shutdown,
1406 	.id_table = aw87xxx_i2c_id,
1407 };
1408 
aw87xxx_pa_init(void)1409 static int __init aw87xxx_pa_init(void)
1410 {
1411 	int ret;
1412 
1413 	AW_LOGI("driver version: %s", AW87XXX_DRIVER_VERSION);
1414 
1415 	ret = i2c_add_driver(&aw87xxx_i2c_driver);
1416 	if (ret < 0) {
1417 		AW_LOGE("Unable to register driver, ret= %d", ret);
1418 		return ret;
1419 	}
1420 	return 0;
1421 }
1422 
aw87xxx_pa_exit(void)1423 static void __exit aw87xxx_pa_exit(void)
1424 {
1425 	AW_LOGI("enter");
1426 	i2c_del_driver(&aw87xxx_i2c_driver);
1427 }
1428 
1429 module_init(aw87xxx_pa_init);
1430 module_exit(aw87xxx_pa_exit);
1431 
1432 MODULE_AUTHOR("<zhaozhongbo@awinic.com>");
1433 MODULE_DESCRIPTION("awinic aw87xxx pa driver");
1434 MODULE_LICENSE("GPL v2");
1435