1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * aw883xx.c aw883xx codec module
4 *
5 * Copyright (c) 2020 AWINIC Technology CO., LTD
6 *
7 * Author: Bruce zhao <zhaolei@awinic.com>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 */
14
15 #include <linux/module.h>
16 #include <linux/i2c.h>
17 #include <sound/core.h>
18 #include <sound/pcm.h>
19 #include <sound/pcm_params.h>
20 #include <sound/soc.h>
21 #include <linux/of_gpio.h>
22 #include <linux/delay.h>
23 #include <linux/device.h>
24 #include <linux/firmware.h>
25 #include <linux/i2c.h>
26 #include <linux/debugfs.h>
27 #include <linux/version.h>
28 #include <linux/input.h>
29 #include <linux/timer.h>
30 #include <linux/workqueue.h>
31 #include <linux/hrtimer.h>
32 #include <linux/syscalls.h>
33 #include <linux/vmalloc.h>
34 #include <sound/tlv.h>
35 #include <linux/uaccess.h>
36 #include "aw_pid_2049_reg.h"
37 #include "aw883xx.h"
38 #include "aw_bin_parse.h"
39 #include "aw_device.h"
40 #include "aw_log.h"
41 #include "aw_spin.h"
42
43 /******************************************************
44 *
45 * Marco
46 *
47 ******************************************************/
48 #define AW883XX_I2C_NAME "aw883xx_smartpa"
49
50 #define AW883XX_DRIVER_VERSION "v1.2.0"
51
52 #define AW883XX_RATES (SNDRV_PCM_RATE_8000_48000 | \
53 SNDRV_PCM_RATE_96000)
54 #define AW883XX_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
55 SNDRV_PCM_FMTBIT_S24_LE | \
56 SNDRV_PCM_FMTBIT_S32_LE)
57
58
59 #define AW883XX_ACF_FILE "aw883xx_acf.bin"
60 #define AW_REQUEST_FW_RETRIES 5 /* 5 times */
61
62 static unsigned int g_aw883xx_dev_cnt;
63 static DEFINE_MUTEX(g_aw883xx_lock);
64 static struct aw_container *g_awinic_cfg;
65
66 static const char *const aw883xx_switch[] = {"Disable", "Enable"};
67
aw883xx_platform_init(struct aw883xx * aw883xx)68 static int aw883xx_platform_init(struct aw883xx *aw883xx)
69 {
70 #ifdef AW_QCOM_PLATFORM
71 aw883xx->aw_pa->platform = AW_QCOM;
72 return 0;
73 #elif defined AW_MTK_PLATFORM
74 aw883xx->aw_pa->platform = AW_MTK;
75 return 0;
76 #elif defined AW_SPRD_PLATFORM
77 aw883xx->aw_pa->platform = AW_SPRD;
78 return 0;
79 #else
80 return -EINVAL;
81 #endif
82 }
83
aw883xx_get_version(char * buf,int size)84 int aw883xx_get_version(char *buf, int size)
85 {
86 if (size > strlen(AW883XX_DRIVER_VERSION)) {
87 memcpy(buf, AW883XX_DRIVER_VERSION, strlen(AW883XX_DRIVER_VERSION));
88 return strlen(AW883XX_DRIVER_VERSION);
89 } else {
90 return -ENOMEM;
91 }
92 }
93
94 /******************************************************
95 *
96 * aw883xx append suffix sound channel information
97 *
98 ******************************************************/
aw883xx_devm_kstrdup(struct device * dev,char * buf)99 static void *aw883xx_devm_kstrdup(struct device *dev, char *buf)
100 {
101 char *str = NULL;
102
103 str = devm_kzalloc(dev, strlen(buf) + 1, GFP_KERNEL);
104 if (!str) {
105 aw_pr_err("devm_kzalloc %s failed", buf);
106 return str;
107 }
108 memcpy(str, buf, strlen(buf));
109 return str;
110 }
111
aw883xx_append_i2c_suffix(const char * format,const char ** change_name,struct aw883xx * aw883xx)112 static int aw883xx_append_i2c_suffix(const char *format,
113 const char **change_name, struct aw883xx *aw883xx)
114 {
115 char buf[64] = { 0 };
116 int i2cbus = aw883xx->i2c->adapter->nr;
117 int addr = aw883xx->i2c->addr;
118
119 snprintf(buf, sizeof(buf), format, *change_name, i2cbus, addr);
120 *change_name = aw883xx_devm_kstrdup(aw883xx->dev, buf);
121 if (!(*change_name))
122 return -ENOMEM;
123
124 aw_dev_info(aw883xx->dev, "change name :%s", *change_name);
125 return 0;
126 }
127
128
129 /******************************************************
130 *
131 * aw883xx distinguish between codecs and components by version
132 *
133 ******************************************************/
134 #ifdef AW_KERNEL_VER_OVER_4_19_1
135 static struct aw_componet_codec_ops aw_componet_codec_ops = {
136 .kcontrol_codec = snd_soc_kcontrol_component,
137 .codec_get_drvdata = snd_soc_component_get_drvdata,
138 .add_codec_controls = snd_soc_add_component_controls,
139 .unregister_codec = snd_soc_unregister_component,
140 .register_codec = snd_soc_register_component,
141 };
142 #else
143 static struct aw_componet_codec_ops aw_componet_codec_ops = {
144 .kcontrol_codec = snd_soc_kcontrol_codec,
145 .codec_get_drvdata = snd_soc_codec_get_drvdata,
146 .add_codec_controls = snd_soc_add_codec_controls,
147 .unregister_codec = snd_soc_unregister_codec,
148 .register_codec = snd_soc_register_codec,
149 };
150 #endif
151
aw883xx_get_codec(struct snd_soc_dai * dai)152 static aw_snd_soc_codec_t *aw883xx_get_codec(struct snd_soc_dai *dai)
153 {
154 #ifdef AW_KERNEL_VER_OVER_4_19_1
155 return dai->component;
156 #else
157 return dai->codec;
158 #endif
159 }
160
161 /******************************************************
162 *
163 * aw883xx reg write/read
164 *
165 ******************************************************/
166
aw883xx_i2c_writes(struct aw883xx * aw883xx,uint8_t reg_addr,uint8_t * buf,uint16_t len)167 int aw883xx_i2c_writes(struct aw883xx *aw883xx,
168 uint8_t reg_addr, uint8_t *buf, uint16_t len)
169 {
170 int ret = -1;
171 uint8_t *data = NULL;
172
173 data = kmalloc(len + 1, GFP_KERNEL);
174 if (data == NULL) {
175 aw_dev_err(aw883xx->dev, "can not allocate memory");
176 return -ENOMEM;
177 }
178
179 data[0] = reg_addr;
180 memcpy(&data[1], buf, len);
181
182 ret = i2c_master_send(aw883xx->i2c, data, len + 1);
183 if (ret < 0) {
184 aw_dev_err(aw883xx->dev, "i2c master send error");
185 goto i2c_master_error;
186 } else if (ret != (len + 1)) {
187 aw_dev_err(aw883xx->dev, "i2c master send error(size error)");
188 ret = -ENXIO;
189 goto i2c_master_error;
190 }
191
192 kfree(data);
193 data = NULL;
194 return 0;
195
196 i2c_master_error:
197 kfree(data);
198 data = NULL;
199 return ret;
200 }
201
aw883xx_i2c_reads(struct aw883xx * aw883xx,uint8_t reg_addr,uint8_t * data_buf,uint16_t data_len)202 static int aw883xx_i2c_reads(struct aw883xx *aw883xx,
203 uint8_t reg_addr, uint8_t *data_buf,
204 uint16_t data_len)
205 {
206 int ret;
207 struct i2c_msg msg[] = {
208 [0] = {
209 .addr = aw883xx->i2c->addr,
210 .flags = 0,
211 .len = sizeof(uint8_t),
212 .buf = ®_addr,
213 },
214 [1] = {
215 .addr = aw883xx->i2c->addr,
216 .flags = I2C_M_RD,
217 .len = data_len,
218 .buf = data_buf,
219 },
220 };
221
222 ret = i2c_transfer(aw883xx->i2c->adapter, msg, ARRAY_SIZE(msg));
223 if (ret < 0) {
224 aw_dev_err(aw883xx->dev, "i2c_transfer failed");
225 return ret;
226 } else if (ret != AW883XX_READ_MSG_NUM) {
227 aw_dev_err(aw883xx->dev, "transfer failed(size error)");
228 return -ENXIO;
229 }
230
231 return 0;
232 }
233
aw883xx_i2c_write(struct aw883xx * aw883xx,uint8_t reg_addr,uint16_t reg_data)234 int aw883xx_i2c_write(struct aw883xx *aw883xx,
235 uint8_t reg_addr, uint16_t reg_data)
236 {
237 int ret = -1;
238 uint8_t cnt = 0;
239 uint8_t buf[2] = {0};
240
241 buf[0] = (reg_data & 0xff00) >> 8;
242 buf[1] = (reg_data & 0x00ff) >> 0;
243
244 while (cnt < AW_I2C_RETRIES) {
245 ret = aw883xx_i2c_writes(aw883xx, reg_addr, buf, 2);
246 if (ret < 0)
247 aw_dev_err(aw883xx->dev, "i2c_write cnt=%d error=%d",
248 cnt, ret);
249 else
250 break;
251 cnt++;
252 }
253
254 if (aw883xx->i2c_log_en)
255 aw_dev_info(aw883xx->dev, "write: reg = 0x%02x, val = 0x%04x",
256 reg_addr, reg_data);
257
258 return ret;
259 }
260
aw883xx_i2c_read(struct aw883xx * aw883xx,uint8_t reg_addr,uint16_t * reg_data)261 int aw883xx_i2c_read(struct aw883xx *aw883xx,
262 uint8_t reg_addr, uint16_t *reg_data)
263 {
264 int ret = -1;
265 uint8_t cnt = 0;
266 uint8_t buf[2] = {0};
267
268 while (cnt < AW_I2C_RETRIES) {
269 ret = aw883xx_i2c_reads(aw883xx, reg_addr, buf, 2);
270 if (ret < 0) {
271 aw_dev_err(aw883xx->dev, "i2c_read cnt=%d error=%d",
272 cnt, ret);
273 } else {
274 *reg_data = (buf[0] << 8) | (buf[1] << 0);
275 break;
276 }
277 cnt++;
278 }
279
280 if (aw883xx->i2c_log_en)
281 aw_dev_info(aw883xx->dev, "read: reg = 0x%02x, val = 0x%04x",
282 reg_addr, *reg_data);
283
284 return ret;
285 }
286
aw883xx_i2c_write_bits(struct aw883xx * aw883xx,uint8_t reg_addr,uint16_t mask,uint16_t reg_data)287 static int aw883xx_i2c_write_bits(struct aw883xx *aw883xx,
288 uint8_t reg_addr, uint16_t mask,
289 uint16_t reg_data)
290 {
291 int ret = -1;
292 uint16_t reg_val = 0;
293
294 ret = aw883xx_i2c_read(aw883xx, reg_addr, ®_val);
295 if (ret < 0) {
296 aw_dev_err(aw883xx->dev, "i2c read error, ret=%d", ret);
297 return ret;
298 }
299 reg_val &= mask;
300 reg_val |= reg_data;
301 ret = aw883xx_i2c_write(aw883xx, reg_addr, reg_val);
302 if (ret < 0) {
303 aw_dev_err(aw883xx->dev, "i2c read error, ret=%d", ret);
304 return ret;
305 }
306
307 return 0;
308 }
309
aw883xx_reg_write(struct aw883xx * aw883xx,uint8_t reg_addr,uint16_t reg_data)310 int aw883xx_reg_write(struct aw883xx *aw883xx,
311 uint8_t reg_addr, uint16_t reg_data)
312 {
313 int ret = -1;
314
315 mutex_lock(&aw883xx->i2c_lock);
316 ret = aw883xx_i2c_write(aw883xx, reg_addr, reg_data);
317 if (ret < 0)
318 aw_dev_err(aw883xx->dev,
319 "write fail, reg = 0x%02x, val = 0x%04x, ret=%d",
320 reg_addr, reg_data, ret);
321 mutex_unlock(&aw883xx->i2c_lock);
322
323 return ret;
324 }
325
aw883xx_reg_read(struct aw883xx * aw883xx,uint8_t reg_addr,uint16_t * reg_data)326 int aw883xx_reg_read(struct aw883xx *aw883xx,
327 uint8_t reg_addr, uint16_t *reg_data)
328 {
329 int ret = -1;
330
331 mutex_lock(&aw883xx->i2c_lock);
332 ret = aw883xx_i2c_read(aw883xx, reg_addr, reg_data);
333 if (ret < 0)
334 aw_dev_err(aw883xx->dev,
335 "read fail: reg = 0x%02x, val = 0x%04x, ret=%d",
336 reg_addr, *reg_data, ret);
337 mutex_unlock(&aw883xx->i2c_lock);
338
339 return ret;
340 }
341
aw883xx_reg_write_bits(struct aw883xx * aw883xx,uint8_t reg_addr,uint16_t mask,uint16_t reg_data)342 int aw883xx_reg_write_bits(struct aw883xx *aw883xx,
343 uint8_t reg_addr, uint16_t mask, uint16_t reg_data)
344 {
345 int ret = -1;
346
347 mutex_lock(&aw883xx->i2c_lock);
348 ret = aw883xx_i2c_write_bits(aw883xx, reg_addr, mask, reg_data);
349 if (ret < 0)
350 aw_dev_err(aw883xx->dev,"fail, ret=%d", ret);
351 mutex_unlock(&aw883xx->i2c_lock);
352
353 return ret;
354 }
355
aw883xx_dsp_write_16bit(struct aw883xx * aw883xx,uint16_t dsp_addr,uint32_t dsp_data)356 static int aw883xx_dsp_write_16bit(struct aw883xx *aw883xx,
357 uint16_t dsp_addr, uint32_t dsp_data)
358 {
359 int ret;
360 struct aw_dsp_mem_desc *desc = &aw883xx->aw_pa->dsp_mem_desc;
361
362 ret = aw883xx_i2c_write(aw883xx, desc->dsp_madd_reg, dsp_addr);
363 if (ret < 0) {
364 aw_dev_err(aw883xx->dev, "i2c write error, ret=%d", ret);
365 return ret;
366 }
367
368 ret = aw883xx_i2c_write(aw883xx, desc->dsp_mdat_reg, (uint16_t)dsp_data);
369 if (ret < 0) {
370 aw_dev_err(aw883xx->dev, "i2c write error, ret=%d", ret);
371 return ret;
372 }
373
374 return 0;
375 }
376
aw883xx_dsp_write_32bit(struct aw883xx * aw883xx,uint16_t dsp_addr,uint32_t dsp_data)377 static int aw883xx_dsp_write_32bit(struct aw883xx *aw883xx,
378 uint16_t dsp_addr, uint32_t dsp_data)
379 {
380 int ret;
381 uint16_t temp_data = 0;
382 struct aw_dsp_mem_desc *desc = &aw883xx->aw_pa->dsp_mem_desc;
383
384 ret = aw883xx_i2c_write(aw883xx, desc->dsp_madd_reg, dsp_addr);
385 if (ret < 0) {
386 aw_dev_err(aw883xx->dev, "i2c write error, ret=%d", ret);
387 return ret;
388 }
389
390 temp_data = dsp_data & AW883XX_DSP_16_DATA_MASK;
391 ret = aw883xx_i2c_write(aw883xx, desc->dsp_mdat_reg, temp_data);
392 if (ret < 0) {
393 aw_dev_err(aw883xx->dev, "i2c write error, ret=%d", ret);
394 return ret;
395 }
396
397 temp_data = dsp_data >> 16;
398 ret = aw883xx_i2c_write(aw883xx, desc->dsp_mdat_reg, temp_data);
399 if (ret < 0) {
400 aw_dev_err(aw883xx->dev, "i2c write error, ret=%d", ret);
401 return ret;
402 }
403
404 return 0;
405 }
406
407 /******************************************************
408 * aw883xx clear dsp chip select state
409 ******************************************************/
aw883xx_clear_dsp_sel_st(struct aw883xx * aw883xx)410 static void aw883xx_clear_dsp_sel_st(struct aw883xx *aw883xx)
411 {
412 uint16_t reg_value;
413 uint8_t reg = aw883xx->aw_pa->soft_rst.reg;
414
415 aw883xx_i2c_read(aw883xx, reg, ®_value);
416 }
417
aw883xx_dsp_write(struct aw883xx * aw883xx,uint16_t dsp_addr,uint32_t dsp_data,uint8_t data_type)418 int aw883xx_dsp_write(struct aw883xx *aw883xx,
419 uint16_t dsp_addr, uint32_t dsp_data, uint8_t data_type)
420 {
421 int ret = -1;
422
423 mutex_lock(&aw883xx->i2c_lock);
424 if (data_type == AW_DSP_16_DATA) {
425 ret = aw883xx_dsp_write_16bit(aw883xx, dsp_addr, dsp_data);
426 if (ret < 0) {
427 aw_dev_err(aw883xx->dev, "write dsp_addr[0x%04x] 16 bit dsp_data[%04x] failed",
428 (uint32_t)dsp_addr, dsp_data);
429 goto exit;
430 }
431 } else if (data_type == AW_DSP_32_DATA) {
432 ret = aw883xx_dsp_write_32bit(aw883xx, dsp_addr, dsp_data);
433 if (ret < 0) {
434 aw_dev_err(aw883xx->dev, "write dsp_addr[0x%04x] 32 bit dsp_data[%08x] failed",
435 (uint32_t)dsp_addr, dsp_data);
436 goto exit;
437 }
438 } else {
439 aw_dev_err(aw883xx->dev, "data type[%d] unsupported", data_type);
440 ret = -EINVAL;
441 goto exit;
442 }
443
444 exit:
445 aw883xx_clear_dsp_sel_st(aw883xx);
446 mutex_unlock(&aw883xx->i2c_lock);
447 return ret;
448 }
449
450
aw883xx_dsp_read_16bit(struct aw883xx * aw883xx,uint16_t dsp_addr,uint32_t * dsp_data)451 static int aw883xx_dsp_read_16bit(struct aw883xx *aw883xx,
452 uint16_t dsp_addr, uint32_t *dsp_data)
453 {
454 int ret;
455 uint16_t temp_data = 0;
456 struct aw_dsp_mem_desc *desc = &aw883xx->aw_pa->dsp_mem_desc;
457
458 ret = aw883xx_i2c_write(aw883xx, desc->dsp_madd_reg, dsp_addr);
459 if (ret < 0) {
460 aw_dev_err(aw883xx->dev, "i2c write error, ret=%d", ret);
461 return ret;
462 }
463
464 ret = aw883xx_i2c_read(aw883xx, desc->dsp_mdat_reg, &temp_data);
465 if (ret < 0) {
466 aw_dev_err(aw883xx->dev, "i2c write error, ret=%d", ret);
467 return ret;
468 }
469
470 *dsp_data = temp_data;
471
472 return 0;
473 }
474
aw883xx_dsp_read_32bit(struct aw883xx * aw883xx,uint16_t dsp_addr,uint32_t * dsp_data)475 static int aw883xx_dsp_read_32bit(struct aw883xx *aw883xx,
476 uint16_t dsp_addr, uint32_t *dsp_data)
477 {
478 int ret;
479 uint16_t temp_data = 0;
480 struct aw_dsp_mem_desc *desc = &aw883xx->aw_pa->dsp_mem_desc;
481
482 /*write dsp addr*/
483 ret = aw883xx_i2c_write(aw883xx, desc->dsp_madd_reg, dsp_addr);
484 if (ret < 0) {
485 aw_dev_err(aw883xx->dev, "i2c write error, ret=%d", ret);
486 return ret;
487 }
488
489 /*get Low 16 bit data*/
490 ret = aw883xx_i2c_read(aw883xx, desc->dsp_mdat_reg, &temp_data);
491 if (ret < 0) {
492 aw_dev_err(aw883xx->dev, "i2c read error, ret=%d", ret);
493 return ret;
494 }
495
496 *dsp_data = temp_data;
497
498 /*get high 16 bit data*/
499 ret = aw883xx_i2c_read(aw883xx, desc->dsp_mdat_reg, &temp_data);
500 if (ret < 0) {
501 aw_dev_err(aw883xx->dev, "i2c read error, ret=%d", ret);
502 return ret;
503 }
504 *dsp_data |= (temp_data << 16);
505
506 return 0;
507 }
508
aw883xx_dsp_read(struct aw883xx * aw883xx,uint16_t dsp_addr,uint32_t * dsp_data,uint8_t data_type)509 int aw883xx_dsp_read(struct aw883xx *aw883xx,
510 uint16_t dsp_addr, uint32_t *dsp_data, uint8_t data_type)
511 {
512 int ret = -1;
513
514 mutex_lock(&aw883xx->i2c_lock);
515 if (data_type == AW_DSP_16_DATA) {
516 ret = aw883xx_dsp_read_16bit(aw883xx, dsp_addr, dsp_data);
517 if (ret < 0) {
518 aw_dev_err(aw883xx->dev, "read dsp_addr[0x%04x] 16 bit dsp_data[%04x] failed",
519 (uint32_t)dsp_addr, *dsp_data);
520 goto exit;
521 }
522 } else if (data_type == AW_DSP_32_DATA) {
523 ret = aw883xx_dsp_read_32bit(aw883xx, dsp_addr, dsp_data);
524 if (ret < 0) {
525 aw_dev_err(aw883xx->dev, "read dsp_addr[0x%04x] 32 bit dsp_data[%08x] failed",
526 (uint32_t)dsp_addr, *dsp_data);
527 goto exit;
528 }
529 } else {
530 aw_dev_err(aw883xx->dev, "data type[%d] unsupported", data_type);
531 ret = -EINVAL;
532 goto exit;
533 }
534
535 exit:
536 aw883xx_clear_dsp_sel_st(aw883xx);
537 mutex_unlock(&aw883xx->i2c_lock);
538 return ret;
539 }
540
541 /******************************************************
542 * aw883xx get dev num
543 ******************************************************/
aw883xx_get_dev_num(void)544 int aw883xx_get_dev_num(void)
545 {
546 return g_aw883xx_dev_cnt;
547 }
548
549 /******************************************************
550 * aw883xx interrupt
551 ******************************************************/
aw883xx_interrupt_work(struct work_struct * work)552 static void aw883xx_interrupt_work(struct work_struct *work)
553 {
554 struct aw883xx *aw883xx = container_of(work,
555 struct aw883xx, interrupt_work.work);
556 int16_t reg_value;
557 int ret;
558
559 aw_dev_info(aw883xx->dev, "enter");
560
561 /*read reg value*/
562 ret = aw_dev_get_int_status(aw883xx->aw_pa, ®_value);
563 if (ret < 0)
564 aw_dev_err(aw883xx->dev, "get init_reg value failed");
565 else
566 aw_dev_info(aw883xx->dev, "int value 0x%x", reg_value);
567
568 /*clear init reg*/
569 aw_dev_clear_int_status(aw883xx->aw_pa);
570
571 /*unmask interrupt*/
572 aw_dev_set_intmask(aw883xx->aw_pa, true);
573 }
574
575 /******************************************************
576 *
577 * Digital Audio Interface
578 *
579 ******************************************************/
aw883xx_startup(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)580 static int aw883xx_startup(struct snd_pcm_substream *substream,
581 struct snd_soc_dai *dai)
582 {
583 aw_snd_soc_codec_t *codec = aw883xx_get_codec(dai);
584 struct aw883xx *aw883xx =
585 aw_componet_codec_ops.codec_get_drvdata(codec);
586
587 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
588 aw_dev_info(aw883xx->dev, "playback enter");
589 /*load cali re*/
590 if (AW_ERRO_CALI_RE_VALUE == aw883xx->aw_pa->cali_desc.cali_re)
591 aw_cali_get_cali_re(&aw883xx->aw_pa->cali_desc);
592 } else {
593 aw_dev_info(aw883xx->dev, "capture enter");
594 }
595
596 return 0;
597 }
598
aw883xx_set_fmt(struct snd_soc_dai * dai,unsigned int fmt)599 static int aw883xx_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
600 {
601 aw_snd_soc_codec_t *codec = aw883xx_get_codec(dai);
602 /*struct aw883xx *aw883xx =
603 aw_componet_codec_ops.aw_snd_soc_codec_get_drvdata(codec); */
604
605 aw_dev_info(codec->dev, "fmt=0x%x", fmt);
606
607 /* supported mode: regular I2S, slave, or PDM */
608 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
609 case SND_SOC_DAIFMT_I2S:
610 if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) !=
611 SND_SOC_DAIFMT_CBS_CFS) {
612 aw_dev_err(codec->dev, "invalid codec master mode");
613 return -EINVAL;
614 }
615 break;
616 default:
617 aw_dev_err(codec->dev, "unsupported DAI format %d",
618 fmt & SND_SOC_DAIFMT_FORMAT_MASK);
619 return -EINVAL;
620 }
621
622 return 0;
623 }
624
aw883xx_set_dai_sysclk(struct snd_soc_dai * dai,int clk_id,unsigned int freq,int dir)625 static int aw883xx_set_dai_sysclk(struct snd_soc_dai *dai,
626 int clk_id, unsigned int freq, int dir)
627 {
628 aw_snd_soc_codec_t *codec = aw883xx_get_codec(dai);
629 struct aw883xx *aw883xx =
630 aw_componet_codec_ops.codec_get_drvdata(codec);
631
632 aw_dev_info(aw883xx->dev, "freq=%d", freq);
633
634 aw883xx->sysclk = freq;
635 return 0;
636 }
637
aw883xx_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)638 static int aw883xx_hw_params(struct snd_pcm_substream *substream,
639 struct snd_pcm_hw_params *params,
640 struct snd_soc_dai *dai)
641 {
642 aw_snd_soc_codec_t *codec = aw883xx_get_codec(dai);
643 struct aw883xx *aw883xx =
644 aw_componet_codec_ops.codec_get_drvdata(codec);
645
646 /* get CAPTURE rate param bit width*/
647 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
648 aw_dev_info(aw883xx->dev,
649 "STREAM_CAPTURE requested rate: %d, width = %d",
650 params_rate(params), params_width(params));
651 }
652
653 /* get PLAYBACK rate param bit width*/
654 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
655 aw_dev_info(aw883xx->dev,
656 "STREAM_PLAYBACK requested rate: %d, width = %d",
657 params_rate(params), params_width(params));
658 }
659
660 return 0;
661 }
662
aw883xx_start_pa(struct aw883xx * aw883xx)663 static void aw883xx_start_pa(struct aw883xx *aw883xx)
664 {
665 int ret, i;
666
667 aw_dev_info(aw883xx->dev, "enter");
668
669 if (aw883xx->allow_pw == false) {
670 aw_dev_info(aw883xx->dev, "dev can not allow power");
671 return;
672 }
673
674 if (aw883xx->pstream == AW883XX_STREAM_CLOSE) {
675 aw_dev_info(aw883xx->dev, "pstream is close");
676 return;
677 }
678
679 for (i = 0; i < AW_START_RETRIES; i++) {
680 ret = aw_device_start(aw883xx->aw_pa);
681 if (ret) {
682 aw_dev_err(aw883xx->dev, "start failed");
683 ret = aw_dev_fw_update(aw883xx->aw_pa, AW_DSP_FW_UPDATE_ON, true);
684 if (ret < 0) {
685 aw_dev_err(aw883xx->dev, "fw update failed");
686 continue;
687 }
688 } else {
689 aw_dev_info(aw883xx->dev, "start success");
690 break;
691 }
692 }
693 }
694
aw883xx_startup_work(struct work_struct * work)695 static void aw883xx_startup_work(struct work_struct *work)
696 {
697 struct aw883xx *aw883xx =
698 container_of(work, struct aw883xx, start_work.work);
699
700 mutex_lock(&aw883xx->lock);
701 aw883xx_start_pa(aw883xx);
702 mutex_unlock(&aw883xx->lock);
703 }
704
aw883xx_start(struct aw883xx * aw883xx,bool sync_start)705 static void aw883xx_start(struct aw883xx *aw883xx, bool sync_start)
706 {
707 int ret;
708 int i;
709
710 if (aw883xx->aw_pa->fw_status == AW_DEV_FW_OK) {
711 if (aw883xx->allow_pw == false) {
712 aw_dev_info(aw883xx->dev, "dev can not allow power");
713 return;
714 }
715
716 if (aw883xx->aw_pa->status == AW_DEV_PW_ON)
717 return;
718
719 for (i = 0; i < AW_START_RETRIES; i++) {
720 ret = aw_dev_fw_update(aw883xx->aw_pa, AW_DSP_FW_UPDATE_OFF,
721 aw883xx->phase_sync);
722 if (ret < 0) {
723 aw_dev_err(aw883xx->dev, "fw update failed");
724 continue;
725 } else {
726 /*firmware update success*/
727 if (sync_start == AW_SYNC_START)
728 aw883xx_start_pa(aw883xx);
729 else
730 queue_delayed_work(aw883xx->work_queue,
731 &aw883xx->start_work,
732 AW883XX_START_WORK_DELAY_MS);
733
734 return;
735 }
736 }
737 }
738 }
739
aw883xx_mute(struct snd_soc_dai * dai,int mute,int stream)740 static int aw883xx_mute(struct snd_soc_dai *dai, int mute, int stream)
741 {
742 aw_snd_soc_codec_t *codec = aw883xx_get_codec(dai);
743 struct aw883xx *aw883xx =
744 aw_componet_codec_ops.codec_get_drvdata(codec);
745
746 aw_dev_info(aw883xx->dev, "mute state=%d", mute);
747
748 if (stream != SNDRV_PCM_STREAM_PLAYBACK) {
749 aw_dev_info(aw883xx->dev, "capture");
750 return 0;
751 }
752
753 if (mute) {
754 aw883xx->pstream = AW883XX_STREAM_CLOSE;
755 cancel_delayed_work_sync(&aw883xx->start_work);
756 mutex_lock(&aw883xx->lock);
757 aw_device_stop(aw883xx->aw_pa);
758 mutex_unlock(&aw883xx->lock);
759 } else {
760 aw883xx->pstream = AW883XX_STREAM_OPEN;
761 mutex_lock(&aw883xx->lock);
762 aw883xx_start(aw883xx, AW_ASYNC_START);
763 aw_hold_dsp_spin_st(&aw883xx->aw_pa->spin_desc);
764 mutex_unlock(&aw883xx->lock);
765 }
766
767 return 0;
768 }
769
aw883xx_shutdown(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)770 static void aw883xx_shutdown(struct snd_pcm_substream *substream,
771 struct snd_soc_dai *dai)
772 {
773 aw_snd_soc_codec_t *codec = aw883xx_get_codec(dai);
774 struct aw883xx *aw883xx =
775 aw_componet_codec_ops.codec_get_drvdata(codec);
776
777 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
778 aw_dev_info(aw883xx->dev, "stream playback");
779 else
780 aw_dev_info(aw883xx->dev, "stream capture");
781 }
782
783 static const struct snd_soc_dai_ops aw883xx_dai_ops = {
784 .startup = aw883xx_startup,
785 .set_fmt = aw883xx_set_fmt,
786 .set_sysclk = aw883xx_set_dai_sysclk,
787 .hw_params = aw883xx_hw_params,
788 .mute_stream = aw883xx_mute,
789 .shutdown = aw883xx_shutdown,
790 };
791
792 static struct snd_soc_dai_driver aw883xx_dai[] = {
793 {
794 .name = "aw883xx-aif",
795 .id = 1,
796 .playback = {
797 .stream_name = "Speaker_Playback",
798 .channels_min = 1,
799 .channels_max = 2,
800 .rates = AW883XX_RATES,
801 .formats = AW883XX_FORMATS,
802 },
803 .capture = {
804 .stream_name = "Speaker_Capture",
805 .channels_min = 1,
806 .channels_max = 2,
807 .rates = AW883XX_RATES,
808 .formats = AW883XX_FORMATS,
809 },
810 .ops = &aw883xx_dai_ops,
811 /*.symmetric_rates = 1,*/
812 },
813 };
814
aw883xx_dai_drv_append_suffix(struct aw883xx * aw883xx,struct snd_soc_dai_driver * dai_drv,int num_dai)815 static int aw883xx_dai_drv_append_suffix(struct aw883xx *aw883xx,
816 struct snd_soc_dai_driver *dai_drv,
817 int num_dai)
818 {
819 int ret;
820 int i;
821
822 if ((dai_drv != NULL) && (num_dai > 0)) {
823 for (i = 0; i < num_dai; i++) {
824 ret = aw883xx_append_i2c_suffix("%s-%x-%x",
825 &dai_drv->name, aw883xx);
826 if (ret < 0)
827 return ret;
828 ret = aw883xx_append_i2c_suffix("%s_%x_%x",
829 &dai_drv->playback.stream_name, aw883xx);
830 if (ret < 0)
831 return ret;
832 ret = aw883xx_append_i2c_suffix("%s_%x_%x",
833 &dai_drv->capture.stream_name, aw883xx);
834 if (ret < 0)
835 return ret;
836
837 aw_dev_info(aw883xx->dev, "dai name [%s]", dai_drv[i].name);
838 aw_dev_info(aw883xx->dev, "pstream_name [%s]",
839 dai_drv[i].playback.stream_name);
840 aw_dev_info(aw883xx->dev, "cstream_name [%s]",
841 dai_drv[i].capture.stream_name);
842 }
843 }
844
845 return 0;
846 }
847
848 /*****************************************************
849 *
850 * codec driver
851 *
852 *****************************************************/
aw883xx_get_fade_in_time(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)853 static int aw883xx_get_fade_in_time(struct snd_kcontrol *kcontrol,
854 struct snd_ctl_elem_value *ucontrol)
855 {
856 unsigned int time;
857
858 aw_dev_get_fade_time(&time, true);
859 ucontrol->value.integer.value[0] = time;
860
861 aw_pr_dbg("step time %ld", ucontrol->value.integer.value[0]);
862
863 return 0;
864
865 }
866
aw883xx_set_fade_in_time(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)867 static int aw883xx_set_fade_in_time(struct snd_kcontrol *kcontrol,
868 struct snd_ctl_elem_value *ucontrol)
869 {
870 struct soc_mixer_control *mc =
871 (struct soc_mixer_control *)kcontrol->private_value;
872
873 if ((ucontrol->value.integer.value[0] > mc->max) ||
874 (ucontrol->value.integer.value[0] < mc->min)) {
875 aw_pr_dbg("set val %ld overflow %d or less than :%d",
876 ucontrol->value.integer.value[0], mc->max, mc->min);
877 return 0;
878 }
879 aw_dev_set_fade_time(ucontrol->value.integer.value[0], true);
880
881 aw_pr_dbg("step time %ld", ucontrol->value.integer.value[0]);
882 return 0;
883 }
884
aw883xx_get_fade_out_time(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)885 static int aw883xx_get_fade_out_time(struct snd_kcontrol *kcontrol,
886 struct snd_ctl_elem_value *ucontrol)
887 {
888 unsigned int time;
889
890 aw_dev_get_fade_time(&time, false);
891 ucontrol->value.integer.value[0] = time;
892
893 aw_pr_dbg("step time %ld", ucontrol->value.integer.value[0]);
894
895 return 0;
896 }
897
aw883xx_set_fade_out_time(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)898 static int aw883xx_set_fade_out_time(struct snd_kcontrol *kcontrol,
899 struct snd_ctl_elem_value *ucontrol)
900 {
901 struct soc_mixer_control *mc =
902 (struct soc_mixer_control *)kcontrol->private_value;
903
904 if ((ucontrol->value.integer.value[0] > mc->max) ||
905 (ucontrol->value.integer.value[0] < mc->min)) {
906 aw_pr_dbg("set val %ld overflow %d or less than :%d",
907 ucontrol->value.integer.value[0], mc->max, mc->min);
908 return 0;
909 }
910
911 aw_dev_set_fade_time(ucontrol->value.integer.value[0], false);
912
913 aw_pr_dbg("step time %ld", ucontrol->value.integer.value[0]);
914
915 return 0;
916 }
917
918 static struct snd_kcontrol_new aw883xx_controls[] = {
919 SOC_SINGLE_EXT("aw883xx_fadein_us", 0, 0, 1000000, 0,
920 aw883xx_get_fade_in_time, aw883xx_set_fade_in_time),
921 SOC_SINGLE_EXT("aw883xx_fadeout_us", 0, 0, 1000000, 0,
922 aw883xx_get_fade_out_time, aw883xx_set_fade_out_time),
923 };
924
925
aw883xx_add_codec_controls(struct aw883xx * aw883xx)926 static void aw883xx_add_codec_controls(struct aw883xx *aw883xx)
927 {
928 aw_dev_info(aw883xx->dev, "enter");
929
930 if (aw883xx->aw_pa->channel == 0) {
931 aw_componet_codec_ops.add_codec_controls(aw883xx->codec,
932 &aw883xx_controls[0], ARRAY_SIZE(aw883xx_controls));
933 aw_add_spin_controls((void *)aw883xx);
934 }
935 }
936
aw883xx_profile_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)937 static int aw883xx_profile_info(struct snd_kcontrol *kcontrol,
938 struct snd_ctl_elem_info *uinfo)
939 {
940 int count;
941 char *name = NULL;
942 const char *prof_name = NULL;
943 aw_snd_soc_codec_t *codec =
944 aw_componet_codec_ops.kcontrol_codec(kcontrol);
945 struct aw883xx *aw883xx =
946 aw_componet_codec_ops.codec_get_drvdata(codec);
947
948 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
949 uinfo->count = 1;
950
951 count = aw_dev_get_profile_count(aw883xx->aw_pa);
952 if (count <= 0) {
953 uinfo->value.enumerated.items = 0;
954 aw_dev_err(aw883xx->dev, "get count[%d] failed", count);
955 return 0;
956 }
957
958 uinfo->value.enumerated.items = count;
959
960 if (uinfo->value.enumerated.item >= count)
961 uinfo->value.enumerated.item = count - 1;
962
963 name = uinfo->value.enumerated.name;
964 count = uinfo->value.enumerated.item;
965
966 prof_name = aw_dev_get_prof_name(aw883xx->aw_pa, count);
967 if (prof_name == NULL) {
968 strlcpy(uinfo->value.enumerated.name, "null",
969 strlen("null") + 1);
970 return 0;
971 }
972
973 strlcpy(name, prof_name, sizeof(uinfo->value.enumerated.name));
974
975 return 0;
976 }
977
aw883xx_profile_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)978 static int aw883xx_profile_get(struct snd_kcontrol *kcontrol,
979 struct snd_ctl_elem_value *ucontrol)
980 {
981 aw_snd_soc_codec_t *codec =
982 aw_componet_codec_ops.kcontrol_codec(kcontrol);
983 struct aw883xx *aw883xx =
984 aw_componet_codec_ops.codec_get_drvdata(codec);
985
986 ucontrol->value.integer.value[0] = aw_dev_get_profile_index(aw883xx->aw_pa);
987 aw_dev_dbg(codec->dev, "profile index [%d]",
988 aw_dev_get_profile_index(aw883xx->aw_pa));
989 return 0;
990
991 }
992
aw883xx_profile_set(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)993 static int aw883xx_profile_set(struct snd_kcontrol *kcontrol,
994 struct snd_ctl_elem_value *ucontrol)
995 {
996 aw_snd_soc_codec_t *codec =
997 aw_componet_codec_ops.kcontrol_codec(kcontrol);
998 struct aw883xx *aw883xx =
999 aw_componet_codec_ops.codec_get_drvdata(codec);
1000 int ret;
1001 int cur_index;
1002
1003 if (aw883xx->dbg_en_prof == false) {
1004 aw_dev_info(codec->dev, "profile close");
1005 return 0;
1006 }
1007
1008 /* check value valid */
1009 ret = aw_dev_check_profile_index(aw883xx->aw_pa, ucontrol->value.integer.value[0]);
1010 if (ret) {
1011 aw_dev_info(codec->dev, "unsupported index %ld",
1012 ucontrol->value.integer.value[0]);
1013 return 0;
1014 }
1015
1016 /*check cur_index == set value*/
1017 cur_index = aw_dev_get_profile_index(aw883xx->aw_pa);
1018 if (cur_index == ucontrol->value.integer.value[0]) {
1019 aw_dev_info(codec->dev, "index no change");
1020 return 0;
1021 }
1022
1023 /*pa stop or stopping just set profile*/
1024 mutex_lock(&aw883xx->lock);
1025 aw_dev_set_profile_index(aw883xx->aw_pa, ucontrol->value.integer.value[0]);
1026
1027 if (aw883xx->pstream) {
1028 aw_device_stop(aw883xx->aw_pa);
1029 aw883xx_start(aw883xx, AW_SYNC_START);
1030 }
1031
1032 mutex_unlock(&aw883xx->lock);
1033
1034 aw_dev_info(codec->dev, "profile id %ld", ucontrol->value.integer.value[0]);
1035 return 0;
1036 }
1037
aw883xx_switch_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1038 static int aw883xx_switch_info(struct snd_kcontrol *kcontrol,
1039 struct snd_ctl_elem_info *uinfo)
1040 {
1041 int count;
1042
1043 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1044 uinfo->count = 1;
1045 count = 2;
1046
1047 uinfo->value.enumerated.items = count;
1048
1049 if (uinfo->value.enumerated.item >= count)
1050 uinfo->value.enumerated.item = count - 1;
1051
1052 strlcpy(uinfo->value.enumerated.name,
1053 aw883xx_switch[uinfo->value.enumerated.item],
1054 strlen(aw883xx_switch[uinfo->value.enumerated.item]) + 1);
1055
1056 return 0;
1057 }
1058
aw883xx_switch_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1059 static int aw883xx_switch_get(struct snd_kcontrol *kcontrol,
1060 struct snd_ctl_elem_value *ucontrol)
1061 {
1062 aw_snd_soc_codec_t *codec =
1063 aw_componet_codec_ops.kcontrol_codec(kcontrol);
1064 struct aw883xx *aw883xx =
1065 aw_componet_codec_ops.codec_get_drvdata(codec);
1066
1067 ucontrol->value.integer.value[0] = aw883xx->allow_pw;
1068
1069 return 0;
1070 }
1071
aw883xx_switch_set(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1072 static int aw883xx_switch_set(struct snd_kcontrol *kcontrol,
1073 struct snd_ctl_elem_value *ucontrol)
1074 {
1075 aw_snd_soc_codec_t *codec =
1076 aw_componet_codec_ops.kcontrol_codec(kcontrol);
1077 struct aw883xx *aw883xx =
1078 aw_componet_codec_ops.codec_get_drvdata(codec);
1079
1080 aw_dev_info(codec->dev, "set value:%ld", ucontrol->value.integer.value[0]);
1081
1082 if (ucontrol->value.integer.value[0] == aw883xx->allow_pw) {
1083 aw_dev_info(aw883xx->dev, "PA switch not change");
1084 return 0;
1085 }
1086
1087 if (aw883xx->pstream) {
1088 if (ucontrol->value.integer.value[0] == 0) {
1089 cancel_delayed_work_sync(&aw883xx->start_work);
1090 mutex_lock(&aw883xx->lock);
1091 aw_device_stop(aw883xx->aw_pa);
1092 aw883xx->allow_pw = false;
1093 mutex_unlock(&aw883xx->lock);
1094 } else {
1095 cancel_delayed_work_sync(&aw883xx->start_work);
1096 mutex_lock(&aw883xx->lock);
1097 aw883xx->allow_pw = true;
1098 aw883xx_start(aw883xx, AW_SYNC_START);
1099 mutex_unlock(&aw883xx->lock);
1100 }
1101 } else {
1102 mutex_lock(&aw883xx->lock);
1103 if (ucontrol->value.integer.value[0])
1104 aw883xx->allow_pw = true;
1105 else
1106 aw883xx->allow_pw = false;
1107 mutex_unlock(&aw883xx->lock);
1108 }
1109
1110 return 0;
1111 }
1112
aw883xx_monitor_switch_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1113 static int aw883xx_monitor_switch_info(struct snd_kcontrol *kcontrol,
1114 struct snd_ctl_elem_info *uinfo)
1115 {
1116 int count;
1117
1118 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1119 uinfo->count = 1;
1120 count = 2;
1121
1122 uinfo->value.enumerated.items = count;
1123
1124 if (uinfo->value.enumerated.item >= count)
1125 uinfo->value.enumerated.item = count - 1;
1126
1127 strlcpy(uinfo->value.enumerated.name,
1128 aw883xx_switch[uinfo->value.enumerated.item],
1129 strlen(aw883xx_switch[uinfo->value.enumerated.item]) + 1);
1130
1131 return 0;
1132 }
1133
aw883xx_monitor_switch_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1134 static int aw883xx_monitor_switch_get(struct snd_kcontrol *kcontrol,
1135 struct snd_ctl_elem_value *ucontrol)
1136 {
1137 aw_snd_soc_codec_t *codec =
1138 aw_componet_codec_ops.kcontrol_codec(kcontrol);
1139 struct aw883xx *aw883xx =
1140 aw_componet_codec_ops.codec_get_drvdata(codec);
1141 struct aw_monitor_desc *monitor_desc = &aw883xx->aw_pa->monitor_desc;
1142
1143 ucontrol->value.integer.value[0] = monitor_desc->monitor_cfg.monitor_switch;
1144
1145 return 0;
1146 }
1147
aw883xx_monitor_switch_set(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1148 static int aw883xx_monitor_switch_set(struct snd_kcontrol *kcontrol,
1149 struct snd_ctl_elem_value *ucontrol)
1150 {
1151 aw_snd_soc_codec_t *codec =
1152 aw_componet_codec_ops.kcontrol_codec(kcontrol);
1153 struct aw883xx *aw883xx =
1154 aw_componet_codec_ops.codec_get_drvdata(codec);
1155 struct aw_monitor_desc *monitor_desc = &aw883xx->aw_pa->monitor_desc;
1156 uint32_t enable = 0;
1157
1158 aw_dev_info(codec->dev, "set monitor_switch:%ld", ucontrol->value.integer.value[0]);
1159
1160 enable = ucontrol->value.integer.value[0];
1161
1162 if (monitor_desc->monitor_cfg.monitor_switch == enable) {
1163 aw_dev_info(aw883xx->dev, "monitor_switch not change");
1164 return 0;
1165 } else {
1166 monitor_desc->monitor_cfg.monitor_switch = enable;
1167 if (enable)
1168 aw_monitor_start(monitor_desc);
1169 }
1170
1171 return 0;
1172 }
1173
aw883xx_dynamic_create_controls(struct aw883xx * aw883xx)1174 static int aw883xx_dynamic_create_controls(struct aw883xx *aw883xx)
1175 {
1176 struct snd_kcontrol_new *aw883xx_dev_control = NULL;
1177 char *kctl_name = NULL;
1178
1179 aw883xx_dev_control = devm_kzalloc(aw883xx->codec->dev,
1180 sizeof(struct snd_kcontrol_new) * AW_KCONTROL_NUM, GFP_KERNEL);
1181 if (aw883xx_dev_control == NULL) {
1182 aw_dev_err(aw883xx->codec->dev, "kcontrol malloc failed!");
1183 return -ENOMEM;
1184 }
1185
1186 kctl_name = devm_kzalloc(aw883xx->codec->dev, AW_NAME_BUF_MAX, GFP_KERNEL);
1187 if (kctl_name == NULL)
1188 return -ENOMEM;
1189
1190 snprintf(kctl_name, AW_NAME_BUF_MAX, "aw_dev_%u_prof",
1191 aw883xx->aw_pa->channel);
1192
1193 aw883xx_dev_control[0].name = kctl_name;
1194 aw883xx_dev_control[0].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1195 aw883xx_dev_control[0].info = aw883xx_profile_info;
1196 aw883xx_dev_control[0].get = aw883xx_profile_get;
1197 aw883xx_dev_control[0].put = aw883xx_profile_set;
1198
1199 kctl_name = devm_kzalloc(aw883xx->codec->dev, AW_NAME_BUF_MAX, GFP_KERNEL);
1200 if (!kctl_name)
1201 return -ENOMEM;
1202
1203 snprintf(kctl_name, AW_NAME_BUF_MAX, "aw_dev_%u_switch", aw883xx->aw_pa->channel);
1204
1205 aw883xx_dev_control[1].name = kctl_name;
1206 aw883xx_dev_control[1].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1207 aw883xx_dev_control[1].info = aw883xx_switch_info;
1208 aw883xx_dev_control[1].get = aw883xx_switch_get;
1209 aw883xx_dev_control[1].put = aw883xx_switch_set;
1210
1211 kctl_name = devm_kzalloc(aw883xx->codec->dev, AW_NAME_BUF_MAX, GFP_KERNEL);
1212 if (!kctl_name)
1213 return -ENOMEM;
1214
1215 snprintf(kctl_name, AW_NAME_BUF_MAX, "aw_dev_%u_monitor_switch", aw883xx->aw_pa->channel);
1216
1217 aw883xx_dev_control[2].name = kctl_name;
1218 aw883xx_dev_control[2].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1219 aw883xx_dev_control[2].info = aw883xx_monitor_switch_info;
1220 aw883xx_dev_control[2].get = aw883xx_monitor_switch_get;
1221 aw883xx_dev_control[2].put = aw883xx_monitor_switch_set;
1222
1223 aw_componet_codec_ops.add_codec_controls(aw883xx->codec,
1224 aw883xx_dev_control, AW_KCONTROL_NUM);
1225
1226 return 0;
1227 }
1228
aw883xx_request_firmware_file(struct aw883xx * aw883xx)1229 static int aw883xx_request_firmware_file(struct aw883xx *aw883xx)
1230 {
1231 const struct firmware *cont = NULL;
1232 struct aw_container *aw_cfg = NULL;
1233 int ret = -1;
1234 int i;
1235
1236 aw883xx->aw_pa->fw_status = AW_DEV_FW_FAILED;
1237
1238 for (i = 0; i < AW_REQUEST_FW_RETRIES; i++) {
1239 ret = request_firmware(&cont, AW883XX_ACF_FILE, aw883xx->dev);
1240 if ((ret < 0) || (!cont)) {
1241 aw883xx->fw_retry_cnt++;
1242 aw_dev_err(aw883xx->dev, "load [%s] try [%d]!",
1243 AW883XX_ACF_FILE, aw883xx->fw_retry_cnt);
1244
1245 if (aw883xx->fw_retry_cnt == AW_REQUEST_FW_RETRIES) {
1246 aw883xx->fw_retry_cnt = 0;
1247 return ret;
1248 }
1249 msleep(1000);
1250 } else {
1251 break;
1252 }
1253 }
1254
1255 if (!cont)
1256 return -ENOMEM;
1257
1258 aw_dev_info(aw883xx->dev, "loaded %s - size: %zu",
1259 AW883XX_ACF_FILE, cont ? cont->size : 0);
1260
1261 mutex_lock(&g_aw883xx_lock);
1262 if (g_awinic_cfg == NULL) {
1263 aw_cfg = vzalloc(cont->size + sizeof(int));
1264 if (aw_cfg == NULL) {
1265 aw_dev_err(aw883xx->dev, "aw883xx_cfg devm_kzalloc failed");
1266 release_firmware(cont);
1267 mutex_unlock(&g_aw883xx_lock);
1268 return -ENOMEM;
1269 }
1270 aw_cfg->len = cont->size;
1271 memcpy(aw_cfg->data, cont->data, cont->size);
1272 ret = aw_dev_load_acf_check(aw_cfg);
1273 if (ret < 0) {
1274 aw_dev_err(aw883xx->dev, "Load [%s] failed ....!", AW883XX_ACF_FILE);
1275 vfree(aw_cfg);
1276 aw_cfg = NULL;
1277 release_firmware(cont);
1278 mutex_unlock(&g_aw883xx_lock);
1279 return ret;
1280 }
1281 g_awinic_cfg = aw_cfg;
1282 } else {
1283 aw_cfg = g_awinic_cfg;
1284 aw_dev_info(aw883xx->dev, "[%s] already loaded...", AW883XX_ACF_FILE);
1285 }
1286 release_firmware(cont);
1287 mutex_unlock(&g_aw883xx_lock);
1288
1289 mutex_lock(&aw883xx->lock);
1290 /*aw device init*/
1291 ret = aw_device_init(aw883xx->aw_pa, aw_cfg);
1292 if (ret < 0) {
1293 aw_dev_info(aw883xx->dev, "dev init failed");
1294 mutex_unlock(&aw883xx->lock);
1295 return ret;
1296 }
1297
1298 aw883xx_dynamic_create_controls(aw883xx);
1299
1300 aw_check_spin_mode(&aw883xx->aw_pa->spin_desc);
1301
1302 mutex_unlock(&aw883xx->lock);
1303
1304 return 0;
1305 }
1306
aw883xx_fw_wrok(struct work_struct * work)1307 static void aw883xx_fw_wrok(struct work_struct *work)
1308 {
1309 struct aw883xx *aw883xx = container_of(work,
1310 struct aw883xx, acf_work.work);
1311 int ret;
1312
1313 ret = aw883xx_request_firmware_file(aw883xx);
1314 if (ret < 0)
1315 aw_dev_err(aw883xx->dev, "load profile failed");
1316
1317 }
1318
aw883xx_load_fw(struct aw883xx * aw883xx)1319 static void aw883xx_load_fw(struct aw883xx *aw883xx)
1320 {
1321
1322 if (aw883xx->aw_pa->platform == AW_QCOM) {
1323 /*QCOM sync loading*/
1324 aw883xx_request_firmware_file(aw883xx);
1325 } else {
1326 /*async loading*/
1327 queue_delayed_work(aw883xx->work_queue,
1328 &aw883xx->acf_work,
1329 msecs_to_jiffies(AW883XX_LOAD_FW_DELAY_TIME));
1330 }
1331 }
1332
1333 #ifdef AW_MTK_PLATFORM
1334
1335 static const struct snd_soc_dapm_widget aw883xx_dapm_widgets[] = {
1336 /* playback */
1337 SND_SOC_DAPM_AIF_IN("AIF_RX", "Speaker_Playback", 0, SND_SOC_NOPM, 0, 0),
1338 SND_SOC_DAPM_OUTPUT("audio_out"),
1339 /* capture */
1340 SND_SOC_DAPM_AIF_OUT("AIF_TX", "Speaker_Capture", 0, SND_SOC_NOPM, 0, 0),
1341 SND_SOC_DAPM_INPUT("iv_in"),
1342 };
1343
1344 static const struct snd_soc_dapm_route aw883xx_audio_map[] = {
1345 {"audio_out", NULL, "AIF_RX"},
1346 {"AIF_TX", NULL, "iv_in"},
1347 };
1348
1349 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 2, 0)
snd_soc_codec_get_dapm(struct snd_soc_codec * codec)1350 static struct snd_soc_dapm_context *snd_soc_codec_get_dapm(struct snd_soc_codec *codec)
1351 {
1352 return &codec->dapm;
1353 }
1354 #endif
1355
aw883xx_add_widgets(struct aw883xx * aw883xx)1356 static int aw883xx_add_widgets(struct aw883xx *aw883xx)
1357 {
1358 int i = 0;
1359 int ret;
1360 struct snd_soc_dapm_widget *aw_widgets = NULL;
1361 struct snd_soc_dapm_route *aw_route = NULL;
1362 #ifdef AW_KERNEL_VER_OVER_4_19_1
1363 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(aw883xx->codec);
1364 #else
1365 struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(aw883xx->codec);
1366 #endif
1367
1368 /*add widgets*/
1369 aw_widgets = devm_kzalloc(aw883xx->dev,
1370 sizeof(struct snd_soc_dapm_widget) * ARRAY_SIZE(aw883xx_dapm_widgets),
1371 GFP_KERNEL);
1372 if (!aw_widgets)
1373 return -ENOMEM;
1374
1375 memcpy(aw_widgets, aw883xx_dapm_widgets,
1376 sizeof(struct snd_soc_dapm_widget) * ARRAY_SIZE(aw883xx_dapm_widgets));
1377
1378 for (i = 0; i < ARRAY_SIZE(aw883xx_dapm_widgets); i++) {
1379 if (aw_widgets[i].name) {
1380 ret = aw883xx_append_i2c_suffix("%s_%x_%x", &aw_widgets[i].name, aw883xx);
1381 if (ret < 0) {
1382 aw_dev_err(aw883xx->dev, "aw_widgets.name append i2c suffix failed!\n");
1383 return ret;
1384 }
1385 }
1386
1387 if (aw_widgets[i].sname) {
1388 ret = aw883xx_append_i2c_suffix("%s_%x_%x", &aw_widgets[i].sname, aw883xx);
1389 if (ret < 0) {
1390 aw_dev_err(aw883xx->dev, "aw_widgets.name append i2c suffix failed!");
1391 return ret;
1392 }
1393 }
1394 }
1395
1396 snd_soc_dapm_new_controls(dapm, aw_widgets, ARRAY_SIZE(aw883xx_dapm_widgets));
1397
1398 /*add route*/
1399 aw_route = devm_kzalloc(aw883xx->dev,
1400 sizeof(struct snd_soc_dapm_route) * ARRAY_SIZE(aw883xx_audio_map),
1401 GFP_KERNEL);
1402 if (!aw_route)
1403 return -ENOMEM;
1404
1405 memcpy(aw_route, aw883xx_audio_map,
1406 sizeof(struct snd_soc_dapm_route) * ARRAY_SIZE(aw883xx_audio_map));
1407
1408 for (i = 0; i < ARRAY_SIZE(aw883xx_audio_map); i++) {
1409 if (aw_route[i].sink) {
1410 ret = aw883xx_append_i2c_suffix("%s_%x_%x", &aw_route[i].sink, aw883xx);
1411 if (ret < 0) {
1412 aw_dev_err(aw883xx->dev, "aw_route.sink append i2c suffix failed!");
1413 return ret;
1414 }
1415 }
1416
1417 if (aw_route[i].source) {
1418 ret = aw883xx_append_i2c_suffix("%s_%x_%x", &aw_route[i].source, aw883xx);
1419 if (ret < 0) {
1420 aw_dev_err(aw883xx->dev, "aw_route.source append i2c suffix failed!");
1421 return ret;
1422 }
1423 }
1424 }
1425 snd_soc_dapm_add_routes(dapm, aw_route, ARRAY_SIZE(aw883xx_audio_map));
1426
1427 return 0;
1428 }
1429 #endif
1430
aw883xx_codec_probe(aw_snd_soc_codec_t * aw_codec)1431 static int aw883xx_codec_probe(aw_snd_soc_codec_t *aw_codec)
1432 {
1433 struct aw883xx *aw883xx =
1434 aw_componet_codec_ops.codec_get_drvdata(aw_codec);
1435
1436 aw_dev_info(aw883xx->dev, "enter");
1437
1438 /*destroy_workqueue(struct workqueue_struct *wq)*/
1439 aw883xx->work_queue = create_singlethread_workqueue("aw883xx");
1440 if (!aw883xx->work_queue) {
1441 aw_dev_err(aw883xx->dev, "create workqueue failed !");
1442 return -EINVAL;
1443 }
1444
1445 INIT_DELAYED_WORK(&aw883xx->interrupt_work, aw883xx_interrupt_work);
1446 INIT_DELAYED_WORK(&aw883xx->start_work, aw883xx_startup_work);
1447 INIT_DELAYED_WORK(&aw883xx->acf_work, aw883xx_fw_wrok);
1448
1449 aw883xx->codec = aw_codec;
1450
1451 aw883xx_add_codec_controls(aw883xx);
1452 #ifdef AW_MTK_PLATFORM
1453 aw883xx_add_widgets(aw883xx);
1454 #endif
1455 aw883xx_load_fw(aw883xx);
1456
1457 return 0;
1458 }
1459
1460 #ifdef AW_KERNEL_VER_OVER_4_19_1
aw883xx_codec_remove(aw_snd_soc_codec_t * aw_codec)1461 static void aw883xx_codec_remove(aw_snd_soc_codec_t *aw_codec)
1462 {
1463 struct aw883xx *aw883xx =
1464 aw_componet_codec_ops.codec_get_drvdata(aw_codec);
1465
1466 aw_dev_info(aw883xx->dev, "enter");
1467
1468 cancel_delayed_work_sync(&aw883xx->interrupt_work);
1469 cancel_delayed_work_sync(&aw883xx->acf_work);
1470 cancel_delayed_work_sync(&aw883xx->aw_pa->monitor_desc.delay_work);
1471 cancel_delayed_work_sync(&aw883xx->start_work);
1472
1473 if (aw883xx->work_queue)
1474 destroy_workqueue(aw883xx->work_queue);
1475
1476 aw_dev_deinit(aw883xx->aw_pa);
1477 }
1478 #else
aw883xx_codec_remove(aw_snd_soc_codec_t * aw_codec)1479 static int aw883xx_codec_remove(aw_snd_soc_codec_t *aw_codec)
1480 {
1481 struct aw883xx *aw883xx =
1482 aw_componet_codec_ops.codec_get_drvdata(aw_codec);
1483
1484 aw_dev_info(aw883xx->dev, "enter");
1485
1486 cancel_delayed_work_sync(&aw883xx->interrupt_work);
1487 cancel_delayed_work_sync(&aw883xx->acf_work);
1488 cancel_delayed_work_sync(&aw883xx->aw_pa->monitor_desc.delay_work);
1489 cancel_delayed_work_sync(&aw883xx->start_work);
1490
1491 if (aw883xx->work_queue)
1492 destroy_workqueue(aw883xx->work_queue);
1493
1494 aw_dev_deinit(aw883xx->aw_pa);
1495
1496 return 0;
1497 }
1498 #endif
1499
1500 #ifdef AW_KERNEL_VER_OVER_4_19_1
1501 static struct snd_soc_component_driver soc_codec_dev_aw883xx = {
1502 .probe = aw883xx_codec_probe,
1503 .remove = aw883xx_codec_remove,
1504 };
1505 #else
1506 static struct snd_soc_codec_driver soc_codec_dev_aw883xx = {
1507 .probe = aw883xx_codec_probe,
1508 .remove = aw883xx_codec_remove,
1509 };
1510 #endif
1511
aw883xx_componet_codec_register(struct aw883xx * aw883xx)1512 static int aw883xx_componet_codec_register(struct aw883xx *aw883xx)
1513 {
1514 struct snd_soc_dai_driver *dai_drv = NULL;
1515 int ret;
1516
1517 dai_drv = devm_kzalloc(aw883xx->dev, sizeof(aw883xx_dai), GFP_KERNEL);
1518 if (dai_drv == NULL) {
1519 aw_dev_err(aw883xx->dev, "dai_driver malloc failed");
1520 return -ENOMEM;
1521 }
1522
1523 memcpy(dai_drv, aw883xx_dai, sizeof(aw883xx_dai));
1524
1525 ret = aw883xx_dai_drv_append_suffix(aw883xx, dai_drv, 1);
1526 if (ret < 0)
1527 return ret;
1528
1529 ret = aw883xx->codec_ops->register_codec(aw883xx->dev,
1530 &soc_codec_dev_aw883xx,
1531 dai_drv, ARRAY_SIZE(aw883xx_dai));
1532 if (ret < 0) {
1533 aw_dev_err(aw883xx->dev, "failed to register aw883xx: %d", ret);
1534 return ret;
1535 }
1536
1537 return 0;
1538 }
1539
1540
aw883xx_malloc_init(struct i2c_client * i2c)1541 static struct aw883xx *aw883xx_malloc_init(struct i2c_client *i2c)
1542 {
1543 struct aw883xx *aw883xx = devm_kzalloc(&i2c->dev,
1544 sizeof(struct aw883xx), GFP_KERNEL);
1545 if (aw883xx == NULL) {
1546 aw_dev_err(&i2c->dev, "devm_kzalloc failed");
1547 return NULL;
1548 }
1549
1550 aw883xx->dev = &i2c->dev;
1551 aw883xx->i2c = i2c;
1552 aw883xx->aw_pa = NULL;
1553 aw883xx->codec = NULL;
1554 aw883xx->codec_ops = &aw_componet_codec_ops;
1555 aw883xx->dbg_en_prof = true;
1556 aw883xx->allow_pw = true;
1557 aw883xx->work_queue = NULL;
1558 aw883xx->i2c_log_en = 0;
1559 mutex_init(&aw883xx->lock);
1560 mutex_init(&aw883xx->i2c_lock);
1561
1562 return aw883xx;
1563 }
1564
aw883xx_gpio_request(struct aw883xx * aw883xx)1565 static int aw883xx_gpio_request(struct aw883xx *aw883xx)
1566 {
1567 int ret;
1568
1569 if (gpio_is_valid(aw883xx->reset_gpio)) {
1570 ret = devm_gpio_request_one(aw883xx->dev, aw883xx->reset_gpio,
1571 GPIOF_OUT_INIT_LOW, "aw883xx_rst");
1572 if (ret) {
1573 aw_dev_err(aw883xx->dev, "rst request failed");
1574 return ret;
1575 }
1576 }
1577
1578 if (gpio_is_valid(aw883xx->irq_gpio)) {
1579 ret = devm_gpio_request_one(aw883xx->dev, aw883xx->irq_gpio,
1580 GPIOF_DIR_IN, "aw883xx_int");
1581 if (ret) {
1582 aw_dev_err(aw883xx->dev, "int request failed");
1583 return ret;
1584 }
1585 }
1586
1587 return 0;
1588 }
1589
1590 /*****************************************************
1591 *
1592 * device tree
1593 *
1594 *****************************************************/
aw883xx_parse_gpio_dt(struct aw883xx * aw883xx)1595 static int aw883xx_parse_gpio_dt(struct aw883xx *aw883xx)
1596 {
1597 struct device_node *np = aw883xx->dev->of_node;
1598
1599 aw883xx->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0);
1600 if (aw883xx->reset_gpio < 0) {
1601 aw_dev_err(aw883xx->dev, "no reset gpio provided, will not hw reset");
1602 /* return -EIO; */
1603 } else {
1604 aw_dev_info(aw883xx->dev, "reset gpio provided ok");
1605 }
1606
1607 aw883xx->irq_gpio = of_get_named_gpio(np, "irq-gpio", 0);
1608 if (aw883xx->irq_gpio < 0)
1609 aw_dev_info(aw883xx->dev, "no irq gpio provided.");
1610 else
1611 aw_dev_info(aw883xx->dev, "irq gpio provided ok.");
1612
1613 return 0;
1614 }
1615
aw883xx_parse_sync_flag_dt(struct aw883xx * aw883xx)1616 static void aw883xx_parse_sync_flag_dt(struct aw883xx *aw883xx)
1617 {
1618 int ret;
1619 int32_t sync_enable = 0;
1620 struct device_node *np = aw883xx->dev->of_node;
1621
1622 ret = of_property_read_u32(np, "sync-flag", &sync_enable);
1623 if (ret < 0) {
1624 aw_dev_info(aw883xx->dev,
1625 "read sync flag failed,default phase sync off");
1626 sync_enable = false;
1627 } else {
1628 aw_dev_info(aw883xx->dev,
1629 "sync flag is %d", sync_enable);
1630 }
1631
1632 aw883xx->phase_sync = sync_enable;
1633 }
1634
aw883xx_parse_dt(struct aw883xx * aw883xx)1635 static int aw883xx_parse_dt(struct aw883xx *aw883xx)
1636 {
1637 aw883xx_parse_sync_flag_dt(aw883xx);
1638
1639 return aw883xx_parse_gpio_dt(aw883xx);
1640 }
1641
aw883xx_hw_reset(struct aw883xx * aw883xx)1642 static int aw883xx_hw_reset(struct aw883xx *aw883xx)
1643 {
1644 aw_dev_info(aw883xx->dev, "enter");
1645
1646 if (gpio_is_valid(aw883xx->reset_gpio)) {
1647 gpio_set_value_cansleep(aw883xx->reset_gpio, 0);
1648 usleep_range(AW_1000_US, AW_1000_US + 10);
1649 gpio_set_value_cansleep(aw883xx->reset_gpio, 1);
1650 usleep_range(AW_1000_US, AW_1000_US + 10);
1651 } else {
1652 aw_dev_err(aw883xx->dev, "failed");
1653 }
1654 return 0;
1655 }
1656
aw883xx_read_chipid(struct aw883xx * aw883xx)1657 static int aw883xx_read_chipid(struct aw883xx *aw883xx)
1658 {
1659 int ret = -1;
1660 uint16_t reg_val = 0;
1661
1662 ret = aw883xx_reg_read(aw883xx, AW883XX_CHIP_ID_REG, ®_val);
1663 if (ret < 0) {
1664 aw_dev_err(aw883xx->dev,
1665 "failed to read chip id, ret=%d", ret);
1666 return -EIO;
1667 }
1668
1669 aw883xx->chip_id = reg_val;
1670 aw_dev_info(aw883xx->dev,
1671 "read chip id: 0x%x", reg_val);
1672
1673 return 0;
1674 }
1675
1676 /******************************************************
1677 *
1678 * irq
1679 *
1680 ******************************************************/
aw883xx_irq(int irq,void * data)1681 static irqreturn_t aw883xx_irq(int irq, void *data)
1682 {
1683 struct aw883xx *aw883xx = data;
1684
1685 if (aw883xx == NULL) {
1686 aw_pr_err("pointer is NULL");
1687 return -EINVAL;
1688 }
1689
1690 aw_dev_info(aw883xx->dev, "enter");
1691 /*mask all irq*/
1692 aw_dev_set_intmask(aw883xx->aw_pa, false);
1693
1694 /*upload workqueue*/
1695 if (aw883xx->work_queue)
1696 queue_delayed_work(aw883xx->work_queue,
1697 &aw883xx->interrupt_work, 0);
1698
1699 return IRQ_HANDLED;
1700 }
1701
aw883xx_interrupt_init(struct aw883xx * aw883xx)1702 static int aw883xx_interrupt_init(struct aw883xx *aw883xx)
1703 {
1704 int irq_flags;
1705 int ret;
1706
1707 if (gpio_is_valid(aw883xx->irq_gpio)) {
1708 irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
1709 ret = devm_request_threaded_irq(aw883xx->dev,
1710 gpio_to_irq(aw883xx->irq_gpio),
1711 NULL, aw883xx_irq, irq_flags,
1712 "aw883xx", aw883xx);
1713 if (ret) {
1714 aw_dev_err(aw883xx->dev, "Failed to request IRQ %d: %d",
1715 gpio_to_irq(aw883xx->irq_gpio), ret);
1716 return ret;
1717 }
1718 } else {
1719 aw_dev_info(aw883xx->dev, "skipping IRQ registration");
1720 }
1721
1722 return 0;
1723 }
1724
1725
1726 /******************************************************
1727 *
1728 * sys group attribute: reg
1729 *
1730 ******************************************************/
aw883xx_reg_show(struct device * dev,struct device_attribute * attr,char * buf)1731 static ssize_t aw883xx_reg_show(struct device *dev,
1732 struct device_attribute *attr, char *buf)
1733 {
1734 struct aw883xx *aw883xx = dev_get_drvdata(dev);
1735 int reg_num = aw883xx->aw_pa->ops.aw_get_reg_num();
1736 ssize_t len = 0;
1737 uint8_t i = 0;
1738 uint16_t reg_val = 0;
1739
1740 for (i = 0; i < reg_num; i++) {
1741 if (aw883xx->aw_pa->ops.aw_check_rd_access(i)) {
1742 aw883xx_reg_read(aw883xx, i, ®_val);
1743 len += snprintf(buf + len, PAGE_SIZE - len,
1744 "reg:0x%02x=0x%04x\n", i, reg_val);
1745 }
1746 }
1747
1748 return len;
1749 }
1750
aw883xx_reg_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1751 static ssize_t aw883xx_reg_store(struct device *dev,
1752 struct device_attribute *attr, const char *buf,
1753 size_t count)
1754 {
1755 struct aw883xx *aw883xx = dev_get_drvdata(dev);
1756 unsigned int databuf[2] = { 0 };
1757
1758 if (2 == sscanf(buf, "%x %x", &databuf[0], &databuf[1]))
1759 aw883xx_reg_write(aw883xx, databuf[0], databuf[1]);
1760
1761 return count;
1762 }
1763
aw883xx_rw_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1764 static ssize_t aw883xx_rw_store(struct device *dev,
1765 struct device_attribute *attr, const char *buf,
1766 size_t count)
1767 {
1768 struct aw883xx *aw883xx = dev_get_drvdata(dev);
1769 unsigned int databuf[2] = { 0 };
1770
1771 if (2 == sscanf(buf, "%x %x", &databuf[0], &databuf[1])) {
1772 aw883xx->reg_addr = (uint8_t)databuf[0];
1773 if (aw883xx->aw_pa->ops.aw_check_rd_access(databuf[0]))
1774 aw883xx_reg_write(aw883xx, databuf[0], databuf[1]);
1775 } else if (1 == sscanf(buf, "%x", &databuf[0])) {
1776 aw883xx->reg_addr = (uint8_t)databuf[0];
1777 }
1778
1779 return count;
1780 }
1781
aw883xx_rw_show(struct device * dev,struct device_attribute * attr,char * buf)1782 static ssize_t aw883xx_rw_show(struct device *dev,
1783 struct device_attribute *attr, char *buf)
1784 {
1785 struct aw883xx *aw883xx = dev_get_drvdata(dev);
1786 ssize_t len = 0;
1787 uint16_t reg_val = 0;
1788
1789 if (aw883xx->aw_pa->ops.aw_check_rd_access(aw883xx->reg_addr)) {
1790 aw883xx_reg_read(aw883xx, aw883xx->reg_addr, ®_val);
1791 len += snprintf(buf + len, PAGE_SIZE - len,
1792 "reg:0x%02x=0x%04x\n", aw883xx->reg_addr,
1793 reg_val);
1794 }
1795
1796 return len;
1797 }
1798
aw883xx_drv_ver_show(struct device * dev,struct device_attribute * attr,char * buf)1799 static ssize_t aw883xx_drv_ver_show(struct device *dev,
1800 struct device_attribute *attr, char *buf)
1801 {
1802 ssize_t len = 0;
1803
1804 len += snprintf(buf+len, PAGE_SIZE-len,
1805 "driver_ver: %s \n", AW883XX_DRIVER_VERSION);
1806
1807 return len;
1808 }
1809
aw883xx_dsp_rw_show(struct device * dev,struct device_attribute * attr,char * buf)1810 static ssize_t aw883xx_dsp_rw_show(struct device *dev,
1811 struct device_attribute *attr, char *buf)
1812 {
1813 struct aw883xx *aw883xx = dev_get_drvdata(dev);
1814 ssize_t len = 0;
1815 uint16_t reg_val = 0;
1816
1817 mutex_lock(&aw883xx->i2c_lock);
1818 aw883xx_i2c_write(aw883xx, aw883xx->aw_pa->dsp_mem_desc.dsp_madd_reg, aw883xx->dsp_addr);
1819 aw883xx_i2c_read(aw883xx, aw883xx->aw_pa->dsp_mem_desc.dsp_mdat_reg, ®_val);
1820 len += snprintf(buf + len, PAGE_SIZE - len,
1821 "dsp:0x%04x=0x%04x\n", aw883xx->dsp_addr, reg_val);
1822 aw883xx_i2c_read(aw883xx, aw883xx->aw_pa->dsp_mem_desc.dsp_mdat_reg, ®_val);
1823 len += snprintf(buf + len, PAGE_SIZE - len,
1824 "dsp:0x%04x=0x%04x\n", aw883xx->dsp_addr + 1, reg_val);
1825 aw883xx_clear_dsp_sel_st(aw883xx);
1826 mutex_unlock(&aw883xx->i2c_lock);
1827
1828 return len;
1829 }
1830
aw883xx_dsp_rw_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1831 static ssize_t aw883xx_dsp_rw_store(struct device *dev,
1832 struct device_attribute *attr,
1833 const char *buf, size_t count)
1834 {
1835 struct aw883xx *aw883xx = dev_get_drvdata(dev);
1836 unsigned int databuf[2] = { 0 };
1837
1838 if (2 == sscanf(buf, "%x %x", &databuf[0], &databuf[1])) {
1839 aw883xx->dsp_addr = (unsigned int)databuf[0];
1840 aw883xx_dsp_write(aw883xx, databuf[0], databuf[1], AW_DSP_16_DATA);
1841 aw_dev_dbg(aw883xx->dev, "get param: %x %x",
1842 databuf[0], databuf[1]);
1843 } else if (1 == sscanf(buf, "%x", &databuf[0])) {
1844 aw883xx->dsp_addr = (unsigned int)databuf[0];
1845 aw_dev_dbg(aw883xx->dev, "get param: %x",
1846 databuf[0]);
1847 }
1848 aw883xx_clear_dsp_sel_st(aw883xx);
1849
1850 return count;
1851 }
1852
aw883xx_awrw_write(struct aw883xx * aw883xx,const char * buf,size_t count)1853 static int aw883xx_awrw_write(struct aw883xx *aw883xx, const char *buf, size_t count)
1854 {
1855 int i, ret;
1856 char *data_buf = NULL;
1857 int str_len, data_len, temp_data;
1858 struct aw883xx_i2c_packet *packet = &aw883xx->i2c_packet;
1859 uint32_t dsp_addr_h = 0, dsp_addr_l = 0;
1860
1861 data_len = AWRW_DATA_BYTES * packet->reg_num;
1862
1863 str_len = count - AWRW_HDR_LEN - 1;
1864 if ((data_len * 5 - 1) > str_len) {
1865 aw_dev_err(aw883xx->dev, "data_str_len [%d], requeset len [%d]",
1866 str_len, (data_len * 5 - 1));
1867 return -EINVAL;
1868 }
1869
1870 if (packet->reg_addr == aw883xx->aw_pa->dsp_mem_desc.dsp_madd_reg) {
1871 if (sscanf(buf + AWRW_HDR_LEN + 1, "0x%02x 0x%02x", &dsp_addr_h, &dsp_addr_l) == 2) {
1872 packet->dsp_addr = (dsp_addr_h << 8) | dsp_addr_l;
1873 packet->dsp_status = AWRW_DSP_READY;
1874 aw_dev_dbg(aw883xx->dev, "write:reg_addr[0x%02x], dsp_base_addr:[0x%02x]",
1875 packet->reg_addr, packet->dsp_addr);
1876 return 0;
1877 } else {
1878 aw_dev_err(aw883xx->dev, "get reg 0x%x data failed", packet->reg_addr);
1879 return -EINVAL;
1880 }
1881 }
1882
1883 mutex_lock(&aw883xx->i2c_lock);
1884 if (packet->reg_addr == aw883xx->aw_pa->dsp_mem_desc.dsp_mdat_reg) {
1885 if (packet->dsp_status != AWRW_DSP_READY) {
1886 aw_dev_err(aw883xx->dev, "please write reg[0x40] first");
1887 ret = -EINVAL;
1888 goto exit;
1889 }
1890 aw883xx_i2c_write(aw883xx, aw883xx->aw_pa->dsp_mem_desc.dsp_madd_reg, packet->dsp_addr);
1891 packet->dsp_status = AWRW_DSP_ST_NONE;
1892 }
1893
1894 aw_dev_info(aw883xx->dev, "write:reg_addr[0x%02x], reg_num[%d]",
1895 packet->reg_addr, packet->reg_num);
1896
1897 data_buf = devm_kzalloc(aw883xx->dev, data_len, GFP_KERNEL);
1898 if (data_buf == NULL) {
1899 aw_dev_err(aw883xx->dev, "alloc memory failed");
1900 ret = -ENOMEM;
1901 goto exit;
1902 }
1903
1904 for (i = 0; i < data_len; i++) {
1905 sscanf(buf + AWRW_HDR_LEN + 1 + i * 5, "0x%02x", &temp_data);
1906 data_buf[i] = temp_data;
1907
1908 }
1909
1910 ret = aw883xx_i2c_writes(aw883xx, packet->reg_addr, data_buf, data_len);
1911 if (ret < 0) {
1912 aw_dev_err(aw883xx->dev, "write failed");
1913 devm_kfree(aw883xx->dev, data_buf);
1914 data_buf = NULL;
1915 goto exit;
1916 }
1917
1918 devm_kfree(aw883xx->dev, data_buf);
1919 data_buf = NULL;
1920 aw_dev_info(aw883xx->dev, "write success");
1921 exit:
1922 mutex_unlock(&aw883xx->i2c_lock);
1923 return ret;
1924 }
1925
aw883xx_awrw_data_check(struct aw883xx * aw883xx,int * data)1926 static int aw883xx_awrw_data_check(struct aw883xx *aw883xx, int *data)
1927 {
1928 if ((data[AWRW_HDR_ADDR_BYTES] != AWRW_ADDR_BYTES) ||
1929 (data[AWRW_HDR_DATA_BYTES] != AWRW_DATA_BYTES)) {
1930 aw_dev_err(aw883xx->dev, "addr_bytes [%d] or data_bytes [%d] unsupport",
1931 data[AWRW_HDR_ADDR_BYTES], data[AWRW_HDR_DATA_BYTES]);
1932 return -EINVAL;
1933 }
1934
1935 return 0;
1936 }
1937
1938 /* flag addr_bytes data_bytes reg_num reg_addr*/
aw883xx_awrw_parse_buf(struct aw883xx * aw883xx,const char * buf,size_t count)1939 static int aw883xx_awrw_parse_buf(struct aw883xx *aw883xx, const char *buf, size_t count)
1940 {
1941 int data[AWRW_HDR_MAX] = { 0 };
1942 struct aw883xx_i2c_packet *packet = &aw883xx->i2c_packet;
1943 int ret;
1944
1945 if (sscanf(buf, "0x%02x 0x%02x 0x%02x 0x%02x 0x%02x",
1946 &data[AWRW_HDR_WR_FLAG], &data[AWRW_HDR_ADDR_BYTES], &data[AWRW_HDR_DATA_BYTES],
1947 &data[AWRW_HDR_REG_NUM], &data[AWRW_HDR_REG_ADDR]) == 5) {
1948
1949 ret = aw883xx_awrw_data_check(aw883xx, data);
1950 if (ret < 0)
1951 return ret;
1952
1953 packet->reg_addr = data[AWRW_HDR_REG_ADDR];
1954 packet->reg_num = data[AWRW_HDR_REG_NUM];
1955
1956 if (data[AWRW_HDR_WR_FLAG] == AWRW_FLAG_WRITE) {
1957 return aw883xx_awrw_write(aw883xx, buf, count);
1958 } else if (data[AWRW_HDR_WR_FLAG] == AWRW_FLAG_READ) {
1959 packet->i2c_status = AWRW_I2C_ST_READ;
1960 aw_dev_info(aw883xx->dev, "read_cmd:reg_addr[0x%02x], reg_num[%d]",
1961 packet->reg_addr, packet->reg_num);
1962
1963 } else {
1964 aw_dev_err(aw883xx->dev, "please check str format, unsupport flag %d", data[AWRW_HDR_WR_FLAG]);
1965 return -EINVAL;
1966 }
1967 } else {
1968 aw_dev_err(aw883xx->dev, "can not parse string");
1969 return -EINVAL;
1970 }
1971
1972 return 0;
1973 }
1974
aw883xx_awrw_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1975 static ssize_t aw883xx_awrw_store(struct device *dev,
1976 struct device_attribute *attr, const char *buf, size_t count)
1977 {
1978 struct aw883xx *aw883xx = dev_get_drvdata(dev);
1979 int ret;
1980
1981 if (count < AWRW_HDR_LEN) {
1982 aw_dev_err(dev, "data count too smaller, please check write format");
1983 aw_dev_err(dev, "string %s", buf);
1984 return -EINVAL;
1985 }
1986
1987 ret = aw883xx_awrw_parse_buf(aw883xx, buf, count);
1988 if (ret)
1989 return -EINVAL;
1990
1991
1992 return count;
1993 }
1994
aw883xx_awrw_show(struct device * dev,struct device_attribute * attr,char * buf)1995 static ssize_t aw883xx_awrw_show(struct device *dev,
1996 struct device_attribute *attr, char *buf)
1997 {
1998 struct aw883xx *aw883xx = dev_get_drvdata(dev);
1999 struct aw883xx_i2c_packet *packet = &aw883xx->i2c_packet;
2000 int data_len, len = 0;
2001 int ret, i;
2002 uint8_t *reg_data = NULL;
2003
2004 if (packet->i2c_status != AWRW_I2C_ST_READ) {
2005 aw_dev_err(aw883xx->dev, "please write read cmd first");
2006 return -EINVAL;
2007 }
2008
2009 mutex_lock(&aw883xx->i2c_lock);
2010 if (packet->reg_addr == aw883xx->aw_pa->dsp_mem_desc.dsp_mdat_reg) {
2011 if (packet->dsp_status != AWRW_DSP_READY) {
2012 aw_dev_err(aw883xx->dev, "please write reg[0x40] first");
2013 mutex_unlock(&aw883xx->i2c_lock);
2014 return -EINVAL;
2015 }
2016 ret = aw883xx_i2c_write(aw883xx, aw883xx->aw_pa->dsp_mem_desc.dsp_madd_reg, packet->dsp_addr);
2017 if (ret < 0) {
2018 mutex_unlock(&aw883xx->i2c_lock);
2019 return ret;
2020 }
2021 packet->dsp_status = AWRW_DSP_ST_NONE;
2022 }
2023
2024 data_len = AWRW_DATA_BYTES * packet->reg_num;
2025 reg_data = devm_kzalloc(dev, data_len, GFP_KERNEL);
2026 if (reg_data == NULL) {
2027 aw_dev_err(aw883xx->dev, "memory alloc failed");
2028 ret = -EINVAL;
2029 goto exit;
2030 }
2031
2032 ret = aw883xx_i2c_reads(aw883xx, packet->reg_addr, reg_data, data_len);
2033 if (ret < 0) {
2034 ret = -EFAULT;
2035 goto exit;
2036 }
2037
2038 aw_dev_info(aw883xx->dev, "reg_addr 0x%02x, reg_num %d",
2039 packet->reg_addr, packet->reg_num);
2040
2041 for (i = 0; i < data_len; i++) {
2042 len += snprintf(buf + len, PAGE_SIZE - len,
2043 "0x%02x,", reg_data[i]);
2044
2045 }
2046 ret = len;
2047
2048 exit:
2049 if (reg_data) {
2050 devm_kfree(dev, reg_data);
2051 reg_data = NULL;
2052 }
2053 mutex_unlock(&aw883xx->i2c_lock);
2054 packet->i2c_status = AWRW_I2C_ST_NONE;
2055 return ret;
2056 }
2057
aw883xx_fade_step_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2058 static ssize_t aw883xx_fade_step_store(struct device *dev,
2059 struct device_attribute *attr, const char *buf, size_t count)
2060 {
2061 struct aw883xx *aw883xx = dev_get_drvdata(dev);
2062 unsigned int databuf = 0;
2063
2064 if (1 == sscanf(buf, "%d", &databuf)) {
2065 if (databuf > (aw883xx->aw_pa->volume_desc.mute_volume)) {
2066 aw_dev_info(aw883xx->dev, "step overflow %d Db", databuf);
2067 return count;
2068 }
2069 aw_dev_set_fade_vol_step(aw883xx->aw_pa, databuf);
2070 }
2071 aw_dev_info(aw883xx->dev, "set step %d DB Done", databuf);
2072
2073 return count;
2074 }
2075
aw883xx_fade_step_show(struct device * dev,struct device_attribute * attr,char * buf)2076 static ssize_t aw883xx_fade_step_show(struct device *dev,
2077 struct device_attribute *attr, char *buf)
2078 {
2079 ssize_t len = 0;
2080 struct aw883xx *aw883xx = dev_get_drvdata(dev);
2081
2082 len += snprintf(buf+len, PAGE_SIZE-len,
2083 "step: %d \n", aw_dev_get_fade_vol_step(aw883xx->aw_pa));
2084
2085 return len;
2086 }
2087
aw883xx_dbg_prof_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2088 static ssize_t aw883xx_dbg_prof_store(struct device *dev,
2089 struct device_attribute *attr, const char *buf, size_t count)
2090 {
2091 struct aw883xx *aw883xx = dev_get_drvdata(dev);
2092 unsigned int databuf = 0;
2093
2094 if (1 == sscanf(buf, "%d", &databuf)) {
2095 if (databuf)
2096 aw883xx->dbg_en_prof = true;
2097 else
2098 aw883xx->dbg_en_prof = false;
2099 }
2100 aw_dev_info(aw883xx->dev, "en_prof %d Done", databuf);
2101
2102 return count;
2103 }
2104
aw883xx_dbg_prof_show(struct device * dev,struct device_attribute * attr,char * buf)2105 static ssize_t aw883xx_dbg_prof_show(struct device *dev,
2106 struct device_attribute *attr, char *buf)
2107 {
2108 struct aw883xx *aw883xx = dev_get_drvdata(dev);
2109 ssize_t len = 0;
2110
2111 len += snprintf(buf+len, PAGE_SIZE-len,
2112 " %d\n", aw883xx->dbg_en_prof);
2113
2114 return len;
2115 }
2116
aw883xx_spk_temp_show(struct device * dev,struct device_attribute * attr,char * buf)2117 static ssize_t aw883xx_spk_temp_show(struct device *dev,
2118 struct device_attribute *attr, char *buf)
2119 {
2120 struct aw883xx *aw883xx = dev_get_drvdata(dev);
2121 ssize_t len = 0;
2122 int ret;
2123 int32_t te;
2124
2125 ret = aw_cali_svc_get_dev_te(&aw883xx->aw_pa->cali_desc, &te);
2126 if (ret < 0)
2127 return ret;
2128
2129 len += snprintf(buf + len, PAGE_SIZE - len,
2130 "Temp:%d\n", te);
2131
2132 return len;
2133 }
2134
aw883xx_sync_flag_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2135 static ssize_t aw883xx_sync_flag_store(struct device *dev,
2136 struct device_attribute *attr,
2137 const char *buf, size_t count)
2138 {
2139 struct aw883xx *aw883xx = dev_get_drvdata(dev);
2140 unsigned int flag = 0;
2141 int ret;
2142
2143 ret = kstrtouint(buf, 0, &flag);
2144 if (ret < 0)
2145 return ret;
2146
2147 flag = ((flag == false) ? false : true);
2148
2149 aw_dev_info(aw883xx->dev, "set phase sync flag : [%d]", flag);
2150
2151 aw883xx->phase_sync = flag;
2152
2153 return count;
2154 }
2155
aw883xx_sync_flag_show(struct device * dev,struct device_attribute * attr,char * buf)2156 static ssize_t aw883xx_sync_flag_show(struct device *dev,
2157 struct device_attribute *attr, char *buf)
2158 {
2159 struct aw883xx *aw883xx = dev_get_drvdata(dev);
2160 ssize_t len = 0;
2161
2162 len += snprintf(buf + len, PAGE_SIZE - len,
2163 "sync flag : %d\n", aw883xx->phase_sync);
2164
2165 return len;
2166 }
2167
aw883xx_fade_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2168 static ssize_t aw883xx_fade_enable_store(struct device *dev,
2169 struct device_attribute *attr, const char *buf, size_t count)
2170 {
2171 struct aw883xx *aw883xx = dev_get_drvdata(dev);
2172 uint32_t fade_en = 0;
2173
2174 if (1 == sscanf(buf, "%u", &fade_en))
2175 aw883xx->aw_pa->fade_en = fade_en;
2176
2177 aw_dev_info(aw883xx->dev, "set fade_en %d", aw883xx->aw_pa->fade_en);
2178
2179 return count;
2180 }
2181
aw883xx_fade_enable_show(struct device * dev,struct device_attribute * attr,char * buf)2182 static ssize_t aw883xx_fade_enable_show(struct device *dev,
2183 struct device_attribute *attr, char *buf)
2184 {
2185 struct aw883xx *aw883xx = dev_get_drvdata(dev);
2186 ssize_t len = 0;
2187
2188 len += snprintf(buf+len, PAGE_SIZE-len,
2189 "fade_en: %d\n", aw883xx->aw_pa->fade_en);
2190
2191 return len;
2192 }
2193
aw883xx_dsp_re_show(struct device * dev,struct device_attribute * attr,char * buf)2194 static ssize_t aw883xx_dsp_re_show(struct device *dev,
2195 struct device_attribute *attr, char *buf)
2196 {
2197 struct aw883xx *aw883xx = dev_get_drvdata(dev);
2198 ssize_t len = 0;
2199 int ret;
2200 uint32_t read_re = 0;
2201
2202 ret = aw_cali_read_cali_re_from_dsp(&aw883xx->aw_pa->cali_desc, &read_re);
2203 if (ret < 0) {
2204 aw_dev_err(aw883xx->dev, "%s:read dsp re fail\n", __func__);
2205 return ret;
2206 }
2207
2208 len += snprintf((char *)(buf + len),
2209 PAGE_SIZE - len,
2210 "dsp_re: %d\n", read_re);
2211
2212 return len;
2213 }
2214
aw883xx_log_en_show(struct device * dev,struct device_attribute * attr,char * buf)2215 static ssize_t aw883xx_log_en_show(struct device *dev,
2216 struct device_attribute *attr, char *buf)
2217 {
2218 struct aw883xx *aw883xx = dev_get_drvdata(dev);
2219 ssize_t len = 0;
2220
2221 len += snprintf(buf + len, PAGE_SIZE - len, "i2c_log_en: %d\n",
2222 aw883xx->i2c_log_en);
2223
2224 return len;
2225 }
2226
aw883xx_log_en_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2227 static ssize_t aw883xx_log_en_store(struct device *dev,
2228 struct device_attribute *attr, const char *buf, size_t count)
2229 {
2230 struct aw883xx *aw883xx = dev_get_drvdata(dev);
2231 uint32_t log_en = 0;
2232
2233 if (1 == sscanf(buf, "%u", &log_en))
2234 aw883xx->i2c_log_en = log_en;
2235
2236 aw_dev_info(aw883xx->dev, "set i2c_log_en: %d",
2237 aw883xx->i2c_log_en);
2238
2239 return count;
2240 }
2241
aw883xx_dsp_log_info(struct aw883xx * aw883xx,unsigned int base_addr,uint32_t data_len,char * format)2242 static int aw883xx_dsp_log_info(struct aw883xx *aw883xx, unsigned int base_addr,
2243 uint32_t data_len, char *format)
2244 {
2245 uint16_t reg_val = 0;
2246 char *dsp_reg_info = NULL;
2247 ssize_t dsp_info_len = 0;
2248 int i;
2249
2250 dsp_reg_info = devm_kzalloc(aw883xx->dev, AW_NAME_BUF_MAX, GFP_KERNEL);
2251 if (dsp_reg_info == NULL) {
2252 aw_dev_err(aw883xx->dev, "dsp_reg_info kzalloc failed");
2253 return -ENOMEM;
2254 }
2255
2256 mutex_lock(&aw883xx->i2c_lock);
2257 aw883xx_i2c_write(aw883xx, aw883xx->aw_pa->dsp_mem_desc.dsp_madd_reg, base_addr);
2258
2259 for (i = 0; i < data_len; i += 2) {
2260 aw883xx_i2c_read(aw883xx, aw883xx->aw_pa->dsp_mem_desc.dsp_mdat_reg, ®_val);
2261 dsp_info_len += snprintf(dsp_reg_info + dsp_info_len, AW_NAME_BUF_MAX - dsp_info_len,
2262 "%02x,%02x,", (reg_val >> 0) & 0xff,
2263 (reg_val >> 8) & 0xff);
2264 if ((i / 2 + 1) % 8 == 0) {
2265 aw_dev_info(aw883xx->dev, "%s: %s", format, dsp_reg_info);
2266 dsp_info_len = 0;
2267 memset(dsp_reg_info, 0, AW_NAME_BUF_MAX);
2268 }
2269
2270 if (((data_len) % 8 != 0) &&
2271 (i == (data_len - 2))) {
2272 aw_dev_info(aw883xx->dev, "%s: %s", format, dsp_reg_info);
2273 dsp_info_len = 0;
2274 memset(dsp_reg_info, 0, AW_NAME_BUF_MAX);
2275 }
2276 }
2277
2278 memset(dsp_reg_info, 0, AW_NAME_BUF_MAX);
2279 devm_kfree(aw883xx->dev, dsp_reg_info);
2280 dsp_reg_info = NULL;
2281 mutex_unlock(&aw883xx->i2c_lock);
2282
2283 return 0;
2284 }
2285
aw883xx_dsp_show(struct device * dev,struct device_attribute * attr,char * buf)2286 static ssize_t aw883xx_dsp_show(struct device *dev,
2287 struct device_attribute *attr, char *buf)
2288 {
2289 struct aw883xx *aw883xx = dev_get_drvdata(dev);
2290 ssize_t len = 0;
2291 int ret = -1;
2292 uint32_t data_len;
2293
2294 if (aw883xx->aw_pa->dsp_cfg == AW_DEV_DSP_BYPASS) {
2295 len += snprintf((char *)(buf + len), PAGE_SIZE - len,
2296 "%s: dsp bypass\n", __func__);
2297 } else {
2298 len += snprintf((char *)(buf + len), PAGE_SIZE - len,
2299 "%s: dsp working\n", __func__);
2300 ret = aw_dev_get_iis_status(aw883xx->aw_pa);
2301 if (ret < 0) {
2302 len += snprintf((char *)(buf + len),
2303 PAGE_SIZE - len,
2304 "%s: no iis signal\n",
2305 __func__);
2306 aw_dev_err(aw883xx->dev, "no iis signal, dsp show failed");
2307 return len;
2308 }
2309
2310 len += snprintf(buf + len, PAGE_SIZE - len,
2311 "dsp firmware and config info is displayed in the kernel log\n");
2312
2313 aw_dev_info(aw883xx->dev, "dsp_firmware_len:%d", aw883xx->aw_pa->dsp_fw_len);
2314 ret = aw883xx_dsp_log_info(aw883xx, aw883xx->aw_pa->dsp_mem_desc.dsp_fw_base_addr,
2315 aw883xx->aw_pa->dsp_fw_len, "dsp_fw");
2316 if (ret < 0) {
2317 aw_dev_err(aw883xx->dev, "dsp_fw display failed");
2318 return len;
2319 }
2320
2321 aw_dev_info(aw883xx->dev, "dsp_config_len:%d", aw883xx->aw_pa->dsp_cfg_len);
2322 ret = aw883xx_dsp_log_info(aw883xx, aw883xx->aw_pa->dsp_mem_desc.dsp_cfg_base_addr,
2323 aw883xx->aw_pa->dsp_cfg_len, "dsp_config");
2324 if (ret < 0) {
2325 aw_dev_err(aw883xx->dev, "dsp_config display failed");
2326 return len;
2327 }
2328
2329 aw_dev_info(aw883xx->dev, "dsp_config:0x8180-0x83fc");
2330 data_len = 2 * (aw883xx->aw_pa->dsp_st_desc.dsp_reg_e1 -
2331 aw883xx->aw_pa->dsp_st_desc.dsp_reg_s1);
2332 ret = aw883xx_dsp_log_info(aw883xx, aw883xx->aw_pa->dsp_st_desc.dsp_reg_s1,
2333 data_len, "dsp_st");
2334 if (ret < 0) {
2335 aw_dev_err(aw883xx->dev, "dsp_config:0x8180-0x83fc failed");
2336 return len;
2337 }
2338
2339 aw_dev_info(aw883xx->dev, "dsp_config:0x9c00-0x9c5c");
2340 data_len = 2 * (aw883xx->aw_pa->dsp_st_desc.dsp_reg_e2 -
2341 aw883xx->aw_pa->dsp_st_desc.dsp_reg_s2);
2342 ret = aw883xx_dsp_log_info(aw883xx, aw883xx->aw_pa->dsp_st_desc.dsp_reg_s2,
2343 data_len, "dsp_st");
2344 if (ret < 0) {
2345 aw_dev_err(aw883xx->dev, "dsp_config:0x9c00-0x9c5c display failed");
2346 return len;
2347 }
2348 }
2349 return len;
2350 }
2351
2352 static DEVICE_ATTR(reg, S_IWUSR | S_IRUGO,
2353 aw883xx_reg_show, aw883xx_reg_store);
2354 static DEVICE_ATTR(rw, S_IWUSR | S_IRUGO,
2355 aw883xx_rw_show, aw883xx_rw_store);
2356 static DEVICE_ATTR(drv_ver, S_IRUGO,
2357 aw883xx_drv_ver_show, NULL);
2358 static DEVICE_ATTR(dsp_rw, S_IWUSR | S_IRUGO,
2359 aw883xx_dsp_rw_show, aw883xx_dsp_rw_store);
2360 static DEVICE_ATTR(awrw, S_IWUSR | S_IRUGO,
2361 aw883xx_awrw_show, aw883xx_awrw_store);
2362 static DEVICE_ATTR(fade_step, S_IWUSR | S_IRUGO,
2363 aw883xx_fade_step_show, aw883xx_fade_step_store);
2364 static DEVICE_ATTR(dbg_prof, S_IWUSR | S_IRUGO,
2365 aw883xx_dbg_prof_show, aw883xx_dbg_prof_store);
2366 static DEVICE_ATTR(spk_temp, S_IRUGO,
2367 aw883xx_spk_temp_show, NULL);
2368 static DEVICE_ATTR(phase_sync, S_IWUSR | S_IRUGO,
2369 aw883xx_sync_flag_show, aw883xx_sync_flag_store);
2370 static DEVICE_ATTR(fade_en, S_IWUSR | S_IRUGO,
2371 aw883xx_fade_enable_show, aw883xx_fade_enable_store);
2372 static DEVICE_ATTR(dsp_re, S_IRUGO,
2373 aw883xx_dsp_re_show, NULL);
2374 static DEVICE_ATTR(i2c_log_en, S_IWUSR | S_IRUGO,
2375 aw883xx_log_en_show, aw883xx_log_en_store);
2376 static DEVICE_ATTR(dsp, S_IRUGO,
2377 aw883xx_dsp_show, NULL);
2378
2379
2380 static struct attribute *aw883xx_attributes[] = {
2381 &dev_attr_reg.attr,
2382 &dev_attr_rw.attr,
2383 &dev_attr_drv_ver.attr,
2384 &dev_attr_dsp_rw.attr,
2385 &dev_attr_awrw.attr,
2386 &dev_attr_fade_step.attr,
2387 &dev_attr_dbg_prof.attr,
2388 &dev_attr_spk_temp.attr,
2389 &dev_attr_phase_sync.attr,
2390 &dev_attr_fade_en.attr,
2391 &dev_attr_dsp_re.attr,
2392 &dev_attr_i2c_log_en.attr,
2393 &dev_attr_dsp.attr,
2394 NULL
2395 };
2396
2397 static struct attribute_group aw883xx_attribute_group = {
2398 .attrs = aw883xx_attributes
2399 };
2400
2401
2402
2403 /******************************************************
2404 *
2405 * i2c driver
2406 *
2407 ******************************************************/
aw883xx_i2c_probe(struct i2c_client * i2c,const struct i2c_device_id * id)2408 static int aw883xx_i2c_probe(struct i2c_client *i2c,
2409 const struct i2c_device_id *id)
2410 {
2411 struct aw883xx *aw883xx = NULL;
2412 int ret = -1;
2413
2414 aw_dev_info(&i2c->dev, "enter");
2415
2416 if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_I2C)) {
2417 aw_dev_err(&i2c->dev, "check_functionality failed");
2418 return -EIO;
2419 }
2420
2421 aw883xx = aw883xx_malloc_init(i2c);
2422 if (aw883xx == NULL) {
2423 aw_dev_err(&i2c->dev, "malloc aw883xx failed");
2424 return -ENOMEM;
2425 }
2426 i2c_set_clientdata(i2c, aw883xx);
2427
2428 ret = aw883xx_parse_dt(aw883xx);
2429 if (ret < 0) {
2430 aw_dev_err(&i2c->dev, "parse dts failed");
2431 return ret;
2432 }
2433
2434 /*get gpio resource*/
2435 ret = aw883xx_gpio_request(aw883xx);
2436 if (ret)
2437 return ret;
2438
2439 /* hardware reset */
2440 aw883xx_hw_reset(aw883xx);
2441
2442 /* aw883xx chip id */
2443 ret = aw883xx_read_chipid(aw883xx);
2444 if (ret < 0) {
2445 aw_dev_err(&i2c->dev, "aw883xx_read_chipid failed ret=%d", ret);
2446 return ret;
2447 }
2448
2449 /*aw pa init*/
2450 ret = aw883xx_init(aw883xx);
2451 if (ret < 0)
2452 return ret;
2453
2454 ret = aw883xx_platform_init(aw883xx);
2455 if (ret < 0) {
2456 aw_dev_err(&i2c->dev, "get platform failed");
2457 return ret;
2458 }
2459
2460 ret = aw883xx_interrupt_init(aw883xx);
2461 if (ret < 0)
2462 return ret;
2463
2464 ret = aw883xx_componet_codec_register(aw883xx);
2465 if (ret) {
2466 aw_dev_err(&i2c->dev, "codec register failed");
2467 return ret;
2468 }
2469
2470 ret = sysfs_create_group(&i2c->dev.kobj, &aw883xx_attribute_group);
2471 if (ret < 0) {
2472 aw_dev_info(&i2c->dev, "error creating sysfs attr files");
2473 goto err_sysfs;
2474 }
2475
2476 dev_set_drvdata(&i2c->dev, aw883xx);
2477
2478 /*add device to total list*/
2479 mutex_lock(&g_aw883xx_lock);
2480 g_aw883xx_dev_cnt++;
2481 mutex_unlock(&g_aw883xx_lock);
2482
2483 aw_dev_info(&i2c->dev, "dev_cnt %d probe completed successfully",
2484 g_aw883xx_dev_cnt);
2485
2486 return 0;
2487
2488
2489 err_sysfs:
2490 aw_componet_codec_ops.unregister_codec(&i2c->dev);
2491 return ret;
2492 }
2493
aw883xx_i2c_remove(struct i2c_client * i2c)2494 static int aw883xx_i2c_remove(struct i2c_client *i2c)
2495 {
2496 struct aw883xx *aw883xx = i2c_get_clientdata(i2c);
2497
2498 aw_dev_info(aw883xx->dev, "enter");
2499
2500 if (gpio_to_irq(aw883xx->irq_gpio))
2501 devm_free_irq(&i2c->dev,
2502 gpio_to_irq(aw883xx->irq_gpio),
2503 aw883xx);
2504
2505 if (gpio_is_valid(aw883xx->irq_gpio))
2506 devm_gpio_free(&i2c->dev, aw883xx->irq_gpio);
2507 if (gpio_is_valid(aw883xx->reset_gpio))
2508 devm_gpio_free(&i2c->dev, aw883xx->reset_gpio);
2509
2510 sysfs_remove_group(&aw883xx->dev->kobj,
2511 &aw883xx_attribute_group);
2512
2513 /*free device resource */
2514 aw_device_remove(aw883xx->aw_pa);
2515
2516 aw_componet_codec_ops.unregister_codec(&i2c->dev);
2517
2518 mutex_lock(&g_aw883xx_lock);
2519 g_aw883xx_dev_cnt--;
2520 if (g_aw883xx_dev_cnt == 0) {
2521 vfree(g_awinic_cfg);
2522 g_awinic_cfg = NULL;
2523 }
2524 mutex_unlock(&g_aw883xx_lock);
2525
2526 return 0;
2527 }
2528
2529 static const struct i2c_device_id aw883xx_i2c_id[] = {
2530 {AW883XX_I2C_NAME, 0},
2531 {}
2532 };
2533
2534 MODULE_DEVICE_TABLE(i2c, aw883xx_i2c_id);
2535
2536 static struct of_device_id aw883xx_dt_match[] = {
2537 {.compatible = "awinic,aw883xx_smartpa"},
2538 {},
2539 };
2540
2541 static struct i2c_driver aw883xx_i2c_driver = {
2542 .driver = {
2543 .name = AW883XX_I2C_NAME,
2544 .owner = THIS_MODULE,
2545 .of_match_table = of_match_ptr(aw883xx_dt_match),
2546 },
2547 .probe = aw883xx_i2c_probe,
2548 .remove = aw883xx_i2c_remove,
2549 .id_table = aw883xx_i2c_id,
2550 };
2551
aw883xx_i2c_init(void)2552 static int __init aw883xx_i2c_init(void)
2553 {
2554 int ret = -1;
2555
2556 aw_pr_info("aw883xx driver version %s", AW883XX_DRIVER_VERSION);
2557
2558 ret = i2c_add_driver(&aw883xx_i2c_driver);
2559 if (ret)
2560 aw_pr_err("fail to add aw883xx device into i2c");
2561
2562 return ret;
2563 }
2564 module_init(aw883xx_i2c_init);
2565
aw883xx_i2c_exit(void)2566 static void __exit aw883xx_i2c_exit(void)
2567 {
2568 i2c_del_driver(&aw883xx_i2c_driver);
2569 }
2570 module_exit(aw883xx_i2c_exit);
2571
2572 MODULE_DESCRIPTION("ASoC AW883XX Smart PA Driver");
2573 MODULE_LICENSE("GPL v2");
2574
2575