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/regmap.h>
14 #include <linux/regulator/consumer.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,
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 /* keep all three bits from current hw status */
428 read_val &= (~AW88261_AMPPD_MASK) | (~AW88261_PWDN_MASK) |
429 (~AW88261_HMUTE_MASK);
430 reg_val &= (AW88261_AMPPD_MASK & AW88261_PWDN_MASK & AW88261_HMUTE_MASK);
431 reg_val |= read_val;
432
433 /* enable uls hmute */
434 reg_val &= AW88261_ULS_HMUTE_MASK;
435 reg_val |= AW88261_ULS_HMUTE_ENABLE_VALUE;
436 }
437
438 if (reg_addr == AW88261_DBGCTRL_REG) {
439 efcheck_val = reg_val & (~AW88261_EF_DBMD_MASK);
440 if (efcheck_val == AW88261_OR_VALUE)
441 aw88261->efuse_check = AW88261_EF_OR_CHECK;
442 else
443 aw88261->efuse_check = AW88261_EF_AND_CHECK;
444 }
445
446 /* i2stxen */
447 if (reg_addr == AW88261_I2SCTRL3_REG) {
448 /* close tx */
449 reg_val &= AW88261_I2STXEN_MASK;
450 reg_val |= AW88261_I2STXEN_DISABLE_VALUE;
451 }
452
453 if (reg_addr == AW88261_SYSCTRL2_REG) {
454 read_vol = (reg_val & (~AW88261_VOL_MASK)) >>
455 AW88261_VOL_START_BIT;
456 aw_dev->volume_desc.init_volume =
457 REG_VAL_TO_DB(read_vol);
458 }
459
460 if (reg_addr == AW88261_VSNTM1_REG)
461 continue;
462
463 ret = regmap_write(aw_dev->regmap, reg_addr, reg_val);
464 if (ret)
465 break;
466 }
467
468 ret = aw88261_dev_set_vcalb(aw_dev);
469 if (ret)
470 return ret;
471
472 if (aw_dev->prof_cur != aw_dev->prof_index)
473 vol_desc->ctl_volume = 0;
474
475 /* keep min volume */
476 aw88261_dev_set_volume(aw_dev, vol_desc->mute_volume);
477
478 return ret;
479 }
480
aw88261_dev_get_prof_name(struct aw_device * aw_dev,int index,char ** prof_name)481 static int aw88261_dev_get_prof_name(struct aw_device *aw_dev, int index, char **prof_name)
482 {
483 struct aw_prof_info *prof_info = &aw_dev->prof_info;
484 struct aw_prof_desc *prof_desc;
485
486 if ((index >= aw_dev->prof_info.count) || (index < 0)) {
487 dev_err(aw_dev->dev, "index[%d] overflow count[%d]",
488 index, aw_dev->prof_info.count);
489 return -EINVAL;
490 }
491
492 prof_desc = &aw_dev->prof_info.prof_desc[index];
493
494 *prof_name = prof_info->prof_name_list[prof_desc->id];
495
496 return 0;
497 }
498
aw88261_dev_get_prof_data(struct aw_device * aw_dev,int index,struct aw_prof_desc ** prof_desc)499 static int aw88261_dev_get_prof_data(struct aw_device *aw_dev, int index,
500 struct aw_prof_desc **prof_desc)
501 {
502 if ((index >= aw_dev->prof_info.count) || (index < 0)) {
503 dev_err(aw_dev->dev, "%s: index[%d] overflow count[%d]\n",
504 __func__, index, aw_dev->prof_info.count);
505 return -EINVAL;
506 }
507
508 *prof_desc = &aw_dev->prof_info.prof_desc[index];
509
510 return 0;
511 }
512
aw88261_dev_fw_update(struct aw88261 * aw88261)513 static int aw88261_dev_fw_update(struct aw88261 *aw88261)
514 {
515 struct aw_device *aw_dev = aw88261->aw_pa;
516 struct aw_prof_desc *prof_index_desc;
517 struct aw_sec_data_desc *sec_desc;
518 char *prof_name;
519 int ret;
520
521 ret = aw88261_dev_get_prof_name(aw_dev, aw_dev->prof_index, &prof_name);
522 if (ret) {
523 dev_err(aw_dev->dev, "get prof name failed");
524 return -EINVAL;
525 }
526
527 dev_dbg(aw_dev->dev, "start update %s", prof_name);
528
529 ret = aw88261_dev_get_prof_data(aw_dev, aw_dev->prof_index, &prof_index_desc);
530 if (ret)
531 return ret;
532
533 /* update reg */
534 sec_desc = prof_index_desc->sec_desc;
535 ret = aw88261_dev_reg_update(aw88261, sec_desc[AW88395_DATA_TYPE_REG].data,
536 sec_desc[AW88395_DATA_TYPE_REG].len);
537 if (ret) {
538 dev_err(aw_dev->dev, "update reg failed");
539 return ret;
540 }
541
542 aw_dev->prof_cur = aw_dev->prof_index;
543
544 return ret;
545 }
546
aw88261_dev_start(struct aw88261 * aw88261)547 static int aw88261_dev_start(struct aw88261 *aw88261)
548 {
549 struct aw_device *aw_dev = aw88261->aw_pa;
550 int ret;
551
552 if (aw_dev->status == AW88261_DEV_PW_ON) {
553 dev_info(aw_dev->dev, "already power on");
554 return 0;
555 }
556
557 /* power on */
558 aw88261_dev_pwd(aw_dev, false);
559 usleep_range(AW88261_2000_US, AW88261_2000_US + 10);
560
561 ret = aw88261_dev_check_syspll(aw_dev);
562 if (ret) {
563 dev_err(aw_dev->dev, "pll check failed cannot start");
564 goto pll_check_fail;
565 }
566
567 /* amppd on */
568 aw88261_dev_amppd(aw_dev, false);
569 usleep_range(AW88261_1000_US, AW88261_1000_US + 50);
570
571 /* check i2s status */
572 ret = aw88261_dev_check_sysst(aw_dev);
573 if (ret) {
574 dev_err(aw_dev->dev, "sysst check failed");
575 goto sysst_check_fail;
576 }
577
578 /* enable tx feedback */
579 aw88261_dev_i2s_tx_enable(aw_dev, true);
580
581 if (aw88261->amppd_st)
582 aw88261_dev_amppd(aw_dev, true);
583
584 aw88261_reg_force_set(aw88261);
585
586 /* close uls mute */
587 aw88261_dev_uls_hmute(aw_dev, false);
588
589 /* close mute */
590 if (!aw88261->mute_st)
591 aw88261_dev_mute(aw_dev, false);
592
593 /* clear inturrupt */
594 aw88261_dev_clear_int_status(aw_dev);
595 aw_dev->status = AW88261_DEV_PW_ON;
596
597 return 0;
598
599 sysst_check_fail:
600 aw88261_dev_i2s_tx_enable(aw_dev, false);
601 aw88261_dev_clear_int_status(aw_dev);
602 aw88261_dev_amppd(aw_dev, true);
603 pll_check_fail:
604 aw88261_dev_pwd(aw_dev, true);
605 aw_dev->status = AW88261_DEV_PW_OFF;
606
607 return ret;
608 }
609
aw88261_dev_stop(struct aw_device * aw_dev)610 static int aw88261_dev_stop(struct aw_device *aw_dev)
611 {
612 if (aw_dev->status == AW88261_DEV_PW_OFF) {
613 dev_info(aw_dev->dev, "already power off");
614 return 0;
615 }
616
617 aw_dev->status = AW88261_DEV_PW_OFF;
618
619 /* clear inturrupt */
620 aw88261_dev_clear_int_status(aw_dev);
621
622 aw88261_dev_uls_hmute(aw_dev, true);
623 /* set mute */
624 aw88261_dev_mute(aw_dev, true);
625
626 /* close tx feedback */
627 aw88261_dev_i2s_tx_enable(aw_dev, false);
628 usleep_range(AW88261_1000_US, AW88261_1000_US + 100);
629
630 /* enable amppd */
631 aw88261_dev_amppd(aw_dev, true);
632
633 /* set power down */
634 aw88261_dev_pwd(aw_dev, true);
635
636 return 0;
637 }
638
aw88261_reg_update(struct aw88261 * aw88261,bool force)639 static int aw88261_reg_update(struct aw88261 *aw88261, bool force)
640 {
641 struct aw_device *aw_dev = aw88261->aw_pa;
642 int ret;
643
644 if (force) {
645 ret = regmap_write(aw_dev->regmap,
646 AW88261_ID_REG, AW88261_SOFT_RESET_VALUE);
647 if (ret)
648 return ret;
649
650 ret = aw88261_dev_fw_update(aw88261);
651 if (ret)
652 return ret;
653 } else {
654 if (aw_dev->prof_cur != aw_dev->prof_index) {
655 ret = aw88261_dev_fw_update(aw88261);
656 if (ret)
657 return ret;
658 } else {
659 ret = 0;
660 }
661 }
662
663 aw_dev->prof_cur = aw_dev->prof_index;
664
665 return ret;
666 }
667
aw88261_start_pa(struct aw88261 * aw88261)668 static void aw88261_start_pa(struct aw88261 *aw88261)
669 {
670 int ret, i;
671
672 for (i = 0; i < AW88261_START_RETRIES; i++) {
673 ret = aw88261_reg_update(aw88261, aw88261->phase_sync);
674 if (ret) {
675 dev_err(aw88261->aw_pa->dev, "fw update failed, cnt:%d\n", i);
676 continue;
677 }
678 ret = aw88261_dev_start(aw88261);
679 if (ret) {
680 dev_err(aw88261->aw_pa->dev, "aw88261 device start failed. retry = %d", i);
681 continue;
682 } else {
683 dev_info(aw88261->aw_pa->dev, "start success\n");
684 break;
685 }
686 }
687 }
688
aw88261_startup_work(struct work_struct * work)689 static void aw88261_startup_work(struct work_struct *work)
690 {
691 struct aw88261 *aw88261 =
692 container_of(work, struct aw88261, start_work.work);
693
694 mutex_lock(&aw88261->lock);
695 aw88261_start_pa(aw88261);
696 mutex_unlock(&aw88261->lock);
697 }
698
aw88261_start(struct aw88261 * aw88261,bool sync_start)699 static void aw88261_start(struct aw88261 *aw88261, bool sync_start)
700 {
701 if (aw88261->aw_pa->fw_status != AW88261_DEV_FW_OK)
702 return;
703
704 if (aw88261->aw_pa->status == AW88261_DEV_PW_ON)
705 return;
706
707 if (sync_start == AW88261_SYNC_START)
708 aw88261_start_pa(aw88261);
709 else
710 queue_delayed_work(system_dfl_wq,
711 &aw88261->start_work,
712 AW88261_START_WORK_DELAY_MS);
713 }
714
715 static struct snd_soc_dai_driver aw88261_dai[] = {
716 {
717 .name = "aw88261-aif",
718 .id = 1,
719 .playback = {
720 .stream_name = "Speaker_Playback",
721 .channels_min = 1,
722 .channels_max = 2,
723 .rates = AW88261_RATES,
724 .formats = AW88261_FORMATS,
725 },
726 .capture = {
727 .stream_name = "Speaker_Capture",
728 .channels_min = 1,
729 .channels_max = 2,
730 .rates = AW88261_RATES,
731 .formats = AW88261_FORMATS,
732 },
733 },
734 };
735
aw88261_get_fade_in_time(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)736 static int aw88261_get_fade_in_time(struct snd_kcontrol *kcontrol,
737 struct snd_ctl_elem_value *ucontrol)
738 {
739 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
740 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(component);
741 struct aw_device *aw_dev = aw88261->aw_pa;
742
743 ucontrol->value.integer.value[0] = aw_dev->fade_in_time;
744
745 return 0;
746 }
747
aw88261_set_fade_in_time(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)748 static int aw88261_set_fade_in_time(struct snd_kcontrol *kcontrol,
749 struct snd_ctl_elem_value *ucontrol)
750 {
751 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
752 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(component);
753 struct soc_mixer_control *mc =
754 (struct soc_mixer_control *)kcontrol->private_value;
755 struct aw_device *aw_dev = aw88261->aw_pa;
756 int time;
757
758 time = ucontrol->value.integer.value[0];
759
760 if (time < mc->min || time > mc->max)
761 return -EINVAL;
762
763 if (time != aw_dev->fade_in_time) {
764 aw_dev->fade_in_time = time;
765 return 1;
766 }
767
768 return 0;
769 }
770
aw88261_get_fade_out_time(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)771 static int aw88261_get_fade_out_time(struct snd_kcontrol *kcontrol,
772 struct snd_ctl_elem_value *ucontrol)
773 {
774 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
775 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(component);
776 struct aw_device *aw_dev = aw88261->aw_pa;
777
778 ucontrol->value.integer.value[0] = aw_dev->fade_out_time;
779
780 return 0;
781 }
782
aw88261_set_fade_out_time(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)783 static int aw88261_set_fade_out_time(struct snd_kcontrol *kcontrol,
784 struct snd_ctl_elem_value *ucontrol)
785 {
786 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
787 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(component);
788 struct soc_mixer_control *mc =
789 (struct soc_mixer_control *)kcontrol->private_value;
790 struct aw_device *aw_dev = aw88261->aw_pa;
791 int time;
792
793 time = ucontrol->value.integer.value[0];
794 if (time < mc->min || time > mc->max)
795 return -EINVAL;
796
797 if (time != aw_dev->fade_out_time) {
798 aw_dev->fade_out_time = time;
799 return 1;
800 }
801
802 return 0;
803 }
804
aw88261_dev_set_profile_index(struct aw_device * aw_dev,int index)805 static int aw88261_dev_set_profile_index(struct aw_device *aw_dev, int index)
806 {
807 /* check the index whether is valid */
808 if ((index >= aw_dev->prof_info.count) || (index < 0))
809 return -EINVAL;
810 /* check the index whether change */
811 if (aw_dev->prof_index == index)
812 return -EPERM;
813
814 aw_dev->prof_index = index;
815
816 return 0;
817 }
818
aw88261_profile_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)819 static int aw88261_profile_info(struct snd_kcontrol *kcontrol,
820 struct snd_ctl_elem_info *uinfo)
821 {
822 struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
823 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(codec);
824 char *prof_name;
825 int count, ret;
826
827 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
828 uinfo->count = 1;
829
830 count = aw88261->aw_pa->prof_info.count;
831 if (count <= 0) {
832 uinfo->value.enumerated.items = 0;
833 return 0;
834 }
835
836 uinfo->value.enumerated.items = count;
837
838 if (uinfo->value.enumerated.item >= count)
839 uinfo->value.enumerated.item = count - 1;
840
841 count = uinfo->value.enumerated.item;
842
843 ret = aw88261_dev_get_prof_name(aw88261->aw_pa, count, &prof_name);
844 if (ret) {
845 strscpy(uinfo->value.enumerated.name, "null");
846 return 0;
847 }
848
849 strscpy(uinfo->value.enumerated.name, prof_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_kcontrol_chip(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_kcontrol_chip(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_kcontrol_chip(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_kcontrol_chip(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_kcontrol_chip(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_kcontrol_chip(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_to_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_parse_channel_dt(struct aw88261 * aw88261)1177 static void aw88261_parse_channel_dt(struct aw88261 *aw88261)
1178 {
1179 struct aw_device *aw_dev = aw88261->aw_pa;
1180 struct device_node *np = aw_dev->dev->of_node;
1181 u32 channel_value = AW88261_DEV_DEFAULT_CH;
1182
1183 of_property_read_u32(np, "awinic,audio-channel", &channel_value);
1184 aw88261->phase_sync = of_property_read_bool(np, "awinic,sync-flag");
1185
1186 aw_dev->channel = channel_value;
1187 }
1188
aw88261_init(struct aw88261 * aw88261,struct i2c_client * i2c,struct regmap * regmap)1189 static int aw88261_init(struct aw88261 *aw88261, struct i2c_client *i2c, struct regmap *regmap)
1190 {
1191 struct aw_device *aw_dev;
1192 unsigned int chip_id;
1193 int ret;
1194
1195 ret = devm_regulator_get_enable(&i2c->dev, "dvdd");
1196 if (ret)
1197 return dev_err_probe(&i2c->dev, ret, "Failed to enable dvdd supply\n");
1198
1199 /* read chip id */
1200 ret = regmap_read(regmap, AW88261_ID_REG, &chip_id);
1201 if (ret) {
1202 dev_err(&i2c->dev, "%s read chipid error. ret = %d", __func__, ret);
1203 return ret;
1204 }
1205 if (chip_id != AW88261_CHIP_ID) {
1206 dev_err(&i2c->dev, "unsupported device");
1207 return -ENXIO;
1208 }
1209
1210 dev_info(&i2c->dev, "chip id = %x\n", chip_id);
1211
1212 aw_dev = devm_kzalloc(&i2c->dev, sizeof(*aw_dev), GFP_KERNEL);
1213 if (!aw_dev)
1214 return -ENOMEM;
1215
1216 aw88261->aw_pa = aw_dev;
1217 aw_dev->i2c = i2c;
1218 aw_dev->regmap = regmap;
1219 aw_dev->dev = &i2c->dev;
1220 aw_dev->chip_id = AW88261_CHIP_ID;
1221 aw_dev->acf = NULL;
1222 aw_dev->prof_info.prof_desc = NULL;
1223 aw_dev->prof_info.count = 0;
1224 aw_dev->prof_info.prof_type = AW88395_DEV_NONE_TYPE_ID;
1225 aw_dev->channel = 0;
1226 aw_dev->fw_status = AW88261_DEV_FW_FAILED;
1227 aw_dev->fade_step = AW88261_VOLUME_STEP_DB;
1228 aw_dev->volume_desc.ctl_volume = AW88261_VOL_DEFAULT_VALUE;
1229 aw_dev->volume_desc.mute_volume = AW88261_MUTE_VOL;
1230 aw88261_parse_channel_dt(aw88261);
1231
1232 return ret;
1233 }
1234
aw88261_i2c_probe(struct i2c_client * i2c)1235 static int aw88261_i2c_probe(struct i2c_client *i2c)
1236 {
1237 struct aw88261 *aw88261;
1238 int ret;
1239
1240 ret = i2c_check_functionality(i2c->adapter, I2C_FUNC_I2C);
1241 if (!ret)
1242 return dev_err_probe(&i2c->dev, -ENXIO, "check_functionality failed");
1243
1244 aw88261 = devm_kzalloc(&i2c->dev, sizeof(*aw88261), GFP_KERNEL);
1245 if (!aw88261)
1246 return -ENOMEM;
1247
1248 mutex_init(&aw88261->lock);
1249
1250 i2c_set_clientdata(i2c, aw88261);
1251
1252 aw88261->regmap = devm_regmap_init_i2c(i2c, &aw88261_remap_config);
1253 if (IS_ERR(aw88261->regmap)) {
1254 ret = PTR_ERR(aw88261->regmap);
1255 return dev_err_probe(&i2c->dev, ret, "failed to init regmap: %d\n", ret);
1256 }
1257
1258 /* aw pa init */
1259 ret = aw88261_init(aw88261, i2c, aw88261->regmap);
1260 if (ret)
1261 return ret;
1262
1263 ret = devm_snd_soc_register_component(&i2c->dev,
1264 &soc_codec_dev_aw88261,
1265 aw88261_dai, ARRAY_SIZE(aw88261_dai));
1266 if (ret)
1267 dev_err(&i2c->dev, "failed to register aw88261: %d", ret);
1268
1269 return ret;
1270 }
1271
1272 static const struct i2c_device_id aw88261_i2c_id[] = {
1273 { "aw88261" },
1274 { }
1275 };
1276 MODULE_DEVICE_TABLE(i2c, aw88261_i2c_id);
1277
1278 static const struct of_device_id aw88261_of_table[] = {
1279 { .compatible = "awinic,aw88261" },
1280 { }
1281 };
1282 MODULE_DEVICE_TABLE(of, aw88261_of_table);
1283
1284 static struct i2c_driver aw88261_i2c_driver = {
1285 .driver = {
1286 .name = "aw88261",
1287 .of_match_table = aw88261_of_table,
1288 },
1289 .probe = aw88261_i2c_probe,
1290 .id_table = aw88261_i2c_id,
1291 };
1292 module_i2c_driver(aw88261_i2c_driver);
1293
1294 MODULE_DESCRIPTION("ASoC AW88261 Smart PA Driver");
1295 MODULE_LICENSE("GPL v2");
1296