blob: 3bf59e9ed8f5578c9106e9aa81da8b0f01c0a675 [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
1610 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1611 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1612 return IRQ_NONE;
1613 }
1614
1615 mutex_lock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301616 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
1617 ret = IRQ_NONE;
1618 goto exit;
1619 }
1620 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
1621 ret = IRQ_NONE;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301622 goto err_audio_hw_vote;
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001623 }
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301624 swrm_clk_request(swrm, true);
1625 mutex_unlock(&swrm->reslock);
1626
1627 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1628 intr_sts_masked = intr_sts & swrm->intr_mask;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301629
1630 dev_dbg(swrm->dev, "%s: status: 0x%x \n", __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301631handle_irq:
1632 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
1633 value = intr_sts_masked & (1 << i);
1634 if (!value)
1635 continue;
1636
1637 switch (value) {
1638 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1639 dev_dbg(swrm->dev, "%s: Trigger irq to slave device\n",
1640 __func__);
1641 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1642 ret = swrm_find_alert_slave(swrm, status, &devnum);
1643 if (ret) {
1644 dev_err_ratelimited(swrm->dev,
1645 "%s: no slave alert found.spurious interrupt\n",
1646 __func__);
1647 break;
1648 }
1649 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1650 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1651 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1652 SWRS_SCP_INT_STATUS_CLEAR_1);
1653 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1654 SWRS_SCP_INT_STATUS_CLEAR_1);
1655
1656
1657 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1658 if (swr_dev->dev_num != devnum)
1659 continue;
1660 if (swr_dev->slave_irq) {
1661 do {
1662 handle_nested_irq(
1663 irq_find_mapping(
1664 swr_dev->slave_irq, 0));
1665 } while (swr_dev->slave_irq_pending);
1666 }
1667
1668 }
1669 break;
1670 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1671 dev_dbg(swrm->dev, "%s: SWR new slave attached\n",
1672 __func__);
1673 break;
1674 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1675 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1676 if (status == swrm->slave_status) {
1677 dev_dbg(swrm->dev,
1678 "%s: No change in slave status: %d\n",
1679 __func__, status);
1680 break;
1681 }
1682 chg_sts = swrm_check_slave_change_status(swrm, status,
1683 &devnum);
1684 switch (chg_sts) {
1685 case SWR_NOT_PRESENT:
1686 dev_dbg(swrm->dev,
1687 "%s: device %d got detached\n",
1688 __func__, devnum);
1689 break;
1690 case SWR_ATTACHED_OK:
1691 dev_dbg(swrm->dev,
1692 "%s: device %d got attached\n",
1693 __func__, devnum);
1694 /* enable host irq from slave device*/
1695 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1696 SWRS_SCP_INT_STATUS_CLEAR_1);
1697 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1698 SWRS_SCP_INT_STATUS_MASK_1);
1699
1700 break;
1701 case SWR_ALERT:
1702 dev_dbg(swrm->dev,
1703 "%s: device %d has pending interrupt\n",
1704 __func__, devnum);
1705 break;
1706 }
1707 break;
1708 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1709 dev_err_ratelimited(swrm->dev,
1710 "%s: SWR bus clsh detected\n",
1711 __func__);
1712 break;
1713 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1714 dev_dbg(swrm->dev, "%s: SWR read FIFO overflow\n",
1715 __func__);
1716 break;
1717 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1718 dev_dbg(swrm->dev, "%s: SWR read FIFO underflow\n",
1719 __func__);
1720 break;
1721 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1722 dev_dbg(swrm->dev, "%s: SWR write FIFO overflow\n",
1723 __func__);
1724 break;
1725 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1726 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1727 dev_err_ratelimited(swrm->dev,
1728 "%s: SWR CMD error, fifo status 0x%x, flushing fifo\n",
1729 __func__, value);
1730 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1731 break;
1732 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
1733 dev_err_ratelimited(swrm->dev,
1734 "%s: SWR Port collision detected\n",
1735 __func__);
1736 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
1737 swr_master_write(swrm,
1738 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1739 break;
1740 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1741 dev_dbg(swrm->dev,
1742 "%s: SWR read enable valid mismatch\n",
1743 __func__);
1744 swrm->intr_mask &=
1745 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1746 swr_master_write(swrm,
1747 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1748 break;
1749 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1750 complete(&swrm->broadcast);
1751 dev_dbg(swrm->dev, "%s: SWR cmd id finished\n",
1752 __func__);
1753 break;
1754 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED_V2:
1755 break;
1756 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL_V2:
1757 break;
1758 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED_V2:
1759 break;
1760 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED_V2:
1761 break;
1762 case SWRM_INTERRUPT_STATUS_EXT_CLK_STOP_WAKEUP:
1763 if (swrm->state == SWR_MSTR_UP)
1764 dev_dbg(swrm->dev,
1765 "%s:SWR Master is already up\n",
1766 __func__);
1767 else
1768 dev_err_ratelimited(swrm->dev,
1769 "%s: SWR wokeup during clock stop\n",
1770 __func__);
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301771 /* It might be possible the slave device gets reset
1772 * and slave interrupt gets missed. So re-enable
1773 * Host IRQ and process slave pending
1774 * interrupts, if any.
1775 */
1776 swrm_enable_slave_irq(swrm);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301777 break;
1778 default:
1779 dev_err_ratelimited(swrm->dev,
1780 "%s: SWR unknown interrupt value: %d\n",
1781 __func__, value);
1782 ret = IRQ_NONE;
1783 break;
1784 }
1785 }
1786 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1787 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
1788
1789 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1790 intr_sts_masked = intr_sts & swrm->intr_mask;
1791
1792 if (intr_sts_masked) {
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301793 dev_dbg(swrm->dev, "%s: new interrupt received 0x%x\n",
1794 __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301795 goto handle_irq;
1796 }
1797
1798 mutex_lock(&swrm->reslock);
1799 swrm_clk_request(swrm, false);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301800 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
Sudheer Papothi06f43412019-07-09 03:32:54 +05301801
1802err_audio_hw_vote:
Sudheer Papothi384addd2019-06-14 02:26:52 +05301803 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001804exit:
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301805 mutex_unlock(&swrm->reslock);
1806 swrm_unlock_sleep(swrm);
1807 return ret;
1808}
1809
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301810static irqreturn_t swrm_wakeup_interrupt(int irq, void *dev)
1811{
1812 struct swr_mstr_ctrl *swrm = dev;
1813 int ret = IRQ_HANDLED;
1814
1815 if (!swrm || !(swrm->dev)) {
1816 pr_err("%s: swrm or dev is null\n", __func__);
1817 return IRQ_NONE;
1818 }
1819 mutex_lock(&swrm->devlock);
1820 if (!swrm->dev_up) {
1821 if (swrm->wake_irq > 0)
1822 disable_irq_nosync(swrm->wake_irq);
1823 mutex_unlock(&swrm->devlock);
1824 return ret;
1825 }
1826 mutex_unlock(&swrm->devlock);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301827 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1828 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1829 goto exit;
1830 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301831 if (swrm->wake_irq > 0)
1832 disable_irq_nosync(swrm->wake_irq);
1833 pm_runtime_get_sync(swrm->dev);
1834 pm_runtime_mark_last_busy(swrm->dev);
1835 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301836 swrm_unlock_sleep(swrm);
1837exit:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301838 return ret;
1839}
1840
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301841static void swrm_wakeup_work(struct work_struct *work)
1842{
1843 struct swr_mstr_ctrl *swrm;
1844
1845 swrm = container_of(work, struct swr_mstr_ctrl,
1846 wakeup_work);
1847 if (!swrm || !(swrm->dev)) {
1848 pr_err("%s: swrm or dev is null\n", __func__);
1849 return;
1850 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301851
1852 mutex_lock(&swrm->devlock);
1853 if (!swrm->dev_up) {
1854 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301855 goto exit;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301856 }
1857 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301858 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1859 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1860 goto exit;
1861 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301862 pm_runtime_get_sync(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301863 pm_runtime_mark_last_busy(swrm->dev);
1864 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301865 swrm_unlock_sleep(swrm);
1866exit:
1867 pm_relax(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301868}
1869
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301870static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
1871{
1872 u32 val;
1873
1874 swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1875 val = (swrm->slave_status >> (devnum * 2));
1876 val &= SWRM_MCP_SLV_STATUS_MASK;
1877 return val;
1878}
1879
1880static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
1881 u8 *dev_num)
1882{
1883 int i;
1884 u64 id = 0;
1885 int ret = -EINVAL;
1886 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1887 struct swr_device *swr_dev;
1888 u32 num_dev = 0;
1889
1890 if (!swrm) {
1891 pr_err("%s: Invalid handle to swr controller\n",
1892 __func__);
1893 return ret;
1894 }
1895 if (swrm->num_dev)
1896 num_dev = swrm->num_dev;
1897 else
1898 num_dev = mstr->num_dev;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301899
1900 mutex_lock(&swrm->devlock);
1901 if (!swrm->dev_up) {
1902 mutex_unlock(&swrm->devlock);
1903 return ret;
1904 }
1905 mutex_unlock(&swrm->devlock);
1906
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301907 pm_runtime_get_sync(swrm->dev);
1908 for (i = 1; i < (num_dev + 1); i++) {
1909 id = ((u64)(swr_master_read(swrm,
1910 SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
1911 id |= swr_master_read(swrm,
1912 SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301913
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301914 /*
1915 * As pm_runtime_get_sync() brings all slaves out of reset
1916 * update logical device number for all slaves.
1917 */
1918 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1919 if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
1920 u32 status = swrm_get_device_status(swrm, i);
1921
1922 if ((status == 0x01) || (status == 0x02)) {
1923 swr_dev->dev_num = i;
1924 if ((id & SWR_DEV_ID_MASK) == dev_id) {
1925 *dev_num = i;
1926 ret = 0;
1927 }
1928 dev_dbg(swrm->dev,
1929 "%s: devnum %d is assigned for dev addr %lx\n",
1930 __func__, i, swr_dev->addr);
1931 }
1932 }
1933 }
1934 }
1935 if (ret)
1936 dev_err(swrm->dev, "%s: device 0x%llx is not ready\n",
1937 __func__, dev_id);
1938
1939 pm_runtime_mark_last_busy(swrm->dev);
1940 pm_runtime_put_autosuspend(swrm->dev);
1941 return ret;
1942}
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301943
1944static void swrm_device_wakeup_vote(struct swr_master *mstr)
1945{
1946 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1947
1948 if (!swrm) {
1949 pr_err("%s: Invalid handle to swr controller\n",
1950 __func__);
1951 return;
1952 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05301953 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1954 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1955 return;
1956 }
Sudheer Papothi384addd2019-06-14 02:26:52 +05301957 if (++swrm->hw_core_clk_en == 1)
1958 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
1959 dev_err(swrm->dev, "%s:lpass core hw enable failed\n",
1960 __func__);
1961 --swrm->hw_core_clk_en;
1962 }
1963 if ( ++swrm->aud_core_clk_en == 1)
1964 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
1965 dev_err(swrm->dev, "%s:lpass audio hw enable failed\n",
1966 __func__);
1967 --swrm->aud_core_clk_en;
1968 }
1969 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
1970 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301971 pm_runtime_get_sync(swrm->dev);
1972}
1973
1974static void swrm_device_wakeup_unvote(struct swr_master *mstr)
1975{
1976 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1977
1978 if (!swrm) {
1979 pr_err("%s: Invalid handle to swr controller\n",
1980 __func__);
1981 return;
1982 }
1983 pm_runtime_mark_last_busy(swrm->dev);
1984 pm_runtime_put_autosuspend(swrm->dev);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301985 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
1986 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
1987
1988 --swrm->aud_core_clk_en;
1989 if (swrm->aud_core_clk_en < 0)
1990 swrm->aud_core_clk_en = 0;
1991 else if (swrm->aud_core_clk_en == 0)
1992 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
1993
1994 --swrm->hw_core_clk_en;
1995 if (swrm->hw_core_clk_en < 0)
1996 swrm->hw_core_clk_en = 0;
1997 else if (swrm->hw_core_clk_en == 0)
1998 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
1999
Ramprasad Katkam57349872018-11-11 18:34:57 +05302000 swrm_unlock_sleep(swrm);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302001}
2002
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302003static int swrm_master_init(struct swr_mstr_ctrl *swrm)
2004{
2005 int ret = 0;
2006 u32 val;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302007 u8 row_ctrl = SWR_ROW_50;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302008 u8 col_ctrl = SWR_MIN_COL;
2009 u8 ssp_period = 1;
2010 u8 retry_cmd_num = 3;
2011 u32 reg[SWRM_MAX_INIT_REG];
2012 u32 value[SWRM_MAX_INIT_REG];
2013 int len = 0;
2014
2015 /* Clear Rows and Cols */
2016 val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
2017 (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
2018 (ssp_period << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
2019
2020 reg[len] = SWRM_MCP_FRAME_CTRL_BANK_ADDR(0);
2021 value[len++] = val;
2022
2023 /* Set Auto enumeration flag */
2024 reg[len] = SWRM_ENUMERATOR_CFG_ADDR;
2025 value[len++] = 1;
2026
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302027 /* Configure No pings */
2028 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2029 val &= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK;
2030 val |= (0x1f << SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT);
2031 reg[len] = SWRM_MCP_CFG_ADDR;
2032 value[len++] = val;
2033
2034 /* Configure number of retries of a read/write cmd */
2035 val = (retry_cmd_num << SWRM_CMD_FIFO_CFG_NUM_OF_CMD_RETRY_SHFT);
2036 reg[len] = SWRM_CMD_FIFO_CFG_ADDR;
2037 value[len++] = val;
2038
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302039 reg[len] = SWRM_MCP_BUS_CTRL_ADDR;
2040 value[len++] = 0x2;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302041
Ramprasad Katkam83303512018-10-11 17:34:22 +05302042 /* Set IRQ to PULSE */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302043 reg[len] = SWRM_COMP_CFG_ADDR;
Ramprasad Katkam83303512018-10-11 17:34:22 +05302044 value[len++] = 0x02;
2045
2046 reg[len] = SWRM_COMP_CFG_ADDR;
2047 value[len++] = 0x03;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302048
2049 reg[len] = SWRM_INTERRUPT_CLEAR;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302050 value[len++] = 0xFFFFFFFF;
2051
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302052 swrm->intr_mask = SWRM_INTERRUPT_STATUS_MASK;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302053 /* Mask soundwire interrupts */
2054 reg[len] = SWRM_INTERRUPT_MASK_ADDR;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302055 value[len++] = swrm->intr_mask;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302056
2057 reg[len] = SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302058 value[len++] = swrm->intr_mask;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302059
2060 swr_master_bulk_write(swrm, reg, value, len);
2061
Sudheer Papothi63f48152018-11-15 01:08:03 +05302062 /*
2063 * For SWR master version 1.5.1, continue
2064 * execute on command ignore.
2065 */
2066 if (swrm->version == SWRM_VERSION_1_5_1)
2067 swr_master_write(swrm, SWRM_CMD_FIFO_CFG_ADDR,
2068 (swr_master_read(swrm,
2069 SWRM_CMD_FIFO_CFG_ADDR) | 0x80000000));
2070
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302071 return ret;
2072}
2073
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302074static int swrm_event_notify(struct notifier_block *self,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302075 unsigned long action, void *data)
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302076{
2077 struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302078 event_notifier);
2079
2080 if (!swrm || !(swrm->dev)) {
2081 pr_err("%s: swrm or dev is NULL\n", __func__);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302082 return -EINVAL;
2083 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302084 switch (action) {
2085 case MSM_AUD_DC_EVENT:
2086 schedule_work(&(swrm->dc_presence_work));
2087 break;
2088 case SWR_WAKE_IRQ_EVENT:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302089 if (swrm->ipc_wakeup && !swrm->ipc_wakeup_triggered) {
2090 swrm->ipc_wakeup_triggered = true;
Ramprasad Katkam57349872018-11-11 18:34:57 +05302091 pm_stay_awake(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302092 schedule_work(&swrm->wakeup_work);
Ramprasad Katkamcd61c6e2018-09-18 13:22:58 +05302093 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302094 break;
2095 default:
2096 dev_err(swrm->dev, "%s: invalid event type: %lu\n",
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302097 __func__, action);
2098 return -EINVAL;
2099 }
2100
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302101 return 0;
2102}
2103
2104static void swrm_notify_work_fn(struct work_struct *work)
2105{
2106 struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302107 dc_presence_work);
2108
2109 if (!swrm || !swrm->pdev) {
2110 pr_err("%s: swrm or pdev is NULL\n", __func__);
2111 return;
2112 }
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302113 swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL);
2114}
2115
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302116static int swrm_probe(struct platform_device *pdev)
2117{
2118 struct swr_mstr_ctrl *swrm;
2119 struct swr_ctrl_platform_data *pdata;
2120 u32 i, num_ports, port_num, port_type, ch_mask;
2121 u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0;
2122 int ret = 0;
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302123 struct clk *lpass_core_hw_vote = NULL;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302124 struct clk *lpass_core_audio = NULL;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302125
2126 /* Allocate soundwire master driver structure */
2127 swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl),
2128 GFP_KERNEL);
2129 if (!swrm) {
2130 ret = -ENOMEM;
2131 goto err_memory_fail;
2132 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302133 swrm->pdev = pdev;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302134 swrm->dev = &pdev->dev;
2135 platform_set_drvdata(pdev, swrm);
2136 swr_set_ctrl_data(&swrm->master, swrm);
2137 pdata = dev_get_platdata(&pdev->dev);
2138 if (!pdata) {
2139 dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
2140 __func__);
2141 ret = -EINVAL;
2142 goto err_pdata_fail;
2143 }
2144 swrm->handle = (void *)pdata->handle;
2145 if (!swrm->handle) {
2146 dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
2147 __func__);
2148 ret = -EINVAL;
2149 goto err_pdata_fail;
2150 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302151 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr_master_id",
2152 &swrm->master_id);
2153 if (ret) {
2154 dev_err(&pdev->dev, "%s: failed to get master id\n", __func__);
2155 goto err_pdata_fail;
2156 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302157 if (!(of_property_read_u32(pdev->dev.of_node,
2158 "swrm-io-base", &swrm->swrm_base_reg)))
2159 ret = of_property_read_u32(pdev->dev.of_node,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302160 "swrm-io-base", &swrm->swrm_base_reg);
2161 if (!swrm->swrm_base_reg) {
2162 swrm->read = pdata->read;
2163 if (!swrm->read) {
2164 dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
2165 __func__);
2166 ret = -EINVAL;
2167 goto err_pdata_fail;
2168 }
2169 swrm->write = pdata->write;
2170 if (!swrm->write) {
2171 dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
2172 __func__);
2173 ret = -EINVAL;
2174 goto err_pdata_fail;
2175 }
2176 swrm->bulk_write = pdata->bulk_write;
2177 if (!swrm->bulk_write) {
2178 dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
2179 __func__);
2180 ret = -EINVAL;
2181 goto err_pdata_fail;
2182 }
2183 } else {
2184 swrm->swrm_dig_base = devm_ioremap(&pdev->dev,
2185 swrm->swrm_base_reg, SWRM_MAX_REGISTER);
2186 }
2187
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07002188 swrm->core_vote = pdata->core_vote;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302189 swrm->clk = pdata->clk;
2190 if (!swrm->clk) {
2191 dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
2192 __func__);
2193 ret = -EINVAL;
2194 goto err_pdata_fail;
2195 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302196 if (of_property_read_u32(pdev->dev.of_node,
2197 "qcom,swr-clock-stop-mode0",
2198 &swrm->clk_stop_mode0_supp)) {
2199 swrm->clk_stop_mode0_supp = FALSE;
2200 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302201
2202 ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
2203 &swrm->num_dev);
2204 if (ret) {
2205 dev_dbg(&pdev->dev, "%s: Looking up %s property failed\n",
2206 __func__, "qcom,swr-num-dev");
2207 } else {
2208 if (swrm->num_dev > SWR_MAX_SLAVE_DEVICES) {
2209 dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
2210 __func__, swrm->num_dev, SWR_MAX_SLAVE_DEVICES);
2211 ret = -EINVAL;
2212 goto err_pdata_fail;
2213 }
2214 }
2215
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302216 /* Parse soundwire port mapping */
2217 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
2218 &num_ports);
2219 if (ret) {
2220 dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
2221 goto err_pdata_fail;
2222 }
2223 swrm->num_ports = num_ports;
2224
2225 if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
2226 &map_size)) {
2227 dev_err(swrm->dev, "missing port mapping\n");
2228 goto err_pdata_fail;
2229 }
2230
2231 map_length = map_size / (3 * sizeof(u32));
2232 if (num_ports > SWR_MSTR_PORT_LEN) {
2233 dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
2234 __func__);
2235 ret = -EINVAL;
2236 goto err_pdata_fail;
2237 }
2238 temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
2239
2240 if (!temp) {
2241 ret = -ENOMEM;
2242 goto err_pdata_fail;
2243 }
2244 ret = of_property_read_u32_array(pdev->dev.of_node,
2245 "qcom,swr-port-mapping", temp, 3 * map_length);
2246 if (ret) {
2247 dev_err(swrm->dev, "%s: Failed to read port mapping\n",
2248 __func__);
2249 goto err_pdata_fail;
2250 }
2251
2252 for (i = 0; i < map_length; i++) {
2253 port_num = temp[3 * i];
2254 port_type = temp[3 * i + 1];
2255 ch_mask = temp[3 * i + 2];
2256
2257 if (port_num != old_port_num)
2258 ch_iter = 0;
2259 swrm->port_mapping[port_num][ch_iter].port_type = port_type;
2260 swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
2261 old_port_num = port_num;
2262 }
2263 devm_kfree(&pdev->dev, temp);
2264
2265 swrm->reg_irq = pdata->reg_irq;
2266 swrm->master.read = swrm_read;
2267 swrm->master.write = swrm_write;
2268 swrm->master.bulk_write = swrm_bulk_write;
2269 swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
2270 swrm->master.connect_port = swrm_connect_port;
2271 swrm->master.disconnect_port = swrm_disconnect_port;
2272 swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
2273 swrm->master.remove_from_group = swrm_remove_from_group;
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302274 swrm->master.device_wakeup_vote = swrm_device_wakeup_vote;
2275 swrm->master.device_wakeup_unvote = swrm_device_wakeup_unvote;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302276 swrm->master.dev.parent = &pdev->dev;
2277 swrm->master.dev.of_node = pdev->dev.of_node;
2278 swrm->master.num_port = 0;
2279 swrm->rcmd_id = 0;
2280 swrm->wcmd_id = 0;
2281 swrm->slave_status = 0;
2282 swrm->num_rx_chs = 0;
2283 swrm->clk_ref_count = 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302284 swrm->swr_irq_wakeup_capable = 0;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302285 swrm->mclk_freq = MCLK_FREQ;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302286 swrm->dev_up = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302287 swrm->state = SWR_MSTR_UP;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302288 swrm->ipc_wakeup = false;
2289 swrm->ipc_wakeup_triggered = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302290 init_completion(&swrm->reset);
2291 init_completion(&swrm->broadcast);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302292 init_completion(&swrm->clk_off_complete);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302293 mutex_init(&swrm->mlock);
2294 mutex_init(&swrm->reslock);
2295 mutex_init(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302296 mutex_init(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302297 mutex_init(&swrm->clklock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302298 mutex_init(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302299 mutex_init(&swrm->pm_lock);
2300 swrm->wlock_holders = 0;
2301 swrm->pm_state = SWRM_PM_SLEEPABLE;
2302 init_waitqueue_head(&swrm->pm_wq);
2303 pm_qos_add_request(&swrm->pm_qos_req,
2304 PM_QOS_CPU_DMA_LATENCY,
2305 PM_QOS_DEFAULT_VALUE);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302306
2307 for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++)
2308 INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
2309
Sudheer Papothi06f43412019-07-09 03:32:54 +05302310 /* Register LPASS core hw vote */
2311 lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
2312 if (IS_ERR(lpass_core_hw_vote)) {
2313 ret = PTR_ERR(lpass_core_hw_vote);
2314 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2315 __func__, "lpass_core_hw_vote", ret);
2316 lpass_core_hw_vote = NULL;
2317 ret = 0;
2318 }
2319 swrm->lpass_core_hw_vote = lpass_core_hw_vote;
2320
2321 /* Register LPASS audio core vote */
2322 lpass_core_audio = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
2323 if (IS_ERR(lpass_core_audio)) {
2324 ret = PTR_ERR(lpass_core_audio);
2325 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2326 __func__, "lpass_core_audio", ret);
2327 lpass_core_audio = NULL;
2328 ret = 0;
2329 }
2330 swrm->lpass_core_audio = lpass_core_audio;
2331
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302332 if (swrm->reg_irq) {
2333 ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
2334 SWR_IRQ_REGISTER);
2335 if (ret) {
2336 dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
2337 __func__, ret);
2338 goto err_irq_fail;
2339 }
2340 } else {
2341 swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
2342 if (swrm->irq < 0) {
2343 dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
2344 __func__, swrm->irq);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302345 goto err_irq_fail;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302346 }
2347
2348 ret = request_threaded_irq(swrm->irq, NULL,
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302349 swr_mstr_interrupt_v2,
Ramprasad Katkam83303512018-10-11 17:34:22 +05302350 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302351 "swr_master_irq", swrm);
2352 if (ret) {
2353 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2354 __func__, ret);
2355 goto err_irq_fail;
2356 }
2357
2358 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302359 /* Make inband tx interrupts as wakeup capable for slave irq */
2360 ret = of_property_read_u32(pdev->dev.of_node,
2361 "qcom,swr-mstr-irq-wakeup-capable",
2362 &swrm->swr_irq_wakeup_capable);
2363 if (ret)
2364 dev_dbg(swrm->dev, "%s: swrm irq wakeup capable not defined\n",
2365 __func__);
2366 if (swrm->swr_irq_wakeup_capable)
2367 irq_set_irq_wake(swrm->irq, 1);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302368 ret = swr_register_master(&swrm->master);
2369 if (ret) {
2370 dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
2371 goto err_mstr_fail;
2372 }
2373
2374 /* Add devices registered with board-info as the
2375 * controller will be up now
2376 */
2377 swr_master_add_boarddevices(&swrm->master);
2378 mutex_lock(&swrm->mlock);
2379 swrm_clk_request(swrm, true);
2380 ret = swrm_master_init(swrm);
2381 if (ret < 0) {
2382 dev_err(&pdev->dev,
2383 "%s: Error in master Initialization , err %d\n",
2384 __func__, ret);
2385 mutex_unlock(&swrm->mlock);
2386 goto err_mstr_fail;
2387 }
2388 swrm->version = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
2389
2390 mutex_unlock(&swrm->mlock);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302391 INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302392
2393 if (pdev->dev.of_node)
2394 of_register_swr_devices(&swrm->master);
2395
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302396#ifdef CONFIG_DEBUG_FS
2397 swrm->debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
2398 if (!IS_ERR(swrm->debugfs_swrm_dent)) {
2399 swrm->debugfs_peek = debugfs_create_file("swrm_peek",
2400 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2401 (void *) swrm, &swrm_debug_read_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302402
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302403 swrm->debugfs_poke = debugfs_create_file("swrm_poke",
2404 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2405 (void *) swrm, &swrm_debug_write_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302406
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302407 swrm->debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
2408 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2409 (void *) swrm,
2410 &swrm_debug_dump_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302411 }
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302412#endif
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302413 ret = device_init_wakeup(swrm->dev, true);
2414 if (ret) {
2415 dev_err(swrm->dev, "Device wakeup init failed: %d\n", ret);
2416 goto err_irq_wakeup_fail;
2417 }
2418
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302419 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
2420 pm_runtime_use_autosuspend(&pdev->dev);
2421 pm_runtime_set_active(&pdev->dev);
2422 pm_runtime_enable(&pdev->dev);
2423 pm_runtime_mark_last_busy(&pdev->dev);
2424
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302425 INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn);
2426 swrm->event_notifier.notifier_call = swrm_event_notify;
2427 msm_aud_evt_register_client(&swrm->event_notifier);
2428
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302429 return 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302430err_irq_wakeup_fail:
2431 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302432err_mstr_fail:
2433 if (swrm->reg_irq)
2434 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2435 swrm, SWR_IRQ_FREE);
2436 else if (swrm->irq)
2437 free_irq(swrm->irq, swrm);
2438err_irq_fail:
2439 mutex_destroy(&swrm->mlock);
2440 mutex_destroy(&swrm->reslock);
2441 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302442 mutex_destroy(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302443 mutex_destroy(&swrm->clklock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302444 mutex_destroy(&swrm->pm_lock);
2445 pm_qos_remove_request(&swrm->pm_qos_req);
2446
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302447err_pdata_fail:
2448err_memory_fail:
2449 return ret;
2450}
2451
2452static int swrm_remove(struct platform_device *pdev)
2453{
2454 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2455
2456 if (swrm->reg_irq)
2457 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2458 swrm, SWR_IRQ_FREE);
2459 else if (swrm->irq)
2460 free_irq(swrm->irq, swrm);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302461 else if (swrm->wake_irq > 0)
2462 free_irq(swrm->wake_irq, swrm);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302463 if (swrm->swr_irq_wakeup_capable)
2464 irq_set_irq_wake(swrm->irq, 0);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302465 cancel_work_sync(&swrm->wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302466 pm_runtime_disable(&pdev->dev);
2467 pm_runtime_set_suspended(&pdev->dev);
2468 swr_unregister_master(&swrm->master);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302469 msm_aud_evt_unregister_client(&swrm->event_notifier);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302470 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302471 mutex_destroy(&swrm->mlock);
2472 mutex_destroy(&swrm->reslock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302473 mutex_destroy(&swrm->iolock);
2474 mutex_destroy(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302475 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302476 mutex_destroy(&swrm->pm_lock);
2477 pm_qos_remove_request(&swrm->pm_qos_req);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302478 devm_kfree(&pdev->dev, swrm);
2479 return 0;
2480}
2481
2482static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
2483{
2484 u32 val;
2485
2486 dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
2487 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD);
2488 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2489 val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK;
2490 swr_master_write(swrm, SWRM_MCP_CFG_ADDR, val);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302491
2492 return 0;
2493}
2494
2495#ifdef CONFIG_PM
2496static int swrm_runtime_resume(struct device *dev)
2497{
2498 struct platform_device *pdev = to_platform_device(dev);
2499 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2500 int ret = 0;
Vatsal Buchae50b5002019-09-19 14:32:20 +05302501 bool swrm_clk_req_err = false;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302502 bool hw_core_err = false;
2503 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302504 struct swr_master *mstr = &swrm->master;
2505 struct swr_device *swr_dev;
2506
2507 dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
2508 __func__, swrm->state);
2509 mutex_lock(&swrm->reslock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302510
Sudheer Papothi384addd2019-06-14 02:26:52 +05302511 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2512 dev_err(dev, "%s:lpass core hw enable failed\n",
2513 __func__);
2514 hw_core_err = true;
2515 }
2516 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2517 dev_err(dev, "%s:lpass audio hw enable failed\n",
2518 __func__);
2519 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002520 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302521
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302522 if ((swrm->state == SWR_MSTR_DOWN) ||
2523 (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) {
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302524 if (swrm->clk_stop_mode0_supp) {
2525 if (swrm->ipc_wakeup)
2526 msm_aud_evt_blocking_notifier_call_chain(
2527 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302528 }
2529
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302530 if (swrm_clk_request(swrm, true)) {
2531 /*
2532 * Set autosuspend timer to 1 for
2533 * master to enter into suspend.
2534 */
Vatsal Buchae50b5002019-09-19 14:32:20 +05302535 swrm_clk_req_err = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302536 goto exit;
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302537 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302538 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302539 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2540 ret = swr_device_up(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302541 if (ret == -ENODEV) {
2542 dev_dbg(dev,
2543 "%s slave device up not implemented\n",
2544 __func__);
2545 ret = 0;
2546 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302547 dev_err(dev,
2548 "%s: failed to wakeup swr dev %d\n",
2549 __func__, swr_dev->dev_num);
2550 swrm_clk_request(swrm, false);
2551 goto exit;
2552 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302553 }
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05302554 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2555 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2556 swrm_master_init(swrm);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302557 /* wait for hw enumeration to complete */
2558 usleep_range(100, 105);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302559 swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, 0x0,
2560 SWRS_SCP_INT_STATUS_MASK_1);
Karthikeyan Manif6821902019-05-21 17:31:24 -07002561 if (swrm->state == SWR_MSTR_SSR) {
2562 mutex_unlock(&swrm->reslock);
2563 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
2564 mutex_lock(&swrm->reslock);
2565 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302566 } else {
2567 /*wake up from clock stop*/
2568 swr_master_write(swrm, SWRM_MCP_BUS_CTRL_ADDR, 0x2);
2569 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302570 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302571 swrm->state = SWR_MSTR_UP;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302572 }
2573exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302574 if (!aud_core_err)
2575 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2576 if (!hw_core_err)
2577 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Vatsal Buchae50b5002019-09-19 14:32:20 +05302578 if (swrm_clk_req_err)
2579 pm_runtime_set_autosuspend_delay(&pdev->dev,
2580 ERR_AUTO_SUSPEND_TIMER_VAL);
2581 else
2582 pm_runtime_set_autosuspend_delay(&pdev->dev,
2583 auto_suspend_timer);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302584 mutex_unlock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302585
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302586 return ret;
2587}
2588
2589static int swrm_runtime_suspend(struct device *dev)
2590{
2591 struct platform_device *pdev = to_platform_device(dev);
2592 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2593 int ret = 0;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302594 bool hw_core_err = false;
2595 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302596 struct swr_master *mstr = &swrm->master;
2597 struct swr_device *swr_dev;
2598 int current_state = 0;
2599
2600 dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
2601 __func__, swrm->state);
2602 mutex_lock(&swrm->reslock);
2603 mutex_lock(&swrm->force_down_lock);
2604 current_state = swrm->state;
2605 mutex_unlock(&swrm->force_down_lock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302606
2607 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2608 dev_err(dev, "%s:lpass core hw enable failed\n",
2609 __func__);
2610 hw_core_err = true;
2611 }
2612 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2613 dev_err(dev, "%s:lpass audio hw enable failed\n",
2614 __func__);
2615 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002616 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302617
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302618 if ((current_state == SWR_MSTR_UP) ||
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302619 (current_state == SWR_MSTR_SSR)) {
2620
2621 if ((current_state != SWR_MSTR_SSR) &&
2622 swrm_is_port_en(&swrm->master)) {
2623 dev_dbg(dev, "%s ports are enabled\n", __func__);
2624 ret = -EBUSY;
2625 goto exit;
2626 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302627 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05302628 mutex_unlock(&swrm->reslock);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +05302629 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
Sudheer Papothi06f43412019-07-09 03:32:54 +05302630 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302631 swrm_clk_pause(swrm);
2632 swr_master_write(swrm, SWRM_COMP_CFG_ADDR, 0x00);
2633 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2634 ret = swr_device_down(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302635 if (ret == -ENODEV) {
2636 dev_dbg_ratelimited(dev,
2637 "%s slave device down not implemented\n",
2638 __func__);
2639 ret = 0;
2640 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302641 dev_err(dev,
2642 "%s: failed to shutdown swr dev %d\n",
2643 __func__, swr_dev->dev_num);
2644 goto exit;
2645 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302646 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302647 } else {
Sudheer Papothi384addd2019-06-14 02:26:52 +05302648 mutex_unlock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302649 /* clock stop sequence */
2650 swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
2651 SWRS_SCP_CONTROL);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302652 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302653 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302654 }
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07002655 ret = swrm_clk_request(swrm, false);
2656 if (ret) {
2657 dev_err(dev, "%s: swrmn clk failed\n", __func__);
2658 ret = 0;
2659 goto exit;
2660 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302661
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302662 if (swrm->clk_stop_mode0_supp) {
2663 if (swrm->wake_irq > 0) {
2664 enable_irq(swrm->wake_irq);
2665 } else if (swrm->ipc_wakeup) {
2666 msm_aud_evt_blocking_notifier_call_chain(
2667 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
2668 swrm->ipc_wakeup_triggered = false;
2669 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302670 }
2671
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302672 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302673 /* Retain SSR state until resume */
2674 if (current_state != SWR_MSTR_SSR)
2675 swrm->state = SWR_MSTR_DOWN;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302676exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302677 if (!aud_core_err)
2678 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2679 if (!hw_core_err)
2680 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302681 mutex_unlock(&swrm->reslock);
2682 return ret;
2683}
2684#endif /* CONFIG_PM */
2685
Sudheer Papothi06f43412019-07-09 03:32:54 +05302686static int swrm_device_suspend(struct device *dev)
2687{
2688 struct platform_device *pdev = to_platform_device(dev);
2689 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2690 int ret = 0;
2691
2692 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2693 if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
2694 ret = swrm_runtime_suspend(dev);
2695 if (!ret) {
2696 pm_runtime_disable(dev);
2697 pm_runtime_set_suspended(dev);
2698 pm_runtime_enable(dev);
2699 }
2700 }
2701
2702 return 0;
2703}
2704
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302705static int swrm_device_down(struct device *dev)
2706{
2707 struct platform_device *pdev = to_platform_device(dev);
2708 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302709
2710 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2711
2712 mutex_lock(&swrm->force_down_lock);
2713 swrm->state = SWR_MSTR_SSR;
2714 mutex_unlock(&swrm->force_down_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302715
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302716 swrm_device_suspend(dev);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302717 return 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302718}
2719
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302720int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm)
2721{
2722 int ret = 0;
Laxminath Kasama60239e2019-01-10 14:43:03 +05302723 int irq, dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302724
2725 if (!swrm->ipc_wakeup) {
Laxminath Kasama60239e2019-01-10 14:43:03 +05302726 irq = of_get_named_gpio(swrm->dev->of_node,
2727 "qcom,swr-wakeup-irq", 0);
2728 if (gpio_is_valid(irq)) {
2729 swrm->wake_irq = gpio_to_irq(irq);
2730 if (swrm->wake_irq < 0) {
2731 dev_err(swrm->dev,
2732 "Unable to configure irq\n");
2733 return swrm->wake_irq;
2734 }
2735 } else {
2736 dir_apps_irq = platform_get_irq_byname(swrm->pdev,
2737 "swr_wake_irq");
2738 if (dir_apps_irq < 0) {
2739 dev_err(swrm->dev,
2740 "TLMM connect gpio not found\n");
2741 return -EINVAL;
2742 }
2743 swrm->wake_irq = dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302744 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302745 ret = request_threaded_irq(swrm->wake_irq, NULL,
2746 swrm_wakeup_interrupt,
2747 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
2748 "swr_wake_irq", swrm);
2749 if (ret) {
2750 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2751 __func__, ret);
2752 return -EINVAL;
2753 }
Aditya Bavanari3517b112018-12-03 13:26:59 +05302754 irq_set_irq_wake(swrm->wake_irq, 1);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302755 }
2756 return ret;
2757}
2758
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302759static int swrm_alloc_port_mem(struct device *dev, struct swr_mstr_ctrl *swrm,
2760 u32 uc, u32 size)
2761{
2762 if (!swrm->port_param) {
2763 swrm->port_param = devm_kzalloc(dev,
2764 sizeof(swrm->port_param) * SWR_UC_MAX,
2765 GFP_KERNEL);
2766 if (!swrm->port_param)
2767 return -ENOMEM;
2768 }
2769 if (!swrm->port_param[uc]) {
2770 swrm->port_param[uc] = devm_kcalloc(dev, size,
2771 sizeof(struct port_params),
2772 GFP_KERNEL);
2773 if (!swrm->port_param[uc])
2774 return -ENOMEM;
2775 } else {
2776 dev_err_ratelimited(swrm->dev, "%s: called more than once\n",
2777 __func__);
2778 }
2779
2780 return 0;
2781}
2782
2783static int swrm_copy_port_config(struct swr_mstr_ctrl *swrm,
2784 struct swrm_port_config *port_cfg,
2785 u32 size)
2786{
2787 int idx;
2788 struct port_params *params;
2789 int uc = port_cfg->uc;
2790 int ret = 0;
2791
2792 for (idx = 0; idx < size; idx++) {
2793 params = &((struct port_params *)port_cfg->params)[idx];
2794 if (!params) {
2795 dev_err(swrm->dev, "%s: Invalid params\n", __func__);
2796 ret = -EINVAL;
2797 break;
2798 }
2799 memcpy(&swrm->port_param[uc][idx], params,
2800 sizeof(struct port_params));
2801 }
2802
2803 return ret;
2804}
2805
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302806/**
2807 * swrm_wcd_notify - parent device can notify to soundwire master through
2808 * this function
2809 * @pdev: pointer to platform device structure
2810 * @id: command id from parent to the soundwire master
2811 * @data: data from parent device to soundwire master
2812 */
2813int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
2814{
2815 struct swr_mstr_ctrl *swrm;
2816 int ret = 0;
2817 struct swr_master *mstr;
2818 struct swr_device *swr_dev;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302819 struct swrm_port_config *port_cfg;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302820
2821 if (!pdev) {
2822 pr_err("%s: pdev is NULL\n", __func__);
2823 return -EINVAL;
2824 }
2825 swrm = platform_get_drvdata(pdev);
2826 if (!swrm) {
2827 dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__);
2828 return -EINVAL;
2829 }
2830 mstr = &swrm->master;
2831
2832 switch (id) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05302833 case SWR_REQ_CLK_SWITCH:
2834 /* This will put soundwire in clock stop mode and disable the
2835 * clocks, if there is no active usecase running, so that the
2836 * next activity on soundwire will request clock from new clock
2837 * source.
2838 */
2839 mutex_lock(&swrm->mlock);
2840 if (swrm->state == SWR_MSTR_UP)
2841 swrm_device_suspend(&pdev->dev);
2842 mutex_unlock(&swrm->mlock);
2843 break;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302844 case SWR_CLK_FREQ:
2845 if (!data) {
2846 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
2847 ret = -EINVAL;
2848 } else {
2849 mutex_lock(&swrm->mlock);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302850 if (swrm->mclk_freq != *(int *)data) {
2851 dev_dbg(swrm->dev, "%s: freq change: force mstr down\n", __func__);
2852 if (swrm->state == SWR_MSTR_DOWN)
2853 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
2854 __func__, swrm->state);
2855 else
2856 swrm_device_suspend(&pdev->dev);
2857 }
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302858 swrm->mclk_freq = *(int *)data;
2859 mutex_unlock(&swrm->mlock);
2860 }
2861 break;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302862 case SWR_DEVICE_SSR_DOWN:
2863 mutex_lock(&swrm->devlock);
2864 swrm->dev_up = false;
2865 mutex_unlock(&swrm->devlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302866 mutex_lock(&swrm->reslock);
2867 swrm->state = SWR_MSTR_SSR;
2868 mutex_unlock(&swrm->reslock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302869 break;
2870 case SWR_DEVICE_SSR_UP:
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302871 /* wait for clk voting to be zero */
Ramprasad Katkam7f6462e2018-11-06 11:51:22 +05302872 reinit_completion(&swrm->clk_off_complete);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302873 if (swrm->clk_ref_count &&
2874 !wait_for_completion_timeout(&swrm->clk_off_complete,
Ramprasad Katkamc87efeb2018-12-12 19:26:19 +05302875 msecs_to_jiffies(500)))
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302876 dev_err(swrm->dev, "%s: clock voting not zero\n",
2877 __func__);
2878
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302879 mutex_lock(&swrm->devlock);
2880 swrm->dev_up = true;
2881 mutex_unlock(&swrm->devlock);
2882 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302883 case SWR_DEVICE_DOWN:
2884 dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
2885 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302886 if (swrm->state == SWR_MSTR_DOWN)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302887 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
2888 __func__, swrm->state);
2889 else
2890 swrm_device_down(&pdev->dev);
2891 mutex_unlock(&swrm->mlock);
2892 break;
2893 case SWR_DEVICE_UP:
2894 dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
Ramprasad Katkam0fed92f2018-11-08 14:22:22 +05302895 mutex_lock(&swrm->devlock);
2896 if (!swrm->dev_up) {
2897 dev_dbg(swrm->dev, "SSR not complete yet\n");
2898 mutex_unlock(&swrm->devlock);
2899 return -EBUSY;
2900 }
2901 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302902 mutex_lock(&swrm->mlock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302903 pm_runtime_mark_last_busy(&pdev->dev);
2904 pm_runtime_get_sync(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302905 mutex_lock(&swrm->reslock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302906 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2907 ret = swr_reset_device(swr_dev);
2908 if (ret) {
2909 dev_err(swrm->dev,
2910 "%s: failed to reset swr device %d\n",
2911 __func__, swr_dev->dev_num);
2912 swrm_clk_request(swrm, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302913 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302914 }
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302915 pm_runtime_mark_last_busy(&pdev->dev);
2916 pm_runtime_put_autosuspend(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302917 mutex_unlock(&swrm->reslock);
2918 mutex_unlock(&swrm->mlock);
2919 break;
2920 case SWR_SET_NUM_RX_CH:
2921 if (!data) {
2922 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
2923 ret = -EINVAL;
2924 } else {
2925 mutex_lock(&swrm->mlock);
2926 swrm->num_rx_chs = *(int *)data;
2927 if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
2928 list_for_each_entry(swr_dev, &mstr->devices,
2929 dev_list) {
2930 ret = swr_set_device_group(swr_dev,
2931 SWR_BROADCAST);
2932 if (ret)
2933 dev_err(swrm->dev,
2934 "%s: set num ch failed\n",
2935 __func__);
2936 }
2937 } else {
2938 list_for_each_entry(swr_dev, &mstr->devices,
2939 dev_list) {
2940 ret = swr_set_device_group(swr_dev,
2941 SWR_GROUP_NONE);
2942 if (ret)
2943 dev_err(swrm->dev,
2944 "%s: set num ch failed\n",
2945 __func__);
2946 }
2947 }
2948 mutex_unlock(&swrm->mlock);
2949 }
2950 break;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302951 case SWR_REGISTER_WAKE_IRQ:
2952 if (!data) {
2953 dev_err(swrm->dev, "%s: reg wake irq data is NULL\n",
2954 __func__);
2955 ret = -EINVAL;
2956 } else {
2957 mutex_lock(&swrm->mlock);
2958 swrm->ipc_wakeup = *(u32 *)data;
2959 ret = swrm_register_wake_irq(swrm);
2960 if (ret)
2961 dev_err(swrm->dev, "%s: register wake_irq failed\n",
2962 __func__);
2963 mutex_unlock(&swrm->mlock);
2964 }
2965 break;
Sudheer Papothi72ee2642019-08-08 05:15:17 +05302966 case SWR_REGISTER_WAKEUP:
2967 msm_aud_evt_blocking_notifier_call_chain(
2968 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
2969 break;
2970 case SWR_DEREGISTER_WAKEUP:
2971 msm_aud_evt_blocking_notifier_call_chain(
2972 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
2973 break;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302974 case SWR_SET_PORT_MAP:
2975 if (!data) {
2976 dev_err(swrm->dev, "%s: data is NULL for id=%d\n",
2977 __func__, id);
2978 ret = -EINVAL;
2979 } else {
2980 mutex_lock(&swrm->mlock);
2981 port_cfg = (struct swrm_port_config *)data;
2982 if (!port_cfg->size) {
2983 ret = -EINVAL;
2984 goto done;
2985 }
2986 ret = swrm_alloc_port_mem(&pdev->dev, swrm,
2987 port_cfg->uc, port_cfg->size);
2988 if (!ret)
2989 swrm_copy_port_config(swrm, port_cfg,
2990 port_cfg->size);
2991done:
2992 mutex_unlock(&swrm->mlock);
2993 }
2994 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302995 default:
2996 dev_err(swrm->dev, "%s: swr master unknown id %d\n",
2997 __func__, id);
2998 break;
2999 }
3000 return ret;
3001}
3002EXPORT_SYMBOL(swrm_wcd_notify);
3003
Ramprasad Katkam57349872018-11-11 18:34:57 +05303004/*
3005 * swrm_pm_cmpxchg:
3006 * Check old state and exchange with pm new state
3007 * if old state matches with current state
3008 *
3009 * @swrm: pointer to wcd core resource
3010 * @o: pm old state
3011 * @n: pm new state
3012 *
3013 * Returns old state
3014 */
3015static enum swrm_pm_state swrm_pm_cmpxchg(
3016 struct swr_mstr_ctrl *swrm,
3017 enum swrm_pm_state o,
3018 enum swrm_pm_state n)
3019{
3020 enum swrm_pm_state old;
3021
3022 if (!swrm)
3023 return o;
3024
3025 mutex_lock(&swrm->pm_lock);
3026 old = swrm->pm_state;
3027 if (old == o)
3028 swrm->pm_state = n;
3029 mutex_unlock(&swrm->pm_lock);
3030
3031 return old;
3032}
3033
3034static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm)
3035{
3036 enum swrm_pm_state os;
3037
3038 /*
3039 * swrm_{lock/unlock}_sleep will be called by swr irq handler
3040 * and slave wake up requests..
3041 *
3042 * If system didn't resume, we can simply return false so
3043 * IRQ handler can return without handling IRQ.
3044 */
3045 mutex_lock(&swrm->pm_lock);
3046 if (swrm->wlock_holders++ == 0) {
3047 dev_dbg(swrm->dev, "%s: holding wake lock\n", __func__);
3048 pm_qos_update_request(&swrm->pm_qos_req,
3049 msm_cpuidle_get_deep_idle_latency());
3050 pm_stay_awake(swrm->dev);
3051 }
3052 mutex_unlock(&swrm->pm_lock);
3053
3054 if (!wait_event_timeout(swrm->pm_wq,
3055 ((os = swrm_pm_cmpxchg(swrm,
3056 SWRM_PM_SLEEPABLE,
3057 SWRM_PM_AWAKE)) ==
3058 SWRM_PM_SLEEPABLE ||
3059 (os == SWRM_PM_AWAKE)),
3060 msecs_to_jiffies(
3061 SWRM_SYSTEM_RESUME_TIMEOUT_MS))) {
3062 dev_err(swrm->dev, "%s: system didn't resume within %dms, s %d, w %d\n",
3063 __func__, SWRM_SYSTEM_RESUME_TIMEOUT_MS, swrm->pm_state,
3064 swrm->wlock_holders);
3065 swrm_unlock_sleep(swrm);
3066 return false;
3067 }
3068 wake_up_all(&swrm->pm_wq);
3069 return true;
3070}
3071
3072static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm)
3073{
3074 mutex_lock(&swrm->pm_lock);
3075 if (--swrm->wlock_holders == 0) {
3076 dev_dbg(swrm->dev, "%s: releasing wake lock pm_state %d -> %d\n",
3077 __func__, swrm->pm_state, SWRM_PM_SLEEPABLE);
3078 /*
3079 * if swrm_lock_sleep failed, pm_state would be still
3080 * swrm_PM_ASLEEP, don't overwrite
3081 */
3082 if (likely(swrm->pm_state == SWRM_PM_AWAKE))
3083 swrm->pm_state = SWRM_PM_SLEEPABLE;
3084 pm_qos_update_request(&swrm->pm_qos_req,
3085 PM_QOS_DEFAULT_VALUE);
3086 pm_relax(swrm->dev);
3087 }
3088 mutex_unlock(&swrm->pm_lock);
3089 wake_up_all(&swrm->pm_wq);
3090}
3091
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303092#ifdef CONFIG_PM_SLEEP
3093static int swrm_suspend(struct device *dev)
3094{
3095 int ret = -EBUSY;
3096 struct platform_device *pdev = to_platform_device(dev);
3097 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3098
3099 dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
Ramprasad Katkam57349872018-11-11 18:34:57 +05303100
3101 mutex_lock(&swrm->pm_lock);
3102
3103 if (swrm->pm_state == SWRM_PM_SLEEPABLE) {
3104 dev_dbg(swrm->dev, "%s: suspending system, state %d, wlock %d\n",
3105 __func__, swrm->pm_state,
3106 swrm->wlock_holders);
3107 swrm->pm_state = SWRM_PM_ASLEEP;
3108 } else if (swrm->pm_state == SWRM_PM_AWAKE) {
3109 /*
3110 * unlock to wait for pm_state == SWRM_PM_SLEEPABLE
3111 * then set to SWRM_PM_ASLEEP
3112 */
3113 dev_dbg(swrm->dev, "%s: waiting to suspend system, state %d, wlock %d\n",
3114 __func__, swrm->pm_state,
3115 swrm->wlock_holders);
3116 mutex_unlock(&swrm->pm_lock);
3117 if (!(wait_event_timeout(swrm->pm_wq, swrm_pm_cmpxchg(
3118 swrm, SWRM_PM_SLEEPABLE,
3119 SWRM_PM_ASLEEP) ==
3120 SWRM_PM_SLEEPABLE,
3121 msecs_to_jiffies(
3122 SWRM_SYS_SUSPEND_WAIT)))) {
3123 dev_dbg(swrm->dev, "%s: suspend failed state %d, wlock %d\n",
3124 __func__, swrm->pm_state,
3125 swrm->wlock_holders);
3126 return -EBUSY;
3127 } else {
3128 dev_dbg(swrm->dev,
3129 "%s: done, state %d, wlock %d\n",
3130 __func__, swrm->pm_state,
3131 swrm->wlock_holders);
3132 }
3133 mutex_lock(&swrm->pm_lock);
3134 } else if (swrm->pm_state == SWRM_PM_ASLEEP) {
3135 dev_dbg(swrm->dev, "%s: system is already suspended, state %d, wlock %d\n",
3136 __func__, swrm->pm_state,
3137 swrm->wlock_holders);
3138 }
3139
3140 mutex_unlock(&swrm->pm_lock);
3141
3142 if ((!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev))) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303143 ret = swrm_runtime_suspend(dev);
3144 if (!ret) {
3145 /*
3146 * Synchronize runtime-pm and system-pm states:
3147 * At this point, we are already suspended. If
3148 * runtime-pm still thinks its active, then
3149 * make sure its status is in sync with HW
3150 * status. The three below calls let the
3151 * runtime-pm know that we are suspended
3152 * already without re-invoking the suspend
3153 * callback
3154 */
3155 pm_runtime_disable(dev);
3156 pm_runtime_set_suspended(dev);
3157 pm_runtime_enable(dev);
3158 }
3159 }
3160 if (ret == -EBUSY) {
3161 /*
3162 * There is a possibility that some audio stream is active
3163 * during suspend. We dont want to return suspend failure in
3164 * that case so that display and relevant components can still
3165 * go to suspend.
3166 * If there is some other error, then it should be passed-on
3167 * to system level suspend
3168 */
3169 ret = 0;
3170 }
3171 return ret;
3172}
3173
3174static int swrm_resume(struct device *dev)
3175{
3176 int ret = 0;
3177 struct platform_device *pdev = to_platform_device(dev);
3178 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3179
3180 dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
3181 if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
3182 ret = swrm_runtime_resume(dev);
3183 if (!ret) {
3184 pm_runtime_mark_last_busy(dev);
3185 pm_request_autosuspend(dev);
3186 }
3187 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05303188 mutex_lock(&swrm->pm_lock);
3189 if (swrm->pm_state == SWRM_PM_ASLEEP) {
3190 dev_dbg(swrm->dev,
3191 "%s: resuming system, state %d, wlock %d\n",
3192 __func__, swrm->pm_state,
3193 swrm->wlock_holders);
3194 swrm->pm_state = SWRM_PM_SLEEPABLE;
3195 } else {
3196 dev_dbg(swrm->dev, "%s: system is already awake, state %d wlock %d\n",
3197 __func__, swrm->pm_state,
3198 swrm->wlock_holders);
3199 }
3200 mutex_unlock(&swrm->pm_lock);
3201 wake_up_all(&swrm->pm_wq);
3202
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303203 return ret;
3204}
3205#endif /* CONFIG_PM_SLEEP */
3206
3207static const struct dev_pm_ops swrm_dev_pm_ops = {
3208 SET_SYSTEM_SLEEP_PM_OPS(
3209 swrm_suspend,
3210 swrm_resume
3211 )
3212 SET_RUNTIME_PM_OPS(
3213 swrm_runtime_suspend,
3214 swrm_runtime_resume,
3215 NULL
3216 )
3217};
3218
3219static const struct of_device_id swrm_dt_match[] = {
3220 {
3221 .compatible = "qcom,swr-mstr",
3222 },
3223 {}
3224};
3225
3226static struct platform_driver swr_mstr_driver = {
3227 .probe = swrm_probe,
3228 .remove = swrm_remove,
3229 .driver = {
3230 .name = SWR_WCD_NAME,
3231 .owner = THIS_MODULE,
3232 .pm = &swrm_dev_pm_ops,
3233 .of_match_table = swrm_dt_match,
Xiaojun Sang53cd13a2018-06-29 15:14:37 +08003234 .suppress_bind_attrs = true,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303235 },
3236};
3237
3238static int __init swrm_init(void)
3239{
3240 return platform_driver_register(&swr_mstr_driver);
3241}
3242module_init(swrm_init);
3243
3244static void __exit swrm_exit(void)
3245{
3246 platform_driver_unregister(&swr_mstr_driver);
3247}
3248module_exit(swrm_exit);
3249
3250MODULE_LICENSE("GPL v2");
3251MODULE_DESCRIPTION("SoundWire Master Controller");
3252MODULE_ALIAS("platform:swr-mstr");