blob: 2ab1bee1dd11e453fe818494097e40771fa303c7 [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 Kasamec8c9092019-12-17 13:12:58 +053048#define SWRM_LINK_STATUS_RETRY_CNT 100
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 {
Meng Wang31a7ef12019-12-18 10:36:29 +08001810 swr_dev->slave_irq_pending = 0;
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301811 handle_nested_irq(
1812 irq_find_mapping(
1813 swr_dev->slave_irq, 0));
1814 } while (swr_dev->slave_irq_pending);
1815 }
1816
1817 }
1818 break;
1819 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1820 dev_dbg(swrm->dev, "%s: SWR new slave attached\n",
1821 __func__);
1822 break;
1823 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1824 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
Laxminath Kasam44cedb82019-11-20 17:37:23 +05301825 swrm_enable_slave_irq(swrm);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301826 if (status == swrm->slave_status) {
1827 dev_dbg(swrm->dev,
1828 "%s: No change in slave status: %d\n",
1829 __func__, status);
1830 break;
1831 }
1832 chg_sts = swrm_check_slave_change_status(swrm, status,
1833 &devnum);
1834 switch (chg_sts) {
1835 case SWR_NOT_PRESENT:
1836 dev_dbg(swrm->dev,
1837 "%s: device %d got detached\n",
1838 __func__, devnum);
1839 break;
1840 case SWR_ATTACHED_OK:
1841 dev_dbg(swrm->dev,
1842 "%s: device %d got attached\n",
1843 __func__, devnum);
1844 /* enable host irq from slave device*/
1845 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1846 SWRS_SCP_INT_STATUS_CLEAR_1);
1847 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1848 SWRS_SCP_INT_STATUS_MASK_1);
1849
1850 break;
1851 case SWR_ALERT:
1852 dev_dbg(swrm->dev,
1853 "%s: device %d has pending interrupt\n",
1854 __func__, devnum);
1855 break;
1856 }
1857 break;
1858 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1859 dev_err_ratelimited(swrm->dev,
1860 "%s: SWR bus clsh detected\n",
1861 __func__);
1862 break;
1863 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1864 dev_dbg(swrm->dev, "%s: SWR read FIFO overflow\n",
1865 __func__);
1866 break;
1867 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1868 dev_dbg(swrm->dev, "%s: SWR read FIFO underflow\n",
1869 __func__);
1870 break;
1871 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1872 dev_dbg(swrm->dev, "%s: SWR write FIFO overflow\n",
1873 __func__);
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301874 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301875 break;
1876 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1877 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1878 dev_err_ratelimited(swrm->dev,
1879 "%s: SWR CMD error, fifo status 0x%x, flushing fifo\n",
1880 __func__, value);
1881 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1882 break;
1883 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
1884 dev_err_ratelimited(swrm->dev,
1885 "%s: SWR Port collision detected\n",
1886 __func__);
1887 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
1888 swr_master_write(swrm,
1889 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1890 break;
1891 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1892 dev_dbg(swrm->dev,
1893 "%s: SWR read enable valid mismatch\n",
1894 __func__);
1895 swrm->intr_mask &=
1896 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1897 swr_master_write(swrm,
1898 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1899 break;
1900 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1901 complete(&swrm->broadcast);
1902 dev_dbg(swrm->dev, "%s: SWR cmd id finished\n",
1903 __func__);
1904 break;
1905 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED_V2:
1906 break;
1907 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL_V2:
1908 break;
1909 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED_V2:
Laxminath Kasame2291972019-11-08 14:51:59 +05301910 swrm_check_link_status(swrm, 0x1);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301911 break;
1912 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED_V2:
1913 break;
1914 case SWRM_INTERRUPT_STATUS_EXT_CLK_STOP_WAKEUP:
1915 if (swrm->state == SWR_MSTR_UP)
1916 dev_dbg(swrm->dev,
1917 "%s:SWR Master is already up\n",
1918 __func__);
1919 else
1920 dev_err_ratelimited(swrm->dev,
1921 "%s: SWR wokeup during clock stop\n",
1922 __func__);
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301923 /* It might be possible the slave device gets reset
1924 * and slave interrupt gets missed. So re-enable
1925 * Host IRQ and process slave pending
1926 * interrupts, if any.
1927 */
1928 swrm_enable_slave_irq(swrm);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301929 break;
1930 default:
1931 dev_err_ratelimited(swrm->dev,
1932 "%s: SWR unknown interrupt value: %d\n",
1933 __func__, value);
1934 ret = IRQ_NONE;
1935 break;
1936 }
1937 }
1938 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1939 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
1940
1941 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1942 intr_sts_masked = intr_sts & swrm->intr_mask;
1943
1944 if (intr_sts_masked) {
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301945 dev_dbg(swrm->dev, "%s: new interrupt received 0x%x\n",
1946 __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301947 goto handle_irq;
1948 }
1949
1950 mutex_lock(&swrm->reslock);
1951 swrm_clk_request(swrm, false);
Karthikeyan Mani4bee1db2019-09-18 17:58:41 -07001952err_audio_core_vote:
Sudheer Papothi384addd2019-06-14 02:26:52 +05301953 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
Sudheer Papothi06f43412019-07-09 03:32:54 +05301954
1955err_audio_hw_vote:
Sudheer Papothi384addd2019-06-14 02:26:52 +05301956 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001957exit:
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301958 mutex_unlock(&swrm->reslock);
1959 swrm_unlock_sleep(swrm);
1960 return ret;
1961}
1962
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301963static irqreturn_t swrm_wakeup_interrupt(int irq, void *dev)
1964{
1965 struct swr_mstr_ctrl *swrm = dev;
1966 int ret = IRQ_HANDLED;
1967
1968 if (!swrm || !(swrm->dev)) {
1969 pr_err("%s: swrm or dev is null\n", __func__);
1970 return IRQ_NONE;
1971 }
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05301972
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301973 mutex_lock(&swrm->devlock);
1974 if (!swrm->dev_up) {
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05301975 if (swrm->wake_irq > 0) {
1976 if (unlikely(!irq_get_irq_data(swrm->wake_irq))) {
1977 pr_err("%s: irq data is NULL\n", __func__);
1978 mutex_unlock(&swrm->devlock);
1979 return IRQ_NONE;
1980 }
1981 mutex_lock(&swrm->irq_lock);
1982 if (!irqd_irq_disabled(
1983 irq_get_irq_data(swrm->wake_irq)))
1984 disable_irq_nosync(swrm->wake_irq);
1985 mutex_unlock(&swrm->irq_lock);
1986 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301987 mutex_unlock(&swrm->devlock);
1988 return ret;
1989 }
1990 mutex_unlock(&swrm->devlock);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301991 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1992 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1993 goto exit;
1994 }
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05301995 if (swrm->wake_irq > 0) {
1996 if (unlikely(!irq_get_irq_data(swrm->wake_irq))) {
1997 pr_err("%s: irq data is NULL\n", __func__);
1998 return IRQ_NONE;
1999 }
2000 mutex_lock(&swrm->irq_lock);
2001 if (!irqd_irq_disabled(
2002 irq_get_irq_data(swrm->wake_irq)))
2003 disable_irq_nosync(swrm->wake_irq);
2004 mutex_unlock(&swrm->irq_lock);
2005 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302006 pm_runtime_get_sync(swrm->dev);
2007 pm_runtime_mark_last_busy(swrm->dev);
2008 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05302009 swrm_unlock_sleep(swrm);
2010exit:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302011 return ret;
2012}
2013
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302014static void swrm_wakeup_work(struct work_struct *work)
2015{
2016 struct swr_mstr_ctrl *swrm;
2017
2018 swrm = container_of(work, struct swr_mstr_ctrl,
2019 wakeup_work);
2020 if (!swrm || !(swrm->dev)) {
2021 pr_err("%s: swrm or dev is null\n", __func__);
2022 return;
2023 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302024
2025 mutex_lock(&swrm->devlock);
2026 if (!swrm->dev_up) {
2027 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302028 goto exit;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302029 }
2030 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302031 if (unlikely(swrm_lock_sleep(swrm) == false)) {
2032 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
2033 goto exit;
2034 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302035 pm_runtime_get_sync(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302036 pm_runtime_mark_last_busy(swrm->dev);
2037 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302038 swrm_unlock_sleep(swrm);
2039exit:
2040 pm_relax(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302041}
2042
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302043static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
2044{
2045 u32 val;
2046
2047 swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
2048 val = (swrm->slave_status >> (devnum * 2));
2049 val &= SWRM_MCP_SLV_STATUS_MASK;
2050 return val;
2051}
2052
2053static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
2054 u8 *dev_num)
2055{
2056 int i;
2057 u64 id = 0;
2058 int ret = -EINVAL;
2059 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
2060 struct swr_device *swr_dev;
2061 u32 num_dev = 0;
2062
2063 if (!swrm) {
2064 pr_err("%s: Invalid handle to swr controller\n",
2065 __func__);
2066 return ret;
2067 }
2068 if (swrm->num_dev)
2069 num_dev = swrm->num_dev;
2070 else
2071 num_dev = mstr->num_dev;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302072
2073 mutex_lock(&swrm->devlock);
2074 if (!swrm->dev_up) {
2075 mutex_unlock(&swrm->devlock);
2076 return ret;
2077 }
2078 mutex_unlock(&swrm->devlock);
2079
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302080 pm_runtime_get_sync(swrm->dev);
2081 for (i = 1; i < (num_dev + 1); i++) {
2082 id = ((u64)(swr_master_read(swrm,
2083 SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
2084 id |= swr_master_read(swrm,
2085 SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302086
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302087 /*
2088 * As pm_runtime_get_sync() brings all slaves out of reset
2089 * update logical device number for all slaves.
2090 */
2091 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2092 if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
2093 u32 status = swrm_get_device_status(swrm, i);
2094
2095 if ((status == 0x01) || (status == 0x02)) {
2096 swr_dev->dev_num = i;
2097 if ((id & SWR_DEV_ID_MASK) == dev_id) {
2098 *dev_num = i;
2099 ret = 0;
2100 }
2101 dev_dbg(swrm->dev,
2102 "%s: devnum %d is assigned for dev addr %lx\n",
2103 __func__, i, swr_dev->addr);
2104 }
2105 }
2106 }
2107 }
2108 if (ret)
2109 dev_err(swrm->dev, "%s: device 0x%llx is not ready\n",
2110 __func__, dev_id);
2111
2112 pm_runtime_mark_last_busy(swrm->dev);
2113 pm_runtime_put_autosuspend(swrm->dev);
2114 return ret;
2115}
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302116
2117static void swrm_device_wakeup_vote(struct swr_master *mstr)
2118{
2119 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
2120
2121 if (!swrm) {
2122 pr_err("%s: Invalid handle to swr controller\n",
2123 __func__);
2124 return;
2125 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302126 if (unlikely(swrm_lock_sleep(swrm) == false)) {
2127 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
2128 return;
2129 }
Sudheer Papothi384addd2019-06-14 02:26:52 +05302130 if (++swrm->hw_core_clk_en == 1)
2131 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2132 dev_err(swrm->dev, "%s:lpass core hw enable failed\n",
2133 __func__);
2134 --swrm->hw_core_clk_en;
2135 }
2136 if ( ++swrm->aud_core_clk_en == 1)
2137 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2138 dev_err(swrm->dev, "%s:lpass audio hw enable failed\n",
2139 __func__);
2140 --swrm->aud_core_clk_en;
2141 }
2142 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
2143 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302144 pm_runtime_get_sync(swrm->dev);
2145}
2146
2147static void swrm_device_wakeup_unvote(struct swr_master *mstr)
2148{
2149 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
2150
2151 if (!swrm) {
2152 pr_err("%s: Invalid handle to swr controller\n",
2153 __func__);
2154 return;
2155 }
2156 pm_runtime_mark_last_busy(swrm->dev);
2157 pm_runtime_put_autosuspend(swrm->dev);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302158 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
2159 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
2160
2161 --swrm->aud_core_clk_en;
2162 if (swrm->aud_core_clk_en < 0)
2163 swrm->aud_core_clk_en = 0;
2164 else if (swrm->aud_core_clk_en == 0)
2165 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2166
2167 --swrm->hw_core_clk_en;
2168 if (swrm->hw_core_clk_en < 0)
2169 swrm->hw_core_clk_en = 0;
2170 else if (swrm->hw_core_clk_en == 0)
2171 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
2172
Ramprasad Katkam57349872018-11-11 18:34:57 +05302173 swrm_unlock_sleep(swrm);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302174}
2175
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302176static int swrm_master_init(struct swr_mstr_ctrl *swrm)
2177{
2178 int ret = 0;
2179 u32 val;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302180 u8 row_ctrl = SWR_ROW_50;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302181 u8 col_ctrl = SWR_MIN_COL;
2182 u8 ssp_period = 1;
2183 u8 retry_cmd_num = 3;
2184 u32 reg[SWRM_MAX_INIT_REG];
2185 u32 value[SWRM_MAX_INIT_REG];
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302186 u32 temp = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302187 int len = 0;
2188
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302189 ssp_period = swrm_get_ssp_period(swrm, SWRM_ROW_50,
2190 SWRM_COL_02, SWRM_FRAME_SYNC_SEL);
2191 dev_dbg(swrm->dev, "%s: ssp_period: %d\n", __func__, ssp_period);
2192
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302193 /* Clear Rows and Cols */
2194 val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
2195 (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302196 ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302197
2198 reg[len] = SWRM_MCP_FRAME_CTRL_BANK_ADDR(0);
2199 value[len++] = val;
2200
2201 /* Set Auto enumeration flag */
2202 reg[len] = SWRM_ENUMERATOR_CFG_ADDR;
2203 value[len++] = 1;
2204
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302205 /* Configure No pings */
2206 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2207 val &= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK;
2208 val |= (0x1f << SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT);
2209 reg[len] = SWRM_MCP_CFG_ADDR;
2210 value[len++] = val;
2211
2212 /* Configure number of retries of a read/write cmd */
2213 val = (retry_cmd_num << SWRM_CMD_FIFO_CFG_NUM_OF_CMD_RETRY_SHFT);
2214 reg[len] = SWRM_CMD_FIFO_CFG_ADDR;
2215 value[len++] = val;
2216
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302217 reg[len] = SWRM_MCP_BUS_CTRL_ADDR;
2218 value[len++] = 0x2;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302219
Ramprasad Katkam83303512018-10-11 17:34:22 +05302220 /* Set IRQ to PULSE */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302221 reg[len] = SWRM_COMP_CFG_ADDR;
Ramprasad Katkam83303512018-10-11 17:34:22 +05302222 value[len++] = 0x02;
2223
2224 reg[len] = SWRM_COMP_CFG_ADDR;
2225 value[len++] = 0x03;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302226
2227 reg[len] = SWRM_INTERRUPT_CLEAR;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302228 value[len++] = 0xFFFFFFFF;
2229
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302230 swrm->intr_mask = SWRM_INTERRUPT_STATUS_MASK;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302231 /* Mask soundwire interrupts */
2232 reg[len] = SWRM_INTERRUPT_MASK_ADDR;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302233 value[len++] = swrm->intr_mask;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302234
2235 reg[len] = SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302236 value[len++] = swrm->intr_mask;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302237
2238 swr_master_bulk_write(swrm, reg, value, len);
2239
Laxminath Kasamcafe0732019-11-20 17:31:58 +05302240 if (!swrm_check_link_status(swrm, 0x1)) {
2241 dev_err(swrm->dev,
2242 "%s: swr link failed to connect\n",
2243 __func__);
2244 return -EINVAL;
2245 }
Sudheer Papothi63f48152018-11-15 01:08:03 +05302246 /*
2247 * For SWR master version 1.5.1, continue
2248 * execute on command ignore.
2249 */
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302250 /* Execute it for versions >= 1.5.1 */
2251 if (swrm->version >= SWRM_VERSION_1_5_1)
Sudheer Papothi63f48152018-11-15 01:08:03 +05302252 swr_master_write(swrm, SWRM_CMD_FIFO_CFG_ADDR,
2253 (swr_master_read(swrm,
2254 SWRM_CMD_FIFO_CFG_ADDR) | 0x80000000));
2255
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302256 /* SW workaround to gate hw_ctl for SWR version >=1.6 */
2257 if (swrm->version >= SWRM_VERSION_1_6) {
2258 if (swrm->swrm_hctl_reg) {
2259 temp = ioread32(swrm->swrm_hctl_reg);
2260 temp &= 0xFFFFFFFD;
2261 iowrite32(temp, swrm->swrm_hctl_reg);
2262 }
2263 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302264 return ret;
2265}
2266
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302267static int swrm_event_notify(struct notifier_block *self,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302268 unsigned long action, void *data)
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302269{
2270 struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302271 event_notifier);
2272
2273 if (!swrm || !(swrm->dev)) {
2274 pr_err("%s: swrm or dev is NULL\n", __func__);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302275 return -EINVAL;
2276 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302277 switch (action) {
2278 case MSM_AUD_DC_EVENT:
2279 schedule_work(&(swrm->dc_presence_work));
2280 break;
2281 case SWR_WAKE_IRQ_EVENT:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302282 if (swrm->ipc_wakeup && !swrm->ipc_wakeup_triggered) {
2283 swrm->ipc_wakeup_triggered = true;
Ramprasad Katkam57349872018-11-11 18:34:57 +05302284 pm_stay_awake(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302285 schedule_work(&swrm->wakeup_work);
Ramprasad Katkamcd61c6e2018-09-18 13:22:58 +05302286 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302287 break;
2288 default:
2289 dev_err(swrm->dev, "%s: invalid event type: %lu\n",
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302290 __func__, action);
2291 return -EINVAL;
2292 }
2293
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302294 return 0;
2295}
2296
2297static void swrm_notify_work_fn(struct work_struct *work)
2298{
2299 struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302300 dc_presence_work);
2301
2302 if (!swrm || !swrm->pdev) {
2303 pr_err("%s: swrm or pdev is NULL\n", __func__);
2304 return;
2305 }
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302306 swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL);
2307}
2308
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302309static int swrm_probe(struct platform_device *pdev)
2310{
2311 struct swr_mstr_ctrl *swrm;
2312 struct swr_ctrl_platform_data *pdata;
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302313 u32 i, num_ports, port_num, port_type, ch_mask, swrm_hctl_reg = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302314 u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0;
2315 int ret = 0;
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302316 struct clk *lpass_core_hw_vote = NULL;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302317 struct clk *lpass_core_audio = NULL;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302318
2319 /* Allocate soundwire master driver structure */
2320 swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl),
2321 GFP_KERNEL);
2322 if (!swrm) {
2323 ret = -ENOMEM;
2324 goto err_memory_fail;
2325 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302326 swrm->pdev = pdev;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302327 swrm->dev = &pdev->dev;
2328 platform_set_drvdata(pdev, swrm);
2329 swr_set_ctrl_data(&swrm->master, swrm);
2330 pdata = dev_get_platdata(&pdev->dev);
2331 if (!pdata) {
2332 dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
2333 __func__);
2334 ret = -EINVAL;
2335 goto err_pdata_fail;
2336 }
2337 swrm->handle = (void *)pdata->handle;
2338 if (!swrm->handle) {
2339 dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
2340 __func__);
2341 ret = -EINVAL;
2342 goto err_pdata_fail;
2343 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302344 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr_master_id",
2345 &swrm->master_id);
2346 if (ret) {
2347 dev_err(&pdev->dev, "%s: failed to get master id\n", __func__);
2348 goto err_pdata_fail;
2349 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302350 if (!(of_property_read_u32(pdev->dev.of_node,
2351 "swrm-io-base", &swrm->swrm_base_reg)))
2352 ret = of_property_read_u32(pdev->dev.of_node,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302353 "swrm-io-base", &swrm->swrm_base_reg);
2354 if (!swrm->swrm_base_reg) {
2355 swrm->read = pdata->read;
2356 if (!swrm->read) {
2357 dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
2358 __func__);
2359 ret = -EINVAL;
2360 goto err_pdata_fail;
2361 }
2362 swrm->write = pdata->write;
2363 if (!swrm->write) {
2364 dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
2365 __func__);
2366 ret = -EINVAL;
2367 goto err_pdata_fail;
2368 }
2369 swrm->bulk_write = pdata->bulk_write;
2370 if (!swrm->bulk_write) {
2371 dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
2372 __func__);
2373 ret = -EINVAL;
2374 goto err_pdata_fail;
2375 }
2376 } else {
2377 swrm->swrm_dig_base = devm_ioremap(&pdev->dev,
2378 swrm->swrm_base_reg, SWRM_MAX_REGISTER);
2379 }
2380
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07002381 swrm->core_vote = pdata->core_vote;
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302382 if (!(of_property_read_u32(pdev->dev.of_node,
2383 "qcom,swrm-hctl-reg", &swrm_hctl_reg)))
2384 swrm->swrm_hctl_reg = devm_ioremap(&pdev->dev,
2385 swrm_hctl_reg, 0x4);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302386 swrm->clk = pdata->clk;
2387 if (!swrm->clk) {
2388 dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
2389 __func__);
2390 ret = -EINVAL;
2391 goto err_pdata_fail;
2392 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302393 if (of_property_read_u32(pdev->dev.of_node,
2394 "qcom,swr-clock-stop-mode0",
2395 &swrm->clk_stop_mode0_supp)) {
2396 swrm->clk_stop_mode0_supp = FALSE;
2397 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302398
2399 ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
2400 &swrm->num_dev);
2401 if (ret) {
2402 dev_dbg(&pdev->dev, "%s: Looking up %s property failed\n",
2403 __func__, "qcom,swr-num-dev");
2404 } else {
2405 if (swrm->num_dev > SWR_MAX_SLAVE_DEVICES) {
2406 dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
2407 __func__, swrm->num_dev, SWR_MAX_SLAVE_DEVICES);
2408 ret = -EINVAL;
2409 goto err_pdata_fail;
2410 }
2411 }
2412
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302413 /* Parse soundwire port mapping */
2414 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
2415 &num_ports);
2416 if (ret) {
2417 dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
2418 goto err_pdata_fail;
2419 }
2420 swrm->num_ports = num_ports;
2421
2422 if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
2423 &map_size)) {
2424 dev_err(swrm->dev, "missing port mapping\n");
2425 goto err_pdata_fail;
2426 }
2427
2428 map_length = map_size / (3 * sizeof(u32));
2429 if (num_ports > SWR_MSTR_PORT_LEN) {
2430 dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
2431 __func__);
2432 ret = -EINVAL;
2433 goto err_pdata_fail;
2434 }
2435 temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
2436
2437 if (!temp) {
2438 ret = -ENOMEM;
2439 goto err_pdata_fail;
2440 }
2441 ret = of_property_read_u32_array(pdev->dev.of_node,
2442 "qcom,swr-port-mapping", temp, 3 * map_length);
2443 if (ret) {
2444 dev_err(swrm->dev, "%s: Failed to read port mapping\n",
2445 __func__);
2446 goto err_pdata_fail;
2447 }
2448
2449 for (i = 0; i < map_length; i++) {
2450 port_num = temp[3 * i];
2451 port_type = temp[3 * i + 1];
2452 ch_mask = temp[3 * i + 2];
2453
2454 if (port_num != old_port_num)
2455 ch_iter = 0;
2456 swrm->port_mapping[port_num][ch_iter].port_type = port_type;
2457 swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
2458 old_port_num = port_num;
2459 }
2460 devm_kfree(&pdev->dev, temp);
2461
2462 swrm->reg_irq = pdata->reg_irq;
2463 swrm->master.read = swrm_read;
2464 swrm->master.write = swrm_write;
2465 swrm->master.bulk_write = swrm_bulk_write;
2466 swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
2467 swrm->master.connect_port = swrm_connect_port;
2468 swrm->master.disconnect_port = swrm_disconnect_port;
2469 swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
2470 swrm->master.remove_from_group = swrm_remove_from_group;
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302471 swrm->master.device_wakeup_vote = swrm_device_wakeup_vote;
2472 swrm->master.device_wakeup_unvote = swrm_device_wakeup_unvote;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302473 swrm->master.dev.parent = &pdev->dev;
2474 swrm->master.dev.of_node = pdev->dev.of_node;
2475 swrm->master.num_port = 0;
2476 swrm->rcmd_id = 0;
2477 swrm->wcmd_id = 0;
2478 swrm->slave_status = 0;
2479 swrm->num_rx_chs = 0;
2480 swrm->clk_ref_count = 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302481 swrm->swr_irq_wakeup_capable = 0;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302482 swrm->mclk_freq = MCLK_FREQ;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302483 swrm->bus_clk = MCLK_FREQ;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302484 swrm->dev_up = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302485 swrm->state = SWR_MSTR_UP;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302486 swrm->ipc_wakeup = false;
2487 swrm->ipc_wakeup_triggered = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302488 init_completion(&swrm->reset);
2489 init_completion(&swrm->broadcast);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302490 init_completion(&swrm->clk_off_complete);
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302491 mutex_init(&swrm->irq_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302492 mutex_init(&swrm->mlock);
2493 mutex_init(&swrm->reslock);
2494 mutex_init(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302495 mutex_init(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302496 mutex_init(&swrm->clklock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302497 mutex_init(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302498 mutex_init(&swrm->pm_lock);
2499 swrm->wlock_holders = 0;
2500 swrm->pm_state = SWRM_PM_SLEEPABLE;
2501 init_waitqueue_head(&swrm->pm_wq);
2502 pm_qos_add_request(&swrm->pm_qos_req,
2503 PM_QOS_CPU_DMA_LATENCY,
2504 PM_QOS_DEFAULT_VALUE);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302505
2506 for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++)
2507 INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
2508
Sudheer Papothi06f43412019-07-09 03:32:54 +05302509 /* Register LPASS core hw vote */
2510 lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
2511 if (IS_ERR(lpass_core_hw_vote)) {
2512 ret = PTR_ERR(lpass_core_hw_vote);
2513 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2514 __func__, "lpass_core_hw_vote", ret);
2515 lpass_core_hw_vote = NULL;
2516 ret = 0;
2517 }
2518 swrm->lpass_core_hw_vote = lpass_core_hw_vote;
2519
2520 /* Register LPASS audio core vote */
2521 lpass_core_audio = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
2522 if (IS_ERR(lpass_core_audio)) {
2523 ret = PTR_ERR(lpass_core_audio);
2524 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2525 __func__, "lpass_core_audio", ret);
2526 lpass_core_audio = NULL;
2527 ret = 0;
2528 }
2529 swrm->lpass_core_audio = lpass_core_audio;
2530
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302531 if (swrm->reg_irq) {
2532 ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
2533 SWR_IRQ_REGISTER);
2534 if (ret) {
2535 dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
2536 __func__, ret);
2537 goto err_irq_fail;
2538 }
2539 } else {
2540 swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
2541 if (swrm->irq < 0) {
2542 dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
2543 __func__, swrm->irq);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302544 goto err_irq_fail;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302545 }
2546
2547 ret = request_threaded_irq(swrm->irq, NULL,
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302548 swr_mstr_interrupt_v2,
Ramprasad Katkam83303512018-10-11 17:34:22 +05302549 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302550 "swr_master_irq", swrm);
2551 if (ret) {
2552 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2553 __func__, ret);
2554 goto err_irq_fail;
2555 }
2556
2557 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302558 /* Make inband tx interrupts as wakeup capable for slave irq */
2559 ret = of_property_read_u32(pdev->dev.of_node,
2560 "qcom,swr-mstr-irq-wakeup-capable",
2561 &swrm->swr_irq_wakeup_capable);
2562 if (ret)
2563 dev_dbg(swrm->dev, "%s: swrm irq wakeup capable not defined\n",
2564 __func__);
2565 if (swrm->swr_irq_wakeup_capable)
2566 irq_set_irq_wake(swrm->irq, 1);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302567 ret = swr_register_master(&swrm->master);
2568 if (ret) {
2569 dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
2570 goto err_mstr_fail;
2571 }
2572
2573 /* Add devices registered with board-info as the
2574 * controller will be up now
2575 */
2576 swr_master_add_boarddevices(&swrm->master);
2577 mutex_lock(&swrm->mlock);
2578 swrm_clk_request(swrm, true);
Laxminath Kasam4696fff2019-11-26 16:07:11 +05302579 swrm->version = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302580 ret = swrm_master_init(swrm);
2581 if (ret < 0) {
2582 dev_err(&pdev->dev,
2583 "%s: Error in master Initialization , err %d\n",
2584 __func__, ret);
2585 mutex_unlock(&swrm->mlock);
Laxminath Kasamec8c9092019-12-17 13:12:58 +05302586 goto err_mstr_init_fail;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302587 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302588
2589 mutex_unlock(&swrm->mlock);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302590 INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302591
2592 if (pdev->dev.of_node)
2593 of_register_swr_devices(&swrm->master);
2594
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302595#ifdef CONFIG_DEBUG_FS
2596 swrm->debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
2597 if (!IS_ERR(swrm->debugfs_swrm_dent)) {
2598 swrm->debugfs_peek = debugfs_create_file("swrm_peek",
2599 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2600 (void *) swrm, &swrm_debug_read_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302601
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302602 swrm->debugfs_poke = debugfs_create_file("swrm_poke",
2603 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2604 (void *) swrm, &swrm_debug_write_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302605
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302606 swrm->debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
2607 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2608 (void *) swrm,
2609 &swrm_debug_dump_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302610 }
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302611#endif
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302612 ret = device_init_wakeup(swrm->dev, true);
2613 if (ret) {
2614 dev_err(swrm->dev, "Device wakeup init failed: %d\n", ret);
2615 goto err_irq_wakeup_fail;
2616 }
2617
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302618 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
2619 pm_runtime_use_autosuspend(&pdev->dev);
2620 pm_runtime_set_active(&pdev->dev);
2621 pm_runtime_enable(&pdev->dev);
2622 pm_runtime_mark_last_busy(&pdev->dev);
2623
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302624 INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn);
2625 swrm->event_notifier.notifier_call = swrm_event_notify;
2626 msm_aud_evt_register_client(&swrm->event_notifier);
2627
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302628 return 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302629err_irq_wakeup_fail:
2630 device_init_wakeup(swrm->dev, false);
Laxminath Kasamec8c9092019-12-17 13:12:58 +05302631err_mstr_init_fail:
2632 swr_unregister_master(&swrm->master);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302633err_mstr_fail:
2634 if (swrm->reg_irq)
2635 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2636 swrm, SWR_IRQ_FREE);
2637 else if (swrm->irq)
2638 free_irq(swrm->irq, swrm);
2639err_irq_fail:
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302640 mutex_destroy(&swrm->irq_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302641 mutex_destroy(&swrm->mlock);
2642 mutex_destroy(&swrm->reslock);
2643 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302644 mutex_destroy(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302645 mutex_destroy(&swrm->clklock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302646 mutex_destroy(&swrm->pm_lock);
2647 pm_qos_remove_request(&swrm->pm_qos_req);
2648
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302649err_pdata_fail:
2650err_memory_fail:
2651 return ret;
2652}
2653
2654static int swrm_remove(struct platform_device *pdev)
2655{
2656 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2657
2658 if (swrm->reg_irq)
2659 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2660 swrm, SWR_IRQ_FREE);
2661 else if (swrm->irq)
2662 free_irq(swrm->irq, swrm);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302663 else if (swrm->wake_irq > 0)
2664 free_irq(swrm->wake_irq, swrm);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302665 if (swrm->swr_irq_wakeup_capable)
2666 irq_set_irq_wake(swrm->irq, 0);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302667 cancel_work_sync(&swrm->wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302668 pm_runtime_disable(&pdev->dev);
2669 pm_runtime_set_suspended(&pdev->dev);
2670 swr_unregister_master(&swrm->master);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302671 msm_aud_evt_unregister_client(&swrm->event_notifier);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302672 device_init_wakeup(swrm->dev, false);
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302673 mutex_destroy(&swrm->irq_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302674 mutex_destroy(&swrm->mlock);
2675 mutex_destroy(&swrm->reslock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302676 mutex_destroy(&swrm->iolock);
2677 mutex_destroy(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302678 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302679 mutex_destroy(&swrm->pm_lock);
2680 pm_qos_remove_request(&swrm->pm_qos_req);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302681 devm_kfree(&pdev->dev, swrm);
2682 return 0;
2683}
2684
2685static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
2686{
2687 u32 val;
2688
2689 dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
2690 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD);
2691 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2692 val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK;
2693 swr_master_write(swrm, SWRM_MCP_CFG_ADDR, val);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302694
2695 return 0;
2696}
2697
2698#ifdef CONFIG_PM
2699static int swrm_runtime_resume(struct device *dev)
2700{
2701 struct platform_device *pdev = to_platform_device(dev);
2702 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2703 int ret = 0;
Vatsal Buchae50b5002019-09-19 14:32:20 +05302704 bool swrm_clk_req_err = false;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302705 bool hw_core_err = false;
2706 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302707 struct swr_master *mstr = &swrm->master;
2708 struct swr_device *swr_dev;
2709
2710 dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
2711 __func__, swrm->state);
2712 mutex_lock(&swrm->reslock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302713
Sudheer Papothi384addd2019-06-14 02:26:52 +05302714 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2715 dev_err(dev, "%s:lpass core hw enable failed\n",
2716 __func__);
2717 hw_core_err = true;
2718 }
2719 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2720 dev_err(dev, "%s:lpass audio hw enable failed\n",
2721 __func__);
2722 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002723 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302724
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302725 if ((swrm->state == SWR_MSTR_DOWN) ||
2726 (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) {
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302727 if (swrm->clk_stop_mode0_supp) {
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302728 if (swrm->wake_irq > 0) {
2729 if (unlikely(!irq_get_irq_data
2730 (swrm->wake_irq))) {
2731 pr_err("%s: irq data is NULL\n",
2732 __func__);
2733 mutex_unlock(&swrm->reslock);
2734 return IRQ_NONE;
2735 }
2736 mutex_lock(&swrm->irq_lock);
2737 if (!irqd_irq_disabled(
2738 irq_get_irq_data(swrm->wake_irq)))
2739 disable_irq_nosync(swrm->wake_irq);
2740 mutex_unlock(&swrm->irq_lock);
2741 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302742 if (swrm->ipc_wakeup)
2743 msm_aud_evt_blocking_notifier_call_chain(
2744 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302745 }
2746
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302747 if (swrm_clk_request(swrm, true)) {
2748 /*
2749 * Set autosuspend timer to 1 for
2750 * master to enter into suspend.
2751 */
Vatsal Buchae50b5002019-09-19 14:32:20 +05302752 swrm_clk_req_err = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302753 goto exit;
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302754 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302755 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302756 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2757 ret = swr_device_up(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302758 if (ret == -ENODEV) {
2759 dev_dbg(dev,
2760 "%s slave device up not implemented\n",
2761 __func__);
2762 ret = 0;
2763 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302764 dev_err(dev,
2765 "%s: failed to wakeup swr dev %d\n",
2766 __func__, swr_dev->dev_num);
2767 swrm_clk_request(swrm, false);
2768 goto exit;
2769 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302770 }
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05302771 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2772 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2773 swrm_master_init(swrm);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302774 /* wait for hw enumeration to complete */
2775 usleep_range(100, 105);
Laxminath Kasame2291972019-11-08 14:51:59 +05302776 if (!swrm_check_link_status(swrm, 0x1))
Laxminath Kasam696b14b2019-12-03 22:07:34 +05302777 dev_dbg(dev, "%s:failed in connecting, ssr?\n",
2778 __func__);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302779 swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, 0x0,
2780 SWRS_SCP_INT_STATUS_MASK_1);
Karthikeyan Manif6821902019-05-21 17:31:24 -07002781 if (swrm->state == SWR_MSTR_SSR) {
2782 mutex_unlock(&swrm->reslock);
2783 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
2784 mutex_lock(&swrm->reslock);
2785 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302786 } else {
2787 /*wake up from clock stop*/
2788 swr_master_write(swrm, SWRM_MCP_BUS_CTRL_ADDR, 0x2);
Sudheer Papothi55fa5972019-09-28 02:50:27 +05302789 /* clear and enable bus clash interrupt */
2790 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x08);
2791 swrm->intr_mask |= 0x08;
2792 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR,
2793 swrm->intr_mask);
2794 swr_master_write(swrm,
2795 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
2796 swrm->intr_mask);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302797 usleep_range(100, 105);
Laxminath Kasame2291972019-11-08 14:51:59 +05302798 if (!swrm_check_link_status(swrm, 0x1))
Laxminath Kasam696b14b2019-12-03 22:07:34 +05302799 dev_dbg(dev, "%s:failed in connecting, ssr?\n",
2800 __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302801 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302802 swrm->state = SWR_MSTR_UP;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302803 }
2804exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302805 if (!aud_core_err)
2806 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2807 if (!hw_core_err)
2808 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Vatsal Buchae50b5002019-09-19 14:32:20 +05302809 if (swrm_clk_req_err)
2810 pm_runtime_set_autosuspend_delay(&pdev->dev,
2811 ERR_AUTO_SUSPEND_TIMER_VAL);
2812 else
2813 pm_runtime_set_autosuspend_delay(&pdev->dev,
2814 auto_suspend_timer);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302815 mutex_unlock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302816
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302817 return ret;
2818}
2819
2820static int swrm_runtime_suspend(struct device *dev)
2821{
2822 struct platform_device *pdev = to_platform_device(dev);
2823 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2824 int ret = 0;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302825 bool hw_core_err = false;
2826 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302827 struct swr_master *mstr = &swrm->master;
2828 struct swr_device *swr_dev;
2829 int current_state = 0;
2830
2831 dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
2832 __func__, swrm->state);
2833 mutex_lock(&swrm->reslock);
2834 mutex_lock(&swrm->force_down_lock);
2835 current_state = swrm->state;
2836 mutex_unlock(&swrm->force_down_lock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302837
2838 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2839 dev_err(dev, "%s:lpass core hw enable failed\n",
2840 __func__);
2841 hw_core_err = true;
2842 }
2843 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2844 dev_err(dev, "%s:lpass audio hw enable failed\n",
2845 __func__);
2846 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002847 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302848
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302849 if ((current_state == SWR_MSTR_UP) ||
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302850 (current_state == SWR_MSTR_SSR)) {
2851
2852 if ((current_state != SWR_MSTR_SSR) &&
2853 swrm_is_port_en(&swrm->master)) {
2854 dev_dbg(dev, "%s ports are enabled\n", __func__);
2855 ret = -EBUSY;
2856 goto exit;
2857 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302858 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05302859 mutex_unlock(&swrm->reslock);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +05302860 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
Sudheer Papothi06f43412019-07-09 03:32:54 +05302861 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302862 swrm_clk_pause(swrm);
2863 swr_master_write(swrm, SWRM_COMP_CFG_ADDR, 0x00);
2864 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2865 ret = swr_device_down(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302866 if (ret == -ENODEV) {
2867 dev_dbg_ratelimited(dev,
2868 "%s slave device down not implemented\n",
2869 __func__);
2870 ret = 0;
2871 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302872 dev_err(dev,
2873 "%s: failed to shutdown swr dev %d\n",
2874 __func__, swr_dev->dev_num);
2875 goto exit;
2876 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302877 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302878 } else {
Sudheer Papothi55fa5972019-09-28 02:50:27 +05302879 /* Mask bus clash interrupt */
2880 swrm->intr_mask &= ~((u32)0x08);
2881 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR,
2882 swrm->intr_mask);
2883 swr_master_write(swrm,
2884 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
2885 swrm->intr_mask);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302886 mutex_unlock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302887 /* clock stop sequence */
2888 swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
2889 SWRS_SCP_CONTROL);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302890 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302891 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302892 }
Laxminath Kasamcafe0732019-11-20 17:31:58 +05302893 if (!swrm_check_link_status(swrm, 0x0))
Laxminath Kasam696b14b2019-12-03 22:07:34 +05302894 dev_dbg(dev, "%s:failed in disconnecting, ssr?\n",
2895 __func__);
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07002896 ret = swrm_clk_request(swrm, false);
2897 if (ret) {
2898 dev_err(dev, "%s: swrmn clk failed\n", __func__);
2899 ret = 0;
2900 goto exit;
2901 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302902
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302903 if (swrm->clk_stop_mode0_supp) {
2904 if (swrm->wake_irq > 0) {
2905 enable_irq(swrm->wake_irq);
2906 } else if (swrm->ipc_wakeup) {
2907 msm_aud_evt_blocking_notifier_call_chain(
2908 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
2909 swrm->ipc_wakeup_triggered = false;
2910 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302911 }
2912
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302913 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302914 /* Retain SSR state until resume */
2915 if (current_state != SWR_MSTR_SSR)
2916 swrm->state = SWR_MSTR_DOWN;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302917exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302918 if (!aud_core_err)
2919 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2920 if (!hw_core_err)
2921 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302922 mutex_unlock(&swrm->reslock);
2923 return ret;
2924}
2925#endif /* CONFIG_PM */
2926
Sudheer Papothi06f43412019-07-09 03:32:54 +05302927static int swrm_device_suspend(struct device *dev)
2928{
2929 struct platform_device *pdev = to_platform_device(dev);
2930 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2931 int ret = 0;
2932
2933 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2934 if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
2935 ret = swrm_runtime_suspend(dev);
2936 if (!ret) {
2937 pm_runtime_disable(dev);
2938 pm_runtime_set_suspended(dev);
2939 pm_runtime_enable(dev);
2940 }
2941 }
2942
2943 return 0;
2944}
2945
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302946static int swrm_device_down(struct device *dev)
2947{
2948 struct platform_device *pdev = to_platform_device(dev);
2949 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302950
2951 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2952
2953 mutex_lock(&swrm->force_down_lock);
2954 swrm->state = SWR_MSTR_SSR;
2955 mutex_unlock(&swrm->force_down_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302956
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302957 swrm_device_suspend(dev);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302958 return 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302959}
2960
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302961int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm)
2962{
2963 int ret = 0;
Laxminath Kasama60239e2019-01-10 14:43:03 +05302964 int irq, dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302965
2966 if (!swrm->ipc_wakeup) {
Laxminath Kasama60239e2019-01-10 14:43:03 +05302967 irq = of_get_named_gpio(swrm->dev->of_node,
2968 "qcom,swr-wakeup-irq", 0);
2969 if (gpio_is_valid(irq)) {
2970 swrm->wake_irq = gpio_to_irq(irq);
2971 if (swrm->wake_irq < 0) {
2972 dev_err(swrm->dev,
2973 "Unable to configure irq\n");
2974 return swrm->wake_irq;
2975 }
2976 } else {
2977 dir_apps_irq = platform_get_irq_byname(swrm->pdev,
2978 "swr_wake_irq");
2979 if (dir_apps_irq < 0) {
2980 dev_err(swrm->dev,
2981 "TLMM connect gpio not found\n");
2982 return -EINVAL;
2983 }
2984 swrm->wake_irq = dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302985 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302986 ret = request_threaded_irq(swrm->wake_irq, NULL,
2987 swrm_wakeup_interrupt,
2988 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
2989 "swr_wake_irq", swrm);
2990 if (ret) {
2991 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2992 __func__, ret);
2993 return -EINVAL;
2994 }
Aditya Bavanari3517b112018-12-03 13:26:59 +05302995 irq_set_irq_wake(swrm->wake_irq, 1);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302996 }
2997 return ret;
2998}
2999
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05303000static int swrm_alloc_port_mem(struct device *dev, struct swr_mstr_ctrl *swrm,
3001 u32 uc, u32 size)
3002{
3003 if (!swrm->port_param) {
3004 swrm->port_param = devm_kzalloc(dev,
3005 sizeof(swrm->port_param) * SWR_UC_MAX,
3006 GFP_KERNEL);
3007 if (!swrm->port_param)
3008 return -ENOMEM;
3009 }
3010 if (!swrm->port_param[uc]) {
3011 swrm->port_param[uc] = devm_kcalloc(dev, size,
3012 sizeof(struct port_params),
3013 GFP_KERNEL);
3014 if (!swrm->port_param[uc])
3015 return -ENOMEM;
3016 } else {
3017 dev_err_ratelimited(swrm->dev, "%s: called more than once\n",
3018 __func__);
3019 }
3020
3021 return 0;
3022}
3023
3024static int swrm_copy_port_config(struct swr_mstr_ctrl *swrm,
3025 struct swrm_port_config *port_cfg,
3026 u32 size)
3027{
3028 int idx;
3029 struct port_params *params;
3030 int uc = port_cfg->uc;
3031 int ret = 0;
3032
3033 for (idx = 0; idx < size; idx++) {
3034 params = &((struct port_params *)port_cfg->params)[idx];
3035 if (!params) {
3036 dev_err(swrm->dev, "%s: Invalid params\n", __func__);
3037 ret = -EINVAL;
3038 break;
3039 }
3040 memcpy(&swrm->port_param[uc][idx], params,
3041 sizeof(struct port_params));
3042 }
3043
3044 return ret;
3045}
3046
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303047/**
3048 * swrm_wcd_notify - parent device can notify to soundwire master through
3049 * this function
3050 * @pdev: pointer to platform device structure
3051 * @id: command id from parent to the soundwire master
3052 * @data: data from parent device to soundwire master
3053 */
3054int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
3055{
3056 struct swr_mstr_ctrl *swrm;
3057 int ret = 0;
3058 struct swr_master *mstr;
3059 struct swr_device *swr_dev;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05303060 struct swrm_port_config *port_cfg;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303061
3062 if (!pdev) {
3063 pr_err("%s: pdev is NULL\n", __func__);
3064 return -EINVAL;
3065 }
3066 swrm = platform_get_drvdata(pdev);
3067 if (!swrm) {
3068 dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__);
3069 return -EINVAL;
3070 }
3071 mstr = &swrm->master;
3072
3073 switch (id) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05303074 case SWR_REQ_CLK_SWITCH:
3075 /* This will put soundwire in clock stop mode and disable the
3076 * clocks, if there is no active usecase running, so that the
3077 * next activity on soundwire will request clock from new clock
3078 * source.
3079 */
Sudheer Papothi8c50f2f2019-12-05 01:14:47 +05303080 if (!data) {
3081 dev_err(swrm->dev, "%s: data is NULL for id:%d\n",
3082 __func__, id);
3083 ret = -EINVAL;
3084 break;
3085 }
Sudheer Papothi06f43412019-07-09 03:32:54 +05303086 mutex_lock(&swrm->mlock);
Sudheer Papothi8c50f2f2019-12-05 01:14:47 +05303087 if (swrm->clk_src != *(int *)data) {
3088 if (swrm->state == SWR_MSTR_UP)
3089 swrm_device_suspend(&pdev->dev);
3090 swrm->clk_src = *(int *)data;
3091 }
Sudheer Papothi06f43412019-07-09 03:32:54 +05303092 mutex_unlock(&swrm->mlock);
3093 break;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05303094 case SWR_CLK_FREQ:
3095 if (!data) {
3096 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
3097 ret = -EINVAL;
3098 } else {
3099 mutex_lock(&swrm->mlock);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05303100 if (swrm->mclk_freq != *(int *)data) {
3101 dev_dbg(swrm->dev, "%s: freq change: force mstr down\n", __func__);
3102 if (swrm->state == SWR_MSTR_DOWN)
3103 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
3104 __func__, swrm->state);
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +05303105 else {
3106 swrm->mclk_freq = *(int *)data;
3107 swrm->bus_clk = swrm->mclk_freq;
3108 swrm_switch_frame_shape(swrm,
3109 swrm->bus_clk);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05303110 swrm_device_suspend(&pdev->dev);
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +05303111 }
Prasad Kumpatla386df4e2019-10-11 18:23:16 +05303112 /*
3113 * add delay to ensure clk release happen
3114 * if interrupt triggered for clk stop,
3115 * wait for it to exit
3116 */
3117 usleep_range(10000, 10500);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05303118 }
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05303119 swrm->mclk_freq = *(int *)data;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05303120 swrm->bus_clk = swrm->mclk_freq;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05303121 mutex_unlock(&swrm->mlock);
3122 }
3123 break;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303124 case SWR_DEVICE_SSR_DOWN:
3125 mutex_lock(&swrm->devlock);
3126 swrm->dev_up = false;
3127 mutex_unlock(&swrm->devlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05303128 mutex_lock(&swrm->reslock);
3129 swrm->state = SWR_MSTR_SSR;
3130 mutex_unlock(&swrm->reslock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303131 break;
3132 case SWR_DEVICE_SSR_UP:
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05303133 /* wait for clk voting to be zero */
Ramprasad Katkam7f6462e2018-11-06 11:51:22 +05303134 reinit_completion(&swrm->clk_off_complete);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05303135 if (swrm->clk_ref_count &&
3136 !wait_for_completion_timeout(&swrm->clk_off_complete,
Ramprasad Katkamc87efeb2018-12-12 19:26:19 +05303137 msecs_to_jiffies(500)))
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05303138 dev_err(swrm->dev, "%s: clock voting not zero\n",
3139 __func__);
3140
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303141 mutex_lock(&swrm->devlock);
3142 swrm->dev_up = true;
3143 mutex_unlock(&swrm->devlock);
3144 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303145 case SWR_DEVICE_DOWN:
3146 dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
3147 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05303148 if (swrm->state == SWR_MSTR_DOWN)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303149 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
3150 __func__, swrm->state);
3151 else
3152 swrm_device_down(&pdev->dev);
3153 mutex_unlock(&swrm->mlock);
3154 break;
3155 case SWR_DEVICE_UP:
3156 dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
Ramprasad Katkam0fed92f2018-11-08 14:22:22 +05303157 mutex_lock(&swrm->devlock);
3158 if (!swrm->dev_up) {
3159 dev_dbg(swrm->dev, "SSR not complete yet\n");
3160 mutex_unlock(&swrm->devlock);
3161 return -EBUSY;
3162 }
3163 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303164 mutex_lock(&swrm->mlock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303165 pm_runtime_mark_last_busy(&pdev->dev);
3166 pm_runtime_get_sync(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303167 mutex_lock(&swrm->reslock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303168 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
3169 ret = swr_reset_device(swr_dev);
3170 if (ret) {
3171 dev_err(swrm->dev,
3172 "%s: failed to reset swr device %d\n",
3173 __func__, swr_dev->dev_num);
3174 swrm_clk_request(swrm, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303175 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303176 }
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303177 pm_runtime_mark_last_busy(&pdev->dev);
3178 pm_runtime_put_autosuspend(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303179 mutex_unlock(&swrm->reslock);
3180 mutex_unlock(&swrm->mlock);
3181 break;
3182 case SWR_SET_NUM_RX_CH:
3183 if (!data) {
3184 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
3185 ret = -EINVAL;
3186 } else {
3187 mutex_lock(&swrm->mlock);
3188 swrm->num_rx_chs = *(int *)data;
3189 if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
3190 list_for_each_entry(swr_dev, &mstr->devices,
3191 dev_list) {
3192 ret = swr_set_device_group(swr_dev,
3193 SWR_BROADCAST);
3194 if (ret)
3195 dev_err(swrm->dev,
3196 "%s: set num ch failed\n",
3197 __func__);
3198 }
3199 } else {
3200 list_for_each_entry(swr_dev, &mstr->devices,
3201 dev_list) {
3202 ret = swr_set_device_group(swr_dev,
3203 SWR_GROUP_NONE);
3204 if (ret)
3205 dev_err(swrm->dev,
3206 "%s: set num ch failed\n",
3207 __func__);
3208 }
3209 }
3210 mutex_unlock(&swrm->mlock);
3211 }
3212 break;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303213 case SWR_REGISTER_WAKE_IRQ:
3214 if (!data) {
3215 dev_err(swrm->dev, "%s: reg wake irq data is NULL\n",
3216 __func__);
3217 ret = -EINVAL;
3218 } else {
3219 mutex_lock(&swrm->mlock);
3220 swrm->ipc_wakeup = *(u32 *)data;
3221 ret = swrm_register_wake_irq(swrm);
3222 if (ret)
3223 dev_err(swrm->dev, "%s: register wake_irq failed\n",
3224 __func__);
3225 mutex_unlock(&swrm->mlock);
3226 }
3227 break;
Sudheer Papothi72ee2642019-08-08 05:15:17 +05303228 case SWR_REGISTER_WAKEUP:
3229 msm_aud_evt_blocking_notifier_call_chain(
3230 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
3231 break;
3232 case SWR_DEREGISTER_WAKEUP:
3233 msm_aud_evt_blocking_notifier_call_chain(
3234 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
3235 break;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05303236 case SWR_SET_PORT_MAP:
3237 if (!data) {
3238 dev_err(swrm->dev, "%s: data is NULL for id=%d\n",
3239 __func__, id);
3240 ret = -EINVAL;
3241 } else {
3242 mutex_lock(&swrm->mlock);
3243 port_cfg = (struct swrm_port_config *)data;
3244 if (!port_cfg->size) {
3245 ret = -EINVAL;
3246 goto done;
3247 }
3248 ret = swrm_alloc_port_mem(&pdev->dev, swrm,
3249 port_cfg->uc, port_cfg->size);
3250 if (!ret)
3251 swrm_copy_port_config(swrm, port_cfg,
3252 port_cfg->size);
3253done:
3254 mutex_unlock(&swrm->mlock);
3255 }
3256 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303257 default:
3258 dev_err(swrm->dev, "%s: swr master unknown id %d\n",
3259 __func__, id);
3260 break;
3261 }
3262 return ret;
3263}
3264EXPORT_SYMBOL(swrm_wcd_notify);
3265
Ramprasad Katkam57349872018-11-11 18:34:57 +05303266/*
3267 * swrm_pm_cmpxchg:
3268 * Check old state and exchange with pm new state
3269 * if old state matches with current state
3270 *
3271 * @swrm: pointer to wcd core resource
3272 * @o: pm old state
3273 * @n: pm new state
3274 *
3275 * Returns old state
3276 */
3277static enum swrm_pm_state swrm_pm_cmpxchg(
3278 struct swr_mstr_ctrl *swrm,
3279 enum swrm_pm_state o,
3280 enum swrm_pm_state n)
3281{
3282 enum swrm_pm_state old;
3283
3284 if (!swrm)
3285 return o;
3286
3287 mutex_lock(&swrm->pm_lock);
3288 old = swrm->pm_state;
3289 if (old == o)
3290 swrm->pm_state = n;
3291 mutex_unlock(&swrm->pm_lock);
3292
3293 return old;
3294}
3295
3296static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm)
3297{
3298 enum swrm_pm_state os;
3299
3300 /*
3301 * swrm_{lock/unlock}_sleep will be called by swr irq handler
3302 * and slave wake up requests..
3303 *
3304 * If system didn't resume, we can simply return false so
3305 * IRQ handler can return without handling IRQ.
3306 */
3307 mutex_lock(&swrm->pm_lock);
3308 if (swrm->wlock_holders++ == 0) {
3309 dev_dbg(swrm->dev, "%s: holding wake lock\n", __func__);
3310 pm_qos_update_request(&swrm->pm_qos_req,
3311 msm_cpuidle_get_deep_idle_latency());
3312 pm_stay_awake(swrm->dev);
3313 }
3314 mutex_unlock(&swrm->pm_lock);
3315
3316 if (!wait_event_timeout(swrm->pm_wq,
3317 ((os = swrm_pm_cmpxchg(swrm,
3318 SWRM_PM_SLEEPABLE,
3319 SWRM_PM_AWAKE)) ==
3320 SWRM_PM_SLEEPABLE ||
3321 (os == SWRM_PM_AWAKE)),
3322 msecs_to_jiffies(
3323 SWRM_SYSTEM_RESUME_TIMEOUT_MS))) {
3324 dev_err(swrm->dev, "%s: system didn't resume within %dms, s %d, w %d\n",
3325 __func__, SWRM_SYSTEM_RESUME_TIMEOUT_MS, swrm->pm_state,
3326 swrm->wlock_holders);
3327 swrm_unlock_sleep(swrm);
3328 return false;
3329 }
3330 wake_up_all(&swrm->pm_wq);
3331 return true;
3332}
3333
3334static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm)
3335{
3336 mutex_lock(&swrm->pm_lock);
3337 if (--swrm->wlock_holders == 0) {
3338 dev_dbg(swrm->dev, "%s: releasing wake lock pm_state %d -> %d\n",
3339 __func__, swrm->pm_state, SWRM_PM_SLEEPABLE);
3340 /*
3341 * if swrm_lock_sleep failed, pm_state would be still
3342 * swrm_PM_ASLEEP, don't overwrite
3343 */
3344 if (likely(swrm->pm_state == SWRM_PM_AWAKE))
3345 swrm->pm_state = SWRM_PM_SLEEPABLE;
3346 pm_qos_update_request(&swrm->pm_qos_req,
3347 PM_QOS_DEFAULT_VALUE);
3348 pm_relax(swrm->dev);
3349 }
3350 mutex_unlock(&swrm->pm_lock);
3351 wake_up_all(&swrm->pm_wq);
3352}
3353
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303354#ifdef CONFIG_PM_SLEEP
3355static int swrm_suspend(struct device *dev)
3356{
3357 int ret = -EBUSY;
3358 struct platform_device *pdev = to_platform_device(dev);
3359 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3360
3361 dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
Ramprasad Katkam57349872018-11-11 18:34:57 +05303362
3363 mutex_lock(&swrm->pm_lock);
3364
3365 if (swrm->pm_state == SWRM_PM_SLEEPABLE) {
3366 dev_dbg(swrm->dev, "%s: suspending system, state %d, wlock %d\n",
3367 __func__, swrm->pm_state,
3368 swrm->wlock_holders);
3369 swrm->pm_state = SWRM_PM_ASLEEP;
3370 } else if (swrm->pm_state == SWRM_PM_AWAKE) {
3371 /*
3372 * unlock to wait for pm_state == SWRM_PM_SLEEPABLE
3373 * then set to SWRM_PM_ASLEEP
3374 */
3375 dev_dbg(swrm->dev, "%s: waiting to suspend system, state %d, wlock %d\n",
3376 __func__, swrm->pm_state,
3377 swrm->wlock_holders);
3378 mutex_unlock(&swrm->pm_lock);
3379 if (!(wait_event_timeout(swrm->pm_wq, swrm_pm_cmpxchg(
3380 swrm, SWRM_PM_SLEEPABLE,
3381 SWRM_PM_ASLEEP) ==
3382 SWRM_PM_SLEEPABLE,
3383 msecs_to_jiffies(
3384 SWRM_SYS_SUSPEND_WAIT)))) {
3385 dev_dbg(swrm->dev, "%s: suspend failed state %d, wlock %d\n",
3386 __func__, swrm->pm_state,
3387 swrm->wlock_holders);
3388 return -EBUSY;
3389 } else {
3390 dev_dbg(swrm->dev,
3391 "%s: done, state %d, wlock %d\n",
3392 __func__, swrm->pm_state,
3393 swrm->wlock_holders);
3394 }
3395 mutex_lock(&swrm->pm_lock);
3396 } else if (swrm->pm_state == SWRM_PM_ASLEEP) {
3397 dev_dbg(swrm->dev, "%s: system is already suspended, state %d, wlock %d\n",
3398 __func__, swrm->pm_state,
3399 swrm->wlock_holders);
3400 }
3401
3402 mutex_unlock(&swrm->pm_lock);
3403
3404 if ((!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev))) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303405 ret = swrm_runtime_suspend(dev);
3406 if (!ret) {
3407 /*
3408 * Synchronize runtime-pm and system-pm states:
3409 * At this point, we are already suspended. If
3410 * runtime-pm still thinks its active, then
3411 * make sure its status is in sync with HW
3412 * status. The three below calls let the
3413 * runtime-pm know that we are suspended
3414 * already without re-invoking the suspend
3415 * callback
3416 */
3417 pm_runtime_disable(dev);
3418 pm_runtime_set_suspended(dev);
3419 pm_runtime_enable(dev);
3420 }
3421 }
3422 if (ret == -EBUSY) {
3423 /*
3424 * There is a possibility that some audio stream is active
3425 * during suspend. We dont want to return suspend failure in
3426 * that case so that display and relevant components can still
3427 * go to suspend.
3428 * If there is some other error, then it should be passed-on
3429 * to system level suspend
3430 */
3431 ret = 0;
3432 }
3433 return ret;
3434}
3435
3436static int swrm_resume(struct device *dev)
3437{
3438 int ret = 0;
3439 struct platform_device *pdev = to_platform_device(dev);
3440 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3441
3442 dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
3443 if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
3444 ret = swrm_runtime_resume(dev);
3445 if (!ret) {
3446 pm_runtime_mark_last_busy(dev);
3447 pm_request_autosuspend(dev);
3448 }
3449 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05303450 mutex_lock(&swrm->pm_lock);
3451 if (swrm->pm_state == SWRM_PM_ASLEEP) {
3452 dev_dbg(swrm->dev,
3453 "%s: resuming system, state %d, wlock %d\n",
3454 __func__, swrm->pm_state,
3455 swrm->wlock_holders);
3456 swrm->pm_state = SWRM_PM_SLEEPABLE;
3457 } else {
3458 dev_dbg(swrm->dev, "%s: system is already awake, state %d wlock %d\n",
3459 __func__, swrm->pm_state,
3460 swrm->wlock_holders);
3461 }
3462 mutex_unlock(&swrm->pm_lock);
3463 wake_up_all(&swrm->pm_wq);
3464
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303465 return ret;
3466}
3467#endif /* CONFIG_PM_SLEEP */
3468
3469static const struct dev_pm_ops swrm_dev_pm_ops = {
3470 SET_SYSTEM_SLEEP_PM_OPS(
3471 swrm_suspend,
3472 swrm_resume
3473 )
3474 SET_RUNTIME_PM_OPS(
3475 swrm_runtime_suspend,
3476 swrm_runtime_resume,
3477 NULL
3478 )
3479};
3480
3481static const struct of_device_id swrm_dt_match[] = {
3482 {
3483 .compatible = "qcom,swr-mstr",
3484 },
3485 {}
3486};
3487
3488static struct platform_driver swr_mstr_driver = {
3489 .probe = swrm_probe,
3490 .remove = swrm_remove,
3491 .driver = {
3492 .name = SWR_WCD_NAME,
3493 .owner = THIS_MODULE,
3494 .pm = &swrm_dev_pm_ops,
3495 .of_match_table = swrm_dt_match,
Xiaojun Sang53cd13a2018-06-29 15:14:37 +08003496 .suppress_bind_attrs = true,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303497 },
3498};
3499
3500static int __init swrm_init(void)
3501{
3502 return platform_driver_register(&swr_mstr_driver);
3503}
3504module_init(swrm_init);
3505
3506static void __exit swrm_exit(void)
3507{
3508 platform_driver_unregister(&swr_mstr_driver);
3509}
3510module_exit(swrm_exit);
3511
3512MODULE_LICENSE("GPL v2");
3513MODULE_DESCRIPTION("SoundWire Master Controller");
3514MODULE_ALIAS("platform:swr-mstr");