blob: 6eacf22b691196b3ca4a9c2237d406036e3db6df [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 Papothi921b8652019-10-03 02:13:32 +0530318static bool is_swr_clk_needed(struct swr_mstr_ctrl *swrm)
319{
320 return ((swrm->version <= SWRM_VERSION_1_5_1) ? true : false);
321}
322
Sudheer Papothi384addd2019-06-14 02:26:52 +0530323static int swrm_request_hw_vote(struct swr_mstr_ctrl *swrm,
324 int core_type, bool enable)
325{
326 int ret = 0;
327
328 if (core_type == LPASS_HW_CORE) {
329 if (swrm->lpass_core_hw_vote) {
330 if (enable) {
331 ret =
332 clk_prepare_enable(swrm->lpass_core_hw_vote);
333 if (ret < 0)
334 dev_err(swrm->dev,
335 "%s:lpass core hw enable failed\n",
336 __func__);
337 } else
338 clk_disable_unprepare(swrm->lpass_core_hw_vote);
339 }
340 }
341 if (core_type == LPASS_AUDIO_CORE) {
342 if (swrm->lpass_core_audio) {
343 if (enable) {
344 ret =
345 clk_prepare_enable(swrm->lpass_core_audio);
346 if (ret < 0)
347 dev_err(swrm->dev,
348 "%s:lpass audio hw enable failed\n",
349 __func__);
350 } else
351 clk_disable_unprepare(swrm->lpass_core_audio);
352 }
353 }
354
355 return ret;
356}
357
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +0530358static int swrm_get_ssp_period(struct swr_mstr_ctrl *swrm,
359 int row, int col,
360 int frame_sync)
361{
362 if (!swrm || !row || !col || !frame_sync)
363 return 1;
364
365 return ((swrm->bus_clk * 2) / ((row * col) * frame_sync));
366}
367
Sudheer Papothi921b8652019-10-03 02:13:32 +0530368static int swrm_core_vote_request(struct swr_mstr_ctrl *swrm)
369{
370 int ret = 0;
371
372 if (!swrm->handle)
373 return -EINVAL;
374
375 mutex_lock(&swrm->clklock);
376 if (!swrm->dev_up) {
377 ret = -ENODEV;
378 goto exit;
379 }
380 if (swrm->core_vote) {
381 ret = swrm->core_vote(swrm->handle, true);
382 if (ret)
383 dev_err_ratelimited(swrm->dev,
384 "%s: core vote request failed\n", __func__);
385 }
386exit:
387 mutex_unlock(&swrm->clklock);
388
389 return ret;
390}
391
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530392static int swrm_clk_request(struct swr_mstr_ctrl *swrm, bool enable)
393{
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530394 int ret = 0;
395
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530396 if (!swrm->clk || !swrm->handle)
397 return -EINVAL;
398
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530399 mutex_lock(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530400 if (enable) {
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530401 if (!swrm->dev_up) {
402 ret = -ENODEV;
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530403 goto exit;
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530404 }
Sudheer Papothi921b8652019-10-03 02:13:32 +0530405 if (is_swr_clk_needed(swrm)) {
406 if (swrm->core_vote) {
407 ret = swrm->core_vote(swrm->handle, true);
408 if (ret) {
409 dev_err_ratelimited(swrm->dev,
410 "%s: core vote request failed\n",
411 __func__);
412 goto exit;
413 }
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -0700414 }
415 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530416 swrm->clk_ref_count++;
417 if (swrm->clk_ref_count == 1) {
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530418 ret = swrm->clk(swrm->handle, true);
419 if (ret) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +0530420 dev_err_ratelimited(swrm->dev,
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530421 "%s: clock enable req failed",
422 __func__);
423 --swrm->clk_ref_count;
424 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530425 }
426 } else if (--swrm->clk_ref_count == 0) {
427 swrm->clk(swrm->handle, false);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530428 complete(&swrm->clk_off_complete);
429 }
430 if (swrm->clk_ref_count < 0) {
Meng Wang8c60bb52019-06-19 15:49:06 +0800431 dev_err(swrm->dev, "%s: swrm clk count mismatch\n", __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530432 swrm->clk_ref_count = 0;
433 }
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530434
435exit:
436 mutex_unlock(&swrm->clklock);
437 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530438}
439
440static int swrm_ahb_write(struct swr_mstr_ctrl *swrm,
441 u16 reg, u32 *value)
442{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530443 u32 temp = (u32)(*value);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530444 int ret = 0;
445
446 mutex_lock(&swrm->devlock);
447 if (!swrm->dev_up)
448 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530449
Sudheer Papothi921b8652019-10-03 02:13:32 +0530450 if (is_swr_clk_needed(swrm)) {
451 ret = swrm_clk_request(swrm, TRUE);
452 if (ret) {
453 dev_err_ratelimited(swrm->dev,
454 "%s: clock request failed\n",
455 __func__);
456 goto err;
457 }
458 } else if (swrm_core_vote_request(swrm)) {
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530459 goto err;
460 }
Sudheer Papothi921b8652019-10-03 02:13:32 +0530461
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530462 iowrite32(temp, swrm->swrm_dig_base + reg);
Sudheer Papothi921b8652019-10-03 02:13:32 +0530463 if (is_swr_clk_needed(swrm))
464 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530465err:
466 mutex_unlock(&swrm->devlock);
467 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530468}
469
470static int swrm_ahb_read(struct swr_mstr_ctrl *swrm,
471 u16 reg, u32 *value)
472{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530473 u32 temp = 0;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530474 int ret = 0;
475
476 mutex_lock(&swrm->devlock);
477 if (!swrm->dev_up)
478 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530479
Sudheer Papothi921b8652019-10-03 02:13:32 +0530480 if (is_swr_clk_needed(swrm)) {
481 ret = swrm_clk_request(swrm, TRUE);
482 if (ret) {
483 dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
484 __func__);
485 goto err;
486 }
487 } else if (swrm_core_vote_request(swrm)) {
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530488 goto err;
489 }
Sudheer Papothi921b8652019-10-03 02:13:32 +0530490
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530491 temp = ioread32(swrm->swrm_dig_base + reg);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530492 *value = temp;
Sudheer Papothi921b8652019-10-03 02:13:32 +0530493 if (is_swr_clk_needed(swrm))
494 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530495err:
496 mutex_unlock(&swrm->devlock);
497 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530498}
499
500static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr)
501{
502 u32 val = 0;
503
504 if (swrm->read)
505 val = swrm->read(swrm->handle, reg_addr);
506 else
507 swrm_ahb_read(swrm, reg_addr, &val);
508 return val;
509}
510
511static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val)
512{
513 if (swrm->write)
514 swrm->write(swrm->handle, reg_addr, val);
515 else
516 swrm_ahb_write(swrm, reg_addr, &val);
517}
518
519static int swr_master_bulk_write(struct swr_mstr_ctrl *swrm, u32 *reg_addr,
520 u32 *val, unsigned int length)
521{
522 int i = 0;
523
524 if (swrm->bulk_write)
525 swrm->bulk_write(swrm->handle, reg_addr, val, length);
526 else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530527 mutex_lock(&swrm->iolock);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530528 for (i = 0; i < length; i++) {
529 /* wait for FIFO WR command to complete to avoid overflow */
Karthikeyan Mani5d52dd82019-08-15 16:58:08 -0700530 /*
531 * Reduce sleep from 100us to 10us to meet KPIs
532 * This still meets the hardware spec
533 */
534 usleep_range(10, 12);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530535 swr_master_write(swrm, reg_addr[i], val[i]);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530536 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530537 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530538 }
539 return 0;
540}
541
Laxminath Kasame2291972019-11-08 14:51:59 +0530542static bool swrm_check_link_status(struct swr_mstr_ctrl *swrm, bool active)
543{
544 int retry = SWRM_LINK_STATUS_RETRY_CNT;
545 int ret = false;
546 int status = active ? 0x1 : 0x0;
Laxminath Kasam09819e92019-11-18 14:38:11 +0530547 int comp_sts = 0x0;
Laxminath Kasame2291972019-11-08 14:51:59 +0530548
549 if ((swrm->version <= SWRM_VERSION_1_5_1))
550 return true;
551
552 do {
Laxminath Kasam09819e92019-11-18 14:38:11 +0530553 comp_sts = swr_master_read(swrm, SWRM_COMP_STATUS) & 0x01;
554 /* check comp status and status requested met */
555 if ((comp_sts && status) || (!comp_sts && !status)) {
Laxminath Kasame2291972019-11-08 14:51:59 +0530556 ret = true;
557 break;
558 }
559 retry--;
560 usleep_range(500, 510);
561 } while (retry);
562
563 if (retry == 0)
564 dev_err(swrm->dev, "%s: link status not %s\n", __func__,
565 active ? "connected" : "disconnected");
566
567 return ret;
568}
569
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530570static bool swrm_is_port_en(struct swr_master *mstr)
571{
572 return !!(mstr->num_port);
573}
574
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530575static void copy_port_tables(struct swr_mstr_ctrl *swrm,
576 struct port_params *params)
577{
578 u8 i;
579 struct port_params *config = params;
580
581 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
582 /* wsa uses single frame structure for all configurations */
583 if (!swrm->mport_cfg[i].port_en)
584 continue;
585 swrm->mport_cfg[i].sinterval = config[i].si;
586 swrm->mport_cfg[i].offset1 = config[i].off1;
587 swrm->mport_cfg[i].offset2 = config[i].off2;
588 swrm->mport_cfg[i].hstart = config[i].hstart;
589 swrm->mport_cfg[i].hstop = config[i].hstop;
590 swrm->mport_cfg[i].blk_pack_mode = config[i].bp_mode;
591 swrm->mport_cfg[i].blk_grp_count = config[i].bgp_ctrl;
592 swrm->mport_cfg[i].word_length = config[i].wd_len;
593 swrm->mport_cfg[i].lane_ctrl = config[i].lane_ctrl;
594 }
595}
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530596static int swrm_get_port_config(struct swr_mstr_ctrl *swrm)
597{
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530598 struct port_params *params;
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530599 u32 usecase = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530600
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530601 /* TODO - Send usecase information to avoid checking for master_id */
602 if (swrm->mport_cfg[SWRM_DSD_PARAMS_PORT].port_en &&
603 (swrm->master_id == MASTER_ID_RX))
604 usecase = 1;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530605
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530606 params = swrm->port_param[usecase];
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530607 copy_port_tables(swrm, params);
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530608
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530609 return 0;
610}
611
612static int swrm_get_master_port(struct swr_mstr_ctrl *swrm, u8 *mstr_port_id,
613 u8 *mstr_ch_mask, u8 mstr_prt_type,
614 u8 slv_port_id)
615{
616 int i, j;
617 *mstr_port_id = 0;
618
619 for (i = 1; i <= swrm->num_ports; i++) {
620 for (j = 0; j < SWR_MAX_CH_PER_PORT; j++) {
621 if (swrm->port_mapping[i][j].port_type == mstr_prt_type)
622 goto found;
623 }
624 }
625found:
626 if (i > swrm->num_ports || j == SWR_MAX_CH_PER_PORT) {
627 dev_err(swrm->dev, "%s: port type not supported by master\n",
628 __func__);
629 return -EINVAL;
630 }
631 /* id 0 corresponds to master port 1 */
632 *mstr_port_id = i - 1;
633 *mstr_ch_mask = swrm->port_mapping[i][j].ch_mask;
634
635 return 0;
636
637}
638
639static u32 swrm_get_packed_reg_val(u8 *cmd_id, u8 cmd_data,
640 u8 dev_addr, u16 reg_addr)
641{
642 u32 val;
643 u8 id = *cmd_id;
644
645 if (id != SWR_BROADCAST_CMD_ID) {
646 if (id < 14)
647 id += 1;
648 else
649 id = 0;
650 *cmd_id = id;
651 }
652 val = SWR_REG_VAL_PACK(cmd_data, dev_addr, id, reg_addr);
653
654 return val;
655}
656
657static int swrm_cmd_fifo_rd_cmd(struct swr_mstr_ctrl *swrm, int *cmd_data,
658 u8 dev_addr, u8 cmd_id, u16 reg_addr,
659 u32 len)
660{
661 u32 val;
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530662 u32 retry_attempt = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530663
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530664 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530665 val = swrm_get_packed_reg_val(&swrm->rcmd_id, len, dev_addr, reg_addr);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530666 if (swrm->read) {
667 /* skip delay if read is handled in platform driver */
668 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
669 } else {
670 /* wait for FIFO RD to complete to avoid overflow */
671 usleep_range(100, 105);
672 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
673 /* wait for FIFO RD CMD complete to avoid overflow */
674 usleep_range(250, 255);
675 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530676retry_read:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530677 *cmd_data = swr_master_read(swrm, SWRM_CMD_FIFO_RD_FIFO_ADDR);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530678 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, rcmd_id: 0x%x, \
679 dev_num: 0x%x, cmd_data: 0x%x\n", __func__, reg_addr,
680 cmd_id, swrm->rcmd_id, dev_addr, *cmd_data);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530681 if ((((*cmd_data) & 0xF00) >> 8) != swrm->rcmd_id) {
682 if (retry_attempt < MAX_FIFO_RD_FAIL_RETRY) {
683 /* wait 500 us before retry on fifo read failure */
684 usleep_range(500, 505);
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +0530685 if (retry_attempt == (MAX_FIFO_RD_FAIL_RETRY - 1)) {
686 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
687 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
688 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530689 retry_attempt++;
690 goto retry_read;
691 } else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530692 dev_err_ratelimited(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, \
693 rcmd_id: 0x%x, dev_num: 0x%x, cmd_data: 0x%x\n",
694 __func__, reg_addr, cmd_id, swrm->rcmd_id,
695 dev_addr, *cmd_data);
696
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530697 dev_err_ratelimited(swrm->dev,
698 "%s: failed to read fifo\n", __func__);
699 }
700 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530701 mutex_unlock(&swrm->iolock);
702
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530703 return 0;
704}
705
706static int swrm_cmd_fifo_wr_cmd(struct swr_mstr_ctrl *swrm, u8 cmd_data,
707 u8 dev_addr, u8 cmd_id, u16 reg_addr)
708{
709 u32 val;
710 int ret = 0;
711
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530712 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530713 if (!cmd_id)
714 val = swrm_get_packed_reg_val(&swrm->wcmd_id, cmd_data,
715 dev_addr, reg_addr);
716 else
717 val = swrm_get_packed_reg_val(&cmd_id, cmd_data,
718 dev_addr, reg_addr);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530719 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x,wcmd_id: 0x%x, \
720 dev_num: 0x%x, cmd_data: 0x%x\n", __func__,
721 reg_addr, cmd_id, swrm->wcmd_id,dev_addr, cmd_data);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +0530722 swr_master_write(swrm, SWRM_CMD_FIFO_WR_CMD, val);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530723 /*
724 * wait for FIFO WR command to complete to avoid overflow
725 * skip delay if write is handled in platform driver.
726 */
727 if(!swrm->write)
Karthikeyan Mani5d52dd82019-08-15 16:58:08 -0700728 usleep_range(150, 155);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530729 if (cmd_id == 0xF) {
730 /*
731 * sleep for 10ms for MSM soundwire variant to allow broadcast
732 * command to complete.
733 */
734 if (swrm_is_msm_variant(swrm->version))
735 usleep_range(10000, 10100);
736 else
737 wait_for_completion_timeout(&swrm->broadcast,
738 (2 * HZ/10));
739 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530740 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530741 return ret;
742}
743
744static int swrm_read(struct swr_master *master, u8 dev_num, u16 reg_addr,
745 void *buf, u32 len)
746{
747 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
748 int ret = 0;
749 int val;
750 u8 *reg_val = (u8 *)buf;
751
752 if (!swrm) {
753 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
754 return -EINVAL;
755 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530756 if (!dev_num) {
757 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
758 return -EINVAL;
759 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530760 mutex_lock(&swrm->devlock);
761 if (!swrm->dev_up) {
762 mutex_unlock(&swrm->devlock);
763 return 0;
764 }
765 mutex_unlock(&swrm->devlock);
766
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530767 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530768 ret = swrm_cmd_fifo_rd_cmd(swrm, &val, dev_num, 0, reg_addr, len);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530769
770 if (!ret)
771 *reg_val = (u8)val;
772
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530773 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530774 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530775 return ret;
776}
777
778static int swrm_write(struct swr_master *master, u8 dev_num, u16 reg_addr,
779 const void *buf)
780{
781 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
782 int ret = 0;
783 u8 reg_val = *(u8 *)buf;
784
785 if (!swrm) {
786 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
787 return -EINVAL;
788 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530789 if (!dev_num) {
790 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
791 return -EINVAL;
792 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530793 mutex_lock(&swrm->devlock);
794 if (!swrm->dev_up) {
795 mutex_unlock(&swrm->devlock);
796 return 0;
797 }
798 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530799
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530800 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530801 ret = swrm_cmd_fifo_wr_cmd(swrm, reg_val, dev_num, 0, reg_addr);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530802
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530803 pm_runtime_put_autosuspend(swrm->dev);
804 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530805 return ret;
806}
807
808static int swrm_bulk_write(struct swr_master *master, u8 dev_num, void *reg,
809 const void *buf, size_t len)
810{
811 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
812 int ret = 0;
813 int i;
814 u32 *val;
815 u32 *swr_fifo_reg;
816
817 if (!swrm || !swrm->handle) {
818 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
819 return -EINVAL;
820 }
821 if (len <= 0)
822 return -EINVAL;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530823 mutex_lock(&swrm->devlock);
824 if (!swrm->dev_up) {
825 mutex_unlock(&swrm->devlock);
826 return 0;
827 }
828 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530829
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530830 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530831 if (dev_num) {
832 swr_fifo_reg = kcalloc(len, sizeof(u32), GFP_KERNEL);
833 if (!swr_fifo_reg) {
834 ret = -ENOMEM;
835 goto err;
836 }
837 val = kcalloc(len, sizeof(u32), GFP_KERNEL);
838 if (!val) {
839 ret = -ENOMEM;
840 goto mem_fail;
841 }
842
843 for (i = 0; i < len; i++) {
844 val[i] = swrm_get_packed_reg_val(&swrm->wcmd_id,
845 ((u8 *)buf)[i],
846 dev_num,
847 ((u16 *)reg)[i]);
848 swr_fifo_reg[i] = SWRM_CMD_FIFO_WR_CMD;
849 }
850 ret = swr_master_bulk_write(swrm, swr_fifo_reg, val, len);
851 if (ret) {
852 dev_err(&master->dev, "%s: bulk write failed\n",
853 __func__);
854 ret = -EINVAL;
855 }
856 } else {
857 dev_err(&master->dev,
858 "%s: No support of Bulk write for master regs\n",
859 __func__);
860 ret = -EINVAL;
861 goto err;
862 }
863 kfree(val);
864mem_fail:
865 kfree(swr_fifo_reg);
866err:
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530867 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530868 pm_runtime_mark_last_busy(swrm->dev);
869 return ret;
870}
871
872static u8 get_inactive_bank_num(struct swr_mstr_ctrl *swrm)
873{
874 return (swr_master_read(swrm, SWRM_MCP_STATUS) &
875 SWRM_MCP_STATUS_BANK_NUM_MASK) ? 0 : 1;
876}
877
878static void enable_bank_switch(struct swr_mstr_ctrl *swrm, u8 bank,
879 u8 row, u8 col)
880{
881 swrm_cmd_fifo_wr_cmd(swrm, ((row << 3) | col), 0xF, 0xF,
882 SWRS_SCP_FRAME_CTRL_BANK(bank));
883}
884
885static struct swr_port_info *swrm_get_port_req(struct swrm_mports *mport,
886 u8 slv_port, u8 dev_num)
887{
888 struct swr_port_info *port_req = NULL;
889
890 list_for_each_entry(port_req, &mport->port_req_list, list) {
891 /* Store dev_id instead of dev_num if enumeration is changed run_time */
892 if ((port_req->slave_port_id == slv_port)
893 && (port_req->dev_num == dev_num))
894 return port_req;
895 }
896 return NULL;
897}
898
899static bool swrm_remove_from_group(struct swr_master *master)
900{
901 struct swr_device *swr_dev;
902 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
903 bool is_removed = false;
904
905 if (!swrm)
906 goto end;
907
908 mutex_lock(&swrm->mlock);
909 if ((swrm->num_rx_chs > 1) &&
910 (swrm->num_rx_chs == swrm->num_cfg_devs)) {
911 list_for_each_entry(swr_dev, &master->devices,
912 dev_list) {
913 swr_dev->group_id = SWR_GROUP_NONE;
914 master->gr_sid = 0;
915 }
916 is_removed = true;
917 }
918 mutex_unlock(&swrm->mlock);
919
920end:
921 return is_removed;
922}
923
924static void swrm_disable_ports(struct swr_master *master,
925 u8 bank)
926{
927 u32 value;
928 struct swr_port_info *port_req;
929 int i;
930 struct swrm_mports *mport;
931 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
932
933 if (!swrm) {
934 pr_err("%s: swrm is null\n", __func__);
935 return;
936 }
937
938 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
939 master->num_port);
940
941
942 for (i = 0; i < SWR_MSTR_PORT_LEN ; i++) {
943
944 mport = &(swrm->mport_cfg[i]);
945 if (!mport->port_en)
946 continue;
947
948 list_for_each_entry(port_req, &mport->port_req_list, list) {
949 /* skip ports with no change req's*/
950 if (port_req->req_ch == port_req->ch_en)
951 continue;
952
953 swrm_cmd_fifo_wr_cmd(swrm, port_req->req_ch,
954 port_req->dev_num, 0x00,
955 SWRS_DP_CHANNEL_ENABLE_BANK(port_req->slave_port_id,
956 bank));
957 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x\n",
958 __func__, i,
959 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)));
960 }
961 value = ((mport->req_ch)
962 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
963 value |= ((mport->offset2)
964 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
965 value |= ((mport->offset1)
966 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
967 value |= mport->sinterval;
968
969 swr_master_write(swrm,
970 SWRM_DP_PORT_CTRL_BANK(i+1, bank),
971 value);
972 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
973 __func__, i,
974 (SWRM_DP_PORT_CTRL_BANK(i+1, bank)), value);
975 }
976}
977
978static void swrm_cleanup_disabled_port_reqs(struct swr_master *master)
979{
980 struct swr_port_info *port_req, *next;
981 int i;
982 struct swrm_mports *mport;
983 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
984
985 if (!swrm) {
986 pr_err("%s: swrm is null\n", __func__);
987 return;
988 }
989 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
990 master->num_port);
991
992 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
993 mport = &(swrm->mport_cfg[i]);
994 list_for_each_entry_safe(port_req, next,
995 &mport->port_req_list, list) {
996 /* skip ports without new ch req */
997 if (port_req->ch_en == port_req->req_ch)
998 continue;
999
1000 /* remove new ch req's*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +05301001 port_req->ch_en = port_req->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301002
1003 /* If no streams enabled on port, remove the port req */
1004 if (port_req->ch_en == 0) {
1005 list_del(&port_req->list);
1006 kfree(port_req);
1007 }
1008 }
1009 /* remove new ch req's on mport*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +05301010 mport->ch_en = mport->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301011
1012 if (!(mport->ch_en)) {
1013 mport->port_en = false;
1014 master->port_en_mask &= ~i;
1015 }
1016 }
1017}
1018static void swrm_copy_data_port_config(struct swr_master *master, u8 bank)
1019{
1020 u32 value, slv_id;
1021 struct swr_port_info *port_req;
1022 int i;
1023 struct swrm_mports *mport;
1024 u32 reg[SWRM_MAX_PORT_REG];
1025 u32 val[SWRM_MAX_PORT_REG];
1026 int len = 0;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301027 u8 hparams;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301028 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1029
1030 if (!swrm) {
1031 pr_err("%s: swrm is null\n", __func__);
1032 return;
1033 }
1034
1035 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
1036 master->num_port);
1037
1038 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
1039 mport = &(swrm->mport_cfg[i]);
1040 if (!mport->port_en)
1041 continue;
1042
1043 list_for_each_entry(port_req, &mport->port_req_list, list) {
1044 slv_id = port_req->slave_port_id;
1045 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1046 val[len++] = SWR_REG_VAL_PACK(port_req->req_ch,
1047 port_req->dev_num, 0x00,
1048 SWRS_DP_CHANNEL_ENABLE_BANK(slv_id,
1049 bank));
1050
1051 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1052 val[len++] = SWR_REG_VAL_PACK(mport->sinterval,
1053 port_req->dev_num, 0x00,
1054 SWRS_DP_SAMPLE_CONTROL_1_BANK(slv_id,
1055 bank));
1056
1057 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1058 val[len++] = SWR_REG_VAL_PACK(mport->offset1,
1059 port_req->dev_num, 0x00,
1060 SWRS_DP_OFFSET_CONTROL_1_BANK(slv_id,
1061 bank));
1062
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301063 if (mport->offset2 != SWR_INVALID_PARAM) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301064 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1065 val[len++] = SWR_REG_VAL_PACK(mport->offset2,
1066 port_req->dev_num, 0x00,
1067 SWRS_DP_OFFSET_CONTROL_2_BANK(
1068 slv_id, bank));
1069 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301070 if (mport->hstart != SWR_INVALID_PARAM
1071 && mport->hstop != SWR_INVALID_PARAM) {
1072 hparams = (mport->hstart << 4) | mport->hstop;
1073
1074 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1075 val[len++] = SWR_REG_VAL_PACK(hparams,
1076 port_req->dev_num, 0x00,
1077 SWRS_DP_HCONTROL_BANK(slv_id,
1078 bank));
1079 }
1080 if (mport->word_length != SWR_INVALID_PARAM) {
1081 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1082 val[len++] =
1083 SWR_REG_VAL_PACK(mport->word_length,
1084 port_req->dev_num, 0x00,
1085 SWRS_DP_BLOCK_CONTROL_1(slv_id));
1086 }
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +05301087 if (mport->blk_pack_mode != SWR_INVALID_PARAM
1088 && swrm->master_id != MASTER_ID_WSA) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301089 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1090 val[len++] =
1091 SWR_REG_VAL_PACK(mport->blk_pack_mode,
1092 port_req->dev_num, 0x00,
1093 SWRS_DP_BLOCK_CONTROL_3_BANK(slv_id,
1094 bank));
1095 }
1096 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
1097 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1098 val[len++] =
1099 SWR_REG_VAL_PACK(mport->blk_grp_count,
1100 port_req->dev_num, 0x00,
1101 SWRS_DP_BLOCK_CONTROL_2_BANK(slv_id,
1102 bank));
1103 }
1104 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
1105 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1106 val[len++] =
1107 SWR_REG_VAL_PACK(mport->lane_ctrl,
1108 port_req->dev_num, 0x00,
1109 SWRS_DP_LANE_CONTROL_BANK(slv_id,
1110 bank));
1111 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301112 port_req->ch_en = port_req->req_ch;
1113 }
1114 value = ((mport->req_ch)
1115 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +05301116
1117 if (mport->offset2 != SWR_INVALID_PARAM)
1118 value |= ((mport->offset2)
1119 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301120 value |= ((mport->offset1)
1121 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
1122 value |= mport->sinterval;
1123
1124
1125 reg[len] = SWRM_DP_PORT_CTRL_BANK(i + 1, bank);
1126 val[len++] = value;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301127 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
1128 __func__, i,
1129 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)), value);
1130
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301131 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
1132 reg[len] = SWRM_DP_PORT_CTRL_2_BANK(i + 1, bank);
1133 val[len++] = mport->lane_ctrl;
1134 }
1135 if (mport->word_length != SWR_INVALID_PARAM) {
1136 reg[len] = SWRM_DP_BLOCK_CTRL_1(i + 1);
1137 val[len++] = mport->word_length;
1138 }
1139
1140 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
1141 reg[len] = SWRM_DP_BLOCK_CTRL2_BANK(i + 1, bank);
1142 val[len++] = mport->blk_grp_count;
1143 }
1144 if (mport->hstart != SWR_INVALID_PARAM
1145 && mport->hstop != SWR_INVALID_PARAM) {
1146 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
Laxminath Kasame30eef72018-11-05 17:40:09 +05301147 hparams = (mport->hstop << 4) | mport->hstart;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301148 val[len++] = hparams;
Laxminath Kasam990c70b2018-11-09 23:15:09 +05301149 } else {
1150 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
1151 hparams = (SWR_HSTOP_MAX_VAL << 4) | SWR_HSTART_MIN_VAL;
1152 val[len++] = hparams;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301153 }
1154 if (mport->blk_pack_mode != SWR_INVALID_PARAM) {
1155 reg[len] = SWRM_DP_BLOCK_CTRL3_BANK(i + 1, bank);
1156 val[len++] = mport->blk_pack_mode;
1157 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301158 mport->ch_en = mport->req_ch;
1159
1160 }
Sudheer Papothi0016db12019-06-11 04:42:38 +05301161 swrm_reg_dump(swrm, reg, val, len, __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301162 swr_master_bulk_write(swrm, reg, val, len);
1163}
1164
1165static void swrm_apply_port_config(struct swr_master *master)
1166{
1167 u8 bank;
1168 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1169
1170 if (!swrm) {
1171 pr_err("%s: Invalid handle to swr controller\n",
1172 __func__);
1173 return;
1174 }
1175
1176 bank = get_inactive_bank_num(swrm);
1177 dev_dbg(swrm->dev, "%s: enter bank: %d master_ports: %d\n",
1178 __func__, bank, master->num_port);
1179
1180
1181 swrm_cmd_fifo_wr_cmd(swrm, 0x01, 0xF, 0x00,
1182 SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(bank));
1183
1184 swrm_copy_data_port_config(master, bank);
1185}
1186
1187static int swrm_slvdev_datapath_control(struct swr_master *master, bool enable)
1188{
1189 u8 bank;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301190 u32 value, n_row, n_col;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301191 u32 row = 0, col = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301192 int ret;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301193 u8 ssp_period = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301194 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1195 int mask = (SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK |
1196 SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK |
1197 SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_BMSK);
1198 u8 inactive_bank;
1199
1200 if (!swrm) {
1201 pr_err("%s: swrm is null\n", __func__);
1202 return -EFAULT;
1203 }
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301204
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301205 mutex_lock(&swrm->mlock);
1206
Ramprasad Katkam979b7c92019-05-17 15:31:21 +05301207 /*
1208 * During disable if master is already down, which implies an ssr/pdr
1209 * scenario, just mark ports as disabled and exit
1210 */
1211 if (swrm->state == SWR_MSTR_SSR && !enable) {
1212 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1213 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1214 __func__);
1215 goto exit;
1216 }
1217 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
1218 swrm_cleanup_disabled_port_reqs(master);
1219 if (!swrm_is_port_en(master)) {
1220 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1221 __func__);
1222 pm_runtime_mark_last_busy(swrm->dev);
1223 pm_runtime_put_autosuspend(swrm->dev);
1224 }
1225 goto exit;
1226 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301227 bank = get_inactive_bank_num(swrm);
1228
1229 if (enable) {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301230 if (!test_bit(ENABLE_PENDING, &swrm->port_req_pending)) {
1231 dev_dbg(swrm->dev, "%s:No pending connect port req\n",
1232 __func__);
1233 goto exit;
1234 }
1235 clear_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301236 ret = swrm_get_port_config(swrm);
1237 if (ret) {
1238 /* cannot accommodate ports */
1239 swrm_cleanup_disabled_port_reqs(master);
1240 mutex_unlock(&swrm->mlock);
1241 return -EINVAL;
1242 }
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301243 swr_master_write(swrm, SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301244 SWRM_INTERRUPT_STATUS_MASK);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301245 /* apply the new port config*/
1246 swrm_apply_port_config(master);
1247 } else {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301248 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1249 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1250 __func__);
1251 goto exit;
1252 }
1253 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301254 swrm_disable_ports(master, bank);
1255 }
1256 dev_dbg(swrm->dev, "%s: enable: %d, cfg_devs: %d\n",
1257 __func__, enable, swrm->num_cfg_devs);
1258
1259 if (enable) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301260 /* set col = 16 */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301261 n_col = SWR_MAX_COL;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301262 col = SWRM_COL_16;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301263 } else {
1264 /*
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301265 * Do not change to col = 2 if there are still active ports
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301266 */
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301267 if (!master->num_port) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301268 n_col = SWR_MIN_COL;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301269 col = SWRM_COL_02;
1270 } else {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301271 n_col = SWR_MAX_COL;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301272 col = SWRM_COL_16;
1273 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301274 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301275 /* Use default 50 * x, frame shape. Change based on mclk */
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301276 if (swrm->mclk_freq == MCLK_FREQ_NATIVE) {
1277 dev_dbg(swrm->dev, "setting 64 x %d frameshape\n",
1278 n_col ? 16 : 2);
1279 n_row = SWR_ROW_64;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301280 row = SWRM_ROW_64;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301281 } else {
1282 dev_dbg(swrm->dev, "setting 50 x %d frameshape\n",
1283 n_col ? 16 : 2);
1284 n_row = SWR_ROW_50;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301285 row = SWRM_ROW_50;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301286 }
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301287 ssp_period = swrm_get_ssp_period(swrm, row, col, SWRM_FRAME_SYNC_SEL);
1288 dev_dbg(swrm->dev, "%s: ssp_period: %d\n", __func__, ssp_period);
1289
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301290 value = swr_master_read(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank));
1291 value &= (~mask);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301292 value |= ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301293 (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301294 ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301295 swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1296
1297 dev_dbg(swrm->dev, "%s: regaddr: 0x%x, value: 0x%x\n", __func__,
1298 SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1299
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301300 enable_bank_switch(swrm, bank, n_row, n_col);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301301 inactive_bank = bank ? 0 : 1;
1302
1303 if (enable)
1304 swrm_copy_data_port_config(master, inactive_bank);
1305 else {
1306 swrm_disable_ports(master, inactive_bank);
1307 swrm_cleanup_disabled_port_reqs(master);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301308 }
1309 if (!swrm_is_port_en(master)) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301310 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1311 __func__);
1312 pm_runtime_mark_last_busy(swrm->dev);
1313 pm_runtime_put_autosuspend(swrm->dev);
1314 }
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301315exit:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301316 mutex_unlock(&swrm->mlock);
1317return 0;
1318}
1319
1320static int swrm_connect_port(struct swr_master *master,
1321 struct swr_params *portinfo)
1322{
1323 int i;
1324 struct swr_port_info *port_req;
1325 int ret = 0;
1326 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1327 struct swrm_mports *mport;
1328 u8 mstr_port_id, mstr_ch_msk;
1329
1330 dev_dbg(&master->dev, "%s: enter\n", __func__);
1331 if (!portinfo)
1332 return -EINVAL;
1333
1334 if (!swrm) {
1335 dev_err(&master->dev,
1336 "%s: Invalid handle to swr controller\n",
1337 __func__);
1338 return -EINVAL;
1339 }
1340
1341 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301342 mutex_lock(&swrm->devlock);
1343 if (!swrm->dev_up) {
1344 mutex_unlock(&swrm->devlock);
1345 mutex_unlock(&swrm->mlock);
1346 return -EINVAL;
1347 }
1348 mutex_unlock(&swrm->devlock);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301349 if (!swrm_is_port_en(master))
1350 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301351
1352 for (i = 0; i < portinfo->num_port; i++) {
1353 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_msk,
1354 portinfo->port_type[i],
1355 portinfo->port_id[i]);
1356 if (ret) {
1357 dev_err(&master->dev,
1358 "%s: mstr portid for slv port %d not found\n",
1359 __func__, portinfo->port_id[i]);
1360 goto port_fail;
1361 }
1362
1363 mport = &(swrm->mport_cfg[mstr_port_id]);
1364 /* get port req */
1365 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1366 portinfo->dev_num);
1367 if (!port_req) {
1368 dev_dbg(&master->dev, "%s: new req:port id %d dev %d\n",
1369 __func__, portinfo->port_id[i],
1370 portinfo->dev_num);
1371 port_req = kzalloc(sizeof(struct swr_port_info),
1372 GFP_KERNEL);
1373 if (!port_req) {
1374 ret = -ENOMEM;
1375 goto mem_fail;
1376 }
1377 port_req->dev_num = portinfo->dev_num;
1378 port_req->slave_port_id = portinfo->port_id[i];
1379 port_req->num_ch = portinfo->num_ch[i];
1380 port_req->ch_rate = portinfo->ch_rate[i];
1381 port_req->ch_en = 0;
1382 port_req->master_port_id = mstr_port_id;
1383 list_add(&port_req->list, &mport->port_req_list);
1384 }
1385 port_req->req_ch |= portinfo->ch_en[i];
1386
1387 dev_dbg(&master->dev,
1388 "%s: mstr port %d, slv port %d ch_rate %d num_ch %d\n",
1389 __func__, port_req->master_port_id,
1390 port_req->slave_port_id, port_req->ch_rate,
1391 port_req->num_ch);
1392 /* Put the port req on master port */
1393 mport = &(swrm->mport_cfg[mstr_port_id]);
1394 mport->port_en = true;
1395 mport->req_ch |= mstr_ch_msk;
1396 master->port_en_mask |= (1 << mstr_port_id);
1397 }
1398 master->num_port += portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301399 set_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301400 swr_port_response(master, portinfo->tid);
1401
1402 mutex_unlock(&swrm->mlock);
1403 return 0;
1404
1405port_fail:
1406mem_fail:
1407 /* cleanup port reqs in error condition */
1408 swrm_cleanup_disabled_port_reqs(master);
1409 mutex_unlock(&swrm->mlock);
1410 return ret;
1411}
1412
1413static int swrm_disconnect_port(struct swr_master *master,
1414 struct swr_params *portinfo)
1415{
1416 int i, ret = 0;
1417 struct swr_port_info *port_req;
1418 struct swrm_mports *mport;
1419 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1420 u8 mstr_port_id, mstr_ch_mask;
1421
1422 if (!swrm) {
1423 dev_err(&master->dev,
1424 "%s: Invalid handle to swr controller\n",
1425 __func__);
1426 return -EINVAL;
1427 }
1428
1429 if (!portinfo) {
1430 dev_err(&master->dev, "%s: portinfo is NULL\n", __func__);
1431 return -EINVAL;
1432 }
1433 mutex_lock(&swrm->mlock);
1434
1435 for (i = 0; i < portinfo->num_port; i++) {
1436
1437 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_mask,
1438 portinfo->port_type[i], portinfo->port_id[i]);
1439 if (ret) {
1440 dev_err(&master->dev,
1441 "%s: mstr portid for slv port %d not found\n",
1442 __func__, portinfo->port_id[i]);
1443 mutex_unlock(&swrm->mlock);
1444 return -EINVAL;
1445 }
1446 mport = &(swrm->mport_cfg[mstr_port_id]);
1447 /* get port req */
1448 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1449 portinfo->dev_num);
1450
1451 if (!port_req) {
1452 dev_err(&master->dev, "%s:port not enabled : port %d\n",
1453 __func__, portinfo->port_id[i]);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05301454 mutex_unlock(&swrm->mlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301455 return -EINVAL;
1456 }
1457 port_req->req_ch &= ~portinfo->ch_en[i];
1458 mport->req_ch &= ~mstr_ch_mask;
1459 }
1460 master->num_port -= portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301461 set_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301462 swr_port_response(master, portinfo->tid);
1463 mutex_unlock(&swrm->mlock);
1464
1465 return 0;
1466}
1467
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301468static int swrm_find_alert_slave(struct swr_mstr_ctrl *swrm,
1469 int status, u8 *devnum)
1470{
1471 int i;
1472 bool found = false;
1473
1474 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1475 if ((status & SWRM_MCP_SLV_STATUS_MASK) == SWR_ALERT) {
1476 *devnum = i;
1477 found = true;
1478 break;
1479 }
1480 status >>= 2;
1481 }
1482 if (found)
1483 return 0;
1484 else
1485 return -EINVAL;
1486}
1487
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301488static void swrm_enable_slave_irq(struct swr_mstr_ctrl *swrm)
1489{
1490 int i;
1491 int status = 0;
1492
1493 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1494 if (!status) {
1495 dev_dbg_ratelimited(swrm->dev, "%s: slaves status is 0x%x\n",
1496 __func__, status);
1497 return;
1498 }
1499 dev_dbg(swrm->dev, "%s: slave status: 0x%x\n", __func__, status);
1500 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1501 if (status & SWRM_MCP_SLV_STATUS_MASK)
1502 swrm_cmd_fifo_wr_cmd(swrm, 0x4, i, 0x0,
1503 SWRS_SCP_INT_STATUS_MASK_1);
1504 status >>= 2;
1505 }
1506}
1507
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301508static int swrm_check_slave_change_status(struct swr_mstr_ctrl *swrm,
1509 int status, u8 *devnum)
1510{
1511 int i;
1512 int new_sts = status;
1513 int ret = SWR_NOT_PRESENT;
1514
1515 if (status != swrm->slave_status) {
1516 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1517 if ((status & SWRM_MCP_SLV_STATUS_MASK) !=
1518 (swrm->slave_status & SWRM_MCP_SLV_STATUS_MASK)) {
1519 ret = (status & SWRM_MCP_SLV_STATUS_MASK);
1520 *devnum = i;
1521 break;
1522 }
1523 status >>= 2;
1524 swrm->slave_status >>= 2;
1525 }
1526 swrm->slave_status = new_sts;
1527 }
1528 return ret;
1529}
1530
1531static irqreturn_t swr_mstr_interrupt(int irq, void *dev)
1532{
1533 struct swr_mstr_ctrl *swrm = dev;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301534 u32 value, intr_sts, intr_sts_masked;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301535 u32 temp = 0;
1536 u32 status, chg_sts, i;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301537 u8 devnum = 0;
1538 int ret = IRQ_HANDLED;
1539 struct swr_device *swr_dev;
1540 struct swr_master *mstr = &swrm->master;
1541
Ramprasad Katkam57349872018-11-11 18:34:57 +05301542 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1543 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1544 return IRQ_NONE;
1545 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301546
1547 mutex_lock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301548 if (swrm_clk_request(swrm, true)) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +05301549 dev_err_ratelimited(swrm->dev, "%s:clk request failed\n",
1550 __func__);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301551 mutex_unlock(&swrm->reslock);
1552 goto exit;
1553 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301554 mutex_unlock(&swrm->reslock);
1555
1556 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301557 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301558handle_irq:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301559 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301560 value = intr_sts_masked & (1 << i);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301561 if (!value)
1562 continue;
1563
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301564 switch (value) {
1565 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1566 dev_dbg(swrm->dev, "Trigger irq to slave device\n");
1567 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301568 ret = swrm_find_alert_slave(swrm, status, &devnum);
1569 if (ret) {
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301570 dev_err_ratelimited(swrm->dev,
1571 "no slave alert found.spurious interrupt\n");
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05301572 break;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301573 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301574 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1575 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1576 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1577 SWRS_SCP_INT_STATUS_CLEAR_1);
1578 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1579 SWRS_SCP_INT_STATUS_CLEAR_1);
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301580
1581
1582 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1583 if (swr_dev->dev_num != devnum)
1584 continue;
1585 if (swr_dev->slave_irq) {
1586 do {
Ramprasad Katkam2586a4b2019-03-18 16:53:39 +05301587 swr_dev->slave_irq_pending = 0;
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301588 handle_nested_irq(
1589 irq_find_mapping(
1590 swr_dev->slave_irq, 0));
1591 } while (swr_dev->slave_irq_pending);
1592 }
1593
1594 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301595 break;
1596 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1597 dev_dbg(swrm->dev, "SWR new slave attached\n");
1598 break;
1599 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1600 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1601 if (status == swrm->slave_status) {
1602 dev_dbg(swrm->dev,
1603 "%s: No change in slave status: %d\n",
1604 __func__, status);
1605 break;
1606 }
1607 chg_sts = swrm_check_slave_change_status(swrm, status,
1608 &devnum);
1609 switch (chg_sts) {
1610 case SWR_NOT_PRESENT:
1611 dev_dbg(swrm->dev, "device %d got detached\n",
1612 devnum);
1613 break;
1614 case SWR_ATTACHED_OK:
1615 dev_dbg(swrm->dev, "device %d got attached\n",
1616 devnum);
Ramprasad Katkamdebe8932018-09-25 18:08:18 +05301617 /* enable host irq from slave device*/
1618 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1619 SWRS_SCP_INT_STATUS_CLEAR_1);
1620 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1621 SWRS_SCP_INT_STATUS_MASK_1);
1622
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301623 break;
1624 case SWR_ALERT:
1625 dev_dbg(swrm->dev,
1626 "device %d has pending interrupt\n",
1627 devnum);
1628 break;
1629 }
1630 break;
1631 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1632 dev_err_ratelimited(swrm->dev,
1633 "SWR bus clsh detected\n");
1634 break;
1635 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1636 dev_dbg(swrm->dev, "SWR read FIFO overflow\n");
1637 break;
1638 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1639 dev_dbg(swrm->dev, "SWR read FIFO underflow\n");
1640 break;
1641 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1642 dev_dbg(swrm->dev, "SWR write FIFO overflow\n");
1643 break;
1644 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1645 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1646 dev_err_ratelimited(swrm->dev,
1647 "SWR CMD error, fifo status 0x%x, flushing fifo\n",
1648 value);
1649 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1650 break;
1651 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301652 dev_err_ratelimited(swrm->dev, "SWR Port collision detected\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301653 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301654 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301655 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301656 break;
1657 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1658 dev_dbg(swrm->dev, "SWR read enable valid mismatch\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301659 swrm->intr_mask &=
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301660 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1661 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301662 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301663 break;
1664 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1665 complete(&swrm->broadcast);
1666 dev_dbg(swrm->dev, "SWR cmd id finished\n");
1667 break;
1668 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_AUTO_ENUM_FINISHED:
1669 break;
1670 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED:
1671 break;
1672 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL:
1673 break;
1674 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED:
1675 complete(&swrm->reset);
1676 break;
1677 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED:
1678 break;
1679 default:
1680 dev_err_ratelimited(swrm->dev,
1681 "SWR unknown interrupt\n");
1682 ret = IRQ_NONE;
1683 break;
1684 }
1685 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301686 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1687 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
Ramprasad Katkam83303512018-10-11 17:34:22 +05301688
1689 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301690 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301691
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301692 if (intr_sts_masked) {
Ramprasad Katkam83303512018-10-11 17:34:22 +05301693 dev_dbg(swrm->dev, "%s: new interrupt received\n", __func__);
1694 goto handle_irq;
1695 }
1696
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301697 mutex_lock(&swrm->reslock);
1698 swrm_clk_request(swrm, false);
1699 mutex_unlock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301700exit:
Ramprasad Katkam57349872018-11-11 18:34:57 +05301701 swrm_unlock_sleep(swrm);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301702 return ret;
1703}
1704
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301705static irqreturn_t swr_mstr_interrupt_v2(int irq, void *dev)
1706{
1707 struct swr_mstr_ctrl *swrm = dev;
1708 u32 value, intr_sts, intr_sts_masked;
1709 u32 temp = 0;
1710 u32 status, chg_sts, i;
1711 u8 devnum = 0;
1712 int ret = IRQ_HANDLED;
1713 struct swr_device *swr_dev;
1714 struct swr_master *mstr = &swrm->master;
1715
1716 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1717 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1718 return IRQ_NONE;
1719 }
1720
1721 mutex_lock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301722 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
1723 ret = IRQ_NONE;
1724 goto exit;
1725 }
1726 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
1727 ret = IRQ_NONE;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301728 goto err_audio_hw_vote;
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001729 }
Karthikeyan Mani4bee1db2019-09-18 17:58:41 -07001730 ret = swrm_clk_request(swrm, true);
1731 if (ret) {
1732 dev_err(dev, "%s: swrm clk failed\n", __func__);
1733 ret = IRQ_NONE;
1734 goto err_audio_core_vote;
1735 }
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301736 mutex_unlock(&swrm->reslock);
1737
1738 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1739 intr_sts_masked = intr_sts & swrm->intr_mask;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301740
1741 dev_dbg(swrm->dev, "%s: status: 0x%x \n", __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301742handle_irq:
1743 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
1744 value = intr_sts_masked & (1 << i);
1745 if (!value)
1746 continue;
1747
1748 switch (value) {
1749 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1750 dev_dbg(swrm->dev, "%s: Trigger irq to slave device\n",
1751 __func__);
1752 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1753 ret = swrm_find_alert_slave(swrm, status, &devnum);
1754 if (ret) {
1755 dev_err_ratelimited(swrm->dev,
1756 "%s: no slave alert found.spurious interrupt\n",
1757 __func__);
1758 break;
1759 }
1760 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1761 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1762 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1763 SWRS_SCP_INT_STATUS_CLEAR_1);
1764 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1765 SWRS_SCP_INT_STATUS_CLEAR_1);
1766
1767
1768 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1769 if (swr_dev->dev_num != devnum)
1770 continue;
1771 if (swr_dev->slave_irq) {
1772 do {
1773 handle_nested_irq(
1774 irq_find_mapping(
1775 swr_dev->slave_irq, 0));
1776 } while (swr_dev->slave_irq_pending);
1777 }
1778
1779 }
1780 break;
1781 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1782 dev_dbg(swrm->dev, "%s: SWR new slave attached\n",
1783 __func__);
1784 break;
1785 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1786 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
Laxminath Kasam44cedb82019-11-20 17:37:23 +05301787 swrm_enable_slave_irq(swrm);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301788 if (status == swrm->slave_status) {
1789 dev_dbg(swrm->dev,
1790 "%s: No change in slave status: %d\n",
1791 __func__, status);
1792 break;
1793 }
1794 chg_sts = swrm_check_slave_change_status(swrm, status,
1795 &devnum);
1796 switch (chg_sts) {
1797 case SWR_NOT_PRESENT:
1798 dev_dbg(swrm->dev,
1799 "%s: device %d got detached\n",
1800 __func__, devnum);
1801 break;
1802 case SWR_ATTACHED_OK:
1803 dev_dbg(swrm->dev,
1804 "%s: device %d got attached\n",
1805 __func__, devnum);
1806 /* enable host irq from slave device*/
1807 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1808 SWRS_SCP_INT_STATUS_CLEAR_1);
1809 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1810 SWRS_SCP_INT_STATUS_MASK_1);
1811
1812 break;
1813 case SWR_ALERT:
1814 dev_dbg(swrm->dev,
1815 "%s: device %d has pending interrupt\n",
1816 __func__, devnum);
1817 break;
1818 }
1819 break;
1820 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1821 dev_err_ratelimited(swrm->dev,
1822 "%s: SWR bus clsh detected\n",
1823 __func__);
1824 break;
1825 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1826 dev_dbg(swrm->dev, "%s: SWR read FIFO overflow\n",
1827 __func__);
1828 break;
1829 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1830 dev_dbg(swrm->dev, "%s: SWR read FIFO underflow\n",
1831 __func__);
1832 break;
1833 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1834 dev_dbg(swrm->dev, "%s: SWR write FIFO overflow\n",
1835 __func__);
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301836 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301837 break;
1838 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1839 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1840 dev_err_ratelimited(swrm->dev,
1841 "%s: SWR CMD error, fifo status 0x%x, flushing fifo\n",
1842 __func__, value);
1843 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1844 break;
1845 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
1846 dev_err_ratelimited(swrm->dev,
1847 "%s: SWR Port collision detected\n",
1848 __func__);
1849 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
1850 swr_master_write(swrm,
1851 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1852 break;
1853 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1854 dev_dbg(swrm->dev,
1855 "%s: SWR read enable valid mismatch\n",
1856 __func__);
1857 swrm->intr_mask &=
1858 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1859 swr_master_write(swrm,
1860 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1861 break;
1862 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1863 complete(&swrm->broadcast);
1864 dev_dbg(swrm->dev, "%s: SWR cmd id finished\n",
1865 __func__);
1866 break;
1867 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED_V2:
1868 break;
1869 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL_V2:
1870 break;
1871 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED_V2:
Laxminath Kasame2291972019-11-08 14:51:59 +05301872 swrm_check_link_status(swrm, 0x1);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301873 break;
1874 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED_V2:
1875 break;
1876 case SWRM_INTERRUPT_STATUS_EXT_CLK_STOP_WAKEUP:
1877 if (swrm->state == SWR_MSTR_UP)
1878 dev_dbg(swrm->dev,
1879 "%s:SWR Master is already up\n",
1880 __func__);
1881 else
1882 dev_err_ratelimited(swrm->dev,
1883 "%s: SWR wokeup during clock stop\n",
1884 __func__);
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301885 /* It might be possible the slave device gets reset
1886 * and slave interrupt gets missed. So re-enable
1887 * Host IRQ and process slave pending
1888 * interrupts, if any.
1889 */
1890 swrm_enable_slave_irq(swrm);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301891 break;
1892 default:
1893 dev_err_ratelimited(swrm->dev,
1894 "%s: SWR unknown interrupt value: %d\n",
1895 __func__, value);
1896 ret = IRQ_NONE;
1897 break;
1898 }
1899 }
1900 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1901 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
1902
1903 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1904 intr_sts_masked = intr_sts & swrm->intr_mask;
1905
1906 if (intr_sts_masked) {
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301907 dev_dbg(swrm->dev, "%s: new interrupt received 0x%x\n",
1908 __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301909 goto handle_irq;
1910 }
1911
1912 mutex_lock(&swrm->reslock);
1913 swrm_clk_request(swrm, false);
Karthikeyan Mani4bee1db2019-09-18 17:58:41 -07001914err_audio_core_vote:
Sudheer Papothi384addd2019-06-14 02:26:52 +05301915 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
Sudheer Papothi06f43412019-07-09 03:32:54 +05301916
1917err_audio_hw_vote:
Sudheer Papothi384addd2019-06-14 02:26:52 +05301918 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001919exit:
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301920 mutex_unlock(&swrm->reslock);
1921 swrm_unlock_sleep(swrm);
1922 return ret;
1923}
1924
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301925static irqreturn_t swrm_wakeup_interrupt(int irq, void *dev)
1926{
1927 struct swr_mstr_ctrl *swrm = dev;
1928 int ret = IRQ_HANDLED;
1929
1930 if (!swrm || !(swrm->dev)) {
1931 pr_err("%s: swrm or dev is null\n", __func__);
1932 return IRQ_NONE;
1933 }
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05301934
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301935 mutex_lock(&swrm->devlock);
1936 if (!swrm->dev_up) {
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05301937 if (swrm->wake_irq > 0) {
1938 if (unlikely(!irq_get_irq_data(swrm->wake_irq))) {
1939 pr_err("%s: irq data is NULL\n", __func__);
1940 mutex_unlock(&swrm->devlock);
1941 return IRQ_NONE;
1942 }
1943 mutex_lock(&swrm->irq_lock);
1944 if (!irqd_irq_disabled(
1945 irq_get_irq_data(swrm->wake_irq)))
1946 disable_irq_nosync(swrm->wake_irq);
1947 mutex_unlock(&swrm->irq_lock);
1948 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301949 mutex_unlock(&swrm->devlock);
1950 return ret;
1951 }
1952 mutex_unlock(&swrm->devlock);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301953 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1954 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1955 goto exit;
1956 }
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05301957 if (swrm->wake_irq > 0) {
1958 if (unlikely(!irq_get_irq_data(swrm->wake_irq))) {
1959 pr_err("%s: irq data is NULL\n", __func__);
1960 return IRQ_NONE;
1961 }
1962 mutex_lock(&swrm->irq_lock);
1963 if (!irqd_irq_disabled(
1964 irq_get_irq_data(swrm->wake_irq)))
1965 disable_irq_nosync(swrm->wake_irq);
1966 mutex_unlock(&swrm->irq_lock);
1967 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301968 pm_runtime_get_sync(swrm->dev);
1969 pm_runtime_mark_last_busy(swrm->dev);
1970 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301971 swrm_unlock_sleep(swrm);
1972exit:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301973 return ret;
1974}
1975
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301976static void swrm_wakeup_work(struct work_struct *work)
1977{
1978 struct swr_mstr_ctrl *swrm;
1979
1980 swrm = container_of(work, struct swr_mstr_ctrl,
1981 wakeup_work);
1982 if (!swrm || !(swrm->dev)) {
1983 pr_err("%s: swrm or dev is null\n", __func__);
1984 return;
1985 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301986
1987 mutex_lock(&swrm->devlock);
1988 if (!swrm->dev_up) {
1989 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301990 goto exit;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301991 }
1992 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301993 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1994 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1995 goto exit;
1996 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301997 pm_runtime_get_sync(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301998 pm_runtime_mark_last_busy(swrm->dev);
1999 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302000 swrm_unlock_sleep(swrm);
2001exit:
2002 pm_relax(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302003}
2004
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302005static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
2006{
2007 u32 val;
2008
2009 swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
2010 val = (swrm->slave_status >> (devnum * 2));
2011 val &= SWRM_MCP_SLV_STATUS_MASK;
2012 return val;
2013}
2014
2015static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
2016 u8 *dev_num)
2017{
2018 int i;
2019 u64 id = 0;
2020 int ret = -EINVAL;
2021 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
2022 struct swr_device *swr_dev;
2023 u32 num_dev = 0;
2024
2025 if (!swrm) {
2026 pr_err("%s: Invalid handle to swr controller\n",
2027 __func__);
2028 return ret;
2029 }
2030 if (swrm->num_dev)
2031 num_dev = swrm->num_dev;
2032 else
2033 num_dev = mstr->num_dev;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302034
2035 mutex_lock(&swrm->devlock);
2036 if (!swrm->dev_up) {
2037 mutex_unlock(&swrm->devlock);
2038 return ret;
2039 }
2040 mutex_unlock(&swrm->devlock);
2041
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302042 pm_runtime_get_sync(swrm->dev);
2043 for (i = 1; i < (num_dev + 1); i++) {
2044 id = ((u64)(swr_master_read(swrm,
2045 SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
2046 id |= swr_master_read(swrm,
2047 SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302048
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302049 /*
2050 * As pm_runtime_get_sync() brings all slaves out of reset
2051 * update logical device number for all slaves.
2052 */
2053 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2054 if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
2055 u32 status = swrm_get_device_status(swrm, i);
2056
2057 if ((status == 0x01) || (status == 0x02)) {
2058 swr_dev->dev_num = i;
2059 if ((id & SWR_DEV_ID_MASK) == dev_id) {
2060 *dev_num = i;
2061 ret = 0;
2062 }
2063 dev_dbg(swrm->dev,
2064 "%s: devnum %d is assigned for dev addr %lx\n",
2065 __func__, i, swr_dev->addr);
2066 }
2067 }
2068 }
2069 }
2070 if (ret)
2071 dev_err(swrm->dev, "%s: device 0x%llx is not ready\n",
2072 __func__, dev_id);
2073
2074 pm_runtime_mark_last_busy(swrm->dev);
2075 pm_runtime_put_autosuspend(swrm->dev);
2076 return ret;
2077}
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302078
2079static void swrm_device_wakeup_vote(struct swr_master *mstr)
2080{
2081 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
2082
2083 if (!swrm) {
2084 pr_err("%s: Invalid handle to swr controller\n",
2085 __func__);
2086 return;
2087 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302088 if (unlikely(swrm_lock_sleep(swrm) == false)) {
2089 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
2090 return;
2091 }
Sudheer Papothi384addd2019-06-14 02:26:52 +05302092 if (++swrm->hw_core_clk_en == 1)
2093 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2094 dev_err(swrm->dev, "%s:lpass core hw enable failed\n",
2095 __func__);
2096 --swrm->hw_core_clk_en;
2097 }
2098 if ( ++swrm->aud_core_clk_en == 1)
2099 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2100 dev_err(swrm->dev, "%s:lpass audio hw enable failed\n",
2101 __func__);
2102 --swrm->aud_core_clk_en;
2103 }
2104 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
2105 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302106 pm_runtime_get_sync(swrm->dev);
2107}
2108
2109static void swrm_device_wakeup_unvote(struct swr_master *mstr)
2110{
2111 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
2112
2113 if (!swrm) {
2114 pr_err("%s: Invalid handle to swr controller\n",
2115 __func__);
2116 return;
2117 }
2118 pm_runtime_mark_last_busy(swrm->dev);
2119 pm_runtime_put_autosuspend(swrm->dev);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302120 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
2121 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
2122
2123 --swrm->aud_core_clk_en;
2124 if (swrm->aud_core_clk_en < 0)
2125 swrm->aud_core_clk_en = 0;
2126 else if (swrm->aud_core_clk_en == 0)
2127 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2128
2129 --swrm->hw_core_clk_en;
2130 if (swrm->hw_core_clk_en < 0)
2131 swrm->hw_core_clk_en = 0;
2132 else if (swrm->hw_core_clk_en == 0)
2133 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
2134
Ramprasad Katkam57349872018-11-11 18:34:57 +05302135 swrm_unlock_sleep(swrm);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302136}
2137
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302138static int swrm_master_init(struct swr_mstr_ctrl *swrm)
2139{
2140 int ret = 0;
2141 u32 val;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302142 u8 row_ctrl = SWR_ROW_50;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302143 u8 col_ctrl = SWR_MIN_COL;
2144 u8 ssp_period = 1;
2145 u8 retry_cmd_num = 3;
2146 u32 reg[SWRM_MAX_INIT_REG];
2147 u32 value[SWRM_MAX_INIT_REG];
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302148 u32 temp = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302149 int len = 0;
2150
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302151 ssp_period = swrm_get_ssp_period(swrm, SWRM_ROW_50,
2152 SWRM_COL_02, SWRM_FRAME_SYNC_SEL);
2153 dev_dbg(swrm->dev, "%s: ssp_period: %d\n", __func__, ssp_period);
2154
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302155 /* Clear Rows and Cols */
2156 val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
2157 (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302158 ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302159
2160 reg[len] = SWRM_MCP_FRAME_CTRL_BANK_ADDR(0);
2161 value[len++] = val;
2162
2163 /* Set Auto enumeration flag */
2164 reg[len] = SWRM_ENUMERATOR_CFG_ADDR;
2165 value[len++] = 1;
2166
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302167 /* Configure No pings */
2168 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2169 val &= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK;
2170 val |= (0x1f << SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT);
2171 reg[len] = SWRM_MCP_CFG_ADDR;
2172 value[len++] = val;
2173
2174 /* Configure number of retries of a read/write cmd */
2175 val = (retry_cmd_num << SWRM_CMD_FIFO_CFG_NUM_OF_CMD_RETRY_SHFT);
2176 reg[len] = SWRM_CMD_FIFO_CFG_ADDR;
2177 value[len++] = val;
2178
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302179 reg[len] = SWRM_MCP_BUS_CTRL_ADDR;
2180 value[len++] = 0x2;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302181
Ramprasad Katkam83303512018-10-11 17:34:22 +05302182 /* Set IRQ to PULSE */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302183 reg[len] = SWRM_COMP_CFG_ADDR;
Ramprasad Katkam83303512018-10-11 17:34:22 +05302184 value[len++] = 0x02;
2185
2186 reg[len] = SWRM_COMP_CFG_ADDR;
2187 value[len++] = 0x03;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302188
2189 reg[len] = SWRM_INTERRUPT_CLEAR;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302190 value[len++] = 0xFFFFFFFF;
2191
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302192 swrm->intr_mask = SWRM_INTERRUPT_STATUS_MASK;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302193 /* Mask soundwire interrupts */
2194 reg[len] = SWRM_INTERRUPT_MASK_ADDR;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302195 value[len++] = swrm->intr_mask;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302196
2197 reg[len] = SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302198 value[len++] = swrm->intr_mask;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302199
2200 swr_master_bulk_write(swrm, reg, value, len);
2201
Laxminath Kasamcafe0732019-11-20 17:31:58 +05302202 if (!swrm_check_link_status(swrm, 0x1)) {
2203 dev_err(swrm->dev,
2204 "%s: swr link failed to connect\n",
2205 __func__);
2206 return -EINVAL;
2207 }
Sudheer Papothi63f48152018-11-15 01:08:03 +05302208 /*
2209 * For SWR master version 1.5.1, continue
2210 * execute on command ignore.
2211 */
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302212 /* Execute it for versions >= 1.5.1 */
2213 if (swrm->version >= SWRM_VERSION_1_5_1)
Sudheer Papothi63f48152018-11-15 01:08:03 +05302214 swr_master_write(swrm, SWRM_CMD_FIFO_CFG_ADDR,
2215 (swr_master_read(swrm,
2216 SWRM_CMD_FIFO_CFG_ADDR) | 0x80000000));
2217
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302218 /* SW workaround to gate hw_ctl for SWR version >=1.6 */
2219 if (swrm->version >= SWRM_VERSION_1_6) {
2220 if (swrm->swrm_hctl_reg) {
2221 temp = ioread32(swrm->swrm_hctl_reg);
2222 temp &= 0xFFFFFFFD;
2223 iowrite32(temp, swrm->swrm_hctl_reg);
2224 }
2225 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302226 return ret;
2227}
2228
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302229static int swrm_event_notify(struct notifier_block *self,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302230 unsigned long action, void *data)
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302231{
2232 struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302233 event_notifier);
2234
2235 if (!swrm || !(swrm->dev)) {
2236 pr_err("%s: swrm or dev is NULL\n", __func__);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302237 return -EINVAL;
2238 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302239 switch (action) {
2240 case MSM_AUD_DC_EVENT:
2241 schedule_work(&(swrm->dc_presence_work));
2242 break;
2243 case SWR_WAKE_IRQ_EVENT:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302244 if (swrm->ipc_wakeup && !swrm->ipc_wakeup_triggered) {
2245 swrm->ipc_wakeup_triggered = true;
Ramprasad Katkam57349872018-11-11 18:34:57 +05302246 pm_stay_awake(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302247 schedule_work(&swrm->wakeup_work);
Ramprasad Katkamcd61c6e2018-09-18 13:22:58 +05302248 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302249 break;
2250 default:
2251 dev_err(swrm->dev, "%s: invalid event type: %lu\n",
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302252 __func__, action);
2253 return -EINVAL;
2254 }
2255
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302256 return 0;
2257}
2258
2259static void swrm_notify_work_fn(struct work_struct *work)
2260{
2261 struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302262 dc_presence_work);
2263
2264 if (!swrm || !swrm->pdev) {
2265 pr_err("%s: swrm or pdev is NULL\n", __func__);
2266 return;
2267 }
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302268 swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL);
2269}
2270
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302271static int swrm_probe(struct platform_device *pdev)
2272{
2273 struct swr_mstr_ctrl *swrm;
2274 struct swr_ctrl_platform_data *pdata;
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302275 u32 i, num_ports, port_num, port_type, ch_mask, swrm_hctl_reg = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302276 u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0;
2277 int ret = 0;
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302278 struct clk *lpass_core_hw_vote = NULL;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302279 struct clk *lpass_core_audio = NULL;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302280
2281 /* Allocate soundwire master driver structure */
2282 swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl),
2283 GFP_KERNEL);
2284 if (!swrm) {
2285 ret = -ENOMEM;
2286 goto err_memory_fail;
2287 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302288 swrm->pdev = pdev;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302289 swrm->dev = &pdev->dev;
2290 platform_set_drvdata(pdev, swrm);
2291 swr_set_ctrl_data(&swrm->master, swrm);
2292 pdata = dev_get_platdata(&pdev->dev);
2293 if (!pdata) {
2294 dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
2295 __func__);
2296 ret = -EINVAL;
2297 goto err_pdata_fail;
2298 }
2299 swrm->handle = (void *)pdata->handle;
2300 if (!swrm->handle) {
2301 dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
2302 __func__);
2303 ret = -EINVAL;
2304 goto err_pdata_fail;
2305 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302306 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr_master_id",
2307 &swrm->master_id);
2308 if (ret) {
2309 dev_err(&pdev->dev, "%s: failed to get master id\n", __func__);
2310 goto err_pdata_fail;
2311 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302312 if (!(of_property_read_u32(pdev->dev.of_node,
2313 "swrm-io-base", &swrm->swrm_base_reg)))
2314 ret = of_property_read_u32(pdev->dev.of_node,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302315 "swrm-io-base", &swrm->swrm_base_reg);
2316 if (!swrm->swrm_base_reg) {
2317 swrm->read = pdata->read;
2318 if (!swrm->read) {
2319 dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
2320 __func__);
2321 ret = -EINVAL;
2322 goto err_pdata_fail;
2323 }
2324 swrm->write = pdata->write;
2325 if (!swrm->write) {
2326 dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
2327 __func__);
2328 ret = -EINVAL;
2329 goto err_pdata_fail;
2330 }
2331 swrm->bulk_write = pdata->bulk_write;
2332 if (!swrm->bulk_write) {
2333 dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
2334 __func__);
2335 ret = -EINVAL;
2336 goto err_pdata_fail;
2337 }
2338 } else {
2339 swrm->swrm_dig_base = devm_ioremap(&pdev->dev,
2340 swrm->swrm_base_reg, SWRM_MAX_REGISTER);
2341 }
2342
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07002343 swrm->core_vote = pdata->core_vote;
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302344 if (!(of_property_read_u32(pdev->dev.of_node,
2345 "qcom,swrm-hctl-reg", &swrm_hctl_reg)))
2346 swrm->swrm_hctl_reg = devm_ioremap(&pdev->dev,
2347 swrm_hctl_reg, 0x4);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302348 swrm->clk = pdata->clk;
2349 if (!swrm->clk) {
2350 dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
2351 __func__);
2352 ret = -EINVAL;
2353 goto err_pdata_fail;
2354 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302355 if (of_property_read_u32(pdev->dev.of_node,
2356 "qcom,swr-clock-stop-mode0",
2357 &swrm->clk_stop_mode0_supp)) {
2358 swrm->clk_stop_mode0_supp = FALSE;
2359 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302360
2361 ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
2362 &swrm->num_dev);
2363 if (ret) {
2364 dev_dbg(&pdev->dev, "%s: Looking up %s property failed\n",
2365 __func__, "qcom,swr-num-dev");
2366 } else {
2367 if (swrm->num_dev > SWR_MAX_SLAVE_DEVICES) {
2368 dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
2369 __func__, swrm->num_dev, SWR_MAX_SLAVE_DEVICES);
2370 ret = -EINVAL;
2371 goto err_pdata_fail;
2372 }
2373 }
2374
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302375 /* Parse soundwire port mapping */
2376 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
2377 &num_ports);
2378 if (ret) {
2379 dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
2380 goto err_pdata_fail;
2381 }
2382 swrm->num_ports = num_ports;
2383
2384 if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
2385 &map_size)) {
2386 dev_err(swrm->dev, "missing port mapping\n");
2387 goto err_pdata_fail;
2388 }
2389
2390 map_length = map_size / (3 * sizeof(u32));
2391 if (num_ports > SWR_MSTR_PORT_LEN) {
2392 dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
2393 __func__);
2394 ret = -EINVAL;
2395 goto err_pdata_fail;
2396 }
2397 temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
2398
2399 if (!temp) {
2400 ret = -ENOMEM;
2401 goto err_pdata_fail;
2402 }
2403 ret = of_property_read_u32_array(pdev->dev.of_node,
2404 "qcom,swr-port-mapping", temp, 3 * map_length);
2405 if (ret) {
2406 dev_err(swrm->dev, "%s: Failed to read port mapping\n",
2407 __func__);
2408 goto err_pdata_fail;
2409 }
2410
2411 for (i = 0; i < map_length; i++) {
2412 port_num = temp[3 * i];
2413 port_type = temp[3 * i + 1];
2414 ch_mask = temp[3 * i + 2];
2415
2416 if (port_num != old_port_num)
2417 ch_iter = 0;
2418 swrm->port_mapping[port_num][ch_iter].port_type = port_type;
2419 swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
2420 old_port_num = port_num;
2421 }
2422 devm_kfree(&pdev->dev, temp);
2423
2424 swrm->reg_irq = pdata->reg_irq;
2425 swrm->master.read = swrm_read;
2426 swrm->master.write = swrm_write;
2427 swrm->master.bulk_write = swrm_bulk_write;
2428 swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
2429 swrm->master.connect_port = swrm_connect_port;
2430 swrm->master.disconnect_port = swrm_disconnect_port;
2431 swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
2432 swrm->master.remove_from_group = swrm_remove_from_group;
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302433 swrm->master.device_wakeup_vote = swrm_device_wakeup_vote;
2434 swrm->master.device_wakeup_unvote = swrm_device_wakeup_unvote;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302435 swrm->master.dev.parent = &pdev->dev;
2436 swrm->master.dev.of_node = pdev->dev.of_node;
2437 swrm->master.num_port = 0;
2438 swrm->rcmd_id = 0;
2439 swrm->wcmd_id = 0;
2440 swrm->slave_status = 0;
2441 swrm->num_rx_chs = 0;
2442 swrm->clk_ref_count = 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302443 swrm->swr_irq_wakeup_capable = 0;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302444 swrm->mclk_freq = MCLK_FREQ;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302445 swrm->bus_clk = MCLK_FREQ;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302446 swrm->dev_up = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302447 swrm->state = SWR_MSTR_UP;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302448 swrm->ipc_wakeup = false;
2449 swrm->ipc_wakeup_triggered = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302450 init_completion(&swrm->reset);
2451 init_completion(&swrm->broadcast);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302452 init_completion(&swrm->clk_off_complete);
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302453 mutex_init(&swrm->irq_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302454 mutex_init(&swrm->mlock);
2455 mutex_init(&swrm->reslock);
2456 mutex_init(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302457 mutex_init(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302458 mutex_init(&swrm->clklock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302459 mutex_init(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302460 mutex_init(&swrm->pm_lock);
2461 swrm->wlock_holders = 0;
2462 swrm->pm_state = SWRM_PM_SLEEPABLE;
2463 init_waitqueue_head(&swrm->pm_wq);
2464 pm_qos_add_request(&swrm->pm_qos_req,
2465 PM_QOS_CPU_DMA_LATENCY,
2466 PM_QOS_DEFAULT_VALUE);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302467
2468 for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++)
2469 INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
2470
Sudheer Papothi06f43412019-07-09 03:32:54 +05302471 /* Register LPASS core hw vote */
2472 lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
2473 if (IS_ERR(lpass_core_hw_vote)) {
2474 ret = PTR_ERR(lpass_core_hw_vote);
2475 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2476 __func__, "lpass_core_hw_vote", ret);
2477 lpass_core_hw_vote = NULL;
2478 ret = 0;
2479 }
2480 swrm->lpass_core_hw_vote = lpass_core_hw_vote;
2481
2482 /* Register LPASS audio core vote */
2483 lpass_core_audio = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
2484 if (IS_ERR(lpass_core_audio)) {
2485 ret = PTR_ERR(lpass_core_audio);
2486 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2487 __func__, "lpass_core_audio", ret);
2488 lpass_core_audio = NULL;
2489 ret = 0;
2490 }
2491 swrm->lpass_core_audio = lpass_core_audio;
2492
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302493 if (swrm->reg_irq) {
2494 ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
2495 SWR_IRQ_REGISTER);
2496 if (ret) {
2497 dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
2498 __func__, ret);
2499 goto err_irq_fail;
2500 }
2501 } else {
2502 swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
2503 if (swrm->irq < 0) {
2504 dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
2505 __func__, swrm->irq);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302506 goto err_irq_fail;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302507 }
2508
2509 ret = request_threaded_irq(swrm->irq, NULL,
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302510 swr_mstr_interrupt_v2,
Ramprasad Katkam83303512018-10-11 17:34:22 +05302511 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302512 "swr_master_irq", swrm);
2513 if (ret) {
2514 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2515 __func__, ret);
2516 goto err_irq_fail;
2517 }
2518
2519 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302520 /* Make inband tx interrupts as wakeup capable for slave irq */
2521 ret = of_property_read_u32(pdev->dev.of_node,
2522 "qcom,swr-mstr-irq-wakeup-capable",
2523 &swrm->swr_irq_wakeup_capable);
2524 if (ret)
2525 dev_dbg(swrm->dev, "%s: swrm irq wakeup capable not defined\n",
2526 __func__);
2527 if (swrm->swr_irq_wakeup_capable)
2528 irq_set_irq_wake(swrm->irq, 1);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302529 ret = swr_register_master(&swrm->master);
2530 if (ret) {
2531 dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
2532 goto err_mstr_fail;
2533 }
2534
2535 /* Add devices registered with board-info as the
2536 * controller will be up now
2537 */
2538 swr_master_add_boarddevices(&swrm->master);
2539 mutex_lock(&swrm->mlock);
2540 swrm_clk_request(swrm, true);
Laxminath Kasam4696fff2019-11-26 16:07:11 +05302541 swrm->version = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302542 ret = swrm_master_init(swrm);
2543 if (ret < 0) {
2544 dev_err(&pdev->dev,
2545 "%s: Error in master Initialization , err %d\n",
2546 __func__, ret);
2547 mutex_unlock(&swrm->mlock);
2548 goto err_mstr_fail;
2549 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302550
2551 mutex_unlock(&swrm->mlock);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302552 INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302553
2554 if (pdev->dev.of_node)
2555 of_register_swr_devices(&swrm->master);
2556
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302557#ifdef CONFIG_DEBUG_FS
2558 swrm->debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
2559 if (!IS_ERR(swrm->debugfs_swrm_dent)) {
2560 swrm->debugfs_peek = debugfs_create_file("swrm_peek",
2561 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2562 (void *) swrm, &swrm_debug_read_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302563
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302564 swrm->debugfs_poke = debugfs_create_file("swrm_poke",
2565 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2566 (void *) swrm, &swrm_debug_write_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302567
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302568 swrm->debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
2569 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2570 (void *) swrm,
2571 &swrm_debug_dump_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302572 }
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302573#endif
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302574 ret = device_init_wakeup(swrm->dev, true);
2575 if (ret) {
2576 dev_err(swrm->dev, "Device wakeup init failed: %d\n", ret);
2577 goto err_irq_wakeup_fail;
2578 }
2579
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302580 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
2581 pm_runtime_use_autosuspend(&pdev->dev);
2582 pm_runtime_set_active(&pdev->dev);
2583 pm_runtime_enable(&pdev->dev);
2584 pm_runtime_mark_last_busy(&pdev->dev);
2585
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302586 INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn);
2587 swrm->event_notifier.notifier_call = swrm_event_notify;
2588 msm_aud_evt_register_client(&swrm->event_notifier);
2589
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302590 return 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302591err_irq_wakeup_fail:
2592 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302593err_mstr_fail:
2594 if (swrm->reg_irq)
2595 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2596 swrm, SWR_IRQ_FREE);
2597 else if (swrm->irq)
2598 free_irq(swrm->irq, swrm);
2599err_irq_fail:
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302600 mutex_destroy(&swrm->irq_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302601 mutex_destroy(&swrm->mlock);
2602 mutex_destroy(&swrm->reslock);
2603 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302604 mutex_destroy(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302605 mutex_destroy(&swrm->clklock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302606 mutex_destroy(&swrm->pm_lock);
2607 pm_qos_remove_request(&swrm->pm_qos_req);
2608
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302609err_pdata_fail:
2610err_memory_fail:
2611 return ret;
2612}
2613
2614static int swrm_remove(struct platform_device *pdev)
2615{
2616 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2617
2618 if (swrm->reg_irq)
2619 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2620 swrm, SWR_IRQ_FREE);
2621 else if (swrm->irq)
2622 free_irq(swrm->irq, swrm);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302623 else if (swrm->wake_irq > 0)
2624 free_irq(swrm->wake_irq, swrm);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302625 if (swrm->swr_irq_wakeup_capable)
2626 irq_set_irq_wake(swrm->irq, 0);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302627 cancel_work_sync(&swrm->wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302628 pm_runtime_disable(&pdev->dev);
2629 pm_runtime_set_suspended(&pdev->dev);
2630 swr_unregister_master(&swrm->master);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302631 msm_aud_evt_unregister_client(&swrm->event_notifier);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302632 device_init_wakeup(swrm->dev, false);
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302633 mutex_destroy(&swrm->irq_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302634 mutex_destroy(&swrm->mlock);
2635 mutex_destroy(&swrm->reslock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302636 mutex_destroy(&swrm->iolock);
2637 mutex_destroy(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302638 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302639 mutex_destroy(&swrm->pm_lock);
2640 pm_qos_remove_request(&swrm->pm_qos_req);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302641 devm_kfree(&pdev->dev, swrm);
2642 return 0;
2643}
2644
2645static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
2646{
2647 u32 val;
2648
2649 dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
2650 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD);
2651 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2652 val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK;
2653 swr_master_write(swrm, SWRM_MCP_CFG_ADDR, val);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302654
2655 return 0;
2656}
2657
2658#ifdef CONFIG_PM
2659static int swrm_runtime_resume(struct device *dev)
2660{
2661 struct platform_device *pdev = to_platform_device(dev);
2662 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2663 int ret = 0;
Vatsal Buchae50b5002019-09-19 14:32:20 +05302664 bool swrm_clk_req_err = false;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302665 bool hw_core_err = false;
2666 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302667 struct swr_master *mstr = &swrm->master;
2668 struct swr_device *swr_dev;
2669
2670 dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
2671 __func__, swrm->state);
2672 mutex_lock(&swrm->reslock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302673
Sudheer Papothi384addd2019-06-14 02:26:52 +05302674 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2675 dev_err(dev, "%s:lpass core hw enable failed\n",
2676 __func__);
2677 hw_core_err = true;
2678 }
2679 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2680 dev_err(dev, "%s:lpass audio hw enable failed\n",
2681 __func__);
2682 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002683 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302684
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302685 if ((swrm->state == SWR_MSTR_DOWN) ||
2686 (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) {
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302687 if (swrm->clk_stop_mode0_supp) {
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302688 if (swrm->wake_irq > 0) {
2689 if (unlikely(!irq_get_irq_data
2690 (swrm->wake_irq))) {
2691 pr_err("%s: irq data is NULL\n",
2692 __func__);
2693 mutex_unlock(&swrm->reslock);
2694 return IRQ_NONE;
2695 }
2696 mutex_lock(&swrm->irq_lock);
2697 if (!irqd_irq_disabled(
2698 irq_get_irq_data(swrm->wake_irq)))
2699 disable_irq_nosync(swrm->wake_irq);
2700 mutex_unlock(&swrm->irq_lock);
2701 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302702 if (swrm->ipc_wakeup)
2703 msm_aud_evt_blocking_notifier_call_chain(
2704 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302705 }
2706
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302707 if (swrm_clk_request(swrm, true)) {
2708 /*
2709 * Set autosuspend timer to 1 for
2710 * master to enter into suspend.
2711 */
Vatsal Buchae50b5002019-09-19 14:32:20 +05302712 swrm_clk_req_err = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302713 goto exit;
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302714 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302715 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302716 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2717 ret = swr_device_up(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302718 if (ret == -ENODEV) {
2719 dev_dbg(dev,
2720 "%s slave device up not implemented\n",
2721 __func__);
2722 ret = 0;
2723 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302724 dev_err(dev,
2725 "%s: failed to wakeup swr dev %d\n",
2726 __func__, swr_dev->dev_num);
2727 swrm_clk_request(swrm, false);
2728 goto exit;
2729 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302730 }
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05302731 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2732 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2733 swrm_master_init(swrm);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302734 /* wait for hw enumeration to complete */
2735 usleep_range(100, 105);
Laxminath Kasame2291972019-11-08 14:51:59 +05302736 if (!swrm_check_link_status(swrm, 0x1))
2737 goto exit;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302738 swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, 0x0,
2739 SWRS_SCP_INT_STATUS_MASK_1);
Karthikeyan Manif6821902019-05-21 17:31:24 -07002740 if (swrm->state == SWR_MSTR_SSR) {
2741 mutex_unlock(&swrm->reslock);
2742 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
2743 mutex_lock(&swrm->reslock);
2744 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302745 } else {
2746 /*wake up from clock stop*/
2747 swr_master_write(swrm, SWRM_MCP_BUS_CTRL_ADDR, 0x2);
Sudheer Papothi55fa5972019-09-28 02:50:27 +05302748 /* clear and enable bus clash interrupt */
2749 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x08);
2750 swrm->intr_mask |= 0x08;
2751 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR,
2752 swrm->intr_mask);
2753 swr_master_write(swrm,
2754 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
2755 swrm->intr_mask);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302756 usleep_range(100, 105);
Laxminath Kasame2291972019-11-08 14:51:59 +05302757 if (!swrm_check_link_status(swrm, 0x1))
2758 goto exit;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302759 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302760 swrm->state = SWR_MSTR_UP;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302761 }
2762exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302763 if (!aud_core_err)
2764 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2765 if (!hw_core_err)
2766 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Vatsal Buchae50b5002019-09-19 14:32:20 +05302767 if (swrm_clk_req_err)
2768 pm_runtime_set_autosuspend_delay(&pdev->dev,
2769 ERR_AUTO_SUSPEND_TIMER_VAL);
2770 else
2771 pm_runtime_set_autosuspend_delay(&pdev->dev,
2772 auto_suspend_timer);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302773 mutex_unlock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302774
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302775 return ret;
2776}
2777
2778static int swrm_runtime_suspend(struct device *dev)
2779{
2780 struct platform_device *pdev = to_platform_device(dev);
2781 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2782 int ret = 0;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302783 bool hw_core_err = false;
2784 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302785 struct swr_master *mstr = &swrm->master;
2786 struct swr_device *swr_dev;
2787 int current_state = 0;
2788
2789 dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
2790 __func__, swrm->state);
2791 mutex_lock(&swrm->reslock);
2792 mutex_lock(&swrm->force_down_lock);
2793 current_state = swrm->state;
2794 mutex_unlock(&swrm->force_down_lock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302795
2796 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2797 dev_err(dev, "%s:lpass core hw enable failed\n",
2798 __func__);
2799 hw_core_err = true;
2800 }
2801 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2802 dev_err(dev, "%s:lpass audio hw enable failed\n",
2803 __func__);
2804 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002805 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302806
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302807 if ((current_state == SWR_MSTR_UP) ||
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302808 (current_state == SWR_MSTR_SSR)) {
2809
2810 if ((current_state != SWR_MSTR_SSR) &&
2811 swrm_is_port_en(&swrm->master)) {
2812 dev_dbg(dev, "%s ports are enabled\n", __func__);
2813 ret = -EBUSY;
2814 goto exit;
2815 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302816 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05302817 mutex_unlock(&swrm->reslock);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +05302818 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
Sudheer Papothi06f43412019-07-09 03:32:54 +05302819 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302820 swrm_clk_pause(swrm);
2821 swr_master_write(swrm, SWRM_COMP_CFG_ADDR, 0x00);
2822 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2823 ret = swr_device_down(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302824 if (ret == -ENODEV) {
2825 dev_dbg_ratelimited(dev,
2826 "%s slave device down not implemented\n",
2827 __func__);
2828 ret = 0;
2829 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302830 dev_err(dev,
2831 "%s: failed to shutdown swr dev %d\n",
2832 __func__, swr_dev->dev_num);
2833 goto exit;
2834 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302835 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302836 } else {
Sudheer Papothi55fa5972019-09-28 02:50:27 +05302837 /* Mask bus clash interrupt */
2838 swrm->intr_mask &= ~((u32)0x08);
2839 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR,
2840 swrm->intr_mask);
2841 swr_master_write(swrm,
2842 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
2843 swrm->intr_mask);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302844 mutex_unlock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302845 /* clock stop sequence */
2846 swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
2847 SWRS_SCP_CONTROL);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302848 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302849 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302850 }
Laxminath Kasamcafe0732019-11-20 17:31:58 +05302851 if (!swrm_check_link_status(swrm, 0x0))
2852 goto exit;
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07002853 ret = swrm_clk_request(swrm, false);
2854 if (ret) {
2855 dev_err(dev, "%s: swrmn clk failed\n", __func__);
2856 ret = 0;
2857 goto exit;
2858 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302859
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302860 if (swrm->clk_stop_mode0_supp) {
2861 if (swrm->wake_irq > 0) {
2862 enable_irq(swrm->wake_irq);
2863 } else if (swrm->ipc_wakeup) {
2864 msm_aud_evt_blocking_notifier_call_chain(
2865 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
2866 swrm->ipc_wakeup_triggered = false;
2867 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302868 }
2869
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302870 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302871 /* Retain SSR state until resume */
2872 if (current_state != SWR_MSTR_SSR)
2873 swrm->state = SWR_MSTR_DOWN;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302874exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302875 if (!aud_core_err)
2876 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2877 if (!hw_core_err)
2878 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302879 mutex_unlock(&swrm->reslock);
2880 return ret;
2881}
2882#endif /* CONFIG_PM */
2883
Sudheer Papothi06f43412019-07-09 03:32:54 +05302884static int swrm_device_suspend(struct device *dev)
2885{
2886 struct platform_device *pdev = to_platform_device(dev);
2887 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2888 int ret = 0;
2889
2890 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2891 if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
2892 ret = swrm_runtime_suspend(dev);
2893 if (!ret) {
2894 pm_runtime_disable(dev);
2895 pm_runtime_set_suspended(dev);
2896 pm_runtime_enable(dev);
2897 }
2898 }
2899
2900 return 0;
2901}
2902
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302903static int swrm_device_down(struct device *dev)
2904{
2905 struct platform_device *pdev = to_platform_device(dev);
2906 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302907
2908 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2909
2910 mutex_lock(&swrm->force_down_lock);
2911 swrm->state = SWR_MSTR_SSR;
2912 mutex_unlock(&swrm->force_down_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302913
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302914 swrm_device_suspend(dev);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302915 return 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302916}
2917
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302918int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm)
2919{
2920 int ret = 0;
Laxminath Kasama60239e2019-01-10 14:43:03 +05302921 int irq, dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302922
2923 if (!swrm->ipc_wakeup) {
Laxminath Kasama60239e2019-01-10 14:43:03 +05302924 irq = of_get_named_gpio(swrm->dev->of_node,
2925 "qcom,swr-wakeup-irq", 0);
2926 if (gpio_is_valid(irq)) {
2927 swrm->wake_irq = gpio_to_irq(irq);
2928 if (swrm->wake_irq < 0) {
2929 dev_err(swrm->dev,
2930 "Unable to configure irq\n");
2931 return swrm->wake_irq;
2932 }
2933 } else {
2934 dir_apps_irq = platform_get_irq_byname(swrm->pdev,
2935 "swr_wake_irq");
2936 if (dir_apps_irq < 0) {
2937 dev_err(swrm->dev,
2938 "TLMM connect gpio not found\n");
2939 return -EINVAL;
2940 }
2941 swrm->wake_irq = dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302942 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302943 ret = request_threaded_irq(swrm->wake_irq, NULL,
2944 swrm_wakeup_interrupt,
2945 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
2946 "swr_wake_irq", swrm);
2947 if (ret) {
2948 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2949 __func__, ret);
2950 return -EINVAL;
2951 }
Aditya Bavanari3517b112018-12-03 13:26:59 +05302952 irq_set_irq_wake(swrm->wake_irq, 1);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302953 }
2954 return ret;
2955}
2956
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302957static int swrm_alloc_port_mem(struct device *dev, struct swr_mstr_ctrl *swrm,
2958 u32 uc, u32 size)
2959{
2960 if (!swrm->port_param) {
2961 swrm->port_param = devm_kzalloc(dev,
2962 sizeof(swrm->port_param) * SWR_UC_MAX,
2963 GFP_KERNEL);
2964 if (!swrm->port_param)
2965 return -ENOMEM;
2966 }
2967 if (!swrm->port_param[uc]) {
2968 swrm->port_param[uc] = devm_kcalloc(dev, size,
2969 sizeof(struct port_params),
2970 GFP_KERNEL);
2971 if (!swrm->port_param[uc])
2972 return -ENOMEM;
2973 } else {
2974 dev_err_ratelimited(swrm->dev, "%s: called more than once\n",
2975 __func__);
2976 }
2977
2978 return 0;
2979}
2980
2981static int swrm_copy_port_config(struct swr_mstr_ctrl *swrm,
2982 struct swrm_port_config *port_cfg,
2983 u32 size)
2984{
2985 int idx;
2986 struct port_params *params;
2987 int uc = port_cfg->uc;
2988 int ret = 0;
2989
2990 for (idx = 0; idx < size; idx++) {
2991 params = &((struct port_params *)port_cfg->params)[idx];
2992 if (!params) {
2993 dev_err(swrm->dev, "%s: Invalid params\n", __func__);
2994 ret = -EINVAL;
2995 break;
2996 }
2997 memcpy(&swrm->port_param[uc][idx], params,
2998 sizeof(struct port_params));
2999 }
3000
3001 return ret;
3002}
3003
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303004/**
3005 * swrm_wcd_notify - parent device can notify to soundwire master through
3006 * this function
3007 * @pdev: pointer to platform device structure
3008 * @id: command id from parent to the soundwire master
3009 * @data: data from parent device to soundwire master
3010 */
3011int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
3012{
3013 struct swr_mstr_ctrl *swrm;
3014 int ret = 0;
3015 struct swr_master *mstr;
3016 struct swr_device *swr_dev;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05303017 struct swrm_port_config *port_cfg;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303018
3019 if (!pdev) {
3020 pr_err("%s: pdev is NULL\n", __func__);
3021 return -EINVAL;
3022 }
3023 swrm = platform_get_drvdata(pdev);
3024 if (!swrm) {
3025 dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__);
3026 return -EINVAL;
3027 }
3028 mstr = &swrm->master;
3029
3030 switch (id) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05303031 case SWR_REQ_CLK_SWITCH:
3032 /* This will put soundwire in clock stop mode and disable the
3033 * clocks, if there is no active usecase running, so that the
3034 * next activity on soundwire will request clock from new clock
3035 * source.
3036 */
3037 mutex_lock(&swrm->mlock);
3038 if (swrm->state == SWR_MSTR_UP)
3039 swrm_device_suspend(&pdev->dev);
3040 mutex_unlock(&swrm->mlock);
3041 break;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05303042 case SWR_CLK_FREQ:
3043 if (!data) {
3044 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
3045 ret = -EINVAL;
3046 } else {
3047 mutex_lock(&swrm->mlock);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05303048 if (swrm->mclk_freq != *(int *)data) {
3049 dev_dbg(swrm->dev, "%s: freq change: force mstr down\n", __func__);
3050 if (swrm->state == SWR_MSTR_DOWN)
3051 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
3052 __func__, swrm->state);
3053 else
3054 swrm_device_suspend(&pdev->dev);
Prasad Kumpatla386df4e2019-10-11 18:23:16 +05303055 /*
3056 * add delay to ensure clk release happen
3057 * if interrupt triggered for clk stop,
3058 * wait for it to exit
3059 */
3060 usleep_range(10000, 10500);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05303061 }
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05303062 swrm->mclk_freq = *(int *)data;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05303063 swrm->bus_clk = swrm->mclk_freq;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05303064 mutex_unlock(&swrm->mlock);
3065 }
3066 break;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303067 case SWR_DEVICE_SSR_DOWN:
3068 mutex_lock(&swrm->devlock);
3069 swrm->dev_up = false;
3070 mutex_unlock(&swrm->devlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05303071 mutex_lock(&swrm->reslock);
3072 swrm->state = SWR_MSTR_SSR;
3073 mutex_unlock(&swrm->reslock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303074 break;
3075 case SWR_DEVICE_SSR_UP:
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05303076 /* wait for clk voting to be zero */
Ramprasad Katkam7f6462e2018-11-06 11:51:22 +05303077 reinit_completion(&swrm->clk_off_complete);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05303078 if (swrm->clk_ref_count &&
3079 !wait_for_completion_timeout(&swrm->clk_off_complete,
Ramprasad Katkamc87efeb2018-12-12 19:26:19 +05303080 msecs_to_jiffies(500)))
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05303081 dev_err(swrm->dev, "%s: clock voting not zero\n",
3082 __func__);
3083
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303084 mutex_lock(&swrm->devlock);
3085 swrm->dev_up = true;
3086 mutex_unlock(&swrm->devlock);
3087 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303088 case SWR_DEVICE_DOWN:
3089 dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
3090 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05303091 if (swrm->state == SWR_MSTR_DOWN)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303092 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
3093 __func__, swrm->state);
3094 else
3095 swrm_device_down(&pdev->dev);
3096 mutex_unlock(&swrm->mlock);
3097 break;
3098 case SWR_DEVICE_UP:
3099 dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
Ramprasad Katkam0fed92f2018-11-08 14:22:22 +05303100 mutex_lock(&swrm->devlock);
3101 if (!swrm->dev_up) {
3102 dev_dbg(swrm->dev, "SSR not complete yet\n");
3103 mutex_unlock(&swrm->devlock);
3104 return -EBUSY;
3105 }
3106 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303107 mutex_lock(&swrm->mlock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303108 pm_runtime_mark_last_busy(&pdev->dev);
3109 pm_runtime_get_sync(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303110 mutex_lock(&swrm->reslock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303111 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
3112 ret = swr_reset_device(swr_dev);
3113 if (ret) {
3114 dev_err(swrm->dev,
3115 "%s: failed to reset swr device %d\n",
3116 __func__, swr_dev->dev_num);
3117 swrm_clk_request(swrm, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303118 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303119 }
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303120 pm_runtime_mark_last_busy(&pdev->dev);
3121 pm_runtime_put_autosuspend(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303122 mutex_unlock(&swrm->reslock);
3123 mutex_unlock(&swrm->mlock);
3124 break;
3125 case SWR_SET_NUM_RX_CH:
3126 if (!data) {
3127 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
3128 ret = -EINVAL;
3129 } else {
3130 mutex_lock(&swrm->mlock);
3131 swrm->num_rx_chs = *(int *)data;
3132 if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
3133 list_for_each_entry(swr_dev, &mstr->devices,
3134 dev_list) {
3135 ret = swr_set_device_group(swr_dev,
3136 SWR_BROADCAST);
3137 if (ret)
3138 dev_err(swrm->dev,
3139 "%s: set num ch failed\n",
3140 __func__);
3141 }
3142 } else {
3143 list_for_each_entry(swr_dev, &mstr->devices,
3144 dev_list) {
3145 ret = swr_set_device_group(swr_dev,
3146 SWR_GROUP_NONE);
3147 if (ret)
3148 dev_err(swrm->dev,
3149 "%s: set num ch failed\n",
3150 __func__);
3151 }
3152 }
3153 mutex_unlock(&swrm->mlock);
3154 }
3155 break;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303156 case SWR_REGISTER_WAKE_IRQ:
3157 if (!data) {
3158 dev_err(swrm->dev, "%s: reg wake irq data is NULL\n",
3159 __func__);
3160 ret = -EINVAL;
3161 } else {
3162 mutex_lock(&swrm->mlock);
3163 swrm->ipc_wakeup = *(u32 *)data;
3164 ret = swrm_register_wake_irq(swrm);
3165 if (ret)
3166 dev_err(swrm->dev, "%s: register wake_irq failed\n",
3167 __func__);
3168 mutex_unlock(&swrm->mlock);
3169 }
3170 break;
Sudheer Papothi72ee2642019-08-08 05:15:17 +05303171 case SWR_REGISTER_WAKEUP:
3172 msm_aud_evt_blocking_notifier_call_chain(
3173 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
3174 break;
3175 case SWR_DEREGISTER_WAKEUP:
3176 msm_aud_evt_blocking_notifier_call_chain(
3177 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
3178 break;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05303179 case SWR_SET_PORT_MAP:
3180 if (!data) {
3181 dev_err(swrm->dev, "%s: data is NULL for id=%d\n",
3182 __func__, id);
3183 ret = -EINVAL;
3184 } else {
3185 mutex_lock(&swrm->mlock);
3186 port_cfg = (struct swrm_port_config *)data;
3187 if (!port_cfg->size) {
3188 ret = -EINVAL;
3189 goto done;
3190 }
3191 ret = swrm_alloc_port_mem(&pdev->dev, swrm,
3192 port_cfg->uc, port_cfg->size);
3193 if (!ret)
3194 swrm_copy_port_config(swrm, port_cfg,
3195 port_cfg->size);
3196done:
3197 mutex_unlock(&swrm->mlock);
3198 }
3199 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303200 default:
3201 dev_err(swrm->dev, "%s: swr master unknown id %d\n",
3202 __func__, id);
3203 break;
3204 }
3205 return ret;
3206}
3207EXPORT_SYMBOL(swrm_wcd_notify);
3208
Ramprasad Katkam57349872018-11-11 18:34:57 +05303209/*
3210 * swrm_pm_cmpxchg:
3211 * Check old state and exchange with pm new state
3212 * if old state matches with current state
3213 *
3214 * @swrm: pointer to wcd core resource
3215 * @o: pm old state
3216 * @n: pm new state
3217 *
3218 * Returns old state
3219 */
3220static enum swrm_pm_state swrm_pm_cmpxchg(
3221 struct swr_mstr_ctrl *swrm,
3222 enum swrm_pm_state o,
3223 enum swrm_pm_state n)
3224{
3225 enum swrm_pm_state old;
3226
3227 if (!swrm)
3228 return o;
3229
3230 mutex_lock(&swrm->pm_lock);
3231 old = swrm->pm_state;
3232 if (old == o)
3233 swrm->pm_state = n;
3234 mutex_unlock(&swrm->pm_lock);
3235
3236 return old;
3237}
3238
3239static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm)
3240{
3241 enum swrm_pm_state os;
3242
3243 /*
3244 * swrm_{lock/unlock}_sleep will be called by swr irq handler
3245 * and slave wake up requests..
3246 *
3247 * If system didn't resume, we can simply return false so
3248 * IRQ handler can return without handling IRQ.
3249 */
3250 mutex_lock(&swrm->pm_lock);
3251 if (swrm->wlock_holders++ == 0) {
3252 dev_dbg(swrm->dev, "%s: holding wake lock\n", __func__);
3253 pm_qos_update_request(&swrm->pm_qos_req,
3254 msm_cpuidle_get_deep_idle_latency());
3255 pm_stay_awake(swrm->dev);
3256 }
3257 mutex_unlock(&swrm->pm_lock);
3258
3259 if (!wait_event_timeout(swrm->pm_wq,
3260 ((os = swrm_pm_cmpxchg(swrm,
3261 SWRM_PM_SLEEPABLE,
3262 SWRM_PM_AWAKE)) ==
3263 SWRM_PM_SLEEPABLE ||
3264 (os == SWRM_PM_AWAKE)),
3265 msecs_to_jiffies(
3266 SWRM_SYSTEM_RESUME_TIMEOUT_MS))) {
3267 dev_err(swrm->dev, "%s: system didn't resume within %dms, s %d, w %d\n",
3268 __func__, SWRM_SYSTEM_RESUME_TIMEOUT_MS, swrm->pm_state,
3269 swrm->wlock_holders);
3270 swrm_unlock_sleep(swrm);
3271 return false;
3272 }
3273 wake_up_all(&swrm->pm_wq);
3274 return true;
3275}
3276
3277static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm)
3278{
3279 mutex_lock(&swrm->pm_lock);
3280 if (--swrm->wlock_holders == 0) {
3281 dev_dbg(swrm->dev, "%s: releasing wake lock pm_state %d -> %d\n",
3282 __func__, swrm->pm_state, SWRM_PM_SLEEPABLE);
3283 /*
3284 * if swrm_lock_sleep failed, pm_state would be still
3285 * swrm_PM_ASLEEP, don't overwrite
3286 */
3287 if (likely(swrm->pm_state == SWRM_PM_AWAKE))
3288 swrm->pm_state = SWRM_PM_SLEEPABLE;
3289 pm_qos_update_request(&swrm->pm_qos_req,
3290 PM_QOS_DEFAULT_VALUE);
3291 pm_relax(swrm->dev);
3292 }
3293 mutex_unlock(&swrm->pm_lock);
3294 wake_up_all(&swrm->pm_wq);
3295}
3296
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303297#ifdef CONFIG_PM_SLEEP
3298static int swrm_suspend(struct device *dev)
3299{
3300 int ret = -EBUSY;
3301 struct platform_device *pdev = to_platform_device(dev);
3302 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3303
3304 dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
Ramprasad Katkam57349872018-11-11 18:34:57 +05303305
3306 mutex_lock(&swrm->pm_lock);
3307
3308 if (swrm->pm_state == SWRM_PM_SLEEPABLE) {
3309 dev_dbg(swrm->dev, "%s: suspending system, state %d, wlock %d\n",
3310 __func__, swrm->pm_state,
3311 swrm->wlock_holders);
3312 swrm->pm_state = SWRM_PM_ASLEEP;
3313 } else if (swrm->pm_state == SWRM_PM_AWAKE) {
3314 /*
3315 * unlock to wait for pm_state == SWRM_PM_SLEEPABLE
3316 * then set to SWRM_PM_ASLEEP
3317 */
3318 dev_dbg(swrm->dev, "%s: waiting to suspend system, state %d, wlock %d\n",
3319 __func__, swrm->pm_state,
3320 swrm->wlock_holders);
3321 mutex_unlock(&swrm->pm_lock);
3322 if (!(wait_event_timeout(swrm->pm_wq, swrm_pm_cmpxchg(
3323 swrm, SWRM_PM_SLEEPABLE,
3324 SWRM_PM_ASLEEP) ==
3325 SWRM_PM_SLEEPABLE,
3326 msecs_to_jiffies(
3327 SWRM_SYS_SUSPEND_WAIT)))) {
3328 dev_dbg(swrm->dev, "%s: suspend failed state %d, wlock %d\n",
3329 __func__, swrm->pm_state,
3330 swrm->wlock_holders);
3331 return -EBUSY;
3332 } else {
3333 dev_dbg(swrm->dev,
3334 "%s: done, state %d, wlock %d\n",
3335 __func__, swrm->pm_state,
3336 swrm->wlock_holders);
3337 }
3338 mutex_lock(&swrm->pm_lock);
3339 } else if (swrm->pm_state == SWRM_PM_ASLEEP) {
3340 dev_dbg(swrm->dev, "%s: system is already suspended, state %d, wlock %d\n",
3341 __func__, swrm->pm_state,
3342 swrm->wlock_holders);
3343 }
3344
3345 mutex_unlock(&swrm->pm_lock);
3346
3347 if ((!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev))) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303348 ret = swrm_runtime_suspend(dev);
3349 if (!ret) {
3350 /*
3351 * Synchronize runtime-pm and system-pm states:
3352 * At this point, we are already suspended. If
3353 * runtime-pm still thinks its active, then
3354 * make sure its status is in sync with HW
3355 * status. The three below calls let the
3356 * runtime-pm know that we are suspended
3357 * already without re-invoking the suspend
3358 * callback
3359 */
3360 pm_runtime_disable(dev);
3361 pm_runtime_set_suspended(dev);
3362 pm_runtime_enable(dev);
3363 }
3364 }
3365 if (ret == -EBUSY) {
3366 /*
3367 * There is a possibility that some audio stream is active
3368 * during suspend. We dont want to return suspend failure in
3369 * that case so that display and relevant components can still
3370 * go to suspend.
3371 * If there is some other error, then it should be passed-on
3372 * to system level suspend
3373 */
3374 ret = 0;
3375 }
3376 return ret;
3377}
3378
3379static int swrm_resume(struct device *dev)
3380{
3381 int ret = 0;
3382 struct platform_device *pdev = to_platform_device(dev);
3383 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3384
3385 dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
3386 if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
3387 ret = swrm_runtime_resume(dev);
3388 if (!ret) {
3389 pm_runtime_mark_last_busy(dev);
3390 pm_request_autosuspend(dev);
3391 }
3392 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05303393 mutex_lock(&swrm->pm_lock);
3394 if (swrm->pm_state == SWRM_PM_ASLEEP) {
3395 dev_dbg(swrm->dev,
3396 "%s: resuming system, state %d, wlock %d\n",
3397 __func__, swrm->pm_state,
3398 swrm->wlock_holders);
3399 swrm->pm_state = SWRM_PM_SLEEPABLE;
3400 } else {
3401 dev_dbg(swrm->dev, "%s: system is already awake, state %d wlock %d\n",
3402 __func__, swrm->pm_state,
3403 swrm->wlock_holders);
3404 }
3405 mutex_unlock(&swrm->pm_lock);
3406 wake_up_all(&swrm->pm_wq);
3407
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303408 return ret;
3409}
3410#endif /* CONFIG_PM_SLEEP */
3411
3412static const struct dev_pm_ops swrm_dev_pm_ops = {
3413 SET_SYSTEM_SLEEP_PM_OPS(
3414 swrm_suspend,
3415 swrm_resume
3416 )
3417 SET_RUNTIME_PM_OPS(
3418 swrm_runtime_suspend,
3419 swrm_runtime_resume,
3420 NULL
3421 )
3422};
3423
3424static const struct of_device_id swrm_dt_match[] = {
3425 {
3426 .compatible = "qcom,swr-mstr",
3427 },
3428 {}
3429};
3430
3431static struct platform_driver swr_mstr_driver = {
3432 .probe = swrm_probe,
3433 .remove = swrm_remove,
3434 .driver = {
3435 .name = SWR_WCD_NAME,
3436 .owner = THIS_MODULE,
3437 .pm = &swrm_dev_pm_ops,
3438 .of_match_table = swrm_dt_match,
Xiaojun Sang53cd13a2018-06-29 15:14:37 +08003439 .suppress_bind_attrs = true,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303440 },
3441};
3442
3443static int __init swrm_init(void)
3444{
3445 return platform_driver_register(&swr_mstr_driver);
3446}
3447module_init(swrm_init);
3448
3449static void __exit swrm_exit(void)
3450{
3451 platform_driver_unregister(&swr_mstr_driver);
3452}
3453module_exit(swrm_exit);
3454
3455MODULE_LICENSE("GPL v2");
3456MODULE_DESCRIPTION("SoundWire Master Controller");
3457MODULE_ALIAS("platform:swr-mstr");