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