blob: 44d245d6dfb2b6f0f133e5dd1f7512b3369995af [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
Ramprasad Katkam57349872018-11-11 18:34:57 +053028#define SWRM_SYSTEM_RESUME_TIMEOUT_MS 700
29#define SWRM_SYS_SUSPEND_WAIT 1
Sudheer Papothi3d1596e2018-10-27 06:19:18 +053030
Sudheer Papothi4c322b12018-10-31 06:34:01 +053031#define SWRM_DSD_PARAMS_PORT 4
32
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053033#define SWR_BROADCAST_CMD_ID 0x0F
Sudheer Papothi3590b312019-06-04 23:51:30 +053034#define SWR_AUTO_SUSPEND_DELAY 1 /* delay in sec */
Sudheer Papothi7c067e82018-11-15 06:53:35 +053035#define SWR_DEV_ID_MASK 0xFFFFFFFFFFFF
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053036#define SWR_REG_VAL_PACK(data, dev, id, reg) \
37 ((reg) | ((id) << 16) | ((dev) << 20) | ((data) << 24))
38
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +053039#define SWR_INVALID_PARAM 0xFF
Laxminath Kasam990c70b2018-11-09 23:15:09 +053040#define SWR_HSTOP_MAX_VAL 0xF
41#define SWR_HSTART_MIN_VAL 0x0
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +053042
Vatsal Buchae50b5002019-09-19 14:32:20 +053043#define ERR_AUTO_SUSPEND_TIMER_VAL 0x1
44
Ramprasad Katkam83303512018-10-11 17:34:22 +053045#define SWRM_INTERRUPT_STATUS_MASK 0x1FDFD
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053046/* pm runtime auto suspend timer in msecs */
47static int auto_suspend_timer = SWR_AUTO_SUSPEND_DELAY * 1000;
48module_param(auto_suspend_timer, int, 0664);
49MODULE_PARM_DESC(auto_suspend_timer, "timer for auto suspend");
50
51enum {
52 SWR_NOT_PRESENT, /* Device is detached/not present on the bus */
53 SWR_ATTACHED_OK, /* Device is attached */
54 SWR_ALERT, /* Device alters master for any interrupts */
55 SWR_RESERVED, /* Reserved */
56};
57
58enum {
59 MASTER_ID_WSA = 1,
60 MASTER_ID_RX,
61 MASTER_ID_TX
62};
Ramprasad Katkamcab8d722018-09-28 15:54:06 +053063
64enum {
65 ENABLE_PENDING,
66 DISABLE_PENDING
67};
Sudheer Papothi384addd2019-06-14 02:26:52 +053068
69enum {
70 LPASS_HW_CORE,
71 LPASS_AUDIO_CORE,
72};
73
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053074#define TRUE 1
75#define FALSE 0
76
Ramprasad Katkam1f221262018-08-23 15:01:22 +053077#define SWRM_MAX_PORT_REG 120
Ramprasad Katkam83303512018-10-11 17:34:22 +053078#define SWRM_MAX_INIT_REG 11
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053079
Laxminath Kasamfbcaf322018-07-18 00:38:14 +053080#define MAX_FIFO_RD_FAIL_RETRY 3
81
Ramprasad Katkam57349872018-11-11 18:34:57 +053082static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm);
83static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm);
Sudheer Papothi96c842a2019-08-29 12:11:21 +053084static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr);
85static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053086
87static bool swrm_is_msm_variant(int val)
88{
89 return (val == SWRM_VERSION_1_3);
90}
91
Sudheer Papothi96c842a2019-08-29 12:11:21 +053092#ifdef CONFIG_DEBUG_FS
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053093static int swrm_debug_open(struct inode *inode, struct file *file)
94{
95 file->private_data = inode->i_private;
96 return 0;
97}
98
99static int get_parameters(char *buf, u32 *param1, int num_of_par)
100{
101 char *token;
102 int base, cnt;
103
104 token = strsep(&buf, " ");
105 for (cnt = 0; cnt < num_of_par; cnt++) {
106 if (token) {
107 if ((token[1] == 'x') || (token[1] == 'X'))
108 base = 16;
109 else
110 base = 10;
111
112 if (kstrtou32(token, base, &param1[cnt]) != 0)
113 return -EINVAL;
114
115 token = strsep(&buf, " ");
116 } else
117 return -EINVAL;
118 }
119 return 0;
120}
121
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530122static ssize_t swrm_reg_show(struct swr_mstr_ctrl *swrm, char __user *ubuf,
123 size_t count, loff_t *ppos)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530124{
125 int i, reg_val, len;
126 ssize_t total = 0;
127 char tmp_buf[SWR_MSTR_MAX_BUF_LEN];
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530128 int rem = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530129
130 if (!ubuf || !ppos)
131 return 0;
132
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530133 i = ((int) *ppos + SWR_MSTR_START_REG_ADDR);
134 rem = i%4;
135
136 if (rem)
137 i = (i - rem);
138
139 for (; i <= SWR_MSTR_MAX_REG_ADDR; i += 4) {
140 usleep_range(100, 150);
141 reg_val = swr_master_read(swrm, i);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530142 len = snprintf(tmp_buf, 25, "0x%.3x: 0x%.2x\n", i, reg_val);
Aditya Bavanari9f599b42019-08-27 22:18:41 +0530143 if (len < 0) {
144 pr_err("%s: fail to fill the buffer\n", __func__);
145 total = -EFAULT;
146 goto copy_err;
147 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530148 if ((total + len) >= count - 1)
149 break;
150 if (copy_to_user((ubuf + total), tmp_buf, len)) {
151 pr_err("%s: fail to copy reg dump\n", __func__);
152 total = -EFAULT;
153 goto copy_err;
154 }
155 *ppos += len;
156 total += len;
157 }
158
159copy_err:
160 return total;
161}
162
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530163static ssize_t swrm_debug_reg_dump(struct file *file, char __user *ubuf,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530164 size_t count, loff_t *ppos)
165{
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530166 struct swr_mstr_ctrl *swrm;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530167
168 if (!count || !file || !ppos || !ubuf)
169 return -EINVAL;
170
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530171 swrm = file->private_data;
172 if (!swrm)
173 return -EINVAL;
174
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530175 if (*ppos < 0)
176 return -EINVAL;
177
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530178 return swrm_reg_show(swrm, ubuf, count, ppos);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530179}
180
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530181static ssize_t swrm_debug_read(struct file *file, char __user *ubuf,
182 size_t count, loff_t *ppos)
183{
184 char lbuf[SWR_MSTR_RD_BUF_LEN];
185 struct swr_mstr_ctrl *swrm = NULL;
186
187 if (!count || !file || !ppos || !ubuf)
188 return -EINVAL;
189
190 swrm = file->private_data;
191 if (!swrm)
192 return -EINVAL;
193
194 if (*ppos < 0)
195 return -EINVAL;
196
197 snprintf(lbuf, sizeof(lbuf), "0x%x\n", swrm->read_data);
198
199 return simple_read_from_buffer(ubuf, count, ppos, lbuf,
200 strnlen(lbuf, 7));
201}
202
203static ssize_t swrm_debug_peek_write(struct file *file, const char __user *ubuf,
204 size_t count, loff_t *ppos)
205{
206 char lbuf[SWR_MSTR_RD_BUF_LEN];
207 int rc;
208 u32 param[5];
209 struct swr_mstr_ctrl *swrm = NULL;
210
211 if (!count || !file || !ppos || !ubuf)
212 return -EINVAL;
213
214 swrm = file->private_data;
215 if (!swrm)
216 return -EINVAL;
217
218 if (*ppos < 0)
219 return -EINVAL;
220
221 if (count > sizeof(lbuf) - 1)
222 return -EINVAL;
223
224 rc = copy_from_user(lbuf, ubuf, count);
225 if (rc)
226 return -EFAULT;
227
228 lbuf[count] = '\0';
229 rc = get_parameters(lbuf, param, 1);
230 if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) && (rc == 0))
231 swrm->read_data = swr_master_read(swrm, param[0]);
232 else
233 rc = -EINVAL;
234
235 if (rc == 0)
236 rc = count;
237 else
238 dev_err(swrm->dev, "%s: rc = %d\n", __func__, rc);
239
240 return rc;
241}
242
243static ssize_t swrm_debug_write(struct file *file,
244 const char __user *ubuf, size_t count, loff_t *ppos)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530245{
246 char lbuf[SWR_MSTR_WR_BUF_LEN];
247 int rc;
248 u32 param[5];
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530249 struct swr_mstr_ctrl *swrm;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530250
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530251 if (!file || !ppos || !ubuf)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530252 return -EINVAL;
253
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530254 swrm = file->private_data;
255 if (!swrm)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530256 return -EINVAL;
257
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530258 if (count > sizeof(lbuf) - 1)
259 return -EINVAL;
260
261 rc = copy_from_user(lbuf, ubuf, count);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530262 if (rc)
263 return -EFAULT;
264
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530265 lbuf[count] = '\0';
266 rc = get_parameters(lbuf, param, 2);
267 if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) &&
268 (param[1] <= 0xFFFFFFFF) &&
269 (rc == 0))
270 swr_master_write(swrm, param[0], param[1]);
271 else
272 rc = -EINVAL;
273
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530274 if (rc == 0)
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530275 rc = count;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530276 else
277 pr_err("%s: rc = %d\n", __func__, rc);
278
279 return rc;
280}
281
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530282static const struct file_operations swrm_debug_read_ops = {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530283 .open = swrm_debug_open,
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530284 .write = swrm_debug_peek_write,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530285 .read = swrm_debug_read,
286};
287
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530288static const struct file_operations swrm_debug_write_ops = {
289 .open = swrm_debug_open,
290 .write = swrm_debug_write,
291};
292
293static const struct file_operations swrm_debug_dump_ops = {
294 .open = swrm_debug_open,
295 .read = swrm_debug_reg_dump,
296};
297#endif
298
Sudheer Papothi0016db12019-06-11 04:42:38 +0530299static void swrm_reg_dump(struct swr_mstr_ctrl *swrm,
300 u32 *reg, u32 *val, int len, const char* func)
301{
302 int i = 0;
303
304 for (i = 0; i < len; i++)
305 dev_dbg(swrm->dev, "%s: reg = 0x%x val = 0x%x\n",
306 func, reg[i], val[i]);
307}
308
Sudheer Papothi384addd2019-06-14 02:26:52 +0530309static int swrm_request_hw_vote(struct swr_mstr_ctrl *swrm,
310 int core_type, bool enable)
311{
312 int ret = 0;
313
314 if (core_type == LPASS_HW_CORE) {
315 if (swrm->lpass_core_hw_vote) {
316 if (enable) {
317 ret =
318 clk_prepare_enable(swrm->lpass_core_hw_vote);
319 if (ret < 0)
320 dev_err(swrm->dev,
321 "%s:lpass core hw enable failed\n",
322 __func__);
323 } else
324 clk_disable_unprepare(swrm->lpass_core_hw_vote);
325 }
326 }
327 if (core_type == LPASS_AUDIO_CORE) {
328 if (swrm->lpass_core_audio) {
329 if (enable) {
330 ret =
331 clk_prepare_enable(swrm->lpass_core_audio);
332 if (ret < 0)
333 dev_err(swrm->dev,
334 "%s:lpass audio hw enable failed\n",
335 __func__);
336 } else
337 clk_disable_unprepare(swrm->lpass_core_audio);
338 }
339 }
340
341 return ret;
342}
343
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530344static int swrm_clk_request(struct swr_mstr_ctrl *swrm, bool enable)
345{
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530346 int ret = 0;
347
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530348 if (!swrm->clk || !swrm->handle)
349 return -EINVAL;
350
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530351 mutex_lock(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530352 if (enable) {
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530353 if (!swrm->dev_up) {
354 ret = -ENODEV;
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530355 goto exit;
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530356 }
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -0700357 if (swrm->core_vote) {
358 ret = swrm->core_vote(swrm->handle, true);
359 if (ret) {
360 dev_err_ratelimited(swrm->dev,
361 "%s: clock enable req failed",
362 __func__);
363 goto exit;
364 }
365 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530366 swrm->clk_ref_count++;
367 if (swrm->clk_ref_count == 1) {
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530368 ret = swrm->clk(swrm->handle, true);
369 if (ret) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +0530370 dev_err_ratelimited(swrm->dev,
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530371 "%s: clock enable req failed",
372 __func__);
373 --swrm->clk_ref_count;
374 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530375 }
376 } else if (--swrm->clk_ref_count == 0) {
377 swrm->clk(swrm->handle, false);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530378 complete(&swrm->clk_off_complete);
379 }
380 if (swrm->clk_ref_count < 0) {
Meng Wang8c60bb52019-06-19 15:49:06 +0800381 dev_err(swrm->dev, "%s: swrm clk count mismatch\n", __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530382 swrm->clk_ref_count = 0;
383 }
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530384
385exit:
386 mutex_unlock(&swrm->clklock);
387 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530388}
389
390static int swrm_ahb_write(struct swr_mstr_ctrl *swrm,
391 u16 reg, u32 *value)
392{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530393 u32 temp = (u32)(*value);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530394 int ret = 0;
395
396 mutex_lock(&swrm->devlock);
397 if (!swrm->dev_up)
398 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530399
400 ret = swrm_clk_request(swrm, TRUE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530401 if (ret) {
402 dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
403 __func__);
404 goto err;
405 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530406 iowrite32(temp, swrm->swrm_dig_base + reg);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530407 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530408err:
409 mutex_unlock(&swrm->devlock);
410 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530411}
412
413static int swrm_ahb_read(struct swr_mstr_ctrl *swrm,
414 u16 reg, u32 *value)
415{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530416 u32 temp = 0;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530417 int ret = 0;
418
419 mutex_lock(&swrm->devlock);
420 if (!swrm->dev_up)
421 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530422
423 ret = swrm_clk_request(swrm, TRUE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530424 if (ret) {
425 dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
426 __func__);
427 goto err;
428 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530429 temp = ioread32(swrm->swrm_dig_base + reg);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530430 *value = temp;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530431 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530432err:
433 mutex_unlock(&swrm->devlock);
434 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530435}
436
437static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr)
438{
439 u32 val = 0;
440
441 if (swrm->read)
442 val = swrm->read(swrm->handle, reg_addr);
443 else
444 swrm_ahb_read(swrm, reg_addr, &val);
445 return val;
446}
447
448static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val)
449{
450 if (swrm->write)
451 swrm->write(swrm->handle, reg_addr, val);
452 else
453 swrm_ahb_write(swrm, reg_addr, &val);
454}
455
456static int swr_master_bulk_write(struct swr_mstr_ctrl *swrm, u32 *reg_addr,
457 u32 *val, unsigned int length)
458{
459 int i = 0;
460
461 if (swrm->bulk_write)
462 swrm->bulk_write(swrm->handle, reg_addr, val, length);
463 else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530464 mutex_lock(&swrm->iolock);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530465 for (i = 0; i < length; i++) {
466 /* wait for FIFO WR command to complete to avoid overflow */
Karthikeyan Mani5d52dd82019-08-15 16:58:08 -0700467 /*
468 * Reduce sleep from 100us to 10us to meet KPIs
469 * This still meets the hardware spec
470 */
471 usleep_range(10, 12);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530472 swr_master_write(swrm, reg_addr[i], val[i]);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530473 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530474 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530475 }
476 return 0;
477}
478
479static bool swrm_is_port_en(struct swr_master *mstr)
480{
481 return !!(mstr->num_port);
482}
483
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530484static void copy_port_tables(struct swr_mstr_ctrl *swrm,
485 struct port_params *params)
486{
487 u8 i;
488 struct port_params *config = params;
489
490 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
491 /* wsa uses single frame structure for all configurations */
492 if (!swrm->mport_cfg[i].port_en)
493 continue;
494 swrm->mport_cfg[i].sinterval = config[i].si;
495 swrm->mport_cfg[i].offset1 = config[i].off1;
496 swrm->mport_cfg[i].offset2 = config[i].off2;
497 swrm->mport_cfg[i].hstart = config[i].hstart;
498 swrm->mport_cfg[i].hstop = config[i].hstop;
499 swrm->mport_cfg[i].blk_pack_mode = config[i].bp_mode;
500 swrm->mport_cfg[i].blk_grp_count = config[i].bgp_ctrl;
501 swrm->mport_cfg[i].word_length = config[i].wd_len;
502 swrm->mport_cfg[i].lane_ctrl = config[i].lane_ctrl;
503 }
504}
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530505static int swrm_get_port_config(struct swr_mstr_ctrl *swrm)
506{
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530507 struct port_params *params;
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530508 u32 usecase = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530509
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530510 /* TODO - Send usecase information to avoid checking for master_id */
511 if (swrm->mport_cfg[SWRM_DSD_PARAMS_PORT].port_en &&
512 (swrm->master_id == MASTER_ID_RX))
513 usecase = 1;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530514
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530515 params = swrm->port_param[usecase];
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530516 copy_port_tables(swrm, params);
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530517
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530518 return 0;
519}
520
521static int swrm_get_master_port(struct swr_mstr_ctrl *swrm, u8 *mstr_port_id,
522 u8 *mstr_ch_mask, u8 mstr_prt_type,
523 u8 slv_port_id)
524{
525 int i, j;
526 *mstr_port_id = 0;
527
528 for (i = 1; i <= swrm->num_ports; i++) {
529 for (j = 0; j < SWR_MAX_CH_PER_PORT; j++) {
530 if (swrm->port_mapping[i][j].port_type == mstr_prt_type)
531 goto found;
532 }
533 }
534found:
535 if (i > swrm->num_ports || j == SWR_MAX_CH_PER_PORT) {
536 dev_err(swrm->dev, "%s: port type not supported by master\n",
537 __func__);
538 return -EINVAL;
539 }
540 /* id 0 corresponds to master port 1 */
541 *mstr_port_id = i - 1;
542 *mstr_ch_mask = swrm->port_mapping[i][j].ch_mask;
543
544 return 0;
545
546}
547
548static u32 swrm_get_packed_reg_val(u8 *cmd_id, u8 cmd_data,
549 u8 dev_addr, u16 reg_addr)
550{
551 u32 val;
552 u8 id = *cmd_id;
553
554 if (id != SWR_BROADCAST_CMD_ID) {
555 if (id < 14)
556 id += 1;
557 else
558 id = 0;
559 *cmd_id = id;
560 }
561 val = SWR_REG_VAL_PACK(cmd_data, dev_addr, id, reg_addr);
562
563 return val;
564}
565
566static int swrm_cmd_fifo_rd_cmd(struct swr_mstr_ctrl *swrm, int *cmd_data,
567 u8 dev_addr, u8 cmd_id, u16 reg_addr,
568 u32 len)
569{
570 u32 val;
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530571 u32 retry_attempt = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530572
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530573 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530574 val = swrm_get_packed_reg_val(&swrm->rcmd_id, len, dev_addr, reg_addr);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530575 if (swrm->read) {
576 /* skip delay if read is handled in platform driver */
577 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
578 } else {
579 /* wait for FIFO RD to complete to avoid overflow */
580 usleep_range(100, 105);
581 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
582 /* wait for FIFO RD CMD complete to avoid overflow */
583 usleep_range(250, 255);
584 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530585retry_read:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530586 *cmd_data = swr_master_read(swrm, SWRM_CMD_FIFO_RD_FIFO_ADDR);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530587 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, rcmd_id: 0x%x, \
588 dev_num: 0x%x, cmd_data: 0x%x\n", __func__, reg_addr,
589 cmd_id, swrm->rcmd_id, dev_addr, *cmd_data);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530590 if ((((*cmd_data) & 0xF00) >> 8) != swrm->rcmd_id) {
591 if (retry_attempt < MAX_FIFO_RD_FAIL_RETRY) {
592 /* wait 500 us before retry on fifo read failure */
593 usleep_range(500, 505);
594 retry_attempt++;
595 goto retry_read;
596 } else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530597 dev_err_ratelimited(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, \
598 rcmd_id: 0x%x, dev_num: 0x%x, cmd_data: 0x%x\n",
599 __func__, reg_addr, cmd_id, swrm->rcmd_id,
600 dev_addr, *cmd_data);
601
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530602 dev_err_ratelimited(swrm->dev,
603 "%s: failed to read fifo\n", __func__);
604 }
605 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530606 mutex_unlock(&swrm->iolock);
607
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530608 return 0;
609}
610
611static int swrm_cmd_fifo_wr_cmd(struct swr_mstr_ctrl *swrm, u8 cmd_data,
612 u8 dev_addr, u8 cmd_id, u16 reg_addr)
613{
614 u32 val;
615 int ret = 0;
616
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530617 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530618 if (!cmd_id)
619 val = swrm_get_packed_reg_val(&swrm->wcmd_id, cmd_data,
620 dev_addr, reg_addr);
621 else
622 val = swrm_get_packed_reg_val(&cmd_id, cmd_data,
623 dev_addr, reg_addr);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530624 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x,wcmd_id: 0x%x, \
625 dev_num: 0x%x, cmd_data: 0x%x\n", __func__,
626 reg_addr, cmd_id, swrm->wcmd_id,dev_addr, cmd_data);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +0530627 swr_master_write(swrm, SWRM_CMD_FIFO_WR_CMD, val);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530628 /*
629 * wait for FIFO WR command to complete to avoid overflow
630 * skip delay if write is handled in platform driver.
631 */
632 if(!swrm->write)
Karthikeyan Mani5d52dd82019-08-15 16:58:08 -0700633 usleep_range(150, 155);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530634 if (cmd_id == 0xF) {
635 /*
636 * sleep for 10ms for MSM soundwire variant to allow broadcast
637 * command to complete.
638 */
639 if (swrm_is_msm_variant(swrm->version))
640 usleep_range(10000, 10100);
641 else
642 wait_for_completion_timeout(&swrm->broadcast,
643 (2 * HZ/10));
644 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530645 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530646 return ret;
647}
648
649static int swrm_read(struct swr_master *master, u8 dev_num, u16 reg_addr,
650 void *buf, u32 len)
651{
652 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
653 int ret = 0;
654 int val;
655 u8 *reg_val = (u8 *)buf;
656
657 if (!swrm) {
658 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
659 return -EINVAL;
660 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530661 if (!dev_num) {
662 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
663 return -EINVAL;
664 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530665 mutex_lock(&swrm->devlock);
666 if (!swrm->dev_up) {
667 mutex_unlock(&swrm->devlock);
668 return 0;
669 }
670 mutex_unlock(&swrm->devlock);
671
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530672 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530673 ret = swrm_cmd_fifo_rd_cmd(swrm, &val, dev_num, 0, reg_addr, len);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530674
675 if (!ret)
676 *reg_val = (u8)val;
677
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530678 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530679 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530680 return ret;
681}
682
683static int swrm_write(struct swr_master *master, u8 dev_num, u16 reg_addr,
684 const void *buf)
685{
686 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
687 int ret = 0;
688 u8 reg_val = *(u8 *)buf;
689
690 if (!swrm) {
691 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
692 return -EINVAL;
693 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530694 if (!dev_num) {
695 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
696 return -EINVAL;
697 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530698 mutex_lock(&swrm->devlock);
699 if (!swrm->dev_up) {
700 mutex_unlock(&swrm->devlock);
701 return 0;
702 }
703 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530704
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530705 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530706 ret = swrm_cmd_fifo_wr_cmd(swrm, reg_val, dev_num, 0, reg_addr);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530707
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530708 pm_runtime_put_autosuspend(swrm->dev);
709 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530710 return ret;
711}
712
713static int swrm_bulk_write(struct swr_master *master, u8 dev_num, void *reg,
714 const void *buf, size_t len)
715{
716 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
717 int ret = 0;
718 int i;
719 u32 *val;
720 u32 *swr_fifo_reg;
721
722 if (!swrm || !swrm->handle) {
723 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
724 return -EINVAL;
725 }
726 if (len <= 0)
727 return -EINVAL;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530728 mutex_lock(&swrm->devlock);
729 if (!swrm->dev_up) {
730 mutex_unlock(&swrm->devlock);
731 return 0;
732 }
733 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530734
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530735 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530736 if (dev_num) {
737 swr_fifo_reg = kcalloc(len, sizeof(u32), GFP_KERNEL);
738 if (!swr_fifo_reg) {
739 ret = -ENOMEM;
740 goto err;
741 }
742 val = kcalloc(len, sizeof(u32), GFP_KERNEL);
743 if (!val) {
744 ret = -ENOMEM;
745 goto mem_fail;
746 }
747
748 for (i = 0; i < len; i++) {
749 val[i] = swrm_get_packed_reg_val(&swrm->wcmd_id,
750 ((u8 *)buf)[i],
751 dev_num,
752 ((u16 *)reg)[i]);
753 swr_fifo_reg[i] = SWRM_CMD_FIFO_WR_CMD;
754 }
755 ret = swr_master_bulk_write(swrm, swr_fifo_reg, val, len);
756 if (ret) {
757 dev_err(&master->dev, "%s: bulk write failed\n",
758 __func__);
759 ret = -EINVAL;
760 }
761 } else {
762 dev_err(&master->dev,
763 "%s: No support of Bulk write for master regs\n",
764 __func__);
765 ret = -EINVAL;
766 goto err;
767 }
768 kfree(val);
769mem_fail:
770 kfree(swr_fifo_reg);
771err:
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530772 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530773 pm_runtime_mark_last_busy(swrm->dev);
774 return ret;
775}
776
777static u8 get_inactive_bank_num(struct swr_mstr_ctrl *swrm)
778{
779 return (swr_master_read(swrm, SWRM_MCP_STATUS) &
780 SWRM_MCP_STATUS_BANK_NUM_MASK) ? 0 : 1;
781}
782
783static void enable_bank_switch(struct swr_mstr_ctrl *swrm, u8 bank,
784 u8 row, u8 col)
785{
786 swrm_cmd_fifo_wr_cmd(swrm, ((row << 3) | col), 0xF, 0xF,
787 SWRS_SCP_FRAME_CTRL_BANK(bank));
788}
789
790static struct swr_port_info *swrm_get_port_req(struct swrm_mports *mport,
791 u8 slv_port, u8 dev_num)
792{
793 struct swr_port_info *port_req = NULL;
794
795 list_for_each_entry(port_req, &mport->port_req_list, list) {
796 /* Store dev_id instead of dev_num if enumeration is changed run_time */
797 if ((port_req->slave_port_id == slv_port)
798 && (port_req->dev_num == dev_num))
799 return port_req;
800 }
801 return NULL;
802}
803
804static bool swrm_remove_from_group(struct swr_master *master)
805{
806 struct swr_device *swr_dev;
807 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
808 bool is_removed = false;
809
810 if (!swrm)
811 goto end;
812
813 mutex_lock(&swrm->mlock);
814 if ((swrm->num_rx_chs > 1) &&
815 (swrm->num_rx_chs == swrm->num_cfg_devs)) {
816 list_for_each_entry(swr_dev, &master->devices,
817 dev_list) {
818 swr_dev->group_id = SWR_GROUP_NONE;
819 master->gr_sid = 0;
820 }
821 is_removed = true;
822 }
823 mutex_unlock(&swrm->mlock);
824
825end:
826 return is_removed;
827}
828
829static void swrm_disable_ports(struct swr_master *master,
830 u8 bank)
831{
832 u32 value;
833 struct swr_port_info *port_req;
834 int i;
835 struct swrm_mports *mport;
836 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
837
838 if (!swrm) {
839 pr_err("%s: swrm is null\n", __func__);
840 return;
841 }
842
843 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
844 master->num_port);
845
846
847 for (i = 0; i < SWR_MSTR_PORT_LEN ; i++) {
848
849 mport = &(swrm->mport_cfg[i]);
850 if (!mport->port_en)
851 continue;
852
853 list_for_each_entry(port_req, &mport->port_req_list, list) {
854 /* skip ports with no change req's*/
855 if (port_req->req_ch == port_req->ch_en)
856 continue;
857
858 swrm_cmd_fifo_wr_cmd(swrm, port_req->req_ch,
859 port_req->dev_num, 0x00,
860 SWRS_DP_CHANNEL_ENABLE_BANK(port_req->slave_port_id,
861 bank));
862 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x\n",
863 __func__, i,
864 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)));
865 }
866 value = ((mport->req_ch)
867 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
868 value |= ((mport->offset2)
869 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
870 value |= ((mport->offset1)
871 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
872 value |= mport->sinterval;
873
874 swr_master_write(swrm,
875 SWRM_DP_PORT_CTRL_BANK(i+1, bank),
876 value);
877 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
878 __func__, i,
879 (SWRM_DP_PORT_CTRL_BANK(i+1, bank)), value);
880 }
881}
882
883static void swrm_cleanup_disabled_port_reqs(struct swr_master *master)
884{
885 struct swr_port_info *port_req, *next;
886 int i;
887 struct swrm_mports *mport;
888 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
889
890 if (!swrm) {
891 pr_err("%s: swrm is null\n", __func__);
892 return;
893 }
894 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
895 master->num_port);
896
897 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
898 mport = &(swrm->mport_cfg[i]);
899 list_for_each_entry_safe(port_req, next,
900 &mport->port_req_list, list) {
901 /* skip ports without new ch req */
902 if (port_req->ch_en == port_req->req_ch)
903 continue;
904
905 /* remove new ch req's*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +0530906 port_req->ch_en = port_req->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530907
908 /* If no streams enabled on port, remove the port req */
909 if (port_req->ch_en == 0) {
910 list_del(&port_req->list);
911 kfree(port_req);
912 }
913 }
914 /* remove new ch req's on mport*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +0530915 mport->ch_en = mport->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530916
917 if (!(mport->ch_en)) {
918 mport->port_en = false;
919 master->port_en_mask &= ~i;
920 }
921 }
922}
923static void swrm_copy_data_port_config(struct swr_master *master, u8 bank)
924{
925 u32 value, slv_id;
926 struct swr_port_info *port_req;
927 int i;
928 struct swrm_mports *mport;
929 u32 reg[SWRM_MAX_PORT_REG];
930 u32 val[SWRM_MAX_PORT_REG];
931 int len = 0;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530932 u8 hparams;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530933 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
934
935 if (!swrm) {
936 pr_err("%s: swrm is null\n", __func__);
937 return;
938 }
939
940 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
941 master->num_port);
942
943 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
944 mport = &(swrm->mport_cfg[i]);
945 if (!mport->port_en)
946 continue;
947
948 list_for_each_entry(port_req, &mport->port_req_list, list) {
949 slv_id = port_req->slave_port_id;
950 reg[len] = SWRM_CMD_FIFO_WR_CMD;
951 val[len++] = SWR_REG_VAL_PACK(port_req->req_ch,
952 port_req->dev_num, 0x00,
953 SWRS_DP_CHANNEL_ENABLE_BANK(slv_id,
954 bank));
955
956 reg[len] = SWRM_CMD_FIFO_WR_CMD;
957 val[len++] = SWR_REG_VAL_PACK(mport->sinterval,
958 port_req->dev_num, 0x00,
959 SWRS_DP_SAMPLE_CONTROL_1_BANK(slv_id,
960 bank));
961
962 reg[len] = SWRM_CMD_FIFO_WR_CMD;
963 val[len++] = SWR_REG_VAL_PACK(mport->offset1,
964 port_req->dev_num, 0x00,
965 SWRS_DP_OFFSET_CONTROL_1_BANK(slv_id,
966 bank));
967
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530968 if (mport->offset2 != SWR_INVALID_PARAM) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530969 reg[len] = SWRM_CMD_FIFO_WR_CMD;
970 val[len++] = SWR_REG_VAL_PACK(mport->offset2,
971 port_req->dev_num, 0x00,
972 SWRS_DP_OFFSET_CONTROL_2_BANK(
973 slv_id, bank));
974 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530975 if (mport->hstart != SWR_INVALID_PARAM
976 && mport->hstop != SWR_INVALID_PARAM) {
977 hparams = (mport->hstart << 4) | mport->hstop;
978
979 reg[len] = SWRM_CMD_FIFO_WR_CMD;
980 val[len++] = SWR_REG_VAL_PACK(hparams,
981 port_req->dev_num, 0x00,
982 SWRS_DP_HCONTROL_BANK(slv_id,
983 bank));
984 }
985 if (mport->word_length != SWR_INVALID_PARAM) {
986 reg[len] = SWRM_CMD_FIFO_WR_CMD;
987 val[len++] =
988 SWR_REG_VAL_PACK(mport->word_length,
989 port_req->dev_num, 0x00,
990 SWRS_DP_BLOCK_CONTROL_1(slv_id));
991 }
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +0530992 if (mport->blk_pack_mode != SWR_INVALID_PARAM
993 && swrm->master_id != MASTER_ID_WSA) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530994 reg[len] = SWRM_CMD_FIFO_WR_CMD;
995 val[len++] =
996 SWR_REG_VAL_PACK(mport->blk_pack_mode,
997 port_req->dev_num, 0x00,
998 SWRS_DP_BLOCK_CONTROL_3_BANK(slv_id,
999 bank));
1000 }
1001 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
1002 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1003 val[len++] =
1004 SWR_REG_VAL_PACK(mport->blk_grp_count,
1005 port_req->dev_num, 0x00,
1006 SWRS_DP_BLOCK_CONTROL_2_BANK(slv_id,
1007 bank));
1008 }
1009 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
1010 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1011 val[len++] =
1012 SWR_REG_VAL_PACK(mport->lane_ctrl,
1013 port_req->dev_num, 0x00,
1014 SWRS_DP_LANE_CONTROL_BANK(slv_id,
1015 bank));
1016 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301017 port_req->ch_en = port_req->req_ch;
1018 }
1019 value = ((mport->req_ch)
1020 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +05301021
1022 if (mport->offset2 != SWR_INVALID_PARAM)
1023 value |= ((mport->offset2)
1024 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301025 value |= ((mport->offset1)
1026 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
1027 value |= mport->sinterval;
1028
1029
1030 reg[len] = SWRM_DP_PORT_CTRL_BANK(i + 1, bank);
1031 val[len++] = value;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301032 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
1033 __func__, i,
1034 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)), value);
1035
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301036 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
1037 reg[len] = SWRM_DP_PORT_CTRL_2_BANK(i + 1, bank);
1038 val[len++] = mport->lane_ctrl;
1039 }
1040 if (mport->word_length != SWR_INVALID_PARAM) {
1041 reg[len] = SWRM_DP_BLOCK_CTRL_1(i + 1);
1042 val[len++] = mport->word_length;
1043 }
1044
1045 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
1046 reg[len] = SWRM_DP_BLOCK_CTRL2_BANK(i + 1, bank);
1047 val[len++] = mport->blk_grp_count;
1048 }
1049 if (mport->hstart != SWR_INVALID_PARAM
1050 && mport->hstop != SWR_INVALID_PARAM) {
1051 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
Laxminath Kasame30eef72018-11-05 17:40:09 +05301052 hparams = (mport->hstop << 4) | mport->hstart;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301053 val[len++] = hparams;
Laxminath Kasam990c70b2018-11-09 23:15:09 +05301054 } else {
1055 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
1056 hparams = (SWR_HSTOP_MAX_VAL << 4) | SWR_HSTART_MIN_VAL;
1057 val[len++] = hparams;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301058 }
1059 if (mport->blk_pack_mode != SWR_INVALID_PARAM) {
1060 reg[len] = SWRM_DP_BLOCK_CTRL3_BANK(i + 1, bank);
1061 val[len++] = mport->blk_pack_mode;
1062 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301063 mport->ch_en = mport->req_ch;
1064
1065 }
Sudheer Papothi0016db12019-06-11 04:42:38 +05301066 swrm_reg_dump(swrm, reg, val, len, __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301067 swr_master_bulk_write(swrm, reg, val, len);
1068}
1069
1070static void swrm_apply_port_config(struct swr_master *master)
1071{
1072 u8 bank;
1073 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1074
1075 if (!swrm) {
1076 pr_err("%s: Invalid handle to swr controller\n",
1077 __func__);
1078 return;
1079 }
1080
1081 bank = get_inactive_bank_num(swrm);
1082 dev_dbg(swrm->dev, "%s: enter bank: %d master_ports: %d\n",
1083 __func__, bank, master->num_port);
1084
1085
1086 swrm_cmd_fifo_wr_cmd(swrm, 0x01, 0xF, 0x00,
1087 SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(bank));
1088
1089 swrm_copy_data_port_config(master, bank);
1090}
1091
1092static int swrm_slvdev_datapath_control(struct swr_master *master, bool enable)
1093{
1094 u8 bank;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301095 u32 value, n_row, n_col;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301096 int ret;
1097 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1098 int mask = (SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK |
1099 SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK |
1100 SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_BMSK);
1101 u8 inactive_bank;
1102
1103 if (!swrm) {
1104 pr_err("%s: swrm is null\n", __func__);
1105 return -EFAULT;
1106 }
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301107
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301108 mutex_lock(&swrm->mlock);
1109
Ramprasad Katkam979b7c92019-05-17 15:31:21 +05301110 /*
1111 * During disable if master is already down, which implies an ssr/pdr
1112 * scenario, just mark ports as disabled and exit
1113 */
1114 if (swrm->state == SWR_MSTR_SSR && !enable) {
1115 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1116 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1117 __func__);
1118 goto exit;
1119 }
1120 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
1121 swrm_cleanup_disabled_port_reqs(master);
1122 if (!swrm_is_port_en(master)) {
1123 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1124 __func__);
1125 pm_runtime_mark_last_busy(swrm->dev);
1126 pm_runtime_put_autosuspend(swrm->dev);
1127 }
1128 goto exit;
1129 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301130 bank = get_inactive_bank_num(swrm);
1131
1132 if (enable) {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301133 if (!test_bit(ENABLE_PENDING, &swrm->port_req_pending)) {
1134 dev_dbg(swrm->dev, "%s:No pending connect port req\n",
1135 __func__);
1136 goto exit;
1137 }
1138 clear_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301139 ret = swrm_get_port_config(swrm);
1140 if (ret) {
1141 /* cannot accommodate ports */
1142 swrm_cleanup_disabled_port_reqs(master);
1143 mutex_unlock(&swrm->mlock);
1144 return -EINVAL;
1145 }
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301146 swr_master_write(swrm, SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301147 SWRM_INTERRUPT_STATUS_MASK);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301148 /* apply the new port config*/
1149 swrm_apply_port_config(master);
1150 } else {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301151 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1152 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1153 __func__);
1154 goto exit;
1155 }
1156 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301157 swrm_disable_ports(master, bank);
1158 }
1159 dev_dbg(swrm->dev, "%s: enable: %d, cfg_devs: %d\n",
1160 __func__, enable, swrm->num_cfg_devs);
1161
1162 if (enable) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301163 /* set col = 16 */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301164 n_col = SWR_MAX_COL;
1165 } else {
1166 /*
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301167 * Do not change to col = 2 if there are still active ports
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301168 */
1169 if (!master->num_port)
1170 n_col = SWR_MIN_COL;
1171 else
1172 n_col = SWR_MAX_COL;
1173 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301174 /* Use default 50 * x, frame shape. Change based on mclk */
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301175 if (swrm->mclk_freq == MCLK_FREQ_NATIVE) {
1176 dev_dbg(swrm->dev, "setting 64 x %d frameshape\n",
1177 n_col ? 16 : 2);
1178 n_row = SWR_ROW_64;
1179 } else {
1180 dev_dbg(swrm->dev, "setting 50 x %d frameshape\n",
1181 n_col ? 16 : 2);
1182 n_row = SWR_ROW_50;
1183 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301184 value = swr_master_read(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank));
1185 value &= (~mask);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301186 value |= ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301187 (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
1188 (0 << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
1189 swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1190
1191 dev_dbg(swrm->dev, "%s: regaddr: 0x%x, value: 0x%x\n", __func__,
1192 SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1193
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301194 enable_bank_switch(swrm, bank, n_row, n_col);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301195 inactive_bank = bank ? 0 : 1;
1196
1197 if (enable)
1198 swrm_copy_data_port_config(master, inactive_bank);
1199 else {
1200 swrm_disable_ports(master, inactive_bank);
1201 swrm_cleanup_disabled_port_reqs(master);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301202 }
1203 if (!swrm_is_port_en(master)) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301204 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1205 __func__);
1206 pm_runtime_mark_last_busy(swrm->dev);
1207 pm_runtime_put_autosuspend(swrm->dev);
1208 }
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301209exit:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301210 mutex_unlock(&swrm->mlock);
1211return 0;
1212}
1213
1214static int swrm_connect_port(struct swr_master *master,
1215 struct swr_params *portinfo)
1216{
1217 int i;
1218 struct swr_port_info *port_req;
1219 int ret = 0;
1220 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1221 struct swrm_mports *mport;
1222 u8 mstr_port_id, mstr_ch_msk;
1223
1224 dev_dbg(&master->dev, "%s: enter\n", __func__);
1225 if (!portinfo)
1226 return -EINVAL;
1227
1228 if (!swrm) {
1229 dev_err(&master->dev,
1230 "%s: Invalid handle to swr controller\n",
1231 __func__);
1232 return -EINVAL;
1233 }
1234
1235 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301236 mutex_lock(&swrm->devlock);
1237 if (!swrm->dev_up) {
1238 mutex_unlock(&swrm->devlock);
1239 mutex_unlock(&swrm->mlock);
1240 return -EINVAL;
1241 }
1242 mutex_unlock(&swrm->devlock);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301243 if (!swrm_is_port_en(master))
1244 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301245
1246 for (i = 0; i < portinfo->num_port; i++) {
1247 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_msk,
1248 portinfo->port_type[i],
1249 portinfo->port_id[i]);
1250 if (ret) {
1251 dev_err(&master->dev,
1252 "%s: mstr portid for slv port %d not found\n",
1253 __func__, portinfo->port_id[i]);
1254 goto port_fail;
1255 }
1256
1257 mport = &(swrm->mport_cfg[mstr_port_id]);
1258 /* get port req */
1259 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1260 portinfo->dev_num);
1261 if (!port_req) {
1262 dev_dbg(&master->dev, "%s: new req:port id %d dev %d\n",
1263 __func__, portinfo->port_id[i],
1264 portinfo->dev_num);
1265 port_req = kzalloc(sizeof(struct swr_port_info),
1266 GFP_KERNEL);
1267 if (!port_req) {
1268 ret = -ENOMEM;
1269 goto mem_fail;
1270 }
1271 port_req->dev_num = portinfo->dev_num;
1272 port_req->slave_port_id = portinfo->port_id[i];
1273 port_req->num_ch = portinfo->num_ch[i];
1274 port_req->ch_rate = portinfo->ch_rate[i];
1275 port_req->ch_en = 0;
1276 port_req->master_port_id = mstr_port_id;
1277 list_add(&port_req->list, &mport->port_req_list);
1278 }
1279 port_req->req_ch |= portinfo->ch_en[i];
1280
1281 dev_dbg(&master->dev,
1282 "%s: mstr port %d, slv port %d ch_rate %d num_ch %d\n",
1283 __func__, port_req->master_port_id,
1284 port_req->slave_port_id, port_req->ch_rate,
1285 port_req->num_ch);
1286 /* Put the port req on master port */
1287 mport = &(swrm->mport_cfg[mstr_port_id]);
1288 mport->port_en = true;
1289 mport->req_ch |= mstr_ch_msk;
1290 master->port_en_mask |= (1 << mstr_port_id);
1291 }
1292 master->num_port += portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301293 set_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301294 swr_port_response(master, portinfo->tid);
1295
1296 mutex_unlock(&swrm->mlock);
1297 return 0;
1298
1299port_fail:
1300mem_fail:
1301 /* cleanup port reqs in error condition */
1302 swrm_cleanup_disabled_port_reqs(master);
1303 mutex_unlock(&swrm->mlock);
1304 return ret;
1305}
1306
1307static int swrm_disconnect_port(struct swr_master *master,
1308 struct swr_params *portinfo)
1309{
1310 int i, ret = 0;
1311 struct swr_port_info *port_req;
1312 struct swrm_mports *mport;
1313 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1314 u8 mstr_port_id, mstr_ch_mask;
1315
1316 if (!swrm) {
1317 dev_err(&master->dev,
1318 "%s: Invalid handle to swr controller\n",
1319 __func__);
1320 return -EINVAL;
1321 }
1322
1323 if (!portinfo) {
1324 dev_err(&master->dev, "%s: portinfo is NULL\n", __func__);
1325 return -EINVAL;
1326 }
1327 mutex_lock(&swrm->mlock);
1328
1329 for (i = 0; i < portinfo->num_port; i++) {
1330
1331 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_mask,
1332 portinfo->port_type[i], portinfo->port_id[i]);
1333 if (ret) {
1334 dev_err(&master->dev,
1335 "%s: mstr portid for slv port %d not found\n",
1336 __func__, portinfo->port_id[i]);
1337 mutex_unlock(&swrm->mlock);
1338 return -EINVAL;
1339 }
1340 mport = &(swrm->mport_cfg[mstr_port_id]);
1341 /* get port req */
1342 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1343 portinfo->dev_num);
1344
1345 if (!port_req) {
1346 dev_err(&master->dev, "%s:port not enabled : port %d\n",
1347 __func__, portinfo->port_id[i]);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05301348 mutex_unlock(&swrm->mlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301349 return -EINVAL;
1350 }
1351 port_req->req_ch &= ~portinfo->ch_en[i];
1352 mport->req_ch &= ~mstr_ch_mask;
1353 }
1354 master->num_port -= portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301355 set_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301356 swr_port_response(master, portinfo->tid);
1357 mutex_unlock(&swrm->mlock);
1358
1359 return 0;
1360}
1361
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301362static int swrm_find_alert_slave(struct swr_mstr_ctrl *swrm,
1363 int status, u8 *devnum)
1364{
1365 int i;
1366 bool found = false;
1367
1368 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1369 if ((status & SWRM_MCP_SLV_STATUS_MASK) == SWR_ALERT) {
1370 *devnum = i;
1371 found = true;
1372 break;
1373 }
1374 status >>= 2;
1375 }
1376 if (found)
1377 return 0;
1378 else
1379 return -EINVAL;
1380}
1381
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301382static void swrm_enable_slave_irq(struct swr_mstr_ctrl *swrm)
1383{
1384 int i;
1385 int status = 0;
1386
1387 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1388 if (!status) {
1389 dev_dbg_ratelimited(swrm->dev, "%s: slaves status is 0x%x\n",
1390 __func__, status);
1391 return;
1392 }
1393 dev_dbg(swrm->dev, "%s: slave status: 0x%x\n", __func__, status);
1394 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1395 if (status & SWRM_MCP_SLV_STATUS_MASK)
1396 swrm_cmd_fifo_wr_cmd(swrm, 0x4, i, 0x0,
1397 SWRS_SCP_INT_STATUS_MASK_1);
1398 status >>= 2;
1399 }
1400}
1401
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301402static int swrm_check_slave_change_status(struct swr_mstr_ctrl *swrm,
1403 int status, u8 *devnum)
1404{
1405 int i;
1406 int new_sts = status;
1407 int ret = SWR_NOT_PRESENT;
1408
1409 if (status != swrm->slave_status) {
1410 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1411 if ((status & SWRM_MCP_SLV_STATUS_MASK) !=
1412 (swrm->slave_status & SWRM_MCP_SLV_STATUS_MASK)) {
1413 ret = (status & SWRM_MCP_SLV_STATUS_MASK);
1414 *devnum = i;
1415 break;
1416 }
1417 status >>= 2;
1418 swrm->slave_status >>= 2;
1419 }
1420 swrm->slave_status = new_sts;
1421 }
1422 return ret;
1423}
1424
1425static irqreturn_t swr_mstr_interrupt(int irq, void *dev)
1426{
1427 struct swr_mstr_ctrl *swrm = dev;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301428 u32 value, intr_sts, intr_sts_masked;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301429 u32 temp = 0;
1430 u32 status, chg_sts, i;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301431 u8 devnum = 0;
1432 int ret = IRQ_HANDLED;
1433 struct swr_device *swr_dev;
1434 struct swr_master *mstr = &swrm->master;
1435
Ramprasad Katkam57349872018-11-11 18:34:57 +05301436 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1437 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1438 return IRQ_NONE;
1439 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301440
1441 mutex_lock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301442 if (swrm_clk_request(swrm, true)) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +05301443 dev_err_ratelimited(swrm->dev, "%s:clk request failed\n",
1444 __func__);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301445 mutex_unlock(&swrm->reslock);
1446 goto exit;
1447 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301448 mutex_unlock(&swrm->reslock);
1449
1450 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301451 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301452handle_irq:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301453 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301454 value = intr_sts_masked & (1 << i);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301455 if (!value)
1456 continue;
1457
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301458 switch (value) {
1459 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1460 dev_dbg(swrm->dev, "Trigger irq to slave device\n");
1461 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301462 ret = swrm_find_alert_slave(swrm, status, &devnum);
1463 if (ret) {
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301464 dev_err_ratelimited(swrm->dev,
1465 "no slave alert found.spurious interrupt\n");
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05301466 break;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301467 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301468 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1469 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1470 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1471 SWRS_SCP_INT_STATUS_CLEAR_1);
1472 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1473 SWRS_SCP_INT_STATUS_CLEAR_1);
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301474
1475
1476 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1477 if (swr_dev->dev_num != devnum)
1478 continue;
1479 if (swr_dev->slave_irq) {
1480 do {
Ramprasad Katkam2586a4b2019-03-18 16:53:39 +05301481 swr_dev->slave_irq_pending = 0;
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301482 handle_nested_irq(
1483 irq_find_mapping(
1484 swr_dev->slave_irq, 0));
1485 } while (swr_dev->slave_irq_pending);
1486 }
1487
1488 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301489 break;
1490 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1491 dev_dbg(swrm->dev, "SWR new slave attached\n");
1492 break;
1493 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1494 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1495 if (status == swrm->slave_status) {
1496 dev_dbg(swrm->dev,
1497 "%s: No change in slave status: %d\n",
1498 __func__, status);
1499 break;
1500 }
1501 chg_sts = swrm_check_slave_change_status(swrm, status,
1502 &devnum);
1503 switch (chg_sts) {
1504 case SWR_NOT_PRESENT:
1505 dev_dbg(swrm->dev, "device %d got detached\n",
1506 devnum);
1507 break;
1508 case SWR_ATTACHED_OK:
1509 dev_dbg(swrm->dev, "device %d got attached\n",
1510 devnum);
Ramprasad Katkamdebe8932018-09-25 18:08:18 +05301511 /* enable host irq from slave device*/
1512 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1513 SWRS_SCP_INT_STATUS_CLEAR_1);
1514 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1515 SWRS_SCP_INT_STATUS_MASK_1);
1516
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301517 break;
1518 case SWR_ALERT:
1519 dev_dbg(swrm->dev,
1520 "device %d has pending interrupt\n",
1521 devnum);
1522 break;
1523 }
1524 break;
1525 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1526 dev_err_ratelimited(swrm->dev,
1527 "SWR bus clsh detected\n");
1528 break;
1529 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1530 dev_dbg(swrm->dev, "SWR read FIFO overflow\n");
1531 break;
1532 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1533 dev_dbg(swrm->dev, "SWR read FIFO underflow\n");
1534 break;
1535 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1536 dev_dbg(swrm->dev, "SWR write FIFO overflow\n");
1537 break;
1538 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1539 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1540 dev_err_ratelimited(swrm->dev,
1541 "SWR CMD error, fifo status 0x%x, flushing fifo\n",
1542 value);
1543 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1544 break;
1545 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301546 dev_err_ratelimited(swrm->dev, "SWR Port collision detected\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301547 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301548 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301549 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301550 break;
1551 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1552 dev_dbg(swrm->dev, "SWR read enable valid mismatch\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301553 swrm->intr_mask &=
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301554 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1555 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301556 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301557 break;
1558 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1559 complete(&swrm->broadcast);
1560 dev_dbg(swrm->dev, "SWR cmd id finished\n");
1561 break;
1562 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_AUTO_ENUM_FINISHED:
1563 break;
1564 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED:
1565 break;
1566 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL:
1567 break;
1568 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED:
1569 complete(&swrm->reset);
1570 break;
1571 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED:
1572 break;
1573 default:
1574 dev_err_ratelimited(swrm->dev,
1575 "SWR unknown interrupt\n");
1576 ret = IRQ_NONE;
1577 break;
1578 }
1579 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301580 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1581 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
Ramprasad Katkam83303512018-10-11 17:34:22 +05301582
1583 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301584 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301585
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301586 if (intr_sts_masked) {
Ramprasad Katkam83303512018-10-11 17:34:22 +05301587 dev_dbg(swrm->dev, "%s: new interrupt received\n", __func__);
1588 goto handle_irq;
1589 }
1590
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301591 mutex_lock(&swrm->reslock);
1592 swrm_clk_request(swrm, false);
1593 mutex_unlock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301594exit:
Ramprasad Katkam57349872018-11-11 18:34:57 +05301595 swrm_unlock_sleep(swrm);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301596 return ret;
1597}
1598
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301599static irqreturn_t swr_mstr_interrupt_v2(int irq, void *dev)
1600{
1601 struct swr_mstr_ctrl *swrm = dev;
1602 u32 value, intr_sts, intr_sts_masked;
1603 u32 temp = 0;
1604 u32 status, chg_sts, i;
1605 u8 devnum = 0;
1606 int ret = IRQ_HANDLED;
1607 struct swr_device *swr_dev;
1608 struct swr_master *mstr = &swrm->master;
1609
Karthikeyan Mani4bee1db2019-09-18 17:58:41 -07001610 if (!swrm->dev_up || swrm->state == SWR_MSTR_SSR) {
1611 complete(&swrm->broadcast);
1612 dev_dbg(swrm->dev, "%s swrm is not up\n", __func__);
1613 return IRQ_NONE;
1614 }
1615
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301616 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1617 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1618 return IRQ_NONE;
1619 }
1620
1621 mutex_lock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301622 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
1623 ret = IRQ_NONE;
1624 goto exit;
1625 }
1626 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
1627 ret = IRQ_NONE;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301628 goto err_audio_hw_vote;
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001629 }
Karthikeyan Mani4bee1db2019-09-18 17:58:41 -07001630 ret = swrm_clk_request(swrm, true);
1631 if (ret) {
1632 dev_err(dev, "%s: swrm clk failed\n", __func__);
1633 ret = IRQ_NONE;
1634 goto err_audio_core_vote;
1635 }
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301636 mutex_unlock(&swrm->reslock);
1637
1638 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1639 intr_sts_masked = intr_sts & swrm->intr_mask;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301640
1641 dev_dbg(swrm->dev, "%s: status: 0x%x \n", __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301642handle_irq:
1643 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
1644 value = intr_sts_masked & (1 << i);
1645 if (!value)
1646 continue;
1647
1648 switch (value) {
1649 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1650 dev_dbg(swrm->dev, "%s: Trigger irq to slave device\n",
1651 __func__);
1652 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1653 ret = swrm_find_alert_slave(swrm, status, &devnum);
1654 if (ret) {
1655 dev_err_ratelimited(swrm->dev,
1656 "%s: no slave alert found.spurious interrupt\n",
1657 __func__);
1658 break;
1659 }
1660 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1661 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1662 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1663 SWRS_SCP_INT_STATUS_CLEAR_1);
1664 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1665 SWRS_SCP_INT_STATUS_CLEAR_1);
1666
1667
1668 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1669 if (swr_dev->dev_num != devnum)
1670 continue;
1671 if (swr_dev->slave_irq) {
1672 do {
1673 handle_nested_irq(
1674 irq_find_mapping(
1675 swr_dev->slave_irq, 0));
1676 } while (swr_dev->slave_irq_pending);
1677 }
1678
1679 }
1680 break;
1681 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1682 dev_dbg(swrm->dev, "%s: SWR new slave attached\n",
1683 __func__);
1684 break;
1685 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1686 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1687 if (status == swrm->slave_status) {
1688 dev_dbg(swrm->dev,
1689 "%s: No change in slave status: %d\n",
1690 __func__, status);
1691 break;
1692 }
1693 chg_sts = swrm_check_slave_change_status(swrm, status,
1694 &devnum);
1695 switch (chg_sts) {
1696 case SWR_NOT_PRESENT:
1697 dev_dbg(swrm->dev,
1698 "%s: device %d got detached\n",
1699 __func__, devnum);
1700 break;
1701 case SWR_ATTACHED_OK:
1702 dev_dbg(swrm->dev,
1703 "%s: device %d got attached\n",
1704 __func__, devnum);
1705 /* enable host irq from slave device*/
1706 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1707 SWRS_SCP_INT_STATUS_CLEAR_1);
1708 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1709 SWRS_SCP_INT_STATUS_MASK_1);
1710
1711 break;
1712 case SWR_ALERT:
1713 dev_dbg(swrm->dev,
1714 "%s: device %d has pending interrupt\n",
1715 __func__, devnum);
1716 break;
1717 }
1718 break;
1719 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1720 dev_err_ratelimited(swrm->dev,
1721 "%s: SWR bus clsh detected\n",
1722 __func__);
1723 break;
1724 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1725 dev_dbg(swrm->dev, "%s: SWR read FIFO overflow\n",
1726 __func__);
1727 break;
1728 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1729 dev_dbg(swrm->dev, "%s: SWR read FIFO underflow\n",
1730 __func__);
1731 break;
1732 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1733 dev_dbg(swrm->dev, "%s: SWR write FIFO overflow\n",
1734 __func__);
1735 break;
1736 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1737 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1738 dev_err_ratelimited(swrm->dev,
1739 "%s: SWR CMD error, fifo status 0x%x, flushing fifo\n",
1740 __func__, value);
1741 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1742 break;
1743 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
1744 dev_err_ratelimited(swrm->dev,
1745 "%s: SWR Port collision detected\n",
1746 __func__);
1747 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
1748 swr_master_write(swrm,
1749 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1750 break;
1751 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1752 dev_dbg(swrm->dev,
1753 "%s: SWR read enable valid mismatch\n",
1754 __func__);
1755 swrm->intr_mask &=
1756 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1757 swr_master_write(swrm,
1758 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1759 break;
1760 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1761 complete(&swrm->broadcast);
1762 dev_dbg(swrm->dev, "%s: SWR cmd id finished\n",
1763 __func__);
1764 break;
1765 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED_V2:
1766 break;
1767 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL_V2:
1768 break;
1769 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED_V2:
1770 break;
1771 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED_V2:
1772 break;
1773 case SWRM_INTERRUPT_STATUS_EXT_CLK_STOP_WAKEUP:
1774 if (swrm->state == SWR_MSTR_UP)
1775 dev_dbg(swrm->dev,
1776 "%s:SWR Master is already up\n",
1777 __func__);
1778 else
1779 dev_err_ratelimited(swrm->dev,
1780 "%s: SWR wokeup during clock stop\n",
1781 __func__);
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301782 /* It might be possible the slave device gets reset
1783 * and slave interrupt gets missed. So re-enable
1784 * Host IRQ and process slave pending
1785 * interrupts, if any.
1786 */
1787 swrm_enable_slave_irq(swrm);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301788 break;
1789 default:
1790 dev_err_ratelimited(swrm->dev,
1791 "%s: SWR unknown interrupt value: %d\n",
1792 __func__, value);
1793 ret = IRQ_NONE;
1794 break;
1795 }
1796 }
1797 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1798 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
1799
1800 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1801 intr_sts_masked = intr_sts & swrm->intr_mask;
1802
1803 if (intr_sts_masked) {
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301804 dev_dbg(swrm->dev, "%s: new interrupt received 0x%x\n",
1805 __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301806 goto handle_irq;
1807 }
1808
1809 mutex_lock(&swrm->reslock);
1810 swrm_clk_request(swrm, false);
Karthikeyan Mani4bee1db2019-09-18 17:58:41 -07001811err_audio_core_vote:
Sudheer Papothi384addd2019-06-14 02:26:52 +05301812 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
Sudheer Papothi06f43412019-07-09 03:32:54 +05301813
1814err_audio_hw_vote:
Sudheer Papothi384addd2019-06-14 02:26:52 +05301815 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001816exit:
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301817 mutex_unlock(&swrm->reslock);
1818 swrm_unlock_sleep(swrm);
1819 return ret;
1820}
1821
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301822static irqreturn_t swrm_wakeup_interrupt(int irq, void *dev)
1823{
1824 struct swr_mstr_ctrl *swrm = dev;
1825 int ret = IRQ_HANDLED;
1826
1827 if (!swrm || !(swrm->dev)) {
1828 pr_err("%s: swrm or dev is null\n", __func__);
1829 return IRQ_NONE;
1830 }
1831 mutex_lock(&swrm->devlock);
1832 if (!swrm->dev_up) {
1833 if (swrm->wake_irq > 0)
1834 disable_irq_nosync(swrm->wake_irq);
1835 mutex_unlock(&swrm->devlock);
1836 return ret;
1837 }
1838 mutex_unlock(&swrm->devlock);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301839 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1840 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1841 goto exit;
1842 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301843 if (swrm->wake_irq > 0)
1844 disable_irq_nosync(swrm->wake_irq);
1845 pm_runtime_get_sync(swrm->dev);
1846 pm_runtime_mark_last_busy(swrm->dev);
1847 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301848 swrm_unlock_sleep(swrm);
1849exit:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301850 return ret;
1851}
1852
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301853static void swrm_wakeup_work(struct work_struct *work)
1854{
1855 struct swr_mstr_ctrl *swrm;
1856
1857 swrm = container_of(work, struct swr_mstr_ctrl,
1858 wakeup_work);
1859 if (!swrm || !(swrm->dev)) {
1860 pr_err("%s: swrm or dev is null\n", __func__);
1861 return;
1862 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301863
1864 mutex_lock(&swrm->devlock);
1865 if (!swrm->dev_up) {
1866 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301867 goto exit;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301868 }
1869 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301870 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1871 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1872 goto exit;
1873 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301874 pm_runtime_get_sync(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301875 pm_runtime_mark_last_busy(swrm->dev);
1876 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301877 swrm_unlock_sleep(swrm);
1878exit:
1879 pm_relax(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301880}
1881
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301882static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
1883{
1884 u32 val;
1885
1886 swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1887 val = (swrm->slave_status >> (devnum * 2));
1888 val &= SWRM_MCP_SLV_STATUS_MASK;
1889 return val;
1890}
1891
1892static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
1893 u8 *dev_num)
1894{
1895 int i;
1896 u64 id = 0;
1897 int ret = -EINVAL;
1898 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1899 struct swr_device *swr_dev;
1900 u32 num_dev = 0;
1901
1902 if (!swrm) {
1903 pr_err("%s: Invalid handle to swr controller\n",
1904 __func__);
1905 return ret;
1906 }
1907 if (swrm->num_dev)
1908 num_dev = swrm->num_dev;
1909 else
1910 num_dev = mstr->num_dev;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301911
1912 mutex_lock(&swrm->devlock);
1913 if (!swrm->dev_up) {
1914 mutex_unlock(&swrm->devlock);
1915 return ret;
1916 }
1917 mutex_unlock(&swrm->devlock);
1918
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301919 pm_runtime_get_sync(swrm->dev);
1920 for (i = 1; i < (num_dev + 1); i++) {
1921 id = ((u64)(swr_master_read(swrm,
1922 SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
1923 id |= swr_master_read(swrm,
1924 SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301925
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301926 /*
1927 * As pm_runtime_get_sync() brings all slaves out of reset
1928 * update logical device number for all slaves.
1929 */
1930 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1931 if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
1932 u32 status = swrm_get_device_status(swrm, i);
1933
1934 if ((status == 0x01) || (status == 0x02)) {
1935 swr_dev->dev_num = i;
1936 if ((id & SWR_DEV_ID_MASK) == dev_id) {
1937 *dev_num = i;
1938 ret = 0;
1939 }
1940 dev_dbg(swrm->dev,
1941 "%s: devnum %d is assigned for dev addr %lx\n",
1942 __func__, i, swr_dev->addr);
1943 }
1944 }
1945 }
1946 }
1947 if (ret)
1948 dev_err(swrm->dev, "%s: device 0x%llx is not ready\n",
1949 __func__, dev_id);
1950
1951 pm_runtime_mark_last_busy(swrm->dev);
1952 pm_runtime_put_autosuspend(swrm->dev);
1953 return ret;
1954}
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301955
1956static void swrm_device_wakeup_vote(struct swr_master *mstr)
1957{
1958 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1959
1960 if (!swrm) {
1961 pr_err("%s: Invalid handle to swr controller\n",
1962 __func__);
1963 return;
1964 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05301965 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1966 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1967 return;
1968 }
Sudheer Papothi384addd2019-06-14 02:26:52 +05301969 if (++swrm->hw_core_clk_en == 1)
1970 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
1971 dev_err(swrm->dev, "%s:lpass core hw enable failed\n",
1972 __func__);
1973 --swrm->hw_core_clk_en;
1974 }
1975 if ( ++swrm->aud_core_clk_en == 1)
1976 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
1977 dev_err(swrm->dev, "%s:lpass audio hw enable failed\n",
1978 __func__);
1979 --swrm->aud_core_clk_en;
1980 }
1981 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
1982 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301983 pm_runtime_get_sync(swrm->dev);
1984}
1985
1986static void swrm_device_wakeup_unvote(struct swr_master *mstr)
1987{
1988 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1989
1990 if (!swrm) {
1991 pr_err("%s: Invalid handle to swr controller\n",
1992 __func__);
1993 return;
1994 }
1995 pm_runtime_mark_last_busy(swrm->dev);
1996 pm_runtime_put_autosuspend(swrm->dev);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301997 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
1998 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
1999
2000 --swrm->aud_core_clk_en;
2001 if (swrm->aud_core_clk_en < 0)
2002 swrm->aud_core_clk_en = 0;
2003 else if (swrm->aud_core_clk_en == 0)
2004 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2005
2006 --swrm->hw_core_clk_en;
2007 if (swrm->hw_core_clk_en < 0)
2008 swrm->hw_core_clk_en = 0;
2009 else if (swrm->hw_core_clk_en == 0)
2010 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
2011
Ramprasad Katkam57349872018-11-11 18:34:57 +05302012 swrm_unlock_sleep(swrm);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302013}
2014
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302015static int swrm_master_init(struct swr_mstr_ctrl *swrm)
2016{
2017 int ret = 0;
2018 u32 val;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302019 u8 row_ctrl = SWR_ROW_50;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302020 u8 col_ctrl = SWR_MIN_COL;
2021 u8 ssp_period = 1;
2022 u8 retry_cmd_num = 3;
2023 u32 reg[SWRM_MAX_INIT_REG];
2024 u32 value[SWRM_MAX_INIT_REG];
2025 int len = 0;
2026
2027 /* Clear Rows and Cols */
2028 val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
2029 (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
2030 (ssp_period << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
2031
2032 reg[len] = SWRM_MCP_FRAME_CTRL_BANK_ADDR(0);
2033 value[len++] = val;
2034
2035 /* Set Auto enumeration flag */
2036 reg[len] = SWRM_ENUMERATOR_CFG_ADDR;
2037 value[len++] = 1;
2038
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302039 /* Configure No pings */
2040 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2041 val &= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK;
2042 val |= (0x1f << SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT);
2043 reg[len] = SWRM_MCP_CFG_ADDR;
2044 value[len++] = val;
2045
2046 /* Configure number of retries of a read/write cmd */
2047 val = (retry_cmd_num << SWRM_CMD_FIFO_CFG_NUM_OF_CMD_RETRY_SHFT);
2048 reg[len] = SWRM_CMD_FIFO_CFG_ADDR;
2049 value[len++] = val;
2050
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302051 reg[len] = SWRM_MCP_BUS_CTRL_ADDR;
2052 value[len++] = 0x2;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302053
Ramprasad Katkam83303512018-10-11 17:34:22 +05302054 /* Set IRQ to PULSE */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302055 reg[len] = SWRM_COMP_CFG_ADDR;
Ramprasad Katkam83303512018-10-11 17:34:22 +05302056 value[len++] = 0x02;
2057
2058 reg[len] = SWRM_COMP_CFG_ADDR;
2059 value[len++] = 0x03;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302060
2061 reg[len] = SWRM_INTERRUPT_CLEAR;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302062 value[len++] = 0xFFFFFFFF;
2063
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302064 swrm->intr_mask = SWRM_INTERRUPT_STATUS_MASK;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302065 /* Mask soundwire interrupts */
2066 reg[len] = SWRM_INTERRUPT_MASK_ADDR;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302067 value[len++] = swrm->intr_mask;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302068
2069 reg[len] = SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302070 value[len++] = swrm->intr_mask;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302071
2072 swr_master_bulk_write(swrm, reg, value, len);
2073
Sudheer Papothi63f48152018-11-15 01:08:03 +05302074 /*
2075 * For SWR master version 1.5.1, continue
2076 * execute on command ignore.
2077 */
2078 if (swrm->version == SWRM_VERSION_1_5_1)
2079 swr_master_write(swrm, SWRM_CMD_FIFO_CFG_ADDR,
2080 (swr_master_read(swrm,
2081 SWRM_CMD_FIFO_CFG_ADDR) | 0x80000000));
2082
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302083 return ret;
2084}
2085
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302086static int swrm_event_notify(struct notifier_block *self,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302087 unsigned long action, void *data)
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302088{
2089 struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302090 event_notifier);
2091
2092 if (!swrm || !(swrm->dev)) {
2093 pr_err("%s: swrm or dev is NULL\n", __func__);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302094 return -EINVAL;
2095 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302096 switch (action) {
2097 case MSM_AUD_DC_EVENT:
2098 schedule_work(&(swrm->dc_presence_work));
2099 break;
2100 case SWR_WAKE_IRQ_EVENT:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302101 if (swrm->ipc_wakeup && !swrm->ipc_wakeup_triggered) {
2102 swrm->ipc_wakeup_triggered = true;
Ramprasad Katkam57349872018-11-11 18:34:57 +05302103 pm_stay_awake(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302104 schedule_work(&swrm->wakeup_work);
Ramprasad Katkamcd61c6e2018-09-18 13:22:58 +05302105 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302106 break;
2107 default:
2108 dev_err(swrm->dev, "%s: invalid event type: %lu\n",
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302109 __func__, action);
2110 return -EINVAL;
2111 }
2112
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302113 return 0;
2114}
2115
2116static void swrm_notify_work_fn(struct work_struct *work)
2117{
2118 struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302119 dc_presence_work);
2120
2121 if (!swrm || !swrm->pdev) {
2122 pr_err("%s: swrm or pdev is NULL\n", __func__);
2123 return;
2124 }
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302125 swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL);
2126}
2127
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302128static int swrm_probe(struct platform_device *pdev)
2129{
2130 struct swr_mstr_ctrl *swrm;
2131 struct swr_ctrl_platform_data *pdata;
2132 u32 i, num_ports, port_num, port_type, ch_mask;
2133 u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0;
2134 int ret = 0;
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302135 struct clk *lpass_core_hw_vote = NULL;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302136 struct clk *lpass_core_audio = NULL;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302137
2138 /* Allocate soundwire master driver structure */
2139 swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl),
2140 GFP_KERNEL);
2141 if (!swrm) {
2142 ret = -ENOMEM;
2143 goto err_memory_fail;
2144 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302145 swrm->pdev = pdev;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302146 swrm->dev = &pdev->dev;
2147 platform_set_drvdata(pdev, swrm);
2148 swr_set_ctrl_data(&swrm->master, swrm);
2149 pdata = dev_get_platdata(&pdev->dev);
2150 if (!pdata) {
2151 dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
2152 __func__);
2153 ret = -EINVAL;
2154 goto err_pdata_fail;
2155 }
2156 swrm->handle = (void *)pdata->handle;
2157 if (!swrm->handle) {
2158 dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
2159 __func__);
2160 ret = -EINVAL;
2161 goto err_pdata_fail;
2162 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302163 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr_master_id",
2164 &swrm->master_id);
2165 if (ret) {
2166 dev_err(&pdev->dev, "%s: failed to get master id\n", __func__);
2167 goto err_pdata_fail;
2168 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302169 if (!(of_property_read_u32(pdev->dev.of_node,
2170 "swrm-io-base", &swrm->swrm_base_reg)))
2171 ret = of_property_read_u32(pdev->dev.of_node,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302172 "swrm-io-base", &swrm->swrm_base_reg);
2173 if (!swrm->swrm_base_reg) {
2174 swrm->read = pdata->read;
2175 if (!swrm->read) {
2176 dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
2177 __func__);
2178 ret = -EINVAL;
2179 goto err_pdata_fail;
2180 }
2181 swrm->write = pdata->write;
2182 if (!swrm->write) {
2183 dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
2184 __func__);
2185 ret = -EINVAL;
2186 goto err_pdata_fail;
2187 }
2188 swrm->bulk_write = pdata->bulk_write;
2189 if (!swrm->bulk_write) {
2190 dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
2191 __func__);
2192 ret = -EINVAL;
2193 goto err_pdata_fail;
2194 }
2195 } else {
2196 swrm->swrm_dig_base = devm_ioremap(&pdev->dev,
2197 swrm->swrm_base_reg, SWRM_MAX_REGISTER);
2198 }
2199
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07002200 swrm->core_vote = pdata->core_vote;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302201 swrm->clk = pdata->clk;
2202 if (!swrm->clk) {
2203 dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
2204 __func__);
2205 ret = -EINVAL;
2206 goto err_pdata_fail;
2207 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302208 if (of_property_read_u32(pdev->dev.of_node,
2209 "qcom,swr-clock-stop-mode0",
2210 &swrm->clk_stop_mode0_supp)) {
2211 swrm->clk_stop_mode0_supp = FALSE;
2212 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302213
2214 ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
2215 &swrm->num_dev);
2216 if (ret) {
2217 dev_dbg(&pdev->dev, "%s: Looking up %s property failed\n",
2218 __func__, "qcom,swr-num-dev");
2219 } else {
2220 if (swrm->num_dev > SWR_MAX_SLAVE_DEVICES) {
2221 dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
2222 __func__, swrm->num_dev, SWR_MAX_SLAVE_DEVICES);
2223 ret = -EINVAL;
2224 goto err_pdata_fail;
2225 }
2226 }
2227
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302228 /* Parse soundwire port mapping */
2229 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
2230 &num_ports);
2231 if (ret) {
2232 dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
2233 goto err_pdata_fail;
2234 }
2235 swrm->num_ports = num_ports;
2236
2237 if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
2238 &map_size)) {
2239 dev_err(swrm->dev, "missing port mapping\n");
2240 goto err_pdata_fail;
2241 }
2242
2243 map_length = map_size / (3 * sizeof(u32));
2244 if (num_ports > SWR_MSTR_PORT_LEN) {
2245 dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
2246 __func__);
2247 ret = -EINVAL;
2248 goto err_pdata_fail;
2249 }
2250 temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
2251
2252 if (!temp) {
2253 ret = -ENOMEM;
2254 goto err_pdata_fail;
2255 }
2256 ret = of_property_read_u32_array(pdev->dev.of_node,
2257 "qcom,swr-port-mapping", temp, 3 * map_length);
2258 if (ret) {
2259 dev_err(swrm->dev, "%s: Failed to read port mapping\n",
2260 __func__);
2261 goto err_pdata_fail;
2262 }
2263
2264 for (i = 0; i < map_length; i++) {
2265 port_num = temp[3 * i];
2266 port_type = temp[3 * i + 1];
2267 ch_mask = temp[3 * i + 2];
2268
2269 if (port_num != old_port_num)
2270 ch_iter = 0;
2271 swrm->port_mapping[port_num][ch_iter].port_type = port_type;
2272 swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
2273 old_port_num = port_num;
2274 }
2275 devm_kfree(&pdev->dev, temp);
2276
2277 swrm->reg_irq = pdata->reg_irq;
2278 swrm->master.read = swrm_read;
2279 swrm->master.write = swrm_write;
2280 swrm->master.bulk_write = swrm_bulk_write;
2281 swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
2282 swrm->master.connect_port = swrm_connect_port;
2283 swrm->master.disconnect_port = swrm_disconnect_port;
2284 swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
2285 swrm->master.remove_from_group = swrm_remove_from_group;
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302286 swrm->master.device_wakeup_vote = swrm_device_wakeup_vote;
2287 swrm->master.device_wakeup_unvote = swrm_device_wakeup_unvote;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302288 swrm->master.dev.parent = &pdev->dev;
2289 swrm->master.dev.of_node = pdev->dev.of_node;
2290 swrm->master.num_port = 0;
2291 swrm->rcmd_id = 0;
2292 swrm->wcmd_id = 0;
2293 swrm->slave_status = 0;
2294 swrm->num_rx_chs = 0;
2295 swrm->clk_ref_count = 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302296 swrm->swr_irq_wakeup_capable = 0;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302297 swrm->mclk_freq = MCLK_FREQ;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302298 swrm->dev_up = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302299 swrm->state = SWR_MSTR_UP;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302300 swrm->ipc_wakeup = false;
2301 swrm->ipc_wakeup_triggered = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302302 init_completion(&swrm->reset);
2303 init_completion(&swrm->broadcast);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302304 init_completion(&swrm->clk_off_complete);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302305 mutex_init(&swrm->mlock);
2306 mutex_init(&swrm->reslock);
2307 mutex_init(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302308 mutex_init(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302309 mutex_init(&swrm->clklock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302310 mutex_init(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302311 mutex_init(&swrm->pm_lock);
2312 swrm->wlock_holders = 0;
2313 swrm->pm_state = SWRM_PM_SLEEPABLE;
2314 init_waitqueue_head(&swrm->pm_wq);
2315 pm_qos_add_request(&swrm->pm_qos_req,
2316 PM_QOS_CPU_DMA_LATENCY,
2317 PM_QOS_DEFAULT_VALUE);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302318
2319 for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++)
2320 INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
2321
Sudheer Papothi06f43412019-07-09 03:32:54 +05302322 /* Register LPASS core hw vote */
2323 lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
2324 if (IS_ERR(lpass_core_hw_vote)) {
2325 ret = PTR_ERR(lpass_core_hw_vote);
2326 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2327 __func__, "lpass_core_hw_vote", ret);
2328 lpass_core_hw_vote = NULL;
2329 ret = 0;
2330 }
2331 swrm->lpass_core_hw_vote = lpass_core_hw_vote;
2332
2333 /* Register LPASS audio core vote */
2334 lpass_core_audio = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
2335 if (IS_ERR(lpass_core_audio)) {
2336 ret = PTR_ERR(lpass_core_audio);
2337 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2338 __func__, "lpass_core_audio", ret);
2339 lpass_core_audio = NULL;
2340 ret = 0;
2341 }
2342 swrm->lpass_core_audio = lpass_core_audio;
2343
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302344 if (swrm->reg_irq) {
2345 ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
2346 SWR_IRQ_REGISTER);
2347 if (ret) {
2348 dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
2349 __func__, ret);
2350 goto err_irq_fail;
2351 }
2352 } else {
2353 swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
2354 if (swrm->irq < 0) {
2355 dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
2356 __func__, swrm->irq);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302357 goto err_irq_fail;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302358 }
2359
2360 ret = request_threaded_irq(swrm->irq, NULL,
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302361 swr_mstr_interrupt_v2,
Ramprasad Katkam83303512018-10-11 17:34:22 +05302362 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302363 "swr_master_irq", swrm);
2364 if (ret) {
2365 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2366 __func__, ret);
2367 goto err_irq_fail;
2368 }
2369
2370 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302371 /* Make inband tx interrupts as wakeup capable for slave irq */
2372 ret = of_property_read_u32(pdev->dev.of_node,
2373 "qcom,swr-mstr-irq-wakeup-capable",
2374 &swrm->swr_irq_wakeup_capable);
2375 if (ret)
2376 dev_dbg(swrm->dev, "%s: swrm irq wakeup capable not defined\n",
2377 __func__);
2378 if (swrm->swr_irq_wakeup_capable)
2379 irq_set_irq_wake(swrm->irq, 1);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302380 ret = swr_register_master(&swrm->master);
2381 if (ret) {
2382 dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
2383 goto err_mstr_fail;
2384 }
2385
2386 /* Add devices registered with board-info as the
2387 * controller will be up now
2388 */
2389 swr_master_add_boarddevices(&swrm->master);
2390 mutex_lock(&swrm->mlock);
2391 swrm_clk_request(swrm, true);
2392 ret = swrm_master_init(swrm);
2393 if (ret < 0) {
2394 dev_err(&pdev->dev,
2395 "%s: Error in master Initialization , err %d\n",
2396 __func__, ret);
2397 mutex_unlock(&swrm->mlock);
2398 goto err_mstr_fail;
2399 }
2400 swrm->version = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
2401
2402 mutex_unlock(&swrm->mlock);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302403 INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302404
2405 if (pdev->dev.of_node)
2406 of_register_swr_devices(&swrm->master);
2407
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302408#ifdef CONFIG_DEBUG_FS
2409 swrm->debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
2410 if (!IS_ERR(swrm->debugfs_swrm_dent)) {
2411 swrm->debugfs_peek = debugfs_create_file("swrm_peek",
2412 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2413 (void *) swrm, &swrm_debug_read_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302414
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302415 swrm->debugfs_poke = debugfs_create_file("swrm_poke",
2416 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2417 (void *) swrm, &swrm_debug_write_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302418
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302419 swrm->debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
2420 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2421 (void *) swrm,
2422 &swrm_debug_dump_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302423 }
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302424#endif
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302425 ret = device_init_wakeup(swrm->dev, true);
2426 if (ret) {
2427 dev_err(swrm->dev, "Device wakeup init failed: %d\n", ret);
2428 goto err_irq_wakeup_fail;
2429 }
2430
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302431 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
2432 pm_runtime_use_autosuspend(&pdev->dev);
2433 pm_runtime_set_active(&pdev->dev);
2434 pm_runtime_enable(&pdev->dev);
2435 pm_runtime_mark_last_busy(&pdev->dev);
2436
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302437 INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn);
2438 swrm->event_notifier.notifier_call = swrm_event_notify;
2439 msm_aud_evt_register_client(&swrm->event_notifier);
2440
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302441 return 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302442err_irq_wakeup_fail:
2443 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302444err_mstr_fail:
2445 if (swrm->reg_irq)
2446 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2447 swrm, SWR_IRQ_FREE);
2448 else if (swrm->irq)
2449 free_irq(swrm->irq, swrm);
2450err_irq_fail:
2451 mutex_destroy(&swrm->mlock);
2452 mutex_destroy(&swrm->reslock);
2453 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302454 mutex_destroy(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302455 mutex_destroy(&swrm->clklock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302456 mutex_destroy(&swrm->pm_lock);
2457 pm_qos_remove_request(&swrm->pm_qos_req);
2458
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302459err_pdata_fail:
2460err_memory_fail:
2461 return ret;
2462}
2463
2464static int swrm_remove(struct platform_device *pdev)
2465{
2466 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2467
2468 if (swrm->reg_irq)
2469 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2470 swrm, SWR_IRQ_FREE);
2471 else if (swrm->irq)
2472 free_irq(swrm->irq, swrm);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302473 else if (swrm->wake_irq > 0)
2474 free_irq(swrm->wake_irq, swrm);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302475 if (swrm->swr_irq_wakeup_capable)
2476 irq_set_irq_wake(swrm->irq, 0);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302477 cancel_work_sync(&swrm->wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302478 pm_runtime_disable(&pdev->dev);
2479 pm_runtime_set_suspended(&pdev->dev);
2480 swr_unregister_master(&swrm->master);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302481 msm_aud_evt_unregister_client(&swrm->event_notifier);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302482 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302483 mutex_destroy(&swrm->mlock);
2484 mutex_destroy(&swrm->reslock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302485 mutex_destroy(&swrm->iolock);
2486 mutex_destroy(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302487 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302488 mutex_destroy(&swrm->pm_lock);
2489 pm_qos_remove_request(&swrm->pm_qos_req);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302490 devm_kfree(&pdev->dev, swrm);
2491 return 0;
2492}
2493
2494static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
2495{
2496 u32 val;
2497
2498 dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
2499 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD);
2500 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2501 val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK;
2502 swr_master_write(swrm, SWRM_MCP_CFG_ADDR, val);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302503
2504 return 0;
2505}
2506
2507#ifdef CONFIG_PM
2508static int swrm_runtime_resume(struct device *dev)
2509{
2510 struct platform_device *pdev = to_platform_device(dev);
2511 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2512 int ret = 0;
Vatsal Buchae50b5002019-09-19 14:32:20 +05302513 bool swrm_clk_req_err = false;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302514 bool hw_core_err = false;
2515 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302516 struct swr_master *mstr = &swrm->master;
2517 struct swr_device *swr_dev;
2518
2519 dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
2520 __func__, swrm->state);
2521 mutex_lock(&swrm->reslock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302522
Sudheer Papothi384addd2019-06-14 02:26:52 +05302523 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2524 dev_err(dev, "%s:lpass core hw enable failed\n",
2525 __func__);
2526 hw_core_err = true;
2527 }
2528 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2529 dev_err(dev, "%s:lpass audio hw enable failed\n",
2530 __func__);
2531 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002532 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302533
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302534 if ((swrm->state == SWR_MSTR_DOWN) ||
2535 (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) {
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302536 if (swrm->clk_stop_mode0_supp) {
2537 if (swrm->ipc_wakeup)
2538 msm_aud_evt_blocking_notifier_call_chain(
2539 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302540 }
2541
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302542 if (swrm_clk_request(swrm, true)) {
2543 /*
2544 * Set autosuspend timer to 1 for
2545 * master to enter into suspend.
2546 */
Vatsal Buchae50b5002019-09-19 14:32:20 +05302547 swrm_clk_req_err = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302548 goto exit;
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302549 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302550 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302551 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2552 ret = swr_device_up(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302553 if (ret == -ENODEV) {
2554 dev_dbg(dev,
2555 "%s slave device up not implemented\n",
2556 __func__);
2557 ret = 0;
2558 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302559 dev_err(dev,
2560 "%s: failed to wakeup swr dev %d\n",
2561 __func__, swr_dev->dev_num);
2562 swrm_clk_request(swrm, false);
2563 goto exit;
2564 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302565 }
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05302566 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2567 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2568 swrm_master_init(swrm);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302569 /* wait for hw enumeration to complete */
2570 usleep_range(100, 105);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302571 swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, 0x0,
2572 SWRS_SCP_INT_STATUS_MASK_1);
Karthikeyan Manif6821902019-05-21 17:31:24 -07002573 if (swrm->state == SWR_MSTR_SSR) {
2574 mutex_unlock(&swrm->reslock);
2575 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
2576 mutex_lock(&swrm->reslock);
2577 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302578 } else {
2579 /*wake up from clock stop*/
2580 swr_master_write(swrm, SWRM_MCP_BUS_CTRL_ADDR, 0x2);
2581 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302582 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302583 swrm->state = SWR_MSTR_UP;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302584 }
2585exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302586 if (!aud_core_err)
2587 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2588 if (!hw_core_err)
2589 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Vatsal Buchae50b5002019-09-19 14:32:20 +05302590 if (swrm_clk_req_err)
2591 pm_runtime_set_autosuspend_delay(&pdev->dev,
2592 ERR_AUTO_SUSPEND_TIMER_VAL);
2593 else
2594 pm_runtime_set_autosuspend_delay(&pdev->dev,
2595 auto_suspend_timer);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302596 mutex_unlock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302597
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302598 return ret;
2599}
2600
2601static int swrm_runtime_suspend(struct device *dev)
2602{
2603 struct platform_device *pdev = to_platform_device(dev);
2604 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2605 int ret = 0;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302606 bool hw_core_err = false;
2607 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302608 struct swr_master *mstr = &swrm->master;
2609 struct swr_device *swr_dev;
2610 int current_state = 0;
2611
2612 dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
2613 __func__, swrm->state);
2614 mutex_lock(&swrm->reslock);
2615 mutex_lock(&swrm->force_down_lock);
2616 current_state = swrm->state;
2617 mutex_unlock(&swrm->force_down_lock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302618
2619 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2620 dev_err(dev, "%s:lpass core hw enable failed\n",
2621 __func__);
2622 hw_core_err = true;
2623 }
2624 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2625 dev_err(dev, "%s:lpass audio hw enable failed\n",
2626 __func__);
2627 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002628 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302629
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302630 if ((current_state == SWR_MSTR_UP) ||
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302631 (current_state == SWR_MSTR_SSR)) {
2632
2633 if ((current_state != SWR_MSTR_SSR) &&
2634 swrm_is_port_en(&swrm->master)) {
2635 dev_dbg(dev, "%s ports are enabled\n", __func__);
2636 ret = -EBUSY;
2637 goto exit;
2638 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302639 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05302640 mutex_unlock(&swrm->reslock);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +05302641 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
Sudheer Papothi06f43412019-07-09 03:32:54 +05302642 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302643 swrm_clk_pause(swrm);
2644 swr_master_write(swrm, SWRM_COMP_CFG_ADDR, 0x00);
2645 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2646 ret = swr_device_down(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302647 if (ret == -ENODEV) {
2648 dev_dbg_ratelimited(dev,
2649 "%s slave device down not implemented\n",
2650 __func__);
2651 ret = 0;
2652 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302653 dev_err(dev,
2654 "%s: failed to shutdown swr dev %d\n",
2655 __func__, swr_dev->dev_num);
2656 goto exit;
2657 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302658 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302659 } else {
Sudheer Papothi384addd2019-06-14 02:26:52 +05302660 mutex_unlock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302661 /* clock stop sequence */
2662 swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
2663 SWRS_SCP_CONTROL);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302664 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302665 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302666 }
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07002667 ret = swrm_clk_request(swrm, false);
2668 if (ret) {
2669 dev_err(dev, "%s: swrmn clk failed\n", __func__);
2670 ret = 0;
2671 goto exit;
2672 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302673
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302674 if (swrm->clk_stop_mode0_supp) {
2675 if (swrm->wake_irq > 0) {
2676 enable_irq(swrm->wake_irq);
2677 } else if (swrm->ipc_wakeup) {
2678 msm_aud_evt_blocking_notifier_call_chain(
2679 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
2680 swrm->ipc_wakeup_triggered = false;
2681 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302682 }
2683
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302684 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302685 /* Retain SSR state until resume */
2686 if (current_state != SWR_MSTR_SSR)
2687 swrm->state = SWR_MSTR_DOWN;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302688exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302689 if (!aud_core_err)
2690 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2691 if (!hw_core_err)
2692 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302693 mutex_unlock(&swrm->reslock);
2694 return ret;
2695}
2696#endif /* CONFIG_PM */
2697
Sudheer Papothi06f43412019-07-09 03:32:54 +05302698static int swrm_device_suspend(struct device *dev)
2699{
2700 struct platform_device *pdev = to_platform_device(dev);
2701 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2702 int ret = 0;
2703
2704 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2705 if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
2706 ret = swrm_runtime_suspend(dev);
2707 if (!ret) {
2708 pm_runtime_disable(dev);
2709 pm_runtime_set_suspended(dev);
2710 pm_runtime_enable(dev);
2711 }
2712 }
2713
2714 return 0;
2715}
2716
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302717static int swrm_device_down(struct device *dev)
2718{
2719 struct platform_device *pdev = to_platform_device(dev);
2720 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302721
2722 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2723
2724 mutex_lock(&swrm->force_down_lock);
2725 swrm->state = SWR_MSTR_SSR;
2726 mutex_unlock(&swrm->force_down_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302727
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302728 swrm_device_suspend(dev);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302729 return 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302730}
2731
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302732int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm)
2733{
2734 int ret = 0;
Laxminath Kasama60239e2019-01-10 14:43:03 +05302735 int irq, dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302736
2737 if (!swrm->ipc_wakeup) {
Laxminath Kasama60239e2019-01-10 14:43:03 +05302738 irq = of_get_named_gpio(swrm->dev->of_node,
2739 "qcom,swr-wakeup-irq", 0);
2740 if (gpio_is_valid(irq)) {
2741 swrm->wake_irq = gpio_to_irq(irq);
2742 if (swrm->wake_irq < 0) {
2743 dev_err(swrm->dev,
2744 "Unable to configure irq\n");
2745 return swrm->wake_irq;
2746 }
2747 } else {
2748 dir_apps_irq = platform_get_irq_byname(swrm->pdev,
2749 "swr_wake_irq");
2750 if (dir_apps_irq < 0) {
2751 dev_err(swrm->dev,
2752 "TLMM connect gpio not found\n");
2753 return -EINVAL;
2754 }
2755 swrm->wake_irq = dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302756 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302757 ret = request_threaded_irq(swrm->wake_irq, NULL,
2758 swrm_wakeup_interrupt,
2759 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
2760 "swr_wake_irq", swrm);
2761 if (ret) {
2762 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2763 __func__, ret);
2764 return -EINVAL;
2765 }
Aditya Bavanari3517b112018-12-03 13:26:59 +05302766 irq_set_irq_wake(swrm->wake_irq, 1);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302767 }
2768 return ret;
2769}
2770
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302771static int swrm_alloc_port_mem(struct device *dev, struct swr_mstr_ctrl *swrm,
2772 u32 uc, u32 size)
2773{
2774 if (!swrm->port_param) {
2775 swrm->port_param = devm_kzalloc(dev,
2776 sizeof(swrm->port_param) * SWR_UC_MAX,
2777 GFP_KERNEL);
2778 if (!swrm->port_param)
2779 return -ENOMEM;
2780 }
2781 if (!swrm->port_param[uc]) {
2782 swrm->port_param[uc] = devm_kcalloc(dev, size,
2783 sizeof(struct port_params),
2784 GFP_KERNEL);
2785 if (!swrm->port_param[uc])
2786 return -ENOMEM;
2787 } else {
2788 dev_err_ratelimited(swrm->dev, "%s: called more than once\n",
2789 __func__);
2790 }
2791
2792 return 0;
2793}
2794
2795static int swrm_copy_port_config(struct swr_mstr_ctrl *swrm,
2796 struct swrm_port_config *port_cfg,
2797 u32 size)
2798{
2799 int idx;
2800 struct port_params *params;
2801 int uc = port_cfg->uc;
2802 int ret = 0;
2803
2804 for (idx = 0; idx < size; idx++) {
2805 params = &((struct port_params *)port_cfg->params)[idx];
2806 if (!params) {
2807 dev_err(swrm->dev, "%s: Invalid params\n", __func__);
2808 ret = -EINVAL;
2809 break;
2810 }
2811 memcpy(&swrm->port_param[uc][idx], params,
2812 sizeof(struct port_params));
2813 }
2814
2815 return ret;
2816}
2817
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302818/**
2819 * swrm_wcd_notify - parent device can notify to soundwire master through
2820 * this function
2821 * @pdev: pointer to platform device structure
2822 * @id: command id from parent to the soundwire master
2823 * @data: data from parent device to soundwire master
2824 */
2825int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
2826{
2827 struct swr_mstr_ctrl *swrm;
2828 int ret = 0;
2829 struct swr_master *mstr;
2830 struct swr_device *swr_dev;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302831 struct swrm_port_config *port_cfg;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302832
2833 if (!pdev) {
2834 pr_err("%s: pdev is NULL\n", __func__);
2835 return -EINVAL;
2836 }
2837 swrm = platform_get_drvdata(pdev);
2838 if (!swrm) {
2839 dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__);
2840 return -EINVAL;
2841 }
2842 mstr = &swrm->master;
2843
2844 switch (id) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05302845 case SWR_REQ_CLK_SWITCH:
2846 /* This will put soundwire in clock stop mode and disable the
2847 * clocks, if there is no active usecase running, so that the
2848 * next activity on soundwire will request clock from new clock
2849 * source.
2850 */
2851 mutex_lock(&swrm->mlock);
2852 if (swrm->state == SWR_MSTR_UP)
2853 swrm_device_suspend(&pdev->dev);
2854 mutex_unlock(&swrm->mlock);
2855 break;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302856 case SWR_CLK_FREQ:
2857 if (!data) {
2858 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
2859 ret = -EINVAL;
2860 } else {
2861 mutex_lock(&swrm->mlock);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302862 if (swrm->mclk_freq != *(int *)data) {
2863 dev_dbg(swrm->dev, "%s: freq change: force mstr down\n", __func__);
2864 if (swrm->state == SWR_MSTR_DOWN)
2865 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
2866 __func__, swrm->state);
2867 else
2868 swrm_device_suspend(&pdev->dev);
2869 }
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302870 swrm->mclk_freq = *(int *)data;
2871 mutex_unlock(&swrm->mlock);
2872 }
2873 break;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302874 case SWR_DEVICE_SSR_DOWN:
2875 mutex_lock(&swrm->devlock);
2876 swrm->dev_up = false;
2877 mutex_unlock(&swrm->devlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302878 mutex_lock(&swrm->reslock);
2879 swrm->state = SWR_MSTR_SSR;
2880 mutex_unlock(&swrm->reslock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302881 break;
2882 case SWR_DEVICE_SSR_UP:
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302883 /* wait for clk voting to be zero */
Ramprasad Katkam7f6462e2018-11-06 11:51:22 +05302884 reinit_completion(&swrm->clk_off_complete);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302885 if (swrm->clk_ref_count &&
2886 !wait_for_completion_timeout(&swrm->clk_off_complete,
Ramprasad Katkamc87efeb2018-12-12 19:26:19 +05302887 msecs_to_jiffies(500)))
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302888 dev_err(swrm->dev, "%s: clock voting not zero\n",
2889 __func__);
2890
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302891 mutex_lock(&swrm->devlock);
2892 swrm->dev_up = true;
2893 mutex_unlock(&swrm->devlock);
2894 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302895 case SWR_DEVICE_DOWN:
2896 dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
2897 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302898 if (swrm->state == SWR_MSTR_DOWN)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302899 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
2900 __func__, swrm->state);
2901 else
2902 swrm_device_down(&pdev->dev);
2903 mutex_unlock(&swrm->mlock);
2904 break;
2905 case SWR_DEVICE_UP:
2906 dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
Ramprasad Katkam0fed92f2018-11-08 14:22:22 +05302907 mutex_lock(&swrm->devlock);
2908 if (!swrm->dev_up) {
2909 dev_dbg(swrm->dev, "SSR not complete yet\n");
2910 mutex_unlock(&swrm->devlock);
2911 return -EBUSY;
2912 }
2913 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302914 mutex_lock(&swrm->mlock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302915 pm_runtime_mark_last_busy(&pdev->dev);
2916 pm_runtime_get_sync(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302917 mutex_lock(&swrm->reslock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302918 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2919 ret = swr_reset_device(swr_dev);
2920 if (ret) {
2921 dev_err(swrm->dev,
2922 "%s: failed to reset swr device %d\n",
2923 __func__, swr_dev->dev_num);
2924 swrm_clk_request(swrm, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302925 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302926 }
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302927 pm_runtime_mark_last_busy(&pdev->dev);
2928 pm_runtime_put_autosuspend(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302929 mutex_unlock(&swrm->reslock);
2930 mutex_unlock(&swrm->mlock);
2931 break;
2932 case SWR_SET_NUM_RX_CH:
2933 if (!data) {
2934 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
2935 ret = -EINVAL;
2936 } else {
2937 mutex_lock(&swrm->mlock);
2938 swrm->num_rx_chs = *(int *)data;
2939 if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
2940 list_for_each_entry(swr_dev, &mstr->devices,
2941 dev_list) {
2942 ret = swr_set_device_group(swr_dev,
2943 SWR_BROADCAST);
2944 if (ret)
2945 dev_err(swrm->dev,
2946 "%s: set num ch failed\n",
2947 __func__);
2948 }
2949 } else {
2950 list_for_each_entry(swr_dev, &mstr->devices,
2951 dev_list) {
2952 ret = swr_set_device_group(swr_dev,
2953 SWR_GROUP_NONE);
2954 if (ret)
2955 dev_err(swrm->dev,
2956 "%s: set num ch failed\n",
2957 __func__);
2958 }
2959 }
2960 mutex_unlock(&swrm->mlock);
2961 }
2962 break;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302963 case SWR_REGISTER_WAKE_IRQ:
2964 if (!data) {
2965 dev_err(swrm->dev, "%s: reg wake irq data is NULL\n",
2966 __func__);
2967 ret = -EINVAL;
2968 } else {
2969 mutex_lock(&swrm->mlock);
2970 swrm->ipc_wakeup = *(u32 *)data;
2971 ret = swrm_register_wake_irq(swrm);
2972 if (ret)
2973 dev_err(swrm->dev, "%s: register wake_irq failed\n",
2974 __func__);
2975 mutex_unlock(&swrm->mlock);
2976 }
2977 break;
Sudheer Papothi72ee2642019-08-08 05:15:17 +05302978 case SWR_REGISTER_WAKEUP:
2979 msm_aud_evt_blocking_notifier_call_chain(
2980 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
2981 break;
2982 case SWR_DEREGISTER_WAKEUP:
2983 msm_aud_evt_blocking_notifier_call_chain(
2984 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
2985 break;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302986 case SWR_SET_PORT_MAP:
2987 if (!data) {
2988 dev_err(swrm->dev, "%s: data is NULL for id=%d\n",
2989 __func__, id);
2990 ret = -EINVAL;
2991 } else {
2992 mutex_lock(&swrm->mlock);
2993 port_cfg = (struct swrm_port_config *)data;
2994 if (!port_cfg->size) {
2995 ret = -EINVAL;
2996 goto done;
2997 }
2998 ret = swrm_alloc_port_mem(&pdev->dev, swrm,
2999 port_cfg->uc, port_cfg->size);
3000 if (!ret)
3001 swrm_copy_port_config(swrm, port_cfg,
3002 port_cfg->size);
3003done:
3004 mutex_unlock(&swrm->mlock);
3005 }
3006 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303007 default:
3008 dev_err(swrm->dev, "%s: swr master unknown id %d\n",
3009 __func__, id);
3010 break;
3011 }
3012 return ret;
3013}
3014EXPORT_SYMBOL(swrm_wcd_notify);
3015
Ramprasad Katkam57349872018-11-11 18:34:57 +05303016/*
3017 * swrm_pm_cmpxchg:
3018 * Check old state and exchange with pm new state
3019 * if old state matches with current state
3020 *
3021 * @swrm: pointer to wcd core resource
3022 * @o: pm old state
3023 * @n: pm new state
3024 *
3025 * Returns old state
3026 */
3027static enum swrm_pm_state swrm_pm_cmpxchg(
3028 struct swr_mstr_ctrl *swrm,
3029 enum swrm_pm_state o,
3030 enum swrm_pm_state n)
3031{
3032 enum swrm_pm_state old;
3033
3034 if (!swrm)
3035 return o;
3036
3037 mutex_lock(&swrm->pm_lock);
3038 old = swrm->pm_state;
3039 if (old == o)
3040 swrm->pm_state = n;
3041 mutex_unlock(&swrm->pm_lock);
3042
3043 return old;
3044}
3045
3046static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm)
3047{
3048 enum swrm_pm_state os;
3049
3050 /*
3051 * swrm_{lock/unlock}_sleep will be called by swr irq handler
3052 * and slave wake up requests..
3053 *
3054 * If system didn't resume, we can simply return false so
3055 * IRQ handler can return without handling IRQ.
3056 */
3057 mutex_lock(&swrm->pm_lock);
3058 if (swrm->wlock_holders++ == 0) {
3059 dev_dbg(swrm->dev, "%s: holding wake lock\n", __func__);
3060 pm_qos_update_request(&swrm->pm_qos_req,
3061 msm_cpuidle_get_deep_idle_latency());
3062 pm_stay_awake(swrm->dev);
3063 }
3064 mutex_unlock(&swrm->pm_lock);
3065
3066 if (!wait_event_timeout(swrm->pm_wq,
3067 ((os = swrm_pm_cmpxchg(swrm,
3068 SWRM_PM_SLEEPABLE,
3069 SWRM_PM_AWAKE)) ==
3070 SWRM_PM_SLEEPABLE ||
3071 (os == SWRM_PM_AWAKE)),
3072 msecs_to_jiffies(
3073 SWRM_SYSTEM_RESUME_TIMEOUT_MS))) {
3074 dev_err(swrm->dev, "%s: system didn't resume within %dms, s %d, w %d\n",
3075 __func__, SWRM_SYSTEM_RESUME_TIMEOUT_MS, swrm->pm_state,
3076 swrm->wlock_holders);
3077 swrm_unlock_sleep(swrm);
3078 return false;
3079 }
3080 wake_up_all(&swrm->pm_wq);
3081 return true;
3082}
3083
3084static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm)
3085{
3086 mutex_lock(&swrm->pm_lock);
3087 if (--swrm->wlock_holders == 0) {
3088 dev_dbg(swrm->dev, "%s: releasing wake lock pm_state %d -> %d\n",
3089 __func__, swrm->pm_state, SWRM_PM_SLEEPABLE);
3090 /*
3091 * if swrm_lock_sleep failed, pm_state would be still
3092 * swrm_PM_ASLEEP, don't overwrite
3093 */
3094 if (likely(swrm->pm_state == SWRM_PM_AWAKE))
3095 swrm->pm_state = SWRM_PM_SLEEPABLE;
3096 pm_qos_update_request(&swrm->pm_qos_req,
3097 PM_QOS_DEFAULT_VALUE);
3098 pm_relax(swrm->dev);
3099 }
3100 mutex_unlock(&swrm->pm_lock);
3101 wake_up_all(&swrm->pm_wq);
3102}
3103
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303104#ifdef CONFIG_PM_SLEEP
3105static int swrm_suspend(struct device *dev)
3106{
3107 int ret = -EBUSY;
3108 struct platform_device *pdev = to_platform_device(dev);
3109 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3110
3111 dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
Ramprasad Katkam57349872018-11-11 18:34:57 +05303112
3113 mutex_lock(&swrm->pm_lock);
3114
3115 if (swrm->pm_state == SWRM_PM_SLEEPABLE) {
3116 dev_dbg(swrm->dev, "%s: suspending system, state %d, wlock %d\n",
3117 __func__, swrm->pm_state,
3118 swrm->wlock_holders);
3119 swrm->pm_state = SWRM_PM_ASLEEP;
3120 } else if (swrm->pm_state == SWRM_PM_AWAKE) {
3121 /*
3122 * unlock to wait for pm_state == SWRM_PM_SLEEPABLE
3123 * then set to SWRM_PM_ASLEEP
3124 */
3125 dev_dbg(swrm->dev, "%s: waiting to suspend system, state %d, wlock %d\n",
3126 __func__, swrm->pm_state,
3127 swrm->wlock_holders);
3128 mutex_unlock(&swrm->pm_lock);
3129 if (!(wait_event_timeout(swrm->pm_wq, swrm_pm_cmpxchg(
3130 swrm, SWRM_PM_SLEEPABLE,
3131 SWRM_PM_ASLEEP) ==
3132 SWRM_PM_SLEEPABLE,
3133 msecs_to_jiffies(
3134 SWRM_SYS_SUSPEND_WAIT)))) {
3135 dev_dbg(swrm->dev, "%s: suspend failed state %d, wlock %d\n",
3136 __func__, swrm->pm_state,
3137 swrm->wlock_holders);
3138 return -EBUSY;
3139 } else {
3140 dev_dbg(swrm->dev,
3141 "%s: done, state %d, wlock %d\n",
3142 __func__, swrm->pm_state,
3143 swrm->wlock_holders);
3144 }
3145 mutex_lock(&swrm->pm_lock);
3146 } else if (swrm->pm_state == SWRM_PM_ASLEEP) {
3147 dev_dbg(swrm->dev, "%s: system is already suspended, state %d, wlock %d\n",
3148 __func__, swrm->pm_state,
3149 swrm->wlock_holders);
3150 }
3151
3152 mutex_unlock(&swrm->pm_lock);
3153
3154 if ((!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev))) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303155 ret = swrm_runtime_suspend(dev);
3156 if (!ret) {
3157 /*
3158 * Synchronize runtime-pm and system-pm states:
3159 * At this point, we are already suspended. If
3160 * runtime-pm still thinks its active, then
3161 * make sure its status is in sync with HW
3162 * status. The three below calls let the
3163 * runtime-pm know that we are suspended
3164 * already without re-invoking the suspend
3165 * callback
3166 */
3167 pm_runtime_disable(dev);
3168 pm_runtime_set_suspended(dev);
3169 pm_runtime_enable(dev);
3170 }
3171 }
3172 if (ret == -EBUSY) {
3173 /*
3174 * There is a possibility that some audio stream is active
3175 * during suspend. We dont want to return suspend failure in
3176 * that case so that display and relevant components can still
3177 * go to suspend.
3178 * If there is some other error, then it should be passed-on
3179 * to system level suspend
3180 */
3181 ret = 0;
3182 }
3183 return ret;
3184}
3185
3186static int swrm_resume(struct device *dev)
3187{
3188 int ret = 0;
3189 struct platform_device *pdev = to_platform_device(dev);
3190 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3191
3192 dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
3193 if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
3194 ret = swrm_runtime_resume(dev);
3195 if (!ret) {
3196 pm_runtime_mark_last_busy(dev);
3197 pm_request_autosuspend(dev);
3198 }
3199 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05303200 mutex_lock(&swrm->pm_lock);
3201 if (swrm->pm_state == SWRM_PM_ASLEEP) {
3202 dev_dbg(swrm->dev,
3203 "%s: resuming system, state %d, wlock %d\n",
3204 __func__, swrm->pm_state,
3205 swrm->wlock_holders);
3206 swrm->pm_state = SWRM_PM_SLEEPABLE;
3207 } else {
3208 dev_dbg(swrm->dev, "%s: system is already awake, state %d wlock %d\n",
3209 __func__, swrm->pm_state,
3210 swrm->wlock_holders);
3211 }
3212 mutex_unlock(&swrm->pm_lock);
3213 wake_up_all(&swrm->pm_wq);
3214
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303215 return ret;
3216}
3217#endif /* CONFIG_PM_SLEEP */
3218
3219static const struct dev_pm_ops swrm_dev_pm_ops = {
3220 SET_SYSTEM_SLEEP_PM_OPS(
3221 swrm_suspend,
3222 swrm_resume
3223 )
3224 SET_RUNTIME_PM_OPS(
3225 swrm_runtime_suspend,
3226 swrm_runtime_resume,
3227 NULL
3228 )
3229};
3230
3231static const struct of_device_id swrm_dt_match[] = {
3232 {
3233 .compatible = "qcom,swr-mstr",
3234 },
3235 {}
3236};
3237
3238static struct platform_driver swr_mstr_driver = {
3239 .probe = swrm_probe,
3240 .remove = swrm_remove,
3241 .driver = {
3242 .name = SWR_WCD_NAME,
3243 .owner = THIS_MODULE,
3244 .pm = &swrm_dev_pm_ops,
3245 .of_match_table = swrm_dt_match,
Xiaojun Sang53cd13a2018-06-29 15:14:37 +08003246 .suppress_bind_attrs = true,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303247 },
3248};
3249
3250static int __init swrm_init(void)
3251{
3252 return platform_driver_register(&swr_mstr_driver);
3253}
3254module_init(swrm_init);
3255
3256static void __exit swrm_exit(void)
3257{
3258 platform_driver_unregister(&swr_mstr_driver);
3259}
3260module_exit(swrm_exit);
3261
3262MODULE_LICENSE("GPL v2");
3263MODULE_DESCRIPTION("SoundWire Master Controller");
3264MODULE_ALIAS("platform:swr-mstr");