blob: 2682c454dfee77fcb0353ec9e0a0ccf46164dc3f [file] [log] [blame]
Meng Wang43bbb872018-12-10 12:32:05 +08001// SPDX-License-Identifier: GPL-2.0-only
Meng Wang61af6842018-09-10 17:47:55 +08002/*
Aditya Bavanari3517b112018-12-03 13:26:59 +05303 * Copyright (c) 2015-2019, The Linux Foundation. All rights reserved.
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05304 */
5
6#include <linux/irq.h>
7#include <linux/kernel.h>
8#include <linux/init.h>
9#include <linux/slab.h>
10#include <linux/io.h>
11#include <linux/interrupt.h>
12#include <linux/platform_device.h>
13#include <linux/delay.h>
14#include <linux/kthread.h>
Ramprasad Katkamcab8d722018-09-28 15:54:06 +053015#include <linux/bitops.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053016#include <linux/clk.h>
Laxminath Kasama60239e2019-01-10 14:43:03 +053017#include <linux/gpio.h>
18#include <linux/of_gpio.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053019#include <linux/pm_runtime.h>
20#include <linux/of.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053021#include <soc/soundwire.h>
Sudheer Papothi3d1596e2018-10-27 06:19:18 +053022#include <soc/swr-common.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053023#include <linux/regmap.h>
Ramprasad Katkam68765ab2018-08-30 11:46:32 +053024#include <dsp/msm-audio-event-notify.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053025#include "swrm_registers.h"
26#include "swr-mstr-ctrl.h"
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053027
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +053028#define SWRM_FRAME_SYNC_SEL 4000 /* 4KHz */
Ramprasad Katkam57349872018-11-11 18:34:57 +053029#define SWRM_SYSTEM_RESUME_TIMEOUT_MS 700
30#define SWRM_SYS_SUSPEND_WAIT 1
Sudheer Papothi3d1596e2018-10-27 06:19:18 +053031
Sudheer Papothi4c322b12018-10-31 06:34:01 +053032#define SWRM_DSD_PARAMS_PORT 4
33
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053034#define SWR_BROADCAST_CMD_ID 0x0F
Sudheer Papothi3590b312019-06-04 23:51:30 +053035#define SWR_AUTO_SUSPEND_DELAY 1 /* delay in sec */
Sudheer Papothi7c067e82018-11-15 06:53:35 +053036#define SWR_DEV_ID_MASK 0xFFFFFFFFFFFF
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053037#define SWR_REG_VAL_PACK(data, dev, id, reg) \
38 ((reg) | ((id) << 16) | ((dev) << 20) | ((data) << 24))
39
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +053040#define SWR_INVALID_PARAM 0xFF
Laxminath Kasam990c70b2018-11-09 23:15:09 +053041#define SWR_HSTOP_MAX_VAL 0xF
42#define SWR_HSTART_MIN_VAL 0x0
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +053043
Vatsal Buchae50b5002019-09-19 14:32:20 +053044#define ERR_AUTO_SUSPEND_TIMER_VAL 0x1
45
Ramprasad Katkam83303512018-10-11 17:34:22 +053046#define SWRM_INTERRUPT_STATUS_MASK 0x1FDFD
Laxminath Kasame2291972019-11-08 14:51:59 +053047#define SWRM_LINK_STATUS_RETRY_CNT 0x5
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +053048
49#define SWRM_ROW_48 48
50#define SWRM_ROW_50 50
51#define SWRM_ROW_64 64
52#define SWRM_COL_02 02
53#define SWRM_COL_16 16
54
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053055/* pm runtime auto suspend timer in msecs */
56static int auto_suspend_timer = SWR_AUTO_SUSPEND_DELAY * 1000;
57module_param(auto_suspend_timer, int, 0664);
58MODULE_PARM_DESC(auto_suspend_timer, "timer for auto suspend");
59
60enum {
61 SWR_NOT_PRESENT, /* Device is detached/not present on the bus */
62 SWR_ATTACHED_OK, /* Device is attached */
63 SWR_ALERT, /* Device alters master for any interrupts */
64 SWR_RESERVED, /* Reserved */
65};
66
67enum {
68 MASTER_ID_WSA = 1,
69 MASTER_ID_RX,
70 MASTER_ID_TX
71};
Ramprasad Katkamcab8d722018-09-28 15:54:06 +053072
73enum {
74 ENABLE_PENDING,
75 DISABLE_PENDING
76};
Sudheer Papothi384addd2019-06-14 02:26:52 +053077
78enum {
79 LPASS_HW_CORE,
80 LPASS_AUDIO_CORE,
81};
82
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053083#define TRUE 1
84#define FALSE 0
85
Ramprasad Katkam1f221262018-08-23 15:01:22 +053086#define SWRM_MAX_PORT_REG 120
Ramprasad Katkam83303512018-10-11 17:34:22 +053087#define SWRM_MAX_INIT_REG 11
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053088
Laxminath Kasamfbcaf322018-07-18 00:38:14 +053089#define MAX_FIFO_RD_FAIL_RETRY 3
90
Ramprasad Katkam57349872018-11-11 18:34:57 +053091static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm);
92static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm);
Sudheer Papothi96c842a2019-08-29 12:11:21 +053093static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr);
94static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053095
96static bool swrm_is_msm_variant(int val)
97{
98 return (val == SWRM_VERSION_1_3);
99}
100
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530101#ifdef CONFIG_DEBUG_FS
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530102static int swrm_debug_open(struct inode *inode, struct file *file)
103{
104 file->private_data = inode->i_private;
105 return 0;
106}
107
108static int get_parameters(char *buf, u32 *param1, int num_of_par)
109{
110 char *token;
111 int base, cnt;
112
113 token = strsep(&buf, " ");
114 for (cnt = 0; cnt < num_of_par; cnt++) {
115 if (token) {
116 if ((token[1] == 'x') || (token[1] == 'X'))
117 base = 16;
118 else
119 base = 10;
120
121 if (kstrtou32(token, base, &param1[cnt]) != 0)
122 return -EINVAL;
123
124 token = strsep(&buf, " ");
125 } else
126 return -EINVAL;
127 }
128 return 0;
129}
130
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530131static ssize_t swrm_reg_show(struct swr_mstr_ctrl *swrm, char __user *ubuf,
132 size_t count, loff_t *ppos)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530133{
134 int i, reg_val, len;
135 ssize_t total = 0;
136 char tmp_buf[SWR_MSTR_MAX_BUF_LEN];
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530137 int rem = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530138
139 if (!ubuf || !ppos)
140 return 0;
141
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530142 i = ((int) *ppos + SWR_MSTR_START_REG_ADDR);
143 rem = i%4;
144
145 if (rem)
146 i = (i - rem);
147
148 for (; i <= SWR_MSTR_MAX_REG_ADDR; i += 4) {
149 usleep_range(100, 150);
150 reg_val = swr_master_read(swrm, i);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530151 len = snprintf(tmp_buf, 25, "0x%.3x: 0x%.2x\n", i, reg_val);
Aditya Bavanari9f599b42019-08-27 22:18:41 +0530152 if (len < 0) {
153 pr_err("%s: fail to fill the buffer\n", __func__);
154 total = -EFAULT;
155 goto copy_err;
156 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530157 if ((total + len) >= count - 1)
158 break;
159 if (copy_to_user((ubuf + total), tmp_buf, len)) {
160 pr_err("%s: fail to copy reg dump\n", __func__);
161 total = -EFAULT;
162 goto copy_err;
163 }
164 *ppos += len;
165 total += len;
166 }
167
168copy_err:
169 return total;
170}
171
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530172static ssize_t swrm_debug_reg_dump(struct file *file, char __user *ubuf,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530173 size_t count, loff_t *ppos)
174{
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530175 struct swr_mstr_ctrl *swrm;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530176
177 if (!count || !file || !ppos || !ubuf)
178 return -EINVAL;
179
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530180 swrm = file->private_data;
181 if (!swrm)
182 return -EINVAL;
183
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530184 if (*ppos < 0)
185 return -EINVAL;
186
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530187 return swrm_reg_show(swrm, ubuf, count, ppos);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530188}
189
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530190static ssize_t swrm_debug_read(struct file *file, char __user *ubuf,
191 size_t count, loff_t *ppos)
192{
193 char lbuf[SWR_MSTR_RD_BUF_LEN];
194 struct swr_mstr_ctrl *swrm = NULL;
195
196 if (!count || !file || !ppos || !ubuf)
197 return -EINVAL;
198
199 swrm = file->private_data;
200 if (!swrm)
201 return -EINVAL;
202
203 if (*ppos < 0)
204 return -EINVAL;
205
206 snprintf(lbuf, sizeof(lbuf), "0x%x\n", swrm->read_data);
207
208 return simple_read_from_buffer(ubuf, count, ppos, lbuf,
209 strnlen(lbuf, 7));
210}
211
212static ssize_t swrm_debug_peek_write(struct file *file, const char __user *ubuf,
213 size_t count, loff_t *ppos)
214{
215 char lbuf[SWR_MSTR_RD_BUF_LEN];
216 int rc;
217 u32 param[5];
218 struct swr_mstr_ctrl *swrm = NULL;
219
220 if (!count || !file || !ppos || !ubuf)
221 return -EINVAL;
222
223 swrm = file->private_data;
224 if (!swrm)
225 return -EINVAL;
226
227 if (*ppos < 0)
228 return -EINVAL;
229
230 if (count > sizeof(lbuf) - 1)
231 return -EINVAL;
232
233 rc = copy_from_user(lbuf, ubuf, count);
234 if (rc)
235 return -EFAULT;
236
237 lbuf[count] = '\0';
238 rc = get_parameters(lbuf, param, 1);
239 if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) && (rc == 0))
240 swrm->read_data = swr_master_read(swrm, param[0]);
241 else
242 rc = -EINVAL;
243
244 if (rc == 0)
245 rc = count;
246 else
247 dev_err(swrm->dev, "%s: rc = %d\n", __func__, rc);
248
249 return rc;
250}
251
252static ssize_t swrm_debug_write(struct file *file,
253 const char __user *ubuf, size_t count, loff_t *ppos)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530254{
255 char lbuf[SWR_MSTR_WR_BUF_LEN];
256 int rc;
257 u32 param[5];
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530258 struct swr_mstr_ctrl *swrm;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530259
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530260 if (!file || !ppos || !ubuf)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530261 return -EINVAL;
262
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530263 swrm = file->private_data;
264 if (!swrm)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530265 return -EINVAL;
266
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530267 if (count > sizeof(lbuf) - 1)
268 return -EINVAL;
269
270 rc = copy_from_user(lbuf, ubuf, count);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530271 if (rc)
272 return -EFAULT;
273
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530274 lbuf[count] = '\0';
275 rc = get_parameters(lbuf, param, 2);
276 if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) &&
277 (param[1] <= 0xFFFFFFFF) &&
278 (rc == 0))
279 swr_master_write(swrm, param[0], param[1]);
280 else
281 rc = -EINVAL;
282
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530283 if (rc == 0)
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530284 rc = count;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530285 else
286 pr_err("%s: rc = %d\n", __func__, rc);
287
288 return rc;
289}
290
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530291static const struct file_operations swrm_debug_read_ops = {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530292 .open = swrm_debug_open,
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530293 .write = swrm_debug_peek_write,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530294 .read = swrm_debug_read,
295};
296
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530297static const struct file_operations swrm_debug_write_ops = {
298 .open = swrm_debug_open,
299 .write = swrm_debug_write,
300};
301
302static const struct file_operations swrm_debug_dump_ops = {
303 .open = swrm_debug_open,
304 .read = swrm_debug_reg_dump,
305};
306#endif
307
Sudheer Papothi0016db12019-06-11 04:42:38 +0530308static void swrm_reg_dump(struct swr_mstr_ctrl *swrm,
309 u32 *reg, u32 *val, int len, const char* func)
310{
311 int i = 0;
312
313 for (i = 0; i < len; i++)
314 dev_dbg(swrm->dev, "%s: reg = 0x%x val = 0x%x\n",
315 func, reg[i], val[i]);
316}
317
Sudheer Papothi921b8652019-10-03 02:13:32 +0530318static bool is_swr_clk_needed(struct swr_mstr_ctrl *swrm)
319{
320 return ((swrm->version <= SWRM_VERSION_1_5_1) ? true : false);
321}
322
Sudheer Papothi384addd2019-06-14 02:26:52 +0530323static int swrm_request_hw_vote(struct swr_mstr_ctrl *swrm,
324 int core_type, bool enable)
325{
326 int ret = 0;
327
328 if (core_type == LPASS_HW_CORE) {
329 if (swrm->lpass_core_hw_vote) {
330 if (enable) {
331 ret =
332 clk_prepare_enable(swrm->lpass_core_hw_vote);
333 if (ret < 0)
334 dev_err(swrm->dev,
335 "%s:lpass core hw enable failed\n",
336 __func__);
337 } else
338 clk_disable_unprepare(swrm->lpass_core_hw_vote);
339 }
340 }
341 if (core_type == LPASS_AUDIO_CORE) {
342 if (swrm->lpass_core_audio) {
343 if (enable) {
344 ret =
345 clk_prepare_enable(swrm->lpass_core_audio);
346 if (ret < 0)
347 dev_err(swrm->dev,
348 "%s:lpass audio hw enable failed\n",
349 __func__);
350 } else
351 clk_disable_unprepare(swrm->lpass_core_audio);
352 }
353 }
354
355 return ret;
356}
357
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +0530358static int swrm_get_ssp_period(struct swr_mstr_ctrl *swrm,
359 int row, int col,
360 int frame_sync)
361{
362 if (!swrm || !row || !col || !frame_sync)
363 return 1;
364
365 return ((swrm->bus_clk * 2) / ((row * col) * frame_sync));
366}
367
Sudheer Papothi921b8652019-10-03 02:13:32 +0530368static int swrm_core_vote_request(struct swr_mstr_ctrl *swrm)
369{
370 int ret = 0;
371
372 if (!swrm->handle)
373 return -EINVAL;
374
375 mutex_lock(&swrm->clklock);
376 if (!swrm->dev_up) {
377 ret = -ENODEV;
378 goto exit;
379 }
380 if (swrm->core_vote) {
381 ret = swrm->core_vote(swrm->handle, true);
382 if (ret)
383 dev_err_ratelimited(swrm->dev,
384 "%s: core vote request failed\n", __func__);
385 }
386exit:
387 mutex_unlock(&swrm->clklock);
388
389 return ret;
390}
391
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530392static int swrm_clk_request(struct swr_mstr_ctrl *swrm, bool enable)
393{
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530394 int ret = 0;
395
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530396 if (!swrm->clk || !swrm->handle)
397 return -EINVAL;
398
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530399 mutex_lock(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530400 if (enable) {
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530401 if (!swrm->dev_up) {
402 ret = -ENODEV;
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530403 goto exit;
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530404 }
Sudheer Papothi921b8652019-10-03 02:13:32 +0530405 if (is_swr_clk_needed(swrm)) {
406 if (swrm->core_vote) {
407 ret = swrm->core_vote(swrm->handle, true);
408 if (ret) {
409 dev_err_ratelimited(swrm->dev,
410 "%s: core vote request failed\n",
411 __func__);
412 goto exit;
413 }
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -0700414 }
415 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530416 swrm->clk_ref_count++;
417 if (swrm->clk_ref_count == 1) {
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530418 ret = swrm->clk(swrm->handle, true);
419 if (ret) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +0530420 dev_err_ratelimited(swrm->dev,
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530421 "%s: clock enable req failed",
422 __func__);
423 --swrm->clk_ref_count;
424 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530425 }
426 } else if (--swrm->clk_ref_count == 0) {
427 swrm->clk(swrm->handle, false);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530428 complete(&swrm->clk_off_complete);
429 }
430 if (swrm->clk_ref_count < 0) {
Meng Wang8c60bb52019-06-19 15:49:06 +0800431 dev_err(swrm->dev, "%s: swrm clk count mismatch\n", __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530432 swrm->clk_ref_count = 0;
433 }
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530434
435exit:
436 mutex_unlock(&swrm->clklock);
437 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530438}
439
440static int swrm_ahb_write(struct swr_mstr_ctrl *swrm,
441 u16 reg, u32 *value)
442{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530443 u32 temp = (u32)(*value);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530444 int ret = 0;
445
446 mutex_lock(&swrm->devlock);
447 if (!swrm->dev_up)
448 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530449
Sudheer Papothi921b8652019-10-03 02:13:32 +0530450 if (is_swr_clk_needed(swrm)) {
451 ret = swrm_clk_request(swrm, TRUE);
452 if (ret) {
453 dev_err_ratelimited(swrm->dev,
454 "%s: clock request failed\n",
455 __func__);
456 goto err;
457 }
458 } else if (swrm_core_vote_request(swrm)) {
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530459 goto err;
460 }
Sudheer Papothi921b8652019-10-03 02:13:32 +0530461
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530462 iowrite32(temp, swrm->swrm_dig_base + reg);
Sudheer Papothi921b8652019-10-03 02:13:32 +0530463 if (is_swr_clk_needed(swrm))
464 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530465err:
466 mutex_unlock(&swrm->devlock);
467 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530468}
469
470static int swrm_ahb_read(struct swr_mstr_ctrl *swrm,
471 u16 reg, u32 *value)
472{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530473 u32 temp = 0;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530474 int ret = 0;
475
476 mutex_lock(&swrm->devlock);
477 if (!swrm->dev_up)
478 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530479
Sudheer Papothi921b8652019-10-03 02:13:32 +0530480 if (is_swr_clk_needed(swrm)) {
481 ret = swrm_clk_request(swrm, TRUE);
482 if (ret) {
483 dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
484 __func__);
485 goto err;
486 }
487 } else if (swrm_core_vote_request(swrm)) {
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530488 goto err;
489 }
Sudheer Papothi921b8652019-10-03 02:13:32 +0530490
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530491 temp = ioread32(swrm->swrm_dig_base + reg);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530492 *value = temp;
Sudheer Papothi921b8652019-10-03 02:13:32 +0530493 if (is_swr_clk_needed(swrm))
494 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530495err:
496 mutex_unlock(&swrm->devlock);
497 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530498}
499
500static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr)
501{
502 u32 val = 0;
503
504 if (swrm->read)
505 val = swrm->read(swrm->handle, reg_addr);
506 else
507 swrm_ahb_read(swrm, reg_addr, &val);
508 return val;
509}
510
511static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val)
512{
513 if (swrm->write)
514 swrm->write(swrm->handle, reg_addr, val);
515 else
516 swrm_ahb_write(swrm, reg_addr, &val);
517}
518
519static int swr_master_bulk_write(struct swr_mstr_ctrl *swrm, u32 *reg_addr,
520 u32 *val, unsigned int length)
521{
522 int i = 0;
523
524 if (swrm->bulk_write)
525 swrm->bulk_write(swrm->handle, reg_addr, val, length);
526 else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530527 mutex_lock(&swrm->iolock);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530528 for (i = 0; i < length; i++) {
529 /* wait for FIFO WR command to complete to avoid overflow */
Karthikeyan Mani5d52dd82019-08-15 16:58:08 -0700530 /*
531 * Reduce sleep from 100us to 10us to meet KPIs
532 * This still meets the hardware spec
533 */
534 usleep_range(10, 12);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530535 swr_master_write(swrm, reg_addr[i], val[i]);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530536 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530537 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530538 }
539 return 0;
540}
541
Laxminath Kasame2291972019-11-08 14:51:59 +0530542static bool swrm_check_link_status(struct swr_mstr_ctrl *swrm, bool active)
543{
544 int retry = SWRM_LINK_STATUS_RETRY_CNT;
545 int ret = false;
546 int status = active ? 0x1 : 0x0;
Laxminath Kasam09819e92019-11-18 14:38:11 +0530547 int comp_sts = 0x0;
Laxminath Kasame2291972019-11-08 14:51:59 +0530548
549 if ((swrm->version <= SWRM_VERSION_1_5_1))
550 return true;
551
552 do {
Laxminath Kasam09819e92019-11-18 14:38:11 +0530553 comp_sts = swr_master_read(swrm, SWRM_COMP_STATUS) & 0x01;
554 /* check comp status and status requested met */
555 if ((comp_sts && status) || (!comp_sts && !status)) {
Laxminath Kasame2291972019-11-08 14:51:59 +0530556 ret = true;
557 break;
558 }
559 retry--;
560 usleep_range(500, 510);
561 } while (retry);
562
563 if (retry == 0)
564 dev_err(swrm->dev, "%s: link status not %s\n", __func__,
565 active ? "connected" : "disconnected");
566
567 return ret;
568}
569
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530570static bool swrm_is_port_en(struct swr_master *mstr)
571{
572 return !!(mstr->num_port);
573}
574
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530575static void copy_port_tables(struct swr_mstr_ctrl *swrm,
576 struct port_params *params)
577{
578 u8 i;
579 struct port_params *config = params;
580
581 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
582 /* wsa uses single frame structure for all configurations */
583 if (!swrm->mport_cfg[i].port_en)
584 continue;
585 swrm->mport_cfg[i].sinterval = config[i].si;
586 swrm->mport_cfg[i].offset1 = config[i].off1;
587 swrm->mport_cfg[i].offset2 = config[i].off2;
588 swrm->mport_cfg[i].hstart = config[i].hstart;
589 swrm->mport_cfg[i].hstop = config[i].hstop;
590 swrm->mport_cfg[i].blk_pack_mode = config[i].bp_mode;
591 swrm->mport_cfg[i].blk_grp_count = config[i].bgp_ctrl;
592 swrm->mport_cfg[i].word_length = config[i].wd_len;
593 swrm->mport_cfg[i].lane_ctrl = config[i].lane_ctrl;
594 }
595}
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530596static int swrm_get_port_config(struct swr_mstr_ctrl *swrm)
597{
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530598 struct port_params *params;
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530599 u32 usecase = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530600
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530601 /* TODO - Send usecase information to avoid checking for master_id */
602 if (swrm->mport_cfg[SWRM_DSD_PARAMS_PORT].port_en &&
603 (swrm->master_id == MASTER_ID_RX))
604 usecase = 1;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530605
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530606 params = swrm->port_param[usecase];
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530607 copy_port_tables(swrm, params);
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530608
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530609 return 0;
610}
611
612static int swrm_get_master_port(struct swr_mstr_ctrl *swrm, u8 *mstr_port_id,
613 u8 *mstr_ch_mask, u8 mstr_prt_type,
614 u8 slv_port_id)
615{
616 int i, j;
617 *mstr_port_id = 0;
618
619 for (i = 1; i <= swrm->num_ports; i++) {
620 for (j = 0; j < SWR_MAX_CH_PER_PORT; j++) {
621 if (swrm->port_mapping[i][j].port_type == mstr_prt_type)
622 goto found;
623 }
624 }
625found:
626 if (i > swrm->num_ports || j == SWR_MAX_CH_PER_PORT) {
627 dev_err(swrm->dev, "%s: port type not supported by master\n",
628 __func__);
629 return -EINVAL;
630 }
631 /* id 0 corresponds to master port 1 */
632 *mstr_port_id = i - 1;
633 *mstr_ch_mask = swrm->port_mapping[i][j].ch_mask;
634
635 return 0;
636
637}
638
639static u32 swrm_get_packed_reg_val(u8 *cmd_id, u8 cmd_data,
640 u8 dev_addr, u16 reg_addr)
641{
642 u32 val;
643 u8 id = *cmd_id;
644
645 if (id != SWR_BROADCAST_CMD_ID) {
646 if (id < 14)
647 id += 1;
648 else
649 id = 0;
650 *cmd_id = id;
651 }
652 val = SWR_REG_VAL_PACK(cmd_data, dev_addr, id, reg_addr);
653
654 return val;
655}
656
657static int swrm_cmd_fifo_rd_cmd(struct swr_mstr_ctrl *swrm, int *cmd_data,
658 u8 dev_addr, u8 cmd_id, u16 reg_addr,
659 u32 len)
660{
661 u32 val;
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530662 u32 retry_attempt = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530663
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530664 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530665 val = swrm_get_packed_reg_val(&swrm->rcmd_id, len, dev_addr, reg_addr);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530666 if (swrm->read) {
667 /* skip delay if read is handled in platform driver */
668 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
669 } else {
670 /* wait for FIFO RD to complete to avoid overflow */
671 usleep_range(100, 105);
672 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
673 /* wait for FIFO RD CMD complete to avoid overflow */
674 usleep_range(250, 255);
675 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530676retry_read:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530677 *cmd_data = swr_master_read(swrm, SWRM_CMD_FIFO_RD_FIFO_ADDR);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530678 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, rcmd_id: 0x%x, \
679 dev_num: 0x%x, cmd_data: 0x%x\n", __func__, reg_addr,
680 cmd_id, swrm->rcmd_id, dev_addr, *cmd_data);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530681 if ((((*cmd_data) & 0xF00) >> 8) != swrm->rcmd_id) {
682 if (retry_attempt < MAX_FIFO_RD_FAIL_RETRY) {
683 /* wait 500 us before retry on fifo read failure */
684 usleep_range(500, 505);
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +0530685 if (retry_attempt == (MAX_FIFO_RD_FAIL_RETRY - 1)) {
686 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
687 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
688 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530689 retry_attempt++;
690 goto retry_read;
691 } else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530692 dev_err_ratelimited(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, \
693 rcmd_id: 0x%x, dev_num: 0x%x, cmd_data: 0x%x\n",
694 __func__, reg_addr, cmd_id, swrm->rcmd_id,
695 dev_addr, *cmd_data);
696
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530697 dev_err_ratelimited(swrm->dev,
698 "%s: failed to read fifo\n", __func__);
699 }
700 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530701 mutex_unlock(&swrm->iolock);
702
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530703 return 0;
704}
705
706static int swrm_cmd_fifo_wr_cmd(struct swr_mstr_ctrl *swrm, u8 cmd_data,
707 u8 dev_addr, u8 cmd_id, u16 reg_addr)
708{
709 u32 val;
710 int ret = 0;
711
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530712 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530713 if (!cmd_id)
714 val = swrm_get_packed_reg_val(&swrm->wcmd_id, cmd_data,
715 dev_addr, reg_addr);
716 else
717 val = swrm_get_packed_reg_val(&cmd_id, cmd_data,
718 dev_addr, reg_addr);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530719 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x,wcmd_id: 0x%x, \
720 dev_num: 0x%x, cmd_data: 0x%x\n", __func__,
721 reg_addr, cmd_id, swrm->wcmd_id,dev_addr, cmd_data);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +0530722 swr_master_write(swrm, SWRM_CMD_FIFO_WR_CMD, val);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530723 /*
724 * wait for FIFO WR command to complete to avoid overflow
725 * skip delay if write is handled in platform driver.
726 */
727 if(!swrm->write)
Karthikeyan Mani5d52dd82019-08-15 16:58:08 -0700728 usleep_range(150, 155);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530729 if (cmd_id == 0xF) {
730 /*
731 * sleep for 10ms for MSM soundwire variant to allow broadcast
732 * command to complete.
733 */
734 if (swrm_is_msm_variant(swrm->version))
735 usleep_range(10000, 10100);
736 else
737 wait_for_completion_timeout(&swrm->broadcast,
738 (2 * HZ/10));
739 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530740 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530741 return ret;
742}
743
744static int swrm_read(struct swr_master *master, u8 dev_num, u16 reg_addr,
745 void *buf, u32 len)
746{
747 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
748 int ret = 0;
749 int val;
750 u8 *reg_val = (u8 *)buf;
751
752 if (!swrm) {
753 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
754 return -EINVAL;
755 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530756 if (!dev_num) {
757 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
758 return -EINVAL;
759 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530760 mutex_lock(&swrm->devlock);
761 if (!swrm->dev_up) {
762 mutex_unlock(&swrm->devlock);
763 return 0;
764 }
765 mutex_unlock(&swrm->devlock);
766
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530767 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530768 ret = swrm_cmd_fifo_rd_cmd(swrm, &val, dev_num, 0, reg_addr, len);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530769
770 if (!ret)
771 *reg_val = (u8)val;
772
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530773 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530774 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530775 return ret;
776}
777
778static int swrm_write(struct swr_master *master, u8 dev_num, u16 reg_addr,
779 const void *buf)
780{
781 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
782 int ret = 0;
783 u8 reg_val = *(u8 *)buf;
784
785 if (!swrm) {
786 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
787 return -EINVAL;
788 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530789 if (!dev_num) {
790 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
791 return -EINVAL;
792 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530793 mutex_lock(&swrm->devlock);
794 if (!swrm->dev_up) {
795 mutex_unlock(&swrm->devlock);
796 return 0;
797 }
798 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530799
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530800 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530801 ret = swrm_cmd_fifo_wr_cmd(swrm, reg_val, dev_num, 0, reg_addr);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530802
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530803 pm_runtime_put_autosuspend(swrm->dev);
804 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530805 return ret;
806}
807
808static int swrm_bulk_write(struct swr_master *master, u8 dev_num, void *reg,
809 const void *buf, size_t len)
810{
811 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
812 int ret = 0;
813 int i;
814 u32 *val;
815 u32 *swr_fifo_reg;
816
817 if (!swrm || !swrm->handle) {
818 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
819 return -EINVAL;
820 }
821 if (len <= 0)
822 return -EINVAL;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530823 mutex_lock(&swrm->devlock);
824 if (!swrm->dev_up) {
825 mutex_unlock(&swrm->devlock);
826 return 0;
827 }
828 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530829
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530830 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530831 if (dev_num) {
832 swr_fifo_reg = kcalloc(len, sizeof(u32), GFP_KERNEL);
833 if (!swr_fifo_reg) {
834 ret = -ENOMEM;
835 goto err;
836 }
837 val = kcalloc(len, sizeof(u32), GFP_KERNEL);
838 if (!val) {
839 ret = -ENOMEM;
840 goto mem_fail;
841 }
842
843 for (i = 0; i < len; i++) {
844 val[i] = swrm_get_packed_reg_val(&swrm->wcmd_id,
845 ((u8 *)buf)[i],
846 dev_num,
847 ((u16 *)reg)[i]);
848 swr_fifo_reg[i] = SWRM_CMD_FIFO_WR_CMD;
849 }
850 ret = swr_master_bulk_write(swrm, swr_fifo_reg, val, len);
851 if (ret) {
852 dev_err(&master->dev, "%s: bulk write failed\n",
853 __func__);
854 ret = -EINVAL;
855 }
856 } else {
857 dev_err(&master->dev,
858 "%s: No support of Bulk write for master regs\n",
859 __func__);
860 ret = -EINVAL;
861 goto err;
862 }
863 kfree(val);
864mem_fail:
865 kfree(swr_fifo_reg);
866err:
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530867 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530868 pm_runtime_mark_last_busy(swrm->dev);
869 return ret;
870}
871
872static u8 get_inactive_bank_num(struct swr_mstr_ctrl *swrm)
873{
874 return (swr_master_read(swrm, SWRM_MCP_STATUS) &
875 SWRM_MCP_STATUS_BANK_NUM_MASK) ? 0 : 1;
876}
877
878static void enable_bank_switch(struct swr_mstr_ctrl *swrm, u8 bank,
879 u8 row, u8 col)
880{
881 swrm_cmd_fifo_wr_cmd(swrm, ((row << 3) | col), 0xF, 0xF,
882 SWRS_SCP_FRAME_CTRL_BANK(bank));
883}
884
885static struct swr_port_info *swrm_get_port_req(struct swrm_mports *mport,
886 u8 slv_port, u8 dev_num)
887{
888 struct swr_port_info *port_req = NULL;
889
890 list_for_each_entry(port_req, &mport->port_req_list, list) {
891 /* Store dev_id instead of dev_num if enumeration is changed run_time */
892 if ((port_req->slave_port_id == slv_port)
893 && (port_req->dev_num == dev_num))
894 return port_req;
895 }
896 return NULL;
897}
898
899static bool swrm_remove_from_group(struct swr_master *master)
900{
901 struct swr_device *swr_dev;
902 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
903 bool is_removed = false;
904
905 if (!swrm)
906 goto end;
907
908 mutex_lock(&swrm->mlock);
909 if ((swrm->num_rx_chs > 1) &&
910 (swrm->num_rx_chs == swrm->num_cfg_devs)) {
911 list_for_each_entry(swr_dev, &master->devices,
912 dev_list) {
913 swr_dev->group_id = SWR_GROUP_NONE;
914 master->gr_sid = 0;
915 }
916 is_removed = true;
917 }
918 mutex_unlock(&swrm->mlock);
919
920end:
921 return is_removed;
922}
923
924static void swrm_disable_ports(struct swr_master *master,
925 u8 bank)
926{
927 u32 value;
928 struct swr_port_info *port_req;
929 int i;
930 struct swrm_mports *mport;
931 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
932
933 if (!swrm) {
934 pr_err("%s: swrm is null\n", __func__);
935 return;
936 }
937
938 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
939 master->num_port);
940
941
942 for (i = 0; i < SWR_MSTR_PORT_LEN ; i++) {
943
944 mport = &(swrm->mport_cfg[i]);
945 if (!mport->port_en)
946 continue;
947
948 list_for_each_entry(port_req, &mport->port_req_list, list) {
949 /* skip ports with no change req's*/
950 if (port_req->req_ch == port_req->ch_en)
951 continue;
952
953 swrm_cmd_fifo_wr_cmd(swrm, port_req->req_ch,
954 port_req->dev_num, 0x00,
955 SWRS_DP_CHANNEL_ENABLE_BANK(port_req->slave_port_id,
956 bank));
957 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x\n",
958 __func__, i,
959 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)));
960 }
961 value = ((mport->req_ch)
962 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
963 value |= ((mport->offset2)
964 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
965 value |= ((mport->offset1)
966 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
967 value |= mport->sinterval;
968
969 swr_master_write(swrm,
970 SWRM_DP_PORT_CTRL_BANK(i+1, bank),
971 value);
972 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
973 __func__, i,
974 (SWRM_DP_PORT_CTRL_BANK(i+1, bank)), value);
975 }
976}
977
978static void swrm_cleanup_disabled_port_reqs(struct swr_master *master)
979{
980 struct swr_port_info *port_req, *next;
981 int i;
982 struct swrm_mports *mport;
983 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
984
985 if (!swrm) {
986 pr_err("%s: swrm is null\n", __func__);
987 return;
988 }
989 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
990 master->num_port);
991
992 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
993 mport = &(swrm->mport_cfg[i]);
994 list_for_each_entry_safe(port_req, next,
995 &mport->port_req_list, list) {
996 /* skip ports without new ch req */
997 if (port_req->ch_en == port_req->req_ch)
998 continue;
999
1000 /* remove new ch req's*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +05301001 port_req->ch_en = port_req->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301002
1003 /* If no streams enabled on port, remove the port req */
1004 if (port_req->ch_en == 0) {
1005 list_del(&port_req->list);
1006 kfree(port_req);
1007 }
1008 }
1009 /* remove new ch req's on mport*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +05301010 mport->ch_en = mport->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301011
1012 if (!(mport->ch_en)) {
1013 mport->port_en = false;
1014 master->port_en_mask &= ~i;
1015 }
1016 }
1017}
1018static void swrm_copy_data_port_config(struct swr_master *master, u8 bank)
1019{
1020 u32 value, slv_id;
1021 struct swr_port_info *port_req;
1022 int i;
1023 struct swrm_mports *mport;
1024 u32 reg[SWRM_MAX_PORT_REG];
1025 u32 val[SWRM_MAX_PORT_REG];
1026 int len = 0;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301027 u8 hparams;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301028 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1029
1030 if (!swrm) {
1031 pr_err("%s: swrm is null\n", __func__);
1032 return;
1033 }
1034
1035 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
1036 master->num_port);
1037
1038 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
1039 mport = &(swrm->mport_cfg[i]);
1040 if (!mport->port_en)
1041 continue;
1042
1043 list_for_each_entry(port_req, &mport->port_req_list, list) {
1044 slv_id = port_req->slave_port_id;
1045 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1046 val[len++] = SWR_REG_VAL_PACK(port_req->req_ch,
1047 port_req->dev_num, 0x00,
1048 SWRS_DP_CHANNEL_ENABLE_BANK(slv_id,
1049 bank));
1050
1051 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1052 val[len++] = SWR_REG_VAL_PACK(mport->sinterval,
1053 port_req->dev_num, 0x00,
1054 SWRS_DP_SAMPLE_CONTROL_1_BANK(slv_id,
1055 bank));
1056
1057 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1058 val[len++] = SWR_REG_VAL_PACK(mport->offset1,
1059 port_req->dev_num, 0x00,
1060 SWRS_DP_OFFSET_CONTROL_1_BANK(slv_id,
1061 bank));
1062
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301063 if (mport->offset2 != SWR_INVALID_PARAM) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301064 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1065 val[len++] = SWR_REG_VAL_PACK(mport->offset2,
1066 port_req->dev_num, 0x00,
1067 SWRS_DP_OFFSET_CONTROL_2_BANK(
1068 slv_id, bank));
1069 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301070 if (mport->hstart != SWR_INVALID_PARAM
1071 && mport->hstop != SWR_INVALID_PARAM) {
1072 hparams = (mport->hstart << 4) | mport->hstop;
1073
1074 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1075 val[len++] = SWR_REG_VAL_PACK(hparams,
1076 port_req->dev_num, 0x00,
1077 SWRS_DP_HCONTROL_BANK(slv_id,
1078 bank));
1079 }
1080 if (mport->word_length != SWR_INVALID_PARAM) {
1081 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1082 val[len++] =
1083 SWR_REG_VAL_PACK(mport->word_length,
1084 port_req->dev_num, 0x00,
1085 SWRS_DP_BLOCK_CONTROL_1(slv_id));
1086 }
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +05301087 if (mport->blk_pack_mode != SWR_INVALID_PARAM
1088 && swrm->master_id != MASTER_ID_WSA) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301089 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1090 val[len++] =
1091 SWR_REG_VAL_PACK(mport->blk_pack_mode,
1092 port_req->dev_num, 0x00,
1093 SWRS_DP_BLOCK_CONTROL_3_BANK(slv_id,
1094 bank));
1095 }
1096 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
1097 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1098 val[len++] =
1099 SWR_REG_VAL_PACK(mport->blk_grp_count,
1100 port_req->dev_num, 0x00,
1101 SWRS_DP_BLOCK_CONTROL_2_BANK(slv_id,
1102 bank));
1103 }
1104 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
1105 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1106 val[len++] =
1107 SWR_REG_VAL_PACK(mport->lane_ctrl,
1108 port_req->dev_num, 0x00,
1109 SWRS_DP_LANE_CONTROL_BANK(slv_id,
1110 bank));
1111 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301112 port_req->ch_en = port_req->req_ch;
1113 }
1114 value = ((mport->req_ch)
1115 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +05301116
1117 if (mport->offset2 != SWR_INVALID_PARAM)
1118 value |= ((mport->offset2)
1119 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301120 value |= ((mport->offset1)
1121 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
1122 value |= mport->sinterval;
1123
1124
1125 reg[len] = SWRM_DP_PORT_CTRL_BANK(i + 1, bank);
1126 val[len++] = value;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301127 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
1128 __func__, i,
1129 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)), value);
1130
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301131 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
1132 reg[len] = SWRM_DP_PORT_CTRL_2_BANK(i + 1, bank);
1133 val[len++] = mport->lane_ctrl;
1134 }
1135 if (mport->word_length != SWR_INVALID_PARAM) {
1136 reg[len] = SWRM_DP_BLOCK_CTRL_1(i + 1);
1137 val[len++] = mport->word_length;
1138 }
1139
1140 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
1141 reg[len] = SWRM_DP_BLOCK_CTRL2_BANK(i + 1, bank);
1142 val[len++] = mport->blk_grp_count;
1143 }
1144 if (mport->hstart != SWR_INVALID_PARAM
1145 && mport->hstop != SWR_INVALID_PARAM) {
1146 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
Laxminath Kasame30eef72018-11-05 17:40:09 +05301147 hparams = (mport->hstop << 4) | mport->hstart;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301148 val[len++] = hparams;
Laxminath Kasam990c70b2018-11-09 23:15:09 +05301149 } else {
1150 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
1151 hparams = (SWR_HSTOP_MAX_VAL << 4) | SWR_HSTART_MIN_VAL;
1152 val[len++] = hparams;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301153 }
1154 if (mport->blk_pack_mode != SWR_INVALID_PARAM) {
1155 reg[len] = SWRM_DP_BLOCK_CTRL3_BANK(i + 1, bank);
1156 val[len++] = mport->blk_pack_mode;
1157 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301158 mport->ch_en = mport->req_ch;
1159
1160 }
Sudheer Papothi0016db12019-06-11 04:42:38 +05301161 swrm_reg_dump(swrm, reg, val, len, __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301162 swr_master_bulk_write(swrm, reg, val, len);
1163}
1164
1165static void swrm_apply_port_config(struct swr_master *master)
1166{
1167 u8 bank;
1168 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1169
1170 if (!swrm) {
1171 pr_err("%s: Invalid handle to swr controller\n",
1172 __func__);
1173 return;
1174 }
1175
1176 bank = get_inactive_bank_num(swrm);
1177 dev_dbg(swrm->dev, "%s: enter bank: %d master_ports: %d\n",
1178 __func__, bank, master->num_port);
1179
1180
1181 swrm_cmd_fifo_wr_cmd(swrm, 0x01, 0xF, 0x00,
1182 SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(bank));
1183
1184 swrm_copy_data_port_config(master, bank);
1185}
1186
1187static int swrm_slvdev_datapath_control(struct swr_master *master, bool enable)
1188{
1189 u8 bank;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301190 u32 value, n_row, n_col;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301191 u32 row = 0, col = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301192 int ret;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301193 u8 ssp_period = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301194 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1195 int mask = (SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK |
1196 SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK |
1197 SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_BMSK);
1198 u8 inactive_bank;
1199
1200 if (!swrm) {
1201 pr_err("%s: swrm is null\n", __func__);
1202 return -EFAULT;
1203 }
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301204
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301205 mutex_lock(&swrm->mlock);
1206
Ramprasad Katkam979b7c92019-05-17 15:31:21 +05301207 /*
1208 * During disable if master is already down, which implies an ssr/pdr
1209 * scenario, just mark ports as disabled and exit
1210 */
1211 if (swrm->state == SWR_MSTR_SSR && !enable) {
1212 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1213 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1214 __func__);
1215 goto exit;
1216 }
1217 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
1218 swrm_cleanup_disabled_port_reqs(master);
1219 if (!swrm_is_port_en(master)) {
1220 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1221 __func__);
1222 pm_runtime_mark_last_busy(swrm->dev);
1223 pm_runtime_put_autosuspend(swrm->dev);
1224 }
1225 goto exit;
1226 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301227 bank = get_inactive_bank_num(swrm);
1228
1229 if (enable) {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301230 if (!test_bit(ENABLE_PENDING, &swrm->port_req_pending)) {
1231 dev_dbg(swrm->dev, "%s:No pending connect port req\n",
1232 __func__);
1233 goto exit;
1234 }
1235 clear_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301236 ret = swrm_get_port_config(swrm);
1237 if (ret) {
1238 /* cannot accommodate ports */
1239 swrm_cleanup_disabled_port_reqs(master);
1240 mutex_unlock(&swrm->mlock);
1241 return -EINVAL;
1242 }
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301243 swr_master_write(swrm, SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301244 SWRM_INTERRUPT_STATUS_MASK);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301245 /* apply the new port config*/
1246 swrm_apply_port_config(master);
1247 } else {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301248 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1249 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1250 __func__);
1251 goto exit;
1252 }
1253 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301254 swrm_disable_ports(master, bank);
1255 }
1256 dev_dbg(swrm->dev, "%s: enable: %d, cfg_devs: %d\n",
1257 __func__, enable, swrm->num_cfg_devs);
1258
1259 if (enable) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301260 /* set col = 16 */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301261 n_col = SWR_MAX_COL;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301262 col = SWRM_COL_16;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301263 } else {
1264 /*
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301265 * Do not change to col = 2 if there are still active ports
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301266 */
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301267 if (!master->num_port) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301268 n_col = SWR_MIN_COL;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301269 col = SWRM_COL_02;
1270 } else {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301271 n_col = SWR_MAX_COL;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301272 col = SWRM_COL_16;
1273 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301274 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301275 /* Use default 50 * x, frame shape. Change based on mclk */
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301276 if (swrm->mclk_freq == MCLK_FREQ_NATIVE) {
1277 dev_dbg(swrm->dev, "setting 64 x %d frameshape\n",
1278 n_col ? 16 : 2);
1279 n_row = SWR_ROW_64;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301280 row = SWRM_ROW_64;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301281 } else {
1282 dev_dbg(swrm->dev, "setting 50 x %d frameshape\n",
1283 n_col ? 16 : 2);
1284 n_row = SWR_ROW_50;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301285 row = SWRM_ROW_50;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301286 }
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301287 ssp_period = swrm_get_ssp_period(swrm, row, col, SWRM_FRAME_SYNC_SEL);
1288 dev_dbg(swrm->dev, "%s: ssp_period: %d\n", __func__, ssp_period);
1289
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301290 value = swr_master_read(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank));
1291 value &= (~mask);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301292 value |= ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301293 (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301294 ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301295 swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1296
1297 dev_dbg(swrm->dev, "%s: regaddr: 0x%x, value: 0x%x\n", __func__,
1298 SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1299
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301300 enable_bank_switch(swrm, bank, n_row, n_col);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301301 inactive_bank = bank ? 0 : 1;
1302
1303 if (enable)
1304 swrm_copy_data_port_config(master, inactive_bank);
1305 else {
1306 swrm_disable_ports(master, inactive_bank);
1307 swrm_cleanup_disabled_port_reqs(master);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301308 }
1309 if (!swrm_is_port_en(master)) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301310 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1311 __func__);
1312 pm_runtime_mark_last_busy(swrm->dev);
1313 pm_runtime_put_autosuspend(swrm->dev);
1314 }
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301315exit:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301316 mutex_unlock(&swrm->mlock);
1317return 0;
1318}
1319
1320static int swrm_connect_port(struct swr_master *master,
1321 struct swr_params *portinfo)
1322{
1323 int i;
1324 struct swr_port_info *port_req;
1325 int ret = 0;
1326 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1327 struct swrm_mports *mport;
1328 u8 mstr_port_id, mstr_ch_msk;
1329
1330 dev_dbg(&master->dev, "%s: enter\n", __func__);
1331 if (!portinfo)
1332 return -EINVAL;
1333
1334 if (!swrm) {
1335 dev_err(&master->dev,
1336 "%s: Invalid handle to swr controller\n",
1337 __func__);
1338 return -EINVAL;
1339 }
1340
1341 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301342 mutex_lock(&swrm->devlock);
1343 if (!swrm->dev_up) {
1344 mutex_unlock(&swrm->devlock);
1345 mutex_unlock(&swrm->mlock);
1346 return -EINVAL;
1347 }
1348 mutex_unlock(&swrm->devlock);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301349 if (!swrm_is_port_en(master))
1350 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301351
1352 for (i = 0; i < portinfo->num_port; i++) {
1353 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_msk,
1354 portinfo->port_type[i],
1355 portinfo->port_id[i]);
1356 if (ret) {
1357 dev_err(&master->dev,
1358 "%s: mstr portid for slv port %d not found\n",
1359 __func__, portinfo->port_id[i]);
1360 goto port_fail;
1361 }
1362
1363 mport = &(swrm->mport_cfg[mstr_port_id]);
1364 /* get port req */
1365 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1366 portinfo->dev_num);
1367 if (!port_req) {
1368 dev_dbg(&master->dev, "%s: new req:port id %d dev %d\n",
1369 __func__, portinfo->port_id[i],
1370 portinfo->dev_num);
1371 port_req = kzalloc(sizeof(struct swr_port_info),
1372 GFP_KERNEL);
1373 if (!port_req) {
1374 ret = -ENOMEM;
1375 goto mem_fail;
1376 }
1377 port_req->dev_num = portinfo->dev_num;
1378 port_req->slave_port_id = portinfo->port_id[i];
1379 port_req->num_ch = portinfo->num_ch[i];
1380 port_req->ch_rate = portinfo->ch_rate[i];
1381 port_req->ch_en = 0;
1382 port_req->master_port_id = mstr_port_id;
1383 list_add(&port_req->list, &mport->port_req_list);
1384 }
1385 port_req->req_ch |= portinfo->ch_en[i];
1386
1387 dev_dbg(&master->dev,
1388 "%s: mstr port %d, slv port %d ch_rate %d num_ch %d\n",
1389 __func__, port_req->master_port_id,
1390 port_req->slave_port_id, port_req->ch_rate,
1391 port_req->num_ch);
1392 /* Put the port req on master port */
1393 mport = &(swrm->mport_cfg[mstr_port_id]);
1394 mport->port_en = true;
1395 mport->req_ch |= mstr_ch_msk;
1396 master->port_en_mask |= (1 << mstr_port_id);
1397 }
1398 master->num_port += portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301399 set_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301400 swr_port_response(master, portinfo->tid);
1401
1402 mutex_unlock(&swrm->mlock);
1403 return 0;
1404
1405port_fail:
1406mem_fail:
1407 /* cleanup port reqs in error condition */
1408 swrm_cleanup_disabled_port_reqs(master);
1409 mutex_unlock(&swrm->mlock);
1410 return ret;
1411}
1412
1413static int swrm_disconnect_port(struct swr_master *master,
1414 struct swr_params *portinfo)
1415{
1416 int i, ret = 0;
1417 struct swr_port_info *port_req;
1418 struct swrm_mports *mport;
1419 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1420 u8 mstr_port_id, mstr_ch_mask;
1421
1422 if (!swrm) {
1423 dev_err(&master->dev,
1424 "%s: Invalid handle to swr controller\n",
1425 __func__);
1426 return -EINVAL;
1427 }
1428
1429 if (!portinfo) {
1430 dev_err(&master->dev, "%s: portinfo is NULL\n", __func__);
1431 return -EINVAL;
1432 }
1433 mutex_lock(&swrm->mlock);
1434
1435 for (i = 0; i < portinfo->num_port; i++) {
1436
1437 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_mask,
1438 portinfo->port_type[i], portinfo->port_id[i]);
1439 if (ret) {
1440 dev_err(&master->dev,
1441 "%s: mstr portid for slv port %d not found\n",
1442 __func__, portinfo->port_id[i]);
1443 mutex_unlock(&swrm->mlock);
1444 return -EINVAL;
1445 }
1446 mport = &(swrm->mport_cfg[mstr_port_id]);
1447 /* get port req */
1448 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1449 portinfo->dev_num);
1450
1451 if (!port_req) {
1452 dev_err(&master->dev, "%s:port not enabled : port %d\n",
1453 __func__, portinfo->port_id[i]);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05301454 mutex_unlock(&swrm->mlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301455 return -EINVAL;
1456 }
1457 port_req->req_ch &= ~portinfo->ch_en[i];
1458 mport->req_ch &= ~mstr_ch_mask;
1459 }
1460 master->num_port -= portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301461 set_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301462 swr_port_response(master, portinfo->tid);
1463 mutex_unlock(&swrm->mlock);
1464
1465 return 0;
1466}
1467
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301468static int swrm_find_alert_slave(struct swr_mstr_ctrl *swrm,
1469 int status, u8 *devnum)
1470{
1471 int i;
1472 bool found = false;
1473
1474 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1475 if ((status & SWRM_MCP_SLV_STATUS_MASK) == SWR_ALERT) {
1476 *devnum = i;
1477 found = true;
1478 break;
1479 }
1480 status >>= 2;
1481 }
1482 if (found)
1483 return 0;
1484 else
1485 return -EINVAL;
1486}
1487
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301488static void swrm_enable_slave_irq(struct swr_mstr_ctrl *swrm)
1489{
1490 int i;
1491 int status = 0;
1492
1493 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1494 if (!status) {
1495 dev_dbg_ratelimited(swrm->dev, "%s: slaves status is 0x%x\n",
1496 __func__, status);
1497 return;
1498 }
1499 dev_dbg(swrm->dev, "%s: slave status: 0x%x\n", __func__, status);
1500 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1501 if (status & SWRM_MCP_SLV_STATUS_MASK)
1502 swrm_cmd_fifo_wr_cmd(swrm, 0x4, i, 0x0,
1503 SWRS_SCP_INT_STATUS_MASK_1);
1504 status >>= 2;
1505 }
1506}
1507
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301508static int swrm_check_slave_change_status(struct swr_mstr_ctrl *swrm,
1509 int status, u8 *devnum)
1510{
1511 int i;
1512 int new_sts = status;
1513 int ret = SWR_NOT_PRESENT;
1514
1515 if (status != swrm->slave_status) {
1516 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1517 if ((status & SWRM_MCP_SLV_STATUS_MASK) !=
1518 (swrm->slave_status & SWRM_MCP_SLV_STATUS_MASK)) {
1519 ret = (status & SWRM_MCP_SLV_STATUS_MASK);
1520 *devnum = i;
1521 break;
1522 }
1523 status >>= 2;
1524 swrm->slave_status >>= 2;
1525 }
1526 swrm->slave_status = new_sts;
1527 }
1528 return ret;
1529}
1530
1531static irqreturn_t swr_mstr_interrupt(int irq, void *dev)
1532{
1533 struct swr_mstr_ctrl *swrm = dev;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301534 u32 value, intr_sts, intr_sts_masked;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301535 u32 temp = 0;
1536 u32 status, chg_sts, i;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301537 u8 devnum = 0;
1538 int ret = IRQ_HANDLED;
1539 struct swr_device *swr_dev;
1540 struct swr_master *mstr = &swrm->master;
1541
Ramprasad Katkam57349872018-11-11 18:34:57 +05301542 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1543 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1544 return IRQ_NONE;
1545 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301546
1547 mutex_lock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301548 if (swrm_clk_request(swrm, true)) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +05301549 dev_err_ratelimited(swrm->dev, "%s:clk request failed\n",
1550 __func__);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301551 mutex_unlock(&swrm->reslock);
1552 goto exit;
1553 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301554 mutex_unlock(&swrm->reslock);
1555
1556 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301557 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301558handle_irq:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301559 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301560 value = intr_sts_masked & (1 << i);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301561 if (!value)
1562 continue;
1563
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301564 switch (value) {
1565 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1566 dev_dbg(swrm->dev, "Trigger irq to slave device\n");
1567 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301568 ret = swrm_find_alert_slave(swrm, status, &devnum);
1569 if (ret) {
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301570 dev_err_ratelimited(swrm->dev,
1571 "no slave alert found.spurious interrupt\n");
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05301572 break;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301573 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301574 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1575 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1576 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1577 SWRS_SCP_INT_STATUS_CLEAR_1);
1578 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1579 SWRS_SCP_INT_STATUS_CLEAR_1);
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301580
1581
1582 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1583 if (swr_dev->dev_num != devnum)
1584 continue;
1585 if (swr_dev->slave_irq) {
1586 do {
Ramprasad Katkam2586a4b2019-03-18 16:53:39 +05301587 swr_dev->slave_irq_pending = 0;
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301588 handle_nested_irq(
1589 irq_find_mapping(
1590 swr_dev->slave_irq, 0));
1591 } while (swr_dev->slave_irq_pending);
1592 }
1593
1594 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301595 break;
1596 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1597 dev_dbg(swrm->dev, "SWR new slave attached\n");
1598 break;
1599 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1600 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1601 if (status == swrm->slave_status) {
1602 dev_dbg(swrm->dev,
1603 "%s: No change in slave status: %d\n",
1604 __func__, status);
1605 break;
1606 }
1607 chg_sts = swrm_check_slave_change_status(swrm, status,
1608 &devnum);
1609 switch (chg_sts) {
1610 case SWR_NOT_PRESENT:
1611 dev_dbg(swrm->dev, "device %d got detached\n",
1612 devnum);
1613 break;
1614 case SWR_ATTACHED_OK:
1615 dev_dbg(swrm->dev, "device %d got attached\n",
1616 devnum);
Ramprasad Katkamdebe8932018-09-25 18:08:18 +05301617 /* enable host irq from slave device*/
1618 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1619 SWRS_SCP_INT_STATUS_CLEAR_1);
1620 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1621 SWRS_SCP_INT_STATUS_MASK_1);
1622
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301623 break;
1624 case SWR_ALERT:
1625 dev_dbg(swrm->dev,
1626 "device %d has pending interrupt\n",
1627 devnum);
1628 break;
1629 }
1630 break;
1631 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1632 dev_err_ratelimited(swrm->dev,
1633 "SWR bus clsh detected\n");
1634 break;
1635 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1636 dev_dbg(swrm->dev, "SWR read FIFO overflow\n");
1637 break;
1638 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1639 dev_dbg(swrm->dev, "SWR read FIFO underflow\n");
1640 break;
1641 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1642 dev_dbg(swrm->dev, "SWR write FIFO overflow\n");
1643 break;
1644 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1645 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1646 dev_err_ratelimited(swrm->dev,
1647 "SWR CMD error, fifo status 0x%x, flushing fifo\n",
1648 value);
1649 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1650 break;
1651 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301652 dev_err_ratelimited(swrm->dev, "SWR Port collision detected\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301653 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301654 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301655 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301656 break;
1657 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1658 dev_dbg(swrm->dev, "SWR read enable valid mismatch\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301659 swrm->intr_mask &=
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301660 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1661 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301662 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301663 break;
1664 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1665 complete(&swrm->broadcast);
1666 dev_dbg(swrm->dev, "SWR cmd id finished\n");
1667 break;
1668 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_AUTO_ENUM_FINISHED:
1669 break;
1670 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED:
1671 break;
1672 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL:
1673 break;
1674 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED:
1675 complete(&swrm->reset);
1676 break;
1677 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED:
1678 break;
1679 default:
1680 dev_err_ratelimited(swrm->dev,
1681 "SWR unknown interrupt\n");
1682 ret = IRQ_NONE;
1683 break;
1684 }
1685 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301686 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1687 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
Ramprasad Katkam83303512018-10-11 17:34:22 +05301688
1689 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301690 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301691
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301692 if (intr_sts_masked) {
Ramprasad Katkam83303512018-10-11 17:34:22 +05301693 dev_dbg(swrm->dev, "%s: new interrupt received\n", __func__);
1694 goto handle_irq;
1695 }
1696
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301697 mutex_lock(&swrm->reslock);
1698 swrm_clk_request(swrm, false);
1699 mutex_unlock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301700exit:
Ramprasad Katkam57349872018-11-11 18:34:57 +05301701 swrm_unlock_sleep(swrm);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301702 return ret;
1703}
1704
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301705static irqreturn_t swr_mstr_interrupt_v2(int irq, void *dev)
1706{
1707 struct swr_mstr_ctrl *swrm = dev;
1708 u32 value, intr_sts, intr_sts_masked;
1709 u32 temp = 0;
1710 u32 status, chg_sts, i;
1711 u8 devnum = 0;
1712 int ret = IRQ_HANDLED;
1713 struct swr_device *swr_dev;
1714 struct swr_master *mstr = &swrm->master;
1715
1716 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1717 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1718 return IRQ_NONE;
1719 }
1720
1721 mutex_lock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301722 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
1723 ret = IRQ_NONE;
1724 goto exit;
1725 }
1726 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
1727 ret = IRQ_NONE;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301728 goto err_audio_hw_vote;
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001729 }
Karthikeyan Mani4bee1db2019-09-18 17:58:41 -07001730 ret = swrm_clk_request(swrm, true);
1731 if (ret) {
1732 dev_err(dev, "%s: swrm clk failed\n", __func__);
1733 ret = IRQ_NONE;
1734 goto err_audio_core_vote;
1735 }
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301736 mutex_unlock(&swrm->reslock);
1737
1738 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1739 intr_sts_masked = intr_sts & swrm->intr_mask;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301740
1741 dev_dbg(swrm->dev, "%s: status: 0x%x \n", __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301742handle_irq:
1743 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
1744 value = intr_sts_masked & (1 << i);
1745 if (!value)
1746 continue;
1747
1748 switch (value) {
1749 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1750 dev_dbg(swrm->dev, "%s: Trigger irq to slave device\n",
1751 __func__);
1752 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1753 ret = swrm_find_alert_slave(swrm, status, &devnum);
1754 if (ret) {
1755 dev_err_ratelimited(swrm->dev,
1756 "%s: no slave alert found.spurious interrupt\n",
1757 __func__);
1758 break;
1759 }
1760 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1761 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1762 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1763 SWRS_SCP_INT_STATUS_CLEAR_1);
1764 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1765 SWRS_SCP_INT_STATUS_CLEAR_1);
1766
1767
1768 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1769 if (swr_dev->dev_num != devnum)
1770 continue;
1771 if (swr_dev->slave_irq) {
1772 do {
1773 handle_nested_irq(
1774 irq_find_mapping(
1775 swr_dev->slave_irq, 0));
1776 } while (swr_dev->slave_irq_pending);
1777 }
1778
1779 }
1780 break;
1781 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1782 dev_dbg(swrm->dev, "%s: SWR new slave attached\n",
1783 __func__);
1784 break;
1785 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1786 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1787 if (status == swrm->slave_status) {
1788 dev_dbg(swrm->dev,
1789 "%s: No change in slave status: %d\n",
1790 __func__, status);
1791 break;
1792 }
1793 chg_sts = swrm_check_slave_change_status(swrm, status,
1794 &devnum);
1795 switch (chg_sts) {
1796 case SWR_NOT_PRESENT:
1797 dev_dbg(swrm->dev,
1798 "%s: device %d got detached\n",
1799 __func__, devnum);
1800 break;
1801 case SWR_ATTACHED_OK:
1802 dev_dbg(swrm->dev,
1803 "%s: device %d got attached\n",
1804 __func__, devnum);
1805 /* enable host irq from slave device*/
1806 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1807 SWRS_SCP_INT_STATUS_CLEAR_1);
1808 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1809 SWRS_SCP_INT_STATUS_MASK_1);
1810
1811 break;
1812 case SWR_ALERT:
1813 dev_dbg(swrm->dev,
1814 "%s: device %d has pending interrupt\n",
1815 __func__, devnum);
1816 break;
1817 }
1818 break;
1819 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1820 dev_err_ratelimited(swrm->dev,
1821 "%s: SWR bus clsh detected\n",
1822 __func__);
1823 break;
1824 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1825 dev_dbg(swrm->dev, "%s: SWR read FIFO overflow\n",
1826 __func__);
1827 break;
1828 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1829 dev_dbg(swrm->dev, "%s: SWR read FIFO underflow\n",
1830 __func__);
1831 break;
1832 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1833 dev_dbg(swrm->dev, "%s: SWR write FIFO overflow\n",
1834 __func__);
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301835 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301836 break;
1837 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1838 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1839 dev_err_ratelimited(swrm->dev,
1840 "%s: SWR CMD error, fifo status 0x%x, flushing fifo\n",
1841 __func__, value);
1842 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1843 break;
1844 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
1845 dev_err_ratelimited(swrm->dev,
1846 "%s: SWR Port collision detected\n",
1847 __func__);
1848 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
1849 swr_master_write(swrm,
1850 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1851 break;
1852 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1853 dev_dbg(swrm->dev,
1854 "%s: SWR read enable valid mismatch\n",
1855 __func__);
1856 swrm->intr_mask &=
1857 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1858 swr_master_write(swrm,
1859 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1860 break;
1861 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1862 complete(&swrm->broadcast);
1863 dev_dbg(swrm->dev, "%s: SWR cmd id finished\n",
1864 __func__);
1865 break;
1866 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED_V2:
1867 break;
1868 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL_V2:
1869 break;
1870 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED_V2:
Laxminath Kasame2291972019-11-08 14:51:59 +05301871 swrm_check_link_status(swrm, 0x1);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301872 break;
1873 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED_V2:
1874 break;
1875 case SWRM_INTERRUPT_STATUS_EXT_CLK_STOP_WAKEUP:
1876 if (swrm->state == SWR_MSTR_UP)
1877 dev_dbg(swrm->dev,
1878 "%s:SWR Master is already up\n",
1879 __func__);
1880 else
1881 dev_err_ratelimited(swrm->dev,
1882 "%s: SWR wokeup during clock stop\n",
1883 __func__);
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301884 /* It might be possible the slave device gets reset
1885 * and slave interrupt gets missed. So re-enable
1886 * Host IRQ and process slave pending
1887 * interrupts, if any.
1888 */
1889 swrm_enable_slave_irq(swrm);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301890 break;
1891 default:
1892 dev_err_ratelimited(swrm->dev,
1893 "%s: SWR unknown interrupt value: %d\n",
1894 __func__, value);
1895 ret = IRQ_NONE;
1896 break;
1897 }
1898 }
1899 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1900 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
1901
1902 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1903 intr_sts_masked = intr_sts & swrm->intr_mask;
1904
1905 if (intr_sts_masked) {
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301906 dev_dbg(swrm->dev, "%s: new interrupt received 0x%x\n",
1907 __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301908 goto handle_irq;
1909 }
1910
1911 mutex_lock(&swrm->reslock);
1912 swrm_clk_request(swrm, false);
Karthikeyan Mani4bee1db2019-09-18 17:58:41 -07001913err_audio_core_vote:
Sudheer Papothi384addd2019-06-14 02:26:52 +05301914 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
Sudheer Papothi06f43412019-07-09 03:32:54 +05301915
1916err_audio_hw_vote:
Sudheer Papothi384addd2019-06-14 02:26:52 +05301917 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001918exit:
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301919 mutex_unlock(&swrm->reslock);
1920 swrm_unlock_sleep(swrm);
1921 return ret;
1922}
1923
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301924static irqreturn_t swrm_wakeup_interrupt(int irq, void *dev)
1925{
1926 struct swr_mstr_ctrl *swrm = dev;
1927 int ret = IRQ_HANDLED;
1928
1929 if (!swrm || !(swrm->dev)) {
1930 pr_err("%s: swrm or dev is null\n", __func__);
1931 return IRQ_NONE;
1932 }
1933 mutex_lock(&swrm->devlock);
1934 if (!swrm->dev_up) {
1935 if (swrm->wake_irq > 0)
1936 disable_irq_nosync(swrm->wake_irq);
1937 mutex_unlock(&swrm->devlock);
1938 return ret;
1939 }
1940 mutex_unlock(&swrm->devlock);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301941 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1942 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1943 goto exit;
1944 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301945 if (swrm->wake_irq > 0)
1946 disable_irq_nosync(swrm->wake_irq);
1947 pm_runtime_get_sync(swrm->dev);
1948 pm_runtime_mark_last_busy(swrm->dev);
1949 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301950 swrm_unlock_sleep(swrm);
1951exit:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301952 return ret;
1953}
1954
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301955static void swrm_wakeup_work(struct work_struct *work)
1956{
1957 struct swr_mstr_ctrl *swrm;
1958
1959 swrm = container_of(work, struct swr_mstr_ctrl,
1960 wakeup_work);
1961 if (!swrm || !(swrm->dev)) {
1962 pr_err("%s: swrm or dev is null\n", __func__);
1963 return;
1964 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301965
1966 mutex_lock(&swrm->devlock);
1967 if (!swrm->dev_up) {
1968 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301969 goto exit;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301970 }
1971 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301972 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1973 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1974 goto exit;
1975 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301976 pm_runtime_get_sync(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301977 pm_runtime_mark_last_busy(swrm->dev);
1978 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301979 swrm_unlock_sleep(swrm);
1980exit:
1981 pm_relax(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301982}
1983
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301984static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
1985{
1986 u32 val;
1987
1988 swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1989 val = (swrm->slave_status >> (devnum * 2));
1990 val &= SWRM_MCP_SLV_STATUS_MASK;
1991 return val;
1992}
1993
1994static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
1995 u8 *dev_num)
1996{
1997 int i;
1998 u64 id = 0;
1999 int ret = -EINVAL;
2000 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
2001 struct swr_device *swr_dev;
2002 u32 num_dev = 0;
2003
2004 if (!swrm) {
2005 pr_err("%s: Invalid handle to swr controller\n",
2006 __func__);
2007 return ret;
2008 }
2009 if (swrm->num_dev)
2010 num_dev = swrm->num_dev;
2011 else
2012 num_dev = mstr->num_dev;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302013
2014 mutex_lock(&swrm->devlock);
2015 if (!swrm->dev_up) {
2016 mutex_unlock(&swrm->devlock);
2017 return ret;
2018 }
2019 mutex_unlock(&swrm->devlock);
2020
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302021 pm_runtime_get_sync(swrm->dev);
2022 for (i = 1; i < (num_dev + 1); i++) {
2023 id = ((u64)(swr_master_read(swrm,
2024 SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
2025 id |= swr_master_read(swrm,
2026 SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302027
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302028 /*
2029 * As pm_runtime_get_sync() brings all slaves out of reset
2030 * update logical device number for all slaves.
2031 */
2032 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2033 if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
2034 u32 status = swrm_get_device_status(swrm, i);
2035
2036 if ((status == 0x01) || (status == 0x02)) {
2037 swr_dev->dev_num = i;
2038 if ((id & SWR_DEV_ID_MASK) == dev_id) {
2039 *dev_num = i;
2040 ret = 0;
2041 }
2042 dev_dbg(swrm->dev,
2043 "%s: devnum %d is assigned for dev addr %lx\n",
2044 __func__, i, swr_dev->addr);
2045 }
2046 }
2047 }
2048 }
2049 if (ret)
2050 dev_err(swrm->dev, "%s: device 0x%llx is not ready\n",
2051 __func__, dev_id);
2052
2053 pm_runtime_mark_last_busy(swrm->dev);
2054 pm_runtime_put_autosuspend(swrm->dev);
2055 return ret;
2056}
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302057
2058static void swrm_device_wakeup_vote(struct swr_master *mstr)
2059{
2060 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
2061
2062 if (!swrm) {
2063 pr_err("%s: Invalid handle to swr controller\n",
2064 __func__);
2065 return;
2066 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302067 if (unlikely(swrm_lock_sleep(swrm) == false)) {
2068 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
2069 return;
2070 }
Sudheer Papothi384addd2019-06-14 02:26:52 +05302071 if (++swrm->hw_core_clk_en == 1)
2072 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2073 dev_err(swrm->dev, "%s:lpass core hw enable failed\n",
2074 __func__);
2075 --swrm->hw_core_clk_en;
2076 }
2077 if ( ++swrm->aud_core_clk_en == 1)
2078 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2079 dev_err(swrm->dev, "%s:lpass audio hw enable failed\n",
2080 __func__);
2081 --swrm->aud_core_clk_en;
2082 }
2083 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
2084 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302085 pm_runtime_get_sync(swrm->dev);
2086}
2087
2088static void swrm_device_wakeup_unvote(struct swr_master *mstr)
2089{
2090 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
2091
2092 if (!swrm) {
2093 pr_err("%s: Invalid handle to swr controller\n",
2094 __func__);
2095 return;
2096 }
2097 pm_runtime_mark_last_busy(swrm->dev);
2098 pm_runtime_put_autosuspend(swrm->dev);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302099 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
2100 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
2101
2102 --swrm->aud_core_clk_en;
2103 if (swrm->aud_core_clk_en < 0)
2104 swrm->aud_core_clk_en = 0;
2105 else if (swrm->aud_core_clk_en == 0)
2106 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2107
2108 --swrm->hw_core_clk_en;
2109 if (swrm->hw_core_clk_en < 0)
2110 swrm->hw_core_clk_en = 0;
2111 else if (swrm->hw_core_clk_en == 0)
2112 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
2113
Ramprasad Katkam57349872018-11-11 18:34:57 +05302114 swrm_unlock_sleep(swrm);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302115}
2116
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302117static int swrm_master_init(struct swr_mstr_ctrl *swrm)
2118{
2119 int ret = 0;
2120 u32 val;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302121 u8 row_ctrl = SWR_ROW_50;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302122 u8 col_ctrl = SWR_MIN_COL;
2123 u8 ssp_period = 1;
2124 u8 retry_cmd_num = 3;
2125 u32 reg[SWRM_MAX_INIT_REG];
2126 u32 value[SWRM_MAX_INIT_REG];
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302127 u32 temp = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302128 int len = 0;
2129
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302130 ssp_period = swrm_get_ssp_period(swrm, SWRM_ROW_50,
2131 SWRM_COL_02, SWRM_FRAME_SYNC_SEL);
2132 dev_dbg(swrm->dev, "%s: ssp_period: %d\n", __func__, ssp_period);
2133
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302134 /* Clear Rows and Cols */
2135 val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
2136 (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302137 ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302138
2139 reg[len] = SWRM_MCP_FRAME_CTRL_BANK_ADDR(0);
2140 value[len++] = val;
2141
2142 /* Set Auto enumeration flag */
2143 reg[len] = SWRM_ENUMERATOR_CFG_ADDR;
2144 value[len++] = 1;
2145
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302146 /* Configure No pings */
2147 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2148 val &= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK;
2149 val |= (0x1f << SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT);
2150 reg[len] = SWRM_MCP_CFG_ADDR;
2151 value[len++] = val;
2152
2153 /* Configure number of retries of a read/write cmd */
2154 val = (retry_cmd_num << SWRM_CMD_FIFO_CFG_NUM_OF_CMD_RETRY_SHFT);
2155 reg[len] = SWRM_CMD_FIFO_CFG_ADDR;
2156 value[len++] = val;
2157
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302158 reg[len] = SWRM_MCP_BUS_CTRL_ADDR;
2159 value[len++] = 0x2;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302160
Ramprasad Katkam83303512018-10-11 17:34:22 +05302161 /* Set IRQ to PULSE */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302162 reg[len] = SWRM_COMP_CFG_ADDR;
Ramprasad Katkam83303512018-10-11 17:34:22 +05302163 value[len++] = 0x02;
2164
2165 reg[len] = SWRM_COMP_CFG_ADDR;
2166 value[len++] = 0x03;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302167
2168 reg[len] = SWRM_INTERRUPT_CLEAR;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302169 value[len++] = 0xFFFFFFFF;
2170
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302171 swrm->intr_mask = SWRM_INTERRUPT_STATUS_MASK;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302172 /* Mask soundwire interrupts */
2173 reg[len] = SWRM_INTERRUPT_MASK_ADDR;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302174 value[len++] = swrm->intr_mask;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302175
2176 reg[len] = SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302177 value[len++] = swrm->intr_mask;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302178
2179 swr_master_bulk_write(swrm, reg, value, len);
2180
Sudheer Papothi63f48152018-11-15 01:08:03 +05302181 /*
2182 * For SWR master version 1.5.1, continue
2183 * execute on command ignore.
2184 */
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302185 /* Execute it for versions >= 1.5.1 */
2186 if (swrm->version >= SWRM_VERSION_1_5_1)
Sudheer Papothi63f48152018-11-15 01:08:03 +05302187 swr_master_write(swrm, SWRM_CMD_FIFO_CFG_ADDR,
2188 (swr_master_read(swrm,
2189 SWRM_CMD_FIFO_CFG_ADDR) | 0x80000000));
2190
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302191 /* SW workaround to gate hw_ctl for SWR version >=1.6 */
2192 if (swrm->version >= SWRM_VERSION_1_6) {
2193 if (swrm->swrm_hctl_reg) {
2194 temp = ioread32(swrm->swrm_hctl_reg);
2195 temp &= 0xFFFFFFFD;
2196 iowrite32(temp, swrm->swrm_hctl_reg);
2197 }
2198 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302199 return ret;
2200}
2201
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302202static int swrm_event_notify(struct notifier_block *self,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302203 unsigned long action, void *data)
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302204{
2205 struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302206 event_notifier);
2207
2208 if (!swrm || !(swrm->dev)) {
2209 pr_err("%s: swrm or dev is NULL\n", __func__);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302210 return -EINVAL;
2211 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302212 switch (action) {
2213 case MSM_AUD_DC_EVENT:
2214 schedule_work(&(swrm->dc_presence_work));
2215 break;
2216 case SWR_WAKE_IRQ_EVENT:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302217 if (swrm->ipc_wakeup && !swrm->ipc_wakeup_triggered) {
2218 swrm->ipc_wakeup_triggered = true;
Ramprasad Katkam57349872018-11-11 18:34:57 +05302219 pm_stay_awake(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302220 schedule_work(&swrm->wakeup_work);
Ramprasad Katkamcd61c6e2018-09-18 13:22:58 +05302221 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302222 break;
2223 default:
2224 dev_err(swrm->dev, "%s: invalid event type: %lu\n",
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302225 __func__, action);
2226 return -EINVAL;
2227 }
2228
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302229 return 0;
2230}
2231
2232static void swrm_notify_work_fn(struct work_struct *work)
2233{
2234 struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302235 dc_presence_work);
2236
2237 if (!swrm || !swrm->pdev) {
2238 pr_err("%s: swrm or pdev is NULL\n", __func__);
2239 return;
2240 }
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302241 swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL);
2242}
2243
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302244static int swrm_probe(struct platform_device *pdev)
2245{
2246 struct swr_mstr_ctrl *swrm;
2247 struct swr_ctrl_platform_data *pdata;
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302248 u32 i, num_ports, port_num, port_type, ch_mask, swrm_hctl_reg = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302249 u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0;
2250 int ret = 0;
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302251 struct clk *lpass_core_hw_vote = NULL;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302252 struct clk *lpass_core_audio = NULL;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302253
2254 /* Allocate soundwire master driver structure */
2255 swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl),
2256 GFP_KERNEL);
2257 if (!swrm) {
2258 ret = -ENOMEM;
2259 goto err_memory_fail;
2260 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302261 swrm->pdev = pdev;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302262 swrm->dev = &pdev->dev;
2263 platform_set_drvdata(pdev, swrm);
2264 swr_set_ctrl_data(&swrm->master, swrm);
2265 pdata = dev_get_platdata(&pdev->dev);
2266 if (!pdata) {
2267 dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
2268 __func__);
2269 ret = -EINVAL;
2270 goto err_pdata_fail;
2271 }
2272 swrm->handle = (void *)pdata->handle;
2273 if (!swrm->handle) {
2274 dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
2275 __func__);
2276 ret = -EINVAL;
2277 goto err_pdata_fail;
2278 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302279 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr_master_id",
2280 &swrm->master_id);
2281 if (ret) {
2282 dev_err(&pdev->dev, "%s: failed to get master id\n", __func__);
2283 goto err_pdata_fail;
2284 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302285 if (!(of_property_read_u32(pdev->dev.of_node,
2286 "swrm-io-base", &swrm->swrm_base_reg)))
2287 ret = of_property_read_u32(pdev->dev.of_node,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302288 "swrm-io-base", &swrm->swrm_base_reg);
2289 if (!swrm->swrm_base_reg) {
2290 swrm->read = pdata->read;
2291 if (!swrm->read) {
2292 dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
2293 __func__);
2294 ret = -EINVAL;
2295 goto err_pdata_fail;
2296 }
2297 swrm->write = pdata->write;
2298 if (!swrm->write) {
2299 dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
2300 __func__);
2301 ret = -EINVAL;
2302 goto err_pdata_fail;
2303 }
2304 swrm->bulk_write = pdata->bulk_write;
2305 if (!swrm->bulk_write) {
2306 dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
2307 __func__);
2308 ret = -EINVAL;
2309 goto err_pdata_fail;
2310 }
2311 } else {
2312 swrm->swrm_dig_base = devm_ioremap(&pdev->dev,
2313 swrm->swrm_base_reg, SWRM_MAX_REGISTER);
2314 }
2315
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07002316 swrm->core_vote = pdata->core_vote;
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302317 if (!(of_property_read_u32(pdev->dev.of_node,
2318 "qcom,swrm-hctl-reg", &swrm_hctl_reg)))
2319 swrm->swrm_hctl_reg = devm_ioremap(&pdev->dev,
2320 swrm_hctl_reg, 0x4);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302321 swrm->clk = pdata->clk;
2322 if (!swrm->clk) {
2323 dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
2324 __func__);
2325 ret = -EINVAL;
2326 goto err_pdata_fail;
2327 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302328 if (of_property_read_u32(pdev->dev.of_node,
2329 "qcom,swr-clock-stop-mode0",
2330 &swrm->clk_stop_mode0_supp)) {
2331 swrm->clk_stop_mode0_supp = FALSE;
2332 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302333
2334 ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
2335 &swrm->num_dev);
2336 if (ret) {
2337 dev_dbg(&pdev->dev, "%s: Looking up %s property failed\n",
2338 __func__, "qcom,swr-num-dev");
2339 } else {
2340 if (swrm->num_dev > SWR_MAX_SLAVE_DEVICES) {
2341 dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
2342 __func__, swrm->num_dev, SWR_MAX_SLAVE_DEVICES);
2343 ret = -EINVAL;
2344 goto err_pdata_fail;
2345 }
2346 }
2347
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302348 /* Parse soundwire port mapping */
2349 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
2350 &num_ports);
2351 if (ret) {
2352 dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
2353 goto err_pdata_fail;
2354 }
2355 swrm->num_ports = num_ports;
2356
2357 if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
2358 &map_size)) {
2359 dev_err(swrm->dev, "missing port mapping\n");
2360 goto err_pdata_fail;
2361 }
2362
2363 map_length = map_size / (3 * sizeof(u32));
2364 if (num_ports > SWR_MSTR_PORT_LEN) {
2365 dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
2366 __func__);
2367 ret = -EINVAL;
2368 goto err_pdata_fail;
2369 }
2370 temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
2371
2372 if (!temp) {
2373 ret = -ENOMEM;
2374 goto err_pdata_fail;
2375 }
2376 ret = of_property_read_u32_array(pdev->dev.of_node,
2377 "qcom,swr-port-mapping", temp, 3 * map_length);
2378 if (ret) {
2379 dev_err(swrm->dev, "%s: Failed to read port mapping\n",
2380 __func__);
2381 goto err_pdata_fail;
2382 }
2383
2384 for (i = 0; i < map_length; i++) {
2385 port_num = temp[3 * i];
2386 port_type = temp[3 * i + 1];
2387 ch_mask = temp[3 * i + 2];
2388
2389 if (port_num != old_port_num)
2390 ch_iter = 0;
2391 swrm->port_mapping[port_num][ch_iter].port_type = port_type;
2392 swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
2393 old_port_num = port_num;
2394 }
2395 devm_kfree(&pdev->dev, temp);
2396
2397 swrm->reg_irq = pdata->reg_irq;
2398 swrm->master.read = swrm_read;
2399 swrm->master.write = swrm_write;
2400 swrm->master.bulk_write = swrm_bulk_write;
2401 swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
2402 swrm->master.connect_port = swrm_connect_port;
2403 swrm->master.disconnect_port = swrm_disconnect_port;
2404 swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
2405 swrm->master.remove_from_group = swrm_remove_from_group;
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302406 swrm->master.device_wakeup_vote = swrm_device_wakeup_vote;
2407 swrm->master.device_wakeup_unvote = swrm_device_wakeup_unvote;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302408 swrm->master.dev.parent = &pdev->dev;
2409 swrm->master.dev.of_node = pdev->dev.of_node;
2410 swrm->master.num_port = 0;
2411 swrm->rcmd_id = 0;
2412 swrm->wcmd_id = 0;
2413 swrm->slave_status = 0;
2414 swrm->num_rx_chs = 0;
2415 swrm->clk_ref_count = 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302416 swrm->swr_irq_wakeup_capable = 0;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302417 swrm->mclk_freq = MCLK_FREQ;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302418 swrm->bus_clk = MCLK_FREQ;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302419 swrm->dev_up = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302420 swrm->state = SWR_MSTR_UP;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302421 swrm->ipc_wakeup = false;
2422 swrm->ipc_wakeup_triggered = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302423 init_completion(&swrm->reset);
2424 init_completion(&swrm->broadcast);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302425 init_completion(&swrm->clk_off_complete);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302426 mutex_init(&swrm->mlock);
2427 mutex_init(&swrm->reslock);
2428 mutex_init(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302429 mutex_init(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302430 mutex_init(&swrm->clklock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302431 mutex_init(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302432 mutex_init(&swrm->pm_lock);
2433 swrm->wlock_holders = 0;
2434 swrm->pm_state = SWRM_PM_SLEEPABLE;
2435 init_waitqueue_head(&swrm->pm_wq);
2436 pm_qos_add_request(&swrm->pm_qos_req,
2437 PM_QOS_CPU_DMA_LATENCY,
2438 PM_QOS_DEFAULT_VALUE);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302439
2440 for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++)
2441 INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
2442
Sudheer Papothi06f43412019-07-09 03:32:54 +05302443 /* Register LPASS core hw vote */
2444 lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
2445 if (IS_ERR(lpass_core_hw_vote)) {
2446 ret = PTR_ERR(lpass_core_hw_vote);
2447 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2448 __func__, "lpass_core_hw_vote", ret);
2449 lpass_core_hw_vote = NULL;
2450 ret = 0;
2451 }
2452 swrm->lpass_core_hw_vote = lpass_core_hw_vote;
2453
2454 /* Register LPASS audio core vote */
2455 lpass_core_audio = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
2456 if (IS_ERR(lpass_core_audio)) {
2457 ret = PTR_ERR(lpass_core_audio);
2458 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2459 __func__, "lpass_core_audio", ret);
2460 lpass_core_audio = NULL;
2461 ret = 0;
2462 }
2463 swrm->lpass_core_audio = lpass_core_audio;
2464
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302465 if (swrm->reg_irq) {
2466 ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
2467 SWR_IRQ_REGISTER);
2468 if (ret) {
2469 dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
2470 __func__, ret);
2471 goto err_irq_fail;
2472 }
2473 } else {
2474 swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
2475 if (swrm->irq < 0) {
2476 dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
2477 __func__, swrm->irq);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302478 goto err_irq_fail;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302479 }
2480
2481 ret = request_threaded_irq(swrm->irq, NULL,
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302482 swr_mstr_interrupt_v2,
Ramprasad Katkam83303512018-10-11 17:34:22 +05302483 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302484 "swr_master_irq", swrm);
2485 if (ret) {
2486 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2487 __func__, ret);
2488 goto err_irq_fail;
2489 }
2490
2491 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302492 /* Make inband tx interrupts as wakeup capable for slave irq */
2493 ret = of_property_read_u32(pdev->dev.of_node,
2494 "qcom,swr-mstr-irq-wakeup-capable",
2495 &swrm->swr_irq_wakeup_capable);
2496 if (ret)
2497 dev_dbg(swrm->dev, "%s: swrm irq wakeup capable not defined\n",
2498 __func__);
2499 if (swrm->swr_irq_wakeup_capable)
2500 irq_set_irq_wake(swrm->irq, 1);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302501 ret = swr_register_master(&swrm->master);
2502 if (ret) {
2503 dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
2504 goto err_mstr_fail;
2505 }
2506
2507 /* Add devices registered with board-info as the
2508 * controller will be up now
2509 */
2510 swr_master_add_boarddevices(&swrm->master);
2511 mutex_lock(&swrm->mlock);
2512 swrm_clk_request(swrm, true);
2513 ret = swrm_master_init(swrm);
2514 if (ret < 0) {
2515 dev_err(&pdev->dev,
2516 "%s: Error in master Initialization , err %d\n",
2517 __func__, ret);
2518 mutex_unlock(&swrm->mlock);
2519 goto err_mstr_fail;
2520 }
2521 swrm->version = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
2522
2523 mutex_unlock(&swrm->mlock);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302524 INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302525
2526 if (pdev->dev.of_node)
2527 of_register_swr_devices(&swrm->master);
2528
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302529#ifdef CONFIG_DEBUG_FS
2530 swrm->debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
2531 if (!IS_ERR(swrm->debugfs_swrm_dent)) {
2532 swrm->debugfs_peek = debugfs_create_file("swrm_peek",
2533 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2534 (void *) swrm, &swrm_debug_read_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302535
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302536 swrm->debugfs_poke = debugfs_create_file("swrm_poke",
2537 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2538 (void *) swrm, &swrm_debug_write_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302539
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302540 swrm->debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
2541 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2542 (void *) swrm,
2543 &swrm_debug_dump_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302544 }
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302545#endif
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302546 ret = device_init_wakeup(swrm->dev, true);
2547 if (ret) {
2548 dev_err(swrm->dev, "Device wakeup init failed: %d\n", ret);
2549 goto err_irq_wakeup_fail;
2550 }
2551
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302552 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
2553 pm_runtime_use_autosuspend(&pdev->dev);
2554 pm_runtime_set_active(&pdev->dev);
2555 pm_runtime_enable(&pdev->dev);
2556 pm_runtime_mark_last_busy(&pdev->dev);
2557
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302558 INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn);
2559 swrm->event_notifier.notifier_call = swrm_event_notify;
2560 msm_aud_evt_register_client(&swrm->event_notifier);
2561
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302562 return 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302563err_irq_wakeup_fail:
2564 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302565err_mstr_fail:
2566 if (swrm->reg_irq)
2567 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2568 swrm, SWR_IRQ_FREE);
2569 else if (swrm->irq)
2570 free_irq(swrm->irq, swrm);
2571err_irq_fail:
2572 mutex_destroy(&swrm->mlock);
2573 mutex_destroy(&swrm->reslock);
2574 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302575 mutex_destroy(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302576 mutex_destroy(&swrm->clklock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302577 mutex_destroy(&swrm->pm_lock);
2578 pm_qos_remove_request(&swrm->pm_qos_req);
2579
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302580err_pdata_fail:
2581err_memory_fail:
2582 return ret;
2583}
2584
2585static int swrm_remove(struct platform_device *pdev)
2586{
2587 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2588
2589 if (swrm->reg_irq)
2590 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2591 swrm, SWR_IRQ_FREE);
2592 else if (swrm->irq)
2593 free_irq(swrm->irq, swrm);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302594 else if (swrm->wake_irq > 0)
2595 free_irq(swrm->wake_irq, swrm);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302596 if (swrm->swr_irq_wakeup_capable)
2597 irq_set_irq_wake(swrm->irq, 0);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302598 cancel_work_sync(&swrm->wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302599 pm_runtime_disable(&pdev->dev);
2600 pm_runtime_set_suspended(&pdev->dev);
2601 swr_unregister_master(&swrm->master);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302602 msm_aud_evt_unregister_client(&swrm->event_notifier);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302603 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302604 mutex_destroy(&swrm->mlock);
2605 mutex_destroy(&swrm->reslock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302606 mutex_destroy(&swrm->iolock);
2607 mutex_destroy(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302608 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302609 mutex_destroy(&swrm->pm_lock);
2610 pm_qos_remove_request(&swrm->pm_qos_req);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302611 devm_kfree(&pdev->dev, swrm);
2612 return 0;
2613}
2614
2615static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
2616{
2617 u32 val;
2618
2619 dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
2620 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD);
2621 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2622 val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK;
2623 swr_master_write(swrm, SWRM_MCP_CFG_ADDR, val);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302624
2625 return 0;
2626}
2627
2628#ifdef CONFIG_PM
2629static int swrm_runtime_resume(struct device *dev)
2630{
2631 struct platform_device *pdev = to_platform_device(dev);
2632 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2633 int ret = 0;
Vatsal Buchae50b5002019-09-19 14:32:20 +05302634 bool swrm_clk_req_err = false;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302635 bool hw_core_err = false;
2636 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302637 struct swr_master *mstr = &swrm->master;
2638 struct swr_device *swr_dev;
2639
2640 dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
2641 __func__, swrm->state);
2642 mutex_lock(&swrm->reslock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302643
Sudheer Papothi384addd2019-06-14 02:26:52 +05302644 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2645 dev_err(dev, "%s:lpass core hw enable failed\n",
2646 __func__);
2647 hw_core_err = true;
2648 }
2649 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2650 dev_err(dev, "%s:lpass audio hw enable failed\n",
2651 __func__);
2652 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002653 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302654
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302655 if ((swrm->state == SWR_MSTR_DOWN) ||
2656 (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) {
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302657 if (swrm->clk_stop_mode0_supp) {
2658 if (swrm->ipc_wakeup)
2659 msm_aud_evt_blocking_notifier_call_chain(
2660 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302661 }
2662
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302663 if (swrm_clk_request(swrm, true)) {
2664 /*
2665 * Set autosuspend timer to 1 for
2666 * master to enter into suspend.
2667 */
Vatsal Buchae50b5002019-09-19 14:32:20 +05302668 swrm_clk_req_err = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302669 goto exit;
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302670 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302671 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302672 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2673 ret = swr_device_up(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302674 if (ret == -ENODEV) {
2675 dev_dbg(dev,
2676 "%s slave device up not implemented\n",
2677 __func__);
2678 ret = 0;
2679 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302680 dev_err(dev,
2681 "%s: failed to wakeup swr dev %d\n",
2682 __func__, swr_dev->dev_num);
2683 swrm_clk_request(swrm, false);
2684 goto exit;
2685 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302686 }
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05302687 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2688 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2689 swrm_master_init(swrm);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302690 /* wait for hw enumeration to complete */
2691 usleep_range(100, 105);
Laxminath Kasame2291972019-11-08 14:51:59 +05302692 if (!swrm_check_link_status(swrm, 0x1))
2693 goto exit;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302694 swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, 0x0,
2695 SWRS_SCP_INT_STATUS_MASK_1);
Karthikeyan Manif6821902019-05-21 17:31:24 -07002696 if (swrm->state == SWR_MSTR_SSR) {
2697 mutex_unlock(&swrm->reslock);
2698 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
2699 mutex_lock(&swrm->reslock);
2700 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302701 } else {
2702 /*wake up from clock stop*/
2703 swr_master_write(swrm, SWRM_MCP_BUS_CTRL_ADDR, 0x2);
2704 usleep_range(100, 105);
Laxminath Kasame2291972019-11-08 14:51:59 +05302705 if (!swrm_check_link_status(swrm, 0x1))
2706 goto exit;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302707 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302708 swrm->state = SWR_MSTR_UP;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302709 }
2710exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302711 if (!aud_core_err)
2712 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2713 if (!hw_core_err)
2714 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Vatsal Buchae50b5002019-09-19 14:32:20 +05302715 if (swrm_clk_req_err)
2716 pm_runtime_set_autosuspend_delay(&pdev->dev,
2717 ERR_AUTO_SUSPEND_TIMER_VAL);
2718 else
2719 pm_runtime_set_autosuspend_delay(&pdev->dev,
2720 auto_suspend_timer);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302721 mutex_unlock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302722
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302723 return ret;
2724}
2725
2726static int swrm_runtime_suspend(struct device *dev)
2727{
2728 struct platform_device *pdev = to_platform_device(dev);
2729 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2730 int ret = 0;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302731 bool hw_core_err = false;
2732 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302733 struct swr_master *mstr = &swrm->master;
2734 struct swr_device *swr_dev;
2735 int current_state = 0;
2736
2737 dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
2738 __func__, swrm->state);
2739 mutex_lock(&swrm->reslock);
2740 mutex_lock(&swrm->force_down_lock);
2741 current_state = swrm->state;
2742 mutex_unlock(&swrm->force_down_lock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302743
2744 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2745 dev_err(dev, "%s:lpass core hw enable failed\n",
2746 __func__);
2747 hw_core_err = true;
2748 }
2749 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2750 dev_err(dev, "%s:lpass audio hw enable failed\n",
2751 __func__);
2752 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002753 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302754
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302755 if ((current_state == SWR_MSTR_UP) ||
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302756 (current_state == SWR_MSTR_SSR)) {
2757
2758 if ((current_state != SWR_MSTR_SSR) &&
2759 swrm_is_port_en(&swrm->master)) {
2760 dev_dbg(dev, "%s ports are enabled\n", __func__);
2761 ret = -EBUSY;
2762 goto exit;
2763 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302764 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05302765 mutex_unlock(&swrm->reslock);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +05302766 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
Sudheer Papothi06f43412019-07-09 03:32:54 +05302767 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302768 swrm_clk_pause(swrm);
2769 swr_master_write(swrm, SWRM_COMP_CFG_ADDR, 0x00);
2770 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2771 ret = swr_device_down(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302772 if (ret == -ENODEV) {
2773 dev_dbg_ratelimited(dev,
2774 "%s slave device down not implemented\n",
2775 __func__);
2776 ret = 0;
2777 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302778 dev_err(dev,
2779 "%s: failed to shutdown swr dev %d\n",
2780 __func__, swr_dev->dev_num);
2781 goto exit;
2782 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302783 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302784 } else {
Sudheer Papothi384addd2019-06-14 02:26:52 +05302785 mutex_unlock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302786 /* clock stop sequence */
2787 swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
2788 SWRS_SCP_CONTROL);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302789 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302790 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302791 }
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07002792 ret = swrm_clk_request(swrm, false);
2793 if (ret) {
2794 dev_err(dev, "%s: swrmn clk failed\n", __func__);
2795 ret = 0;
2796 goto exit;
2797 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302798
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302799 if (swrm->clk_stop_mode0_supp) {
2800 if (swrm->wake_irq > 0) {
2801 enable_irq(swrm->wake_irq);
2802 } else if (swrm->ipc_wakeup) {
2803 msm_aud_evt_blocking_notifier_call_chain(
2804 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
2805 swrm->ipc_wakeup_triggered = false;
2806 }
Laxminath Kasame2291972019-11-08 14:51:59 +05302807 if (!swrm_check_link_status(swrm, 0x0))
2808 goto exit;
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302809 }
2810
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302811 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302812 /* Retain SSR state until resume */
2813 if (current_state != SWR_MSTR_SSR)
2814 swrm->state = SWR_MSTR_DOWN;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302815exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302816 if (!aud_core_err)
2817 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2818 if (!hw_core_err)
2819 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302820 mutex_unlock(&swrm->reslock);
2821 return ret;
2822}
2823#endif /* CONFIG_PM */
2824
Sudheer Papothi06f43412019-07-09 03:32:54 +05302825static int swrm_device_suspend(struct device *dev)
2826{
2827 struct platform_device *pdev = to_platform_device(dev);
2828 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2829 int ret = 0;
2830
2831 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2832 if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
2833 ret = swrm_runtime_suspend(dev);
2834 if (!ret) {
2835 pm_runtime_disable(dev);
2836 pm_runtime_set_suspended(dev);
2837 pm_runtime_enable(dev);
2838 }
2839 }
2840
2841 return 0;
2842}
2843
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302844static int swrm_device_down(struct device *dev)
2845{
2846 struct platform_device *pdev = to_platform_device(dev);
2847 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302848
2849 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2850
2851 mutex_lock(&swrm->force_down_lock);
2852 swrm->state = SWR_MSTR_SSR;
2853 mutex_unlock(&swrm->force_down_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302854
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302855 swrm_device_suspend(dev);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302856 return 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302857}
2858
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302859int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm)
2860{
2861 int ret = 0;
Laxminath Kasama60239e2019-01-10 14:43:03 +05302862 int irq, dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302863
2864 if (!swrm->ipc_wakeup) {
Laxminath Kasama60239e2019-01-10 14:43:03 +05302865 irq = of_get_named_gpio(swrm->dev->of_node,
2866 "qcom,swr-wakeup-irq", 0);
2867 if (gpio_is_valid(irq)) {
2868 swrm->wake_irq = gpio_to_irq(irq);
2869 if (swrm->wake_irq < 0) {
2870 dev_err(swrm->dev,
2871 "Unable to configure irq\n");
2872 return swrm->wake_irq;
2873 }
2874 } else {
2875 dir_apps_irq = platform_get_irq_byname(swrm->pdev,
2876 "swr_wake_irq");
2877 if (dir_apps_irq < 0) {
2878 dev_err(swrm->dev,
2879 "TLMM connect gpio not found\n");
2880 return -EINVAL;
2881 }
2882 swrm->wake_irq = dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302883 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302884 ret = request_threaded_irq(swrm->wake_irq, NULL,
2885 swrm_wakeup_interrupt,
2886 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
2887 "swr_wake_irq", swrm);
2888 if (ret) {
2889 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2890 __func__, ret);
2891 return -EINVAL;
2892 }
Aditya Bavanari3517b112018-12-03 13:26:59 +05302893 irq_set_irq_wake(swrm->wake_irq, 1);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302894 }
2895 return ret;
2896}
2897
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302898static int swrm_alloc_port_mem(struct device *dev, struct swr_mstr_ctrl *swrm,
2899 u32 uc, u32 size)
2900{
2901 if (!swrm->port_param) {
2902 swrm->port_param = devm_kzalloc(dev,
2903 sizeof(swrm->port_param) * SWR_UC_MAX,
2904 GFP_KERNEL);
2905 if (!swrm->port_param)
2906 return -ENOMEM;
2907 }
2908 if (!swrm->port_param[uc]) {
2909 swrm->port_param[uc] = devm_kcalloc(dev, size,
2910 sizeof(struct port_params),
2911 GFP_KERNEL);
2912 if (!swrm->port_param[uc])
2913 return -ENOMEM;
2914 } else {
2915 dev_err_ratelimited(swrm->dev, "%s: called more than once\n",
2916 __func__);
2917 }
2918
2919 return 0;
2920}
2921
2922static int swrm_copy_port_config(struct swr_mstr_ctrl *swrm,
2923 struct swrm_port_config *port_cfg,
2924 u32 size)
2925{
2926 int idx;
2927 struct port_params *params;
2928 int uc = port_cfg->uc;
2929 int ret = 0;
2930
2931 for (idx = 0; idx < size; idx++) {
2932 params = &((struct port_params *)port_cfg->params)[idx];
2933 if (!params) {
2934 dev_err(swrm->dev, "%s: Invalid params\n", __func__);
2935 ret = -EINVAL;
2936 break;
2937 }
2938 memcpy(&swrm->port_param[uc][idx], params,
2939 sizeof(struct port_params));
2940 }
2941
2942 return ret;
2943}
2944
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302945/**
2946 * swrm_wcd_notify - parent device can notify to soundwire master through
2947 * this function
2948 * @pdev: pointer to platform device structure
2949 * @id: command id from parent to the soundwire master
2950 * @data: data from parent device to soundwire master
2951 */
2952int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
2953{
2954 struct swr_mstr_ctrl *swrm;
2955 int ret = 0;
2956 struct swr_master *mstr;
2957 struct swr_device *swr_dev;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302958 struct swrm_port_config *port_cfg;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302959
2960 if (!pdev) {
2961 pr_err("%s: pdev is NULL\n", __func__);
2962 return -EINVAL;
2963 }
2964 swrm = platform_get_drvdata(pdev);
2965 if (!swrm) {
2966 dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__);
2967 return -EINVAL;
2968 }
2969 mstr = &swrm->master;
2970
2971 switch (id) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05302972 case SWR_REQ_CLK_SWITCH:
2973 /* This will put soundwire in clock stop mode and disable the
2974 * clocks, if there is no active usecase running, so that the
2975 * next activity on soundwire will request clock from new clock
2976 * source.
2977 */
2978 mutex_lock(&swrm->mlock);
2979 if (swrm->state == SWR_MSTR_UP)
2980 swrm_device_suspend(&pdev->dev);
2981 mutex_unlock(&swrm->mlock);
2982 break;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302983 case SWR_CLK_FREQ:
2984 if (!data) {
2985 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
2986 ret = -EINVAL;
2987 } else {
2988 mutex_lock(&swrm->mlock);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302989 if (swrm->mclk_freq != *(int *)data) {
2990 dev_dbg(swrm->dev, "%s: freq change: force mstr down\n", __func__);
2991 if (swrm->state == SWR_MSTR_DOWN)
2992 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
2993 __func__, swrm->state);
2994 else
2995 swrm_device_suspend(&pdev->dev);
Prasad Kumpatla386df4e2019-10-11 18:23:16 +05302996 /*
2997 * add delay to ensure clk release happen
2998 * if interrupt triggered for clk stop,
2999 * wait for it to exit
3000 */
3001 usleep_range(10000, 10500);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05303002 }
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05303003 swrm->mclk_freq = *(int *)data;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05303004 swrm->bus_clk = swrm->mclk_freq;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05303005 mutex_unlock(&swrm->mlock);
3006 }
3007 break;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303008 case SWR_DEVICE_SSR_DOWN:
3009 mutex_lock(&swrm->devlock);
3010 swrm->dev_up = false;
3011 mutex_unlock(&swrm->devlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05303012 mutex_lock(&swrm->reslock);
3013 swrm->state = SWR_MSTR_SSR;
3014 mutex_unlock(&swrm->reslock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303015 break;
3016 case SWR_DEVICE_SSR_UP:
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05303017 /* wait for clk voting to be zero */
Ramprasad Katkam7f6462e2018-11-06 11:51:22 +05303018 reinit_completion(&swrm->clk_off_complete);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05303019 if (swrm->clk_ref_count &&
3020 !wait_for_completion_timeout(&swrm->clk_off_complete,
Ramprasad Katkamc87efeb2018-12-12 19:26:19 +05303021 msecs_to_jiffies(500)))
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05303022 dev_err(swrm->dev, "%s: clock voting not zero\n",
3023 __func__);
3024
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303025 mutex_lock(&swrm->devlock);
3026 swrm->dev_up = true;
3027 mutex_unlock(&swrm->devlock);
3028 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303029 case SWR_DEVICE_DOWN:
3030 dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
3031 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05303032 if (swrm->state == SWR_MSTR_DOWN)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303033 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
3034 __func__, swrm->state);
3035 else
3036 swrm_device_down(&pdev->dev);
3037 mutex_unlock(&swrm->mlock);
3038 break;
3039 case SWR_DEVICE_UP:
3040 dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
Ramprasad Katkam0fed92f2018-11-08 14:22:22 +05303041 mutex_lock(&swrm->devlock);
3042 if (!swrm->dev_up) {
3043 dev_dbg(swrm->dev, "SSR not complete yet\n");
3044 mutex_unlock(&swrm->devlock);
3045 return -EBUSY;
3046 }
3047 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303048 mutex_lock(&swrm->mlock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303049 pm_runtime_mark_last_busy(&pdev->dev);
3050 pm_runtime_get_sync(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303051 mutex_lock(&swrm->reslock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303052 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
3053 ret = swr_reset_device(swr_dev);
3054 if (ret) {
3055 dev_err(swrm->dev,
3056 "%s: failed to reset swr device %d\n",
3057 __func__, swr_dev->dev_num);
3058 swrm_clk_request(swrm, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303059 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303060 }
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303061 pm_runtime_mark_last_busy(&pdev->dev);
3062 pm_runtime_put_autosuspend(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303063 mutex_unlock(&swrm->reslock);
3064 mutex_unlock(&swrm->mlock);
3065 break;
3066 case SWR_SET_NUM_RX_CH:
3067 if (!data) {
3068 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
3069 ret = -EINVAL;
3070 } else {
3071 mutex_lock(&swrm->mlock);
3072 swrm->num_rx_chs = *(int *)data;
3073 if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
3074 list_for_each_entry(swr_dev, &mstr->devices,
3075 dev_list) {
3076 ret = swr_set_device_group(swr_dev,
3077 SWR_BROADCAST);
3078 if (ret)
3079 dev_err(swrm->dev,
3080 "%s: set num ch failed\n",
3081 __func__);
3082 }
3083 } else {
3084 list_for_each_entry(swr_dev, &mstr->devices,
3085 dev_list) {
3086 ret = swr_set_device_group(swr_dev,
3087 SWR_GROUP_NONE);
3088 if (ret)
3089 dev_err(swrm->dev,
3090 "%s: set num ch failed\n",
3091 __func__);
3092 }
3093 }
3094 mutex_unlock(&swrm->mlock);
3095 }
3096 break;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303097 case SWR_REGISTER_WAKE_IRQ:
3098 if (!data) {
3099 dev_err(swrm->dev, "%s: reg wake irq data is NULL\n",
3100 __func__);
3101 ret = -EINVAL;
3102 } else {
3103 mutex_lock(&swrm->mlock);
3104 swrm->ipc_wakeup = *(u32 *)data;
3105 ret = swrm_register_wake_irq(swrm);
3106 if (ret)
3107 dev_err(swrm->dev, "%s: register wake_irq failed\n",
3108 __func__);
3109 mutex_unlock(&swrm->mlock);
3110 }
3111 break;
Sudheer Papothi72ee2642019-08-08 05:15:17 +05303112 case SWR_REGISTER_WAKEUP:
3113 msm_aud_evt_blocking_notifier_call_chain(
3114 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
3115 break;
3116 case SWR_DEREGISTER_WAKEUP:
3117 msm_aud_evt_blocking_notifier_call_chain(
3118 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
3119 break;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05303120 case SWR_SET_PORT_MAP:
3121 if (!data) {
3122 dev_err(swrm->dev, "%s: data is NULL for id=%d\n",
3123 __func__, id);
3124 ret = -EINVAL;
3125 } else {
3126 mutex_lock(&swrm->mlock);
3127 port_cfg = (struct swrm_port_config *)data;
3128 if (!port_cfg->size) {
3129 ret = -EINVAL;
3130 goto done;
3131 }
3132 ret = swrm_alloc_port_mem(&pdev->dev, swrm,
3133 port_cfg->uc, port_cfg->size);
3134 if (!ret)
3135 swrm_copy_port_config(swrm, port_cfg,
3136 port_cfg->size);
3137done:
3138 mutex_unlock(&swrm->mlock);
3139 }
3140 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303141 default:
3142 dev_err(swrm->dev, "%s: swr master unknown id %d\n",
3143 __func__, id);
3144 break;
3145 }
3146 return ret;
3147}
3148EXPORT_SYMBOL(swrm_wcd_notify);
3149
Ramprasad Katkam57349872018-11-11 18:34:57 +05303150/*
3151 * swrm_pm_cmpxchg:
3152 * Check old state and exchange with pm new state
3153 * if old state matches with current state
3154 *
3155 * @swrm: pointer to wcd core resource
3156 * @o: pm old state
3157 * @n: pm new state
3158 *
3159 * Returns old state
3160 */
3161static enum swrm_pm_state swrm_pm_cmpxchg(
3162 struct swr_mstr_ctrl *swrm,
3163 enum swrm_pm_state o,
3164 enum swrm_pm_state n)
3165{
3166 enum swrm_pm_state old;
3167
3168 if (!swrm)
3169 return o;
3170
3171 mutex_lock(&swrm->pm_lock);
3172 old = swrm->pm_state;
3173 if (old == o)
3174 swrm->pm_state = n;
3175 mutex_unlock(&swrm->pm_lock);
3176
3177 return old;
3178}
3179
3180static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm)
3181{
3182 enum swrm_pm_state os;
3183
3184 /*
3185 * swrm_{lock/unlock}_sleep will be called by swr irq handler
3186 * and slave wake up requests..
3187 *
3188 * If system didn't resume, we can simply return false so
3189 * IRQ handler can return without handling IRQ.
3190 */
3191 mutex_lock(&swrm->pm_lock);
3192 if (swrm->wlock_holders++ == 0) {
3193 dev_dbg(swrm->dev, "%s: holding wake lock\n", __func__);
3194 pm_qos_update_request(&swrm->pm_qos_req,
3195 msm_cpuidle_get_deep_idle_latency());
3196 pm_stay_awake(swrm->dev);
3197 }
3198 mutex_unlock(&swrm->pm_lock);
3199
3200 if (!wait_event_timeout(swrm->pm_wq,
3201 ((os = swrm_pm_cmpxchg(swrm,
3202 SWRM_PM_SLEEPABLE,
3203 SWRM_PM_AWAKE)) ==
3204 SWRM_PM_SLEEPABLE ||
3205 (os == SWRM_PM_AWAKE)),
3206 msecs_to_jiffies(
3207 SWRM_SYSTEM_RESUME_TIMEOUT_MS))) {
3208 dev_err(swrm->dev, "%s: system didn't resume within %dms, s %d, w %d\n",
3209 __func__, SWRM_SYSTEM_RESUME_TIMEOUT_MS, swrm->pm_state,
3210 swrm->wlock_holders);
3211 swrm_unlock_sleep(swrm);
3212 return false;
3213 }
3214 wake_up_all(&swrm->pm_wq);
3215 return true;
3216}
3217
3218static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm)
3219{
3220 mutex_lock(&swrm->pm_lock);
3221 if (--swrm->wlock_holders == 0) {
3222 dev_dbg(swrm->dev, "%s: releasing wake lock pm_state %d -> %d\n",
3223 __func__, swrm->pm_state, SWRM_PM_SLEEPABLE);
3224 /*
3225 * if swrm_lock_sleep failed, pm_state would be still
3226 * swrm_PM_ASLEEP, don't overwrite
3227 */
3228 if (likely(swrm->pm_state == SWRM_PM_AWAKE))
3229 swrm->pm_state = SWRM_PM_SLEEPABLE;
3230 pm_qos_update_request(&swrm->pm_qos_req,
3231 PM_QOS_DEFAULT_VALUE);
3232 pm_relax(swrm->dev);
3233 }
3234 mutex_unlock(&swrm->pm_lock);
3235 wake_up_all(&swrm->pm_wq);
3236}
3237
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303238#ifdef CONFIG_PM_SLEEP
3239static int swrm_suspend(struct device *dev)
3240{
3241 int ret = -EBUSY;
3242 struct platform_device *pdev = to_platform_device(dev);
3243 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3244
3245 dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
Ramprasad Katkam57349872018-11-11 18:34:57 +05303246
3247 mutex_lock(&swrm->pm_lock);
3248
3249 if (swrm->pm_state == SWRM_PM_SLEEPABLE) {
3250 dev_dbg(swrm->dev, "%s: suspending system, state %d, wlock %d\n",
3251 __func__, swrm->pm_state,
3252 swrm->wlock_holders);
3253 swrm->pm_state = SWRM_PM_ASLEEP;
3254 } else if (swrm->pm_state == SWRM_PM_AWAKE) {
3255 /*
3256 * unlock to wait for pm_state == SWRM_PM_SLEEPABLE
3257 * then set to SWRM_PM_ASLEEP
3258 */
3259 dev_dbg(swrm->dev, "%s: waiting to suspend system, state %d, wlock %d\n",
3260 __func__, swrm->pm_state,
3261 swrm->wlock_holders);
3262 mutex_unlock(&swrm->pm_lock);
3263 if (!(wait_event_timeout(swrm->pm_wq, swrm_pm_cmpxchg(
3264 swrm, SWRM_PM_SLEEPABLE,
3265 SWRM_PM_ASLEEP) ==
3266 SWRM_PM_SLEEPABLE,
3267 msecs_to_jiffies(
3268 SWRM_SYS_SUSPEND_WAIT)))) {
3269 dev_dbg(swrm->dev, "%s: suspend failed state %d, wlock %d\n",
3270 __func__, swrm->pm_state,
3271 swrm->wlock_holders);
3272 return -EBUSY;
3273 } else {
3274 dev_dbg(swrm->dev,
3275 "%s: done, state %d, wlock %d\n",
3276 __func__, swrm->pm_state,
3277 swrm->wlock_holders);
3278 }
3279 mutex_lock(&swrm->pm_lock);
3280 } else if (swrm->pm_state == SWRM_PM_ASLEEP) {
3281 dev_dbg(swrm->dev, "%s: system is already suspended, state %d, wlock %d\n",
3282 __func__, swrm->pm_state,
3283 swrm->wlock_holders);
3284 }
3285
3286 mutex_unlock(&swrm->pm_lock);
3287
3288 if ((!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev))) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303289 ret = swrm_runtime_suspend(dev);
3290 if (!ret) {
3291 /*
3292 * Synchronize runtime-pm and system-pm states:
3293 * At this point, we are already suspended. If
3294 * runtime-pm still thinks its active, then
3295 * make sure its status is in sync with HW
3296 * status. The three below calls let the
3297 * runtime-pm know that we are suspended
3298 * already without re-invoking the suspend
3299 * callback
3300 */
3301 pm_runtime_disable(dev);
3302 pm_runtime_set_suspended(dev);
3303 pm_runtime_enable(dev);
3304 }
3305 }
3306 if (ret == -EBUSY) {
3307 /*
3308 * There is a possibility that some audio stream is active
3309 * during suspend. We dont want to return suspend failure in
3310 * that case so that display and relevant components can still
3311 * go to suspend.
3312 * If there is some other error, then it should be passed-on
3313 * to system level suspend
3314 */
3315 ret = 0;
3316 }
3317 return ret;
3318}
3319
3320static int swrm_resume(struct device *dev)
3321{
3322 int ret = 0;
3323 struct platform_device *pdev = to_platform_device(dev);
3324 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3325
3326 dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
3327 if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
3328 ret = swrm_runtime_resume(dev);
3329 if (!ret) {
3330 pm_runtime_mark_last_busy(dev);
3331 pm_request_autosuspend(dev);
3332 }
3333 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05303334 mutex_lock(&swrm->pm_lock);
3335 if (swrm->pm_state == SWRM_PM_ASLEEP) {
3336 dev_dbg(swrm->dev,
3337 "%s: resuming system, state %d, wlock %d\n",
3338 __func__, swrm->pm_state,
3339 swrm->wlock_holders);
3340 swrm->pm_state = SWRM_PM_SLEEPABLE;
3341 } else {
3342 dev_dbg(swrm->dev, "%s: system is already awake, state %d wlock %d\n",
3343 __func__, swrm->pm_state,
3344 swrm->wlock_holders);
3345 }
3346 mutex_unlock(&swrm->pm_lock);
3347 wake_up_all(&swrm->pm_wq);
3348
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303349 return ret;
3350}
3351#endif /* CONFIG_PM_SLEEP */
3352
3353static const struct dev_pm_ops swrm_dev_pm_ops = {
3354 SET_SYSTEM_SLEEP_PM_OPS(
3355 swrm_suspend,
3356 swrm_resume
3357 )
3358 SET_RUNTIME_PM_OPS(
3359 swrm_runtime_suspend,
3360 swrm_runtime_resume,
3361 NULL
3362 )
3363};
3364
3365static const struct of_device_id swrm_dt_match[] = {
3366 {
3367 .compatible = "qcom,swr-mstr",
3368 },
3369 {}
3370};
3371
3372static struct platform_driver swr_mstr_driver = {
3373 .probe = swrm_probe,
3374 .remove = swrm_remove,
3375 .driver = {
3376 .name = SWR_WCD_NAME,
3377 .owner = THIS_MODULE,
3378 .pm = &swrm_dev_pm_ops,
3379 .of_match_table = swrm_dt_match,
Xiaojun Sang53cd13a2018-06-29 15:14:37 +08003380 .suppress_bind_attrs = true,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303381 },
3382};
3383
3384static int __init swrm_init(void)
3385{
3386 return platform_driver_register(&swr_mstr_driver);
3387}
3388module_init(swrm_init);
3389
3390static void __exit swrm_exit(void)
3391{
3392 platform_driver_unregister(&swr_mstr_driver);
3393}
3394module_exit(swrm_exit);
3395
3396MODULE_LICENSE("GPL v2");
3397MODULE_DESCRIPTION("SoundWire Master Controller");
3398MODULE_ALIAS("platform:swr-mstr");