blob: b42cd5f49437003c85c786dda7f0de7610ded698 [file] [log] [blame]
Meng Wang43bbb872018-12-10 12:32:05 +08001// SPDX-License-Identifier: GPL-2.0-only
Meng Wang61af6842018-09-10 17:47:55 +08002/*
Aditya Bavanari3517b112018-12-03 13:26:59 +05303 * Copyright (c) 2015-2019, The Linux Foundation. All rights reserved.
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05304 */
5
6#include <linux/irq.h>
7#include <linux/kernel.h>
8#include <linux/init.h>
9#include <linux/slab.h>
10#include <linux/io.h>
11#include <linux/interrupt.h>
12#include <linux/platform_device.h>
13#include <linux/delay.h>
14#include <linux/kthread.h>
Ramprasad Katkamcab8d722018-09-28 15:54:06 +053015#include <linux/bitops.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053016#include <linux/clk.h>
Laxminath Kasama60239e2019-01-10 14:43:03 +053017#include <linux/gpio.h>
18#include <linux/of_gpio.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053019#include <linux/pm_runtime.h>
20#include <linux/of.h>
21#include <linux/debugfs.h>
22#include <linux/uaccess.h>
23#include <soc/soundwire.h>
Sudheer Papothi3d1596e2018-10-27 06:19:18 +053024#include <soc/swr-common.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053025#include <linux/regmap.h>
Ramprasad Katkam68765ab2018-08-30 11:46:32 +053026#include <dsp/msm-audio-event-notify.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053027#include "swrm_registers.h"
28#include "swr-mstr-ctrl.h"
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053029
Ramprasad Katkam57349872018-11-11 18:34:57 +053030#define SWRM_SYSTEM_RESUME_TIMEOUT_MS 700
31#define SWRM_SYS_SUSPEND_WAIT 1
Sudheer Papothi3d1596e2018-10-27 06:19:18 +053032
Sudheer Papothi4c322b12018-10-31 06:34:01 +053033#define SWRM_DSD_PARAMS_PORT 4
34
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053035#define SWR_BROADCAST_CMD_ID 0x0F
Sudheer Papothi3590b312019-06-04 23:51:30 +053036#define SWR_AUTO_SUSPEND_DELAY 1 /* delay in sec */
Sudheer Papothi7c067e82018-11-15 06:53:35 +053037#define SWR_DEV_ID_MASK 0xFFFFFFFFFFFF
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053038#define SWR_REG_VAL_PACK(data, dev, id, reg) \
39 ((reg) | ((id) << 16) | ((dev) << 20) | ((data) << 24))
40
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +053041#define SWR_INVALID_PARAM 0xFF
Laxminath Kasam990c70b2018-11-09 23:15:09 +053042#define SWR_HSTOP_MAX_VAL 0xF
43#define SWR_HSTART_MIN_VAL 0x0
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +053044
Ramprasad Katkam83303512018-10-11 17:34:22 +053045#define SWRM_INTERRUPT_STATUS_MASK 0x1FDFD
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053046/* pm runtime auto suspend timer in msecs */
47static int auto_suspend_timer = SWR_AUTO_SUSPEND_DELAY * 1000;
48module_param(auto_suspend_timer, int, 0664);
49MODULE_PARM_DESC(auto_suspend_timer, "timer for auto suspend");
50
51enum {
52 SWR_NOT_PRESENT, /* Device is detached/not present on the bus */
53 SWR_ATTACHED_OK, /* Device is attached */
54 SWR_ALERT, /* Device alters master for any interrupts */
55 SWR_RESERVED, /* Reserved */
56};
57
58enum {
59 MASTER_ID_WSA = 1,
60 MASTER_ID_RX,
61 MASTER_ID_TX
62};
Ramprasad Katkamcab8d722018-09-28 15:54:06 +053063
64enum {
65 ENABLE_PENDING,
66 DISABLE_PENDING
67};
Sudheer Papothi384addd2019-06-14 02:26:52 +053068
69enum {
70 LPASS_HW_CORE,
71 LPASS_AUDIO_CORE,
72};
73
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053074#define TRUE 1
75#define FALSE 0
76
Ramprasad Katkam1f221262018-08-23 15:01:22 +053077#define SWRM_MAX_PORT_REG 120
Ramprasad Katkam83303512018-10-11 17:34:22 +053078#define SWRM_MAX_INIT_REG 11
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053079
80#define SWR_MSTR_MAX_REG_ADDR 0x1740
81#define SWR_MSTR_START_REG_ADDR 0x00
82#define SWR_MSTR_MAX_BUF_LEN 32
83#define BYTES_PER_LINE 12
84#define SWR_MSTR_RD_BUF_LEN 8
85#define SWR_MSTR_WR_BUF_LEN 32
86
Laxminath Kasamfbcaf322018-07-18 00:38:14 +053087#define MAX_FIFO_RD_FAIL_RETRY 3
88
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053089static struct swr_mstr_ctrl *dbgswrm;
90static struct dentry *debugfs_swrm_dent;
91static struct dentry *debugfs_peek;
92static struct dentry *debugfs_poke;
93static struct dentry *debugfs_reg_dump;
94static unsigned int read_data;
95
Ramprasad Katkam57349872018-11-11 18:34:57 +053096static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm);
97static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053098
99static bool swrm_is_msm_variant(int val)
100{
101 return (val == SWRM_VERSION_1_3);
102}
103
104static int swrm_debug_open(struct inode *inode, struct file *file)
105{
106 file->private_data = inode->i_private;
107 return 0;
108}
109
110static int get_parameters(char *buf, u32 *param1, int num_of_par)
111{
112 char *token;
113 int base, cnt;
114
115 token = strsep(&buf, " ");
116 for (cnt = 0; cnt < num_of_par; cnt++) {
117 if (token) {
118 if ((token[1] == 'x') || (token[1] == 'X'))
119 base = 16;
120 else
121 base = 10;
122
123 if (kstrtou32(token, base, &param1[cnt]) != 0)
124 return -EINVAL;
125
126 token = strsep(&buf, " ");
127 } else
128 return -EINVAL;
129 }
130 return 0;
131}
132
133static ssize_t swrm_reg_show(char __user *ubuf, size_t count,
134 loff_t *ppos)
135{
136 int i, reg_val, len;
137 ssize_t total = 0;
138 char tmp_buf[SWR_MSTR_MAX_BUF_LEN];
139
140 if (!ubuf || !ppos)
141 return 0;
142
143 for (i = (((int) *ppos / BYTES_PER_LINE) + SWR_MSTR_START_REG_ADDR);
144 i <= SWR_MSTR_MAX_REG_ADDR; i += 4) {
145 reg_val = dbgswrm->read(dbgswrm->handle, i);
146 len = snprintf(tmp_buf, 25, "0x%.3x: 0x%.2x\n", i, reg_val);
Aditya Bavanari9f599b42019-08-27 22:18:41 +0530147 if (len < 0) {
148 pr_err("%s: fail to fill the buffer\n", __func__);
149 total = -EFAULT;
150 goto copy_err;
151 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530152 if ((total + len) >= count - 1)
153 break;
154 if (copy_to_user((ubuf + total), tmp_buf, len)) {
155 pr_err("%s: fail to copy reg dump\n", __func__);
156 total = -EFAULT;
157 goto copy_err;
158 }
159 *ppos += len;
160 total += len;
161 }
162
163copy_err:
164 return total;
165}
166
167static ssize_t swrm_debug_read(struct file *file, char __user *ubuf,
168 size_t count, loff_t *ppos)
169{
170 char lbuf[SWR_MSTR_RD_BUF_LEN];
171 char *access_str;
172 ssize_t ret_cnt;
173
174 if (!count || !file || !ppos || !ubuf)
175 return -EINVAL;
176
177 access_str = file->private_data;
178 if (*ppos < 0)
179 return -EINVAL;
180
181 if (!strcmp(access_str, "swrm_peek")) {
182 snprintf(lbuf, sizeof(lbuf), "0x%x\n", read_data);
183 ret_cnt = simple_read_from_buffer(ubuf, count, ppos, lbuf,
184 strnlen(lbuf, 7));
185 } else if (!strcmp(access_str, "swrm_reg_dump")) {
186 ret_cnt = swrm_reg_show(ubuf, count, ppos);
187 } else {
188 pr_err("%s: %s not permitted to read\n", __func__, access_str);
189 ret_cnt = -EPERM;
190 }
191 return ret_cnt;
192}
193
194static ssize_t swrm_debug_write(struct file *filp,
195 const char __user *ubuf, size_t cnt, loff_t *ppos)
196{
197 char lbuf[SWR_MSTR_WR_BUF_LEN];
198 int rc;
199 u32 param[5];
200 char *access_str;
201
202 if (!filp || !ppos || !ubuf)
203 return -EINVAL;
204
205 access_str = filp->private_data;
206 if (cnt > sizeof(lbuf) - 1)
207 return -EINVAL;
208
209 rc = copy_from_user(lbuf, ubuf, cnt);
210 if (rc)
211 return -EFAULT;
212
213 lbuf[cnt] = '\0';
214 if (!strcmp(access_str, "swrm_poke")) {
215 /* write */
216 rc = get_parameters(lbuf, param, 2);
217 if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) &&
218 (param[1] <= 0xFFFFFFFF) &&
219 (rc == 0))
220 rc = dbgswrm->write(dbgswrm->handle, param[0],
221 param[1]);
222 else
223 rc = -EINVAL;
224 } else if (!strcmp(access_str, "swrm_peek")) {
225 /* read */
226 rc = get_parameters(lbuf, param, 1);
227 if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) && (rc == 0))
228 read_data = dbgswrm->read(dbgswrm->handle, param[0]);
229 else
230 rc = -EINVAL;
231 }
232 if (rc == 0)
233 rc = cnt;
234 else
235 pr_err("%s: rc = %d\n", __func__, rc);
236
237 return rc;
238}
239
240static const struct file_operations swrm_debug_ops = {
241 .open = swrm_debug_open,
242 .write = swrm_debug_write,
243 .read = swrm_debug_read,
244};
245
Sudheer Papothi0016db12019-06-11 04:42:38 +0530246static void swrm_reg_dump(struct swr_mstr_ctrl *swrm,
247 u32 *reg, u32 *val, int len, const char* func)
248{
249 int i = 0;
250
251 for (i = 0; i < len; i++)
252 dev_dbg(swrm->dev, "%s: reg = 0x%x val = 0x%x\n",
253 func, reg[i], val[i]);
254}
255
Sudheer Papothi384addd2019-06-14 02:26:52 +0530256static int swrm_request_hw_vote(struct swr_mstr_ctrl *swrm,
257 int core_type, bool enable)
258{
259 int ret = 0;
260
261 if (core_type == LPASS_HW_CORE) {
262 if (swrm->lpass_core_hw_vote) {
263 if (enable) {
264 ret =
265 clk_prepare_enable(swrm->lpass_core_hw_vote);
266 if (ret < 0)
267 dev_err(swrm->dev,
268 "%s:lpass core hw enable failed\n",
269 __func__);
270 } else
271 clk_disable_unprepare(swrm->lpass_core_hw_vote);
272 }
273 }
274 if (core_type == LPASS_AUDIO_CORE) {
275 if (swrm->lpass_core_audio) {
276 if (enable) {
277 ret =
278 clk_prepare_enable(swrm->lpass_core_audio);
279 if (ret < 0)
280 dev_err(swrm->dev,
281 "%s:lpass audio hw enable failed\n",
282 __func__);
283 } else
284 clk_disable_unprepare(swrm->lpass_core_audio);
285 }
286 }
287
288 return ret;
289}
290
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530291static int swrm_clk_request(struct swr_mstr_ctrl *swrm, bool enable)
292{
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530293 int ret = 0;
294
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530295 if (!swrm->clk || !swrm->handle)
296 return -EINVAL;
297
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530298 mutex_lock(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530299 if (enable) {
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530300 if (!swrm->dev_up) {
301 ret = -ENODEV;
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530302 goto exit;
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530303 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530304 swrm->clk_ref_count++;
305 if (swrm->clk_ref_count == 1) {
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530306 ret = swrm->clk(swrm->handle, true);
307 if (ret) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +0530308 dev_err_ratelimited(swrm->dev,
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530309 "%s: clock enable req failed",
310 __func__);
311 --swrm->clk_ref_count;
312 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530313 }
314 } else if (--swrm->clk_ref_count == 0) {
315 swrm->clk(swrm->handle, false);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530316 complete(&swrm->clk_off_complete);
317 }
318 if (swrm->clk_ref_count < 0) {
Meng Wang8c60bb52019-06-19 15:49:06 +0800319 dev_err(swrm->dev, "%s: swrm clk count mismatch\n", __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530320 swrm->clk_ref_count = 0;
321 }
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530322
323exit:
324 mutex_unlock(&swrm->clklock);
325 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530326}
327
328static int swrm_ahb_write(struct swr_mstr_ctrl *swrm,
329 u16 reg, u32 *value)
330{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530331 u32 temp = (u32)(*value);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530332 int ret = 0;
333
334 mutex_lock(&swrm->devlock);
335 if (!swrm->dev_up)
336 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530337
338 ret = swrm_clk_request(swrm, TRUE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530339 if (ret) {
340 dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
341 __func__);
342 goto err;
343 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530344 iowrite32(temp, swrm->swrm_dig_base + reg);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530345 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530346err:
347 mutex_unlock(&swrm->devlock);
348 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530349}
350
351static int swrm_ahb_read(struct swr_mstr_ctrl *swrm,
352 u16 reg, u32 *value)
353{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530354 u32 temp = 0;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530355 int ret = 0;
356
357 mutex_lock(&swrm->devlock);
358 if (!swrm->dev_up)
359 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530360
361 ret = swrm_clk_request(swrm, TRUE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530362 if (ret) {
363 dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
364 __func__);
365 goto err;
366 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530367 temp = ioread32(swrm->swrm_dig_base + reg);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530368 *value = temp;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530369 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530370err:
371 mutex_unlock(&swrm->devlock);
372 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530373}
374
375static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr)
376{
377 u32 val = 0;
378
379 if (swrm->read)
380 val = swrm->read(swrm->handle, reg_addr);
381 else
382 swrm_ahb_read(swrm, reg_addr, &val);
383 return val;
384}
385
386static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val)
387{
388 if (swrm->write)
389 swrm->write(swrm->handle, reg_addr, val);
390 else
391 swrm_ahb_write(swrm, reg_addr, &val);
392}
393
394static int swr_master_bulk_write(struct swr_mstr_ctrl *swrm, u32 *reg_addr,
395 u32 *val, unsigned int length)
396{
397 int i = 0;
398
399 if (swrm->bulk_write)
400 swrm->bulk_write(swrm->handle, reg_addr, val, length);
401 else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530402 mutex_lock(&swrm->iolock);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530403 for (i = 0; i < length; i++) {
404 /* wait for FIFO WR command to complete to avoid overflow */
405 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530406 swr_master_write(swrm, reg_addr[i], val[i]);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530407 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530408 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530409 }
410 return 0;
411}
412
413static bool swrm_is_port_en(struct swr_master *mstr)
414{
415 return !!(mstr->num_port);
416}
417
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530418static void copy_port_tables(struct swr_mstr_ctrl *swrm,
419 struct port_params *params)
420{
421 u8 i;
422 struct port_params *config = params;
423
424 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
425 /* wsa uses single frame structure for all configurations */
426 if (!swrm->mport_cfg[i].port_en)
427 continue;
428 swrm->mport_cfg[i].sinterval = config[i].si;
429 swrm->mport_cfg[i].offset1 = config[i].off1;
430 swrm->mport_cfg[i].offset2 = config[i].off2;
431 swrm->mport_cfg[i].hstart = config[i].hstart;
432 swrm->mport_cfg[i].hstop = config[i].hstop;
433 swrm->mport_cfg[i].blk_pack_mode = config[i].bp_mode;
434 swrm->mport_cfg[i].blk_grp_count = config[i].bgp_ctrl;
435 swrm->mport_cfg[i].word_length = config[i].wd_len;
436 swrm->mport_cfg[i].lane_ctrl = config[i].lane_ctrl;
437 }
438}
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530439static int swrm_get_port_config(struct swr_mstr_ctrl *swrm)
440{
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530441 struct port_params *params;
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530442 u32 usecase = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530443
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530444 /* TODO - Send usecase information to avoid checking for master_id */
445 if (swrm->mport_cfg[SWRM_DSD_PARAMS_PORT].port_en &&
446 (swrm->master_id == MASTER_ID_RX))
447 usecase = 1;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530448
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530449 params = swrm->port_param[usecase];
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530450 copy_port_tables(swrm, params);
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530451
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530452 return 0;
453}
454
455static int swrm_get_master_port(struct swr_mstr_ctrl *swrm, u8 *mstr_port_id,
456 u8 *mstr_ch_mask, u8 mstr_prt_type,
457 u8 slv_port_id)
458{
459 int i, j;
460 *mstr_port_id = 0;
461
462 for (i = 1; i <= swrm->num_ports; i++) {
463 for (j = 0; j < SWR_MAX_CH_PER_PORT; j++) {
464 if (swrm->port_mapping[i][j].port_type == mstr_prt_type)
465 goto found;
466 }
467 }
468found:
469 if (i > swrm->num_ports || j == SWR_MAX_CH_PER_PORT) {
470 dev_err(swrm->dev, "%s: port type not supported by master\n",
471 __func__);
472 return -EINVAL;
473 }
474 /* id 0 corresponds to master port 1 */
475 *mstr_port_id = i - 1;
476 *mstr_ch_mask = swrm->port_mapping[i][j].ch_mask;
477
478 return 0;
479
480}
481
482static u32 swrm_get_packed_reg_val(u8 *cmd_id, u8 cmd_data,
483 u8 dev_addr, u16 reg_addr)
484{
485 u32 val;
486 u8 id = *cmd_id;
487
488 if (id != SWR_BROADCAST_CMD_ID) {
489 if (id < 14)
490 id += 1;
491 else
492 id = 0;
493 *cmd_id = id;
494 }
495 val = SWR_REG_VAL_PACK(cmd_data, dev_addr, id, reg_addr);
496
497 return val;
498}
499
500static int swrm_cmd_fifo_rd_cmd(struct swr_mstr_ctrl *swrm, int *cmd_data,
501 u8 dev_addr, u8 cmd_id, u16 reg_addr,
502 u32 len)
503{
504 u32 val;
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530505 u32 retry_attempt = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530506
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530507 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530508 val = swrm_get_packed_reg_val(&swrm->rcmd_id, len, dev_addr, reg_addr);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530509 if (swrm->read) {
510 /* skip delay if read is handled in platform driver */
511 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
512 } else {
513 /* wait for FIFO RD to complete to avoid overflow */
514 usleep_range(100, 105);
515 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
516 /* wait for FIFO RD CMD complete to avoid overflow */
517 usleep_range(250, 255);
518 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530519retry_read:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530520 *cmd_data = swr_master_read(swrm, SWRM_CMD_FIFO_RD_FIFO_ADDR);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530521 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, rcmd_id: 0x%x, \
522 dev_num: 0x%x, cmd_data: 0x%x\n", __func__, reg_addr,
523 cmd_id, swrm->rcmd_id, dev_addr, *cmd_data);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530524 if ((((*cmd_data) & 0xF00) >> 8) != swrm->rcmd_id) {
525 if (retry_attempt < MAX_FIFO_RD_FAIL_RETRY) {
526 /* wait 500 us before retry on fifo read failure */
527 usleep_range(500, 505);
528 retry_attempt++;
529 goto retry_read;
530 } else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530531 dev_err_ratelimited(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, \
532 rcmd_id: 0x%x, dev_num: 0x%x, cmd_data: 0x%x\n",
533 __func__, reg_addr, cmd_id, swrm->rcmd_id,
534 dev_addr, *cmd_data);
535
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530536 dev_err_ratelimited(swrm->dev,
537 "%s: failed to read fifo\n", __func__);
538 }
539 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530540 mutex_unlock(&swrm->iolock);
541
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530542 return 0;
543}
544
545static int swrm_cmd_fifo_wr_cmd(struct swr_mstr_ctrl *swrm, u8 cmd_data,
546 u8 dev_addr, u8 cmd_id, u16 reg_addr)
547{
548 u32 val;
549 int ret = 0;
550
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530551 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530552 if (!cmd_id)
553 val = swrm_get_packed_reg_val(&swrm->wcmd_id, cmd_data,
554 dev_addr, reg_addr);
555 else
556 val = swrm_get_packed_reg_val(&cmd_id, cmd_data,
557 dev_addr, reg_addr);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530558 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x,wcmd_id: 0x%x, \
559 dev_num: 0x%x, cmd_data: 0x%x\n", __func__,
560 reg_addr, cmd_id, swrm->wcmd_id,dev_addr, cmd_data);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +0530561 swr_master_write(swrm, SWRM_CMD_FIFO_WR_CMD, val);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530562 /*
563 * wait for FIFO WR command to complete to avoid overflow
564 * skip delay if write is handled in platform driver.
565 */
566 if(!swrm->write)
567 usleep_range(250, 255);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530568 if (cmd_id == 0xF) {
569 /*
570 * sleep for 10ms for MSM soundwire variant to allow broadcast
571 * command to complete.
572 */
573 if (swrm_is_msm_variant(swrm->version))
574 usleep_range(10000, 10100);
575 else
576 wait_for_completion_timeout(&swrm->broadcast,
577 (2 * HZ/10));
578 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530579 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530580 return ret;
581}
582
583static int swrm_read(struct swr_master *master, u8 dev_num, u16 reg_addr,
584 void *buf, u32 len)
585{
586 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
587 int ret = 0;
588 int val;
589 u8 *reg_val = (u8 *)buf;
590
591 if (!swrm) {
592 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
593 return -EINVAL;
594 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530595 if (!dev_num) {
596 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
597 return -EINVAL;
598 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530599 mutex_lock(&swrm->devlock);
600 if (!swrm->dev_up) {
601 mutex_unlock(&swrm->devlock);
602 return 0;
603 }
604 mutex_unlock(&swrm->devlock);
605
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530606 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530607 ret = swrm_cmd_fifo_rd_cmd(swrm, &val, dev_num, 0, reg_addr, len);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530608
609 if (!ret)
610 *reg_val = (u8)val;
611
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530612 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530613 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530614 return ret;
615}
616
617static int swrm_write(struct swr_master *master, u8 dev_num, u16 reg_addr,
618 const void *buf)
619{
620 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
621 int ret = 0;
622 u8 reg_val = *(u8 *)buf;
623
624 if (!swrm) {
625 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
626 return -EINVAL;
627 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530628 if (!dev_num) {
629 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
630 return -EINVAL;
631 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530632 mutex_lock(&swrm->devlock);
633 if (!swrm->dev_up) {
634 mutex_unlock(&swrm->devlock);
635 return 0;
636 }
637 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530638
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530639 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530640 ret = swrm_cmd_fifo_wr_cmd(swrm, reg_val, dev_num, 0, reg_addr);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530641
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530642 pm_runtime_put_autosuspend(swrm->dev);
643 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530644 return ret;
645}
646
647static int swrm_bulk_write(struct swr_master *master, u8 dev_num, void *reg,
648 const void *buf, size_t len)
649{
650 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
651 int ret = 0;
652 int i;
653 u32 *val;
654 u32 *swr_fifo_reg;
655
656 if (!swrm || !swrm->handle) {
657 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
658 return -EINVAL;
659 }
660 if (len <= 0)
661 return -EINVAL;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530662 mutex_lock(&swrm->devlock);
663 if (!swrm->dev_up) {
664 mutex_unlock(&swrm->devlock);
665 return 0;
666 }
667 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530668
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530669 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530670 if (dev_num) {
671 swr_fifo_reg = kcalloc(len, sizeof(u32), GFP_KERNEL);
672 if (!swr_fifo_reg) {
673 ret = -ENOMEM;
674 goto err;
675 }
676 val = kcalloc(len, sizeof(u32), GFP_KERNEL);
677 if (!val) {
678 ret = -ENOMEM;
679 goto mem_fail;
680 }
681
682 for (i = 0; i < len; i++) {
683 val[i] = swrm_get_packed_reg_val(&swrm->wcmd_id,
684 ((u8 *)buf)[i],
685 dev_num,
686 ((u16 *)reg)[i]);
687 swr_fifo_reg[i] = SWRM_CMD_FIFO_WR_CMD;
688 }
689 ret = swr_master_bulk_write(swrm, swr_fifo_reg, val, len);
690 if (ret) {
691 dev_err(&master->dev, "%s: bulk write failed\n",
692 __func__);
693 ret = -EINVAL;
694 }
695 } else {
696 dev_err(&master->dev,
697 "%s: No support of Bulk write for master regs\n",
698 __func__);
699 ret = -EINVAL;
700 goto err;
701 }
702 kfree(val);
703mem_fail:
704 kfree(swr_fifo_reg);
705err:
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530706 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530707 pm_runtime_mark_last_busy(swrm->dev);
708 return ret;
709}
710
711static u8 get_inactive_bank_num(struct swr_mstr_ctrl *swrm)
712{
713 return (swr_master_read(swrm, SWRM_MCP_STATUS) &
714 SWRM_MCP_STATUS_BANK_NUM_MASK) ? 0 : 1;
715}
716
717static void enable_bank_switch(struct swr_mstr_ctrl *swrm, u8 bank,
718 u8 row, u8 col)
719{
720 swrm_cmd_fifo_wr_cmd(swrm, ((row << 3) | col), 0xF, 0xF,
721 SWRS_SCP_FRAME_CTRL_BANK(bank));
722}
723
724static struct swr_port_info *swrm_get_port_req(struct swrm_mports *mport,
725 u8 slv_port, u8 dev_num)
726{
727 struct swr_port_info *port_req = NULL;
728
729 list_for_each_entry(port_req, &mport->port_req_list, list) {
730 /* Store dev_id instead of dev_num if enumeration is changed run_time */
731 if ((port_req->slave_port_id == slv_port)
732 && (port_req->dev_num == dev_num))
733 return port_req;
734 }
735 return NULL;
736}
737
738static bool swrm_remove_from_group(struct swr_master *master)
739{
740 struct swr_device *swr_dev;
741 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
742 bool is_removed = false;
743
744 if (!swrm)
745 goto end;
746
747 mutex_lock(&swrm->mlock);
748 if ((swrm->num_rx_chs > 1) &&
749 (swrm->num_rx_chs == swrm->num_cfg_devs)) {
750 list_for_each_entry(swr_dev, &master->devices,
751 dev_list) {
752 swr_dev->group_id = SWR_GROUP_NONE;
753 master->gr_sid = 0;
754 }
755 is_removed = true;
756 }
757 mutex_unlock(&swrm->mlock);
758
759end:
760 return is_removed;
761}
762
763static void swrm_disable_ports(struct swr_master *master,
764 u8 bank)
765{
766 u32 value;
767 struct swr_port_info *port_req;
768 int i;
769 struct swrm_mports *mport;
770 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
771
772 if (!swrm) {
773 pr_err("%s: swrm is null\n", __func__);
774 return;
775 }
776
777 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
778 master->num_port);
779
780
781 for (i = 0; i < SWR_MSTR_PORT_LEN ; i++) {
782
783 mport = &(swrm->mport_cfg[i]);
784 if (!mport->port_en)
785 continue;
786
787 list_for_each_entry(port_req, &mport->port_req_list, list) {
788 /* skip ports with no change req's*/
789 if (port_req->req_ch == port_req->ch_en)
790 continue;
791
792 swrm_cmd_fifo_wr_cmd(swrm, port_req->req_ch,
793 port_req->dev_num, 0x00,
794 SWRS_DP_CHANNEL_ENABLE_BANK(port_req->slave_port_id,
795 bank));
796 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x\n",
797 __func__, i,
798 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)));
799 }
800 value = ((mport->req_ch)
801 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
802 value |= ((mport->offset2)
803 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
804 value |= ((mport->offset1)
805 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
806 value |= mport->sinterval;
807
808 swr_master_write(swrm,
809 SWRM_DP_PORT_CTRL_BANK(i+1, bank),
810 value);
811 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
812 __func__, i,
813 (SWRM_DP_PORT_CTRL_BANK(i+1, bank)), value);
814 }
815}
816
817static void swrm_cleanup_disabled_port_reqs(struct swr_master *master)
818{
819 struct swr_port_info *port_req, *next;
820 int i;
821 struct swrm_mports *mport;
822 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
823
824 if (!swrm) {
825 pr_err("%s: swrm is null\n", __func__);
826 return;
827 }
828 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
829 master->num_port);
830
831 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
832 mport = &(swrm->mport_cfg[i]);
833 list_for_each_entry_safe(port_req, next,
834 &mport->port_req_list, list) {
835 /* skip ports without new ch req */
836 if (port_req->ch_en == port_req->req_ch)
837 continue;
838
839 /* remove new ch req's*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +0530840 port_req->ch_en = port_req->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530841
842 /* If no streams enabled on port, remove the port req */
843 if (port_req->ch_en == 0) {
844 list_del(&port_req->list);
845 kfree(port_req);
846 }
847 }
848 /* remove new ch req's on mport*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +0530849 mport->ch_en = mport->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530850
851 if (!(mport->ch_en)) {
852 mport->port_en = false;
853 master->port_en_mask &= ~i;
854 }
855 }
856}
857static void swrm_copy_data_port_config(struct swr_master *master, u8 bank)
858{
859 u32 value, slv_id;
860 struct swr_port_info *port_req;
861 int i;
862 struct swrm_mports *mport;
863 u32 reg[SWRM_MAX_PORT_REG];
864 u32 val[SWRM_MAX_PORT_REG];
865 int len = 0;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530866 u8 hparams;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530867 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
868
869 if (!swrm) {
870 pr_err("%s: swrm is null\n", __func__);
871 return;
872 }
873
874 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
875 master->num_port);
876
877 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
878 mport = &(swrm->mport_cfg[i]);
879 if (!mport->port_en)
880 continue;
881
882 list_for_each_entry(port_req, &mport->port_req_list, list) {
883 slv_id = port_req->slave_port_id;
884 reg[len] = SWRM_CMD_FIFO_WR_CMD;
885 val[len++] = SWR_REG_VAL_PACK(port_req->req_ch,
886 port_req->dev_num, 0x00,
887 SWRS_DP_CHANNEL_ENABLE_BANK(slv_id,
888 bank));
889
890 reg[len] = SWRM_CMD_FIFO_WR_CMD;
891 val[len++] = SWR_REG_VAL_PACK(mport->sinterval,
892 port_req->dev_num, 0x00,
893 SWRS_DP_SAMPLE_CONTROL_1_BANK(slv_id,
894 bank));
895
896 reg[len] = SWRM_CMD_FIFO_WR_CMD;
897 val[len++] = SWR_REG_VAL_PACK(mport->offset1,
898 port_req->dev_num, 0x00,
899 SWRS_DP_OFFSET_CONTROL_1_BANK(slv_id,
900 bank));
901
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530902 if (mport->offset2 != SWR_INVALID_PARAM) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530903 reg[len] = SWRM_CMD_FIFO_WR_CMD;
904 val[len++] = SWR_REG_VAL_PACK(mport->offset2,
905 port_req->dev_num, 0x00,
906 SWRS_DP_OFFSET_CONTROL_2_BANK(
907 slv_id, bank));
908 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530909 if (mport->hstart != SWR_INVALID_PARAM
910 && mport->hstop != SWR_INVALID_PARAM) {
911 hparams = (mport->hstart << 4) | mport->hstop;
912
913 reg[len] = SWRM_CMD_FIFO_WR_CMD;
914 val[len++] = SWR_REG_VAL_PACK(hparams,
915 port_req->dev_num, 0x00,
916 SWRS_DP_HCONTROL_BANK(slv_id,
917 bank));
918 }
919 if (mport->word_length != SWR_INVALID_PARAM) {
920 reg[len] = SWRM_CMD_FIFO_WR_CMD;
921 val[len++] =
922 SWR_REG_VAL_PACK(mport->word_length,
923 port_req->dev_num, 0x00,
924 SWRS_DP_BLOCK_CONTROL_1(slv_id));
925 }
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +0530926 if (mport->blk_pack_mode != SWR_INVALID_PARAM
927 && swrm->master_id != MASTER_ID_WSA) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530928 reg[len] = SWRM_CMD_FIFO_WR_CMD;
929 val[len++] =
930 SWR_REG_VAL_PACK(mport->blk_pack_mode,
931 port_req->dev_num, 0x00,
932 SWRS_DP_BLOCK_CONTROL_3_BANK(slv_id,
933 bank));
934 }
935 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
936 reg[len] = SWRM_CMD_FIFO_WR_CMD;
937 val[len++] =
938 SWR_REG_VAL_PACK(mport->blk_grp_count,
939 port_req->dev_num, 0x00,
940 SWRS_DP_BLOCK_CONTROL_2_BANK(slv_id,
941 bank));
942 }
943 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
944 reg[len] = SWRM_CMD_FIFO_WR_CMD;
945 val[len++] =
946 SWR_REG_VAL_PACK(mport->lane_ctrl,
947 port_req->dev_num, 0x00,
948 SWRS_DP_LANE_CONTROL_BANK(slv_id,
949 bank));
950 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530951 port_req->ch_en = port_req->req_ch;
952 }
953 value = ((mport->req_ch)
954 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +0530955
956 if (mport->offset2 != SWR_INVALID_PARAM)
957 value |= ((mport->offset2)
958 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530959 value |= ((mport->offset1)
960 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
961 value |= mport->sinterval;
962
963
964 reg[len] = SWRM_DP_PORT_CTRL_BANK(i + 1, bank);
965 val[len++] = value;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530966 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
967 __func__, i,
968 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)), value);
969
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530970 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
971 reg[len] = SWRM_DP_PORT_CTRL_2_BANK(i + 1, bank);
972 val[len++] = mport->lane_ctrl;
973 }
974 if (mport->word_length != SWR_INVALID_PARAM) {
975 reg[len] = SWRM_DP_BLOCK_CTRL_1(i + 1);
976 val[len++] = mport->word_length;
977 }
978
979 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
980 reg[len] = SWRM_DP_BLOCK_CTRL2_BANK(i + 1, bank);
981 val[len++] = mport->blk_grp_count;
982 }
983 if (mport->hstart != SWR_INVALID_PARAM
984 && mport->hstop != SWR_INVALID_PARAM) {
985 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
Laxminath Kasame30eef72018-11-05 17:40:09 +0530986 hparams = (mport->hstop << 4) | mport->hstart;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530987 val[len++] = hparams;
Laxminath Kasam990c70b2018-11-09 23:15:09 +0530988 } else {
989 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
990 hparams = (SWR_HSTOP_MAX_VAL << 4) | SWR_HSTART_MIN_VAL;
991 val[len++] = hparams;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530992 }
993 if (mport->blk_pack_mode != SWR_INVALID_PARAM) {
994 reg[len] = SWRM_DP_BLOCK_CTRL3_BANK(i + 1, bank);
995 val[len++] = mport->blk_pack_mode;
996 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530997 mport->ch_en = mport->req_ch;
998
999 }
Sudheer Papothi0016db12019-06-11 04:42:38 +05301000 swrm_reg_dump(swrm, reg, val, len, __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301001 swr_master_bulk_write(swrm, reg, val, len);
1002}
1003
1004static void swrm_apply_port_config(struct swr_master *master)
1005{
1006 u8 bank;
1007 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1008
1009 if (!swrm) {
1010 pr_err("%s: Invalid handle to swr controller\n",
1011 __func__);
1012 return;
1013 }
1014
1015 bank = get_inactive_bank_num(swrm);
1016 dev_dbg(swrm->dev, "%s: enter bank: %d master_ports: %d\n",
1017 __func__, bank, master->num_port);
1018
1019
1020 swrm_cmd_fifo_wr_cmd(swrm, 0x01, 0xF, 0x00,
1021 SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(bank));
1022
1023 swrm_copy_data_port_config(master, bank);
1024}
1025
1026static int swrm_slvdev_datapath_control(struct swr_master *master, bool enable)
1027{
1028 u8 bank;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301029 u32 value, n_row, n_col;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301030 int ret;
1031 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1032 int mask = (SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK |
1033 SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK |
1034 SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_BMSK);
1035 u8 inactive_bank;
1036
1037 if (!swrm) {
1038 pr_err("%s: swrm is null\n", __func__);
1039 return -EFAULT;
1040 }
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301041
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301042 mutex_lock(&swrm->mlock);
1043
Ramprasad Katkam979b7c92019-05-17 15:31:21 +05301044 /*
1045 * During disable if master is already down, which implies an ssr/pdr
1046 * scenario, just mark ports as disabled and exit
1047 */
1048 if (swrm->state == SWR_MSTR_SSR && !enable) {
1049 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1050 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1051 __func__);
1052 goto exit;
1053 }
1054 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
1055 swrm_cleanup_disabled_port_reqs(master);
1056 if (!swrm_is_port_en(master)) {
1057 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1058 __func__);
1059 pm_runtime_mark_last_busy(swrm->dev);
1060 pm_runtime_put_autosuspend(swrm->dev);
1061 }
1062 goto exit;
1063 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301064 bank = get_inactive_bank_num(swrm);
1065
1066 if (enable) {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301067 if (!test_bit(ENABLE_PENDING, &swrm->port_req_pending)) {
1068 dev_dbg(swrm->dev, "%s:No pending connect port req\n",
1069 __func__);
1070 goto exit;
1071 }
1072 clear_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301073 ret = swrm_get_port_config(swrm);
1074 if (ret) {
1075 /* cannot accommodate ports */
1076 swrm_cleanup_disabled_port_reqs(master);
1077 mutex_unlock(&swrm->mlock);
1078 return -EINVAL;
1079 }
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301080 swr_master_write(swrm, SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301081 SWRM_INTERRUPT_STATUS_MASK);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301082 /* apply the new port config*/
1083 swrm_apply_port_config(master);
1084 } else {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301085 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1086 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1087 __func__);
1088 goto exit;
1089 }
1090 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301091 swrm_disable_ports(master, bank);
1092 }
1093 dev_dbg(swrm->dev, "%s: enable: %d, cfg_devs: %d\n",
1094 __func__, enable, swrm->num_cfg_devs);
1095
1096 if (enable) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301097 /* set col = 16 */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301098 n_col = SWR_MAX_COL;
1099 } else {
1100 /*
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301101 * Do not change to col = 2 if there are still active ports
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301102 */
1103 if (!master->num_port)
1104 n_col = SWR_MIN_COL;
1105 else
1106 n_col = SWR_MAX_COL;
1107 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301108 /* Use default 50 * x, frame shape. Change based on mclk */
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301109 if (swrm->mclk_freq == MCLK_FREQ_NATIVE) {
1110 dev_dbg(swrm->dev, "setting 64 x %d frameshape\n",
1111 n_col ? 16 : 2);
1112 n_row = SWR_ROW_64;
1113 } else {
1114 dev_dbg(swrm->dev, "setting 50 x %d frameshape\n",
1115 n_col ? 16 : 2);
1116 n_row = SWR_ROW_50;
1117 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301118 value = swr_master_read(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank));
1119 value &= (~mask);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301120 value |= ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301121 (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
1122 (0 << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
1123 swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1124
1125 dev_dbg(swrm->dev, "%s: regaddr: 0x%x, value: 0x%x\n", __func__,
1126 SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1127
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301128 enable_bank_switch(swrm, bank, n_row, n_col);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301129 inactive_bank = bank ? 0 : 1;
1130
1131 if (enable)
1132 swrm_copy_data_port_config(master, inactive_bank);
1133 else {
1134 swrm_disable_ports(master, inactive_bank);
1135 swrm_cleanup_disabled_port_reqs(master);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301136 }
1137 if (!swrm_is_port_en(master)) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301138 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1139 __func__);
1140 pm_runtime_mark_last_busy(swrm->dev);
1141 pm_runtime_put_autosuspend(swrm->dev);
1142 }
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301143exit:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301144 mutex_unlock(&swrm->mlock);
1145return 0;
1146}
1147
1148static int swrm_connect_port(struct swr_master *master,
1149 struct swr_params *portinfo)
1150{
1151 int i;
1152 struct swr_port_info *port_req;
1153 int ret = 0;
1154 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1155 struct swrm_mports *mport;
1156 u8 mstr_port_id, mstr_ch_msk;
1157
1158 dev_dbg(&master->dev, "%s: enter\n", __func__);
1159 if (!portinfo)
1160 return -EINVAL;
1161
1162 if (!swrm) {
1163 dev_err(&master->dev,
1164 "%s: Invalid handle to swr controller\n",
1165 __func__);
1166 return -EINVAL;
1167 }
1168
1169 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301170 mutex_lock(&swrm->devlock);
1171 if (!swrm->dev_up) {
1172 mutex_unlock(&swrm->devlock);
1173 mutex_unlock(&swrm->mlock);
1174 return -EINVAL;
1175 }
1176 mutex_unlock(&swrm->devlock);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301177 if (!swrm_is_port_en(master))
1178 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301179
1180 for (i = 0; i < portinfo->num_port; i++) {
1181 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_msk,
1182 portinfo->port_type[i],
1183 portinfo->port_id[i]);
1184 if (ret) {
1185 dev_err(&master->dev,
1186 "%s: mstr portid for slv port %d not found\n",
1187 __func__, portinfo->port_id[i]);
1188 goto port_fail;
1189 }
1190
1191 mport = &(swrm->mport_cfg[mstr_port_id]);
1192 /* get port req */
1193 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1194 portinfo->dev_num);
1195 if (!port_req) {
1196 dev_dbg(&master->dev, "%s: new req:port id %d dev %d\n",
1197 __func__, portinfo->port_id[i],
1198 portinfo->dev_num);
1199 port_req = kzalloc(sizeof(struct swr_port_info),
1200 GFP_KERNEL);
1201 if (!port_req) {
1202 ret = -ENOMEM;
1203 goto mem_fail;
1204 }
1205 port_req->dev_num = portinfo->dev_num;
1206 port_req->slave_port_id = portinfo->port_id[i];
1207 port_req->num_ch = portinfo->num_ch[i];
1208 port_req->ch_rate = portinfo->ch_rate[i];
1209 port_req->ch_en = 0;
1210 port_req->master_port_id = mstr_port_id;
1211 list_add(&port_req->list, &mport->port_req_list);
1212 }
1213 port_req->req_ch |= portinfo->ch_en[i];
1214
1215 dev_dbg(&master->dev,
1216 "%s: mstr port %d, slv port %d ch_rate %d num_ch %d\n",
1217 __func__, port_req->master_port_id,
1218 port_req->slave_port_id, port_req->ch_rate,
1219 port_req->num_ch);
1220 /* Put the port req on master port */
1221 mport = &(swrm->mport_cfg[mstr_port_id]);
1222 mport->port_en = true;
1223 mport->req_ch |= mstr_ch_msk;
1224 master->port_en_mask |= (1 << mstr_port_id);
1225 }
1226 master->num_port += portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301227 set_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301228 swr_port_response(master, portinfo->tid);
1229
1230 mutex_unlock(&swrm->mlock);
1231 return 0;
1232
1233port_fail:
1234mem_fail:
1235 /* cleanup port reqs in error condition */
1236 swrm_cleanup_disabled_port_reqs(master);
1237 mutex_unlock(&swrm->mlock);
1238 return ret;
1239}
1240
1241static int swrm_disconnect_port(struct swr_master *master,
1242 struct swr_params *portinfo)
1243{
1244 int i, ret = 0;
1245 struct swr_port_info *port_req;
1246 struct swrm_mports *mport;
1247 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1248 u8 mstr_port_id, mstr_ch_mask;
1249
1250 if (!swrm) {
1251 dev_err(&master->dev,
1252 "%s: Invalid handle to swr controller\n",
1253 __func__);
1254 return -EINVAL;
1255 }
1256
1257 if (!portinfo) {
1258 dev_err(&master->dev, "%s: portinfo is NULL\n", __func__);
1259 return -EINVAL;
1260 }
1261 mutex_lock(&swrm->mlock);
1262
1263 for (i = 0; i < portinfo->num_port; i++) {
1264
1265 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_mask,
1266 portinfo->port_type[i], portinfo->port_id[i]);
1267 if (ret) {
1268 dev_err(&master->dev,
1269 "%s: mstr portid for slv port %d not found\n",
1270 __func__, portinfo->port_id[i]);
1271 mutex_unlock(&swrm->mlock);
1272 return -EINVAL;
1273 }
1274 mport = &(swrm->mport_cfg[mstr_port_id]);
1275 /* get port req */
1276 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1277 portinfo->dev_num);
1278
1279 if (!port_req) {
1280 dev_err(&master->dev, "%s:port not enabled : port %d\n",
1281 __func__, portinfo->port_id[i]);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05301282 mutex_unlock(&swrm->mlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301283 return -EINVAL;
1284 }
1285 port_req->req_ch &= ~portinfo->ch_en[i];
1286 mport->req_ch &= ~mstr_ch_mask;
1287 }
1288 master->num_port -= portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301289 set_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301290 swr_port_response(master, portinfo->tid);
1291 mutex_unlock(&swrm->mlock);
1292
1293 return 0;
1294}
1295
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301296static int swrm_find_alert_slave(struct swr_mstr_ctrl *swrm,
1297 int status, u8 *devnum)
1298{
1299 int i;
1300 bool found = false;
1301
1302 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1303 if ((status & SWRM_MCP_SLV_STATUS_MASK) == SWR_ALERT) {
1304 *devnum = i;
1305 found = true;
1306 break;
1307 }
1308 status >>= 2;
1309 }
1310 if (found)
1311 return 0;
1312 else
1313 return -EINVAL;
1314}
1315
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301316static void swrm_enable_slave_irq(struct swr_mstr_ctrl *swrm)
1317{
1318 int i;
1319 int status = 0;
1320
1321 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1322 if (!status) {
1323 dev_dbg_ratelimited(swrm->dev, "%s: slaves status is 0x%x\n",
1324 __func__, status);
1325 return;
1326 }
1327 dev_dbg(swrm->dev, "%s: slave status: 0x%x\n", __func__, status);
1328 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1329 if (status & SWRM_MCP_SLV_STATUS_MASK)
1330 swrm_cmd_fifo_wr_cmd(swrm, 0x4, i, 0x0,
1331 SWRS_SCP_INT_STATUS_MASK_1);
1332 status >>= 2;
1333 }
1334}
1335
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301336static int swrm_check_slave_change_status(struct swr_mstr_ctrl *swrm,
1337 int status, u8 *devnum)
1338{
1339 int i;
1340 int new_sts = status;
1341 int ret = SWR_NOT_PRESENT;
1342
1343 if (status != swrm->slave_status) {
1344 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1345 if ((status & SWRM_MCP_SLV_STATUS_MASK) !=
1346 (swrm->slave_status & SWRM_MCP_SLV_STATUS_MASK)) {
1347 ret = (status & SWRM_MCP_SLV_STATUS_MASK);
1348 *devnum = i;
1349 break;
1350 }
1351 status >>= 2;
1352 swrm->slave_status >>= 2;
1353 }
1354 swrm->slave_status = new_sts;
1355 }
1356 return ret;
1357}
1358
1359static irqreturn_t swr_mstr_interrupt(int irq, void *dev)
1360{
1361 struct swr_mstr_ctrl *swrm = dev;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301362 u32 value, intr_sts, intr_sts_masked;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301363 u32 temp = 0;
1364 u32 status, chg_sts, i;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301365 u8 devnum = 0;
1366 int ret = IRQ_HANDLED;
1367 struct swr_device *swr_dev;
1368 struct swr_master *mstr = &swrm->master;
1369
Ramprasad Katkam57349872018-11-11 18:34:57 +05301370 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1371 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1372 return IRQ_NONE;
1373 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301374
1375 mutex_lock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301376 if (swrm_clk_request(swrm, true)) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +05301377 dev_err_ratelimited(swrm->dev, "%s:clk request failed\n",
1378 __func__);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301379 mutex_unlock(&swrm->reslock);
1380 goto exit;
1381 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301382 mutex_unlock(&swrm->reslock);
1383
1384 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301385 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301386handle_irq:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301387 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301388 value = intr_sts_masked & (1 << i);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301389 if (!value)
1390 continue;
1391
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301392 switch (value) {
1393 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1394 dev_dbg(swrm->dev, "Trigger irq to slave device\n");
1395 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301396 ret = swrm_find_alert_slave(swrm, status, &devnum);
1397 if (ret) {
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301398 dev_err_ratelimited(swrm->dev,
1399 "no slave alert found.spurious interrupt\n");
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05301400 break;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301401 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301402 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1403 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1404 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1405 SWRS_SCP_INT_STATUS_CLEAR_1);
1406 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1407 SWRS_SCP_INT_STATUS_CLEAR_1);
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301408
1409
1410 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1411 if (swr_dev->dev_num != devnum)
1412 continue;
1413 if (swr_dev->slave_irq) {
1414 do {
1415 handle_nested_irq(
1416 irq_find_mapping(
1417 swr_dev->slave_irq, 0));
1418 } while (swr_dev->slave_irq_pending);
1419 }
1420
1421 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301422 break;
1423 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1424 dev_dbg(swrm->dev, "SWR new slave attached\n");
1425 break;
1426 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1427 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1428 if (status == swrm->slave_status) {
1429 dev_dbg(swrm->dev,
1430 "%s: No change in slave status: %d\n",
1431 __func__, status);
1432 break;
1433 }
1434 chg_sts = swrm_check_slave_change_status(swrm, status,
1435 &devnum);
1436 switch (chg_sts) {
1437 case SWR_NOT_PRESENT:
1438 dev_dbg(swrm->dev, "device %d got detached\n",
1439 devnum);
1440 break;
1441 case SWR_ATTACHED_OK:
1442 dev_dbg(swrm->dev, "device %d got attached\n",
1443 devnum);
Ramprasad Katkamdebe8932018-09-25 18:08:18 +05301444 /* enable host irq from slave device*/
1445 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1446 SWRS_SCP_INT_STATUS_CLEAR_1);
1447 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1448 SWRS_SCP_INT_STATUS_MASK_1);
1449
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301450 break;
1451 case SWR_ALERT:
1452 dev_dbg(swrm->dev,
1453 "device %d has pending interrupt\n",
1454 devnum);
1455 break;
1456 }
1457 break;
1458 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1459 dev_err_ratelimited(swrm->dev,
1460 "SWR bus clsh detected\n");
1461 break;
1462 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1463 dev_dbg(swrm->dev, "SWR read FIFO overflow\n");
1464 break;
1465 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1466 dev_dbg(swrm->dev, "SWR read FIFO underflow\n");
1467 break;
1468 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1469 dev_dbg(swrm->dev, "SWR write FIFO overflow\n");
1470 break;
1471 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1472 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1473 dev_err_ratelimited(swrm->dev,
1474 "SWR CMD error, fifo status 0x%x, flushing fifo\n",
1475 value);
1476 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1477 break;
1478 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301479 dev_err_ratelimited(swrm->dev, "SWR Port collision detected\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301480 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301481 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301482 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301483 break;
1484 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1485 dev_dbg(swrm->dev, "SWR read enable valid mismatch\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301486 swrm->intr_mask &=
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301487 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1488 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301489 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301490 break;
1491 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1492 complete(&swrm->broadcast);
1493 dev_dbg(swrm->dev, "SWR cmd id finished\n");
1494 break;
1495 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_AUTO_ENUM_FINISHED:
1496 break;
1497 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED:
1498 break;
1499 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL:
1500 break;
1501 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED:
1502 complete(&swrm->reset);
1503 break;
1504 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED:
1505 break;
1506 default:
1507 dev_err_ratelimited(swrm->dev,
1508 "SWR unknown interrupt\n");
1509 ret = IRQ_NONE;
1510 break;
1511 }
1512 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301513 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1514 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
Ramprasad Katkam83303512018-10-11 17:34:22 +05301515
1516 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301517 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301518
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301519 if (intr_sts_masked) {
Ramprasad Katkam83303512018-10-11 17:34:22 +05301520 dev_dbg(swrm->dev, "%s: new interrupt received\n", __func__);
1521 goto handle_irq;
1522 }
1523
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301524 mutex_lock(&swrm->reslock);
1525 swrm_clk_request(swrm, false);
1526 mutex_unlock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301527exit:
Ramprasad Katkam57349872018-11-11 18:34:57 +05301528 swrm_unlock_sleep(swrm);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301529 return ret;
1530}
1531
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301532static irqreturn_t swr_mstr_interrupt_v2(int irq, void *dev)
1533{
1534 struct swr_mstr_ctrl *swrm = dev;
1535 u32 value, intr_sts, intr_sts_masked;
1536 u32 temp = 0;
1537 u32 status, chg_sts, i;
1538 u8 devnum = 0;
1539 int ret = IRQ_HANDLED;
1540 struct swr_device *swr_dev;
1541 struct swr_master *mstr = &swrm->master;
1542
1543 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1544 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1545 return IRQ_NONE;
1546 }
1547
1548 mutex_lock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301549 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
1550 ret = IRQ_NONE;
1551 goto exit;
1552 }
1553 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
1554 ret = IRQ_NONE;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301555 goto err_audio_hw_vote;
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001556 }
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301557 swrm_clk_request(swrm, true);
1558 mutex_unlock(&swrm->reslock);
1559
1560 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1561 intr_sts_masked = intr_sts & swrm->intr_mask;
Sudheer Papothi06f43412019-07-09 03:32:54 +05301562
1563 dev_dbg(swrm->dev, "%s: status: 0x%x \n", __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301564handle_irq:
1565 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
1566 value = intr_sts_masked & (1 << i);
1567 if (!value)
1568 continue;
1569
1570 switch (value) {
1571 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1572 dev_dbg(swrm->dev, "%s: Trigger irq to slave device\n",
1573 __func__);
1574 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1575 ret = swrm_find_alert_slave(swrm, status, &devnum);
1576 if (ret) {
1577 dev_err_ratelimited(swrm->dev,
1578 "%s: no slave alert found.spurious interrupt\n",
1579 __func__);
1580 break;
1581 }
1582 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1583 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1584 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1585 SWRS_SCP_INT_STATUS_CLEAR_1);
1586 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1587 SWRS_SCP_INT_STATUS_CLEAR_1);
1588
1589
1590 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1591 if (swr_dev->dev_num != devnum)
1592 continue;
1593 if (swr_dev->slave_irq) {
1594 do {
1595 handle_nested_irq(
1596 irq_find_mapping(
1597 swr_dev->slave_irq, 0));
1598 } while (swr_dev->slave_irq_pending);
1599 }
1600
1601 }
1602 break;
1603 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1604 dev_dbg(swrm->dev, "%s: SWR new slave attached\n",
1605 __func__);
1606 break;
1607 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1608 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1609 if (status == swrm->slave_status) {
1610 dev_dbg(swrm->dev,
1611 "%s: No change in slave status: %d\n",
1612 __func__, status);
1613 break;
1614 }
1615 chg_sts = swrm_check_slave_change_status(swrm, status,
1616 &devnum);
1617 switch (chg_sts) {
1618 case SWR_NOT_PRESENT:
1619 dev_dbg(swrm->dev,
1620 "%s: device %d got detached\n",
1621 __func__, devnum);
1622 break;
1623 case SWR_ATTACHED_OK:
1624 dev_dbg(swrm->dev,
1625 "%s: device %d got attached\n",
1626 __func__, devnum);
1627 /* enable host irq from slave device*/
1628 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1629 SWRS_SCP_INT_STATUS_CLEAR_1);
1630 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1631 SWRS_SCP_INT_STATUS_MASK_1);
1632
1633 break;
1634 case SWR_ALERT:
1635 dev_dbg(swrm->dev,
1636 "%s: device %d has pending interrupt\n",
1637 __func__, devnum);
1638 break;
1639 }
1640 break;
1641 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1642 dev_err_ratelimited(swrm->dev,
1643 "%s: SWR bus clsh detected\n",
1644 __func__);
1645 break;
1646 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1647 dev_dbg(swrm->dev, "%s: SWR read FIFO overflow\n",
1648 __func__);
1649 break;
1650 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1651 dev_dbg(swrm->dev, "%s: SWR read FIFO underflow\n",
1652 __func__);
1653 break;
1654 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1655 dev_dbg(swrm->dev, "%s: SWR write FIFO overflow\n",
1656 __func__);
1657 break;
1658 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1659 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1660 dev_err_ratelimited(swrm->dev,
1661 "%s: SWR CMD error, fifo status 0x%x, flushing fifo\n",
1662 __func__, value);
1663 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1664 break;
1665 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
1666 dev_err_ratelimited(swrm->dev,
1667 "%s: SWR Port collision detected\n",
1668 __func__);
1669 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
1670 swr_master_write(swrm,
1671 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1672 break;
1673 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1674 dev_dbg(swrm->dev,
1675 "%s: SWR read enable valid mismatch\n",
1676 __func__);
1677 swrm->intr_mask &=
1678 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1679 swr_master_write(swrm,
1680 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1681 break;
1682 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1683 complete(&swrm->broadcast);
1684 dev_dbg(swrm->dev, "%s: SWR cmd id finished\n",
1685 __func__);
1686 break;
1687 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED_V2:
1688 break;
1689 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL_V2:
1690 break;
1691 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED_V2:
1692 break;
1693 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED_V2:
1694 break;
1695 case SWRM_INTERRUPT_STATUS_EXT_CLK_STOP_WAKEUP:
1696 if (swrm->state == SWR_MSTR_UP)
1697 dev_dbg(swrm->dev,
1698 "%s:SWR Master is already up\n",
1699 __func__);
1700 else
1701 dev_err_ratelimited(swrm->dev,
1702 "%s: SWR wokeup during clock stop\n",
1703 __func__);
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301704 /* It might be possible the slave device gets reset
1705 * and slave interrupt gets missed. So re-enable
1706 * Host IRQ and process slave pending
1707 * interrupts, if any.
1708 */
1709 swrm_enable_slave_irq(swrm);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301710 break;
1711 default:
1712 dev_err_ratelimited(swrm->dev,
1713 "%s: SWR unknown interrupt value: %d\n",
1714 __func__, value);
1715 ret = IRQ_NONE;
1716 break;
1717 }
1718 }
1719 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1720 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
1721
1722 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1723 intr_sts_masked = intr_sts & swrm->intr_mask;
1724
1725 if (intr_sts_masked) {
Sudheer Papothi07d5afc2019-07-17 06:25:45 +05301726 dev_dbg(swrm->dev, "%s: new interrupt received 0x%x\n",
1727 __func__, intr_sts_masked);
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301728 goto handle_irq;
1729 }
1730
1731 mutex_lock(&swrm->reslock);
1732 swrm_clk_request(swrm, false);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301733 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
Sudheer Papothi06f43412019-07-09 03:32:54 +05301734
1735err_audio_hw_vote:
Sudheer Papothi384addd2019-06-14 02:26:52 +05301736 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001737exit:
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301738 mutex_unlock(&swrm->reslock);
1739 swrm_unlock_sleep(swrm);
1740 return ret;
1741}
1742
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301743static irqreturn_t swrm_wakeup_interrupt(int irq, void *dev)
1744{
1745 struct swr_mstr_ctrl *swrm = dev;
1746 int ret = IRQ_HANDLED;
1747
1748 if (!swrm || !(swrm->dev)) {
1749 pr_err("%s: swrm or dev is null\n", __func__);
1750 return IRQ_NONE;
1751 }
1752 mutex_lock(&swrm->devlock);
1753 if (!swrm->dev_up) {
1754 if (swrm->wake_irq > 0)
1755 disable_irq_nosync(swrm->wake_irq);
1756 mutex_unlock(&swrm->devlock);
1757 return ret;
1758 }
1759 mutex_unlock(&swrm->devlock);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301760 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1761 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1762 goto exit;
1763 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301764 if (swrm->wake_irq > 0)
1765 disable_irq_nosync(swrm->wake_irq);
1766 pm_runtime_get_sync(swrm->dev);
1767 pm_runtime_mark_last_busy(swrm->dev);
1768 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301769 swrm_unlock_sleep(swrm);
1770exit:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301771 return ret;
1772}
1773
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301774static void swrm_wakeup_work(struct work_struct *work)
1775{
1776 struct swr_mstr_ctrl *swrm;
1777
1778 swrm = container_of(work, struct swr_mstr_ctrl,
1779 wakeup_work);
1780 if (!swrm || !(swrm->dev)) {
1781 pr_err("%s: swrm or dev is null\n", __func__);
1782 return;
1783 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301784
1785 mutex_lock(&swrm->devlock);
1786 if (!swrm->dev_up) {
1787 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301788 goto exit;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301789 }
1790 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301791 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1792 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1793 goto exit;
1794 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301795 pm_runtime_get_sync(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301796 pm_runtime_mark_last_busy(swrm->dev);
1797 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301798 swrm_unlock_sleep(swrm);
1799exit:
1800 pm_relax(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301801}
1802
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301803static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
1804{
1805 u32 val;
1806
1807 swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1808 val = (swrm->slave_status >> (devnum * 2));
1809 val &= SWRM_MCP_SLV_STATUS_MASK;
1810 return val;
1811}
1812
1813static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
1814 u8 *dev_num)
1815{
1816 int i;
1817 u64 id = 0;
1818 int ret = -EINVAL;
1819 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1820 struct swr_device *swr_dev;
1821 u32 num_dev = 0;
1822
1823 if (!swrm) {
1824 pr_err("%s: Invalid handle to swr controller\n",
1825 __func__);
1826 return ret;
1827 }
1828 if (swrm->num_dev)
1829 num_dev = swrm->num_dev;
1830 else
1831 num_dev = mstr->num_dev;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301832
1833 mutex_lock(&swrm->devlock);
1834 if (!swrm->dev_up) {
1835 mutex_unlock(&swrm->devlock);
1836 return ret;
1837 }
1838 mutex_unlock(&swrm->devlock);
1839
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301840 pm_runtime_get_sync(swrm->dev);
1841 for (i = 1; i < (num_dev + 1); i++) {
1842 id = ((u64)(swr_master_read(swrm,
1843 SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
1844 id |= swr_master_read(swrm,
1845 SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301846
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301847 /*
1848 * As pm_runtime_get_sync() brings all slaves out of reset
1849 * update logical device number for all slaves.
1850 */
1851 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1852 if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
1853 u32 status = swrm_get_device_status(swrm, i);
1854
1855 if ((status == 0x01) || (status == 0x02)) {
1856 swr_dev->dev_num = i;
1857 if ((id & SWR_DEV_ID_MASK) == dev_id) {
1858 *dev_num = i;
1859 ret = 0;
1860 }
1861 dev_dbg(swrm->dev,
1862 "%s: devnum %d is assigned for dev addr %lx\n",
1863 __func__, i, swr_dev->addr);
1864 }
1865 }
1866 }
1867 }
1868 if (ret)
1869 dev_err(swrm->dev, "%s: device 0x%llx is not ready\n",
1870 __func__, dev_id);
1871
1872 pm_runtime_mark_last_busy(swrm->dev);
1873 pm_runtime_put_autosuspend(swrm->dev);
1874 return ret;
1875}
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301876
1877static void swrm_device_wakeup_vote(struct swr_master *mstr)
1878{
1879 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1880
1881 if (!swrm) {
1882 pr_err("%s: Invalid handle to swr controller\n",
1883 __func__);
1884 return;
1885 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05301886 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1887 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1888 return;
1889 }
Sudheer Papothi384addd2019-06-14 02:26:52 +05301890 if (++swrm->hw_core_clk_en == 1)
1891 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
1892 dev_err(swrm->dev, "%s:lpass core hw enable failed\n",
1893 __func__);
1894 --swrm->hw_core_clk_en;
1895 }
1896 if ( ++swrm->aud_core_clk_en == 1)
1897 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
1898 dev_err(swrm->dev, "%s:lpass audio hw enable failed\n",
1899 __func__);
1900 --swrm->aud_core_clk_en;
1901 }
1902 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
1903 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301904 pm_runtime_get_sync(swrm->dev);
1905}
1906
1907static void swrm_device_wakeup_unvote(struct swr_master *mstr)
1908{
1909 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1910
1911 if (!swrm) {
1912 pr_err("%s: Invalid handle to swr controller\n",
1913 __func__);
1914 return;
1915 }
1916 pm_runtime_mark_last_busy(swrm->dev);
1917 pm_runtime_put_autosuspend(swrm->dev);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301918 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
1919 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
1920
1921 --swrm->aud_core_clk_en;
1922 if (swrm->aud_core_clk_en < 0)
1923 swrm->aud_core_clk_en = 0;
1924 else if (swrm->aud_core_clk_en == 0)
1925 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
1926
1927 --swrm->hw_core_clk_en;
1928 if (swrm->hw_core_clk_en < 0)
1929 swrm->hw_core_clk_en = 0;
1930 else if (swrm->hw_core_clk_en == 0)
1931 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
1932
Ramprasad Katkam57349872018-11-11 18:34:57 +05301933 swrm_unlock_sleep(swrm);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301934}
1935
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301936static int swrm_master_init(struct swr_mstr_ctrl *swrm)
1937{
1938 int ret = 0;
1939 u32 val;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301940 u8 row_ctrl = SWR_ROW_50;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301941 u8 col_ctrl = SWR_MIN_COL;
1942 u8 ssp_period = 1;
1943 u8 retry_cmd_num = 3;
1944 u32 reg[SWRM_MAX_INIT_REG];
1945 u32 value[SWRM_MAX_INIT_REG];
1946 int len = 0;
1947
1948 /* Clear Rows and Cols */
1949 val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
1950 (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
1951 (ssp_period << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
1952
1953 reg[len] = SWRM_MCP_FRAME_CTRL_BANK_ADDR(0);
1954 value[len++] = val;
1955
1956 /* Set Auto enumeration flag */
1957 reg[len] = SWRM_ENUMERATOR_CFG_ADDR;
1958 value[len++] = 1;
1959
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301960 /* Configure No pings */
1961 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
1962 val &= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK;
1963 val |= (0x1f << SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT);
1964 reg[len] = SWRM_MCP_CFG_ADDR;
1965 value[len++] = val;
1966
1967 /* Configure number of retries of a read/write cmd */
1968 val = (retry_cmd_num << SWRM_CMD_FIFO_CFG_NUM_OF_CMD_RETRY_SHFT);
1969 reg[len] = SWRM_CMD_FIFO_CFG_ADDR;
1970 value[len++] = val;
1971
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301972 reg[len] = SWRM_MCP_BUS_CTRL_ADDR;
1973 value[len++] = 0x2;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301974
Ramprasad Katkam83303512018-10-11 17:34:22 +05301975 /* Set IRQ to PULSE */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301976 reg[len] = SWRM_COMP_CFG_ADDR;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301977 value[len++] = 0x02;
1978
1979 reg[len] = SWRM_COMP_CFG_ADDR;
1980 value[len++] = 0x03;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301981
1982 reg[len] = SWRM_INTERRUPT_CLEAR;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301983 value[len++] = 0xFFFFFFFF;
1984
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301985 swrm->intr_mask = SWRM_INTERRUPT_STATUS_MASK;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301986 /* Mask soundwire interrupts */
1987 reg[len] = SWRM_INTERRUPT_MASK_ADDR;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301988 value[len++] = swrm->intr_mask;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301989
1990 reg[len] = SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301991 value[len++] = swrm->intr_mask;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301992
1993 swr_master_bulk_write(swrm, reg, value, len);
1994
Sudheer Papothi63f48152018-11-15 01:08:03 +05301995 /*
1996 * For SWR master version 1.5.1, continue
1997 * execute on command ignore.
1998 */
1999 if (swrm->version == SWRM_VERSION_1_5_1)
2000 swr_master_write(swrm, SWRM_CMD_FIFO_CFG_ADDR,
2001 (swr_master_read(swrm,
2002 SWRM_CMD_FIFO_CFG_ADDR) | 0x80000000));
2003
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302004 return ret;
2005}
2006
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302007static int swrm_event_notify(struct notifier_block *self,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302008 unsigned long action, void *data)
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302009{
2010 struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302011 event_notifier);
2012
2013 if (!swrm || !(swrm->dev)) {
2014 pr_err("%s: swrm or dev is NULL\n", __func__);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302015 return -EINVAL;
2016 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302017 switch (action) {
2018 case MSM_AUD_DC_EVENT:
2019 schedule_work(&(swrm->dc_presence_work));
2020 break;
2021 case SWR_WAKE_IRQ_EVENT:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302022 if (swrm->ipc_wakeup && !swrm->ipc_wakeup_triggered) {
2023 swrm->ipc_wakeup_triggered = true;
Ramprasad Katkam57349872018-11-11 18:34:57 +05302024 pm_stay_awake(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302025 schedule_work(&swrm->wakeup_work);
Ramprasad Katkamcd61c6e2018-09-18 13:22:58 +05302026 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302027 break;
2028 default:
2029 dev_err(swrm->dev, "%s: invalid event type: %lu\n",
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302030 __func__, action);
2031 return -EINVAL;
2032 }
2033
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302034 return 0;
2035}
2036
2037static void swrm_notify_work_fn(struct work_struct *work)
2038{
2039 struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302040 dc_presence_work);
2041
2042 if (!swrm || !swrm->pdev) {
2043 pr_err("%s: swrm or pdev is NULL\n", __func__);
2044 return;
2045 }
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302046 swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL);
2047}
2048
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302049static int swrm_probe(struct platform_device *pdev)
2050{
2051 struct swr_mstr_ctrl *swrm;
2052 struct swr_ctrl_platform_data *pdata;
2053 u32 i, num_ports, port_num, port_type, ch_mask;
2054 u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0;
2055 int ret = 0;
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302056 struct clk *lpass_core_hw_vote = NULL;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302057 struct clk *lpass_core_audio = NULL;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302058
2059 /* Allocate soundwire master driver structure */
2060 swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl),
2061 GFP_KERNEL);
2062 if (!swrm) {
2063 ret = -ENOMEM;
2064 goto err_memory_fail;
2065 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302066 swrm->pdev = pdev;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302067 swrm->dev = &pdev->dev;
2068 platform_set_drvdata(pdev, swrm);
2069 swr_set_ctrl_data(&swrm->master, swrm);
2070 pdata = dev_get_platdata(&pdev->dev);
2071 if (!pdata) {
2072 dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
2073 __func__);
2074 ret = -EINVAL;
2075 goto err_pdata_fail;
2076 }
2077 swrm->handle = (void *)pdata->handle;
2078 if (!swrm->handle) {
2079 dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
2080 __func__);
2081 ret = -EINVAL;
2082 goto err_pdata_fail;
2083 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302084 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr_master_id",
2085 &swrm->master_id);
2086 if (ret) {
2087 dev_err(&pdev->dev, "%s: failed to get master id\n", __func__);
2088 goto err_pdata_fail;
2089 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302090 if (!(of_property_read_u32(pdev->dev.of_node,
2091 "swrm-io-base", &swrm->swrm_base_reg)))
2092 ret = of_property_read_u32(pdev->dev.of_node,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302093 "swrm-io-base", &swrm->swrm_base_reg);
2094 if (!swrm->swrm_base_reg) {
2095 swrm->read = pdata->read;
2096 if (!swrm->read) {
2097 dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
2098 __func__);
2099 ret = -EINVAL;
2100 goto err_pdata_fail;
2101 }
2102 swrm->write = pdata->write;
2103 if (!swrm->write) {
2104 dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
2105 __func__);
2106 ret = -EINVAL;
2107 goto err_pdata_fail;
2108 }
2109 swrm->bulk_write = pdata->bulk_write;
2110 if (!swrm->bulk_write) {
2111 dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
2112 __func__);
2113 ret = -EINVAL;
2114 goto err_pdata_fail;
2115 }
2116 } else {
2117 swrm->swrm_dig_base = devm_ioremap(&pdev->dev,
2118 swrm->swrm_base_reg, SWRM_MAX_REGISTER);
2119 }
2120
2121 swrm->clk = pdata->clk;
2122 if (!swrm->clk) {
2123 dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
2124 __func__);
2125 ret = -EINVAL;
2126 goto err_pdata_fail;
2127 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302128 if (of_property_read_u32(pdev->dev.of_node,
2129 "qcom,swr-clock-stop-mode0",
2130 &swrm->clk_stop_mode0_supp)) {
2131 swrm->clk_stop_mode0_supp = FALSE;
2132 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302133
2134 ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
2135 &swrm->num_dev);
2136 if (ret) {
2137 dev_dbg(&pdev->dev, "%s: Looking up %s property failed\n",
2138 __func__, "qcom,swr-num-dev");
2139 } else {
2140 if (swrm->num_dev > SWR_MAX_SLAVE_DEVICES) {
2141 dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
2142 __func__, swrm->num_dev, SWR_MAX_SLAVE_DEVICES);
2143 ret = -EINVAL;
2144 goto err_pdata_fail;
2145 }
2146 }
2147
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302148 /* Parse soundwire port mapping */
2149 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
2150 &num_ports);
2151 if (ret) {
2152 dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
2153 goto err_pdata_fail;
2154 }
2155 swrm->num_ports = num_ports;
2156
2157 if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
2158 &map_size)) {
2159 dev_err(swrm->dev, "missing port mapping\n");
2160 goto err_pdata_fail;
2161 }
2162
2163 map_length = map_size / (3 * sizeof(u32));
2164 if (num_ports > SWR_MSTR_PORT_LEN) {
2165 dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
2166 __func__);
2167 ret = -EINVAL;
2168 goto err_pdata_fail;
2169 }
2170 temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
2171
2172 if (!temp) {
2173 ret = -ENOMEM;
2174 goto err_pdata_fail;
2175 }
2176 ret = of_property_read_u32_array(pdev->dev.of_node,
2177 "qcom,swr-port-mapping", temp, 3 * map_length);
2178 if (ret) {
2179 dev_err(swrm->dev, "%s: Failed to read port mapping\n",
2180 __func__);
2181 goto err_pdata_fail;
2182 }
2183
2184 for (i = 0; i < map_length; i++) {
2185 port_num = temp[3 * i];
2186 port_type = temp[3 * i + 1];
2187 ch_mask = temp[3 * i + 2];
2188
2189 if (port_num != old_port_num)
2190 ch_iter = 0;
2191 swrm->port_mapping[port_num][ch_iter].port_type = port_type;
2192 swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
2193 old_port_num = port_num;
2194 }
2195 devm_kfree(&pdev->dev, temp);
2196
2197 swrm->reg_irq = pdata->reg_irq;
2198 swrm->master.read = swrm_read;
2199 swrm->master.write = swrm_write;
2200 swrm->master.bulk_write = swrm_bulk_write;
2201 swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
2202 swrm->master.connect_port = swrm_connect_port;
2203 swrm->master.disconnect_port = swrm_disconnect_port;
2204 swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
2205 swrm->master.remove_from_group = swrm_remove_from_group;
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302206 swrm->master.device_wakeup_vote = swrm_device_wakeup_vote;
2207 swrm->master.device_wakeup_unvote = swrm_device_wakeup_unvote;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302208 swrm->master.dev.parent = &pdev->dev;
2209 swrm->master.dev.of_node = pdev->dev.of_node;
2210 swrm->master.num_port = 0;
2211 swrm->rcmd_id = 0;
2212 swrm->wcmd_id = 0;
2213 swrm->slave_status = 0;
2214 swrm->num_rx_chs = 0;
2215 swrm->clk_ref_count = 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302216 swrm->swr_irq_wakeup_capable = 0;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302217 swrm->mclk_freq = MCLK_FREQ;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302218 swrm->dev_up = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302219 swrm->state = SWR_MSTR_UP;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302220 swrm->ipc_wakeup = false;
2221 swrm->ipc_wakeup_triggered = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302222 init_completion(&swrm->reset);
2223 init_completion(&swrm->broadcast);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302224 init_completion(&swrm->clk_off_complete);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302225 mutex_init(&swrm->mlock);
2226 mutex_init(&swrm->reslock);
2227 mutex_init(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302228 mutex_init(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302229 mutex_init(&swrm->clklock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302230 mutex_init(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302231 mutex_init(&swrm->pm_lock);
2232 swrm->wlock_holders = 0;
2233 swrm->pm_state = SWRM_PM_SLEEPABLE;
2234 init_waitqueue_head(&swrm->pm_wq);
2235 pm_qos_add_request(&swrm->pm_qos_req,
2236 PM_QOS_CPU_DMA_LATENCY,
2237 PM_QOS_DEFAULT_VALUE);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302238
2239 for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++)
2240 INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
2241
Sudheer Papothi06f43412019-07-09 03:32:54 +05302242 /* Register LPASS core hw vote */
2243 lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
2244 if (IS_ERR(lpass_core_hw_vote)) {
2245 ret = PTR_ERR(lpass_core_hw_vote);
2246 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2247 __func__, "lpass_core_hw_vote", ret);
2248 lpass_core_hw_vote = NULL;
2249 ret = 0;
2250 }
2251 swrm->lpass_core_hw_vote = lpass_core_hw_vote;
2252
2253 /* Register LPASS audio core vote */
2254 lpass_core_audio = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
2255 if (IS_ERR(lpass_core_audio)) {
2256 ret = PTR_ERR(lpass_core_audio);
2257 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2258 __func__, "lpass_core_audio", ret);
2259 lpass_core_audio = NULL;
2260 ret = 0;
2261 }
2262 swrm->lpass_core_audio = lpass_core_audio;
2263
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302264 if (swrm->reg_irq) {
2265 ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
2266 SWR_IRQ_REGISTER);
2267 if (ret) {
2268 dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
2269 __func__, ret);
2270 goto err_irq_fail;
2271 }
2272 } else {
2273 swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
2274 if (swrm->irq < 0) {
2275 dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
2276 __func__, swrm->irq);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302277 goto err_irq_fail;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302278 }
2279
2280 ret = request_threaded_irq(swrm->irq, NULL,
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302281 swr_mstr_interrupt_v2,
Ramprasad Katkam83303512018-10-11 17:34:22 +05302282 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302283 "swr_master_irq", swrm);
2284 if (ret) {
2285 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2286 __func__, ret);
2287 goto err_irq_fail;
2288 }
2289
2290 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302291 /* Make inband tx interrupts as wakeup capable for slave irq */
2292 ret = of_property_read_u32(pdev->dev.of_node,
2293 "qcom,swr-mstr-irq-wakeup-capable",
2294 &swrm->swr_irq_wakeup_capable);
2295 if (ret)
2296 dev_dbg(swrm->dev, "%s: swrm irq wakeup capable not defined\n",
2297 __func__);
2298 if (swrm->swr_irq_wakeup_capable)
2299 irq_set_irq_wake(swrm->irq, 1);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302300 ret = swr_register_master(&swrm->master);
2301 if (ret) {
2302 dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
2303 goto err_mstr_fail;
2304 }
2305
2306 /* Add devices registered with board-info as the
2307 * controller will be up now
2308 */
2309 swr_master_add_boarddevices(&swrm->master);
2310 mutex_lock(&swrm->mlock);
2311 swrm_clk_request(swrm, true);
2312 ret = swrm_master_init(swrm);
2313 if (ret < 0) {
2314 dev_err(&pdev->dev,
2315 "%s: Error in master Initialization , err %d\n",
2316 __func__, ret);
2317 mutex_unlock(&swrm->mlock);
2318 goto err_mstr_fail;
2319 }
2320 swrm->version = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
2321
2322 mutex_unlock(&swrm->mlock);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302323 INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302324
2325 if (pdev->dev.of_node)
2326 of_register_swr_devices(&swrm->master);
2327
2328 dbgswrm = swrm;
2329 debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
2330 if (!IS_ERR(debugfs_swrm_dent)) {
2331 debugfs_peek = debugfs_create_file("swrm_peek",
2332 S_IFREG | 0444, debugfs_swrm_dent,
2333 (void *) "swrm_peek", &swrm_debug_ops);
2334
2335 debugfs_poke = debugfs_create_file("swrm_poke",
2336 S_IFREG | 0444, debugfs_swrm_dent,
2337 (void *) "swrm_poke", &swrm_debug_ops);
2338
2339 debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
2340 S_IFREG | 0444, debugfs_swrm_dent,
2341 (void *) "swrm_reg_dump",
2342 &swrm_debug_ops);
2343 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302344
2345 ret = device_init_wakeup(swrm->dev, true);
2346 if (ret) {
2347 dev_err(swrm->dev, "Device wakeup init failed: %d\n", ret);
2348 goto err_irq_wakeup_fail;
2349 }
2350
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302351 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
2352 pm_runtime_use_autosuspend(&pdev->dev);
2353 pm_runtime_set_active(&pdev->dev);
2354 pm_runtime_enable(&pdev->dev);
2355 pm_runtime_mark_last_busy(&pdev->dev);
2356
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302357 INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn);
2358 swrm->event_notifier.notifier_call = swrm_event_notify;
2359 msm_aud_evt_register_client(&swrm->event_notifier);
2360
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302361 return 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302362err_irq_wakeup_fail:
2363 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302364err_mstr_fail:
2365 if (swrm->reg_irq)
2366 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2367 swrm, SWR_IRQ_FREE);
2368 else if (swrm->irq)
2369 free_irq(swrm->irq, swrm);
2370err_irq_fail:
2371 mutex_destroy(&swrm->mlock);
2372 mutex_destroy(&swrm->reslock);
2373 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302374 mutex_destroy(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302375 mutex_destroy(&swrm->clklock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302376 mutex_destroy(&swrm->pm_lock);
2377 pm_qos_remove_request(&swrm->pm_qos_req);
2378
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302379err_pdata_fail:
2380err_memory_fail:
2381 return ret;
2382}
2383
2384static int swrm_remove(struct platform_device *pdev)
2385{
2386 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2387
2388 if (swrm->reg_irq)
2389 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2390 swrm, SWR_IRQ_FREE);
2391 else if (swrm->irq)
2392 free_irq(swrm->irq, swrm);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302393 else if (swrm->wake_irq > 0)
2394 free_irq(swrm->wake_irq, swrm);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302395 if (swrm->swr_irq_wakeup_capable)
2396 irq_set_irq_wake(swrm->irq, 0);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302397 cancel_work_sync(&swrm->wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302398 pm_runtime_disable(&pdev->dev);
2399 pm_runtime_set_suspended(&pdev->dev);
2400 swr_unregister_master(&swrm->master);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302401 msm_aud_evt_unregister_client(&swrm->event_notifier);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302402 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302403 mutex_destroy(&swrm->mlock);
2404 mutex_destroy(&swrm->reslock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302405 mutex_destroy(&swrm->iolock);
2406 mutex_destroy(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302407 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302408 mutex_destroy(&swrm->pm_lock);
2409 pm_qos_remove_request(&swrm->pm_qos_req);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302410 devm_kfree(&pdev->dev, swrm);
2411 return 0;
2412}
2413
2414static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
2415{
2416 u32 val;
2417
2418 dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
2419 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD);
2420 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2421 val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK;
2422 swr_master_write(swrm, SWRM_MCP_CFG_ADDR, val);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302423
2424 return 0;
2425}
2426
2427#ifdef CONFIG_PM
2428static int swrm_runtime_resume(struct device *dev)
2429{
2430 struct platform_device *pdev = to_platform_device(dev);
2431 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2432 int ret = 0;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302433 bool hw_core_err = false;
2434 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302435 struct swr_master *mstr = &swrm->master;
2436 struct swr_device *swr_dev;
2437
2438 dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
2439 __func__, swrm->state);
2440 mutex_lock(&swrm->reslock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302441
Sudheer Papothi384addd2019-06-14 02:26:52 +05302442 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2443 dev_err(dev, "%s:lpass core hw enable failed\n",
2444 __func__);
2445 hw_core_err = true;
2446 }
2447 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2448 dev_err(dev, "%s:lpass audio hw enable failed\n",
2449 __func__);
2450 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002451 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302452
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302453 if ((swrm->state == SWR_MSTR_DOWN) ||
2454 (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) {
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302455 if (swrm->clk_stop_mode0_supp) {
2456 if (swrm->ipc_wakeup)
2457 msm_aud_evt_blocking_notifier_call_chain(
2458 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302459 }
2460
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302461 if (swrm_clk_request(swrm, true)) {
2462 /*
2463 * Set autosuspend timer to 1 for
2464 * master to enter into suspend.
2465 */
2466 auto_suspend_timer = 1;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302467 goto exit;
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302468 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302469 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302470 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2471 ret = swr_device_up(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302472 if (ret == -ENODEV) {
2473 dev_dbg(dev,
2474 "%s slave device up not implemented\n",
2475 __func__);
2476 ret = 0;
2477 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302478 dev_err(dev,
2479 "%s: failed to wakeup swr dev %d\n",
2480 __func__, swr_dev->dev_num);
2481 swrm_clk_request(swrm, false);
2482 goto exit;
2483 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302484 }
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05302485 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2486 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2487 swrm_master_init(swrm);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302488 /* wait for hw enumeration to complete */
2489 usleep_range(100, 105);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302490 swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, 0x0,
2491 SWRS_SCP_INT_STATUS_MASK_1);
Karthikeyan Manif6821902019-05-21 17:31:24 -07002492 if (swrm->state == SWR_MSTR_SSR) {
2493 mutex_unlock(&swrm->reslock);
2494 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
2495 mutex_lock(&swrm->reslock);
2496 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302497 } else {
2498 /*wake up from clock stop*/
2499 swr_master_write(swrm, SWRM_MCP_BUS_CTRL_ADDR, 0x2);
2500 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302501 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302502 swrm->state = SWR_MSTR_UP;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302503 }
2504exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302505 if (!aud_core_err)
2506 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2507 if (!hw_core_err)
2508 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302509 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
Vatsal Bucha63b193f2019-08-12 11:56:55 +05302510 auto_suspend_timer = SWR_AUTO_SUSPEND_DELAY * 1000;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302511 mutex_unlock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302512
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302513 return ret;
2514}
2515
2516static int swrm_runtime_suspend(struct device *dev)
2517{
2518 struct platform_device *pdev = to_platform_device(dev);
2519 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2520 int ret = 0;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302521 bool hw_core_err = false;
2522 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302523 struct swr_master *mstr = &swrm->master;
2524 struct swr_device *swr_dev;
2525 int current_state = 0;
2526
2527 dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
2528 __func__, swrm->state);
2529 mutex_lock(&swrm->reslock);
2530 mutex_lock(&swrm->force_down_lock);
2531 current_state = swrm->state;
2532 mutex_unlock(&swrm->force_down_lock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302533
2534 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2535 dev_err(dev, "%s:lpass core hw enable failed\n",
2536 __func__);
2537 hw_core_err = true;
2538 }
2539 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2540 dev_err(dev, "%s:lpass audio hw enable failed\n",
2541 __func__);
2542 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002543 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302544
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302545 if ((current_state == SWR_MSTR_UP) ||
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302546 (current_state == SWR_MSTR_SSR)) {
2547
2548 if ((current_state != SWR_MSTR_SSR) &&
2549 swrm_is_port_en(&swrm->master)) {
2550 dev_dbg(dev, "%s ports are enabled\n", __func__);
2551 ret = -EBUSY;
2552 goto exit;
2553 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302554 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05302555 mutex_unlock(&swrm->reslock);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +05302556 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
Sudheer Papothi06f43412019-07-09 03:32:54 +05302557 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302558 swrm_clk_pause(swrm);
2559 swr_master_write(swrm, SWRM_COMP_CFG_ADDR, 0x00);
2560 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2561 ret = swr_device_down(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302562 if (ret == -ENODEV) {
2563 dev_dbg_ratelimited(dev,
2564 "%s slave device down not implemented\n",
2565 __func__);
2566 ret = 0;
2567 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302568 dev_err(dev,
2569 "%s: failed to shutdown swr dev %d\n",
2570 __func__, swr_dev->dev_num);
2571 goto exit;
2572 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302573 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302574 } else {
Sudheer Papothi384addd2019-06-14 02:26:52 +05302575 mutex_unlock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302576 /* clock stop sequence */
2577 swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
2578 SWRS_SCP_CONTROL);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302579 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302580 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302581 }
2582 swrm_clk_request(swrm, false);
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302583
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302584 if (swrm->clk_stop_mode0_supp) {
2585 if (swrm->wake_irq > 0) {
2586 enable_irq(swrm->wake_irq);
2587 } else if (swrm->ipc_wakeup) {
2588 msm_aud_evt_blocking_notifier_call_chain(
2589 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
2590 swrm->ipc_wakeup_triggered = false;
2591 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302592 }
2593
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302594 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302595 /* Retain SSR state until resume */
2596 if (current_state != SWR_MSTR_SSR)
2597 swrm->state = SWR_MSTR_DOWN;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302598exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302599 if (!aud_core_err)
2600 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2601 if (!hw_core_err)
2602 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302603 mutex_unlock(&swrm->reslock);
2604 return ret;
2605}
2606#endif /* CONFIG_PM */
2607
Sudheer Papothi06f43412019-07-09 03:32:54 +05302608static int swrm_device_suspend(struct device *dev)
2609{
2610 struct platform_device *pdev = to_platform_device(dev);
2611 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2612 int ret = 0;
2613
2614 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2615 if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
2616 ret = swrm_runtime_suspend(dev);
2617 if (!ret) {
2618 pm_runtime_disable(dev);
2619 pm_runtime_set_suspended(dev);
2620 pm_runtime_enable(dev);
2621 }
2622 }
2623
2624 return 0;
2625}
2626
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302627static int swrm_device_down(struct device *dev)
2628{
2629 struct platform_device *pdev = to_platform_device(dev);
2630 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302631
2632 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2633
2634 mutex_lock(&swrm->force_down_lock);
2635 swrm->state = SWR_MSTR_SSR;
2636 mutex_unlock(&swrm->force_down_lock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302637
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302638 swrm_device_suspend(dev);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302639 return 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302640}
2641
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302642int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm)
2643{
2644 int ret = 0;
Laxminath Kasama60239e2019-01-10 14:43:03 +05302645 int irq, dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302646
2647 if (!swrm->ipc_wakeup) {
Laxminath Kasama60239e2019-01-10 14:43:03 +05302648 irq = of_get_named_gpio(swrm->dev->of_node,
2649 "qcom,swr-wakeup-irq", 0);
2650 if (gpio_is_valid(irq)) {
2651 swrm->wake_irq = gpio_to_irq(irq);
2652 if (swrm->wake_irq < 0) {
2653 dev_err(swrm->dev,
2654 "Unable to configure irq\n");
2655 return swrm->wake_irq;
2656 }
2657 } else {
2658 dir_apps_irq = platform_get_irq_byname(swrm->pdev,
2659 "swr_wake_irq");
2660 if (dir_apps_irq < 0) {
2661 dev_err(swrm->dev,
2662 "TLMM connect gpio not found\n");
2663 return -EINVAL;
2664 }
2665 swrm->wake_irq = dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302666 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302667 ret = request_threaded_irq(swrm->wake_irq, NULL,
2668 swrm_wakeup_interrupt,
2669 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
2670 "swr_wake_irq", swrm);
2671 if (ret) {
2672 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2673 __func__, ret);
2674 return -EINVAL;
2675 }
Aditya Bavanari3517b112018-12-03 13:26:59 +05302676 irq_set_irq_wake(swrm->wake_irq, 1);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302677 }
2678 return ret;
2679}
2680
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302681static int swrm_alloc_port_mem(struct device *dev, struct swr_mstr_ctrl *swrm,
2682 u32 uc, u32 size)
2683{
2684 if (!swrm->port_param) {
2685 swrm->port_param = devm_kzalloc(dev,
2686 sizeof(swrm->port_param) * SWR_UC_MAX,
2687 GFP_KERNEL);
2688 if (!swrm->port_param)
2689 return -ENOMEM;
2690 }
2691 if (!swrm->port_param[uc]) {
2692 swrm->port_param[uc] = devm_kcalloc(dev, size,
2693 sizeof(struct port_params),
2694 GFP_KERNEL);
2695 if (!swrm->port_param[uc])
2696 return -ENOMEM;
2697 } else {
2698 dev_err_ratelimited(swrm->dev, "%s: called more than once\n",
2699 __func__);
2700 }
2701
2702 return 0;
2703}
2704
2705static int swrm_copy_port_config(struct swr_mstr_ctrl *swrm,
2706 struct swrm_port_config *port_cfg,
2707 u32 size)
2708{
2709 int idx;
2710 struct port_params *params;
2711 int uc = port_cfg->uc;
2712 int ret = 0;
2713
2714 for (idx = 0; idx < size; idx++) {
2715 params = &((struct port_params *)port_cfg->params)[idx];
2716 if (!params) {
2717 dev_err(swrm->dev, "%s: Invalid params\n", __func__);
2718 ret = -EINVAL;
2719 break;
2720 }
2721 memcpy(&swrm->port_param[uc][idx], params,
2722 sizeof(struct port_params));
2723 }
2724
2725 return ret;
2726}
2727
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302728/**
2729 * swrm_wcd_notify - parent device can notify to soundwire master through
2730 * this function
2731 * @pdev: pointer to platform device structure
2732 * @id: command id from parent to the soundwire master
2733 * @data: data from parent device to soundwire master
2734 */
2735int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
2736{
2737 struct swr_mstr_ctrl *swrm;
2738 int ret = 0;
2739 struct swr_master *mstr;
2740 struct swr_device *swr_dev;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302741 struct swrm_port_config *port_cfg;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302742
2743 if (!pdev) {
2744 pr_err("%s: pdev is NULL\n", __func__);
2745 return -EINVAL;
2746 }
2747 swrm = platform_get_drvdata(pdev);
2748 if (!swrm) {
2749 dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__);
2750 return -EINVAL;
2751 }
2752 mstr = &swrm->master;
2753
2754 switch (id) {
Sudheer Papothi06f43412019-07-09 03:32:54 +05302755 case SWR_REQ_CLK_SWITCH:
2756 /* This will put soundwire in clock stop mode and disable the
2757 * clocks, if there is no active usecase running, so that the
2758 * next activity on soundwire will request clock from new clock
2759 * source.
2760 */
2761 mutex_lock(&swrm->mlock);
2762 if (swrm->state == SWR_MSTR_UP)
2763 swrm_device_suspend(&pdev->dev);
2764 mutex_unlock(&swrm->mlock);
2765 break;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302766 case SWR_CLK_FREQ:
2767 if (!data) {
2768 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
2769 ret = -EINVAL;
2770 } else {
2771 mutex_lock(&swrm->mlock);
Ramprasad Katkam2e85a542019-04-26 18:28:31 +05302772 if (swrm->mclk_freq != *(int *)data) {
2773 dev_dbg(swrm->dev, "%s: freq change: force mstr down\n", __func__);
2774 if (swrm->state == SWR_MSTR_DOWN)
2775 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
2776 __func__, swrm->state);
2777 else
2778 swrm_device_suspend(&pdev->dev);
2779 }
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302780 swrm->mclk_freq = *(int *)data;
2781 mutex_unlock(&swrm->mlock);
2782 }
2783 break;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302784 case SWR_DEVICE_SSR_DOWN:
2785 mutex_lock(&swrm->devlock);
2786 swrm->dev_up = false;
2787 mutex_unlock(&swrm->devlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302788 mutex_lock(&swrm->reslock);
2789 swrm->state = SWR_MSTR_SSR;
2790 mutex_unlock(&swrm->reslock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302791 break;
2792 case SWR_DEVICE_SSR_UP:
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302793 /* wait for clk voting to be zero */
Ramprasad Katkam7f6462e2018-11-06 11:51:22 +05302794 reinit_completion(&swrm->clk_off_complete);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302795 if (swrm->clk_ref_count &&
2796 !wait_for_completion_timeout(&swrm->clk_off_complete,
Ramprasad Katkamc87efeb2018-12-12 19:26:19 +05302797 msecs_to_jiffies(500)))
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302798 dev_err(swrm->dev, "%s: clock voting not zero\n",
2799 __func__);
2800
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302801 mutex_lock(&swrm->devlock);
2802 swrm->dev_up = true;
2803 mutex_unlock(&swrm->devlock);
2804 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302805 case SWR_DEVICE_DOWN:
2806 dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
2807 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302808 if (swrm->state == SWR_MSTR_DOWN)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302809 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
2810 __func__, swrm->state);
2811 else
2812 swrm_device_down(&pdev->dev);
2813 mutex_unlock(&swrm->mlock);
2814 break;
2815 case SWR_DEVICE_UP:
2816 dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
Ramprasad Katkam0fed92f2018-11-08 14:22:22 +05302817 mutex_lock(&swrm->devlock);
2818 if (!swrm->dev_up) {
2819 dev_dbg(swrm->dev, "SSR not complete yet\n");
2820 mutex_unlock(&swrm->devlock);
2821 return -EBUSY;
2822 }
2823 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302824 mutex_lock(&swrm->mlock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302825 pm_runtime_mark_last_busy(&pdev->dev);
2826 pm_runtime_get_sync(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302827 mutex_lock(&swrm->reslock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302828 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2829 ret = swr_reset_device(swr_dev);
2830 if (ret) {
2831 dev_err(swrm->dev,
2832 "%s: failed to reset swr device %d\n",
2833 __func__, swr_dev->dev_num);
2834 swrm_clk_request(swrm, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302835 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302836 }
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302837 pm_runtime_mark_last_busy(&pdev->dev);
2838 pm_runtime_put_autosuspend(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302839 mutex_unlock(&swrm->reslock);
2840 mutex_unlock(&swrm->mlock);
2841 break;
2842 case SWR_SET_NUM_RX_CH:
2843 if (!data) {
2844 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
2845 ret = -EINVAL;
2846 } else {
2847 mutex_lock(&swrm->mlock);
2848 swrm->num_rx_chs = *(int *)data;
2849 if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
2850 list_for_each_entry(swr_dev, &mstr->devices,
2851 dev_list) {
2852 ret = swr_set_device_group(swr_dev,
2853 SWR_BROADCAST);
2854 if (ret)
2855 dev_err(swrm->dev,
2856 "%s: set num ch failed\n",
2857 __func__);
2858 }
2859 } else {
2860 list_for_each_entry(swr_dev, &mstr->devices,
2861 dev_list) {
2862 ret = swr_set_device_group(swr_dev,
2863 SWR_GROUP_NONE);
2864 if (ret)
2865 dev_err(swrm->dev,
2866 "%s: set num ch failed\n",
2867 __func__);
2868 }
2869 }
2870 mutex_unlock(&swrm->mlock);
2871 }
2872 break;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302873 case SWR_REGISTER_WAKE_IRQ:
2874 if (!data) {
2875 dev_err(swrm->dev, "%s: reg wake irq data is NULL\n",
2876 __func__);
2877 ret = -EINVAL;
2878 } else {
2879 mutex_lock(&swrm->mlock);
2880 swrm->ipc_wakeup = *(u32 *)data;
2881 ret = swrm_register_wake_irq(swrm);
2882 if (ret)
2883 dev_err(swrm->dev, "%s: register wake_irq failed\n",
2884 __func__);
2885 mutex_unlock(&swrm->mlock);
2886 }
2887 break;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302888 case SWR_SET_PORT_MAP:
2889 if (!data) {
2890 dev_err(swrm->dev, "%s: data is NULL for id=%d\n",
2891 __func__, id);
2892 ret = -EINVAL;
2893 } else {
2894 mutex_lock(&swrm->mlock);
2895 port_cfg = (struct swrm_port_config *)data;
2896 if (!port_cfg->size) {
2897 ret = -EINVAL;
2898 goto done;
2899 }
2900 ret = swrm_alloc_port_mem(&pdev->dev, swrm,
2901 port_cfg->uc, port_cfg->size);
2902 if (!ret)
2903 swrm_copy_port_config(swrm, port_cfg,
2904 port_cfg->size);
2905done:
2906 mutex_unlock(&swrm->mlock);
2907 }
2908 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302909 default:
2910 dev_err(swrm->dev, "%s: swr master unknown id %d\n",
2911 __func__, id);
2912 break;
2913 }
2914 return ret;
2915}
2916EXPORT_SYMBOL(swrm_wcd_notify);
2917
Ramprasad Katkam57349872018-11-11 18:34:57 +05302918/*
2919 * swrm_pm_cmpxchg:
2920 * Check old state and exchange with pm new state
2921 * if old state matches with current state
2922 *
2923 * @swrm: pointer to wcd core resource
2924 * @o: pm old state
2925 * @n: pm new state
2926 *
2927 * Returns old state
2928 */
2929static enum swrm_pm_state swrm_pm_cmpxchg(
2930 struct swr_mstr_ctrl *swrm,
2931 enum swrm_pm_state o,
2932 enum swrm_pm_state n)
2933{
2934 enum swrm_pm_state old;
2935
2936 if (!swrm)
2937 return o;
2938
2939 mutex_lock(&swrm->pm_lock);
2940 old = swrm->pm_state;
2941 if (old == o)
2942 swrm->pm_state = n;
2943 mutex_unlock(&swrm->pm_lock);
2944
2945 return old;
2946}
2947
2948static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm)
2949{
2950 enum swrm_pm_state os;
2951
2952 /*
2953 * swrm_{lock/unlock}_sleep will be called by swr irq handler
2954 * and slave wake up requests..
2955 *
2956 * If system didn't resume, we can simply return false so
2957 * IRQ handler can return without handling IRQ.
2958 */
2959 mutex_lock(&swrm->pm_lock);
2960 if (swrm->wlock_holders++ == 0) {
2961 dev_dbg(swrm->dev, "%s: holding wake lock\n", __func__);
2962 pm_qos_update_request(&swrm->pm_qos_req,
2963 msm_cpuidle_get_deep_idle_latency());
2964 pm_stay_awake(swrm->dev);
2965 }
2966 mutex_unlock(&swrm->pm_lock);
2967
2968 if (!wait_event_timeout(swrm->pm_wq,
2969 ((os = swrm_pm_cmpxchg(swrm,
2970 SWRM_PM_SLEEPABLE,
2971 SWRM_PM_AWAKE)) ==
2972 SWRM_PM_SLEEPABLE ||
2973 (os == SWRM_PM_AWAKE)),
2974 msecs_to_jiffies(
2975 SWRM_SYSTEM_RESUME_TIMEOUT_MS))) {
2976 dev_err(swrm->dev, "%s: system didn't resume within %dms, s %d, w %d\n",
2977 __func__, SWRM_SYSTEM_RESUME_TIMEOUT_MS, swrm->pm_state,
2978 swrm->wlock_holders);
2979 swrm_unlock_sleep(swrm);
2980 return false;
2981 }
2982 wake_up_all(&swrm->pm_wq);
2983 return true;
2984}
2985
2986static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm)
2987{
2988 mutex_lock(&swrm->pm_lock);
2989 if (--swrm->wlock_holders == 0) {
2990 dev_dbg(swrm->dev, "%s: releasing wake lock pm_state %d -> %d\n",
2991 __func__, swrm->pm_state, SWRM_PM_SLEEPABLE);
2992 /*
2993 * if swrm_lock_sleep failed, pm_state would be still
2994 * swrm_PM_ASLEEP, don't overwrite
2995 */
2996 if (likely(swrm->pm_state == SWRM_PM_AWAKE))
2997 swrm->pm_state = SWRM_PM_SLEEPABLE;
2998 pm_qos_update_request(&swrm->pm_qos_req,
2999 PM_QOS_DEFAULT_VALUE);
3000 pm_relax(swrm->dev);
3001 }
3002 mutex_unlock(&swrm->pm_lock);
3003 wake_up_all(&swrm->pm_wq);
3004}
3005
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303006#ifdef CONFIG_PM_SLEEP
3007static int swrm_suspend(struct device *dev)
3008{
3009 int ret = -EBUSY;
3010 struct platform_device *pdev = to_platform_device(dev);
3011 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3012
3013 dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
Ramprasad Katkam57349872018-11-11 18:34:57 +05303014
3015 mutex_lock(&swrm->pm_lock);
3016
3017 if (swrm->pm_state == SWRM_PM_SLEEPABLE) {
3018 dev_dbg(swrm->dev, "%s: suspending system, state %d, wlock %d\n",
3019 __func__, swrm->pm_state,
3020 swrm->wlock_holders);
3021 swrm->pm_state = SWRM_PM_ASLEEP;
3022 } else if (swrm->pm_state == SWRM_PM_AWAKE) {
3023 /*
3024 * unlock to wait for pm_state == SWRM_PM_SLEEPABLE
3025 * then set to SWRM_PM_ASLEEP
3026 */
3027 dev_dbg(swrm->dev, "%s: waiting to suspend system, state %d, wlock %d\n",
3028 __func__, swrm->pm_state,
3029 swrm->wlock_holders);
3030 mutex_unlock(&swrm->pm_lock);
3031 if (!(wait_event_timeout(swrm->pm_wq, swrm_pm_cmpxchg(
3032 swrm, SWRM_PM_SLEEPABLE,
3033 SWRM_PM_ASLEEP) ==
3034 SWRM_PM_SLEEPABLE,
3035 msecs_to_jiffies(
3036 SWRM_SYS_SUSPEND_WAIT)))) {
3037 dev_dbg(swrm->dev, "%s: suspend failed state %d, wlock %d\n",
3038 __func__, swrm->pm_state,
3039 swrm->wlock_holders);
3040 return -EBUSY;
3041 } else {
3042 dev_dbg(swrm->dev,
3043 "%s: done, state %d, wlock %d\n",
3044 __func__, swrm->pm_state,
3045 swrm->wlock_holders);
3046 }
3047 mutex_lock(&swrm->pm_lock);
3048 } else if (swrm->pm_state == SWRM_PM_ASLEEP) {
3049 dev_dbg(swrm->dev, "%s: system is already suspended, state %d, wlock %d\n",
3050 __func__, swrm->pm_state,
3051 swrm->wlock_holders);
3052 }
3053
3054 mutex_unlock(&swrm->pm_lock);
3055
3056 if ((!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev))) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303057 ret = swrm_runtime_suspend(dev);
3058 if (!ret) {
3059 /*
3060 * Synchronize runtime-pm and system-pm states:
3061 * At this point, we are already suspended. If
3062 * runtime-pm still thinks its active, then
3063 * make sure its status is in sync with HW
3064 * status. The three below calls let the
3065 * runtime-pm know that we are suspended
3066 * already without re-invoking the suspend
3067 * callback
3068 */
3069 pm_runtime_disable(dev);
3070 pm_runtime_set_suspended(dev);
3071 pm_runtime_enable(dev);
3072 }
3073 }
3074 if (ret == -EBUSY) {
3075 /*
3076 * There is a possibility that some audio stream is active
3077 * during suspend. We dont want to return suspend failure in
3078 * that case so that display and relevant components can still
3079 * go to suspend.
3080 * If there is some other error, then it should be passed-on
3081 * to system level suspend
3082 */
3083 ret = 0;
3084 }
3085 return ret;
3086}
3087
3088static int swrm_resume(struct device *dev)
3089{
3090 int ret = 0;
3091 struct platform_device *pdev = to_platform_device(dev);
3092 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3093
3094 dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
3095 if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
3096 ret = swrm_runtime_resume(dev);
3097 if (!ret) {
3098 pm_runtime_mark_last_busy(dev);
3099 pm_request_autosuspend(dev);
3100 }
3101 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05303102 mutex_lock(&swrm->pm_lock);
3103 if (swrm->pm_state == SWRM_PM_ASLEEP) {
3104 dev_dbg(swrm->dev,
3105 "%s: resuming system, state %d, wlock %d\n",
3106 __func__, swrm->pm_state,
3107 swrm->wlock_holders);
3108 swrm->pm_state = SWRM_PM_SLEEPABLE;
3109 } else {
3110 dev_dbg(swrm->dev, "%s: system is already awake, state %d wlock %d\n",
3111 __func__, swrm->pm_state,
3112 swrm->wlock_holders);
3113 }
3114 mutex_unlock(&swrm->pm_lock);
3115 wake_up_all(&swrm->pm_wq);
3116
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303117 return ret;
3118}
3119#endif /* CONFIG_PM_SLEEP */
3120
3121static const struct dev_pm_ops swrm_dev_pm_ops = {
3122 SET_SYSTEM_SLEEP_PM_OPS(
3123 swrm_suspend,
3124 swrm_resume
3125 )
3126 SET_RUNTIME_PM_OPS(
3127 swrm_runtime_suspend,
3128 swrm_runtime_resume,
3129 NULL
3130 )
3131};
3132
3133static const struct of_device_id swrm_dt_match[] = {
3134 {
3135 .compatible = "qcom,swr-mstr",
3136 },
3137 {}
3138};
3139
3140static struct platform_driver swr_mstr_driver = {
3141 .probe = swrm_probe,
3142 .remove = swrm_remove,
3143 .driver = {
3144 .name = SWR_WCD_NAME,
3145 .owner = THIS_MODULE,
3146 .pm = &swrm_dev_pm_ops,
3147 .of_match_table = swrm_dt_match,
Xiaojun Sang53cd13a2018-06-29 15:14:37 +08003148 .suppress_bind_attrs = true,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303149 },
3150};
3151
3152static int __init swrm_init(void)
3153{
3154 return platform_driver_register(&swr_mstr_driver);
3155}
3156module_init(swrm_init);
3157
3158static void __exit swrm_exit(void)
3159{
3160 platform_driver_unregister(&swr_mstr_driver);
3161}
3162module_exit(swrm_exit);
3163
3164MODULE_LICENSE("GPL v2");
3165MODULE_DESCRIPTION("SoundWire Master Controller");
3166MODULE_ALIAS("platform:swr-mstr");