blob: 598b269834a7570e40fb7f8f86fa59fac9dbacc2 [file] [log] [blame]
Meng Wang43bbb872018-12-10 12:32:05 +08001// SPDX-License-Identifier: GPL-2.0-only
Meng Wang61af6842018-09-10 17:47:55 +08002/*
Aditya Bavanari3517b112018-12-03 13:26:59 +05303 * Copyright (c) 2015-2019, The Linux Foundation. All rights reserved.
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05304 */
5
6#include <linux/irq.h>
7#include <linux/kernel.h>
8#include <linux/init.h>
9#include <linux/slab.h>
10#include <linux/io.h>
11#include <linux/interrupt.h>
12#include <linux/platform_device.h>
13#include <linux/delay.h>
14#include <linux/kthread.h>
Ramprasad Katkamcab8d722018-09-28 15:54:06 +053015#include <linux/bitops.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053016#include <linux/clk.h>
Laxminath Kasama60239e2019-01-10 14:43:03 +053017#include <linux/gpio.h>
18#include <linux/of_gpio.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053019#include <linux/pm_runtime.h>
20#include <linux/of.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053021#include <soc/soundwire.h>
Sudheer Papothi3d1596e2018-10-27 06:19:18 +053022#include <soc/swr-common.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053023#include <linux/regmap.h>
Ramprasad Katkam68765ab2018-08-30 11:46:32 +053024#include <dsp/msm-audio-event-notify.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053025#include "swrm_registers.h"
26#include "swr-mstr-ctrl.h"
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053027
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +053028#define SWRM_FRAME_SYNC_SEL 4000 /* 4KHz */
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +053029#define SWRM_FRAME_SYNC_SEL_NATIVE 3675 /* 3.675KHz */
Ramprasad Katkam57349872018-11-11 18:34:57 +053030#define SWRM_SYSTEM_RESUME_TIMEOUT_MS 700
31#define SWRM_SYS_SUSPEND_WAIT 1
Sudheer Papothi3d1596e2018-10-27 06:19:18 +053032
Sudheer Papothi4c322b12018-10-31 06:34:01 +053033#define SWRM_DSD_PARAMS_PORT 4
34
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053035#define SWR_BROADCAST_CMD_ID 0x0F
Sudheer Papothi3590b312019-06-04 23:51:30 +053036#define SWR_AUTO_SUSPEND_DELAY 1 /* delay in sec */
Sudheer Papothi7c067e82018-11-15 06:53:35 +053037#define SWR_DEV_ID_MASK 0xFFFFFFFFFFFF
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053038#define SWR_REG_VAL_PACK(data, dev, id, reg) \
39 ((reg) | ((id) << 16) | ((dev) << 20) | ((data) << 24))
40
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +053041#define SWR_INVALID_PARAM 0xFF
Laxminath Kasam990c70b2018-11-09 23:15:09 +053042#define SWR_HSTOP_MAX_VAL 0xF
43#define SWR_HSTART_MIN_VAL 0x0
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +053044
Vatsal Buchae50b5002019-09-19 14:32:20 +053045#define ERR_AUTO_SUSPEND_TIMER_VAL 0x1
46
Ramprasad Katkam83303512018-10-11 17:34:22 +053047#define SWRM_INTERRUPT_STATUS_MASK 0x1FDFD
Laxminath Kasame2291972019-11-08 14:51:59 +053048#define SWRM_LINK_STATUS_RETRY_CNT 0x5
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +053049
50#define SWRM_ROW_48 48
51#define SWRM_ROW_50 50
52#define SWRM_ROW_64 64
53#define SWRM_COL_02 02
54#define SWRM_COL_16 16
55
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053056/* pm runtime auto suspend timer in msecs */
57static int auto_suspend_timer = SWR_AUTO_SUSPEND_DELAY * 1000;
58module_param(auto_suspend_timer, int, 0664);
59MODULE_PARM_DESC(auto_suspend_timer, "timer for auto suspend");
60
61enum {
62 SWR_NOT_PRESENT, /* Device is detached/not present on the bus */
63 SWR_ATTACHED_OK, /* Device is attached */
64 SWR_ALERT, /* Device alters master for any interrupts */
65 SWR_RESERVED, /* Reserved */
66};
67
68enum {
69 MASTER_ID_WSA = 1,
70 MASTER_ID_RX,
71 MASTER_ID_TX
72};
Ramprasad Katkamcab8d722018-09-28 15:54:06 +053073
74enum {
75 ENABLE_PENDING,
76 DISABLE_PENDING
77};
Sudheer Papothi384addd2019-06-14 02:26:52 +053078
79enum {
80 LPASS_HW_CORE,
81 LPASS_AUDIO_CORE,
82};
83
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053084#define TRUE 1
85#define FALSE 0
86
Ramprasad Katkam1f221262018-08-23 15:01:22 +053087#define SWRM_MAX_PORT_REG 120
Ramprasad Katkam83303512018-10-11 17:34:22 +053088#define SWRM_MAX_INIT_REG 11
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053089
Laxminath Kasamfbcaf322018-07-18 00:38:14 +053090#define MAX_FIFO_RD_FAIL_RETRY 3
91
Ramprasad Katkam57349872018-11-11 18:34:57 +053092static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm);
93static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm);
Sudheer Papothi96c842a2019-08-29 12:11:21 +053094static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr);
95static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053096
97static bool swrm_is_msm_variant(int val)
98{
99 return (val == SWRM_VERSION_1_3);
100}
101
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530102#ifdef CONFIG_DEBUG_FS
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530103static int swrm_debug_open(struct inode *inode, struct file *file)
104{
105 file->private_data = inode->i_private;
106 return 0;
107}
108
109static int get_parameters(char *buf, u32 *param1, int num_of_par)
110{
111 char *token;
112 int base, cnt;
113
114 token = strsep(&buf, " ");
115 for (cnt = 0; cnt < num_of_par; cnt++) {
116 if (token) {
117 if ((token[1] == 'x') || (token[1] == 'X'))
118 base = 16;
119 else
120 base = 10;
121
122 if (kstrtou32(token, base, &param1[cnt]) != 0)
123 return -EINVAL;
124
125 token = strsep(&buf, " ");
126 } else
127 return -EINVAL;
128 }
129 return 0;
130}
131
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530132static ssize_t swrm_reg_show(struct swr_mstr_ctrl *swrm, char __user *ubuf,
133 size_t count, loff_t *ppos)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530134{
135 int i, reg_val, len;
136 ssize_t total = 0;
137 char tmp_buf[SWR_MSTR_MAX_BUF_LEN];
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530138 int rem = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530139
140 if (!ubuf || !ppos)
141 return 0;
142
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530143 i = ((int) *ppos + SWR_MSTR_START_REG_ADDR);
144 rem = i%4;
145
146 if (rem)
147 i = (i - rem);
148
149 for (; i <= SWR_MSTR_MAX_REG_ADDR; i += 4) {
150 usleep_range(100, 150);
151 reg_val = swr_master_read(swrm, i);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530152 len = snprintf(tmp_buf, 25, "0x%.3x: 0x%.2x\n", i, reg_val);
Aditya Bavanari9f599b42019-08-27 22:18:41 +0530153 if (len < 0) {
154 pr_err("%s: fail to fill the buffer\n", __func__);
155 total = -EFAULT;
156 goto copy_err;
157 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530158 if ((total + len) >= count - 1)
159 break;
160 if (copy_to_user((ubuf + total), tmp_buf, len)) {
161 pr_err("%s: fail to copy reg dump\n", __func__);
162 total = -EFAULT;
163 goto copy_err;
164 }
165 *ppos += len;
166 total += len;
167 }
168
169copy_err:
170 return total;
171}
172
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530173static ssize_t swrm_debug_reg_dump(struct file *file, char __user *ubuf,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530174 size_t count, loff_t *ppos)
175{
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530176 struct swr_mstr_ctrl *swrm;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530177
178 if (!count || !file || !ppos || !ubuf)
179 return -EINVAL;
180
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530181 swrm = file->private_data;
182 if (!swrm)
183 return -EINVAL;
184
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530185 if (*ppos < 0)
186 return -EINVAL;
187
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530188 return swrm_reg_show(swrm, ubuf, count, ppos);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530189}
190
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530191static ssize_t swrm_debug_read(struct file *file, char __user *ubuf,
192 size_t count, loff_t *ppos)
193{
194 char lbuf[SWR_MSTR_RD_BUF_LEN];
195 struct swr_mstr_ctrl *swrm = NULL;
196
197 if (!count || !file || !ppos || !ubuf)
198 return -EINVAL;
199
200 swrm = file->private_data;
201 if (!swrm)
202 return -EINVAL;
203
204 if (*ppos < 0)
205 return -EINVAL;
206
207 snprintf(lbuf, sizeof(lbuf), "0x%x\n", swrm->read_data);
208
209 return simple_read_from_buffer(ubuf, count, ppos, lbuf,
210 strnlen(lbuf, 7));
211}
212
213static ssize_t swrm_debug_peek_write(struct file *file, const char __user *ubuf,
214 size_t count, loff_t *ppos)
215{
216 char lbuf[SWR_MSTR_RD_BUF_LEN];
217 int rc;
218 u32 param[5];
219 struct swr_mstr_ctrl *swrm = NULL;
220
221 if (!count || !file || !ppos || !ubuf)
222 return -EINVAL;
223
224 swrm = file->private_data;
225 if (!swrm)
226 return -EINVAL;
227
228 if (*ppos < 0)
229 return -EINVAL;
230
231 if (count > sizeof(lbuf) - 1)
232 return -EINVAL;
233
234 rc = copy_from_user(lbuf, ubuf, count);
235 if (rc)
236 return -EFAULT;
237
238 lbuf[count] = '\0';
239 rc = get_parameters(lbuf, param, 1);
240 if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) && (rc == 0))
241 swrm->read_data = swr_master_read(swrm, param[0]);
242 else
243 rc = -EINVAL;
244
245 if (rc == 0)
246 rc = count;
247 else
248 dev_err(swrm->dev, "%s: rc = %d\n", __func__, rc);
249
250 return rc;
251}
252
253static ssize_t swrm_debug_write(struct file *file,
254 const char __user *ubuf, size_t count, loff_t *ppos)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530255{
256 char lbuf[SWR_MSTR_WR_BUF_LEN];
257 int rc;
258 u32 param[5];
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530259 struct swr_mstr_ctrl *swrm;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530260
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530261 if (!file || !ppos || !ubuf)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530262 return -EINVAL;
263
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530264 swrm = file->private_data;
265 if (!swrm)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530266 return -EINVAL;
267
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530268 if (count > sizeof(lbuf) - 1)
269 return -EINVAL;
270
271 rc = copy_from_user(lbuf, ubuf, count);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530272 if (rc)
273 return -EFAULT;
274
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530275 lbuf[count] = '\0';
276 rc = get_parameters(lbuf, param, 2);
277 if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) &&
278 (param[1] <= 0xFFFFFFFF) &&
279 (rc == 0))
280 swr_master_write(swrm, param[0], param[1]);
281 else
282 rc = -EINVAL;
283
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530284 if (rc == 0)
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530285 rc = count;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530286 else
287 pr_err("%s: rc = %d\n", __func__, rc);
288
289 return rc;
290}
291
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530292static const struct file_operations swrm_debug_read_ops = {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530293 .open = swrm_debug_open,
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530294 .write = swrm_debug_peek_write,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530295 .read = swrm_debug_read,
296};
297
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530298static const struct file_operations swrm_debug_write_ops = {
299 .open = swrm_debug_open,
300 .write = swrm_debug_write,
301};
302
303static const struct file_operations swrm_debug_dump_ops = {
304 .open = swrm_debug_open,
305 .read = swrm_debug_reg_dump,
306};
307#endif
308
Sudheer Papothi0016db12019-06-11 04:42:38 +0530309static void swrm_reg_dump(struct swr_mstr_ctrl *swrm,
310 u32 *reg, u32 *val, int len, const char* func)
311{
312 int i = 0;
313
314 for (i = 0; i < len; i++)
315 dev_dbg(swrm->dev, "%s: reg = 0x%x val = 0x%x\n",
316 func, reg[i], val[i]);
317}
318
Sudheer Papothi921b8652019-10-03 02:13:32 +0530319static bool is_swr_clk_needed(struct swr_mstr_ctrl *swrm)
320{
321 return ((swrm->version <= SWRM_VERSION_1_5_1) ? true : false);
322}
323
Sudheer Papothi384addd2019-06-14 02:26:52 +0530324static int swrm_request_hw_vote(struct swr_mstr_ctrl *swrm,
325 int core_type, bool enable)
326{
327 int ret = 0;
328
329 if (core_type == LPASS_HW_CORE) {
330 if (swrm->lpass_core_hw_vote) {
331 if (enable) {
332 ret =
333 clk_prepare_enable(swrm->lpass_core_hw_vote);
334 if (ret < 0)
335 dev_err(swrm->dev,
336 "%s:lpass core hw enable failed\n",
337 __func__);
338 } else
339 clk_disable_unprepare(swrm->lpass_core_hw_vote);
340 }
341 }
342 if (core_type == LPASS_AUDIO_CORE) {
343 if (swrm->lpass_core_audio) {
344 if (enable) {
345 ret =
346 clk_prepare_enable(swrm->lpass_core_audio);
347 if (ret < 0)
348 dev_err(swrm->dev,
349 "%s:lpass audio hw enable failed\n",
350 __func__);
351 } else
352 clk_disable_unprepare(swrm->lpass_core_audio);
353 }
354 }
355
356 return ret;
357}
358
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +0530359static int swrm_get_ssp_period(struct swr_mstr_ctrl *swrm,
360 int row, int col,
361 int frame_sync)
362{
363 if (!swrm || !row || !col || !frame_sync)
364 return 1;
365
366 return ((swrm->bus_clk * 2) / ((row * col) * frame_sync));
367}
368
Sudheer Papothi921b8652019-10-03 02:13:32 +0530369static int swrm_core_vote_request(struct swr_mstr_ctrl *swrm)
370{
371 int ret = 0;
372
373 if (!swrm->handle)
374 return -EINVAL;
375
376 mutex_lock(&swrm->clklock);
377 if (!swrm->dev_up) {
378 ret = -ENODEV;
379 goto exit;
380 }
381 if (swrm->core_vote) {
382 ret = swrm->core_vote(swrm->handle, true);
383 if (ret)
384 dev_err_ratelimited(swrm->dev,
385 "%s: core vote request failed\n", __func__);
386 }
387exit:
388 mutex_unlock(&swrm->clklock);
389
390 return ret;
391}
392
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530393static int swrm_clk_request(struct swr_mstr_ctrl *swrm, bool enable)
394{
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530395 int ret = 0;
396
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530397 if (!swrm->clk || !swrm->handle)
398 return -EINVAL;
399
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530400 mutex_lock(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530401 if (enable) {
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530402 if (!swrm->dev_up) {
403 ret = -ENODEV;
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530404 goto exit;
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530405 }
Sudheer Papothi921b8652019-10-03 02:13:32 +0530406 if (is_swr_clk_needed(swrm)) {
407 if (swrm->core_vote) {
408 ret = swrm->core_vote(swrm->handle, true);
409 if (ret) {
410 dev_err_ratelimited(swrm->dev,
411 "%s: core vote request failed\n",
412 __func__);
413 goto exit;
414 }
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -0700415 }
416 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530417 swrm->clk_ref_count++;
418 if (swrm->clk_ref_count == 1) {
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530419 ret = swrm->clk(swrm->handle, true);
420 if (ret) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +0530421 dev_err_ratelimited(swrm->dev,
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530422 "%s: clock enable req failed",
423 __func__);
424 --swrm->clk_ref_count;
425 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530426 }
427 } else if (--swrm->clk_ref_count == 0) {
428 swrm->clk(swrm->handle, false);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530429 complete(&swrm->clk_off_complete);
430 }
431 if (swrm->clk_ref_count < 0) {
Meng Wang8c60bb52019-06-19 15:49:06 +0800432 dev_err(swrm->dev, "%s: swrm clk count mismatch\n", __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530433 swrm->clk_ref_count = 0;
434 }
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530435
436exit:
437 mutex_unlock(&swrm->clklock);
438 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530439}
440
441static int swrm_ahb_write(struct swr_mstr_ctrl *swrm,
442 u16 reg, u32 *value)
443{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530444 u32 temp = (u32)(*value);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530445 int ret = 0;
446
447 mutex_lock(&swrm->devlock);
448 if (!swrm->dev_up)
449 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530450
Sudheer Papothi921b8652019-10-03 02:13:32 +0530451 if (is_swr_clk_needed(swrm)) {
452 ret = swrm_clk_request(swrm, TRUE);
453 if (ret) {
454 dev_err_ratelimited(swrm->dev,
455 "%s: clock request failed\n",
456 __func__);
457 goto err;
458 }
459 } else if (swrm_core_vote_request(swrm)) {
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530460 goto err;
461 }
Sudheer Papothi921b8652019-10-03 02:13:32 +0530462
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530463 iowrite32(temp, swrm->swrm_dig_base + reg);
Sudheer Papothi921b8652019-10-03 02:13:32 +0530464 if (is_swr_clk_needed(swrm))
465 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530466err:
467 mutex_unlock(&swrm->devlock);
468 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530469}
470
471static int swrm_ahb_read(struct swr_mstr_ctrl *swrm,
472 u16 reg, u32 *value)
473{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530474 u32 temp = 0;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530475 int ret = 0;
476
477 mutex_lock(&swrm->devlock);
478 if (!swrm->dev_up)
479 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530480
Sudheer Papothi921b8652019-10-03 02:13:32 +0530481 if (is_swr_clk_needed(swrm)) {
482 ret = swrm_clk_request(swrm, TRUE);
483 if (ret) {
484 dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
485 __func__);
486 goto err;
487 }
488 } else if (swrm_core_vote_request(swrm)) {
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530489 goto err;
490 }
Sudheer Papothi921b8652019-10-03 02:13:32 +0530491
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530492 temp = ioread32(swrm->swrm_dig_base + reg);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530493 *value = temp;
Sudheer Papothi921b8652019-10-03 02:13:32 +0530494 if (is_swr_clk_needed(swrm))
495 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530496err:
497 mutex_unlock(&swrm->devlock);
498 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530499}
500
501static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr)
502{
503 u32 val = 0;
504
505 if (swrm->read)
506 val = swrm->read(swrm->handle, reg_addr);
507 else
508 swrm_ahb_read(swrm, reg_addr, &val);
509 return val;
510}
511
512static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val)
513{
514 if (swrm->write)
515 swrm->write(swrm->handle, reg_addr, val);
516 else
517 swrm_ahb_write(swrm, reg_addr, &val);
518}
519
520static int swr_master_bulk_write(struct swr_mstr_ctrl *swrm, u32 *reg_addr,
521 u32 *val, unsigned int length)
522{
523 int i = 0;
524
525 if (swrm->bulk_write)
526 swrm->bulk_write(swrm->handle, reg_addr, val, length);
527 else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530528 mutex_lock(&swrm->iolock);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530529 for (i = 0; i < length; i++) {
530 /* wait for FIFO WR command to complete to avoid overflow */
Karthikeyan Mani5d52dd82019-08-15 16:58:08 -0700531 /*
532 * Reduce sleep from 100us to 10us to meet KPIs
533 * This still meets the hardware spec
534 */
535 usleep_range(10, 12);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530536 swr_master_write(swrm, reg_addr[i], val[i]);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530537 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530538 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530539 }
540 return 0;
541}
542
Laxminath Kasame2291972019-11-08 14:51:59 +0530543static bool swrm_check_link_status(struct swr_mstr_ctrl *swrm, bool active)
544{
545 int retry = SWRM_LINK_STATUS_RETRY_CNT;
546 int ret = false;
547 int status = active ? 0x1 : 0x0;
Laxminath Kasam09819e92019-11-18 14:38:11 +0530548 int comp_sts = 0x0;
Laxminath Kasame2291972019-11-08 14:51:59 +0530549
550 if ((swrm->version <= SWRM_VERSION_1_5_1))
551 return true;
552
553 do {
Laxminath Kasam09819e92019-11-18 14:38:11 +0530554 comp_sts = swr_master_read(swrm, SWRM_COMP_STATUS) & 0x01;
555 /* check comp status and status requested met */
556 if ((comp_sts && status) || (!comp_sts && !status)) {
Laxminath Kasame2291972019-11-08 14:51:59 +0530557 ret = true;
558 break;
559 }
560 retry--;
561 usleep_range(500, 510);
562 } while (retry);
563
564 if (retry == 0)
565 dev_err(swrm->dev, "%s: link status not %s\n", __func__,
566 active ? "connected" : "disconnected");
567
568 return ret;
569}
570
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530571static bool swrm_is_port_en(struct swr_master *mstr)
572{
573 return !!(mstr->num_port);
574}
575
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530576static void copy_port_tables(struct swr_mstr_ctrl *swrm,
577 struct port_params *params)
578{
579 u8 i;
580 struct port_params *config = params;
581
582 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
583 /* wsa uses single frame structure for all configurations */
584 if (!swrm->mport_cfg[i].port_en)
585 continue;
586 swrm->mport_cfg[i].sinterval = config[i].si;
587 swrm->mport_cfg[i].offset1 = config[i].off1;
588 swrm->mport_cfg[i].offset2 = config[i].off2;
589 swrm->mport_cfg[i].hstart = config[i].hstart;
590 swrm->mport_cfg[i].hstop = config[i].hstop;
591 swrm->mport_cfg[i].blk_pack_mode = config[i].bp_mode;
592 swrm->mport_cfg[i].blk_grp_count = config[i].bgp_ctrl;
593 swrm->mport_cfg[i].word_length = config[i].wd_len;
594 swrm->mport_cfg[i].lane_ctrl = config[i].lane_ctrl;
595 }
596}
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530597static int swrm_get_port_config(struct swr_mstr_ctrl *swrm)
598{
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530599 struct port_params *params;
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530600 u32 usecase = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530601
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530602 /* TODO - Send usecase information to avoid checking for master_id */
603 if (swrm->mport_cfg[SWRM_DSD_PARAMS_PORT].port_en &&
604 (swrm->master_id == MASTER_ID_RX))
605 usecase = 1;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530606
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530607 params = swrm->port_param[usecase];
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530608 copy_port_tables(swrm, params);
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530609
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530610 return 0;
611}
612
613static int swrm_get_master_port(struct swr_mstr_ctrl *swrm, u8 *mstr_port_id,
614 u8 *mstr_ch_mask, u8 mstr_prt_type,
615 u8 slv_port_id)
616{
617 int i, j;
618 *mstr_port_id = 0;
619
620 for (i = 1; i <= swrm->num_ports; i++) {
621 for (j = 0; j < SWR_MAX_CH_PER_PORT; j++) {
622 if (swrm->port_mapping[i][j].port_type == mstr_prt_type)
623 goto found;
624 }
625 }
626found:
627 if (i > swrm->num_ports || j == SWR_MAX_CH_PER_PORT) {
628 dev_err(swrm->dev, "%s: port type not supported by master\n",
629 __func__);
630 return -EINVAL;
631 }
632 /* id 0 corresponds to master port 1 */
633 *mstr_port_id = i - 1;
634 *mstr_ch_mask = swrm->port_mapping[i][j].ch_mask;
635
636 return 0;
637
638}
639
640static u32 swrm_get_packed_reg_val(u8 *cmd_id, u8 cmd_data,
641 u8 dev_addr, u16 reg_addr)
642{
643 u32 val;
644 u8 id = *cmd_id;
645
646 if (id != SWR_BROADCAST_CMD_ID) {
647 if (id < 14)
648 id += 1;
649 else
650 id = 0;
651 *cmd_id = id;
652 }
653 val = SWR_REG_VAL_PACK(cmd_data, dev_addr, id, reg_addr);
654
655 return val;
656}
657
658static int swrm_cmd_fifo_rd_cmd(struct swr_mstr_ctrl *swrm, int *cmd_data,
659 u8 dev_addr, u8 cmd_id, u16 reg_addr,
660 u32 len)
661{
662 u32 val;
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530663 u32 retry_attempt = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530664
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530665 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530666 val = swrm_get_packed_reg_val(&swrm->rcmd_id, len, dev_addr, reg_addr);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530667 if (swrm->read) {
668 /* skip delay if read is handled in platform driver */
669 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
670 } else {
671 /* wait for FIFO RD to complete to avoid overflow */
672 usleep_range(100, 105);
673 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
674 /* wait for FIFO RD CMD complete to avoid overflow */
675 usleep_range(250, 255);
676 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530677retry_read:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530678 *cmd_data = swr_master_read(swrm, SWRM_CMD_FIFO_RD_FIFO_ADDR);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530679 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, rcmd_id: 0x%x, \
680 dev_num: 0x%x, cmd_data: 0x%x\n", __func__, reg_addr,
681 cmd_id, swrm->rcmd_id, dev_addr, *cmd_data);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530682 if ((((*cmd_data) & 0xF00) >> 8) != swrm->rcmd_id) {
683 if (retry_attempt < MAX_FIFO_RD_FAIL_RETRY) {
684 /* wait 500 us before retry on fifo read failure */
685 usleep_range(500, 505);
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +0530686 if (retry_attempt == (MAX_FIFO_RD_FAIL_RETRY - 1)) {
687 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
688 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
689 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530690 retry_attempt++;
691 goto retry_read;
692 } else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530693 dev_err_ratelimited(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, \
694 rcmd_id: 0x%x, dev_num: 0x%x, cmd_data: 0x%x\n",
695 __func__, reg_addr, cmd_id, swrm->rcmd_id,
696 dev_addr, *cmd_data);
697
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530698 dev_err_ratelimited(swrm->dev,
699 "%s: failed to read fifo\n", __func__);
700 }
701 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530702 mutex_unlock(&swrm->iolock);
703
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530704 return 0;
705}
706
707static int swrm_cmd_fifo_wr_cmd(struct swr_mstr_ctrl *swrm, u8 cmd_data,
708 u8 dev_addr, u8 cmd_id, u16 reg_addr)
709{
710 u32 val;
711 int ret = 0;
712
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530713 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530714 if (!cmd_id)
715 val = swrm_get_packed_reg_val(&swrm->wcmd_id, cmd_data,
716 dev_addr, reg_addr);
717 else
718 val = swrm_get_packed_reg_val(&cmd_id, cmd_data,
719 dev_addr, reg_addr);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530720 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x,wcmd_id: 0x%x, \
721 dev_num: 0x%x, cmd_data: 0x%x\n", __func__,
722 reg_addr, cmd_id, swrm->wcmd_id,dev_addr, cmd_data);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +0530723 swr_master_write(swrm, SWRM_CMD_FIFO_WR_CMD, val);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530724 /*
725 * wait for FIFO WR command to complete to avoid overflow
726 * skip delay if write is handled in platform driver.
727 */
728 if(!swrm->write)
Karthikeyan Mani5d52dd82019-08-15 16:58:08 -0700729 usleep_range(150, 155);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530730 if (cmd_id == 0xF) {
731 /*
732 * sleep for 10ms for MSM soundwire variant to allow broadcast
733 * command to complete.
734 */
735 if (swrm_is_msm_variant(swrm->version))
736 usleep_range(10000, 10100);
737 else
738 wait_for_completion_timeout(&swrm->broadcast,
739 (2 * HZ/10));
740 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530741 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530742 return ret;
743}
744
745static int swrm_read(struct swr_master *master, u8 dev_num, u16 reg_addr,
746 void *buf, u32 len)
747{
748 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
749 int ret = 0;
750 int val;
751 u8 *reg_val = (u8 *)buf;
752
753 if (!swrm) {
754 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
755 return -EINVAL;
756 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530757 if (!dev_num) {
758 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
759 return -EINVAL;
760 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530761 mutex_lock(&swrm->devlock);
762 if (!swrm->dev_up) {
763 mutex_unlock(&swrm->devlock);
764 return 0;
765 }
766 mutex_unlock(&swrm->devlock);
767
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530768 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530769 ret = swrm_cmd_fifo_rd_cmd(swrm, &val, dev_num, 0, reg_addr, len);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530770
771 if (!ret)
772 *reg_val = (u8)val;
773
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530774 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530775 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530776 return ret;
777}
778
779static int swrm_write(struct swr_master *master, u8 dev_num, u16 reg_addr,
780 const void *buf)
781{
782 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
783 int ret = 0;
784 u8 reg_val = *(u8 *)buf;
785
786 if (!swrm) {
787 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
788 return -EINVAL;
789 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530790 if (!dev_num) {
791 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
792 return -EINVAL;
793 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530794 mutex_lock(&swrm->devlock);
795 if (!swrm->dev_up) {
796 mutex_unlock(&swrm->devlock);
797 return 0;
798 }
799 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530800
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530801 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530802 ret = swrm_cmd_fifo_wr_cmd(swrm, reg_val, dev_num, 0, reg_addr);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530803
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530804 pm_runtime_put_autosuspend(swrm->dev);
805 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530806 return ret;
807}
808
809static int swrm_bulk_write(struct swr_master *master, u8 dev_num, void *reg,
810 const void *buf, size_t len)
811{
812 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
813 int ret = 0;
814 int i;
815 u32 *val;
816 u32 *swr_fifo_reg;
817
818 if (!swrm || !swrm->handle) {
819 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
820 return -EINVAL;
821 }
822 if (len <= 0)
823 return -EINVAL;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530824 mutex_lock(&swrm->devlock);
825 if (!swrm->dev_up) {
826 mutex_unlock(&swrm->devlock);
827 return 0;
828 }
829 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530830
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530831 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530832 if (dev_num) {
833 swr_fifo_reg = kcalloc(len, sizeof(u32), GFP_KERNEL);
834 if (!swr_fifo_reg) {
835 ret = -ENOMEM;
836 goto err;
837 }
838 val = kcalloc(len, sizeof(u32), GFP_KERNEL);
839 if (!val) {
840 ret = -ENOMEM;
841 goto mem_fail;
842 }
843
844 for (i = 0; i < len; i++) {
845 val[i] = swrm_get_packed_reg_val(&swrm->wcmd_id,
846 ((u8 *)buf)[i],
847 dev_num,
848 ((u16 *)reg)[i]);
849 swr_fifo_reg[i] = SWRM_CMD_FIFO_WR_CMD;
850 }
851 ret = swr_master_bulk_write(swrm, swr_fifo_reg, val, len);
852 if (ret) {
853 dev_err(&master->dev, "%s: bulk write failed\n",
854 __func__);
855 ret = -EINVAL;
856 }
857 } else {
858 dev_err(&master->dev,
859 "%s: No support of Bulk write for master regs\n",
860 __func__);
861 ret = -EINVAL;
862 goto err;
863 }
864 kfree(val);
865mem_fail:
866 kfree(swr_fifo_reg);
867err:
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530868 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530869 pm_runtime_mark_last_busy(swrm->dev);
870 return ret;
871}
872
873static u8 get_inactive_bank_num(struct swr_mstr_ctrl *swrm)
874{
875 return (swr_master_read(swrm, SWRM_MCP_STATUS) &
876 SWRM_MCP_STATUS_BANK_NUM_MASK) ? 0 : 1;
877}
878
879static void enable_bank_switch(struct swr_mstr_ctrl *swrm, u8 bank,
880 u8 row, u8 col)
881{
882 swrm_cmd_fifo_wr_cmd(swrm, ((row << 3) | col), 0xF, 0xF,
883 SWRS_SCP_FRAME_CTRL_BANK(bank));
884}
885
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +0530886static void swrm_switch_frame_shape(struct swr_mstr_ctrl *swrm, int mclk_freq)
887{
888 u8 bank;
889 u32 n_row, n_col;
890 u32 value = 0;
891 u32 row = 0, col = 0;
892 u8 ssp_period = 0;
893 int frame_sync = SWRM_FRAME_SYNC_SEL;
894
895 if (mclk_freq == MCLK_FREQ_NATIVE) {
896 n_col = SWR_MAX_COL;
897 col = SWRM_COL_16;
898 n_row = SWR_ROW_64;
899 row = SWRM_ROW_64;
900 frame_sync = SWRM_FRAME_SYNC_SEL_NATIVE;
901 } else {
902 n_col = SWR_MIN_COL;
903 col = SWRM_COL_02;
904 n_row = SWR_ROW_50;
905 row = SWRM_ROW_50;
906 frame_sync = SWRM_FRAME_SYNC_SEL;
907 }
908
909 bank = get_inactive_bank_num(swrm);
910 ssp_period = swrm_get_ssp_period(swrm, row, col, frame_sync);
911 dev_dbg(swrm->dev, "%s: ssp_period: %d\n", __func__, ssp_period);
912 value = ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
913 (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
914 ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
915 swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
916 enable_bank_switch(swrm, bank, n_row, n_col);
917}
918
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530919static struct swr_port_info *swrm_get_port_req(struct swrm_mports *mport,
920 u8 slv_port, u8 dev_num)
921{
922 struct swr_port_info *port_req = NULL;
923
924 list_for_each_entry(port_req, &mport->port_req_list, list) {
925 /* Store dev_id instead of dev_num if enumeration is changed run_time */
926 if ((port_req->slave_port_id == slv_port)
927 && (port_req->dev_num == dev_num))
928 return port_req;
929 }
930 return NULL;
931}
932
933static bool swrm_remove_from_group(struct swr_master *master)
934{
935 struct swr_device *swr_dev;
936 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
937 bool is_removed = false;
938
939 if (!swrm)
940 goto end;
941
942 mutex_lock(&swrm->mlock);
943 if ((swrm->num_rx_chs > 1) &&
944 (swrm->num_rx_chs == swrm->num_cfg_devs)) {
945 list_for_each_entry(swr_dev, &master->devices,
946 dev_list) {
947 swr_dev->group_id = SWR_GROUP_NONE;
948 master->gr_sid = 0;
949 }
950 is_removed = true;
951 }
952 mutex_unlock(&swrm->mlock);
953
954end:
955 return is_removed;
956}
957
958static void swrm_disable_ports(struct swr_master *master,
959 u8 bank)
960{
961 u32 value;
962 struct swr_port_info *port_req;
963 int i;
964 struct swrm_mports *mport;
965 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
966
967 if (!swrm) {
968 pr_err("%s: swrm is null\n", __func__);
969 return;
970 }
971
972 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
973 master->num_port);
974
975
976 for (i = 0; i < SWR_MSTR_PORT_LEN ; i++) {
977
978 mport = &(swrm->mport_cfg[i]);
979 if (!mport->port_en)
980 continue;
981
982 list_for_each_entry(port_req, &mport->port_req_list, list) {
983 /* skip ports with no change req's*/
984 if (port_req->req_ch == port_req->ch_en)
985 continue;
986
987 swrm_cmd_fifo_wr_cmd(swrm, port_req->req_ch,
988 port_req->dev_num, 0x00,
989 SWRS_DP_CHANNEL_ENABLE_BANK(port_req->slave_port_id,
990 bank));
991 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x\n",
992 __func__, i,
993 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)));
994 }
995 value = ((mport->req_ch)
996 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
997 value |= ((mport->offset2)
998 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
999 value |= ((mport->offset1)
1000 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
1001 value |= mport->sinterval;
1002
1003 swr_master_write(swrm,
1004 SWRM_DP_PORT_CTRL_BANK(i+1, bank),
1005 value);
1006 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
1007 __func__, i,
1008 (SWRM_DP_PORT_CTRL_BANK(i+1, bank)), value);
1009 }
1010}
1011
1012static void swrm_cleanup_disabled_port_reqs(struct swr_master *master)
1013{
1014 struct swr_port_info *port_req, *next;
1015 int i;
1016 struct swrm_mports *mport;
1017 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1018
1019 if (!swrm) {
1020 pr_err("%s: swrm is null\n", __func__);
1021 return;
1022 }
1023 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
1024 master->num_port);
1025
1026 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
1027 mport = &(swrm->mport_cfg[i]);
1028 list_for_each_entry_safe(port_req, next,
1029 &mport->port_req_list, list) {
1030 /* skip ports without new ch req */
1031 if (port_req->ch_en == port_req->req_ch)
1032 continue;
1033
1034 /* remove new ch req's*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +05301035 port_req->ch_en = port_req->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301036
1037 /* If no streams enabled on port, remove the port req */
1038 if (port_req->ch_en == 0) {
1039 list_del(&port_req->list);
1040 kfree(port_req);
1041 }
1042 }
1043 /* remove new ch req's on mport*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +05301044 mport->ch_en = mport->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301045
1046 if (!(mport->ch_en)) {
1047 mport->port_en = false;
1048 master->port_en_mask &= ~i;
1049 }
1050 }
1051}
1052static void swrm_copy_data_port_config(struct swr_master *master, u8 bank)
1053{
1054 u32 value, slv_id;
1055 struct swr_port_info *port_req;
1056 int i;
1057 struct swrm_mports *mport;
1058 u32 reg[SWRM_MAX_PORT_REG];
1059 u32 val[SWRM_MAX_PORT_REG];
1060 int len = 0;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301061 u8 hparams;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301062 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1063
1064 if (!swrm) {
1065 pr_err("%s: swrm is null\n", __func__);
1066 return;
1067 }
1068
1069 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
1070 master->num_port);
1071
1072 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
1073 mport = &(swrm->mport_cfg[i]);
1074 if (!mport->port_en)
1075 continue;
1076
1077 list_for_each_entry(port_req, &mport->port_req_list, list) {
1078 slv_id = port_req->slave_port_id;
1079 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1080 val[len++] = SWR_REG_VAL_PACK(port_req->req_ch,
1081 port_req->dev_num, 0x00,
1082 SWRS_DP_CHANNEL_ENABLE_BANK(slv_id,
1083 bank));
1084
1085 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1086 val[len++] = SWR_REG_VAL_PACK(mport->sinterval,
1087 port_req->dev_num, 0x00,
1088 SWRS_DP_SAMPLE_CONTROL_1_BANK(slv_id,
1089 bank));
1090
1091 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1092 val[len++] = SWR_REG_VAL_PACK(mport->offset1,
1093 port_req->dev_num, 0x00,
1094 SWRS_DP_OFFSET_CONTROL_1_BANK(slv_id,
1095 bank));
1096
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301097 if (mport->offset2 != SWR_INVALID_PARAM) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301098 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1099 val[len++] = SWR_REG_VAL_PACK(mport->offset2,
1100 port_req->dev_num, 0x00,
1101 SWRS_DP_OFFSET_CONTROL_2_BANK(
1102 slv_id, bank));
1103 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301104 if (mport->hstart != SWR_INVALID_PARAM
1105 && mport->hstop != SWR_INVALID_PARAM) {
1106 hparams = (mport->hstart << 4) | mport->hstop;
1107
1108 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1109 val[len++] = SWR_REG_VAL_PACK(hparams,
1110 port_req->dev_num, 0x00,
1111 SWRS_DP_HCONTROL_BANK(slv_id,
1112 bank));
1113 }
1114 if (mport->word_length != SWR_INVALID_PARAM) {
1115 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1116 val[len++] =
1117 SWR_REG_VAL_PACK(mport->word_length,
1118 port_req->dev_num, 0x00,
1119 SWRS_DP_BLOCK_CONTROL_1(slv_id));
1120 }
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +05301121 if (mport->blk_pack_mode != SWR_INVALID_PARAM
1122 && swrm->master_id != MASTER_ID_WSA) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301123 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1124 val[len++] =
1125 SWR_REG_VAL_PACK(mport->blk_pack_mode,
1126 port_req->dev_num, 0x00,
1127 SWRS_DP_BLOCK_CONTROL_3_BANK(slv_id,
1128 bank));
1129 }
1130 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
1131 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1132 val[len++] =
1133 SWR_REG_VAL_PACK(mport->blk_grp_count,
1134 port_req->dev_num, 0x00,
1135 SWRS_DP_BLOCK_CONTROL_2_BANK(slv_id,
1136 bank));
1137 }
1138 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
1139 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1140 val[len++] =
1141 SWR_REG_VAL_PACK(mport->lane_ctrl,
1142 port_req->dev_num, 0x00,
1143 SWRS_DP_LANE_CONTROL_BANK(slv_id,
1144 bank));
1145 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301146 port_req->ch_en = port_req->req_ch;
1147 }
1148 value = ((mport->req_ch)
1149 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +05301150
1151 if (mport->offset2 != SWR_INVALID_PARAM)
1152 value |= ((mport->offset2)
1153 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301154 value |= ((mport->offset1)
1155 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
1156 value |= mport->sinterval;
1157
1158
1159 reg[len] = SWRM_DP_PORT_CTRL_BANK(i + 1, bank);
1160 val[len++] = value;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301161 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
1162 __func__, i,
1163 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)), value);
1164
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301165 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
1166 reg[len] = SWRM_DP_PORT_CTRL_2_BANK(i + 1, bank);
1167 val[len++] = mport->lane_ctrl;
1168 }
1169 if (mport->word_length != SWR_INVALID_PARAM) {
1170 reg[len] = SWRM_DP_BLOCK_CTRL_1(i + 1);
1171 val[len++] = mport->word_length;
1172 }
1173
1174 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
1175 reg[len] = SWRM_DP_BLOCK_CTRL2_BANK(i + 1, bank);
1176 val[len++] = mport->blk_grp_count;
1177 }
1178 if (mport->hstart != SWR_INVALID_PARAM
1179 && mport->hstop != SWR_INVALID_PARAM) {
1180 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
Laxminath Kasame30eef72018-11-05 17:40:09 +05301181 hparams = (mport->hstop << 4) | mport->hstart;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301182 val[len++] = hparams;
Laxminath Kasam990c70b2018-11-09 23:15:09 +05301183 } else {
1184 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
1185 hparams = (SWR_HSTOP_MAX_VAL << 4) | SWR_HSTART_MIN_VAL;
1186 val[len++] = hparams;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301187 }
1188 if (mport->blk_pack_mode != SWR_INVALID_PARAM) {
1189 reg[len] = SWRM_DP_BLOCK_CTRL3_BANK(i + 1, bank);
1190 val[len++] = mport->blk_pack_mode;
1191 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301192 mport->ch_en = mport->req_ch;
1193
1194 }
Sudheer Papothi0016db12019-06-11 04:42:38 +05301195 swrm_reg_dump(swrm, reg, val, len, __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301196 swr_master_bulk_write(swrm, reg, val, len);
1197}
1198
1199static void swrm_apply_port_config(struct swr_master *master)
1200{
1201 u8 bank;
1202 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1203
1204 if (!swrm) {
1205 pr_err("%s: Invalid handle to swr controller\n",
1206 __func__);
1207 return;
1208 }
1209
1210 bank = get_inactive_bank_num(swrm);
1211 dev_dbg(swrm->dev, "%s: enter bank: %d master_ports: %d\n",
1212 __func__, bank, master->num_port);
1213
1214
1215 swrm_cmd_fifo_wr_cmd(swrm, 0x01, 0xF, 0x00,
1216 SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(bank));
1217
1218 swrm_copy_data_port_config(master, bank);
1219}
1220
1221static int swrm_slvdev_datapath_control(struct swr_master *master, bool enable)
1222{
1223 u8 bank;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301224 u32 value, n_row, n_col;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301225 u32 row = 0, col = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301226 int ret;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301227 u8 ssp_period = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301228 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1229 int mask = (SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK |
1230 SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK |
1231 SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_BMSK);
1232 u8 inactive_bank;
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +05301233 int frame_sync = SWRM_FRAME_SYNC_SEL;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301234
1235 if (!swrm) {
1236 pr_err("%s: swrm is null\n", __func__);
1237 return -EFAULT;
1238 }
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301239
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301240 mutex_lock(&swrm->mlock);
1241
Ramprasad Katkam979b7c92019-05-17 15:31:21 +05301242 /*
1243 * During disable if master is already down, which implies an ssr/pdr
1244 * scenario, just mark ports as disabled and exit
1245 */
1246 if (swrm->state == SWR_MSTR_SSR && !enable) {
1247 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1248 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1249 __func__);
1250 goto exit;
1251 }
1252 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
1253 swrm_cleanup_disabled_port_reqs(master);
1254 if (!swrm_is_port_en(master)) {
1255 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1256 __func__);
1257 pm_runtime_mark_last_busy(swrm->dev);
1258 pm_runtime_put_autosuspend(swrm->dev);
1259 }
1260 goto exit;
1261 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301262 bank = get_inactive_bank_num(swrm);
1263
1264 if (enable) {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301265 if (!test_bit(ENABLE_PENDING, &swrm->port_req_pending)) {
1266 dev_dbg(swrm->dev, "%s:No pending connect port req\n",
1267 __func__);
1268 goto exit;
1269 }
1270 clear_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301271 ret = swrm_get_port_config(swrm);
1272 if (ret) {
1273 /* cannot accommodate ports */
1274 swrm_cleanup_disabled_port_reqs(master);
1275 mutex_unlock(&swrm->mlock);
1276 return -EINVAL;
1277 }
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301278 swr_master_write(swrm, SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301279 SWRM_INTERRUPT_STATUS_MASK);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301280 /* apply the new port config*/
1281 swrm_apply_port_config(master);
1282 } else {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301283 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1284 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1285 __func__);
1286 goto exit;
1287 }
1288 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301289 swrm_disable_ports(master, bank);
1290 }
1291 dev_dbg(swrm->dev, "%s: enable: %d, cfg_devs: %d\n",
1292 __func__, enable, swrm->num_cfg_devs);
1293
1294 if (enable) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301295 /* set col = 16 */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301296 n_col = SWR_MAX_COL;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301297 col = SWRM_COL_16;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301298 } else {
1299 /*
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301300 * Do not change to col = 2 if there are still active ports
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301301 */
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301302 if (!master->num_port) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301303 n_col = SWR_MIN_COL;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301304 col = SWRM_COL_02;
1305 } else {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301306 n_col = SWR_MAX_COL;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301307 col = SWRM_COL_16;
1308 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301309 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301310 /* Use default 50 * x, frame shape. Change based on mclk */
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301311 if (swrm->mclk_freq == MCLK_FREQ_NATIVE) {
1312 dev_dbg(swrm->dev, "setting 64 x %d frameshape\n",
1313 n_col ? 16 : 2);
1314 n_row = SWR_ROW_64;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301315 row = SWRM_ROW_64;
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +05301316 frame_sync = SWRM_FRAME_SYNC_SEL_NATIVE;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301317 } else {
1318 dev_dbg(swrm->dev, "setting 50 x %d frameshape\n",
1319 n_col ? 16 : 2);
1320 n_row = SWR_ROW_50;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301321 row = SWRM_ROW_50;
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +05301322 frame_sync = SWRM_FRAME_SYNC_SEL;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301323 }
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +05301324 ssp_period = swrm_get_ssp_period(swrm, row, col, frame_sync);
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301325 dev_dbg(swrm->dev, "%s: ssp_period: %d\n", __func__, ssp_period);
1326
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301327 value = swr_master_read(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank));
1328 value &= (~mask);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301329 value |= ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301330 (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301331 ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301332 swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1333
1334 dev_dbg(swrm->dev, "%s: regaddr: 0x%x, value: 0x%x\n", __func__,
1335 SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1336
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301337 enable_bank_switch(swrm, bank, n_row, n_col);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301338 inactive_bank = bank ? 0 : 1;
1339
1340 if (enable)
1341 swrm_copy_data_port_config(master, inactive_bank);
1342 else {
1343 swrm_disable_ports(master, inactive_bank);
1344 swrm_cleanup_disabled_port_reqs(master);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301345 }
1346 if (!swrm_is_port_en(master)) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301347 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1348 __func__);
1349 pm_runtime_mark_last_busy(swrm->dev);
1350 pm_runtime_put_autosuspend(swrm->dev);
1351 }
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301352exit:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301353 mutex_unlock(&swrm->mlock);
1354return 0;
1355}
1356
1357static int swrm_connect_port(struct swr_master *master,
1358 struct swr_params *portinfo)
1359{
1360 int i;
1361 struct swr_port_info *port_req;
1362 int ret = 0;
1363 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1364 struct swrm_mports *mport;
1365 u8 mstr_port_id, mstr_ch_msk;
1366
1367 dev_dbg(&master->dev, "%s: enter\n", __func__);
1368 if (!portinfo)
1369 return -EINVAL;
1370
1371 if (!swrm) {
1372 dev_err(&master->dev,
1373 "%s: Invalid handle to swr controller\n",
1374 __func__);
1375 return -EINVAL;
1376 }
1377
1378 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301379 mutex_lock(&swrm->devlock);
1380 if (!swrm->dev_up) {
1381 mutex_unlock(&swrm->devlock);
1382 mutex_unlock(&swrm->mlock);
1383 return -EINVAL;
1384 }
1385 mutex_unlock(&swrm->devlock);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301386 if (!swrm_is_port_en(master))
1387 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301388
1389 for (i = 0; i < portinfo->num_port; i++) {
1390 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_msk,
1391 portinfo->port_type[i],
1392 portinfo->port_id[i]);
1393 if (ret) {
1394 dev_err(&master->dev,
1395 "%s: mstr portid for slv port %d not found\n",
1396 __func__, portinfo->port_id[i]);
1397 goto port_fail;
1398 }
1399
1400 mport = &(swrm->mport_cfg[mstr_port_id]);
1401 /* get port req */
1402 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1403 portinfo->dev_num);
1404 if (!port_req) {
1405 dev_dbg(&master->dev, "%s: new req:port id %d dev %d\n",
1406 __func__, portinfo->port_id[i],
1407 portinfo->dev_num);
1408 port_req = kzalloc(sizeof(struct swr_port_info),
1409 GFP_KERNEL);
1410 if (!port_req) {
1411 ret = -ENOMEM;
1412 goto mem_fail;
1413 }
1414 port_req->dev_num = portinfo->dev_num;
1415 port_req->slave_port_id = portinfo->port_id[i];
1416 port_req->num_ch = portinfo->num_ch[i];
1417 port_req->ch_rate = portinfo->ch_rate[i];
1418 port_req->ch_en = 0;
1419 port_req->master_port_id = mstr_port_id;
1420 list_add(&port_req->list, &mport->port_req_list);
1421 }
1422 port_req->req_ch |= portinfo->ch_en[i];
1423
1424 dev_dbg(&master->dev,
1425 "%s: mstr port %d, slv port %d ch_rate %d num_ch %d\n",
1426 __func__, port_req->master_port_id,
1427 port_req->slave_port_id, port_req->ch_rate,
1428 port_req->num_ch);
1429 /* Put the port req on master port */
1430 mport = &(swrm->mport_cfg[mstr_port_id]);
1431 mport->port_en = true;
1432 mport->req_ch |= mstr_ch_msk;
1433 master->port_en_mask |= (1 << mstr_port_id);
1434 }
1435 master->num_port += portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301436 set_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301437 swr_port_response(master, portinfo->tid);
1438
1439 mutex_unlock(&swrm->mlock);
1440 return 0;
1441
1442port_fail:
1443mem_fail:
1444 /* cleanup port reqs in error condition */
1445 swrm_cleanup_disabled_port_reqs(master);
1446 mutex_unlock(&swrm->mlock);
1447 return ret;
1448}
1449
1450static int swrm_disconnect_port(struct swr_master *master,
1451 struct swr_params *portinfo)
1452{
1453 int i, ret = 0;
1454 struct swr_port_info *port_req;
1455 struct swrm_mports *mport;
1456 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1457 u8 mstr_port_id, mstr_ch_mask;
1458
1459 if (!swrm) {
1460 dev_err(&master->dev,
1461 "%s: Invalid handle to swr controller\n",
1462 __func__);
1463 return -EINVAL;
1464 }
1465
1466 if (!portinfo) {
1467 dev_err(&master->dev, "%s: portinfo is NULL\n", __func__);
1468 return -EINVAL;
1469 }
1470 mutex_lock(&swrm->mlock);
1471
1472 for (i = 0; i < portinfo->num_port; i++) {
1473
1474 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_mask,
1475 portinfo->port_type[i], portinfo->port_id[i]);
1476 if (ret) {
1477 dev_err(&master->dev,
1478 "%s: mstr portid for slv port %d not found\n",
1479 __func__, portinfo->port_id[i]);
1480 mutex_unlock(&swrm->mlock);
1481 return -EINVAL;
1482 }
1483 mport = &(swrm->mport_cfg[mstr_port_id]);
1484 /* get port req */
1485 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1486 portinfo->dev_num);
1487
1488 if (!port_req) {
1489 dev_err(&master->dev, "%s:port not enabled : port %d\n",
1490 __func__, portinfo->port_id[i]);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05301491 mutex_unlock(&swrm->mlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301492 return -EINVAL;
1493 }
1494 port_req->req_ch &= ~portinfo->ch_en[i];
1495 mport->req_ch &= ~mstr_ch_mask;
1496 }
1497 master->num_port -= portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301498 set_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301499 swr_port_response(master, portinfo->tid);
1500 mutex_unlock(&swrm->mlock);
1501
1502 return 0;
1503}
1504
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301505static int swrm_find_alert_slave(struct swr_mstr_ctrl *swrm,
1506 int status, u8 *devnum)
1507{
1508 int i;
1509 bool found = false;
1510
1511 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1512 if ((status & SWRM_MCP_SLV_STATUS_MASK) == SWR_ALERT) {
1513 *devnum = i;
1514 found = true;
1515 break;
1516 }
1517 status >>= 2;
1518 }
1519 if (found)
1520 return 0;
1521 else
1522 return -EINVAL;
1523}
1524
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301525static void swrm_enable_slave_irq(struct swr_mstr_ctrl *swrm)
1526{
1527 int i;
1528 int status = 0;
1529
1530 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1531 if (!status) {
1532 dev_dbg_ratelimited(swrm->dev, "%s: slaves status is 0x%x\n",
1533 __func__, status);
1534 return;
1535 }
1536 dev_dbg(swrm->dev, "%s: slave status: 0x%x\n", __func__, status);
1537 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1538 if (status & SWRM_MCP_SLV_STATUS_MASK)
1539 swrm_cmd_fifo_wr_cmd(swrm, 0x4, i, 0x0,
1540 SWRS_SCP_INT_STATUS_MASK_1);
1541 status >>= 2;
1542 }
1543}
1544
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301545static int swrm_check_slave_change_status(struct swr_mstr_ctrl *swrm,
1546 int status, u8 *devnum)
1547{
1548 int i;
1549 int new_sts = status;
1550 int ret = SWR_NOT_PRESENT;
1551
1552 if (status != swrm->slave_status) {
1553 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1554 if ((status & SWRM_MCP_SLV_STATUS_MASK) !=
1555 (swrm->slave_status & SWRM_MCP_SLV_STATUS_MASK)) {
1556 ret = (status & SWRM_MCP_SLV_STATUS_MASK);
1557 *devnum = i;
1558 break;
1559 }
1560 status >>= 2;
1561 swrm->slave_status >>= 2;
1562 }
1563 swrm->slave_status = new_sts;
1564 }
1565 return ret;
1566}
1567
1568static irqreturn_t swr_mstr_interrupt(int irq, void *dev)
1569{
1570 struct swr_mstr_ctrl *swrm = dev;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301571 u32 value, intr_sts, intr_sts_masked;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301572 u32 temp = 0;
1573 u32 status, chg_sts, i;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301574 u8 devnum = 0;
1575 int ret = IRQ_HANDLED;
1576 struct swr_device *swr_dev;
1577 struct swr_master *mstr = &swrm->master;
1578
Ramprasad Katkam57349872018-11-11 18:34:57 +05301579 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1580 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1581 return IRQ_NONE;
1582 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301583
1584 mutex_lock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301585 if (swrm_clk_request(swrm, true)) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +05301586 dev_err_ratelimited(swrm->dev, "%s:clk request failed\n",
1587 __func__);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301588 mutex_unlock(&swrm->reslock);
1589 goto exit;
1590 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301591 mutex_unlock(&swrm->reslock);
1592
1593 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301594 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301595handle_irq:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301596 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301597 value = intr_sts_masked & (1 << i);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301598 if (!value)
1599 continue;
1600
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301601 switch (value) {
1602 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1603 dev_dbg(swrm->dev, "Trigger irq to slave device\n");
1604 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301605 ret = swrm_find_alert_slave(swrm, status, &devnum);
1606 if (ret) {
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301607 dev_err_ratelimited(swrm->dev,
1608 "no slave alert found.spurious interrupt\n");
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05301609 break;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301610 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301611 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1612 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1613 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1614 SWRS_SCP_INT_STATUS_CLEAR_1);
1615 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1616 SWRS_SCP_INT_STATUS_CLEAR_1);
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301617
1618
1619 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1620 if (swr_dev->dev_num != devnum)
1621 continue;
1622 if (swr_dev->slave_irq) {
1623 do {
Ramprasad Katkam2586a4b2019-03-18 16:53:39 +05301624 swr_dev->slave_irq_pending = 0;
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301625 handle_nested_irq(
1626 irq_find_mapping(
1627 swr_dev->slave_irq, 0));
1628 } while (swr_dev->slave_irq_pending);
1629 }
1630
1631 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301632 break;
1633 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1634 dev_dbg(swrm->dev, "SWR new slave attached\n");
1635 break;
1636 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1637 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1638 if (status == swrm->slave_status) {
1639 dev_dbg(swrm->dev,
1640 "%s: No change in slave status: %d\n",
1641 __func__, status);
1642 break;
1643 }
1644 chg_sts = swrm_check_slave_change_status(swrm, status,
1645 &devnum);
1646 switch (chg_sts) {
1647 case SWR_NOT_PRESENT:
1648 dev_dbg(swrm->dev, "device %d got detached\n",
1649 devnum);
1650 break;
1651 case SWR_ATTACHED_OK:
1652 dev_dbg(swrm->dev, "device %d got attached\n",
1653 devnum);
Ramprasad Katkamdebe8932018-09-25 18:08:18 +05301654 /* enable host irq from slave device*/
1655 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1656 SWRS_SCP_INT_STATUS_CLEAR_1);
1657 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1658 SWRS_SCP_INT_STATUS_MASK_1);
1659
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301660 break;
1661 case SWR_ALERT:
1662 dev_dbg(swrm->dev,
1663 "device %d has pending interrupt\n",
1664 devnum);
1665 break;
1666 }
1667 break;
1668 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1669 dev_err_ratelimited(swrm->dev,
1670 "SWR bus clsh detected\n");
1671 break;
1672 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1673 dev_dbg(swrm->dev, "SWR read FIFO overflow\n");
1674 break;
1675 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1676 dev_dbg(swrm->dev, "SWR read FIFO underflow\n");
1677 break;
1678 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1679 dev_dbg(swrm->dev, "SWR write FIFO overflow\n");
1680 break;
1681 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1682 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1683 dev_err_ratelimited(swrm->dev,
1684 "SWR CMD error, fifo status 0x%x, flushing fifo\n",
1685 value);
1686 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1687 break;
1688 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301689 dev_err_ratelimited(swrm->dev, "SWR Port collision detected\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301690 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301691 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301692 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301693 break;
1694 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1695 dev_dbg(swrm->dev, "SWR read enable valid mismatch\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301696 swrm->intr_mask &=
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301697 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1698 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301699 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301700 break;
1701 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1702 complete(&swrm->broadcast);
1703 dev_dbg(swrm->dev, "SWR cmd id finished\n");
1704 break;
1705 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_AUTO_ENUM_FINISHED:
1706 break;
1707 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED:
1708 break;
1709 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL:
1710 break;
1711 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED:
1712 complete(&swrm->reset);
1713 break;
1714 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED:
1715 break;
1716 default:
1717 dev_err_ratelimited(swrm->dev,
1718 "SWR unknown interrupt\n");
1719 ret = IRQ_NONE;
1720 break;
1721 }
1722 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301723 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1724 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
Ramprasad Katkam83303512018-10-11 17:34:22 +05301725
1726 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301727 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301728
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301729 if (intr_sts_masked) {
Ramprasad Katkam83303512018-10-11 17:34:22 +05301730 dev_dbg(swrm->dev, "%s: new interrupt received\n", __func__);
1731 goto handle_irq;
1732 }
1733
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301734 mutex_lock(&swrm->reslock);
1735 swrm_clk_request(swrm, false);
1736 mutex_unlock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301737exit:
Ramprasad Katkam57349872018-11-11 18:34:57 +05301738 swrm_unlock_sleep(swrm);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301739 return ret;
1740}
1741
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301742static irqreturn_t swr_mstr_interrupt_v2(int irq, void *dev)
1743{
1744 struct swr_mstr_ctrl *swrm = dev;
1745 u32 value, intr_sts, intr_sts_masked;
1746 u32 temp = 0;
1747 u32 status, chg_sts, i;
1748 u8 devnum = 0;
1749 int ret = IRQ_HANDLED;
1750 struct swr_device *swr_dev;
1751 struct swr_master *mstr = &swrm->master;
1752
1753 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1754 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1755 return IRQ_NONE;
1756 }
1757
1758 mutex_lock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301759 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
1760 ret = IRQ_NONE;
1761 goto exit;
1762 }
1763 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
1764 ret = IRQ_NONE;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301765 goto err_audio_hw_vote;
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001766 }
Karthikeyan Mani4bee1db2019-09-18 17:58:41 -07001767 ret = swrm_clk_request(swrm, true);
1768 if (ret) {
1769 dev_err(dev, "%s: swrm clk failed\n", __func__);
1770 ret = IRQ_NONE;
1771 goto err_audio_core_vote;
1772 }
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301773 mutex_unlock(&swrm->reslock);
1774
1775 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1776 intr_sts_masked = intr_sts & swrm->intr_mask;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301777
1778 dev_dbg(swrm->dev, "%s: status: 0x%x \n", __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301779handle_irq:
1780 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
1781 value = intr_sts_masked & (1 << i);
1782 if (!value)
1783 continue;
1784
1785 switch (value) {
1786 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1787 dev_dbg(swrm->dev, "%s: Trigger irq to slave device\n",
1788 __func__);
1789 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1790 ret = swrm_find_alert_slave(swrm, status, &devnum);
1791 if (ret) {
1792 dev_err_ratelimited(swrm->dev,
1793 "%s: no slave alert found.spurious interrupt\n",
1794 __func__);
1795 break;
1796 }
1797 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1798 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1799 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1800 SWRS_SCP_INT_STATUS_CLEAR_1);
1801 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1802 SWRS_SCP_INT_STATUS_CLEAR_1);
1803
1804
1805 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1806 if (swr_dev->dev_num != devnum)
1807 continue;
1808 if (swr_dev->slave_irq) {
1809 do {
1810 handle_nested_irq(
1811 irq_find_mapping(
1812 swr_dev->slave_irq, 0));
1813 } while (swr_dev->slave_irq_pending);
1814 }
1815
1816 }
1817 break;
1818 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1819 dev_dbg(swrm->dev, "%s: SWR new slave attached\n",
1820 __func__);
1821 break;
1822 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1823 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
Laxminath Kasam44cedb82019-11-20 17:37:23 +05301824 swrm_enable_slave_irq(swrm);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301825 if (status == swrm->slave_status) {
1826 dev_dbg(swrm->dev,
1827 "%s: No change in slave status: %d\n",
1828 __func__, status);
1829 break;
1830 }
1831 chg_sts = swrm_check_slave_change_status(swrm, status,
1832 &devnum);
1833 switch (chg_sts) {
1834 case SWR_NOT_PRESENT:
1835 dev_dbg(swrm->dev,
1836 "%s: device %d got detached\n",
1837 __func__, devnum);
1838 break;
1839 case SWR_ATTACHED_OK:
1840 dev_dbg(swrm->dev,
1841 "%s: device %d got attached\n",
1842 __func__, devnum);
1843 /* enable host irq from slave device*/
1844 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1845 SWRS_SCP_INT_STATUS_CLEAR_1);
1846 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1847 SWRS_SCP_INT_STATUS_MASK_1);
1848
1849 break;
1850 case SWR_ALERT:
1851 dev_dbg(swrm->dev,
1852 "%s: device %d has pending interrupt\n",
1853 __func__, devnum);
1854 break;
1855 }
1856 break;
1857 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1858 dev_err_ratelimited(swrm->dev,
1859 "%s: SWR bus clsh detected\n",
1860 __func__);
1861 break;
1862 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1863 dev_dbg(swrm->dev, "%s: SWR read FIFO overflow\n",
1864 __func__);
1865 break;
1866 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1867 dev_dbg(swrm->dev, "%s: SWR read FIFO underflow\n",
1868 __func__);
1869 break;
1870 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1871 dev_dbg(swrm->dev, "%s: SWR write FIFO overflow\n",
1872 __func__);
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301873 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301874 break;
1875 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1876 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1877 dev_err_ratelimited(swrm->dev,
1878 "%s: SWR CMD error, fifo status 0x%x, flushing fifo\n",
1879 __func__, value);
1880 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1881 break;
1882 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
1883 dev_err_ratelimited(swrm->dev,
1884 "%s: SWR Port collision detected\n",
1885 __func__);
1886 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
1887 swr_master_write(swrm,
1888 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1889 break;
1890 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1891 dev_dbg(swrm->dev,
1892 "%s: SWR read enable valid mismatch\n",
1893 __func__);
1894 swrm->intr_mask &=
1895 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1896 swr_master_write(swrm,
1897 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1898 break;
1899 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1900 complete(&swrm->broadcast);
1901 dev_dbg(swrm->dev, "%s: SWR cmd id finished\n",
1902 __func__);
1903 break;
1904 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED_V2:
1905 break;
1906 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL_V2:
1907 break;
1908 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED_V2:
Laxminath Kasame2291972019-11-08 14:51:59 +05301909 swrm_check_link_status(swrm, 0x1);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301910 break;
1911 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED_V2:
1912 break;
1913 case SWRM_INTERRUPT_STATUS_EXT_CLK_STOP_WAKEUP:
1914 if (swrm->state == SWR_MSTR_UP)
1915 dev_dbg(swrm->dev,
1916 "%s:SWR Master is already up\n",
1917 __func__);
1918 else
1919 dev_err_ratelimited(swrm->dev,
1920 "%s: SWR wokeup during clock stop\n",
1921 __func__);
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301922 /* It might be possible the slave device gets reset
1923 * and slave interrupt gets missed. So re-enable
1924 * Host IRQ and process slave pending
1925 * interrupts, if any.
1926 */
1927 swrm_enable_slave_irq(swrm);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301928 break;
1929 default:
1930 dev_err_ratelimited(swrm->dev,
1931 "%s: SWR unknown interrupt value: %d\n",
1932 __func__, value);
1933 ret = IRQ_NONE;
1934 break;
1935 }
1936 }
1937 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1938 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
1939
1940 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1941 intr_sts_masked = intr_sts & swrm->intr_mask;
1942
1943 if (intr_sts_masked) {
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301944 dev_dbg(swrm->dev, "%s: new interrupt received 0x%x\n",
1945 __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301946 goto handle_irq;
1947 }
1948
1949 mutex_lock(&swrm->reslock);
1950 swrm_clk_request(swrm, false);
Karthikeyan Mani4bee1db2019-09-18 17:58:41 -07001951err_audio_core_vote:
Sudheer Papothi384addd2019-06-14 02:26:52 +05301952 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
Sudheer Papothi06f43412019-07-09 03:32:54 +05301953
1954err_audio_hw_vote:
Sudheer Papothi384addd2019-06-14 02:26:52 +05301955 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001956exit:
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301957 mutex_unlock(&swrm->reslock);
1958 swrm_unlock_sleep(swrm);
1959 return ret;
1960}
1961
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301962static irqreturn_t swrm_wakeup_interrupt(int irq, void *dev)
1963{
1964 struct swr_mstr_ctrl *swrm = dev;
1965 int ret = IRQ_HANDLED;
1966
1967 if (!swrm || !(swrm->dev)) {
1968 pr_err("%s: swrm or dev is null\n", __func__);
1969 return IRQ_NONE;
1970 }
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05301971
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301972 mutex_lock(&swrm->devlock);
1973 if (!swrm->dev_up) {
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05301974 if (swrm->wake_irq > 0) {
1975 if (unlikely(!irq_get_irq_data(swrm->wake_irq))) {
1976 pr_err("%s: irq data is NULL\n", __func__);
1977 mutex_unlock(&swrm->devlock);
1978 return IRQ_NONE;
1979 }
1980 mutex_lock(&swrm->irq_lock);
1981 if (!irqd_irq_disabled(
1982 irq_get_irq_data(swrm->wake_irq)))
1983 disable_irq_nosync(swrm->wake_irq);
1984 mutex_unlock(&swrm->irq_lock);
1985 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301986 mutex_unlock(&swrm->devlock);
1987 return ret;
1988 }
1989 mutex_unlock(&swrm->devlock);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301990 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1991 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1992 goto exit;
1993 }
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05301994 if (swrm->wake_irq > 0) {
1995 if (unlikely(!irq_get_irq_data(swrm->wake_irq))) {
1996 pr_err("%s: irq data is NULL\n", __func__);
1997 return IRQ_NONE;
1998 }
1999 mutex_lock(&swrm->irq_lock);
2000 if (!irqd_irq_disabled(
2001 irq_get_irq_data(swrm->wake_irq)))
2002 disable_irq_nosync(swrm->wake_irq);
2003 mutex_unlock(&swrm->irq_lock);
2004 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302005 pm_runtime_get_sync(swrm->dev);
2006 pm_runtime_mark_last_busy(swrm->dev);
2007 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05302008 swrm_unlock_sleep(swrm);
2009exit:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302010 return ret;
2011}
2012
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302013static void swrm_wakeup_work(struct work_struct *work)
2014{
2015 struct swr_mstr_ctrl *swrm;
2016
2017 swrm = container_of(work, struct swr_mstr_ctrl,
2018 wakeup_work);
2019 if (!swrm || !(swrm->dev)) {
2020 pr_err("%s: swrm or dev is null\n", __func__);
2021 return;
2022 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302023
2024 mutex_lock(&swrm->devlock);
2025 if (!swrm->dev_up) {
2026 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302027 goto exit;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302028 }
2029 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302030 if (unlikely(swrm_lock_sleep(swrm) == false)) {
2031 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
2032 goto exit;
2033 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302034 pm_runtime_get_sync(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302035 pm_runtime_mark_last_busy(swrm->dev);
2036 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302037 swrm_unlock_sleep(swrm);
2038exit:
2039 pm_relax(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302040}
2041
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302042static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
2043{
2044 u32 val;
2045
2046 swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
2047 val = (swrm->slave_status >> (devnum * 2));
2048 val &= SWRM_MCP_SLV_STATUS_MASK;
2049 return val;
2050}
2051
2052static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
2053 u8 *dev_num)
2054{
2055 int i;
2056 u64 id = 0;
2057 int ret = -EINVAL;
2058 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
2059 struct swr_device *swr_dev;
2060 u32 num_dev = 0;
2061
2062 if (!swrm) {
2063 pr_err("%s: Invalid handle to swr controller\n",
2064 __func__);
2065 return ret;
2066 }
2067 if (swrm->num_dev)
2068 num_dev = swrm->num_dev;
2069 else
2070 num_dev = mstr->num_dev;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302071
2072 mutex_lock(&swrm->devlock);
2073 if (!swrm->dev_up) {
2074 mutex_unlock(&swrm->devlock);
2075 return ret;
2076 }
2077 mutex_unlock(&swrm->devlock);
2078
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302079 pm_runtime_get_sync(swrm->dev);
2080 for (i = 1; i < (num_dev + 1); i++) {
2081 id = ((u64)(swr_master_read(swrm,
2082 SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
2083 id |= swr_master_read(swrm,
2084 SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302085
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302086 /*
2087 * As pm_runtime_get_sync() brings all slaves out of reset
2088 * update logical device number for all slaves.
2089 */
2090 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2091 if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
2092 u32 status = swrm_get_device_status(swrm, i);
2093
2094 if ((status == 0x01) || (status == 0x02)) {
2095 swr_dev->dev_num = i;
2096 if ((id & SWR_DEV_ID_MASK) == dev_id) {
2097 *dev_num = i;
2098 ret = 0;
2099 }
2100 dev_dbg(swrm->dev,
2101 "%s: devnum %d is assigned for dev addr %lx\n",
2102 __func__, i, swr_dev->addr);
2103 }
2104 }
2105 }
2106 }
2107 if (ret)
2108 dev_err(swrm->dev, "%s: device 0x%llx is not ready\n",
2109 __func__, dev_id);
2110
2111 pm_runtime_mark_last_busy(swrm->dev);
2112 pm_runtime_put_autosuspend(swrm->dev);
2113 return ret;
2114}
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302115
2116static void swrm_device_wakeup_vote(struct swr_master *mstr)
2117{
2118 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
2119
2120 if (!swrm) {
2121 pr_err("%s: Invalid handle to swr controller\n",
2122 __func__);
2123 return;
2124 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302125 if (unlikely(swrm_lock_sleep(swrm) == false)) {
2126 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
2127 return;
2128 }
Sudheer Papothi384addd2019-06-14 02:26:52 +05302129 if (++swrm->hw_core_clk_en == 1)
2130 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2131 dev_err(swrm->dev, "%s:lpass core hw enable failed\n",
2132 __func__);
2133 --swrm->hw_core_clk_en;
2134 }
2135 if ( ++swrm->aud_core_clk_en == 1)
2136 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2137 dev_err(swrm->dev, "%s:lpass audio hw enable failed\n",
2138 __func__);
2139 --swrm->aud_core_clk_en;
2140 }
2141 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
2142 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302143 pm_runtime_get_sync(swrm->dev);
2144}
2145
2146static void swrm_device_wakeup_unvote(struct swr_master *mstr)
2147{
2148 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
2149
2150 if (!swrm) {
2151 pr_err("%s: Invalid handle to swr controller\n",
2152 __func__);
2153 return;
2154 }
2155 pm_runtime_mark_last_busy(swrm->dev);
2156 pm_runtime_put_autosuspend(swrm->dev);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302157 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
2158 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
2159
2160 --swrm->aud_core_clk_en;
2161 if (swrm->aud_core_clk_en < 0)
2162 swrm->aud_core_clk_en = 0;
2163 else if (swrm->aud_core_clk_en == 0)
2164 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2165
2166 --swrm->hw_core_clk_en;
2167 if (swrm->hw_core_clk_en < 0)
2168 swrm->hw_core_clk_en = 0;
2169 else if (swrm->hw_core_clk_en == 0)
2170 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
2171
Ramprasad Katkam57349872018-11-11 18:34:57 +05302172 swrm_unlock_sleep(swrm);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302173}
2174
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302175static int swrm_master_init(struct swr_mstr_ctrl *swrm)
2176{
2177 int ret = 0;
2178 u32 val;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302179 u8 row_ctrl = SWR_ROW_50;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302180 u8 col_ctrl = SWR_MIN_COL;
2181 u8 ssp_period = 1;
2182 u8 retry_cmd_num = 3;
2183 u32 reg[SWRM_MAX_INIT_REG];
2184 u32 value[SWRM_MAX_INIT_REG];
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302185 u32 temp = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302186 int len = 0;
2187
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302188 ssp_period = swrm_get_ssp_period(swrm, SWRM_ROW_50,
2189 SWRM_COL_02, SWRM_FRAME_SYNC_SEL);
2190 dev_dbg(swrm->dev, "%s: ssp_period: %d\n", __func__, ssp_period);
2191
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302192 /* Clear Rows and Cols */
2193 val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
2194 (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302195 ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302196
2197 reg[len] = SWRM_MCP_FRAME_CTRL_BANK_ADDR(0);
2198 value[len++] = val;
2199
2200 /* Set Auto enumeration flag */
2201 reg[len] = SWRM_ENUMERATOR_CFG_ADDR;
2202 value[len++] = 1;
2203
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302204 /* Configure No pings */
2205 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2206 val &= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK;
2207 val |= (0x1f << SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT);
2208 reg[len] = SWRM_MCP_CFG_ADDR;
2209 value[len++] = val;
2210
2211 /* Configure number of retries of a read/write cmd */
2212 val = (retry_cmd_num << SWRM_CMD_FIFO_CFG_NUM_OF_CMD_RETRY_SHFT);
2213 reg[len] = SWRM_CMD_FIFO_CFG_ADDR;
2214 value[len++] = val;
2215
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302216 reg[len] = SWRM_MCP_BUS_CTRL_ADDR;
2217 value[len++] = 0x2;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302218
Ramprasad Katkam83303512018-10-11 17:34:22 +05302219 /* Set IRQ to PULSE */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302220 reg[len] = SWRM_COMP_CFG_ADDR;
Ramprasad Katkam83303512018-10-11 17:34:22 +05302221 value[len++] = 0x02;
2222
2223 reg[len] = SWRM_COMP_CFG_ADDR;
2224 value[len++] = 0x03;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302225
2226 reg[len] = SWRM_INTERRUPT_CLEAR;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302227 value[len++] = 0xFFFFFFFF;
2228
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302229 swrm->intr_mask = SWRM_INTERRUPT_STATUS_MASK;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302230 /* Mask soundwire interrupts */
2231 reg[len] = SWRM_INTERRUPT_MASK_ADDR;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302232 value[len++] = swrm->intr_mask;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302233
2234 reg[len] = SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302235 value[len++] = swrm->intr_mask;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302236
2237 swr_master_bulk_write(swrm, reg, value, len);
2238
Laxminath Kasamcafe0732019-11-20 17:31:58 +05302239 if (!swrm_check_link_status(swrm, 0x1)) {
2240 dev_err(swrm->dev,
2241 "%s: swr link failed to connect\n",
2242 __func__);
2243 return -EINVAL;
2244 }
Sudheer Papothi63f48152018-11-15 01:08:03 +05302245 /*
2246 * For SWR master version 1.5.1, continue
2247 * execute on command ignore.
2248 */
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302249 /* Execute it for versions >= 1.5.1 */
2250 if (swrm->version >= SWRM_VERSION_1_5_1)
Sudheer Papothi63f48152018-11-15 01:08:03 +05302251 swr_master_write(swrm, SWRM_CMD_FIFO_CFG_ADDR,
2252 (swr_master_read(swrm,
2253 SWRM_CMD_FIFO_CFG_ADDR) | 0x80000000));
2254
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302255 /* SW workaround to gate hw_ctl for SWR version >=1.6 */
2256 if (swrm->version >= SWRM_VERSION_1_6) {
2257 if (swrm->swrm_hctl_reg) {
2258 temp = ioread32(swrm->swrm_hctl_reg);
2259 temp &= 0xFFFFFFFD;
2260 iowrite32(temp, swrm->swrm_hctl_reg);
2261 }
2262 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302263 return ret;
2264}
2265
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302266static int swrm_event_notify(struct notifier_block *self,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302267 unsigned long action, void *data)
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302268{
2269 struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302270 event_notifier);
2271
2272 if (!swrm || !(swrm->dev)) {
2273 pr_err("%s: swrm or dev is NULL\n", __func__);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302274 return -EINVAL;
2275 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302276 switch (action) {
2277 case MSM_AUD_DC_EVENT:
2278 schedule_work(&(swrm->dc_presence_work));
2279 break;
2280 case SWR_WAKE_IRQ_EVENT:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302281 if (swrm->ipc_wakeup && !swrm->ipc_wakeup_triggered) {
2282 swrm->ipc_wakeup_triggered = true;
Ramprasad Katkam57349872018-11-11 18:34:57 +05302283 pm_stay_awake(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302284 schedule_work(&swrm->wakeup_work);
Ramprasad Katkamcd61c6e2018-09-18 13:22:58 +05302285 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302286 break;
2287 default:
2288 dev_err(swrm->dev, "%s: invalid event type: %lu\n",
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302289 __func__, action);
2290 return -EINVAL;
2291 }
2292
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302293 return 0;
2294}
2295
2296static void swrm_notify_work_fn(struct work_struct *work)
2297{
2298 struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302299 dc_presence_work);
2300
2301 if (!swrm || !swrm->pdev) {
2302 pr_err("%s: swrm or pdev is NULL\n", __func__);
2303 return;
2304 }
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302305 swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL);
2306}
2307
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302308static int swrm_probe(struct platform_device *pdev)
2309{
2310 struct swr_mstr_ctrl *swrm;
2311 struct swr_ctrl_platform_data *pdata;
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302312 u32 i, num_ports, port_num, port_type, ch_mask, swrm_hctl_reg = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302313 u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0;
2314 int ret = 0;
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302315 struct clk *lpass_core_hw_vote = NULL;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302316 struct clk *lpass_core_audio = NULL;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302317
2318 /* Allocate soundwire master driver structure */
2319 swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl),
2320 GFP_KERNEL);
2321 if (!swrm) {
2322 ret = -ENOMEM;
2323 goto err_memory_fail;
2324 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302325 swrm->pdev = pdev;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302326 swrm->dev = &pdev->dev;
2327 platform_set_drvdata(pdev, swrm);
2328 swr_set_ctrl_data(&swrm->master, swrm);
2329 pdata = dev_get_platdata(&pdev->dev);
2330 if (!pdata) {
2331 dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
2332 __func__);
2333 ret = -EINVAL;
2334 goto err_pdata_fail;
2335 }
2336 swrm->handle = (void *)pdata->handle;
2337 if (!swrm->handle) {
2338 dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
2339 __func__);
2340 ret = -EINVAL;
2341 goto err_pdata_fail;
2342 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302343 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr_master_id",
2344 &swrm->master_id);
2345 if (ret) {
2346 dev_err(&pdev->dev, "%s: failed to get master id\n", __func__);
2347 goto err_pdata_fail;
2348 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302349 if (!(of_property_read_u32(pdev->dev.of_node,
2350 "swrm-io-base", &swrm->swrm_base_reg)))
2351 ret = of_property_read_u32(pdev->dev.of_node,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302352 "swrm-io-base", &swrm->swrm_base_reg);
2353 if (!swrm->swrm_base_reg) {
2354 swrm->read = pdata->read;
2355 if (!swrm->read) {
2356 dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
2357 __func__);
2358 ret = -EINVAL;
2359 goto err_pdata_fail;
2360 }
2361 swrm->write = pdata->write;
2362 if (!swrm->write) {
2363 dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
2364 __func__);
2365 ret = -EINVAL;
2366 goto err_pdata_fail;
2367 }
2368 swrm->bulk_write = pdata->bulk_write;
2369 if (!swrm->bulk_write) {
2370 dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
2371 __func__);
2372 ret = -EINVAL;
2373 goto err_pdata_fail;
2374 }
2375 } else {
2376 swrm->swrm_dig_base = devm_ioremap(&pdev->dev,
2377 swrm->swrm_base_reg, SWRM_MAX_REGISTER);
2378 }
2379
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07002380 swrm->core_vote = pdata->core_vote;
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302381 if (!(of_property_read_u32(pdev->dev.of_node,
2382 "qcom,swrm-hctl-reg", &swrm_hctl_reg)))
2383 swrm->swrm_hctl_reg = devm_ioremap(&pdev->dev,
2384 swrm_hctl_reg, 0x4);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302385 swrm->clk = pdata->clk;
2386 if (!swrm->clk) {
2387 dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
2388 __func__);
2389 ret = -EINVAL;
2390 goto err_pdata_fail;
2391 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302392 if (of_property_read_u32(pdev->dev.of_node,
2393 "qcom,swr-clock-stop-mode0",
2394 &swrm->clk_stop_mode0_supp)) {
2395 swrm->clk_stop_mode0_supp = FALSE;
2396 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302397
2398 ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
2399 &swrm->num_dev);
2400 if (ret) {
2401 dev_dbg(&pdev->dev, "%s: Looking up %s property failed\n",
2402 __func__, "qcom,swr-num-dev");
2403 } else {
2404 if (swrm->num_dev > SWR_MAX_SLAVE_DEVICES) {
2405 dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
2406 __func__, swrm->num_dev, SWR_MAX_SLAVE_DEVICES);
2407 ret = -EINVAL;
2408 goto err_pdata_fail;
2409 }
2410 }
2411
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302412 /* Parse soundwire port mapping */
2413 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
2414 &num_ports);
2415 if (ret) {
2416 dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
2417 goto err_pdata_fail;
2418 }
2419 swrm->num_ports = num_ports;
2420
2421 if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
2422 &map_size)) {
2423 dev_err(swrm->dev, "missing port mapping\n");
2424 goto err_pdata_fail;
2425 }
2426
2427 map_length = map_size / (3 * sizeof(u32));
2428 if (num_ports > SWR_MSTR_PORT_LEN) {
2429 dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
2430 __func__);
2431 ret = -EINVAL;
2432 goto err_pdata_fail;
2433 }
2434 temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
2435
2436 if (!temp) {
2437 ret = -ENOMEM;
2438 goto err_pdata_fail;
2439 }
2440 ret = of_property_read_u32_array(pdev->dev.of_node,
2441 "qcom,swr-port-mapping", temp, 3 * map_length);
2442 if (ret) {
2443 dev_err(swrm->dev, "%s: Failed to read port mapping\n",
2444 __func__);
2445 goto err_pdata_fail;
2446 }
2447
2448 for (i = 0; i < map_length; i++) {
2449 port_num = temp[3 * i];
2450 port_type = temp[3 * i + 1];
2451 ch_mask = temp[3 * i + 2];
2452
2453 if (port_num != old_port_num)
2454 ch_iter = 0;
2455 swrm->port_mapping[port_num][ch_iter].port_type = port_type;
2456 swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
2457 old_port_num = port_num;
2458 }
2459 devm_kfree(&pdev->dev, temp);
2460
2461 swrm->reg_irq = pdata->reg_irq;
2462 swrm->master.read = swrm_read;
2463 swrm->master.write = swrm_write;
2464 swrm->master.bulk_write = swrm_bulk_write;
2465 swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
2466 swrm->master.connect_port = swrm_connect_port;
2467 swrm->master.disconnect_port = swrm_disconnect_port;
2468 swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
2469 swrm->master.remove_from_group = swrm_remove_from_group;
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302470 swrm->master.device_wakeup_vote = swrm_device_wakeup_vote;
2471 swrm->master.device_wakeup_unvote = swrm_device_wakeup_unvote;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302472 swrm->master.dev.parent = &pdev->dev;
2473 swrm->master.dev.of_node = pdev->dev.of_node;
2474 swrm->master.num_port = 0;
2475 swrm->rcmd_id = 0;
2476 swrm->wcmd_id = 0;
2477 swrm->slave_status = 0;
2478 swrm->num_rx_chs = 0;
2479 swrm->clk_ref_count = 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302480 swrm->swr_irq_wakeup_capable = 0;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302481 swrm->mclk_freq = MCLK_FREQ;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302482 swrm->bus_clk = MCLK_FREQ;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302483 swrm->dev_up = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302484 swrm->state = SWR_MSTR_UP;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302485 swrm->ipc_wakeup = false;
2486 swrm->ipc_wakeup_triggered = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302487 init_completion(&swrm->reset);
2488 init_completion(&swrm->broadcast);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302489 init_completion(&swrm->clk_off_complete);
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302490 mutex_init(&swrm->irq_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302491 mutex_init(&swrm->mlock);
2492 mutex_init(&swrm->reslock);
2493 mutex_init(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302494 mutex_init(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302495 mutex_init(&swrm->clklock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302496 mutex_init(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302497 mutex_init(&swrm->pm_lock);
2498 swrm->wlock_holders = 0;
2499 swrm->pm_state = SWRM_PM_SLEEPABLE;
2500 init_waitqueue_head(&swrm->pm_wq);
2501 pm_qos_add_request(&swrm->pm_qos_req,
2502 PM_QOS_CPU_DMA_LATENCY,
2503 PM_QOS_DEFAULT_VALUE);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302504
2505 for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++)
2506 INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
2507
Sudheer Papothi06f43412019-07-09 03:32:54 +05302508 /* Register LPASS core hw vote */
2509 lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
2510 if (IS_ERR(lpass_core_hw_vote)) {
2511 ret = PTR_ERR(lpass_core_hw_vote);
2512 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2513 __func__, "lpass_core_hw_vote", ret);
2514 lpass_core_hw_vote = NULL;
2515 ret = 0;
2516 }
2517 swrm->lpass_core_hw_vote = lpass_core_hw_vote;
2518
2519 /* Register LPASS audio core vote */
2520 lpass_core_audio = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
2521 if (IS_ERR(lpass_core_audio)) {
2522 ret = PTR_ERR(lpass_core_audio);
2523 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2524 __func__, "lpass_core_audio", ret);
2525 lpass_core_audio = NULL;
2526 ret = 0;
2527 }
2528 swrm->lpass_core_audio = lpass_core_audio;
2529
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302530 if (swrm->reg_irq) {
2531 ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
2532 SWR_IRQ_REGISTER);
2533 if (ret) {
2534 dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
2535 __func__, ret);
2536 goto err_irq_fail;
2537 }
2538 } else {
2539 swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
2540 if (swrm->irq < 0) {
2541 dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
2542 __func__, swrm->irq);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302543 goto err_irq_fail;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302544 }
2545
2546 ret = request_threaded_irq(swrm->irq, NULL,
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302547 swr_mstr_interrupt_v2,
Ramprasad Katkam83303512018-10-11 17:34:22 +05302548 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302549 "swr_master_irq", swrm);
2550 if (ret) {
2551 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2552 __func__, ret);
2553 goto err_irq_fail;
2554 }
2555
2556 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302557 /* Make inband tx interrupts as wakeup capable for slave irq */
2558 ret = of_property_read_u32(pdev->dev.of_node,
2559 "qcom,swr-mstr-irq-wakeup-capable",
2560 &swrm->swr_irq_wakeup_capable);
2561 if (ret)
2562 dev_dbg(swrm->dev, "%s: swrm irq wakeup capable not defined\n",
2563 __func__);
2564 if (swrm->swr_irq_wakeup_capable)
2565 irq_set_irq_wake(swrm->irq, 1);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302566 ret = swr_register_master(&swrm->master);
2567 if (ret) {
2568 dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
2569 goto err_mstr_fail;
2570 }
2571
2572 /* Add devices registered with board-info as the
2573 * controller will be up now
2574 */
2575 swr_master_add_boarddevices(&swrm->master);
2576 mutex_lock(&swrm->mlock);
2577 swrm_clk_request(swrm, true);
Laxminath Kasam4696fff2019-11-26 16:07:11 +05302578 swrm->version = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302579 ret = swrm_master_init(swrm);
2580 if (ret < 0) {
2581 dev_err(&pdev->dev,
2582 "%s: Error in master Initialization , err %d\n",
2583 __func__, ret);
2584 mutex_unlock(&swrm->mlock);
2585 goto err_mstr_fail;
2586 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302587
2588 mutex_unlock(&swrm->mlock);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302589 INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302590
2591 if (pdev->dev.of_node)
2592 of_register_swr_devices(&swrm->master);
2593
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302594#ifdef CONFIG_DEBUG_FS
2595 swrm->debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
2596 if (!IS_ERR(swrm->debugfs_swrm_dent)) {
2597 swrm->debugfs_peek = debugfs_create_file("swrm_peek",
2598 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2599 (void *) swrm, &swrm_debug_read_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302600
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302601 swrm->debugfs_poke = debugfs_create_file("swrm_poke",
2602 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2603 (void *) swrm, &swrm_debug_write_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302604
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302605 swrm->debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
2606 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2607 (void *) swrm,
2608 &swrm_debug_dump_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302609 }
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302610#endif
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302611 ret = device_init_wakeup(swrm->dev, true);
2612 if (ret) {
2613 dev_err(swrm->dev, "Device wakeup init failed: %d\n", ret);
2614 goto err_irq_wakeup_fail;
2615 }
2616
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302617 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
2618 pm_runtime_use_autosuspend(&pdev->dev);
2619 pm_runtime_set_active(&pdev->dev);
2620 pm_runtime_enable(&pdev->dev);
2621 pm_runtime_mark_last_busy(&pdev->dev);
2622
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302623 INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn);
2624 swrm->event_notifier.notifier_call = swrm_event_notify;
2625 msm_aud_evt_register_client(&swrm->event_notifier);
2626
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302627 return 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302628err_irq_wakeup_fail:
2629 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302630err_mstr_fail:
2631 if (swrm->reg_irq)
2632 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2633 swrm, SWR_IRQ_FREE);
2634 else if (swrm->irq)
2635 free_irq(swrm->irq, swrm);
2636err_irq_fail:
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302637 mutex_destroy(&swrm->irq_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302638 mutex_destroy(&swrm->mlock);
2639 mutex_destroy(&swrm->reslock);
2640 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302641 mutex_destroy(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302642 mutex_destroy(&swrm->clklock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302643 mutex_destroy(&swrm->pm_lock);
2644 pm_qos_remove_request(&swrm->pm_qos_req);
2645
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302646err_pdata_fail:
2647err_memory_fail:
2648 return ret;
2649}
2650
2651static int swrm_remove(struct platform_device *pdev)
2652{
2653 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2654
2655 if (swrm->reg_irq)
2656 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2657 swrm, SWR_IRQ_FREE);
2658 else if (swrm->irq)
2659 free_irq(swrm->irq, swrm);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302660 else if (swrm->wake_irq > 0)
2661 free_irq(swrm->wake_irq, swrm);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302662 if (swrm->swr_irq_wakeup_capable)
2663 irq_set_irq_wake(swrm->irq, 0);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302664 cancel_work_sync(&swrm->wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302665 pm_runtime_disable(&pdev->dev);
2666 pm_runtime_set_suspended(&pdev->dev);
2667 swr_unregister_master(&swrm->master);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302668 msm_aud_evt_unregister_client(&swrm->event_notifier);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302669 device_init_wakeup(swrm->dev, false);
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302670 mutex_destroy(&swrm->irq_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302671 mutex_destroy(&swrm->mlock);
2672 mutex_destroy(&swrm->reslock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302673 mutex_destroy(&swrm->iolock);
2674 mutex_destroy(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302675 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302676 mutex_destroy(&swrm->pm_lock);
2677 pm_qos_remove_request(&swrm->pm_qos_req);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302678 devm_kfree(&pdev->dev, swrm);
2679 return 0;
2680}
2681
2682static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
2683{
2684 u32 val;
2685
2686 dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
2687 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD);
2688 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2689 val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK;
2690 swr_master_write(swrm, SWRM_MCP_CFG_ADDR, val);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302691
2692 return 0;
2693}
2694
2695#ifdef CONFIG_PM
2696static int swrm_runtime_resume(struct device *dev)
2697{
2698 struct platform_device *pdev = to_platform_device(dev);
2699 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2700 int ret = 0;
Vatsal Buchae50b5002019-09-19 14:32:20 +05302701 bool swrm_clk_req_err = false;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302702 bool hw_core_err = false;
2703 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302704 struct swr_master *mstr = &swrm->master;
2705 struct swr_device *swr_dev;
2706
2707 dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
2708 __func__, swrm->state);
2709 mutex_lock(&swrm->reslock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302710
Sudheer Papothi384addd2019-06-14 02:26:52 +05302711 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2712 dev_err(dev, "%s:lpass core hw enable failed\n",
2713 __func__);
2714 hw_core_err = true;
2715 }
2716 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2717 dev_err(dev, "%s:lpass audio hw enable failed\n",
2718 __func__);
2719 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002720 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302721
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302722 if ((swrm->state == SWR_MSTR_DOWN) ||
2723 (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) {
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302724 if (swrm->clk_stop_mode0_supp) {
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302725 if (swrm->wake_irq > 0) {
2726 if (unlikely(!irq_get_irq_data
2727 (swrm->wake_irq))) {
2728 pr_err("%s: irq data is NULL\n",
2729 __func__);
2730 mutex_unlock(&swrm->reslock);
2731 return IRQ_NONE;
2732 }
2733 mutex_lock(&swrm->irq_lock);
2734 if (!irqd_irq_disabled(
2735 irq_get_irq_data(swrm->wake_irq)))
2736 disable_irq_nosync(swrm->wake_irq);
2737 mutex_unlock(&swrm->irq_lock);
2738 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302739 if (swrm->ipc_wakeup)
2740 msm_aud_evt_blocking_notifier_call_chain(
2741 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302742 }
2743
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302744 if (swrm_clk_request(swrm, true)) {
2745 /*
2746 * Set autosuspend timer to 1 for
2747 * master to enter into suspend.
2748 */
Vatsal Buchae50b5002019-09-19 14:32:20 +05302749 swrm_clk_req_err = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302750 goto exit;
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302751 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302752 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302753 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2754 ret = swr_device_up(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302755 if (ret == -ENODEV) {
2756 dev_dbg(dev,
2757 "%s slave device up not implemented\n",
2758 __func__);
2759 ret = 0;
2760 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302761 dev_err(dev,
2762 "%s: failed to wakeup swr dev %d\n",
2763 __func__, swr_dev->dev_num);
2764 swrm_clk_request(swrm, false);
2765 goto exit;
2766 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302767 }
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05302768 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2769 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2770 swrm_master_init(swrm);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302771 /* wait for hw enumeration to complete */
2772 usleep_range(100, 105);
Laxminath Kasame2291972019-11-08 14:51:59 +05302773 if (!swrm_check_link_status(swrm, 0x1))
Laxminath Kasam696b14b2019-12-03 22:07:34 +05302774 dev_dbg(dev, "%s:failed in connecting, ssr?\n",
2775 __func__);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302776 swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, 0x0,
2777 SWRS_SCP_INT_STATUS_MASK_1);
Karthikeyan Manif6821902019-05-21 17:31:24 -07002778 if (swrm->state == SWR_MSTR_SSR) {
2779 mutex_unlock(&swrm->reslock);
2780 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
2781 mutex_lock(&swrm->reslock);
2782 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302783 } else {
2784 /*wake up from clock stop*/
2785 swr_master_write(swrm, SWRM_MCP_BUS_CTRL_ADDR, 0x2);
Sudheer Papothi55fa5972019-09-28 02:50:27 +05302786 /* clear and enable bus clash interrupt */
2787 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x08);
2788 swrm->intr_mask |= 0x08;
2789 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR,
2790 swrm->intr_mask);
2791 swr_master_write(swrm,
2792 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
2793 swrm->intr_mask);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302794 usleep_range(100, 105);
Laxminath Kasame2291972019-11-08 14:51:59 +05302795 if (!swrm_check_link_status(swrm, 0x1))
Laxminath Kasam696b14b2019-12-03 22:07:34 +05302796 dev_dbg(dev, "%s:failed in connecting, ssr?\n",
2797 __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302798 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302799 swrm->state = SWR_MSTR_UP;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302800 }
2801exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302802 if (!aud_core_err)
2803 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2804 if (!hw_core_err)
2805 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Vatsal Buchae50b5002019-09-19 14:32:20 +05302806 if (swrm_clk_req_err)
2807 pm_runtime_set_autosuspend_delay(&pdev->dev,
2808 ERR_AUTO_SUSPEND_TIMER_VAL);
2809 else
2810 pm_runtime_set_autosuspend_delay(&pdev->dev,
2811 auto_suspend_timer);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302812 mutex_unlock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302813
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302814 return ret;
2815}
2816
2817static int swrm_runtime_suspend(struct device *dev)
2818{
2819 struct platform_device *pdev = to_platform_device(dev);
2820 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2821 int ret = 0;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302822 bool hw_core_err = false;
2823 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302824 struct swr_master *mstr = &swrm->master;
2825 struct swr_device *swr_dev;
2826 int current_state = 0;
2827
2828 dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
2829 __func__, swrm->state);
2830 mutex_lock(&swrm->reslock);
2831 mutex_lock(&swrm->force_down_lock);
2832 current_state = swrm->state;
2833 mutex_unlock(&swrm->force_down_lock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302834
2835 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2836 dev_err(dev, "%s:lpass core hw enable failed\n",
2837 __func__);
2838 hw_core_err = true;
2839 }
2840 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2841 dev_err(dev, "%s:lpass audio hw enable failed\n",
2842 __func__);
2843 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002844 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302845
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302846 if ((current_state == SWR_MSTR_UP) ||
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302847 (current_state == SWR_MSTR_SSR)) {
2848
2849 if ((current_state != SWR_MSTR_SSR) &&
2850 swrm_is_port_en(&swrm->master)) {
2851 dev_dbg(dev, "%s ports are enabled\n", __func__);
2852 ret = -EBUSY;
2853 goto exit;
2854 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302855 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05302856 mutex_unlock(&swrm->reslock);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +05302857 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
Sudheer Papothi06f43412019-07-09 03:32:54 +05302858 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302859 swrm_clk_pause(swrm);
2860 swr_master_write(swrm, SWRM_COMP_CFG_ADDR, 0x00);
2861 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2862 ret = swr_device_down(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302863 if (ret == -ENODEV) {
2864 dev_dbg_ratelimited(dev,
2865 "%s slave device down not implemented\n",
2866 __func__);
2867 ret = 0;
2868 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302869 dev_err(dev,
2870 "%s: failed to shutdown swr dev %d\n",
2871 __func__, swr_dev->dev_num);
2872 goto exit;
2873 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302874 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302875 } else {
Sudheer Papothi55fa5972019-09-28 02:50:27 +05302876 /* Mask bus clash interrupt */
2877 swrm->intr_mask &= ~((u32)0x08);
2878 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR,
2879 swrm->intr_mask);
2880 swr_master_write(swrm,
2881 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
2882 swrm->intr_mask);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302883 mutex_unlock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302884 /* clock stop sequence */
2885 swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
2886 SWRS_SCP_CONTROL);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302887 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302888 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302889 }
Laxminath Kasamcafe0732019-11-20 17:31:58 +05302890 if (!swrm_check_link_status(swrm, 0x0))
Laxminath Kasam696b14b2019-12-03 22:07:34 +05302891 dev_dbg(dev, "%s:failed in disconnecting, ssr?\n",
2892 __func__);
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07002893 ret = swrm_clk_request(swrm, false);
2894 if (ret) {
2895 dev_err(dev, "%s: swrmn clk failed\n", __func__);
2896 ret = 0;
2897 goto exit;
2898 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302899
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302900 if (swrm->clk_stop_mode0_supp) {
2901 if (swrm->wake_irq > 0) {
2902 enable_irq(swrm->wake_irq);
2903 } else if (swrm->ipc_wakeup) {
2904 msm_aud_evt_blocking_notifier_call_chain(
2905 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
2906 swrm->ipc_wakeup_triggered = false;
2907 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302908 }
2909
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302910 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302911 /* Retain SSR state until resume */
2912 if (current_state != SWR_MSTR_SSR)
2913 swrm->state = SWR_MSTR_DOWN;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302914exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302915 if (!aud_core_err)
2916 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2917 if (!hw_core_err)
2918 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302919 mutex_unlock(&swrm->reslock);
2920 return ret;
2921}
2922#endif /* CONFIG_PM */
2923
Sudheer Papothi06f43412019-07-09 03:32:54 +05302924static int swrm_device_suspend(struct device *dev)
2925{
2926 struct platform_device *pdev = to_platform_device(dev);
2927 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2928 int ret = 0;
2929
2930 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2931 if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
2932 ret = swrm_runtime_suspend(dev);
2933 if (!ret) {
2934 pm_runtime_disable(dev);
2935 pm_runtime_set_suspended(dev);
2936 pm_runtime_enable(dev);
2937 }
2938 }
2939
2940 return 0;
2941}
2942
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302943static int swrm_device_down(struct device *dev)
2944{
2945 struct platform_device *pdev = to_platform_device(dev);
2946 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302947
2948 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2949
2950 mutex_lock(&swrm->force_down_lock);
2951 swrm->state = SWR_MSTR_SSR;
2952 mutex_unlock(&swrm->force_down_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302953
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302954 swrm_device_suspend(dev);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302955 return 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302956}
2957
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302958int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm)
2959{
2960 int ret = 0;
Laxminath Kasama60239e2019-01-10 14:43:03 +05302961 int irq, dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302962
2963 if (!swrm->ipc_wakeup) {
Laxminath Kasama60239e2019-01-10 14:43:03 +05302964 irq = of_get_named_gpio(swrm->dev->of_node,
2965 "qcom,swr-wakeup-irq", 0);
2966 if (gpio_is_valid(irq)) {
2967 swrm->wake_irq = gpio_to_irq(irq);
2968 if (swrm->wake_irq < 0) {
2969 dev_err(swrm->dev,
2970 "Unable to configure irq\n");
2971 return swrm->wake_irq;
2972 }
2973 } else {
2974 dir_apps_irq = platform_get_irq_byname(swrm->pdev,
2975 "swr_wake_irq");
2976 if (dir_apps_irq < 0) {
2977 dev_err(swrm->dev,
2978 "TLMM connect gpio not found\n");
2979 return -EINVAL;
2980 }
2981 swrm->wake_irq = dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302982 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302983 ret = request_threaded_irq(swrm->wake_irq, NULL,
2984 swrm_wakeup_interrupt,
2985 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
2986 "swr_wake_irq", swrm);
2987 if (ret) {
2988 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2989 __func__, ret);
2990 return -EINVAL;
2991 }
Aditya Bavanari3517b112018-12-03 13:26:59 +05302992 irq_set_irq_wake(swrm->wake_irq, 1);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302993 }
2994 return ret;
2995}
2996
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302997static int swrm_alloc_port_mem(struct device *dev, struct swr_mstr_ctrl *swrm,
2998 u32 uc, u32 size)
2999{
3000 if (!swrm->port_param) {
3001 swrm->port_param = devm_kzalloc(dev,
3002 sizeof(swrm->port_param) * SWR_UC_MAX,
3003 GFP_KERNEL);
3004 if (!swrm->port_param)
3005 return -ENOMEM;
3006 }
3007 if (!swrm->port_param[uc]) {
3008 swrm->port_param[uc] = devm_kcalloc(dev, size,
3009 sizeof(struct port_params),
3010 GFP_KERNEL);
3011 if (!swrm->port_param[uc])
3012 return -ENOMEM;
3013 } else {
3014 dev_err_ratelimited(swrm->dev, "%s: called more than once\n",
3015 __func__);
3016 }
3017
3018 return 0;
3019}
3020
3021static int swrm_copy_port_config(struct swr_mstr_ctrl *swrm,
3022 struct swrm_port_config *port_cfg,
3023 u32 size)
3024{
3025 int idx;
3026 struct port_params *params;
3027 int uc = port_cfg->uc;
3028 int ret = 0;
3029
3030 for (idx = 0; idx < size; idx++) {
3031 params = &((struct port_params *)port_cfg->params)[idx];
3032 if (!params) {
3033 dev_err(swrm->dev, "%s: Invalid params\n", __func__);
3034 ret = -EINVAL;
3035 break;
3036 }
3037 memcpy(&swrm->port_param[uc][idx], params,
3038 sizeof(struct port_params));
3039 }
3040
3041 return ret;
3042}
3043
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303044/**
3045 * swrm_wcd_notify - parent device can notify to soundwire master through
3046 * this function
3047 * @pdev: pointer to platform device structure
3048 * @id: command id from parent to the soundwire master
3049 * @data: data from parent device to soundwire master
3050 */
3051int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
3052{
3053 struct swr_mstr_ctrl *swrm;
3054 int ret = 0;
3055 struct swr_master *mstr;
3056 struct swr_device *swr_dev;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05303057 struct swrm_port_config *port_cfg;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303058
3059 if (!pdev) {
3060 pr_err("%s: pdev is NULL\n", __func__);
3061 return -EINVAL;
3062 }
3063 swrm = platform_get_drvdata(pdev);
3064 if (!swrm) {
3065 dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__);
3066 return -EINVAL;
3067 }
3068 mstr = &swrm->master;
3069
3070 switch (id) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05303071 case SWR_REQ_CLK_SWITCH:
3072 /* This will put soundwire in clock stop mode and disable the
3073 * clocks, if there is no active usecase running, so that the
3074 * next activity on soundwire will request clock from new clock
3075 * source.
3076 */
3077 mutex_lock(&swrm->mlock);
3078 if (swrm->state == SWR_MSTR_UP)
3079 swrm_device_suspend(&pdev->dev);
3080 mutex_unlock(&swrm->mlock);
3081 break;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05303082 case SWR_CLK_FREQ:
3083 if (!data) {
3084 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
3085 ret = -EINVAL;
3086 } else {
3087 mutex_lock(&swrm->mlock);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05303088 if (swrm->mclk_freq != *(int *)data) {
3089 dev_dbg(swrm->dev, "%s: freq change: force mstr down\n", __func__);
3090 if (swrm->state == SWR_MSTR_DOWN)
3091 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
3092 __func__, swrm->state);
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +05303093 else {
3094 swrm->mclk_freq = *(int *)data;
3095 swrm->bus_clk = swrm->mclk_freq;
3096 swrm_switch_frame_shape(swrm,
3097 swrm->bus_clk);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05303098 swrm_device_suspend(&pdev->dev);
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +05303099 }
Prasad Kumpatla386df4e2019-10-11 18:23:16 +05303100 /*
3101 * add delay to ensure clk release happen
3102 * if interrupt triggered for clk stop,
3103 * wait for it to exit
3104 */
3105 usleep_range(10000, 10500);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05303106 }
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05303107 swrm->mclk_freq = *(int *)data;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05303108 swrm->bus_clk = swrm->mclk_freq;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05303109 mutex_unlock(&swrm->mlock);
3110 }
3111 break;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303112 case SWR_DEVICE_SSR_DOWN:
3113 mutex_lock(&swrm->devlock);
3114 swrm->dev_up = false;
3115 mutex_unlock(&swrm->devlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05303116 mutex_lock(&swrm->reslock);
3117 swrm->state = SWR_MSTR_SSR;
3118 mutex_unlock(&swrm->reslock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303119 break;
3120 case SWR_DEVICE_SSR_UP:
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05303121 /* wait for clk voting to be zero */
Ramprasad Katkam7f6462e2018-11-06 11:51:22 +05303122 reinit_completion(&swrm->clk_off_complete);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05303123 if (swrm->clk_ref_count &&
3124 !wait_for_completion_timeout(&swrm->clk_off_complete,
Ramprasad Katkamc87efeb2018-12-12 19:26:19 +05303125 msecs_to_jiffies(500)))
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05303126 dev_err(swrm->dev, "%s: clock voting not zero\n",
3127 __func__);
3128
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303129 mutex_lock(&swrm->devlock);
3130 swrm->dev_up = true;
3131 mutex_unlock(&swrm->devlock);
3132 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303133 case SWR_DEVICE_DOWN:
3134 dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
3135 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05303136 if (swrm->state == SWR_MSTR_DOWN)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303137 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
3138 __func__, swrm->state);
3139 else
3140 swrm_device_down(&pdev->dev);
3141 mutex_unlock(&swrm->mlock);
3142 break;
3143 case SWR_DEVICE_UP:
3144 dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
Ramprasad Katkam0fed92f2018-11-08 14:22:22 +05303145 mutex_lock(&swrm->devlock);
3146 if (!swrm->dev_up) {
3147 dev_dbg(swrm->dev, "SSR not complete yet\n");
3148 mutex_unlock(&swrm->devlock);
3149 return -EBUSY;
3150 }
3151 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303152 mutex_lock(&swrm->mlock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303153 pm_runtime_mark_last_busy(&pdev->dev);
3154 pm_runtime_get_sync(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303155 mutex_lock(&swrm->reslock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303156 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
3157 ret = swr_reset_device(swr_dev);
3158 if (ret) {
3159 dev_err(swrm->dev,
3160 "%s: failed to reset swr device %d\n",
3161 __func__, swr_dev->dev_num);
3162 swrm_clk_request(swrm, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303163 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303164 }
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303165 pm_runtime_mark_last_busy(&pdev->dev);
3166 pm_runtime_put_autosuspend(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303167 mutex_unlock(&swrm->reslock);
3168 mutex_unlock(&swrm->mlock);
3169 break;
3170 case SWR_SET_NUM_RX_CH:
3171 if (!data) {
3172 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
3173 ret = -EINVAL;
3174 } else {
3175 mutex_lock(&swrm->mlock);
3176 swrm->num_rx_chs = *(int *)data;
3177 if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
3178 list_for_each_entry(swr_dev, &mstr->devices,
3179 dev_list) {
3180 ret = swr_set_device_group(swr_dev,
3181 SWR_BROADCAST);
3182 if (ret)
3183 dev_err(swrm->dev,
3184 "%s: set num ch failed\n",
3185 __func__);
3186 }
3187 } else {
3188 list_for_each_entry(swr_dev, &mstr->devices,
3189 dev_list) {
3190 ret = swr_set_device_group(swr_dev,
3191 SWR_GROUP_NONE);
3192 if (ret)
3193 dev_err(swrm->dev,
3194 "%s: set num ch failed\n",
3195 __func__);
3196 }
3197 }
3198 mutex_unlock(&swrm->mlock);
3199 }
3200 break;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303201 case SWR_REGISTER_WAKE_IRQ:
3202 if (!data) {
3203 dev_err(swrm->dev, "%s: reg wake irq data is NULL\n",
3204 __func__);
3205 ret = -EINVAL;
3206 } else {
3207 mutex_lock(&swrm->mlock);
3208 swrm->ipc_wakeup = *(u32 *)data;
3209 ret = swrm_register_wake_irq(swrm);
3210 if (ret)
3211 dev_err(swrm->dev, "%s: register wake_irq failed\n",
3212 __func__);
3213 mutex_unlock(&swrm->mlock);
3214 }
3215 break;
Sudheer Papothi72ee2642019-08-08 05:15:17 +05303216 case SWR_REGISTER_WAKEUP:
3217 msm_aud_evt_blocking_notifier_call_chain(
3218 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
3219 break;
3220 case SWR_DEREGISTER_WAKEUP:
3221 msm_aud_evt_blocking_notifier_call_chain(
3222 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
3223 break;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05303224 case SWR_SET_PORT_MAP:
3225 if (!data) {
3226 dev_err(swrm->dev, "%s: data is NULL for id=%d\n",
3227 __func__, id);
3228 ret = -EINVAL;
3229 } else {
3230 mutex_lock(&swrm->mlock);
3231 port_cfg = (struct swrm_port_config *)data;
3232 if (!port_cfg->size) {
3233 ret = -EINVAL;
3234 goto done;
3235 }
3236 ret = swrm_alloc_port_mem(&pdev->dev, swrm,
3237 port_cfg->uc, port_cfg->size);
3238 if (!ret)
3239 swrm_copy_port_config(swrm, port_cfg,
3240 port_cfg->size);
3241done:
3242 mutex_unlock(&swrm->mlock);
3243 }
3244 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303245 default:
3246 dev_err(swrm->dev, "%s: swr master unknown id %d\n",
3247 __func__, id);
3248 break;
3249 }
3250 return ret;
3251}
3252EXPORT_SYMBOL(swrm_wcd_notify);
3253
Ramprasad Katkam57349872018-11-11 18:34:57 +05303254/*
3255 * swrm_pm_cmpxchg:
3256 * Check old state and exchange with pm new state
3257 * if old state matches with current state
3258 *
3259 * @swrm: pointer to wcd core resource
3260 * @o: pm old state
3261 * @n: pm new state
3262 *
3263 * Returns old state
3264 */
3265static enum swrm_pm_state swrm_pm_cmpxchg(
3266 struct swr_mstr_ctrl *swrm,
3267 enum swrm_pm_state o,
3268 enum swrm_pm_state n)
3269{
3270 enum swrm_pm_state old;
3271
3272 if (!swrm)
3273 return o;
3274
3275 mutex_lock(&swrm->pm_lock);
3276 old = swrm->pm_state;
3277 if (old == o)
3278 swrm->pm_state = n;
3279 mutex_unlock(&swrm->pm_lock);
3280
3281 return old;
3282}
3283
3284static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm)
3285{
3286 enum swrm_pm_state os;
3287
3288 /*
3289 * swrm_{lock/unlock}_sleep will be called by swr irq handler
3290 * and slave wake up requests..
3291 *
3292 * If system didn't resume, we can simply return false so
3293 * IRQ handler can return without handling IRQ.
3294 */
3295 mutex_lock(&swrm->pm_lock);
3296 if (swrm->wlock_holders++ == 0) {
3297 dev_dbg(swrm->dev, "%s: holding wake lock\n", __func__);
3298 pm_qos_update_request(&swrm->pm_qos_req,
3299 msm_cpuidle_get_deep_idle_latency());
3300 pm_stay_awake(swrm->dev);
3301 }
3302 mutex_unlock(&swrm->pm_lock);
3303
3304 if (!wait_event_timeout(swrm->pm_wq,
3305 ((os = swrm_pm_cmpxchg(swrm,
3306 SWRM_PM_SLEEPABLE,
3307 SWRM_PM_AWAKE)) ==
3308 SWRM_PM_SLEEPABLE ||
3309 (os == SWRM_PM_AWAKE)),
3310 msecs_to_jiffies(
3311 SWRM_SYSTEM_RESUME_TIMEOUT_MS))) {
3312 dev_err(swrm->dev, "%s: system didn't resume within %dms, s %d, w %d\n",
3313 __func__, SWRM_SYSTEM_RESUME_TIMEOUT_MS, swrm->pm_state,
3314 swrm->wlock_holders);
3315 swrm_unlock_sleep(swrm);
3316 return false;
3317 }
3318 wake_up_all(&swrm->pm_wq);
3319 return true;
3320}
3321
3322static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm)
3323{
3324 mutex_lock(&swrm->pm_lock);
3325 if (--swrm->wlock_holders == 0) {
3326 dev_dbg(swrm->dev, "%s: releasing wake lock pm_state %d -> %d\n",
3327 __func__, swrm->pm_state, SWRM_PM_SLEEPABLE);
3328 /*
3329 * if swrm_lock_sleep failed, pm_state would be still
3330 * swrm_PM_ASLEEP, don't overwrite
3331 */
3332 if (likely(swrm->pm_state == SWRM_PM_AWAKE))
3333 swrm->pm_state = SWRM_PM_SLEEPABLE;
3334 pm_qos_update_request(&swrm->pm_qos_req,
3335 PM_QOS_DEFAULT_VALUE);
3336 pm_relax(swrm->dev);
3337 }
3338 mutex_unlock(&swrm->pm_lock);
3339 wake_up_all(&swrm->pm_wq);
3340}
3341
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303342#ifdef CONFIG_PM_SLEEP
3343static int swrm_suspend(struct device *dev)
3344{
3345 int ret = -EBUSY;
3346 struct platform_device *pdev = to_platform_device(dev);
3347 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3348
3349 dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
Ramprasad Katkam57349872018-11-11 18:34:57 +05303350
3351 mutex_lock(&swrm->pm_lock);
3352
3353 if (swrm->pm_state == SWRM_PM_SLEEPABLE) {
3354 dev_dbg(swrm->dev, "%s: suspending system, state %d, wlock %d\n",
3355 __func__, swrm->pm_state,
3356 swrm->wlock_holders);
3357 swrm->pm_state = SWRM_PM_ASLEEP;
3358 } else if (swrm->pm_state == SWRM_PM_AWAKE) {
3359 /*
3360 * unlock to wait for pm_state == SWRM_PM_SLEEPABLE
3361 * then set to SWRM_PM_ASLEEP
3362 */
3363 dev_dbg(swrm->dev, "%s: waiting to suspend system, state %d, wlock %d\n",
3364 __func__, swrm->pm_state,
3365 swrm->wlock_holders);
3366 mutex_unlock(&swrm->pm_lock);
3367 if (!(wait_event_timeout(swrm->pm_wq, swrm_pm_cmpxchg(
3368 swrm, SWRM_PM_SLEEPABLE,
3369 SWRM_PM_ASLEEP) ==
3370 SWRM_PM_SLEEPABLE,
3371 msecs_to_jiffies(
3372 SWRM_SYS_SUSPEND_WAIT)))) {
3373 dev_dbg(swrm->dev, "%s: suspend failed state %d, wlock %d\n",
3374 __func__, swrm->pm_state,
3375 swrm->wlock_holders);
3376 return -EBUSY;
3377 } else {
3378 dev_dbg(swrm->dev,
3379 "%s: done, state %d, wlock %d\n",
3380 __func__, swrm->pm_state,
3381 swrm->wlock_holders);
3382 }
3383 mutex_lock(&swrm->pm_lock);
3384 } else if (swrm->pm_state == SWRM_PM_ASLEEP) {
3385 dev_dbg(swrm->dev, "%s: system is already suspended, state %d, wlock %d\n",
3386 __func__, swrm->pm_state,
3387 swrm->wlock_holders);
3388 }
3389
3390 mutex_unlock(&swrm->pm_lock);
3391
3392 if ((!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev))) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303393 ret = swrm_runtime_suspend(dev);
3394 if (!ret) {
3395 /*
3396 * Synchronize runtime-pm and system-pm states:
3397 * At this point, we are already suspended. If
3398 * runtime-pm still thinks its active, then
3399 * make sure its status is in sync with HW
3400 * status. The three below calls let the
3401 * runtime-pm know that we are suspended
3402 * already without re-invoking the suspend
3403 * callback
3404 */
3405 pm_runtime_disable(dev);
3406 pm_runtime_set_suspended(dev);
3407 pm_runtime_enable(dev);
3408 }
3409 }
3410 if (ret == -EBUSY) {
3411 /*
3412 * There is a possibility that some audio stream is active
3413 * during suspend. We dont want to return suspend failure in
3414 * that case so that display and relevant components can still
3415 * go to suspend.
3416 * If there is some other error, then it should be passed-on
3417 * to system level suspend
3418 */
3419 ret = 0;
3420 }
3421 return ret;
3422}
3423
3424static int swrm_resume(struct device *dev)
3425{
3426 int ret = 0;
3427 struct platform_device *pdev = to_platform_device(dev);
3428 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3429
3430 dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
3431 if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
3432 ret = swrm_runtime_resume(dev);
3433 if (!ret) {
3434 pm_runtime_mark_last_busy(dev);
3435 pm_request_autosuspend(dev);
3436 }
3437 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05303438 mutex_lock(&swrm->pm_lock);
3439 if (swrm->pm_state == SWRM_PM_ASLEEP) {
3440 dev_dbg(swrm->dev,
3441 "%s: resuming system, state %d, wlock %d\n",
3442 __func__, swrm->pm_state,
3443 swrm->wlock_holders);
3444 swrm->pm_state = SWRM_PM_SLEEPABLE;
3445 } else {
3446 dev_dbg(swrm->dev, "%s: system is already awake, state %d wlock %d\n",
3447 __func__, swrm->pm_state,
3448 swrm->wlock_holders);
3449 }
3450 mutex_unlock(&swrm->pm_lock);
3451 wake_up_all(&swrm->pm_wq);
3452
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303453 return ret;
3454}
3455#endif /* CONFIG_PM_SLEEP */
3456
3457static const struct dev_pm_ops swrm_dev_pm_ops = {
3458 SET_SYSTEM_SLEEP_PM_OPS(
3459 swrm_suspend,
3460 swrm_resume
3461 )
3462 SET_RUNTIME_PM_OPS(
3463 swrm_runtime_suspend,
3464 swrm_runtime_resume,
3465 NULL
3466 )
3467};
3468
3469static const struct of_device_id swrm_dt_match[] = {
3470 {
3471 .compatible = "qcom,swr-mstr",
3472 },
3473 {}
3474};
3475
3476static struct platform_driver swr_mstr_driver = {
3477 .probe = swrm_probe,
3478 .remove = swrm_remove,
3479 .driver = {
3480 .name = SWR_WCD_NAME,
3481 .owner = THIS_MODULE,
3482 .pm = &swrm_dev_pm_ops,
3483 .of_match_table = swrm_dt_match,
Xiaojun Sang53cd13a2018-06-29 15:14:37 +08003484 .suppress_bind_attrs = true,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303485 },
3486};
3487
3488static int __init swrm_init(void)
3489{
3490 return platform_driver_register(&swr_mstr_driver);
3491}
3492module_init(swrm_init);
3493
3494static void __exit swrm_exit(void)
3495{
3496 platform_driver_unregister(&swr_mstr_driver);
3497}
3498module_exit(swrm_exit);
3499
3500MODULE_LICENSE("GPL v2");
3501MODULE_DESCRIPTION("SoundWire Master Controller");
3502MODULE_ALIAS("platform:swr-mstr");