blob: 998cddcc26b22c72ef389820e77b77991dfc1f39 [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 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302576 if (of_property_read_u32(pdev->dev.of_node,
2577 "qcom,swr-clock-stop-mode0",
2578 &swrm->clk_stop_mode0_supp)) {
2579 swrm->clk_stop_mode0_supp = FALSE;
2580 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302581
2582 ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
2583 &swrm->num_dev);
2584 if (ret) {
2585 dev_dbg(&pdev->dev, "%s: Looking up %s property failed\n",
2586 __func__, "qcom,swr-num-dev");
2587 } else {
Sudheer Papothiae5c3632019-11-27 06:52:06 +05302588 if (swrm->num_dev > SWRM_NUM_AUTO_ENUM_SLAVES) {
Ramprasad Katkam57349872018-11-11 18:34:57 +05302589 dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
Sudheer Papothiae5c3632019-11-27 06:52:06 +05302590 __func__, swrm->num_dev,
2591 SWRM_NUM_AUTO_ENUM_SLAVES);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302592 ret = -EINVAL;
2593 goto err_pdata_fail;
2594 }
2595 }
2596
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302597 /* Parse soundwire port mapping */
2598 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
2599 &num_ports);
2600 if (ret) {
2601 dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
2602 goto err_pdata_fail;
2603 }
2604 swrm->num_ports = num_ports;
2605
2606 if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
2607 &map_size)) {
2608 dev_err(swrm->dev, "missing port mapping\n");
2609 goto err_pdata_fail;
2610 }
2611
2612 map_length = map_size / (3 * sizeof(u32));
2613 if (num_ports > SWR_MSTR_PORT_LEN) {
2614 dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
2615 __func__);
2616 ret = -EINVAL;
2617 goto err_pdata_fail;
2618 }
2619 temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
2620
2621 if (!temp) {
2622 ret = -ENOMEM;
2623 goto err_pdata_fail;
2624 }
2625 ret = of_property_read_u32_array(pdev->dev.of_node,
2626 "qcom,swr-port-mapping", temp, 3 * map_length);
2627 if (ret) {
2628 dev_err(swrm->dev, "%s: Failed to read port mapping\n",
2629 __func__);
2630 goto err_pdata_fail;
2631 }
2632
2633 for (i = 0; i < map_length; i++) {
2634 port_num = temp[3 * i];
2635 port_type = temp[3 * i + 1];
2636 ch_mask = temp[3 * i + 2];
2637
2638 if (port_num != old_port_num)
2639 ch_iter = 0;
2640 swrm->port_mapping[port_num][ch_iter].port_type = port_type;
2641 swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
2642 old_port_num = port_num;
2643 }
2644 devm_kfree(&pdev->dev, temp);
2645
2646 swrm->reg_irq = pdata->reg_irq;
2647 swrm->master.read = swrm_read;
2648 swrm->master.write = swrm_write;
2649 swrm->master.bulk_write = swrm_bulk_write;
2650 swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
2651 swrm->master.connect_port = swrm_connect_port;
2652 swrm->master.disconnect_port = swrm_disconnect_port;
2653 swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
2654 swrm->master.remove_from_group = swrm_remove_from_group;
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302655 swrm->master.device_wakeup_vote = swrm_device_wakeup_vote;
2656 swrm->master.device_wakeup_unvote = swrm_device_wakeup_unvote;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302657 swrm->master.dev.parent = &pdev->dev;
2658 swrm->master.dev.of_node = pdev->dev.of_node;
2659 swrm->master.num_port = 0;
2660 swrm->rcmd_id = 0;
2661 swrm->wcmd_id = 0;
2662 swrm->slave_status = 0;
2663 swrm->num_rx_chs = 0;
2664 swrm->clk_ref_count = 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302665 swrm->swr_irq_wakeup_capable = 0;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302666 swrm->mclk_freq = MCLK_FREQ;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05302667 swrm->bus_clk = MCLK_FREQ;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302668 swrm->dev_up = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302669 swrm->state = SWR_MSTR_UP;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302670 swrm->ipc_wakeup = false;
2671 swrm->ipc_wakeup_triggered = false;
Vatsal Bucha687f9822020-04-01 18:21:41 +05302672 swrm->disable_div2_clk_switch = FALSE;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302673 init_completion(&swrm->reset);
2674 init_completion(&swrm->broadcast);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302675 init_completion(&swrm->clk_off_complete);
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302676 mutex_init(&swrm->irq_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302677 mutex_init(&swrm->mlock);
2678 mutex_init(&swrm->reslock);
2679 mutex_init(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302680 mutex_init(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302681 mutex_init(&swrm->clklock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302682 mutex_init(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302683 mutex_init(&swrm->pm_lock);
2684 swrm->wlock_holders = 0;
2685 swrm->pm_state = SWRM_PM_SLEEPABLE;
2686 init_waitqueue_head(&swrm->pm_wq);
2687 pm_qos_add_request(&swrm->pm_qos_req,
2688 PM_QOS_CPU_DMA_LATENCY,
2689 PM_QOS_DEFAULT_VALUE);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302690
2691 for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++)
2692 INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
2693
Vatsal Bucha687f9822020-04-01 18:21:41 +05302694 if (of_property_read_u32(pdev->dev.of_node,
2695 "qcom,disable-div2-clk-switch",
2696 &swrm->disable_div2_clk_switch)) {
2697 swrm->disable_div2_clk_switch = FALSE;
2698 }
2699
Sudheer Papothi06f43412019-07-09 03:32:54 +05302700 /* Register LPASS core hw vote */
2701 lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
2702 if (IS_ERR(lpass_core_hw_vote)) {
2703 ret = PTR_ERR(lpass_core_hw_vote);
2704 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2705 __func__, "lpass_core_hw_vote", ret);
2706 lpass_core_hw_vote = NULL;
2707 ret = 0;
2708 }
2709 swrm->lpass_core_hw_vote = lpass_core_hw_vote;
2710
2711 /* Register LPASS audio core vote */
2712 lpass_core_audio = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
2713 if (IS_ERR(lpass_core_audio)) {
2714 ret = PTR_ERR(lpass_core_audio);
2715 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2716 __func__, "lpass_core_audio", ret);
2717 lpass_core_audio = NULL;
2718 ret = 0;
2719 }
2720 swrm->lpass_core_audio = lpass_core_audio;
2721
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302722 if (swrm->reg_irq) {
2723 ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
2724 SWR_IRQ_REGISTER);
2725 if (ret) {
2726 dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
2727 __func__, ret);
2728 goto err_irq_fail;
2729 }
2730 } else {
2731 swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
2732 if (swrm->irq < 0) {
2733 dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
2734 __func__, swrm->irq);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302735 goto err_irq_fail;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302736 }
2737
2738 ret = request_threaded_irq(swrm->irq, NULL,
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302739 swr_mstr_interrupt_v2,
Ramprasad Katkam83303512018-10-11 17:34:22 +05302740 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302741 "swr_master_irq", swrm);
2742 if (ret) {
2743 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2744 __func__, ret);
2745 goto err_irq_fail;
2746 }
2747
2748 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302749 /* Make inband tx interrupts as wakeup capable for slave irq */
2750 ret = of_property_read_u32(pdev->dev.of_node,
2751 "qcom,swr-mstr-irq-wakeup-capable",
2752 &swrm->swr_irq_wakeup_capable);
2753 if (ret)
2754 dev_dbg(swrm->dev, "%s: swrm irq wakeup capable not defined\n",
2755 __func__);
2756 if (swrm->swr_irq_wakeup_capable)
2757 irq_set_irq_wake(swrm->irq, 1);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302758 ret = swr_register_master(&swrm->master);
2759 if (ret) {
2760 dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
2761 goto err_mstr_fail;
2762 }
2763
2764 /* Add devices registered with board-info as the
2765 * controller will be up now
2766 */
2767 swr_master_add_boarddevices(&swrm->master);
2768 mutex_lock(&swrm->mlock);
2769 swrm_clk_request(swrm, true);
Laxminath Kasam4696fff2019-11-26 16:07:11 +05302770 swrm->version = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302771 ret = swrm_master_init(swrm);
2772 if (ret < 0) {
2773 dev_err(&pdev->dev,
2774 "%s: Error in master Initialization , err %d\n",
2775 __func__, ret);
2776 mutex_unlock(&swrm->mlock);
Laxminath Kasam36956422020-06-01 19:23:48 +05302777 ret = -EPROBE_DEFER;
Laxminath Kasamec8c9092019-12-17 13:12:58 +05302778 goto err_mstr_init_fail;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302779 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302780
2781 mutex_unlock(&swrm->mlock);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302782 INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302783
2784 if (pdev->dev.of_node)
2785 of_register_swr_devices(&swrm->master);
2786
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302787#ifdef CONFIG_DEBUG_FS
2788 swrm->debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
2789 if (!IS_ERR(swrm->debugfs_swrm_dent)) {
2790 swrm->debugfs_peek = debugfs_create_file("swrm_peek",
2791 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2792 (void *) swrm, &swrm_debug_read_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302793
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302794 swrm->debugfs_poke = debugfs_create_file("swrm_poke",
2795 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2796 (void *) swrm, &swrm_debug_write_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302797
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302798 swrm->debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
2799 S_IFREG | 0444, swrm->debugfs_swrm_dent,
2800 (void *) swrm,
2801 &swrm_debug_dump_ops);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302802 }
Sudheer Papothi96c842a2019-08-29 12:11:21 +05302803#endif
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302804 ret = device_init_wakeup(swrm->dev, true);
2805 if (ret) {
2806 dev_err(swrm->dev, "Device wakeup init failed: %d\n", ret);
2807 goto err_irq_wakeup_fail;
2808 }
2809
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302810 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
2811 pm_runtime_use_autosuspend(&pdev->dev);
2812 pm_runtime_set_active(&pdev->dev);
2813 pm_runtime_enable(&pdev->dev);
2814 pm_runtime_mark_last_busy(&pdev->dev);
2815
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302816 INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn);
2817 swrm->event_notifier.notifier_call = swrm_event_notify;
2818 msm_aud_evt_register_client(&swrm->event_notifier);
2819
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302820 return 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302821err_irq_wakeup_fail:
2822 device_init_wakeup(swrm->dev, false);
Laxminath Kasamec8c9092019-12-17 13:12:58 +05302823err_mstr_init_fail:
2824 swr_unregister_master(&swrm->master);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302825err_mstr_fail:
Laxminath Kasam36956422020-06-01 19:23:48 +05302826 if (swrm->reg_irq) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302827 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2828 swrm, SWR_IRQ_FREE);
Laxminath Kasam36956422020-06-01 19:23:48 +05302829 } else if (swrm->irq) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302830 free_irq(swrm->irq, swrm);
Laxminath Kasam36956422020-06-01 19:23:48 +05302831 irqd_set_trigger_type(
2832 irq_get_irq_data(swrm->irq),
2833 IRQ_TYPE_NONE);
2834 }
2835 if (swrm->swr_irq_wakeup_capable)
2836 irq_set_irq_wake(swrm->irq, 0);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302837err_irq_fail:
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302838 mutex_destroy(&swrm->irq_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302839 mutex_destroy(&swrm->mlock);
2840 mutex_destroy(&swrm->reslock);
2841 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302842 mutex_destroy(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302843 mutex_destroy(&swrm->clklock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302844 mutex_destroy(&swrm->pm_lock);
2845 pm_qos_remove_request(&swrm->pm_qos_req);
2846
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302847err_pdata_fail:
2848err_memory_fail:
2849 return ret;
2850}
2851
2852static int swrm_remove(struct platform_device *pdev)
2853{
2854 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2855
Laxminath Kasam36956422020-06-01 19:23:48 +05302856 if (swrm->reg_irq) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302857 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2858 swrm, SWR_IRQ_FREE);
Laxminath Kasam36956422020-06-01 19:23:48 +05302859 } else if (swrm->irq) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302860 free_irq(swrm->irq, swrm);
Laxminath Kasam36956422020-06-01 19:23:48 +05302861 irqd_set_trigger_type(
2862 irq_get_irq_data(swrm->irq),
2863 IRQ_TYPE_NONE);
2864 } else if (swrm->wake_irq > 0) {
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302865 free_irq(swrm->wake_irq, swrm);
Laxminath Kasam36956422020-06-01 19:23:48 +05302866 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302867 if (swrm->swr_irq_wakeup_capable)
2868 irq_set_irq_wake(swrm->irq, 0);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302869 cancel_work_sync(&swrm->wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302870 pm_runtime_disable(&pdev->dev);
2871 pm_runtime_set_suspended(&pdev->dev);
2872 swr_unregister_master(&swrm->master);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302873 msm_aud_evt_unregister_client(&swrm->event_notifier);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302874 device_init_wakeup(swrm->dev, false);
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302875 mutex_destroy(&swrm->irq_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302876 mutex_destroy(&swrm->mlock);
2877 mutex_destroy(&swrm->reslock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302878 mutex_destroy(&swrm->iolock);
2879 mutex_destroy(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302880 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302881 mutex_destroy(&swrm->pm_lock);
2882 pm_qos_remove_request(&swrm->pm_qos_req);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302883 devm_kfree(&pdev->dev, swrm);
2884 return 0;
2885}
2886
2887static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
2888{
2889 u32 val;
2890
2891 dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
2892 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD);
2893 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2894 val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK;
2895 swr_master_write(swrm, SWRM_MCP_CFG_ADDR, val);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302896
2897 return 0;
2898}
2899
2900#ifdef CONFIG_PM
2901static int swrm_runtime_resume(struct device *dev)
2902{
2903 struct platform_device *pdev = to_platform_device(dev);
2904 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2905 int ret = 0;
Vatsal Buchae50b5002019-09-19 14:32:20 +05302906 bool swrm_clk_req_err = false;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302907 bool hw_core_err = false;
Laxminath Kasam2ffc69e2020-06-16 20:15:26 +05302908 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302909 struct swr_master *mstr = &swrm->master;
2910 struct swr_device *swr_dev;
2911
2912 dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
2913 __func__, swrm->state);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07002914 trace_printk("%s: pm_runtime: resume, state:%d\n",
2915 __func__, swrm->state);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302916 mutex_lock(&swrm->reslock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302917
Sudheer Papothi384addd2019-06-14 02:26:52 +05302918 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2919 dev_err(dev, "%s:lpass core hw enable failed\n",
2920 __func__);
2921 hw_core_err = true;
2922 }
2923 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2924 dev_err(dev, "%s:lpass audio hw enable failed\n",
2925 __func__);
Laxminath Kasam2ffc69e2020-06-16 20:15:26 +05302926 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002927 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302928
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302929 if ((swrm->state == SWR_MSTR_DOWN) ||
2930 (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) {
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302931 if (swrm->clk_stop_mode0_supp) {
Vatsal Bucha8bcaeab2019-10-31 11:45:36 +05302932 if (swrm->wake_irq > 0) {
2933 if (unlikely(!irq_get_irq_data
2934 (swrm->wake_irq))) {
2935 pr_err("%s: irq data is NULL\n",
2936 __func__);
2937 mutex_unlock(&swrm->reslock);
2938 return IRQ_NONE;
2939 }
2940 mutex_lock(&swrm->irq_lock);
2941 if (!irqd_irq_disabled(
2942 irq_get_irq_data(swrm->wake_irq)))
2943 disable_irq_nosync(swrm->wake_irq);
2944 mutex_unlock(&swrm->irq_lock);
2945 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302946 if (swrm->ipc_wakeup)
2947 msm_aud_evt_blocking_notifier_call_chain(
2948 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302949 }
2950
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302951 if (swrm_clk_request(swrm, true)) {
2952 /*
2953 * Set autosuspend timer to 1 for
2954 * master to enter into suspend.
2955 */
Vatsal Buchae50b5002019-09-19 14:32:20 +05302956 swrm_clk_req_err = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302957 goto exit;
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302958 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302959 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302960 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2961 ret = swr_device_up(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302962 if (ret == -ENODEV) {
2963 dev_dbg(dev,
2964 "%s slave device up not implemented\n",
2965 __func__);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07002966 trace_printk(
2967 "%s slave device up not implemented\n",
2968 __func__);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302969 ret = 0;
2970 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302971 dev_err(dev,
2972 "%s: failed to wakeup swr dev %d\n",
2973 __func__, swr_dev->dev_num);
2974 swrm_clk_request(swrm, false);
2975 goto exit;
2976 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302977 }
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05302978 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2979 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2980 swrm_master_init(swrm);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302981 /* wait for hw enumeration to complete */
2982 usleep_range(100, 105);
Laxminath Kasame2291972019-11-08 14:51:59 +05302983 if (!swrm_check_link_status(swrm, 0x1))
Laxminath Kasam696b14b2019-12-03 22:07:34 +05302984 dev_dbg(dev, "%s:failed in connecting, ssr?\n",
2985 __func__);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302986 swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, 0x0,
2987 SWRS_SCP_INT_STATUS_MASK_1);
Karthikeyan Manif6821902019-05-21 17:31:24 -07002988 if (swrm->state == SWR_MSTR_SSR) {
2989 mutex_unlock(&swrm->reslock);
2990 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
2991 mutex_lock(&swrm->reslock);
2992 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302993 } else {
2994 /*wake up from clock stop*/
2995 swr_master_write(swrm, SWRM_MCP_BUS_CTRL_ADDR, 0x2);
Sudheer Papothi55fa5972019-09-28 02:50:27 +05302996 /* clear and enable bus clash interrupt */
2997 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x08);
2998 swrm->intr_mask |= 0x08;
2999 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR,
3000 swrm->intr_mask);
3001 swr_master_write(swrm,
3002 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
3003 swrm->intr_mask);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05303004 usleep_range(100, 105);
Laxminath Kasame2291972019-11-08 14:51:59 +05303005 if (!swrm_check_link_status(swrm, 0x1))
Laxminath Kasam696b14b2019-12-03 22:07:34 +05303006 dev_dbg(dev, "%s:failed in connecting, ssr?\n",
3007 __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303008 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05303009 swrm->state = SWR_MSTR_UP;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303010 }
3011exit:
Laxminath Kasam2ffc69e2020-06-16 20:15:26 +05303012 if (!aud_core_err)
Sudheer Papothi384addd2019-06-14 02:26:52 +05303013 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
3014 if (!hw_core_err)
3015 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Vatsal Buchae50b5002019-09-19 14:32:20 +05303016 if (swrm_clk_req_err)
3017 pm_runtime_set_autosuspend_delay(&pdev->dev,
3018 ERR_AUTO_SUSPEND_TIMER_VAL);
3019 else
3020 pm_runtime_set_autosuspend_delay(&pdev->dev,
3021 auto_suspend_timer);
Vatsal Buchabd68a7c2020-06-08 10:41:56 +05303022 if (swrm->req_clk_switch)
3023 swrm->req_clk_switch = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303024 mutex_unlock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05303025
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003026 trace_printk("%s: pm_runtime: resume done, state:%d\n",
3027 __func__, swrm->state);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303028 return ret;
3029}
3030
3031static int swrm_runtime_suspend(struct device *dev)
3032{
3033 struct platform_device *pdev = to_platform_device(dev);
3034 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3035 int ret = 0;
Sudheer Papothi384addd2019-06-14 02:26:52 +05303036 bool hw_core_err = false;
Laxminath Kasam2ffc69e2020-06-16 20:15:26 +05303037 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303038 struct swr_master *mstr = &swrm->master;
3039 struct swr_device *swr_dev;
3040 int current_state = 0;
3041
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003042 trace_printk("%s: pm_runtime: suspend state: %d\n",
3043 __func__, swrm->state);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303044 dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
3045 __func__, swrm->state);
3046 mutex_lock(&swrm->reslock);
3047 mutex_lock(&swrm->force_down_lock);
3048 current_state = swrm->state;
3049 mutex_unlock(&swrm->force_down_lock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05303050
3051 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
3052 dev_err(dev, "%s:lpass core hw enable failed\n",
3053 __func__);
3054 hw_core_err = true;
3055 }
Laxminath Kasam2ffc69e2020-06-16 20:15:26 +05303056 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
3057 dev_err(dev, "%s:lpass audio hw enable failed\n",
3058 __func__);
3059 aud_core_err = true;
3060 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05303061
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05303062 if ((current_state == SWR_MSTR_UP) ||
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303063 (current_state == SWR_MSTR_SSR)) {
3064
3065 if ((current_state != SWR_MSTR_SSR) &&
3066 swrm_is_port_en(&swrm->master)) {
3067 dev_dbg(dev, "%s ports are enabled\n", __func__);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003068 trace_printk("%s ports are enabled\n", __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303069 ret = -EBUSY;
3070 goto exit;
3071 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05303072 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003073 dev_err(dev, "%s: clk stop mode not supported or SSR entry\n",
3074 __func__);
Sudheer Papothi06f43412019-07-09 03:32:54 +05303075 mutex_unlock(&swrm->reslock);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +05303076 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
Sudheer Papothi06f43412019-07-09 03:32:54 +05303077 mutex_lock(&swrm->reslock);
Sudheer Papothi1f2565b2020-01-30 05:22:47 +05303078 if (!swrm->clk_stop_mode0_supp) {
3079 swrm_clk_pause(swrm);
3080 } else {
3081 /* Mask bus clash interrupt */
3082 swrm->intr_mask &= ~((u32)0x08);
3083 swr_master_write(swrm,
3084 SWRM_INTERRUPT_MASK_ADDR,
3085 swrm->intr_mask);
3086 swr_master_write(swrm,
3087 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
3088 swrm->intr_mask);
3089 mutex_unlock(&swrm->reslock);
3090 /* clock stop sequence */
3091 swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
3092 SWRS_SCP_CONTROL);
3093 mutex_lock(&swrm->reslock);
3094 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05303095 swr_master_write(swrm, SWRM_COMP_CFG_ADDR, 0x00);
3096 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
3097 ret = swr_device_down(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05303098 if (ret == -ENODEV) {
3099 dev_dbg_ratelimited(dev,
3100 "%s slave device down not implemented\n",
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003101 __func__);
3102 trace_printk(
3103 "%s slave device down not implemented\n",
3104 __func__);
Sudheer Papothi79c90752019-04-23 06:09:52 +05303105 ret = 0;
3106 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05303107 dev_err(dev,
3108 "%s: failed to shutdown swr dev %d\n",
3109 __func__, swr_dev->dev_num);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003110 trace_printk(
3111 "%s: failed to shutdown swr dev %d\n",
3112 __func__, swr_dev->dev_num);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05303113 goto exit;
3114 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303115 }
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003116 trace_printk("%s: clk stop mode not supported or SSR exit\n",
3117 __func__);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05303118 } else {
Sudheer Papothi55fa5972019-09-28 02:50:27 +05303119 /* Mask bus clash interrupt */
3120 swrm->intr_mask &= ~((u32)0x08);
3121 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR,
3122 swrm->intr_mask);
3123 swr_master_write(swrm,
3124 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
3125 swrm->intr_mask);
Sudheer Papothi384addd2019-06-14 02:26:52 +05303126 mutex_unlock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05303127 /* clock stop sequence */
3128 swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
3129 SWRS_SCP_CONTROL);
Sudheer Papothi384addd2019-06-14 02:26:52 +05303130 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05303131 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303132 }
Laxminath Kasamcafe0732019-11-20 17:31:58 +05303133 if (!swrm_check_link_status(swrm, 0x0))
Laxminath Kasam696b14b2019-12-03 22:07:34 +05303134 dev_dbg(dev, "%s:failed in disconnecting, ssr?\n",
3135 __func__);
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07003136 ret = swrm_clk_request(swrm, false);
3137 if (ret) {
3138 dev_err(dev, "%s: swrmn clk failed\n", __func__);
3139 ret = 0;
3140 goto exit;
3141 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05303142
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303143 if (swrm->clk_stop_mode0_supp) {
3144 if (swrm->wake_irq > 0) {
3145 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 Kasam2ffc69e2020-06-16 20:15:26 +05303158 if (!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);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303162 mutex_unlock(&swrm->reslock);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003163 trace_printk("%s: pm_runtime: suspend done state: %d\n",
3164 __func__, swrm->state);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303165 return ret;
3166}
3167#endif /* CONFIG_PM */
3168
Sudheer Papothi06f43412019-07-09 03:32:54 +05303169static int swrm_device_suspend(struct device *dev)
3170{
3171 struct platform_device *pdev = to_platform_device(dev);
3172 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3173 int ret = 0;
3174
3175 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003176 trace_printk("%s: swrm state: %d\n", __func__, swrm->state);
Sudheer Papothi06f43412019-07-09 03:32:54 +05303177 if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
3178 ret = swrm_runtime_suspend(dev);
3179 if (!ret) {
3180 pm_runtime_disable(dev);
3181 pm_runtime_set_suspended(dev);
3182 pm_runtime_enable(dev);
3183 }
3184 }
3185
3186 return 0;
3187}
3188
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303189static int swrm_device_down(struct device *dev)
3190{
3191 struct platform_device *pdev = to_platform_device(dev);
3192 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303193
3194 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003195 trace_printk("%s: swrm state: %d\n", __func__, swrm->state);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303196
3197 mutex_lock(&swrm->force_down_lock);
3198 swrm->state = SWR_MSTR_SSR;
3199 mutex_unlock(&swrm->force_down_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303200
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05303201 swrm_device_suspend(dev);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303202 return 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303203}
3204
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303205int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm)
3206{
3207 int ret = 0;
Laxminath Kasama60239e2019-01-10 14:43:03 +05303208 int irq, dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303209
3210 if (!swrm->ipc_wakeup) {
Laxminath Kasama60239e2019-01-10 14:43:03 +05303211 irq = of_get_named_gpio(swrm->dev->of_node,
3212 "qcom,swr-wakeup-irq", 0);
3213 if (gpio_is_valid(irq)) {
3214 swrm->wake_irq = gpio_to_irq(irq);
3215 if (swrm->wake_irq < 0) {
3216 dev_err(swrm->dev,
3217 "Unable to configure irq\n");
3218 return swrm->wake_irq;
3219 }
3220 } else {
3221 dir_apps_irq = platform_get_irq_byname(swrm->pdev,
3222 "swr_wake_irq");
3223 if (dir_apps_irq < 0) {
3224 dev_err(swrm->dev,
3225 "TLMM connect gpio not found\n");
3226 return -EINVAL;
3227 }
3228 swrm->wake_irq = dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303229 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303230 ret = request_threaded_irq(swrm->wake_irq, NULL,
3231 swrm_wakeup_interrupt,
3232 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
3233 "swr_wake_irq", swrm);
3234 if (ret) {
3235 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
3236 __func__, ret);
3237 return -EINVAL;
3238 }
Aditya Bavanari3517b112018-12-03 13:26:59 +05303239 irq_set_irq_wake(swrm->wake_irq, 1);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303240 }
3241 return ret;
3242}
3243
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05303244static int swrm_alloc_port_mem(struct device *dev, struct swr_mstr_ctrl *swrm,
3245 u32 uc, u32 size)
3246{
3247 if (!swrm->port_param) {
3248 swrm->port_param = devm_kzalloc(dev,
3249 sizeof(swrm->port_param) * SWR_UC_MAX,
3250 GFP_KERNEL);
3251 if (!swrm->port_param)
3252 return -ENOMEM;
3253 }
3254 if (!swrm->port_param[uc]) {
3255 swrm->port_param[uc] = devm_kcalloc(dev, size,
3256 sizeof(struct port_params),
3257 GFP_KERNEL);
3258 if (!swrm->port_param[uc])
3259 return -ENOMEM;
3260 } else {
3261 dev_err_ratelimited(swrm->dev, "%s: called more than once\n",
3262 __func__);
3263 }
3264
3265 return 0;
3266}
3267
3268static int swrm_copy_port_config(struct swr_mstr_ctrl *swrm,
3269 struct swrm_port_config *port_cfg,
3270 u32 size)
3271{
3272 int idx;
3273 struct port_params *params;
3274 int uc = port_cfg->uc;
3275 int ret = 0;
3276
3277 for (idx = 0; idx < size; idx++) {
3278 params = &((struct port_params *)port_cfg->params)[idx];
3279 if (!params) {
3280 dev_err(swrm->dev, "%s: Invalid params\n", __func__);
3281 ret = -EINVAL;
3282 break;
3283 }
3284 memcpy(&swrm->port_param[uc][idx], params,
3285 sizeof(struct port_params));
3286 }
3287
3288 return ret;
3289}
3290
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303291/**
3292 * swrm_wcd_notify - parent device can notify to soundwire master through
3293 * this function
3294 * @pdev: pointer to platform device structure
3295 * @id: command id from parent to the soundwire master
3296 * @data: data from parent device to soundwire master
3297 */
3298int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
3299{
3300 struct swr_mstr_ctrl *swrm;
3301 int ret = 0;
3302 struct swr_master *mstr;
3303 struct swr_device *swr_dev;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05303304 struct swrm_port_config *port_cfg;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303305
3306 if (!pdev) {
3307 pr_err("%s: pdev is NULL\n", __func__);
3308 return -EINVAL;
3309 }
3310 swrm = platform_get_drvdata(pdev);
3311 if (!swrm) {
3312 dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__);
3313 return -EINVAL;
3314 }
3315 mstr = &swrm->master;
3316
3317 switch (id) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05303318 case SWR_REQ_CLK_SWITCH:
3319 /* This will put soundwire in clock stop mode and disable the
3320 * clocks, if there is no active usecase running, so that the
3321 * next activity on soundwire will request clock from new clock
3322 * source.
3323 */
Sudheer Papothi8c50f2f2019-12-05 01:14:47 +05303324 if (!data) {
3325 dev_err(swrm->dev, "%s: data is NULL for id:%d\n",
3326 __func__, id);
3327 ret = -EINVAL;
3328 break;
3329 }
Sudheer Papothi06f43412019-07-09 03:32:54 +05303330 mutex_lock(&swrm->mlock);
Sudheer Papothi8c50f2f2019-12-05 01:14:47 +05303331 if (swrm->clk_src != *(int *)data) {
Vatsal Buchabd68a7c2020-06-08 10:41:56 +05303332 if (swrm->state == SWR_MSTR_UP) {
3333 swrm->req_clk_switch = true;
Sudheer Papothi8c50f2f2019-12-05 01:14:47 +05303334 swrm_device_suspend(&pdev->dev);
Vatsal Buchabd68a7c2020-06-08 10:41:56 +05303335 if (swrm->state == SWR_MSTR_UP)
3336 swrm->req_clk_switch = false;
3337 }
Sudheer Papothi8c50f2f2019-12-05 01:14:47 +05303338 swrm->clk_src = *(int *)data;
3339 }
Sudheer Papothi06f43412019-07-09 03:32:54 +05303340 mutex_unlock(&swrm->mlock);
3341 break;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05303342 case SWR_CLK_FREQ:
3343 if (!data) {
3344 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
3345 ret = -EINVAL;
3346 } else {
3347 mutex_lock(&swrm->mlock);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05303348 if (swrm->mclk_freq != *(int *)data) {
3349 dev_dbg(swrm->dev, "%s: freq change: force mstr down\n", __func__);
3350 if (swrm->state == SWR_MSTR_DOWN)
3351 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
3352 __func__, swrm->state);
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +05303353 else {
3354 swrm->mclk_freq = *(int *)data;
3355 swrm->bus_clk = swrm->mclk_freq;
3356 swrm_switch_frame_shape(swrm,
3357 swrm->bus_clk);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05303358 swrm_device_suspend(&pdev->dev);
Sudheer Papothi8a8b12b2019-11-15 23:06:41 +05303359 }
Prasad Kumpatla386df4e2019-10-11 18:23:16 +05303360 /*
3361 * add delay to ensure clk release happen
3362 * if interrupt triggered for clk stop,
3363 * wait for it to exit
3364 */
3365 usleep_range(10000, 10500);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05303366 }
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05303367 swrm->mclk_freq = *(int *)data;
Sudheer Papothiac0ae1c2019-10-17 05:38:40 +05303368 swrm->bus_clk = swrm->mclk_freq;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05303369 mutex_unlock(&swrm->mlock);
3370 }
3371 break;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303372 case SWR_DEVICE_SSR_DOWN:
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003373 trace_printk("%s: swr device down called\n", __func__);
Prasad Kumpatla71fef462020-01-31 21:38:58 +05303374 mutex_lock(&swrm->mlock);
3375 if (swrm->state == SWR_MSTR_DOWN)
3376 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
3377 __func__, swrm->state);
3378 else
3379 swrm_device_down(&pdev->dev);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303380 mutex_lock(&swrm->devlock);
3381 swrm->dev_up = false;
Aditya Bavanaribb981b72020-03-19 18:30:10 +05303382 swrm->hw_core_clk_en = 0;
3383 swrm->aud_core_clk_en = 0;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303384 mutex_unlock(&swrm->devlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05303385 mutex_lock(&swrm->reslock);
3386 swrm->state = SWR_MSTR_SSR;
3387 mutex_unlock(&swrm->reslock);
Prasad Kumpatla71fef462020-01-31 21:38:58 +05303388 mutex_unlock(&swrm->mlock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303389 break;
3390 case SWR_DEVICE_SSR_UP:
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05303391 /* wait for clk voting to be zero */
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003392 trace_printk("%s: swr device up called\n", __func__);
Ramprasad Katkam7f6462e2018-11-06 11:51:22 +05303393 reinit_completion(&swrm->clk_off_complete);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05303394 if (swrm->clk_ref_count &&
3395 !wait_for_completion_timeout(&swrm->clk_off_complete,
Ramprasad Katkamc87efeb2018-12-12 19:26:19 +05303396 msecs_to_jiffies(500)))
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05303397 dev_err(swrm->dev, "%s: clock voting not zero\n",
3398 __func__);
3399
Laxminath Kasam1df09a82018-09-20 18:57:49 +05303400 mutex_lock(&swrm->devlock);
3401 swrm->dev_up = true;
3402 mutex_unlock(&swrm->devlock);
3403 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303404 case SWR_DEVICE_DOWN:
3405 dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003406 trace_printk("%s: swr master down called\n", __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303407 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05303408 if (swrm->state == SWR_MSTR_DOWN)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303409 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
3410 __func__, swrm->state);
3411 else
3412 swrm_device_down(&pdev->dev);
3413 mutex_unlock(&swrm->mlock);
3414 break;
3415 case SWR_DEVICE_UP:
3416 dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
Aditya Bavanarif500a1d2019-09-16 18:27:51 -07003417 trace_printk("%s: swr master up called\n", __func__);
Ramprasad Katkam0fed92f2018-11-08 14:22:22 +05303418 mutex_lock(&swrm->devlock);
3419 if (!swrm->dev_up) {
3420 dev_dbg(swrm->dev, "SSR not complete yet\n");
3421 mutex_unlock(&swrm->devlock);
3422 return -EBUSY;
3423 }
3424 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303425 mutex_lock(&swrm->mlock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303426 pm_runtime_mark_last_busy(&pdev->dev);
3427 pm_runtime_get_sync(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303428 mutex_lock(&swrm->reslock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303429 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
3430 ret = swr_reset_device(swr_dev);
3431 if (ret) {
3432 dev_err(swrm->dev,
3433 "%s: failed to reset swr device %d\n",
3434 __func__, swr_dev->dev_num);
3435 swrm_clk_request(swrm, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303436 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303437 }
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05303438 pm_runtime_mark_last_busy(&pdev->dev);
3439 pm_runtime_put_autosuspend(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303440 mutex_unlock(&swrm->reslock);
3441 mutex_unlock(&swrm->mlock);
3442 break;
3443 case SWR_SET_NUM_RX_CH:
3444 if (!data) {
3445 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
3446 ret = -EINVAL;
3447 } else {
3448 mutex_lock(&swrm->mlock);
3449 swrm->num_rx_chs = *(int *)data;
3450 if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
3451 list_for_each_entry(swr_dev, &mstr->devices,
3452 dev_list) {
3453 ret = swr_set_device_group(swr_dev,
3454 SWR_BROADCAST);
3455 if (ret)
3456 dev_err(swrm->dev,
3457 "%s: set num ch failed\n",
3458 __func__);
3459 }
3460 } else {
3461 list_for_each_entry(swr_dev, &mstr->devices,
3462 dev_list) {
3463 ret = swr_set_device_group(swr_dev,
3464 SWR_GROUP_NONE);
3465 if (ret)
3466 dev_err(swrm->dev,
3467 "%s: set num ch failed\n",
3468 __func__);
3469 }
3470 }
3471 mutex_unlock(&swrm->mlock);
3472 }
3473 break;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05303474 case SWR_REGISTER_WAKE_IRQ:
3475 if (!data) {
3476 dev_err(swrm->dev, "%s: reg wake irq data is NULL\n",
3477 __func__);
3478 ret = -EINVAL;
3479 } else {
3480 mutex_lock(&swrm->mlock);
3481 swrm->ipc_wakeup = *(u32 *)data;
3482 ret = swrm_register_wake_irq(swrm);
3483 if (ret)
3484 dev_err(swrm->dev, "%s: register wake_irq failed\n",
3485 __func__);
3486 mutex_unlock(&swrm->mlock);
3487 }
3488 break;
Sudheer Papothi72ee2642019-08-08 05:15:17 +05303489 case SWR_REGISTER_WAKEUP:
3490 msm_aud_evt_blocking_notifier_call_chain(
3491 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
3492 break;
3493 case SWR_DEREGISTER_WAKEUP:
3494 msm_aud_evt_blocking_notifier_call_chain(
3495 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
3496 break;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05303497 case SWR_SET_PORT_MAP:
3498 if (!data) {
3499 dev_err(swrm->dev, "%s: data is NULL for id=%d\n",
3500 __func__, id);
3501 ret = -EINVAL;
3502 } else {
3503 mutex_lock(&swrm->mlock);
3504 port_cfg = (struct swrm_port_config *)data;
3505 if (!port_cfg->size) {
3506 ret = -EINVAL;
3507 goto done;
3508 }
3509 ret = swrm_alloc_port_mem(&pdev->dev, swrm,
3510 port_cfg->uc, port_cfg->size);
3511 if (!ret)
3512 swrm_copy_port_config(swrm, port_cfg,
3513 port_cfg->size);
3514done:
3515 mutex_unlock(&swrm->mlock);
3516 }
3517 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303518 default:
3519 dev_err(swrm->dev, "%s: swr master unknown id %d\n",
3520 __func__, id);
3521 break;
3522 }
3523 return ret;
3524}
3525EXPORT_SYMBOL(swrm_wcd_notify);
3526
Ramprasad Katkam57349872018-11-11 18:34:57 +05303527/*
3528 * swrm_pm_cmpxchg:
3529 * Check old state and exchange with pm new state
3530 * if old state matches with current state
3531 *
3532 * @swrm: pointer to wcd core resource
3533 * @o: pm old state
3534 * @n: pm new state
3535 *
3536 * Returns old state
3537 */
3538static enum swrm_pm_state swrm_pm_cmpxchg(
3539 struct swr_mstr_ctrl *swrm,
3540 enum swrm_pm_state o,
3541 enum swrm_pm_state n)
3542{
3543 enum swrm_pm_state old;
3544
3545 if (!swrm)
3546 return o;
3547
3548 mutex_lock(&swrm->pm_lock);
3549 old = swrm->pm_state;
3550 if (old == o)
3551 swrm->pm_state = n;
3552 mutex_unlock(&swrm->pm_lock);
3553
3554 return old;
3555}
3556
3557static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm)
3558{
3559 enum swrm_pm_state os;
3560
3561 /*
3562 * swrm_{lock/unlock}_sleep will be called by swr irq handler
3563 * and slave wake up requests..
3564 *
3565 * If system didn't resume, we can simply return false so
3566 * IRQ handler can return without handling IRQ.
3567 */
3568 mutex_lock(&swrm->pm_lock);
3569 if (swrm->wlock_holders++ == 0) {
3570 dev_dbg(swrm->dev, "%s: holding wake lock\n", __func__);
3571 pm_qos_update_request(&swrm->pm_qos_req,
3572 msm_cpuidle_get_deep_idle_latency());
3573 pm_stay_awake(swrm->dev);
3574 }
3575 mutex_unlock(&swrm->pm_lock);
3576
3577 if (!wait_event_timeout(swrm->pm_wq,
3578 ((os = swrm_pm_cmpxchg(swrm,
3579 SWRM_PM_SLEEPABLE,
3580 SWRM_PM_AWAKE)) ==
3581 SWRM_PM_SLEEPABLE ||
3582 (os == SWRM_PM_AWAKE)),
3583 msecs_to_jiffies(
3584 SWRM_SYSTEM_RESUME_TIMEOUT_MS))) {
3585 dev_err(swrm->dev, "%s: system didn't resume within %dms, s %d, w %d\n",
3586 __func__, SWRM_SYSTEM_RESUME_TIMEOUT_MS, swrm->pm_state,
3587 swrm->wlock_holders);
3588 swrm_unlock_sleep(swrm);
3589 return false;
3590 }
3591 wake_up_all(&swrm->pm_wq);
3592 return true;
3593}
3594
3595static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm)
3596{
3597 mutex_lock(&swrm->pm_lock);
3598 if (--swrm->wlock_holders == 0) {
3599 dev_dbg(swrm->dev, "%s: releasing wake lock pm_state %d -> %d\n",
3600 __func__, swrm->pm_state, SWRM_PM_SLEEPABLE);
3601 /*
3602 * if swrm_lock_sleep failed, pm_state would be still
3603 * swrm_PM_ASLEEP, don't overwrite
3604 */
3605 if (likely(swrm->pm_state == SWRM_PM_AWAKE))
3606 swrm->pm_state = SWRM_PM_SLEEPABLE;
3607 pm_qos_update_request(&swrm->pm_qos_req,
3608 PM_QOS_DEFAULT_VALUE);
3609 pm_relax(swrm->dev);
3610 }
3611 mutex_unlock(&swrm->pm_lock);
3612 wake_up_all(&swrm->pm_wq);
3613}
3614
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303615#ifdef CONFIG_PM_SLEEP
3616static int swrm_suspend(struct device *dev)
3617{
3618 int ret = -EBUSY;
3619 struct platform_device *pdev = to_platform_device(dev);
3620 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3621
3622 dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
Ramprasad Katkam57349872018-11-11 18:34:57 +05303623
3624 mutex_lock(&swrm->pm_lock);
3625
3626 if (swrm->pm_state == SWRM_PM_SLEEPABLE) {
3627 dev_dbg(swrm->dev, "%s: suspending system, state %d, wlock %d\n",
3628 __func__, swrm->pm_state,
3629 swrm->wlock_holders);
3630 swrm->pm_state = SWRM_PM_ASLEEP;
3631 } else if (swrm->pm_state == SWRM_PM_AWAKE) {
3632 /*
3633 * unlock to wait for pm_state == SWRM_PM_SLEEPABLE
3634 * then set to SWRM_PM_ASLEEP
3635 */
3636 dev_dbg(swrm->dev, "%s: waiting to suspend system, state %d, wlock %d\n",
3637 __func__, swrm->pm_state,
3638 swrm->wlock_holders);
3639 mutex_unlock(&swrm->pm_lock);
3640 if (!(wait_event_timeout(swrm->pm_wq, swrm_pm_cmpxchg(
3641 swrm, SWRM_PM_SLEEPABLE,
3642 SWRM_PM_ASLEEP) ==
3643 SWRM_PM_SLEEPABLE,
3644 msecs_to_jiffies(
3645 SWRM_SYS_SUSPEND_WAIT)))) {
3646 dev_dbg(swrm->dev, "%s: suspend failed state %d, wlock %d\n",
3647 __func__, swrm->pm_state,
3648 swrm->wlock_holders);
3649 return -EBUSY;
3650 } else {
3651 dev_dbg(swrm->dev,
3652 "%s: done, state %d, wlock %d\n",
3653 __func__, swrm->pm_state,
3654 swrm->wlock_holders);
3655 }
3656 mutex_lock(&swrm->pm_lock);
3657 } else if (swrm->pm_state == SWRM_PM_ASLEEP) {
3658 dev_dbg(swrm->dev, "%s: system is already suspended, state %d, wlock %d\n",
3659 __func__, swrm->pm_state,
3660 swrm->wlock_holders);
3661 }
3662
3663 mutex_unlock(&swrm->pm_lock);
3664
3665 if ((!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev))) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303666 ret = swrm_runtime_suspend(dev);
3667 if (!ret) {
3668 /*
3669 * Synchronize runtime-pm and system-pm states:
3670 * At this point, we are already suspended. If
3671 * runtime-pm still thinks its active, then
3672 * make sure its status is in sync with HW
3673 * status. The three below calls let the
3674 * runtime-pm know that we are suspended
3675 * already without re-invoking the suspend
3676 * callback
3677 */
3678 pm_runtime_disable(dev);
3679 pm_runtime_set_suspended(dev);
3680 pm_runtime_enable(dev);
3681 }
3682 }
3683 if (ret == -EBUSY) {
3684 /*
3685 * There is a possibility that some audio stream is active
3686 * during suspend. We dont want to return suspend failure in
3687 * that case so that display and relevant components can still
3688 * go to suspend.
3689 * If there is some other error, then it should be passed-on
3690 * to system level suspend
3691 */
3692 ret = 0;
3693 }
3694 return ret;
3695}
3696
3697static int swrm_resume(struct device *dev)
3698{
3699 int ret = 0;
3700 struct platform_device *pdev = to_platform_device(dev);
3701 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3702
3703 dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
3704 if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
3705 ret = swrm_runtime_resume(dev);
3706 if (!ret) {
3707 pm_runtime_mark_last_busy(dev);
3708 pm_request_autosuspend(dev);
3709 }
3710 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05303711 mutex_lock(&swrm->pm_lock);
3712 if (swrm->pm_state == SWRM_PM_ASLEEP) {
3713 dev_dbg(swrm->dev,
3714 "%s: resuming system, state %d, wlock %d\n",
3715 __func__, swrm->pm_state,
3716 swrm->wlock_holders);
3717 swrm->pm_state = SWRM_PM_SLEEPABLE;
3718 } else {
3719 dev_dbg(swrm->dev, "%s: system is already awake, state %d wlock %d\n",
3720 __func__, swrm->pm_state,
3721 swrm->wlock_holders);
3722 }
3723 mutex_unlock(&swrm->pm_lock);
3724 wake_up_all(&swrm->pm_wq);
3725
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303726 return ret;
3727}
3728#endif /* CONFIG_PM_SLEEP */
3729
3730static const struct dev_pm_ops swrm_dev_pm_ops = {
3731 SET_SYSTEM_SLEEP_PM_OPS(
3732 swrm_suspend,
3733 swrm_resume
3734 )
3735 SET_RUNTIME_PM_OPS(
3736 swrm_runtime_suspend,
3737 swrm_runtime_resume,
3738 NULL
3739 )
3740};
3741
3742static const struct of_device_id swrm_dt_match[] = {
3743 {
3744 .compatible = "qcom,swr-mstr",
3745 },
3746 {}
3747};
3748
3749static struct platform_driver swr_mstr_driver = {
3750 .probe = swrm_probe,
3751 .remove = swrm_remove,
3752 .driver = {
3753 .name = SWR_WCD_NAME,
3754 .owner = THIS_MODULE,
3755 .pm = &swrm_dev_pm_ops,
3756 .of_match_table = swrm_dt_match,
Xiaojun Sang53cd13a2018-06-29 15:14:37 +08003757 .suppress_bind_attrs = true,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303758 },
3759};
3760
3761static int __init swrm_init(void)
3762{
3763 return platform_driver_register(&swr_mstr_driver);
3764}
3765module_init(swrm_init);
3766
3767static void __exit swrm_exit(void)
3768{
3769 platform_driver_unregister(&swr_mstr_driver);
3770}
3771module_exit(swrm_exit);
3772
3773MODULE_LICENSE("GPL v2");
3774MODULE_DESCRIPTION("SoundWire Master Controller");
3775MODULE_ALIAS("platform:swr-mstr");