blob: 30c1f14c416f54448285f9244b8c2a1b1e81c69a [file] [log] [blame]
Meng Wang43bbb872018-12-10 12:32:05 +08001// SPDX-License-Identifier: GPL-2.0-only
Meng Wang61af6842018-09-10 17:47:55 +08002/*
Aditya Bavanari3517b112018-12-03 13:26:59 +05303 * Copyright (c) 2015-2019, The Linux Foundation. All rights reserved.
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05304 */
5
6#include <linux/irq.h>
7#include <linux/kernel.h>
8#include <linux/init.h>
9#include <linux/slab.h>
10#include <linux/io.h>
11#include <linux/interrupt.h>
12#include <linux/platform_device.h>
13#include <linux/delay.h>
14#include <linux/kthread.h>
Ramprasad Katkamcab8d722018-09-28 15:54:06 +053015#include <linux/bitops.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053016#include <linux/clk.h>
Laxminath Kasama60239e2019-01-10 14:43:03 +053017#include <linux/gpio.h>
18#include <linux/of_gpio.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053019#include <linux/pm_runtime.h>
20#include <linux/of.h>
21#include <linux/debugfs.h>
22#include <linux/uaccess.h>
23#include <soc/soundwire.h>
Sudheer Papothi3d1596e2018-10-27 06:19:18 +053024#include <soc/swr-common.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053025#include <linux/regmap.h>
Ramprasad Katkam68765ab2018-08-30 11:46:32 +053026#include <dsp/msm-audio-event-notify.h>
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053027#include "swrm_registers.h"
28#include "swr-mstr-ctrl.h"
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053029
Ramprasad Katkam57349872018-11-11 18:34:57 +053030#define SWRM_SYSTEM_RESUME_TIMEOUT_MS 700
31#define SWRM_SYS_SUSPEND_WAIT 1
Sudheer Papothi3d1596e2018-10-27 06:19:18 +053032
Sudheer Papothi4c322b12018-10-31 06:34:01 +053033#define SWRM_DSD_PARAMS_PORT 4
34
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053035#define SWR_BROADCAST_CMD_ID 0x0F
Sudheer Papothi3590b312019-06-04 23:51:30 +053036#define SWR_AUTO_SUSPEND_DELAY 1 /* delay in sec */
Sudheer Papothi7c067e82018-11-15 06:53:35 +053037#define SWR_DEV_ID_MASK 0xFFFFFFFFFFFF
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053038#define SWR_REG_VAL_PACK(data, dev, id, reg) \
39 ((reg) | ((id) << 16) | ((dev) << 20) | ((data) << 24))
40
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +053041#define SWR_INVALID_PARAM 0xFF
Laxminath Kasam990c70b2018-11-09 23:15:09 +053042#define SWR_HSTOP_MAX_VAL 0xF
43#define SWR_HSTART_MIN_VAL 0x0
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +053044
Ramprasad Katkam83303512018-10-11 17:34:22 +053045#define SWRM_INTERRUPT_STATUS_MASK 0x1FDFD
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053046/* pm runtime auto suspend timer in msecs */
47static int auto_suspend_timer = SWR_AUTO_SUSPEND_DELAY * 1000;
48module_param(auto_suspend_timer, int, 0664);
49MODULE_PARM_DESC(auto_suspend_timer, "timer for auto suspend");
50
51enum {
52 SWR_NOT_PRESENT, /* Device is detached/not present on the bus */
53 SWR_ATTACHED_OK, /* Device is attached */
54 SWR_ALERT, /* Device alters master for any interrupts */
55 SWR_RESERVED, /* Reserved */
56};
57
58enum {
59 MASTER_ID_WSA = 1,
60 MASTER_ID_RX,
61 MASTER_ID_TX
62};
Ramprasad Katkamcab8d722018-09-28 15:54:06 +053063
64enum {
65 ENABLE_PENDING,
66 DISABLE_PENDING
67};
Sudheer Papothi384addd2019-06-14 02:26:52 +053068
69enum {
70 LPASS_HW_CORE,
71 LPASS_AUDIO_CORE,
72};
73
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053074#define TRUE 1
75#define FALSE 0
76
Ramprasad Katkam1f221262018-08-23 15:01:22 +053077#define SWRM_MAX_PORT_REG 120
Ramprasad Katkam83303512018-10-11 17:34:22 +053078#define SWRM_MAX_INIT_REG 11
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053079
80#define SWR_MSTR_MAX_REG_ADDR 0x1740
81#define SWR_MSTR_START_REG_ADDR 0x00
82#define SWR_MSTR_MAX_BUF_LEN 32
83#define BYTES_PER_LINE 12
84#define SWR_MSTR_RD_BUF_LEN 8
85#define SWR_MSTR_WR_BUF_LEN 32
86
Laxminath Kasamfbcaf322018-07-18 00:38:14 +053087#define MAX_FIFO_RD_FAIL_RETRY 3
88
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053089static struct swr_mstr_ctrl *dbgswrm;
90static struct dentry *debugfs_swrm_dent;
91static struct dentry *debugfs_peek;
92static struct dentry *debugfs_poke;
93static struct dentry *debugfs_reg_dump;
94static unsigned int read_data;
95
Ramprasad Katkam57349872018-11-11 18:34:57 +053096static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm);
97static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +053098
99static bool swrm_is_msm_variant(int val)
100{
101 return (val == SWRM_VERSION_1_3);
102}
103
104static int swrm_debug_open(struct inode *inode, struct file *file)
105{
106 file->private_data = inode->i_private;
107 return 0;
108}
109
110static int get_parameters(char *buf, u32 *param1, int num_of_par)
111{
112 char *token;
113 int base, cnt;
114
115 token = strsep(&buf, " ");
116 for (cnt = 0; cnt < num_of_par; cnt++) {
117 if (token) {
118 if ((token[1] == 'x') || (token[1] == 'X'))
119 base = 16;
120 else
121 base = 10;
122
123 if (kstrtou32(token, base, &param1[cnt]) != 0)
124 return -EINVAL;
125
126 token = strsep(&buf, " ");
127 } else
128 return -EINVAL;
129 }
130 return 0;
131}
132
133static ssize_t swrm_reg_show(char __user *ubuf, size_t count,
134 loff_t *ppos)
135{
136 int i, reg_val, len;
137 ssize_t total = 0;
138 char tmp_buf[SWR_MSTR_MAX_BUF_LEN];
139
140 if (!ubuf || !ppos)
141 return 0;
142
143 for (i = (((int) *ppos / BYTES_PER_LINE) + SWR_MSTR_START_REG_ADDR);
144 i <= SWR_MSTR_MAX_REG_ADDR; i += 4) {
145 reg_val = dbgswrm->read(dbgswrm->handle, i);
146 len = snprintf(tmp_buf, 25, "0x%.3x: 0x%.2x\n", i, reg_val);
147 if ((total + len) >= count - 1)
148 break;
149 if (copy_to_user((ubuf + total), tmp_buf, len)) {
150 pr_err("%s: fail to copy reg dump\n", __func__);
151 total = -EFAULT;
152 goto copy_err;
153 }
154 *ppos += len;
155 total += len;
156 }
157
158copy_err:
159 return total;
160}
161
162static ssize_t swrm_debug_read(struct file *file, char __user *ubuf,
163 size_t count, loff_t *ppos)
164{
165 char lbuf[SWR_MSTR_RD_BUF_LEN];
166 char *access_str;
167 ssize_t ret_cnt;
168
169 if (!count || !file || !ppos || !ubuf)
170 return -EINVAL;
171
172 access_str = file->private_data;
173 if (*ppos < 0)
174 return -EINVAL;
175
176 if (!strcmp(access_str, "swrm_peek")) {
177 snprintf(lbuf, sizeof(lbuf), "0x%x\n", read_data);
178 ret_cnt = simple_read_from_buffer(ubuf, count, ppos, lbuf,
179 strnlen(lbuf, 7));
180 } else if (!strcmp(access_str, "swrm_reg_dump")) {
181 ret_cnt = swrm_reg_show(ubuf, count, ppos);
182 } else {
183 pr_err("%s: %s not permitted to read\n", __func__, access_str);
184 ret_cnt = -EPERM;
185 }
186 return ret_cnt;
187}
188
189static ssize_t swrm_debug_write(struct file *filp,
190 const char __user *ubuf, size_t cnt, loff_t *ppos)
191{
192 char lbuf[SWR_MSTR_WR_BUF_LEN];
193 int rc;
194 u32 param[5];
195 char *access_str;
196
197 if (!filp || !ppos || !ubuf)
198 return -EINVAL;
199
200 access_str = filp->private_data;
201 if (cnt > sizeof(lbuf) - 1)
202 return -EINVAL;
203
204 rc = copy_from_user(lbuf, ubuf, cnt);
205 if (rc)
206 return -EFAULT;
207
208 lbuf[cnt] = '\0';
209 if (!strcmp(access_str, "swrm_poke")) {
210 /* write */
211 rc = get_parameters(lbuf, param, 2);
212 if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) &&
213 (param[1] <= 0xFFFFFFFF) &&
214 (rc == 0))
215 rc = dbgswrm->write(dbgswrm->handle, param[0],
216 param[1]);
217 else
218 rc = -EINVAL;
219 } else if (!strcmp(access_str, "swrm_peek")) {
220 /* read */
221 rc = get_parameters(lbuf, param, 1);
222 if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) && (rc == 0))
223 read_data = dbgswrm->read(dbgswrm->handle, param[0]);
224 else
225 rc = -EINVAL;
226 }
227 if (rc == 0)
228 rc = cnt;
229 else
230 pr_err("%s: rc = %d\n", __func__, rc);
231
232 return rc;
233}
234
235static const struct file_operations swrm_debug_ops = {
236 .open = swrm_debug_open,
237 .write = swrm_debug_write,
238 .read = swrm_debug_read,
239};
240
Sudheer Papothi0016db12019-06-11 04:42:38 +0530241static void swrm_reg_dump(struct swr_mstr_ctrl *swrm,
242 u32 *reg, u32 *val, int len, const char* func)
243{
244 int i = 0;
245
246 for (i = 0; i < len; i++)
247 dev_dbg(swrm->dev, "%s: reg = 0x%x val = 0x%x\n",
248 func, reg[i], val[i]);
249}
250
Sudheer Papothi384addd2019-06-14 02:26:52 +0530251static int swrm_request_hw_vote(struct swr_mstr_ctrl *swrm,
252 int core_type, bool enable)
253{
254 int ret = 0;
255
256 if (core_type == LPASS_HW_CORE) {
257 if (swrm->lpass_core_hw_vote) {
258 if (enable) {
259 ret =
260 clk_prepare_enable(swrm->lpass_core_hw_vote);
261 if (ret < 0)
262 dev_err(swrm->dev,
263 "%s:lpass core hw enable failed\n",
264 __func__);
265 } else
266 clk_disable_unprepare(swrm->lpass_core_hw_vote);
267 }
268 }
269 if (core_type == LPASS_AUDIO_CORE) {
270 if (swrm->lpass_core_audio) {
271 if (enable) {
272 ret =
273 clk_prepare_enable(swrm->lpass_core_audio);
274 if (ret < 0)
275 dev_err(swrm->dev,
276 "%s:lpass audio hw enable failed\n",
277 __func__);
278 } else
279 clk_disable_unprepare(swrm->lpass_core_audio);
280 }
281 }
282
283 return ret;
284}
285
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530286static int swrm_clk_request(struct swr_mstr_ctrl *swrm, bool enable)
287{
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530288 int ret = 0;
289
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530290 if (!swrm->clk || !swrm->handle)
291 return -EINVAL;
292
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530293 mutex_lock(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530294 if (enable) {
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530295 if (!swrm->dev_up) {
296 ret = -ENODEV;
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530297 goto exit;
Aditya Bavanarif4a471d2019-02-19 17:57:12 +0530298 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530299 swrm->clk_ref_count++;
300 if (swrm->clk_ref_count == 1) {
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530301 ret = swrm->clk(swrm->handle, true);
302 if (ret) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +0530303 dev_err_ratelimited(swrm->dev,
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530304 "%s: clock enable req failed",
305 __func__);
306 --swrm->clk_ref_count;
307 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530308 }
309 } else if (--swrm->clk_ref_count == 0) {
310 swrm->clk(swrm->handle, false);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530311 complete(&swrm->clk_off_complete);
312 }
313 if (swrm->clk_ref_count < 0) {
Meng Wang8c60bb52019-06-19 15:49:06 +0800314 dev_err(swrm->dev, "%s: swrm clk count mismatch\n", __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530315 swrm->clk_ref_count = 0;
316 }
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +0530317
318exit:
319 mutex_unlock(&swrm->clklock);
320 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530321}
322
323static int swrm_ahb_write(struct swr_mstr_ctrl *swrm,
324 u16 reg, u32 *value)
325{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530326 u32 temp = (u32)(*value);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530327 int ret = 0;
328
329 mutex_lock(&swrm->devlock);
330 if (!swrm->dev_up)
331 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530332
333 ret = swrm_clk_request(swrm, TRUE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530334 if (ret) {
335 dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
336 __func__);
337 goto err;
338 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530339 iowrite32(temp, swrm->swrm_dig_base + reg);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530340 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530341err:
342 mutex_unlock(&swrm->devlock);
343 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530344}
345
346static int swrm_ahb_read(struct swr_mstr_ctrl *swrm,
347 u16 reg, u32 *value)
348{
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530349 u32 temp = 0;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530350 int ret = 0;
351
352 mutex_lock(&swrm->devlock);
353 if (!swrm->dev_up)
354 goto err;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530355
356 ret = swrm_clk_request(swrm, TRUE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530357 if (ret) {
358 dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
359 __func__);
360 goto err;
361 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530362 temp = ioread32(swrm->swrm_dig_base + reg);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530363 *value = temp;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530364 swrm_clk_request(swrm, FALSE);
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530365err:
366 mutex_unlock(&swrm->devlock);
367 return ret;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530368}
369
370static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr)
371{
372 u32 val = 0;
373
374 if (swrm->read)
375 val = swrm->read(swrm->handle, reg_addr);
376 else
377 swrm_ahb_read(swrm, reg_addr, &val);
378 return val;
379}
380
381static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val)
382{
383 if (swrm->write)
384 swrm->write(swrm->handle, reg_addr, val);
385 else
386 swrm_ahb_write(swrm, reg_addr, &val);
387}
388
389static int swr_master_bulk_write(struct swr_mstr_ctrl *swrm, u32 *reg_addr,
390 u32 *val, unsigned int length)
391{
392 int i = 0;
393
394 if (swrm->bulk_write)
395 swrm->bulk_write(swrm->handle, reg_addr, val, length);
396 else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530397 mutex_lock(&swrm->iolock);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530398 for (i = 0; i < length; i++) {
399 /* wait for FIFO WR command to complete to avoid overflow */
400 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530401 swr_master_write(swrm, reg_addr[i], val[i]);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530402 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530403 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530404 }
405 return 0;
406}
407
408static bool swrm_is_port_en(struct swr_master *mstr)
409{
410 return !!(mstr->num_port);
411}
412
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530413static void copy_port_tables(struct swr_mstr_ctrl *swrm,
414 struct port_params *params)
415{
416 u8 i;
417 struct port_params *config = params;
418
419 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
420 /* wsa uses single frame structure for all configurations */
421 if (!swrm->mport_cfg[i].port_en)
422 continue;
423 swrm->mport_cfg[i].sinterval = config[i].si;
424 swrm->mport_cfg[i].offset1 = config[i].off1;
425 swrm->mport_cfg[i].offset2 = config[i].off2;
426 swrm->mport_cfg[i].hstart = config[i].hstart;
427 swrm->mport_cfg[i].hstop = config[i].hstop;
428 swrm->mport_cfg[i].blk_pack_mode = config[i].bp_mode;
429 swrm->mport_cfg[i].blk_grp_count = config[i].bgp_ctrl;
430 swrm->mport_cfg[i].word_length = config[i].wd_len;
431 swrm->mport_cfg[i].lane_ctrl = config[i].lane_ctrl;
432 }
433}
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530434static int swrm_get_port_config(struct swr_mstr_ctrl *swrm)
435{
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530436 struct port_params *params;
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530437 u32 usecase = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530438
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530439 /* TODO - Send usecase information to avoid checking for master_id */
440 if (swrm->mport_cfg[SWRM_DSD_PARAMS_PORT].port_en &&
441 (swrm->master_id == MASTER_ID_RX))
442 usecase = 1;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530443
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530444 params = swrm->port_param[usecase];
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530445 copy_port_tables(swrm, params);
Sudheer Papothi4c322b12018-10-31 06:34:01 +0530446
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530447 return 0;
448}
449
450static int swrm_get_master_port(struct swr_mstr_ctrl *swrm, u8 *mstr_port_id,
451 u8 *mstr_ch_mask, u8 mstr_prt_type,
452 u8 slv_port_id)
453{
454 int i, j;
455 *mstr_port_id = 0;
456
457 for (i = 1; i <= swrm->num_ports; i++) {
458 for (j = 0; j < SWR_MAX_CH_PER_PORT; j++) {
459 if (swrm->port_mapping[i][j].port_type == mstr_prt_type)
460 goto found;
461 }
462 }
463found:
464 if (i > swrm->num_ports || j == SWR_MAX_CH_PER_PORT) {
465 dev_err(swrm->dev, "%s: port type not supported by master\n",
466 __func__);
467 return -EINVAL;
468 }
469 /* id 0 corresponds to master port 1 */
470 *mstr_port_id = i - 1;
471 *mstr_ch_mask = swrm->port_mapping[i][j].ch_mask;
472
473 return 0;
474
475}
476
477static u32 swrm_get_packed_reg_val(u8 *cmd_id, u8 cmd_data,
478 u8 dev_addr, u16 reg_addr)
479{
480 u32 val;
481 u8 id = *cmd_id;
482
483 if (id != SWR_BROADCAST_CMD_ID) {
484 if (id < 14)
485 id += 1;
486 else
487 id = 0;
488 *cmd_id = id;
489 }
490 val = SWR_REG_VAL_PACK(cmd_data, dev_addr, id, reg_addr);
491
492 return val;
493}
494
495static int swrm_cmd_fifo_rd_cmd(struct swr_mstr_ctrl *swrm, int *cmd_data,
496 u8 dev_addr, u8 cmd_id, u16 reg_addr,
497 u32 len)
498{
499 u32 val;
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530500 u32 retry_attempt = 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530501
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530502 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530503 val = swrm_get_packed_reg_val(&swrm->rcmd_id, len, dev_addr, reg_addr);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530504 if (swrm->read) {
505 /* skip delay if read is handled in platform driver */
506 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
507 } else {
508 /* wait for FIFO RD to complete to avoid overflow */
509 usleep_range(100, 105);
510 swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
511 /* wait for FIFO RD CMD complete to avoid overflow */
512 usleep_range(250, 255);
513 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530514retry_read:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530515 *cmd_data = swr_master_read(swrm, SWRM_CMD_FIFO_RD_FIFO_ADDR);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530516 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, rcmd_id: 0x%x, \
517 dev_num: 0x%x, cmd_data: 0x%x\n", __func__, reg_addr,
518 cmd_id, swrm->rcmd_id, dev_addr, *cmd_data);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530519 if ((((*cmd_data) & 0xF00) >> 8) != swrm->rcmd_id) {
520 if (retry_attempt < MAX_FIFO_RD_FAIL_RETRY) {
521 /* wait 500 us before retry on fifo read failure */
522 usleep_range(500, 505);
523 retry_attempt++;
524 goto retry_read;
525 } else {
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530526 dev_err_ratelimited(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, \
527 rcmd_id: 0x%x, dev_num: 0x%x, cmd_data: 0x%x\n",
528 __func__, reg_addr, cmd_id, swrm->rcmd_id,
529 dev_addr, *cmd_data);
530
Laxminath Kasamfbcaf322018-07-18 00:38:14 +0530531 dev_err_ratelimited(swrm->dev,
532 "%s: failed to read fifo\n", __func__);
533 }
534 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530535 mutex_unlock(&swrm->iolock);
536
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530537 return 0;
538}
539
540static int swrm_cmd_fifo_wr_cmd(struct swr_mstr_ctrl *swrm, u8 cmd_data,
541 u8 dev_addr, u8 cmd_id, u16 reg_addr)
542{
543 u32 val;
544 int ret = 0;
545
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530546 mutex_lock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530547 if (!cmd_id)
548 val = swrm_get_packed_reg_val(&swrm->wcmd_id, cmd_data,
549 dev_addr, reg_addr);
550 else
551 val = swrm_get_packed_reg_val(&cmd_id, cmd_data,
552 dev_addr, reg_addr);
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530553 dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x,wcmd_id: 0x%x, \
554 dev_num: 0x%x, cmd_data: 0x%x\n", __func__,
555 reg_addr, cmd_id, swrm->wcmd_id,dev_addr, cmd_data);
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +0530556 swr_master_write(swrm, SWRM_CMD_FIFO_WR_CMD, val);
Ramprasad Katkam1e906202019-01-30 14:16:34 +0530557 /*
558 * wait for FIFO WR command to complete to avoid overflow
559 * skip delay if write is handled in platform driver.
560 */
561 if(!swrm->write)
562 usleep_range(250, 255);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530563 if (cmd_id == 0xF) {
564 /*
565 * sleep for 10ms for MSM soundwire variant to allow broadcast
566 * command to complete.
567 */
568 if (swrm_is_msm_variant(swrm->version))
569 usleep_range(10000, 10100);
570 else
571 wait_for_completion_timeout(&swrm->broadcast,
572 (2 * HZ/10));
573 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530574 mutex_unlock(&swrm->iolock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530575 return ret;
576}
577
578static int swrm_read(struct swr_master *master, u8 dev_num, u16 reg_addr,
579 void *buf, u32 len)
580{
581 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
582 int ret = 0;
583 int val;
584 u8 *reg_val = (u8 *)buf;
585
586 if (!swrm) {
587 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
588 return -EINVAL;
589 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530590 if (!dev_num) {
591 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
592 return -EINVAL;
593 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530594 mutex_lock(&swrm->devlock);
595 if (!swrm->dev_up) {
596 mutex_unlock(&swrm->devlock);
597 return 0;
598 }
599 mutex_unlock(&swrm->devlock);
600
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530601 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530602 ret = swrm_cmd_fifo_rd_cmd(swrm, &val, dev_num, 0, reg_addr, len);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530603
604 if (!ret)
605 *reg_val = (u8)val;
606
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530607 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530608 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530609 return ret;
610}
611
612static int swrm_write(struct swr_master *master, u8 dev_num, u16 reg_addr,
613 const void *buf)
614{
615 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
616 int ret = 0;
617 u8 reg_val = *(u8 *)buf;
618
619 if (!swrm) {
620 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
621 return -EINVAL;
622 }
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530623 if (!dev_num) {
624 dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
625 return -EINVAL;
626 }
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530627 mutex_lock(&swrm->devlock);
628 if (!swrm->dev_up) {
629 mutex_unlock(&swrm->devlock);
630 return 0;
631 }
632 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530633
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530634 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam0db48012018-11-09 11:01:23 +0530635 ret = swrm_cmd_fifo_wr_cmd(swrm, reg_val, dev_num, 0, reg_addr);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530636
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530637 pm_runtime_put_autosuspend(swrm->dev);
638 pm_runtime_mark_last_busy(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530639 return ret;
640}
641
642static int swrm_bulk_write(struct swr_master *master, u8 dev_num, void *reg,
643 const void *buf, size_t len)
644{
645 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
646 int ret = 0;
647 int i;
648 u32 *val;
649 u32 *swr_fifo_reg;
650
651 if (!swrm || !swrm->handle) {
652 dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
653 return -EINVAL;
654 }
655 if (len <= 0)
656 return -EINVAL;
Laxminath Kasam1df09a82018-09-20 18:57:49 +0530657 mutex_lock(&swrm->devlock);
658 if (!swrm->dev_up) {
659 mutex_unlock(&swrm->devlock);
660 return 0;
661 }
662 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530663
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530664 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530665 if (dev_num) {
666 swr_fifo_reg = kcalloc(len, sizeof(u32), GFP_KERNEL);
667 if (!swr_fifo_reg) {
668 ret = -ENOMEM;
669 goto err;
670 }
671 val = kcalloc(len, sizeof(u32), GFP_KERNEL);
672 if (!val) {
673 ret = -ENOMEM;
674 goto mem_fail;
675 }
676
677 for (i = 0; i < len; i++) {
678 val[i] = swrm_get_packed_reg_val(&swrm->wcmd_id,
679 ((u8 *)buf)[i],
680 dev_num,
681 ((u16 *)reg)[i]);
682 swr_fifo_reg[i] = SWRM_CMD_FIFO_WR_CMD;
683 }
684 ret = swr_master_bulk_write(swrm, swr_fifo_reg, val, len);
685 if (ret) {
686 dev_err(&master->dev, "%s: bulk write failed\n",
687 __func__);
688 ret = -EINVAL;
689 }
690 } else {
691 dev_err(&master->dev,
692 "%s: No support of Bulk write for master regs\n",
693 __func__);
694 ret = -EINVAL;
695 goto err;
696 }
697 kfree(val);
698mem_fail:
699 kfree(swr_fifo_reg);
700err:
Ramprasad Katkam1f221262018-08-23 15:01:22 +0530701 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530702 pm_runtime_mark_last_busy(swrm->dev);
703 return ret;
704}
705
706static u8 get_inactive_bank_num(struct swr_mstr_ctrl *swrm)
707{
708 return (swr_master_read(swrm, SWRM_MCP_STATUS) &
709 SWRM_MCP_STATUS_BANK_NUM_MASK) ? 0 : 1;
710}
711
712static void enable_bank_switch(struct swr_mstr_ctrl *swrm, u8 bank,
713 u8 row, u8 col)
714{
715 swrm_cmd_fifo_wr_cmd(swrm, ((row << 3) | col), 0xF, 0xF,
716 SWRS_SCP_FRAME_CTRL_BANK(bank));
717}
718
719static struct swr_port_info *swrm_get_port_req(struct swrm_mports *mport,
720 u8 slv_port, u8 dev_num)
721{
722 struct swr_port_info *port_req = NULL;
723
724 list_for_each_entry(port_req, &mport->port_req_list, list) {
725 /* Store dev_id instead of dev_num if enumeration is changed run_time */
726 if ((port_req->slave_port_id == slv_port)
727 && (port_req->dev_num == dev_num))
728 return port_req;
729 }
730 return NULL;
731}
732
733static bool swrm_remove_from_group(struct swr_master *master)
734{
735 struct swr_device *swr_dev;
736 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
737 bool is_removed = false;
738
739 if (!swrm)
740 goto end;
741
742 mutex_lock(&swrm->mlock);
743 if ((swrm->num_rx_chs > 1) &&
744 (swrm->num_rx_chs == swrm->num_cfg_devs)) {
745 list_for_each_entry(swr_dev, &master->devices,
746 dev_list) {
747 swr_dev->group_id = SWR_GROUP_NONE;
748 master->gr_sid = 0;
749 }
750 is_removed = true;
751 }
752 mutex_unlock(&swrm->mlock);
753
754end:
755 return is_removed;
756}
757
758static void swrm_disable_ports(struct swr_master *master,
759 u8 bank)
760{
761 u32 value;
762 struct swr_port_info *port_req;
763 int i;
764 struct swrm_mports *mport;
765 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
766
767 if (!swrm) {
768 pr_err("%s: swrm is null\n", __func__);
769 return;
770 }
771
772 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
773 master->num_port);
774
775
776 for (i = 0; i < SWR_MSTR_PORT_LEN ; i++) {
777
778 mport = &(swrm->mport_cfg[i]);
779 if (!mport->port_en)
780 continue;
781
782 list_for_each_entry(port_req, &mport->port_req_list, list) {
783 /* skip ports with no change req's*/
784 if (port_req->req_ch == port_req->ch_en)
785 continue;
786
787 swrm_cmd_fifo_wr_cmd(swrm, port_req->req_ch,
788 port_req->dev_num, 0x00,
789 SWRS_DP_CHANNEL_ENABLE_BANK(port_req->slave_port_id,
790 bank));
791 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x\n",
792 __func__, i,
793 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)));
794 }
795 value = ((mport->req_ch)
796 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
797 value |= ((mport->offset2)
798 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
799 value |= ((mport->offset1)
800 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
801 value |= mport->sinterval;
802
803 swr_master_write(swrm,
804 SWRM_DP_PORT_CTRL_BANK(i+1, bank),
805 value);
806 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
807 __func__, i,
808 (SWRM_DP_PORT_CTRL_BANK(i+1, bank)), value);
809 }
810}
811
812static void swrm_cleanup_disabled_port_reqs(struct swr_master *master)
813{
814 struct swr_port_info *port_req, *next;
815 int i;
816 struct swrm_mports *mport;
817 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
818
819 if (!swrm) {
820 pr_err("%s: swrm is null\n", __func__);
821 return;
822 }
823 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
824 master->num_port);
825
826 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
827 mport = &(swrm->mport_cfg[i]);
828 list_for_each_entry_safe(port_req, next,
829 &mport->port_req_list, list) {
830 /* skip ports without new ch req */
831 if (port_req->ch_en == port_req->req_ch)
832 continue;
833
834 /* remove new ch req's*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +0530835 port_req->ch_en = port_req->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530836
837 /* If no streams enabled on port, remove the port req */
838 if (port_req->ch_en == 0) {
839 list_del(&port_req->list);
840 kfree(port_req);
841 }
842 }
843 /* remove new ch req's on mport*/
Ramprasad Katkamc8d52a12018-08-31 02:30:00 +0530844 mport->ch_en = mport->req_ch;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530845
846 if (!(mport->ch_en)) {
847 mport->port_en = false;
848 master->port_en_mask &= ~i;
849 }
850 }
851}
852static void swrm_copy_data_port_config(struct swr_master *master, u8 bank)
853{
854 u32 value, slv_id;
855 struct swr_port_info *port_req;
856 int i;
857 struct swrm_mports *mport;
858 u32 reg[SWRM_MAX_PORT_REG];
859 u32 val[SWRM_MAX_PORT_REG];
860 int len = 0;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530861 u8 hparams;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530862 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
863
864 if (!swrm) {
865 pr_err("%s: swrm is null\n", __func__);
866 return;
867 }
868
869 dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
870 master->num_port);
871
872 for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
873 mport = &(swrm->mport_cfg[i]);
874 if (!mport->port_en)
875 continue;
876
877 list_for_each_entry(port_req, &mport->port_req_list, list) {
878 slv_id = port_req->slave_port_id;
879 reg[len] = SWRM_CMD_FIFO_WR_CMD;
880 val[len++] = SWR_REG_VAL_PACK(port_req->req_ch,
881 port_req->dev_num, 0x00,
882 SWRS_DP_CHANNEL_ENABLE_BANK(slv_id,
883 bank));
884
885 reg[len] = SWRM_CMD_FIFO_WR_CMD;
886 val[len++] = SWR_REG_VAL_PACK(mport->sinterval,
887 port_req->dev_num, 0x00,
888 SWRS_DP_SAMPLE_CONTROL_1_BANK(slv_id,
889 bank));
890
891 reg[len] = SWRM_CMD_FIFO_WR_CMD;
892 val[len++] = SWR_REG_VAL_PACK(mport->offset1,
893 port_req->dev_num, 0x00,
894 SWRS_DP_OFFSET_CONTROL_1_BANK(slv_id,
895 bank));
896
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530897 if (mport->offset2 != SWR_INVALID_PARAM) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530898 reg[len] = SWRM_CMD_FIFO_WR_CMD;
899 val[len++] = SWR_REG_VAL_PACK(mport->offset2,
900 port_req->dev_num, 0x00,
901 SWRS_DP_OFFSET_CONTROL_2_BANK(
902 slv_id, bank));
903 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530904 if (mport->hstart != SWR_INVALID_PARAM
905 && mport->hstop != SWR_INVALID_PARAM) {
906 hparams = (mport->hstart << 4) | mport->hstop;
907
908 reg[len] = SWRM_CMD_FIFO_WR_CMD;
909 val[len++] = SWR_REG_VAL_PACK(hparams,
910 port_req->dev_num, 0x00,
911 SWRS_DP_HCONTROL_BANK(slv_id,
912 bank));
913 }
914 if (mport->word_length != SWR_INVALID_PARAM) {
915 reg[len] = SWRM_CMD_FIFO_WR_CMD;
916 val[len++] =
917 SWR_REG_VAL_PACK(mport->word_length,
918 port_req->dev_num, 0x00,
919 SWRS_DP_BLOCK_CONTROL_1(slv_id));
920 }
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +0530921 if (mport->blk_pack_mode != SWR_INVALID_PARAM
922 && swrm->master_id != MASTER_ID_WSA) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530923 reg[len] = SWRM_CMD_FIFO_WR_CMD;
924 val[len++] =
925 SWR_REG_VAL_PACK(mport->blk_pack_mode,
926 port_req->dev_num, 0x00,
927 SWRS_DP_BLOCK_CONTROL_3_BANK(slv_id,
928 bank));
929 }
930 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
931 reg[len] = SWRM_CMD_FIFO_WR_CMD;
932 val[len++] =
933 SWR_REG_VAL_PACK(mport->blk_grp_count,
934 port_req->dev_num, 0x00,
935 SWRS_DP_BLOCK_CONTROL_2_BANK(slv_id,
936 bank));
937 }
938 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
939 reg[len] = SWRM_CMD_FIFO_WR_CMD;
940 val[len++] =
941 SWR_REG_VAL_PACK(mport->lane_ctrl,
942 port_req->dev_num, 0x00,
943 SWRS_DP_LANE_CONTROL_BANK(slv_id,
944 bank));
945 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530946 port_req->ch_en = port_req->req_ch;
947 }
948 value = ((mport->req_ch)
949 << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
Ramprasad Katkam2a0996b2018-09-25 20:13:30 +0530950
951 if (mport->offset2 != SWR_INVALID_PARAM)
952 value |= ((mport->offset2)
953 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530954 value |= ((mport->offset1)
955 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
956 value |= mport->sinterval;
957
958
959 reg[len] = SWRM_DP_PORT_CTRL_BANK(i + 1, bank);
960 val[len++] = value;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530961 dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
962 __func__, i,
963 (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)), value);
964
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530965 if (mport->lane_ctrl != SWR_INVALID_PARAM) {
966 reg[len] = SWRM_DP_PORT_CTRL_2_BANK(i + 1, bank);
967 val[len++] = mport->lane_ctrl;
968 }
969 if (mport->word_length != SWR_INVALID_PARAM) {
970 reg[len] = SWRM_DP_BLOCK_CTRL_1(i + 1);
971 val[len++] = mport->word_length;
972 }
973
974 if (mport->blk_grp_count != SWR_INVALID_PARAM) {
975 reg[len] = SWRM_DP_BLOCK_CTRL2_BANK(i + 1, bank);
976 val[len++] = mport->blk_grp_count;
977 }
978 if (mport->hstart != SWR_INVALID_PARAM
979 && mport->hstop != SWR_INVALID_PARAM) {
980 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
Laxminath Kasame30eef72018-11-05 17:40:09 +0530981 hparams = (mport->hstop << 4) | mport->hstart;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530982 val[len++] = hparams;
Laxminath Kasam990c70b2018-11-09 23:15:09 +0530983 } else {
984 reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
985 hparams = (SWR_HSTOP_MAX_VAL << 4) | SWR_HSTART_MIN_VAL;
986 val[len++] = hparams;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +0530987 }
988 if (mport->blk_pack_mode != SWR_INVALID_PARAM) {
989 reg[len] = SWRM_DP_BLOCK_CTRL3_BANK(i + 1, bank);
990 val[len++] = mport->blk_pack_mode;
991 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530992 mport->ch_en = mport->req_ch;
993
994 }
Sudheer Papothi0016db12019-06-11 04:42:38 +0530995 swrm_reg_dump(swrm, reg, val, len, __func__);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +0530996 swr_master_bulk_write(swrm, reg, val, len);
997}
998
999static void swrm_apply_port_config(struct swr_master *master)
1000{
1001 u8 bank;
1002 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1003
1004 if (!swrm) {
1005 pr_err("%s: Invalid handle to swr controller\n",
1006 __func__);
1007 return;
1008 }
1009
1010 bank = get_inactive_bank_num(swrm);
1011 dev_dbg(swrm->dev, "%s: enter bank: %d master_ports: %d\n",
1012 __func__, bank, master->num_port);
1013
1014
1015 swrm_cmd_fifo_wr_cmd(swrm, 0x01, 0xF, 0x00,
1016 SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(bank));
1017
1018 swrm_copy_data_port_config(master, bank);
1019}
1020
1021static int swrm_slvdev_datapath_control(struct swr_master *master, bool enable)
1022{
1023 u8 bank;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301024 u32 value, n_row, n_col;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301025 int ret;
1026 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1027 int mask = (SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK |
1028 SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK |
1029 SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_BMSK);
1030 u8 inactive_bank;
1031
1032 if (!swrm) {
1033 pr_err("%s: swrm is null\n", __func__);
1034 return -EFAULT;
1035 }
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301036
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301037 mutex_lock(&swrm->mlock);
1038
Ramprasad Katkam979b7c92019-05-17 15:31:21 +05301039 /*
1040 * During disable if master is already down, which implies an ssr/pdr
1041 * scenario, just mark ports as disabled and exit
1042 */
1043 if (swrm->state == SWR_MSTR_SSR && !enable) {
1044 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1045 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1046 __func__);
1047 goto exit;
1048 }
1049 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
1050 swrm_cleanup_disabled_port_reqs(master);
1051 if (!swrm_is_port_en(master)) {
1052 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1053 __func__);
1054 pm_runtime_mark_last_busy(swrm->dev);
1055 pm_runtime_put_autosuspend(swrm->dev);
1056 }
1057 goto exit;
1058 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301059 bank = get_inactive_bank_num(swrm);
1060
1061 if (enable) {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301062 if (!test_bit(ENABLE_PENDING, &swrm->port_req_pending)) {
1063 dev_dbg(swrm->dev, "%s:No pending connect port req\n",
1064 __func__);
1065 goto exit;
1066 }
1067 clear_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301068 ret = swrm_get_port_config(swrm);
1069 if (ret) {
1070 /* cannot accommodate ports */
1071 swrm_cleanup_disabled_port_reqs(master);
1072 mutex_unlock(&swrm->mlock);
1073 return -EINVAL;
1074 }
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301075 swr_master_write(swrm, SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301076 SWRM_INTERRUPT_STATUS_MASK);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301077 /* apply the new port config*/
1078 swrm_apply_port_config(master);
1079 } else {
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301080 if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
1081 dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
1082 __func__);
1083 goto exit;
1084 }
1085 clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301086 swrm_disable_ports(master, bank);
1087 }
1088 dev_dbg(swrm->dev, "%s: enable: %d, cfg_devs: %d\n",
1089 __func__, enable, swrm->num_cfg_devs);
1090
1091 if (enable) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301092 /* set col = 16 */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301093 n_col = SWR_MAX_COL;
1094 } else {
1095 /*
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301096 * Do not change to col = 2 if there are still active ports
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301097 */
1098 if (!master->num_port)
1099 n_col = SWR_MIN_COL;
1100 else
1101 n_col = SWR_MAX_COL;
1102 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301103 /* Use default 50 * x, frame shape. Change based on mclk */
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05301104 if (swrm->mclk_freq == MCLK_FREQ_NATIVE) {
1105 dev_dbg(swrm->dev, "setting 64 x %d frameshape\n",
1106 n_col ? 16 : 2);
1107 n_row = SWR_ROW_64;
1108 } else {
1109 dev_dbg(swrm->dev, "setting 50 x %d frameshape\n",
1110 n_col ? 16 : 2);
1111 n_row = SWR_ROW_50;
1112 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301113 value = swr_master_read(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank));
1114 value &= (~mask);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301115 value |= ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301116 (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
1117 (0 << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
1118 swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1119
1120 dev_dbg(swrm->dev, "%s: regaddr: 0x%x, value: 0x%x\n", __func__,
1121 SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
1122
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301123 enable_bank_switch(swrm, bank, n_row, n_col);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301124 inactive_bank = bank ? 0 : 1;
1125
1126 if (enable)
1127 swrm_copy_data_port_config(master, inactive_bank);
1128 else {
1129 swrm_disable_ports(master, inactive_bank);
1130 swrm_cleanup_disabled_port_reqs(master);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301131 }
1132 if (!swrm_is_port_en(master)) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301133 dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
1134 __func__);
1135 pm_runtime_mark_last_busy(swrm->dev);
1136 pm_runtime_put_autosuspend(swrm->dev);
1137 }
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301138exit:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301139 mutex_unlock(&swrm->mlock);
1140return 0;
1141}
1142
1143static int swrm_connect_port(struct swr_master *master,
1144 struct swr_params *portinfo)
1145{
1146 int i;
1147 struct swr_port_info *port_req;
1148 int ret = 0;
1149 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1150 struct swrm_mports *mport;
1151 u8 mstr_port_id, mstr_ch_msk;
1152
1153 dev_dbg(&master->dev, "%s: enter\n", __func__);
1154 if (!portinfo)
1155 return -EINVAL;
1156
1157 if (!swrm) {
1158 dev_err(&master->dev,
1159 "%s: Invalid handle to swr controller\n",
1160 __func__);
1161 return -EINVAL;
1162 }
1163
1164 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301165 mutex_lock(&swrm->devlock);
1166 if (!swrm->dev_up) {
1167 mutex_unlock(&swrm->devlock);
1168 mutex_unlock(&swrm->mlock);
1169 return -EINVAL;
1170 }
1171 mutex_unlock(&swrm->devlock);
Ramprasad Katkam7cb4ff62018-09-12 04:00:26 +05301172 if (!swrm_is_port_en(master))
1173 pm_runtime_get_sync(swrm->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301174
1175 for (i = 0; i < portinfo->num_port; i++) {
1176 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_msk,
1177 portinfo->port_type[i],
1178 portinfo->port_id[i]);
1179 if (ret) {
1180 dev_err(&master->dev,
1181 "%s: mstr portid for slv port %d not found\n",
1182 __func__, portinfo->port_id[i]);
1183 goto port_fail;
1184 }
1185
1186 mport = &(swrm->mport_cfg[mstr_port_id]);
1187 /* get port req */
1188 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1189 portinfo->dev_num);
1190 if (!port_req) {
1191 dev_dbg(&master->dev, "%s: new req:port id %d dev %d\n",
1192 __func__, portinfo->port_id[i],
1193 portinfo->dev_num);
1194 port_req = kzalloc(sizeof(struct swr_port_info),
1195 GFP_KERNEL);
1196 if (!port_req) {
1197 ret = -ENOMEM;
1198 goto mem_fail;
1199 }
1200 port_req->dev_num = portinfo->dev_num;
1201 port_req->slave_port_id = portinfo->port_id[i];
1202 port_req->num_ch = portinfo->num_ch[i];
1203 port_req->ch_rate = portinfo->ch_rate[i];
1204 port_req->ch_en = 0;
1205 port_req->master_port_id = mstr_port_id;
1206 list_add(&port_req->list, &mport->port_req_list);
1207 }
1208 port_req->req_ch |= portinfo->ch_en[i];
1209
1210 dev_dbg(&master->dev,
1211 "%s: mstr port %d, slv port %d ch_rate %d num_ch %d\n",
1212 __func__, port_req->master_port_id,
1213 port_req->slave_port_id, port_req->ch_rate,
1214 port_req->num_ch);
1215 /* Put the port req on master port */
1216 mport = &(swrm->mport_cfg[mstr_port_id]);
1217 mport->port_en = true;
1218 mport->req_ch |= mstr_ch_msk;
1219 master->port_en_mask |= (1 << mstr_port_id);
1220 }
1221 master->num_port += portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301222 set_bit(ENABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301223 swr_port_response(master, portinfo->tid);
1224
1225 mutex_unlock(&swrm->mlock);
1226 return 0;
1227
1228port_fail:
1229mem_fail:
1230 /* cleanup port reqs in error condition */
1231 swrm_cleanup_disabled_port_reqs(master);
1232 mutex_unlock(&swrm->mlock);
1233 return ret;
1234}
1235
1236static int swrm_disconnect_port(struct swr_master *master,
1237 struct swr_params *portinfo)
1238{
1239 int i, ret = 0;
1240 struct swr_port_info *port_req;
1241 struct swrm_mports *mport;
1242 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
1243 u8 mstr_port_id, mstr_ch_mask;
1244
1245 if (!swrm) {
1246 dev_err(&master->dev,
1247 "%s: Invalid handle to swr controller\n",
1248 __func__);
1249 return -EINVAL;
1250 }
1251
1252 if (!portinfo) {
1253 dev_err(&master->dev, "%s: portinfo is NULL\n", __func__);
1254 return -EINVAL;
1255 }
1256 mutex_lock(&swrm->mlock);
1257
1258 for (i = 0; i < portinfo->num_port; i++) {
1259
1260 ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_mask,
1261 portinfo->port_type[i], portinfo->port_id[i]);
1262 if (ret) {
1263 dev_err(&master->dev,
1264 "%s: mstr portid for slv port %d not found\n",
1265 __func__, portinfo->port_id[i]);
1266 mutex_unlock(&swrm->mlock);
1267 return -EINVAL;
1268 }
1269 mport = &(swrm->mport_cfg[mstr_port_id]);
1270 /* get port req */
1271 port_req = swrm_get_port_req(mport, portinfo->port_id[i],
1272 portinfo->dev_num);
1273
1274 if (!port_req) {
1275 dev_err(&master->dev, "%s:port not enabled : port %d\n",
1276 __func__, portinfo->port_id[i]);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05301277 mutex_unlock(&swrm->mlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301278 return -EINVAL;
1279 }
1280 port_req->req_ch &= ~portinfo->ch_en[i];
1281 mport->req_ch &= ~mstr_ch_mask;
1282 }
1283 master->num_port -= portinfo->num_port;
Ramprasad Katkamcab8d722018-09-28 15:54:06 +05301284 set_bit(DISABLE_PENDING, &swrm->port_req_pending);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301285 swr_port_response(master, portinfo->tid);
1286 mutex_unlock(&swrm->mlock);
1287
1288 return 0;
1289}
1290
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301291static int swrm_find_alert_slave(struct swr_mstr_ctrl *swrm,
1292 int status, u8 *devnum)
1293{
1294 int i;
1295 bool found = false;
1296
1297 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1298 if ((status & SWRM_MCP_SLV_STATUS_MASK) == SWR_ALERT) {
1299 *devnum = i;
1300 found = true;
1301 break;
1302 }
1303 status >>= 2;
1304 }
1305 if (found)
1306 return 0;
1307 else
1308 return -EINVAL;
1309}
1310
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301311static int swrm_check_slave_change_status(struct swr_mstr_ctrl *swrm,
1312 int status, u8 *devnum)
1313{
1314 int i;
1315 int new_sts = status;
1316 int ret = SWR_NOT_PRESENT;
1317
1318 if (status != swrm->slave_status) {
1319 for (i = 0; i < (swrm->master.num_dev + 1); i++) {
1320 if ((status & SWRM_MCP_SLV_STATUS_MASK) !=
1321 (swrm->slave_status & SWRM_MCP_SLV_STATUS_MASK)) {
1322 ret = (status & SWRM_MCP_SLV_STATUS_MASK);
1323 *devnum = i;
1324 break;
1325 }
1326 status >>= 2;
1327 swrm->slave_status >>= 2;
1328 }
1329 swrm->slave_status = new_sts;
1330 }
1331 return ret;
1332}
1333
1334static irqreturn_t swr_mstr_interrupt(int irq, void *dev)
1335{
1336 struct swr_mstr_ctrl *swrm = dev;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301337 u32 value, intr_sts, intr_sts_masked;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301338 u32 temp = 0;
1339 u32 status, chg_sts, i;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301340 u8 devnum = 0;
1341 int ret = IRQ_HANDLED;
1342 struct swr_device *swr_dev;
1343 struct swr_master *mstr = &swrm->master;
1344
Ramprasad Katkam57349872018-11-11 18:34:57 +05301345 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1346 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1347 return IRQ_NONE;
1348 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301349
1350 mutex_lock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301351 if (swrm_clk_request(swrm, true)) {
Ramprasad Katkam14efed62019-03-07 13:16:50 +05301352 dev_err_ratelimited(swrm->dev, "%s:clk request failed\n",
1353 __func__);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301354 mutex_unlock(&swrm->reslock);
1355 goto exit;
1356 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301357 mutex_unlock(&swrm->reslock);
1358
1359 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301360 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301361handle_irq:
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301362 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301363 value = intr_sts_masked & (1 << i);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301364 if (!value)
1365 continue;
1366
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301367 switch (value) {
1368 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1369 dev_dbg(swrm->dev, "Trigger irq to slave device\n");
1370 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301371 ret = swrm_find_alert_slave(swrm, status, &devnum);
1372 if (ret) {
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301373 dev_err_ratelimited(swrm->dev,
1374 "no slave alert found.spurious interrupt\n");
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05301375 break;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301376 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301377 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1378 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1379 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1380 SWRS_SCP_INT_STATUS_CLEAR_1);
1381 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1382 SWRS_SCP_INT_STATUS_CLEAR_1);
Ramprasad Katkam62d6d762018-09-20 17:50:28 +05301383
1384
1385 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1386 if (swr_dev->dev_num != devnum)
1387 continue;
1388 if (swr_dev->slave_irq) {
1389 do {
1390 handle_nested_irq(
1391 irq_find_mapping(
1392 swr_dev->slave_irq, 0));
1393 } while (swr_dev->slave_irq_pending);
1394 }
1395
1396 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301397 break;
1398 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1399 dev_dbg(swrm->dev, "SWR new slave attached\n");
1400 break;
1401 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1402 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1403 if (status == swrm->slave_status) {
1404 dev_dbg(swrm->dev,
1405 "%s: No change in slave status: %d\n",
1406 __func__, status);
1407 break;
1408 }
1409 chg_sts = swrm_check_slave_change_status(swrm, status,
1410 &devnum);
1411 switch (chg_sts) {
1412 case SWR_NOT_PRESENT:
1413 dev_dbg(swrm->dev, "device %d got detached\n",
1414 devnum);
1415 break;
1416 case SWR_ATTACHED_OK:
1417 dev_dbg(swrm->dev, "device %d got attached\n",
1418 devnum);
Ramprasad Katkamdebe8932018-09-25 18:08:18 +05301419 /* enable host irq from slave device*/
1420 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1421 SWRS_SCP_INT_STATUS_CLEAR_1);
1422 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1423 SWRS_SCP_INT_STATUS_MASK_1);
1424
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301425 break;
1426 case SWR_ALERT:
1427 dev_dbg(swrm->dev,
1428 "device %d has pending interrupt\n",
1429 devnum);
1430 break;
1431 }
1432 break;
1433 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1434 dev_err_ratelimited(swrm->dev,
1435 "SWR bus clsh detected\n");
1436 break;
1437 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1438 dev_dbg(swrm->dev, "SWR read FIFO overflow\n");
1439 break;
1440 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1441 dev_dbg(swrm->dev, "SWR read FIFO underflow\n");
1442 break;
1443 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1444 dev_dbg(swrm->dev, "SWR write FIFO overflow\n");
1445 break;
1446 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1447 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1448 dev_err_ratelimited(swrm->dev,
1449 "SWR CMD error, fifo status 0x%x, flushing fifo\n",
1450 value);
1451 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1452 break;
1453 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301454 dev_err_ratelimited(swrm->dev, "SWR Port collision detected\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301455 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301456 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301457 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301458 break;
1459 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1460 dev_dbg(swrm->dev, "SWR read enable valid mismatch\n");
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301461 swrm->intr_mask &=
Ramprasad Katkam18bc8e22018-10-25 15:04:24 +05301462 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1463 swr_master_write(swrm,
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301464 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301465 break;
1466 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1467 complete(&swrm->broadcast);
1468 dev_dbg(swrm->dev, "SWR cmd id finished\n");
1469 break;
1470 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_AUTO_ENUM_FINISHED:
1471 break;
1472 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED:
1473 break;
1474 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL:
1475 break;
1476 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED:
1477 complete(&swrm->reset);
1478 break;
1479 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED:
1480 break;
1481 default:
1482 dev_err_ratelimited(swrm->dev,
1483 "SWR unknown interrupt\n");
1484 ret = IRQ_NONE;
1485 break;
1486 }
1487 }
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301488 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1489 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
Ramprasad Katkam83303512018-10-11 17:34:22 +05301490
1491 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301492 intr_sts_masked = intr_sts & swrm->intr_mask;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301493
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301494 if (intr_sts_masked) {
Ramprasad Katkam83303512018-10-11 17:34:22 +05301495 dev_dbg(swrm->dev, "%s: new interrupt received\n", __func__);
1496 goto handle_irq;
1497 }
1498
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301499 mutex_lock(&swrm->reslock);
1500 swrm_clk_request(swrm, false);
1501 mutex_unlock(&swrm->reslock);
Aditya Bavanarif4a471d2019-02-19 17:57:12 +05301502exit:
Ramprasad Katkam57349872018-11-11 18:34:57 +05301503 swrm_unlock_sleep(swrm);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301504 return ret;
1505}
1506
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301507static irqreturn_t swr_mstr_interrupt_v2(int irq, void *dev)
1508{
1509 struct swr_mstr_ctrl *swrm = dev;
1510 u32 value, intr_sts, intr_sts_masked;
1511 u32 temp = 0;
1512 u32 status, chg_sts, i;
1513 u8 devnum = 0;
1514 int ret = IRQ_HANDLED;
1515 struct swr_device *swr_dev;
1516 struct swr_master *mstr = &swrm->master;
1517
1518 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1519 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1520 return IRQ_NONE;
1521 }
1522
1523 mutex_lock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301524 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
1525 ret = IRQ_NONE;
1526 goto exit;
1527 }
1528 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
1529 ret = IRQ_NONE;
1530 goto exit;
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001531 }
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301532 swrm_clk_request(swrm, true);
1533 mutex_unlock(&swrm->reslock);
1534
1535 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1536 intr_sts_masked = intr_sts & swrm->intr_mask;
1537handle_irq:
1538 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
1539 value = intr_sts_masked & (1 << i);
1540 if (!value)
1541 continue;
1542
1543 switch (value) {
1544 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
1545 dev_dbg(swrm->dev, "%s: Trigger irq to slave device\n",
1546 __func__);
1547 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1548 ret = swrm_find_alert_slave(swrm, status, &devnum);
1549 if (ret) {
1550 dev_err_ratelimited(swrm->dev,
1551 "%s: no slave alert found.spurious interrupt\n",
1552 __func__);
1553 break;
1554 }
1555 swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
1556 SWRS_SCP_INT_STATUS_CLEAR_1, 1);
1557 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1558 SWRS_SCP_INT_STATUS_CLEAR_1);
1559 swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
1560 SWRS_SCP_INT_STATUS_CLEAR_1);
1561
1562
1563 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1564 if (swr_dev->dev_num != devnum)
1565 continue;
1566 if (swr_dev->slave_irq) {
1567 do {
1568 handle_nested_irq(
1569 irq_find_mapping(
1570 swr_dev->slave_irq, 0));
1571 } while (swr_dev->slave_irq_pending);
1572 }
1573
1574 }
1575 break;
1576 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
1577 dev_dbg(swrm->dev, "%s: SWR new slave attached\n",
1578 __func__);
1579 break;
1580 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
1581 status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1582 if (status == swrm->slave_status) {
1583 dev_dbg(swrm->dev,
1584 "%s: No change in slave status: %d\n",
1585 __func__, status);
1586 break;
1587 }
1588 chg_sts = swrm_check_slave_change_status(swrm, status,
1589 &devnum);
1590 switch (chg_sts) {
1591 case SWR_NOT_PRESENT:
1592 dev_dbg(swrm->dev,
1593 "%s: device %d got detached\n",
1594 __func__, devnum);
1595 break;
1596 case SWR_ATTACHED_OK:
1597 dev_dbg(swrm->dev,
1598 "%s: device %d got attached\n",
1599 __func__, devnum);
1600 /* enable host irq from slave device*/
1601 swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
1602 SWRS_SCP_INT_STATUS_CLEAR_1);
1603 swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
1604 SWRS_SCP_INT_STATUS_MASK_1);
1605
1606 break;
1607 case SWR_ALERT:
1608 dev_dbg(swrm->dev,
1609 "%s: device %d has pending interrupt\n",
1610 __func__, devnum);
1611 break;
1612 }
1613 break;
1614 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
1615 dev_err_ratelimited(swrm->dev,
1616 "%s: SWR bus clsh detected\n",
1617 __func__);
1618 break;
1619 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
1620 dev_dbg(swrm->dev, "%s: SWR read FIFO overflow\n",
1621 __func__);
1622 break;
1623 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
1624 dev_dbg(swrm->dev, "%s: SWR read FIFO underflow\n",
1625 __func__);
1626 break;
1627 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
1628 dev_dbg(swrm->dev, "%s: SWR write FIFO overflow\n",
1629 __func__);
1630 break;
1631 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
1632 value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
1633 dev_err_ratelimited(swrm->dev,
1634 "%s: SWR CMD error, fifo status 0x%x, flushing fifo\n",
1635 __func__, value);
1636 swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
1637 break;
1638 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
1639 dev_err_ratelimited(swrm->dev,
1640 "%s: SWR Port collision detected\n",
1641 __func__);
1642 swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
1643 swr_master_write(swrm,
1644 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1645 break;
1646 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
1647 dev_dbg(swrm->dev,
1648 "%s: SWR read enable valid mismatch\n",
1649 __func__);
1650 swrm->intr_mask &=
1651 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
1652 swr_master_write(swrm,
1653 SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
1654 break;
1655 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
1656 complete(&swrm->broadcast);
1657 dev_dbg(swrm->dev, "%s: SWR cmd id finished\n",
1658 __func__);
1659 break;
1660 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED_V2:
1661 break;
1662 case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL_V2:
1663 break;
1664 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED_V2:
1665 break;
1666 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED_V2:
1667 break;
1668 case SWRM_INTERRUPT_STATUS_EXT_CLK_STOP_WAKEUP:
1669 if (swrm->state == SWR_MSTR_UP)
1670 dev_dbg(swrm->dev,
1671 "%s:SWR Master is already up\n",
1672 __func__);
1673 else
1674 dev_err_ratelimited(swrm->dev,
1675 "%s: SWR wokeup during clock stop\n",
1676 __func__);
1677 break;
1678 default:
1679 dev_err_ratelimited(swrm->dev,
1680 "%s: SWR unknown interrupt value: %d\n",
1681 __func__, value);
1682 ret = IRQ_NONE;
1683 break;
1684 }
1685 }
1686 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
1687 swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
1688
1689 intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
1690 intr_sts_masked = intr_sts & swrm->intr_mask;
1691
1692 if (intr_sts_masked) {
1693 dev_dbg(swrm->dev, "%s: new interrupt received\n", __func__);
1694 goto handle_irq;
1695 }
1696
1697 mutex_lock(&swrm->reslock);
1698 swrm_clk_request(swrm, false);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301699 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
1700 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Karthikeyan Mani035c50b2019-05-02 13:35:01 -07001701exit:
Sudheer Papothid19d0c52019-02-23 05:41:39 +05301702 mutex_unlock(&swrm->reslock);
1703 swrm_unlock_sleep(swrm);
1704 return ret;
1705}
1706
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301707static irqreturn_t swrm_wakeup_interrupt(int irq, void *dev)
1708{
1709 struct swr_mstr_ctrl *swrm = dev;
1710 int ret = IRQ_HANDLED;
1711
1712 if (!swrm || !(swrm->dev)) {
1713 pr_err("%s: swrm or dev is null\n", __func__);
1714 return IRQ_NONE;
1715 }
1716 mutex_lock(&swrm->devlock);
1717 if (!swrm->dev_up) {
1718 if (swrm->wake_irq > 0)
1719 disable_irq_nosync(swrm->wake_irq);
1720 mutex_unlock(&swrm->devlock);
1721 return ret;
1722 }
1723 mutex_unlock(&swrm->devlock);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301724 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1725 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1726 goto exit;
1727 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301728 if (swrm->wake_irq > 0)
1729 disable_irq_nosync(swrm->wake_irq);
1730 pm_runtime_get_sync(swrm->dev);
1731 pm_runtime_mark_last_busy(swrm->dev);
1732 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam44b7a962018-12-20 15:08:44 +05301733 swrm_unlock_sleep(swrm);
1734exit:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301735 return ret;
1736}
1737
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301738static void swrm_wakeup_work(struct work_struct *work)
1739{
1740 struct swr_mstr_ctrl *swrm;
1741
1742 swrm = container_of(work, struct swr_mstr_ctrl,
1743 wakeup_work);
1744 if (!swrm || !(swrm->dev)) {
1745 pr_err("%s: swrm or dev is null\n", __func__);
1746 return;
1747 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301748
1749 mutex_lock(&swrm->devlock);
1750 if (!swrm->dev_up) {
1751 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301752 goto exit;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301753 }
1754 mutex_unlock(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301755 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1756 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1757 goto exit;
1758 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301759 pm_runtime_get_sync(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301760 pm_runtime_mark_last_busy(swrm->dev);
1761 pm_runtime_put_autosuspend(swrm->dev);
Ramprasad Katkam57349872018-11-11 18:34:57 +05301762 swrm_unlock_sleep(swrm);
1763exit:
1764 pm_relax(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301765}
1766
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301767static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
1768{
1769 u32 val;
1770
1771 swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
1772 val = (swrm->slave_status >> (devnum * 2));
1773 val &= SWRM_MCP_SLV_STATUS_MASK;
1774 return val;
1775}
1776
1777static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
1778 u8 *dev_num)
1779{
1780 int i;
1781 u64 id = 0;
1782 int ret = -EINVAL;
1783 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1784 struct swr_device *swr_dev;
1785 u32 num_dev = 0;
1786
1787 if (!swrm) {
1788 pr_err("%s: Invalid handle to swr controller\n",
1789 __func__);
1790 return ret;
1791 }
1792 if (swrm->num_dev)
1793 num_dev = swrm->num_dev;
1794 else
1795 num_dev = mstr->num_dev;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05301796
1797 mutex_lock(&swrm->devlock);
1798 if (!swrm->dev_up) {
1799 mutex_unlock(&swrm->devlock);
1800 return ret;
1801 }
1802 mutex_unlock(&swrm->devlock);
1803
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301804 pm_runtime_get_sync(swrm->dev);
1805 for (i = 1; i < (num_dev + 1); i++) {
1806 id = ((u64)(swr_master_read(swrm,
1807 SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
1808 id |= swr_master_read(swrm,
1809 SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301810
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301811 /*
1812 * As pm_runtime_get_sync() brings all slaves out of reset
1813 * update logical device number for all slaves.
1814 */
1815 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
1816 if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
1817 u32 status = swrm_get_device_status(swrm, i);
1818
1819 if ((status == 0x01) || (status == 0x02)) {
1820 swr_dev->dev_num = i;
1821 if ((id & SWR_DEV_ID_MASK) == dev_id) {
1822 *dev_num = i;
1823 ret = 0;
1824 }
1825 dev_dbg(swrm->dev,
1826 "%s: devnum %d is assigned for dev addr %lx\n",
1827 __func__, i, swr_dev->addr);
1828 }
1829 }
1830 }
1831 }
1832 if (ret)
1833 dev_err(swrm->dev, "%s: device 0x%llx is not ready\n",
1834 __func__, dev_id);
1835
1836 pm_runtime_mark_last_busy(swrm->dev);
1837 pm_runtime_put_autosuspend(swrm->dev);
1838 return ret;
1839}
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301840
1841static void swrm_device_wakeup_vote(struct swr_master *mstr)
1842{
1843 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1844
1845 if (!swrm) {
1846 pr_err("%s: Invalid handle to swr controller\n",
1847 __func__);
1848 return;
1849 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05301850 if (unlikely(swrm_lock_sleep(swrm) == false)) {
1851 dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
1852 return;
1853 }
Sudheer Papothi384addd2019-06-14 02:26:52 +05301854 if (++swrm->hw_core_clk_en == 1)
1855 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
1856 dev_err(swrm->dev, "%s:lpass core hw enable failed\n",
1857 __func__);
1858 --swrm->hw_core_clk_en;
1859 }
1860 if ( ++swrm->aud_core_clk_en == 1)
1861 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
1862 dev_err(swrm->dev, "%s:lpass audio hw enable failed\n",
1863 __func__);
1864 --swrm->aud_core_clk_en;
1865 }
1866 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
1867 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301868 pm_runtime_get_sync(swrm->dev);
1869}
1870
1871static void swrm_device_wakeup_unvote(struct swr_master *mstr)
1872{
1873 struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
1874
1875 if (!swrm) {
1876 pr_err("%s: Invalid handle to swr controller\n",
1877 __func__);
1878 return;
1879 }
1880 pm_runtime_mark_last_busy(swrm->dev);
1881 pm_runtime_put_autosuspend(swrm->dev);
Sudheer Papothi384addd2019-06-14 02:26:52 +05301882 dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
1883 __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
1884
1885 --swrm->aud_core_clk_en;
1886 if (swrm->aud_core_clk_en < 0)
1887 swrm->aud_core_clk_en = 0;
1888 else if (swrm->aud_core_clk_en == 0)
1889 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
1890
1891 --swrm->hw_core_clk_en;
1892 if (swrm->hw_core_clk_en < 0)
1893 swrm->hw_core_clk_en = 0;
1894 else if (swrm->hw_core_clk_en == 0)
1895 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
1896
Ramprasad Katkam57349872018-11-11 18:34:57 +05301897 swrm_unlock_sleep(swrm);
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05301898}
1899
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301900static int swrm_master_init(struct swr_mstr_ctrl *swrm)
1901{
1902 int ret = 0;
1903 u32 val;
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05301904 u8 row_ctrl = SWR_ROW_50;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301905 u8 col_ctrl = SWR_MIN_COL;
1906 u8 ssp_period = 1;
1907 u8 retry_cmd_num = 3;
1908 u32 reg[SWRM_MAX_INIT_REG];
1909 u32 value[SWRM_MAX_INIT_REG];
1910 int len = 0;
1911
1912 /* Clear Rows and Cols */
1913 val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
1914 (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
1915 (ssp_period << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
1916
1917 reg[len] = SWRM_MCP_FRAME_CTRL_BANK_ADDR(0);
1918 value[len++] = val;
1919
1920 /* Set Auto enumeration flag */
1921 reg[len] = SWRM_ENUMERATOR_CFG_ADDR;
1922 value[len++] = 1;
1923
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301924 /* Configure No pings */
1925 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
1926 val &= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK;
1927 val |= (0x1f << SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT);
1928 reg[len] = SWRM_MCP_CFG_ADDR;
1929 value[len++] = val;
1930
1931 /* Configure number of retries of a read/write cmd */
1932 val = (retry_cmd_num << SWRM_CMD_FIFO_CFG_NUM_OF_CMD_RETRY_SHFT);
1933 reg[len] = SWRM_CMD_FIFO_CFG_ADDR;
1934 value[len++] = val;
1935
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301936 reg[len] = SWRM_MCP_BUS_CTRL_ADDR;
1937 value[len++] = 0x2;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301938
Ramprasad Katkam83303512018-10-11 17:34:22 +05301939 /* Set IRQ to PULSE */
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301940 reg[len] = SWRM_COMP_CFG_ADDR;
Ramprasad Katkam83303512018-10-11 17:34:22 +05301941 value[len++] = 0x02;
1942
1943 reg[len] = SWRM_COMP_CFG_ADDR;
1944 value[len++] = 0x03;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301945
1946 reg[len] = SWRM_INTERRUPT_CLEAR;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301947 value[len++] = 0xFFFFFFFF;
1948
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301949 swrm->intr_mask = SWRM_INTERRUPT_STATUS_MASK;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301950 /* Mask soundwire interrupts */
1951 reg[len] = SWRM_INTERRUPT_MASK_ADDR;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301952 value[len++] = swrm->intr_mask;
Ramprasad Katkam1f221262018-08-23 15:01:22 +05301953
1954 reg[len] = SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN;
Ramprasad Katkam7e354782018-11-21 15:52:54 +05301955 value[len++] = swrm->intr_mask;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301956
1957 swr_master_bulk_write(swrm, reg, value, len);
1958
Sudheer Papothi63f48152018-11-15 01:08:03 +05301959 /*
1960 * For SWR master version 1.5.1, continue
1961 * execute on command ignore.
1962 */
1963 if (swrm->version == SWRM_VERSION_1_5_1)
1964 swr_master_write(swrm, SWRM_CMD_FIFO_CFG_ADDR,
1965 (swr_master_read(swrm,
1966 SWRM_CMD_FIFO_CFG_ADDR) | 0x80000000));
1967
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05301968 return ret;
1969}
1970
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05301971static int swrm_event_notify(struct notifier_block *self,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301972 unsigned long action, void *data)
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05301973{
1974 struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301975 event_notifier);
1976
1977 if (!swrm || !(swrm->dev)) {
1978 pr_err("%s: swrm or dev is NULL\n", __func__);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05301979 return -EINVAL;
1980 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301981 switch (action) {
1982 case MSM_AUD_DC_EVENT:
1983 schedule_work(&(swrm->dc_presence_work));
1984 break;
1985 case SWR_WAKE_IRQ_EVENT:
Aditya Bavanaric034fad2018-11-12 22:55:11 +05301986 if (swrm->ipc_wakeup && !swrm->ipc_wakeup_triggered) {
1987 swrm->ipc_wakeup_triggered = true;
Ramprasad Katkam57349872018-11-11 18:34:57 +05301988 pm_stay_awake(swrm->dev);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301989 schedule_work(&swrm->wakeup_work);
Ramprasad Katkamcd61c6e2018-09-18 13:22:58 +05301990 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05301991 break;
1992 default:
1993 dev_err(swrm->dev, "%s: invalid event type: %lu\n",
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05301994 __func__, action);
1995 return -EINVAL;
1996 }
1997
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05301998 return 0;
1999}
2000
2001static void swrm_notify_work_fn(struct work_struct *work)
2002{
2003 struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl,
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302004 dc_presence_work);
2005
2006 if (!swrm || !swrm->pdev) {
2007 pr_err("%s: swrm or pdev is NULL\n", __func__);
2008 return;
2009 }
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302010 swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL);
2011}
2012
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302013static int swrm_probe(struct platform_device *pdev)
2014{
2015 struct swr_mstr_ctrl *swrm;
2016 struct swr_ctrl_platform_data *pdata;
2017 u32 i, num_ports, port_num, port_type, ch_mask;
2018 u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0;
2019 int ret = 0;
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302020 struct clk *lpass_core_hw_vote = NULL;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302021 struct clk *lpass_core_audio = NULL;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302022
2023 /* Allocate soundwire master driver structure */
2024 swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl),
2025 GFP_KERNEL);
2026 if (!swrm) {
2027 ret = -ENOMEM;
2028 goto err_memory_fail;
2029 }
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302030 swrm->pdev = pdev;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302031 swrm->dev = &pdev->dev;
2032 platform_set_drvdata(pdev, swrm);
2033 swr_set_ctrl_data(&swrm->master, swrm);
2034 pdata = dev_get_platdata(&pdev->dev);
2035 if (!pdata) {
2036 dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
2037 __func__);
2038 ret = -EINVAL;
2039 goto err_pdata_fail;
2040 }
2041 swrm->handle = (void *)pdata->handle;
2042 if (!swrm->handle) {
2043 dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
2044 __func__);
2045 ret = -EINVAL;
2046 goto err_pdata_fail;
2047 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302048 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr_master_id",
2049 &swrm->master_id);
2050 if (ret) {
2051 dev_err(&pdev->dev, "%s: failed to get master id\n", __func__);
2052 goto err_pdata_fail;
2053 }
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302054 if (!(of_property_read_u32(pdev->dev.of_node,
2055 "swrm-io-base", &swrm->swrm_base_reg)))
2056 ret = of_property_read_u32(pdev->dev.of_node,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302057 "swrm-io-base", &swrm->swrm_base_reg);
2058 if (!swrm->swrm_base_reg) {
2059 swrm->read = pdata->read;
2060 if (!swrm->read) {
2061 dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
2062 __func__);
2063 ret = -EINVAL;
2064 goto err_pdata_fail;
2065 }
2066 swrm->write = pdata->write;
2067 if (!swrm->write) {
2068 dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
2069 __func__);
2070 ret = -EINVAL;
2071 goto err_pdata_fail;
2072 }
2073 swrm->bulk_write = pdata->bulk_write;
2074 if (!swrm->bulk_write) {
2075 dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
2076 __func__);
2077 ret = -EINVAL;
2078 goto err_pdata_fail;
2079 }
2080 } else {
2081 swrm->swrm_dig_base = devm_ioremap(&pdev->dev,
2082 swrm->swrm_base_reg, SWRM_MAX_REGISTER);
2083 }
2084
2085 swrm->clk = pdata->clk;
2086 if (!swrm->clk) {
2087 dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
2088 __func__);
2089 ret = -EINVAL;
2090 goto err_pdata_fail;
2091 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302092 if (of_property_read_u32(pdev->dev.of_node,
2093 "qcom,swr-clock-stop-mode0",
2094 &swrm->clk_stop_mode0_supp)) {
2095 swrm->clk_stop_mode0_supp = FALSE;
2096 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05302097
2098 ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
2099 &swrm->num_dev);
2100 if (ret) {
2101 dev_dbg(&pdev->dev, "%s: Looking up %s property failed\n",
2102 __func__, "qcom,swr-num-dev");
2103 } else {
2104 if (swrm->num_dev > SWR_MAX_SLAVE_DEVICES) {
2105 dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
2106 __func__, swrm->num_dev, SWR_MAX_SLAVE_DEVICES);
2107 ret = -EINVAL;
2108 goto err_pdata_fail;
2109 }
2110 }
2111
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302112 /* Parse soundwire port mapping */
2113 ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
2114 &num_ports);
2115 if (ret) {
2116 dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
2117 goto err_pdata_fail;
2118 }
2119 swrm->num_ports = num_ports;
2120
2121 if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
2122 &map_size)) {
2123 dev_err(swrm->dev, "missing port mapping\n");
2124 goto err_pdata_fail;
2125 }
2126
2127 map_length = map_size / (3 * sizeof(u32));
2128 if (num_ports > SWR_MSTR_PORT_LEN) {
2129 dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
2130 __func__);
2131 ret = -EINVAL;
2132 goto err_pdata_fail;
2133 }
2134 temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
2135
2136 if (!temp) {
2137 ret = -ENOMEM;
2138 goto err_pdata_fail;
2139 }
2140 ret = of_property_read_u32_array(pdev->dev.of_node,
2141 "qcom,swr-port-mapping", temp, 3 * map_length);
2142 if (ret) {
2143 dev_err(swrm->dev, "%s: Failed to read port mapping\n",
2144 __func__);
2145 goto err_pdata_fail;
2146 }
2147
2148 for (i = 0; i < map_length; i++) {
2149 port_num = temp[3 * i];
2150 port_type = temp[3 * i + 1];
2151 ch_mask = temp[3 * i + 2];
2152
2153 if (port_num != old_port_num)
2154 ch_iter = 0;
2155 swrm->port_mapping[port_num][ch_iter].port_type = port_type;
2156 swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
2157 old_port_num = port_num;
2158 }
2159 devm_kfree(&pdev->dev, temp);
2160
2161 swrm->reg_irq = pdata->reg_irq;
2162 swrm->master.read = swrm_read;
2163 swrm->master.write = swrm_write;
2164 swrm->master.bulk_write = swrm_bulk_write;
2165 swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
2166 swrm->master.connect_port = swrm_connect_port;
2167 swrm->master.disconnect_port = swrm_disconnect_port;
2168 swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
2169 swrm->master.remove_from_group = swrm_remove_from_group;
Sudheer Papothi6abd2de2018-09-05 05:57:04 +05302170 swrm->master.device_wakeup_vote = swrm_device_wakeup_vote;
2171 swrm->master.device_wakeup_unvote = swrm_device_wakeup_unvote;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302172 swrm->master.dev.parent = &pdev->dev;
2173 swrm->master.dev.of_node = pdev->dev.of_node;
2174 swrm->master.num_port = 0;
2175 swrm->rcmd_id = 0;
2176 swrm->wcmd_id = 0;
2177 swrm->slave_status = 0;
2178 swrm->num_rx_chs = 0;
2179 swrm->clk_ref_count = 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302180 swrm->swr_irq_wakeup_capable = 0;
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302181 swrm->mclk_freq = MCLK_FREQ;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302182 swrm->dev_up = true;
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302183 swrm->state = SWR_MSTR_UP;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302184 swrm->ipc_wakeup = false;
2185 swrm->ipc_wakeup_triggered = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302186 init_completion(&swrm->reset);
2187 init_completion(&swrm->broadcast);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302188 init_completion(&swrm->clk_off_complete);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302189 mutex_init(&swrm->mlock);
2190 mutex_init(&swrm->reslock);
2191 mutex_init(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302192 mutex_init(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302193 mutex_init(&swrm->clklock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302194 mutex_init(&swrm->devlock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302195 mutex_init(&swrm->pm_lock);
2196 swrm->wlock_holders = 0;
2197 swrm->pm_state = SWRM_PM_SLEEPABLE;
2198 init_waitqueue_head(&swrm->pm_wq);
2199 pm_qos_add_request(&swrm->pm_qos_req,
2200 PM_QOS_CPU_DMA_LATENCY,
2201 PM_QOS_DEFAULT_VALUE);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302202
2203 for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++)
2204 INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
2205
2206 if (swrm->reg_irq) {
2207 ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
2208 SWR_IRQ_REGISTER);
2209 if (ret) {
2210 dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
2211 __func__, ret);
2212 goto err_irq_fail;
2213 }
2214 } else {
2215 swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
2216 if (swrm->irq < 0) {
2217 dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
2218 __func__, swrm->irq);
Laxminath Kasamfbcaf322018-07-18 00:38:14 +05302219 goto err_irq_fail;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302220 }
2221
2222 ret = request_threaded_irq(swrm->irq, NULL,
Sudheer Papothid19d0c52019-02-23 05:41:39 +05302223 swr_mstr_interrupt_v2,
Ramprasad Katkam83303512018-10-11 17:34:22 +05302224 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302225 "swr_master_irq", swrm);
2226 if (ret) {
2227 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2228 __func__, ret);
2229 goto err_irq_fail;
2230 }
2231
2232 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302233 /* Make inband tx interrupts as wakeup capable for slave irq */
2234 ret = of_property_read_u32(pdev->dev.of_node,
2235 "qcom,swr-mstr-irq-wakeup-capable",
2236 &swrm->swr_irq_wakeup_capable);
2237 if (ret)
2238 dev_dbg(swrm->dev, "%s: swrm irq wakeup capable not defined\n",
2239 __func__);
2240 if (swrm->swr_irq_wakeup_capable)
2241 irq_set_irq_wake(swrm->irq, 1);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302242 ret = swr_register_master(&swrm->master);
2243 if (ret) {
2244 dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
2245 goto err_mstr_fail;
2246 }
2247
2248 /* Add devices registered with board-info as the
2249 * controller will be up now
2250 */
2251 swr_master_add_boarddevices(&swrm->master);
2252 mutex_lock(&swrm->mlock);
2253 swrm_clk_request(swrm, true);
2254 ret = swrm_master_init(swrm);
2255 if (ret < 0) {
2256 dev_err(&pdev->dev,
2257 "%s: Error in master Initialization , err %d\n",
2258 __func__, ret);
2259 mutex_unlock(&swrm->mlock);
2260 goto err_mstr_fail;
2261 }
2262 swrm->version = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
2263
2264 mutex_unlock(&swrm->mlock);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302265 INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302266
2267 if (pdev->dev.of_node)
2268 of_register_swr_devices(&swrm->master);
2269
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302270 /* Register LPASS core hw vote */
2271 lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
2272 if (IS_ERR(lpass_core_hw_vote)) {
2273 ret = PTR_ERR(lpass_core_hw_vote);
2274 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2275 __func__, "lpass_core_hw_vote", ret);
2276 lpass_core_hw_vote = NULL;
2277 ret = 0;
2278 }
2279 swrm->lpass_core_hw_vote = lpass_core_hw_vote;
2280
Sudheer Papothi384addd2019-06-14 02:26:52 +05302281 /* Register LPASS audio core vote */
2282 lpass_core_audio = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
2283 if (IS_ERR(lpass_core_audio)) {
2284 ret = PTR_ERR(lpass_core_audio);
2285 dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
2286 __func__, "lpass_core_audio", ret);
2287 lpass_core_audio = NULL;
2288 ret = 0;
2289 }
2290 swrm->lpass_core_audio = lpass_core_audio;
2291
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302292 dbgswrm = swrm;
2293 debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
2294 if (!IS_ERR(debugfs_swrm_dent)) {
2295 debugfs_peek = debugfs_create_file("swrm_peek",
2296 S_IFREG | 0444, debugfs_swrm_dent,
2297 (void *) "swrm_peek", &swrm_debug_ops);
2298
2299 debugfs_poke = debugfs_create_file("swrm_poke",
2300 S_IFREG | 0444, debugfs_swrm_dent,
2301 (void *) "swrm_poke", &swrm_debug_ops);
2302
2303 debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
2304 S_IFREG | 0444, debugfs_swrm_dent,
2305 (void *) "swrm_reg_dump",
2306 &swrm_debug_ops);
2307 }
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302308
2309 ret = device_init_wakeup(swrm->dev, true);
2310 if (ret) {
2311 dev_err(swrm->dev, "Device wakeup init failed: %d\n", ret);
2312 goto err_irq_wakeup_fail;
2313 }
2314
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302315 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
2316 pm_runtime_use_autosuspend(&pdev->dev);
2317 pm_runtime_set_active(&pdev->dev);
2318 pm_runtime_enable(&pdev->dev);
2319 pm_runtime_mark_last_busy(&pdev->dev);
2320
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302321 INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn);
2322 swrm->event_notifier.notifier_call = swrm_event_notify;
2323 msm_aud_evt_register_client(&swrm->event_notifier);
2324
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302325 return 0;
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302326err_irq_wakeup_fail:
2327 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302328err_mstr_fail:
2329 if (swrm->reg_irq)
2330 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2331 swrm, SWR_IRQ_FREE);
2332 else if (swrm->irq)
2333 free_irq(swrm->irq, swrm);
2334err_irq_fail:
2335 mutex_destroy(&swrm->mlock);
2336 mutex_destroy(&swrm->reslock);
2337 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam1f221262018-08-23 15:01:22 +05302338 mutex_destroy(&swrm->iolock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302339 mutex_destroy(&swrm->clklock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302340 mutex_destroy(&swrm->pm_lock);
2341 pm_qos_remove_request(&swrm->pm_qos_req);
2342
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302343err_pdata_fail:
2344err_memory_fail:
2345 return ret;
2346}
2347
2348static int swrm_remove(struct platform_device *pdev)
2349{
2350 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2351
2352 if (swrm->reg_irq)
2353 swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
2354 swrm, SWR_IRQ_FREE);
2355 else if (swrm->irq)
2356 free_irq(swrm->irq, swrm);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302357 else if (swrm->wake_irq > 0)
2358 free_irq(swrm->wake_irq, swrm);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302359 if (swrm->swr_irq_wakeup_capable)
2360 irq_set_irq_wake(swrm->irq, 0);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302361 cancel_work_sync(&swrm->wakeup_work);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302362 pm_runtime_disable(&pdev->dev);
2363 pm_runtime_set_suspended(&pdev->dev);
2364 swr_unregister_master(&swrm->master);
Ramprasad Katkam68765ab2018-08-30 11:46:32 +05302365 msm_aud_evt_unregister_client(&swrm->event_notifier);
Vatsal Buchadf38c3e2019-03-11 17:10:23 +05302366 device_init_wakeup(swrm->dev, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302367 mutex_destroy(&swrm->mlock);
2368 mutex_destroy(&swrm->reslock);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302369 mutex_destroy(&swrm->iolock);
2370 mutex_destroy(&swrm->clklock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302371 mutex_destroy(&swrm->force_down_lock);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302372 mutex_destroy(&swrm->pm_lock);
2373 pm_qos_remove_request(&swrm->pm_qos_req);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302374 devm_kfree(&pdev->dev, swrm);
2375 return 0;
2376}
2377
2378static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
2379{
2380 u32 val;
2381
2382 dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
2383 swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD);
2384 val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
2385 val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK;
2386 swr_master_write(swrm, SWRM_MCP_CFG_ADDR, val);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302387
2388 return 0;
2389}
2390
2391#ifdef CONFIG_PM
2392static int swrm_runtime_resume(struct device *dev)
2393{
2394 struct platform_device *pdev = to_platform_device(dev);
2395 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2396 int ret = 0;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302397 bool hw_core_err = false;
2398 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302399 struct swr_master *mstr = &swrm->master;
2400 struct swr_device *swr_dev;
2401
2402 dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
2403 __func__, swrm->state);
2404 mutex_lock(&swrm->reslock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302405
Sudheer Papothi384addd2019-06-14 02:26:52 +05302406 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2407 dev_err(dev, "%s:lpass core hw enable failed\n",
2408 __func__);
2409 hw_core_err = true;
2410 }
2411 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2412 dev_err(dev, "%s:lpass audio hw enable failed\n",
2413 __func__);
2414 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002415 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302416
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302417 if ((swrm->state == SWR_MSTR_DOWN) ||
2418 (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) {
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302419 if (swrm->clk_stop_mode0_supp) {
2420 if (swrm->ipc_wakeup)
2421 msm_aud_evt_blocking_notifier_call_chain(
2422 SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
Laxminath Kasamf0128ef2018-08-31 15:15:09 +05302423 }
2424
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302425 if (swrm_clk_request(swrm, true))
2426 goto exit;
2427 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302428 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2429 ret = swr_device_up(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302430 if (ret == -ENODEV) {
2431 dev_dbg(dev,
2432 "%s slave device up not implemented\n",
2433 __func__);
2434 ret = 0;
2435 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302436 dev_err(dev,
2437 "%s: failed to wakeup swr dev %d\n",
2438 __func__, swr_dev->dev_num);
2439 swrm_clk_request(swrm, false);
2440 goto exit;
2441 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302442 }
Ramprasad Katkam48b49b22018-10-01 20:12:46 +05302443 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2444 swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
2445 swrm_master_init(swrm);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302446 swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, 0x0,
2447 SWRS_SCP_INT_STATUS_MASK_1);
Karthikeyan Manif6821902019-05-21 17:31:24 -07002448 if (swrm->state == SWR_MSTR_SSR) {
2449 mutex_unlock(&swrm->reslock);
2450 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
2451 mutex_lock(&swrm->reslock);
2452 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302453 } else {
2454 /*wake up from clock stop*/
2455 swr_master_write(swrm, SWRM_MCP_BUS_CTRL_ADDR, 0x2);
2456 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302457 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302458 swrm->state = SWR_MSTR_UP;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302459 }
2460exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302461 if (!aud_core_err)
2462 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2463 if (!hw_core_err)
2464 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302465 pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
2466 mutex_unlock(&swrm->reslock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302467
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302468 return ret;
2469}
2470
2471static int swrm_runtime_suspend(struct device *dev)
2472{
2473 struct platform_device *pdev = to_platform_device(dev);
2474 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2475 int ret = 0;
Sudheer Papothi384addd2019-06-14 02:26:52 +05302476 bool hw_core_err = false;
2477 bool aud_core_err = false;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302478 struct swr_master *mstr = &swrm->master;
2479 struct swr_device *swr_dev;
2480 int current_state = 0;
2481
2482 dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
2483 __func__, swrm->state);
2484 mutex_lock(&swrm->reslock);
2485 mutex_lock(&swrm->force_down_lock);
2486 current_state = swrm->state;
2487 mutex_unlock(&swrm->force_down_lock);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302488
2489 if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
2490 dev_err(dev, "%s:lpass core hw enable failed\n",
2491 __func__);
2492 hw_core_err = true;
2493 }
2494 if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
2495 dev_err(dev, "%s:lpass audio hw enable failed\n",
2496 __func__);
2497 aud_core_err = true;
Karthikeyan Manif6821902019-05-21 17:31:24 -07002498 }
Sudheer Papothi66d6fd12019-03-27 17:34:48 +05302499
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302500 if ((current_state == SWR_MSTR_UP) ||
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302501 (current_state == SWR_MSTR_SSR)) {
2502
2503 if ((current_state != SWR_MSTR_SSR) &&
2504 swrm_is_port_en(&swrm->master)) {
2505 dev_dbg(dev, "%s ports are enabled\n", __func__);
2506 ret = -EBUSY;
2507 goto exit;
2508 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302509 if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
Ramprasad Katkamb4c7c682018-12-19 18:58:36 +05302510 enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302511 swrm_clk_pause(swrm);
2512 swr_master_write(swrm, SWRM_COMP_CFG_ADDR, 0x00);
2513 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2514 ret = swr_device_down(swr_dev);
Sudheer Papothi79c90752019-04-23 06:09:52 +05302515 if (ret == -ENODEV) {
2516 dev_dbg_ratelimited(dev,
2517 "%s slave device down not implemented\n",
2518 __func__);
2519 ret = 0;
2520 } else if (ret) {
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302521 dev_err(dev,
2522 "%s: failed to shutdown swr dev %d\n",
2523 __func__, swr_dev->dev_num);
2524 goto exit;
2525 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302526 }
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302527 } else {
Sudheer Papothi384addd2019-06-14 02:26:52 +05302528 mutex_unlock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302529 /* clock stop sequence */
2530 swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
2531 SWRS_SCP_CONTROL);
Sudheer Papothi384addd2019-06-14 02:26:52 +05302532 mutex_lock(&swrm->reslock);
Ramprasad Katkam14f47cc2018-07-25 17:20:18 +05302533 usleep_range(100, 105);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302534 }
2535 swrm_clk_request(swrm, false);
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302536
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302537 if (swrm->clk_stop_mode0_supp) {
2538 if (swrm->wake_irq > 0) {
2539 enable_irq(swrm->wake_irq);
2540 } else if (swrm->ipc_wakeup) {
2541 msm_aud_evt_blocking_notifier_call_chain(
2542 SWR_WAKE_IRQ_REGISTER, (void *)swrm);
2543 swrm->ipc_wakeup_triggered = false;
2544 }
Ramprasad Katkam6a3050d2018-10-10 02:08:00 +05302545 }
2546
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302547 }
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302548 /* Retain SSR state until resume */
2549 if (current_state != SWR_MSTR_SSR)
2550 swrm->state = SWR_MSTR_DOWN;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302551exit:
Sudheer Papothi384addd2019-06-14 02:26:52 +05302552 if (!aud_core_err)
2553 swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
2554 if (!hw_core_err)
2555 swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302556 mutex_unlock(&swrm->reslock);
2557 return ret;
2558}
2559#endif /* CONFIG_PM */
2560
2561static int swrm_device_down(struct device *dev)
2562{
2563 struct platform_device *pdev = to_platform_device(dev);
2564 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2565 int ret = 0;
2566
2567 dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
2568
2569 mutex_lock(&swrm->force_down_lock);
2570 swrm->state = SWR_MSTR_SSR;
2571 mutex_unlock(&swrm->force_down_lock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302572 if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
2573 ret = swrm_runtime_suspend(dev);
2574 if (!ret) {
2575 pm_runtime_disable(dev);
2576 pm_runtime_set_suspended(dev);
2577 pm_runtime_enable(dev);
2578 }
2579 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302580
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302581 return 0;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302582}
2583
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302584int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm)
2585{
2586 int ret = 0;
Laxminath Kasama60239e2019-01-10 14:43:03 +05302587 int irq, dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302588
2589 if (!swrm->ipc_wakeup) {
Laxminath Kasama60239e2019-01-10 14:43:03 +05302590 irq = of_get_named_gpio(swrm->dev->of_node,
2591 "qcom,swr-wakeup-irq", 0);
2592 if (gpio_is_valid(irq)) {
2593 swrm->wake_irq = gpio_to_irq(irq);
2594 if (swrm->wake_irq < 0) {
2595 dev_err(swrm->dev,
2596 "Unable to configure irq\n");
2597 return swrm->wake_irq;
2598 }
2599 } else {
2600 dir_apps_irq = platform_get_irq_byname(swrm->pdev,
2601 "swr_wake_irq");
2602 if (dir_apps_irq < 0) {
2603 dev_err(swrm->dev,
2604 "TLMM connect gpio not found\n");
2605 return -EINVAL;
2606 }
2607 swrm->wake_irq = dir_apps_irq;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302608 }
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302609 ret = request_threaded_irq(swrm->wake_irq, NULL,
2610 swrm_wakeup_interrupt,
2611 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
2612 "swr_wake_irq", swrm);
2613 if (ret) {
2614 dev_err(swrm->dev, "%s: Failed to request irq %d\n",
2615 __func__, ret);
2616 return -EINVAL;
2617 }
Aditya Bavanari3517b112018-12-03 13:26:59 +05302618 irq_set_irq_wake(swrm->wake_irq, 1);
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302619 }
2620 return ret;
2621}
2622
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302623static int swrm_alloc_port_mem(struct device *dev, struct swr_mstr_ctrl *swrm,
2624 u32 uc, u32 size)
2625{
2626 if (!swrm->port_param) {
2627 swrm->port_param = devm_kzalloc(dev,
2628 sizeof(swrm->port_param) * SWR_UC_MAX,
2629 GFP_KERNEL);
2630 if (!swrm->port_param)
2631 return -ENOMEM;
2632 }
2633 if (!swrm->port_param[uc]) {
2634 swrm->port_param[uc] = devm_kcalloc(dev, size,
2635 sizeof(struct port_params),
2636 GFP_KERNEL);
2637 if (!swrm->port_param[uc])
2638 return -ENOMEM;
2639 } else {
2640 dev_err_ratelimited(swrm->dev, "%s: called more than once\n",
2641 __func__);
2642 }
2643
2644 return 0;
2645}
2646
2647static int swrm_copy_port_config(struct swr_mstr_ctrl *swrm,
2648 struct swrm_port_config *port_cfg,
2649 u32 size)
2650{
2651 int idx;
2652 struct port_params *params;
2653 int uc = port_cfg->uc;
2654 int ret = 0;
2655
2656 for (idx = 0; idx < size; idx++) {
2657 params = &((struct port_params *)port_cfg->params)[idx];
2658 if (!params) {
2659 dev_err(swrm->dev, "%s: Invalid params\n", __func__);
2660 ret = -EINVAL;
2661 break;
2662 }
2663 memcpy(&swrm->port_param[uc][idx], params,
2664 sizeof(struct port_params));
2665 }
2666
2667 return ret;
2668}
2669
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302670/**
2671 * swrm_wcd_notify - parent device can notify to soundwire master through
2672 * this function
2673 * @pdev: pointer to platform device structure
2674 * @id: command id from parent to the soundwire master
2675 * @data: data from parent device to soundwire master
2676 */
2677int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
2678{
2679 struct swr_mstr_ctrl *swrm;
2680 int ret = 0;
2681 struct swr_master *mstr;
2682 struct swr_device *swr_dev;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302683 struct swrm_port_config *port_cfg;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302684
2685 if (!pdev) {
2686 pr_err("%s: pdev is NULL\n", __func__);
2687 return -EINVAL;
2688 }
2689 swrm = platform_get_drvdata(pdev);
2690 if (!swrm) {
2691 dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__);
2692 return -EINVAL;
2693 }
2694 mstr = &swrm->master;
2695
2696 switch (id) {
Laxminath Kasamb0f27cd2018-09-06 12:17:11 +05302697 case SWR_CLK_FREQ:
2698 if (!data) {
2699 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
2700 ret = -EINVAL;
2701 } else {
2702 mutex_lock(&swrm->mlock);
2703 swrm->mclk_freq = *(int *)data;
2704 mutex_unlock(&swrm->mlock);
2705 }
2706 break;
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302707 case SWR_DEVICE_SSR_DOWN:
2708 mutex_lock(&swrm->devlock);
2709 swrm->dev_up = false;
2710 mutex_unlock(&swrm->devlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302711 mutex_lock(&swrm->reslock);
2712 swrm->state = SWR_MSTR_SSR;
2713 mutex_unlock(&swrm->reslock);
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302714 break;
2715 case SWR_DEVICE_SSR_UP:
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302716 /* wait for clk voting to be zero */
Ramprasad Katkam7f6462e2018-11-06 11:51:22 +05302717 reinit_completion(&swrm->clk_off_complete);
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302718 if (swrm->clk_ref_count &&
2719 !wait_for_completion_timeout(&swrm->clk_off_complete,
Ramprasad Katkamc87efeb2018-12-12 19:26:19 +05302720 msecs_to_jiffies(500)))
Ramprasad Katkam6bce2e72018-10-10 19:20:13 +05302721 dev_err(swrm->dev, "%s: clock voting not zero\n",
2722 __func__);
2723
Laxminath Kasam1df09a82018-09-20 18:57:49 +05302724 mutex_lock(&swrm->devlock);
2725 swrm->dev_up = true;
2726 mutex_unlock(&swrm->devlock);
2727 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302728 case SWR_DEVICE_DOWN:
2729 dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
2730 mutex_lock(&swrm->mlock);
Ramprasad Katkam2a799b42018-10-04 20:23:28 +05302731 if (swrm->state == SWR_MSTR_DOWN)
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302732 dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
2733 __func__, swrm->state);
2734 else
2735 swrm_device_down(&pdev->dev);
2736 mutex_unlock(&swrm->mlock);
2737 break;
2738 case SWR_DEVICE_UP:
2739 dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
Ramprasad Katkam0fed92f2018-11-08 14:22:22 +05302740 mutex_lock(&swrm->devlock);
2741 if (!swrm->dev_up) {
2742 dev_dbg(swrm->dev, "SSR not complete yet\n");
2743 mutex_unlock(&swrm->devlock);
2744 return -EBUSY;
2745 }
2746 mutex_unlock(&swrm->devlock);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302747 mutex_lock(&swrm->mlock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302748 pm_runtime_mark_last_busy(&pdev->dev);
2749 pm_runtime_get_sync(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302750 mutex_lock(&swrm->reslock);
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302751 list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
2752 ret = swr_reset_device(swr_dev);
2753 if (ret) {
2754 dev_err(swrm->dev,
2755 "%s: failed to reset swr device %d\n",
2756 __func__, swr_dev->dev_num);
2757 swrm_clk_request(swrm, false);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302758 }
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302759 }
Ramprasad Katkam86c45e02018-10-16 19:31:51 +05302760 pm_runtime_mark_last_busy(&pdev->dev);
2761 pm_runtime_put_autosuspend(&pdev->dev);
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302762 mutex_unlock(&swrm->reslock);
2763 mutex_unlock(&swrm->mlock);
2764 break;
2765 case SWR_SET_NUM_RX_CH:
2766 if (!data) {
2767 dev_err(swrm->dev, "%s: data is NULL\n", __func__);
2768 ret = -EINVAL;
2769 } else {
2770 mutex_lock(&swrm->mlock);
2771 swrm->num_rx_chs = *(int *)data;
2772 if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
2773 list_for_each_entry(swr_dev, &mstr->devices,
2774 dev_list) {
2775 ret = swr_set_device_group(swr_dev,
2776 SWR_BROADCAST);
2777 if (ret)
2778 dev_err(swrm->dev,
2779 "%s: set num ch failed\n",
2780 __func__);
2781 }
2782 } else {
2783 list_for_each_entry(swr_dev, &mstr->devices,
2784 dev_list) {
2785 ret = swr_set_device_group(swr_dev,
2786 SWR_GROUP_NONE);
2787 if (ret)
2788 dev_err(swrm->dev,
2789 "%s: set num ch failed\n",
2790 __func__);
2791 }
2792 }
2793 mutex_unlock(&swrm->mlock);
2794 }
2795 break;
Aditya Bavanaric034fad2018-11-12 22:55:11 +05302796 case SWR_REGISTER_WAKE_IRQ:
2797 if (!data) {
2798 dev_err(swrm->dev, "%s: reg wake irq data is NULL\n",
2799 __func__);
2800 ret = -EINVAL;
2801 } else {
2802 mutex_lock(&swrm->mlock);
2803 swrm->ipc_wakeup = *(u32 *)data;
2804 ret = swrm_register_wake_irq(swrm);
2805 if (ret)
2806 dev_err(swrm->dev, "%s: register wake_irq failed\n",
2807 __func__);
2808 mutex_unlock(&swrm->mlock);
2809 }
2810 break;
Sudheer Papothi3d1596e2018-10-27 06:19:18 +05302811 case SWR_SET_PORT_MAP:
2812 if (!data) {
2813 dev_err(swrm->dev, "%s: data is NULL for id=%d\n",
2814 __func__, id);
2815 ret = -EINVAL;
2816 } else {
2817 mutex_lock(&swrm->mlock);
2818 port_cfg = (struct swrm_port_config *)data;
2819 if (!port_cfg->size) {
2820 ret = -EINVAL;
2821 goto done;
2822 }
2823 ret = swrm_alloc_port_mem(&pdev->dev, swrm,
2824 port_cfg->uc, port_cfg->size);
2825 if (!ret)
2826 swrm_copy_port_config(swrm, port_cfg,
2827 port_cfg->size);
2828done:
2829 mutex_unlock(&swrm->mlock);
2830 }
2831 break;
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302832 default:
2833 dev_err(swrm->dev, "%s: swr master unknown id %d\n",
2834 __func__, id);
2835 break;
2836 }
2837 return ret;
2838}
2839EXPORT_SYMBOL(swrm_wcd_notify);
2840
Ramprasad Katkam57349872018-11-11 18:34:57 +05302841/*
2842 * swrm_pm_cmpxchg:
2843 * Check old state and exchange with pm new state
2844 * if old state matches with current state
2845 *
2846 * @swrm: pointer to wcd core resource
2847 * @o: pm old state
2848 * @n: pm new state
2849 *
2850 * Returns old state
2851 */
2852static enum swrm_pm_state swrm_pm_cmpxchg(
2853 struct swr_mstr_ctrl *swrm,
2854 enum swrm_pm_state o,
2855 enum swrm_pm_state n)
2856{
2857 enum swrm_pm_state old;
2858
2859 if (!swrm)
2860 return o;
2861
2862 mutex_lock(&swrm->pm_lock);
2863 old = swrm->pm_state;
2864 if (old == o)
2865 swrm->pm_state = n;
2866 mutex_unlock(&swrm->pm_lock);
2867
2868 return old;
2869}
2870
2871static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm)
2872{
2873 enum swrm_pm_state os;
2874
2875 /*
2876 * swrm_{lock/unlock}_sleep will be called by swr irq handler
2877 * and slave wake up requests..
2878 *
2879 * If system didn't resume, we can simply return false so
2880 * IRQ handler can return without handling IRQ.
2881 */
2882 mutex_lock(&swrm->pm_lock);
2883 if (swrm->wlock_holders++ == 0) {
2884 dev_dbg(swrm->dev, "%s: holding wake lock\n", __func__);
2885 pm_qos_update_request(&swrm->pm_qos_req,
2886 msm_cpuidle_get_deep_idle_latency());
2887 pm_stay_awake(swrm->dev);
2888 }
2889 mutex_unlock(&swrm->pm_lock);
2890
2891 if (!wait_event_timeout(swrm->pm_wq,
2892 ((os = swrm_pm_cmpxchg(swrm,
2893 SWRM_PM_SLEEPABLE,
2894 SWRM_PM_AWAKE)) ==
2895 SWRM_PM_SLEEPABLE ||
2896 (os == SWRM_PM_AWAKE)),
2897 msecs_to_jiffies(
2898 SWRM_SYSTEM_RESUME_TIMEOUT_MS))) {
2899 dev_err(swrm->dev, "%s: system didn't resume within %dms, s %d, w %d\n",
2900 __func__, SWRM_SYSTEM_RESUME_TIMEOUT_MS, swrm->pm_state,
2901 swrm->wlock_holders);
2902 swrm_unlock_sleep(swrm);
2903 return false;
2904 }
2905 wake_up_all(&swrm->pm_wq);
2906 return true;
2907}
2908
2909static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm)
2910{
2911 mutex_lock(&swrm->pm_lock);
2912 if (--swrm->wlock_holders == 0) {
2913 dev_dbg(swrm->dev, "%s: releasing wake lock pm_state %d -> %d\n",
2914 __func__, swrm->pm_state, SWRM_PM_SLEEPABLE);
2915 /*
2916 * if swrm_lock_sleep failed, pm_state would be still
2917 * swrm_PM_ASLEEP, don't overwrite
2918 */
2919 if (likely(swrm->pm_state == SWRM_PM_AWAKE))
2920 swrm->pm_state = SWRM_PM_SLEEPABLE;
2921 pm_qos_update_request(&swrm->pm_qos_req,
2922 PM_QOS_DEFAULT_VALUE);
2923 pm_relax(swrm->dev);
2924 }
2925 mutex_unlock(&swrm->pm_lock);
2926 wake_up_all(&swrm->pm_wq);
2927}
2928
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302929#ifdef CONFIG_PM_SLEEP
2930static int swrm_suspend(struct device *dev)
2931{
2932 int ret = -EBUSY;
2933 struct platform_device *pdev = to_platform_device(dev);
2934 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
2935
2936 dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
Ramprasad Katkam57349872018-11-11 18:34:57 +05302937
2938 mutex_lock(&swrm->pm_lock);
2939
2940 if (swrm->pm_state == SWRM_PM_SLEEPABLE) {
2941 dev_dbg(swrm->dev, "%s: suspending system, state %d, wlock %d\n",
2942 __func__, swrm->pm_state,
2943 swrm->wlock_holders);
2944 swrm->pm_state = SWRM_PM_ASLEEP;
2945 } else if (swrm->pm_state == SWRM_PM_AWAKE) {
2946 /*
2947 * unlock to wait for pm_state == SWRM_PM_SLEEPABLE
2948 * then set to SWRM_PM_ASLEEP
2949 */
2950 dev_dbg(swrm->dev, "%s: waiting to suspend system, state %d, wlock %d\n",
2951 __func__, swrm->pm_state,
2952 swrm->wlock_holders);
2953 mutex_unlock(&swrm->pm_lock);
2954 if (!(wait_event_timeout(swrm->pm_wq, swrm_pm_cmpxchg(
2955 swrm, SWRM_PM_SLEEPABLE,
2956 SWRM_PM_ASLEEP) ==
2957 SWRM_PM_SLEEPABLE,
2958 msecs_to_jiffies(
2959 SWRM_SYS_SUSPEND_WAIT)))) {
2960 dev_dbg(swrm->dev, "%s: suspend failed state %d, wlock %d\n",
2961 __func__, swrm->pm_state,
2962 swrm->wlock_holders);
2963 return -EBUSY;
2964 } else {
2965 dev_dbg(swrm->dev,
2966 "%s: done, state %d, wlock %d\n",
2967 __func__, swrm->pm_state,
2968 swrm->wlock_holders);
2969 }
2970 mutex_lock(&swrm->pm_lock);
2971 } else if (swrm->pm_state == SWRM_PM_ASLEEP) {
2972 dev_dbg(swrm->dev, "%s: system is already suspended, state %d, wlock %d\n",
2973 __func__, swrm->pm_state,
2974 swrm->wlock_holders);
2975 }
2976
2977 mutex_unlock(&swrm->pm_lock);
2978
2979 if ((!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev))) {
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05302980 ret = swrm_runtime_suspend(dev);
2981 if (!ret) {
2982 /*
2983 * Synchronize runtime-pm and system-pm states:
2984 * At this point, we are already suspended. If
2985 * runtime-pm still thinks its active, then
2986 * make sure its status is in sync with HW
2987 * status. The three below calls let the
2988 * runtime-pm know that we are suspended
2989 * already without re-invoking the suspend
2990 * callback
2991 */
2992 pm_runtime_disable(dev);
2993 pm_runtime_set_suspended(dev);
2994 pm_runtime_enable(dev);
2995 }
2996 }
2997 if (ret == -EBUSY) {
2998 /*
2999 * There is a possibility that some audio stream is active
3000 * during suspend. We dont want to return suspend failure in
3001 * that case so that display and relevant components can still
3002 * go to suspend.
3003 * If there is some other error, then it should be passed-on
3004 * to system level suspend
3005 */
3006 ret = 0;
3007 }
3008 return ret;
3009}
3010
3011static int swrm_resume(struct device *dev)
3012{
3013 int ret = 0;
3014 struct platform_device *pdev = to_platform_device(dev);
3015 struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
3016
3017 dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
3018 if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
3019 ret = swrm_runtime_resume(dev);
3020 if (!ret) {
3021 pm_runtime_mark_last_busy(dev);
3022 pm_request_autosuspend(dev);
3023 }
3024 }
Ramprasad Katkam57349872018-11-11 18:34:57 +05303025 mutex_lock(&swrm->pm_lock);
3026 if (swrm->pm_state == SWRM_PM_ASLEEP) {
3027 dev_dbg(swrm->dev,
3028 "%s: resuming system, state %d, wlock %d\n",
3029 __func__, swrm->pm_state,
3030 swrm->wlock_holders);
3031 swrm->pm_state = SWRM_PM_SLEEPABLE;
3032 } else {
3033 dev_dbg(swrm->dev, "%s: system is already awake, state %d wlock %d\n",
3034 __func__, swrm->pm_state,
3035 swrm->wlock_holders);
3036 }
3037 mutex_unlock(&swrm->pm_lock);
3038 wake_up_all(&swrm->pm_wq);
3039
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303040 return ret;
3041}
3042#endif /* CONFIG_PM_SLEEP */
3043
3044static const struct dev_pm_ops swrm_dev_pm_ops = {
3045 SET_SYSTEM_SLEEP_PM_OPS(
3046 swrm_suspend,
3047 swrm_resume
3048 )
3049 SET_RUNTIME_PM_OPS(
3050 swrm_runtime_suspend,
3051 swrm_runtime_resume,
3052 NULL
3053 )
3054};
3055
3056static const struct of_device_id swrm_dt_match[] = {
3057 {
3058 .compatible = "qcom,swr-mstr",
3059 },
3060 {}
3061};
3062
3063static struct platform_driver swr_mstr_driver = {
3064 .probe = swrm_probe,
3065 .remove = swrm_remove,
3066 .driver = {
3067 .name = SWR_WCD_NAME,
3068 .owner = THIS_MODULE,
3069 .pm = &swrm_dev_pm_ops,
3070 .of_match_table = swrm_dt_match,
Xiaojun Sang53cd13a2018-06-29 15:14:37 +08003071 .suppress_bind_attrs = true,
Ramprasad Katkam9f040f32018-05-16 10:19:25 +05303072 },
3073};
3074
3075static int __init swrm_init(void)
3076{
3077 return platform_driver_register(&swr_mstr_driver);
3078}
3079module_init(swrm_init);
3080
3081static void __exit swrm_exit(void)
3082{
3083 platform_driver_unregister(&swr_mstr_driver);
3084}
3085module_exit(swrm_exit);
3086
3087MODULE_LICENSE("GPL v2");
3088MODULE_DESCRIPTION("SoundWire Master Controller");
3089MODULE_ALIAS("platform:swr-mstr");