blob: a99ad5a2f9c8752315c800fbb8ad2a017b3f2604 [file] [log] [blame]
Banajit Goswamide8271c2017-01-18 00:28:59 -08001/* Copyright (c) 2012-2017, The Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12#include <linux/slab.h>
13#include <linux/mutex.h>
14#include <linux/mfd/wcd9xxx/wcd9xxx-slimslave.h>
15#include <linux/mfd/wcd9xxx/wcd9xxx_registers.h>
16
17struct wcd9xxx_slim_sch {
18 u16 rx_port_ch_reg_base;
19 u16 port_tx_cfg_reg_base;
20 u16 port_rx_cfg_reg_base;
21};
22
23static struct wcd9xxx_slim_sch sh_ch;
24
25static int wcd9xxx_alloc_slim_sh_ch(struct wcd9xxx *wcd9xxx,
26 u8 wcd9xxx_pgd_la, u32 cnt,
27 struct wcd9xxx_ch *channels, u32 path);
28
29static int wcd9xxx_dealloc_slim_sh_ch(struct slim_device *slim,
30 u32 cnt, struct wcd9xxx_ch *channels);
31
32static int wcd9xxx_configure_ports(struct wcd9xxx *wcd9xxx)
33{
34 if (wcd9xxx->codec_type->slim_slave_type ==
35 WCD9XXX_SLIM_SLAVE_ADDR_TYPE_0) {
36 sh_ch.rx_port_ch_reg_base = 0x180;
37 sh_ch.port_rx_cfg_reg_base = 0x040;
38 sh_ch.port_tx_cfg_reg_base = 0x040;
39 } else {
40 sh_ch.rx_port_ch_reg_base =
41 0x180 - (TAIKO_SB_PGD_OFFSET_OF_RX_SLAVE_DEV_PORTS * 4);
42 sh_ch.port_rx_cfg_reg_base =
43 0x040 - TAIKO_SB_PGD_OFFSET_OF_RX_SLAVE_DEV_PORTS;
44 sh_ch.port_tx_cfg_reg_base = 0x050;
45 }
46
47 return 0;
48}
49
Karthikeyan Manieea18362017-06-27 18:09:46 -070050/**
51 * wcd9xxx_init_slimslave
52 *
53 * @wcd9xxx: pointer to wcd9xxx struct
54 * @wcd9xxx_pgd_la: pgd_la value
55 * @tx_num: tx number
56 * @rx_num: rx number
57 * @tx_slot: pointer to tx slot
58 * @rx_slot: pointer to rx slot
59 *
60 * Returns 0 on success, appropriate error code otherwise
61 */
Banajit Goswamide8271c2017-01-18 00:28:59 -080062int wcd9xxx_init_slimslave(struct wcd9xxx *wcd9xxx, u8 wcd9xxx_pgd_la,
63 unsigned int tx_num, unsigned int *tx_slot,
64 unsigned int rx_num, unsigned int *rx_slot)
65{
66 int ret = 0;
67 int i;
68
69 ret = wcd9xxx_configure_ports(wcd9xxx);
70 if (ret) {
71 pr_err("%s: Failed to configure register address offset\n",
72 __func__);
73 goto err;
74 }
75
Xiaoyu Yeb849d532016-12-20 10:56:59 -080076 if (!rx_num || rx_num > wcd9xxx->num_rx_port) {
77 pr_err("%s: invalid rx num %d\n", __func__, rx_num);
78 return -EINVAL;
79 }
Banajit Goswamide8271c2017-01-18 00:28:59 -080080 if (wcd9xxx->rx_chs) {
81 wcd9xxx->num_rx_port = rx_num;
82 for (i = 0; i < rx_num; i++) {
83 wcd9xxx->rx_chs[i].ch_num = rx_slot[i];
84 INIT_LIST_HEAD(&wcd9xxx->rx_chs[i].list);
85 }
86 ret = wcd9xxx_alloc_slim_sh_ch(wcd9xxx, wcd9xxx_pgd_la,
87 wcd9xxx->num_rx_port,
88 wcd9xxx->rx_chs,
89 SLIM_SINK);
90 if (ret) {
91 pr_err("%s: Failed to alloc %d rx slimbus channels\n",
92 __func__, wcd9xxx->num_rx_port);
93 kfree(wcd9xxx->rx_chs);
94 wcd9xxx->rx_chs = NULL;
95 wcd9xxx->num_rx_port = 0;
96 }
97 } else {
98 pr_err("Not able to allocate memory for %d slimbus rx ports\n",
99 wcd9xxx->num_rx_port);
100 }
101
Xiaoyu Yeb849d532016-12-20 10:56:59 -0800102 if (!tx_num || tx_num > wcd9xxx->num_tx_port) {
103 pr_err("%s: invalid tx num %d\n", __func__, tx_num);
104 return -EINVAL;
105 }
Banajit Goswamide8271c2017-01-18 00:28:59 -0800106 if (wcd9xxx->tx_chs) {
107 wcd9xxx->num_tx_port = tx_num;
108 for (i = 0; i < tx_num; i++) {
109 wcd9xxx->tx_chs[i].ch_num = tx_slot[i];
110 INIT_LIST_HEAD(&wcd9xxx->tx_chs[i].list);
111 }
112 ret = wcd9xxx_alloc_slim_sh_ch(wcd9xxx, wcd9xxx_pgd_la,
113 wcd9xxx->num_tx_port,
114 wcd9xxx->tx_chs,
115 SLIM_SRC);
116 if (ret) {
117 pr_err("%s: Failed to alloc %d tx slimbus channels\n",
118 __func__, wcd9xxx->num_tx_port);
119 kfree(wcd9xxx->tx_chs);
120 wcd9xxx->tx_chs = NULL;
121 wcd9xxx->num_tx_port = 0;
122 }
123 } else {
124 pr_err("Not able to allocate memory for %d slimbus tx ports\n",
125 wcd9xxx->num_tx_port);
126 }
127 return 0;
128err:
129 return ret;
130}
Karthikeyan Manieea18362017-06-27 18:09:46 -0700131EXPORT_SYMBOL(wcd9xxx_init_slimslave);
Banajit Goswamide8271c2017-01-18 00:28:59 -0800132
133int wcd9xxx_deinit_slimslave(struct wcd9xxx *wcd9xxx)
134{
135 if (wcd9xxx->num_rx_port) {
136 wcd9xxx_dealloc_slim_sh_ch(wcd9xxx->slim,
137 wcd9xxx->num_rx_port,
138 wcd9xxx->rx_chs);
139 wcd9xxx->num_rx_port = 0;
140 }
141 if (wcd9xxx->num_tx_port) {
142 wcd9xxx_dealloc_slim_sh_ch(wcd9xxx->slim,
143 wcd9xxx->num_tx_port,
144 wcd9xxx->tx_chs);
145 wcd9xxx->num_tx_port = 0;
146 }
147 return 0;
148}
149
150
151static int wcd9xxx_alloc_slim_sh_ch(struct wcd9xxx *wcd9xxx,
152 u8 wcd9xxx_pgd_la, u32 cnt,
153 struct wcd9xxx_ch *channels, u32 path)
154{
155 int ret = 0;
156 u32 ch_idx;
157
158 /* The slimbus channel allocation seem take longer time
159 * so do the allocation up front to avoid delay in start of
160 * playback
161 */
162 pr_debug("%s: pgd_la[%d]\n", __func__, wcd9xxx_pgd_la);
163 for (ch_idx = 0; ch_idx < cnt; ch_idx++) {
164 ret = slim_get_slaveport(wcd9xxx_pgd_la,
165 channels[ch_idx].port,
166 &channels[ch_idx].sph, path);
167 pr_debug("%s: pgd_la[%d] channels[%d].port[%d]\n"
168 "channels[%d].sph[%d] path[%d]\n",
169 __func__, wcd9xxx_pgd_la, ch_idx,
170 channels[ch_idx].port,
171 ch_idx, channels[ch_idx].sph, path);
172 if (ret < 0) {
173 pr_err("%s: slave port failure id[%d] ret[%d]\n",
174 __func__, channels[ch_idx].ch_num, ret);
175 goto err;
176 }
177
178 ret = slim_query_ch(wcd9xxx->slim,
179 channels[ch_idx].ch_num,
180 &channels[ch_idx].ch_h);
181 if (ret < 0) {
182 pr_err("%s: slim_query_ch failed ch-num[%d] ret[%d]\n",
183 __func__, channels[ch_idx].ch_num, ret);
184 goto err;
185 }
186 }
187err:
188 return ret;
189}
190
191static int wcd9xxx_dealloc_slim_sh_ch(struct slim_device *slim,
192 u32 cnt, struct wcd9xxx_ch *channels)
193{
194 int idx = 0;
195 int ret = 0;
196 /* slim_dealloc_ch */
197 for (idx = 0; idx < cnt; idx++) {
198 ret = slim_dealloc_ch(slim, channels[idx].ch_h);
199 if (ret < 0) {
200 pr_err("%s: slim_dealloc_ch fail ret[%d] ch_h[%d]\n",
201 __func__, ret, channels[idx].ch_h);
202 }
203 }
204 return ret;
205}
206
207/* Enable slimbus slave device for RX path */
208int wcd9xxx_cfg_slim_sch_rx(struct wcd9xxx *wcd9xxx,
209 struct list_head *wcd9xxx_ch_list,
210 unsigned int rate, unsigned int bit_width,
211 u16 *grph)
212{
213 u8 ch_cnt = 0;
214 u16 ch_h[SLIM_MAX_RX_PORTS] = {0};
215 u8 payload = 0;
216 u16 codec_port = 0;
217 int ret;
218 struct slim_ch prop;
219 struct wcd9xxx_ch *rx;
220 int size = ARRAY_SIZE(ch_h);
221
222 /* Configure slave interface device */
223
224 list_for_each_entry(rx, wcd9xxx_ch_list, list) {
225 payload |= 1 << rx->shift;
226 if (ch_cnt < size) {
227 ch_h[ch_cnt] = rx->ch_h;
228 ch_cnt++;
229 pr_debug("list ch->ch_h %d ch->sph %d\n",
230 rx->ch_h, rx->sph);
231 } else {
232 pr_err("%s: allocated channel number %u is out of max rangae %d\n",
233 __func__, ch_cnt,
234 size);
235 ret = EINVAL;
236 goto err;
237 }
238 }
239 pr_debug("%s: ch_cnt[%d] rate=%d WATER_MARK_VAL %d\n",
240 __func__, ch_cnt, rate, WATER_MARK_VAL);
241 /* slim_define_ch api */
242 prop.prot = SLIM_AUTO_ISO;
243 if ((rate == 44100) || (rate == 88200) || (rate == 176400) ||
244 (rate == 352800)) {
245 prop.baser = SLIM_RATE_11025HZ;
246 prop.ratem = (rate/11025);
247 } else {
248 prop.baser = SLIM_RATE_4000HZ;
249 prop.ratem = (rate/4000);
250 }
251 prop.dataf = SLIM_CH_DATAF_NOT_DEFINED;
252 prop.auxf = SLIM_CH_AUXF_NOT_APPLICABLE;
253 prop.sampleszbits = bit_width;
254
255 pr_debug("Before slim_define_ch:\n"
256 "ch_cnt %d,ch_h[0] %d ch_h[1] %d, grph %d\n",
257 ch_cnt, ch_h[0], ch_h[1], *grph);
258 ret = slim_define_ch(wcd9xxx->slim, &prop, ch_h, ch_cnt,
259 true, grph);
260 if (ret < 0) {
261 pr_err("%s: slim_define_ch failed ret[%d]\n",
262 __func__, ret);
263 goto err;
264 }
265
266 list_for_each_entry(rx, wcd9xxx_ch_list, list) {
267 codec_port = rx->port;
268 pr_debug("%s: codec_port %d rx 0x%p, payload %d\n"
269 "sh_ch.rx_port_ch_reg_base0 0x%x\n"
270 "sh_ch.port_rx_cfg_reg_base 0x%x\n",
271 __func__, codec_port, rx, payload,
272 sh_ch.rx_port_ch_reg_base,
273 sh_ch.port_rx_cfg_reg_base);
274
275 /* look for the valid port range and chose the
276 * payload accordingly
277 */
278 /* write to interface device */
279 ret = wcd9xxx_interface_reg_write(wcd9xxx,
280 SB_PGD_RX_PORT_MULTI_CHANNEL_0(
281 sh_ch.rx_port_ch_reg_base, codec_port),
282 payload);
283
284 if (ret < 0) {
285 pr_err("%s:Intf-dev fail reg[%d] payload[%d] ret[%d]\n",
286 __func__,
287 SB_PGD_RX_PORT_MULTI_CHANNEL_0(
288 sh_ch.rx_port_ch_reg_base, codec_port),
289 payload, ret);
290 goto err;
291 }
292 /* configure the slave port for water mark and enable*/
293 ret = wcd9xxx_interface_reg_write(wcd9xxx,
294 SB_PGD_PORT_CFG_BYTE_ADDR(
295 sh_ch.port_rx_cfg_reg_base, codec_port),
296 WATER_MARK_VAL);
297 if (ret < 0) {
298 pr_err("%s:watermark set failure for port[%d] ret[%d]",
299 __func__, codec_port, ret);
300 }
301
302 ret = slim_connect_sink(wcd9xxx->slim, &rx->sph, 1, rx->ch_h);
303 if (ret < 0) {
304 pr_err("%s: slim_connect_sink failed ret[%d]\n",
305 __func__, ret);
306 goto err_close_slim_sch;
307 }
308 }
309 /* slim_control_ch */
310 ret = slim_control_ch(wcd9xxx->slim, *grph, SLIM_CH_ACTIVATE,
311 true);
312 if (ret < 0) {
313 pr_err("%s: slim_control_ch failed ret[%d]\n",
314 __func__, ret);
315 goto err_close_slim_sch;
316 }
317 return 0;
318
319err_close_slim_sch:
320 /* release all acquired handles */
321 wcd9xxx_close_slim_sch_rx(wcd9xxx, wcd9xxx_ch_list, *grph);
322err:
323 return ret;
324}
325EXPORT_SYMBOL(wcd9xxx_cfg_slim_sch_rx);
326
327/* Enable slimbus slave device for RX path */
328int wcd9xxx_cfg_slim_sch_tx(struct wcd9xxx *wcd9xxx,
329 struct list_head *wcd9xxx_ch_list,
330 unsigned int rate, unsigned int bit_width,
331 u16 *grph)
332{
333 u16 ch_cnt = 0;
334 u16 payload = 0;
335 u16 ch_h[SLIM_MAX_TX_PORTS] = {0};
336 u16 codec_port;
337 int ret = 0;
338 struct wcd9xxx_ch *tx;
339 int size = ARRAY_SIZE(ch_h);
340
341 struct slim_ch prop;
342
343 list_for_each_entry(tx, wcd9xxx_ch_list, list) {
344 payload |= 1 << tx->shift;
345 if (ch_cnt < size) {
346 ch_h[ch_cnt] = tx->ch_h;
347 ch_cnt++;
348 } else {
349 pr_err("%s: allocated channel number %u is out of max rangae %d\n",
350 __func__, ch_cnt,
351 size);
352 ret = EINVAL;
353 goto err;
354 }
355 }
356
357 /* slim_define_ch api */
358 prop.prot = SLIM_AUTO_ISO;
359 prop.baser = SLIM_RATE_4000HZ;
360 prop.dataf = SLIM_CH_DATAF_NOT_DEFINED;
361 prop.auxf = SLIM_CH_AUXF_NOT_APPLICABLE;
362 prop.ratem = (rate/4000);
363 prop.sampleszbits = bit_width;
364 ret = slim_define_ch(wcd9xxx->slim, &prop, ch_h, ch_cnt,
365 true, grph);
366 if (ret < 0) {
367 pr_err("%s: slim_define_ch failed ret[%d]\n",
368 __func__, ret);
369 goto err;
370 }
371
372 pr_debug("%s: ch_cnt[%d] rate[%d] bitwidth[%u]\n", __func__, ch_cnt,
373 rate, bit_width);
374 list_for_each_entry(tx, wcd9xxx_ch_list, list) {
375 codec_port = tx->port;
376 pr_debug("%s: codec_port %d tx 0x%p, payload 0x%x\n",
377 __func__, codec_port, tx, payload);
378 /* write to interface device */
379 ret = wcd9xxx_interface_reg_write(wcd9xxx,
380 SB_PGD_TX_PORT_MULTI_CHANNEL_0(codec_port),
381 payload & 0x00FF);
382 if (ret < 0) {
383 pr_err("%s:Intf-dev fail reg[%d] payload[%d] ret[%d]\n",
384 __func__,
385 SB_PGD_TX_PORT_MULTI_CHANNEL_0(codec_port),
386 payload, ret);
387 goto err;
388 }
389 /* ports 8,9 */
390 ret = wcd9xxx_interface_reg_write(wcd9xxx,
391 SB_PGD_TX_PORT_MULTI_CHANNEL_1(codec_port),
392 (payload & 0xFF00)>>8);
393 if (ret < 0) {
394 pr_err("%s:Intf-dev fail reg[%d] payload[%d] ret[%d]\n",
395 __func__,
396 SB_PGD_TX_PORT_MULTI_CHANNEL_1(codec_port),
397 payload, ret);
398 goto err;
399 }
400 /* configure the slave port for water mark and enable*/
401 ret = wcd9xxx_interface_reg_write(wcd9xxx,
402 SB_PGD_PORT_CFG_BYTE_ADDR(
403 sh_ch.port_tx_cfg_reg_base, codec_port),
404 WATER_MARK_VAL);
405 if (ret < 0) {
406 pr_err("%s:watermark set failure for port[%d] ret[%d]",
407 __func__, codec_port, ret);
408 }
409
410 ret = slim_connect_src(wcd9xxx->slim, tx->sph, tx->ch_h);
411
412 if (ret < 0) {
413 pr_err("%s: slim_connect_src failed ret[%d]\n",
414 __func__, ret);
415 goto err;
416 }
417 }
418 /* slim_control_ch */
419 ret = slim_control_ch(wcd9xxx->slim, *grph, SLIM_CH_ACTIVATE,
420 true);
421 if (ret < 0) {
422 pr_err("%s: slim_control_ch failed ret[%d]\n",
423 __func__, ret);
424 goto err;
425 }
426 return 0;
427err:
428 /* release all acquired handles */
429 wcd9xxx_close_slim_sch_tx(wcd9xxx, wcd9xxx_ch_list, *grph);
430 return ret;
431}
432EXPORT_SYMBOL(wcd9xxx_cfg_slim_sch_tx);
433
434int wcd9xxx_close_slim_sch_rx(struct wcd9xxx *wcd9xxx,
435 struct list_head *wcd9xxx_ch_list, u16 grph)
436{
437 u32 sph[SLIM_MAX_RX_PORTS] = {0};
438 int ch_cnt = 0;
439 int ret = 0;
440 struct wcd9xxx_ch *rx;
441
442 list_for_each_entry(rx, wcd9xxx_ch_list, list)
443 sph[ch_cnt++] = rx->sph;
444
445 pr_debug("%s ch_cht %d, sph[0] %d sph[1] %d\n", __func__, ch_cnt,
446 sph[0], sph[1]);
447
448 /* slim_control_ch (REMOVE) */
449 pr_debug("%s before slim_control_ch grph %d\n", __func__, grph);
450 ret = slim_control_ch(wcd9xxx->slim, grph, SLIM_CH_REMOVE, true);
451 if (ret < 0) {
452 pr_err("%s: slim_control_ch failed ret[%d]\n", __func__, ret);
453 goto err;
454 }
455err:
456 return ret;
457}
458EXPORT_SYMBOL(wcd9xxx_close_slim_sch_rx);
459
460int wcd9xxx_close_slim_sch_tx(struct wcd9xxx *wcd9xxx,
461 struct list_head *wcd9xxx_ch_list,
462 u16 grph)
463{
464 u32 sph[SLIM_MAX_TX_PORTS] = {0};
465 int ret = 0;
466 int ch_cnt = 0;
467 struct wcd9xxx_ch *tx;
468
469 pr_debug("%s\n", __func__);
470 list_for_each_entry(tx, wcd9xxx_ch_list, list)
471 sph[ch_cnt++] = tx->sph;
472
473 pr_debug("%s ch_cht %d, sph[0] %d sph[1] %d\n",
474 __func__, ch_cnt, sph[0], sph[1]);
475 /* slim_control_ch (REMOVE) */
476 ret = slim_control_ch(wcd9xxx->slim, grph, SLIM_CH_REMOVE, true);
477 if (ret < 0) {
478 pr_err("%s: slim_control_ch failed ret[%d]\n",
479 __func__, ret);
480 goto err;
481 }
482err:
483 return ret;
484}
485EXPORT_SYMBOL(wcd9xxx_close_slim_sch_tx);
486
487int wcd9xxx_get_slave_port(unsigned int ch_num)
488{
489 int ret = 0;
490
491 ret = (ch_num - BASE_CH_NUM);
492 pr_debug("%s: ch_num[%d] slave port[%d]\n", __func__, ch_num, ret);
493 if (ret < 0) {
494 pr_err("%s: Error:- Invalid slave port found = %d\n",
495 __func__, ret);
496 return -EINVAL;
497 }
498 return ret;
499}
500EXPORT_SYMBOL(wcd9xxx_get_slave_port);
501
502int wcd9xxx_disconnect_port(struct wcd9xxx *wcd9xxx,
503 struct list_head *wcd9xxx_ch_list, u16 grph)
504{
505 u32 sph[SLIM_MAX_TX_PORTS + SLIM_MAX_RX_PORTS] = {0};
506 int ch_cnt = 0;
507 int ret = 0;
508 struct wcd9xxx_ch *slim_ch;
509
510 list_for_each_entry(slim_ch, wcd9xxx_ch_list, list)
511 sph[ch_cnt++] = slim_ch->sph;
512
513 /* slim_disconnect_port */
514 ret = slim_disconnect_ports(wcd9xxx->slim, sph, ch_cnt);
515 if (ret < 0) {
516 pr_err("%s: slim_disconnect_ports failed ret[%d]\n",
517 __func__, ret);
518 }
519 return ret;
520}
521EXPORT_SYMBOL(wcd9xxx_disconnect_port);
522
523/* This function is called with mutex acquired */
524int wcd9xxx_rx_vport_validation(u32 port_id,
525 struct list_head *codec_dai_list)
526{
527 struct wcd9xxx_ch *ch;
528 int ret = 0;
529
530 pr_debug("%s: port_id %u\n", __func__, port_id);
531
532 list_for_each_entry(ch,
533 codec_dai_list, list) {
534 pr_debug("%s: ch->port %u\n", __func__, ch->port);
535 if (ch->port == port_id) {
536 ret = -EINVAL;
537 break;
538 }
539 }
540 return ret;
541}
542EXPORT_SYMBOL(wcd9xxx_rx_vport_validation);
543
544
545/* This function is called with mutex acquired */
546int wcd9xxx_tx_vport_validation(u32 table, u32 port_id,
547 struct wcd9xxx_codec_dai_data *codec_dai,
548 u32 num_codec_dais)
549{
550 struct wcd9xxx_ch *ch;
551 int ret = 0;
552 u32 index;
553 unsigned long vtable = table;
554 u32 size = sizeof(table) * BITS_PER_BYTE;
555
556 pr_debug("%s: vtable 0x%lx port_id %u size %d\n", __func__,
557 vtable, port_id, size);
558 for_each_set_bit(index, &vtable, size) {
559 if (index < num_codec_dais) {
560 list_for_each_entry(ch,
561 &codec_dai[index].wcd9xxx_ch_list,
562 list) {
563 pr_debug("%s: index %u ch->port %u vtable 0x%lx\n",
564 __func__, index, ch->port,
565 vtable);
566 if (ch->port == port_id) {
567 pr_err("%s: TX%u is used by AIF%u_CAP Mixer\n",
568 __func__, port_id + 1,
569 (index + 1)/2);
570 ret = -EINVAL;
571 break;
572 }
573 }
574 } else {
575 pr_err("%s: Invalid index %d of codec dai",
576 __func__, index);
577 ret = -EINVAL;
578 }
579 if (ret)
580 break;
581 }
582 return ret;
583}
584EXPORT_SYMBOL(wcd9xxx_tx_vport_validation);