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