blob: 39d323d18a2d3242956c5e4811c4135c74caa6c9 [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 }
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05301886
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301887 mutex_lock(&swrm->devlock);
1888 if (!swrm->dev_up) {
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05301889 if (swrm->wake_irq > 0) {
1890 if (unlikely(!irq_get_irq_data(swrm->wake_irq))) {
1891 pr_err("%s: irq data is NULL\n", __func__);
1892 mutex_unlock(&swrm->devlock);
1893 return IRQ_NONE;
1894 }
1895 mutex_lock(&swrm->irq_lock);
1896 if (!irqd_irq_disabled(
1897 irq_get_irq_data(swrm->wake_irq)))
1898 disable_irq_nosync(swrm->wake_irq);
1899 mutex_unlock(&swrm->irq_lock);
1900 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301901 mutex_unlock(&swrm->devlock);
1902 return ret;
1903 }
1904 mutex_unlock(&swrm->devlock);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301905 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1906 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1907 goto exit;
1908 }
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05301909 if (swrm->wake_irq > 0) {
1910 if (unlikely(!irq_get_irq_data(swrm->wake_irq))) {
1911 pr_err("%s: irq data is NULL\n", __func__);
1912 return IRQ_NONE;
1913 }
1914 mutex_lock(&swrm->irq_lock);
1915 if (!irqd_irq_disabled(
1916 irq_get_irq_data(swrm->wake_irq)))
1917 disable_irq_nosync(swrm->wake_irq);
1918 mutex_unlock(&swrm->irq_lock);
1919 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301920 pm_runtime_get_sync(swrm->dev);
1921 pm_runtime_mark_last_busy(swrm->dev);
1922 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301923 swrm_unlock_sleep(swrm);
1924exit:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301925 return ret;
1926}
1927
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301928static void swrm_wakeup_work(struct work_struct *work)
1929{
1930 struct swr_mstr_ctrl *swrm;
1931
1932 swrm = container_of(work, struct swr_mstr_ctrl,
1933 wakeup_work);
1934 if (!swrm || !(swrm->dev)) {
1935 pr_err("%s: swrm or dev is null\n", __func__);
1936 return;
1937 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301938
1939 mutex_lock(&swrm->devlock);
1940 if (!swrm->dev_up) {
1941 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301942 goto exit;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301943 }
1944 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301945 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1946 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1947 goto exit;
1948 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301949 pm_runtime_get_sync(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301950 pm_runtime_mark_last_busy(swrm->dev);
1951 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301952 swrm_unlock_sleep(swrm);
1953exit:
1954 pm_relax(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301955}
1956
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301957static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
1958{
1959 u32 val;
1960
1961 swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1962 val = (swrm->slave_status >> (devnum * 2));
1963 val &= SWRM_MCP_SLV_STATUS_MASK;
1964 return val;
1965}
1966
1967static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
1968 u8 *dev_num)
1969{
1970 int i;
1971 u64 id = 0;
1972 int ret = -EINVAL;
1973 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1974 struct swr_device *swr_dev;
1975 u32 num_dev = 0;
1976
1977 if (!swrm) {
1978 pr_err("%s: Invalid handle to swr controller\n",
1979 __func__);
1980 return ret;
1981 }
1982 if (swrm->num_dev)
1983 num_dev = swrm->num_dev;
1984 else
1985 num_dev = mstr->num_dev;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301986
1987 mutex_lock(&swrm->devlock);
1988 if (!swrm->dev_up) {
1989 mutex_unlock(&swrm->devlock);
1990 return ret;
1991 }
1992 mutex_unlock(&swrm->devlock);
1993
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301994 pm_runtime_get_sync(swrm->dev);
1995 for (i = 1; i < (num_dev + 1); i++) {
1996 id = ((u64)(swr_master_read(swrm,
1997 SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
1998 id |= swr_master_read(swrm,
1999 SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302000
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302001 /*
2002 * As pm_runtime_get_sync() brings all slaves out of reset
2003 * update logical device number for all slaves.
2004 */
2005 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2006 if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
2007 u32 status = swrm_get_device_status(swrm, i);
2008
2009 if ((status == 0x01) || (status == 0x02)) {
2010 swr_dev->dev_num = i;
2011 if ((id & SWR_DEV_ID_MASK) == dev_id) {
2012 *dev_num = i;
2013 ret = 0;
2014 }
2015 dev_dbg(swrm->dev,
2016 "%s: devnum %d is assigned for dev addr %lx\n",
2017 __func__, i, swr_dev->addr);
2018 }
2019 }
2020 }
2021 }
2022 if (ret)
2023 dev_err(swrm->dev, "%s: device 0x%llx is not ready\n",
2024 __func__, dev_id);
2025
2026 pm_runtime_mark_last_busy(swrm->dev);
2027 pm_runtime_put_autosuspend(swrm->dev);
2028 return ret;
2029}
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302030
2031static void swrm_device_wakeup_vote(struct swr_master *mstr)
2032{
2033 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
2034
2035 if (!swrm) {
2036 pr_err("%s: Invalid handle to swr controller\n",
2037 __func__);
2038 return;
2039 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302040 if (unlikely(swrm_lock_sleep(swrm) == false)) {
2041 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
2042 return;
2043 }
Sudheer Papothi384addd2019-06-14 02:26:52 +05302044 if (++swrm->hw_core_clk_en == 1)
2045 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2046 dev_err(swrm->dev, "%s:lpass core hw enable failed\n",
2047 __func__);
2048 --swrm->hw_core_clk_en;
2049 }
2050 if ( ++swrm->aud_core_clk_en == 1)
2051 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2052 dev_err(swrm->dev, "%s:lpass audio hw enable failed\n",
2053 __func__);
2054 --swrm->aud_core_clk_en;
2055 }
2056 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
2057 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302058 pm_runtime_get_sync(swrm->dev);
2059}
2060
2061static void swrm_device_wakeup_unvote(struct swr_master *mstr)
2062{
2063 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
2064
2065 if (!swrm) {
2066 pr_err("%s: Invalid handle to swr controller\n",
2067 __func__);
2068 return;
2069 }
2070 pm_runtime_mark_last_busy(swrm->dev);
2071 pm_runtime_put_autosuspend(swrm->dev);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302072 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
2073 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
2074
2075 --swrm->aud_core_clk_en;
2076 if (swrm->aud_core_clk_en < 0)
2077 swrm->aud_core_clk_en = 0;
2078 else if (swrm->aud_core_clk_en == 0)
2079 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2080
2081 --swrm->hw_core_clk_en;
2082 if (swrm->hw_core_clk_en < 0)
2083 swrm->hw_core_clk_en = 0;
2084 else if (swrm->hw_core_clk_en == 0)
2085 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
2086
Ramprasad Katkam57349872018-11-11 18:34:57 +05302087 swrm_unlock_sleep(swrm);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302088}
2089
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302090static int swrm_master_init(struct swr_mstr_ctrl *swrm)
2091{
2092 int ret = 0;
2093 u32 val;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302094 u8 row_ctrl = SWR_ROW_50;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302095 u8 col_ctrl = SWR_MIN_COL;
2096 u8 ssp_period = 1;
2097 u8 retry_cmd_num = 3;
2098 u32 reg[SWRM_MAX_INIT_REG];
2099 u32 value[SWRM_MAX_INIT_REG];
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302100 u32 temp = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302101 int len = 0;
2102
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302103 ssp_period = swrm_get_ssp_period(swrm, SWRM_ROW_50,
2104 SWRM_COL_02, SWRM_FRAME_SYNC_SEL);
2105 dev_dbg(swrm->dev, "%s: ssp_period: %d\n", __func__, ssp_period);
2106
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302107 /* Clear Rows and Cols */
2108 val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
2109 (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302110 ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302111
2112 reg[len] = SWRM_MCP_FRAME_CTRL_BANK_ADDR(0);
2113 value[len++] = val;
2114
2115 /* Set Auto enumeration flag */
2116 reg[len] = SWRM_ENUMERATOR_CFG_ADDR;
2117 value[len++] = 1;
2118
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302119 /* Configure No pings */
2120 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2121 val &= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK;
2122 val |= (0x1f << SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT);
2123 reg[len] = SWRM_MCP_CFG_ADDR;
2124 value[len++] = val;
2125
2126 /* Configure number of retries of a read/write cmd */
2127 val = (retry_cmd_num << SWRM_CMD_FIFO_CFG_NUM_OF_CMD_RETRY_SHFT);
2128 reg[len] = SWRM_CMD_FIFO_CFG_ADDR;
2129 value[len++] = val;
2130
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302131 reg[len] = SWRM_MCP_BUS_CTRL_ADDR;
2132 value[len++] = 0x2;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302133
Ramprasad Katkam83303512018-10-11 17:34:22 +05302134 /* Set IRQ to PULSE */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302135 reg[len] = SWRM_COMP_CFG_ADDR;
Ramprasad Katkam83303512018-10-11 17:34:22 +05302136 value[len++] = 0x02;
2137
2138 reg[len] = SWRM_COMP_CFG_ADDR;
2139 value[len++] = 0x03;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302140
2141 reg[len] = SWRM_INTERRUPT_CLEAR;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302142 value[len++] = 0xFFFFFFFF;
2143
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302144 swrm->intr_mask = SWRM_INTERRUPT_STATUS_MASK;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302145 /* Mask soundwire interrupts */
2146 reg[len] = SWRM_INTERRUPT_MASK_ADDR;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302147 value[len++] = swrm->intr_mask;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302148
2149 reg[len] = SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302150 value[len++] = swrm->intr_mask;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302151
2152 swr_master_bulk_write(swrm, reg, value, len);
2153
Sudheer Papothi63f48152018-11-15 01:08:03 +05302154 /*
2155 * For SWR master version 1.5.1, continue
2156 * execute on command ignore.
2157 */
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302158 /* Execute it for versions >= 1.5.1 */
2159 if (swrm->version >= SWRM_VERSION_1_5_1)
Sudheer Papothi63f48152018-11-15 01:08:03 +05302160 swr_master_write(swrm, SWRM_CMD_FIFO_CFG_ADDR,
2161 (swr_master_read(swrm,
2162 SWRM_CMD_FIFO_CFG_ADDR) | 0x80000000));
2163
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302164 /* SW workaround to gate hw_ctl for SWR version >=1.6 */
2165 if (swrm->version >= SWRM_VERSION_1_6) {
2166 if (swrm->swrm_hctl_reg) {
2167 temp = ioread32(swrm->swrm_hctl_reg);
2168 temp &= 0xFFFFFFFD;
2169 iowrite32(temp, swrm->swrm_hctl_reg);
2170 }
2171 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302172 return ret;
2173}
2174
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302175static int swrm_event_notify(struct notifier_block *self,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302176 unsigned long action, void *data)
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302177{
2178 struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302179 event_notifier);
2180
2181 if (!swrm || !(swrm->dev)) {
2182 pr_err("%s: swrm or dev is NULL\n", __func__);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302183 return -EINVAL;
2184 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302185 switch (action) {
2186 case MSM_AUD_DC_EVENT:
2187 schedule_work(&(swrm->dc_presence_work));
2188 break;
2189 case SWR_WAKE_IRQ_EVENT:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302190 if (swrm->ipc_wakeup && !swrm->ipc_wakeup_triggered) {
2191 swrm->ipc_wakeup_triggered = true;
Ramprasad Katkam57349872018-11-11 18:34:57 +05302192 pm_stay_awake(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302193 schedule_work(&swrm->wakeup_work);
Ramprasad Katkamcd61c6e2018-09-18 13:22:58 +05302194 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302195 break;
2196 default:
2197 dev_err(swrm->dev, "%s: invalid event type: %lu\n",
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302198 __func__, action);
2199 return -EINVAL;
2200 }
2201
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302202 return 0;
2203}
2204
2205static void swrm_notify_work_fn(struct work_struct *work)
2206{
2207 struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302208 dc_presence_work);
2209
2210 if (!swrm || !swrm->pdev) {
2211 pr_err("%s: swrm or pdev is NULL\n", __func__);
2212 return;
2213 }
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302214 swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL);
2215}
2216
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302217static int swrm_probe(struct platform_device *pdev)
2218{
2219 struct swr_mstr_ctrl *swrm;
2220 struct swr_ctrl_platform_data *pdata;
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302221 u32 i, num_ports, port_num, port_type, ch_mask, swrm_hctl_reg = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302222 u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0;
2223 int ret = 0;
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302224 struct clk *lpass_core_hw_vote = NULL;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302225 struct clk *lpass_core_audio = NULL;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302226
2227 /* Allocate soundwire master driver structure */
2228 swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl),
2229 GFP_KERNEL);
2230 if (!swrm) {
2231 ret = -ENOMEM;
2232 goto err_memory_fail;
2233 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302234 swrm->pdev = pdev;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302235 swrm->dev = &pdev->dev;
2236 platform_set_drvdata(pdev, swrm);
2237 swr_set_ctrl_data(&swrm->master, swrm);
2238 pdata = dev_get_platdata(&pdev->dev);
2239 if (!pdata) {
2240 dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
2241 __func__);
2242 ret = -EINVAL;
2243 goto err_pdata_fail;
2244 }
2245 swrm->handle = (void *)pdata->handle;
2246 if (!swrm->handle) {
2247 dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
2248 __func__);
2249 ret = -EINVAL;
2250 goto err_pdata_fail;
2251 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302252 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr_master_id",
2253 &swrm->master_id);
2254 if (ret) {
2255 dev_err(&pdev->dev, "%s: failed to get master id\n", __func__);
2256 goto err_pdata_fail;
2257 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302258 if (!(of_property_read_u32(pdev->dev.of_node,
2259 "swrm-io-base", &swrm->swrm_base_reg)))
2260 ret = of_property_read_u32(pdev->dev.of_node,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302261 "swrm-io-base", &swrm->swrm_base_reg);
2262 if (!swrm->swrm_base_reg) {
2263 swrm->read = pdata->read;
2264 if (!swrm->read) {
2265 dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
2266 __func__);
2267 ret = -EINVAL;
2268 goto err_pdata_fail;
2269 }
2270 swrm->write = pdata->write;
2271 if (!swrm->write) {
2272 dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
2273 __func__);
2274 ret = -EINVAL;
2275 goto err_pdata_fail;
2276 }
2277 swrm->bulk_write = pdata->bulk_write;
2278 if (!swrm->bulk_write) {
2279 dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
2280 __func__);
2281 ret = -EINVAL;
2282 goto err_pdata_fail;
2283 }
2284 } else {
2285 swrm->swrm_dig_base = devm_ioremap(&pdev->dev,
2286 swrm->swrm_base_reg, SWRM_MAX_REGISTER);
2287 }
2288
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07002289 swrm->core_vote = pdata->core_vote;
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302290 if (!(of_property_read_u32(pdev->dev.of_node,
2291 "qcom,swrm-hctl-reg", &swrm_hctl_reg)))
2292 swrm->swrm_hctl_reg = devm_ioremap(&pdev->dev,
2293 swrm_hctl_reg, 0x4);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302294 swrm->clk = pdata->clk;
2295 if (!swrm->clk) {
2296 dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
2297 __func__);
2298 ret = -EINVAL;
2299 goto err_pdata_fail;
2300 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302301 if (of_property_read_u32(pdev->dev.of_node,
2302 "qcom,swr-clock-stop-mode0",
2303 &swrm->clk_stop_mode0_supp)) {
2304 swrm->clk_stop_mode0_supp = FALSE;
2305 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302306
2307 ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
2308 &swrm->num_dev);
2309 if (ret) {
2310 dev_dbg(&pdev->dev, "%s: Looking up %s property failed\n",
2311 __func__, "qcom,swr-num-dev");
2312 } else {
2313 if (swrm->num_dev > SWR_MAX_SLAVE_DEVICES) {
2314 dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
2315 __func__, swrm->num_dev, SWR_MAX_SLAVE_DEVICES);
2316 ret = -EINVAL;
2317 goto err_pdata_fail;
2318 }
2319 }
2320
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302321 /* Parse soundwire port mapping */
2322 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
2323 &num_ports);
2324 if (ret) {
2325 dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
2326 goto err_pdata_fail;
2327 }
2328 swrm->num_ports = num_ports;
2329
2330 if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
2331 &map_size)) {
2332 dev_err(swrm->dev, "missing port mapping\n");
2333 goto err_pdata_fail;
2334 }
2335
2336 map_length = map_size / (3 * sizeof(u32));
2337 if (num_ports > SWR_MSTR_PORT_LEN) {
2338 dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
2339 __func__);
2340 ret = -EINVAL;
2341 goto err_pdata_fail;
2342 }
2343 temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
2344
2345 if (!temp) {
2346 ret = -ENOMEM;
2347 goto err_pdata_fail;
2348 }
2349 ret = of_property_read_u32_array(pdev->dev.of_node,
2350 "qcom,swr-port-mapping", temp, 3 * map_length);
2351 if (ret) {
2352 dev_err(swrm->dev, "%s: Failed to read port mapping\n",
2353 __func__);
2354 goto err_pdata_fail;
2355 }
2356
2357 for (i = 0; i < map_length; i++) {
2358 port_num = temp[3 * i];
2359 port_type = temp[3 * i + 1];
2360 ch_mask = temp[3 * i + 2];
2361
2362 if (port_num != old_port_num)
2363 ch_iter = 0;
2364 swrm->port_mapping[port_num][ch_iter].port_type = port_type;
2365 swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
2366 old_port_num = port_num;
2367 }
2368 devm_kfree(&pdev->dev, temp);
2369
2370 swrm->reg_irq = pdata->reg_irq;
2371 swrm->master.read = swrm_read;
2372 swrm->master.write = swrm_write;
2373 swrm->master.bulk_write = swrm_bulk_write;
2374 swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
2375 swrm->master.connect_port = swrm_connect_port;
2376 swrm->master.disconnect_port = swrm_disconnect_port;
2377 swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
2378 swrm->master.remove_from_group = swrm_remove_from_group;
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302379 swrm->master.device_wakeup_vote = swrm_device_wakeup_vote;
2380 swrm->master.device_wakeup_unvote = swrm_device_wakeup_unvote;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302381 swrm->master.dev.parent = &pdev->dev;
2382 swrm->master.dev.of_node = pdev->dev.of_node;
2383 swrm->master.num_port = 0;
2384 swrm->rcmd_id = 0;
2385 swrm->wcmd_id = 0;
2386 swrm->slave_status = 0;
2387 swrm->num_rx_chs = 0;
2388 swrm->clk_ref_count = 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302389 swrm->swr_irq_wakeup_capable = 0;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302390 swrm->mclk_freq = MCLK_FREQ;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302391 swrm->bus_clk = MCLK_FREQ;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302392 swrm->dev_up = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302393 swrm->state = SWR_MSTR_UP;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302394 swrm->ipc_wakeup = false;
2395 swrm->ipc_wakeup_triggered = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302396 init_completion(&swrm->reset);
2397 init_completion(&swrm->broadcast);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302398 init_completion(&swrm->clk_off_complete);
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302399 mutex_init(&swrm->irq_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302400 mutex_init(&swrm->mlock);
2401 mutex_init(&swrm->reslock);
2402 mutex_init(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302403 mutex_init(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302404 mutex_init(&swrm->clklock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302405 mutex_init(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302406 mutex_init(&swrm->pm_lock);
2407 swrm->wlock_holders = 0;
2408 swrm->pm_state = SWRM_PM_SLEEPABLE;
2409 init_waitqueue_head(&swrm->pm_wq);
2410 pm_qos_add_request(&swrm->pm_qos_req,
2411 PM_QOS_CPU_DMA_LATENCY,
2412 PM_QOS_DEFAULT_VALUE);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302413
2414 for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++)
2415 INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
2416
Sudheer Papothi06f43412019-07-09 03:32:54 +05302417 /* Register LPASS core hw vote */
2418 lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
2419 if (IS_ERR(lpass_core_hw_vote)) {
2420 ret = PTR_ERR(lpass_core_hw_vote);
2421 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2422 __func__, "lpass_core_hw_vote", ret);
2423 lpass_core_hw_vote = NULL;
2424 ret = 0;
2425 }
2426 swrm->lpass_core_hw_vote = lpass_core_hw_vote;
2427
2428 /* Register LPASS audio core vote */
2429 lpass_core_audio = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
2430 if (IS_ERR(lpass_core_audio)) {
2431 ret = PTR_ERR(lpass_core_audio);
2432 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2433 __func__, "lpass_core_audio", ret);
2434 lpass_core_audio = NULL;
2435 ret = 0;
2436 }
2437 swrm->lpass_core_audio = lpass_core_audio;
2438
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302439 if (swrm->reg_irq) {
2440 ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
2441 SWR_IRQ_REGISTER);
2442 if (ret) {
2443 dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
2444 __func__, ret);
2445 goto err_irq_fail;
2446 }
2447 } else {
2448 swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
2449 if (swrm->irq < 0) {
2450 dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
2451 __func__, swrm->irq);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302452 goto err_irq_fail;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302453 }
2454
2455 ret = request_threaded_irq(swrm->irq, NULL,
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302456 swr_mstr_interrupt_v2,
Ramprasad Katkam83303512018-10-11 17:34:22 +05302457 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302458 "swr_master_irq", swrm);
2459 if (ret) {
2460 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2461 __func__, ret);
2462 goto err_irq_fail;
2463 }
2464
2465 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302466 /* Make inband tx interrupts as wakeup capable for slave irq */
2467 ret = of_property_read_u32(pdev->dev.of_node,
2468 "qcom,swr-mstr-irq-wakeup-capable",
2469 &swrm->swr_irq_wakeup_capable);
2470 if (ret)
2471 dev_dbg(swrm->dev, "%s: swrm irq wakeup capable not defined\n",
2472 __func__);
2473 if (swrm->swr_irq_wakeup_capable)
2474 irq_set_irq_wake(swrm->irq, 1);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302475 ret = swr_register_master(&swrm->master);
2476 if (ret) {
2477 dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
2478 goto err_mstr_fail;
2479 }
2480
2481 /* Add devices registered with board-info as the
2482 * controller will be up now
2483 */
2484 swr_master_add_boarddevices(&swrm->master);
2485 mutex_lock(&swrm->mlock);
2486 swrm_clk_request(swrm, true);
2487 ret = swrm_master_init(swrm);
2488 if (ret < 0) {
2489 dev_err(&pdev->dev,
2490 "%s: Error in master Initialization , err %d\n",
2491 __func__, ret);
2492 mutex_unlock(&swrm->mlock);
2493 goto err_mstr_fail;
2494 }
2495 swrm->version = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
2496
2497 mutex_unlock(&swrm->mlock);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302498 INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302499
2500 if (pdev->dev.of_node)
2501 of_register_swr_devices(&swrm->master);
2502
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302503#ifdef CONFIG_DEBUG_FS
2504 swrm->debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
2505 if (!IS_ERR(swrm->debugfs_swrm_dent)) {
2506 swrm->debugfs_peek = debugfs_create_file("swrm_peek",
2507 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2508 (void *) swrm, &swrm_debug_read_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302509
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302510 swrm->debugfs_poke = debugfs_create_file("swrm_poke",
2511 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2512 (void *) swrm, &swrm_debug_write_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302513
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302514 swrm->debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
2515 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2516 (void *) swrm,
2517 &swrm_debug_dump_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302518 }
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302519#endif
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302520 ret = device_init_wakeup(swrm->dev, true);
2521 if (ret) {
2522 dev_err(swrm->dev, "Device wakeup init failed: %d\n", ret);
2523 goto err_irq_wakeup_fail;
2524 }
2525
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302526 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
2527 pm_runtime_use_autosuspend(&pdev->dev);
2528 pm_runtime_set_active(&pdev->dev);
2529 pm_runtime_enable(&pdev->dev);
2530 pm_runtime_mark_last_busy(&pdev->dev);
2531
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302532 INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn);
2533 swrm->event_notifier.notifier_call = swrm_event_notify;
2534 msm_aud_evt_register_client(&swrm->event_notifier);
2535
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302536 return 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302537err_irq_wakeup_fail:
2538 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302539err_mstr_fail:
2540 if (swrm->reg_irq)
2541 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2542 swrm, SWR_IRQ_FREE);
2543 else if (swrm->irq)
2544 free_irq(swrm->irq, swrm);
2545err_irq_fail:
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302546 mutex_destroy(&swrm->irq_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302547 mutex_destroy(&swrm->mlock);
2548 mutex_destroy(&swrm->reslock);
2549 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302550 mutex_destroy(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302551 mutex_destroy(&swrm->clklock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302552 mutex_destroy(&swrm->pm_lock);
2553 pm_qos_remove_request(&swrm->pm_qos_req);
2554
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302555err_pdata_fail:
2556err_memory_fail:
2557 return ret;
2558}
2559
2560static int swrm_remove(struct platform_device *pdev)
2561{
2562 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2563
2564 if (swrm->reg_irq)
2565 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2566 swrm, SWR_IRQ_FREE);
2567 else if (swrm->irq)
2568 free_irq(swrm->irq, swrm);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302569 else if (swrm->wake_irq > 0)
2570 free_irq(swrm->wake_irq, swrm);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302571 if (swrm->swr_irq_wakeup_capable)
2572 irq_set_irq_wake(swrm->irq, 0);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302573 cancel_work_sync(&swrm->wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302574 pm_runtime_disable(&pdev->dev);
2575 pm_runtime_set_suspended(&pdev->dev);
2576 swr_unregister_master(&swrm->master);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302577 msm_aud_evt_unregister_client(&swrm->event_notifier);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302578 device_init_wakeup(swrm->dev, false);
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302579 mutex_destroy(&swrm->irq_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302580 mutex_destroy(&swrm->mlock);
2581 mutex_destroy(&swrm->reslock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302582 mutex_destroy(&swrm->iolock);
2583 mutex_destroy(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302584 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302585 mutex_destroy(&swrm->pm_lock);
2586 pm_qos_remove_request(&swrm->pm_qos_req);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302587 devm_kfree(&pdev->dev, swrm);
2588 return 0;
2589}
2590
2591static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
2592{
2593 u32 val;
2594
2595 dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
2596 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD);
2597 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2598 val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK;
2599 swr_master_write(swrm, SWRM_MCP_CFG_ADDR, val);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302600
2601 return 0;
2602}
2603
2604#ifdef CONFIG_PM
2605static int swrm_runtime_resume(struct device *dev)
2606{
2607 struct platform_device *pdev = to_platform_device(dev);
2608 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2609 int ret = 0;
Vatsal Buchae50b5002019-09-19 14:32:20 +05302610 bool swrm_clk_req_err = false;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302611 bool hw_core_err = false;
2612 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302613 struct swr_master *mstr = &swrm->master;
2614 struct swr_device *swr_dev;
2615
2616 dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
2617 __func__, swrm->state);
2618 mutex_lock(&swrm->reslock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302619
Sudheer Papothi384addd2019-06-14 02:26:52 +05302620 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2621 dev_err(dev, "%s:lpass core hw enable failed\n",
2622 __func__);
2623 hw_core_err = true;
2624 }
2625 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2626 dev_err(dev, "%s:lpass audio hw enable failed\n",
2627 __func__);
2628 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002629 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302630
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302631 if ((swrm->state == SWR_MSTR_DOWN) ||
2632 (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) {
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302633 if (swrm->clk_stop_mode0_supp) {
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302634 if (swrm->wake_irq > 0) {
2635 if (unlikely(!irq_get_irq_data
2636 (swrm->wake_irq))) {
2637 pr_err("%s: irq data is NULL\n",
2638 __func__);
2639 mutex_unlock(&swrm->reslock);
2640 return IRQ_NONE;
2641 }
2642 mutex_lock(&swrm->irq_lock);
2643 if (!irqd_irq_disabled(
2644 irq_get_irq_data(swrm->wake_irq)))
2645 disable_irq_nosync(swrm->wake_irq);
2646 mutex_unlock(&swrm->irq_lock);
2647 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302648 if (swrm->ipc_wakeup)
2649 msm_aud_evt_blocking_notifier_call_chain(
2650 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302651 }
2652
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302653 if (swrm_clk_request(swrm, true)) {
2654 /*
2655 * Set autosuspend timer to 1 for
2656 * master to enter into suspend.
2657 */
Vatsal Buchae50b5002019-09-19 14:32:20 +05302658 swrm_clk_req_err = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302659 goto exit;
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302660 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302661 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302662 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2663 ret = swr_device_up(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302664 if (ret == -ENODEV) {
2665 dev_dbg(dev,
2666 "%s slave device up not implemented\n",
2667 __func__);
2668 ret = 0;
2669 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302670 dev_err(dev,
2671 "%s: failed to wakeup swr dev %d\n",
2672 __func__, swr_dev->dev_num);
2673 swrm_clk_request(swrm, false);
2674 goto exit;
2675 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302676 }
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05302677 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2678 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2679 swrm_master_init(swrm);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302680 /* wait for hw enumeration to complete */
2681 usleep_range(100, 105);
Laxminath Kasame2291972019-11-08 14:51:59 +05302682 if (!swrm_check_link_status(swrm, 0x1))
2683 goto exit;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302684 swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, 0x0,
2685 SWRS_SCP_INT_STATUS_MASK_1);
Karthikeyan Manif6821902019-05-21 17:31:24 -07002686 if (swrm->state == SWR_MSTR_SSR) {
2687 mutex_unlock(&swrm->reslock);
2688 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
2689 mutex_lock(&swrm->reslock);
2690 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302691 } else {
2692 /*wake up from clock stop*/
2693 swr_master_write(swrm, SWRM_MCP_BUS_CTRL_ADDR, 0x2);
Sudheer Papothi55fa5972019-09-28 02:50:27 +05302694 /* clear and enable bus clash interrupt */
2695 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x08);
2696 swrm->intr_mask |= 0x08;
2697 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR,
2698 swrm->intr_mask);
2699 swr_master_write(swrm,
2700 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
2701 swrm->intr_mask);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302702 usleep_range(100, 105);
Laxminath Kasame2291972019-11-08 14:51:59 +05302703 if (!swrm_check_link_status(swrm, 0x1))
2704 goto exit;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302705 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302706 swrm->state = SWR_MSTR_UP;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302707 }
2708exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302709 if (!aud_core_err)
2710 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2711 if (!hw_core_err)
2712 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Vatsal Buchae50b5002019-09-19 14:32:20 +05302713 if (swrm_clk_req_err)
2714 pm_runtime_set_autosuspend_delay(&pdev->dev,
2715 ERR_AUTO_SUSPEND_TIMER_VAL);
2716 else
2717 pm_runtime_set_autosuspend_delay(&pdev->dev,
2718 auto_suspend_timer);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302719 mutex_unlock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302720
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302721 return ret;
2722}
2723
2724static int swrm_runtime_suspend(struct device *dev)
2725{
2726 struct platform_device *pdev = to_platform_device(dev);
2727 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2728 int ret = 0;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302729 bool hw_core_err = false;
2730 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302731 struct swr_master *mstr = &swrm->master;
2732 struct swr_device *swr_dev;
2733 int current_state = 0;
2734
2735 dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
2736 __func__, swrm->state);
2737 mutex_lock(&swrm->reslock);
2738 mutex_lock(&swrm->force_down_lock);
2739 current_state = swrm->state;
2740 mutex_unlock(&swrm->force_down_lock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302741
2742 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2743 dev_err(dev, "%s:lpass core hw enable failed\n",
2744 __func__);
2745 hw_core_err = true;
2746 }
2747 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2748 dev_err(dev, "%s:lpass audio hw enable failed\n",
2749 __func__);
2750 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002751 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302752
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302753 if ((current_state == SWR_MSTR_UP) ||
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302754 (current_state == SWR_MSTR_SSR)) {
2755
2756 if ((current_state != SWR_MSTR_SSR) &&
2757 swrm_is_port_en(&swrm->master)) {
2758 dev_dbg(dev, "%s ports are enabled\n", __func__);
2759 ret = -EBUSY;
2760 goto exit;
2761 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302762 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05302763 mutex_unlock(&swrm->reslock);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +05302764 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
Sudheer Papothi06f43412019-07-09 03:32:54 +05302765 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302766 swrm_clk_pause(swrm);
2767 swr_master_write(swrm, SWRM_COMP_CFG_ADDR, 0x00);
2768 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2769 ret = swr_device_down(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302770 if (ret == -ENODEV) {
2771 dev_dbg_ratelimited(dev,
2772 "%s slave device down not implemented\n",
2773 __func__);
2774 ret = 0;
2775 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302776 dev_err(dev,
2777 "%s: failed to shutdown swr dev %d\n",
2778 __func__, swr_dev->dev_num);
2779 goto exit;
2780 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302781 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302782 } else {
Sudheer Papothi55fa5972019-09-28 02:50:27 +05302783 /* Mask bus clash interrupt */
2784 swrm->intr_mask &= ~((u32)0x08);
2785 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR,
2786 swrm->intr_mask);
2787 swr_master_write(swrm,
2788 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
2789 swrm->intr_mask);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302790 mutex_unlock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302791 /* clock stop sequence */
2792 swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
2793 SWRS_SCP_CONTROL);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302794 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302795 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302796 }
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07002797 ret = swrm_clk_request(swrm, false);
2798 if (ret) {
2799 dev_err(dev, "%s: swrmn clk failed\n", __func__);
2800 ret = 0;
2801 goto exit;
2802 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302803
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302804 if (swrm->clk_stop_mode0_supp) {
2805 if (swrm->wake_irq > 0) {
2806 enable_irq(swrm->wake_irq);
2807 } else if (swrm->ipc_wakeup) {
2808 msm_aud_evt_blocking_notifier_call_chain(
2809 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
2810 swrm->ipc_wakeup_triggered = false;
2811 }
Laxminath Kasame2291972019-11-08 14:51:59 +05302812 if (!swrm_check_link_status(swrm, 0x0))
2813 goto exit;
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302814 }
2815
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302816 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302817 /* Retain SSR state until resume */
2818 if (current_state != SWR_MSTR_SSR)
2819 swrm->state = SWR_MSTR_DOWN;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302820exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302821 if (!aud_core_err)
2822 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2823 if (!hw_core_err)
2824 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302825 mutex_unlock(&swrm->reslock);
2826 return ret;
2827}
2828#endif /* CONFIG_PM */
2829
Sudheer Papothi06f43412019-07-09 03:32:54 +05302830static int swrm_device_suspend(struct device *dev)
2831{
2832 struct platform_device *pdev = to_platform_device(dev);
2833 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2834 int ret = 0;
2835
2836 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2837 if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
2838 ret = swrm_runtime_suspend(dev);
2839 if (!ret) {
2840 pm_runtime_disable(dev);
2841 pm_runtime_set_suspended(dev);
2842 pm_runtime_enable(dev);
2843 }
2844 }
2845
2846 return 0;
2847}
2848
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302849static int swrm_device_down(struct device *dev)
2850{
2851 struct platform_device *pdev = to_platform_device(dev);
2852 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302853
2854 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2855
2856 mutex_lock(&swrm->force_down_lock);
2857 swrm->state = SWR_MSTR_SSR;
2858 mutex_unlock(&swrm->force_down_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302859
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302860 swrm_device_suspend(dev);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302861 return 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302862}
2863
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302864int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm)
2865{
2866 int ret = 0;
Laxminath Kasama60239e2019-01-10 14:43:03 +05302867 int irq, dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302868
2869 if (!swrm->ipc_wakeup) {
Laxminath Kasama60239e2019-01-10 14:43:03 +05302870 irq = of_get_named_gpio(swrm->dev->of_node,
2871 "qcom,swr-wakeup-irq", 0);
2872 if (gpio_is_valid(irq)) {
2873 swrm->wake_irq = gpio_to_irq(irq);
2874 if (swrm->wake_irq < 0) {
2875 dev_err(swrm->dev,
2876 "Unable to configure irq\n");
2877 return swrm->wake_irq;
2878 }
2879 } else {
2880 dir_apps_irq = platform_get_irq_byname(swrm->pdev,
2881 "swr_wake_irq");
2882 if (dir_apps_irq < 0) {
2883 dev_err(swrm->dev,
2884 "TLMM connect gpio not found\n");
2885 return -EINVAL;
2886 }
2887 swrm->wake_irq = dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302888 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302889 ret = request_threaded_irq(swrm->wake_irq, NULL,
2890 swrm_wakeup_interrupt,
2891 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
2892 "swr_wake_irq", swrm);
2893 if (ret) {
2894 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2895 __func__, ret);
2896 return -EINVAL;
2897 }
Aditya Bavanari3517b112018-12-03 13:26:59 +05302898 irq_set_irq_wake(swrm->wake_irq, 1);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302899 }
2900 return ret;
2901}
2902
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302903static int swrm_alloc_port_mem(struct device *dev, struct swr_mstr_ctrl *swrm,
2904 u32 uc, u32 size)
2905{
2906 if (!swrm->port_param) {
2907 swrm->port_param = devm_kzalloc(dev,
2908 sizeof(swrm->port_param) * SWR_UC_MAX,
2909 GFP_KERNEL);
2910 if (!swrm->port_param)
2911 return -ENOMEM;
2912 }
2913 if (!swrm->port_param[uc]) {
2914 swrm->port_param[uc] = devm_kcalloc(dev, size,
2915 sizeof(struct port_params),
2916 GFP_KERNEL);
2917 if (!swrm->port_param[uc])
2918 return -ENOMEM;
2919 } else {
2920 dev_err_ratelimited(swrm->dev, "%s: called more than once\n",
2921 __func__);
2922 }
2923
2924 return 0;
2925}
2926
2927static int swrm_copy_port_config(struct swr_mstr_ctrl *swrm,
2928 struct swrm_port_config *port_cfg,
2929 u32 size)
2930{
2931 int idx;
2932 struct port_params *params;
2933 int uc = port_cfg->uc;
2934 int ret = 0;
2935
2936 for (idx = 0; idx < size; idx++) {
2937 params = &((struct port_params *)port_cfg->params)[idx];
2938 if (!params) {
2939 dev_err(swrm->dev, "%s: Invalid params\n", __func__);
2940 ret = -EINVAL;
2941 break;
2942 }
2943 memcpy(&swrm->port_param[uc][idx], params,
2944 sizeof(struct port_params));
2945 }
2946
2947 return ret;
2948}
2949
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302950/**
2951 * swrm_wcd_notify - parent device can notify to soundwire master through
2952 * this function
2953 * @pdev: pointer to platform device structure
2954 * @id: command id from parent to the soundwire master
2955 * @data: data from parent device to soundwire master
2956 */
2957int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
2958{
2959 struct swr_mstr_ctrl *swrm;
2960 int ret = 0;
2961 struct swr_master *mstr;
2962 struct swr_device *swr_dev;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302963 struct swrm_port_config *port_cfg;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302964
2965 if (!pdev) {
2966 pr_err("%s: pdev is NULL\n", __func__);
2967 return -EINVAL;
2968 }
2969 swrm = platform_get_drvdata(pdev);
2970 if (!swrm) {
2971 dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__);
2972 return -EINVAL;
2973 }
2974 mstr = &swrm->master;
2975
2976 switch (id) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05302977 case SWR_REQ_CLK_SWITCH:
2978 /* This will put soundwire in clock stop mode and disable the
2979 * clocks, if there is no active usecase running, so that the
2980 * next activity on soundwire will request clock from new clock
2981 * source.
2982 */
2983 mutex_lock(&swrm->mlock);
2984 if (swrm->state == SWR_MSTR_UP)
2985 swrm_device_suspend(&pdev->dev);
2986 mutex_unlock(&swrm->mlock);
2987 break;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302988 case SWR_CLK_FREQ:
2989 if (!data) {
2990 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
2991 ret = -EINVAL;
2992 } else {
2993 mutex_lock(&swrm->mlock);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302994 if (swrm->mclk_freq != *(int *)data) {
2995 dev_dbg(swrm->dev, "%s: freq change: force mstr down\n", __func__);
2996 if (swrm->state == SWR_MSTR_DOWN)
2997 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
2998 __func__, swrm->state);
2999 else
3000 swrm_device_suspend(&pdev->dev);
Prasad Kumpatla386df4e2019-10-11 18:23:16 +05303001 /*
3002 * add delay to ensure clk release happen
3003 * if interrupt triggered for clk stop,
3004 * wait for it to exit
3005 */
3006 usleep_range(10000, 10500);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05303007 }
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05303008 swrm->mclk_freq = *(int *)data;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05303009 swrm->bus_clk = swrm->mclk_freq;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05303010 mutex_unlock(&swrm->mlock);
3011 }
3012 break;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303013 case SWR_DEVICE_SSR_DOWN:
3014 mutex_lock(&swrm->devlock);
3015 swrm->dev_up = false;
3016 mutex_unlock(&swrm->devlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05303017 mutex_lock(&swrm->reslock);
3018 swrm->state = SWR_MSTR_SSR;
3019 mutex_unlock(&swrm->reslock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303020 break;
3021 case SWR_DEVICE_SSR_UP:
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05303022 /* wait for clk voting to be zero */
Ramprasad Katkam7f6462e2018-11-06 11:51:22 +05303023 reinit_completion(&swrm->clk_off_complete);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05303024 if (swrm->clk_ref_count &&
3025 !wait_for_completion_timeout(&swrm->clk_off_complete,
Ramprasad Katkamc87efeb2018-12-12 19:26:19 +05303026 msecs_to_jiffies(500)))
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05303027 dev_err(swrm->dev, "%s: clock voting not zero\n",
3028 __func__);
3029
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303030 mutex_lock(&swrm->devlock);
3031 swrm->dev_up = true;
3032 mutex_unlock(&swrm->devlock);
3033 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303034 case SWR_DEVICE_DOWN:
3035 dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
3036 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05303037 if (swrm->state == SWR_MSTR_DOWN)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303038 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
3039 __func__, swrm->state);
3040 else
3041 swrm_device_down(&pdev->dev);
3042 mutex_unlock(&swrm->mlock);
3043 break;
3044 case SWR_DEVICE_UP:
3045 dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
Ramprasad Katkam0fed92f2018-11-08 14:22:22 +05303046 mutex_lock(&swrm->devlock);
3047 if (!swrm->dev_up) {
3048 dev_dbg(swrm->dev, "SSR not complete yet\n");
3049 mutex_unlock(&swrm->devlock);
3050 return -EBUSY;
3051 }
3052 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303053 mutex_lock(&swrm->mlock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303054 pm_runtime_mark_last_busy(&pdev->dev);
3055 pm_runtime_get_sync(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303056 mutex_lock(&swrm->reslock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303057 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
3058 ret = swr_reset_device(swr_dev);
3059 if (ret) {
3060 dev_err(swrm->dev,
3061 "%s: failed to reset swr device %d\n",
3062 __func__, swr_dev->dev_num);
3063 swrm_clk_request(swrm, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303064 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303065 }
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303066 pm_runtime_mark_last_busy(&pdev->dev);
3067 pm_runtime_put_autosuspend(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303068 mutex_unlock(&swrm->reslock);
3069 mutex_unlock(&swrm->mlock);
3070 break;
3071 case SWR_SET_NUM_RX_CH:
3072 if (!data) {
3073 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
3074 ret = -EINVAL;
3075 } else {
3076 mutex_lock(&swrm->mlock);
3077 swrm->num_rx_chs = *(int *)data;
3078 if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
3079 list_for_each_entry(swr_dev, &mstr->devices,
3080 dev_list) {
3081 ret = swr_set_device_group(swr_dev,
3082 SWR_BROADCAST);
3083 if (ret)
3084 dev_err(swrm->dev,
3085 "%s: set num ch failed\n",
3086 __func__);
3087 }
3088 } else {
3089 list_for_each_entry(swr_dev, &mstr->devices,
3090 dev_list) {
3091 ret = swr_set_device_group(swr_dev,
3092 SWR_GROUP_NONE);
3093 if (ret)
3094 dev_err(swrm->dev,
3095 "%s: set num ch failed\n",
3096 __func__);
3097 }
3098 }
3099 mutex_unlock(&swrm->mlock);
3100 }
3101 break;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303102 case SWR_REGISTER_WAKE_IRQ:
3103 if (!data) {
3104 dev_err(swrm->dev, "%s: reg wake irq data is NULL\n",
3105 __func__);
3106 ret = -EINVAL;
3107 } else {
3108 mutex_lock(&swrm->mlock);
3109 swrm->ipc_wakeup = *(u32 *)data;
3110 ret = swrm_register_wake_irq(swrm);
3111 if (ret)
3112 dev_err(swrm->dev, "%s: register wake_irq failed\n",
3113 __func__);
3114 mutex_unlock(&swrm->mlock);
3115 }
3116 break;
Sudheer Papothi72ee2642019-08-08 05:15:17 +05303117 case SWR_REGISTER_WAKEUP:
3118 msm_aud_evt_blocking_notifier_call_chain(
3119 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
3120 break;
3121 case SWR_DEREGISTER_WAKEUP:
3122 msm_aud_evt_blocking_notifier_call_chain(
3123 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
3124 break;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05303125 case SWR_SET_PORT_MAP:
3126 if (!data) {
3127 dev_err(swrm->dev, "%s: data is NULL for id=%d\n",
3128 __func__, id);
3129 ret = -EINVAL;
3130 } else {
3131 mutex_lock(&swrm->mlock);
3132 port_cfg = (struct swrm_port_config *)data;
3133 if (!port_cfg->size) {
3134 ret = -EINVAL;
3135 goto done;
3136 }
3137 ret = swrm_alloc_port_mem(&pdev->dev, swrm,
3138 port_cfg->uc, port_cfg->size);
3139 if (!ret)
3140 swrm_copy_port_config(swrm, port_cfg,
3141 port_cfg->size);
3142done:
3143 mutex_unlock(&swrm->mlock);
3144 }
3145 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303146 default:
3147 dev_err(swrm->dev, "%s: swr master unknown id %d\n",
3148 __func__, id);
3149 break;
3150 }
3151 return ret;
3152}
3153EXPORT_SYMBOL(swrm_wcd_notify);
3154
Ramprasad Katkam57349872018-11-11 18:34:57 +05303155/*
3156 * swrm_pm_cmpxchg:
3157 * Check old state and exchange with pm new state
3158 * if old state matches with current state
3159 *
3160 * @swrm: pointer to wcd core resource
3161 * @o: pm old state
3162 * @n: pm new state
3163 *
3164 * Returns old state
3165 */
3166static enum swrm_pm_state swrm_pm_cmpxchg(
3167 struct swr_mstr_ctrl *swrm,
3168 enum swrm_pm_state o,
3169 enum swrm_pm_state n)
3170{
3171 enum swrm_pm_state old;
3172
3173 if (!swrm)
3174 return o;
3175
3176 mutex_lock(&swrm->pm_lock);
3177 old = swrm->pm_state;
3178 if (old == o)
3179 swrm->pm_state = n;
3180 mutex_unlock(&swrm->pm_lock);
3181
3182 return old;
3183}
3184
3185static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm)
3186{
3187 enum swrm_pm_state os;
3188
3189 /*
3190 * swrm_{lock/unlock}_sleep will be called by swr irq handler
3191 * and slave wake up requests..
3192 *
3193 * If system didn't resume, we can simply return false so
3194 * IRQ handler can return without handling IRQ.
3195 */
3196 mutex_lock(&swrm->pm_lock);
3197 if (swrm->wlock_holders++ == 0) {
3198 dev_dbg(swrm->dev, "%s: holding wake lock\n", __func__);
3199 pm_qos_update_request(&swrm->pm_qos_req,
3200 msm_cpuidle_get_deep_idle_latency());
3201 pm_stay_awake(swrm->dev);
3202 }
3203 mutex_unlock(&swrm->pm_lock);
3204
3205 if (!wait_event_timeout(swrm->pm_wq,
3206 ((os = swrm_pm_cmpxchg(swrm,
3207 SWRM_PM_SLEEPABLE,
3208 SWRM_PM_AWAKE)) ==
3209 SWRM_PM_SLEEPABLE ||
3210 (os == SWRM_PM_AWAKE)),
3211 msecs_to_jiffies(
3212 SWRM_SYSTEM_RESUME_TIMEOUT_MS))) {
3213 dev_err(swrm->dev, "%s: system didn't resume within %dms, s %d, w %d\n",
3214 __func__, SWRM_SYSTEM_RESUME_TIMEOUT_MS, swrm->pm_state,
3215 swrm->wlock_holders);
3216 swrm_unlock_sleep(swrm);
3217 return false;
3218 }
3219 wake_up_all(&swrm->pm_wq);
3220 return true;
3221}
3222
3223static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm)
3224{
3225 mutex_lock(&swrm->pm_lock);
3226 if (--swrm->wlock_holders == 0) {
3227 dev_dbg(swrm->dev, "%s: releasing wake lock pm_state %d -> %d\n",
3228 __func__, swrm->pm_state, SWRM_PM_SLEEPABLE);
3229 /*
3230 * if swrm_lock_sleep failed, pm_state would be still
3231 * swrm_PM_ASLEEP, don't overwrite
3232 */
3233 if (likely(swrm->pm_state == SWRM_PM_AWAKE))
3234 swrm->pm_state = SWRM_PM_SLEEPABLE;
3235 pm_qos_update_request(&swrm->pm_qos_req,
3236 PM_QOS_DEFAULT_VALUE);
3237 pm_relax(swrm->dev);
3238 }
3239 mutex_unlock(&swrm->pm_lock);
3240 wake_up_all(&swrm->pm_wq);
3241}
3242
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303243#ifdef CONFIG_PM_SLEEP
3244static int swrm_suspend(struct device *dev)
3245{
3246 int ret = -EBUSY;
3247 struct platform_device *pdev = to_platform_device(dev);
3248 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3249
3250 dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
Ramprasad Katkam57349872018-11-11 18:34:57 +05303251
3252 mutex_lock(&swrm->pm_lock);
3253
3254 if (swrm->pm_state == SWRM_PM_SLEEPABLE) {
3255 dev_dbg(swrm->dev, "%s: suspending system, state %d, wlock %d\n",
3256 __func__, swrm->pm_state,
3257 swrm->wlock_holders);
3258 swrm->pm_state = SWRM_PM_ASLEEP;
3259 } else if (swrm->pm_state == SWRM_PM_AWAKE) {
3260 /*
3261 * unlock to wait for pm_state == SWRM_PM_SLEEPABLE
3262 * then set to SWRM_PM_ASLEEP
3263 */
3264 dev_dbg(swrm->dev, "%s: waiting to suspend system, state %d, wlock %d\n",
3265 __func__, swrm->pm_state,
3266 swrm->wlock_holders);
3267 mutex_unlock(&swrm->pm_lock);
3268 if (!(wait_event_timeout(swrm->pm_wq, swrm_pm_cmpxchg(
3269 swrm, SWRM_PM_SLEEPABLE,
3270 SWRM_PM_ASLEEP) ==
3271 SWRM_PM_SLEEPABLE,
3272 msecs_to_jiffies(
3273 SWRM_SYS_SUSPEND_WAIT)))) {
3274 dev_dbg(swrm->dev, "%s: suspend failed state %d, wlock %d\n",
3275 __func__, swrm->pm_state,
3276 swrm->wlock_holders);
3277 return -EBUSY;
3278 } else {
3279 dev_dbg(swrm->dev,
3280 "%s: done, state %d, wlock %d\n",
3281 __func__, swrm->pm_state,
3282 swrm->wlock_holders);
3283 }
3284 mutex_lock(&swrm->pm_lock);
3285 } else if (swrm->pm_state == SWRM_PM_ASLEEP) {
3286 dev_dbg(swrm->dev, "%s: system is already suspended, state %d, wlock %d\n",
3287 __func__, swrm->pm_state,
3288 swrm->wlock_holders);
3289 }
3290
3291 mutex_unlock(&swrm->pm_lock);
3292
3293 if ((!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev))) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303294 ret = swrm_runtime_suspend(dev);
3295 if (!ret) {
3296 /*
3297 * Synchronize runtime-pm and system-pm states:
3298 * At this point, we are already suspended. If
3299 * runtime-pm still thinks its active, then
3300 * make sure its status is in sync with HW
3301 * status. The three below calls let the
3302 * runtime-pm know that we are suspended
3303 * already without re-invoking the suspend
3304 * callback
3305 */
3306 pm_runtime_disable(dev);
3307 pm_runtime_set_suspended(dev);
3308 pm_runtime_enable(dev);
3309 }
3310 }
3311 if (ret == -EBUSY) {
3312 /*
3313 * There is a possibility that some audio stream is active
3314 * during suspend. We dont want to return suspend failure in
3315 * that case so that display and relevant components can still
3316 * go to suspend.
3317 * If there is some other error, then it should be passed-on
3318 * to system level suspend
3319 */
3320 ret = 0;
3321 }
3322 return ret;
3323}
3324
3325static int swrm_resume(struct device *dev)
3326{
3327 int ret = 0;
3328 struct platform_device *pdev = to_platform_device(dev);
3329 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3330
3331 dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
3332 if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
3333 ret = swrm_runtime_resume(dev);
3334 if (!ret) {
3335 pm_runtime_mark_last_busy(dev);
3336 pm_request_autosuspend(dev);
3337 }
3338 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05303339 mutex_lock(&swrm->pm_lock);
3340 if (swrm->pm_state == SWRM_PM_ASLEEP) {
3341 dev_dbg(swrm->dev,
3342 "%s: resuming system, state %d, wlock %d\n",
3343 __func__, swrm->pm_state,
3344 swrm->wlock_holders);
3345 swrm->pm_state = SWRM_PM_SLEEPABLE;
3346 } else {
3347 dev_dbg(swrm->dev, "%s: system is already awake, state %d wlock %d\n",
3348 __func__, swrm->pm_state,
3349 swrm->wlock_holders);
3350 }
3351 mutex_unlock(&swrm->pm_lock);
3352 wake_up_all(&swrm->pm_wq);
3353
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303354 return ret;
3355}
3356#endif /* CONFIG_PM_SLEEP */
3357
3358static const struct dev_pm_ops swrm_dev_pm_ops = {
3359 SET_SYSTEM_SLEEP_PM_OPS(
3360 swrm_suspend,
3361 swrm_resume
3362 )
3363 SET_RUNTIME_PM_OPS(
3364 swrm_runtime_suspend,
3365 swrm_runtime_resume,
3366 NULL
3367 )
3368};
3369
3370static const struct of_device_id swrm_dt_match[] = {
3371 {
3372 .compatible = "qcom,swr-mstr",
3373 },
3374 {}
3375};
3376
3377static struct platform_driver swr_mstr_driver = {
3378 .probe = swrm_probe,
3379 .remove = swrm_remove,
3380 .driver = {
3381 .name = SWR_WCD_NAME,
3382 .owner = THIS_MODULE,
3383 .pm = &swrm_dev_pm_ops,
3384 .of_match_table = swrm_dt_match,
Xiaojun Sang53cd13a2018-06-29 15:14:37 +08003385 .suppress_bind_attrs = true,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303386 },
3387};
3388
3389static int __init swrm_init(void)
3390{
3391 return platform_driver_register(&swr_mstr_driver);
3392}
3393module_init(swrm_init);
3394
3395static void __exit swrm_exit(void)
3396{
3397 platform_driver_unregister(&swr_mstr_driver);
3398}
3399module_exit(swrm_exit);
3400
3401MODULE_LICENSE("GPL v2");
3402MODULE_DESCRIPTION("SoundWire Master Controller");
3403MODULE_ALIAS("platform:swr-mstr");