1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // fs210x.c -- Driver for the FS2104/5S Audio Amplifier
4 //
5 // Copyright (C) 2016-2025 Shanghai FourSemi Semiconductor Co.,Ltd.
6
7 #include <linux/cleanup.h>
8 #include <linux/clk.h>
9 #include <linux/delay.h>
10 #include <linux/i2c.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
14 #include <linux/of.h>
15 #include <linux/regmap.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/workqueue.h>
18 #include <sound/soc.h>
19 #include <sound/tlv.h>
20
21 #include "fs210x.h"
22 #include "fs-amp-lib.h"
23
24 #define FS210X_DEFAULT_FWM_NAME "fs210x_fwm.bin"
25 #define FS210X_DEFAULT_DAI_NAME "fs210x-aif"
26 #define FS2105S_DEVICE_ID 0x20 /* FS2105S */
27 #define FS210X_DEVICE_ID 0x45 /* FS2104 */
28 #define FS210X_REG_MAX 0xF8
29 #define FS210X_INIT_SCENE 0
30 #define FS210X_DEFAULT_SCENE 1
31 #define FS210X_START_DELAY_MS 5
32 #define FS210X_FAULT_CHECK_INTERVAL_MS 2000
33 #define FS2105S_RATES (SNDRV_PCM_RATE_32000 | \
34 SNDRV_PCM_RATE_44100 | \
35 SNDRV_PCM_RATE_48000 | \
36 SNDRV_PCM_RATE_88200 | \
37 SNDRV_PCM_RATE_96000)
38 #define FS210X_RATES (SNDRV_PCM_RATE_16000 | FS2105S_RATES)
39 #define FS210X_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
40 SNDRV_PCM_FMTBIT_S24_LE | \
41 SNDRV_PCM_FMTBIT_S24_3LE | \
42 SNDRV_PCM_FMTBIT_S32_LE)
43 #define FS210X_NUM_SUPPLIES ARRAY_SIZE(fs210x_supply_names)
44
45 static const char *const fs210x_supply_names[] = {
46 "pvdd",
47 "dvdd",
48 };
49
50 struct fs210x_platform_data {
51 const char *fwm_name;
52 };
53
54 struct fs210x_priv {
55 struct i2c_client *i2c;
56 struct device *dev;
57 struct regmap *regmap;
58 struct fs210x_platform_data pdata;
59 struct regulator_bulk_data supplies[FS210X_NUM_SUPPLIES];
60 struct gpio_desc *gpio_sdz;
61 struct delayed_work start_work;
62 struct delayed_work fault_check_work;
63 struct fs_amp_lib amp_lib;
64 const struct fs_amp_scene *cur_scene;
65 struct clk *clk_bclk;
66 /*
67 * @lock: Mutex ensuring exclusive access for critical device operations
68 *
69 * This lock serializes access between the following actions:
70 * - Device initialization procedures(probe)
71 * - Enable/disable device(DAPM event)
72 * - Suspend/resume device(PM)
73 * - Runtime scene switching(control)
74 * - Scheduling/execution of delayed works items(delayed works)
75 */
76 struct mutex lock;
77 unsigned int check_interval_ms;
78 unsigned int bclk;
79 unsigned int srate;
80 int scene_id;
81 u16 devid;
82 bool is_inited;
83 bool is_suspended;
84 bool is_bclk_on;
85 bool is_playing;
86 };
87
88 static const unsigned int fs2105s_rates[] = {
89 32000, 44100, 48000, 88200, 96000
90 };
91
92 static const struct snd_pcm_hw_constraint_list fs2105s_constraints = {
93 .count = ARRAY_SIZE(fs2105s_rates),
94 .list = fs2105s_rates,
95 };
96
97 static const unsigned int fs210x_rates[] = {
98 16000, 32000, 44100, 48000, 88200, 96000
99 };
100
101 static const struct snd_pcm_hw_constraint_list fs210x_constraints = {
102 .count = ARRAY_SIZE(fs210x_rates),
103 .list = fs210x_rates,
104 };
105
106 static const struct fs_pll_div fs210x_pll_div[] = {
107 /* bclk, pll1, pll2, pll3 */
108 { 512000, 0x006C, 0x0120, 0x0001 },
109 { 768000, 0x016C, 0x00C0, 0x0001 },
110 { 1024000, 0x016C, 0x0090, 0x0001 },
111 { 1536000, 0x016C, 0x0060, 0x0001 },
112 { 2048000, 0x016C, 0x0090, 0x0002 },
113 { 2304000, 0x016C, 0x0080, 0x0002 },
114 { 3072000, 0x016C, 0x0090, 0x0003 },
115 { 4096000, 0x016C, 0x0090, 0x0004 },
116 { 4608000, 0x016C, 0x0080, 0x0004 },
117 { 6144000, 0x016C, 0x0090, 0x0006 },
118 { 8192000, 0x016C, 0x0090, 0x0008 },
119 { 9216000, 0x016C, 0x0090, 0x0009 },
120 { 12288000, 0x016C, 0x0090, 0x000C },
121 { 16384000, 0x016C, 0x0090, 0x0010 },
122 { 18432000, 0x016C, 0x0090, 0x0012 },
123 { 24576000, 0x016C, 0x0090, 0x0018 },
124 { 1411200, 0x016C, 0x0060, 0x0001 },
125 { 2116800, 0x016C, 0x0080, 0x0002 },
126 { 2822400, 0x016C, 0x0090, 0x0003 },
127 { 4233600, 0x016C, 0x0080, 0x0004 },
128 { 5644800, 0x016C, 0x0090, 0x0006 },
129 { 8467200, 0x016C, 0x0090, 0x0009 },
130 { 11289600, 0x016C, 0x0090, 0x000C },
131 { 16934400, 0x016C, 0x0090, 0x0012 },
132 { 22579200, 0x016C, 0x0090, 0x0018 },
133 { 2000000, 0x017C, 0x0093, 0x0002 },
134 };
135
fs210x_bclk_set(struct fs210x_priv * fs210x,bool on)136 static int fs210x_bclk_set(struct fs210x_priv *fs210x, bool on)
137 {
138 int ret = 0;
139
140 if (!fs210x || !fs210x->dev)
141 return -EINVAL;
142
143 if ((fs210x->is_bclk_on ^ on) == 0)
144 return 0;
145
146 if (on) {
147 clk_set_rate(fs210x->clk_bclk, fs210x->bclk);
148 ret = clk_prepare_enable(fs210x->clk_bclk);
149 fs210x->is_bclk_on = true;
150 fsleep(2000); /* >= 2ms */
151 } else {
152 clk_disable_unprepare(fs210x->clk_bclk);
153 fs210x->is_bclk_on = false;
154 }
155
156 return ret;
157 }
158
fs210x_reg_write(struct fs210x_priv * fs210x,u8 reg,u16 val)159 static int fs210x_reg_write(struct fs210x_priv *fs210x,
160 u8 reg, u16 val)
161 {
162 int ret;
163
164 ret = regmap_write(fs210x->regmap, reg, val);
165 if (ret) {
166 dev_err(fs210x->dev, "Failed to write %02Xh: %d\n", reg, ret);
167 return ret;
168 }
169
170 return 0;
171 }
172
fs210x_reg_read(struct fs210x_priv * fs210x,u8 reg,u16 * pval)173 static int fs210x_reg_read(struct fs210x_priv *fs210x,
174 u8 reg, u16 *pval)
175 {
176 unsigned int val;
177 int ret;
178
179 ret = regmap_read(fs210x->regmap, reg, &val);
180 if (ret) {
181 dev_err(fs210x->dev, "Failed to read %02Xh: %d\n", reg, ret);
182 return ret;
183 }
184
185 *pval = (u16)val;
186
187 return 0;
188 }
189
fs210x_reg_update_bits(struct fs210x_priv * fs210x,u8 reg,u16 mask,u16 val)190 static int fs210x_reg_update_bits(struct fs210x_priv *fs210x,
191 u8 reg, u16 mask, u16 val)
192 {
193 int ret;
194
195 ret = regmap_update_bits(fs210x->regmap, reg, mask, val);
196 if (ret) {
197 dev_err(fs210x->dev, "Failed to update %02Xh: %d\n", reg, ret);
198 return ret;
199 }
200
201 return 0;
202 }
203
fs210x_reg_bulk_write(struct fs210x_priv * fs210x,u8 reg,const void * val,u32 size)204 static int fs210x_reg_bulk_write(struct fs210x_priv *fs210x,
205 u8 reg, const void *val, u32 size)
206 {
207 int ret;
208
209 ret = regmap_bulk_write(fs210x->regmap, reg, val, size / 2);
210 if (ret) {
211 dev_err(fs210x->dev, "Failed to bulk write %02Xh: %d\n",
212 reg, ret);
213 return ret;
214 }
215
216 return 0;
217 }
218
fs210x_write_reg_val(struct fs210x_priv * fs210x,const struct fs_reg_val * regv)219 static inline int fs210x_write_reg_val(struct fs210x_priv *fs210x,
220 const struct fs_reg_val *regv)
221 {
222 return fs210x_reg_write(fs210x, regv->reg, regv->val);
223 }
224
fs210x_write_reg_bits(struct fs210x_priv * fs210x,const struct fs_reg_bits * regu)225 static inline int fs210x_write_reg_bits(struct fs210x_priv *fs210x,
226 const struct fs_reg_bits *regu)
227 {
228 return fs210x_reg_update_bits(fs210x,
229 regu->reg,
230 regu->mask,
231 regu->val);
232 }
233
fs210x_set_cmd_pkg(struct fs210x_priv * fs210x,const struct fs_cmd_pkg * pkg,unsigned int * offset)234 static inline int fs210x_set_cmd_pkg(struct fs210x_priv *fs210x,
235 const struct fs_cmd_pkg *pkg,
236 unsigned int *offset)
237 {
238 int delay_us;
239
240 if (pkg->cmd >= 0x00 && pkg->cmd <= FS210X_REG_MAX) {
241 *offset = sizeof(pkg->regv);
242 return fs210x_write_reg_val(fs210x, &pkg->regv);
243 } else if (pkg->cmd == FS_CMD_UPDATE) {
244 *offset = sizeof(pkg->regb);
245 return fs210x_write_reg_bits(fs210x, &pkg->regb);
246 } else if (pkg->cmd == FS_CMD_DELAY) {
247 if (pkg->regv.val > FS_CMD_DELAY_MS_MAX)
248 return -EOPNOTSUPP;
249 delay_us = pkg->regv.val * 1000; /* ms -> us */
250 fsleep(delay_us);
251 *offset = sizeof(pkg->regv);
252 return 0;
253 }
254
255 dev_err(fs210x->dev, "Invalid pkg cmd: %d\n", pkg->cmd);
256
257 return -EOPNOTSUPP;
258 }
259
fs210x_reg_write_table(struct fs210x_priv * fs210x,const struct fs_reg_table * reg)260 static int fs210x_reg_write_table(struct fs210x_priv *fs210x,
261 const struct fs_reg_table *reg)
262 {
263 const struct fs_cmd_pkg *pkg;
264 unsigned int index, offset;
265 int ret;
266
267 if (!fs210x || !fs210x->dev)
268 return -EINVAL;
269
270 if (!reg || reg->size == 0)
271 return -EFAULT;
272
273 for (index = 0; index < reg->size; index += offset) {
274 pkg = (struct fs_cmd_pkg *)(reg->buf + index);
275 ret = fs210x_set_cmd_pkg(fs210x, pkg, &offset);
276 if (ret) {
277 dev_err(fs210x->dev, "Failed to set cmd pkg: %02X-%d\n",
278 pkg->cmd, ret);
279 return ret;
280 }
281 }
282
283 if (index != reg->size) {
284 dev_err(fs210x->dev, "Invalid reg table size: %d-%d\n",
285 index, reg->size);
286 return -EFAULT;
287 }
288
289 return 0;
290 }
291
fs210x_dev_play(struct fs210x_priv * fs210x)292 static int fs210x_dev_play(struct fs210x_priv *fs210x)
293 {
294 int ret;
295
296 if (!fs210x->is_inited)
297 return -EFAULT;
298
299 if (fs210x->is_playing)
300 return 0;
301
302 ret = fs210x_reg_write(fs210x, FS210X_11H_SYSCTRL,
303 FS210X_11H_DPS_PLAY);
304 if (!ret)
305 fs210x->is_playing = true;
306
307 fsleep(10000); /* >= 10ms */
308
309 return ret;
310 }
311
fs210x_dev_stop(struct fs210x_priv * fs210x)312 static int fs210x_dev_stop(struct fs210x_priv *fs210x)
313 {
314 int ret;
315
316 if (!fs210x->is_inited)
317 return -EFAULT;
318
319 if (!fs210x->is_playing)
320 return 0;
321
322 ret = fs210x_reg_write(fs210x, FS210X_11H_SYSCTRL,
323 FS210X_11H_DPS_PWDN);
324 fs210x->is_playing = false;
325
326 fsleep(30000); /* >= 30ms */
327
328 return ret;
329 }
330
fs210x_set_reg_table(struct fs210x_priv * fs210x,const struct fs_amp_scene * scene)331 static int fs210x_set_reg_table(struct fs210x_priv *fs210x,
332 const struct fs_amp_scene *scene)
333 {
334 const struct fs_amp_scene *cur_scene;
335 const struct fs_reg_table *reg;
336
337 if (!fs210x || !fs210x->dev || !scene)
338 return -EINVAL;
339
340 cur_scene = fs210x->cur_scene;
341 if (!scene->reg || cur_scene == scene) {
342 dev_dbg(fs210x->dev, "Skip writing reg table\n");
343 return 0;
344 }
345
346 reg = scene->reg;
347 dev_dbg(fs210x->dev, "reg table size: %d\n", reg->size);
348
349 return fs210x_reg_write_table(fs210x, reg);
350 }
351
fs210x_set_woofer_table(struct fs210x_priv * fs210x)352 static int fs210x_set_woofer_table(struct fs210x_priv *fs210x)
353 {
354 const struct fs_file_table *woofer;
355 const struct fs_fwm_table *table;
356 int ret;
357
358 if (!fs210x || !fs210x->dev)
359 return -EINVAL;
360
361 /* NOTE: fs2105s has woofer ram only */
362 if (fs210x->devid != FS2105S_DEVICE_ID)
363 return 0;
364
365 table = fs210x->amp_lib.table[FS_INDEX_WOOFER];
366 if (!table) {
367 dev_dbg(fs210x->dev, "Skip writing woofer table\n");
368 return 0;
369 }
370
371 woofer = (struct fs_file_table *)table->buf;
372 dev_dbg(fs210x->dev, "woofer table size: %d\n", woofer->size);
373 /* Unit of woofer data is u32(4 bytes) */
374 if (woofer->size == 0 || (woofer->size & 0x3)) {
375 dev_err(fs210x->dev, "Invalid woofer size: %d\n",
376 woofer->size);
377 return -EINVAL;
378 }
379
380 ret = fs210x_reg_write(fs210x, FS210X_46H_DACEQA,
381 FS2105S_46H_CAM_BURST_W);
382 ret |= fs210x_reg_bulk_write(fs210x, FS210X_42H_DACEQWL,
383 woofer->buf, woofer->size);
384
385 return ret;
386 }
387
fs210x_set_effect_table(struct fs210x_priv * fs210x,const struct fs_amp_scene * scene)388 static int fs210x_set_effect_table(struct fs210x_priv *fs210x,
389 const struct fs_amp_scene *scene)
390 {
391 const struct fs_amp_scene *cur_scene;
392 const struct fs_file_table *effect;
393 int half_size;
394 int ret;
395
396 if (!fs210x || !fs210x->dev || !scene)
397 return -EINVAL;
398
399 cur_scene = fs210x->cur_scene;
400 if (!scene->effect || cur_scene == scene) {
401 dev_dbg(fs210x->dev, "Skip writing effect table\n");
402 return 0;
403 }
404
405 effect = scene->effect;
406 dev_dbg(fs210x->dev, "effect table size: %d\n", effect->size);
407
408 /* Unit of effect data is u32(4 bytes), 2 channels */
409 if (effect->size == 0 || (effect->size & 0x7)) {
410 dev_err(fs210x->dev, "Invalid effect size: %d\n",
411 effect->size);
412 return -EINVAL;
413 }
414
415 half_size = effect->size / 2;
416
417 /* Left channel */
418 ret = fs210x_reg_write(fs210x, FS210X_46H_DACEQA,
419 FS210X_46H_CAM_BURST_L);
420 ret |= fs210x_reg_bulk_write(fs210x, FS210X_42H_DACEQWL,
421 effect->buf, half_size);
422 if (ret)
423 return ret;
424
425 /* Right channel */
426 ret = fs210x_reg_write(fs210x, FS210X_46H_DACEQA,
427 FS210X_46H_CAM_BURST_R);
428 ret |= fs210x_reg_bulk_write(fs210x, FS210X_42H_DACEQWL,
429 effect->buf + half_size, half_size);
430
431 return ret;
432 }
433
fs210x_access_dsp_ram(struct fs210x_priv * fs210x,bool enable)434 static int fs210x_access_dsp_ram(struct fs210x_priv *fs210x, bool enable)
435 {
436 int ret;
437
438 if (!fs210x || !fs210x->dev)
439 return -EINVAL;
440
441 if (enable) {
442 ret = fs210x_reg_write(fs210x, FS210X_11H_SYSCTRL,
443 FS210X_11H_DPS_HIZ);
444 ret |= fs210x_reg_write(fs210x, FS210X_0BH_ACCKEY,
445 FS210X_0BH_ACCKEY_ON);
446 } else {
447 ret = fs210x_reg_write(fs210x, FS210X_0BH_ACCKEY,
448 FS210X_0BH_ACCKEY_OFF);
449 ret |= fs210x_reg_write(fs210x, FS210X_11H_SYSCTRL,
450 FS210X_11H_DPS_PWDN);
451 }
452
453 fsleep(10000); /* >= 10ms */
454
455 return ret;
456 }
457
fs210x_write_dsp_effect(struct fs210x_priv * fs210x,const struct fs_amp_scene * scene,int scene_id)458 static int fs210x_write_dsp_effect(struct fs210x_priv *fs210x,
459 const struct fs_amp_scene *scene,
460 int scene_id)
461 {
462 int ret;
463
464 if (!fs210x || !scene)
465 return -EINVAL;
466
467 ret = fs210x_access_dsp_ram(fs210x, true);
468 if (ret) {
469 dev_err(fs210x->dev, "Failed to access dsp: %d\n", ret);
470 goto tag_exit;
471 }
472
473 ret = fs210x_set_effect_table(fs210x, scene);
474 if (ret) {
475 dev_err(fs210x->dev, "Failed to set effect: %d\n", ret);
476 goto tag_exit;
477 }
478
479 if (scene_id == FS210X_INIT_SCENE)
480 ret = fs210x_set_woofer_table(fs210x);
481
482 tag_exit:
483 fs210x_reg_write(fs210x, FS210X_46H_DACEQA,
484 FS210X_46H_CAM_CLEAR);
485 fs210x_access_dsp_ram(fs210x, false);
486
487 return ret;
488 }
489
fs210x_check_scene(struct fs210x_priv * fs210x,int scene_id,bool * skip_set)490 static int fs210x_check_scene(struct fs210x_priv *fs210x,
491 int scene_id, bool *skip_set)
492 {
493 struct fs_amp_lib *amp_lib;
494
495 if (!fs210x || !skip_set)
496 return -EINVAL;
497
498 amp_lib = &fs210x->amp_lib;
499 if (amp_lib->scene_count == 0 || !amp_lib->scene) {
500 dev_err(fs210x->dev, "There's no scene data\n");
501 return -EINVAL;
502 }
503
504 if (scene_id < 0 || scene_id >= amp_lib->scene_count) {
505 dev_err(fs210x->dev, "Invalid scene_id: %d\n", scene_id);
506 return -EINVAL;
507 }
508
509 if (fs210x->scene_id == scene_id) {
510 dev_dbg(fs210x->dev, "Skip to set same scene\n");
511 return 0;
512 }
513
514 *skip_set = false;
515
516 return 0;
517 }
518
fs210x_set_scene(struct fs210x_priv * fs210x,int scene_id)519 static int fs210x_set_scene(struct fs210x_priv *fs210x, int scene_id)
520 {
521 const struct fs_amp_scene *scene;
522 bool skip_set = true;
523 bool is_playing;
524 int ret;
525
526 if (!fs210x || !fs210x->dev)
527 return -EINVAL;
528
529 ret = fs210x_check_scene(fs210x, scene_id, &skip_set);
530 if (ret || skip_set)
531 return ret;
532
533 scene = fs210x->amp_lib.scene + scene_id;
534 dev_info(fs210x->dev, "Switch scene.%d: %s\n",
535 scene_id, scene->name);
536
537 is_playing = fs210x->is_playing;
538 if (is_playing)
539 fs210x_dev_stop(fs210x);
540
541 ret = fs210x_set_reg_table(fs210x, scene);
542 if (ret) {
543 dev_err(fs210x->dev, "Failed to set reg: %d\n", ret);
544 return ret;
545 }
546
547 ret = fs210x_write_dsp_effect(fs210x, scene, scene_id);
548 if (ret) {
549 dev_err(fs210x->dev, "Failed to write ram: %d\n", ret);
550 return ret;
551 }
552
553 fs210x->cur_scene = scene;
554 fs210x->scene_id = scene_id;
555
556 if (is_playing)
557 fs210x_dev_play(fs210x);
558
559 return 0;
560 }
561
fs210x_init_chip(struct fs210x_priv * fs210x)562 static int fs210x_init_chip(struct fs210x_priv *fs210x)
563 {
564 int scene_id;
565 int ret;
566
567 regcache_cache_bypass(fs210x->regmap, true);
568
569 if (!fs210x->gpio_sdz) {
570 /* Gpio is not found, i2c reset */
571 ret = fs210x_reg_write(fs210x, FS210X_10H_PWRCTRL,
572 FS210X_10H_I2C_RESET);
573 if (ret)
574 goto tag_power_down;
575 } else {
576 /* gpio reset, deactivate */
577 gpiod_set_value_cansleep(fs210x->gpio_sdz, 0);
578 }
579
580 fsleep(10000); /* >= 10ms */
581
582 /* Backup scene id */
583 scene_id = fs210x->scene_id;
584 fs210x->scene_id = -1;
585
586 /* Init registers/RAM by init scene */
587 ret = fs210x_set_scene(fs210x, FS210X_INIT_SCENE);
588 if (ret)
589 goto tag_power_down;
590
591 /*
592 * If the firmware has effect scene(s),
593 * we load effect scene by default scene or scene_id
594 */
595 if (fs210x->amp_lib.scene_count > 1) {
596 if (scene_id < FS210X_DEFAULT_SCENE)
597 scene_id = FS210X_DEFAULT_SCENE;
598 ret = fs210x_set_scene(fs210x, scene_id);
599 if (ret)
600 goto tag_power_down;
601 }
602
603 tag_power_down:
604 /* Power down the device */
605 ret |= fs210x_reg_write(fs210x, FS210X_11H_SYSCTRL,
606 FS210X_11H_DPS_PWDN);
607 fsleep(10000); /* >= 10ms */
608
609 regcache_cache_bypass(fs210x->regmap, false);
610 if (!ret) {
611 regcache_cache_only(fs210x->regmap, false);
612 regcache_mark_dirty(fs210x->regmap);
613 regcache_sync(fs210x->regmap);
614 fs210x->is_inited = true;
615 }
616
617 return ret;
618 }
619
fs210x_set_i2s_params(struct fs210x_priv * fs210x)620 static int fs210x_set_i2s_params(struct fs210x_priv *fs210x)
621 {
622 const struct fs_i2s_srate params[] = {
623 { 16000, 0x3 },
624 { 32000, 0x7 },
625 { 44100, 0x8 },
626 { 48000, 0x9 },
627 { 88200, 0xA },
628 { 96000, 0xB },
629 };
630 u16 val;
631 int i, ret;
632
633 for (i = 0; i < ARRAY_SIZE(params); i++) {
634 if (params[i].srate != fs210x->srate)
635 continue;
636 val = params[i].i2ssr << FS210X_17H_I2SSR_SHIFT;
637 ret = fs210x_reg_update_bits(fs210x,
638 FS210X_17H_I2SCTRL,
639 FS210X_17H_I2SSR_MASK,
640 val);
641 return ret;
642 }
643
644 dev_err(fs210x->dev, "Invalid sample rate: %d\n", fs210x->srate);
645
646 return -EINVAL;
647 }
648
fs210x_get_pll_div(struct fs210x_priv * fs210x,const struct fs_pll_div ** pll_div)649 static int fs210x_get_pll_div(struct fs210x_priv *fs210x,
650 const struct fs_pll_div **pll_div)
651 {
652 int i;
653
654 if (!fs210x || !pll_div)
655 return -EINVAL;
656
657 for (i = 0; i < ARRAY_SIZE(fs210x_pll_div); i++) {
658 if (fs210x_pll_div[i].bclk != fs210x->bclk)
659 continue;
660 *pll_div = fs210x_pll_div + i;
661 return 0;
662 }
663
664 dev_err(fs210x->dev, "No PLL table for bclk: %d\n", fs210x->bclk);
665
666 return -EFAULT;
667 }
668
fs210x_set_hw_params(struct fs210x_priv * fs210x)669 static int fs210x_set_hw_params(struct fs210x_priv *fs210x)
670 {
671 const struct fs_pll_div *pll_div;
672 int ret;
673
674 ret = fs210x_set_i2s_params(fs210x);
675 if (ret) {
676 dev_err(fs210x->dev, "Failed to set i2s params: %d\n", ret);
677 return ret;
678 }
679
680 /* Set pll params */
681 ret = fs210x_get_pll_div(fs210x, &pll_div);
682 if (ret)
683 return ret;
684
685 ret = fs210x_reg_write(fs210x, FS210X_A1H_PLLCTRL1, pll_div->pll1);
686 ret |= fs210x_reg_write(fs210x, FS210X_A2H_PLLCTRL2, pll_div->pll2);
687 ret |= fs210x_reg_write(fs210x, FS210X_A3H_PLLCTRL3, pll_div->pll3);
688
689 return ret;
690 }
691
fs210x_dai_startup(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)692 static int fs210x_dai_startup(struct snd_pcm_substream *substream,
693 struct snd_soc_dai *dai)
694 {
695 const struct snd_pcm_hw_constraint_list *list;
696 struct fs210x_priv *fs210x;
697 int ret;
698
699 fs210x = snd_soc_component_get_drvdata(dai->component);
700 if (!fs210x) {
701 pr_err("dai_startup: fs210x is null\n");
702 return -EINVAL;
703 }
704
705 if (!substream->runtime)
706 return 0;
707
708 ret = snd_pcm_hw_constraint_mask64(substream->runtime,
709 SNDRV_PCM_HW_PARAM_FORMAT,
710 FS210X_FORMATS);
711 if (ret < 0) {
712 dev_err(fs210x->dev,
713 "Failed to set hw param format: %d\n", ret);
714 return ret;
715 }
716
717 if (fs210x->devid == FS2105S_DEVICE_ID)
718 list = &fs2105s_constraints;
719 else
720 list = &fs210x_constraints;
721
722 ret = snd_pcm_hw_constraint_list(substream->runtime, 0,
723 SNDRV_PCM_HW_PARAM_RATE,
724 list);
725 if (ret < 0) {
726 dev_err(fs210x->dev,
727 "Failed to set hw param rate: %d\n", ret);
728 return ret;
729 }
730
731 return 0;
732 }
733
fs210x_dai_set_fmt(struct snd_soc_dai * dai,unsigned int fmt)734 static int fs210x_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
735 {
736 struct fs210x_priv *fs210x;
737
738 fs210x = snd_soc_component_get_drvdata(dai->component);
739
740 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
741 case SND_SOC_DAIFMT_CBC_CFC:
742 /* Only supports consumer mode */
743 break;
744 default:
745 dev_err(fs210x->dev, "Only supports consumer mode\n");
746 return -EINVAL;
747 }
748
749 return 0;
750 }
751
fs210x_dai_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)752 static int fs210x_dai_hw_params(struct snd_pcm_substream *substream,
753 struct snd_pcm_hw_params *params,
754 struct snd_soc_dai *dai)
755 {
756 struct fs210x_priv *fs210x;
757 int chn_num;
758 int ret;
759
760 if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
761 return 0;
762
763 fs210x = snd_soc_component_get_drvdata(dai->component);
764
765 fs210x->srate = params_rate(params);
766 fs210x->bclk = snd_soc_params_to_bclk(params);
767 chn_num = params_channels(params);
768 if (chn_num == 1) /* mono */
769 fs210x->bclk *= 2; /* I2S bus has 2 channels */
770
771 /* The FS2105S can't support 16kHz sample rate. */
772 if (fs210x->devid == FS2105S_DEVICE_ID && fs210x->srate == 16000)
773 return -EOPNOTSUPP;
774
775 scoped_guard(mutex, &fs210x->lock)
776 ret = fs210x_set_hw_params(fs210x);
777 if (ret)
778 dev_err(fs210x->dev, "Failed to set hw params: %d\n", ret);
779
780 return ret;
781 }
782
fs210x_dai_mute(struct snd_soc_dai * dai,int mute,int stream)783 static int fs210x_dai_mute(struct snd_soc_dai *dai, int mute, int stream)
784 {
785 struct fs210x_priv *fs210x;
786 unsigned long delay;
787
788 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
789 return 0;
790
791 fs210x = snd_soc_component_get_drvdata(dai->component);
792
793 scoped_guard(mutex, &fs210x->lock) {
794 if (!fs210x->is_inited || fs210x->is_suspended)
795 return 0;
796 }
797
798 if (mute) {
799 cancel_delayed_work_sync(&fs210x->fault_check_work);
800 cancel_delayed_work_sync(&fs210x->start_work);
801 } else {
802 delay = msecs_to_jiffies(fs210x->check_interval_ms);
803 schedule_delayed_work(&fs210x->fault_check_work, delay);
804 }
805
806 return 0;
807 }
808
fs210x_dai_trigger(struct snd_pcm_substream * substream,int cmd,struct snd_soc_dai * dai)809 static int fs210x_dai_trigger(struct snd_pcm_substream *substream,
810 int cmd, struct snd_soc_dai *dai)
811 {
812 struct fs210x_priv *fs210x;
813
814 fs210x = snd_soc_component_get_drvdata(dai->component);
815
816 scoped_guard(mutex, &fs210x->lock) {
817 if (!fs210x->is_inited || fs210x->is_suspended || fs210x->is_playing)
818 return 0;
819 }
820
821 switch (cmd) {
822 case SNDRV_PCM_TRIGGER_START:
823 case SNDRV_PCM_TRIGGER_RESUME:
824 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
825 /*
826 * According to the power up/down sequence of FS210x,
827 * it requests the I2S clock has been present
828 * and stable(>= 2ms) before playing.
829 */
830 schedule_delayed_work(&fs210x->start_work,
831 msecs_to_jiffies(FS210X_START_DELAY_MS));
832 break;
833
834 default:
835 break;
836 }
837
838 return 0;
839 }
840
fs210x_start_work(struct work_struct * work)841 static void fs210x_start_work(struct work_struct *work)
842 {
843 struct fs210x_priv *fs210x;
844 int ret;
845
846 fs210x = container_of(work, struct fs210x_priv, start_work.work);
847
848 guard(mutex)(&fs210x->lock);
849
850 ret = fs210x_dev_play(fs210x);
851 if (ret)
852 dev_err(fs210x->dev, "Failed to start playing: %d\n", ret);
853 }
854
fs210x_fault_check_work(struct work_struct * work)855 static void fs210x_fault_check_work(struct work_struct *work)
856 {
857 struct fs210x_priv *fs210x;
858 u16 status;
859 int ret;
860
861 fs210x = container_of(work, struct fs210x_priv, fault_check_work.work);
862
863 scoped_guard(mutex, &fs210x->lock) {
864 if (!fs210x->is_inited || fs210x->is_suspended || !fs210x->is_playing)
865 return;
866
867 ret = fs210x_reg_read(fs210x, FS210X_05H_ANASTAT, &status);
868 }
869
870 if (ret)
871 return;
872
873 if (!(status & FS210X_05H_PVDD_MASK))
874 dev_err(fs210x->dev, "PVDD fault\n");
875 if (status & FS210X_05H_OCDL_MASK)
876 dev_err(fs210x->dev, "OC detected\n");
877 if (status & FS210X_05H_UVDL_MASK)
878 dev_err(fs210x->dev, "UV detected\n");
879 if (status & FS210X_05H_OVDL_MASK)
880 dev_err(fs210x->dev, "OV detected\n");
881 if (status & FS210X_05H_OTPDL_MASK)
882 dev_err(fs210x->dev, "OT detected\n");
883 if (status & FS210X_05H_OCRDL_MASK)
884 dev_err(fs210x->dev, "OCR detected\n");
885 if (status & FS210X_05H_OCLDL_MASK)
886 dev_err(fs210x->dev, "OCL detected\n");
887 if (status & FS210X_05H_DCRDL_MASK)
888 dev_err(fs210x->dev, "DCR detected\n");
889 if (status & FS210X_05H_DCLDL_MASK)
890 dev_err(fs210x->dev, "DCL detected\n");
891 if (status & FS210X_05H_SRDL_MASK)
892 dev_err(fs210x->dev, "SR detected\n");
893 if (status & FS210X_05H_OTWDL_MASK)
894 dev_err(fs210x->dev, "OTW detected\n");
895 if (!(status & FS210X_05H_AMPS_MASK))
896 dev_dbg(fs210x->dev, "Amplifier unready\n");
897 if (!(status & FS210X_05H_PLLS_MASK))
898 dev_err(fs210x->dev, "PLL unlock\n");
899 if (!(status & FS210X_05H_ANAS_MASK))
900 dev_err(fs210x->dev, "Analog power fault\n");
901
902 schedule_delayed_work(&fs210x->fault_check_work,
903 msecs_to_jiffies(fs210x->check_interval_ms));
904 }
905
fs210x_get_drvdata_from_kctrl(struct snd_kcontrol * kctrl,struct fs210x_priv ** fs210x)906 static int fs210x_get_drvdata_from_kctrl(struct snd_kcontrol *kctrl,
907 struct fs210x_priv **fs210x)
908 {
909 struct snd_soc_component *cmpnt;
910
911 if (!kctrl) {
912 pr_err("fs210x: kcontrol is null\n");
913 return -EINVAL;
914 }
915
916 cmpnt = snd_kcontrol_chip(kctrl);
917 if (!cmpnt) {
918 pr_err("fs210x: component is null\n");
919 return -EINVAL;
920 }
921
922 *fs210x = snd_soc_component_get_drvdata(cmpnt);
923
924 return 0;
925 }
926
fs210x_effect_scene_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)927 static int fs210x_effect_scene_info(struct snd_kcontrol *kcontrol,
928 struct snd_ctl_elem_info *uinfo)
929 {
930 const struct fs_amp_scene *scene;
931 struct fs210x_priv *fs210x;
932 const char *name = "N/A";
933 int idx, count;
934 int ret;
935
936 ret = fs210x_get_drvdata_from_kctrl(kcontrol, &fs210x);
937 if (ret || !fs210x->dev) {
938 pr_err("scene_effect_info: fs210x is null\n");
939 return -EINVAL;
940 }
941
942 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
943 uinfo->count = 1;
944
945 count = fs210x->amp_lib.scene_count - 1; /* Skip init scene */
946 if (count < 1) {
947 uinfo->value.enumerated.items = 0;
948 return 0;
949 }
950
951 uinfo->value.enumerated.items = count;
952 if (uinfo->value.enumerated.item >= count)
953 uinfo->value.enumerated.item = count - 1;
954
955 idx = uinfo->value.enumerated.item;
956 scene = fs210x->amp_lib.scene + idx + 1;
957 if (scene->name)
958 name = scene->name;
959
960 strscpy(uinfo->value.enumerated.name, name);
961
962 return 0;
963 }
964
fs210x_effect_scene_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)965 static int fs210x_effect_scene_get(struct snd_kcontrol *kcontrol,
966 struct snd_ctl_elem_value *ucontrol)
967 {
968 struct fs210x_priv *fs210x;
969 int index;
970 int ret;
971
972 ret = fs210x_get_drvdata_from_kctrl(kcontrol, &fs210x);
973 if (ret || !fs210x->dev) {
974 pr_err("scene_effect_get: fs210x is null\n");
975 return -EINVAL;
976 }
977
978 /* The id of effect scene is from 1 to N. */
979 if (fs210x->scene_id < 1)
980 return -EINVAL;
981
982 guard(mutex)(&fs210x->lock);
983 /*
984 * FS210x has scene(s) as below:
985 * init scene: id = 0
986 * effect scene(s): id = 1~N (optional)
987 * effect_index = scene_id - 1
988 */
989 index = fs210x->scene_id - 1;
990 ucontrol->value.integer.value[0] = index;
991
992 return 0;
993 }
994
fs210x_effect_scene_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)995 static int fs210x_effect_scene_put(struct snd_kcontrol *kcontrol,
996 struct snd_ctl_elem_value *ucontrol)
997 {
998 struct fs210x_priv *fs210x;
999 int scene_id, scene_count;
1000 bool is_changed = false;
1001 int ret;
1002
1003 ret = fs210x_get_drvdata_from_kctrl(kcontrol, &fs210x);
1004 if (ret || !fs210x->dev) {
1005 pr_err("scene_effect_put: fs210x is null\n");
1006 return -EINVAL;
1007 }
1008
1009 guard(mutex)(&fs210x->lock);
1010
1011 /*
1012 * FS210x has scene(s) as below:
1013 * init scene: id = 0 (It's set in fs210x_init_chip() only)
1014 * effect scene(s): id = 1~N (optional)
1015 * scene_id = effect_index + 1.
1016 */
1017 scene_id = ucontrol->value.integer.value[0] + 1;
1018 scene_count = fs210x->amp_lib.scene_count - 1; /* Skip init scene */
1019 if (scene_id < 1 || scene_id > scene_count)
1020 return -ERANGE;
1021
1022 if (scene_id != fs210x->scene_id)
1023 is_changed = true;
1024
1025 if (fs210x->is_suspended) {
1026 fs210x->scene_id = scene_id;
1027 return is_changed;
1028 }
1029
1030 ret = fs210x_set_scene(fs210x, scene_id);
1031 if (ret)
1032 dev_err(fs210x->dev, "Failed to set scene: %d\n", ret);
1033
1034 if (!ret && is_changed)
1035 return 1;
1036
1037 return ret;
1038 }
1039
fs210x_playback_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kc,int event)1040 static int fs210x_playback_event(struct snd_soc_dapm_widget *w,
1041 struct snd_kcontrol *kc, int event)
1042 {
1043 struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
1044 struct fs210x_priv *fs210x = snd_soc_component_get_drvdata(cmpnt);
1045 int ret = 0;
1046
1047 guard(mutex)(&fs210x->lock);
1048
1049 if (fs210x->is_suspended)
1050 return 0;
1051
1052 switch (event) {
1053 case SND_SOC_DAPM_PRE_PMU:
1054 /*
1055 * If there is no bclk for us to set the clock output,
1056 * we will enable the device(start_work) in dai trigger.
1057 */
1058 if (!fs210x->clk_bclk)
1059 break;
1060 fs210x_bclk_set(fs210x, true);
1061 ret = fs210x_dev_play(fs210x);
1062 break;
1063 case SND_SOC_DAPM_POST_PMD:
1064 ret = fs210x_dev_stop(fs210x);
1065 fs210x_bclk_set(fs210x, false);
1066 break;
1067 default:
1068 break;
1069 }
1070
1071 return ret;
1072 }
1073
1074 static const struct snd_soc_dai_ops fs210x_dai_ops = {
1075 .startup = fs210x_dai_startup,
1076 .set_fmt = fs210x_dai_set_fmt,
1077 .hw_params = fs210x_dai_hw_params,
1078 .mute_stream = fs210x_dai_mute,
1079 .trigger = fs210x_dai_trigger,
1080 };
1081
1082 static const struct snd_soc_dai_driver fs210x_dai = {
1083 .name = FS210X_DEFAULT_DAI_NAME,
1084 .playback = {
1085 .stream_name = "Playback",
1086 .channels_min = 1,
1087 .channels_max = 2,
1088 .rates = FS210X_RATES,
1089 .formats = FS210X_FORMATS,
1090 },
1091 .capture = {
1092 .stream_name = "Capture",
1093 .channels_min = 1,
1094 .channels_max = 2,
1095 .rates = FS210X_RATES,
1096 .formats = FS210X_FORMATS,
1097 },
1098 .ops = &fs210x_dai_ops,
1099 .symmetric_rate = 1,
1100 .symmetric_sample_bits = 1,
1101 };
1102
1103 static const DECLARE_TLV_DB_SCALE(fs2105s_vol_tlv, -9709, 19, 1);
1104 static const DECLARE_TLV_DB_SCALE(fs210x_vol_tlv, -13357, 19, 1);
1105
1106 static const struct snd_kcontrol_new fs2105s_vol_control[] = {
1107 SOC_DOUBLE_R_TLV("PCM Playback Volume",
1108 FS210X_39H_LVOLCTRL, FS210X_3AH_RVOLCTRL,
1109 7, 0x1FF, 0, fs2105s_vol_tlv),
1110 };
1111
1112 static const struct snd_kcontrol_new fs210x_vol_control[] = {
1113 SOC_DOUBLE_R_TLV("PCM Playback Volume",
1114 FS210X_39H_LVOLCTRL, FS210X_3AH_RVOLCTRL,
1115 6, 0x2BF, 0, fs210x_vol_tlv),
1116 };
1117
1118 static const struct snd_kcontrol_new fs210x_controls[] = {
1119 SOC_DOUBLE("DAC Mute Switch", FS210X_30H_DACCTRL, 4, 8, 1, 0),
1120 SOC_DOUBLE("DAC Fade Switch", FS210X_30H_DACCTRL, 5, 9, 1, 0),
1121 };
1122
1123 static const struct snd_kcontrol_new fs210x_scene_control[] = {
1124 FS_SOC_ENUM_EXT("Effect Scene",
1125 fs210x_effect_scene_info,
1126 fs210x_effect_scene_get,
1127 fs210x_effect_scene_put),
1128 };
1129
1130 static const struct snd_soc_dapm_widget fs210x_dapm_widgets[] = {
1131 SND_SOC_DAPM_AIF_IN_E("AIF IN", "Playback", 0, SND_SOC_NOPM, 0, 0,
1132 fs210x_playback_event,
1133 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1134 SND_SOC_DAPM_AIF_OUT("AIF OUT", "Capture", 0, SND_SOC_NOPM, 0, 0),
1135 SND_SOC_DAPM_OUTPUT("OUTL"),
1136 SND_SOC_DAPM_OUTPUT("OUTR"),
1137 SND_SOC_DAPM_INPUT("SDO"),
1138 };
1139
1140 static const struct snd_soc_dapm_route fs210x_dapm_routes[] = {
1141 { "OUTL", NULL, "AIF IN" },
1142 { "OUTR", NULL, "AIF IN" },
1143 { "AIF OUT", NULL, "SDO" },
1144 };
1145
fs210x_add_mixer_controls(struct fs210x_priv * fs210x,struct snd_soc_component * cmpnt)1146 static int fs210x_add_mixer_controls(struct fs210x_priv *fs210x,
1147 struct snd_soc_component *cmpnt)
1148 {
1149 const struct snd_kcontrol_new *kctrl;
1150 int count;
1151 int ret;
1152
1153 if (!fs210x || !cmpnt)
1154 return -EINVAL;
1155
1156 if (fs210x->devid == FS2105S_DEVICE_ID) {
1157 kctrl = fs2105s_vol_control;
1158 count = ARRAY_SIZE(fs2105s_vol_control);
1159 } else {
1160 kctrl = fs210x_vol_control;
1161 count = ARRAY_SIZE(fs210x_vol_control);
1162 }
1163
1164 ret = snd_soc_add_component_controls(cmpnt, kctrl, count);
1165 if (ret)
1166 return ret;
1167
1168 /*
1169 * If the firmware has no scene or only init scene,
1170 * we skip adding this mixer control.
1171 */
1172 if (fs210x->amp_lib.scene_count < 2)
1173 return 0;
1174
1175 kctrl = fs210x_scene_control;
1176 count = ARRAY_SIZE(fs210x_scene_control);
1177
1178 return snd_soc_add_component_controls(cmpnt, kctrl, count);
1179 }
1180
fs210x_probe(struct snd_soc_component * cmpnt)1181 static int fs210x_probe(struct snd_soc_component *cmpnt)
1182 {
1183 struct fs210x_priv *fs210x;
1184 int ret;
1185
1186 fs210x = snd_soc_component_get_drvdata(cmpnt);
1187 if (!fs210x || !fs210x->dev)
1188 return -EINVAL;
1189
1190 fs210x->amp_lib.dev = fs210x->dev;
1191 fs210x->amp_lib.devid = fs210x->devid;
1192
1193 ret = fs_amp_load_firmware(&fs210x->amp_lib, fs210x->pdata.fwm_name);
1194 if (ret)
1195 return ret;
1196
1197 ret = fs210x_add_mixer_controls(fs210x, cmpnt);
1198 if (ret)
1199 return ret;
1200
1201 guard(mutex)(&fs210x->lock);
1202
1203 return fs210x_init_chip(fs210x);
1204 }
1205
fs210x_remove(struct snd_soc_component * cmpnt)1206 static void fs210x_remove(struct snd_soc_component *cmpnt)
1207 {
1208 struct fs210x_priv *fs210x;
1209
1210 fs210x = snd_soc_component_get_drvdata(cmpnt);
1211 if (!fs210x || !fs210x->dev)
1212 return;
1213
1214 cancel_delayed_work_sync(&fs210x->start_work);
1215 cancel_delayed_work_sync(&fs210x->fault_check_work);
1216 }
1217
1218 #ifdef CONFIG_PM
fs210x_suspend(struct snd_soc_component * cmpnt)1219 static int fs210x_suspend(struct snd_soc_component *cmpnt)
1220 {
1221 struct fs210x_priv *fs210x;
1222 int ret;
1223
1224 fs210x = snd_soc_component_get_drvdata(cmpnt);
1225 if (!fs210x || !fs210x->dev)
1226 return -EINVAL;
1227
1228 regcache_cache_only(fs210x->regmap, true);
1229
1230 scoped_guard(mutex, &fs210x->lock) {
1231 fs210x->cur_scene = NULL;
1232 fs210x->is_inited = false;
1233 fs210x->is_playing = false;
1234 fs210x->is_suspended = true;
1235
1236 gpiod_set_value_cansleep(fs210x->gpio_sdz, 1); /* Active */
1237 fsleep(30000); /* >= 30ms */
1238 }
1239
1240 cancel_delayed_work_sync(&fs210x->start_work);
1241 cancel_delayed_work_sync(&fs210x->fault_check_work);
1242
1243 ret = regulator_bulk_disable(FS210X_NUM_SUPPLIES, fs210x->supplies);
1244 if (ret) {
1245 dev_err(fs210x->dev, "Failed to suspend: %d\n", ret);
1246 return ret;
1247 }
1248
1249 return 0;
1250 }
1251
fs210x_resume(struct snd_soc_component * cmpnt)1252 static int fs210x_resume(struct snd_soc_component *cmpnt)
1253 {
1254 struct fs210x_priv *fs210x;
1255 int ret;
1256
1257 fs210x = snd_soc_component_get_drvdata(cmpnt);
1258 if (!fs210x || !fs210x->dev)
1259 return -EINVAL;
1260
1261 ret = regulator_bulk_enable(FS210X_NUM_SUPPLIES, fs210x->supplies);
1262 if (ret) {
1263 dev_err(fs210x->dev, "Failed to enable supplies: %d\n", ret);
1264 return ret;
1265 }
1266
1267 guard(mutex)(&fs210x->lock);
1268
1269 fs210x->is_suspended = false;
1270
1271 return fs210x_init_chip(fs210x);
1272 }
1273 #else
1274 #define fs210x_suspend NULL
1275 #define fs210x_resume NULL
1276 #endif // CONFIG_PM
1277
fs210x_volatile_registers(struct device * dev,unsigned int reg)1278 static bool fs210x_volatile_registers(struct device *dev, unsigned int reg)
1279 {
1280 switch (reg) {
1281 case FS210X_00H_STATUS ... FS210X_0FH_I2CADDR:
1282 case FS210X_ABH_INTSTAT:
1283 case FS210X_ACH_INTSTATR:
1284 return true;
1285 default:
1286 return false;
1287 }
1288 }
1289
1290 static const struct snd_soc_component_driver fs210x_soc_component_dev = {
1291 .probe = fs210x_probe,
1292 .remove = fs210x_remove,
1293 .suspend = fs210x_suspend,
1294 .resume = fs210x_resume,
1295 .controls = fs210x_controls,
1296 .num_controls = ARRAY_SIZE(fs210x_controls),
1297 .dapm_widgets = fs210x_dapm_widgets,
1298 .num_dapm_widgets = ARRAY_SIZE(fs210x_dapm_widgets),
1299 .dapm_routes = fs210x_dapm_routes,
1300 .num_dapm_routes = ARRAY_SIZE(fs210x_dapm_routes),
1301 };
1302
1303 static const struct regmap_config fs210x_regmap = {
1304 .reg_bits = 8,
1305 .val_bits = 16,
1306 .max_register = FS210X_REG_MAX,
1307 .val_format_endian = REGMAP_ENDIAN_BIG,
1308 .cache_type = REGCACHE_MAPLE,
1309 .volatile_reg = fs210x_volatile_registers,
1310 };
1311
fs210x_detect_device(struct fs210x_priv * fs210x)1312 static int fs210x_detect_device(struct fs210x_priv *fs210x)
1313 {
1314 u16 devid;
1315 int ret;
1316
1317 ret = fs210x_reg_read(fs210x, FS210X_03H_DEVID, &devid);
1318 if (ret)
1319 return ret;
1320
1321 fs210x->devid = HI_U16(devid);
1322
1323 switch (fs210x->devid) {
1324 case FS210X_DEVICE_ID:
1325 dev_info(fs210x->dev, "FS2104 detected\n");
1326 break;
1327 case FS2105S_DEVICE_ID:
1328 dev_info(fs210x->dev, "FS2105S detected\n");
1329 break;
1330 default:
1331 dev_err(fs210x->dev, "DEVID: 0x%04X dismatch\n", devid);
1332 return -ENODEV;
1333 }
1334
1335 return 0;
1336 }
1337
fs210x_parse_dts(struct fs210x_priv * fs210x,struct fs210x_platform_data * pdata)1338 static int fs210x_parse_dts(struct fs210x_priv *fs210x,
1339 struct fs210x_platform_data *pdata)
1340 {
1341 struct device_node *node = fs210x->dev->of_node;
1342 int i, ret;
1343
1344 if (!node)
1345 return 0;
1346
1347 ret = of_property_read_string(node, "firmware-name", &pdata->fwm_name);
1348 if (ret)
1349 pdata->fwm_name = FS210X_DEFAULT_FWM_NAME;
1350
1351 fs210x->gpio_sdz = devm_gpiod_get_optional(fs210x->dev,
1352 "reset", GPIOD_OUT_HIGH);
1353 if (IS_ERR(fs210x->gpio_sdz))
1354 return dev_err_probe(fs210x->dev, PTR_ERR(fs210x->gpio_sdz),
1355 "Failed to get reset-gpios\n");
1356
1357 for (i = 0; i < FS210X_NUM_SUPPLIES; i++)
1358 fs210x->supplies[i].supply = fs210x_supply_names[i];
1359
1360 ret = devm_regulator_bulk_get(fs210x->dev,
1361 ARRAY_SIZE(fs210x->supplies),
1362 fs210x->supplies);
1363 if (ret)
1364 return dev_err_probe(fs210x->dev, ret,
1365 "Failed to get supplies\n");
1366
1367 return 0;
1368 }
1369
fs210x_deinit(struct fs210x_priv * fs210x)1370 static void fs210x_deinit(struct fs210x_priv *fs210x)
1371 {
1372 gpiod_set_value_cansleep(fs210x->gpio_sdz, 1); /* Active */
1373 fsleep(10000); /* >= 10ms */
1374
1375 regulator_bulk_disable(FS210X_NUM_SUPPLIES, fs210x->supplies);
1376 }
1377
fs210x_init(struct fs210x_priv * fs210x)1378 static int fs210x_init(struct fs210x_priv *fs210x)
1379 {
1380 int ret;
1381
1382 ret = fs210x_parse_dts(fs210x, &fs210x->pdata);
1383 if (ret)
1384 return ret;
1385
1386 fs210x->clk_bclk = devm_clk_get_optional(fs210x->dev, "bclk");
1387 if (IS_ERR(fs210x->clk_bclk))
1388 return dev_err_probe(fs210x->dev, PTR_ERR(fs210x->clk_bclk),
1389 "Failed to get bclk\n");
1390
1391 ret = regulator_bulk_enable(FS210X_NUM_SUPPLIES, fs210x->supplies);
1392 if (ret)
1393 return dev_err_probe(fs210x->dev, ret,
1394 "Failed to enable supplies\n");
1395
1396 /* Make sure the SDZ pin is pulled down enough time. */
1397 fsleep(10000); /* >= 10ms */
1398 gpiod_set_value_cansleep(fs210x->gpio_sdz, 0); /* Deactivate */
1399 fsleep(10000); /* >= 10ms */
1400
1401 ret = fs210x_detect_device(fs210x);
1402 if (ret) {
1403 fs210x_deinit(fs210x);
1404 return ret;
1405 }
1406
1407 fs210x->scene_id = -1; /* Invalid scene */
1408 fs210x->cur_scene = NULL;
1409 fs210x->is_playing = false;
1410 fs210x->is_inited = false;
1411 fs210x->is_suspended = false;
1412 fs210x->check_interval_ms = FS210X_FAULT_CHECK_INTERVAL_MS;
1413
1414 INIT_DELAYED_WORK(&fs210x->fault_check_work, fs210x_fault_check_work);
1415 INIT_DELAYED_WORK(&fs210x->start_work, fs210x_start_work);
1416 mutex_init(&fs210x->lock);
1417
1418 return 0;
1419 }
1420
fs210x_register_snd_component(struct fs210x_priv * fs210x)1421 static int fs210x_register_snd_component(struct fs210x_priv *fs210x)
1422 {
1423 struct snd_soc_dai_driver *dai_drv;
1424 static int instance_id;
1425 int ret;
1426
1427 dai_drv = devm_kmemdup(fs210x->dev, &fs210x_dai,
1428 sizeof(fs210x_dai), GFP_KERNEL);
1429 if (!dai_drv)
1430 return -ENOMEM;
1431
1432 dai_drv->name = devm_kasprintf(fs210x->dev,
1433 GFP_KERNEL, "%s-%d",
1434 dai_drv->name, instance_id);
1435 if (!dai_drv->name)
1436 return -ENOMEM;
1437
1438 instance_id++;
1439
1440 if (fs210x->devid == FS2105S_DEVICE_ID) {
1441 dai_drv->playback.rates = FS2105S_RATES;
1442 dai_drv->capture.rates = FS2105S_RATES;
1443 }
1444
1445 ret = snd_soc_register_component(fs210x->dev,
1446 &fs210x_soc_component_dev,
1447 dai_drv, 1);
1448 return ret;
1449 }
1450
check_interval_ms_show(struct device * dev,struct device_attribute * devattr,char * buf)1451 static ssize_t check_interval_ms_show(struct device *dev,
1452 struct device_attribute *devattr,
1453 char *buf)
1454 {
1455 struct fs210x_priv *fs210x = dev_get_drvdata(dev);
1456
1457 return sysfs_emit(buf, "%d\n", fs210x->check_interval_ms);
1458 }
1459
check_interval_ms_store(struct device * dev,struct device_attribute * devattr,const char * buf,size_t count)1460 static ssize_t check_interval_ms_store(struct device *dev,
1461 struct device_attribute *devattr,
1462 const char *buf,
1463 size_t count)
1464 {
1465 struct fs210x_priv *fs210x = dev_get_drvdata(dev);
1466 int ret;
1467
1468 ret = kstrtouint(buf, 10, &fs210x->check_interval_ms);
1469 if (ret)
1470 return -EINVAL;
1471
1472 return (ssize_t)count;
1473 }
1474
1475 static DEVICE_ATTR_RW(check_interval_ms);
1476
1477 static struct attribute *fs210x_attrs[] = {
1478 &dev_attr_check_interval_ms.attr,
1479 NULL,
1480 };
1481
1482 static struct attribute_group fs210x_attr_group = {
1483 .attrs = fs210x_attrs,
1484 };
1485
fs210x_i2c_probe(struct i2c_client * client)1486 static int fs210x_i2c_probe(struct i2c_client *client)
1487 {
1488 struct fs210x_priv *fs210x;
1489 int ret;
1490
1491 fs210x = devm_kzalloc(&client->dev, sizeof(*fs210x), GFP_KERNEL);
1492 if (!fs210x)
1493 return -ENOMEM;
1494
1495 fs210x->i2c = client;
1496 fs210x->dev = &client->dev;
1497 i2c_set_clientdata(client, fs210x);
1498
1499 fs210x->regmap = devm_regmap_init_i2c(client, &fs210x_regmap);
1500 if (IS_ERR(fs210x->regmap))
1501 return dev_err_probe(fs210x->dev, PTR_ERR(fs210x->regmap),
1502 "Failed to get regmap\n");
1503
1504 ret = fs210x_init(fs210x);
1505 if (ret)
1506 return ret;
1507
1508 ret = devm_device_add_group(fs210x->dev, &fs210x_attr_group);
1509 if (ret) {
1510 fs210x_deinit(fs210x);
1511 return dev_err_probe(fs210x->dev, ret,
1512 "Failed to create sysfs group\n");
1513 }
1514
1515 ret = fs210x_register_snd_component(fs210x);
1516 if (ret) {
1517 fs210x_deinit(fs210x);
1518 return dev_err_probe(fs210x->dev, ret,
1519 "Failed to register component\n");
1520 }
1521
1522 return 0;
1523 }
1524
fs210x_i2c_remove(struct i2c_client * client)1525 static void fs210x_i2c_remove(struct i2c_client *client)
1526 {
1527 struct fs210x_priv *fs210x = i2c_get_clientdata(client);
1528
1529 snd_soc_unregister_component(fs210x->dev);
1530 fs210x_deinit(fs210x);
1531 }
1532
1533 static const struct i2c_device_id fs210x_i2c_id[] = {
1534 { .name = "fs2104" },
1535 { .name = "fs2105s" },
1536 { }
1537 };
1538 MODULE_DEVICE_TABLE(i2c, fs210x_i2c_id);
1539
1540 static const struct of_device_id fs210x_of_match[] = {
1541 { .compatible = "foursemi,fs2105s", },
1542 {},
1543 };
1544 MODULE_DEVICE_TABLE(of, fs210x_of_match);
1545
1546 static struct i2c_driver fs210x_i2c_driver = {
1547 .driver = {
1548 .name = "fs210x",
1549 .of_match_table = fs210x_of_match,
1550 },
1551 .id_table = fs210x_i2c_id,
1552 .probe = fs210x_i2c_probe,
1553 .remove = fs210x_i2c_remove,
1554 };
1555
1556 module_i2c_driver(fs210x_i2c_driver);
1557
1558 MODULE_AUTHOR("Nick Li <nick.li@foursemi.com>");
1559 MODULE_DESCRIPTION("FS2104/5S Audio Amplifier Driver");
1560 MODULE_LICENSE("GPL");
1561