blob: 614a2be3117dadcd1f9240dd9099d228f9abdcce [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 Papothi384addd2019-06-14 02:26:52 +0530317static int swrm_request_hw_vote(struct swr_mstr_ctrl *swrm,
318 int core_type, bool enable)
319{
320 int ret = 0;
321
322 if (core_type == LPASS_HW_CORE) {
323 if (swrm->lpass_core_hw_vote) {
324 if (enable) {
325 ret =
326 clk_prepare_enable(swrm->lpass_core_hw_vote);
327 if (ret < 0)
328 dev_err(swrm->dev,
329 "%s:lpass core hw enable failed\n",
330 __func__);
331 } else
332 clk_disable_unprepare(swrm->lpass_core_hw_vote);
333 }
334 }
335 if (core_type == LPASS_AUDIO_CORE) {
336 if (swrm->lpass_core_audio) {
337 if (enable) {
338 ret =
339 clk_prepare_enable(swrm->lpass_core_audio);
340 if (ret < 0)
341 dev_err(swrm->dev,
342 "%s:lpass audio hw enable failed\n",
343 __func__);
344 } else
345 clk_disable_unprepare(swrm->lpass_core_audio);
346 }
347 }
348
349 return ret;
350}
351
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +0530352static int swrm_get_ssp_period(struct swr_mstr_ctrl *swrm,
353 int row, int col,
354 int frame_sync)
355{
356 if (!swrm || !row || !col || !frame_sync)
357 return 1;
358
359 return ((swrm->bus_clk * 2) / ((row * col) * frame_sync));
360}
361
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530362static int swrm_clk_request(struct swr_mstr_ctrl *swrm, bool enable)
363{
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530364 int ret = 0;
365
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530366 if (!swrm->clk || !swrm->handle)
367 return -EINVAL;
368
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530369 mutex_lock(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530370 if (enable) {
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530371 if (!swrm->dev_up) {
372 ret = -ENODEV;
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530373 goto exit;
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530374 }
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -0700375 if (swrm->core_vote) {
376 ret = swrm->core_vote(swrm->handle, true);
377 if (ret) {
378 dev_err_ratelimited(swrm->dev,
379 "%s: clock enable req failed",
380 __func__);
381 goto exit;
382 }
383 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530384 swrm->clk_ref_count++;
385 if (swrm->clk_ref_count == 1) {
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530386 ret = swrm->clk(swrm->handle, true);
387 if (ret) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +0530388 dev_err_ratelimited(swrm->dev,
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530389 "%s: clock enable req failed",
390 __func__);
391 --swrm->clk_ref_count;
392 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530393 }
394 } else if (--swrm->clk_ref_count == 0) {
395 swrm->clk(swrm->handle, false);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530396 complete(&swrm->clk_off_complete);
397 }
398 if (swrm->clk_ref_count < 0) {
Meng Wang8c60bb52019-06-19 15:49:06 +0800399 dev_err(swrm->dev, "%s: swrm clk count mismatch\n", __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530400 swrm->clk_ref_count = 0;
401 }
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530402
403exit:
404 mutex_unlock(&swrm->clklock);
405 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530406}
407
408static int swrm_ahb_write(struct swr_mstr_ctrl *swrm,
409 u16 reg, u32 *value)
410{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530411 u32 temp = (u32)(*value);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530412 int ret = 0;
413
414 mutex_lock(&swrm->devlock);
415 if (!swrm->dev_up)
416 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530417
418 ret = swrm_clk_request(swrm, TRUE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530419 if (ret) {
420 dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
421 __func__);
422 goto err;
423 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530424 iowrite32(temp, swrm->swrm_dig_base + reg);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530425 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530426err:
427 mutex_unlock(&swrm->devlock);
428 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530429}
430
431static int swrm_ahb_read(struct swr_mstr_ctrl *swrm,
432 u16 reg, u32 *value)
433{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530434 u32 temp = 0;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530435 int ret = 0;
436
437 mutex_lock(&swrm->devlock);
438 if (!swrm->dev_up)
439 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530440
441 ret = swrm_clk_request(swrm, TRUE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530442 if (ret) {
443 dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
444 __func__);
445 goto err;
446 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530447 temp = ioread32(swrm->swrm_dig_base + reg);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530448 *value = temp;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530449 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530450err:
451 mutex_unlock(&swrm->devlock);
452 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530453}
454
455static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr)
456{
457 u32 val = 0;
458
459 if (swrm->read)
460 val = swrm->read(swrm->handle, reg_addr);
461 else
462 swrm_ahb_read(swrm, reg_addr, &val);
463 return val;
464}
465
466static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val)
467{
468 if (swrm->write)
469 swrm->write(swrm->handle, reg_addr, val);
470 else
471 swrm_ahb_write(swrm, reg_addr, &val);
472}
473
474static int swr_master_bulk_write(struct swr_mstr_ctrl *swrm, u32 *reg_addr,
475 u32 *val, unsigned int length)
476{
477 int i = 0;
478
479 if (swrm->bulk_write)
480 swrm->bulk_write(swrm->handle, reg_addr, val, length);
481 else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530482 mutex_lock(&swrm->iolock);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530483 for (i = 0; i < length; i++) {
484 /* wait for FIFO WR command to complete to avoid overflow */
Karthikeyan Mani5d52dd82019-08-15 16:58:08 -0700485 /*
486 * Reduce sleep from 100us to 10us to meet KPIs
487 * This still meets the hardware spec
488 */
489 usleep_range(10, 12);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530490 swr_master_write(swrm, reg_addr[i], val[i]);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530491 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530492 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530493 }
494 return 0;
495}
496
497static bool swrm_is_port_en(struct swr_master *mstr)
498{
499 return !!(mstr->num_port);
500}
501
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530502static void copy_port_tables(struct swr_mstr_ctrl *swrm,
503 struct port_params *params)
504{
505 u8 i;
506 struct port_params *config = params;
507
508 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
509 /* wsa uses single frame structure for all configurations */
510 if (!swrm->mport_cfg[i].port_en)
511 continue;
512 swrm->mport_cfg[i].sinterval = config[i].si;
513 swrm->mport_cfg[i].offset1 = config[i].off1;
514 swrm->mport_cfg[i].offset2 = config[i].off2;
515 swrm->mport_cfg[i].hstart = config[i].hstart;
516 swrm->mport_cfg[i].hstop = config[i].hstop;
517 swrm->mport_cfg[i].blk_pack_mode = config[i].bp_mode;
518 swrm->mport_cfg[i].blk_grp_count = config[i].bgp_ctrl;
519 swrm->mport_cfg[i].word_length = config[i].wd_len;
520 swrm->mport_cfg[i].lane_ctrl = config[i].lane_ctrl;
521 }
522}
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530523static int swrm_get_port_config(struct swr_mstr_ctrl *swrm)
524{
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530525 struct port_params *params;
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530526 u32 usecase = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530527
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530528 /* TODO - Send usecase information to avoid checking for master_id */
529 if (swrm->mport_cfg[SWRM_DSD_PARAMS_PORT].port_en &&
530 (swrm->master_id == MASTER_ID_RX))
531 usecase = 1;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530532
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530533 params = swrm->port_param[usecase];
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530534 copy_port_tables(swrm, params);
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530535
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530536 return 0;
537}
538
539static int swrm_get_master_port(struct swr_mstr_ctrl *swrm, u8 *mstr_port_id,
540 u8 *mstr_ch_mask, u8 mstr_prt_type,
541 u8 slv_port_id)
542{
543 int i, j;
544 *mstr_port_id = 0;
545
546 for (i = 1; i <= swrm->num_ports; i++) {
547 for (j = 0; j < SWR_MAX_CH_PER_PORT; j++) {
548 if (swrm->port_mapping[i][j].port_type == mstr_prt_type)
549 goto found;
550 }
551 }
552found:
553 if (i > swrm->num_ports || j == SWR_MAX_CH_PER_PORT) {
554 dev_err(swrm->dev, "%s: port type not supported by master\n",
555 __func__);
556 return -EINVAL;
557 }
558 /* id 0 corresponds to master port 1 */
559 *mstr_port_id = i - 1;
560 *mstr_ch_mask = swrm->port_mapping[i][j].ch_mask;
561
562 return 0;
563
564}
565
566static u32 swrm_get_packed_reg_val(u8 *cmd_id, u8 cmd_data,
567 u8 dev_addr, u16 reg_addr)
568{
569 u32 val;
570 u8 id = *cmd_id;
571
572 if (id != SWR_BROADCAST_CMD_ID) {
573 if (id < 14)
574 id += 1;
575 else
576 id = 0;
577 *cmd_id = id;
578 }
579 val = SWR_REG_VAL_PACK(cmd_data, dev_addr, id, reg_addr);
580
581 return val;
582}
583
584static int swrm_cmd_fifo_rd_cmd(struct swr_mstr_ctrl *swrm, int *cmd_data,
585 u8 dev_addr, u8 cmd_id, u16 reg_addr,
586 u32 len)
587{
588 u32 val;
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530589 u32 retry_attempt = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530590
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530591 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530592 val = swrm_get_packed_reg_val(&swrm->rcmd_id, len, dev_addr, reg_addr);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530593 if (swrm->read) {
594 /* skip delay if read is handled in platform driver */
595 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
596 } else {
597 /* wait for FIFO RD to complete to avoid overflow */
598 usleep_range(100, 105);
599 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
600 /* wait for FIFO RD CMD complete to avoid overflow */
601 usleep_range(250, 255);
602 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530603retry_read:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530604 *cmd_data = swr_master_read(swrm, SWRM_CMD_FIFO_RD_FIFO_ADDR);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530605 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, rcmd_id: 0x%x, \
606 dev_num: 0x%x, cmd_data: 0x%x\n", __func__, reg_addr,
607 cmd_id, swrm->rcmd_id, dev_addr, *cmd_data);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530608 if ((((*cmd_data) & 0xF00) >> 8) != swrm->rcmd_id) {
609 if (retry_attempt < MAX_FIFO_RD_FAIL_RETRY) {
610 /* wait 500 us before retry on fifo read failure */
611 usleep_range(500, 505);
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +0530612 if (retry_attempt == (MAX_FIFO_RD_FAIL_RETRY - 1)) {
613 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
614 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
615 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530616 retry_attempt++;
617 goto retry_read;
618 } else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530619 dev_err_ratelimited(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, \
620 rcmd_id: 0x%x, dev_num: 0x%x, cmd_data: 0x%x\n",
621 __func__, reg_addr, cmd_id, swrm->rcmd_id,
622 dev_addr, *cmd_data);
623
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530624 dev_err_ratelimited(swrm->dev,
625 "%s: failed to read fifo\n", __func__);
626 }
627 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530628 mutex_unlock(&swrm->iolock);
629
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530630 return 0;
631}
632
633static int swrm_cmd_fifo_wr_cmd(struct swr_mstr_ctrl *swrm, u8 cmd_data,
634 u8 dev_addr, u8 cmd_id, u16 reg_addr)
635{
636 u32 val;
637 int ret = 0;
638
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530639 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530640 if (!cmd_id)
641 val = swrm_get_packed_reg_val(&swrm->wcmd_id, cmd_data,
642 dev_addr, reg_addr);
643 else
644 val = swrm_get_packed_reg_val(&cmd_id, cmd_data,
645 dev_addr, reg_addr);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530646 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x,wcmd_id: 0x%x, \
647 dev_num: 0x%x, cmd_data: 0x%x\n", __func__,
648 reg_addr, cmd_id, swrm->wcmd_id,dev_addr, cmd_data);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +0530649 swr_master_write(swrm, SWRM_CMD_FIFO_WR_CMD, val);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530650 /*
651 * wait for FIFO WR command to complete to avoid overflow
652 * skip delay if write is handled in platform driver.
653 */
654 if(!swrm->write)
Karthikeyan Mani5d52dd82019-08-15 16:58:08 -0700655 usleep_range(150, 155);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530656 if (cmd_id == 0xF) {
657 /*
658 * sleep for 10ms for MSM soundwire variant to allow broadcast
659 * command to complete.
660 */
661 if (swrm_is_msm_variant(swrm->version))
662 usleep_range(10000, 10100);
663 else
664 wait_for_completion_timeout(&swrm->broadcast,
665 (2 * HZ/10));
666 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530667 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530668 return ret;
669}
670
671static int swrm_read(struct swr_master *master, u8 dev_num, u16 reg_addr,
672 void *buf, u32 len)
673{
674 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
675 int ret = 0;
676 int val;
677 u8 *reg_val = (u8 *)buf;
678
679 if (!swrm) {
680 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
681 return -EINVAL;
682 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530683 if (!dev_num) {
684 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
685 return -EINVAL;
686 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530687 mutex_lock(&swrm->devlock);
688 if (!swrm->dev_up) {
689 mutex_unlock(&swrm->devlock);
690 return 0;
691 }
692 mutex_unlock(&swrm->devlock);
693
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530694 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530695 ret = swrm_cmd_fifo_rd_cmd(swrm, &val, dev_num, 0, reg_addr, len);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530696
697 if (!ret)
698 *reg_val = (u8)val;
699
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530700 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530701 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530702 return ret;
703}
704
705static int swrm_write(struct swr_master *master, u8 dev_num, u16 reg_addr,
706 const void *buf)
707{
708 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
709 int ret = 0;
710 u8 reg_val = *(u8 *)buf;
711
712 if (!swrm) {
713 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
714 return -EINVAL;
715 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530716 if (!dev_num) {
717 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
718 return -EINVAL;
719 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530720 mutex_lock(&swrm->devlock);
721 if (!swrm->dev_up) {
722 mutex_unlock(&swrm->devlock);
723 return 0;
724 }
725 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530726
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530727 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530728 ret = swrm_cmd_fifo_wr_cmd(swrm, reg_val, dev_num, 0, reg_addr);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530729
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530730 pm_runtime_put_autosuspend(swrm->dev);
731 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530732 return ret;
733}
734
735static int swrm_bulk_write(struct swr_master *master, u8 dev_num, void *reg,
736 const void *buf, size_t len)
737{
738 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
739 int ret = 0;
740 int i;
741 u32 *val;
742 u32 *swr_fifo_reg;
743
744 if (!swrm || !swrm->handle) {
745 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
746 return -EINVAL;
747 }
748 if (len <= 0)
749 return -EINVAL;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530750 mutex_lock(&swrm->devlock);
751 if (!swrm->dev_up) {
752 mutex_unlock(&swrm->devlock);
753 return 0;
754 }
755 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530756
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530757 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530758 if (dev_num) {
759 swr_fifo_reg = kcalloc(len, sizeof(u32), GFP_KERNEL);
760 if (!swr_fifo_reg) {
761 ret = -ENOMEM;
762 goto err;
763 }
764 val = kcalloc(len, sizeof(u32), GFP_KERNEL);
765 if (!val) {
766 ret = -ENOMEM;
767 goto mem_fail;
768 }
769
770 for (i = 0; i < len; i++) {
771 val[i] = swrm_get_packed_reg_val(&swrm->wcmd_id,
772 ((u8 *)buf)[i],
773 dev_num,
774 ((u16 *)reg)[i]);
775 swr_fifo_reg[i] = SWRM_CMD_FIFO_WR_CMD;
776 }
777 ret = swr_master_bulk_write(swrm, swr_fifo_reg, val, len);
778 if (ret) {
779 dev_err(&master->dev, "%s: bulk write failed\n",
780 __func__);
781 ret = -EINVAL;
782 }
783 } else {
784 dev_err(&master->dev,
785 "%s: No support of Bulk write for master regs\n",
786 __func__);
787 ret = -EINVAL;
788 goto err;
789 }
790 kfree(val);
791mem_fail:
792 kfree(swr_fifo_reg);
793err:
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530794 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530795 pm_runtime_mark_last_busy(swrm->dev);
796 return ret;
797}
798
799static u8 get_inactive_bank_num(struct swr_mstr_ctrl *swrm)
800{
801 return (swr_master_read(swrm, SWRM_MCP_STATUS) &
802 SWRM_MCP_STATUS_BANK_NUM_MASK) ? 0 : 1;
803}
804
805static void enable_bank_switch(struct swr_mstr_ctrl *swrm, u8 bank,
806 u8 row, u8 col)
807{
808 swrm_cmd_fifo_wr_cmd(swrm, ((row << 3) | col), 0xF, 0xF,
809 SWRS_SCP_FRAME_CTRL_BANK(bank));
810}
811
812static struct swr_port_info *swrm_get_port_req(struct swrm_mports *mport,
813 u8 slv_port, u8 dev_num)
814{
815 struct swr_port_info *port_req = NULL;
816
817 list_for_each_entry(port_req, &mport->port_req_list, list) {
818 /* Store dev_id instead of dev_num if enumeration is changed run_time */
819 if ((port_req->slave_port_id == slv_port)
820 && (port_req->dev_num == dev_num))
821 return port_req;
822 }
823 return NULL;
824}
825
826static bool swrm_remove_from_group(struct swr_master *master)
827{
828 struct swr_device *swr_dev;
829 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
830 bool is_removed = false;
831
832 if (!swrm)
833 goto end;
834
835 mutex_lock(&swrm->mlock);
836 if ((swrm->num_rx_chs > 1) &&
837 (swrm->num_rx_chs == swrm->num_cfg_devs)) {
838 list_for_each_entry(swr_dev, &master->devices,
839 dev_list) {
840 swr_dev->group_id = SWR_GROUP_NONE;
841 master->gr_sid = 0;
842 }
843 is_removed = true;
844 }
845 mutex_unlock(&swrm->mlock);
846
847end:
848 return is_removed;
849}
850
851static void swrm_disable_ports(struct swr_master *master,
852 u8 bank)
853{
854 u32 value;
855 struct swr_port_info *port_req;
856 int i;
857 struct swrm_mports *mport;
858 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
859
860 if (!swrm) {
861 pr_err("%s: swrm is null\n", __func__);
862 return;
863 }
864
865 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
866 master->num_port);
867
868
869 for (i = 0; i < SWR_MSTR_PORT_LEN ; i++) {
870
871 mport = &(swrm->mport_cfg[i]);
872 if (!mport->port_en)
873 continue;
874
875 list_for_each_entry(port_req, &mport->port_req_list, list) {
876 /* skip ports with no change req's*/
877 if (port_req->req_ch == port_req->ch_en)
878 continue;
879
880 swrm_cmd_fifo_wr_cmd(swrm, port_req->req_ch,
881 port_req->dev_num, 0x00,
882 SWRS_DP_CHANNEL_ENABLE_BANK(port_req->slave_port_id,
883 bank));
884 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x\n",
885 __func__, i,
886 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)));
887 }
888 value = ((mport->req_ch)
889 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
890 value |= ((mport->offset2)
891 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
892 value |= ((mport->offset1)
893 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
894 value |= mport->sinterval;
895
896 swr_master_write(swrm,
897 SWRM_DP_PORT_CTRL_BANK(i+1, bank),
898 value);
899 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
900 __func__, i,
901 (SWRM_DP_PORT_CTRL_BANK(i+1, bank)), value);
902 }
903}
904
905static void swrm_cleanup_disabled_port_reqs(struct swr_master *master)
906{
907 struct swr_port_info *port_req, *next;
908 int i;
909 struct swrm_mports *mport;
910 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
911
912 if (!swrm) {
913 pr_err("%s: swrm is null\n", __func__);
914 return;
915 }
916 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
917 master->num_port);
918
919 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
920 mport = &(swrm->mport_cfg[i]);
921 list_for_each_entry_safe(port_req, next,
922 &mport->port_req_list, list) {
923 /* skip ports without new ch req */
924 if (port_req->ch_en == port_req->req_ch)
925 continue;
926
927 /* remove new ch req's*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +0530928 port_req->ch_en = port_req->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530929
930 /* If no streams enabled on port, remove the port req */
931 if (port_req->ch_en == 0) {
932 list_del(&port_req->list);
933 kfree(port_req);
934 }
935 }
936 /* remove new ch req's on mport*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +0530937 mport->ch_en = mport->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530938
939 if (!(mport->ch_en)) {
940 mport->port_en = false;
941 master->port_en_mask &= ~i;
942 }
943 }
944}
945static void swrm_copy_data_port_config(struct swr_master *master, u8 bank)
946{
947 u32 value, slv_id;
948 struct swr_port_info *port_req;
949 int i;
950 struct swrm_mports *mport;
951 u32 reg[SWRM_MAX_PORT_REG];
952 u32 val[SWRM_MAX_PORT_REG];
953 int len = 0;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530954 u8 hparams;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530955 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
956
957 if (!swrm) {
958 pr_err("%s: swrm is null\n", __func__);
959 return;
960 }
961
962 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
963 master->num_port);
964
965 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
966 mport = &(swrm->mport_cfg[i]);
967 if (!mport->port_en)
968 continue;
969
970 list_for_each_entry(port_req, &mport->port_req_list, list) {
971 slv_id = port_req->slave_port_id;
972 reg[len] = SWRM_CMD_FIFO_WR_CMD;
973 val[len++] = SWR_REG_VAL_PACK(port_req->req_ch,
974 port_req->dev_num, 0x00,
975 SWRS_DP_CHANNEL_ENABLE_BANK(slv_id,
976 bank));
977
978 reg[len] = SWRM_CMD_FIFO_WR_CMD;
979 val[len++] = SWR_REG_VAL_PACK(mport->sinterval,
980 port_req->dev_num, 0x00,
981 SWRS_DP_SAMPLE_CONTROL_1_BANK(slv_id,
982 bank));
983
984 reg[len] = SWRM_CMD_FIFO_WR_CMD;
985 val[len++] = SWR_REG_VAL_PACK(mport->offset1,
986 port_req->dev_num, 0x00,
987 SWRS_DP_OFFSET_CONTROL_1_BANK(slv_id,
988 bank));
989
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530990 if (mport->offset2 != SWR_INVALID_PARAM) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530991 reg[len] = SWRM_CMD_FIFO_WR_CMD;
992 val[len++] = SWR_REG_VAL_PACK(mport->offset2,
993 port_req->dev_num, 0x00,
994 SWRS_DP_OFFSET_CONTROL_2_BANK(
995 slv_id, bank));
996 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530997 if (mport->hstart != SWR_INVALID_PARAM
998 && mport->hstop != SWR_INVALID_PARAM) {
999 hparams = (mport->hstart << 4) | mport->hstop;
1000
1001 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1002 val[len++] = SWR_REG_VAL_PACK(hparams,
1003 port_req->dev_num, 0x00,
1004 SWRS_DP_HCONTROL_BANK(slv_id,
1005 bank));
1006 }
1007 if (mport->word_length != SWR_INVALID_PARAM) {
1008 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1009 val[len++] =
1010 SWR_REG_VAL_PACK(mport->word_length,
1011 port_req->dev_num, 0x00,
1012 SWRS_DP_BLOCK_CONTROL_1(slv_id));
1013 }
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +05301014 if (mport->blk_pack_mode != SWR_INVALID_PARAM
1015 && swrm->master_id != MASTER_ID_WSA) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301016 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1017 val[len++] =
1018 SWR_REG_VAL_PACK(mport->blk_pack_mode,
1019 port_req->dev_num, 0x00,
1020 SWRS_DP_BLOCK_CONTROL_3_BANK(slv_id,
1021 bank));
1022 }
1023 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
1024 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1025 val[len++] =
1026 SWR_REG_VAL_PACK(mport->blk_grp_count,
1027 port_req->dev_num, 0x00,
1028 SWRS_DP_BLOCK_CONTROL_2_BANK(slv_id,
1029 bank));
1030 }
1031 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
1032 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1033 val[len++] =
1034 SWR_REG_VAL_PACK(mport->lane_ctrl,
1035 port_req->dev_num, 0x00,
1036 SWRS_DP_LANE_CONTROL_BANK(slv_id,
1037 bank));
1038 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301039 port_req->ch_en = port_req->req_ch;
1040 }
1041 value = ((mport->req_ch)
1042 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +05301043
1044 if (mport->offset2 != SWR_INVALID_PARAM)
1045 value |= ((mport->offset2)
1046 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301047 value |= ((mport->offset1)
1048 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
1049 value |= mport->sinterval;
1050
1051
1052 reg[len] = SWRM_DP_PORT_CTRL_BANK(i + 1, bank);
1053 val[len++] = value;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301054 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
1055 __func__, i,
1056 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)), value);
1057
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301058 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
1059 reg[len] = SWRM_DP_PORT_CTRL_2_BANK(i + 1, bank);
1060 val[len++] = mport->lane_ctrl;
1061 }
1062 if (mport->word_length != SWR_INVALID_PARAM) {
1063 reg[len] = SWRM_DP_BLOCK_CTRL_1(i + 1);
1064 val[len++] = mport->word_length;
1065 }
1066
1067 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
1068 reg[len] = SWRM_DP_BLOCK_CTRL2_BANK(i + 1, bank);
1069 val[len++] = mport->blk_grp_count;
1070 }
1071 if (mport->hstart != SWR_INVALID_PARAM
1072 && mport->hstop != SWR_INVALID_PARAM) {
1073 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
Laxminath Kasame30eef72018-11-05 17:40:09 +05301074 hparams = (mport->hstop << 4) | mport->hstart;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301075 val[len++] = hparams;
Laxminath Kasam990c70b2018-11-09 23:15:09 +05301076 } else {
1077 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
1078 hparams = (SWR_HSTOP_MAX_VAL << 4) | SWR_HSTART_MIN_VAL;
1079 val[len++] = hparams;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301080 }
1081 if (mport->blk_pack_mode != SWR_INVALID_PARAM) {
1082 reg[len] = SWRM_DP_BLOCK_CTRL3_BANK(i + 1, bank);
1083 val[len++] = mport->blk_pack_mode;
1084 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301085 mport->ch_en = mport->req_ch;
1086
1087 }
Sudheer Papothi0016db12019-06-11 04:42:38 +05301088 swrm_reg_dump(swrm, reg, val, len, __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301089 swr_master_bulk_write(swrm, reg, val, len);
1090}
1091
1092static void swrm_apply_port_config(struct swr_master *master)
1093{
1094 u8 bank;
1095 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1096
1097 if (!swrm) {
1098 pr_err("%s: Invalid handle to swr controller\n",
1099 __func__);
1100 return;
1101 }
1102
1103 bank = get_inactive_bank_num(swrm);
1104 dev_dbg(swrm->dev, "%s: enter bank: %d master_ports: %d\n",
1105 __func__, bank, master->num_port);
1106
1107
1108 swrm_cmd_fifo_wr_cmd(swrm, 0x01, 0xF, 0x00,
1109 SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(bank));
1110
1111 swrm_copy_data_port_config(master, bank);
1112}
1113
1114static int swrm_slvdev_datapath_control(struct swr_master *master, bool enable)
1115{
1116 u8 bank;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301117 u32 value, n_row, n_col;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301118 u32 row = 0, col = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301119 int ret;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301120 u8 ssp_period = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301121 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1122 int mask = (SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK |
1123 SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK |
1124 SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_BMSK);
1125 u8 inactive_bank;
1126
1127 if (!swrm) {
1128 pr_err("%s: swrm is null\n", __func__);
1129 return -EFAULT;
1130 }
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301131
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301132 mutex_lock(&swrm->mlock);
1133
Ramprasad Katkam979b7c92019-05-17 15:31:21 +05301134 /*
1135 * During disable if master is already down, which implies an ssr/pdr
1136 * scenario, just mark ports as disabled and exit
1137 */
1138 if (swrm->state == SWR_MSTR_SSR && !enable) {
1139 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1140 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1141 __func__);
1142 goto exit;
1143 }
1144 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
1145 swrm_cleanup_disabled_port_reqs(master);
1146 if (!swrm_is_port_en(master)) {
1147 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1148 __func__);
1149 pm_runtime_mark_last_busy(swrm->dev);
1150 pm_runtime_put_autosuspend(swrm->dev);
1151 }
1152 goto exit;
1153 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301154 bank = get_inactive_bank_num(swrm);
1155
1156 if (enable) {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301157 if (!test_bit(ENABLE_PENDING, &swrm->port_req_pending)) {
1158 dev_dbg(swrm->dev, "%s:No pending connect port req\n",
1159 __func__);
1160 goto exit;
1161 }
1162 clear_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301163 ret = swrm_get_port_config(swrm);
1164 if (ret) {
1165 /* cannot accommodate ports */
1166 swrm_cleanup_disabled_port_reqs(master);
1167 mutex_unlock(&swrm->mlock);
1168 return -EINVAL;
1169 }
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301170 swr_master_write(swrm, SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301171 SWRM_INTERRUPT_STATUS_MASK);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301172 /* apply the new port config*/
1173 swrm_apply_port_config(master);
1174 } else {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301175 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1176 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1177 __func__);
1178 goto exit;
1179 }
1180 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301181 swrm_disable_ports(master, bank);
1182 }
1183 dev_dbg(swrm->dev, "%s: enable: %d, cfg_devs: %d\n",
1184 __func__, enable, swrm->num_cfg_devs);
1185
1186 if (enable) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301187 /* set col = 16 */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301188 n_col = SWR_MAX_COL;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301189 col = SWRM_COL_16;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301190 } else {
1191 /*
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301192 * Do not change to col = 2 if there are still active ports
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301193 */
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301194 if (!master->num_port) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301195 n_col = SWR_MIN_COL;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301196 col = SWRM_COL_02;
1197 } else {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301198 n_col = SWR_MAX_COL;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301199 col = SWRM_COL_16;
1200 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301201 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301202 /* Use default 50 * x, frame shape. Change based on mclk */
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301203 if (swrm->mclk_freq == MCLK_FREQ_NATIVE) {
1204 dev_dbg(swrm->dev, "setting 64 x %d frameshape\n",
1205 n_col ? 16 : 2);
1206 n_row = SWR_ROW_64;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301207 row = SWRM_ROW_64;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301208 } else {
1209 dev_dbg(swrm->dev, "setting 50 x %d frameshape\n",
1210 n_col ? 16 : 2);
1211 n_row = SWR_ROW_50;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301212 row = SWRM_ROW_50;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301213 }
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301214 ssp_period = swrm_get_ssp_period(swrm, row, col, SWRM_FRAME_SYNC_SEL);
1215 dev_dbg(swrm->dev, "%s: ssp_period: %d\n", __func__, ssp_period);
1216
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301217 value = swr_master_read(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank));
1218 value &= (~mask);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301219 value |= ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301220 (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301221 ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301222 swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1223
1224 dev_dbg(swrm->dev, "%s: regaddr: 0x%x, value: 0x%x\n", __func__,
1225 SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1226
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301227 enable_bank_switch(swrm, bank, n_row, n_col);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301228 inactive_bank = bank ? 0 : 1;
1229
1230 if (enable)
1231 swrm_copy_data_port_config(master, inactive_bank);
1232 else {
1233 swrm_disable_ports(master, inactive_bank);
1234 swrm_cleanup_disabled_port_reqs(master);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301235 }
1236 if (!swrm_is_port_en(master)) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301237 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1238 __func__);
1239 pm_runtime_mark_last_busy(swrm->dev);
1240 pm_runtime_put_autosuspend(swrm->dev);
1241 }
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301242exit:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301243 mutex_unlock(&swrm->mlock);
1244return 0;
1245}
1246
1247static int swrm_connect_port(struct swr_master *master,
1248 struct swr_params *portinfo)
1249{
1250 int i;
1251 struct swr_port_info *port_req;
1252 int ret = 0;
1253 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1254 struct swrm_mports *mport;
1255 u8 mstr_port_id, mstr_ch_msk;
1256
1257 dev_dbg(&master->dev, "%s: enter\n", __func__);
1258 if (!portinfo)
1259 return -EINVAL;
1260
1261 if (!swrm) {
1262 dev_err(&master->dev,
1263 "%s: Invalid handle to swr controller\n",
1264 __func__);
1265 return -EINVAL;
1266 }
1267
1268 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301269 mutex_lock(&swrm->devlock);
1270 if (!swrm->dev_up) {
1271 mutex_unlock(&swrm->devlock);
1272 mutex_unlock(&swrm->mlock);
1273 return -EINVAL;
1274 }
1275 mutex_unlock(&swrm->devlock);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301276 if (!swrm_is_port_en(master))
1277 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301278
1279 for (i = 0; i < portinfo->num_port; i++) {
1280 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_msk,
1281 portinfo->port_type[i],
1282 portinfo->port_id[i]);
1283 if (ret) {
1284 dev_err(&master->dev,
1285 "%s: mstr portid for slv port %d not found\n",
1286 __func__, portinfo->port_id[i]);
1287 goto port_fail;
1288 }
1289
1290 mport = &(swrm->mport_cfg[mstr_port_id]);
1291 /* get port req */
1292 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1293 portinfo->dev_num);
1294 if (!port_req) {
1295 dev_dbg(&master->dev, "%s: new req:port id %d dev %d\n",
1296 __func__, portinfo->port_id[i],
1297 portinfo->dev_num);
1298 port_req = kzalloc(sizeof(struct swr_port_info),
1299 GFP_KERNEL);
1300 if (!port_req) {
1301 ret = -ENOMEM;
1302 goto mem_fail;
1303 }
1304 port_req->dev_num = portinfo->dev_num;
1305 port_req->slave_port_id = portinfo->port_id[i];
1306 port_req->num_ch = portinfo->num_ch[i];
1307 port_req->ch_rate = portinfo->ch_rate[i];
1308 port_req->ch_en = 0;
1309 port_req->master_port_id = mstr_port_id;
1310 list_add(&port_req->list, &mport->port_req_list);
1311 }
1312 port_req->req_ch |= portinfo->ch_en[i];
1313
1314 dev_dbg(&master->dev,
1315 "%s: mstr port %d, slv port %d ch_rate %d num_ch %d\n",
1316 __func__, port_req->master_port_id,
1317 port_req->slave_port_id, port_req->ch_rate,
1318 port_req->num_ch);
1319 /* Put the port req on master port */
1320 mport = &(swrm->mport_cfg[mstr_port_id]);
1321 mport->port_en = true;
1322 mport->req_ch |= mstr_ch_msk;
1323 master->port_en_mask |= (1 << mstr_port_id);
1324 }
1325 master->num_port += portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301326 set_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301327 swr_port_response(master, portinfo->tid);
1328
1329 mutex_unlock(&swrm->mlock);
1330 return 0;
1331
1332port_fail:
1333mem_fail:
1334 /* cleanup port reqs in error condition */
1335 swrm_cleanup_disabled_port_reqs(master);
1336 mutex_unlock(&swrm->mlock);
1337 return ret;
1338}
1339
1340static int swrm_disconnect_port(struct swr_master *master,
1341 struct swr_params *portinfo)
1342{
1343 int i, ret = 0;
1344 struct swr_port_info *port_req;
1345 struct swrm_mports *mport;
1346 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1347 u8 mstr_port_id, mstr_ch_mask;
1348
1349 if (!swrm) {
1350 dev_err(&master->dev,
1351 "%s: Invalid handle to swr controller\n",
1352 __func__);
1353 return -EINVAL;
1354 }
1355
1356 if (!portinfo) {
1357 dev_err(&master->dev, "%s: portinfo is NULL\n", __func__);
1358 return -EINVAL;
1359 }
1360 mutex_lock(&swrm->mlock);
1361
1362 for (i = 0; i < portinfo->num_port; i++) {
1363
1364 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_mask,
1365 portinfo->port_type[i], portinfo->port_id[i]);
1366 if (ret) {
1367 dev_err(&master->dev,
1368 "%s: mstr portid for slv port %d not found\n",
1369 __func__, portinfo->port_id[i]);
1370 mutex_unlock(&swrm->mlock);
1371 return -EINVAL;
1372 }
1373 mport = &(swrm->mport_cfg[mstr_port_id]);
1374 /* get port req */
1375 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1376 portinfo->dev_num);
1377
1378 if (!port_req) {
1379 dev_err(&master->dev, "%s:port not enabled : port %d\n",
1380 __func__, portinfo->port_id[i]);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05301381 mutex_unlock(&swrm->mlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301382 return -EINVAL;
1383 }
1384 port_req->req_ch &= ~portinfo->ch_en[i];
1385 mport->req_ch &= ~mstr_ch_mask;
1386 }
1387 master->num_port -= portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301388 set_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301389 swr_port_response(master, portinfo->tid);
1390 mutex_unlock(&swrm->mlock);
1391
1392 return 0;
1393}
1394
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301395static int swrm_find_alert_slave(struct swr_mstr_ctrl *swrm,
1396 int status, u8 *devnum)
1397{
1398 int i;
1399 bool found = false;
1400
1401 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1402 if ((status & SWRM_MCP_SLV_STATUS_MASK) == SWR_ALERT) {
1403 *devnum = i;
1404 found = true;
1405 break;
1406 }
1407 status >>= 2;
1408 }
1409 if (found)
1410 return 0;
1411 else
1412 return -EINVAL;
1413}
1414
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301415static void swrm_enable_slave_irq(struct swr_mstr_ctrl *swrm)
1416{
1417 int i;
1418 int status = 0;
1419
1420 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1421 if (!status) {
1422 dev_dbg_ratelimited(swrm->dev, "%s: slaves status is 0x%x\n",
1423 __func__, status);
1424 return;
1425 }
1426 dev_dbg(swrm->dev, "%s: slave status: 0x%x\n", __func__, status);
1427 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1428 if (status & SWRM_MCP_SLV_STATUS_MASK)
1429 swrm_cmd_fifo_wr_cmd(swrm, 0x4, i, 0x0,
1430 SWRS_SCP_INT_STATUS_MASK_1);
1431 status >>= 2;
1432 }
1433}
1434
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301435static int swrm_check_slave_change_status(struct swr_mstr_ctrl *swrm,
1436 int status, u8 *devnum)
1437{
1438 int i;
1439 int new_sts = status;
1440 int ret = SWR_NOT_PRESENT;
1441
1442 if (status != swrm->slave_status) {
1443 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1444 if ((status & SWRM_MCP_SLV_STATUS_MASK) !=
1445 (swrm->slave_status & SWRM_MCP_SLV_STATUS_MASK)) {
1446 ret = (status & SWRM_MCP_SLV_STATUS_MASK);
1447 *devnum = i;
1448 break;
1449 }
1450 status >>= 2;
1451 swrm->slave_status >>= 2;
1452 }
1453 swrm->slave_status = new_sts;
1454 }
1455 return ret;
1456}
1457
1458static irqreturn_t swr_mstr_interrupt(int irq, void *dev)
1459{
1460 struct swr_mstr_ctrl *swrm = dev;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301461 u32 value, intr_sts, intr_sts_masked;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301462 u32 temp = 0;
1463 u32 status, chg_sts, i;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301464 u8 devnum = 0;
1465 int ret = IRQ_HANDLED;
1466 struct swr_device *swr_dev;
1467 struct swr_master *mstr = &swrm->master;
1468
Ramprasad Katkam57349872018-11-11 18:34:57 +05301469 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1470 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1471 return IRQ_NONE;
1472 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301473
1474 mutex_lock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301475 if (swrm_clk_request(swrm, true)) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +05301476 dev_err_ratelimited(swrm->dev, "%s:clk request failed\n",
1477 __func__);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301478 mutex_unlock(&swrm->reslock);
1479 goto exit;
1480 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301481 mutex_unlock(&swrm->reslock);
1482
1483 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301484 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301485handle_irq:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301486 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301487 value = intr_sts_masked & (1 << i);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301488 if (!value)
1489 continue;
1490
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301491 switch (value) {
1492 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1493 dev_dbg(swrm->dev, "Trigger irq to slave device\n");
1494 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301495 ret = swrm_find_alert_slave(swrm, status, &devnum);
1496 if (ret) {
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301497 dev_err_ratelimited(swrm->dev,
1498 "no slave alert found.spurious interrupt\n");
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05301499 break;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301500 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301501 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1502 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1503 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1504 SWRS_SCP_INT_STATUS_CLEAR_1);
1505 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1506 SWRS_SCP_INT_STATUS_CLEAR_1);
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301507
1508
1509 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1510 if (swr_dev->dev_num != devnum)
1511 continue;
1512 if (swr_dev->slave_irq) {
1513 do {
Ramprasad Katkam2586a4b2019-03-18 16:53:39 +05301514 swr_dev->slave_irq_pending = 0;
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301515 handle_nested_irq(
1516 irq_find_mapping(
1517 swr_dev->slave_irq, 0));
1518 } while (swr_dev->slave_irq_pending);
1519 }
1520
1521 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301522 break;
1523 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1524 dev_dbg(swrm->dev, "SWR new slave attached\n");
1525 break;
1526 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1527 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1528 if (status == swrm->slave_status) {
1529 dev_dbg(swrm->dev,
1530 "%s: No change in slave status: %d\n",
1531 __func__, status);
1532 break;
1533 }
1534 chg_sts = swrm_check_slave_change_status(swrm, status,
1535 &devnum);
1536 switch (chg_sts) {
1537 case SWR_NOT_PRESENT:
1538 dev_dbg(swrm->dev, "device %d got detached\n",
1539 devnum);
1540 break;
1541 case SWR_ATTACHED_OK:
1542 dev_dbg(swrm->dev, "device %d got attached\n",
1543 devnum);
Ramprasad Katkamdebe8932018-09-25 18:08:18 +05301544 /* enable host irq from slave device*/
1545 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1546 SWRS_SCP_INT_STATUS_CLEAR_1);
1547 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1548 SWRS_SCP_INT_STATUS_MASK_1);
1549
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301550 break;
1551 case SWR_ALERT:
1552 dev_dbg(swrm->dev,
1553 "device %d has pending interrupt\n",
1554 devnum);
1555 break;
1556 }
1557 break;
1558 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1559 dev_err_ratelimited(swrm->dev,
1560 "SWR bus clsh detected\n");
1561 break;
1562 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1563 dev_dbg(swrm->dev, "SWR read FIFO overflow\n");
1564 break;
1565 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1566 dev_dbg(swrm->dev, "SWR read FIFO underflow\n");
1567 break;
1568 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1569 dev_dbg(swrm->dev, "SWR write FIFO overflow\n");
1570 break;
1571 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1572 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1573 dev_err_ratelimited(swrm->dev,
1574 "SWR CMD error, fifo status 0x%x, flushing fifo\n",
1575 value);
1576 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1577 break;
1578 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301579 dev_err_ratelimited(swrm->dev, "SWR Port collision detected\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301580 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301581 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301582 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301583 break;
1584 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1585 dev_dbg(swrm->dev, "SWR read enable valid mismatch\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301586 swrm->intr_mask &=
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301587 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1588 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301589 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301590 break;
1591 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1592 complete(&swrm->broadcast);
1593 dev_dbg(swrm->dev, "SWR cmd id finished\n");
1594 break;
1595 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_AUTO_ENUM_FINISHED:
1596 break;
1597 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED:
1598 break;
1599 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL:
1600 break;
1601 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED:
1602 complete(&swrm->reset);
1603 break;
1604 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED:
1605 break;
1606 default:
1607 dev_err_ratelimited(swrm->dev,
1608 "SWR unknown interrupt\n");
1609 ret = IRQ_NONE;
1610 break;
1611 }
1612 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301613 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1614 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
Ramprasad Katkam83303512018-10-11 17:34:22 +05301615
1616 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301617 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301618
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301619 if (intr_sts_masked) {
Ramprasad Katkam83303512018-10-11 17:34:22 +05301620 dev_dbg(swrm->dev, "%s: new interrupt received\n", __func__);
1621 goto handle_irq;
1622 }
1623
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301624 mutex_lock(&swrm->reslock);
1625 swrm_clk_request(swrm, false);
1626 mutex_unlock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301627exit:
Ramprasad Katkam57349872018-11-11 18:34:57 +05301628 swrm_unlock_sleep(swrm);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301629 return ret;
1630}
1631
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301632static irqreturn_t swr_mstr_interrupt_v2(int irq, void *dev)
1633{
1634 struct swr_mstr_ctrl *swrm = dev;
1635 u32 value, intr_sts, intr_sts_masked;
1636 u32 temp = 0;
1637 u32 status, chg_sts, i;
1638 u8 devnum = 0;
1639 int ret = IRQ_HANDLED;
1640 struct swr_device *swr_dev;
1641 struct swr_master *mstr = &swrm->master;
1642
Karthikeyan Mani4bee1db2019-09-18 17:58:41 -07001643 if (!swrm->dev_up || swrm->state == SWR_MSTR_SSR) {
1644 complete(&swrm->broadcast);
1645 dev_dbg(swrm->dev, "%s swrm is not up\n", __func__);
1646 return IRQ_NONE;
1647 }
1648
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301649 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1650 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1651 return IRQ_NONE;
1652 }
1653
1654 mutex_lock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301655 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
1656 ret = IRQ_NONE;
1657 goto exit;
1658 }
1659 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
1660 ret = IRQ_NONE;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301661 goto err_audio_hw_vote;
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001662 }
Karthikeyan Mani4bee1db2019-09-18 17:58:41 -07001663 ret = swrm_clk_request(swrm, true);
1664 if (ret) {
1665 dev_err(dev, "%s: swrm clk failed\n", __func__);
1666 ret = IRQ_NONE;
1667 goto err_audio_core_vote;
1668 }
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301669 mutex_unlock(&swrm->reslock);
1670
1671 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1672 intr_sts_masked = intr_sts & swrm->intr_mask;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301673
1674 dev_dbg(swrm->dev, "%s: status: 0x%x \n", __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301675handle_irq:
1676 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
1677 value = intr_sts_masked & (1 << i);
1678 if (!value)
1679 continue;
1680
1681 switch (value) {
1682 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1683 dev_dbg(swrm->dev, "%s: Trigger irq to slave device\n",
1684 __func__);
1685 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1686 ret = swrm_find_alert_slave(swrm, status, &devnum);
1687 if (ret) {
1688 dev_err_ratelimited(swrm->dev,
1689 "%s: no slave alert found.spurious interrupt\n",
1690 __func__);
1691 break;
1692 }
1693 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1694 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1695 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1696 SWRS_SCP_INT_STATUS_CLEAR_1);
1697 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1698 SWRS_SCP_INT_STATUS_CLEAR_1);
1699
1700
1701 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1702 if (swr_dev->dev_num != devnum)
1703 continue;
1704 if (swr_dev->slave_irq) {
1705 do {
1706 handle_nested_irq(
1707 irq_find_mapping(
1708 swr_dev->slave_irq, 0));
1709 } while (swr_dev->slave_irq_pending);
1710 }
1711
1712 }
1713 break;
1714 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1715 dev_dbg(swrm->dev, "%s: SWR new slave attached\n",
1716 __func__);
1717 break;
1718 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1719 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1720 if (status == swrm->slave_status) {
1721 dev_dbg(swrm->dev,
1722 "%s: No change in slave status: %d\n",
1723 __func__, status);
1724 break;
1725 }
1726 chg_sts = swrm_check_slave_change_status(swrm, status,
1727 &devnum);
1728 switch (chg_sts) {
1729 case SWR_NOT_PRESENT:
1730 dev_dbg(swrm->dev,
1731 "%s: device %d got detached\n",
1732 __func__, devnum);
1733 break;
1734 case SWR_ATTACHED_OK:
1735 dev_dbg(swrm->dev,
1736 "%s: device %d got attached\n",
1737 __func__, devnum);
1738 /* enable host irq from slave device*/
1739 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1740 SWRS_SCP_INT_STATUS_CLEAR_1);
1741 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1742 SWRS_SCP_INT_STATUS_MASK_1);
1743
1744 break;
1745 case SWR_ALERT:
1746 dev_dbg(swrm->dev,
1747 "%s: device %d has pending interrupt\n",
1748 __func__, devnum);
1749 break;
1750 }
1751 break;
1752 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1753 dev_err_ratelimited(swrm->dev,
1754 "%s: SWR bus clsh detected\n",
1755 __func__);
1756 break;
1757 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1758 dev_dbg(swrm->dev, "%s: SWR read FIFO overflow\n",
1759 __func__);
1760 break;
1761 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1762 dev_dbg(swrm->dev, "%s: SWR read FIFO underflow\n",
1763 __func__);
1764 break;
1765 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1766 dev_dbg(swrm->dev, "%s: SWR write FIFO overflow\n",
1767 __func__);
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301768 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301769 break;
1770 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1771 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1772 dev_err_ratelimited(swrm->dev,
1773 "%s: SWR CMD error, fifo status 0x%x, flushing fifo\n",
1774 __func__, value);
1775 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1776 break;
1777 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
1778 dev_err_ratelimited(swrm->dev,
1779 "%s: SWR Port collision detected\n",
1780 __func__);
1781 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
1782 swr_master_write(swrm,
1783 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1784 break;
1785 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1786 dev_dbg(swrm->dev,
1787 "%s: SWR read enable valid mismatch\n",
1788 __func__);
1789 swrm->intr_mask &=
1790 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1791 swr_master_write(swrm,
1792 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1793 break;
1794 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1795 complete(&swrm->broadcast);
1796 dev_dbg(swrm->dev, "%s: SWR cmd id finished\n",
1797 __func__);
1798 break;
1799 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED_V2:
1800 break;
1801 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL_V2:
1802 break;
1803 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED_V2:
1804 break;
1805 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED_V2:
1806 break;
1807 case SWRM_INTERRUPT_STATUS_EXT_CLK_STOP_WAKEUP:
1808 if (swrm->state == SWR_MSTR_UP)
1809 dev_dbg(swrm->dev,
1810 "%s:SWR Master is already up\n",
1811 __func__);
1812 else
1813 dev_err_ratelimited(swrm->dev,
1814 "%s: SWR wokeup during clock stop\n",
1815 __func__);
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301816 /* It might be possible the slave device gets reset
1817 * and slave interrupt gets missed. So re-enable
1818 * Host IRQ and process slave pending
1819 * interrupts, if any.
1820 */
1821 swrm_enable_slave_irq(swrm);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301822 break;
1823 default:
1824 dev_err_ratelimited(swrm->dev,
1825 "%s: SWR unknown interrupt value: %d\n",
1826 __func__, value);
1827 ret = IRQ_NONE;
1828 break;
1829 }
1830 }
1831 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1832 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
1833
1834 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1835 intr_sts_masked = intr_sts & swrm->intr_mask;
1836
1837 if (intr_sts_masked) {
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301838 dev_dbg(swrm->dev, "%s: new interrupt received 0x%x\n",
1839 __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301840 goto handle_irq;
1841 }
1842
1843 mutex_lock(&swrm->reslock);
1844 swrm_clk_request(swrm, false);
Karthikeyan Mani4bee1db2019-09-18 17:58:41 -07001845err_audio_core_vote:
Sudheer Papothi384addd2019-06-14 02:26:52 +05301846 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
Sudheer Papothi06f43412019-07-09 03:32:54 +05301847
1848err_audio_hw_vote:
Sudheer Papothi384addd2019-06-14 02:26:52 +05301849 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001850exit:
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301851 mutex_unlock(&swrm->reslock);
1852 swrm_unlock_sleep(swrm);
1853 return ret;
1854}
1855
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301856static irqreturn_t swrm_wakeup_interrupt(int irq, void *dev)
1857{
1858 struct swr_mstr_ctrl *swrm = dev;
1859 int ret = IRQ_HANDLED;
1860
1861 if (!swrm || !(swrm->dev)) {
1862 pr_err("%s: swrm or dev is null\n", __func__);
1863 return IRQ_NONE;
1864 }
1865 mutex_lock(&swrm->devlock);
1866 if (!swrm->dev_up) {
1867 if (swrm->wake_irq > 0)
1868 disable_irq_nosync(swrm->wake_irq);
1869 mutex_unlock(&swrm->devlock);
1870 return ret;
1871 }
1872 mutex_unlock(&swrm->devlock);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301873 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1874 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1875 goto exit;
1876 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301877 if (swrm->wake_irq > 0)
1878 disable_irq_nosync(swrm->wake_irq);
1879 pm_runtime_get_sync(swrm->dev);
1880 pm_runtime_mark_last_busy(swrm->dev);
1881 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301882 swrm_unlock_sleep(swrm);
1883exit:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301884 return ret;
1885}
1886
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301887static void swrm_wakeup_work(struct work_struct *work)
1888{
1889 struct swr_mstr_ctrl *swrm;
1890
1891 swrm = container_of(work, struct swr_mstr_ctrl,
1892 wakeup_work);
1893 if (!swrm || !(swrm->dev)) {
1894 pr_err("%s: swrm or dev is null\n", __func__);
1895 return;
1896 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301897
1898 mutex_lock(&swrm->devlock);
1899 if (!swrm->dev_up) {
1900 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301901 goto exit;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301902 }
1903 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301904 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1905 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1906 goto exit;
1907 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301908 pm_runtime_get_sync(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301909 pm_runtime_mark_last_busy(swrm->dev);
1910 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301911 swrm_unlock_sleep(swrm);
1912exit:
1913 pm_relax(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301914}
1915
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301916static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
1917{
1918 u32 val;
1919
1920 swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1921 val = (swrm->slave_status >> (devnum * 2));
1922 val &= SWRM_MCP_SLV_STATUS_MASK;
1923 return val;
1924}
1925
1926static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
1927 u8 *dev_num)
1928{
1929 int i;
1930 u64 id = 0;
1931 int ret = -EINVAL;
1932 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1933 struct swr_device *swr_dev;
1934 u32 num_dev = 0;
1935
1936 if (!swrm) {
1937 pr_err("%s: Invalid handle to swr controller\n",
1938 __func__);
1939 return ret;
1940 }
1941 if (swrm->num_dev)
1942 num_dev = swrm->num_dev;
1943 else
1944 num_dev = mstr->num_dev;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301945
1946 mutex_lock(&swrm->devlock);
1947 if (!swrm->dev_up) {
1948 mutex_unlock(&swrm->devlock);
1949 return ret;
1950 }
1951 mutex_unlock(&swrm->devlock);
1952
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301953 pm_runtime_get_sync(swrm->dev);
1954 for (i = 1; i < (num_dev + 1); i++) {
1955 id = ((u64)(swr_master_read(swrm,
1956 SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
1957 id |= swr_master_read(swrm,
1958 SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301959
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301960 /*
1961 * As pm_runtime_get_sync() brings all slaves out of reset
1962 * update logical device number for all slaves.
1963 */
1964 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1965 if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
1966 u32 status = swrm_get_device_status(swrm, i);
1967
1968 if ((status == 0x01) || (status == 0x02)) {
1969 swr_dev->dev_num = i;
1970 if ((id & SWR_DEV_ID_MASK) == dev_id) {
1971 *dev_num = i;
1972 ret = 0;
1973 }
1974 dev_dbg(swrm->dev,
1975 "%s: devnum %d is assigned for dev addr %lx\n",
1976 __func__, i, swr_dev->addr);
1977 }
1978 }
1979 }
1980 }
1981 if (ret)
1982 dev_err(swrm->dev, "%s: device 0x%llx is not ready\n",
1983 __func__, dev_id);
1984
1985 pm_runtime_mark_last_busy(swrm->dev);
1986 pm_runtime_put_autosuspend(swrm->dev);
1987 return ret;
1988}
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301989
1990static void swrm_device_wakeup_vote(struct swr_master *mstr)
1991{
1992 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1993
1994 if (!swrm) {
1995 pr_err("%s: Invalid handle to swr controller\n",
1996 __func__);
1997 return;
1998 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05301999 if (unlikely(swrm_lock_sleep(swrm) == false)) {
2000 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
2001 return;
2002 }
Sudheer Papothi384addd2019-06-14 02:26:52 +05302003 if (++swrm->hw_core_clk_en == 1)
2004 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2005 dev_err(swrm->dev, "%s:lpass core hw enable failed\n",
2006 __func__);
2007 --swrm->hw_core_clk_en;
2008 }
2009 if ( ++swrm->aud_core_clk_en == 1)
2010 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2011 dev_err(swrm->dev, "%s:lpass audio hw enable failed\n",
2012 __func__);
2013 --swrm->aud_core_clk_en;
2014 }
2015 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
2016 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302017 pm_runtime_get_sync(swrm->dev);
2018}
2019
2020static void swrm_device_wakeup_unvote(struct swr_master *mstr)
2021{
2022 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
2023
2024 if (!swrm) {
2025 pr_err("%s: Invalid handle to swr controller\n",
2026 __func__);
2027 return;
2028 }
2029 pm_runtime_mark_last_busy(swrm->dev);
2030 pm_runtime_put_autosuspend(swrm->dev);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302031 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
2032 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
2033
2034 --swrm->aud_core_clk_en;
2035 if (swrm->aud_core_clk_en < 0)
2036 swrm->aud_core_clk_en = 0;
2037 else if (swrm->aud_core_clk_en == 0)
2038 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2039
2040 --swrm->hw_core_clk_en;
2041 if (swrm->hw_core_clk_en < 0)
2042 swrm->hw_core_clk_en = 0;
2043 else if (swrm->hw_core_clk_en == 0)
2044 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
2045
Ramprasad Katkam57349872018-11-11 18:34:57 +05302046 swrm_unlock_sleep(swrm);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302047}
2048
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302049static int swrm_master_init(struct swr_mstr_ctrl *swrm)
2050{
2051 int ret = 0;
2052 u32 val;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302053 u8 row_ctrl = SWR_ROW_50;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302054 u8 col_ctrl = SWR_MIN_COL;
2055 u8 ssp_period = 1;
2056 u8 retry_cmd_num = 3;
2057 u32 reg[SWRM_MAX_INIT_REG];
2058 u32 value[SWRM_MAX_INIT_REG];
2059 int len = 0;
2060
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302061 ssp_period = swrm_get_ssp_period(swrm, SWRM_ROW_50,
2062 SWRM_COL_02, SWRM_FRAME_SYNC_SEL);
2063 dev_dbg(swrm->dev, "%s: ssp_period: %d\n", __func__, ssp_period);
2064
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302065 /* Clear Rows and Cols */
2066 val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
2067 (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302068 ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302069
2070 reg[len] = SWRM_MCP_FRAME_CTRL_BANK_ADDR(0);
2071 value[len++] = val;
2072
2073 /* Set Auto enumeration flag */
2074 reg[len] = SWRM_ENUMERATOR_CFG_ADDR;
2075 value[len++] = 1;
2076
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302077 /* Configure No pings */
2078 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2079 val &= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK;
2080 val |= (0x1f << SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT);
2081 reg[len] = SWRM_MCP_CFG_ADDR;
2082 value[len++] = val;
2083
2084 /* Configure number of retries of a read/write cmd */
2085 val = (retry_cmd_num << SWRM_CMD_FIFO_CFG_NUM_OF_CMD_RETRY_SHFT);
2086 reg[len] = SWRM_CMD_FIFO_CFG_ADDR;
2087 value[len++] = val;
2088
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302089 reg[len] = SWRM_MCP_BUS_CTRL_ADDR;
2090 value[len++] = 0x2;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302091
Ramprasad Katkam83303512018-10-11 17:34:22 +05302092 /* Set IRQ to PULSE */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302093 reg[len] = SWRM_COMP_CFG_ADDR;
Ramprasad Katkam83303512018-10-11 17:34:22 +05302094 value[len++] = 0x02;
2095
2096 reg[len] = SWRM_COMP_CFG_ADDR;
2097 value[len++] = 0x03;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302098
2099 reg[len] = SWRM_INTERRUPT_CLEAR;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302100 value[len++] = 0xFFFFFFFF;
2101
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302102 swrm->intr_mask = SWRM_INTERRUPT_STATUS_MASK;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302103 /* Mask soundwire interrupts */
2104 reg[len] = SWRM_INTERRUPT_MASK_ADDR;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302105 value[len++] = swrm->intr_mask;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302106
2107 reg[len] = SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302108 value[len++] = swrm->intr_mask;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302109
2110 swr_master_bulk_write(swrm, reg, value, len);
2111
Sudheer Papothi63f48152018-11-15 01:08:03 +05302112 /*
2113 * For SWR master version 1.5.1, continue
2114 * execute on command ignore.
2115 */
2116 if (swrm->version == SWRM_VERSION_1_5_1)
2117 swr_master_write(swrm, SWRM_CMD_FIFO_CFG_ADDR,
2118 (swr_master_read(swrm,
2119 SWRM_CMD_FIFO_CFG_ADDR) | 0x80000000));
2120
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302121 return ret;
2122}
2123
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302124static int swrm_event_notify(struct notifier_block *self,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302125 unsigned long action, void *data)
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302126{
2127 struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302128 event_notifier);
2129
2130 if (!swrm || !(swrm->dev)) {
2131 pr_err("%s: swrm or dev is NULL\n", __func__);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302132 return -EINVAL;
2133 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302134 switch (action) {
2135 case MSM_AUD_DC_EVENT:
2136 schedule_work(&(swrm->dc_presence_work));
2137 break;
2138 case SWR_WAKE_IRQ_EVENT:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302139 if (swrm->ipc_wakeup && !swrm->ipc_wakeup_triggered) {
2140 swrm->ipc_wakeup_triggered = true;
Ramprasad Katkam57349872018-11-11 18:34:57 +05302141 pm_stay_awake(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302142 schedule_work(&swrm->wakeup_work);
Ramprasad Katkamcd61c6e2018-09-18 13:22:58 +05302143 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302144 break;
2145 default:
2146 dev_err(swrm->dev, "%s: invalid event type: %lu\n",
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302147 __func__, action);
2148 return -EINVAL;
2149 }
2150
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302151 return 0;
2152}
2153
2154static void swrm_notify_work_fn(struct work_struct *work)
2155{
2156 struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302157 dc_presence_work);
2158
2159 if (!swrm || !swrm->pdev) {
2160 pr_err("%s: swrm or pdev is NULL\n", __func__);
2161 return;
2162 }
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302163 swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL);
2164}
2165
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302166static int swrm_probe(struct platform_device *pdev)
2167{
2168 struct swr_mstr_ctrl *swrm;
2169 struct swr_ctrl_platform_data *pdata;
2170 u32 i, num_ports, port_num, port_type, ch_mask;
2171 u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0;
2172 int ret = 0;
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302173 struct clk *lpass_core_hw_vote = NULL;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302174 struct clk *lpass_core_audio = NULL;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302175
2176 /* Allocate soundwire master driver structure */
2177 swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl),
2178 GFP_KERNEL);
2179 if (!swrm) {
2180 ret = -ENOMEM;
2181 goto err_memory_fail;
2182 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302183 swrm->pdev = pdev;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302184 swrm->dev = &pdev->dev;
2185 platform_set_drvdata(pdev, swrm);
2186 swr_set_ctrl_data(&swrm->master, swrm);
2187 pdata = dev_get_platdata(&pdev->dev);
2188 if (!pdata) {
2189 dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
2190 __func__);
2191 ret = -EINVAL;
2192 goto err_pdata_fail;
2193 }
2194 swrm->handle = (void *)pdata->handle;
2195 if (!swrm->handle) {
2196 dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
2197 __func__);
2198 ret = -EINVAL;
2199 goto err_pdata_fail;
2200 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302201 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr_master_id",
2202 &swrm->master_id);
2203 if (ret) {
2204 dev_err(&pdev->dev, "%s: failed to get master id\n", __func__);
2205 goto err_pdata_fail;
2206 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302207 if (!(of_property_read_u32(pdev->dev.of_node,
2208 "swrm-io-base", &swrm->swrm_base_reg)))
2209 ret = of_property_read_u32(pdev->dev.of_node,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302210 "swrm-io-base", &swrm->swrm_base_reg);
2211 if (!swrm->swrm_base_reg) {
2212 swrm->read = pdata->read;
2213 if (!swrm->read) {
2214 dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
2215 __func__);
2216 ret = -EINVAL;
2217 goto err_pdata_fail;
2218 }
2219 swrm->write = pdata->write;
2220 if (!swrm->write) {
2221 dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
2222 __func__);
2223 ret = -EINVAL;
2224 goto err_pdata_fail;
2225 }
2226 swrm->bulk_write = pdata->bulk_write;
2227 if (!swrm->bulk_write) {
2228 dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
2229 __func__);
2230 ret = -EINVAL;
2231 goto err_pdata_fail;
2232 }
2233 } else {
2234 swrm->swrm_dig_base = devm_ioremap(&pdev->dev,
2235 swrm->swrm_base_reg, SWRM_MAX_REGISTER);
2236 }
2237
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07002238 swrm->core_vote = pdata->core_vote;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302239 swrm->clk = pdata->clk;
2240 if (!swrm->clk) {
2241 dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
2242 __func__);
2243 ret = -EINVAL;
2244 goto err_pdata_fail;
2245 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302246 if (of_property_read_u32(pdev->dev.of_node,
2247 "qcom,swr-clock-stop-mode0",
2248 &swrm->clk_stop_mode0_supp)) {
2249 swrm->clk_stop_mode0_supp = FALSE;
2250 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302251
2252 ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
2253 &swrm->num_dev);
2254 if (ret) {
2255 dev_dbg(&pdev->dev, "%s: Looking up %s property failed\n",
2256 __func__, "qcom,swr-num-dev");
2257 } else {
2258 if (swrm->num_dev > SWR_MAX_SLAVE_DEVICES) {
2259 dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
2260 __func__, swrm->num_dev, SWR_MAX_SLAVE_DEVICES);
2261 ret = -EINVAL;
2262 goto err_pdata_fail;
2263 }
2264 }
2265
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302266 /* Parse soundwire port mapping */
2267 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
2268 &num_ports);
2269 if (ret) {
2270 dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
2271 goto err_pdata_fail;
2272 }
2273 swrm->num_ports = num_ports;
2274
2275 if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
2276 &map_size)) {
2277 dev_err(swrm->dev, "missing port mapping\n");
2278 goto err_pdata_fail;
2279 }
2280
2281 map_length = map_size / (3 * sizeof(u32));
2282 if (num_ports > SWR_MSTR_PORT_LEN) {
2283 dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
2284 __func__);
2285 ret = -EINVAL;
2286 goto err_pdata_fail;
2287 }
2288 temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
2289
2290 if (!temp) {
2291 ret = -ENOMEM;
2292 goto err_pdata_fail;
2293 }
2294 ret = of_property_read_u32_array(pdev->dev.of_node,
2295 "qcom,swr-port-mapping", temp, 3 * map_length);
2296 if (ret) {
2297 dev_err(swrm->dev, "%s: Failed to read port mapping\n",
2298 __func__);
2299 goto err_pdata_fail;
2300 }
2301
2302 for (i = 0; i < map_length; i++) {
2303 port_num = temp[3 * i];
2304 port_type = temp[3 * i + 1];
2305 ch_mask = temp[3 * i + 2];
2306
2307 if (port_num != old_port_num)
2308 ch_iter = 0;
2309 swrm->port_mapping[port_num][ch_iter].port_type = port_type;
2310 swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
2311 old_port_num = port_num;
2312 }
2313 devm_kfree(&pdev->dev, temp);
2314
2315 swrm->reg_irq = pdata->reg_irq;
2316 swrm->master.read = swrm_read;
2317 swrm->master.write = swrm_write;
2318 swrm->master.bulk_write = swrm_bulk_write;
2319 swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
2320 swrm->master.connect_port = swrm_connect_port;
2321 swrm->master.disconnect_port = swrm_disconnect_port;
2322 swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
2323 swrm->master.remove_from_group = swrm_remove_from_group;
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302324 swrm->master.device_wakeup_vote = swrm_device_wakeup_vote;
2325 swrm->master.device_wakeup_unvote = swrm_device_wakeup_unvote;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302326 swrm->master.dev.parent = &pdev->dev;
2327 swrm->master.dev.of_node = pdev->dev.of_node;
2328 swrm->master.num_port = 0;
2329 swrm->rcmd_id = 0;
2330 swrm->wcmd_id = 0;
2331 swrm->slave_status = 0;
2332 swrm->num_rx_chs = 0;
2333 swrm->clk_ref_count = 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302334 swrm->swr_irq_wakeup_capable = 0;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302335 swrm->mclk_freq = MCLK_FREQ;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302336 swrm->bus_clk = MCLK_FREQ;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302337 swrm->dev_up = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302338 swrm->state = SWR_MSTR_UP;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302339 swrm->ipc_wakeup = false;
2340 swrm->ipc_wakeup_triggered = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302341 init_completion(&swrm->reset);
2342 init_completion(&swrm->broadcast);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302343 init_completion(&swrm->clk_off_complete);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302344 mutex_init(&swrm->mlock);
2345 mutex_init(&swrm->reslock);
2346 mutex_init(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302347 mutex_init(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302348 mutex_init(&swrm->clklock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302349 mutex_init(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302350 mutex_init(&swrm->pm_lock);
2351 swrm->wlock_holders = 0;
2352 swrm->pm_state = SWRM_PM_SLEEPABLE;
2353 init_waitqueue_head(&swrm->pm_wq);
2354 pm_qos_add_request(&swrm->pm_qos_req,
2355 PM_QOS_CPU_DMA_LATENCY,
2356 PM_QOS_DEFAULT_VALUE);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302357
2358 for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++)
2359 INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
2360
Sudheer Papothi06f43412019-07-09 03:32:54 +05302361 /* Register LPASS core hw vote */
2362 lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
2363 if (IS_ERR(lpass_core_hw_vote)) {
2364 ret = PTR_ERR(lpass_core_hw_vote);
2365 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2366 __func__, "lpass_core_hw_vote", ret);
2367 lpass_core_hw_vote = NULL;
2368 ret = 0;
2369 }
2370 swrm->lpass_core_hw_vote = lpass_core_hw_vote;
2371
2372 /* Register LPASS audio core vote */
2373 lpass_core_audio = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
2374 if (IS_ERR(lpass_core_audio)) {
2375 ret = PTR_ERR(lpass_core_audio);
2376 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2377 __func__, "lpass_core_audio", ret);
2378 lpass_core_audio = NULL;
2379 ret = 0;
2380 }
2381 swrm->lpass_core_audio = lpass_core_audio;
2382
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302383 if (swrm->reg_irq) {
2384 ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
2385 SWR_IRQ_REGISTER);
2386 if (ret) {
2387 dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
2388 __func__, ret);
2389 goto err_irq_fail;
2390 }
2391 } else {
2392 swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
2393 if (swrm->irq < 0) {
2394 dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
2395 __func__, swrm->irq);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302396 goto err_irq_fail;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302397 }
2398
2399 ret = request_threaded_irq(swrm->irq, NULL,
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302400 swr_mstr_interrupt_v2,
Ramprasad Katkam83303512018-10-11 17:34:22 +05302401 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302402 "swr_master_irq", swrm);
2403 if (ret) {
2404 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2405 __func__, ret);
2406 goto err_irq_fail;
2407 }
2408
2409 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302410 /* Make inband tx interrupts as wakeup capable for slave irq */
2411 ret = of_property_read_u32(pdev->dev.of_node,
2412 "qcom,swr-mstr-irq-wakeup-capable",
2413 &swrm->swr_irq_wakeup_capable);
2414 if (ret)
2415 dev_dbg(swrm->dev, "%s: swrm irq wakeup capable not defined\n",
2416 __func__);
2417 if (swrm->swr_irq_wakeup_capable)
2418 irq_set_irq_wake(swrm->irq, 1);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302419 ret = swr_register_master(&swrm->master);
2420 if (ret) {
2421 dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
2422 goto err_mstr_fail;
2423 }
2424
2425 /* Add devices registered with board-info as the
2426 * controller will be up now
2427 */
2428 swr_master_add_boarddevices(&swrm->master);
2429 mutex_lock(&swrm->mlock);
2430 swrm_clk_request(swrm, true);
2431 ret = swrm_master_init(swrm);
2432 if (ret < 0) {
2433 dev_err(&pdev->dev,
2434 "%s: Error in master Initialization , err %d\n",
2435 __func__, ret);
2436 mutex_unlock(&swrm->mlock);
2437 goto err_mstr_fail;
2438 }
2439 swrm->version = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
2440
2441 mutex_unlock(&swrm->mlock);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302442 INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302443
2444 if (pdev->dev.of_node)
2445 of_register_swr_devices(&swrm->master);
2446
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302447#ifdef CONFIG_DEBUG_FS
2448 swrm->debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
2449 if (!IS_ERR(swrm->debugfs_swrm_dent)) {
2450 swrm->debugfs_peek = debugfs_create_file("swrm_peek",
2451 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2452 (void *) swrm, &swrm_debug_read_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302453
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302454 swrm->debugfs_poke = debugfs_create_file("swrm_poke",
2455 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2456 (void *) swrm, &swrm_debug_write_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302457
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302458 swrm->debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
2459 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2460 (void *) swrm,
2461 &swrm_debug_dump_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302462 }
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302463#endif
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302464 ret = device_init_wakeup(swrm->dev, true);
2465 if (ret) {
2466 dev_err(swrm->dev, "Device wakeup init failed: %d\n", ret);
2467 goto err_irq_wakeup_fail;
2468 }
2469
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302470 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
2471 pm_runtime_use_autosuspend(&pdev->dev);
2472 pm_runtime_set_active(&pdev->dev);
2473 pm_runtime_enable(&pdev->dev);
2474 pm_runtime_mark_last_busy(&pdev->dev);
2475
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302476 INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn);
2477 swrm->event_notifier.notifier_call = swrm_event_notify;
2478 msm_aud_evt_register_client(&swrm->event_notifier);
2479
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302480 return 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302481err_irq_wakeup_fail:
2482 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302483err_mstr_fail:
2484 if (swrm->reg_irq)
2485 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2486 swrm, SWR_IRQ_FREE);
2487 else if (swrm->irq)
2488 free_irq(swrm->irq, swrm);
2489err_irq_fail:
2490 mutex_destroy(&swrm->mlock);
2491 mutex_destroy(&swrm->reslock);
2492 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302493 mutex_destroy(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302494 mutex_destroy(&swrm->clklock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302495 mutex_destroy(&swrm->pm_lock);
2496 pm_qos_remove_request(&swrm->pm_qos_req);
2497
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302498err_pdata_fail:
2499err_memory_fail:
2500 return ret;
2501}
2502
2503static int swrm_remove(struct platform_device *pdev)
2504{
2505 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2506
2507 if (swrm->reg_irq)
2508 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2509 swrm, SWR_IRQ_FREE);
2510 else if (swrm->irq)
2511 free_irq(swrm->irq, swrm);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302512 else if (swrm->wake_irq > 0)
2513 free_irq(swrm->wake_irq, swrm);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302514 if (swrm->swr_irq_wakeup_capable)
2515 irq_set_irq_wake(swrm->irq, 0);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302516 cancel_work_sync(&swrm->wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302517 pm_runtime_disable(&pdev->dev);
2518 pm_runtime_set_suspended(&pdev->dev);
2519 swr_unregister_master(&swrm->master);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302520 msm_aud_evt_unregister_client(&swrm->event_notifier);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302521 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302522 mutex_destroy(&swrm->mlock);
2523 mutex_destroy(&swrm->reslock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302524 mutex_destroy(&swrm->iolock);
2525 mutex_destroy(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302526 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302527 mutex_destroy(&swrm->pm_lock);
2528 pm_qos_remove_request(&swrm->pm_qos_req);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302529 devm_kfree(&pdev->dev, swrm);
2530 return 0;
2531}
2532
2533static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
2534{
2535 u32 val;
2536
2537 dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
2538 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD);
2539 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2540 val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK;
2541 swr_master_write(swrm, SWRM_MCP_CFG_ADDR, val);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302542
2543 return 0;
2544}
2545
2546#ifdef CONFIG_PM
2547static int swrm_runtime_resume(struct device *dev)
2548{
2549 struct platform_device *pdev = to_platform_device(dev);
2550 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2551 int ret = 0;
Vatsal Buchae50b5002019-09-19 14:32:20 +05302552 bool swrm_clk_req_err = false;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302553 bool hw_core_err = false;
2554 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302555 struct swr_master *mstr = &swrm->master;
2556 struct swr_device *swr_dev;
2557
2558 dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
2559 __func__, swrm->state);
2560 mutex_lock(&swrm->reslock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302561
Sudheer Papothi384addd2019-06-14 02:26:52 +05302562 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2563 dev_err(dev, "%s:lpass core hw enable failed\n",
2564 __func__);
2565 hw_core_err = true;
2566 }
2567 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2568 dev_err(dev, "%s:lpass audio hw enable failed\n",
2569 __func__);
2570 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002571 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302572
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302573 if ((swrm->state == SWR_MSTR_DOWN) ||
2574 (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) {
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302575 if (swrm->clk_stop_mode0_supp) {
2576 if (swrm->ipc_wakeup)
2577 msm_aud_evt_blocking_notifier_call_chain(
2578 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302579 }
2580
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302581 if (swrm_clk_request(swrm, true)) {
2582 /*
2583 * Set autosuspend timer to 1 for
2584 * master to enter into suspend.
2585 */
Vatsal Buchae50b5002019-09-19 14:32:20 +05302586 swrm_clk_req_err = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302587 goto exit;
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302588 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302589 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302590 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2591 ret = swr_device_up(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302592 if (ret == -ENODEV) {
2593 dev_dbg(dev,
2594 "%s slave device up not implemented\n",
2595 __func__);
2596 ret = 0;
2597 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302598 dev_err(dev,
2599 "%s: failed to wakeup swr dev %d\n",
2600 __func__, swr_dev->dev_num);
2601 swrm_clk_request(swrm, false);
2602 goto exit;
2603 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302604 }
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05302605 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2606 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2607 swrm_master_init(swrm);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302608 /* wait for hw enumeration to complete */
2609 usleep_range(100, 105);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302610 swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, 0x0,
2611 SWRS_SCP_INT_STATUS_MASK_1);
Karthikeyan Manif6821902019-05-21 17:31:24 -07002612 if (swrm->state == SWR_MSTR_SSR) {
2613 mutex_unlock(&swrm->reslock);
2614 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
2615 mutex_lock(&swrm->reslock);
2616 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302617 } else {
2618 /*wake up from clock stop*/
2619 swr_master_write(swrm, SWRM_MCP_BUS_CTRL_ADDR, 0x2);
2620 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302621 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302622 swrm->state = SWR_MSTR_UP;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302623 }
2624exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302625 if (!aud_core_err)
2626 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2627 if (!hw_core_err)
2628 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Vatsal Buchae50b5002019-09-19 14:32:20 +05302629 if (swrm_clk_req_err)
2630 pm_runtime_set_autosuspend_delay(&pdev->dev,
2631 ERR_AUTO_SUSPEND_TIMER_VAL);
2632 else
2633 pm_runtime_set_autosuspend_delay(&pdev->dev,
2634 auto_suspend_timer);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302635 mutex_unlock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302636
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302637 return ret;
2638}
2639
2640static int swrm_runtime_suspend(struct device *dev)
2641{
2642 struct platform_device *pdev = to_platform_device(dev);
2643 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2644 int ret = 0;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302645 bool hw_core_err = false;
2646 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302647 struct swr_master *mstr = &swrm->master;
2648 struct swr_device *swr_dev;
2649 int current_state = 0;
2650
2651 dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
2652 __func__, swrm->state);
2653 mutex_lock(&swrm->reslock);
2654 mutex_lock(&swrm->force_down_lock);
2655 current_state = swrm->state;
2656 mutex_unlock(&swrm->force_down_lock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302657
2658 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2659 dev_err(dev, "%s:lpass core hw enable failed\n",
2660 __func__);
2661 hw_core_err = true;
2662 }
2663 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2664 dev_err(dev, "%s:lpass audio hw enable failed\n",
2665 __func__);
2666 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002667 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302668
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302669 if ((current_state == SWR_MSTR_UP) ||
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302670 (current_state == SWR_MSTR_SSR)) {
2671
2672 if ((current_state != SWR_MSTR_SSR) &&
2673 swrm_is_port_en(&swrm->master)) {
2674 dev_dbg(dev, "%s ports are enabled\n", __func__);
2675 ret = -EBUSY;
2676 goto exit;
2677 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302678 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05302679 mutex_unlock(&swrm->reslock);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +05302680 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
Sudheer Papothi06f43412019-07-09 03:32:54 +05302681 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302682 swrm_clk_pause(swrm);
2683 swr_master_write(swrm, SWRM_COMP_CFG_ADDR, 0x00);
2684 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2685 ret = swr_device_down(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302686 if (ret == -ENODEV) {
2687 dev_dbg_ratelimited(dev,
2688 "%s slave device down not implemented\n",
2689 __func__);
2690 ret = 0;
2691 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302692 dev_err(dev,
2693 "%s: failed to shutdown swr dev %d\n",
2694 __func__, swr_dev->dev_num);
2695 goto exit;
2696 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302697 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302698 } else {
Sudheer Papothi384addd2019-06-14 02:26:52 +05302699 mutex_unlock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302700 /* clock stop sequence */
2701 swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
2702 SWRS_SCP_CONTROL);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302703 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302704 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302705 }
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07002706 ret = swrm_clk_request(swrm, false);
2707 if (ret) {
2708 dev_err(dev, "%s: swrmn clk failed\n", __func__);
2709 ret = 0;
2710 goto exit;
2711 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302712
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302713 if (swrm->clk_stop_mode0_supp) {
2714 if (swrm->wake_irq > 0) {
2715 enable_irq(swrm->wake_irq);
2716 } else if (swrm->ipc_wakeup) {
2717 msm_aud_evt_blocking_notifier_call_chain(
2718 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
2719 swrm->ipc_wakeup_triggered = false;
2720 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302721 }
2722
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302723 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302724 /* Retain SSR state until resume */
2725 if (current_state != SWR_MSTR_SSR)
2726 swrm->state = SWR_MSTR_DOWN;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302727exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302728 if (!aud_core_err)
2729 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2730 if (!hw_core_err)
2731 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302732 mutex_unlock(&swrm->reslock);
2733 return ret;
2734}
2735#endif /* CONFIG_PM */
2736
Sudheer Papothi06f43412019-07-09 03:32:54 +05302737static int swrm_device_suspend(struct device *dev)
2738{
2739 struct platform_device *pdev = to_platform_device(dev);
2740 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2741 int ret = 0;
2742
2743 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2744 if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
2745 ret = swrm_runtime_suspend(dev);
2746 if (!ret) {
2747 pm_runtime_disable(dev);
2748 pm_runtime_set_suspended(dev);
2749 pm_runtime_enable(dev);
2750 }
2751 }
2752
2753 return 0;
2754}
2755
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302756static int swrm_device_down(struct device *dev)
2757{
2758 struct platform_device *pdev = to_platform_device(dev);
2759 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302760
2761 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2762
2763 mutex_lock(&swrm->force_down_lock);
2764 swrm->state = SWR_MSTR_SSR;
2765 mutex_unlock(&swrm->force_down_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302766
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302767 swrm_device_suspend(dev);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302768 return 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302769}
2770
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302771int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm)
2772{
2773 int ret = 0;
Laxminath Kasama60239e2019-01-10 14:43:03 +05302774 int irq, dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302775
2776 if (!swrm->ipc_wakeup) {
Laxminath Kasama60239e2019-01-10 14:43:03 +05302777 irq = of_get_named_gpio(swrm->dev->of_node,
2778 "qcom,swr-wakeup-irq", 0);
2779 if (gpio_is_valid(irq)) {
2780 swrm->wake_irq = gpio_to_irq(irq);
2781 if (swrm->wake_irq < 0) {
2782 dev_err(swrm->dev,
2783 "Unable to configure irq\n");
2784 return swrm->wake_irq;
2785 }
2786 } else {
2787 dir_apps_irq = platform_get_irq_byname(swrm->pdev,
2788 "swr_wake_irq");
2789 if (dir_apps_irq < 0) {
2790 dev_err(swrm->dev,
2791 "TLMM connect gpio not found\n");
2792 return -EINVAL;
2793 }
2794 swrm->wake_irq = dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302795 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302796 ret = request_threaded_irq(swrm->wake_irq, NULL,
2797 swrm_wakeup_interrupt,
2798 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
2799 "swr_wake_irq", swrm);
2800 if (ret) {
2801 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2802 __func__, ret);
2803 return -EINVAL;
2804 }
Aditya Bavanari3517b112018-12-03 13:26:59 +05302805 irq_set_irq_wake(swrm->wake_irq, 1);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302806 }
2807 return ret;
2808}
2809
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302810static int swrm_alloc_port_mem(struct device *dev, struct swr_mstr_ctrl *swrm,
2811 u32 uc, u32 size)
2812{
2813 if (!swrm->port_param) {
2814 swrm->port_param = devm_kzalloc(dev,
2815 sizeof(swrm->port_param) * SWR_UC_MAX,
2816 GFP_KERNEL);
2817 if (!swrm->port_param)
2818 return -ENOMEM;
2819 }
2820 if (!swrm->port_param[uc]) {
2821 swrm->port_param[uc] = devm_kcalloc(dev, size,
2822 sizeof(struct port_params),
2823 GFP_KERNEL);
2824 if (!swrm->port_param[uc])
2825 return -ENOMEM;
2826 } else {
2827 dev_err_ratelimited(swrm->dev, "%s: called more than once\n",
2828 __func__);
2829 }
2830
2831 return 0;
2832}
2833
2834static int swrm_copy_port_config(struct swr_mstr_ctrl *swrm,
2835 struct swrm_port_config *port_cfg,
2836 u32 size)
2837{
2838 int idx;
2839 struct port_params *params;
2840 int uc = port_cfg->uc;
2841 int ret = 0;
2842
2843 for (idx = 0; idx < size; idx++) {
2844 params = &((struct port_params *)port_cfg->params)[idx];
2845 if (!params) {
2846 dev_err(swrm->dev, "%s: Invalid params\n", __func__);
2847 ret = -EINVAL;
2848 break;
2849 }
2850 memcpy(&swrm->port_param[uc][idx], params,
2851 sizeof(struct port_params));
2852 }
2853
2854 return ret;
2855}
2856
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302857/**
2858 * swrm_wcd_notify - parent device can notify to soundwire master through
2859 * this function
2860 * @pdev: pointer to platform device structure
2861 * @id: command id from parent to the soundwire master
2862 * @data: data from parent device to soundwire master
2863 */
2864int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
2865{
2866 struct swr_mstr_ctrl *swrm;
2867 int ret = 0;
2868 struct swr_master *mstr;
2869 struct swr_device *swr_dev;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302870 struct swrm_port_config *port_cfg;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302871
2872 if (!pdev) {
2873 pr_err("%s: pdev is NULL\n", __func__);
2874 return -EINVAL;
2875 }
2876 swrm = platform_get_drvdata(pdev);
2877 if (!swrm) {
2878 dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__);
2879 return -EINVAL;
2880 }
2881 mstr = &swrm->master;
2882
2883 switch (id) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05302884 case SWR_REQ_CLK_SWITCH:
2885 /* This will put soundwire in clock stop mode and disable the
2886 * clocks, if there is no active usecase running, so that the
2887 * next activity on soundwire will request clock from new clock
2888 * source.
2889 */
2890 mutex_lock(&swrm->mlock);
2891 if (swrm->state == SWR_MSTR_UP)
2892 swrm_device_suspend(&pdev->dev);
2893 mutex_unlock(&swrm->mlock);
2894 break;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302895 case SWR_CLK_FREQ:
2896 if (!data) {
2897 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
2898 ret = -EINVAL;
2899 } else {
2900 mutex_lock(&swrm->mlock);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302901 if (swrm->mclk_freq != *(int *)data) {
2902 dev_dbg(swrm->dev, "%s: freq change: force mstr down\n", __func__);
2903 if (swrm->state == SWR_MSTR_DOWN)
2904 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
2905 __func__, swrm->state);
2906 else
2907 swrm_device_suspend(&pdev->dev);
2908 }
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302909 swrm->mclk_freq = *(int *)data;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302910 swrm->bus_clk = swrm->mclk_freq;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302911 mutex_unlock(&swrm->mlock);
2912 }
2913 break;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302914 case SWR_DEVICE_SSR_DOWN:
2915 mutex_lock(&swrm->devlock);
2916 swrm->dev_up = false;
2917 mutex_unlock(&swrm->devlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302918 mutex_lock(&swrm->reslock);
2919 swrm->state = SWR_MSTR_SSR;
2920 mutex_unlock(&swrm->reslock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302921 break;
2922 case SWR_DEVICE_SSR_UP:
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302923 /* wait for clk voting to be zero */
Ramprasad Katkam7f6462e2018-11-06 11:51:22 +05302924 reinit_completion(&swrm->clk_off_complete);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302925 if (swrm->clk_ref_count &&
2926 !wait_for_completion_timeout(&swrm->clk_off_complete,
Ramprasad Katkamc87efeb2018-12-12 19:26:19 +05302927 msecs_to_jiffies(500)))
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302928 dev_err(swrm->dev, "%s: clock voting not zero\n",
2929 __func__);
2930
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302931 mutex_lock(&swrm->devlock);
2932 swrm->dev_up = true;
2933 mutex_unlock(&swrm->devlock);
2934 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302935 case SWR_DEVICE_DOWN:
2936 dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
2937 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302938 if (swrm->state == SWR_MSTR_DOWN)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302939 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
2940 __func__, swrm->state);
2941 else
2942 swrm_device_down(&pdev->dev);
2943 mutex_unlock(&swrm->mlock);
2944 break;
2945 case SWR_DEVICE_UP:
2946 dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
Ramprasad Katkam0fed92f2018-11-08 14:22:22 +05302947 mutex_lock(&swrm->devlock);
2948 if (!swrm->dev_up) {
2949 dev_dbg(swrm->dev, "SSR not complete yet\n");
2950 mutex_unlock(&swrm->devlock);
2951 return -EBUSY;
2952 }
2953 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302954 mutex_lock(&swrm->mlock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302955 pm_runtime_mark_last_busy(&pdev->dev);
2956 pm_runtime_get_sync(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302957 mutex_lock(&swrm->reslock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302958 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2959 ret = swr_reset_device(swr_dev);
2960 if (ret) {
2961 dev_err(swrm->dev,
2962 "%s: failed to reset swr device %d\n",
2963 __func__, swr_dev->dev_num);
2964 swrm_clk_request(swrm, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302965 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302966 }
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302967 pm_runtime_mark_last_busy(&pdev->dev);
2968 pm_runtime_put_autosuspend(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302969 mutex_unlock(&swrm->reslock);
2970 mutex_unlock(&swrm->mlock);
2971 break;
2972 case SWR_SET_NUM_RX_CH:
2973 if (!data) {
2974 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
2975 ret = -EINVAL;
2976 } else {
2977 mutex_lock(&swrm->mlock);
2978 swrm->num_rx_chs = *(int *)data;
2979 if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
2980 list_for_each_entry(swr_dev, &mstr->devices,
2981 dev_list) {
2982 ret = swr_set_device_group(swr_dev,
2983 SWR_BROADCAST);
2984 if (ret)
2985 dev_err(swrm->dev,
2986 "%s: set num ch failed\n",
2987 __func__);
2988 }
2989 } else {
2990 list_for_each_entry(swr_dev, &mstr->devices,
2991 dev_list) {
2992 ret = swr_set_device_group(swr_dev,
2993 SWR_GROUP_NONE);
2994 if (ret)
2995 dev_err(swrm->dev,
2996 "%s: set num ch failed\n",
2997 __func__);
2998 }
2999 }
3000 mutex_unlock(&swrm->mlock);
3001 }
3002 break;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303003 case SWR_REGISTER_WAKE_IRQ:
3004 if (!data) {
3005 dev_err(swrm->dev, "%s: reg wake irq data is NULL\n",
3006 __func__);
3007 ret = -EINVAL;
3008 } else {
3009 mutex_lock(&swrm->mlock);
3010 swrm->ipc_wakeup = *(u32 *)data;
3011 ret = swrm_register_wake_irq(swrm);
3012 if (ret)
3013 dev_err(swrm->dev, "%s: register wake_irq failed\n",
3014 __func__);
3015 mutex_unlock(&swrm->mlock);
3016 }
3017 break;
Sudheer Papothi72ee2642019-08-08 05:15:17 +05303018 case SWR_REGISTER_WAKEUP:
3019 msm_aud_evt_blocking_notifier_call_chain(
3020 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
3021 break;
3022 case SWR_DEREGISTER_WAKEUP:
3023 msm_aud_evt_blocking_notifier_call_chain(
3024 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
3025 break;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05303026 case SWR_SET_PORT_MAP:
3027 if (!data) {
3028 dev_err(swrm->dev, "%s: data is NULL for id=%d\n",
3029 __func__, id);
3030 ret = -EINVAL;
3031 } else {
3032 mutex_lock(&swrm->mlock);
3033 port_cfg = (struct swrm_port_config *)data;
3034 if (!port_cfg->size) {
3035 ret = -EINVAL;
3036 goto done;
3037 }
3038 ret = swrm_alloc_port_mem(&pdev->dev, swrm,
3039 port_cfg->uc, port_cfg->size);
3040 if (!ret)
3041 swrm_copy_port_config(swrm, port_cfg,
3042 port_cfg->size);
3043done:
3044 mutex_unlock(&swrm->mlock);
3045 }
3046 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303047 default:
3048 dev_err(swrm->dev, "%s: swr master unknown id %d\n",
3049 __func__, id);
3050 break;
3051 }
3052 return ret;
3053}
3054EXPORT_SYMBOL(swrm_wcd_notify);
3055
Ramprasad Katkam57349872018-11-11 18:34:57 +05303056/*
3057 * swrm_pm_cmpxchg:
3058 * Check old state and exchange with pm new state
3059 * if old state matches with current state
3060 *
3061 * @swrm: pointer to wcd core resource
3062 * @o: pm old state
3063 * @n: pm new state
3064 *
3065 * Returns old state
3066 */
3067static enum swrm_pm_state swrm_pm_cmpxchg(
3068 struct swr_mstr_ctrl *swrm,
3069 enum swrm_pm_state o,
3070 enum swrm_pm_state n)
3071{
3072 enum swrm_pm_state old;
3073
3074 if (!swrm)
3075 return o;
3076
3077 mutex_lock(&swrm->pm_lock);
3078 old = swrm->pm_state;
3079 if (old == o)
3080 swrm->pm_state = n;
3081 mutex_unlock(&swrm->pm_lock);
3082
3083 return old;
3084}
3085
3086static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm)
3087{
3088 enum swrm_pm_state os;
3089
3090 /*
3091 * swrm_{lock/unlock}_sleep will be called by swr irq handler
3092 * and slave wake up requests..
3093 *
3094 * If system didn't resume, we can simply return false so
3095 * IRQ handler can return without handling IRQ.
3096 */
3097 mutex_lock(&swrm->pm_lock);
3098 if (swrm->wlock_holders++ == 0) {
3099 dev_dbg(swrm->dev, "%s: holding wake lock\n", __func__);
3100 pm_qos_update_request(&swrm->pm_qos_req,
3101 msm_cpuidle_get_deep_idle_latency());
3102 pm_stay_awake(swrm->dev);
3103 }
3104 mutex_unlock(&swrm->pm_lock);
3105
3106 if (!wait_event_timeout(swrm->pm_wq,
3107 ((os = swrm_pm_cmpxchg(swrm,
3108 SWRM_PM_SLEEPABLE,
3109 SWRM_PM_AWAKE)) ==
3110 SWRM_PM_SLEEPABLE ||
3111 (os == SWRM_PM_AWAKE)),
3112 msecs_to_jiffies(
3113 SWRM_SYSTEM_RESUME_TIMEOUT_MS))) {
3114 dev_err(swrm->dev, "%s: system didn't resume within %dms, s %d, w %d\n",
3115 __func__, SWRM_SYSTEM_RESUME_TIMEOUT_MS, swrm->pm_state,
3116 swrm->wlock_holders);
3117 swrm_unlock_sleep(swrm);
3118 return false;
3119 }
3120 wake_up_all(&swrm->pm_wq);
3121 return true;
3122}
3123
3124static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm)
3125{
3126 mutex_lock(&swrm->pm_lock);
3127 if (--swrm->wlock_holders == 0) {
3128 dev_dbg(swrm->dev, "%s: releasing wake lock pm_state %d -> %d\n",
3129 __func__, swrm->pm_state, SWRM_PM_SLEEPABLE);
3130 /*
3131 * if swrm_lock_sleep failed, pm_state would be still
3132 * swrm_PM_ASLEEP, don't overwrite
3133 */
3134 if (likely(swrm->pm_state == SWRM_PM_AWAKE))
3135 swrm->pm_state = SWRM_PM_SLEEPABLE;
3136 pm_qos_update_request(&swrm->pm_qos_req,
3137 PM_QOS_DEFAULT_VALUE);
3138 pm_relax(swrm->dev);
3139 }
3140 mutex_unlock(&swrm->pm_lock);
3141 wake_up_all(&swrm->pm_wq);
3142}
3143
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303144#ifdef CONFIG_PM_SLEEP
3145static int swrm_suspend(struct device *dev)
3146{
3147 int ret = -EBUSY;
3148 struct platform_device *pdev = to_platform_device(dev);
3149 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3150
3151 dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
Ramprasad Katkam57349872018-11-11 18:34:57 +05303152
3153 mutex_lock(&swrm->pm_lock);
3154
3155 if (swrm->pm_state == SWRM_PM_SLEEPABLE) {
3156 dev_dbg(swrm->dev, "%s: suspending system, state %d, wlock %d\n",
3157 __func__, swrm->pm_state,
3158 swrm->wlock_holders);
3159 swrm->pm_state = SWRM_PM_ASLEEP;
3160 } else if (swrm->pm_state == SWRM_PM_AWAKE) {
3161 /*
3162 * unlock to wait for pm_state == SWRM_PM_SLEEPABLE
3163 * then set to SWRM_PM_ASLEEP
3164 */
3165 dev_dbg(swrm->dev, "%s: waiting to suspend system, state %d, wlock %d\n",
3166 __func__, swrm->pm_state,
3167 swrm->wlock_holders);
3168 mutex_unlock(&swrm->pm_lock);
3169 if (!(wait_event_timeout(swrm->pm_wq, swrm_pm_cmpxchg(
3170 swrm, SWRM_PM_SLEEPABLE,
3171 SWRM_PM_ASLEEP) ==
3172 SWRM_PM_SLEEPABLE,
3173 msecs_to_jiffies(
3174 SWRM_SYS_SUSPEND_WAIT)))) {
3175 dev_dbg(swrm->dev, "%s: suspend failed state %d, wlock %d\n",
3176 __func__, swrm->pm_state,
3177 swrm->wlock_holders);
3178 return -EBUSY;
3179 } else {
3180 dev_dbg(swrm->dev,
3181 "%s: done, state %d, wlock %d\n",
3182 __func__, swrm->pm_state,
3183 swrm->wlock_holders);
3184 }
3185 mutex_lock(&swrm->pm_lock);
3186 } else if (swrm->pm_state == SWRM_PM_ASLEEP) {
3187 dev_dbg(swrm->dev, "%s: system is already suspended, state %d, wlock %d\n",
3188 __func__, swrm->pm_state,
3189 swrm->wlock_holders);
3190 }
3191
3192 mutex_unlock(&swrm->pm_lock);
3193
3194 if ((!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev))) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303195 ret = swrm_runtime_suspend(dev);
3196 if (!ret) {
3197 /*
3198 * Synchronize runtime-pm and system-pm states:
3199 * At this point, we are already suspended. If
3200 * runtime-pm still thinks its active, then
3201 * make sure its status is in sync with HW
3202 * status. The three below calls let the
3203 * runtime-pm know that we are suspended
3204 * already without re-invoking the suspend
3205 * callback
3206 */
3207 pm_runtime_disable(dev);
3208 pm_runtime_set_suspended(dev);
3209 pm_runtime_enable(dev);
3210 }
3211 }
3212 if (ret == -EBUSY) {
3213 /*
3214 * There is a possibility that some audio stream is active
3215 * during suspend. We dont want to return suspend failure in
3216 * that case so that display and relevant components can still
3217 * go to suspend.
3218 * If there is some other error, then it should be passed-on
3219 * to system level suspend
3220 */
3221 ret = 0;
3222 }
3223 return ret;
3224}
3225
3226static int swrm_resume(struct device *dev)
3227{
3228 int ret = 0;
3229 struct platform_device *pdev = to_platform_device(dev);
3230 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3231
3232 dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
3233 if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
3234 ret = swrm_runtime_resume(dev);
3235 if (!ret) {
3236 pm_runtime_mark_last_busy(dev);
3237 pm_request_autosuspend(dev);
3238 }
3239 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05303240 mutex_lock(&swrm->pm_lock);
3241 if (swrm->pm_state == SWRM_PM_ASLEEP) {
3242 dev_dbg(swrm->dev,
3243 "%s: resuming system, state %d, wlock %d\n",
3244 __func__, swrm->pm_state,
3245 swrm->wlock_holders);
3246 swrm->pm_state = SWRM_PM_SLEEPABLE;
3247 } else {
3248 dev_dbg(swrm->dev, "%s: system is already awake, state %d wlock %d\n",
3249 __func__, swrm->pm_state,
3250 swrm->wlock_holders);
3251 }
3252 mutex_unlock(&swrm->pm_lock);
3253 wake_up_all(&swrm->pm_wq);
3254
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303255 return ret;
3256}
3257#endif /* CONFIG_PM_SLEEP */
3258
3259static const struct dev_pm_ops swrm_dev_pm_ops = {
3260 SET_SYSTEM_SLEEP_PM_OPS(
3261 swrm_suspend,
3262 swrm_resume
3263 )
3264 SET_RUNTIME_PM_OPS(
3265 swrm_runtime_suspend,
3266 swrm_runtime_resume,
3267 NULL
3268 )
3269};
3270
3271static const struct of_device_id swrm_dt_match[] = {
3272 {
3273 .compatible = "qcom,swr-mstr",
3274 },
3275 {}
3276};
3277
3278static struct platform_driver swr_mstr_driver = {
3279 .probe = swrm_probe,
3280 .remove = swrm_remove,
3281 .driver = {
3282 .name = SWR_WCD_NAME,
3283 .owner = THIS_MODULE,
3284 .pm = &swrm_dev_pm_ops,
3285 .of_match_table = swrm_dt_match,
Xiaojun Sang53cd13a2018-06-29 15:14:37 +08003286 .suppress_bind_attrs = true,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303287 },
3288};
3289
3290static int __init swrm_init(void)
3291{
3292 return platform_driver_register(&swr_mstr_driver);
3293}
3294module_init(swrm_init);
3295
3296static void __exit swrm_exit(void)
3297{
3298 platform_driver_unregister(&swr_mstr_driver);
3299}
3300module_exit(swrm_exit);
3301
3302MODULE_LICENSE("GPL v2");
3303MODULE_DESCRIPTION("SoundWire Master Controller");
3304MODULE_ALIAS("platform:swr-mstr");