blob: 3150e23dcf08abe5ce0415009a595b0cfcf6eaff [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/*
Sudheer Papothiae5c3632019-11-27 06:52:06 +05303 * Copyright (c) 2015-2020, 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 Papothiae5c3632019-11-27 06:52:06 +053028#define SWR_NUM_PORTS 4 /* TODO - Get this info from DT */
29
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +053030#define SWRM_FRAME_SYNC_SEL 4000 /* 4KHz */
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +053031#define SWRM_FRAME_SYNC_SEL_NATIVE 3675 /* 3.675KHz */
Ramprasad Katkam57349872018-11-11 18:34:57 +053032#define SWRM_SYSTEM_RESUME_TIMEOUT_MS 700
33#define SWRM_SYS_SUSPEND_WAIT 1
Sudheer Papothi3d1596e2018-10-27 06:19:18 +053034
Sudheer Papothi4c322b12018-10-31 06:34:01 +053035#define SWRM_DSD_PARAMS_PORT 4
36
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053037#define SWR_BROADCAST_CMD_ID 0x0F
Sudheer Papothi3590b312019-06-04 23:51:30 +053038#define SWR_AUTO_SUSPEND_DELAY 1 /* delay in sec */
Sudheer Papothi7c067e82018-11-15 06:53:35 +053039#define SWR_DEV_ID_MASK 0xFFFFFFFFFFFF
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053040#define SWR_REG_VAL_PACK(data, dev, id, reg) \
41 ((reg) | ((id) << 16) | ((dev) << 20) | ((data) << 24))
42
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +053043#define SWR_INVALID_PARAM 0xFF
Laxminath Kasam990c70b2018-11-09 23:15:09 +053044#define SWR_HSTOP_MAX_VAL 0xF
45#define SWR_HSTART_MIN_VAL 0x0
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +053046
Vatsal Buchae50b5002019-09-19 14:32:20 +053047#define ERR_AUTO_SUSPEND_TIMER_VAL 0x1
48
Ramprasad Katkam83303512018-10-11 17:34:22 +053049#define SWRM_INTERRUPT_STATUS_MASK 0x1FDFD
Laxminath Kasamec8c9092019-12-17 13:12:58 +053050#define SWRM_LINK_STATUS_RETRY_CNT 100
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +053051
52#define SWRM_ROW_48 48
53#define SWRM_ROW_50 50
54#define SWRM_ROW_64 64
55#define SWRM_COL_02 02
56#define SWRM_COL_16 16
57
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053058/* pm runtime auto suspend timer in msecs */
59static int auto_suspend_timer = SWR_AUTO_SUSPEND_DELAY * 1000;
60module_param(auto_suspend_timer, int, 0664);
61MODULE_PARM_DESC(auto_suspend_timer, "timer for auto suspend");
62
63enum {
64 SWR_NOT_PRESENT, /* Device is detached/not present on the bus */
65 SWR_ATTACHED_OK, /* Device is attached */
66 SWR_ALERT, /* Device alters master for any interrupts */
67 SWR_RESERVED, /* Reserved */
68};
69
70enum {
71 MASTER_ID_WSA = 1,
72 MASTER_ID_RX,
73 MASTER_ID_TX
74};
Ramprasad Katkamcab8d722018-09-28 15:54:06 +053075
76enum {
77 ENABLE_PENDING,
78 DISABLE_PENDING
79};
Sudheer Papothi384addd2019-06-14 02:26:52 +053080
81enum {
82 LPASS_HW_CORE,
83 LPASS_AUDIO_CORE,
84};
85
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053086#define TRUE 1
87#define FALSE 0
88
Ramprasad Katkam1f221262018-08-23 15:01:22 +053089#define SWRM_MAX_PORT_REG 120
Ramprasad Katkam83303512018-10-11 17:34:22 +053090#define SWRM_MAX_INIT_REG 11
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053091
Laxminath Kasamfbcaf322018-07-18 00:38:14 +053092#define MAX_FIFO_RD_FAIL_RETRY 3
93
Ramprasad Katkam57349872018-11-11 18:34:57 +053094static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm);
95static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm);
Sudheer Papothi96c842a2019-08-29 12:11:21 +053096static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr);
97static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053098
Sudheer Papothiae5c3632019-11-27 06:52:06 +053099
100static u8 swrm_get_clk_div(int mclk_freq, int bus_clk_freq)
101{
102 int clk_div = 0;
103 u8 div_val = 0;
104
105 if (!mclk_freq || !bus_clk_freq)
106 return 0;
107
108 clk_div = (mclk_freq / bus_clk_freq);
109
110 switch (clk_div) {
111 case 32:
112 div_val = 5;
113 break;
114 case 16:
115 div_val = 4;
116 break;
117 case 8:
118 div_val = 3;
119 break;
120 case 4:
121 div_val = 2;
122 break;
123 case 2:
124 div_val = 1;
125 break;
126 case 1:
127 default:
128 div_val = 0;
129 break;
130 }
131
132 return div_val;
133}
134
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530135static bool swrm_is_msm_variant(int val)
136{
137 return (val == SWRM_VERSION_1_3);
138}
139
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530140#ifdef CONFIG_DEBUG_FS
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530141static int swrm_debug_open(struct inode *inode, struct file *file)
142{
143 file->private_data = inode->i_private;
144 return 0;
145}
146
147static int get_parameters(char *buf, u32 *param1, int num_of_par)
148{
149 char *token;
150 int base, cnt;
151
152 token = strsep(&buf, " ");
153 for (cnt = 0; cnt < num_of_par; cnt++) {
154 if (token) {
155 if ((token[1] == 'x') || (token[1] == 'X'))
156 base = 16;
157 else
158 base = 10;
159
160 if (kstrtou32(token, base, &param1[cnt]) != 0)
161 return -EINVAL;
162
163 token = strsep(&buf, " ");
164 } else
165 return -EINVAL;
166 }
167 return 0;
168}
169
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530170static ssize_t swrm_reg_show(struct swr_mstr_ctrl *swrm, char __user *ubuf,
171 size_t count, loff_t *ppos)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530172{
173 int i, reg_val, len;
174 ssize_t total = 0;
175 char tmp_buf[SWR_MSTR_MAX_BUF_LEN];
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530176 int rem = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530177
178 if (!ubuf || !ppos)
179 return 0;
180
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530181 i = ((int) *ppos + SWR_MSTR_START_REG_ADDR);
182 rem = i%4;
183
184 if (rem)
185 i = (i - rem);
186
187 for (; i <= SWR_MSTR_MAX_REG_ADDR; i += 4) {
188 usleep_range(100, 150);
189 reg_val = swr_master_read(swrm, i);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530190 len = snprintf(tmp_buf, 25, "0x%.3x: 0x%.2x\n", i, reg_val);
Aditya Bavanari9f599b42019-08-27 22:18:41 +0530191 if (len < 0) {
192 pr_err("%s: fail to fill the buffer\n", __func__);
193 total = -EFAULT;
194 goto copy_err;
195 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530196 if ((total + len) >= count - 1)
197 break;
198 if (copy_to_user((ubuf + total), tmp_buf, len)) {
199 pr_err("%s: fail to copy reg dump\n", __func__);
200 total = -EFAULT;
201 goto copy_err;
202 }
203 *ppos += len;
204 total += len;
205 }
206
207copy_err:
208 return total;
209}
210
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530211static ssize_t swrm_debug_reg_dump(struct file *file, char __user *ubuf,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530212 size_t count, loff_t *ppos)
213{
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530214 struct swr_mstr_ctrl *swrm;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530215
216 if (!count || !file || !ppos || !ubuf)
217 return -EINVAL;
218
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530219 swrm = file->private_data;
220 if (!swrm)
221 return -EINVAL;
222
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530223 if (*ppos < 0)
224 return -EINVAL;
225
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530226 return swrm_reg_show(swrm, ubuf, count, ppos);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530227}
228
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530229static ssize_t swrm_debug_read(struct file *file, char __user *ubuf,
230 size_t count, loff_t *ppos)
231{
232 char lbuf[SWR_MSTR_RD_BUF_LEN];
233 struct swr_mstr_ctrl *swrm = NULL;
234
235 if (!count || !file || !ppos || !ubuf)
236 return -EINVAL;
237
238 swrm = file->private_data;
239 if (!swrm)
240 return -EINVAL;
241
242 if (*ppos < 0)
243 return -EINVAL;
244
245 snprintf(lbuf, sizeof(lbuf), "0x%x\n", swrm->read_data);
246
247 return simple_read_from_buffer(ubuf, count, ppos, lbuf,
248 strnlen(lbuf, 7));
249}
250
251static ssize_t swrm_debug_peek_write(struct file *file, const char __user *ubuf,
252 size_t count, loff_t *ppos)
253{
254 char lbuf[SWR_MSTR_RD_BUF_LEN];
255 int rc;
256 u32 param[5];
257 struct swr_mstr_ctrl *swrm = NULL;
258
259 if (!count || !file || !ppos || !ubuf)
260 return -EINVAL;
261
262 swrm = file->private_data;
263 if (!swrm)
264 return -EINVAL;
265
266 if (*ppos < 0)
267 return -EINVAL;
268
269 if (count > sizeof(lbuf) - 1)
270 return -EINVAL;
271
272 rc = copy_from_user(lbuf, ubuf, count);
273 if (rc)
274 return -EFAULT;
275
276 lbuf[count] = '\0';
277 rc = get_parameters(lbuf, param, 1);
278 if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) && (rc == 0))
279 swrm->read_data = swr_master_read(swrm, param[0]);
280 else
281 rc = -EINVAL;
282
283 if (rc == 0)
284 rc = count;
285 else
286 dev_err(swrm->dev, "%s: rc = %d\n", __func__, rc);
287
288 return rc;
289}
290
291static ssize_t swrm_debug_write(struct file *file,
292 const char __user *ubuf, size_t count, loff_t *ppos)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530293{
294 char lbuf[SWR_MSTR_WR_BUF_LEN];
295 int rc;
296 u32 param[5];
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530297 struct swr_mstr_ctrl *swrm;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530298
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530299 if (!file || !ppos || !ubuf)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530300 return -EINVAL;
301
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530302 swrm = file->private_data;
303 if (!swrm)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530304 return -EINVAL;
305
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530306 if (count > sizeof(lbuf) - 1)
307 return -EINVAL;
308
309 rc = copy_from_user(lbuf, ubuf, count);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530310 if (rc)
311 return -EFAULT;
312
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530313 lbuf[count] = '\0';
314 rc = get_parameters(lbuf, param, 2);
315 if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) &&
316 (param[1] <= 0xFFFFFFFF) &&
317 (rc == 0))
318 swr_master_write(swrm, param[0], param[1]);
319 else
320 rc = -EINVAL;
321
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530322 if (rc == 0)
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530323 rc = count;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530324 else
325 pr_err("%s: rc = %d\n", __func__, rc);
326
327 return rc;
328}
329
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530330static const struct file_operations swrm_debug_read_ops = {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530331 .open = swrm_debug_open,
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530332 .write = swrm_debug_peek_write,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530333 .read = swrm_debug_read,
334};
335
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530336static const struct file_operations swrm_debug_write_ops = {
337 .open = swrm_debug_open,
338 .write = swrm_debug_write,
339};
340
341static const struct file_operations swrm_debug_dump_ops = {
342 .open = swrm_debug_open,
343 .read = swrm_debug_reg_dump,
344};
345#endif
346
Sudheer Papothi0016db12019-06-11 04:42:38 +0530347static void swrm_reg_dump(struct swr_mstr_ctrl *swrm,
348 u32 *reg, u32 *val, int len, const char* func)
349{
350 int i = 0;
351
352 for (i = 0; i < len; i++)
353 dev_dbg(swrm->dev, "%s: reg = 0x%x val = 0x%x\n",
354 func, reg[i], val[i]);
355}
356
Sudheer Papothi921b8652019-10-03 02:13:32 +0530357static bool is_swr_clk_needed(struct swr_mstr_ctrl *swrm)
358{
359 return ((swrm->version <= SWRM_VERSION_1_5_1) ? true : false);
360}
361
Sudheer Papothi384addd2019-06-14 02:26:52 +0530362static int swrm_request_hw_vote(struct swr_mstr_ctrl *swrm,
363 int core_type, bool enable)
364{
365 int ret = 0;
366
367 if (core_type == LPASS_HW_CORE) {
368 if (swrm->lpass_core_hw_vote) {
369 if (enable) {
370 ret =
371 clk_prepare_enable(swrm->lpass_core_hw_vote);
372 if (ret < 0)
373 dev_err(swrm->dev,
374 "%s:lpass core hw enable failed\n",
375 __func__);
376 } else
377 clk_disable_unprepare(swrm->lpass_core_hw_vote);
378 }
379 }
380 if (core_type == LPASS_AUDIO_CORE) {
381 if (swrm->lpass_core_audio) {
382 if (enable) {
383 ret =
384 clk_prepare_enable(swrm->lpass_core_audio);
385 if (ret < 0)
386 dev_err(swrm->dev,
387 "%s:lpass audio hw enable failed\n",
388 __func__);
389 } else
390 clk_disable_unprepare(swrm->lpass_core_audio);
391 }
392 }
393
394 return ret;
395}
396
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +0530397static int swrm_get_ssp_period(struct swr_mstr_ctrl *swrm,
398 int row, int col,
399 int frame_sync)
400{
401 if (!swrm || !row || !col || !frame_sync)
402 return 1;
403
404 return ((swrm->bus_clk * 2) / ((row * col) * frame_sync));
405}
406
Sudheer Papothi921b8652019-10-03 02:13:32 +0530407static int swrm_core_vote_request(struct swr_mstr_ctrl *swrm)
408{
409 int ret = 0;
410
411 if (!swrm->handle)
412 return -EINVAL;
413
414 mutex_lock(&swrm->clklock);
415 if (!swrm->dev_up) {
416 ret = -ENODEV;
417 goto exit;
418 }
419 if (swrm->core_vote) {
420 ret = swrm->core_vote(swrm->handle, true);
421 if (ret)
422 dev_err_ratelimited(swrm->dev,
423 "%s: core vote request failed\n", __func__);
424 }
425exit:
426 mutex_unlock(&swrm->clklock);
427
428 return ret;
429}
430
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530431static int swrm_clk_request(struct swr_mstr_ctrl *swrm, bool enable)
432{
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530433 int ret = 0;
434
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530435 if (!swrm->clk || !swrm->handle)
436 return -EINVAL;
437
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530438 mutex_lock(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530439 if (enable) {
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530440 if (!swrm->dev_up) {
441 ret = -ENODEV;
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530442 goto exit;
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530443 }
Sudheer Papothi921b8652019-10-03 02:13:32 +0530444 if (is_swr_clk_needed(swrm)) {
445 if (swrm->core_vote) {
446 ret = swrm->core_vote(swrm->handle, true);
447 if (ret) {
448 dev_err_ratelimited(swrm->dev,
449 "%s: core vote request failed\n",
450 __func__);
451 goto exit;
452 }
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -0700453 }
454 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530455 swrm->clk_ref_count++;
456 if (swrm->clk_ref_count == 1) {
Aditya Bavanarif500a1d2019-09-16 18:27:51 -0700457 trace_printk("%s: clock enable count %d",
458 __func__, swrm->clk_ref_count);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530459 ret = swrm->clk(swrm->handle, true);
460 if (ret) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +0530461 dev_err_ratelimited(swrm->dev,
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530462 "%s: clock enable req failed",
463 __func__);
464 --swrm->clk_ref_count;
465 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530466 }
467 } else if (--swrm->clk_ref_count == 0) {
Aditya Bavanarif500a1d2019-09-16 18:27:51 -0700468 trace_printk("%s: clock disable count %d",
469 __func__, swrm->clk_ref_count);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530470 swrm->clk(swrm->handle, false);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530471 complete(&swrm->clk_off_complete);
472 }
473 if (swrm->clk_ref_count < 0) {
Meng Wang8c60bb52019-06-19 15:49:06 +0800474 dev_err(swrm->dev, "%s: swrm clk count mismatch\n", __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530475 swrm->clk_ref_count = 0;
476 }
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530477
478exit:
479 mutex_unlock(&swrm->clklock);
480 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530481}
482
483static int swrm_ahb_write(struct swr_mstr_ctrl *swrm,
484 u16 reg, u32 *value)
485{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530486 u32 temp = (u32)(*value);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530487 int ret = 0;
488
489 mutex_lock(&swrm->devlock);
490 if (!swrm->dev_up)
491 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530492
Sudheer Papothi921b8652019-10-03 02:13:32 +0530493 if (is_swr_clk_needed(swrm)) {
494 ret = swrm_clk_request(swrm, TRUE);
495 if (ret) {
496 dev_err_ratelimited(swrm->dev,
497 "%s: clock request failed\n",
498 __func__);
499 goto err;
500 }
501 } else if (swrm_core_vote_request(swrm)) {
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530502 goto err;
503 }
Sudheer Papothi921b8652019-10-03 02:13:32 +0530504
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530505 iowrite32(temp, swrm->swrm_dig_base + reg);
Sudheer Papothi921b8652019-10-03 02:13:32 +0530506 if (is_swr_clk_needed(swrm))
507 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530508err:
509 mutex_unlock(&swrm->devlock);
510 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530511}
512
513static int swrm_ahb_read(struct swr_mstr_ctrl *swrm,
514 u16 reg, u32 *value)
515{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530516 u32 temp = 0;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530517 int ret = 0;
518
519 mutex_lock(&swrm->devlock);
520 if (!swrm->dev_up)
521 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530522
Sudheer Papothi921b8652019-10-03 02:13:32 +0530523 if (is_swr_clk_needed(swrm)) {
524 ret = swrm_clk_request(swrm, TRUE);
525 if (ret) {
526 dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
527 __func__);
528 goto err;
529 }
530 } else if (swrm_core_vote_request(swrm)) {
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530531 goto err;
532 }
Sudheer Papothi921b8652019-10-03 02:13:32 +0530533
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530534 temp = ioread32(swrm->swrm_dig_base + reg);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530535 *value = temp;
Sudheer Papothi921b8652019-10-03 02:13:32 +0530536 if (is_swr_clk_needed(swrm))
537 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530538err:
539 mutex_unlock(&swrm->devlock);
540 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530541}
542
543static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr)
544{
545 u32 val = 0;
546
547 if (swrm->read)
548 val = swrm->read(swrm->handle, reg_addr);
549 else
550 swrm_ahb_read(swrm, reg_addr, &val);
551 return val;
552}
553
554static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val)
555{
556 if (swrm->write)
557 swrm->write(swrm->handle, reg_addr, val);
558 else
559 swrm_ahb_write(swrm, reg_addr, &val);
560}
561
562static int swr_master_bulk_write(struct swr_mstr_ctrl *swrm, u32 *reg_addr,
563 u32 *val, unsigned int length)
564{
565 int i = 0;
566
567 if (swrm->bulk_write)
568 swrm->bulk_write(swrm->handle, reg_addr, val, length);
569 else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530570 mutex_lock(&swrm->iolock);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530571 for (i = 0; i < length; i++) {
572 /* wait for FIFO WR command to complete to avoid overflow */
Karthikeyan Mani5d52dd82019-08-15 16:58:08 -0700573 /*
574 * Reduce sleep from 100us to 10us to meet KPIs
575 * This still meets the hardware spec
576 */
577 usleep_range(10, 12);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530578 swr_master_write(swrm, reg_addr[i], val[i]);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530579 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530580 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530581 }
582 return 0;
583}
584
Laxminath Kasame2291972019-11-08 14:51:59 +0530585static bool swrm_check_link_status(struct swr_mstr_ctrl *swrm, bool active)
586{
587 int retry = SWRM_LINK_STATUS_RETRY_CNT;
588 int ret = false;
589 int status = active ? 0x1 : 0x0;
Laxminath Kasam09819e92019-11-18 14:38:11 +0530590 int comp_sts = 0x0;
Laxminath Kasame2291972019-11-08 14:51:59 +0530591
592 if ((swrm->version <= SWRM_VERSION_1_5_1))
593 return true;
594
595 do {
Laxminath Kasam09819e92019-11-18 14:38:11 +0530596 comp_sts = swr_master_read(swrm, SWRM_COMP_STATUS) & 0x01;
597 /* check comp status and status requested met */
598 if ((comp_sts && status) || (!comp_sts && !status)) {
Laxminath Kasame2291972019-11-08 14:51:59 +0530599 ret = true;
600 break;
601 }
602 retry--;
603 usleep_range(500, 510);
604 } while (retry);
605
606 if (retry == 0)
607 dev_err(swrm->dev, "%s: link status not %s\n", __func__,
608 active ? "connected" : "disconnected");
609
610 return ret;
611}
612
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530613static bool swrm_is_port_en(struct swr_master *mstr)
614{
615 return !!(mstr->num_port);
616}
617
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530618static void copy_port_tables(struct swr_mstr_ctrl *swrm,
619 struct port_params *params)
620{
621 u8 i;
622 struct port_params *config = params;
623
624 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
625 /* wsa uses single frame structure for all configurations */
626 if (!swrm->mport_cfg[i].port_en)
627 continue;
628 swrm->mport_cfg[i].sinterval = config[i].si;
629 swrm->mport_cfg[i].offset1 = config[i].off1;
630 swrm->mport_cfg[i].offset2 = config[i].off2;
631 swrm->mport_cfg[i].hstart = config[i].hstart;
632 swrm->mport_cfg[i].hstop = config[i].hstop;
633 swrm->mport_cfg[i].blk_pack_mode = config[i].bp_mode;
634 swrm->mport_cfg[i].blk_grp_count = config[i].bgp_ctrl;
635 swrm->mport_cfg[i].word_length = config[i].wd_len;
636 swrm->mport_cfg[i].lane_ctrl = config[i].lane_ctrl;
637 }
638}
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530639static int swrm_get_port_config(struct swr_mstr_ctrl *swrm)
640{
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530641 struct port_params *params;
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530642 u32 usecase = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530643
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530644 /* TODO - Send usecase information to avoid checking for master_id */
645 if (swrm->mport_cfg[SWRM_DSD_PARAMS_PORT].port_en &&
646 (swrm->master_id == MASTER_ID_RX))
647 usecase = 1;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530648
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530649 params = swrm->port_param[usecase];
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530650 copy_port_tables(swrm, params);
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530651
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530652 return 0;
653}
654
655static int swrm_get_master_port(struct swr_mstr_ctrl *swrm, u8 *mstr_port_id,
656 u8 *mstr_ch_mask, u8 mstr_prt_type,
657 u8 slv_port_id)
658{
659 int i, j;
660 *mstr_port_id = 0;
661
662 for (i = 1; i <= swrm->num_ports; i++) {
663 for (j = 0; j < SWR_MAX_CH_PER_PORT; j++) {
664 if (swrm->port_mapping[i][j].port_type == mstr_prt_type)
665 goto found;
666 }
667 }
668found:
669 if (i > swrm->num_ports || j == SWR_MAX_CH_PER_PORT) {
670 dev_err(swrm->dev, "%s: port type not supported by master\n",
671 __func__);
672 return -EINVAL;
673 }
674 /* id 0 corresponds to master port 1 */
675 *mstr_port_id = i - 1;
676 *mstr_ch_mask = swrm->port_mapping[i][j].ch_mask;
677
678 return 0;
679
680}
681
682static u32 swrm_get_packed_reg_val(u8 *cmd_id, u8 cmd_data,
683 u8 dev_addr, u16 reg_addr)
684{
685 u32 val;
686 u8 id = *cmd_id;
687
688 if (id != SWR_BROADCAST_CMD_ID) {
689 if (id < 14)
690 id += 1;
691 else
692 id = 0;
693 *cmd_id = id;
694 }
695 val = SWR_REG_VAL_PACK(cmd_data, dev_addr, id, reg_addr);
696
697 return val;
698}
699
700static int swrm_cmd_fifo_rd_cmd(struct swr_mstr_ctrl *swrm, int *cmd_data,
701 u8 dev_addr, u8 cmd_id, u16 reg_addr,
702 u32 len)
703{
704 u32 val;
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530705 u32 retry_attempt = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530706
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530707 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530708 val = swrm_get_packed_reg_val(&swrm->rcmd_id, len, dev_addr, reg_addr);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530709 if (swrm->read) {
710 /* skip delay if read is handled in platform driver */
711 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
712 } else {
713 /* wait for FIFO RD to complete to avoid overflow */
714 usleep_range(100, 105);
715 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
716 /* wait for FIFO RD CMD complete to avoid overflow */
717 usleep_range(250, 255);
718 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530719retry_read:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530720 *cmd_data = swr_master_read(swrm, SWRM_CMD_FIFO_RD_FIFO_ADDR);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530721 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, rcmd_id: 0x%x, \
722 dev_num: 0x%x, cmd_data: 0x%x\n", __func__, reg_addr,
723 cmd_id, swrm->rcmd_id, dev_addr, *cmd_data);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530724 if ((((*cmd_data) & 0xF00) >> 8) != swrm->rcmd_id) {
725 if (retry_attempt < MAX_FIFO_RD_FAIL_RETRY) {
726 /* wait 500 us before retry on fifo read failure */
727 usleep_range(500, 505);
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +0530728 if (retry_attempt == (MAX_FIFO_RD_FAIL_RETRY - 1)) {
729 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
730 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
731 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530732 retry_attempt++;
733 goto retry_read;
734 } else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530735 dev_err_ratelimited(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, \
736 rcmd_id: 0x%x, dev_num: 0x%x, cmd_data: 0x%x\n",
737 __func__, reg_addr, cmd_id, swrm->rcmd_id,
738 dev_addr, *cmd_data);
739
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530740 dev_err_ratelimited(swrm->dev,
741 "%s: failed to read fifo\n", __func__);
742 }
743 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530744 mutex_unlock(&swrm->iolock);
745
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530746 return 0;
747}
748
749static int swrm_cmd_fifo_wr_cmd(struct swr_mstr_ctrl *swrm, u8 cmd_data,
750 u8 dev_addr, u8 cmd_id, u16 reg_addr)
751{
752 u32 val;
753 int ret = 0;
754
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530755 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530756 if (!cmd_id)
757 val = swrm_get_packed_reg_val(&swrm->wcmd_id, cmd_data,
758 dev_addr, reg_addr);
759 else
760 val = swrm_get_packed_reg_val(&cmd_id, cmd_data,
761 dev_addr, reg_addr);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530762 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x,wcmd_id: 0x%x, \
763 dev_num: 0x%x, cmd_data: 0x%x\n", __func__,
764 reg_addr, cmd_id, swrm->wcmd_id,dev_addr, cmd_data);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +0530765 swr_master_write(swrm, SWRM_CMD_FIFO_WR_CMD, val);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530766 /*
767 * wait for FIFO WR command to complete to avoid overflow
768 * skip delay if write is handled in platform driver.
769 */
770 if(!swrm->write)
Karthikeyan Mani5d52dd82019-08-15 16:58:08 -0700771 usleep_range(150, 155);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530772 if (cmd_id == 0xF) {
773 /*
774 * sleep for 10ms for MSM soundwire variant to allow broadcast
775 * command to complete.
776 */
777 if (swrm_is_msm_variant(swrm->version))
778 usleep_range(10000, 10100);
779 else
780 wait_for_completion_timeout(&swrm->broadcast,
781 (2 * HZ/10));
782 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530783 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530784 return ret;
785}
786
787static int swrm_read(struct swr_master *master, u8 dev_num, u16 reg_addr,
788 void *buf, u32 len)
789{
790 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
791 int ret = 0;
792 int val;
793 u8 *reg_val = (u8 *)buf;
794
795 if (!swrm) {
796 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
797 return -EINVAL;
798 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530799 if (!dev_num) {
800 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
801 return -EINVAL;
802 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530803 mutex_lock(&swrm->devlock);
804 if (!swrm->dev_up) {
805 mutex_unlock(&swrm->devlock);
806 return 0;
807 }
808 mutex_unlock(&swrm->devlock);
809
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530810 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530811 ret = swrm_cmd_fifo_rd_cmd(swrm, &val, dev_num, 0, reg_addr, len);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530812
813 if (!ret)
814 *reg_val = (u8)val;
815
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530816 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530817 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530818 return ret;
819}
820
821static int swrm_write(struct swr_master *master, u8 dev_num, u16 reg_addr,
822 const void *buf)
823{
824 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
825 int ret = 0;
826 u8 reg_val = *(u8 *)buf;
827
828 if (!swrm) {
829 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
830 return -EINVAL;
831 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530832 if (!dev_num) {
833 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
834 return -EINVAL;
835 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530836 mutex_lock(&swrm->devlock);
837 if (!swrm->dev_up) {
838 mutex_unlock(&swrm->devlock);
839 return 0;
840 }
841 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530842
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530843 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530844 ret = swrm_cmd_fifo_wr_cmd(swrm, reg_val, dev_num, 0, reg_addr);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530845
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530846 pm_runtime_put_autosuspend(swrm->dev);
847 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530848 return ret;
849}
850
851static int swrm_bulk_write(struct swr_master *master, u8 dev_num, void *reg,
852 const void *buf, size_t len)
853{
854 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
855 int ret = 0;
856 int i;
857 u32 *val;
858 u32 *swr_fifo_reg;
859
860 if (!swrm || !swrm->handle) {
861 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
862 return -EINVAL;
863 }
864 if (len <= 0)
865 return -EINVAL;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530866 mutex_lock(&swrm->devlock);
867 if (!swrm->dev_up) {
868 mutex_unlock(&swrm->devlock);
869 return 0;
870 }
871 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530872
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530873 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530874 if (dev_num) {
875 swr_fifo_reg = kcalloc(len, sizeof(u32), GFP_KERNEL);
876 if (!swr_fifo_reg) {
877 ret = -ENOMEM;
878 goto err;
879 }
880 val = kcalloc(len, sizeof(u32), GFP_KERNEL);
881 if (!val) {
882 ret = -ENOMEM;
883 goto mem_fail;
884 }
885
886 for (i = 0; i < len; i++) {
887 val[i] = swrm_get_packed_reg_val(&swrm->wcmd_id,
888 ((u8 *)buf)[i],
889 dev_num,
890 ((u16 *)reg)[i]);
891 swr_fifo_reg[i] = SWRM_CMD_FIFO_WR_CMD;
892 }
893 ret = swr_master_bulk_write(swrm, swr_fifo_reg, val, len);
894 if (ret) {
895 dev_err(&master->dev, "%s: bulk write failed\n",
896 __func__);
897 ret = -EINVAL;
898 }
899 } else {
900 dev_err(&master->dev,
901 "%s: No support of Bulk write for master regs\n",
902 __func__);
903 ret = -EINVAL;
904 goto err;
905 }
906 kfree(val);
907mem_fail:
908 kfree(swr_fifo_reg);
909err:
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530910 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530911 pm_runtime_mark_last_busy(swrm->dev);
912 return ret;
913}
914
915static u8 get_inactive_bank_num(struct swr_mstr_ctrl *swrm)
916{
917 return (swr_master_read(swrm, SWRM_MCP_STATUS) &
918 SWRM_MCP_STATUS_BANK_NUM_MASK) ? 0 : 1;
919}
920
921static void enable_bank_switch(struct swr_mstr_ctrl *swrm, u8 bank,
922 u8 row, u8 col)
923{
924 swrm_cmd_fifo_wr_cmd(swrm, ((row << 3) | col), 0xF, 0xF,
925 SWRS_SCP_FRAME_CTRL_BANK(bank));
926}
927
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +0530928static void swrm_switch_frame_shape(struct swr_mstr_ctrl *swrm, int mclk_freq)
929{
930 u8 bank;
931 u32 n_row, n_col;
932 u32 value = 0;
933 u32 row = 0, col = 0;
934 u8 ssp_period = 0;
935 int frame_sync = SWRM_FRAME_SYNC_SEL;
936
937 if (mclk_freq == MCLK_FREQ_NATIVE) {
938 n_col = SWR_MAX_COL;
939 col = SWRM_COL_16;
940 n_row = SWR_ROW_64;
941 row = SWRM_ROW_64;
942 frame_sync = SWRM_FRAME_SYNC_SEL_NATIVE;
943 } else {
944 n_col = SWR_MIN_COL;
945 col = SWRM_COL_02;
946 n_row = SWR_ROW_50;
947 row = SWRM_ROW_50;
948 frame_sync = SWRM_FRAME_SYNC_SEL;
949 }
950
951 bank = get_inactive_bank_num(swrm);
952 ssp_period = swrm_get_ssp_period(swrm, row, col, frame_sync);
953 dev_dbg(swrm->dev, "%s: ssp_period: %d\n", __func__, ssp_period);
954 value = ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
955 (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
956 ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
957 swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
958 enable_bank_switch(swrm, bank, n_row, n_col);
959}
960
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530961static struct swr_port_info *swrm_get_port_req(struct swrm_mports *mport,
962 u8 slv_port, u8 dev_num)
963{
964 struct swr_port_info *port_req = NULL;
965
966 list_for_each_entry(port_req, &mport->port_req_list, list) {
967 /* Store dev_id instead of dev_num if enumeration is changed run_time */
968 if ((port_req->slave_port_id == slv_port)
969 && (port_req->dev_num == dev_num))
970 return port_req;
971 }
972 return NULL;
973}
974
975static bool swrm_remove_from_group(struct swr_master *master)
976{
977 struct swr_device *swr_dev;
978 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
979 bool is_removed = false;
980
981 if (!swrm)
982 goto end;
983
984 mutex_lock(&swrm->mlock);
985 if ((swrm->num_rx_chs > 1) &&
986 (swrm->num_rx_chs == swrm->num_cfg_devs)) {
987 list_for_each_entry(swr_dev, &master->devices,
988 dev_list) {
989 swr_dev->group_id = SWR_GROUP_NONE;
990 master->gr_sid = 0;
991 }
992 is_removed = true;
993 }
994 mutex_unlock(&swrm->mlock);
995
996end:
997 return is_removed;
998}
999
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301000int swrm_get_clk_div_rate(int mclk_freq, int bus_clk_freq)
1001{
1002 if (!bus_clk_freq)
1003 return mclk_freq;
1004
1005 if (mclk_freq == SWR_CLK_RATE_9P6MHZ) {
1006 if (bus_clk_freq <= SWR_CLK_RATE_0P6MHZ)
1007 bus_clk_freq = SWR_CLK_RATE_0P6MHZ;
1008 else if (bus_clk_freq <= SWR_CLK_RATE_1P2MHZ)
1009 bus_clk_freq = SWR_CLK_RATE_1P2MHZ;
1010 else if (bus_clk_freq <= SWR_CLK_RATE_2P4MHZ)
1011 bus_clk_freq = SWR_CLK_RATE_2P4MHZ;
1012 else if(bus_clk_freq <= SWR_CLK_RATE_4P8MHZ)
1013 bus_clk_freq = SWR_CLK_RATE_4P8MHZ;
1014 else if(bus_clk_freq <= SWR_CLK_RATE_9P6MHZ)
1015 bus_clk_freq = SWR_CLK_RATE_9P6MHZ;
1016 } else if (mclk_freq == SWR_CLK_RATE_11P2896MHZ)
1017 bus_clk_freq = SWR_CLK_RATE_11P2896MHZ;
1018
1019 return bus_clk_freq;
1020}
1021
1022static int swrm_update_bus_clk(struct swr_mstr_ctrl *swrm)
1023{
1024 int ret = 0;
1025 int agg_clk = 0;
1026 int i;
1027
1028 for (i = 0; i < SWR_MSTR_PORT_LEN; i++)
1029 agg_clk += swrm->mport_cfg[i].ch_rate;
1030
1031 if (agg_clk)
1032 swrm->bus_clk = swrm_get_clk_div_rate(swrm->mclk_freq,
1033 agg_clk);
1034 else
1035 swrm->bus_clk = swrm->mclk_freq;
1036
1037 dev_dbg(swrm->dev, "%s: all_port_clk: %d, bus_clk: %d\n",
1038 __func__, agg_clk, swrm->bus_clk);
1039
1040 return ret;
1041}
1042
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301043static void swrm_disable_ports(struct swr_master *master,
1044 u8 bank)
1045{
1046 u32 value;
1047 struct swr_port_info *port_req;
1048 int i;
1049 struct swrm_mports *mport;
1050 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1051
1052 if (!swrm) {
1053 pr_err("%s: swrm is null\n", __func__);
1054 return;
1055 }
1056
1057 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
1058 master->num_port);
1059
1060
1061 for (i = 0; i < SWR_MSTR_PORT_LEN ; i++) {
1062
1063 mport = &(swrm->mport_cfg[i]);
1064 if (!mport->port_en)
1065 continue;
1066
1067 list_for_each_entry(port_req, &mport->port_req_list, list) {
1068 /* skip ports with no change req's*/
1069 if (port_req->req_ch == port_req->ch_en)
1070 continue;
1071
1072 swrm_cmd_fifo_wr_cmd(swrm, port_req->req_ch,
1073 port_req->dev_num, 0x00,
1074 SWRS_DP_CHANNEL_ENABLE_BANK(port_req->slave_port_id,
1075 bank));
1076 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x\n",
1077 __func__, i,
1078 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)));
1079 }
1080 value = ((mport->req_ch)
1081 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
1082 value |= ((mport->offset2)
1083 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
1084 value |= ((mport->offset1)
1085 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
1086 value |= mport->sinterval;
1087
1088 swr_master_write(swrm,
1089 SWRM_DP_PORT_CTRL_BANK(i+1, bank),
1090 value);
1091 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
1092 __func__, i,
1093 (SWRM_DP_PORT_CTRL_BANK(i+1, bank)), value);
1094 }
1095}
1096
1097static void swrm_cleanup_disabled_port_reqs(struct swr_master *master)
1098{
1099 struct swr_port_info *port_req, *next;
1100 int i;
1101 struct swrm_mports *mport;
1102 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1103
1104 if (!swrm) {
1105 pr_err("%s: swrm is null\n", __func__);
1106 return;
1107 }
1108 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
1109 master->num_port);
1110
1111 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
1112 mport = &(swrm->mport_cfg[i]);
1113 list_for_each_entry_safe(port_req, next,
1114 &mport->port_req_list, list) {
1115 /* skip ports without new ch req */
1116 if (port_req->ch_en == port_req->req_ch)
1117 continue;
1118
1119 /* remove new ch req's*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +05301120 port_req->ch_en = port_req->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301121
1122 /* If no streams enabled on port, remove the port req */
1123 if (port_req->ch_en == 0) {
1124 list_del(&port_req->list);
1125 kfree(port_req);
1126 }
1127 }
1128 /* remove new ch req's on mport*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +05301129 mport->ch_en = mport->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301130
1131 if (!(mport->ch_en)) {
1132 mport->port_en = false;
1133 master->port_en_mask &= ~i;
1134 }
1135 }
1136}
1137static void swrm_copy_data_port_config(struct swr_master *master, u8 bank)
1138{
1139 u32 value, slv_id;
1140 struct swr_port_info *port_req;
1141 int i;
1142 struct swrm_mports *mport;
1143 u32 reg[SWRM_MAX_PORT_REG];
1144 u32 val[SWRM_MAX_PORT_REG];
1145 int len = 0;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301146 u8 hparams;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301147 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1148
1149 if (!swrm) {
1150 pr_err("%s: swrm is null\n", __func__);
1151 return;
1152 }
1153
1154 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
1155 master->num_port);
1156
1157 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
1158 mport = &(swrm->mport_cfg[i]);
1159 if (!mport->port_en)
1160 continue;
1161
1162 list_for_each_entry(port_req, &mport->port_req_list, list) {
1163 slv_id = port_req->slave_port_id;
1164 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1165 val[len++] = SWR_REG_VAL_PACK(port_req->req_ch,
1166 port_req->dev_num, 0x00,
1167 SWRS_DP_CHANNEL_ENABLE_BANK(slv_id,
1168 bank));
1169
1170 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1171 val[len++] = SWR_REG_VAL_PACK(mport->sinterval,
1172 port_req->dev_num, 0x00,
1173 SWRS_DP_SAMPLE_CONTROL_1_BANK(slv_id,
1174 bank));
1175
1176 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1177 val[len++] = SWR_REG_VAL_PACK(mport->offset1,
1178 port_req->dev_num, 0x00,
1179 SWRS_DP_OFFSET_CONTROL_1_BANK(slv_id,
1180 bank));
1181
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301182 if (mport->offset2 != SWR_INVALID_PARAM) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301183 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1184 val[len++] = SWR_REG_VAL_PACK(mport->offset2,
1185 port_req->dev_num, 0x00,
1186 SWRS_DP_OFFSET_CONTROL_2_BANK(
1187 slv_id, bank));
1188 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301189 if (mport->hstart != SWR_INVALID_PARAM
1190 && mport->hstop != SWR_INVALID_PARAM) {
1191 hparams = (mport->hstart << 4) | mport->hstop;
1192
1193 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1194 val[len++] = SWR_REG_VAL_PACK(hparams,
1195 port_req->dev_num, 0x00,
1196 SWRS_DP_HCONTROL_BANK(slv_id,
1197 bank));
1198 }
1199 if (mport->word_length != SWR_INVALID_PARAM) {
1200 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1201 val[len++] =
1202 SWR_REG_VAL_PACK(mport->word_length,
1203 port_req->dev_num, 0x00,
1204 SWRS_DP_BLOCK_CONTROL_1(slv_id));
1205 }
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +05301206 if (mport->blk_pack_mode != SWR_INVALID_PARAM
1207 && swrm->master_id != MASTER_ID_WSA) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301208 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1209 val[len++] =
1210 SWR_REG_VAL_PACK(mport->blk_pack_mode,
1211 port_req->dev_num, 0x00,
1212 SWRS_DP_BLOCK_CONTROL_3_BANK(slv_id,
1213 bank));
1214 }
1215 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
1216 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1217 val[len++] =
1218 SWR_REG_VAL_PACK(mport->blk_grp_count,
1219 port_req->dev_num, 0x00,
1220 SWRS_DP_BLOCK_CONTROL_2_BANK(slv_id,
1221 bank));
1222 }
1223 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
1224 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1225 val[len++] =
1226 SWR_REG_VAL_PACK(mport->lane_ctrl,
1227 port_req->dev_num, 0x00,
1228 SWRS_DP_LANE_CONTROL_BANK(slv_id,
1229 bank));
1230 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301231 port_req->ch_en = port_req->req_ch;
1232 }
1233 value = ((mport->req_ch)
1234 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +05301235
1236 if (mport->offset2 != SWR_INVALID_PARAM)
1237 value |= ((mport->offset2)
1238 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301239 value |= ((mport->offset1)
1240 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
1241 value |= mport->sinterval;
1242
1243
1244 reg[len] = SWRM_DP_PORT_CTRL_BANK(i + 1, bank);
1245 val[len++] = value;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301246 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
1247 __func__, i,
1248 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)), value);
1249
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301250 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
1251 reg[len] = SWRM_DP_PORT_CTRL_2_BANK(i + 1, bank);
1252 val[len++] = mport->lane_ctrl;
1253 }
1254 if (mport->word_length != SWR_INVALID_PARAM) {
1255 reg[len] = SWRM_DP_BLOCK_CTRL_1(i + 1);
1256 val[len++] = mport->word_length;
1257 }
1258
1259 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
1260 reg[len] = SWRM_DP_BLOCK_CTRL2_BANK(i + 1, bank);
1261 val[len++] = mport->blk_grp_count;
1262 }
1263 if (mport->hstart != SWR_INVALID_PARAM
1264 && mport->hstop != SWR_INVALID_PARAM) {
1265 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
Laxminath Kasame30eef72018-11-05 17:40:09 +05301266 hparams = (mport->hstop << 4) | mport->hstart;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301267 val[len++] = hparams;
Laxminath Kasam990c70b2018-11-09 23:15:09 +05301268 } else {
1269 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
1270 hparams = (SWR_HSTOP_MAX_VAL << 4) | SWR_HSTART_MIN_VAL;
1271 val[len++] = hparams;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301272 }
1273 if (mport->blk_pack_mode != SWR_INVALID_PARAM) {
1274 reg[len] = SWRM_DP_BLOCK_CTRL3_BANK(i + 1, bank);
1275 val[len++] = mport->blk_pack_mode;
1276 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301277 mport->ch_en = mport->req_ch;
1278
1279 }
Sudheer Papothi0016db12019-06-11 04:42:38 +05301280 swrm_reg_dump(swrm, reg, val, len, __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301281 swr_master_bulk_write(swrm, reg, val, len);
1282}
1283
1284static void swrm_apply_port_config(struct swr_master *master)
1285{
1286 u8 bank;
1287 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1288
1289 if (!swrm) {
1290 pr_err("%s: Invalid handle to swr controller\n",
1291 __func__);
1292 return;
1293 }
1294
1295 bank = get_inactive_bank_num(swrm);
1296 dev_dbg(swrm->dev, "%s: enter bank: %d master_ports: %d\n",
1297 __func__, bank, master->num_port);
1298
Vatsal Bucha80995e62020-02-20 14:35:44 +05301299
1300 swrm_cmd_fifo_wr_cmd(swrm, 0x01, 0xF, 0x00,
1301 SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(bank));
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301302
1303 swrm_copy_data_port_config(master, bank);
1304}
1305
1306static int swrm_slvdev_datapath_control(struct swr_master *master, bool enable)
1307{
1308 u8 bank;
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301309 u32 value = 0, n_row = 0, n_col = 0;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301310 u32 row = 0, col = 0;
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301311 int bus_clk_div_factor;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301312 int ret;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301313 u8 ssp_period = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301314 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1315 int mask = (SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK |
1316 SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK |
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301317 SWRM_MCP_FRAME_CTRL_BANK_CLK_DIV_VALUE_BMSK |
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301318 SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_BMSK);
1319 u8 inactive_bank;
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +05301320 int frame_sync = SWRM_FRAME_SYNC_SEL;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301321
1322 if (!swrm) {
1323 pr_err("%s: swrm is null\n", __func__);
1324 return -EFAULT;
1325 }
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301326
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301327 mutex_lock(&swrm->mlock);
1328
Ramprasad Katkam979b7c92019-05-17 15:31:21 +05301329 /*
1330 * During disable if master is already down, which implies an ssr/pdr
1331 * scenario, just mark ports as disabled and exit
1332 */
1333 if (swrm->state == SWR_MSTR_SSR && !enable) {
1334 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1335 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1336 __func__);
1337 goto exit;
1338 }
1339 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
1340 swrm_cleanup_disabled_port_reqs(master);
1341 if (!swrm_is_port_en(master)) {
1342 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1343 __func__);
1344 pm_runtime_mark_last_busy(swrm->dev);
1345 pm_runtime_put_autosuspend(swrm->dev);
1346 }
1347 goto exit;
1348 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301349 bank = get_inactive_bank_num(swrm);
1350
1351 if (enable) {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301352 if (!test_bit(ENABLE_PENDING, &swrm->port_req_pending)) {
1353 dev_dbg(swrm->dev, "%s:No pending connect port req\n",
1354 __func__);
1355 goto exit;
1356 }
1357 clear_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301358 ret = swrm_get_port_config(swrm);
1359 if (ret) {
1360 /* cannot accommodate ports */
1361 swrm_cleanup_disabled_port_reqs(master);
1362 mutex_unlock(&swrm->mlock);
1363 return -EINVAL;
1364 }
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301365 swr_master_write(swrm, SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301366 SWRM_INTERRUPT_STATUS_MASK);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301367 /* apply the new port config*/
1368 swrm_apply_port_config(master);
1369 } else {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301370 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1371 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1372 __func__);
1373 goto exit;
1374 }
1375 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301376 swrm_disable_ports(master, bank);
1377 }
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301378 dev_dbg(swrm->dev, "%s: enable: %d, cfg_devs: %d freq %d\n",
1379 __func__, enable, swrm->num_cfg_devs, swrm->mclk_freq);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301380
1381 if (enable) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301382 /* set col = 16 */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301383 n_col = SWR_MAX_COL;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301384 col = SWRM_COL_16;
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301385 if (swrm->bus_clk == MCLK_FREQ_LP) {
1386 n_col = SWR_MIN_COL;
1387 col = SWRM_COL_02;
1388 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301389 } else {
1390 /*
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301391 * Do not change to col = 2 if there are still active ports
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301392 */
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301393 if (!master->num_port) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301394 n_col = SWR_MIN_COL;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301395 col = SWRM_COL_02;
1396 } else {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301397 n_col = SWR_MAX_COL;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301398 col = SWRM_COL_16;
1399 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301400 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301401 /* Use default 50 * x, frame shape. Change based on mclk */
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301402 if (swrm->mclk_freq == MCLK_FREQ_NATIVE) {
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301403 dev_dbg(swrm->dev, "setting 64 x %d frameshape\n", col);
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301404 n_row = SWR_ROW_64;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301405 row = SWRM_ROW_64;
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +05301406 frame_sync = SWRM_FRAME_SYNC_SEL_NATIVE;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301407 } else {
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301408 dev_dbg(swrm->dev, "setting 50 x %d frameshape\n", col);
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301409 n_row = SWR_ROW_50;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301410 row = SWRM_ROW_50;
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +05301411 frame_sync = SWRM_FRAME_SYNC_SEL;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301412 }
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +05301413 ssp_period = swrm_get_ssp_period(swrm, row, col, frame_sync);
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301414 bus_clk_div_factor = swrm_get_clk_div(swrm->mclk_freq, swrm->bus_clk);
1415 dev_dbg(swrm->dev, "%s: ssp_period: %d, bus_clk_div:%d \n", __func__,
1416 ssp_period, bus_clk_div_factor);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301417 value = swr_master_read(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank));
1418 value &= (~mask);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301419 value |= ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301420 (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301421 (bus_clk_div_factor <<
1422 SWRM_MCP_FRAME_CTRL_BANK_CLK_DIV_VALUE_SHFT) |
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301423 ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301424 swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1425
1426 dev_dbg(swrm->dev, "%s: regaddr: 0x%x, value: 0x%x\n", __func__,
1427 SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1428
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301429 enable_bank_switch(swrm, bank, n_row, n_col);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301430 inactive_bank = bank ? 0 : 1;
1431
1432 if (enable)
1433 swrm_copy_data_port_config(master, inactive_bank);
1434 else {
1435 swrm_disable_ports(master, inactive_bank);
1436 swrm_cleanup_disabled_port_reqs(master);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301437 }
1438 if (!swrm_is_port_en(master)) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301439 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1440 __func__);
1441 pm_runtime_mark_last_busy(swrm->dev);
1442 pm_runtime_put_autosuspend(swrm->dev);
1443 }
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301444exit:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301445 mutex_unlock(&swrm->mlock);
1446return 0;
1447}
1448
1449static int swrm_connect_port(struct swr_master *master,
1450 struct swr_params *portinfo)
1451{
1452 int i;
1453 struct swr_port_info *port_req;
1454 int ret = 0;
1455 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1456 struct swrm_mports *mport;
1457 u8 mstr_port_id, mstr_ch_msk;
1458
1459 dev_dbg(&master->dev, "%s: enter\n", __func__);
1460 if (!portinfo)
1461 return -EINVAL;
1462
1463 if (!swrm) {
1464 dev_err(&master->dev,
1465 "%s: Invalid handle to swr controller\n",
1466 __func__);
1467 return -EINVAL;
1468 }
1469
1470 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301471 mutex_lock(&swrm->devlock);
1472 if (!swrm->dev_up) {
1473 mutex_unlock(&swrm->devlock);
1474 mutex_unlock(&swrm->mlock);
1475 return -EINVAL;
1476 }
1477 mutex_unlock(&swrm->devlock);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301478 if (!swrm_is_port_en(master))
1479 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301480
1481 for (i = 0; i < portinfo->num_port; i++) {
1482 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_msk,
1483 portinfo->port_type[i],
1484 portinfo->port_id[i]);
1485 if (ret) {
1486 dev_err(&master->dev,
1487 "%s: mstr portid for slv port %d not found\n",
1488 __func__, portinfo->port_id[i]);
1489 goto port_fail;
1490 }
1491
1492 mport = &(swrm->mport_cfg[mstr_port_id]);
1493 /* get port req */
1494 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1495 portinfo->dev_num);
1496 if (!port_req) {
1497 dev_dbg(&master->dev, "%s: new req:port id %d dev %d\n",
1498 __func__, portinfo->port_id[i],
1499 portinfo->dev_num);
1500 port_req = kzalloc(sizeof(struct swr_port_info),
1501 GFP_KERNEL);
1502 if (!port_req) {
1503 ret = -ENOMEM;
1504 goto mem_fail;
1505 }
1506 port_req->dev_num = portinfo->dev_num;
1507 port_req->slave_port_id = portinfo->port_id[i];
1508 port_req->num_ch = portinfo->num_ch[i];
1509 port_req->ch_rate = portinfo->ch_rate[i];
1510 port_req->ch_en = 0;
1511 port_req->master_port_id = mstr_port_id;
1512 list_add(&port_req->list, &mport->port_req_list);
1513 }
1514 port_req->req_ch |= portinfo->ch_en[i];
1515
1516 dev_dbg(&master->dev,
1517 "%s: mstr port %d, slv port %d ch_rate %d num_ch %d\n",
1518 __func__, port_req->master_port_id,
1519 port_req->slave_port_id, port_req->ch_rate,
1520 port_req->num_ch);
1521 /* Put the port req on master port */
1522 mport = &(swrm->mport_cfg[mstr_port_id]);
1523 mport->port_en = true;
1524 mport->req_ch |= mstr_ch_msk;
1525 master->port_en_mask |= (1 << mstr_port_id);
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301526 if (swrm->clk_stop_mode0_supp &&
1527 (mport->ch_rate < portinfo->ch_rate[i])) {
1528 mport->ch_rate = portinfo->ch_rate[i];
1529 swrm_update_bus_clk(swrm);
1530 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301531 }
1532 master->num_port += portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301533 set_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301534 swr_port_response(master, portinfo->tid);
1535
1536 mutex_unlock(&swrm->mlock);
1537 return 0;
1538
1539port_fail:
1540mem_fail:
1541 /* cleanup port reqs in error condition */
1542 swrm_cleanup_disabled_port_reqs(master);
1543 mutex_unlock(&swrm->mlock);
1544 return ret;
1545}
1546
1547static int swrm_disconnect_port(struct swr_master *master,
1548 struct swr_params *portinfo)
1549{
1550 int i, ret = 0;
1551 struct swr_port_info *port_req;
1552 struct swrm_mports *mport;
1553 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1554 u8 mstr_port_id, mstr_ch_mask;
1555
1556 if (!swrm) {
1557 dev_err(&master->dev,
1558 "%s: Invalid handle to swr controller\n",
1559 __func__);
1560 return -EINVAL;
1561 }
1562
1563 if (!portinfo) {
1564 dev_err(&master->dev, "%s: portinfo is NULL\n", __func__);
1565 return -EINVAL;
1566 }
1567 mutex_lock(&swrm->mlock);
1568
1569 for (i = 0; i < portinfo->num_port; i++) {
1570
1571 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_mask,
1572 portinfo->port_type[i], portinfo->port_id[i]);
1573 if (ret) {
1574 dev_err(&master->dev,
1575 "%s: mstr portid for slv port %d not found\n",
1576 __func__, portinfo->port_id[i]);
1577 mutex_unlock(&swrm->mlock);
1578 return -EINVAL;
1579 }
1580 mport = &(swrm->mport_cfg[mstr_port_id]);
1581 /* get port req */
1582 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1583 portinfo->dev_num);
1584
1585 if (!port_req) {
1586 dev_err(&master->dev, "%s:port not enabled : port %d\n",
1587 __func__, portinfo->port_id[i]);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05301588 mutex_unlock(&swrm->mlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301589 return -EINVAL;
1590 }
1591 port_req->req_ch &= ~portinfo->ch_en[i];
1592 mport->req_ch &= ~mstr_ch_mask;
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301593 if (swrm->clk_stop_mode0_supp && !mport->req_ch) {
1594 mport->ch_rate = 0;
1595 swrm_update_bus_clk(swrm);
1596 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301597 }
1598 master->num_port -= portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301599 set_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301600 swr_port_response(master, portinfo->tid);
1601 mutex_unlock(&swrm->mlock);
1602
1603 return 0;
1604}
1605
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301606static int swrm_find_alert_slave(struct swr_mstr_ctrl *swrm,
1607 int status, u8 *devnum)
1608{
1609 int i;
1610 bool found = false;
1611
1612 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1613 if ((status & SWRM_MCP_SLV_STATUS_MASK) == SWR_ALERT) {
1614 *devnum = i;
1615 found = true;
1616 break;
1617 }
1618 status >>= 2;
1619 }
1620 if (found)
1621 return 0;
1622 else
1623 return -EINVAL;
1624}
1625
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301626static void swrm_enable_slave_irq(struct swr_mstr_ctrl *swrm)
1627{
1628 int i;
1629 int status = 0;
1630
1631 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1632 if (!status) {
1633 dev_dbg_ratelimited(swrm->dev, "%s: slaves status is 0x%x\n",
1634 __func__, status);
1635 return;
1636 }
1637 dev_dbg(swrm->dev, "%s: slave status: 0x%x\n", __func__, status);
1638 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
Vatsal Bucha21ba0772020-02-28 20:36:46 +05301639 if (status & SWRM_MCP_SLV_STATUS_MASK) {
1640 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, i, 0x0,
1641 SWRS_SCP_INT_STATUS_CLEAR_1);
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301642 swrm_cmd_fifo_wr_cmd(swrm, 0x4, i, 0x0,
Vatsal Bucha21ba0772020-02-28 20:36:46 +05301643 SWRS_SCP_INT_STATUS_MASK_1);
1644 }
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301645 status >>= 2;
1646 }
1647}
1648
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301649static int swrm_check_slave_change_status(struct swr_mstr_ctrl *swrm,
1650 int status, u8 *devnum)
1651{
1652 int i;
1653 int new_sts = status;
1654 int ret = SWR_NOT_PRESENT;
1655
1656 if (status != swrm->slave_status) {
1657 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1658 if ((status & SWRM_MCP_SLV_STATUS_MASK) !=
1659 (swrm->slave_status & SWRM_MCP_SLV_STATUS_MASK)) {
1660 ret = (status & SWRM_MCP_SLV_STATUS_MASK);
1661 *devnum = i;
1662 break;
1663 }
1664 status >>= 2;
1665 swrm->slave_status >>= 2;
1666 }
1667 swrm->slave_status = new_sts;
1668 }
1669 return ret;
1670}
1671
1672static irqreturn_t swr_mstr_interrupt(int irq, void *dev)
1673{
1674 struct swr_mstr_ctrl *swrm = dev;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301675 u32 value, intr_sts, intr_sts_masked;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301676 u32 temp = 0;
1677 u32 status, chg_sts, i;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301678 u8 devnum = 0;
1679 int ret = IRQ_HANDLED;
1680 struct swr_device *swr_dev;
1681 struct swr_master *mstr = &swrm->master;
1682
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07001683 trace_printk("%s enter\n", __func__);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301684 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1685 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1686 return IRQ_NONE;
1687 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301688
1689 mutex_lock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301690 if (swrm_clk_request(swrm, true)) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +05301691 dev_err_ratelimited(swrm->dev, "%s:clk request failed\n",
1692 __func__);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301693 mutex_unlock(&swrm->reslock);
1694 goto exit;
1695 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301696 mutex_unlock(&swrm->reslock);
1697
1698 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301699 intr_sts_masked = intr_sts & swrm->intr_mask;
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07001700
1701 trace_printk("%s: status: 0x%x \n", __func__, intr_sts_masked);
Ramprasad Katkam83303512018-10-11 17:34:22 +05301702handle_irq:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301703 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301704 value = intr_sts_masked & (1 << i);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301705 if (!value)
1706 continue;
1707
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301708 switch (value) {
1709 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1710 dev_dbg(swrm->dev, "Trigger irq to slave device\n");
1711 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301712 ret = swrm_find_alert_slave(swrm, status, &devnum);
1713 if (ret) {
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301714 dev_err_ratelimited(swrm->dev,
1715 "no slave alert found.spurious interrupt\n");
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05301716 break;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301717 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301718 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1719 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1720 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1721 SWRS_SCP_INT_STATUS_CLEAR_1);
1722 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1723 SWRS_SCP_INT_STATUS_CLEAR_1);
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301724
1725
1726 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1727 if (swr_dev->dev_num != devnum)
1728 continue;
1729 if (swr_dev->slave_irq) {
1730 do {
Ramprasad Katkam2586a4b2019-03-18 16:53:39 +05301731 swr_dev->slave_irq_pending = 0;
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301732 handle_nested_irq(
1733 irq_find_mapping(
1734 swr_dev->slave_irq, 0));
1735 } while (swr_dev->slave_irq_pending);
1736 }
1737
1738 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301739 break;
1740 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1741 dev_dbg(swrm->dev, "SWR new slave attached\n");
1742 break;
1743 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1744 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1745 if (status == swrm->slave_status) {
1746 dev_dbg(swrm->dev,
1747 "%s: No change in slave status: %d\n",
1748 __func__, status);
1749 break;
1750 }
1751 chg_sts = swrm_check_slave_change_status(swrm, status,
1752 &devnum);
1753 switch (chg_sts) {
1754 case SWR_NOT_PRESENT:
1755 dev_dbg(swrm->dev, "device %d got detached\n",
1756 devnum);
1757 break;
1758 case SWR_ATTACHED_OK:
1759 dev_dbg(swrm->dev, "device %d got attached\n",
1760 devnum);
Ramprasad Katkamdebe8932018-09-25 18:08:18 +05301761 /* enable host irq from slave device*/
1762 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1763 SWRS_SCP_INT_STATUS_CLEAR_1);
1764 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1765 SWRS_SCP_INT_STATUS_MASK_1);
1766
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301767 break;
1768 case SWR_ALERT:
1769 dev_dbg(swrm->dev,
1770 "device %d has pending interrupt\n",
1771 devnum);
1772 break;
1773 }
1774 break;
1775 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1776 dev_err_ratelimited(swrm->dev,
1777 "SWR bus clsh detected\n");
1778 break;
1779 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1780 dev_dbg(swrm->dev, "SWR read FIFO overflow\n");
1781 break;
1782 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1783 dev_dbg(swrm->dev, "SWR read FIFO underflow\n");
1784 break;
1785 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1786 dev_dbg(swrm->dev, "SWR write FIFO overflow\n");
1787 break;
1788 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1789 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1790 dev_err_ratelimited(swrm->dev,
1791 "SWR CMD error, fifo status 0x%x, flushing fifo\n",
1792 value);
1793 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1794 break;
1795 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301796 dev_err_ratelimited(swrm->dev, "SWR Port collision detected\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301797 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301798 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301799 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301800 break;
1801 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1802 dev_dbg(swrm->dev, "SWR read enable valid mismatch\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301803 swrm->intr_mask &=
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301804 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1805 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301806 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301807 break;
1808 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1809 complete(&swrm->broadcast);
1810 dev_dbg(swrm->dev, "SWR cmd id finished\n");
1811 break;
1812 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_AUTO_ENUM_FINISHED:
1813 break;
1814 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED:
1815 break;
1816 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL:
1817 break;
1818 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED:
1819 complete(&swrm->reset);
1820 break;
1821 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED:
1822 break;
1823 default:
1824 dev_err_ratelimited(swrm->dev,
1825 "SWR unknown interrupt\n");
1826 ret = IRQ_NONE;
1827 break;
1828 }
1829 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301830 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1831 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
Ramprasad Katkam83303512018-10-11 17:34:22 +05301832
1833 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301834 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301835
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301836 if (intr_sts_masked) {
Ramprasad Katkam83303512018-10-11 17:34:22 +05301837 dev_dbg(swrm->dev, "%s: new interrupt received\n", __func__);
1838 goto handle_irq;
1839 }
1840
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301841 mutex_lock(&swrm->reslock);
1842 swrm_clk_request(swrm, false);
1843 mutex_unlock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301844exit:
Ramprasad Katkam57349872018-11-11 18:34:57 +05301845 swrm_unlock_sleep(swrm);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07001846 trace_printk("%s exit\n", __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301847 return ret;
1848}
1849
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301850static irqreturn_t swr_mstr_interrupt_v2(int irq, void *dev)
1851{
1852 struct swr_mstr_ctrl *swrm = dev;
1853 u32 value, intr_sts, intr_sts_masked;
1854 u32 temp = 0;
1855 u32 status, chg_sts, i;
1856 u8 devnum = 0;
1857 int ret = IRQ_HANDLED;
1858 struct swr_device *swr_dev;
1859 struct swr_master *mstr = &swrm->master;
1860
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07001861 trace_printk("%s enter\n", __func__);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301862 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1863 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1864 return IRQ_NONE;
1865 }
1866
1867 mutex_lock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301868 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
1869 ret = IRQ_NONE;
1870 goto exit;
1871 }
1872 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
1873 ret = IRQ_NONE;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301874 goto err_audio_hw_vote;
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001875 }
Karthikeyan Mani4bee1db2019-09-18 17:58:41 -07001876 ret = swrm_clk_request(swrm, true);
1877 if (ret) {
1878 dev_err(dev, "%s: swrm clk failed\n", __func__);
1879 ret = IRQ_NONE;
1880 goto err_audio_core_vote;
1881 }
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301882 mutex_unlock(&swrm->reslock);
1883
1884 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1885 intr_sts_masked = intr_sts & swrm->intr_mask;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301886
1887 dev_dbg(swrm->dev, "%s: status: 0x%x \n", __func__, intr_sts_masked);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07001888 trace_printk("%s: status: 0x%x \n", __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301889handle_irq:
1890 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
1891 value = intr_sts_masked & (1 << i);
1892 if (!value)
1893 continue;
1894
1895 switch (value) {
1896 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1897 dev_dbg(swrm->dev, "%s: Trigger irq to slave device\n",
1898 __func__);
1899 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1900 ret = swrm_find_alert_slave(swrm, status, &devnum);
1901 if (ret) {
1902 dev_err_ratelimited(swrm->dev,
1903 "%s: no slave alert found.spurious interrupt\n",
1904 __func__);
1905 break;
1906 }
1907 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1908 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1909 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1910 SWRS_SCP_INT_STATUS_CLEAR_1);
1911 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1912 SWRS_SCP_INT_STATUS_CLEAR_1);
1913
1914
1915 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1916 if (swr_dev->dev_num != devnum)
1917 continue;
1918 if (swr_dev->slave_irq) {
1919 do {
Meng Wang31a7ef12019-12-18 10:36:29 +08001920 swr_dev->slave_irq_pending = 0;
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301921 handle_nested_irq(
1922 irq_find_mapping(
1923 swr_dev->slave_irq, 0));
1924 } while (swr_dev->slave_irq_pending);
1925 }
1926
1927 }
1928 break;
1929 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1930 dev_dbg(swrm->dev, "%s: SWR new slave attached\n",
1931 __func__);
1932 break;
1933 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1934 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
Laxminath Kasam44cedb82019-11-20 17:37:23 +05301935 swrm_enable_slave_irq(swrm);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301936 if (status == swrm->slave_status) {
1937 dev_dbg(swrm->dev,
1938 "%s: No change in slave status: %d\n",
1939 __func__, status);
1940 break;
1941 }
1942 chg_sts = swrm_check_slave_change_status(swrm, status,
1943 &devnum);
1944 switch (chg_sts) {
1945 case SWR_NOT_PRESENT:
1946 dev_dbg(swrm->dev,
1947 "%s: device %d got detached\n",
1948 __func__, devnum);
Vatsal Bucha21ba0772020-02-28 20:36:46 +05301949 if (devnum == 0) {
1950 /*
1951 * enable host irq if device 0 detached
1952 * as hw will mask host_irq at slave
1953 * but will not unmask it afterwards.
1954 */
1955 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1956 SWRS_SCP_INT_STATUS_CLEAR_1);
1957 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1958 SWRS_SCP_INT_STATUS_MASK_1);
1959 }
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301960 break;
1961 case SWR_ATTACHED_OK:
1962 dev_dbg(swrm->dev,
1963 "%s: device %d got attached\n",
1964 __func__, devnum);
1965 /* enable host irq from slave device*/
1966 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1967 SWRS_SCP_INT_STATUS_CLEAR_1);
1968 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1969 SWRS_SCP_INT_STATUS_MASK_1);
1970
1971 break;
1972 case SWR_ALERT:
1973 dev_dbg(swrm->dev,
1974 "%s: device %d has pending interrupt\n",
1975 __func__, devnum);
1976 break;
1977 }
1978 break;
1979 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1980 dev_err_ratelimited(swrm->dev,
1981 "%s: SWR bus clsh detected\n",
1982 __func__);
1983 break;
1984 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1985 dev_dbg(swrm->dev, "%s: SWR read FIFO overflow\n",
1986 __func__);
1987 break;
1988 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1989 dev_dbg(swrm->dev, "%s: SWR read FIFO underflow\n",
1990 __func__);
1991 break;
1992 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1993 dev_dbg(swrm->dev, "%s: SWR write FIFO overflow\n",
1994 __func__);
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301995 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301996 break;
1997 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1998 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1999 dev_err_ratelimited(swrm->dev,
2000 "%s: SWR CMD error, fifo status 0x%x, flushing fifo\n",
2001 __func__, value);
2002 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
2003 break;
2004 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
2005 dev_err_ratelimited(swrm->dev,
2006 "%s: SWR Port collision detected\n",
2007 __func__);
2008 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
2009 swr_master_write(swrm,
2010 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
2011 break;
2012 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
2013 dev_dbg(swrm->dev,
2014 "%s: SWR read enable valid mismatch\n",
2015 __func__);
2016 swrm->intr_mask &=
2017 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
2018 swr_master_write(swrm,
2019 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
2020 break;
2021 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
2022 complete(&swrm->broadcast);
2023 dev_dbg(swrm->dev, "%s: SWR cmd id finished\n",
2024 __func__);
2025 break;
2026 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED_V2:
2027 break;
2028 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL_V2:
2029 break;
2030 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED_V2:
Laxminath Kasame2291972019-11-08 14:51:59 +05302031 swrm_check_link_status(swrm, 0x1);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302032 break;
2033 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED_V2:
2034 break;
2035 case SWRM_INTERRUPT_STATUS_EXT_CLK_STOP_WAKEUP:
2036 if (swrm->state == SWR_MSTR_UP)
2037 dev_dbg(swrm->dev,
2038 "%s:SWR Master is already up\n",
2039 __func__);
2040 else
2041 dev_err_ratelimited(swrm->dev,
2042 "%s: SWR wokeup during clock stop\n",
2043 __func__);
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05302044 /* It might be possible the slave device gets reset
2045 * and slave interrupt gets missed. So re-enable
2046 * Host IRQ and process slave pending
2047 * interrupts, if any.
2048 */
2049 swrm_enable_slave_irq(swrm);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302050 break;
2051 default:
2052 dev_err_ratelimited(swrm->dev,
2053 "%s: SWR unknown interrupt value: %d\n",
2054 __func__, value);
2055 ret = IRQ_NONE;
2056 break;
2057 }
2058 }
2059 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
2060 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
2061
2062 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
2063 intr_sts_masked = intr_sts & swrm->intr_mask;
2064
2065 if (intr_sts_masked) {
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05302066 dev_dbg(swrm->dev, "%s: new interrupt received 0x%x\n",
2067 __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302068 goto handle_irq;
2069 }
2070
2071 mutex_lock(&swrm->reslock);
2072 swrm_clk_request(swrm, false);
Karthikeyan Mani4bee1db2019-09-18 17:58:41 -07002073err_audio_core_vote:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302074 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
Sudheer Papothi06f43412019-07-09 03:32:54 +05302075
2076err_audio_hw_vote:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302077 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07002078exit:
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302079 mutex_unlock(&swrm->reslock);
2080 swrm_unlock_sleep(swrm);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07002081 trace_printk("%s exit\n", __func__);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302082 return ret;
2083}
2084
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302085static irqreturn_t swrm_wakeup_interrupt(int irq, void *dev)
2086{
2087 struct swr_mstr_ctrl *swrm = dev;
2088 int ret = IRQ_HANDLED;
2089
2090 if (!swrm || !(swrm->dev)) {
2091 pr_err("%s: swrm or dev is null\n", __func__);
2092 return IRQ_NONE;
2093 }
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302094
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07002095 trace_printk("%s enter\n", __func__);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302096 mutex_lock(&swrm->devlock);
2097 if (!swrm->dev_up) {
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302098 if (swrm->wake_irq > 0) {
2099 if (unlikely(!irq_get_irq_data(swrm->wake_irq))) {
2100 pr_err("%s: irq data is NULL\n", __func__);
2101 mutex_unlock(&swrm->devlock);
2102 return IRQ_NONE;
2103 }
2104 mutex_lock(&swrm->irq_lock);
2105 if (!irqd_irq_disabled(
2106 irq_get_irq_data(swrm->wake_irq)))
2107 disable_irq_nosync(swrm->wake_irq);
2108 mutex_unlock(&swrm->irq_lock);
2109 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302110 mutex_unlock(&swrm->devlock);
2111 return ret;
2112 }
2113 mutex_unlock(&swrm->devlock);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05302114 if (unlikely(swrm_lock_sleep(swrm) == false)) {
2115 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
2116 goto exit;
2117 }
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302118 if (swrm->wake_irq > 0) {
2119 if (unlikely(!irq_get_irq_data(swrm->wake_irq))) {
2120 pr_err("%s: irq data is NULL\n", __func__);
2121 return IRQ_NONE;
2122 }
2123 mutex_lock(&swrm->irq_lock);
2124 if (!irqd_irq_disabled(
2125 irq_get_irq_data(swrm->wake_irq)))
2126 disable_irq_nosync(swrm->wake_irq);
2127 mutex_unlock(&swrm->irq_lock);
2128 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302129 pm_runtime_get_sync(swrm->dev);
2130 pm_runtime_mark_last_busy(swrm->dev);
2131 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05302132 swrm_unlock_sleep(swrm);
2133exit:
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07002134 trace_printk("%s exit\n", __func__);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302135 return ret;
2136}
2137
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302138static void swrm_wakeup_work(struct work_struct *work)
2139{
2140 struct swr_mstr_ctrl *swrm;
2141
2142 swrm = container_of(work, struct swr_mstr_ctrl,
2143 wakeup_work);
2144 if (!swrm || !(swrm->dev)) {
2145 pr_err("%s: swrm or dev is null\n", __func__);
2146 return;
2147 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302148
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07002149 trace_printk("%s enter\n", __func__);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302150 mutex_lock(&swrm->devlock);
2151 if (!swrm->dev_up) {
2152 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302153 goto exit;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302154 }
2155 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302156 if (unlikely(swrm_lock_sleep(swrm) == false)) {
2157 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
2158 goto exit;
2159 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302160 pm_runtime_get_sync(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302161 pm_runtime_mark_last_busy(swrm->dev);
2162 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302163 swrm_unlock_sleep(swrm);
2164exit:
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07002165 trace_printk("%s exit\n", __func__);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302166 pm_relax(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302167}
2168
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302169static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
2170{
2171 u32 val;
2172
2173 swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
2174 val = (swrm->slave_status >> (devnum * 2));
2175 val &= SWRM_MCP_SLV_STATUS_MASK;
2176 return val;
2177}
2178
2179static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
2180 u8 *dev_num)
2181{
2182 int i;
2183 u64 id = 0;
2184 int ret = -EINVAL;
2185 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
2186 struct swr_device *swr_dev;
2187 u32 num_dev = 0;
2188
2189 if (!swrm) {
2190 pr_err("%s: Invalid handle to swr controller\n",
2191 __func__);
2192 return ret;
2193 }
2194 if (swrm->num_dev)
2195 num_dev = swrm->num_dev;
2196 else
2197 num_dev = mstr->num_dev;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302198
2199 mutex_lock(&swrm->devlock);
2200 if (!swrm->dev_up) {
2201 mutex_unlock(&swrm->devlock);
2202 return ret;
2203 }
2204 mutex_unlock(&swrm->devlock);
2205
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302206 pm_runtime_get_sync(swrm->dev);
2207 for (i = 1; i < (num_dev + 1); i++) {
2208 id = ((u64)(swr_master_read(swrm,
2209 SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
2210 id |= swr_master_read(swrm,
2211 SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302212
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302213 /*
2214 * As pm_runtime_get_sync() brings all slaves out of reset
2215 * update logical device number for all slaves.
2216 */
2217 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2218 if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
2219 u32 status = swrm_get_device_status(swrm, i);
2220
2221 if ((status == 0x01) || (status == 0x02)) {
2222 swr_dev->dev_num = i;
2223 if ((id & SWR_DEV_ID_MASK) == dev_id) {
2224 *dev_num = i;
2225 ret = 0;
2226 }
2227 dev_dbg(swrm->dev,
Xiao Lid8bb93c2020-01-07 12:59:05 +08002228 "%s: devnum %d is assigned for dev addr %llx\n",
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302229 __func__, i, swr_dev->addr);
2230 }
2231 }
2232 }
2233 }
2234 if (ret)
2235 dev_err(swrm->dev, "%s: device 0x%llx is not ready\n",
2236 __func__, dev_id);
2237
2238 pm_runtime_mark_last_busy(swrm->dev);
2239 pm_runtime_put_autosuspend(swrm->dev);
2240 return ret;
2241}
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302242
2243static void swrm_device_wakeup_vote(struct swr_master *mstr)
2244{
2245 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
2246
2247 if (!swrm) {
2248 pr_err("%s: Invalid handle to swr controller\n",
2249 __func__);
2250 return;
2251 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302252 if (unlikely(swrm_lock_sleep(swrm) == false)) {
2253 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
2254 return;
2255 }
Sudheer Papothi384addd2019-06-14 02:26:52 +05302256 if (++swrm->hw_core_clk_en == 1)
2257 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2258 dev_err(swrm->dev, "%s:lpass core hw enable failed\n",
2259 __func__);
2260 --swrm->hw_core_clk_en;
2261 }
2262 if ( ++swrm->aud_core_clk_en == 1)
2263 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2264 dev_err(swrm->dev, "%s:lpass audio hw enable failed\n",
2265 __func__);
2266 --swrm->aud_core_clk_en;
2267 }
2268 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
2269 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07002270 trace_printk("%s: hw_clk_en: %d audio_core_clk_en: %d\n",
2271 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302272 pm_runtime_get_sync(swrm->dev);
2273}
2274
2275static void swrm_device_wakeup_unvote(struct swr_master *mstr)
2276{
2277 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
2278
2279 if (!swrm) {
2280 pr_err("%s: Invalid handle to swr controller\n",
2281 __func__);
2282 return;
2283 }
2284 pm_runtime_mark_last_busy(swrm->dev);
2285 pm_runtime_put_autosuspend(swrm->dev);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302286 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
2287 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
2288
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07002289 trace_printk("%s: hw_clk_en: %d audio_core_clk_en: %d\n",
2290 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302291 --swrm->aud_core_clk_en;
2292 if (swrm->aud_core_clk_en < 0)
2293 swrm->aud_core_clk_en = 0;
2294 else if (swrm->aud_core_clk_en == 0)
2295 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2296
2297 --swrm->hw_core_clk_en;
2298 if (swrm->hw_core_clk_en < 0)
2299 swrm->hw_core_clk_en = 0;
2300 else if (swrm->hw_core_clk_en == 0)
2301 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
2302
Ramprasad Katkam57349872018-11-11 18:34:57 +05302303 swrm_unlock_sleep(swrm);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302304}
2305
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302306static int swrm_master_init(struct swr_mstr_ctrl *swrm)
2307{
2308 int ret = 0;
2309 u32 val;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302310 u8 row_ctrl = SWR_ROW_50;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302311 u8 col_ctrl = SWR_MIN_COL;
2312 u8 ssp_period = 1;
2313 u8 retry_cmd_num = 3;
2314 u32 reg[SWRM_MAX_INIT_REG];
2315 u32 value[SWRM_MAX_INIT_REG];
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302316 u32 temp = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302317 int len = 0;
2318
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302319 ssp_period = swrm_get_ssp_period(swrm, SWRM_ROW_50,
2320 SWRM_COL_02, SWRM_FRAME_SYNC_SEL);
2321 dev_dbg(swrm->dev, "%s: ssp_period: %d\n", __func__, ssp_period);
2322
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302323 /* Clear Rows and Cols */
2324 val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
2325 (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302326 ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302327
2328 reg[len] = SWRM_MCP_FRAME_CTRL_BANK_ADDR(0);
2329 value[len++] = val;
2330
2331 /* Set Auto enumeration flag */
2332 reg[len] = SWRM_ENUMERATOR_CFG_ADDR;
2333 value[len++] = 1;
2334
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302335 /* Configure No pings */
2336 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2337 val &= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK;
2338 val |= (0x1f << SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT);
2339 reg[len] = SWRM_MCP_CFG_ADDR;
2340 value[len++] = val;
2341
2342 /* Configure number of retries of a read/write cmd */
2343 val = (retry_cmd_num << SWRM_CMD_FIFO_CFG_NUM_OF_CMD_RETRY_SHFT);
2344 reg[len] = SWRM_CMD_FIFO_CFG_ADDR;
2345 value[len++] = val;
2346
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302347 reg[len] = SWRM_MCP_BUS_CTRL_ADDR;
2348 value[len++] = 0x2;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302349
Ramprasad Katkam83303512018-10-11 17:34:22 +05302350 /* Set IRQ to PULSE */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302351 reg[len] = SWRM_COMP_CFG_ADDR;
Ramprasad Katkam83303512018-10-11 17:34:22 +05302352 value[len++] = 0x02;
2353
2354 reg[len] = SWRM_COMP_CFG_ADDR;
2355 value[len++] = 0x03;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302356
2357 reg[len] = SWRM_INTERRUPT_CLEAR;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302358 value[len++] = 0xFFFFFFFF;
2359
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302360 swrm->intr_mask = SWRM_INTERRUPT_STATUS_MASK;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302361 /* Mask soundwire interrupts */
2362 reg[len] = SWRM_INTERRUPT_MASK_ADDR;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302363 value[len++] = swrm->intr_mask;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302364
2365 reg[len] = SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302366 value[len++] = swrm->intr_mask;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302367
2368 swr_master_bulk_write(swrm, reg, value, len);
2369
Laxminath Kasamcafe0732019-11-20 17:31:58 +05302370 if (!swrm_check_link_status(swrm, 0x1)) {
2371 dev_err(swrm->dev,
2372 "%s: swr link failed to connect\n",
2373 __func__);
2374 return -EINVAL;
2375 }
Sudheer Papothi63f48152018-11-15 01:08:03 +05302376 /*
2377 * For SWR master version 1.5.1, continue
2378 * execute on command ignore.
2379 */
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302380 /* Execute it for versions >= 1.5.1 */
2381 if (swrm->version >= SWRM_VERSION_1_5_1)
Sudheer Papothi63f48152018-11-15 01:08:03 +05302382 swr_master_write(swrm, SWRM_CMD_FIFO_CFG_ADDR,
2383 (swr_master_read(swrm,
2384 SWRM_CMD_FIFO_CFG_ADDR) | 0x80000000));
2385
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302386 /* SW workaround to gate hw_ctl for SWR version >=1.6 */
2387 if (swrm->version >= SWRM_VERSION_1_6) {
2388 if (swrm->swrm_hctl_reg) {
2389 temp = ioread32(swrm->swrm_hctl_reg);
2390 temp &= 0xFFFFFFFD;
2391 iowrite32(temp, swrm->swrm_hctl_reg);
2392 }
2393 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302394 return ret;
2395}
2396
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302397static int swrm_event_notify(struct notifier_block *self,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302398 unsigned long action, void *data)
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302399{
2400 struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302401 event_notifier);
2402
2403 if (!swrm || !(swrm->dev)) {
2404 pr_err("%s: swrm or dev is NULL\n", __func__);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302405 return -EINVAL;
2406 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302407 switch (action) {
2408 case MSM_AUD_DC_EVENT:
2409 schedule_work(&(swrm->dc_presence_work));
2410 break;
2411 case SWR_WAKE_IRQ_EVENT:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302412 if (swrm->ipc_wakeup && !swrm->ipc_wakeup_triggered) {
2413 swrm->ipc_wakeup_triggered = true;
Ramprasad Katkam57349872018-11-11 18:34:57 +05302414 pm_stay_awake(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302415 schedule_work(&swrm->wakeup_work);
Ramprasad Katkamcd61c6e2018-09-18 13:22:58 +05302416 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302417 break;
2418 default:
2419 dev_err(swrm->dev, "%s: invalid event type: %lu\n",
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302420 __func__, action);
2421 return -EINVAL;
2422 }
2423
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302424 return 0;
2425}
2426
2427static void swrm_notify_work_fn(struct work_struct *work)
2428{
2429 struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302430 dc_presence_work);
2431
2432 if (!swrm || !swrm->pdev) {
2433 pr_err("%s: swrm or pdev is NULL\n", __func__);
2434 return;
2435 }
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302436 swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL);
2437}
2438
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302439static int swrm_probe(struct platform_device *pdev)
2440{
2441 struct swr_mstr_ctrl *swrm;
2442 struct swr_ctrl_platform_data *pdata;
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302443 u32 i, num_ports, port_num, port_type, ch_mask, swrm_hctl_reg = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302444 u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0;
2445 int ret = 0;
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302446 struct clk *lpass_core_hw_vote = NULL;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302447 struct clk *lpass_core_audio = NULL;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302448
2449 /* Allocate soundwire master driver structure */
2450 swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl),
2451 GFP_KERNEL);
2452 if (!swrm) {
2453 ret = -ENOMEM;
2454 goto err_memory_fail;
2455 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302456 swrm->pdev = pdev;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302457 swrm->dev = &pdev->dev;
2458 platform_set_drvdata(pdev, swrm);
2459 swr_set_ctrl_data(&swrm->master, swrm);
2460 pdata = dev_get_platdata(&pdev->dev);
2461 if (!pdata) {
2462 dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
2463 __func__);
2464 ret = -EINVAL;
2465 goto err_pdata_fail;
2466 }
2467 swrm->handle = (void *)pdata->handle;
2468 if (!swrm->handle) {
2469 dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
2470 __func__);
2471 ret = -EINVAL;
2472 goto err_pdata_fail;
2473 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302474 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr_master_id",
2475 &swrm->master_id);
2476 if (ret) {
2477 dev_err(&pdev->dev, "%s: failed to get master id\n", __func__);
2478 goto err_pdata_fail;
2479 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302480 if (!(of_property_read_u32(pdev->dev.of_node,
2481 "swrm-io-base", &swrm->swrm_base_reg)))
2482 ret = of_property_read_u32(pdev->dev.of_node,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302483 "swrm-io-base", &swrm->swrm_base_reg);
2484 if (!swrm->swrm_base_reg) {
2485 swrm->read = pdata->read;
2486 if (!swrm->read) {
2487 dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
2488 __func__);
2489 ret = -EINVAL;
2490 goto err_pdata_fail;
2491 }
2492 swrm->write = pdata->write;
2493 if (!swrm->write) {
2494 dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
2495 __func__);
2496 ret = -EINVAL;
2497 goto err_pdata_fail;
2498 }
2499 swrm->bulk_write = pdata->bulk_write;
2500 if (!swrm->bulk_write) {
2501 dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
2502 __func__);
2503 ret = -EINVAL;
2504 goto err_pdata_fail;
2505 }
2506 } else {
2507 swrm->swrm_dig_base = devm_ioremap(&pdev->dev,
2508 swrm->swrm_base_reg, SWRM_MAX_REGISTER);
2509 }
2510
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07002511 swrm->core_vote = pdata->core_vote;
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302512 if (!(of_property_read_u32(pdev->dev.of_node,
2513 "qcom,swrm-hctl-reg", &swrm_hctl_reg)))
2514 swrm->swrm_hctl_reg = devm_ioremap(&pdev->dev,
2515 swrm_hctl_reg, 0x4);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302516 swrm->clk = pdata->clk;
2517 if (!swrm->clk) {
2518 dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
2519 __func__);
2520 ret = -EINVAL;
2521 goto err_pdata_fail;
2522 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302523 if (of_property_read_u32(pdev->dev.of_node,
2524 "qcom,swr-clock-stop-mode0",
2525 &swrm->clk_stop_mode0_supp)) {
2526 swrm->clk_stop_mode0_supp = FALSE;
2527 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302528
2529 ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
2530 &swrm->num_dev);
2531 if (ret) {
2532 dev_dbg(&pdev->dev, "%s: Looking up %s property failed\n",
2533 __func__, "qcom,swr-num-dev");
2534 } else {
Sudheer Papothiae5c3632019-11-27 06:52:06 +05302535 if (swrm->num_dev > SWRM_NUM_AUTO_ENUM_SLAVES) {
Ramprasad Katkam57349872018-11-11 18:34:57 +05302536 dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
Sudheer Papothiae5c3632019-11-27 06:52:06 +05302537 __func__, swrm->num_dev,
2538 SWRM_NUM_AUTO_ENUM_SLAVES);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302539 ret = -EINVAL;
2540 goto err_pdata_fail;
2541 }
2542 }
2543
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302544 /* Parse soundwire port mapping */
2545 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
2546 &num_ports);
2547 if (ret) {
2548 dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
2549 goto err_pdata_fail;
2550 }
2551 swrm->num_ports = num_ports;
2552
2553 if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
2554 &map_size)) {
2555 dev_err(swrm->dev, "missing port mapping\n");
2556 goto err_pdata_fail;
2557 }
2558
2559 map_length = map_size / (3 * sizeof(u32));
2560 if (num_ports > SWR_MSTR_PORT_LEN) {
2561 dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
2562 __func__);
2563 ret = -EINVAL;
2564 goto err_pdata_fail;
2565 }
2566 temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
2567
2568 if (!temp) {
2569 ret = -ENOMEM;
2570 goto err_pdata_fail;
2571 }
2572 ret = of_property_read_u32_array(pdev->dev.of_node,
2573 "qcom,swr-port-mapping", temp, 3 * map_length);
2574 if (ret) {
2575 dev_err(swrm->dev, "%s: Failed to read port mapping\n",
2576 __func__);
2577 goto err_pdata_fail;
2578 }
2579
2580 for (i = 0; i < map_length; i++) {
2581 port_num = temp[3 * i];
2582 port_type = temp[3 * i + 1];
2583 ch_mask = temp[3 * i + 2];
2584
2585 if (port_num != old_port_num)
2586 ch_iter = 0;
2587 swrm->port_mapping[port_num][ch_iter].port_type = port_type;
2588 swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
2589 old_port_num = port_num;
2590 }
2591 devm_kfree(&pdev->dev, temp);
2592
2593 swrm->reg_irq = pdata->reg_irq;
2594 swrm->master.read = swrm_read;
2595 swrm->master.write = swrm_write;
2596 swrm->master.bulk_write = swrm_bulk_write;
2597 swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
2598 swrm->master.connect_port = swrm_connect_port;
2599 swrm->master.disconnect_port = swrm_disconnect_port;
2600 swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
2601 swrm->master.remove_from_group = swrm_remove_from_group;
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302602 swrm->master.device_wakeup_vote = swrm_device_wakeup_vote;
2603 swrm->master.device_wakeup_unvote = swrm_device_wakeup_unvote;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302604 swrm->master.dev.parent = &pdev->dev;
2605 swrm->master.dev.of_node = pdev->dev.of_node;
2606 swrm->master.num_port = 0;
2607 swrm->rcmd_id = 0;
2608 swrm->wcmd_id = 0;
2609 swrm->slave_status = 0;
2610 swrm->num_rx_chs = 0;
2611 swrm->clk_ref_count = 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302612 swrm->swr_irq_wakeup_capable = 0;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302613 swrm->mclk_freq = MCLK_FREQ;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302614 swrm->bus_clk = MCLK_FREQ;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302615 swrm->dev_up = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302616 swrm->state = SWR_MSTR_UP;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302617 swrm->ipc_wakeup = false;
2618 swrm->ipc_wakeup_triggered = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302619 init_completion(&swrm->reset);
2620 init_completion(&swrm->broadcast);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302621 init_completion(&swrm->clk_off_complete);
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302622 mutex_init(&swrm->irq_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302623 mutex_init(&swrm->mlock);
2624 mutex_init(&swrm->reslock);
2625 mutex_init(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302626 mutex_init(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302627 mutex_init(&swrm->clklock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302628 mutex_init(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302629 mutex_init(&swrm->pm_lock);
2630 swrm->wlock_holders = 0;
2631 swrm->pm_state = SWRM_PM_SLEEPABLE;
2632 init_waitqueue_head(&swrm->pm_wq);
2633 pm_qos_add_request(&swrm->pm_qos_req,
2634 PM_QOS_CPU_DMA_LATENCY,
2635 PM_QOS_DEFAULT_VALUE);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302636
2637 for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++)
2638 INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
2639
Sudheer Papothi06f43412019-07-09 03:32:54 +05302640 /* Register LPASS core hw vote */
2641 lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
2642 if (IS_ERR(lpass_core_hw_vote)) {
2643 ret = PTR_ERR(lpass_core_hw_vote);
2644 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2645 __func__, "lpass_core_hw_vote", ret);
2646 lpass_core_hw_vote = NULL;
2647 ret = 0;
2648 }
2649 swrm->lpass_core_hw_vote = lpass_core_hw_vote;
2650
2651 /* Register LPASS audio core vote */
2652 lpass_core_audio = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
2653 if (IS_ERR(lpass_core_audio)) {
2654 ret = PTR_ERR(lpass_core_audio);
2655 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2656 __func__, "lpass_core_audio", ret);
2657 lpass_core_audio = NULL;
2658 ret = 0;
2659 }
2660 swrm->lpass_core_audio = lpass_core_audio;
2661
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302662 if (swrm->reg_irq) {
2663 ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
2664 SWR_IRQ_REGISTER);
2665 if (ret) {
2666 dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
2667 __func__, ret);
2668 goto err_irq_fail;
2669 }
2670 } else {
2671 swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
2672 if (swrm->irq < 0) {
2673 dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
2674 __func__, swrm->irq);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302675 goto err_irq_fail;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302676 }
2677
2678 ret = request_threaded_irq(swrm->irq, NULL,
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302679 swr_mstr_interrupt_v2,
Ramprasad Katkam83303512018-10-11 17:34:22 +05302680 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302681 "swr_master_irq", swrm);
2682 if (ret) {
2683 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2684 __func__, ret);
2685 goto err_irq_fail;
2686 }
2687
2688 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302689 /* Make inband tx interrupts as wakeup capable for slave irq */
2690 ret = of_property_read_u32(pdev->dev.of_node,
2691 "qcom,swr-mstr-irq-wakeup-capable",
2692 &swrm->swr_irq_wakeup_capable);
2693 if (ret)
2694 dev_dbg(swrm->dev, "%s: swrm irq wakeup capable not defined\n",
2695 __func__);
2696 if (swrm->swr_irq_wakeup_capable)
2697 irq_set_irq_wake(swrm->irq, 1);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302698 ret = swr_register_master(&swrm->master);
2699 if (ret) {
2700 dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
2701 goto err_mstr_fail;
2702 }
2703
2704 /* Add devices registered with board-info as the
2705 * controller will be up now
2706 */
2707 swr_master_add_boarddevices(&swrm->master);
2708 mutex_lock(&swrm->mlock);
2709 swrm_clk_request(swrm, true);
Laxminath Kasam4696fff2019-11-26 16:07:11 +05302710 swrm->version = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302711 ret = swrm_master_init(swrm);
2712 if (ret < 0) {
2713 dev_err(&pdev->dev,
2714 "%s: Error in master Initialization , err %d\n",
2715 __func__, ret);
2716 mutex_unlock(&swrm->mlock);
Laxminath Kasamec8c9092019-12-17 13:12:58 +05302717 goto err_mstr_init_fail;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302718 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302719
2720 mutex_unlock(&swrm->mlock);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302721 INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302722
2723 if (pdev->dev.of_node)
2724 of_register_swr_devices(&swrm->master);
2725
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302726#ifdef CONFIG_DEBUG_FS
2727 swrm->debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
2728 if (!IS_ERR(swrm->debugfs_swrm_dent)) {
2729 swrm->debugfs_peek = debugfs_create_file("swrm_peek",
2730 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2731 (void *) swrm, &swrm_debug_read_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302732
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302733 swrm->debugfs_poke = debugfs_create_file("swrm_poke",
2734 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2735 (void *) swrm, &swrm_debug_write_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302736
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302737 swrm->debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
2738 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2739 (void *) swrm,
2740 &swrm_debug_dump_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302741 }
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302742#endif
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302743 ret = device_init_wakeup(swrm->dev, true);
2744 if (ret) {
2745 dev_err(swrm->dev, "Device wakeup init failed: %d\n", ret);
2746 goto err_irq_wakeup_fail;
2747 }
2748
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302749 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
2750 pm_runtime_use_autosuspend(&pdev->dev);
2751 pm_runtime_set_active(&pdev->dev);
2752 pm_runtime_enable(&pdev->dev);
2753 pm_runtime_mark_last_busy(&pdev->dev);
2754
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302755 INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn);
2756 swrm->event_notifier.notifier_call = swrm_event_notify;
2757 msm_aud_evt_register_client(&swrm->event_notifier);
2758
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302759 return 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302760err_irq_wakeup_fail:
2761 device_init_wakeup(swrm->dev, false);
Laxminath Kasamec8c9092019-12-17 13:12:58 +05302762err_mstr_init_fail:
2763 swr_unregister_master(&swrm->master);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302764err_mstr_fail:
2765 if (swrm->reg_irq)
2766 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2767 swrm, SWR_IRQ_FREE);
2768 else if (swrm->irq)
2769 free_irq(swrm->irq, swrm);
2770err_irq_fail:
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302771 mutex_destroy(&swrm->irq_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302772 mutex_destroy(&swrm->mlock);
2773 mutex_destroy(&swrm->reslock);
2774 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302775 mutex_destroy(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302776 mutex_destroy(&swrm->clklock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302777 mutex_destroy(&swrm->pm_lock);
2778 pm_qos_remove_request(&swrm->pm_qos_req);
2779
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302780err_pdata_fail:
2781err_memory_fail:
2782 return ret;
2783}
2784
2785static int swrm_remove(struct platform_device *pdev)
2786{
2787 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2788
2789 if (swrm->reg_irq)
2790 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2791 swrm, SWR_IRQ_FREE);
2792 else if (swrm->irq)
2793 free_irq(swrm->irq, swrm);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302794 else if (swrm->wake_irq > 0)
2795 free_irq(swrm->wake_irq, swrm);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302796 if (swrm->swr_irq_wakeup_capable)
2797 irq_set_irq_wake(swrm->irq, 0);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302798 cancel_work_sync(&swrm->wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302799 pm_runtime_disable(&pdev->dev);
2800 pm_runtime_set_suspended(&pdev->dev);
2801 swr_unregister_master(&swrm->master);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302802 msm_aud_evt_unregister_client(&swrm->event_notifier);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302803 device_init_wakeup(swrm->dev, false);
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302804 mutex_destroy(&swrm->irq_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302805 mutex_destroy(&swrm->mlock);
2806 mutex_destroy(&swrm->reslock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302807 mutex_destroy(&swrm->iolock);
2808 mutex_destroy(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302809 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302810 mutex_destroy(&swrm->pm_lock);
2811 pm_qos_remove_request(&swrm->pm_qos_req);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302812 devm_kfree(&pdev->dev, swrm);
2813 return 0;
2814}
2815
2816static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
2817{
2818 u32 val;
2819
2820 dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
2821 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD);
2822 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2823 val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK;
2824 swr_master_write(swrm, SWRM_MCP_CFG_ADDR, val);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302825
2826 return 0;
2827}
2828
2829#ifdef CONFIG_PM
2830static int swrm_runtime_resume(struct device *dev)
2831{
2832 struct platform_device *pdev = to_platform_device(dev);
2833 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2834 int ret = 0;
Vatsal Buchae50b5002019-09-19 14:32:20 +05302835 bool swrm_clk_req_err = false;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302836 bool hw_core_err = false;
2837 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302838 struct swr_master *mstr = &swrm->master;
2839 struct swr_device *swr_dev;
2840
2841 dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
2842 __func__, swrm->state);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07002843 trace_printk("%s: pm_runtime: resume, state:%d\n",
2844 __func__, swrm->state);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302845 mutex_lock(&swrm->reslock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302846
Sudheer Papothi384addd2019-06-14 02:26:52 +05302847 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2848 dev_err(dev, "%s:lpass core hw enable failed\n",
2849 __func__);
2850 hw_core_err = true;
2851 }
2852 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2853 dev_err(dev, "%s:lpass audio hw enable failed\n",
2854 __func__);
2855 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002856 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302857
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302858 if ((swrm->state == SWR_MSTR_DOWN) ||
2859 (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) {
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302860 if (swrm->clk_stop_mode0_supp) {
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302861 if (swrm->wake_irq > 0) {
2862 if (unlikely(!irq_get_irq_data
2863 (swrm->wake_irq))) {
2864 pr_err("%s: irq data is NULL\n",
2865 __func__);
2866 mutex_unlock(&swrm->reslock);
2867 return IRQ_NONE;
2868 }
2869 mutex_lock(&swrm->irq_lock);
2870 if (!irqd_irq_disabled(
2871 irq_get_irq_data(swrm->wake_irq)))
2872 disable_irq_nosync(swrm->wake_irq);
2873 mutex_unlock(&swrm->irq_lock);
2874 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302875 if (swrm->ipc_wakeup)
2876 msm_aud_evt_blocking_notifier_call_chain(
2877 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302878 }
2879
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302880 if (swrm_clk_request(swrm, true)) {
2881 /*
2882 * Set autosuspend timer to 1 for
2883 * master to enter into suspend.
2884 */
Vatsal Buchae50b5002019-09-19 14:32:20 +05302885 swrm_clk_req_err = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302886 goto exit;
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302887 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302888 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302889 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2890 ret = swr_device_up(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302891 if (ret == -ENODEV) {
2892 dev_dbg(dev,
2893 "%s slave device up not implemented\n",
2894 __func__);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07002895 trace_printk(
2896 "%s slave device up not implemented\n",
2897 __func__);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302898 ret = 0;
2899 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302900 dev_err(dev,
2901 "%s: failed to wakeup swr dev %d\n",
2902 __func__, swr_dev->dev_num);
2903 swrm_clk_request(swrm, false);
2904 goto exit;
2905 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302906 }
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05302907 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2908 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2909 swrm_master_init(swrm);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302910 /* wait for hw enumeration to complete */
2911 usleep_range(100, 105);
Laxminath Kasame2291972019-11-08 14:51:59 +05302912 if (!swrm_check_link_status(swrm, 0x1))
Laxminath Kasam696b14b2019-12-03 22:07:34 +05302913 dev_dbg(dev, "%s:failed in connecting, ssr?\n",
2914 __func__);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302915 swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, 0x0,
2916 SWRS_SCP_INT_STATUS_MASK_1);
Karthikeyan Manif6821902019-05-21 17:31:24 -07002917 if (swrm->state == SWR_MSTR_SSR) {
2918 mutex_unlock(&swrm->reslock);
2919 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
2920 mutex_lock(&swrm->reslock);
2921 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302922 } else {
2923 /*wake up from clock stop*/
2924 swr_master_write(swrm, SWRM_MCP_BUS_CTRL_ADDR, 0x2);
Sudheer Papothi55fa5972019-09-28 02:50:27 +05302925 /* clear and enable bus clash interrupt */
2926 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x08);
2927 swrm->intr_mask |= 0x08;
2928 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR,
2929 swrm->intr_mask);
2930 swr_master_write(swrm,
2931 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
2932 swrm->intr_mask);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302933 usleep_range(100, 105);
Laxminath Kasame2291972019-11-08 14:51:59 +05302934 if (!swrm_check_link_status(swrm, 0x1))
Laxminath Kasam696b14b2019-12-03 22:07:34 +05302935 dev_dbg(dev, "%s:failed in connecting, ssr?\n",
2936 __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302937 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302938 swrm->state = SWR_MSTR_UP;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302939 }
2940exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302941 if (!aud_core_err)
2942 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2943 if (!hw_core_err)
2944 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Vatsal Buchae50b5002019-09-19 14:32:20 +05302945 if (swrm_clk_req_err)
2946 pm_runtime_set_autosuspend_delay(&pdev->dev,
2947 ERR_AUTO_SUSPEND_TIMER_VAL);
2948 else
2949 pm_runtime_set_autosuspend_delay(&pdev->dev,
2950 auto_suspend_timer);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302951 mutex_unlock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302952
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07002953 trace_printk("%s: pm_runtime: resume done, state:%d\n",
2954 __func__, swrm->state);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302955 return ret;
2956}
2957
2958static int swrm_runtime_suspend(struct device *dev)
2959{
2960 struct platform_device *pdev = to_platform_device(dev);
2961 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2962 int ret = 0;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302963 bool hw_core_err = false;
2964 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302965 struct swr_master *mstr = &swrm->master;
2966 struct swr_device *swr_dev;
2967 int current_state = 0;
2968
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07002969 trace_printk("%s: pm_runtime: suspend state: %d\n",
2970 __func__, swrm->state);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302971 dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
2972 __func__, swrm->state);
2973 mutex_lock(&swrm->reslock);
2974 mutex_lock(&swrm->force_down_lock);
2975 current_state = swrm->state;
2976 mutex_unlock(&swrm->force_down_lock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302977
2978 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2979 dev_err(dev, "%s:lpass core hw enable failed\n",
2980 __func__);
2981 hw_core_err = true;
2982 }
2983 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2984 dev_err(dev, "%s:lpass audio hw enable failed\n",
2985 __func__);
2986 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002987 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302988
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302989 if ((current_state == SWR_MSTR_UP) ||
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302990 (current_state == SWR_MSTR_SSR)) {
2991
2992 if ((current_state != SWR_MSTR_SSR) &&
2993 swrm_is_port_en(&swrm->master)) {
2994 dev_dbg(dev, "%s ports are enabled\n", __func__);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07002995 trace_printk("%s ports are enabled\n", __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302996 ret = -EBUSY;
2997 goto exit;
2998 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302999 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003000 dev_err(dev, "%s: clk stop mode not supported or SSR entry\n",
3001 __func__);
Sudheer Papothi06f43412019-07-09 03:32:54 +05303002 mutex_unlock(&swrm->reslock);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +05303003 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
Sudheer Papothi06f43412019-07-09 03:32:54 +05303004 mutex_lock(&swrm->reslock);
Sudheer Papothi1f2565b2020-01-30 05:22:47 +05303005 if (!swrm->clk_stop_mode0_supp) {
3006 swrm_clk_pause(swrm);
3007 } else {
3008 /* Mask bus clash interrupt */
3009 swrm->intr_mask &= ~((u32)0x08);
3010 swr_master_write(swrm,
3011 SWRM_INTERRUPT_MASK_ADDR,
3012 swrm->intr_mask);
3013 swr_master_write(swrm,
3014 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
3015 swrm->intr_mask);
3016 mutex_unlock(&swrm->reslock);
3017 /* clock stop sequence */
3018 swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
3019 SWRS_SCP_CONTROL);
3020 mutex_lock(&swrm->reslock);
3021 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05303022 swr_master_write(swrm, SWRM_COMP_CFG_ADDR, 0x00);
3023 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
3024 ret = swr_device_down(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05303025 if (ret == -ENODEV) {
3026 dev_dbg_ratelimited(dev,
3027 "%s slave device down not implemented\n",
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003028 __func__);
3029 trace_printk(
3030 "%s slave device down not implemented\n",
3031 __func__);
Sudheer Papothi79c90752019-04-23 06:09:52 +05303032 ret = 0;
3033 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05303034 dev_err(dev,
3035 "%s: failed to shutdown swr dev %d\n",
3036 __func__, swr_dev->dev_num);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003037 trace_printk(
3038 "%s: failed to shutdown swr dev %d\n",
3039 __func__, swr_dev->dev_num);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05303040 goto exit;
3041 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303042 }
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003043 trace_printk("%s: clk stop mode not supported or SSR exit\n",
3044 __func__);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05303045 } else {
Sudheer Papothi55fa5972019-09-28 02:50:27 +05303046 /* Mask bus clash interrupt */
3047 swrm->intr_mask &= ~((u32)0x08);
3048 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR,
3049 swrm->intr_mask);
3050 swr_master_write(swrm,
3051 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
3052 swrm->intr_mask);
Sudheer Papothi384addd2019-06-14 02:26:52 +05303053 mutex_unlock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05303054 /* clock stop sequence */
3055 swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
3056 SWRS_SCP_CONTROL);
Sudheer Papothi384addd2019-06-14 02:26:52 +05303057 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05303058 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303059 }
Laxminath Kasamcafe0732019-11-20 17:31:58 +05303060 if (!swrm_check_link_status(swrm, 0x0))
Laxminath Kasam696b14b2019-12-03 22:07:34 +05303061 dev_dbg(dev, "%s:failed in disconnecting, ssr?\n",
3062 __func__);
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07003063 ret = swrm_clk_request(swrm, false);
3064 if (ret) {
3065 dev_err(dev, "%s: swrmn clk failed\n", __func__);
3066 ret = 0;
3067 goto exit;
3068 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05303069
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303070 if (swrm->clk_stop_mode0_supp) {
3071 if (swrm->wake_irq > 0) {
3072 enable_irq(swrm->wake_irq);
3073 } else if (swrm->ipc_wakeup) {
3074 msm_aud_evt_blocking_notifier_call_chain(
3075 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
3076 swrm->ipc_wakeup_triggered = false;
3077 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05303078 }
3079
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303080 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05303081 /* Retain SSR state until resume */
3082 if (current_state != SWR_MSTR_SSR)
3083 swrm->state = SWR_MSTR_DOWN;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303084exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05303085 if (!aud_core_err)
3086 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
3087 if (!hw_core_err)
3088 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303089 mutex_unlock(&swrm->reslock);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003090 trace_printk("%s: pm_runtime: suspend done state: %d\n",
3091 __func__, swrm->state);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303092 return ret;
3093}
3094#endif /* CONFIG_PM */
3095
Sudheer Papothi06f43412019-07-09 03:32:54 +05303096static int swrm_device_suspend(struct device *dev)
3097{
3098 struct platform_device *pdev = to_platform_device(dev);
3099 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3100 int ret = 0;
3101
3102 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003103 trace_printk("%s: swrm state: %d\n", __func__, swrm->state);
Sudheer Papothi06f43412019-07-09 03:32:54 +05303104 if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
3105 ret = swrm_runtime_suspend(dev);
3106 if (!ret) {
3107 pm_runtime_disable(dev);
3108 pm_runtime_set_suspended(dev);
3109 pm_runtime_enable(dev);
3110 }
3111 }
3112
3113 return 0;
3114}
3115
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303116static int swrm_device_down(struct device *dev)
3117{
3118 struct platform_device *pdev = to_platform_device(dev);
3119 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303120
3121 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003122 trace_printk("%s: swrm state: %d\n", __func__, swrm->state);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303123
3124 mutex_lock(&swrm->force_down_lock);
3125 swrm->state = SWR_MSTR_SSR;
3126 mutex_unlock(&swrm->force_down_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303127
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05303128 swrm_device_suspend(dev);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303129 return 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303130}
3131
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303132int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm)
3133{
3134 int ret = 0;
Laxminath Kasama60239e2019-01-10 14:43:03 +05303135 int irq, dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303136
3137 if (!swrm->ipc_wakeup) {
Laxminath Kasama60239e2019-01-10 14:43:03 +05303138 irq = of_get_named_gpio(swrm->dev->of_node,
3139 "qcom,swr-wakeup-irq", 0);
3140 if (gpio_is_valid(irq)) {
3141 swrm->wake_irq = gpio_to_irq(irq);
3142 if (swrm->wake_irq < 0) {
3143 dev_err(swrm->dev,
3144 "Unable to configure irq\n");
3145 return swrm->wake_irq;
3146 }
3147 } else {
3148 dir_apps_irq = platform_get_irq_byname(swrm->pdev,
3149 "swr_wake_irq");
3150 if (dir_apps_irq < 0) {
3151 dev_err(swrm->dev,
3152 "TLMM connect gpio not found\n");
3153 return -EINVAL;
3154 }
3155 swrm->wake_irq = dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303156 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303157 ret = request_threaded_irq(swrm->wake_irq, NULL,
3158 swrm_wakeup_interrupt,
3159 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
3160 "swr_wake_irq", swrm);
3161 if (ret) {
3162 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
3163 __func__, ret);
3164 return -EINVAL;
3165 }
Aditya Bavanari3517b112018-12-03 13:26:59 +05303166 irq_set_irq_wake(swrm->wake_irq, 1);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303167 }
3168 return ret;
3169}
3170
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05303171static int swrm_alloc_port_mem(struct device *dev, struct swr_mstr_ctrl *swrm,
3172 u32 uc, u32 size)
3173{
3174 if (!swrm->port_param) {
3175 swrm->port_param = devm_kzalloc(dev,
3176 sizeof(swrm->port_param) * SWR_UC_MAX,
3177 GFP_KERNEL);
3178 if (!swrm->port_param)
3179 return -ENOMEM;
3180 }
3181 if (!swrm->port_param[uc]) {
3182 swrm->port_param[uc] = devm_kcalloc(dev, size,
3183 sizeof(struct port_params),
3184 GFP_KERNEL);
3185 if (!swrm->port_param[uc])
3186 return -ENOMEM;
3187 } else {
3188 dev_err_ratelimited(swrm->dev, "%s: called more than once\n",
3189 __func__);
3190 }
3191
3192 return 0;
3193}
3194
3195static int swrm_copy_port_config(struct swr_mstr_ctrl *swrm,
3196 struct swrm_port_config *port_cfg,
3197 u32 size)
3198{
3199 int idx;
3200 struct port_params *params;
3201 int uc = port_cfg->uc;
3202 int ret = 0;
3203
3204 for (idx = 0; idx < size; idx++) {
3205 params = &((struct port_params *)port_cfg->params)[idx];
3206 if (!params) {
3207 dev_err(swrm->dev, "%s: Invalid params\n", __func__);
3208 ret = -EINVAL;
3209 break;
3210 }
3211 memcpy(&swrm->port_param[uc][idx], params,
3212 sizeof(struct port_params));
3213 }
3214
3215 return ret;
3216}
3217
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303218/**
3219 * swrm_wcd_notify - parent device can notify to soundwire master through
3220 * this function
3221 * @pdev: pointer to platform device structure
3222 * @id: command id from parent to the soundwire master
3223 * @data: data from parent device to soundwire master
3224 */
3225int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
3226{
3227 struct swr_mstr_ctrl *swrm;
3228 int ret = 0;
3229 struct swr_master *mstr;
3230 struct swr_device *swr_dev;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05303231 struct swrm_port_config *port_cfg;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303232
3233 if (!pdev) {
3234 pr_err("%s: pdev is NULL\n", __func__);
3235 return -EINVAL;
3236 }
3237 swrm = platform_get_drvdata(pdev);
3238 if (!swrm) {
3239 dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__);
3240 return -EINVAL;
3241 }
3242 mstr = &swrm->master;
3243
3244 switch (id) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05303245 case SWR_REQ_CLK_SWITCH:
3246 /* This will put soundwire in clock stop mode and disable the
3247 * clocks, if there is no active usecase running, so that the
3248 * next activity on soundwire will request clock from new clock
3249 * source.
3250 */
Sudheer Papothi8c50f2f2019-12-05 01:14:47 +05303251 if (!data) {
3252 dev_err(swrm->dev, "%s: data is NULL for id:%d\n",
3253 __func__, id);
3254 ret = -EINVAL;
3255 break;
3256 }
Sudheer Papothi06f43412019-07-09 03:32:54 +05303257 mutex_lock(&swrm->mlock);
Sudheer Papothi8c50f2f2019-12-05 01:14:47 +05303258 if (swrm->clk_src != *(int *)data) {
3259 if (swrm->state == SWR_MSTR_UP)
3260 swrm_device_suspend(&pdev->dev);
3261 swrm->clk_src = *(int *)data;
3262 }
Sudheer Papothi06f43412019-07-09 03:32:54 +05303263 mutex_unlock(&swrm->mlock);
3264 break;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05303265 case SWR_CLK_FREQ:
3266 if (!data) {
3267 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
3268 ret = -EINVAL;
3269 } else {
3270 mutex_lock(&swrm->mlock);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05303271 if (swrm->mclk_freq != *(int *)data) {
3272 dev_dbg(swrm->dev, "%s: freq change: force mstr down\n", __func__);
3273 if (swrm->state == SWR_MSTR_DOWN)
3274 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
3275 __func__, swrm->state);
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +05303276 else {
3277 swrm->mclk_freq = *(int *)data;
3278 swrm->bus_clk = swrm->mclk_freq;
3279 swrm_switch_frame_shape(swrm,
3280 swrm->bus_clk);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05303281 swrm_device_suspend(&pdev->dev);
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +05303282 }
Prasad Kumpatla386df4e2019-10-11 18:23:16 +05303283 /*
3284 * add delay to ensure clk release happen
3285 * if interrupt triggered for clk stop,
3286 * wait for it to exit
3287 */
3288 usleep_range(10000, 10500);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05303289 }
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05303290 swrm->mclk_freq = *(int *)data;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05303291 swrm->bus_clk = swrm->mclk_freq;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05303292 mutex_unlock(&swrm->mlock);
3293 }
3294 break;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303295 case SWR_DEVICE_SSR_DOWN:
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003296 trace_printk("%s: swr device down called\n", __func__);
Prasad Kumpatla71fef462020-01-31 21:38:58 +05303297 mutex_lock(&swrm->mlock);
3298 if (swrm->state == SWR_MSTR_DOWN)
3299 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
3300 __func__, swrm->state);
3301 else
3302 swrm_device_down(&pdev->dev);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303303 mutex_lock(&swrm->devlock);
3304 swrm->dev_up = false;
3305 mutex_unlock(&swrm->devlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05303306 mutex_lock(&swrm->reslock);
3307 swrm->state = SWR_MSTR_SSR;
3308 mutex_unlock(&swrm->reslock);
Prasad Kumpatla71fef462020-01-31 21:38:58 +05303309 mutex_unlock(&swrm->mlock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303310 break;
3311 case SWR_DEVICE_SSR_UP:
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05303312 /* wait for clk voting to be zero */
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003313 trace_printk("%s: swr device up called\n", __func__);
Ramprasad Katkam7f6462e2018-11-06 11:51:22 +05303314 reinit_completion(&swrm->clk_off_complete);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05303315 if (swrm->clk_ref_count &&
3316 !wait_for_completion_timeout(&swrm->clk_off_complete,
Ramprasad Katkamc87efeb2018-12-12 19:26:19 +05303317 msecs_to_jiffies(500)))
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05303318 dev_err(swrm->dev, "%s: clock voting not zero\n",
3319 __func__);
3320
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303321 mutex_lock(&swrm->devlock);
3322 swrm->dev_up = true;
3323 mutex_unlock(&swrm->devlock);
3324 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303325 case SWR_DEVICE_DOWN:
3326 dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003327 trace_printk("%s: swr master down called\n", __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303328 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05303329 if (swrm->state == SWR_MSTR_DOWN)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303330 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
3331 __func__, swrm->state);
3332 else
3333 swrm_device_down(&pdev->dev);
3334 mutex_unlock(&swrm->mlock);
3335 break;
3336 case SWR_DEVICE_UP:
3337 dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003338 trace_printk("%s: swr master up called\n", __func__);
Ramprasad Katkam0fed92f2018-11-08 14:22:22 +05303339 mutex_lock(&swrm->devlock);
3340 if (!swrm->dev_up) {
3341 dev_dbg(swrm->dev, "SSR not complete yet\n");
3342 mutex_unlock(&swrm->devlock);
3343 return -EBUSY;
3344 }
3345 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303346 mutex_lock(&swrm->mlock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303347 pm_runtime_mark_last_busy(&pdev->dev);
3348 pm_runtime_get_sync(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303349 mutex_lock(&swrm->reslock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303350 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
3351 ret = swr_reset_device(swr_dev);
3352 if (ret) {
3353 dev_err(swrm->dev,
3354 "%s: failed to reset swr device %d\n",
3355 __func__, swr_dev->dev_num);
3356 swrm_clk_request(swrm, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303357 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303358 }
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303359 pm_runtime_mark_last_busy(&pdev->dev);
3360 pm_runtime_put_autosuspend(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303361 mutex_unlock(&swrm->reslock);
3362 mutex_unlock(&swrm->mlock);
3363 break;
3364 case SWR_SET_NUM_RX_CH:
3365 if (!data) {
3366 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
3367 ret = -EINVAL;
3368 } else {
3369 mutex_lock(&swrm->mlock);
3370 swrm->num_rx_chs = *(int *)data;
3371 if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
3372 list_for_each_entry(swr_dev, &mstr->devices,
3373 dev_list) {
3374 ret = swr_set_device_group(swr_dev,
3375 SWR_BROADCAST);
3376 if (ret)
3377 dev_err(swrm->dev,
3378 "%s: set num ch failed\n",
3379 __func__);
3380 }
3381 } else {
3382 list_for_each_entry(swr_dev, &mstr->devices,
3383 dev_list) {
3384 ret = swr_set_device_group(swr_dev,
3385 SWR_GROUP_NONE);
3386 if (ret)
3387 dev_err(swrm->dev,
3388 "%s: set num ch failed\n",
3389 __func__);
3390 }
3391 }
3392 mutex_unlock(&swrm->mlock);
3393 }
3394 break;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303395 case SWR_REGISTER_WAKE_IRQ:
3396 if (!data) {
3397 dev_err(swrm->dev, "%s: reg wake irq data is NULL\n",
3398 __func__);
3399 ret = -EINVAL;
3400 } else {
3401 mutex_lock(&swrm->mlock);
3402 swrm->ipc_wakeup = *(u32 *)data;
3403 ret = swrm_register_wake_irq(swrm);
3404 if (ret)
3405 dev_err(swrm->dev, "%s: register wake_irq failed\n",
3406 __func__);
3407 mutex_unlock(&swrm->mlock);
3408 }
3409 break;
Sudheer Papothi72ee2642019-08-08 05:15:17 +05303410 case SWR_REGISTER_WAKEUP:
3411 msm_aud_evt_blocking_notifier_call_chain(
3412 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
3413 break;
3414 case SWR_DEREGISTER_WAKEUP:
3415 msm_aud_evt_blocking_notifier_call_chain(
3416 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
3417 break;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05303418 case SWR_SET_PORT_MAP:
3419 if (!data) {
3420 dev_err(swrm->dev, "%s: data is NULL for id=%d\n",
3421 __func__, id);
3422 ret = -EINVAL;
3423 } else {
3424 mutex_lock(&swrm->mlock);
3425 port_cfg = (struct swrm_port_config *)data;
3426 if (!port_cfg->size) {
3427 ret = -EINVAL;
3428 goto done;
3429 }
3430 ret = swrm_alloc_port_mem(&pdev->dev, swrm,
3431 port_cfg->uc, port_cfg->size);
3432 if (!ret)
3433 swrm_copy_port_config(swrm, port_cfg,
3434 port_cfg->size);
3435done:
3436 mutex_unlock(&swrm->mlock);
3437 }
3438 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303439 default:
3440 dev_err(swrm->dev, "%s: swr master unknown id %d\n",
3441 __func__, id);
3442 break;
3443 }
3444 return ret;
3445}
3446EXPORT_SYMBOL(swrm_wcd_notify);
3447
Ramprasad Katkam57349872018-11-11 18:34:57 +05303448/*
3449 * swrm_pm_cmpxchg:
3450 * Check old state and exchange with pm new state
3451 * if old state matches with current state
3452 *
3453 * @swrm: pointer to wcd core resource
3454 * @o: pm old state
3455 * @n: pm new state
3456 *
3457 * Returns old state
3458 */
3459static enum swrm_pm_state swrm_pm_cmpxchg(
3460 struct swr_mstr_ctrl *swrm,
3461 enum swrm_pm_state o,
3462 enum swrm_pm_state n)
3463{
3464 enum swrm_pm_state old;
3465
3466 if (!swrm)
3467 return o;
3468
3469 mutex_lock(&swrm->pm_lock);
3470 old = swrm->pm_state;
3471 if (old == o)
3472 swrm->pm_state = n;
3473 mutex_unlock(&swrm->pm_lock);
3474
3475 return old;
3476}
3477
3478static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm)
3479{
3480 enum swrm_pm_state os;
3481
3482 /*
3483 * swrm_{lock/unlock}_sleep will be called by swr irq handler
3484 * and slave wake up requests..
3485 *
3486 * If system didn't resume, we can simply return false so
3487 * IRQ handler can return without handling IRQ.
3488 */
3489 mutex_lock(&swrm->pm_lock);
3490 if (swrm->wlock_holders++ == 0) {
3491 dev_dbg(swrm->dev, "%s: holding wake lock\n", __func__);
3492 pm_qos_update_request(&swrm->pm_qos_req,
3493 msm_cpuidle_get_deep_idle_latency());
3494 pm_stay_awake(swrm->dev);
3495 }
3496 mutex_unlock(&swrm->pm_lock);
3497
3498 if (!wait_event_timeout(swrm->pm_wq,
3499 ((os = swrm_pm_cmpxchg(swrm,
3500 SWRM_PM_SLEEPABLE,
3501 SWRM_PM_AWAKE)) ==
3502 SWRM_PM_SLEEPABLE ||
3503 (os == SWRM_PM_AWAKE)),
3504 msecs_to_jiffies(
3505 SWRM_SYSTEM_RESUME_TIMEOUT_MS))) {
3506 dev_err(swrm->dev, "%s: system didn't resume within %dms, s %d, w %d\n",
3507 __func__, SWRM_SYSTEM_RESUME_TIMEOUT_MS, swrm->pm_state,
3508 swrm->wlock_holders);
3509 swrm_unlock_sleep(swrm);
3510 return false;
3511 }
3512 wake_up_all(&swrm->pm_wq);
3513 return true;
3514}
3515
3516static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm)
3517{
3518 mutex_lock(&swrm->pm_lock);
3519 if (--swrm->wlock_holders == 0) {
3520 dev_dbg(swrm->dev, "%s: releasing wake lock pm_state %d -> %d\n",
3521 __func__, swrm->pm_state, SWRM_PM_SLEEPABLE);
3522 /*
3523 * if swrm_lock_sleep failed, pm_state would be still
3524 * swrm_PM_ASLEEP, don't overwrite
3525 */
3526 if (likely(swrm->pm_state == SWRM_PM_AWAKE))
3527 swrm->pm_state = SWRM_PM_SLEEPABLE;
3528 pm_qos_update_request(&swrm->pm_qos_req,
3529 PM_QOS_DEFAULT_VALUE);
3530 pm_relax(swrm->dev);
3531 }
3532 mutex_unlock(&swrm->pm_lock);
3533 wake_up_all(&swrm->pm_wq);
3534}
3535
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303536#ifdef CONFIG_PM_SLEEP
3537static int swrm_suspend(struct device *dev)
3538{
3539 int ret = -EBUSY;
3540 struct platform_device *pdev = to_platform_device(dev);
3541 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3542
3543 dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
Ramprasad Katkam57349872018-11-11 18:34:57 +05303544
3545 mutex_lock(&swrm->pm_lock);
3546
3547 if (swrm->pm_state == SWRM_PM_SLEEPABLE) {
3548 dev_dbg(swrm->dev, "%s: suspending system, state %d, wlock %d\n",
3549 __func__, swrm->pm_state,
3550 swrm->wlock_holders);
3551 swrm->pm_state = SWRM_PM_ASLEEP;
3552 } else if (swrm->pm_state == SWRM_PM_AWAKE) {
3553 /*
3554 * unlock to wait for pm_state == SWRM_PM_SLEEPABLE
3555 * then set to SWRM_PM_ASLEEP
3556 */
3557 dev_dbg(swrm->dev, "%s: waiting to suspend system, state %d, wlock %d\n",
3558 __func__, swrm->pm_state,
3559 swrm->wlock_holders);
3560 mutex_unlock(&swrm->pm_lock);
3561 if (!(wait_event_timeout(swrm->pm_wq, swrm_pm_cmpxchg(
3562 swrm, SWRM_PM_SLEEPABLE,
3563 SWRM_PM_ASLEEP) ==
3564 SWRM_PM_SLEEPABLE,
3565 msecs_to_jiffies(
3566 SWRM_SYS_SUSPEND_WAIT)))) {
3567 dev_dbg(swrm->dev, "%s: suspend failed state %d, wlock %d\n",
3568 __func__, swrm->pm_state,
3569 swrm->wlock_holders);
3570 return -EBUSY;
3571 } else {
3572 dev_dbg(swrm->dev,
3573 "%s: done, state %d, wlock %d\n",
3574 __func__, swrm->pm_state,
3575 swrm->wlock_holders);
3576 }
3577 mutex_lock(&swrm->pm_lock);
3578 } else if (swrm->pm_state == SWRM_PM_ASLEEP) {
3579 dev_dbg(swrm->dev, "%s: system is already suspended, state %d, wlock %d\n",
3580 __func__, swrm->pm_state,
3581 swrm->wlock_holders);
3582 }
3583
3584 mutex_unlock(&swrm->pm_lock);
3585
3586 if ((!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev))) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303587 ret = swrm_runtime_suspend(dev);
3588 if (!ret) {
3589 /*
3590 * Synchronize runtime-pm and system-pm states:
3591 * At this point, we are already suspended. If
3592 * runtime-pm still thinks its active, then
3593 * make sure its status is in sync with HW
3594 * status. The three below calls let the
3595 * runtime-pm know that we are suspended
3596 * already without re-invoking the suspend
3597 * callback
3598 */
3599 pm_runtime_disable(dev);
3600 pm_runtime_set_suspended(dev);
3601 pm_runtime_enable(dev);
3602 }
3603 }
3604 if (ret == -EBUSY) {
3605 /*
3606 * There is a possibility that some audio stream is active
3607 * during suspend. We dont want to return suspend failure in
3608 * that case so that display and relevant components can still
3609 * go to suspend.
3610 * If there is some other error, then it should be passed-on
3611 * to system level suspend
3612 */
3613 ret = 0;
3614 }
3615 return ret;
3616}
3617
3618static int swrm_resume(struct device *dev)
3619{
3620 int ret = 0;
3621 struct platform_device *pdev = to_platform_device(dev);
3622 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3623
3624 dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
3625 if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
3626 ret = swrm_runtime_resume(dev);
3627 if (!ret) {
3628 pm_runtime_mark_last_busy(dev);
3629 pm_request_autosuspend(dev);
3630 }
3631 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05303632 mutex_lock(&swrm->pm_lock);
3633 if (swrm->pm_state == SWRM_PM_ASLEEP) {
3634 dev_dbg(swrm->dev,
3635 "%s: resuming system, state %d, wlock %d\n",
3636 __func__, swrm->pm_state,
3637 swrm->wlock_holders);
3638 swrm->pm_state = SWRM_PM_SLEEPABLE;
3639 } else {
3640 dev_dbg(swrm->dev, "%s: system is already awake, state %d wlock %d\n",
3641 __func__, swrm->pm_state,
3642 swrm->wlock_holders);
3643 }
3644 mutex_unlock(&swrm->pm_lock);
3645 wake_up_all(&swrm->pm_wq);
3646
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303647 return ret;
3648}
3649#endif /* CONFIG_PM_SLEEP */
3650
3651static const struct dev_pm_ops swrm_dev_pm_ops = {
3652 SET_SYSTEM_SLEEP_PM_OPS(
3653 swrm_suspend,
3654 swrm_resume
3655 )
3656 SET_RUNTIME_PM_OPS(
3657 swrm_runtime_suspend,
3658 swrm_runtime_resume,
3659 NULL
3660 )
3661};
3662
3663static const struct of_device_id swrm_dt_match[] = {
3664 {
3665 .compatible = "qcom,swr-mstr",
3666 },
3667 {}
3668};
3669
3670static struct platform_driver swr_mstr_driver = {
3671 .probe = swrm_probe,
3672 .remove = swrm_remove,
3673 .driver = {
3674 .name = SWR_WCD_NAME,
3675 .owner = THIS_MODULE,
3676 .pm = &swrm_dev_pm_ops,
3677 .of_match_table = swrm_dt_match,
Xiaojun Sang53cd13a2018-06-29 15:14:37 +08003678 .suppress_bind_attrs = true,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303679 },
3680};
3681
3682static int __init swrm_init(void)
3683{
3684 return platform_driver_register(&swr_mstr_driver);
3685}
3686module_init(swrm_init);
3687
3688static void __exit swrm_exit(void)
3689{
3690 platform_driver_unregister(&swr_mstr_driver);
3691}
3692module_exit(swrm_exit);
3693
3694MODULE_LICENSE("GPL v2");
3695MODULE_DESCRIPTION("SoundWire Master Controller");
3696MODULE_ALIAS("platform:swr-mstr");