blob: 512ef618f708415d13e54c1f6a1ff28db7ed586e [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/*
Sudheer Papothiae5c3632019-11-27 06:52:06 +05303 * Copyright (c) 2015-2020, 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>
Aditya Bavanaribb981b72020-03-19 18:30:10 +053025#include <dsp/digital-cdc-rsc-mgr.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053026#include "swrm_registers.h"
27#include "swr-mstr-ctrl.h"
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053028
Sudheer Papothiae5c3632019-11-27 06:52:06 +053029#define SWR_NUM_PORTS 4 /* TODO - Get this info from DT */
30
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +053031#define SWRM_FRAME_SYNC_SEL 4000 /* 4KHz */
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +053032#define SWRM_FRAME_SYNC_SEL_NATIVE 3675 /* 3.675KHz */
Ramprasad Katkam57349872018-11-11 18:34:57 +053033#define SWRM_SYSTEM_RESUME_TIMEOUT_MS 700
34#define SWRM_SYS_SUSPEND_WAIT 1
Sudheer Papothi3d1596e2018-10-27 06:19:18 +053035
Sudheer Papothi4c322b12018-10-31 06:34:01 +053036#define SWRM_DSD_PARAMS_PORT 4
37
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053038#define SWR_BROADCAST_CMD_ID 0x0F
Sudheer Papothi3590b312019-06-04 23:51:30 +053039#define SWR_AUTO_SUSPEND_DELAY 1 /* delay in sec */
Sudheer Papothi7c067e82018-11-15 06:53:35 +053040#define SWR_DEV_ID_MASK 0xFFFFFFFFFFFF
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053041#define SWR_REG_VAL_PACK(data, dev, id, reg) \
42 ((reg) | ((id) << 16) | ((dev) << 20) | ((data) << 24))
43
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +053044#define SWR_INVALID_PARAM 0xFF
Laxminath Kasam990c70b2018-11-09 23:15:09 +053045#define SWR_HSTOP_MAX_VAL 0xF
46#define SWR_HSTART_MIN_VAL 0x0
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +053047
Vatsal Buchae50b5002019-09-19 14:32:20 +053048#define ERR_AUTO_SUSPEND_TIMER_VAL 0x1
49
Ramprasad Katkam83303512018-10-11 17:34:22 +053050#define SWRM_INTERRUPT_STATUS_MASK 0x1FDFD
Laxminath Kasamec8c9092019-12-17 13:12:58 +053051#define SWRM_LINK_STATUS_RETRY_CNT 100
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +053052
53#define SWRM_ROW_48 48
54#define SWRM_ROW_50 50
55#define SWRM_ROW_64 64
56#define SWRM_COL_02 02
57#define SWRM_COL_16 16
58
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053059/* pm runtime auto suspend timer in msecs */
60static int auto_suspend_timer = SWR_AUTO_SUSPEND_DELAY * 1000;
61module_param(auto_suspend_timer, int, 0664);
62MODULE_PARM_DESC(auto_suspend_timer, "timer for auto suspend");
63
64enum {
65 SWR_NOT_PRESENT, /* Device is detached/not present on the bus */
66 SWR_ATTACHED_OK, /* Device is attached */
67 SWR_ALERT, /* Device alters master for any interrupts */
68 SWR_RESERVED, /* Reserved */
69};
70
71enum {
72 MASTER_ID_WSA = 1,
73 MASTER_ID_RX,
74 MASTER_ID_TX
75};
Ramprasad Katkamcab8d722018-09-28 15:54:06 +053076
77enum {
78 ENABLE_PENDING,
79 DISABLE_PENDING
80};
Sudheer Papothi384addd2019-06-14 02:26:52 +053081
82enum {
83 LPASS_HW_CORE,
84 LPASS_AUDIO_CORE,
85};
86
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053087#define TRUE 1
88#define FALSE 0
89
Ramprasad Katkam1f221262018-08-23 15:01:22 +053090#define SWRM_MAX_PORT_REG 120
Ramprasad Katkam83303512018-10-11 17:34:22 +053091#define SWRM_MAX_INIT_REG 11
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053092
Laxminath Kasamfbcaf322018-07-18 00:38:14 +053093#define MAX_FIFO_RD_FAIL_RETRY 3
94
Ramprasad Katkam57349872018-11-11 18:34:57 +053095static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm);
96static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm);
Sudheer Papothi96c842a2019-08-29 12:11:21 +053097static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr);
98static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val);
Vatsal Buchabd68a7c2020-06-08 10:41:56 +053099static int swrm_runtime_resume(struct device *dev);
Sudheer Papothiae5c3632019-11-27 06:52:06 +0530100
101static u8 swrm_get_clk_div(int mclk_freq, int bus_clk_freq)
102{
103 int clk_div = 0;
104 u8 div_val = 0;
105
106 if (!mclk_freq || !bus_clk_freq)
107 return 0;
108
109 clk_div = (mclk_freq / bus_clk_freq);
110
111 switch (clk_div) {
112 case 32:
113 div_val = 5;
114 break;
115 case 16:
116 div_val = 4;
117 break;
118 case 8:
119 div_val = 3;
120 break;
121 case 4:
122 div_val = 2;
123 break;
124 case 2:
125 div_val = 1;
126 break;
127 case 1:
128 default:
129 div_val = 0;
130 break;
131 }
132
133 return div_val;
134}
135
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530136static bool swrm_is_msm_variant(int val)
137{
138 return (val == SWRM_VERSION_1_3);
139}
140
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530141#ifdef CONFIG_DEBUG_FS
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530142static int swrm_debug_open(struct inode *inode, struct file *file)
143{
144 file->private_data = inode->i_private;
145 return 0;
146}
147
148static int get_parameters(char *buf, u32 *param1, int num_of_par)
149{
150 char *token;
151 int base, cnt;
152
153 token = strsep(&buf, " ");
154 for (cnt = 0; cnt < num_of_par; cnt++) {
155 if (token) {
156 if ((token[1] == 'x') || (token[1] == 'X'))
157 base = 16;
158 else
159 base = 10;
160
161 if (kstrtou32(token, base, &param1[cnt]) != 0)
162 return -EINVAL;
163
164 token = strsep(&buf, " ");
165 } else
166 return -EINVAL;
167 }
168 return 0;
169}
170
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530171static ssize_t swrm_reg_show(struct swr_mstr_ctrl *swrm, char __user *ubuf,
172 size_t count, loff_t *ppos)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530173{
174 int i, reg_val, len;
175 ssize_t total = 0;
176 char tmp_buf[SWR_MSTR_MAX_BUF_LEN];
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530177 int rem = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530178
179 if (!ubuf || !ppos)
180 return 0;
181
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530182 i = ((int) *ppos + SWR_MSTR_START_REG_ADDR);
183 rem = i%4;
184
185 if (rem)
186 i = (i - rem);
187
188 for (; i <= SWR_MSTR_MAX_REG_ADDR; i += 4) {
189 usleep_range(100, 150);
190 reg_val = swr_master_read(swrm, i);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530191 len = snprintf(tmp_buf, 25, "0x%.3x: 0x%.2x\n", i, reg_val);
Aditya Bavanari9f599b42019-08-27 22:18:41 +0530192 if (len < 0) {
193 pr_err("%s: fail to fill the buffer\n", __func__);
194 total = -EFAULT;
195 goto copy_err;
196 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530197 if ((total + len) >= count - 1)
198 break;
199 if (copy_to_user((ubuf + total), tmp_buf, len)) {
200 pr_err("%s: fail to copy reg dump\n", __func__);
201 total = -EFAULT;
202 goto copy_err;
203 }
204 *ppos += len;
205 total += len;
206 }
207
208copy_err:
209 return total;
210}
211
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530212static ssize_t swrm_debug_reg_dump(struct file *file, char __user *ubuf,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530213 size_t count, loff_t *ppos)
214{
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530215 struct swr_mstr_ctrl *swrm;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530216
217 if (!count || !file || !ppos || !ubuf)
218 return -EINVAL;
219
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530220 swrm = file->private_data;
221 if (!swrm)
222 return -EINVAL;
223
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530224 if (*ppos < 0)
225 return -EINVAL;
226
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530227 return swrm_reg_show(swrm, ubuf, count, ppos);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530228}
229
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530230static ssize_t swrm_debug_read(struct file *file, char __user *ubuf,
231 size_t count, loff_t *ppos)
232{
233 char lbuf[SWR_MSTR_RD_BUF_LEN];
234 struct swr_mstr_ctrl *swrm = NULL;
235
236 if (!count || !file || !ppos || !ubuf)
237 return -EINVAL;
238
239 swrm = file->private_data;
240 if (!swrm)
241 return -EINVAL;
242
243 if (*ppos < 0)
244 return -EINVAL;
245
246 snprintf(lbuf, sizeof(lbuf), "0x%x\n", swrm->read_data);
247
248 return simple_read_from_buffer(ubuf, count, ppos, lbuf,
249 strnlen(lbuf, 7));
250}
251
252static ssize_t swrm_debug_peek_write(struct file *file, const char __user *ubuf,
253 size_t count, loff_t *ppos)
254{
255 char lbuf[SWR_MSTR_RD_BUF_LEN];
256 int rc;
257 u32 param[5];
258 struct swr_mstr_ctrl *swrm = NULL;
259
260 if (!count || !file || !ppos || !ubuf)
261 return -EINVAL;
262
263 swrm = file->private_data;
264 if (!swrm)
265 return -EINVAL;
266
267 if (*ppos < 0)
268 return -EINVAL;
269
270 if (count > sizeof(lbuf) - 1)
271 return -EINVAL;
272
273 rc = copy_from_user(lbuf, ubuf, count);
274 if (rc)
275 return -EFAULT;
276
277 lbuf[count] = '\0';
278 rc = get_parameters(lbuf, param, 1);
279 if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) && (rc == 0))
280 swrm->read_data = swr_master_read(swrm, param[0]);
281 else
282 rc = -EINVAL;
283
284 if (rc == 0)
285 rc = count;
286 else
287 dev_err(swrm->dev, "%s: rc = %d\n", __func__, rc);
288
289 return rc;
290}
291
292static ssize_t swrm_debug_write(struct file *file,
293 const char __user *ubuf, size_t count, loff_t *ppos)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530294{
295 char lbuf[SWR_MSTR_WR_BUF_LEN];
296 int rc;
297 u32 param[5];
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530298 struct swr_mstr_ctrl *swrm;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530299
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530300 if (!file || !ppos || !ubuf)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530301 return -EINVAL;
302
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530303 swrm = file->private_data;
304 if (!swrm)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530305 return -EINVAL;
306
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530307 if (count > sizeof(lbuf) - 1)
308 return -EINVAL;
309
310 rc = copy_from_user(lbuf, ubuf, count);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530311 if (rc)
312 return -EFAULT;
313
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530314 lbuf[count] = '\0';
315 rc = get_parameters(lbuf, param, 2);
316 if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) &&
317 (param[1] <= 0xFFFFFFFF) &&
318 (rc == 0))
319 swr_master_write(swrm, param[0], param[1]);
320 else
321 rc = -EINVAL;
322
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530323 if (rc == 0)
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530324 rc = count;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530325 else
326 pr_err("%s: rc = %d\n", __func__, rc);
327
328 return rc;
329}
330
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530331static const struct file_operations swrm_debug_read_ops = {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530332 .open = swrm_debug_open,
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530333 .write = swrm_debug_peek_write,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530334 .read = swrm_debug_read,
335};
336
Sudheer Papothi96c842a2019-08-29 12:11:21 +0530337static const struct file_operations swrm_debug_write_ops = {
338 .open = swrm_debug_open,
339 .write = swrm_debug_write,
340};
341
342static const struct file_operations swrm_debug_dump_ops = {
343 .open = swrm_debug_open,
344 .read = swrm_debug_reg_dump,
345};
346#endif
347
Sudheer Papothi0016db12019-06-11 04:42:38 +0530348static void swrm_reg_dump(struct swr_mstr_ctrl *swrm,
349 u32 *reg, u32 *val, int len, const char* func)
350{
351 int i = 0;
352
353 for (i = 0; i < len; i++)
354 dev_dbg(swrm->dev, "%s: reg = 0x%x val = 0x%x\n",
355 func, reg[i], val[i]);
356}
357
Sudheer Papothi921b8652019-10-03 02:13:32 +0530358static bool is_swr_clk_needed(struct swr_mstr_ctrl *swrm)
359{
360 return ((swrm->version <= SWRM_VERSION_1_5_1) ? true : false);
361}
362
Sudheer Papothi384addd2019-06-14 02:26:52 +0530363static int swrm_request_hw_vote(struct swr_mstr_ctrl *swrm,
364 int core_type, bool enable)
365{
366 int ret = 0;
367
Aditya Bavanarieb044612019-12-22 17:14:15 +0530368 mutex_lock(&swrm->devlock);
Sudheer Papothi384addd2019-06-14 02:26:52 +0530369 if (core_type == LPASS_HW_CORE) {
370 if (swrm->lpass_core_hw_vote) {
371 if (enable) {
Aditya Bavanarieb044612019-12-22 17:14:15 +0530372 if (!swrm->dev_up) {
373 dev_dbg(swrm->dev, "%s: device is down or SSR state\n",
374 __func__);
375 trace_printk("%s: device is down or SSR state\n",
376 __func__);
377 mutex_unlock(&swrm->devlock);
378 return -ENODEV;
379 }
380 if (++swrm->hw_core_clk_en == 1) {
381 ret =
Aditya Bavanaribb981b72020-03-19 18:30:10 +0530382 digital_cdc_rsc_mgr_hw_vote_enable(
383 swrm->lpass_core_hw_vote);
Aditya Bavanarieb044612019-12-22 17:14:15 +0530384 if (ret < 0) {
385 dev_err(swrm->dev,
386 "%s:lpass core hw enable failed\n",
387 __func__);
388 --swrm->hw_core_clk_en;
389 }
390 }
391 } else {
392 --swrm->hw_core_clk_en;
393 if (swrm->hw_core_clk_en < 0)
394 swrm->hw_core_clk_en = 0;
395 else if (swrm->hw_core_clk_en == 0)
Aditya Bavanaribb981b72020-03-19 18:30:10 +0530396 digital_cdc_rsc_mgr_hw_vote_disable(
397 swrm->lpass_core_hw_vote);
Aditya Bavanarieb044612019-12-22 17:14:15 +0530398 }
Sudheer Papothi384addd2019-06-14 02:26:52 +0530399 }
400 }
401 if (core_type == LPASS_AUDIO_CORE) {
402 if (swrm->lpass_core_audio) {
403 if (enable) {
Aditya Bavanarieb044612019-12-22 17:14:15 +0530404 if (!swrm->dev_up) {
405 dev_dbg(swrm->dev, "%s: device is down or SSR state\n",
406 __func__);
407 trace_printk("%s: device is down or SSR state\n",
408 __func__);
409 mutex_unlock(&swrm->devlock);
410 return -ENODEV;
411 }
412 if (++swrm->aud_core_clk_en == 1) {
413 ret =
Aditya Bavanaribb981b72020-03-19 18:30:10 +0530414 digital_cdc_rsc_mgr_hw_vote_enable(
415 swrm->lpass_core_audio);
Aditya Bavanarieb044612019-12-22 17:14:15 +0530416 if (ret < 0) {
417 dev_err(swrm->dev,
418 "%s:lpass audio hw enable failed\n",
419 __func__);
420 --swrm->aud_core_clk_en;
421 }
422 }
423 } else {
424 --swrm->aud_core_clk_en;
425 if (swrm->aud_core_clk_en < 0)
426 swrm->aud_core_clk_en = 0;
427 else if (swrm->aud_core_clk_en == 0)
Aditya Bavanaribb981b72020-03-19 18:30:10 +0530428 digital_cdc_rsc_mgr_hw_vote_disable(
429 swrm->lpass_core_audio);
Aditya Bavanarieb044612019-12-22 17:14:15 +0530430 }
Sudheer Papothi384addd2019-06-14 02:26:52 +0530431 }
432 }
433
Aditya Bavanarieb044612019-12-22 17:14:15 +0530434 mutex_unlock(&swrm->devlock);
435 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
436 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
437 trace_printk("%s: hw_clk_en: %d audio_core_clk_en: %d\n",
438 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
Sudheer Papothi384addd2019-06-14 02:26:52 +0530439 return ret;
440}
441
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +0530442static int swrm_get_ssp_period(struct swr_mstr_ctrl *swrm,
443 int row, int col,
444 int frame_sync)
445{
446 if (!swrm || !row || !col || !frame_sync)
447 return 1;
448
449 return ((swrm->bus_clk * 2) / ((row * col) * frame_sync));
450}
451
Sudheer Papothi921b8652019-10-03 02:13:32 +0530452static int swrm_core_vote_request(struct swr_mstr_ctrl *swrm)
453{
454 int ret = 0;
455
456 if (!swrm->handle)
457 return -EINVAL;
458
459 mutex_lock(&swrm->clklock);
460 if (!swrm->dev_up) {
461 ret = -ENODEV;
462 goto exit;
463 }
464 if (swrm->core_vote) {
465 ret = swrm->core_vote(swrm->handle, true);
466 if (ret)
467 dev_err_ratelimited(swrm->dev,
468 "%s: core vote request failed\n", __func__);
469 }
470exit:
471 mutex_unlock(&swrm->clklock);
472
473 return ret;
474}
475
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530476static int swrm_clk_request(struct swr_mstr_ctrl *swrm, bool enable)
477{
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530478 int ret = 0;
479
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530480 if (!swrm->clk || !swrm->handle)
481 return -EINVAL;
482
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530483 mutex_lock(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530484 if (enable) {
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530485 if (!swrm->dev_up) {
486 ret = -ENODEV;
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530487 goto exit;
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530488 }
Sudheer Papothi921b8652019-10-03 02:13:32 +0530489 if (is_swr_clk_needed(swrm)) {
490 if (swrm->core_vote) {
491 ret = swrm->core_vote(swrm->handle, true);
492 if (ret) {
493 dev_err_ratelimited(swrm->dev,
494 "%s: core vote request failed\n",
495 __func__);
496 goto exit;
497 }
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -0700498 }
499 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530500 swrm->clk_ref_count++;
501 if (swrm->clk_ref_count == 1) {
Aditya Bavanarif500a1d2019-09-16 18:27:51 -0700502 trace_printk("%s: clock enable count %d",
503 __func__, swrm->clk_ref_count);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530504 ret = swrm->clk(swrm->handle, true);
505 if (ret) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +0530506 dev_err_ratelimited(swrm->dev,
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530507 "%s: clock enable req failed",
508 __func__);
509 --swrm->clk_ref_count;
510 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530511 }
512 } else if (--swrm->clk_ref_count == 0) {
Aditya Bavanarif500a1d2019-09-16 18:27:51 -0700513 trace_printk("%s: clock disable count %d",
514 __func__, swrm->clk_ref_count);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530515 swrm->clk(swrm->handle, false);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530516 complete(&swrm->clk_off_complete);
517 }
518 if (swrm->clk_ref_count < 0) {
Meng Wang8c60bb52019-06-19 15:49:06 +0800519 dev_err(swrm->dev, "%s: swrm clk count mismatch\n", __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530520 swrm->clk_ref_count = 0;
521 }
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530522
523exit:
524 mutex_unlock(&swrm->clklock);
525 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530526}
527
528static int swrm_ahb_write(struct swr_mstr_ctrl *swrm,
529 u16 reg, u32 *value)
530{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530531 u32 temp = (u32)(*value);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530532 int ret = 0;
533
534 mutex_lock(&swrm->devlock);
535 if (!swrm->dev_up)
536 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530537
Sudheer Papothi921b8652019-10-03 02:13:32 +0530538 if (is_swr_clk_needed(swrm)) {
539 ret = swrm_clk_request(swrm, TRUE);
540 if (ret) {
541 dev_err_ratelimited(swrm->dev,
542 "%s: clock request failed\n",
543 __func__);
544 goto err;
545 }
546 } else if (swrm_core_vote_request(swrm)) {
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530547 goto err;
548 }
Sudheer Papothi921b8652019-10-03 02:13:32 +0530549
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530550 iowrite32(temp, swrm->swrm_dig_base + reg);
Sudheer Papothi921b8652019-10-03 02:13:32 +0530551 if (is_swr_clk_needed(swrm))
552 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530553err:
554 mutex_unlock(&swrm->devlock);
555 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530556}
557
558static int swrm_ahb_read(struct swr_mstr_ctrl *swrm,
559 u16 reg, u32 *value)
560{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530561 u32 temp = 0;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530562 int ret = 0;
563
564 mutex_lock(&swrm->devlock);
565 if (!swrm->dev_up)
566 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530567
Sudheer Papothi921b8652019-10-03 02:13:32 +0530568 if (is_swr_clk_needed(swrm)) {
569 ret = swrm_clk_request(swrm, TRUE);
570 if (ret) {
571 dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
572 __func__);
573 goto err;
574 }
575 } else if (swrm_core_vote_request(swrm)) {
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530576 goto err;
577 }
Sudheer Papothi921b8652019-10-03 02:13:32 +0530578
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530579 temp = ioread32(swrm->swrm_dig_base + reg);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530580 *value = temp;
Sudheer Papothi921b8652019-10-03 02:13:32 +0530581 if (is_swr_clk_needed(swrm))
582 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530583err:
584 mutex_unlock(&swrm->devlock);
585 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530586}
587
588static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr)
589{
590 u32 val = 0;
591
592 if (swrm->read)
593 val = swrm->read(swrm->handle, reg_addr);
594 else
595 swrm_ahb_read(swrm, reg_addr, &val);
596 return val;
597}
598
599static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val)
600{
601 if (swrm->write)
602 swrm->write(swrm->handle, reg_addr, val);
603 else
604 swrm_ahb_write(swrm, reg_addr, &val);
605}
606
607static int swr_master_bulk_write(struct swr_mstr_ctrl *swrm, u32 *reg_addr,
608 u32 *val, unsigned int length)
609{
610 int i = 0;
611
612 if (swrm->bulk_write)
613 swrm->bulk_write(swrm->handle, reg_addr, val, length);
614 else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530615 mutex_lock(&swrm->iolock);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530616 for (i = 0; i < length; i++) {
617 /* wait for FIFO WR command to complete to avoid overflow */
Karthikeyan Mani5d52dd82019-08-15 16:58:08 -0700618 /*
Vatsal Buchadc52af12020-03-16 23:54:29 +0530619 * Reduce sleep from 100us to 50us to meet KPIs
Karthikeyan Mani5d52dd82019-08-15 16:58:08 -0700620 * This still meets the hardware spec
621 */
Vatsal Buchadc52af12020-03-16 23:54:29 +0530622 usleep_range(50, 55);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530623 swr_master_write(swrm, reg_addr[i], val[i]);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530624 }
Vatsal Buchae44b8fb2020-06-25 11:43:18 +0530625 usleep_range(100, 110);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530626 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530627 }
628 return 0;
629}
630
Laxminath Kasame2291972019-11-08 14:51:59 +0530631static bool swrm_check_link_status(struct swr_mstr_ctrl *swrm, bool active)
632{
633 int retry = SWRM_LINK_STATUS_RETRY_CNT;
634 int ret = false;
635 int status = active ? 0x1 : 0x0;
Laxminath Kasam09819e92019-11-18 14:38:11 +0530636 int comp_sts = 0x0;
Laxminath Kasame2291972019-11-08 14:51:59 +0530637
638 if ((swrm->version <= SWRM_VERSION_1_5_1))
639 return true;
640
641 do {
Laxminath Kasam09819e92019-11-18 14:38:11 +0530642 comp_sts = swr_master_read(swrm, SWRM_COMP_STATUS) & 0x01;
643 /* check comp status and status requested met */
644 if ((comp_sts && status) || (!comp_sts && !status)) {
Laxminath Kasame2291972019-11-08 14:51:59 +0530645 ret = true;
646 break;
647 }
648 retry--;
649 usleep_range(500, 510);
650 } while (retry);
651
652 if (retry == 0)
653 dev_err(swrm->dev, "%s: link status not %s\n", __func__,
654 active ? "connected" : "disconnected");
655
656 return ret;
657}
658
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530659static bool swrm_is_port_en(struct swr_master *mstr)
660{
661 return !!(mstr->num_port);
662}
663
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530664static void copy_port_tables(struct swr_mstr_ctrl *swrm,
665 struct port_params *params)
666{
667 u8 i;
668 struct port_params *config = params;
669
670 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
671 /* wsa uses single frame structure for all configurations */
672 if (!swrm->mport_cfg[i].port_en)
673 continue;
674 swrm->mport_cfg[i].sinterval = config[i].si;
675 swrm->mport_cfg[i].offset1 = config[i].off1;
676 swrm->mport_cfg[i].offset2 = config[i].off2;
677 swrm->mport_cfg[i].hstart = config[i].hstart;
678 swrm->mport_cfg[i].hstop = config[i].hstop;
679 swrm->mport_cfg[i].blk_pack_mode = config[i].bp_mode;
680 swrm->mport_cfg[i].blk_grp_count = config[i].bgp_ctrl;
681 swrm->mport_cfg[i].word_length = config[i].wd_len;
682 swrm->mport_cfg[i].lane_ctrl = config[i].lane_ctrl;
683 }
684}
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530685static int swrm_get_port_config(struct swr_mstr_ctrl *swrm)
686{
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530687 struct port_params *params;
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530688 u32 usecase = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530689
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530690 /* TODO - Send usecase information to avoid checking for master_id */
691 if (swrm->mport_cfg[SWRM_DSD_PARAMS_PORT].port_en &&
692 (swrm->master_id == MASTER_ID_RX))
693 usecase = 1;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530694
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530695 params = swrm->port_param[usecase];
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530696 copy_port_tables(swrm, params);
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530697
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530698 return 0;
699}
700
701static int swrm_get_master_port(struct swr_mstr_ctrl *swrm, u8 *mstr_port_id,
702 u8 *mstr_ch_mask, u8 mstr_prt_type,
703 u8 slv_port_id)
704{
705 int i, j;
706 *mstr_port_id = 0;
707
708 for (i = 1; i <= swrm->num_ports; i++) {
709 for (j = 0; j < SWR_MAX_CH_PER_PORT; j++) {
710 if (swrm->port_mapping[i][j].port_type == mstr_prt_type)
711 goto found;
712 }
713 }
714found:
715 if (i > swrm->num_ports || j == SWR_MAX_CH_PER_PORT) {
716 dev_err(swrm->dev, "%s: port type not supported by master\n",
717 __func__);
718 return -EINVAL;
719 }
720 /* id 0 corresponds to master port 1 */
721 *mstr_port_id = i - 1;
722 *mstr_ch_mask = swrm->port_mapping[i][j].ch_mask;
723
724 return 0;
725
726}
727
728static u32 swrm_get_packed_reg_val(u8 *cmd_id, u8 cmd_data,
729 u8 dev_addr, u16 reg_addr)
730{
731 u32 val;
732 u8 id = *cmd_id;
733
734 if (id != SWR_BROADCAST_CMD_ID) {
735 if (id < 14)
736 id += 1;
737 else
738 id = 0;
739 *cmd_id = id;
740 }
741 val = SWR_REG_VAL_PACK(cmd_data, dev_addr, id, reg_addr);
742
743 return val;
744}
745
746static int swrm_cmd_fifo_rd_cmd(struct swr_mstr_ctrl *swrm, int *cmd_data,
747 u8 dev_addr, u8 cmd_id, u16 reg_addr,
748 u32 len)
749{
750 u32 val;
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530751 u32 retry_attempt = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530752
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530753 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530754 val = swrm_get_packed_reg_val(&swrm->rcmd_id, len, dev_addr, reg_addr);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530755 if (swrm->read) {
756 /* skip delay if read is handled in platform driver */
757 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
758 } else {
759 /* wait for FIFO RD to complete to avoid overflow */
760 usleep_range(100, 105);
761 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
762 /* wait for FIFO RD CMD complete to avoid overflow */
763 usleep_range(250, 255);
764 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530765retry_read:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530766 *cmd_data = swr_master_read(swrm, SWRM_CMD_FIFO_RD_FIFO_ADDR);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530767 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, rcmd_id: 0x%x, \
768 dev_num: 0x%x, cmd_data: 0x%x\n", __func__, reg_addr,
769 cmd_id, swrm->rcmd_id, dev_addr, *cmd_data);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530770 if ((((*cmd_data) & 0xF00) >> 8) != swrm->rcmd_id) {
771 if (retry_attempt < MAX_FIFO_RD_FAIL_RETRY) {
772 /* wait 500 us before retry on fifo read failure */
773 usleep_range(500, 505);
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +0530774 if (retry_attempt == (MAX_FIFO_RD_FAIL_RETRY - 1)) {
775 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
776 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
777 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530778 retry_attempt++;
779 goto retry_read;
780 } else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530781 dev_err_ratelimited(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, \
782 rcmd_id: 0x%x, dev_num: 0x%x, cmd_data: 0x%x\n",
783 __func__, reg_addr, cmd_id, swrm->rcmd_id,
784 dev_addr, *cmd_data);
785
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530786 dev_err_ratelimited(swrm->dev,
787 "%s: failed to read fifo\n", __func__);
788 }
789 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530790 mutex_unlock(&swrm->iolock);
791
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530792 return 0;
793}
794
795static int swrm_cmd_fifo_wr_cmd(struct swr_mstr_ctrl *swrm, u8 cmd_data,
796 u8 dev_addr, u8 cmd_id, u16 reg_addr)
797{
798 u32 val;
799 int ret = 0;
800
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530801 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530802 if (!cmd_id)
803 val = swrm_get_packed_reg_val(&swrm->wcmd_id, cmd_data,
804 dev_addr, reg_addr);
805 else
806 val = swrm_get_packed_reg_val(&cmd_id, cmd_data,
807 dev_addr, reg_addr);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530808 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x,wcmd_id: 0x%x, \
809 dev_num: 0x%x, cmd_data: 0x%x\n", __func__,
810 reg_addr, cmd_id, swrm->wcmd_id,dev_addr, cmd_data);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +0530811 swr_master_write(swrm, SWRM_CMD_FIFO_WR_CMD, val);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530812 /*
813 * wait for FIFO WR command to complete to avoid overflow
814 * skip delay if write is handled in platform driver.
815 */
816 if(!swrm->write)
Karthikeyan Mani5d52dd82019-08-15 16:58:08 -0700817 usleep_range(150, 155);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530818 if (cmd_id == 0xF) {
819 /*
820 * sleep for 10ms for MSM soundwire variant to allow broadcast
821 * command to complete.
822 */
823 if (swrm_is_msm_variant(swrm->version))
824 usleep_range(10000, 10100);
825 else
826 wait_for_completion_timeout(&swrm->broadcast,
827 (2 * HZ/10));
828 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530829 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530830 return ret;
831}
832
833static int swrm_read(struct swr_master *master, u8 dev_num, u16 reg_addr,
834 void *buf, u32 len)
835{
836 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
837 int ret = 0;
838 int val;
839 u8 *reg_val = (u8 *)buf;
840
841 if (!swrm) {
842 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
843 return -EINVAL;
844 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530845 if (!dev_num) {
846 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
847 return -EINVAL;
848 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530849 mutex_lock(&swrm->devlock);
850 if (!swrm->dev_up) {
851 mutex_unlock(&swrm->devlock);
852 return 0;
853 }
854 mutex_unlock(&swrm->devlock);
855
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530856 pm_runtime_get_sync(swrm->dev);
Vatsal Buchabd68a7c2020-06-08 10:41:56 +0530857 if (swrm->req_clk_switch)
858 swrm_runtime_resume(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530859 ret = swrm_cmd_fifo_rd_cmd(swrm, &val, dev_num, 0, reg_addr, len);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530860
861 if (!ret)
862 *reg_val = (u8)val;
863
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530864 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530865 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530866 return ret;
867}
868
869static int swrm_write(struct swr_master *master, u8 dev_num, u16 reg_addr,
870 const void *buf)
871{
872 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
873 int ret = 0;
874 u8 reg_val = *(u8 *)buf;
875
876 if (!swrm) {
877 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
878 return -EINVAL;
879 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530880 if (!dev_num) {
881 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
882 return -EINVAL;
883 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530884 mutex_lock(&swrm->devlock);
885 if (!swrm->dev_up) {
886 mutex_unlock(&swrm->devlock);
887 return 0;
888 }
889 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530890
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530891 pm_runtime_get_sync(swrm->dev);
Vatsal Buchabd68a7c2020-06-08 10:41:56 +0530892 if (swrm->req_clk_switch)
893 swrm_runtime_resume(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530894 ret = swrm_cmd_fifo_wr_cmd(swrm, reg_val, dev_num, 0, reg_addr);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530895
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530896 pm_runtime_put_autosuspend(swrm->dev);
897 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530898 return ret;
899}
900
901static int swrm_bulk_write(struct swr_master *master, u8 dev_num, void *reg,
902 const void *buf, size_t len)
903{
904 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
905 int ret = 0;
906 int i;
907 u32 *val;
908 u32 *swr_fifo_reg;
909
910 if (!swrm || !swrm->handle) {
911 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
912 return -EINVAL;
913 }
914 if (len <= 0)
915 return -EINVAL;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530916 mutex_lock(&swrm->devlock);
917 if (!swrm->dev_up) {
918 mutex_unlock(&swrm->devlock);
919 return 0;
920 }
921 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530922
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530923 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530924 if (dev_num) {
925 swr_fifo_reg = kcalloc(len, sizeof(u32), GFP_KERNEL);
926 if (!swr_fifo_reg) {
927 ret = -ENOMEM;
928 goto err;
929 }
930 val = kcalloc(len, sizeof(u32), GFP_KERNEL);
931 if (!val) {
932 ret = -ENOMEM;
933 goto mem_fail;
934 }
935
936 for (i = 0; i < len; i++) {
937 val[i] = swrm_get_packed_reg_val(&swrm->wcmd_id,
938 ((u8 *)buf)[i],
939 dev_num,
940 ((u16 *)reg)[i]);
941 swr_fifo_reg[i] = SWRM_CMD_FIFO_WR_CMD;
942 }
943 ret = swr_master_bulk_write(swrm, swr_fifo_reg, val, len);
944 if (ret) {
945 dev_err(&master->dev, "%s: bulk write failed\n",
946 __func__);
947 ret = -EINVAL;
948 }
949 } else {
950 dev_err(&master->dev,
951 "%s: No support of Bulk write for master regs\n",
952 __func__);
953 ret = -EINVAL;
954 goto err;
955 }
956 kfree(val);
957mem_fail:
958 kfree(swr_fifo_reg);
959err:
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530960 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530961 pm_runtime_mark_last_busy(swrm->dev);
962 return ret;
963}
964
965static u8 get_inactive_bank_num(struct swr_mstr_ctrl *swrm)
966{
967 return (swr_master_read(swrm, SWRM_MCP_STATUS) &
968 SWRM_MCP_STATUS_BANK_NUM_MASK) ? 0 : 1;
969}
970
971static void enable_bank_switch(struct swr_mstr_ctrl *swrm, u8 bank,
972 u8 row, u8 col)
973{
974 swrm_cmd_fifo_wr_cmd(swrm, ((row << 3) | col), 0xF, 0xF,
975 SWRS_SCP_FRAME_CTRL_BANK(bank));
976}
977
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +0530978static void swrm_switch_frame_shape(struct swr_mstr_ctrl *swrm, int mclk_freq)
979{
980 u8 bank;
981 u32 n_row, n_col;
982 u32 value = 0;
983 u32 row = 0, col = 0;
984 u8 ssp_period = 0;
985 int frame_sync = SWRM_FRAME_SYNC_SEL;
986
987 if (mclk_freq == MCLK_FREQ_NATIVE) {
988 n_col = SWR_MAX_COL;
989 col = SWRM_COL_16;
990 n_row = SWR_ROW_64;
991 row = SWRM_ROW_64;
992 frame_sync = SWRM_FRAME_SYNC_SEL_NATIVE;
993 } else {
994 n_col = SWR_MIN_COL;
995 col = SWRM_COL_02;
996 n_row = SWR_ROW_50;
997 row = SWRM_ROW_50;
998 frame_sync = SWRM_FRAME_SYNC_SEL;
999 }
1000
1001 bank = get_inactive_bank_num(swrm);
1002 ssp_period = swrm_get_ssp_period(swrm, row, col, frame_sync);
1003 dev_dbg(swrm->dev, "%s: ssp_period: %d\n", __func__, ssp_period);
1004 value = ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
1005 (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
1006 ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
1007 swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1008 enable_bank_switch(swrm, bank, n_row, n_col);
1009}
1010
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301011static struct swr_port_info *swrm_get_port_req(struct swrm_mports *mport,
1012 u8 slv_port, u8 dev_num)
1013{
1014 struct swr_port_info *port_req = NULL;
1015
1016 list_for_each_entry(port_req, &mport->port_req_list, list) {
1017 /* Store dev_id instead of dev_num if enumeration is changed run_time */
1018 if ((port_req->slave_port_id == slv_port)
1019 && (port_req->dev_num == dev_num))
1020 return port_req;
1021 }
1022 return NULL;
1023}
1024
1025static bool swrm_remove_from_group(struct swr_master *master)
1026{
1027 struct swr_device *swr_dev;
1028 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1029 bool is_removed = false;
1030
1031 if (!swrm)
1032 goto end;
1033
1034 mutex_lock(&swrm->mlock);
1035 if ((swrm->num_rx_chs > 1) &&
1036 (swrm->num_rx_chs == swrm->num_cfg_devs)) {
1037 list_for_each_entry(swr_dev, &master->devices,
1038 dev_list) {
1039 swr_dev->group_id = SWR_GROUP_NONE;
1040 master->gr_sid = 0;
1041 }
1042 is_removed = true;
1043 }
1044 mutex_unlock(&swrm->mlock);
1045
1046end:
1047 return is_removed;
1048}
1049
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301050int swrm_get_clk_div_rate(int mclk_freq, int bus_clk_freq)
1051{
1052 if (!bus_clk_freq)
1053 return mclk_freq;
1054
1055 if (mclk_freq == SWR_CLK_RATE_9P6MHZ) {
1056 if (bus_clk_freq <= SWR_CLK_RATE_0P6MHZ)
1057 bus_clk_freq = SWR_CLK_RATE_0P6MHZ;
1058 else if (bus_clk_freq <= SWR_CLK_RATE_1P2MHZ)
1059 bus_clk_freq = SWR_CLK_RATE_1P2MHZ;
1060 else if (bus_clk_freq <= SWR_CLK_RATE_2P4MHZ)
1061 bus_clk_freq = SWR_CLK_RATE_2P4MHZ;
1062 else if(bus_clk_freq <= SWR_CLK_RATE_4P8MHZ)
1063 bus_clk_freq = SWR_CLK_RATE_4P8MHZ;
1064 else if(bus_clk_freq <= SWR_CLK_RATE_9P6MHZ)
1065 bus_clk_freq = SWR_CLK_RATE_9P6MHZ;
1066 } else if (mclk_freq == SWR_CLK_RATE_11P2896MHZ)
1067 bus_clk_freq = SWR_CLK_RATE_11P2896MHZ;
1068
1069 return bus_clk_freq;
1070}
1071
1072static int swrm_update_bus_clk(struct swr_mstr_ctrl *swrm)
1073{
1074 int ret = 0;
1075 int agg_clk = 0;
1076 int i;
1077
1078 for (i = 0; i < SWR_MSTR_PORT_LEN; i++)
1079 agg_clk += swrm->mport_cfg[i].ch_rate;
1080
1081 if (agg_clk)
1082 swrm->bus_clk = swrm_get_clk_div_rate(swrm->mclk_freq,
1083 agg_clk);
1084 else
1085 swrm->bus_clk = swrm->mclk_freq;
1086
1087 dev_dbg(swrm->dev, "%s: all_port_clk: %d, bus_clk: %d\n",
1088 __func__, agg_clk, swrm->bus_clk);
1089
1090 return ret;
1091}
1092
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301093static void swrm_disable_ports(struct swr_master *master,
1094 u8 bank)
1095{
1096 u32 value;
1097 struct swr_port_info *port_req;
1098 int i;
1099 struct swrm_mports *mport;
1100 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1101
1102 if (!swrm) {
1103 pr_err("%s: swrm is null\n", __func__);
1104 return;
1105 }
1106
1107 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
1108 master->num_port);
1109
1110
1111 for (i = 0; i < SWR_MSTR_PORT_LEN ; i++) {
1112
1113 mport = &(swrm->mport_cfg[i]);
1114 if (!mport->port_en)
1115 continue;
1116
1117 list_for_each_entry(port_req, &mport->port_req_list, list) {
1118 /* skip ports with no change req's*/
1119 if (port_req->req_ch == port_req->ch_en)
1120 continue;
1121
1122 swrm_cmd_fifo_wr_cmd(swrm, port_req->req_ch,
1123 port_req->dev_num, 0x00,
1124 SWRS_DP_CHANNEL_ENABLE_BANK(port_req->slave_port_id,
1125 bank));
1126 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x\n",
1127 __func__, i,
1128 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)));
1129 }
1130 value = ((mport->req_ch)
1131 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
1132 value |= ((mport->offset2)
1133 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
1134 value |= ((mport->offset1)
1135 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
1136 value |= mport->sinterval;
1137
1138 swr_master_write(swrm,
1139 SWRM_DP_PORT_CTRL_BANK(i+1, bank),
1140 value);
1141 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
1142 __func__, i,
1143 (SWRM_DP_PORT_CTRL_BANK(i+1, bank)), value);
1144 }
1145}
1146
1147static void swrm_cleanup_disabled_port_reqs(struct swr_master *master)
1148{
1149 struct swr_port_info *port_req, *next;
1150 int i;
1151 struct swrm_mports *mport;
1152 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1153
1154 if (!swrm) {
1155 pr_err("%s: swrm is null\n", __func__);
1156 return;
1157 }
1158 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
1159 master->num_port);
1160
1161 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
1162 mport = &(swrm->mport_cfg[i]);
1163 list_for_each_entry_safe(port_req, next,
1164 &mport->port_req_list, list) {
1165 /* skip ports without new ch req */
1166 if (port_req->ch_en == port_req->req_ch)
1167 continue;
1168
1169 /* remove new ch req's*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +05301170 port_req->ch_en = port_req->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301171
1172 /* If no streams enabled on port, remove the port req */
1173 if (port_req->ch_en == 0) {
1174 list_del(&port_req->list);
1175 kfree(port_req);
1176 }
1177 }
1178 /* remove new ch req's on mport*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +05301179 mport->ch_en = mport->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301180
1181 if (!(mport->ch_en)) {
1182 mport->port_en = false;
1183 master->port_en_mask &= ~i;
1184 }
1185 }
1186}
1187static void swrm_copy_data_port_config(struct swr_master *master, u8 bank)
1188{
1189 u32 value, slv_id;
1190 struct swr_port_info *port_req;
1191 int i;
1192 struct swrm_mports *mport;
1193 u32 reg[SWRM_MAX_PORT_REG];
1194 u32 val[SWRM_MAX_PORT_REG];
1195 int len = 0;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301196 u8 hparams;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301197 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1198
1199 if (!swrm) {
1200 pr_err("%s: swrm is null\n", __func__);
1201 return;
1202 }
1203
1204 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
1205 master->num_port);
1206
1207 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
1208 mport = &(swrm->mport_cfg[i]);
1209 if (!mport->port_en)
1210 continue;
1211
1212 list_for_each_entry(port_req, &mport->port_req_list, list) {
1213 slv_id = port_req->slave_port_id;
1214 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1215 val[len++] = SWR_REG_VAL_PACK(port_req->req_ch,
1216 port_req->dev_num, 0x00,
1217 SWRS_DP_CHANNEL_ENABLE_BANK(slv_id,
1218 bank));
1219
1220 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1221 val[len++] = SWR_REG_VAL_PACK(mport->sinterval,
1222 port_req->dev_num, 0x00,
1223 SWRS_DP_SAMPLE_CONTROL_1_BANK(slv_id,
1224 bank));
1225
1226 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1227 val[len++] = SWR_REG_VAL_PACK(mport->offset1,
1228 port_req->dev_num, 0x00,
1229 SWRS_DP_OFFSET_CONTROL_1_BANK(slv_id,
1230 bank));
1231
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301232 if (mport->offset2 != SWR_INVALID_PARAM) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301233 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1234 val[len++] = SWR_REG_VAL_PACK(mport->offset2,
1235 port_req->dev_num, 0x00,
1236 SWRS_DP_OFFSET_CONTROL_2_BANK(
1237 slv_id, bank));
1238 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301239 if (mport->hstart != SWR_INVALID_PARAM
1240 && mport->hstop != SWR_INVALID_PARAM) {
1241 hparams = (mport->hstart << 4) | mport->hstop;
1242
1243 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1244 val[len++] = SWR_REG_VAL_PACK(hparams,
1245 port_req->dev_num, 0x00,
1246 SWRS_DP_HCONTROL_BANK(slv_id,
1247 bank));
1248 }
1249 if (mport->word_length != SWR_INVALID_PARAM) {
1250 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1251 val[len++] =
1252 SWR_REG_VAL_PACK(mport->word_length,
1253 port_req->dev_num, 0x00,
1254 SWRS_DP_BLOCK_CONTROL_1(slv_id));
1255 }
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +05301256 if (mport->blk_pack_mode != SWR_INVALID_PARAM
1257 && swrm->master_id != MASTER_ID_WSA) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301258 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1259 val[len++] =
1260 SWR_REG_VAL_PACK(mport->blk_pack_mode,
1261 port_req->dev_num, 0x00,
1262 SWRS_DP_BLOCK_CONTROL_3_BANK(slv_id,
1263 bank));
1264 }
1265 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
1266 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1267 val[len++] =
1268 SWR_REG_VAL_PACK(mport->blk_grp_count,
1269 port_req->dev_num, 0x00,
1270 SWRS_DP_BLOCK_CONTROL_2_BANK(slv_id,
1271 bank));
1272 }
1273 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
1274 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1275 val[len++] =
1276 SWR_REG_VAL_PACK(mport->lane_ctrl,
1277 port_req->dev_num, 0x00,
1278 SWRS_DP_LANE_CONTROL_BANK(slv_id,
1279 bank));
1280 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301281 port_req->ch_en = port_req->req_ch;
1282 }
1283 value = ((mport->req_ch)
1284 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +05301285
1286 if (mport->offset2 != SWR_INVALID_PARAM)
1287 value |= ((mport->offset2)
1288 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301289 value |= ((mport->offset1)
1290 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
1291 value |= mport->sinterval;
1292
1293
1294 reg[len] = SWRM_DP_PORT_CTRL_BANK(i + 1, bank);
1295 val[len++] = value;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301296 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
1297 __func__, i,
1298 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)), value);
1299
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301300 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
1301 reg[len] = SWRM_DP_PORT_CTRL_2_BANK(i + 1, bank);
1302 val[len++] = mport->lane_ctrl;
1303 }
1304 if (mport->word_length != SWR_INVALID_PARAM) {
1305 reg[len] = SWRM_DP_BLOCK_CTRL_1(i + 1);
1306 val[len++] = mport->word_length;
1307 }
1308
1309 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
1310 reg[len] = SWRM_DP_BLOCK_CTRL2_BANK(i + 1, bank);
1311 val[len++] = mport->blk_grp_count;
1312 }
1313 if (mport->hstart != SWR_INVALID_PARAM
1314 && mport->hstop != SWR_INVALID_PARAM) {
1315 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
Laxminath Kasame30eef72018-11-05 17:40:09 +05301316 hparams = (mport->hstop << 4) | mport->hstart;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301317 val[len++] = hparams;
Laxminath Kasam990c70b2018-11-09 23:15:09 +05301318 } else {
1319 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
1320 hparams = (SWR_HSTOP_MAX_VAL << 4) | SWR_HSTART_MIN_VAL;
1321 val[len++] = hparams;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301322 }
1323 if (mport->blk_pack_mode != SWR_INVALID_PARAM) {
1324 reg[len] = SWRM_DP_BLOCK_CTRL3_BANK(i + 1, bank);
1325 val[len++] = mport->blk_pack_mode;
1326 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301327 mport->ch_en = mport->req_ch;
1328
1329 }
Sudheer Papothi0016db12019-06-11 04:42:38 +05301330 swrm_reg_dump(swrm, reg, val, len, __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301331 swr_master_bulk_write(swrm, reg, val, len);
1332}
1333
1334static void swrm_apply_port_config(struct swr_master *master)
1335{
1336 u8 bank;
1337 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1338
1339 if (!swrm) {
1340 pr_err("%s: Invalid handle to swr controller\n",
1341 __func__);
1342 return;
1343 }
1344
1345 bank = get_inactive_bank_num(swrm);
1346 dev_dbg(swrm->dev, "%s: enter bank: %d master_ports: %d\n",
1347 __func__, bank, master->num_port);
1348
Vatsal Bucha687f9822020-04-01 18:21:41 +05301349 if (!swrm->disable_div2_clk_switch)
1350 swrm_cmd_fifo_wr_cmd(swrm, 0x01, 0xF, 0x00,
1351 SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(bank));
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301352
1353 swrm_copy_data_port_config(master, bank);
1354}
1355
1356static int swrm_slvdev_datapath_control(struct swr_master *master, bool enable)
1357{
1358 u8 bank;
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301359 u32 value = 0, n_row = 0, n_col = 0;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301360 u32 row = 0, col = 0;
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301361 int bus_clk_div_factor;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301362 int ret;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301363 u8 ssp_period = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301364 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1365 int mask = (SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK |
1366 SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK |
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301367 SWRM_MCP_FRAME_CTRL_BANK_CLK_DIV_VALUE_BMSK |
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301368 SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_BMSK);
1369 u8 inactive_bank;
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +05301370 int frame_sync = SWRM_FRAME_SYNC_SEL;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301371
1372 if (!swrm) {
1373 pr_err("%s: swrm is null\n", __func__);
1374 return -EFAULT;
1375 }
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301376
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301377 mutex_lock(&swrm->mlock);
1378
Ramprasad Katkam979b7c92019-05-17 15:31:21 +05301379 /*
1380 * During disable if master is already down, which implies an ssr/pdr
1381 * scenario, just mark ports as disabled and exit
1382 */
1383 if (swrm->state == SWR_MSTR_SSR && !enable) {
1384 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1385 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1386 __func__);
1387 goto exit;
1388 }
1389 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
1390 swrm_cleanup_disabled_port_reqs(master);
1391 if (!swrm_is_port_en(master)) {
1392 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1393 __func__);
1394 pm_runtime_mark_last_busy(swrm->dev);
1395 pm_runtime_put_autosuspend(swrm->dev);
1396 }
1397 goto exit;
1398 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301399 bank = get_inactive_bank_num(swrm);
1400
1401 if (enable) {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301402 if (!test_bit(ENABLE_PENDING, &swrm->port_req_pending)) {
1403 dev_dbg(swrm->dev, "%s:No pending connect port req\n",
1404 __func__);
1405 goto exit;
1406 }
1407 clear_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301408 ret = swrm_get_port_config(swrm);
1409 if (ret) {
1410 /* cannot accommodate ports */
1411 swrm_cleanup_disabled_port_reqs(master);
1412 mutex_unlock(&swrm->mlock);
1413 return -EINVAL;
1414 }
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301415 swr_master_write(swrm, SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301416 SWRM_INTERRUPT_STATUS_MASK);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301417 /* apply the new port config*/
1418 swrm_apply_port_config(master);
1419 } else {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301420 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1421 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1422 __func__);
1423 goto exit;
1424 }
1425 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301426 swrm_disable_ports(master, bank);
1427 }
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301428 dev_dbg(swrm->dev, "%s: enable: %d, cfg_devs: %d freq %d\n",
1429 __func__, enable, swrm->num_cfg_devs, swrm->mclk_freq);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301430
1431 if (enable) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301432 /* set col = 16 */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301433 n_col = SWR_MAX_COL;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301434 col = SWRM_COL_16;
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301435 if (swrm->bus_clk == MCLK_FREQ_LP) {
1436 n_col = SWR_MIN_COL;
1437 col = SWRM_COL_02;
1438 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301439 } else {
1440 /*
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301441 * Do not change to col = 2 if there are still active ports
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301442 */
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301443 if (!master->num_port) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301444 n_col = SWR_MIN_COL;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301445 col = SWRM_COL_02;
1446 } else {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301447 n_col = SWR_MAX_COL;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301448 col = SWRM_COL_16;
1449 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301450 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301451 /* Use default 50 * x, frame shape. Change based on mclk */
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301452 if (swrm->mclk_freq == MCLK_FREQ_NATIVE) {
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301453 dev_dbg(swrm->dev, "setting 64 x %d frameshape\n", col);
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301454 n_row = SWR_ROW_64;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301455 row = SWRM_ROW_64;
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +05301456 frame_sync = SWRM_FRAME_SYNC_SEL_NATIVE;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301457 } else {
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301458 dev_dbg(swrm->dev, "setting 50 x %d frameshape\n", col);
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301459 n_row = SWR_ROW_50;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301460 row = SWRM_ROW_50;
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +05301461 frame_sync = SWRM_FRAME_SYNC_SEL;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301462 }
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +05301463 ssp_period = swrm_get_ssp_period(swrm, row, col, frame_sync);
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301464 bus_clk_div_factor = swrm_get_clk_div(swrm->mclk_freq, swrm->bus_clk);
1465 dev_dbg(swrm->dev, "%s: ssp_period: %d, bus_clk_div:%d \n", __func__,
1466 ssp_period, bus_clk_div_factor);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301467 value = swr_master_read(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank));
1468 value &= (~mask);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301469 value |= ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301470 (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301471 (bus_clk_div_factor <<
1472 SWRM_MCP_FRAME_CTRL_BANK_CLK_DIV_VALUE_SHFT) |
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301473 ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301474 swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1475
1476 dev_dbg(swrm->dev, "%s: regaddr: 0x%x, value: 0x%x\n", __func__,
1477 SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1478
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301479 enable_bank_switch(swrm, bank, n_row, n_col);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301480 inactive_bank = bank ? 0 : 1;
1481
1482 if (enable)
1483 swrm_copy_data_port_config(master, inactive_bank);
1484 else {
1485 swrm_disable_ports(master, inactive_bank);
1486 swrm_cleanup_disabled_port_reqs(master);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301487 }
1488 if (!swrm_is_port_en(master)) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301489 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1490 __func__);
1491 pm_runtime_mark_last_busy(swrm->dev);
1492 pm_runtime_put_autosuspend(swrm->dev);
1493 }
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301494exit:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301495 mutex_unlock(&swrm->mlock);
1496return 0;
1497}
1498
1499static int swrm_connect_port(struct swr_master *master,
1500 struct swr_params *portinfo)
1501{
1502 int i;
1503 struct swr_port_info *port_req;
1504 int ret = 0;
1505 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1506 struct swrm_mports *mport;
1507 u8 mstr_port_id, mstr_ch_msk;
1508
1509 dev_dbg(&master->dev, "%s: enter\n", __func__);
1510 if (!portinfo)
1511 return -EINVAL;
1512
1513 if (!swrm) {
1514 dev_err(&master->dev,
1515 "%s: Invalid handle to swr controller\n",
1516 __func__);
1517 return -EINVAL;
1518 }
1519
1520 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301521 mutex_lock(&swrm->devlock);
1522 if (!swrm->dev_up) {
1523 mutex_unlock(&swrm->devlock);
1524 mutex_unlock(&swrm->mlock);
1525 return -EINVAL;
1526 }
1527 mutex_unlock(&swrm->devlock);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301528 if (!swrm_is_port_en(master))
1529 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301530
1531 for (i = 0; i < portinfo->num_port; i++) {
1532 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_msk,
1533 portinfo->port_type[i],
1534 portinfo->port_id[i]);
1535 if (ret) {
1536 dev_err(&master->dev,
1537 "%s: mstr portid for slv port %d not found\n",
1538 __func__, portinfo->port_id[i]);
1539 goto port_fail;
1540 }
1541
1542 mport = &(swrm->mport_cfg[mstr_port_id]);
1543 /* get port req */
1544 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1545 portinfo->dev_num);
1546 if (!port_req) {
1547 dev_dbg(&master->dev, "%s: new req:port id %d dev %d\n",
1548 __func__, portinfo->port_id[i],
1549 portinfo->dev_num);
1550 port_req = kzalloc(sizeof(struct swr_port_info),
1551 GFP_KERNEL);
1552 if (!port_req) {
1553 ret = -ENOMEM;
1554 goto mem_fail;
1555 }
1556 port_req->dev_num = portinfo->dev_num;
1557 port_req->slave_port_id = portinfo->port_id[i];
1558 port_req->num_ch = portinfo->num_ch[i];
1559 port_req->ch_rate = portinfo->ch_rate[i];
1560 port_req->ch_en = 0;
1561 port_req->master_port_id = mstr_port_id;
1562 list_add(&port_req->list, &mport->port_req_list);
1563 }
1564 port_req->req_ch |= portinfo->ch_en[i];
1565
1566 dev_dbg(&master->dev,
1567 "%s: mstr port %d, slv port %d ch_rate %d num_ch %d\n",
1568 __func__, port_req->master_port_id,
1569 port_req->slave_port_id, port_req->ch_rate,
1570 port_req->num_ch);
1571 /* Put the port req on master port */
1572 mport = &(swrm->mport_cfg[mstr_port_id]);
1573 mport->port_en = true;
1574 mport->req_ch |= mstr_ch_msk;
1575 master->port_en_mask |= (1 << mstr_port_id);
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301576 if (swrm->clk_stop_mode0_supp &&
1577 (mport->ch_rate < portinfo->ch_rate[i])) {
1578 mport->ch_rate = portinfo->ch_rate[i];
1579 swrm_update_bus_clk(swrm);
1580 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301581 }
1582 master->num_port += portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301583 set_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301584 swr_port_response(master, portinfo->tid);
1585
1586 mutex_unlock(&swrm->mlock);
1587 return 0;
1588
1589port_fail:
1590mem_fail:
1591 /* cleanup port reqs in error condition */
1592 swrm_cleanup_disabled_port_reqs(master);
1593 mutex_unlock(&swrm->mlock);
1594 return ret;
1595}
1596
1597static int swrm_disconnect_port(struct swr_master *master,
1598 struct swr_params *portinfo)
1599{
1600 int i, ret = 0;
1601 struct swr_port_info *port_req;
1602 struct swrm_mports *mport;
1603 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1604 u8 mstr_port_id, mstr_ch_mask;
1605
1606 if (!swrm) {
1607 dev_err(&master->dev,
1608 "%s: Invalid handle to swr controller\n",
1609 __func__);
1610 return -EINVAL;
1611 }
1612
1613 if (!portinfo) {
1614 dev_err(&master->dev, "%s: portinfo is NULL\n", __func__);
1615 return -EINVAL;
1616 }
1617 mutex_lock(&swrm->mlock);
1618
1619 for (i = 0; i < portinfo->num_port; i++) {
1620
1621 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_mask,
1622 portinfo->port_type[i], portinfo->port_id[i]);
1623 if (ret) {
1624 dev_err(&master->dev,
1625 "%s: mstr portid for slv port %d not found\n",
1626 __func__, portinfo->port_id[i]);
1627 mutex_unlock(&swrm->mlock);
1628 return -EINVAL;
1629 }
1630 mport = &(swrm->mport_cfg[mstr_port_id]);
1631 /* get port req */
1632 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1633 portinfo->dev_num);
1634
1635 if (!port_req) {
1636 dev_err(&master->dev, "%s:port not enabled : port %d\n",
1637 __func__, portinfo->port_id[i]);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05301638 mutex_unlock(&swrm->mlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301639 return -EINVAL;
1640 }
1641 port_req->req_ch &= ~portinfo->ch_en[i];
1642 mport->req_ch &= ~mstr_ch_mask;
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301643 if (swrm->clk_stop_mode0_supp && !mport->req_ch) {
1644 mport->ch_rate = 0;
1645 swrm_update_bus_clk(swrm);
1646 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301647 }
1648 master->num_port -= portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301649 set_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301650 swr_port_response(master, portinfo->tid);
1651 mutex_unlock(&swrm->mlock);
1652
1653 return 0;
1654}
1655
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301656static int swrm_find_alert_slave(struct swr_mstr_ctrl *swrm,
1657 int status, u8 *devnum)
1658{
1659 int i;
1660 bool found = false;
1661
1662 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1663 if ((status & SWRM_MCP_SLV_STATUS_MASK) == SWR_ALERT) {
1664 *devnum = i;
1665 found = true;
1666 break;
1667 }
1668 status >>= 2;
1669 }
1670 if (found)
1671 return 0;
1672 else
1673 return -EINVAL;
1674}
1675
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301676static void swrm_enable_slave_irq(struct swr_mstr_ctrl *swrm)
1677{
1678 int i;
1679 int status = 0;
1680
1681 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1682 if (!status) {
1683 dev_dbg_ratelimited(swrm->dev, "%s: slaves status is 0x%x\n",
1684 __func__, status);
1685 return;
1686 }
1687 dev_dbg(swrm->dev, "%s: slave status: 0x%x\n", __func__, status);
1688 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
Vatsal Bucha21ba0772020-02-28 20:36:46 +05301689 if (status & SWRM_MCP_SLV_STATUS_MASK) {
1690 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, i, 0x0,
1691 SWRS_SCP_INT_STATUS_CLEAR_1);
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301692 swrm_cmd_fifo_wr_cmd(swrm, 0x4, i, 0x0,
Vatsal Bucha21ba0772020-02-28 20:36:46 +05301693 SWRS_SCP_INT_STATUS_MASK_1);
1694 }
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301695 status >>= 2;
1696 }
1697}
1698
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301699static int swrm_check_slave_change_status(struct swr_mstr_ctrl *swrm,
1700 int status, u8 *devnum)
1701{
1702 int i;
1703 int new_sts = status;
1704 int ret = SWR_NOT_PRESENT;
1705
1706 if (status != swrm->slave_status) {
1707 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1708 if ((status & SWRM_MCP_SLV_STATUS_MASK) !=
1709 (swrm->slave_status & SWRM_MCP_SLV_STATUS_MASK)) {
1710 ret = (status & SWRM_MCP_SLV_STATUS_MASK);
1711 *devnum = i;
1712 break;
1713 }
1714 status >>= 2;
1715 swrm->slave_status >>= 2;
1716 }
1717 swrm->slave_status = new_sts;
1718 }
1719 return ret;
1720}
1721
1722static irqreturn_t swr_mstr_interrupt(int irq, void *dev)
1723{
1724 struct swr_mstr_ctrl *swrm = dev;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301725 u32 value, intr_sts, intr_sts_masked;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301726 u32 temp = 0;
1727 u32 status, chg_sts, i;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301728 u8 devnum = 0;
1729 int ret = IRQ_HANDLED;
1730 struct swr_device *swr_dev;
1731 struct swr_master *mstr = &swrm->master;
1732
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07001733 trace_printk("%s enter\n", __func__);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301734 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1735 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1736 return IRQ_NONE;
1737 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301738
1739 mutex_lock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301740 if (swrm_clk_request(swrm, true)) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +05301741 dev_err_ratelimited(swrm->dev, "%s:clk request failed\n",
1742 __func__);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301743 mutex_unlock(&swrm->reslock);
1744 goto exit;
1745 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301746 mutex_unlock(&swrm->reslock);
1747
1748 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301749 intr_sts_masked = intr_sts & swrm->intr_mask;
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07001750
1751 trace_printk("%s: status: 0x%x \n", __func__, intr_sts_masked);
Ramprasad Katkam83303512018-10-11 17:34:22 +05301752handle_irq:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301753 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301754 value = intr_sts_masked & (1 << i);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301755 if (!value)
1756 continue;
1757
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301758 switch (value) {
1759 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1760 dev_dbg(swrm->dev, "Trigger irq to slave device\n");
1761 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301762 ret = swrm_find_alert_slave(swrm, status, &devnum);
1763 if (ret) {
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301764 dev_err_ratelimited(swrm->dev,
1765 "no slave alert found.spurious interrupt\n");
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05301766 break;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301767 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301768 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1769 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1770 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1771 SWRS_SCP_INT_STATUS_CLEAR_1);
1772 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1773 SWRS_SCP_INT_STATUS_CLEAR_1);
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301774
1775
1776 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1777 if (swr_dev->dev_num != devnum)
1778 continue;
1779 if (swr_dev->slave_irq) {
1780 do {
Ramprasad Katkam2586a4b2019-03-18 16:53:39 +05301781 swr_dev->slave_irq_pending = 0;
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301782 handle_nested_irq(
1783 irq_find_mapping(
1784 swr_dev->slave_irq, 0));
1785 } while (swr_dev->slave_irq_pending);
1786 }
1787
1788 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301789 break;
1790 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1791 dev_dbg(swrm->dev, "SWR new slave attached\n");
1792 break;
1793 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1794 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1795 if (status == swrm->slave_status) {
1796 dev_dbg(swrm->dev,
1797 "%s: No change in slave status: %d\n",
1798 __func__, status);
1799 break;
1800 }
1801 chg_sts = swrm_check_slave_change_status(swrm, status,
1802 &devnum);
1803 switch (chg_sts) {
1804 case SWR_NOT_PRESENT:
1805 dev_dbg(swrm->dev, "device %d got detached\n",
1806 devnum);
1807 break;
1808 case SWR_ATTACHED_OK:
1809 dev_dbg(swrm->dev, "device %d got attached\n",
1810 devnum);
Ramprasad Katkamdebe8932018-09-25 18:08:18 +05301811 /* enable host irq from slave device*/
1812 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1813 SWRS_SCP_INT_STATUS_CLEAR_1);
1814 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1815 SWRS_SCP_INT_STATUS_MASK_1);
1816
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301817 break;
1818 case SWR_ALERT:
1819 dev_dbg(swrm->dev,
1820 "device %d has pending interrupt\n",
1821 devnum);
1822 break;
1823 }
1824 break;
1825 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1826 dev_err_ratelimited(swrm->dev,
1827 "SWR bus clsh detected\n");
1828 break;
1829 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1830 dev_dbg(swrm->dev, "SWR read FIFO overflow\n");
1831 break;
1832 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1833 dev_dbg(swrm->dev, "SWR read FIFO underflow\n");
1834 break;
1835 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1836 dev_dbg(swrm->dev, "SWR write FIFO overflow\n");
1837 break;
1838 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1839 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1840 dev_err_ratelimited(swrm->dev,
1841 "SWR CMD error, fifo status 0x%x, flushing fifo\n",
1842 value);
1843 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1844 break;
1845 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301846 dev_err_ratelimited(swrm->dev, "SWR Port collision detected\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301847 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301848 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301849 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301850 break;
1851 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1852 dev_dbg(swrm->dev, "SWR read enable valid mismatch\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301853 swrm->intr_mask &=
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301854 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1855 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301856 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301857 break;
1858 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1859 complete(&swrm->broadcast);
1860 dev_dbg(swrm->dev, "SWR cmd id finished\n");
1861 break;
1862 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_AUTO_ENUM_FINISHED:
1863 break;
1864 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED:
1865 break;
1866 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL:
1867 break;
1868 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED:
1869 complete(&swrm->reset);
1870 break;
1871 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED:
1872 break;
1873 default:
1874 dev_err_ratelimited(swrm->dev,
1875 "SWR unknown interrupt\n");
1876 ret = IRQ_NONE;
1877 break;
1878 }
1879 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301880 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1881 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
Ramprasad Katkam83303512018-10-11 17:34:22 +05301882
1883 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301884 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301885
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301886 if (intr_sts_masked) {
Ramprasad Katkam83303512018-10-11 17:34:22 +05301887 dev_dbg(swrm->dev, "%s: new interrupt received\n", __func__);
1888 goto handle_irq;
1889 }
1890
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301891 mutex_lock(&swrm->reslock);
1892 swrm_clk_request(swrm, false);
1893 mutex_unlock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301894exit:
Ramprasad Katkam57349872018-11-11 18:34:57 +05301895 swrm_unlock_sleep(swrm);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07001896 trace_printk("%s exit\n", __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301897 return ret;
1898}
1899
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301900static irqreturn_t swr_mstr_interrupt_v2(int irq, void *dev)
1901{
1902 struct swr_mstr_ctrl *swrm = dev;
1903 u32 value, intr_sts, intr_sts_masked;
1904 u32 temp = 0;
1905 u32 status, chg_sts, i;
1906 u8 devnum = 0;
1907 int ret = IRQ_HANDLED;
1908 struct swr_device *swr_dev;
1909 struct swr_master *mstr = &swrm->master;
1910
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07001911 trace_printk("%s enter\n", __func__);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301912 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1913 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1914 return IRQ_NONE;
1915 }
1916
1917 mutex_lock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301918 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
1919 ret = IRQ_NONE;
1920 goto exit;
1921 }
1922 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
1923 ret = IRQ_NONE;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301924 goto err_audio_hw_vote;
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001925 }
Karthikeyan Mani4bee1db2019-09-18 17:58:41 -07001926 ret = swrm_clk_request(swrm, true);
1927 if (ret) {
1928 dev_err(dev, "%s: swrm clk failed\n", __func__);
1929 ret = IRQ_NONE;
1930 goto err_audio_core_vote;
1931 }
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301932 mutex_unlock(&swrm->reslock);
1933
1934 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1935 intr_sts_masked = intr_sts & swrm->intr_mask;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301936
1937 dev_dbg(swrm->dev, "%s: status: 0x%x \n", __func__, intr_sts_masked);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07001938 trace_printk("%s: status: 0x%x \n", __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301939handle_irq:
1940 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
1941 value = intr_sts_masked & (1 << i);
1942 if (!value)
1943 continue;
1944
1945 switch (value) {
1946 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1947 dev_dbg(swrm->dev, "%s: Trigger irq to slave device\n",
1948 __func__);
1949 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1950 ret = swrm_find_alert_slave(swrm, status, &devnum);
1951 if (ret) {
1952 dev_err_ratelimited(swrm->dev,
1953 "%s: no slave alert found.spurious interrupt\n",
1954 __func__);
1955 break;
1956 }
1957 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1958 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1959 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1960 SWRS_SCP_INT_STATUS_CLEAR_1);
1961 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1962 SWRS_SCP_INT_STATUS_CLEAR_1);
1963
1964
1965 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1966 if (swr_dev->dev_num != devnum)
1967 continue;
1968 if (swr_dev->slave_irq) {
1969 do {
Meng Wang31a7ef12019-12-18 10:36:29 +08001970 swr_dev->slave_irq_pending = 0;
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301971 handle_nested_irq(
1972 irq_find_mapping(
1973 swr_dev->slave_irq, 0));
1974 } while (swr_dev->slave_irq_pending);
1975 }
1976
1977 }
1978 break;
1979 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1980 dev_dbg(swrm->dev, "%s: SWR new slave attached\n",
1981 __func__);
1982 break;
1983 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1984 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
Laxminath Kasam44cedb82019-11-20 17:37:23 +05301985 swrm_enable_slave_irq(swrm);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301986 if (status == swrm->slave_status) {
1987 dev_dbg(swrm->dev,
1988 "%s: No change in slave status: %d\n",
1989 __func__, status);
1990 break;
1991 }
1992 chg_sts = swrm_check_slave_change_status(swrm, status,
1993 &devnum);
1994 switch (chg_sts) {
1995 case SWR_NOT_PRESENT:
1996 dev_dbg(swrm->dev,
1997 "%s: device %d got detached\n",
1998 __func__, devnum);
Vatsal Bucha21ba0772020-02-28 20:36:46 +05301999 if (devnum == 0) {
2000 /*
2001 * enable host irq if device 0 detached
2002 * as hw will mask host_irq at slave
2003 * but will not unmask it afterwards.
2004 */
2005 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
2006 SWRS_SCP_INT_STATUS_CLEAR_1);
2007 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
2008 SWRS_SCP_INT_STATUS_MASK_1);
2009 }
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302010 break;
2011 case SWR_ATTACHED_OK:
2012 dev_dbg(swrm->dev,
2013 "%s: device %d got attached\n",
2014 __func__, devnum);
2015 /* enable host irq from slave device*/
2016 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
2017 SWRS_SCP_INT_STATUS_CLEAR_1);
2018 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
2019 SWRS_SCP_INT_STATUS_MASK_1);
2020
2021 break;
2022 case SWR_ALERT:
2023 dev_dbg(swrm->dev,
2024 "%s: device %d has pending interrupt\n",
2025 __func__, devnum);
2026 break;
2027 }
2028 break;
2029 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
2030 dev_err_ratelimited(swrm->dev,
2031 "%s: SWR bus clsh detected\n",
2032 __func__);
Aditya Bavanarif0106d92020-01-31 17:01:21 +05302033 swrm->intr_mask &=
2034 ~SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET;
2035 swr_master_write(swrm,
2036 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
2037 swrm->intr_mask);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302038 break;
2039 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
Vatsal Buchabd68a7c2020-06-08 10:41:56 +05302040 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
2041 dev_err(swrm->dev,
2042 "%s: SWR read FIFO overflow fifo status 0x%x\n",
2043 __func__, value);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302044 break;
2045 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
Vatsal Buchabd68a7c2020-06-08 10:41:56 +05302046 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
2047 dev_err(swrm->dev,
2048 "%s: SWR read FIFO underflow fifo status 0x%x\n",
2049 __func__, value);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302050 break;
2051 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
Vatsal Buchabd68a7c2020-06-08 10:41:56 +05302052 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
2053 dev_err(swrm->dev,
2054 "%s: SWR write FIFO overflow fifo status %x\n",
2055 __func__, value);
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302056 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302057 break;
2058 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
2059 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
2060 dev_err_ratelimited(swrm->dev,
2061 "%s: SWR CMD error, fifo status 0x%x, flushing fifo\n",
2062 __func__, value);
2063 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
2064 break;
2065 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
2066 dev_err_ratelimited(swrm->dev,
2067 "%s: SWR Port collision detected\n",
2068 __func__);
2069 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
2070 swr_master_write(swrm,
2071 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
2072 break;
2073 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
2074 dev_dbg(swrm->dev,
2075 "%s: SWR read enable valid mismatch\n",
2076 __func__);
2077 swrm->intr_mask &=
2078 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
2079 swr_master_write(swrm,
2080 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
2081 break;
2082 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
2083 complete(&swrm->broadcast);
2084 dev_dbg(swrm->dev, "%s: SWR cmd id finished\n",
2085 __func__);
2086 break;
2087 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED_V2:
2088 break;
2089 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL_V2:
2090 break;
2091 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED_V2:
Laxminath Kasame2291972019-11-08 14:51:59 +05302092 swrm_check_link_status(swrm, 0x1);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302093 break;
2094 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED_V2:
2095 break;
2096 case SWRM_INTERRUPT_STATUS_EXT_CLK_STOP_WAKEUP:
2097 if (swrm->state == SWR_MSTR_UP)
2098 dev_dbg(swrm->dev,
2099 "%s:SWR Master is already up\n",
2100 __func__);
2101 else
2102 dev_err_ratelimited(swrm->dev,
2103 "%s: SWR wokeup during clock stop\n",
2104 __func__);
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05302105 /* It might be possible the slave device gets reset
2106 * and slave interrupt gets missed. So re-enable
2107 * Host IRQ and process slave pending
2108 * interrupts, if any.
2109 */
2110 swrm_enable_slave_irq(swrm);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302111 break;
2112 default:
2113 dev_err_ratelimited(swrm->dev,
2114 "%s: SWR unknown interrupt value: %d\n",
2115 __func__, value);
2116 ret = IRQ_NONE;
2117 break;
2118 }
2119 }
2120 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
2121 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
2122
2123 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
2124 intr_sts_masked = intr_sts & swrm->intr_mask;
2125
2126 if (intr_sts_masked) {
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05302127 dev_dbg(swrm->dev, "%s: new interrupt received 0x%x\n",
2128 __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302129 goto handle_irq;
2130 }
2131
2132 mutex_lock(&swrm->reslock);
2133 swrm_clk_request(swrm, false);
Karthikeyan Mani4bee1db2019-09-18 17:58:41 -07002134err_audio_core_vote:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302135 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
Sudheer Papothi06f43412019-07-09 03:32:54 +05302136
2137err_audio_hw_vote:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302138 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07002139exit:
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302140 mutex_unlock(&swrm->reslock);
2141 swrm_unlock_sleep(swrm);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07002142 trace_printk("%s exit\n", __func__);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302143 return ret;
2144}
2145
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302146static irqreturn_t swrm_wakeup_interrupt(int irq, void *dev)
2147{
2148 struct swr_mstr_ctrl *swrm = dev;
2149 int ret = IRQ_HANDLED;
2150
2151 if (!swrm || !(swrm->dev)) {
2152 pr_err("%s: swrm or dev is null\n", __func__);
2153 return IRQ_NONE;
2154 }
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302155
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07002156 trace_printk("%s enter\n", __func__);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302157 mutex_lock(&swrm->devlock);
2158 if (!swrm->dev_up) {
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302159 if (swrm->wake_irq > 0) {
2160 if (unlikely(!irq_get_irq_data(swrm->wake_irq))) {
2161 pr_err("%s: irq data is NULL\n", __func__);
2162 mutex_unlock(&swrm->devlock);
2163 return IRQ_NONE;
2164 }
2165 mutex_lock(&swrm->irq_lock);
2166 if (!irqd_irq_disabled(
2167 irq_get_irq_data(swrm->wake_irq)))
2168 disable_irq_nosync(swrm->wake_irq);
2169 mutex_unlock(&swrm->irq_lock);
2170 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302171 mutex_unlock(&swrm->devlock);
2172 return ret;
2173 }
2174 mutex_unlock(&swrm->devlock);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05302175 if (unlikely(swrm_lock_sleep(swrm) == false)) {
2176 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
2177 goto exit;
2178 }
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302179 if (swrm->wake_irq > 0) {
2180 if (unlikely(!irq_get_irq_data(swrm->wake_irq))) {
2181 pr_err("%s: irq data is NULL\n", __func__);
2182 return IRQ_NONE;
2183 }
2184 mutex_lock(&swrm->irq_lock);
2185 if (!irqd_irq_disabled(
2186 irq_get_irq_data(swrm->wake_irq)))
2187 disable_irq_nosync(swrm->wake_irq);
2188 mutex_unlock(&swrm->irq_lock);
2189 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302190 pm_runtime_get_sync(swrm->dev);
2191 pm_runtime_mark_last_busy(swrm->dev);
2192 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05302193 swrm_unlock_sleep(swrm);
2194exit:
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07002195 trace_printk("%s exit\n", __func__);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302196 return ret;
2197}
2198
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302199static void swrm_wakeup_work(struct work_struct *work)
2200{
2201 struct swr_mstr_ctrl *swrm;
2202
2203 swrm = container_of(work, struct swr_mstr_ctrl,
2204 wakeup_work);
2205 if (!swrm || !(swrm->dev)) {
2206 pr_err("%s: swrm or dev is null\n", __func__);
2207 return;
2208 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302209
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07002210 trace_printk("%s enter\n", __func__);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302211 mutex_lock(&swrm->devlock);
2212 if (!swrm->dev_up) {
2213 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302214 goto exit;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302215 }
2216 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302217 if (unlikely(swrm_lock_sleep(swrm) == false)) {
2218 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
2219 goto exit;
2220 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302221 pm_runtime_get_sync(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302222 pm_runtime_mark_last_busy(swrm->dev);
2223 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302224 swrm_unlock_sleep(swrm);
2225exit:
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07002226 trace_printk("%s exit\n", __func__);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302227 pm_relax(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302228}
2229
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302230static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
2231{
2232 u32 val;
2233
2234 swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
2235 val = (swrm->slave_status >> (devnum * 2));
2236 val &= SWRM_MCP_SLV_STATUS_MASK;
2237 return val;
2238}
2239
2240static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
2241 u8 *dev_num)
2242{
2243 int i;
2244 u64 id = 0;
2245 int ret = -EINVAL;
2246 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
2247 struct swr_device *swr_dev;
2248 u32 num_dev = 0;
2249
2250 if (!swrm) {
2251 pr_err("%s: Invalid handle to swr controller\n",
2252 __func__);
2253 return ret;
2254 }
2255 if (swrm->num_dev)
2256 num_dev = swrm->num_dev;
2257 else
2258 num_dev = mstr->num_dev;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302259
2260 mutex_lock(&swrm->devlock);
2261 if (!swrm->dev_up) {
2262 mutex_unlock(&swrm->devlock);
2263 return ret;
2264 }
2265 mutex_unlock(&swrm->devlock);
2266
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302267 pm_runtime_get_sync(swrm->dev);
2268 for (i = 1; i < (num_dev + 1); i++) {
2269 id = ((u64)(swr_master_read(swrm,
2270 SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
2271 id |= swr_master_read(swrm,
2272 SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302273
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302274 /*
2275 * As pm_runtime_get_sync() brings all slaves out of reset
2276 * update logical device number for all slaves.
2277 */
2278 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2279 if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
2280 u32 status = swrm_get_device_status(swrm, i);
2281
2282 if ((status == 0x01) || (status == 0x02)) {
2283 swr_dev->dev_num = i;
2284 if ((id & SWR_DEV_ID_MASK) == dev_id) {
2285 *dev_num = i;
2286 ret = 0;
2287 }
2288 dev_dbg(swrm->dev,
Xiao Lid8bb93c2020-01-07 12:59:05 +08002289 "%s: devnum %d is assigned for dev addr %llx\n",
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302290 __func__, i, swr_dev->addr);
2291 }
2292 }
2293 }
2294 }
2295 if (ret)
2296 dev_err(swrm->dev, "%s: device 0x%llx is not ready\n",
2297 __func__, dev_id);
2298
2299 pm_runtime_mark_last_busy(swrm->dev);
2300 pm_runtime_put_autosuspend(swrm->dev);
2301 return ret;
2302}
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302303
2304static void swrm_device_wakeup_vote(struct swr_master *mstr)
2305{
2306 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
2307
2308 if (!swrm) {
2309 pr_err("%s: Invalid handle to swr controller\n",
2310 __func__);
2311 return;
2312 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302313 if (unlikely(swrm_lock_sleep(swrm) == false)) {
2314 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
2315 return;
2316 }
Aditya Bavanarieb044612019-12-22 17:14:15 +05302317 mutex_lock(&swrm->reslock);
2318 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true))
2319 dev_err(swrm->dev, "%s:lpass core hw enable failed\n",
2320 __func__);
2321 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true))
2322 dev_err(swrm->dev, "%s:lpass audio hw enable failed\n",
2323 __func__);
2324 mutex_unlock(&swrm->reslock);
2325
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302326 pm_runtime_get_sync(swrm->dev);
2327}
2328
2329static void swrm_device_wakeup_unvote(struct swr_master *mstr)
2330{
2331 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
2332
2333 if (!swrm) {
2334 pr_err("%s: Invalid handle to swr controller\n",
2335 __func__);
2336 return;
2337 }
2338 pm_runtime_mark_last_busy(swrm->dev);
2339 pm_runtime_put_autosuspend(swrm->dev);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302340
Aditya Bavanarieb044612019-12-22 17:14:15 +05302341 mutex_lock(&swrm->reslock);
2342 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2343 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
2344 mutex_unlock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302345
Ramprasad Katkam57349872018-11-11 18:34:57 +05302346 swrm_unlock_sleep(swrm);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302347}
2348
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302349static int swrm_master_init(struct swr_mstr_ctrl *swrm)
2350{
Laxminath Kasam36956422020-06-01 19:23:48 +05302351 int ret = 0, i = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302352 u32 val;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302353 u8 row_ctrl = SWR_ROW_50;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302354 u8 col_ctrl = SWR_MIN_COL;
2355 u8 ssp_period = 1;
2356 u8 retry_cmd_num = 3;
2357 u32 reg[SWRM_MAX_INIT_REG];
2358 u32 value[SWRM_MAX_INIT_REG];
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302359 u32 temp = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302360 int len = 0;
2361
Laxminath Kasam36956422020-06-01 19:23:48 +05302362 /* SW workaround to gate hw_ctl for SWR version >=1.6 */
2363 if (swrm->version >= SWRM_VERSION_1_6) {
2364 if (swrm->swrm_hctl_reg) {
2365 temp = ioread32(swrm->swrm_hctl_reg);
2366 temp &= 0xFFFFFFFD;
2367 iowrite32(temp, swrm->swrm_hctl_reg);
2368 usleep_range(500, 505);
2369 temp = ioread32(swrm->swrm_hctl_reg);
2370 dev_dbg(swrm->dev, "%s: hctl_reg val: 0x%x\n",
2371 __func__, temp);
2372 }
2373 }
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302374 ssp_period = swrm_get_ssp_period(swrm, SWRM_ROW_50,
2375 SWRM_COL_02, SWRM_FRAME_SYNC_SEL);
2376 dev_dbg(swrm->dev, "%s: ssp_period: %d\n", __func__, ssp_period);
2377
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302378 /* Clear Rows and Cols */
2379 val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
2380 (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302381 ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302382
2383 reg[len] = SWRM_MCP_FRAME_CTRL_BANK_ADDR(0);
2384 value[len++] = val;
2385
2386 /* Set Auto enumeration flag */
2387 reg[len] = SWRM_ENUMERATOR_CFG_ADDR;
2388 value[len++] = 1;
2389
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302390 /* Configure No pings */
2391 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2392 val &= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK;
2393 val |= (0x1f << SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT);
2394 reg[len] = SWRM_MCP_CFG_ADDR;
2395 value[len++] = val;
2396
2397 /* Configure number of retries of a read/write cmd */
2398 val = (retry_cmd_num << SWRM_CMD_FIFO_CFG_NUM_OF_CMD_RETRY_SHFT);
2399 reg[len] = SWRM_CMD_FIFO_CFG_ADDR;
2400 value[len++] = val;
2401
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302402 reg[len] = SWRM_MCP_BUS_CTRL_ADDR;
2403 value[len++] = 0x2;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302404
Ramprasad Katkam83303512018-10-11 17:34:22 +05302405 /* Set IRQ to PULSE */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302406 reg[len] = SWRM_COMP_CFG_ADDR;
Ramprasad Katkam83303512018-10-11 17:34:22 +05302407 value[len++] = 0x02;
2408
2409 reg[len] = SWRM_COMP_CFG_ADDR;
2410 value[len++] = 0x03;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302411
2412 reg[len] = SWRM_INTERRUPT_CLEAR;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302413 value[len++] = 0xFFFFFFFF;
2414
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302415 swrm->intr_mask = SWRM_INTERRUPT_STATUS_MASK;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302416 /* Mask soundwire interrupts */
2417 reg[len] = SWRM_INTERRUPT_MASK_ADDR;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302418 value[len++] = swrm->intr_mask;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302419
2420 reg[len] = SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302421 value[len++] = swrm->intr_mask;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302422
2423 swr_master_bulk_write(swrm, reg, value, len);
2424
Laxminath Kasamcafe0732019-11-20 17:31:58 +05302425 if (!swrm_check_link_status(swrm, 0x1)) {
2426 dev_err(swrm->dev,
2427 "%s: swr link failed to connect\n",
2428 __func__);
Laxminath Kasam36956422020-06-01 19:23:48 +05302429 for (i = 0; i < len; i++) {
2430 usleep_range(50, 55);
2431 dev_err(swrm->dev,
2432 "%s:reg:0x%x val:0x%x\n",
2433 __func__,
2434 reg[i], swr_master_read(swrm, reg[i]));
2435 }
Laxminath Kasamcafe0732019-11-20 17:31:58 +05302436 return -EINVAL;
2437 }
Sudheer Papothi63f48152018-11-15 01:08:03 +05302438 /*
2439 * For SWR master version 1.5.1, continue
2440 * execute on command ignore.
2441 */
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302442 /* Execute it for versions >= 1.5.1 */
2443 if (swrm->version >= SWRM_VERSION_1_5_1)
Sudheer Papothi63f48152018-11-15 01:08:03 +05302444 swr_master_write(swrm, SWRM_CMD_FIFO_CFG_ADDR,
2445 (swr_master_read(swrm,
2446 SWRM_CMD_FIFO_CFG_ADDR) | 0x80000000));
2447
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302448 return ret;
2449}
2450
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302451static int swrm_event_notify(struct notifier_block *self,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302452 unsigned long action, void *data)
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302453{
2454 struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302455 event_notifier);
2456
2457 if (!swrm || !(swrm->dev)) {
2458 pr_err("%s: swrm or dev is NULL\n", __func__);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302459 return -EINVAL;
2460 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302461 switch (action) {
2462 case MSM_AUD_DC_EVENT:
2463 schedule_work(&(swrm->dc_presence_work));
2464 break;
2465 case SWR_WAKE_IRQ_EVENT:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302466 if (swrm->ipc_wakeup && !swrm->ipc_wakeup_triggered) {
2467 swrm->ipc_wakeup_triggered = true;
Ramprasad Katkam57349872018-11-11 18:34:57 +05302468 pm_stay_awake(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302469 schedule_work(&swrm->wakeup_work);
Ramprasad Katkamcd61c6e2018-09-18 13:22:58 +05302470 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302471 break;
2472 default:
2473 dev_err(swrm->dev, "%s: invalid event type: %lu\n",
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302474 __func__, action);
2475 return -EINVAL;
2476 }
2477
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302478 return 0;
2479}
2480
2481static void swrm_notify_work_fn(struct work_struct *work)
2482{
2483 struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302484 dc_presence_work);
2485
2486 if (!swrm || !swrm->pdev) {
2487 pr_err("%s: swrm or pdev is NULL\n", __func__);
2488 return;
2489 }
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302490 swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL);
2491}
2492
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302493static int swrm_probe(struct platform_device *pdev)
2494{
2495 struct swr_mstr_ctrl *swrm;
2496 struct swr_ctrl_platform_data *pdata;
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302497 u32 i, num_ports, port_num, port_type, ch_mask, swrm_hctl_reg = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302498 u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0;
2499 int ret = 0;
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302500 struct clk *lpass_core_hw_vote = NULL;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302501 struct clk *lpass_core_audio = NULL;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302502
2503 /* Allocate soundwire master driver structure */
2504 swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl),
2505 GFP_KERNEL);
2506 if (!swrm) {
2507 ret = -ENOMEM;
2508 goto err_memory_fail;
2509 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302510 swrm->pdev = pdev;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302511 swrm->dev = &pdev->dev;
2512 platform_set_drvdata(pdev, swrm);
2513 swr_set_ctrl_data(&swrm->master, swrm);
2514 pdata = dev_get_platdata(&pdev->dev);
2515 if (!pdata) {
2516 dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
2517 __func__);
2518 ret = -EINVAL;
2519 goto err_pdata_fail;
2520 }
2521 swrm->handle = (void *)pdata->handle;
2522 if (!swrm->handle) {
2523 dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
2524 __func__);
2525 ret = -EINVAL;
2526 goto err_pdata_fail;
2527 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302528 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr_master_id",
2529 &swrm->master_id);
2530 if (ret) {
2531 dev_err(&pdev->dev, "%s: failed to get master id\n", __func__);
2532 goto err_pdata_fail;
2533 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302534 if (!(of_property_read_u32(pdev->dev.of_node,
2535 "swrm-io-base", &swrm->swrm_base_reg)))
2536 ret = of_property_read_u32(pdev->dev.of_node,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302537 "swrm-io-base", &swrm->swrm_base_reg);
2538 if (!swrm->swrm_base_reg) {
2539 swrm->read = pdata->read;
2540 if (!swrm->read) {
2541 dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
2542 __func__);
2543 ret = -EINVAL;
2544 goto err_pdata_fail;
2545 }
2546 swrm->write = pdata->write;
2547 if (!swrm->write) {
2548 dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
2549 __func__);
2550 ret = -EINVAL;
2551 goto err_pdata_fail;
2552 }
2553 swrm->bulk_write = pdata->bulk_write;
2554 if (!swrm->bulk_write) {
2555 dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
2556 __func__);
2557 ret = -EINVAL;
2558 goto err_pdata_fail;
2559 }
2560 } else {
2561 swrm->swrm_dig_base = devm_ioremap(&pdev->dev,
2562 swrm->swrm_base_reg, SWRM_MAX_REGISTER);
2563 }
2564
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07002565 swrm->core_vote = pdata->core_vote;
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302566 if (!(of_property_read_u32(pdev->dev.of_node,
2567 "qcom,swrm-hctl-reg", &swrm_hctl_reg)))
2568 swrm->swrm_hctl_reg = devm_ioremap(&pdev->dev,
2569 swrm_hctl_reg, 0x4);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302570 swrm->clk = pdata->clk;
2571 if (!swrm->clk) {
2572 dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
2573 __func__);
2574 ret = -EINVAL;
2575 goto err_pdata_fail;
2576 }
Laxminath Kasamea6cbee2020-04-28 00:02:32 +05302577 swrm->pinctrl_setup = pdata->pinctrl_setup;
2578
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302579 if (of_property_read_u32(pdev->dev.of_node,
2580 "qcom,swr-clock-stop-mode0",
2581 &swrm->clk_stop_mode0_supp)) {
2582 swrm->clk_stop_mode0_supp = FALSE;
2583 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302584
2585 ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
2586 &swrm->num_dev);
2587 if (ret) {
2588 dev_dbg(&pdev->dev, "%s: Looking up %s property failed\n",
2589 __func__, "qcom,swr-num-dev");
2590 } else {
Sudheer Papothiae5c3632019-11-27 06:52:06 +05302591 if (swrm->num_dev > SWRM_NUM_AUTO_ENUM_SLAVES) {
Ramprasad Katkam57349872018-11-11 18:34:57 +05302592 dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
Sudheer Papothiae5c3632019-11-27 06:52:06 +05302593 __func__, swrm->num_dev,
2594 SWRM_NUM_AUTO_ENUM_SLAVES);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302595 ret = -EINVAL;
2596 goto err_pdata_fail;
2597 }
2598 }
2599
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302600 /* Parse soundwire port mapping */
2601 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
2602 &num_ports);
2603 if (ret) {
2604 dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
2605 goto err_pdata_fail;
2606 }
2607 swrm->num_ports = num_ports;
2608
2609 if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
2610 &map_size)) {
2611 dev_err(swrm->dev, "missing port mapping\n");
2612 goto err_pdata_fail;
2613 }
2614
2615 map_length = map_size / (3 * sizeof(u32));
2616 if (num_ports > SWR_MSTR_PORT_LEN) {
2617 dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
2618 __func__);
2619 ret = -EINVAL;
2620 goto err_pdata_fail;
2621 }
2622 temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
2623
2624 if (!temp) {
2625 ret = -ENOMEM;
2626 goto err_pdata_fail;
2627 }
2628 ret = of_property_read_u32_array(pdev->dev.of_node,
2629 "qcom,swr-port-mapping", temp, 3 * map_length);
2630 if (ret) {
2631 dev_err(swrm->dev, "%s: Failed to read port mapping\n",
2632 __func__);
2633 goto err_pdata_fail;
2634 }
2635
2636 for (i = 0; i < map_length; i++) {
2637 port_num = temp[3 * i];
2638 port_type = temp[3 * i + 1];
2639 ch_mask = temp[3 * i + 2];
2640
2641 if (port_num != old_port_num)
2642 ch_iter = 0;
2643 swrm->port_mapping[port_num][ch_iter].port_type = port_type;
2644 swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
2645 old_port_num = port_num;
2646 }
2647 devm_kfree(&pdev->dev, temp);
2648
2649 swrm->reg_irq = pdata->reg_irq;
2650 swrm->master.read = swrm_read;
2651 swrm->master.write = swrm_write;
2652 swrm->master.bulk_write = swrm_bulk_write;
2653 swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
2654 swrm->master.connect_port = swrm_connect_port;
2655 swrm->master.disconnect_port = swrm_disconnect_port;
2656 swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
2657 swrm->master.remove_from_group = swrm_remove_from_group;
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302658 swrm->master.device_wakeup_vote = swrm_device_wakeup_vote;
2659 swrm->master.device_wakeup_unvote = swrm_device_wakeup_unvote;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302660 swrm->master.dev.parent = &pdev->dev;
2661 swrm->master.dev.of_node = pdev->dev.of_node;
2662 swrm->master.num_port = 0;
2663 swrm->rcmd_id = 0;
2664 swrm->wcmd_id = 0;
2665 swrm->slave_status = 0;
2666 swrm->num_rx_chs = 0;
2667 swrm->clk_ref_count = 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302668 swrm->swr_irq_wakeup_capable = 0;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302669 swrm->mclk_freq = MCLK_FREQ;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302670 swrm->bus_clk = MCLK_FREQ;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302671 swrm->dev_up = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302672 swrm->state = SWR_MSTR_UP;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302673 swrm->ipc_wakeup = false;
2674 swrm->ipc_wakeup_triggered = false;
Vatsal Bucha687f9822020-04-01 18:21:41 +05302675 swrm->disable_div2_clk_switch = FALSE;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302676 init_completion(&swrm->reset);
2677 init_completion(&swrm->broadcast);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302678 init_completion(&swrm->clk_off_complete);
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302679 mutex_init(&swrm->irq_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302680 mutex_init(&swrm->mlock);
2681 mutex_init(&swrm->reslock);
2682 mutex_init(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302683 mutex_init(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302684 mutex_init(&swrm->clklock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302685 mutex_init(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302686 mutex_init(&swrm->pm_lock);
2687 swrm->wlock_holders = 0;
2688 swrm->pm_state = SWRM_PM_SLEEPABLE;
2689 init_waitqueue_head(&swrm->pm_wq);
2690 pm_qos_add_request(&swrm->pm_qos_req,
2691 PM_QOS_CPU_DMA_LATENCY,
2692 PM_QOS_DEFAULT_VALUE);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302693
2694 for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++)
2695 INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
2696
Vatsal Bucha687f9822020-04-01 18:21:41 +05302697 if (of_property_read_u32(pdev->dev.of_node,
2698 "qcom,disable-div2-clk-switch",
2699 &swrm->disable_div2_clk_switch)) {
2700 swrm->disable_div2_clk_switch = FALSE;
2701 }
2702
Sudheer Papothi06f43412019-07-09 03:32:54 +05302703 /* Register LPASS core hw vote */
2704 lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
2705 if (IS_ERR(lpass_core_hw_vote)) {
2706 ret = PTR_ERR(lpass_core_hw_vote);
2707 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2708 __func__, "lpass_core_hw_vote", ret);
2709 lpass_core_hw_vote = NULL;
2710 ret = 0;
2711 }
2712 swrm->lpass_core_hw_vote = lpass_core_hw_vote;
2713
2714 /* Register LPASS audio core vote */
2715 lpass_core_audio = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
2716 if (IS_ERR(lpass_core_audio)) {
2717 ret = PTR_ERR(lpass_core_audio);
2718 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2719 __func__, "lpass_core_audio", ret);
2720 lpass_core_audio = NULL;
2721 ret = 0;
2722 }
2723 swrm->lpass_core_audio = lpass_core_audio;
2724
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302725 if (swrm->reg_irq) {
2726 ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
2727 SWR_IRQ_REGISTER);
2728 if (ret) {
2729 dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
2730 __func__, ret);
2731 goto err_irq_fail;
2732 }
2733 } else {
2734 swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
2735 if (swrm->irq < 0) {
2736 dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
2737 __func__, swrm->irq);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302738 goto err_irq_fail;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302739 }
2740
2741 ret = request_threaded_irq(swrm->irq, NULL,
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302742 swr_mstr_interrupt_v2,
Ramprasad Katkam83303512018-10-11 17:34:22 +05302743 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302744 "swr_master_irq", swrm);
2745 if (ret) {
2746 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2747 __func__, ret);
2748 goto err_irq_fail;
2749 }
2750
2751 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302752 /* Make inband tx interrupts as wakeup capable for slave irq */
2753 ret = of_property_read_u32(pdev->dev.of_node,
2754 "qcom,swr-mstr-irq-wakeup-capable",
2755 &swrm->swr_irq_wakeup_capable);
2756 if (ret)
2757 dev_dbg(swrm->dev, "%s: swrm irq wakeup capable not defined\n",
2758 __func__);
2759 if (swrm->swr_irq_wakeup_capable)
2760 irq_set_irq_wake(swrm->irq, 1);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302761 ret = swr_register_master(&swrm->master);
2762 if (ret) {
2763 dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
2764 goto err_mstr_fail;
2765 }
2766
2767 /* Add devices registered with board-info as the
2768 * controller will be up now
2769 */
2770 swr_master_add_boarddevices(&swrm->master);
2771 mutex_lock(&swrm->mlock);
2772 swrm_clk_request(swrm, true);
Laxminath Kasam4696fff2019-11-26 16:07:11 +05302773 swrm->version = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302774 ret = swrm_master_init(swrm);
2775 if (ret < 0) {
2776 dev_err(&pdev->dev,
2777 "%s: Error in master Initialization , err %d\n",
2778 __func__, ret);
2779 mutex_unlock(&swrm->mlock);
Laxminath Kasam36956422020-06-01 19:23:48 +05302780 ret = -EPROBE_DEFER;
Laxminath Kasamec8c9092019-12-17 13:12:58 +05302781 goto err_mstr_init_fail;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302782 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302783
2784 mutex_unlock(&swrm->mlock);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302785 INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302786
2787 if (pdev->dev.of_node)
2788 of_register_swr_devices(&swrm->master);
2789
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302790#ifdef CONFIG_DEBUG_FS
2791 swrm->debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
2792 if (!IS_ERR(swrm->debugfs_swrm_dent)) {
2793 swrm->debugfs_peek = debugfs_create_file("swrm_peek",
2794 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2795 (void *) swrm, &swrm_debug_read_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302796
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302797 swrm->debugfs_poke = debugfs_create_file("swrm_poke",
2798 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2799 (void *) swrm, &swrm_debug_write_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302800
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302801 swrm->debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
2802 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2803 (void *) swrm,
2804 &swrm_debug_dump_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302805 }
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302806#endif
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302807 ret = device_init_wakeup(swrm->dev, true);
2808 if (ret) {
2809 dev_err(swrm->dev, "Device wakeup init failed: %d\n", ret);
2810 goto err_irq_wakeup_fail;
2811 }
2812
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302813 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
2814 pm_runtime_use_autosuspend(&pdev->dev);
2815 pm_runtime_set_active(&pdev->dev);
2816 pm_runtime_enable(&pdev->dev);
2817 pm_runtime_mark_last_busy(&pdev->dev);
2818
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302819 INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn);
2820 swrm->event_notifier.notifier_call = swrm_event_notify;
2821 msm_aud_evt_register_client(&swrm->event_notifier);
2822
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302823 return 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302824err_irq_wakeup_fail:
2825 device_init_wakeup(swrm->dev, false);
Laxminath Kasamec8c9092019-12-17 13:12:58 +05302826err_mstr_init_fail:
2827 swr_unregister_master(&swrm->master);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302828err_mstr_fail:
Laxminath Kasam36956422020-06-01 19:23:48 +05302829 if (swrm->reg_irq) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302830 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2831 swrm, SWR_IRQ_FREE);
Laxminath Kasam36956422020-06-01 19:23:48 +05302832 } else if (swrm->irq) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302833 free_irq(swrm->irq, swrm);
Laxminath Kasam36956422020-06-01 19:23:48 +05302834 irqd_set_trigger_type(
2835 irq_get_irq_data(swrm->irq),
2836 IRQ_TYPE_NONE);
2837 }
2838 if (swrm->swr_irq_wakeup_capable)
2839 irq_set_irq_wake(swrm->irq, 0);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302840err_irq_fail:
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302841 mutex_destroy(&swrm->irq_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302842 mutex_destroy(&swrm->mlock);
2843 mutex_destroy(&swrm->reslock);
2844 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302845 mutex_destroy(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302846 mutex_destroy(&swrm->clklock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302847 mutex_destroy(&swrm->pm_lock);
2848 pm_qos_remove_request(&swrm->pm_qos_req);
2849
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302850err_pdata_fail:
2851err_memory_fail:
2852 return ret;
2853}
2854
2855static int swrm_remove(struct platform_device *pdev)
2856{
2857 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2858
Laxminath Kasam36956422020-06-01 19:23:48 +05302859 if (swrm->reg_irq) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302860 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2861 swrm, SWR_IRQ_FREE);
Laxminath Kasam36956422020-06-01 19:23:48 +05302862 } else if (swrm->irq) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302863 free_irq(swrm->irq, swrm);
Laxminath Kasam36956422020-06-01 19:23:48 +05302864 irqd_set_trigger_type(
2865 irq_get_irq_data(swrm->irq),
2866 IRQ_TYPE_NONE);
2867 } else if (swrm->wake_irq > 0) {
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302868 free_irq(swrm->wake_irq, swrm);
Laxminath Kasam36956422020-06-01 19:23:48 +05302869 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302870 if (swrm->swr_irq_wakeup_capable)
2871 irq_set_irq_wake(swrm->irq, 0);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302872 cancel_work_sync(&swrm->wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302873 pm_runtime_disable(&pdev->dev);
2874 pm_runtime_set_suspended(&pdev->dev);
2875 swr_unregister_master(&swrm->master);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302876 msm_aud_evt_unregister_client(&swrm->event_notifier);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302877 device_init_wakeup(swrm->dev, false);
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302878 mutex_destroy(&swrm->irq_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302879 mutex_destroy(&swrm->mlock);
2880 mutex_destroy(&swrm->reslock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302881 mutex_destroy(&swrm->iolock);
2882 mutex_destroy(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302883 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302884 mutex_destroy(&swrm->pm_lock);
2885 pm_qos_remove_request(&swrm->pm_qos_req);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302886 devm_kfree(&pdev->dev, swrm);
2887 return 0;
2888}
2889
2890static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
2891{
2892 u32 val;
2893
2894 dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
2895 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD);
2896 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2897 val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK;
2898 swr_master_write(swrm, SWRM_MCP_CFG_ADDR, val);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302899
2900 return 0;
2901}
2902
2903#ifdef CONFIG_PM
2904static int swrm_runtime_resume(struct device *dev)
2905{
2906 struct platform_device *pdev = to_platform_device(dev);
2907 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2908 int ret = 0;
Vatsal Buchae50b5002019-09-19 14:32:20 +05302909 bool swrm_clk_req_err = false;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302910 bool hw_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302911 struct swr_master *mstr = &swrm->master;
2912 struct swr_device *swr_dev;
2913
2914 dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
2915 __func__, swrm->state);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07002916 trace_printk("%s: pm_runtime: resume, state:%d\n",
2917 __func__, swrm->state);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302918 mutex_lock(&swrm->reslock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302919
Sudheer Papothi384addd2019-06-14 02:26:52 +05302920 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2921 dev_err(dev, "%s:lpass core hw enable failed\n",
2922 __func__);
2923 hw_core_err = true;
2924 }
2925 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2926 dev_err(dev, "%s:lpass audio hw enable failed\n",
2927 __func__);
Laxminath Kasamea6cbee2020-04-28 00:02:32 +05302928 swrm->aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002929 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302930
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302931 if ((swrm->state == SWR_MSTR_DOWN) ||
2932 (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) {
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302933 if (swrm->clk_stop_mode0_supp) {
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302934 if (swrm->wake_irq > 0) {
2935 if (unlikely(!irq_get_irq_data
2936 (swrm->wake_irq))) {
2937 pr_err("%s: irq data is NULL\n",
2938 __func__);
2939 mutex_unlock(&swrm->reslock);
2940 return IRQ_NONE;
2941 }
2942 mutex_lock(&swrm->irq_lock);
2943 if (!irqd_irq_disabled(
2944 irq_get_irq_data(swrm->wake_irq)))
2945 disable_irq_nosync(swrm->wake_irq);
2946 mutex_unlock(&swrm->irq_lock);
Laxminath Kasamea6cbee2020-04-28 00:02:32 +05302947 if (swrm->dmic_sva && swrm->pinctrl_setup)
2948 swrm->pinctrl_setup(swrm->handle,
2949 false);
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302950 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302951 if (swrm->ipc_wakeup)
2952 msm_aud_evt_blocking_notifier_call_chain(
2953 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302954 }
2955
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302956 if (swrm_clk_request(swrm, true)) {
2957 /*
2958 * Set autosuspend timer to 1 for
2959 * master to enter into suspend.
2960 */
Vatsal Buchae50b5002019-09-19 14:32:20 +05302961 swrm_clk_req_err = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302962 goto exit;
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302963 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302964 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302965 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2966 ret = swr_device_up(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302967 if (ret == -ENODEV) {
2968 dev_dbg(dev,
2969 "%s slave device up not implemented\n",
2970 __func__);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07002971 trace_printk(
2972 "%s slave device up not implemented\n",
2973 __func__);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302974 ret = 0;
2975 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302976 dev_err(dev,
2977 "%s: failed to wakeup swr dev %d\n",
2978 __func__, swr_dev->dev_num);
2979 swrm_clk_request(swrm, false);
2980 goto exit;
2981 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302982 }
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05302983 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2984 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2985 swrm_master_init(swrm);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302986 /* wait for hw enumeration to complete */
2987 usleep_range(100, 105);
Laxminath Kasame2291972019-11-08 14:51:59 +05302988 if (!swrm_check_link_status(swrm, 0x1))
Laxminath Kasam696b14b2019-12-03 22:07:34 +05302989 dev_dbg(dev, "%s:failed in connecting, ssr?\n",
2990 __func__);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302991 swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, 0x0,
2992 SWRS_SCP_INT_STATUS_MASK_1);
Karthikeyan Manif6821902019-05-21 17:31:24 -07002993 if (swrm->state == SWR_MSTR_SSR) {
2994 mutex_unlock(&swrm->reslock);
2995 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
2996 mutex_lock(&swrm->reslock);
2997 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302998 } else {
2999 /*wake up from clock stop*/
3000 swr_master_write(swrm, SWRM_MCP_BUS_CTRL_ADDR, 0x2);
Sudheer Papothi55fa5972019-09-28 02:50:27 +05303001 /* clear and enable bus clash interrupt */
3002 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x08);
3003 swrm->intr_mask |= 0x08;
3004 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR,
3005 swrm->intr_mask);
3006 swr_master_write(swrm,
3007 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
3008 swrm->intr_mask);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05303009 usleep_range(100, 105);
Laxminath Kasame2291972019-11-08 14:51:59 +05303010 if (!swrm_check_link_status(swrm, 0x1))
Laxminath Kasam696b14b2019-12-03 22:07:34 +05303011 dev_dbg(dev, "%s:failed in connecting, ssr?\n",
3012 __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303013 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05303014 swrm->state = SWR_MSTR_UP;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303015 }
3016exit:
Laxminath Kasamea6cbee2020-04-28 00:02:32 +05303017 if (ret && !swrm->aud_core_err)
Sudheer Papothi384addd2019-06-14 02:26:52 +05303018 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
3019 if (!hw_core_err)
3020 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Vatsal Buchae50b5002019-09-19 14:32:20 +05303021 if (swrm_clk_req_err)
3022 pm_runtime_set_autosuspend_delay(&pdev->dev,
3023 ERR_AUTO_SUSPEND_TIMER_VAL);
3024 else
3025 pm_runtime_set_autosuspend_delay(&pdev->dev,
3026 auto_suspend_timer);
Vatsal Buchabd68a7c2020-06-08 10:41:56 +05303027 if (swrm->req_clk_switch)
3028 swrm->req_clk_switch = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303029 mutex_unlock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05303030
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003031 trace_printk("%s: pm_runtime: resume done, state:%d\n",
3032 __func__, swrm->state);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303033 return ret;
3034}
3035
3036static int swrm_runtime_suspend(struct device *dev)
3037{
3038 struct platform_device *pdev = to_platform_device(dev);
3039 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3040 int ret = 0;
Sudheer Papothi384addd2019-06-14 02:26:52 +05303041 bool hw_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303042 struct swr_master *mstr = &swrm->master;
3043 struct swr_device *swr_dev;
3044 int current_state = 0;
3045
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003046 trace_printk("%s: pm_runtime: suspend state: %d\n",
3047 __func__, swrm->state);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303048 dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
3049 __func__, swrm->state);
3050 mutex_lock(&swrm->reslock);
3051 mutex_lock(&swrm->force_down_lock);
3052 current_state = swrm->state;
3053 mutex_unlock(&swrm->force_down_lock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05303054
3055 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
3056 dev_err(dev, "%s:lpass core hw enable failed\n",
3057 __func__);
3058 hw_core_err = true;
3059 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05303060
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05303061 if ((current_state == SWR_MSTR_UP) ||
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303062 (current_state == SWR_MSTR_SSR)) {
3063
3064 if ((current_state != SWR_MSTR_SSR) &&
3065 swrm_is_port_en(&swrm->master)) {
3066 dev_dbg(dev, "%s ports are enabled\n", __func__);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003067 trace_printk("%s ports are enabled\n", __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303068 ret = -EBUSY;
3069 goto exit;
3070 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05303071 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003072 dev_err(dev, "%s: clk stop mode not supported or SSR entry\n",
3073 __func__);
Sudheer Papothi06f43412019-07-09 03:32:54 +05303074 mutex_unlock(&swrm->reslock);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +05303075 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
Sudheer Papothi06f43412019-07-09 03:32:54 +05303076 mutex_lock(&swrm->reslock);
Sudheer Papothi1f2565b2020-01-30 05:22:47 +05303077 if (!swrm->clk_stop_mode0_supp) {
3078 swrm_clk_pause(swrm);
3079 } else {
3080 /* Mask bus clash interrupt */
3081 swrm->intr_mask &= ~((u32)0x08);
3082 swr_master_write(swrm,
3083 SWRM_INTERRUPT_MASK_ADDR,
3084 swrm->intr_mask);
3085 swr_master_write(swrm,
3086 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
3087 swrm->intr_mask);
3088 mutex_unlock(&swrm->reslock);
3089 /* clock stop sequence */
3090 swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
3091 SWRS_SCP_CONTROL);
3092 mutex_lock(&swrm->reslock);
3093 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05303094 swr_master_write(swrm, SWRM_COMP_CFG_ADDR, 0x00);
3095 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
3096 ret = swr_device_down(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05303097 if (ret == -ENODEV) {
3098 dev_dbg_ratelimited(dev,
3099 "%s slave device down not implemented\n",
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003100 __func__);
3101 trace_printk(
3102 "%s slave device down not implemented\n",
3103 __func__);
Sudheer Papothi79c90752019-04-23 06:09:52 +05303104 ret = 0;
3105 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05303106 dev_err(dev,
3107 "%s: failed to shutdown swr dev %d\n",
3108 __func__, swr_dev->dev_num);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003109 trace_printk(
3110 "%s: failed to shutdown swr dev %d\n",
3111 __func__, swr_dev->dev_num);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05303112 goto exit;
3113 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303114 }
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003115 trace_printk("%s: clk stop mode not supported or SSR exit\n",
3116 __func__);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05303117 } else {
Sudheer Papothi55fa5972019-09-28 02:50:27 +05303118 /* Mask bus clash interrupt */
3119 swrm->intr_mask &= ~((u32)0x08);
3120 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR,
3121 swrm->intr_mask);
3122 swr_master_write(swrm,
3123 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
3124 swrm->intr_mask);
Sudheer Papothi384addd2019-06-14 02:26:52 +05303125 mutex_unlock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05303126 /* clock stop sequence */
3127 swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
3128 SWRS_SCP_CONTROL);
Sudheer Papothi384addd2019-06-14 02:26:52 +05303129 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05303130 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303131 }
Laxminath Kasamcafe0732019-11-20 17:31:58 +05303132 if (!swrm_check_link_status(swrm, 0x0))
Laxminath Kasam696b14b2019-12-03 22:07:34 +05303133 dev_dbg(dev, "%s:failed in disconnecting, ssr?\n",
3134 __func__);
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07003135 ret = swrm_clk_request(swrm, false);
3136 if (ret) {
3137 dev_err(dev, "%s: swrmn clk failed\n", __func__);
3138 ret = 0;
3139 goto exit;
3140 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05303141
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303142 if (swrm->clk_stop_mode0_supp) {
3143 if (swrm->wake_irq > 0) {
Laxminath Kasamea6cbee2020-04-28 00:02:32 +05303144 if (swrm->dmic_sva && swrm->pinctrl_setup)
3145 swrm->pinctrl_setup(swrm->handle, true);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303146 enable_irq(swrm->wake_irq);
3147 } else if (swrm->ipc_wakeup) {
3148 msm_aud_evt_blocking_notifier_call_chain(
3149 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
3150 swrm->ipc_wakeup_triggered = false;
3151 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05303152 }
3153
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303154 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05303155 /* Retain SSR state until resume */
3156 if (current_state != SWR_MSTR_SSR)
3157 swrm->state = SWR_MSTR_DOWN;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303158exit:
Laxminath Kasamea6cbee2020-04-28 00:02:32 +05303159 if (!swrm->aud_core_err)
Sudheer Papothi384addd2019-06-14 02:26:52 +05303160 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
3161 if (!hw_core_err)
3162 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Laxminath Kasamea6cbee2020-04-28 00:02:32 +05303163 swrm->aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303164 mutex_unlock(&swrm->reslock);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003165 trace_printk("%s: pm_runtime: suspend done state: %d\n",
3166 __func__, swrm->state);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303167 return ret;
3168}
3169#endif /* CONFIG_PM */
3170
Sudheer Papothi06f43412019-07-09 03:32:54 +05303171static int swrm_device_suspend(struct device *dev)
3172{
3173 struct platform_device *pdev = to_platform_device(dev);
3174 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3175 int ret = 0;
3176
3177 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003178 trace_printk("%s: swrm state: %d\n", __func__, swrm->state);
Sudheer Papothi06f43412019-07-09 03:32:54 +05303179 if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
3180 ret = swrm_runtime_suspend(dev);
3181 if (!ret) {
3182 pm_runtime_disable(dev);
3183 pm_runtime_set_suspended(dev);
3184 pm_runtime_enable(dev);
3185 }
3186 }
3187
3188 return 0;
3189}
3190
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303191static int swrm_device_down(struct device *dev)
3192{
3193 struct platform_device *pdev = to_platform_device(dev);
3194 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303195
3196 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003197 trace_printk("%s: swrm state: %d\n", __func__, swrm->state);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303198
3199 mutex_lock(&swrm->force_down_lock);
3200 swrm->state = SWR_MSTR_SSR;
3201 mutex_unlock(&swrm->force_down_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303202
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05303203 swrm_device_suspend(dev);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303204 return 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303205}
3206
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303207int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm)
3208{
3209 int ret = 0;
Laxminath Kasama60239e2019-01-10 14:43:03 +05303210 int irq, dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303211
3212 if (!swrm->ipc_wakeup) {
Laxminath Kasama60239e2019-01-10 14:43:03 +05303213 irq = of_get_named_gpio(swrm->dev->of_node,
3214 "qcom,swr-wakeup-irq", 0);
3215 if (gpio_is_valid(irq)) {
3216 swrm->wake_irq = gpio_to_irq(irq);
3217 if (swrm->wake_irq < 0) {
3218 dev_err(swrm->dev,
3219 "Unable to configure irq\n");
3220 return swrm->wake_irq;
3221 }
3222 } else {
3223 dir_apps_irq = platform_get_irq_byname(swrm->pdev,
3224 "swr_wake_irq");
3225 if (dir_apps_irq < 0) {
3226 dev_err(swrm->dev,
3227 "TLMM connect gpio not found\n");
3228 return -EINVAL;
3229 }
3230 swrm->wake_irq = dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303231 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303232 ret = request_threaded_irq(swrm->wake_irq, NULL,
3233 swrm_wakeup_interrupt,
3234 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
3235 "swr_wake_irq", swrm);
3236 if (ret) {
3237 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
3238 __func__, ret);
3239 return -EINVAL;
3240 }
Aditya Bavanari3517b112018-12-03 13:26:59 +05303241 irq_set_irq_wake(swrm->wake_irq, 1);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303242 }
3243 return ret;
3244}
3245
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05303246static int swrm_alloc_port_mem(struct device *dev, struct swr_mstr_ctrl *swrm,
3247 u32 uc, u32 size)
3248{
3249 if (!swrm->port_param) {
3250 swrm->port_param = devm_kzalloc(dev,
3251 sizeof(swrm->port_param) * SWR_UC_MAX,
3252 GFP_KERNEL);
3253 if (!swrm->port_param)
3254 return -ENOMEM;
3255 }
3256 if (!swrm->port_param[uc]) {
3257 swrm->port_param[uc] = devm_kcalloc(dev, size,
3258 sizeof(struct port_params),
3259 GFP_KERNEL);
3260 if (!swrm->port_param[uc])
3261 return -ENOMEM;
3262 } else {
3263 dev_err_ratelimited(swrm->dev, "%s: called more than once\n",
3264 __func__);
3265 }
3266
3267 return 0;
3268}
3269
3270static int swrm_copy_port_config(struct swr_mstr_ctrl *swrm,
3271 struct swrm_port_config *port_cfg,
3272 u32 size)
3273{
3274 int idx;
3275 struct port_params *params;
3276 int uc = port_cfg->uc;
3277 int ret = 0;
3278
3279 for (idx = 0; idx < size; idx++) {
3280 params = &((struct port_params *)port_cfg->params)[idx];
3281 if (!params) {
3282 dev_err(swrm->dev, "%s: Invalid params\n", __func__);
3283 ret = -EINVAL;
3284 break;
3285 }
3286 memcpy(&swrm->port_param[uc][idx], params,
3287 sizeof(struct port_params));
3288 }
3289
3290 return ret;
3291}
3292
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303293/**
3294 * swrm_wcd_notify - parent device can notify to soundwire master through
3295 * this function
3296 * @pdev: pointer to platform device structure
3297 * @id: command id from parent to the soundwire master
3298 * @data: data from parent device to soundwire master
3299 */
3300int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
3301{
3302 struct swr_mstr_ctrl *swrm;
3303 int ret = 0;
3304 struct swr_master *mstr;
3305 struct swr_device *swr_dev;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05303306 struct swrm_port_config *port_cfg;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303307
3308 if (!pdev) {
3309 pr_err("%s: pdev is NULL\n", __func__);
3310 return -EINVAL;
3311 }
3312 swrm = platform_get_drvdata(pdev);
3313 if (!swrm) {
3314 dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__);
3315 return -EINVAL;
3316 }
3317 mstr = &swrm->master;
3318
3319 switch (id) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05303320 case SWR_REQ_CLK_SWITCH:
3321 /* This will put soundwire in clock stop mode and disable the
3322 * clocks, if there is no active usecase running, so that the
3323 * next activity on soundwire will request clock from new clock
3324 * source.
3325 */
Sudheer Papothi8c50f2f2019-12-05 01:14:47 +05303326 if (!data) {
3327 dev_err(swrm->dev, "%s: data is NULL for id:%d\n",
3328 __func__, id);
3329 ret = -EINVAL;
3330 break;
3331 }
Sudheer Papothi06f43412019-07-09 03:32:54 +05303332 mutex_lock(&swrm->mlock);
Sudheer Papothi8c50f2f2019-12-05 01:14:47 +05303333 if (swrm->clk_src != *(int *)data) {
Vatsal Buchabd68a7c2020-06-08 10:41:56 +05303334 if (swrm->state == SWR_MSTR_UP) {
3335 swrm->req_clk_switch = true;
Sudheer Papothi8c50f2f2019-12-05 01:14:47 +05303336 swrm_device_suspend(&pdev->dev);
Vatsal Buchabd68a7c2020-06-08 10:41:56 +05303337 if (swrm->state == SWR_MSTR_UP)
3338 swrm->req_clk_switch = false;
3339 }
Sudheer Papothi8c50f2f2019-12-05 01:14:47 +05303340 swrm->clk_src = *(int *)data;
3341 }
Sudheer Papothi06f43412019-07-09 03:32:54 +05303342 mutex_unlock(&swrm->mlock);
3343 break;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05303344 case SWR_CLK_FREQ:
3345 if (!data) {
3346 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
3347 ret = -EINVAL;
3348 } else {
3349 mutex_lock(&swrm->mlock);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05303350 if (swrm->mclk_freq != *(int *)data) {
3351 dev_dbg(swrm->dev, "%s: freq change: force mstr down\n", __func__);
3352 if (swrm->state == SWR_MSTR_DOWN)
3353 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
3354 __func__, swrm->state);
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +05303355 else {
3356 swrm->mclk_freq = *(int *)data;
3357 swrm->bus_clk = swrm->mclk_freq;
3358 swrm_switch_frame_shape(swrm,
3359 swrm->bus_clk);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05303360 swrm_device_suspend(&pdev->dev);
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +05303361 }
Prasad Kumpatla386df4e2019-10-11 18:23:16 +05303362 /*
3363 * add delay to ensure clk release happen
3364 * if interrupt triggered for clk stop,
3365 * wait for it to exit
3366 */
3367 usleep_range(10000, 10500);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05303368 }
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05303369 swrm->mclk_freq = *(int *)data;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05303370 swrm->bus_clk = swrm->mclk_freq;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05303371 mutex_unlock(&swrm->mlock);
3372 }
3373 break;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303374 case SWR_DEVICE_SSR_DOWN:
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003375 trace_printk("%s: swr device down called\n", __func__);
Prasad Kumpatla71fef462020-01-31 21:38:58 +05303376 mutex_lock(&swrm->mlock);
3377 if (swrm->state == SWR_MSTR_DOWN)
3378 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
3379 __func__, swrm->state);
3380 else
3381 swrm_device_down(&pdev->dev);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303382 mutex_lock(&swrm->devlock);
3383 swrm->dev_up = false;
Aditya Bavanaribb981b72020-03-19 18:30:10 +05303384 swrm->hw_core_clk_en = 0;
3385 swrm->aud_core_clk_en = 0;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303386 mutex_unlock(&swrm->devlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05303387 mutex_lock(&swrm->reslock);
3388 swrm->state = SWR_MSTR_SSR;
3389 mutex_unlock(&swrm->reslock);
Prasad Kumpatla71fef462020-01-31 21:38:58 +05303390 mutex_unlock(&swrm->mlock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303391 break;
3392 case SWR_DEVICE_SSR_UP:
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05303393 /* wait for clk voting to be zero */
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003394 trace_printk("%s: swr device up called\n", __func__);
Ramprasad Katkam7f6462e2018-11-06 11:51:22 +05303395 reinit_completion(&swrm->clk_off_complete);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05303396 if (swrm->clk_ref_count &&
3397 !wait_for_completion_timeout(&swrm->clk_off_complete,
Ramprasad Katkamc87efeb2018-12-12 19:26:19 +05303398 msecs_to_jiffies(500)))
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05303399 dev_err(swrm->dev, "%s: clock voting not zero\n",
3400 __func__);
3401
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303402 mutex_lock(&swrm->devlock);
3403 swrm->dev_up = true;
3404 mutex_unlock(&swrm->devlock);
3405 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303406 case SWR_DEVICE_DOWN:
3407 dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003408 trace_printk("%s: swr master down called\n", __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303409 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05303410 if (swrm->state == SWR_MSTR_DOWN)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303411 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
3412 __func__, swrm->state);
3413 else
3414 swrm_device_down(&pdev->dev);
3415 mutex_unlock(&swrm->mlock);
3416 break;
3417 case SWR_DEVICE_UP:
3418 dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003419 trace_printk("%s: swr master up called\n", __func__);
Ramprasad Katkam0fed92f2018-11-08 14:22:22 +05303420 mutex_lock(&swrm->devlock);
3421 if (!swrm->dev_up) {
3422 dev_dbg(swrm->dev, "SSR not complete yet\n");
3423 mutex_unlock(&swrm->devlock);
3424 return -EBUSY;
3425 }
3426 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303427 mutex_lock(&swrm->mlock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303428 pm_runtime_mark_last_busy(&pdev->dev);
3429 pm_runtime_get_sync(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303430 mutex_lock(&swrm->reslock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303431 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
3432 ret = swr_reset_device(swr_dev);
3433 if (ret) {
3434 dev_err(swrm->dev,
3435 "%s: failed to reset swr device %d\n",
3436 __func__, swr_dev->dev_num);
3437 swrm_clk_request(swrm, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303438 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303439 }
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303440 pm_runtime_mark_last_busy(&pdev->dev);
3441 pm_runtime_put_autosuspend(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303442 mutex_unlock(&swrm->reslock);
3443 mutex_unlock(&swrm->mlock);
3444 break;
3445 case SWR_SET_NUM_RX_CH:
3446 if (!data) {
3447 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
3448 ret = -EINVAL;
3449 } else {
3450 mutex_lock(&swrm->mlock);
3451 swrm->num_rx_chs = *(int *)data;
3452 if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
3453 list_for_each_entry(swr_dev, &mstr->devices,
3454 dev_list) {
3455 ret = swr_set_device_group(swr_dev,
3456 SWR_BROADCAST);
3457 if (ret)
3458 dev_err(swrm->dev,
3459 "%s: set num ch failed\n",
3460 __func__);
3461 }
3462 } else {
3463 list_for_each_entry(swr_dev, &mstr->devices,
3464 dev_list) {
3465 ret = swr_set_device_group(swr_dev,
3466 SWR_GROUP_NONE);
3467 if (ret)
3468 dev_err(swrm->dev,
3469 "%s: set num ch failed\n",
3470 __func__);
3471 }
3472 }
3473 mutex_unlock(&swrm->mlock);
3474 }
3475 break;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303476 case SWR_REGISTER_WAKE_IRQ:
3477 if (!data) {
3478 dev_err(swrm->dev, "%s: reg wake irq data is NULL\n",
3479 __func__);
3480 ret = -EINVAL;
3481 } else {
3482 mutex_lock(&swrm->mlock);
3483 swrm->ipc_wakeup = *(u32 *)data;
3484 ret = swrm_register_wake_irq(swrm);
3485 if (ret)
3486 dev_err(swrm->dev, "%s: register wake_irq failed\n",
3487 __func__);
3488 mutex_unlock(&swrm->mlock);
3489 }
3490 break;
Sudheer Papothi72ee2642019-08-08 05:15:17 +05303491 case SWR_REGISTER_WAKEUP:
3492 msm_aud_evt_blocking_notifier_call_chain(
3493 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
Laxminath Kasamea6cbee2020-04-28 00:02:32 +05303494 swrm->dmic_sva = *(u32 *)data;
Sudheer Papothi72ee2642019-08-08 05:15:17 +05303495 break;
3496 case SWR_DEREGISTER_WAKEUP:
3497 msm_aud_evt_blocking_notifier_call_chain(
3498 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
Laxminath Kasamea6cbee2020-04-28 00:02:32 +05303499 swrm->dmic_sva = 0;
Sudheer Papothi72ee2642019-08-08 05:15:17 +05303500 break;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05303501 case SWR_SET_PORT_MAP:
3502 if (!data) {
3503 dev_err(swrm->dev, "%s: data is NULL for id=%d\n",
3504 __func__, id);
3505 ret = -EINVAL;
3506 } else {
3507 mutex_lock(&swrm->mlock);
3508 port_cfg = (struct swrm_port_config *)data;
3509 if (!port_cfg->size) {
3510 ret = -EINVAL;
3511 goto done;
3512 }
3513 ret = swrm_alloc_port_mem(&pdev->dev, swrm,
3514 port_cfg->uc, port_cfg->size);
3515 if (!ret)
3516 swrm_copy_port_config(swrm, port_cfg,
3517 port_cfg->size);
3518done:
3519 mutex_unlock(&swrm->mlock);
3520 }
3521 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303522 default:
3523 dev_err(swrm->dev, "%s: swr master unknown id %d\n",
3524 __func__, id);
3525 break;
3526 }
3527 return ret;
3528}
3529EXPORT_SYMBOL(swrm_wcd_notify);
3530
Ramprasad Katkam57349872018-11-11 18:34:57 +05303531/*
3532 * swrm_pm_cmpxchg:
3533 * Check old state and exchange with pm new state
3534 * if old state matches with current state
3535 *
3536 * @swrm: pointer to wcd core resource
3537 * @o: pm old state
3538 * @n: pm new state
3539 *
3540 * Returns old state
3541 */
3542static enum swrm_pm_state swrm_pm_cmpxchg(
3543 struct swr_mstr_ctrl *swrm,
3544 enum swrm_pm_state o,
3545 enum swrm_pm_state n)
3546{
3547 enum swrm_pm_state old;
3548
3549 if (!swrm)
3550 return o;
3551
3552 mutex_lock(&swrm->pm_lock);
3553 old = swrm->pm_state;
3554 if (old == o)
3555 swrm->pm_state = n;
3556 mutex_unlock(&swrm->pm_lock);
3557
3558 return old;
3559}
3560
3561static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm)
3562{
3563 enum swrm_pm_state os;
3564
3565 /*
3566 * swrm_{lock/unlock}_sleep will be called by swr irq handler
3567 * and slave wake up requests..
3568 *
3569 * If system didn't resume, we can simply return false so
3570 * IRQ handler can return without handling IRQ.
3571 */
3572 mutex_lock(&swrm->pm_lock);
3573 if (swrm->wlock_holders++ == 0) {
3574 dev_dbg(swrm->dev, "%s: holding wake lock\n", __func__);
3575 pm_qos_update_request(&swrm->pm_qos_req,
3576 msm_cpuidle_get_deep_idle_latency());
3577 pm_stay_awake(swrm->dev);
3578 }
3579 mutex_unlock(&swrm->pm_lock);
3580
3581 if (!wait_event_timeout(swrm->pm_wq,
3582 ((os = swrm_pm_cmpxchg(swrm,
3583 SWRM_PM_SLEEPABLE,
3584 SWRM_PM_AWAKE)) ==
3585 SWRM_PM_SLEEPABLE ||
3586 (os == SWRM_PM_AWAKE)),
3587 msecs_to_jiffies(
3588 SWRM_SYSTEM_RESUME_TIMEOUT_MS))) {
3589 dev_err(swrm->dev, "%s: system didn't resume within %dms, s %d, w %d\n",
3590 __func__, SWRM_SYSTEM_RESUME_TIMEOUT_MS, swrm->pm_state,
3591 swrm->wlock_holders);
3592 swrm_unlock_sleep(swrm);
3593 return false;
3594 }
3595 wake_up_all(&swrm->pm_wq);
3596 return true;
3597}
3598
3599static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm)
3600{
3601 mutex_lock(&swrm->pm_lock);
3602 if (--swrm->wlock_holders == 0) {
3603 dev_dbg(swrm->dev, "%s: releasing wake lock pm_state %d -> %d\n",
3604 __func__, swrm->pm_state, SWRM_PM_SLEEPABLE);
3605 /*
3606 * if swrm_lock_sleep failed, pm_state would be still
3607 * swrm_PM_ASLEEP, don't overwrite
3608 */
3609 if (likely(swrm->pm_state == SWRM_PM_AWAKE))
3610 swrm->pm_state = SWRM_PM_SLEEPABLE;
3611 pm_qos_update_request(&swrm->pm_qos_req,
3612 PM_QOS_DEFAULT_VALUE);
3613 pm_relax(swrm->dev);
3614 }
3615 mutex_unlock(&swrm->pm_lock);
3616 wake_up_all(&swrm->pm_wq);
3617}
3618
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303619#ifdef CONFIG_PM_SLEEP
3620static int swrm_suspend(struct device *dev)
3621{
3622 int ret = -EBUSY;
3623 struct platform_device *pdev = to_platform_device(dev);
3624 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3625
3626 dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
Ramprasad Katkam57349872018-11-11 18:34:57 +05303627
3628 mutex_lock(&swrm->pm_lock);
3629
3630 if (swrm->pm_state == SWRM_PM_SLEEPABLE) {
3631 dev_dbg(swrm->dev, "%s: suspending system, state %d, wlock %d\n",
3632 __func__, swrm->pm_state,
3633 swrm->wlock_holders);
3634 swrm->pm_state = SWRM_PM_ASLEEP;
3635 } else if (swrm->pm_state == SWRM_PM_AWAKE) {
3636 /*
3637 * unlock to wait for pm_state == SWRM_PM_SLEEPABLE
3638 * then set to SWRM_PM_ASLEEP
3639 */
3640 dev_dbg(swrm->dev, "%s: waiting to suspend system, state %d, wlock %d\n",
3641 __func__, swrm->pm_state,
3642 swrm->wlock_holders);
3643 mutex_unlock(&swrm->pm_lock);
3644 if (!(wait_event_timeout(swrm->pm_wq, swrm_pm_cmpxchg(
3645 swrm, SWRM_PM_SLEEPABLE,
3646 SWRM_PM_ASLEEP) ==
3647 SWRM_PM_SLEEPABLE,
3648 msecs_to_jiffies(
3649 SWRM_SYS_SUSPEND_WAIT)))) {
3650 dev_dbg(swrm->dev, "%s: suspend failed state %d, wlock %d\n",
3651 __func__, swrm->pm_state,
3652 swrm->wlock_holders);
3653 return -EBUSY;
3654 } else {
3655 dev_dbg(swrm->dev,
3656 "%s: done, state %d, wlock %d\n",
3657 __func__, swrm->pm_state,
3658 swrm->wlock_holders);
3659 }
3660 mutex_lock(&swrm->pm_lock);
3661 } else if (swrm->pm_state == SWRM_PM_ASLEEP) {
3662 dev_dbg(swrm->dev, "%s: system is already suspended, state %d, wlock %d\n",
3663 __func__, swrm->pm_state,
3664 swrm->wlock_holders);
3665 }
3666
3667 mutex_unlock(&swrm->pm_lock);
3668
3669 if ((!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev))) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303670 ret = swrm_runtime_suspend(dev);
3671 if (!ret) {
3672 /*
3673 * Synchronize runtime-pm and system-pm states:
3674 * At this point, we are already suspended. If
3675 * runtime-pm still thinks its active, then
3676 * make sure its status is in sync with HW
3677 * status. The three below calls let the
3678 * runtime-pm know that we are suspended
3679 * already without re-invoking the suspend
3680 * callback
3681 */
3682 pm_runtime_disable(dev);
3683 pm_runtime_set_suspended(dev);
3684 pm_runtime_enable(dev);
3685 }
3686 }
3687 if (ret == -EBUSY) {
3688 /*
3689 * There is a possibility that some audio stream is active
3690 * during suspend. We dont want to return suspend failure in
3691 * that case so that display and relevant components can still
3692 * go to suspend.
3693 * If there is some other error, then it should be passed-on
3694 * to system level suspend
3695 */
3696 ret = 0;
3697 }
3698 return ret;
3699}
3700
3701static int swrm_resume(struct device *dev)
3702{
3703 int ret = 0;
3704 struct platform_device *pdev = to_platform_device(dev);
3705 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3706
3707 dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
3708 if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
3709 ret = swrm_runtime_resume(dev);
3710 if (!ret) {
3711 pm_runtime_mark_last_busy(dev);
3712 pm_request_autosuspend(dev);
3713 }
3714 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05303715 mutex_lock(&swrm->pm_lock);
3716 if (swrm->pm_state == SWRM_PM_ASLEEP) {
3717 dev_dbg(swrm->dev,
3718 "%s: resuming system, state %d, wlock %d\n",
3719 __func__, swrm->pm_state,
3720 swrm->wlock_holders);
3721 swrm->pm_state = SWRM_PM_SLEEPABLE;
3722 } else {
3723 dev_dbg(swrm->dev, "%s: system is already awake, state %d wlock %d\n",
3724 __func__, swrm->pm_state,
3725 swrm->wlock_holders);
3726 }
3727 mutex_unlock(&swrm->pm_lock);
3728 wake_up_all(&swrm->pm_wq);
3729
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303730 return ret;
3731}
3732#endif /* CONFIG_PM_SLEEP */
3733
3734static const struct dev_pm_ops swrm_dev_pm_ops = {
3735 SET_SYSTEM_SLEEP_PM_OPS(
3736 swrm_suspend,
3737 swrm_resume
3738 )
3739 SET_RUNTIME_PM_OPS(
3740 swrm_runtime_suspend,
3741 swrm_runtime_resume,
3742 NULL
3743 )
3744};
3745
3746static const struct of_device_id swrm_dt_match[] = {
3747 {
3748 .compatible = "qcom,swr-mstr",
3749 },
3750 {}
3751};
3752
3753static struct platform_driver swr_mstr_driver = {
3754 .probe = swrm_probe,
3755 .remove = swrm_remove,
3756 .driver = {
3757 .name = SWR_WCD_NAME,
3758 .owner = THIS_MODULE,
3759 .pm = &swrm_dev_pm_ops,
3760 .of_match_table = swrm_dt_match,
Xiaojun Sang53cd13a2018-06-29 15:14:37 +08003761 .suppress_bind_attrs = true,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303762 },
3763};
3764
3765static int __init swrm_init(void)
3766{
3767 return platform_driver_register(&swr_mstr_driver);
3768}
3769module_init(swrm_init);
3770
3771static void __exit swrm_exit(void)
3772{
3773 platform_driver_unregister(&swr_mstr_driver);
3774}
3775module_exit(swrm_exit);
3776
3777MODULE_LICENSE("GPL v2");
3778MODULE_DESCRIPTION("SoundWire Master Controller");
3779MODULE_ALIAS("platform:swr-mstr");