blob: f16f1d83774470496c21d53d45f8f9d22505b5e7 [file] [log] [blame]
Meng Wang43bbb872018-12-10 12:32:05 +08001// SPDX-License-Identifier: GPL-2.0-only
Meng Wang61af6842018-09-10 17:47:55 +08002/*
Aditya Bavanari3517b112018-12-03 13:26:59 +05303 * Copyright (c) 2015-2019, The Linux Foundation. All rights reserved.
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05304 */
5
6#include <linux/irq.h>
7#include <linux/kernel.h>
8#include <linux/init.h>
9#include <linux/slab.h>
10#include <linux/io.h>
11#include <linux/interrupt.h>
12#include <linux/platform_device.h>
13#include <linux/delay.h>
14#include <linux/kthread.h>
Ramprasad Katkamcab8d722018-09-28 15:54:06 +053015#include <linux/bitops.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053016#include <linux/clk.h>
Laxminath Kasama60239e2019-01-10 14:43:03 +053017#include <linux/gpio.h>
18#include <linux/of_gpio.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053019#include <linux/pm_runtime.h>
20#include <linux/of.h>
21#include <linux/debugfs.h>
22#include <linux/uaccess.h>
23#include <soc/soundwire.h>
Sudheer Papothi3d1596e2018-10-27 06:19:18 +053024#include <soc/swr-common.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053025#include <linux/regmap.h>
Ramprasad Katkam68765ab2018-08-30 11:46:32 +053026#include <dsp/msm-audio-event-notify.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053027#include "swrm_registers.h"
28#include "swr-mstr-ctrl.h"
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053029
Ramprasad Katkam57349872018-11-11 18:34:57 +053030#define SWRM_SYSTEM_RESUME_TIMEOUT_MS 700
31#define SWRM_SYS_SUSPEND_WAIT 1
Sudheer Papothi3d1596e2018-10-27 06:19:18 +053032
Sudheer Papothi4c322b12018-10-31 06:34:01 +053033#define SWRM_DSD_PARAMS_PORT 4
34
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053035#define SWR_BROADCAST_CMD_ID 0x0F
Sudheer Papothi3590b312019-06-04 23:51:30 +053036#define SWR_AUTO_SUSPEND_DELAY 1 /* delay in sec */
Sudheer Papothi7c067e82018-11-15 06:53:35 +053037#define SWR_DEV_ID_MASK 0xFFFFFFFFFFFF
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053038#define SWR_REG_VAL_PACK(data, dev, id, reg) \
39 ((reg) | ((id) << 16) | ((dev) << 20) | ((data) << 24))
40
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +053041#define SWR_INVALID_PARAM 0xFF
Laxminath Kasam990c70b2018-11-09 23:15:09 +053042#define SWR_HSTOP_MAX_VAL 0xF
43#define SWR_HSTART_MIN_VAL 0x0
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +053044
Ramprasad Katkam83303512018-10-11 17:34:22 +053045#define SWRM_INTERRUPT_STATUS_MASK 0x1FDFD
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053046/* pm runtime auto suspend timer in msecs */
47static int auto_suspend_timer = SWR_AUTO_SUSPEND_DELAY * 1000;
48module_param(auto_suspend_timer, int, 0664);
49MODULE_PARM_DESC(auto_suspend_timer, "timer for auto suspend");
50
51enum {
52 SWR_NOT_PRESENT, /* Device is detached/not present on the bus */
53 SWR_ATTACHED_OK, /* Device is attached */
54 SWR_ALERT, /* Device alters master for any interrupts */
55 SWR_RESERVED, /* Reserved */
56};
57
58enum {
59 MASTER_ID_WSA = 1,
60 MASTER_ID_RX,
61 MASTER_ID_TX
62};
Ramprasad Katkamcab8d722018-09-28 15:54:06 +053063
64enum {
65 ENABLE_PENDING,
66 DISABLE_PENDING
67};
Sudheer Papothi384addd2019-06-14 02:26:52 +053068
69enum {
70 LPASS_HW_CORE,
71 LPASS_AUDIO_CORE,
72};
73
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053074#define TRUE 1
75#define FALSE 0
76
Ramprasad Katkam1f221262018-08-23 15:01:22 +053077#define SWRM_MAX_PORT_REG 120
Ramprasad Katkam83303512018-10-11 17:34:22 +053078#define SWRM_MAX_INIT_REG 11
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053079
80#define SWR_MSTR_MAX_REG_ADDR 0x1740
81#define SWR_MSTR_START_REG_ADDR 0x00
82#define SWR_MSTR_MAX_BUF_LEN 32
83#define BYTES_PER_LINE 12
84#define SWR_MSTR_RD_BUF_LEN 8
85#define SWR_MSTR_WR_BUF_LEN 32
86
Laxminath Kasamfbcaf322018-07-18 00:38:14 +053087#define MAX_FIFO_RD_FAIL_RETRY 3
88
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053089static struct swr_mstr_ctrl *dbgswrm;
90static struct dentry *debugfs_swrm_dent;
91static struct dentry *debugfs_peek;
92static struct dentry *debugfs_poke;
93static struct dentry *debugfs_reg_dump;
94static unsigned int read_data;
95
Ramprasad Katkam57349872018-11-11 18:34:57 +053096static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm);
97static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053098
99static bool swrm_is_msm_variant(int val)
100{
101 return (val == SWRM_VERSION_1_3);
102}
103
104static int swrm_debug_open(struct inode *inode, struct file *file)
105{
106 file->private_data = inode->i_private;
107 return 0;
108}
109
110static int get_parameters(char *buf, u32 *param1, int num_of_par)
111{
112 char *token;
113 int base, cnt;
114
115 token = strsep(&buf, " ");
116 for (cnt = 0; cnt < num_of_par; cnt++) {
117 if (token) {
118 if ((token[1] == 'x') || (token[1] == 'X'))
119 base = 16;
120 else
121 base = 10;
122
123 if (kstrtou32(token, base, &param1[cnt]) != 0)
124 return -EINVAL;
125
126 token = strsep(&buf, " ");
127 } else
128 return -EINVAL;
129 }
130 return 0;
131}
132
133static ssize_t swrm_reg_show(char __user *ubuf, size_t count,
134 loff_t *ppos)
135{
136 int i, reg_val, len;
137 ssize_t total = 0;
138 char tmp_buf[SWR_MSTR_MAX_BUF_LEN];
139
140 if (!ubuf || !ppos)
141 return 0;
142
143 for (i = (((int) *ppos / BYTES_PER_LINE) + SWR_MSTR_START_REG_ADDR);
144 i <= SWR_MSTR_MAX_REG_ADDR; i += 4) {
145 reg_val = dbgswrm->read(dbgswrm->handle, i);
146 len = snprintf(tmp_buf, 25, "0x%.3x: 0x%.2x\n", i, reg_val);
147 if ((total + len) >= count - 1)
148 break;
149 if (copy_to_user((ubuf + total), tmp_buf, len)) {
150 pr_err("%s: fail to copy reg dump\n", __func__);
151 total = -EFAULT;
152 goto copy_err;
153 }
154 *ppos += len;
155 total += len;
156 }
157
158copy_err:
159 return total;
160}
161
162static ssize_t swrm_debug_read(struct file *file, char __user *ubuf,
163 size_t count, loff_t *ppos)
164{
165 char lbuf[SWR_MSTR_RD_BUF_LEN];
166 char *access_str;
167 ssize_t ret_cnt;
168
169 if (!count || !file || !ppos || !ubuf)
170 return -EINVAL;
171
172 access_str = file->private_data;
173 if (*ppos < 0)
174 return -EINVAL;
175
176 if (!strcmp(access_str, "swrm_peek")) {
177 snprintf(lbuf, sizeof(lbuf), "0x%x\n", read_data);
178 ret_cnt = simple_read_from_buffer(ubuf, count, ppos, lbuf,
179 strnlen(lbuf, 7));
180 } else if (!strcmp(access_str, "swrm_reg_dump")) {
181 ret_cnt = swrm_reg_show(ubuf, count, ppos);
182 } else {
183 pr_err("%s: %s not permitted to read\n", __func__, access_str);
184 ret_cnt = -EPERM;
185 }
186 return ret_cnt;
187}
188
189static ssize_t swrm_debug_write(struct file *filp,
190 const char __user *ubuf, size_t cnt, loff_t *ppos)
191{
192 char lbuf[SWR_MSTR_WR_BUF_LEN];
193 int rc;
194 u32 param[5];
195 char *access_str;
196
197 if (!filp || !ppos || !ubuf)
198 return -EINVAL;
199
200 access_str = filp->private_data;
201 if (cnt > sizeof(lbuf) - 1)
202 return -EINVAL;
203
204 rc = copy_from_user(lbuf, ubuf, cnt);
205 if (rc)
206 return -EFAULT;
207
208 lbuf[cnt] = '\0';
209 if (!strcmp(access_str, "swrm_poke")) {
210 /* write */
211 rc = get_parameters(lbuf, param, 2);
212 if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) &&
213 (param[1] <= 0xFFFFFFFF) &&
214 (rc == 0))
215 rc = dbgswrm->write(dbgswrm->handle, param[0],
216 param[1]);
217 else
218 rc = -EINVAL;
219 } else if (!strcmp(access_str, "swrm_peek")) {
220 /* read */
221 rc = get_parameters(lbuf, param, 1);
222 if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) && (rc == 0))
223 read_data = dbgswrm->read(dbgswrm->handle, param[0]);
224 else
225 rc = -EINVAL;
226 }
227 if (rc == 0)
228 rc = cnt;
229 else
230 pr_err("%s: rc = %d\n", __func__, rc);
231
232 return rc;
233}
234
235static const struct file_operations swrm_debug_ops = {
236 .open = swrm_debug_open,
237 .write = swrm_debug_write,
238 .read = swrm_debug_read,
239};
240
Sudheer Papothi0016db12019-06-11 04:42:38 +0530241static void swrm_reg_dump(struct swr_mstr_ctrl *swrm,
242 u32 *reg, u32 *val, int len, const char* func)
243{
244 int i = 0;
245
246 for (i = 0; i < len; i++)
247 dev_dbg(swrm->dev, "%s: reg = 0x%x val = 0x%x\n",
248 func, reg[i], val[i]);
249}
250
Sudheer Papothi384addd2019-06-14 02:26:52 +0530251static int swrm_request_hw_vote(struct swr_mstr_ctrl *swrm,
252 int core_type, bool enable)
253{
254 int ret = 0;
255
256 if (core_type == LPASS_HW_CORE) {
257 if (swrm->lpass_core_hw_vote) {
258 if (enable) {
259 ret =
260 clk_prepare_enable(swrm->lpass_core_hw_vote);
261 if (ret < 0)
262 dev_err(swrm->dev,
263 "%s:lpass core hw enable failed\n",
264 __func__);
265 } else
266 clk_disable_unprepare(swrm->lpass_core_hw_vote);
267 }
268 }
269 if (core_type == LPASS_AUDIO_CORE) {
270 if (swrm->lpass_core_audio) {
271 if (enable) {
272 ret =
273 clk_prepare_enable(swrm->lpass_core_audio);
274 if (ret < 0)
275 dev_err(swrm->dev,
276 "%s:lpass audio hw enable failed\n",
277 __func__);
278 } else
279 clk_disable_unprepare(swrm->lpass_core_audio);
280 }
281 }
282
283 return ret;
284}
285
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530286static int swrm_clk_request(struct swr_mstr_ctrl *swrm, bool enable)
287{
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530288 int ret = 0;
289
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530290 if (!swrm->clk || !swrm->handle)
291 return -EINVAL;
292
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530293 mutex_lock(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530294 if (enable) {
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530295 if (!swrm->dev_up) {
296 ret = -ENODEV;
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530297 goto exit;
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530298 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530299 swrm->clk_ref_count++;
300 if (swrm->clk_ref_count == 1) {
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530301 ret = swrm->clk(swrm->handle, true);
302 if (ret) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +0530303 dev_err_ratelimited(swrm->dev,
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530304 "%s: clock enable req failed",
305 __func__);
306 --swrm->clk_ref_count;
307 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530308 }
309 } else if (--swrm->clk_ref_count == 0) {
310 swrm->clk(swrm->handle, false);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530311 complete(&swrm->clk_off_complete);
312 }
313 if (swrm->clk_ref_count < 0) {
Meng Wang8c60bb52019-06-19 15:49:06 +0800314 dev_err(swrm->dev, "%s: swrm clk count mismatch\n", __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530315 swrm->clk_ref_count = 0;
316 }
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530317
318exit:
319 mutex_unlock(&swrm->clklock);
320 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530321}
322
323static int swrm_ahb_write(struct swr_mstr_ctrl *swrm,
324 u16 reg, u32 *value)
325{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530326 u32 temp = (u32)(*value);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530327 int ret = 0;
328
329 mutex_lock(&swrm->devlock);
330 if (!swrm->dev_up)
331 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530332
333 ret = swrm_clk_request(swrm, TRUE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530334 if (ret) {
335 dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
336 __func__);
337 goto err;
338 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530339 iowrite32(temp, swrm->swrm_dig_base + reg);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530340 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530341err:
342 mutex_unlock(&swrm->devlock);
343 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530344}
345
346static int swrm_ahb_read(struct swr_mstr_ctrl *swrm,
347 u16 reg, u32 *value)
348{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530349 u32 temp = 0;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530350 int ret = 0;
351
352 mutex_lock(&swrm->devlock);
353 if (!swrm->dev_up)
354 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530355
356 ret = swrm_clk_request(swrm, TRUE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530357 if (ret) {
358 dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
359 __func__);
360 goto err;
361 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530362 temp = ioread32(swrm->swrm_dig_base + reg);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530363 *value = temp;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530364 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530365err:
366 mutex_unlock(&swrm->devlock);
367 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530368}
369
370static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr)
371{
372 u32 val = 0;
373
374 if (swrm->read)
375 val = swrm->read(swrm->handle, reg_addr);
376 else
377 swrm_ahb_read(swrm, reg_addr, &val);
378 return val;
379}
380
381static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val)
382{
383 if (swrm->write)
384 swrm->write(swrm->handle, reg_addr, val);
385 else
386 swrm_ahb_write(swrm, reg_addr, &val);
387}
388
389static int swr_master_bulk_write(struct swr_mstr_ctrl *swrm, u32 *reg_addr,
390 u32 *val, unsigned int length)
391{
392 int i = 0;
393
394 if (swrm->bulk_write)
395 swrm->bulk_write(swrm->handle, reg_addr, val, length);
396 else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530397 mutex_lock(&swrm->iolock);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530398 for (i = 0; i < length; i++) {
399 /* wait for FIFO WR command to complete to avoid overflow */
Karthikeyan Mani5d52dd82019-08-15 16:58:08 -0700400 /*
401 * Reduce sleep from 100us to 10us to meet KPIs
402 * This still meets the hardware spec
403 */
404 usleep_range(10, 12);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530405 swr_master_write(swrm, reg_addr[i], val[i]);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530406 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530407 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530408 }
409 return 0;
410}
411
412static bool swrm_is_port_en(struct swr_master *mstr)
413{
414 return !!(mstr->num_port);
415}
416
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530417static void copy_port_tables(struct swr_mstr_ctrl *swrm,
418 struct port_params *params)
419{
420 u8 i;
421 struct port_params *config = params;
422
423 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
424 /* wsa uses single frame structure for all configurations */
425 if (!swrm->mport_cfg[i].port_en)
426 continue;
427 swrm->mport_cfg[i].sinterval = config[i].si;
428 swrm->mport_cfg[i].offset1 = config[i].off1;
429 swrm->mport_cfg[i].offset2 = config[i].off2;
430 swrm->mport_cfg[i].hstart = config[i].hstart;
431 swrm->mport_cfg[i].hstop = config[i].hstop;
432 swrm->mport_cfg[i].blk_pack_mode = config[i].bp_mode;
433 swrm->mport_cfg[i].blk_grp_count = config[i].bgp_ctrl;
434 swrm->mport_cfg[i].word_length = config[i].wd_len;
435 swrm->mport_cfg[i].lane_ctrl = config[i].lane_ctrl;
436 }
437}
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530438static int swrm_get_port_config(struct swr_mstr_ctrl *swrm)
439{
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530440 struct port_params *params;
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530441 u32 usecase = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530442
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530443 /* TODO - Send usecase information to avoid checking for master_id */
444 if (swrm->mport_cfg[SWRM_DSD_PARAMS_PORT].port_en &&
445 (swrm->master_id == MASTER_ID_RX))
446 usecase = 1;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530447
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530448 params = swrm->port_param[usecase];
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530449 copy_port_tables(swrm, params);
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530450
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530451 return 0;
452}
453
454static int swrm_get_master_port(struct swr_mstr_ctrl *swrm, u8 *mstr_port_id,
455 u8 *mstr_ch_mask, u8 mstr_prt_type,
456 u8 slv_port_id)
457{
458 int i, j;
459 *mstr_port_id = 0;
460
461 for (i = 1; i <= swrm->num_ports; i++) {
462 for (j = 0; j < SWR_MAX_CH_PER_PORT; j++) {
463 if (swrm->port_mapping[i][j].port_type == mstr_prt_type)
464 goto found;
465 }
466 }
467found:
468 if (i > swrm->num_ports || j == SWR_MAX_CH_PER_PORT) {
469 dev_err(swrm->dev, "%s: port type not supported by master\n",
470 __func__);
471 return -EINVAL;
472 }
473 /* id 0 corresponds to master port 1 */
474 *mstr_port_id = i - 1;
475 *mstr_ch_mask = swrm->port_mapping[i][j].ch_mask;
476
477 return 0;
478
479}
480
481static u32 swrm_get_packed_reg_val(u8 *cmd_id, u8 cmd_data,
482 u8 dev_addr, u16 reg_addr)
483{
484 u32 val;
485 u8 id = *cmd_id;
486
487 if (id != SWR_BROADCAST_CMD_ID) {
488 if (id < 14)
489 id += 1;
490 else
491 id = 0;
492 *cmd_id = id;
493 }
494 val = SWR_REG_VAL_PACK(cmd_data, dev_addr, id, reg_addr);
495
496 return val;
497}
498
499static int swrm_cmd_fifo_rd_cmd(struct swr_mstr_ctrl *swrm, int *cmd_data,
500 u8 dev_addr, u8 cmd_id, u16 reg_addr,
501 u32 len)
502{
503 u32 val;
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530504 u32 retry_attempt = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530505
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530506 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530507 val = swrm_get_packed_reg_val(&swrm->rcmd_id, len, dev_addr, reg_addr);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530508 if (swrm->read) {
509 /* skip delay if read is handled in platform driver */
510 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
511 } else {
512 /* wait for FIFO RD to complete to avoid overflow */
513 usleep_range(100, 105);
514 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
515 /* wait for FIFO RD CMD complete to avoid overflow */
516 usleep_range(250, 255);
517 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530518retry_read:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530519 *cmd_data = swr_master_read(swrm, SWRM_CMD_FIFO_RD_FIFO_ADDR);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530520 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, rcmd_id: 0x%x, \
521 dev_num: 0x%x, cmd_data: 0x%x\n", __func__, reg_addr,
522 cmd_id, swrm->rcmd_id, dev_addr, *cmd_data);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530523 if ((((*cmd_data) & 0xF00) >> 8) != swrm->rcmd_id) {
524 if (retry_attempt < MAX_FIFO_RD_FAIL_RETRY) {
525 /* wait 500 us before retry on fifo read failure */
526 usleep_range(500, 505);
527 retry_attempt++;
528 goto retry_read;
529 } else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530530 dev_err_ratelimited(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, \
531 rcmd_id: 0x%x, dev_num: 0x%x, cmd_data: 0x%x\n",
532 __func__, reg_addr, cmd_id, swrm->rcmd_id,
533 dev_addr, *cmd_data);
534
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530535 dev_err_ratelimited(swrm->dev,
536 "%s: failed to read fifo\n", __func__);
537 }
538 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530539 mutex_unlock(&swrm->iolock);
540
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530541 return 0;
542}
543
544static int swrm_cmd_fifo_wr_cmd(struct swr_mstr_ctrl *swrm, u8 cmd_data,
545 u8 dev_addr, u8 cmd_id, u16 reg_addr)
546{
547 u32 val;
548 int ret = 0;
549
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530550 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530551 if (!cmd_id)
552 val = swrm_get_packed_reg_val(&swrm->wcmd_id, cmd_data,
553 dev_addr, reg_addr);
554 else
555 val = swrm_get_packed_reg_val(&cmd_id, cmd_data,
556 dev_addr, reg_addr);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530557 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x,wcmd_id: 0x%x, \
558 dev_num: 0x%x, cmd_data: 0x%x\n", __func__,
559 reg_addr, cmd_id, swrm->wcmd_id,dev_addr, cmd_data);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +0530560 swr_master_write(swrm, SWRM_CMD_FIFO_WR_CMD, val);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530561 /*
562 * wait for FIFO WR command to complete to avoid overflow
563 * skip delay if write is handled in platform driver.
564 */
565 if(!swrm->write)
Karthikeyan Mani5d52dd82019-08-15 16:58:08 -0700566 usleep_range(150, 155);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530567 if (cmd_id == 0xF) {
568 /*
569 * sleep for 10ms for MSM soundwire variant to allow broadcast
570 * command to complete.
571 */
572 if (swrm_is_msm_variant(swrm->version))
573 usleep_range(10000, 10100);
574 else
575 wait_for_completion_timeout(&swrm->broadcast,
576 (2 * HZ/10));
577 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530578 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530579 return ret;
580}
581
582static int swrm_read(struct swr_master *master, u8 dev_num, u16 reg_addr,
583 void *buf, u32 len)
584{
585 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
586 int ret = 0;
587 int val;
588 u8 *reg_val = (u8 *)buf;
589
590 if (!swrm) {
591 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
592 return -EINVAL;
593 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530594 if (!dev_num) {
595 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
596 return -EINVAL;
597 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530598 mutex_lock(&swrm->devlock);
599 if (!swrm->dev_up) {
600 mutex_unlock(&swrm->devlock);
601 return 0;
602 }
603 mutex_unlock(&swrm->devlock);
604
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530605 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530606 ret = swrm_cmd_fifo_rd_cmd(swrm, &val, dev_num, 0, reg_addr, len);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530607
608 if (!ret)
609 *reg_val = (u8)val;
610
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530611 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530612 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530613 return ret;
614}
615
616static int swrm_write(struct swr_master *master, u8 dev_num, u16 reg_addr,
617 const void *buf)
618{
619 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
620 int ret = 0;
621 u8 reg_val = *(u8 *)buf;
622
623 if (!swrm) {
624 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
625 return -EINVAL;
626 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530627 if (!dev_num) {
628 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
629 return -EINVAL;
630 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530631 mutex_lock(&swrm->devlock);
632 if (!swrm->dev_up) {
633 mutex_unlock(&swrm->devlock);
634 return 0;
635 }
636 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530637
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530638 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530639 ret = swrm_cmd_fifo_wr_cmd(swrm, reg_val, dev_num, 0, reg_addr);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530640
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530641 pm_runtime_put_autosuspend(swrm->dev);
642 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530643 return ret;
644}
645
646static int swrm_bulk_write(struct swr_master *master, u8 dev_num, void *reg,
647 const void *buf, size_t len)
648{
649 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
650 int ret = 0;
651 int i;
652 u32 *val;
653 u32 *swr_fifo_reg;
654
655 if (!swrm || !swrm->handle) {
656 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
657 return -EINVAL;
658 }
659 if (len <= 0)
660 return -EINVAL;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530661 mutex_lock(&swrm->devlock);
662 if (!swrm->dev_up) {
663 mutex_unlock(&swrm->devlock);
664 return 0;
665 }
666 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530667
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530668 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530669 if (dev_num) {
670 swr_fifo_reg = kcalloc(len, sizeof(u32), GFP_KERNEL);
671 if (!swr_fifo_reg) {
672 ret = -ENOMEM;
673 goto err;
674 }
675 val = kcalloc(len, sizeof(u32), GFP_KERNEL);
676 if (!val) {
677 ret = -ENOMEM;
678 goto mem_fail;
679 }
680
681 for (i = 0; i < len; i++) {
682 val[i] = swrm_get_packed_reg_val(&swrm->wcmd_id,
683 ((u8 *)buf)[i],
684 dev_num,
685 ((u16 *)reg)[i]);
686 swr_fifo_reg[i] = SWRM_CMD_FIFO_WR_CMD;
687 }
688 ret = swr_master_bulk_write(swrm, swr_fifo_reg, val, len);
689 if (ret) {
690 dev_err(&master->dev, "%s: bulk write failed\n",
691 __func__);
692 ret = -EINVAL;
693 }
694 } else {
695 dev_err(&master->dev,
696 "%s: No support of Bulk write for master regs\n",
697 __func__);
698 ret = -EINVAL;
699 goto err;
700 }
701 kfree(val);
702mem_fail:
703 kfree(swr_fifo_reg);
704err:
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530705 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530706 pm_runtime_mark_last_busy(swrm->dev);
707 return ret;
708}
709
710static u8 get_inactive_bank_num(struct swr_mstr_ctrl *swrm)
711{
712 return (swr_master_read(swrm, SWRM_MCP_STATUS) &
713 SWRM_MCP_STATUS_BANK_NUM_MASK) ? 0 : 1;
714}
715
716static void enable_bank_switch(struct swr_mstr_ctrl *swrm, u8 bank,
717 u8 row, u8 col)
718{
719 swrm_cmd_fifo_wr_cmd(swrm, ((row << 3) | col), 0xF, 0xF,
720 SWRS_SCP_FRAME_CTRL_BANK(bank));
721}
722
723static struct swr_port_info *swrm_get_port_req(struct swrm_mports *mport,
724 u8 slv_port, u8 dev_num)
725{
726 struct swr_port_info *port_req = NULL;
727
728 list_for_each_entry(port_req, &mport->port_req_list, list) {
729 /* Store dev_id instead of dev_num if enumeration is changed run_time */
730 if ((port_req->slave_port_id == slv_port)
731 && (port_req->dev_num == dev_num))
732 return port_req;
733 }
734 return NULL;
735}
736
737static bool swrm_remove_from_group(struct swr_master *master)
738{
739 struct swr_device *swr_dev;
740 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
741 bool is_removed = false;
742
743 if (!swrm)
744 goto end;
745
746 mutex_lock(&swrm->mlock);
747 if ((swrm->num_rx_chs > 1) &&
748 (swrm->num_rx_chs == swrm->num_cfg_devs)) {
749 list_for_each_entry(swr_dev, &master->devices,
750 dev_list) {
751 swr_dev->group_id = SWR_GROUP_NONE;
752 master->gr_sid = 0;
753 }
754 is_removed = true;
755 }
756 mutex_unlock(&swrm->mlock);
757
758end:
759 return is_removed;
760}
761
762static void swrm_disable_ports(struct swr_master *master,
763 u8 bank)
764{
765 u32 value;
766 struct swr_port_info *port_req;
767 int i;
768 struct swrm_mports *mport;
769 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
770
771 if (!swrm) {
772 pr_err("%s: swrm is null\n", __func__);
773 return;
774 }
775
776 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
777 master->num_port);
778
779
780 for (i = 0; i < SWR_MSTR_PORT_LEN ; i++) {
781
782 mport = &(swrm->mport_cfg[i]);
783 if (!mport->port_en)
784 continue;
785
786 list_for_each_entry(port_req, &mport->port_req_list, list) {
787 /* skip ports with no change req's*/
788 if (port_req->req_ch == port_req->ch_en)
789 continue;
790
791 swrm_cmd_fifo_wr_cmd(swrm, port_req->req_ch,
792 port_req->dev_num, 0x00,
793 SWRS_DP_CHANNEL_ENABLE_BANK(port_req->slave_port_id,
794 bank));
795 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x\n",
796 __func__, i,
797 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)));
798 }
799 value = ((mport->req_ch)
800 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
801 value |= ((mport->offset2)
802 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
803 value |= ((mport->offset1)
804 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
805 value |= mport->sinterval;
806
807 swr_master_write(swrm,
808 SWRM_DP_PORT_CTRL_BANK(i+1, bank),
809 value);
810 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
811 __func__, i,
812 (SWRM_DP_PORT_CTRL_BANK(i+1, bank)), value);
813 }
814}
815
816static void swrm_cleanup_disabled_port_reqs(struct swr_master *master)
817{
818 struct swr_port_info *port_req, *next;
819 int i;
820 struct swrm_mports *mport;
821 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
822
823 if (!swrm) {
824 pr_err("%s: swrm is null\n", __func__);
825 return;
826 }
827 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
828 master->num_port);
829
830 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
831 mport = &(swrm->mport_cfg[i]);
832 list_for_each_entry_safe(port_req, next,
833 &mport->port_req_list, list) {
834 /* skip ports without new ch req */
835 if (port_req->ch_en == port_req->req_ch)
836 continue;
837
838 /* remove new ch req's*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +0530839 port_req->ch_en = port_req->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530840
841 /* If no streams enabled on port, remove the port req */
842 if (port_req->ch_en == 0) {
843 list_del(&port_req->list);
844 kfree(port_req);
845 }
846 }
847 /* remove new ch req's on mport*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +0530848 mport->ch_en = mport->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530849
850 if (!(mport->ch_en)) {
851 mport->port_en = false;
852 master->port_en_mask &= ~i;
853 }
854 }
855}
856static void swrm_copy_data_port_config(struct swr_master *master, u8 bank)
857{
858 u32 value, slv_id;
859 struct swr_port_info *port_req;
860 int i;
861 struct swrm_mports *mport;
862 u32 reg[SWRM_MAX_PORT_REG];
863 u32 val[SWRM_MAX_PORT_REG];
864 int len = 0;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530865 u8 hparams;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530866 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
867
868 if (!swrm) {
869 pr_err("%s: swrm is null\n", __func__);
870 return;
871 }
872
873 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
874 master->num_port);
875
876 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
877 mport = &(swrm->mport_cfg[i]);
878 if (!mport->port_en)
879 continue;
880
881 list_for_each_entry(port_req, &mport->port_req_list, list) {
882 slv_id = port_req->slave_port_id;
883 reg[len] = SWRM_CMD_FIFO_WR_CMD;
884 val[len++] = SWR_REG_VAL_PACK(port_req->req_ch,
885 port_req->dev_num, 0x00,
886 SWRS_DP_CHANNEL_ENABLE_BANK(slv_id,
887 bank));
888
889 reg[len] = SWRM_CMD_FIFO_WR_CMD;
890 val[len++] = SWR_REG_VAL_PACK(mport->sinterval,
891 port_req->dev_num, 0x00,
892 SWRS_DP_SAMPLE_CONTROL_1_BANK(slv_id,
893 bank));
894
895 reg[len] = SWRM_CMD_FIFO_WR_CMD;
896 val[len++] = SWR_REG_VAL_PACK(mport->offset1,
897 port_req->dev_num, 0x00,
898 SWRS_DP_OFFSET_CONTROL_1_BANK(slv_id,
899 bank));
900
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530901 if (mport->offset2 != SWR_INVALID_PARAM) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530902 reg[len] = SWRM_CMD_FIFO_WR_CMD;
903 val[len++] = SWR_REG_VAL_PACK(mport->offset2,
904 port_req->dev_num, 0x00,
905 SWRS_DP_OFFSET_CONTROL_2_BANK(
906 slv_id, bank));
907 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530908 if (mport->hstart != SWR_INVALID_PARAM
909 && mport->hstop != SWR_INVALID_PARAM) {
910 hparams = (mport->hstart << 4) | mport->hstop;
911
912 reg[len] = SWRM_CMD_FIFO_WR_CMD;
913 val[len++] = SWR_REG_VAL_PACK(hparams,
914 port_req->dev_num, 0x00,
915 SWRS_DP_HCONTROL_BANK(slv_id,
916 bank));
917 }
918 if (mport->word_length != SWR_INVALID_PARAM) {
919 reg[len] = SWRM_CMD_FIFO_WR_CMD;
920 val[len++] =
921 SWR_REG_VAL_PACK(mport->word_length,
922 port_req->dev_num, 0x00,
923 SWRS_DP_BLOCK_CONTROL_1(slv_id));
924 }
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +0530925 if (mport->blk_pack_mode != SWR_INVALID_PARAM
926 && swrm->master_id != MASTER_ID_WSA) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530927 reg[len] = SWRM_CMD_FIFO_WR_CMD;
928 val[len++] =
929 SWR_REG_VAL_PACK(mport->blk_pack_mode,
930 port_req->dev_num, 0x00,
931 SWRS_DP_BLOCK_CONTROL_3_BANK(slv_id,
932 bank));
933 }
934 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
935 reg[len] = SWRM_CMD_FIFO_WR_CMD;
936 val[len++] =
937 SWR_REG_VAL_PACK(mport->blk_grp_count,
938 port_req->dev_num, 0x00,
939 SWRS_DP_BLOCK_CONTROL_2_BANK(slv_id,
940 bank));
941 }
942 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
943 reg[len] = SWRM_CMD_FIFO_WR_CMD;
944 val[len++] =
945 SWR_REG_VAL_PACK(mport->lane_ctrl,
946 port_req->dev_num, 0x00,
947 SWRS_DP_LANE_CONTROL_BANK(slv_id,
948 bank));
949 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530950 port_req->ch_en = port_req->req_ch;
951 }
952 value = ((mport->req_ch)
953 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +0530954
955 if (mport->offset2 != SWR_INVALID_PARAM)
956 value |= ((mport->offset2)
957 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530958 value |= ((mport->offset1)
959 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
960 value |= mport->sinterval;
961
962
963 reg[len] = SWRM_DP_PORT_CTRL_BANK(i + 1, bank);
964 val[len++] = value;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530965 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
966 __func__, i,
967 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)), value);
968
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530969 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
970 reg[len] = SWRM_DP_PORT_CTRL_2_BANK(i + 1, bank);
971 val[len++] = mport->lane_ctrl;
972 }
973 if (mport->word_length != SWR_INVALID_PARAM) {
974 reg[len] = SWRM_DP_BLOCK_CTRL_1(i + 1);
975 val[len++] = mport->word_length;
976 }
977
978 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
979 reg[len] = SWRM_DP_BLOCK_CTRL2_BANK(i + 1, bank);
980 val[len++] = mport->blk_grp_count;
981 }
982 if (mport->hstart != SWR_INVALID_PARAM
983 && mport->hstop != SWR_INVALID_PARAM) {
984 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
Laxminath Kasame30eef72018-11-05 17:40:09 +0530985 hparams = (mport->hstop << 4) | mport->hstart;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530986 val[len++] = hparams;
Laxminath Kasam990c70b2018-11-09 23:15:09 +0530987 } else {
988 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
989 hparams = (SWR_HSTOP_MAX_VAL << 4) | SWR_HSTART_MIN_VAL;
990 val[len++] = hparams;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530991 }
992 if (mport->blk_pack_mode != SWR_INVALID_PARAM) {
993 reg[len] = SWRM_DP_BLOCK_CTRL3_BANK(i + 1, bank);
994 val[len++] = mport->blk_pack_mode;
995 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530996 mport->ch_en = mport->req_ch;
997
998 }
Sudheer Papothi0016db12019-06-11 04:42:38 +0530999 swrm_reg_dump(swrm, reg, val, len, __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301000 swr_master_bulk_write(swrm, reg, val, len);
1001}
1002
1003static void swrm_apply_port_config(struct swr_master *master)
1004{
1005 u8 bank;
1006 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1007
1008 if (!swrm) {
1009 pr_err("%s: Invalid handle to swr controller\n",
1010 __func__);
1011 return;
1012 }
1013
1014 bank = get_inactive_bank_num(swrm);
1015 dev_dbg(swrm->dev, "%s: enter bank: %d master_ports: %d\n",
1016 __func__, bank, master->num_port);
1017
1018
1019 swrm_cmd_fifo_wr_cmd(swrm, 0x01, 0xF, 0x00,
1020 SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(bank));
1021
1022 swrm_copy_data_port_config(master, bank);
1023}
1024
1025static int swrm_slvdev_datapath_control(struct swr_master *master, bool enable)
1026{
1027 u8 bank;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301028 u32 value, n_row, n_col;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301029 int ret;
1030 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1031 int mask = (SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK |
1032 SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK |
1033 SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_BMSK);
1034 u8 inactive_bank;
1035
1036 if (!swrm) {
1037 pr_err("%s: swrm is null\n", __func__);
1038 return -EFAULT;
1039 }
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301040
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301041 mutex_lock(&swrm->mlock);
1042
Ramprasad Katkam979b7c92019-05-17 15:31:21 +05301043 /*
1044 * During disable if master is already down, which implies an ssr/pdr
1045 * scenario, just mark ports as disabled and exit
1046 */
1047 if (swrm->state == SWR_MSTR_SSR && !enable) {
1048 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1049 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1050 __func__);
1051 goto exit;
1052 }
1053 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
1054 swrm_cleanup_disabled_port_reqs(master);
1055 if (!swrm_is_port_en(master)) {
1056 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1057 __func__);
1058 pm_runtime_mark_last_busy(swrm->dev);
1059 pm_runtime_put_autosuspend(swrm->dev);
1060 }
1061 goto exit;
1062 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301063 bank = get_inactive_bank_num(swrm);
1064
1065 if (enable) {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301066 if (!test_bit(ENABLE_PENDING, &swrm->port_req_pending)) {
1067 dev_dbg(swrm->dev, "%s:No pending connect port req\n",
1068 __func__);
1069 goto exit;
1070 }
1071 clear_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301072 ret = swrm_get_port_config(swrm);
1073 if (ret) {
1074 /* cannot accommodate ports */
1075 swrm_cleanup_disabled_port_reqs(master);
1076 mutex_unlock(&swrm->mlock);
1077 return -EINVAL;
1078 }
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301079 swr_master_write(swrm, SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301080 SWRM_INTERRUPT_STATUS_MASK);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301081 /* apply the new port config*/
1082 swrm_apply_port_config(master);
1083 } else {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301084 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1085 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1086 __func__);
1087 goto exit;
1088 }
1089 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301090 swrm_disable_ports(master, bank);
1091 }
1092 dev_dbg(swrm->dev, "%s: enable: %d, cfg_devs: %d\n",
1093 __func__, enable, swrm->num_cfg_devs);
1094
1095 if (enable) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301096 /* set col = 16 */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301097 n_col = SWR_MAX_COL;
1098 } else {
1099 /*
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301100 * Do not change to col = 2 if there are still active ports
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301101 */
1102 if (!master->num_port)
1103 n_col = SWR_MIN_COL;
1104 else
1105 n_col = SWR_MAX_COL;
1106 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301107 /* Use default 50 * x, frame shape. Change based on mclk */
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301108 if (swrm->mclk_freq == MCLK_FREQ_NATIVE) {
1109 dev_dbg(swrm->dev, "setting 64 x %d frameshape\n",
1110 n_col ? 16 : 2);
1111 n_row = SWR_ROW_64;
1112 } else {
1113 dev_dbg(swrm->dev, "setting 50 x %d frameshape\n",
1114 n_col ? 16 : 2);
1115 n_row = SWR_ROW_50;
1116 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301117 value = swr_master_read(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank));
1118 value &= (~mask);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301119 value |= ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301120 (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
1121 (0 << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
1122 swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1123
1124 dev_dbg(swrm->dev, "%s: regaddr: 0x%x, value: 0x%x\n", __func__,
1125 SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1126
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301127 enable_bank_switch(swrm, bank, n_row, n_col);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301128 inactive_bank = bank ? 0 : 1;
1129
1130 if (enable)
1131 swrm_copy_data_port_config(master, inactive_bank);
1132 else {
1133 swrm_disable_ports(master, inactive_bank);
1134 swrm_cleanup_disabled_port_reqs(master);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301135 }
1136 if (!swrm_is_port_en(master)) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301137 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1138 __func__);
1139 pm_runtime_mark_last_busy(swrm->dev);
1140 pm_runtime_put_autosuspend(swrm->dev);
1141 }
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301142exit:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301143 mutex_unlock(&swrm->mlock);
1144return 0;
1145}
1146
1147static int swrm_connect_port(struct swr_master *master,
1148 struct swr_params *portinfo)
1149{
1150 int i;
1151 struct swr_port_info *port_req;
1152 int ret = 0;
1153 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1154 struct swrm_mports *mport;
1155 u8 mstr_port_id, mstr_ch_msk;
1156
1157 dev_dbg(&master->dev, "%s: enter\n", __func__);
1158 if (!portinfo)
1159 return -EINVAL;
1160
1161 if (!swrm) {
1162 dev_err(&master->dev,
1163 "%s: Invalid handle to swr controller\n",
1164 __func__);
1165 return -EINVAL;
1166 }
1167
1168 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301169 mutex_lock(&swrm->devlock);
1170 if (!swrm->dev_up) {
1171 mutex_unlock(&swrm->devlock);
1172 mutex_unlock(&swrm->mlock);
1173 return -EINVAL;
1174 }
1175 mutex_unlock(&swrm->devlock);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301176 if (!swrm_is_port_en(master))
1177 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301178
1179 for (i = 0; i < portinfo->num_port; i++) {
1180 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_msk,
1181 portinfo->port_type[i],
1182 portinfo->port_id[i]);
1183 if (ret) {
1184 dev_err(&master->dev,
1185 "%s: mstr portid for slv port %d not found\n",
1186 __func__, portinfo->port_id[i]);
1187 goto port_fail;
1188 }
1189
1190 mport = &(swrm->mport_cfg[mstr_port_id]);
1191 /* get port req */
1192 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1193 portinfo->dev_num);
1194 if (!port_req) {
1195 dev_dbg(&master->dev, "%s: new req:port id %d dev %d\n",
1196 __func__, portinfo->port_id[i],
1197 portinfo->dev_num);
1198 port_req = kzalloc(sizeof(struct swr_port_info),
1199 GFP_KERNEL);
1200 if (!port_req) {
1201 ret = -ENOMEM;
1202 goto mem_fail;
1203 }
1204 port_req->dev_num = portinfo->dev_num;
1205 port_req->slave_port_id = portinfo->port_id[i];
1206 port_req->num_ch = portinfo->num_ch[i];
1207 port_req->ch_rate = portinfo->ch_rate[i];
1208 port_req->ch_en = 0;
1209 port_req->master_port_id = mstr_port_id;
1210 list_add(&port_req->list, &mport->port_req_list);
1211 }
1212 port_req->req_ch |= portinfo->ch_en[i];
1213
1214 dev_dbg(&master->dev,
1215 "%s: mstr port %d, slv port %d ch_rate %d num_ch %d\n",
1216 __func__, port_req->master_port_id,
1217 port_req->slave_port_id, port_req->ch_rate,
1218 port_req->num_ch);
1219 /* Put the port req on master port */
1220 mport = &(swrm->mport_cfg[mstr_port_id]);
1221 mport->port_en = true;
1222 mport->req_ch |= mstr_ch_msk;
1223 master->port_en_mask |= (1 << mstr_port_id);
1224 }
1225 master->num_port += portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301226 set_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301227 swr_port_response(master, portinfo->tid);
1228
1229 mutex_unlock(&swrm->mlock);
1230 return 0;
1231
1232port_fail:
1233mem_fail:
1234 /* cleanup port reqs in error condition */
1235 swrm_cleanup_disabled_port_reqs(master);
1236 mutex_unlock(&swrm->mlock);
1237 return ret;
1238}
1239
1240static int swrm_disconnect_port(struct swr_master *master,
1241 struct swr_params *portinfo)
1242{
1243 int i, ret = 0;
1244 struct swr_port_info *port_req;
1245 struct swrm_mports *mport;
1246 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1247 u8 mstr_port_id, mstr_ch_mask;
1248
1249 if (!swrm) {
1250 dev_err(&master->dev,
1251 "%s: Invalid handle to swr controller\n",
1252 __func__);
1253 return -EINVAL;
1254 }
1255
1256 if (!portinfo) {
1257 dev_err(&master->dev, "%s: portinfo is NULL\n", __func__);
1258 return -EINVAL;
1259 }
1260 mutex_lock(&swrm->mlock);
1261
1262 for (i = 0; i < portinfo->num_port; i++) {
1263
1264 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_mask,
1265 portinfo->port_type[i], portinfo->port_id[i]);
1266 if (ret) {
1267 dev_err(&master->dev,
1268 "%s: mstr portid for slv port %d not found\n",
1269 __func__, portinfo->port_id[i]);
1270 mutex_unlock(&swrm->mlock);
1271 return -EINVAL;
1272 }
1273 mport = &(swrm->mport_cfg[mstr_port_id]);
1274 /* get port req */
1275 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1276 portinfo->dev_num);
1277
1278 if (!port_req) {
1279 dev_err(&master->dev, "%s:port not enabled : port %d\n",
1280 __func__, portinfo->port_id[i]);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05301281 mutex_unlock(&swrm->mlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301282 return -EINVAL;
1283 }
1284 port_req->req_ch &= ~portinfo->ch_en[i];
1285 mport->req_ch &= ~mstr_ch_mask;
1286 }
1287 master->num_port -= portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301288 set_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301289 swr_port_response(master, portinfo->tid);
1290 mutex_unlock(&swrm->mlock);
1291
1292 return 0;
1293}
1294
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301295static int swrm_find_alert_slave(struct swr_mstr_ctrl *swrm,
1296 int status, u8 *devnum)
1297{
1298 int i;
1299 bool found = false;
1300
1301 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1302 if ((status & SWRM_MCP_SLV_STATUS_MASK) == SWR_ALERT) {
1303 *devnum = i;
1304 found = true;
1305 break;
1306 }
1307 status >>= 2;
1308 }
1309 if (found)
1310 return 0;
1311 else
1312 return -EINVAL;
1313}
1314
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301315static void swrm_enable_slave_irq(struct swr_mstr_ctrl *swrm)
1316{
1317 int i;
1318 int status = 0;
1319
1320 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1321 if (!status) {
1322 dev_dbg_ratelimited(swrm->dev, "%s: slaves status is 0x%x\n",
1323 __func__, status);
1324 return;
1325 }
1326 dev_dbg(swrm->dev, "%s: slave status: 0x%x\n", __func__, status);
1327 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1328 if (status & SWRM_MCP_SLV_STATUS_MASK)
1329 swrm_cmd_fifo_wr_cmd(swrm, 0x4, i, 0x0,
1330 SWRS_SCP_INT_STATUS_MASK_1);
1331 status >>= 2;
1332 }
1333}
1334
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301335static int swrm_check_slave_change_status(struct swr_mstr_ctrl *swrm,
1336 int status, u8 *devnum)
1337{
1338 int i;
1339 int new_sts = status;
1340 int ret = SWR_NOT_PRESENT;
1341
1342 if (status != swrm->slave_status) {
1343 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1344 if ((status & SWRM_MCP_SLV_STATUS_MASK) !=
1345 (swrm->slave_status & SWRM_MCP_SLV_STATUS_MASK)) {
1346 ret = (status & SWRM_MCP_SLV_STATUS_MASK);
1347 *devnum = i;
1348 break;
1349 }
1350 status >>= 2;
1351 swrm->slave_status >>= 2;
1352 }
1353 swrm->slave_status = new_sts;
1354 }
1355 return ret;
1356}
1357
1358static irqreturn_t swr_mstr_interrupt(int irq, void *dev)
1359{
1360 struct swr_mstr_ctrl *swrm = dev;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301361 u32 value, intr_sts, intr_sts_masked;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301362 u32 temp = 0;
1363 u32 status, chg_sts, i;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301364 u8 devnum = 0;
1365 int ret = IRQ_HANDLED;
1366 struct swr_device *swr_dev;
1367 struct swr_master *mstr = &swrm->master;
1368
Ramprasad Katkam57349872018-11-11 18:34:57 +05301369 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1370 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1371 return IRQ_NONE;
1372 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301373
1374 mutex_lock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301375 if (swrm_clk_request(swrm, true)) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +05301376 dev_err_ratelimited(swrm->dev, "%s:clk request failed\n",
1377 __func__);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301378 mutex_unlock(&swrm->reslock);
1379 goto exit;
1380 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301381 mutex_unlock(&swrm->reslock);
1382
1383 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301384 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301385handle_irq:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301386 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301387 value = intr_sts_masked & (1 << i);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301388 if (!value)
1389 continue;
1390
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301391 switch (value) {
1392 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1393 dev_dbg(swrm->dev, "Trigger irq to slave device\n");
1394 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301395 ret = swrm_find_alert_slave(swrm, status, &devnum);
1396 if (ret) {
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301397 dev_err_ratelimited(swrm->dev,
1398 "no slave alert found.spurious interrupt\n");
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05301399 break;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301400 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301401 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1402 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1403 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1404 SWRS_SCP_INT_STATUS_CLEAR_1);
1405 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1406 SWRS_SCP_INT_STATUS_CLEAR_1);
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301407
1408
1409 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1410 if (swr_dev->dev_num != devnum)
1411 continue;
1412 if (swr_dev->slave_irq) {
1413 do {
1414 handle_nested_irq(
1415 irq_find_mapping(
1416 swr_dev->slave_irq, 0));
1417 } while (swr_dev->slave_irq_pending);
1418 }
1419
1420 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301421 break;
1422 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1423 dev_dbg(swrm->dev, "SWR new slave attached\n");
1424 break;
1425 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1426 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1427 if (status == swrm->slave_status) {
1428 dev_dbg(swrm->dev,
1429 "%s: No change in slave status: %d\n",
1430 __func__, status);
1431 break;
1432 }
1433 chg_sts = swrm_check_slave_change_status(swrm, status,
1434 &devnum);
1435 switch (chg_sts) {
1436 case SWR_NOT_PRESENT:
1437 dev_dbg(swrm->dev, "device %d got detached\n",
1438 devnum);
1439 break;
1440 case SWR_ATTACHED_OK:
1441 dev_dbg(swrm->dev, "device %d got attached\n",
1442 devnum);
Ramprasad Katkamdebe8932018-09-25 18:08:18 +05301443 /* enable host irq from slave device*/
1444 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1445 SWRS_SCP_INT_STATUS_CLEAR_1);
1446 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1447 SWRS_SCP_INT_STATUS_MASK_1);
1448
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301449 break;
1450 case SWR_ALERT:
1451 dev_dbg(swrm->dev,
1452 "device %d has pending interrupt\n",
1453 devnum);
1454 break;
1455 }
1456 break;
1457 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1458 dev_err_ratelimited(swrm->dev,
1459 "SWR bus clsh detected\n");
1460 break;
1461 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1462 dev_dbg(swrm->dev, "SWR read FIFO overflow\n");
1463 break;
1464 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1465 dev_dbg(swrm->dev, "SWR read FIFO underflow\n");
1466 break;
1467 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1468 dev_dbg(swrm->dev, "SWR write FIFO overflow\n");
1469 break;
1470 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1471 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1472 dev_err_ratelimited(swrm->dev,
1473 "SWR CMD error, fifo status 0x%x, flushing fifo\n",
1474 value);
1475 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1476 break;
1477 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301478 dev_err_ratelimited(swrm->dev, "SWR Port collision detected\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301479 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301480 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301481 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301482 break;
1483 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1484 dev_dbg(swrm->dev, "SWR read enable valid mismatch\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301485 swrm->intr_mask &=
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301486 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1487 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301488 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301489 break;
1490 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1491 complete(&swrm->broadcast);
1492 dev_dbg(swrm->dev, "SWR cmd id finished\n");
1493 break;
1494 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_AUTO_ENUM_FINISHED:
1495 break;
1496 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED:
1497 break;
1498 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL:
1499 break;
1500 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED:
1501 complete(&swrm->reset);
1502 break;
1503 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED:
1504 break;
1505 default:
1506 dev_err_ratelimited(swrm->dev,
1507 "SWR unknown interrupt\n");
1508 ret = IRQ_NONE;
1509 break;
1510 }
1511 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301512 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1513 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
Ramprasad Katkam83303512018-10-11 17:34:22 +05301514
1515 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301516 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301517
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301518 if (intr_sts_masked) {
Ramprasad Katkam83303512018-10-11 17:34:22 +05301519 dev_dbg(swrm->dev, "%s: new interrupt received\n", __func__);
1520 goto handle_irq;
1521 }
1522
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301523 mutex_lock(&swrm->reslock);
1524 swrm_clk_request(swrm, false);
1525 mutex_unlock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301526exit:
Ramprasad Katkam57349872018-11-11 18:34:57 +05301527 swrm_unlock_sleep(swrm);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301528 return ret;
1529}
1530
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301531static irqreturn_t swr_mstr_interrupt_v2(int irq, void *dev)
1532{
1533 struct swr_mstr_ctrl *swrm = dev;
1534 u32 value, intr_sts, intr_sts_masked;
1535 u32 temp = 0;
1536 u32 status, chg_sts, i;
1537 u8 devnum = 0;
1538 int ret = IRQ_HANDLED;
1539 struct swr_device *swr_dev;
1540 struct swr_master *mstr = &swrm->master;
1541
1542 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1543 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1544 return IRQ_NONE;
1545 }
1546
1547 mutex_lock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301548 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
1549 ret = IRQ_NONE;
1550 goto exit;
1551 }
1552 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
1553 ret = IRQ_NONE;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301554 goto err_audio_hw_vote;
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001555 }
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301556 swrm_clk_request(swrm, true);
1557 mutex_unlock(&swrm->reslock);
1558
1559 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1560 intr_sts_masked = intr_sts & swrm->intr_mask;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301561
1562 dev_dbg(swrm->dev, "%s: status: 0x%x \n", __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301563handle_irq:
1564 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
1565 value = intr_sts_masked & (1 << i);
1566 if (!value)
1567 continue;
1568
1569 switch (value) {
1570 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1571 dev_dbg(swrm->dev, "%s: Trigger irq to slave device\n",
1572 __func__);
1573 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1574 ret = swrm_find_alert_slave(swrm, status, &devnum);
1575 if (ret) {
1576 dev_err_ratelimited(swrm->dev,
1577 "%s: no slave alert found.spurious interrupt\n",
1578 __func__);
1579 break;
1580 }
1581 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1582 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1583 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1584 SWRS_SCP_INT_STATUS_CLEAR_1);
1585 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1586 SWRS_SCP_INT_STATUS_CLEAR_1);
1587
1588
1589 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1590 if (swr_dev->dev_num != devnum)
1591 continue;
1592 if (swr_dev->slave_irq) {
1593 do {
1594 handle_nested_irq(
1595 irq_find_mapping(
1596 swr_dev->slave_irq, 0));
1597 } while (swr_dev->slave_irq_pending);
1598 }
1599
1600 }
1601 break;
1602 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1603 dev_dbg(swrm->dev, "%s: SWR new slave attached\n",
1604 __func__);
1605 break;
1606 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1607 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1608 if (status == swrm->slave_status) {
1609 dev_dbg(swrm->dev,
1610 "%s: No change in slave status: %d\n",
1611 __func__, status);
1612 break;
1613 }
1614 chg_sts = swrm_check_slave_change_status(swrm, status,
1615 &devnum);
1616 switch (chg_sts) {
1617 case SWR_NOT_PRESENT:
1618 dev_dbg(swrm->dev,
1619 "%s: device %d got detached\n",
1620 __func__, devnum);
1621 break;
1622 case SWR_ATTACHED_OK:
1623 dev_dbg(swrm->dev,
1624 "%s: device %d got attached\n",
1625 __func__, devnum);
1626 /* enable host irq from slave device*/
1627 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1628 SWRS_SCP_INT_STATUS_CLEAR_1);
1629 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1630 SWRS_SCP_INT_STATUS_MASK_1);
1631
1632 break;
1633 case SWR_ALERT:
1634 dev_dbg(swrm->dev,
1635 "%s: device %d has pending interrupt\n",
1636 __func__, devnum);
1637 break;
1638 }
1639 break;
1640 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1641 dev_err_ratelimited(swrm->dev,
1642 "%s: SWR bus clsh detected\n",
1643 __func__);
1644 break;
1645 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1646 dev_dbg(swrm->dev, "%s: SWR read FIFO overflow\n",
1647 __func__);
1648 break;
1649 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1650 dev_dbg(swrm->dev, "%s: SWR read FIFO underflow\n",
1651 __func__);
1652 break;
1653 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1654 dev_dbg(swrm->dev, "%s: SWR write FIFO overflow\n",
1655 __func__);
1656 break;
1657 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1658 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1659 dev_err_ratelimited(swrm->dev,
1660 "%s: SWR CMD error, fifo status 0x%x, flushing fifo\n",
1661 __func__, value);
1662 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1663 break;
1664 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
1665 dev_err_ratelimited(swrm->dev,
1666 "%s: SWR Port collision detected\n",
1667 __func__);
1668 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
1669 swr_master_write(swrm,
1670 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1671 break;
1672 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1673 dev_dbg(swrm->dev,
1674 "%s: SWR read enable valid mismatch\n",
1675 __func__);
1676 swrm->intr_mask &=
1677 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1678 swr_master_write(swrm,
1679 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1680 break;
1681 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1682 complete(&swrm->broadcast);
1683 dev_dbg(swrm->dev, "%s: SWR cmd id finished\n",
1684 __func__);
1685 break;
1686 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED_V2:
1687 break;
1688 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL_V2:
1689 break;
1690 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED_V2:
1691 break;
1692 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED_V2:
1693 break;
1694 case SWRM_INTERRUPT_STATUS_EXT_CLK_STOP_WAKEUP:
1695 if (swrm->state == SWR_MSTR_UP)
1696 dev_dbg(swrm->dev,
1697 "%s:SWR Master is already up\n",
1698 __func__);
1699 else
1700 dev_err_ratelimited(swrm->dev,
1701 "%s: SWR wokeup during clock stop\n",
1702 __func__);
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301703 /* It might be possible the slave device gets reset
1704 * and slave interrupt gets missed. So re-enable
1705 * Host IRQ and process slave pending
1706 * interrupts, if any.
1707 */
1708 swrm_enable_slave_irq(swrm);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301709 break;
1710 default:
1711 dev_err_ratelimited(swrm->dev,
1712 "%s: SWR unknown interrupt value: %d\n",
1713 __func__, value);
1714 ret = IRQ_NONE;
1715 break;
1716 }
1717 }
1718 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1719 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
1720
1721 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1722 intr_sts_masked = intr_sts & swrm->intr_mask;
1723
1724 if (intr_sts_masked) {
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301725 dev_dbg(swrm->dev, "%s: new interrupt received 0x%x\n",
1726 __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301727 goto handle_irq;
1728 }
1729
1730 mutex_lock(&swrm->reslock);
1731 swrm_clk_request(swrm, false);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301732 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
Sudheer Papothi06f43412019-07-09 03:32:54 +05301733
1734err_audio_hw_vote:
Sudheer Papothi384addd2019-06-14 02:26:52 +05301735 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001736exit:
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301737 mutex_unlock(&swrm->reslock);
1738 swrm_unlock_sleep(swrm);
1739 return ret;
1740}
1741
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301742static irqreturn_t swrm_wakeup_interrupt(int irq, void *dev)
1743{
1744 struct swr_mstr_ctrl *swrm = dev;
1745 int ret = IRQ_HANDLED;
1746
1747 if (!swrm || !(swrm->dev)) {
1748 pr_err("%s: swrm or dev is null\n", __func__);
1749 return IRQ_NONE;
1750 }
1751 mutex_lock(&swrm->devlock);
1752 if (!swrm->dev_up) {
1753 if (swrm->wake_irq > 0)
1754 disable_irq_nosync(swrm->wake_irq);
1755 mutex_unlock(&swrm->devlock);
1756 return ret;
1757 }
1758 mutex_unlock(&swrm->devlock);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301759 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1760 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1761 goto exit;
1762 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301763 if (swrm->wake_irq > 0)
1764 disable_irq_nosync(swrm->wake_irq);
1765 pm_runtime_get_sync(swrm->dev);
1766 pm_runtime_mark_last_busy(swrm->dev);
1767 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301768 swrm_unlock_sleep(swrm);
1769exit:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301770 return ret;
1771}
1772
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301773static void swrm_wakeup_work(struct work_struct *work)
1774{
1775 struct swr_mstr_ctrl *swrm;
1776
1777 swrm = container_of(work, struct swr_mstr_ctrl,
1778 wakeup_work);
1779 if (!swrm || !(swrm->dev)) {
1780 pr_err("%s: swrm or dev is null\n", __func__);
1781 return;
1782 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301783
1784 mutex_lock(&swrm->devlock);
1785 if (!swrm->dev_up) {
1786 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301787 goto exit;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301788 }
1789 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301790 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1791 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1792 goto exit;
1793 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301794 pm_runtime_get_sync(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301795 pm_runtime_mark_last_busy(swrm->dev);
1796 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301797 swrm_unlock_sleep(swrm);
1798exit:
1799 pm_relax(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301800}
1801
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301802static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
1803{
1804 u32 val;
1805
1806 swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1807 val = (swrm->slave_status >> (devnum * 2));
1808 val &= SWRM_MCP_SLV_STATUS_MASK;
1809 return val;
1810}
1811
1812static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
1813 u8 *dev_num)
1814{
1815 int i;
1816 u64 id = 0;
1817 int ret = -EINVAL;
1818 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1819 struct swr_device *swr_dev;
1820 u32 num_dev = 0;
1821
1822 if (!swrm) {
1823 pr_err("%s: Invalid handle to swr controller\n",
1824 __func__);
1825 return ret;
1826 }
1827 if (swrm->num_dev)
1828 num_dev = swrm->num_dev;
1829 else
1830 num_dev = mstr->num_dev;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301831
1832 mutex_lock(&swrm->devlock);
1833 if (!swrm->dev_up) {
1834 mutex_unlock(&swrm->devlock);
1835 return ret;
1836 }
1837 mutex_unlock(&swrm->devlock);
1838
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301839 pm_runtime_get_sync(swrm->dev);
1840 for (i = 1; i < (num_dev + 1); i++) {
1841 id = ((u64)(swr_master_read(swrm,
1842 SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
1843 id |= swr_master_read(swrm,
1844 SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301845
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301846 /*
1847 * As pm_runtime_get_sync() brings all slaves out of reset
1848 * update logical device number for all slaves.
1849 */
1850 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1851 if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
1852 u32 status = swrm_get_device_status(swrm, i);
1853
1854 if ((status == 0x01) || (status == 0x02)) {
1855 swr_dev->dev_num = i;
1856 if ((id & SWR_DEV_ID_MASK) == dev_id) {
1857 *dev_num = i;
1858 ret = 0;
1859 }
1860 dev_dbg(swrm->dev,
1861 "%s: devnum %d is assigned for dev addr %lx\n",
1862 __func__, i, swr_dev->addr);
1863 }
1864 }
1865 }
1866 }
1867 if (ret)
1868 dev_err(swrm->dev, "%s: device 0x%llx is not ready\n",
1869 __func__, dev_id);
1870
1871 pm_runtime_mark_last_busy(swrm->dev);
1872 pm_runtime_put_autosuspend(swrm->dev);
1873 return ret;
1874}
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301875
1876static void swrm_device_wakeup_vote(struct swr_master *mstr)
1877{
1878 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1879
1880 if (!swrm) {
1881 pr_err("%s: Invalid handle to swr controller\n",
1882 __func__);
1883 return;
1884 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05301885 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1886 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1887 return;
1888 }
Sudheer Papothi384addd2019-06-14 02:26:52 +05301889 if (++swrm->hw_core_clk_en == 1)
1890 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
1891 dev_err(swrm->dev, "%s:lpass core hw enable failed\n",
1892 __func__);
1893 --swrm->hw_core_clk_en;
1894 }
1895 if ( ++swrm->aud_core_clk_en == 1)
1896 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
1897 dev_err(swrm->dev, "%s:lpass audio hw enable failed\n",
1898 __func__);
1899 --swrm->aud_core_clk_en;
1900 }
1901 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
1902 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301903 pm_runtime_get_sync(swrm->dev);
1904}
1905
1906static void swrm_device_wakeup_unvote(struct swr_master *mstr)
1907{
1908 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1909
1910 if (!swrm) {
1911 pr_err("%s: Invalid handle to swr controller\n",
1912 __func__);
1913 return;
1914 }
1915 pm_runtime_mark_last_busy(swrm->dev);
1916 pm_runtime_put_autosuspend(swrm->dev);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301917 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
1918 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
1919
1920 --swrm->aud_core_clk_en;
1921 if (swrm->aud_core_clk_en < 0)
1922 swrm->aud_core_clk_en = 0;
1923 else if (swrm->aud_core_clk_en == 0)
1924 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
1925
1926 --swrm->hw_core_clk_en;
1927 if (swrm->hw_core_clk_en < 0)
1928 swrm->hw_core_clk_en = 0;
1929 else if (swrm->hw_core_clk_en == 0)
1930 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
1931
Ramprasad Katkam57349872018-11-11 18:34:57 +05301932 swrm_unlock_sleep(swrm);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301933}
1934
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301935static int swrm_master_init(struct swr_mstr_ctrl *swrm)
1936{
1937 int ret = 0;
1938 u32 val;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301939 u8 row_ctrl = SWR_ROW_50;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301940 u8 col_ctrl = SWR_MIN_COL;
1941 u8 ssp_period = 1;
1942 u8 retry_cmd_num = 3;
1943 u32 reg[SWRM_MAX_INIT_REG];
1944 u32 value[SWRM_MAX_INIT_REG];
1945 int len = 0;
1946
1947 /* Clear Rows and Cols */
1948 val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
1949 (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
1950 (ssp_period << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
1951
1952 reg[len] = SWRM_MCP_FRAME_CTRL_BANK_ADDR(0);
1953 value[len++] = val;
1954
1955 /* Set Auto enumeration flag */
1956 reg[len] = SWRM_ENUMERATOR_CFG_ADDR;
1957 value[len++] = 1;
1958
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301959 /* Configure No pings */
1960 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
1961 val &= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK;
1962 val |= (0x1f << SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT);
1963 reg[len] = SWRM_MCP_CFG_ADDR;
1964 value[len++] = val;
1965
1966 /* Configure number of retries of a read/write cmd */
1967 val = (retry_cmd_num << SWRM_CMD_FIFO_CFG_NUM_OF_CMD_RETRY_SHFT);
1968 reg[len] = SWRM_CMD_FIFO_CFG_ADDR;
1969 value[len++] = val;
1970
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301971 reg[len] = SWRM_MCP_BUS_CTRL_ADDR;
1972 value[len++] = 0x2;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301973
Ramprasad Katkam83303512018-10-11 17:34:22 +05301974 /* Set IRQ to PULSE */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301975 reg[len] = SWRM_COMP_CFG_ADDR;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301976 value[len++] = 0x02;
1977
1978 reg[len] = SWRM_COMP_CFG_ADDR;
1979 value[len++] = 0x03;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301980
1981 reg[len] = SWRM_INTERRUPT_CLEAR;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301982 value[len++] = 0xFFFFFFFF;
1983
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301984 swrm->intr_mask = SWRM_INTERRUPT_STATUS_MASK;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301985 /* Mask soundwire interrupts */
1986 reg[len] = SWRM_INTERRUPT_MASK_ADDR;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301987 value[len++] = swrm->intr_mask;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301988
1989 reg[len] = SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301990 value[len++] = swrm->intr_mask;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301991
1992 swr_master_bulk_write(swrm, reg, value, len);
1993
Sudheer Papothi63f48152018-11-15 01:08:03 +05301994 /*
1995 * For SWR master version 1.5.1, continue
1996 * execute on command ignore.
1997 */
1998 if (swrm->version == SWRM_VERSION_1_5_1)
1999 swr_master_write(swrm, SWRM_CMD_FIFO_CFG_ADDR,
2000 (swr_master_read(swrm,
2001 SWRM_CMD_FIFO_CFG_ADDR) | 0x80000000));
2002
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302003 return ret;
2004}
2005
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302006static int swrm_event_notify(struct notifier_block *self,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302007 unsigned long action, void *data)
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302008{
2009 struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302010 event_notifier);
2011
2012 if (!swrm || !(swrm->dev)) {
2013 pr_err("%s: swrm or dev is NULL\n", __func__);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302014 return -EINVAL;
2015 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302016 switch (action) {
2017 case MSM_AUD_DC_EVENT:
2018 schedule_work(&(swrm->dc_presence_work));
2019 break;
2020 case SWR_WAKE_IRQ_EVENT:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302021 if (swrm->ipc_wakeup && !swrm->ipc_wakeup_triggered) {
2022 swrm->ipc_wakeup_triggered = true;
Ramprasad Katkam57349872018-11-11 18:34:57 +05302023 pm_stay_awake(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302024 schedule_work(&swrm->wakeup_work);
Ramprasad Katkamcd61c6e2018-09-18 13:22:58 +05302025 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302026 break;
2027 default:
2028 dev_err(swrm->dev, "%s: invalid event type: %lu\n",
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302029 __func__, action);
2030 return -EINVAL;
2031 }
2032
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302033 return 0;
2034}
2035
2036static void swrm_notify_work_fn(struct work_struct *work)
2037{
2038 struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302039 dc_presence_work);
2040
2041 if (!swrm || !swrm->pdev) {
2042 pr_err("%s: swrm or pdev is NULL\n", __func__);
2043 return;
2044 }
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302045 swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL);
2046}
2047
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302048static int swrm_probe(struct platform_device *pdev)
2049{
2050 struct swr_mstr_ctrl *swrm;
2051 struct swr_ctrl_platform_data *pdata;
2052 u32 i, num_ports, port_num, port_type, ch_mask;
2053 u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0;
2054 int ret = 0;
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302055 struct clk *lpass_core_hw_vote = NULL;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302056 struct clk *lpass_core_audio = NULL;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302057
2058 /* Allocate soundwire master driver structure */
2059 swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl),
2060 GFP_KERNEL);
2061 if (!swrm) {
2062 ret = -ENOMEM;
2063 goto err_memory_fail;
2064 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302065 swrm->pdev = pdev;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302066 swrm->dev = &pdev->dev;
2067 platform_set_drvdata(pdev, swrm);
2068 swr_set_ctrl_data(&swrm->master, swrm);
2069 pdata = dev_get_platdata(&pdev->dev);
2070 if (!pdata) {
2071 dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
2072 __func__);
2073 ret = -EINVAL;
2074 goto err_pdata_fail;
2075 }
2076 swrm->handle = (void *)pdata->handle;
2077 if (!swrm->handle) {
2078 dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
2079 __func__);
2080 ret = -EINVAL;
2081 goto err_pdata_fail;
2082 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302083 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr_master_id",
2084 &swrm->master_id);
2085 if (ret) {
2086 dev_err(&pdev->dev, "%s: failed to get master id\n", __func__);
2087 goto err_pdata_fail;
2088 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302089 if (!(of_property_read_u32(pdev->dev.of_node,
2090 "swrm-io-base", &swrm->swrm_base_reg)))
2091 ret = of_property_read_u32(pdev->dev.of_node,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302092 "swrm-io-base", &swrm->swrm_base_reg);
2093 if (!swrm->swrm_base_reg) {
2094 swrm->read = pdata->read;
2095 if (!swrm->read) {
2096 dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
2097 __func__);
2098 ret = -EINVAL;
2099 goto err_pdata_fail;
2100 }
2101 swrm->write = pdata->write;
2102 if (!swrm->write) {
2103 dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
2104 __func__);
2105 ret = -EINVAL;
2106 goto err_pdata_fail;
2107 }
2108 swrm->bulk_write = pdata->bulk_write;
2109 if (!swrm->bulk_write) {
2110 dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
2111 __func__);
2112 ret = -EINVAL;
2113 goto err_pdata_fail;
2114 }
2115 } else {
2116 swrm->swrm_dig_base = devm_ioremap(&pdev->dev,
2117 swrm->swrm_base_reg, SWRM_MAX_REGISTER);
2118 }
2119
2120 swrm->clk = pdata->clk;
2121 if (!swrm->clk) {
2122 dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
2123 __func__);
2124 ret = -EINVAL;
2125 goto err_pdata_fail;
2126 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302127 if (of_property_read_u32(pdev->dev.of_node,
2128 "qcom,swr-clock-stop-mode0",
2129 &swrm->clk_stop_mode0_supp)) {
2130 swrm->clk_stop_mode0_supp = FALSE;
2131 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302132
2133 ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
2134 &swrm->num_dev);
2135 if (ret) {
2136 dev_dbg(&pdev->dev, "%s: Looking up %s property failed\n",
2137 __func__, "qcom,swr-num-dev");
2138 } else {
2139 if (swrm->num_dev > SWR_MAX_SLAVE_DEVICES) {
2140 dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
2141 __func__, swrm->num_dev, SWR_MAX_SLAVE_DEVICES);
2142 ret = -EINVAL;
2143 goto err_pdata_fail;
2144 }
2145 }
2146
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302147 /* Parse soundwire port mapping */
2148 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
2149 &num_ports);
2150 if (ret) {
2151 dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
2152 goto err_pdata_fail;
2153 }
2154 swrm->num_ports = num_ports;
2155
2156 if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
2157 &map_size)) {
2158 dev_err(swrm->dev, "missing port mapping\n");
2159 goto err_pdata_fail;
2160 }
2161
2162 map_length = map_size / (3 * sizeof(u32));
2163 if (num_ports > SWR_MSTR_PORT_LEN) {
2164 dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
2165 __func__);
2166 ret = -EINVAL;
2167 goto err_pdata_fail;
2168 }
2169 temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
2170
2171 if (!temp) {
2172 ret = -ENOMEM;
2173 goto err_pdata_fail;
2174 }
2175 ret = of_property_read_u32_array(pdev->dev.of_node,
2176 "qcom,swr-port-mapping", temp, 3 * map_length);
2177 if (ret) {
2178 dev_err(swrm->dev, "%s: Failed to read port mapping\n",
2179 __func__);
2180 goto err_pdata_fail;
2181 }
2182
2183 for (i = 0; i < map_length; i++) {
2184 port_num = temp[3 * i];
2185 port_type = temp[3 * i + 1];
2186 ch_mask = temp[3 * i + 2];
2187
2188 if (port_num != old_port_num)
2189 ch_iter = 0;
2190 swrm->port_mapping[port_num][ch_iter].port_type = port_type;
2191 swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
2192 old_port_num = port_num;
2193 }
2194 devm_kfree(&pdev->dev, temp);
2195
2196 swrm->reg_irq = pdata->reg_irq;
2197 swrm->master.read = swrm_read;
2198 swrm->master.write = swrm_write;
2199 swrm->master.bulk_write = swrm_bulk_write;
2200 swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
2201 swrm->master.connect_port = swrm_connect_port;
2202 swrm->master.disconnect_port = swrm_disconnect_port;
2203 swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
2204 swrm->master.remove_from_group = swrm_remove_from_group;
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302205 swrm->master.device_wakeup_vote = swrm_device_wakeup_vote;
2206 swrm->master.device_wakeup_unvote = swrm_device_wakeup_unvote;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302207 swrm->master.dev.parent = &pdev->dev;
2208 swrm->master.dev.of_node = pdev->dev.of_node;
2209 swrm->master.num_port = 0;
2210 swrm->rcmd_id = 0;
2211 swrm->wcmd_id = 0;
2212 swrm->slave_status = 0;
2213 swrm->num_rx_chs = 0;
2214 swrm->clk_ref_count = 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302215 swrm->swr_irq_wakeup_capable = 0;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302216 swrm->mclk_freq = MCLK_FREQ;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302217 swrm->dev_up = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302218 swrm->state = SWR_MSTR_UP;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302219 swrm->ipc_wakeup = false;
2220 swrm->ipc_wakeup_triggered = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302221 init_completion(&swrm->reset);
2222 init_completion(&swrm->broadcast);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302223 init_completion(&swrm->clk_off_complete);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302224 mutex_init(&swrm->mlock);
2225 mutex_init(&swrm->reslock);
2226 mutex_init(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302227 mutex_init(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302228 mutex_init(&swrm->clklock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302229 mutex_init(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302230 mutex_init(&swrm->pm_lock);
2231 swrm->wlock_holders = 0;
2232 swrm->pm_state = SWRM_PM_SLEEPABLE;
2233 init_waitqueue_head(&swrm->pm_wq);
2234 pm_qos_add_request(&swrm->pm_qos_req,
2235 PM_QOS_CPU_DMA_LATENCY,
2236 PM_QOS_DEFAULT_VALUE);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302237
2238 for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++)
2239 INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
2240
Sudheer Papothi06f43412019-07-09 03:32:54 +05302241 /* Register LPASS core hw vote */
2242 lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
2243 if (IS_ERR(lpass_core_hw_vote)) {
2244 ret = PTR_ERR(lpass_core_hw_vote);
2245 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2246 __func__, "lpass_core_hw_vote", ret);
2247 lpass_core_hw_vote = NULL;
2248 ret = 0;
2249 }
2250 swrm->lpass_core_hw_vote = lpass_core_hw_vote;
2251
2252 /* Register LPASS audio core vote */
2253 lpass_core_audio = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
2254 if (IS_ERR(lpass_core_audio)) {
2255 ret = PTR_ERR(lpass_core_audio);
2256 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2257 __func__, "lpass_core_audio", ret);
2258 lpass_core_audio = NULL;
2259 ret = 0;
2260 }
2261 swrm->lpass_core_audio = lpass_core_audio;
2262
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302263 if (swrm->reg_irq) {
2264 ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
2265 SWR_IRQ_REGISTER);
2266 if (ret) {
2267 dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
2268 __func__, ret);
2269 goto err_irq_fail;
2270 }
2271 } else {
2272 swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
2273 if (swrm->irq < 0) {
2274 dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
2275 __func__, swrm->irq);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302276 goto err_irq_fail;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302277 }
2278
2279 ret = request_threaded_irq(swrm->irq, NULL,
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302280 swr_mstr_interrupt_v2,
Ramprasad Katkam83303512018-10-11 17:34:22 +05302281 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302282 "swr_master_irq", swrm);
2283 if (ret) {
2284 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2285 __func__, ret);
2286 goto err_irq_fail;
2287 }
2288
2289 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302290 /* Make inband tx interrupts as wakeup capable for slave irq */
2291 ret = of_property_read_u32(pdev->dev.of_node,
2292 "qcom,swr-mstr-irq-wakeup-capable",
2293 &swrm->swr_irq_wakeup_capable);
2294 if (ret)
2295 dev_dbg(swrm->dev, "%s: swrm irq wakeup capable not defined\n",
2296 __func__);
2297 if (swrm->swr_irq_wakeup_capable)
2298 irq_set_irq_wake(swrm->irq, 1);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302299 ret = swr_register_master(&swrm->master);
2300 if (ret) {
2301 dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
2302 goto err_mstr_fail;
2303 }
2304
2305 /* Add devices registered with board-info as the
2306 * controller will be up now
2307 */
2308 swr_master_add_boarddevices(&swrm->master);
2309 mutex_lock(&swrm->mlock);
2310 swrm_clk_request(swrm, true);
2311 ret = swrm_master_init(swrm);
2312 if (ret < 0) {
2313 dev_err(&pdev->dev,
2314 "%s: Error in master Initialization , err %d\n",
2315 __func__, ret);
2316 mutex_unlock(&swrm->mlock);
2317 goto err_mstr_fail;
2318 }
2319 swrm->version = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
2320
2321 mutex_unlock(&swrm->mlock);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302322 INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302323
2324 if (pdev->dev.of_node)
2325 of_register_swr_devices(&swrm->master);
2326
2327 dbgswrm = swrm;
2328 debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
2329 if (!IS_ERR(debugfs_swrm_dent)) {
2330 debugfs_peek = debugfs_create_file("swrm_peek",
2331 S_IFREG | 0444, debugfs_swrm_dent,
2332 (void *) "swrm_peek", &swrm_debug_ops);
2333
2334 debugfs_poke = debugfs_create_file("swrm_poke",
2335 S_IFREG | 0444, debugfs_swrm_dent,
2336 (void *) "swrm_poke", &swrm_debug_ops);
2337
2338 debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
2339 S_IFREG | 0444, debugfs_swrm_dent,
2340 (void *) "swrm_reg_dump",
2341 &swrm_debug_ops);
2342 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302343
2344 ret = device_init_wakeup(swrm->dev, true);
2345 if (ret) {
2346 dev_err(swrm->dev, "Device wakeup init failed: %d\n", ret);
2347 goto err_irq_wakeup_fail;
2348 }
2349
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302350 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
2351 pm_runtime_use_autosuspend(&pdev->dev);
2352 pm_runtime_set_active(&pdev->dev);
2353 pm_runtime_enable(&pdev->dev);
2354 pm_runtime_mark_last_busy(&pdev->dev);
2355
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302356 INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn);
2357 swrm->event_notifier.notifier_call = swrm_event_notify;
2358 msm_aud_evt_register_client(&swrm->event_notifier);
2359
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302360 return 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302361err_irq_wakeup_fail:
2362 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302363err_mstr_fail:
2364 if (swrm->reg_irq)
2365 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2366 swrm, SWR_IRQ_FREE);
2367 else if (swrm->irq)
2368 free_irq(swrm->irq, swrm);
2369err_irq_fail:
2370 mutex_destroy(&swrm->mlock);
2371 mutex_destroy(&swrm->reslock);
2372 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302373 mutex_destroy(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302374 mutex_destroy(&swrm->clklock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302375 mutex_destroy(&swrm->pm_lock);
2376 pm_qos_remove_request(&swrm->pm_qos_req);
2377
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302378err_pdata_fail:
2379err_memory_fail:
2380 return ret;
2381}
2382
2383static int swrm_remove(struct platform_device *pdev)
2384{
2385 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2386
2387 if (swrm->reg_irq)
2388 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2389 swrm, SWR_IRQ_FREE);
2390 else if (swrm->irq)
2391 free_irq(swrm->irq, swrm);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302392 else if (swrm->wake_irq > 0)
2393 free_irq(swrm->wake_irq, swrm);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302394 if (swrm->swr_irq_wakeup_capable)
2395 irq_set_irq_wake(swrm->irq, 0);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302396 cancel_work_sync(&swrm->wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302397 pm_runtime_disable(&pdev->dev);
2398 pm_runtime_set_suspended(&pdev->dev);
2399 swr_unregister_master(&swrm->master);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302400 msm_aud_evt_unregister_client(&swrm->event_notifier);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302401 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302402 mutex_destroy(&swrm->mlock);
2403 mutex_destroy(&swrm->reslock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302404 mutex_destroy(&swrm->iolock);
2405 mutex_destroy(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302406 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302407 mutex_destroy(&swrm->pm_lock);
2408 pm_qos_remove_request(&swrm->pm_qos_req);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302409 devm_kfree(&pdev->dev, swrm);
2410 return 0;
2411}
2412
2413static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
2414{
2415 u32 val;
2416
2417 dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
2418 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD);
2419 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2420 val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK;
2421 swr_master_write(swrm, SWRM_MCP_CFG_ADDR, val);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302422
2423 return 0;
2424}
2425
2426#ifdef CONFIG_PM
2427static int swrm_runtime_resume(struct device *dev)
2428{
2429 struct platform_device *pdev = to_platform_device(dev);
2430 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2431 int ret = 0;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302432 bool hw_core_err = false;
2433 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302434 struct swr_master *mstr = &swrm->master;
2435 struct swr_device *swr_dev;
2436
2437 dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
2438 __func__, swrm->state);
2439 mutex_lock(&swrm->reslock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302440
Sudheer Papothi384addd2019-06-14 02:26:52 +05302441 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2442 dev_err(dev, "%s:lpass core hw enable failed\n",
2443 __func__);
2444 hw_core_err = true;
2445 }
2446 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2447 dev_err(dev, "%s:lpass audio hw enable failed\n",
2448 __func__);
2449 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002450 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302451
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302452 if ((swrm->state == SWR_MSTR_DOWN) ||
2453 (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) {
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302454 if (swrm->clk_stop_mode0_supp) {
2455 if (swrm->ipc_wakeup)
2456 msm_aud_evt_blocking_notifier_call_chain(
2457 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302458 }
2459
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302460 if (swrm_clk_request(swrm, true))
2461 goto exit;
2462 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302463 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2464 ret = swr_device_up(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302465 if (ret == -ENODEV) {
2466 dev_dbg(dev,
2467 "%s slave device up not implemented\n",
2468 __func__);
2469 ret = 0;
2470 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302471 dev_err(dev,
2472 "%s: failed to wakeup swr dev %d\n",
2473 __func__, swr_dev->dev_num);
2474 swrm_clk_request(swrm, false);
2475 goto exit;
2476 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302477 }
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05302478 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2479 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2480 swrm_master_init(swrm);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302481 swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, 0x0,
2482 SWRS_SCP_INT_STATUS_MASK_1);
Karthikeyan Manif6821902019-05-21 17:31:24 -07002483 if (swrm->state == SWR_MSTR_SSR) {
2484 mutex_unlock(&swrm->reslock);
2485 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
2486 mutex_lock(&swrm->reslock);
2487 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302488 } else {
2489 /*wake up from clock stop*/
2490 swr_master_write(swrm, SWRM_MCP_BUS_CTRL_ADDR, 0x2);
2491 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302492 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302493 swrm->state = SWR_MSTR_UP;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302494 }
2495exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302496 if (!aud_core_err)
2497 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2498 if (!hw_core_err)
2499 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302500 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
2501 mutex_unlock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302502
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302503 return ret;
2504}
2505
2506static int swrm_runtime_suspend(struct device *dev)
2507{
2508 struct platform_device *pdev = to_platform_device(dev);
2509 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2510 int ret = 0;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302511 bool hw_core_err = false;
2512 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302513 struct swr_master *mstr = &swrm->master;
2514 struct swr_device *swr_dev;
2515 int current_state = 0;
2516
2517 dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
2518 __func__, swrm->state);
2519 mutex_lock(&swrm->reslock);
2520 mutex_lock(&swrm->force_down_lock);
2521 current_state = swrm->state;
2522 mutex_unlock(&swrm->force_down_lock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302523
2524 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2525 dev_err(dev, "%s:lpass core hw enable failed\n",
2526 __func__);
2527 hw_core_err = true;
2528 }
2529 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2530 dev_err(dev, "%s:lpass audio hw enable failed\n",
2531 __func__);
2532 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002533 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302534
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302535 if ((current_state == SWR_MSTR_UP) ||
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302536 (current_state == SWR_MSTR_SSR)) {
2537
2538 if ((current_state != SWR_MSTR_SSR) &&
2539 swrm_is_port_en(&swrm->master)) {
2540 dev_dbg(dev, "%s ports are enabled\n", __func__);
2541 ret = -EBUSY;
2542 goto exit;
2543 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302544 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05302545 mutex_unlock(&swrm->reslock);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +05302546 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
Sudheer Papothi06f43412019-07-09 03:32:54 +05302547 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302548 swrm_clk_pause(swrm);
2549 swr_master_write(swrm, SWRM_COMP_CFG_ADDR, 0x00);
2550 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2551 ret = swr_device_down(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302552 if (ret == -ENODEV) {
2553 dev_dbg_ratelimited(dev,
2554 "%s slave device down not implemented\n",
2555 __func__);
2556 ret = 0;
2557 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302558 dev_err(dev,
2559 "%s: failed to shutdown swr dev %d\n",
2560 __func__, swr_dev->dev_num);
2561 goto exit;
2562 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302563 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302564 } else {
Sudheer Papothi384addd2019-06-14 02:26:52 +05302565 mutex_unlock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302566 /* clock stop sequence */
2567 swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
2568 SWRS_SCP_CONTROL);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302569 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302570 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302571 }
2572 swrm_clk_request(swrm, false);
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302573
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302574 if (swrm->clk_stop_mode0_supp) {
2575 if (swrm->wake_irq > 0) {
2576 enable_irq(swrm->wake_irq);
2577 } else if (swrm->ipc_wakeup) {
2578 msm_aud_evt_blocking_notifier_call_chain(
2579 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
2580 swrm->ipc_wakeup_triggered = false;
2581 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302582 }
2583
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302584 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302585 /* Retain SSR state until resume */
2586 if (current_state != SWR_MSTR_SSR)
2587 swrm->state = SWR_MSTR_DOWN;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302588exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302589 if (!aud_core_err)
2590 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2591 if (!hw_core_err)
2592 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302593 mutex_unlock(&swrm->reslock);
2594 return ret;
2595}
2596#endif /* CONFIG_PM */
2597
Sudheer Papothi06f43412019-07-09 03:32:54 +05302598static int swrm_device_suspend(struct device *dev)
2599{
2600 struct platform_device *pdev = to_platform_device(dev);
2601 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2602 int ret = 0;
2603
2604 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2605 if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
2606 ret = swrm_runtime_suspend(dev);
2607 if (!ret) {
2608 pm_runtime_disable(dev);
2609 pm_runtime_set_suspended(dev);
2610 pm_runtime_enable(dev);
2611 }
2612 }
2613
2614 return 0;
2615}
2616
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302617static int swrm_device_down(struct device *dev)
2618{
2619 struct platform_device *pdev = to_platform_device(dev);
2620 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2621 int ret = 0;
2622
2623 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2624
2625 mutex_lock(&swrm->force_down_lock);
2626 swrm->state = SWR_MSTR_SSR;
2627 mutex_unlock(&swrm->force_down_lock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302628 if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
2629 ret = swrm_runtime_suspend(dev);
2630 if (!ret) {
2631 pm_runtime_disable(dev);
2632 pm_runtime_set_suspended(dev);
2633 pm_runtime_enable(dev);
2634 }
2635 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302636
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302637 return 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302638}
2639
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302640int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm)
2641{
2642 int ret = 0;
Laxminath Kasama60239e2019-01-10 14:43:03 +05302643 int irq, dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302644
2645 if (!swrm->ipc_wakeup) {
Laxminath Kasama60239e2019-01-10 14:43:03 +05302646 irq = of_get_named_gpio(swrm->dev->of_node,
2647 "qcom,swr-wakeup-irq", 0);
2648 if (gpio_is_valid(irq)) {
2649 swrm->wake_irq = gpio_to_irq(irq);
2650 if (swrm->wake_irq < 0) {
2651 dev_err(swrm->dev,
2652 "Unable to configure irq\n");
2653 return swrm->wake_irq;
2654 }
2655 } else {
2656 dir_apps_irq = platform_get_irq_byname(swrm->pdev,
2657 "swr_wake_irq");
2658 if (dir_apps_irq < 0) {
2659 dev_err(swrm->dev,
2660 "TLMM connect gpio not found\n");
2661 return -EINVAL;
2662 }
2663 swrm->wake_irq = dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302664 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302665 ret = request_threaded_irq(swrm->wake_irq, NULL,
2666 swrm_wakeup_interrupt,
2667 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
2668 "swr_wake_irq", swrm);
2669 if (ret) {
2670 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2671 __func__, ret);
2672 return -EINVAL;
2673 }
Aditya Bavanari3517b112018-12-03 13:26:59 +05302674 irq_set_irq_wake(swrm->wake_irq, 1);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302675 }
2676 return ret;
2677}
2678
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302679static int swrm_alloc_port_mem(struct device *dev, struct swr_mstr_ctrl *swrm,
2680 u32 uc, u32 size)
2681{
2682 if (!swrm->port_param) {
2683 swrm->port_param = devm_kzalloc(dev,
2684 sizeof(swrm->port_param) * SWR_UC_MAX,
2685 GFP_KERNEL);
2686 if (!swrm->port_param)
2687 return -ENOMEM;
2688 }
2689 if (!swrm->port_param[uc]) {
2690 swrm->port_param[uc] = devm_kcalloc(dev, size,
2691 sizeof(struct port_params),
2692 GFP_KERNEL);
2693 if (!swrm->port_param[uc])
2694 return -ENOMEM;
2695 } else {
2696 dev_err_ratelimited(swrm->dev, "%s: called more than once\n",
2697 __func__);
2698 }
2699
2700 return 0;
2701}
2702
2703static int swrm_copy_port_config(struct swr_mstr_ctrl *swrm,
2704 struct swrm_port_config *port_cfg,
2705 u32 size)
2706{
2707 int idx;
2708 struct port_params *params;
2709 int uc = port_cfg->uc;
2710 int ret = 0;
2711
2712 for (idx = 0; idx < size; idx++) {
2713 params = &((struct port_params *)port_cfg->params)[idx];
2714 if (!params) {
2715 dev_err(swrm->dev, "%s: Invalid params\n", __func__);
2716 ret = -EINVAL;
2717 break;
2718 }
2719 memcpy(&swrm->port_param[uc][idx], params,
2720 sizeof(struct port_params));
2721 }
2722
2723 return ret;
2724}
2725
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302726/**
2727 * swrm_wcd_notify - parent device can notify to soundwire master through
2728 * this function
2729 * @pdev: pointer to platform device structure
2730 * @id: command id from parent to the soundwire master
2731 * @data: data from parent device to soundwire master
2732 */
2733int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
2734{
2735 struct swr_mstr_ctrl *swrm;
2736 int ret = 0;
2737 struct swr_master *mstr;
2738 struct swr_device *swr_dev;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302739 struct swrm_port_config *port_cfg;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302740
2741 if (!pdev) {
2742 pr_err("%s: pdev is NULL\n", __func__);
2743 return -EINVAL;
2744 }
2745 swrm = platform_get_drvdata(pdev);
2746 if (!swrm) {
2747 dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__);
2748 return -EINVAL;
2749 }
2750 mstr = &swrm->master;
2751
2752 switch (id) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05302753 case SWR_REQ_CLK_SWITCH:
2754 /* This will put soundwire in clock stop mode and disable the
2755 * clocks, if there is no active usecase running, so that the
2756 * next activity on soundwire will request clock from new clock
2757 * source.
2758 */
2759 mutex_lock(&swrm->mlock);
2760 if (swrm->state == SWR_MSTR_UP)
2761 swrm_device_suspend(&pdev->dev);
2762 mutex_unlock(&swrm->mlock);
2763 break;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302764 case SWR_CLK_FREQ:
2765 if (!data) {
2766 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
2767 ret = -EINVAL;
2768 } else {
2769 mutex_lock(&swrm->mlock);
2770 swrm->mclk_freq = *(int *)data;
2771 mutex_unlock(&swrm->mlock);
2772 }
2773 break;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302774 case SWR_DEVICE_SSR_DOWN:
2775 mutex_lock(&swrm->devlock);
2776 swrm->dev_up = false;
2777 mutex_unlock(&swrm->devlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302778 mutex_lock(&swrm->reslock);
2779 swrm->state = SWR_MSTR_SSR;
2780 mutex_unlock(&swrm->reslock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302781 break;
2782 case SWR_DEVICE_SSR_UP:
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302783 /* wait for clk voting to be zero */
Ramprasad Katkam7f6462e2018-11-06 11:51:22 +05302784 reinit_completion(&swrm->clk_off_complete);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302785 if (swrm->clk_ref_count &&
2786 !wait_for_completion_timeout(&swrm->clk_off_complete,
Ramprasad Katkamc87efeb2018-12-12 19:26:19 +05302787 msecs_to_jiffies(500)))
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302788 dev_err(swrm->dev, "%s: clock voting not zero\n",
2789 __func__);
2790
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302791 mutex_lock(&swrm->devlock);
2792 swrm->dev_up = true;
2793 mutex_unlock(&swrm->devlock);
2794 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302795 case SWR_DEVICE_DOWN:
2796 dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
2797 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302798 if (swrm->state == SWR_MSTR_DOWN)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302799 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
2800 __func__, swrm->state);
2801 else
2802 swrm_device_down(&pdev->dev);
2803 mutex_unlock(&swrm->mlock);
2804 break;
2805 case SWR_DEVICE_UP:
2806 dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
Ramprasad Katkam0fed92f2018-11-08 14:22:22 +05302807 mutex_lock(&swrm->devlock);
2808 if (!swrm->dev_up) {
2809 dev_dbg(swrm->dev, "SSR not complete yet\n");
2810 mutex_unlock(&swrm->devlock);
2811 return -EBUSY;
2812 }
2813 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302814 mutex_lock(&swrm->mlock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302815 pm_runtime_mark_last_busy(&pdev->dev);
2816 pm_runtime_get_sync(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302817 mutex_lock(&swrm->reslock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302818 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2819 ret = swr_reset_device(swr_dev);
2820 if (ret) {
2821 dev_err(swrm->dev,
2822 "%s: failed to reset swr device %d\n",
2823 __func__, swr_dev->dev_num);
2824 swrm_clk_request(swrm, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302825 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302826 }
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302827 pm_runtime_mark_last_busy(&pdev->dev);
2828 pm_runtime_put_autosuspend(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302829 mutex_unlock(&swrm->reslock);
2830 mutex_unlock(&swrm->mlock);
2831 break;
2832 case SWR_SET_NUM_RX_CH:
2833 if (!data) {
2834 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
2835 ret = -EINVAL;
2836 } else {
2837 mutex_lock(&swrm->mlock);
2838 swrm->num_rx_chs = *(int *)data;
2839 if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
2840 list_for_each_entry(swr_dev, &mstr->devices,
2841 dev_list) {
2842 ret = swr_set_device_group(swr_dev,
2843 SWR_BROADCAST);
2844 if (ret)
2845 dev_err(swrm->dev,
2846 "%s: set num ch failed\n",
2847 __func__);
2848 }
2849 } else {
2850 list_for_each_entry(swr_dev, &mstr->devices,
2851 dev_list) {
2852 ret = swr_set_device_group(swr_dev,
2853 SWR_GROUP_NONE);
2854 if (ret)
2855 dev_err(swrm->dev,
2856 "%s: set num ch failed\n",
2857 __func__);
2858 }
2859 }
2860 mutex_unlock(&swrm->mlock);
2861 }
2862 break;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302863 case SWR_REGISTER_WAKE_IRQ:
2864 if (!data) {
2865 dev_err(swrm->dev, "%s: reg wake irq data is NULL\n",
2866 __func__);
2867 ret = -EINVAL;
2868 } else {
2869 mutex_lock(&swrm->mlock);
2870 swrm->ipc_wakeup = *(u32 *)data;
2871 ret = swrm_register_wake_irq(swrm);
2872 if (ret)
2873 dev_err(swrm->dev, "%s: register wake_irq failed\n",
2874 __func__);
2875 mutex_unlock(&swrm->mlock);
2876 }
2877 break;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302878 case SWR_SET_PORT_MAP:
2879 if (!data) {
2880 dev_err(swrm->dev, "%s: data is NULL for id=%d\n",
2881 __func__, id);
2882 ret = -EINVAL;
2883 } else {
2884 mutex_lock(&swrm->mlock);
2885 port_cfg = (struct swrm_port_config *)data;
2886 if (!port_cfg->size) {
2887 ret = -EINVAL;
2888 goto done;
2889 }
2890 ret = swrm_alloc_port_mem(&pdev->dev, swrm,
2891 port_cfg->uc, port_cfg->size);
2892 if (!ret)
2893 swrm_copy_port_config(swrm, port_cfg,
2894 port_cfg->size);
2895done:
2896 mutex_unlock(&swrm->mlock);
2897 }
2898 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302899 default:
2900 dev_err(swrm->dev, "%s: swr master unknown id %d\n",
2901 __func__, id);
2902 break;
2903 }
2904 return ret;
2905}
2906EXPORT_SYMBOL(swrm_wcd_notify);
2907
Ramprasad Katkam57349872018-11-11 18:34:57 +05302908/*
2909 * swrm_pm_cmpxchg:
2910 * Check old state and exchange with pm new state
2911 * if old state matches with current state
2912 *
2913 * @swrm: pointer to wcd core resource
2914 * @o: pm old state
2915 * @n: pm new state
2916 *
2917 * Returns old state
2918 */
2919static enum swrm_pm_state swrm_pm_cmpxchg(
2920 struct swr_mstr_ctrl *swrm,
2921 enum swrm_pm_state o,
2922 enum swrm_pm_state n)
2923{
2924 enum swrm_pm_state old;
2925
2926 if (!swrm)
2927 return o;
2928
2929 mutex_lock(&swrm->pm_lock);
2930 old = swrm->pm_state;
2931 if (old == o)
2932 swrm->pm_state = n;
2933 mutex_unlock(&swrm->pm_lock);
2934
2935 return old;
2936}
2937
2938static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm)
2939{
2940 enum swrm_pm_state os;
2941
2942 /*
2943 * swrm_{lock/unlock}_sleep will be called by swr irq handler
2944 * and slave wake up requests..
2945 *
2946 * If system didn't resume, we can simply return false so
2947 * IRQ handler can return without handling IRQ.
2948 */
2949 mutex_lock(&swrm->pm_lock);
2950 if (swrm->wlock_holders++ == 0) {
2951 dev_dbg(swrm->dev, "%s: holding wake lock\n", __func__);
2952 pm_qos_update_request(&swrm->pm_qos_req,
2953 msm_cpuidle_get_deep_idle_latency());
2954 pm_stay_awake(swrm->dev);
2955 }
2956 mutex_unlock(&swrm->pm_lock);
2957
2958 if (!wait_event_timeout(swrm->pm_wq,
2959 ((os = swrm_pm_cmpxchg(swrm,
2960 SWRM_PM_SLEEPABLE,
2961 SWRM_PM_AWAKE)) ==
2962 SWRM_PM_SLEEPABLE ||
2963 (os == SWRM_PM_AWAKE)),
2964 msecs_to_jiffies(
2965 SWRM_SYSTEM_RESUME_TIMEOUT_MS))) {
2966 dev_err(swrm->dev, "%s: system didn't resume within %dms, s %d, w %d\n",
2967 __func__, SWRM_SYSTEM_RESUME_TIMEOUT_MS, swrm->pm_state,
2968 swrm->wlock_holders);
2969 swrm_unlock_sleep(swrm);
2970 return false;
2971 }
2972 wake_up_all(&swrm->pm_wq);
2973 return true;
2974}
2975
2976static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm)
2977{
2978 mutex_lock(&swrm->pm_lock);
2979 if (--swrm->wlock_holders == 0) {
2980 dev_dbg(swrm->dev, "%s: releasing wake lock pm_state %d -> %d\n",
2981 __func__, swrm->pm_state, SWRM_PM_SLEEPABLE);
2982 /*
2983 * if swrm_lock_sleep failed, pm_state would be still
2984 * swrm_PM_ASLEEP, don't overwrite
2985 */
2986 if (likely(swrm->pm_state == SWRM_PM_AWAKE))
2987 swrm->pm_state = SWRM_PM_SLEEPABLE;
2988 pm_qos_update_request(&swrm->pm_qos_req,
2989 PM_QOS_DEFAULT_VALUE);
2990 pm_relax(swrm->dev);
2991 }
2992 mutex_unlock(&swrm->pm_lock);
2993 wake_up_all(&swrm->pm_wq);
2994}
2995
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302996#ifdef CONFIG_PM_SLEEP
2997static int swrm_suspend(struct device *dev)
2998{
2999 int ret = -EBUSY;
3000 struct platform_device *pdev = to_platform_device(dev);
3001 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3002
3003 dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
Ramprasad Katkam57349872018-11-11 18:34:57 +05303004
3005 mutex_lock(&swrm->pm_lock);
3006
3007 if (swrm->pm_state == SWRM_PM_SLEEPABLE) {
3008 dev_dbg(swrm->dev, "%s: suspending system, state %d, wlock %d\n",
3009 __func__, swrm->pm_state,
3010 swrm->wlock_holders);
3011 swrm->pm_state = SWRM_PM_ASLEEP;
3012 } else if (swrm->pm_state == SWRM_PM_AWAKE) {
3013 /*
3014 * unlock to wait for pm_state == SWRM_PM_SLEEPABLE
3015 * then set to SWRM_PM_ASLEEP
3016 */
3017 dev_dbg(swrm->dev, "%s: waiting to suspend system, state %d, wlock %d\n",
3018 __func__, swrm->pm_state,
3019 swrm->wlock_holders);
3020 mutex_unlock(&swrm->pm_lock);
3021 if (!(wait_event_timeout(swrm->pm_wq, swrm_pm_cmpxchg(
3022 swrm, SWRM_PM_SLEEPABLE,
3023 SWRM_PM_ASLEEP) ==
3024 SWRM_PM_SLEEPABLE,
3025 msecs_to_jiffies(
3026 SWRM_SYS_SUSPEND_WAIT)))) {
3027 dev_dbg(swrm->dev, "%s: suspend failed state %d, wlock %d\n",
3028 __func__, swrm->pm_state,
3029 swrm->wlock_holders);
3030 return -EBUSY;
3031 } else {
3032 dev_dbg(swrm->dev,
3033 "%s: done, state %d, wlock %d\n",
3034 __func__, swrm->pm_state,
3035 swrm->wlock_holders);
3036 }
3037 mutex_lock(&swrm->pm_lock);
3038 } else if (swrm->pm_state == SWRM_PM_ASLEEP) {
3039 dev_dbg(swrm->dev, "%s: system is already suspended, state %d, wlock %d\n",
3040 __func__, swrm->pm_state,
3041 swrm->wlock_holders);
3042 }
3043
3044 mutex_unlock(&swrm->pm_lock);
3045
3046 if ((!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev))) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303047 ret = swrm_runtime_suspend(dev);
3048 if (!ret) {
3049 /*
3050 * Synchronize runtime-pm and system-pm states:
3051 * At this point, we are already suspended. If
3052 * runtime-pm still thinks its active, then
3053 * make sure its status is in sync with HW
3054 * status. The three below calls let the
3055 * runtime-pm know that we are suspended
3056 * already without re-invoking the suspend
3057 * callback
3058 */
3059 pm_runtime_disable(dev);
3060 pm_runtime_set_suspended(dev);
3061 pm_runtime_enable(dev);
3062 }
3063 }
3064 if (ret == -EBUSY) {
3065 /*
3066 * There is a possibility that some audio stream is active
3067 * during suspend. We dont want to return suspend failure in
3068 * that case so that display and relevant components can still
3069 * go to suspend.
3070 * If there is some other error, then it should be passed-on
3071 * to system level suspend
3072 */
3073 ret = 0;
3074 }
3075 return ret;
3076}
3077
3078static int swrm_resume(struct device *dev)
3079{
3080 int ret = 0;
3081 struct platform_device *pdev = to_platform_device(dev);
3082 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3083
3084 dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
3085 if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
3086 ret = swrm_runtime_resume(dev);
3087 if (!ret) {
3088 pm_runtime_mark_last_busy(dev);
3089 pm_request_autosuspend(dev);
3090 }
3091 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05303092 mutex_lock(&swrm->pm_lock);
3093 if (swrm->pm_state == SWRM_PM_ASLEEP) {
3094 dev_dbg(swrm->dev,
3095 "%s: resuming system, state %d, wlock %d\n",
3096 __func__, swrm->pm_state,
3097 swrm->wlock_holders);
3098 swrm->pm_state = SWRM_PM_SLEEPABLE;
3099 } else {
3100 dev_dbg(swrm->dev, "%s: system is already awake, state %d wlock %d\n",
3101 __func__, swrm->pm_state,
3102 swrm->wlock_holders);
3103 }
3104 mutex_unlock(&swrm->pm_lock);
3105 wake_up_all(&swrm->pm_wq);
3106
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303107 return ret;
3108}
3109#endif /* CONFIG_PM_SLEEP */
3110
3111static const struct dev_pm_ops swrm_dev_pm_ops = {
3112 SET_SYSTEM_SLEEP_PM_OPS(
3113 swrm_suspend,
3114 swrm_resume
3115 )
3116 SET_RUNTIME_PM_OPS(
3117 swrm_runtime_suspend,
3118 swrm_runtime_resume,
3119 NULL
3120 )
3121};
3122
3123static const struct of_device_id swrm_dt_match[] = {
3124 {
3125 .compatible = "qcom,swr-mstr",
3126 },
3127 {}
3128};
3129
3130static struct platform_driver swr_mstr_driver = {
3131 .probe = swrm_probe,
3132 .remove = swrm_remove,
3133 .driver = {
3134 .name = SWR_WCD_NAME,
3135 .owner = THIS_MODULE,
3136 .pm = &swrm_dev_pm_ops,
3137 .of_match_table = swrm_dt_match,
Xiaojun Sang53cd13a2018-06-29 15:14:37 +08003138 .suppress_bind_attrs = true,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303139 },
3140};
3141
3142static int __init swrm_init(void)
3143{
3144 return platform_driver_register(&swr_mstr_driver);
3145}
3146module_init(swrm_init);
3147
3148static void __exit swrm_exit(void)
3149{
3150 platform_driver_unregister(&swr_mstr_driver);
3151}
3152module_exit(swrm_exit);
3153
3154MODULE_LICENSE("GPL v2");
3155MODULE_DESCRIPTION("SoundWire Master Controller");
3156MODULE_ALIAS("platform:swr-mstr");