blob: 9ad849e866d053bbe623937575f0b30685cc4e9e [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 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530304 swrm->clk_ref_count++;
305 if (swrm->clk_ref_count == 1) {
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530306 ret = swrm->clk(swrm->handle, true);
307 if (ret) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +0530308 dev_err_ratelimited(swrm->dev,
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530309 "%s: clock enable req failed",
310 __func__);
311 --swrm->clk_ref_count;
312 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530313 }
314 } else if (--swrm->clk_ref_count == 0) {
315 swrm->clk(swrm->handle, false);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530316 complete(&swrm->clk_off_complete);
317 }
318 if (swrm->clk_ref_count < 0) {
Meng Wang8c60bb52019-06-19 15:49:06 +0800319 dev_err(swrm->dev, "%s: swrm clk count mismatch\n", __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530320 swrm->clk_ref_count = 0;
321 }
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530322
323exit:
324 mutex_unlock(&swrm->clklock);
325 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530326}
327
328static int swrm_ahb_write(struct swr_mstr_ctrl *swrm,
329 u16 reg, u32 *value)
330{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530331 u32 temp = (u32)(*value);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530332 int ret = 0;
333
334 mutex_lock(&swrm->devlock);
335 if (!swrm->dev_up)
336 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530337
338 ret = swrm_clk_request(swrm, TRUE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530339 if (ret) {
340 dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
341 __func__);
342 goto err;
343 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530344 iowrite32(temp, swrm->swrm_dig_base + reg);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530345 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530346err:
347 mutex_unlock(&swrm->devlock);
348 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530349}
350
351static int swrm_ahb_read(struct swr_mstr_ctrl *swrm,
352 u16 reg, u32 *value)
353{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530354 u32 temp = 0;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530355 int ret = 0;
356
357 mutex_lock(&swrm->devlock);
358 if (!swrm->dev_up)
359 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530360
361 ret = swrm_clk_request(swrm, TRUE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530362 if (ret) {
363 dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
364 __func__);
365 goto err;
366 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530367 temp = ioread32(swrm->swrm_dig_base + reg);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530368 *value = temp;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530369 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530370err:
371 mutex_unlock(&swrm->devlock);
372 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530373}
374
375static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr)
376{
377 u32 val = 0;
378
379 if (swrm->read)
380 val = swrm->read(swrm->handle, reg_addr);
381 else
382 swrm_ahb_read(swrm, reg_addr, &val);
383 return val;
384}
385
386static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val)
387{
388 if (swrm->write)
389 swrm->write(swrm->handle, reg_addr, val);
390 else
391 swrm_ahb_write(swrm, reg_addr, &val);
392}
393
394static int swr_master_bulk_write(struct swr_mstr_ctrl *swrm, u32 *reg_addr,
395 u32 *val, unsigned int length)
396{
397 int i = 0;
398
399 if (swrm->bulk_write)
400 swrm->bulk_write(swrm->handle, reg_addr, val, length);
401 else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530402 mutex_lock(&swrm->iolock);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530403 for (i = 0; i < length; i++) {
404 /* wait for FIFO WR command to complete to avoid overflow */
Karthikeyan Mani5d52dd82019-08-15 16:58:08 -0700405 /*
406 * Reduce sleep from 100us to 10us to meet KPIs
407 * This still meets the hardware spec
408 */
409 usleep_range(10, 12);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530410 swr_master_write(swrm, reg_addr[i], val[i]);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530411 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530412 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530413 }
414 return 0;
415}
416
417static bool swrm_is_port_en(struct swr_master *mstr)
418{
419 return !!(mstr->num_port);
420}
421
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530422static void copy_port_tables(struct swr_mstr_ctrl *swrm,
423 struct port_params *params)
424{
425 u8 i;
426 struct port_params *config = params;
427
428 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
429 /* wsa uses single frame structure for all configurations */
430 if (!swrm->mport_cfg[i].port_en)
431 continue;
432 swrm->mport_cfg[i].sinterval = config[i].si;
433 swrm->mport_cfg[i].offset1 = config[i].off1;
434 swrm->mport_cfg[i].offset2 = config[i].off2;
435 swrm->mport_cfg[i].hstart = config[i].hstart;
436 swrm->mport_cfg[i].hstop = config[i].hstop;
437 swrm->mport_cfg[i].blk_pack_mode = config[i].bp_mode;
438 swrm->mport_cfg[i].blk_grp_count = config[i].bgp_ctrl;
439 swrm->mport_cfg[i].word_length = config[i].wd_len;
440 swrm->mport_cfg[i].lane_ctrl = config[i].lane_ctrl;
441 }
442}
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530443static int swrm_get_port_config(struct swr_mstr_ctrl *swrm)
444{
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530445 struct port_params *params;
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530446 u32 usecase = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530447
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530448 /* TODO - Send usecase information to avoid checking for master_id */
449 if (swrm->mport_cfg[SWRM_DSD_PARAMS_PORT].port_en &&
450 (swrm->master_id == MASTER_ID_RX))
451 usecase = 1;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530452
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530453 params = swrm->port_param[usecase];
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530454 copy_port_tables(swrm, params);
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530455
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530456 return 0;
457}
458
459static int swrm_get_master_port(struct swr_mstr_ctrl *swrm, u8 *mstr_port_id,
460 u8 *mstr_ch_mask, u8 mstr_prt_type,
461 u8 slv_port_id)
462{
463 int i, j;
464 *mstr_port_id = 0;
465
466 for (i = 1; i <= swrm->num_ports; i++) {
467 for (j = 0; j < SWR_MAX_CH_PER_PORT; j++) {
468 if (swrm->port_mapping[i][j].port_type == mstr_prt_type)
469 goto found;
470 }
471 }
472found:
473 if (i > swrm->num_ports || j == SWR_MAX_CH_PER_PORT) {
474 dev_err(swrm->dev, "%s: port type not supported by master\n",
475 __func__);
476 return -EINVAL;
477 }
478 /* id 0 corresponds to master port 1 */
479 *mstr_port_id = i - 1;
480 *mstr_ch_mask = swrm->port_mapping[i][j].ch_mask;
481
482 return 0;
483
484}
485
486static u32 swrm_get_packed_reg_val(u8 *cmd_id, u8 cmd_data,
487 u8 dev_addr, u16 reg_addr)
488{
489 u32 val;
490 u8 id = *cmd_id;
491
492 if (id != SWR_BROADCAST_CMD_ID) {
493 if (id < 14)
494 id += 1;
495 else
496 id = 0;
497 *cmd_id = id;
498 }
499 val = SWR_REG_VAL_PACK(cmd_data, dev_addr, id, reg_addr);
500
501 return val;
502}
503
504static int swrm_cmd_fifo_rd_cmd(struct swr_mstr_ctrl *swrm, int *cmd_data,
505 u8 dev_addr, u8 cmd_id, u16 reg_addr,
506 u32 len)
507{
508 u32 val;
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530509 u32 retry_attempt = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530510
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530511 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530512 val = swrm_get_packed_reg_val(&swrm->rcmd_id, len, dev_addr, reg_addr);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530513 if (swrm->read) {
514 /* skip delay if read is handled in platform driver */
515 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
516 } else {
517 /* wait for FIFO RD to complete to avoid overflow */
518 usleep_range(100, 105);
519 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
520 /* wait for FIFO RD CMD complete to avoid overflow */
521 usleep_range(250, 255);
522 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530523retry_read:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530524 *cmd_data = swr_master_read(swrm, SWRM_CMD_FIFO_RD_FIFO_ADDR);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530525 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, rcmd_id: 0x%x, \
526 dev_num: 0x%x, cmd_data: 0x%x\n", __func__, reg_addr,
527 cmd_id, swrm->rcmd_id, dev_addr, *cmd_data);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530528 if ((((*cmd_data) & 0xF00) >> 8) != swrm->rcmd_id) {
529 if (retry_attempt < MAX_FIFO_RD_FAIL_RETRY) {
530 /* wait 500 us before retry on fifo read failure */
531 usleep_range(500, 505);
532 retry_attempt++;
533 goto retry_read;
534 } else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530535 dev_err_ratelimited(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, \
536 rcmd_id: 0x%x, dev_num: 0x%x, cmd_data: 0x%x\n",
537 __func__, reg_addr, cmd_id, swrm->rcmd_id,
538 dev_addr, *cmd_data);
539
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530540 dev_err_ratelimited(swrm->dev,
541 "%s: failed to read fifo\n", __func__);
542 }
543 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530544 mutex_unlock(&swrm->iolock);
545
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530546 return 0;
547}
548
549static int swrm_cmd_fifo_wr_cmd(struct swr_mstr_ctrl *swrm, u8 cmd_data,
550 u8 dev_addr, u8 cmd_id, u16 reg_addr)
551{
552 u32 val;
553 int ret = 0;
554
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530555 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530556 if (!cmd_id)
557 val = swrm_get_packed_reg_val(&swrm->wcmd_id, cmd_data,
558 dev_addr, reg_addr);
559 else
560 val = swrm_get_packed_reg_val(&cmd_id, cmd_data,
561 dev_addr, reg_addr);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530562 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x,wcmd_id: 0x%x, \
563 dev_num: 0x%x, cmd_data: 0x%x\n", __func__,
564 reg_addr, cmd_id, swrm->wcmd_id,dev_addr, cmd_data);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +0530565 swr_master_write(swrm, SWRM_CMD_FIFO_WR_CMD, val);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530566 /*
567 * wait for FIFO WR command to complete to avoid overflow
568 * skip delay if write is handled in platform driver.
569 */
570 if(!swrm->write)
Karthikeyan Mani5d52dd82019-08-15 16:58:08 -0700571 usleep_range(150, 155);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530572 if (cmd_id == 0xF) {
573 /*
574 * sleep for 10ms for MSM soundwire variant to allow broadcast
575 * command to complete.
576 */
577 if (swrm_is_msm_variant(swrm->version))
578 usleep_range(10000, 10100);
579 else
580 wait_for_completion_timeout(&swrm->broadcast,
581 (2 * HZ/10));
582 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530583 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530584 return ret;
585}
586
587static int swrm_read(struct swr_master *master, u8 dev_num, u16 reg_addr,
588 void *buf, u32 len)
589{
590 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
591 int ret = 0;
592 int val;
593 u8 *reg_val = (u8 *)buf;
594
595 if (!swrm) {
596 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
597 return -EINVAL;
598 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530599 if (!dev_num) {
600 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
601 return -EINVAL;
602 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530603 mutex_lock(&swrm->devlock);
604 if (!swrm->dev_up) {
605 mutex_unlock(&swrm->devlock);
606 return 0;
607 }
608 mutex_unlock(&swrm->devlock);
609
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530610 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530611 ret = swrm_cmd_fifo_rd_cmd(swrm, &val, dev_num, 0, reg_addr, len);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530612
613 if (!ret)
614 *reg_val = (u8)val;
615
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530616 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530617 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530618 return ret;
619}
620
621static int swrm_write(struct swr_master *master, u8 dev_num, u16 reg_addr,
622 const void *buf)
623{
624 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
625 int ret = 0;
626 u8 reg_val = *(u8 *)buf;
627
628 if (!swrm) {
629 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
630 return -EINVAL;
631 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530632 if (!dev_num) {
633 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
634 return -EINVAL;
635 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530636 mutex_lock(&swrm->devlock);
637 if (!swrm->dev_up) {
638 mutex_unlock(&swrm->devlock);
639 return 0;
640 }
641 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530642
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530643 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530644 ret = swrm_cmd_fifo_wr_cmd(swrm, reg_val, dev_num, 0, reg_addr);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530645
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530646 pm_runtime_put_autosuspend(swrm->dev);
647 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530648 return ret;
649}
650
651static int swrm_bulk_write(struct swr_master *master, u8 dev_num, void *reg,
652 const void *buf, size_t len)
653{
654 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
655 int ret = 0;
656 int i;
657 u32 *val;
658 u32 *swr_fifo_reg;
659
660 if (!swrm || !swrm->handle) {
661 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
662 return -EINVAL;
663 }
664 if (len <= 0)
665 return -EINVAL;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530666 mutex_lock(&swrm->devlock);
667 if (!swrm->dev_up) {
668 mutex_unlock(&swrm->devlock);
669 return 0;
670 }
671 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530672
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530673 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530674 if (dev_num) {
675 swr_fifo_reg = kcalloc(len, sizeof(u32), GFP_KERNEL);
676 if (!swr_fifo_reg) {
677 ret = -ENOMEM;
678 goto err;
679 }
680 val = kcalloc(len, sizeof(u32), GFP_KERNEL);
681 if (!val) {
682 ret = -ENOMEM;
683 goto mem_fail;
684 }
685
686 for (i = 0; i < len; i++) {
687 val[i] = swrm_get_packed_reg_val(&swrm->wcmd_id,
688 ((u8 *)buf)[i],
689 dev_num,
690 ((u16 *)reg)[i]);
691 swr_fifo_reg[i] = SWRM_CMD_FIFO_WR_CMD;
692 }
693 ret = swr_master_bulk_write(swrm, swr_fifo_reg, val, len);
694 if (ret) {
695 dev_err(&master->dev, "%s: bulk write failed\n",
696 __func__);
697 ret = -EINVAL;
698 }
699 } else {
700 dev_err(&master->dev,
701 "%s: No support of Bulk write for master regs\n",
702 __func__);
703 ret = -EINVAL;
704 goto err;
705 }
706 kfree(val);
707mem_fail:
708 kfree(swr_fifo_reg);
709err:
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530710 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530711 pm_runtime_mark_last_busy(swrm->dev);
712 return ret;
713}
714
715static u8 get_inactive_bank_num(struct swr_mstr_ctrl *swrm)
716{
717 return (swr_master_read(swrm, SWRM_MCP_STATUS) &
718 SWRM_MCP_STATUS_BANK_NUM_MASK) ? 0 : 1;
719}
720
721static void enable_bank_switch(struct swr_mstr_ctrl *swrm, u8 bank,
722 u8 row, u8 col)
723{
724 swrm_cmd_fifo_wr_cmd(swrm, ((row << 3) | col), 0xF, 0xF,
725 SWRS_SCP_FRAME_CTRL_BANK(bank));
726}
727
728static struct swr_port_info *swrm_get_port_req(struct swrm_mports *mport,
729 u8 slv_port, u8 dev_num)
730{
731 struct swr_port_info *port_req = NULL;
732
733 list_for_each_entry(port_req, &mport->port_req_list, list) {
734 /* Store dev_id instead of dev_num if enumeration is changed run_time */
735 if ((port_req->slave_port_id == slv_port)
736 && (port_req->dev_num == dev_num))
737 return port_req;
738 }
739 return NULL;
740}
741
742static bool swrm_remove_from_group(struct swr_master *master)
743{
744 struct swr_device *swr_dev;
745 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
746 bool is_removed = false;
747
748 if (!swrm)
749 goto end;
750
751 mutex_lock(&swrm->mlock);
752 if ((swrm->num_rx_chs > 1) &&
753 (swrm->num_rx_chs == swrm->num_cfg_devs)) {
754 list_for_each_entry(swr_dev, &master->devices,
755 dev_list) {
756 swr_dev->group_id = SWR_GROUP_NONE;
757 master->gr_sid = 0;
758 }
759 is_removed = true;
760 }
761 mutex_unlock(&swrm->mlock);
762
763end:
764 return is_removed;
765}
766
767static void swrm_disable_ports(struct swr_master *master,
768 u8 bank)
769{
770 u32 value;
771 struct swr_port_info *port_req;
772 int i;
773 struct swrm_mports *mport;
774 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
775
776 if (!swrm) {
777 pr_err("%s: swrm is null\n", __func__);
778 return;
779 }
780
781 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
782 master->num_port);
783
784
785 for (i = 0; i < SWR_MSTR_PORT_LEN ; i++) {
786
787 mport = &(swrm->mport_cfg[i]);
788 if (!mport->port_en)
789 continue;
790
791 list_for_each_entry(port_req, &mport->port_req_list, list) {
792 /* skip ports with no change req's*/
793 if (port_req->req_ch == port_req->ch_en)
794 continue;
795
796 swrm_cmd_fifo_wr_cmd(swrm, port_req->req_ch,
797 port_req->dev_num, 0x00,
798 SWRS_DP_CHANNEL_ENABLE_BANK(port_req->slave_port_id,
799 bank));
800 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x\n",
801 __func__, i,
802 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)));
803 }
804 value = ((mport->req_ch)
805 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
806 value |= ((mport->offset2)
807 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
808 value |= ((mport->offset1)
809 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
810 value |= mport->sinterval;
811
812 swr_master_write(swrm,
813 SWRM_DP_PORT_CTRL_BANK(i+1, bank),
814 value);
815 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
816 __func__, i,
817 (SWRM_DP_PORT_CTRL_BANK(i+1, bank)), value);
818 }
819}
820
821static void swrm_cleanup_disabled_port_reqs(struct swr_master *master)
822{
823 struct swr_port_info *port_req, *next;
824 int i;
825 struct swrm_mports *mport;
826 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
827
828 if (!swrm) {
829 pr_err("%s: swrm is null\n", __func__);
830 return;
831 }
832 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
833 master->num_port);
834
835 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
836 mport = &(swrm->mport_cfg[i]);
837 list_for_each_entry_safe(port_req, next,
838 &mport->port_req_list, list) {
839 /* skip ports without new ch req */
840 if (port_req->ch_en == port_req->req_ch)
841 continue;
842
843 /* remove new ch req's*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +0530844 port_req->ch_en = port_req->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530845
846 /* If no streams enabled on port, remove the port req */
847 if (port_req->ch_en == 0) {
848 list_del(&port_req->list);
849 kfree(port_req);
850 }
851 }
852 /* remove new ch req's on mport*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +0530853 mport->ch_en = mport->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530854
855 if (!(mport->ch_en)) {
856 mport->port_en = false;
857 master->port_en_mask &= ~i;
858 }
859 }
860}
861static void swrm_copy_data_port_config(struct swr_master *master, u8 bank)
862{
863 u32 value, slv_id;
864 struct swr_port_info *port_req;
865 int i;
866 struct swrm_mports *mport;
867 u32 reg[SWRM_MAX_PORT_REG];
868 u32 val[SWRM_MAX_PORT_REG];
869 int len = 0;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530870 u8 hparams;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530871 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
872
873 if (!swrm) {
874 pr_err("%s: swrm is null\n", __func__);
875 return;
876 }
877
878 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
879 master->num_port);
880
881 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
882 mport = &(swrm->mport_cfg[i]);
883 if (!mport->port_en)
884 continue;
885
886 list_for_each_entry(port_req, &mport->port_req_list, list) {
887 slv_id = port_req->slave_port_id;
888 reg[len] = SWRM_CMD_FIFO_WR_CMD;
889 val[len++] = SWR_REG_VAL_PACK(port_req->req_ch,
890 port_req->dev_num, 0x00,
891 SWRS_DP_CHANNEL_ENABLE_BANK(slv_id,
892 bank));
893
894 reg[len] = SWRM_CMD_FIFO_WR_CMD;
895 val[len++] = SWR_REG_VAL_PACK(mport->sinterval,
896 port_req->dev_num, 0x00,
897 SWRS_DP_SAMPLE_CONTROL_1_BANK(slv_id,
898 bank));
899
900 reg[len] = SWRM_CMD_FIFO_WR_CMD;
901 val[len++] = SWR_REG_VAL_PACK(mport->offset1,
902 port_req->dev_num, 0x00,
903 SWRS_DP_OFFSET_CONTROL_1_BANK(slv_id,
904 bank));
905
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530906 if (mport->offset2 != SWR_INVALID_PARAM) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530907 reg[len] = SWRM_CMD_FIFO_WR_CMD;
908 val[len++] = SWR_REG_VAL_PACK(mport->offset2,
909 port_req->dev_num, 0x00,
910 SWRS_DP_OFFSET_CONTROL_2_BANK(
911 slv_id, bank));
912 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530913 if (mport->hstart != SWR_INVALID_PARAM
914 && mport->hstop != SWR_INVALID_PARAM) {
915 hparams = (mport->hstart << 4) | mport->hstop;
916
917 reg[len] = SWRM_CMD_FIFO_WR_CMD;
918 val[len++] = SWR_REG_VAL_PACK(hparams,
919 port_req->dev_num, 0x00,
920 SWRS_DP_HCONTROL_BANK(slv_id,
921 bank));
922 }
923 if (mport->word_length != SWR_INVALID_PARAM) {
924 reg[len] = SWRM_CMD_FIFO_WR_CMD;
925 val[len++] =
926 SWR_REG_VAL_PACK(mport->word_length,
927 port_req->dev_num, 0x00,
928 SWRS_DP_BLOCK_CONTROL_1(slv_id));
929 }
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +0530930 if (mport->blk_pack_mode != SWR_INVALID_PARAM
931 && swrm->master_id != MASTER_ID_WSA) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530932 reg[len] = SWRM_CMD_FIFO_WR_CMD;
933 val[len++] =
934 SWR_REG_VAL_PACK(mport->blk_pack_mode,
935 port_req->dev_num, 0x00,
936 SWRS_DP_BLOCK_CONTROL_3_BANK(slv_id,
937 bank));
938 }
939 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
940 reg[len] = SWRM_CMD_FIFO_WR_CMD;
941 val[len++] =
942 SWR_REG_VAL_PACK(mport->blk_grp_count,
943 port_req->dev_num, 0x00,
944 SWRS_DP_BLOCK_CONTROL_2_BANK(slv_id,
945 bank));
946 }
947 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
948 reg[len] = SWRM_CMD_FIFO_WR_CMD;
949 val[len++] =
950 SWR_REG_VAL_PACK(mport->lane_ctrl,
951 port_req->dev_num, 0x00,
952 SWRS_DP_LANE_CONTROL_BANK(slv_id,
953 bank));
954 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530955 port_req->ch_en = port_req->req_ch;
956 }
957 value = ((mport->req_ch)
958 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +0530959
960 if (mport->offset2 != SWR_INVALID_PARAM)
961 value |= ((mport->offset2)
962 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530963 value |= ((mport->offset1)
964 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
965 value |= mport->sinterval;
966
967
968 reg[len] = SWRM_DP_PORT_CTRL_BANK(i + 1, bank);
969 val[len++] = value;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530970 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
971 __func__, i,
972 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)), value);
973
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530974 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
975 reg[len] = SWRM_DP_PORT_CTRL_2_BANK(i + 1, bank);
976 val[len++] = mport->lane_ctrl;
977 }
978 if (mport->word_length != SWR_INVALID_PARAM) {
979 reg[len] = SWRM_DP_BLOCK_CTRL_1(i + 1);
980 val[len++] = mport->word_length;
981 }
982
983 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
984 reg[len] = SWRM_DP_BLOCK_CTRL2_BANK(i + 1, bank);
985 val[len++] = mport->blk_grp_count;
986 }
987 if (mport->hstart != SWR_INVALID_PARAM
988 && mport->hstop != SWR_INVALID_PARAM) {
989 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
Laxminath Kasame30eef72018-11-05 17:40:09 +0530990 hparams = (mport->hstop << 4) | mport->hstart;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530991 val[len++] = hparams;
Laxminath Kasam990c70b2018-11-09 23:15:09 +0530992 } else {
993 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
994 hparams = (SWR_HSTOP_MAX_VAL << 4) | SWR_HSTART_MIN_VAL;
995 val[len++] = hparams;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530996 }
997 if (mport->blk_pack_mode != SWR_INVALID_PARAM) {
998 reg[len] = SWRM_DP_BLOCK_CTRL3_BANK(i + 1, bank);
999 val[len++] = mport->blk_pack_mode;
1000 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301001 mport->ch_en = mport->req_ch;
1002
1003 }
Sudheer Papothi0016db12019-06-11 04:42:38 +05301004 swrm_reg_dump(swrm, reg, val, len, __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301005 swr_master_bulk_write(swrm, reg, val, len);
1006}
1007
1008static void swrm_apply_port_config(struct swr_master *master)
1009{
1010 u8 bank;
1011 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1012
1013 if (!swrm) {
1014 pr_err("%s: Invalid handle to swr controller\n",
1015 __func__);
1016 return;
1017 }
1018
1019 bank = get_inactive_bank_num(swrm);
1020 dev_dbg(swrm->dev, "%s: enter bank: %d master_ports: %d\n",
1021 __func__, bank, master->num_port);
1022
1023
1024 swrm_cmd_fifo_wr_cmd(swrm, 0x01, 0xF, 0x00,
1025 SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(bank));
1026
1027 swrm_copy_data_port_config(master, bank);
1028}
1029
1030static int swrm_slvdev_datapath_control(struct swr_master *master, bool enable)
1031{
1032 u8 bank;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301033 u32 value, n_row, n_col;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301034 int ret;
1035 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1036 int mask = (SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK |
1037 SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK |
1038 SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_BMSK);
1039 u8 inactive_bank;
1040
1041 if (!swrm) {
1042 pr_err("%s: swrm is null\n", __func__);
1043 return -EFAULT;
1044 }
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301045
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301046 mutex_lock(&swrm->mlock);
1047
Ramprasad Katkam979b7c92019-05-17 15:31:21 +05301048 /*
1049 * During disable if master is already down, which implies an ssr/pdr
1050 * scenario, just mark ports as disabled and exit
1051 */
1052 if (swrm->state == SWR_MSTR_SSR && !enable) {
1053 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1054 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1055 __func__);
1056 goto exit;
1057 }
1058 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
1059 swrm_cleanup_disabled_port_reqs(master);
1060 if (!swrm_is_port_en(master)) {
1061 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1062 __func__);
1063 pm_runtime_mark_last_busy(swrm->dev);
1064 pm_runtime_put_autosuspend(swrm->dev);
1065 }
1066 goto exit;
1067 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301068 bank = get_inactive_bank_num(swrm);
1069
1070 if (enable) {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301071 if (!test_bit(ENABLE_PENDING, &swrm->port_req_pending)) {
1072 dev_dbg(swrm->dev, "%s:No pending connect port req\n",
1073 __func__);
1074 goto exit;
1075 }
1076 clear_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301077 ret = swrm_get_port_config(swrm);
1078 if (ret) {
1079 /* cannot accommodate ports */
1080 swrm_cleanup_disabled_port_reqs(master);
1081 mutex_unlock(&swrm->mlock);
1082 return -EINVAL;
1083 }
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301084 swr_master_write(swrm, SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301085 SWRM_INTERRUPT_STATUS_MASK);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301086 /* apply the new port config*/
1087 swrm_apply_port_config(master);
1088 } else {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301089 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1090 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1091 __func__);
1092 goto exit;
1093 }
1094 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301095 swrm_disable_ports(master, bank);
1096 }
1097 dev_dbg(swrm->dev, "%s: enable: %d, cfg_devs: %d\n",
1098 __func__, enable, swrm->num_cfg_devs);
1099
1100 if (enable) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301101 /* set col = 16 */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301102 n_col = SWR_MAX_COL;
1103 } else {
1104 /*
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301105 * Do not change to col = 2 if there are still active ports
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301106 */
1107 if (!master->num_port)
1108 n_col = SWR_MIN_COL;
1109 else
1110 n_col = SWR_MAX_COL;
1111 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301112 /* Use default 50 * x, frame shape. Change based on mclk */
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301113 if (swrm->mclk_freq == MCLK_FREQ_NATIVE) {
1114 dev_dbg(swrm->dev, "setting 64 x %d frameshape\n",
1115 n_col ? 16 : 2);
1116 n_row = SWR_ROW_64;
1117 } else {
1118 dev_dbg(swrm->dev, "setting 50 x %d frameshape\n",
1119 n_col ? 16 : 2);
1120 n_row = SWR_ROW_50;
1121 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301122 value = swr_master_read(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank));
1123 value &= (~mask);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301124 value |= ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301125 (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
1126 (0 << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
1127 swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1128
1129 dev_dbg(swrm->dev, "%s: regaddr: 0x%x, value: 0x%x\n", __func__,
1130 SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1131
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301132 enable_bank_switch(swrm, bank, n_row, n_col);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301133 inactive_bank = bank ? 0 : 1;
1134
1135 if (enable)
1136 swrm_copy_data_port_config(master, inactive_bank);
1137 else {
1138 swrm_disable_ports(master, inactive_bank);
1139 swrm_cleanup_disabled_port_reqs(master);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301140 }
1141 if (!swrm_is_port_en(master)) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301142 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1143 __func__);
1144 pm_runtime_mark_last_busy(swrm->dev);
1145 pm_runtime_put_autosuspend(swrm->dev);
1146 }
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301147exit:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301148 mutex_unlock(&swrm->mlock);
1149return 0;
1150}
1151
1152static int swrm_connect_port(struct swr_master *master,
1153 struct swr_params *portinfo)
1154{
1155 int i;
1156 struct swr_port_info *port_req;
1157 int ret = 0;
1158 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1159 struct swrm_mports *mport;
1160 u8 mstr_port_id, mstr_ch_msk;
1161
1162 dev_dbg(&master->dev, "%s: enter\n", __func__);
1163 if (!portinfo)
1164 return -EINVAL;
1165
1166 if (!swrm) {
1167 dev_err(&master->dev,
1168 "%s: Invalid handle to swr controller\n",
1169 __func__);
1170 return -EINVAL;
1171 }
1172
1173 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301174 mutex_lock(&swrm->devlock);
1175 if (!swrm->dev_up) {
1176 mutex_unlock(&swrm->devlock);
1177 mutex_unlock(&swrm->mlock);
1178 return -EINVAL;
1179 }
1180 mutex_unlock(&swrm->devlock);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301181 if (!swrm_is_port_en(master))
1182 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301183
1184 for (i = 0; i < portinfo->num_port; i++) {
1185 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_msk,
1186 portinfo->port_type[i],
1187 portinfo->port_id[i]);
1188 if (ret) {
1189 dev_err(&master->dev,
1190 "%s: mstr portid for slv port %d not found\n",
1191 __func__, portinfo->port_id[i]);
1192 goto port_fail;
1193 }
1194
1195 mport = &(swrm->mport_cfg[mstr_port_id]);
1196 /* get port req */
1197 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1198 portinfo->dev_num);
1199 if (!port_req) {
1200 dev_dbg(&master->dev, "%s: new req:port id %d dev %d\n",
1201 __func__, portinfo->port_id[i],
1202 portinfo->dev_num);
1203 port_req = kzalloc(sizeof(struct swr_port_info),
1204 GFP_KERNEL);
1205 if (!port_req) {
1206 ret = -ENOMEM;
1207 goto mem_fail;
1208 }
1209 port_req->dev_num = portinfo->dev_num;
1210 port_req->slave_port_id = portinfo->port_id[i];
1211 port_req->num_ch = portinfo->num_ch[i];
1212 port_req->ch_rate = portinfo->ch_rate[i];
1213 port_req->ch_en = 0;
1214 port_req->master_port_id = mstr_port_id;
1215 list_add(&port_req->list, &mport->port_req_list);
1216 }
1217 port_req->req_ch |= portinfo->ch_en[i];
1218
1219 dev_dbg(&master->dev,
1220 "%s: mstr port %d, slv port %d ch_rate %d num_ch %d\n",
1221 __func__, port_req->master_port_id,
1222 port_req->slave_port_id, port_req->ch_rate,
1223 port_req->num_ch);
1224 /* Put the port req on master port */
1225 mport = &(swrm->mport_cfg[mstr_port_id]);
1226 mport->port_en = true;
1227 mport->req_ch |= mstr_ch_msk;
1228 master->port_en_mask |= (1 << mstr_port_id);
1229 }
1230 master->num_port += portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301231 set_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301232 swr_port_response(master, portinfo->tid);
1233
1234 mutex_unlock(&swrm->mlock);
1235 return 0;
1236
1237port_fail:
1238mem_fail:
1239 /* cleanup port reqs in error condition */
1240 swrm_cleanup_disabled_port_reqs(master);
1241 mutex_unlock(&swrm->mlock);
1242 return ret;
1243}
1244
1245static int swrm_disconnect_port(struct swr_master *master,
1246 struct swr_params *portinfo)
1247{
1248 int i, ret = 0;
1249 struct swr_port_info *port_req;
1250 struct swrm_mports *mport;
1251 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1252 u8 mstr_port_id, mstr_ch_mask;
1253
1254 if (!swrm) {
1255 dev_err(&master->dev,
1256 "%s: Invalid handle to swr controller\n",
1257 __func__);
1258 return -EINVAL;
1259 }
1260
1261 if (!portinfo) {
1262 dev_err(&master->dev, "%s: portinfo is NULL\n", __func__);
1263 return -EINVAL;
1264 }
1265 mutex_lock(&swrm->mlock);
1266
1267 for (i = 0; i < portinfo->num_port; i++) {
1268
1269 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_mask,
1270 portinfo->port_type[i], portinfo->port_id[i]);
1271 if (ret) {
1272 dev_err(&master->dev,
1273 "%s: mstr portid for slv port %d not found\n",
1274 __func__, portinfo->port_id[i]);
1275 mutex_unlock(&swrm->mlock);
1276 return -EINVAL;
1277 }
1278 mport = &(swrm->mport_cfg[mstr_port_id]);
1279 /* get port req */
1280 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1281 portinfo->dev_num);
1282
1283 if (!port_req) {
1284 dev_err(&master->dev, "%s:port not enabled : port %d\n",
1285 __func__, portinfo->port_id[i]);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05301286 mutex_unlock(&swrm->mlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301287 return -EINVAL;
1288 }
1289 port_req->req_ch &= ~portinfo->ch_en[i];
1290 mport->req_ch &= ~mstr_ch_mask;
1291 }
1292 master->num_port -= portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301293 set_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301294 swr_port_response(master, portinfo->tid);
1295 mutex_unlock(&swrm->mlock);
1296
1297 return 0;
1298}
1299
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301300static int swrm_find_alert_slave(struct swr_mstr_ctrl *swrm,
1301 int status, u8 *devnum)
1302{
1303 int i;
1304 bool found = false;
1305
1306 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1307 if ((status & SWRM_MCP_SLV_STATUS_MASK) == SWR_ALERT) {
1308 *devnum = i;
1309 found = true;
1310 break;
1311 }
1312 status >>= 2;
1313 }
1314 if (found)
1315 return 0;
1316 else
1317 return -EINVAL;
1318}
1319
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301320static void swrm_enable_slave_irq(struct swr_mstr_ctrl *swrm)
1321{
1322 int i;
1323 int status = 0;
1324
1325 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1326 if (!status) {
1327 dev_dbg_ratelimited(swrm->dev, "%s: slaves status is 0x%x\n",
1328 __func__, status);
1329 return;
1330 }
1331 dev_dbg(swrm->dev, "%s: slave status: 0x%x\n", __func__, status);
1332 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1333 if (status & SWRM_MCP_SLV_STATUS_MASK)
1334 swrm_cmd_fifo_wr_cmd(swrm, 0x4, i, 0x0,
1335 SWRS_SCP_INT_STATUS_MASK_1);
1336 status >>= 2;
1337 }
1338}
1339
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301340static int swrm_check_slave_change_status(struct swr_mstr_ctrl *swrm,
1341 int status, u8 *devnum)
1342{
1343 int i;
1344 int new_sts = status;
1345 int ret = SWR_NOT_PRESENT;
1346
1347 if (status != swrm->slave_status) {
1348 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1349 if ((status & SWRM_MCP_SLV_STATUS_MASK) !=
1350 (swrm->slave_status & SWRM_MCP_SLV_STATUS_MASK)) {
1351 ret = (status & SWRM_MCP_SLV_STATUS_MASK);
1352 *devnum = i;
1353 break;
1354 }
1355 status >>= 2;
1356 swrm->slave_status >>= 2;
1357 }
1358 swrm->slave_status = new_sts;
1359 }
1360 return ret;
1361}
1362
1363static irqreturn_t swr_mstr_interrupt(int irq, void *dev)
1364{
1365 struct swr_mstr_ctrl *swrm = dev;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301366 u32 value, intr_sts, intr_sts_masked;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301367 u32 temp = 0;
1368 u32 status, chg_sts, i;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301369 u8 devnum = 0;
1370 int ret = IRQ_HANDLED;
1371 struct swr_device *swr_dev;
1372 struct swr_master *mstr = &swrm->master;
1373
Ramprasad Katkam57349872018-11-11 18:34:57 +05301374 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1375 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1376 return IRQ_NONE;
1377 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301378
1379 mutex_lock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301380 if (swrm_clk_request(swrm, true)) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +05301381 dev_err_ratelimited(swrm->dev, "%s:clk request failed\n",
1382 __func__);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301383 mutex_unlock(&swrm->reslock);
1384 goto exit;
1385 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301386 mutex_unlock(&swrm->reslock);
1387
1388 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301389 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301390handle_irq:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301391 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301392 value = intr_sts_masked & (1 << i);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301393 if (!value)
1394 continue;
1395
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301396 switch (value) {
1397 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1398 dev_dbg(swrm->dev, "Trigger irq to slave device\n");
1399 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301400 ret = swrm_find_alert_slave(swrm, status, &devnum);
1401 if (ret) {
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301402 dev_err_ratelimited(swrm->dev,
1403 "no slave alert found.spurious interrupt\n");
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05301404 break;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301405 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301406 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1407 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1408 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1409 SWRS_SCP_INT_STATUS_CLEAR_1);
1410 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1411 SWRS_SCP_INT_STATUS_CLEAR_1);
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301412
1413
1414 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1415 if (swr_dev->dev_num != devnum)
1416 continue;
1417 if (swr_dev->slave_irq) {
1418 do {
Ramprasad Katkam2586a4b2019-03-18 16:53:39 +05301419 swr_dev->slave_irq_pending = 0;
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301420 handle_nested_irq(
1421 irq_find_mapping(
1422 swr_dev->slave_irq, 0));
1423 } while (swr_dev->slave_irq_pending);
1424 }
1425
1426 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301427 break;
1428 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1429 dev_dbg(swrm->dev, "SWR new slave attached\n");
1430 break;
1431 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1432 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1433 if (status == swrm->slave_status) {
1434 dev_dbg(swrm->dev,
1435 "%s: No change in slave status: %d\n",
1436 __func__, status);
1437 break;
1438 }
1439 chg_sts = swrm_check_slave_change_status(swrm, status,
1440 &devnum);
1441 switch (chg_sts) {
1442 case SWR_NOT_PRESENT:
1443 dev_dbg(swrm->dev, "device %d got detached\n",
1444 devnum);
1445 break;
1446 case SWR_ATTACHED_OK:
1447 dev_dbg(swrm->dev, "device %d got attached\n",
1448 devnum);
Ramprasad Katkamdebe8932018-09-25 18:08:18 +05301449 /* enable host irq from slave device*/
1450 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1451 SWRS_SCP_INT_STATUS_CLEAR_1);
1452 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1453 SWRS_SCP_INT_STATUS_MASK_1);
1454
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301455 break;
1456 case SWR_ALERT:
1457 dev_dbg(swrm->dev,
1458 "device %d has pending interrupt\n",
1459 devnum);
1460 break;
1461 }
1462 break;
1463 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1464 dev_err_ratelimited(swrm->dev,
1465 "SWR bus clsh detected\n");
1466 break;
1467 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1468 dev_dbg(swrm->dev, "SWR read FIFO overflow\n");
1469 break;
1470 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1471 dev_dbg(swrm->dev, "SWR read FIFO underflow\n");
1472 break;
1473 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1474 dev_dbg(swrm->dev, "SWR write FIFO overflow\n");
1475 break;
1476 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1477 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1478 dev_err_ratelimited(swrm->dev,
1479 "SWR CMD error, fifo status 0x%x, flushing fifo\n",
1480 value);
1481 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1482 break;
1483 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301484 dev_err_ratelimited(swrm->dev, "SWR Port collision detected\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301485 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301486 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301487 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301488 break;
1489 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1490 dev_dbg(swrm->dev, "SWR read enable valid mismatch\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301491 swrm->intr_mask &=
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301492 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1493 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301494 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301495 break;
1496 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1497 complete(&swrm->broadcast);
1498 dev_dbg(swrm->dev, "SWR cmd id finished\n");
1499 break;
1500 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_AUTO_ENUM_FINISHED:
1501 break;
1502 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED:
1503 break;
1504 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL:
1505 break;
1506 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED:
1507 complete(&swrm->reset);
1508 break;
1509 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED:
1510 break;
1511 default:
1512 dev_err_ratelimited(swrm->dev,
1513 "SWR unknown interrupt\n");
1514 ret = IRQ_NONE;
1515 break;
1516 }
1517 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301518 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1519 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
Ramprasad Katkam83303512018-10-11 17:34:22 +05301520
1521 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301522 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301523
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301524 if (intr_sts_masked) {
Ramprasad Katkam83303512018-10-11 17:34:22 +05301525 dev_dbg(swrm->dev, "%s: new interrupt received\n", __func__);
1526 goto handle_irq;
1527 }
1528
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301529 mutex_lock(&swrm->reslock);
1530 swrm_clk_request(swrm, false);
1531 mutex_unlock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301532exit:
Ramprasad Katkam57349872018-11-11 18:34:57 +05301533 swrm_unlock_sleep(swrm);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301534 return ret;
1535}
1536
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301537static irqreturn_t swr_mstr_interrupt_v2(int irq, void *dev)
1538{
1539 struct swr_mstr_ctrl *swrm = dev;
1540 u32 value, intr_sts, intr_sts_masked;
1541 u32 temp = 0;
1542 u32 status, chg_sts, i;
1543 u8 devnum = 0;
1544 int ret = IRQ_HANDLED;
1545 struct swr_device *swr_dev;
1546 struct swr_master *mstr = &swrm->master;
1547
1548 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1549 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1550 return IRQ_NONE;
1551 }
1552
1553 mutex_lock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301554 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
1555 ret = IRQ_NONE;
1556 goto exit;
1557 }
1558 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
1559 ret = IRQ_NONE;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301560 goto err_audio_hw_vote;
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001561 }
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301562 swrm_clk_request(swrm, true);
1563 mutex_unlock(&swrm->reslock);
1564
1565 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1566 intr_sts_masked = intr_sts & swrm->intr_mask;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301567
1568 dev_dbg(swrm->dev, "%s: status: 0x%x \n", __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301569handle_irq:
1570 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
1571 value = intr_sts_masked & (1 << i);
1572 if (!value)
1573 continue;
1574
1575 switch (value) {
1576 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1577 dev_dbg(swrm->dev, "%s: Trigger irq to slave device\n",
1578 __func__);
1579 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1580 ret = swrm_find_alert_slave(swrm, status, &devnum);
1581 if (ret) {
1582 dev_err_ratelimited(swrm->dev,
1583 "%s: no slave alert found.spurious interrupt\n",
1584 __func__);
1585 break;
1586 }
1587 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1588 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1589 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1590 SWRS_SCP_INT_STATUS_CLEAR_1);
1591 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1592 SWRS_SCP_INT_STATUS_CLEAR_1);
1593
1594
1595 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1596 if (swr_dev->dev_num != devnum)
1597 continue;
1598 if (swr_dev->slave_irq) {
1599 do {
1600 handle_nested_irq(
1601 irq_find_mapping(
1602 swr_dev->slave_irq, 0));
1603 } while (swr_dev->slave_irq_pending);
1604 }
1605
1606 }
1607 break;
1608 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1609 dev_dbg(swrm->dev, "%s: SWR new slave attached\n",
1610 __func__);
1611 break;
1612 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1613 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1614 if (status == swrm->slave_status) {
1615 dev_dbg(swrm->dev,
1616 "%s: No change in slave status: %d\n",
1617 __func__, status);
1618 break;
1619 }
1620 chg_sts = swrm_check_slave_change_status(swrm, status,
1621 &devnum);
1622 switch (chg_sts) {
1623 case SWR_NOT_PRESENT:
1624 dev_dbg(swrm->dev,
1625 "%s: device %d got detached\n",
1626 __func__, devnum);
1627 break;
1628 case SWR_ATTACHED_OK:
1629 dev_dbg(swrm->dev,
1630 "%s: device %d got attached\n",
1631 __func__, devnum);
1632 /* enable host irq from slave device*/
1633 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1634 SWRS_SCP_INT_STATUS_CLEAR_1);
1635 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1636 SWRS_SCP_INT_STATUS_MASK_1);
1637
1638 break;
1639 case SWR_ALERT:
1640 dev_dbg(swrm->dev,
1641 "%s: device %d has pending interrupt\n",
1642 __func__, devnum);
1643 break;
1644 }
1645 break;
1646 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1647 dev_err_ratelimited(swrm->dev,
1648 "%s: SWR bus clsh detected\n",
1649 __func__);
1650 break;
1651 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1652 dev_dbg(swrm->dev, "%s: SWR read FIFO overflow\n",
1653 __func__);
1654 break;
1655 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1656 dev_dbg(swrm->dev, "%s: SWR read FIFO underflow\n",
1657 __func__);
1658 break;
1659 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1660 dev_dbg(swrm->dev, "%s: SWR write FIFO overflow\n",
1661 __func__);
1662 break;
1663 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1664 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1665 dev_err_ratelimited(swrm->dev,
1666 "%s: SWR CMD error, fifo status 0x%x, flushing fifo\n",
1667 __func__, value);
1668 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1669 break;
1670 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
1671 dev_err_ratelimited(swrm->dev,
1672 "%s: SWR Port collision detected\n",
1673 __func__);
1674 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
1675 swr_master_write(swrm,
1676 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1677 break;
1678 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1679 dev_dbg(swrm->dev,
1680 "%s: SWR read enable valid mismatch\n",
1681 __func__);
1682 swrm->intr_mask &=
1683 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1684 swr_master_write(swrm,
1685 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1686 break;
1687 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1688 complete(&swrm->broadcast);
1689 dev_dbg(swrm->dev, "%s: SWR cmd id finished\n",
1690 __func__);
1691 break;
1692 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED_V2:
1693 break;
1694 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL_V2:
1695 break;
1696 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED_V2:
1697 break;
1698 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED_V2:
1699 break;
1700 case SWRM_INTERRUPT_STATUS_EXT_CLK_STOP_WAKEUP:
1701 if (swrm->state == SWR_MSTR_UP)
1702 dev_dbg(swrm->dev,
1703 "%s:SWR Master is already up\n",
1704 __func__);
1705 else
1706 dev_err_ratelimited(swrm->dev,
1707 "%s: SWR wokeup during clock stop\n",
1708 __func__);
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301709 /* It might be possible the slave device gets reset
1710 * and slave interrupt gets missed. So re-enable
1711 * Host IRQ and process slave pending
1712 * interrupts, if any.
1713 */
1714 swrm_enable_slave_irq(swrm);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301715 break;
1716 default:
1717 dev_err_ratelimited(swrm->dev,
1718 "%s: SWR unknown interrupt value: %d\n",
1719 __func__, value);
1720 ret = IRQ_NONE;
1721 break;
1722 }
1723 }
1724 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1725 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
1726
1727 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1728 intr_sts_masked = intr_sts & swrm->intr_mask;
1729
1730 if (intr_sts_masked) {
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301731 dev_dbg(swrm->dev, "%s: new interrupt received 0x%x\n",
1732 __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301733 goto handle_irq;
1734 }
1735
1736 mutex_lock(&swrm->reslock);
1737 swrm_clk_request(swrm, false);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301738 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
Sudheer Papothi06f43412019-07-09 03:32:54 +05301739
1740err_audio_hw_vote:
Sudheer Papothi384addd2019-06-14 02:26:52 +05301741 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001742exit:
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301743 mutex_unlock(&swrm->reslock);
1744 swrm_unlock_sleep(swrm);
1745 return ret;
1746}
1747
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301748static irqreturn_t swrm_wakeup_interrupt(int irq, void *dev)
1749{
1750 struct swr_mstr_ctrl *swrm = dev;
1751 int ret = IRQ_HANDLED;
1752
1753 if (!swrm || !(swrm->dev)) {
1754 pr_err("%s: swrm or dev is null\n", __func__);
1755 return IRQ_NONE;
1756 }
1757 mutex_lock(&swrm->devlock);
1758 if (!swrm->dev_up) {
1759 if (swrm->wake_irq > 0)
1760 disable_irq_nosync(swrm->wake_irq);
1761 mutex_unlock(&swrm->devlock);
1762 return ret;
1763 }
1764 mutex_unlock(&swrm->devlock);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301765 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1766 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1767 goto exit;
1768 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301769 if (swrm->wake_irq > 0)
1770 disable_irq_nosync(swrm->wake_irq);
1771 pm_runtime_get_sync(swrm->dev);
1772 pm_runtime_mark_last_busy(swrm->dev);
1773 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301774 swrm_unlock_sleep(swrm);
1775exit:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301776 return ret;
1777}
1778
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301779static void swrm_wakeup_work(struct work_struct *work)
1780{
1781 struct swr_mstr_ctrl *swrm;
1782
1783 swrm = container_of(work, struct swr_mstr_ctrl,
1784 wakeup_work);
1785 if (!swrm || !(swrm->dev)) {
1786 pr_err("%s: swrm or dev is null\n", __func__);
1787 return;
1788 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301789
1790 mutex_lock(&swrm->devlock);
1791 if (!swrm->dev_up) {
1792 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301793 goto exit;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301794 }
1795 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301796 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1797 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1798 goto exit;
1799 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301800 pm_runtime_get_sync(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301801 pm_runtime_mark_last_busy(swrm->dev);
1802 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301803 swrm_unlock_sleep(swrm);
1804exit:
1805 pm_relax(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301806}
1807
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301808static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
1809{
1810 u32 val;
1811
1812 swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1813 val = (swrm->slave_status >> (devnum * 2));
1814 val &= SWRM_MCP_SLV_STATUS_MASK;
1815 return val;
1816}
1817
1818static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
1819 u8 *dev_num)
1820{
1821 int i;
1822 u64 id = 0;
1823 int ret = -EINVAL;
1824 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1825 struct swr_device *swr_dev;
1826 u32 num_dev = 0;
1827
1828 if (!swrm) {
1829 pr_err("%s: Invalid handle to swr controller\n",
1830 __func__);
1831 return ret;
1832 }
1833 if (swrm->num_dev)
1834 num_dev = swrm->num_dev;
1835 else
1836 num_dev = mstr->num_dev;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301837
1838 mutex_lock(&swrm->devlock);
1839 if (!swrm->dev_up) {
1840 mutex_unlock(&swrm->devlock);
1841 return ret;
1842 }
1843 mutex_unlock(&swrm->devlock);
1844
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301845 pm_runtime_get_sync(swrm->dev);
1846 for (i = 1; i < (num_dev + 1); i++) {
1847 id = ((u64)(swr_master_read(swrm,
1848 SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
1849 id |= swr_master_read(swrm,
1850 SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301851
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301852 /*
1853 * As pm_runtime_get_sync() brings all slaves out of reset
1854 * update logical device number for all slaves.
1855 */
1856 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1857 if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
1858 u32 status = swrm_get_device_status(swrm, i);
1859
1860 if ((status == 0x01) || (status == 0x02)) {
1861 swr_dev->dev_num = i;
1862 if ((id & SWR_DEV_ID_MASK) == dev_id) {
1863 *dev_num = i;
1864 ret = 0;
1865 }
1866 dev_dbg(swrm->dev,
1867 "%s: devnum %d is assigned for dev addr %lx\n",
1868 __func__, i, swr_dev->addr);
1869 }
1870 }
1871 }
1872 }
1873 if (ret)
1874 dev_err(swrm->dev, "%s: device 0x%llx is not ready\n",
1875 __func__, dev_id);
1876
1877 pm_runtime_mark_last_busy(swrm->dev);
1878 pm_runtime_put_autosuspend(swrm->dev);
1879 return ret;
1880}
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301881
1882static void swrm_device_wakeup_vote(struct swr_master *mstr)
1883{
1884 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1885
1886 if (!swrm) {
1887 pr_err("%s: Invalid handle to swr controller\n",
1888 __func__);
1889 return;
1890 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05301891 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1892 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1893 return;
1894 }
Sudheer Papothi384addd2019-06-14 02:26:52 +05301895 if (++swrm->hw_core_clk_en == 1)
1896 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
1897 dev_err(swrm->dev, "%s:lpass core hw enable failed\n",
1898 __func__);
1899 --swrm->hw_core_clk_en;
1900 }
1901 if ( ++swrm->aud_core_clk_en == 1)
1902 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
1903 dev_err(swrm->dev, "%s:lpass audio hw enable failed\n",
1904 __func__);
1905 --swrm->aud_core_clk_en;
1906 }
1907 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
1908 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301909 pm_runtime_get_sync(swrm->dev);
1910}
1911
1912static void swrm_device_wakeup_unvote(struct swr_master *mstr)
1913{
1914 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1915
1916 if (!swrm) {
1917 pr_err("%s: Invalid handle to swr controller\n",
1918 __func__);
1919 return;
1920 }
1921 pm_runtime_mark_last_busy(swrm->dev);
1922 pm_runtime_put_autosuspend(swrm->dev);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301923 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
1924 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
1925
1926 --swrm->aud_core_clk_en;
1927 if (swrm->aud_core_clk_en < 0)
1928 swrm->aud_core_clk_en = 0;
1929 else if (swrm->aud_core_clk_en == 0)
1930 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
1931
1932 --swrm->hw_core_clk_en;
1933 if (swrm->hw_core_clk_en < 0)
1934 swrm->hw_core_clk_en = 0;
1935 else if (swrm->hw_core_clk_en == 0)
1936 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
1937
Ramprasad Katkam57349872018-11-11 18:34:57 +05301938 swrm_unlock_sleep(swrm);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301939}
1940
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301941static int swrm_master_init(struct swr_mstr_ctrl *swrm)
1942{
1943 int ret = 0;
1944 u32 val;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301945 u8 row_ctrl = SWR_ROW_50;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301946 u8 col_ctrl = SWR_MIN_COL;
1947 u8 ssp_period = 1;
1948 u8 retry_cmd_num = 3;
1949 u32 reg[SWRM_MAX_INIT_REG];
1950 u32 value[SWRM_MAX_INIT_REG];
1951 int len = 0;
1952
1953 /* Clear Rows and Cols */
1954 val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
1955 (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
1956 (ssp_period << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
1957
1958 reg[len] = SWRM_MCP_FRAME_CTRL_BANK_ADDR(0);
1959 value[len++] = val;
1960
1961 /* Set Auto enumeration flag */
1962 reg[len] = SWRM_ENUMERATOR_CFG_ADDR;
1963 value[len++] = 1;
1964
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301965 /* Configure No pings */
1966 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
1967 val &= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK;
1968 val |= (0x1f << SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT);
1969 reg[len] = SWRM_MCP_CFG_ADDR;
1970 value[len++] = val;
1971
1972 /* Configure number of retries of a read/write cmd */
1973 val = (retry_cmd_num << SWRM_CMD_FIFO_CFG_NUM_OF_CMD_RETRY_SHFT);
1974 reg[len] = SWRM_CMD_FIFO_CFG_ADDR;
1975 value[len++] = val;
1976
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301977 reg[len] = SWRM_MCP_BUS_CTRL_ADDR;
1978 value[len++] = 0x2;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301979
Ramprasad Katkam83303512018-10-11 17:34:22 +05301980 /* Set IRQ to PULSE */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301981 reg[len] = SWRM_COMP_CFG_ADDR;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301982 value[len++] = 0x02;
1983
1984 reg[len] = SWRM_COMP_CFG_ADDR;
1985 value[len++] = 0x03;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301986
1987 reg[len] = SWRM_INTERRUPT_CLEAR;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301988 value[len++] = 0xFFFFFFFF;
1989
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301990 swrm->intr_mask = SWRM_INTERRUPT_STATUS_MASK;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301991 /* Mask soundwire interrupts */
1992 reg[len] = SWRM_INTERRUPT_MASK_ADDR;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301993 value[len++] = swrm->intr_mask;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301994
1995 reg[len] = SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301996 value[len++] = swrm->intr_mask;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301997
1998 swr_master_bulk_write(swrm, reg, value, len);
1999
Sudheer Papothi63f48152018-11-15 01:08:03 +05302000 /*
2001 * For SWR master version 1.5.1, continue
2002 * execute on command ignore.
2003 */
2004 if (swrm->version == SWRM_VERSION_1_5_1)
2005 swr_master_write(swrm, SWRM_CMD_FIFO_CFG_ADDR,
2006 (swr_master_read(swrm,
2007 SWRM_CMD_FIFO_CFG_ADDR) | 0x80000000));
2008
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302009 return ret;
2010}
2011
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302012static int swrm_event_notify(struct notifier_block *self,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302013 unsigned long action, void *data)
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302014{
2015 struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302016 event_notifier);
2017
2018 if (!swrm || !(swrm->dev)) {
2019 pr_err("%s: swrm or dev is NULL\n", __func__);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302020 return -EINVAL;
2021 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302022 switch (action) {
2023 case MSM_AUD_DC_EVENT:
2024 schedule_work(&(swrm->dc_presence_work));
2025 break;
2026 case SWR_WAKE_IRQ_EVENT:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302027 if (swrm->ipc_wakeup && !swrm->ipc_wakeup_triggered) {
2028 swrm->ipc_wakeup_triggered = true;
Ramprasad Katkam57349872018-11-11 18:34:57 +05302029 pm_stay_awake(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302030 schedule_work(&swrm->wakeup_work);
Ramprasad Katkamcd61c6e2018-09-18 13:22:58 +05302031 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302032 break;
2033 default:
2034 dev_err(swrm->dev, "%s: invalid event type: %lu\n",
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302035 __func__, action);
2036 return -EINVAL;
2037 }
2038
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302039 return 0;
2040}
2041
2042static void swrm_notify_work_fn(struct work_struct *work)
2043{
2044 struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302045 dc_presence_work);
2046
2047 if (!swrm || !swrm->pdev) {
2048 pr_err("%s: swrm or pdev is NULL\n", __func__);
2049 return;
2050 }
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302051 swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL);
2052}
2053
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302054static int swrm_probe(struct platform_device *pdev)
2055{
2056 struct swr_mstr_ctrl *swrm;
2057 struct swr_ctrl_platform_data *pdata;
2058 u32 i, num_ports, port_num, port_type, ch_mask;
2059 u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0;
2060 int ret = 0;
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302061 struct clk *lpass_core_hw_vote = NULL;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302062 struct clk *lpass_core_audio = NULL;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302063
2064 /* Allocate soundwire master driver structure */
2065 swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl),
2066 GFP_KERNEL);
2067 if (!swrm) {
2068 ret = -ENOMEM;
2069 goto err_memory_fail;
2070 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302071 swrm->pdev = pdev;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302072 swrm->dev = &pdev->dev;
2073 platform_set_drvdata(pdev, swrm);
2074 swr_set_ctrl_data(&swrm->master, swrm);
2075 pdata = dev_get_platdata(&pdev->dev);
2076 if (!pdata) {
2077 dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
2078 __func__);
2079 ret = -EINVAL;
2080 goto err_pdata_fail;
2081 }
2082 swrm->handle = (void *)pdata->handle;
2083 if (!swrm->handle) {
2084 dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
2085 __func__);
2086 ret = -EINVAL;
2087 goto err_pdata_fail;
2088 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302089 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr_master_id",
2090 &swrm->master_id);
2091 if (ret) {
2092 dev_err(&pdev->dev, "%s: failed to get master id\n", __func__);
2093 goto err_pdata_fail;
2094 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302095 if (!(of_property_read_u32(pdev->dev.of_node,
2096 "swrm-io-base", &swrm->swrm_base_reg)))
2097 ret = of_property_read_u32(pdev->dev.of_node,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302098 "swrm-io-base", &swrm->swrm_base_reg);
2099 if (!swrm->swrm_base_reg) {
2100 swrm->read = pdata->read;
2101 if (!swrm->read) {
2102 dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
2103 __func__);
2104 ret = -EINVAL;
2105 goto err_pdata_fail;
2106 }
2107 swrm->write = pdata->write;
2108 if (!swrm->write) {
2109 dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
2110 __func__);
2111 ret = -EINVAL;
2112 goto err_pdata_fail;
2113 }
2114 swrm->bulk_write = pdata->bulk_write;
2115 if (!swrm->bulk_write) {
2116 dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
2117 __func__);
2118 ret = -EINVAL;
2119 goto err_pdata_fail;
2120 }
2121 } else {
2122 swrm->swrm_dig_base = devm_ioremap(&pdev->dev,
2123 swrm->swrm_base_reg, SWRM_MAX_REGISTER);
2124 }
2125
2126 swrm->clk = pdata->clk;
2127 if (!swrm->clk) {
2128 dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
2129 __func__);
2130 ret = -EINVAL;
2131 goto err_pdata_fail;
2132 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302133 if (of_property_read_u32(pdev->dev.of_node,
2134 "qcom,swr-clock-stop-mode0",
2135 &swrm->clk_stop_mode0_supp)) {
2136 swrm->clk_stop_mode0_supp = FALSE;
2137 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302138
2139 ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
2140 &swrm->num_dev);
2141 if (ret) {
2142 dev_dbg(&pdev->dev, "%s: Looking up %s property failed\n",
2143 __func__, "qcom,swr-num-dev");
2144 } else {
2145 if (swrm->num_dev > SWR_MAX_SLAVE_DEVICES) {
2146 dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
2147 __func__, swrm->num_dev, SWR_MAX_SLAVE_DEVICES);
2148 ret = -EINVAL;
2149 goto err_pdata_fail;
2150 }
2151 }
2152
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302153 /* Parse soundwire port mapping */
2154 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
2155 &num_ports);
2156 if (ret) {
2157 dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
2158 goto err_pdata_fail;
2159 }
2160 swrm->num_ports = num_ports;
2161
2162 if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
2163 &map_size)) {
2164 dev_err(swrm->dev, "missing port mapping\n");
2165 goto err_pdata_fail;
2166 }
2167
2168 map_length = map_size / (3 * sizeof(u32));
2169 if (num_ports > SWR_MSTR_PORT_LEN) {
2170 dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
2171 __func__);
2172 ret = -EINVAL;
2173 goto err_pdata_fail;
2174 }
2175 temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
2176
2177 if (!temp) {
2178 ret = -ENOMEM;
2179 goto err_pdata_fail;
2180 }
2181 ret = of_property_read_u32_array(pdev->dev.of_node,
2182 "qcom,swr-port-mapping", temp, 3 * map_length);
2183 if (ret) {
2184 dev_err(swrm->dev, "%s: Failed to read port mapping\n",
2185 __func__);
2186 goto err_pdata_fail;
2187 }
2188
2189 for (i = 0; i < map_length; i++) {
2190 port_num = temp[3 * i];
2191 port_type = temp[3 * i + 1];
2192 ch_mask = temp[3 * i + 2];
2193
2194 if (port_num != old_port_num)
2195 ch_iter = 0;
2196 swrm->port_mapping[port_num][ch_iter].port_type = port_type;
2197 swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
2198 old_port_num = port_num;
2199 }
2200 devm_kfree(&pdev->dev, temp);
2201
2202 swrm->reg_irq = pdata->reg_irq;
2203 swrm->master.read = swrm_read;
2204 swrm->master.write = swrm_write;
2205 swrm->master.bulk_write = swrm_bulk_write;
2206 swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
2207 swrm->master.connect_port = swrm_connect_port;
2208 swrm->master.disconnect_port = swrm_disconnect_port;
2209 swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
2210 swrm->master.remove_from_group = swrm_remove_from_group;
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302211 swrm->master.device_wakeup_vote = swrm_device_wakeup_vote;
2212 swrm->master.device_wakeup_unvote = swrm_device_wakeup_unvote;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302213 swrm->master.dev.parent = &pdev->dev;
2214 swrm->master.dev.of_node = pdev->dev.of_node;
2215 swrm->master.num_port = 0;
2216 swrm->rcmd_id = 0;
2217 swrm->wcmd_id = 0;
2218 swrm->slave_status = 0;
2219 swrm->num_rx_chs = 0;
2220 swrm->clk_ref_count = 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302221 swrm->swr_irq_wakeup_capable = 0;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302222 swrm->mclk_freq = MCLK_FREQ;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302223 swrm->dev_up = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302224 swrm->state = SWR_MSTR_UP;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302225 swrm->ipc_wakeup = false;
2226 swrm->ipc_wakeup_triggered = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302227 init_completion(&swrm->reset);
2228 init_completion(&swrm->broadcast);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302229 init_completion(&swrm->clk_off_complete);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302230 mutex_init(&swrm->mlock);
2231 mutex_init(&swrm->reslock);
2232 mutex_init(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302233 mutex_init(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302234 mutex_init(&swrm->clklock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302235 mutex_init(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302236 mutex_init(&swrm->pm_lock);
2237 swrm->wlock_holders = 0;
2238 swrm->pm_state = SWRM_PM_SLEEPABLE;
2239 init_waitqueue_head(&swrm->pm_wq);
2240 pm_qos_add_request(&swrm->pm_qos_req,
2241 PM_QOS_CPU_DMA_LATENCY,
2242 PM_QOS_DEFAULT_VALUE);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302243
2244 for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++)
2245 INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
2246
Sudheer Papothi06f43412019-07-09 03:32:54 +05302247 /* Register LPASS core hw vote */
2248 lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
2249 if (IS_ERR(lpass_core_hw_vote)) {
2250 ret = PTR_ERR(lpass_core_hw_vote);
2251 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2252 __func__, "lpass_core_hw_vote", ret);
2253 lpass_core_hw_vote = NULL;
2254 ret = 0;
2255 }
2256 swrm->lpass_core_hw_vote = lpass_core_hw_vote;
2257
2258 /* Register LPASS audio core vote */
2259 lpass_core_audio = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
2260 if (IS_ERR(lpass_core_audio)) {
2261 ret = PTR_ERR(lpass_core_audio);
2262 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2263 __func__, "lpass_core_audio", ret);
2264 lpass_core_audio = NULL;
2265 ret = 0;
2266 }
2267 swrm->lpass_core_audio = lpass_core_audio;
2268
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302269 if (swrm->reg_irq) {
2270 ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
2271 SWR_IRQ_REGISTER);
2272 if (ret) {
2273 dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
2274 __func__, ret);
2275 goto err_irq_fail;
2276 }
2277 } else {
2278 swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
2279 if (swrm->irq < 0) {
2280 dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
2281 __func__, swrm->irq);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302282 goto err_irq_fail;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302283 }
2284
2285 ret = request_threaded_irq(swrm->irq, NULL,
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302286 swr_mstr_interrupt_v2,
Ramprasad Katkam83303512018-10-11 17:34:22 +05302287 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302288 "swr_master_irq", swrm);
2289 if (ret) {
2290 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2291 __func__, ret);
2292 goto err_irq_fail;
2293 }
2294
2295 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302296 /* Make inband tx interrupts as wakeup capable for slave irq */
2297 ret = of_property_read_u32(pdev->dev.of_node,
2298 "qcom,swr-mstr-irq-wakeup-capable",
2299 &swrm->swr_irq_wakeup_capable);
2300 if (ret)
2301 dev_dbg(swrm->dev, "%s: swrm irq wakeup capable not defined\n",
2302 __func__);
2303 if (swrm->swr_irq_wakeup_capable)
2304 irq_set_irq_wake(swrm->irq, 1);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302305 ret = swr_register_master(&swrm->master);
2306 if (ret) {
2307 dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
2308 goto err_mstr_fail;
2309 }
2310
2311 /* Add devices registered with board-info as the
2312 * controller will be up now
2313 */
2314 swr_master_add_boarddevices(&swrm->master);
2315 mutex_lock(&swrm->mlock);
2316 swrm_clk_request(swrm, true);
2317 ret = swrm_master_init(swrm);
2318 if (ret < 0) {
2319 dev_err(&pdev->dev,
2320 "%s: Error in master Initialization , err %d\n",
2321 __func__, ret);
2322 mutex_unlock(&swrm->mlock);
2323 goto err_mstr_fail;
2324 }
2325 swrm->version = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
2326
2327 mutex_unlock(&swrm->mlock);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302328 INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302329
2330 if (pdev->dev.of_node)
2331 of_register_swr_devices(&swrm->master);
2332
2333 dbgswrm = swrm;
2334 debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
2335 if (!IS_ERR(debugfs_swrm_dent)) {
2336 debugfs_peek = debugfs_create_file("swrm_peek",
2337 S_IFREG | 0444, debugfs_swrm_dent,
2338 (void *) "swrm_peek", &swrm_debug_ops);
2339
2340 debugfs_poke = debugfs_create_file("swrm_poke",
2341 S_IFREG | 0444, debugfs_swrm_dent,
2342 (void *) "swrm_poke", &swrm_debug_ops);
2343
2344 debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
2345 S_IFREG | 0444, debugfs_swrm_dent,
2346 (void *) "swrm_reg_dump",
2347 &swrm_debug_ops);
2348 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302349
2350 ret = device_init_wakeup(swrm->dev, true);
2351 if (ret) {
2352 dev_err(swrm->dev, "Device wakeup init failed: %d\n", ret);
2353 goto err_irq_wakeup_fail;
2354 }
2355
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302356 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
2357 pm_runtime_use_autosuspend(&pdev->dev);
2358 pm_runtime_set_active(&pdev->dev);
2359 pm_runtime_enable(&pdev->dev);
2360 pm_runtime_mark_last_busy(&pdev->dev);
2361
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302362 INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn);
2363 swrm->event_notifier.notifier_call = swrm_event_notify;
2364 msm_aud_evt_register_client(&swrm->event_notifier);
2365
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302366 return 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302367err_irq_wakeup_fail:
2368 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302369err_mstr_fail:
2370 if (swrm->reg_irq)
2371 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2372 swrm, SWR_IRQ_FREE);
2373 else if (swrm->irq)
2374 free_irq(swrm->irq, swrm);
2375err_irq_fail:
2376 mutex_destroy(&swrm->mlock);
2377 mutex_destroy(&swrm->reslock);
2378 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302379 mutex_destroy(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302380 mutex_destroy(&swrm->clklock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302381 mutex_destroy(&swrm->pm_lock);
2382 pm_qos_remove_request(&swrm->pm_qos_req);
2383
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302384err_pdata_fail:
2385err_memory_fail:
2386 return ret;
2387}
2388
2389static int swrm_remove(struct platform_device *pdev)
2390{
2391 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2392
2393 if (swrm->reg_irq)
2394 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2395 swrm, SWR_IRQ_FREE);
2396 else if (swrm->irq)
2397 free_irq(swrm->irq, swrm);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302398 else if (swrm->wake_irq > 0)
2399 free_irq(swrm->wake_irq, swrm);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302400 if (swrm->swr_irq_wakeup_capable)
2401 irq_set_irq_wake(swrm->irq, 0);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302402 cancel_work_sync(&swrm->wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302403 pm_runtime_disable(&pdev->dev);
2404 pm_runtime_set_suspended(&pdev->dev);
2405 swr_unregister_master(&swrm->master);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302406 msm_aud_evt_unregister_client(&swrm->event_notifier);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302407 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302408 mutex_destroy(&swrm->mlock);
2409 mutex_destroy(&swrm->reslock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302410 mutex_destroy(&swrm->iolock);
2411 mutex_destroy(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302412 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302413 mutex_destroy(&swrm->pm_lock);
2414 pm_qos_remove_request(&swrm->pm_qos_req);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302415 devm_kfree(&pdev->dev, swrm);
2416 return 0;
2417}
2418
2419static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
2420{
2421 u32 val;
2422
2423 dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
2424 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD);
2425 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2426 val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK;
2427 swr_master_write(swrm, SWRM_MCP_CFG_ADDR, val);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302428
2429 return 0;
2430}
2431
2432#ifdef CONFIG_PM
2433static int swrm_runtime_resume(struct device *dev)
2434{
2435 struct platform_device *pdev = to_platform_device(dev);
2436 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2437 int ret = 0;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302438 bool hw_core_err = false;
2439 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302440 struct swr_master *mstr = &swrm->master;
2441 struct swr_device *swr_dev;
2442
2443 dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
2444 __func__, swrm->state);
2445 mutex_lock(&swrm->reslock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302446
Sudheer Papothi384addd2019-06-14 02:26:52 +05302447 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2448 dev_err(dev, "%s:lpass core hw enable failed\n",
2449 __func__);
2450 hw_core_err = true;
2451 }
2452 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2453 dev_err(dev, "%s:lpass audio hw enable failed\n",
2454 __func__);
2455 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002456 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302457
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302458 if ((swrm->state == SWR_MSTR_DOWN) ||
2459 (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) {
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302460 if (swrm->clk_stop_mode0_supp) {
2461 if (swrm->ipc_wakeup)
2462 msm_aud_evt_blocking_notifier_call_chain(
2463 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302464 }
2465
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302466 if (swrm_clk_request(swrm, true)) {
2467 /*
2468 * Set autosuspend timer to 1 for
2469 * master to enter into suspend.
2470 */
2471 auto_suspend_timer = 1;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302472 goto exit;
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302473 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302474 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302475 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2476 ret = swr_device_up(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302477 if (ret == -ENODEV) {
2478 dev_dbg(dev,
2479 "%s slave device up not implemented\n",
2480 __func__);
2481 ret = 0;
2482 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302483 dev_err(dev,
2484 "%s: failed to wakeup swr dev %d\n",
2485 __func__, swr_dev->dev_num);
2486 swrm_clk_request(swrm, false);
2487 goto exit;
2488 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302489 }
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05302490 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2491 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2492 swrm_master_init(swrm);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302493 /* wait for hw enumeration to complete */
2494 usleep_range(100, 105);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302495 swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, 0x0,
2496 SWRS_SCP_INT_STATUS_MASK_1);
Karthikeyan Manif6821902019-05-21 17:31:24 -07002497 if (swrm->state == SWR_MSTR_SSR) {
2498 mutex_unlock(&swrm->reslock);
2499 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
2500 mutex_lock(&swrm->reslock);
2501 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302502 } else {
2503 /*wake up from clock stop*/
2504 swr_master_write(swrm, SWRM_MCP_BUS_CTRL_ADDR, 0x2);
2505 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302506 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302507 swrm->state = SWR_MSTR_UP;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302508 }
2509exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302510 if (!aud_core_err)
2511 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2512 if (!hw_core_err)
2513 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302514 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302515 auto_suspend_timer = SWR_AUTO_SUSPEND_DELAY * 1000;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302516 mutex_unlock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302517
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302518 return ret;
2519}
2520
2521static int swrm_runtime_suspend(struct device *dev)
2522{
2523 struct platform_device *pdev = to_platform_device(dev);
2524 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2525 int ret = 0;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302526 bool hw_core_err = false;
2527 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302528 struct swr_master *mstr = &swrm->master;
2529 struct swr_device *swr_dev;
2530 int current_state = 0;
2531
2532 dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
2533 __func__, swrm->state);
2534 mutex_lock(&swrm->reslock);
2535 mutex_lock(&swrm->force_down_lock);
2536 current_state = swrm->state;
2537 mutex_unlock(&swrm->force_down_lock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302538
2539 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2540 dev_err(dev, "%s:lpass core hw enable failed\n",
2541 __func__);
2542 hw_core_err = true;
2543 }
2544 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2545 dev_err(dev, "%s:lpass audio hw enable failed\n",
2546 __func__);
2547 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002548 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302549
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302550 if ((current_state == SWR_MSTR_UP) ||
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302551 (current_state == SWR_MSTR_SSR)) {
2552
2553 if ((current_state != SWR_MSTR_SSR) &&
2554 swrm_is_port_en(&swrm->master)) {
2555 dev_dbg(dev, "%s ports are enabled\n", __func__);
2556 ret = -EBUSY;
2557 goto exit;
2558 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302559 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05302560 mutex_unlock(&swrm->reslock);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +05302561 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
Sudheer Papothi06f43412019-07-09 03:32:54 +05302562 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302563 swrm_clk_pause(swrm);
2564 swr_master_write(swrm, SWRM_COMP_CFG_ADDR, 0x00);
2565 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2566 ret = swr_device_down(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302567 if (ret == -ENODEV) {
2568 dev_dbg_ratelimited(dev,
2569 "%s slave device down not implemented\n",
2570 __func__);
2571 ret = 0;
2572 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302573 dev_err(dev,
2574 "%s: failed to shutdown swr dev %d\n",
2575 __func__, swr_dev->dev_num);
2576 goto exit;
2577 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302578 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302579 } else {
Sudheer Papothi384addd2019-06-14 02:26:52 +05302580 mutex_unlock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302581 /* clock stop sequence */
2582 swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
2583 SWRS_SCP_CONTROL);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302584 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302585 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302586 }
2587 swrm_clk_request(swrm, false);
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302588
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302589 if (swrm->clk_stop_mode0_supp) {
2590 if (swrm->wake_irq > 0) {
2591 enable_irq(swrm->wake_irq);
2592 } else if (swrm->ipc_wakeup) {
2593 msm_aud_evt_blocking_notifier_call_chain(
2594 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
2595 swrm->ipc_wakeup_triggered = false;
2596 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302597 }
2598
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302599 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302600 /* Retain SSR state until resume */
2601 if (current_state != SWR_MSTR_SSR)
2602 swrm->state = SWR_MSTR_DOWN;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302603exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302604 if (!aud_core_err)
2605 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2606 if (!hw_core_err)
2607 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302608 mutex_unlock(&swrm->reslock);
2609 return ret;
2610}
2611#endif /* CONFIG_PM */
2612
Sudheer Papothi06f43412019-07-09 03:32:54 +05302613static int swrm_device_suspend(struct device *dev)
2614{
2615 struct platform_device *pdev = to_platform_device(dev);
2616 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2617 int ret = 0;
2618
2619 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2620 if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
2621 ret = swrm_runtime_suspend(dev);
2622 if (!ret) {
2623 pm_runtime_disable(dev);
2624 pm_runtime_set_suspended(dev);
2625 pm_runtime_enable(dev);
2626 }
2627 }
2628
2629 return 0;
2630}
2631
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302632static int swrm_device_down(struct device *dev)
2633{
2634 struct platform_device *pdev = to_platform_device(dev);
2635 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302636
2637 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2638
2639 mutex_lock(&swrm->force_down_lock);
2640 swrm->state = SWR_MSTR_SSR;
2641 mutex_unlock(&swrm->force_down_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302642
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302643 swrm_device_suspend(dev);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302644 return 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302645}
2646
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302647int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm)
2648{
2649 int ret = 0;
Laxminath Kasama60239e2019-01-10 14:43:03 +05302650 int irq, dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302651
2652 if (!swrm->ipc_wakeup) {
Laxminath Kasama60239e2019-01-10 14:43:03 +05302653 irq = of_get_named_gpio(swrm->dev->of_node,
2654 "qcom,swr-wakeup-irq", 0);
2655 if (gpio_is_valid(irq)) {
2656 swrm->wake_irq = gpio_to_irq(irq);
2657 if (swrm->wake_irq < 0) {
2658 dev_err(swrm->dev,
2659 "Unable to configure irq\n");
2660 return swrm->wake_irq;
2661 }
2662 } else {
2663 dir_apps_irq = platform_get_irq_byname(swrm->pdev,
2664 "swr_wake_irq");
2665 if (dir_apps_irq < 0) {
2666 dev_err(swrm->dev,
2667 "TLMM connect gpio not found\n");
2668 return -EINVAL;
2669 }
2670 swrm->wake_irq = dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302671 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302672 ret = request_threaded_irq(swrm->wake_irq, NULL,
2673 swrm_wakeup_interrupt,
2674 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
2675 "swr_wake_irq", swrm);
2676 if (ret) {
2677 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2678 __func__, ret);
2679 return -EINVAL;
2680 }
Aditya Bavanari3517b112018-12-03 13:26:59 +05302681 irq_set_irq_wake(swrm->wake_irq, 1);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302682 }
2683 return ret;
2684}
2685
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302686static int swrm_alloc_port_mem(struct device *dev, struct swr_mstr_ctrl *swrm,
2687 u32 uc, u32 size)
2688{
2689 if (!swrm->port_param) {
2690 swrm->port_param = devm_kzalloc(dev,
2691 sizeof(swrm->port_param) * SWR_UC_MAX,
2692 GFP_KERNEL);
2693 if (!swrm->port_param)
2694 return -ENOMEM;
2695 }
2696 if (!swrm->port_param[uc]) {
2697 swrm->port_param[uc] = devm_kcalloc(dev, size,
2698 sizeof(struct port_params),
2699 GFP_KERNEL);
2700 if (!swrm->port_param[uc])
2701 return -ENOMEM;
2702 } else {
2703 dev_err_ratelimited(swrm->dev, "%s: called more than once\n",
2704 __func__);
2705 }
2706
2707 return 0;
2708}
2709
2710static int swrm_copy_port_config(struct swr_mstr_ctrl *swrm,
2711 struct swrm_port_config *port_cfg,
2712 u32 size)
2713{
2714 int idx;
2715 struct port_params *params;
2716 int uc = port_cfg->uc;
2717 int ret = 0;
2718
2719 for (idx = 0; idx < size; idx++) {
2720 params = &((struct port_params *)port_cfg->params)[idx];
2721 if (!params) {
2722 dev_err(swrm->dev, "%s: Invalid params\n", __func__);
2723 ret = -EINVAL;
2724 break;
2725 }
2726 memcpy(&swrm->port_param[uc][idx], params,
2727 sizeof(struct port_params));
2728 }
2729
2730 return ret;
2731}
2732
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302733/**
2734 * swrm_wcd_notify - parent device can notify to soundwire master through
2735 * this function
2736 * @pdev: pointer to platform device structure
2737 * @id: command id from parent to the soundwire master
2738 * @data: data from parent device to soundwire master
2739 */
2740int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
2741{
2742 struct swr_mstr_ctrl *swrm;
2743 int ret = 0;
2744 struct swr_master *mstr;
2745 struct swr_device *swr_dev;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302746 struct swrm_port_config *port_cfg;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302747
2748 if (!pdev) {
2749 pr_err("%s: pdev is NULL\n", __func__);
2750 return -EINVAL;
2751 }
2752 swrm = platform_get_drvdata(pdev);
2753 if (!swrm) {
2754 dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__);
2755 return -EINVAL;
2756 }
2757 mstr = &swrm->master;
2758
2759 switch (id) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05302760 case SWR_REQ_CLK_SWITCH:
2761 /* This will put soundwire in clock stop mode and disable the
2762 * clocks, if there is no active usecase running, so that the
2763 * next activity on soundwire will request clock from new clock
2764 * source.
2765 */
2766 mutex_lock(&swrm->mlock);
2767 if (swrm->state == SWR_MSTR_UP)
2768 swrm_device_suspend(&pdev->dev);
2769 mutex_unlock(&swrm->mlock);
2770 break;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302771 case SWR_CLK_FREQ:
2772 if (!data) {
2773 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
2774 ret = -EINVAL;
2775 } else {
2776 mutex_lock(&swrm->mlock);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302777 if (swrm->mclk_freq != *(int *)data) {
2778 dev_dbg(swrm->dev, "%s: freq change: force mstr down\n", __func__);
2779 if (swrm->state == SWR_MSTR_DOWN)
2780 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
2781 __func__, swrm->state);
2782 else
2783 swrm_device_suspend(&pdev->dev);
2784 }
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302785 swrm->mclk_freq = *(int *)data;
2786 mutex_unlock(&swrm->mlock);
2787 }
2788 break;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302789 case SWR_DEVICE_SSR_DOWN:
2790 mutex_lock(&swrm->devlock);
2791 swrm->dev_up = false;
2792 mutex_unlock(&swrm->devlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302793 mutex_lock(&swrm->reslock);
2794 swrm->state = SWR_MSTR_SSR;
2795 mutex_unlock(&swrm->reslock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302796 break;
2797 case SWR_DEVICE_SSR_UP:
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302798 /* wait for clk voting to be zero */
Ramprasad Katkam7f6462e2018-11-06 11:51:22 +05302799 reinit_completion(&swrm->clk_off_complete);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302800 if (swrm->clk_ref_count &&
2801 !wait_for_completion_timeout(&swrm->clk_off_complete,
Ramprasad Katkamc87efeb2018-12-12 19:26:19 +05302802 msecs_to_jiffies(500)))
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302803 dev_err(swrm->dev, "%s: clock voting not zero\n",
2804 __func__);
2805
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302806 mutex_lock(&swrm->devlock);
2807 swrm->dev_up = true;
2808 mutex_unlock(&swrm->devlock);
2809 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302810 case SWR_DEVICE_DOWN:
2811 dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
2812 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302813 if (swrm->state == SWR_MSTR_DOWN)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302814 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
2815 __func__, swrm->state);
2816 else
2817 swrm_device_down(&pdev->dev);
2818 mutex_unlock(&swrm->mlock);
2819 break;
2820 case SWR_DEVICE_UP:
2821 dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
Ramprasad Katkam0fed92f2018-11-08 14:22:22 +05302822 mutex_lock(&swrm->devlock);
2823 if (!swrm->dev_up) {
2824 dev_dbg(swrm->dev, "SSR not complete yet\n");
2825 mutex_unlock(&swrm->devlock);
2826 return -EBUSY;
2827 }
2828 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302829 mutex_lock(&swrm->mlock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302830 pm_runtime_mark_last_busy(&pdev->dev);
2831 pm_runtime_get_sync(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302832 mutex_lock(&swrm->reslock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302833 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2834 ret = swr_reset_device(swr_dev);
2835 if (ret) {
2836 dev_err(swrm->dev,
2837 "%s: failed to reset swr device %d\n",
2838 __func__, swr_dev->dev_num);
2839 swrm_clk_request(swrm, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302840 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302841 }
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302842 pm_runtime_mark_last_busy(&pdev->dev);
2843 pm_runtime_put_autosuspend(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302844 mutex_unlock(&swrm->reslock);
2845 mutex_unlock(&swrm->mlock);
2846 break;
2847 case SWR_SET_NUM_RX_CH:
2848 if (!data) {
2849 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
2850 ret = -EINVAL;
2851 } else {
2852 mutex_lock(&swrm->mlock);
2853 swrm->num_rx_chs = *(int *)data;
2854 if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
2855 list_for_each_entry(swr_dev, &mstr->devices,
2856 dev_list) {
2857 ret = swr_set_device_group(swr_dev,
2858 SWR_BROADCAST);
2859 if (ret)
2860 dev_err(swrm->dev,
2861 "%s: set num ch failed\n",
2862 __func__);
2863 }
2864 } else {
2865 list_for_each_entry(swr_dev, &mstr->devices,
2866 dev_list) {
2867 ret = swr_set_device_group(swr_dev,
2868 SWR_GROUP_NONE);
2869 if (ret)
2870 dev_err(swrm->dev,
2871 "%s: set num ch failed\n",
2872 __func__);
2873 }
2874 }
2875 mutex_unlock(&swrm->mlock);
2876 }
2877 break;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302878 case SWR_REGISTER_WAKE_IRQ:
2879 if (!data) {
2880 dev_err(swrm->dev, "%s: reg wake irq data is NULL\n",
2881 __func__);
2882 ret = -EINVAL;
2883 } else {
2884 mutex_lock(&swrm->mlock);
2885 swrm->ipc_wakeup = *(u32 *)data;
2886 ret = swrm_register_wake_irq(swrm);
2887 if (ret)
2888 dev_err(swrm->dev, "%s: register wake_irq failed\n",
2889 __func__);
2890 mutex_unlock(&swrm->mlock);
2891 }
2892 break;
Sudheer Papothi72ee2642019-08-08 05:15:17 +05302893 case SWR_REGISTER_WAKEUP:
2894 msm_aud_evt_blocking_notifier_call_chain(
2895 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
2896 break;
2897 case SWR_DEREGISTER_WAKEUP:
2898 msm_aud_evt_blocking_notifier_call_chain(
2899 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
2900 break;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302901 case SWR_SET_PORT_MAP:
2902 if (!data) {
2903 dev_err(swrm->dev, "%s: data is NULL for id=%d\n",
2904 __func__, id);
2905 ret = -EINVAL;
2906 } else {
2907 mutex_lock(&swrm->mlock);
2908 port_cfg = (struct swrm_port_config *)data;
2909 if (!port_cfg->size) {
2910 ret = -EINVAL;
2911 goto done;
2912 }
2913 ret = swrm_alloc_port_mem(&pdev->dev, swrm,
2914 port_cfg->uc, port_cfg->size);
2915 if (!ret)
2916 swrm_copy_port_config(swrm, port_cfg,
2917 port_cfg->size);
2918done:
2919 mutex_unlock(&swrm->mlock);
2920 }
2921 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302922 default:
2923 dev_err(swrm->dev, "%s: swr master unknown id %d\n",
2924 __func__, id);
2925 break;
2926 }
2927 return ret;
2928}
2929EXPORT_SYMBOL(swrm_wcd_notify);
2930
Ramprasad Katkam57349872018-11-11 18:34:57 +05302931/*
2932 * swrm_pm_cmpxchg:
2933 * Check old state and exchange with pm new state
2934 * if old state matches with current state
2935 *
2936 * @swrm: pointer to wcd core resource
2937 * @o: pm old state
2938 * @n: pm new state
2939 *
2940 * Returns old state
2941 */
2942static enum swrm_pm_state swrm_pm_cmpxchg(
2943 struct swr_mstr_ctrl *swrm,
2944 enum swrm_pm_state o,
2945 enum swrm_pm_state n)
2946{
2947 enum swrm_pm_state old;
2948
2949 if (!swrm)
2950 return o;
2951
2952 mutex_lock(&swrm->pm_lock);
2953 old = swrm->pm_state;
2954 if (old == o)
2955 swrm->pm_state = n;
2956 mutex_unlock(&swrm->pm_lock);
2957
2958 return old;
2959}
2960
2961static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm)
2962{
2963 enum swrm_pm_state os;
2964
2965 /*
2966 * swrm_{lock/unlock}_sleep will be called by swr irq handler
2967 * and slave wake up requests..
2968 *
2969 * If system didn't resume, we can simply return false so
2970 * IRQ handler can return without handling IRQ.
2971 */
2972 mutex_lock(&swrm->pm_lock);
2973 if (swrm->wlock_holders++ == 0) {
2974 dev_dbg(swrm->dev, "%s: holding wake lock\n", __func__);
2975 pm_qos_update_request(&swrm->pm_qos_req,
2976 msm_cpuidle_get_deep_idle_latency());
2977 pm_stay_awake(swrm->dev);
2978 }
2979 mutex_unlock(&swrm->pm_lock);
2980
2981 if (!wait_event_timeout(swrm->pm_wq,
2982 ((os = swrm_pm_cmpxchg(swrm,
2983 SWRM_PM_SLEEPABLE,
2984 SWRM_PM_AWAKE)) ==
2985 SWRM_PM_SLEEPABLE ||
2986 (os == SWRM_PM_AWAKE)),
2987 msecs_to_jiffies(
2988 SWRM_SYSTEM_RESUME_TIMEOUT_MS))) {
2989 dev_err(swrm->dev, "%s: system didn't resume within %dms, s %d, w %d\n",
2990 __func__, SWRM_SYSTEM_RESUME_TIMEOUT_MS, swrm->pm_state,
2991 swrm->wlock_holders);
2992 swrm_unlock_sleep(swrm);
2993 return false;
2994 }
2995 wake_up_all(&swrm->pm_wq);
2996 return true;
2997}
2998
2999static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm)
3000{
3001 mutex_lock(&swrm->pm_lock);
3002 if (--swrm->wlock_holders == 0) {
3003 dev_dbg(swrm->dev, "%s: releasing wake lock pm_state %d -> %d\n",
3004 __func__, swrm->pm_state, SWRM_PM_SLEEPABLE);
3005 /*
3006 * if swrm_lock_sleep failed, pm_state would be still
3007 * swrm_PM_ASLEEP, don't overwrite
3008 */
3009 if (likely(swrm->pm_state == SWRM_PM_AWAKE))
3010 swrm->pm_state = SWRM_PM_SLEEPABLE;
3011 pm_qos_update_request(&swrm->pm_qos_req,
3012 PM_QOS_DEFAULT_VALUE);
3013 pm_relax(swrm->dev);
3014 }
3015 mutex_unlock(&swrm->pm_lock);
3016 wake_up_all(&swrm->pm_wq);
3017}
3018
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303019#ifdef CONFIG_PM_SLEEP
3020static int swrm_suspend(struct device *dev)
3021{
3022 int ret = -EBUSY;
3023 struct platform_device *pdev = to_platform_device(dev);
3024 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3025
3026 dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
Ramprasad Katkam57349872018-11-11 18:34:57 +05303027
3028 mutex_lock(&swrm->pm_lock);
3029
3030 if (swrm->pm_state == SWRM_PM_SLEEPABLE) {
3031 dev_dbg(swrm->dev, "%s: suspending system, state %d, wlock %d\n",
3032 __func__, swrm->pm_state,
3033 swrm->wlock_holders);
3034 swrm->pm_state = SWRM_PM_ASLEEP;
3035 } else if (swrm->pm_state == SWRM_PM_AWAKE) {
3036 /*
3037 * unlock to wait for pm_state == SWRM_PM_SLEEPABLE
3038 * then set to SWRM_PM_ASLEEP
3039 */
3040 dev_dbg(swrm->dev, "%s: waiting to suspend system, state %d, wlock %d\n",
3041 __func__, swrm->pm_state,
3042 swrm->wlock_holders);
3043 mutex_unlock(&swrm->pm_lock);
3044 if (!(wait_event_timeout(swrm->pm_wq, swrm_pm_cmpxchg(
3045 swrm, SWRM_PM_SLEEPABLE,
3046 SWRM_PM_ASLEEP) ==
3047 SWRM_PM_SLEEPABLE,
3048 msecs_to_jiffies(
3049 SWRM_SYS_SUSPEND_WAIT)))) {
3050 dev_dbg(swrm->dev, "%s: suspend failed state %d, wlock %d\n",
3051 __func__, swrm->pm_state,
3052 swrm->wlock_holders);
3053 return -EBUSY;
3054 } else {
3055 dev_dbg(swrm->dev,
3056 "%s: done, state %d, wlock %d\n",
3057 __func__, swrm->pm_state,
3058 swrm->wlock_holders);
3059 }
3060 mutex_lock(&swrm->pm_lock);
3061 } else if (swrm->pm_state == SWRM_PM_ASLEEP) {
3062 dev_dbg(swrm->dev, "%s: system is already suspended, state %d, wlock %d\n",
3063 __func__, swrm->pm_state,
3064 swrm->wlock_holders);
3065 }
3066
3067 mutex_unlock(&swrm->pm_lock);
3068
3069 if ((!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev))) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303070 ret = swrm_runtime_suspend(dev);
3071 if (!ret) {
3072 /*
3073 * Synchronize runtime-pm and system-pm states:
3074 * At this point, we are already suspended. If
3075 * runtime-pm still thinks its active, then
3076 * make sure its status is in sync with HW
3077 * status. The three below calls let the
3078 * runtime-pm know that we are suspended
3079 * already without re-invoking the suspend
3080 * callback
3081 */
3082 pm_runtime_disable(dev);
3083 pm_runtime_set_suspended(dev);
3084 pm_runtime_enable(dev);
3085 }
3086 }
3087 if (ret == -EBUSY) {
3088 /*
3089 * There is a possibility that some audio stream is active
3090 * during suspend. We dont want to return suspend failure in
3091 * that case so that display and relevant components can still
3092 * go to suspend.
3093 * If there is some other error, then it should be passed-on
3094 * to system level suspend
3095 */
3096 ret = 0;
3097 }
3098 return ret;
3099}
3100
3101static int swrm_resume(struct device *dev)
3102{
3103 int ret = 0;
3104 struct platform_device *pdev = to_platform_device(dev);
3105 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3106
3107 dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
3108 if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
3109 ret = swrm_runtime_resume(dev);
3110 if (!ret) {
3111 pm_runtime_mark_last_busy(dev);
3112 pm_request_autosuspend(dev);
3113 }
3114 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05303115 mutex_lock(&swrm->pm_lock);
3116 if (swrm->pm_state == SWRM_PM_ASLEEP) {
3117 dev_dbg(swrm->dev,
3118 "%s: resuming system, state %d, wlock %d\n",
3119 __func__, swrm->pm_state,
3120 swrm->wlock_holders);
3121 swrm->pm_state = SWRM_PM_SLEEPABLE;
3122 } else {
3123 dev_dbg(swrm->dev, "%s: system is already awake, state %d wlock %d\n",
3124 __func__, swrm->pm_state,
3125 swrm->wlock_holders);
3126 }
3127 mutex_unlock(&swrm->pm_lock);
3128 wake_up_all(&swrm->pm_wq);
3129
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303130 return ret;
3131}
3132#endif /* CONFIG_PM_SLEEP */
3133
3134static const struct dev_pm_ops swrm_dev_pm_ops = {
3135 SET_SYSTEM_SLEEP_PM_OPS(
3136 swrm_suspend,
3137 swrm_resume
3138 )
3139 SET_RUNTIME_PM_OPS(
3140 swrm_runtime_suspend,
3141 swrm_runtime_resume,
3142 NULL
3143 )
3144};
3145
3146static const struct of_device_id swrm_dt_match[] = {
3147 {
3148 .compatible = "qcom,swr-mstr",
3149 },
3150 {}
3151};
3152
3153static struct platform_driver swr_mstr_driver = {
3154 .probe = swrm_probe,
3155 .remove = swrm_remove,
3156 .driver = {
3157 .name = SWR_WCD_NAME,
3158 .owner = THIS_MODULE,
3159 .pm = &swrm_dev_pm_ops,
3160 .of_match_table = swrm_dt_match,
Xiaojun Sang53cd13a2018-06-29 15:14:37 +08003161 .suppress_bind_attrs = true,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303162 },
3163};
3164
3165static int __init swrm_init(void)
3166{
3167 return platform_driver_register(&swr_mstr_driver);
3168}
3169module_init(swrm_init);
3170
3171static void __exit swrm_exit(void)
3172{
3173 platform_driver_unregister(&swr_mstr_driver);
3174}
3175module_exit(swrm_exit);
3176
3177MODULE_LICENSE("GPL v2");
3178MODULE_DESCRIPTION("SoundWire Master Controller");
3179MODULE_ALIAS("platform:swr-mstr");