blob: bd4f56acc8e28eee80f34777fcde2257e53b7428 [file] [log] [blame]
Meng Wang43bbb872018-12-10 12:32:05 +08001// SPDX-License-Identifier: GPL-2.0-only
Meng Wang61af6842018-09-10 17:47:55 +08002/*
Aditya Bavanari3517b112018-12-03 13:26:59 +05303 * Copyright (c) 2015-2019, The Linux Foundation. All rights reserved.
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05304 */
5
6#include <linux/irq.h>
7#include <linux/kernel.h>
8#include <linux/init.h>
9#include <linux/slab.h>
10#include <linux/io.h>
11#include <linux/interrupt.h>
12#include <linux/platform_device.h>
13#include <linux/delay.h>
14#include <linux/kthread.h>
Ramprasad Katkamcab8d722018-09-28 15:54:06 +053015#include <linux/bitops.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053016#include <linux/clk.h>
Laxminath Kasama60239e2019-01-10 14:43:03 +053017#include <linux/gpio.h>
18#include <linux/of_gpio.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053019#include <linux/pm_runtime.h>
20#include <linux/of.h>
21#include <linux/debugfs.h>
22#include <linux/uaccess.h>
23#include <soc/soundwire.h>
Sudheer Papothi3d1596e2018-10-27 06:19:18 +053024#include <soc/swr-common.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053025#include <linux/regmap.h>
Ramprasad Katkam68765ab2018-08-30 11:46:32 +053026#include <dsp/msm-audio-event-notify.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053027#include "swrm_registers.h"
28#include "swr-mstr-ctrl.h"
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053029
Ramprasad Katkam57349872018-11-11 18:34:57 +053030#define SWRM_SYSTEM_RESUME_TIMEOUT_MS 700
31#define SWRM_SYS_SUSPEND_WAIT 1
Sudheer Papothi3d1596e2018-10-27 06:19:18 +053032
Sudheer Papothi4c322b12018-10-31 06:34:01 +053033#define SWRM_DSD_PARAMS_PORT 4
34
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053035#define SWR_BROADCAST_CMD_ID 0x0F
Sudheer Papothi3590b312019-06-04 23:51:30 +053036#define SWR_AUTO_SUSPEND_DELAY 1 /* delay in sec */
Sudheer Papothi7c067e82018-11-15 06:53:35 +053037#define SWR_DEV_ID_MASK 0xFFFFFFFFFFFF
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053038#define SWR_REG_VAL_PACK(data, dev, id, reg) \
39 ((reg) | ((id) << 16) | ((dev) << 20) | ((data) << 24))
40
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +053041#define SWR_INVALID_PARAM 0xFF
Laxminath Kasam990c70b2018-11-09 23:15:09 +053042#define SWR_HSTOP_MAX_VAL 0xF
43#define SWR_HSTART_MIN_VAL 0x0
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +053044
Ramprasad Katkam83303512018-10-11 17:34:22 +053045#define SWRM_INTERRUPT_STATUS_MASK 0x1FDFD
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053046/* pm runtime auto suspend timer in msecs */
47static int auto_suspend_timer = SWR_AUTO_SUSPEND_DELAY * 1000;
48module_param(auto_suspend_timer, int, 0664);
49MODULE_PARM_DESC(auto_suspend_timer, "timer for auto suspend");
50
51enum {
52 SWR_NOT_PRESENT, /* Device is detached/not present on the bus */
53 SWR_ATTACHED_OK, /* Device is attached */
54 SWR_ALERT, /* Device alters master for any interrupts */
55 SWR_RESERVED, /* Reserved */
56};
57
58enum {
59 MASTER_ID_WSA = 1,
60 MASTER_ID_RX,
61 MASTER_ID_TX
62};
Ramprasad Katkamcab8d722018-09-28 15:54:06 +053063
64enum {
65 ENABLE_PENDING,
66 DISABLE_PENDING
67};
Sudheer Papothi384addd2019-06-14 02:26:52 +053068
69enum {
70 LPASS_HW_CORE,
71 LPASS_AUDIO_CORE,
72};
73
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053074#define TRUE 1
75#define FALSE 0
76
Ramprasad Katkam1f221262018-08-23 15:01:22 +053077#define SWRM_MAX_PORT_REG 120
Ramprasad Katkam83303512018-10-11 17:34:22 +053078#define SWRM_MAX_INIT_REG 11
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053079
80#define SWR_MSTR_MAX_REG_ADDR 0x1740
81#define SWR_MSTR_START_REG_ADDR 0x00
82#define SWR_MSTR_MAX_BUF_LEN 32
83#define BYTES_PER_LINE 12
84#define SWR_MSTR_RD_BUF_LEN 8
85#define SWR_MSTR_WR_BUF_LEN 32
86
Laxminath Kasamfbcaf322018-07-18 00:38:14 +053087#define MAX_FIFO_RD_FAIL_RETRY 3
88
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053089static struct swr_mstr_ctrl *dbgswrm;
90static struct dentry *debugfs_swrm_dent;
91static struct dentry *debugfs_peek;
92static struct dentry *debugfs_poke;
93static struct dentry *debugfs_reg_dump;
94static unsigned int read_data;
95
Ramprasad Katkam57349872018-11-11 18:34:57 +053096static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm);
97static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053098
99static bool swrm_is_msm_variant(int val)
100{
101 return (val == SWRM_VERSION_1_3);
102}
103
104static int swrm_debug_open(struct inode *inode, struct file *file)
105{
106 file->private_data = inode->i_private;
107 return 0;
108}
109
110static int get_parameters(char *buf, u32 *param1, int num_of_par)
111{
112 char *token;
113 int base, cnt;
114
115 token = strsep(&buf, " ");
116 for (cnt = 0; cnt < num_of_par; cnt++) {
117 if (token) {
118 if ((token[1] == 'x') || (token[1] == 'X'))
119 base = 16;
120 else
121 base = 10;
122
123 if (kstrtou32(token, base, &param1[cnt]) != 0)
124 return -EINVAL;
125
126 token = strsep(&buf, " ");
127 } else
128 return -EINVAL;
129 }
130 return 0;
131}
132
133static ssize_t swrm_reg_show(char __user *ubuf, size_t count,
134 loff_t *ppos)
135{
136 int i, reg_val, len;
137 ssize_t total = 0;
138 char tmp_buf[SWR_MSTR_MAX_BUF_LEN];
139
140 if (!ubuf || !ppos)
141 return 0;
142
143 for (i = (((int) *ppos / BYTES_PER_LINE) + SWR_MSTR_START_REG_ADDR);
144 i <= SWR_MSTR_MAX_REG_ADDR; i += 4) {
145 reg_val = dbgswrm->read(dbgswrm->handle, i);
146 len = snprintf(tmp_buf, 25, "0x%.3x: 0x%.2x\n", i, reg_val);
Aditya Bavanari9f599b42019-08-27 22:18:41 +0530147 if (len < 0) {
148 pr_err("%s: fail to fill the buffer\n", __func__);
149 total = -EFAULT;
150 goto copy_err;
151 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530152 if ((total + len) >= count - 1)
153 break;
154 if (copy_to_user((ubuf + total), tmp_buf, len)) {
155 pr_err("%s: fail to copy reg dump\n", __func__);
156 total = -EFAULT;
157 goto copy_err;
158 }
159 *ppos += len;
160 total += len;
161 }
162
163copy_err:
164 return total;
165}
166
167static ssize_t swrm_debug_read(struct file *file, char __user *ubuf,
168 size_t count, loff_t *ppos)
169{
170 char lbuf[SWR_MSTR_RD_BUF_LEN];
171 char *access_str;
172 ssize_t ret_cnt;
173
174 if (!count || !file || !ppos || !ubuf)
175 return -EINVAL;
176
177 access_str = file->private_data;
178 if (*ppos < 0)
179 return -EINVAL;
180
181 if (!strcmp(access_str, "swrm_peek")) {
182 snprintf(lbuf, sizeof(lbuf), "0x%x\n", read_data);
183 ret_cnt = simple_read_from_buffer(ubuf, count, ppos, lbuf,
184 strnlen(lbuf, 7));
185 } else if (!strcmp(access_str, "swrm_reg_dump")) {
186 ret_cnt = swrm_reg_show(ubuf, count, ppos);
187 } else {
188 pr_err("%s: %s not permitted to read\n", __func__, access_str);
189 ret_cnt = -EPERM;
190 }
191 return ret_cnt;
192}
193
194static ssize_t swrm_debug_write(struct file *filp,
195 const char __user *ubuf, size_t cnt, loff_t *ppos)
196{
197 char lbuf[SWR_MSTR_WR_BUF_LEN];
198 int rc;
199 u32 param[5];
200 char *access_str;
201
202 if (!filp || !ppos || !ubuf)
203 return -EINVAL;
204
205 access_str = filp->private_data;
206 if (cnt > sizeof(lbuf) - 1)
207 return -EINVAL;
208
209 rc = copy_from_user(lbuf, ubuf, cnt);
210 if (rc)
211 return -EFAULT;
212
213 lbuf[cnt] = '\0';
214 if (!strcmp(access_str, "swrm_poke")) {
215 /* write */
216 rc = get_parameters(lbuf, param, 2);
217 if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) &&
218 (param[1] <= 0xFFFFFFFF) &&
219 (rc == 0))
220 rc = dbgswrm->write(dbgswrm->handle, param[0],
221 param[1]);
222 else
223 rc = -EINVAL;
224 } else if (!strcmp(access_str, "swrm_peek")) {
225 /* read */
226 rc = get_parameters(lbuf, param, 1);
227 if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) && (rc == 0))
228 read_data = dbgswrm->read(dbgswrm->handle, param[0]);
229 else
230 rc = -EINVAL;
231 }
232 if (rc == 0)
233 rc = cnt;
234 else
235 pr_err("%s: rc = %d\n", __func__, rc);
236
237 return rc;
238}
239
240static const struct file_operations swrm_debug_ops = {
241 .open = swrm_debug_open,
242 .write = swrm_debug_write,
243 .read = swrm_debug_read,
244};
245
Sudheer Papothi0016db12019-06-11 04:42:38 +0530246static void swrm_reg_dump(struct swr_mstr_ctrl *swrm,
247 u32 *reg, u32 *val, int len, const char* func)
248{
249 int i = 0;
250
251 for (i = 0; i < len; i++)
252 dev_dbg(swrm->dev, "%s: reg = 0x%x val = 0x%x\n",
253 func, reg[i], val[i]);
254}
255
Sudheer Papothi384addd2019-06-14 02:26:52 +0530256static int swrm_request_hw_vote(struct swr_mstr_ctrl *swrm,
257 int core_type, bool enable)
258{
259 int ret = 0;
260
261 if (core_type == LPASS_HW_CORE) {
262 if (swrm->lpass_core_hw_vote) {
263 if (enable) {
264 ret =
265 clk_prepare_enable(swrm->lpass_core_hw_vote);
266 if (ret < 0)
267 dev_err(swrm->dev,
268 "%s:lpass core hw enable failed\n",
269 __func__);
270 } else
271 clk_disable_unprepare(swrm->lpass_core_hw_vote);
272 }
273 }
274 if (core_type == LPASS_AUDIO_CORE) {
275 if (swrm->lpass_core_audio) {
276 if (enable) {
277 ret =
278 clk_prepare_enable(swrm->lpass_core_audio);
279 if (ret < 0)
280 dev_err(swrm->dev,
281 "%s:lpass audio hw enable failed\n",
282 __func__);
283 } else
284 clk_disable_unprepare(swrm->lpass_core_audio);
285 }
286 }
287
288 return ret;
289}
290
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530291static int swrm_clk_request(struct swr_mstr_ctrl *swrm, bool enable)
292{
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530293 int ret = 0;
294
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530295 if (!swrm->clk || !swrm->handle)
296 return -EINVAL;
297
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530298 mutex_lock(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530299 if (enable) {
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530300 if (!swrm->dev_up) {
301 ret = -ENODEV;
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530302 goto exit;
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530303 }
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -0700304 if (swrm->core_vote) {
305 ret = swrm->core_vote(swrm->handle, true);
306 if (ret) {
307 dev_err_ratelimited(swrm->dev,
308 "%s: clock enable req failed",
309 __func__);
310 goto exit;
311 }
312 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530313 swrm->clk_ref_count++;
314 if (swrm->clk_ref_count == 1) {
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530315 ret = swrm->clk(swrm->handle, true);
316 if (ret) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +0530317 dev_err_ratelimited(swrm->dev,
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530318 "%s: clock enable req failed",
319 __func__);
320 --swrm->clk_ref_count;
321 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530322 }
323 } else if (--swrm->clk_ref_count == 0) {
324 swrm->clk(swrm->handle, false);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530325 complete(&swrm->clk_off_complete);
326 }
327 if (swrm->clk_ref_count < 0) {
Meng Wang8c60bb52019-06-19 15:49:06 +0800328 dev_err(swrm->dev, "%s: swrm clk count mismatch\n", __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530329 swrm->clk_ref_count = 0;
330 }
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530331
332exit:
333 mutex_unlock(&swrm->clklock);
334 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530335}
336
337static int swrm_ahb_write(struct swr_mstr_ctrl *swrm,
338 u16 reg, u32 *value)
339{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530340 u32 temp = (u32)(*value);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530341 int ret = 0;
342
343 mutex_lock(&swrm->devlock);
344 if (!swrm->dev_up)
345 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530346
347 ret = swrm_clk_request(swrm, TRUE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530348 if (ret) {
349 dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
350 __func__);
351 goto err;
352 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530353 iowrite32(temp, swrm->swrm_dig_base + reg);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530354 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530355err:
356 mutex_unlock(&swrm->devlock);
357 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530358}
359
360static int swrm_ahb_read(struct swr_mstr_ctrl *swrm,
361 u16 reg, u32 *value)
362{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530363 u32 temp = 0;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530364 int ret = 0;
365
366 mutex_lock(&swrm->devlock);
367 if (!swrm->dev_up)
368 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530369
370 ret = swrm_clk_request(swrm, TRUE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530371 if (ret) {
372 dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
373 __func__);
374 goto err;
375 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530376 temp = ioread32(swrm->swrm_dig_base + reg);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530377 *value = temp;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530378 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530379err:
380 mutex_unlock(&swrm->devlock);
381 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530382}
383
384static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr)
385{
386 u32 val = 0;
387
388 if (swrm->read)
389 val = swrm->read(swrm->handle, reg_addr);
390 else
391 swrm_ahb_read(swrm, reg_addr, &val);
392 return val;
393}
394
395static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val)
396{
397 if (swrm->write)
398 swrm->write(swrm->handle, reg_addr, val);
399 else
400 swrm_ahb_write(swrm, reg_addr, &val);
401}
402
403static int swr_master_bulk_write(struct swr_mstr_ctrl *swrm, u32 *reg_addr,
404 u32 *val, unsigned int length)
405{
406 int i = 0;
407
408 if (swrm->bulk_write)
409 swrm->bulk_write(swrm->handle, reg_addr, val, length);
410 else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530411 mutex_lock(&swrm->iolock);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530412 for (i = 0; i < length; i++) {
413 /* wait for FIFO WR command to complete to avoid overflow */
Karthikeyan Mani5d52dd82019-08-15 16:58:08 -0700414 /*
415 * Reduce sleep from 100us to 10us to meet KPIs
416 * This still meets the hardware spec
417 */
418 usleep_range(10, 12);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530419 swr_master_write(swrm, reg_addr[i], val[i]);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530420 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530421 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530422 }
423 return 0;
424}
425
426static bool swrm_is_port_en(struct swr_master *mstr)
427{
428 return !!(mstr->num_port);
429}
430
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530431static void copy_port_tables(struct swr_mstr_ctrl *swrm,
432 struct port_params *params)
433{
434 u8 i;
435 struct port_params *config = params;
436
437 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
438 /* wsa uses single frame structure for all configurations */
439 if (!swrm->mport_cfg[i].port_en)
440 continue;
441 swrm->mport_cfg[i].sinterval = config[i].si;
442 swrm->mport_cfg[i].offset1 = config[i].off1;
443 swrm->mport_cfg[i].offset2 = config[i].off2;
444 swrm->mport_cfg[i].hstart = config[i].hstart;
445 swrm->mport_cfg[i].hstop = config[i].hstop;
446 swrm->mport_cfg[i].blk_pack_mode = config[i].bp_mode;
447 swrm->mport_cfg[i].blk_grp_count = config[i].bgp_ctrl;
448 swrm->mport_cfg[i].word_length = config[i].wd_len;
449 swrm->mport_cfg[i].lane_ctrl = config[i].lane_ctrl;
450 }
451}
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530452static int swrm_get_port_config(struct swr_mstr_ctrl *swrm)
453{
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530454 struct port_params *params;
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530455 u32 usecase = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530456
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530457 /* TODO - Send usecase information to avoid checking for master_id */
458 if (swrm->mport_cfg[SWRM_DSD_PARAMS_PORT].port_en &&
459 (swrm->master_id == MASTER_ID_RX))
460 usecase = 1;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530461
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530462 params = swrm->port_param[usecase];
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530463 copy_port_tables(swrm, params);
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530464
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530465 return 0;
466}
467
468static int swrm_get_master_port(struct swr_mstr_ctrl *swrm, u8 *mstr_port_id,
469 u8 *mstr_ch_mask, u8 mstr_prt_type,
470 u8 slv_port_id)
471{
472 int i, j;
473 *mstr_port_id = 0;
474
475 for (i = 1; i <= swrm->num_ports; i++) {
476 for (j = 0; j < SWR_MAX_CH_PER_PORT; j++) {
477 if (swrm->port_mapping[i][j].port_type == mstr_prt_type)
478 goto found;
479 }
480 }
481found:
482 if (i > swrm->num_ports || j == SWR_MAX_CH_PER_PORT) {
483 dev_err(swrm->dev, "%s: port type not supported by master\n",
484 __func__);
485 return -EINVAL;
486 }
487 /* id 0 corresponds to master port 1 */
488 *mstr_port_id = i - 1;
489 *mstr_ch_mask = swrm->port_mapping[i][j].ch_mask;
490
491 return 0;
492
493}
494
495static u32 swrm_get_packed_reg_val(u8 *cmd_id, u8 cmd_data,
496 u8 dev_addr, u16 reg_addr)
497{
498 u32 val;
499 u8 id = *cmd_id;
500
501 if (id != SWR_BROADCAST_CMD_ID) {
502 if (id < 14)
503 id += 1;
504 else
505 id = 0;
506 *cmd_id = id;
507 }
508 val = SWR_REG_VAL_PACK(cmd_data, dev_addr, id, reg_addr);
509
510 return val;
511}
512
513static int swrm_cmd_fifo_rd_cmd(struct swr_mstr_ctrl *swrm, int *cmd_data,
514 u8 dev_addr, u8 cmd_id, u16 reg_addr,
515 u32 len)
516{
517 u32 val;
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530518 u32 retry_attempt = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530519
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530520 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530521 val = swrm_get_packed_reg_val(&swrm->rcmd_id, len, dev_addr, reg_addr);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530522 if (swrm->read) {
523 /* skip delay if read is handled in platform driver */
524 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
525 } else {
526 /* wait for FIFO RD to complete to avoid overflow */
527 usleep_range(100, 105);
528 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
529 /* wait for FIFO RD CMD complete to avoid overflow */
530 usleep_range(250, 255);
531 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530532retry_read:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530533 *cmd_data = swr_master_read(swrm, SWRM_CMD_FIFO_RD_FIFO_ADDR);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530534 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, rcmd_id: 0x%x, \
535 dev_num: 0x%x, cmd_data: 0x%x\n", __func__, reg_addr,
536 cmd_id, swrm->rcmd_id, dev_addr, *cmd_data);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530537 if ((((*cmd_data) & 0xF00) >> 8) != swrm->rcmd_id) {
538 if (retry_attempt < MAX_FIFO_RD_FAIL_RETRY) {
539 /* wait 500 us before retry on fifo read failure */
540 usleep_range(500, 505);
541 retry_attempt++;
542 goto retry_read;
543 } else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530544 dev_err_ratelimited(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, \
545 rcmd_id: 0x%x, dev_num: 0x%x, cmd_data: 0x%x\n",
546 __func__, reg_addr, cmd_id, swrm->rcmd_id,
547 dev_addr, *cmd_data);
548
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530549 dev_err_ratelimited(swrm->dev,
550 "%s: failed to read fifo\n", __func__);
551 }
552 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530553 mutex_unlock(&swrm->iolock);
554
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530555 return 0;
556}
557
558static int swrm_cmd_fifo_wr_cmd(struct swr_mstr_ctrl *swrm, u8 cmd_data,
559 u8 dev_addr, u8 cmd_id, u16 reg_addr)
560{
561 u32 val;
562 int ret = 0;
563
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530564 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530565 if (!cmd_id)
566 val = swrm_get_packed_reg_val(&swrm->wcmd_id, cmd_data,
567 dev_addr, reg_addr);
568 else
569 val = swrm_get_packed_reg_val(&cmd_id, cmd_data,
570 dev_addr, reg_addr);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530571 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x,wcmd_id: 0x%x, \
572 dev_num: 0x%x, cmd_data: 0x%x\n", __func__,
573 reg_addr, cmd_id, swrm->wcmd_id,dev_addr, cmd_data);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +0530574 swr_master_write(swrm, SWRM_CMD_FIFO_WR_CMD, val);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530575 /*
576 * wait for FIFO WR command to complete to avoid overflow
577 * skip delay if write is handled in platform driver.
578 */
579 if(!swrm->write)
Karthikeyan Mani5d52dd82019-08-15 16:58:08 -0700580 usleep_range(150, 155);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530581 if (cmd_id == 0xF) {
582 /*
583 * sleep for 10ms for MSM soundwire variant to allow broadcast
584 * command to complete.
585 */
586 if (swrm_is_msm_variant(swrm->version))
587 usleep_range(10000, 10100);
588 else
589 wait_for_completion_timeout(&swrm->broadcast,
590 (2 * HZ/10));
591 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530592 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530593 return ret;
594}
595
596static int swrm_read(struct swr_master *master, u8 dev_num, u16 reg_addr,
597 void *buf, u32 len)
598{
599 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
600 int ret = 0;
601 int val;
602 u8 *reg_val = (u8 *)buf;
603
604 if (!swrm) {
605 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
606 return -EINVAL;
607 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530608 if (!dev_num) {
609 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
610 return -EINVAL;
611 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530612 mutex_lock(&swrm->devlock);
613 if (!swrm->dev_up) {
614 mutex_unlock(&swrm->devlock);
615 return 0;
616 }
617 mutex_unlock(&swrm->devlock);
618
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530619 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530620 ret = swrm_cmd_fifo_rd_cmd(swrm, &val, dev_num, 0, reg_addr, len);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530621
622 if (!ret)
623 *reg_val = (u8)val;
624
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530625 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530626 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530627 return ret;
628}
629
630static int swrm_write(struct swr_master *master, u8 dev_num, u16 reg_addr,
631 const void *buf)
632{
633 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
634 int ret = 0;
635 u8 reg_val = *(u8 *)buf;
636
637 if (!swrm) {
638 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
639 return -EINVAL;
640 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530641 if (!dev_num) {
642 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
643 return -EINVAL;
644 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530645 mutex_lock(&swrm->devlock);
646 if (!swrm->dev_up) {
647 mutex_unlock(&swrm->devlock);
648 return 0;
649 }
650 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530651
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530652 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530653 ret = swrm_cmd_fifo_wr_cmd(swrm, reg_val, dev_num, 0, reg_addr);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530654
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530655 pm_runtime_put_autosuspend(swrm->dev);
656 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530657 return ret;
658}
659
660static int swrm_bulk_write(struct swr_master *master, u8 dev_num, void *reg,
661 const void *buf, size_t len)
662{
663 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
664 int ret = 0;
665 int i;
666 u32 *val;
667 u32 *swr_fifo_reg;
668
669 if (!swrm || !swrm->handle) {
670 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
671 return -EINVAL;
672 }
673 if (len <= 0)
674 return -EINVAL;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530675 mutex_lock(&swrm->devlock);
676 if (!swrm->dev_up) {
677 mutex_unlock(&swrm->devlock);
678 return 0;
679 }
680 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530681
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530682 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530683 if (dev_num) {
684 swr_fifo_reg = kcalloc(len, sizeof(u32), GFP_KERNEL);
685 if (!swr_fifo_reg) {
686 ret = -ENOMEM;
687 goto err;
688 }
689 val = kcalloc(len, sizeof(u32), GFP_KERNEL);
690 if (!val) {
691 ret = -ENOMEM;
692 goto mem_fail;
693 }
694
695 for (i = 0; i < len; i++) {
696 val[i] = swrm_get_packed_reg_val(&swrm->wcmd_id,
697 ((u8 *)buf)[i],
698 dev_num,
699 ((u16 *)reg)[i]);
700 swr_fifo_reg[i] = SWRM_CMD_FIFO_WR_CMD;
701 }
702 ret = swr_master_bulk_write(swrm, swr_fifo_reg, val, len);
703 if (ret) {
704 dev_err(&master->dev, "%s: bulk write failed\n",
705 __func__);
706 ret = -EINVAL;
707 }
708 } else {
709 dev_err(&master->dev,
710 "%s: No support of Bulk write for master regs\n",
711 __func__);
712 ret = -EINVAL;
713 goto err;
714 }
715 kfree(val);
716mem_fail:
717 kfree(swr_fifo_reg);
718err:
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530719 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530720 pm_runtime_mark_last_busy(swrm->dev);
721 return ret;
722}
723
724static u8 get_inactive_bank_num(struct swr_mstr_ctrl *swrm)
725{
726 return (swr_master_read(swrm, SWRM_MCP_STATUS) &
727 SWRM_MCP_STATUS_BANK_NUM_MASK) ? 0 : 1;
728}
729
730static void enable_bank_switch(struct swr_mstr_ctrl *swrm, u8 bank,
731 u8 row, u8 col)
732{
733 swrm_cmd_fifo_wr_cmd(swrm, ((row << 3) | col), 0xF, 0xF,
734 SWRS_SCP_FRAME_CTRL_BANK(bank));
735}
736
737static struct swr_port_info *swrm_get_port_req(struct swrm_mports *mport,
738 u8 slv_port, u8 dev_num)
739{
740 struct swr_port_info *port_req = NULL;
741
742 list_for_each_entry(port_req, &mport->port_req_list, list) {
743 /* Store dev_id instead of dev_num if enumeration is changed run_time */
744 if ((port_req->slave_port_id == slv_port)
745 && (port_req->dev_num == dev_num))
746 return port_req;
747 }
748 return NULL;
749}
750
751static bool swrm_remove_from_group(struct swr_master *master)
752{
753 struct swr_device *swr_dev;
754 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
755 bool is_removed = false;
756
757 if (!swrm)
758 goto end;
759
760 mutex_lock(&swrm->mlock);
761 if ((swrm->num_rx_chs > 1) &&
762 (swrm->num_rx_chs == swrm->num_cfg_devs)) {
763 list_for_each_entry(swr_dev, &master->devices,
764 dev_list) {
765 swr_dev->group_id = SWR_GROUP_NONE;
766 master->gr_sid = 0;
767 }
768 is_removed = true;
769 }
770 mutex_unlock(&swrm->mlock);
771
772end:
773 return is_removed;
774}
775
776static void swrm_disable_ports(struct swr_master *master,
777 u8 bank)
778{
779 u32 value;
780 struct swr_port_info *port_req;
781 int i;
782 struct swrm_mports *mport;
783 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
784
785 if (!swrm) {
786 pr_err("%s: swrm is null\n", __func__);
787 return;
788 }
789
790 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
791 master->num_port);
792
793
794 for (i = 0; i < SWR_MSTR_PORT_LEN ; i++) {
795
796 mport = &(swrm->mport_cfg[i]);
797 if (!mport->port_en)
798 continue;
799
800 list_for_each_entry(port_req, &mport->port_req_list, list) {
801 /* skip ports with no change req's*/
802 if (port_req->req_ch == port_req->ch_en)
803 continue;
804
805 swrm_cmd_fifo_wr_cmd(swrm, port_req->req_ch,
806 port_req->dev_num, 0x00,
807 SWRS_DP_CHANNEL_ENABLE_BANK(port_req->slave_port_id,
808 bank));
809 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x\n",
810 __func__, i,
811 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)));
812 }
813 value = ((mport->req_ch)
814 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
815 value |= ((mport->offset2)
816 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
817 value |= ((mport->offset1)
818 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
819 value |= mport->sinterval;
820
821 swr_master_write(swrm,
822 SWRM_DP_PORT_CTRL_BANK(i+1, bank),
823 value);
824 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
825 __func__, i,
826 (SWRM_DP_PORT_CTRL_BANK(i+1, bank)), value);
827 }
828}
829
830static void swrm_cleanup_disabled_port_reqs(struct swr_master *master)
831{
832 struct swr_port_info *port_req, *next;
833 int i;
834 struct swrm_mports *mport;
835 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
836
837 if (!swrm) {
838 pr_err("%s: swrm is null\n", __func__);
839 return;
840 }
841 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
842 master->num_port);
843
844 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
845 mport = &(swrm->mport_cfg[i]);
846 list_for_each_entry_safe(port_req, next,
847 &mport->port_req_list, list) {
848 /* skip ports without new ch req */
849 if (port_req->ch_en == port_req->req_ch)
850 continue;
851
852 /* remove new ch req's*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +0530853 port_req->ch_en = port_req->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530854
855 /* If no streams enabled on port, remove the port req */
856 if (port_req->ch_en == 0) {
857 list_del(&port_req->list);
858 kfree(port_req);
859 }
860 }
861 /* remove new ch req's on mport*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +0530862 mport->ch_en = mport->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530863
864 if (!(mport->ch_en)) {
865 mport->port_en = false;
866 master->port_en_mask &= ~i;
867 }
868 }
869}
870static void swrm_copy_data_port_config(struct swr_master *master, u8 bank)
871{
872 u32 value, slv_id;
873 struct swr_port_info *port_req;
874 int i;
875 struct swrm_mports *mport;
876 u32 reg[SWRM_MAX_PORT_REG];
877 u32 val[SWRM_MAX_PORT_REG];
878 int len = 0;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530879 u8 hparams;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530880 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
881
882 if (!swrm) {
883 pr_err("%s: swrm is null\n", __func__);
884 return;
885 }
886
887 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
888 master->num_port);
889
890 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
891 mport = &(swrm->mport_cfg[i]);
892 if (!mport->port_en)
893 continue;
894
895 list_for_each_entry(port_req, &mport->port_req_list, list) {
896 slv_id = port_req->slave_port_id;
897 reg[len] = SWRM_CMD_FIFO_WR_CMD;
898 val[len++] = SWR_REG_VAL_PACK(port_req->req_ch,
899 port_req->dev_num, 0x00,
900 SWRS_DP_CHANNEL_ENABLE_BANK(slv_id,
901 bank));
902
903 reg[len] = SWRM_CMD_FIFO_WR_CMD;
904 val[len++] = SWR_REG_VAL_PACK(mport->sinterval,
905 port_req->dev_num, 0x00,
906 SWRS_DP_SAMPLE_CONTROL_1_BANK(slv_id,
907 bank));
908
909 reg[len] = SWRM_CMD_FIFO_WR_CMD;
910 val[len++] = SWR_REG_VAL_PACK(mport->offset1,
911 port_req->dev_num, 0x00,
912 SWRS_DP_OFFSET_CONTROL_1_BANK(slv_id,
913 bank));
914
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530915 if (mport->offset2 != SWR_INVALID_PARAM) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530916 reg[len] = SWRM_CMD_FIFO_WR_CMD;
917 val[len++] = SWR_REG_VAL_PACK(mport->offset2,
918 port_req->dev_num, 0x00,
919 SWRS_DP_OFFSET_CONTROL_2_BANK(
920 slv_id, bank));
921 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530922 if (mport->hstart != SWR_INVALID_PARAM
923 && mport->hstop != SWR_INVALID_PARAM) {
924 hparams = (mport->hstart << 4) | mport->hstop;
925
926 reg[len] = SWRM_CMD_FIFO_WR_CMD;
927 val[len++] = SWR_REG_VAL_PACK(hparams,
928 port_req->dev_num, 0x00,
929 SWRS_DP_HCONTROL_BANK(slv_id,
930 bank));
931 }
932 if (mport->word_length != SWR_INVALID_PARAM) {
933 reg[len] = SWRM_CMD_FIFO_WR_CMD;
934 val[len++] =
935 SWR_REG_VAL_PACK(mport->word_length,
936 port_req->dev_num, 0x00,
937 SWRS_DP_BLOCK_CONTROL_1(slv_id));
938 }
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +0530939 if (mport->blk_pack_mode != SWR_INVALID_PARAM
940 && swrm->master_id != MASTER_ID_WSA) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530941 reg[len] = SWRM_CMD_FIFO_WR_CMD;
942 val[len++] =
943 SWR_REG_VAL_PACK(mport->blk_pack_mode,
944 port_req->dev_num, 0x00,
945 SWRS_DP_BLOCK_CONTROL_3_BANK(slv_id,
946 bank));
947 }
948 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
949 reg[len] = SWRM_CMD_FIFO_WR_CMD;
950 val[len++] =
951 SWR_REG_VAL_PACK(mport->blk_grp_count,
952 port_req->dev_num, 0x00,
953 SWRS_DP_BLOCK_CONTROL_2_BANK(slv_id,
954 bank));
955 }
956 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
957 reg[len] = SWRM_CMD_FIFO_WR_CMD;
958 val[len++] =
959 SWR_REG_VAL_PACK(mport->lane_ctrl,
960 port_req->dev_num, 0x00,
961 SWRS_DP_LANE_CONTROL_BANK(slv_id,
962 bank));
963 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530964 port_req->ch_en = port_req->req_ch;
965 }
966 value = ((mport->req_ch)
967 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +0530968
969 if (mport->offset2 != SWR_INVALID_PARAM)
970 value |= ((mport->offset2)
971 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530972 value |= ((mport->offset1)
973 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
974 value |= mport->sinterval;
975
976
977 reg[len] = SWRM_DP_PORT_CTRL_BANK(i + 1, bank);
978 val[len++] = value;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530979 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
980 __func__, i,
981 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)), value);
982
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530983 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
984 reg[len] = SWRM_DP_PORT_CTRL_2_BANK(i + 1, bank);
985 val[len++] = mport->lane_ctrl;
986 }
987 if (mport->word_length != SWR_INVALID_PARAM) {
988 reg[len] = SWRM_DP_BLOCK_CTRL_1(i + 1);
989 val[len++] = mport->word_length;
990 }
991
992 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
993 reg[len] = SWRM_DP_BLOCK_CTRL2_BANK(i + 1, bank);
994 val[len++] = mport->blk_grp_count;
995 }
996 if (mport->hstart != SWR_INVALID_PARAM
997 && mport->hstop != SWR_INVALID_PARAM) {
998 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
Laxminath Kasame30eef72018-11-05 17:40:09 +0530999 hparams = (mport->hstop << 4) | mport->hstart;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301000 val[len++] = hparams;
Laxminath Kasam990c70b2018-11-09 23:15:09 +05301001 } else {
1002 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
1003 hparams = (SWR_HSTOP_MAX_VAL << 4) | SWR_HSTART_MIN_VAL;
1004 val[len++] = hparams;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301005 }
1006 if (mport->blk_pack_mode != SWR_INVALID_PARAM) {
1007 reg[len] = SWRM_DP_BLOCK_CTRL3_BANK(i + 1, bank);
1008 val[len++] = mport->blk_pack_mode;
1009 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301010 mport->ch_en = mport->req_ch;
1011
1012 }
Sudheer Papothi0016db12019-06-11 04:42:38 +05301013 swrm_reg_dump(swrm, reg, val, len, __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301014 swr_master_bulk_write(swrm, reg, val, len);
1015}
1016
1017static void swrm_apply_port_config(struct swr_master *master)
1018{
1019 u8 bank;
1020 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1021
1022 if (!swrm) {
1023 pr_err("%s: Invalid handle to swr controller\n",
1024 __func__);
1025 return;
1026 }
1027
1028 bank = get_inactive_bank_num(swrm);
1029 dev_dbg(swrm->dev, "%s: enter bank: %d master_ports: %d\n",
1030 __func__, bank, master->num_port);
1031
1032
1033 swrm_cmd_fifo_wr_cmd(swrm, 0x01, 0xF, 0x00,
1034 SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(bank));
1035
1036 swrm_copy_data_port_config(master, bank);
1037}
1038
1039static int swrm_slvdev_datapath_control(struct swr_master *master, bool enable)
1040{
1041 u8 bank;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301042 u32 value, n_row, n_col;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301043 int ret;
1044 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1045 int mask = (SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK |
1046 SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK |
1047 SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_BMSK);
1048 u8 inactive_bank;
1049
1050 if (!swrm) {
1051 pr_err("%s: swrm is null\n", __func__);
1052 return -EFAULT;
1053 }
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301054
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301055 mutex_lock(&swrm->mlock);
1056
Ramprasad Katkam979b7c92019-05-17 15:31:21 +05301057 /*
1058 * During disable if master is already down, which implies an ssr/pdr
1059 * scenario, just mark ports as disabled and exit
1060 */
1061 if (swrm->state == SWR_MSTR_SSR && !enable) {
1062 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1063 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1064 __func__);
1065 goto exit;
1066 }
1067 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
1068 swrm_cleanup_disabled_port_reqs(master);
1069 if (!swrm_is_port_en(master)) {
1070 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1071 __func__);
1072 pm_runtime_mark_last_busy(swrm->dev);
1073 pm_runtime_put_autosuspend(swrm->dev);
1074 }
1075 goto exit;
1076 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301077 bank = get_inactive_bank_num(swrm);
1078
1079 if (enable) {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301080 if (!test_bit(ENABLE_PENDING, &swrm->port_req_pending)) {
1081 dev_dbg(swrm->dev, "%s:No pending connect port req\n",
1082 __func__);
1083 goto exit;
1084 }
1085 clear_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301086 ret = swrm_get_port_config(swrm);
1087 if (ret) {
1088 /* cannot accommodate ports */
1089 swrm_cleanup_disabled_port_reqs(master);
1090 mutex_unlock(&swrm->mlock);
1091 return -EINVAL;
1092 }
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301093 swr_master_write(swrm, SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301094 SWRM_INTERRUPT_STATUS_MASK);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301095 /* apply the new port config*/
1096 swrm_apply_port_config(master);
1097 } else {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301098 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1099 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1100 __func__);
1101 goto exit;
1102 }
1103 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301104 swrm_disable_ports(master, bank);
1105 }
1106 dev_dbg(swrm->dev, "%s: enable: %d, cfg_devs: %d\n",
1107 __func__, enable, swrm->num_cfg_devs);
1108
1109 if (enable) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301110 /* set col = 16 */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301111 n_col = SWR_MAX_COL;
1112 } else {
1113 /*
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301114 * Do not change to col = 2 if there are still active ports
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301115 */
1116 if (!master->num_port)
1117 n_col = SWR_MIN_COL;
1118 else
1119 n_col = SWR_MAX_COL;
1120 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301121 /* Use default 50 * x, frame shape. Change based on mclk */
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301122 if (swrm->mclk_freq == MCLK_FREQ_NATIVE) {
1123 dev_dbg(swrm->dev, "setting 64 x %d frameshape\n",
1124 n_col ? 16 : 2);
1125 n_row = SWR_ROW_64;
1126 } else {
1127 dev_dbg(swrm->dev, "setting 50 x %d frameshape\n",
1128 n_col ? 16 : 2);
1129 n_row = SWR_ROW_50;
1130 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301131 value = swr_master_read(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank));
1132 value &= (~mask);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301133 value |= ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301134 (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
1135 (0 << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
1136 swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1137
1138 dev_dbg(swrm->dev, "%s: regaddr: 0x%x, value: 0x%x\n", __func__,
1139 SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1140
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301141 enable_bank_switch(swrm, bank, n_row, n_col);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301142 inactive_bank = bank ? 0 : 1;
1143
1144 if (enable)
1145 swrm_copy_data_port_config(master, inactive_bank);
1146 else {
1147 swrm_disable_ports(master, inactive_bank);
1148 swrm_cleanup_disabled_port_reqs(master);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301149 }
1150 if (!swrm_is_port_en(master)) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301151 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1152 __func__);
1153 pm_runtime_mark_last_busy(swrm->dev);
1154 pm_runtime_put_autosuspend(swrm->dev);
1155 }
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301156exit:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301157 mutex_unlock(&swrm->mlock);
1158return 0;
1159}
1160
1161static int swrm_connect_port(struct swr_master *master,
1162 struct swr_params *portinfo)
1163{
1164 int i;
1165 struct swr_port_info *port_req;
1166 int ret = 0;
1167 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1168 struct swrm_mports *mport;
1169 u8 mstr_port_id, mstr_ch_msk;
1170
1171 dev_dbg(&master->dev, "%s: enter\n", __func__);
1172 if (!portinfo)
1173 return -EINVAL;
1174
1175 if (!swrm) {
1176 dev_err(&master->dev,
1177 "%s: Invalid handle to swr controller\n",
1178 __func__);
1179 return -EINVAL;
1180 }
1181
1182 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301183 mutex_lock(&swrm->devlock);
1184 if (!swrm->dev_up) {
1185 mutex_unlock(&swrm->devlock);
1186 mutex_unlock(&swrm->mlock);
1187 return -EINVAL;
1188 }
1189 mutex_unlock(&swrm->devlock);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301190 if (!swrm_is_port_en(master))
1191 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301192
1193 for (i = 0; i < portinfo->num_port; i++) {
1194 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_msk,
1195 portinfo->port_type[i],
1196 portinfo->port_id[i]);
1197 if (ret) {
1198 dev_err(&master->dev,
1199 "%s: mstr portid for slv port %d not found\n",
1200 __func__, portinfo->port_id[i]);
1201 goto port_fail;
1202 }
1203
1204 mport = &(swrm->mport_cfg[mstr_port_id]);
1205 /* get port req */
1206 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1207 portinfo->dev_num);
1208 if (!port_req) {
1209 dev_dbg(&master->dev, "%s: new req:port id %d dev %d\n",
1210 __func__, portinfo->port_id[i],
1211 portinfo->dev_num);
1212 port_req = kzalloc(sizeof(struct swr_port_info),
1213 GFP_KERNEL);
1214 if (!port_req) {
1215 ret = -ENOMEM;
1216 goto mem_fail;
1217 }
1218 port_req->dev_num = portinfo->dev_num;
1219 port_req->slave_port_id = portinfo->port_id[i];
1220 port_req->num_ch = portinfo->num_ch[i];
1221 port_req->ch_rate = portinfo->ch_rate[i];
1222 port_req->ch_en = 0;
1223 port_req->master_port_id = mstr_port_id;
1224 list_add(&port_req->list, &mport->port_req_list);
1225 }
1226 port_req->req_ch |= portinfo->ch_en[i];
1227
1228 dev_dbg(&master->dev,
1229 "%s: mstr port %d, slv port %d ch_rate %d num_ch %d\n",
1230 __func__, port_req->master_port_id,
1231 port_req->slave_port_id, port_req->ch_rate,
1232 port_req->num_ch);
1233 /* Put the port req on master port */
1234 mport = &(swrm->mport_cfg[mstr_port_id]);
1235 mport->port_en = true;
1236 mport->req_ch |= mstr_ch_msk;
1237 master->port_en_mask |= (1 << mstr_port_id);
1238 }
1239 master->num_port += portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301240 set_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301241 swr_port_response(master, portinfo->tid);
1242
1243 mutex_unlock(&swrm->mlock);
1244 return 0;
1245
1246port_fail:
1247mem_fail:
1248 /* cleanup port reqs in error condition */
1249 swrm_cleanup_disabled_port_reqs(master);
1250 mutex_unlock(&swrm->mlock);
1251 return ret;
1252}
1253
1254static int swrm_disconnect_port(struct swr_master *master,
1255 struct swr_params *portinfo)
1256{
1257 int i, ret = 0;
1258 struct swr_port_info *port_req;
1259 struct swrm_mports *mport;
1260 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1261 u8 mstr_port_id, mstr_ch_mask;
1262
1263 if (!swrm) {
1264 dev_err(&master->dev,
1265 "%s: Invalid handle to swr controller\n",
1266 __func__);
1267 return -EINVAL;
1268 }
1269
1270 if (!portinfo) {
1271 dev_err(&master->dev, "%s: portinfo is NULL\n", __func__);
1272 return -EINVAL;
1273 }
1274 mutex_lock(&swrm->mlock);
1275
1276 for (i = 0; i < portinfo->num_port; i++) {
1277
1278 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_mask,
1279 portinfo->port_type[i], portinfo->port_id[i]);
1280 if (ret) {
1281 dev_err(&master->dev,
1282 "%s: mstr portid for slv port %d not found\n",
1283 __func__, portinfo->port_id[i]);
1284 mutex_unlock(&swrm->mlock);
1285 return -EINVAL;
1286 }
1287 mport = &(swrm->mport_cfg[mstr_port_id]);
1288 /* get port req */
1289 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1290 portinfo->dev_num);
1291
1292 if (!port_req) {
1293 dev_err(&master->dev, "%s:port not enabled : port %d\n",
1294 __func__, portinfo->port_id[i]);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05301295 mutex_unlock(&swrm->mlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301296 return -EINVAL;
1297 }
1298 port_req->req_ch &= ~portinfo->ch_en[i];
1299 mport->req_ch &= ~mstr_ch_mask;
1300 }
1301 master->num_port -= portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301302 set_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301303 swr_port_response(master, portinfo->tid);
1304 mutex_unlock(&swrm->mlock);
1305
1306 return 0;
1307}
1308
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301309static int swrm_find_alert_slave(struct swr_mstr_ctrl *swrm,
1310 int status, u8 *devnum)
1311{
1312 int i;
1313 bool found = false;
1314
1315 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1316 if ((status & SWRM_MCP_SLV_STATUS_MASK) == SWR_ALERT) {
1317 *devnum = i;
1318 found = true;
1319 break;
1320 }
1321 status >>= 2;
1322 }
1323 if (found)
1324 return 0;
1325 else
1326 return -EINVAL;
1327}
1328
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301329static void swrm_enable_slave_irq(struct swr_mstr_ctrl *swrm)
1330{
1331 int i;
1332 int status = 0;
1333
1334 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1335 if (!status) {
1336 dev_dbg_ratelimited(swrm->dev, "%s: slaves status is 0x%x\n",
1337 __func__, status);
1338 return;
1339 }
1340 dev_dbg(swrm->dev, "%s: slave status: 0x%x\n", __func__, status);
1341 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1342 if (status & SWRM_MCP_SLV_STATUS_MASK)
1343 swrm_cmd_fifo_wr_cmd(swrm, 0x4, i, 0x0,
1344 SWRS_SCP_INT_STATUS_MASK_1);
1345 status >>= 2;
1346 }
1347}
1348
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301349static int swrm_check_slave_change_status(struct swr_mstr_ctrl *swrm,
1350 int status, u8 *devnum)
1351{
1352 int i;
1353 int new_sts = status;
1354 int ret = SWR_NOT_PRESENT;
1355
1356 if (status != swrm->slave_status) {
1357 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1358 if ((status & SWRM_MCP_SLV_STATUS_MASK) !=
1359 (swrm->slave_status & SWRM_MCP_SLV_STATUS_MASK)) {
1360 ret = (status & SWRM_MCP_SLV_STATUS_MASK);
1361 *devnum = i;
1362 break;
1363 }
1364 status >>= 2;
1365 swrm->slave_status >>= 2;
1366 }
1367 swrm->slave_status = new_sts;
1368 }
1369 return ret;
1370}
1371
1372static irqreturn_t swr_mstr_interrupt(int irq, void *dev)
1373{
1374 struct swr_mstr_ctrl *swrm = dev;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301375 u32 value, intr_sts, intr_sts_masked;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301376 u32 temp = 0;
1377 u32 status, chg_sts, i;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301378 u8 devnum = 0;
1379 int ret = IRQ_HANDLED;
1380 struct swr_device *swr_dev;
1381 struct swr_master *mstr = &swrm->master;
1382
Ramprasad Katkam57349872018-11-11 18:34:57 +05301383 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1384 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1385 return IRQ_NONE;
1386 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301387
1388 mutex_lock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301389 if (swrm_clk_request(swrm, true)) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +05301390 dev_err_ratelimited(swrm->dev, "%s:clk request failed\n",
1391 __func__);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301392 mutex_unlock(&swrm->reslock);
1393 goto exit;
1394 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301395 mutex_unlock(&swrm->reslock);
1396
1397 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301398 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301399handle_irq:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301400 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301401 value = intr_sts_masked & (1 << i);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301402 if (!value)
1403 continue;
1404
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301405 switch (value) {
1406 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1407 dev_dbg(swrm->dev, "Trigger irq to slave device\n");
1408 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301409 ret = swrm_find_alert_slave(swrm, status, &devnum);
1410 if (ret) {
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301411 dev_err_ratelimited(swrm->dev,
1412 "no slave alert found.spurious interrupt\n");
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05301413 break;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301414 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301415 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1416 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1417 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1418 SWRS_SCP_INT_STATUS_CLEAR_1);
1419 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1420 SWRS_SCP_INT_STATUS_CLEAR_1);
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301421
1422
1423 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1424 if (swr_dev->dev_num != devnum)
1425 continue;
1426 if (swr_dev->slave_irq) {
1427 do {
Ramprasad Katkam2586a4b2019-03-18 16:53:39 +05301428 swr_dev->slave_irq_pending = 0;
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301429 handle_nested_irq(
1430 irq_find_mapping(
1431 swr_dev->slave_irq, 0));
1432 } while (swr_dev->slave_irq_pending);
1433 }
1434
1435 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301436 break;
1437 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1438 dev_dbg(swrm->dev, "SWR new slave attached\n");
1439 break;
1440 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1441 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1442 if (status == swrm->slave_status) {
1443 dev_dbg(swrm->dev,
1444 "%s: No change in slave status: %d\n",
1445 __func__, status);
1446 break;
1447 }
1448 chg_sts = swrm_check_slave_change_status(swrm, status,
1449 &devnum);
1450 switch (chg_sts) {
1451 case SWR_NOT_PRESENT:
1452 dev_dbg(swrm->dev, "device %d got detached\n",
1453 devnum);
1454 break;
1455 case SWR_ATTACHED_OK:
1456 dev_dbg(swrm->dev, "device %d got attached\n",
1457 devnum);
Ramprasad Katkamdebe8932018-09-25 18:08:18 +05301458 /* enable host irq from slave device*/
1459 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1460 SWRS_SCP_INT_STATUS_CLEAR_1);
1461 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1462 SWRS_SCP_INT_STATUS_MASK_1);
1463
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301464 break;
1465 case SWR_ALERT:
1466 dev_dbg(swrm->dev,
1467 "device %d has pending interrupt\n",
1468 devnum);
1469 break;
1470 }
1471 break;
1472 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1473 dev_err_ratelimited(swrm->dev,
1474 "SWR bus clsh detected\n");
1475 break;
1476 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1477 dev_dbg(swrm->dev, "SWR read FIFO overflow\n");
1478 break;
1479 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1480 dev_dbg(swrm->dev, "SWR read FIFO underflow\n");
1481 break;
1482 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1483 dev_dbg(swrm->dev, "SWR write FIFO overflow\n");
1484 break;
1485 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1486 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1487 dev_err_ratelimited(swrm->dev,
1488 "SWR CMD error, fifo status 0x%x, flushing fifo\n",
1489 value);
1490 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1491 break;
1492 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301493 dev_err_ratelimited(swrm->dev, "SWR Port collision detected\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301494 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301495 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301496 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301497 break;
1498 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1499 dev_dbg(swrm->dev, "SWR read enable valid mismatch\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301500 swrm->intr_mask &=
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301501 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1502 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301503 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301504 break;
1505 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1506 complete(&swrm->broadcast);
1507 dev_dbg(swrm->dev, "SWR cmd id finished\n");
1508 break;
1509 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_AUTO_ENUM_FINISHED:
1510 break;
1511 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED:
1512 break;
1513 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL:
1514 break;
1515 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED:
1516 complete(&swrm->reset);
1517 break;
1518 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED:
1519 break;
1520 default:
1521 dev_err_ratelimited(swrm->dev,
1522 "SWR unknown interrupt\n");
1523 ret = IRQ_NONE;
1524 break;
1525 }
1526 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301527 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1528 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
Ramprasad Katkam83303512018-10-11 17:34:22 +05301529
1530 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301531 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301532
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301533 if (intr_sts_masked) {
Ramprasad Katkam83303512018-10-11 17:34:22 +05301534 dev_dbg(swrm->dev, "%s: new interrupt received\n", __func__);
1535 goto handle_irq;
1536 }
1537
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301538 mutex_lock(&swrm->reslock);
1539 swrm_clk_request(swrm, false);
1540 mutex_unlock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301541exit:
Ramprasad Katkam57349872018-11-11 18:34:57 +05301542 swrm_unlock_sleep(swrm);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301543 return ret;
1544}
1545
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301546static irqreturn_t swr_mstr_interrupt_v2(int irq, void *dev)
1547{
1548 struct swr_mstr_ctrl *swrm = dev;
1549 u32 value, intr_sts, intr_sts_masked;
1550 u32 temp = 0;
1551 u32 status, chg_sts, i;
1552 u8 devnum = 0;
1553 int ret = IRQ_HANDLED;
1554 struct swr_device *swr_dev;
1555 struct swr_master *mstr = &swrm->master;
1556
1557 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1558 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1559 return IRQ_NONE;
1560 }
1561
1562 mutex_lock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301563 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
1564 ret = IRQ_NONE;
1565 goto exit;
1566 }
1567 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
1568 ret = IRQ_NONE;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301569 goto err_audio_hw_vote;
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001570 }
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301571 swrm_clk_request(swrm, true);
1572 mutex_unlock(&swrm->reslock);
1573
1574 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1575 intr_sts_masked = intr_sts & swrm->intr_mask;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301576
1577 dev_dbg(swrm->dev, "%s: status: 0x%x \n", __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301578handle_irq:
1579 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
1580 value = intr_sts_masked & (1 << i);
1581 if (!value)
1582 continue;
1583
1584 switch (value) {
1585 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1586 dev_dbg(swrm->dev, "%s: Trigger irq to slave device\n",
1587 __func__);
1588 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1589 ret = swrm_find_alert_slave(swrm, status, &devnum);
1590 if (ret) {
1591 dev_err_ratelimited(swrm->dev,
1592 "%s: no slave alert found.spurious interrupt\n",
1593 __func__);
1594 break;
1595 }
1596 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1597 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1598 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1599 SWRS_SCP_INT_STATUS_CLEAR_1);
1600 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1601 SWRS_SCP_INT_STATUS_CLEAR_1);
1602
1603
1604 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1605 if (swr_dev->dev_num != devnum)
1606 continue;
1607 if (swr_dev->slave_irq) {
1608 do {
1609 handle_nested_irq(
1610 irq_find_mapping(
1611 swr_dev->slave_irq, 0));
1612 } while (swr_dev->slave_irq_pending);
1613 }
1614
1615 }
1616 break;
1617 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1618 dev_dbg(swrm->dev, "%s: SWR new slave attached\n",
1619 __func__);
1620 break;
1621 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1622 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1623 if (status == swrm->slave_status) {
1624 dev_dbg(swrm->dev,
1625 "%s: No change in slave status: %d\n",
1626 __func__, status);
1627 break;
1628 }
1629 chg_sts = swrm_check_slave_change_status(swrm, status,
1630 &devnum);
1631 switch (chg_sts) {
1632 case SWR_NOT_PRESENT:
1633 dev_dbg(swrm->dev,
1634 "%s: device %d got detached\n",
1635 __func__, devnum);
1636 break;
1637 case SWR_ATTACHED_OK:
1638 dev_dbg(swrm->dev,
1639 "%s: device %d got attached\n",
1640 __func__, devnum);
1641 /* enable host irq from slave device*/
1642 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1643 SWRS_SCP_INT_STATUS_CLEAR_1);
1644 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1645 SWRS_SCP_INT_STATUS_MASK_1);
1646
1647 break;
1648 case SWR_ALERT:
1649 dev_dbg(swrm->dev,
1650 "%s: device %d has pending interrupt\n",
1651 __func__, devnum);
1652 break;
1653 }
1654 break;
1655 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1656 dev_err_ratelimited(swrm->dev,
1657 "%s: SWR bus clsh detected\n",
1658 __func__);
1659 break;
1660 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1661 dev_dbg(swrm->dev, "%s: SWR read FIFO overflow\n",
1662 __func__);
1663 break;
1664 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1665 dev_dbg(swrm->dev, "%s: SWR read FIFO underflow\n",
1666 __func__);
1667 break;
1668 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1669 dev_dbg(swrm->dev, "%s: SWR write FIFO overflow\n",
1670 __func__);
1671 break;
1672 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1673 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1674 dev_err_ratelimited(swrm->dev,
1675 "%s: SWR CMD error, fifo status 0x%x, flushing fifo\n",
1676 __func__, value);
1677 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1678 break;
1679 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
1680 dev_err_ratelimited(swrm->dev,
1681 "%s: SWR Port collision detected\n",
1682 __func__);
1683 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
1684 swr_master_write(swrm,
1685 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1686 break;
1687 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1688 dev_dbg(swrm->dev,
1689 "%s: SWR read enable valid mismatch\n",
1690 __func__);
1691 swrm->intr_mask &=
1692 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1693 swr_master_write(swrm,
1694 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1695 break;
1696 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1697 complete(&swrm->broadcast);
1698 dev_dbg(swrm->dev, "%s: SWR cmd id finished\n",
1699 __func__);
1700 break;
1701 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED_V2:
1702 break;
1703 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL_V2:
1704 break;
1705 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED_V2:
1706 break;
1707 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED_V2:
1708 break;
1709 case SWRM_INTERRUPT_STATUS_EXT_CLK_STOP_WAKEUP:
1710 if (swrm->state == SWR_MSTR_UP)
1711 dev_dbg(swrm->dev,
1712 "%s:SWR Master is already up\n",
1713 __func__);
1714 else
1715 dev_err_ratelimited(swrm->dev,
1716 "%s: SWR wokeup during clock stop\n",
1717 __func__);
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301718 /* It might be possible the slave device gets reset
1719 * and slave interrupt gets missed. So re-enable
1720 * Host IRQ and process slave pending
1721 * interrupts, if any.
1722 */
1723 swrm_enable_slave_irq(swrm);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301724 break;
1725 default:
1726 dev_err_ratelimited(swrm->dev,
1727 "%s: SWR unknown interrupt value: %d\n",
1728 __func__, value);
1729 ret = IRQ_NONE;
1730 break;
1731 }
1732 }
1733 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1734 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
1735
1736 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1737 intr_sts_masked = intr_sts & swrm->intr_mask;
1738
1739 if (intr_sts_masked) {
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301740 dev_dbg(swrm->dev, "%s: new interrupt received 0x%x\n",
1741 __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301742 goto handle_irq;
1743 }
1744
1745 mutex_lock(&swrm->reslock);
1746 swrm_clk_request(swrm, false);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301747 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
Sudheer Papothi06f43412019-07-09 03:32:54 +05301748
1749err_audio_hw_vote:
Sudheer Papothi384addd2019-06-14 02:26:52 +05301750 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001751exit:
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301752 mutex_unlock(&swrm->reslock);
1753 swrm_unlock_sleep(swrm);
1754 return ret;
1755}
1756
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301757static irqreturn_t swrm_wakeup_interrupt(int irq, void *dev)
1758{
1759 struct swr_mstr_ctrl *swrm = dev;
1760 int ret = IRQ_HANDLED;
1761
1762 if (!swrm || !(swrm->dev)) {
1763 pr_err("%s: swrm or dev is null\n", __func__);
1764 return IRQ_NONE;
1765 }
1766 mutex_lock(&swrm->devlock);
1767 if (!swrm->dev_up) {
1768 if (swrm->wake_irq > 0)
1769 disable_irq_nosync(swrm->wake_irq);
1770 mutex_unlock(&swrm->devlock);
1771 return ret;
1772 }
1773 mutex_unlock(&swrm->devlock);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301774 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1775 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1776 goto exit;
1777 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301778 if (swrm->wake_irq > 0)
1779 disable_irq_nosync(swrm->wake_irq);
1780 pm_runtime_get_sync(swrm->dev);
1781 pm_runtime_mark_last_busy(swrm->dev);
1782 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301783 swrm_unlock_sleep(swrm);
1784exit:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301785 return ret;
1786}
1787
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301788static void swrm_wakeup_work(struct work_struct *work)
1789{
1790 struct swr_mstr_ctrl *swrm;
1791
1792 swrm = container_of(work, struct swr_mstr_ctrl,
1793 wakeup_work);
1794 if (!swrm || !(swrm->dev)) {
1795 pr_err("%s: swrm or dev is null\n", __func__);
1796 return;
1797 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301798
1799 mutex_lock(&swrm->devlock);
1800 if (!swrm->dev_up) {
1801 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301802 goto exit;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301803 }
1804 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301805 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1806 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1807 goto exit;
1808 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301809 pm_runtime_get_sync(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301810 pm_runtime_mark_last_busy(swrm->dev);
1811 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301812 swrm_unlock_sleep(swrm);
1813exit:
1814 pm_relax(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301815}
1816
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301817static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
1818{
1819 u32 val;
1820
1821 swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1822 val = (swrm->slave_status >> (devnum * 2));
1823 val &= SWRM_MCP_SLV_STATUS_MASK;
1824 return val;
1825}
1826
1827static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
1828 u8 *dev_num)
1829{
1830 int i;
1831 u64 id = 0;
1832 int ret = -EINVAL;
1833 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1834 struct swr_device *swr_dev;
1835 u32 num_dev = 0;
1836
1837 if (!swrm) {
1838 pr_err("%s: Invalid handle to swr controller\n",
1839 __func__);
1840 return ret;
1841 }
1842 if (swrm->num_dev)
1843 num_dev = swrm->num_dev;
1844 else
1845 num_dev = mstr->num_dev;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301846
1847 mutex_lock(&swrm->devlock);
1848 if (!swrm->dev_up) {
1849 mutex_unlock(&swrm->devlock);
1850 return ret;
1851 }
1852 mutex_unlock(&swrm->devlock);
1853
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301854 pm_runtime_get_sync(swrm->dev);
1855 for (i = 1; i < (num_dev + 1); i++) {
1856 id = ((u64)(swr_master_read(swrm,
1857 SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
1858 id |= swr_master_read(swrm,
1859 SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301860
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301861 /*
1862 * As pm_runtime_get_sync() brings all slaves out of reset
1863 * update logical device number for all slaves.
1864 */
1865 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1866 if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
1867 u32 status = swrm_get_device_status(swrm, i);
1868
1869 if ((status == 0x01) || (status == 0x02)) {
1870 swr_dev->dev_num = i;
1871 if ((id & SWR_DEV_ID_MASK) == dev_id) {
1872 *dev_num = i;
1873 ret = 0;
1874 }
1875 dev_dbg(swrm->dev,
1876 "%s: devnum %d is assigned for dev addr %lx\n",
1877 __func__, i, swr_dev->addr);
1878 }
1879 }
1880 }
1881 }
1882 if (ret)
1883 dev_err(swrm->dev, "%s: device 0x%llx is not ready\n",
1884 __func__, dev_id);
1885
1886 pm_runtime_mark_last_busy(swrm->dev);
1887 pm_runtime_put_autosuspend(swrm->dev);
1888 return ret;
1889}
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301890
1891static void swrm_device_wakeup_vote(struct swr_master *mstr)
1892{
1893 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1894
1895 if (!swrm) {
1896 pr_err("%s: Invalid handle to swr controller\n",
1897 __func__);
1898 return;
1899 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05301900 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1901 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1902 return;
1903 }
Sudheer Papothi384addd2019-06-14 02:26:52 +05301904 if (++swrm->hw_core_clk_en == 1)
1905 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
1906 dev_err(swrm->dev, "%s:lpass core hw enable failed\n",
1907 __func__);
1908 --swrm->hw_core_clk_en;
1909 }
1910 if ( ++swrm->aud_core_clk_en == 1)
1911 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
1912 dev_err(swrm->dev, "%s:lpass audio hw enable failed\n",
1913 __func__);
1914 --swrm->aud_core_clk_en;
1915 }
1916 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
1917 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301918 pm_runtime_get_sync(swrm->dev);
1919}
1920
1921static void swrm_device_wakeup_unvote(struct swr_master *mstr)
1922{
1923 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1924
1925 if (!swrm) {
1926 pr_err("%s: Invalid handle to swr controller\n",
1927 __func__);
1928 return;
1929 }
1930 pm_runtime_mark_last_busy(swrm->dev);
1931 pm_runtime_put_autosuspend(swrm->dev);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301932 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
1933 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
1934
1935 --swrm->aud_core_clk_en;
1936 if (swrm->aud_core_clk_en < 0)
1937 swrm->aud_core_clk_en = 0;
1938 else if (swrm->aud_core_clk_en == 0)
1939 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
1940
1941 --swrm->hw_core_clk_en;
1942 if (swrm->hw_core_clk_en < 0)
1943 swrm->hw_core_clk_en = 0;
1944 else if (swrm->hw_core_clk_en == 0)
1945 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
1946
Ramprasad Katkam57349872018-11-11 18:34:57 +05301947 swrm_unlock_sleep(swrm);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301948}
1949
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301950static int swrm_master_init(struct swr_mstr_ctrl *swrm)
1951{
1952 int ret = 0;
1953 u32 val;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301954 u8 row_ctrl = SWR_ROW_50;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301955 u8 col_ctrl = SWR_MIN_COL;
1956 u8 ssp_period = 1;
1957 u8 retry_cmd_num = 3;
1958 u32 reg[SWRM_MAX_INIT_REG];
1959 u32 value[SWRM_MAX_INIT_REG];
1960 int len = 0;
1961
1962 /* Clear Rows and Cols */
1963 val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
1964 (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
1965 (ssp_period << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
1966
1967 reg[len] = SWRM_MCP_FRAME_CTRL_BANK_ADDR(0);
1968 value[len++] = val;
1969
1970 /* Set Auto enumeration flag */
1971 reg[len] = SWRM_ENUMERATOR_CFG_ADDR;
1972 value[len++] = 1;
1973
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301974 /* Configure No pings */
1975 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
1976 val &= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK;
1977 val |= (0x1f << SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT);
1978 reg[len] = SWRM_MCP_CFG_ADDR;
1979 value[len++] = val;
1980
1981 /* Configure number of retries of a read/write cmd */
1982 val = (retry_cmd_num << SWRM_CMD_FIFO_CFG_NUM_OF_CMD_RETRY_SHFT);
1983 reg[len] = SWRM_CMD_FIFO_CFG_ADDR;
1984 value[len++] = val;
1985
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301986 reg[len] = SWRM_MCP_BUS_CTRL_ADDR;
1987 value[len++] = 0x2;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301988
Ramprasad Katkam83303512018-10-11 17:34:22 +05301989 /* Set IRQ to PULSE */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301990 reg[len] = SWRM_COMP_CFG_ADDR;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301991 value[len++] = 0x02;
1992
1993 reg[len] = SWRM_COMP_CFG_ADDR;
1994 value[len++] = 0x03;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301995
1996 reg[len] = SWRM_INTERRUPT_CLEAR;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301997 value[len++] = 0xFFFFFFFF;
1998
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301999 swrm->intr_mask = SWRM_INTERRUPT_STATUS_MASK;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302000 /* Mask soundwire interrupts */
2001 reg[len] = SWRM_INTERRUPT_MASK_ADDR;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302002 value[len++] = swrm->intr_mask;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302003
2004 reg[len] = SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05302005 value[len++] = swrm->intr_mask;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302006
2007 swr_master_bulk_write(swrm, reg, value, len);
2008
Sudheer Papothi63f48152018-11-15 01:08:03 +05302009 /*
2010 * For SWR master version 1.5.1, continue
2011 * execute on command ignore.
2012 */
2013 if (swrm->version == SWRM_VERSION_1_5_1)
2014 swr_master_write(swrm, SWRM_CMD_FIFO_CFG_ADDR,
2015 (swr_master_read(swrm,
2016 SWRM_CMD_FIFO_CFG_ADDR) | 0x80000000));
2017
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302018 return ret;
2019}
2020
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302021static int swrm_event_notify(struct notifier_block *self,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302022 unsigned long action, void *data)
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302023{
2024 struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302025 event_notifier);
2026
2027 if (!swrm || !(swrm->dev)) {
2028 pr_err("%s: swrm or dev is NULL\n", __func__);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302029 return -EINVAL;
2030 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302031 switch (action) {
2032 case MSM_AUD_DC_EVENT:
2033 schedule_work(&(swrm->dc_presence_work));
2034 break;
2035 case SWR_WAKE_IRQ_EVENT:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302036 if (swrm->ipc_wakeup && !swrm->ipc_wakeup_triggered) {
2037 swrm->ipc_wakeup_triggered = true;
Ramprasad Katkam57349872018-11-11 18:34:57 +05302038 pm_stay_awake(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302039 schedule_work(&swrm->wakeup_work);
Ramprasad Katkamcd61c6e2018-09-18 13:22:58 +05302040 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302041 break;
2042 default:
2043 dev_err(swrm->dev, "%s: invalid event type: %lu\n",
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302044 __func__, action);
2045 return -EINVAL;
2046 }
2047
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302048 return 0;
2049}
2050
2051static void swrm_notify_work_fn(struct work_struct *work)
2052{
2053 struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302054 dc_presence_work);
2055
2056 if (!swrm || !swrm->pdev) {
2057 pr_err("%s: swrm or pdev is NULL\n", __func__);
2058 return;
2059 }
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302060 swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL);
2061}
2062
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302063static int swrm_probe(struct platform_device *pdev)
2064{
2065 struct swr_mstr_ctrl *swrm;
2066 struct swr_ctrl_platform_data *pdata;
2067 u32 i, num_ports, port_num, port_type, ch_mask;
2068 u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0;
2069 int ret = 0;
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302070 struct clk *lpass_core_hw_vote = NULL;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302071 struct clk *lpass_core_audio = NULL;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302072
2073 /* Allocate soundwire master driver structure */
2074 swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl),
2075 GFP_KERNEL);
2076 if (!swrm) {
2077 ret = -ENOMEM;
2078 goto err_memory_fail;
2079 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302080 swrm->pdev = pdev;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302081 swrm->dev = &pdev->dev;
2082 platform_set_drvdata(pdev, swrm);
2083 swr_set_ctrl_data(&swrm->master, swrm);
2084 pdata = dev_get_platdata(&pdev->dev);
2085 if (!pdata) {
2086 dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
2087 __func__);
2088 ret = -EINVAL;
2089 goto err_pdata_fail;
2090 }
2091 swrm->handle = (void *)pdata->handle;
2092 if (!swrm->handle) {
2093 dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
2094 __func__);
2095 ret = -EINVAL;
2096 goto err_pdata_fail;
2097 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302098 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr_master_id",
2099 &swrm->master_id);
2100 if (ret) {
2101 dev_err(&pdev->dev, "%s: failed to get master id\n", __func__);
2102 goto err_pdata_fail;
2103 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302104 if (!(of_property_read_u32(pdev->dev.of_node,
2105 "swrm-io-base", &swrm->swrm_base_reg)))
2106 ret = of_property_read_u32(pdev->dev.of_node,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302107 "swrm-io-base", &swrm->swrm_base_reg);
2108 if (!swrm->swrm_base_reg) {
2109 swrm->read = pdata->read;
2110 if (!swrm->read) {
2111 dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
2112 __func__);
2113 ret = -EINVAL;
2114 goto err_pdata_fail;
2115 }
2116 swrm->write = pdata->write;
2117 if (!swrm->write) {
2118 dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
2119 __func__);
2120 ret = -EINVAL;
2121 goto err_pdata_fail;
2122 }
2123 swrm->bulk_write = pdata->bulk_write;
2124 if (!swrm->bulk_write) {
2125 dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
2126 __func__);
2127 ret = -EINVAL;
2128 goto err_pdata_fail;
2129 }
2130 } else {
2131 swrm->swrm_dig_base = devm_ioremap(&pdev->dev,
2132 swrm->swrm_base_reg, SWRM_MAX_REGISTER);
2133 }
2134
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07002135 swrm->core_vote = pdata->core_vote;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302136 swrm->clk = pdata->clk;
2137 if (!swrm->clk) {
2138 dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
2139 __func__);
2140 ret = -EINVAL;
2141 goto err_pdata_fail;
2142 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302143 if (of_property_read_u32(pdev->dev.of_node,
2144 "qcom,swr-clock-stop-mode0",
2145 &swrm->clk_stop_mode0_supp)) {
2146 swrm->clk_stop_mode0_supp = FALSE;
2147 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302148
2149 ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
2150 &swrm->num_dev);
2151 if (ret) {
2152 dev_dbg(&pdev->dev, "%s: Looking up %s property failed\n",
2153 __func__, "qcom,swr-num-dev");
2154 } else {
2155 if (swrm->num_dev > SWR_MAX_SLAVE_DEVICES) {
2156 dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
2157 __func__, swrm->num_dev, SWR_MAX_SLAVE_DEVICES);
2158 ret = -EINVAL;
2159 goto err_pdata_fail;
2160 }
2161 }
2162
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302163 /* Parse soundwire port mapping */
2164 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
2165 &num_ports);
2166 if (ret) {
2167 dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
2168 goto err_pdata_fail;
2169 }
2170 swrm->num_ports = num_ports;
2171
2172 if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
2173 &map_size)) {
2174 dev_err(swrm->dev, "missing port mapping\n");
2175 goto err_pdata_fail;
2176 }
2177
2178 map_length = map_size / (3 * sizeof(u32));
2179 if (num_ports > SWR_MSTR_PORT_LEN) {
2180 dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
2181 __func__);
2182 ret = -EINVAL;
2183 goto err_pdata_fail;
2184 }
2185 temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
2186
2187 if (!temp) {
2188 ret = -ENOMEM;
2189 goto err_pdata_fail;
2190 }
2191 ret = of_property_read_u32_array(pdev->dev.of_node,
2192 "qcom,swr-port-mapping", temp, 3 * map_length);
2193 if (ret) {
2194 dev_err(swrm->dev, "%s: Failed to read port mapping\n",
2195 __func__);
2196 goto err_pdata_fail;
2197 }
2198
2199 for (i = 0; i < map_length; i++) {
2200 port_num = temp[3 * i];
2201 port_type = temp[3 * i + 1];
2202 ch_mask = temp[3 * i + 2];
2203
2204 if (port_num != old_port_num)
2205 ch_iter = 0;
2206 swrm->port_mapping[port_num][ch_iter].port_type = port_type;
2207 swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
2208 old_port_num = port_num;
2209 }
2210 devm_kfree(&pdev->dev, temp);
2211
2212 swrm->reg_irq = pdata->reg_irq;
2213 swrm->master.read = swrm_read;
2214 swrm->master.write = swrm_write;
2215 swrm->master.bulk_write = swrm_bulk_write;
2216 swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
2217 swrm->master.connect_port = swrm_connect_port;
2218 swrm->master.disconnect_port = swrm_disconnect_port;
2219 swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
2220 swrm->master.remove_from_group = swrm_remove_from_group;
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302221 swrm->master.device_wakeup_vote = swrm_device_wakeup_vote;
2222 swrm->master.device_wakeup_unvote = swrm_device_wakeup_unvote;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302223 swrm->master.dev.parent = &pdev->dev;
2224 swrm->master.dev.of_node = pdev->dev.of_node;
2225 swrm->master.num_port = 0;
2226 swrm->rcmd_id = 0;
2227 swrm->wcmd_id = 0;
2228 swrm->slave_status = 0;
2229 swrm->num_rx_chs = 0;
2230 swrm->clk_ref_count = 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302231 swrm->swr_irq_wakeup_capable = 0;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302232 swrm->mclk_freq = MCLK_FREQ;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302233 swrm->dev_up = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302234 swrm->state = SWR_MSTR_UP;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302235 swrm->ipc_wakeup = false;
2236 swrm->ipc_wakeup_triggered = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302237 init_completion(&swrm->reset);
2238 init_completion(&swrm->broadcast);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302239 init_completion(&swrm->clk_off_complete);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302240 mutex_init(&swrm->mlock);
2241 mutex_init(&swrm->reslock);
2242 mutex_init(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302243 mutex_init(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302244 mutex_init(&swrm->clklock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302245 mutex_init(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302246 mutex_init(&swrm->pm_lock);
2247 swrm->wlock_holders = 0;
2248 swrm->pm_state = SWRM_PM_SLEEPABLE;
2249 init_waitqueue_head(&swrm->pm_wq);
2250 pm_qos_add_request(&swrm->pm_qos_req,
2251 PM_QOS_CPU_DMA_LATENCY,
2252 PM_QOS_DEFAULT_VALUE);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302253
2254 for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++)
2255 INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
2256
Sudheer Papothi06f43412019-07-09 03:32:54 +05302257 /* Register LPASS core hw vote */
2258 lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
2259 if (IS_ERR(lpass_core_hw_vote)) {
2260 ret = PTR_ERR(lpass_core_hw_vote);
2261 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2262 __func__, "lpass_core_hw_vote", ret);
2263 lpass_core_hw_vote = NULL;
2264 ret = 0;
2265 }
2266 swrm->lpass_core_hw_vote = lpass_core_hw_vote;
2267
2268 /* Register LPASS audio core vote */
2269 lpass_core_audio = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
2270 if (IS_ERR(lpass_core_audio)) {
2271 ret = PTR_ERR(lpass_core_audio);
2272 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2273 __func__, "lpass_core_audio", ret);
2274 lpass_core_audio = NULL;
2275 ret = 0;
2276 }
2277 swrm->lpass_core_audio = lpass_core_audio;
2278
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302279 if (swrm->reg_irq) {
2280 ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
2281 SWR_IRQ_REGISTER);
2282 if (ret) {
2283 dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
2284 __func__, ret);
2285 goto err_irq_fail;
2286 }
2287 } else {
2288 swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
2289 if (swrm->irq < 0) {
2290 dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
2291 __func__, swrm->irq);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302292 goto err_irq_fail;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302293 }
2294
2295 ret = request_threaded_irq(swrm->irq, NULL,
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302296 swr_mstr_interrupt_v2,
Ramprasad Katkam83303512018-10-11 17:34:22 +05302297 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302298 "swr_master_irq", swrm);
2299 if (ret) {
2300 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2301 __func__, ret);
2302 goto err_irq_fail;
2303 }
2304
2305 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302306 /* Make inband tx interrupts as wakeup capable for slave irq */
2307 ret = of_property_read_u32(pdev->dev.of_node,
2308 "qcom,swr-mstr-irq-wakeup-capable",
2309 &swrm->swr_irq_wakeup_capable);
2310 if (ret)
2311 dev_dbg(swrm->dev, "%s: swrm irq wakeup capable not defined\n",
2312 __func__);
2313 if (swrm->swr_irq_wakeup_capable)
2314 irq_set_irq_wake(swrm->irq, 1);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302315 ret = swr_register_master(&swrm->master);
2316 if (ret) {
2317 dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
2318 goto err_mstr_fail;
2319 }
2320
2321 /* Add devices registered with board-info as the
2322 * controller will be up now
2323 */
2324 swr_master_add_boarddevices(&swrm->master);
2325 mutex_lock(&swrm->mlock);
2326 swrm_clk_request(swrm, true);
2327 ret = swrm_master_init(swrm);
2328 if (ret < 0) {
2329 dev_err(&pdev->dev,
2330 "%s: Error in master Initialization , err %d\n",
2331 __func__, ret);
2332 mutex_unlock(&swrm->mlock);
2333 goto err_mstr_fail;
2334 }
2335 swrm->version = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
2336
2337 mutex_unlock(&swrm->mlock);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302338 INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302339
2340 if (pdev->dev.of_node)
2341 of_register_swr_devices(&swrm->master);
2342
2343 dbgswrm = swrm;
2344 debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
2345 if (!IS_ERR(debugfs_swrm_dent)) {
2346 debugfs_peek = debugfs_create_file("swrm_peek",
2347 S_IFREG | 0444, debugfs_swrm_dent,
2348 (void *) "swrm_peek", &swrm_debug_ops);
2349
2350 debugfs_poke = debugfs_create_file("swrm_poke",
2351 S_IFREG | 0444, debugfs_swrm_dent,
2352 (void *) "swrm_poke", &swrm_debug_ops);
2353
2354 debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
2355 S_IFREG | 0444, debugfs_swrm_dent,
2356 (void *) "swrm_reg_dump",
2357 &swrm_debug_ops);
2358 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302359
2360 ret = device_init_wakeup(swrm->dev, true);
2361 if (ret) {
2362 dev_err(swrm->dev, "Device wakeup init failed: %d\n", ret);
2363 goto err_irq_wakeup_fail;
2364 }
2365
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302366 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
2367 pm_runtime_use_autosuspend(&pdev->dev);
2368 pm_runtime_set_active(&pdev->dev);
2369 pm_runtime_enable(&pdev->dev);
2370 pm_runtime_mark_last_busy(&pdev->dev);
2371
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302372 INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn);
2373 swrm->event_notifier.notifier_call = swrm_event_notify;
2374 msm_aud_evt_register_client(&swrm->event_notifier);
2375
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302376 return 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302377err_irq_wakeup_fail:
2378 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302379err_mstr_fail:
2380 if (swrm->reg_irq)
2381 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2382 swrm, SWR_IRQ_FREE);
2383 else if (swrm->irq)
2384 free_irq(swrm->irq, swrm);
2385err_irq_fail:
2386 mutex_destroy(&swrm->mlock);
2387 mutex_destroy(&swrm->reslock);
2388 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302389 mutex_destroy(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302390 mutex_destroy(&swrm->clklock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302391 mutex_destroy(&swrm->pm_lock);
2392 pm_qos_remove_request(&swrm->pm_qos_req);
2393
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302394err_pdata_fail:
2395err_memory_fail:
2396 return ret;
2397}
2398
2399static int swrm_remove(struct platform_device *pdev)
2400{
2401 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2402
2403 if (swrm->reg_irq)
2404 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2405 swrm, SWR_IRQ_FREE);
2406 else if (swrm->irq)
2407 free_irq(swrm->irq, swrm);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302408 else if (swrm->wake_irq > 0)
2409 free_irq(swrm->wake_irq, swrm);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302410 if (swrm->swr_irq_wakeup_capable)
2411 irq_set_irq_wake(swrm->irq, 0);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302412 cancel_work_sync(&swrm->wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302413 pm_runtime_disable(&pdev->dev);
2414 pm_runtime_set_suspended(&pdev->dev);
2415 swr_unregister_master(&swrm->master);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302416 msm_aud_evt_unregister_client(&swrm->event_notifier);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302417 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302418 mutex_destroy(&swrm->mlock);
2419 mutex_destroy(&swrm->reslock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302420 mutex_destroy(&swrm->iolock);
2421 mutex_destroy(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302422 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302423 mutex_destroy(&swrm->pm_lock);
2424 pm_qos_remove_request(&swrm->pm_qos_req);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302425 devm_kfree(&pdev->dev, swrm);
2426 return 0;
2427}
2428
2429static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
2430{
2431 u32 val;
2432
2433 dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
2434 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD);
2435 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2436 val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK;
2437 swr_master_write(swrm, SWRM_MCP_CFG_ADDR, val);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302438
2439 return 0;
2440}
2441
2442#ifdef CONFIG_PM
2443static int swrm_runtime_resume(struct device *dev)
2444{
2445 struct platform_device *pdev = to_platform_device(dev);
2446 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2447 int ret = 0;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302448 bool hw_core_err = false;
2449 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302450 struct swr_master *mstr = &swrm->master;
2451 struct swr_device *swr_dev;
2452
2453 dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
2454 __func__, swrm->state);
2455 mutex_lock(&swrm->reslock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302456
Sudheer Papothi384addd2019-06-14 02:26:52 +05302457 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2458 dev_err(dev, "%s:lpass core hw enable failed\n",
2459 __func__);
2460 hw_core_err = true;
2461 }
2462 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2463 dev_err(dev, "%s:lpass audio hw enable failed\n",
2464 __func__);
2465 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002466 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302467
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302468 if ((swrm->state == SWR_MSTR_DOWN) ||
2469 (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) {
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302470 if (swrm->clk_stop_mode0_supp) {
2471 if (swrm->ipc_wakeup)
2472 msm_aud_evt_blocking_notifier_call_chain(
2473 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302474 }
2475
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302476 if (swrm_clk_request(swrm, true)) {
2477 /*
2478 * Set autosuspend timer to 1 for
2479 * master to enter into suspend.
2480 */
2481 auto_suspend_timer = 1;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302482 goto exit;
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302483 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302484 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302485 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2486 ret = swr_device_up(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302487 if (ret == -ENODEV) {
2488 dev_dbg(dev,
2489 "%s slave device up not implemented\n",
2490 __func__);
2491 ret = 0;
2492 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302493 dev_err(dev,
2494 "%s: failed to wakeup swr dev %d\n",
2495 __func__, swr_dev->dev_num);
2496 swrm_clk_request(swrm, false);
2497 goto exit;
2498 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302499 }
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05302500 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2501 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2502 swrm_master_init(swrm);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302503 /* wait for hw enumeration to complete */
2504 usleep_range(100, 105);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302505 swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, 0x0,
2506 SWRS_SCP_INT_STATUS_MASK_1);
Karthikeyan Manif6821902019-05-21 17:31:24 -07002507 if (swrm->state == SWR_MSTR_SSR) {
2508 mutex_unlock(&swrm->reslock);
2509 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
2510 mutex_lock(&swrm->reslock);
2511 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302512 } else {
2513 /*wake up from clock stop*/
2514 swr_master_write(swrm, SWRM_MCP_BUS_CTRL_ADDR, 0x2);
2515 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302516 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302517 swrm->state = SWR_MSTR_UP;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302518 }
2519exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302520 if (!aud_core_err)
2521 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2522 if (!hw_core_err)
2523 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302524 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302525 auto_suspend_timer = SWR_AUTO_SUSPEND_DELAY * 1000;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302526 mutex_unlock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302527
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302528 return ret;
2529}
2530
2531static int swrm_runtime_suspend(struct device *dev)
2532{
2533 struct platform_device *pdev = to_platform_device(dev);
2534 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2535 int ret = 0;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302536 bool hw_core_err = false;
2537 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302538 struct swr_master *mstr = &swrm->master;
2539 struct swr_device *swr_dev;
2540 int current_state = 0;
2541
2542 dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
2543 __func__, swrm->state);
2544 mutex_lock(&swrm->reslock);
2545 mutex_lock(&swrm->force_down_lock);
2546 current_state = swrm->state;
2547 mutex_unlock(&swrm->force_down_lock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302548
2549 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2550 dev_err(dev, "%s:lpass core hw enable failed\n",
2551 __func__);
2552 hw_core_err = true;
2553 }
2554 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2555 dev_err(dev, "%s:lpass audio hw enable failed\n",
2556 __func__);
2557 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002558 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302559
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302560 if ((current_state == SWR_MSTR_UP) ||
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302561 (current_state == SWR_MSTR_SSR)) {
2562
2563 if ((current_state != SWR_MSTR_SSR) &&
2564 swrm_is_port_en(&swrm->master)) {
2565 dev_dbg(dev, "%s ports are enabled\n", __func__);
2566 ret = -EBUSY;
2567 goto exit;
2568 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302569 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05302570 mutex_unlock(&swrm->reslock);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +05302571 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
Sudheer Papothi06f43412019-07-09 03:32:54 +05302572 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302573 swrm_clk_pause(swrm);
2574 swr_master_write(swrm, SWRM_COMP_CFG_ADDR, 0x00);
2575 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2576 ret = swr_device_down(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302577 if (ret == -ENODEV) {
2578 dev_dbg_ratelimited(dev,
2579 "%s slave device down not implemented\n",
2580 __func__);
2581 ret = 0;
2582 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302583 dev_err(dev,
2584 "%s: failed to shutdown swr dev %d\n",
2585 __func__, swr_dev->dev_num);
2586 goto exit;
2587 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302588 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302589 } else {
Sudheer Papothi384addd2019-06-14 02:26:52 +05302590 mutex_unlock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302591 /* clock stop sequence */
2592 swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
2593 SWRS_SCP_CONTROL);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302594 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302595 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302596 }
Karthikeyan Mani1d750fe2019-09-06 14:36:09 -07002597 ret = swrm_clk_request(swrm, false);
2598 if (ret) {
2599 dev_err(dev, "%s: swrmn clk failed\n", __func__);
2600 ret = 0;
2601 goto exit;
2602 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302603
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302604 if (swrm->clk_stop_mode0_supp) {
2605 if (swrm->wake_irq > 0) {
2606 enable_irq(swrm->wake_irq);
2607 } else if (swrm->ipc_wakeup) {
2608 msm_aud_evt_blocking_notifier_call_chain(
2609 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
2610 swrm->ipc_wakeup_triggered = false;
2611 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302612 }
2613
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302614 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302615 /* Retain SSR state until resume */
2616 if (current_state != SWR_MSTR_SSR)
2617 swrm->state = SWR_MSTR_DOWN;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302618exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302619 if (!aud_core_err)
2620 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2621 if (!hw_core_err)
2622 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302623 mutex_unlock(&swrm->reslock);
2624 return ret;
2625}
2626#endif /* CONFIG_PM */
2627
Sudheer Papothi06f43412019-07-09 03:32:54 +05302628static int swrm_device_suspend(struct device *dev)
2629{
2630 struct platform_device *pdev = to_platform_device(dev);
2631 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2632 int ret = 0;
2633
2634 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2635 if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
2636 ret = swrm_runtime_suspend(dev);
2637 if (!ret) {
2638 pm_runtime_disable(dev);
2639 pm_runtime_set_suspended(dev);
2640 pm_runtime_enable(dev);
2641 }
2642 }
2643
2644 return 0;
2645}
2646
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302647static int swrm_device_down(struct device *dev)
2648{
2649 struct platform_device *pdev = to_platform_device(dev);
2650 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302651
2652 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2653
2654 mutex_lock(&swrm->force_down_lock);
2655 swrm->state = SWR_MSTR_SSR;
2656 mutex_unlock(&swrm->force_down_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302657
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302658 swrm_device_suspend(dev);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302659 return 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302660}
2661
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302662int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm)
2663{
2664 int ret = 0;
Laxminath Kasama60239e2019-01-10 14:43:03 +05302665 int irq, dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302666
2667 if (!swrm->ipc_wakeup) {
Laxminath Kasama60239e2019-01-10 14:43:03 +05302668 irq = of_get_named_gpio(swrm->dev->of_node,
2669 "qcom,swr-wakeup-irq", 0);
2670 if (gpio_is_valid(irq)) {
2671 swrm->wake_irq = gpio_to_irq(irq);
2672 if (swrm->wake_irq < 0) {
2673 dev_err(swrm->dev,
2674 "Unable to configure irq\n");
2675 return swrm->wake_irq;
2676 }
2677 } else {
2678 dir_apps_irq = platform_get_irq_byname(swrm->pdev,
2679 "swr_wake_irq");
2680 if (dir_apps_irq < 0) {
2681 dev_err(swrm->dev,
2682 "TLMM connect gpio not found\n");
2683 return -EINVAL;
2684 }
2685 swrm->wake_irq = dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302686 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302687 ret = request_threaded_irq(swrm->wake_irq, NULL,
2688 swrm_wakeup_interrupt,
2689 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
2690 "swr_wake_irq", swrm);
2691 if (ret) {
2692 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2693 __func__, ret);
2694 return -EINVAL;
2695 }
Aditya Bavanari3517b112018-12-03 13:26:59 +05302696 irq_set_irq_wake(swrm->wake_irq, 1);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302697 }
2698 return ret;
2699}
2700
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302701static int swrm_alloc_port_mem(struct device *dev, struct swr_mstr_ctrl *swrm,
2702 u32 uc, u32 size)
2703{
2704 if (!swrm->port_param) {
2705 swrm->port_param = devm_kzalloc(dev,
2706 sizeof(swrm->port_param) * SWR_UC_MAX,
2707 GFP_KERNEL);
2708 if (!swrm->port_param)
2709 return -ENOMEM;
2710 }
2711 if (!swrm->port_param[uc]) {
2712 swrm->port_param[uc] = devm_kcalloc(dev, size,
2713 sizeof(struct port_params),
2714 GFP_KERNEL);
2715 if (!swrm->port_param[uc])
2716 return -ENOMEM;
2717 } else {
2718 dev_err_ratelimited(swrm->dev, "%s: called more than once\n",
2719 __func__);
2720 }
2721
2722 return 0;
2723}
2724
2725static int swrm_copy_port_config(struct swr_mstr_ctrl *swrm,
2726 struct swrm_port_config *port_cfg,
2727 u32 size)
2728{
2729 int idx;
2730 struct port_params *params;
2731 int uc = port_cfg->uc;
2732 int ret = 0;
2733
2734 for (idx = 0; idx < size; idx++) {
2735 params = &((struct port_params *)port_cfg->params)[idx];
2736 if (!params) {
2737 dev_err(swrm->dev, "%s: Invalid params\n", __func__);
2738 ret = -EINVAL;
2739 break;
2740 }
2741 memcpy(&swrm->port_param[uc][idx], params,
2742 sizeof(struct port_params));
2743 }
2744
2745 return ret;
2746}
2747
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302748/**
2749 * swrm_wcd_notify - parent device can notify to soundwire master through
2750 * this function
2751 * @pdev: pointer to platform device structure
2752 * @id: command id from parent to the soundwire master
2753 * @data: data from parent device to soundwire master
2754 */
2755int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
2756{
2757 struct swr_mstr_ctrl *swrm;
2758 int ret = 0;
2759 struct swr_master *mstr;
2760 struct swr_device *swr_dev;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302761 struct swrm_port_config *port_cfg;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302762
2763 if (!pdev) {
2764 pr_err("%s: pdev is NULL\n", __func__);
2765 return -EINVAL;
2766 }
2767 swrm = platform_get_drvdata(pdev);
2768 if (!swrm) {
2769 dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__);
2770 return -EINVAL;
2771 }
2772 mstr = &swrm->master;
2773
2774 switch (id) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05302775 case SWR_REQ_CLK_SWITCH:
2776 /* This will put soundwire in clock stop mode and disable the
2777 * clocks, if there is no active usecase running, so that the
2778 * next activity on soundwire will request clock from new clock
2779 * source.
2780 */
2781 mutex_lock(&swrm->mlock);
2782 if (swrm->state == SWR_MSTR_UP)
2783 swrm_device_suspend(&pdev->dev);
2784 mutex_unlock(&swrm->mlock);
2785 break;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302786 case SWR_CLK_FREQ:
2787 if (!data) {
2788 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
2789 ret = -EINVAL;
2790 } else {
2791 mutex_lock(&swrm->mlock);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302792 if (swrm->mclk_freq != *(int *)data) {
2793 dev_dbg(swrm->dev, "%s: freq change: force mstr down\n", __func__);
2794 if (swrm->state == SWR_MSTR_DOWN)
2795 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
2796 __func__, swrm->state);
2797 else
2798 swrm_device_suspend(&pdev->dev);
2799 }
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302800 swrm->mclk_freq = *(int *)data;
2801 mutex_unlock(&swrm->mlock);
2802 }
2803 break;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302804 case SWR_DEVICE_SSR_DOWN:
2805 mutex_lock(&swrm->devlock);
2806 swrm->dev_up = false;
2807 mutex_unlock(&swrm->devlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302808 mutex_lock(&swrm->reslock);
2809 swrm->state = SWR_MSTR_SSR;
2810 mutex_unlock(&swrm->reslock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302811 break;
2812 case SWR_DEVICE_SSR_UP:
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302813 /* wait for clk voting to be zero */
Ramprasad Katkam7f6462e2018-11-06 11:51:22 +05302814 reinit_completion(&swrm->clk_off_complete);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302815 if (swrm->clk_ref_count &&
2816 !wait_for_completion_timeout(&swrm->clk_off_complete,
Ramprasad Katkamc87efeb2018-12-12 19:26:19 +05302817 msecs_to_jiffies(500)))
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302818 dev_err(swrm->dev, "%s: clock voting not zero\n",
2819 __func__);
2820
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302821 mutex_lock(&swrm->devlock);
2822 swrm->dev_up = true;
2823 mutex_unlock(&swrm->devlock);
2824 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302825 case SWR_DEVICE_DOWN:
2826 dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
2827 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302828 if (swrm->state == SWR_MSTR_DOWN)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302829 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
2830 __func__, swrm->state);
2831 else
2832 swrm_device_down(&pdev->dev);
2833 mutex_unlock(&swrm->mlock);
2834 break;
2835 case SWR_DEVICE_UP:
2836 dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
Ramprasad Katkam0fed92f2018-11-08 14:22:22 +05302837 mutex_lock(&swrm->devlock);
2838 if (!swrm->dev_up) {
2839 dev_dbg(swrm->dev, "SSR not complete yet\n");
2840 mutex_unlock(&swrm->devlock);
2841 return -EBUSY;
2842 }
2843 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302844 mutex_lock(&swrm->mlock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302845 pm_runtime_mark_last_busy(&pdev->dev);
2846 pm_runtime_get_sync(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302847 mutex_lock(&swrm->reslock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302848 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2849 ret = swr_reset_device(swr_dev);
2850 if (ret) {
2851 dev_err(swrm->dev,
2852 "%s: failed to reset swr device %d\n",
2853 __func__, swr_dev->dev_num);
2854 swrm_clk_request(swrm, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302855 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302856 }
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302857 pm_runtime_mark_last_busy(&pdev->dev);
2858 pm_runtime_put_autosuspend(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302859 mutex_unlock(&swrm->reslock);
2860 mutex_unlock(&swrm->mlock);
2861 break;
2862 case SWR_SET_NUM_RX_CH:
2863 if (!data) {
2864 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
2865 ret = -EINVAL;
2866 } else {
2867 mutex_lock(&swrm->mlock);
2868 swrm->num_rx_chs = *(int *)data;
2869 if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
2870 list_for_each_entry(swr_dev, &mstr->devices,
2871 dev_list) {
2872 ret = swr_set_device_group(swr_dev,
2873 SWR_BROADCAST);
2874 if (ret)
2875 dev_err(swrm->dev,
2876 "%s: set num ch failed\n",
2877 __func__);
2878 }
2879 } else {
2880 list_for_each_entry(swr_dev, &mstr->devices,
2881 dev_list) {
2882 ret = swr_set_device_group(swr_dev,
2883 SWR_GROUP_NONE);
2884 if (ret)
2885 dev_err(swrm->dev,
2886 "%s: set num ch failed\n",
2887 __func__);
2888 }
2889 }
2890 mutex_unlock(&swrm->mlock);
2891 }
2892 break;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302893 case SWR_REGISTER_WAKE_IRQ:
2894 if (!data) {
2895 dev_err(swrm->dev, "%s: reg wake irq data is NULL\n",
2896 __func__);
2897 ret = -EINVAL;
2898 } else {
2899 mutex_lock(&swrm->mlock);
2900 swrm->ipc_wakeup = *(u32 *)data;
2901 ret = swrm_register_wake_irq(swrm);
2902 if (ret)
2903 dev_err(swrm->dev, "%s: register wake_irq failed\n",
2904 __func__);
2905 mutex_unlock(&swrm->mlock);
2906 }
2907 break;
Sudheer Papothi72ee2642019-08-08 05:15:17 +05302908 case SWR_REGISTER_WAKEUP:
2909 msm_aud_evt_blocking_notifier_call_chain(
2910 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
2911 break;
2912 case SWR_DEREGISTER_WAKEUP:
2913 msm_aud_evt_blocking_notifier_call_chain(
2914 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
2915 break;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302916 case SWR_SET_PORT_MAP:
2917 if (!data) {
2918 dev_err(swrm->dev, "%s: data is NULL for id=%d\n",
2919 __func__, id);
2920 ret = -EINVAL;
2921 } else {
2922 mutex_lock(&swrm->mlock);
2923 port_cfg = (struct swrm_port_config *)data;
2924 if (!port_cfg->size) {
2925 ret = -EINVAL;
2926 goto done;
2927 }
2928 ret = swrm_alloc_port_mem(&pdev->dev, swrm,
2929 port_cfg->uc, port_cfg->size);
2930 if (!ret)
2931 swrm_copy_port_config(swrm, port_cfg,
2932 port_cfg->size);
2933done:
2934 mutex_unlock(&swrm->mlock);
2935 }
2936 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302937 default:
2938 dev_err(swrm->dev, "%s: swr master unknown id %d\n",
2939 __func__, id);
2940 break;
2941 }
2942 return ret;
2943}
2944EXPORT_SYMBOL(swrm_wcd_notify);
2945
Ramprasad Katkam57349872018-11-11 18:34:57 +05302946/*
2947 * swrm_pm_cmpxchg:
2948 * Check old state and exchange with pm new state
2949 * if old state matches with current state
2950 *
2951 * @swrm: pointer to wcd core resource
2952 * @o: pm old state
2953 * @n: pm new state
2954 *
2955 * Returns old state
2956 */
2957static enum swrm_pm_state swrm_pm_cmpxchg(
2958 struct swr_mstr_ctrl *swrm,
2959 enum swrm_pm_state o,
2960 enum swrm_pm_state n)
2961{
2962 enum swrm_pm_state old;
2963
2964 if (!swrm)
2965 return o;
2966
2967 mutex_lock(&swrm->pm_lock);
2968 old = swrm->pm_state;
2969 if (old == o)
2970 swrm->pm_state = n;
2971 mutex_unlock(&swrm->pm_lock);
2972
2973 return old;
2974}
2975
2976static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm)
2977{
2978 enum swrm_pm_state os;
2979
2980 /*
2981 * swrm_{lock/unlock}_sleep will be called by swr irq handler
2982 * and slave wake up requests..
2983 *
2984 * If system didn't resume, we can simply return false so
2985 * IRQ handler can return without handling IRQ.
2986 */
2987 mutex_lock(&swrm->pm_lock);
2988 if (swrm->wlock_holders++ == 0) {
2989 dev_dbg(swrm->dev, "%s: holding wake lock\n", __func__);
2990 pm_qos_update_request(&swrm->pm_qos_req,
2991 msm_cpuidle_get_deep_idle_latency());
2992 pm_stay_awake(swrm->dev);
2993 }
2994 mutex_unlock(&swrm->pm_lock);
2995
2996 if (!wait_event_timeout(swrm->pm_wq,
2997 ((os = swrm_pm_cmpxchg(swrm,
2998 SWRM_PM_SLEEPABLE,
2999 SWRM_PM_AWAKE)) ==
3000 SWRM_PM_SLEEPABLE ||
3001 (os == SWRM_PM_AWAKE)),
3002 msecs_to_jiffies(
3003 SWRM_SYSTEM_RESUME_TIMEOUT_MS))) {
3004 dev_err(swrm->dev, "%s: system didn't resume within %dms, s %d, w %d\n",
3005 __func__, SWRM_SYSTEM_RESUME_TIMEOUT_MS, swrm->pm_state,
3006 swrm->wlock_holders);
3007 swrm_unlock_sleep(swrm);
3008 return false;
3009 }
3010 wake_up_all(&swrm->pm_wq);
3011 return true;
3012}
3013
3014static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm)
3015{
3016 mutex_lock(&swrm->pm_lock);
3017 if (--swrm->wlock_holders == 0) {
3018 dev_dbg(swrm->dev, "%s: releasing wake lock pm_state %d -> %d\n",
3019 __func__, swrm->pm_state, SWRM_PM_SLEEPABLE);
3020 /*
3021 * if swrm_lock_sleep failed, pm_state would be still
3022 * swrm_PM_ASLEEP, don't overwrite
3023 */
3024 if (likely(swrm->pm_state == SWRM_PM_AWAKE))
3025 swrm->pm_state = SWRM_PM_SLEEPABLE;
3026 pm_qos_update_request(&swrm->pm_qos_req,
3027 PM_QOS_DEFAULT_VALUE);
3028 pm_relax(swrm->dev);
3029 }
3030 mutex_unlock(&swrm->pm_lock);
3031 wake_up_all(&swrm->pm_wq);
3032}
3033
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303034#ifdef CONFIG_PM_SLEEP
3035static int swrm_suspend(struct device *dev)
3036{
3037 int ret = -EBUSY;
3038 struct platform_device *pdev = to_platform_device(dev);
3039 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3040
3041 dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
Ramprasad Katkam57349872018-11-11 18:34:57 +05303042
3043 mutex_lock(&swrm->pm_lock);
3044
3045 if (swrm->pm_state == SWRM_PM_SLEEPABLE) {
3046 dev_dbg(swrm->dev, "%s: suspending system, state %d, wlock %d\n",
3047 __func__, swrm->pm_state,
3048 swrm->wlock_holders);
3049 swrm->pm_state = SWRM_PM_ASLEEP;
3050 } else if (swrm->pm_state == SWRM_PM_AWAKE) {
3051 /*
3052 * unlock to wait for pm_state == SWRM_PM_SLEEPABLE
3053 * then set to SWRM_PM_ASLEEP
3054 */
3055 dev_dbg(swrm->dev, "%s: waiting to suspend system, state %d, wlock %d\n",
3056 __func__, swrm->pm_state,
3057 swrm->wlock_holders);
3058 mutex_unlock(&swrm->pm_lock);
3059 if (!(wait_event_timeout(swrm->pm_wq, swrm_pm_cmpxchg(
3060 swrm, SWRM_PM_SLEEPABLE,
3061 SWRM_PM_ASLEEP) ==
3062 SWRM_PM_SLEEPABLE,
3063 msecs_to_jiffies(
3064 SWRM_SYS_SUSPEND_WAIT)))) {
3065 dev_dbg(swrm->dev, "%s: suspend failed state %d, wlock %d\n",
3066 __func__, swrm->pm_state,
3067 swrm->wlock_holders);
3068 return -EBUSY;
3069 } else {
3070 dev_dbg(swrm->dev,
3071 "%s: done, state %d, wlock %d\n",
3072 __func__, swrm->pm_state,
3073 swrm->wlock_holders);
3074 }
3075 mutex_lock(&swrm->pm_lock);
3076 } else if (swrm->pm_state == SWRM_PM_ASLEEP) {
3077 dev_dbg(swrm->dev, "%s: system is already suspended, state %d, wlock %d\n",
3078 __func__, swrm->pm_state,
3079 swrm->wlock_holders);
3080 }
3081
3082 mutex_unlock(&swrm->pm_lock);
3083
3084 if ((!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev))) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303085 ret = swrm_runtime_suspend(dev);
3086 if (!ret) {
3087 /*
3088 * Synchronize runtime-pm and system-pm states:
3089 * At this point, we are already suspended. If
3090 * runtime-pm still thinks its active, then
3091 * make sure its status is in sync with HW
3092 * status. The three below calls let the
3093 * runtime-pm know that we are suspended
3094 * already without re-invoking the suspend
3095 * callback
3096 */
3097 pm_runtime_disable(dev);
3098 pm_runtime_set_suspended(dev);
3099 pm_runtime_enable(dev);
3100 }
3101 }
3102 if (ret == -EBUSY) {
3103 /*
3104 * There is a possibility that some audio stream is active
3105 * during suspend. We dont want to return suspend failure in
3106 * that case so that display and relevant components can still
3107 * go to suspend.
3108 * If there is some other error, then it should be passed-on
3109 * to system level suspend
3110 */
3111 ret = 0;
3112 }
3113 return ret;
3114}
3115
3116static int swrm_resume(struct device *dev)
3117{
3118 int ret = 0;
3119 struct platform_device *pdev = to_platform_device(dev);
3120 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3121
3122 dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
3123 if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
3124 ret = swrm_runtime_resume(dev);
3125 if (!ret) {
3126 pm_runtime_mark_last_busy(dev);
3127 pm_request_autosuspend(dev);
3128 }
3129 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05303130 mutex_lock(&swrm->pm_lock);
3131 if (swrm->pm_state == SWRM_PM_ASLEEP) {
3132 dev_dbg(swrm->dev,
3133 "%s: resuming system, state %d, wlock %d\n",
3134 __func__, swrm->pm_state,
3135 swrm->wlock_holders);
3136 swrm->pm_state = SWRM_PM_SLEEPABLE;
3137 } else {
3138 dev_dbg(swrm->dev, "%s: system is already awake, state %d wlock %d\n",
3139 __func__, swrm->pm_state,
3140 swrm->wlock_holders);
3141 }
3142 mutex_unlock(&swrm->pm_lock);
3143 wake_up_all(&swrm->pm_wq);
3144
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303145 return ret;
3146}
3147#endif /* CONFIG_PM_SLEEP */
3148
3149static const struct dev_pm_ops swrm_dev_pm_ops = {
3150 SET_SYSTEM_SLEEP_PM_OPS(
3151 swrm_suspend,
3152 swrm_resume
3153 )
3154 SET_RUNTIME_PM_OPS(
3155 swrm_runtime_suspend,
3156 swrm_runtime_resume,
3157 NULL
3158 )
3159};
3160
3161static const struct of_device_id swrm_dt_match[] = {
3162 {
3163 .compatible = "qcom,swr-mstr",
3164 },
3165 {}
3166};
3167
3168static struct platform_driver swr_mstr_driver = {
3169 .probe = swrm_probe,
3170 .remove = swrm_remove,
3171 .driver = {
3172 .name = SWR_WCD_NAME,
3173 .owner = THIS_MODULE,
3174 .pm = &swrm_dev_pm_ops,
3175 .of_match_table = swrm_dt_match,
Xiaojun Sang53cd13a2018-06-29 15:14:37 +08003176 .suppress_bind_attrs = true,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303177 },
3178};
3179
3180static int __init swrm_init(void)
3181{
3182 return platform_driver_register(&swr_mstr_driver);
3183}
3184module_init(swrm_init);
3185
3186static void __exit swrm_exit(void)
3187{
3188 platform_driver_unregister(&swr_mstr_driver);
3189}
3190module_exit(swrm_exit);
3191
3192MODULE_LICENSE("GPL v2");
3193MODULE_DESCRIPTION("SoundWire Master Controller");
3194MODULE_ALIAS("platform:swr-mstr");