1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2011-2017, The Linux Foundation. All rights reserved.
3 // Copyright (c) 2018, Linaro Limited
6 #include <linux/kernel.h>
7 #include <linux/init.h>
8 #include <linux/slab.h>
9 #include <linux/interrupt.h>
10 #include <linux/platform_device.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/dmaengine.h>
13 #include <linux/slimbus.h>
14 #include <linux/delay.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/mutex.h>
17 #include <linux/notifier.h>
18 #include <linux/remoteproc/qcom_rproc.h>
21 #include <linux/soc/qcom/qmi.h>
22 #include <linux/soc/qcom/pdr.h>
26 /* NGD (Non-ported Generic Device) registers */
28 #define NGD_CFG_ENABLE BIT(0)
29 #define NGD_CFG_RX_MSGQ_EN BIT(1)
30 #define NGD_CFG_TX_MSGQ_EN BIT(2)
31 #define NGD_STATUS 0x4
32 #define NGD_LADDR BIT(1)
33 #define NGD_RX_MSGQ_CFG 0x8
34 #define NGD_INT_EN 0x10
35 #define NGD_INT_RECFG_DONE BIT(24)
36 #define NGD_INT_TX_NACKED_2 BIT(25)
37 #define NGD_INT_MSG_BUF_CONTE BIT(26)
38 #define NGD_INT_MSG_TX_INVAL BIT(27)
39 #define NGD_INT_IE_VE_CHG BIT(28)
40 #define NGD_INT_DEV_ERR BIT(29)
41 #define NGD_INT_RX_MSG_RCVD BIT(30)
42 #define NGD_INT_TX_MSG_SENT BIT(31)
43 #define NGD_INT_STAT 0x14
44 #define NGD_INT_CLR 0x18
45 #define DEF_NGD_INT_MASK (NGD_INT_TX_NACKED_2 | NGD_INT_MSG_BUF_CONTE | \
46 NGD_INT_MSG_TX_INVAL | NGD_INT_IE_VE_CHG | \
47 NGD_INT_DEV_ERR | NGD_INT_TX_MSG_SENT | \
50 /* Slimbus QMI service */
51 #define SLIMBUS_QMI_SVC_ID 0x0301
52 #define SLIMBUS_QMI_SVC_V1 1
53 #define SLIMBUS_QMI_INS_ID 0
54 #define SLIMBUS_QMI_SELECT_INSTANCE_REQ_V01 0x0020
55 #define SLIMBUS_QMI_SELECT_INSTANCE_RESP_V01 0x0020
56 #define SLIMBUS_QMI_POWER_REQ_V01 0x0021
57 #define SLIMBUS_QMI_POWER_RESP_V01 0x0021
58 #define SLIMBUS_QMI_CHECK_FRAMER_STATUS_REQ 0x0022
59 #define SLIMBUS_QMI_CHECK_FRAMER_STATUS_RESP 0x0022
60 #define SLIMBUS_QMI_POWER_REQ_MAX_MSG_LEN 14
61 #define SLIMBUS_QMI_POWER_RESP_MAX_MSG_LEN 7
62 #define SLIMBUS_QMI_SELECT_INSTANCE_REQ_MAX_MSG_LEN 14
63 #define SLIMBUS_QMI_SELECT_INSTANCE_RESP_MAX_MSG_LEN 7
64 #define SLIMBUS_QMI_CHECK_FRAMER_STAT_RESP_MAX_MSG_LEN 7
65 /* QMI response timeout of 500ms */
66 #define SLIMBUS_QMI_RESP_TOUT 1000
68 /* User defined commands */
69 #define SLIM_USR_MC_GENERIC_ACK 0x25
70 #define SLIM_USR_MC_MASTER_CAPABILITY 0x0
71 #define SLIM_USR_MC_REPORT_SATELLITE 0x1
72 #define SLIM_USR_MC_ADDR_QUERY 0xD
73 #define SLIM_USR_MC_ADDR_REPLY 0xE
74 #define SLIM_USR_MC_DEFINE_CHAN 0x20
75 #define SLIM_USR_MC_DEF_ACT_CHAN 0x21
76 #define SLIM_USR_MC_CHAN_CTRL 0x23
77 #define SLIM_USR_MC_RECONFIG_NOW 0x24
78 #define SLIM_USR_MC_REQ_BW 0x28
79 #define SLIM_USR_MC_CONNECT_SRC 0x2C
80 #define SLIM_USR_MC_CONNECT_SINK 0x2D
81 #define SLIM_USR_MC_DISCONNECT_PORT 0x2E
82 #define SLIM_USR_MC_REPEAT_CHANGE_VALUE 0x0
84 #define QCOM_SLIM_NGD_AUTOSUSPEND MSEC_PER_SEC
85 #define SLIM_RX_MSGQ_TIMEOUT_VAL 0x10000
87 #define SLIM_LA_MGR 0xFF
88 #define SLIM_ROOT_FREQ 24576000
91 /* Per spec.max 40 bytes per received message */
92 #define SLIM_MSGQ_BUF_LEN 40
93 #define QCOM_SLIM_NGD_DESC_NUM 32
95 #define SLIM_MSG_ASM_FIRST_WORD(l, mt, mc, dt, ad) \
96 ((l) | ((mt) << 5) | ((mc) << 8) | ((dt) << 15) | ((ad) << 16))
98 #define INIT_MX_RETRIES 10
99 #define DEF_RETRY_MS 10
100 #define SAT_MAGIC_LSB 0xD9
101 #define SAT_MAGIC_MSB 0xC5
102 #define SAT_MSG_VER 0x1
103 #define SAT_MSG_PROT 0x1
104 #define to_ngd(d) container_of(d, struct qcom_slim_ngd, dev)
106 struct ngd_reg_offset_data
{
110 static const struct ngd_reg_offset_data ngd_v1_5_offset_info
= {
115 enum qcom_slim_ngd_state
{
116 QCOM_SLIM_NGD_CTRL_AWAKE
,
117 QCOM_SLIM_NGD_CTRL_IDLE
,
118 QCOM_SLIM_NGD_CTRL_ASLEEP
,
119 QCOM_SLIM_NGD_CTRL_DOWN
,
122 struct qcom_slim_ngd_qmi
{
123 struct qmi_handle qmi
;
124 struct sockaddr_qrtr svc_info
;
125 struct qmi_handle svc_event_hdl
;
126 struct qmi_response_type_v01 resp
;
127 struct qmi_handle
*handle
;
128 struct completion qmi_comp
;
131 struct qcom_slim_ngd_ctrl
;
132 struct qcom_slim_ngd
;
134 struct qcom_slim_ngd_dma_desc
{
135 struct dma_async_tx_descriptor
*desc
;
136 struct qcom_slim_ngd_ctrl
*ctrl
;
137 struct completion
*comp
;
143 struct qcom_slim_ngd
{
144 struct platform_device
*pdev
;
149 struct qcom_slim_ngd_ctrl
{
150 struct slim_framer framer
;
151 struct slim_controller ctrl
;
152 struct qcom_slim_ngd_qmi qmi
;
153 struct qcom_slim_ngd
*ngd
;
156 struct dma_chan
*dma_rx_channel
;
157 struct dma_chan
*dma_tx_channel
;
158 struct qcom_slim_ngd_dma_desc rx_desc
[QCOM_SLIM_NGD_DESC_NUM
];
159 struct qcom_slim_ngd_dma_desc txdesc
[QCOM_SLIM_NGD_DESC_NUM
];
160 struct completion reconf
;
161 struct work_struct m_work
;
162 struct work_struct ngd_up_work
;
163 struct workqueue_struct
*mwq
;
164 struct completion qmi_up
;
165 spinlock_t tx_buf_lock
;
166 struct mutex tx_lock
;
167 struct mutex ssr_lock
;
168 struct notifier_block nb
;
170 struct pdr_handle
*pdr
;
171 enum qcom_slim_ngd_state state
;
172 dma_addr_t rx_phys_base
;
173 dma_addr_t tx_phys_base
;
181 enum slimbus_mode_enum_type_v01
{
182 /* To force a 32 bit signed enum. Do not change or use*/
183 SLIMBUS_MODE_ENUM_TYPE_MIN_ENUM_VAL_V01
= INT_MIN
,
184 SLIMBUS_MODE_SATELLITE_V01
= 1,
185 SLIMBUS_MODE_MASTER_V01
= 2,
186 SLIMBUS_MODE_ENUM_TYPE_MAX_ENUM_VAL_V01
= INT_MAX
,
189 enum slimbus_pm_enum_type_v01
{
190 /* To force a 32 bit signed enum. Do not change or use*/
191 SLIMBUS_PM_ENUM_TYPE_MIN_ENUM_VAL_V01
= INT_MIN
,
192 SLIMBUS_PM_INACTIVE_V01
= 1,
193 SLIMBUS_PM_ACTIVE_V01
= 2,
194 SLIMBUS_PM_ENUM_TYPE_MAX_ENUM_VAL_V01
= INT_MAX
,
197 enum slimbus_resp_enum_type_v01
{
198 SLIMBUS_RESP_ENUM_TYPE_MIN_VAL_V01
= INT_MIN
,
199 SLIMBUS_RESP_SYNCHRONOUS_V01
= 1,
200 SLIMBUS_RESP_ENUM_TYPE_MAX_VAL_V01
= INT_MAX
,
203 struct slimbus_select_inst_req_msg_v01
{
206 enum slimbus_mode_enum_type_v01 mode
;
209 struct slimbus_select_inst_resp_msg_v01
{
210 struct qmi_response_type_v01 resp
;
213 struct slimbus_power_req_msg_v01
{
214 enum slimbus_pm_enum_type_v01 pm_req
;
215 uint8_t resp_type_valid
;
216 enum slimbus_resp_enum_type_v01 resp_type
;
219 struct slimbus_power_resp_msg_v01
{
220 struct qmi_response_type_v01 resp
;
223 static struct qmi_elem_info slimbus_select_inst_req_msg_v01_ei
[] = {
225 .data_type
= QMI_UNSIGNED_4_BYTE
,
227 .elem_size
= sizeof(uint32_t),
228 .array_type
= NO_ARRAY
,
230 .offset
= offsetof(struct slimbus_select_inst_req_msg_v01
,
235 .data_type
= QMI_OPT_FLAG
,
237 .elem_size
= sizeof(uint8_t),
238 .array_type
= NO_ARRAY
,
240 .offset
= offsetof(struct slimbus_select_inst_req_msg_v01
,
245 .data_type
= QMI_UNSIGNED_4_BYTE
,
247 .elem_size
= sizeof(enum slimbus_mode_enum_type_v01
),
248 .array_type
= NO_ARRAY
,
250 .offset
= offsetof(struct slimbus_select_inst_req_msg_v01
,
255 .data_type
= QMI_EOTI
,
258 .array_type
= NO_ARRAY
,
265 static struct qmi_elem_info slimbus_select_inst_resp_msg_v01_ei
[] = {
267 .data_type
= QMI_STRUCT
,
269 .elem_size
= sizeof(struct qmi_response_type_v01
),
270 .array_type
= NO_ARRAY
,
272 .offset
= offsetof(struct slimbus_select_inst_resp_msg_v01
,
274 .ei_array
= qmi_response_type_v01_ei
,
277 .data_type
= QMI_EOTI
,
280 .array_type
= NO_ARRAY
,
287 static struct qmi_elem_info slimbus_power_req_msg_v01_ei
[] = {
289 .data_type
= QMI_UNSIGNED_4_BYTE
,
291 .elem_size
= sizeof(enum slimbus_pm_enum_type_v01
),
292 .array_type
= NO_ARRAY
,
294 .offset
= offsetof(struct slimbus_power_req_msg_v01
,
299 .data_type
= QMI_OPT_FLAG
,
301 .elem_size
= sizeof(uint8_t),
302 .array_type
= NO_ARRAY
,
304 .offset
= offsetof(struct slimbus_power_req_msg_v01
,
308 .data_type
= QMI_SIGNED_4_BYTE_ENUM
,
310 .elem_size
= sizeof(enum slimbus_resp_enum_type_v01
),
311 .array_type
= NO_ARRAY
,
313 .offset
= offsetof(struct slimbus_power_req_msg_v01
,
317 .data_type
= QMI_EOTI
,
320 .array_type
= NO_ARRAY
,
327 static struct qmi_elem_info slimbus_power_resp_msg_v01_ei
[] = {
329 .data_type
= QMI_STRUCT
,
331 .elem_size
= sizeof(struct qmi_response_type_v01
),
332 .array_type
= NO_ARRAY
,
334 .offset
= offsetof(struct slimbus_power_resp_msg_v01
, resp
),
335 .ei_array
= qmi_response_type_v01_ei
,
338 .data_type
= QMI_EOTI
,
341 .array_type
= NO_ARRAY
,
348 static int qcom_slim_qmi_send_select_inst_req(struct qcom_slim_ngd_ctrl
*ctrl
,
349 struct slimbus_select_inst_req_msg_v01
*req
)
351 struct slimbus_select_inst_resp_msg_v01 resp
= { { 0, 0 } };
355 rc
= qmi_txn_init(ctrl
->qmi
.handle
, &txn
,
356 slimbus_select_inst_resp_msg_v01_ei
, &resp
);
358 dev_err(ctrl
->dev
, "QMI TXN init fail: %d\n", rc
);
362 rc
= qmi_send_request(ctrl
->qmi
.handle
, NULL
, &txn
,
363 SLIMBUS_QMI_SELECT_INSTANCE_REQ_V01
,
364 SLIMBUS_QMI_SELECT_INSTANCE_REQ_MAX_MSG_LEN
,
365 slimbus_select_inst_req_msg_v01_ei
, req
);
367 dev_err(ctrl
->dev
, "QMI send req fail %d\n", rc
);
368 qmi_txn_cancel(&txn
);
372 rc
= qmi_txn_wait(&txn
, SLIMBUS_QMI_RESP_TOUT
);
374 dev_err(ctrl
->dev
, "QMI TXN wait fail: %d\n", rc
);
377 /* Check the response */
378 if (resp
.resp
.result
!= QMI_RESULT_SUCCESS_V01
) {
379 dev_err(ctrl
->dev
, "QMI request failed 0x%x\n",
387 static void qcom_slim_qmi_power_resp_cb(struct qmi_handle
*handle
,
388 struct sockaddr_qrtr
*sq
,
389 struct qmi_txn
*txn
, const void *data
)
391 struct slimbus_power_resp_msg_v01
*resp
;
393 resp
= (struct slimbus_power_resp_msg_v01
*)data
;
394 if (resp
->resp
.result
!= QMI_RESULT_SUCCESS_V01
)
395 pr_err("QMI power request failed 0x%x\n",
398 complete(&txn
->completion
);
401 static int qcom_slim_qmi_send_power_request(struct qcom_slim_ngd_ctrl
*ctrl
,
402 struct slimbus_power_req_msg_v01
*req
)
404 struct slimbus_power_resp_msg_v01 resp
= { { 0, 0 } };
408 rc
= qmi_txn_init(ctrl
->qmi
.handle
, &txn
,
409 slimbus_power_resp_msg_v01_ei
, &resp
);
411 rc
= qmi_send_request(ctrl
->qmi
.handle
, NULL
, &txn
,
412 SLIMBUS_QMI_POWER_REQ_V01
,
413 SLIMBUS_QMI_POWER_REQ_MAX_MSG_LEN
,
414 slimbus_power_req_msg_v01_ei
, req
);
416 dev_err(ctrl
->dev
, "QMI send req fail %d\n", rc
);
417 qmi_txn_cancel(&txn
);
421 rc
= qmi_txn_wait(&txn
, SLIMBUS_QMI_RESP_TOUT
);
423 dev_err(ctrl
->dev
, "QMI TXN wait fail: %d\n", rc
);
427 /* Check the response */
428 if (resp
.resp
.result
!= QMI_RESULT_SUCCESS_V01
) {
429 dev_err(ctrl
->dev
, "QMI request failed 0x%x\n",
437 static const struct qmi_msg_handler qcom_slim_qmi_msg_handlers
[] = {
439 .type
= QMI_RESPONSE
,
440 .msg_id
= SLIMBUS_QMI_POWER_RESP_V01
,
441 .ei
= slimbus_power_resp_msg_v01_ei
,
442 .decoded_size
= sizeof(struct slimbus_power_resp_msg_v01
),
443 .fn
= qcom_slim_qmi_power_resp_cb
,
448 static int qcom_slim_qmi_init(struct qcom_slim_ngd_ctrl
*ctrl
,
451 struct slimbus_select_inst_req_msg_v01 req
;
452 struct qmi_handle
*handle
;
455 handle
= devm_kzalloc(ctrl
->dev
, sizeof(*handle
), GFP_KERNEL
);
459 rc
= qmi_handle_init(handle
, SLIMBUS_QMI_POWER_REQ_MAX_MSG_LEN
,
460 NULL
, qcom_slim_qmi_msg_handlers
);
462 dev_err(ctrl
->dev
, "QMI client init failed: %d\n", rc
);
463 goto qmi_handle_init_failed
;
466 rc
= kernel_connect(handle
->sock
,
467 (struct sockaddr
*)&ctrl
->qmi
.svc_info
,
468 sizeof(ctrl
->qmi
.svc_info
), 0);
470 dev_err(ctrl
->dev
, "Remote Service connect failed: %d\n", rc
);
471 goto qmi_connect_to_service_failed
;
474 /* Instance is 0 based */
475 req
.instance
= (ctrl
->ngd
->id
>> 1);
478 /* Mode indicates the role of the ADSP */
480 req
.mode
= SLIMBUS_MODE_SATELLITE_V01
;
482 req
.mode
= SLIMBUS_MODE_MASTER_V01
;
484 ctrl
->qmi
.handle
= handle
;
486 rc
= qcom_slim_qmi_send_select_inst_req(ctrl
, &req
);
488 dev_err(ctrl
->dev
, "failed to select h/w instance\n");
489 goto qmi_select_instance_failed
;
494 qmi_select_instance_failed
:
495 ctrl
->qmi
.handle
= NULL
;
496 qmi_connect_to_service_failed
:
497 qmi_handle_release(handle
);
498 qmi_handle_init_failed
:
499 devm_kfree(ctrl
->dev
, handle
);
503 static void qcom_slim_qmi_exit(struct qcom_slim_ngd_ctrl
*ctrl
)
505 if (!ctrl
->qmi
.handle
)
508 qmi_handle_release(ctrl
->qmi
.handle
);
509 devm_kfree(ctrl
->dev
, ctrl
->qmi
.handle
);
510 ctrl
->qmi
.handle
= NULL
;
513 static int qcom_slim_qmi_power_request(struct qcom_slim_ngd_ctrl
*ctrl
,
516 struct slimbus_power_req_msg_v01 req
;
519 req
.pm_req
= SLIMBUS_PM_ACTIVE_V01
;
521 req
.pm_req
= SLIMBUS_PM_INACTIVE_V01
;
523 req
.resp_type_valid
= 0;
525 return qcom_slim_qmi_send_power_request(ctrl
, &req
);
528 static u32
*qcom_slim_ngd_tx_msg_get(struct qcom_slim_ngd_ctrl
*ctrl
, int len
,
529 struct completion
*comp
)
531 struct qcom_slim_ngd_dma_desc
*desc
;
534 spin_lock_irqsave(&ctrl
->tx_buf_lock
, flags
);
536 if ((ctrl
->tx_tail
+ 1) % QCOM_SLIM_NGD_DESC_NUM
== ctrl
->tx_head
) {
537 spin_unlock_irqrestore(&ctrl
->tx_buf_lock
, flags
);
540 desc
= &ctrl
->txdesc
[ctrl
->tx_tail
];
541 desc
->base
= ctrl
->tx_base
+ ctrl
->tx_tail
* SLIM_MSGQ_BUF_LEN
;
543 ctrl
->tx_tail
= (ctrl
->tx_tail
+ 1) % QCOM_SLIM_NGD_DESC_NUM
;
545 spin_unlock_irqrestore(&ctrl
->tx_buf_lock
, flags
);
550 static void qcom_slim_ngd_tx_msg_dma_cb(void *args
)
552 struct qcom_slim_ngd_dma_desc
*desc
= args
;
553 struct qcom_slim_ngd_ctrl
*ctrl
= desc
->ctrl
;
556 spin_lock_irqsave(&ctrl
->tx_buf_lock
, flags
);
559 complete(desc
->comp
);
563 ctrl
->tx_head
= (ctrl
->tx_head
+ 1) % QCOM_SLIM_NGD_DESC_NUM
;
564 spin_unlock_irqrestore(&ctrl
->tx_buf_lock
, flags
);
567 static int qcom_slim_ngd_tx_msg_post(struct qcom_slim_ngd_ctrl
*ctrl
,
570 struct qcom_slim_ngd_dma_desc
*desc
;
574 spin_lock_irqsave(&ctrl
->tx_buf_lock
, flags
);
575 offset
= buf
- ctrl
->tx_base
;
576 index
= offset
/SLIM_MSGQ_BUF_LEN
;
578 desc
= &ctrl
->txdesc
[index
];
579 desc
->phys
= ctrl
->tx_phys_base
+ offset
;
580 desc
->base
= ctrl
->tx_base
+ offset
;
582 len
= (len
+ 3) & 0xfc;
584 desc
->desc
= dmaengine_prep_slave_single(ctrl
->dma_tx_channel
,
589 dev_err(ctrl
->dev
, "unable to prepare channel\n");
590 spin_unlock_irqrestore(&ctrl
->tx_buf_lock
, flags
);
594 desc
->desc
->callback
= qcom_slim_ngd_tx_msg_dma_cb
;
595 desc
->desc
->callback_param
= desc
;
596 desc
->desc
->cookie
= dmaengine_submit(desc
->desc
);
597 dma_async_issue_pending(ctrl
->dma_tx_channel
);
598 spin_unlock_irqrestore(&ctrl
->tx_buf_lock
, flags
);
603 static void qcom_slim_ngd_rx(struct qcom_slim_ngd_ctrl
*ctrl
, u8
*buf
)
607 mt
= SLIM_HEADER_GET_MT(buf
[0]);
608 len
= SLIM_HEADER_GET_RL(buf
[0]);
609 mc
= SLIM_HEADER_GET_MC(buf
[1]);
611 if (mc
== SLIM_USR_MC_MASTER_CAPABILITY
&&
612 mt
== SLIM_MSG_MT_SRC_REFERRED_USER
)
613 queue_work(ctrl
->mwq
, &ctrl
->m_work
);
615 if (mc
== SLIM_MSG_MC_REPLY_INFORMATION
||
616 mc
== SLIM_MSG_MC_REPLY_VALUE
|| (mc
== SLIM_USR_MC_ADDR_REPLY
&&
617 mt
== SLIM_MSG_MT_SRC_REFERRED_USER
) ||
618 (mc
== SLIM_USR_MC_GENERIC_ACK
&&
619 mt
== SLIM_MSG_MT_SRC_REFERRED_USER
)) {
620 slim_msg_response(&ctrl
->ctrl
, &buf
[4], buf
[3], len
- 4);
621 pm_runtime_mark_last_busy(ctrl
->dev
);
625 static void qcom_slim_ngd_rx_msgq_cb(void *args
)
627 struct qcom_slim_ngd_dma_desc
*desc
= args
;
628 struct qcom_slim_ngd_ctrl
*ctrl
= desc
->ctrl
;
630 qcom_slim_ngd_rx(ctrl
, (u8
*)desc
->base
);
631 /* Add descriptor back to the queue */
632 desc
->desc
= dmaengine_prep_slave_single(ctrl
->dma_rx_channel
,
633 desc
->phys
, SLIM_MSGQ_BUF_LEN
,
637 dev_err(ctrl
->dev
, "Unable to prepare rx channel\n");
641 desc
->desc
->callback
= qcom_slim_ngd_rx_msgq_cb
;
642 desc
->desc
->callback_param
= desc
;
643 desc
->desc
->cookie
= dmaengine_submit(desc
->desc
);
644 dma_async_issue_pending(ctrl
->dma_rx_channel
);
647 static int qcom_slim_ngd_post_rx_msgq(struct qcom_slim_ngd_ctrl
*ctrl
)
649 struct qcom_slim_ngd_dma_desc
*desc
;
652 for (i
= 0; i
< QCOM_SLIM_NGD_DESC_NUM
; i
++) {
653 desc
= &ctrl
->rx_desc
[i
];
654 desc
->phys
= ctrl
->rx_phys_base
+ i
* SLIM_MSGQ_BUF_LEN
;
656 desc
->base
= ctrl
->rx_base
+ i
* SLIM_MSGQ_BUF_LEN
;
657 desc
->desc
= dmaengine_prep_slave_single(ctrl
->dma_rx_channel
,
658 desc
->phys
, SLIM_MSGQ_BUF_LEN
,
662 dev_err(ctrl
->dev
, "Unable to prepare rx channel\n");
666 desc
->desc
->callback
= qcom_slim_ngd_rx_msgq_cb
;
667 desc
->desc
->callback_param
= desc
;
668 desc
->desc
->cookie
= dmaengine_submit(desc
->desc
);
670 dma_async_issue_pending(ctrl
->dma_rx_channel
);
675 static int qcom_slim_ngd_init_rx_msgq(struct qcom_slim_ngd_ctrl
*ctrl
)
677 struct device
*dev
= ctrl
->dev
;
680 ctrl
->dma_rx_channel
= dma_request_chan(dev
, "rx");
681 if (IS_ERR(ctrl
->dma_rx_channel
)) {
682 dev_err(dev
, "Failed to request RX dma channel");
683 ret
= PTR_ERR(ctrl
->dma_rx_channel
);
684 ctrl
->dma_rx_channel
= NULL
;
688 size
= QCOM_SLIM_NGD_DESC_NUM
* SLIM_MSGQ_BUF_LEN
;
689 ctrl
->rx_base
= dma_alloc_coherent(dev
, size
, &ctrl
->rx_phys_base
,
691 if (!ctrl
->rx_base
) {
696 ret
= qcom_slim_ngd_post_rx_msgq(ctrl
);
698 dev_err(dev
, "post_rx_msgq() failed 0x%x\n", ret
);
705 dma_free_coherent(dev
, size
, ctrl
->rx_base
, ctrl
->rx_phys_base
);
707 dma_release_channel(ctrl
->dma_rx_channel
);
711 static int qcom_slim_ngd_init_tx_msgq(struct qcom_slim_ngd_ctrl
*ctrl
)
713 struct device
*dev
= ctrl
->dev
;
718 ctrl
->dma_tx_channel
= dma_request_chan(dev
, "tx");
719 if (IS_ERR(ctrl
->dma_tx_channel
)) {
720 dev_err(dev
, "Failed to request TX dma channel");
721 ret
= PTR_ERR(ctrl
->dma_tx_channel
);
722 ctrl
->dma_tx_channel
= NULL
;
726 size
= ((QCOM_SLIM_NGD_DESC_NUM
+ 1) * SLIM_MSGQ_BUF_LEN
);
727 ctrl
->tx_base
= dma_alloc_coherent(dev
, size
, &ctrl
->tx_phys_base
,
729 if (!ctrl
->tx_base
) {
734 spin_lock_irqsave(&ctrl
->tx_buf_lock
, flags
);
737 spin_unlock_irqrestore(&ctrl
->tx_buf_lock
, flags
);
741 dma_release_channel(ctrl
->dma_tx_channel
);
745 static int qcom_slim_ngd_init_dma(struct qcom_slim_ngd_ctrl
*ctrl
)
749 ret
= qcom_slim_ngd_init_rx_msgq(ctrl
);
751 dev_err(ctrl
->dev
, "rx dma init failed\n");
755 ret
= qcom_slim_ngd_init_tx_msgq(ctrl
);
757 dev_err(ctrl
->dev
, "tx dma init failed\n");
762 static irqreturn_t
qcom_slim_ngd_interrupt(int irq
, void *d
)
764 struct qcom_slim_ngd_ctrl
*ctrl
= d
;
765 void __iomem
*base
= ctrl
->ngd
->base
;
766 u32 stat
= readl(base
+ NGD_INT_STAT
);
768 if ((stat
& NGD_INT_MSG_BUF_CONTE
) ||
769 (stat
& NGD_INT_MSG_TX_INVAL
) || (stat
& NGD_INT_DEV_ERR
) ||
770 (stat
& NGD_INT_TX_NACKED_2
)) {
771 dev_err(ctrl
->dev
, "Error Interrupt received 0x%x\n", stat
);
774 writel(stat
, base
+ NGD_INT_CLR
);
779 static int qcom_slim_ngd_xfer_msg(struct slim_controller
*sctrl
,
780 struct slim_msg_txn
*txn
)
782 struct qcom_slim_ngd_ctrl
*ctrl
= dev_get_drvdata(sctrl
->dev
);
783 DECLARE_COMPLETION_ONSTACK(tx_sent
);
784 DECLARE_COMPLETION_ONSTACK(done
);
786 u8 wbuf
[SLIM_MSGQ_BUF_LEN
];
787 u8 rbuf
[SLIM_MSGQ_BUF_LEN
];
791 bool usr_msg
= false;
793 if (txn
->mt
== SLIM_MSG_MT_CORE
&&
794 (txn
->mc
>= SLIM_MSG_MC_BEGIN_RECONFIGURATION
&&
795 txn
->mc
<= SLIM_MSG_MC_RECONFIGURE_NOW
))
798 if (txn
->dt
== SLIM_MSG_DEST_ENUMADDR
)
799 return -EPROTONOSUPPORT
;
801 if (txn
->msg
->num_bytes
> SLIM_MSGQ_BUF_LEN
||
802 txn
->rl
> SLIM_MSGQ_BUF_LEN
) {
803 dev_err(ctrl
->dev
, "msg exceeds HW limit\n");
807 pbuf
= qcom_slim_ngd_tx_msg_get(ctrl
, txn
->rl
, &tx_sent
);
809 dev_err(ctrl
->dev
, "Message buffer unavailable\n");
813 if (txn
->mt
== SLIM_MSG_MT_CORE
&&
814 (txn
->mc
== SLIM_MSG_MC_CONNECT_SOURCE
||
815 txn
->mc
== SLIM_MSG_MC_CONNECT_SINK
||
816 txn
->mc
== SLIM_MSG_MC_DISCONNECT_PORT
)) {
817 txn
->mt
= SLIM_MSG_MT_DEST_REFERRED_USER
;
819 case SLIM_MSG_MC_CONNECT_SOURCE
:
820 txn
->mc
= SLIM_USR_MC_CONNECT_SRC
;
822 case SLIM_MSG_MC_CONNECT_SINK
:
823 txn
->mc
= SLIM_USR_MC_CONNECT_SINK
;
825 case SLIM_MSG_MC_DISCONNECT_PORT
:
826 txn
->mc
= SLIM_USR_MC_DISCONNECT_PORT
;
836 wbuf
[i
++] = txn
->msg
->wbuf
[0];
837 if (txn
->mc
!= SLIM_USR_MC_DISCONNECT_PORT
)
838 wbuf
[i
++] = txn
->msg
->wbuf
[1];
841 ret
= slim_alloc_txn_tid(sctrl
, txn
);
843 dev_err(ctrl
->dev
, "Unable to allocate TID\n");
847 wbuf
[i
++] = txn
->tid
;
849 txn
->msg
->num_bytes
= i
;
850 txn
->msg
->wbuf
= wbuf
;
851 txn
->msg
->rbuf
= rbuf
;
852 txn
->rl
= txn
->msg
->num_bytes
+ 4;
855 /* HW expects length field to be excluded */
859 if (txn
->dt
== SLIM_MSG_DEST_LOGICALADDR
) {
860 *pbuf
= SLIM_MSG_ASM_FIRST_WORD(txn
->rl
, txn
->mt
, txn
->mc
, 0,
864 *pbuf
= SLIM_MSG_ASM_FIRST_WORD(txn
->rl
, txn
->mt
, txn
->mc
, 1,
869 if (slim_tid_txn(txn
->mt
, txn
->mc
))
872 if (slim_ec_txn(txn
->mt
, txn
->mc
)) {
873 *(puc
++) = (txn
->ec
& 0xFF);
874 *(puc
++) = (txn
->ec
>> 8) & 0xFF;
877 if (txn
->msg
&& txn
->msg
->wbuf
)
878 memcpy(puc
, txn
->msg
->wbuf
, txn
->msg
->num_bytes
);
880 mutex_lock(&ctrl
->tx_lock
);
881 ret
= qcom_slim_ngd_tx_msg_post(ctrl
, pbuf
, txn
->rl
);
883 mutex_unlock(&ctrl
->tx_lock
);
887 timeout
= wait_for_completion_timeout(&tx_sent
, HZ
);
889 dev_err(sctrl
->dev
, "TX timed out:MC:0x%x,mt:0x%x", txn
->mc
,
891 mutex_unlock(&ctrl
->tx_lock
);
896 timeout
= wait_for_completion_timeout(&done
, HZ
);
898 dev_err(sctrl
->dev
, "TX timed out:MC:0x%x,mt:0x%x",
900 mutex_unlock(&ctrl
->tx_lock
);
905 mutex_unlock(&ctrl
->tx_lock
);
909 static int qcom_slim_ngd_xfer_msg_sync(struct slim_controller
*ctrl
,
910 struct slim_msg_txn
*txn
)
912 DECLARE_COMPLETION_ONSTACK(done
);
915 pm_runtime_get_sync(ctrl
->dev
);
919 ret
= qcom_slim_ngd_xfer_msg(ctrl
, txn
);
923 timeout
= wait_for_completion_timeout(&done
, HZ
);
925 dev_err(ctrl
->dev
, "TX timed out:MC:0x%x,mt:0x%x", txn
->mc
,
932 static int qcom_slim_ngd_enable_stream(struct slim_stream_runtime
*rt
)
934 struct slim_device
*sdev
= rt
->dev
;
935 struct slim_controller
*ctrl
= sdev
->ctrl
;
936 struct slim_val_inf msg
= {0};
937 u8 wbuf
[SLIM_MSGQ_BUF_LEN
];
938 u8 rbuf
[SLIM_MSGQ_BUF_LEN
];
939 struct slim_msg_txn txn
= {0,};
942 txn
.mt
= SLIM_MSG_MT_DEST_REFERRED_USER
;
943 txn
.dt
= SLIM_MSG_DEST_LOGICALADDR
;
944 txn
.la
= SLIM_LA_MGR
;
947 txn
.msg
->num_bytes
= 0;
948 txn
.msg
->wbuf
= wbuf
;
949 txn
.msg
->rbuf
= rbuf
;
951 for (i
= 0; i
< rt
->num_ports
; i
++) {
952 struct slim_port
*port
= &rt
->ports
[i
];
954 if (txn
.msg
->num_bytes
== 0) {
955 int seg_interval
= SLIM_SLOTS_PER_SUPERFRAME
/rt
->ratem
;
958 wbuf
[txn
.msg
->num_bytes
++] = sdev
->laddr
;
959 wbuf
[txn
.msg
->num_bytes
] = rt
->bps
>> 2 |
960 (port
->ch
.aux_fmt
<< 6);
962 /* Data channel segment interval not multiple of 3 */
963 exp
= seg_interval
% 3;
965 wbuf
[txn
.msg
->num_bytes
] |= BIT(5);
967 txn
.msg
->num_bytes
++;
968 wbuf
[txn
.msg
->num_bytes
++] = exp
<< 4 | rt
->prot
;
970 if (rt
->prot
== SLIM_PROTO_ISO
)
971 wbuf
[txn
.msg
->num_bytes
++] =
973 SLIM_CHANNEL_CONTENT_FL
;
975 wbuf
[txn
.msg
->num_bytes
++] = port
->ch
.prrate
;
977 ret
= slim_alloc_txn_tid(ctrl
, &txn
);
979 dev_err(&sdev
->dev
, "Fail to allocate TID\n");
982 wbuf
[txn
.msg
->num_bytes
++] = txn
.tid
;
984 wbuf
[txn
.msg
->num_bytes
++] = port
->ch
.id
;
987 txn
.mc
= SLIM_USR_MC_DEF_ACT_CHAN
;
988 txn
.rl
= txn
.msg
->num_bytes
+ 4;
989 ret
= qcom_slim_ngd_xfer_msg_sync(ctrl
, &txn
);
991 slim_free_txn_tid(ctrl
, &txn
);
992 dev_err(&sdev
->dev
, "TX timed out:MC:0x%x,mt:0x%x", txn
.mc
,
997 txn
.mc
= SLIM_USR_MC_RECONFIG_NOW
;
998 txn
.msg
->num_bytes
= 2;
999 wbuf
[1] = sdev
->laddr
;
1000 txn
.rl
= txn
.msg
->num_bytes
+ 4;
1002 ret
= slim_alloc_txn_tid(ctrl
, &txn
);
1004 dev_err(ctrl
->dev
, "Fail to allocate TID\n");
1009 ret
= qcom_slim_ngd_xfer_msg_sync(ctrl
, &txn
);
1011 slim_free_txn_tid(ctrl
, &txn
);
1012 dev_err(&sdev
->dev
, "TX timed out:MC:0x%x,mt:0x%x", txn
.mc
,
1019 static int qcom_slim_ngd_get_laddr(struct slim_controller
*ctrl
,
1020 struct slim_eaddr
*ea
, u8
*laddr
)
1022 struct slim_val_inf msg
= {0};
1023 u8 failed_ea
[6] = {0, 0, 0, 0, 0, 0};
1024 struct slim_msg_txn txn
;
1029 txn
.mt
= SLIM_MSG_MT_DEST_REFERRED_USER
;
1030 txn
.dt
= SLIM_MSG_DEST_LOGICALADDR
;
1031 txn
.la
= SLIM_LA_MGR
;
1034 txn
.mc
= SLIM_USR_MC_ADDR_QUERY
;
1037 txn
.msg
->num_bytes
= 7;
1038 txn
.msg
->wbuf
= wbuf
;
1039 txn
.msg
->rbuf
= rbuf
;
1041 ret
= slim_alloc_txn_tid(ctrl
, &txn
);
1045 wbuf
[0] = (u8
)txn
.tid
;
1046 memcpy(&wbuf
[1], ea
, sizeof(*ea
));
1048 ret
= qcom_slim_ngd_xfer_msg_sync(ctrl
, &txn
);
1050 slim_free_txn_tid(ctrl
, &txn
);
1054 if (!memcmp(rbuf
, failed_ea
, 6))
1062 static int qcom_slim_ngd_exit_dma(struct qcom_slim_ngd_ctrl
*ctrl
)
1064 if (ctrl
->dma_rx_channel
) {
1065 dmaengine_terminate_sync(ctrl
->dma_rx_channel
);
1066 dma_release_channel(ctrl
->dma_rx_channel
);
1069 if (ctrl
->dma_tx_channel
) {
1070 dmaengine_terminate_sync(ctrl
->dma_tx_channel
);
1071 dma_release_channel(ctrl
->dma_tx_channel
);
1074 ctrl
->dma_tx_channel
= ctrl
->dma_rx_channel
= NULL
;
1079 static void qcom_slim_ngd_setup(struct qcom_slim_ngd_ctrl
*ctrl
)
1081 u32 cfg
= readl_relaxed(ctrl
->ngd
->base
);
1083 if (ctrl
->state
== QCOM_SLIM_NGD_CTRL_DOWN
)
1084 qcom_slim_ngd_init_dma(ctrl
);
1086 /* By default enable message queues */
1087 cfg
|= NGD_CFG_RX_MSGQ_EN
;
1088 cfg
|= NGD_CFG_TX_MSGQ_EN
;
1090 /* Enable NGD if it's not already enabled*/
1091 if (!(cfg
& NGD_CFG_ENABLE
))
1092 cfg
|= NGD_CFG_ENABLE
;
1094 writel_relaxed(cfg
, ctrl
->ngd
->base
);
1097 static int qcom_slim_ngd_power_up(struct qcom_slim_ngd_ctrl
*ctrl
)
1099 enum qcom_slim_ngd_state cur_state
= ctrl
->state
;
1100 struct qcom_slim_ngd
*ngd
= ctrl
->ngd
;
1102 int timeout
, ret
= 0;
1104 if (ctrl
->state
== QCOM_SLIM_NGD_CTRL_DOWN
) {
1105 timeout
= wait_for_completion_timeout(&ctrl
->qmi
.qmi_comp
, HZ
);
1110 if (ctrl
->state
== QCOM_SLIM_NGD_CTRL_ASLEEP
||
1111 ctrl
->state
== QCOM_SLIM_NGD_CTRL_DOWN
) {
1112 ret
= qcom_slim_qmi_power_request(ctrl
, true);
1114 dev_err(ctrl
->dev
, "SLIM QMI power request failed:%d\n",
1120 ctrl
->ver
= readl_relaxed(ctrl
->base
);
1121 /* Version info in 16 MSbits */
1124 laddr
= readl_relaxed(ngd
->base
+ NGD_STATUS
);
1125 if (laddr
& NGD_LADDR
) {
1127 * external MDM restart case where ADSP itself was active framer
1128 * For example, modem restarted when playback was active
1130 if (cur_state
== QCOM_SLIM_NGD_CTRL_AWAKE
) {
1131 dev_info(ctrl
->dev
, "Subsys restart: ADSP active framer\n");
1137 writel_relaxed(DEF_NGD_INT_MASK
, ngd
->base
+ NGD_INT_EN
);
1138 rx_msgq
= readl_relaxed(ngd
->base
+ NGD_RX_MSGQ_CFG
);
1140 writel_relaxed(rx_msgq
|SLIM_RX_MSGQ_TIMEOUT_VAL
,
1141 ngd
->base
+ NGD_RX_MSGQ_CFG
);
1142 qcom_slim_ngd_setup(ctrl
);
1144 timeout
= wait_for_completion_timeout(&ctrl
->reconf
, HZ
);
1146 dev_err(ctrl
->dev
, "capability exchange timed-out\n");
1153 static void qcom_slim_ngd_notify_slaves(struct qcom_slim_ngd_ctrl
*ctrl
)
1155 struct slim_device
*sbdev
;
1156 struct device_node
*node
;
1158 for_each_child_of_node(ctrl
->ngd
->pdev
->dev
.of_node
, node
) {
1159 sbdev
= of_slim_get_device(&ctrl
->ctrl
, node
);
1163 if (slim_get_logical_addr(sbdev
))
1164 dev_err(ctrl
->dev
, "Failed to get logical address\n");
1168 static void qcom_slim_ngd_master_worker(struct work_struct
*work
)
1170 struct qcom_slim_ngd_ctrl
*ctrl
;
1171 struct slim_msg_txn txn
;
1172 struct slim_val_inf msg
= {0};
1177 ctrl
= container_of(work
, struct qcom_slim_ngd_ctrl
, m_work
);
1178 txn
.dt
= SLIM_MSG_DEST_LOGICALADDR
;
1180 txn
.mc
= SLIM_USR_MC_REPORT_SATELLITE
;
1181 txn
.mt
= SLIM_MSG_MT_SRC_REFERRED_USER
;
1182 txn
.la
= SLIM_LA_MGR
;
1183 wbuf
[0] = SAT_MAGIC_LSB
;
1184 wbuf
[1] = SAT_MAGIC_MSB
;
1185 wbuf
[2] = SAT_MSG_VER
;
1186 wbuf
[3] = SAT_MSG_PROT
;
1188 txn
.msg
->wbuf
= wbuf
;
1189 txn
.msg
->num_bytes
= 4;
1192 dev_info(ctrl
->dev
, "SLIM SAT: Rcvd master capability\n");
1195 ret
= qcom_slim_ngd_xfer_msg(&ctrl
->ctrl
, &txn
);
1197 if (ctrl
->state
>= QCOM_SLIM_NGD_CTRL_ASLEEP
)
1198 complete(&ctrl
->reconf
);
1200 dev_err(ctrl
->dev
, "unexpected state:%d\n",
1203 if (ctrl
->state
== QCOM_SLIM_NGD_CTRL_DOWN
)
1204 qcom_slim_ngd_notify_slaves(ctrl
);
1206 } else if (ret
== -EIO
) {
1207 dev_err(ctrl
->dev
, "capability message NACKed, retrying\n");
1208 if (retries
< INIT_MX_RETRIES
) {
1209 msleep(DEF_RETRY_MS
);
1211 goto capability_retry
;
1214 dev_err(ctrl
->dev
, "SLIM: capability TX failed:%d\n", ret
);
1218 static int qcom_slim_ngd_update_device_status(struct device
*dev
, void *null
)
1220 slim_report_absent(to_slim_device(dev
));
1225 static int qcom_slim_ngd_runtime_resume(struct device
*dev
)
1227 struct qcom_slim_ngd_ctrl
*ctrl
= dev_get_drvdata(dev
);
1230 if (!ctrl
->qmi
.handle
)
1233 if (ctrl
->state
>= QCOM_SLIM_NGD_CTRL_ASLEEP
)
1234 ret
= qcom_slim_ngd_power_up(ctrl
);
1236 /* Did SSR cause this power up failure */
1237 if (ctrl
->state
!= QCOM_SLIM_NGD_CTRL_DOWN
)
1238 ctrl
->state
= QCOM_SLIM_NGD_CTRL_ASLEEP
;
1240 dev_err(ctrl
->dev
, "HW wakeup attempt during SSR\n");
1242 ctrl
->state
= QCOM_SLIM_NGD_CTRL_AWAKE
;
1248 static int qcom_slim_ngd_enable(struct qcom_slim_ngd_ctrl
*ctrl
, bool enable
)
1251 int ret
= qcom_slim_qmi_init(ctrl
, false);
1254 dev_err(ctrl
->dev
, "qmi init fail, ret:%d, state:%d\n",
1258 /* controller state should be in sync with framework state */
1259 complete(&ctrl
->qmi
.qmi_comp
);
1260 if (!pm_runtime_enabled(ctrl
->dev
) ||
1261 !pm_runtime_suspended(ctrl
->dev
))
1262 qcom_slim_ngd_runtime_resume(ctrl
->dev
);
1264 pm_runtime_resume(ctrl
->dev
);
1265 pm_runtime_mark_last_busy(ctrl
->dev
);
1266 pm_runtime_put(ctrl
->dev
);
1268 ret
= slim_register_controller(&ctrl
->ctrl
);
1270 dev_err(ctrl
->dev
, "error adding slim controller\n");
1274 dev_info(ctrl
->dev
, "SLIM controller Registered\n");
1276 qcom_slim_qmi_exit(ctrl
);
1277 slim_unregister_controller(&ctrl
->ctrl
);
1283 static int qcom_slim_ngd_qmi_new_server(struct qmi_handle
*hdl
,
1284 struct qmi_service
*service
)
1286 struct qcom_slim_ngd_qmi
*qmi
=
1287 container_of(hdl
, struct qcom_slim_ngd_qmi
, svc_event_hdl
);
1288 struct qcom_slim_ngd_ctrl
*ctrl
=
1289 container_of(qmi
, struct qcom_slim_ngd_ctrl
, qmi
);
1291 qmi
->svc_info
.sq_family
= AF_QIPCRTR
;
1292 qmi
->svc_info
.sq_node
= service
->node
;
1293 qmi
->svc_info
.sq_port
= service
->port
;
1295 complete(&ctrl
->qmi_up
);
1300 static void qcom_slim_ngd_qmi_del_server(struct qmi_handle
*hdl
,
1301 struct qmi_service
*service
)
1303 struct qcom_slim_ngd_qmi
*qmi
=
1304 container_of(hdl
, struct qcom_slim_ngd_qmi
, svc_event_hdl
);
1305 struct qcom_slim_ngd_ctrl
*ctrl
=
1306 container_of(qmi
, struct qcom_slim_ngd_ctrl
, qmi
);
1308 reinit_completion(&ctrl
->qmi_up
);
1309 qmi
->svc_info
.sq_node
= 0;
1310 qmi
->svc_info
.sq_port
= 0;
1313 static const struct qmi_ops qcom_slim_ngd_qmi_svc_event_ops
= {
1314 .new_server
= qcom_slim_ngd_qmi_new_server
,
1315 .del_server
= qcom_slim_ngd_qmi_del_server
,
1318 static int qcom_slim_ngd_qmi_svc_event_init(struct qcom_slim_ngd_ctrl
*ctrl
)
1320 struct qcom_slim_ngd_qmi
*qmi
= &ctrl
->qmi
;
1323 ret
= qmi_handle_init(&qmi
->svc_event_hdl
, 0,
1324 &qcom_slim_ngd_qmi_svc_event_ops
, NULL
);
1326 dev_err(ctrl
->dev
, "qmi_handle_init failed: %d\n", ret
);
1330 ret
= qmi_add_lookup(&qmi
->svc_event_hdl
, SLIMBUS_QMI_SVC_ID
,
1331 SLIMBUS_QMI_SVC_V1
, SLIMBUS_QMI_INS_ID
);
1333 dev_err(ctrl
->dev
, "qmi_add_lookup failed: %d\n", ret
);
1334 qmi_handle_release(&qmi
->svc_event_hdl
);
1339 static void qcom_slim_ngd_qmi_svc_event_deinit(struct qcom_slim_ngd_qmi
*qmi
)
1341 qmi_handle_release(&qmi
->svc_event_hdl
);
1344 static struct platform_driver qcom_slim_ngd_driver
;
1345 #define QCOM_SLIM_NGD_DRV_NAME "qcom,slim-ngd"
1347 static const struct of_device_id qcom_slim_ngd_dt_match
[] = {
1349 .compatible
= "qcom,slim-ngd-v1.5.0",
1350 .data
= &ngd_v1_5_offset_info
,
1352 .compatible
= "qcom,slim-ngd-v2.1.0",
1353 .data
= &ngd_v1_5_offset_info
,
1358 MODULE_DEVICE_TABLE(of
, qcom_slim_ngd_dt_match
);
1360 static void qcom_slim_ngd_down(struct qcom_slim_ngd_ctrl
*ctrl
)
1362 mutex_lock(&ctrl
->ssr_lock
);
1363 device_for_each_child(ctrl
->ctrl
.dev
, NULL
,
1364 qcom_slim_ngd_update_device_status
);
1365 qcom_slim_ngd_enable(ctrl
, false);
1366 mutex_unlock(&ctrl
->ssr_lock
);
1369 static void qcom_slim_ngd_up_worker(struct work_struct
*work
)
1371 struct qcom_slim_ngd_ctrl
*ctrl
;
1373 ctrl
= container_of(work
, struct qcom_slim_ngd_ctrl
, ngd_up_work
);
1375 /* Make sure qmi service is up before continuing */
1376 wait_for_completion_interruptible(&ctrl
->qmi_up
);
1378 mutex_lock(&ctrl
->ssr_lock
);
1379 qcom_slim_ngd_enable(ctrl
, true);
1380 mutex_unlock(&ctrl
->ssr_lock
);
1383 static int qcom_slim_ngd_ssr_pdr_notify(struct qcom_slim_ngd_ctrl
*ctrl
,
1384 unsigned long action
)
1387 case QCOM_SSR_BEFORE_SHUTDOWN
:
1388 case SERVREG_SERVICE_STATE_DOWN
:
1389 /* Make sure the last dma xfer is finished */
1390 mutex_lock(&ctrl
->tx_lock
);
1391 if (ctrl
->state
!= QCOM_SLIM_NGD_CTRL_DOWN
) {
1392 pm_runtime_get_noresume(ctrl
->dev
);
1393 ctrl
->state
= QCOM_SLIM_NGD_CTRL_DOWN
;
1394 qcom_slim_ngd_down(ctrl
);
1395 qcom_slim_ngd_exit_dma(ctrl
);
1397 mutex_unlock(&ctrl
->tx_lock
);
1399 case QCOM_SSR_AFTER_POWERUP
:
1400 case SERVREG_SERVICE_STATE_UP
:
1401 schedule_work(&ctrl
->ngd_up_work
);
1410 static int qcom_slim_ngd_ssr_notify(struct notifier_block
*nb
,
1411 unsigned long action
,
1414 struct qcom_slim_ngd_ctrl
*ctrl
= container_of(nb
,
1415 struct qcom_slim_ngd_ctrl
, nb
);
1417 return qcom_slim_ngd_ssr_pdr_notify(ctrl
, action
);
1420 static void slim_pd_status(int state
, char *svc_path
, void *priv
)
1422 struct qcom_slim_ngd_ctrl
*ctrl
= (struct qcom_slim_ngd_ctrl
*)priv
;
1424 qcom_slim_ngd_ssr_pdr_notify(ctrl
, state
);
1426 static int of_qcom_slim_ngd_register(struct device
*parent
,
1427 struct qcom_slim_ngd_ctrl
*ctrl
)
1429 const struct ngd_reg_offset_data
*data
;
1430 struct qcom_slim_ngd
*ngd
;
1431 const struct of_device_id
*match
;
1432 struct device_node
*node
;
1435 match
= of_match_node(qcom_slim_ngd_dt_match
, parent
->of_node
);
1437 for_each_available_child_of_node(parent
->of_node
, node
) {
1438 if (of_property_read_u32(node
, "reg", &id
))
1441 ngd
= kzalloc(sizeof(*ngd
), GFP_KERNEL
);
1447 ngd
->pdev
= platform_device_alloc(QCOM_SLIM_NGD_DRV_NAME
, id
);
1454 ngd
->pdev
->dev
.parent
= parent
;
1455 ngd
->pdev
->driver_override
= QCOM_SLIM_NGD_DRV_NAME
;
1456 ngd
->pdev
->dev
.of_node
= node
;
1459 platform_device_add(ngd
->pdev
);
1460 ngd
->base
= ctrl
->base
+ ngd
->id
* data
->offset
+
1461 (ngd
->id
- 1) * data
->size
;
1469 static int qcom_slim_ngd_probe(struct platform_device
*pdev
)
1471 struct device
*dev
= &pdev
->dev
;
1472 struct qcom_slim_ngd_ctrl
*ctrl
= dev_get_drvdata(dev
->parent
);
1475 ctrl
->ctrl
.dev
= dev
;
1477 platform_set_drvdata(pdev
, ctrl
);
1478 pm_runtime_use_autosuspend(dev
);
1479 pm_runtime_set_autosuspend_delay(dev
, QCOM_SLIM_NGD_AUTOSUSPEND
);
1480 pm_runtime_set_suspended(dev
);
1481 pm_runtime_enable(dev
);
1482 pm_runtime_get_noresume(dev
);
1483 ret
= qcom_slim_ngd_qmi_svc_event_init(ctrl
);
1485 dev_err(&pdev
->dev
, "QMI service registration failed:%d", ret
);
1489 INIT_WORK(&ctrl
->m_work
, qcom_slim_ngd_master_worker
);
1490 INIT_WORK(&ctrl
->ngd_up_work
, qcom_slim_ngd_up_worker
);
1491 ctrl
->mwq
= create_singlethread_workqueue("ngd_master");
1493 dev_err(&pdev
->dev
, "Failed to start master worker\n");
1500 qcom_slim_ngd_qmi_svc_event_deinit(&ctrl
->qmi
);
1502 destroy_workqueue(ctrl
->mwq
);
1507 static int qcom_slim_ngd_ctrl_probe(struct platform_device
*pdev
)
1509 struct device
*dev
= &pdev
->dev
;
1510 struct qcom_slim_ngd_ctrl
*ctrl
;
1511 struct resource
*res
;
1513 struct pdr_service
*pds
;
1515 ctrl
= devm_kzalloc(dev
, sizeof(*ctrl
), GFP_KERNEL
);
1519 dev_set_drvdata(dev
, ctrl
);
1521 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1522 ctrl
->base
= devm_ioremap_resource(dev
, res
);
1523 if (IS_ERR(ctrl
->base
))
1524 return PTR_ERR(ctrl
->base
);
1526 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1528 dev_err(&pdev
->dev
, "no slimbus IRQ resource\n");
1532 ret
= devm_request_irq(dev
, res
->start
, qcom_slim_ngd_interrupt
,
1533 IRQF_TRIGGER_HIGH
, "slim-ngd", ctrl
);
1535 dev_err(&pdev
->dev
, "request IRQ failed\n");
1539 ctrl
->nb
.notifier_call
= qcom_slim_ngd_ssr_notify
;
1540 ctrl
->notifier
= qcom_register_ssr_notifier("lpass", &ctrl
->nb
);
1541 if (IS_ERR(ctrl
->notifier
))
1542 return PTR_ERR(ctrl
->notifier
);
1545 ctrl
->framer
.rootfreq
= SLIM_ROOT_FREQ
>> 3;
1546 ctrl
->framer
.superfreq
=
1547 ctrl
->framer
.rootfreq
/ SLIM_CL_PER_SUPERFRAME_DIV8
;
1549 ctrl
->ctrl
.a_framer
= &ctrl
->framer
;
1550 ctrl
->ctrl
.clkgear
= SLIM_MAX_CLK_GEAR
;
1551 ctrl
->ctrl
.get_laddr
= qcom_slim_ngd_get_laddr
;
1552 ctrl
->ctrl
.enable_stream
= qcom_slim_ngd_enable_stream
;
1553 ctrl
->ctrl
.xfer_msg
= qcom_slim_ngd_xfer_msg
;
1554 ctrl
->ctrl
.wakeup
= NULL
;
1555 ctrl
->state
= QCOM_SLIM_NGD_CTRL_DOWN
;
1557 mutex_init(&ctrl
->tx_lock
);
1558 mutex_init(&ctrl
->ssr_lock
);
1559 spin_lock_init(&ctrl
->tx_buf_lock
);
1560 init_completion(&ctrl
->reconf
);
1561 init_completion(&ctrl
->qmi
.qmi_comp
);
1562 init_completion(&ctrl
->qmi_up
);
1564 ctrl
->pdr
= pdr_handle_alloc(slim_pd_status
, ctrl
);
1565 if (IS_ERR(ctrl
->pdr
)) {
1566 dev_err(dev
, "Failed to init PDR handle\n");
1567 return PTR_ERR(ctrl
->pdr
);
1570 pds
= pdr_add_lookup(ctrl
->pdr
, "avs/audio", "msm/adsp/audio_pd");
1571 if (IS_ERR(pds
) && PTR_ERR(pds
) != -EALREADY
) {
1572 dev_err(dev
, "pdr add lookup failed: %d\n", ret
);
1573 return PTR_ERR(pds
);
1576 platform_driver_register(&qcom_slim_ngd_driver
);
1577 return of_qcom_slim_ngd_register(dev
, ctrl
);
1580 static int qcom_slim_ngd_ctrl_remove(struct platform_device
*pdev
)
1582 platform_driver_unregister(&qcom_slim_ngd_driver
);
1587 static int qcom_slim_ngd_remove(struct platform_device
*pdev
)
1589 struct qcom_slim_ngd_ctrl
*ctrl
= platform_get_drvdata(pdev
);
1591 pm_runtime_disable(&pdev
->dev
);
1592 pdr_handle_release(ctrl
->pdr
);
1593 qcom_unregister_ssr_notifier(ctrl
->notifier
, &ctrl
->nb
);
1594 qcom_slim_ngd_enable(ctrl
, false);
1595 qcom_slim_ngd_exit_dma(ctrl
);
1596 qcom_slim_ngd_qmi_svc_event_deinit(&ctrl
->qmi
);
1598 destroy_workqueue(ctrl
->mwq
);
1605 static int __maybe_unused
qcom_slim_ngd_runtime_idle(struct device
*dev
)
1607 struct qcom_slim_ngd_ctrl
*ctrl
= dev_get_drvdata(dev
);
1609 if (ctrl
->state
== QCOM_SLIM_NGD_CTRL_AWAKE
)
1610 ctrl
->state
= QCOM_SLIM_NGD_CTRL_IDLE
;
1611 pm_request_autosuspend(dev
);
1615 static int __maybe_unused
qcom_slim_ngd_runtime_suspend(struct device
*dev
)
1617 struct qcom_slim_ngd_ctrl
*ctrl
= dev_get_drvdata(dev
);
1620 if (!ctrl
->qmi
.handle
)
1623 ret
= qcom_slim_qmi_power_request(ctrl
, false);
1624 if (ret
&& ret
!= -EBUSY
)
1625 dev_info(ctrl
->dev
, "slim resource not idle:%d\n", ret
);
1626 if (!ret
|| ret
== -ETIMEDOUT
)
1627 ctrl
->state
= QCOM_SLIM_NGD_CTRL_ASLEEP
;
1632 static const struct dev_pm_ops qcom_slim_ngd_dev_pm_ops
= {
1633 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
1634 pm_runtime_force_resume
)
1636 qcom_slim_ngd_runtime_suspend
,
1637 qcom_slim_ngd_runtime_resume
,
1638 qcom_slim_ngd_runtime_idle
1642 static struct platform_driver qcom_slim_ngd_ctrl_driver
= {
1643 .probe
= qcom_slim_ngd_ctrl_probe
,
1644 .remove
= qcom_slim_ngd_ctrl_remove
,
1646 .name
= "qcom,slim-ngd-ctrl",
1647 .of_match_table
= qcom_slim_ngd_dt_match
,
1651 static struct platform_driver qcom_slim_ngd_driver
= {
1652 .probe
= qcom_slim_ngd_probe
,
1653 .remove
= qcom_slim_ngd_remove
,
1655 .name
= QCOM_SLIM_NGD_DRV_NAME
,
1656 .pm
= &qcom_slim_ngd_dev_pm_ops
,
1660 module_platform_driver(qcom_slim_ngd_ctrl_driver
);
1661 MODULE_LICENSE("GPL v2");
1662 MODULE_DESCRIPTION("Qualcomm SLIMBus NGD controller");