blob: 88ee4832c8154cadf3607ada25bb622208d4c501 [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
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +053047
48#define SWRM_ROW_48 48
49#define SWRM_ROW_50 50
50#define SWRM_ROW_64 64
51#define SWRM_COL_02 02
52#define SWRM_COL_16 16
53
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053054/* pm runtime auto suspend timer in msecs */
55static int auto_suspend_timer = SWR_AUTO_SUSPEND_DELAY * 1000;
56module_param(auto_suspend_timer, int, 0664);
57MODULE_PARM_DESC(auto_suspend_timer, "timer for auto suspend");
58
59enum {
60 SWR_NOT_PRESENT, /* Device is detached/not present on the bus */
61 SWR_ATTACHED_OK, /* Device is attached */
62 SWR_ALERT, /* Device alters master for any interrupts */
63 SWR_RESERVED, /* Reserved */
64};
65
66enum {
67 MASTER_ID_WSA = 1,
68 MASTER_ID_RX,
69 MASTER_ID_TX
70};
Ramprasad Katkamcab8d722018-09-28 15:54:06 +053071
72enum {
73 ENABLE_PENDING,
74 DISABLE_PENDING
75};
Sudheer Papothi384addd2019-06-14 02:26:52 +053076
77enum {
78 LPASS_HW_CORE,
79 LPASS_AUDIO_CORE,
80};
81
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053082#define TRUE 1
83#define FALSE 0
84
Ramprasad Katkam1f221262018-08-23 15:01:22 +053085#define SWRM_MAX_PORT_REG 120
Ramprasad Katkam83303512018-10-11 17:34:22 +053086#define SWRM_MAX_INIT_REG 11
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053087
Laxminath Kasamfbcaf322018-07-18 00:38:14 +053088#define MAX_FIFO_RD_FAIL_RETRY 3
89
Ramprasad Katkam57349872018-11-11 18:34:57 +053090static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm);
91static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm);
Sudheer Papothi96c842a2019-08-29 12:11:21 +053092static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr);
93static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053094
95static bool swrm_is_msm_variant(int val)
96{
97 return (val == SWRM_VERSION_1_3);
98}
99
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530100#ifdef CONFIG_DEBUG_FS
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530101static int swrm_debug_open(struct inode *inode, struct file *file)
102{
103 file->private_data = inode->i_private;
104 return 0;
105}
106
107static int get_parameters(char *buf, u32 *param1, int num_of_par)
108{
109 char *token;
110 int base, cnt;
111
112 token = strsep(&buf, " ");
113 for (cnt = 0; cnt < num_of_par; cnt++) {
114 if (token) {
115 if ((token[1] == 'x') || (token[1] == 'X'))
116 base = 16;
117 else
118 base = 10;
119
120 if (kstrtou32(token, base, &param1[cnt]) != 0)
121 return -EINVAL;
122
123 token = strsep(&buf, " ");
124 } else
125 return -EINVAL;
126 }
127 return 0;
128}
129
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530130static ssize_t swrm_reg_show(struct swr_mstr_ctrl *swrm, char __user *ubuf,
131 size_t count, loff_t *ppos)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530132{
133 int i, reg_val, len;
134 ssize_t total = 0;
135 char tmp_buf[SWR_MSTR_MAX_BUF_LEN];
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530136 int rem = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530137
138 if (!ubuf || !ppos)
139 return 0;
140
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530141 i = ((int) *ppos + SWR_MSTR_START_REG_ADDR);
142 rem = i%4;
143
144 if (rem)
145 i = (i - rem);
146
147 for (; i <= SWR_MSTR_MAX_REG_ADDR; i += 4) {
148 usleep_range(100, 150);
149 reg_val = swr_master_read(swrm, i);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530150 len = snprintf(tmp_buf, 25, "0x%.3x: 0x%.2x\n", i, reg_val);
Aditya Bavanari9f599b42019-08-27 22:18:41 +0530151 if (len < 0) {
152 pr_err("%s: fail to fill the buffer\n", __func__);
153 total = -EFAULT;
154 goto copy_err;
155 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530156 if ((total + len) >= count - 1)
157 break;
158 if (copy_to_user((ubuf + total), tmp_buf, len)) {
159 pr_err("%s: fail to copy reg dump\n", __func__);
160 total = -EFAULT;
161 goto copy_err;
162 }
163 *ppos += len;
164 total += len;
165 }
166
167copy_err:
168 return total;
169}
170
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530171static ssize_t swrm_debug_reg_dump(struct file *file, char __user *ubuf,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530172 size_t count, loff_t *ppos)
173{
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530174 struct swr_mstr_ctrl *swrm;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530175
176 if (!count || !file || !ppos || !ubuf)
177 return -EINVAL;
178
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530179 swrm = file->private_data;
180 if (!swrm)
181 return -EINVAL;
182
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530183 if (*ppos < 0)
184 return -EINVAL;
185
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530186 return swrm_reg_show(swrm, ubuf, count, ppos);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530187}
188
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530189static ssize_t swrm_debug_read(struct file *file, char __user *ubuf,
190 size_t count, loff_t *ppos)
191{
192 char lbuf[SWR_MSTR_RD_BUF_LEN];
193 struct swr_mstr_ctrl *swrm = NULL;
194
195 if (!count || !file || !ppos || !ubuf)
196 return -EINVAL;
197
198 swrm = file->private_data;
199 if (!swrm)
200 return -EINVAL;
201
202 if (*ppos < 0)
203 return -EINVAL;
204
205 snprintf(lbuf, sizeof(lbuf), "0x%x\n", swrm->read_data);
206
207 return simple_read_from_buffer(ubuf, count, ppos, lbuf,
208 strnlen(lbuf, 7));
209}
210
211static ssize_t swrm_debug_peek_write(struct file *file, const char __user *ubuf,
212 size_t count, loff_t *ppos)
213{
214 char lbuf[SWR_MSTR_RD_BUF_LEN];
215 int rc;
216 u32 param[5];
217 struct swr_mstr_ctrl *swrm = NULL;
218
219 if (!count || !file || !ppos || !ubuf)
220 return -EINVAL;
221
222 swrm = file->private_data;
223 if (!swrm)
224 return -EINVAL;
225
226 if (*ppos < 0)
227 return -EINVAL;
228
229 if (count > sizeof(lbuf) - 1)
230 return -EINVAL;
231
232 rc = copy_from_user(lbuf, ubuf, count);
233 if (rc)
234 return -EFAULT;
235
236 lbuf[count] = '\0';
237 rc = get_parameters(lbuf, param, 1);
238 if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) && (rc == 0))
239 swrm->read_data = swr_master_read(swrm, param[0]);
240 else
241 rc = -EINVAL;
242
243 if (rc == 0)
244 rc = count;
245 else
246 dev_err(swrm->dev, "%s: rc = %d\n", __func__, rc);
247
248 return rc;
249}
250
251static ssize_t swrm_debug_write(struct file *file,
252 const char __user *ubuf, size_t count, loff_t *ppos)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530253{
254 char lbuf[SWR_MSTR_WR_BUF_LEN];
255 int rc;
256 u32 param[5];
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530257 struct swr_mstr_ctrl *swrm;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530258
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530259 if (!file || !ppos || !ubuf)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530260 return -EINVAL;
261
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530262 swrm = file->private_data;
263 if (!swrm)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530264 return -EINVAL;
265
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530266 if (count > sizeof(lbuf) - 1)
267 return -EINVAL;
268
269 rc = copy_from_user(lbuf, ubuf, count);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530270 if (rc)
271 return -EFAULT;
272
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530273 lbuf[count] = '\0';
274 rc = get_parameters(lbuf, param, 2);
275 if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) &&
276 (param[1] <= 0xFFFFFFFF) &&
277 (rc == 0))
278 swr_master_write(swrm, param[0], param[1]);
279 else
280 rc = -EINVAL;
281
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530282 if (rc == 0)
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530283 rc = count;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530284 else
285 pr_err("%s: rc = %d\n", __func__, rc);
286
287 return rc;
288}
289
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530290static const struct file_operations swrm_debug_read_ops = {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530291 .open = swrm_debug_open,
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530292 .write = swrm_debug_peek_write,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530293 .read = swrm_debug_read,
294};
295
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530296static const struct file_operations swrm_debug_write_ops = {
297 .open = swrm_debug_open,
298 .write = swrm_debug_write,
299};
300
301static const struct file_operations swrm_debug_dump_ops = {
302 .open = swrm_debug_open,
303 .read = swrm_debug_reg_dump,
304};
305#endif
306
Sudheer Papothi0016db12019-06-11 04:42:38 +0530307static void swrm_reg_dump(struct swr_mstr_ctrl *swrm,
308 u32 *reg, u32 *val, int len, const char* func)
309{
310 int i = 0;
311
312 for (i = 0; i < len; i++)
313 dev_dbg(swrm->dev, "%s: reg = 0x%x val = 0x%x\n",
314 func, reg[i], val[i]);
315}
316
Sudheer Papothi921b8652019-10-03 02:13:32 +0530317static bool is_swr_clk_needed(struct swr_mstr_ctrl *swrm)
318{
319 return ((swrm->version <= SWRM_VERSION_1_5_1) ? true : false);
320}
321
Sudheer Papothi384addd2019-06-14 02:26:52 +0530322static int swrm_request_hw_vote(struct swr_mstr_ctrl *swrm,
323 int core_type, bool enable)
324{
325 int ret = 0;
326
327 if (core_type == LPASS_HW_CORE) {
328 if (swrm->lpass_core_hw_vote) {
329 if (enable) {
330 ret =
331 clk_prepare_enable(swrm->lpass_core_hw_vote);
332 if (ret < 0)
333 dev_err(swrm->dev,
334 "%s:lpass core hw enable failed\n",
335 __func__);
336 } else
337 clk_disable_unprepare(swrm->lpass_core_hw_vote);
338 }
339 }
340 if (core_type == LPASS_AUDIO_CORE) {
341 if (swrm->lpass_core_audio) {
342 if (enable) {
343 ret =
344 clk_prepare_enable(swrm->lpass_core_audio);
345 if (ret < 0)
346 dev_err(swrm->dev,
347 "%s:lpass audio hw enable failed\n",
348 __func__);
349 } else
350 clk_disable_unprepare(swrm->lpass_core_audio);
351 }
352 }
353
354 return ret;
355}
356
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +0530357static int swrm_get_ssp_period(struct swr_mstr_ctrl *swrm,
358 int row, int col,
359 int frame_sync)
360{
361 if (!swrm || !row || !col || !frame_sync)
362 return 1;
363
364 return ((swrm->bus_clk * 2) / ((row * col) * frame_sync));
365}
366
Sudheer Papothi921b8652019-10-03 02:13:32 +0530367static int swrm_core_vote_request(struct swr_mstr_ctrl *swrm)
368{
369 int ret = 0;
370
371 if (!swrm->handle)
372 return -EINVAL;
373
374 mutex_lock(&swrm->clklock);
375 if (!swrm->dev_up) {
376 ret = -ENODEV;
377 goto exit;
378 }
379 if (swrm->core_vote) {
380 ret = swrm->core_vote(swrm->handle, true);
381 if (ret)
382 dev_err_ratelimited(swrm->dev,
383 "%s: core vote request failed\n", __func__);
384 }
385exit:
386 mutex_unlock(&swrm->clklock);
387
388 return ret;
389}
390
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530391static int swrm_clk_request(struct swr_mstr_ctrl *swrm, bool enable)
392{
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530393 int ret = 0;
394
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530395 if (!swrm->clk || !swrm->handle)
396 return -EINVAL;
397
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530398 mutex_lock(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530399 if (enable) {
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530400 if (!swrm->dev_up) {
401 ret = -ENODEV;
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530402 goto exit;
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530403 }
Sudheer Papothi921b8652019-10-03 02:13:32 +0530404 if (is_swr_clk_needed(swrm)) {
405 if (swrm->core_vote) {
406 ret = swrm->core_vote(swrm->handle, true);
407 if (ret) {
408 dev_err_ratelimited(swrm->dev,
409 "%s: core vote request failed\n",
410 __func__);
411 goto exit;
412 }
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -0700413 }
414 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530415 swrm->clk_ref_count++;
416 if (swrm->clk_ref_count == 1) {
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530417 ret = swrm->clk(swrm->handle, true);
418 if (ret) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +0530419 dev_err_ratelimited(swrm->dev,
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530420 "%s: clock enable req failed",
421 __func__);
422 --swrm->clk_ref_count;
423 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530424 }
425 } else if (--swrm->clk_ref_count == 0) {
426 swrm->clk(swrm->handle, false);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530427 complete(&swrm->clk_off_complete);
428 }
429 if (swrm->clk_ref_count < 0) {
Meng Wang8c60bb52019-06-19 15:49:06 +0800430 dev_err(swrm->dev, "%s: swrm clk count mismatch\n", __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530431 swrm->clk_ref_count = 0;
432 }
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530433
434exit:
435 mutex_unlock(&swrm->clklock);
436 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530437}
438
439static int swrm_ahb_write(struct swr_mstr_ctrl *swrm,
440 u16 reg, u32 *value)
441{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530442 u32 temp = (u32)(*value);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530443 int ret = 0;
444
445 mutex_lock(&swrm->devlock);
446 if (!swrm->dev_up)
447 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530448
Sudheer Papothi921b8652019-10-03 02:13:32 +0530449 if (is_swr_clk_needed(swrm)) {
450 ret = swrm_clk_request(swrm, TRUE);
451 if (ret) {
452 dev_err_ratelimited(swrm->dev,
453 "%s: clock request failed\n",
454 __func__);
455 goto err;
456 }
457 } else if (swrm_core_vote_request(swrm)) {
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530458 goto err;
459 }
Sudheer Papothi921b8652019-10-03 02:13:32 +0530460
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530461 iowrite32(temp, swrm->swrm_dig_base + reg);
Sudheer Papothi921b8652019-10-03 02:13:32 +0530462 if (is_swr_clk_needed(swrm))
463 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530464err:
465 mutex_unlock(&swrm->devlock);
466 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530467}
468
469static int swrm_ahb_read(struct swr_mstr_ctrl *swrm,
470 u16 reg, u32 *value)
471{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530472 u32 temp = 0;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530473 int ret = 0;
474
475 mutex_lock(&swrm->devlock);
476 if (!swrm->dev_up)
477 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530478
Sudheer Papothi921b8652019-10-03 02:13:32 +0530479 if (is_swr_clk_needed(swrm)) {
480 ret = swrm_clk_request(swrm, TRUE);
481 if (ret) {
482 dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
483 __func__);
484 goto err;
485 }
486 } else if (swrm_core_vote_request(swrm)) {
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530487 goto err;
488 }
Sudheer Papothi921b8652019-10-03 02:13:32 +0530489
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530490 temp = ioread32(swrm->swrm_dig_base + reg);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530491 *value = temp;
Sudheer Papothi921b8652019-10-03 02:13:32 +0530492 if (is_swr_clk_needed(swrm))
493 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530494err:
495 mutex_unlock(&swrm->devlock);
496 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530497}
498
499static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr)
500{
501 u32 val = 0;
502
503 if (swrm->read)
504 val = swrm->read(swrm->handle, reg_addr);
505 else
506 swrm_ahb_read(swrm, reg_addr, &val);
507 return val;
508}
509
510static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val)
511{
512 if (swrm->write)
513 swrm->write(swrm->handle, reg_addr, val);
514 else
515 swrm_ahb_write(swrm, reg_addr, &val);
516}
517
518static int swr_master_bulk_write(struct swr_mstr_ctrl *swrm, u32 *reg_addr,
519 u32 *val, unsigned int length)
520{
521 int i = 0;
522
523 if (swrm->bulk_write)
524 swrm->bulk_write(swrm->handle, reg_addr, val, length);
525 else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530526 mutex_lock(&swrm->iolock);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530527 for (i = 0; i < length; i++) {
528 /* wait for FIFO WR command to complete to avoid overflow */
Karthikeyan Mani5d52dd82019-08-15 16:58:08 -0700529 /*
530 * Reduce sleep from 100us to 10us to meet KPIs
531 * This still meets the hardware spec
532 */
533 usleep_range(10, 12);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530534 swr_master_write(swrm, reg_addr[i], val[i]);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530535 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530536 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530537 }
538 return 0;
539}
540
541static bool swrm_is_port_en(struct swr_master *mstr)
542{
543 return !!(mstr->num_port);
544}
545
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530546static void copy_port_tables(struct swr_mstr_ctrl *swrm,
547 struct port_params *params)
548{
549 u8 i;
550 struct port_params *config = params;
551
552 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
553 /* wsa uses single frame structure for all configurations */
554 if (!swrm->mport_cfg[i].port_en)
555 continue;
556 swrm->mport_cfg[i].sinterval = config[i].si;
557 swrm->mport_cfg[i].offset1 = config[i].off1;
558 swrm->mport_cfg[i].offset2 = config[i].off2;
559 swrm->mport_cfg[i].hstart = config[i].hstart;
560 swrm->mport_cfg[i].hstop = config[i].hstop;
561 swrm->mport_cfg[i].blk_pack_mode = config[i].bp_mode;
562 swrm->mport_cfg[i].blk_grp_count = config[i].bgp_ctrl;
563 swrm->mport_cfg[i].word_length = config[i].wd_len;
564 swrm->mport_cfg[i].lane_ctrl = config[i].lane_ctrl;
565 }
566}
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530567static int swrm_get_port_config(struct swr_mstr_ctrl *swrm)
568{
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530569 struct port_params *params;
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530570 u32 usecase = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530571
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530572 /* TODO - Send usecase information to avoid checking for master_id */
573 if (swrm->mport_cfg[SWRM_DSD_PARAMS_PORT].port_en &&
574 (swrm->master_id == MASTER_ID_RX))
575 usecase = 1;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530576
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530577 params = swrm->port_param[usecase];
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530578 copy_port_tables(swrm, params);
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530579
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530580 return 0;
581}
582
583static int swrm_get_master_port(struct swr_mstr_ctrl *swrm, u8 *mstr_port_id,
584 u8 *mstr_ch_mask, u8 mstr_prt_type,
585 u8 slv_port_id)
586{
587 int i, j;
588 *mstr_port_id = 0;
589
590 for (i = 1; i <= swrm->num_ports; i++) {
591 for (j = 0; j < SWR_MAX_CH_PER_PORT; j++) {
592 if (swrm->port_mapping[i][j].port_type == mstr_prt_type)
593 goto found;
594 }
595 }
596found:
597 if (i > swrm->num_ports || j == SWR_MAX_CH_PER_PORT) {
598 dev_err(swrm->dev, "%s: port type not supported by master\n",
599 __func__);
600 return -EINVAL;
601 }
602 /* id 0 corresponds to master port 1 */
603 *mstr_port_id = i - 1;
604 *mstr_ch_mask = swrm->port_mapping[i][j].ch_mask;
605
606 return 0;
607
608}
609
610static u32 swrm_get_packed_reg_val(u8 *cmd_id, u8 cmd_data,
611 u8 dev_addr, u16 reg_addr)
612{
613 u32 val;
614 u8 id = *cmd_id;
615
616 if (id != SWR_BROADCAST_CMD_ID) {
617 if (id < 14)
618 id += 1;
619 else
620 id = 0;
621 *cmd_id = id;
622 }
623 val = SWR_REG_VAL_PACK(cmd_data, dev_addr, id, reg_addr);
624
625 return val;
626}
627
628static int swrm_cmd_fifo_rd_cmd(struct swr_mstr_ctrl *swrm, int *cmd_data,
629 u8 dev_addr, u8 cmd_id, u16 reg_addr,
630 u32 len)
631{
632 u32 val;
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530633 u32 retry_attempt = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530634
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530635 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530636 val = swrm_get_packed_reg_val(&swrm->rcmd_id, len, dev_addr, reg_addr);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530637 if (swrm->read) {
638 /* skip delay if read is handled in platform driver */
639 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
640 } else {
641 /* wait for FIFO RD to complete to avoid overflow */
642 usleep_range(100, 105);
643 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
644 /* wait for FIFO RD CMD complete to avoid overflow */
645 usleep_range(250, 255);
646 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530647retry_read:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530648 *cmd_data = swr_master_read(swrm, SWRM_CMD_FIFO_RD_FIFO_ADDR);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530649 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, rcmd_id: 0x%x, \
650 dev_num: 0x%x, cmd_data: 0x%x\n", __func__, reg_addr,
651 cmd_id, swrm->rcmd_id, dev_addr, *cmd_data);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530652 if ((((*cmd_data) & 0xF00) >> 8) != swrm->rcmd_id) {
653 if (retry_attempt < MAX_FIFO_RD_FAIL_RETRY) {
654 /* wait 500 us before retry on fifo read failure */
655 usleep_range(500, 505);
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +0530656 if (retry_attempt == (MAX_FIFO_RD_FAIL_RETRY - 1)) {
657 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
658 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
659 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530660 retry_attempt++;
661 goto retry_read;
662 } else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530663 dev_err_ratelimited(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, \
664 rcmd_id: 0x%x, dev_num: 0x%x, cmd_data: 0x%x\n",
665 __func__, reg_addr, cmd_id, swrm->rcmd_id,
666 dev_addr, *cmd_data);
667
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530668 dev_err_ratelimited(swrm->dev,
669 "%s: failed to read fifo\n", __func__);
670 }
671 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530672 mutex_unlock(&swrm->iolock);
673
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530674 return 0;
675}
676
677static int swrm_cmd_fifo_wr_cmd(struct swr_mstr_ctrl *swrm, u8 cmd_data,
678 u8 dev_addr, u8 cmd_id, u16 reg_addr)
679{
680 u32 val;
681 int ret = 0;
682
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530683 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530684 if (!cmd_id)
685 val = swrm_get_packed_reg_val(&swrm->wcmd_id, cmd_data,
686 dev_addr, reg_addr);
687 else
688 val = swrm_get_packed_reg_val(&cmd_id, cmd_data,
689 dev_addr, reg_addr);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530690 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x,wcmd_id: 0x%x, \
691 dev_num: 0x%x, cmd_data: 0x%x\n", __func__,
692 reg_addr, cmd_id, swrm->wcmd_id,dev_addr, cmd_data);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +0530693 swr_master_write(swrm, SWRM_CMD_FIFO_WR_CMD, val);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530694 /*
695 * wait for FIFO WR command to complete to avoid overflow
696 * skip delay if write is handled in platform driver.
697 */
698 if(!swrm->write)
Karthikeyan Mani5d52dd82019-08-15 16:58:08 -0700699 usleep_range(150, 155);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530700 if (cmd_id == 0xF) {
701 /*
702 * sleep for 10ms for MSM soundwire variant to allow broadcast
703 * command to complete.
704 */
705 if (swrm_is_msm_variant(swrm->version))
706 usleep_range(10000, 10100);
707 else
708 wait_for_completion_timeout(&swrm->broadcast,
709 (2 * HZ/10));
710 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530711 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530712 return ret;
713}
714
715static int swrm_read(struct swr_master *master, u8 dev_num, u16 reg_addr,
716 void *buf, u32 len)
717{
718 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
719 int ret = 0;
720 int val;
721 u8 *reg_val = (u8 *)buf;
722
723 if (!swrm) {
724 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
725 return -EINVAL;
726 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530727 if (!dev_num) {
728 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
729 return -EINVAL;
730 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530731 mutex_lock(&swrm->devlock);
732 if (!swrm->dev_up) {
733 mutex_unlock(&swrm->devlock);
734 return 0;
735 }
736 mutex_unlock(&swrm->devlock);
737
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530738 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530739 ret = swrm_cmd_fifo_rd_cmd(swrm, &val, dev_num, 0, reg_addr, len);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530740
741 if (!ret)
742 *reg_val = (u8)val;
743
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530744 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530745 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530746 return ret;
747}
748
749static int swrm_write(struct swr_master *master, u8 dev_num, u16 reg_addr,
750 const void *buf)
751{
752 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
753 int ret = 0;
754 u8 reg_val = *(u8 *)buf;
755
756 if (!swrm) {
757 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
758 return -EINVAL;
759 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530760 if (!dev_num) {
761 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
762 return -EINVAL;
763 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530764 mutex_lock(&swrm->devlock);
765 if (!swrm->dev_up) {
766 mutex_unlock(&swrm->devlock);
767 return 0;
768 }
769 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530770
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530771 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530772 ret = swrm_cmd_fifo_wr_cmd(swrm, reg_val, dev_num, 0, reg_addr);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530773
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530774 pm_runtime_put_autosuspend(swrm->dev);
775 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530776 return ret;
777}
778
779static int swrm_bulk_write(struct swr_master *master, u8 dev_num, void *reg,
780 const void *buf, size_t len)
781{
782 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
783 int ret = 0;
784 int i;
785 u32 *val;
786 u32 *swr_fifo_reg;
787
788 if (!swrm || !swrm->handle) {
789 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
790 return -EINVAL;
791 }
792 if (len <= 0)
793 return -EINVAL;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530794 mutex_lock(&swrm->devlock);
795 if (!swrm->dev_up) {
796 mutex_unlock(&swrm->devlock);
797 return 0;
798 }
799 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530800
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530801 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530802 if (dev_num) {
803 swr_fifo_reg = kcalloc(len, sizeof(u32), GFP_KERNEL);
804 if (!swr_fifo_reg) {
805 ret = -ENOMEM;
806 goto err;
807 }
808 val = kcalloc(len, sizeof(u32), GFP_KERNEL);
809 if (!val) {
810 ret = -ENOMEM;
811 goto mem_fail;
812 }
813
814 for (i = 0; i < len; i++) {
815 val[i] = swrm_get_packed_reg_val(&swrm->wcmd_id,
816 ((u8 *)buf)[i],
817 dev_num,
818 ((u16 *)reg)[i]);
819 swr_fifo_reg[i] = SWRM_CMD_FIFO_WR_CMD;
820 }
821 ret = swr_master_bulk_write(swrm, swr_fifo_reg, val, len);
822 if (ret) {
823 dev_err(&master->dev, "%s: bulk write failed\n",
824 __func__);
825 ret = -EINVAL;
826 }
827 } else {
828 dev_err(&master->dev,
829 "%s: No support of Bulk write for master regs\n",
830 __func__);
831 ret = -EINVAL;
832 goto err;
833 }
834 kfree(val);
835mem_fail:
836 kfree(swr_fifo_reg);
837err:
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530838 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530839 pm_runtime_mark_last_busy(swrm->dev);
840 return ret;
841}
842
843static u8 get_inactive_bank_num(struct swr_mstr_ctrl *swrm)
844{
845 return (swr_master_read(swrm, SWRM_MCP_STATUS) &
846 SWRM_MCP_STATUS_BANK_NUM_MASK) ? 0 : 1;
847}
848
849static void enable_bank_switch(struct swr_mstr_ctrl *swrm, u8 bank,
850 u8 row, u8 col)
851{
852 swrm_cmd_fifo_wr_cmd(swrm, ((row << 3) | col), 0xF, 0xF,
853 SWRS_SCP_FRAME_CTRL_BANK(bank));
854}
855
856static struct swr_port_info *swrm_get_port_req(struct swrm_mports *mport,
857 u8 slv_port, u8 dev_num)
858{
859 struct swr_port_info *port_req = NULL;
860
861 list_for_each_entry(port_req, &mport->port_req_list, list) {
862 /* Store dev_id instead of dev_num if enumeration is changed run_time */
863 if ((port_req->slave_port_id == slv_port)
864 && (port_req->dev_num == dev_num))
865 return port_req;
866 }
867 return NULL;
868}
869
870static bool swrm_remove_from_group(struct swr_master *master)
871{
872 struct swr_device *swr_dev;
873 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
874 bool is_removed = false;
875
876 if (!swrm)
877 goto end;
878
879 mutex_lock(&swrm->mlock);
880 if ((swrm->num_rx_chs > 1) &&
881 (swrm->num_rx_chs == swrm->num_cfg_devs)) {
882 list_for_each_entry(swr_dev, &master->devices,
883 dev_list) {
884 swr_dev->group_id = SWR_GROUP_NONE;
885 master->gr_sid = 0;
886 }
887 is_removed = true;
888 }
889 mutex_unlock(&swrm->mlock);
890
891end:
892 return is_removed;
893}
894
895static void swrm_disable_ports(struct swr_master *master,
896 u8 bank)
897{
898 u32 value;
899 struct swr_port_info *port_req;
900 int i;
901 struct swrm_mports *mport;
902 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
903
904 if (!swrm) {
905 pr_err("%s: swrm is null\n", __func__);
906 return;
907 }
908
909 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
910 master->num_port);
911
912
913 for (i = 0; i < SWR_MSTR_PORT_LEN ; i++) {
914
915 mport = &(swrm->mport_cfg[i]);
916 if (!mport->port_en)
917 continue;
918
919 list_for_each_entry(port_req, &mport->port_req_list, list) {
920 /* skip ports with no change req's*/
921 if (port_req->req_ch == port_req->ch_en)
922 continue;
923
924 swrm_cmd_fifo_wr_cmd(swrm, port_req->req_ch,
925 port_req->dev_num, 0x00,
926 SWRS_DP_CHANNEL_ENABLE_BANK(port_req->slave_port_id,
927 bank));
928 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x\n",
929 __func__, i,
930 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)));
931 }
932 value = ((mport->req_ch)
933 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
934 value |= ((mport->offset2)
935 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
936 value |= ((mport->offset1)
937 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
938 value |= mport->sinterval;
939
940 swr_master_write(swrm,
941 SWRM_DP_PORT_CTRL_BANK(i+1, bank),
942 value);
943 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
944 __func__, i,
945 (SWRM_DP_PORT_CTRL_BANK(i+1, bank)), value);
946 }
947}
948
949static void swrm_cleanup_disabled_port_reqs(struct swr_master *master)
950{
951 struct swr_port_info *port_req, *next;
952 int i;
953 struct swrm_mports *mport;
954 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
955
956 if (!swrm) {
957 pr_err("%s: swrm is null\n", __func__);
958 return;
959 }
960 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
961 master->num_port);
962
963 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
964 mport = &(swrm->mport_cfg[i]);
965 list_for_each_entry_safe(port_req, next,
966 &mport->port_req_list, list) {
967 /* skip ports without new ch req */
968 if (port_req->ch_en == port_req->req_ch)
969 continue;
970
971 /* remove new ch req's*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +0530972 port_req->ch_en = port_req->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530973
974 /* If no streams enabled on port, remove the port req */
975 if (port_req->ch_en == 0) {
976 list_del(&port_req->list);
977 kfree(port_req);
978 }
979 }
980 /* remove new ch req's on mport*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +0530981 mport->ch_en = mport->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530982
983 if (!(mport->ch_en)) {
984 mport->port_en = false;
985 master->port_en_mask &= ~i;
986 }
987 }
988}
989static void swrm_copy_data_port_config(struct swr_master *master, u8 bank)
990{
991 u32 value, slv_id;
992 struct swr_port_info *port_req;
993 int i;
994 struct swrm_mports *mport;
995 u32 reg[SWRM_MAX_PORT_REG];
996 u32 val[SWRM_MAX_PORT_REG];
997 int len = 0;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530998 u8 hparams;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530999 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1000
1001 if (!swrm) {
1002 pr_err("%s: swrm is null\n", __func__);
1003 return;
1004 }
1005
1006 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
1007 master->num_port);
1008
1009 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
1010 mport = &(swrm->mport_cfg[i]);
1011 if (!mport->port_en)
1012 continue;
1013
1014 list_for_each_entry(port_req, &mport->port_req_list, list) {
1015 slv_id = port_req->slave_port_id;
1016 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1017 val[len++] = SWR_REG_VAL_PACK(port_req->req_ch,
1018 port_req->dev_num, 0x00,
1019 SWRS_DP_CHANNEL_ENABLE_BANK(slv_id,
1020 bank));
1021
1022 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1023 val[len++] = SWR_REG_VAL_PACK(mport->sinterval,
1024 port_req->dev_num, 0x00,
1025 SWRS_DP_SAMPLE_CONTROL_1_BANK(slv_id,
1026 bank));
1027
1028 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1029 val[len++] = SWR_REG_VAL_PACK(mport->offset1,
1030 port_req->dev_num, 0x00,
1031 SWRS_DP_OFFSET_CONTROL_1_BANK(slv_id,
1032 bank));
1033
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301034 if (mport->offset2 != SWR_INVALID_PARAM) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301035 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1036 val[len++] = SWR_REG_VAL_PACK(mport->offset2,
1037 port_req->dev_num, 0x00,
1038 SWRS_DP_OFFSET_CONTROL_2_BANK(
1039 slv_id, bank));
1040 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301041 if (mport->hstart != SWR_INVALID_PARAM
1042 && mport->hstop != SWR_INVALID_PARAM) {
1043 hparams = (mport->hstart << 4) | mport->hstop;
1044
1045 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1046 val[len++] = SWR_REG_VAL_PACK(hparams,
1047 port_req->dev_num, 0x00,
1048 SWRS_DP_HCONTROL_BANK(slv_id,
1049 bank));
1050 }
1051 if (mport->word_length != SWR_INVALID_PARAM) {
1052 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1053 val[len++] =
1054 SWR_REG_VAL_PACK(mport->word_length,
1055 port_req->dev_num, 0x00,
1056 SWRS_DP_BLOCK_CONTROL_1(slv_id));
1057 }
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +05301058 if (mport->blk_pack_mode != SWR_INVALID_PARAM
1059 && swrm->master_id != MASTER_ID_WSA) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301060 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1061 val[len++] =
1062 SWR_REG_VAL_PACK(mport->blk_pack_mode,
1063 port_req->dev_num, 0x00,
1064 SWRS_DP_BLOCK_CONTROL_3_BANK(slv_id,
1065 bank));
1066 }
1067 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
1068 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1069 val[len++] =
1070 SWR_REG_VAL_PACK(mport->blk_grp_count,
1071 port_req->dev_num, 0x00,
1072 SWRS_DP_BLOCK_CONTROL_2_BANK(slv_id,
1073 bank));
1074 }
1075 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
1076 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1077 val[len++] =
1078 SWR_REG_VAL_PACK(mport->lane_ctrl,
1079 port_req->dev_num, 0x00,
1080 SWRS_DP_LANE_CONTROL_BANK(slv_id,
1081 bank));
1082 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301083 port_req->ch_en = port_req->req_ch;
1084 }
1085 value = ((mport->req_ch)
1086 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +05301087
1088 if (mport->offset2 != SWR_INVALID_PARAM)
1089 value |= ((mport->offset2)
1090 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301091 value |= ((mport->offset1)
1092 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
1093 value |= mport->sinterval;
1094
1095
1096 reg[len] = SWRM_DP_PORT_CTRL_BANK(i + 1, bank);
1097 val[len++] = value;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301098 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
1099 __func__, i,
1100 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)), value);
1101
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301102 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
1103 reg[len] = SWRM_DP_PORT_CTRL_2_BANK(i + 1, bank);
1104 val[len++] = mport->lane_ctrl;
1105 }
1106 if (mport->word_length != SWR_INVALID_PARAM) {
1107 reg[len] = SWRM_DP_BLOCK_CTRL_1(i + 1);
1108 val[len++] = mport->word_length;
1109 }
1110
1111 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
1112 reg[len] = SWRM_DP_BLOCK_CTRL2_BANK(i + 1, bank);
1113 val[len++] = mport->blk_grp_count;
1114 }
1115 if (mport->hstart != SWR_INVALID_PARAM
1116 && mport->hstop != SWR_INVALID_PARAM) {
1117 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
Laxminath Kasame30eef72018-11-05 17:40:09 +05301118 hparams = (mport->hstop << 4) | mport->hstart;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301119 val[len++] = hparams;
Laxminath Kasam990c70b2018-11-09 23:15:09 +05301120 } else {
1121 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
1122 hparams = (SWR_HSTOP_MAX_VAL << 4) | SWR_HSTART_MIN_VAL;
1123 val[len++] = hparams;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301124 }
1125 if (mport->blk_pack_mode != SWR_INVALID_PARAM) {
1126 reg[len] = SWRM_DP_BLOCK_CTRL3_BANK(i + 1, bank);
1127 val[len++] = mport->blk_pack_mode;
1128 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301129 mport->ch_en = mport->req_ch;
1130
1131 }
Sudheer Papothi0016db12019-06-11 04:42:38 +05301132 swrm_reg_dump(swrm, reg, val, len, __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301133 swr_master_bulk_write(swrm, reg, val, len);
1134}
1135
1136static void swrm_apply_port_config(struct swr_master *master)
1137{
1138 u8 bank;
1139 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1140
1141 if (!swrm) {
1142 pr_err("%s: Invalid handle to swr controller\n",
1143 __func__);
1144 return;
1145 }
1146
1147 bank = get_inactive_bank_num(swrm);
1148 dev_dbg(swrm->dev, "%s: enter bank: %d master_ports: %d\n",
1149 __func__, bank, master->num_port);
1150
1151
1152 swrm_cmd_fifo_wr_cmd(swrm, 0x01, 0xF, 0x00,
1153 SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(bank));
1154
1155 swrm_copy_data_port_config(master, bank);
1156}
1157
1158static int swrm_slvdev_datapath_control(struct swr_master *master, bool enable)
1159{
1160 u8 bank;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301161 u32 value, n_row, n_col;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301162 u32 row = 0, col = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301163 int ret;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301164 u8 ssp_period = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301165 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1166 int mask = (SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK |
1167 SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK |
1168 SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_BMSK);
1169 u8 inactive_bank;
1170
1171 if (!swrm) {
1172 pr_err("%s: swrm is null\n", __func__);
1173 return -EFAULT;
1174 }
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301175
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301176 mutex_lock(&swrm->mlock);
1177
Ramprasad Katkam979b7c92019-05-17 15:31:21 +05301178 /*
1179 * During disable if master is already down, which implies an ssr/pdr
1180 * scenario, just mark ports as disabled and exit
1181 */
1182 if (swrm->state == SWR_MSTR_SSR && !enable) {
1183 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1184 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1185 __func__);
1186 goto exit;
1187 }
1188 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
1189 swrm_cleanup_disabled_port_reqs(master);
1190 if (!swrm_is_port_en(master)) {
1191 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1192 __func__);
1193 pm_runtime_mark_last_busy(swrm->dev);
1194 pm_runtime_put_autosuspend(swrm->dev);
1195 }
1196 goto exit;
1197 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301198 bank = get_inactive_bank_num(swrm);
1199
1200 if (enable) {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301201 if (!test_bit(ENABLE_PENDING, &swrm->port_req_pending)) {
1202 dev_dbg(swrm->dev, "%s:No pending connect port req\n",
1203 __func__);
1204 goto exit;
1205 }
1206 clear_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301207 ret = swrm_get_port_config(swrm);
1208 if (ret) {
1209 /* cannot accommodate ports */
1210 swrm_cleanup_disabled_port_reqs(master);
1211 mutex_unlock(&swrm->mlock);
1212 return -EINVAL;
1213 }
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301214 swr_master_write(swrm, SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301215 SWRM_INTERRUPT_STATUS_MASK);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301216 /* apply the new port config*/
1217 swrm_apply_port_config(master);
1218 } else {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301219 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1220 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1221 __func__);
1222 goto exit;
1223 }
1224 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301225 swrm_disable_ports(master, bank);
1226 }
1227 dev_dbg(swrm->dev, "%s: enable: %d, cfg_devs: %d\n",
1228 __func__, enable, swrm->num_cfg_devs);
1229
1230 if (enable) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301231 /* set col = 16 */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301232 n_col = SWR_MAX_COL;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301233 col = SWRM_COL_16;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301234 } else {
1235 /*
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301236 * Do not change to col = 2 if there are still active ports
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301237 */
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301238 if (!master->num_port) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301239 n_col = SWR_MIN_COL;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301240 col = SWRM_COL_02;
1241 } else {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301242 n_col = SWR_MAX_COL;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301243 col = SWRM_COL_16;
1244 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301245 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301246 /* Use default 50 * x, frame shape. Change based on mclk */
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301247 if (swrm->mclk_freq == MCLK_FREQ_NATIVE) {
1248 dev_dbg(swrm->dev, "setting 64 x %d frameshape\n",
1249 n_col ? 16 : 2);
1250 n_row = SWR_ROW_64;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301251 row = SWRM_ROW_64;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301252 } else {
1253 dev_dbg(swrm->dev, "setting 50 x %d frameshape\n",
1254 n_col ? 16 : 2);
1255 n_row = SWR_ROW_50;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301256 row = SWRM_ROW_50;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301257 }
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301258 ssp_period = swrm_get_ssp_period(swrm, row, col, SWRM_FRAME_SYNC_SEL);
1259 dev_dbg(swrm->dev, "%s: ssp_period: %d\n", __func__, ssp_period);
1260
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301261 value = swr_master_read(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank));
1262 value &= (~mask);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301263 value |= ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301264 (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301265 ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301266 swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1267
1268 dev_dbg(swrm->dev, "%s: regaddr: 0x%x, value: 0x%x\n", __func__,
1269 SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1270
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301271 enable_bank_switch(swrm, bank, n_row, n_col);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301272 inactive_bank = bank ? 0 : 1;
1273
1274 if (enable)
1275 swrm_copy_data_port_config(master, inactive_bank);
1276 else {
1277 swrm_disable_ports(master, inactive_bank);
1278 swrm_cleanup_disabled_port_reqs(master);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301279 }
1280 if (!swrm_is_port_en(master)) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301281 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1282 __func__);
1283 pm_runtime_mark_last_busy(swrm->dev);
1284 pm_runtime_put_autosuspend(swrm->dev);
1285 }
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301286exit:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301287 mutex_unlock(&swrm->mlock);
1288return 0;
1289}
1290
1291static int swrm_connect_port(struct swr_master *master,
1292 struct swr_params *portinfo)
1293{
1294 int i;
1295 struct swr_port_info *port_req;
1296 int ret = 0;
1297 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1298 struct swrm_mports *mport;
1299 u8 mstr_port_id, mstr_ch_msk;
1300
1301 dev_dbg(&master->dev, "%s: enter\n", __func__);
1302 if (!portinfo)
1303 return -EINVAL;
1304
1305 if (!swrm) {
1306 dev_err(&master->dev,
1307 "%s: Invalid handle to swr controller\n",
1308 __func__);
1309 return -EINVAL;
1310 }
1311
1312 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301313 mutex_lock(&swrm->devlock);
1314 if (!swrm->dev_up) {
1315 mutex_unlock(&swrm->devlock);
1316 mutex_unlock(&swrm->mlock);
1317 return -EINVAL;
1318 }
1319 mutex_unlock(&swrm->devlock);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301320 if (!swrm_is_port_en(master))
1321 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301322
1323 for (i = 0; i < portinfo->num_port; i++) {
1324 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_msk,
1325 portinfo->port_type[i],
1326 portinfo->port_id[i]);
1327 if (ret) {
1328 dev_err(&master->dev,
1329 "%s: mstr portid for slv port %d not found\n",
1330 __func__, portinfo->port_id[i]);
1331 goto port_fail;
1332 }
1333
1334 mport = &(swrm->mport_cfg[mstr_port_id]);
1335 /* get port req */
1336 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1337 portinfo->dev_num);
1338 if (!port_req) {
1339 dev_dbg(&master->dev, "%s: new req:port id %d dev %d\n",
1340 __func__, portinfo->port_id[i],
1341 portinfo->dev_num);
1342 port_req = kzalloc(sizeof(struct swr_port_info),
1343 GFP_KERNEL);
1344 if (!port_req) {
1345 ret = -ENOMEM;
1346 goto mem_fail;
1347 }
1348 port_req->dev_num = portinfo->dev_num;
1349 port_req->slave_port_id = portinfo->port_id[i];
1350 port_req->num_ch = portinfo->num_ch[i];
1351 port_req->ch_rate = portinfo->ch_rate[i];
1352 port_req->ch_en = 0;
1353 port_req->master_port_id = mstr_port_id;
1354 list_add(&port_req->list, &mport->port_req_list);
1355 }
1356 port_req->req_ch |= portinfo->ch_en[i];
1357
1358 dev_dbg(&master->dev,
1359 "%s: mstr port %d, slv port %d ch_rate %d num_ch %d\n",
1360 __func__, port_req->master_port_id,
1361 port_req->slave_port_id, port_req->ch_rate,
1362 port_req->num_ch);
1363 /* Put the port req on master port */
1364 mport = &(swrm->mport_cfg[mstr_port_id]);
1365 mport->port_en = true;
1366 mport->req_ch |= mstr_ch_msk;
1367 master->port_en_mask |= (1 << mstr_port_id);
1368 }
1369 master->num_port += portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301370 set_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301371 swr_port_response(master, portinfo->tid);
1372
1373 mutex_unlock(&swrm->mlock);
1374 return 0;
1375
1376port_fail:
1377mem_fail:
1378 /* cleanup port reqs in error condition */
1379 swrm_cleanup_disabled_port_reqs(master);
1380 mutex_unlock(&swrm->mlock);
1381 return ret;
1382}
1383
1384static int swrm_disconnect_port(struct swr_master *master,
1385 struct swr_params *portinfo)
1386{
1387 int i, ret = 0;
1388 struct swr_port_info *port_req;
1389 struct swrm_mports *mport;
1390 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1391 u8 mstr_port_id, mstr_ch_mask;
1392
1393 if (!swrm) {
1394 dev_err(&master->dev,
1395 "%s: Invalid handle to swr controller\n",
1396 __func__);
1397 return -EINVAL;
1398 }
1399
1400 if (!portinfo) {
1401 dev_err(&master->dev, "%s: portinfo is NULL\n", __func__);
1402 return -EINVAL;
1403 }
1404 mutex_lock(&swrm->mlock);
1405
1406 for (i = 0; i < portinfo->num_port; i++) {
1407
1408 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_mask,
1409 portinfo->port_type[i], portinfo->port_id[i]);
1410 if (ret) {
1411 dev_err(&master->dev,
1412 "%s: mstr portid for slv port %d not found\n",
1413 __func__, portinfo->port_id[i]);
1414 mutex_unlock(&swrm->mlock);
1415 return -EINVAL;
1416 }
1417 mport = &(swrm->mport_cfg[mstr_port_id]);
1418 /* get port req */
1419 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1420 portinfo->dev_num);
1421
1422 if (!port_req) {
1423 dev_err(&master->dev, "%s:port not enabled : port %d\n",
1424 __func__, portinfo->port_id[i]);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05301425 mutex_unlock(&swrm->mlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301426 return -EINVAL;
1427 }
1428 port_req->req_ch &= ~portinfo->ch_en[i];
1429 mport->req_ch &= ~mstr_ch_mask;
1430 }
1431 master->num_port -= portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301432 set_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301433 swr_port_response(master, portinfo->tid);
1434 mutex_unlock(&swrm->mlock);
1435
1436 return 0;
1437}
1438
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301439static int swrm_find_alert_slave(struct swr_mstr_ctrl *swrm,
1440 int status, u8 *devnum)
1441{
1442 int i;
1443 bool found = false;
1444
1445 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1446 if ((status & SWRM_MCP_SLV_STATUS_MASK) == SWR_ALERT) {
1447 *devnum = i;
1448 found = true;
1449 break;
1450 }
1451 status >>= 2;
1452 }
1453 if (found)
1454 return 0;
1455 else
1456 return -EINVAL;
1457}
1458
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301459static void swrm_enable_slave_irq(struct swr_mstr_ctrl *swrm)
1460{
1461 int i;
1462 int status = 0;
1463
1464 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1465 if (!status) {
1466 dev_dbg_ratelimited(swrm->dev, "%s: slaves status is 0x%x\n",
1467 __func__, status);
1468 return;
1469 }
1470 dev_dbg(swrm->dev, "%s: slave status: 0x%x\n", __func__, status);
1471 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1472 if (status & SWRM_MCP_SLV_STATUS_MASK)
1473 swrm_cmd_fifo_wr_cmd(swrm, 0x4, i, 0x0,
1474 SWRS_SCP_INT_STATUS_MASK_1);
1475 status >>= 2;
1476 }
1477}
1478
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301479static int swrm_check_slave_change_status(struct swr_mstr_ctrl *swrm,
1480 int status, u8 *devnum)
1481{
1482 int i;
1483 int new_sts = status;
1484 int ret = SWR_NOT_PRESENT;
1485
1486 if (status != swrm->slave_status) {
1487 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1488 if ((status & SWRM_MCP_SLV_STATUS_MASK) !=
1489 (swrm->slave_status & SWRM_MCP_SLV_STATUS_MASK)) {
1490 ret = (status & SWRM_MCP_SLV_STATUS_MASK);
1491 *devnum = i;
1492 break;
1493 }
1494 status >>= 2;
1495 swrm->slave_status >>= 2;
1496 }
1497 swrm->slave_status = new_sts;
1498 }
1499 return ret;
1500}
1501
1502static irqreturn_t swr_mstr_interrupt(int irq, void *dev)
1503{
1504 struct swr_mstr_ctrl *swrm = dev;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301505 u32 value, intr_sts, intr_sts_masked;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301506 u32 temp = 0;
1507 u32 status, chg_sts, i;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301508 u8 devnum = 0;
1509 int ret = IRQ_HANDLED;
1510 struct swr_device *swr_dev;
1511 struct swr_master *mstr = &swrm->master;
1512
Ramprasad Katkam57349872018-11-11 18:34:57 +05301513 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1514 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1515 return IRQ_NONE;
1516 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301517
1518 mutex_lock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301519 if (swrm_clk_request(swrm, true)) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +05301520 dev_err_ratelimited(swrm->dev, "%s:clk request failed\n",
1521 __func__);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301522 mutex_unlock(&swrm->reslock);
1523 goto exit;
1524 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301525 mutex_unlock(&swrm->reslock);
1526
1527 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301528 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301529handle_irq:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301530 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301531 value = intr_sts_masked & (1 << i);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301532 if (!value)
1533 continue;
1534
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301535 switch (value) {
1536 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1537 dev_dbg(swrm->dev, "Trigger irq to slave device\n");
1538 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301539 ret = swrm_find_alert_slave(swrm, status, &devnum);
1540 if (ret) {
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301541 dev_err_ratelimited(swrm->dev,
1542 "no slave alert found.spurious interrupt\n");
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05301543 break;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301544 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301545 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1546 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1547 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1548 SWRS_SCP_INT_STATUS_CLEAR_1);
1549 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1550 SWRS_SCP_INT_STATUS_CLEAR_1);
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301551
1552
1553 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1554 if (swr_dev->dev_num != devnum)
1555 continue;
1556 if (swr_dev->slave_irq) {
1557 do {
Ramprasad Katkam2586a4b2019-03-18 16:53:39 +05301558 swr_dev->slave_irq_pending = 0;
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301559 handle_nested_irq(
1560 irq_find_mapping(
1561 swr_dev->slave_irq, 0));
1562 } while (swr_dev->slave_irq_pending);
1563 }
1564
1565 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301566 break;
1567 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1568 dev_dbg(swrm->dev, "SWR new slave attached\n");
1569 break;
1570 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1571 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1572 if (status == swrm->slave_status) {
1573 dev_dbg(swrm->dev,
1574 "%s: No change in slave status: %d\n",
1575 __func__, status);
1576 break;
1577 }
1578 chg_sts = swrm_check_slave_change_status(swrm, status,
1579 &devnum);
1580 switch (chg_sts) {
1581 case SWR_NOT_PRESENT:
1582 dev_dbg(swrm->dev, "device %d got detached\n",
1583 devnum);
1584 break;
1585 case SWR_ATTACHED_OK:
1586 dev_dbg(swrm->dev, "device %d got attached\n",
1587 devnum);
Ramprasad Katkamdebe8932018-09-25 18:08:18 +05301588 /* enable host irq from slave device*/
1589 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1590 SWRS_SCP_INT_STATUS_CLEAR_1);
1591 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1592 SWRS_SCP_INT_STATUS_MASK_1);
1593
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301594 break;
1595 case SWR_ALERT:
1596 dev_dbg(swrm->dev,
1597 "device %d has pending interrupt\n",
1598 devnum);
1599 break;
1600 }
1601 break;
1602 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1603 dev_err_ratelimited(swrm->dev,
1604 "SWR bus clsh detected\n");
1605 break;
1606 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1607 dev_dbg(swrm->dev, "SWR read FIFO overflow\n");
1608 break;
1609 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1610 dev_dbg(swrm->dev, "SWR read FIFO underflow\n");
1611 break;
1612 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1613 dev_dbg(swrm->dev, "SWR write FIFO overflow\n");
1614 break;
1615 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1616 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1617 dev_err_ratelimited(swrm->dev,
1618 "SWR CMD error, fifo status 0x%x, flushing fifo\n",
1619 value);
1620 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1621 break;
1622 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301623 dev_err_ratelimited(swrm->dev, "SWR Port collision detected\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301624 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301625 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301626 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301627 break;
1628 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1629 dev_dbg(swrm->dev, "SWR read enable valid mismatch\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301630 swrm->intr_mask &=
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301631 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1632 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301633 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301634 break;
1635 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1636 complete(&swrm->broadcast);
1637 dev_dbg(swrm->dev, "SWR cmd id finished\n");
1638 break;
1639 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_AUTO_ENUM_FINISHED:
1640 break;
1641 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED:
1642 break;
1643 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL:
1644 break;
1645 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED:
1646 complete(&swrm->reset);
1647 break;
1648 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED:
1649 break;
1650 default:
1651 dev_err_ratelimited(swrm->dev,
1652 "SWR unknown interrupt\n");
1653 ret = IRQ_NONE;
1654 break;
1655 }
1656 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301657 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1658 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
Ramprasad Katkam83303512018-10-11 17:34:22 +05301659
1660 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301661 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301662
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301663 if (intr_sts_masked) {
Ramprasad Katkam83303512018-10-11 17:34:22 +05301664 dev_dbg(swrm->dev, "%s: new interrupt received\n", __func__);
1665 goto handle_irq;
1666 }
1667
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301668 mutex_lock(&swrm->reslock);
1669 swrm_clk_request(swrm, false);
1670 mutex_unlock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301671exit:
Ramprasad Katkam57349872018-11-11 18:34:57 +05301672 swrm_unlock_sleep(swrm);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301673 return ret;
1674}
1675
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301676static irqreturn_t swr_mstr_interrupt_v2(int irq, void *dev)
1677{
1678 struct swr_mstr_ctrl *swrm = dev;
1679 u32 value, intr_sts, intr_sts_masked;
1680 u32 temp = 0;
1681 u32 status, chg_sts, i;
1682 u8 devnum = 0;
1683 int ret = IRQ_HANDLED;
1684 struct swr_device *swr_dev;
1685 struct swr_master *mstr = &swrm->master;
1686
1687 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1688 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1689 return IRQ_NONE;
1690 }
1691
1692 mutex_lock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301693 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
1694 ret = IRQ_NONE;
1695 goto exit;
1696 }
1697 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
1698 ret = IRQ_NONE;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301699 goto err_audio_hw_vote;
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001700 }
Karthikeyan Mani4bee1db2019-09-18 17:58:41 -07001701 ret = swrm_clk_request(swrm, true);
1702 if (ret) {
1703 dev_err(dev, "%s: swrm clk failed\n", __func__);
1704 ret = IRQ_NONE;
1705 goto err_audio_core_vote;
1706 }
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301707 mutex_unlock(&swrm->reslock);
1708
1709 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1710 intr_sts_masked = intr_sts & swrm->intr_mask;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301711
1712 dev_dbg(swrm->dev, "%s: status: 0x%x \n", __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301713handle_irq:
1714 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
1715 value = intr_sts_masked & (1 << i);
1716 if (!value)
1717 continue;
1718
1719 switch (value) {
1720 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1721 dev_dbg(swrm->dev, "%s: Trigger irq to slave device\n",
1722 __func__);
1723 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1724 ret = swrm_find_alert_slave(swrm, status, &devnum);
1725 if (ret) {
1726 dev_err_ratelimited(swrm->dev,
1727 "%s: no slave alert found.spurious interrupt\n",
1728 __func__);
1729 break;
1730 }
1731 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1732 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1733 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1734 SWRS_SCP_INT_STATUS_CLEAR_1);
1735 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1736 SWRS_SCP_INT_STATUS_CLEAR_1);
1737
1738
1739 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1740 if (swr_dev->dev_num != devnum)
1741 continue;
1742 if (swr_dev->slave_irq) {
1743 do {
1744 handle_nested_irq(
1745 irq_find_mapping(
1746 swr_dev->slave_irq, 0));
1747 } while (swr_dev->slave_irq_pending);
1748 }
1749
1750 }
1751 break;
1752 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1753 dev_dbg(swrm->dev, "%s: SWR new slave attached\n",
1754 __func__);
1755 break;
1756 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1757 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1758 if (status == swrm->slave_status) {
1759 dev_dbg(swrm->dev,
1760 "%s: No change in slave status: %d\n",
1761 __func__, status);
1762 break;
1763 }
1764 chg_sts = swrm_check_slave_change_status(swrm, status,
1765 &devnum);
1766 switch (chg_sts) {
1767 case SWR_NOT_PRESENT:
1768 dev_dbg(swrm->dev,
1769 "%s: device %d got detached\n",
1770 __func__, devnum);
1771 break;
1772 case SWR_ATTACHED_OK:
1773 dev_dbg(swrm->dev,
1774 "%s: device %d got attached\n",
1775 __func__, devnum);
1776 /* enable host irq from slave device*/
1777 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1778 SWRS_SCP_INT_STATUS_CLEAR_1);
1779 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1780 SWRS_SCP_INT_STATUS_MASK_1);
1781
1782 break;
1783 case SWR_ALERT:
1784 dev_dbg(swrm->dev,
1785 "%s: device %d has pending interrupt\n",
1786 __func__, devnum);
1787 break;
1788 }
1789 break;
1790 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1791 dev_err_ratelimited(swrm->dev,
1792 "%s: SWR bus clsh detected\n",
1793 __func__);
1794 break;
1795 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1796 dev_dbg(swrm->dev, "%s: SWR read FIFO overflow\n",
1797 __func__);
1798 break;
1799 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1800 dev_dbg(swrm->dev, "%s: SWR read FIFO underflow\n",
1801 __func__);
1802 break;
1803 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1804 dev_dbg(swrm->dev, "%s: SWR write FIFO overflow\n",
1805 __func__);
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301806 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301807 break;
1808 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1809 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1810 dev_err_ratelimited(swrm->dev,
1811 "%s: SWR CMD error, fifo status 0x%x, flushing fifo\n",
1812 __func__, value);
1813 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1814 break;
1815 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
1816 dev_err_ratelimited(swrm->dev,
1817 "%s: SWR Port collision detected\n",
1818 __func__);
1819 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
1820 swr_master_write(swrm,
1821 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1822 break;
1823 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1824 dev_dbg(swrm->dev,
1825 "%s: SWR read enable valid mismatch\n",
1826 __func__);
1827 swrm->intr_mask &=
1828 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1829 swr_master_write(swrm,
1830 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1831 break;
1832 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1833 complete(&swrm->broadcast);
1834 dev_dbg(swrm->dev, "%s: SWR cmd id finished\n",
1835 __func__);
1836 break;
1837 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED_V2:
1838 break;
1839 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL_V2:
1840 break;
1841 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED_V2:
1842 break;
1843 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED_V2:
1844 break;
1845 case SWRM_INTERRUPT_STATUS_EXT_CLK_STOP_WAKEUP:
1846 if (swrm->state == SWR_MSTR_UP)
1847 dev_dbg(swrm->dev,
1848 "%s:SWR Master is already up\n",
1849 __func__);
1850 else
1851 dev_err_ratelimited(swrm->dev,
1852 "%s: SWR wokeup during clock stop\n",
1853 __func__);
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301854 /* It might be possible the slave device gets reset
1855 * and slave interrupt gets missed. So re-enable
1856 * Host IRQ and process slave pending
1857 * interrupts, if any.
1858 */
1859 swrm_enable_slave_irq(swrm);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301860 break;
1861 default:
1862 dev_err_ratelimited(swrm->dev,
1863 "%s: SWR unknown interrupt value: %d\n",
1864 __func__, value);
1865 ret = IRQ_NONE;
1866 break;
1867 }
1868 }
1869 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1870 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
1871
1872 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1873 intr_sts_masked = intr_sts & swrm->intr_mask;
1874
1875 if (intr_sts_masked) {
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301876 dev_dbg(swrm->dev, "%s: new interrupt received 0x%x\n",
1877 __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301878 goto handle_irq;
1879 }
1880
1881 mutex_lock(&swrm->reslock);
1882 swrm_clk_request(swrm, false);
Karthikeyan Mani4bee1db2019-09-18 17:58:41 -07001883err_audio_core_vote:
Sudheer Papothi384addd2019-06-14 02:26:52 +05301884 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
Sudheer Papothi06f43412019-07-09 03:32:54 +05301885
1886err_audio_hw_vote:
Sudheer Papothi384addd2019-06-14 02:26:52 +05301887 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001888exit:
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301889 mutex_unlock(&swrm->reslock);
1890 swrm_unlock_sleep(swrm);
1891 return ret;
1892}
1893
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301894static irqreturn_t swrm_wakeup_interrupt(int irq, void *dev)
1895{
1896 struct swr_mstr_ctrl *swrm = dev;
1897 int ret = IRQ_HANDLED;
1898
1899 if (!swrm || !(swrm->dev)) {
1900 pr_err("%s: swrm or dev is null\n", __func__);
1901 return IRQ_NONE;
1902 }
1903 mutex_lock(&swrm->devlock);
1904 if (!swrm->dev_up) {
1905 if (swrm->wake_irq > 0)
1906 disable_irq_nosync(swrm->wake_irq);
1907 mutex_unlock(&swrm->devlock);
1908 return ret;
1909 }
1910 mutex_unlock(&swrm->devlock);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301911 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1912 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1913 goto exit;
1914 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301915 if (swrm->wake_irq > 0)
1916 disable_irq_nosync(swrm->wake_irq);
1917 pm_runtime_get_sync(swrm->dev);
1918 pm_runtime_mark_last_busy(swrm->dev);
1919 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301920 swrm_unlock_sleep(swrm);
1921exit:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301922 return ret;
1923}
1924
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301925static void swrm_wakeup_work(struct work_struct *work)
1926{
1927 struct swr_mstr_ctrl *swrm;
1928
1929 swrm = container_of(work, struct swr_mstr_ctrl,
1930 wakeup_work);
1931 if (!swrm || !(swrm->dev)) {
1932 pr_err("%s: swrm or dev is null\n", __func__);
1933 return;
1934 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301935
1936 mutex_lock(&swrm->devlock);
1937 if (!swrm->dev_up) {
1938 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301939 goto exit;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301940 }
1941 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301942 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1943 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1944 goto exit;
1945 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301946 pm_runtime_get_sync(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301947 pm_runtime_mark_last_busy(swrm->dev);
1948 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301949 swrm_unlock_sleep(swrm);
1950exit:
1951 pm_relax(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301952}
1953
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301954static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
1955{
1956 u32 val;
1957
1958 swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1959 val = (swrm->slave_status >> (devnum * 2));
1960 val &= SWRM_MCP_SLV_STATUS_MASK;
1961 return val;
1962}
1963
1964static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
1965 u8 *dev_num)
1966{
1967 int i;
1968 u64 id = 0;
1969 int ret = -EINVAL;
1970 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1971 struct swr_device *swr_dev;
1972 u32 num_dev = 0;
1973
1974 if (!swrm) {
1975 pr_err("%s: Invalid handle to swr controller\n",
1976 __func__);
1977 return ret;
1978 }
1979 if (swrm->num_dev)
1980 num_dev = swrm->num_dev;
1981 else
1982 num_dev = mstr->num_dev;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301983
1984 mutex_lock(&swrm->devlock);
1985 if (!swrm->dev_up) {
1986 mutex_unlock(&swrm->devlock);
1987 return ret;
1988 }
1989 mutex_unlock(&swrm->devlock);
1990
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301991 pm_runtime_get_sync(swrm->dev);
1992 for (i = 1; i < (num_dev + 1); i++) {
1993 id = ((u64)(swr_master_read(swrm,
1994 SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
1995 id |= swr_master_read(swrm,
1996 SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301997
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301998 /*
1999 * As pm_runtime_get_sync() brings all slaves out of reset
2000 * update logical device number for all slaves.
2001 */
2002 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2003 if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
2004 u32 status = swrm_get_device_status(swrm, i);
2005
2006 if ((status == 0x01) || (status == 0x02)) {
2007 swr_dev->dev_num = i;
2008 if ((id & SWR_DEV_ID_MASK) == dev_id) {
2009 *dev_num = i;
2010 ret = 0;
2011 }
2012 dev_dbg(swrm->dev,
2013 "%s: devnum %d is assigned for dev addr %lx\n",
2014 __func__, i, swr_dev->addr);
2015 }
2016 }
2017 }
2018 }
2019 if (ret)
2020 dev_err(swrm->dev, "%s: device 0x%llx is not ready\n",
2021 __func__, dev_id);
2022
2023 pm_runtime_mark_last_busy(swrm->dev);
2024 pm_runtime_put_autosuspend(swrm->dev);
2025 return ret;
2026}
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302027
2028static void swrm_device_wakeup_vote(struct swr_master *mstr)
2029{
2030 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
2031
2032 if (!swrm) {
2033 pr_err("%s: Invalid handle to swr controller\n",
2034 __func__);
2035 return;
2036 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302037 if (unlikely(swrm_lock_sleep(swrm) == false)) {
2038 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
2039 return;
2040 }
Sudheer Papothi384addd2019-06-14 02:26:52 +05302041 if (++swrm->hw_core_clk_en == 1)
2042 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2043 dev_err(swrm->dev, "%s:lpass core hw enable failed\n",
2044 __func__);
2045 --swrm->hw_core_clk_en;
2046 }
2047 if ( ++swrm->aud_core_clk_en == 1)
2048 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2049 dev_err(swrm->dev, "%s:lpass audio hw enable failed\n",
2050 __func__);
2051 --swrm->aud_core_clk_en;
2052 }
2053 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
2054 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302055 pm_runtime_get_sync(swrm->dev);
2056}
2057
2058static void swrm_device_wakeup_unvote(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 }
2067 pm_runtime_mark_last_busy(swrm->dev);
2068 pm_runtime_put_autosuspend(swrm->dev);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302069 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
2070 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
2071
2072 --swrm->aud_core_clk_en;
2073 if (swrm->aud_core_clk_en < 0)
2074 swrm->aud_core_clk_en = 0;
2075 else if (swrm->aud_core_clk_en == 0)
2076 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2077
2078 --swrm->hw_core_clk_en;
2079 if (swrm->hw_core_clk_en < 0)
2080 swrm->hw_core_clk_en = 0;
2081 else if (swrm->hw_core_clk_en == 0)
2082 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
2083
Ramprasad Katkam57349872018-11-11 18:34:57 +05302084 swrm_unlock_sleep(swrm);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302085}
2086
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302087static int swrm_master_init(struct swr_mstr_ctrl *swrm)
2088{
2089 int ret = 0;
2090 u32 val;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302091 u8 row_ctrl = SWR_ROW_50;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302092 u8 col_ctrl = SWR_MIN_COL;
2093 u8 ssp_period = 1;
2094 u8 retry_cmd_num = 3;
2095 u32 reg[SWRM_MAX_INIT_REG];
2096 u32 value[SWRM_MAX_INIT_REG];
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302097 u32 temp = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302098 int len = 0;
2099
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302100 ssp_period = swrm_get_ssp_period(swrm, SWRM_ROW_50,
2101 SWRM_COL_02, SWRM_FRAME_SYNC_SEL);
2102 dev_dbg(swrm->dev, "%s: ssp_period: %d\n", __func__, ssp_period);
2103
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302104 /* Clear Rows and Cols */
2105 val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
2106 (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302107 ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302108
2109 reg[len] = SWRM_MCP_FRAME_CTRL_BANK_ADDR(0);
2110 value[len++] = val;
2111
2112 /* Set Auto enumeration flag */
2113 reg[len] = SWRM_ENUMERATOR_CFG_ADDR;
2114 value[len++] = 1;
2115
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302116 /* Configure No pings */
2117 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2118 val &= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK;
2119 val |= (0x1f << SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT);
2120 reg[len] = SWRM_MCP_CFG_ADDR;
2121 value[len++] = val;
2122
2123 /* Configure number of retries of a read/write cmd */
2124 val = (retry_cmd_num << SWRM_CMD_FIFO_CFG_NUM_OF_CMD_RETRY_SHFT);
2125 reg[len] = SWRM_CMD_FIFO_CFG_ADDR;
2126 value[len++] = val;
2127
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302128 reg[len] = SWRM_MCP_BUS_CTRL_ADDR;
2129 value[len++] = 0x2;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302130
Ramprasad Katkam83303512018-10-11 17:34:22 +05302131 /* Set IRQ to PULSE */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302132 reg[len] = SWRM_COMP_CFG_ADDR;
Ramprasad Katkam83303512018-10-11 17:34:22 +05302133 value[len++] = 0x02;
2134
2135 reg[len] = SWRM_COMP_CFG_ADDR;
2136 value[len++] = 0x03;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302137
2138 reg[len] = SWRM_INTERRUPT_CLEAR;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302139 value[len++] = 0xFFFFFFFF;
2140
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302141 swrm->intr_mask = SWRM_INTERRUPT_STATUS_MASK;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302142 /* Mask soundwire interrupts */
2143 reg[len] = SWRM_INTERRUPT_MASK_ADDR;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302144 value[len++] = swrm->intr_mask;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302145
2146 reg[len] = SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302147 value[len++] = swrm->intr_mask;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302148
2149 swr_master_bulk_write(swrm, reg, value, len);
2150
Sudheer Papothi63f48152018-11-15 01:08:03 +05302151 /*
2152 * For SWR master version 1.5.1, continue
2153 * execute on command ignore.
2154 */
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302155 /* Execute it for versions >= 1.5.1 */
2156 if (swrm->version >= SWRM_VERSION_1_5_1)
Sudheer Papothi63f48152018-11-15 01:08:03 +05302157 swr_master_write(swrm, SWRM_CMD_FIFO_CFG_ADDR,
2158 (swr_master_read(swrm,
2159 SWRM_CMD_FIFO_CFG_ADDR) | 0x80000000));
2160
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302161 /* SW workaround to gate hw_ctl for SWR version >=1.6 */
2162 if (swrm->version >= SWRM_VERSION_1_6) {
2163 if (swrm->swrm_hctl_reg) {
2164 temp = ioread32(swrm->swrm_hctl_reg);
2165 temp &= 0xFFFFFFFD;
2166 iowrite32(temp, swrm->swrm_hctl_reg);
2167 }
2168 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302169 return ret;
2170}
2171
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302172static int swrm_event_notify(struct notifier_block *self,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302173 unsigned long action, void *data)
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302174{
2175 struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302176 event_notifier);
2177
2178 if (!swrm || !(swrm->dev)) {
2179 pr_err("%s: swrm or dev is NULL\n", __func__);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302180 return -EINVAL;
2181 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302182 switch (action) {
2183 case MSM_AUD_DC_EVENT:
2184 schedule_work(&(swrm->dc_presence_work));
2185 break;
2186 case SWR_WAKE_IRQ_EVENT:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302187 if (swrm->ipc_wakeup && !swrm->ipc_wakeup_triggered) {
2188 swrm->ipc_wakeup_triggered = true;
Ramprasad Katkam57349872018-11-11 18:34:57 +05302189 pm_stay_awake(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302190 schedule_work(&swrm->wakeup_work);
Ramprasad Katkamcd61c6e2018-09-18 13:22:58 +05302191 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302192 break;
2193 default:
2194 dev_err(swrm->dev, "%s: invalid event type: %lu\n",
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302195 __func__, action);
2196 return -EINVAL;
2197 }
2198
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302199 return 0;
2200}
2201
2202static void swrm_notify_work_fn(struct work_struct *work)
2203{
2204 struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302205 dc_presence_work);
2206
2207 if (!swrm || !swrm->pdev) {
2208 pr_err("%s: swrm or pdev is NULL\n", __func__);
2209 return;
2210 }
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302211 swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL);
2212}
2213
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302214static int swrm_probe(struct platform_device *pdev)
2215{
2216 struct swr_mstr_ctrl *swrm;
2217 struct swr_ctrl_platform_data *pdata;
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302218 u32 i, num_ports, port_num, port_type, ch_mask, swrm_hctl_reg = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302219 u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0;
2220 int ret = 0;
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302221 struct clk *lpass_core_hw_vote = NULL;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302222 struct clk *lpass_core_audio = NULL;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302223
2224 /* Allocate soundwire master driver structure */
2225 swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl),
2226 GFP_KERNEL);
2227 if (!swrm) {
2228 ret = -ENOMEM;
2229 goto err_memory_fail;
2230 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302231 swrm->pdev = pdev;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302232 swrm->dev = &pdev->dev;
2233 platform_set_drvdata(pdev, swrm);
2234 swr_set_ctrl_data(&swrm->master, swrm);
2235 pdata = dev_get_platdata(&pdev->dev);
2236 if (!pdata) {
2237 dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
2238 __func__);
2239 ret = -EINVAL;
2240 goto err_pdata_fail;
2241 }
2242 swrm->handle = (void *)pdata->handle;
2243 if (!swrm->handle) {
2244 dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
2245 __func__);
2246 ret = -EINVAL;
2247 goto err_pdata_fail;
2248 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302249 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr_master_id",
2250 &swrm->master_id);
2251 if (ret) {
2252 dev_err(&pdev->dev, "%s: failed to get master id\n", __func__);
2253 goto err_pdata_fail;
2254 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302255 if (!(of_property_read_u32(pdev->dev.of_node,
2256 "swrm-io-base", &swrm->swrm_base_reg)))
2257 ret = of_property_read_u32(pdev->dev.of_node,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302258 "swrm-io-base", &swrm->swrm_base_reg);
2259 if (!swrm->swrm_base_reg) {
2260 swrm->read = pdata->read;
2261 if (!swrm->read) {
2262 dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
2263 __func__);
2264 ret = -EINVAL;
2265 goto err_pdata_fail;
2266 }
2267 swrm->write = pdata->write;
2268 if (!swrm->write) {
2269 dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
2270 __func__);
2271 ret = -EINVAL;
2272 goto err_pdata_fail;
2273 }
2274 swrm->bulk_write = pdata->bulk_write;
2275 if (!swrm->bulk_write) {
2276 dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
2277 __func__);
2278 ret = -EINVAL;
2279 goto err_pdata_fail;
2280 }
2281 } else {
2282 swrm->swrm_dig_base = devm_ioremap(&pdev->dev,
2283 swrm->swrm_base_reg, SWRM_MAX_REGISTER);
2284 }
2285
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07002286 swrm->core_vote = pdata->core_vote;
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302287 if (!(of_property_read_u32(pdev->dev.of_node,
2288 "qcom,swrm-hctl-reg", &swrm_hctl_reg)))
2289 swrm->swrm_hctl_reg = devm_ioremap(&pdev->dev,
2290 swrm_hctl_reg, 0x4);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302291 swrm->clk = pdata->clk;
2292 if (!swrm->clk) {
2293 dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
2294 __func__);
2295 ret = -EINVAL;
2296 goto err_pdata_fail;
2297 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302298 if (of_property_read_u32(pdev->dev.of_node,
2299 "qcom,swr-clock-stop-mode0",
2300 &swrm->clk_stop_mode0_supp)) {
2301 swrm->clk_stop_mode0_supp = FALSE;
2302 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302303
2304 ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
2305 &swrm->num_dev);
2306 if (ret) {
2307 dev_dbg(&pdev->dev, "%s: Looking up %s property failed\n",
2308 __func__, "qcom,swr-num-dev");
2309 } else {
2310 if (swrm->num_dev > SWR_MAX_SLAVE_DEVICES) {
2311 dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
2312 __func__, swrm->num_dev, SWR_MAX_SLAVE_DEVICES);
2313 ret = -EINVAL;
2314 goto err_pdata_fail;
2315 }
2316 }
2317
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302318 /* Parse soundwire port mapping */
2319 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
2320 &num_ports);
2321 if (ret) {
2322 dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
2323 goto err_pdata_fail;
2324 }
2325 swrm->num_ports = num_ports;
2326
2327 if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
2328 &map_size)) {
2329 dev_err(swrm->dev, "missing port mapping\n");
2330 goto err_pdata_fail;
2331 }
2332
2333 map_length = map_size / (3 * sizeof(u32));
2334 if (num_ports > SWR_MSTR_PORT_LEN) {
2335 dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
2336 __func__);
2337 ret = -EINVAL;
2338 goto err_pdata_fail;
2339 }
2340 temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
2341
2342 if (!temp) {
2343 ret = -ENOMEM;
2344 goto err_pdata_fail;
2345 }
2346 ret = of_property_read_u32_array(pdev->dev.of_node,
2347 "qcom,swr-port-mapping", temp, 3 * map_length);
2348 if (ret) {
2349 dev_err(swrm->dev, "%s: Failed to read port mapping\n",
2350 __func__);
2351 goto err_pdata_fail;
2352 }
2353
2354 for (i = 0; i < map_length; i++) {
2355 port_num = temp[3 * i];
2356 port_type = temp[3 * i + 1];
2357 ch_mask = temp[3 * i + 2];
2358
2359 if (port_num != old_port_num)
2360 ch_iter = 0;
2361 swrm->port_mapping[port_num][ch_iter].port_type = port_type;
2362 swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
2363 old_port_num = port_num;
2364 }
2365 devm_kfree(&pdev->dev, temp);
2366
2367 swrm->reg_irq = pdata->reg_irq;
2368 swrm->master.read = swrm_read;
2369 swrm->master.write = swrm_write;
2370 swrm->master.bulk_write = swrm_bulk_write;
2371 swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
2372 swrm->master.connect_port = swrm_connect_port;
2373 swrm->master.disconnect_port = swrm_disconnect_port;
2374 swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
2375 swrm->master.remove_from_group = swrm_remove_from_group;
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302376 swrm->master.device_wakeup_vote = swrm_device_wakeup_vote;
2377 swrm->master.device_wakeup_unvote = swrm_device_wakeup_unvote;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302378 swrm->master.dev.parent = &pdev->dev;
2379 swrm->master.dev.of_node = pdev->dev.of_node;
2380 swrm->master.num_port = 0;
2381 swrm->rcmd_id = 0;
2382 swrm->wcmd_id = 0;
2383 swrm->slave_status = 0;
2384 swrm->num_rx_chs = 0;
2385 swrm->clk_ref_count = 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302386 swrm->swr_irq_wakeup_capable = 0;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302387 swrm->mclk_freq = MCLK_FREQ;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302388 swrm->bus_clk = MCLK_FREQ;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302389 swrm->dev_up = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302390 swrm->state = SWR_MSTR_UP;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302391 swrm->ipc_wakeup = false;
2392 swrm->ipc_wakeup_triggered = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302393 init_completion(&swrm->reset);
2394 init_completion(&swrm->broadcast);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302395 init_completion(&swrm->clk_off_complete);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302396 mutex_init(&swrm->mlock);
2397 mutex_init(&swrm->reslock);
2398 mutex_init(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302399 mutex_init(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302400 mutex_init(&swrm->clklock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302401 mutex_init(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302402 mutex_init(&swrm->pm_lock);
2403 swrm->wlock_holders = 0;
2404 swrm->pm_state = SWRM_PM_SLEEPABLE;
2405 init_waitqueue_head(&swrm->pm_wq);
2406 pm_qos_add_request(&swrm->pm_qos_req,
2407 PM_QOS_CPU_DMA_LATENCY,
2408 PM_QOS_DEFAULT_VALUE);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302409
2410 for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++)
2411 INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
2412
Sudheer Papothi06f43412019-07-09 03:32:54 +05302413 /* Register LPASS core hw vote */
2414 lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
2415 if (IS_ERR(lpass_core_hw_vote)) {
2416 ret = PTR_ERR(lpass_core_hw_vote);
2417 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2418 __func__, "lpass_core_hw_vote", ret);
2419 lpass_core_hw_vote = NULL;
2420 ret = 0;
2421 }
2422 swrm->lpass_core_hw_vote = lpass_core_hw_vote;
2423
2424 /* Register LPASS audio core vote */
2425 lpass_core_audio = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
2426 if (IS_ERR(lpass_core_audio)) {
2427 ret = PTR_ERR(lpass_core_audio);
2428 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2429 __func__, "lpass_core_audio", ret);
2430 lpass_core_audio = NULL;
2431 ret = 0;
2432 }
2433 swrm->lpass_core_audio = lpass_core_audio;
2434
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302435 if (swrm->reg_irq) {
2436 ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
2437 SWR_IRQ_REGISTER);
2438 if (ret) {
2439 dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
2440 __func__, ret);
2441 goto err_irq_fail;
2442 }
2443 } else {
2444 swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
2445 if (swrm->irq < 0) {
2446 dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
2447 __func__, swrm->irq);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302448 goto err_irq_fail;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302449 }
2450
2451 ret = request_threaded_irq(swrm->irq, NULL,
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302452 swr_mstr_interrupt_v2,
Ramprasad Katkam83303512018-10-11 17:34:22 +05302453 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302454 "swr_master_irq", swrm);
2455 if (ret) {
2456 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2457 __func__, ret);
2458 goto err_irq_fail;
2459 }
2460
2461 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302462 /* Make inband tx interrupts as wakeup capable for slave irq */
2463 ret = of_property_read_u32(pdev->dev.of_node,
2464 "qcom,swr-mstr-irq-wakeup-capable",
2465 &swrm->swr_irq_wakeup_capable);
2466 if (ret)
2467 dev_dbg(swrm->dev, "%s: swrm irq wakeup capable not defined\n",
2468 __func__);
2469 if (swrm->swr_irq_wakeup_capable)
2470 irq_set_irq_wake(swrm->irq, 1);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302471 ret = swr_register_master(&swrm->master);
2472 if (ret) {
2473 dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
2474 goto err_mstr_fail;
2475 }
2476
2477 /* Add devices registered with board-info as the
2478 * controller will be up now
2479 */
2480 swr_master_add_boarddevices(&swrm->master);
2481 mutex_lock(&swrm->mlock);
2482 swrm_clk_request(swrm, true);
2483 ret = swrm_master_init(swrm);
2484 if (ret < 0) {
2485 dev_err(&pdev->dev,
2486 "%s: Error in master Initialization , err %d\n",
2487 __func__, ret);
2488 mutex_unlock(&swrm->mlock);
2489 goto err_mstr_fail;
2490 }
2491 swrm->version = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
2492
2493 mutex_unlock(&swrm->mlock);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302494 INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302495
2496 if (pdev->dev.of_node)
2497 of_register_swr_devices(&swrm->master);
2498
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302499#ifdef CONFIG_DEBUG_FS
2500 swrm->debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
2501 if (!IS_ERR(swrm->debugfs_swrm_dent)) {
2502 swrm->debugfs_peek = debugfs_create_file("swrm_peek",
2503 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2504 (void *) swrm, &swrm_debug_read_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302505
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302506 swrm->debugfs_poke = debugfs_create_file("swrm_poke",
2507 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2508 (void *) swrm, &swrm_debug_write_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302509
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302510 swrm->debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
2511 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2512 (void *) swrm,
2513 &swrm_debug_dump_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302514 }
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302515#endif
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302516 ret = device_init_wakeup(swrm->dev, true);
2517 if (ret) {
2518 dev_err(swrm->dev, "Device wakeup init failed: %d\n", ret);
2519 goto err_irq_wakeup_fail;
2520 }
2521
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302522 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
2523 pm_runtime_use_autosuspend(&pdev->dev);
2524 pm_runtime_set_active(&pdev->dev);
2525 pm_runtime_enable(&pdev->dev);
2526 pm_runtime_mark_last_busy(&pdev->dev);
2527
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302528 INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn);
2529 swrm->event_notifier.notifier_call = swrm_event_notify;
2530 msm_aud_evt_register_client(&swrm->event_notifier);
2531
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302532 return 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302533err_irq_wakeup_fail:
2534 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302535err_mstr_fail:
2536 if (swrm->reg_irq)
2537 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2538 swrm, SWR_IRQ_FREE);
2539 else if (swrm->irq)
2540 free_irq(swrm->irq, swrm);
2541err_irq_fail:
2542 mutex_destroy(&swrm->mlock);
2543 mutex_destroy(&swrm->reslock);
2544 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302545 mutex_destroy(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302546 mutex_destroy(&swrm->clklock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302547 mutex_destroy(&swrm->pm_lock);
2548 pm_qos_remove_request(&swrm->pm_qos_req);
2549
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302550err_pdata_fail:
2551err_memory_fail:
2552 return ret;
2553}
2554
2555static int swrm_remove(struct platform_device *pdev)
2556{
2557 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2558
2559 if (swrm->reg_irq)
2560 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2561 swrm, SWR_IRQ_FREE);
2562 else if (swrm->irq)
2563 free_irq(swrm->irq, swrm);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302564 else if (swrm->wake_irq > 0)
2565 free_irq(swrm->wake_irq, swrm);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302566 if (swrm->swr_irq_wakeup_capable)
2567 irq_set_irq_wake(swrm->irq, 0);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302568 cancel_work_sync(&swrm->wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302569 pm_runtime_disable(&pdev->dev);
2570 pm_runtime_set_suspended(&pdev->dev);
2571 swr_unregister_master(&swrm->master);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302572 msm_aud_evt_unregister_client(&swrm->event_notifier);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302573 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302574 mutex_destroy(&swrm->mlock);
2575 mutex_destroy(&swrm->reslock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302576 mutex_destroy(&swrm->iolock);
2577 mutex_destroy(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302578 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302579 mutex_destroy(&swrm->pm_lock);
2580 pm_qos_remove_request(&swrm->pm_qos_req);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302581 devm_kfree(&pdev->dev, swrm);
2582 return 0;
2583}
2584
2585static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
2586{
2587 u32 val;
2588
2589 dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
2590 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD);
2591 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2592 val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK;
2593 swr_master_write(swrm, SWRM_MCP_CFG_ADDR, val);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302594
2595 return 0;
2596}
2597
2598#ifdef CONFIG_PM
2599static int swrm_runtime_resume(struct device *dev)
2600{
2601 struct platform_device *pdev = to_platform_device(dev);
2602 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2603 int ret = 0;
Vatsal Buchae50b5002019-09-19 14:32:20 +05302604 bool swrm_clk_req_err = false;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302605 bool hw_core_err = false;
2606 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302607 struct swr_master *mstr = &swrm->master;
2608 struct swr_device *swr_dev;
2609
2610 dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
2611 __func__, swrm->state);
2612 mutex_lock(&swrm->reslock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302613
Sudheer Papothi384addd2019-06-14 02:26:52 +05302614 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2615 dev_err(dev, "%s:lpass core hw enable failed\n",
2616 __func__);
2617 hw_core_err = true;
2618 }
2619 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2620 dev_err(dev, "%s:lpass audio hw enable failed\n",
2621 __func__);
2622 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002623 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302624
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302625 if ((swrm->state == SWR_MSTR_DOWN) ||
2626 (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) {
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302627 if (swrm->clk_stop_mode0_supp) {
2628 if (swrm->ipc_wakeup)
2629 msm_aud_evt_blocking_notifier_call_chain(
2630 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302631 }
2632
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302633 if (swrm_clk_request(swrm, true)) {
2634 /*
2635 * Set autosuspend timer to 1 for
2636 * master to enter into suspend.
2637 */
Vatsal Buchae50b5002019-09-19 14:32:20 +05302638 swrm_clk_req_err = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302639 goto exit;
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302640 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302641 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302642 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2643 ret = swr_device_up(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302644 if (ret == -ENODEV) {
2645 dev_dbg(dev,
2646 "%s slave device up not implemented\n",
2647 __func__);
2648 ret = 0;
2649 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302650 dev_err(dev,
2651 "%s: failed to wakeup swr dev %d\n",
2652 __func__, swr_dev->dev_num);
2653 swrm_clk_request(swrm, false);
2654 goto exit;
2655 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302656 }
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05302657 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2658 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2659 swrm_master_init(swrm);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302660 /* wait for hw enumeration to complete */
2661 usleep_range(100, 105);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302662 swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, 0x0,
2663 SWRS_SCP_INT_STATUS_MASK_1);
Karthikeyan Manif6821902019-05-21 17:31:24 -07002664 if (swrm->state == SWR_MSTR_SSR) {
2665 mutex_unlock(&swrm->reslock);
2666 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
2667 mutex_lock(&swrm->reslock);
2668 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302669 } else {
2670 /*wake up from clock stop*/
2671 swr_master_write(swrm, SWRM_MCP_BUS_CTRL_ADDR, 0x2);
2672 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302673 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302674 swrm->state = SWR_MSTR_UP;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302675 }
2676exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302677 if (!aud_core_err)
2678 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2679 if (!hw_core_err)
2680 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Vatsal Buchae50b5002019-09-19 14:32:20 +05302681 if (swrm_clk_req_err)
2682 pm_runtime_set_autosuspend_delay(&pdev->dev,
2683 ERR_AUTO_SUSPEND_TIMER_VAL);
2684 else
2685 pm_runtime_set_autosuspend_delay(&pdev->dev,
2686 auto_suspend_timer);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302687 mutex_unlock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302688
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302689 return ret;
2690}
2691
2692static int swrm_runtime_suspend(struct device *dev)
2693{
2694 struct platform_device *pdev = to_platform_device(dev);
2695 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2696 int ret = 0;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302697 bool hw_core_err = false;
2698 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302699 struct swr_master *mstr = &swrm->master;
2700 struct swr_device *swr_dev;
2701 int current_state = 0;
2702
2703 dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
2704 __func__, swrm->state);
2705 mutex_lock(&swrm->reslock);
2706 mutex_lock(&swrm->force_down_lock);
2707 current_state = swrm->state;
2708 mutex_unlock(&swrm->force_down_lock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302709
2710 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2711 dev_err(dev, "%s:lpass core hw enable failed\n",
2712 __func__);
2713 hw_core_err = true;
2714 }
2715 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2716 dev_err(dev, "%s:lpass audio hw enable failed\n",
2717 __func__);
2718 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002719 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302720
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302721 if ((current_state == SWR_MSTR_UP) ||
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302722 (current_state == SWR_MSTR_SSR)) {
2723
2724 if ((current_state != SWR_MSTR_SSR) &&
2725 swrm_is_port_en(&swrm->master)) {
2726 dev_dbg(dev, "%s ports are enabled\n", __func__);
2727 ret = -EBUSY;
2728 goto exit;
2729 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302730 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05302731 mutex_unlock(&swrm->reslock);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +05302732 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
Sudheer Papothi06f43412019-07-09 03:32:54 +05302733 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302734 swrm_clk_pause(swrm);
2735 swr_master_write(swrm, SWRM_COMP_CFG_ADDR, 0x00);
2736 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2737 ret = swr_device_down(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302738 if (ret == -ENODEV) {
2739 dev_dbg_ratelimited(dev,
2740 "%s slave device down not implemented\n",
2741 __func__);
2742 ret = 0;
2743 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302744 dev_err(dev,
2745 "%s: failed to shutdown swr dev %d\n",
2746 __func__, swr_dev->dev_num);
2747 goto exit;
2748 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302749 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302750 } else {
Sudheer Papothi384addd2019-06-14 02:26:52 +05302751 mutex_unlock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302752 /* clock stop sequence */
2753 swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
2754 SWRS_SCP_CONTROL);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302755 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302756 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302757 }
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07002758 ret = swrm_clk_request(swrm, false);
2759 if (ret) {
2760 dev_err(dev, "%s: swrmn clk failed\n", __func__);
2761 ret = 0;
2762 goto exit;
2763 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302764
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302765 if (swrm->clk_stop_mode0_supp) {
2766 if (swrm->wake_irq > 0) {
2767 enable_irq(swrm->wake_irq);
2768 } else if (swrm->ipc_wakeup) {
2769 msm_aud_evt_blocking_notifier_call_chain(
2770 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
2771 swrm->ipc_wakeup_triggered = false;
2772 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302773 }
2774
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302775 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302776 /* Retain SSR state until resume */
2777 if (current_state != SWR_MSTR_SSR)
2778 swrm->state = SWR_MSTR_DOWN;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302779exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302780 if (!aud_core_err)
2781 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2782 if (!hw_core_err)
2783 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302784 mutex_unlock(&swrm->reslock);
2785 return ret;
2786}
2787#endif /* CONFIG_PM */
2788
Sudheer Papothi06f43412019-07-09 03:32:54 +05302789static int swrm_device_suspend(struct device *dev)
2790{
2791 struct platform_device *pdev = to_platform_device(dev);
2792 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2793 int ret = 0;
2794
2795 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2796 if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
2797 ret = swrm_runtime_suspend(dev);
2798 if (!ret) {
2799 pm_runtime_disable(dev);
2800 pm_runtime_set_suspended(dev);
2801 pm_runtime_enable(dev);
2802 }
2803 }
2804
2805 return 0;
2806}
2807
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302808static int swrm_device_down(struct device *dev)
2809{
2810 struct platform_device *pdev = to_platform_device(dev);
2811 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302812
2813 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2814
2815 mutex_lock(&swrm->force_down_lock);
2816 swrm->state = SWR_MSTR_SSR;
2817 mutex_unlock(&swrm->force_down_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302818
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302819 swrm_device_suspend(dev);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302820 return 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302821}
2822
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302823int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm)
2824{
2825 int ret = 0;
Laxminath Kasama60239e2019-01-10 14:43:03 +05302826 int irq, dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302827
2828 if (!swrm->ipc_wakeup) {
Laxminath Kasama60239e2019-01-10 14:43:03 +05302829 irq = of_get_named_gpio(swrm->dev->of_node,
2830 "qcom,swr-wakeup-irq", 0);
2831 if (gpio_is_valid(irq)) {
2832 swrm->wake_irq = gpio_to_irq(irq);
2833 if (swrm->wake_irq < 0) {
2834 dev_err(swrm->dev,
2835 "Unable to configure irq\n");
2836 return swrm->wake_irq;
2837 }
2838 } else {
2839 dir_apps_irq = platform_get_irq_byname(swrm->pdev,
2840 "swr_wake_irq");
2841 if (dir_apps_irq < 0) {
2842 dev_err(swrm->dev,
2843 "TLMM connect gpio not found\n");
2844 return -EINVAL;
2845 }
2846 swrm->wake_irq = dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302847 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302848 ret = request_threaded_irq(swrm->wake_irq, NULL,
2849 swrm_wakeup_interrupt,
2850 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
2851 "swr_wake_irq", swrm);
2852 if (ret) {
2853 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2854 __func__, ret);
2855 return -EINVAL;
2856 }
Aditya Bavanari3517b112018-12-03 13:26:59 +05302857 irq_set_irq_wake(swrm->wake_irq, 1);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302858 }
2859 return ret;
2860}
2861
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302862static int swrm_alloc_port_mem(struct device *dev, struct swr_mstr_ctrl *swrm,
2863 u32 uc, u32 size)
2864{
2865 if (!swrm->port_param) {
2866 swrm->port_param = devm_kzalloc(dev,
2867 sizeof(swrm->port_param) * SWR_UC_MAX,
2868 GFP_KERNEL);
2869 if (!swrm->port_param)
2870 return -ENOMEM;
2871 }
2872 if (!swrm->port_param[uc]) {
2873 swrm->port_param[uc] = devm_kcalloc(dev, size,
2874 sizeof(struct port_params),
2875 GFP_KERNEL);
2876 if (!swrm->port_param[uc])
2877 return -ENOMEM;
2878 } else {
2879 dev_err_ratelimited(swrm->dev, "%s: called more than once\n",
2880 __func__);
2881 }
2882
2883 return 0;
2884}
2885
2886static int swrm_copy_port_config(struct swr_mstr_ctrl *swrm,
2887 struct swrm_port_config *port_cfg,
2888 u32 size)
2889{
2890 int idx;
2891 struct port_params *params;
2892 int uc = port_cfg->uc;
2893 int ret = 0;
2894
2895 for (idx = 0; idx < size; idx++) {
2896 params = &((struct port_params *)port_cfg->params)[idx];
2897 if (!params) {
2898 dev_err(swrm->dev, "%s: Invalid params\n", __func__);
2899 ret = -EINVAL;
2900 break;
2901 }
2902 memcpy(&swrm->port_param[uc][idx], params,
2903 sizeof(struct port_params));
2904 }
2905
2906 return ret;
2907}
2908
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302909/**
2910 * swrm_wcd_notify - parent device can notify to soundwire master through
2911 * this function
2912 * @pdev: pointer to platform device structure
2913 * @id: command id from parent to the soundwire master
2914 * @data: data from parent device to soundwire master
2915 */
2916int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
2917{
2918 struct swr_mstr_ctrl *swrm;
2919 int ret = 0;
2920 struct swr_master *mstr;
2921 struct swr_device *swr_dev;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302922 struct swrm_port_config *port_cfg;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302923
2924 if (!pdev) {
2925 pr_err("%s: pdev is NULL\n", __func__);
2926 return -EINVAL;
2927 }
2928 swrm = platform_get_drvdata(pdev);
2929 if (!swrm) {
2930 dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__);
2931 return -EINVAL;
2932 }
2933 mstr = &swrm->master;
2934
2935 switch (id) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05302936 case SWR_REQ_CLK_SWITCH:
2937 /* This will put soundwire in clock stop mode and disable the
2938 * clocks, if there is no active usecase running, so that the
2939 * next activity on soundwire will request clock from new clock
2940 * source.
2941 */
2942 mutex_lock(&swrm->mlock);
2943 if (swrm->state == SWR_MSTR_UP)
2944 swrm_device_suspend(&pdev->dev);
2945 mutex_unlock(&swrm->mlock);
2946 break;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302947 case SWR_CLK_FREQ:
2948 if (!data) {
2949 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
2950 ret = -EINVAL;
2951 } else {
2952 mutex_lock(&swrm->mlock);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302953 if (swrm->mclk_freq != *(int *)data) {
2954 dev_dbg(swrm->dev, "%s: freq change: force mstr down\n", __func__);
2955 if (swrm->state == SWR_MSTR_DOWN)
2956 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
2957 __func__, swrm->state);
2958 else
2959 swrm_device_suspend(&pdev->dev);
Prasad Kumpatla386df4e2019-10-11 18:23:16 +05302960 /*
2961 * add delay to ensure clk release happen
2962 * if interrupt triggered for clk stop,
2963 * wait for it to exit
2964 */
2965 usleep_range(10000, 10500);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302966 }
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302967 swrm->mclk_freq = *(int *)data;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302968 swrm->bus_clk = swrm->mclk_freq;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302969 mutex_unlock(&swrm->mlock);
2970 }
2971 break;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302972 case SWR_DEVICE_SSR_DOWN:
2973 mutex_lock(&swrm->devlock);
2974 swrm->dev_up = false;
2975 mutex_unlock(&swrm->devlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302976 mutex_lock(&swrm->reslock);
2977 swrm->state = SWR_MSTR_SSR;
2978 mutex_unlock(&swrm->reslock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302979 break;
2980 case SWR_DEVICE_SSR_UP:
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302981 /* wait for clk voting to be zero */
Ramprasad Katkam7f6462e2018-11-06 11:51:22 +05302982 reinit_completion(&swrm->clk_off_complete);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302983 if (swrm->clk_ref_count &&
2984 !wait_for_completion_timeout(&swrm->clk_off_complete,
Ramprasad Katkamc87efeb2018-12-12 19:26:19 +05302985 msecs_to_jiffies(500)))
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302986 dev_err(swrm->dev, "%s: clock voting not zero\n",
2987 __func__);
2988
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302989 mutex_lock(&swrm->devlock);
2990 swrm->dev_up = true;
2991 mutex_unlock(&swrm->devlock);
2992 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302993 case SWR_DEVICE_DOWN:
2994 dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
2995 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302996 if (swrm->state == SWR_MSTR_DOWN)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302997 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
2998 __func__, swrm->state);
2999 else
3000 swrm_device_down(&pdev->dev);
3001 mutex_unlock(&swrm->mlock);
3002 break;
3003 case SWR_DEVICE_UP:
3004 dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
Ramprasad Katkam0fed92f2018-11-08 14:22:22 +05303005 mutex_lock(&swrm->devlock);
3006 if (!swrm->dev_up) {
3007 dev_dbg(swrm->dev, "SSR not complete yet\n");
3008 mutex_unlock(&swrm->devlock);
3009 return -EBUSY;
3010 }
3011 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303012 mutex_lock(&swrm->mlock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303013 pm_runtime_mark_last_busy(&pdev->dev);
3014 pm_runtime_get_sync(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303015 mutex_lock(&swrm->reslock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303016 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
3017 ret = swr_reset_device(swr_dev);
3018 if (ret) {
3019 dev_err(swrm->dev,
3020 "%s: failed to reset swr device %d\n",
3021 __func__, swr_dev->dev_num);
3022 swrm_clk_request(swrm, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303023 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303024 }
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303025 pm_runtime_mark_last_busy(&pdev->dev);
3026 pm_runtime_put_autosuspend(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303027 mutex_unlock(&swrm->reslock);
3028 mutex_unlock(&swrm->mlock);
3029 break;
3030 case SWR_SET_NUM_RX_CH:
3031 if (!data) {
3032 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
3033 ret = -EINVAL;
3034 } else {
3035 mutex_lock(&swrm->mlock);
3036 swrm->num_rx_chs = *(int *)data;
3037 if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
3038 list_for_each_entry(swr_dev, &mstr->devices,
3039 dev_list) {
3040 ret = swr_set_device_group(swr_dev,
3041 SWR_BROADCAST);
3042 if (ret)
3043 dev_err(swrm->dev,
3044 "%s: set num ch failed\n",
3045 __func__);
3046 }
3047 } else {
3048 list_for_each_entry(swr_dev, &mstr->devices,
3049 dev_list) {
3050 ret = swr_set_device_group(swr_dev,
3051 SWR_GROUP_NONE);
3052 if (ret)
3053 dev_err(swrm->dev,
3054 "%s: set num ch failed\n",
3055 __func__);
3056 }
3057 }
3058 mutex_unlock(&swrm->mlock);
3059 }
3060 break;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303061 case SWR_REGISTER_WAKE_IRQ:
3062 if (!data) {
3063 dev_err(swrm->dev, "%s: reg wake irq data is NULL\n",
3064 __func__);
3065 ret = -EINVAL;
3066 } else {
3067 mutex_lock(&swrm->mlock);
3068 swrm->ipc_wakeup = *(u32 *)data;
3069 ret = swrm_register_wake_irq(swrm);
3070 if (ret)
3071 dev_err(swrm->dev, "%s: register wake_irq failed\n",
3072 __func__);
3073 mutex_unlock(&swrm->mlock);
3074 }
3075 break;
Sudheer Papothi72ee2642019-08-08 05:15:17 +05303076 case SWR_REGISTER_WAKEUP:
3077 msm_aud_evt_blocking_notifier_call_chain(
3078 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
3079 break;
3080 case SWR_DEREGISTER_WAKEUP:
3081 msm_aud_evt_blocking_notifier_call_chain(
3082 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
3083 break;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05303084 case SWR_SET_PORT_MAP:
3085 if (!data) {
3086 dev_err(swrm->dev, "%s: data is NULL for id=%d\n",
3087 __func__, id);
3088 ret = -EINVAL;
3089 } else {
3090 mutex_lock(&swrm->mlock);
3091 port_cfg = (struct swrm_port_config *)data;
3092 if (!port_cfg->size) {
3093 ret = -EINVAL;
3094 goto done;
3095 }
3096 ret = swrm_alloc_port_mem(&pdev->dev, swrm,
3097 port_cfg->uc, port_cfg->size);
3098 if (!ret)
3099 swrm_copy_port_config(swrm, port_cfg,
3100 port_cfg->size);
3101done:
3102 mutex_unlock(&swrm->mlock);
3103 }
3104 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303105 default:
3106 dev_err(swrm->dev, "%s: swr master unknown id %d\n",
3107 __func__, id);
3108 break;
3109 }
3110 return ret;
3111}
3112EXPORT_SYMBOL(swrm_wcd_notify);
3113
Ramprasad Katkam57349872018-11-11 18:34:57 +05303114/*
3115 * swrm_pm_cmpxchg:
3116 * Check old state and exchange with pm new state
3117 * if old state matches with current state
3118 *
3119 * @swrm: pointer to wcd core resource
3120 * @o: pm old state
3121 * @n: pm new state
3122 *
3123 * Returns old state
3124 */
3125static enum swrm_pm_state swrm_pm_cmpxchg(
3126 struct swr_mstr_ctrl *swrm,
3127 enum swrm_pm_state o,
3128 enum swrm_pm_state n)
3129{
3130 enum swrm_pm_state old;
3131
3132 if (!swrm)
3133 return o;
3134
3135 mutex_lock(&swrm->pm_lock);
3136 old = swrm->pm_state;
3137 if (old == o)
3138 swrm->pm_state = n;
3139 mutex_unlock(&swrm->pm_lock);
3140
3141 return old;
3142}
3143
3144static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm)
3145{
3146 enum swrm_pm_state os;
3147
3148 /*
3149 * swrm_{lock/unlock}_sleep will be called by swr irq handler
3150 * and slave wake up requests..
3151 *
3152 * If system didn't resume, we can simply return false so
3153 * IRQ handler can return without handling IRQ.
3154 */
3155 mutex_lock(&swrm->pm_lock);
3156 if (swrm->wlock_holders++ == 0) {
3157 dev_dbg(swrm->dev, "%s: holding wake lock\n", __func__);
3158 pm_qos_update_request(&swrm->pm_qos_req,
3159 msm_cpuidle_get_deep_idle_latency());
3160 pm_stay_awake(swrm->dev);
3161 }
3162 mutex_unlock(&swrm->pm_lock);
3163
3164 if (!wait_event_timeout(swrm->pm_wq,
3165 ((os = swrm_pm_cmpxchg(swrm,
3166 SWRM_PM_SLEEPABLE,
3167 SWRM_PM_AWAKE)) ==
3168 SWRM_PM_SLEEPABLE ||
3169 (os == SWRM_PM_AWAKE)),
3170 msecs_to_jiffies(
3171 SWRM_SYSTEM_RESUME_TIMEOUT_MS))) {
3172 dev_err(swrm->dev, "%s: system didn't resume within %dms, s %d, w %d\n",
3173 __func__, SWRM_SYSTEM_RESUME_TIMEOUT_MS, swrm->pm_state,
3174 swrm->wlock_holders);
3175 swrm_unlock_sleep(swrm);
3176 return false;
3177 }
3178 wake_up_all(&swrm->pm_wq);
3179 return true;
3180}
3181
3182static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm)
3183{
3184 mutex_lock(&swrm->pm_lock);
3185 if (--swrm->wlock_holders == 0) {
3186 dev_dbg(swrm->dev, "%s: releasing wake lock pm_state %d -> %d\n",
3187 __func__, swrm->pm_state, SWRM_PM_SLEEPABLE);
3188 /*
3189 * if swrm_lock_sleep failed, pm_state would be still
3190 * swrm_PM_ASLEEP, don't overwrite
3191 */
3192 if (likely(swrm->pm_state == SWRM_PM_AWAKE))
3193 swrm->pm_state = SWRM_PM_SLEEPABLE;
3194 pm_qos_update_request(&swrm->pm_qos_req,
3195 PM_QOS_DEFAULT_VALUE);
3196 pm_relax(swrm->dev);
3197 }
3198 mutex_unlock(&swrm->pm_lock);
3199 wake_up_all(&swrm->pm_wq);
3200}
3201
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303202#ifdef CONFIG_PM_SLEEP
3203static int swrm_suspend(struct device *dev)
3204{
3205 int ret = -EBUSY;
3206 struct platform_device *pdev = to_platform_device(dev);
3207 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3208
3209 dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
Ramprasad Katkam57349872018-11-11 18:34:57 +05303210
3211 mutex_lock(&swrm->pm_lock);
3212
3213 if (swrm->pm_state == SWRM_PM_SLEEPABLE) {
3214 dev_dbg(swrm->dev, "%s: suspending system, state %d, wlock %d\n",
3215 __func__, swrm->pm_state,
3216 swrm->wlock_holders);
3217 swrm->pm_state = SWRM_PM_ASLEEP;
3218 } else if (swrm->pm_state == SWRM_PM_AWAKE) {
3219 /*
3220 * unlock to wait for pm_state == SWRM_PM_SLEEPABLE
3221 * then set to SWRM_PM_ASLEEP
3222 */
3223 dev_dbg(swrm->dev, "%s: waiting to suspend system, state %d, wlock %d\n",
3224 __func__, swrm->pm_state,
3225 swrm->wlock_holders);
3226 mutex_unlock(&swrm->pm_lock);
3227 if (!(wait_event_timeout(swrm->pm_wq, swrm_pm_cmpxchg(
3228 swrm, SWRM_PM_SLEEPABLE,
3229 SWRM_PM_ASLEEP) ==
3230 SWRM_PM_SLEEPABLE,
3231 msecs_to_jiffies(
3232 SWRM_SYS_SUSPEND_WAIT)))) {
3233 dev_dbg(swrm->dev, "%s: suspend failed state %d, wlock %d\n",
3234 __func__, swrm->pm_state,
3235 swrm->wlock_holders);
3236 return -EBUSY;
3237 } else {
3238 dev_dbg(swrm->dev,
3239 "%s: done, state %d, wlock %d\n",
3240 __func__, swrm->pm_state,
3241 swrm->wlock_holders);
3242 }
3243 mutex_lock(&swrm->pm_lock);
3244 } else if (swrm->pm_state == SWRM_PM_ASLEEP) {
3245 dev_dbg(swrm->dev, "%s: system is already suspended, state %d, wlock %d\n",
3246 __func__, swrm->pm_state,
3247 swrm->wlock_holders);
3248 }
3249
3250 mutex_unlock(&swrm->pm_lock);
3251
3252 if ((!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev))) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303253 ret = swrm_runtime_suspend(dev);
3254 if (!ret) {
3255 /*
3256 * Synchronize runtime-pm and system-pm states:
3257 * At this point, we are already suspended. If
3258 * runtime-pm still thinks its active, then
3259 * make sure its status is in sync with HW
3260 * status. The three below calls let the
3261 * runtime-pm know that we are suspended
3262 * already without re-invoking the suspend
3263 * callback
3264 */
3265 pm_runtime_disable(dev);
3266 pm_runtime_set_suspended(dev);
3267 pm_runtime_enable(dev);
3268 }
3269 }
3270 if (ret == -EBUSY) {
3271 /*
3272 * There is a possibility that some audio stream is active
3273 * during suspend. We dont want to return suspend failure in
3274 * that case so that display and relevant components can still
3275 * go to suspend.
3276 * If there is some other error, then it should be passed-on
3277 * to system level suspend
3278 */
3279 ret = 0;
3280 }
3281 return ret;
3282}
3283
3284static int swrm_resume(struct device *dev)
3285{
3286 int ret = 0;
3287 struct platform_device *pdev = to_platform_device(dev);
3288 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3289
3290 dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
3291 if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
3292 ret = swrm_runtime_resume(dev);
3293 if (!ret) {
3294 pm_runtime_mark_last_busy(dev);
3295 pm_request_autosuspend(dev);
3296 }
3297 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05303298 mutex_lock(&swrm->pm_lock);
3299 if (swrm->pm_state == SWRM_PM_ASLEEP) {
3300 dev_dbg(swrm->dev,
3301 "%s: resuming system, state %d, wlock %d\n",
3302 __func__, swrm->pm_state,
3303 swrm->wlock_holders);
3304 swrm->pm_state = SWRM_PM_SLEEPABLE;
3305 } else {
3306 dev_dbg(swrm->dev, "%s: system is already awake, state %d wlock %d\n",
3307 __func__, swrm->pm_state,
3308 swrm->wlock_holders);
3309 }
3310 mutex_unlock(&swrm->pm_lock);
3311 wake_up_all(&swrm->pm_wq);
3312
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303313 return ret;
3314}
3315#endif /* CONFIG_PM_SLEEP */
3316
3317static const struct dev_pm_ops swrm_dev_pm_ops = {
3318 SET_SYSTEM_SLEEP_PM_OPS(
3319 swrm_suspend,
3320 swrm_resume
3321 )
3322 SET_RUNTIME_PM_OPS(
3323 swrm_runtime_suspend,
3324 swrm_runtime_resume,
3325 NULL
3326 )
3327};
3328
3329static const struct of_device_id swrm_dt_match[] = {
3330 {
3331 .compatible = "qcom,swr-mstr",
3332 },
3333 {}
3334};
3335
3336static struct platform_driver swr_mstr_driver = {
3337 .probe = swrm_probe,
3338 .remove = swrm_remove,
3339 .driver = {
3340 .name = SWR_WCD_NAME,
3341 .owner = THIS_MODULE,
3342 .pm = &swrm_dev_pm_ops,
3343 .of_match_table = swrm_dt_match,
Xiaojun Sang53cd13a2018-06-29 15:14:37 +08003344 .suppress_bind_attrs = true,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303345 },
3346};
3347
3348static int __init swrm_init(void)
3349{
3350 return platform_driver_register(&swr_mstr_driver);
3351}
3352module_init(swrm_init);
3353
3354static void __exit swrm_exit(void)
3355{
3356 platform_driver_unregister(&swr_mstr_driver);
3357}
3358module_exit(swrm_exit);
3359
3360MODULE_LICENSE("GPL v2");
3361MODULE_DESCRIPTION("SoundWire Master Controller");
3362MODULE_ALIAS("platform:swr-mstr");