blob: 722501ab1f25cf3b35cba30c2208c34482564af8 [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
36#define SWR_AUTO_SUSPEND_DELAY 3 /* 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};
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053068#define TRUE 1
69#define FALSE 0
70
Ramprasad Katkam1f221262018-08-23 15:01:22 +053071#define SWRM_MAX_PORT_REG 120
Ramprasad Katkam83303512018-10-11 17:34:22 +053072#define SWRM_MAX_INIT_REG 11
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053073
74#define SWR_MSTR_MAX_REG_ADDR 0x1740
75#define SWR_MSTR_START_REG_ADDR 0x00
76#define SWR_MSTR_MAX_BUF_LEN 32
77#define BYTES_PER_LINE 12
78#define SWR_MSTR_RD_BUF_LEN 8
79#define SWR_MSTR_WR_BUF_LEN 32
80
Laxminath Kasamfbcaf322018-07-18 00:38:14 +053081#define MAX_FIFO_RD_FAIL_RETRY 3
82
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053083static struct swr_mstr_ctrl *dbgswrm;
84static struct dentry *debugfs_swrm_dent;
85static struct dentry *debugfs_peek;
86static struct dentry *debugfs_poke;
87static struct dentry *debugfs_reg_dump;
88static unsigned int read_data;
89
Ramprasad Katkam57349872018-11-11 18:34:57 +053090static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm);
91static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053092
93static bool swrm_is_msm_variant(int val)
94{
95 return (val == SWRM_VERSION_1_3);
96}
97
98static int swrm_debug_open(struct inode *inode, struct file *file)
99{
100 file->private_data = inode->i_private;
101 return 0;
102}
103
104static int get_parameters(char *buf, u32 *param1, int num_of_par)
105{
106 char *token;
107 int base, cnt;
108
109 token = strsep(&buf, " ");
110 for (cnt = 0; cnt < num_of_par; cnt++) {
111 if (token) {
112 if ((token[1] == 'x') || (token[1] == 'X'))
113 base = 16;
114 else
115 base = 10;
116
117 if (kstrtou32(token, base, &param1[cnt]) != 0)
118 return -EINVAL;
119
120 token = strsep(&buf, " ");
121 } else
122 return -EINVAL;
123 }
124 return 0;
125}
126
127static ssize_t swrm_reg_show(char __user *ubuf, size_t count,
128 loff_t *ppos)
129{
130 int i, reg_val, len;
131 ssize_t total = 0;
132 char tmp_buf[SWR_MSTR_MAX_BUF_LEN];
133
134 if (!ubuf || !ppos)
135 return 0;
136
137 for (i = (((int) *ppos / BYTES_PER_LINE) + SWR_MSTR_START_REG_ADDR);
138 i <= SWR_MSTR_MAX_REG_ADDR; i += 4) {
139 reg_val = dbgswrm->read(dbgswrm->handle, i);
140 len = snprintf(tmp_buf, 25, "0x%.3x: 0x%.2x\n", i, reg_val);
141 if ((total + len) >= count - 1)
142 break;
143 if (copy_to_user((ubuf + total), tmp_buf, len)) {
144 pr_err("%s: fail to copy reg dump\n", __func__);
145 total = -EFAULT;
146 goto copy_err;
147 }
148 *ppos += len;
149 total += len;
150 }
151
152copy_err:
153 return total;
154}
155
156static ssize_t swrm_debug_read(struct file *file, char __user *ubuf,
157 size_t count, loff_t *ppos)
158{
159 char lbuf[SWR_MSTR_RD_BUF_LEN];
160 char *access_str;
161 ssize_t ret_cnt;
162
163 if (!count || !file || !ppos || !ubuf)
164 return -EINVAL;
165
166 access_str = file->private_data;
167 if (*ppos < 0)
168 return -EINVAL;
169
170 if (!strcmp(access_str, "swrm_peek")) {
171 snprintf(lbuf, sizeof(lbuf), "0x%x\n", read_data);
172 ret_cnt = simple_read_from_buffer(ubuf, count, ppos, lbuf,
173 strnlen(lbuf, 7));
174 } else if (!strcmp(access_str, "swrm_reg_dump")) {
175 ret_cnt = swrm_reg_show(ubuf, count, ppos);
176 } else {
177 pr_err("%s: %s not permitted to read\n", __func__, access_str);
178 ret_cnt = -EPERM;
179 }
180 return ret_cnt;
181}
182
183static ssize_t swrm_debug_write(struct file *filp,
184 const char __user *ubuf, size_t cnt, loff_t *ppos)
185{
186 char lbuf[SWR_MSTR_WR_BUF_LEN];
187 int rc;
188 u32 param[5];
189 char *access_str;
190
191 if (!filp || !ppos || !ubuf)
192 return -EINVAL;
193
194 access_str = filp->private_data;
195 if (cnt > sizeof(lbuf) - 1)
196 return -EINVAL;
197
198 rc = copy_from_user(lbuf, ubuf, cnt);
199 if (rc)
200 return -EFAULT;
201
202 lbuf[cnt] = '\0';
203 if (!strcmp(access_str, "swrm_poke")) {
204 /* write */
205 rc = get_parameters(lbuf, param, 2);
206 if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) &&
207 (param[1] <= 0xFFFFFFFF) &&
208 (rc == 0))
209 rc = dbgswrm->write(dbgswrm->handle, param[0],
210 param[1]);
211 else
212 rc = -EINVAL;
213 } else if (!strcmp(access_str, "swrm_peek")) {
214 /* read */
215 rc = get_parameters(lbuf, param, 1);
216 if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) && (rc == 0))
217 read_data = dbgswrm->read(dbgswrm->handle, param[0]);
218 else
219 rc = -EINVAL;
220 }
221 if (rc == 0)
222 rc = cnt;
223 else
224 pr_err("%s: rc = %d\n", __func__, rc);
225
226 return rc;
227}
228
229static const struct file_operations swrm_debug_ops = {
230 .open = swrm_debug_open,
231 .write = swrm_debug_write,
232 .read = swrm_debug_read,
233};
234
235static int swrm_clk_request(struct swr_mstr_ctrl *swrm, bool enable)
236{
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530237 int ret = 0;
238
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530239 if (!swrm->clk || !swrm->handle)
240 return -EINVAL;
241
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530242 mutex_lock(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530243 if (enable) {
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530244 if (!swrm->dev_up)
245 goto exit;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530246 swrm->clk_ref_count++;
247 if (swrm->clk_ref_count == 1) {
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530248 ret = swrm->clk(swrm->handle, true);
249 if (ret) {
250 dev_err(swrm->dev,
251 "%s: clock enable req failed",
252 __func__);
253 --swrm->clk_ref_count;
254 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530255 }
256 } else if (--swrm->clk_ref_count == 0) {
257 swrm->clk(swrm->handle, false);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530258 complete(&swrm->clk_off_complete);
259 }
260 if (swrm->clk_ref_count < 0) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530261 pr_err("%s: swrm clk count mismatch\n", __func__);
262 swrm->clk_ref_count = 0;
263 }
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530264
265exit:
266 mutex_unlock(&swrm->clklock);
267 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530268}
269
270static int swrm_ahb_write(struct swr_mstr_ctrl *swrm,
271 u16 reg, u32 *value)
272{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530273 u32 temp = (u32)(*value);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530274 int ret = 0;
275
276 mutex_lock(&swrm->devlock);
277 if (!swrm->dev_up)
278 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530279
280 ret = swrm_clk_request(swrm, TRUE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530281 if (ret) {
282 dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
283 __func__);
284 goto err;
285 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530286 iowrite32(temp, swrm->swrm_dig_base + reg);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530287 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530288err:
289 mutex_unlock(&swrm->devlock);
290 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530291}
292
293static int swrm_ahb_read(struct swr_mstr_ctrl *swrm,
294 u16 reg, u32 *value)
295{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530296 u32 temp = 0;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530297 int ret = 0;
298
299 mutex_lock(&swrm->devlock);
300 if (!swrm->dev_up)
301 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530302
303 ret = swrm_clk_request(swrm, TRUE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530304 if (ret) {
305 dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
306 __func__);
307 goto err;
308 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530309 temp = ioread32(swrm->swrm_dig_base + reg);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530310 *value = temp;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530311 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530312err:
313 mutex_unlock(&swrm->devlock);
314 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530315}
316
317static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr)
318{
319 u32 val = 0;
320
321 if (swrm->read)
322 val = swrm->read(swrm->handle, reg_addr);
323 else
324 swrm_ahb_read(swrm, reg_addr, &val);
325 return val;
326}
327
328static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val)
329{
330 if (swrm->write)
331 swrm->write(swrm->handle, reg_addr, val);
332 else
333 swrm_ahb_write(swrm, reg_addr, &val);
334}
335
336static int swr_master_bulk_write(struct swr_mstr_ctrl *swrm, u32 *reg_addr,
337 u32 *val, unsigned int length)
338{
339 int i = 0;
340
341 if (swrm->bulk_write)
342 swrm->bulk_write(swrm->handle, reg_addr, val, length);
343 else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530344 mutex_lock(&swrm->iolock);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530345 for (i = 0; i < length; i++) {
346 /* wait for FIFO WR command to complete to avoid overflow */
347 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530348 swr_master_write(swrm, reg_addr[i], val[i]);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530349 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530350 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530351 }
352 return 0;
353}
354
355static bool swrm_is_port_en(struct swr_master *mstr)
356{
357 return !!(mstr->num_port);
358}
359
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530360static void copy_port_tables(struct swr_mstr_ctrl *swrm,
361 struct port_params *params)
362{
363 u8 i;
364 struct port_params *config = params;
365
366 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
367 /* wsa uses single frame structure for all configurations */
368 if (!swrm->mport_cfg[i].port_en)
369 continue;
370 swrm->mport_cfg[i].sinterval = config[i].si;
371 swrm->mport_cfg[i].offset1 = config[i].off1;
372 swrm->mport_cfg[i].offset2 = config[i].off2;
373 swrm->mport_cfg[i].hstart = config[i].hstart;
374 swrm->mport_cfg[i].hstop = config[i].hstop;
375 swrm->mport_cfg[i].blk_pack_mode = config[i].bp_mode;
376 swrm->mport_cfg[i].blk_grp_count = config[i].bgp_ctrl;
377 swrm->mport_cfg[i].word_length = config[i].wd_len;
378 swrm->mport_cfg[i].lane_ctrl = config[i].lane_ctrl;
379 }
380}
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530381static int swrm_get_port_config(struct swr_mstr_ctrl *swrm)
382{
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530383 struct port_params *params;
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530384 u32 usecase = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530385
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530386 /* TODO - Send usecase information to avoid checking for master_id */
387 if (swrm->mport_cfg[SWRM_DSD_PARAMS_PORT].port_en &&
388 (swrm->master_id == MASTER_ID_RX))
389 usecase = 1;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530390
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530391 params = swrm->port_param[usecase];
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530392 copy_port_tables(swrm, params);
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530393
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530394 return 0;
395}
396
397static int swrm_get_master_port(struct swr_mstr_ctrl *swrm, u8 *mstr_port_id,
398 u8 *mstr_ch_mask, u8 mstr_prt_type,
399 u8 slv_port_id)
400{
401 int i, j;
402 *mstr_port_id = 0;
403
404 for (i = 1; i <= swrm->num_ports; i++) {
405 for (j = 0; j < SWR_MAX_CH_PER_PORT; j++) {
406 if (swrm->port_mapping[i][j].port_type == mstr_prt_type)
407 goto found;
408 }
409 }
410found:
411 if (i > swrm->num_ports || j == SWR_MAX_CH_PER_PORT) {
412 dev_err(swrm->dev, "%s: port type not supported by master\n",
413 __func__);
414 return -EINVAL;
415 }
416 /* id 0 corresponds to master port 1 */
417 *mstr_port_id = i - 1;
418 *mstr_ch_mask = swrm->port_mapping[i][j].ch_mask;
419
420 return 0;
421
422}
423
424static u32 swrm_get_packed_reg_val(u8 *cmd_id, u8 cmd_data,
425 u8 dev_addr, u16 reg_addr)
426{
427 u32 val;
428 u8 id = *cmd_id;
429
430 if (id != SWR_BROADCAST_CMD_ID) {
431 if (id < 14)
432 id += 1;
433 else
434 id = 0;
435 *cmd_id = id;
436 }
437 val = SWR_REG_VAL_PACK(cmd_data, dev_addr, id, reg_addr);
438
439 return val;
440}
441
442static int swrm_cmd_fifo_rd_cmd(struct swr_mstr_ctrl *swrm, int *cmd_data,
443 u8 dev_addr, u8 cmd_id, u16 reg_addr,
444 u32 len)
445{
446 u32 val;
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530447 u32 retry_attempt = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530448
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530449 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530450 val = swrm_get_packed_reg_val(&swrm->rcmd_id, len, dev_addr, reg_addr);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530451 if (swrm->read) {
452 /* skip delay if read is handled in platform driver */
453 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
454 } else {
455 /* wait for FIFO RD to complete to avoid overflow */
456 usleep_range(100, 105);
457 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
458 /* wait for FIFO RD CMD complete to avoid overflow */
459 usleep_range(250, 255);
460 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530461retry_read:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530462 *cmd_data = swr_master_read(swrm, SWRM_CMD_FIFO_RD_FIFO_ADDR);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530463 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, rcmd_id: 0x%x, \
464 dev_num: 0x%x, cmd_data: 0x%x\n", __func__, reg_addr,
465 cmd_id, swrm->rcmd_id, dev_addr, *cmd_data);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530466 if ((((*cmd_data) & 0xF00) >> 8) != swrm->rcmd_id) {
467 if (retry_attempt < MAX_FIFO_RD_FAIL_RETRY) {
468 /* wait 500 us before retry on fifo read failure */
469 usleep_range(500, 505);
470 retry_attempt++;
471 goto retry_read;
472 } else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530473 dev_err_ratelimited(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, \
474 rcmd_id: 0x%x, dev_num: 0x%x, cmd_data: 0x%x\n",
475 __func__, reg_addr, cmd_id, swrm->rcmd_id,
476 dev_addr, *cmd_data);
477
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530478 dev_err_ratelimited(swrm->dev,
479 "%s: failed to read fifo\n", __func__);
480 }
481 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530482 mutex_unlock(&swrm->iolock);
483
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530484 return 0;
485}
486
487static int swrm_cmd_fifo_wr_cmd(struct swr_mstr_ctrl *swrm, u8 cmd_data,
488 u8 dev_addr, u8 cmd_id, u16 reg_addr)
489{
490 u32 val;
491 int ret = 0;
492
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530493 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530494 if (!cmd_id)
495 val = swrm_get_packed_reg_val(&swrm->wcmd_id, cmd_data,
496 dev_addr, reg_addr);
497 else
498 val = swrm_get_packed_reg_val(&cmd_id, cmd_data,
499 dev_addr, reg_addr);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530500 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x,wcmd_id: 0x%x, \
501 dev_num: 0x%x, cmd_data: 0x%x\n", __func__,
502 reg_addr, cmd_id, swrm->wcmd_id,dev_addr, cmd_data);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +0530503 swr_master_write(swrm, SWRM_CMD_FIFO_WR_CMD, val);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530504 /*
505 * wait for FIFO WR command to complete to avoid overflow
506 * skip delay if write is handled in platform driver.
507 */
508 if(!swrm->write)
509 usleep_range(250, 255);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530510 if (cmd_id == 0xF) {
511 /*
512 * sleep for 10ms for MSM soundwire variant to allow broadcast
513 * command to complete.
514 */
515 if (swrm_is_msm_variant(swrm->version))
516 usleep_range(10000, 10100);
517 else
518 wait_for_completion_timeout(&swrm->broadcast,
519 (2 * HZ/10));
520 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530521 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530522 return ret;
523}
524
525static int swrm_read(struct swr_master *master, u8 dev_num, u16 reg_addr,
526 void *buf, u32 len)
527{
528 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
529 int ret = 0;
530 int val;
531 u8 *reg_val = (u8 *)buf;
532
533 if (!swrm) {
534 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
535 return -EINVAL;
536 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530537 if (!dev_num) {
538 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
539 return -EINVAL;
540 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530541 mutex_lock(&swrm->devlock);
542 if (!swrm->dev_up) {
543 mutex_unlock(&swrm->devlock);
544 return 0;
545 }
546 mutex_unlock(&swrm->devlock);
547
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530548 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530549 ret = swrm_cmd_fifo_rd_cmd(swrm, &val, dev_num, 0, reg_addr, len);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530550
551 if (!ret)
552 *reg_val = (u8)val;
553
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530554 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530555 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530556 return ret;
557}
558
559static int swrm_write(struct swr_master *master, u8 dev_num, u16 reg_addr,
560 const void *buf)
561{
562 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
563 int ret = 0;
564 u8 reg_val = *(u8 *)buf;
565
566 if (!swrm) {
567 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
568 return -EINVAL;
569 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530570 if (!dev_num) {
571 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
572 return -EINVAL;
573 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530574 mutex_lock(&swrm->devlock);
575 if (!swrm->dev_up) {
576 mutex_unlock(&swrm->devlock);
577 return 0;
578 }
579 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530580
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530581 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530582 ret = swrm_cmd_fifo_wr_cmd(swrm, reg_val, dev_num, 0, reg_addr);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530583
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530584 pm_runtime_put_autosuspend(swrm->dev);
585 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530586 return ret;
587}
588
589static int swrm_bulk_write(struct swr_master *master, u8 dev_num, void *reg,
590 const void *buf, size_t len)
591{
592 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
593 int ret = 0;
594 int i;
595 u32 *val;
596 u32 *swr_fifo_reg;
597
598 if (!swrm || !swrm->handle) {
599 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
600 return -EINVAL;
601 }
602 if (len <= 0)
603 return -EINVAL;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530604 mutex_lock(&swrm->devlock);
605 if (!swrm->dev_up) {
606 mutex_unlock(&swrm->devlock);
607 return 0;
608 }
609 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530610
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530611 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530612 if (dev_num) {
613 swr_fifo_reg = kcalloc(len, sizeof(u32), GFP_KERNEL);
614 if (!swr_fifo_reg) {
615 ret = -ENOMEM;
616 goto err;
617 }
618 val = kcalloc(len, sizeof(u32), GFP_KERNEL);
619 if (!val) {
620 ret = -ENOMEM;
621 goto mem_fail;
622 }
623
624 for (i = 0; i < len; i++) {
625 val[i] = swrm_get_packed_reg_val(&swrm->wcmd_id,
626 ((u8 *)buf)[i],
627 dev_num,
628 ((u16 *)reg)[i]);
629 swr_fifo_reg[i] = SWRM_CMD_FIFO_WR_CMD;
630 }
631 ret = swr_master_bulk_write(swrm, swr_fifo_reg, val, len);
632 if (ret) {
633 dev_err(&master->dev, "%s: bulk write failed\n",
634 __func__);
635 ret = -EINVAL;
636 }
637 } else {
638 dev_err(&master->dev,
639 "%s: No support of Bulk write for master regs\n",
640 __func__);
641 ret = -EINVAL;
642 goto err;
643 }
644 kfree(val);
645mem_fail:
646 kfree(swr_fifo_reg);
647err:
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530648 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530649 pm_runtime_mark_last_busy(swrm->dev);
650 return ret;
651}
652
653static u8 get_inactive_bank_num(struct swr_mstr_ctrl *swrm)
654{
655 return (swr_master_read(swrm, SWRM_MCP_STATUS) &
656 SWRM_MCP_STATUS_BANK_NUM_MASK) ? 0 : 1;
657}
658
659static void enable_bank_switch(struct swr_mstr_ctrl *swrm, u8 bank,
660 u8 row, u8 col)
661{
662 swrm_cmd_fifo_wr_cmd(swrm, ((row << 3) | col), 0xF, 0xF,
663 SWRS_SCP_FRAME_CTRL_BANK(bank));
664}
665
666static struct swr_port_info *swrm_get_port_req(struct swrm_mports *mport,
667 u8 slv_port, u8 dev_num)
668{
669 struct swr_port_info *port_req = NULL;
670
671 list_for_each_entry(port_req, &mport->port_req_list, list) {
672 /* Store dev_id instead of dev_num if enumeration is changed run_time */
673 if ((port_req->slave_port_id == slv_port)
674 && (port_req->dev_num == dev_num))
675 return port_req;
676 }
677 return NULL;
678}
679
680static bool swrm_remove_from_group(struct swr_master *master)
681{
682 struct swr_device *swr_dev;
683 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
684 bool is_removed = false;
685
686 if (!swrm)
687 goto end;
688
689 mutex_lock(&swrm->mlock);
690 if ((swrm->num_rx_chs > 1) &&
691 (swrm->num_rx_chs == swrm->num_cfg_devs)) {
692 list_for_each_entry(swr_dev, &master->devices,
693 dev_list) {
694 swr_dev->group_id = SWR_GROUP_NONE;
695 master->gr_sid = 0;
696 }
697 is_removed = true;
698 }
699 mutex_unlock(&swrm->mlock);
700
701end:
702 return is_removed;
703}
704
705static void swrm_disable_ports(struct swr_master *master,
706 u8 bank)
707{
708 u32 value;
709 struct swr_port_info *port_req;
710 int i;
711 struct swrm_mports *mport;
712 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
713
714 if (!swrm) {
715 pr_err("%s: swrm is null\n", __func__);
716 return;
717 }
718
719 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
720 master->num_port);
721
722
723 for (i = 0; i < SWR_MSTR_PORT_LEN ; i++) {
724
725 mport = &(swrm->mport_cfg[i]);
726 if (!mport->port_en)
727 continue;
728
729 list_for_each_entry(port_req, &mport->port_req_list, list) {
730 /* skip ports with no change req's*/
731 if (port_req->req_ch == port_req->ch_en)
732 continue;
733
734 swrm_cmd_fifo_wr_cmd(swrm, port_req->req_ch,
735 port_req->dev_num, 0x00,
736 SWRS_DP_CHANNEL_ENABLE_BANK(port_req->slave_port_id,
737 bank));
738 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x\n",
739 __func__, i,
740 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)));
741 }
742 value = ((mport->req_ch)
743 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
744 value |= ((mport->offset2)
745 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
746 value |= ((mport->offset1)
747 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
748 value |= mport->sinterval;
749
750 swr_master_write(swrm,
751 SWRM_DP_PORT_CTRL_BANK(i+1, bank),
752 value);
753 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
754 __func__, i,
755 (SWRM_DP_PORT_CTRL_BANK(i+1, bank)), value);
756 }
757}
758
759static void swrm_cleanup_disabled_port_reqs(struct swr_master *master)
760{
761 struct swr_port_info *port_req, *next;
762 int i;
763 struct swrm_mports *mport;
764 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
765
766 if (!swrm) {
767 pr_err("%s: swrm is null\n", __func__);
768 return;
769 }
770 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
771 master->num_port);
772
773 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
774 mport = &(swrm->mport_cfg[i]);
775 list_for_each_entry_safe(port_req, next,
776 &mport->port_req_list, list) {
777 /* skip ports without new ch req */
778 if (port_req->ch_en == port_req->req_ch)
779 continue;
780
781 /* remove new ch req's*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +0530782 port_req->ch_en = port_req->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530783
784 /* If no streams enabled on port, remove the port req */
785 if (port_req->ch_en == 0) {
786 list_del(&port_req->list);
787 kfree(port_req);
788 }
789 }
790 /* remove new ch req's on mport*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +0530791 mport->ch_en = mport->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530792
793 if (!(mport->ch_en)) {
794 mport->port_en = false;
795 master->port_en_mask &= ~i;
796 }
797 }
798}
799static void swrm_copy_data_port_config(struct swr_master *master, u8 bank)
800{
801 u32 value, slv_id;
802 struct swr_port_info *port_req;
803 int i;
804 struct swrm_mports *mport;
805 u32 reg[SWRM_MAX_PORT_REG];
806 u32 val[SWRM_MAX_PORT_REG];
807 int len = 0;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530808 u8 hparams;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530809 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
810
811 if (!swrm) {
812 pr_err("%s: swrm is null\n", __func__);
813 return;
814 }
815
816 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
817 master->num_port);
818
819 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
820 mport = &(swrm->mport_cfg[i]);
821 if (!mport->port_en)
822 continue;
823
824 list_for_each_entry(port_req, &mport->port_req_list, list) {
825 slv_id = port_req->slave_port_id;
826 reg[len] = SWRM_CMD_FIFO_WR_CMD;
827 val[len++] = SWR_REG_VAL_PACK(port_req->req_ch,
828 port_req->dev_num, 0x00,
829 SWRS_DP_CHANNEL_ENABLE_BANK(slv_id,
830 bank));
831
832 reg[len] = SWRM_CMD_FIFO_WR_CMD;
833 val[len++] = SWR_REG_VAL_PACK(mport->sinterval,
834 port_req->dev_num, 0x00,
835 SWRS_DP_SAMPLE_CONTROL_1_BANK(slv_id,
836 bank));
837
838 reg[len] = SWRM_CMD_FIFO_WR_CMD;
839 val[len++] = SWR_REG_VAL_PACK(mport->offset1,
840 port_req->dev_num, 0x00,
841 SWRS_DP_OFFSET_CONTROL_1_BANK(slv_id,
842 bank));
843
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530844 if (mport->offset2 != SWR_INVALID_PARAM) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530845 reg[len] = SWRM_CMD_FIFO_WR_CMD;
846 val[len++] = SWR_REG_VAL_PACK(mport->offset2,
847 port_req->dev_num, 0x00,
848 SWRS_DP_OFFSET_CONTROL_2_BANK(
849 slv_id, bank));
850 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530851 if (mport->hstart != SWR_INVALID_PARAM
852 && mport->hstop != SWR_INVALID_PARAM) {
853 hparams = (mport->hstart << 4) | mport->hstop;
854
855 reg[len] = SWRM_CMD_FIFO_WR_CMD;
856 val[len++] = SWR_REG_VAL_PACK(hparams,
857 port_req->dev_num, 0x00,
858 SWRS_DP_HCONTROL_BANK(slv_id,
859 bank));
860 }
861 if (mport->word_length != SWR_INVALID_PARAM) {
862 reg[len] = SWRM_CMD_FIFO_WR_CMD;
863 val[len++] =
864 SWR_REG_VAL_PACK(mport->word_length,
865 port_req->dev_num, 0x00,
866 SWRS_DP_BLOCK_CONTROL_1(slv_id));
867 }
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +0530868 if (mport->blk_pack_mode != SWR_INVALID_PARAM
869 && swrm->master_id != MASTER_ID_WSA) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530870 reg[len] = SWRM_CMD_FIFO_WR_CMD;
871 val[len++] =
872 SWR_REG_VAL_PACK(mport->blk_pack_mode,
873 port_req->dev_num, 0x00,
874 SWRS_DP_BLOCK_CONTROL_3_BANK(slv_id,
875 bank));
876 }
877 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
878 reg[len] = SWRM_CMD_FIFO_WR_CMD;
879 val[len++] =
880 SWR_REG_VAL_PACK(mport->blk_grp_count,
881 port_req->dev_num, 0x00,
882 SWRS_DP_BLOCK_CONTROL_2_BANK(slv_id,
883 bank));
884 }
885 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
886 reg[len] = SWRM_CMD_FIFO_WR_CMD;
887 val[len++] =
888 SWR_REG_VAL_PACK(mport->lane_ctrl,
889 port_req->dev_num, 0x00,
890 SWRS_DP_LANE_CONTROL_BANK(slv_id,
891 bank));
892 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530893 port_req->ch_en = port_req->req_ch;
894 }
895 value = ((mport->req_ch)
896 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +0530897
898 if (mport->offset2 != SWR_INVALID_PARAM)
899 value |= ((mport->offset2)
900 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530901 value |= ((mport->offset1)
902 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
903 value |= mport->sinterval;
904
905
906 reg[len] = SWRM_DP_PORT_CTRL_BANK(i + 1, bank);
907 val[len++] = value;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530908 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
909 __func__, i,
910 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)), value);
911
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530912 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
913 reg[len] = SWRM_DP_PORT_CTRL_2_BANK(i + 1, bank);
914 val[len++] = mport->lane_ctrl;
915 }
916 if (mport->word_length != SWR_INVALID_PARAM) {
917 reg[len] = SWRM_DP_BLOCK_CTRL_1(i + 1);
918 val[len++] = mport->word_length;
919 }
920
921 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
922 reg[len] = SWRM_DP_BLOCK_CTRL2_BANK(i + 1, bank);
923 val[len++] = mport->blk_grp_count;
924 }
925 if (mport->hstart != SWR_INVALID_PARAM
926 && mport->hstop != SWR_INVALID_PARAM) {
927 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
Laxminath Kasame30eef72018-11-05 17:40:09 +0530928 hparams = (mport->hstop << 4) | mport->hstart;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530929 val[len++] = hparams;
Laxminath Kasam990c70b2018-11-09 23:15:09 +0530930 } else {
931 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
932 hparams = (SWR_HSTOP_MAX_VAL << 4) | SWR_HSTART_MIN_VAL;
933 val[len++] = hparams;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530934 }
935 if (mport->blk_pack_mode != SWR_INVALID_PARAM) {
936 reg[len] = SWRM_DP_BLOCK_CTRL3_BANK(i + 1, bank);
937 val[len++] = mport->blk_pack_mode;
938 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530939 mport->ch_en = mport->req_ch;
940
941 }
942 swr_master_bulk_write(swrm, reg, val, len);
943}
944
945static void swrm_apply_port_config(struct swr_master *master)
946{
947 u8 bank;
948 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
949
950 if (!swrm) {
951 pr_err("%s: Invalid handle to swr controller\n",
952 __func__);
953 return;
954 }
955
956 bank = get_inactive_bank_num(swrm);
957 dev_dbg(swrm->dev, "%s: enter bank: %d master_ports: %d\n",
958 __func__, bank, master->num_port);
959
960
961 swrm_cmd_fifo_wr_cmd(swrm, 0x01, 0xF, 0x00,
962 SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(bank));
963
964 swrm_copy_data_port_config(master, bank);
965}
966
967static int swrm_slvdev_datapath_control(struct swr_master *master, bool enable)
968{
969 u8 bank;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530970 u32 value, n_row, n_col;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530971 int ret;
972 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
973 int mask = (SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK |
974 SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK |
975 SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_BMSK);
976 u8 inactive_bank;
977
978 if (!swrm) {
979 pr_err("%s: swrm is null\n", __func__);
980 return -EFAULT;
981 }
Ramprasad Katkam7e354782018-11-21 15:52:54 +0530982
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530983 mutex_lock(&swrm->mlock);
984
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530985 bank = get_inactive_bank_num(swrm);
986
987 if (enable) {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +0530988 if (!test_bit(ENABLE_PENDING, &swrm->port_req_pending)) {
989 dev_dbg(swrm->dev, "%s:No pending connect port req\n",
990 __func__);
991 goto exit;
992 }
993 clear_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530994 ret = swrm_get_port_config(swrm);
995 if (ret) {
996 /* cannot accommodate ports */
997 swrm_cleanup_disabled_port_reqs(master);
998 mutex_unlock(&swrm->mlock);
999 return -EINVAL;
1000 }
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301001 swr_master_write(swrm, SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301002 SWRM_INTERRUPT_STATUS_MASK);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301003 /* apply the new port config*/
1004 swrm_apply_port_config(master);
1005 } else {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301006 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1007 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1008 __func__);
1009 goto exit;
1010 }
1011 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301012 swrm_disable_ports(master, bank);
1013 }
1014 dev_dbg(swrm->dev, "%s: enable: %d, cfg_devs: %d\n",
1015 __func__, enable, swrm->num_cfg_devs);
1016
1017 if (enable) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301018 /* set col = 16 */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301019 n_col = SWR_MAX_COL;
1020 } else {
1021 /*
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301022 * Do not change to col = 2 if there are still active ports
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301023 */
1024 if (!master->num_port)
1025 n_col = SWR_MIN_COL;
1026 else
1027 n_col = SWR_MAX_COL;
1028 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301029 /* Use default 50 * x, frame shape. Change based on mclk */
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301030 if (swrm->mclk_freq == MCLK_FREQ_NATIVE) {
1031 dev_dbg(swrm->dev, "setting 64 x %d frameshape\n",
1032 n_col ? 16 : 2);
1033 n_row = SWR_ROW_64;
1034 } else {
1035 dev_dbg(swrm->dev, "setting 50 x %d frameshape\n",
1036 n_col ? 16 : 2);
1037 n_row = SWR_ROW_50;
1038 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301039 value = swr_master_read(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank));
1040 value &= (~mask);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301041 value |= ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301042 (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
1043 (0 << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
1044 swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1045
1046 dev_dbg(swrm->dev, "%s: regaddr: 0x%x, value: 0x%x\n", __func__,
1047 SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1048
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301049 enable_bank_switch(swrm, bank, n_row, n_col);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301050 inactive_bank = bank ? 0 : 1;
1051
1052 if (enable)
1053 swrm_copy_data_port_config(master, inactive_bank);
1054 else {
1055 swrm_disable_ports(master, inactive_bank);
1056 swrm_cleanup_disabled_port_reqs(master);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301057 }
1058 if (!swrm_is_port_en(master)) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301059 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1060 __func__);
1061 pm_runtime_mark_last_busy(swrm->dev);
1062 pm_runtime_put_autosuspend(swrm->dev);
1063 }
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301064exit:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301065 mutex_unlock(&swrm->mlock);
1066return 0;
1067}
1068
1069static int swrm_connect_port(struct swr_master *master,
1070 struct swr_params *portinfo)
1071{
1072 int i;
1073 struct swr_port_info *port_req;
1074 int ret = 0;
1075 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1076 struct swrm_mports *mport;
1077 u8 mstr_port_id, mstr_ch_msk;
1078
1079 dev_dbg(&master->dev, "%s: enter\n", __func__);
1080 if (!portinfo)
1081 return -EINVAL;
1082
1083 if (!swrm) {
1084 dev_err(&master->dev,
1085 "%s: Invalid handle to swr controller\n",
1086 __func__);
1087 return -EINVAL;
1088 }
1089
1090 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301091 mutex_lock(&swrm->devlock);
1092 if (!swrm->dev_up) {
1093 mutex_unlock(&swrm->devlock);
1094 mutex_unlock(&swrm->mlock);
1095 return -EINVAL;
1096 }
1097 mutex_unlock(&swrm->devlock);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301098 if (!swrm_is_port_en(master))
1099 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301100
1101 for (i = 0; i < portinfo->num_port; i++) {
1102 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_msk,
1103 portinfo->port_type[i],
1104 portinfo->port_id[i]);
1105 if (ret) {
1106 dev_err(&master->dev,
1107 "%s: mstr portid for slv port %d not found\n",
1108 __func__, portinfo->port_id[i]);
1109 goto port_fail;
1110 }
1111
1112 mport = &(swrm->mport_cfg[mstr_port_id]);
1113 /* get port req */
1114 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1115 portinfo->dev_num);
1116 if (!port_req) {
1117 dev_dbg(&master->dev, "%s: new req:port id %d dev %d\n",
1118 __func__, portinfo->port_id[i],
1119 portinfo->dev_num);
1120 port_req = kzalloc(sizeof(struct swr_port_info),
1121 GFP_KERNEL);
1122 if (!port_req) {
1123 ret = -ENOMEM;
1124 goto mem_fail;
1125 }
1126 port_req->dev_num = portinfo->dev_num;
1127 port_req->slave_port_id = portinfo->port_id[i];
1128 port_req->num_ch = portinfo->num_ch[i];
1129 port_req->ch_rate = portinfo->ch_rate[i];
1130 port_req->ch_en = 0;
1131 port_req->master_port_id = mstr_port_id;
1132 list_add(&port_req->list, &mport->port_req_list);
1133 }
1134 port_req->req_ch |= portinfo->ch_en[i];
1135
1136 dev_dbg(&master->dev,
1137 "%s: mstr port %d, slv port %d ch_rate %d num_ch %d\n",
1138 __func__, port_req->master_port_id,
1139 port_req->slave_port_id, port_req->ch_rate,
1140 port_req->num_ch);
1141 /* Put the port req on master port */
1142 mport = &(swrm->mport_cfg[mstr_port_id]);
1143 mport->port_en = true;
1144 mport->req_ch |= mstr_ch_msk;
1145 master->port_en_mask |= (1 << mstr_port_id);
1146 }
1147 master->num_port += portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301148 set_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301149 swr_port_response(master, portinfo->tid);
1150
1151 mutex_unlock(&swrm->mlock);
1152 return 0;
1153
1154port_fail:
1155mem_fail:
1156 /* cleanup port reqs in error condition */
1157 swrm_cleanup_disabled_port_reqs(master);
1158 mutex_unlock(&swrm->mlock);
1159 return ret;
1160}
1161
1162static int swrm_disconnect_port(struct swr_master *master,
1163 struct swr_params *portinfo)
1164{
1165 int i, ret = 0;
1166 struct swr_port_info *port_req;
1167 struct swrm_mports *mport;
1168 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1169 u8 mstr_port_id, mstr_ch_mask;
1170
1171 if (!swrm) {
1172 dev_err(&master->dev,
1173 "%s: Invalid handle to swr controller\n",
1174 __func__);
1175 return -EINVAL;
1176 }
1177
1178 if (!portinfo) {
1179 dev_err(&master->dev, "%s: portinfo is NULL\n", __func__);
1180 return -EINVAL;
1181 }
1182 mutex_lock(&swrm->mlock);
1183
1184 for (i = 0; i < portinfo->num_port; i++) {
1185
1186 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_mask,
1187 portinfo->port_type[i], portinfo->port_id[i]);
1188 if (ret) {
1189 dev_err(&master->dev,
1190 "%s: mstr portid for slv port %d not found\n",
1191 __func__, portinfo->port_id[i]);
1192 mutex_unlock(&swrm->mlock);
1193 return -EINVAL;
1194 }
1195 mport = &(swrm->mport_cfg[mstr_port_id]);
1196 /* get port req */
1197 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1198 portinfo->dev_num);
1199
1200 if (!port_req) {
1201 dev_err(&master->dev, "%s:port not enabled : port %d\n",
1202 __func__, portinfo->port_id[i]);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05301203 mutex_unlock(&swrm->mlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301204 return -EINVAL;
1205 }
1206 port_req->req_ch &= ~portinfo->ch_en[i];
1207 mport->req_ch &= ~mstr_ch_mask;
1208 }
1209 master->num_port -= portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301210 set_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301211 swr_port_response(master, portinfo->tid);
1212 mutex_unlock(&swrm->mlock);
1213
1214 return 0;
1215}
1216
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301217static int swrm_find_alert_slave(struct swr_mstr_ctrl *swrm,
1218 int status, u8 *devnum)
1219{
1220 int i;
1221 bool found = false;
1222
1223 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1224 if ((status & SWRM_MCP_SLV_STATUS_MASK) == SWR_ALERT) {
1225 *devnum = i;
1226 found = true;
1227 break;
1228 }
1229 status >>= 2;
1230 }
1231 if (found)
1232 return 0;
1233 else
1234 return -EINVAL;
1235}
1236
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301237static int swrm_check_slave_change_status(struct swr_mstr_ctrl *swrm,
1238 int status, u8 *devnum)
1239{
1240 int i;
1241 int new_sts = status;
1242 int ret = SWR_NOT_PRESENT;
1243
1244 if (status != swrm->slave_status) {
1245 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1246 if ((status & SWRM_MCP_SLV_STATUS_MASK) !=
1247 (swrm->slave_status & SWRM_MCP_SLV_STATUS_MASK)) {
1248 ret = (status & SWRM_MCP_SLV_STATUS_MASK);
1249 *devnum = i;
1250 break;
1251 }
1252 status >>= 2;
1253 swrm->slave_status >>= 2;
1254 }
1255 swrm->slave_status = new_sts;
1256 }
1257 return ret;
1258}
1259
1260static irqreturn_t swr_mstr_interrupt(int irq, void *dev)
1261{
1262 struct swr_mstr_ctrl *swrm = dev;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301263 u32 value, intr_sts, intr_sts_masked;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301264 u32 temp = 0;
1265 u32 status, chg_sts, i;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301266 u8 devnum = 0;
1267 int ret = IRQ_HANDLED;
1268 struct swr_device *swr_dev;
1269 struct swr_master *mstr = &swrm->master;
1270
Ramprasad Katkam57349872018-11-11 18:34:57 +05301271 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1272 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1273 return IRQ_NONE;
1274 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301275
1276 mutex_lock(&swrm->reslock);
1277 swrm_clk_request(swrm, true);
1278 mutex_unlock(&swrm->reslock);
1279
1280 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301281 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301282handle_irq:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301283 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301284 value = intr_sts_masked & (1 << i);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301285 if (!value)
1286 continue;
1287
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301288 switch (value) {
1289 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1290 dev_dbg(swrm->dev, "Trigger irq to slave device\n");
1291 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301292 ret = swrm_find_alert_slave(swrm, status, &devnum);
1293 if (ret) {
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301294 dev_err_ratelimited(swrm->dev,
1295 "no slave alert found.spurious interrupt\n");
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05301296 break;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301297 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301298 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1299 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1300 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1301 SWRS_SCP_INT_STATUS_CLEAR_1);
1302 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1303 SWRS_SCP_INT_STATUS_CLEAR_1);
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301304
1305
1306 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1307 if (swr_dev->dev_num != devnum)
1308 continue;
1309 if (swr_dev->slave_irq) {
1310 do {
1311 handle_nested_irq(
1312 irq_find_mapping(
1313 swr_dev->slave_irq, 0));
1314 } while (swr_dev->slave_irq_pending);
1315 }
1316
1317 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301318 break;
1319 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1320 dev_dbg(swrm->dev, "SWR new slave attached\n");
1321 break;
1322 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1323 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1324 if (status == swrm->slave_status) {
1325 dev_dbg(swrm->dev,
1326 "%s: No change in slave status: %d\n",
1327 __func__, status);
1328 break;
1329 }
1330 chg_sts = swrm_check_slave_change_status(swrm, status,
1331 &devnum);
1332 switch (chg_sts) {
1333 case SWR_NOT_PRESENT:
1334 dev_dbg(swrm->dev, "device %d got detached\n",
1335 devnum);
1336 break;
1337 case SWR_ATTACHED_OK:
1338 dev_dbg(swrm->dev, "device %d got attached\n",
1339 devnum);
Ramprasad Katkamdebe8932018-09-25 18:08:18 +05301340 /* enable host irq from slave device*/
1341 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1342 SWRS_SCP_INT_STATUS_CLEAR_1);
1343 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1344 SWRS_SCP_INT_STATUS_MASK_1);
1345
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301346 break;
1347 case SWR_ALERT:
1348 dev_dbg(swrm->dev,
1349 "device %d has pending interrupt\n",
1350 devnum);
1351 break;
1352 }
1353 break;
1354 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1355 dev_err_ratelimited(swrm->dev,
1356 "SWR bus clsh detected\n");
1357 break;
1358 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1359 dev_dbg(swrm->dev, "SWR read FIFO overflow\n");
1360 break;
1361 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1362 dev_dbg(swrm->dev, "SWR read FIFO underflow\n");
1363 break;
1364 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1365 dev_dbg(swrm->dev, "SWR write FIFO overflow\n");
1366 break;
1367 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1368 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1369 dev_err_ratelimited(swrm->dev,
1370 "SWR CMD error, fifo status 0x%x, flushing fifo\n",
1371 value);
1372 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1373 break;
1374 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301375 dev_err_ratelimited(swrm->dev, "SWR Port collision detected\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301376 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301377 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301378 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301379 break;
1380 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1381 dev_dbg(swrm->dev, "SWR read enable valid mismatch\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301382 swrm->intr_mask &=
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301383 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1384 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301385 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301386 break;
1387 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1388 complete(&swrm->broadcast);
1389 dev_dbg(swrm->dev, "SWR cmd id finished\n");
1390 break;
1391 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_AUTO_ENUM_FINISHED:
1392 break;
1393 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED:
1394 break;
1395 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL:
1396 break;
1397 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED:
1398 complete(&swrm->reset);
1399 break;
1400 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED:
1401 break;
1402 default:
1403 dev_err_ratelimited(swrm->dev,
1404 "SWR unknown interrupt\n");
1405 ret = IRQ_NONE;
1406 break;
1407 }
1408 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301409 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1410 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
Ramprasad Katkam83303512018-10-11 17:34:22 +05301411
1412 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301413 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301414
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301415 if (intr_sts_masked) {
Ramprasad Katkam83303512018-10-11 17:34:22 +05301416 dev_dbg(swrm->dev, "%s: new interrupt received\n", __func__);
1417 goto handle_irq;
1418 }
1419
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301420 mutex_lock(&swrm->reslock);
1421 swrm_clk_request(swrm, false);
1422 mutex_unlock(&swrm->reslock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301423 swrm_unlock_sleep(swrm);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301424 return ret;
1425}
1426
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301427static irqreturn_t swr_mstr_interrupt_v2(int irq, void *dev)
1428{
1429 struct swr_mstr_ctrl *swrm = dev;
1430 u32 value, intr_sts, intr_sts_masked;
1431 u32 temp = 0;
1432 u32 status, chg_sts, i;
1433 u8 devnum = 0;
1434 int ret = IRQ_HANDLED;
1435 struct swr_device *swr_dev;
1436 struct swr_master *mstr = &swrm->master;
1437
1438 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1439 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1440 return IRQ_NONE;
1441 }
1442
1443 mutex_lock(&swrm->reslock);
1444 swrm_clk_request(swrm, true);
1445 mutex_unlock(&swrm->reslock);
1446
1447 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1448 intr_sts_masked = intr_sts & swrm->intr_mask;
1449handle_irq:
1450 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
1451 value = intr_sts_masked & (1 << i);
1452 if (!value)
1453 continue;
1454
1455 switch (value) {
1456 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1457 dev_dbg(swrm->dev, "%s: Trigger irq to slave device\n",
1458 __func__);
1459 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1460 ret = swrm_find_alert_slave(swrm, status, &devnum);
1461 if (ret) {
1462 dev_err_ratelimited(swrm->dev,
1463 "%s: no slave alert found.spurious interrupt\n",
1464 __func__);
1465 break;
1466 }
1467 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1468 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1469 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1470 SWRS_SCP_INT_STATUS_CLEAR_1);
1471 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1472 SWRS_SCP_INT_STATUS_CLEAR_1);
1473
1474
1475 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1476 if (swr_dev->dev_num != devnum)
1477 continue;
1478 if (swr_dev->slave_irq) {
1479 do {
1480 handle_nested_irq(
1481 irq_find_mapping(
1482 swr_dev->slave_irq, 0));
1483 } while (swr_dev->slave_irq_pending);
1484 }
1485
1486 }
1487 break;
1488 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1489 dev_dbg(swrm->dev, "%s: SWR new slave attached\n",
1490 __func__);
1491 break;
1492 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1493 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1494 if (status == swrm->slave_status) {
1495 dev_dbg(swrm->dev,
1496 "%s: No change in slave status: %d\n",
1497 __func__, status);
1498 break;
1499 }
1500 chg_sts = swrm_check_slave_change_status(swrm, status,
1501 &devnum);
1502 switch (chg_sts) {
1503 case SWR_NOT_PRESENT:
1504 dev_dbg(swrm->dev,
1505 "%s: device %d got detached\n",
1506 __func__, devnum);
1507 break;
1508 case SWR_ATTACHED_OK:
1509 dev_dbg(swrm->dev,
1510 "%s: device %d got attached\n",
1511 __func__, devnum);
1512 /* enable host irq from slave device*/
1513 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1514 SWRS_SCP_INT_STATUS_CLEAR_1);
1515 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1516 SWRS_SCP_INT_STATUS_MASK_1);
1517
1518 break;
1519 case SWR_ALERT:
1520 dev_dbg(swrm->dev,
1521 "%s: device %d has pending interrupt\n",
1522 __func__, devnum);
1523 break;
1524 }
1525 break;
1526 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1527 dev_err_ratelimited(swrm->dev,
1528 "%s: SWR bus clsh detected\n",
1529 __func__);
1530 break;
1531 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1532 dev_dbg(swrm->dev, "%s: SWR read FIFO overflow\n",
1533 __func__);
1534 break;
1535 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1536 dev_dbg(swrm->dev, "%s: SWR read FIFO underflow\n",
1537 __func__);
1538 break;
1539 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1540 dev_dbg(swrm->dev, "%s: SWR write FIFO overflow\n",
1541 __func__);
1542 break;
1543 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1544 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1545 dev_err_ratelimited(swrm->dev,
1546 "%s: SWR CMD error, fifo status 0x%x, flushing fifo\n",
1547 __func__, value);
1548 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1549 break;
1550 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
1551 dev_err_ratelimited(swrm->dev,
1552 "%s: SWR Port collision detected\n",
1553 __func__);
1554 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
1555 swr_master_write(swrm,
1556 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1557 break;
1558 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1559 dev_dbg(swrm->dev,
1560 "%s: SWR read enable valid mismatch\n",
1561 __func__);
1562 swrm->intr_mask &=
1563 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1564 swr_master_write(swrm,
1565 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1566 break;
1567 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1568 complete(&swrm->broadcast);
1569 dev_dbg(swrm->dev, "%s: SWR cmd id finished\n",
1570 __func__);
1571 break;
1572 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED_V2:
1573 break;
1574 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL_V2:
1575 break;
1576 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED_V2:
1577 break;
1578 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED_V2:
1579 break;
1580 case SWRM_INTERRUPT_STATUS_EXT_CLK_STOP_WAKEUP:
1581 if (swrm->state == SWR_MSTR_UP)
1582 dev_dbg(swrm->dev,
1583 "%s:SWR Master is already up\n",
1584 __func__);
1585 else
1586 dev_err_ratelimited(swrm->dev,
1587 "%s: SWR wokeup during clock stop\n",
1588 __func__);
1589 break;
1590 default:
1591 dev_err_ratelimited(swrm->dev,
1592 "%s: SWR unknown interrupt value: %d\n",
1593 __func__, value);
1594 ret = IRQ_NONE;
1595 break;
1596 }
1597 }
1598 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1599 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
1600
1601 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1602 intr_sts_masked = intr_sts & swrm->intr_mask;
1603
1604 if (intr_sts_masked) {
1605 dev_dbg(swrm->dev, "%s: new interrupt received\n", __func__);
1606 goto handle_irq;
1607 }
1608
1609 mutex_lock(&swrm->reslock);
1610 swrm_clk_request(swrm, false);
1611 mutex_unlock(&swrm->reslock);
1612 swrm_unlock_sleep(swrm);
1613 return ret;
1614}
1615
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301616static irqreturn_t swrm_wakeup_interrupt(int irq, void *dev)
1617{
1618 struct swr_mstr_ctrl *swrm = dev;
1619 int ret = IRQ_HANDLED;
1620
1621 if (!swrm || !(swrm->dev)) {
1622 pr_err("%s: swrm or dev is null\n", __func__);
1623 return IRQ_NONE;
1624 }
1625 mutex_lock(&swrm->devlock);
1626 if (!swrm->dev_up) {
1627 if (swrm->wake_irq > 0)
1628 disable_irq_nosync(swrm->wake_irq);
1629 mutex_unlock(&swrm->devlock);
1630 return ret;
1631 }
1632 mutex_unlock(&swrm->devlock);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301633 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1634 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1635 goto exit;
1636 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301637 if (swrm->wake_irq > 0)
1638 disable_irq_nosync(swrm->wake_irq);
1639 pm_runtime_get_sync(swrm->dev);
1640 pm_runtime_mark_last_busy(swrm->dev);
1641 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301642 swrm_unlock_sleep(swrm);
1643exit:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301644 return ret;
1645}
1646
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301647static void swrm_wakeup_work(struct work_struct *work)
1648{
1649 struct swr_mstr_ctrl *swrm;
1650
1651 swrm = container_of(work, struct swr_mstr_ctrl,
1652 wakeup_work);
1653 if (!swrm || !(swrm->dev)) {
1654 pr_err("%s: swrm or dev is null\n", __func__);
1655 return;
1656 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301657
1658 mutex_lock(&swrm->devlock);
1659 if (!swrm->dev_up) {
1660 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301661 goto exit;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301662 }
1663 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301664 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1665 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1666 goto exit;
1667 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301668 pm_runtime_get_sync(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301669 pm_runtime_mark_last_busy(swrm->dev);
1670 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301671 swrm_unlock_sleep(swrm);
1672exit:
1673 pm_relax(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301674}
1675
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301676static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
1677{
1678 u32 val;
1679
1680 swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1681 val = (swrm->slave_status >> (devnum * 2));
1682 val &= SWRM_MCP_SLV_STATUS_MASK;
1683 return val;
1684}
1685
1686static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
1687 u8 *dev_num)
1688{
1689 int i;
1690 u64 id = 0;
1691 int ret = -EINVAL;
1692 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1693 struct swr_device *swr_dev;
1694 u32 num_dev = 0;
1695
1696 if (!swrm) {
1697 pr_err("%s: Invalid handle to swr controller\n",
1698 __func__);
1699 return ret;
1700 }
1701 if (swrm->num_dev)
1702 num_dev = swrm->num_dev;
1703 else
1704 num_dev = mstr->num_dev;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301705
1706 mutex_lock(&swrm->devlock);
1707 if (!swrm->dev_up) {
1708 mutex_unlock(&swrm->devlock);
1709 return ret;
1710 }
1711 mutex_unlock(&swrm->devlock);
1712
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301713 pm_runtime_get_sync(swrm->dev);
1714 for (i = 1; i < (num_dev + 1); i++) {
1715 id = ((u64)(swr_master_read(swrm,
1716 SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
1717 id |= swr_master_read(swrm,
1718 SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301719
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301720 /*
1721 * As pm_runtime_get_sync() brings all slaves out of reset
1722 * update logical device number for all slaves.
1723 */
1724 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1725 if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
1726 u32 status = swrm_get_device_status(swrm, i);
1727
1728 if ((status == 0x01) || (status == 0x02)) {
1729 swr_dev->dev_num = i;
1730 if ((id & SWR_DEV_ID_MASK) == dev_id) {
1731 *dev_num = i;
1732 ret = 0;
1733 }
1734 dev_dbg(swrm->dev,
1735 "%s: devnum %d is assigned for dev addr %lx\n",
1736 __func__, i, swr_dev->addr);
1737 }
1738 }
1739 }
1740 }
1741 if (ret)
1742 dev_err(swrm->dev, "%s: device 0x%llx is not ready\n",
1743 __func__, dev_id);
1744
1745 pm_runtime_mark_last_busy(swrm->dev);
1746 pm_runtime_put_autosuspend(swrm->dev);
1747 return ret;
1748}
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301749
1750static void swrm_device_wakeup_vote(struct swr_master *mstr)
1751{
1752 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1753
1754 if (!swrm) {
1755 pr_err("%s: Invalid handle to swr controller\n",
1756 __func__);
1757 return;
1758 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05301759 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1760 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1761 return;
1762 }
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301763 pm_runtime_get_sync(swrm->dev);
1764}
1765
1766static void swrm_device_wakeup_unvote(struct swr_master *mstr)
1767{
1768 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1769
1770 if (!swrm) {
1771 pr_err("%s: Invalid handle to swr controller\n",
1772 __func__);
1773 return;
1774 }
1775 pm_runtime_mark_last_busy(swrm->dev);
1776 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301777 swrm_unlock_sleep(swrm);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301778}
1779
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301780static int swrm_master_init(struct swr_mstr_ctrl *swrm)
1781{
1782 int ret = 0;
1783 u32 val;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301784 u8 row_ctrl = SWR_ROW_50;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301785 u8 col_ctrl = SWR_MIN_COL;
1786 u8 ssp_period = 1;
1787 u8 retry_cmd_num = 3;
1788 u32 reg[SWRM_MAX_INIT_REG];
1789 u32 value[SWRM_MAX_INIT_REG];
1790 int len = 0;
1791
1792 /* Clear Rows and Cols */
1793 val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
1794 (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
1795 (ssp_period << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
1796
1797 reg[len] = SWRM_MCP_FRAME_CTRL_BANK_ADDR(0);
1798 value[len++] = val;
1799
1800 /* Set Auto enumeration flag */
1801 reg[len] = SWRM_ENUMERATOR_CFG_ADDR;
1802 value[len++] = 1;
1803
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301804 /* Configure No pings */
1805 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
1806 val &= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK;
1807 val |= (0x1f << SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT);
1808 reg[len] = SWRM_MCP_CFG_ADDR;
1809 value[len++] = val;
1810
1811 /* Configure number of retries of a read/write cmd */
1812 val = (retry_cmd_num << SWRM_CMD_FIFO_CFG_NUM_OF_CMD_RETRY_SHFT);
1813 reg[len] = SWRM_CMD_FIFO_CFG_ADDR;
1814 value[len++] = val;
1815
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301816 reg[len] = SWRM_MCP_BUS_CTRL_ADDR;
1817 value[len++] = 0x2;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301818
Ramprasad Katkam83303512018-10-11 17:34:22 +05301819 /* Set IRQ to PULSE */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301820 reg[len] = SWRM_COMP_CFG_ADDR;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301821 value[len++] = 0x02;
1822
1823 reg[len] = SWRM_COMP_CFG_ADDR;
1824 value[len++] = 0x03;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301825
1826 reg[len] = SWRM_INTERRUPT_CLEAR;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301827 value[len++] = 0xFFFFFFFF;
1828
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301829 swrm->intr_mask = SWRM_INTERRUPT_STATUS_MASK;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301830 /* Mask soundwire interrupts */
1831 reg[len] = SWRM_INTERRUPT_MASK_ADDR;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301832 value[len++] = swrm->intr_mask;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301833
1834 reg[len] = SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301835 value[len++] = swrm->intr_mask;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301836
1837 swr_master_bulk_write(swrm, reg, value, len);
1838
Sudheer Papothi63f48152018-11-15 01:08:03 +05301839 /*
1840 * For SWR master version 1.5.1, continue
1841 * execute on command ignore.
1842 */
1843 if (swrm->version == SWRM_VERSION_1_5_1)
1844 swr_master_write(swrm, SWRM_CMD_FIFO_CFG_ADDR,
1845 (swr_master_read(swrm,
1846 SWRM_CMD_FIFO_CFG_ADDR) | 0x80000000));
1847
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301848 return ret;
1849}
1850
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05301851static int swrm_event_notify(struct notifier_block *self,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301852 unsigned long action, void *data)
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05301853{
1854 struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301855 event_notifier);
1856
1857 if (!swrm || !(swrm->dev)) {
1858 pr_err("%s: swrm or dev is NULL\n", __func__);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05301859 return -EINVAL;
1860 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301861 switch (action) {
1862 case MSM_AUD_DC_EVENT:
1863 schedule_work(&(swrm->dc_presence_work));
1864 break;
1865 case SWR_WAKE_IRQ_EVENT:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301866 if (swrm->ipc_wakeup && !swrm->ipc_wakeup_triggered) {
1867 swrm->ipc_wakeup_triggered = true;
Ramprasad Katkam57349872018-11-11 18:34:57 +05301868 pm_stay_awake(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301869 schedule_work(&swrm->wakeup_work);
Ramprasad Katkamcd61c6e2018-09-18 13:22:58 +05301870 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301871 break;
1872 default:
1873 dev_err(swrm->dev, "%s: invalid event type: %lu\n",
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05301874 __func__, action);
1875 return -EINVAL;
1876 }
1877
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05301878 return 0;
1879}
1880
1881static void swrm_notify_work_fn(struct work_struct *work)
1882{
1883 struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301884 dc_presence_work);
1885
1886 if (!swrm || !swrm->pdev) {
1887 pr_err("%s: swrm or pdev is NULL\n", __func__);
1888 return;
1889 }
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05301890 swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL);
1891}
1892
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301893static int swrm_probe(struct platform_device *pdev)
1894{
1895 struct swr_mstr_ctrl *swrm;
1896 struct swr_ctrl_platform_data *pdata;
1897 u32 i, num_ports, port_num, port_type, ch_mask;
1898 u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0;
1899 int ret = 0;
1900
1901 /* Allocate soundwire master driver structure */
1902 swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl),
1903 GFP_KERNEL);
1904 if (!swrm) {
1905 ret = -ENOMEM;
1906 goto err_memory_fail;
1907 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301908 swrm->pdev = pdev;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301909 swrm->dev = &pdev->dev;
1910 platform_set_drvdata(pdev, swrm);
1911 swr_set_ctrl_data(&swrm->master, swrm);
1912 pdata = dev_get_platdata(&pdev->dev);
1913 if (!pdata) {
1914 dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
1915 __func__);
1916 ret = -EINVAL;
1917 goto err_pdata_fail;
1918 }
1919 swrm->handle = (void *)pdata->handle;
1920 if (!swrm->handle) {
1921 dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
1922 __func__);
1923 ret = -EINVAL;
1924 goto err_pdata_fail;
1925 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301926 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr_master_id",
1927 &swrm->master_id);
1928 if (ret) {
1929 dev_err(&pdev->dev, "%s: failed to get master id\n", __func__);
1930 goto err_pdata_fail;
1931 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05301932 if (!(of_property_read_u32(pdev->dev.of_node,
1933 "swrm-io-base", &swrm->swrm_base_reg)))
1934 ret = of_property_read_u32(pdev->dev.of_node,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301935 "swrm-io-base", &swrm->swrm_base_reg);
1936 if (!swrm->swrm_base_reg) {
1937 swrm->read = pdata->read;
1938 if (!swrm->read) {
1939 dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
1940 __func__);
1941 ret = -EINVAL;
1942 goto err_pdata_fail;
1943 }
1944 swrm->write = pdata->write;
1945 if (!swrm->write) {
1946 dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
1947 __func__);
1948 ret = -EINVAL;
1949 goto err_pdata_fail;
1950 }
1951 swrm->bulk_write = pdata->bulk_write;
1952 if (!swrm->bulk_write) {
1953 dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
1954 __func__);
1955 ret = -EINVAL;
1956 goto err_pdata_fail;
1957 }
1958 } else {
1959 swrm->swrm_dig_base = devm_ioremap(&pdev->dev,
1960 swrm->swrm_base_reg, SWRM_MAX_REGISTER);
1961 }
1962
1963 swrm->clk = pdata->clk;
1964 if (!swrm->clk) {
1965 dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
1966 __func__);
1967 ret = -EINVAL;
1968 goto err_pdata_fail;
1969 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301970 if (of_property_read_u32(pdev->dev.of_node,
1971 "qcom,swr-clock-stop-mode0",
1972 &swrm->clk_stop_mode0_supp)) {
1973 swrm->clk_stop_mode0_supp = FALSE;
1974 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05301975
1976 ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
1977 &swrm->num_dev);
1978 if (ret) {
1979 dev_dbg(&pdev->dev, "%s: Looking up %s property failed\n",
1980 __func__, "qcom,swr-num-dev");
1981 } else {
1982 if (swrm->num_dev > SWR_MAX_SLAVE_DEVICES) {
1983 dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
1984 __func__, swrm->num_dev, SWR_MAX_SLAVE_DEVICES);
1985 ret = -EINVAL;
1986 goto err_pdata_fail;
1987 }
1988 }
1989
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301990 /* Parse soundwire port mapping */
1991 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
1992 &num_ports);
1993 if (ret) {
1994 dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
1995 goto err_pdata_fail;
1996 }
1997 swrm->num_ports = num_ports;
1998
1999 if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
2000 &map_size)) {
2001 dev_err(swrm->dev, "missing port mapping\n");
2002 goto err_pdata_fail;
2003 }
2004
2005 map_length = map_size / (3 * sizeof(u32));
2006 if (num_ports > SWR_MSTR_PORT_LEN) {
2007 dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
2008 __func__);
2009 ret = -EINVAL;
2010 goto err_pdata_fail;
2011 }
2012 temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
2013
2014 if (!temp) {
2015 ret = -ENOMEM;
2016 goto err_pdata_fail;
2017 }
2018 ret = of_property_read_u32_array(pdev->dev.of_node,
2019 "qcom,swr-port-mapping", temp, 3 * map_length);
2020 if (ret) {
2021 dev_err(swrm->dev, "%s: Failed to read port mapping\n",
2022 __func__);
2023 goto err_pdata_fail;
2024 }
2025
2026 for (i = 0; i < map_length; i++) {
2027 port_num = temp[3 * i];
2028 port_type = temp[3 * i + 1];
2029 ch_mask = temp[3 * i + 2];
2030
2031 if (port_num != old_port_num)
2032 ch_iter = 0;
2033 swrm->port_mapping[port_num][ch_iter].port_type = port_type;
2034 swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
2035 old_port_num = port_num;
2036 }
2037 devm_kfree(&pdev->dev, temp);
2038
2039 swrm->reg_irq = pdata->reg_irq;
2040 swrm->master.read = swrm_read;
2041 swrm->master.write = swrm_write;
2042 swrm->master.bulk_write = swrm_bulk_write;
2043 swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
2044 swrm->master.connect_port = swrm_connect_port;
2045 swrm->master.disconnect_port = swrm_disconnect_port;
2046 swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
2047 swrm->master.remove_from_group = swrm_remove_from_group;
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302048 swrm->master.device_wakeup_vote = swrm_device_wakeup_vote;
2049 swrm->master.device_wakeup_unvote = swrm_device_wakeup_unvote;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302050 swrm->master.dev.parent = &pdev->dev;
2051 swrm->master.dev.of_node = pdev->dev.of_node;
2052 swrm->master.num_port = 0;
2053 swrm->rcmd_id = 0;
2054 swrm->wcmd_id = 0;
2055 swrm->slave_status = 0;
2056 swrm->num_rx_chs = 0;
2057 swrm->clk_ref_count = 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302058 swrm->swr_irq_wakeup_capable = 0;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302059 swrm->mclk_freq = MCLK_FREQ;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302060 swrm->dev_up = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302061 swrm->state = SWR_MSTR_UP;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302062 swrm->ipc_wakeup = false;
2063 swrm->ipc_wakeup_triggered = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302064 init_completion(&swrm->reset);
2065 init_completion(&swrm->broadcast);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302066 init_completion(&swrm->clk_off_complete);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302067 mutex_init(&swrm->mlock);
2068 mutex_init(&swrm->reslock);
2069 mutex_init(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302070 mutex_init(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302071 mutex_init(&swrm->clklock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302072 mutex_init(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302073 mutex_init(&swrm->pm_lock);
2074 swrm->wlock_holders = 0;
2075 swrm->pm_state = SWRM_PM_SLEEPABLE;
2076 init_waitqueue_head(&swrm->pm_wq);
2077 pm_qos_add_request(&swrm->pm_qos_req,
2078 PM_QOS_CPU_DMA_LATENCY,
2079 PM_QOS_DEFAULT_VALUE);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302080
2081 for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++)
2082 INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
2083
2084 if (swrm->reg_irq) {
2085 ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
2086 SWR_IRQ_REGISTER);
2087 if (ret) {
2088 dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
2089 __func__, ret);
2090 goto err_irq_fail;
2091 }
2092 } else {
2093 swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
2094 if (swrm->irq < 0) {
2095 dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
2096 __func__, swrm->irq);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302097 goto err_irq_fail;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302098 }
2099
2100 ret = request_threaded_irq(swrm->irq, NULL,
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302101 swr_mstr_interrupt_v2,
Ramprasad Katkam83303512018-10-11 17:34:22 +05302102 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302103 "swr_master_irq", swrm);
2104 if (ret) {
2105 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2106 __func__, ret);
2107 goto err_irq_fail;
2108 }
2109
2110 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302111 /* Make inband tx interrupts as wakeup capable for slave irq */
2112 ret = of_property_read_u32(pdev->dev.of_node,
2113 "qcom,swr-mstr-irq-wakeup-capable",
2114 &swrm->swr_irq_wakeup_capable);
2115 if (ret)
2116 dev_dbg(swrm->dev, "%s: swrm irq wakeup capable not defined\n",
2117 __func__);
2118 if (swrm->swr_irq_wakeup_capable)
2119 irq_set_irq_wake(swrm->irq, 1);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302120 ret = swr_register_master(&swrm->master);
2121 if (ret) {
2122 dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
2123 goto err_mstr_fail;
2124 }
2125
2126 /* Add devices registered with board-info as the
2127 * controller will be up now
2128 */
2129 swr_master_add_boarddevices(&swrm->master);
2130 mutex_lock(&swrm->mlock);
2131 swrm_clk_request(swrm, true);
2132 ret = swrm_master_init(swrm);
2133 if (ret < 0) {
2134 dev_err(&pdev->dev,
2135 "%s: Error in master Initialization , err %d\n",
2136 __func__, ret);
2137 mutex_unlock(&swrm->mlock);
2138 goto err_mstr_fail;
2139 }
2140 swrm->version = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
2141
2142 mutex_unlock(&swrm->mlock);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302143 INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302144
2145 if (pdev->dev.of_node)
2146 of_register_swr_devices(&swrm->master);
2147
2148 dbgswrm = swrm;
2149 debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
2150 if (!IS_ERR(debugfs_swrm_dent)) {
2151 debugfs_peek = debugfs_create_file("swrm_peek",
2152 S_IFREG | 0444, debugfs_swrm_dent,
2153 (void *) "swrm_peek", &swrm_debug_ops);
2154
2155 debugfs_poke = debugfs_create_file("swrm_poke",
2156 S_IFREG | 0444, debugfs_swrm_dent,
2157 (void *) "swrm_poke", &swrm_debug_ops);
2158
2159 debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
2160 S_IFREG | 0444, debugfs_swrm_dent,
2161 (void *) "swrm_reg_dump",
2162 &swrm_debug_ops);
2163 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302164
2165 ret = device_init_wakeup(swrm->dev, true);
2166 if (ret) {
2167 dev_err(swrm->dev, "Device wakeup init failed: %d\n", ret);
2168 goto err_irq_wakeup_fail;
2169 }
2170
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302171 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
2172 pm_runtime_use_autosuspend(&pdev->dev);
2173 pm_runtime_set_active(&pdev->dev);
2174 pm_runtime_enable(&pdev->dev);
2175 pm_runtime_mark_last_busy(&pdev->dev);
2176
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302177 INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn);
2178 swrm->event_notifier.notifier_call = swrm_event_notify;
2179 msm_aud_evt_register_client(&swrm->event_notifier);
2180
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302181 return 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302182err_irq_wakeup_fail:
2183 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302184err_mstr_fail:
2185 if (swrm->reg_irq)
2186 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2187 swrm, SWR_IRQ_FREE);
2188 else if (swrm->irq)
2189 free_irq(swrm->irq, swrm);
2190err_irq_fail:
2191 mutex_destroy(&swrm->mlock);
2192 mutex_destroy(&swrm->reslock);
2193 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302194 mutex_destroy(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302195 mutex_destroy(&swrm->clklock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302196 mutex_destroy(&swrm->pm_lock);
2197 pm_qos_remove_request(&swrm->pm_qos_req);
2198
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302199err_pdata_fail:
2200err_memory_fail:
2201 return ret;
2202}
2203
2204static int swrm_remove(struct platform_device *pdev)
2205{
2206 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2207
2208 if (swrm->reg_irq)
2209 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2210 swrm, SWR_IRQ_FREE);
2211 else if (swrm->irq)
2212 free_irq(swrm->irq, swrm);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302213 else if (swrm->wake_irq > 0)
2214 free_irq(swrm->wake_irq, swrm);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302215 if (swrm->swr_irq_wakeup_capable)
2216 irq_set_irq_wake(swrm->irq, 0);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302217 cancel_work_sync(&swrm->wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302218 pm_runtime_disable(&pdev->dev);
2219 pm_runtime_set_suspended(&pdev->dev);
2220 swr_unregister_master(&swrm->master);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302221 msm_aud_evt_unregister_client(&swrm->event_notifier);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302222 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302223 mutex_destroy(&swrm->mlock);
2224 mutex_destroy(&swrm->reslock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302225 mutex_destroy(&swrm->iolock);
2226 mutex_destroy(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302227 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302228 mutex_destroy(&swrm->pm_lock);
2229 pm_qos_remove_request(&swrm->pm_qos_req);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302230 devm_kfree(&pdev->dev, swrm);
2231 return 0;
2232}
2233
2234static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
2235{
2236 u32 val;
2237
2238 dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
2239 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD);
2240 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2241 val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK;
2242 swr_master_write(swrm, SWRM_MCP_CFG_ADDR, val);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302243
2244 return 0;
2245}
2246
2247#ifdef CONFIG_PM
2248static int swrm_runtime_resume(struct device *dev)
2249{
2250 struct platform_device *pdev = to_platform_device(dev);
2251 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2252 int ret = 0;
2253 struct swr_master *mstr = &swrm->master;
2254 struct swr_device *swr_dev;
2255
2256 dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
2257 __func__, swrm->state);
2258 mutex_lock(&swrm->reslock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302259
2260 if ((swrm->state == SWR_MSTR_DOWN) ||
2261 (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) {
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302262 if (swrm->clk_stop_mode0_supp) {
2263 if (swrm->ipc_wakeup)
2264 msm_aud_evt_blocking_notifier_call_chain(
2265 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302266 }
2267
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302268 if (swrm_clk_request(swrm, true))
2269 goto exit;
2270 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +05302271 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302272 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2273 ret = swr_device_up(swr_dev);
2274 if (ret) {
2275 dev_err(dev,
2276 "%s: failed to wakeup swr dev %d\n",
2277 __func__, swr_dev->dev_num);
2278 swrm_clk_request(swrm, false);
2279 goto exit;
2280 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302281 }
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05302282 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2283 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2284 swrm_master_init(swrm);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302285 swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, 0x0,
2286 SWRS_SCP_INT_STATUS_MASK_1);
2287
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302288 } else {
2289 /*wake up from clock stop*/
2290 swr_master_write(swrm, SWRM_MCP_BUS_CTRL_ADDR, 0x2);
2291 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302292 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302293 swrm->state = SWR_MSTR_UP;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302294 }
2295exit:
2296 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
2297 mutex_unlock(&swrm->reslock);
2298 return ret;
2299}
2300
2301static int swrm_runtime_suspend(struct device *dev)
2302{
2303 struct platform_device *pdev = to_platform_device(dev);
2304 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2305 int ret = 0;
2306 struct swr_master *mstr = &swrm->master;
2307 struct swr_device *swr_dev;
2308 int current_state = 0;
2309
2310 dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
2311 __func__, swrm->state);
2312 mutex_lock(&swrm->reslock);
2313 mutex_lock(&swrm->force_down_lock);
2314 current_state = swrm->state;
2315 mutex_unlock(&swrm->force_down_lock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302316 if ((current_state == SWR_MSTR_UP) ||
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302317 (current_state == SWR_MSTR_SSR)) {
2318
2319 if ((current_state != SWR_MSTR_SSR) &&
2320 swrm_is_port_en(&swrm->master)) {
2321 dev_dbg(dev, "%s ports are enabled\n", __func__);
2322 ret = -EBUSY;
2323 goto exit;
2324 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302325 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +05302326 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302327 swrm_clk_pause(swrm);
2328 swr_master_write(swrm, SWRM_COMP_CFG_ADDR, 0x00);
2329 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2330 ret = swr_device_down(swr_dev);
2331 if (ret) {
2332 dev_err(dev,
2333 "%s: failed to shutdown swr dev %d\n",
2334 __func__, swr_dev->dev_num);
2335 goto exit;
2336 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302337 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302338 } else {
2339 /* clock stop sequence */
2340 swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
2341 SWRS_SCP_CONTROL);
2342 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302343 }
2344 swrm_clk_request(swrm, false);
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302345
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302346 if (swrm->clk_stop_mode0_supp) {
2347 if (swrm->wake_irq > 0) {
2348 enable_irq(swrm->wake_irq);
2349 } else if (swrm->ipc_wakeup) {
2350 msm_aud_evt_blocking_notifier_call_chain(
2351 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
2352 swrm->ipc_wakeup_triggered = false;
2353 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302354 }
2355
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302356 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302357 /* Retain SSR state until resume */
2358 if (current_state != SWR_MSTR_SSR)
2359 swrm->state = SWR_MSTR_DOWN;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302360exit:
2361 mutex_unlock(&swrm->reslock);
2362 return ret;
2363}
2364#endif /* CONFIG_PM */
2365
2366static int swrm_device_down(struct device *dev)
2367{
2368 struct platform_device *pdev = to_platform_device(dev);
2369 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2370 int ret = 0;
2371
2372 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2373
2374 mutex_lock(&swrm->force_down_lock);
2375 swrm->state = SWR_MSTR_SSR;
2376 mutex_unlock(&swrm->force_down_lock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302377 if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
2378 ret = swrm_runtime_suspend(dev);
2379 if (!ret) {
2380 pm_runtime_disable(dev);
2381 pm_runtime_set_suspended(dev);
2382 pm_runtime_enable(dev);
2383 }
2384 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302385
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302386 return 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302387}
2388
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302389int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm)
2390{
2391 int ret = 0;
Laxminath Kasama60239e2019-01-10 14:43:03 +05302392 int irq, dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302393
2394 if (!swrm->ipc_wakeup) {
Laxminath Kasama60239e2019-01-10 14:43:03 +05302395 irq = of_get_named_gpio(swrm->dev->of_node,
2396 "qcom,swr-wakeup-irq", 0);
2397 if (gpio_is_valid(irq)) {
2398 swrm->wake_irq = gpio_to_irq(irq);
2399 if (swrm->wake_irq < 0) {
2400 dev_err(swrm->dev,
2401 "Unable to configure irq\n");
2402 return swrm->wake_irq;
2403 }
2404 } else {
2405 dir_apps_irq = platform_get_irq_byname(swrm->pdev,
2406 "swr_wake_irq");
2407 if (dir_apps_irq < 0) {
2408 dev_err(swrm->dev,
2409 "TLMM connect gpio not found\n");
2410 return -EINVAL;
2411 }
2412 swrm->wake_irq = dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302413 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302414 ret = request_threaded_irq(swrm->wake_irq, NULL,
2415 swrm_wakeup_interrupt,
2416 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
2417 "swr_wake_irq", swrm);
2418 if (ret) {
2419 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2420 __func__, ret);
2421 return -EINVAL;
2422 }
Aditya Bavanari3517b112018-12-03 13:26:59 +05302423 irq_set_irq_wake(swrm->wake_irq, 1);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302424 }
2425 return ret;
2426}
2427
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302428static int swrm_alloc_port_mem(struct device *dev, struct swr_mstr_ctrl *swrm,
2429 u32 uc, u32 size)
2430{
2431 if (!swrm->port_param) {
2432 swrm->port_param = devm_kzalloc(dev,
2433 sizeof(swrm->port_param) * SWR_UC_MAX,
2434 GFP_KERNEL);
2435 if (!swrm->port_param)
2436 return -ENOMEM;
2437 }
2438 if (!swrm->port_param[uc]) {
2439 swrm->port_param[uc] = devm_kcalloc(dev, size,
2440 sizeof(struct port_params),
2441 GFP_KERNEL);
2442 if (!swrm->port_param[uc])
2443 return -ENOMEM;
2444 } else {
2445 dev_err_ratelimited(swrm->dev, "%s: called more than once\n",
2446 __func__);
2447 }
2448
2449 return 0;
2450}
2451
2452static int swrm_copy_port_config(struct swr_mstr_ctrl *swrm,
2453 struct swrm_port_config *port_cfg,
2454 u32 size)
2455{
2456 int idx;
2457 struct port_params *params;
2458 int uc = port_cfg->uc;
2459 int ret = 0;
2460
2461 for (idx = 0; idx < size; idx++) {
2462 params = &((struct port_params *)port_cfg->params)[idx];
2463 if (!params) {
2464 dev_err(swrm->dev, "%s: Invalid params\n", __func__);
2465 ret = -EINVAL;
2466 break;
2467 }
2468 memcpy(&swrm->port_param[uc][idx], params,
2469 sizeof(struct port_params));
2470 }
2471
2472 return ret;
2473}
2474
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302475/**
2476 * swrm_wcd_notify - parent device can notify to soundwire master through
2477 * this function
2478 * @pdev: pointer to platform device structure
2479 * @id: command id from parent to the soundwire master
2480 * @data: data from parent device to soundwire master
2481 */
2482int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
2483{
2484 struct swr_mstr_ctrl *swrm;
2485 int ret = 0;
2486 struct swr_master *mstr;
2487 struct swr_device *swr_dev;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302488 struct swrm_port_config *port_cfg;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302489
2490 if (!pdev) {
2491 pr_err("%s: pdev is NULL\n", __func__);
2492 return -EINVAL;
2493 }
2494 swrm = platform_get_drvdata(pdev);
2495 if (!swrm) {
2496 dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__);
2497 return -EINVAL;
2498 }
2499 mstr = &swrm->master;
2500
2501 switch (id) {
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302502 case SWR_CLK_FREQ:
2503 if (!data) {
2504 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
2505 ret = -EINVAL;
2506 } else {
2507 mutex_lock(&swrm->mlock);
2508 swrm->mclk_freq = *(int *)data;
2509 mutex_unlock(&swrm->mlock);
2510 }
2511 break;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302512 case SWR_DEVICE_SSR_DOWN:
2513 mutex_lock(&swrm->devlock);
2514 swrm->dev_up = false;
2515 mutex_unlock(&swrm->devlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302516 mutex_lock(&swrm->reslock);
2517 swrm->state = SWR_MSTR_SSR;
2518 mutex_unlock(&swrm->reslock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302519 break;
2520 case SWR_DEVICE_SSR_UP:
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302521 /* wait for clk voting to be zero */
Ramprasad Katkam7f6462e2018-11-06 11:51:22 +05302522 reinit_completion(&swrm->clk_off_complete);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302523 if (swrm->clk_ref_count &&
2524 !wait_for_completion_timeout(&swrm->clk_off_complete,
Ramprasad Katkamc87efeb2018-12-12 19:26:19 +05302525 msecs_to_jiffies(500)))
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302526 dev_err(swrm->dev, "%s: clock voting not zero\n",
2527 __func__);
2528
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302529 mutex_lock(&swrm->devlock);
2530 swrm->dev_up = true;
2531 mutex_unlock(&swrm->devlock);
2532 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302533 case SWR_DEVICE_DOWN:
2534 dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
2535 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302536 if (swrm->state == SWR_MSTR_DOWN)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302537 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
2538 __func__, swrm->state);
2539 else
2540 swrm_device_down(&pdev->dev);
2541 mutex_unlock(&swrm->mlock);
2542 break;
2543 case SWR_DEVICE_UP:
2544 dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
Ramprasad Katkam0fed92f2018-11-08 14:22:22 +05302545 mutex_lock(&swrm->devlock);
2546 if (!swrm->dev_up) {
2547 dev_dbg(swrm->dev, "SSR not complete yet\n");
2548 mutex_unlock(&swrm->devlock);
2549 return -EBUSY;
2550 }
2551 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302552 mutex_lock(&swrm->mlock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302553 pm_runtime_mark_last_busy(&pdev->dev);
2554 pm_runtime_get_sync(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302555 mutex_lock(&swrm->reslock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302556 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2557 ret = swr_reset_device(swr_dev);
2558 if (ret) {
2559 dev_err(swrm->dev,
2560 "%s: failed to reset swr device %d\n",
2561 __func__, swr_dev->dev_num);
2562 swrm_clk_request(swrm, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302563 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302564 }
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302565 pm_runtime_mark_last_busy(&pdev->dev);
2566 pm_runtime_put_autosuspend(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302567 mutex_unlock(&swrm->reslock);
2568 mutex_unlock(&swrm->mlock);
2569 break;
2570 case SWR_SET_NUM_RX_CH:
2571 if (!data) {
2572 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
2573 ret = -EINVAL;
2574 } else {
2575 mutex_lock(&swrm->mlock);
2576 swrm->num_rx_chs = *(int *)data;
2577 if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
2578 list_for_each_entry(swr_dev, &mstr->devices,
2579 dev_list) {
2580 ret = swr_set_device_group(swr_dev,
2581 SWR_BROADCAST);
2582 if (ret)
2583 dev_err(swrm->dev,
2584 "%s: set num ch failed\n",
2585 __func__);
2586 }
2587 } else {
2588 list_for_each_entry(swr_dev, &mstr->devices,
2589 dev_list) {
2590 ret = swr_set_device_group(swr_dev,
2591 SWR_GROUP_NONE);
2592 if (ret)
2593 dev_err(swrm->dev,
2594 "%s: set num ch failed\n",
2595 __func__);
2596 }
2597 }
2598 mutex_unlock(&swrm->mlock);
2599 }
2600 break;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302601 case SWR_REGISTER_WAKE_IRQ:
2602 if (!data) {
2603 dev_err(swrm->dev, "%s: reg wake irq data is NULL\n",
2604 __func__);
2605 ret = -EINVAL;
2606 } else {
2607 mutex_lock(&swrm->mlock);
2608 swrm->ipc_wakeup = *(u32 *)data;
2609 ret = swrm_register_wake_irq(swrm);
2610 if (ret)
2611 dev_err(swrm->dev, "%s: register wake_irq failed\n",
2612 __func__);
2613 mutex_unlock(&swrm->mlock);
2614 }
2615 break;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302616 case SWR_SET_PORT_MAP:
2617 if (!data) {
2618 dev_err(swrm->dev, "%s: data is NULL for id=%d\n",
2619 __func__, id);
2620 ret = -EINVAL;
2621 } else {
2622 mutex_lock(&swrm->mlock);
2623 port_cfg = (struct swrm_port_config *)data;
2624 if (!port_cfg->size) {
2625 ret = -EINVAL;
2626 goto done;
2627 }
2628 ret = swrm_alloc_port_mem(&pdev->dev, swrm,
2629 port_cfg->uc, port_cfg->size);
2630 if (!ret)
2631 swrm_copy_port_config(swrm, port_cfg,
2632 port_cfg->size);
2633done:
2634 mutex_unlock(&swrm->mlock);
2635 }
2636 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302637 default:
2638 dev_err(swrm->dev, "%s: swr master unknown id %d\n",
2639 __func__, id);
2640 break;
2641 }
2642 return ret;
2643}
2644EXPORT_SYMBOL(swrm_wcd_notify);
2645
Ramprasad Katkam57349872018-11-11 18:34:57 +05302646/*
2647 * swrm_pm_cmpxchg:
2648 * Check old state and exchange with pm new state
2649 * if old state matches with current state
2650 *
2651 * @swrm: pointer to wcd core resource
2652 * @o: pm old state
2653 * @n: pm new state
2654 *
2655 * Returns old state
2656 */
2657static enum swrm_pm_state swrm_pm_cmpxchg(
2658 struct swr_mstr_ctrl *swrm,
2659 enum swrm_pm_state o,
2660 enum swrm_pm_state n)
2661{
2662 enum swrm_pm_state old;
2663
2664 if (!swrm)
2665 return o;
2666
2667 mutex_lock(&swrm->pm_lock);
2668 old = swrm->pm_state;
2669 if (old == o)
2670 swrm->pm_state = n;
2671 mutex_unlock(&swrm->pm_lock);
2672
2673 return old;
2674}
2675
2676static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm)
2677{
2678 enum swrm_pm_state os;
2679
2680 /*
2681 * swrm_{lock/unlock}_sleep will be called by swr irq handler
2682 * and slave wake up requests..
2683 *
2684 * If system didn't resume, we can simply return false so
2685 * IRQ handler can return without handling IRQ.
2686 */
2687 mutex_lock(&swrm->pm_lock);
2688 if (swrm->wlock_holders++ == 0) {
2689 dev_dbg(swrm->dev, "%s: holding wake lock\n", __func__);
2690 pm_qos_update_request(&swrm->pm_qos_req,
2691 msm_cpuidle_get_deep_idle_latency());
2692 pm_stay_awake(swrm->dev);
2693 }
2694 mutex_unlock(&swrm->pm_lock);
2695
2696 if (!wait_event_timeout(swrm->pm_wq,
2697 ((os = swrm_pm_cmpxchg(swrm,
2698 SWRM_PM_SLEEPABLE,
2699 SWRM_PM_AWAKE)) ==
2700 SWRM_PM_SLEEPABLE ||
2701 (os == SWRM_PM_AWAKE)),
2702 msecs_to_jiffies(
2703 SWRM_SYSTEM_RESUME_TIMEOUT_MS))) {
2704 dev_err(swrm->dev, "%s: system didn't resume within %dms, s %d, w %d\n",
2705 __func__, SWRM_SYSTEM_RESUME_TIMEOUT_MS, swrm->pm_state,
2706 swrm->wlock_holders);
2707 swrm_unlock_sleep(swrm);
2708 return false;
2709 }
2710 wake_up_all(&swrm->pm_wq);
2711 return true;
2712}
2713
2714static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm)
2715{
2716 mutex_lock(&swrm->pm_lock);
2717 if (--swrm->wlock_holders == 0) {
2718 dev_dbg(swrm->dev, "%s: releasing wake lock pm_state %d -> %d\n",
2719 __func__, swrm->pm_state, SWRM_PM_SLEEPABLE);
2720 /*
2721 * if swrm_lock_sleep failed, pm_state would be still
2722 * swrm_PM_ASLEEP, don't overwrite
2723 */
2724 if (likely(swrm->pm_state == SWRM_PM_AWAKE))
2725 swrm->pm_state = SWRM_PM_SLEEPABLE;
2726 pm_qos_update_request(&swrm->pm_qos_req,
2727 PM_QOS_DEFAULT_VALUE);
2728 pm_relax(swrm->dev);
2729 }
2730 mutex_unlock(&swrm->pm_lock);
2731 wake_up_all(&swrm->pm_wq);
2732}
2733
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302734#ifdef CONFIG_PM_SLEEP
2735static int swrm_suspend(struct device *dev)
2736{
2737 int ret = -EBUSY;
2738 struct platform_device *pdev = to_platform_device(dev);
2739 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2740
2741 dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302742
2743 mutex_lock(&swrm->pm_lock);
2744
2745 if (swrm->pm_state == SWRM_PM_SLEEPABLE) {
2746 dev_dbg(swrm->dev, "%s: suspending system, state %d, wlock %d\n",
2747 __func__, swrm->pm_state,
2748 swrm->wlock_holders);
2749 swrm->pm_state = SWRM_PM_ASLEEP;
2750 } else if (swrm->pm_state == SWRM_PM_AWAKE) {
2751 /*
2752 * unlock to wait for pm_state == SWRM_PM_SLEEPABLE
2753 * then set to SWRM_PM_ASLEEP
2754 */
2755 dev_dbg(swrm->dev, "%s: waiting to suspend system, state %d, wlock %d\n",
2756 __func__, swrm->pm_state,
2757 swrm->wlock_holders);
2758 mutex_unlock(&swrm->pm_lock);
2759 if (!(wait_event_timeout(swrm->pm_wq, swrm_pm_cmpxchg(
2760 swrm, SWRM_PM_SLEEPABLE,
2761 SWRM_PM_ASLEEP) ==
2762 SWRM_PM_SLEEPABLE,
2763 msecs_to_jiffies(
2764 SWRM_SYS_SUSPEND_WAIT)))) {
2765 dev_dbg(swrm->dev, "%s: suspend failed state %d, wlock %d\n",
2766 __func__, swrm->pm_state,
2767 swrm->wlock_holders);
2768 return -EBUSY;
2769 } else {
2770 dev_dbg(swrm->dev,
2771 "%s: done, state %d, wlock %d\n",
2772 __func__, swrm->pm_state,
2773 swrm->wlock_holders);
2774 }
2775 mutex_lock(&swrm->pm_lock);
2776 } else if (swrm->pm_state == SWRM_PM_ASLEEP) {
2777 dev_dbg(swrm->dev, "%s: system is already suspended, state %d, wlock %d\n",
2778 __func__, swrm->pm_state,
2779 swrm->wlock_holders);
2780 }
2781
2782 mutex_unlock(&swrm->pm_lock);
2783
2784 if ((!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev))) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302785 ret = swrm_runtime_suspend(dev);
2786 if (!ret) {
2787 /*
2788 * Synchronize runtime-pm and system-pm states:
2789 * At this point, we are already suspended. If
2790 * runtime-pm still thinks its active, then
2791 * make sure its status is in sync with HW
2792 * status. The three below calls let the
2793 * runtime-pm know that we are suspended
2794 * already without re-invoking the suspend
2795 * callback
2796 */
2797 pm_runtime_disable(dev);
2798 pm_runtime_set_suspended(dev);
2799 pm_runtime_enable(dev);
2800 }
2801 }
2802 if (ret == -EBUSY) {
2803 /*
2804 * There is a possibility that some audio stream is active
2805 * during suspend. We dont want to return suspend failure in
2806 * that case so that display and relevant components can still
2807 * go to suspend.
2808 * If there is some other error, then it should be passed-on
2809 * to system level suspend
2810 */
2811 ret = 0;
2812 }
2813 return ret;
2814}
2815
2816static int swrm_resume(struct device *dev)
2817{
2818 int ret = 0;
2819 struct platform_device *pdev = to_platform_device(dev);
2820 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2821
2822 dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
2823 if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
2824 ret = swrm_runtime_resume(dev);
2825 if (!ret) {
2826 pm_runtime_mark_last_busy(dev);
2827 pm_request_autosuspend(dev);
2828 }
2829 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302830 mutex_lock(&swrm->pm_lock);
2831 if (swrm->pm_state == SWRM_PM_ASLEEP) {
2832 dev_dbg(swrm->dev,
2833 "%s: resuming system, state %d, wlock %d\n",
2834 __func__, swrm->pm_state,
2835 swrm->wlock_holders);
2836 swrm->pm_state = SWRM_PM_SLEEPABLE;
2837 } else {
2838 dev_dbg(swrm->dev, "%s: system is already awake, state %d wlock %d\n",
2839 __func__, swrm->pm_state,
2840 swrm->wlock_holders);
2841 }
2842 mutex_unlock(&swrm->pm_lock);
2843 wake_up_all(&swrm->pm_wq);
2844
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302845 return ret;
2846}
2847#endif /* CONFIG_PM_SLEEP */
2848
2849static const struct dev_pm_ops swrm_dev_pm_ops = {
2850 SET_SYSTEM_SLEEP_PM_OPS(
2851 swrm_suspend,
2852 swrm_resume
2853 )
2854 SET_RUNTIME_PM_OPS(
2855 swrm_runtime_suspend,
2856 swrm_runtime_resume,
2857 NULL
2858 )
2859};
2860
2861static const struct of_device_id swrm_dt_match[] = {
2862 {
2863 .compatible = "qcom,swr-mstr",
2864 },
2865 {}
2866};
2867
2868static struct platform_driver swr_mstr_driver = {
2869 .probe = swrm_probe,
2870 .remove = swrm_remove,
2871 .driver = {
2872 .name = SWR_WCD_NAME,
2873 .owner = THIS_MODULE,
2874 .pm = &swrm_dev_pm_ops,
2875 .of_match_table = swrm_dt_match,
2876 },
2877};
2878
2879static int __init swrm_init(void)
2880{
2881 return platform_driver_register(&swr_mstr_driver);
2882}
2883module_init(swrm_init);
2884
2885static void __exit swrm_exit(void)
2886{
2887 platform_driver_unregister(&swr_mstr_driver);
2888}
2889module_exit(swrm_exit);
2890
2891MODULE_LICENSE("GPL v2");
2892MODULE_DESCRIPTION("SoundWire Master Controller");
2893MODULE_ALIAS("platform:swr-mstr");