blob: 2e7779ac402d53c801930bfedf609673dd412cdb [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 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530625 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530626 }
627 return 0;
628}
629
Laxminath Kasame2291972019-11-08 14:51:59 +0530630static bool swrm_check_link_status(struct swr_mstr_ctrl *swrm, bool active)
631{
632 int retry = SWRM_LINK_STATUS_RETRY_CNT;
633 int ret = false;
634 int status = active ? 0x1 : 0x0;
Laxminath Kasam09819e92019-11-18 14:38:11 +0530635 int comp_sts = 0x0;
Laxminath Kasame2291972019-11-08 14:51:59 +0530636
637 if ((swrm->version <= SWRM_VERSION_1_5_1))
638 return true;
639
640 do {
Laxminath Kasam09819e92019-11-18 14:38:11 +0530641 comp_sts = swr_master_read(swrm, SWRM_COMP_STATUS) & 0x01;
642 /* check comp status and status requested met */
643 if ((comp_sts && status) || (!comp_sts && !status)) {
Laxminath Kasame2291972019-11-08 14:51:59 +0530644 ret = true;
645 break;
646 }
647 retry--;
648 usleep_range(500, 510);
649 } while (retry);
650
651 if (retry == 0)
652 dev_err(swrm->dev, "%s: link status not %s\n", __func__,
653 active ? "connected" : "disconnected");
654
655 return ret;
656}
657
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530658static bool swrm_is_port_en(struct swr_master *mstr)
659{
660 return !!(mstr->num_port);
661}
662
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530663static void copy_port_tables(struct swr_mstr_ctrl *swrm,
664 struct port_params *params)
665{
666 u8 i;
667 struct port_params *config = params;
668
669 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
670 /* wsa uses single frame structure for all configurations */
671 if (!swrm->mport_cfg[i].port_en)
672 continue;
673 swrm->mport_cfg[i].sinterval = config[i].si;
674 swrm->mport_cfg[i].offset1 = config[i].off1;
675 swrm->mport_cfg[i].offset2 = config[i].off2;
676 swrm->mport_cfg[i].hstart = config[i].hstart;
677 swrm->mport_cfg[i].hstop = config[i].hstop;
678 swrm->mport_cfg[i].blk_pack_mode = config[i].bp_mode;
679 swrm->mport_cfg[i].blk_grp_count = config[i].bgp_ctrl;
680 swrm->mport_cfg[i].word_length = config[i].wd_len;
681 swrm->mport_cfg[i].lane_ctrl = config[i].lane_ctrl;
682 }
683}
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530684static int swrm_get_port_config(struct swr_mstr_ctrl *swrm)
685{
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530686 struct port_params *params;
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530687 u32 usecase = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530688
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530689 /* TODO - Send usecase information to avoid checking for master_id */
690 if (swrm->mport_cfg[SWRM_DSD_PARAMS_PORT].port_en &&
691 (swrm->master_id == MASTER_ID_RX))
692 usecase = 1;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530693
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530694 params = swrm->port_param[usecase];
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530695 copy_port_tables(swrm, params);
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530696
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530697 return 0;
698}
699
700static int swrm_get_master_port(struct swr_mstr_ctrl *swrm, u8 *mstr_port_id,
701 u8 *mstr_ch_mask, u8 mstr_prt_type,
702 u8 slv_port_id)
703{
704 int i, j;
705 *mstr_port_id = 0;
706
707 for (i = 1; i <= swrm->num_ports; i++) {
708 for (j = 0; j < SWR_MAX_CH_PER_PORT; j++) {
709 if (swrm->port_mapping[i][j].port_type == mstr_prt_type)
710 goto found;
711 }
712 }
713found:
714 if (i > swrm->num_ports || j == SWR_MAX_CH_PER_PORT) {
715 dev_err(swrm->dev, "%s: port type not supported by master\n",
716 __func__);
717 return -EINVAL;
718 }
719 /* id 0 corresponds to master port 1 */
720 *mstr_port_id = i - 1;
721 *mstr_ch_mask = swrm->port_mapping[i][j].ch_mask;
722
723 return 0;
724
725}
726
727static u32 swrm_get_packed_reg_val(u8 *cmd_id, u8 cmd_data,
728 u8 dev_addr, u16 reg_addr)
729{
730 u32 val;
731 u8 id = *cmd_id;
732
733 if (id != SWR_BROADCAST_CMD_ID) {
734 if (id < 14)
735 id += 1;
736 else
737 id = 0;
738 *cmd_id = id;
739 }
740 val = SWR_REG_VAL_PACK(cmd_data, dev_addr, id, reg_addr);
741
742 return val;
743}
744
745static int swrm_cmd_fifo_rd_cmd(struct swr_mstr_ctrl *swrm, int *cmd_data,
746 u8 dev_addr, u8 cmd_id, u16 reg_addr,
747 u32 len)
748{
749 u32 val;
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530750 u32 retry_attempt = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530751
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530752 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530753 val = swrm_get_packed_reg_val(&swrm->rcmd_id, len, dev_addr, reg_addr);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530754 if (swrm->read) {
755 /* skip delay if read is handled in platform driver */
756 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
757 } else {
758 /* wait for FIFO RD to complete to avoid overflow */
759 usleep_range(100, 105);
760 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
761 /* wait for FIFO RD CMD complete to avoid overflow */
762 usleep_range(250, 255);
763 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530764retry_read:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530765 *cmd_data = swr_master_read(swrm, SWRM_CMD_FIFO_RD_FIFO_ADDR);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530766 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, rcmd_id: 0x%x, \
767 dev_num: 0x%x, cmd_data: 0x%x\n", __func__, reg_addr,
768 cmd_id, swrm->rcmd_id, dev_addr, *cmd_data);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530769 if ((((*cmd_data) & 0xF00) >> 8) != swrm->rcmd_id) {
770 if (retry_attempt < MAX_FIFO_RD_FAIL_RETRY) {
771 /* wait 500 us before retry on fifo read failure */
772 usleep_range(500, 505);
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +0530773 if (retry_attempt == (MAX_FIFO_RD_FAIL_RETRY - 1)) {
774 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
775 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
776 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530777 retry_attempt++;
778 goto retry_read;
779 } else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530780 dev_err_ratelimited(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, \
781 rcmd_id: 0x%x, dev_num: 0x%x, cmd_data: 0x%x\n",
782 __func__, reg_addr, cmd_id, swrm->rcmd_id,
783 dev_addr, *cmd_data);
784
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530785 dev_err_ratelimited(swrm->dev,
786 "%s: failed to read fifo\n", __func__);
787 }
788 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530789 mutex_unlock(&swrm->iolock);
790
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530791 return 0;
792}
793
794static int swrm_cmd_fifo_wr_cmd(struct swr_mstr_ctrl *swrm, u8 cmd_data,
795 u8 dev_addr, u8 cmd_id, u16 reg_addr)
796{
797 u32 val;
798 int ret = 0;
799
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530800 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530801 if (!cmd_id)
802 val = swrm_get_packed_reg_val(&swrm->wcmd_id, cmd_data,
803 dev_addr, reg_addr);
804 else
805 val = swrm_get_packed_reg_val(&cmd_id, cmd_data,
806 dev_addr, reg_addr);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530807 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x,wcmd_id: 0x%x, \
808 dev_num: 0x%x, cmd_data: 0x%x\n", __func__,
809 reg_addr, cmd_id, swrm->wcmd_id,dev_addr, cmd_data);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +0530810 swr_master_write(swrm, SWRM_CMD_FIFO_WR_CMD, val);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530811 /*
812 * wait for FIFO WR command to complete to avoid overflow
813 * skip delay if write is handled in platform driver.
814 */
815 if(!swrm->write)
Karthikeyan Mani5d52dd82019-08-15 16:58:08 -0700816 usleep_range(150, 155);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530817 if (cmd_id == 0xF) {
818 /*
819 * sleep for 10ms for MSM soundwire variant to allow broadcast
820 * command to complete.
821 */
822 if (swrm_is_msm_variant(swrm->version))
823 usleep_range(10000, 10100);
824 else
825 wait_for_completion_timeout(&swrm->broadcast,
826 (2 * HZ/10));
827 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530828 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530829 return ret;
830}
831
832static int swrm_read(struct swr_master *master, u8 dev_num, u16 reg_addr,
833 void *buf, u32 len)
834{
835 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
836 int ret = 0;
837 int val;
838 u8 *reg_val = (u8 *)buf;
839
840 if (!swrm) {
841 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
842 return -EINVAL;
843 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530844 if (!dev_num) {
845 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
846 return -EINVAL;
847 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530848 mutex_lock(&swrm->devlock);
849 if (!swrm->dev_up) {
850 mutex_unlock(&swrm->devlock);
851 return 0;
852 }
853 mutex_unlock(&swrm->devlock);
854
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530855 pm_runtime_get_sync(swrm->dev);
Vatsal Buchabd68a7c2020-06-08 10:41:56 +0530856 if (swrm->req_clk_switch)
857 swrm_runtime_resume(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530858 ret = swrm_cmd_fifo_rd_cmd(swrm, &val, dev_num, 0, reg_addr, len);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530859
860 if (!ret)
861 *reg_val = (u8)val;
862
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530863 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530864 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530865 return ret;
866}
867
868static int swrm_write(struct swr_master *master, u8 dev_num, u16 reg_addr,
869 const void *buf)
870{
871 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
872 int ret = 0;
873 u8 reg_val = *(u8 *)buf;
874
875 if (!swrm) {
876 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
877 return -EINVAL;
878 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530879 if (!dev_num) {
880 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
881 return -EINVAL;
882 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530883 mutex_lock(&swrm->devlock);
884 if (!swrm->dev_up) {
885 mutex_unlock(&swrm->devlock);
886 return 0;
887 }
888 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530889
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530890 pm_runtime_get_sync(swrm->dev);
Vatsal Buchabd68a7c2020-06-08 10:41:56 +0530891 if (swrm->req_clk_switch)
892 swrm_runtime_resume(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530893 ret = swrm_cmd_fifo_wr_cmd(swrm, reg_val, dev_num, 0, reg_addr);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530894
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530895 pm_runtime_put_autosuspend(swrm->dev);
896 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530897 return ret;
898}
899
900static int swrm_bulk_write(struct swr_master *master, u8 dev_num, void *reg,
901 const void *buf, size_t len)
902{
903 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
904 int ret = 0;
905 int i;
906 u32 *val;
907 u32 *swr_fifo_reg;
908
909 if (!swrm || !swrm->handle) {
910 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
911 return -EINVAL;
912 }
913 if (len <= 0)
914 return -EINVAL;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530915 mutex_lock(&swrm->devlock);
916 if (!swrm->dev_up) {
917 mutex_unlock(&swrm->devlock);
918 return 0;
919 }
920 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530921
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530922 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530923 if (dev_num) {
924 swr_fifo_reg = kcalloc(len, sizeof(u32), GFP_KERNEL);
925 if (!swr_fifo_reg) {
926 ret = -ENOMEM;
927 goto err;
928 }
929 val = kcalloc(len, sizeof(u32), GFP_KERNEL);
930 if (!val) {
931 ret = -ENOMEM;
932 goto mem_fail;
933 }
934
935 for (i = 0; i < len; i++) {
936 val[i] = swrm_get_packed_reg_val(&swrm->wcmd_id,
937 ((u8 *)buf)[i],
938 dev_num,
939 ((u16 *)reg)[i]);
940 swr_fifo_reg[i] = SWRM_CMD_FIFO_WR_CMD;
941 }
942 ret = swr_master_bulk_write(swrm, swr_fifo_reg, val, len);
943 if (ret) {
944 dev_err(&master->dev, "%s: bulk write failed\n",
945 __func__);
946 ret = -EINVAL;
947 }
948 } else {
949 dev_err(&master->dev,
950 "%s: No support of Bulk write for master regs\n",
951 __func__);
952 ret = -EINVAL;
953 goto err;
954 }
955 kfree(val);
956mem_fail:
957 kfree(swr_fifo_reg);
958err:
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530959 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530960 pm_runtime_mark_last_busy(swrm->dev);
961 return ret;
962}
963
964static u8 get_inactive_bank_num(struct swr_mstr_ctrl *swrm)
965{
966 return (swr_master_read(swrm, SWRM_MCP_STATUS) &
967 SWRM_MCP_STATUS_BANK_NUM_MASK) ? 0 : 1;
968}
969
970static void enable_bank_switch(struct swr_mstr_ctrl *swrm, u8 bank,
971 u8 row, u8 col)
972{
973 swrm_cmd_fifo_wr_cmd(swrm, ((row << 3) | col), 0xF, 0xF,
974 SWRS_SCP_FRAME_CTRL_BANK(bank));
975}
976
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +0530977static void swrm_switch_frame_shape(struct swr_mstr_ctrl *swrm, int mclk_freq)
978{
979 u8 bank;
980 u32 n_row, n_col;
981 u32 value = 0;
982 u32 row = 0, col = 0;
983 u8 ssp_period = 0;
984 int frame_sync = SWRM_FRAME_SYNC_SEL;
985
986 if (mclk_freq == MCLK_FREQ_NATIVE) {
987 n_col = SWR_MAX_COL;
988 col = SWRM_COL_16;
989 n_row = SWR_ROW_64;
990 row = SWRM_ROW_64;
991 frame_sync = SWRM_FRAME_SYNC_SEL_NATIVE;
992 } else {
993 n_col = SWR_MIN_COL;
994 col = SWRM_COL_02;
995 n_row = SWR_ROW_50;
996 row = SWRM_ROW_50;
997 frame_sync = SWRM_FRAME_SYNC_SEL;
998 }
999
1000 bank = get_inactive_bank_num(swrm);
1001 ssp_period = swrm_get_ssp_period(swrm, row, col, frame_sync);
1002 dev_dbg(swrm->dev, "%s: ssp_period: %d\n", __func__, ssp_period);
1003 value = ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
1004 (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
1005 ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
1006 swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1007 enable_bank_switch(swrm, bank, n_row, n_col);
1008}
1009
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301010static struct swr_port_info *swrm_get_port_req(struct swrm_mports *mport,
1011 u8 slv_port, u8 dev_num)
1012{
1013 struct swr_port_info *port_req = NULL;
1014
1015 list_for_each_entry(port_req, &mport->port_req_list, list) {
1016 /* Store dev_id instead of dev_num if enumeration is changed run_time */
1017 if ((port_req->slave_port_id == slv_port)
1018 && (port_req->dev_num == dev_num))
1019 return port_req;
1020 }
1021 return NULL;
1022}
1023
1024static bool swrm_remove_from_group(struct swr_master *master)
1025{
1026 struct swr_device *swr_dev;
1027 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1028 bool is_removed = false;
1029
1030 if (!swrm)
1031 goto end;
1032
1033 mutex_lock(&swrm->mlock);
1034 if ((swrm->num_rx_chs > 1) &&
1035 (swrm->num_rx_chs == swrm->num_cfg_devs)) {
1036 list_for_each_entry(swr_dev, &master->devices,
1037 dev_list) {
1038 swr_dev->group_id = SWR_GROUP_NONE;
1039 master->gr_sid = 0;
1040 }
1041 is_removed = true;
1042 }
1043 mutex_unlock(&swrm->mlock);
1044
1045end:
1046 return is_removed;
1047}
1048
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301049int swrm_get_clk_div_rate(int mclk_freq, int bus_clk_freq)
1050{
1051 if (!bus_clk_freq)
1052 return mclk_freq;
1053
1054 if (mclk_freq == SWR_CLK_RATE_9P6MHZ) {
1055 if (bus_clk_freq <= SWR_CLK_RATE_0P6MHZ)
1056 bus_clk_freq = SWR_CLK_RATE_0P6MHZ;
1057 else if (bus_clk_freq <= SWR_CLK_RATE_1P2MHZ)
1058 bus_clk_freq = SWR_CLK_RATE_1P2MHZ;
1059 else if (bus_clk_freq <= SWR_CLK_RATE_2P4MHZ)
1060 bus_clk_freq = SWR_CLK_RATE_2P4MHZ;
1061 else if(bus_clk_freq <= SWR_CLK_RATE_4P8MHZ)
1062 bus_clk_freq = SWR_CLK_RATE_4P8MHZ;
1063 else if(bus_clk_freq <= SWR_CLK_RATE_9P6MHZ)
1064 bus_clk_freq = SWR_CLK_RATE_9P6MHZ;
1065 } else if (mclk_freq == SWR_CLK_RATE_11P2896MHZ)
1066 bus_clk_freq = SWR_CLK_RATE_11P2896MHZ;
1067
1068 return bus_clk_freq;
1069}
1070
1071static int swrm_update_bus_clk(struct swr_mstr_ctrl *swrm)
1072{
1073 int ret = 0;
1074 int agg_clk = 0;
1075 int i;
1076
1077 for (i = 0; i < SWR_MSTR_PORT_LEN; i++)
1078 agg_clk += swrm->mport_cfg[i].ch_rate;
1079
1080 if (agg_clk)
1081 swrm->bus_clk = swrm_get_clk_div_rate(swrm->mclk_freq,
1082 agg_clk);
1083 else
1084 swrm->bus_clk = swrm->mclk_freq;
1085
1086 dev_dbg(swrm->dev, "%s: all_port_clk: %d, bus_clk: %d\n",
1087 __func__, agg_clk, swrm->bus_clk);
1088
1089 return ret;
1090}
1091
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301092static void swrm_disable_ports(struct swr_master *master,
1093 u8 bank)
1094{
1095 u32 value;
1096 struct swr_port_info *port_req;
1097 int i;
1098 struct swrm_mports *mport;
1099 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1100
1101 if (!swrm) {
1102 pr_err("%s: swrm is null\n", __func__);
1103 return;
1104 }
1105
1106 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
1107 master->num_port);
1108
1109
1110 for (i = 0; i < SWR_MSTR_PORT_LEN ; i++) {
1111
1112 mport = &(swrm->mport_cfg[i]);
1113 if (!mport->port_en)
1114 continue;
1115
1116 list_for_each_entry(port_req, &mport->port_req_list, list) {
1117 /* skip ports with no change req's*/
1118 if (port_req->req_ch == port_req->ch_en)
1119 continue;
1120
1121 swrm_cmd_fifo_wr_cmd(swrm, port_req->req_ch,
1122 port_req->dev_num, 0x00,
1123 SWRS_DP_CHANNEL_ENABLE_BANK(port_req->slave_port_id,
1124 bank));
1125 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x\n",
1126 __func__, i,
1127 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)));
1128 }
1129 value = ((mport->req_ch)
1130 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
1131 value |= ((mport->offset2)
1132 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
1133 value |= ((mport->offset1)
1134 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
1135 value |= mport->sinterval;
1136
1137 swr_master_write(swrm,
1138 SWRM_DP_PORT_CTRL_BANK(i+1, bank),
1139 value);
1140 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
1141 __func__, i,
1142 (SWRM_DP_PORT_CTRL_BANK(i+1, bank)), value);
1143 }
1144}
1145
1146static void swrm_cleanup_disabled_port_reqs(struct swr_master *master)
1147{
1148 struct swr_port_info *port_req, *next;
1149 int i;
1150 struct swrm_mports *mport;
1151 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1152
1153 if (!swrm) {
1154 pr_err("%s: swrm is null\n", __func__);
1155 return;
1156 }
1157 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
1158 master->num_port);
1159
1160 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
1161 mport = &(swrm->mport_cfg[i]);
1162 list_for_each_entry_safe(port_req, next,
1163 &mport->port_req_list, list) {
1164 /* skip ports without new ch req */
1165 if (port_req->ch_en == port_req->req_ch)
1166 continue;
1167
1168 /* remove new ch req's*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +05301169 port_req->ch_en = port_req->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301170
1171 /* If no streams enabled on port, remove the port req */
1172 if (port_req->ch_en == 0) {
1173 list_del(&port_req->list);
1174 kfree(port_req);
1175 }
1176 }
1177 /* remove new ch req's on mport*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +05301178 mport->ch_en = mport->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301179
1180 if (!(mport->ch_en)) {
1181 mport->port_en = false;
1182 master->port_en_mask &= ~i;
1183 }
1184 }
1185}
1186static void swrm_copy_data_port_config(struct swr_master *master, u8 bank)
1187{
1188 u32 value, slv_id;
1189 struct swr_port_info *port_req;
1190 int i;
1191 struct swrm_mports *mport;
1192 u32 reg[SWRM_MAX_PORT_REG];
1193 u32 val[SWRM_MAX_PORT_REG];
1194 int len = 0;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301195 u8 hparams;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301196 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1197
1198 if (!swrm) {
1199 pr_err("%s: swrm is null\n", __func__);
1200 return;
1201 }
1202
1203 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
1204 master->num_port);
1205
1206 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
1207 mport = &(swrm->mport_cfg[i]);
1208 if (!mport->port_en)
1209 continue;
1210
1211 list_for_each_entry(port_req, &mport->port_req_list, list) {
1212 slv_id = port_req->slave_port_id;
1213 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1214 val[len++] = SWR_REG_VAL_PACK(port_req->req_ch,
1215 port_req->dev_num, 0x00,
1216 SWRS_DP_CHANNEL_ENABLE_BANK(slv_id,
1217 bank));
1218
1219 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1220 val[len++] = SWR_REG_VAL_PACK(mport->sinterval,
1221 port_req->dev_num, 0x00,
1222 SWRS_DP_SAMPLE_CONTROL_1_BANK(slv_id,
1223 bank));
1224
1225 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1226 val[len++] = SWR_REG_VAL_PACK(mport->offset1,
1227 port_req->dev_num, 0x00,
1228 SWRS_DP_OFFSET_CONTROL_1_BANK(slv_id,
1229 bank));
1230
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301231 if (mport->offset2 != SWR_INVALID_PARAM) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301232 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1233 val[len++] = SWR_REG_VAL_PACK(mport->offset2,
1234 port_req->dev_num, 0x00,
1235 SWRS_DP_OFFSET_CONTROL_2_BANK(
1236 slv_id, bank));
1237 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301238 if (mport->hstart != SWR_INVALID_PARAM
1239 && mport->hstop != SWR_INVALID_PARAM) {
1240 hparams = (mport->hstart << 4) | mport->hstop;
1241
1242 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1243 val[len++] = SWR_REG_VAL_PACK(hparams,
1244 port_req->dev_num, 0x00,
1245 SWRS_DP_HCONTROL_BANK(slv_id,
1246 bank));
1247 }
1248 if (mport->word_length != SWR_INVALID_PARAM) {
1249 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1250 val[len++] =
1251 SWR_REG_VAL_PACK(mport->word_length,
1252 port_req->dev_num, 0x00,
1253 SWRS_DP_BLOCK_CONTROL_1(slv_id));
1254 }
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +05301255 if (mport->blk_pack_mode != SWR_INVALID_PARAM
1256 && swrm->master_id != MASTER_ID_WSA) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301257 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1258 val[len++] =
1259 SWR_REG_VAL_PACK(mport->blk_pack_mode,
1260 port_req->dev_num, 0x00,
1261 SWRS_DP_BLOCK_CONTROL_3_BANK(slv_id,
1262 bank));
1263 }
1264 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
1265 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1266 val[len++] =
1267 SWR_REG_VAL_PACK(mport->blk_grp_count,
1268 port_req->dev_num, 0x00,
1269 SWRS_DP_BLOCK_CONTROL_2_BANK(slv_id,
1270 bank));
1271 }
1272 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
1273 reg[len] = SWRM_CMD_FIFO_WR_CMD;
1274 val[len++] =
1275 SWR_REG_VAL_PACK(mport->lane_ctrl,
1276 port_req->dev_num, 0x00,
1277 SWRS_DP_LANE_CONTROL_BANK(slv_id,
1278 bank));
1279 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301280 port_req->ch_en = port_req->req_ch;
1281 }
1282 value = ((mport->req_ch)
1283 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +05301284
1285 if (mport->offset2 != SWR_INVALID_PARAM)
1286 value |= ((mport->offset2)
1287 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301288 value |= ((mport->offset1)
1289 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
1290 value |= mport->sinterval;
1291
1292
1293 reg[len] = SWRM_DP_PORT_CTRL_BANK(i + 1, bank);
1294 val[len++] = value;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301295 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
1296 __func__, i,
1297 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)), value);
1298
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301299 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
1300 reg[len] = SWRM_DP_PORT_CTRL_2_BANK(i + 1, bank);
1301 val[len++] = mport->lane_ctrl;
1302 }
1303 if (mport->word_length != SWR_INVALID_PARAM) {
1304 reg[len] = SWRM_DP_BLOCK_CTRL_1(i + 1);
1305 val[len++] = mport->word_length;
1306 }
1307
1308 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
1309 reg[len] = SWRM_DP_BLOCK_CTRL2_BANK(i + 1, bank);
1310 val[len++] = mport->blk_grp_count;
1311 }
1312 if (mport->hstart != SWR_INVALID_PARAM
1313 && mport->hstop != SWR_INVALID_PARAM) {
1314 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
Laxminath Kasame30eef72018-11-05 17:40:09 +05301315 hparams = (mport->hstop << 4) | mport->hstart;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301316 val[len++] = hparams;
Laxminath Kasam990c70b2018-11-09 23:15:09 +05301317 } else {
1318 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
1319 hparams = (SWR_HSTOP_MAX_VAL << 4) | SWR_HSTART_MIN_VAL;
1320 val[len++] = hparams;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301321 }
1322 if (mport->blk_pack_mode != SWR_INVALID_PARAM) {
1323 reg[len] = SWRM_DP_BLOCK_CTRL3_BANK(i + 1, bank);
1324 val[len++] = mport->blk_pack_mode;
1325 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301326 mport->ch_en = mport->req_ch;
1327
1328 }
Sudheer Papothi0016db12019-06-11 04:42:38 +05301329 swrm_reg_dump(swrm, reg, val, len, __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301330 swr_master_bulk_write(swrm, reg, val, len);
1331}
1332
1333static void swrm_apply_port_config(struct swr_master *master)
1334{
1335 u8 bank;
1336 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1337
1338 if (!swrm) {
1339 pr_err("%s: Invalid handle to swr controller\n",
1340 __func__);
1341 return;
1342 }
1343
1344 bank = get_inactive_bank_num(swrm);
1345 dev_dbg(swrm->dev, "%s: enter bank: %d master_ports: %d\n",
1346 __func__, bank, master->num_port);
1347
Vatsal Bucha687f9822020-04-01 18:21:41 +05301348 if (!swrm->disable_div2_clk_switch)
1349 swrm_cmd_fifo_wr_cmd(swrm, 0x01, 0xF, 0x00,
1350 SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(bank));
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301351
1352 swrm_copy_data_port_config(master, bank);
1353}
1354
1355static int swrm_slvdev_datapath_control(struct swr_master *master, bool enable)
1356{
1357 u8 bank;
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301358 u32 value = 0, n_row = 0, n_col = 0;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301359 u32 row = 0, col = 0;
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301360 int bus_clk_div_factor;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301361 int ret;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301362 u8 ssp_period = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301363 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1364 int mask = (SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK |
1365 SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK |
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301366 SWRM_MCP_FRAME_CTRL_BANK_CLK_DIV_VALUE_BMSK |
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301367 SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_BMSK);
1368 u8 inactive_bank;
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +05301369 int frame_sync = SWRM_FRAME_SYNC_SEL;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301370
1371 if (!swrm) {
1372 pr_err("%s: swrm is null\n", __func__);
1373 return -EFAULT;
1374 }
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301375
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301376 mutex_lock(&swrm->mlock);
1377
Ramprasad Katkam979b7c92019-05-17 15:31:21 +05301378 /*
1379 * During disable if master is already down, which implies an ssr/pdr
1380 * scenario, just mark ports as disabled and exit
1381 */
1382 if (swrm->state == SWR_MSTR_SSR && !enable) {
1383 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1384 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1385 __func__);
1386 goto exit;
1387 }
1388 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
1389 swrm_cleanup_disabled_port_reqs(master);
1390 if (!swrm_is_port_en(master)) {
1391 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1392 __func__);
1393 pm_runtime_mark_last_busy(swrm->dev);
1394 pm_runtime_put_autosuspend(swrm->dev);
1395 }
1396 goto exit;
1397 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301398 bank = get_inactive_bank_num(swrm);
1399
1400 if (enable) {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301401 if (!test_bit(ENABLE_PENDING, &swrm->port_req_pending)) {
1402 dev_dbg(swrm->dev, "%s:No pending connect port req\n",
1403 __func__);
1404 goto exit;
1405 }
1406 clear_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301407 ret = swrm_get_port_config(swrm);
1408 if (ret) {
1409 /* cannot accommodate ports */
1410 swrm_cleanup_disabled_port_reqs(master);
1411 mutex_unlock(&swrm->mlock);
1412 return -EINVAL;
1413 }
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301414 swr_master_write(swrm, SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301415 SWRM_INTERRUPT_STATUS_MASK);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301416 /* apply the new port config*/
1417 swrm_apply_port_config(master);
1418 } else {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301419 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1420 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1421 __func__);
1422 goto exit;
1423 }
1424 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301425 swrm_disable_ports(master, bank);
1426 }
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301427 dev_dbg(swrm->dev, "%s: enable: %d, cfg_devs: %d freq %d\n",
1428 __func__, enable, swrm->num_cfg_devs, swrm->mclk_freq);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301429
1430 if (enable) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301431 /* set col = 16 */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301432 n_col = SWR_MAX_COL;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301433 col = SWRM_COL_16;
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301434 if (swrm->bus_clk == MCLK_FREQ_LP) {
1435 n_col = SWR_MIN_COL;
1436 col = SWRM_COL_02;
1437 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301438 } else {
1439 /*
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301440 * Do not change to col = 2 if there are still active ports
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301441 */
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301442 if (!master->num_port) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301443 n_col = SWR_MIN_COL;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301444 col = SWRM_COL_02;
1445 } else {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301446 n_col = SWR_MAX_COL;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301447 col = SWRM_COL_16;
1448 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301449 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301450 /* Use default 50 * x, frame shape. Change based on mclk */
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301451 if (swrm->mclk_freq == MCLK_FREQ_NATIVE) {
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301452 dev_dbg(swrm->dev, "setting 64 x %d frameshape\n", col);
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301453 n_row = SWR_ROW_64;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301454 row = SWRM_ROW_64;
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +05301455 frame_sync = SWRM_FRAME_SYNC_SEL_NATIVE;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301456 } else {
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301457 dev_dbg(swrm->dev, "setting 50 x %d frameshape\n", col);
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301458 n_row = SWR_ROW_50;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301459 row = SWRM_ROW_50;
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +05301460 frame_sync = SWRM_FRAME_SYNC_SEL;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301461 }
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +05301462 ssp_period = swrm_get_ssp_period(swrm, row, col, frame_sync);
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301463 bus_clk_div_factor = swrm_get_clk_div(swrm->mclk_freq, swrm->bus_clk);
1464 dev_dbg(swrm->dev, "%s: ssp_period: %d, bus_clk_div:%d \n", __func__,
1465 ssp_period, bus_clk_div_factor);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301466 value = swr_master_read(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank));
1467 value &= (~mask);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301468 value |= ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301469 (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301470 (bus_clk_div_factor <<
1471 SWRM_MCP_FRAME_CTRL_BANK_CLK_DIV_VALUE_SHFT) |
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05301472 ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301473 swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1474
1475 dev_dbg(swrm->dev, "%s: regaddr: 0x%x, value: 0x%x\n", __func__,
1476 SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1477
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301478 enable_bank_switch(swrm, bank, n_row, n_col);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301479 inactive_bank = bank ? 0 : 1;
1480
1481 if (enable)
1482 swrm_copy_data_port_config(master, inactive_bank);
1483 else {
1484 swrm_disable_ports(master, inactive_bank);
1485 swrm_cleanup_disabled_port_reqs(master);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301486 }
1487 if (!swrm_is_port_en(master)) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301488 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1489 __func__);
1490 pm_runtime_mark_last_busy(swrm->dev);
1491 pm_runtime_put_autosuspend(swrm->dev);
1492 }
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301493exit:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301494 mutex_unlock(&swrm->mlock);
1495return 0;
1496}
1497
1498static int swrm_connect_port(struct swr_master *master,
1499 struct swr_params *portinfo)
1500{
1501 int i;
1502 struct swr_port_info *port_req;
1503 int ret = 0;
1504 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1505 struct swrm_mports *mport;
1506 u8 mstr_port_id, mstr_ch_msk;
1507
1508 dev_dbg(&master->dev, "%s: enter\n", __func__);
1509 if (!portinfo)
1510 return -EINVAL;
1511
1512 if (!swrm) {
1513 dev_err(&master->dev,
1514 "%s: Invalid handle to swr controller\n",
1515 __func__);
1516 return -EINVAL;
1517 }
1518
1519 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301520 mutex_lock(&swrm->devlock);
1521 if (!swrm->dev_up) {
1522 mutex_unlock(&swrm->devlock);
1523 mutex_unlock(&swrm->mlock);
1524 return -EINVAL;
1525 }
1526 mutex_unlock(&swrm->devlock);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301527 if (!swrm_is_port_en(master))
1528 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301529
1530 for (i = 0; i < portinfo->num_port; i++) {
1531 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_msk,
1532 portinfo->port_type[i],
1533 portinfo->port_id[i]);
1534 if (ret) {
1535 dev_err(&master->dev,
1536 "%s: mstr portid for slv port %d not found\n",
1537 __func__, portinfo->port_id[i]);
1538 goto port_fail;
1539 }
1540
1541 mport = &(swrm->mport_cfg[mstr_port_id]);
1542 /* get port req */
1543 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1544 portinfo->dev_num);
1545 if (!port_req) {
1546 dev_dbg(&master->dev, "%s: new req:port id %d dev %d\n",
1547 __func__, portinfo->port_id[i],
1548 portinfo->dev_num);
1549 port_req = kzalloc(sizeof(struct swr_port_info),
1550 GFP_KERNEL);
1551 if (!port_req) {
1552 ret = -ENOMEM;
1553 goto mem_fail;
1554 }
1555 port_req->dev_num = portinfo->dev_num;
1556 port_req->slave_port_id = portinfo->port_id[i];
1557 port_req->num_ch = portinfo->num_ch[i];
1558 port_req->ch_rate = portinfo->ch_rate[i];
1559 port_req->ch_en = 0;
1560 port_req->master_port_id = mstr_port_id;
1561 list_add(&port_req->list, &mport->port_req_list);
1562 }
1563 port_req->req_ch |= portinfo->ch_en[i];
1564
1565 dev_dbg(&master->dev,
1566 "%s: mstr port %d, slv port %d ch_rate %d num_ch %d\n",
1567 __func__, port_req->master_port_id,
1568 port_req->slave_port_id, port_req->ch_rate,
1569 port_req->num_ch);
1570 /* Put the port req on master port */
1571 mport = &(swrm->mport_cfg[mstr_port_id]);
1572 mport->port_en = true;
1573 mport->req_ch |= mstr_ch_msk;
1574 master->port_en_mask |= (1 << mstr_port_id);
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301575 if (swrm->clk_stop_mode0_supp &&
1576 (mport->ch_rate < portinfo->ch_rate[i])) {
1577 mport->ch_rate = portinfo->ch_rate[i];
1578 swrm_update_bus_clk(swrm);
1579 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301580 }
1581 master->num_port += portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301582 set_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301583 swr_port_response(master, portinfo->tid);
1584
1585 mutex_unlock(&swrm->mlock);
1586 return 0;
1587
1588port_fail:
1589mem_fail:
1590 /* cleanup port reqs in error condition */
1591 swrm_cleanup_disabled_port_reqs(master);
1592 mutex_unlock(&swrm->mlock);
1593 return ret;
1594}
1595
1596static int swrm_disconnect_port(struct swr_master *master,
1597 struct swr_params *portinfo)
1598{
1599 int i, ret = 0;
1600 struct swr_port_info *port_req;
1601 struct swrm_mports *mport;
1602 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1603 u8 mstr_port_id, mstr_ch_mask;
1604
1605 if (!swrm) {
1606 dev_err(&master->dev,
1607 "%s: Invalid handle to swr controller\n",
1608 __func__);
1609 return -EINVAL;
1610 }
1611
1612 if (!portinfo) {
1613 dev_err(&master->dev, "%s: portinfo is NULL\n", __func__);
1614 return -EINVAL;
1615 }
1616 mutex_lock(&swrm->mlock);
1617
1618 for (i = 0; i < portinfo->num_port; i++) {
1619
1620 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_mask,
1621 portinfo->port_type[i], portinfo->port_id[i]);
1622 if (ret) {
1623 dev_err(&master->dev,
1624 "%s: mstr portid for slv port %d not found\n",
1625 __func__, portinfo->port_id[i]);
1626 mutex_unlock(&swrm->mlock);
1627 return -EINVAL;
1628 }
1629 mport = &(swrm->mport_cfg[mstr_port_id]);
1630 /* get port req */
1631 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1632 portinfo->dev_num);
1633
1634 if (!port_req) {
1635 dev_err(&master->dev, "%s:port not enabled : port %d\n",
1636 __func__, portinfo->port_id[i]);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05301637 mutex_unlock(&swrm->mlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301638 return -EINVAL;
1639 }
1640 port_req->req_ch &= ~portinfo->ch_en[i];
1641 mport->req_ch &= ~mstr_ch_mask;
Sudheer Papothiae5c3632019-11-27 06:52:06 +05301642 if (swrm->clk_stop_mode0_supp && !mport->req_ch) {
1643 mport->ch_rate = 0;
1644 swrm_update_bus_clk(swrm);
1645 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301646 }
1647 master->num_port -= portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301648 set_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301649 swr_port_response(master, portinfo->tid);
1650 mutex_unlock(&swrm->mlock);
1651
1652 return 0;
1653}
1654
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301655static int swrm_find_alert_slave(struct swr_mstr_ctrl *swrm,
1656 int status, u8 *devnum)
1657{
1658 int i;
1659 bool found = false;
1660
1661 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1662 if ((status & SWRM_MCP_SLV_STATUS_MASK) == SWR_ALERT) {
1663 *devnum = i;
1664 found = true;
1665 break;
1666 }
1667 status >>= 2;
1668 }
1669 if (found)
1670 return 0;
1671 else
1672 return -EINVAL;
1673}
1674
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301675static void swrm_enable_slave_irq(struct swr_mstr_ctrl *swrm)
1676{
1677 int i;
1678 int status = 0;
1679
1680 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1681 if (!status) {
1682 dev_dbg_ratelimited(swrm->dev, "%s: slaves status is 0x%x\n",
1683 __func__, status);
1684 return;
1685 }
1686 dev_dbg(swrm->dev, "%s: slave status: 0x%x\n", __func__, status);
1687 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
Vatsal Bucha21ba0772020-02-28 20:36:46 +05301688 if (status & SWRM_MCP_SLV_STATUS_MASK) {
1689 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, i, 0x0,
1690 SWRS_SCP_INT_STATUS_CLEAR_1);
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301691 swrm_cmd_fifo_wr_cmd(swrm, 0x4, i, 0x0,
Vatsal Bucha21ba0772020-02-28 20:36:46 +05301692 SWRS_SCP_INT_STATUS_MASK_1);
1693 }
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301694 status >>= 2;
1695 }
1696}
1697
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301698static int swrm_check_slave_change_status(struct swr_mstr_ctrl *swrm,
1699 int status, u8 *devnum)
1700{
1701 int i;
1702 int new_sts = status;
1703 int ret = SWR_NOT_PRESENT;
1704
1705 if (status != swrm->slave_status) {
1706 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1707 if ((status & SWRM_MCP_SLV_STATUS_MASK) !=
1708 (swrm->slave_status & SWRM_MCP_SLV_STATUS_MASK)) {
1709 ret = (status & SWRM_MCP_SLV_STATUS_MASK);
1710 *devnum = i;
1711 break;
1712 }
1713 status >>= 2;
1714 swrm->slave_status >>= 2;
1715 }
1716 swrm->slave_status = new_sts;
1717 }
1718 return ret;
1719}
1720
1721static irqreturn_t swr_mstr_interrupt(int irq, void *dev)
1722{
1723 struct swr_mstr_ctrl *swrm = dev;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301724 u32 value, intr_sts, intr_sts_masked;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301725 u32 temp = 0;
1726 u32 status, chg_sts, i;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301727 u8 devnum = 0;
1728 int ret = IRQ_HANDLED;
1729 struct swr_device *swr_dev;
1730 struct swr_master *mstr = &swrm->master;
1731
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07001732 trace_printk("%s enter\n", __func__);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301733 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1734 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1735 return IRQ_NONE;
1736 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301737
1738 mutex_lock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301739 if (swrm_clk_request(swrm, true)) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +05301740 dev_err_ratelimited(swrm->dev, "%s:clk request failed\n",
1741 __func__);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301742 mutex_unlock(&swrm->reslock);
1743 goto exit;
1744 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301745 mutex_unlock(&swrm->reslock);
1746
1747 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301748 intr_sts_masked = intr_sts & swrm->intr_mask;
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07001749
1750 trace_printk("%s: status: 0x%x \n", __func__, intr_sts_masked);
Ramprasad Katkam83303512018-10-11 17:34:22 +05301751handle_irq:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301752 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301753 value = intr_sts_masked & (1 << i);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301754 if (!value)
1755 continue;
1756
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301757 switch (value) {
1758 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1759 dev_dbg(swrm->dev, "Trigger irq to slave device\n");
1760 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301761 ret = swrm_find_alert_slave(swrm, status, &devnum);
1762 if (ret) {
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301763 dev_err_ratelimited(swrm->dev,
1764 "no slave alert found.spurious interrupt\n");
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05301765 break;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301766 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301767 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1768 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1769 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1770 SWRS_SCP_INT_STATUS_CLEAR_1);
1771 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1772 SWRS_SCP_INT_STATUS_CLEAR_1);
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301773
1774
1775 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1776 if (swr_dev->dev_num != devnum)
1777 continue;
1778 if (swr_dev->slave_irq) {
1779 do {
Ramprasad Katkam2586a4b2019-03-18 16:53:39 +05301780 swr_dev->slave_irq_pending = 0;
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301781 handle_nested_irq(
1782 irq_find_mapping(
1783 swr_dev->slave_irq, 0));
1784 } while (swr_dev->slave_irq_pending);
1785 }
1786
1787 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301788 break;
1789 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1790 dev_dbg(swrm->dev, "SWR new slave attached\n");
1791 break;
1792 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1793 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1794 if (status == swrm->slave_status) {
1795 dev_dbg(swrm->dev,
1796 "%s: No change in slave status: %d\n",
1797 __func__, status);
1798 break;
1799 }
1800 chg_sts = swrm_check_slave_change_status(swrm, status,
1801 &devnum);
1802 switch (chg_sts) {
1803 case SWR_NOT_PRESENT:
1804 dev_dbg(swrm->dev, "device %d got detached\n",
1805 devnum);
1806 break;
1807 case SWR_ATTACHED_OK:
1808 dev_dbg(swrm->dev, "device %d got attached\n",
1809 devnum);
Ramprasad Katkamdebe8932018-09-25 18:08:18 +05301810 /* enable host irq from slave device*/
1811 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1812 SWRS_SCP_INT_STATUS_CLEAR_1);
1813 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1814 SWRS_SCP_INT_STATUS_MASK_1);
1815
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301816 break;
1817 case SWR_ALERT:
1818 dev_dbg(swrm->dev,
1819 "device %d has pending interrupt\n",
1820 devnum);
1821 break;
1822 }
1823 break;
1824 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1825 dev_err_ratelimited(swrm->dev,
1826 "SWR bus clsh detected\n");
1827 break;
1828 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1829 dev_dbg(swrm->dev, "SWR read FIFO overflow\n");
1830 break;
1831 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1832 dev_dbg(swrm->dev, "SWR read FIFO underflow\n");
1833 break;
1834 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1835 dev_dbg(swrm->dev, "SWR write FIFO overflow\n");
1836 break;
1837 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1838 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1839 dev_err_ratelimited(swrm->dev,
1840 "SWR CMD error, fifo status 0x%x, flushing fifo\n",
1841 value);
1842 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1843 break;
1844 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301845 dev_err_ratelimited(swrm->dev, "SWR Port collision detected\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301846 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301847 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301848 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301849 break;
1850 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1851 dev_dbg(swrm->dev, "SWR read enable valid mismatch\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301852 swrm->intr_mask &=
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301853 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1854 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301855 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301856 break;
1857 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1858 complete(&swrm->broadcast);
1859 dev_dbg(swrm->dev, "SWR cmd id finished\n");
1860 break;
1861 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_AUTO_ENUM_FINISHED:
1862 break;
1863 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED:
1864 break;
1865 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL:
1866 break;
1867 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED:
1868 complete(&swrm->reset);
1869 break;
1870 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED:
1871 break;
1872 default:
1873 dev_err_ratelimited(swrm->dev,
1874 "SWR unknown interrupt\n");
1875 ret = IRQ_NONE;
1876 break;
1877 }
1878 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301879 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1880 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
Ramprasad Katkam83303512018-10-11 17:34:22 +05301881
1882 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301883 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301884
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301885 if (intr_sts_masked) {
Ramprasad Katkam83303512018-10-11 17:34:22 +05301886 dev_dbg(swrm->dev, "%s: new interrupt received\n", __func__);
1887 goto handle_irq;
1888 }
1889
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301890 mutex_lock(&swrm->reslock);
1891 swrm_clk_request(swrm, false);
1892 mutex_unlock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301893exit:
Ramprasad Katkam57349872018-11-11 18:34:57 +05301894 swrm_unlock_sleep(swrm);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07001895 trace_printk("%s exit\n", __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301896 return ret;
1897}
1898
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301899static irqreturn_t swr_mstr_interrupt_v2(int irq, void *dev)
1900{
1901 struct swr_mstr_ctrl *swrm = dev;
1902 u32 value, intr_sts, intr_sts_masked;
1903 u32 temp = 0;
1904 u32 status, chg_sts, i;
1905 u8 devnum = 0;
1906 int ret = IRQ_HANDLED;
1907 struct swr_device *swr_dev;
1908 struct swr_master *mstr = &swrm->master;
1909
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07001910 trace_printk("%s enter\n", __func__);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301911 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1912 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1913 return IRQ_NONE;
1914 }
1915
1916 mutex_lock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301917 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
1918 ret = IRQ_NONE;
1919 goto exit;
1920 }
1921 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
1922 ret = IRQ_NONE;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301923 goto err_audio_hw_vote;
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001924 }
Karthikeyan Mani4bee1db2019-09-18 17:58:41 -07001925 ret = swrm_clk_request(swrm, true);
1926 if (ret) {
1927 dev_err(dev, "%s: swrm clk failed\n", __func__);
1928 ret = IRQ_NONE;
1929 goto err_audio_core_vote;
1930 }
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301931 mutex_unlock(&swrm->reslock);
1932
1933 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1934 intr_sts_masked = intr_sts & swrm->intr_mask;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301935
1936 dev_dbg(swrm->dev, "%s: status: 0x%x \n", __func__, intr_sts_masked);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07001937 trace_printk("%s: status: 0x%x \n", __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301938handle_irq:
1939 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
1940 value = intr_sts_masked & (1 << i);
1941 if (!value)
1942 continue;
1943
1944 switch (value) {
1945 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1946 dev_dbg(swrm->dev, "%s: Trigger irq to slave device\n",
1947 __func__);
1948 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1949 ret = swrm_find_alert_slave(swrm, status, &devnum);
1950 if (ret) {
1951 dev_err_ratelimited(swrm->dev,
1952 "%s: no slave alert found.spurious interrupt\n",
1953 __func__);
1954 break;
1955 }
1956 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1957 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1958 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1959 SWRS_SCP_INT_STATUS_CLEAR_1);
1960 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1961 SWRS_SCP_INT_STATUS_CLEAR_1);
1962
1963
1964 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1965 if (swr_dev->dev_num != devnum)
1966 continue;
1967 if (swr_dev->slave_irq) {
1968 do {
Meng Wang31a7ef12019-12-18 10:36:29 +08001969 swr_dev->slave_irq_pending = 0;
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301970 handle_nested_irq(
1971 irq_find_mapping(
1972 swr_dev->slave_irq, 0));
1973 } while (swr_dev->slave_irq_pending);
1974 }
1975
1976 }
1977 break;
1978 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1979 dev_dbg(swrm->dev, "%s: SWR new slave attached\n",
1980 __func__);
1981 break;
1982 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1983 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
Laxminath Kasam44cedb82019-11-20 17:37:23 +05301984 swrm_enable_slave_irq(swrm);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301985 if (status == swrm->slave_status) {
1986 dev_dbg(swrm->dev,
1987 "%s: No change in slave status: %d\n",
1988 __func__, status);
1989 break;
1990 }
1991 chg_sts = swrm_check_slave_change_status(swrm, status,
1992 &devnum);
1993 switch (chg_sts) {
1994 case SWR_NOT_PRESENT:
1995 dev_dbg(swrm->dev,
1996 "%s: device %d got detached\n",
1997 __func__, devnum);
Vatsal Bucha21ba0772020-02-28 20:36:46 +05301998 if (devnum == 0) {
1999 /*
2000 * enable host irq if device 0 detached
2001 * as hw will mask host_irq at slave
2002 * but will not unmask it afterwards.
2003 */
2004 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
2005 SWRS_SCP_INT_STATUS_CLEAR_1);
2006 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
2007 SWRS_SCP_INT_STATUS_MASK_1);
2008 }
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302009 break;
2010 case SWR_ATTACHED_OK:
2011 dev_dbg(swrm->dev,
2012 "%s: device %d got attached\n",
2013 __func__, devnum);
2014 /* enable host irq from slave device*/
2015 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
2016 SWRS_SCP_INT_STATUS_CLEAR_1);
2017 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
2018 SWRS_SCP_INT_STATUS_MASK_1);
2019
2020 break;
2021 case SWR_ALERT:
2022 dev_dbg(swrm->dev,
2023 "%s: device %d has pending interrupt\n",
2024 __func__, devnum);
2025 break;
2026 }
2027 break;
2028 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
2029 dev_err_ratelimited(swrm->dev,
2030 "%s: SWR bus clsh detected\n",
2031 __func__);
Aditya Bavanarif0106d92020-01-31 17:01:21 +05302032 swrm->intr_mask &=
2033 ~SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET;
2034 swr_master_write(swrm,
2035 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
2036 swrm->intr_mask);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302037 break;
2038 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
Vatsal Buchabd68a7c2020-06-08 10:41:56 +05302039 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
2040 dev_err(swrm->dev,
2041 "%s: SWR read FIFO overflow fifo status 0x%x\n",
2042 __func__, value);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302043 break;
2044 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
Vatsal Buchabd68a7c2020-06-08 10:41:56 +05302045 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
2046 dev_err(swrm->dev,
2047 "%s: SWR read FIFO underflow fifo status 0x%x\n",
2048 __func__, value);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302049 break;
2050 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
Vatsal Buchabd68a7c2020-06-08 10:41:56 +05302051 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
2052 dev_err(swrm->dev,
2053 "%s: SWR write FIFO overflow fifo status %x\n",
2054 __func__, value);
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302055 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302056 break;
2057 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
2058 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
2059 dev_err_ratelimited(swrm->dev,
2060 "%s: SWR CMD error, fifo status 0x%x, flushing fifo\n",
2061 __func__, value);
2062 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
2063 break;
2064 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
2065 dev_err_ratelimited(swrm->dev,
2066 "%s: SWR Port collision detected\n",
2067 __func__);
2068 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
2069 swr_master_write(swrm,
2070 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
2071 break;
2072 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
2073 dev_dbg(swrm->dev,
2074 "%s: SWR read enable valid mismatch\n",
2075 __func__);
2076 swrm->intr_mask &=
2077 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
2078 swr_master_write(swrm,
2079 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
2080 break;
2081 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
2082 complete(&swrm->broadcast);
2083 dev_dbg(swrm->dev, "%s: SWR cmd id finished\n",
2084 __func__);
2085 break;
2086 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED_V2:
2087 break;
2088 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL_V2:
2089 break;
2090 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED_V2:
Laxminath Kasame2291972019-11-08 14:51:59 +05302091 swrm_check_link_status(swrm, 0x1);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302092 break;
2093 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED_V2:
2094 break;
2095 case SWRM_INTERRUPT_STATUS_EXT_CLK_STOP_WAKEUP:
2096 if (swrm->state == SWR_MSTR_UP)
2097 dev_dbg(swrm->dev,
2098 "%s:SWR Master is already up\n",
2099 __func__);
2100 else
2101 dev_err_ratelimited(swrm->dev,
2102 "%s: SWR wokeup during clock stop\n",
2103 __func__);
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05302104 /* It might be possible the slave device gets reset
2105 * and slave interrupt gets missed. So re-enable
2106 * Host IRQ and process slave pending
2107 * interrupts, if any.
2108 */
2109 swrm_enable_slave_irq(swrm);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302110 break;
2111 default:
2112 dev_err_ratelimited(swrm->dev,
2113 "%s: SWR unknown interrupt value: %d\n",
2114 __func__, value);
2115 ret = IRQ_NONE;
2116 break;
2117 }
2118 }
2119 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
2120 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
2121
2122 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
2123 intr_sts_masked = intr_sts & swrm->intr_mask;
2124
2125 if (intr_sts_masked) {
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05302126 dev_dbg(swrm->dev, "%s: new interrupt received 0x%x\n",
2127 __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302128 goto handle_irq;
2129 }
2130
2131 mutex_lock(&swrm->reslock);
2132 swrm_clk_request(swrm, false);
Karthikeyan Mani4bee1db2019-09-18 17:58:41 -07002133err_audio_core_vote:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302134 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
Sudheer Papothi06f43412019-07-09 03:32:54 +05302135
2136err_audio_hw_vote:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302137 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07002138exit:
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302139 mutex_unlock(&swrm->reslock);
2140 swrm_unlock_sleep(swrm);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07002141 trace_printk("%s exit\n", __func__);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302142 return ret;
2143}
2144
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302145static irqreturn_t swrm_wakeup_interrupt(int irq, void *dev)
2146{
2147 struct swr_mstr_ctrl *swrm = dev;
2148 int ret = IRQ_HANDLED;
2149
2150 if (!swrm || !(swrm->dev)) {
2151 pr_err("%s: swrm or dev is null\n", __func__);
2152 return IRQ_NONE;
2153 }
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302154
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07002155 trace_printk("%s enter\n", __func__);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302156 mutex_lock(&swrm->devlock);
2157 if (!swrm->dev_up) {
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302158 if (swrm->wake_irq > 0) {
2159 if (unlikely(!irq_get_irq_data(swrm->wake_irq))) {
2160 pr_err("%s: irq data is NULL\n", __func__);
2161 mutex_unlock(&swrm->devlock);
2162 return IRQ_NONE;
2163 }
2164 mutex_lock(&swrm->irq_lock);
2165 if (!irqd_irq_disabled(
2166 irq_get_irq_data(swrm->wake_irq)))
2167 disable_irq_nosync(swrm->wake_irq);
2168 mutex_unlock(&swrm->irq_lock);
2169 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302170 mutex_unlock(&swrm->devlock);
2171 return ret;
2172 }
2173 mutex_unlock(&swrm->devlock);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05302174 if (unlikely(swrm_lock_sleep(swrm) == false)) {
2175 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
2176 goto exit;
2177 }
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302178 if (swrm->wake_irq > 0) {
2179 if (unlikely(!irq_get_irq_data(swrm->wake_irq))) {
2180 pr_err("%s: irq data is NULL\n", __func__);
2181 return IRQ_NONE;
2182 }
2183 mutex_lock(&swrm->irq_lock);
2184 if (!irqd_irq_disabled(
2185 irq_get_irq_data(swrm->wake_irq)))
2186 disable_irq_nosync(swrm->wake_irq);
2187 mutex_unlock(&swrm->irq_lock);
2188 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302189 pm_runtime_get_sync(swrm->dev);
2190 pm_runtime_mark_last_busy(swrm->dev);
2191 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05302192 swrm_unlock_sleep(swrm);
2193exit:
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07002194 trace_printk("%s exit\n", __func__);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302195 return ret;
2196}
2197
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302198static void swrm_wakeup_work(struct work_struct *work)
2199{
2200 struct swr_mstr_ctrl *swrm;
2201
2202 swrm = container_of(work, struct swr_mstr_ctrl,
2203 wakeup_work);
2204 if (!swrm || !(swrm->dev)) {
2205 pr_err("%s: swrm or dev is null\n", __func__);
2206 return;
2207 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302208
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07002209 trace_printk("%s enter\n", __func__);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302210 mutex_lock(&swrm->devlock);
2211 if (!swrm->dev_up) {
2212 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302213 goto exit;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302214 }
2215 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302216 if (unlikely(swrm_lock_sleep(swrm) == false)) {
2217 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
2218 goto exit;
2219 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302220 pm_runtime_get_sync(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302221 pm_runtime_mark_last_busy(swrm->dev);
2222 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302223 swrm_unlock_sleep(swrm);
2224exit:
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07002225 trace_printk("%s exit\n", __func__);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302226 pm_relax(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302227}
2228
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302229static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
2230{
2231 u32 val;
2232
2233 swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
2234 val = (swrm->slave_status >> (devnum * 2));
2235 val &= SWRM_MCP_SLV_STATUS_MASK;
2236 return val;
2237}
2238
2239static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
2240 u8 *dev_num)
2241{
2242 int i;
2243 u64 id = 0;
2244 int ret = -EINVAL;
2245 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
2246 struct swr_device *swr_dev;
2247 u32 num_dev = 0;
2248
2249 if (!swrm) {
2250 pr_err("%s: Invalid handle to swr controller\n",
2251 __func__);
2252 return ret;
2253 }
2254 if (swrm->num_dev)
2255 num_dev = swrm->num_dev;
2256 else
2257 num_dev = mstr->num_dev;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302258
2259 mutex_lock(&swrm->devlock);
2260 if (!swrm->dev_up) {
2261 mutex_unlock(&swrm->devlock);
2262 return ret;
2263 }
2264 mutex_unlock(&swrm->devlock);
2265
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302266 pm_runtime_get_sync(swrm->dev);
2267 for (i = 1; i < (num_dev + 1); i++) {
2268 id = ((u64)(swr_master_read(swrm,
2269 SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
2270 id |= swr_master_read(swrm,
2271 SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302272
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302273 /*
2274 * As pm_runtime_get_sync() brings all slaves out of reset
2275 * update logical device number for all slaves.
2276 */
2277 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2278 if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
2279 u32 status = swrm_get_device_status(swrm, i);
2280
2281 if ((status == 0x01) || (status == 0x02)) {
2282 swr_dev->dev_num = i;
2283 if ((id & SWR_DEV_ID_MASK) == dev_id) {
2284 *dev_num = i;
2285 ret = 0;
2286 }
2287 dev_dbg(swrm->dev,
Xiao Lid8bb93c2020-01-07 12:59:05 +08002288 "%s: devnum %d is assigned for dev addr %llx\n",
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302289 __func__, i, swr_dev->addr);
2290 }
2291 }
2292 }
2293 }
2294 if (ret)
2295 dev_err(swrm->dev, "%s: device 0x%llx is not ready\n",
2296 __func__, dev_id);
2297
2298 pm_runtime_mark_last_busy(swrm->dev);
2299 pm_runtime_put_autosuspend(swrm->dev);
2300 return ret;
2301}
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302302
2303static void swrm_device_wakeup_vote(struct swr_master *mstr)
2304{
2305 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
2306
2307 if (!swrm) {
2308 pr_err("%s: Invalid handle to swr controller\n",
2309 __func__);
2310 return;
2311 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302312 if (unlikely(swrm_lock_sleep(swrm) == false)) {
2313 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
2314 return;
2315 }
Aditya Bavanarieb044612019-12-22 17:14:15 +05302316 mutex_lock(&swrm->reslock);
2317 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true))
2318 dev_err(swrm->dev, "%s:lpass core hw enable failed\n",
2319 __func__);
2320 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true))
2321 dev_err(swrm->dev, "%s:lpass audio hw enable failed\n",
2322 __func__);
2323 mutex_unlock(&swrm->reslock);
2324
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302325 pm_runtime_get_sync(swrm->dev);
2326}
2327
2328static void swrm_device_wakeup_unvote(struct swr_master *mstr)
2329{
2330 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
2331
2332 if (!swrm) {
2333 pr_err("%s: Invalid handle to swr controller\n",
2334 __func__);
2335 return;
2336 }
2337 pm_runtime_mark_last_busy(swrm->dev);
2338 pm_runtime_put_autosuspend(swrm->dev);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302339
Aditya Bavanarieb044612019-12-22 17:14:15 +05302340 mutex_lock(&swrm->reslock);
2341 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2342 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
2343 mutex_unlock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302344
Ramprasad Katkam57349872018-11-11 18:34:57 +05302345 swrm_unlock_sleep(swrm);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302346}
2347
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302348static int swrm_master_init(struct swr_mstr_ctrl *swrm)
2349{
Laxminath Kasam36956422020-06-01 19:23:48 +05302350 int ret = 0, i = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302351 u32 val;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302352 u8 row_ctrl = SWR_ROW_50;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302353 u8 col_ctrl = SWR_MIN_COL;
2354 u8 ssp_period = 1;
2355 u8 retry_cmd_num = 3;
2356 u32 reg[SWRM_MAX_INIT_REG];
2357 u32 value[SWRM_MAX_INIT_REG];
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302358 u32 temp = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302359 int len = 0;
2360
Laxminath Kasam36956422020-06-01 19:23:48 +05302361 /* SW workaround to gate hw_ctl for SWR version >=1.6 */
2362 if (swrm->version >= SWRM_VERSION_1_6) {
2363 if (swrm->swrm_hctl_reg) {
2364 temp = ioread32(swrm->swrm_hctl_reg);
2365 temp &= 0xFFFFFFFD;
2366 iowrite32(temp, swrm->swrm_hctl_reg);
2367 usleep_range(500, 505);
2368 temp = ioread32(swrm->swrm_hctl_reg);
2369 dev_dbg(swrm->dev, "%s: hctl_reg val: 0x%x\n",
2370 __func__, temp);
2371 }
2372 }
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302373 ssp_period = swrm_get_ssp_period(swrm, SWRM_ROW_50,
2374 SWRM_COL_02, SWRM_FRAME_SYNC_SEL);
2375 dev_dbg(swrm->dev, "%s: ssp_period: %d\n", __func__, ssp_period);
2376
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302377 /* Clear Rows and Cols */
2378 val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
2379 (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302380 ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302381
2382 reg[len] = SWRM_MCP_FRAME_CTRL_BANK_ADDR(0);
2383 value[len++] = val;
2384
2385 /* Set Auto enumeration flag */
2386 reg[len] = SWRM_ENUMERATOR_CFG_ADDR;
2387 value[len++] = 1;
2388
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302389 /* Configure No pings */
2390 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2391 val &= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK;
2392 val |= (0x1f << SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT);
2393 reg[len] = SWRM_MCP_CFG_ADDR;
2394 value[len++] = val;
2395
2396 /* Configure number of retries of a read/write cmd */
2397 val = (retry_cmd_num << SWRM_CMD_FIFO_CFG_NUM_OF_CMD_RETRY_SHFT);
2398 reg[len] = SWRM_CMD_FIFO_CFG_ADDR;
2399 value[len++] = val;
2400
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302401 reg[len] = SWRM_MCP_BUS_CTRL_ADDR;
2402 value[len++] = 0x2;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302403
Ramprasad Katkam83303512018-10-11 17:34:22 +05302404 /* Set IRQ to PULSE */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302405 reg[len] = SWRM_COMP_CFG_ADDR;
Ramprasad Katkam83303512018-10-11 17:34:22 +05302406 value[len++] = 0x02;
2407
2408 reg[len] = SWRM_COMP_CFG_ADDR;
2409 value[len++] = 0x03;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302410
2411 reg[len] = SWRM_INTERRUPT_CLEAR;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302412 value[len++] = 0xFFFFFFFF;
2413
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302414 swrm->intr_mask = SWRM_INTERRUPT_STATUS_MASK;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302415 /* Mask soundwire interrupts */
2416 reg[len] = SWRM_INTERRUPT_MASK_ADDR;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302417 value[len++] = swrm->intr_mask;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302418
2419 reg[len] = SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302420 value[len++] = swrm->intr_mask;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302421
2422 swr_master_bulk_write(swrm, reg, value, len);
2423
Laxminath Kasamcafe0732019-11-20 17:31:58 +05302424 if (!swrm_check_link_status(swrm, 0x1)) {
2425 dev_err(swrm->dev,
2426 "%s: swr link failed to connect\n",
2427 __func__);
Laxminath Kasam36956422020-06-01 19:23:48 +05302428 for (i = 0; i < len; i++) {
2429 usleep_range(50, 55);
2430 dev_err(swrm->dev,
2431 "%s:reg:0x%x val:0x%x\n",
2432 __func__,
2433 reg[i], swr_master_read(swrm, reg[i]));
2434 }
Laxminath Kasamcafe0732019-11-20 17:31:58 +05302435 return -EINVAL;
2436 }
Sudheer Papothi63f48152018-11-15 01:08:03 +05302437 /*
2438 * For SWR master version 1.5.1, continue
2439 * execute on command ignore.
2440 */
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302441 /* Execute it for versions >= 1.5.1 */
2442 if (swrm->version >= SWRM_VERSION_1_5_1)
Sudheer Papothi63f48152018-11-15 01:08:03 +05302443 swr_master_write(swrm, SWRM_CMD_FIFO_CFG_ADDR,
2444 (swr_master_read(swrm,
2445 SWRM_CMD_FIFO_CFG_ADDR) | 0x80000000));
2446
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302447 return ret;
2448}
2449
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302450static int swrm_event_notify(struct notifier_block *self,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302451 unsigned long action, void *data)
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302452{
2453 struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302454 event_notifier);
2455
2456 if (!swrm || !(swrm->dev)) {
2457 pr_err("%s: swrm or dev is NULL\n", __func__);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302458 return -EINVAL;
2459 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302460 switch (action) {
2461 case MSM_AUD_DC_EVENT:
2462 schedule_work(&(swrm->dc_presence_work));
2463 break;
2464 case SWR_WAKE_IRQ_EVENT:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302465 if (swrm->ipc_wakeup && !swrm->ipc_wakeup_triggered) {
2466 swrm->ipc_wakeup_triggered = true;
Ramprasad Katkam57349872018-11-11 18:34:57 +05302467 pm_stay_awake(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302468 schedule_work(&swrm->wakeup_work);
Ramprasad Katkamcd61c6e2018-09-18 13:22:58 +05302469 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302470 break;
2471 default:
2472 dev_err(swrm->dev, "%s: invalid event type: %lu\n",
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302473 __func__, action);
2474 return -EINVAL;
2475 }
2476
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302477 return 0;
2478}
2479
2480static void swrm_notify_work_fn(struct work_struct *work)
2481{
2482 struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302483 dc_presence_work);
2484
2485 if (!swrm || !swrm->pdev) {
2486 pr_err("%s: swrm or pdev is NULL\n", __func__);
2487 return;
2488 }
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302489 swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL);
2490}
2491
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302492static int swrm_probe(struct platform_device *pdev)
2493{
2494 struct swr_mstr_ctrl *swrm;
2495 struct swr_ctrl_platform_data *pdata;
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302496 u32 i, num_ports, port_num, port_type, ch_mask, swrm_hctl_reg = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302497 u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0;
2498 int ret = 0;
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302499 struct clk *lpass_core_hw_vote = NULL;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302500 struct clk *lpass_core_audio = NULL;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302501
2502 /* Allocate soundwire master driver structure */
2503 swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl),
2504 GFP_KERNEL);
2505 if (!swrm) {
2506 ret = -ENOMEM;
2507 goto err_memory_fail;
2508 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302509 swrm->pdev = pdev;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302510 swrm->dev = &pdev->dev;
2511 platform_set_drvdata(pdev, swrm);
2512 swr_set_ctrl_data(&swrm->master, swrm);
2513 pdata = dev_get_platdata(&pdev->dev);
2514 if (!pdata) {
2515 dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
2516 __func__);
2517 ret = -EINVAL;
2518 goto err_pdata_fail;
2519 }
2520 swrm->handle = (void *)pdata->handle;
2521 if (!swrm->handle) {
2522 dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
2523 __func__);
2524 ret = -EINVAL;
2525 goto err_pdata_fail;
2526 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302527 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr_master_id",
2528 &swrm->master_id);
2529 if (ret) {
2530 dev_err(&pdev->dev, "%s: failed to get master id\n", __func__);
2531 goto err_pdata_fail;
2532 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302533 if (!(of_property_read_u32(pdev->dev.of_node,
2534 "swrm-io-base", &swrm->swrm_base_reg)))
2535 ret = of_property_read_u32(pdev->dev.of_node,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302536 "swrm-io-base", &swrm->swrm_base_reg);
2537 if (!swrm->swrm_base_reg) {
2538 swrm->read = pdata->read;
2539 if (!swrm->read) {
2540 dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
2541 __func__);
2542 ret = -EINVAL;
2543 goto err_pdata_fail;
2544 }
2545 swrm->write = pdata->write;
2546 if (!swrm->write) {
2547 dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
2548 __func__);
2549 ret = -EINVAL;
2550 goto err_pdata_fail;
2551 }
2552 swrm->bulk_write = pdata->bulk_write;
2553 if (!swrm->bulk_write) {
2554 dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
2555 __func__);
2556 ret = -EINVAL;
2557 goto err_pdata_fail;
2558 }
2559 } else {
2560 swrm->swrm_dig_base = devm_ioremap(&pdev->dev,
2561 swrm->swrm_base_reg, SWRM_MAX_REGISTER);
2562 }
2563
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07002564 swrm->core_vote = pdata->core_vote;
Laxminath Kasamc7bfab92019-08-27 16:19:14 +05302565 if (!(of_property_read_u32(pdev->dev.of_node,
2566 "qcom,swrm-hctl-reg", &swrm_hctl_reg)))
2567 swrm->swrm_hctl_reg = devm_ioremap(&pdev->dev,
2568 swrm_hctl_reg, 0x4);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302569 swrm->clk = pdata->clk;
2570 if (!swrm->clk) {
2571 dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
2572 __func__);
2573 ret = -EINVAL;
2574 goto err_pdata_fail;
2575 }
Laxminath Kasamea6cbee2020-04-28 00:02:32 +05302576 swrm->pinctrl_setup = pdata->pinctrl_setup;
2577
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302578 if (of_property_read_u32(pdev->dev.of_node,
2579 "qcom,swr-clock-stop-mode0",
2580 &swrm->clk_stop_mode0_supp)) {
2581 swrm->clk_stop_mode0_supp = FALSE;
2582 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302583
2584 ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
2585 &swrm->num_dev);
2586 if (ret) {
2587 dev_dbg(&pdev->dev, "%s: Looking up %s property failed\n",
2588 __func__, "qcom,swr-num-dev");
2589 } else {
Sudheer Papothiae5c3632019-11-27 06:52:06 +05302590 if (swrm->num_dev > SWRM_NUM_AUTO_ENUM_SLAVES) {
Ramprasad Katkam57349872018-11-11 18:34:57 +05302591 dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
Sudheer Papothiae5c3632019-11-27 06:52:06 +05302592 __func__, swrm->num_dev,
2593 SWRM_NUM_AUTO_ENUM_SLAVES);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302594 ret = -EINVAL;
2595 goto err_pdata_fail;
2596 }
2597 }
2598
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302599 /* Parse soundwire port mapping */
2600 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
2601 &num_ports);
2602 if (ret) {
2603 dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
2604 goto err_pdata_fail;
2605 }
2606 swrm->num_ports = num_ports;
2607
2608 if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
2609 &map_size)) {
2610 dev_err(swrm->dev, "missing port mapping\n");
2611 goto err_pdata_fail;
2612 }
2613
2614 map_length = map_size / (3 * sizeof(u32));
2615 if (num_ports > SWR_MSTR_PORT_LEN) {
2616 dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
2617 __func__);
2618 ret = -EINVAL;
2619 goto err_pdata_fail;
2620 }
2621 temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
2622
2623 if (!temp) {
2624 ret = -ENOMEM;
2625 goto err_pdata_fail;
2626 }
2627 ret = of_property_read_u32_array(pdev->dev.of_node,
2628 "qcom,swr-port-mapping", temp, 3 * map_length);
2629 if (ret) {
2630 dev_err(swrm->dev, "%s: Failed to read port mapping\n",
2631 __func__);
2632 goto err_pdata_fail;
2633 }
2634
2635 for (i = 0; i < map_length; i++) {
2636 port_num = temp[3 * i];
2637 port_type = temp[3 * i + 1];
2638 ch_mask = temp[3 * i + 2];
2639
2640 if (port_num != old_port_num)
2641 ch_iter = 0;
2642 swrm->port_mapping[port_num][ch_iter].port_type = port_type;
2643 swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
2644 old_port_num = port_num;
2645 }
2646 devm_kfree(&pdev->dev, temp);
2647
2648 swrm->reg_irq = pdata->reg_irq;
2649 swrm->master.read = swrm_read;
2650 swrm->master.write = swrm_write;
2651 swrm->master.bulk_write = swrm_bulk_write;
2652 swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
2653 swrm->master.connect_port = swrm_connect_port;
2654 swrm->master.disconnect_port = swrm_disconnect_port;
2655 swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
2656 swrm->master.remove_from_group = swrm_remove_from_group;
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302657 swrm->master.device_wakeup_vote = swrm_device_wakeup_vote;
2658 swrm->master.device_wakeup_unvote = swrm_device_wakeup_unvote;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302659 swrm->master.dev.parent = &pdev->dev;
2660 swrm->master.dev.of_node = pdev->dev.of_node;
2661 swrm->master.num_port = 0;
2662 swrm->rcmd_id = 0;
2663 swrm->wcmd_id = 0;
2664 swrm->slave_status = 0;
2665 swrm->num_rx_chs = 0;
2666 swrm->clk_ref_count = 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302667 swrm->swr_irq_wakeup_capable = 0;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302668 swrm->mclk_freq = MCLK_FREQ;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302669 swrm->bus_clk = MCLK_FREQ;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302670 swrm->dev_up = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302671 swrm->state = SWR_MSTR_UP;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302672 swrm->ipc_wakeup = false;
2673 swrm->ipc_wakeup_triggered = false;
Vatsal Bucha687f9822020-04-01 18:21:41 +05302674 swrm->disable_div2_clk_switch = FALSE;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302675 init_completion(&swrm->reset);
2676 init_completion(&swrm->broadcast);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302677 init_completion(&swrm->clk_off_complete);
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302678 mutex_init(&swrm->irq_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302679 mutex_init(&swrm->mlock);
2680 mutex_init(&swrm->reslock);
2681 mutex_init(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302682 mutex_init(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302683 mutex_init(&swrm->clklock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302684 mutex_init(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302685 mutex_init(&swrm->pm_lock);
2686 swrm->wlock_holders = 0;
2687 swrm->pm_state = SWRM_PM_SLEEPABLE;
2688 init_waitqueue_head(&swrm->pm_wq);
2689 pm_qos_add_request(&swrm->pm_qos_req,
2690 PM_QOS_CPU_DMA_LATENCY,
2691 PM_QOS_DEFAULT_VALUE);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302692
2693 for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++)
2694 INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
2695
Vatsal Bucha687f9822020-04-01 18:21:41 +05302696 if (of_property_read_u32(pdev->dev.of_node,
2697 "qcom,disable-div2-clk-switch",
2698 &swrm->disable_div2_clk_switch)) {
2699 swrm->disable_div2_clk_switch = FALSE;
2700 }
2701
Sudheer Papothi06f43412019-07-09 03:32:54 +05302702 /* Register LPASS core hw vote */
2703 lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
2704 if (IS_ERR(lpass_core_hw_vote)) {
2705 ret = PTR_ERR(lpass_core_hw_vote);
2706 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2707 __func__, "lpass_core_hw_vote", ret);
2708 lpass_core_hw_vote = NULL;
2709 ret = 0;
2710 }
2711 swrm->lpass_core_hw_vote = lpass_core_hw_vote;
2712
2713 /* Register LPASS audio core vote */
2714 lpass_core_audio = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
2715 if (IS_ERR(lpass_core_audio)) {
2716 ret = PTR_ERR(lpass_core_audio);
2717 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2718 __func__, "lpass_core_audio", ret);
2719 lpass_core_audio = NULL;
2720 ret = 0;
2721 }
2722 swrm->lpass_core_audio = lpass_core_audio;
2723
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302724 if (swrm->reg_irq) {
2725 ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
2726 SWR_IRQ_REGISTER);
2727 if (ret) {
2728 dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
2729 __func__, ret);
2730 goto err_irq_fail;
2731 }
2732 } else {
2733 swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
2734 if (swrm->irq < 0) {
2735 dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
2736 __func__, swrm->irq);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302737 goto err_irq_fail;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302738 }
2739
2740 ret = request_threaded_irq(swrm->irq, NULL,
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302741 swr_mstr_interrupt_v2,
Ramprasad Katkam83303512018-10-11 17:34:22 +05302742 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302743 "swr_master_irq", swrm);
2744 if (ret) {
2745 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2746 __func__, ret);
2747 goto err_irq_fail;
2748 }
2749
2750 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302751 /* Make inband tx interrupts as wakeup capable for slave irq */
2752 ret = of_property_read_u32(pdev->dev.of_node,
2753 "qcom,swr-mstr-irq-wakeup-capable",
2754 &swrm->swr_irq_wakeup_capable);
2755 if (ret)
2756 dev_dbg(swrm->dev, "%s: swrm irq wakeup capable not defined\n",
2757 __func__);
2758 if (swrm->swr_irq_wakeup_capable)
2759 irq_set_irq_wake(swrm->irq, 1);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302760 ret = swr_register_master(&swrm->master);
2761 if (ret) {
2762 dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
2763 goto err_mstr_fail;
2764 }
2765
2766 /* Add devices registered with board-info as the
2767 * controller will be up now
2768 */
2769 swr_master_add_boarddevices(&swrm->master);
2770 mutex_lock(&swrm->mlock);
2771 swrm_clk_request(swrm, true);
Laxminath Kasam4696fff2019-11-26 16:07:11 +05302772 swrm->version = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302773 ret = swrm_master_init(swrm);
2774 if (ret < 0) {
2775 dev_err(&pdev->dev,
2776 "%s: Error in master Initialization , err %d\n",
2777 __func__, ret);
2778 mutex_unlock(&swrm->mlock);
Laxminath Kasam36956422020-06-01 19:23:48 +05302779 ret = -EPROBE_DEFER;
Laxminath Kasamec8c9092019-12-17 13:12:58 +05302780 goto err_mstr_init_fail;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302781 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302782
2783 mutex_unlock(&swrm->mlock);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302784 INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302785
2786 if (pdev->dev.of_node)
2787 of_register_swr_devices(&swrm->master);
2788
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302789#ifdef CONFIG_DEBUG_FS
2790 swrm->debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
2791 if (!IS_ERR(swrm->debugfs_swrm_dent)) {
2792 swrm->debugfs_peek = debugfs_create_file("swrm_peek",
2793 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2794 (void *) swrm, &swrm_debug_read_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302795
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302796 swrm->debugfs_poke = debugfs_create_file("swrm_poke",
2797 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2798 (void *) swrm, &swrm_debug_write_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302799
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302800 swrm->debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
2801 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2802 (void *) swrm,
2803 &swrm_debug_dump_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302804 }
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302805#endif
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302806 ret = device_init_wakeup(swrm->dev, true);
2807 if (ret) {
2808 dev_err(swrm->dev, "Device wakeup init failed: %d\n", ret);
2809 goto err_irq_wakeup_fail;
2810 }
2811
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302812 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
2813 pm_runtime_use_autosuspend(&pdev->dev);
2814 pm_runtime_set_active(&pdev->dev);
2815 pm_runtime_enable(&pdev->dev);
2816 pm_runtime_mark_last_busy(&pdev->dev);
2817
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302818 INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn);
2819 swrm->event_notifier.notifier_call = swrm_event_notify;
2820 msm_aud_evt_register_client(&swrm->event_notifier);
2821
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302822 return 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302823err_irq_wakeup_fail:
2824 device_init_wakeup(swrm->dev, false);
Laxminath Kasamec8c9092019-12-17 13:12:58 +05302825err_mstr_init_fail:
2826 swr_unregister_master(&swrm->master);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302827err_mstr_fail:
Laxminath Kasam36956422020-06-01 19:23:48 +05302828 if (swrm->reg_irq) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302829 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2830 swrm, SWR_IRQ_FREE);
Laxminath Kasam36956422020-06-01 19:23:48 +05302831 } else if (swrm->irq) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302832 free_irq(swrm->irq, swrm);
Laxminath Kasam36956422020-06-01 19:23:48 +05302833 irqd_set_trigger_type(
2834 irq_get_irq_data(swrm->irq),
2835 IRQ_TYPE_NONE);
2836 }
2837 if (swrm->swr_irq_wakeup_capable)
2838 irq_set_irq_wake(swrm->irq, 0);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302839err_irq_fail:
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302840 mutex_destroy(&swrm->irq_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302841 mutex_destroy(&swrm->mlock);
2842 mutex_destroy(&swrm->reslock);
2843 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302844 mutex_destroy(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302845 mutex_destroy(&swrm->clklock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302846 mutex_destroy(&swrm->pm_lock);
2847 pm_qos_remove_request(&swrm->pm_qos_req);
2848
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302849err_pdata_fail:
2850err_memory_fail:
2851 return ret;
2852}
2853
2854static int swrm_remove(struct platform_device *pdev)
2855{
2856 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2857
Laxminath Kasam36956422020-06-01 19:23:48 +05302858 if (swrm->reg_irq) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302859 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2860 swrm, SWR_IRQ_FREE);
Laxminath Kasam36956422020-06-01 19:23:48 +05302861 } else if (swrm->irq) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302862 free_irq(swrm->irq, swrm);
Laxminath Kasam36956422020-06-01 19:23:48 +05302863 irqd_set_trigger_type(
2864 irq_get_irq_data(swrm->irq),
2865 IRQ_TYPE_NONE);
2866 } else if (swrm->wake_irq > 0) {
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302867 free_irq(swrm->wake_irq, swrm);
Laxminath Kasam36956422020-06-01 19:23:48 +05302868 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302869 if (swrm->swr_irq_wakeup_capable)
2870 irq_set_irq_wake(swrm->irq, 0);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302871 cancel_work_sync(&swrm->wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302872 pm_runtime_disable(&pdev->dev);
2873 pm_runtime_set_suspended(&pdev->dev);
2874 swr_unregister_master(&swrm->master);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302875 msm_aud_evt_unregister_client(&swrm->event_notifier);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302876 device_init_wakeup(swrm->dev, false);
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302877 mutex_destroy(&swrm->irq_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302878 mutex_destroy(&swrm->mlock);
2879 mutex_destroy(&swrm->reslock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302880 mutex_destroy(&swrm->iolock);
2881 mutex_destroy(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302882 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302883 mutex_destroy(&swrm->pm_lock);
2884 pm_qos_remove_request(&swrm->pm_qos_req);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302885 devm_kfree(&pdev->dev, swrm);
2886 return 0;
2887}
2888
2889static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
2890{
2891 u32 val;
2892
2893 dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
2894 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD);
2895 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2896 val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK;
2897 swr_master_write(swrm, SWRM_MCP_CFG_ADDR, val);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302898
2899 return 0;
2900}
2901
2902#ifdef CONFIG_PM
2903static int swrm_runtime_resume(struct device *dev)
2904{
2905 struct platform_device *pdev = to_platform_device(dev);
2906 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2907 int ret = 0;
Vatsal Buchae50b5002019-09-19 14:32:20 +05302908 bool swrm_clk_req_err = false;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302909 bool hw_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302910 struct swr_master *mstr = &swrm->master;
2911 struct swr_device *swr_dev;
2912
2913 dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
2914 __func__, swrm->state);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07002915 trace_printk("%s: pm_runtime: resume, state:%d\n",
2916 __func__, swrm->state);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302917 mutex_lock(&swrm->reslock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302918
Sudheer Papothi384addd2019-06-14 02:26:52 +05302919 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2920 dev_err(dev, "%s:lpass core hw enable failed\n",
2921 __func__);
2922 hw_core_err = true;
2923 }
2924 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2925 dev_err(dev, "%s:lpass audio hw enable failed\n",
2926 __func__);
Laxminath Kasamea6cbee2020-04-28 00:02:32 +05302927 swrm->aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002928 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302929
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302930 if ((swrm->state == SWR_MSTR_DOWN) ||
2931 (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) {
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302932 if (swrm->clk_stop_mode0_supp) {
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302933 if (swrm->wake_irq > 0) {
2934 if (unlikely(!irq_get_irq_data
2935 (swrm->wake_irq))) {
2936 pr_err("%s: irq data is NULL\n",
2937 __func__);
2938 mutex_unlock(&swrm->reslock);
2939 return IRQ_NONE;
2940 }
2941 mutex_lock(&swrm->irq_lock);
2942 if (!irqd_irq_disabled(
2943 irq_get_irq_data(swrm->wake_irq)))
2944 disable_irq_nosync(swrm->wake_irq);
2945 mutex_unlock(&swrm->irq_lock);
Laxminath Kasamea6cbee2020-04-28 00:02:32 +05302946 if (swrm->dmic_sva && swrm->pinctrl_setup)
2947 swrm->pinctrl_setup(swrm->handle,
2948 false);
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302949 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302950 if (swrm->ipc_wakeup)
2951 msm_aud_evt_blocking_notifier_call_chain(
2952 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302953 }
2954
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302955 if (swrm_clk_request(swrm, true)) {
2956 /*
2957 * Set autosuspend timer to 1 for
2958 * master to enter into suspend.
2959 */
Vatsal Buchae50b5002019-09-19 14:32:20 +05302960 swrm_clk_req_err = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302961 goto exit;
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302962 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302963 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302964 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2965 ret = swr_device_up(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302966 if (ret == -ENODEV) {
2967 dev_dbg(dev,
2968 "%s slave device up not implemented\n",
2969 __func__);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07002970 trace_printk(
2971 "%s slave device up not implemented\n",
2972 __func__);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302973 ret = 0;
2974 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302975 dev_err(dev,
2976 "%s: failed to wakeup swr dev %d\n",
2977 __func__, swr_dev->dev_num);
2978 swrm_clk_request(swrm, false);
2979 goto exit;
2980 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302981 }
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05302982 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2983 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2984 swrm_master_init(swrm);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302985 /* wait for hw enumeration to complete */
2986 usleep_range(100, 105);
Laxminath Kasame2291972019-11-08 14:51:59 +05302987 if (!swrm_check_link_status(swrm, 0x1))
Laxminath Kasam696b14b2019-12-03 22:07:34 +05302988 dev_dbg(dev, "%s:failed in connecting, ssr?\n",
2989 __func__);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302990 swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, 0x0,
2991 SWRS_SCP_INT_STATUS_MASK_1);
Karthikeyan Manif6821902019-05-21 17:31:24 -07002992 if (swrm->state == SWR_MSTR_SSR) {
2993 mutex_unlock(&swrm->reslock);
2994 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
2995 mutex_lock(&swrm->reslock);
2996 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302997 } else {
2998 /*wake up from clock stop*/
2999 swr_master_write(swrm, SWRM_MCP_BUS_CTRL_ADDR, 0x2);
Sudheer Papothi55fa5972019-09-28 02:50:27 +05303000 /* clear and enable bus clash interrupt */
3001 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x08);
3002 swrm->intr_mask |= 0x08;
3003 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR,
3004 swrm->intr_mask);
3005 swr_master_write(swrm,
3006 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
3007 swrm->intr_mask);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05303008 usleep_range(100, 105);
Laxminath Kasame2291972019-11-08 14:51:59 +05303009 if (!swrm_check_link_status(swrm, 0x1))
Laxminath Kasam696b14b2019-12-03 22:07:34 +05303010 dev_dbg(dev, "%s:failed in connecting, ssr?\n",
3011 __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303012 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05303013 swrm->state = SWR_MSTR_UP;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303014 }
3015exit:
Laxminath Kasamea6cbee2020-04-28 00:02:32 +05303016 if (ret && !swrm->aud_core_err)
Sudheer Papothi384addd2019-06-14 02:26:52 +05303017 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
3018 if (!hw_core_err)
3019 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Vatsal Buchae50b5002019-09-19 14:32:20 +05303020 if (swrm_clk_req_err)
3021 pm_runtime_set_autosuspend_delay(&pdev->dev,
3022 ERR_AUTO_SUSPEND_TIMER_VAL);
3023 else
3024 pm_runtime_set_autosuspend_delay(&pdev->dev,
3025 auto_suspend_timer);
Vatsal Buchabd68a7c2020-06-08 10:41:56 +05303026 if (swrm->req_clk_switch)
3027 swrm->req_clk_switch = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303028 mutex_unlock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05303029
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003030 trace_printk("%s: pm_runtime: resume done, state:%d\n",
3031 __func__, swrm->state);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303032 return ret;
3033}
3034
3035static int swrm_runtime_suspend(struct device *dev)
3036{
3037 struct platform_device *pdev = to_platform_device(dev);
3038 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3039 int ret = 0;
Sudheer Papothi384addd2019-06-14 02:26:52 +05303040 bool hw_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303041 struct swr_master *mstr = &swrm->master;
3042 struct swr_device *swr_dev;
3043 int current_state = 0;
3044
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003045 trace_printk("%s: pm_runtime: suspend state: %d\n",
3046 __func__, swrm->state);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303047 dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
3048 __func__, swrm->state);
3049 mutex_lock(&swrm->reslock);
3050 mutex_lock(&swrm->force_down_lock);
3051 current_state = swrm->state;
3052 mutex_unlock(&swrm->force_down_lock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05303053
3054 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
3055 dev_err(dev, "%s:lpass core hw enable failed\n",
3056 __func__);
3057 hw_core_err = true;
3058 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05303059
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05303060 if ((current_state == SWR_MSTR_UP) ||
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303061 (current_state == SWR_MSTR_SSR)) {
3062
3063 if ((current_state != SWR_MSTR_SSR) &&
3064 swrm_is_port_en(&swrm->master)) {
3065 dev_dbg(dev, "%s ports are enabled\n", __func__);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003066 trace_printk("%s ports are enabled\n", __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303067 ret = -EBUSY;
3068 goto exit;
3069 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05303070 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003071 dev_err(dev, "%s: clk stop mode not supported or SSR entry\n",
3072 __func__);
Sudheer Papothi06f43412019-07-09 03:32:54 +05303073 mutex_unlock(&swrm->reslock);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +05303074 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
Sudheer Papothi06f43412019-07-09 03:32:54 +05303075 mutex_lock(&swrm->reslock);
Sudheer Papothi1f2565b2020-01-30 05:22:47 +05303076 if (!swrm->clk_stop_mode0_supp) {
3077 swrm_clk_pause(swrm);
3078 } else {
3079 /* Mask bus clash interrupt */
3080 swrm->intr_mask &= ~((u32)0x08);
3081 swr_master_write(swrm,
3082 SWRM_INTERRUPT_MASK_ADDR,
3083 swrm->intr_mask);
3084 swr_master_write(swrm,
3085 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
3086 swrm->intr_mask);
3087 mutex_unlock(&swrm->reslock);
3088 /* clock stop sequence */
3089 swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
3090 SWRS_SCP_CONTROL);
3091 mutex_lock(&swrm->reslock);
3092 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05303093 swr_master_write(swrm, SWRM_COMP_CFG_ADDR, 0x00);
3094 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
3095 ret = swr_device_down(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05303096 if (ret == -ENODEV) {
3097 dev_dbg_ratelimited(dev,
3098 "%s slave device down not implemented\n",
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003099 __func__);
3100 trace_printk(
3101 "%s slave device down not implemented\n",
3102 __func__);
Sudheer Papothi79c90752019-04-23 06:09:52 +05303103 ret = 0;
3104 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05303105 dev_err(dev,
3106 "%s: failed to shutdown swr dev %d\n",
3107 __func__, swr_dev->dev_num);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003108 trace_printk(
3109 "%s: failed to shutdown swr dev %d\n",
3110 __func__, swr_dev->dev_num);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05303111 goto exit;
3112 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303113 }
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003114 trace_printk("%s: clk stop mode not supported or SSR exit\n",
3115 __func__);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05303116 } else {
Sudheer Papothi55fa5972019-09-28 02:50:27 +05303117 /* Mask bus clash interrupt */
3118 swrm->intr_mask &= ~((u32)0x08);
3119 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR,
3120 swrm->intr_mask);
3121 swr_master_write(swrm,
3122 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
3123 swrm->intr_mask);
Sudheer Papothi384addd2019-06-14 02:26:52 +05303124 mutex_unlock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05303125 /* clock stop sequence */
3126 swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
3127 SWRS_SCP_CONTROL);
Sudheer Papothi384addd2019-06-14 02:26:52 +05303128 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05303129 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303130 }
Laxminath Kasamcafe0732019-11-20 17:31:58 +05303131 if (!swrm_check_link_status(swrm, 0x0))
Laxminath Kasam696b14b2019-12-03 22:07:34 +05303132 dev_dbg(dev, "%s:failed in disconnecting, ssr?\n",
3133 __func__);
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07003134 ret = swrm_clk_request(swrm, false);
3135 if (ret) {
3136 dev_err(dev, "%s: swrmn clk failed\n", __func__);
3137 ret = 0;
3138 goto exit;
3139 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05303140
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303141 if (swrm->clk_stop_mode0_supp) {
3142 if (swrm->wake_irq > 0) {
Laxminath Kasamea6cbee2020-04-28 00:02:32 +05303143 if (swrm->dmic_sva && swrm->pinctrl_setup)
3144 swrm->pinctrl_setup(swrm->handle, true);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303145 enable_irq(swrm->wake_irq);
3146 } else if (swrm->ipc_wakeup) {
3147 msm_aud_evt_blocking_notifier_call_chain(
3148 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
3149 swrm->ipc_wakeup_triggered = false;
3150 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05303151 }
3152
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303153 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05303154 /* Retain SSR state until resume */
3155 if (current_state != SWR_MSTR_SSR)
3156 swrm->state = SWR_MSTR_DOWN;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303157exit:
Laxminath Kasamea6cbee2020-04-28 00:02:32 +05303158 if (!swrm->aud_core_err)
Sudheer Papothi384addd2019-06-14 02:26:52 +05303159 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
3160 if (!hw_core_err)
3161 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Laxminath Kasamea6cbee2020-04-28 00:02:32 +05303162 swrm->aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303163 mutex_unlock(&swrm->reslock);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003164 trace_printk("%s: pm_runtime: suspend done state: %d\n",
3165 __func__, swrm->state);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303166 return ret;
3167}
3168#endif /* CONFIG_PM */
3169
Sudheer Papothi06f43412019-07-09 03:32:54 +05303170static int swrm_device_suspend(struct device *dev)
3171{
3172 struct platform_device *pdev = to_platform_device(dev);
3173 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3174 int ret = 0;
3175
3176 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003177 trace_printk("%s: swrm state: %d\n", __func__, swrm->state);
Sudheer Papothi06f43412019-07-09 03:32:54 +05303178 if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
3179 ret = swrm_runtime_suspend(dev);
3180 if (!ret) {
3181 pm_runtime_disable(dev);
3182 pm_runtime_set_suspended(dev);
3183 pm_runtime_enable(dev);
3184 }
3185 }
3186
3187 return 0;
3188}
3189
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303190static int swrm_device_down(struct device *dev)
3191{
3192 struct platform_device *pdev = to_platform_device(dev);
3193 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303194
3195 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003196 trace_printk("%s: swrm state: %d\n", __func__, swrm->state);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303197
3198 mutex_lock(&swrm->force_down_lock);
3199 swrm->state = SWR_MSTR_SSR;
3200 mutex_unlock(&swrm->force_down_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303201
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05303202 swrm_device_suspend(dev);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303203 return 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303204}
3205
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303206int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm)
3207{
3208 int ret = 0;
Laxminath Kasama60239e2019-01-10 14:43:03 +05303209 int irq, dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303210
3211 if (!swrm->ipc_wakeup) {
Laxminath Kasama60239e2019-01-10 14:43:03 +05303212 irq = of_get_named_gpio(swrm->dev->of_node,
3213 "qcom,swr-wakeup-irq", 0);
3214 if (gpio_is_valid(irq)) {
3215 swrm->wake_irq = gpio_to_irq(irq);
3216 if (swrm->wake_irq < 0) {
3217 dev_err(swrm->dev,
3218 "Unable to configure irq\n");
3219 return swrm->wake_irq;
3220 }
3221 } else {
3222 dir_apps_irq = platform_get_irq_byname(swrm->pdev,
3223 "swr_wake_irq");
3224 if (dir_apps_irq < 0) {
3225 dev_err(swrm->dev,
3226 "TLMM connect gpio not found\n");
3227 return -EINVAL;
3228 }
3229 swrm->wake_irq = dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303230 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303231 ret = request_threaded_irq(swrm->wake_irq, NULL,
3232 swrm_wakeup_interrupt,
3233 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
3234 "swr_wake_irq", swrm);
3235 if (ret) {
3236 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
3237 __func__, ret);
3238 return -EINVAL;
3239 }
Aditya Bavanari3517b112018-12-03 13:26:59 +05303240 irq_set_irq_wake(swrm->wake_irq, 1);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303241 }
3242 return ret;
3243}
3244
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05303245static int swrm_alloc_port_mem(struct device *dev, struct swr_mstr_ctrl *swrm,
3246 u32 uc, u32 size)
3247{
3248 if (!swrm->port_param) {
3249 swrm->port_param = devm_kzalloc(dev,
3250 sizeof(swrm->port_param) * SWR_UC_MAX,
3251 GFP_KERNEL);
3252 if (!swrm->port_param)
3253 return -ENOMEM;
3254 }
3255 if (!swrm->port_param[uc]) {
3256 swrm->port_param[uc] = devm_kcalloc(dev, size,
3257 sizeof(struct port_params),
3258 GFP_KERNEL);
3259 if (!swrm->port_param[uc])
3260 return -ENOMEM;
3261 } else {
3262 dev_err_ratelimited(swrm->dev, "%s: called more than once\n",
3263 __func__);
3264 }
3265
3266 return 0;
3267}
3268
3269static int swrm_copy_port_config(struct swr_mstr_ctrl *swrm,
3270 struct swrm_port_config *port_cfg,
3271 u32 size)
3272{
3273 int idx;
3274 struct port_params *params;
3275 int uc = port_cfg->uc;
3276 int ret = 0;
3277
3278 for (idx = 0; idx < size; idx++) {
3279 params = &((struct port_params *)port_cfg->params)[idx];
3280 if (!params) {
3281 dev_err(swrm->dev, "%s: Invalid params\n", __func__);
3282 ret = -EINVAL;
3283 break;
3284 }
3285 memcpy(&swrm->port_param[uc][idx], params,
3286 sizeof(struct port_params));
3287 }
3288
3289 return ret;
3290}
3291
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303292/**
3293 * swrm_wcd_notify - parent device can notify to soundwire master through
3294 * this function
3295 * @pdev: pointer to platform device structure
3296 * @id: command id from parent to the soundwire master
3297 * @data: data from parent device to soundwire master
3298 */
3299int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
3300{
3301 struct swr_mstr_ctrl *swrm;
3302 int ret = 0;
3303 struct swr_master *mstr;
3304 struct swr_device *swr_dev;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05303305 struct swrm_port_config *port_cfg;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303306
3307 if (!pdev) {
3308 pr_err("%s: pdev is NULL\n", __func__);
3309 return -EINVAL;
3310 }
3311 swrm = platform_get_drvdata(pdev);
3312 if (!swrm) {
3313 dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__);
3314 return -EINVAL;
3315 }
3316 mstr = &swrm->master;
3317
3318 switch (id) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05303319 case SWR_REQ_CLK_SWITCH:
3320 /* This will put soundwire in clock stop mode and disable the
3321 * clocks, if there is no active usecase running, so that the
3322 * next activity on soundwire will request clock from new clock
3323 * source.
3324 */
Sudheer Papothi8c50f2f2019-12-05 01:14:47 +05303325 if (!data) {
3326 dev_err(swrm->dev, "%s: data is NULL for id:%d\n",
3327 __func__, id);
3328 ret = -EINVAL;
3329 break;
3330 }
Sudheer Papothi06f43412019-07-09 03:32:54 +05303331 mutex_lock(&swrm->mlock);
Sudheer Papothi8c50f2f2019-12-05 01:14:47 +05303332 if (swrm->clk_src != *(int *)data) {
Vatsal Buchabd68a7c2020-06-08 10:41:56 +05303333 if (swrm->state == SWR_MSTR_UP) {
3334 swrm->req_clk_switch = true;
Sudheer Papothi8c50f2f2019-12-05 01:14:47 +05303335 swrm_device_suspend(&pdev->dev);
Vatsal Buchabd68a7c2020-06-08 10:41:56 +05303336 if (swrm->state == SWR_MSTR_UP)
3337 swrm->req_clk_switch = false;
3338 }
Sudheer Papothi8c50f2f2019-12-05 01:14:47 +05303339 swrm->clk_src = *(int *)data;
3340 }
Sudheer Papothi06f43412019-07-09 03:32:54 +05303341 mutex_unlock(&swrm->mlock);
3342 break;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05303343 case SWR_CLK_FREQ:
3344 if (!data) {
3345 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
3346 ret = -EINVAL;
3347 } else {
3348 mutex_lock(&swrm->mlock);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05303349 if (swrm->mclk_freq != *(int *)data) {
3350 dev_dbg(swrm->dev, "%s: freq change: force mstr down\n", __func__);
3351 if (swrm->state == SWR_MSTR_DOWN)
3352 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
3353 __func__, swrm->state);
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +05303354 else {
3355 swrm->mclk_freq = *(int *)data;
3356 swrm->bus_clk = swrm->mclk_freq;
3357 swrm_switch_frame_shape(swrm,
3358 swrm->bus_clk);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05303359 swrm_device_suspend(&pdev->dev);
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +05303360 }
Prasad Kumpatla386df4e2019-10-11 18:23:16 +05303361 /*
3362 * add delay to ensure clk release happen
3363 * if interrupt triggered for clk stop,
3364 * wait for it to exit
3365 */
3366 usleep_range(10000, 10500);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05303367 }
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05303368 swrm->mclk_freq = *(int *)data;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05303369 swrm->bus_clk = swrm->mclk_freq;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05303370 mutex_unlock(&swrm->mlock);
3371 }
3372 break;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303373 case SWR_DEVICE_SSR_DOWN:
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003374 trace_printk("%s: swr device down called\n", __func__);
Prasad Kumpatla71fef462020-01-31 21:38:58 +05303375 mutex_lock(&swrm->mlock);
3376 if (swrm->state == SWR_MSTR_DOWN)
3377 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
3378 __func__, swrm->state);
3379 else
3380 swrm_device_down(&pdev->dev);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303381 mutex_lock(&swrm->devlock);
3382 swrm->dev_up = false;
Aditya Bavanaribb981b72020-03-19 18:30:10 +05303383 swrm->hw_core_clk_en = 0;
3384 swrm->aud_core_clk_en = 0;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303385 mutex_unlock(&swrm->devlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05303386 mutex_lock(&swrm->reslock);
3387 swrm->state = SWR_MSTR_SSR;
3388 mutex_unlock(&swrm->reslock);
Prasad Kumpatla71fef462020-01-31 21:38:58 +05303389 mutex_unlock(&swrm->mlock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303390 break;
3391 case SWR_DEVICE_SSR_UP:
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05303392 /* wait for clk voting to be zero */
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003393 trace_printk("%s: swr device up called\n", __func__);
Ramprasad Katkam7f6462e2018-11-06 11:51:22 +05303394 reinit_completion(&swrm->clk_off_complete);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05303395 if (swrm->clk_ref_count &&
3396 !wait_for_completion_timeout(&swrm->clk_off_complete,
Ramprasad Katkamc87efeb2018-12-12 19:26:19 +05303397 msecs_to_jiffies(500)))
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05303398 dev_err(swrm->dev, "%s: clock voting not zero\n",
3399 __func__);
3400
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303401 mutex_lock(&swrm->devlock);
3402 swrm->dev_up = true;
3403 mutex_unlock(&swrm->devlock);
3404 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303405 case SWR_DEVICE_DOWN:
3406 dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003407 trace_printk("%s: swr master down called\n", __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303408 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05303409 if (swrm->state == SWR_MSTR_DOWN)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303410 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
3411 __func__, swrm->state);
3412 else
3413 swrm_device_down(&pdev->dev);
3414 mutex_unlock(&swrm->mlock);
3415 break;
3416 case SWR_DEVICE_UP:
3417 dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003418 trace_printk("%s: swr master up called\n", __func__);
Ramprasad Katkam0fed92f2018-11-08 14:22:22 +05303419 mutex_lock(&swrm->devlock);
3420 if (!swrm->dev_up) {
3421 dev_dbg(swrm->dev, "SSR not complete yet\n");
3422 mutex_unlock(&swrm->devlock);
3423 return -EBUSY;
3424 }
3425 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303426 mutex_lock(&swrm->mlock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303427 pm_runtime_mark_last_busy(&pdev->dev);
3428 pm_runtime_get_sync(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303429 mutex_lock(&swrm->reslock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303430 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
3431 ret = swr_reset_device(swr_dev);
3432 if (ret) {
3433 dev_err(swrm->dev,
3434 "%s: failed to reset swr device %d\n",
3435 __func__, swr_dev->dev_num);
3436 swrm_clk_request(swrm, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303437 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303438 }
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303439 pm_runtime_mark_last_busy(&pdev->dev);
3440 pm_runtime_put_autosuspend(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303441 mutex_unlock(&swrm->reslock);
3442 mutex_unlock(&swrm->mlock);
3443 break;
3444 case SWR_SET_NUM_RX_CH:
3445 if (!data) {
3446 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
3447 ret = -EINVAL;
3448 } else {
3449 mutex_lock(&swrm->mlock);
3450 swrm->num_rx_chs = *(int *)data;
3451 if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
3452 list_for_each_entry(swr_dev, &mstr->devices,
3453 dev_list) {
3454 ret = swr_set_device_group(swr_dev,
3455 SWR_BROADCAST);
3456 if (ret)
3457 dev_err(swrm->dev,
3458 "%s: set num ch failed\n",
3459 __func__);
3460 }
3461 } else {
3462 list_for_each_entry(swr_dev, &mstr->devices,
3463 dev_list) {
3464 ret = swr_set_device_group(swr_dev,
3465 SWR_GROUP_NONE);
3466 if (ret)
3467 dev_err(swrm->dev,
3468 "%s: set num ch failed\n",
3469 __func__);
3470 }
3471 }
3472 mutex_unlock(&swrm->mlock);
3473 }
3474 break;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303475 case SWR_REGISTER_WAKE_IRQ:
3476 if (!data) {
3477 dev_err(swrm->dev, "%s: reg wake irq data is NULL\n",
3478 __func__);
3479 ret = -EINVAL;
3480 } else {
3481 mutex_lock(&swrm->mlock);
3482 swrm->ipc_wakeup = *(u32 *)data;
3483 ret = swrm_register_wake_irq(swrm);
3484 if (ret)
3485 dev_err(swrm->dev, "%s: register wake_irq failed\n",
3486 __func__);
3487 mutex_unlock(&swrm->mlock);
3488 }
3489 break;
Sudheer Papothi72ee2642019-08-08 05:15:17 +05303490 case SWR_REGISTER_WAKEUP:
3491 msm_aud_evt_blocking_notifier_call_chain(
3492 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
Laxminath Kasamea6cbee2020-04-28 00:02:32 +05303493 swrm->dmic_sva = *(u32 *)data;
Sudheer Papothi72ee2642019-08-08 05:15:17 +05303494 break;
3495 case SWR_DEREGISTER_WAKEUP:
3496 msm_aud_evt_blocking_notifier_call_chain(
3497 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
Laxminath Kasamea6cbee2020-04-28 00:02:32 +05303498 swrm->dmic_sva = 0;
Sudheer Papothi72ee2642019-08-08 05:15:17 +05303499 break;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05303500 case SWR_SET_PORT_MAP:
3501 if (!data) {
3502 dev_err(swrm->dev, "%s: data is NULL for id=%d\n",
3503 __func__, id);
3504 ret = -EINVAL;
3505 } else {
3506 mutex_lock(&swrm->mlock);
3507 port_cfg = (struct swrm_port_config *)data;
3508 if (!port_cfg->size) {
3509 ret = -EINVAL;
3510 goto done;
3511 }
3512 ret = swrm_alloc_port_mem(&pdev->dev, swrm,
3513 port_cfg->uc, port_cfg->size);
3514 if (!ret)
3515 swrm_copy_port_config(swrm, port_cfg,
3516 port_cfg->size);
3517done:
3518 mutex_unlock(&swrm->mlock);
3519 }
3520 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303521 default:
3522 dev_err(swrm->dev, "%s: swr master unknown id %d\n",
3523 __func__, id);
3524 break;
3525 }
3526 return ret;
3527}
3528EXPORT_SYMBOL(swrm_wcd_notify);
3529
Ramprasad Katkam57349872018-11-11 18:34:57 +05303530/*
3531 * swrm_pm_cmpxchg:
3532 * Check old state and exchange with pm new state
3533 * if old state matches with current state
3534 *
3535 * @swrm: pointer to wcd core resource
3536 * @o: pm old state
3537 * @n: pm new state
3538 *
3539 * Returns old state
3540 */
3541static enum swrm_pm_state swrm_pm_cmpxchg(
3542 struct swr_mstr_ctrl *swrm,
3543 enum swrm_pm_state o,
3544 enum swrm_pm_state n)
3545{
3546 enum swrm_pm_state old;
3547
3548 if (!swrm)
3549 return o;
3550
3551 mutex_lock(&swrm->pm_lock);
3552 old = swrm->pm_state;
3553 if (old == o)
3554 swrm->pm_state = n;
3555 mutex_unlock(&swrm->pm_lock);
3556
3557 return old;
3558}
3559
3560static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm)
3561{
3562 enum swrm_pm_state os;
3563
3564 /*
3565 * swrm_{lock/unlock}_sleep will be called by swr irq handler
3566 * and slave wake up requests..
3567 *
3568 * If system didn't resume, we can simply return false so
3569 * IRQ handler can return without handling IRQ.
3570 */
3571 mutex_lock(&swrm->pm_lock);
3572 if (swrm->wlock_holders++ == 0) {
3573 dev_dbg(swrm->dev, "%s: holding wake lock\n", __func__);
3574 pm_qos_update_request(&swrm->pm_qos_req,
3575 msm_cpuidle_get_deep_idle_latency());
3576 pm_stay_awake(swrm->dev);
3577 }
3578 mutex_unlock(&swrm->pm_lock);
3579
3580 if (!wait_event_timeout(swrm->pm_wq,
3581 ((os = swrm_pm_cmpxchg(swrm,
3582 SWRM_PM_SLEEPABLE,
3583 SWRM_PM_AWAKE)) ==
3584 SWRM_PM_SLEEPABLE ||
3585 (os == SWRM_PM_AWAKE)),
3586 msecs_to_jiffies(
3587 SWRM_SYSTEM_RESUME_TIMEOUT_MS))) {
3588 dev_err(swrm->dev, "%s: system didn't resume within %dms, s %d, w %d\n",
3589 __func__, SWRM_SYSTEM_RESUME_TIMEOUT_MS, swrm->pm_state,
3590 swrm->wlock_holders);
3591 swrm_unlock_sleep(swrm);
3592 return false;
3593 }
3594 wake_up_all(&swrm->pm_wq);
3595 return true;
3596}
3597
3598static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm)
3599{
3600 mutex_lock(&swrm->pm_lock);
3601 if (--swrm->wlock_holders == 0) {
3602 dev_dbg(swrm->dev, "%s: releasing wake lock pm_state %d -> %d\n",
3603 __func__, swrm->pm_state, SWRM_PM_SLEEPABLE);
3604 /*
3605 * if swrm_lock_sleep failed, pm_state would be still
3606 * swrm_PM_ASLEEP, don't overwrite
3607 */
3608 if (likely(swrm->pm_state == SWRM_PM_AWAKE))
3609 swrm->pm_state = SWRM_PM_SLEEPABLE;
3610 pm_qos_update_request(&swrm->pm_qos_req,
3611 PM_QOS_DEFAULT_VALUE);
3612 pm_relax(swrm->dev);
3613 }
3614 mutex_unlock(&swrm->pm_lock);
3615 wake_up_all(&swrm->pm_wq);
3616}
3617
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303618#ifdef CONFIG_PM_SLEEP
3619static int swrm_suspend(struct device *dev)
3620{
3621 int ret = -EBUSY;
3622 struct platform_device *pdev = to_platform_device(dev);
3623 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3624
3625 dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
Ramprasad Katkam57349872018-11-11 18:34:57 +05303626
3627 mutex_lock(&swrm->pm_lock);
3628
3629 if (swrm->pm_state == SWRM_PM_SLEEPABLE) {
3630 dev_dbg(swrm->dev, "%s: suspending system, state %d, wlock %d\n",
3631 __func__, swrm->pm_state,
3632 swrm->wlock_holders);
3633 swrm->pm_state = SWRM_PM_ASLEEP;
3634 } else if (swrm->pm_state == SWRM_PM_AWAKE) {
3635 /*
3636 * unlock to wait for pm_state == SWRM_PM_SLEEPABLE
3637 * then set to SWRM_PM_ASLEEP
3638 */
3639 dev_dbg(swrm->dev, "%s: waiting to suspend system, state %d, wlock %d\n",
3640 __func__, swrm->pm_state,
3641 swrm->wlock_holders);
3642 mutex_unlock(&swrm->pm_lock);
3643 if (!(wait_event_timeout(swrm->pm_wq, swrm_pm_cmpxchg(
3644 swrm, SWRM_PM_SLEEPABLE,
3645 SWRM_PM_ASLEEP) ==
3646 SWRM_PM_SLEEPABLE,
3647 msecs_to_jiffies(
3648 SWRM_SYS_SUSPEND_WAIT)))) {
3649 dev_dbg(swrm->dev, "%s: suspend failed state %d, wlock %d\n",
3650 __func__, swrm->pm_state,
3651 swrm->wlock_holders);
3652 return -EBUSY;
3653 } else {
3654 dev_dbg(swrm->dev,
3655 "%s: done, state %d, wlock %d\n",
3656 __func__, swrm->pm_state,
3657 swrm->wlock_holders);
3658 }
3659 mutex_lock(&swrm->pm_lock);
3660 } else if (swrm->pm_state == SWRM_PM_ASLEEP) {
3661 dev_dbg(swrm->dev, "%s: system is already suspended, state %d, wlock %d\n",
3662 __func__, swrm->pm_state,
3663 swrm->wlock_holders);
3664 }
3665
3666 mutex_unlock(&swrm->pm_lock);
3667
3668 if ((!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev))) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303669 ret = swrm_runtime_suspend(dev);
3670 if (!ret) {
3671 /*
3672 * Synchronize runtime-pm and system-pm states:
3673 * At this point, we are already suspended. If
3674 * runtime-pm still thinks its active, then
3675 * make sure its status is in sync with HW
3676 * status. The three below calls let the
3677 * runtime-pm know that we are suspended
3678 * already without re-invoking the suspend
3679 * callback
3680 */
3681 pm_runtime_disable(dev);
3682 pm_runtime_set_suspended(dev);
3683 pm_runtime_enable(dev);
3684 }
3685 }
3686 if (ret == -EBUSY) {
3687 /*
3688 * There is a possibility that some audio stream is active
3689 * during suspend. We dont want to return suspend failure in
3690 * that case so that display and relevant components can still
3691 * go to suspend.
3692 * If there is some other error, then it should be passed-on
3693 * to system level suspend
3694 */
3695 ret = 0;
3696 }
3697 return ret;
3698}
3699
3700static int swrm_resume(struct device *dev)
3701{
3702 int ret = 0;
3703 struct platform_device *pdev = to_platform_device(dev);
3704 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3705
3706 dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
3707 if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
3708 ret = swrm_runtime_resume(dev);
3709 if (!ret) {
3710 pm_runtime_mark_last_busy(dev);
3711 pm_request_autosuspend(dev);
3712 }
3713 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05303714 mutex_lock(&swrm->pm_lock);
3715 if (swrm->pm_state == SWRM_PM_ASLEEP) {
3716 dev_dbg(swrm->dev,
3717 "%s: resuming system, state %d, wlock %d\n",
3718 __func__, swrm->pm_state,
3719 swrm->wlock_holders);
3720 swrm->pm_state = SWRM_PM_SLEEPABLE;
3721 } else {
3722 dev_dbg(swrm->dev, "%s: system is already awake, state %d wlock %d\n",
3723 __func__, swrm->pm_state,
3724 swrm->wlock_holders);
3725 }
3726 mutex_unlock(&swrm->pm_lock);
3727 wake_up_all(&swrm->pm_wq);
3728
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303729 return ret;
3730}
3731#endif /* CONFIG_PM_SLEEP */
3732
3733static const struct dev_pm_ops swrm_dev_pm_ops = {
3734 SET_SYSTEM_SLEEP_PM_OPS(
3735 swrm_suspend,
3736 swrm_resume
3737 )
3738 SET_RUNTIME_PM_OPS(
3739 swrm_runtime_suspend,
3740 swrm_runtime_resume,
3741 NULL
3742 )
3743};
3744
3745static const struct of_device_id swrm_dt_match[] = {
3746 {
3747 .compatible = "qcom,swr-mstr",
3748 },
3749 {}
3750};
3751
3752static struct platform_driver swr_mstr_driver = {
3753 .probe = swrm_probe,
3754 .remove = swrm_remove,
3755 .driver = {
3756 .name = SWR_WCD_NAME,
3757 .owner = THIS_MODULE,
3758 .pm = &swrm_dev_pm_ops,
3759 .of_match_table = swrm_dt_match,
Xiaojun Sang53cd13a2018-06-29 15:14:37 +08003760 .suppress_bind_attrs = true,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303761 },
3762};
3763
3764static int __init swrm_init(void)
3765{
3766 return platform_driver_register(&swr_mstr_driver);
3767}
3768module_init(swrm_init);
3769
3770static void __exit swrm_exit(void)
3771{
3772 platform_driver_unregister(&swr_mstr_driver);
3773}
3774module_exit(swrm_exit);
3775
3776MODULE_LICENSE("GPL v2");
3777MODULE_DESCRIPTION("SoundWire Master Controller");
3778MODULE_ALIAS("platform:swr-mstr");