1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
|
/*
* DSPG DBMD4/DBMD6 SPI interface driver
*
* Copyright (C) 2014 DSP Group
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
/* #define DEBUG */
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/spi/spi.h>
#include <linux/of_gpio.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/clk.h>
#include <linux/of.h>
#include <linux/firmware.h>
#include "dbmdx-interface.h"
#include "dbmdx-va-regmap.h"
#include "dbmdx-vqe-regmap.h"
#include "dbmdx-spi.h"
#define DBMD4_MAX_SPI_BOOT_SPEED 4000000
static const u8 clr_crc_cmd[] = {0x5A, 0x0F};
static const u8 chng_pll_cmd[] = {0x5A, 0x10, 0, 0xEC, 0x0B, 00};
static int dbmd4_spi_boot(const void *fw_data, size_t fw_size,
struct dbmdx_private *p, const void *checksum,
size_t chksum_len, int load_fw)
{
int retry = RETRY_COUNT;
int ret = 0;
ssize_t send_bytes;
struct dbmdx_spi_private *spi_p =
(struct dbmdx_spi_private *)p->chip->pdata;
struct spi_device *spi = spi_p->client;
dev_dbg(spi_p->dev, "%s\n", __func__);
while (retry--) {
if (p->active_fw == DBMDX_FW_PRE_BOOT) {
/* reset DBMD4 chip */
p->reset_sequence(p);
/* delay before sending commands */
if (p->clk_get_rate(p, DBMDX_CLK_MASTER) <= 32768)
msleep(DBMDX_MSLEEP_SPI_D4_AFTER_RESET_32K);
else
usleep_range(DBMDX_USLEEP_SPI_D4_AFTER_RESET,
DBMDX_USLEEP_SPI_D4_AFTER_RESET + 5000);
ret = spi_set_speed(p, DBMDX_VA_SPEED_MAX);
if (ret < 0) {
dev_err(spi_p->dev, "%s:failed %x\n",
__func__, ret);
continue;
}
if ((spi->max_speed_hz > DBMD4_MAX_SPI_BOOT_SPEED) &&
(p->pdata->firmware_id == DBMDX_FIRMWARE_ID_DBMD4)) {
ret = spi_set_speed(p, DBMDX_VA_SPEED_NORMAL);
if (ret < 0) {
dev_err(spi_p->dev, "%s:failed %x\n",
__func__, ret);
continue;
}
/* send CRC clear command */
ret = send_spi_data(p, chng_pll_cmd,
sizeof(chng_pll_cmd));
if (ret != sizeof(chng_pll_cmd)) {
dev_err(p->dev,
"%s: failed to clear CRC\n",
__func__);
continue;
}
msleep(DBMDX_MSLEEP_SPI_D4_AFTER_PLL_CHANGE);
ret = spi_set_speed(p, DBMDX_VA_SPEED_MAX);
if (ret < 0) {
dev_err(spi_p->dev, "%s:failed %x\n",
__func__, ret);
continue;
}
}
/* send CRC clear command */
ret = send_spi_data(p, clr_crc_cmd,
sizeof(clr_crc_cmd));
if (ret != sizeof(clr_crc_cmd)) {
dev_err(p->dev, "%s: failed to clear CRC\n",
__func__);
continue;
}
} else {
ret = send_spi_cmd_va(p,
DBMDX_VA_SWITCH_TO_BOOT,
NULL);
if (ret < 0) {
dev_err(p->dev,
"%s: failed to send 'Switch to BOOT' cmd\n",
__func__);
continue;
}
}
if (!load_fw)
break;
/* send firmware */
send_bytes = send_spi_data(p, fw_data, fw_size - 4);
if (send_bytes != fw_size - 4) {
dev_err(p->dev,
"%s: -----------> load firmware error\n",
__func__);
continue;
}
/* verify checksum */
if (checksum) {
ret = spi_verify_boot_checksum(p, checksum, chksum_len);
if (ret < 0) {
dev_err(spi_p->dev,
"%s: could not verify checksum\n",
__func__);
continue;
}
}
dev_info(p->dev, "%s: ---------> firmware loaded\n",
__func__);
break;
}
/* no retries left, failed to boot */
if (retry < 0) {
dev_err(p->dev, "%s: failed to load firmware\n", __func__);
return -1;
}
/* send boot command */
ret = send_spi_cmd_boot(p, DBMDX_FIRMWARE_BOOT);
if (ret < 0) {
dev_err(p->dev, "%s: booting the firmware failed\n", __func__);
return -1;
}
ret = spi_set_speed(p, DBMDX_VA_SPEED_NORMAL);
if (ret < 0)
dev_err(spi_p->dev, "%s:failed %x\n", __func__, ret);
/* wait some time */
usleep_range(DBMDX_USLEEP_SPI_D4_AFTER_BOOT,
DBMDX_USLEEP_SPI_D4_AFTER_BOOT + 1000);
return ret;
}
static int dbmd4_spi_reset_post_pll_divider(struct dbmdx_private *p)
{
struct dbmdx_spi_private *spi_p =
(struct dbmdx_spi_private *)p->chip->pdata;
int ret;
dev_dbg(spi_p->dev, "%s\n", __func__);
ret = send_spi_cmd_va(p,
DBMDX_VA_GENERAL_CONFIGURATION_2,
&spi_p->post_pll_div);
if (ret < 0) {
dev_err(spi_p->dev,
"%s: failed to get post pll divider\n",
__func__);
return ret;
}
ret = send_spi_cmd_va(p,
DBMDX_VA_GENERAL_CONFIGURATION_2 |
(spi_p->post_pll_div & ~DBMDX_POST_PLL_DIV_MASK),
NULL);
if (ret < 0) {
dev_err(spi_p->dev,
"%s: failed to get post pll divider\n",
__func__);
return ret;
}
usleep_range(DBMDX_USLEEP_SPI_D4_POST_PLL,
DBMDX_USLEEP_SPI_D4_POST_PLL + 1000);
return 0;
}
static int dbmd4_spi_restore_post_pll_divider(struct dbmdx_private *p)
{
struct dbmdx_spi_private *spi_p =
(struct dbmdx_spi_private *)p->chip->pdata;
int ret;
dev_dbg(spi_p->dev, "%s\n", __func__);
ret = send_spi_cmd_va(p,
DBMDX_VA_GENERAL_CONFIGURATION_2 |
spi_p->post_pll_div,
NULL);
if (ret < 0) {
dev_err(spi_p->dev,
"%s: failed to restore post pll divider\n",
__func__);
return ret;
}
usleep_range(DBMDX_USLEEP_SPI_D4_POST_PLL,
DBMDX_USLEEP_SPI_D4_POST_PLL + 1000);
return 0;
}
static int dbmd4_reconfigure_dsp_clock(struct dbmdx_private *p, int index)
{
struct dbmdx_spi_private *spi_p =
(struct dbmdx_spi_private *)p->chip->pdata;
int ret;
u32 dsp_clock_cfg;
u16 mic1_val;
u16 mic2_val;
dev_dbg(spi_p->dev, "%s\n", __func__);
dsp_clock_cfg = p->pdata->va_speed_cfg[index].cfg;
if (!dsp_clock_cfg) {
dev_dbg(spi_p->dev,
"%s: dsp clock cfg is not set for rate #%d\n",
__func__, index);
return 0;
}
ret = send_spi_cmd_va(p,
DBMDX_VA_MICROPHONE1_CONFIGURATION,
&mic1_val);
if (ret < 0) {
dev_err(spi_p->dev,
"%s: failed to get mic1 value\n",
__func__);
return ret;
}
ret = send_spi_cmd_va(p,
DBMDX_VA_MICROPHONE2_CONFIGURATION,
&mic2_val);
if (ret < 0) {
dev_err(spi_p->dev,
"%s: failed to get mic2 value\n",
__func__);
return ret;
}
ret = send_spi_cmd_va(p,
DBMDX_VA_MICROPHONE1_CONFIGURATION,
NULL);
if (ret < 0) {
dev_err(spi_p->dev,
"%s: failed to reset mic1 value\n",
__func__);
return ret;
}
ret = send_spi_cmd_va(p,
DBMDX_VA_MICROPHONE2_CONFIGURATION,
NULL);
if (ret < 0) {
dev_err(spi_p->dev,
"%s: failed to reset mic2 value\n",
__func__);
return ret;
}
ret = send_spi_cmd_va(p,
DBMDX_VA_CLK_CFG | (dsp_clock_cfg & 0xffff),
NULL);
if (ret < 0) {
dev_err(spi_p->dev,
"%s: failed to set DBMDX_VA_CLK_CFG\n",
__func__);
return ret;
}
/* Give to PLL enough time for stabilization */
msleep(DBMDX_MSLEEP_CONFIG_VA_MODE_REG);
ret = send_spi_cmd_va(p,
DBMDX_VA_MICROPHONE1_CONFIGURATION | mic1_val,
NULL);
if (ret < 0) {
dev_err(spi_p->dev,
"%s: failed to restore mic1 value\n",
__func__);
return ret;
}
ret = send_spi_cmd_va(p,
DBMDX_VA_MICROPHONE2_CONFIGURATION | mic2_val,
NULL);
if (ret < 0) {
dev_err(spi_p->dev,
"%s: failed to restore mic2 value\n",
__func__);
return ret;
}
return 0;
}
static int dbmd4_spi_switch_to_buffering_speed(struct dbmdx_private *p,
bool reconfigure_dsp_clock)
{
struct dbmdx_spi_private *spi_p =
(struct dbmdx_spi_private *)p->chip->pdata;
int ret;
dev_dbg(spi_p->dev, "%s\n", __func__);
ret = dbmd4_spi_reset_post_pll_divider(p);
if (ret < 0) {
dev_err(spi_p->dev,
"%s: failed, cannot reset post pll divider\n",
__func__);
return ret;
}
if (reconfigure_dsp_clock) {
ret = dbmd4_reconfigure_dsp_clock(p, DBMDX_VA_SPEED_BUFFERING);
if (ret < 0) {
dev_err(spi_p->dev,
"%s: failed to reconfigure dsp clock\n",
__func__);
return ret;
}
}
ret = spi_set_speed(p, DBMDX_VA_SPEED_BUFFERING);
if (ret < 0) {
dev_err(spi_p->dev, "%s:failed setting speed %x\n",
__func__, ret);
return ret;
}
return 0;
}
static int dbmd4_spi_switch_to_normal_speed(struct dbmdx_private *p,
bool reconfigure_dsp_clock)
{
struct dbmdx_spi_private *spi_p =
(struct dbmdx_spi_private *)p->chip->pdata;
int ret;
dev_dbg(spi_p->dev, "%s\n", __func__);
ret = spi_set_speed(p, DBMDX_VA_SPEED_NORMAL);
if (ret < 0) {
dev_err(spi_p->dev, "%s:failed setting speed %x\n",
__func__, ret);
return ret;
}
ret = dbmd4_spi_restore_post_pll_divider(p);
if (ret < 0) {
dev_err(p->dev,
"%s: failed, cannot restore post pll divider\n",
__func__);
return ret;
}
if (reconfigure_dsp_clock) {
ret = dbmd4_reconfigure_dsp_clock(p, DBMDX_VA_SPEED_NORMAL);
if (ret < 0) {
dev_err(spi_p->dev,
"%s: failed to reconfigure dsp clock\n",
__func__);
return ret;
}
}
return 0;
}
static int dbmd4_spi_prepare_buffering(struct dbmdx_private *p)
{
struct dbmdx_spi_private *spi_p =
(struct dbmdx_spi_private *)p->chip->pdata;
int ret;
dev_dbg(spi_p->dev, "%s\n", __func__);
ret = dbmd4_spi_switch_to_buffering_speed(p, false);
if (ret < 0) {
dev_err(p->dev,
"%s: failed to change speed to buffering\n",
__func__);
goto out;
}
out:
return ret;
}
static int dbmd4_spi_finish_buffering(struct dbmdx_private *p)
{
struct dbmdx_spi_private *spi_p =
(struct dbmdx_spi_private *)p->chip->pdata;
int ret;
dev_dbg(spi_p->dev, "%s\n", __func__);
ret = dbmd4_spi_switch_to_normal_speed(p, false);
if (ret < 0) {
dev_err(p->dev,
"%s: failed to change speed to buffering\n",
__func__);
goto out;
}
out:
return ret;
}
static int dbmd4_spi_prepare_amodel_loading(struct dbmdx_private *p)
{
struct dbmdx_spi_private *spi_p =
(struct dbmdx_spi_private *)p->chip->pdata;
int ret;
dev_dbg(spi_p->dev, "%s\n", __func__);
ret = dbmd4_spi_switch_to_buffering_speed(p, true);
if (ret < 0) {
dev_err(p->dev,
"%s: failed to change speed to buffering\n",
__func__);
goto out;
}
out:
return ret;
}
static int dbmd4_spi_finish_amodel_loading(struct dbmdx_private *p)
{
struct dbmdx_spi_private *spi_p =
(struct dbmdx_spi_private *)p->chip->pdata;
int ret;
dev_dbg(spi_p->dev, "%s\n", __func__);
ret = dbmd4_spi_switch_to_normal_speed(p, true);
if (ret < 0) {
dev_err(p->dev,
"%s: failed to change speed to buffering\n",
__func__);
goto out;
}
out:
return ret;
}
#ifdef CONFIG_PM_SLEEP
static int dbmdx_spi_suspend(struct device *dev)
{
struct chip_interface *ci = spi_get_drvdata(to_spi_device(dev));
struct dbmdx_spi_private *spi_p = (struct dbmdx_spi_private *)ci->pdata;
dev_dbg(dev, "%s\n", __func__);
spi_interface_suspend(spi_p);
return 0;
}
static int dbmdx_spi_resume(struct device *dev)
{
struct chip_interface *ci = spi_get_drvdata(to_spi_device(dev));
struct dbmdx_spi_private *spi_p = (struct dbmdx_spi_private *)ci->pdata;
dev_dbg(dev, "%s\n", __func__);
spi_interface_resume(spi_p);
return 0;
}
#endif /* CONFIG_PM_SLEEP */
#ifdef CONFIG_PM_RUNTIME
static int dbmdx_spi_runtime_suspend(struct device *dev)
{
struct chip_interface *ci = spi_get_drvdata(to_spi_device(dev));
struct dbmdx_spi_private *spi_p = (struct dbmdx_spi_private *)ci->pdata;
dev_dbg(dev, "%s\n", __func__);
spi_interface_suspend(spi_p);
return 0;
}
static int dbmdx_spi_runtime_resume(struct device *dev)
{
struct chip_interface *ci = spi_get_drvdata(to_spi_device(dev));
struct dbmdx_spi_private *spi_p = (struct dbmdx_spi_private *)ci->pdata;
dev_dbg(dev, "%s\n", __func__);
spi_interface_resume(spi_p);
return 0;
}
#endif /* CONFIG_PM_RUNTIME */
static const struct dev_pm_ops dbmdx_spi_pm = {
SET_SYSTEM_SLEEP_PM_OPS(dbmdx_spi_suspend, dbmdx_spi_resume)
SET_RUNTIME_PM_OPS(dbmdx_spi_runtime_suspend,
dbmdx_spi_runtime_resume, NULL)
};
static int dbmd4_spi_probe(struct spi_device *client)
{
int rc;
struct dbmdx_spi_private *p;
struct chip_interface *ci;
rc = spi_common_probe(client);
if (rc < 0)
return rc;
ci = spi_get_drvdata(client);
p = (struct dbmdx_spi_private *)ci->pdata;
/* fill in chip interface functions */
p->chip.prepare_amodel_loading = dbmd4_spi_prepare_amodel_loading;
p->chip.finish_amodel_loading = dbmd4_spi_finish_amodel_loading;
p->chip.prepare_buffering = dbmd4_spi_prepare_buffering;
p->chip.finish_buffering = dbmd4_spi_finish_buffering;
p->chip.boot = dbmd4_spi_boot;
return rc;
}
static const struct of_device_id dbmd_4_6_spi_of_match[] = {
{ .compatible = "dspg,dbmd4-spi", },
{ .compatible = "dspg,dbmd6-spi", },
{},
};
MODULE_DEVICE_TABLE(of, dbmd_4_6_spi_of_match);
static const struct spi_device_id dbmd_4_6_spi_id[] = {
{ "dbmdx-spi", 0 },
{ "dbmd4-spi", 0 },
{ "dbmd6-spi", 0 },
{ }
};
MODULE_DEVICE_TABLE(spi, dbmd_4_6_spi_id);
static struct spi_driver dbmd_4_6_spi_driver = {
.driver = {
.name = "dbmd_4_6-spi",
.bus = &spi_bus_type,
.owner = THIS_MODULE,
.of_match_table = dbmd_4_6_spi_of_match,
.pm = &dbmdx_spi_pm,
},
.probe = dbmd4_spi_probe,
.remove = spi_common_remove,
.id_table = dbmd_4_6_spi_id,
};
static int __init dbmd_4_6_modinit(void)
{
return spi_register_driver(&dbmd_4_6_spi_driver);
}
module_init(dbmd_4_6_modinit);
static void __exit dbmd_4_6_exit(void)
{
spi_unregister_driver(&dbmd_4_6_spi_driver);
}
module_exit(dbmd_4_6_exit);
MODULE_DESCRIPTION("DSPG DBMD4/DBMD6 spi interface driver");
MODULE_LICENSE("GPL");
|