Meng Wang | 43bbb87 | 2018-12-10 12:32:05 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Meng Wang | 61af684 | 2018-09-10 17:47:55 +0800 | [diff] [blame] | 2 | /* |
Aditya Bavanari | 3517b11 | 2018-12-03 13:26:59 +0530 | [diff] [blame] | 3 | * Copyright (c) 2015-2019, The Linux Foundation. All rights reserved. |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 4 | */ |
| 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 Katkam | cab8d72 | 2018-09-28 15:54:06 +0530 | [diff] [blame] | 15 | #include <linux/bitops.h> |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 16 | #include <linux/clk.h> |
Laxminath Kasam | a60239e | 2019-01-10 14:43:03 +0530 | [diff] [blame] | 17 | #include <linux/gpio.h> |
| 18 | #include <linux/of_gpio.h> |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 19 | #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 Papothi | 3d1596e | 2018-10-27 06:19:18 +0530 | [diff] [blame] | 24 | #include <soc/swr-common.h> |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 25 | #include <linux/regmap.h> |
Ramprasad Katkam | 68765ab | 2018-08-30 11:46:32 +0530 | [diff] [blame] | 26 | #include <dsp/msm-audio-event-notify.h> |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 27 | #include "swrm_registers.h" |
| 28 | #include "swr-mstr-ctrl.h" |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 29 | |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 30 | #define SWRM_SYSTEM_RESUME_TIMEOUT_MS 700 |
| 31 | #define SWRM_SYS_SUSPEND_WAIT 1 |
Sudheer Papothi | 3d1596e | 2018-10-27 06:19:18 +0530 | [diff] [blame] | 32 | |
Sudheer Papothi | 4c322b1 | 2018-10-31 06:34:01 +0530 | [diff] [blame] | 33 | #define SWRM_DSD_PARAMS_PORT 4 |
| 34 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 35 | #define SWR_BROADCAST_CMD_ID 0x0F |
Sudheer Papothi | 3590b31 | 2019-06-04 23:51:30 +0530 | [diff] [blame] | 36 | #define SWR_AUTO_SUSPEND_DELAY 1 /* delay in sec */ |
Sudheer Papothi | 7c067e8 | 2018-11-15 06:53:35 +0530 | [diff] [blame] | 37 | #define SWR_DEV_ID_MASK 0xFFFFFFFFFFFF |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 38 | #define SWR_REG_VAL_PACK(data, dev, id, reg) \ |
| 39 | ((reg) | ((id) << 16) | ((dev) << 20) | ((data) << 24)) |
| 40 | |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 41 | #define SWR_INVALID_PARAM 0xFF |
Laxminath Kasam | 990c70b | 2018-11-09 23:15:09 +0530 | [diff] [blame] | 42 | #define SWR_HSTOP_MAX_VAL 0xF |
| 43 | #define SWR_HSTART_MIN_VAL 0x0 |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 44 | |
Ramprasad Katkam | 8330351 | 2018-10-11 17:34:22 +0530 | [diff] [blame] | 45 | #define SWRM_INTERRUPT_STATUS_MASK 0x1FDFD |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 46 | /* pm runtime auto suspend timer in msecs */ |
| 47 | static int auto_suspend_timer = SWR_AUTO_SUSPEND_DELAY * 1000; |
| 48 | module_param(auto_suspend_timer, int, 0664); |
| 49 | MODULE_PARM_DESC(auto_suspend_timer, "timer for auto suspend"); |
| 50 | |
| 51 | enum { |
| 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 | |
| 58 | enum { |
| 59 | MASTER_ID_WSA = 1, |
| 60 | MASTER_ID_RX, |
| 61 | MASTER_ID_TX |
| 62 | }; |
Ramprasad Katkam | cab8d72 | 2018-09-28 15:54:06 +0530 | [diff] [blame] | 63 | |
| 64 | enum { |
| 65 | ENABLE_PENDING, |
| 66 | DISABLE_PENDING |
| 67 | }; |
Sudheer Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 68 | |
| 69 | enum { |
| 70 | LPASS_HW_CORE, |
| 71 | LPASS_AUDIO_CORE, |
| 72 | }; |
| 73 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 74 | #define TRUE 1 |
| 75 | #define FALSE 0 |
| 76 | |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 77 | #define SWRM_MAX_PORT_REG 120 |
Ramprasad Katkam | 8330351 | 2018-10-11 17:34:22 +0530 | [diff] [blame] | 78 | #define SWRM_MAX_INIT_REG 11 |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 79 | |
| 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 Kasam | fbcaf32 | 2018-07-18 00:38:14 +0530 | [diff] [blame] | 87 | #define MAX_FIFO_RD_FAIL_RETRY 3 |
| 88 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 89 | static struct swr_mstr_ctrl *dbgswrm; |
| 90 | static struct dentry *debugfs_swrm_dent; |
| 91 | static struct dentry *debugfs_peek; |
| 92 | static struct dentry *debugfs_poke; |
| 93 | static struct dentry *debugfs_reg_dump; |
| 94 | static unsigned int read_data; |
| 95 | |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 96 | static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm); |
| 97 | static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 98 | |
| 99 | static bool swrm_is_msm_variant(int val) |
| 100 | { |
| 101 | return (val == SWRM_VERSION_1_3); |
| 102 | } |
| 103 | |
| 104 | static int swrm_debug_open(struct inode *inode, struct file *file) |
| 105 | { |
| 106 | file->private_data = inode->i_private; |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static 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, ¶m1[cnt]) != 0) |
| 124 | return -EINVAL; |
| 125 | |
| 126 | token = strsep(&buf, " "); |
| 127 | } else |
| 128 | return -EINVAL; |
| 129 | } |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static 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 | |
| 158 | copy_err: |
| 159 | return total; |
| 160 | } |
| 161 | |
| 162 | static 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 | |
| 189 | static 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 | |
| 235 | static 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 Papothi | 0016db1 | 2019-06-11 04:42:38 +0530 | [diff] [blame] | 241 | static 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 Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 251 | static 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 286 | static int swrm_clk_request(struct swr_mstr_ctrl *swrm, bool enable) |
| 287 | { |
Ramprasad Katkam | 6bce2e7 | 2018-10-10 19:20:13 +0530 | [diff] [blame] | 288 | int ret = 0; |
| 289 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 290 | if (!swrm->clk || !swrm->handle) |
| 291 | return -EINVAL; |
| 292 | |
Ramprasad Katkam | 6bce2e7 | 2018-10-10 19:20:13 +0530 | [diff] [blame] | 293 | mutex_lock(&swrm->clklock); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 294 | if (enable) { |
Aditya Bavanari | f4a471d | 2019-02-19 17:57:12 +0530 | [diff] [blame] | 295 | if (!swrm->dev_up) { |
| 296 | ret = -ENODEV; |
Ramprasad Katkam | 6bce2e7 | 2018-10-10 19:20:13 +0530 | [diff] [blame] | 297 | goto exit; |
Aditya Bavanari | f4a471d | 2019-02-19 17:57:12 +0530 | [diff] [blame] | 298 | } |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 299 | swrm->clk_ref_count++; |
| 300 | if (swrm->clk_ref_count == 1) { |
Ramprasad Katkam | 6bce2e7 | 2018-10-10 19:20:13 +0530 | [diff] [blame] | 301 | ret = swrm->clk(swrm->handle, true); |
| 302 | if (ret) { |
Ramprasad Katkam | 14efed6 | 2019-03-07 13:16:50 +0530 | [diff] [blame] | 303 | dev_err_ratelimited(swrm->dev, |
Ramprasad Katkam | 6bce2e7 | 2018-10-10 19:20:13 +0530 | [diff] [blame] | 304 | "%s: clock enable req failed", |
| 305 | __func__); |
| 306 | --swrm->clk_ref_count; |
| 307 | } |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 308 | } |
| 309 | } else if (--swrm->clk_ref_count == 0) { |
| 310 | swrm->clk(swrm->handle, false); |
Ramprasad Katkam | 6bce2e7 | 2018-10-10 19:20:13 +0530 | [diff] [blame] | 311 | complete(&swrm->clk_off_complete); |
| 312 | } |
| 313 | if (swrm->clk_ref_count < 0) { |
Meng Wang | 8c60bb5 | 2019-06-19 15:49:06 +0800 | [diff] [blame] | 314 | dev_err(swrm->dev, "%s: swrm clk count mismatch\n", __func__); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 315 | swrm->clk_ref_count = 0; |
| 316 | } |
Ramprasad Katkam | 6bce2e7 | 2018-10-10 19:20:13 +0530 | [diff] [blame] | 317 | |
| 318 | exit: |
| 319 | mutex_unlock(&swrm->clklock); |
| 320 | return ret; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | static int swrm_ahb_write(struct swr_mstr_ctrl *swrm, |
| 324 | u16 reg, u32 *value) |
| 325 | { |
Laxminath Kasam | fbcaf32 | 2018-07-18 00:38:14 +0530 | [diff] [blame] | 326 | u32 temp = (u32)(*value); |
Laxminath Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 327 | int ret = 0; |
| 328 | |
| 329 | mutex_lock(&swrm->devlock); |
| 330 | if (!swrm->dev_up) |
| 331 | goto err; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 332 | |
| 333 | ret = swrm_clk_request(swrm, TRUE); |
Laxminath Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 334 | if (ret) { |
| 335 | dev_err_ratelimited(swrm->dev, "%s: clock request failed\n", |
| 336 | __func__); |
| 337 | goto err; |
| 338 | } |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 339 | iowrite32(temp, swrm->swrm_dig_base + reg); |
Laxminath Kasam | fbcaf32 | 2018-07-18 00:38:14 +0530 | [diff] [blame] | 340 | swrm_clk_request(swrm, FALSE); |
Laxminath Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 341 | err: |
| 342 | mutex_unlock(&swrm->devlock); |
| 343 | return ret; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | static int swrm_ahb_read(struct swr_mstr_ctrl *swrm, |
| 347 | u16 reg, u32 *value) |
| 348 | { |
Laxminath Kasam | fbcaf32 | 2018-07-18 00:38:14 +0530 | [diff] [blame] | 349 | u32 temp = 0; |
Laxminath Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 350 | int ret = 0; |
| 351 | |
| 352 | mutex_lock(&swrm->devlock); |
| 353 | if (!swrm->dev_up) |
| 354 | goto err; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 355 | |
| 356 | ret = swrm_clk_request(swrm, TRUE); |
Laxminath Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 357 | if (ret) { |
| 358 | dev_err_ratelimited(swrm->dev, "%s: clock request failed\n", |
| 359 | __func__); |
| 360 | goto err; |
| 361 | } |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 362 | temp = ioread32(swrm->swrm_dig_base + reg); |
Laxminath Kasam | fbcaf32 | 2018-07-18 00:38:14 +0530 | [diff] [blame] | 363 | *value = temp; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 364 | swrm_clk_request(swrm, FALSE); |
Laxminath Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 365 | err: |
| 366 | mutex_unlock(&swrm->devlock); |
| 367 | return ret; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | static 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 | |
| 381 | static 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 | |
| 389 | static 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 Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 397 | mutex_lock(&swrm->iolock); |
Laxminath Kasam | fbcaf32 | 2018-07-18 00:38:14 +0530 | [diff] [blame] | 398 | for (i = 0; i < length; i++) { |
| 399 | /* wait for FIFO WR command to complete to avoid overflow */ |
| 400 | usleep_range(100, 105); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 401 | swr_master_write(swrm, reg_addr[i], val[i]); |
Laxminath Kasam | fbcaf32 | 2018-07-18 00:38:14 +0530 | [diff] [blame] | 402 | } |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 403 | mutex_unlock(&swrm->iolock); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 404 | } |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | static bool swrm_is_port_en(struct swr_master *mstr) |
| 409 | { |
| 410 | return !!(mstr->num_port); |
| 411 | } |
| 412 | |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 413 | static 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 434 | static int swrm_get_port_config(struct swr_mstr_ctrl *swrm) |
| 435 | { |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 436 | struct port_params *params; |
Sudheer Papothi | 4c322b1 | 2018-10-31 06:34:01 +0530 | [diff] [blame] | 437 | u32 usecase = 0; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 438 | |
Sudheer Papothi | 4c322b1 | 2018-10-31 06:34:01 +0530 | [diff] [blame] | 439 | /* 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 Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 443 | |
Sudheer Papothi | 4c322b1 | 2018-10-31 06:34:01 +0530 | [diff] [blame] | 444 | params = swrm->port_param[usecase]; |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 445 | copy_port_tables(swrm, params); |
Sudheer Papothi | 4c322b1 | 2018-10-31 06:34:01 +0530 | [diff] [blame] | 446 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | static 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 | } |
| 463 | found: |
| 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 | |
| 477 | static 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 | |
| 495 | static 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 Kasam | fbcaf32 | 2018-07-18 00:38:14 +0530 | [diff] [blame] | 500 | u32 retry_attempt = 0; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 501 | |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 502 | mutex_lock(&swrm->iolock); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 503 | val = swrm_get_packed_reg_val(&swrm->rcmd_id, len, dev_addr, reg_addr); |
Ramprasad Katkam | 1e90620 | 2019-01-30 14:16:34 +0530 | [diff] [blame] | 504 | 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 Kasam | fbcaf32 | 2018-07-18 00:38:14 +0530 | [diff] [blame] | 514 | retry_read: |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 515 | *cmd_data = swr_master_read(swrm, SWRM_CMD_FIFO_RD_FIFO_ADDR); |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 516 | 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 Kasam | fbcaf32 | 2018-07-18 00:38:14 +0530 | [diff] [blame] | 519 | 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 Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 526 | 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 Kasam | fbcaf32 | 2018-07-18 00:38:14 +0530 | [diff] [blame] | 531 | dev_err_ratelimited(swrm->dev, |
| 532 | "%s: failed to read fifo\n", __func__); |
| 533 | } |
| 534 | } |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 535 | mutex_unlock(&swrm->iolock); |
| 536 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 537 | return 0; |
| 538 | } |
| 539 | |
| 540 | static 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 Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 546 | mutex_lock(&swrm->iolock); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 547 | 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 Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 553 | 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 Katkam | b4c7c68 | 2018-12-19 18:58:36 +0530 | [diff] [blame] | 556 | swr_master_write(swrm, SWRM_CMD_FIFO_WR_CMD, val); |
Ramprasad Katkam | 1e90620 | 2019-01-30 14:16:34 +0530 | [diff] [blame] | 557 | /* |
| 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 563 | 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 Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 574 | mutex_unlock(&swrm->iolock); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 575 | return ret; |
| 576 | } |
| 577 | |
| 578 | static 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 Katkam | 0db4801 | 2018-11-09 11:01:23 +0530 | [diff] [blame] | 590 | if (!dev_num) { |
| 591 | dev_err(&master->dev, "%s: invalid slave dev num\n", __func__); |
| 592 | return -EINVAL; |
| 593 | } |
Laxminath Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 594 | 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 Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 601 | pm_runtime_get_sync(swrm->dev); |
Ramprasad Katkam | 0db4801 | 2018-11-09 11:01:23 +0530 | [diff] [blame] | 602 | ret = swrm_cmd_fifo_rd_cmd(swrm, &val, dev_num, 0, reg_addr, len); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 603 | |
| 604 | if (!ret) |
| 605 | *reg_val = (u8)val; |
| 606 | |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 607 | pm_runtime_put_autosuspend(swrm->dev); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 608 | pm_runtime_mark_last_busy(swrm->dev); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 609 | return ret; |
| 610 | } |
| 611 | |
| 612 | static 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 Katkam | 0db4801 | 2018-11-09 11:01:23 +0530 | [diff] [blame] | 623 | if (!dev_num) { |
| 624 | dev_err(&master->dev, "%s: invalid slave dev num\n", __func__); |
| 625 | return -EINVAL; |
| 626 | } |
Laxminath Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 627 | mutex_lock(&swrm->devlock); |
| 628 | if (!swrm->dev_up) { |
| 629 | mutex_unlock(&swrm->devlock); |
| 630 | return 0; |
| 631 | } |
| 632 | mutex_unlock(&swrm->devlock); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 633 | |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 634 | pm_runtime_get_sync(swrm->dev); |
Ramprasad Katkam | 0db4801 | 2018-11-09 11:01:23 +0530 | [diff] [blame] | 635 | ret = swrm_cmd_fifo_wr_cmd(swrm, reg_val, dev_num, 0, reg_addr); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 636 | |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 637 | pm_runtime_put_autosuspend(swrm->dev); |
| 638 | pm_runtime_mark_last_busy(swrm->dev); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 639 | return ret; |
| 640 | } |
| 641 | |
| 642 | static 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 Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 657 | mutex_lock(&swrm->devlock); |
| 658 | if (!swrm->dev_up) { |
| 659 | mutex_unlock(&swrm->devlock); |
| 660 | return 0; |
| 661 | } |
| 662 | mutex_unlock(&swrm->devlock); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 663 | |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 664 | pm_runtime_get_sync(swrm->dev); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 665 | 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); |
| 698 | mem_fail: |
| 699 | kfree(swr_fifo_reg); |
| 700 | err: |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 701 | pm_runtime_put_autosuspend(swrm->dev); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 702 | pm_runtime_mark_last_busy(swrm->dev); |
| 703 | return ret; |
| 704 | } |
| 705 | |
| 706 | static 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 | |
| 712 | static 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 | |
| 719 | static 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 | |
| 733 | static 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 | |
| 754 | end: |
| 755 | return is_removed; |
| 756 | } |
| 757 | |
| 758 | static 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 | |
| 812 | static 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 Katkam | c8d52a1 | 2018-08-31 02:30:00 +0530 | [diff] [blame] | 835 | port_req->ch_en = port_req->req_ch; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 836 | |
| 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 Katkam | c8d52a1 | 2018-08-31 02:30:00 +0530 | [diff] [blame] | 844 | mport->ch_en = mport->req_ch; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 845 | |
| 846 | if (!(mport->ch_en)) { |
| 847 | mport->port_en = false; |
| 848 | master->port_en_mask &= ~i; |
| 849 | } |
| 850 | } |
| 851 | } |
| 852 | static 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 Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 861 | u8 hparams; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 862 | 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 Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 897 | if (mport->offset2 != SWR_INVALID_PARAM) { |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 898 | 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 Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 904 | 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 Katkam | 2a0996b | 2018-09-25 20:13:30 +0530 | [diff] [blame] | 921 | if (mport->blk_pack_mode != SWR_INVALID_PARAM |
| 922 | && swrm->master_id != MASTER_ID_WSA) { |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 923 | 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 946 | port_req->ch_en = port_req->req_ch; |
| 947 | } |
| 948 | value = ((mport->req_ch) |
| 949 | << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT); |
Ramprasad Katkam | 2a0996b | 2018-09-25 20:13:30 +0530 | [diff] [blame] | 950 | |
| 951 | if (mport->offset2 != SWR_INVALID_PARAM) |
| 952 | value |= ((mport->offset2) |
| 953 | << SWRM_DP_PORT_CTRL_OFFSET2_SHFT); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 954 | 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 961 | 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 Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 965 | 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 Kasam | e30eef7 | 2018-11-05 17:40:09 +0530 | [diff] [blame] | 981 | hparams = (mport->hstop << 4) | mport->hstart; |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 982 | val[len++] = hparams; |
Laxminath Kasam | 990c70b | 2018-11-09 23:15:09 +0530 | [diff] [blame] | 983 | } 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 Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 987 | } |
| 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 992 | mport->ch_en = mport->req_ch; |
| 993 | |
| 994 | } |
Sudheer Papothi | 0016db1 | 2019-06-11 04:42:38 +0530 | [diff] [blame] | 995 | swrm_reg_dump(swrm, reg, val, len, __func__); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 996 | swr_master_bulk_write(swrm, reg, val, len); |
| 997 | } |
| 998 | |
| 999 | static 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 | |
| 1021 | static int swrm_slvdev_datapath_control(struct swr_master *master, bool enable) |
| 1022 | { |
| 1023 | u8 bank; |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 1024 | u32 value, n_row, n_col; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1025 | 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 Katkam | 7e35478 | 2018-11-21 15:52:54 +0530 | [diff] [blame] | 1036 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1037 | mutex_lock(&swrm->mlock); |
| 1038 | |
Ramprasad Katkam | 979b7c9 | 2019-05-17 15:31:21 +0530 | [diff] [blame] | 1039 | /* |
| 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1059 | bank = get_inactive_bank_num(swrm); |
| 1060 | |
| 1061 | if (enable) { |
Ramprasad Katkam | cab8d72 | 2018-09-28 15:54:06 +0530 | [diff] [blame] | 1062 | 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1068 | 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 Katkam | 18bc8e2 | 2018-10-25 15:04:24 +0530 | [diff] [blame] | 1075 | swr_master_write(swrm, SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, |
Ramprasad Katkam | 7e35478 | 2018-11-21 15:52:54 +0530 | [diff] [blame] | 1076 | SWRM_INTERRUPT_STATUS_MASK); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1077 | /* apply the new port config*/ |
| 1078 | swrm_apply_port_config(master); |
| 1079 | } else { |
Ramprasad Katkam | cab8d72 | 2018-09-28 15:54:06 +0530 | [diff] [blame] | 1080 | 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1086 | 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 Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 1092 | /* set col = 16 */ |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1093 | n_col = SWR_MAX_COL; |
| 1094 | } else { |
| 1095 | /* |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 1096 | * Do not change to col = 2 if there are still active ports |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1097 | */ |
| 1098 | if (!master->num_port) |
| 1099 | n_col = SWR_MIN_COL; |
| 1100 | else |
| 1101 | n_col = SWR_MAX_COL; |
| 1102 | } |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 1103 | /* Use default 50 * x, frame shape. Change based on mclk */ |
Laxminath Kasam | b0f27cd | 2018-09-06 12:17:11 +0530 | [diff] [blame] | 1104 | 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1113 | value = swr_master_read(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank)); |
| 1114 | value &= (~mask); |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 1115 | value |= ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1116 | (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 Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 1123 | enable_bank_switch(swrm, bank, n_row, n_col); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1124 | 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 Katkam | 7cb4ff6 | 2018-09-12 04:00:26 +0530 | [diff] [blame] | 1131 | } |
| 1132 | if (!swrm_is_port_en(master)) { |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1133 | 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 Katkam | cab8d72 | 2018-09-28 15:54:06 +0530 | [diff] [blame] | 1138 | exit: |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1139 | mutex_unlock(&swrm->mlock); |
| 1140 | return 0; |
| 1141 | } |
| 1142 | |
| 1143 | static 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 Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 1165 | 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 Katkam | 7cb4ff6 | 2018-09-12 04:00:26 +0530 | [diff] [blame] | 1172 | if (!swrm_is_port_en(master)) |
| 1173 | pm_runtime_get_sync(swrm->dev); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1174 | |
| 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 Katkam | cab8d72 | 2018-09-28 15:54:06 +0530 | [diff] [blame] | 1222 | set_bit(ENABLE_PENDING, &swrm->port_req_pending); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1223 | swr_port_response(master, portinfo->tid); |
| 1224 | |
| 1225 | mutex_unlock(&swrm->mlock); |
| 1226 | return 0; |
| 1227 | |
| 1228 | port_fail: |
| 1229 | mem_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 | |
| 1236 | static 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 Katkam | 86c45e0 | 2018-10-16 19:31:51 +0530 | [diff] [blame] | 1277 | mutex_unlock(&swrm->mlock); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1278 | 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 Katkam | cab8d72 | 2018-09-28 15:54:06 +0530 | [diff] [blame] | 1284 | set_bit(DISABLE_PENDING, &swrm->port_req_pending); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1285 | swr_port_response(master, portinfo->tid); |
| 1286 | mutex_unlock(&swrm->mlock); |
| 1287 | |
| 1288 | return 0; |
| 1289 | } |
| 1290 | |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 1291 | static 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1311 | static 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 | |
| 1334 | static irqreturn_t swr_mstr_interrupt(int irq, void *dev) |
| 1335 | { |
| 1336 | struct swr_mstr_ctrl *swrm = dev; |
Ramprasad Katkam | 7e35478 | 2018-11-21 15:52:54 +0530 | [diff] [blame] | 1337 | u32 value, intr_sts, intr_sts_masked; |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 1338 | u32 temp = 0; |
| 1339 | u32 status, chg_sts, i; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1340 | u8 devnum = 0; |
| 1341 | int ret = IRQ_HANDLED; |
| 1342 | struct swr_device *swr_dev; |
| 1343 | struct swr_master *mstr = &swrm->master; |
| 1344 | |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 1345 | 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1349 | |
| 1350 | mutex_lock(&swrm->reslock); |
Aditya Bavanari | f4a471d | 2019-02-19 17:57:12 +0530 | [diff] [blame] | 1351 | if (swrm_clk_request(swrm, true)) { |
Ramprasad Katkam | 14efed6 | 2019-03-07 13:16:50 +0530 | [diff] [blame] | 1352 | dev_err_ratelimited(swrm->dev, "%s:clk request failed\n", |
| 1353 | __func__); |
Aditya Bavanari | f4a471d | 2019-02-19 17:57:12 +0530 | [diff] [blame] | 1354 | mutex_unlock(&swrm->reslock); |
| 1355 | goto exit; |
| 1356 | } |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1357 | mutex_unlock(&swrm->reslock); |
| 1358 | |
| 1359 | intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS); |
Ramprasad Katkam | 7e35478 | 2018-11-21 15:52:54 +0530 | [diff] [blame] | 1360 | intr_sts_masked = intr_sts & swrm->intr_mask; |
Ramprasad Katkam | 8330351 | 2018-10-11 17:34:22 +0530 | [diff] [blame] | 1361 | handle_irq: |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1362 | for (i = 0; i < SWRM_INTERRUPT_MAX; i++) { |
Ramprasad Katkam | 7e35478 | 2018-11-21 15:52:54 +0530 | [diff] [blame] | 1363 | value = intr_sts_masked & (1 << i); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1364 | if (!value) |
| 1365 | continue; |
| 1366 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1367 | 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 Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 1371 | ret = swrm_find_alert_slave(swrm, status, &devnum); |
| 1372 | if (ret) { |
Ramprasad Katkam | 18bc8e2 | 2018-10-25 15:04:24 +0530 | [diff] [blame] | 1373 | dev_err_ratelimited(swrm->dev, |
| 1374 | "no slave alert found.spurious interrupt\n"); |
Ramprasad Katkam | 48b49b2 | 2018-10-01 20:12:46 +0530 | [diff] [blame] | 1375 | break; |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 1376 | } |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 1377 | 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 Katkam | 62d6d76 | 2018-09-20 17:50:28 +0530 | [diff] [blame] | 1383 | |
| 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1397 | 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 Katkam | debe893 | 2018-09-25 18:08:18 +0530 | [diff] [blame] | 1419 | /* 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1425 | 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 Katkam | 18bc8e2 | 2018-10-25 15:04:24 +0530 | [diff] [blame] | 1454 | dev_err_ratelimited(swrm->dev, "SWR Port collision detected\n"); |
Ramprasad Katkam | 7e35478 | 2018-11-21 15:52:54 +0530 | [diff] [blame] | 1455 | swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION; |
Ramprasad Katkam | 18bc8e2 | 2018-10-25 15:04:24 +0530 | [diff] [blame] | 1456 | swr_master_write(swrm, |
Ramprasad Katkam | 7e35478 | 2018-11-21 15:52:54 +0530 | [diff] [blame] | 1457 | SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1458 | break; |
| 1459 | case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH: |
| 1460 | dev_dbg(swrm->dev, "SWR read enable valid mismatch\n"); |
Ramprasad Katkam | 7e35478 | 2018-11-21 15:52:54 +0530 | [diff] [blame] | 1461 | swrm->intr_mask &= |
Ramprasad Katkam | 18bc8e2 | 2018-10-25 15:04:24 +0530 | [diff] [blame] | 1462 | ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH; |
| 1463 | swr_master_write(swrm, |
Ramprasad Katkam | 7e35478 | 2018-11-21 15:52:54 +0530 | [diff] [blame] | 1464 | SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1465 | 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 Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 1488 | swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts); |
| 1489 | swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0); |
Ramprasad Katkam | 8330351 | 2018-10-11 17:34:22 +0530 | [diff] [blame] | 1490 | |
| 1491 | intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS); |
Ramprasad Katkam | 7e35478 | 2018-11-21 15:52:54 +0530 | [diff] [blame] | 1492 | intr_sts_masked = intr_sts & swrm->intr_mask; |
Ramprasad Katkam | 8330351 | 2018-10-11 17:34:22 +0530 | [diff] [blame] | 1493 | |
Ramprasad Katkam | 7e35478 | 2018-11-21 15:52:54 +0530 | [diff] [blame] | 1494 | if (intr_sts_masked) { |
Ramprasad Katkam | 8330351 | 2018-10-11 17:34:22 +0530 | [diff] [blame] | 1495 | dev_dbg(swrm->dev, "%s: new interrupt received\n", __func__); |
| 1496 | goto handle_irq; |
| 1497 | } |
| 1498 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1499 | mutex_lock(&swrm->reslock); |
| 1500 | swrm_clk_request(swrm, false); |
| 1501 | mutex_unlock(&swrm->reslock); |
Aditya Bavanari | f4a471d | 2019-02-19 17:57:12 +0530 | [diff] [blame] | 1502 | exit: |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 1503 | swrm_unlock_sleep(swrm); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1504 | return ret; |
| 1505 | } |
| 1506 | |
Sudheer Papothi | d19d0c5 | 2019-02-23 05:41:39 +0530 | [diff] [blame] | 1507 | static 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 Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 1524 | 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 Mani | 035c50b | 2019-05-02 13:35:01 -0700 | [diff] [blame] | 1531 | } |
Sudheer Papothi | d19d0c5 | 2019-02-23 05:41:39 +0530 | [diff] [blame] | 1532 | 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; |
| 1537 | handle_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 Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 1699 | swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false); |
| 1700 | swrm_request_hw_vote(swrm, LPASS_HW_CORE, false); |
Karthikeyan Mani | 035c50b | 2019-05-02 13:35:01 -0700 | [diff] [blame] | 1701 | exit: |
Sudheer Papothi | d19d0c5 | 2019-02-23 05:41:39 +0530 | [diff] [blame] | 1702 | mutex_unlock(&swrm->reslock); |
| 1703 | swrm_unlock_sleep(swrm); |
| 1704 | return ret; |
| 1705 | } |
| 1706 | |
Aditya Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 1707 | static 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 Katkam | 44b7a96 | 2018-12-20 15:08:44 +0530 | [diff] [blame] | 1724 | if (unlikely(swrm_lock_sleep(swrm) == false)) { |
| 1725 | dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__); |
| 1726 | goto exit; |
| 1727 | } |
Aditya Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 1728 | 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 Katkam | 44b7a96 | 2018-12-20 15:08:44 +0530 | [diff] [blame] | 1733 | swrm_unlock_sleep(swrm); |
| 1734 | exit: |
Aditya Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 1735 | return ret; |
| 1736 | } |
| 1737 | |
Laxminath Kasam | f0128ef | 2018-08-31 15:15:09 +0530 | [diff] [blame] | 1738 | static 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 Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 1748 | |
| 1749 | mutex_lock(&swrm->devlock); |
| 1750 | if (!swrm->dev_up) { |
| 1751 | mutex_unlock(&swrm->devlock); |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 1752 | goto exit; |
Ramprasad Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 1753 | } |
| 1754 | mutex_unlock(&swrm->devlock); |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 1755 | if (unlikely(swrm_lock_sleep(swrm) == false)) { |
| 1756 | dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__); |
| 1757 | goto exit; |
| 1758 | } |
Laxminath Kasam | f0128ef | 2018-08-31 15:15:09 +0530 | [diff] [blame] | 1759 | pm_runtime_get_sync(swrm->dev); |
Laxminath Kasam | f0128ef | 2018-08-31 15:15:09 +0530 | [diff] [blame] | 1760 | pm_runtime_mark_last_busy(swrm->dev); |
| 1761 | pm_runtime_put_autosuspend(swrm->dev); |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 1762 | swrm_unlock_sleep(swrm); |
| 1763 | exit: |
| 1764 | pm_relax(swrm->dev); |
Laxminath Kasam | f0128ef | 2018-08-31 15:15:09 +0530 | [diff] [blame] | 1765 | } |
| 1766 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1767 | static 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 | |
| 1777 | static 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 Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 1796 | |
| 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1804 | 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 Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 1810 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1811 | /* |
| 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 Papothi | 6abd2de | 2018-09-05 05:57:04 +0530 | [diff] [blame] | 1840 | |
| 1841 | static 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 Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 1850 | if (unlikely(swrm_lock_sleep(swrm) == false)) { |
| 1851 | dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__); |
| 1852 | return; |
| 1853 | } |
Sudheer Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 1854 | 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 Papothi | 6abd2de | 2018-09-05 05:57:04 +0530 | [diff] [blame] | 1868 | pm_runtime_get_sync(swrm->dev); |
| 1869 | } |
| 1870 | |
| 1871 | static 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 Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 1882 | 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 Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 1897 | swrm_unlock_sleep(swrm); |
Sudheer Papothi | 6abd2de | 2018-09-05 05:57:04 +0530 | [diff] [blame] | 1898 | } |
| 1899 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1900 | static int swrm_master_init(struct swr_mstr_ctrl *swrm) |
| 1901 | { |
| 1902 | int ret = 0; |
| 1903 | u32 val; |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 1904 | u8 row_ctrl = SWR_ROW_50; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1905 | 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1924 | /* 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 Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 1936 | reg[len] = SWRM_MCP_BUS_CTRL_ADDR; |
| 1937 | value[len++] = 0x2; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1938 | |
Ramprasad Katkam | 8330351 | 2018-10-11 17:34:22 +0530 | [diff] [blame] | 1939 | /* Set IRQ to PULSE */ |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1940 | reg[len] = SWRM_COMP_CFG_ADDR; |
Ramprasad Katkam | 8330351 | 2018-10-11 17:34:22 +0530 | [diff] [blame] | 1941 | value[len++] = 0x02; |
| 1942 | |
| 1943 | reg[len] = SWRM_COMP_CFG_ADDR; |
| 1944 | value[len++] = 0x03; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1945 | |
| 1946 | reg[len] = SWRM_INTERRUPT_CLEAR; |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 1947 | value[len++] = 0xFFFFFFFF; |
| 1948 | |
Ramprasad Katkam | 7e35478 | 2018-11-21 15:52:54 +0530 | [diff] [blame] | 1949 | swrm->intr_mask = SWRM_INTERRUPT_STATUS_MASK; |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 1950 | /* Mask soundwire interrupts */ |
| 1951 | reg[len] = SWRM_INTERRUPT_MASK_ADDR; |
Ramprasad Katkam | 7e35478 | 2018-11-21 15:52:54 +0530 | [diff] [blame] | 1952 | value[len++] = swrm->intr_mask; |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 1953 | |
| 1954 | reg[len] = SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN; |
Ramprasad Katkam | 7e35478 | 2018-11-21 15:52:54 +0530 | [diff] [blame] | 1955 | value[len++] = swrm->intr_mask; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1956 | |
| 1957 | swr_master_bulk_write(swrm, reg, value, len); |
| 1958 | |
Sudheer Papothi | 63f4815 | 2018-11-15 01:08:03 +0530 | [diff] [blame] | 1959 | /* |
| 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1968 | return ret; |
| 1969 | } |
| 1970 | |
Ramprasad Katkam | 68765ab | 2018-08-30 11:46:32 +0530 | [diff] [blame] | 1971 | static int swrm_event_notify(struct notifier_block *self, |
Laxminath Kasam | f0128ef | 2018-08-31 15:15:09 +0530 | [diff] [blame] | 1972 | unsigned long action, void *data) |
Ramprasad Katkam | 68765ab | 2018-08-30 11:46:32 +0530 | [diff] [blame] | 1973 | { |
| 1974 | struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl, |
Laxminath Kasam | f0128ef | 2018-08-31 15:15:09 +0530 | [diff] [blame] | 1975 | event_notifier); |
| 1976 | |
| 1977 | if (!swrm || !(swrm->dev)) { |
| 1978 | pr_err("%s: swrm or dev is NULL\n", __func__); |
Ramprasad Katkam | 68765ab | 2018-08-30 11:46:32 +0530 | [diff] [blame] | 1979 | return -EINVAL; |
| 1980 | } |
Laxminath Kasam | f0128ef | 2018-08-31 15:15:09 +0530 | [diff] [blame] | 1981 | switch (action) { |
| 1982 | case MSM_AUD_DC_EVENT: |
| 1983 | schedule_work(&(swrm->dc_presence_work)); |
| 1984 | break; |
| 1985 | case SWR_WAKE_IRQ_EVENT: |
Aditya Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 1986 | if (swrm->ipc_wakeup && !swrm->ipc_wakeup_triggered) { |
| 1987 | swrm->ipc_wakeup_triggered = true; |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 1988 | pm_stay_awake(swrm->dev); |
Laxminath Kasam | f0128ef | 2018-08-31 15:15:09 +0530 | [diff] [blame] | 1989 | schedule_work(&swrm->wakeup_work); |
Ramprasad Katkam | cd61c6e | 2018-09-18 13:22:58 +0530 | [diff] [blame] | 1990 | } |
Laxminath Kasam | f0128ef | 2018-08-31 15:15:09 +0530 | [diff] [blame] | 1991 | break; |
| 1992 | default: |
| 1993 | dev_err(swrm->dev, "%s: invalid event type: %lu\n", |
Ramprasad Katkam | 68765ab | 2018-08-30 11:46:32 +0530 | [diff] [blame] | 1994 | __func__, action); |
| 1995 | return -EINVAL; |
| 1996 | } |
| 1997 | |
Ramprasad Katkam | 68765ab | 2018-08-30 11:46:32 +0530 | [diff] [blame] | 1998 | return 0; |
| 1999 | } |
| 2000 | |
| 2001 | static void swrm_notify_work_fn(struct work_struct *work) |
| 2002 | { |
| 2003 | struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl, |
Laxminath Kasam | f0128ef | 2018-08-31 15:15:09 +0530 | [diff] [blame] | 2004 | dc_presence_work); |
| 2005 | |
| 2006 | if (!swrm || !swrm->pdev) { |
| 2007 | pr_err("%s: swrm or pdev is NULL\n", __func__); |
| 2008 | return; |
| 2009 | } |
Ramprasad Katkam | 68765ab | 2018-08-30 11:46:32 +0530 | [diff] [blame] | 2010 | swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL); |
| 2011 | } |
| 2012 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2013 | static 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 Papothi | 66d6fd1 | 2019-03-27 17:34:48 +0530 | [diff] [blame] | 2020 | struct clk *lpass_core_hw_vote = NULL; |
Sudheer Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 2021 | struct clk *lpass_core_audio = NULL; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2022 | |
| 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 Kasam | f0128ef | 2018-08-31 15:15:09 +0530 | [diff] [blame] | 2030 | swrm->pdev = pdev; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2031 | 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 Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 2048 | 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 Kasam | fbcaf32 | 2018-07-18 00:38:14 +0530 | [diff] [blame] | 2054 | 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2057 | "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 Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 2092 | 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 Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 2097 | |
| 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2112 | /* 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 Papothi | 6abd2de | 2018-09-05 05:57:04 +0530 | [diff] [blame] | 2170 | swrm->master.device_wakeup_vote = swrm_device_wakeup_vote; |
| 2171 | swrm->master.device_wakeup_unvote = swrm_device_wakeup_unvote; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2172 | 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 Bucha | df38c3e | 2019-03-11 17:10:23 +0530 | [diff] [blame] | 2180 | swrm->swr_irq_wakeup_capable = 0; |
Laxminath Kasam | b0f27cd | 2018-09-06 12:17:11 +0530 | [diff] [blame] | 2181 | swrm->mclk_freq = MCLK_FREQ; |
Laxminath Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 2182 | swrm->dev_up = true; |
Ramprasad Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 2183 | swrm->state = SWR_MSTR_UP; |
Aditya Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 2184 | swrm->ipc_wakeup = false; |
| 2185 | swrm->ipc_wakeup_triggered = false; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2186 | init_completion(&swrm->reset); |
| 2187 | init_completion(&swrm->broadcast); |
Ramprasad Katkam | 6bce2e7 | 2018-10-10 19:20:13 +0530 | [diff] [blame] | 2188 | init_completion(&swrm->clk_off_complete); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2189 | mutex_init(&swrm->mlock); |
| 2190 | mutex_init(&swrm->reslock); |
| 2191 | mutex_init(&swrm->force_down_lock); |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 2192 | mutex_init(&swrm->iolock); |
Ramprasad Katkam | 6bce2e7 | 2018-10-10 19:20:13 +0530 | [diff] [blame] | 2193 | mutex_init(&swrm->clklock); |
Laxminath Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 2194 | mutex_init(&swrm->devlock); |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 2195 | 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2202 | |
| 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 Kasam | fbcaf32 | 2018-07-18 00:38:14 +0530 | [diff] [blame] | 2219 | goto err_irq_fail; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2220 | } |
| 2221 | |
| 2222 | ret = request_threaded_irq(swrm->irq, NULL, |
Sudheer Papothi | d19d0c5 | 2019-02-23 05:41:39 +0530 | [diff] [blame] | 2223 | swr_mstr_interrupt_v2, |
Ramprasad Katkam | 8330351 | 2018-10-11 17:34:22 +0530 | [diff] [blame] | 2224 | IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2225 | "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 Bucha | df38c3e | 2019-03-11 17:10:23 +0530 | [diff] [blame] | 2233 | /* 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2242 | 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 Kasam | f0128ef | 2018-08-31 15:15:09 +0530 | [diff] [blame] | 2265 | INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2266 | |
| 2267 | if (pdev->dev.of_node) |
| 2268 | of_register_swr_devices(&swrm->master); |
| 2269 | |
Sudheer Papothi | 66d6fd1 | 2019-03-27 17:34:48 +0530 | [diff] [blame] | 2270 | /* 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 Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 2281 | /* 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2292 | 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 Bucha | df38c3e | 2019-03-11 17:10:23 +0530 | [diff] [blame] | 2308 | |
| 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2315 | 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 Katkam | 68765ab | 2018-08-30 11:46:32 +0530 | [diff] [blame] | 2321 | 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2325 | return 0; |
Vatsal Bucha | df38c3e | 2019-03-11 17:10:23 +0530 | [diff] [blame] | 2326 | err_irq_wakeup_fail: |
| 2327 | device_init_wakeup(swrm->dev, false); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2328 | err_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); |
| 2334 | err_irq_fail: |
| 2335 | mutex_destroy(&swrm->mlock); |
| 2336 | mutex_destroy(&swrm->reslock); |
| 2337 | mutex_destroy(&swrm->force_down_lock); |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 2338 | mutex_destroy(&swrm->iolock); |
Ramprasad Katkam | 6bce2e7 | 2018-10-10 19:20:13 +0530 | [diff] [blame] | 2339 | mutex_destroy(&swrm->clklock); |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 2340 | mutex_destroy(&swrm->pm_lock); |
| 2341 | pm_qos_remove_request(&swrm->pm_qos_req); |
| 2342 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2343 | err_pdata_fail: |
| 2344 | err_memory_fail: |
| 2345 | return ret; |
| 2346 | } |
| 2347 | |
| 2348 | static 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 Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 2357 | else if (swrm->wake_irq > 0) |
| 2358 | free_irq(swrm->wake_irq, swrm); |
Vatsal Bucha | df38c3e | 2019-03-11 17:10:23 +0530 | [diff] [blame] | 2359 | if (swrm->swr_irq_wakeup_capable) |
| 2360 | irq_set_irq_wake(swrm->irq, 0); |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 2361 | cancel_work_sync(&swrm->wakeup_work); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2362 | pm_runtime_disable(&pdev->dev); |
| 2363 | pm_runtime_set_suspended(&pdev->dev); |
| 2364 | swr_unregister_master(&swrm->master); |
Ramprasad Katkam | 68765ab | 2018-08-30 11:46:32 +0530 | [diff] [blame] | 2365 | msm_aud_evt_unregister_client(&swrm->event_notifier); |
Vatsal Bucha | df38c3e | 2019-03-11 17:10:23 +0530 | [diff] [blame] | 2366 | device_init_wakeup(swrm->dev, false); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2367 | mutex_destroy(&swrm->mlock); |
| 2368 | mutex_destroy(&swrm->reslock); |
Ramprasad Katkam | 6bce2e7 | 2018-10-10 19:20:13 +0530 | [diff] [blame] | 2369 | mutex_destroy(&swrm->iolock); |
| 2370 | mutex_destroy(&swrm->clklock); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2371 | mutex_destroy(&swrm->force_down_lock); |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 2372 | mutex_destroy(&swrm->pm_lock); |
| 2373 | pm_qos_remove_request(&swrm->pm_qos_req); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2374 | devm_kfree(&pdev->dev, swrm); |
| 2375 | return 0; |
| 2376 | } |
| 2377 | |
| 2378 | static 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2387 | |
| 2388 | return 0; |
| 2389 | } |
| 2390 | |
| 2391 | #ifdef CONFIG_PM |
| 2392 | static 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 Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 2397 | bool hw_core_err = false; |
| 2398 | bool aud_core_err = false; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2399 | 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 Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 2405 | |
Sudheer Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 2406 | 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 Mani | f682190 | 2019-05-21 17:31:24 -0700 | [diff] [blame] | 2415 | } |
Sudheer Papothi | 66d6fd1 | 2019-03-27 17:34:48 +0530 | [diff] [blame] | 2416 | |
Ramprasad Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 2417 | if ((swrm->state == SWR_MSTR_DOWN) || |
| 2418 | (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) { |
Aditya Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 2419 | 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 Kasam | f0128ef | 2018-08-31 15:15:09 +0530 | [diff] [blame] | 2423 | } |
| 2424 | |
Ramprasad Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 2425 | if (swrm_clk_request(swrm, true)) |
| 2426 | goto exit; |
| 2427 | if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) { |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 2428 | list_for_each_entry(swr_dev, &mstr->devices, dev_list) { |
| 2429 | ret = swr_device_up(swr_dev); |
Sudheer Papothi | 79c9075 | 2019-04-23 06:09:52 +0530 | [diff] [blame] | 2430 | 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 Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 2436 | 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2442 | } |
Ramprasad Katkam | 48b49b2 | 2018-10-01 20:12:46 +0530 | [diff] [blame] | 2443 | 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 Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 2446 | swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, 0x0, |
| 2447 | SWRS_SCP_INT_STATUS_MASK_1); |
Karthikeyan Mani | f682190 | 2019-05-21 17:31:24 -0700 | [diff] [blame] | 2448 | 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 Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 2453 | } else { |
| 2454 | /*wake up from clock stop*/ |
| 2455 | swr_master_write(swrm, SWRM_MCP_BUS_CTRL_ADDR, 0x2); |
| 2456 | usleep_range(100, 105); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2457 | } |
Ramprasad Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 2458 | swrm->state = SWR_MSTR_UP; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2459 | } |
| 2460 | exit: |
Sudheer Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 2461 | 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2465 | pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer); |
| 2466 | mutex_unlock(&swrm->reslock); |
Sudheer Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 2467 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2468 | return ret; |
| 2469 | } |
| 2470 | |
| 2471 | static 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 Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 2476 | bool hw_core_err = false; |
| 2477 | bool aud_core_err = false; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2478 | 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 Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 2488 | |
| 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 Mani | f682190 | 2019-05-21 17:31:24 -0700 | [diff] [blame] | 2498 | } |
Sudheer Papothi | 66d6fd1 | 2019-03-27 17:34:48 +0530 | [diff] [blame] | 2499 | |
Ramprasad Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 2500 | if ((current_state == SWR_MSTR_UP) || |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2501 | (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 Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 2509 | if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) { |
Ramprasad Katkam | b4c7c68 | 2018-12-19 18:58:36 +0530 | [diff] [blame] | 2510 | enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL); |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 2511 | 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 Papothi | 79c9075 | 2019-04-23 06:09:52 +0530 | [diff] [blame] | 2515 | 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 Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 2521 | dev_err(dev, |
| 2522 | "%s: failed to shutdown swr dev %d\n", |
| 2523 | __func__, swr_dev->dev_num); |
| 2524 | goto exit; |
| 2525 | } |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2526 | } |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 2527 | } else { |
Sudheer Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 2528 | mutex_unlock(&swrm->reslock); |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 2529 | /* clock stop sequence */ |
| 2530 | swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF, |
| 2531 | SWRS_SCP_CONTROL); |
Sudheer Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 2532 | mutex_lock(&swrm->reslock); |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 2533 | usleep_range(100, 105); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2534 | } |
| 2535 | swrm_clk_request(swrm, false); |
Ramprasad Katkam | 6a3050d | 2018-10-10 02:08:00 +0530 | [diff] [blame] | 2536 | |
Aditya Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 2537 | 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 Katkam | 6a3050d | 2018-10-10 02:08:00 +0530 | [diff] [blame] | 2545 | } |
| 2546 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2547 | } |
Ramprasad Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 2548 | /* Retain SSR state until resume */ |
| 2549 | if (current_state != SWR_MSTR_SSR) |
| 2550 | swrm->state = SWR_MSTR_DOWN; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2551 | exit: |
Sudheer Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 2552 | 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2556 | mutex_unlock(&swrm->reslock); |
| 2557 | return ret; |
| 2558 | } |
| 2559 | #endif /* CONFIG_PM */ |
| 2560 | |
| 2561 | static 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 Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 2572 | 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2580 | |
Laxminath Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 2581 | return 0; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2582 | } |
| 2583 | |
Aditya Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 2584 | int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm) |
| 2585 | { |
| 2586 | int ret = 0; |
Laxminath Kasam | a60239e | 2019-01-10 14:43:03 +0530 | [diff] [blame] | 2587 | int irq, dir_apps_irq; |
Aditya Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 2588 | |
| 2589 | if (!swrm->ipc_wakeup) { |
Laxminath Kasam | a60239e | 2019-01-10 14:43:03 +0530 | [diff] [blame] | 2590 | 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 Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 2608 | } |
Aditya Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 2609 | 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 Bavanari | 3517b11 | 2018-12-03 13:26:59 +0530 | [diff] [blame] | 2618 | irq_set_irq_wake(swrm->wake_irq, 1); |
Aditya Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 2619 | } |
| 2620 | return ret; |
| 2621 | } |
| 2622 | |
Sudheer Papothi | 3d1596e | 2018-10-27 06:19:18 +0530 | [diff] [blame] | 2623 | static 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 | |
| 2647 | static 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2670 | /** |
| 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 | */ |
| 2677 | int 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 Papothi | 3d1596e | 2018-10-27 06:19:18 +0530 | [diff] [blame] | 2683 | struct swrm_port_config *port_cfg; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2684 | |
| 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 Kasam | b0f27cd | 2018-09-06 12:17:11 +0530 | [diff] [blame] | 2697 | 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 Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 2707 | case SWR_DEVICE_SSR_DOWN: |
| 2708 | mutex_lock(&swrm->devlock); |
| 2709 | swrm->dev_up = false; |
| 2710 | mutex_unlock(&swrm->devlock); |
Ramprasad Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 2711 | mutex_lock(&swrm->reslock); |
| 2712 | swrm->state = SWR_MSTR_SSR; |
| 2713 | mutex_unlock(&swrm->reslock); |
Laxminath Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 2714 | break; |
| 2715 | case SWR_DEVICE_SSR_UP: |
Ramprasad Katkam | 6bce2e7 | 2018-10-10 19:20:13 +0530 | [diff] [blame] | 2716 | /* wait for clk voting to be zero */ |
Ramprasad Katkam | 7f6462e | 2018-11-06 11:51:22 +0530 | [diff] [blame] | 2717 | reinit_completion(&swrm->clk_off_complete); |
Ramprasad Katkam | 6bce2e7 | 2018-10-10 19:20:13 +0530 | [diff] [blame] | 2718 | if (swrm->clk_ref_count && |
| 2719 | !wait_for_completion_timeout(&swrm->clk_off_complete, |
Ramprasad Katkam | c87efeb | 2018-12-12 19:26:19 +0530 | [diff] [blame] | 2720 | msecs_to_jiffies(500))) |
Ramprasad Katkam | 6bce2e7 | 2018-10-10 19:20:13 +0530 | [diff] [blame] | 2721 | dev_err(swrm->dev, "%s: clock voting not zero\n", |
| 2722 | __func__); |
| 2723 | |
Laxminath Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 2724 | mutex_lock(&swrm->devlock); |
| 2725 | swrm->dev_up = true; |
| 2726 | mutex_unlock(&swrm->devlock); |
| 2727 | break; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2728 | case SWR_DEVICE_DOWN: |
| 2729 | dev_dbg(swrm->dev, "%s: swr master down called\n", __func__); |
| 2730 | mutex_lock(&swrm->mlock); |
Ramprasad Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 2731 | if (swrm->state == SWR_MSTR_DOWN) |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2732 | 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 Katkam | 0fed92f | 2018-11-08 14:22:22 +0530 | [diff] [blame] | 2740 | 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2747 | mutex_lock(&swrm->mlock); |
Ramprasad Katkam | 86c45e0 | 2018-10-16 19:31:51 +0530 | [diff] [blame] | 2748 | pm_runtime_mark_last_busy(&pdev->dev); |
| 2749 | pm_runtime_get_sync(&pdev->dev); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2750 | mutex_lock(&swrm->reslock); |
Ramprasad Katkam | 86c45e0 | 2018-10-16 19:31:51 +0530 | [diff] [blame] | 2751 | 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2758 | } |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2759 | } |
Ramprasad Katkam | 86c45e0 | 2018-10-16 19:31:51 +0530 | [diff] [blame] | 2760 | pm_runtime_mark_last_busy(&pdev->dev); |
| 2761 | pm_runtime_put_autosuspend(&pdev->dev); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2762 | 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 Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 2796 | 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 Papothi | 3d1596e | 2018-10-27 06:19:18 +0530 | [diff] [blame] | 2811 | 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); |
| 2828 | done: |
| 2829 | mutex_unlock(&swrm->mlock); |
| 2830 | } |
| 2831 | break; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2832 | default: |
| 2833 | dev_err(swrm->dev, "%s: swr master unknown id %d\n", |
| 2834 | __func__, id); |
| 2835 | break; |
| 2836 | } |
| 2837 | return ret; |
| 2838 | } |
| 2839 | EXPORT_SYMBOL(swrm_wcd_notify); |
| 2840 | |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 2841 | /* |
| 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 | */ |
| 2852 | static 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 | |
| 2871 | static 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 | |
| 2909 | static 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2929 | #ifdef CONFIG_PM_SLEEP |
| 2930 | static 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 Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 2937 | |
| 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2980 | 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 | |
| 3011 | static 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 Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 3025 | 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 Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3040 | return ret; |
| 3041 | } |
| 3042 | #endif /* CONFIG_PM_SLEEP */ |
| 3043 | |
| 3044 | static 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 | |
| 3056 | static const struct of_device_id swrm_dt_match[] = { |
| 3057 | { |
| 3058 | .compatible = "qcom,swr-mstr", |
| 3059 | }, |
| 3060 | {} |
| 3061 | }; |
| 3062 | |
| 3063 | static 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 Sang | 53cd13a | 2018-06-29 15:14:37 +0800 | [diff] [blame] | 3071 | .suppress_bind_attrs = true, |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3072 | }, |
| 3073 | }; |
| 3074 | |
| 3075 | static int __init swrm_init(void) |
| 3076 | { |
| 3077 | return platform_driver_register(&swr_mstr_driver); |
| 3078 | } |
| 3079 | module_init(swrm_init); |
| 3080 | |
| 3081 | static void __exit swrm_exit(void) |
| 3082 | { |
| 3083 | platform_driver_unregister(&swr_mstr_driver); |
| 3084 | } |
| 3085 | module_exit(swrm_exit); |
| 3086 | |
| 3087 | MODULE_LICENSE("GPL v2"); |
| 3088 | MODULE_DESCRIPTION("SoundWire Master Controller"); |
| 3089 | MODULE_ALIAS("platform:swr-mstr"); |