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
|
/*
* es705-uart.c -- Audience eS705 UART interface
*
* Copyright 2013 Audience, Inc.
*
* Author: Matt Lupfer <mlupfer@cardinalpeak.com>
*
* Code Updates:
* Genisim Tsilker <gtsilker@audience.com>
* - Add optional UART VS FW download
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/firmware.h>
#include <linux/delay.h>
#include <linux/pm.h>
#include <linux/completion.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/err.h>
#include <linux/kthread.h>
#include <linux/esxxx.h>
#include <linux/serial_core.h>
#include <linux/tty.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include <sound/soc-dapm.h>
#include <sound/initval.h>
#include <sound/tlv.h>
#include "es705.h"
#include "es705-platform.h"
#include "es705-uart.h"
#include "es705-uart-common.h"
#include "es705-cdev.h"
#ifdef ES705_FW_LOAD_BUF_SZ
#undef ES705_FW_LOAD_BUF_SZ
#endif
#define ES705_FW_LOAD_BUF_SZ 1024
const u32 es705_uart_baud_rate[] = {
0x80190002, /* Default 460.8KBps */
0x80190002, /* baud rate 460.8Kbps, Clock 9.6Mhz */
0x80190202, /* baud rate 1Mbps, Clock 9.6Mhz */
0x80190702, /* baud rate 3Mbps, Clock 9.6Mhz */
};
static int es705_set_uart_baud_rate(struct es705_priv *es705)
{
int rc;
u32 uart_rate_request;
u32 resp;
const int match = 1;
dev_dbg(es705->dev, "%s(): UART baud rate request = 0x%08x\n",
__func__, es705_uart_baud_rate[es705->uart_fw_download_rate]);
resp = uart_rate_request =
cpu_to_be32(es705_uart_baud_rate[es705->uart_fw_download_rate]);
rc = es705_uart_write_then_read(es705, &uart_rate_request, 4, &resp,
match);
resp = be32_to_cpu(resp);
if (rc < 0)
dev_err(es705->dev,
"%s(): UART baud rate set for FW download FAIL\n",
__func__);
else
dev_dbg(es705->dev, "%s(): UART baud rate resp = 0x%08x\n",
__func__, resp);
return rc;
}
static int es705_uart_boot_setup(struct es705_priv *es705)
{
u8 sbl_sync_cmd = ES705_SBL_SYNC_CMD;
u8 sbl_boot_cmd = ES705_SBL_BOOT_CMD;
u32 rspn = sbl_sync_cmd;
const int match = 1;
int rc;
/* Detect BAUD RATE send SBL SYNC BYTE 0x00 */
dev_dbg(es705->dev, "%s(): write ES705_SBL_SYNC_CMD = 0x%02x\n",
__func__, sbl_sync_cmd);
rc = es705_uart_write_then_read(es705, &sbl_sync_cmd, 1, &rspn, match);
if (rc < 0) {
dev_err(es705->dev, "%s(): UART baud rate detection fail\n",
__func__);
goto es705_bootup_failed;
}
dev_dbg(es705->dev, "%s(): sbl sync ack = 0x%02x\n", __func__, rspn);
es705->uart_fw_download_rate = 3;
rc = es705_set_uart_baud_rate(es705);
if (rc < 0) {
dev_err(es705->dev, "%s(): uart baud rate set error\n",
__func__);
goto es705_bootup_failed;
}
es705_set_tty_baud_rate(es705->uart_fw_download_rate);
/* SBL SYNC BYTE 0x00 */
dev_dbg(es705->dev, "%s(): write ES705_SBL_SYNC_CMD = 0x%02x\n",
__func__, sbl_sync_cmd);
rc = es705_uart_write_then_read(es705, &sbl_sync_cmd, 1, &rspn, match);
if (rc < 0) {
dev_err(es705->dev, "%s(): UART FW Download SBL SYNC fail\n",
__func__);
goto es705_bootup_failed;
}
dev_dbg(es705->dev, "%s(): sbl sync ack = 0x%02x\n", __func__, rspn);
/* SBL BOOT BYTE 0x01 */
dev_dbg(es705->dev, "%s(): write ES705_SBL_BOOT_CMD = 0x%02x\n",
__func__, sbl_boot_cmd);
rspn = ES705_SBL_BOOT_ACK;
rc = es705_uart_write_then_read(es705, &sbl_boot_cmd, 1, &rspn, match);
if (rc < 0) {
dev_err(es705->dev, "%s(): UART FW Download BOOT CMD fail\n",
__func__);
goto es705_bootup_failed;
}
dev_dbg(es705->dev, "%s(): sbl boot ack = 0x%02x\n", __func__, rspn);
es705_bootup_failed:
return rc;
}
static int es705_uart_boot_finish(struct es705_priv *es705)
{
u32 sync_cmd;
u32 sync_resp;
char msg[4];
int rc, retry = 3;
/*
* Give the chip some time to become ready after firmware
* download. (FW is still transferring)
*/
msleep(50);
/*
* Read 4 bytes of VS FW download status
* TODO To avoid possible problem in the future
* if FW goes to send less or more then 4 bytes
* Need modify code to read UART until read
* buffer is empty
*/
rc = es705_uart_read(es705, msg, 4);
if (rc < 0)
dev_err(es705->dev, "%s(): UART read fail\n", __func__);
else
dev_dbg(es705->dev, "%s(): read byte = 0x%02x%02x%02x%02x\n",
__func__, msg[0], msg[1], msg[2], msg[3]);
if (es705->es705_power_state == ES705_SET_POWER_STATE_VS_OVERLAY) {
sync_cmd = (ES705_SYNC_CMD << 16) | ES705_SYNC_INTR_RISING_EDGE;
dev_info(es705->dev, "%s(): FW type : VOICESENSE\n", __func__);
} else {
sync_cmd = (ES705_SYNC_CMD << 16) | ES705_SYNC_POLLING;
dev_info(es705->dev, "%s(): FW type : STANDARD\n", __func__);
}
dev_dbg(es705->dev, "%s(): write ES705_SYNC_CMD = 0x%08x\n", __func__,
sync_cmd);
es705_boot_retry:
rc = es705_uart_cmd(es705, sync_cmd, 0, &sync_resp);
if (rc < 0) {
dev_err(es705->dev,
"%s(): firmware load failed (no sync response)\n",
__func__);
goto es705_boot_finish_failed;
}
if (sync_cmd != sync_resp) {
dev_err(es705->dev,
"%s(): firmware load failed, invalid sync response 0x%08x\n",
__func__, sync_resp);
if (--retry) {
dev_info(es705->dev, "%s(): buffer flush & retry\n",
__func__);
tty_perform_flush(es705->uart_dev.tty, TCIFLUSH);
goto es705_boot_retry;
}
rc = -EIO;
goto es705_boot_finish_failed;
}
dev_dbg(es705->dev, "%s(): firmware load success 0x%08x\n", __func__,
sync_resp);
es705_boot_finish_failed:
return rc;
}
int es705_uart_dev_rdb(struct es705_priv *es705, void *buf, int id)
{
u32 cmd;
u32 resp;
u8 *dptr;
int ret;
int size;
int rdcnt = 0;
dptr = (u8 *) buf;
/* Read voice sense keyword data block request. */
cmd = (ES705_RDB_CMD << 16) | (id & 0xFFFF);
cmd = cpu_to_be32(cmd);
ret = es705_uart_write(es705, (char *)&cmd, 4);
if (ret < 0) {
dev_err(es705->dev, "%s(): RDB cmd write err = %d\n", __func__,
ret);
goto rdb_err;
}
/* Refer to "ES705 Advanced API Guide" for details of interface */
usleep_range(10000, 11000);
ret = es705_uart_read(es705, (char *)&resp, 4);
if (ret < 0) {
dev_err(es705->dev, "%s(): error sending request = %d\n",
__func__, ret);
goto rdb_err;
}
be32_to_cpus(&resp);
size = resp & 0xffff;
dev_dbg(es705->dev, "%s(): resp=0x%08x size=%d\n", __func__, resp,
size);
if ((resp & 0xffff0000) != (ES705_RDB_CMD << 16)) {
dev_err(es705->dev,
"%s(): invalid read v-s data block response = 0x%08x\n",
__func__, resp);
goto rdb_err;
}
if (size == 0 || size % 4 != 0) {
dev_err(es705->dev, "%s(): read request return size of 0\n",
__func__);
goto rdb_err;
}
if (size > PARSE_BUFFER_SIZE)
size = PARSE_BUFFER_SIZE;
for (rdcnt = 0; rdcnt < size;) {
ret = es705_uart_read(es705, dptr, 4);
if (ret < 0) {
dev_err(es705->dev,
"%s(): data block ed error %d bytes ret = %d\n",
__func__, rdcnt, ret);
goto rdb_err;
}
rdcnt += 4;
dptr += 4;
}
es705->rdb_read_count = size;
dev_dbg(es705->dev, "%s(): rdcnt=%d\n", __func__, rdcnt);
ret = 0;
goto exit;
rdb_err:
es705->rdb_read_count = 0;
ret = -EIO;
exit:
return ret;
}
int es705_uart_dev_wdb(struct es705_priv *es705, const void *buf, int len)
{
/* At this point the command has been determined and is not part
* of the data in the buffer. Its just data. Note that we donot
* evaluate the contents of the data. It is up to the higher layers
* to insure the the codes mode of operation matchs what is being
* sent.
*/
int ret;
u32 resp;
u8 *dptr;
u32 cmd = ES705_WDB_CMD << 16;
dev_dbg(es705->dev, "%s(): len = 0x%08x\n", __func__, len);
dptr = (char *)buf;
cmd = cmd | (len & 0xFFFF);
dev_dbg(es705->dev, "%s(): cmd = 0x%08x\n", __func__, cmd);
cmd = cpu_to_be32(cmd);
ret = es705_uart_write_then_read(es705, &cmd, sizeof(cmd), &resp, 0);
if (ret < 0) {
dev_err(es705->dev, "%s(): cmd write err=%hu\n", __func__, ret);
goto wdb_err;
}
be32_to_cpus(&resp);
dev_dbg(es705->dev, "%s(): resp = 0x%08x\n", __func__, resp);
if ((resp & 0xffff0000) != (ES705_WDB_CMD << 16)) {
dev_err(es705->dev, "%s(): invalid write data block 0x%08x\n",
__func__, resp);
goto wdb_err;
}
/* The API requires that the subsequent writes are to be
* a byte stream (one byte per transport transaction)
*/
ret = es705_uart_write(es705, dptr, len);
if (ret < 0) {
dev_err(es705->dev, "%s(): wdb error =%d\n", __func__, ret);
goto wdb_err;
}
/* One last ACK read */
ret = es705_uart_read(es705, &resp, 4);
if (ret < 0) {
dev_err(es705->dev, "%s(): last ack %d\n", __func__, ret);
goto wdb_err;
}
if (resp & 0xff000000) {
dev_err(es705->dev, "%s(): write data block error 0x%0x\n",
__func__, resp);
goto wdb_err;
}
dev_dbg(es705->dev, "%s(): len = %d\n", __func__, len);
goto exit;
wdb_err:
len = -EIO;
exit:
return len;
}
int es705_uart_es705_wakeup(struct es705_priv *es705)
{
int rc;
char wakeup_char = 'A';
dev_info(es705_priv.dev, "%s(): ********* START UART wakeup\n",
__func__);
rc = es705_uart_open(es705);
if (rc) {
dev_err(es705->dev, "%s(): uart open error\n", __func__);
goto es705_uart_es705_wakeup_exit;
}
/* eS705 wakeup. Write wakeup character to UART */
rc = es705_uart_write(es705, &wakeup_char, sizeof(wakeup_char));
if (rc < 0)
dev_err(es705->dev, "%s(): wakeup via uart FAIL\n", __func__);
es705_uart_close(es705);
es705_uart_es705_wakeup_exit:
dev_info(es705_priv.dev, "%s(): ********* END UART wakeup\n", __func__);
return rc;
}
#define UART_DOWNLOAD_INITIAL_SYNC_BAUD_RATE_INDEX 1
int es705_uart_fw_download(struct es705_priv *es705, int fw_type)
{
int rc;
dev_info(es705_priv.dev, "%s(): ********* START VS FW Download\n",
__func__);
rc = es705_uart_open(es705);
if (rc) {
dev_err(es705->dev, "%s(): uart open error\n", __func__);
goto uart_open_error;
}
es705_set_tty_baud_rate(UART_DOWNLOAD_INITIAL_SYNC_BAUD_RATE_INDEX);
rc = es705_uart_boot_setup(es705);
if (rc < 0) {
dev_err(es705->dev, "%s(): uart boot setup error\n", __func__);
goto uart_download_error;
}
if (fw_type == VOICESENSE) {
rc = es705_uart_write(es705, (char *)es705->vs->data,
es705->vs->size);
} else {
rc = es705_uart_write(es705, (char *)es705->standard->data,
es705->standard->size);
}
if (rc < 0) {
dev_err(es705->dev, "%s(): uart %s image write fail\n",
__func__, fw_type == VOICESENSE ? "vs" : "standard");
rc = -EIO;
goto uart_download_error;
}
dev_dbg(es705->dev, "%s(): %s fw download done\n",
__func__, fw_type == VOICESENSE ? "vs" : "standard");
rc = es705_uart_boot_finish(es705);
if (rc < 0)
dev_err(es705->dev, "%s(): uart boot finish error\n", __func__);
uart_download_error:
es705_uart_close(es705);
uart_open_error:
dev_info(es705_priv.dev, "%s(): ********* END VS FW Download\n",
__func__);
return rc;
}
static int es705_uart_probe_thread(void *ptr)
{
int rc = 0;
struct device *dev = (struct device *)ptr;
rc = es705_uart_open(&es705_priv);
if (rc) {
dev_err(dev, "%s(): es705_uart_open() failed %d\n", __func__,
rc);
return rc;
}
/* set es705 function pointers */
es705_priv.dev_read = es705_uart_read;
es705_priv.dev_write = es705_uart_write;
es705_priv.cmd = es705_uart_cmd;
es705_priv.boot_setup = es705_uart_boot_setup;
es705_priv.boot_finish = es705_uart_boot_finish;
rc = es705_core_init(dev);
if (rc) {
dev_err(dev, "%s(): es705_core_init() failed %d\n", __func__,
rc);
goto bootup_error;
}
rc = es705_bootup(&es705_priv);
if (rc) {
dev_err(dev, "%s(): es705_bootup failed %d\n", __func__, rc);
goto bootup_error;
}
/* init es705 character device here, now that the UART is discovered */
rc = es705_init_cdev(&es705_priv);
if (rc) {
dev_err(dev, "%s(): failed to initialize char device = %d\n",
__func__, rc);
goto cdev_init_error;
}
return rc;
bootup_error:
/* close filp */
es705_uart_close(&es705_priv);
cdev_init_error:
dev_dbg(es705_priv.dev, "%s(): exit with error\n", __func__);
return rc;
}
static int es705_uart_probe(struct platform_device *dev)
{
int rc = 0;
struct task_struct *uart_probe_thread = NULL;
uart_probe_thread = kthread_run(es705_uart_probe_thread,
(void *)&dev->dev, "es705 uart thread");
if (IS_ERR_OR_NULL(uart_probe_thread)) {
dev_err(&dev->dev,
"%s(): can't create es705 UART probe thread = %p\n",
__func__, uart_probe_thread);
rc = -ENOMEM;
}
return rc;
}
static int es705_uart_remove(struct platform_device *dev)
{
int rc = 0;
if (es705_priv.uart_dev.file)
es705_uart_close(&es705_priv);
es705_priv.uart_dev.tty = NULL;
es705_priv.uart_dev.file = NULL;
snd_soc_unregister_codec(es705_priv.dev);
return rc;
}
struct platform_driver es705_uart_driver = {
.driver = {
.name = "es705-codec",
.owner = THIS_MODULE,
},
.probe = es705_uart_probe,
.remove = es705_uart_remove,
};
static struct esxxx_platform_data esxxx_platform_data = {
.irq_base = 0,
.reset_gpio = -1,
.gpiob_gpio = -1,
.esxxx_clk_cb = NULL,
};
struct platform_device es705_uart_device = {
.name = "es705-codec",
.resource = NULL,
.num_resources = 0,
.dev = {
.platform_data = &esxxx_platform_data,
}
};
/* FIXME: Kludge for es705_bus_init abstraction */
int es705_uart_bus_init(struct es705_priv *es705)
{
int rc;
rc = platform_driver_register(&es705_uart_driver);
if (rc)
return rc;
rc = platform_device_register(&es705_uart_device);
if (rc)
return rc;
dev_dbg(es705->dev, "%s(): registered as UART", __func__);
return rc;
}
MODULE_DESCRIPTION("ASoC ES705 driver");
MODULE_AUTHOR("Greg Clemson <gclemson@audience.com>");
MODULE_AUTHOR("Genisim Tsilker<gtsilker@audience.com>");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:es705-codec");
|