blob: 45781c76f4ae02968116e8be4ce5bd4091d644b5 [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) {
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530244 if (!swrm->dev_up) {
245 ret = -ENODEV;
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530246 goto exit;
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530247 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530248 swrm->clk_ref_count++;
249 if (swrm->clk_ref_count == 1) {
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530250 ret = swrm->clk(swrm->handle, true);
251 if (ret) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +0530252 dev_err_ratelimited(swrm->dev,
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530253 "%s: clock enable req failed",
254 __func__);
255 --swrm->clk_ref_count;
256 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530257 }
258 } else if (--swrm->clk_ref_count == 0) {
259 swrm->clk(swrm->handle, false);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530260 complete(&swrm->clk_off_complete);
261 }
262 if (swrm->clk_ref_count < 0) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530263 pr_err("%s: swrm clk count mismatch\n", __func__);
264 swrm->clk_ref_count = 0;
265 }
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530266
267exit:
268 mutex_unlock(&swrm->clklock);
269 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530270}
271
272static int swrm_ahb_write(struct swr_mstr_ctrl *swrm,
273 u16 reg, u32 *value)
274{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530275 u32 temp = (u32)(*value);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530276 int ret = 0;
277
278 mutex_lock(&swrm->devlock);
279 if (!swrm->dev_up)
280 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530281
282 ret = swrm_clk_request(swrm, TRUE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530283 if (ret) {
284 dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
285 __func__);
286 goto err;
287 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530288 iowrite32(temp, swrm->swrm_dig_base + reg);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530289 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530290err:
291 mutex_unlock(&swrm->devlock);
292 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530293}
294
295static int swrm_ahb_read(struct swr_mstr_ctrl *swrm,
296 u16 reg, u32 *value)
297{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530298 u32 temp = 0;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530299 int ret = 0;
300
301 mutex_lock(&swrm->devlock);
302 if (!swrm->dev_up)
303 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530304
305 ret = swrm_clk_request(swrm, TRUE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530306 if (ret) {
307 dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
308 __func__);
309 goto err;
310 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530311 temp = ioread32(swrm->swrm_dig_base + reg);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530312 *value = temp;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530313 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530314err:
315 mutex_unlock(&swrm->devlock);
316 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530317}
318
319static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr)
320{
321 u32 val = 0;
322
323 if (swrm->read)
324 val = swrm->read(swrm->handle, reg_addr);
325 else
326 swrm_ahb_read(swrm, reg_addr, &val);
327 return val;
328}
329
330static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val)
331{
332 if (swrm->write)
333 swrm->write(swrm->handle, reg_addr, val);
334 else
335 swrm_ahb_write(swrm, reg_addr, &val);
336}
337
338static int swr_master_bulk_write(struct swr_mstr_ctrl *swrm, u32 *reg_addr,
339 u32 *val, unsigned int length)
340{
341 int i = 0;
342
343 if (swrm->bulk_write)
344 swrm->bulk_write(swrm->handle, reg_addr, val, length);
345 else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530346 mutex_lock(&swrm->iolock);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530347 for (i = 0; i < length; i++) {
348 /* wait for FIFO WR command to complete to avoid overflow */
349 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530350 swr_master_write(swrm, reg_addr[i], val[i]);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530351 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530352 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530353 }
354 return 0;
355}
356
357static bool swrm_is_port_en(struct swr_master *mstr)
358{
359 return !!(mstr->num_port);
360}
361
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530362static void copy_port_tables(struct swr_mstr_ctrl *swrm,
363 struct port_params *params)
364{
365 u8 i;
366 struct port_params *config = params;
367
368 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
369 /* wsa uses single frame structure for all configurations */
370 if (!swrm->mport_cfg[i].port_en)
371 continue;
372 swrm->mport_cfg[i].sinterval = config[i].si;
373 swrm->mport_cfg[i].offset1 = config[i].off1;
374 swrm->mport_cfg[i].offset2 = config[i].off2;
375 swrm->mport_cfg[i].hstart = config[i].hstart;
376 swrm->mport_cfg[i].hstop = config[i].hstop;
377 swrm->mport_cfg[i].blk_pack_mode = config[i].bp_mode;
378 swrm->mport_cfg[i].blk_grp_count = config[i].bgp_ctrl;
379 swrm->mport_cfg[i].word_length = config[i].wd_len;
380 swrm->mport_cfg[i].lane_ctrl = config[i].lane_ctrl;
381 }
382}
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530383static int swrm_get_port_config(struct swr_mstr_ctrl *swrm)
384{
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530385 struct port_params *params;
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530386 u32 usecase = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530387
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530388 /* TODO - Send usecase information to avoid checking for master_id */
389 if (swrm->mport_cfg[SWRM_DSD_PARAMS_PORT].port_en &&
390 (swrm->master_id == MASTER_ID_RX))
391 usecase = 1;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530392
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530393 params = swrm->port_param[usecase];
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530394 copy_port_tables(swrm, params);
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530395
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530396 return 0;
397}
398
399static int swrm_get_master_port(struct swr_mstr_ctrl *swrm, u8 *mstr_port_id,
400 u8 *mstr_ch_mask, u8 mstr_prt_type,
401 u8 slv_port_id)
402{
403 int i, j;
404 *mstr_port_id = 0;
405
406 for (i = 1; i <= swrm->num_ports; i++) {
407 for (j = 0; j < SWR_MAX_CH_PER_PORT; j++) {
408 if (swrm->port_mapping[i][j].port_type == mstr_prt_type)
409 goto found;
410 }
411 }
412found:
413 if (i > swrm->num_ports || j == SWR_MAX_CH_PER_PORT) {
414 dev_err(swrm->dev, "%s: port type not supported by master\n",
415 __func__);
416 return -EINVAL;
417 }
418 /* id 0 corresponds to master port 1 */
419 *mstr_port_id = i - 1;
420 *mstr_ch_mask = swrm->port_mapping[i][j].ch_mask;
421
422 return 0;
423
424}
425
426static u32 swrm_get_packed_reg_val(u8 *cmd_id, u8 cmd_data,
427 u8 dev_addr, u16 reg_addr)
428{
429 u32 val;
430 u8 id = *cmd_id;
431
432 if (id != SWR_BROADCAST_CMD_ID) {
433 if (id < 14)
434 id += 1;
435 else
436 id = 0;
437 *cmd_id = id;
438 }
439 val = SWR_REG_VAL_PACK(cmd_data, dev_addr, id, reg_addr);
440
441 return val;
442}
443
444static int swrm_cmd_fifo_rd_cmd(struct swr_mstr_ctrl *swrm, int *cmd_data,
445 u8 dev_addr, u8 cmd_id, u16 reg_addr,
446 u32 len)
447{
448 u32 val;
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530449 u32 retry_attempt = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530450
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530451 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530452 val = swrm_get_packed_reg_val(&swrm->rcmd_id, len, dev_addr, reg_addr);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530453 if (swrm->read) {
454 /* skip delay if read is handled in platform driver */
455 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
456 } else {
457 /* wait for FIFO RD to complete to avoid overflow */
458 usleep_range(100, 105);
459 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
460 /* wait for FIFO RD CMD complete to avoid overflow */
461 usleep_range(250, 255);
462 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530463retry_read:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530464 *cmd_data = swr_master_read(swrm, SWRM_CMD_FIFO_RD_FIFO_ADDR);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530465 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, rcmd_id: 0x%x, \
466 dev_num: 0x%x, cmd_data: 0x%x\n", __func__, reg_addr,
467 cmd_id, swrm->rcmd_id, dev_addr, *cmd_data);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530468 if ((((*cmd_data) & 0xF00) >> 8) != swrm->rcmd_id) {
469 if (retry_attempt < MAX_FIFO_RD_FAIL_RETRY) {
470 /* wait 500 us before retry on fifo read failure */
471 usleep_range(500, 505);
472 retry_attempt++;
473 goto retry_read;
474 } else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530475 dev_err_ratelimited(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, \
476 rcmd_id: 0x%x, dev_num: 0x%x, cmd_data: 0x%x\n",
477 __func__, reg_addr, cmd_id, swrm->rcmd_id,
478 dev_addr, *cmd_data);
479
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530480 dev_err_ratelimited(swrm->dev,
481 "%s: failed to read fifo\n", __func__);
482 }
483 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530484 mutex_unlock(&swrm->iolock);
485
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530486 return 0;
487}
488
489static int swrm_cmd_fifo_wr_cmd(struct swr_mstr_ctrl *swrm, u8 cmd_data,
490 u8 dev_addr, u8 cmd_id, u16 reg_addr)
491{
492 u32 val;
493 int ret = 0;
494
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530495 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530496 if (!cmd_id)
497 val = swrm_get_packed_reg_val(&swrm->wcmd_id, cmd_data,
498 dev_addr, reg_addr);
499 else
500 val = swrm_get_packed_reg_val(&cmd_id, cmd_data,
501 dev_addr, reg_addr);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530502 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x,wcmd_id: 0x%x, \
503 dev_num: 0x%x, cmd_data: 0x%x\n", __func__,
504 reg_addr, cmd_id, swrm->wcmd_id,dev_addr, cmd_data);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +0530505 swr_master_write(swrm, SWRM_CMD_FIFO_WR_CMD, val);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530506 /*
507 * wait for FIFO WR command to complete to avoid overflow
508 * skip delay if write is handled in platform driver.
509 */
510 if(!swrm->write)
511 usleep_range(250, 255);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530512 if (cmd_id == 0xF) {
513 /*
514 * sleep for 10ms for MSM soundwire variant to allow broadcast
515 * command to complete.
516 */
517 if (swrm_is_msm_variant(swrm->version))
518 usleep_range(10000, 10100);
519 else
520 wait_for_completion_timeout(&swrm->broadcast,
521 (2 * HZ/10));
522 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530523 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530524 return ret;
525}
526
527static int swrm_read(struct swr_master *master, u8 dev_num, u16 reg_addr,
528 void *buf, u32 len)
529{
530 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
531 int ret = 0;
532 int val;
533 u8 *reg_val = (u8 *)buf;
534
535 if (!swrm) {
536 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
537 return -EINVAL;
538 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530539 if (!dev_num) {
540 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
541 return -EINVAL;
542 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530543 mutex_lock(&swrm->devlock);
544 if (!swrm->dev_up) {
545 mutex_unlock(&swrm->devlock);
546 return 0;
547 }
548 mutex_unlock(&swrm->devlock);
549
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530550 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530551 ret = swrm_cmd_fifo_rd_cmd(swrm, &val, dev_num, 0, reg_addr, len);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530552
553 if (!ret)
554 *reg_val = (u8)val;
555
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530556 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530557 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530558 return ret;
559}
560
561static int swrm_write(struct swr_master *master, u8 dev_num, u16 reg_addr,
562 const void *buf)
563{
564 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
565 int ret = 0;
566 u8 reg_val = *(u8 *)buf;
567
568 if (!swrm) {
569 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
570 return -EINVAL;
571 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530572 if (!dev_num) {
573 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
574 return -EINVAL;
575 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530576 mutex_lock(&swrm->devlock);
577 if (!swrm->dev_up) {
578 mutex_unlock(&swrm->devlock);
579 return 0;
580 }
581 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530582
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530583 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530584 ret = swrm_cmd_fifo_wr_cmd(swrm, reg_val, dev_num, 0, reg_addr);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530585
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530586 pm_runtime_put_autosuspend(swrm->dev);
587 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530588 return ret;
589}
590
591static int swrm_bulk_write(struct swr_master *master, u8 dev_num, void *reg,
592 const void *buf, size_t len)
593{
594 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
595 int ret = 0;
596 int i;
597 u32 *val;
598 u32 *swr_fifo_reg;
599
600 if (!swrm || !swrm->handle) {
601 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
602 return -EINVAL;
603 }
604 if (len <= 0)
605 return -EINVAL;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530606 mutex_lock(&swrm->devlock);
607 if (!swrm->dev_up) {
608 mutex_unlock(&swrm->devlock);
609 return 0;
610 }
611 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530612
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530613 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530614 if (dev_num) {
615 swr_fifo_reg = kcalloc(len, sizeof(u32), GFP_KERNEL);
616 if (!swr_fifo_reg) {
617 ret = -ENOMEM;
618 goto err;
619 }
620 val = kcalloc(len, sizeof(u32), GFP_KERNEL);
621 if (!val) {
622 ret = -ENOMEM;
623 goto mem_fail;
624 }
625
626 for (i = 0; i < len; i++) {
627 val[i] = swrm_get_packed_reg_val(&swrm->wcmd_id,
628 ((u8 *)buf)[i],
629 dev_num,
630 ((u16 *)reg)[i]);
631 swr_fifo_reg[i] = SWRM_CMD_FIFO_WR_CMD;
632 }
633 ret = swr_master_bulk_write(swrm, swr_fifo_reg, val, len);
634 if (ret) {
635 dev_err(&master->dev, "%s: bulk write failed\n",
636 __func__);
637 ret = -EINVAL;
638 }
639 } else {
640 dev_err(&master->dev,
641 "%s: No support of Bulk write for master regs\n",
642 __func__);
643 ret = -EINVAL;
644 goto err;
645 }
646 kfree(val);
647mem_fail:
648 kfree(swr_fifo_reg);
649err:
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530650 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530651 pm_runtime_mark_last_busy(swrm->dev);
652 return ret;
653}
654
655static u8 get_inactive_bank_num(struct swr_mstr_ctrl *swrm)
656{
657 return (swr_master_read(swrm, SWRM_MCP_STATUS) &
658 SWRM_MCP_STATUS_BANK_NUM_MASK) ? 0 : 1;
659}
660
661static void enable_bank_switch(struct swr_mstr_ctrl *swrm, u8 bank,
662 u8 row, u8 col)
663{
664 swrm_cmd_fifo_wr_cmd(swrm, ((row << 3) | col), 0xF, 0xF,
665 SWRS_SCP_FRAME_CTRL_BANK(bank));
666}
667
668static struct swr_port_info *swrm_get_port_req(struct swrm_mports *mport,
669 u8 slv_port, u8 dev_num)
670{
671 struct swr_port_info *port_req = NULL;
672
673 list_for_each_entry(port_req, &mport->port_req_list, list) {
674 /* Store dev_id instead of dev_num if enumeration is changed run_time */
675 if ((port_req->slave_port_id == slv_port)
676 && (port_req->dev_num == dev_num))
677 return port_req;
678 }
679 return NULL;
680}
681
682static bool swrm_remove_from_group(struct swr_master *master)
683{
684 struct swr_device *swr_dev;
685 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
686 bool is_removed = false;
687
688 if (!swrm)
689 goto end;
690
691 mutex_lock(&swrm->mlock);
692 if ((swrm->num_rx_chs > 1) &&
693 (swrm->num_rx_chs == swrm->num_cfg_devs)) {
694 list_for_each_entry(swr_dev, &master->devices,
695 dev_list) {
696 swr_dev->group_id = SWR_GROUP_NONE;
697 master->gr_sid = 0;
698 }
699 is_removed = true;
700 }
701 mutex_unlock(&swrm->mlock);
702
703end:
704 return is_removed;
705}
706
707static void swrm_disable_ports(struct swr_master *master,
708 u8 bank)
709{
710 u32 value;
711 struct swr_port_info *port_req;
712 int i;
713 struct swrm_mports *mport;
714 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
715
716 if (!swrm) {
717 pr_err("%s: swrm is null\n", __func__);
718 return;
719 }
720
721 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
722 master->num_port);
723
724
725 for (i = 0; i < SWR_MSTR_PORT_LEN ; i++) {
726
727 mport = &(swrm->mport_cfg[i]);
728 if (!mport->port_en)
729 continue;
730
731 list_for_each_entry(port_req, &mport->port_req_list, list) {
732 /* skip ports with no change req's*/
733 if (port_req->req_ch == port_req->ch_en)
734 continue;
735
736 swrm_cmd_fifo_wr_cmd(swrm, port_req->req_ch,
737 port_req->dev_num, 0x00,
738 SWRS_DP_CHANNEL_ENABLE_BANK(port_req->slave_port_id,
739 bank));
740 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x\n",
741 __func__, i,
742 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)));
743 }
744 value = ((mport->req_ch)
745 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
746 value |= ((mport->offset2)
747 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
748 value |= ((mport->offset1)
749 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
750 value |= mport->sinterval;
751
752 swr_master_write(swrm,
753 SWRM_DP_PORT_CTRL_BANK(i+1, bank),
754 value);
755 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
756 __func__, i,
757 (SWRM_DP_PORT_CTRL_BANK(i+1, bank)), value);
758 }
759}
760
761static void swrm_cleanup_disabled_port_reqs(struct swr_master *master)
762{
763 struct swr_port_info *port_req, *next;
764 int i;
765 struct swrm_mports *mport;
766 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
767
768 if (!swrm) {
769 pr_err("%s: swrm is null\n", __func__);
770 return;
771 }
772 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
773 master->num_port);
774
775 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
776 mport = &(swrm->mport_cfg[i]);
777 list_for_each_entry_safe(port_req, next,
778 &mport->port_req_list, list) {
779 /* skip ports without new ch req */
780 if (port_req->ch_en == port_req->req_ch)
781 continue;
782
783 /* remove new ch req's*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +0530784 port_req->ch_en = port_req->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530785
786 /* If no streams enabled on port, remove the port req */
787 if (port_req->ch_en == 0) {
788 list_del(&port_req->list);
789 kfree(port_req);
790 }
791 }
792 /* remove new ch req's on mport*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +0530793 mport->ch_en = mport->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530794
795 if (!(mport->ch_en)) {
796 mport->port_en = false;
797 master->port_en_mask &= ~i;
798 }
799 }
800}
801static void swrm_copy_data_port_config(struct swr_master *master, u8 bank)
802{
803 u32 value, slv_id;
804 struct swr_port_info *port_req;
805 int i;
806 struct swrm_mports *mport;
807 u32 reg[SWRM_MAX_PORT_REG];
808 u32 val[SWRM_MAX_PORT_REG];
809 int len = 0;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530810 u8 hparams;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530811 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
812
813 if (!swrm) {
814 pr_err("%s: swrm is null\n", __func__);
815 return;
816 }
817
818 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
819 master->num_port);
820
821 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
822 mport = &(swrm->mport_cfg[i]);
823 if (!mport->port_en)
824 continue;
825
826 list_for_each_entry(port_req, &mport->port_req_list, list) {
827 slv_id = port_req->slave_port_id;
828 reg[len] = SWRM_CMD_FIFO_WR_CMD;
829 val[len++] = SWR_REG_VAL_PACK(port_req->req_ch,
830 port_req->dev_num, 0x00,
831 SWRS_DP_CHANNEL_ENABLE_BANK(slv_id,
832 bank));
833
834 reg[len] = SWRM_CMD_FIFO_WR_CMD;
835 val[len++] = SWR_REG_VAL_PACK(mport->sinterval,
836 port_req->dev_num, 0x00,
837 SWRS_DP_SAMPLE_CONTROL_1_BANK(slv_id,
838 bank));
839
840 reg[len] = SWRM_CMD_FIFO_WR_CMD;
841 val[len++] = SWR_REG_VAL_PACK(mport->offset1,
842 port_req->dev_num, 0x00,
843 SWRS_DP_OFFSET_CONTROL_1_BANK(slv_id,
844 bank));
845
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530846 if (mport->offset2 != SWR_INVALID_PARAM) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530847 reg[len] = SWRM_CMD_FIFO_WR_CMD;
848 val[len++] = SWR_REG_VAL_PACK(mport->offset2,
849 port_req->dev_num, 0x00,
850 SWRS_DP_OFFSET_CONTROL_2_BANK(
851 slv_id, bank));
852 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530853 if (mport->hstart != SWR_INVALID_PARAM
854 && mport->hstop != SWR_INVALID_PARAM) {
855 hparams = (mport->hstart << 4) | mport->hstop;
856
857 reg[len] = SWRM_CMD_FIFO_WR_CMD;
858 val[len++] = SWR_REG_VAL_PACK(hparams,
859 port_req->dev_num, 0x00,
860 SWRS_DP_HCONTROL_BANK(slv_id,
861 bank));
862 }
863 if (mport->word_length != SWR_INVALID_PARAM) {
864 reg[len] = SWRM_CMD_FIFO_WR_CMD;
865 val[len++] =
866 SWR_REG_VAL_PACK(mport->word_length,
867 port_req->dev_num, 0x00,
868 SWRS_DP_BLOCK_CONTROL_1(slv_id));
869 }
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +0530870 if (mport->blk_pack_mode != SWR_INVALID_PARAM
871 && swrm->master_id != MASTER_ID_WSA) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530872 reg[len] = SWRM_CMD_FIFO_WR_CMD;
873 val[len++] =
874 SWR_REG_VAL_PACK(mport->blk_pack_mode,
875 port_req->dev_num, 0x00,
876 SWRS_DP_BLOCK_CONTROL_3_BANK(slv_id,
877 bank));
878 }
879 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
880 reg[len] = SWRM_CMD_FIFO_WR_CMD;
881 val[len++] =
882 SWR_REG_VAL_PACK(mport->blk_grp_count,
883 port_req->dev_num, 0x00,
884 SWRS_DP_BLOCK_CONTROL_2_BANK(slv_id,
885 bank));
886 }
887 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
888 reg[len] = SWRM_CMD_FIFO_WR_CMD;
889 val[len++] =
890 SWR_REG_VAL_PACK(mport->lane_ctrl,
891 port_req->dev_num, 0x00,
892 SWRS_DP_LANE_CONTROL_BANK(slv_id,
893 bank));
894 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530895 port_req->ch_en = port_req->req_ch;
896 }
897 value = ((mport->req_ch)
898 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +0530899
900 if (mport->offset2 != SWR_INVALID_PARAM)
901 value |= ((mport->offset2)
902 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530903 value |= ((mport->offset1)
904 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
905 value |= mport->sinterval;
906
907
908 reg[len] = SWRM_DP_PORT_CTRL_BANK(i + 1, bank);
909 val[len++] = value;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530910 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
911 __func__, i,
912 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)), value);
913
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530914 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
915 reg[len] = SWRM_DP_PORT_CTRL_2_BANK(i + 1, bank);
916 val[len++] = mport->lane_ctrl;
917 }
918 if (mport->word_length != SWR_INVALID_PARAM) {
919 reg[len] = SWRM_DP_BLOCK_CTRL_1(i + 1);
920 val[len++] = mport->word_length;
921 }
922
923 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
924 reg[len] = SWRM_DP_BLOCK_CTRL2_BANK(i + 1, bank);
925 val[len++] = mport->blk_grp_count;
926 }
927 if (mport->hstart != SWR_INVALID_PARAM
928 && mport->hstop != SWR_INVALID_PARAM) {
929 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
Laxminath Kasame30eef72018-11-05 17:40:09 +0530930 hparams = (mport->hstop << 4) | mport->hstart;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530931 val[len++] = hparams;
Laxminath Kasam990c70b2018-11-09 23:15:09 +0530932 } else {
933 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
934 hparams = (SWR_HSTOP_MAX_VAL << 4) | SWR_HSTART_MIN_VAL;
935 val[len++] = hparams;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530936 }
937 if (mport->blk_pack_mode != SWR_INVALID_PARAM) {
938 reg[len] = SWRM_DP_BLOCK_CTRL3_BANK(i + 1, bank);
939 val[len++] = mport->blk_pack_mode;
940 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530941 mport->ch_en = mport->req_ch;
942
943 }
944 swr_master_bulk_write(swrm, reg, val, len);
945}
946
947static void swrm_apply_port_config(struct swr_master *master)
948{
949 u8 bank;
950 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
951
952 if (!swrm) {
953 pr_err("%s: Invalid handle to swr controller\n",
954 __func__);
955 return;
956 }
957
958 bank = get_inactive_bank_num(swrm);
959 dev_dbg(swrm->dev, "%s: enter bank: %d master_ports: %d\n",
960 __func__, bank, master->num_port);
961
962
963 swrm_cmd_fifo_wr_cmd(swrm, 0x01, 0xF, 0x00,
964 SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(bank));
965
966 swrm_copy_data_port_config(master, bank);
967}
968
969static int swrm_slvdev_datapath_control(struct swr_master *master, bool enable)
970{
971 u8 bank;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530972 u32 value, n_row, n_col;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530973 int ret;
974 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
975 int mask = (SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK |
976 SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK |
977 SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_BMSK);
978 u8 inactive_bank;
979
980 if (!swrm) {
981 pr_err("%s: swrm is null\n", __func__);
982 return -EFAULT;
983 }
Ramprasad Katkam7e354782018-11-21 15:52:54 +0530984
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530985 mutex_lock(&swrm->mlock);
986
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530987 bank = get_inactive_bank_num(swrm);
988
989 if (enable) {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +0530990 if (!test_bit(ENABLE_PENDING, &swrm->port_req_pending)) {
991 dev_dbg(swrm->dev, "%s:No pending connect port req\n",
992 __func__);
993 goto exit;
994 }
995 clear_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530996 ret = swrm_get_port_config(swrm);
997 if (ret) {
998 /* cannot accommodate ports */
999 swrm_cleanup_disabled_port_reqs(master);
1000 mutex_unlock(&swrm->mlock);
1001 return -EINVAL;
1002 }
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301003 swr_master_write(swrm, SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301004 SWRM_INTERRUPT_STATUS_MASK);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301005 /* apply the new port config*/
1006 swrm_apply_port_config(master);
1007 } else {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301008 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1009 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1010 __func__);
1011 goto exit;
1012 }
1013 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301014 swrm_disable_ports(master, bank);
1015 }
1016 dev_dbg(swrm->dev, "%s: enable: %d, cfg_devs: %d\n",
1017 __func__, enable, swrm->num_cfg_devs);
1018
1019 if (enable) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301020 /* set col = 16 */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301021 n_col = SWR_MAX_COL;
1022 } else {
1023 /*
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301024 * Do not change to col = 2 if there are still active ports
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301025 */
1026 if (!master->num_port)
1027 n_col = SWR_MIN_COL;
1028 else
1029 n_col = SWR_MAX_COL;
1030 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301031 /* Use default 50 * x, frame shape. Change based on mclk */
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301032 if (swrm->mclk_freq == MCLK_FREQ_NATIVE) {
1033 dev_dbg(swrm->dev, "setting 64 x %d frameshape\n",
1034 n_col ? 16 : 2);
1035 n_row = SWR_ROW_64;
1036 } else {
1037 dev_dbg(swrm->dev, "setting 50 x %d frameshape\n",
1038 n_col ? 16 : 2);
1039 n_row = SWR_ROW_50;
1040 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301041 value = swr_master_read(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank));
1042 value &= (~mask);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301043 value |= ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301044 (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
1045 (0 << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
1046 swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1047
1048 dev_dbg(swrm->dev, "%s: regaddr: 0x%x, value: 0x%x\n", __func__,
1049 SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1050
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301051 enable_bank_switch(swrm, bank, n_row, n_col);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301052 inactive_bank = bank ? 0 : 1;
1053
1054 if (enable)
1055 swrm_copy_data_port_config(master, inactive_bank);
1056 else {
1057 swrm_disable_ports(master, inactive_bank);
1058 swrm_cleanup_disabled_port_reqs(master);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301059 }
1060 if (!swrm_is_port_en(master)) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301061 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1062 __func__);
1063 pm_runtime_mark_last_busy(swrm->dev);
1064 pm_runtime_put_autosuspend(swrm->dev);
1065 }
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301066exit:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301067 mutex_unlock(&swrm->mlock);
1068return 0;
1069}
1070
1071static int swrm_connect_port(struct swr_master *master,
1072 struct swr_params *portinfo)
1073{
1074 int i;
1075 struct swr_port_info *port_req;
1076 int ret = 0;
1077 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1078 struct swrm_mports *mport;
1079 u8 mstr_port_id, mstr_ch_msk;
1080
1081 dev_dbg(&master->dev, "%s: enter\n", __func__);
1082 if (!portinfo)
1083 return -EINVAL;
1084
1085 if (!swrm) {
1086 dev_err(&master->dev,
1087 "%s: Invalid handle to swr controller\n",
1088 __func__);
1089 return -EINVAL;
1090 }
1091
1092 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301093 mutex_lock(&swrm->devlock);
1094 if (!swrm->dev_up) {
1095 mutex_unlock(&swrm->devlock);
1096 mutex_unlock(&swrm->mlock);
1097 return -EINVAL;
1098 }
1099 mutex_unlock(&swrm->devlock);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301100 if (!swrm_is_port_en(master))
1101 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301102
1103 for (i = 0; i < portinfo->num_port; i++) {
1104 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_msk,
1105 portinfo->port_type[i],
1106 portinfo->port_id[i]);
1107 if (ret) {
1108 dev_err(&master->dev,
1109 "%s: mstr portid for slv port %d not found\n",
1110 __func__, portinfo->port_id[i]);
1111 goto port_fail;
1112 }
1113
1114 mport = &(swrm->mport_cfg[mstr_port_id]);
1115 /* get port req */
1116 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1117 portinfo->dev_num);
1118 if (!port_req) {
1119 dev_dbg(&master->dev, "%s: new req:port id %d dev %d\n",
1120 __func__, portinfo->port_id[i],
1121 portinfo->dev_num);
1122 port_req = kzalloc(sizeof(struct swr_port_info),
1123 GFP_KERNEL);
1124 if (!port_req) {
1125 ret = -ENOMEM;
1126 goto mem_fail;
1127 }
1128 port_req->dev_num = portinfo->dev_num;
1129 port_req->slave_port_id = portinfo->port_id[i];
1130 port_req->num_ch = portinfo->num_ch[i];
1131 port_req->ch_rate = portinfo->ch_rate[i];
1132 port_req->ch_en = 0;
1133 port_req->master_port_id = mstr_port_id;
1134 list_add(&port_req->list, &mport->port_req_list);
1135 }
1136 port_req->req_ch |= portinfo->ch_en[i];
1137
1138 dev_dbg(&master->dev,
1139 "%s: mstr port %d, slv port %d ch_rate %d num_ch %d\n",
1140 __func__, port_req->master_port_id,
1141 port_req->slave_port_id, port_req->ch_rate,
1142 port_req->num_ch);
1143 /* Put the port req on master port */
1144 mport = &(swrm->mport_cfg[mstr_port_id]);
1145 mport->port_en = true;
1146 mport->req_ch |= mstr_ch_msk;
1147 master->port_en_mask |= (1 << mstr_port_id);
1148 }
1149 master->num_port += portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301150 set_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301151 swr_port_response(master, portinfo->tid);
1152
1153 mutex_unlock(&swrm->mlock);
1154 return 0;
1155
1156port_fail:
1157mem_fail:
1158 /* cleanup port reqs in error condition */
1159 swrm_cleanup_disabled_port_reqs(master);
1160 mutex_unlock(&swrm->mlock);
1161 return ret;
1162}
1163
1164static int swrm_disconnect_port(struct swr_master *master,
1165 struct swr_params *portinfo)
1166{
1167 int i, ret = 0;
1168 struct swr_port_info *port_req;
1169 struct swrm_mports *mport;
1170 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1171 u8 mstr_port_id, mstr_ch_mask;
1172
1173 if (!swrm) {
1174 dev_err(&master->dev,
1175 "%s: Invalid handle to swr controller\n",
1176 __func__);
1177 return -EINVAL;
1178 }
1179
1180 if (!portinfo) {
1181 dev_err(&master->dev, "%s: portinfo is NULL\n", __func__);
1182 return -EINVAL;
1183 }
1184 mutex_lock(&swrm->mlock);
1185
1186 for (i = 0; i < portinfo->num_port; i++) {
1187
1188 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_mask,
1189 portinfo->port_type[i], portinfo->port_id[i]);
1190 if (ret) {
1191 dev_err(&master->dev,
1192 "%s: mstr portid for slv port %d not found\n",
1193 __func__, portinfo->port_id[i]);
1194 mutex_unlock(&swrm->mlock);
1195 return -EINVAL;
1196 }
1197 mport = &(swrm->mport_cfg[mstr_port_id]);
1198 /* get port req */
1199 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1200 portinfo->dev_num);
1201
1202 if (!port_req) {
1203 dev_err(&master->dev, "%s:port not enabled : port %d\n",
1204 __func__, portinfo->port_id[i]);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05301205 mutex_unlock(&swrm->mlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301206 return -EINVAL;
1207 }
1208 port_req->req_ch &= ~portinfo->ch_en[i];
1209 mport->req_ch &= ~mstr_ch_mask;
1210 }
1211 master->num_port -= portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301212 set_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301213 swr_port_response(master, portinfo->tid);
1214 mutex_unlock(&swrm->mlock);
1215
1216 return 0;
1217}
1218
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301219static int swrm_find_alert_slave(struct swr_mstr_ctrl *swrm,
1220 int status, u8 *devnum)
1221{
1222 int i;
1223 bool found = false;
1224
1225 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1226 if ((status & SWRM_MCP_SLV_STATUS_MASK) == SWR_ALERT) {
1227 *devnum = i;
1228 found = true;
1229 break;
1230 }
1231 status >>= 2;
1232 }
1233 if (found)
1234 return 0;
1235 else
1236 return -EINVAL;
1237}
1238
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301239static int swrm_check_slave_change_status(struct swr_mstr_ctrl *swrm,
1240 int status, u8 *devnum)
1241{
1242 int i;
1243 int new_sts = status;
1244 int ret = SWR_NOT_PRESENT;
1245
1246 if (status != swrm->slave_status) {
1247 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1248 if ((status & SWRM_MCP_SLV_STATUS_MASK) !=
1249 (swrm->slave_status & SWRM_MCP_SLV_STATUS_MASK)) {
1250 ret = (status & SWRM_MCP_SLV_STATUS_MASK);
1251 *devnum = i;
1252 break;
1253 }
1254 status >>= 2;
1255 swrm->slave_status >>= 2;
1256 }
1257 swrm->slave_status = new_sts;
1258 }
1259 return ret;
1260}
1261
1262static irqreturn_t swr_mstr_interrupt(int irq, void *dev)
1263{
1264 struct swr_mstr_ctrl *swrm = dev;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301265 u32 value, intr_sts, intr_sts_masked;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301266 u32 temp = 0;
1267 u32 status, chg_sts, i;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301268 u8 devnum = 0;
1269 int ret = IRQ_HANDLED;
1270 struct swr_device *swr_dev;
1271 struct swr_master *mstr = &swrm->master;
1272
Ramprasad Katkam57349872018-11-11 18:34:57 +05301273 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1274 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1275 return IRQ_NONE;
1276 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301277
1278 mutex_lock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301279 if (swrm_clk_request(swrm, true)) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +05301280 dev_err_ratelimited(swrm->dev, "%s:clk request failed\n",
1281 __func__);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301282 mutex_unlock(&swrm->reslock);
1283 goto exit;
1284 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301285 mutex_unlock(&swrm->reslock);
1286
1287 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301288 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301289handle_irq:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301290 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301291 value = intr_sts_masked & (1 << i);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301292 if (!value)
1293 continue;
1294
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301295 switch (value) {
1296 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1297 dev_dbg(swrm->dev, "Trigger irq to slave device\n");
1298 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301299 ret = swrm_find_alert_slave(swrm, status, &devnum);
1300 if (ret) {
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301301 dev_err_ratelimited(swrm->dev,
1302 "no slave alert found.spurious interrupt\n");
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05301303 break;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301304 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301305 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1306 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1307 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1308 SWRS_SCP_INT_STATUS_CLEAR_1);
1309 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1310 SWRS_SCP_INT_STATUS_CLEAR_1);
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301311
1312
1313 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1314 if (swr_dev->dev_num != devnum)
1315 continue;
1316 if (swr_dev->slave_irq) {
1317 do {
1318 handle_nested_irq(
1319 irq_find_mapping(
1320 swr_dev->slave_irq, 0));
1321 } while (swr_dev->slave_irq_pending);
1322 }
1323
1324 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301325 break;
1326 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1327 dev_dbg(swrm->dev, "SWR new slave attached\n");
1328 break;
1329 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1330 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1331 if (status == swrm->slave_status) {
1332 dev_dbg(swrm->dev,
1333 "%s: No change in slave status: %d\n",
1334 __func__, status);
1335 break;
1336 }
1337 chg_sts = swrm_check_slave_change_status(swrm, status,
1338 &devnum);
1339 switch (chg_sts) {
1340 case SWR_NOT_PRESENT:
1341 dev_dbg(swrm->dev, "device %d got detached\n",
1342 devnum);
1343 break;
1344 case SWR_ATTACHED_OK:
1345 dev_dbg(swrm->dev, "device %d got attached\n",
1346 devnum);
Ramprasad Katkamdebe8932018-09-25 18:08:18 +05301347 /* enable host irq from slave device*/
1348 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1349 SWRS_SCP_INT_STATUS_CLEAR_1);
1350 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1351 SWRS_SCP_INT_STATUS_MASK_1);
1352
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301353 break;
1354 case SWR_ALERT:
1355 dev_dbg(swrm->dev,
1356 "device %d has pending interrupt\n",
1357 devnum);
1358 break;
1359 }
1360 break;
1361 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1362 dev_err_ratelimited(swrm->dev,
1363 "SWR bus clsh detected\n");
1364 break;
1365 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1366 dev_dbg(swrm->dev, "SWR read FIFO overflow\n");
1367 break;
1368 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1369 dev_dbg(swrm->dev, "SWR read FIFO underflow\n");
1370 break;
1371 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1372 dev_dbg(swrm->dev, "SWR write FIFO overflow\n");
1373 break;
1374 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1375 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1376 dev_err_ratelimited(swrm->dev,
1377 "SWR CMD error, fifo status 0x%x, flushing fifo\n",
1378 value);
1379 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1380 break;
1381 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301382 dev_err_ratelimited(swrm->dev, "SWR Port collision detected\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301383 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301384 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_READ_EN_RD_VALID_MISMATCH:
1388 dev_dbg(swrm->dev, "SWR read enable valid mismatch\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301389 swrm->intr_mask &=
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301390 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1391 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301392 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301393 break;
1394 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1395 complete(&swrm->broadcast);
1396 dev_dbg(swrm->dev, "SWR cmd id finished\n");
1397 break;
1398 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_AUTO_ENUM_FINISHED:
1399 break;
1400 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED:
1401 break;
1402 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL:
1403 break;
1404 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED:
1405 complete(&swrm->reset);
1406 break;
1407 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED:
1408 break;
1409 default:
1410 dev_err_ratelimited(swrm->dev,
1411 "SWR unknown interrupt\n");
1412 ret = IRQ_NONE;
1413 break;
1414 }
1415 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301416 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1417 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
Ramprasad Katkam83303512018-10-11 17:34:22 +05301418
1419 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301420 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301421
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301422 if (intr_sts_masked) {
Ramprasad Katkam83303512018-10-11 17:34:22 +05301423 dev_dbg(swrm->dev, "%s: new interrupt received\n", __func__);
1424 goto handle_irq;
1425 }
1426
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301427 mutex_lock(&swrm->reslock);
1428 swrm_clk_request(swrm, false);
1429 mutex_unlock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301430exit:
Ramprasad Katkam57349872018-11-11 18:34:57 +05301431 swrm_unlock_sleep(swrm);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301432 return ret;
1433}
1434
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301435static irqreturn_t swr_mstr_interrupt_v2(int irq, void *dev)
1436{
1437 struct swr_mstr_ctrl *swrm = dev;
1438 u32 value, intr_sts, intr_sts_masked;
1439 u32 temp = 0;
1440 u32 status, chg_sts, i;
1441 u8 devnum = 0;
1442 int ret = IRQ_HANDLED;
1443 struct swr_device *swr_dev;
1444 struct swr_master *mstr = &swrm->master;
1445
1446 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1447 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1448 return IRQ_NONE;
1449 }
1450
1451 mutex_lock(&swrm->reslock);
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001452 if (swrm->lpass_core_hw_vote) {
1453 ret = clk_prepare_enable(swrm->lpass_core_hw_vote);
1454 if (ret < 0) {
1455 dev_err(dev, "%s:lpass core hw enable failed\n",
1456 __func__);
1457 ret = IRQ_NONE;
1458 goto exit;
1459 }
1460 }
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301461 swrm_clk_request(swrm, true);
1462 mutex_unlock(&swrm->reslock);
1463
1464 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1465 intr_sts_masked = intr_sts & swrm->intr_mask;
1466handle_irq:
1467 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
1468 value = intr_sts_masked & (1 << i);
1469 if (!value)
1470 continue;
1471
1472 switch (value) {
1473 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1474 dev_dbg(swrm->dev, "%s: Trigger irq to slave device\n",
1475 __func__);
1476 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1477 ret = swrm_find_alert_slave(swrm, status, &devnum);
1478 if (ret) {
1479 dev_err_ratelimited(swrm->dev,
1480 "%s: no slave alert found.spurious interrupt\n",
1481 __func__);
1482 break;
1483 }
1484 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1485 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1486 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1487 SWRS_SCP_INT_STATUS_CLEAR_1);
1488 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1489 SWRS_SCP_INT_STATUS_CLEAR_1);
1490
1491
1492 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1493 if (swr_dev->dev_num != devnum)
1494 continue;
1495 if (swr_dev->slave_irq) {
1496 do {
1497 handle_nested_irq(
1498 irq_find_mapping(
1499 swr_dev->slave_irq, 0));
1500 } while (swr_dev->slave_irq_pending);
1501 }
1502
1503 }
1504 break;
1505 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1506 dev_dbg(swrm->dev, "%s: SWR new slave attached\n",
1507 __func__);
1508 break;
1509 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1510 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1511 if (status == swrm->slave_status) {
1512 dev_dbg(swrm->dev,
1513 "%s: No change in slave status: %d\n",
1514 __func__, status);
1515 break;
1516 }
1517 chg_sts = swrm_check_slave_change_status(swrm, status,
1518 &devnum);
1519 switch (chg_sts) {
1520 case SWR_NOT_PRESENT:
1521 dev_dbg(swrm->dev,
1522 "%s: device %d got detached\n",
1523 __func__, devnum);
1524 break;
1525 case SWR_ATTACHED_OK:
1526 dev_dbg(swrm->dev,
1527 "%s: device %d got attached\n",
1528 __func__, devnum);
1529 /* enable host irq from slave device*/
1530 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1531 SWRS_SCP_INT_STATUS_CLEAR_1);
1532 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1533 SWRS_SCP_INT_STATUS_MASK_1);
1534
1535 break;
1536 case SWR_ALERT:
1537 dev_dbg(swrm->dev,
1538 "%s: device %d has pending interrupt\n",
1539 __func__, devnum);
1540 break;
1541 }
1542 break;
1543 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1544 dev_err_ratelimited(swrm->dev,
1545 "%s: SWR bus clsh detected\n",
1546 __func__);
1547 break;
1548 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1549 dev_dbg(swrm->dev, "%s: SWR read FIFO overflow\n",
1550 __func__);
1551 break;
1552 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1553 dev_dbg(swrm->dev, "%s: SWR read FIFO underflow\n",
1554 __func__);
1555 break;
1556 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1557 dev_dbg(swrm->dev, "%s: SWR write FIFO overflow\n",
1558 __func__);
1559 break;
1560 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1561 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1562 dev_err_ratelimited(swrm->dev,
1563 "%s: SWR CMD error, fifo status 0x%x, flushing fifo\n",
1564 __func__, value);
1565 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1566 break;
1567 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
1568 dev_err_ratelimited(swrm->dev,
1569 "%s: SWR Port collision detected\n",
1570 __func__);
1571 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
1572 swr_master_write(swrm,
1573 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1574 break;
1575 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1576 dev_dbg(swrm->dev,
1577 "%s: SWR read enable valid mismatch\n",
1578 __func__);
1579 swrm->intr_mask &=
1580 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1581 swr_master_write(swrm,
1582 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1583 break;
1584 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1585 complete(&swrm->broadcast);
1586 dev_dbg(swrm->dev, "%s: SWR cmd id finished\n",
1587 __func__);
1588 break;
1589 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED_V2:
1590 break;
1591 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL_V2:
1592 break;
1593 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED_V2:
1594 break;
1595 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED_V2:
1596 break;
1597 case SWRM_INTERRUPT_STATUS_EXT_CLK_STOP_WAKEUP:
1598 if (swrm->state == SWR_MSTR_UP)
1599 dev_dbg(swrm->dev,
1600 "%s:SWR Master is already up\n",
1601 __func__);
1602 else
1603 dev_err_ratelimited(swrm->dev,
1604 "%s: SWR wokeup during clock stop\n",
1605 __func__);
1606 break;
1607 default:
1608 dev_err_ratelimited(swrm->dev,
1609 "%s: SWR unknown interrupt value: %d\n",
1610 __func__, value);
1611 ret = IRQ_NONE;
1612 break;
1613 }
1614 }
1615 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1616 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
1617
1618 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1619 intr_sts_masked = intr_sts & swrm->intr_mask;
1620
1621 if (intr_sts_masked) {
1622 dev_dbg(swrm->dev, "%s: new interrupt received\n", __func__);
1623 goto handle_irq;
1624 }
1625
1626 mutex_lock(&swrm->reslock);
1627 swrm_clk_request(swrm, false);
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05301628 if (swrm->lpass_core_hw_vote)
1629 clk_disable_unprepare(swrm->lpass_core_hw_vote);
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001630exit:
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301631 mutex_unlock(&swrm->reslock);
1632 swrm_unlock_sleep(swrm);
1633 return ret;
1634}
1635
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301636static irqreturn_t swrm_wakeup_interrupt(int irq, void *dev)
1637{
1638 struct swr_mstr_ctrl *swrm = dev;
1639 int ret = IRQ_HANDLED;
1640
1641 if (!swrm || !(swrm->dev)) {
1642 pr_err("%s: swrm or dev is null\n", __func__);
1643 return IRQ_NONE;
1644 }
1645 mutex_lock(&swrm->devlock);
1646 if (!swrm->dev_up) {
1647 if (swrm->wake_irq > 0)
1648 disable_irq_nosync(swrm->wake_irq);
1649 mutex_unlock(&swrm->devlock);
1650 return ret;
1651 }
1652 mutex_unlock(&swrm->devlock);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301653 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1654 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1655 goto exit;
1656 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301657 if (swrm->wake_irq > 0)
1658 disable_irq_nosync(swrm->wake_irq);
1659 pm_runtime_get_sync(swrm->dev);
1660 pm_runtime_mark_last_busy(swrm->dev);
1661 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301662 swrm_unlock_sleep(swrm);
1663exit:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301664 return ret;
1665}
1666
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301667static void swrm_wakeup_work(struct work_struct *work)
1668{
1669 struct swr_mstr_ctrl *swrm;
1670
1671 swrm = container_of(work, struct swr_mstr_ctrl,
1672 wakeup_work);
1673 if (!swrm || !(swrm->dev)) {
1674 pr_err("%s: swrm or dev is null\n", __func__);
1675 return;
1676 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301677
1678 mutex_lock(&swrm->devlock);
1679 if (!swrm->dev_up) {
1680 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301681 goto exit;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301682 }
1683 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301684 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1685 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1686 goto exit;
1687 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301688 pm_runtime_get_sync(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301689 pm_runtime_mark_last_busy(swrm->dev);
1690 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301691 swrm_unlock_sleep(swrm);
1692exit:
1693 pm_relax(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301694}
1695
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301696static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
1697{
1698 u32 val;
1699
1700 swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1701 val = (swrm->slave_status >> (devnum * 2));
1702 val &= SWRM_MCP_SLV_STATUS_MASK;
1703 return val;
1704}
1705
1706static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
1707 u8 *dev_num)
1708{
1709 int i;
1710 u64 id = 0;
1711 int ret = -EINVAL;
1712 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1713 struct swr_device *swr_dev;
1714 u32 num_dev = 0;
1715
1716 if (!swrm) {
1717 pr_err("%s: Invalid handle to swr controller\n",
1718 __func__);
1719 return ret;
1720 }
1721 if (swrm->num_dev)
1722 num_dev = swrm->num_dev;
1723 else
1724 num_dev = mstr->num_dev;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301725
1726 mutex_lock(&swrm->devlock);
1727 if (!swrm->dev_up) {
1728 mutex_unlock(&swrm->devlock);
1729 return ret;
1730 }
1731 mutex_unlock(&swrm->devlock);
1732
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301733 pm_runtime_get_sync(swrm->dev);
1734 for (i = 1; i < (num_dev + 1); i++) {
1735 id = ((u64)(swr_master_read(swrm,
1736 SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
1737 id |= swr_master_read(swrm,
1738 SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301739
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301740 /*
1741 * As pm_runtime_get_sync() brings all slaves out of reset
1742 * update logical device number for all slaves.
1743 */
1744 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1745 if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
1746 u32 status = swrm_get_device_status(swrm, i);
1747
1748 if ((status == 0x01) || (status == 0x02)) {
1749 swr_dev->dev_num = i;
1750 if ((id & SWR_DEV_ID_MASK) == dev_id) {
1751 *dev_num = i;
1752 ret = 0;
1753 }
1754 dev_dbg(swrm->dev,
1755 "%s: devnum %d is assigned for dev addr %lx\n",
1756 __func__, i, swr_dev->addr);
1757 }
1758 }
1759 }
1760 }
1761 if (ret)
1762 dev_err(swrm->dev, "%s: device 0x%llx is not ready\n",
1763 __func__, dev_id);
1764
1765 pm_runtime_mark_last_busy(swrm->dev);
1766 pm_runtime_put_autosuspend(swrm->dev);
1767 return ret;
1768}
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301769
1770static void swrm_device_wakeup_vote(struct swr_master *mstr)
1771{
1772 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1773
1774 if (!swrm) {
1775 pr_err("%s: Invalid handle to swr controller\n",
1776 __func__);
1777 return;
1778 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05301779 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1780 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1781 return;
1782 }
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301783 pm_runtime_get_sync(swrm->dev);
1784}
1785
1786static void swrm_device_wakeup_unvote(struct swr_master *mstr)
1787{
1788 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1789
1790 if (!swrm) {
1791 pr_err("%s: Invalid handle to swr controller\n",
1792 __func__);
1793 return;
1794 }
1795 pm_runtime_mark_last_busy(swrm->dev);
1796 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301797 swrm_unlock_sleep(swrm);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301798}
1799
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301800static int swrm_master_init(struct swr_mstr_ctrl *swrm)
1801{
1802 int ret = 0;
1803 u32 val;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301804 u8 row_ctrl = SWR_ROW_50;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301805 u8 col_ctrl = SWR_MIN_COL;
1806 u8 ssp_period = 1;
1807 u8 retry_cmd_num = 3;
1808 u32 reg[SWRM_MAX_INIT_REG];
1809 u32 value[SWRM_MAX_INIT_REG];
1810 int len = 0;
1811
1812 /* Clear Rows and Cols */
1813 val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
1814 (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
1815 (ssp_period << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
1816
1817 reg[len] = SWRM_MCP_FRAME_CTRL_BANK_ADDR(0);
1818 value[len++] = val;
1819
1820 /* Set Auto enumeration flag */
1821 reg[len] = SWRM_ENUMERATOR_CFG_ADDR;
1822 value[len++] = 1;
1823
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301824 /* Configure No pings */
1825 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
1826 val &= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK;
1827 val |= (0x1f << SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT);
1828 reg[len] = SWRM_MCP_CFG_ADDR;
1829 value[len++] = val;
1830
1831 /* Configure number of retries of a read/write cmd */
1832 val = (retry_cmd_num << SWRM_CMD_FIFO_CFG_NUM_OF_CMD_RETRY_SHFT);
1833 reg[len] = SWRM_CMD_FIFO_CFG_ADDR;
1834 value[len++] = val;
1835
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301836 reg[len] = SWRM_MCP_BUS_CTRL_ADDR;
1837 value[len++] = 0x2;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301838
Ramprasad Katkam83303512018-10-11 17:34:22 +05301839 /* Set IRQ to PULSE */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301840 reg[len] = SWRM_COMP_CFG_ADDR;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301841 value[len++] = 0x02;
1842
1843 reg[len] = SWRM_COMP_CFG_ADDR;
1844 value[len++] = 0x03;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301845
1846 reg[len] = SWRM_INTERRUPT_CLEAR;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301847 value[len++] = 0xFFFFFFFF;
1848
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301849 swrm->intr_mask = SWRM_INTERRUPT_STATUS_MASK;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301850 /* Mask soundwire interrupts */
1851 reg[len] = SWRM_INTERRUPT_MASK_ADDR;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301852 value[len++] = swrm->intr_mask;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301853
1854 reg[len] = SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301855 value[len++] = swrm->intr_mask;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301856
1857 swr_master_bulk_write(swrm, reg, value, len);
1858
Sudheer Papothi63f48152018-11-15 01:08:03 +05301859 /*
1860 * For SWR master version 1.5.1, continue
1861 * execute on command ignore.
1862 */
1863 if (swrm->version == SWRM_VERSION_1_5_1)
1864 swr_master_write(swrm, SWRM_CMD_FIFO_CFG_ADDR,
1865 (swr_master_read(swrm,
1866 SWRM_CMD_FIFO_CFG_ADDR) | 0x80000000));
1867
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301868 return ret;
1869}
1870
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05301871static int swrm_event_notify(struct notifier_block *self,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301872 unsigned long action, void *data)
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05301873{
1874 struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301875 event_notifier);
1876
1877 if (!swrm || !(swrm->dev)) {
1878 pr_err("%s: swrm or dev is NULL\n", __func__);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05301879 return -EINVAL;
1880 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301881 switch (action) {
1882 case MSM_AUD_DC_EVENT:
1883 schedule_work(&(swrm->dc_presence_work));
1884 break;
1885 case SWR_WAKE_IRQ_EVENT:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301886 if (swrm->ipc_wakeup && !swrm->ipc_wakeup_triggered) {
1887 swrm->ipc_wakeup_triggered = true;
Ramprasad Katkam57349872018-11-11 18:34:57 +05301888 pm_stay_awake(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301889 schedule_work(&swrm->wakeup_work);
Ramprasad Katkamcd61c6e2018-09-18 13:22:58 +05301890 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301891 break;
1892 default:
1893 dev_err(swrm->dev, "%s: invalid event type: %lu\n",
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05301894 __func__, action);
1895 return -EINVAL;
1896 }
1897
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05301898 return 0;
1899}
1900
1901static void swrm_notify_work_fn(struct work_struct *work)
1902{
1903 struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301904 dc_presence_work);
1905
1906 if (!swrm || !swrm->pdev) {
1907 pr_err("%s: swrm or pdev is NULL\n", __func__);
1908 return;
1909 }
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05301910 swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL);
1911}
1912
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301913static int swrm_probe(struct platform_device *pdev)
1914{
1915 struct swr_mstr_ctrl *swrm;
1916 struct swr_ctrl_platform_data *pdata;
1917 u32 i, num_ports, port_num, port_type, ch_mask;
1918 u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0;
1919 int ret = 0;
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05301920 struct clk *lpass_core_hw_vote = NULL;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301921
1922 /* Allocate soundwire master driver structure */
1923 swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl),
1924 GFP_KERNEL);
1925 if (!swrm) {
1926 ret = -ENOMEM;
1927 goto err_memory_fail;
1928 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301929 swrm->pdev = pdev;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301930 swrm->dev = &pdev->dev;
1931 platform_set_drvdata(pdev, swrm);
1932 swr_set_ctrl_data(&swrm->master, swrm);
1933 pdata = dev_get_platdata(&pdev->dev);
1934 if (!pdata) {
1935 dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
1936 __func__);
1937 ret = -EINVAL;
1938 goto err_pdata_fail;
1939 }
1940 swrm->handle = (void *)pdata->handle;
1941 if (!swrm->handle) {
1942 dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
1943 __func__);
1944 ret = -EINVAL;
1945 goto err_pdata_fail;
1946 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301947 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr_master_id",
1948 &swrm->master_id);
1949 if (ret) {
1950 dev_err(&pdev->dev, "%s: failed to get master id\n", __func__);
1951 goto err_pdata_fail;
1952 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05301953 if (!(of_property_read_u32(pdev->dev.of_node,
1954 "swrm-io-base", &swrm->swrm_base_reg)))
1955 ret = of_property_read_u32(pdev->dev.of_node,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301956 "swrm-io-base", &swrm->swrm_base_reg);
1957 if (!swrm->swrm_base_reg) {
1958 swrm->read = pdata->read;
1959 if (!swrm->read) {
1960 dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
1961 __func__);
1962 ret = -EINVAL;
1963 goto err_pdata_fail;
1964 }
1965 swrm->write = pdata->write;
1966 if (!swrm->write) {
1967 dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
1968 __func__);
1969 ret = -EINVAL;
1970 goto err_pdata_fail;
1971 }
1972 swrm->bulk_write = pdata->bulk_write;
1973 if (!swrm->bulk_write) {
1974 dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
1975 __func__);
1976 ret = -EINVAL;
1977 goto err_pdata_fail;
1978 }
1979 } else {
1980 swrm->swrm_dig_base = devm_ioremap(&pdev->dev,
1981 swrm->swrm_base_reg, SWRM_MAX_REGISTER);
1982 }
1983
1984 swrm->clk = pdata->clk;
1985 if (!swrm->clk) {
1986 dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
1987 __func__);
1988 ret = -EINVAL;
1989 goto err_pdata_fail;
1990 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301991 if (of_property_read_u32(pdev->dev.of_node,
1992 "qcom,swr-clock-stop-mode0",
1993 &swrm->clk_stop_mode0_supp)) {
1994 swrm->clk_stop_mode0_supp = FALSE;
1995 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05301996
1997 ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
1998 &swrm->num_dev);
1999 if (ret) {
2000 dev_dbg(&pdev->dev, "%s: Looking up %s property failed\n",
2001 __func__, "qcom,swr-num-dev");
2002 } else {
2003 if (swrm->num_dev > SWR_MAX_SLAVE_DEVICES) {
2004 dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
2005 __func__, swrm->num_dev, SWR_MAX_SLAVE_DEVICES);
2006 ret = -EINVAL;
2007 goto err_pdata_fail;
2008 }
2009 }
2010
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302011 /* Parse soundwire port mapping */
2012 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
2013 &num_ports);
2014 if (ret) {
2015 dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
2016 goto err_pdata_fail;
2017 }
2018 swrm->num_ports = num_ports;
2019
2020 if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
2021 &map_size)) {
2022 dev_err(swrm->dev, "missing port mapping\n");
2023 goto err_pdata_fail;
2024 }
2025
2026 map_length = map_size / (3 * sizeof(u32));
2027 if (num_ports > SWR_MSTR_PORT_LEN) {
2028 dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
2029 __func__);
2030 ret = -EINVAL;
2031 goto err_pdata_fail;
2032 }
2033 temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
2034
2035 if (!temp) {
2036 ret = -ENOMEM;
2037 goto err_pdata_fail;
2038 }
2039 ret = of_property_read_u32_array(pdev->dev.of_node,
2040 "qcom,swr-port-mapping", temp, 3 * map_length);
2041 if (ret) {
2042 dev_err(swrm->dev, "%s: Failed to read port mapping\n",
2043 __func__);
2044 goto err_pdata_fail;
2045 }
2046
2047 for (i = 0; i < map_length; i++) {
2048 port_num = temp[3 * i];
2049 port_type = temp[3 * i + 1];
2050 ch_mask = temp[3 * i + 2];
2051
2052 if (port_num != old_port_num)
2053 ch_iter = 0;
2054 swrm->port_mapping[port_num][ch_iter].port_type = port_type;
2055 swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
2056 old_port_num = port_num;
2057 }
2058 devm_kfree(&pdev->dev, temp);
2059
2060 swrm->reg_irq = pdata->reg_irq;
2061 swrm->master.read = swrm_read;
2062 swrm->master.write = swrm_write;
2063 swrm->master.bulk_write = swrm_bulk_write;
2064 swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
2065 swrm->master.connect_port = swrm_connect_port;
2066 swrm->master.disconnect_port = swrm_disconnect_port;
2067 swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
2068 swrm->master.remove_from_group = swrm_remove_from_group;
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302069 swrm->master.device_wakeup_vote = swrm_device_wakeup_vote;
2070 swrm->master.device_wakeup_unvote = swrm_device_wakeup_unvote;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302071 swrm->master.dev.parent = &pdev->dev;
2072 swrm->master.dev.of_node = pdev->dev.of_node;
2073 swrm->master.num_port = 0;
2074 swrm->rcmd_id = 0;
2075 swrm->wcmd_id = 0;
2076 swrm->slave_status = 0;
2077 swrm->num_rx_chs = 0;
2078 swrm->clk_ref_count = 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302079 swrm->swr_irq_wakeup_capable = 0;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302080 swrm->mclk_freq = MCLK_FREQ;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302081 swrm->dev_up = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302082 swrm->state = SWR_MSTR_UP;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302083 swrm->ipc_wakeup = false;
2084 swrm->ipc_wakeup_triggered = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302085 init_completion(&swrm->reset);
2086 init_completion(&swrm->broadcast);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302087 init_completion(&swrm->clk_off_complete);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302088 mutex_init(&swrm->mlock);
2089 mutex_init(&swrm->reslock);
2090 mutex_init(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302091 mutex_init(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302092 mutex_init(&swrm->clklock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302093 mutex_init(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302094 mutex_init(&swrm->pm_lock);
2095 swrm->wlock_holders = 0;
2096 swrm->pm_state = SWRM_PM_SLEEPABLE;
2097 init_waitqueue_head(&swrm->pm_wq);
2098 pm_qos_add_request(&swrm->pm_qos_req,
2099 PM_QOS_CPU_DMA_LATENCY,
2100 PM_QOS_DEFAULT_VALUE);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302101
2102 for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++)
2103 INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
2104
2105 if (swrm->reg_irq) {
2106 ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
2107 SWR_IRQ_REGISTER);
2108 if (ret) {
2109 dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
2110 __func__, ret);
2111 goto err_irq_fail;
2112 }
2113 } else {
2114 swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
2115 if (swrm->irq < 0) {
2116 dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
2117 __func__, swrm->irq);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302118 goto err_irq_fail;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302119 }
2120
2121 ret = request_threaded_irq(swrm->irq, NULL,
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302122 swr_mstr_interrupt_v2,
Ramprasad Katkam83303512018-10-11 17:34:22 +05302123 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302124 "swr_master_irq", swrm);
2125 if (ret) {
2126 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2127 __func__, ret);
2128 goto err_irq_fail;
2129 }
2130
2131 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302132 /* Make inband tx interrupts as wakeup capable for slave irq */
2133 ret = of_property_read_u32(pdev->dev.of_node,
2134 "qcom,swr-mstr-irq-wakeup-capable",
2135 &swrm->swr_irq_wakeup_capable);
2136 if (ret)
2137 dev_dbg(swrm->dev, "%s: swrm irq wakeup capable not defined\n",
2138 __func__);
2139 if (swrm->swr_irq_wakeup_capable)
2140 irq_set_irq_wake(swrm->irq, 1);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302141 ret = swr_register_master(&swrm->master);
2142 if (ret) {
2143 dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
2144 goto err_mstr_fail;
2145 }
2146
2147 /* Add devices registered with board-info as the
2148 * controller will be up now
2149 */
2150 swr_master_add_boarddevices(&swrm->master);
2151 mutex_lock(&swrm->mlock);
2152 swrm_clk_request(swrm, true);
2153 ret = swrm_master_init(swrm);
2154 if (ret < 0) {
2155 dev_err(&pdev->dev,
2156 "%s: Error in master Initialization , err %d\n",
2157 __func__, ret);
2158 mutex_unlock(&swrm->mlock);
2159 goto err_mstr_fail;
2160 }
2161 swrm->version = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
2162
2163 mutex_unlock(&swrm->mlock);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302164 INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302165
2166 if (pdev->dev.of_node)
2167 of_register_swr_devices(&swrm->master);
2168
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302169 /* Register LPASS core hw vote */
2170 lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
2171 if (IS_ERR(lpass_core_hw_vote)) {
2172 ret = PTR_ERR(lpass_core_hw_vote);
2173 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2174 __func__, "lpass_core_hw_vote", ret);
2175 lpass_core_hw_vote = NULL;
2176 ret = 0;
2177 }
2178 swrm->lpass_core_hw_vote = lpass_core_hw_vote;
2179
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302180 dbgswrm = swrm;
2181 debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
2182 if (!IS_ERR(debugfs_swrm_dent)) {
2183 debugfs_peek = debugfs_create_file("swrm_peek",
2184 S_IFREG | 0444, debugfs_swrm_dent,
2185 (void *) "swrm_peek", &swrm_debug_ops);
2186
2187 debugfs_poke = debugfs_create_file("swrm_poke",
2188 S_IFREG | 0444, debugfs_swrm_dent,
2189 (void *) "swrm_poke", &swrm_debug_ops);
2190
2191 debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
2192 S_IFREG | 0444, debugfs_swrm_dent,
2193 (void *) "swrm_reg_dump",
2194 &swrm_debug_ops);
2195 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302196
2197 ret = device_init_wakeup(swrm->dev, true);
2198 if (ret) {
2199 dev_err(swrm->dev, "Device wakeup init failed: %d\n", ret);
2200 goto err_irq_wakeup_fail;
2201 }
2202
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302203 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
2204 pm_runtime_use_autosuspend(&pdev->dev);
2205 pm_runtime_set_active(&pdev->dev);
2206 pm_runtime_enable(&pdev->dev);
2207 pm_runtime_mark_last_busy(&pdev->dev);
2208
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302209 INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn);
2210 swrm->event_notifier.notifier_call = swrm_event_notify;
2211 msm_aud_evt_register_client(&swrm->event_notifier);
2212
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302213 return 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302214err_irq_wakeup_fail:
2215 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302216err_mstr_fail:
2217 if (swrm->reg_irq)
2218 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2219 swrm, SWR_IRQ_FREE);
2220 else if (swrm->irq)
2221 free_irq(swrm->irq, swrm);
2222err_irq_fail:
2223 mutex_destroy(&swrm->mlock);
2224 mutex_destroy(&swrm->reslock);
2225 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302226 mutex_destroy(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302227 mutex_destroy(&swrm->clklock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302228 mutex_destroy(&swrm->pm_lock);
2229 pm_qos_remove_request(&swrm->pm_qos_req);
2230
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302231err_pdata_fail:
2232err_memory_fail:
2233 return ret;
2234}
2235
2236static int swrm_remove(struct platform_device *pdev)
2237{
2238 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2239
2240 if (swrm->reg_irq)
2241 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2242 swrm, SWR_IRQ_FREE);
2243 else if (swrm->irq)
2244 free_irq(swrm->irq, swrm);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302245 else if (swrm->wake_irq > 0)
2246 free_irq(swrm->wake_irq, swrm);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302247 if (swrm->swr_irq_wakeup_capable)
2248 irq_set_irq_wake(swrm->irq, 0);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302249 cancel_work_sync(&swrm->wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302250 pm_runtime_disable(&pdev->dev);
2251 pm_runtime_set_suspended(&pdev->dev);
2252 swr_unregister_master(&swrm->master);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302253 msm_aud_evt_unregister_client(&swrm->event_notifier);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302254 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302255 mutex_destroy(&swrm->mlock);
2256 mutex_destroy(&swrm->reslock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302257 mutex_destroy(&swrm->iolock);
2258 mutex_destroy(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302259 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302260 mutex_destroy(&swrm->pm_lock);
2261 pm_qos_remove_request(&swrm->pm_qos_req);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302262 devm_kfree(&pdev->dev, swrm);
2263 return 0;
2264}
2265
2266static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
2267{
2268 u32 val;
2269
2270 dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
2271 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD);
2272 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2273 val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK;
2274 swr_master_write(swrm, SWRM_MCP_CFG_ADDR, val);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302275
2276 return 0;
2277}
2278
2279#ifdef CONFIG_PM
2280static int swrm_runtime_resume(struct device *dev)
2281{
2282 struct platform_device *pdev = to_platform_device(dev);
2283 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2284 int ret = 0;
2285 struct swr_master *mstr = &swrm->master;
2286 struct swr_device *swr_dev;
2287
2288 dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
2289 __func__, swrm->state);
2290 mutex_lock(&swrm->reslock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302291
Karthikeyan Manif6821902019-05-21 17:31:24 -07002292 if (swrm->lpass_core_hw_vote) {
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302293 ret = clk_prepare_enable(swrm->lpass_core_hw_vote);
Karthikeyan Manif6821902019-05-21 17:31:24 -07002294 if (ret < 0) {
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302295 dev_err(dev, "%s:lpass core hw enable failed\n",
2296 __func__);
Karthikeyan Manif6821902019-05-21 17:31:24 -07002297 ret = 0;
2298 }
2299 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302300
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302301 if ((swrm->state == SWR_MSTR_DOWN) ||
2302 (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) {
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302303 if (swrm->clk_stop_mode0_supp) {
2304 if (swrm->ipc_wakeup)
2305 msm_aud_evt_blocking_notifier_call_chain(
2306 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302307 }
2308
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302309 if (swrm_clk_request(swrm, true))
2310 goto exit;
2311 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302312 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2313 ret = swr_device_up(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302314 if (ret == -ENODEV) {
2315 dev_dbg(dev,
2316 "%s slave device up not implemented\n",
2317 __func__);
2318 ret = 0;
2319 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302320 dev_err(dev,
2321 "%s: failed to wakeup swr dev %d\n",
2322 __func__, swr_dev->dev_num);
2323 swrm_clk_request(swrm, false);
2324 goto exit;
2325 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302326 }
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05302327 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2328 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2329 swrm_master_init(swrm);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302330 swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, 0x0,
2331 SWRS_SCP_INT_STATUS_MASK_1);
Karthikeyan Manif6821902019-05-21 17:31:24 -07002332 if (swrm->state == SWR_MSTR_SSR) {
2333 mutex_unlock(&swrm->reslock);
2334 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
2335 mutex_lock(&swrm->reslock);
2336 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302337 } else {
2338 /*wake up from clock stop*/
2339 swr_master_write(swrm, SWRM_MCP_BUS_CTRL_ADDR, 0x2);
2340 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302341 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302342 swrm->state = SWR_MSTR_UP;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302343 }
2344exit:
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302345 if (swrm->lpass_core_hw_vote)
2346 clk_disable_unprepare(swrm->lpass_core_hw_vote);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302347 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
2348 mutex_unlock(&swrm->reslock);
2349 return ret;
2350}
2351
2352static int swrm_runtime_suspend(struct device *dev)
2353{
2354 struct platform_device *pdev = to_platform_device(dev);
2355 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2356 int ret = 0;
2357 struct swr_master *mstr = &swrm->master;
2358 struct swr_device *swr_dev;
2359 int current_state = 0;
2360
2361 dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
2362 __func__, swrm->state);
2363 mutex_lock(&swrm->reslock);
2364 mutex_lock(&swrm->force_down_lock);
2365 current_state = swrm->state;
2366 mutex_unlock(&swrm->force_down_lock);
Karthikeyan Manif6821902019-05-21 17:31:24 -07002367 if (swrm->lpass_core_hw_vote) {
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302368 ret = clk_prepare_enable(swrm->lpass_core_hw_vote);
Karthikeyan Manif6821902019-05-21 17:31:24 -07002369 if (ret < 0) {
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302370 dev_err(dev, "%s:lpass core hw enable failed\n",
2371 __func__);
Karthikeyan Manif6821902019-05-21 17:31:24 -07002372 ret = 0;
2373 }
2374 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302375
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302376 if ((current_state == SWR_MSTR_UP) ||
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302377 (current_state == SWR_MSTR_SSR)) {
2378
2379 if ((current_state != SWR_MSTR_SSR) &&
2380 swrm_is_port_en(&swrm->master)) {
2381 dev_dbg(dev, "%s ports are enabled\n", __func__);
2382 ret = -EBUSY;
2383 goto exit;
2384 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302385 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +05302386 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302387 swrm_clk_pause(swrm);
2388 swr_master_write(swrm, SWRM_COMP_CFG_ADDR, 0x00);
2389 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2390 ret = swr_device_down(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302391 if (ret == -ENODEV) {
2392 dev_dbg_ratelimited(dev,
2393 "%s slave device down not implemented\n",
2394 __func__);
2395 ret = 0;
2396 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302397 dev_err(dev,
2398 "%s: failed to shutdown swr dev %d\n",
2399 __func__, swr_dev->dev_num);
2400 goto exit;
2401 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302402 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302403 } else {
2404 /* clock stop sequence */
2405 swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
2406 SWRS_SCP_CONTROL);
2407 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302408 }
2409 swrm_clk_request(swrm, false);
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302410
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302411 if (swrm->clk_stop_mode0_supp) {
2412 if (swrm->wake_irq > 0) {
2413 enable_irq(swrm->wake_irq);
2414 } else if (swrm->ipc_wakeup) {
2415 msm_aud_evt_blocking_notifier_call_chain(
2416 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
2417 swrm->ipc_wakeup_triggered = false;
2418 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302419 }
2420
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302421 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302422 /* Retain SSR state until resume */
2423 if (current_state != SWR_MSTR_SSR)
2424 swrm->state = SWR_MSTR_DOWN;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302425exit:
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302426 if (swrm->lpass_core_hw_vote)
2427 clk_disable_unprepare(swrm->lpass_core_hw_vote);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302428 mutex_unlock(&swrm->reslock);
2429 return ret;
2430}
2431#endif /* CONFIG_PM */
2432
2433static int swrm_device_down(struct device *dev)
2434{
2435 struct platform_device *pdev = to_platform_device(dev);
2436 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2437 int ret = 0;
2438
2439 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2440
2441 mutex_lock(&swrm->force_down_lock);
2442 swrm->state = SWR_MSTR_SSR;
2443 mutex_unlock(&swrm->force_down_lock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302444 if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
2445 ret = swrm_runtime_suspend(dev);
2446 if (!ret) {
2447 pm_runtime_disable(dev);
2448 pm_runtime_set_suspended(dev);
2449 pm_runtime_enable(dev);
2450 }
2451 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302452
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302453 return 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302454}
2455
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302456int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm)
2457{
2458 int ret = 0;
Laxminath Kasama60239e2019-01-10 14:43:03 +05302459 int irq, dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302460
2461 if (!swrm->ipc_wakeup) {
Laxminath Kasama60239e2019-01-10 14:43:03 +05302462 irq = of_get_named_gpio(swrm->dev->of_node,
2463 "qcom,swr-wakeup-irq", 0);
2464 if (gpio_is_valid(irq)) {
2465 swrm->wake_irq = gpio_to_irq(irq);
2466 if (swrm->wake_irq < 0) {
2467 dev_err(swrm->dev,
2468 "Unable to configure irq\n");
2469 return swrm->wake_irq;
2470 }
2471 } else {
2472 dir_apps_irq = platform_get_irq_byname(swrm->pdev,
2473 "swr_wake_irq");
2474 if (dir_apps_irq < 0) {
2475 dev_err(swrm->dev,
2476 "TLMM connect gpio not found\n");
2477 return -EINVAL;
2478 }
2479 swrm->wake_irq = dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302480 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302481 ret = request_threaded_irq(swrm->wake_irq, NULL,
2482 swrm_wakeup_interrupt,
2483 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
2484 "swr_wake_irq", swrm);
2485 if (ret) {
2486 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2487 __func__, ret);
2488 return -EINVAL;
2489 }
Aditya Bavanari3517b112018-12-03 13:26:59 +05302490 irq_set_irq_wake(swrm->wake_irq, 1);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302491 }
2492 return ret;
2493}
2494
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302495static int swrm_alloc_port_mem(struct device *dev, struct swr_mstr_ctrl *swrm,
2496 u32 uc, u32 size)
2497{
2498 if (!swrm->port_param) {
2499 swrm->port_param = devm_kzalloc(dev,
2500 sizeof(swrm->port_param) * SWR_UC_MAX,
2501 GFP_KERNEL);
2502 if (!swrm->port_param)
2503 return -ENOMEM;
2504 }
2505 if (!swrm->port_param[uc]) {
2506 swrm->port_param[uc] = devm_kcalloc(dev, size,
2507 sizeof(struct port_params),
2508 GFP_KERNEL);
2509 if (!swrm->port_param[uc])
2510 return -ENOMEM;
2511 } else {
2512 dev_err_ratelimited(swrm->dev, "%s: called more than once\n",
2513 __func__);
2514 }
2515
2516 return 0;
2517}
2518
2519static int swrm_copy_port_config(struct swr_mstr_ctrl *swrm,
2520 struct swrm_port_config *port_cfg,
2521 u32 size)
2522{
2523 int idx;
2524 struct port_params *params;
2525 int uc = port_cfg->uc;
2526 int ret = 0;
2527
2528 for (idx = 0; idx < size; idx++) {
2529 params = &((struct port_params *)port_cfg->params)[idx];
2530 if (!params) {
2531 dev_err(swrm->dev, "%s: Invalid params\n", __func__);
2532 ret = -EINVAL;
2533 break;
2534 }
2535 memcpy(&swrm->port_param[uc][idx], params,
2536 sizeof(struct port_params));
2537 }
2538
2539 return ret;
2540}
2541
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302542/**
2543 * swrm_wcd_notify - parent device can notify to soundwire master through
2544 * this function
2545 * @pdev: pointer to platform device structure
2546 * @id: command id from parent to the soundwire master
2547 * @data: data from parent device to soundwire master
2548 */
2549int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
2550{
2551 struct swr_mstr_ctrl *swrm;
2552 int ret = 0;
2553 struct swr_master *mstr;
2554 struct swr_device *swr_dev;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302555 struct swrm_port_config *port_cfg;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302556
2557 if (!pdev) {
2558 pr_err("%s: pdev is NULL\n", __func__);
2559 return -EINVAL;
2560 }
2561 swrm = platform_get_drvdata(pdev);
2562 if (!swrm) {
2563 dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__);
2564 return -EINVAL;
2565 }
2566 mstr = &swrm->master;
2567
2568 switch (id) {
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302569 case SWR_CLK_FREQ:
2570 if (!data) {
2571 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
2572 ret = -EINVAL;
2573 } else {
2574 mutex_lock(&swrm->mlock);
2575 swrm->mclk_freq = *(int *)data;
2576 mutex_unlock(&swrm->mlock);
2577 }
2578 break;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302579 case SWR_DEVICE_SSR_DOWN:
2580 mutex_lock(&swrm->devlock);
2581 swrm->dev_up = false;
2582 mutex_unlock(&swrm->devlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302583 mutex_lock(&swrm->reslock);
2584 swrm->state = SWR_MSTR_SSR;
2585 mutex_unlock(&swrm->reslock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302586 break;
2587 case SWR_DEVICE_SSR_UP:
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302588 /* wait for clk voting to be zero */
Ramprasad Katkam7f6462e2018-11-06 11:51:22 +05302589 reinit_completion(&swrm->clk_off_complete);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302590 if (swrm->clk_ref_count &&
2591 !wait_for_completion_timeout(&swrm->clk_off_complete,
Ramprasad Katkamc87efeb2018-12-12 19:26:19 +05302592 msecs_to_jiffies(500)))
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302593 dev_err(swrm->dev, "%s: clock voting not zero\n",
2594 __func__);
2595
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302596 mutex_lock(&swrm->devlock);
2597 swrm->dev_up = true;
2598 mutex_unlock(&swrm->devlock);
2599 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302600 case SWR_DEVICE_DOWN:
2601 dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
2602 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302603 if (swrm->state == SWR_MSTR_DOWN)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302604 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
2605 __func__, swrm->state);
2606 else
2607 swrm_device_down(&pdev->dev);
2608 mutex_unlock(&swrm->mlock);
2609 break;
2610 case SWR_DEVICE_UP:
2611 dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
Ramprasad Katkam0fed92f2018-11-08 14:22:22 +05302612 mutex_lock(&swrm->devlock);
2613 if (!swrm->dev_up) {
2614 dev_dbg(swrm->dev, "SSR not complete yet\n");
2615 mutex_unlock(&swrm->devlock);
2616 return -EBUSY;
2617 }
2618 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302619 mutex_lock(&swrm->mlock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302620 pm_runtime_mark_last_busy(&pdev->dev);
2621 pm_runtime_get_sync(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302622 mutex_lock(&swrm->reslock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302623 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2624 ret = swr_reset_device(swr_dev);
2625 if (ret) {
2626 dev_err(swrm->dev,
2627 "%s: failed to reset swr device %d\n",
2628 __func__, swr_dev->dev_num);
2629 swrm_clk_request(swrm, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302630 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302631 }
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302632 pm_runtime_mark_last_busy(&pdev->dev);
2633 pm_runtime_put_autosuspend(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302634 mutex_unlock(&swrm->reslock);
2635 mutex_unlock(&swrm->mlock);
2636 break;
2637 case SWR_SET_NUM_RX_CH:
2638 if (!data) {
2639 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
2640 ret = -EINVAL;
2641 } else {
2642 mutex_lock(&swrm->mlock);
2643 swrm->num_rx_chs = *(int *)data;
2644 if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
2645 list_for_each_entry(swr_dev, &mstr->devices,
2646 dev_list) {
2647 ret = swr_set_device_group(swr_dev,
2648 SWR_BROADCAST);
2649 if (ret)
2650 dev_err(swrm->dev,
2651 "%s: set num ch failed\n",
2652 __func__);
2653 }
2654 } else {
2655 list_for_each_entry(swr_dev, &mstr->devices,
2656 dev_list) {
2657 ret = swr_set_device_group(swr_dev,
2658 SWR_GROUP_NONE);
2659 if (ret)
2660 dev_err(swrm->dev,
2661 "%s: set num ch failed\n",
2662 __func__);
2663 }
2664 }
2665 mutex_unlock(&swrm->mlock);
2666 }
2667 break;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302668 case SWR_REGISTER_WAKE_IRQ:
2669 if (!data) {
2670 dev_err(swrm->dev, "%s: reg wake irq data is NULL\n",
2671 __func__);
2672 ret = -EINVAL;
2673 } else {
2674 mutex_lock(&swrm->mlock);
2675 swrm->ipc_wakeup = *(u32 *)data;
2676 ret = swrm_register_wake_irq(swrm);
2677 if (ret)
2678 dev_err(swrm->dev, "%s: register wake_irq failed\n",
2679 __func__);
2680 mutex_unlock(&swrm->mlock);
2681 }
2682 break;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302683 case SWR_SET_PORT_MAP:
2684 if (!data) {
2685 dev_err(swrm->dev, "%s: data is NULL for id=%d\n",
2686 __func__, id);
2687 ret = -EINVAL;
2688 } else {
2689 mutex_lock(&swrm->mlock);
2690 port_cfg = (struct swrm_port_config *)data;
2691 if (!port_cfg->size) {
2692 ret = -EINVAL;
2693 goto done;
2694 }
2695 ret = swrm_alloc_port_mem(&pdev->dev, swrm,
2696 port_cfg->uc, port_cfg->size);
2697 if (!ret)
2698 swrm_copy_port_config(swrm, port_cfg,
2699 port_cfg->size);
2700done:
2701 mutex_unlock(&swrm->mlock);
2702 }
2703 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302704 default:
2705 dev_err(swrm->dev, "%s: swr master unknown id %d\n",
2706 __func__, id);
2707 break;
2708 }
2709 return ret;
2710}
2711EXPORT_SYMBOL(swrm_wcd_notify);
2712
Ramprasad Katkam57349872018-11-11 18:34:57 +05302713/*
2714 * swrm_pm_cmpxchg:
2715 * Check old state and exchange with pm new state
2716 * if old state matches with current state
2717 *
2718 * @swrm: pointer to wcd core resource
2719 * @o: pm old state
2720 * @n: pm new state
2721 *
2722 * Returns old state
2723 */
2724static enum swrm_pm_state swrm_pm_cmpxchg(
2725 struct swr_mstr_ctrl *swrm,
2726 enum swrm_pm_state o,
2727 enum swrm_pm_state n)
2728{
2729 enum swrm_pm_state old;
2730
2731 if (!swrm)
2732 return o;
2733
2734 mutex_lock(&swrm->pm_lock);
2735 old = swrm->pm_state;
2736 if (old == o)
2737 swrm->pm_state = n;
2738 mutex_unlock(&swrm->pm_lock);
2739
2740 return old;
2741}
2742
2743static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm)
2744{
2745 enum swrm_pm_state os;
2746
2747 /*
2748 * swrm_{lock/unlock}_sleep will be called by swr irq handler
2749 * and slave wake up requests..
2750 *
2751 * If system didn't resume, we can simply return false so
2752 * IRQ handler can return without handling IRQ.
2753 */
2754 mutex_lock(&swrm->pm_lock);
2755 if (swrm->wlock_holders++ == 0) {
2756 dev_dbg(swrm->dev, "%s: holding wake lock\n", __func__);
2757 pm_qos_update_request(&swrm->pm_qos_req,
2758 msm_cpuidle_get_deep_idle_latency());
2759 pm_stay_awake(swrm->dev);
2760 }
2761 mutex_unlock(&swrm->pm_lock);
2762
2763 if (!wait_event_timeout(swrm->pm_wq,
2764 ((os = swrm_pm_cmpxchg(swrm,
2765 SWRM_PM_SLEEPABLE,
2766 SWRM_PM_AWAKE)) ==
2767 SWRM_PM_SLEEPABLE ||
2768 (os == SWRM_PM_AWAKE)),
2769 msecs_to_jiffies(
2770 SWRM_SYSTEM_RESUME_TIMEOUT_MS))) {
2771 dev_err(swrm->dev, "%s: system didn't resume within %dms, s %d, w %d\n",
2772 __func__, SWRM_SYSTEM_RESUME_TIMEOUT_MS, swrm->pm_state,
2773 swrm->wlock_holders);
2774 swrm_unlock_sleep(swrm);
2775 return false;
2776 }
2777 wake_up_all(&swrm->pm_wq);
2778 return true;
2779}
2780
2781static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm)
2782{
2783 mutex_lock(&swrm->pm_lock);
2784 if (--swrm->wlock_holders == 0) {
2785 dev_dbg(swrm->dev, "%s: releasing wake lock pm_state %d -> %d\n",
2786 __func__, swrm->pm_state, SWRM_PM_SLEEPABLE);
2787 /*
2788 * if swrm_lock_sleep failed, pm_state would be still
2789 * swrm_PM_ASLEEP, don't overwrite
2790 */
2791 if (likely(swrm->pm_state == SWRM_PM_AWAKE))
2792 swrm->pm_state = SWRM_PM_SLEEPABLE;
2793 pm_qos_update_request(&swrm->pm_qos_req,
2794 PM_QOS_DEFAULT_VALUE);
2795 pm_relax(swrm->dev);
2796 }
2797 mutex_unlock(&swrm->pm_lock);
2798 wake_up_all(&swrm->pm_wq);
2799}
2800
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302801#ifdef CONFIG_PM_SLEEP
2802static int swrm_suspend(struct device *dev)
2803{
2804 int ret = -EBUSY;
2805 struct platform_device *pdev = to_platform_device(dev);
2806 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2807
2808 dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302809
2810 mutex_lock(&swrm->pm_lock);
2811
2812 if (swrm->pm_state == SWRM_PM_SLEEPABLE) {
2813 dev_dbg(swrm->dev, "%s: suspending system, state %d, wlock %d\n",
2814 __func__, swrm->pm_state,
2815 swrm->wlock_holders);
2816 swrm->pm_state = SWRM_PM_ASLEEP;
2817 } else if (swrm->pm_state == SWRM_PM_AWAKE) {
2818 /*
2819 * unlock to wait for pm_state == SWRM_PM_SLEEPABLE
2820 * then set to SWRM_PM_ASLEEP
2821 */
2822 dev_dbg(swrm->dev, "%s: waiting to suspend system, state %d, wlock %d\n",
2823 __func__, swrm->pm_state,
2824 swrm->wlock_holders);
2825 mutex_unlock(&swrm->pm_lock);
2826 if (!(wait_event_timeout(swrm->pm_wq, swrm_pm_cmpxchg(
2827 swrm, SWRM_PM_SLEEPABLE,
2828 SWRM_PM_ASLEEP) ==
2829 SWRM_PM_SLEEPABLE,
2830 msecs_to_jiffies(
2831 SWRM_SYS_SUSPEND_WAIT)))) {
2832 dev_dbg(swrm->dev, "%s: suspend failed state %d, wlock %d\n",
2833 __func__, swrm->pm_state,
2834 swrm->wlock_holders);
2835 return -EBUSY;
2836 } else {
2837 dev_dbg(swrm->dev,
2838 "%s: done, state %d, wlock %d\n",
2839 __func__, swrm->pm_state,
2840 swrm->wlock_holders);
2841 }
2842 mutex_lock(&swrm->pm_lock);
2843 } else if (swrm->pm_state == SWRM_PM_ASLEEP) {
2844 dev_dbg(swrm->dev, "%s: system is already suspended, state %d, wlock %d\n",
2845 __func__, swrm->pm_state,
2846 swrm->wlock_holders);
2847 }
2848
2849 mutex_unlock(&swrm->pm_lock);
2850
2851 if ((!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev))) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302852 ret = swrm_runtime_suspend(dev);
2853 if (!ret) {
2854 /*
2855 * Synchronize runtime-pm and system-pm states:
2856 * At this point, we are already suspended. If
2857 * runtime-pm still thinks its active, then
2858 * make sure its status is in sync with HW
2859 * status. The three below calls let the
2860 * runtime-pm know that we are suspended
2861 * already without re-invoking the suspend
2862 * callback
2863 */
2864 pm_runtime_disable(dev);
2865 pm_runtime_set_suspended(dev);
2866 pm_runtime_enable(dev);
2867 }
2868 }
2869 if (ret == -EBUSY) {
2870 /*
2871 * There is a possibility that some audio stream is active
2872 * during suspend. We dont want to return suspend failure in
2873 * that case so that display and relevant components can still
2874 * go to suspend.
2875 * If there is some other error, then it should be passed-on
2876 * to system level suspend
2877 */
2878 ret = 0;
2879 }
2880 return ret;
2881}
2882
2883static int swrm_resume(struct device *dev)
2884{
2885 int ret = 0;
2886 struct platform_device *pdev = to_platform_device(dev);
2887 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2888
2889 dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
2890 if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
2891 ret = swrm_runtime_resume(dev);
2892 if (!ret) {
2893 pm_runtime_mark_last_busy(dev);
2894 pm_request_autosuspend(dev);
2895 }
2896 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302897 mutex_lock(&swrm->pm_lock);
2898 if (swrm->pm_state == SWRM_PM_ASLEEP) {
2899 dev_dbg(swrm->dev,
2900 "%s: resuming system, state %d, wlock %d\n",
2901 __func__, swrm->pm_state,
2902 swrm->wlock_holders);
2903 swrm->pm_state = SWRM_PM_SLEEPABLE;
2904 } else {
2905 dev_dbg(swrm->dev, "%s: system is already awake, state %d wlock %d\n",
2906 __func__, swrm->pm_state,
2907 swrm->wlock_holders);
2908 }
2909 mutex_unlock(&swrm->pm_lock);
2910 wake_up_all(&swrm->pm_wq);
2911
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302912 return ret;
2913}
2914#endif /* CONFIG_PM_SLEEP */
2915
2916static const struct dev_pm_ops swrm_dev_pm_ops = {
2917 SET_SYSTEM_SLEEP_PM_OPS(
2918 swrm_suspend,
2919 swrm_resume
2920 )
2921 SET_RUNTIME_PM_OPS(
2922 swrm_runtime_suspend,
2923 swrm_runtime_resume,
2924 NULL
2925 )
2926};
2927
2928static const struct of_device_id swrm_dt_match[] = {
2929 {
2930 .compatible = "qcom,swr-mstr",
2931 },
2932 {}
2933};
2934
2935static struct platform_driver swr_mstr_driver = {
2936 .probe = swrm_probe,
2937 .remove = swrm_remove,
2938 .driver = {
2939 .name = SWR_WCD_NAME,
2940 .owner = THIS_MODULE,
2941 .pm = &swrm_dev_pm_ops,
2942 .of_match_table = swrm_dt_match,
Xiaojun Sang53cd13a2018-06-29 15:14:37 +08002943 .suppress_bind_attrs = true,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302944 },
2945};
2946
2947static int __init swrm_init(void)
2948{
2949 return platform_driver_register(&swr_mstr_driver);
2950}
2951module_init(swrm_init);
2952
2953static void __exit swrm_exit(void)
2954{
2955 platform_driver_unregister(&swr_mstr_driver);
2956}
2957module_exit(swrm_exit);
2958
2959MODULE_LICENSE("GPL v2");
2960MODULE_DESCRIPTION("SoundWire Master Controller");
2961MODULE_ALIAS("platform:swr-mstr");