blob: e1ec36f0597a86889423fc690724dba3c5cd8351 [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
Ramprasad Katkam83303512018-10-11 17:34:22 +053043#define SWRM_INTERRUPT_STATUS_MASK 0x1FDFD
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053044/* pm runtime auto suspend timer in msecs */
45static int auto_suspend_timer = SWR_AUTO_SUSPEND_DELAY * 1000;
46module_param(auto_suspend_timer, int, 0664);
47MODULE_PARM_DESC(auto_suspend_timer, "timer for auto suspend");
48
49enum {
50 SWR_NOT_PRESENT, /* Device is detached/not present on the bus */
51 SWR_ATTACHED_OK, /* Device is attached */
52 SWR_ALERT, /* Device alters master for any interrupts */
53 SWR_RESERVED, /* Reserved */
54};
55
56enum {
57 MASTER_ID_WSA = 1,
58 MASTER_ID_RX,
59 MASTER_ID_TX
60};
Ramprasad Katkamcab8d722018-09-28 15:54:06 +053061
62enum {
63 ENABLE_PENDING,
64 DISABLE_PENDING
65};
Sudheer Papothi384addd2019-06-14 02:26:52 +053066
67enum {
68 LPASS_HW_CORE,
69 LPASS_AUDIO_CORE,
70};
71
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053072#define TRUE 1
73#define FALSE 0
74
Ramprasad Katkam1f221262018-08-23 15:01:22 +053075#define SWRM_MAX_PORT_REG 120
Ramprasad Katkam83303512018-10-11 17:34:22 +053076#define SWRM_MAX_INIT_REG 11
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053077
Laxminath Kasamfbcaf322018-07-18 00:38:14 +053078#define MAX_FIFO_RD_FAIL_RETRY 3
79
Ramprasad Katkam57349872018-11-11 18:34:57 +053080static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm);
81static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm);
Sudheer Papothi96c842a2019-08-29 12:11:21 +053082static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr);
83static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053084
85static bool swrm_is_msm_variant(int val)
86{
87 return (val == SWRM_VERSION_1_3);
88}
89
Sudheer Papothi96c842a2019-08-29 12:11:21 +053090#ifdef CONFIG_DEBUG_FS
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053091static int swrm_debug_open(struct inode *inode, struct file *file)
92{
93 file->private_data = inode->i_private;
94 return 0;
95}
96
97static int get_parameters(char *buf, u32 *param1, int num_of_par)
98{
99 char *token;
100 int base, cnt;
101
102 token = strsep(&buf, " ");
103 for (cnt = 0; cnt < num_of_par; cnt++) {
104 if (token) {
105 if ((token[1] == 'x') || (token[1] == 'X'))
106 base = 16;
107 else
108 base = 10;
109
110 if (kstrtou32(token, base, &param1[cnt]) != 0)
111 return -EINVAL;
112
113 token = strsep(&buf, " ");
114 } else
115 return -EINVAL;
116 }
117 return 0;
118}
119
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530120static ssize_t swrm_reg_show(struct swr_mstr_ctrl *swrm, char __user *ubuf,
121 size_t count, loff_t *ppos)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530122{
123 int i, reg_val, len;
124 ssize_t total = 0;
125 char tmp_buf[SWR_MSTR_MAX_BUF_LEN];
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530126 int rem = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530127
128 if (!ubuf || !ppos)
129 return 0;
130
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530131 i = ((int) *ppos + SWR_MSTR_START_REG_ADDR);
132 rem = i%4;
133
134 if (rem)
135 i = (i - rem);
136
137 for (; i <= SWR_MSTR_MAX_REG_ADDR; i += 4) {
138 usleep_range(100, 150);
139 reg_val = swr_master_read(swrm, i);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530140 len = snprintf(tmp_buf, 25, "0x%.3x: 0x%.2x\n", i, reg_val);
Aditya Bavanari9f599b42019-08-27 22:18:41 +0530141 if (len < 0) {
142 pr_err("%s: fail to fill the buffer\n", __func__);
143 total = -EFAULT;
144 goto copy_err;
145 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530146 if ((total + len) >= count - 1)
147 break;
148 if (copy_to_user((ubuf + total), tmp_buf, len)) {
149 pr_err("%s: fail to copy reg dump\n", __func__);
150 total = -EFAULT;
151 goto copy_err;
152 }
153 *ppos += len;
154 total += len;
155 }
156
157copy_err:
158 return total;
159}
160
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530161static ssize_t swrm_debug_reg_dump(struct file *file, char __user *ubuf,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530162 size_t count, loff_t *ppos)
163{
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530164 struct swr_mstr_ctrl *swrm;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530165
166 if (!count || !file || !ppos || !ubuf)
167 return -EINVAL;
168
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530169 swrm = file->private_data;
170 if (!swrm)
171 return -EINVAL;
172
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530173 if (*ppos < 0)
174 return -EINVAL;
175
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530176 return swrm_reg_show(swrm, ubuf, count, ppos);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530177}
178
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530179static ssize_t swrm_debug_read(struct file *file, char __user *ubuf,
180 size_t count, loff_t *ppos)
181{
182 char lbuf[SWR_MSTR_RD_BUF_LEN];
183 struct swr_mstr_ctrl *swrm = NULL;
184
185 if (!count || !file || !ppos || !ubuf)
186 return -EINVAL;
187
188 swrm = file->private_data;
189 if (!swrm)
190 return -EINVAL;
191
192 if (*ppos < 0)
193 return -EINVAL;
194
195 snprintf(lbuf, sizeof(lbuf), "0x%x\n", swrm->read_data);
196
197 return simple_read_from_buffer(ubuf, count, ppos, lbuf,
198 strnlen(lbuf, 7));
199}
200
201static ssize_t swrm_debug_peek_write(struct file *file, const char __user *ubuf,
202 size_t count, loff_t *ppos)
203{
204 char lbuf[SWR_MSTR_RD_BUF_LEN];
205 int rc;
206 u32 param[5];
207 struct swr_mstr_ctrl *swrm = NULL;
208
209 if (!count || !file || !ppos || !ubuf)
210 return -EINVAL;
211
212 swrm = file->private_data;
213 if (!swrm)
214 return -EINVAL;
215
216 if (*ppos < 0)
217 return -EINVAL;
218
219 if (count > sizeof(lbuf) - 1)
220 return -EINVAL;
221
222 rc = copy_from_user(lbuf, ubuf, count);
223 if (rc)
224 return -EFAULT;
225
226 lbuf[count] = '\0';
227 rc = get_parameters(lbuf, param, 1);
228 if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) && (rc == 0))
229 swrm->read_data = swr_master_read(swrm, param[0]);
230 else
231 rc = -EINVAL;
232
233 if (rc == 0)
234 rc = count;
235 else
236 dev_err(swrm->dev, "%s: rc = %d\n", __func__, rc);
237
238 return rc;
239}
240
241static ssize_t swrm_debug_write(struct file *file,
242 const char __user *ubuf, size_t count, loff_t *ppos)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530243{
244 char lbuf[SWR_MSTR_WR_BUF_LEN];
245 int rc;
246 u32 param[5];
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530247 struct swr_mstr_ctrl *swrm;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530248
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530249 if (!file || !ppos || !ubuf)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530250 return -EINVAL;
251
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530252 swrm = file->private_data;
253 if (!swrm)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530254 return -EINVAL;
255
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530256 if (count > sizeof(lbuf) - 1)
257 return -EINVAL;
258
259 rc = copy_from_user(lbuf, ubuf, count);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530260 if (rc)
261 return -EFAULT;
262
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530263 lbuf[count] = '\0';
264 rc = get_parameters(lbuf, param, 2);
265 if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) &&
266 (param[1] <= 0xFFFFFFFF) &&
267 (rc == 0))
268 swr_master_write(swrm, param[0], param[1]);
269 else
270 rc = -EINVAL;
271
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530272 if (rc == 0)
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530273 rc = count;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530274 else
275 pr_err("%s: rc = %d\n", __func__, rc);
276
277 return rc;
278}
279
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530280static const struct file_operations swrm_debug_read_ops = {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530281 .open = swrm_debug_open,
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530282 .write = swrm_debug_peek_write,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530283 .read = swrm_debug_read,
284};
285
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530286static const struct file_operations swrm_debug_write_ops = {
287 .open = swrm_debug_open,
288 .write = swrm_debug_write,
289};
290
291static const struct file_operations swrm_debug_dump_ops = {
292 .open = swrm_debug_open,
293 .read = swrm_debug_reg_dump,
294};
295#endif
296
Sudheer Papothi0016db12019-06-11 04:42:38 +0530297static void swrm_reg_dump(struct swr_mstr_ctrl *swrm,
298 u32 *reg, u32 *val, int len, const char* func)
299{
300 int i = 0;
301
302 for (i = 0; i < len; i++)
303 dev_dbg(swrm->dev, "%s: reg = 0x%x val = 0x%x\n",
304 func, reg[i], val[i]);
305}
306
Sudheer Papothi384addd2019-06-14 02:26:52 +0530307static int swrm_request_hw_vote(struct swr_mstr_ctrl *swrm,
308 int core_type, bool enable)
309{
310 int ret = 0;
311
312 if (core_type == LPASS_HW_CORE) {
313 if (swrm->lpass_core_hw_vote) {
314 if (enable) {
315 ret =
316 clk_prepare_enable(swrm->lpass_core_hw_vote);
317 if (ret < 0)
318 dev_err(swrm->dev,
319 "%s:lpass core hw enable failed\n",
320 __func__);
321 } else
322 clk_disable_unprepare(swrm->lpass_core_hw_vote);
323 }
324 }
325 if (core_type == LPASS_AUDIO_CORE) {
326 if (swrm->lpass_core_audio) {
327 if (enable) {
328 ret =
329 clk_prepare_enable(swrm->lpass_core_audio);
330 if (ret < 0)
331 dev_err(swrm->dev,
332 "%s:lpass audio hw enable failed\n",
333 __func__);
334 } else
335 clk_disable_unprepare(swrm->lpass_core_audio);
336 }
337 }
338
339 return ret;
340}
341
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530342static int swrm_clk_request(struct swr_mstr_ctrl *swrm, bool enable)
343{
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530344 int ret = 0;
345
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530346 if (!swrm->clk || !swrm->handle)
347 return -EINVAL;
348
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530349 mutex_lock(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530350 if (enable) {
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530351 if (!swrm->dev_up) {
352 ret = -ENODEV;
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530353 goto exit;
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530354 }
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -0700355 if (swrm->core_vote) {
356 ret = swrm->core_vote(swrm->handle, true);
357 if (ret) {
358 dev_err_ratelimited(swrm->dev,
359 "%s: clock enable req failed",
360 __func__);
361 goto exit;
362 }
363 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530364 swrm->clk_ref_count++;
365 if (swrm->clk_ref_count == 1) {
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530366 ret = swrm->clk(swrm->handle, true);
367 if (ret) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +0530368 dev_err_ratelimited(swrm->dev,
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530369 "%s: clock enable req failed",
370 __func__);
371 --swrm->clk_ref_count;
372 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530373 }
374 } else if (--swrm->clk_ref_count == 0) {
375 swrm->clk(swrm->handle, false);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530376 complete(&swrm->clk_off_complete);
377 }
378 if (swrm->clk_ref_count < 0) {
Meng Wang8c60bb52019-06-19 15:49:06 +0800379 dev_err(swrm->dev, "%s: swrm clk count mismatch\n", __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530380 swrm->clk_ref_count = 0;
381 }
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530382
383exit:
384 mutex_unlock(&swrm->clklock);
385 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530386}
387
388static int swrm_ahb_write(struct swr_mstr_ctrl *swrm,
389 u16 reg, u32 *value)
390{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530391 u32 temp = (u32)(*value);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530392 int ret = 0;
393
394 mutex_lock(&swrm->devlock);
395 if (!swrm->dev_up)
396 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530397
398 ret = swrm_clk_request(swrm, TRUE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530399 if (ret) {
400 dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
401 __func__);
402 goto err;
403 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530404 iowrite32(temp, swrm->swrm_dig_base + reg);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530405 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530406err:
407 mutex_unlock(&swrm->devlock);
408 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530409}
410
411static int swrm_ahb_read(struct swr_mstr_ctrl *swrm,
412 u16 reg, u32 *value)
413{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530414 u32 temp = 0;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530415 int ret = 0;
416
417 mutex_lock(&swrm->devlock);
418 if (!swrm->dev_up)
419 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530420
421 ret = swrm_clk_request(swrm, TRUE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530422 if (ret) {
423 dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
424 __func__);
425 goto err;
426 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530427 temp = ioread32(swrm->swrm_dig_base + reg);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530428 *value = temp;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530429 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530430err:
431 mutex_unlock(&swrm->devlock);
432 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530433}
434
435static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr)
436{
437 u32 val = 0;
438
439 if (swrm->read)
440 val = swrm->read(swrm->handle, reg_addr);
441 else
442 swrm_ahb_read(swrm, reg_addr, &val);
443 return val;
444}
445
446static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val)
447{
448 if (swrm->write)
449 swrm->write(swrm->handle, reg_addr, val);
450 else
451 swrm_ahb_write(swrm, reg_addr, &val);
452}
453
454static int swr_master_bulk_write(struct swr_mstr_ctrl *swrm, u32 *reg_addr,
455 u32 *val, unsigned int length)
456{
457 int i = 0;
458
459 if (swrm->bulk_write)
460 swrm->bulk_write(swrm->handle, reg_addr, val, length);
461 else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530462 mutex_lock(&swrm->iolock);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530463 for (i = 0; i < length; i++) {
464 /* wait for FIFO WR command to complete to avoid overflow */
Karthikeyan Mani5d52dd82019-08-15 16:58:08 -0700465 /*
466 * Reduce sleep from 100us to 10us to meet KPIs
467 * This still meets the hardware spec
468 */
469 usleep_range(10, 12);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530470 swr_master_write(swrm, reg_addr[i], val[i]);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530471 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530472 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530473 }
474 return 0;
475}
476
477static bool swrm_is_port_en(struct swr_master *mstr)
478{
479 return !!(mstr->num_port);
480}
481
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530482static void copy_port_tables(struct swr_mstr_ctrl *swrm,
483 struct port_params *params)
484{
485 u8 i;
486 struct port_params *config = params;
487
488 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
489 /* wsa uses single frame structure for all configurations */
490 if (!swrm->mport_cfg[i].port_en)
491 continue;
492 swrm->mport_cfg[i].sinterval = config[i].si;
493 swrm->mport_cfg[i].offset1 = config[i].off1;
494 swrm->mport_cfg[i].offset2 = config[i].off2;
495 swrm->mport_cfg[i].hstart = config[i].hstart;
496 swrm->mport_cfg[i].hstop = config[i].hstop;
497 swrm->mport_cfg[i].blk_pack_mode = config[i].bp_mode;
498 swrm->mport_cfg[i].blk_grp_count = config[i].bgp_ctrl;
499 swrm->mport_cfg[i].word_length = config[i].wd_len;
500 swrm->mport_cfg[i].lane_ctrl = config[i].lane_ctrl;
501 }
502}
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530503static int swrm_get_port_config(struct swr_mstr_ctrl *swrm)
504{
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530505 struct port_params *params;
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530506 u32 usecase = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530507
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530508 /* TODO - Send usecase information to avoid checking for master_id */
509 if (swrm->mport_cfg[SWRM_DSD_PARAMS_PORT].port_en &&
510 (swrm->master_id == MASTER_ID_RX))
511 usecase = 1;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530512
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530513 params = swrm->port_param[usecase];
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530514 copy_port_tables(swrm, params);
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530515
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530516 return 0;
517}
518
519static int swrm_get_master_port(struct swr_mstr_ctrl *swrm, u8 *mstr_port_id,
520 u8 *mstr_ch_mask, u8 mstr_prt_type,
521 u8 slv_port_id)
522{
523 int i, j;
524 *mstr_port_id = 0;
525
526 for (i = 1; i <= swrm->num_ports; i++) {
527 for (j = 0; j < SWR_MAX_CH_PER_PORT; j++) {
528 if (swrm->port_mapping[i][j].port_type == mstr_prt_type)
529 goto found;
530 }
531 }
532found:
533 if (i > swrm->num_ports || j == SWR_MAX_CH_PER_PORT) {
534 dev_err(swrm->dev, "%s: port type not supported by master\n",
535 __func__);
536 return -EINVAL;
537 }
538 /* id 0 corresponds to master port 1 */
539 *mstr_port_id = i - 1;
540 *mstr_ch_mask = swrm->port_mapping[i][j].ch_mask;
541
542 return 0;
543
544}
545
546static u32 swrm_get_packed_reg_val(u8 *cmd_id, u8 cmd_data,
547 u8 dev_addr, u16 reg_addr)
548{
549 u32 val;
550 u8 id = *cmd_id;
551
552 if (id != SWR_BROADCAST_CMD_ID) {
553 if (id < 14)
554 id += 1;
555 else
556 id = 0;
557 *cmd_id = id;
558 }
559 val = SWR_REG_VAL_PACK(cmd_data, dev_addr, id, reg_addr);
560
561 return val;
562}
563
564static int swrm_cmd_fifo_rd_cmd(struct swr_mstr_ctrl *swrm, int *cmd_data,
565 u8 dev_addr, u8 cmd_id, u16 reg_addr,
566 u32 len)
567{
568 u32 val;
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530569 u32 retry_attempt = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530570
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530571 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530572 val = swrm_get_packed_reg_val(&swrm->rcmd_id, len, dev_addr, reg_addr);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530573 if (swrm->read) {
574 /* skip delay if read is handled in platform driver */
575 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
576 } else {
577 /* wait for FIFO RD to complete to avoid overflow */
578 usleep_range(100, 105);
579 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
580 /* wait for FIFO RD CMD complete to avoid overflow */
581 usleep_range(250, 255);
582 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530583retry_read:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530584 *cmd_data = swr_master_read(swrm, SWRM_CMD_FIFO_RD_FIFO_ADDR);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530585 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, rcmd_id: 0x%x, \
586 dev_num: 0x%x, cmd_data: 0x%x\n", __func__, reg_addr,
587 cmd_id, swrm->rcmd_id, dev_addr, *cmd_data);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530588 if ((((*cmd_data) & 0xF00) >> 8) != swrm->rcmd_id) {
589 if (retry_attempt < MAX_FIFO_RD_FAIL_RETRY) {
590 /* wait 500 us before retry on fifo read failure */
591 usleep_range(500, 505);
592 retry_attempt++;
593 goto retry_read;
594 } else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530595 dev_err_ratelimited(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, \
596 rcmd_id: 0x%x, dev_num: 0x%x, cmd_data: 0x%x\n",
597 __func__, reg_addr, cmd_id, swrm->rcmd_id,
598 dev_addr, *cmd_data);
599
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530600 dev_err_ratelimited(swrm->dev,
601 "%s: failed to read fifo\n", __func__);
602 }
603 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530604 mutex_unlock(&swrm->iolock);
605
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530606 return 0;
607}
608
609static int swrm_cmd_fifo_wr_cmd(struct swr_mstr_ctrl *swrm, u8 cmd_data,
610 u8 dev_addr, u8 cmd_id, u16 reg_addr)
611{
612 u32 val;
613 int ret = 0;
614
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530615 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530616 if (!cmd_id)
617 val = swrm_get_packed_reg_val(&swrm->wcmd_id, cmd_data,
618 dev_addr, reg_addr);
619 else
620 val = swrm_get_packed_reg_val(&cmd_id, cmd_data,
621 dev_addr, reg_addr);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530622 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x,wcmd_id: 0x%x, \
623 dev_num: 0x%x, cmd_data: 0x%x\n", __func__,
624 reg_addr, cmd_id, swrm->wcmd_id,dev_addr, cmd_data);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +0530625 swr_master_write(swrm, SWRM_CMD_FIFO_WR_CMD, val);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530626 /*
627 * wait for FIFO WR command to complete to avoid overflow
628 * skip delay if write is handled in platform driver.
629 */
630 if(!swrm->write)
Karthikeyan Mani5d52dd82019-08-15 16:58:08 -0700631 usleep_range(150, 155);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530632 if (cmd_id == 0xF) {
633 /*
634 * sleep for 10ms for MSM soundwire variant to allow broadcast
635 * command to complete.
636 */
637 if (swrm_is_msm_variant(swrm->version))
638 usleep_range(10000, 10100);
639 else
640 wait_for_completion_timeout(&swrm->broadcast,
641 (2 * HZ/10));
642 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530643 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530644 return ret;
645}
646
647static int swrm_read(struct swr_master *master, u8 dev_num, u16 reg_addr,
648 void *buf, u32 len)
649{
650 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
651 int ret = 0;
652 int val;
653 u8 *reg_val = (u8 *)buf;
654
655 if (!swrm) {
656 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
657 return -EINVAL;
658 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530659 if (!dev_num) {
660 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
661 return -EINVAL;
662 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530663 mutex_lock(&swrm->devlock);
664 if (!swrm->dev_up) {
665 mutex_unlock(&swrm->devlock);
666 return 0;
667 }
668 mutex_unlock(&swrm->devlock);
669
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530670 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530671 ret = swrm_cmd_fifo_rd_cmd(swrm, &val, dev_num, 0, reg_addr, len);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530672
673 if (!ret)
674 *reg_val = (u8)val;
675
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530676 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530677 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530678 return ret;
679}
680
681static int swrm_write(struct swr_master *master, u8 dev_num, u16 reg_addr,
682 const void *buf)
683{
684 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
685 int ret = 0;
686 u8 reg_val = *(u8 *)buf;
687
688 if (!swrm) {
689 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
690 return -EINVAL;
691 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530692 if (!dev_num) {
693 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
694 return -EINVAL;
695 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530696 mutex_lock(&swrm->devlock);
697 if (!swrm->dev_up) {
698 mutex_unlock(&swrm->devlock);
699 return 0;
700 }
701 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530702
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530703 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530704 ret = swrm_cmd_fifo_wr_cmd(swrm, reg_val, dev_num, 0, reg_addr);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530705
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530706 pm_runtime_put_autosuspend(swrm->dev);
707 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530708 return ret;
709}
710
711static int swrm_bulk_write(struct swr_master *master, u8 dev_num, void *reg,
712 const void *buf, size_t len)
713{
714 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
715 int ret = 0;
716 int i;
717 u32 *val;
718 u32 *swr_fifo_reg;
719
720 if (!swrm || !swrm->handle) {
721 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
722 return -EINVAL;
723 }
724 if (len <= 0)
725 return -EINVAL;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530726 mutex_lock(&swrm->devlock);
727 if (!swrm->dev_up) {
728 mutex_unlock(&swrm->devlock);
729 return 0;
730 }
731 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530732
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530733 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530734 if (dev_num) {
735 swr_fifo_reg = kcalloc(len, sizeof(u32), GFP_KERNEL);
736 if (!swr_fifo_reg) {
737 ret = -ENOMEM;
738 goto err;
739 }
740 val = kcalloc(len, sizeof(u32), GFP_KERNEL);
741 if (!val) {
742 ret = -ENOMEM;
743 goto mem_fail;
744 }
745
746 for (i = 0; i < len; i++) {
747 val[i] = swrm_get_packed_reg_val(&swrm->wcmd_id,
748 ((u8 *)buf)[i],
749 dev_num,
750 ((u16 *)reg)[i]);
751 swr_fifo_reg[i] = SWRM_CMD_FIFO_WR_CMD;
752 }
753 ret = swr_master_bulk_write(swrm, swr_fifo_reg, val, len);
754 if (ret) {
755 dev_err(&master->dev, "%s: bulk write failed\n",
756 __func__);
757 ret = -EINVAL;
758 }
759 } else {
760 dev_err(&master->dev,
761 "%s: No support of Bulk write for master regs\n",
762 __func__);
763 ret = -EINVAL;
764 goto err;
765 }
766 kfree(val);
767mem_fail:
768 kfree(swr_fifo_reg);
769err:
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530770 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530771 pm_runtime_mark_last_busy(swrm->dev);
772 return ret;
773}
774
775static u8 get_inactive_bank_num(struct swr_mstr_ctrl *swrm)
776{
777 return (swr_master_read(swrm, SWRM_MCP_STATUS) &
778 SWRM_MCP_STATUS_BANK_NUM_MASK) ? 0 : 1;
779}
780
781static void enable_bank_switch(struct swr_mstr_ctrl *swrm, u8 bank,
782 u8 row, u8 col)
783{
784 swrm_cmd_fifo_wr_cmd(swrm, ((row << 3) | col), 0xF, 0xF,
785 SWRS_SCP_FRAME_CTRL_BANK(bank));
786}
787
788static struct swr_port_info *swrm_get_port_req(struct swrm_mports *mport,
789 u8 slv_port, u8 dev_num)
790{
791 struct swr_port_info *port_req = NULL;
792
793 list_for_each_entry(port_req, &mport->port_req_list, list) {
794 /* Store dev_id instead of dev_num if enumeration is changed run_time */
795 if ((port_req->slave_port_id == slv_port)
796 && (port_req->dev_num == dev_num))
797 return port_req;
798 }
799 return NULL;
800}
801
802static bool swrm_remove_from_group(struct swr_master *master)
803{
804 struct swr_device *swr_dev;
805 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
806 bool is_removed = false;
807
808 if (!swrm)
809 goto end;
810
811 mutex_lock(&swrm->mlock);
812 if ((swrm->num_rx_chs > 1) &&
813 (swrm->num_rx_chs == swrm->num_cfg_devs)) {
814 list_for_each_entry(swr_dev, &master->devices,
815 dev_list) {
816 swr_dev->group_id = SWR_GROUP_NONE;
817 master->gr_sid = 0;
818 }
819 is_removed = true;
820 }
821 mutex_unlock(&swrm->mlock);
822
823end:
824 return is_removed;
825}
826
827static void swrm_disable_ports(struct swr_master *master,
828 u8 bank)
829{
830 u32 value;
831 struct swr_port_info *port_req;
832 int i;
833 struct swrm_mports *mport;
834 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
835
836 if (!swrm) {
837 pr_err("%s: swrm is null\n", __func__);
838 return;
839 }
840
841 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
842 master->num_port);
843
844
845 for (i = 0; i < SWR_MSTR_PORT_LEN ; i++) {
846
847 mport = &(swrm->mport_cfg[i]);
848 if (!mport->port_en)
849 continue;
850
851 list_for_each_entry(port_req, &mport->port_req_list, list) {
852 /* skip ports with no change req's*/
853 if (port_req->req_ch == port_req->ch_en)
854 continue;
855
856 swrm_cmd_fifo_wr_cmd(swrm, port_req->req_ch,
857 port_req->dev_num, 0x00,
858 SWRS_DP_CHANNEL_ENABLE_BANK(port_req->slave_port_id,
859 bank));
860 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x\n",
861 __func__, i,
862 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)));
863 }
864 value = ((mport->req_ch)
865 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
866 value |= ((mport->offset2)
867 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
868 value |= ((mport->offset1)
869 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
870 value |= mport->sinterval;
871
872 swr_master_write(swrm,
873 SWRM_DP_PORT_CTRL_BANK(i+1, bank),
874 value);
875 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
876 __func__, i,
877 (SWRM_DP_PORT_CTRL_BANK(i+1, bank)), value);
878 }
879}
880
881static void swrm_cleanup_disabled_port_reqs(struct swr_master *master)
882{
883 struct swr_port_info *port_req, *next;
884 int i;
885 struct swrm_mports *mport;
886 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
887
888 if (!swrm) {
889 pr_err("%s: swrm is null\n", __func__);
890 return;
891 }
892 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
893 master->num_port);
894
895 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
896 mport = &(swrm->mport_cfg[i]);
897 list_for_each_entry_safe(port_req, next,
898 &mport->port_req_list, list) {
899 /* skip ports without new ch req */
900 if (port_req->ch_en == port_req->req_ch)
901 continue;
902
903 /* remove new ch req's*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +0530904 port_req->ch_en = port_req->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530905
906 /* If no streams enabled on port, remove the port req */
907 if (port_req->ch_en == 0) {
908 list_del(&port_req->list);
909 kfree(port_req);
910 }
911 }
912 /* remove new ch req's on mport*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +0530913 mport->ch_en = mport->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530914
915 if (!(mport->ch_en)) {
916 mport->port_en = false;
917 master->port_en_mask &= ~i;
918 }
919 }
920}
921static void swrm_copy_data_port_config(struct swr_master *master, u8 bank)
922{
923 u32 value, slv_id;
924 struct swr_port_info *port_req;
925 int i;
926 struct swrm_mports *mport;
927 u32 reg[SWRM_MAX_PORT_REG];
928 u32 val[SWRM_MAX_PORT_REG];
929 int len = 0;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530930 u8 hparams;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530931 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
932
933 if (!swrm) {
934 pr_err("%s: swrm is null\n", __func__);
935 return;
936 }
937
938 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
939 master->num_port);
940
941 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
942 mport = &(swrm->mport_cfg[i]);
943 if (!mport->port_en)
944 continue;
945
946 list_for_each_entry(port_req, &mport->port_req_list, list) {
947 slv_id = port_req->slave_port_id;
948 reg[len] = SWRM_CMD_FIFO_WR_CMD;
949 val[len++] = SWR_REG_VAL_PACK(port_req->req_ch,
950 port_req->dev_num, 0x00,
951 SWRS_DP_CHANNEL_ENABLE_BANK(slv_id,
952 bank));
953
954 reg[len] = SWRM_CMD_FIFO_WR_CMD;
955 val[len++] = SWR_REG_VAL_PACK(mport->sinterval,
956 port_req->dev_num, 0x00,
957 SWRS_DP_SAMPLE_CONTROL_1_BANK(slv_id,
958 bank));
959
960 reg[len] = SWRM_CMD_FIFO_WR_CMD;
961 val[len++] = SWR_REG_VAL_PACK(mport->offset1,
962 port_req->dev_num, 0x00,
963 SWRS_DP_OFFSET_CONTROL_1_BANK(slv_id,
964 bank));
965
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530966 if (mport->offset2 != SWR_INVALID_PARAM) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530967 reg[len] = SWRM_CMD_FIFO_WR_CMD;
968 val[len++] = SWR_REG_VAL_PACK(mport->offset2,
969 port_req->dev_num, 0x00,
970 SWRS_DP_OFFSET_CONTROL_2_BANK(
971 slv_id, bank));
972 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530973 if (mport->hstart != SWR_INVALID_PARAM
974 && mport->hstop != SWR_INVALID_PARAM) {
975 hparams = (mport->hstart << 4) | mport->hstop;
976
977 reg[len] = SWRM_CMD_FIFO_WR_CMD;
978 val[len++] = SWR_REG_VAL_PACK(hparams,
979 port_req->dev_num, 0x00,
980 SWRS_DP_HCONTROL_BANK(slv_id,
981 bank));
982 }
983 if (mport->word_length != SWR_INVALID_PARAM) {
984 reg[len] = SWRM_CMD_FIFO_WR_CMD;
985 val[len++] =
986 SWR_REG_VAL_PACK(mport->word_length,
987 port_req->dev_num, 0x00,
988 SWRS_DP_BLOCK_CONTROL_1(slv_id));
989 }
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +0530990 if (mport->blk_pack_mode != SWR_INVALID_PARAM
991 && swrm->master_id != MASTER_ID_WSA) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530992 reg[len] = SWRM_CMD_FIFO_WR_CMD;
993 val[len++] =
994 SWR_REG_VAL_PACK(mport->blk_pack_mode,
995 port_req->dev_num, 0x00,
996 SWRS_DP_BLOCK_CONTROL_3_BANK(slv_id,
997 bank));
998 }
999 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
1000 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1001 val[len++] =
1002 SWR_REG_VAL_PACK(mport->blk_grp_count,
1003 port_req->dev_num, 0x00,
1004 SWRS_DP_BLOCK_CONTROL_2_BANK(slv_id,
1005 bank));
1006 }
1007 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
1008 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1009 val[len++] =
1010 SWR_REG_VAL_PACK(mport->lane_ctrl,
1011 port_req->dev_num, 0x00,
1012 SWRS_DP_LANE_CONTROL_BANK(slv_id,
1013 bank));
1014 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301015 port_req->ch_en = port_req->req_ch;
1016 }
1017 value = ((mport->req_ch)
1018 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +05301019
1020 if (mport->offset2 != SWR_INVALID_PARAM)
1021 value |= ((mport->offset2)
1022 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301023 value |= ((mport->offset1)
1024 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
1025 value |= mport->sinterval;
1026
1027
1028 reg[len] = SWRM_DP_PORT_CTRL_BANK(i + 1, bank);
1029 val[len++] = value;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301030 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
1031 __func__, i,
1032 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)), value);
1033
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301034 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
1035 reg[len] = SWRM_DP_PORT_CTRL_2_BANK(i + 1, bank);
1036 val[len++] = mport->lane_ctrl;
1037 }
1038 if (mport->word_length != SWR_INVALID_PARAM) {
1039 reg[len] = SWRM_DP_BLOCK_CTRL_1(i + 1);
1040 val[len++] = mport->word_length;
1041 }
1042
1043 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
1044 reg[len] = SWRM_DP_BLOCK_CTRL2_BANK(i + 1, bank);
1045 val[len++] = mport->blk_grp_count;
1046 }
1047 if (mport->hstart != SWR_INVALID_PARAM
1048 && mport->hstop != SWR_INVALID_PARAM) {
1049 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
Laxminath Kasame30eef72018-11-05 17:40:09 +05301050 hparams = (mport->hstop << 4) | mport->hstart;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301051 val[len++] = hparams;
Laxminath Kasam990c70b2018-11-09 23:15:09 +05301052 } else {
1053 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
1054 hparams = (SWR_HSTOP_MAX_VAL << 4) | SWR_HSTART_MIN_VAL;
1055 val[len++] = hparams;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301056 }
1057 if (mport->blk_pack_mode != SWR_INVALID_PARAM) {
1058 reg[len] = SWRM_DP_BLOCK_CTRL3_BANK(i + 1, bank);
1059 val[len++] = mport->blk_pack_mode;
1060 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301061 mport->ch_en = mport->req_ch;
1062
1063 }
Sudheer Papothi0016db12019-06-11 04:42:38 +05301064 swrm_reg_dump(swrm, reg, val, len, __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301065 swr_master_bulk_write(swrm, reg, val, len);
1066}
1067
1068static void swrm_apply_port_config(struct swr_master *master)
1069{
1070 u8 bank;
1071 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1072
1073 if (!swrm) {
1074 pr_err("%s: Invalid handle to swr controller\n",
1075 __func__);
1076 return;
1077 }
1078
1079 bank = get_inactive_bank_num(swrm);
1080 dev_dbg(swrm->dev, "%s: enter bank: %d master_ports: %d\n",
1081 __func__, bank, master->num_port);
1082
1083
1084 swrm_cmd_fifo_wr_cmd(swrm, 0x01, 0xF, 0x00,
1085 SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(bank));
1086
1087 swrm_copy_data_port_config(master, bank);
1088}
1089
1090static int swrm_slvdev_datapath_control(struct swr_master *master, bool enable)
1091{
1092 u8 bank;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301093 u32 value, n_row, n_col;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301094 int ret;
1095 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1096 int mask = (SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK |
1097 SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK |
1098 SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_BMSK);
1099 u8 inactive_bank;
1100
1101 if (!swrm) {
1102 pr_err("%s: swrm is null\n", __func__);
1103 return -EFAULT;
1104 }
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301105
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301106 mutex_lock(&swrm->mlock);
1107
Ramprasad Katkam979b7c92019-05-17 15:31:21 +05301108 /*
1109 * During disable if master is already down, which implies an ssr/pdr
1110 * scenario, just mark ports as disabled and exit
1111 */
1112 if (swrm->state == SWR_MSTR_SSR && !enable) {
1113 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1114 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1115 __func__);
1116 goto exit;
1117 }
1118 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
1119 swrm_cleanup_disabled_port_reqs(master);
1120 if (!swrm_is_port_en(master)) {
1121 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1122 __func__);
1123 pm_runtime_mark_last_busy(swrm->dev);
1124 pm_runtime_put_autosuspend(swrm->dev);
1125 }
1126 goto exit;
1127 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301128 bank = get_inactive_bank_num(swrm);
1129
1130 if (enable) {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301131 if (!test_bit(ENABLE_PENDING, &swrm->port_req_pending)) {
1132 dev_dbg(swrm->dev, "%s:No pending connect port req\n",
1133 __func__);
1134 goto exit;
1135 }
1136 clear_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301137 ret = swrm_get_port_config(swrm);
1138 if (ret) {
1139 /* cannot accommodate ports */
1140 swrm_cleanup_disabled_port_reqs(master);
1141 mutex_unlock(&swrm->mlock);
1142 return -EINVAL;
1143 }
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301144 swr_master_write(swrm, SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301145 SWRM_INTERRUPT_STATUS_MASK);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301146 /* apply the new port config*/
1147 swrm_apply_port_config(master);
1148 } else {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301149 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1150 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1151 __func__);
1152 goto exit;
1153 }
1154 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301155 swrm_disable_ports(master, bank);
1156 }
1157 dev_dbg(swrm->dev, "%s: enable: %d, cfg_devs: %d\n",
1158 __func__, enable, swrm->num_cfg_devs);
1159
1160 if (enable) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301161 /* set col = 16 */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301162 n_col = SWR_MAX_COL;
1163 } else {
1164 /*
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301165 * Do not change to col = 2 if there are still active ports
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301166 */
1167 if (!master->num_port)
1168 n_col = SWR_MIN_COL;
1169 else
1170 n_col = SWR_MAX_COL;
1171 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301172 /* Use default 50 * x, frame shape. Change based on mclk */
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301173 if (swrm->mclk_freq == MCLK_FREQ_NATIVE) {
1174 dev_dbg(swrm->dev, "setting 64 x %d frameshape\n",
1175 n_col ? 16 : 2);
1176 n_row = SWR_ROW_64;
1177 } else {
1178 dev_dbg(swrm->dev, "setting 50 x %d frameshape\n",
1179 n_col ? 16 : 2);
1180 n_row = SWR_ROW_50;
1181 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301182 value = swr_master_read(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank));
1183 value &= (~mask);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301184 value |= ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301185 (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
1186 (0 << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
1187 swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1188
1189 dev_dbg(swrm->dev, "%s: regaddr: 0x%x, value: 0x%x\n", __func__,
1190 SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1191
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301192 enable_bank_switch(swrm, bank, n_row, n_col);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301193 inactive_bank = bank ? 0 : 1;
1194
1195 if (enable)
1196 swrm_copy_data_port_config(master, inactive_bank);
1197 else {
1198 swrm_disable_ports(master, inactive_bank);
1199 swrm_cleanup_disabled_port_reqs(master);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301200 }
1201 if (!swrm_is_port_en(master)) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301202 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1203 __func__);
1204 pm_runtime_mark_last_busy(swrm->dev);
1205 pm_runtime_put_autosuspend(swrm->dev);
1206 }
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301207exit:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301208 mutex_unlock(&swrm->mlock);
1209return 0;
1210}
1211
1212static int swrm_connect_port(struct swr_master *master,
1213 struct swr_params *portinfo)
1214{
1215 int i;
1216 struct swr_port_info *port_req;
1217 int ret = 0;
1218 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1219 struct swrm_mports *mport;
1220 u8 mstr_port_id, mstr_ch_msk;
1221
1222 dev_dbg(&master->dev, "%s: enter\n", __func__);
1223 if (!portinfo)
1224 return -EINVAL;
1225
1226 if (!swrm) {
1227 dev_err(&master->dev,
1228 "%s: Invalid handle to swr controller\n",
1229 __func__);
1230 return -EINVAL;
1231 }
1232
1233 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301234 mutex_lock(&swrm->devlock);
1235 if (!swrm->dev_up) {
1236 mutex_unlock(&swrm->devlock);
1237 mutex_unlock(&swrm->mlock);
1238 return -EINVAL;
1239 }
1240 mutex_unlock(&swrm->devlock);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301241 if (!swrm_is_port_en(master))
1242 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301243
1244 for (i = 0; i < portinfo->num_port; i++) {
1245 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_msk,
1246 portinfo->port_type[i],
1247 portinfo->port_id[i]);
1248 if (ret) {
1249 dev_err(&master->dev,
1250 "%s: mstr portid for slv port %d not found\n",
1251 __func__, portinfo->port_id[i]);
1252 goto port_fail;
1253 }
1254
1255 mport = &(swrm->mport_cfg[mstr_port_id]);
1256 /* get port req */
1257 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1258 portinfo->dev_num);
1259 if (!port_req) {
1260 dev_dbg(&master->dev, "%s: new req:port id %d dev %d\n",
1261 __func__, portinfo->port_id[i],
1262 portinfo->dev_num);
1263 port_req = kzalloc(sizeof(struct swr_port_info),
1264 GFP_KERNEL);
1265 if (!port_req) {
1266 ret = -ENOMEM;
1267 goto mem_fail;
1268 }
1269 port_req->dev_num = portinfo->dev_num;
1270 port_req->slave_port_id = portinfo->port_id[i];
1271 port_req->num_ch = portinfo->num_ch[i];
1272 port_req->ch_rate = portinfo->ch_rate[i];
1273 port_req->ch_en = 0;
1274 port_req->master_port_id = mstr_port_id;
1275 list_add(&port_req->list, &mport->port_req_list);
1276 }
1277 port_req->req_ch |= portinfo->ch_en[i];
1278
1279 dev_dbg(&master->dev,
1280 "%s: mstr port %d, slv port %d ch_rate %d num_ch %d\n",
1281 __func__, port_req->master_port_id,
1282 port_req->slave_port_id, port_req->ch_rate,
1283 port_req->num_ch);
1284 /* Put the port req on master port */
1285 mport = &(swrm->mport_cfg[mstr_port_id]);
1286 mport->port_en = true;
1287 mport->req_ch |= mstr_ch_msk;
1288 master->port_en_mask |= (1 << mstr_port_id);
1289 }
1290 master->num_port += portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301291 set_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301292 swr_port_response(master, portinfo->tid);
1293
1294 mutex_unlock(&swrm->mlock);
1295 return 0;
1296
1297port_fail:
1298mem_fail:
1299 /* cleanup port reqs in error condition */
1300 swrm_cleanup_disabled_port_reqs(master);
1301 mutex_unlock(&swrm->mlock);
1302 return ret;
1303}
1304
1305static int swrm_disconnect_port(struct swr_master *master,
1306 struct swr_params *portinfo)
1307{
1308 int i, ret = 0;
1309 struct swr_port_info *port_req;
1310 struct swrm_mports *mport;
1311 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1312 u8 mstr_port_id, mstr_ch_mask;
1313
1314 if (!swrm) {
1315 dev_err(&master->dev,
1316 "%s: Invalid handle to swr controller\n",
1317 __func__);
1318 return -EINVAL;
1319 }
1320
1321 if (!portinfo) {
1322 dev_err(&master->dev, "%s: portinfo is NULL\n", __func__);
1323 return -EINVAL;
1324 }
1325 mutex_lock(&swrm->mlock);
1326
1327 for (i = 0; i < portinfo->num_port; i++) {
1328
1329 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_mask,
1330 portinfo->port_type[i], portinfo->port_id[i]);
1331 if (ret) {
1332 dev_err(&master->dev,
1333 "%s: mstr portid for slv port %d not found\n",
1334 __func__, portinfo->port_id[i]);
1335 mutex_unlock(&swrm->mlock);
1336 return -EINVAL;
1337 }
1338 mport = &(swrm->mport_cfg[mstr_port_id]);
1339 /* get port req */
1340 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1341 portinfo->dev_num);
1342
1343 if (!port_req) {
1344 dev_err(&master->dev, "%s:port not enabled : port %d\n",
1345 __func__, portinfo->port_id[i]);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05301346 mutex_unlock(&swrm->mlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301347 return -EINVAL;
1348 }
1349 port_req->req_ch &= ~portinfo->ch_en[i];
1350 mport->req_ch &= ~mstr_ch_mask;
1351 }
1352 master->num_port -= portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301353 set_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301354 swr_port_response(master, portinfo->tid);
1355 mutex_unlock(&swrm->mlock);
1356
1357 return 0;
1358}
1359
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301360static int swrm_find_alert_slave(struct swr_mstr_ctrl *swrm,
1361 int status, u8 *devnum)
1362{
1363 int i;
1364 bool found = false;
1365
1366 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1367 if ((status & SWRM_MCP_SLV_STATUS_MASK) == SWR_ALERT) {
1368 *devnum = i;
1369 found = true;
1370 break;
1371 }
1372 status >>= 2;
1373 }
1374 if (found)
1375 return 0;
1376 else
1377 return -EINVAL;
1378}
1379
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301380static void swrm_enable_slave_irq(struct swr_mstr_ctrl *swrm)
1381{
1382 int i;
1383 int status = 0;
1384
1385 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1386 if (!status) {
1387 dev_dbg_ratelimited(swrm->dev, "%s: slaves status is 0x%x\n",
1388 __func__, status);
1389 return;
1390 }
1391 dev_dbg(swrm->dev, "%s: slave status: 0x%x\n", __func__, status);
1392 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1393 if (status & SWRM_MCP_SLV_STATUS_MASK)
1394 swrm_cmd_fifo_wr_cmd(swrm, 0x4, i, 0x0,
1395 SWRS_SCP_INT_STATUS_MASK_1);
1396 status >>= 2;
1397 }
1398}
1399
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301400static int swrm_check_slave_change_status(struct swr_mstr_ctrl *swrm,
1401 int status, u8 *devnum)
1402{
1403 int i;
1404 int new_sts = status;
1405 int ret = SWR_NOT_PRESENT;
1406
1407 if (status != swrm->slave_status) {
1408 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1409 if ((status & SWRM_MCP_SLV_STATUS_MASK) !=
1410 (swrm->slave_status & SWRM_MCP_SLV_STATUS_MASK)) {
1411 ret = (status & SWRM_MCP_SLV_STATUS_MASK);
1412 *devnum = i;
1413 break;
1414 }
1415 status >>= 2;
1416 swrm->slave_status >>= 2;
1417 }
1418 swrm->slave_status = new_sts;
1419 }
1420 return ret;
1421}
1422
1423static irqreturn_t swr_mstr_interrupt(int irq, void *dev)
1424{
1425 struct swr_mstr_ctrl *swrm = dev;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301426 u32 value, intr_sts, intr_sts_masked;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301427 u32 temp = 0;
1428 u32 status, chg_sts, i;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301429 u8 devnum = 0;
1430 int ret = IRQ_HANDLED;
1431 struct swr_device *swr_dev;
1432 struct swr_master *mstr = &swrm->master;
1433
Ramprasad Katkam57349872018-11-11 18:34:57 +05301434 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1435 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1436 return IRQ_NONE;
1437 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301438
1439 mutex_lock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301440 if (swrm_clk_request(swrm, true)) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +05301441 dev_err_ratelimited(swrm->dev, "%s:clk request failed\n",
1442 __func__);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301443 mutex_unlock(&swrm->reslock);
1444 goto exit;
1445 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301446 mutex_unlock(&swrm->reslock);
1447
1448 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301449 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301450handle_irq:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301451 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301452 value = intr_sts_masked & (1 << i);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301453 if (!value)
1454 continue;
1455
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301456 switch (value) {
1457 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1458 dev_dbg(swrm->dev, "Trigger irq to slave device\n");
1459 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301460 ret = swrm_find_alert_slave(swrm, status, &devnum);
1461 if (ret) {
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301462 dev_err_ratelimited(swrm->dev,
1463 "no slave alert found.spurious interrupt\n");
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05301464 break;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301465 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301466 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1467 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1468 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1469 SWRS_SCP_INT_STATUS_CLEAR_1);
1470 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1471 SWRS_SCP_INT_STATUS_CLEAR_1);
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301472
1473
1474 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1475 if (swr_dev->dev_num != devnum)
1476 continue;
1477 if (swr_dev->slave_irq) {
1478 do {
Ramprasad Katkam2586a4b2019-03-18 16:53:39 +05301479 swr_dev->slave_irq_pending = 0;
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301480 handle_nested_irq(
1481 irq_find_mapping(
1482 swr_dev->slave_irq, 0));
1483 } while (swr_dev->slave_irq_pending);
1484 }
1485
1486 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301487 break;
1488 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1489 dev_dbg(swrm->dev, "SWR new slave attached\n");
1490 break;
1491 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1492 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1493 if (status == swrm->slave_status) {
1494 dev_dbg(swrm->dev,
1495 "%s: No change in slave status: %d\n",
1496 __func__, status);
1497 break;
1498 }
1499 chg_sts = swrm_check_slave_change_status(swrm, status,
1500 &devnum);
1501 switch (chg_sts) {
1502 case SWR_NOT_PRESENT:
1503 dev_dbg(swrm->dev, "device %d got detached\n",
1504 devnum);
1505 break;
1506 case SWR_ATTACHED_OK:
1507 dev_dbg(swrm->dev, "device %d got attached\n",
1508 devnum);
Ramprasad Katkamdebe8932018-09-25 18:08:18 +05301509 /* enable host irq from slave device*/
1510 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1511 SWRS_SCP_INT_STATUS_CLEAR_1);
1512 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1513 SWRS_SCP_INT_STATUS_MASK_1);
1514
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301515 break;
1516 case SWR_ALERT:
1517 dev_dbg(swrm->dev,
1518 "device %d has pending interrupt\n",
1519 devnum);
1520 break;
1521 }
1522 break;
1523 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1524 dev_err_ratelimited(swrm->dev,
1525 "SWR bus clsh detected\n");
1526 break;
1527 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1528 dev_dbg(swrm->dev, "SWR read FIFO overflow\n");
1529 break;
1530 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1531 dev_dbg(swrm->dev, "SWR read FIFO underflow\n");
1532 break;
1533 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1534 dev_dbg(swrm->dev, "SWR write FIFO overflow\n");
1535 break;
1536 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1537 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1538 dev_err_ratelimited(swrm->dev,
1539 "SWR CMD error, fifo status 0x%x, flushing fifo\n",
1540 value);
1541 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1542 break;
1543 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301544 dev_err_ratelimited(swrm->dev, "SWR Port collision detected\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301545 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301546 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301547 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301548 break;
1549 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1550 dev_dbg(swrm->dev, "SWR read enable valid mismatch\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301551 swrm->intr_mask &=
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301552 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1553 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301554 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301555 break;
1556 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1557 complete(&swrm->broadcast);
1558 dev_dbg(swrm->dev, "SWR cmd id finished\n");
1559 break;
1560 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_AUTO_ENUM_FINISHED:
1561 break;
1562 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED:
1563 break;
1564 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL:
1565 break;
1566 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED:
1567 complete(&swrm->reset);
1568 break;
1569 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED:
1570 break;
1571 default:
1572 dev_err_ratelimited(swrm->dev,
1573 "SWR unknown interrupt\n");
1574 ret = IRQ_NONE;
1575 break;
1576 }
1577 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301578 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1579 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
Ramprasad Katkam83303512018-10-11 17:34:22 +05301580
1581 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301582 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301583
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301584 if (intr_sts_masked) {
Ramprasad Katkam83303512018-10-11 17:34:22 +05301585 dev_dbg(swrm->dev, "%s: new interrupt received\n", __func__);
1586 goto handle_irq;
1587 }
1588
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301589 mutex_lock(&swrm->reslock);
1590 swrm_clk_request(swrm, false);
1591 mutex_unlock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301592exit:
Ramprasad Katkam57349872018-11-11 18:34:57 +05301593 swrm_unlock_sleep(swrm);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301594 return ret;
1595}
1596
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301597static irqreturn_t swr_mstr_interrupt_v2(int irq, void *dev)
1598{
1599 struct swr_mstr_ctrl *swrm = dev;
1600 u32 value, intr_sts, intr_sts_masked;
1601 u32 temp = 0;
1602 u32 status, chg_sts, i;
1603 u8 devnum = 0;
1604 int ret = IRQ_HANDLED;
1605 struct swr_device *swr_dev;
1606 struct swr_master *mstr = &swrm->master;
1607
1608 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1609 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1610 return IRQ_NONE;
1611 }
1612
1613 mutex_lock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301614 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
1615 ret = IRQ_NONE;
1616 goto exit;
1617 }
1618 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
1619 ret = IRQ_NONE;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301620 goto err_audio_hw_vote;
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001621 }
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301622 swrm_clk_request(swrm, true);
1623 mutex_unlock(&swrm->reslock);
1624
1625 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1626 intr_sts_masked = intr_sts & swrm->intr_mask;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301627
1628 dev_dbg(swrm->dev, "%s: status: 0x%x \n", __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301629handle_irq:
1630 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
1631 value = intr_sts_masked & (1 << i);
1632 if (!value)
1633 continue;
1634
1635 switch (value) {
1636 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1637 dev_dbg(swrm->dev, "%s: Trigger irq to slave device\n",
1638 __func__);
1639 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1640 ret = swrm_find_alert_slave(swrm, status, &devnum);
1641 if (ret) {
1642 dev_err_ratelimited(swrm->dev,
1643 "%s: no slave alert found.spurious interrupt\n",
1644 __func__);
1645 break;
1646 }
1647 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1648 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1649 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1650 SWRS_SCP_INT_STATUS_CLEAR_1);
1651 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1652 SWRS_SCP_INT_STATUS_CLEAR_1);
1653
1654
1655 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1656 if (swr_dev->dev_num != devnum)
1657 continue;
1658 if (swr_dev->slave_irq) {
1659 do {
1660 handle_nested_irq(
1661 irq_find_mapping(
1662 swr_dev->slave_irq, 0));
1663 } while (swr_dev->slave_irq_pending);
1664 }
1665
1666 }
1667 break;
1668 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1669 dev_dbg(swrm->dev, "%s: SWR new slave attached\n",
1670 __func__);
1671 break;
1672 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1673 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1674 if (status == swrm->slave_status) {
1675 dev_dbg(swrm->dev,
1676 "%s: No change in slave status: %d\n",
1677 __func__, status);
1678 break;
1679 }
1680 chg_sts = swrm_check_slave_change_status(swrm, status,
1681 &devnum);
1682 switch (chg_sts) {
1683 case SWR_NOT_PRESENT:
1684 dev_dbg(swrm->dev,
1685 "%s: device %d got detached\n",
1686 __func__, devnum);
1687 break;
1688 case SWR_ATTACHED_OK:
1689 dev_dbg(swrm->dev,
1690 "%s: device %d got attached\n",
1691 __func__, devnum);
1692 /* enable host irq from slave device*/
1693 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1694 SWRS_SCP_INT_STATUS_CLEAR_1);
1695 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1696 SWRS_SCP_INT_STATUS_MASK_1);
1697
1698 break;
1699 case SWR_ALERT:
1700 dev_dbg(swrm->dev,
1701 "%s: device %d has pending interrupt\n",
1702 __func__, devnum);
1703 break;
1704 }
1705 break;
1706 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1707 dev_err_ratelimited(swrm->dev,
1708 "%s: SWR bus clsh detected\n",
1709 __func__);
1710 break;
1711 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1712 dev_dbg(swrm->dev, "%s: SWR read FIFO overflow\n",
1713 __func__);
1714 break;
1715 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1716 dev_dbg(swrm->dev, "%s: SWR read FIFO underflow\n",
1717 __func__);
1718 break;
1719 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1720 dev_dbg(swrm->dev, "%s: SWR write FIFO overflow\n",
1721 __func__);
1722 break;
1723 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1724 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1725 dev_err_ratelimited(swrm->dev,
1726 "%s: SWR CMD error, fifo status 0x%x, flushing fifo\n",
1727 __func__, value);
1728 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1729 break;
1730 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
1731 dev_err_ratelimited(swrm->dev,
1732 "%s: SWR Port collision detected\n",
1733 __func__);
1734 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
1735 swr_master_write(swrm,
1736 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1737 break;
1738 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1739 dev_dbg(swrm->dev,
1740 "%s: SWR read enable valid mismatch\n",
1741 __func__);
1742 swrm->intr_mask &=
1743 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1744 swr_master_write(swrm,
1745 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1746 break;
1747 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1748 complete(&swrm->broadcast);
1749 dev_dbg(swrm->dev, "%s: SWR cmd id finished\n",
1750 __func__);
1751 break;
1752 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED_V2:
1753 break;
1754 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL_V2:
1755 break;
1756 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED_V2:
1757 break;
1758 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED_V2:
1759 break;
1760 case SWRM_INTERRUPT_STATUS_EXT_CLK_STOP_WAKEUP:
1761 if (swrm->state == SWR_MSTR_UP)
1762 dev_dbg(swrm->dev,
1763 "%s:SWR Master is already up\n",
1764 __func__);
1765 else
1766 dev_err_ratelimited(swrm->dev,
1767 "%s: SWR wokeup during clock stop\n",
1768 __func__);
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301769 /* It might be possible the slave device gets reset
1770 * and slave interrupt gets missed. So re-enable
1771 * Host IRQ and process slave pending
1772 * interrupts, if any.
1773 */
1774 swrm_enable_slave_irq(swrm);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301775 break;
1776 default:
1777 dev_err_ratelimited(swrm->dev,
1778 "%s: SWR unknown interrupt value: %d\n",
1779 __func__, value);
1780 ret = IRQ_NONE;
1781 break;
1782 }
1783 }
1784 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1785 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
1786
1787 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1788 intr_sts_masked = intr_sts & swrm->intr_mask;
1789
1790 if (intr_sts_masked) {
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301791 dev_dbg(swrm->dev, "%s: new interrupt received 0x%x\n",
1792 __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301793 goto handle_irq;
1794 }
1795
1796 mutex_lock(&swrm->reslock);
1797 swrm_clk_request(swrm, false);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301798 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
Sudheer Papothi06f43412019-07-09 03:32:54 +05301799
1800err_audio_hw_vote:
Sudheer Papothi384addd2019-06-14 02:26:52 +05301801 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001802exit:
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301803 mutex_unlock(&swrm->reslock);
1804 swrm_unlock_sleep(swrm);
1805 return ret;
1806}
1807
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301808static irqreturn_t swrm_wakeup_interrupt(int irq, void *dev)
1809{
1810 struct swr_mstr_ctrl *swrm = dev;
1811 int ret = IRQ_HANDLED;
1812
1813 if (!swrm || !(swrm->dev)) {
1814 pr_err("%s: swrm or dev is null\n", __func__);
1815 return IRQ_NONE;
1816 }
1817 mutex_lock(&swrm->devlock);
1818 if (!swrm->dev_up) {
1819 if (swrm->wake_irq > 0)
1820 disable_irq_nosync(swrm->wake_irq);
1821 mutex_unlock(&swrm->devlock);
1822 return ret;
1823 }
1824 mutex_unlock(&swrm->devlock);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301825 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1826 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1827 goto exit;
1828 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301829 if (swrm->wake_irq > 0)
1830 disable_irq_nosync(swrm->wake_irq);
1831 pm_runtime_get_sync(swrm->dev);
1832 pm_runtime_mark_last_busy(swrm->dev);
1833 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301834 swrm_unlock_sleep(swrm);
1835exit:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301836 return ret;
1837}
1838
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301839static void swrm_wakeup_work(struct work_struct *work)
1840{
1841 struct swr_mstr_ctrl *swrm;
1842
1843 swrm = container_of(work, struct swr_mstr_ctrl,
1844 wakeup_work);
1845 if (!swrm || !(swrm->dev)) {
1846 pr_err("%s: swrm or dev is null\n", __func__);
1847 return;
1848 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301849
1850 mutex_lock(&swrm->devlock);
1851 if (!swrm->dev_up) {
1852 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301853 goto exit;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301854 }
1855 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301856 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1857 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1858 goto exit;
1859 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301860 pm_runtime_get_sync(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301861 pm_runtime_mark_last_busy(swrm->dev);
1862 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301863 swrm_unlock_sleep(swrm);
1864exit:
1865 pm_relax(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301866}
1867
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301868static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
1869{
1870 u32 val;
1871
1872 swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1873 val = (swrm->slave_status >> (devnum * 2));
1874 val &= SWRM_MCP_SLV_STATUS_MASK;
1875 return val;
1876}
1877
1878static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
1879 u8 *dev_num)
1880{
1881 int i;
1882 u64 id = 0;
1883 int ret = -EINVAL;
1884 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1885 struct swr_device *swr_dev;
1886 u32 num_dev = 0;
1887
1888 if (!swrm) {
1889 pr_err("%s: Invalid handle to swr controller\n",
1890 __func__);
1891 return ret;
1892 }
1893 if (swrm->num_dev)
1894 num_dev = swrm->num_dev;
1895 else
1896 num_dev = mstr->num_dev;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301897
1898 mutex_lock(&swrm->devlock);
1899 if (!swrm->dev_up) {
1900 mutex_unlock(&swrm->devlock);
1901 return ret;
1902 }
1903 mutex_unlock(&swrm->devlock);
1904
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301905 pm_runtime_get_sync(swrm->dev);
1906 for (i = 1; i < (num_dev + 1); i++) {
1907 id = ((u64)(swr_master_read(swrm,
1908 SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
1909 id |= swr_master_read(swrm,
1910 SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301911
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301912 /*
1913 * As pm_runtime_get_sync() brings all slaves out of reset
1914 * update logical device number for all slaves.
1915 */
1916 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1917 if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
1918 u32 status = swrm_get_device_status(swrm, i);
1919
1920 if ((status == 0x01) || (status == 0x02)) {
1921 swr_dev->dev_num = i;
1922 if ((id & SWR_DEV_ID_MASK) == dev_id) {
1923 *dev_num = i;
1924 ret = 0;
1925 }
1926 dev_dbg(swrm->dev,
1927 "%s: devnum %d is assigned for dev addr %lx\n",
1928 __func__, i, swr_dev->addr);
1929 }
1930 }
1931 }
1932 }
1933 if (ret)
1934 dev_err(swrm->dev, "%s: device 0x%llx is not ready\n",
1935 __func__, dev_id);
1936
1937 pm_runtime_mark_last_busy(swrm->dev);
1938 pm_runtime_put_autosuspend(swrm->dev);
1939 return ret;
1940}
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301941
1942static void swrm_device_wakeup_vote(struct swr_master *mstr)
1943{
1944 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1945
1946 if (!swrm) {
1947 pr_err("%s: Invalid handle to swr controller\n",
1948 __func__);
1949 return;
1950 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05301951 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1952 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1953 return;
1954 }
Sudheer Papothi384addd2019-06-14 02:26:52 +05301955 if (++swrm->hw_core_clk_en == 1)
1956 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
1957 dev_err(swrm->dev, "%s:lpass core hw enable failed\n",
1958 __func__);
1959 --swrm->hw_core_clk_en;
1960 }
1961 if ( ++swrm->aud_core_clk_en == 1)
1962 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
1963 dev_err(swrm->dev, "%s:lpass audio hw enable failed\n",
1964 __func__);
1965 --swrm->aud_core_clk_en;
1966 }
1967 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
1968 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301969 pm_runtime_get_sync(swrm->dev);
1970}
1971
1972static void swrm_device_wakeup_unvote(struct swr_master *mstr)
1973{
1974 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1975
1976 if (!swrm) {
1977 pr_err("%s: Invalid handle to swr controller\n",
1978 __func__);
1979 return;
1980 }
1981 pm_runtime_mark_last_busy(swrm->dev);
1982 pm_runtime_put_autosuspend(swrm->dev);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301983 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
1984 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
1985
1986 --swrm->aud_core_clk_en;
1987 if (swrm->aud_core_clk_en < 0)
1988 swrm->aud_core_clk_en = 0;
1989 else if (swrm->aud_core_clk_en == 0)
1990 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
1991
1992 --swrm->hw_core_clk_en;
1993 if (swrm->hw_core_clk_en < 0)
1994 swrm->hw_core_clk_en = 0;
1995 else if (swrm->hw_core_clk_en == 0)
1996 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
1997
Ramprasad Katkam57349872018-11-11 18:34:57 +05301998 swrm_unlock_sleep(swrm);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301999}
2000
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302001static int swrm_master_init(struct swr_mstr_ctrl *swrm)
2002{
2003 int ret = 0;
2004 u32 val;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302005 u8 row_ctrl = SWR_ROW_50;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302006 u8 col_ctrl = SWR_MIN_COL;
2007 u8 ssp_period = 1;
2008 u8 retry_cmd_num = 3;
2009 u32 reg[SWRM_MAX_INIT_REG];
2010 u32 value[SWRM_MAX_INIT_REG];
2011 int len = 0;
2012
2013 /* Clear Rows and Cols */
2014 val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
2015 (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
2016 (ssp_period << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
2017
2018 reg[len] = SWRM_MCP_FRAME_CTRL_BANK_ADDR(0);
2019 value[len++] = val;
2020
2021 /* Set Auto enumeration flag */
2022 reg[len] = SWRM_ENUMERATOR_CFG_ADDR;
2023 value[len++] = 1;
2024
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302025 /* Configure No pings */
2026 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2027 val &= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK;
2028 val |= (0x1f << SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT);
2029 reg[len] = SWRM_MCP_CFG_ADDR;
2030 value[len++] = val;
2031
2032 /* Configure number of retries of a read/write cmd */
2033 val = (retry_cmd_num << SWRM_CMD_FIFO_CFG_NUM_OF_CMD_RETRY_SHFT);
2034 reg[len] = SWRM_CMD_FIFO_CFG_ADDR;
2035 value[len++] = val;
2036
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302037 reg[len] = SWRM_MCP_BUS_CTRL_ADDR;
2038 value[len++] = 0x2;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302039
Ramprasad Katkam83303512018-10-11 17:34:22 +05302040 /* Set IRQ to PULSE */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302041 reg[len] = SWRM_COMP_CFG_ADDR;
Ramprasad Katkam83303512018-10-11 17:34:22 +05302042 value[len++] = 0x02;
2043
2044 reg[len] = SWRM_COMP_CFG_ADDR;
2045 value[len++] = 0x03;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302046
2047 reg[len] = SWRM_INTERRUPT_CLEAR;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302048 value[len++] = 0xFFFFFFFF;
2049
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302050 swrm->intr_mask = SWRM_INTERRUPT_STATUS_MASK;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302051 /* Mask soundwire interrupts */
2052 reg[len] = SWRM_INTERRUPT_MASK_ADDR;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302053 value[len++] = swrm->intr_mask;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302054
2055 reg[len] = SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302056 value[len++] = swrm->intr_mask;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302057
2058 swr_master_bulk_write(swrm, reg, value, len);
2059
Sudheer Papothi63f48152018-11-15 01:08:03 +05302060 /*
2061 * For SWR master version 1.5.1, continue
2062 * execute on command ignore.
2063 */
2064 if (swrm->version == SWRM_VERSION_1_5_1)
2065 swr_master_write(swrm, SWRM_CMD_FIFO_CFG_ADDR,
2066 (swr_master_read(swrm,
2067 SWRM_CMD_FIFO_CFG_ADDR) | 0x80000000));
2068
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302069 return ret;
2070}
2071
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302072static int swrm_event_notify(struct notifier_block *self,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302073 unsigned long action, void *data)
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302074{
2075 struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302076 event_notifier);
2077
2078 if (!swrm || !(swrm->dev)) {
2079 pr_err("%s: swrm or dev is NULL\n", __func__);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302080 return -EINVAL;
2081 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302082 switch (action) {
2083 case MSM_AUD_DC_EVENT:
2084 schedule_work(&(swrm->dc_presence_work));
2085 break;
2086 case SWR_WAKE_IRQ_EVENT:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302087 if (swrm->ipc_wakeup && !swrm->ipc_wakeup_triggered) {
2088 swrm->ipc_wakeup_triggered = true;
Ramprasad Katkam57349872018-11-11 18:34:57 +05302089 pm_stay_awake(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302090 schedule_work(&swrm->wakeup_work);
Ramprasad Katkamcd61c6e2018-09-18 13:22:58 +05302091 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302092 break;
2093 default:
2094 dev_err(swrm->dev, "%s: invalid event type: %lu\n",
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302095 __func__, action);
2096 return -EINVAL;
2097 }
2098
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302099 return 0;
2100}
2101
2102static void swrm_notify_work_fn(struct work_struct *work)
2103{
2104 struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302105 dc_presence_work);
2106
2107 if (!swrm || !swrm->pdev) {
2108 pr_err("%s: swrm or pdev is NULL\n", __func__);
2109 return;
2110 }
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302111 swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL);
2112}
2113
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302114static int swrm_probe(struct platform_device *pdev)
2115{
2116 struct swr_mstr_ctrl *swrm;
2117 struct swr_ctrl_platform_data *pdata;
2118 u32 i, num_ports, port_num, port_type, ch_mask;
2119 u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0;
2120 int ret = 0;
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302121 struct clk *lpass_core_hw_vote = NULL;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302122 struct clk *lpass_core_audio = NULL;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302123
2124 /* Allocate soundwire master driver structure */
2125 swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl),
2126 GFP_KERNEL);
2127 if (!swrm) {
2128 ret = -ENOMEM;
2129 goto err_memory_fail;
2130 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302131 swrm->pdev = pdev;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302132 swrm->dev = &pdev->dev;
2133 platform_set_drvdata(pdev, swrm);
2134 swr_set_ctrl_data(&swrm->master, swrm);
2135 pdata = dev_get_platdata(&pdev->dev);
2136 if (!pdata) {
2137 dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
2138 __func__);
2139 ret = -EINVAL;
2140 goto err_pdata_fail;
2141 }
2142 swrm->handle = (void *)pdata->handle;
2143 if (!swrm->handle) {
2144 dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
2145 __func__);
2146 ret = -EINVAL;
2147 goto err_pdata_fail;
2148 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302149 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr_master_id",
2150 &swrm->master_id);
2151 if (ret) {
2152 dev_err(&pdev->dev, "%s: failed to get master id\n", __func__);
2153 goto err_pdata_fail;
2154 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302155 if (!(of_property_read_u32(pdev->dev.of_node,
2156 "swrm-io-base", &swrm->swrm_base_reg)))
2157 ret = of_property_read_u32(pdev->dev.of_node,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302158 "swrm-io-base", &swrm->swrm_base_reg);
2159 if (!swrm->swrm_base_reg) {
2160 swrm->read = pdata->read;
2161 if (!swrm->read) {
2162 dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
2163 __func__);
2164 ret = -EINVAL;
2165 goto err_pdata_fail;
2166 }
2167 swrm->write = pdata->write;
2168 if (!swrm->write) {
2169 dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
2170 __func__);
2171 ret = -EINVAL;
2172 goto err_pdata_fail;
2173 }
2174 swrm->bulk_write = pdata->bulk_write;
2175 if (!swrm->bulk_write) {
2176 dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
2177 __func__);
2178 ret = -EINVAL;
2179 goto err_pdata_fail;
2180 }
2181 } else {
2182 swrm->swrm_dig_base = devm_ioremap(&pdev->dev,
2183 swrm->swrm_base_reg, SWRM_MAX_REGISTER);
2184 }
2185
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07002186 swrm->core_vote = pdata->core_vote;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302187 swrm->clk = pdata->clk;
2188 if (!swrm->clk) {
2189 dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
2190 __func__);
2191 ret = -EINVAL;
2192 goto err_pdata_fail;
2193 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302194 if (of_property_read_u32(pdev->dev.of_node,
2195 "qcom,swr-clock-stop-mode0",
2196 &swrm->clk_stop_mode0_supp)) {
2197 swrm->clk_stop_mode0_supp = FALSE;
2198 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302199
2200 ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
2201 &swrm->num_dev);
2202 if (ret) {
2203 dev_dbg(&pdev->dev, "%s: Looking up %s property failed\n",
2204 __func__, "qcom,swr-num-dev");
2205 } else {
2206 if (swrm->num_dev > SWR_MAX_SLAVE_DEVICES) {
2207 dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
2208 __func__, swrm->num_dev, SWR_MAX_SLAVE_DEVICES);
2209 ret = -EINVAL;
2210 goto err_pdata_fail;
2211 }
2212 }
2213
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302214 /* Parse soundwire port mapping */
2215 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
2216 &num_ports);
2217 if (ret) {
2218 dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
2219 goto err_pdata_fail;
2220 }
2221 swrm->num_ports = num_ports;
2222
2223 if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
2224 &map_size)) {
2225 dev_err(swrm->dev, "missing port mapping\n");
2226 goto err_pdata_fail;
2227 }
2228
2229 map_length = map_size / (3 * sizeof(u32));
2230 if (num_ports > SWR_MSTR_PORT_LEN) {
2231 dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
2232 __func__);
2233 ret = -EINVAL;
2234 goto err_pdata_fail;
2235 }
2236 temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
2237
2238 if (!temp) {
2239 ret = -ENOMEM;
2240 goto err_pdata_fail;
2241 }
2242 ret = of_property_read_u32_array(pdev->dev.of_node,
2243 "qcom,swr-port-mapping", temp, 3 * map_length);
2244 if (ret) {
2245 dev_err(swrm->dev, "%s: Failed to read port mapping\n",
2246 __func__);
2247 goto err_pdata_fail;
2248 }
2249
2250 for (i = 0; i < map_length; i++) {
2251 port_num = temp[3 * i];
2252 port_type = temp[3 * i + 1];
2253 ch_mask = temp[3 * i + 2];
2254
2255 if (port_num != old_port_num)
2256 ch_iter = 0;
2257 swrm->port_mapping[port_num][ch_iter].port_type = port_type;
2258 swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
2259 old_port_num = port_num;
2260 }
2261 devm_kfree(&pdev->dev, temp);
2262
2263 swrm->reg_irq = pdata->reg_irq;
2264 swrm->master.read = swrm_read;
2265 swrm->master.write = swrm_write;
2266 swrm->master.bulk_write = swrm_bulk_write;
2267 swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
2268 swrm->master.connect_port = swrm_connect_port;
2269 swrm->master.disconnect_port = swrm_disconnect_port;
2270 swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
2271 swrm->master.remove_from_group = swrm_remove_from_group;
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302272 swrm->master.device_wakeup_vote = swrm_device_wakeup_vote;
2273 swrm->master.device_wakeup_unvote = swrm_device_wakeup_unvote;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302274 swrm->master.dev.parent = &pdev->dev;
2275 swrm->master.dev.of_node = pdev->dev.of_node;
2276 swrm->master.num_port = 0;
2277 swrm->rcmd_id = 0;
2278 swrm->wcmd_id = 0;
2279 swrm->slave_status = 0;
2280 swrm->num_rx_chs = 0;
2281 swrm->clk_ref_count = 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302282 swrm->swr_irq_wakeup_capable = 0;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302283 swrm->mclk_freq = MCLK_FREQ;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302284 swrm->dev_up = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302285 swrm->state = SWR_MSTR_UP;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302286 swrm->ipc_wakeup = false;
2287 swrm->ipc_wakeup_triggered = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302288 init_completion(&swrm->reset);
2289 init_completion(&swrm->broadcast);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302290 init_completion(&swrm->clk_off_complete);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302291 mutex_init(&swrm->mlock);
2292 mutex_init(&swrm->reslock);
2293 mutex_init(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302294 mutex_init(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302295 mutex_init(&swrm->clklock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302296 mutex_init(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302297 mutex_init(&swrm->pm_lock);
2298 swrm->wlock_holders = 0;
2299 swrm->pm_state = SWRM_PM_SLEEPABLE;
2300 init_waitqueue_head(&swrm->pm_wq);
2301 pm_qos_add_request(&swrm->pm_qos_req,
2302 PM_QOS_CPU_DMA_LATENCY,
2303 PM_QOS_DEFAULT_VALUE);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302304
2305 for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++)
2306 INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
2307
Sudheer Papothi06f43412019-07-09 03:32:54 +05302308 /* Register LPASS core hw vote */
2309 lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
2310 if (IS_ERR(lpass_core_hw_vote)) {
2311 ret = PTR_ERR(lpass_core_hw_vote);
2312 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2313 __func__, "lpass_core_hw_vote", ret);
2314 lpass_core_hw_vote = NULL;
2315 ret = 0;
2316 }
2317 swrm->lpass_core_hw_vote = lpass_core_hw_vote;
2318
2319 /* Register LPASS audio core vote */
2320 lpass_core_audio = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
2321 if (IS_ERR(lpass_core_audio)) {
2322 ret = PTR_ERR(lpass_core_audio);
2323 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2324 __func__, "lpass_core_audio", ret);
2325 lpass_core_audio = NULL;
2326 ret = 0;
2327 }
2328 swrm->lpass_core_audio = lpass_core_audio;
2329
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302330 if (swrm->reg_irq) {
2331 ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
2332 SWR_IRQ_REGISTER);
2333 if (ret) {
2334 dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
2335 __func__, ret);
2336 goto err_irq_fail;
2337 }
2338 } else {
2339 swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
2340 if (swrm->irq < 0) {
2341 dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
2342 __func__, swrm->irq);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302343 goto err_irq_fail;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302344 }
2345
2346 ret = request_threaded_irq(swrm->irq, NULL,
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302347 swr_mstr_interrupt_v2,
Ramprasad Katkam83303512018-10-11 17:34:22 +05302348 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302349 "swr_master_irq", swrm);
2350 if (ret) {
2351 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2352 __func__, ret);
2353 goto err_irq_fail;
2354 }
2355
2356 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302357 /* Make inband tx interrupts as wakeup capable for slave irq */
2358 ret = of_property_read_u32(pdev->dev.of_node,
2359 "qcom,swr-mstr-irq-wakeup-capable",
2360 &swrm->swr_irq_wakeup_capable);
2361 if (ret)
2362 dev_dbg(swrm->dev, "%s: swrm irq wakeup capable not defined\n",
2363 __func__);
2364 if (swrm->swr_irq_wakeup_capable)
2365 irq_set_irq_wake(swrm->irq, 1);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302366 ret = swr_register_master(&swrm->master);
2367 if (ret) {
2368 dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
2369 goto err_mstr_fail;
2370 }
2371
2372 /* Add devices registered with board-info as the
2373 * controller will be up now
2374 */
2375 swr_master_add_boarddevices(&swrm->master);
2376 mutex_lock(&swrm->mlock);
2377 swrm_clk_request(swrm, true);
2378 ret = swrm_master_init(swrm);
2379 if (ret < 0) {
2380 dev_err(&pdev->dev,
2381 "%s: Error in master Initialization , err %d\n",
2382 __func__, ret);
2383 mutex_unlock(&swrm->mlock);
2384 goto err_mstr_fail;
2385 }
2386 swrm->version = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
2387
2388 mutex_unlock(&swrm->mlock);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302389 INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302390
2391 if (pdev->dev.of_node)
2392 of_register_swr_devices(&swrm->master);
2393
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302394#ifdef CONFIG_DEBUG_FS
2395 swrm->debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
2396 if (!IS_ERR(swrm->debugfs_swrm_dent)) {
2397 swrm->debugfs_peek = debugfs_create_file("swrm_peek",
2398 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2399 (void *) swrm, &swrm_debug_read_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302400
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302401 swrm->debugfs_poke = debugfs_create_file("swrm_poke",
2402 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2403 (void *) swrm, &swrm_debug_write_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302404
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302405 swrm->debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
2406 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2407 (void *) swrm,
2408 &swrm_debug_dump_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302409 }
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302410#endif
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302411 ret = device_init_wakeup(swrm->dev, true);
2412 if (ret) {
2413 dev_err(swrm->dev, "Device wakeup init failed: %d\n", ret);
2414 goto err_irq_wakeup_fail;
2415 }
2416
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302417 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
2418 pm_runtime_use_autosuspend(&pdev->dev);
2419 pm_runtime_set_active(&pdev->dev);
2420 pm_runtime_enable(&pdev->dev);
2421 pm_runtime_mark_last_busy(&pdev->dev);
2422
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302423 INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn);
2424 swrm->event_notifier.notifier_call = swrm_event_notify;
2425 msm_aud_evt_register_client(&swrm->event_notifier);
2426
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302427 return 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302428err_irq_wakeup_fail:
2429 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302430err_mstr_fail:
2431 if (swrm->reg_irq)
2432 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2433 swrm, SWR_IRQ_FREE);
2434 else if (swrm->irq)
2435 free_irq(swrm->irq, swrm);
2436err_irq_fail:
2437 mutex_destroy(&swrm->mlock);
2438 mutex_destroy(&swrm->reslock);
2439 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302440 mutex_destroy(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302441 mutex_destroy(&swrm->clklock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302442 mutex_destroy(&swrm->pm_lock);
2443 pm_qos_remove_request(&swrm->pm_qos_req);
2444
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302445err_pdata_fail:
2446err_memory_fail:
2447 return ret;
2448}
2449
2450static int swrm_remove(struct platform_device *pdev)
2451{
2452 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2453
2454 if (swrm->reg_irq)
2455 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2456 swrm, SWR_IRQ_FREE);
2457 else if (swrm->irq)
2458 free_irq(swrm->irq, swrm);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302459 else if (swrm->wake_irq > 0)
2460 free_irq(swrm->wake_irq, swrm);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302461 if (swrm->swr_irq_wakeup_capable)
2462 irq_set_irq_wake(swrm->irq, 0);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302463 cancel_work_sync(&swrm->wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302464 pm_runtime_disable(&pdev->dev);
2465 pm_runtime_set_suspended(&pdev->dev);
2466 swr_unregister_master(&swrm->master);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302467 msm_aud_evt_unregister_client(&swrm->event_notifier);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302468 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302469 mutex_destroy(&swrm->mlock);
2470 mutex_destroy(&swrm->reslock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302471 mutex_destroy(&swrm->iolock);
2472 mutex_destroy(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302473 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302474 mutex_destroy(&swrm->pm_lock);
2475 pm_qos_remove_request(&swrm->pm_qos_req);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302476 devm_kfree(&pdev->dev, swrm);
2477 return 0;
2478}
2479
2480static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
2481{
2482 u32 val;
2483
2484 dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
2485 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD);
2486 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2487 val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK;
2488 swr_master_write(swrm, SWRM_MCP_CFG_ADDR, val);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302489
2490 return 0;
2491}
2492
2493#ifdef CONFIG_PM
2494static int swrm_runtime_resume(struct device *dev)
2495{
2496 struct platform_device *pdev = to_platform_device(dev);
2497 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2498 int ret = 0;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302499 bool hw_core_err = false;
2500 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302501 struct swr_master *mstr = &swrm->master;
2502 struct swr_device *swr_dev;
2503
2504 dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
2505 __func__, swrm->state);
2506 mutex_lock(&swrm->reslock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302507
Sudheer Papothi384addd2019-06-14 02:26:52 +05302508 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2509 dev_err(dev, "%s:lpass core hw enable failed\n",
2510 __func__);
2511 hw_core_err = true;
2512 }
2513 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2514 dev_err(dev, "%s:lpass audio hw enable failed\n",
2515 __func__);
2516 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002517 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302518
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302519 if ((swrm->state == SWR_MSTR_DOWN) ||
2520 (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) {
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302521 if (swrm->clk_stop_mode0_supp) {
2522 if (swrm->ipc_wakeup)
2523 msm_aud_evt_blocking_notifier_call_chain(
2524 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302525 }
2526
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302527 if (swrm_clk_request(swrm, true)) {
2528 /*
2529 * Set autosuspend timer to 1 for
2530 * master to enter into suspend.
2531 */
2532 auto_suspend_timer = 1;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302533 goto exit;
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302534 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302535 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302536 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2537 ret = swr_device_up(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302538 if (ret == -ENODEV) {
2539 dev_dbg(dev,
2540 "%s slave device up not implemented\n",
2541 __func__);
2542 ret = 0;
2543 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302544 dev_err(dev,
2545 "%s: failed to wakeup swr dev %d\n",
2546 __func__, swr_dev->dev_num);
2547 swrm_clk_request(swrm, false);
2548 goto exit;
2549 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302550 }
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05302551 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2552 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2553 swrm_master_init(swrm);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302554 /* wait for hw enumeration to complete */
2555 usleep_range(100, 105);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302556 swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, 0x0,
2557 SWRS_SCP_INT_STATUS_MASK_1);
Karthikeyan Manif6821902019-05-21 17:31:24 -07002558 if (swrm->state == SWR_MSTR_SSR) {
2559 mutex_unlock(&swrm->reslock);
2560 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
2561 mutex_lock(&swrm->reslock);
2562 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302563 } else {
2564 /*wake up from clock stop*/
2565 swr_master_write(swrm, SWRM_MCP_BUS_CTRL_ADDR, 0x2);
2566 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302567 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302568 swrm->state = SWR_MSTR_UP;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302569 }
2570exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302571 if (!aud_core_err)
2572 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2573 if (!hw_core_err)
2574 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302575 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302576 auto_suspend_timer = SWR_AUTO_SUSPEND_DELAY * 1000;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302577 mutex_unlock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302578
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302579 return ret;
2580}
2581
2582static int swrm_runtime_suspend(struct device *dev)
2583{
2584 struct platform_device *pdev = to_platform_device(dev);
2585 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2586 int ret = 0;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302587 bool hw_core_err = false;
2588 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302589 struct swr_master *mstr = &swrm->master;
2590 struct swr_device *swr_dev;
2591 int current_state = 0;
2592
2593 dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
2594 __func__, swrm->state);
2595 mutex_lock(&swrm->reslock);
2596 mutex_lock(&swrm->force_down_lock);
2597 current_state = swrm->state;
2598 mutex_unlock(&swrm->force_down_lock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302599
2600 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2601 dev_err(dev, "%s:lpass core hw enable failed\n",
2602 __func__);
2603 hw_core_err = true;
2604 }
2605 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2606 dev_err(dev, "%s:lpass audio hw enable failed\n",
2607 __func__);
2608 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002609 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302610
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302611 if ((current_state == SWR_MSTR_UP) ||
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302612 (current_state == SWR_MSTR_SSR)) {
2613
2614 if ((current_state != SWR_MSTR_SSR) &&
2615 swrm_is_port_en(&swrm->master)) {
2616 dev_dbg(dev, "%s ports are enabled\n", __func__);
2617 ret = -EBUSY;
2618 goto exit;
2619 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302620 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05302621 mutex_unlock(&swrm->reslock);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +05302622 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
Sudheer Papothi06f43412019-07-09 03:32:54 +05302623 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302624 swrm_clk_pause(swrm);
2625 swr_master_write(swrm, SWRM_COMP_CFG_ADDR, 0x00);
2626 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2627 ret = swr_device_down(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302628 if (ret == -ENODEV) {
2629 dev_dbg_ratelimited(dev,
2630 "%s slave device down not implemented\n",
2631 __func__);
2632 ret = 0;
2633 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302634 dev_err(dev,
2635 "%s: failed to shutdown swr dev %d\n",
2636 __func__, swr_dev->dev_num);
2637 goto exit;
2638 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302639 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302640 } else {
Sudheer Papothi384addd2019-06-14 02:26:52 +05302641 mutex_unlock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302642 /* clock stop sequence */
2643 swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
2644 SWRS_SCP_CONTROL);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302645 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302646 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302647 }
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07002648 ret = swrm_clk_request(swrm, false);
2649 if (ret) {
2650 dev_err(dev, "%s: swrmn clk failed\n", __func__);
2651 ret = 0;
2652 goto exit;
2653 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302654
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302655 if (swrm->clk_stop_mode0_supp) {
2656 if (swrm->wake_irq > 0) {
2657 enable_irq(swrm->wake_irq);
2658 } else if (swrm->ipc_wakeup) {
2659 msm_aud_evt_blocking_notifier_call_chain(
2660 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
2661 swrm->ipc_wakeup_triggered = false;
2662 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302663 }
2664
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302665 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302666 /* Retain SSR state until resume */
2667 if (current_state != SWR_MSTR_SSR)
2668 swrm->state = SWR_MSTR_DOWN;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302669exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302670 if (!aud_core_err)
2671 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2672 if (!hw_core_err)
2673 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302674 mutex_unlock(&swrm->reslock);
2675 return ret;
2676}
2677#endif /* CONFIG_PM */
2678
Sudheer Papothi06f43412019-07-09 03:32:54 +05302679static int swrm_device_suspend(struct device *dev)
2680{
2681 struct platform_device *pdev = to_platform_device(dev);
2682 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2683 int ret = 0;
2684
2685 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2686 if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
2687 ret = swrm_runtime_suspend(dev);
2688 if (!ret) {
2689 pm_runtime_disable(dev);
2690 pm_runtime_set_suspended(dev);
2691 pm_runtime_enable(dev);
2692 }
2693 }
2694
2695 return 0;
2696}
2697
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302698static int swrm_device_down(struct device *dev)
2699{
2700 struct platform_device *pdev = to_platform_device(dev);
2701 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302702
2703 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2704
2705 mutex_lock(&swrm->force_down_lock);
2706 swrm->state = SWR_MSTR_SSR;
2707 mutex_unlock(&swrm->force_down_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302708
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302709 swrm_device_suspend(dev);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302710 return 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302711}
2712
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302713int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm)
2714{
2715 int ret = 0;
Laxminath Kasama60239e2019-01-10 14:43:03 +05302716 int irq, dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302717
2718 if (!swrm->ipc_wakeup) {
Laxminath Kasama60239e2019-01-10 14:43:03 +05302719 irq = of_get_named_gpio(swrm->dev->of_node,
2720 "qcom,swr-wakeup-irq", 0);
2721 if (gpio_is_valid(irq)) {
2722 swrm->wake_irq = gpio_to_irq(irq);
2723 if (swrm->wake_irq < 0) {
2724 dev_err(swrm->dev,
2725 "Unable to configure irq\n");
2726 return swrm->wake_irq;
2727 }
2728 } else {
2729 dir_apps_irq = platform_get_irq_byname(swrm->pdev,
2730 "swr_wake_irq");
2731 if (dir_apps_irq < 0) {
2732 dev_err(swrm->dev,
2733 "TLMM connect gpio not found\n");
2734 return -EINVAL;
2735 }
2736 swrm->wake_irq = dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302737 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302738 ret = request_threaded_irq(swrm->wake_irq, NULL,
2739 swrm_wakeup_interrupt,
2740 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
2741 "swr_wake_irq", swrm);
2742 if (ret) {
2743 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2744 __func__, ret);
2745 return -EINVAL;
2746 }
Aditya Bavanari3517b112018-12-03 13:26:59 +05302747 irq_set_irq_wake(swrm->wake_irq, 1);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302748 }
2749 return ret;
2750}
2751
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302752static int swrm_alloc_port_mem(struct device *dev, struct swr_mstr_ctrl *swrm,
2753 u32 uc, u32 size)
2754{
2755 if (!swrm->port_param) {
2756 swrm->port_param = devm_kzalloc(dev,
2757 sizeof(swrm->port_param) * SWR_UC_MAX,
2758 GFP_KERNEL);
2759 if (!swrm->port_param)
2760 return -ENOMEM;
2761 }
2762 if (!swrm->port_param[uc]) {
2763 swrm->port_param[uc] = devm_kcalloc(dev, size,
2764 sizeof(struct port_params),
2765 GFP_KERNEL);
2766 if (!swrm->port_param[uc])
2767 return -ENOMEM;
2768 } else {
2769 dev_err_ratelimited(swrm->dev, "%s: called more than once\n",
2770 __func__);
2771 }
2772
2773 return 0;
2774}
2775
2776static int swrm_copy_port_config(struct swr_mstr_ctrl *swrm,
2777 struct swrm_port_config *port_cfg,
2778 u32 size)
2779{
2780 int idx;
2781 struct port_params *params;
2782 int uc = port_cfg->uc;
2783 int ret = 0;
2784
2785 for (idx = 0; idx < size; idx++) {
2786 params = &((struct port_params *)port_cfg->params)[idx];
2787 if (!params) {
2788 dev_err(swrm->dev, "%s: Invalid params\n", __func__);
2789 ret = -EINVAL;
2790 break;
2791 }
2792 memcpy(&swrm->port_param[uc][idx], params,
2793 sizeof(struct port_params));
2794 }
2795
2796 return ret;
2797}
2798
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302799/**
2800 * swrm_wcd_notify - parent device can notify to soundwire master through
2801 * this function
2802 * @pdev: pointer to platform device structure
2803 * @id: command id from parent to the soundwire master
2804 * @data: data from parent device to soundwire master
2805 */
2806int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
2807{
2808 struct swr_mstr_ctrl *swrm;
2809 int ret = 0;
2810 struct swr_master *mstr;
2811 struct swr_device *swr_dev;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302812 struct swrm_port_config *port_cfg;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302813
2814 if (!pdev) {
2815 pr_err("%s: pdev is NULL\n", __func__);
2816 return -EINVAL;
2817 }
2818 swrm = platform_get_drvdata(pdev);
2819 if (!swrm) {
2820 dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__);
2821 return -EINVAL;
2822 }
2823 mstr = &swrm->master;
2824
2825 switch (id) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05302826 case SWR_REQ_CLK_SWITCH:
2827 /* This will put soundwire in clock stop mode and disable the
2828 * clocks, if there is no active usecase running, so that the
2829 * next activity on soundwire will request clock from new clock
2830 * source.
2831 */
2832 mutex_lock(&swrm->mlock);
2833 if (swrm->state == SWR_MSTR_UP)
2834 swrm_device_suspend(&pdev->dev);
2835 mutex_unlock(&swrm->mlock);
2836 break;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302837 case SWR_CLK_FREQ:
2838 if (!data) {
2839 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
2840 ret = -EINVAL;
2841 } else {
2842 mutex_lock(&swrm->mlock);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302843 if (swrm->mclk_freq != *(int *)data) {
2844 dev_dbg(swrm->dev, "%s: freq change: force mstr down\n", __func__);
2845 if (swrm->state == SWR_MSTR_DOWN)
2846 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
2847 __func__, swrm->state);
2848 else
2849 swrm_device_suspend(&pdev->dev);
Prasad Kumpatla386df4e2019-10-11 18:23:16 +05302850 /*
2851 * add delay to ensure clk release happen
2852 * if interrupt triggered for clk stop,
2853 * wait for it to exit
2854 */
2855 usleep_range(10000, 10500);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302856 }
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302857 swrm->mclk_freq = *(int *)data;
2858 mutex_unlock(&swrm->mlock);
2859 }
2860 break;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302861 case SWR_DEVICE_SSR_DOWN:
2862 mutex_lock(&swrm->devlock);
2863 swrm->dev_up = false;
2864 mutex_unlock(&swrm->devlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302865 mutex_lock(&swrm->reslock);
2866 swrm->state = SWR_MSTR_SSR;
2867 mutex_unlock(&swrm->reslock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302868 break;
2869 case SWR_DEVICE_SSR_UP:
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302870 /* wait for clk voting to be zero */
Ramprasad Katkam7f6462e2018-11-06 11:51:22 +05302871 reinit_completion(&swrm->clk_off_complete);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302872 if (swrm->clk_ref_count &&
2873 !wait_for_completion_timeout(&swrm->clk_off_complete,
Ramprasad Katkamc87efeb2018-12-12 19:26:19 +05302874 msecs_to_jiffies(500)))
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302875 dev_err(swrm->dev, "%s: clock voting not zero\n",
2876 __func__);
2877
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302878 mutex_lock(&swrm->devlock);
2879 swrm->dev_up = true;
2880 mutex_unlock(&swrm->devlock);
2881 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302882 case SWR_DEVICE_DOWN:
2883 dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
2884 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302885 if (swrm->state == SWR_MSTR_DOWN)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302886 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
2887 __func__, swrm->state);
2888 else
2889 swrm_device_down(&pdev->dev);
2890 mutex_unlock(&swrm->mlock);
2891 break;
2892 case SWR_DEVICE_UP:
2893 dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
Ramprasad Katkam0fed92f2018-11-08 14:22:22 +05302894 mutex_lock(&swrm->devlock);
2895 if (!swrm->dev_up) {
2896 dev_dbg(swrm->dev, "SSR not complete yet\n");
2897 mutex_unlock(&swrm->devlock);
2898 return -EBUSY;
2899 }
2900 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302901 mutex_lock(&swrm->mlock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302902 pm_runtime_mark_last_busy(&pdev->dev);
2903 pm_runtime_get_sync(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302904 mutex_lock(&swrm->reslock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302905 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2906 ret = swr_reset_device(swr_dev);
2907 if (ret) {
2908 dev_err(swrm->dev,
2909 "%s: failed to reset swr device %d\n",
2910 __func__, swr_dev->dev_num);
2911 swrm_clk_request(swrm, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302912 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302913 }
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302914 pm_runtime_mark_last_busy(&pdev->dev);
2915 pm_runtime_put_autosuspend(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302916 mutex_unlock(&swrm->reslock);
2917 mutex_unlock(&swrm->mlock);
2918 break;
2919 case SWR_SET_NUM_RX_CH:
2920 if (!data) {
2921 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
2922 ret = -EINVAL;
2923 } else {
2924 mutex_lock(&swrm->mlock);
2925 swrm->num_rx_chs = *(int *)data;
2926 if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
2927 list_for_each_entry(swr_dev, &mstr->devices,
2928 dev_list) {
2929 ret = swr_set_device_group(swr_dev,
2930 SWR_BROADCAST);
2931 if (ret)
2932 dev_err(swrm->dev,
2933 "%s: set num ch failed\n",
2934 __func__);
2935 }
2936 } else {
2937 list_for_each_entry(swr_dev, &mstr->devices,
2938 dev_list) {
2939 ret = swr_set_device_group(swr_dev,
2940 SWR_GROUP_NONE);
2941 if (ret)
2942 dev_err(swrm->dev,
2943 "%s: set num ch failed\n",
2944 __func__);
2945 }
2946 }
2947 mutex_unlock(&swrm->mlock);
2948 }
2949 break;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302950 case SWR_REGISTER_WAKE_IRQ:
2951 if (!data) {
2952 dev_err(swrm->dev, "%s: reg wake irq data is NULL\n",
2953 __func__);
2954 ret = -EINVAL;
2955 } else {
2956 mutex_lock(&swrm->mlock);
2957 swrm->ipc_wakeup = *(u32 *)data;
2958 ret = swrm_register_wake_irq(swrm);
2959 if (ret)
2960 dev_err(swrm->dev, "%s: register wake_irq failed\n",
2961 __func__);
2962 mutex_unlock(&swrm->mlock);
2963 }
2964 break;
Sudheer Papothi72ee2642019-08-08 05:15:17 +05302965 case SWR_REGISTER_WAKEUP:
2966 msm_aud_evt_blocking_notifier_call_chain(
2967 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
2968 break;
2969 case SWR_DEREGISTER_WAKEUP:
2970 msm_aud_evt_blocking_notifier_call_chain(
2971 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
2972 break;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302973 case SWR_SET_PORT_MAP:
2974 if (!data) {
2975 dev_err(swrm->dev, "%s: data is NULL for id=%d\n",
2976 __func__, id);
2977 ret = -EINVAL;
2978 } else {
2979 mutex_lock(&swrm->mlock);
2980 port_cfg = (struct swrm_port_config *)data;
2981 if (!port_cfg->size) {
2982 ret = -EINVAL;
2983 goto done;
2984 }
2985 ret = swrm_alloc_port_mem(&pdev->dev, swrm,
2986 port_cfg->uc, port_cfg->size);
2987 if (!ret)
2988 swrm_copy_port_config(swrm, port_cfg,
2989 port_cfg->size);
2990done:
2991 mutex_unlock(&swrm->mlock);
2992 }
2993 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302994 default:
2995 dev_err(swrm->dev, "%s: swr master unknown id %d\n",
2996 __func__, id);
2997 break;
2998 }
2999 return ret;
3000}
3001EXPORT_SYMBOL(swrm_wcd_notify);
3002
Ramprasad Katkam57349872018-11-11 18:34:57 +05303003/*
3004 * swrm_pm_cmpxchg:
3005 * Check old state and exchange with pm new state
3006 * if old state matches with current state
3007 *
3008 * @swrm: pointer to wcd core resource
3009 * @o: pm old state
3010 * @n: pm new state
3011 *
3012 * Returns old state
3013 */
3014static enum swrm_pm_state swrm_pm_cmpxchg(
3015 struct swr_mstr_ctrl *swrm,
3016 enum swrm_pm_state o,
3017 enum swrm_pm_state n)
3018{
3019 enum swrm_pm_state old;
3020
3021 if (!swrm)
3022 return o;
3023
3024 mutex_lock(&swrm->pm_lock);
3025 old = swrm->pm_state;
3026 if (old == o)
3027 swrm->pm_state = n;
3028 mutex_unlock(&swrm->pm_lock);
3029
3030 return old;
3031}
3032
3033static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm)
3034{
3035 enum swrm_pm_state os;
3036
3037 /*
3038 * swrm_{lock/unlock}_sleep will be called by swr irq handler
3039 * and slave wake up requests..
3040 *
3041 * If system didn't resume, we can simply return false so
3042 * IRQ handler can return without handling IRQ.
3043 */
3044 mutex_lock(&swrm->pm_lock);
3045 if (swrm->wlock_holders++ == 0) {
3046 dev_dbg(swrm->dev, "%s: holding wake lock\n", __func__);
3047 pm_qos_update_request(&swrm->pm_qos_req,
3048 msm_cpuidle_get_deep_idle_latency());
3049 pm_stay_awake(swrm->dev);
3050 }
3051 mutex_unlock(&swrm->pm_lock);
3052
3053 if (!wait_event_timeout(swrm->pm_wq,
3054 ((os = swrm_pm_cmpxchg(swrm,
3055 SWRM_PM_SLEEPABLE,
3056 SWRM_PM_AWAKE)) ==
3057 SWRM_PM_SLEEPABLE ||
3058 (os == SWRM_PM_AWAKE)),
3059 msecs_to_jiffies(
3060 SWRM_SYSTEM_RESUME_TIMEOUT_MS))) {
3061 dev_err(swrm->dev, "%s: system didn't resume within %dms, s %d, w %d\n",
3062 __func__, SWRM_SYSTEM_RESUME_TIMEOUT_MS, swrm->pm_state,
3063 swrm->wlock_holders);
3064 swrm_unlock_sleep(swrm);
3065 return false;
3066 }
3067 wake_up_all(&swrm->pm_wq);
3068 return true;
3069}
3070
3071static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm)
3072{
3073 mutex_lock(&swrm->pm_lock);
3074 if (--swrm->wlock_holders == 0) {
3075 dev_dbg(swrm->dev, "%s: releasing wake lock pm_state %d -> %d\n",
3076 __func__, swrm->pm_state, SWRM_PM_SLEEPABLE);
3077 /*
3078 * if swrm_lock_sleep failed, pm_state would be still
3079 * swrm_PM_ASLEEP, don't overwrite
3080 */
3081 if (likely(swrm->pm_state == SWRM_PM_AWAKE))
3082 swrm->pm_state = SWRM_PM_SLEEPABLE;
3083 pm_qos_update_request(&swrm->pm_qos_req,
3084 PM_QOS_DEFAULT_VALUE);
3085 pm_relax(swrm->dev);
3086 }
3087 mutex_unlock(&swrm->pm_lock);
3088 wake_up_all(&swrm->pm_wq);
3089}
3090
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303091#ifdef CONFIG_PM_SLEEP
3092static int swrm_suspend(struct device *dev)
3093{
3094 int ret = -EBUSY;
3095 struct platform_device *pdev = to_platform_device(dev);
3096 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3097
3098 dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
Ramprasad Katkam57349872018-11-11 18:34:57 +05303099
3100 mutex_lock(&swrm->pm_lock);
3101
3102 if (swrm->pm_state == SWRM_PM_SLEEPABLE) {
3103 dev_dbg(swrm->dev, "%s: suspending system, state %d, wlock %d\n",
3104 __func__, swrm->pm_state,
3105 swrm->wlock_holders);
3106 swrm->pm_state = SWRM_PM_ASLEEP;
3107 } else if (swrm->pm_state == SWRM_PM_AWAKE) {
3108 /*
3109 * unlock to wait for pm_state == SWRM_PM_SLEEPABLE
3110 * then set to SWRM_PM_ASLEEP
3111 */
3112 dev_dbg(swrm->dev, "%s: waiting to suspend system, state %d, wlock %d\n",
3113 __func__, swrm->pm_state,
3114 swrm->wlock_holders);
3115 mutex_unlock(&swrm->pm_lock);
3116 if (!(wait_event_timeout(swrm->pm_wq, swrm_pm_cmpxchg(
3117 swrm, SWRM_PM_SLEEPABLE,
3118 SWRM_PM_ASLEEP) ==
3119 SWRM_PM_SLEEPABLE,
3120 msecs_to_jiffies(
3121 SWRM_SYS_SUSPEND_WAIT)))) {
3122 dev_dbg(swrm->dev, "%s: suspend failed state %d, wlock %d\n",
3123 __func__, swrm->pm_state,
3124 swrm->wlock_holders);
3125 return -EBUSY;
3126 } else {
3127 dev_dbg(swrm->dev,
3128 "%s: done, state %d, wlock %d\n",
3129 __func__, swrm->pm_state,
3130 swrm->wlock_holders);
3131 }
3132 mutex_lock(&swrm->pm_lock);
3133 } else if (swrm->pm_state == SWRM_PM_ASLEEP) {
3134 dev_dbg(swrm->dev, "%s: system is already suspended, state %d, wlock %d\n",
3135 __func__, swrm->pm_state,
3136 swrm->wlock_holders);
3137 }
3138
3139 mutex_unlock(&swrm->pm_lock);
3140
3141 if ((!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev))) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303142 ret = swrm_runtime_suspend(dev);
3143 if (!ret) {
3144 /*
3145 * Synchronize runtime-pm and system-pm states:
3146 * At this point, we are already suspended. If
3147 * runtime-pm still thinks its active, then
3148 * make sure its status is in sync with HW
3149 * status. The three below calls let the
3150 * runtime-pm know that we are suspended
3151 * already without re-invoking the suspend
3152 * callback
3153 */
3154 pm_runtime_disable(dev);
3155 pm_runtime_set_suspended(dev);
3156 pm_runtime_enable(dev);
3157 }
3158 }
3159 if (ret == -EBUSY) {
3160 /*
3161 * There is a possibility that some audio stream is active
3162 * during suspend. We dont want to return suspend failure in
3163 * that case so that display and relevant components can still
3164 * go to suspend.
3165 * If there is some other error, then it should be passed-on
3166 * to system level suspend
3167 */
3168 ret = 0;
3169 }
3170 return ret;
3171}
3172
3173static int swrm_resume(struct device *dev)
3174{
3175 int ret = 0;
3176 struct platform_device *pdev = to_platform_device(dev);
3177 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3178
3179 dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
3180 if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
3181 ret = swrm_runtime_resume(dev);
3182 if (!ret) {
3183 pm_runtime_mark_last_busy(dev);
3184 pm_request_autosuspend(dev);
3185 }
3186 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05303187 mutex_lock(&swrm->pm_lock);
3188 if (swrm->pm_state == SWRM_PM_ASLEEP) {
3189 dev_dbg(swrm->dev,
3190 "%s: resuming system, state %d, wlock %d\n",
3191 __func__, swrm->pm_state,
3192 swrm->wlock_holders);
3193 swrm->pm_state = SWRM_PM_SLEEPABLE;
3194 } else {
3195 dev_dbg(swrm->dev, "%s: system is already awake, state %d wlock %d\n",
3196 __func__, swrm->pm_state,
3197 swrm->wlock_holders);
3198 }
3199 mutex_unlock(&swrm->pm_lock);
3200 wake_up_all(&swrm->pm_wq);
3201
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303202 return ret;
3203}
3204#endif /* CONFIG_PM_SLEEP */
3205
3206static const struct dev_pm_ops swrm_dev_pm_ops = {
3207 SET_SYSTEM_SLEEP_PM_OPS(
3208 swrm_suspend,
3209 swrm_resume
3210 )
3211 SET_RUNTIME_PM_OPS(
3212 swrm_runtime_suspend,
3213 swrm_runtime_resume,
3214 NULL
3215 )
3216};
3217
3218static const struct of_device_id swrm_dt_match[] = {
3219 {
3220 .compatible = "qcom,swr-mstr",
3221 },
3222 {}
3223};
3224
3225static struct platform_driver swr_mstr_driver = {
3226 .probe = swrm_probe,
3227 .remove = swrm_remove,
3228 .driver = {
3229 .name = SWR_WCD_NAME,
3230 .owner = THIS_MODULE,
3231 .pm = &swrm_dev_pm_ops,
3232 .of_match_table = swrm_dt_match,
Xiaojun Sang53cd13a2018-06-29 15:14:37 +08003233 .suppress_bind_attrs = true,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303234 },
3235};
3236
3237static int __init swrm_init(void)
3238{
3239 return platform_driver_register(&swr_mstr_driver);
3240}
3241module_init(swrm_init);
3242
3243static void __exit swrm_exit(void)
3244{
3245 platform_driver_unregister(&swr_mstr_driver);
3246}
3247module_exit(swrm_exit);
3248
3249MODULE_LICENSE("GPL v2");
3250MODULE_DESCRIPTION("SoundWire Master Controller");
3251MODULE_ALIAS("platform:swr-mstr");