blob: 55334b6202354be9c02af020446b63e55693a37c [file] [log] [blame]
Meng Wang43bbb872018-12-10 12:32:05 +08001// SPDX-License-Identifier: GPL-2.0-only
Meng Wang61af6842018-09-10 17:47:55 +08002/*
Aditya Bavanari3517b112018-12-03 13:26:59 +05303 * Copyright (c) 2015-2019, The Linux Foundation. All rights reserved.
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05304 */
5
6#include <linux/irq.h>
7#include <linux/kernel.h>
8#include <linux/init.h>
9#include <linux/slab.h>
10#include <linux/io.h>
11#include <linux/interrupt.h>
12#include <linux/platform_device.h>
13#include <linux/delay.h>
14#include <linux/kthread.h>
Ramprasad Katkamcab8d722018-09-28 15:54:06 +053015#include <linux/bitops.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053016#include <linux/clk.h>
Laxminath Kasama60239e2019-01-10 14:43:03 +053017#include <linux/gpio.h>
18#include <linux/of_gpio.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053019#include <linux/pm_runtime.h>
20#include <linux/of.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053021#include <soc/soundwire.h>
Sudheer Papothi3d1596e2018-10-27 06:19:18 +053022#include <soc/swr-common.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053023#include <linux/regmap.h>
Ramprasad Katkam68765ab2018-08-30 11:46:32 +053024#include <dsp/msm-audio-event-notify.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053025#include "swrm_registers.h"
26#include "swr-mstr-ctrl.h"
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053027
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +053028#define SWRM_FRAME_SYNC_SEL 4000 /* 4KHz */
Ramprasad Katkam57349872018-11-11 18:34:57 +053029#define SWRM_SYSTEM_RESUME_TIMEOUT_MS 700
30#define SWRM_SYS_SUSPEND_WAIT 1
Sudheer Papothi3d1596e2018-10-27 06:19:18 +053031
Sudheer Papothi4c322b12018-10-31 06:34:01 +053032#define SWRM_DSD_PARAMS_PORT 4
33
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053034#define SWR_BROADCAST_CMD_ID 0x0F
Sudheer Papothi3590b312019-06-04 23:51:30 +053035#define SWR_AUTO_SUSPEND_DELAY 1 /* delay in sec */
Sudheer Papothi7c067e82018-11-15 06:53:35 +053036#define SWR_DEV_ID_MASK 0xFFFFFFFFFFFF
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053037#define SWR_REG_VAL_PACK(data, dev, id, reg) \
38 ((reg) | ((id) << 16) | ((dev) << 20) | ((data) << 24))
39
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +053040#define SWR_INVALID_PARAM 0xFF
Laxminath Kasam990c70b2018-11-09 23:15:09 +053041#define SWR_HSTOP_MAX_VAL 0xF
42#define SWR_HSTART_MIN_VAL 0x0
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +053043
Vatsal Buchae50b5002019-09-19 14:32:20 +053044#define ERR_AUTO_SUSPEND_TIMER_VAL 0x1
45
Ramprasad Katkam83303512018-10-11 17:34:22 +053046#define SWRM_INTERRUPT_STATUS_MASK 0x1FDFD
Laxminath Kasame2291972019-11-08 14:51:59 +053047#define SWRM_LINK_STATUS_RETRY_CNT 0x5
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +053048
49#define SWRM_ROW_48 48
50#define SWRM_ROW_50 50
51#define SWRM_ROW_64 64
52#define SWRM_COL_02 02
53#define SWRM_COL_16 16
54
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053055/* pm runtime auto suspend timer in msecs */
56static int auto_suspend_timer = SWR_AUTO_SUSPEND_DELAY * 1000;
57module_param(auto_suspend_timer, int, 0664);
58MODULE_PARM_DESC(auto_suspend_timer, "timer for auto suspend");
59
60enum {
61 SWR_NOT_PRESENT, /* Device is detached/not present on the bus */
62 SWR_ATTACHED_OK, /* Device is attached */
63 SWR_ALERT, /* Device alters master for any interrupts */
64 SWR_RESERVED, /* Reserved */
65};
66
67enum {
68 MASTER_ID_WSA = 1,
69 MASTER_ID_RX,
70 MASTER_ID_TX
71};
Ramprasad Katkamcab8d722018-09-28 15:54:06 +053072
73enum {
74 ENABLE_PENDING,
75 DISABLE_PENDING
76};
Sudheer Papothi384addd2019-06-14 02:26:52 +053077
78enum {
79 LPASS_HW_CORE,
80 LPASS_AUDIO_CORE,
81};
82
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053083#define TRUE 1
84#define FALSE 0
85
Ramprasad Katkam1f221262018-08-23 15:01:22 +053086#define SWRM_MAX_PORT_REG 120
Ramprasad Katkam83303512018-10-11 17:34:22 +053087#define SWRM_MAX_INIT_REG 11
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053088
Laxminath Kasamfbcaf322018-07-18 00:38:14 +053089#define MAX_FIFO_RD_FAIL_RETRY 3
90
Ramprasad Katkam57349872018-11-11 18:34:57 +053091static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm);
92static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm);
Sudheer Papothi96c842a2019-08-29 12:11:21 +053093static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr);
94static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053095
96static bool swrm_is_msm_variant(int val)
97{
98 return (val == SWRM_VERSION_1_3);
99}
100
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530101#ifdef CONFIG_DEBUG_FS
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530102static int swrm_debug_open(struct inode *inode, struct file *file)
103{
104 file->private_data = inode->i_private;
105 return 0;
106}
107
108static int get_parameters(char *buf, u32 *param1, int num_of_par)
109{
110 char *token;
111 int base, cnt;
112
113 token = strsep(&buf, " ");
114 for (cnt = 0; cnt < num_of_par; cnt++) {
115 if (token) {
116 if ((token[1] == 'x') || (token[1] == 'X'))
117 base = 16;
118 else
119 base = 10;
120
121 if (kstrtou32(token, base, &param1[cnt]) != 0)
122 return -EINVAL;
123
124 token = strsep(&buf, " ");
125 } else
126 return -EINVAL;
127 }
128 return 0;
129}
130
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530131static ssize_t swrm_reg_show(struct swr_mstr_ctrl *swrm, char __user *ubuf,
132 size_t count, loff_t *ppos)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530133{
134 int i, reg_val, len;
135 ssize_t total = 0;
136 char tmp_buf[SWR_MSTR_MAX_BUF_LEN];
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530137 int rem = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530138
139 if (!ubuf || !ppos)
140 return 0;
141
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530142 i = ((int) *ppos + SWR_MSTR_START_REG_ADDR);
143 rem = i%4;
144
145 if (rem)
146 i = (i - rem);
147
148 for (; i <= SWR_MSTR_MAX_REG_ADDR; i += 4) {
149 usleep_range(100, 150);
150 reg_val = swr_master_read(swrm, i);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530151 len = snprintf(tmp_buf, 25, "0x%.3x: 0x%.2x\n", i, reg_val);
Aditya Bavanari9f599b42019-08-27 22:18:41 +0530152 if (len < 0) {
153 pr_err("%s: fail to fill the buffer\n", __func__);
154 total = -EFAULT;
155 goto copy_err;
156 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530157 if ((total + len) >= count - 1)
158 break;
159 if (copy_to_user((ubuf + total), tmp_buf, len)) {
160 pr_err("%s: fail to copy reg dump\n", __func__);
161 total = -EFAULT;
162 goto copy_err;
163 }
164 *ppos += len;
165 total += len;
166 }
167
168copy_err:
169 return total;
170}
171
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530172static ssize_t swrm_debug_reg_dump(struct file *file, char __user *ubuf,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530173 size_t count, loff_t *ppos)
174{
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530175 struct swr_mstr_ctrl *swrm;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530176
177 if (!count || !file || !ppos || !ubuf)
178 return -EINVAL;
179
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530180 swrm = file->private_data;
181 if (!swrm)
182 return -EINVAL;
183
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530184 if (*ppos < 0)
185 return -EINVAL;
186
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530187 return swrm_reg_show(swrm, ubuf, count, ppos);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530188}
189
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530190static ssize_t swrm_debug_read(struct file *file, char __user *ubuf,
191 size_t count, loff_t *ppos)
192{
193 char lbuf[SWR_MSTR_RD_BUF_LEN];
194 struct swr_mstr_ctrl *swrm = NULL;
195
196 if (!count || !file || !ppos || !ubuf)
197 return -EINVAL;
198
199 swrm = file->private_data;
200 if (!swrm)
201 return -EINVAL;
202
203 if (*ppos < 0)
204 return -EINVAL;
205
206 snprintf(lbuf, sizeof(lbuf), "0x%x\n", swrm->read_data);
207
208 return simple_read_from_buffer(ubuf, count, ppos, lbuf,
209 strnlen(lbuf, 7));
210}
211
212static ssize_t swrm_debug_peek_write(struct file *file, const char __user *ubuf,
213 size_t count, loff_t *ppos)
214{
215 char lbuf[SWR_MSTR_RD_BUF_LEN];
216 int rc;
217 u32 param[5];
218 struct swr_mstr_ctrl *swrm = NULL;
219
220 if (!count || !file || !ppos || !ubuf)
221 return -EINVAL;
222
223 swrm = file->private_data;
224 if (!swrm)
225 return -EINVAL;
226
227 if (*ppos < 0)
228 return -EINVAL;
229
230 if (count > sizeof(lbuf) - 1)
231 return -EINVAL;
232
233 rc = copy_from_user(lbuf, ubuf, count);
234 if (rc)
235 return -EFAULT;
236
237 lbuf[count] = '\0';
238 rc = get_parameters(lbuf, param, 1);
239 if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) && (rc == 0))
240 swrm->read_data = swr_master_read(swrm, param[0]);
241 else
242 rc = -EINVAL;
243
244 if (rc == 0)
245 rc = count;
246 else
247 dev_err(swrm->dev, "%s: rc = %d\n", __func__, rc);
248
249 return rc;
250}
251
252static ssize_t swrm_debug_write(struct file *file,
253 const char __user *ubuf, size_t count, loff_t *ppos)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530254{
255 char lbuf[SWR_MSTR_WR_BUF_LEN];
256 int rc;
257 u32 param[5];
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530258 struct swr_mstr_ctrl *swrm;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530259
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530260 if (!file || !ppos || !ubuf)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530261 return -EINVAL;
262
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530263 swrm = file->private_data;
264 if (!swrm)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530265 return -EINVAL;
266
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530267 if (count > sizeof(lbuf) - 1)
268 return -EINVAL;
269
270 rc = copy_from_user(lbuf, ubuf, count);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530271 if (rc)
272 return -EFAULT;
273
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530274 lbuf[count] = '\0';
275 rc = get_parameters(lbuf, param, 2);
276 if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) &&
277 (param[1] <= 0xFFFFFFFF) &&
278 (rc == 0))
279 swr_master_write(swrm, param[0], param[1]);
280 else
281 rc = -EINVAL;
282
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530283 if (rc == 0)
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530284 rc = count;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530285 else
286 pr_err("%s: rc = %d\n", __func__, rc);
287
288 return rc;
289}
290
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530291static const struct file_operations swrm_debug_read_ops = {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530292 .open = swrm_debug_open,
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530293 .write = swrm_debug_peek_write,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530294 .read = swrm_debug_read,
295};
296
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530297static const struct file_operations swrm_debug_write_ops = {
298 .open = swrm_debug_open,
299 .write = swrm_debug_write,
300};
301
302static const struct file_operations swrm_debug_dump_ops = {
303 .open = swrm_debug_open,
304 .read = swrm_debug_reg_dump,
305};
306#endif
307
Sudheer Papothi0016db12019-06-11 04:42:38 +0530308static void swrm_reg_dump(struct swr_mstr_ctrl *swrm,
309 u32 *reg, u32 *val, int len, const char* func)
310{
311 int i = 0;
312
313 for (i = 0; i < len; i++)
314 dev_dbg(swrm->dev, "%s: reg = 0x%x val = 0x%x\n",
315 func, reg[i], val[i]);
316}
317
Sudheer Papothi384addd2019-06-14 02:26:52 +0530318static int swrm_request_hw_vote(struct swr_mstr_ctrl *swrm,
319 int core_type, bool enable)
320{
321 int ret = 0;
322
323 if (core_type == LPASS_HW_CORE) {
324 if (swrm->lpass_core_hw_vote) {
325 if (enable) {
326 ret =
327 clk_prepare_enable(swrm->lpass_core_hw_vote);
328 if (ret < 0)
329 dev_err(swrm->dev,
330 "%s:lpass core hw enable failed\n",
331 __func__);
332 } else
333 clk_disable_unprepare(swrm->lpass_core_hw_vote);
334 }
335 }
336 if (core_type == LPASS_AUDIO_CORE) {
337 if (swrm->lpass_core_audio) {
338 if (enable) {
339 ret =
340 clk_prepare_enable(swrm->lpass_core_audio);
341 if (ret < 0)
342 dev_err(swrm->dev,
343 "%s:lpass audio hw enable failed\n",
344 __func__);
345 } else
346 clk_disable_unprepare(swrm->lpass_core_audio);
347 }
348 }
349
350 return ret;
351}
352
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +0530353static int swrm_get_ssp_period(struct swr_mstr_ctrl *swrm,
354 int row, int col,
355 int frame_sync)
356{
357 if (!swrm || !row || !col || !frame_sync)
358 return 1;
359
360 return ((swrm->bus_clk * 2) / ((row * col) * frame_sync));
361}
362
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530363static int swrm_clk_request(struct swr_mstr_ctrl *swrm, bool enable)
364{
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530365 int ret = 0;
366
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530367 if (!swrm->clk || !swrm->handle)
368 return -EINVAL;
369
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530370 mutex_lock(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530371 if (enable) {
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530372 if (!swrm->dev_up) {
373 ret = -ENODEV;
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530374 goto exit;
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530375 }
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -0700376 if (swrm->core_vote) {
377 ret = swrm->core_vote(swrm->handle, true);
378 if (ret) {
379 dev_err_ratelimited(swrm->dev,
380 "%s: clock enable req failed",
381 __func__);
382 goto exit;
383 }
384 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530385 swrm->clk_ref_count++;
386 if (swrm->clk_ref_count == 1) {
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530387 ret = swrm->clk(swrm->handle, true);
388 if (ret) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +0530389 dev_err_ratelimited(swrm->dev,
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530390 "%s: clock enable req failed",
391 __func__);
392 --swrm->clk_ref_count;
393 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530394 }
395 } else if (--swrm->clk_ref_count == 0) {
396 swrm->clk(swrm->handle, false);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530397 complete(&swrm->clk_off_complete);
398 }
399 if (swrm->clk_ref_count < 0) {
Meng Wang8c60bb52019-06-19 15:49:06 +0800400 dev_err(swrm->dev, "%s: swrm clk count mismatch\n", __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530401 swrm->clk_ref_count = 0;
402 }
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530403
404exit:
405 mutex_unlock(&swrm->clklock);
406 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530407}
408
409static int swrm_ahb_write(struct swr_mstr_ctrl *swrm,
410 u16 reg, u32 *value)
411{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530412 u32 temp = (u32)(*value);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530413 int ret = 0;
414
415 mutex_lock(&swrm->devlock);
416 if (!swrm->dev_up)
417 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530418
419 ret = swrm_clk_request(swrm, TRUE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530420 if (ret) {
421 dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
422 __func__);
423 goto err;
424 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530425 iowrite32(temp, swrm->swrm_dig_base + reg);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530426 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530427err:
428 mutex_unlock(&swrm->devlock);
429 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530430}
431
432static int swrm_ahb_read(struct swr_mstr_ctrl *swrm,
433 u16 reg, u32 *value)
434{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530435 u32 temp = 0;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530436 int ret = 0;
437
438 mutex_lock(&swrm->devlock);
439 if (!swrm->dev_up)
440 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530441
442 ret = swrm_clk_request(swrm, TRUE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530443 if (ret) {
444 dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
445 __func__);
446 goto err;
447 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530448 temp = ioread32(swrm->swrm_dig_base + reg);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530449 *value = temp;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530450 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530451err:
452 mutex_unlock(&swrm->devlock);
453 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530454}
455
456static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr)
457{
458 u32 val = 0;
459
460 if (swrm->read)
461 val = swrm->read(swrm->handle, reg_addr);
462 else
463 swrm_ahb_read(swrm, reg_addr, &val);
464 return val;
465}
466
467static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val)
468{
469 if (swrm->write)
470 swrm->write(swrm->handle, reg_addr, val);
471 else
472 swrm_ahb_write(swrm, reg_addr, &val);
473}
474
475static int swr_master_bulk_write(struct swr_mstr_ctrl *swrm, u32 *reg_addr,
476 u32 *val, unsigned int length)
477{
478 int i = 0;
479
480 if (swrm->bulk_write)
481 swrm->bulk_write(swrm->handle, reg_addr, val, length);
482 else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530483 mutex_lock(&swrm->iolock);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530484 for (i = 0; i < length; i++) {
485 /* wait for FIFO WR command to complete to avoid overflow */
Karthikeyan Mani5d52dd82019-08-15 16:58:08 -0700486 /*
487 * Reduce sleep from 100us to 10us to meet KPIs
488 * This still meets the hardware spec
489 */
490 usleep_range(10, 12);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530491 swr_master_write(swrm, reg_addr[i], val[i]);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530492 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530493 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530494 }
495 return 0;
496}
497
Laxminath Kasame2291972019-11-08 14:51:59 +0530498static bool swrm_check_link_status(struct swr_mstr_ctrl *swrm, bool active)
499{
500 int retry = SWRM_LINK_STATUS_RETRY_CNT;
501 int ret = false;
502 int status = active ? 0x1 : 0x0;
503
504 if ((swrm->version <= SWRM_VERSION_1_5_1))
505 return true;
506
507 do {
508 if (swr_master_read(swrm, SWRM_COMP_STATUS) & status) {
509 ret = true;
510 break;
511 }
512 retry--;
513 usleep_range(500, 510);
514 } while (retry);
515
516 if (retry == 0)
517 dev_err(swrm->dev, "%s: link status not %s\n", __func__,
518 active ? "connected" : "disconnected");
519
520 return ret;
521}
522
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530523static bool swrm_is_port_en(struct swr_master *mstr)
524{
525 return !!(mstr->num_port);
526}
527
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530528static void copy_port_tables(struct swr_mstr_ctrl *swrm,
529 struct port_params *params)
530{
531 u8 i;
532 struct port_params *config = params;
533
534 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
535 /* wsa uses single frame structure for all configurations */
536 if (!swrm->mport_cfg[i].port_en)
537 continue;
538 swrm->mport_cfg[i].sinterval = config[i].si;
539 swrm->mport_cfg[i].offset1 = config[i].off1;
540 swrm->mport_cfg[i].offset2 = config[i].off2;
541 swrm->mport_cfg[i].hstart = config[i].hstart;
542 swrm->mport_cfg[i].hstop = config[i].hstop;
543 swrm->mport_cfg[i].blk_pack_mode = config[i].bp_mode;
544 swrm->mport_cfg[i].blk_grp_count = config[i].bgp_ctrl;
545 swrm->mport_cfg[i].word_length = config[i].wd_len;
546 swrm->mport_cfg[i].lane_ctrl = config[i].lane_ctrl;
547 }
548}
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530549static int swrm_get_port_config(struct swr_mstr_ctrl *swrm)
550{
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530551 struct port_params *params;
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530552 u32 usecase = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530553
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530554 /* TODO - Send usecase information to avoid checking for master_id */
555 if (swrm->mport_cfg[SWRM_DSD_PARAMS_PORT].port_en &&
556 (swrm->master_id == MASTER_ID_RX))
557 usecase = 1;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530558
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530559 params = swrm->port_param[usecase];
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530560 copy_port_tables(swrm, params);
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530561
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530562 return 0;
563}
564
565static int swrm_get_master_port(struct swr_mstr_ctrl *swrm, u8 *mstr_port_id,
566 u8 *mstr_ch_mask, u8 mstr_prt_type,
567 u8 slv_port_id)
568{
569 int i, j;
570 *mstr_port_id = 0;
571
572 for (i = 1; i <= swrm->num_ports; i++) {
573 for (j = 0; j < SWR_MAX_CH_PER_PORT; j++) {
574 if (swrm->port_mapping[i][j].port_type == mstr_prt_type)
575 goto found;
576 }
577 }
578found:
579 if (i > swrm->num_ports || j == SWR_MAX_CH_PER_PORT) {
580 dev_err(swrm->dev, "%s: port type not supported by master\n",
581 __func__);
582 return -EINVAL;
583 }
584 /* id 0 corresponds to master port 1 */
585 *mstr_port_id = i - 1;
586 *mstr_ch_mask = swrm->port_mapping[i][j].ch_mask;
587
588 return 0;
589
590}
591
592static u32 swrm_get_packed_reg_val(u8 *cmd_id, u8 cmd_data,
593 u8 dev_addr, u16 reg_addr)
594{
595 u32 val;
596 u8 id = *cmd_id;
597
598 if (id != SWR_BROADCAST_CMD_ID) {
599 if (id < 14)
600 id += 1;
601 else
602 id = 0;
603 *cmd_id = id;
604 }
605 val = SWR_REG_VAL_PACK(cmd_data, dev_addr, id, reg_addr);
606
607 return val;
608}
609
610static int swrm_cmd_fifo_rd_cmd(struct swr_mstr_ctrl *swrm, int *cmd_data,
611 u8 dev_addr, u8 cmd_id, u16 reg_addr,
612 u32 len)
613{
614 u32 val;
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530615 u32 retry_attempt = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530616
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530617 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530618 val = swrm_get_packed_reg_val(&swrm->rcmd_id, len, dev_addr, reg_addr);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530619 if (swrm->read) {
620 /* skip delay if read is handled in platform driver */
621 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
622 } else {
623 /* wait for FIFO RD to complete to avoid overflow */
624 usleep_range(100, 105);
625 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
626 /* wait for FIFO RD CMD complete to avoid overflow */
627 usleep_range(250, 255);
628 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530629retry_read:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530630 *cmd_data = swr_master_read(swrm, SWRM_CMD_FIFO_RD_FIFO_ADDR);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530631 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, rcmd_id: 0x%x, \
632 dev_num: 0x%x, cmd_data: 0x%x\n", __func__, reg_addr,
633 cmd_id, swrm->rcmd_id, dev_addr, *cmd_data);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530634 if ((((*cmd_data) & 0xF00) >> 8) != swrm->rcmd_id) {
635 if (retry_attempt < MAX_FIFO_RD_FAIL_RETRY) {
636 /* wait 500 us before retry on fifo read failure */
637 usleep_range(500, 505);
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +0530638 if (retry_attempt == (MAX_FIFO_RD_FAIL_RETRY - 1)) {
639 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
640 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
641 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530642 retry_attempt++;
643 goto retry_read;
644 } else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530645 dev_err_ratelimited(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, \
646 rcmd_id: 0x%x, dev_num: 0x%x, cmd_data: 0x%x\n",
647 __func__, reg_addr, cmd_id, swrm->rcmd_id,
648 dev_addr, *cmd_data);
649
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530650 dev_err_ratelimited(swrm->dev,
651 "%s: failed to read fifo\n", __func__);
652 }
653 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530654 mutex_unlock(&swrm->iolock);
655
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530656 return 0;
657}
658
659static int swrm_cmd_fifo_wr_cmd(struct swr_mstr_ctrl *swrm, u8 cmd_data,
660 u8 dev_addr, u8 cmd_id, u16 reg_addr)
661{
662 u32 val;
663 int ret = 0;
664
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530665 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530666 if (!cmd_id)
667 val = swrm_get_packed_reg_val(&swrm->wcmd_id, cmd_data,
668 dev_addr, reg_addr);
669 else
670 val = swrm_get_packed_reg_val(&cmd_id, cmd_data,
671 dev_addr, reg_addr);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530672 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x,wcmd_id: 0x%x, \
673 dev_num: 0x%x, cmd_data: 0x%x\n", __func__,
674 reg_addr, cmd_id, swrm->wcmd_id,dev_addr, cmd_data);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +0530675 swr_master_write(swrm, SWRM_CMD_FIFO_WR_CMD, val);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530676 /*
677 * wait for FIFO WR command to complete to avoid overflow
678 * skip delay if write is handled in platform driver.
679 */
680 if(!swrm->write)
Karthikeyan Mani5d52dd82019-08-15 16:58:08 -0700681 usleep_range(150, 155);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530682 if (cmd_id == 0xF) {
683 /*
684 * sleep for 10ms for MSM soundwire variant to allow broadcast
685 * command to complete.
686 */
687 if (swrm_is_msm_variant(swrm->version))
688 usleep_range(10000, 10100);
689 else
690 wait_for_completion_timeout(&swrm->broadcast,
691 (2 * HZ/10));
692 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530693 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530694 return ret;
695}
696
697static int swrm_read(struct swr_master *master, u8 dev_num, u16 reg_addr,
698 void *buf, u32 len)
699{
700 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
701 int ret = 0;
702 int val;
703 u8 *reg_val = (u8 *)buf;
704
705 if (!swrm) {
706 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
707 return -EINVAL;
708 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530709 if (!dev_num) {
710 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
711 return -EINVAL;
712 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530713 mutex_lock(&swrm->devlock);
714 if (!swrm->dev_up) {
715 mutex_unlock(&swrm->devlock);
716 return 0;
717 }
718 mutex_unlock(&swrm->devlock);
719
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530720 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530721 ret = swrm_cmd_fifo_rd_cmd(swrm, &val, dev_num, 0, reg_addr, len);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530722
723 if (!ret)
724 *reg_val = (u8)val;
725
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530726 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530727 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530728 return ret;
729}
730
731static int swrm_write(struct swr_master *master, u8 dev_num, u16 reg_addr,
732 const void *buf)
733{
734 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
735 int ret = 0;
736 u8 reg_val = *(u8 *)buf;
737
738 if (!swrm) {
739 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
740 return -EINVAL;
741 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530742 if (!dev_num) {
743 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
744 return -EINVAL;
745 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530746 mutex_lock(&swrm->devlock);
747 if (!swrm->dev_up) {
748 mutex_unlock(&swrm->devlock);
749 return 0;
750 }
751 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530752
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530753 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530754 ret = swrm_cmd_fifo_wr_cmd(swrm, reg_val, dev_num, 0, reg_addr);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530755
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530756 pm_runtime_put_autosuspend(swrm->dev);
757 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530758 return ret;
759}
760
761static int swrm_bulk_write(struct swr_master *master, u8 dev_num, void *reg,
762 const void *buf, size_t len)
763{
764 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
765 int ret = 0;
766 int i;
767 u32 *val;
768 u32 *swr_fifo_reg;
769
770 if (!swrm || !swrm->handle) {
771 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
772 return -EINVAL;
773 }
774 if (len <= 0)
775 return -EINVAL;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530776 mutex_lock(&swrm->devlock);
777 if (!swrm->dev_up) {
778 mutex_unlock(&swrm->devlock);
779 return 0;
780 }
781 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530782
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530783 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530784 if (dev_num) {
785 swr_fifo_reg = kcalloc(len, sizeof(u32), GFP_KERNEL);
786 if (!swr_fifo_reg) {
787 ret = -ENOMEM;
788 goto err;
789 }
790 val = kcalloc(len, sizeof(u32), GFP_KERNEL);
791 if (!val) {
792 ret = -ENOMEM;
793 goto mem_fail;
794 }
795
796 for (i = 0; i < len; i++) {
797 val[i] = swrm_get_packed_reg_val(&swrm->wcmd_id,
798 ((u8 *)buf)[i],
799 dev_num,
800 ((u16 *)reg)[i]);
801 swr_fifo_reg[i] = SWRM_CMD_FIFO_WR_CMD;
802 }
803 ret = swr_master_bulk_write(swrm, swr_fifo_reg, val, len);
804 if (ret) {
805 dev_err(&master->dev, "%s: bulk write failed\n",
806 __func__);
807 ret = -EINVAL;
808 }
809 } else {
810 dev_err(&master->dev,
811 "%s: No support of Bulk write for master regs\n",
812 __func__);
813 ret = -EINVAL;
814 goto err;
815 }
816 kfree(val);
817mem_fail:
818 kfree(swr_fifo_reg);
819err:
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530820 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530821 pm_runtime_mark_last_busy(swrm->dev);
822 return ret;
823}
824
825static u8 get_inactive_bank_num(struct swr_mstr_ctrl *swrm)
826{
827 return (swr_master_read(swrm, SWRM_MCP_STATUS) &
828 SWRM_MCP_STATUS_BANK_NUM_MASK) ? 0 : 1;
829}
830
831static void enable_bank_switch(struct swr_mstr_ctrl *swrm, u8 bank,
832 u8 row, u8 col)
833{
834 swrm_cmd_fifo_wr_cmd(swrm, ((row << 3) | col), 0xF, 0xF,
835 SWRS_SCP_FRAME_CTRL_BANK(bank));
836}
837
838static struct swr_port_info *swrm_get_port_req(struct swrm_mports *mport,
839 u8 slv_port, u8 dev_num)
840{
841 struct swr_port_info *port_req = NULL;
842
843 list_for_each_entry(port_req, &mport->port_req_list, list) {
844 /* Store dev_id instead of dev_num if enumeration is changed run_time */
845 if ((port_req->slave_port_id == slv_port)
846 && (port_req->dev_num == dev_num))
847 return port_req;
848 }
849 return NULL;
850}
851
852static bool swrm_remove_from_group(struct swr_master *master)
853{
854 struct swr_device *swr_dev;
855 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
856 bool is_removed = false;
857
858 if (!swrm)
859 goto end;
860
861 mutex_lock(&swrm->mlock);
862 if ((swrm->num_rx_chs > 1) &&
863 (swrm->num_rx_chs == swrm->num_cfg_devs)) {
864 list_for_each_entry(swr_dev, &master->devices,
865 dev_list) {
866 swr_dev->group_id = SWR_GROUP_NONE;
867 master->gr_sid = 0;
868 }
869 is_removed = true;
870 }
871 mutex_unlock(&swrm->mlock);
872
873end:
874 return is_removed;
875}
876
877static void swrm_disable_ports(struct swr_master *master,
878 u8 bank)
879{
880 u32 value;
881 struct swr_port_info *port_req;
882 int i;
883 struct swrm_mports *mport;
884 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
885
886 if (!swrm) {
887 pr_err("%s: swrm is null\n", __func__);
888 return;
889 }
890
891 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
892 master->num_port);
893
894
895 for (i = 0; i < SWR_MSTR_PORT_LEN ; i++) {
896
897 mport = &(swrm->mport_cfg[i]);
898 if (!mport->port_en)
899 continue;
900
901 list_for_each_entry(port_req, &mport->port_req_list, list) {
902 /* skip ports with no change req's*/
903 if (port_req->req_ch == port_req->ch_en)
904 continue;
905
906 swrm_cmd_fifo_wr_cmd(swrm, port_req->req_ch,
907 port_req->dev_num, 0x00,
908 SWRS_DP_CHANNEL_ENABLE_BANK(port_req->slave_port_id,
909 bank));
910 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x\n",
911 __func__, i,
912 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)));
913 }
914 value = ((mport->req_ch)
915 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
916 value |= ((mport->offset2)
917 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
918 value |= ((mport->offset1)
919 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
920 value |= mport->sinterval;
921
922 swr_master_write(swrm,
923 SWRM_DP_PORT_CTRL_BANK(i+1, bank),
924 value);
925 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
926 __func__, i,
927 (SWRM_DP_PORT_CTRL_BANK(i+1, bank)), value);
928 }
929}
930
931static void swrm_cleanup_disabled_port_reqs(struct swr_master *master)
932{
933 struct swr_port_info *port_req, *next;
934 int i;
935 struct swrm_mports *mport;
936 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
937
938 if (!swrm) {
939 pr_err("%s: swrm is null\n", __func__);
940 return;
941 }
942 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
943 master->num_port);
944
945 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
946 mport = &(swrm->mport_cfg[i]);
947 list_for_each_entry_safe(port_req, next,
948 &mport->port_req_list, list) {
949 /* skip ports without new ch req */
950 if (port_req->ch_en == port_req->req_ch)
951 continue;
952
953 /* remove new ch req's*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +0530954 port_req->ch_en = port_req->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530955
956 /* If no streams enabled on port, remove the port req */
957 if (port_req->ch_en == 0) {
958 list_del(&port_req->list);
959 kfree(port_req);
960 }
961 }
962 /* remove new ch req's on mport*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +0530963 mport->ch_en = mport->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530964
965 if (!(mport->ch_en)) {
966 mport->port_en = false;
967 master->port_en_mask &= ~i;
968 }
969 }
970}
971static void swrm_copy_data_port_config(struct swr_master *master, u8 bank)
972{
973 u32 value, slv_id;
974 struct swr_port_info *port_req;
975 int i;
976 struct swrm_mports *mport;
977 u32 reg[SWRM_MAX_PORT_REG];
978 u32 val[SWRM_MAX_PORT_REG];
979 int len = 0;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530980 u8 hparams;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530981 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
982
983 if (!swrm) {
984 pr_err("%s: swrm is null\n", __func__);
985 return;
986 }
987
988 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
989 master->num_port);
990
991 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
992 mport = &(swrm->mport_cfg[i]);
993 if (!mport->port_en)
994 continue;
995
996 list_for_each_entry(port_req, &mport->port_req_list, list) {
997 slv_id = port_req->slave_port_id;
998 reg[len] = SWRM_CMD_FIFO_WR_CMD;
999 val[len++] = SWR_REG_VAL_PACK(port_req->req_ch,
1000 port_req->dev_num, 0x00,
1001 SWRS_DP_CHANNEL_ENABLE_BANK(slv_id,
1002 bank));
1003
1004 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1005 val[len++] = SWR_REG_VAL_PACK(mport->sinterval,
1006 port_req->dev_num, 0x00,
1007 SWRS_DP_SAMPLE_CONTROL_1_BANK(slv_id,
1008 bank));
1009
1010 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1011 val[len++] = SWR_REG_VAL_PACK(mport->offset1,
1012 port_req->dev_num, 0x00,
1013 SWRS_DP_OFFSET_CONTROL_1_BANK(slv_id,
1014 bank));
1015
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301016 if (mport->offset2 != SWR_INVALID_PARAM) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301017 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1018 val[len++] = SWR_REG_VAL_PACK(mport->offset2,
1019 port_req->dev_num, 0x00,
1020 SWRS_DP_OFFSET_CONTROL_2_BANK(
1021 slv_id, bank));
1022 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301023 if (mport->hstart != SWR_INVALID_PARAM
1024 && mport->hstop != SWR_INVALID_PARAM) {
1025 hparams = (mport->hstart << 4) | mport->hstop;
1026
1027 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1028 val[len++] = SWR_REG_VAL_PACK(hparams,
1029 port_req->dev_num, 0x00,
1030 SWRS_DP_HCONTROL_BANK(slv_id,
1031 bank));
1032 }
1033 if (mport->word_length != SWR_INVALID_PARAM) {
1034 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1035 val[len++] =
1036 SWR_REG_VAL_PACK(mport->word_length,
1037 port_req->dev_num, 0x00,
1038 SWRS_DP_BLOCK_CONTROL_1(slv_id));
1039 }
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +05301040 if (mport->blk_pack_mode != SWR_INVALID_PARAM
1041 && swrm->master_id != MASTER_ID_WSA) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301042 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1043 val[len++] =
1044 SWR_REG_VAL_PACK(mport->blk_pack_mode,
1045 port_req->dev_num, 0x00,
1046 SWRS_DP_BLOCK_CONTROL_3_BANK(slv_id,
1047 bank));
1048 }
1049 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
1050 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1051 val[len++] =
1052 SWR_REG_VAL_PACK(mport->blk_grp_count,
1053 port_req->dev_num, 0x00,
1054 SWRS_DP_BLOCK_CONTROL_2_BANK(slv_id,
1055 bank));
1056 }
1057 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
1058 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1059 val[len++] =
1060 SWR_REG_VAL_PACK(mport->lane_ctrl,
1061 port_req->dev_num, 0x00,
1062 SWRS_DP_LANE_CONTROL_BANK(slv_id,
1063 bank));
1064 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301065 port_req->ch_en = port_req->req_ch;
1066 }
1067 value = ((mport->req_ch)
1068 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +05301069
1070 if (mport->offset2 != SWR_INVALID_PARAM)
1071 value |= ((mport->offset2)
1072 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301073 value |= ((mport->offset1)
1074 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
1075 value |= mport->sinterval;
1076
1077
1078 reg[len] = SWRM_DP_PORT_CTRL_BANK(i + 1, bank);
1079 val[len++] = value;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301080 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
1081 __func__, i,
1082 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)), value);
1083
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301084 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
1085 reg[len] = SWRM_DP_PORT_CTRL_2_BANK(i + 1, bank);
1086 val[len++] = mport->lane_ctrl;
1087 }
1088 if (mport->word_length != SWR_INVALID_PARAM) {
1089 reg[len] = SWRM_DP_BLOCK_CTRL_1(i + 1);
1090 val[len++] = mport->word_length;
1091 }
1092
1093 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
1094 reg[len] = SWRM_DP_BLOCK_CTRL2_BANK(i + 1, bank);
1095 val[len++] = mport->blk_grp_count;
1096 }
1097 if (mport->hstart != SWR_INVALID_PARAM
1098 && mport->hstop != SWR_INVALID_PARAM) {
1099 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
Laxminath Kasame30eef72018-11-05 17:40:09 +05301100 hparams = (mport->hstop << 4) | mport->hstart;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301101 val[len++] = hparams;
Laxminath Kasam990c70b2018-11-09 23:15:09 +05301102 } else {
1103 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
1104 hparams = (SWR_HSTOP_MAX_VAL << 4) | SWR_HSTART_MIN_VAL;
1105 val[len++] = hparams;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301106 }
1107 if (mport->blk_pack_mode != SWR_INVALID_PARAM) {
1108 reg[len] = SWRM_DP_BLOCK_CTRL3_BANK(i + 1, bank);
1109 val[len++] = mport->blk_pack_mode;
1110 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301111 mport->ch_en = mport->req_ch;
1112
1113 }
Sudheer Papothi0016db12019-06-11 04:42:38 +05301114 swrm_reg_dump(swrm, reg, val, len, __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301115 swr_master_bulk_write(swrm, reg, val, len);
1116}
1117
1118static void swrm_apply_port_config(struct swr_master *master)
1119{
1120 u8 bank;
1121 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1122
1123 if (!swrm) {
1124 pr_err("%s: Invalid handle to swr controller\n",
1125 __func__);
1126 return;
1127 }
1128
1129 bank = get_inactive_bank_num(swrm);
1130 dev_dbg(swrm->dev, "%s: enter bank: %d master_ports: %d\n",
1131 __func__, bank, master->num_port);
1132
1133
1134 swrm_cmd_fifo_wr_cmd(swrm, 0x01, 0xF, 0x00,
1135 SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(bank));
1136
1137 swrm_copy_data_port_config(master, bank);
1138}
1139
1140static int swrm_slvdev_datapath_control(struct swr_master *master, bool enable)
1141{
1142 u8 bank;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301143 u32 value, n_row, n_col;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301144 u32 row = 0, col = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301145 int ret;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301146 u8 ssp_period = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301147 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1148 int mask = (SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK |
1149 SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK |
1150 SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_BMSK);
1151 u8 inactive_bank;
1152
1153 if (!swrm) {
1154 pr_err("%s: swrm is null\n", __func__);
1155 return -EFAULT;
1156 }
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301157
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301158 mutex_lock(&swrm->mlock);
1159
Ramprasad Katkam979b7c92019-05-17 15:31:21 +05301160 /*
1161 * During disable if master is already down, which implies an ssr/pdr
1162 * scenario, just mark ports as disabled and exit
1163 */
1164 if (swrm->state == SWR_MSTR_SSR && !enable) {
1165 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1166 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1167 __func__);
1168 goto exit;
1169 }
1170 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
1171 swrm_cleanup_disabled_port_reqs(master);
1172 if (!swrm_is_port_en(master)) {
1173 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1174 __func__);
1175 pm_runtime_mark_last_busy(swrm->dev);
1176 pm_runtime_put_autosuspend(swrm->dev);
1177 }
1178 goto exit;
1179 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301180 bank = get_inactive_bank_num(swrm);
1181
1182 if (enable) {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301183 if (!test_bit(ENABLE_PENDING, &swrm->port_req_pending)) {
1184 dev_dbg(swrm->dev, "%s:No pending connect port req\n",
1185 __func__);
1186 goto exit;
1187 }
1188 clear_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301189 ret = swrm_get_port_config(swrm);
1190 if (ret) {
1191 /* cannot accommodate ports */
1192 swrm_cleanup_disabled_port_reqs(master);
1193 mutex_unlock(&swrm->mlock);
1194 return -EINVAL;
1195 }
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301196 swr_master_write(swrm, SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301197 SWRM_INTERRUPT_STATUS_MASK);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301198 /* apply the new port config*/
1199 swrm_apply_port_config(master);
1200 } else {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301201 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1202 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1203 __func__);
1204 goto exit;
1205 }
1206 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301207 swrm_disable_ports(master, bank);
1208 }
1209 dev_dbg(swrm->dev, "%s: enable: %d, cfg_devs: %d\n",
1210 __func__, enable, swrm->num_cfg_devs);
1211
1212 if (enable) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301213 /* set col = 16 */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301214 n_col = SWR_MAX_COL;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301215 col = SWRM_COL_16;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301216 } else {
1217 /*
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301218 * Do not change to col = 2 if there are still active ports
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301219 */
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301220 if (!master->num_port) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301221 n_col = SWR_MIN_COL;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301222 col = SWRM_COL_02;
1223 } else {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301224 n_col = SWR_MAX_COL;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301225 col = SWRM_COL_16;
1226 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301227 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301228 /* Use default 50 * x, frame shape. Change based on mclk */
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301229 if (swrm->mclk_freq == MCLK_FREQ_NATIVE) {
1230 dev_dbg(swrm->dev, "setting 64 x %d frameshape\n",
1231 n_col ? 16 : 2);
1232 n_row = SWR_ROW_64;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301233 row = SWRM_ROW_64;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301234 } else {
1235 dev_dbg(swrm->dev, "setting 50 x %d frameshape\n",
1236 n_col ? 16 : 2);
1237 n_row = SWR_ROW_50;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301238 row = SWRM_ROW_50;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301239 }
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301240 ssp_period = swrm_get_ssp_period(swrm, row, col, SWRM_FRAME_SYNC_SEL);
1241 dev_dbg(swrm->dev, "%s: ssp_period: %d\n", __func__, ssp_period);
1242
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301243 value = swr_master_read(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank));
1244 value &= (~mask);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301245 value |= ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301246 (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301247 ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301248 swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1249
1250 dev_dbg(swrm->dev, "%s: regaddr: 0x%x, value: 0x%x\n", __func__,
1251 SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1252
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301253 enable_bank_switch(swrm, bank, n_row, n_col);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301254 inactive_bank = bank ? 0 : 1;
1255
1256 if (enable)
1257 swrm_copy_data_port_config(master, inactive_bank);
1258 else {
1259 swrm_disable_ports(master, inactive_bank);
1260 swrm_cleanup_disabled_port_reqs(master);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301261 }
1262 if (!swrm_is_port_en(master)) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301263 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1264 __func__);
1265 pm_runtime_mark_last_busy(swrm->dev);
1266 pm_runtime_put_autosuspend(swrm->dev);
1267 }
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301268exit:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301269 mutex_unlock(&swrm->mlock);
1270return 0;
1271}
1272
1273static int swrm_connect_port(struct swr_master *master,
1274 struct swr_params *portinfo)
1275{
1276 int i;
1277 struct swr_port_info *port_req;
1278 int ret = 0;
1279 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1280 struct swrm_mports *mport;
1281 u8 mstr_port_id, mstr_ch_msk;
1282
1283 dev_dbg(&master->dev, "%s: enter\n", __func__);
1284 if (!portinfo)
1285 return -EINVAL;
1286
1287 if (!swrm) {
1288 dev_err(&master->dev,
1289 "%s: Invalid handle to swr controller\n",
1290 __func__);
1291 return -EINVAL;
1292 }
1293
1294 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301295 mutex_lock(&swrm->devlock);
1296 if (!swrm->dev_up) {
1297 mutex_unlock(&swrm->devlock);
1298 mutex_unlock(&swrm->mlock);
1299 return -EINVAL;
1300 }
1301 mutex_unlock(&swrm->devlock);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301302 if (!swrm_is_port_en(master))
1303 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301304
1305 for (i = 0; i < portinfo->num_port; i++) {
1306 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_msk,
1307 portinfo->port_type[i],
1308 portinfo->port_id[i]);
1309 if (ret) {
1310 dev_err(&master->dev,
1311 "%s: mstr portid for slv port %d not found\n",
1312 __func__, portinfo->port_id[i]);
1313 goto port_fail;
1314 }
1315
1316 mport = &(swrm->mport_cfg[mstr_port_id]);
1317 /* get port req */
1318 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1319 portinfo->dev_num);
1320 if (!port_req) {
1321 dev_dbg(&master->dev, "%s: new req:port id %d dev %d\n",
1322 __func__, portinfo->port_id[i],
1323 portinfo->dev_num);
1324 port_req = kzalloc(sizeof(struct swr_port_info),
1325 GFP_KERNEL);
1326 if (!port_req) {
1327 ret = -ENOMEM;
1328 goto mem_fail;
1329 }
1330 port_req->dev_num = portinfo->dev_num;
1331 port_req->slave_port_id = portinfo->port_id[i];
1332 port_req->num_ch = portinfo->num_ch[i];
1333 port_req->ch_rate = portinfo->ch_rate[i];
1334 port_req->ch_en = 0;
1335 port_req->master_port_id = mstr_port_id;
1336 list_add(&port_req->list, &mport->port_req_list);
1337 }
1338 port_req->req_ch |= portinfo->ch_en[i];
1339
1340 dev_dbg(&master->dev,
1341 "%s: mstr port %d, slv port %d ch_rate %d num_ch %d\n",
1342 __func__, port_req->master_port_id,
1343 port_req->slave_port_id, port_req->ch_rate,
1344 port_req->num_ch);
1345 /* Put the port req on master port */
1346 mport = &(swrm->mport_cfg[mstr_port_id]);
1347 mport->port_en = true;
1348 mport->req_ch |= mstr_ch_msk;
1349 master->port_en_mask |= (1 << mstr_port_id);
1350 }
1351 master->num_port += portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301352 set_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301353 swr_port_response(master, portinfo->tid);
1354
1355 mutex_unlock(&swrm->mlock);
1356 return 0;
1357
1358port_fail:
1359mem_fail:
1360 /* cleanup port reqs in error condition */
1361 swrm_cleanup_disabled_port_reqs(master);
1362 mutex_unlock(&swrm->mlock);
1363 return ret;
1364}
1365
1366static int swrm_disconnect_port(struct swr_master *master,
1367 struct swr_params *portinfo)
1368{
1369 int i, ret = 0;
1370 struct swr_port_info *port_req;
1371 struct swrm_mports *mport;
1372 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1373 u8 mstr_port_id, mstr_ch_mask;
1374
1375 if (!swrm) {
1376 dev_err(&master->dev,
1377 "%s: Invalid handle to swr controller\n",
1378 __func__);
1379 return -EINVAL;
1380 }
1381
1382 if (!portinfo) {
1383 dev_err(&master->dev, "%s: portinfo is NULL\n", __func__);
1384 return -EINVAL;
1385 }
1386 mutex_lock(&swrm->mlock);
1387
1388 for (i = 0; i < portinfo->num_port; i++) {
1389
1390 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_mask,
1391 portinfo->port_type[i], portinfo->port_id[i]);
1392 if (ret) {
1393 dev_err(&master->dev,
1394 "%s: mstr portid for slv port %d not found\n",
1395 __func__, portinfo->port_id[i]);
1396 mutex_unlock(&swrm->mlock);
1397 return -EINVAL;
1398 }
1399 mport = &(swrm->mport_cfg[mstr_port_id]);
1400 /* get port req */
1401 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1402 portinfo->dev_num);
1403
1404 if (!port_req) {
1405 dev_err(&master->dev, "%s:port not enabled : port %d\n",
1406 __func__, portinfo->port_id[i]);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05301407 mutex_unlock(&swrm->mlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301408 return -EINVAL;
1409 }
1410 port_req->req_ch &= ~portinfo->ch_en[i];
1411 mport->req_ch &= ~mstr_ch_mask;
1412 }
1413 master->num_port -= portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301414 set_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301415 swr_port_response(master, portinfo->tid);
1416 mutex_unlock(&swrm->mlock);
1417
1418 return 0;
1419}
1420
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301421static int swrm_find_alert_slave(struct swr_mstr_ctrl *swrm,
1422 int status, u8 *devnum)
1423{
1424 int i;
1425 bool found = false;
1426
1427 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1428 if ((status & SWRM_MCP_SLV_STATUS_MASK) == SWR_ALERT) {
1429 *devnum = i;
1430 found = true;
1431 break;
1432 }
1433 status >>= 2;
1434 }
1435 if (found)
1436 return 0;
1437 else
1438 return -EINVAL;
1439}
1440
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301441static void swrm_enable_slave_irq(struct swr_mstr_ctrl *swrm)
1442{
1443 int i;
1444 int status = 0;
1445
1446 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1447 if (!status) {
1448 dev_dbg_ratelimited(swrm->dev, "%s: slaves status is 0x%x\n",
1449 __func__, status);
1450 return;
1451 }
1452 dev_dbg(swrm->dev, "%s: slave status: 0x%x\n", __func__, status);
1453 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1454 if (status & SWRM_MCP_SLV_STATUS_MASK)
1455 swrm_cmd_fifo_wr_cmd(swrm, 0x4, i, 0x0,
1456 SWRS_SCP_INT_STATUS_MASK_1);
1457 status >>= 2;
1458 }
1459}
1460
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301461static int swrm_check_slave_change_status(struct swr_mstr_ctrl *swrm,
1462 int status, u8 *devnum)
1463{
1464 int i;
1465 int new_sts = status;
1466 int ret = SWR_NOT_PRESENT;
1467
1468 if (status != swrm->slave_status) {
1469 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1470 if ((status & SWRM_MCP_SLV_STATUS_MASK) !=
1471 (swrm->slave_status & SWRM_MCP_SLV_STATUS_MASK)) {
1472 ret = (status & SWRM_MCP_SLV_STATUS_MASK);
1473 *devnum = i;
1474 break;
1475 }
1476 status >>= 2;
1477 swrm->slave_status >>= 2;
1478 }
1479 swrm->slave_status = new_sts;
1480 }
1481 return ret;
1482}
1483
1484static irqreturn_t swr_mstr_interrupt(int irq, void *dev)
1485{
1486 struct swr_mstr_ctrl *swrm = dev;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301487 u32 value, intr_sts, intr_sts_masked;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301488 u32 temp = 0;
1489 u32 status, chg_sts, i;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301490 u8 devnum = 0;
1491 int ret = IRQ_HANDLED;
1492 struct swr_device *swr_dev;
1493 struct swr_master *mstr = &swrm->master;
1494
Ramprasad Katkam57349872018-11-11 18:34:57 +05301495 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1496 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1497 return IRQ_NONE;
1498 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301499
1500 mutex_lock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301501 if (swrm_clk_request(swrm, true)) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +05301502 dev_err_ratelimited(swrm->dev, "%s:clk request failed\n",
1503 __func__);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301504 mutex_unlock(&swrm->reslock);
1505 goto exit;
1506 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301507 mutex_unlock(&swrm->reslock);
1508
1509 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301510 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301511handle_irq:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301512 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301513 value = intr_sts_masked & (1 << i);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301514 if (!value)
1515 continue;
1516
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301517 switch (value) {
1518 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1519 dev_dbg(swrm->dev, "Trigger irq to slave device\n");
1520 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301521 ret = swrm_find_alert_slave(swrm, status, &devnum);
1522 if (ret) {
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301523 dev_err_ratelimited(swrm->dev,
1524 "no slave alert found.spurious interrupt\n");
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05301525 break;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301526 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301527 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1528 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1529 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1530 SWRS_SCP_INT_STATUS_CLEAR_1);
1531 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1532 SWRS_SCP_INT_STATUS_CLEAR_1);
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301533
1534
1535 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1536 if (swr_dev->dev_num != devnum)
1537 continue;
1538 if (swr_dev->slave_irq) {
1539 do {
Ramprasad Katkam2586a4b2019-03-18 16:53:39 +05301540 swr_dev->slave_irq_pending = 0;
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301541 handle_nested_irq(
1542 irq_find_mapping(
1543 swr_dev->slave_irq, 0));
1544 } while (swr_dev->slave_irq_pending);
1545 }
1546
1547 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301548 break;
1549 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1550 dev_dbg(swrm->dev, "SWR new slave attached\n");
1551 break;
1552 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1553 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1554 if (status == swrm->slave_status) {
1555 dev_dbg(swrm->dev,
1556 "%s: No change in slave status: %d\n",
1557 __func__, status);
1558 break;
1559 }
1560 chg_sts = swrm_check_slave_change_status(swrm, status,
1561 &devnum);
1562 switch (chg_sts) {
1563 case SWR_NOT_PRESENT:
1564 dev_dbg(swrm->dev, "device %d got detached\n",
1565 devnum);
1566 break;
1567 case SWR_ATTACHED_OK:
1568 dev_dbg(swrm->dev, "device %d got attached\n",
1569 devnum);
Ramprasad Katkamdebe8932018-09-25 18:08:18 +05301570 /* enable host irq from slave device*/
1571 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1572 SWRS_SCP_INT_STATUS_CLEAR_1);
1573 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1574 SWRS_SCP_INT_STATUS_MASK_1);
1575
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301576 break;
1577 case SWR_ALERT:
1578 dev_dbg(swrm->dev,
1579 "device %d has pending interrupt\n",
1580 devnum);
1581 break;
1582 }
1583 break;
1584 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1585 dev_err_ratelimited(swrm->dev,
1586 "SWR bus clsh detected\n");
1587 break;
1588 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1589 dev_dbg(swrm->dev, "SWR read FIFO overflow\n");
1590 break;
1591 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1592 dev_dbg(swrm->dev, "SWR read FIFO underflow\n");
1593 break;
1594 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1595 dev_dbg(swrm->dev, "SWR write FIFO overflow\n");
1596 break;
1597 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1598 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1599 dev_err_ratelimited(swrm->dev,
1600 "SWR CMD error, fifo status 0x%x, flushing fifo\n",
1601 value);
1602 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1603 break;
1604 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301605 dev_err_ratelimited(swrm->dev, "SWR Port collision detected\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301606 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301607 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301608 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301609 break;
1610 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1611 dev_dbg(swrm->dev, "SWR read enable valid mismatch\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301612 swrm->intr_mask &=
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301613 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1614 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301615 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301616 break;
1617 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1618 complete(&swrm->broadcast);
1619 dev_dbg(swrm->dev, "SWR cmd id finished\n");
1620 break;
1621 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_AUTO_ENUM_FINISHED:
1622 break;
1623 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED:
1624 break;
1625 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL:
1626 break;
1627 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED:
1628 complete(&swrm->reset);
1629 break;
1630 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED:
1631 break;
1632 default:
1633 dev_err_ratelimited(swrm->dev,
1634 "SWR unknown interrupt\n");
1635 ret = IRQ_NONE;
1636 break;
1637 }
1638 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301639 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1640 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
Ramprasad Katkam83303512018-10-11 17:34:22 +05301641
1642 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301643 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301644
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301645 if (intr_sts_masked) {
Ramprasad Katkam83303512018-10-11 17:34:22 +05301646 dev_dbg(swrm->dev, "%s: new interrupt received\n", __func__);
1647 goto handle_irq;
1648 }
1649
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301650 mutex_lock(&swrm->reslock);
1651 swrm_clk_request(swrm, false);
1652 mutex_unlock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301653exit:
Ramprasad Katkam57349872018-11-11 18:34:57 +05301654 swrm_unlock_sleep(swrm);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301655 return ret;
1656}
1657
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301658static irqreturn_t swr_mstr_interrupt_v2(int irq, void *dev)
1659{
1660 struct swr_mstr_ctrl *swrm = dev;
1661 u32 value, intr_sts, intr_sts_masked;
1662 u32 temp = 0;
1663 u32 status, chg_sts, i;
1664 u8 devnum = 0;
1665 int ret = IRQ_HANDLED;
1666 struct swr_device *swr_dev;
1667 struct swr_master *mstr = &swrm->master;
1668
1669 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1670 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1671 return IRQ_NONE;
1672 }
1673
1674 mutex_lock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301675 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
1676 ret = IRQ_NONE;
1677 goto exit;
1678 }
1679 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
1680 ret = IRQ_NONE;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301681 goto err_audio_hw_vote;
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001682 }
Karthikeyan Mani4bee1db2019-09-18 17:58:41 -07001683 ret = swrm_clk_request(swrm, true);
1684 if (ret) {
1685 dev_err(dev, "%s: swrm clk failed\n", __func__);
1686 ret = IRQ_NONE;
1687 goto err_audio_core_vote;
1688 }
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301689 mutex_unlock(&swrm->reslock);
1690
1691 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1692 intr_sts_masked = intr_sts & swrm->intr_mask;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301693
1694 dev_dbg(swrm->dev, "%s: status: 0x%x \n", __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301695handle_irq:
1696 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
1697 value = intr_sts_masked & (1 << i);
1698 if (!value)
1699 continue;
1700
1701 switch (value) {
1702 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1703 dev_dbg(swrm->dev, "%s: Trigger irq to slave device\n",
1704 __func__);
1705 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1706 ret = swrm_find_alert_slave(swrm, status, &devnum);
1707 if (ret) {
1708 dev_err_ratelimited(swrm->dev,
1709 "%s: no slave alert found.spurious interrupt\n",
1710 __func__);
1711 break;
1712 }
1713 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1714 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1715 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1716 SWRS_SCP_INT_STATUS_CLEAR_1);
1717 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1718 SWRS_SCP_INT_STATUS_CLEAR_1);
1719
1720
1721 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1722 if (swr_dev->dev_num != devnum)
1723 continue;
1724 if (swr_dev->slave_irq) {
1725 do {
1726 handle_nested_irq(
1727 irq_find_mapping(
1728 swr_dev->slave_irq, 0));
1729 } while (swr_dev->slave_irq_pending);
1730 }
1731
1732 }
1733 break;
1734 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1735 dev_dbg(swrm->dev, "%s: SWR new slave attached\n",
1736 __func__);
1737 break;
1738 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1739 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
Laxminath Kasam44cedb82019-11-20 17:37:23 +05301740 swrm_enable_slave_irq(swrm);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301741 if (status == swrm->slave_status) {
1742 dev_dbg(swrm->dev,
1743 "%s: No change in slave status: %d\n",
1744 __func__, status);
1745 break;
1746 }
1747 chg_sts = swrm_check_slave_change_status(swrm, status,
1748 &devnum);
1749 switch (chg_sts) {
1750 case SWR_NOT_PRESENT:
1751 dev_dbg(swrm->dev,
1752 "%s: device %d got detached\n",
1753 __func__, devnum);
1754 break;
1755 case SWR_ATTACHED_OK:
1756 dev_dbg(swrm->dev,
1757 "%s: device %d got attached\n",
1758 __func__, devnum);
1759 /* enable host irq from slave device*/
1760 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1761 SWRS_SCP_INT_STATUS_CLEAR_1);
1762 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1763 SWRS_SCP_INT_STATUS_MASK_1);
1764
1765 break;
1766 case SWR_ALERT:
1767 dev_dbg(swrm->dev,
1768 "%s: device %d has pending interrupt\n",
1769 __func__, devnum);
1770 break;
1771 }
1772 break;
1773 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1774 dev_err_ratelimited(swrm->dev,
1775 "%s: SWR bus clsh detected\n",
1776 __func__);
1777 break;
1778 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1779 dev_dbg(swrm->dev, "%s: SWR read FIFO overflow\n",
1780 __func__);
1781 break;
1782 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1783 dev_dbg(swrm->dev, "%s: SWR read FIFO underflow\n",
1784 __func__);
1785 break;
1786 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1787 dev_dbg(swrm->dev, "%s: SWR write FIFO overflow\n",
1788 __func__);
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301789 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301790 break;
1791 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1792 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1793 dev_err_ratelimited(swrm->dev,
1794 "%s: SWR CMD error, fifo status 0x%x, flushing fifo\n",
1795 __func__, value);
1796 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1797 break;
1798 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
1799 dev_err_ratelimited(swrm->dev,
1800 "%s: SWR Port collision detected\n",
1801 __func__);
1802 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
1803 swr_master_write(swrm,
1804 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1805 break;
1806 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1807 dev_dbg(swrm->dev,
1808 "%s: SWR read enable valid mismatch\n",
1809 __func__);
1810 swrm->intr_mask &=
1811 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1812 swr_master_write(swrm,
1813 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1814 break;
1815 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1816 complete(&swrm->broadcast);
1817 dev_dbg(swrm->dev, "%s: SWR cmd id finished\n",
1818 __func__);
1819 break;
1820 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED_V2:
1821 break;
1822 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL_V2:
1823 break;
1824 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED_V2:
Laxminath Kasame2291972019-11-08 14:51:59 +05301825 swrm_check_link_status(swrm, 0x1);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301826 break;
1827 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED_V2:
1828 break;
1829 case SWRM_INTERRUPT_STATUS_EXT_CLK_STOP_WAKEUP:
1830 if (swrm->state == SWR_MSTR_UP)
1831 dev_dbg(swrm->dev,
1832 "%s:SWR Master is already up\n",
1833 __func__);
1834 else
1835 dev_err_ratelimited(swrm->dev,
1836 "%s: SWR wokeup during clock stop\n",
1837 __func__);
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301838 /* It might be possible the slave device gets reset
1839 * and slave interrupt gets missed. So re-enable
1840 * Host IRQ and process slave pending
1841 * interrupts, if any.
1842 */
1843 swrm_enable_slave_irq(swrm);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301844 break;
1845 default:
1846 dev_err_ratelimited(swrm->dev,
1847 "%s: SWR unknown interrupt value: %d\n",
1848 __func__, value);
1849 ret = IRQ_NONE;
1850 break;
1851 }
1852 }
1853 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1854 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
1855
1856 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1857 intr_sts_masked = intr_sts & swrm->intr_mask;
1858
1859 if (intr_sts_masked) {
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301860 dev_dbg(swrm->dev, "%s: new interrupt received 0x%x\n",
1861 __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301862 goto handle_irq;
1863 }
1864
1865 mutex_lock(&swrm->reslock);
1866 swrm_clk_request(swrm, false);
Karthikeyan Mani4bee1db2019-09-18 17:58:41 -07001867err_audio_core_vote:
Sudheer Papothi384addd2019-06-14 02:26:52 +05301868 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
Sudheer Papothi06f43412019-07-09 03:32:54 +05301869
1870err_audio_hw_vote:
Sudheer Papothi384addd2019-06-14 02:26:52 +05301871 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001872exit:
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301873 mutex_unlock(&swrm->reslock);
1874 swrm_unlock_sleep(swrm);
1875 return ret;
1876}
1877
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301878static irqreturn_t swrm_wakeup_interrupt(int irq, void *dev)
1879{
1880 struct swr_mstr_ctrl *swrm = dev;
1881 int ret = IRQ_HANDLED;
1882
1883 if (!swrm || !(swrm->dev)) {
1884 pr_err("%s: swrm or dev is null\n", __func__);
1885 return IRQ_NONE;
1886 }
1887 mutex_lock(&swrm->devlock);
1888 if (!swrm->dev_up) {
1889 if (swrm->wake_irq > 0)
1890 disable_irq_nosync(swrm->wake_irq);
1891 mutex_unlock(&swrm->devlock);
1892 return ret;
1893 }
1894 mutex_unlock(&swrm->devlock);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301895 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1896 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1897 goto exit;
1898 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301899 if (swrm->wake_irq > 0)
1900 disable_irq_nosync(swrm->wake_irq);
1901 pm_runtime_get_sync(swrm->dev);
1902 pm_runtime_mark_last_busy(swrm->dev);
1903 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301904 swrm_unlock_sleep(swrm);
1905exit:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301906 return ret;
1907}
1908
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301909static void swrm_wakeup_work(struct work_struct *work)
1910{
1911 struct swr_mstr_ctrl *swrm;
1912
1913 swrm = container_of(work, struct swr_mstr_ctrl,
1914 wakeup_work);
1915 if (!swrm || !(swrm->dev)) {
1916 pr_err("%s: swrm or dev is null\n", __func__);
1917 return;
1918 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301919
1920 mutex_lock(&swrm->devlock);
1921 if (!swrm->dev_up) {
1922 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301923 goto exit;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301924 }
1925 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301926 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1927 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1928 goto exit;
1929 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301930 pm_runtime_get_sync(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301931 pm_runtime_mark_last_busy(swrm->dev);
1932 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301933 swrm_unlock_sleep(swrm);
1934exit:
1935 pm_relax(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301936}
1937
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301938static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
1939{
1940 u32 val;
1941
1942 swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1943 val = (swrm->slave_status >> (devnum * 2));
1944 val &= SWRM_MCP_SLV_STATUS_MASK;
1945 return val;
1946}
1947
1948static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
1949 u8 *dev_num)
1950{
1951 int i;
1952 u64 id = 0;
1953 int ret = -EINVAL;
1954 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1955 struct swr_device *swr_dev;
1956 u32 num_dev = 0;
1957
1958 if (!swrm) {
1959 pr_err("%s: Invalid handle to swr controller\n",
1960 __func__);
1961 return ret;
1962 }
1963 if (swrm->num_dev)
1964 num_dev = swrm->num_dev;
1965 else
1966 num_dev = mstr->num_dev;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301967
1968 mutex_lock(&swrm->devlock);
1969 if (!swrm->dev_up) {
1970 mutex_unlock(&swrm->devlock);
1971 return ret;
1972 }
1973 mutex_unlock(&swrm->devlock);
1974
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301975 pm_runtime_get_sync(swrm->dev);
1976 for (i = 1; i < (num_dev + 1); i++) {
1977 id = ((u64)(swr_master_read(swrm,
1978 SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
1979 id |= swr_master_read(swrm,
1980 SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301981
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301982 /*
1983 * As pm_runtime_get_sync() brings all slaves out of reset
1984 * update logical device number for all slaves.
1985 */
1986 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1987 if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
1988 u32 status = swrm_get_device_status(swrm, i);
1989
1990 if ((status == 0x01) || (status == 0x02)) {
1991 swr_dev->dev_num = i;
1992 if ((id & SWR_DEV_ID_MASK) == dev_id) {
1993 *dev_num = i;
1994 ret = 0;
1995 }
1996 dev_dbg(swrm->dev,
1997 "%s: devnum %d is assigned for dev addr %lx\n",
1998 __func__, i, swr_dev->addr);
1999 }
2000 }
2001 }
2002 }
2003 if (ret)
2004 dev_err(swrm->dev, "%s: device 0x%llx is not ready\n",
2005 __func__, dev_id);
2006
2007 pm_runtime_mark_last_busy(swrm->dev);
2008 pm_runtime_put_autosuspend(swrm->dev);
2009 return ret;
2010}
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302011
2012static void swrm_device_wakeup_vote(struct swr_master *mstr)
2013{
2014 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
2015
2016 if (!swrm) {
2017 pr_err("%s: Invalid handle to swr controller\n",
2018 __func__);
2019 return;
2020 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302021 if (unlikely(swrm_lock_sleep(swrm) == false)) {
2022 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
2023 return;
2024 }
Sudheer Papothi384addd2019-06-14 02:26:52 +05302025 if (++swrm->hw_core_clk_en == 1)
2026 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2027 dev_err(swrm->dev, "%s:lpass core hw enable failed\n",
2028 __func__);
2029 --swrm->hw_core_clk_en;
2030 }
2031 if ( ++swrm->aud_core_clk_en == 1)
2032 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2033 dev_err(swrm->dev, "%s:lpass audio hw enable failed\n",
2034 __func__);
2035 --swrm->aud_core_clk_en;
2036 }
2037 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
2038 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302039 pm_runtime_get_sync(swrm->dev);
2040}
2041
2042static void swrm_device_wakeup_unvote(struct swr_master *mstr)
2043{
2044 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
2045
2046 if (!swrm) {
2047 pr_err("%s: Invalid handle to swr controller\n",
2048 __func__);
2049 return;
2050 }
2051 pm_runtime_mark_last_busy(swrm->dev);
2052 pm_runtime_put_autosuspend(swrm->dev);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302053 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
2054 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
2055
2056 --swrm->aud_core_clk_en;
2057 if (swrm->aud_core_clk_en < 0)
2058 swrm->aud_core_clk_en = 0;
2059 else if (swrm->aud_core_clk_en == 0)
2060 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2061
2062 --swrm->hw_core_clk_en;
2063 if (swrm->hw_core_clk_en < 0)
2064 swrm->hw_core_clk_en = 0;
2065 else if (swrm->hw_core_clk_en == 0)
2066 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
2067
Ramprasad Katkam57349872018-11-11 18:34:57 +05302068 swrm_unlock_sleep(swrm);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302069}
2070
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302071static int swrm_master_init(struct swr_mstr_ctrl *swrm)
2072{
2073 int ret = 0;
2074 u32 val;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302075 u8 row_ctrl = SWR_ROW_50;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302076 u8 col_ctrl = SWR_MIN_COL;
2077 u8 ssp_period = 1;
2078 u8 retry_cmd_num = 3;
2079 u32 reg[SWRM_MAX_INIT_REG];
2080 u32 value[SWRM_MAX_INIT_REG];
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302081 u32 temp = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302082 int len = 0;
2083
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302084 ssp_period = swrm_get_ssp_period(swrm, SWRM_ROW_50,
2085 SWRM_COL_02, SWRM_FRAME_SYNC_SEL);
2086 dev_dbg(swrm->dev, "%s: ssp_period: %d\n", __func__, ssp_period);
2087
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302088 /* Clear Rows and Cols */
2089 val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
2090 (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302091 ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302092
2093 reg[len] = SWRM_MCP_FRAME_CTRL_BANK_ADDR(0);
2094 value[len++] = val;
2095
2096 /* Set Auto enumeration flag */
2097 reg[len] = SWRM_ENUMERATOR_CFG_ADDR;
2098 value[len++] = 1;
2099
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302100 /* Configure No pings */
2101 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2102 val &= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK;
2103 val |= (0x1f << SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT);
2104 reg[len] = SWRM_MCP_CFG_ADDR;
2105 value[len++] = val;
2106
2107 /* Configure number of retries of a read/write cmd */
2108 val = (retry_cmd_num << SWRM_CMD_FIFO_CFG_NUM_OF_CMD_RETRY_SHFT);
2109 reg[len] = SWRM_CMD_FIFO_CFG_ADDR;
2110 value[len++] = val;
2111
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302112 reg[len] = SWRM_MCP_BUS_CTRL_ADDR;
2113 value[len++] = 0x2;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302114
Ramprasad Katkam83303512018-10-11 17:34:22 +05302115 /* Set IRQ to PULSE */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302116 reg[len] = SWRM_COMP_CFG_ADDR;
Ramprasad Katkam83303512018-10-11 17:34:22 +05302117 value[len++] = 0x02;
2118
2119 reg[len] = SWRM_COMP_CFG_ADDR;
2120 value[len++] = 0x03;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302121
2122 reg[len] = SWRM_INTERRUPT_CLEAR;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302123 value[len++] = 0xFFFFFFFF;
2124
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302125 swrm->intr_mask = SWRM_INTERRUPT_STATUS_MASK;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302126 /* Mask soundwire interrupts */
2127 reg[len] = SWRM_INTERRUPT_MASK_ADDR;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302128 value[len++] = swrm->intr_mask;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302129
2130 reg[len] = SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302131 value[len++] = swrm->intr_mask;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302132
2133 swr_master_bulk_write(swrm, reg, value, len);
2134
Sudheer Papothi63f48152018-11-15 01:08:03 +05302135 /*
2136 * For SWR master version 1.5.1, continue
2137 * execute on command ignore.
2138 */
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302139 /* Execute it for versions >= 1.5.1 */
2140 if (swrm->version >= SWRM_VERSION_1_5_1)
Sudheer Papothi63f48152018-11-15 01:08:03 +05302141 swr_master_write(swrm, SWRM_CMD_FIFO_CFG_ADDR,
2142 (swr_master_read(swrm,
2143 SWRM_CMD_FIFO_CFG_ADDR) | 0x80000000));
2144
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302145 /* SW workaround to gate hw_ctl for SWR version >=1.6 */
2146 if (swrm->version >= SWRM_VERSION_1_6) {
2147 if (swrm->swrm_hctl_reg) {
2148 temp = ioread32(swrm->swrm_hctl_reg);
2149 temp &= 0xFFFFFFFD;
2150 iowrite32(temp, swrm->swrm_hctl_reg);
2151 }
2152 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302153 return ret;
2154}
2155
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302156static int swrm_event_notify(struct notifier_block *self,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302157 unsigned long action, void *data)
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302158{
2159 struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302160 event_notifier);
2161
2162 if (!swrm || !(swrm->dev)) {
2163 pr_err("%s: swrm or dev is NULL\n", __func__);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302164 return -EINVAL;
2165 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302166 switch (action) {
2167 case MSM_AUD_DC_EVENT:
2168 schedule_work(&(swrm->dc_presence_work));
2169 break;
2170 case SWR_WAKE_IRQ_EVENT:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302171 if (swrm->ipc_wakeup && !swrm->ipc_wakeup_triggered) {
2172 swrm->ipc_wakeup_triggered = true;
Ramprasad Katkam57349872018-11-11 18:34:57 +05302173 pm_stay_awake(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302174 schedule_work(&swrm->wakeup_work);
Ramprasad Katkamcd61c6e2018-09-18 13:22:58 +05302175 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302176 break;
2177 default:
2178 dev_err(swrm->dev, "%s: invalid event type: %lu\n",
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302179 __func__, action);
2180 return -EINVAL;
2181 }
2182
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302183 return 0;
2184}
2185
2186static void swrm_notify_work_fn(struct work_struct *work)
2187{
2188 struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302189 dc_presence_work);
2190
2191 if (!swrm || !swrm->pdev) {
2192 pr_err("%s: swrm or pdev is NULL\n", __func__);
2193 return;
2194 }
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302195 swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL);
2196}
2197
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302198static int swrm_probe(struct platform_device *pdev)
2199{
2200 struct swr_mstr_ctrl *swrm;
2201 struct swr_ctrl_platform_data *pdata;
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302202 u32 i, num_ports, port_num, port_type, ch_mask, swrm_hctl_reg = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302203 u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0;
2204 int ret = 0;
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302205 struct clk *lpass_core_hw_vote = NULL;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302206 struct clk *lpass_core_audio = NULL;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302207
2208 /* Allocate soundwire master driver structure */
2209 swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl),
2210 GFP_KERNEL);
2211 if (!swrm) {
2212 ret = -ENOMEM;
2213 goto err_memory_fail;
2214 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302215 swrm->pdev = pdev;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302216 swrm->dev = &pdev->dev;
2217 platform_set_drvdata(pdev, swrm);
2218 swr_set_ctrl_data(&swrm->master, swrm);
2219 pdata = dev_get_platdata(&pdev->dev);
2220 if (!pdata) {
2221 dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
2222 __func__);
2223 ret = -EINVAL;
2224 goto err_pdata_fail;
2225 }
2226 swrm->handle = (void *)pdata->handle;
2227 if (!swrm->handle) {
2228 dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
2229 __func__);
2230 ret = -EINVAL;
2231 goto err_pdata_fail;
2232 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302233 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr_master_id",
2234 &swrm->master_id);
2235 if (ret) {
2236 dev_err(&pdev->dev, "%s: failed to get master id\n", __func__);
2237 goto err_pdata_fail;
2238 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302239 if (!(of_property_read_u32(pdev->dev.of_node,
2240 "swrm-io-base", &swrm->swrm_base_reg)))
2241 ret = of_property_read_u32(pdev->dev.of_node,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302242 "swrm-io-base", &swrm->swrm_base_reg);
2243 if (!swrm->swrm_base_reg) {
2244 swrm->read = pdata->read;
2245 if (!swrm->read) {
2246 dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
2247 __func__);
2248 ret = -EINVAL;
2249 goto err_pdata_fail;
2250 }
2251 swrm->write = pdata->write;
2252 if (!swrm->write) {
2253 dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
2254 __func__);
2255 ret = -EINVAL;
2256 goto err_pdata_fail;
2257 }
2258 swrm->bulk_write = pdata->bulk_write;
2259 if (!swrm->bulk_write) {
2260 dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
2261 __func__);
2262 ret = -EINVAL;
2263 goto err_pdata_fail;
2264 }
2265 } else {
2266 swrm->swrm_dig_base = devm_ioremap(&pdev->dev,
2267 swrm->swrm_base_reg, SWRM_MAX_REGISTER);
2268 }
2269
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07002270 swrm->core_vote = pdata->core_vote;
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302271 if (!(of_property_read_u32(pdev->dev.of_node,
2272 "qcom,swrm-hctl-reg", &swrm_hctl_reg)))
2273 swrm->swrm_hctl_reg = devm_ioremap(&pdev->dev,
2274 swrm_hctl_reg, 0x4);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302275 swrm->clk = pdata->clk;
2276 if (!swrm->clk) {
2277 dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
2278 __func__);
2279 ret = -EINVAL;
2280 goto err_pdata_fail;
2281 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302282 if (of_property_read_u32(pdev->dev.of_node,
2283 "qcom,swr-clock-stop-mode0",
2284 &swrm->clk_stop_mode0_supp)) {
2285 swrm->clk_stop_mode0_supp = FALSE;
2286 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302287
2288 ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
2289 &swrm->num_dev);
2290 if (ret) {
2291 dev_dbg(&pdev->dev, "%s: Looking up %s property failed\n",
2292 __func__, "qcom,swr-num-dev");
2293 } else {
2294 if (swrm->num_dev > SWR_MAX_SLAVE_DEVICES) {
2295 dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
2296 __func__, swrm->num_dev, SWR_MAX_SLAVE_DEVICES);
2297 ret = -EINVAL;
2298 goto err_pdata_fail;
2299 }
2300 }
2301
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302302 /* Parse soundwire port mapping */
2303 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
2304 &num_ports);
2305 if (ret) {
2306 dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
2307 goto err_pdata_fail;
2308 }
2309 swrm->num_ports = num_ports;
2310
2311 if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
2312 &map_size)) {
2313 dev_err(swrm->dev, "missing port mapping\n");
2314 goto err_pdata_fail;
2315 }
2316
2317 map_length = map_size / (3 * sizeof(u32));
2318 if (num_ports > SWR_MSTR_PORT_LEN) {
2319 dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
2320 __func__);
2321 ret = -EINVAL;
2322 goto err_pdata_fail;
2323 }
2324 temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
2325
2326 if (!temp) {
2327 ret = -ENOMEM;
2328 goto err_pdata_fail;
2329 }
2330 ret = of_property_read_u32_array(pdev->dev.of_node,
2331 "qcom,swr-port-mapping", temp, 3 * map_length);
2332 if (ret) {
2333 dev_err(swrm->dev, "%s: Failed to read port mapping\n",
2334 __func__);
2335 goto err_pdata_fail;
2336 }
2337
2338 for (i = 0; i < map_length; i++) {
2339 port_num = temp[3 * i];
2340 port_type = temp[3 * i + 1];
2341 ch_mask = temp[3 * i + 2];
2342
2343 if (port_num != old_port_num)
2344 ch_iter = 0;
2345 swrm->port_mapping[port_num][ch_iter].port_type = port_type;
2346 swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
2347 old_port_num = port_num;
2348 }
2349 devm_kfree(&pdev->dev, temp);
2350
2351 swrm->reg_irq = pdata->reg_irq;
2352 swrm->master.read = swrm_read;
2353 swrm->master.write = swrm_write;
2354 swrm->master.bulk_write = swrm_bulk_write;
2355 swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
2356 swrm->master.connect_port = swrm_connect_port;
2357 swrm->master.disconnect_port = swrm_disconnect_port;
2358 swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
2359 swrm->master.remove_from_group = swrm_remove_from_group;
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302360 swrm->master.device_wakeup_vote = swrm_device_wakeup_vote;
2361 swrm->master.device_wakeup_unvote = swrm_device_wakeup_unvote;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302362 swrm->master.dev.parent = &pdev->dev;
2363 swrm->master.dev.of_node = pdev->dev.of_node;
2364 swrm->master.num_port = 0;
2365 swrm->rcmd_id = 0;
2366 swrm->wcmd_id = 0;
2367 swrm->slave_status = 0;
2368 swrm->num_rx_chs = 0;
2369 swrm->clk_ref_count = 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302370 swrm->swr_irq_wakeup_capable = 0;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302371 swrm->mclk_freq = MCLK_FREQ;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302372 swrm->bus_clk = MCLK_FREQ;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302373 swrm->dev_up = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302374 swrm->state = SWR_MSTR_UP;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302375 swrm->ipc_wakeup = false;
2376 swrm->ipc_wakeup_triggered = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302377 init_completion(&swrm->reset);
2378 init_completion(&swrm->broadcast);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302379 init_completion(&swrm->clk_off_complete);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302380 mutex_init(&swrm->mlock);
2381 mutex_init(&swrm->reslock);
2382 mutex_init(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302383 mutex_init(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302384 mutex_init(&swrm->clklock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302385 mutex_init(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302386 mutex_init(&swrm->pm_lock);
2387 swrm->wlock_holders = 0;
2388 swrm->pm_state = SWRM_PM_SLEEPABLE;
2389 init_waitqueue_head(&swrm->pm_wq);
2390 pm_qos_add_request(&swrm->pm_qos_req,
2391 PM_QOS_CPU_DMA_LATENCY,
2392 PM_QOS_DEFAULT_VALUE);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302393
2394 for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++)
2395 INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
2396
Sudheer Papothi06f43412019-07-09 03:32:54 +05302397 /* Register LPASS core hw vote */
2398 lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
2399 if (IS_ERR(lpass_core_hw_vote)) {
2400 ret = PTR_ERR(lpass_core_hw_vote);
2401 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2402 __func__, "lpass_core_hw_vote", ret);
2403 lpass_core_hw_vote = NULL;
2404 ret = 0;
2405 }
2406 swrm->lpass_core_hw_vote = lpass_core_hw_vote;
2407
2408 /* Register LPASS audio core vote */
2409 lpass_core_audio = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
2410 if (IS_ERR(lpass_core_audio)) {
2411 ret = PTR_ERR(lpass_core_audio);
2412 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2413 __func__, "lpass_core_audio", ret);
2414 lpass_core_audio = NULL;
2415 ret = 0;
2416 }
2417 swrm->lpass_core_audio = lpass_core_audio;
2418
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302419 if (swrm->reg_irq) {
2420 ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
2421 SWR_IRQ_REGISTER);
2422 if (ret) {
2423 dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
2424 __func__, ret);
2425 goto err_irq_fail;
2426 }
2427 } else {
2428 swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
2429 if (swrm->irq < 0) {
2430 dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
2431 __func__, swrm->irq);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302432 goto err_irq_fail;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302433 }
2434
2435 ret = request_threaded_irq(swrm->irq, NULL,
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302436 swr_mstr_interrupt_v2,
Ramprasad Katkam83303512018-10-11 17:34:22 +05302437 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302438 "swr_master_irq", swrm);
2439 if (ret) {
2440 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2441 __func__, ret);
2442 goto err_irq_fail;
2443 }
2444
2445 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302446 /* Make inband tx interrupts as wakeup capable for slave irq */
2447 ret = of_property_read_u32(pdev->dev.of_node,
2448 "qcom,swr-mstr-irq-wakeup-capable",
2449 &swrm->swr_irq_wakeup_capable);
2450 if (ret)
2451 dev_dbg(swrm->dev, "%s: swrm irq wakeup capable not defined\n",
2452 __func__);
2453 if (swrm->swr_irq_wakeup_capable)
2454 irq_set_irq_wake(swrm->irq, 1);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302455 ret = swr_register_master(&swrm->master);
2456 if (ret) {
2457 dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
2458 goto err_mstr_fail;
2459 }
2460
2461 /* Add devices registered with board-info as the
2462 * controller will be up now
2463 */
2464 swr_master_add_boarddevices(&swrm->master);
2465 mutex_lock(&swrm->mlock);
2466 swrm_clk_request(swrm, true);
2467 ret = swrm_master_init(swrm);
2468 if (ret < 0) {
2469 dev_err(&pdev->dev,
2470 "%s: Error in master Initialization , err %d\n",
2471 __func__, ret);
2472 mutex_unlock(&swrm->mlock);
2473 goto err_mstr_fail;
2474 }
2475 swrm->version = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
2476
2477 mutex_unlock(&swrm->mlock);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302478 INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302479
2480 if (pdev->dev.of_node)
2481 of_register_swr_devices(&swrm->master);
2482
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302483#ifdef CONFIG_DEBUG_FS
2484 swrm->debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
2485 if (!IS_ERR(swrm->debugfs_swrm_dent)) {
2486 swrm->debugfs_peek = debugfs_create_file("swrm_peek",
2487 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2488 (void *) swrm, &swrm_debug_read_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302489
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302490 swrm->debugfs_poke = debugfs_create_file("swrm_poke",
2491 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2492 (void *) swrm, &swrm_debug_write_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302493
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302494 swrm->debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
2495 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2496 (void *) swrm,
2497 &swrm_debug_dump_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302498 }
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302499#endif
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302500 ret = device_init_wakeup(swrm->dev, true);
2501 if (ret) {
2502 dev_err(swrm->dev, "Device wakeup init failed: %d\n", ret);
2503 goto err_irq_wakeup_fail;
2504 }
2505
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302506 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
2507 pm_runtime_use_autosuspend(&pdev->dev);
2508 pm_runtime_set_active(&pdev->dev);
2509 pm_runtime_enable(&pdev->dev);
2510 pm_runtime_mark_last_busy(&pdev->dev);
2511
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302512 INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn);
2513 swrm->event_notifier.notifier_call = swrm_event_notify;
2514 msm_aud_evt_register_client(&swrm->event_notifier);
2515
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302516 return 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302517err_irq_wakeup_fail:
2518 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302519err_mstr_fail:
2520 if (swrm->reg_irq)
2521 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2522 swrm, SWR_IRQ_FREE);
2523 else if (swrm->irq)
2524 free_irq(swrm->irq, swrm);
2525err_irq_fail:
2526 mutex_destroy(&swrm->mlock);
2527 mutex_destroy(&swrm->reslock);
2528 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302529 mutex_destroy(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302530 mutex_destroy(&swrm->clklock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302531 mutex_destroy(&swrm->pm_lock);
2532 pm_qos_remove_request(&swrm->pm_qos_req);
2533
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302534err_pdata_fail:
2535err_memory_fail:
2536 return ret;
2537}
2538
2539static int swrm_remove(struct platform_device *pdev)
2540{
2541 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2542
2543 if (swrm->reg_irq)
2544 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2545 swrm, SWR_IRQ_FREE);
2546 else if (swrm->irq)
2547 free_irq(swrm->irq, swrm);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302548 else if (swrm->wake_irq > 0)
2549 free_irq(swrm->wake_irq, swrm);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302550 if (swrm->swr_irq_wakeup_capable)
2551 irq_set_irq_wake(swrm->irq, 0);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302552 cancel_work_sync(&swrm->wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302553 pm_runtime_disable(&pdev->dev);
2554 pm_runtime_set_suspended(&pdev->dev);
2555 swr_unregister_master(&swrm->master);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302556 msm_aud_evt_unregister_client(&swrm->event_notifier);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302557 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302558 mutex_destroy(&swrm->mlock);
2559 mutex_destroy(&swrm->reslock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302560 mutex_destroy(&swrm->iolock);
2561 mutex_destroy(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302562 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302563 mutex_destroy(&swrm->pm_lock);
2564 pm_qos_remove_request(&swrm->pm_qos_req);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302565 devm_kfree(&pdev->dev, swrm);
2566 return 0;
2567}
2568
2569static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
2570{
2571 u32 val;
2572
2573 dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
2574 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD);
2575 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2576 val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK;
2577 swr_master_write(swrm, SWRM_MCP_CFG_ADDR, val);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302578
2579 return 0;
2580}
2581
2582#ifdef CONFIG_PM
2583static int swrm_runtime_resume(struct device *dev)
2584{
2585 struct platform_device *pdev = to_platform_device(dev);
2586 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2587 int ret = 0;
Vatsal Buchae50b5002019-09-19 14:32:20 +05302588 bool swrm_clk_req_err = false;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302589 bool hw_core_err = false;
2590 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302591 struct swr_master *mstr = &swrm->master;
2592 struct swr_device *swr_dev;
2593
2594 dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
2595 __func__, swrm->state);
2596 mutex_lock(&swrm->reslock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302597
Sudheer Papothi384addd2019-06-14 02:26:52 +05302598 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2599 dev_err(dev, "%s:lpass core hw enable failed\n",
2600 __func__);
2601 hw_core_err = true;
2602 }
2603 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2604 dev_err(dev, "%s:lpass audio hw enable failed\n",
2605 __func__);
2606 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002607 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302608
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302609 if ((swrm->state == SWR_MSTR_DOWN) ||
2610 (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) {
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302611 if (swrm->clk_stop_mode0_supp) {
2612 if (swrm->ipc_wakeup)
2613 msm_aud_evt_blocking_notifier_call_chain(
2614 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302615 }
2616
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302617 if (swrm_clk_request(swrm, true)) {
2618 /*
2619 * Set autosuspend timer to 1 for
2620 * master to enter into suspend.
2621 */
Vatsal Buchae50b5002019-09-19 14:32:20 +05302622 swrm_clk_req_err = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302623 goto exit;
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302624 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302625 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302626 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2627 ret = swr_device_up(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302628 if (ret == -ENODEV) {
2629 dev_dbg(dev,
2630 "%s slave device up not implemented\n",
2631 __func__);
2632 ret = 0;
2633 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302634 dev_err(dev,
2635 "%s: failed to wakeup swr dev %d\n",
2636 __func__, swr_dev->dev_num);
2637 swrm_clk_request(swrm, false);
2638 goto exit;
2639 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302640 }
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05302641 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2642 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2643 swrm_master_init(swrm);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302644 /* wait for hw enumeration to complete */
2645 usleep_range(100, 105);
Laxminath Kasame2291972019-11-08 14:51:59 +05302646 if (!swrm_check_link_status(swrm, 0x1))
2647 goto exit;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302648 swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, 0x0,
2649 SWRS_SCP_INT_STATUS_MASK_1);
Karthikeyan Manif6821902019-05-21 17:31:24 -07002650 if (swrm->state == SWR_MSTR_SSR) {
2651 mutex_unlock(&swrm->reslock);
2652 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
2653 mutex_lock(&swrm->reslock);
2654 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302655 } else {
2656 /*wake up from clock stop*/
2657 swr_master_write(swrm, SWRM_MCP_BUS_CTRL_ADDR, 0x2);
2658 usleep_range(100, 105);
Laxminath Kasame2291972019-11-08 14:51:59 +05302659 if (!swrm_check_link_status(swrm, 0x1))
2660 goto exit;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302661 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302662 swrm->state = SWR_MSTR_UP;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302663 }
2664exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302665 if (!aud_core_err)
2666 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2667 if (!hw_core_err)
2668 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Vatsal Buchae50b5002019-09-19 14:32:20 +05302669 if (swrm_clk_req_err)
2670 pm_runtime_set_autosuspend_delay(&pdev->dev,
2671 ERR_AUTO_SUSPEND_TIMER_VAL);
2672 else
2673 pm_runtime_set_autosuspend_delay(&pdev->dev,
2674 auto_suspend_timer);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302675 mutex_unlock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302676
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302677 return ret;
2678}
2679
2680static int swrm_runtime_suspend(struct device *dev)
2681{
2682 struct platform_device *pdev = to_platform_device(dev);
2683 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2684 int ret = 0;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302685 bool hw_core_err = false;
2686 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302687 struct swr_master *mstr = &swrm->master;
2688 struct swr_device *swr_dev;
2689 int current_state = 0;
2690
2691 dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
2692 __func__, swrm->state);
2693 mutex_lock(&swrm->reslock);
2694 mutex_lock(&swrm->force_down_lock);
2695 current_state = swrm->state;
2696 mutex_unlock(&swrm->force_down_lock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302697
2698 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2699 dev_err(dev, "%s:lpass core hw enable failed\n",
2700 __func__);
2701 hw_core_err = true;
2702 }
2703 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2704 dev_err(dev, "%s:lpass audio hw enable failed\n",
2705 __func__);
2706 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002707 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302708
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302709 if ((current_state == SWR_MSTR_UP) ||
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302710 (current_state == SWR_MSTR_SSR)) {
2711
2712 if ((current_state != SWR_MSTR_SSR) &&
2713 swrm_is_port_en(&swrm->master)) {
2714 dev_dbg(dev, "%s ports are enabled\n", __func__);
2715 ret = -EBUSY;
2716 goto exit;
2717 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302718 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05302719 mutex_unlock(&swrm->reslock);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +05302720 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
Sudheer Papothi06f43412019-07-09 03:32:54 +05302721 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302722 swrm_clk_pause(swrm);
2723 swr_master_write(swrm, SWRM_COMP_CFG_ADDR, 0x00);
2724 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2725 ret = swr_device_down(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302726 if (ret == -ENODEV) {
2727 dev_dbg_ratelimited(dev,
2728 "%s slave device down not implemented\n",
2729 __func__);
2730 ret = 0;
2731 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302732 dev_err(dev,
2733 "%s: failed to shutdown swr dev %d\n",
2734 __func__, swr_dev->dev_num);
2735 goto exit;
2736 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302737 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302738 } else {
Sudheer Papothi384addd2019-06-14 02:26:52 +05302739 mutex_unlock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302740 /* clock stop sequence */
2741 swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
2742 SWRS_SCP_CONTROL);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302743 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302744 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302745 }
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07002746 ret = swrm_clk_request(swrm, false);
2747 if (ret) {
2748 dev_err(dev, "%s: swrmn clk failed\n", __func__);
2749 ret = 0;
2750 goto exit;
2751 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302752
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302753 if (swrm->clk_stop_mode0_supp) {
2754 if (swrm->wake_irq > 0) {
2755 enable_irq(swrm->wake_irq);
2756 } else if (swrm->ipc_wakeup) {
2757 msm_aud_evt_blocking_notifier_call_chain(
2758 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
2759 swrm->ipc_wakeup_triggered = false;
2760 }
Laxminath Kasame2291972019-11-08 14:51:59 +05302761 if (!swrm_check_link_status(swrm, 0x0))
2762 goto exit;
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302763 }
2764
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302765 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302766 /* Retain SSR state until resume */
2767 if (current_state != SWR_MSTR_SSR)
2768 swrm->state = SWR_MSTR_DOWN;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302769exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302770 if (!aud_core_err)
2771 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2772 if (!hw_core_err)
2773 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302774 mutex_unlock(&swrm->reslock);
2775 return ret;
2776}
2777#endif /* CONFIG_PM */
2778
Sudheer Papothi06f43412019-07-09 03:32:54 +05302779static int swrm_device_suspend(struct device *dev)
2780{
2781 struct platform_device *pdev = to_platform_device(dev);
2782 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2783 int ret = 0;
2784
2785 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2786 if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
2787 ret = swrm_runtime_suspend(dev);
2788 if (!ret) {
2789 pm_runtime_disable(dev);
2790 pm_runtime_set_suspended(dev);
2791 pm_runtime_enable(dev);
2792 }
2793 }
2794
2795 return 0;
2796}
2797
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302798static int swrm_device_down(struct device *dev)
2799{
2800 struct platform_device *pdev = to_platform_device(dev);
2801 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302802
2803 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2804
2805 mutex_lock(&swrm->force_down_lock);
2806 swrm->state = SWR_MSTR_SSR;
2807 mutex_unlock(&swrm->force_down_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302808
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302809 swrm_device_suspend(dev);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302810 return 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302811}
2812
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302813int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm)
2814{
2815 int ret = 0;
Laxminath Kasama60239e2019-01-10 14:43:03 +05302816 int irq, dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302817
2818 if (!swrm->ipc_wakeup) {
Laxminath Kasama60239e2019-01-10 14:43:03 +05302819 irq = of_get_named_gpio(swrm->dev->of_node,
2820 "qcom,swr-wakeup-irq", 0);
2821 if (gpio_is_valid(irq)) {
2822 swrm->wake_irq = gpio_to_irq(irq);
2823 if (swrm->wake_irq < 0) {
2824 dev_err(swrm->dev,
2825 "Unable to configure irq\n");
2826 return swrm->wake_irq;
2827 }
2828 } else {
2829 dir_apps_irq = platform_get_irq_byname(swrm->pdev,
2830 "swr_wake_irq");
2831 if (dir_apps_irq < 0) {
2832 dev_err(swrm->dev,
2833 "TLMM connect gpio not found\n");
2834 return -EINVAL;
2835 }
2836 swrm->wake_irq = dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302837 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302838 ret = request_threaded_irq(swrm->wake_irq, NULL,
2839 swrm_wakeup_interrupt,
2840 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
2841 "swr_wake_irq", swrm);
2842 if (ret) {
2843 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2844 __func__, ret);
2845 return -EINVAL;
2846 }
Aditya Bavanari3517b112018-12-03 13:26:59 +05302847 irq_set_irq_wake(swrm->wake_irq, 1);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302848 }
2849 return ret;
2850}
2851
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302852static int swrm_alloc_port_mem(struct device *dev, struct swr_mstr_ctrl *swrm,
2853 u32 uc, u32 size)
2854{
2855 if (!swrm->port_param) {
2856 swrm->port_param = devm_kzalloc(dev,
2857 sizeof(swrm->port_param) * SWR_UC_MAX,
2858 GFP_KERNEL);
2859 if (!swrm->port_param)
2860 return -ENOMEM;
2861 }
2862 if (!swrm->port_param[uc]) {
2863 swrm->port_param[uc] = devm_kcalloc(dev, size,
2864 sizeof(struct port_params),
2865 GFP_KERNEL);
2866 if (!swrm->port_param[uc])
2867 return -ENOMEM;
2868 } else {
2869 dev_err_ratelimited(swrm->dev, "%s: called more than once\n",
2870 __func__);
2871 }
2872
2873 return 0;
2874}
2875
2876static int swrm_copy_port_config(struct swr_mstr_ctrl *swrm,
2877 struct swrm_port_config *port_cfg,
2878 u32 size)
2879{
2880 int idx;
2881 struct port_params *params;
2882 int uc = port_cfg->uc;
2883 int ret = 0;
2884
2885 for (idx = 0; idx < size; idx++) {
2886 params = &((struct port_params *)port_cfg->params)[idx];
2887 if (!params) {
2888 dev_err(swrm->dev, "%s: Invalid params\n", __func__);
2889 ret = -EINVAL;
2890 break;
2891 }
2892 memcpy(&swrm->port_param[uc][idx], params,
2893 sizeof(struct port_params));
2894 }
2895
2896 return ret;
2897}
2898
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302899/**
2900 * swrm_wcd_notify - parent device can notify to soundwire master through
2901 * this function
2902 * @pdev: pointer to platform device structure
2903 * @id: command id from parent to the soundwire master
2904 * @data: data from parent device to soundwire master
2905 */
2906int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
2907{
2908 struct swr_mstr_ctrl *swrm;
2909 int ret = 0;
2910 struct swr_master *mstr;
2911 struct swr_device *swr_dev;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302912 struct swrm_port_config *port_cfg;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302913
2914 if (!pdev) {
2915 pr_err("%s: pdev is NULL\n", __func__);
2916 return -EINVAL;
2917 }
2918 swrm = platform_get_drvdata(pdev);
2919 if (!swrm) {
2920 dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__);
2921 return -EINVAL;
2922 }
2923 mstr = &swrm->master;
2924
2925 switch (id) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05302926 case SWR_REQ_CLK_SWITCH:
2927 /* This will put soundwire in clock stop mode and disable the
2928 * clocks, if there is no active usecase running, so that the
2929 * next activity on soundwire will request clock from new clock
2930 * source.
2931 */
2932 mutex_lock(&swrm->mlock);
2933 if (swrm->state == SWR_MSTR_UP)
2934 swrm_device_suspend(&pdev->dev);
2935 mutex_unlock(&swrm->mlock);
2936 break;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302937 case SWR_CLK_FREQ:
2938 if (!data) {
2939 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
2940 ret = -EINVAL;
2941 } else {
2942 mutex_lock(&swrm->mlock);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302943 if (swrm->mclk_freq != *(int *)data) {
2944 dev_dbg(swrm->dev, "%s: freq change: force mstr down\n", __func__);
2945 if (swrm->state == SWR_MSTR_DOWN)
2946 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
2947 __func__, swrm->state);
2948 else
2949 swrm_device_suspend(&pdev->dev);
Prasad Kumpatla386df4e2019-10-11 18:23:16 +05302950 /*
2951 * add delay to ensure clk release happen
2952 * if interrupt triggered for clk stop,
2953 * wait for it to exit
2954 */
2955 usleep_range(10000, 10500);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302956 }
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302957 swrm->mclk_freq = *(int *)data;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302958 swrm->bus_clk = swrm->mclk_freq;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302959 mutex_unlock(&swrm->mlock);
2960 }
2961 break;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302962 case SWR_DEVICE_SSR_DOWN:
2963 mutex_lock(&swrm->devlock);
2964 swrm->dev_up = false;
2965 mutex_unlock(&swrm->devlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302966 mutex_lock(&swrm->reslock);
2967 swrm->state = SWR_MSTR_SSR;
2968 mutex_unlock(&swrm->reslock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302969 break;
2970 case SWR_DEVICE_SSR_UP:
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302971 /* wait for clk voting to be zero */
Ramprasad Katkam7f6462e2018-11-06 11:51:22 +05302972 reinit_completion(&swrm->clk_off_complete);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302973 if (swrm->clk_ref_count &&
2974 !wait_for_completion_timeout(&swrm->clk_off_complete,
Ramprasad Katkamc87efeb2018-12-12 19:26:19 +05302975 msecs_to_jiffies(500)))
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302976 dev_err(swrm->dev, "%s: clock voting not zero\n",
2977 __func__);
2978
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302979 mutex_lock(&swrm->devlock);
2980 swrm->dev_up = true;
2981 mutex_unlock(&swrm->devlock);
2982 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302983 case SWR_DEVICE_DOWN:
2984 dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
2985 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302986 if (swrm->state == SWR_MSTR_DOWN)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302987 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
2988 __func__, swrm->state);
2989 else
2990 swrm_device_down(&pdev->dev);
2991 mutex_unlock(&swrm->mlock);
2992 break;
2993 case SWR_DEVICE_UP:
2994 dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
Ramprasad Katkam0fed92f2018-11-08 14:22:22 +05302995 mutex_lock(&swrm->devlock);
2996 if (!swrm->dev_up) {
2997 dev_dbg(swrm->dev, "SSR not complete yet\n");
2998 mutex_unlock(&swrm->devlock);
2999 return -EBUSY;
3000 }
3001 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303002 mutex_lock(&swrm->mlock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303003 pm_runtime_mark_last_busy(&pdev->dev);
3004 pm_runtime_get_sync(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303005 mutex_lock(&swrm->reslock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303006 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
3007 ret = swr_reset_device(swr_dev);
3008 if (ret) {
3009 dev_err(swrm->dev,
3010 "%s: failed to reset swr device %d\n",
3011 __func__, swr_dev->dev_num);
3012 swrm_clk_request(swrm, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303013 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303014 }
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303015 pm_runtime_mark_last_busy(&pdev->dev);
3016 pm_runtime_put_autosuspend(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303017 mutex_unlock(&swrm->reslock);
3018 mutex_unlock(&swrm->mlock);
3019 break;
3020 case SWR_SET_NUM_RX_CH:
3021 if (!data) {
3022 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
3023 ret = -EINVAL;
3024 } else {
3025 mutex_lock(&swrm->mlock);
3026 swrm->num_rx_chs = *(int *)data;
3027 if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
3028 list_for_each_entry(swr_dev, &mstr->devices,
3029 dev_list) {
3030 ret = swr_set_device_group(swr_dev,
3031 SWR_BROADCAST);
3032 if (ret)
3033 dev_err(swrm->dev,
3034 "%s: set num ch failed\n",
3035 __func__);
3036 }
3037 } else {
3038 list_for_each_entry(swr_dev, &mstr->devices,
3039 dev_list) {
3040 ret = swr_set_device_group(swr_dev,
3041 SWR_GROUP_NONE);
3042 if (ret)
3043 dev_err(swrm->dev,
3044 "%s: set num ch failed\n",
3045 __func__);
3046 }
3047 }
3048 mutex_unlock(&swrm->mlock);
3049 }
3050 break;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303051 case SWR_REGISTER_WAKE_IRQ:
3052 if (!data) {
3053 dev_err(swrm->dev, "%s: reg wake irq data is NULL\n",
3054 __func__);
3055 ret = -EINVAL;
3056 } else {
3057 mutex_lock(&swrm->mlock);
3058 swrm->ipc_wakeup = *(u32 *)data;
3059 ret = swrm_register_wake_irq(swrm);
3060 if (ret)
3061 dev_err(swrm->dev, "%s: register wake_irq failed\n",
3062 __func__);
3063 mutex_unlock(&swrm->mlock);
3064 }
3065 break;
Sudheer Papothi72ee2642019-08-08 05:15:17 +05303066 case SWR_REGISTER_WAKEUP:
3067 msm_aud_evt_blocking_notifier_call_chain(
3068 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
3069 break;
3070 case SWR_DEREGISTER_WAKEUP:
3071 msm_aud_evt_blocking_notifier_call_chain(
3072 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
3073 break;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05303074 case SWR_SET_PORT_MAP:
3075 if (!data) {
3076 dev_err(swrm->dev, "%s: data is NULL for id=%d\n",
3077 __func__, id);
3078 ret = -EINVAL;
3079 } else {
3080 mutex_lock(&swrm->mlock);
3081 port_cfg = (struct swrm_port_config *)data;
3082 if (!port_cfg->size) {
3083 ret = -EINVAL;
3084 goto done;
3085 }
3086 ret = swrm_alloc_port_mem(&pdev->dev, swrm,
3087 port_cfg->uc, port_cfg->size);
3088 if (!ret)
3089 swrm_copy_port_config(swrm, port_cfg,
3090 port_cfg->size);
3091done:
3092 mutex_unlock(&swrm->mlock);
3093 }
3094 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303095 default:
3096 dev_err(swrm->dev, "%s: swr master unknown id %d\n",
3097 __func__, id);
3098 break;
3099 }
3100 return ret;
3101}
3102EXPORT_SYMBOL(swrm_wcd_notify);
3103
Ramprasad Katkam57349872018-11-11 18:34:57 +05303104/*
3105 * swrm_pm_cmpxchg:
3106 * Check old state and exchange with pm new state
3107 * if old state matches with current state
3108 *
3109 * @swrm: pointer to wcd core resource
3110 * @o: pm old state
3111 * @n: pm new state
3112 *
3113 * Returns old state
3114 */
3115static enum swrm_pm_state swrm_pm_cmpxchg(
3116 struct swr_mstr_ctrl *swrm,
3117 enum swrm_pm_state o,
3118 enum swrm_pm_state n)
3119{
3120 enum swrm_pm_state old;
3121
3122 if (!swrm)
3123 return o;
3124
3125 mutex_lock(&swrm->pm_lock);
3126 old = swrm->pm_state;
3127 if (old == o)
3128 swrm->pm_state = n;
3129 mutex_unlock(&swrm->pm_lock);
3130
3131 return old;
3132}
3133
3134static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm)
3135{
3136 enum swrm_pm_state os;
3137
3138 /*
3139 * swrm_{lock/unlock}_sleep will be called by swr irq handler
3140 * and slave wake up requests..
3141 *
3142 * If system didn't resume, we can simply return false so
3143 * IRQ handler can return without handling IRQ.
3144 */
3145 mutex_lock(&swrm->pm_lock);
3146 if (swrm->wlock_holders++ == 0) {
3147 dev_dbg(swrm->dev, "%s: holding wake lock\n", __func__);
3148 pm_qos_update_request(&swrm->pm_qos_req,
3149 msm_cpuidle_get_deep_idle_latency());
3150 pm_stay_awake(swrm->dev);
3151 }
3152 mutex_unlock(&swrm->pm_lock);
3153
3154 if (!wait_event_timeout(swrm->pm_wq,
3155 ((os = swrm_pm_cmpxchg(swrm,
3156 SWRM_PM_SLEEPABLE,
3157 SWRM_PM_AWAKE)) ==
3158 SWRM_PM_SLEEPABLE ||
3159 (os == SWRM_PM_AWAKE)),
3160 msecs_to_jiffies(
3161 SWRM_SYSTEM_RESUME_TIMEOUT_MS))) {
3162 dev_err(swrm->dev, "%s: system didn't resume within %dms, s %d, w %d\n",
3163 __func__, SWRM_SYSTEM_RESUME_TIMEOUT_MS, swrm->pm_state,
3164 swrm->wlock_holders);
3165 swrm_unlock_sleep(swrm);
3166 return false;
3167 }
3168 wake_up_all(&swrm->pm_wq);
3169 return true;
3170}
3171
3172static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm)
3173{
3174 mutex_lock(&swrm->pm_lock);
3175 if (--swrm->wlock_holders == 0) {
3176 dev_dbg(swrm->dev, "%s: releasing wake lock pm_state %d -> %d\n",
3177 __func__, swrm->pm_state, SWRM_PM_SLEEPABLE);
3178 /*
3179 * if swrm_lock_sleep failed, pm_state would be still
3180 * swrm_PM_ASLEEP, don't overwrite
3181 */
3182 if (likely(swrm->pm_state == SWRM_PM_AWAKE))
3183 swrm->pm_state = SWRM_PM_SLEEPABLE;
3184 pm_qos_update_request(&swrm->pm_qos_req,
3185 PM_QOS_DEFAULT_VALUE);
3186 pm_relax(swrm->dev);
3187 }
3188 mutex_unlock(&swrm->pm_lock);
3189 wake_up_all(&swrm->pm_wq);
3190}
3191
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303192#ifdef CONFIG_PM_SLEEP
3193static int swrm_suspend(struct device *dev)
3194{
3195 int ret = -EBUSY;
3196 struct platform_device *pdev = to_platform_device(dev);
3197 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3198
3199 dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
Ramprasad Katkam57349872018-11-11 18:34:57 +05303200
3201 mutex_lock(&swrm->pm_lock);
3202
3203 if (swrm->pm_state == SWRM_PM_SLEEPABLE) {
3204 dev_dbg(swrm->dev, "%s: suspending system, state %d, wlock %d\n",
3205 __func__, swrm->pm_state,
3206 swrm->wlock_holders);
3207 swrm->pm_state = SWRM_PM_ASLEEP;
3208 } else if (swrm->pm_state == SWRM_PM_AWAKE) {
3209 /*
3210 * unlock to wait for pm_state == SWRM_PM_SLEEPABLE
3211 * then set to SWRM_PM_ASLEEP
3212 */
3213 dev_dbg(swrm->dev, "%s: waiting to suspend system, state %d, wlock %d\n",
3214 __func__, swrm->pm_state,
3215 swrm->wlock_holders);
3216 mutex_unlock(&swrm->pm_lock);
3217 if (!(wait_event_timeout(swrm->pm_wq, swrm_pm_cmpxchg(
3218 swrm, SWRM_PM_SLEEPABLE,
3219 SWRM_PM_ASLEEP) ==
3220 SWRM_PM_SLEEPABLE,
3221 msecs_to_jiffies(
3222 SWRM_SYS_SUSPEND_WAIT)))) {
3223 dev_dbg(swrm->dev, "%s: suspend failed state %d, wlock %d\n",
3224 __func__, swrm->pm_state,
3225 swrm->wlock_holders);
3226 return -EBUSY;
3227 } else {
3228 dev_dbg(swrm->dev,
3229 "%s: done, state %d, wlock %d\n",
3230 __func__, swrm->pm_state,
3231 swrm->wlock_holders);
3232 }
3233 mutex_lock(&swrm->pm_lock);
3234 } else if (swrm->pm_state == SWRM_PM_ASLEEP) {
3235 dev_dbg(swrm->dev, "%s: system is already suspended, state %d, wlock %d\n",
3236 __func__, swrm->pm_state,
3237 swrm->wlock_holders);
3238 }
3239
3240 mutex_unlock(&swrm->pm_lock);
3241
3242 if ((!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev))) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303243 ret = swrm_runtime_suspend(dev);
3244 if (!ret) {
3245 /*
3246 * Synchronize runtime-pm and system-pm states:
3247 * At this point, we are already suspended. If
3248 * runtime-pm still thinks its active, then
3249 * make sure its status is in sync with HW
3250 * status. The three below calls let the
3251 * runtime-pm know that we are suspended
3252 * already without re-invoking the suspend
3253 * callback
3254 */
3255 pm_runtime_disable(dev);
3256 pm_runtime_set_suspended(dev);
3257 pm_runtime_enable(dev);
3258 }
3259 }
3260 if (ret == -EBUSY) {
3261 /*
3262 * There is a possibility that some audio stream is active
3263 * during suspend. We dont want to return suspend failure in
3264 * that case so that display and relevant components can still
3265 * go to suspend.
3266 * If there is some other error, then it should be passed-on
3267 * to system level suspend
3268 */
3269 ret = 0;
3270 }
3271 return ret;
3272}
3273
3274static int swrm_resume(struct device *dev)
3275{
3276 int ret = 0;
3277 struct platform_device *pdev = to_platform_device(dev);
3278 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3279
3280 dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
3281 if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
3282 ret = swrm_runtime_resume(dev);
3283 if (!ret) {
3284 pm_runtime_mark_last_busy(dev);
3285 pm_request_autosuspend(dev);
3286 }
3287 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05303288 mutex_lock(&swrm->pm_lock);
3289 if (swrm->pm_state == SWRM_PM_ASLEEP) {
3290 dev_dbg(swrm->dev,
3291 "%s: resuming system, state %d, wlock %d\n",
3292 __func__, swrm->pm_state,
3293 swrm->wlock_holders);
3294 swrm->pm_state = SWRM_PM_SLEEPABLE;
3295 } else {
3296 dev_dbg(swrm->dev, "%s: system is already awake, state %d wlock %d\n",
3297 __func__, swrm->pm_state,
3298 swrm->wlock_holders);
3299 }
3300 mutex_unlock(&swrm->pm_lock);
3301 wake_up_all(&swrm->pm_wq);
3302
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303303 return ret;
3304}
3305#endif /* CONFIG_PM_SLEEP */
3306
3307static const struct dev_pm_ops swrm_dev_pm_ops = {
3308 SET_SYSTEM_SLEEP_PM_OPS(
3309 swrm_suspend,
3310 swrm_resume
3311 )
3312 SET_RUNTIME_PM_OPS(
3313 swrm_runtime_suspend,
3314 swrm_runtime_resume,
3315 NULL
3316 )
3317};
3318
3319static const struct of_device_id swrm_dt_match[] = {
3320 {
3321 .compatible = "qcom,swr-mstr",
3322 },
3323 {}
3324};
3325
3326static struct platform_driver swr_mstr_driver = {
3327 .probe = swrm_probe,
3328 .remove = swrm_remove,
3329 .driver = {
3330 .name = SWR_WCD_NAME,
3331 .owner = THIS_MODULE,
3332 .pm = &swrm_dev_pm_ops,
3333 .of_match_table = swrm_dt_match,
Xiaojun Sang53cd13a2018-06-29 15:14:37 +08003334 .suppress_bind_attrs = true,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303335 },
3336};
3337
3338static int __init swrm_init(void)
3339{
3340 return platform_driver_register(&swr_mstr_driver);
3341}
3342module_init(swrm_init);
3343
3344static void __exit swrm_exit(void)
3345{
3346 platform_driver_unregister(&swr_mstr_driver);
3347}
3348module_exit(swrm_exit);
3349
3350MODULE_LICENSE("GPL v2");
3351MODULE_DESCRIPTION("SoundWire Master Controller");
3352MODULE_ALIAS("platform:swr-mstr");