1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // aw88261.c -- AW88261 ALSA SoC Audio driver
4 //
5 // Copyright (c) 2023 awinic Technology CO., LTD
6 //
7 // Author: Jimmy Zhang <zhangjianming@awinic.com>
8 // Author: Weidong Wang <wangweidong.a@awinic.com>
9 //
10
11 #include <linux/i2c.h>
12 #include <linux/firmware.h>
13 #include <linux/of_gpio.h>
14 #include <linux/regmap.h>
15 #include <sound/soc.h>
16 #include "aw88261.h"
17 #include "aw88395/aw88395_data_type.h"
18 #include "aw88395/aw88395_device.h"
19
20 static const struct regmap_config aw88261_remap_config = {
21 .val_bits = 16,
22 .reg_bits = 8,
23 .max_register = AW88261_REG_MAX - 1,
24 .reg_format_endian = REGMAP_ENDIAN_LITTLE,
25 .val_format_endian = REGMAP_ENDIAN_BIG,
26 };
27
aw88261_dev_set_volume(struct aw_device * aw_dev,unsigned int value)28 static void aw88261_dev_set_volume(struct aw_device *aw_dev, unsigned int value)
29 {
30 struct aw_volume_desc *vol_desc = &aw_dev->volume_desc;
31 unsigned int real_value, volume;
32 unsigned int reg_value;
33
34 volume = min((value + vol_desc->init_volume), (unsigned int)AW88261_MUTE_VOL);
35 real_value = DB_TO_REG_VAL(volume);
36
37 regmap_read(aw_dev->regmap, AW88261_SYSCTRL2_REG, ®_value);
38
39 real_value = (real_value | (reg_value & AW88261_VOL_START_MASK));
40
41 dev_dbg(aw_dev->dev, "value 0x%x , real_value:0x%x", value, real_value);
42
43 regmap_write(aw_dev->regmap, AW88261_SYSCTRL2_REG, real_value);
44 }
45
aw88261_dev_fade_in(struct aw_device * aw_dev)46 static void aw88261_dev_fade_in(struct aw_device *aw_dev)
47 {
48 struct aw_volume_desc *desc = &aw_dev->volume_desc;
49 int fade_in_vol = desc->ctl_volume;
50 int fade_step = aw_dev->fade_step;
51 int i;
52
53 if (fade_step == 0 || aw_dev->fade_in_time == 0) {
54 aw88261_dev_set_volume(aw_dev, fade_in_vol);
55 return;
56 }
57
58 for (i = AW88261_MUTE_VOL; i >= fade_in_vol; i -= fade_step) {
59 aw88261_dev_set_volume(aw_dev, i);
60 usleep_range(aw_dev->fade_in_time,
61 aw_dev->fade_in_time + 10);
62 }
63
64 if (i != fade_in_vol)
65 aw88261_dev_set_volume(aw_dev, fade_in_vol);
66 }
67
aw88261_dev_fade_out(struct aw_device * aw_dev)68 static void aw88261_dev_fade_out(struct aw_device *aw_dev)
69 {
70 struct aw_volume_desc *desc = &aw_dev->volume_desc;
71 int fade_step = aw_dev->fade_step;
72 int i;
73
74 if (fade_step == 0 || aw_dev->fade_out_time == 0) {
75 aw88261_dev_set_volume(aw_dev, AW88261_MUTE_VOL);
76 return;
77 }
78
79 for (i = desc->ctl_volume; i <= AW88261_MUTE_VOL; i += fade_step) {
80 aw88261_dev_set_volume(aw_dev, i);
81 usleep_range(aw_dev->fade_out_time, aw_dev->fade_out_time + 10);
82 }
83
84 if (i != AW88261_MUTE_VOL) {
85 aw88261_dev_set_volume(aw_dev, AW88261_MUTE_VOL);
86 usleep_range(aw_dev->fade_out_time, aw_dev->fade_out_time + 10);
87 }
88 }
89
aw88261_dev_i2s_tx_enable(struct aw_device * aw_dev,bool flag)90 static void aw88261_dev_i2s_tx_enable(struct aw_device *aw_dev, bool flag)
91 {
92 if (flag)
93 regmap_update_bits(aw_dev->regmap, AW88261_I2SCFG1_REG,
94 ~AW88261_I2STXEN_MASK, AW88261_I2STXEN_ENABLE_VALUE);
95 else
96 regmap_update_bits(aw_dev->regmap, AW88261_I2SCFG1_REG,
97 ~AW88261_I2STXEN_MASK, AW88261_I2STXEN_DISABLE_VALUE);
98 }
99
aw88261_dev_pwd(struct aw_device * aw_dev,bool pwd)100 static void aw88261_dev_pwd(struct aw_device *aw_dev, bool pwd)
101 {
102 if (pwd)
103 regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
104 ~AW88261_PWDN_MASK, AW88261_PWDN_POWER_DOWN_VALUE);
105 else
106 regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
107 ~AW88261_PWDN_MASK, AW88261_PWDN_WORKING_VALUE);
108 }
109
aw88261_dev_amppd(struct aw_device * aw_dev,bool amppd)110 static void aw88261_dev_amppd(struct aw_device *aw_dev, bool amppd)
111 {
112 if (amppd)
113 regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
114 ~AW88261_AMPPD_MASK, AW88261_AMPPD_POWER_DOWN_VALUE);
115 else
116 regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
117 ~AW88261_AMPPD_MASK, AW88261_AMPPD_WORKING_VALUE);
118 }
119
aw88261_dev_mute(struct aw_device * aw_dev,bool is_mute)120 static void aw88261_dev_mute(struct aw_device *aw_dev, bool is_mute)
121 {
122 if (is_mute) {
123 aw88261_dev_fade_out(aw_dev);
124 regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
125 ~AW88261_HMUTE_MASK, AW88261_HMUTE_ENABLE_VALUE);
126 } else {
127 regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
128 ~AW88261_HMUTE_MASK, AW88261_HMUTE_DISABLE_VALUE);
129 aw88261_dev_fade_in(aw_dev);
130 }
131 }
132
aw88261_dev_clear_int_status(struct aw_device * aw_dev)133 static void aw88261_dev_clear_int_status(struct aw_device *aw_dev)
134 {
135 unsigned int int_status;
136
137 /* read int status and clear */
138 regmap_read(aw_dev->regmap, AW88261_SYSINT_REG, &int_status);
139 /* make sure int status is clear */
140 regmap_read(aw_dev->regmap, AW88261_SYSINT_REG, &int_status);
141
142 dev_dbg(aw_dev->dev, "read interrupt reg = 0x%04x", int_status);
143 }
144
aw88261_dev_get_iis_status(struct aw_device * aw_dev)145 static int aw88261_dev_get_iis_status(struct aw_device *aw_dev)
146 {
147 unsigned int reg_val;
148 int ret;
149
150 ret = regmap_read(aw_dev->regmap, AW88261_SYSST_REG, ®_val);
151 if (ret)
152 return ret;
153 if ((reg_val & AW88261_BIT_PLL_CHECK) != AW88261_BIT_PLL_CHECK) {
154 dev_err(aw_dev->dev, "check pll lock fail,reg_val:0x%04x", reg_val);
155 return -EINVAL;
156 }
157
158 return ret;
159 }
160
aw88261_dev_check_mode1_pll(struct aw_device * aw_dev)161 static int aw88261_dev_check_mode1_pll(struct aw_device *aw_dev)
162 {
163 int ret, i;
164
165 for (i = 0; i < AW88261_DEV_SYSST_CHECK_MAX; i++) {
166 ret = aw88261_dev_get_iis_status(aw_dev);
167 if (ret) {
168 dev_err(aw_dev->dev, "mode1 iis signal check error");
169 usleep_range(AW88261_2000_US, AW88261_2000_US + 10);
170 } else {
171 return ret;
172 }
173 }
174
175 return -EPERM;
176 }
177
aw88261_dev_check_mode2_pll(struct aw_device * aw_dev)178 static int aw88261_dev_check_mode2_pll(struct aw_device *aw_dev)
179 {
180 unsigned int reg_val;
181 int ret, i;
182
183 ret = regmap_read(aw_dev->regmap, AW88261_PLLCTRL1_REG, ®_val);
184 if (ret)
185 return ret;
186
187 reg_val &= (~AW88261_CCO_MUX_MASK);
188 if (reg_val == AW88261_CCO_MUX_DIVIDED_VALUE) {
189 dev_dbg(aw_dev->dev, "CCO_MUX is already divider");
190 return -EPERM;
191 }
192
193 /* change mode2 */
194 ret = regmap_update_bits(aw_dev->regmap, AW88261_PLLCTRL1_REG,
195 ~AW88261_CCO_MUX_MASK, AW88261_CCO_MUX_DIVIDED_VALUE);
196 if (ret)
197 return ret;
198
199 for (i = 0; i < AW88261_DEV_SYSST_CHECK_MAX; i++) {
200 ret = aw88261_dev_get_iis_status(aw_dev);
201 if (ret) {
202 dev_err(aw_dev->dev, "mode2 iis signal check error");
203 usleep_range(AW88261_2000_US, AW88261_2000_US + 10);
204 } else {
205 break;
206 }
207 }
208
209 /* change mode1 */
210 ret = regmap_update_bits(aw_dev->regmap, AW88261_PLLCTRL1_REG,
211 ~AW88261_CCO_MUX_MASK, AW88261_CCO_MUX_BYPASS_VALUE);
212 if (ret == 0) {
213 usleep_range(AW88261_2000_US, AW88261_2000_US + 10);
214 for (i = 0; i < AW88261_DEV_SYSST_CHECK_MAX; i++) {
215 ret = aw88261_dev_check_mode1_pll(aw_dev);
216 if (ret) {
217 dev_err(aw_dev->dev, "mode2 switch to mode1, iis signal check error");
218 usleep_range(AW88261_2000_US, AW88261_2000_US + 10);
219 } else {
220 break;
221 }
222 }
223 }
224
225 return ret;
226 }
227
aw88261_dev_check_syspll(struct aw_device * aw_dev)228 static int aw88261_dev_check_syspll(struct aw_device *aw_dev)
229 {
230 int ret;
231
232 ret = aw88261_dev_check_mode1_pll(aw_dev);
233 if (ret) {
234 dev_dbg(aw_dev->dev, "mode1 check iis failed try switch to mode2 check");
235 ret = aw88261_dev_check_mode2_pll(aw_dev);
236 if (ret) {
237 dev_err(aw_dev->dev, "mode2 check iis failed");
238 return ret;
239 }
240 }
241
242 return ret;
243 }
244
aw88261_dev_check_sysst(struct aw_device * aw_dev)245 static int aw88261_dev_check_sysst(struct aw_device *aw_dev)
246 {
247 unsigned int check_val;
248 unsigned int reg_val;
249 int ret, i;
250
251 for (i = 0; i < AW88261_DEV_SYSST_CHECK_MAX; i++) {
252 ret = regmap_read(aw_dev->regmap, AW88261_SYSST_REG, ®_val);
253 if (ret)
254 return ret;
255
256 check_val = reg_val & (~AW88261_BIT_SYSST_CHECK_MASK)
257 & AW88261_BIT_SYSST_CHECK;
258 if (check_val != AW88261_BIT_SYSST_CHECK) {
259 dev_err(aw_dev->dev, "check sysst fail, reg_val=0x%04x, check:0x%x",
260 reg_val, AW88261_BIT_SYSST_CHECK);
261 usleep_range(AW88261_2000_US, AW88261_2000_US + 10);
262 } else {
263 return 0;
264 }
265 }
266
267 return -EPERM;
268 }
269
aw88261_dev_uls_hmute(struct aw_device * aw_dev,bool uls_hmute)270 static void aw88261_dev_uls_hmute(struct aw_device *aw_dev, bool uls_hmute)
271 {
272 if (uls_hmute)
273 regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
274 ~AW88261_ULS_HMUTE_MASK,
275 AW88261_ULS_HMUTE_ENABLE_VALUE);
276 else
277 regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
278 ~AW88261_ULS_HMUTE_MASK,
279 AW88261_ULS_HMUTE_DISABLE_VALUE);
280 }
281
aw88261_reg_force_set(struct aw88261 * aw88261)282 static void aw88261_reg_force_set(struct aw88261 *aw88261)
283 {
284 if (aw88261->frcset_en == AW88261_FRCSET_ENABLE) {
285 /* set FORCE_PWM */
286 regmap_update_bits(aw88261->regmap, AW88261_BSTCTRL3_REG,
287 AW88261_FORCE_PWM_MASK, AW88261_FORCE_PWM_FORCEMINUS_PWM_VALUE);
288 /* set BOOST_OS_WIDTH */
289 regmap_update_bits(aw88261->regmap, AW88261_BSTCTRL5_REG,
290 AW88261_BST_OS_WIDTH_MASK, AW88261_BST_OS_WIDTH_50NS_VALUE);
291 /* set BURST_LOOPR */
292 regmap_update_bits(aw88261->regmap, AW88261_BSTCTRL6_REG,
293 AW88261_BST_LOOPR_MASK, AW88261_BST_LOOPR_340K_VALUE);
294 /* set RSQN_DLY */
295 regmap_update_bits(aw88261->regmap, AW88261_BSTCTRL7_REG,
296 AW88261_RSQN_DLY_MASK, AW88261_RSQN_DLY_35NS_VALUE);
297 /* set BURST_SSMODE */
298 regmap_update_bits(aw88261->regmap, AW88261_BSTCTRL8_REG,
299 AW88261_BURST_SSMODE_MASK, AW88261_BURST_SSMODE_FAST_VALUE);
300 /* set BST_BURST */
301 regmap_update_bits(aw88261->regmap, AW88261_BSTCTRL9_REG,
302 AW88261_BST_BURST_MASK, AW88261_BST_BURST_30MA_VALUE);
303 } else {
304 dev_dbg(aw88261->aw_pa->dev, "needn't set reg value");
305 }
306 }
307
aw88261_dev_get_icalk(struct aw_device * aw_dev,int16_t * icalk)308 static int aw88261_dev_get_icalk(struct aw_device *aw_dev, int16_t *icalk)
309 {
310 u16 reg_icalk, reg_icalkl;
311 unsigned int reg_val;
312 int ret;
313
314 ret = regmap_read(aw_dev->regmap, AW88261_EFRH4_REG, ®_val);
315 if (ret)
316 return ret;
317
318 reg_icalk = reg_val & (~AW88261_EF_ISN_GESLP_H_MASK);
319
320 ret = regmap_read(aw_dev->regmap, AW88261_EFRL4_REG, ®_val);
321 if (ret)
322 return ret;
323
324 reg_icalkl = reg_val & (~AW88261_EF_ISN_GESLP_L_MASK);
325
326 reg_icalk = (reg_icalk >> AW88261_ICALK_SHIFT) & (reg_icalkl >> AW88261_ICALKL_SHIFT);
327
328 if (reg_icalk & (~AW88261_EF_ISN_GESLP_SIGN_MASK))
329 reg_icalk = reg_icalk | ~AW88261_EF_ISN_GESLP_NEG;
330
331 *icalk = (int16_t)reg_icalk;
332
333 return ret;
334 }
335
aw88261_dev_get_vcalk(struct aw_device * aw_dev,int16_t * vcalk)336 static int aw88261_dev_get_vcalk(struct aw_device *aw_dev, int16_t *vcalk)
337 {
338 u16 reg_vcalk, reg_vcalkl;
339 unsigned int reg_val;
340 int ret;
341
342 ret = regmap_read(aw_dev->regmap, AW88261_EFRH3_REG, ®_val);
343 if (ret)
344 return ret;
345
346 reg_vcalk = (u16)reg_val & (~AW88261_EF_VSN_GESLP_H_MASK);
347
348 ret = regmap_read(aw_dev->regmap, AW88261_EFRL3_REG, ®_val);
349 if (ret)
350 return ret;
351
352 reg_vcalkl = (u16)reg_val & (~AW88261_EF_VSN_GESLP_L_MASK);
353
354 reg_vcalk = (reg_vcalk >> AW88261_VCALK_SHIFT) & (reg_vcalkl >> AW88261_VCALKL_SHIFT);
355
356 if (reg_vcalk & AW88261_EF_VSN_GESLP_SIGN_MASK)
357 reg_vcalk = reg_vcalk | (~AW88261_EF_VSN_GESLP_NEG);
358 *vcalk = (int16_t)reg_vcalk;
359
360 return ret;
361 }
362
aw88261_dev_set_vcalb(struct aw_device * aw_dev)363 static int aw88261_dev_set_vcalb(struct aw_device *aw_dev)
364 {
365 int16_t icalk_val, vcalk_val;
366 int icalk, vcalk, vcalb;
367 u32 reg_val;
368 int ret;
369
370 ret = aw88261_dev_get_icalk(aw_dev, &icalk_val);
371 if (ret)
372 return ret;
373
374 ret = aw88261_dev_get_vcalk(aw_dev, &vcalk_val);
375 if (ret)
376 return ret;
377
378 icalk = AW88261_CABL_BASE_VALUE + AW88261_ICABLK_FACTOR * icalk_val;
379 vcalk = AW88261_CABL_BASE_VALUE + AW88261_VCABLK_FACTOR * vcalk_val;
380 if (!vcalk)
381 return -EINVAL;
382
383 vcalb = AW88261_VCAL_FACTOR * icalk / vcalk;
384 reg_val = (unsigned int)vcalb;
385
386 dev_dbg(aw_dev->dev, "icalk=%d, vcalk=%d, vcalb=%d, reg_val=0x%04x",
387 icalk, vcalk, vcalb, reg_val);
388 ret = regmap_write(aw_dev->regmap, AW88261_VSNTM1_REG, reg_val);
389
390 return ret;
391 }
392
aw88261_dev_reg_update(struct aw88261 * aw88261,unsigned char * data,unsigned int len)393 static int aw88261_dev_reg_update(struct aw88261 *aw88261,
394 unsigned char *data, unsigned int len)
395 {
396 struct aw_device *aw_dev = aw88261->aw_pa;
397 struct aw_volume_desc *vol_desc = &aw_dev->volume_desc;
398 unsigned int read_val, efcheck_val, read_vol;
399 int data_len, i, ret;
400 int16_t *reg_data;
401 u16 reg_val;
402 u8 reg_addr;
403
404 if (!len || !data) {
405 dev_err(aw_dev->dev, "reg data is null or len is 0");
406 return -EINVAL;
407 }
408
409 reg_data = (int16_t *)data;
410 data_len = len >> 1;
411
412 if (data_len & 0x1) {
413 dev_err(aw_dev->dev, "data len:%d unsupported", data_len);
414 return -EINVAL;
415 }
416
417 for (i = 0; i < data_len; i += 2) {
418 reg_addr = reg_data[i];
419 reg_val = reg_data[i + 1];
420
421 if (reg_addr == AW88261_SYSCTRL_REG) {
422 aw88261->amppd_st = reg_val & (~AW88261_AMPPD_MASK);
423 ret = regmap_read(aw_dev->regmap, reg_addr, &read_val);
424 if (ret)
425 break;
426
427 read_val &= (~AW88261_AMPPD_MASK) | (~AW88261_PWDN_MASK) |
428 (~AW88261_HMUTE_MASK);
429 reg_val &= (AW88261_AMPPD_MASK | AW88261_PWDN_MASK | AW88261_HMUTE_MASK);
430 reg_val |= read_val;
431
432 /* enable uls hmute */
433 reg_val &= AW88261_ULS_HMUTE_MASK;
434 reg_val |= AW88261_ULS_HMUTE_ENABLE_VALUE;
435 }
436
437 if (reg_addr == AW88261_DBGCTRL_REG) {
438 efcheck_val = reg_val & (~AW88261_EF_DBMD_MASK);
439 if (efcheck_val == AW88261_OR_VALUE)
440 aw88261->efuse_check = AW88261_EF_OR_CHECK;
441 else
442 aw88261->efuse_check = AW88261_EF_AND_CHECK;
443 }
444
445 /* i2stxen */
446 if (reg_addr == AW88261_I2SCTRL3_REG) {
447 /* close tx */
448 reg_val &= AW88261_I2STXEN_MASK;
449 reg_val |= AW88261_I2STXEN_DISABLE_VALUE;
450 }
451
452 if (reg_addr == AW88261_SYSCTRL2_REG) {
453 read_vol = (reg_val & (~AW88261_VOL_MASK)) >>
454 AW88261_VOL_START_BIT;
455 aw_dev->volume_desc.init_volume =
456 REG_VAL_TO_DB(read_vol);
457 }
458
459 if (reg_addr == AW88261_VSNTM1_REG)
460 continue;
461
462 ret = regmap_write(aw_dev->regmap, reg_addr, reg_val);
463 if (ret)
464 break;
465 }
466
467 ret = aw88261_dev_set_vcalb(aw_dev);
468 if (ret)
469 return ret;
470
471 if (aw_dev->prof_cur != aw_dev->prof_index)
472 vol_desc->ctl_volume = 0;
473
474 /* keep min volume */
475 aw88261_dev_set_volume(aw_dev, vol_desc->mute_volume);
476
477 return ret;
478 }
479
aw88261_dev_get_prof_name(struct aw_device * aw_dev,int index)480 static char *aw88261_dev_get_prof_name(struct aw_device *aw_dev, int index)
481 {
482 struct aw_prof_info *prof_info = &aw_dev->prof_info;
483 struct aw_prof_desc *prof_desc;
484
485 if ((index >= aw_dev->prof_info.count) || (index < 0)) {
486 dev_err(aw_dev->dev, "index[%d] overflow count[%d]",
487 index, aw_dev->prof_info.count);
488 return NULL;
489 }
490
491 prof_desc = &aw_dev->prof_info.prof_desc[index];
492
493 return prof_info->prof_name_list[prof_desc->id];
494 }
495
aw88261_dev_get_prof_data(struct aw_device * aw_dev,int index,struct aw_prof_desc ** prof_desc)496 static int aw88261_dev_get_prof_data(struct aw_device *aw_dev, int index,
497 struct aw_prof_desc **prof_desc)
498 {
499 if ((index >= aw_dev->prof_info.count) || (index < 0)) {
500 dev_err(aw_dev->dev, "%s: index[%d] overflow count[%d]\n",
501 __func__, index, aw_dev->prof_info.count);
502 return -EINVAL;
503 }
504
505 *prof_desc = &aw_dev->prof_info.prof_desc[index];
506
507 return 0;
508 }
509
aw88261_dev_fw_update(struct aw88261 * aw88261)510 static int aw88261_dev_fw_update(struct aw88261 *aw88261)
511 {
512 struct aw_device *aw_dev = aw88261->aw_pa;
513 struct aw_prof_desc *prof_index_desc;
514 struct aw_sec_data_desc *sec_desc;
515 char *prof_name;
516 int ret;
517
518 prof_name = aw88261_dev_get_prof_name(aw_dev, aw_dev->prof_index);
519 if (!prof_name) {
520 dev_err(aw_dev->dev, "get prof name failed");
521 return -EINVAL;
522 }
523
524 dev_dbg(aw_dev->dev, "start update %s", prof_name);
525
526 ret = aw88261_dev_get_prof_data(aw_dev, aw_dev->prof_index, &prof_index_desc);
527 if (ret)
528 return ret;
529
530 /* update reg */
531 sec_desc = prof_index_desc->sec_desc;
532 ret = aw88261_dev_reg_update(aw88261, sec_desc[AW88395_DATA_TYPE_REG].data,
533 sec_desc[AW88395_DATA_TYPE_REG].len);
534 if (ret) {
535 dev_err(aw_dev->dev, "update reg failed");
536 return ret;
537 }
538
539 aw_dev->prof_cur = aw_dev->prof_index;
540
541 return ret;
542 }
543
aw88261_dev_start(struct aw88261 * aw88261)544 static int aw88261_dev_start(struct aw88261 *aw88261)
545 {
546 struct aw_device *aw_dev = aw88261->aw_pa;
547 int ret;
548
549 if (aw_dev->status == AW88261_DEV_PW_ON) {
550 dev_info(aw_dev->dev, "already power on");
551 return 0;
552 }
553
554 /* power on */
555 aw88261_dev_pwd(aw_dev, false);
556 usleep_range(AW88261_2000_US, AW88261_2000_US + 10);
557
558 ret = aw88261_dev_check_syspll(aw_dev);
559 if (ret) {
560 dev_err(aw_dev->dev, "pll check failed cannot start");
561 goto pll_check_fail;
562 }
563
564 /* amppd on */
565 aw88261_dev_amppd(aw_dev, false);
566 usleep_range(AW88261_1000_US, AW88261_1000_US + 50);
567
568 /* check i2s status */
569 ret = aw88261_dev_check_sysst(aw_dev);
570 if (ret) {
571 dev_err(aw_dev->dev, "sysst check failed");
572 goto sysst_check_fail;
573 }
574
575 /* enable tx feedback */
576 aw88261_dev_i2s_tx_enable(aw_dev, true);
577
578 if (aw88261->amppd_st)
579 aw88261_dev_amppd(aw_dev, true);
580
581 aw88261_reg_force_set(aw88261);
582
583 /* close uls mute */
584 aw88261_dev_uls_hmute(aw_dev, false);
585
586 /* close mute */
587 if (!aw88261->mute_st)
588 aw88261_dev_mute(aw_dev, false);
589
590 /* clear inturrupt */
591 aw88261_dev_clear_int_status(aw_dev);
592 aw_dev->status = AW88261_DEV_PW_ON;
593
594 return 0;
595
596 sysst_check_fail:
597 aw88261_dev_i2s_tx_enable(aw_dev, false);
598 aw88261_dev_clear_int_status(aw_dev);
599 aw88261_dev_amppd(aw_dev, true);
600 pll_check_fail:
601 aw88261_dev_pwd(aw_dev, true);
602 aw_dev->status = AW88261_DEV_PW_OFF;
603
604 return ret;
605 }
606
aw88261_dev_stop(struct aw_device * aw_dev)607 static int aw88261_dev_stop(struct aw_device *aw_dev)
608 {
609 if (aw_dev->status == AW88261_DEV_PW_OFF) {
610 dev_info(aw_dev->dev, "already power off");
611 return 0;
612 }
613
614 aw_dev->status = AW88261_DEV_PW_OFF;
615
616 /* clear inturrupt */
617 aw88261_dev_clear_int_status(aw_dev);
618
619 aw88261_dev_uls_hmute(aw_dev, true);
620 /* set mute */
621 aw88261_dev_mute(aw_dev, true);
622
623 /* close tx feedback */
624 aw88261_dev_i2s_tx_enable(aw_dev, false);
625 usleep_range(AW88261_1000_US, AW88261_1000_US + 100);
626
627 /* enable amppd */
628 aw88261_dev_amppd(aw_dev, true);
629
630 /* set power down */
631 aw88261_dev_pwd(aw_dev, true);
632
633 return 0;
634 }
635
aw88261_reg_update(struct aw88261 * aw88261,bool force)636 static int aw88261_reg_update(struct aw88261 *aw88261, bool force)
637 {
638 struct aw_device *aw_dev = aw88261->aw_pa;
639 int ret;
640
641 if (force) {
642 ret = regmap_write(aw_dev->regmap,
643 AW88261_ID_REG, AW88261_SOFT_RESET_VALUE);
644 if (ret)
645 return ret;
646
647 ret = aw88261_dev_fw_update(aw88261);
648 if (ret)
649 return ret;
650 } else {
651 if (aw_dev->prof_cur != aw_dev->prof_index) {
652 ret = aw88261_dev_fw_update(aw88261);
653 if (ret)
654 return ret;
655 } else {
656 ret = 0;
657 }
658 }
659
660 aw_dev->prof_cur = aw_dev->prof_index;
661
662 return ret;
663 }
664
aw88261_start_pa(struct aw88261 * aw88261)665 static void aw88261_start_pa(struct aw88261 *aw88261)
666 {
667 int ret, i;
668
669 for (i = 0; i < AW88261_START_RETRIES; i++) {
670 ret = aw88261_reg_update(aw88261, aw88261->phase_sync);
671 if (ret) {
672 dev_err(aw88261->aw_pa->dev, "fw update failed, cnt:%d\n", i);
673 continue;
674 }
675 ret = aw88261_dev_start(aw88261);
676 if (ret) {
677 dev_err(aw88261->aw_pa->dev, "aw88261 device start failed. retry = %d", i);
678 continue;
679 } else {
680 dev_info(aw88261->aw_pa->dev, "start success\n");
681 break;
682 }
683 }
684 }
685
aw88261_startup_work(struct work_struct * work)686 static void aw88261_startup_work(struct work_struct *work)
687 {
688 struct aw88261 *aw88261 =
689 container_of(work, struct aw88261, start_work.work);
690
691 mutex_lock(&aw88261->lock);
692 aw88261_start_pa(aw88261);
693 mutex_unlock(&aw88261->lock);
694 }
695
aw88261_start(struct aw88261 * aw88261,bool sync_start)696 static void aw88261_start(struct aw88261 *aw88261, bool sync_start)
697 {
698 if (aw88261->aw_pa->fw_status != AW88261_DEV_FW_OK)
699 return;
700
701 if (aw88261->aw_pa->status == AW88261_DEV_PW_ON)
702 return;
703
704 if (sync_start == AW88261_SYNC_START)
705 aw88261_start_pa(aw88261);
706 else
707 queue_delayed_work(system_wq,
708 &aw88261->start_work,
709 AW88261_START_WORK_DELAY_MS);
710 }
711
712 static struct snd_soc_dai_driver aw88261_dai[] = {
713 {
714 .name = "aw88261-aif",
715 .id = 1,
716 .playback = {
717 .stream_name = "Speaker_Playback",
718 .channels_min = 1,
719 .channels_max = 2,
720 .rates = AW88261_RATES,
721 .formats = AW88261_FORMATS,
722 },
723 .capture = {
724 .stream_name = "Speaker_Capture",
725 .channels_min = 1,
726 .channels_max = 2,
727 .rates = AW88261_RATES,
728 .formats = AW88261_FORMATS,
729 },
730 },
731 };
732
aw88261_get_fade_in_time(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)733 static int aw88261_get_fade_in_time(struct snd_kcontrol *kcontrol,
734 struct snd_ctl_elem_value *ucontrol)
735 {
736 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
737 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(component);
738 struct aw_device *aw_dev = aw88261->aw_pa;
739
740 ucontrol->value.integer.value[0] = aw_dev->fade_in_time;
741
742 return 0;
743 }
744
aw88261_set_fade_in_time(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)745 static int aw88261_set_fade_in_time(struct snd_kcontrol *kcontrol,
746 struct snd_ctl_elem_value *ucontrol)
747 {
748 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
749 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(component);
750 struct soc_mixer_control *mc =
751 (struct soc_mixer_control *)kcontrol->private_value;
752 struct aw_device *aw_dev = aw88261->aw_pa;
753 int time;
754
755 time = ucontrol->value.integer.value[0];
756
757 if (time < mc->min || time > mc->max)
758 return -EINVAL;
759
760 if (time != aw_dev->fade_in_time) {
761 aw_dev->fade_in_time = time;
762 return 1;
763 }
764
765 return 0;
766 }
767
aw88261_get_fade_out_time(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)768 static int aw88261_get_fade_out_time(struct snd_kcontrol *kcontrol,
769 struct snd_ctl_elem_value *ucontrol)
770 {
771 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
772 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(component);
773 struct aw_device *aw_dev = aw88261->aw_pa;
774
775 ucontrol->value.integer.value[0] = aw_dev->fade_out_time;
776
777 return 0;
778 }
779
aw88261_set_fade_out_time(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)780 static int aw88261_set_fade_out_time(struct snd_kcontrol *kcontrol,
781 struct snd_ctl_elem_value *ucontrol)
782 {
783 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
784 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(component);
785 struct soc_mixer_control *mc =
786 (struct soc_mixer_control *)kcontrol->private_value;
787 struct aw_device *aw_dev = aw88261->aw_pa;
788 int time;
789
790 time = ucontrol->value.integer.value[0];
791 if (time < mc->min || time > mc->max)
792 return -EINVAL;
793
794 if (time != aw_dev->fade_out_time) {
795 aw_dev->fade_out_time = time;
796 return 1;
797 }
798
799 return 0;
800 }
801
aw88261_dev_set_profile_index(struct aw_device * aw_dev,int index)802 static int aw88261_dev_set_profile_index(struct aw_device *aw_dev, int index)
803 {
804 /* check the index whether is valid */
805 if ((index >= aw_dev->prof_info.count) || (index < 0))
806 return -EINVAL;
807 /* check the index whether change */
808 if (aw_dev->prof_index == index)
809 return -EPERM;
810
811 aw_dev->prof_index = index;
812
813 return 0;
814 }
815
aw88261_profile_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)816 static int aw88261_profile_info(struct snd_kcontrol *kcontrol,
817 struct snd_ctl_elem_info *uinfo)
818 {
819 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
820 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(codec);
821 const char *prof_name;
822 char *name;
823 int count;
824
825 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
826 uinfo->count = 1;
827
828 count = aw88261->aw_pa->prof_info.count;
829 if (count <= 0) {
830 uinfo->value.enumerated.items = 0;
831 return 0;
832 }
833
834 uinfo->value.enumerated.items = count;
835
836 if (uinfo->value.enumerated.item >= count)
837 uinfo->value.enumerated.item = count - 1;
838
839 name = uinfo->value.enumerated.name;
840 count = uinfo->value.enumerated.item;
841
842 prof_name = aw88261_dev_get_prof_name(aw88261->aw_pa, count);
843 if (!prof_name) {
844 strscpy(uinfo->value.enumerated.name, "null",
845 strlen("null") + 1);
846 return 0;
847 }
848
849 strscpy(name, prof_name, sizeof(uinfo->value.enumerated.name));
850
851 return 0;
852 }
853
aw88261_profile_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)854 static int aw88261_profile_get(struct snd_kcontrol *kcontrol,
855 struct snd_ctl_elem_value *ucontrol)
856 {
857 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
858 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(codec);
859
860 ucontrol->value.integer.value[0] = aw88261->aw_pa->prof_index;
861
862 return 0;
863 }
864
aw88261_profile_set(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)865 static int aw88261_profile_set(struct snd_kcontrol *kcontrol,
866 struct snd_ctl_elem_value *ucontrol)
867 {
868 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
869 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(codec);
870 int ret;
871
872 /* pa stop or stopping just set profile */
873 mutex_lock(&aw88261->lock);
874 ret = aw88261_dev_set_profile_index(aw88261->aw_pa, ucontrol->value.integer.value[0]);
875 if (ret) {
876 dev_dbg(codec->dev, "profile index does not change");
877 mutex_unlock(&aw88261->lock);
878 return 0;
879 }
880
881 if (aw88261->aw_pa->status) {
882 aw88261_dev_stop(aw88261->aw_pa);
883 aw88261_start(aw88261, AW88261_SYNC_START);
884 }
885
886 mutex_unlock(&aw88261->lock);
887
888 return 1;
889 }
890
aw88261_volume_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)891 static int aw88261_volume_get(struct snd_kcontrol *kcontrol,
892 struct snd_ctl_elem_value *ucontrol)
893 {
894 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
895 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(codec);
896 struct aw_volume_desc *vol_desc = &aw88261->aw_pa->volume_desc;
897
898 ucontrol->value.integer.value[0] = vol_desc->ctl_volume;
899
900 return 0;
901 }
902
aw88261_volume_set(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)903 static int aw88261_volume_set(struct snd_kcontrol *kcontrol,
904 struct snd_ctl_elem_value *ucontrol)
905 {
906 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
907 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(codec);
908 struct aw_volume_desc *vol_desc = &aw88261->aw_pa->volume_desc;
909 struct soc_mixer_control *mc =
910 (struct soc_mixer_control *)kcontrol->private_value;
911 int value;
912
913 value = ucontrol->value.integer.value[0];
914
915 if (value < mc->min || value > mc->max)
916 return -EINVAL;
917
918 if (vol_desc->ctl_volume != value) {
919 vol_desc->ctl_volume = value;
920 aw88261_dev_set_volume(aw88261->aw_pa, vol_desc->ctl_volume);
921
922 return 1;
923 }
924
925 return 0;
926 }
927
aw88261_get_fade_step(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)928 static int aw88261_get_fade_step(struct snd_kcontrol *kcontrol,
929 struct snd_ctl_elem_value *ucontrol)
930 {
931 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
932 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(codec);
933
934 ucontrol->value.integer.value[0] = aw88261->aw_pa->fade_step;
935
936 return 0;
937 }
938
aw88261_set_fade_step(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)939 static int aw88261_set_fade_step(struct snd_kcontrol *kcontrol,
940 struct snd_ctl_elem_value *ucontrol)
941 {
942 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
943 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(codec);
944 struct soc_mixer_control *mc =
945 (struct soc_mixer_control *)kcontrol->private_value;
946 int value;
947
948 value = ucontrol->value.integer.value[0];
949 if (value < mc->min || value > mc->max)
950 return -EINVAL;
951
952 if (aw88261->aw_pa->fade_step != value) {
953 aw88261->aw_pa->fade_step = value;
954 return 1;
955 }
956
957 return 0;
958 }
959
960 static const struct snd_kcontrol_new aw88261_controls[] = {
961 SOC_SINGLE_EXT("PCM Playback Volume", AW88261_SYSCTRL2_REG,
962 6, AW88261_MUTE_VOL, 0, aw88261_volume_get,
963 aw88261_volume_set),
964 SOC_SINGLE_EXT("Fade Step", 0, 0, AW88261_MUTE_VOL, 0,
965 aw88261_get_fade_step, aw88261_set_fade_step),
966 SOC_SINGLE_EXT("Volume Ramp Up Step", 0, 0, FADE_TIME_MAX, FADE_TIME_MIN,
967 aw88261_get_fade_in_time, aw88261_set_fade_in_time),
968 SOC_SINGLE_EXT("Volume Ramp Down Step", 0, 0, FADE_TIME_MAX, FADE_TIME_MIN,
969 aw88261_get_fade_out_time, aw88261_set_fade_out_time),
970 AW88261_PROFILE_EXT("Profile Set", aw88261_profile_info,
971 aw88261_profile_get, aw88261_profile_set),
972 };
973
aw88261_playback_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * k,int event)974 static int aw88261_playback_event(struct snd_soc_dapm_widget *w,
975 struct snd_kcontrol *k, int event)
976 {
977 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
978 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(component);
979
980 mutex_lock(&aw88261->lock);
981 switch (event) {
982 case SND_SOC_DAPM_PRE_PMU:
983 aw88261_start(aw88261, AW88261_ASYNC_START);
984 break;
985 case SND_SOC_DAPM_POST_PMD:
986 aw88261_dev_stop(aw88261->aw_pa);
987 break;
988 default:
989 break;
990 }
991 mutex_unlock(&aw88261->lock);
992
993 return 0;
994 }
995
996 static const struct snd_soc_dapm_widget aw88261_dapm_widgets[] = {
997 /* playback */
998 SND_SOC_DAPM_AIF_IN_E("AIF_RX", "Speaker_Playback", 0, 0, 0, 0,
999 aw88261_playback_event,
1000 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1001 SND_SOC_DAPM_OUTPUT("DAC Output"),
1002
1003 /* capture */
1004 SND_SOC_DAPM_AIF_OUT("AIF_TX", "Speaker_Capture", 0, SND_SOC_NOPM, 0, 0),
1005 SND_SOC_DAPM_INPUT("ADC Input"),
1006 };
1007
1008 static const struct snd_soc_dapm_route aw88261_audio_map[] = {
1009 {"DAC Output", NULL, "AIF_RX"},
1010 {"AIF_TX", NULL, "ADC Input"},
1011 };
1012
aw88261_frcset_check(struct aw88261 * aw88261)1013 static int aw88261_frcset_check(struct aw88261 *aw88261)
1014 {
1015 unsigned int reg_val;
1016 u16 temh, teml, tem;
1017 int ret;
1018
1019 ret = regmap_read(aw88261->regmap, AW88261_EFRH3_REG, ®_val);
1020 if (ret)
1021 return ret;
1022 temh = ((u16)reg_val & (~AW88261_TEMH_MASK));
1023
1024 ret = regmap_read(aw88261->regmap, AW88261_EFRL3_REG, ®_val);
1025 if (ret)
1026 return ret;
1027 teml = ((u16)reg_val & (~AW88261_TEML_MASK));
1028
1029 if (aw88261->efuse_check == AW88261_EF_OR_CHECK)
1030 tem = (temh | teml);
1031 else
1032 tem = (temh & teml);
1033
1034 if (tem == AW88261_DEFAULT_CFG)
1035 aw88261->frcset_en = AW88261_FRCSET_ENABLE;
1036 else
1037 aw88261->frcset_en = AW88261_FRCSET_DISABLE;
1038
1039 dev_dbg(aw88261->aw_pa->dev, "tem is 0x%04x, frcset_en is %d",
1040 tem, aw88261->frcset_en);
1041
1042 return ret;
1043 }
1044
aw88261_dev_init(struct aw88261 * aw88261,struct aw_container * aw_cfg)1045 static int aw88261_dev_init(struct aw88261 *aw88261, struct aw_container *aw_cfg)
1046 {
1047 struct aw_device *aw_dev = aw88261->aw_pa;
1048 int ret;
1049
1050 ret = aw88395_dev_cfg_load(aw_dev, aw_cfg);
1051 if (ret) {
1052 dev_err(aw_dev->dev, "aw_dev acf parse failed");
1053 return -EINVAL;
1054 }
1055
1056 ret = regmap_write(aw_dev->regmap, AW88261_ID_REG, AW88261_SOFT_RESET_VALUE);
1057 if (ret)
1058 return ret;
1059
1060 aw_dev->fade_in_time = AW88261_500_US;
1061 aw_dev->fade_out_time = AW88261_500_US;
1062 aw_dev->prof_cur = AW88261_INIT_PROFILE;
1063 aw_dev->prof_index = AW88261_INIT_PROFILE;
1064
1065 ret = aw88261_dev_fw_update(aw88261);
1066 if (ret) {
1067 dev_err(aw_dev->dev, "fw update failed ret = %d\n", ret);
1068 return ret;
1069 }
1070
1071 ret = aw88261_frcset_check(aw88261);
1072 if (ret) {
1073 dev_err(aw_dev->dev, "aw88261_frcset_check ret = %d\n", ret);
1074 return ret;
1075 }
1076
1077 aw88261_dev_clear_int_status(aw_dev);
1078
1079 aw88261_dev_uls_hmute(aw_dev, true);
1080
1081 aw88261_dev_mute(aw_dev, true);
1082
1083 aw88261_dev_i2s_tx_enable(aw_dev, false);
1084
1085 usleep_range(AW88261_1000_US, AW88261_1000_US + 100);
1086
1087 aw88261_dev_amppd(aw_dev, true);
1088
1089 aw88261_dev_pwd(aw_dev, true);
1090
1091 return 0;
1092 }
1093
aw88261_request_firmware_file(struct aw88261 * aw88261)1094 static int aw88261_request_firmware_file(struct aw88261 *aw88261)
1095 {
1096 const struct firmware *cont = NULL;
1097 int ret;
1098
1099 aw88261->aw_pa->fw_status = AW88261_DEV_FW_FAILED;
1100
1101 ret = request_firmware(&cont, AW88261_ACF_FILE, aw88261->aw_pa->dev);
1102 if (ret)
1103 return dev_err_probe(aw88261->aw_pa->dev, ret,
1104 "load [%s] failed!", AW88261_ACF_FILE);
1105
1106 dev_info(aw88261->aw_pa->dev, "loaded %s - size: %zu\n",
1107 AW88261_ACF_FILE, cont ? cont->size : 0);
1108
1109 aw88261->aw_cfg = devm_kzalloc(aw88261->aw_pa->dev, cont->size + sizeof(int), GFP_KERNEL);
1110 if (!aw88261->aw_cfg) {
1111 release_firmware(cont);
1112 return -ENOMEM;
1113 }
1114 aw88261->aw_cfg->len = (int)cont->size;
1115 memcpy(aw88261->aw_cfg->data, cont->data, cont->size);
1116 release_firmware(cont);
1117
1118 ret = aw88395_dev_load_acf_check(aw88261->aw_pa, aw88261->aw_cfg);
1119 if (ret) {
1120 dev_err(aw88261->aw_pa->dev, "load [%s] failed !", AW88261_ACF_FILE);
1121 return ret;
1122 }
1123
1124 mutex_lock(&aw88261->lock);
1125 /* aw device init */
1126 ret = aw88261_dev_init(aw88261, aw88261->aw_cfg);
1127 if (ret)
1128 dev_err(aw88261->aw_pa->dev, "dev init failed");
1129 mutex_unlock(&aw88261->lock);
1130
1131 return ret;
1132 }
1133
aw88261_codec_probe(struct snd_soc_component * component)1134 static int aw88261_codec_probe(struct snd_soc_component *component)
1135 {
1136 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1137 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(component);
1138 int ret;
1139
1140 INIT_DELAYED_WORK(&aw88261->start_work, aw88261_startup_work);
1141
1142 ret = aw88261_request_firmware_file(aw88261);
1143 if (ret)
1144 return dev_err_probe(aw88261->aw_pa->dev, ret,
1145 "aw88261_request_firmware_file failed\n");
1146
1147 /* add widgets */
1148 ret = snd_soc_dapm_new_controls(dapm, aw88261_dapm_widgets,
1149 ARRAY_SIZE(aw88261_dapm_widgets));
1150 if (ret)
1151 return ret;
1152
1153 /* add route */
1154 ret = snd_soc_dapm_add_routes(dapm, aw88261_audio_map,
1155 ARRAY_SIZE(aw88261_audio_map));
1156 if (ret)
1157 return ret;
1158
1159 ret = snd_soc_add_component_controls(component, aw88261_controls,
1160 ARRAY_SIZE(aw88261_controls));
1161
1162 return ret;
1163 }
1164
aw88261_codec_remove(struct snd_soc_component * aw_codec)1165 static void aw88261_codec_remove(struct snd_soc_component *aw_codec)
1166 {
1167 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(aw_codec);
1168
1169 cancel_delayed_work_sync(&aw88261->start_work);
1170 }
1171
1172 static const struct snd_soc_component_driver soc_codec_dev_aw88261 = {
1173 .probe = aw88261_codec_probe,
1174 .remove = aw88261_codec_remove,
1175 };
1176
aw88261_hw_reset(struct aw88261 * aw88261)1177 static void aw88261_hw_reset(struct aw88261 *aw88261)
1178 {
1179 gpiod_set_value_cansleep(aw88261->reset_gpio, 0);
1180 usleep_range(AW88261_1000_US, AW88261_1000_US + 10);
1181 gpiod_set_value_cansleep(aw88261->reset_gpio, 1);
1182 usleep_range(AW88261_1000_US, AW88261_1000_US + 10);
1183 }
1184
aw88261_parse_channel_dt(struct aw88261 * aw88261)1185 static void aw88261_parse_channel_dt(struct aw88261 *aw88261)
1186 {
1187 struct aw_device *aw_dev = aw88261->aw_pa;
1188 struct device_node *np = aw_dev->dev->of_node;
1189 u32 channel_value = AW88261_DEV_DEFAULT_CH;
1190 u32 sync_enable = false;
1191
1192 of_property_read_u32(np, "sound-channel", &channel_value);
1193 of_property_read_u32(np, "sync-flag", &sync_enable);
1194
1195 aw_dev->channel = channel_value;
1196 aw88261->phase_sync = sync_enable;
1197 }
1198
aw88261_init(struct aw88261 ** aw88261,struct i2c_client * i2c,struct regmap * regmap)1199 static int aw88261_init(struct aw88261 **aw88261, struct i2c_client *i2c, struct regmap *regmap)
1200 {
1201 struct aw_device *aw_dev;
1202 unsigned int chip_id;
1203 int ret;
1204
1205 /* read chip id */
1206 ret = regmap_read(regmap, AW88261_ID_REG, &chip_id);
1207 if (ret) {
1208 dev_err(&i2c->dev, "%s read chipid error. ret = %d", __func__, ret);
1209 return ret;
1210 }
1211 if (chip_id != AW88261_CHIP_ID) {
1212 dev_err(&i2c->dev, "unsupported device");
1213 return -ENXIO;
1214 }
1215
1216 dev_info(&i2c->dev, "chip id = %x\n", chip_id);
1217
1218 aw_dev = devm_kzalloc(&i2c->dev, sizeof(*aw_dev), GFP_KERNEL);
1219 if (!aw_dev)
1220 return -ENOMEM;
1221
1222 (*aw88261)->aw_pa = aw_dev;
1223 aw_dev->i2c = i2c;
1224 aw_dev->regmap = regmap;
1225 aw_dev->dev = &i2c->dev;
1226 aw_dev->chip_id = AW88261_CHIP_ID;
1227 aw_dev->acf = NULL;
1228 aw_dev->prof_info.prof_desc = NULL;
1229 aw_dev->prof_info.count = 0;
1230 aw_dev->prof_info.prof_type = AW88395_DEV_NONE_TYPE_ID;
1231 aw_dev->channel = 0;
1232 aw_dev->fw_status = AW88261_DEV_FW_FAILED;
1233 aw_dev->fade_step = AW88261_VOLUME_STEP_DB;
1234 aw_dev->volume_desc.ctl_volume = AW88261_VOL_DEFAULT_VALUE;
1235 aw_dev->volume_desc.mute_volume = AW88261_MUTE_VOL;
1236 aw88261_parse_channel_dt(*aw88261);
1237
1238 return ret;
1239 }
1240
aw88261_i2c_probe(struct i2c_client * i2c)1241 static int aw88261_i2c_probe(struct i2c_client *i2c)
1242 {
1243 struct aw88261 *aw88261;
1244 int ret;
1245
1246 ret = i2c_check_functionality(i2c->adapter, I2C_FUNC_I2C);
1247 if (!ret)
1248 return dev_err_probe(&i2c->dev, -ENXIO, "check_functionality failed");
1249
1250 aw88261 = devm_kzalloc(&i2c->dev, sizeof(*aw88261), GFP_KERNEL);
1251 if (!aw88261)
1252 return -ENOMEM;
1253
1254 mutex_init(&aw88261->lock);
1255
1256 i2c_set_clientdata(i2c, aw88261);
1257
1258 aw88261->reset_gpio = devm_gpiod_get_optional(&i2c->dev, "reset", GPIOD_OUT_LOW);
1259 if (IS_ERR(aw88261->reset_gpio))
1260 dev_info(&i2c->dev, "reset gpio not defined\n");
1261 else
1262 aw88261_hw_reset(aw88261);
1263
1264 aw88261->regmap = devm_regmap_init_i2c(i2c, &aw88261_remap_config);
1265 if (IS_ERR(aw88261->regmap)) {
1266 ret = PTR_ERR(aw88261->regmap);
1267 return dev_err_probe(&i2c->dev, ret, "failed to init regmap: %d\n", ret);
1268 }
1269
1270 /* aw pa init */
1271 ret = aw88261_init(&aw88261, i2c, aw88261->regmap);
1272 if (ret)
1273 return ret;
1274
1275 ret = devm_snd_soc_register_component(&i2c->dev,
1276 &soc_codec_dev_aw88261,
1277 aw88261_dai, ARRAY_SIZE(aw88261_dai));
1278 if (ret)
1279 dev_err(&i2c->dev, "failed to register aw88261: %d", ret);
1280
1281 return ret;
1282 }
1283
1284 static const struct i2c_device_id aw88261_i2c_id[] = {
1285 { AW88261_I2C_NAME, 0 },
1286 { }
1287 };
1288 MODULE_DEVICE_TABLE(i2c, aw88261_i2c_id);
1289
1290 static struct i2c_driver aw88261_i2c_driver = {
1291 .driver = {
1292 .name = AW88261_I2C_NAME,
1293 },
1294 .probe = aw88261_i2c_probe,
1295 .id_table = aw88261_i2c_id,
1296 };
1297 module_i2c_driver(aw88261_i2c_driver);
1298
1299 MODULE_DESCRIPTION("ASoC AW88261 Smart PA Driver");
1300 MODULE_LICENSE("GPL v2");
1301