blob: c4deb20014ea206536b7d734929789381b187bb6 [file] [log] [blame]
Meng Wang43bbb872018-12-10 12:32:05 +08001// SPDX-License-Identifier: GPL-2.0-only
Meng Wang61af6842018-09-10 17:47:55 +08002/*
Aditya Bavanari3517b112018-12-03 13:26:59 +05303 * Copyright (c) 2015-2019, The Linux Foundation. All rights reserved.
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05304 */
5
6#include <linux/irq.h>
7#include <linux/kernel.h>
8#include <linux/init.h>
9#include <linux/slab.h>
10#include <linux/io.h>
11#include <linux/interrupt.h>
12#include <linux/platform_device.h>
13#include <linux/delay.h>
14#include <linux/kthread.h>
Ramprasad Katkamcab8d722018-09-28 15:54:06 +053015#include <linux/bitops.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053016#include <linux/clk.h>
Laxminath Kasama60239e2019-01-10 14:43:03 +053017#include <linux/gpio.h>
18#include <linux/of_gpio.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053019#include <linux/pm_runtime.h>
20#include <linux/of.h>
21#include <linux/debugfs.h>
22#include <linux/uaccess.h>
23#include <soc/soundwire.h>
Sudheer Papothi3d1596e2018-10-27 06:19:18 +053024#include <soc/swr-common.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053025#include <linux/regmap.h>
Ramprasad Katkam68765ab2018-08-30 11:46:32 +053026#include <dsp/msm-audio-event-notify.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053027#include "swrm_registers.h"
28#include "swr-mstr-ctrl.h"
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053029
Ramprasad Katkam57349872018-11-11 18:34:57 +053030#define SWRM_SYSTEM_RESUME_TIMEOUT_MS 700
31#define SWRM_SYS_SUSPEND_WAIT 1
Sudheer Papothi3d1596e2018-10-27 06:19:18 +053032
Sudheer Papothi4c322b12018-10-31 06:34:01 +053033#define SWRM_DSD_PARAMS_PORT 4
34
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053035#define SWR_BROADCAST_CMD_ID 0x0F
Sudheer Papothi3590b312019-06-04 23:51:30 +053036#define SWR_AUTO_SUSPEND_DELAY 1 /* delay in sec */
Sudheer Papothi7c067e82018-11-15 06:53:35 +053037#define SWR_DEV_ID_MASK 0xFFFFFFFFFFFF
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053038#define SWR_REG_VAL_PACK(data, dev, id, reg) \
39 ((reg) | ((id) << 16) | ((dev) << 20) | ((data) << 24))
40
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +053041#define SWR_INVALID_PARAM 0xFF
Laxminath Kasam990c70b2018-11-09 23:15:09 +053042#define SWR_HSTOP_MAX_VAL 0xF
43#define SWR_HSTART_MIN_VAL 0x0
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +053044
Ramprasad Katkam83303512018-10-11 17:34:22 +053045#define SWRM_INTERRUPT_STATUS_MASK 0x1FDFD
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053046/* pm runtime auto suspend timer in msecs */
47static int auto_suspend_timer = SWR_AUTO_SUSPEND_DELAY * 1000;
48module_param(auto_suspend_timer, int, 0664);
49MODULE_PARM_DESC(auto_suspend_timer, "timer for auto suspend");
50
51enum {
52 SWR_NOT_PRESENT, /* Device is detached/not present on the bus */
53 SWR_ATTACHED_OK, /* Device is attached */
54 SWR_ALERT, /* Device alters master for any interrupts */
55 SWR_RESERVED, /* Reserved */
56};
57
58enum {
59 MASTER_ID_WSA = 1,
60 MASTER_ID_RX,
61 MASTER_ID_TX
62};
Ramprasad Katkamcab8d722018-09-28 15:54:06 +053063
64enum {
65 ENABLE_PENDING,
66 DISABLE_PENDING
67};
Sudheer Papothi384addd2019-06-14 02:26:52 +053068
69enum {
70 LPASS_HW_CORE,
71 LPASS_AUDIO_CORE,
72};
73
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053074#define TRUE 1
75#define FALSE 0
76
Ramprasad Katkam1f221262018-08-23 15:01:22 +053077#define SWRM_MAX_PORT_REG 120
Ramprasad Katkam83303512018-10-11 17:34:22 +053078#define SWRM_MAX_INIT_REG 11
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053079
80#define SWR_MSTR_MAX_REG_ADDR 0x1740
81#define SWR_MSTR_START_REG_ADDR 0x00
82#define SWR_MSTR_MAX_BUF_LEN 32
83#define BYTES_PER_LINE 12
84#define SWR_MSTR_RD_BUF_LEN 8
85#define SWR_MSTR_WR_BUF_LEN 32
86
Laxminath Kasamfbcaf322018-07-18 00:38:14 +053087#define MAX_FIFO_RD_FAIL_RETRY 3
88
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053089static struct swr_mstr_ctrl *dbgswrm;
90static struct dentry *debugfs_swrm_dent;
91static struct dentry *debugfs_peek;
92static struct dentry *debugfs_poke;
93static struct dentry *debugfs_reg_dump;
94static unsigned int read_data;
95
Ramprasad Katkam57349872018-11-11 18:34:57 +053096static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm);
97static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053098
99static bool swrm_is_msm_variant(int val)
100{
101 return (val == SWRM_VERSION_1_3);
102}
103
104static int swrm_debug_open(struct inode *inode, struct file *file)
105{
106 file->private_data = inode->i_private;
107 return 0;
108}
109
110static int get_parameters(char *buf, u32 *param1, int num_of_par)
111{
112 char *token;
113 int base, cnt;
114
115 token = strsep(&buf, " ");
116 for (cnt = 0; cnt < num_of_par; cnt++) {
117 if (token) {
118 if ((token[1] == 'x') || (token[1] == 'X'))
119 base = 16;
120 else
121 base = 10;
122
123 if (kstrtou32(token, base, &param1[cnt]) != 0)
124 return -EINVAL;
125
126 token = strsep(&buf, " ");
127 } else
128 return -EINVAL;
129 }
130 return 0;
131}
132
133static ssize_t swrm_reg_show(char __user *ubuf, size_t count,
134 loff_t *ppos)
135{
136 int i, reg_val, len;
137 ssize_t total = 0;
138 char tmp_buf[SWR_MSTR_MAX_BUF_LEN];
139
140 if (!ubuf || !ppos)
141 return 0;
142
143 for (i = (((int) *ppos / BYTES_PER_LINE) + SWR_MSTR_START_REG_ADDR);
144 i <= SWR_MSTR_MAX_REG_ADDR; i += 4) {
145 reg_val = dbgswrm->read(dbgswrm->handle, i);
146 len = snprintf(tmp_buf, 25, "0x%.3x: 0x%.2x\n", i, reg_val);
147 if ((total + len) >= count - 1)
148 break;
149 if (copy_to_user((ubuf + total), tmp_buf, len)) {
150 pr_err("%s: fail to copy reg dump\n", __func__);
151 total = -EFAULT;
152 goto copy_err;
153 }
154 *ppos += len;
155 total += len;
156 }
157
158copy_err:
159 return total;
160}
161
162static ssize_t swrm_debug_read(struct file *file, char __user *ubuf,
163 size_t count, loff_t *ppos)
164{
165 char lbuf[SWR_MSTR_RD_BUF_LEN];
166 char *access_str;
167 ssize_t ret_cnt;
168
169 if (!count || !file || !ppos || !ubuf)
170 return -EINVAL;
171
172 access_str = file->private_data;
173 if (*ppos < 0)
174 return -EINVAL;
175
176 if (!strcmp(access_str, "swrm_peek")) {
177 snprintf(lbuf, sizeof(lbuf), "0x%x\n", read_data);
178 ret_cnt = simple_read_from_buffer(ubuf, count, ppos, lbuf,
179 strnlen(lbuf, 7));
180 } else if (!strcmp(access_str, "swrm_reg_dump")) {
181 ret_cnt = swrm_reg_show(ubuf, count, ppos);
182 } else {
183 pr_err("%s: %s not permitted to read\n", __func__, access_str);
184 ret_cnt = -EPERM;
185 }
186 return ret_cnt;
187}
188
189static ssize_t swrm_debug_write(struct file *filp,
190 const char __user *ubuf, size_t cnt, loff_t *ppos)
191{
192 char lbuf[SWR_MSTR_WR_BUF_LEN];
193 int rc;
194 u32 param[5];
195 char *access_str;
196
197 if (!filp || !ppos || !ubuf)
198 return -EINVAL;
199
200 access_str = filp->private_data;
201 if (cnt > sizeof(lbuf) - 1)
202 return -EINVAL;
203
204 rc = copy_from_user(lbuf, ubuf, cnt);
205 if (rc)
206 return -EFAULT;
207
208 lbuf[cnt] = '\0';
209 if (!strcmp(access_str, "swrm_poke")) {
210 /* write */
211 rc = get_parameters(lbuf, param, 2);
212 if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) &&
213 (param[1] <= 0xFFFFFFFF) &&
214 (rc == 0))
215 rc = dbgswrm->write(dbgswrm->handle, param[0],
216 param[1]);
217 else
218 rc = -EINVAL;
219 } else if (!strcmp(access_str, "swrm_peek")) {
220 /* read */
221 rc = get_parameters(lbuf, param, 1);
222 if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) && (rc == 0))
223 read_data = dbgswrm->read(dbgswrm->handle, param[0]);
224 else
225 rc = -EINVAL;
226 }
227 if (rc == 0)
228 rc = cnt;
229 else
230 pr_err("%s: rc = %d\n", __func__, rc);
231
232 return rc;
233}
234
235static const struct file_operations swrm_debug_ops = {
236 .open = swrm_debug_open,
237 .write = swrm_debug_write,
238 .read = swrm_debug_read,
239};
240
Sudheer Papothi0016db12019-06-11 04:42:38 +0530241static void swrm_reg_dump(struct swr_mstr_ctrl *swrm,
242 u32 *reg, u32 *val, int len, const char* func)
243{
244 int i = 0;
245
246 for (i = 0; i < len; i++)
247 dev_dbg(swrm->dev, "%s: reg = 0x%x val = 0x%x\n",
248 func, reg[i], val[i]);
249}
250
Sudheer Papothi384addd2019-06-14 02:26:52 +0530251static int swrm_request_hw_vote(struct swr_mstr_ctrl *swrm,
252 int core_type, bool enable)
253{
254 int ret = 0;
255
256 if (core_type == LPASS_HW_CORE) {
257 if (swrm->lpass_core_hw_vote) {
258 if (enable) {
259 ret =
260 clk_prepare_enable(swrm->lpass_core_hw_vote);
261 if (ret < 0)
262 dev_err(swrm->dev,
263 "%s:lpass core hw enable failed\n",
264 __func__);
265 } else
266 clk_disable_unprepare(swrm->lpass_core_hw_vote);
267 }
268 }
269 if (core_type == LPASS_AUDIO_CORE) {
270 if (swrm->lpass_core_audio) {
271 if (enable) {
272 ret =
273 clk_prepare_enable(swrm->lpass_core_audio);
274 if (ret < 0)
275 dev_err(swrm->dev,
276 "%s:lpass audio hw enable failed\n",
277 __func__);
278 } else
279 clk_disable_unprepare(swrm->lpass_core_audio);
280 }
281 }
282
283 return ret;
284}
285
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530286static int swrm_clk_request(struct swr_mstr_ctrl *swrm, bool enable)
287{
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530288 int ret = 0;
289
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530290 if (!swrm->clk || !swrm->handle)
291 return -EINVAL;
292
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530293 mutex_lock(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530294 if (enable) {
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530295 if (!swrm->dev_up) {
296 ret = -ENODEV;
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530297 goto exit;
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530298 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530299 swrm->clk_ref_count++;
300 if (swrm->clk_ref_count == 1) {
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530301 ret = swrm->clk(swrm->handle, true);
302 if (ret) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +0530303 dev_err_ratelimited(swrm->dev,
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530304 "%s: clock enable req failed",
305 __func__);
306 --swrm->clk_ref_count;
307 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530308 }
309 } else if (--swrm->clk_ref_count == 0) {
310 swrm->clk(swrm->handle, false);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530311 complete(&swrm->clk_off_complete);
312 }
313 if (swrm->clk_ref_count < 0) {
Meng Wang8c60bb52019-06-19 15:49:06 +0800314 dev_err(swrm->dev, "%s: swrm clk count mismatch\n", __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530315 swrm->clk_ref_count = 0;
316 }
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530317
318exit:
319 mutex_unlock(&swrm->clklock);
320 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530321}
322
323static int swrm_ahb_write(struct swr_mstr_ctrl *swrm,
324 u16 reg, u32 *value)
325{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530326 u32 temp = (u32)(*value);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530327 int ret = 0;
328
329 mutex_lock(&swrm->devlock);
330 if (!swrm->dev_up)
331 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530332
333 ret = swrm_clk_request(swrm, TRUE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530334 if (ret) {
335 dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
336 __func__);
337 goto err;
338 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530339 iowrite32(temp, swrm->swrm_dig_base + reg);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530340 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530341err:
342 mutex_unlock(&swrm->devlock);
343 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530344}
345
346static int swrm_ahb_read(struct swr_mstr_ctrl *swrm,
347 u16 reg, u32 *value)
348{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530349 u32 temp = 0;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530350 int ret = 0;
351
352 mutex_lock(&swrm->devlock);
353 if (!swrm->dev_up)
354 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530355
356 ret = swrm_clk_request(swrm, TRUE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530357 if (ret) {
358 dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
359 __func__);
360 goto err;
361 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530362 temp = ioread32(swrm->swrm_dig_base + reg);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530363 *value = temp;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530364 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530365err:
366 mutex_unlock(&swrm->devlock);
367 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530368}
369
370static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr)
371{
372 u32 val = 0;
373
374 if (swrm->read)
375 val = swrm->read(swrm->handle, reg_addr);
376 else
377 swrm_ahb_read(swrm, reg_addr, &val);
378 return val;
379}
380
381static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val)
382{
383 if (swrm->write)
384 swrm->write(swrm->handle, reg_addr, val);
385 else
386 swrm_ahb_write(swrm, reg_addr, &val);
387}
388
389static int swr_master_bulk_write(struct swr_mstr_ctrl *swrm, u32 *reg_addr,
390 u32 *val, unsigned int length)
391{
392 int i = 0;
393
394 if (swrm->bulk_write)
395 swrm->bulk_write(swrm->handle, reg_addr, val, length);
396 else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530397 mutex_lock(&swrm->iolock);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530398 for (i = 0; i < length; i++) {
399 /* wait for FIFO WR command to complete to avoid overflow */
400 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530401 swr_master_write(swrm, reg_addr[i], val[i]);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530402 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530403 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530404 }
405 return 0;
406}
407
408static bool swrm_is_port_en(struct swr_master *mstr)
409{
410 return !!(mstr->num_port);
411}
412
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530413static void copy_port_tables(struct swr_mstr_ctrl *swrm,
414 struct port_params *params)
415{
416 u8 i;
417 struct port_params *config = params;
418
419 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
420 /* wsa uses single frame structure for all configurations */
421 if (!swrm->mport_cfg[i].port_en)
422 continue;
423 swrm->mport_cfg[i].sinterval = config[i].si;
424 swrm->mport_cfg[i].offset1 = config[i].off1;
425 swrm->mport_cfg[i].offset2 = config[i].off2;
426 swrm->mport_cfg[i].hstart = config[i].hstart;
427 swrm->mport_cfg[i].hstop = config[i].hstop;
428 swrm->mport_cfg[i].blk_pack_mode = config[i].bp_mode;
429 swrm->mport_cfg[i].blk_grp_count = config[i].bgp_ctrl;
430 swrm->mport_cfg[i].word_length = config[i].wd_len;
431 swrm->mport_cfg[i].lane_ctrl = config[i].lane_ctrl;
432 }
433}
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530434static int swrm_get_port_config(struct swr_mstr_ctrl *swrm)
435{
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530436 struct port_params *params;
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530437 u32 usecase = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530438
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530439 /* TODO - Send usecase information to avoid checking for master_id */
440 if (swrm->mport_cfg[SWRM_DSD_PARAMS_PORT].port_en &&
441 (swrm->master_id == MASTER_ID_RX))
442 usecase = 1;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530443
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530444 params = swrm->port_param[usecase];
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530445 copy_port_tables(swrm, params);
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530446
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530447 return 0;
448}
449
450static int swrm_get_master_port(struct swr_mstr_ctrl *swrm, u8 *mstr_port_id,
451 u8 *mstr_ch_mask, u8 mstr_prt_type,
452 u8 slv_port_id)
453{
454 int i, j;
455 *mstr_port_id = 0;
456
457 for (i = 1; i <= swrm->num_ports; i++) {
458 for (j = 0; j < SWR_MAX_CH_PER_PORT; j++) {
459 if (swrm->port_mapping[i][j].port_type == mstr_prt_type)
460 goto found;
461 }
462 }
463found:
464 if (i > swrm->num_ports || j == SWR_MAX_CH_PER_PORT) {
465 dev_err(swrm->dev, "%s: port type not supported by master\n",
466 __func__);
467 return -EINVAL;
468 }
469 /* id 0 corresponds to master port 1 */
470 *mstr_port_id = i - 1;
471 *mstr_ch_mask = swrm->port_mapping[i][j].ch_mask;
472
473 return 0;
474
475}
476
477static u32 swrm_get_packed_reg_val(u8 *cmd_id, u8 cmd_data,
478 u8 dev_addr, u16 reg_addr)
479{
480 u32 val;
481 u8 id = *cmd_id;
482
483 if (id != SWR_BROADCAST_CMD_ID) {
484 if (id < 14)
485 id += 1;
486 else
487 id = 0;
488 *cmd_id = id;
489 }
490 val = SWR_REG_VAL_PACK(cmd_data, dev_addr, id, reg_addr);
491
492 return val;
493}
494
495static int swrm_cmd_fifo_rd_cmd(struct swr_mstr_ctrl *swrm, int *cmd_data,
496 u8 dev_addr, u8 cmd_id, u16 reg_addr,
497 u32 len)
498{
499 u32 val;
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530500 u32 retry_attempt = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530501
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530502 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530503 val = swrm_get_packed_reg_val(&swrm->rcmd_id, len, dev_addr, reg_addr);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530504 if (swrm->read) {
505 /* skip delay if read is handled in platform driver */
506 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
507 } else {
508 /* wait for FIFO RD to complete to avoid overflow */
509 usleep_range(100, 105);
510 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
511 /* wait for FIFO RD CMD complete to avoid overflow */
512 usleep_range(250, 255);
513 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530514retry_read:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530515 *cmd_data = swr_master_read(swrm, SWRM_CMD_FIFO_RD_FIFO_ADDR);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530516 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, rcmd_id: 0x%x, \
517 dev_num: 0x%x, cmd_data: 0x%x\n", __func__, reg_addr,
518 cmd_id, swrm->rcmd_id, dev_addr, *cmd_data);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530519 if ((((*cmd_data) & 0xF00) >> 8) != swrm->rcmd_id) {
520 if (retry_attempt < MAX_FIFO_RD_FAIL_RETRY) {
521 /* wait 500 us before retry on fifo read failure */
522 usleep_range(500, 505);
523 retry_attempt++;
524 goto retry_read;
525 } else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530526 dev_err_ratelimited(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, \
527 rcmd_id: 0x%x, dev_num: 0x%x, cmd_data: 0x%x\n",
528 __func__, reg_addr, cmd_id, swrm->rcmd_id,
529 dev_addr, *cmd_data);
530
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530531 dev_err_ratelimited(swrm->dev,
532 "%s: failed to read fifo\n", __func__);
533 }
534 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530535 mutex_unlock(&swrm->iolock);
536
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530537 return 0;
538}
539
540static int swrm_cmd_fifo_wr_cmd(struct swr_mstr_ctrl *swrm, u8 cmd_data,
541 u8 dev_addr, u8 cmd_id, u16 reg_addr)
542{
543 u32 val;
544 int ret = 0;
545
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530546 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530547 if (!cmd_id)
548 val = swrm_get_packed_reg_val(&swrm->wcmd_id, cmd_data,
549 dev_addr, reg_addr);
550 else
551 val = swrm_get_packed_reg_val(&cmd_id, cmd_data,
552 dev_addr, reg_addr);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530553 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x,wcmd_id: 0x%x, \
554 dev_num: 0x%x, cmd_data: 0x%x\n", __func__,
555 reg_addr, cmd_id, swrm->wcmd_id,dev_addr, cmd_data);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +0530556 swr_master_write(swrm, SWRM_CMD_FIFO_WR_CMD, val);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530557 /*
558 * wait for FIFO WR command to complete to avoid overflow
559 * skip delay if write is handled in platform driver.
560 */
561 if(!swrm->write)
562 usleep_range(250, 255);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530563 if (cmd_id == 0xF) {
564 /*
565 * sleep for 10ms for MSM soundwire variant to allow broadcast
566 * command to complete.
567 */
568 if (swrm_is_msm_variant(swrm->version))
569 usleep_range(10000, 10100);
570 else
571 wait_for_completion_timeout(&swrm->broadcast,
572 (2 * HZ/10));
573 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530574 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530575 return ret;
576}
577
578static int swrm_read(struct swr_master *master, u8 dev_num, u16 reg_addr,
579 void *buf, u32 len)
580{
581 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
582 int ret = 0;
583 int val;
584 u8 *reg_val = (u8 *)buf;
585
586 if (!swrm) {
587 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
588 return -EINVAL;
589 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530590 if (!dev_num) {
591 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
592 return -EINVAL;
593 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530594 mutex_lock(&swrm->devlock);
595 if (!swrm->dev_up) {
596 mutex_unlock(&swrm->devlock);
597 return 0;
598 }
599 mutex_unlock(&swrm->devlock);
600
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530601 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530602 ret = swrm_cmd_fifo_rd_cmd(swrm, &val, dev_num, 0, reg_addr, len);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530603
604 if (!ret)
605 *reg_val = (u8)val;
606
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530607 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530608 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530609 return ret;
610}
611
612static int swrm_write(struct swr_master *master, u8 dev_num, u16 reg_addr,
613 const void *buf)
614{
615 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
616 int ret = 0;
617 u8 reg_val = *(u8 *)buf;
618
619 if (!swrm) {
620 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
621 return -EINVAL;
622 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530623 if (!dev_num) {
624 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
625 return -EINVAL;
626 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530627 mutex_lock(&swrm->devlock);
628 if (!swrm->dev_up) {
629 mutex_unlock(&swrm->devlock);
630 return 0;
631 }
632 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530633
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530634 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530635 ret = swrm_cmd_fifo_wr_cmd(swrm, reg_val, dev_num, 0, reg_addr);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530636
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530637 pm_runtime_put_autosuspend(swrm->dev);
638 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530639 return ret;
640}
641
642static int swrm_bulk_write(struct swr_master *master, u8 dev_num, void *reg,
643 const void *buf, size_t len)
644{
645 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
646 int ret = 0;
647 int i;
648 u32 *val;
649 u32 *swr_fifo_reg;
650
651 if (!swrm || !swrm->handle) {
652 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
653 return -EINVAL;
654 }
655 if (len <= 0)
656 return -EINVAL;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530657 mutex_lock(&swrm->devlock);
658 if (!swrm->dev_up) {
659 mutex_unlock(&swrm->devlock);
660 return 0;
661 }
662 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530663
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530664 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530665 if (dev_num) {
666 swr_fifo_reg = kcalloc(len, sizeof(u32), GFP_KERNEL);
667 if (!swr_fifo_reg) {
668 ret = -ENOMEM;
669 goto err;
670 }
671 val = kcalloc(len, sizeof(u32), GFP_KERNEL);
672 if (!val) {
673 ret = -ENOMEM;
674 goto mem_fail;
675 }
676
677 for (i = 0; i < len; i++) {
678 val[i] = swrm_get_packed_reg_val(&swrm->wcmd_id,
679 ((u8 *)buf)[i],
680 dev_num,
681 ((u16 *)reg)[i]);
682 swr_fifo_reg[i] = SWRM_CMD_FIFO_WR_CMD;
683 }
684 ret = swr_master_bulk_write(swrm, swr_fifo_reg, val, len);
685 if (ret) {
686 dev_err(&master->dev, "%s: bulk write failed\n",
687 __func__);
688 ret = -EINVAL;
689 }
690 } else {
691 dev_err(&master->dev,
692 "%s: No support of Bulk write for master regs\n",
693 __func__);
694 ret = -EINVAL;
695 goto err;
696 }
697 kfree(val);
698mem_fail:
699 kfree(swr_fifo_reg);
700err:
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530701 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530702 pm_runtime_mark_last_busy(swrm->dev);
703 return ret;
704}
705
706static u8 get_inactive_bank_num(struct swr_mstr_ctrl *swrm)
707{
708 return (swr_master_read(swrm, SWRM_MCP_STATUS) &
709 SWRM_MCP_STATUS_BANK_NUM_MASK) ? 0 : 1;
710}
711
712static void enable_bank_switch(struct swr_mstr_ctrl *swrm, u8 bank,
713 u8 row, u8 col)
714{
715 swrm_cmd_fifo_wr_cmd(swrm, ((row << 3) | col), 0xF, 0xF,
716 SWRS_SCP_FRAME_CTRL_BANK(bank));
717}
718
719static struct swr_port_info *swrm_get_port_req(struct swrm_mports *mport,
720 u8 slv_port, u8 dev_num)
721{
722 struct swr_port_info *port_req = NULL;
723
724 list_for_each_entry(port_req, &mport->port_req_list, list) {
725 /* Store dev_id instead of dev_num if enumeration is changed run_time */
726 if ((port_req->slave_port_id == slv_port)
727 && (port_req->dev_num == dev_num))
728 return port_req;
729 }
730 return NULL;
731}
732
733static bool swrm_remove_from_group(struct swr_master *master)
734{
735 struct swr_device *swr_dev;
736 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
737 bool is_removed = false;
738
739 if (!swrm)
740 goto end;
741
742 mutex_lock(&swrm->mlock);
743 if ((swrm->num_rx_chs > 1) &&
744 (swrm->num_rx_chs == swrm->num_cfg_devs)) {
745 list_for_each_entry(swr_dev, &master->devices,
746 dev_list) {
747 swr_dev->group_id = SWR_GROUP_NONE;
748 master->gr_sid = 0;
749 }
750 is_removed = true;
751 }
752 mutex_unlock(&swrm->mlock);
753
754end:
755 return is_removed;
756}
757
758static void swrm_disable_ports(struct swr_master *master,
759 u8 bank)
760{
761 u32 value;
762 struct swr_port_info *port_req;
763 int i;
764 struct swrm_mports *mport;
765 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
766
767 if (!swrm) {
768 pr_err("%s: swrm is null\n", __func__);
769 return;
770 }
771
772 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
773 master->num_port);
774
775
776 for (i = 0; i < SWR_MSTR_PORT_LEN ; i++) {
777
778 mport = &(swrm->mport_cfg[i]);
779 if (!mport->port_en)
780 continue;
781
782 list_for_each_entry(port_req, &mport->port_req_list, list) {
783 /* skip ports with no change req's*/
784 if (port_req->req_ch == port_req->ch_en)
785 continue;
786
787 swrm_cmd_fifo_wr_cmd(swrm, port_req->req_ch,
788 port_req->dev_num, 0x00,
789 SWRS_DP_CHANNEL_ENABLE_BANK(port_req->slave_port_id,
790 bank));
791 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x\n",
792 __func__, i,
793 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)));
794 }
795 value = ((mport->req_ch)
796 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
797 value |= ((mport->offset2)
798 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
799 value |= ((mport->offset1)
800 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
801 value |= mport->sinterval;
802
803 swr_master_write(swrm,
804 SWRM_DP_PORT_CTRL_BANK(i+1, bank),
805 value);
806 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
807 __func__, i,
808 (SWRM_DP_PORT_CTRL_BANK(i+1, bank)), value);
809 }
810}
811
812static void swrm_cleanup_disabled_port_reqs(struct swr_master *master)
813{
814 struct swr_port_info *port_req, *next;
815 int i;
816 struct swrm_mports *mport;
817 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
818
819 if (!swrm) {
820 pr_err("%s: swrm is null\n", __func__);
821 return;
822 }
823 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
824 master->num_port);
825
826 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
827 mport = &(swrm->mport_cfg[i]);
828 list_for_each_entry_safe(port_req, next,
829 &mport->port_req_list, list) {
830 /* skip ports without new ch req */
831 if (port_req->ch_en == port_req->req_ch)
832 continue;
833
834 /* remove new ch req's*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +0530835 port_req->ch_en = port_req->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530836
837 /* If no streams enabled on port, remove the port req */
838 if (port_req->ch_en == 0) {
839 list_del(&port_req->list);
840 kfree(port_req);
841 }
842 }
843 /* remove new ch req's on mport*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +0530844 mport->ch_en = mport->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530845
846 if (!(mport->ch_en)) {
847 mport->port_en = false;
848 master->port_en_mask &= ~i;
849 }
850 }
851}
852static void swrm_copy_data_port_config(struct swr_master *master, u8 bank)
853{
854 u32 value, slv_id;
855 struct swr_port_info *port_req;
856 int i;
857 struct swrm_mports *mport;
858 u32 reg[SWRM_MAX_PORT_REG];
859 u32 val[SWRM_MAX_PORT_REG];
860 int len = 0;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530861 u8 hparams;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530862 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
863
864 if (!swrm) {
865 pr_err("%s: swrm is null\n", __func__);
866 return;
867 }
868
869 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
870 master->num_port);
871
872 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
873 mport = &(swrm->mport_cfg[i]);
874 if (!mport->port_en)
875 continue;
876
877 list_for_each_entry(port_req, &mport->port_req_list, list) {
878 slv_id = port_req->slave_port_id;
879 reg[len] = SWRM_CMD_FIFO_WR_CMD;
880 val[len++] = SWR_REG_VAL_PACK(port_req->req_ch,
881 port_req->dev_num, 0x00,
882 SWRS_DP_CHANNEL_ENABLE_BANK(slv_id,
883 bank));
884
885 reg[len] = SWRM_CMD_FIFO_WR_CMD;
886 val[len++] = SWR_REG_VAL_PACK(mport->sinterval,
887 port_req->dev_num, 0x00,
888 SWRS_DP_SAMPLE_CONTROL_1_BANK(slv_id,
889 bank));
890
891 reg[len] = SWRM_CMD_FIFO_WR_CMD;
892 val[len++] = SWR_REG_VAL_PACK(mport->offset1,
893 port_req->dev_num, 0x00,
894 SWRS_DP_OFFSET_CONTROL_1_BANK(slv_id,
895 bank));
896
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530897 if (mport->offset2 != SWR_INVALID_PARAM) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530898 reg[len] = SWRM_CMD_FIFO_WR_CMD;
899 val[len++] = SWR_REG_VAL_PACK(mport->offset2,
900 port_req->dev_num, 0x00,
901 SWRS_DP_OFFSET_CONTROL_2_BANK(
902 slv_id, bank));
903 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530904 if (mport->hstart != SWR_INVALID_PARAM
905 && mport->hstop != SWR_INVALID_PARAM) {
906 hparams = (mport->hstart << 4) | mport->hstop;
907
908 reg[len] = SWRM_CMD_FIFO_WR_CMD;
909 val[len++] = SWR_REG_VAL_PACK(hparams,
910 port_req->dev_num, 0x00,
911 SWRS_DP_HCONTROL_BANK(slv_id,
912 bank));
913 }
914 if (mport->word_length != SWR_INVALID_PARAM) {
915 reg[len] = SWRM_CMD_FIFO_WR_CMD;
916 val[len++] =
917 SWR_REG_VAL_PACK(mport->word_length,
918 port_req->dev_num, 0x00,
919 SWRS_DP_BLOCK_CONTROL_1(slv_id));
920 }
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +0530921 if (mport->blk_pack_mode != SWR_INVALID_PARAM
922 && swrm->master_id != MASTER_ID_WSA) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530923 reg[len] = SWRM_CMD_FIFO_WR_CMD;
924 val[len++] =
925 SWR_REG_VAL_PACK(mport->blk_pack_mode,
926 port_req->dev_num, 0x00,
927 SWRS_DP_BLOCK_CONTROL_3_BANK(slv_id,
928 bank));
929 }
930 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
931 reg[len] = SWRM_CMD_FIFO_WR_CMD;
932 val[len++] =
933 SWR_REG_VAL_PACK(mport->blk_grp_count,
934 port_req->dev_num, 0x00,
935 SWRS_DP_BLOCK_CONTROL_2_BANK(slv_id,
936 bank));
937 }
938 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
939 reg[len] = SWRM_CMD_FIFO_WR_CMD;
940 val[len++] =
941 SWR_REG_VAL_PACK(mport->lane_ctrl,
942 port_req->dev_num, 0x00,
943 SWRS_DP_LANE_CONTROL_BANK(slv_id,
944 bank));
945 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530946 port_req->ch_en = port_req->req_ch;
947 }
948 value = ((mport->req_ch)
949 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +0530950
951 if (mport->offset2 != SWR_INVALID_PARAM)
952 value |= ((mport->offset2)
953 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530954 value |= ((mport->offset1)
955 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
956 value |= mport->sinterval;
957
958
959 reg[len] = SWRM_DP_PORT_CTRL_BANK(i + 1, bank);
960 val[len++] = value;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530961 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
962 __func__, i,
963 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)), value);
964
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530965 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
966 reg[len] = SWRM_DP_PORT_CTRL_2_BANK(i + 1, bank);
967 val[len++] = mport->lane_ctrl;
968 }
969 if (mport->word_length != SWR_INVALID_PARAM) {
970 reg[len] = SWRM_DP_BLOCK_CTRL_1(i + 1);
971 val[len++] = mport->word_length;
972 }
973
974 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
975 reg[len] = SWRM_DP_BLOCK_CTRL2_BANK(i + 1, bank);
976 val[len++] = mport->blk_grp_count;
977 }
978 if (mport->hstart != SWR_INVALID_PARAM
979 && mport->hstop != SWR_INVALID_PARAM) {
980 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
Laxminath Kasame30eef72018-11-05 17:40:09 +0530981 hparams = (mport->hstop << 4) | mport->hstart;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530982 val[len++] = hparams;
Laxminath Kasam990c70b2018-11-09 23:15:09 +0530983 } else {
984 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
985 hparams = (SWR_HSTOP_MAX_VAL << 4) | SWR_HSTART_MIN_VAL;
986 val[len++] = hparams;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530987 }
988 if (mport->blk_pack_mode != SWR_INVALID_PARAM) {
989 reg[len] = SWRM_DP_BLOCK_CTRL3_BANK(i + 1, bank);
990 val[len++] = mport->blk_pack_mode;
991 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530992 mport->ch_en = mport->req_ch;
993
994 }
Sudheer Papothi0016db12019-06-11 04:42:38 +0530995 swrm_reg_dump(swrm, reg, val, len, __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530996 swr_master_bulk_write(swrm, reg, val, len);
997}
998
999static void swrm_apply_port_config(struct swr_master *master)
1000{
1001 u8 bank;
1002 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1003
1004 if (!swrm) {
1005 pr_err("%s: Invalid handle to swr controller\n",
1006 __func__);
1007 return;
1008 }
1009
1010 bank = get_inactive_bank_num(swrm);
1011 dev_dbg(swrm->dev, "%s: enter bank: %d master_ports: %d\n",
1012 __func__, bank, master->num_port);
1013
1014
1015 swrm_cmd_fifo_wr_cmd(swrm, 0x01, 0xF, 0x00,
1016 SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(bank));
1017
1018 swrm_copy_data_port_config(master, bank);
1019}
1020
1021static int swrm_slvdev_datapath_control(struct swr_master *master, bool enable)
1022{
1023 u8 bank;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301024 u32 value, n_row, n_col;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301025 int ret;
1026 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1027 int mask = (SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK |
1028 SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK |
1029 SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_BMSK);
1030 u8 inactive_bank;
1031
1032 if (!swrm) {
1033 pr_err("%s: swrm is null\n", __func__);
1034 return -EFAULT;
1035 }
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301036
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301037 mutex_lock(&swrm->mlock);
1038
Ramprasad Katkam979b7c92019-05-17 15:31:21 +05301039 /*
1040 * During disable if master is already down, which implies an ssr/pdr
1041 * scenario, just mark ports as disabled and exit
1042 */
1043 if (swrm->state == SWR_MSTR_SSR && !enable) {
1044 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1045 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1046 __func__);
1047 goto exit;
1048 }
1049 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
1050 swrm_cleanup_disabled_port_reqs(master);
1051 if (!swrm_is_port_en(master)) {
1052 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1053 __func__);
1054 pm_runtime_mark_last_busy(swrm->dev);
1055 pm_runtime_put_autosuspend(swrm->dev);
1056 }
1057 goto exit;
1058 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301059 bank = get_inactive_bank_num(swrm);
1060
1061 if (enable) {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301062 if (!test_bit(ENABLE_PENDING, &swrm->port_req_pending)) {
1063 dev_dbg(swrm->dev, "%s:No pending connect port req\n",
1064 __func__);
1065 goto exit;
1066 }
1067 clear_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301068 ret = swrm_get_port_config(swrm);
1069 if (ret) {
1070 /* cannot accommodate ports */
1071 swrm_cleanup_disabled_port_reqs(master);
1072 mutex_unlock(&swrm->mlock);
1073 return -EINVAL;
1074 }
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301075 swr_master_write(swrm, SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301076 SWRM_INTERRUPT_STATUS_MASK);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301077 /* apply the new port config*/
1078 swrm_apply_port_config(master);
1079 } else {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301080 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1081 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1082 __func__);
1083 goto exit;
1084 }
1085 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301086 swrm_disable_ports(master, bank);
1087 }
1088 dev_dbg(swrm->dev, "%s: enable: %d, cfg_devs: %d\n",
1089 __func__, enable, swrm->num_cfg_devs);
1090
1091 if (enable) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301092 /* set col = 16 */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301093 n_col = SWR_MAX_COL;
1094 } else {
1095 /*
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301096 * Do not change to col = 2 if there are still active ports
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301097 */
1098 if (!master->num_port)
1099 n_col = SWR_MIN_COL;
1100 else
1101 n_col = SWR_MAX_COL;
1102 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301103 /* Use default 50 * x, frame shape. Change based on mclk */
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301104 if (swrm->mclk_freq == MCLK_FREQ_NATIVE) {
1105 dev_dbg(swrm->dev, "setting 64 x %d frameshape\n",
1106 n_col ? 16 : 2);
1107 n_row = SWR_ROW_64;
1108 } else {
1109 dev_dbg(swrm->dev, "setting 50 x %d frameshape\n",
1110 n_col ? 16 : 2);
1111 n_row = SWR_ROW_50;
1112 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301113 value = swr_master_read(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank));
1114 value &= (~mask);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301115 value |= ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301116 (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
1117 (0 << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
1118 swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1119
1120 dev_dbg(swrm->dev, "%s: regaddr: 0x%x, value: 0x%x\n", __func__,
1121 SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1122
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301123 enable_bank_switch(swrm, bank, n_row, n_col);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301124 inactive_bank = bank ? 0 : 1;
1125
1126 if (enable)
1127 swrm_copy_data_port_config(master, inactive_bank);
1128 else {
1129 swrm_disable_ports(master, inactive_bank);
1130 swrm_cleanup_disabled_port_reqs(master);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301131 }
1132 if (!swrm_is_port_en(master)) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301133 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1134 __func__);
1135 pm_runtime_mark_last_busy(swrm->dev);
1136 pm_runtime_put_autosuspend(swrm->dev);
1137 }
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301138exit:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301139 mutex_unlock(&swrm->mlock);
1140return 0;
1141}
1142
1143static int swrm_connect_port(struct swr_master *master,
1144 struct swr_params *portinfo)
1145{
1146 int i;
1147 struct swr_port_info *port_req;
1148 int ret = 0;
1149 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1150 struct swrm_mports *mport;
1151 u8 mstr_port_id, mstr_ch_msk;
1152
1153 dev_dbg(&master->dev, "%s: enter\n", __func__);
1154 if (!portinfo)
1155 return -EINVAL;
1156
1157 if (!swrm) {
1158 dev_err(&master->dev,
1159 "%s: Invalid handle to swr controller\n",
1160 __func__);
1161 return -EINVAL;
1162 }
1163
1164 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301165 mutex_lock(&swrm->devlock);
1166 if (!swrm->dev_up) {
1167 mutex_unlock(&swrm->devlock);
1168 mutex_unlock(&swrm->mlock);
1169 return -EINVAL;
1170 }
1171 mutex_unlock(&swrm->devlock);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301172 if (!swrm_is_port_en(master))
1173 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301174
1175 for (i = 0; i < portinfo->num_port; i++) {
1176 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_msk,
1177 portinfo->port_type[i],
1178 portinfo->port_id[i]);
1179 if (ret) {
1180 dev_err(&master->dev,
1181 "%s: mstr portid for slv port %d not found\n",
1182 __func__, portinfo->port_id[i]);
1183 goto port_fail;
1184 }
1185
1186 mport = &(swrm->mport_cfg[mstr_port_id]);
1187 /* get port req */
1188 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1189 portinfo->dev_num);
1190 if (!port_req) {
1191 dev_dbg(&master->dev, "%s: new req:port id %d dev %d\n",
1192 __func__, portinfo->port_id[i],
1193 portinfo->dev_num);
1194 port_req = kzalloc(sizeof(struct swr_port_info),
1195 GFP_KERNEL);
1196 if (!port_req) {
1197 ret = -ENOMEM;
1198 goto mem_fail;
1199 }
1200 port_req->dev_num = portinfo->dev_num;
1201 port_req->slave_port_id = portinfo->port_id[i];
1202 port_req->num_ch = portinfo->num_ch[i];
1203 port_req->ch_rate = portinfo->ch_rate[i];
1204 port_req->ch_en = 0;
1205 port_req->master_port_id = mstr_port_id;
1206 list_add(&port_req->list, &mport->port_req_list);
1207 }
1208 port_req->req_ch |= portinfo->ch_en[i];
1209
1210 dev_dbg(&master->dev,
1211 "%s: mstr port %d, slv port %d ch_rate %d num_ch %d\n",
1212 __func__, port_req->master_port_id,
1213 port_req->slave_port_id, port_req->ch_rate,
1214 port_req->num_ch);
1215 /* Put the port req on master port */
1216 mport = &(swrm->mport_cfg[mstr_port_id]);
1217 mport->port_en = true;
1218 mport->req_ch |= mstr_ch_msk;
1219 master->port_en_mask |= (1 << mstr_port_id);
1220 }
1221 master->num_port += portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301222 set_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301223 swr_port_response(master, portinfo->tid);
1224
1225 mutex_unlock(&swrm->mlock);
1226 return 0;
1227
1228port_fail:
1229mem_fail:
1230 /* cleanup port reqs in error condition */
1231 swrm_cleanup_disabled_port_reqs(master);
1232 mutex_unlock(&swrm->mlock);
1233 return ret;
1234}
1235
1236static int swrm_disconnect_port(struct swr_master *master,
1237 struct swr_params *portinfo)
1238{
1239 int i, ret = 0;
1240 struct swr_port_info *port_req;
1241 struct swrm_mports *mport;
1242 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1243 u8 mstr_port_id, mstr_ch_mask;
1244
1245 if (!swrm) {
1246 dev_err(&master->dev,
1247 "%s: Invalid handle to swr controller\n",
1248 __func__);
1249 return -EINVAL;
1250 }
1251
1252 if (!portinfo) {
1253 dev_err(&master->dev, "%s: portinfo is NULL\n", __func__);
1254 return -EINVAL;
1255 }
1256 mutex_lock(&swrm->mlock);
1257
1258 for (i = 0; i < portinfo->num_port; i++) {
1259
1260 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_mask,
1261 portinfo->port_type[i], portinfo->port_id[i]);
1262 if (ret) {
1263 dev_err(&master->dev,
1264 "%s: mstr portid for slv port %d not found\n",
1265 __func__, portinfo->port_id[i]);
1266 mutex_unlock(&swrm->mlock);
1267 return -EINVAL;
1268 }
1269 mport = &(swrm->mport_cfg[mstr_port_id]);
1270 /* get port req */
1271 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1272 portinfo->dev_num);
1273
1274 if (!port_req) {
1275 dev_err(&master->dev, "%s:port not enabled : port %d\n",
1276 __func__, portinfo->port_id[i]);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05301277 mutex_unlock(&swrm->mlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301278 return -EINVAL;
1279 }
1280 port_req->req_ch &= ~portinfo->ch_en[i];
1281 mport->req_ch &= ~mstr_ch_mask;
1282 }
1283 master->num_port -= portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301284 set_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301285 swr_port_response(master, portinfo->tid);
1286 mutex_unlock(&swrm->mlock);
1287
1288 return 0;
1289}
1290
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301291static int swrm_find_alert_slave(struct swr_mstr_ctrl *swrm,
1292 int status, u8 *devnum)
1293{
1294 int i;
1295 bool found = false;
1296
1297 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1298 if ((status & SWRM_MCP_SLV_STATUS_MASK) == SWR_ALERT) {
1299 *devnum = i;
1300 found = true;
1301 break;
1302 }
1303 status >>= 2;
1304 }
1305 if (found)
1306 return 0;
1307 else
1308 return -EINVAL;
1309}
1310
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301311static void swrm_enable_slave_irq(struct swr_mstr_ctrl *swrm)
1312{
1313 int i;
1314 int status = 0;
1315
1316 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1317 if (!status) {
1318 dev_dbg_ratelimited(swrm->dev, "%s: slaves status is 0x%x\n",
1319 __func__, status);
1320 return;
1321 }
1322 dev_dbg(swrm->dev, "%s: slave status: 0x%x\n", __func__, status);
1323 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1324 if (status & SWRM_MCP_SLV_STATUS_MASK)
1325 swrm_cmd_fifo_wr_cmd(swrm, 0x4, i, 0x0,
1326 SWRS_SCP_INT_STATUS_MASK_1);
1327 status >>= 2;
1328 }
1329}
1330
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301331static int swrm_check_slave_change_status(struct swr_mstr_ctrl *swrm,
1332 int status, u8 *devnum)
1333{
1334 int i;
1335 int new_sts = status;
1336 int ret = SWR_NOT_PRESENT;
1337
1338 if (status != swrm->slave_status) {
1339 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1340 if ((status & SWRM_MCP_SLV_STATUS_MASK) !=
1341 (swrm->slave_status & SWRM_MCP_SLV_STATUS_MASK)) {
1342 ret = (status & SWRM_MCP_SLV_STATUS_MASK);
1343 *devnum = i;
1344 break;
1345 }
1346 status >>= 2;
1347 swrm->slave_status >>= 2;
1348 }
1349 swrm->slave_status = new_sts;
1350 }
1351 return ret;
1352}
1353
1354static irqreturn_t swr_mstr_interrupt(int irq, void *dev)
1355{
1356 struct swr_mstr_ctrl *swrm = dev;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301357 u32 value, intr_sts, intr_sts_masked;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301358 u32 temp = 0;
1359 u32 status, chg_sts, i;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301360 u8 devnum = 0;
1361 int ret = IRQ_HANDLED;
1362 struct swr_device *swr_dev;
1363 struct swr_master *mstr = &swrm->master;
1364
Ramprasad Katkam57349872018-11-11 18:34:57 +05301365 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1366 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1367 return IRQ_NONE;
1368 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301369
1370 mutex_lock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301371 if (swrm_clk_request(swrm, true)) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +05301372 dev_err_ratelimited(swrm->dev, "%s:clk request failed\n",
1373 __func__);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301374 mutex_unlock(&swrm->reslock);
1375 goto exit;
1376 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301377 mutex_unlock(&swrm->reslock);
1378
1379 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301380 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301381handle_irq:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301382 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301383 value = intr_sts_masked & (1 << i);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301384 if (!value)
1385 continue;
1386
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301387 switch (value) {
1388 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1389 dev_dbg(swrm->dev, "Trigger irq to slave device\n");
1390 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301391 ret = swrm_find_alert_slave(swrm, status, &devnum);
1392 if (ret) {
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301393 dev_err_ratelimited(swrm->dev,
1394 "no slave alert found.spurious interrupt\n");
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05301395 break;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301396 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301397 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1398 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1399 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1400 SWRS_SCP_INT_STATUS_CLEAR_1);
1401 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1402 SWRS_SCP_INT_STATUS_CLEAR_1);
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301403
1404
1405 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1406 if (swr_dev->dev_num != devnum)
1407 continue;
1408 if (swr_dev->slave_irq) {
1409 do {
1410 handle_nested_irq(
1411 irq_find_mapping(
1412 swr_dev->slave_irq, 0));
1413 } while (swr_dev->slave_irq_pending);
1414 }
1415
1416 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301417 break;
1418 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1419 dev_dbg(swrm->dev, "SWR new slave attached\n");
1420 break;
1421 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1422 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1423 if (status == swrm->slave_status) {
1424 dev_dbg(swrm->dev,
1425 "%s: No change in slave status: %d\n",
1426 __func__, status);
1427 break;
1428 }
1429 chg_sts = swrm_check_slave_change_status(swrm, status,
1430 &devnum);
1431 switch (chg_sts) {
1432 case SWR_NOT_PRESENT:
1433 dev_dbg(swrm->dev, "device %d got detached\n",
1434 devnum);
1435 break;
1436 case SWR_ATTACHED_OK:
1437 dev_dbg(swrm->dev, "device %d got attached\n",
1438 devnum);
Ramprasad Katkamdebe8932018-09-25 18:08:18 +05301439 /* enable host irq from slave device*/
1440 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1441 SWRS_SCP_INT_STATUS_CLEAR_1);
1442 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1443 SWRS_SCP_INT_STATUS_MASK_1);
1444
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301445 break;
1446 case SWR_ALERT:
1447 dev_dbg(swrm->dev,
1448 "device %d has pending interrupt\n",
1449 devnum);
1450 break;
1451 }
1452 break;
1453 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1454 dev_err_ratelimited(swrm->dev,
1455 "SWR bus clsh detected\n");
1456 break;
1457 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1458 dev_dbg(swrm->dev, "SWR read FIFO overflow\n");
1459 break;
1460 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1461 dev_dbg(swrm->dev, "SWR read FIFO underflow\n");
1462 break;
1463 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1464 dev_dbg(swrm->dev, "SWR write FIFO overflow\n");
1465 break;
1466 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1467 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1468 dev_err_ratelimited(swrm->dev,
1469 "SWR CMD error, fifo status 0x%x, flushing fifo\n",
1470 value);
1471 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1472 break;
1473 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301474 dev_err_ratelimited(swrm->dev, "SWR Port collision detected\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301475 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301476 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301477 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301478 break;
1479 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1480 dev_dbg(swrm->dev, "SWR read enable valid mismatch\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301481 swrm->intr_mask &=
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301482 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1483 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301484 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301485 break;
1486 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1487 complete(&swrm->broadcast);
1488 dev_dbg(swrm->dev, "SWR cmd id finished\n");
1489 break;
1490 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_AUTO_ENUM_FINISHED:
1491 break;
1492 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED:
1493 break;
1494 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL:
1495 break;
1496 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED:
1497 complete(&swrm->reset);
1498 break;
1499 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED:
1500 break;
1501 default:
1502 dev_err_ratelimited(swrm->dev,
1503 "SWR unknown interrupt\n");
1504 ret = IRQ_NONE;
1505 break;
1506 }
1507 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301508 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1509 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
Ramprasad Katkam83303512018-10-11 17:34:22 +05301510
1511 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301512 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301513
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301514 if (intr_sts_masked) {
Ramprasad Katkam83303512018-10-11 17:34:22 +05301515 dev_dbg(swrm->dev, "%s: new interrupt received\n", __func__);
1516 goto handle_irq;
1517 }
1518
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301519 mutex_lock(&swrm->reslock);
1520 swrm_clk_request(swrm, false);
1521 mutex_unlock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301522exit:
Ramprasad Katkam57349872018-11-11 18:34:57 +05301523 swrm_unlock_sleep(swrm);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301524 return ret;
1525}
1526
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301527static irqreturn_t swr_mstr_interrupt_v2(int irq, void *dev)
1528{
1529 struct swr_mstr_ctrl *swrm = dev;
1530 u32 value, intr_sts, intr_sts_masked;
1531 u32 temp = 0;
1532 u32 status, chg_sts, i;
1533 u8 devnum = 0;
1534 int ret = IRQ_HANDLED;
1535 struct swr_device *swr_dev;
1536 struct swr_master *mstr = &swrm->master;
1537
1538 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1539 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1540 return IRQ_NONE;
1541 }
1542
1543 mutex_lock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301544 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
1545 ret = IRQ_NONE;
1546 goto exit;
1547 }
1548 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
1549 ret = IRQ_NONE;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301550 goto err_audio_hw_vote;
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001551 }
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301552 swrm_clk_request(swrm, true);
1553 mutex_unlock(&swrm->reslock);
1554
1555 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1556 intr_sts_masked = intr_sts & swrm->intr_mask;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301557
1558 dev_dbg(swrm->dev, "%s: status: 0x%x \n", __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301559handle_irq:
1560 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
1561 value = intr_sts_masked & (1 << i);
1562 if (!value)
1563 continue;
1564
1565 switch (value) {
1566 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1567 dev_dbg(swrm->dev, "%s: Trigger irq to slave device\n",
1568 __func__);
1569 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1570 ret = swrm_find_alert_slave(swrm, status, &devnum);
1571 if (ret) {
1572 dev_err_ratelimited(swrm->dev,
1573 "%s: no slave alert found.spurious interrupt\n",
1574 __func__);
1575 break;
1576 }
1577 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1578 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1579 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1580 SWRS_SCP_INT_STATUS_CLEAR_1);
1581 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1582 SWRS_SCP_INT_STATUS_CLEAR_1);
1583
1584
1585 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1586 if (swr_dev->dev_num != devnum)
1587 continue;
1588 if (swr_dev->slave_irq) {
1589 do {
1590 handle_nested_irq(
1591 irq_find_mapping(
1592 swr_dev->slave_irq, 0));
1593 } while (swr_dev->slave_irq_pending);
1594 }
1595
1596 }
1597 break;
1598 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1599 dev_dbg(swrm->dev, "%s: SWR new slave attached\n",
1600 __func__);
1601 break;
1602 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1603 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1604 if (status == swrm->slave_status) {
1605 dev_dbg(swrm->dev,
1606 "%s: No change in slave status: %d\n",
1607 __func__, status);
1608 break;
1609 }
1610 chg_sts = swrm_check_slave_change_status(swrm, status,
1611 &devnum);
1612 switch (chg_sts) {
1613 case SWR_NOT_PRESENT:
1614 dev_dbg(swrm->dev,
1615 "%s: device %d got detached\n",
1616 __func__, devnum);
1617 break;
1618 case SWR_ATTACHED_OK:
1619 dev_dbg(swrm->dev,
1620 "%s: device %d got attached\n",
1621 __func__, devnum);
1622 /* enable host irq from slave device*/
1623 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1624 SWRS_SCP_INT_STATUS_CLEAR_1);
1625 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1626 SWRS_SCP_INT_STATUS_MASK_1);
1627
1628 break;
1629 case SWR_ALERT:
1630 dev_dbg(swrm->dev,
1631 "%s: device %d has pending interrupt\n",
1632 __func__, devnum);
1633 break;
1634 }
1635 break;
1636 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1637 dev_err_ratelimited(swrm->dev,
1638 "%s: SWR bus clsh detected\n",
1639 __func__);
1640 break;
1641 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1642 dev_dbg(swrm->dev, "%s: SWR read FIFO overflow\n",
1643 __func__);
1644 break;
1645 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1646 dev_dbg(swrm->dev, "%s: SWR read FIFO underflow\n",
1647 __func__);
1648 break;
1649 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1650 dev_dbg(swrm->dev, "%s: SWR write FIFO overflow\n",
1651 __func__);
1652 break;
1653 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1654 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1655 dev_err_ratelimited(swrm->dev,
1656 "%s: SWR CMD error, fifo status 0x%x, flushing fifo\n",
1657 __func__, value);
1658 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1659 break;
1660 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
1661 dev_err_ratelimited(swrm->dev,
1662 "%s: SWR Port collision detected\n",
1663 __func__);
1664 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
1665 swr_master_write(swrm,
1666 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1667 break;
1668 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1669 dev_dbg(swrm->dev,
1670 "%s: SWR read enable valid mismatch\n",
1671 __func__);
1672 swrm->intr_mask &=
1673 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1674 swr_master_write(swrm,
1675 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1676 break;
1677 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1678 complete(&swrm->broadcast);
1679 dev_dbg(swrm->dev, "%s: SWR cmd id finished\n",
1680 __func__);
1681 break;
1682 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED_V2:
1683 break;
1684 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL_V2:
1685 break;
1686 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED_V2:
1687 break;
1688 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED_V2:
1689 break;
1690 case SWRM_INTERRUPT_STATUS_EXT_CLK_STOP_WAKEUP:
1691 if (swrm->state == SWR_MSTR_UP)
1692 dev_dbg(swrm->dev,
1693 "%s:SWR Master is already up\n",
1694 __func__);
1695 else
1696 dev_err_ratelimited(swrm->dev,
1697 "%s: SWR wokeup during clock stop\n",
1698 __func__);
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301699 /* It might be possible the slave device gets reset
1700 * and slave interrupt gets missed. So re-enable
1701 * Host IRQ and process slave pending
1702 * interrupts, if any.
1703 */
1704 swrm_enable_slave_irq(swrm);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301705 break;
1706 default:
1707 dev_err_ratelimited(swrm->dev,
1708 "%s: SWR unknown interrupt value: %d\n",
1709 __func__, value);
1710 ret = IRQ_NONE;
1711 break;
1712 }
1713 }
1714 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1715 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
1716
1717 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1718 intr_sts_masked = intr_sts & swrm->intr_mask;
1719
1720 if (intr_sts_masked) {
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301721 dev_dbg(swrm->dev, "%s: new interrupt received 0x%x\n",
1722 __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301723 goto handle_irq;
1724 }
1725
1726 mutex_lock(&swrm->reslock);
1727 swrm_clk_request(swrm, false);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301728 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
Sudheer Papothi06f43412019-07-09 03:32:54 +05301729
1730err_audio_hw_vote:
Sudheer Papothi384addd2019-06-14 02:26:52 +05301731 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001732exit:
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301733 mutex_unlock(&swrm->reslock);
1734 swrm_unlock_sleep(swrm);
1735 return ret;
1736}
1737
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301738static irqreturn_t swrm_wakeup_interrupt(int irq, void *dev)
1739{
1740 struct swr_mstr_ctrl *swrm = dev;
1741 int ret = IRQ_HANDLED;
1742
1743 if (!swrm || !(swrm->dev)) {
1744 pr_err("%s: swrm or dev is null\n", __func__);
1745 return IRQ_NONE;
1746 }
1747 mutex_lock(&swrm->devlock);
1748 if (!swrm->dev_up) {
1749 if (swrm->wake_irq > 0)
1750 disable_irq_nosync(swrm->wake_irq);
1751 mutex_unlock(&swrm->devlock);
1752 return ret;
1753 }
1754 mutex_unlock(&swrm->devlock);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301755 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1756 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1757 goto exit;
1758 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301759 if (swrm->wake_irq > 0)
1760 disable_irq_nosync(swrm->wake_irq);
1761 pm_runtime_get_sync(swrm->dev);
1762 pm_runtime_mark_last_busy(swrm->dev);
1763 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301764 swrm_unlock_sleep(swrm);
1765exit:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301766 return ret;
1767}
1768
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301769static void swrm_wakeup_work(struct work_struct *work)
1770{
1771 struct swr_mstr_ctrl *swrm;
1772
1773 swrm = container_of(work, struct swr_mstr_ctrl,
1774 wakeup_work);
1775 if (!swrm || !(swrm->dev)) {
1776 pr_err("%s: swrm or dev is null\n", __func__);
1777 return;
1778 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301779
1780 mutex_lock(&swrm->devlock);
1781 if (!swrm->dev_up) {
1782 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301783 goto exit;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301784 }
1785 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301786 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1787 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1788 goto exit;
1789 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301790 pm_runtime_get_sync(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301791 pm_runtime_mark_last_busy(swrm->dev);
1792 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301793 swrm_unlock_sleep(swrm);
1794exit:
1795 pm_relax(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301796}
1797
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301798static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
1799{
1800 u32 val;
1801
1802 swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1803 val = (swrm->slave_status >> (devnum * 2));
1804 val &= SWRM_MCP_SLV_STATUS_MASK;
1805 return val;
1806}
1807
1808static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
1809 u8 *dev_num)
1810{
1811 int i;
1812 u64 id = 0;
1813 int ret = -EINVAL;
1814 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1815 struct swr_device *swr_dev;
1816 u32 num_dev = 0;
1817
1818 if (!swrm) {
1819 pr_err("%s: Invalid handle to swr controller\n",
1820 __func__);
1821 return ret;
1822 }
1823 if (swrm->num_dev)
1824 num_dev = swrm->num_dev;
1825 else
1826 num_dev = mstr->num_dev;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301827
1828 mutex_lock(&swrm->devlock);
1829 if (!swrm->dev_up) {
1830 mutex_unlock(&swrm->devlock);
1831 return ret;
1832 }
1833 mutex_unlock(&swrm->devlock);
1834
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301835 pm_runtime_get_sync(swrm->dev);
1836 for (i = 1; i < (num_dev + 1); i++) {
1837 id = ((u64)(swr_master_read(swrm,
1838 SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
1839 id |= swr_master_read(swrm,
1840 SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301841
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301842 /*
1843 * As pm_runtime_get_sync() brings all slaves out of reset
1844 * update logical device number for all slaves.
1845 */
1846 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1847 if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
1848 u32 status = swrm_get_device_status(swrm, i);
1849
1850 if ((status == 0x01) || (status == 0x02)) {
1851 swr_dev->dev_num = i;
1852 if ((id & SWR_DEV_ID_MASK) == dev_id) {
1853 *dev_num = i;
1854 ret = 0;
1855 }
1856 dev_dbg(swrm->dev,
1857 "%s: devnum %d is assigned for dev addr %lx\n",
1858 __func__, i, swr_dev->addr);
1859 }
1860 }
1861 }
1862 }
1863 if (ret)
1864 dev_err(swrm->dev, "%s: device 0x%llx is not ready\n",
1865 __func__, dev_id);
1866
1867 pm_runtime_mark_last_busy(swrm->dev);
1868 pm_runtime_put_autosuspend(swrm->dev);
1869 return ret;
1870}
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301871
1872static void swrm_device_wakeup_vote(struct swr_master *mstr)
1873{
1874 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1875
1876 if (!swrm) {
1877 pr_err("%s: Invalid handle to swr controller\n",
1878 __func__);
1879 return;
1880 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05301881 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1882 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1883 return;
1884 }
Sudheer Papothi384addd2019-06-14 02:26:52 +05301885 if (++swrm->hw_core_clk_en == 1)
1886 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
1887 dev_err(swrm->dev, "%s:lpass core hw enable failed\n",
1888 __func__);
1889 --swrm->hw_core_clk_en;
1890 }
1891 if ( ++swrm->aud_core_clk_en == 1)
1892 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
1893 dev_err(swrm->dev, "%s:lpass audio hw enable failed\n",
1894 __func__);
1895 --swrm->aud_core_clk_en;
1896 }
1897 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
1898 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301899 pm_runtime_get_sync(swrm->dev);
1900}
1901
1902static void swrm_device_wakeup_unvote(struct swr_master *mstr)
1903{
1904 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1905
1906 if (!swrm) {
1907 pr_err("%s: Invalid handle to swr controller\n",
1908 __func__);
1909 return;
1910 }
1911 pm_runtime_mark_last_busy(swrm->dev);
1912 pm_runtime_put_autosuspend(swrm->dev);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301913 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
1914 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
1915
1916 --swrm->aud_core_clk_en;
1917 if (swrm->aud_core_clk_en < 0)
1918 swrm->aud_core_clk_en = 0;
1919 else if (swrm->aud_core_clk_en == 0)
1920 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
1921
1922 --swrm->hw_core_clk_en;
1923 if (swrm->hw_core_clk_en < 0)
1924 swrm->hw_core_clk_en = 0;
1925 else if (swrm->hw_core_clk_en == 0)
1926 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
1927
Ramprasad Katkam57349872018-11-11 18:34:57 +05301928 swrm_unlock_sleep(swrm);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301929}
1930
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301931static int swrm_master_init(struct swr_mstr_ctrl *swrm)
1932{
1933 int ret = 0;
1934 u32 val;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301935 u8 row_ctrl = SWR_ROW_50;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301936 u8 col_ctrl = SWR_MIN_COL;
1937 u8 ssp_period = 1;
1938 u8 retry_cmd_num = 3;
1939 u32 reg[SWRM_MAX_INIT_REG];
1940 u32 value[SWRM_MAX_INIT_REG];
1941 int len = 0;
1942
1943 /* Clear Rows and Cols */
1944 val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
1945 (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
1946 (ssp_period << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
1947
1948 reg[len] = SWRM_MCP_FRAME_CTRL_BANK_ADDR(0);
1949 value[len++] = val;
1950
1951 /* Set Auto enumeration flag */
1952 reg[len] = SWRM_ENUMERATOR_CFG_ADDR;
1953 value[len++] = 1;
1954
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301955 /* Configure No pings */
1956 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
1957 val &= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK;
1958 val |= (0x1f << SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT);
1959 reg[len] = SWRM_MCP_CFG_ADDR;
1960 value[len++] = val;
1961
1962 /* Configure number of retries of a read/write cmd */
1963 val = (retry_cmd_num << SWRM_CMD_FIFO_CFG_NUM_OF_CMD_RETRY_SHFT);
1964 reg[len] = SWRM_CMD_FIFO_CFG_ADDR;
1965 value[len++] = val;
1966
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301967 reg[len] = SWRM_MCP_BUS_CTRL_ADDR;
1968 value[len++] = 0x2;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301969
Ramprasad Katkam83303512018-10-11 17:34:22 +05301970 /* Set IRQ to PULSE */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301971 reg[len] = SWRM_COMP_CFG_ADDR;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301972 value[len++] = 0x02;
1973
1974 reg[len] = SWRM_COMP_CFG_ADDR;
1975 value[len++] = 0x03;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301976
1977 reg[len] = SWRM_INTERRUPT_CLEAR;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301978 value[len++] = 0xFFFFFFFF;
1979
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301980 swrm->intr_mask = SWRM_INTERRUPT_STATUS_MASK;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301981 /* Mask soundwire interrupts */
1982 reg[len] = SWRM_INTERRUPT_MASK_ADDR;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301983 value[len++] = swrm->intr_mask;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301984
1985 reg[len] = SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301986 value[len++] = swrm->intr_mask;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301987
1988 swr_master_bulk_write(swrm, reg, value, len);
1989
Sudheer Papothi63f48152018-11-15 01:08:03 +05301990 /*
1991 * For SWR master version 1.5.1, continue
1992 * execute on command ignore.
1993 */
1994 if (swrm->version == SWRM_VERSION_1_5_1)
1995 swr_master_write(swrm, SWRM_CMD_FIFO_CFG_ADDR,
1996 (swr_master_read(swrm,
1997 SWRM_CMD_FIFO_CFG_ADDR) | 0x80000000));
1998
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301999 return ret;
2000}
2001
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302002static int swrm_event_notify(struct notifier_block *self,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302003 unsigned long action, void *data)
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302004{
2005 struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302006 event_notifier);
2007
2008 if (!swrm || !(swrm->dev)) {
2009 pr_err("%s: swrm or dev is NULL\n", __func__);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302010 return -EINVAL;
2011 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302012 switch (action) {
2013 case MSM_AUD_DC_EVENT:
2014 schedule_work(&(swrm->dc_presence_work));
2015 break;
2016 case SWR_WAKE_IRQ_EVENT:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302017 if (swrm->ipc_wakeup && !swrm->ipc_wakeup_triggered) {
2018 swrm->ipc_wakeup_triggered = true;
Ramprasad Katkam57349872018-11-11 18:34:57 +05302019 pm_stay_awake(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302020 schedule_work(&swrm->wakeup_work);
Ramprasad Katkamcd61c6e2018-09-18 13:22:58 +05302021 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302022 break;
2023 default:
2024 dev_err(swrm->dev, "%s: invalid event type: %lu\n",
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302025 __func__, action);
2026 return -EINVAL;
2027 }
2028
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302029 return 0;
2030}
2031
2032static void swrm_notify_work_fn(struct work_struct *work)
2033{
2034 struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302035 dc_presence_work);
2036
2037 if (!swrm || !swrm->pdev) {
2038 pr_err("%s: swrm or pdev is NULL\n", __func__);
2039 return;
2040 }
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302041 swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL);
2042}
2043
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302044static int swrm_probe(struct platform_device *pdev)
2045{
2046 struct swr_mstr_ctrl *swrm;
2047 struct swr_ctrl_platform_data *pdata;
2048 u32 i, num_ports, port_num, port_type, ch_mask;
2049 u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0;
2050 int ret = 0;
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302051 struct clk *lpass_core_hw_vote = NULL;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302052 struct clk *lpass_core_audio = NULL;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302053
2054 /* Allocate soundwire master driver structure */
2055 swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl),
2056 GFP_KERNEL);
2057 if (!swrm) {
2058 ret = -ENOMEM;
2059 goto err_memory_fail;
2060 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302061 swrm->pdev = pdev;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302062 swrm->dev = &pdev->dev;
2063 platform_set_drvdata(pdev, swrm);
2064 swr_set_ctrl_data(&swrm->master, swrm);
2065 pdata = dev_get_platdata(&pdev->dev);
2066 if (!pdata) {
2067 dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
2068 __func__);
2069 ret = -EINVAL;
2070 goto err_pdata_fail;
2071 }
2072 swrm->handle = (void *)pdata->handle;
2073 if (!swrm->handle) {
2074 dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
2075 __func__);
2076 ret = -EINVAL;
2077 goto err_pdata_fail;
2078 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302079 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr_master_id",
2080 &swrm->master_id);
2081 if (ret) {
2082 dev_err(&pdev->dev, "%s: failed to get master id\n", __func__);
2083 goto err_pdata_fail;
2084 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302085 if (!(of_property_read_u32(pdev->dev.of_node,
2086 "swrm-io-base", &swrm->swrm_base_reg)))
2087 ret = of_property_read_u32(pdev->dev.of_node,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302088 "swrm-io-base", &swrm->swrm_base_reg);
2089 if (!swrm->swrm_base_reg) {
2090 swrm->read = pdata->read;
2091 if (!swrm->read) {
2092 dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
2093 __func__);
2094 ret = -EINVAL;
2095 goto err_pdata_fail;
2096 }
2097 swrm->write = pdata->write;
2098 if (!swrm->write) {
2099 dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
2100 __func__);
2101 ret = -EINVAL;
2102 goto err_pdata_fail;
2103 }
2104 swrm->bulk_write = pdata->bulk_write;
2105 if (!swrm->bulk_write) {
2106 dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
2107 __func__);
2108 ret = -EINVAL;
2109 goto err_pdata_fail;
2110 }
2111 } else {
2112 swrm->swrm_dig_base = devm_ioremap(&pdev->dev,
2113 swrm->swrm_base_reg, SWRM_MAX_REGISTER);
2114 }
2115
2116 swrm->clk = pdata->clk;
2117 if (!swrm->clk) {
2118 dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
2119 __func__);
2120 ret = -EINVAL;
2121 goto err_pdata_fail;
2122 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302123 if (of_property_read_u32(pdev->dev.of_node,
2124 "qcom,swr-clock-stop-mode0",
2125 &swrm->clk_stop_mode0_supp)) {
2126 swrm->clk_stop_mode0_supp = FALSE;
2127 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302128
2129 ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
2130 &swrm->num_dev);
2131 if (ret) {
2132 dev_dbg(&pdev->dev, "%s: Looking up %s property failed\n",
2133 __func__, "qcom,swr-num-dev");
2134 } else {
2135 if (swrm->num_dev > SWR_MAX_SLAVE_DEVICES) {
2136 dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
2137 __func__, swrm->num_dev, SWR_MAX_SLAVE_DEVICES);
2138 ret = -EINVAL;
2139 goto err_pdata_fail;
2140 }
2141 }
2142
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302143 /* Parse soundwire port mapping */
2144 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
2145 &num_ports);
2146 if (ret) {
2147 dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
2148 goto err_pdata_fail;
2149 }
2150 swrm->num_ports = num_ports;
2151
2152 if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
2153 &map_size)) {
2154 dev_err(swrm->dev, "missing port mapping\n");
2155 goto err_pdata_fail;
2156 }
2157
2158 map_length = map_size / (3 * sizeof(u32));
2159 if (num_ports > SWR_MSTR_PORT_LEN) {
2160 dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
2161 __func__);
2162 ret = -EINVAL;
2163 goto err_pdata_fail;
2164 }
2165 temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
2166
2167 if (!temp) {
2168 ret = -ENOMEM;
2169 goto err_pdata_fail;
2170 }
2171 ret = of_property_read_u32_array(pdev->dev.of_node,
2172 "qcom,swr-port-mapping", temp, 3 * map_length);
2173 if (ret) {
2174 dev_err(swrm->dev, "%s: Failed to read port mapping\n",
2175 __func__);
2176 goto err_pdata_fail;
2177 }
2178
2179 for (i = 0; i < map_length; i++) {
2180 port_num = temp[3 * i];
2181 port_type = temp[3 * i + 1];
2182 ch_mask = temp[3 * i + 2];
2183
2184 if (port_num != old_port_num)
2185 ch_iter = 0;
2186 swrm->port_mapping[port_num][ch_iter].port_type = port_type;
2187 swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
2188 old_port_num = port_num;
2189 }
2190 devm_kfree(&pdev->dev, temp);
2191
2192 swrm->reg_irq = pdata->reg_irq;
2193 swrm->master.read = swrm_read;
2194 swrm->master.write = swrm_write;
2195 swrm->master.bulk_write = swrm_bulk_write;
2196 swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
2197 swrm->master.connect_port = swrm_connect_port;
2198 swrm->master.disconnect_port = swrm_disconnect_port;
2199 swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
2200 swrm->master.remove_from_group = swrm_remove_from_group;
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302201 swrm->master.device_wakeup_vote = swrm_device_wakeup_vote;
2202 swrm->master.device_wakeup_unvote = swrm_device_wakeup_unvote;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302203 swrm->master.dev.parent = &pdev->dev;
2204 swrm->master.dev.of_node = pdev->dev.of_node;
2205 swrm->master.num_port = 0;
2206 swrm->rcmd_id = 0;
2207 swrm->wcmd_id = 0;
2208 swrm->slave_status = 0;
2209 swrm->num_rx_chs = 0;
2210 swrm->clk_ref_count = 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302211 swrm->swr_irq_wakeup_capable = 0;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302212 swrm->mclk_freq = MCLK_FREQ;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302213 swrm->dev_up = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302214 swrm->state = SWR_MSTR_UP;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302215 swrm->ipc_wakeup = false;
2216 swrm->ipc_wakeup_triggered = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302217 init_completion(&swrm->reset);
2218 init_completion(&swrm->broadcast);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302219 init_completion(&swrm->clk_off_complete);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302220 mutex_init(&swrm->mlock);
2221 mutex_init(&swrm->reslock);
2222 mutex_init(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302223 mutex_init(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302224 mutex_init(&swrm->clklock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302225 mutex_init(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302226 mutex_init(&swrm->pm_lock);
2227 swrm->wlock_holders = 0;
2228 swrm->pm_state = SWRM_PM_SLEEPABLE;
2229 init_waitqueue_head(&swrm->pm_wq);
2230 pm_qos_add_request(&swrm->pm_qos_req,
2231 PM_QOS_CPU_DMA_LATENCY,
2232 PM_QOS_DEFAULT_VALUE);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302233
2234 for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++)
2235 INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
2236
Sudheer Papothi06f43412019-07-09 03:32:54 +05302237 /* Register LPASS core hw vote */
2238 lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
2239 if (IS_ERR(lpass_core_hw_vote)) {
2240 ret = PTR_ERR(lpass_core_hw_vote);
2241 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2242 __func__, "lpass_core_hw_vote", ret);
2243 lpass_core_hw_vote = NULL;
2244 ret = 0;
2245 }
2246 swrm->lpass_core_hw_vote = lpass_core_hw_vote;
2247
2248 /* Register LPASS audio core vote */
2249 lpass_core_audio = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
2250 if (IS_ERR(lpass_core_audio)) {
2251 ret = PTR_ERR(lpass_core_audio);
2252 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2253 __func__, "lpass_core_audio", ret);
2254 lpass_core_audio = NULL;
2255 ret = 0;
2256 }
2257 swrm->lpass_core_audio = lpass_core_audio;
2258
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302259 if (swrm->reg_irq) {
2260 ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
2261 SWR_IRQ_REGISTER);
2262 if (ret) {
2263 dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
2264 __func__, ret);
2265 goto err_irq_fail;
2266 }
2267 } else {
2268 swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
2269 if (swrm->irq < 0) {
2270 dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
2271 __func__, swrm->irq);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302272 goto err_irq_fail;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302273 }
2274
2275 ret = request_threaded_irq(swrm->irq, NULL,
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302276 swr_mstr_interrupt_v2,
Ramprasad Katkam83303512018-10-11 17:34:22 +05302277 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302278 "swr_master_irq", swrm);
2279 if (ret) {
2280 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2281 __func__, ret);
2282 goto err_irq_fail;
2283 }
2284
2285 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302286 /* Make inband tx interrupts as wakeup capable for slave irq */
2287 ret = of_property_read_u32(pdev->dev.of_node,
2288 "qcom,swr-mstr-irq-wakeup-capable",
2289 &swrm->swr_irq_wakeup_capable);
2290 if (ret)
2291 dev_dbg(swrm->dev, "%s: swrm irq wakeup capable not defined\n",
2292 __func__);
2293 if (swrm->swr_irq_wakeup_capable)
2294 irq_set_irq_wake(swrm->irq, 1);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302295 ret = swr_register_master(&swrm->master);
2296 if (ret) {
2297 dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
2298 goto err_mstr_fail;
2299 }
2300
2301 /* Add devices registered with board-info as the
2302 * controller will be up now
2303 */
2304 swr_master_add_boarddevices(&swrm->master);
2305 mutex_lock(&swrm->mlock);
2306 swrm_clk_request(swrm, true);
2307 ret = swrm_master_init(swrm);
2308 if (ret < 0) {
2309 dev_err(&pdev->dev,
2310 "%s: Error in master Initialization , err %d\n",
2311 __func__, ret);
2312 mutex_unlock(&swrm->mlock);
2313 goto err_mstr_fail;
2314 }
2315 swrm->version = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
2316
2317 mutex_unlock(&swrm->mlock);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302318 INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302319
2320 if (pdev->dev.of_node)
2321 of_register_swr_devices(&swrm->master);
2322
2323 dbgswrm = swrm;
2324 debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
2325 if (!IS_ERR(debugfs_swrm_dent)) {
2326 debugfs_peek = debugfs_create_file("swrm_peek",
2327 S_IFREG | 0444, debugfs_swrm_dent,
2328 (void *) "swrm_peek", &swrm_debug_ops);
2329
2330 debugfs_poke = debugfs_create_file("swrm_poke",
2331 S_IFREG | 0444, debugfs_swrm_dent,
2332 (void *) "swrm_poke", &swrm_debug_ops);
2333
2334 debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
2335 S_IFREG | 0444, debugfs_swrm_dent,
2336 (void *) "swrm_reg_dump",
2337 &swrm_debug_ops);
2338 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302339
2340 ret = device_init_wakeup(swrm->dev, true);
2341 if (ret) {
2342 dev_err(swrm->dev, "Device wakeup init failed: %d\n", ret);
2343 goto err_irq_wakeup_fail;
2344 }
2345
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302346 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
2347 pm_runtime_use_autosuspend(&pdev->dev);
2348 pm_runtime_set_active(&pdev->dev);
2349 pm_runtime_enable(&pdev->dev);
2350 pm_runtime_mark_last_busy(&pdev->dev);
2351
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302352 INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn);
2353 swrm->event_notifier.notifier_call = swrm_event_notify;
2354 msm_aud_evt_register_client(&swrm->event_notifier);
2355
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302356 return 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302357err_irq_wakeup_fail:
2358 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302359err_mstr_fail:
2360 if (swrm->reg_irq)
2361 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2362 swrm, SWR_IRQ_FREE);
2363 else if (swrm->irq)
2364 free_irq(swrm->irq, swrm);
2365err_irq_fail:
2366 mutex_destroy(&swrm->mlock);
2367 mutex_destroy(&swrm->reslock);
2368 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302369 mutex_destroy(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302370 mutex_destroy(&swrm->clklock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302371 mutex_destroy(&swrm->pm_lock);
2372 pm_qos_remove_request(&swrm->pm_qos_req);
2373
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302374err_pdata_fail:
2375err_memory_fail:
2376 return ret;
2377}
2378
2379static int swrm_remove(struct platform_device *pdev)
2380{
2381 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2382
2383 if (swrm->reg_irq)
2384 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2385 swrm, SWR_IRQ_FREE);
2386 else if (swrm->irq)
2387 free_irq(swrm->irq, swrm);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302388 else if (swrm->wake_irq > 0)
2389 free_irq(swrm->wake_irq, swrm);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302390 if (swrm->swr_irq_wakeup_capable)
2391 irq_set_irq_wake(swrm->irq, 0);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302392 cancel_work_sync(&swrm->wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302393 pm_runtime_disable(&pdev->dev);
2394 pm_runtime_set_suspended(&pdev->dev);
2395 swr_unregister_master(&swrm->master);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302396 msm_aud_evt_unregister_client(&swrm->event_notifier);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302397 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302398 mutex_destroy(&swrm->mlock);
2399 mutex_destroy(&swrm->reslock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302400 mutex_destroy(&swrm->iolock);
2401 mutex_destroy(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302402 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302403 mutex_destroy(&swrm->pm_lock);
2404 pm_qos_remove_request(&swrm->pm_qos_req);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302405 devm_kfree(&pdev->dev, swrm);
2406 return 0;
2407}
2408
2409static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
2410{
2411 u32 val;
2412
2413 dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
2414 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD);
2415 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2416 val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK;
2417 swr_master_write(swrm, SWRM_MCP_CFG_ADDR, val);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302418
2419 return 0;
2420}
2421
2422#ifdef CONFIG_PM
2423static int swrm_runtime_resume(struct device *dev)
2424{
2425 struct platform_device *pdev = to_platform_device(dev);
2426 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2427 int ret = 0;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302428 bool hw_core_err = false;
2429 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302430 struct swr_master *mstr = &swrm->master;
2431 struct swr_device *swr_dev;
2432
2433 dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
2434 __func__, swrm->state);
2435 mutex_lock(&swrm->reslock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302436
Sudheer Papothi384addd2019-06-14 02:26:52 +05302437 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2438 dev_err(dev, "%s:lpass core hw enable failed\n",
2439 __func__);
2440 hw_core_err = true;
2441 }
2442 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2443 dev_err(dev, "%s:lpass audio hw enable failed\n",
2444 __func__);
2445 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002446 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302447
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302448 if ((swrm->state == SWR_MSTR_DOWN) ||
2449 (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) {
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302450 if (swrm->clk_stop_mode0_supp) {
2451 if (swrm->ipc_wakeup)
2452 msm_aud_evt_blocking_notifier_call_chain(
2453 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302454 }
2455
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302456 if (swrm_clk_request(swrm, true)) {
2457 /*
2458 * Set autosuspend timer to 1 for
2459 * master to enter into suspend.
2460 */
2461 auto_suspend_timer = 1;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302462 goto exit;
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302463 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302464 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302465 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2466 ret = swr_device_up(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302467 if (ret == -ENODEV) {
2468 dev_dbg(dev,
2469 "%s slave device up not implemented\n",
2470 __func__);
2471 ret = 0;
2472 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302473 dev_err(dev,
2474 "%s: failed to wakeup swr dev %d\n",
2475 __func__, swr_dev->dev_num);
2476 swrm_clk_request(swrm, false);
2477 goto exit;
2478 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302479 }
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05302480 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2481 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2482 swrm_master_init(swrm);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302483 /* wait for hw enumeration to complete */
2484 usleep_range(100, 105);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302485 swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, 0x0,
2486 SWRS_SCP_INT_STATUS_MASK_1);
Karthikeyan Manif6821902019-05-21 17:31:24 -07002487 if (swrm->state == SWR_MSTR_SSR) {
2488 mutex_unlock(&swrm->reslock);
2489 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
2490 mutex_lock(&swrm->reslock);
2491 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302492 } else {
2493 /*wake up from clock stop*/
2494 swr_master_write(swrm, SWRM_MCP_BUS_CTRL_ADDR, 0x2);
2495 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302496 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302497 swrm->state = SWR_MSTR_UP;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302498 }
2499exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302500 if (!aud_core_err)
2501 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2502 if (!hw_core_err)
2503 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302504 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302505 auto_suspend_timer = SWR_AUTO_SUSPEND_DELAY * 1000;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302506 mutex_unlock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302507
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302508 return ret;
2509}
2510
2511static int swrm_runtime_suspend(struct device *dev)
2512{
2513 struct platform_device *pdev = to_platform_device(dev);
2514 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2515 int ret = 0;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302516 bool hw_core_err = false;
2517 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302518 struct swr_master *mstr = &swrm->master;
2519 struct swr_device *swr_dev;
2520 int current_state = 0;
2521
2522 dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
2523 __func__, swrm->state);
2524 mutex_lock(&swrm->reslock);
2525 mutex_lock(&swrm->force_down_lock);
2526 current_state = swrm->state;
2527 mutex_unlock(&swrm->force_down_lock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302528
2529 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2530 dev_err(dev, "%s:lpass core hw enable failed\n",
2531 __func__);
2532 hw_core_err = true;
2533 }
2534 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2535 dev_err(dev, "%s:lpass audio hw enable failed\n",
2536 __func__);
2537 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002538 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302539
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302540 if ((current_state == SWR_MSTR_UP) ||
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302541 (current_state == SWR_MSTR_SSR)) {
2542
2543 if ((current_state != SWR_MSTR_SSR) &&
2544 swrm_is_port_en(&swrm->master)) {
2545 dev_dbg(dev, "%s ports are enabled\n", __func__);
2546 ret = -EBUSY;
2547 goto exit;
2548 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302549 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05302550 mutex_unlock(&swrm->reslock);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +05302551 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
Sudheer Papothi06f43412019-07-09 03:32:54 +05302552 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302553 swrm_clk_pause(swrm);
2554 swr_master_write(swrm, SWRM_COMP_CFG_ADDR, 0x00);
2555 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2556 ret = swr_device_down(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302557 if (ret == -ENODEV) {
2558 dev_dbg_ratelimited(dev,
2559 "%s slave device down not implemented\n",
2560 __func__);
2561 ret = 0;
2562 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302563 dev_err(dev,
2564 "%s: failed to shutdown swr dev %d\n",
2565 __func__, swr_dev->dev_num);
2566 goto exit;
2567 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302568 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302569 } else {
Sudheer Papothi384addd2019-06-14 02:26:52 +05302570 mutex_unlock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302571 /* clock stop sequence */
2572 swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
2573 SWRS_SCP_CONTROL);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302574 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302575 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302576 }
2577 swrm_clk_request(swrm, false);
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302578
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302579 if (swrm->clk_stop_mode0_supp) {
2580 if (swrm->wake_irq > 0) {
2581 enable_irq(swrm->wake_irq);
2582 } else if (swrm->ipc_wakeup) {
2583 msm_aud_evt_blocking_notifier_call_chain(
2584 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
2585 swrm->ipc_wakeup_triggered = false;
2586 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302587 }
2588
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302589 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302590 /* Retain SSR state until resume */
2591 if (current_state != SWR_MSTR_SSR)
2592 swrm->state = SWR_MSTR_DOWN;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302593exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302594 if (!aud_core_err)
2595 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2596 if (!hw_core_err)
2597 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302598 mutex_unlock(&swrm->reslock);
2599 return ret;
2600}
2601#endif /* CONFIG_PM */
2602
Sudheer Papothi06f43412019-07-09 03:32:54 +05302603static int swrm_device_suspend(struct device *dev)
2604{
2605 struct platform_device *pdev = to_platform_device(dev);
2606 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2607 int ret = 0;
2608
2609 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2610 if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
2611 ret = swrm_runtime_suspend(dev);
2612 if (!ret) {
2613 pm_runtime_disable(dev);
2614 pm_runtime_set_suspended(dev);
2615 pm_runtime_enable(dev);
2616 }
2617 }
2618
2619 return 0;
2620}
2621
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302622static int swrm_device_down(struct device *dev)
2623{
2624 struct platform_device *pdev = to_platform_device(dev);
2625 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302626
2627 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2628
2629 mutex_lock(&swrm->force_down_lock);
2630 swrm->state = SWR_MSTR_SSR;
2631 mutex_unlock(&swrm->force_down_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302632
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302633 swrm_device_suspend(dev);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302634 return 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302635}
2636
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302637int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm)
2638{
2639 int ret = 0;
Laxminath Kasama60239e2019-01-10 14:43:03 +05302640 int irq, dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302641
2642 if (!swrm->ipc_wakeup) {
Laxminath Kasama60239e2019-01-10 14:43:03 +05302643 irq = of_get_named_gpio(swrm->dev->of_node,
2644 "qcom,swr-wakeup-irq", 0);
2645 if (gpio_is_valid(irq)) {
2646 swrm->wake_irq = gpio_to_irq(irq);
2647 if (swrm->wake_irq < 0) {
2648 dev_err(swrm->dev,
2649 "Unable to configure irq\n");
2650 return swrm->wake_irq;
2651 }
2652 } else {
2653 dir_apps_irq = platform_get_irq_byname(swrm->pdev,
2654 "swr_wake_irq");
2655 if (dir_apps_irq < 0) {
2656 dev_err(swrm->dev,
2657 "TLMM connect gpio not found\n");
2658 return -EINVAL;
2659 }
2660 swrm->wake_irq = dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302661 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302662 ret = request_threaded_irq(swrm->wake_irq, NULL,
2663 swrm_wakeup_interrupt,
2664 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
2665 "swr_wake_irq", swrm);
2666 if (ret) {
2667 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2668 __func__, ret);
2669 return -EINVAL;
2670 }
Aditya Bavanari3517b112018-12-03 13:26:59 +05302671 irq_set_irq_wake(swrm->wake_irq, 1);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302672 }
2673 return ret;
2674}
2675
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302676static int swrm_alloc_port_mem(struct device *dev, struct swr_mstr_ctrl *swrm,
2677 u32 uc, u32 size)
2678{
2679 if (!swrm->port_param) {
2680 swrm->port_param = devm_kzalloc(dev,
2681 sizeof(swrm->port_param) * SWR_UC_MAX,
2682 GFP_KERNEL);
2683 if (!swrm->port_param)
2684 return -ENOMEM;
2685 }
2686 if (!swrm->port_param[uc]) {
2687 swrm->port_param[uc] = devm_kcalloc(dev, size,
2688 sizeof(struct port_params),
2689 GFP_KERNEL);
2690 if (!swrm->port_param[uc])
2691 return -ENOMEM;
2692 } else {
2693 dev_err_ratelimited(swrm->dev, "%s: called more than once\n",
2694 __func__);
2695 }
2696
2697 return 0;
2698}
2699
2700static int swrm_copy_port_config(struct swr_mstr_ctrl *swrm,
2701 struct swrm_port_config *port_cfg,
2702 u32 size)
2703{
2704 int idx;
2705 struct port_params *params;
2706 int uc = port_cfg->uc;
2707 int ret = 0;
2708
2709 for (idx = 0; idx < size; idx++) {
2710 params = &((struct port_params *)port_cfg->params)[idx];
2711 if (!params) {
2712 dev_err(swrm->dev, "%s: Invalid params\n", __func__);
2713 ret = -EINVAL;
2714 break;
2715 }
2716 memcpy(&swrm->port_param[uc][idx], params,
2717 sizeof(struct port_params));
2718 }
2719
2720 return ret;
2721}
2722
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302723/**
2724 * swrm_wcd_notify - parent device can notify to soundwire master through
2725 * this function
2726 * @pdev: pointer to platform device structure
2727 * @id: command id from parent to the soundwire master
2728 * @data: data from parent device to soundwire master
2729 */
2730int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
2731{
2732 struct swr_mstr_ctrl *swrm;
2733 int ret = 0;
2734 struct swr_master *mstr;
2735 struct swr_device *swr_dev;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302736 struct swrm_port_config *port_cfg;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302737
2738 if (!pdev) {
2739 pr_err("%s: pdev is NULL\n", __func__);
2740 return -EINVAL;
2741 }
2742 swrm = platform_get_drvdata(pdev);
2743 if (!swrm) {
2744 dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__);
2745 return -EINVAL;
2746 }
2747 mstr = &swrm->master;
2748
2749 switch (id) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05302750 case SWR_REQ_CLK_SWITCH:
2751 /* This will put soundwire in clock stop mode and disable the
2752 * clocks, if there is no active usecase running, so that the
2753 * next activity on soundwire will request clock from new clock
2754 * source.
2755 */
2756 mutex_lock(&swrm->mlock);
2757 if (swrm->state == SWR_MSTR_UP)
2758 swrm_device_suspend(&pdev->dev);
2759 mutex_unlock(&swrm->mlock);
2760 break;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302761 case SWR_CLK_FREQ:
2762 if (!data) {
2763 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
2764 ret = -EINVAL;
2765 } else {
2766 mutex_lock(&swrm->mlock);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302767 if (swrm->mclk_freq != *(int *)data) {
2768 dev_dbg(swrm->dev, "%s: freq change: force mstr down\n", __func__);
2769 if (swrm->state == SWR_MSTR_DOWN)
2770 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
2771 __func__, swrm->state);
2772 else
2773 swrm_device_suspend(&pdev->dev);
2774 }
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302775 swrm->mclk_freq = *(int *)data;
2776 mutex_unlock(&swrm->mlock);
2777 }
2778 break;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302779 case SWR_DEVICE_SSR_DOWN:
2780 mutex_lock(&swrm->devlock);
2781 swrm->dev_up = false;
2782 mutex_unlock(&swrm->devlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302783 mutex_lock(&swrm->reslock);
2784 swrm->state = SWR_MSTR_SSR;
2785 mutex_unlock(&swrm->reslock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302786 break;
2787 case SWR_DEVICE_SSR_UP:
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302788 /* wait for clk voting to be zero */
Ramprasad Katkam7f6462e2018-11-06 11:51:22 +05302789 reinit_completion(&swrm->clk_off_complete);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302790 if (swrm->clk_ref_count &&
2791 !wait_for_completion_timeout(&swrm->clk_off_complete,
Ramprasad Katkamc87efeb2018-12-12 19:26:19 +05302792 msecs_to_jiffies(500)))
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302793 dev_err(swrm->dev, "%s: clock voting not zero\n",
2794 __func__);
2795
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302796 mutex_lock(&swrm->devlock);
2797 swrm->dev_up = true;
2798 mutex_unlock(&swrm->devlock);
2799 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302800 case SWR_DEVICE_DOWN:
2801 dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
2802 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302803 if (swrm->state == SWR_MSTR_DOWN)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302804 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
2805 __func__, swrm->state);
2806 else
2807 swrm_device_down(&pdev->dev);
2808 mutex_unlock(&swrm->mlock);
2809 break;
2810 case SWR_DEVICE_UP:
2811 dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
Ramprasad Katkam0fed92f2018-11-08 14:22:22 +05302812 mutex_lock(&swrm->devlock);
2813 if (!swrm->dev_up) {
2814 dev_dbg(swrm->dev, "SSR not complete yet\n");
2815 mutex_unlock(&swrm->devlock);
2816 return -EBUSY;
2817 }
2818 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302819 mutex_lock(&swrm->mlock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302820 pm_runtime_mark_last_busy(&pdev->dev);
2821 pm_runtime_get_sync(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302822 mutex_lock(&swrm->reslock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302823 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2824 ret = swr_reset_device(swr_dev);
2825 if (ret) {
2826 dev_err(swrm->dev,
2827 "%s: failed to reset swr device %d\n",
2828 __func__, swr_dev->dev_num);
2829 swrm_clk_request(swrm, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302830 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302831 }
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302832 pm_runtime_mark_last_busy(&pdev->dev);
2833 pm_runtime_put_autosuspend(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302834 mutex_unlock(&swrm->reslock);
2835 mutex_unlock(&swrm->mlock);
2836 break;
2837 case SWR_SET_NUM_RX_CH:
2838 if (!data) {
2839 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
2840 ret = -EINVAL;
2841 } else {
2842 mutex_lock(&swrm->mlock);
2843 swrm->num_rx_chs = *(int *)data;
2844 if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
2845 list_for_each_entry(swr_dev, &mstr->devices,
2846 dev_list) {
2847 ret = swr_set_device_group(swr_dev,
2848 SWR_BROADCAST);
2849 if (ret)
2850 dev_err(swrm->dev,
2851 "%s: set num ch failed\n",
2852 __func__);
2853 }
2854 } else {
2855 list_for_each_entry(swr_dev, &mstr->devices,
2856 dev_list) {
2857 ret = swr_set_device_group(swr_dev,
2858 SWR_GROUP_NONE);
2859 if (ret)
2860 dev_err(swrm->dev,
2861 "%s: set num ch failed\n",
2862 __func__);
2863 }
2864 }
2865 mutex_unlock(&swrm->mlock);
2866 }
2867 break;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302868 case SWR_REGISTER_WAKE_IRQ:
2869 if (!data) {
2870 dev_err(swrm->dev, "%s: reg wake irq data is NULL\n",
2871 __func__);
2872 ret = -EINVAL;
2873 } else {
2874 mutex_lock(&swrm->mlock);
2875 swrm->ipc_wakeup = *(u32 *)data;
2876 ret = swrm_register_wake_irq(swrm);
2877 if (ret)
2878 dev_err(swrm->dev, "%s: register wake_irq failed\n",
2879 __func__);
2880 mutex_unlock(&swrm->mlock);
2881 }
2882 break;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302883 case SWR_SET_PORT_MAP:
2884 if (!data) {
2885 dev_err(swrm->dev, "%s: data is NULL for id=%d\n",
2886 __func__, id);
2887 ret = -EINVAL;
2888 } else {
2889 mutex_lock(&swrm->mlock);
2890 port_cfg = (struct swrm_port_config *)data;
2891 if (!port_cfg->size) {
2892 ret = -EINVAL;
2893 goto done;
2894 }
2895 ret = swrm_alloc_port_mem(&pdev->dev, swrm,
2896 port_cfg->uc, port_cfg->size);
2897 if (!ret)
2898 swrm_copy_port_config(swrm, port_cfg,
2899 port_cfg->size);
2900done:
2901 mutex_unlock(&swrm->mlock);
2902 }
2903 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302904 default:
2905 dev_err(swrm->dev, "%s: swr master unknown id %d\n",
2906 __func__, id);
2907 break;
2908 }
2909 return ret;
2910}
2911EXPORT_SYMBOL(swrm_wcd_notify);
2912
Ramprasad Katkam57349872018-11-11 18:34:57 +05302913/*
2914 * swrm_pm_cmpxchg:
2915 * Check old state and exchange with pm new state
2916 * if old state matches with current state
2917 *
2918 * @swrm: pointer to wcd core resource
2919 * @o: pm old state
2920 * @n: pm new state
2921 *
2922 * Returns old state
2923 */
2924static enum swrm_pm_state swrm_pm_cmpxchg(
2925 struct swr_mstr_ctrl *swrm,
2926 enum swrm_pm_state o,
2927 enum swrm_pm_state n)
2928{
2929 enum swrm_pm_state old;
2930
2931 if (!swrm)
2932 return o;
2933
2934 mutex_lock(&swrm->pm_lock);
2935 old = swrm->pm_state;
2936 if (old == o)
2937 swrm->pm_state = n;
2938 mutex_unlock(&swrm->pm_lock);
2939
2940 return old;
2941}
2942
2943static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm)
2944{
2945 enum swrm_pm_state os;
2946
2947 /*
2948 * swrm_{lock/unlock}_sleep will be called by swr irq handler
2949 * and slave wake up requests..
2950 *
2951 * If system didn't resume, we can simply return false so
2952 * IRQ handler can return without handling IRQ.
2953 */
2954 mutex_lock(&swrm->pm_lock);
2955 if (swrm->wlock_holders++ == 0) {
2956 dev_dbg(swrm->dev, "%s: holding wake lock\n", __func__);
2957 pm_qos_update_request(&swrm->pm_qos_req,
2958 msm_cpuidle_get_deep_idle_latency());
2959 pm_stay_awake(swrm->dev);
2960 }
2961 mutex_unlock(&swrm->pm_lock);
2962
2963 if (!wait_event_timeout(swrm->pm_wq,
2964 ((os = swrm_pm_cmpxchg(swrm,
2965 SWRM_PM_SLEEPABLE,
2966 SWRM_PM_AWAKE)) ==
2967 SWRM_PM_SLEEPABLE ||
2968 (os == SWRM_PM_AWAKE)),
2969 msecs_to_jiffies(
2970 SWRM_SYSTEM_RESUME_TIMEOUT_MS))) {
2971 dev_err(swrm->dev, "%s: system didn't resume within %dms, s %d, w %d\n",
2972 __func__, SWRM_SYSTEM_RESUME_TIMEOUT_MS, swrm->pm_state,
2973 swrm->wlock_holders);
2974 swrm_unlock_sleep(swrm);
2975 return false;
2976 }
2977 wake_up_all(&swrm->pm_wq);
2978 return true;
2979}
2980
2981static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm)
2982{
2983 mutex_lock(&swrm->pm_lock);
2984 if (--swrm->wlock_holders == 0) {
2985 dev_dbg(swrm->dev, "%s: releasing wake lock pm_state %d -> %d\n",
2986 __func__, swrm->pm_state, SWRM_PM_SLEEPABLE);
2987 /*
2988 * if swrm_lock_sleep failed, pm_state would be still
2989 * swrm_PM_ASLEEP, don't overwrite
2990 */
2991 if (likely(swrm->pm_state == SWRM_PM_AWAKE))
2992 swrm->pm_state = SWRM_PM_SLEEPABLE;
2993 pm_qos_update_request(&swrm->pm_qos_req,
2994 PM_QOS_DEFAULT_VALUE);
2995 pm_relax(swrm->dev);
2996 }
2997 mutex_unlock(&swrm->pm_lock);
2998 wake_up_all(&swrm->pm_wq);
2999}
3000
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303001#ifdef CONFIG_PM_SLEEP
3002static int swrm_suspend(struct device *dev)
3003{
3004 int ret = -EBUSY;
3005 struct platform_device *pdev = to_platform_device(dev);
3006 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3007
3008 dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
Ramprasad Katkam57349872018-11-11 18:34:57 +05303009
3010 mutex_lock(&swrm->pm_lock);
3011
3012 if (swrm->pm_state == SWRM_PM_SLEEPABLE) {
3013 dev_dbg(swrm->dev, "%s: suspending system, state %d, wlock %d\n",
3014 __func__, swrm->pm_state,
3015 swrm->wlock_holders);
3016 swrm->pm_state = SWRM_PM_ASLEEP;
3017 } else if (swrm->pm_state == SWRM_PM_AWAKE) {
3018 /*
3019 * unlock to wait for pm_state == SWRM_PM_SLEEPABLE
3020 * then set to SWRM_PM_ASLEEP
3021 */
3022 dev_dbg(swrm->dev, "%s: waiting to suspend system, state %d, wlock %d\n",
3023 __func__, swrm->pm_state,
3024 swrm->wlock_holders);
3025 mutex_unlock(&swrm->pm_lock);
3026 if (!(wait_event_timeout(swrm->pm_wq, swrm_pm_cmpxchg(
3027 swrm, SWRM_PM_SLEEPABLE,
3028 SWRM_PM_ASLEEP) ==
3029 SWRM_PM_SLEEPABLE,
3030 msecs_to_jiffies(
3031 SWRM_SYS_SUSPEND_WAIT)))) {
3032 dev_dbg(swrm->dev, "%s: suspend failed state %d, wlock %d\n",
3033 __func__, swrm->pm_state,
3034 swrm->wlock_holders);
3035 return -EBUSY;
3036 } else {
3037 dev_dbg(swrm->dev,
3038 "%s: done, state %d, wlock %d\n",
3039 __func__, swrm->pm_state,
3040 swrm->wlock_holders);
3041 }
3042 mutex_lock(&swrm->pm_lock);
3043 } else if (swrm->pm_state == SWRM_PM_ASLEEP) {
3044 dev_dbg(swrm->dev, "%s: system is already suspended, state %d, wlock %d\n",
3045 __func__, swrm->pm_state,
3046 swrm->wlock_holders);
3047 }
3048
3049 mutex_unlock(&swrm->pm_lock);
3050
3051 if ((!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev))) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303052 ret = swrm_runtime_suspend(dev);
3053 if (!ret) {
3054 /*
3055 * Synchronize runtime-pm and system-pm states:
3056 * At this point, we are already suspended. If
3057 * runtime-pm still thinks its active, then
3058 * make sure its status is in sync with HW
3059 * status. The three below calls let the
3060 * runtime-pm know that we are suspended
3061 * already without re-invoking the suspend
3062 * callback
3063 */
3064 pm_runtime_disable(dev);
3065 pm_runtime_set_suspended(dev);
3066 pm_runtime_enable(dev);
3067 }
3068 }
3069 if (ret == -EBUSY) {
3070 /*
3071 * There is a possibility that some audio stream is active
3072 * during suspend. We dont want to return suspend failure in
3073 * that case so that display and relevant components can still
3074 * go to suspend.
3075 * If there is some other error, then it should be passed-on
3076 * to system level suspend
3077 */
3078 ret = 0;
3079 }
3080 return ret;
3081}
3082
3083static int swrm_resume(struct device *dev)
3084{
3085 int ret = 0;
3086 struct platform_device *pdev = to_platform_device(dev);
3087 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3088
3089 dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
3090 if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
3091 ret = swrm_runtime_resume(dev);
3092 if (!ret) {
3093 pm_runtime_mark_last_busy(dev);
3094 pm_request_autosuspend(dev);
3095 }
3096 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05303097 mutex_lock(&swrm->pm_lock);
3098 if (swrm->pm_state == SWRM_PM_ASLEEP) {
3099 dev_dbg(swrm->dev,
3100 "%s: resuming system, state %d, wlock %d\n",
3101 __func__, swrm->pm_state,
3102 swrm->wlock_holders);
3103 swrm->pm_state = SWRM_PM_SLEEPABLE;
3104 } else {
3105 dev_dbg(swrm->dev, "%s: system is already awake, state %d wlock %d\n",
3106 __func__, swrm->pm_state,
3107 swrm->wlock_holders);
3108 }
3109 mutex_unlock(&swrm->pm_lock);
3110 wake_up_all(&swrm->pm_wq);
3111
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303112 return ret;
3113}
3114#endif /* CONFIG_PM_SLEEP */
3115
3116static const struct dev_pm_ops swrm_dev_pm_ops = {
3117 SET_SYSTEM_SLEEP_PM_OPS(
3118 swrm_suspend,
3119 swrm_resume
3120 )
3121 SET_RUNTIME_PM_OPS(
3122 swrm_runtime_suspend,
3123 swrm_runtime_resume,
3124 NULL
3125 )
3126};
3127
3128static const struct of_device_id swrm_dt_match[] = {
3129 {
3130 .compatible = "qcom,swr-mstr",
3131 },
3132 {}
3133};
3134
3135static struct platform_driver swr_mstr_driver = {
3136 .probe = swrm_probe,
3137 .remove = swrm_remove,
3138 .driver = {
3139 .name = SWR_WCD_NAME,
3140 .owner = THIS_MODULE,
3141 .pm = &swrm_dev_pm_ops,
3142 .of_match_table = swrm_dt_match,
Xiaojun Sang53cd13a2018-06-29 15:14:37 +08003143 .suppress_bind_attrs = true,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303144 },
3145};
3146
3147static int __init swrm_init(void)
3148{
3149 return platform_driver_register(&swr_mstr_driver);
3150}
3151module_init(swrm_init);
3152
3153static void __exit swrm_exit(void)
3154{
3155 platform_driver_unregister(&swr_mstr_driver);
3156}
3157module_exit(swrm_exit);
3158
3159MODULE_LICENSE("GPL v2");
3160MODULE_DESCRIPTION("SoundWire Master Controller");
3161MODULE_ALIAS("platform:swr-mstr");