blob: 2ab70f3e02a17d63fcdba1dc851565d45eff2fe4 [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;
Laxminath Kasam09819e92019-11-18 14:38:11 +0530503 int comp_sts = 0x0;
Laxminath Kasame2291972019-11-08 14:51:59 +0530504
505 if ((swrm->version <= SWRM_VERSION_1_5_1))
506 return true;
507
508 do {
Laxminath Kasam09819e92019-11-18 14:38:11 +0530509 comp_sts = swr_master_read(swrm, SWRM_COMP_STATUS) & 0x01;
510 /* check comp status and status requested met */
511 if ((comp_sts && status) || (!comp_sts && !status)) {
Laxminath Kasame2291972019-11-08 14:51:59 +0530512 ret = true;
513 break;
514 }
515 retry--;
516 usleep_range(500, 510);
517 } while (retry);
518
519 if (retry == 0)
520 dev_err(swrm->dev, "%s: link status not %s\n", __func__,
521 active ? "connected" : "disconnected");
522
523 return ret;
524}
525
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530526static bool swrm_is_port_en(struct swr_master *mstr)
527{
528 return !!(mstr->num_port);
529}
530
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530531static void copy_port_tables(struct swr_mstr_ctrl *swrm,
532 struct port_params *params)
533{
534 u8 i;
535 struct port_params *config = params;
536
537 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
538 /* wsa uses single frame structure for all configurations */
539 if (!swrm->mport_cfg[i].port_en)
540 continue;
541 swrm->mport_cfg[i].sinterval = config[i].si;
542 swrm->mport_cfg[i].offset1 = config[i].off1;
543 swrm->mport_cfg[i].offset2 = config[i].off2;
544 swrm->mport_cfg[i].hstart = config[i].hstart;
545 swrm->mport_cfg[i].hstop = config[i].hstop;
546 swrm->mport_cfg[i].blk_pack_mode = config[i].bp_mode;
547 swrm->mport_cfg[i].blk_grp_count = config[i].bgp_ctrl;
548 swrm->mport_cfg[i].word_length = config[i].wd_len;
549 swrm->mport_cfg[i].lane_ctrl = config[i].lane_ctrl;
550 }
551}
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530552static int swrm_get_port_config(struct swr_mstr_ctrl *swrm)
553{
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530554 struct port_params *params;
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530555 u32 usecase = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530556
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530557 /* TODO - Send usecase information to avoid checking for master_id */
558 if (swrm->mport_cfg[SWRM_DSD_PARAMS_PORT].port_en &&
559 (swrm->master_id == MASTER_ID_RX))
560 usecase = 1;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530561
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530562 params = swrm->port_param[usecase];
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530563 copy_port_tables(swrm, params);
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530564
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530565 return 0;
566}
567
568static int swrm_get_master_port(struct swr_mstr_ctrl *swrm, u8 *mstr_port_id,
569 u8 *mstr_ch_mask, u8 mstr_prt_type,
570 u8 slv_port_id)
571{
572 int i, j;
573 *mstr_port_id = 0;
574
575 for (i = 1; i <= swrm->num_ports; i++) {
576 for (j = 0; j < SWR_MAX_CH_PER_PORT; j++) {
577 if (swrm->port_mapping[i][j].port_type == mstr_prt_type)
578 goto found;
579 }
580 }
581found:
582 if (i > swrm->num_ports || j == SWR_MAX_CH_PER_PORT) {
583 dev_err(swrm->dev, "%s: port type not supported by master\n",
584 __func__);
585 return -EINVAL;
586 }
587 /* id 0 corresponds to master port 1 */
588 *mstr_port_id = i - 1;
589 *mstr_ch_mask = swrm->port_mapping[i][j].ch_mask;
590
591 return 0;
592
593}
594
595static u32 swrm_get_packed_reg_val(u8 *cmd_id, u8 cmd_data,
596 u8 dev_addr, u16 reg_addr)
597{
598 u32 val;
599 u8 id = *cmd_id;
600
601 if (id != SWR_BROADCAST_CMD_ID) {
602 if (id < 14)
603 id += 1;
604 else
605 id = 0;
606 *cmd_id = id;
607 }
608 val = SWR_REG_VAL_PACK(cmd_data, dev_addr, id, reg_addr);
609
610 return val;
611}
612
613static int swrm_cmd_fifo_rd_cmd(struct swr_mstr_ctrl *swrm, int *cmd_data,
614 u8 dev_addr, u8 cmd_id, u16 reg_addr,
615 u32 len)
616{
617 u32 val;
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530618 u32 retry_attempt = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530619
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530620 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530621 val = swrm_get_packed_reg_val(&swrm->rcmd_id, len, dev_addr, reg_addr);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530622 if (swrm->read) {
623 /* skip delay if read is handled in platform driver */
624 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
625 } else {
626 /* wait for FIFO RD to complete to avoid overflow */
627 usleep_range(100, 105);
628 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
629 /* wait for FIFO RD CMD complete to avoid overflow */
630 usleep_range(250, 255);
631 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530632retry_read:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530633 *cmd_data = swr_master_read(swrm, SWRM_CMD_FIFO_RD_FIFO_ADDR);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530634 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, rcmd_id: 0x%x, \
635 dev_num: 0x%x, cmd_data: 0x%x\n", __func__, reg_addr,
636 cmd_id, swrm->rcmd_id, dev_addr, *cmd_data);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530637 if ((((*cmd_data) & 0xF00) >> 8) != swrm->rcmd_id) {
638 if (retry_attempt < MAX_FIFO_RD_FAIL_RETRY) {
639 /* wait 500 us before retry on fifo read failure */
640 usleep_range(500, 505);
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +0530641 if (retry_attempt == (MAX_FIFO_RD_FAIL_RETRY - 1)) {
642 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
643 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
644 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530645 retry_attempt++;
646 goto retry_read;
647 } else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530648 dev_err_ratelimited(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, \
649 rcmd_id: 0x%x, dev_num: 0x%x, cmd_data: 0x%x\n",
650 __func__, reg_addr, cmd_id, swrm->rcmd_id,
651 dev_addr, *cmd_data);
652
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530653 dev_err_ratelimited(swrm->dev,
654 "%s: failed to read fifo\n", __func__);
655 }
656 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530657 mutex_unlock(&swrm->iolock);
658
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530659 return 0;
660}
661
662static int swrm_cmd_fifo_wr_cmd(struct swr_mstr_ctrl *swrm, u8 cmd_data,
663 u8 dev_addr, u8 cmd_id, u16 reg_addr)
664{
665 u32 val;
666 int ret = 0;
667
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530668 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530669 if (!cmd_id)
670 val = swrm_get_packed_reg_val(&swrm->wcmd_id, cmd_data,
671 dev_addr, reg_addr);
672 else
673 val = swrm_get_packed_reg_val(&cmd_id, cmd_data,
674 dev_addr, reg_addr);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530675 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x,wcmd_id: 0x%x, \
676 dev_num: 0x%x, cmd_data: 0x%x\n", __func__,
677 reg_addr, cmd_id, swrm->wcmd_id,dev_addr, cmd_data);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +0530678 swr_master_write(swrm, SWRM_CMD_FIFO_WR_CMD, val);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530679 /*
680 * wait for FIFO WR command to complete to avoid overflow
681 * skip delay if write is handled in platform driver.
682 */
683 if(!swrm->write)
Karthikeyan Mani5d52dd82019-08-15 16:58:08 -0700684 usleep_range(150, 155);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530685 if (cmd_id == 0xF) {
686 /*
687 * sleep for 10ms for MSM soundwire variant to allow broadcast
688 * command to complete.
689 */
690 if (swrm_is_msm_variant(swrm->version))
691 usleep_range(10000, 10100);
692 else
693 wait_for_completion_timeout(&swrm->broadcast,
694 (2 * HZ/10));
695 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530696 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530697 return ret;
698}
699
700static int swrm_read(struct swr_master *master, u8 dev_num, u16 reg_addr,
701 void *buf, u32 len)
702{
703 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
704 int ret = 0;
705 int val;
706 u8 *reg_val = (u8 *)buf;
707
708 if (!swrm) {
709 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
710 return -EINVAL;
711 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530712 if (!dev_num) {
713 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
714 return -EINVAL;
715 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530716 mutex_lock(&swrm->devlock);
717 if (!swrm->dev_up) {
718 mutex_unlock(&swrm->devlock);
719 return 0;
720 }
721 mutex_unlock(&swrm->devlock);
722
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530723 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530724 ret = swrm_cmd_fifo_rd_cmd(swrm, &val, dev_num, 0, reg_addr, len);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530725
726 if (!ret)
727 *reg_val = (u8)val;
728
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530729 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530730 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530731 return ret;
732}
733
734static int swrm_write(struct swr_master *master, u8 dev_num, u16 reg_addr,
735 const void *buf)
736{
737 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
738 int ret = 0;
739 u8 reg_val = *(u8 *)buf;
740
741 if (!swrm) {
742 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
743 return -EINVAL;
744 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530745 if (!dev_num) {
746 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
747 return -EINVAL;
748 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530749 mutex_lock(&swrm->devlock);
750 if (!swrm->dev_up) {
751 mutex_unlock(&swrm->devlock);
752 return 0;
753 }
754 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530755
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530756 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530757 ret = swrm_cmd_fifo_wr_cmd(swrm, reg_val, dev_num, 0, reg_addr);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530758
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530759 pm_runtime_put_autosuspend(swrm->dev);
760 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530761 return ret;
762}
763
764static int swrm_bulk_write(struct swr_master *master, u8 dev_num, void *reg,
765 const void *buf, size_t len)
766{
767 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
768 int ret = 0;
769 int i;
770 u32 *val;
771 u32 *swr_fifo_reg;
772
773 if (!swrm || !swrm->handle) {
774 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
775 return -EINVAL;
776 }
777 if (len <= 0)
778 return -EINVAL;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530779 mutex_lock(&swrm->devlock);
780 if (!swrm->dev_up) {
781 mutex_unlock(&swrm->devlock);
782 return 0;
783 }
784 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530785
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530786 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530787 if (dev_num) {
788 swr_fifo_reg = kcalloc(len, sizeof(u32), GFP_KERNEL);
789 if (!swr_fifo_reg) {
790 ret = -ENOMEM;
791 goto err;
792 }
793 val = kcalloc(len, sizeof(u32), GFP_KERNEL);
794 if (!val) {
795 ret = -ENOMEM;
796 goto mem_fail;
797 }
798
799 for (i = 0; i < len; i++) {
800 val[i] = swrm_get_packed_reg_val(&swrm->wcmd_id,
801 ((u8 *)buf)[i],
802 dev_num,
803 ((u16 *)reg)[i]);
804 swr_fifo_reg[i] = SWRM_CMD_FIFO_WR_CMD;
805 }
806 ret = swr_master_bulk_write(swrm, swr_fifo_reg, val, len);
807 if (ret) {
808 dev_err(&master->dev, "%s: bulk write failed\n",
809 __func__);
810 ret = -EINVAL;
811 }
812 } else {
813 dev_err(&master->dev,
814 "%s: No support of Bulk write for master regs\n",
815 __func__);
816 ret = -EINVAL;
817 goto err;
818 }
819 kfree(val);
820mem_fail:
821 kfree(swr_fifo_reg);
822err:
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530823 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530824 pm_runtime_mark_last_busy(swrm->dev);
825 return ret;
826}
827
828static u8 get_inactive_bank_num(struct swr_mstr_ctrl *swrm)
829{
830 return (swr_master_read(swrm, SWRM_MCP_STATUS) &
831 SWRM_MCP_STATUS_BANK_NUM_MASK) ? 0 : 1;
832}
833
834static void enable_bank_switch(struct swr_mstr_ctrl *swrm, u8 bank,
835 u8 row, u8 col)
836{
837 swrm_cmd_fifo_wr_cmd(swrm, ((row << 3) | col), 0xF, 0xF,
838 SWRS_SCP_FRAME_CTRL_BANK(bank));
839}
840
841static struct swr_port_info *swrm_get_port_req(struct swrm_mports *mport,
842 u8 slv_port, u8 dev_num)
843{
844 struct swr_port_info *port_req = NULL;
845
846 list_for_each_entry(port_req, &mport->port_req_list, list) {
847 /* Store dev_id instead of dev_num if enumeration is changed run_time */
848 if ((port_req->slave_port_id == slv_port)
849 && (port_req->dev_num == dev_num))
850 return port_req;
851 }
852 return NULL;
853}
854
855static bool swrm_remove_from_group(struct swr_master *master)
856{
857 struct swr_device *swr_dev;
858 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
859 bool is_removed = false;
860
861 if (!swrm)
862 goto end;
863
864 mutex_lock(&swrm->mlock);
865 if ((swrm->num_rx_chs > 1) &&
866 (swrm->num_rx_chs == swrm->num_cfg_devs)) {
867 list_for_each_entry(swr_dev, &master->devices,
868 dev_list) {
869 swr_dev->group_id = SWR_GROUP_NONE;
870 master->gr_sid = 0;
871 }
872 is_removed = true;
873 }
874 mutex_unlock(&swrm->mlock);
875
876end:
877 return is_removed;
878}
879
880static void swrm_disable_ports(struct swr_master *master,
881 u8 bank)
882{
883 u32 value;
884 struct swr_port_info *port_req;
885 int i;
886 struct swrm_mports *mport;
887 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
888
889 if (!swrm) {
890 pr_err("%s: swrm is null\n", __func__);
891 return;
892 }
893
894 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
895 master->num_port);
896
897
898 for (i = 0; i < SWR_MSTR_PORT_LEN ; i++) {
899
900 mport = &(swrm->mport_cfg[i]);
901 if (!mport->port_en)
902 continue;
903
904 list_for_each_entry(port_req, &mport->port_req_list, list) {
905 /* skip ports with no change req's*/
906 if (port_req->req_ch == port_req->ch_en)
907 continue;
908
909 swrm_cmd_fifo_wr_cmd(swrm, port_req->req_ch,
910 port_req->dev_num, 0x00,
911 SWRS_DP_CHANNEL_ENABLE_BANK(port_req->slave_port_id,
912 bank));
913 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x\n",
914 __func__, i,
915 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)));
916 }
917 value = ((mport->req_ch)
918 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
919 value |= ((mport->offset2)
920 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
921 value |= ((mport->offset1)
922 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
923 value |= mport->sinterval;
924
925 swr_master_write(swrm,
926 SWRM_DP_PORT_CTRL_BANK(i+1, bank),
927 value);
928 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
929 __func__, i,
930 (SWRM_DP_PORT_CTRL_BANK(i+1, bank)), value);
931 }
932}
933
934static void swrm_cleanup_disabled_port_reqs(struct swr_master *master)
935{
936 struct swr_port_info *port_req, *next;
937 int i;
938 struct swrm_mports *mport;
939 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
940
941 if (!swrm) {
942 pr_err("%s: swrm is null\n", __func__);
943 return;
944 }
945 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
946 master->num_port);
947
948 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
949 mport = &(swrm->mport_cfg[i]);
950 list_for_each_entry_safe(port_req, next,
951 &mport->port_req_list, list) {
952 /* skip ports without new ch req */
953 if (port_req->ch_en == port_req->req_ch)
954 continue;
955
956 /* remove new ch req's*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +0530957 port_req->ch_en = port_req->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530958
959 /* If no streams enabled on port, remove the port req */
960 if (port_req->ch_en == 0) {
961 list_del(&port_req->list);
962 kfree(port_req);
963 }
964 }
965 /* remove new ch req's on mport*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +0530966 mport->ch_en = mport->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530967
968 if (!(mport->ch_en)) {
969 mport->port_en = false;
970 master->port_en_mask &= ~i;
971 }
972 }
973}
974static void swrm_copy_data_port_config(struct swr_master *master, u8 bank)
975{
976 u32 value, slv_id;
977 struct swr_port_info *port_req;
978 int i;
979 struct swrm_mports *mport;
980 u32 reg[SWRM_MAX_PORT_REG];
981 u32 val[SWRM_MAX_PORT_REG];
982 int len = 0;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530983 u8 hparams;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530984 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
985
986 if (!swrm) {
987 pr_err("%s: swrm is null\n", __func__);
988 return;
989 }
990
991 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
992 master->num_port);
993
994 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
995 mport = &(swrm->mport_cfg[i]);
996 if (!mport->port_en)
997 continue;
998
999 list_for_each_entry(port_req, &mport->port_req_list, list) {
1000 slv_id = port_req->slave_port_id;
1001 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1002 val[len++] = SWR_REG_VAL_PACK(port_req->req_ch,
1003 port_req->dev_num, 0x00,
1004 SWRS_DP_CHANNEL_ENABLE_BANK(slv_id,
1005 bank));
1006
1007 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1008 val[len++] = SWR_REG_VAL_PACK(mport->sinterval,
1009 port_req->dev_num, 0x00,
1010 SWRS_DP_SAMPLE_CONTROL_1_BANK(slv_id,
1011 bank));
1012
1013 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1014 val[len++] = SWR_REG_VAL_PACK(mport->offset1,
1015 port_req->dev_num, 0x00,
1016 SWRS_DP_OFFSET_CONTROL_1_BANK(slv_id,
1017 bank));
1018
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301019 if (mport->offset2 != SWR_INVALID_PARAM) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301020 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1021 val[len++] = SWR_REG_VAL_PACK(mport->offset2,
1022 port_req->dev_num, 0x00,
1023 SWRS_DP_OFFSET_CONTROL_2_BANK(
1024 slv_id, bank));
1025 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301026 if (mport->hstart != SWR_INVALID_PARAM
1027 && mport->hstop != SWR_INVALID_PARAM) {
1028 hparams = (mport->hstart << 4) | mport->hstop;
1029
1030 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1031 val[len++] = SWR_REG_VAL_PACK(hparams,
1032 port_req->dev_num, 0x00,
1033 SWRS_DP_HCONTROL_BANK(slv_id,
1034 bank));
1035 }
1036 if (mport->word_length != SWR_INVALID_PARAM) {
1037 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1038 val[len++] =
1039 SWR_REG_VAL_PACK(mport->word_length,
1040 port_req->dev_num, 0x00,
1041 SWRS_DP_BLOCK_CONTROL_1(slv_id));
1042 }
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +05301043 if (mport->blk_pack_mode != SWR_INVALID_PARAM
1044 && swrm->master_id != MASTER_ID_WSA) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301045 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1046 val[len++] =
1047 SWR_REG_VAL_PACK(mport->blk_pack_mode,
1048 port_req->dev_num, 0x00,
1049 SWRS_DP_BLOCK_CONTROL_3_BANK(slv_id,
1050 bank));
1051 }
1052 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
1053 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1054 val[len++] =
1055 SWR_REG_VAL_PACK(mport->blk_grp_count,
1056 port_req->dev_num, 0x00,
1057 SWRS_DP_BLOCK_CONTROL_2_BANK(slv_id,
1058 bank));
1059 }
1060 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
1061 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1062 val[len++] =
1063 SWR_REG_VAL_PACK(mport->lane_ctrl,
1064 port_req->dev_num, 0x00,
1065 SWRS_DP_LANE_CONTROL_BANK(slv_id,
1066 bank));
1067 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301068 port_req->ch_en = port_req->req_ch;
1069 }
1070 value = ((mport->req_ch)
1071 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +05301072
1073 if (mport->offset2 != SWR_INVALID_PARAM)
1074 value |= ((mport->offset2)
1075 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301076 value |= ((mport->offset1)
1077 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
1078 value |= mport->sinterval;
1079
1080
1081 reg[len] = SWRM_DP_PORT_CTRL_BANK(i + 1, bank);
1082 val[len++] = value;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301083 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
1084 __func__, i,
1085 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)), value);
1086
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301087 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
1088 reg[len] = SWRM_DP_PORT_CTRL_2_BANK(i + 1, bank);
1089 val[len++] = mport->lane_ctrl;
1090 }
1091 if (mport->word_length != SWR_INVALID_PARAM) {
1092 reg[len] = SWRM_DP_BLOCK_CTRL_1(i + 1);
1093 val[len++] = mport->word_length;
1094 }
1095
1096 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
1097 reg[len] = SWRM_DP_BLOCK_CTRL2_BANK(i + 1, bank);
1098 val[len++] = mport->blk_grp_count;
1099 }
1100 if (mport->hstart != SWR_INVALID_PARAM
1101 && mport->hstop != SWR_INVALID_PARAM) {
1102 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
Laxminath Kasame30eef72018-11-05 17:40:09 +05301103 hparams = (mport->hstop << 4) | mport->hstart;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301104 val[len++] = hparams;
Laxminath Kasam990c70b2018-11-09 23:15:09 +05301105 } else {
1106 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
1107 hparams = (SWR_HSTOP_MAX_VAL << 4) | SWR_HSTART_MIN_VAL;
1108 val[len++] = hparams;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301109 }
1110 if (mport->blk_pack_mode != SWR_INVALID_PARAM) {
1111 reg[len] = SWRM_DP_BLOCK_CTRL3_BANK(i + 1, bank);
1112 val[len++] = mport->blk_pack_mode;
1113 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301114 mport->ch_en = mport->req_ch;
1115
1116 }
Sudheer Papothi0016db12019-06-11 04:42:38 +05301117 swrm_reg_dump(swrm, reg, val, len, __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301118 swr_master_bulk_write(swrm, reg, val, len);
1119}
1120
1121static void swrm_apply_port_config(struct swr_master *master)
1122{
1123 u8 bank;
1124 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1125
1126 if (!swrm) {
1127 pr_err("%s: Invalid handle to swr controller\n",
1128 __func__);
1129 return;
1130 }
1131
1132 bank = get_inactive_bank_num(swrm);
1133 dev_dbg(swrm->dev, "%s: enter bank: %d master_ports: %d\n",
1134 __func__, bank, master->num_port);
1135
1136
1137 swrm_cmd_fifo_wr_cmd(swrm, 0x01, 0xF, 0x00,
1138 SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(bank));
1139
1140 swrm_copy_data_port_config(master, bank);
1141}
1142
1143static int swrm_slvdev_datapath_control(struct swr_master *master, bool enable)
1144{
1145 u8 bank;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301146 u32 value, n_row, n_col;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301147 u32 row = 0, col = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301148 int ret;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301149 u8 ssp_period = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301150 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1151 int mask = (SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK |
1152 SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK |
1153 SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_BMSK);
1154 u8 inactive_bank;
1155
1156 if (!swrm) {
1157 pr_err("%s: swrm is null\n", __func__);
1158 return -EFAULT;
1159 }
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301160
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301161 mutex_lock(&swrm->mlock);
1162
Ramprasad Katkam979b7c92019-05-17 15:31:21 +05301163 /*
1164 * During disable if master is already down, which implies an ssr/pdr
1165 * scenario, just mark ports as disabled and exit
1166 */
1167 if (swrm->state == SWR_MSTR_SSR && !enable) {
1168 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1169 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1170 __func__);
1171 goto exit;
1172 }
1173 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
1174 swrm_cleanup_disabled_port_reqs(master);
1175 if (!swrm_is_port_en(master)) {
1176 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1177 __func__);
1178 pm_runtime_mark_last_busy(swrm->dev);
1179 pm_runtime_put_autosuspend(swrm->dev);
1180 }
1181 goto exit;
1182 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301183 bank = get_inactive_bank_num(swrm);
1184
1185 if (enable) {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301186 if (!test_bit(ENABLE_PENDING, &swrm->port_req_pending)) {
1187 dev_dbg(swrm->dev, "%s:No pending connect port req\n",
1188 __func__);
1189 goto exit;
1190 }
1191 clear_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301192 ret = swrm_get_port_config(swrm);
1193 if (ret) {
1194 /* cannot accommodate ports */
1195 swrm_cleanup_disabled_port_reqs(master);
1196 mutex_unlock(&swrm->mlock);
1197 return -EINVAL;
1198 }
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301199 swr_master_write(swrm, SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301200 SWRM_INTERRUPT_STATUS_MASK);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301201 /* apply the new port config*/
1202 swrm_apply_port_config(master);
1203 } else {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301204 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1205 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1206 __func__);
1207 goto exit;
1208 }
1209 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301210 swrm_disable_ports(master, bank);
1211 }
1212 dev_dbg(swrm->dev, "%s: enable: %d, cfg_devs: %d\n",
1213 __func__, enable, swrm->num_cfg_devs);
1214
1215 if (enable) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301216 /* set col = 16 */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301217 n_col = SWR_MAX_COL;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301218 col = SWRM_COL_16;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301219 } else {
1220 /*
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301221 * Do not change to col = 2 if there are still active ports
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301222 */
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301223 if (!master->num_port) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301224 n_col = SWR_MIN_COL;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301225 col = SWRM_COL_02;
1226 } else {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301227 n_col = SWR_MAX_COL;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301228 col = SWRM_COL_16;
1229 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301230 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301231 /* Use default 50 * x, frame shape. Change based on mclk */
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301232 if (swrm->mclk_freq == MCLK_FREQ_NATIVE) {
1233 dev_dbg(swrm->dev, "setting 64 x %d frameshape\n",
1234 n_col ? 16 : 2);
1235 n_row = SWR_ROW_64;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301236 row = SWRM_ROW_64;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301237 } else {
1238 dev_dbg(swrm->dev, "setting 50 x %d frameshape\n",
1239 n_col ? 16 : 2);
1240 n_row = SWR_ROW_50;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301241 row = SWRM_ROW_50;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301242 }
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301243 ssp_period = swrm_get_ssp_period(swrm, row, col, SWRM_FRAME_SYNC_SEL);
1244 dev_dbg(swrm->dev, "%s: ssp_period: %d\n", __func__, ssp_period);
1245
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301246 value = swr_master_read(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank));
1247 value &= (~mask);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301248 value |= ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301249 (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301250 ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301251 swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1252
1253 dev_dbg(swrm->dev, "%s: regaddr: 0x%x, value: 0x%x\n", __func__,
1254 SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1255
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301256 enable_bank_switch(swrm, bank, n_row, n_col);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301257 inactive_bank = bank ? 0 : 1;
1258
1259 if (enable)
1260 swrm_copy_data_port_config(master, inactive_bank);
1261 else {
1262 swrm_disable_ports(master, inactive_bank);
1263 swrm_cleanup_disabled_port_reqs(master);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301264 }
1265 if (!swrm_is_port_en(master)) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301266 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1267 __func__);
1268 pm_runtime_mark_last_busy(swrm->dev);
1269 pm_runtime_put_autosuspend(swrm->dev);
1270 }
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301271exit:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301272 mutex_unlock(&swrm->mlock);
1273return 0;
1274}
1275
1276static int swrm_connect_port(struct swr_master *master,
1277 struct swr_params *portinfo)
1278{
1279 int i;
1280 struct swr_port_info *port_req;
1281 int ret = 0;
1282 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1283 struct swrm_mports *mport;
1284 u8 mstr_port_id, mstr_ch_msk;
1285
1286 dev_dbg(&master->dev, "%s: enter\n", __func__);
1287 if (!portinfo)
1288 return -EINVAL;
1289
1290 if (!swrm) {
1291 dev_err(&master->dev,
1292 "%s: Invalid handle to swr controller\n",
1293 __func__);
1294 return -EINVAL;
1295 }
1296
1297 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301298 mutex_lock(&swrm->devlock);
1299 if (!swrm->dev_up) {
1300 mutex_unlock(&swrm->devlock);
1301 mutex_unlock(&swrm->mlock);
1302 return -EINVAL;
1303 }
1304 mutex_unlock(&swrm->devlock);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301305 if (!swrm_is_port_en(master))
1306 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301307
1308 for (i = 0; i < portinfo->num_port; i++) {
1309 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_msk,
1310 portinfo->port_type[i],
1311 portinfo->port_id[i]);
1312 if (ret) {
1313 dev_err(&master->dev,
1314 "%s: mstr portid for slv port %d not found\n",
1315 __func__, portinfo->port_id[i]);
1316 goto port_fail;
1317 }
1318
1319 mport = &(swrm->mport_cfg[mstr_port_id]);
1320 /* get port req */
1321 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1322 portinfo->dev_num);
1323 if (!port_req) {
1324 dev_dbg(&master->dev, "%s: new req:port id %d dev %d\n",
1325 __func__, portinfo->port_id[i],
1326 portinfo->dev_num);
1327 port_req = kzalloc(sizeof(struct swr_port_info),
1328 GFP_KERNEL);
1329 if (!port_req) {
1330 ret = -ENOMEM;
1331 goto mem_fail;
1332 }
1333 port_req->dev_num = portinfo->dev_num;
1334 port_req->slave_port_id = portinfo->port_id[i];
1335 port_req->num_ch = portinfo->num_ch[i];
1336 port_req->ch_rate = portinfo->ch_rate[i];
1337 port_req->ch_en = 0;
1338 port_req->master_port_id = mstr_port_id;
1339 list_add(&port_req->list, &mport->port_req_list);
1340 }
1341 port_req->req_ch |= portinfo->ch_en[i];
1342
1343 dev_dbg(&master->dev,
1344 "%s: mstr port %d, slv port %d ch_rate %d num_ch %d\n",
1345 __func__, port_req->master_port_id,
1346 port_req->slave_port_id, port_req->ch_rate,
1347 port_req->num_ch);
1348 /* Put the port req on master port */
1349 mport = &(swrm->mport_cfg[mstr_port_id]);
1350 mport->port_en = true;
1351 mport->req_ch |= mstr_ch_msk;
1352 master->port_en_mask |= (1 << mstr_port_id);
1353 }
1354 master->num_port += portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301355 set_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301356 swr_port_response(master, portinfo->tid);
1357
1358 mutex_unlock(&swrm->mlock);
1359 return 0;
1360
1361port_fail:
1362mem_fail:
1363 /* cleanup port reqs in error condition */
1364 swrm_cleanup_disabled_port_reqs(master);
1365 mutex_unlock(&swrm->mlock);
1366 return ret;
1367}
1368
1369static int swrm_disconnect_port(struct swr_master *master,
1370 struct swr_params *portinfo)
1371{
1372 int i, ret = 0;
1373 struct swr_port_info *port_req;
1374 struct swrm_mports *mport;
1375 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1376 u8 mstr_port_id, mstr_ch_mask;
1377
1378 if (!swrm) {
1379 dev_err(&master->dev,
1380 "%s: Invalid handle to swr controller\n",
1381 __func__);
1382 return -EINVAL;
1383 }
1384
1385 if (!portinfo) {
1386 dev_err(&master->dev, "%s: portinfo is NULL\n", __func__);
1387 return -EINVAL;
1388 }
1389 mutex_lock(&swrm->mlock);
1390
1391 for (i = 0; i < portinfo->num_port; i++) {
1392
1393 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_mask,
1394 portinfo->port_type[i], portinfo->port_id[i]);
1395 if (ret) {
1396 dev_err(&master->dev,
1397 "%s: mstr portid for slv port %d not found\n",
1398 __func__, portinfo->port_id[i]);
1399 mutex_unlock(&swrm->mlock);
1400 return -EINVAL;
1401 }
1402 mport = &(swrm->mport_cfg[mstr_port_id]);
1403 /* get port req */
1404 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1405 portinfo->dev_num);
1406
1407 if (!port_req) {
1408 dev_err(&master->dev, "%s:port not enabled : port %d\n",
1409 __func__, portinfo->port_id[i]);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05301410 mutex_unlock(&swrm->mlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301411 return -EINVAL;
1412 }
1413 port_req->req_ch &= ~portinfo->ch_en[i];
1414 mport->req_ch &= ~mstr_ch_mask;
1415 }
1416 master->num_port -= portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301417 set_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301418 swr_port_response(master, portinfo->tid);
1419 mutex_unlock(&swrm->mlock);
1420
1421 return 0;
1422}
1423
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301424static int swrm_find_alert_slave(struct swr_mstr_ctrl *swrm,
1425 int status, u8 *devnum)
1426{
1427 int i;
1428 bool found = false;
1429
1430 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1431 if ((status & SWRM_MCP_SLV_STATUS_MASK) == SWR_ALERT) {
1432 *devnum = i;
1433 found = true;
1434 break;
1435 }
1436 status >>= 2;
1437 }
1438 if (found)
1439 return 0;
1440 else
1441 return -EINVAL;
1442}
1443
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301444static void swrm_enable_slave_irq(struct swr_mstr_ctrl *swrm)
1445{
1446 int i;
1447 int status = 0;
1448
1449 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1450 if (!status) {
1451 dev_dbg_ratelimited(swrm->dev, "%s: slaves status is 0x%x\n",
1452 __func__, status);
1453 return;
1454 }
1455 dev_dbg(swrm->dev, "%s: slave status: 0x%x\n", __func__, status);
1456 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1457 if (status & SWRM_MCP_SLV_STATUS_MASK)
1458 swrm_cmd_fifo_wr_cmd(swrm, 0x4, i, 0x0,
1459 SWRS_SCP_INT_STATUS_MASK_1);
1460 status >>= 2;
1461 }
1462}
1463
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301464static int swrm_check_slave_change_status(struct swr_mstr_ctrl *swrm,
1465 int status, u8 *devnum)
1466{
1467 int i;
1468 int new_sts = status;
1469 int ret = SWR_NOT_PRESENT;
1470
1471 if (status != swrm->slave_status) {
1472 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1473 if ((status & SWRM_MCP_SLV_STATUS_MASK) !=
1474 (swrm->slave_status & SWRM_MCP_SLV_STATUS_MASK)) {
1475 ret = (status & SWRM_MCP_SLV_STATUS_MASK);
1476 *devnum = i;
1477 break;
1478 }
1479 status >>= 2;
1480 swrm->slave_status >>= 2;
1481 }
1482 swrm->slave_status = new_sts;
1483 }
1484 return ret;
1485}
1486
1487static irqreturn_t swr_mstr_interrupt(int irq, void *dev)
1488{
1489 struct swr_mstr_ctrl *swrm = dev;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301490 u32 value, intr_sts, intr_sts_masked;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301491 u32 temp = 0;
1492 u32 status, chg_sts, i;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301493 u8 devnum = 0;
1494 int ret = IRQ_HANDLED;
1495 struct swr_device *swr_dev;
1496 struct swr_master *mstr = &swrm->master;
1497
Ramprasad Katkam57349872018-11-11 18:34:57 +05301498 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1499 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1500 return IRQ_NONE;
1501 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301502
1503 mutex_lock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301504 if (swrm_clk_request(swrm, true)) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +05301505 dev_err_ratelimited(swrm->dev, "%s:clk request failed\n",
1506 __func__);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301507 mutex_unlock(&swrm->reslock);
1508 goto exit;
1509 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301510 mutex_unlock(&swrm->reslock);
1511
1512 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301513 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301514handle_irq:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301515 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301516 value = intr_sts_masked & (1 << i);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301517 if (!value)
1518 continue;
1519
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301520 switch (value) {
1521 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1522 dev_dbg(swrm->dev, "Trigger irq to slave device\n");
1523 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301524 ret = swrm_find_alert_slave(swrm, status, &devnum);
1525 if (ret) {
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301526 dev_err_ratelimited(swrm->dev,
1527 "no slave alert found.spurious interrupt\n");
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05301528 break;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301529 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301530 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1531 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1532 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1533 SWRS_SCP_INT_STATUS_CLEAR_1);
1534 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1535 SWRS_SCP_INT_STATUS_CLEAR_1);
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301536
1537
1538 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1539 if (swr_dev->dev_num != devnum)
1540 continue;
1541 if (swr_dev->slave_irq) {
1542 do {
Ramprasad Katkam2586a4b2019-03-18 16:53:39 +05301543 swr_dev->slave_irq_pending = 0;
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301544 handle_nested_irq(
1545 irq_find_mapping(
1546 swr_dev->slave_irq, 0));
1547 } while (swr_dev->slave_irq_pending);
1548 }
1549
1550 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301551 break;
1552 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1553 dev_dbg(swrm->dev, "SWR new slave attached\n");
1554 break;
1555 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1556 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1557 if (status == swrm->slave_status) {
1558 dev_dbg(swrm->dev,
1559 "%s: No change in slave status: %d\n",
1560 __func__, status);
1561 break;
1562 }
1563 chg_sts = swrm_check_slave_change_status(swrm, status,
1564 &devnum);
1565 switch (chg_sts) {
1566 case SWR_NOT_PRESENT:
1567 dev_dbg(swrm->dev, "device %d got detached\n",
1568 devnum);
1569 break;
1570 case SWR_ATTACHED_OK:
1571 dev_dbg(swrm->dev, "device %d got attached\n",
1572 devnum);
Ramprasad Katkamdebe8932018-09-25 18:08:18 +05301573 /* enable host irq from slave device*/
1574 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1575 SWRS_SCP_INT_STATUS_CLEAR_1);
1576 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1577 SWRS_SCP_INT_STATUS_MASK_1);
1578
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301579 break;
1580 case SWR_ALERT:
1581 dev_dbg(swrm->dev,
1582 "device %d has pending interrupt\n",
1583 devnum);
1584 break;
1585 }
1586 break;
1587 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1588 dev_err_ratelimited(swrm->dev,
1589 "SWR bus clsh detected\n");
1590 break;
1591 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1592 dev_dbg(swrm->dev, "SWR read FIFO overflow\n");
1593 break;
1594 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1595 dev_dbg(swrm->dev, "SWR read FIFO underflow\n");
1596 break;
1597 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1598 dev_dbg(swrm->dev, "SWR write FIFO overflow\n");
1599 break;
1600 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1601 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1602 dev_err_ratelimited(swrm->dev,
1603 "SWR CMD error, fifo status 0x%x, flushing fifo\n",
1604 value);
1605 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1606 break;
1607 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301608 dev_err_ratelimited(swrm->dev, "SWR Port collision detected\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301609 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301610 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301611 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301612 break;
1613 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1614 dev_dbg(swrm->dev, "SWR read enable valid mismatch\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301615 swrm->intr_mask &=
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301616 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1617 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301618 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301619 break;
1620 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1621 complete(&swrm->broadcast);
1622 dev_dbg(swrm->dev, "SWR cmd id finished\n");
1623 break;
1624 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_AUTO_ENUM_FINISHED:
1625 break;
1626 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED:
1627 break;
1628 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL:
1629 break;
1630 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED:
1631 complete(&swrm->reset);
1632 break;
1633 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED:
1634 break;
1635 default:
1636 dev_err_ratelimited(swrm->dev,
1637 "SWR unknown interrupt\n");
1638 ret = IRQ_NONE;
1639 break;
1640 }
1641 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301642 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1643 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
Ramprasad Katkam83303512018-10-11 17:34:22 +05301644
1645 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301646 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301647
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301648 if (intr_sts_masked) {
Ramprasad Katkam83303512018-10-11 17:34:22 +05301649 dev_dbg(swrm->dev, "%s: new interrupt received\n", __func__);
1650 goto handle_irq;
1651 }
1652
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301653 mutex_lock(&swrm->reslock);
1654 swrm_clk_request(swrm, false);
1655 mutex_unlock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301656exit:
Ramprasad Katkam57349872018-11-11 18:34:57 +05301657 swrm_unlock_sleep(swrm);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301658 return ret;
1659}
1660
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301661static irqreturn_t swr_mstr_interrupt_v2(int irq, void *dev)
1662{
1663 struct swr_mstr_ctrl *swrm = dev;
1664 u32 value, intr_sts, intr_sts_masked;
1665 u32 temp = 0;
1666 u32 status, chg_sts, i;
1667 u8 devnum = 0;
1668 int ret = IRQ_HANDLED;
1669 struct swr_device *swr_dev;
1670 struct swr_master *mstr = &swrm->master;
1671
1672 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1673 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1674 return IRQ_NONE;
1675 }
1676
1677 mutex_lock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301678 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
1679 ret = IRQ_NONE;
1680 goto exit;
1681 }
1682 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
1683 ret = IRQ_NONE;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301684 goto err_audio_hw_vote;
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001685 }
Karthikeyan Mani4bee1db2019-09-18 17:58:41 -07001686 ret = swrm_clk_request(swrm, true);
1687 if (ret) {
1688 dev_err(dev, "%s: swrm clk failed\n", __func__);
1689 ret = IRQ_NONE;
1690 goto err_audio_core_vote;
1691 }
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301692 mutex_unlock(&swrm->reslock);
1693
1694 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1695 intr_sts_masked = intr_sts & swrm->intr_mask;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301696
1697 dev_dbg(swrm->dev, "%s: status: 0x%x \n", __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301698handle_irq:
1699 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
1700 value = intr_sts_masked & (1 << i);
1701 if (!value)
1702 continue;
1703
1704 switch (value) {
1705 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1706 dev_dbg(swrm->dev, "%s: Trigger irq to slave device\n",
1707 __func__);
1708 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1709 ret = swrm_find_alert_slave(swrm, status, &devnum);
1710 if (ret) {
1711 dev_err_ratelimited(swrm->dev,
1712 "%s: no slave alert found.spurious interrupt\n",
1713 __func__);
1714 break;
1715 }
1716 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1717 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1718 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1719 SWRS_SCP_INT_STATUS_CLEAR_1);
1720 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1721 SWRS_SCP_INT_STATUS_CLEAR_1);
1722
1723
1724 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1725 if (swr_dev->dev_num != devnum)
1726 continue;
1727 if (swr_dev->slave_irq) {
1728 do {
1729 handle_nested_irq(
1730 irq_find_mapping(
1731 swr_dev->slave_irq, 0));
1732 } while (swr_dev->slave_irq_pending);
1733 }
1734
1735 }
1736 break;
1737 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1738 dev_dbg(swrm->dev, "%s: SWR new slave attached\n",
1739 __func__);
1740 break;
1741 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1742 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1743 if (status == swrm->slave_status) {
1744 dev_dbg(swrm->dev,
1745 "%s: No change in slave status: %d\n",
1746 __func__, status);
1747 break;
1748 }
1749 chg_sts = swrm_check_slave_change_status(swrm, status,
1750 &devnum);
1751 switch (chg_sts) {
1752 case SWR_NOT_PRESENT:
1753 dev_dbg(swrm->dev,
1754 "%s: device %d got detached\n",
1755 __func__, devnum);
1756 break;
1757 case SWR_ATTACHED_OK:
1758 dev_dbg(swrm->dev,
1759 "%s: device %d got attached\n",
1760 __func__, devnum);
1761 /* enable host irq from slave device*/
1762 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1763 SWRS_SCP_INT_STATUS_CLEAR_1);
1764 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1765 SWRS_SCP_INT_STATUS_MASK_1);
1766
1767 break;
1768 case SWR_ALERT:
1769 dev_dbg(swrm->dev,
1770 "%s: device %d has pending interrupt\n",
1771 __func__, devnum);
1772 break;
1773 }
1774 break;
1775 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1776 dev_err_ratelimited(swrm->dev,
1777 "%s: SWR bus clsh detected\n",
1778 __func__);
1779 break;
1780 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1781 dev_dbg(swrm->dev, "%s: SWR read FIFO overflow\n",
1782 __func__);
1783 break;
1784 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1785 dev_dbg(swrm->dev, "%s: SWR read FIFO underflow\n",
1786 __func__);
1787 break;
1788 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1789 dev_dbg(swrm->dev, "%s: SWR write FIFO overflow\n",
1790 __func__);
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301791 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301792 break;
1793 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1794 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1795 dev_err_ratelimited(swrm->dev,
1796 "%s: SWR CMD error, fifo status 0x%x, flushing fifo\n",
1797 __func__, value);
1798 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1799 break;
1800 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
1801 dev_err_ratelimited(swrm->dev,
1802 "%s: SWR Port collision detected\n",
1803 __func__);
1804 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
1805 swr_master_write(swrm,
1806 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1807 break;
1808 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1809 dev_dbg(swrm->dev,
1810 "%s: SWR read enable valid mismatch\n",
1811 __func__);
1812 swrm->intr_mask &=
1813 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1814 swr_master_write(swrm,
1815 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1816 break;
1817 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1818 complete(&swrm->broadcast);
1819 dev_dbg(swrm->dev, "%s: SWR cmd id finished\n",
1820 __func__);
1821 break;
1822 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED_V2:
1823 break;
1824 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL_V2:
1825 break;
1826 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED_V2:
Laxminath Kasame2291972019-11-08 14:51:59 +05301827 swrm_check_link_status(swrm, 0x1);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301828 break;
1829 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED_V2:
1830 break;
1831 case SWRM_INTERRUPT_STATUS_EXT_CLK_STOP_WAKEUP:
1832 if (swrm->state == SWR_MSTR_UP)
1833 dev_dbg(swrm->dev,
1834 "%s:SWR Master is already up\n",
1835 __func__);
1836 else
1837 dev_err_ratelimited(swrm->dev,
1838 "%s: SWR wokeup during clock stop\n",
1839 __func__);
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301840 /* It might be possible the slave device gets reset
1841 * and slave interrupt gets missed. So re-enable
1842 * Host IRQ and process slave pending
1843 * interrupts, if any.
1844 */
1845 swrm_enable_slave_irq(swrm);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301846 break;
1847 default:
1848 dev_err_ratelimited(swrm->dev,
1849 "%s: SWR unknown interrupt value: %d\n",
1850 __func__, value);
1851 ret = IRQ_NONE;
1852 break;
1853 }
1854 }
1855 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1856 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
1857
1858 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1859 intr_sts_masked = intr_sts & swrm->intr_mask;
1860
1861 if (intr_sts_masked) {
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301862 dev_dbg(swrm->dev, "%s: new interrupt received 0x%x\n",
1863 __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301864 goto handle_irq;
1865 }
1866
1867 mutex_lock(&swrm->reslock);
1868 swrm_clk_request(swrm, false);
Karthikeyan Mani4bee1db2019-09-18 17:58:41 -07001869err_audio_core_vote:
Sudheer Papothi384addd2019-06-14 02:26:52 +05301870 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
Sudheer Papothi06f43412019-07-09 03:32:54 +05301871
1872err_audio_hw_vote:
Sudheer Papothi384addd2019-06-14 02:26:52 +05301873 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001874exit:
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301875 mutex_unlock(&swrm->reslock);
1876 swrm_unlock_sleep(swrm);
1877 return ret;
1878}
1879
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301880static irqreturn_t swrm_wakeup_interrupt(int irq, void *dev)
1881{
1882 struct swr_mstr_ctrl *swrm = dev;
1883 int ret = IRQ_HANDLED;
1884
1885 if (!swrm || !(swrm->dev)) {
1886 pr_err("%s: swrm or dev is null\n", __func__);
1887 return IRQ_NONE;
1888 }
1889 mutex_lock(&swrm->devlock);
1890 if (!swrm->dev_up) {
1891 if (swrm->wake_irq > 0)
1892 disable_irq_nosync(swrm->wake_irq);
1893 mutex_unlock(&swrm->devlock);
1894 return ret;
1895 }
1896 mutex_unlock(&swrm->devlock);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301897 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1898 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1899 goto exit;
1900 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301901 if (swrm->wake_irq > 0)
1902 disable_irq_nosync(swrm->wake_irq);
1903 pm_runtime_get_sync(swrm->dev);
1904 pm_runtime_mark_last_busy(swrm->dev);
1905 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301906 swrm_unlock_sleep(swrm);
1907exit:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301908 return ret;
1909}
1910
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301911static void swrm_wakeup_work(struct work_struct *work)
1912{
1913 struct swr_mstr_ctrl *swrm;
1914
1915 swrm = container_of(work, struct swr_mstr_ctrl,
1916 wakeup_work);
1917 if (!swrm || !(swrm->dev)) {
1918 pr_err("%s: swrm or dev is null\n", __func__);
1919 return;
1920 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301921
1922 mutex_lock(&swrm->devlock);
1923 if (!swrm->dev_up) {
1924 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301925 goto exit;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301926 }
1927 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301928 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1929 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1930 goto exit;
1931 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301932 pm_runtime_get_sync(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301933 pm_runtime_mark_last_busy(swrm->dev);
1934 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301935 swrm_unlock_sleep(swrm);
1936exit:
1937 pm_relax(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301938}
1939
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301940static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
1941{
1942 u32 val;
1943
1944 swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1945 val = (swrm->slave_status >> (devnum * 2));
1946 val &= SWRM_MCP_SLV_STATUS_MASK;
1947 return val;
1948}
1949
1950static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
1951 u8 *dev_num)
1952{
1953 int i;
1954 u64 id = 0;
1955 int ret = -EINVAL;
1956 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1957 struct swr_device *swr_dev;
1958 u32 num_dev = 0;
1959
1960 if (!swrm) {
1961 pr_err("%s: Invalid handle to swr controller\n",
1962 __func__);
1963 return ret;
1964 }
1965 if (swrm->num_dev)
1966 num_dev = swrm->num_dev;
1967 else
1968 num_dev = mstr->num_dev;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301969
1970 mutex_lock(&swrm->devlock);
1971 if (!swrm->dev_up) {
1972 mutex_unlock(&swrm->devlock);
1973 return ret;
1974 }
1975 mutex_unlock(&swrm->devlock);
1976
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301977 pm_runtime_get_sync(swrm->dev);
1978 for (i = 1; i < (num_dev + 1); i++) {
1979 id = ((u64)(swr_master_read(swrm,
1980 SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
1981 id |= swr_master_read(swrm,
1982 SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301983
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301984 /*
1985 * As pm_runtime_get_sync() brings all slaves out of reset
1986 * update logical device number for all slaves.
1987 */
1988 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1989 if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
1990 u32 status = swrm_get_device_status(swrm, i);
1991
1992 if ((status == 0x01) || (status == 0x02)) {
1993 swr_dev->dev_num = i;
1994 if ((id & SWR_DEV_ID_MASK) == dev_id) {
1995 *dev_num = i;
1996 ret = 0;
1997 }
1998 dev_dbg(swrm->dev,
1999 "%s: devnum %d is assigned for dev addr %lx\n",
2000 __func__, i, swr_dev->addr);
2001 }
2002 }
2003 }
2004 }
2005 if (ret)
2006 dev_err(swrm->dev, "%s: device 0x%llx is not ready\n",
2007 __func__, dev_id);
2008
2009 pm_runtime_mark_last_busy(swrm->dev);
2010 pm_runtime_put_autosuspend(swrm->dev);
2011 return ret;
2012}
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302013
2014static void swrm_device_wakeup_vote(struct swr_master *mstr)
2015{
2016 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
2017
2018 if (!swrm) {
2019 pr_err("%s: Invalid handle to swr controller\n",
2020 __func__);
2021 return;
2022 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302023 if (unlikely(swrm_lock_sleep(swrm) == false)) {
2024 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
2025 return;
2026 }
Sudheer Papothi384addd2019-06-14 02:26:52 +05302027 if (++swrm->hw_core_clk_en == 1)
2028 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2029 dev_err(swrm->dev, "%s:lpass core hw enable failed\n",
2030 __func__);
2031 --swrm->hw_core_clk_en;
2032 }
2033 if ( ++swrm->aud_core_clk_en == 1)
2034 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2035 dev_err(swrm->dev, "%s:lpass audio hw enable failed\n",
2036 __func__);
2037 --swrm->aud_core_clk_en;
2038 }
2039 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
2040 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302041 pm_runtime_get_sync(swrm->dev);
2042}
2043
2044static void swrm_device_wakeup_unvote(struct swr_master *mstr)
2045{
2046 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
2047
2048 if (!swrm) {
2049 pr_err("%s: Invalid handle to swr controller\n",
2050 __func__);
2051 return;
2052 }
2053 pm_runtime_mark_last_busy(swrm->dev);
2054 pm_runtime_put_autosuspend(swrm->dev);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302055 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
2056 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
2057
2058 --swrm->aud_core_clk_en;
2059 if (swrm->aud_core_clk_en < 0)
2060 swrm->aud_core_clk_en = 0;
2061 else if (swrm->aud_core_clk_en == 0)
2062 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2063
2064 --swrm->hw_core_clk_en;
2065 if (swrm->hw_core_clk_en < 0)
2066 swrm->hw_core_clk_en = 0;
2067 else if (swrm->hw_core_clk_en == 0)
2068 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
2069
Ramprasad Katkam57349872018-11-11 18:34:57 +05302070 swrm_unlock_sleep(swrm);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302071}
2072
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302073static int swrm_master_init(struct swr_mstr_ctrl *swrm)
2074{
2075 int ret = 0;
2076 u32 val;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302077 u8 row_ctrl = SWR_ROW_50;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302078 u8 col_ctrl = SWR_MIN_COL;
2079 u8 ssp_period = 1;
2080 u8 retry_cmd_num = 3;
2081 u32 reg[SWRM_MAX_INIT_REG];
2082 u32 value[SWRM_MAX_INIT_REG];
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302083 u32 temp = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302084 int len = 0;
2085
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302086 ssp_period = swrm_get_ssp_period(swrm, SWRM_ROW_50,
2087 SWRM_COL_02, SWRM_FRAME_SYNC_SEL);
2088 dev_dbg(swrm->dev, "%s: ssp_period: %d\n", __func__, ssp_period);
2089
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302090 /* Clear Rows and Cols */
2091 val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
2092 (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302093 ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302094
2095 reg[len] = SWRM_MCP_FRAME_CTRL_BANK_ADDR(0);
2096 value[len++] = val;
2097
2098 /* Set Auto enumeration flag */
2099 reg[len] = SWRM_ENUMERATOR_CFG_ADDR;
2100 value[len++] = 1;
2101
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302102 /* Configure No pings */
2103 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2104 val &= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK;
2105 val |= (0x1f << SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT);
2106 reg[len] = SWRM_MCP_CFG_ADDR;
2107 value[len++] = val;
2108
2109 /* Configure number of retries of a read/write cmd */
2110 val = (retry_cmd_num << SWRM_CMD_FIFO_CFG_NUM_OF_CMD_RETRY_SHFT);
2111 reg[len] = SWRM_CMD_FIFO_CFG_ADDR;
2112 value[len++] = val;
2113
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302114 reg[len] = SWRM_MCP_BUS_CTRL_ADDR;
2115 value[len++] = 0x2;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302116
Ramprasad Katkam83303512018-10-11 17:34:22 +05302117 /* Set IRQ to PULSE */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302118 reg[len] = SWRM_COMP_CFG_ADDR;
Ramprasad Katkam83303512018-10-11 17:34:22 +05302119 value[len++] = 0x02;
2120
2121 reg[len] = SWRM_COMP_CFG_ADDR;
2122 value[len++] = 0x03;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302123
2124 reg[len] = SWRM_INTERRUPT_CLEAR;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302125 value[len++] = 0xFFFFFFFF;
2126
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302127 swrm->intr_mask = SWRM_INTERRUPT_STATUS_MASK;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302128 /* Mask soundwire interrupts */
2129 reg[len] = SWRM_INTERRUPT_MASK_ADDR;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302130 value[len++] = swrm->intr_mask;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302131
2132 reg[len] = SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302133 value[len++] = swrm->intr_mask;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302134
2135 swr_master_bulk_write(swrm, reg, value, len);
2136
Sudheer Papothi63f48152018-11-15 01:08:03 +05302137 /*
2138 * For SWR master version 1.5.1, continue
2139 * execute on command ignore.
2140 */
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302141 /* Execute it for versions >= 1.5.1 */
2142 if (swrm->version >= SWRM_VERSION_1_5_1)
Sudheer Papothi63f48152018-11-15 01:08:03 +05302143 swr_master_write(swrm, SWRM_CMD_FIFO_CFG_ADDR,
2144 (swr_master_read(swrm,
2145 SWRM_CMD_FIFO_CFG_ADDR) | 0x80000000));
2146
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302147 /* SW workaround to gate hw_ctl for SWR version >=1.6 */
2148 if (swrm->version >= SWRM_VERSION_1_6) {
2149 if (swrm->swrm_hctl_reg) {
2150 temp = ioread32(swrm->swrm_hctl_reg);
2151 temp &= 0xFFFFFFFD;
2152 iowrite32(temp, swrm->swrm_hctl_reg);
2153 }
2154 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302155 return ret;
2156}
2157
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302158static int swrm_event_notify(struct notifier_block *self,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302159 unsigned long action, void *data)
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302160{
2161 struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302162 event_notifier);
2163
2164 if (!swrm || !(swrm->dev)) {
2165 pr_err("%s: swrm or dev is NULL\n", __func__);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302166 return -EINVAL;
2167 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302168 switch (action) {
2169 case MSM_AUD_DC_EVENT:
2170 schedule_work(&(swrm->dc_presence_work));
2171 break;
2172 case SWR_WAKE_IRQ_EVENT:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302173 if (swrm->ipc_wakeup && !swrm->ipc_wakeup_triggered) {
2174 swrm->ipc_wakeup_triggered = true;
Ramprasad Katkam57349872018-11-11 18:34:57 +05302175 pm_stay_awake(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302176 schedule_work(&swrm->wakeup_work);
Ramprasad Katkamcd61c6e2018-09-18 13:22:58 +05302177 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302178 break;
2179 default:
2180 dev_err(swrm->dev, "%s: invalid event type: %lu\n",
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302181 __func__, action);
2182 return -EINVAL;
2183 }
2184
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302185 return 0;
2186}
2187
2188static void swrm_notify_work_fn(struct work_struct *work)
2189{
2190 struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302191 dc_presence_work);
2192
2193 if (!swrm || !swrm->pdev) {
2194 pr_err("%s: swrm or pdev is NULL\n", __func__);
2195 return;
2196 }
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302197 swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL);
2198}
2199
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302200static int swrm_probe(struct platform_device *pdev)
2201{
2202 struct swr_mstr_ctrl *swrm;
2203 struct swr_ctrl_platform_data *pdata;
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302204 u32 i, num_ports, port_num, port_type, ch_mask, swrm_hctl_reg = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302205 u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0;
2206 int ret = 0;
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302207 struct clk *lpass_core_hw_vote = NULL;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302208 struct clk *lpass_core_audio = NULL;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302209
2210 /* Allocate soundwire master driver structure */
2211 swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl),
2212 GFP_KERNEL);
2213 if (!swrm) {
2214 ret = -ENOMEM;
2215 goto err_memory_fail;
2216 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302217 swrm->pdev = pdev;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302218 swrm->dev = &pdev->dev;
2219 platform_set_drvdata(pdev, swrm);
2220 swr_set_ctrl_data(&swrm->master, swrm);
2221 pdata = dev_get_platdata(&pdev->dev);
2222 if (!pdata) {
2223 dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
2224 __func__);
2225 ret = -EINVAL;
2226 goto err_pdata_fail;
2227 }
2228 swrm->handle = (void *)pdata->handle;
2229 if (!swrm->handle) {
2230 dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
2231 __func__);
2232 ret = -EINVAL;
2233 goto err_pdata_fail;
2234 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302235 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr_master_id",
2236 &swrm->master_id);
2237 if (ret) {
2238 dev_err(&pdev->dev, "%s: failed to get master id\n", __func__);
2239 goto err_pdata_fail;
2240 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302241 if (!(of_property_read_u32(pdev->dev.of_node,
2242 "swrm-io-base", &swrm->swrm_base_reg)))
2243 ret = of_property_read_u32(pdev->dev.of_node,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302244 "swrm-io-base", &swrm->swrm_base_reg);
2245 if (!swrm->swrm_base_reg) {
2246 swrm->read = pdata->read;
2247 if (!swrm->read) {
2248 dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
2249 __func__);
2250 ret = -EINVAL;
2251 goto err_pdata_fail;
2252 }
2253 swrm->write = pdata->write;
2254 if (!swrm->write) {
2255 dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
2256 __func__);
2257 ret = -EINVAL;
2258 goto err_pdata_fail;
2259 }
2260 swrm->bulk_write = pdata->bulk_write;
2261 if (!swrm->bulk_write) {
2262 dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
2263 __func__);
2264 ret = -EINVAL;
2265 goto err_pdata_fail;
2266 }
2267 } else {
2268 swrm->swrm_dig_base = devm_ioremap(&pdev->dev,
2269 swrm->swrm_base_reg, SWRM_MAX_REGISTER);
2270 }
2271
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07002272 swrm->core_vote = pdata->core_vote;
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302273 if (!(of_property_read_u32(pdev->dev.of_node,
2274 "qcom,swrm-hctl-reg", &swrm_hctl_reg)))
2275 swrm->swrm_hctl_reg = devm_ioremap(&pdev->dev,
2276 swrm_hctl_reg, 0x4);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302277 swrm->clk = pdata->clk;
2278 if (!swrm->clk) {
2279 dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
2280 __func__);
2281 ret = -EINVAL;
2282 goto err_pdata_fail;
2283 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302284 if (of_property_read_u32(pdev->dev.of_node,
2285 "qcom,swr-clock-stop-mode0",
2286 &swrm->clk_stop_mode0_supp)) {
2287 swrm->clk_stop_mode0_supp = FALSE;
2288 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302289
2290 ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
2291 &swrm->num_dev);
2292 if (ret) {
2293 dev_dbg(&pdev->dev, "%s: Looking up %s property failed\n",
2294 __func__, "qcom,swr-num-dev");
2295 } else {
2296 if (swrm->num_dev > SWR_MAX_SLAVE_DEVICES) {
2297 dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
2298 __func__, swrm->num_dev, SWR_MAX_SLAVE_DEVICES);
2299 ret = -EINVAL;
2300 goto err_pdata_fail;
2301 }
2302 }
2303
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302304 /* Parse soundwire port mapping */
2305 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
2306 &num_ports);
2307 if (ret) {
2308 dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
2309 goto err_pdata_fail;
2310 }
2311 swrm->num_ports = num_ports;
2312
2313 if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
2314 &map_size)) {
2315 dev_err(swrm->dev, "missing port mapping\n");
2316 goto err_pdata_fail;
2317 }
2318
2319 map_length = map_size / (3 * sizeof(u32));
2320 if (num_ports > SWR_MSTR_PORT_LEN) {
2321 dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
2322 __func__);
2323 ret = -EINVAL;
2324 goto err_pdata_fail;
2325 }
2326 temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
2327
2328 if (!temp) {
2329 ret = -ENOMEM;
2330 goto err_pdata_fail;
2331 }
2332 ret = of_property_read_u32_array(pdev->dev.of_node,
2333 "qcom,swr-port-mapping", temp, 3 * map_length);
2334 if (ret) {
2335 dev_err(swrm->dev, "%s: Failed to read port mapping\n",
2336 __func__);
2337 goto err_pdata_fail;
2338 }
2339
2340 for (i = 0; i < map_length; i++) {
2341 port_num = temp[3 * i];
2342 port_type = temp[3 * i + 1];
2343 ch_mask = temp[3 * i + 2];
2344
2345 if (port_num != old_port_num)
2346 ch_iter = 0;
2347 swrm->port_mapping[port_num][ch_iter].port_type = port_type;
2348 swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
2349 old_port_num = port_num;
2350 }
2351 devm_kfree(&pdev->dev, temp);
2352
2353 swrm->reg_irq = pdata->reg_irq;
2354 swrm->master.read = swrm_read;
2355 swrm->master.write = swrm_write;
2356 swrm->master.bulk_write = swrm_bulk_write;
2357 swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
2358 swrm->master.connect_port = swrm_connect_port;
2359 swrm->master.disconnect_port = swrm_disconnect_port;
2360 swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
2361 swrm->master.remove_from_group = swrm_remove_from_group;
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302362 swrm->master.device_wakeup_vote = swrm_device_wakeup_vote;
2363 swrm->master.device_wakeup_unvote = swrm_device_wakeup_unvote;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302364 swrm->master.dev.parent = &pdev->dev;
2365 swrm->master.dev.of_node = pdev->dev.of_node;
2366 swrm->master.num_port = 0;
2367 swrm->rcmd_id = 0;
2368 swrm->wcmd_id = 0;
2369 swrm->slave_status = 0;
2370 swrm->num_rx_chs = 0;
2371 swrm->clk_ref_count = 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302372 swrm->swr_irq_wakeup_capable = 0;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302373 swrm->mclk_freq = MCLK_FREQ;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302374 swrm->bus_clk = MCLK_FREQ;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302375 swrm->dev_up = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302376 swrm->state = SWR_MSTR_UP;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302377 swrm->ipc_wakeup = false;
2378 swrm->ipc_wakeup_triggered = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302379 init_completion(&swrm->reset);
2380 init_completion(&swrm->broadcast);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302381 init_completion(&swrm->clk_off_complete);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302382 mutex_init(&swrm->mlock);
2383 mutex_init(&swrm->reslock);
2384 mutex_init(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302385 mutex_init(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302386 mutex_init(&swrm->clklock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302387 mutex_init(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302388 mutex_init(&swrm->pm_lock);
2389 swrm->wlock_holders = 0;
2390 swrm->pm_state = SWRM_PM_SLEEPABLE;
2391 init_waitqueue_head(&swrm->pm_wq);
2392 pm_qos_add_request(&swrm->pm_qos_req,
2393 PM_QOS_CPU_DMA_LATENCY,
2394 PM_QOS_DEFAULT_VALUE);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302395
2396 for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++)
2397 INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
2398
Sudheer Papothi06f43412019-07-09 03:32:54 +05302399 /* Register LPASS core hw vote */
2400 lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
2401 if (IS_ERR(lpass_core_hw_vote)) {
2402 ret = PTR_ERR(lpass_core_hw_vote);
2403 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2404 __func__, "lpass_core_hw_vote", ret);
2405 lpass_core_hw_vote = NULL;
2406 ret = 0;
2407 }
2408 swrm->lpass_core_hw_vote = lpass_core_hw_vote;
2409
2410 /* Register LPASS audio core vote */
2411 lpass_core_audio = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
2412 if (IS_ERR(lpass_core_audio)) {
2413 ret = PTR_ERR(lpass_core_audio);
2414 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2415 __func__, "lpass_core_audio", ret);
2416 lpass_core_audio = NULL;
2417 ret = 0;
2418 }
2419 swrm->lpass_core_audio = lpass_core_audio;
2420
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302421 if (swrm->reg_irq) {
2422 ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
2423 SWR_IRQ_REGISTER);
2424 if (ret) {
2425 dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
2426 __func__, ret);
2427 goto err_irq_fail;
2428 }
2429 } else {
2430 swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
2431 if (swrm->irq < 0) {
2432 dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
2433 __func__, swrm->irq);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302434 goto err_irq_fail;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302435 }
2436
2437 ret = request_threaded_irq(swrm->irq, NULL,
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302438 swr_mstr_interrupt_v2,
Ramprasad Katkam83303512018-10-11 17:34:22 +05302439 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302440 "swr_master_irq", swrm);
2441 if (ret) {
2442 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2443 __func__, ret);
2444 goto err_irq_fail;
2445 }
2446
2447 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302448 /* Make inband tx interrupts as wakeup capable for slave irq */
2449 ret = of_property_read_u32(pdev->dev.of_node,
2450 "qcom,swr-mstr-irq-wakeup-capable",
2451 &swrm->swr_irq_wakeup_capable);
2452 if (ret)
2453 dev_dbg(swrm->dev, "%s: swrm irq wakeup capable not defined\n",
2454 __func__);
2455 if (swrm->swr_irq_wakeup_capable)
2456 irq_set_irq_wake(swrm->irq, 1);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302457 ret = swr_register_master(&swrm->master);
2458 if (ret) {
2459 dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
2460 goto err_mstr_fail;
2461 }
2462
2463 /* Add devices registered with board-info as the
2464 * controller will be up now
2465 */
2466 swr_master_add_boarddevices(&swrm->master);
2467 mutex_lock(&swrm->mlock);
2468 swrm_clk_request(swrm, true);
2469 ret = swrm_master_init(swrm);
2470 if (ret < 0) {
2471 dev_err(&pdev->dev,
2472 "%s: Error in master Initialization , err %d\n",
2473 __func__, ret);
2474 mutex_unlock(&swrm->mlock);
2475 goto err_mstr_fail;
2476 }
2477 swrm->version = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
2478
2479 mutex_unlock(&swrm->mlock);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302480 INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302481
2482 if (pdev->dev.of_node)
2483 of_register_swr_devices(&swrm->master);
2484
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302485#ifdef CONFIG_DEBUG_FS
2486 swrm->debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
2487 if (!IS_ERR(swrm->debugfs_swrm_dent)) {
2488 swrm->debugfs_peek = debugfs_create_file("swrm_peek",
2489 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2490 (void *) swrm, &swrm_debug_read_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302491
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302492 swrm->debugfs_poke = debugfs_create_file("swrm_poke",
2493 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2494 (void *) swrm, &swrm_debug_write_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302495
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302496 swrm->debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
2497 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2498 (void *) swrm,
2499 &swrm_debug_dump_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302500 }
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302501#endif
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302502 ret = device_init_wakeup(swrm->dev, true);
2503 if (ret) {
2504 dev_err(swrm->dev, "Device wakeup init failed: %d\n", ret);
2505 goto err_irq_wakeup_fail;
2506 }
2507
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302508 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
2509 pm_runtime_use_autosuspend(&pdev->dev);
2510 pm_runtime_set_active(&pdev->dev);
2511 pm_runtime_enable(&pdev->dev);
2512 pm_runtime_mark_last_busy(&pdev->dev);
2513
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302514 INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn);
2515 swrm->event_notifier.notifier_call = swrm_event_notify;
2516 msm_aud_evt_register_client(&swrm->event_notifier);
2517
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302518 return 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302519err_irq_wakeup_fail:
2520 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302521err_mstr_fail:
2522 if (swrm->reg_irq)
2523 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2524 swrm, SWR_IRQ_FREE);
2525 else if (swrm->irq)
2526 free_irq(swrm->irq, swrm);
2527err_irq_fail:
2528 mutex_destroy(&swrm->mlock);
2529 mutex_destroy(&swrm->reslock);
2530 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302531 mutex_destroy(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302532 mutex_destroy(&swrm->clklock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302533 mutex_destroy(&swrm->pm_lock);
2534 pm_qos_remove_request(&swrm->pm_qos_req);
2535
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302536err_pdata_fail:
2537err_memory_fail:
2538 return ret;
2539}
2540
2541static int swrm_remove(struct platform_device *pdev)
2542{
2543 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2544
2545 if (swrm->reg_irq)
2546 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2547 swrm, SWR_IRQ_FREE);
2548 else if (swrm->irq)
2549 free_irq(swrm->irq, swrm);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302550 else if (swrm->wake_irq > 0)
2551 free_irq(swrm->wake_irq, swrm);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302552 if (swrm->swr_irq_wakeup_capable)
2553 irq_set_irq_wake(swrm->irq, 0);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302554 cancel_work_sync(&swrm->wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302555 pm_runtime_disable(&pdev->dev);
2556 pm_runtime_set_suspended(&pdev->dev);
2557 swr_unregister_master(&swrm->master);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302558 msm_aud_evt_unregister_client(&swrm->event_notifier);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302559 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302560 mutex_destroy(&swrm->mlock);
2561 mutex_destroy(&swrm->reslock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302562 mutex_destroy(&swrm->iolock);
2563 mutex_destroy(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302564 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302565 mutex_destroy(&swrm->pm_lock);
2566 pm_qos_remove_request(&swrm->pm_qos_req);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302567 devm_kfree(&pdev->dev, swrm);
2568 return 0;
2569}
2570
2571static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
2572{
2573 u32 val;
2574
2575 dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
2576 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD);
2577 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2578 val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK;
2579 swr_master_write(swrm, SWRM_MCP_CFG_ADDR, val);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302580
2581 return 0;
2582}
2583
2584#ifdef CONFIG_PM
2585static int swrm_runtime_resume(struct device *dev)
2586{
2587 struct platform_device *pdev = to_platform_device(dev);
2588 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2589 int ret = 0;
Vatsal Buchae50b5002019-09-19 14:32:20 +05302590 bool swrm_clk_req_err = false;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302591 bool hw_core_err = false;
2592 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302593 struct swr_master *mstr = &swrm->master;
2594 struct swr_device *swr_dev;
2595
2596 dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
2597 __func__, swrm->state);
2598 mutex_lock(&swrm->reslock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302599
Sudheer Papothi384addd2019-06-14 02:26:52 +05302600 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2601 dev_err(dev, "%s:lpass core hw enable failed\n",
2602 __func__);
2603 hw_core_err = true;
2604 }
2605 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2606 dev_err(dev, "%s:lpass audio hw enable failed\n",
2607 __func__);
2608 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002609 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302610
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302611 if ((swrm->state == SWR_MSTR_DOWN) ||
2612 (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) {
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302613 if (swrm->clk_stop_mode0_supp) {
2614 if (swrm->ipc_wakeup)
2615 msm_aud_evt_blocking_notifier_call_chain(
2616 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302617 }
2618
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302619 if (swrm_clk_request(swrm, true)) {
2620 /*
2621 * Set autosuspend timer to 1 for
2622 * master to enter into suspend.
2623 */
Vatsal Buchae50b5002019-09-19 14:32:20 +05302624 swrm_clk_req_err = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302625 goto exit;
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302626 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302627 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302628 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2629 ret = swr_device_up(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302630 if (ret == -ENODEV) {
2631 dev_dbg(dev,
2632 "%s slave device up not implemented\n",
2633 __func__);
2634 ret = 0;
2635 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302636 dev_err(dev,
2637 "%s: failed to wakeup swr dev %d\n",
2638 __func__, swr_dev->dev_num);
2639 swrm_clk_request(swrm, false);
2640 goto exit;
2641 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302642 }
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05302643 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2644 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2645 swrm_master_init(swrm);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302646 /* wait for hw enumeration to complete */
2647 usleep_range(100, 105);
Laxminath Kasame2291972019-11-08 14:51:59 +05302648 if (!swrm_check_link_status(swrm, 0x1))
2649 goto exit;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302650 swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, 0x0,
2651 SWRS_SCP_INT_STATUS_MASK_1);
Karthikeyan Manif6821902019-05-21 17:31:24 -07002652 if (swrm->state == SWR_MSTR_SSR) {
2653 mutex_unlock(&swrm->reslock);
2654 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
2655 mutex_lock(&swrm->reslock);
2656 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302657 } else {
2658 /*wake up from clock stop*/
2659 swr_master_write(swrm, SWRM_MCP_BUS_CTRL_ADDR, 0x2);
2660 usleep_range(100, 105);
Laxminath Kasame2291972019-11-08 14:51:59 +05302661 if (!swrm_check_link_status(swrm, 0x1))
2662 goto exit;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302663 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302664 swrm->state = SWR_MSTR_UP;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302665 }
2666exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302667 if (!aud_core_err)
2668 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2669 if (!hw_core_err)
2670 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Vatsal Buchae50b5002019-09-19 14:32:20 +05302671 if (swrm_clk_req_err)
2672 pm_runtime_set_autosuspend_delay(&pdev->dev,
2673 ERR_AUTO_SUSPEND_TIMER_VAL);
2674 else
2675 pm_runtime_set_autosuspend_delay(&pdev->dev,
2676 auto_suspend_timer);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302677 mutex_unlock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302678
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302679 return ret;
2680}
2681
2682static int swrm_runtime_suspend(struct device *dev)
2683{
2684 struct platform_device *pdev = to_platform_device(dev);
2685 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2686 int ret = 0;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302687 bool hw_core_err = false;
2688 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302689 struct swr_master *mstr = &swrm->master;
2690 struct swr_device *swr_dev;
2691 int current_state = 0;
2692
2693 dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
2694 __func__, swrm->state);
2695 mutex_lock(&swrm->reslock);
2696 mutex_lock(&swrm->force_down_lock);
2697 current_state = swrm->state;
2698 mutex_unlock(&swrm->force_down_lock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302699
2700 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2701 dev_err(dev, "%s:lpass core hw enable failed\n",
2702 __func__);
2703 hw_core_err = true;
2704 }
2705 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2706 dev_err(dev, "%s:lpass audio hw enable failed\n",
2707 __func__);
2708 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002709 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302710
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302711 if ((current_state == SWR_MSTR_UP) ||
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302712 (current_state == SWR_MSTR_SSR)) {
2713
2714 if ((current_state != SWR_MSTR_SSR) &&
2715 swrm_is_port_en(&swrm->master)) {
2716 dev_dbg(dev, "%s ports are enabled\n", __func__);
2717 ret = -EBUSY;
2718 goto exit;
2719 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302720 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05302721 mutex_unlock(&swrm->reslock);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +05302722 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
Sudheer Papothi06f43412019-07-09 03:32:54 +05302723 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302724 swrm_clk_pause(swrm);
2725 swr_master_write(swrm, SWRM_COMP_CFG_ADDR, 0x00);
2726 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2727 ret = swr_device_down(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302728 if (ret == -ENODEV) {
2729 dev_dbg_ratelimited(dev,
2730 "%s slave device down not implemented\n",
2731 __func__);
2732 ret = 0;
2733 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302734 dev_err(dev,
2735 "%s: failed to shutdown swr dev %d\n",
2736 __func__, swr_dev->dev_num);
2737 goto exit;
2738 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302739 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302740 } else {
Sudheer Papothi384addd2019-06-14 02:26:52 +05302741 mutex_unlock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302742 /* clock stop sequence */
2743 swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
2744 SWRS_SCP_CONTROL);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302745 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302746 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302747 }
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07002748 ret = swrm_clk_request(swrm, false);
2749 if (ret) {
2750 dev_err(dev, "%s: swrmn clk failed\n", __func__);
2751 ret = 0;
2752 goto exit;
2753 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302754
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302755 if (swrm->clk_stop_mode0_supp) {
2756 if (swrm->wake_irq > 0) {
2757 enable_irq(swrm->wake_irq);
2758 } else if (swrm->ipc_wakeup) {
2759 msm_aud_evt_blocking_notifier_call_chain(
2760 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
2761 swrm->ipc_wakeup_triggered = false;
2762 }
Laxminath Kasame2291972019-11-08 14:51:59 +05302763 if (!swrm_check_link_status(swrm, 0x0))
2764 goto exit;
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302765 }
2766
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302767 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302768 /* Retain SSR state until resume */
2769 if (current_state != SWR_MSTR_SSR)
2770 swrm->state = SWR_MSTR_DOWN;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302771exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302772 if (!aud_core_err)
2773 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2774 if (!hw_core_err)
2775 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302776 mutex_unlock(&swrm->reslock);
2777 return ret;
2778}
2779#endif /* CONFIG_PM */
2780
Sudheer Papothi06f43412019-07-09 03:32:54 +05302781static int swrm_device_suspend(struct device *dev)
2782{
2783 struct platform_device *pdev = to_platform_device(dev);
2784 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2785 int ret = 0;
2786
2787 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2788 if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
2789 ret = swrm_runtime_suspend(dev);
2790 if (!ret) {
2791 pm_runtime_disable(dev);
2792 pm_runtime_set_suspended(dev);
2793 pm_runtime_enable(dev);
2794 }
2795 }
2796
2797 return 0;
2798}
2799
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302800static int swrm_device_down(struct device *dev)
2801{
2802 struct platform_device *pdev = to_platform_device(dev);
2803 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302804
2805 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2806
2807 mutex_lock(&swrm->force_down_lock);
2808 swrm->state = SWR_MSTR_SSR;
2809 mutex_unlock(&swrm->force_down_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302810
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302811 swrm_device_suspend(dev);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302812 return 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302813}
2814
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302815int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm)
2816{
2817 int ret = 0;
Laxminath Kasama60239e2019-01-10 14:43:03 +05302818 int irq, dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302819
2820 if (!swrm->ipc_wakeup) {
Laxminath Kasama60239e2019-01-10 14:43:03 +05302821 irq = of_get_named_gpio(swrm->dev->of_node,
2822 "qcom,swr-wakeup-irq", 0);
2823 if (gpio_is_valid(irq)) {
2824 swrm->wake_irq = gpio_to_irq(irq);
2825 if (swrm->wake_irq < 0) {
2826 dev_err(swrm->dev,
2827 "Unable to configure irq\n");
2828 return swrm->wake_irq;
2829 }
2830 } else {
2831 dir_apps_irq = platform_get_irq_byname(swrm->pdev,
2832 "swr_wake_irq");
2833 if (dir_apps_irq < 0) {
2834 dev_err(swrm->dev,
2835 "TLMM connect gpio not found\n");
2836 return -EINVAL;
2837 }
2838 swrm->wake_irq = dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302839 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302840 ret = request_threaded_irq(swrm->wake_irq, NULL,
2841 swrm_wakeup_interrupt,
2842 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
2843 "swr_wake_irq", swrm);
2844 if (ret) {
2845 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2846 __func__, ret);
2847 return -EINVAL;
2848 }
Aditya Bavanari3517b112018-12-03 13:26:59 +05302849 irq_set_irq_wake(swrm->wake_irq, 1);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302850 }
2851 return ret;
2852}
2853
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302854static int swrm_alloc_port_mem(struct device *dev, struct swr_mstr_ctrl *swrm,
2855 u32 uc, u32 size)
2856{
2857 if (!swrm->port_param) {
2858 swrm->port_param = devm_kzalloc(dev,
2859 sizeof(swrm->port_param) * SWR_UC_MAX,
2860 GFP_KERNEL);
2861 if (!swrm->port_param)
2862 return -ENOMEM;
2863 }
2864 if (!swrm->port_param[uc]) {
2865 swrm->port_param[uc] = devm_kcalloc(dev, size,
2866 sizeof(struct port_params),
2867 GFP_KERNEL);
2868 if (!swrm->port_param[uc])
2869 return -ENOMEM;
2870 } else {
2871 dev_err_ratelimited(swrm->dev, "%s: called more than once\n",
2872 __func__);
2873 }
2874
2875 return 0;
2876}
2877
2878static int swrm_copy_port_config(struct swr_mstr_ctrl *swrm,
2879 struct swrm_port_config *port_cfg,
2880 u32 size)
2881{
2882 int idx;
2883 struct port_params *params;
2884 int uc = port_cfg->uc;
2885 int ret = 0;
2886
2887 for (idx = 0; idx < size; idx++) {
2888 params = &((struct port_params *)port_cfg->params)[idx];
2889 if (!params) {
2890 dev_err(swrm->dev, "%s: Invalid params\n", __func__);
2891 ret = -EINVAL;
2892 break;
2893 }
2894 memcpy(&swrm->port_param[uc][idx], params,
2895 sizeof(struct port_params));
2896 }
2897
2898 return ret;
2899}
2900
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302901/**
2902 * swrm_wcd_notify - parent device can notify to soundwire master through
2903 * this function
2904 * @pdev: pointer to platform device structure
2905 * @id: command id from parent to the soundwire master
2906 * @data: data from parent device to soundwire master
2907 */
2908int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
2909{
2910 struct swr_mstr_ctrl *swrm;
2911 int ret = 0;
2912 struct swr_master *mstr;
2913 struct swr_device *swr_dev;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302914 struct swrm_port_config *port_cfg;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302915
2916 if (!pdev) {
2917 pr_err("%s: pdev is NULL\n", __func__);
2918 return -EINVAL;
2919 }
2920 swrm = platform_get_drvdata(pdev);
2921 if (!swrm) {
2922 dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__);
2923 return -EINVAL;
2924 }
2925 mstr = &swrm->master;
2926
2927 switch (id) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05302928 case SWR_REQ_CLK_SWITCH:
2929 /* This will put soundwire in clock stop mode and disable the
2930 * clocks, if there is no active usecase running, so that the
2931 * next activity on soundwire will request clock from new clock
2932 * source.
2933 */
2934 mutex_lock(&swrm->mlock);
2935 if (swrm->state == SWR_MSTR_UP)
2936 swrm_device_suspend(&pdev->dev);
2937 mutex_unlock(&swrm->mlock);
2938 break;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302939 case SWR_CLK_FREQ:
2940 if (!data) {
2941 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
2942 ret = -EINVAL;
2943 } else {
2944 mutex_lock(&swrm->mlock);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302945 if (swrm->mclk_freq != *(int *)data) {
2946 dev_dbg(swrm->dev, "%s: freq change: force mstr down\n", __func__);
2947 if (swrm->state == SWR_MSTR_DOWN)
2948 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
2949 __func__, swrm->state);
2950 else
2951 swrm_device_suspend(&pdev->dev);
Prasad Kumpatla386df4e2019-10-11 18:23:16 +05302952 /*
2953 * add delay to ensure clk release happen
2954 * if interrupt triggered for clk stop,
2955 * wait for it to exit
2956 */
2957 usleep_range(10000, 10500);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302958 }
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302959 swrm->mclk_freq = *(int *)data;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302960 swrm->bus_clk = swrm->mclk_freq;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302961 mutex_unlock(&swrm->mlock);
2962 }
2963 break;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302964 case SWR_DEVICE_SSR_DOWN:
2965 mutex_lock(&swrm->devlock);
2966 swrm->dev_up = false;
2967 mutex_unlock(&swrm->devlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302968 mutex_lock(&swrm->reslock);
2969 swrm->state = SWR_MSTR_SSR;
2970 mutex_unlock(&swrm->reslock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302971 break;
2972 case SWR_DEVICE_SSR_UP:
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302973 /* wait for clk voting to be zero */
Ramprasad Katkam7f6462e2018-11-06 11:51:22 +05302974 reinit_completion(&swrm->clk_off_complete);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302975 if (swrm->clk_ref_count &&
2976 !wait_for_completion_timeout(&swrm->clk_off_complete,
Ramprasad Katkamc87efeb2018-12-12 19:26:19 +05302977 msecs_to_jiffies(500)))
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302978 dev_err(swrm->dev, "%s: clock voting not zero\n",
2979 __func__);
2980
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302981 mutex_lock(&swrm->devlock);
2982 swrm->dev_up = true;
2983 mutex_unlock(&swrm->devlock);
2984 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302985 case SWR_DEVICE_DOWN:
2986 dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
2987 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302988 if (swrm->state == SWR_MSTR_DOWN)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302989 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
2990 __func__, swrm->state);
2991 else
2992 swrm_device_down(&pdev->dev);
2993 mutex_unlock(&swrm->mlock);
2994 break;
2995 case SWR_DEVICE_UP:
2996 dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
Ramprasad Katkam0fed92f2018-11-08 14:22:22 +05302997 mutex_lock(&swrm->devlock);
2998 if (!swrm->dev_up) {
2999 dev_dbg(swrm->dev, "SSR not complete yet\n");
3000 mutex_unlock(&swrm->devlock);
3001 return -EBUSY;
3002 }
3003 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303004 mutex_lock(&swrm->mlock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303005 pm_runtime_mark_last_busy(&pdev->dev);
3006 pm_runtime_get_sync(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303007 mutex_lock(&swrm->reslock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303008 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
3009 ret = swr_reset_device(swr_dev);
3010 if (ret) {
3011 dev_err(swrm->dev,
3012 "%s: failed to reset swr device %d\n",
3013 __func__, swr_dev->dev_num);
3014 swrm_clk_request(swrm, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303015 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303016 }
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303017 pm_runtime_mark_last_busy(&pdev->dev);
3018 pm_runtime_put_autosuspend(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303019 mutex_unlock(&swrm->reslock);
3020 mutex_unlock(&swrm->mlock);
3021 break;
3022 case SWR_SET_NUM_RX_CH:
3023 if (!data) {
3024 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
3025 ret = -EINVAL;
3026 } else {
3027 mutex_lock(&swrm->mlock);
3028 swrm->num_rx_chs = *(int *)data;
3029 if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
3030 list_for_each_entry(swr_dev, &mstr->devices,
3031 dev_list) {
3032 ret = swr_set_device_group(swr_dev,
3033 SWR_BROADCAST);
3034 if (ret)
3035 dev_err(swrm->dev,
3036 "%s: set num ch failed\n",
3037 __func__);
3038 }
3039 } else {
3040 list_for_each_entry(swr_dev, &mstr->devices,
3041 dev_list) {
3042 ret = swr_set_device_group(swr_dev,
3043 SWR_GROUP_NONE);
3044 if (ret)
3045 dev_err(swrm->dev,
3046 "%s: set num ch failed\n",
3047 __func__);
3048 }
3049 }
3050 mutex_unlock(&swrm->mlock);
3051 }
3052 break;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303053 case SWR_REGISTER_WAKE_IRQ:
3054 if (!data) {
3055 dev_err(swrm->dev, "%s: reg wake irq data is NULL\n",
3056 __func__);
3057 ret = -EINVAL;
3058 } else {
3059 mutex_lock(&swrm->mlock);
3060 swrm->ipc_wakeup = *(u32 *)data;
3061 ret = swrm_register_wake_irq(swrm);
3062 if (ret)
3063 dev_err(swrm->dev, "%s: register wake_irq failed\n",
3064 __func__);
3065 mutex_unlock(&swrm->mlock);
3066 }
3067 break;
Sudheer Papothi72ee2642019-08-08 05:15:17 +05303068 case SWR_REGISTER_WAKEUP:
3069 msm_aud_evt_blocking_notifier_call_chain(
3070 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
3071 break;
3072 case SWR_DEREGISTER_WAKEUP:
3073 msm_aud_evt_blocking_notifier_call_chain(
3074 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
3075 break;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05303076 case SWR_SET_PORT_MAP:
3077 if (!data) {
3078 dev_err(swrm->dev, "%s: data is NULL for id=%d\n",
3079 __func__, id);
3080 ret = -EINVAL;
3081 } else {
3082 mutex_lock(&swrm->mlock);
3083 port_cfg = (struct swrm_port_config *)data;
3084 if (!port_cfg->size) {
3085 ret = -EINVAL;
3086 goto done;
3087 }
3088 ret = swrm_alloc_port_mem(&pdev->dev, swrm,
3089 port_cfg->uc, port_cfg->size);
3090 if (!ret)
3091 swrm_copy_port_config(swrm, port_cfg,
3092 port_cfg->size);
3093done:
3094 mutex_unlock(&swrm->mlock);
3095 }
3096 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303097 default:
3098 dev_err(swrm->dev, "%s: swr master unknown id %d\n",
3099 __func__, id);
3100 break;
3101 }
3102 return ret;
3103}
3104EXPORT_SYMBOL(swrm_wcd_notify);
3105
Ramprasad Katkam57349872018-11-11 18:34:57 +05303106/*
3107 * swrm_pm_cmpxchg:
3108 * Check old state and exchange with pm new state
3109 * if old state matches with current state
3110 *
3111 * @swrm: pointer to wcd core resource
3112 * @o: pm old state
3113 * @n: pm new state
3114 *
3115 * Returns old state
3116 */
3117static enum swrm_pm_state swrm_pm_cmpxchg(
3118 struct swr_mstr_ctrl *swrm,
3119 enum swrm_pm_state o,
3120 enum swrm_pm_state n)
3121{
3122 enum swrm_pm_state old;
3123
3124 if (!swrm)
3125 return o;
3126
3127 mutex_lock(&swrm->pm_lock);
3128 old = swrm->pm_state;
3129 if (old == o)
3130 swrm->pm_state = n;
3131 mutex_unlock(&swrm->pm_lock);
3132
3133 return old;
3134}
3135
3136static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm)
3137{
3138 enum swrm_pm_state os;
3139
3140 /*
3141 * swrm_{lock/unlock}_sleep will be called by swr irq handler
3142 * and slave wake up requests..
3143 *
3144 * If system didn't resume, we can simply return false so
3145 * IRQ handler can return without handling IRQ.
3146 */
3147 mutex_lock(&swrm->pm_lock);
3148 if (swrm->wlock_holders++ == 0) {
3149 dev_dbg(swrm->dev, "%s: holding wake lock\n", __func__);
3150 pm_qos_update_request(&swrm->pm_qos_req,
3151 msm_cpuidle_get_deep_idle_latency());
3152 pm_stay_awake(swrm->dev);
3153 }
3154 mutex_unlock(&swrm->pm_lock);
3155
3156 if (!wait_event_timeout(swrm->pm_wq,
3157 ((os = swrm_pm_cmpxchg(swrm,
3158 SWRM_PM_SLEEPABLE,
3159 SWRM_PM_AWAKE)) ==
3160 SWRM_PM_SLEEPABLE ||
3161 (os == SWRM_PM_AWAKE)),
3162 msecs_to_jiffies(
3163 SWRM_SYSTEM_RESUME_TIMEOUT_MS))) {
3164 dev_err(swrm->dev, "%s: system didn't resume within %dms, s %d, w %d\n",
3165 __func__, SWRM_SYSTEM_RESUME_TIMEOUT_MS, swrm->pm_state,
3166 swrm->wlock_holders);
3167 swrm_unlock_sleep(swrm);
3168 return false;
3169 }
3170 wake_up_all(&swrm->pm_wq);
3171 return true;
3172}
3173
3174static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm)
3175{
3176 mutex_lock(&swrm->pm_lock);
3177 if (--swrm->wlock_holders == 0) {
3178 dev_dbg(swrm->dev, "%s: releasing wake lock pm_state %d -> %d\n",
3179 __func__, swrm->pm_state, SWRM_PM_SLEEPABLE);
3180 /*
3181 * if swrm_lock_sleep failed, pm_state would be still
3182 * swrm_PM_ASLEEP, don't overwrite
3183 */
3184 if (likely(swrm->pm_state == SWRM_PM_AWAKE))
3185 swrm->pm_state = SWRM_PM_SLEEPABLE;
3186 pm_qos_update_request(&swrm->pm_qos_req,
3187 PM_QOS_DEFAULT_VALUE);
3188 pm_relax(swrm->dev);
3189 }
3190 mutex_unlock(&swrm->pm_lock);
3191 wake_up_all(&swrm->pm_wq);
3192}
3193
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303194#ifdef CONFIG_PM_SLEEP
3195static int swrm_suspend(struct device *dev)
3196{
3197 int ret = -EBUSY;
3198 struct platform_device *pdev = to_platform_device(dev);
3199 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3200
3201 dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
Ramprasad Katkam57349872018-11-11 18:34:57 +05303202
3203 mutex_lock(&swrm->pm_lock);
3204
3205 if (swrm->pm_state == SWRM_PM_SLEEPABLE) {
3206 dev_dbg(swrm->dev, "%s: suspending system, state %d, wlock %d\n",
3207 __func__, swrm->pm_state,
3208 swrm->wlock_holders);
3209 swrm->pm_state = SWRM_PM_ASLEEP;
3210 } else if (swrm->pm_state == SWRM_PM_AWAKE) {
3211 /*
3212 * unlock to wait for pm_state == SWRM_PM_SLEEPABLE
3213 * then set to SWRM_PM_ASLEEP
3214 */
3215 dev_dbg(swrm->dev, "%s: waiting to suspend system, state %d, wlock %d\n",
3216 __func__, swrm->pm_state,
3217 swrm->wlock_holders);
3218 mutex_unlock(&swrm->pm_lock);
3219 if (!(wait_event_timeout(swrm->pm_wq, swrm_pm_cmpxchg(
3220 swrm, SWRM_PM_SLEEPABLE,
3221 SWRM_PM_ASLEEP) ==
3222 SWRM_PM_SLEEPABLE,
3223 msecs_to_jiffies(
3224 SWRM_SYS_SUSPEND_WAIT)))) {
3225 dev_dbg(swrm->dev, "%s: suspend failed state %d, wlock %d\n",
3226 __func__, swrm->pm_state,
3227 swrm->wlock_holders);
3228 return -EBUSY;
3229 } else {
3230 dev_dbg(swrm->dev,
3231 "%s: done, state %d, wlock %d\n",
3232 __func__, swrm->pm_state,
3233 swrm->wlock_holders);
3234 }
3235 mutex_lock(&swrm->pm_lock);
3236 } else if (swrm->pm_state == SWRM_PM_ASLEEP) {
3237 dev_dbg(swrm->dev, "%s: system is already suspended, state %d, wlock %d\n",
3238 __func__, swrm->pm_state,
3239 swrm->wlock_holders);
3240 }
3241
3242 mutex_unlock(&swrm->pm_lock);
3243
3244 if ((!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev))) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303245 ret = swrm_runtime_suspend(dev);
3246 if (!ret) {
3247 /*
3248 * Synchronize runtime-pm and system-pm states:
3249 * At this point, we are already suspended. If
3250 * runtime-pm still thinks its active, then
3251 * make sure its status is in sync with HW
3252 * status. The three below calls let the
3253 * runtime-pm know that we are suspended
3254 * already without re-invoking the suspend
3255 * callback
3256 */
3257 pm_runtime_disable(dev);
3258 pm_runtime_set_suspended(dev);
3259 pm_runtime_enable(dev);
3260 }
3261 }
3262 if (ret == -EBUSY) {
3263 /*
3264 * There is a possibility that some audio stream is active
3265 * during suspend. We dont want to return suspend failure in
3266 * that case so that display and relevant components can still
3267 * go to suspend.
3268 * If there is some other error, then it should be passed-on
3269 * to system level suspend
3270 */
3271 ret = 0;
3272 }
3273 return ret;
3274}
3275
3276static int swrm_resume(struct device *dev)
3277{
3278 int ret = 0;
3279 struct platform_device *pdev = to_platform_device(dev);
3280 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3281
3282 dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
3283 if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
3284 ret = swrm_runtime_resume(dev);
3285 if (!ret) {
3286 pm_runtime_mark_last_busy(dev);
3287 pm_request_autosuspend(dev);
3288 }
3289 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05303290 mutex_lock(&swrm->pm_lock);
3291 if (swrm->pm_state == SWRM_PM_ASLEEP) {
3292 dev_dbg(swrm->dev,
3293 "%s: resuming system, state %d, wlock %d\n",
3294 __func__, swrm->pm_state,
3295 swrm->wlock_holders);
3296 swrm->pm_state = SWRM_PM_SLEEPABLE;
3297 } else {
3298 dev_dbg(swrm->dev, "%s: system is already awake, state %d wlock %d\n",
3299 __func__, swrm->pm_state,
3300 swrm->wlock_holders);
3301 }
3302 mutex_unlock(&swrm->pm_lock);
3303 wake_up_all(&swrm->pm_wq);
3304
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303305 return ret;
3306}
3307#endif /* CONFIG_PM_SLEEP */
3308
3309static const struct dev_pm_ops swrm_dev_pm_ops = {
3310 SET_SYSTEM_SLEEP_PM_OPS(
3311 swrm_suspend,
3312 swrm_resume
3313 )
3314 SET_RUNTIME_PM_OPS(
3315 swrm_runtime_suspend,
3316 swrm_runtime_resume,
3317 NULL
3318 )
3319};
3320
3321static const struct of_device_id swrm_dt_match[] = {
3322 {
3323 .compatible = "qcom,swr-mstr",
3324 },
3325 {}
3326};
3327
3328static struct platform_driver swr_mstr_driver = {
3329 .probe = swrm_probe,
3330 .remove = swrm_remove,
3331 .driver = {
3332 .name = SWR_WCD_NAME,
3333 .owner = THIS_MODULE,
3334 .pm = &swrm_dev_pm_ops,
3335 .of_match_table = swrm_dt_match,
Xiaojun Sang53cd13a2018-06-29 15:14:37 +08003336 .suppress_bind_attrs = true,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303337 },
3338};
3339
3340static int __init swrm_init(void)
3341{
3342 return platform_driver_register(&swr_mstr_driver);
3343}
3344module_init(swrm_init);
3345
3346static void __exit swrm_exit(void)
3347{
3348 platform_driver_unregister(&swr_mstr_driver);
3349}
3350module_exit(swrm_exit);
3351
3352MODULE_LICENSE("GPL v2");
3353MODULE_DESCRIPTION("SoundWire Master Controller");
3354MODULE_ALIAS("platform:swr-mstr");