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 | /* |
Sudheer Papothi | ae5c363 | 2019-11-27 06:52:06 +0530 | [diff] [blame] | 3 | * Copyright (c) 2015-2020, 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> |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 21 | #include <soc/soundwire.h> |
Sudheer Papothi | 3d1596e | 2018-10-27 06:19:18 +0530 | [diff] [blame] | 22 | #include <soc/swr-common.h> |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 23 | #include <linux/regmap.h> |
Ramprasad Katkam | 68765ab | 2018-08-30 11:46:32 +0530 | [diff] [blame] | 24 | #include <dsp/msm-audio-event-notify.h> |
Aditya Bavanari | bb981b7 | 2020-03-19 18:30:10 +0530 | [diff] [blame] | 25 | #include <dsp/digital-cdc-rsc-mgr.h> |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 26 | #include "swrm_registers.h" |
| 27 | #include "swr-mstr-ctrl.h" |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 28 | |
Sudheer Papothi | ae5c363 | 2019-11-27 06:52:06 +0530 | [diff] [blame] | 29 | #define SWR_NUM_PORTS 4 /* TODO - Get this info from DT */ |
| 30 | |
Sudheer Papothi | ac0ae1c | 2019-10-17 05:38:40 +0530 | [diff] [blame] | 31 | #define SWRM_FRAME_SYNC_SEL 4000 /* 4KHz */ |
Sudheer Papothi | 8a8b12b | 2019-11-15 23:06:41 +0530 | [diff] [blame] | 32 | #define SWRM_FRAME_SYNC_SEL_NATIVE 3675 /* 3.675KHz */ |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 33 | #define SWRM_SYSTEM_RESUME_TIMEOUT_MS 700 |
| 34 | #define SWRM_SYS_SUSPEND_WAIT 1 |
Sudheer Papothi | 3d1596e | 2018-10-27 06:19:18 +0530 | [diff] [blame] | 35 | |
Sudheer Papothi | 4c322b1 | 2018-10-31 06:34:01 +0530 | [diff] [blame] | 36 | #define SWRM_DSD_PARAMS_PORT 4 |
| 37 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 38 | #define SWR_BROADCAST_CMD_ID 0x0F |
Sudheer Papothi | 3590b31 | 2019-06-04 23:51:30 +0530 | [diff] [blame] | 39 | #define SWR_AUTO_SUSPEND_DELAY 1 /* delay in sec */ |
Sudheer Papothi | 7c067e8 | 2018-11-15 06:53:35 +0530 | [diff] [blame] | 40 | #define SWR_DEV_ID_MASK 0xFFFFFFFFFFFF |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 41 | #define SWR_REG_VAL_PACK(data, dev, id, reg) \ |
| 42 | ((reg) | ((id) << 16) | ((dev) << 20) | ((data) << 24)) |
| 43 | |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 44 | #define SWR_INVALID_PARAM 0xFF |
Laxminath Kasam | 990c70b | 2018-11-09 23:15:09 +0530 | [diff] [blame] | 45 | #define SWR_HSTOP_MAX_VAL 0xF |
| 46 | #define SWR_HSTART_MIN_VAL 0x0 |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 47 | |
Vatsal Bucha | e50b500 | 2019-09-19 14:32:20 +0530 | [diff] [blame] | 48 | #define ERR_AUTO_SUSPEND_TIMER_VAL 0x1 |
| 49 | |
Ramprasad Katkam | 8330351 | 2018-10-11 17:34:22 +0530 | [diff] [blame] | 50 | #define SWRM_INTERRUPT_STATUS_MASK 0x1FDFD |
Laxminath Kasam | ec8c909 | 2019-12-17 13:12:58 +0530 | [diff] [blame] | 51 | #define SWRM_LINK_STATUS_RETRY_CNT 100 |
Sudheer Papothi | ac0ae1c | 2019-10-17 05:38:40 +0530 | [diff] [blame] | 52 | |
| 53 | #define SWRM_ROW_48 48 |
| 54 | #define SWRM_ROW_50 50 |
| 55 | #define SWRM_ROW_64 64 |
| 56 | #define SWRM_COL_02 02 |
| 57 | #define SWRM_COL_16 16 |
| 58 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 59 | /* pm runtime auto suspend timer in msecs */ |
| 60 | static int auto_suspend_timer = SWR_AUTO_SUSPEND_DELAY * 1000; |
| 61 | module_param(auto_suspend_timer, int, 0664); |
| 62 | MODULE_PARM_DESC(auto_suspend_timer, "timer for auto suspend"); |
| 63 | |
| 64 | enum { |
| 65 | SWR_NOT_PRESENT, /* Device is detached/not present on the bus */ |
| 66 | SWR_ATTACHED_OK, /* Device is attached */ |
| 67 | SWR_ALERT, /* Device alters master for any interrupts */ |
| 68 | SWR_RESERVED, /* Reserved */ |
| 69 | }; |
| 70 | |
| 71 | enum { |
| 72 | MASTER_ID_WSA = 1, |
| 73 | MASTER_ID_RX, |
| 74 | MASTER_ID_TX |
| 75 | }; |
Ramprasad Katkam | cab8d72 | 2018-09-28 15:54:06 +0530 | [diff] [blame] | 76 | |
| 77 | enum { |
| 78 | ENABLE_PENDING, |
| 79 | DISABLE_PENDING |
| 80 | }; |
Sudheer Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 81 | |
| 82 | enum { |
| 83 | LPASS_HW_CORE, |
| 84 | LPASS_AUDIO_CORE, |
| 85 | }; |
| 86 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 87 | #define TRUE 1 |
| 88 | #define FALSE 0 |
| 89 | |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 90 | #define SWRM_MAX_PORT_REG 120 |
Ramprasad Katkam | 8330351 | 2018-10-11 17:34:22 +0530 | [diff] [blame] | 91 | #define SWRM_MAX_INIT_REG 11 |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 92 | |
Laxminath Kasam | fbcaf32 | 2018-07-18 00:38:14 +0530 | [diff] [blame] | 93 | #define MAX_FIFO_RD_FAIL_RETRY 3 |
| 94 | |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 95 | static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm); |
| 96 | static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm); |
Sudheer Papothi | 96c842a | 2019-08-29 12:11:21 +0530 | [diff] [blame] | 97 | static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr); |
| 98 | static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val); |
Vatsal Bucha | bd68a7c | 2020-06-08 10:41:56 +0530 | [diff] [blame] | 99 | static int swrm_runtime_resume(struct device *dev); |
Sudheer Papothi | ae5c363 | 2019-11-27 06:52:06 +0530 | [diff] [blame] | 100 | |
| 101 | static u8 swrm_get_clk_div(int mclk_freq, int bus_clk_freq) |
| 102 | { |
| 103 | int clk_div = 0; |
| 104 | u8 div_val = 0; |
| 105 | |
| 106 | if (!mclk_freq || !bus_clk_freq) |
| 107 | return 0; |
| 108 | |
| 109 | clk_div = (mclk_freq / bus_clk_freq); |
| 110 | |
| 111 | switch (clk_div) { |
| 112 | case 32: |
| 113 | div_val = 5; |
| 114 | break; |
| 115 | case 16: |
| 116 | div_val = 4; |
| 117 | break; |
| 118 | case 8: |
| 119 | div_val = 3; |
| 120 | break; |
| 121 | case 4: |
| 122 | div_val = 2; |
| 123 | break; |
| 124 | case 2: |
| 125 | div_val = 1; |
| 126 | break; |
| 127 | case 1: |
| 128 | default: |
| 129 | div_val = 0; |
| 130 | break; |
| 131 | } |
| 132 | |
| 133 | return div_val; |
| 134 | } |
| 135 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 136 | static bool swrm_is_msm_variant(int val) |
| 137 | { |
| 138 | return (val == SWRM_VERSION_1_3); |
| 139 | } |
| 140 | |
Sudheer Papothi | 96c842a | 2019-08-29 12:11:21 +0530 | [diff] [blame] | 141 | #ifdef CONFIG_DEBUG_FS |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 142 | static int swrm_debug_open(struct inode *inode, struct file *file) |
| 143 | { |
| 144 | file->private_data = inode->i_private; |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | static int get_parameters(char *buf, u32 *param1, int num_of_par) |
| 149 | { |
| 150 | char *token; |
| 151 | int base, cnt; |
| 152 | |
| 153 | token = strsep(&buf, " "); |
| 154 | for (cnt = 0; cnt < num_of_par; cnt++) { |
| 155 | if (token) { |
| 156 | if ((token[1] == 'x') || (token[1] == 'X')) |
| 157 | base = 16; |
| 158 | else |
| 159 | base = 10; |
| 160 | |
| 161 | if (kstrtou32(token, base, ¶m1[cnt]) != 0) |
| 162 | return -EINVAL; |
| 163 | |
| 164 | token = strsep(&buf, " "); |
| 165 | } else |
| 166 | return -EINVAL; |
| 167 | } |
| 168 | return 0; |
| 169 | } |
| 170 | |
Sudheer Papothi | 96c842a | 2019-08-29 12:11:21 +0530 | [diff] [blame] | 171 | static ssize_t swrm_reg_show(struct swr_mstr_ctrl *swrm, char __user *ubuf, |
| 172 | size_t count, loff_t *ppos) |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 173 | { |
| 174 | int i, reg_val, len; |
| 175 | ssize_t total = 0; |
| 176 | char tmp_buf[SWR_MSTR_MAX_BUF_LEN]; |
Sudheer Papothi | 96c842a | 2019-08-29 12:11:21 +0530 | [diff] [blame] | 177 | int rem = 0; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 178 | |
| 179 | if (!ubuf || !ppos) |
| 180 | return 0; |
| 181 | |
Sudheer Papothi | 96c842a | 2019-08-29 12:11:21 +0530 | [diff] [blame] | 182 | i = ((int) *ppos + SWR_MSTR_START_REG_ADDR); |
| 183 | rem = i%4; |
| 184 | |
| 185 | if (rem) |
| 186 | i = (i - rem); |
| 187 | |
| 188 | for (; i <= SWR_MSTR_MAX_REG_ADDR; i += 4) { |
| 189 | usleep_range(100, 150); |
| 190 | reg_val = swr_master_read(swrm, i); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 191 | len = snprintf(tmp_buf, 25, "0x%.3x: 0x%.2x\n", i, reg_val); |
Aditya Bavanari | 9f599b4 | 2019-08-27 22:18:41 +0530 | [diff] [blame] | 192 | if (len < 0) { |
| 193 | pr_err("%s: fail to fill the buffer\n", __func__); |
| 194 | total = -EFAULT; |
| 195 | goto copy_err; |
| 196 | } |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 197 | if ((total + len) >= count - 1) |
| 198 | break; |
| 199 | if (copy_to_user((ubuf + total), tmp_buf, len)) { |
| 200 | pr_err("%s: fail to copy reg dump\n", __func__); |
| 201 | total = -EFAULT; |
| 202 | goto copy_err; |
| 203 | } |
| 204 | *ppos += len; |
| 205 | total += len; |
| 206 | } |
| 207 | |
| 208 | copy_err: |
| 209 | return total; |
| 210 | } |
| 211 | |
Sudheer Papothi | 96c842a | 2019-08-29 12:11:21 +0530 | [diff] [blame] | 212 | static ssize_t swrm_debug_reg_dump(struct file *file, char __user *ubuf, |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 213 | size_t count, loff_t *ppos) |
| 214 | { |
Sudheer Papothi | 96c842a | 2019-08-29 12:11:21 +0530 | [diff] [blame] | 215 | struct swr_mstr_ctrl *swrm; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 216 | |
| 217 | if (!count || !file || !ppos || !ubuf) |
| 218 | return -EINVAL; |
| 219 | |
Sudheer Papothi | 96c842a | 2019-08-29 12:11:21 +0530 | [diff] [blame] | 220 | swrm = file->private_data; |
| 221 | if (!swrm) |
| 222 | return -EINVAL; |
| 223 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 224 | if (*ppos < 0) |
| 225 | return -EINVAL; |
| 226 | |
Sudheer Papothi | 96c842a | 2019-08-29 12:11:21 +0530 | [diff] [blame] | 227 | return swrm_reg_show(swrm, ubuf, count, ppos); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 228 | } |
| 229 | |
Sudheer Papothi | 96c842a | 2019-08-29 12:11:21 +0530 | [diff] [blame] | 230 | static ssize_t swrm_debug_read(struct file *file, char __user *ubuf, |
| 231 | size_t count, loff_t *ppos) |
| 232 | { |
| 233 | char lbuf[SWR_MSTR_RD_BUF_LEN]; |
| 234 | struct swr_mstr_ctrl *swrm = NULL; |
| 235 | |
| 236 | if (!count || !file || !ppos || !ubuf) |
| 237 | return -EINVAL; |
| 238 | |
| 239 | swrm = file->private_data; |
| 240 | if (!swrm) |
| 241 | return -EINVAL; |
| 242 | |
| 243 | if (*ppos < 0) |
| 244 | return -EINVAL; |
| 245 | |
| 246 | snprintf(lbuf, sizeof(lbuf), "0x%x\n", swrm->read_data); |
| 247 | |
| 248 | return simple_read_from_buffer(ubuf, count, ppos, lbuf, |
| 249 | strnlen(lbuf, 7)); |
| 250 | } |
| 251 | |
| 252 | static ssize_t swrm_debug_peek_write(struct file *file, const char __user *ubuf, |
| 253 | size_t count, loff_t *ppos) |
| 254 | { |
| 255 | char lbuf[SWR_MSTR_RD_BUF_LEN]; |
| 256 | int rc; |
| 257 | u32 param[5]; |
| 258 | struct swr_mstr_ctrl *swrm = NULL; |
| 259 | |
| 260 | if (!count || !file || !ppos || !ubuf) |
| 261 | return -EINVAL; |
| 262 | |
| 263 | swrm = file->private_data; |
| 264 | if (!swrm) |
| 265 | return -EINVAL; |
| 266 | |
| 267 | if (*ppos < 0) |
| 268 | return -EINVAL; |
| 269 | |
| 270 | if (count > sizeof(lbuf) - 1) |
| 271 | return -EINVAL; |
| 272 | |
| 273 | rc = copy_from_user(lbuf, ubuf, count); |
| 274 | if (rc) |
| 275 | return -EFAULT; |
| 276 | |
| 277 | lbuf[count] = '\0'; |
| 278 | rc = get_parameters(lbuf, param, 1); |
| 279 | if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) && (rc == 0)) |
| 280 | swrm->read_data = swr_master_read(swrm, param[0]); |
| 281 | else |
| 282 | rc = -EINVAL; |
| 283 | |
| 284 | if (rc == 0) |
| 285 | rc = count; |
| 286 | else |
| 287 | dev_err(swrm->dev, "%s: rc = %d\n", __func__, rc); |
| 288 | |
| 289 | return rc; |
| 290 | } |
| 291 | |
| 292 | static ssize_t swrm_debug_write(struct file *file, |
| 293 | const char __user *ubuf, size_t count, loff_t *ppos) |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 294 | { |
| 295 | char lbuf[SWR_MSTR_WR_BUF_LEN]; |
| 296 | int rc; |
| 297 | u32 param[5]; |
Sudheer Papothi | 96c842a | 2019-08-29 12:11:21 +0530 | [diff] [blame] | 298 | struct swr_mstr_ctrl *swrm; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 299 | |
Sudheer Papothi | 96c842a | 2019-08-29 12:11:21 +0530 | [diff] [blame] | 300 | if (!file || !ppos || !ubuf) |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 301 | return -EINVAL; |
| 302 | |
Sudheer Papothi | 96c842a | 2019-08-29 12:11:21 +0530 | [diff] [blame] | 303 | swrm = file->private_data; |
| 304 | if (!swrm) |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 305 | return -EINVAL; |
| 306 | |
Sudheer Papothi | 96c842a | 2019-08-29 12:11:21 +0530 | [diff] [blame] | 307 | if (count > sizeof(lbuf) - 1) |
| 308 | return -EINVAL; |
| 309 | |
| 310 | rc = copy_from_user(lbuf, ubuf, count); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 311 | if (rc) |
| 312 | return -EFAULT; |
| 313 | |
Sudheer Papothi | 96c842a | 2019-08-29 12:11:21 +0530 | [diff] [blame] | 314 | lbuf[count] = '\0'; |
| 315 | rc = get_parameters(lbuf, param, 2); |
| 316 | if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) && |
| 317 | (param[1] <= 0xFFFFFFFF) && |
| 318 | (rc == 0)) |
| 319 | swr_master_write(swrm, param[0], param[1]); |
| 320 | else |
| 321 | rc = -EINVAL; |
| 322 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 323 | if (rc == 0) |
Sudheer Papothi | 96c842a | 2019-08-29 12:11:21 +0530 | [diff] [blame] | 324 | rc = count; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 325 | else |
| 326 | pr_err("%s: rc = %d\n", __func__, rc); |
| 327 | |
| 328 | return rc; |
| 329 | } |
| 330 | |
Sudheer Papothi | 96c842a | 2019-08-29 12:11:21 +0530 | [diff] [blame] | 331 | static const struct file_operations swrm_debug_read_ops = { |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 332 | .open = swrm_debug_open, |
Sudheer Papothi | 96c842a | 2019-08-29 12:11:21 +0530 | [diff] [blame] | 333 | .write = swrm_debug_peek_write, |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 334 | .read = swrm_debug_read, |
| 335 | }; |
| 336 | |
Sudheer Papothi | 96c842a | 2019-08-29 12:11:21 +0530 | [diff] [blame] | 337 | static const struct file_operations swrm_debug_write_ops = { |
| 338 | .open = swrm_debug_open, |
| 339 | .write = swrm_debug_write, |
| 340 | }; |
| 341 | |
| 342 | static const struct file_operations swrm_debug_dump_ops = { |
| 343 | .open = swrm_debug_open, |
| 344 | .read = swrm_debug_reg_dump, |
| 345 | }; |
| 346 | #endif |
| 347 | |
Sudheer Papothi | 0016db1 | 2019-06-11 04:42:38 +0530 | [diff] [blame] | 348 | static void swrm_reg_dump(struct swr_mstr_ctrl *swrm, |
| 349 | u32 *reg, u32 *val, int len, const char* func) |
| 350 | { |
| 351 | int i = 0; |
| 352 | |
| 353 | for (i = 0; i < len; i++) |
| 354 | dev_dbg(swrm->dev, "%s: reg = 0x%x val = 0x%x\n", |
| 355 | func, reg[i], val[i]); |
| 356 | } |
| 357 | |
Sudheer Papothi | 921b865 | 2019-10-03 02:13:32 +0530 | [diff] [blame] | 358 | static bool is_swr_clk_needed(struct swr_mstr_ctrl *swrm) |
| 359 | { |
| 360 | return ((swrm->version <= SWRM_VERSION_1_5_1) ? true : false); |
| 361 | } |
| 362 | |
Sudheer Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 363 | static int swrm_request_hw_vote(struct swr_mstr_ctrl *swrm, |
| 364 | int core_type, bool enable) |
| 365 | { |
| 366 | int ret = 0; |
| 367 | |
Aditya Bavanari | eb04461 | 2019-12-22 17:14:15 +0530 | [diff] [blame] | 368 | mutex_lock(&swrm->devlock); |
Sudheer Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 369 | if (core_type == LPASS_HW_CORE) { |
| 370 | if (swrm->lpass_core_hw_vote) { |
| 371 | if (enable) { |
Aditya Bavanari | eb04461 | 2019-12-22 17:14:15 +0530 | [diff] [blame] | 372 | if (!swrm->dev_up) { |
| 373 | dev_dbg(swrm->dev, "%s: device is down or SSR state\n", |
| 374 | __func__); |
| 375 | trace_printk("%s: device is down or SSR state\n", |
| 376 | __func__); |
| 377 | mutex_unlock(&swrm->devlock); |
| 378 | return -ENODEV; |
| 379 | } |
| 380 | if (++swrm->hw_core_clk_en == 1) { |
| 381 | ret = |
Aditya Bavanari | bb981b7 | 2020-03-19 18:30:10 +0530 | [diff] [blame] | 382 | digital_cdc_rsc_mgr_hw_vote_enable( |
| 383 | swrm->lpass_core_hw_vote); |
Aditya Bavanari | eb04461 | 2019-12-22 17:14:15 +0530 | [diff] [blame] | 384 | if (ret < 0) { |
| 385 | dev_err(swrm->dev, |
| 386 | "%s:lpass core hw enable failed\n", |
| 387 | __func__); |
| 388 | --swrm->hw_core_clk_en; |
| 389 | } |
| 390 | } |
| 391 | } else { |
| 392 | --swrm->hw_core_clk_en; |
| 393 | if (swrm->hw_core_clk_en < 0) |
| 394 | swrm->hw_core_clk_en = 0; |
| 395 | else if (swrm->hw_core_clk_en == 0) |
Aditya Bavanari | bb981b7 | 2020-03-19 18:30:10 +0530 | [diff] [blame] | 396 | digital_cdc_rsc_mgr_hw_vote_disable( |
| 397 | swrm->lpass_core_hw_vote); |
Aditya Bavanari | eb04461 | 2019-12-22 17:14:15 +0530 | [diff] [blame] | 398 | } |
Sudheer Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | if (core_type == LPASS_AUDIO_CORE) { |
| 402 | if (swrm->lpass_core_audio) { |
| 403 | if (enable) { |
Aditya Bavanari | eb04461 | 2019-12-22 17:14:15 +0530 | [diff] [blame] | 404 | if (!swrm->dev_up) { |
| 405 | dev_dbg(swrm->dev, "%s: device is down or SSR state\n", |
| 406 | __func__); |
| 407 | trace_printk("%s: device is down or SSR state\n", |
| 408 | __func__); |
| 409 | mutex_unlock(&swrm->devlock); |
| 410 | return -ENODEV; |
| 411 | } |
| 412 | if (++swrm->aud_core_clk_en == 1) { |
| 413 | ret = |
Aditya Bavanari | bb981b7 | 2020-03-19 18:30:10 +0530 | [diff] [blame] | 414 | digital_cdc_rsc_mgr_hw_vote_enable( |
| 415 | swrm->lpass_core_audio); |
Aditya Bavanari | eb04461 | 2019-12-22 17:14:15 +0530 | [diff] [blame] | 416 | if (ret < 0) { |
| 417 | dev_err(swrm->dev, |
| 418 | "%s:lpass audio hw enable failed\n", |
| 419 | __func__); |
| 420 | --swrm->aud_core_clk_en; |
| 421 | } |
| 422 | } |
| 423 | } else { |
| 424 | --swrm->aud_core_clk_en; |
| 425 | if (swrm->aud_core_clk_en < 0) |
| 426 | swrm->aud_core_clk_en = 0; |
| 427 | else if (swrm->aud_core_clk_en == 0) |
Aditya Bavanari | bb981b7 | 2020-03-19 18:30:10 +0530 | [diff] [blame] | 428 | digital_cdc_rsc_mgr_hw_vote_disable( |
| 429 | swrm->lpass_core_audio); |
Aditya Bavanari | eb04461 | 2019-12-22 17:14:15 +0530 | [diff] [blame] | 430 | } |
Sudheer Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
Aditya Bavanari | eb04461 | 2019-12-22 17:14:15 +0530 | [diff] [blame] | 434 | mutex_unlock(&swrm->devlock); |
| 435 | dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n", |
| 436 | __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en); |
| 437 | trace_printk("%s: hw_clk_en: %d audio_core_clk_en: %d\n", |
| 438 | __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en); |
Sudheer Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 439 | return ret; |
| 440 | } |
| 441 | |
Sudheer Papothi | ac0ae1c | 2019-10-17 05:38:40 +0530 | [diff] [blame] | 442 | static int swrm_get_ssp_period(struct swr_mstr_ctrl *swrm, |
| 443 | int row, int col, |
| 444 | int frame_sync) |
| 445 | { |
| 446 | if (!swrm || !row || !col || !frame_sync) |
| 447 | return 1; |
| 448 | |
| 449 | return ((swrm->bus_clk * 2) / ((row * col) * frame_sync)); |
| 450 | } |
| 451 | |
Sudheer Papothi | 921b865 | 2019-10-03 02:13:32 +0530 | [diff] [blame] | 452 | static int swrm_core_vote_request(struct swr_mstr_ctrl *swrm) |
| 453 | { |
| 454 | int ret = 0; |
| 455 | |
| 456 | if (!swrm->handle) |
| 457 | return -EINVAL; |
| 458 | |
| 459 | mutex_lock(&swrm->clklock); |
| 460 | if (!swrm->dev_up) { |
| 461 | ret = -ENODEV; |
| 462 | goto exit; |
| 463 | } |
| 464 | if (swrm->core_vote) { |
| 465 | ret = swrm->core_vote(swrm->handle, true); |
| 466 | if (ret) |
| 467 | dev_err_ratelimited(swrm->dev, |
| 468 | "%s: core vote request failed\n", __func__); |
| 469 | } |
| 470 | exit: |
| 471 | mutex_unlock(&swrm->clklock); |
| 472 | |
| 473 | return ret; |
| 474 | } |
| 475 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 476 | static int swrm_clk_request(struct swr_mstr_ctrl *swrm, bool enable) |
| 477 | { |
Ramprasad Katkam | 6bce2e7 | 2018-10-10 19:20:13 +0530 | [diff] [blame] | 478 | int ret = 0; |
| 479 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 480 | if (!swrm->clk || !swrm->handle) |
| 481 | return -EINVAL; |
| 482 | |
Ramprasad Katkam | 6bce2e7 | 2018-10-10 19:20:13 +0530 | [diff] [blame] | 483 | mutex_lock(&swrm->clklock); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 484 | if (enable) { |
Aditya Bavanari | f4a471d | 2019-02-19 17:57:12 +0530 | [diff] [blame] | 485 | if (!swrm->dev_up) { |
| 486 | ret = -ENODEV; |
Ramprasad Katkam | 6bce2e7 | 2018-10-10 19:20:13 +0530 | [diff] [blame] | 487 | goto exit; |
Aditya Bavanari | f4a471d | 2019-02-19 17:57:12 +0530 | [diff] [blame] | 488 | } |
Sudheer Papothi | 921b865 | 2019-10-03 02:13:32 +0530 | [diff] [blame] | 489 | if (is_swr_clk_needed(swrm)) { |
| 490 | if (swrm->core_vote) { |
| 491 | ret = swrm->core_vote(swrm->handle, true); |
| 492 | if (ret) { |
| 493 | dev_err_ratelimited(swrm->dev, |
| 494 | "%s: core vote request failed\n", |
| 495 | __func__); |
| 496 | goto exit; |
| 497 | } |
Karthikeyan Mani | 1d750fe | 2019-09-06 14:36:09 -0700 | [diff] [blame] | 498 | } |
| 499 | } |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 500 | swrm->clk_ref_count++; |
| 501 | if (swrm->clk_ref_count == 1) { |
Aditya Bavanari | f500a1d | 2019-09-16 18:27:51 -0700 | [diff] [blame] | 502 | trace_printk("%s: clock enable count %d", |
| 503 | __func__, swrm->clk_ref_count); |
Ramprasad Katkam | 6bce2e7 | 2018-10-10 19:20:13 +0530 | [diff] [blame] | 504 | ret = swrm->clk(swrm->handle, true); |
| 505 | if (ret) { |
Ramprasad Katkam | 14efed6 | 2019-03-07 13:16:50 +0530 | [diff] [blame] | 506 | dev_err_ratelimited(swrm->dev, |
Ramprasad Katkam | 6bce2e7 | 2018-10-10 19:20:13 +0530 | [diff] [blame] | 507 | "%s: clock enable req failed", |
| 508 | __func__); |
| 509 | --swrm->clk_ref_count; |
| 510 | } |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 511 | } |
| 512 | } else if (--swrm->clk_ref_count == 0) { |
Aditya Bavanari | f500a1d | 2019-09-16 18:27:51 -0700 | [diff] [blame] | 513 | trace_printk("%s: clock disable count %d", |
| 514 | __func__, swrm->clk_ref_count); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 515 | swrm->clk(swrm->handle, false); |
Ramprasad Katkam | 6bce2e7 | 2018-10-10 19:20:13 +0530 | [diff] [blame] | 516 | complete(&swrm->clk_off_complete); |
| 517 | } |
| 518 | if (swrm->clk_ref_count < 0) { |
Meng Wang | 8c60bb5 | 2019-06-19 15:49:06 +0800 | [diff] [blame] | 519 | dev_err(swrm->dev, "%s: swrm clk count mismatch\n", __func__); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 520 | swrm->clk_ref_count = 0; |
| 521 | } |
Ramprasad Katkam | 6bce2e7 | 2018-10-10 19:20:13 +0530 | [diff] [blame] | 522 | |
| 523 | exit: |
| 524 | mutex_unlock(&swrm->clklock); |
| 525 | return ret; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | static int swrm_ahb_write(struct swr_mstr_ctrl *swrm, |
| 529 | u16 reg, u32 *value) |
| 530 | { |
Laxminath Kasam | fbcaf32 | 2018-07-18 00:38:14 +0530 | [diff] [blame] | 531 | u32 temp = (u32)(*value); |
Laxminath Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 532 | int ret = 0; |
| 533 | |
| 534 | mutex_lock(&swrm->devlock); |
| 535 | if (!swrm->dev_up) |
| 536 | goto err; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 537 | |
Sudheer Papothi | 921b865 | 2019-10-03 02:13:32 +0530 | [diff] [blame] | 538 | if (is_swr_clk_needed(swrm)) { |
| 539 | ret = swrm_clk_request(swrm, TRUE); |
| 540 | if (ret) { |
| 541 | dev_err_ratelimited(swrm->dev, |
| 542 | "%s: clock request failed\n", |
| 543 | __func__); |
| 544 | goto err; |
| 545 | } |
| 546 | } else if (swrm_core_vote_request(swrm)) { |
Laxminath Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 547 | goto err; |
| 548 | } |
Sudheer Papothi | 921b865 | 2019-10-03 02:13:32 +0530 | [diff] [blame] | 549 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 550 | iowrite32(temp, swrm->swrm_dig_base + reg); |
Sudheer Papothi | 921b865 | 2019-10-03 02:13:32 +0530 | [diff] [blame] | 551 | if (is_swr_clk_needed(swrm)) |
| 552 | swrm_clk_request(swrm, FALSE); |
Laxminath Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 553 | err: |
| 554 | mutex_unlock(&swrm->devlock); |
| 555 | return ret; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | static int swrm_ahb_read(struct swr_mstr_ctrl *swrm, |
| 559 | u16 reg, u32 *value) |
| 560 | { |
Laxminath Kasam | fbcaf32 | 2018-07-18 00:38:14 +0530 | [diff] [blame] | 561 | u32 temp = 0; |
Laxminath Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 562 | int ret = 0; |
| 563 | |
| 564 | mutex_lock(&swrm->devlock); |
| 565 | if (!swrm->dev_up) |
| 566 | goto err; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 567 | |
Sudheer Papothi | 921b865 | 2019-10-03 02:13:32 +0530 | [diff] [blame] | 568 | if (is_swr_clk_needed(swrm)) { |
| 569 | ret = swrm_clk_request(swrm, TRUE); |
| 570 | if (ret) { |
| 571 | dev_err_ratelimited(swrm->dev, "%s: clock request failed\n", |
| 572 | __func__); |
| 573 | goto err; |
| 574 | } |
| 575 | } else if (swrm_core_vote_request(swrm)) { |
Laxminath Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 576 | goto err; |
| 577 | } |
Sudheer Papothi | 921b865 | 2019-10-03 02:13:32 +0530 | [diff] [blame] | 578 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 579 | temp = ioread32(swrm->swrm_dig_base + reg); |
Laxminath Kasam | fbcaf32 | 2018-07-18 00:38:14 +0530 | [diff] [blame] | 580 | *value = temp; |
Sudheer Papothi | 921b865 | 2019-10-03 02:13:32 +0530 | [diff] [blame] | 581 | if (is_swr_clk_needed(swrm)) |
| 582 | swrm_clk_request(swrm, FALSE); |
Laxminath Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 583 | err: |
| 584 | mutex_unlock(&swrm->devlock); |
| 585 | return ret; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr) |
| 589 | { |
| 590 | u32 val = 0; |
| 591 | |
| 592 | if (swrm->read) |
| 593 | val = swrm->read(swrm->handle, reg_addr); |
| 594 | else |
| 595 | swrm_ahb_read(swrm, reg_addr, &val); |
| 596 | return val; |
| 597 | } |
| 598 | |
| 599 | static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val) |
| 600 | { |
| 601 | if (swrm->write) |
| 602 | swrm->write(swrm->handle, reg_addr, val); |
| 603 | else |
| 604 | swrm_ahb_write(swrm, reg_addr, &val); |
| 605 | } |
| 606 | |
| 607 | static int swr_master_bulk_write(struct swr_mstr_ctrl *swrm, u32 *reg_addr, |
| 608 | u32 *val, unsigned int length) |
| 609 | { |
| 610 | int i = 0; |
| 611 | |
| 612 | if (swrm->bulk_write) |
| 613 | swrm->bulk_write(swrm->handle, reg_addr, val, length); |
| 614 | else { |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 615 | mutex_lock(&swrm->iolock); |
Laxminath Kasam | fbcaf32 | 2018-07-18 00:38:14 +0530 | [diff] [blame] | 616 | for (i = 0; i < length; i++) { |
| 617 | /* wait for FIFO WR command to complete to avoid overflow */ |
Karthikeyan Mani | 5d52dd8 | 2019-08-15 16:58:08 -0700 | [diff] [blame] | 618 | /* |
Vatsal Bucha | dc52af1 | 2020-03-16 23:54:29 +0530 | [diff] [blame] | 619 | * Reduce sleep from 100us to 50us to meet KPIs |
Karthikeyan Mani | 5d52dd8 | 2019-08-15 16:58:08 -0700 | [diff] [blame] | 620 | * This still meets the hardware spec |
| 621 | */ |
Vatsal Bucha | dc52af1 | 2020-03-16 23:54:29 +0530 | [diff] [blame] | 622 | usleep_range(50, 55); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 623 | swr_master_write(swrm, reg_addr[i], val[i]); |
Laxminath Kasam | fbcaf32 | 2018-07-18 00:38:14 +0530 | [diff] [blame] | 624 | } |
Vatsal Bucha | e44b8fb | 2020-06-25 11:43:18 +0530 | [diff] [blame^] | 625 | usleep_range(100, 110); |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 626 | mutex_unlock(&swrm->iolock); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 627 | } |
| 628 | return 0; |
| 629 | } |
| 630 | |
Laxminath Kasam | e229197 | 2019-11-08 14:51:59 +0530 | [diff] [blame] | 631 | static bool swrm_check_link_status(struct swr_mstr_ctrl *swrm, bool active) |
| 632 | { |
| 633 | int retry = SWRM_LINK_STATUS_RETRY_CNT; |
| 634 | int ret = false; |
| 635 | int status = active ? 0x1 : 0x0; |
Laxminath Kasam | 09819e9 | 2019-11-18 14:38:11 +0530 | [diff] [blame] | 636 | int comp_sts = 0x0; |
Laxminath Kasam | e229197 | 2019-11-08 14:51:59 +0530 | [diff] [blame] | 637 | |
| 638 | if ((swrm->version <= SWRM_VERSION_1_5_1)) |
| 639 | return true; |
| 640 | |
| 641 | do { |
Laxminath Kasam | 09819e9 | 2019-11-18 14:38:11 +0530 | [diff] [blame] | 642 | comp_sts = swr_master_read(swrm, SWRM_COMP_STATUS) & 0x01; |
| 643 | /* check comp status and status requested met */ |
| 644 | if ((comp_sts && status) || (!comp_sts && !status)) { |
Laxminath Kasam | e229197 | 2019-11-08 14:51:59 +0530 | [diff] [blame] | 645 | ret = true; |
| 646 | break; |
| 647 | } |
| 648 | retry--; |
| 649 | usleep_range(500, 510); |
| 650 | } while (retry); |
| 651 | |
| 652 | if (retry == 0) |
| 653 | dev_err(swrm->dev, "%s: link status not %s\n", __func__, |
| 654 | active ? "connected" : "disconnected"); |
| 655 | |
| 656 | return ret; |
| 657 | } |
| 658 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 659 | static bool swrm_is_port_en(struct swr_master *mstr) |
| 660 | { |
| 661 | return !!(mstr->num_port); |
| 662 | } |
| 663 | |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 664 | static void copy_port_tables(struct swr_mstr_ctrl *swrm, |
| 665 | struct port_params *params) |
| 666 | { |
| 667 | u8 i; |
| 668 | struct port_params *config = params; |
| 669 | |
| 670 | for (i = 0; i < SWR_MSTR_PORT_LEN; i++) { |
| 671 | /* wsa uses single frame structure for all configurations */ |
| 672 | if (!swrm->mport_cfg[i].port_en) |
| 673 | continue; |
| 674 | swrm->mport_cfg[i].sinterval = config[i].si; |
| 675 | swrm->mport_cfg[i].offset1 = config[i].off1; |
| 676 | swrm->mport_cfg[i].offset2 = config[i].off2; |
| 677 | swrm->mport_cfg[i].hstart = config[i].hstart; |
| 678 | swrm->mport_cfg[i].hstop = config[i].hstop; |
| 679 | swrm->mport_cfg[i].blk_pack_mode = config[i].bp_mode; |
| 680 | swrm->mport_cfg[i].blk_grp_count = config[i].bgp_ctrl; |
| 681 | swrm->mport_cfg[i].word_length = config[i].wd_len; |
| 682 | swrm->mport_cfg[i].lane_ctrl = config[i].lane_ctrl; |
| 683 | } |
| 684 | } |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 685 | static int swrm_get_port_config(struct swr_mstr_ctrl *swrm) |
| 686 | { |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 687 | struct port_params *params; |
Sudheer Papothi | 4c322b1 | 2018-10-31 06:34:01 +0530 | [diff] [blame] | 688 | u32 usecase = 0; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 689 | |
Sudheer Papothi | 4c322b1 | 2018-10-31 06:34:01 +0530 | [diff] [blame] | 690 | /* TODO - Send usecase information to avoid checking for master_id */ |
| 691 | if (swrm->mport_cfg[SWRM_DSD_PARAMS_PORT].port_en && |
| 692 | (swrm->master_id == MASTER_ID_RX)) |
| 693 | usecase = 1; |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 694 | |
Sudheer Papothi | 4c322b1 | 2018-10-31 06:34:01 +0530 | [diff] [blame] | 695 | params = swrm->port_param[usecase]; |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 696 | copy_port_tables(swrm, params); |
Sudheer Papothi | 4c322b1 | 2018-10-31 06:34:01 +0530 | [diff] [blame] | 697 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | static int swrm_get_master_port(struct swr_mstr_ctrl *swrm, u8 *mstr_port_id, |
| 702 | u8 *mstr_ch_mask, u8 mstr_prt_type, |
| 703 | u8 slv_port_id) |
| 704 | { |
| 705 | int i, j; |
| 706 | *mstr_port_id = 0; |
| 707 | |
| 708 | for (i = 1; i <= swrm->num_ports; i++) { |
| 709 | for (j = 0; j < SWR_MAX_CH_PER_PORT; j++) { |
| 710 | if (swrm->port_mapping[i][j].port_type == mstr_prt_type) |
| 711 | goto found; |
| 712 | } |
| 713 | } |
| 714 | found: |
| 715 | if (i > swrm->num_ports || j == SWR_MAX_CH_PER_PORT) { |
| 716 | dev_err(swrm->dev, "%s: port type not supported by master\n", |
| 717 | __func__); |
| 718 | return -EINVAL; |
| 719 | } |
| 720 | /* id 0 corresponds to master port 1 */ |
| 721 | *mstr_port_id = i - 1; |
| 722 | *mstr_ch_mask = swrm->port_mapping[i][j].ch_mask; |
| 723 | |
| 724 | return 0; |
| 725 | |
| 726 | } |
| 727 | |
| 728 | static u32 swrm_get_packed_reg_val(u8 *cmd_id, u8 cmd_data, |
| 729 | u8 dev_addr, u16 reg_addr) |
| 730 | { |
| 731 | u32 val; |
| 732 | u8 id = *cmd_id; |
| 733 | |
| 734 | if (id != SWR_BROADCAST_CMD_ID) { |
| 735 | if (id < 14) |
| 736 | id += 1; |
| 737 | else |
| 738 | id = 0; |
| 739 | *cmd_id = id; |
| 740 | } |
| 741 | val = SWR_REG_VAL_PACK(cmd_data, dev_addr, id, reg_addr); |
| 742 | |
| 743 | return val; |
| 744 | } |
| 745 | |
| 746 | static int swrm_cmd_fifo_rd_cmd(struct swr_mstr_ctrl *swrm, int *cmd_data, |
| 747 | u8 dev_addr, u8 cmd_id, u16 reg_addr, |
| 748 | u32 len) |
| 749 | { |
| 750 | u32 val; |
Laxminath Kasam | fbcaf32 | 2018-07-18 00:38:14 +0530 | [diff] [blame] | 751 | u32 retry_attempt = 0; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 752 | |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 753 | mutex_lock(&swrm->iolock); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 754 | 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] | 755 | if (swrm->read) { |
| 756 | /* skip delay if read is handled in platform driver */ |
| 757 | swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val); |
| 758 | } else { |
| 759 | /* wait for FIFO RD to complete to avoid overflow */ |
| 760 | usleep_range(100, 105); |
| 761 | swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val); |
| 762 | /* wait for FIFO RD CMD complete to avoid overflow */ |
| 763 | usleep_range(250, 255); |
| 764 | } |
Laxminath Kasam | fbcaf32 | 2018-07-18 00:38:14 +0530 | [diff] [blame] | 765 | retry_read: |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 766 | *cmd_data = swr_master_read(swrm, SWRM_CMD_FIFO_RD_FIFO_ADDR); |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 767 | dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, rcmd_id: 0x%x, \ |
| 768 | dev_num: 0x%x, cmd_data: 0x%x\n", __func__, reg_addr, |
| 769 | cmd_id, swrm->rcmd_id, dev_addr, *cmd_data); |
Laxminath Kasam | fbcaf32 | 2018-07-18 00:38:14 +0530 | [diff] [blame] | 770 | if ((((*cmd_data) & 0xF00) >> 8) != swrm->rcmd_id) { |
| 771 | if (retry_attempt < MAX_FIFO_RD_FAIL_RETRY) { |
| 772 | /* wait 500 us before retry on fifo read failure */ |
| 773 | usleep_range(500, 505); |
Sudheer Papothi | ac0ae1c | 2019-10-17 05:38:40 +0530 | [diff] [blame] | 774 | if (retry_attempt == (MAX_FIFO_RD_FAIL_RETRY - 1)) { |
| 775 | swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1); |
| 776 | swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val); |
| 777 | } |
Laxminath Kasam | fbcaf32 | 2018-07-18 00:38:14 +0530 | [diff] [blame] | 778 | retry_attempt++; |
| 779 | goto retry_read; |
| 780 | } else { |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 781 | dev_err_ratelimited(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, \ |
| 782 | rcmd_id: 0x%x, dev_num: 0x%x, cmd_data: 0x%x\n", |
| 783 | __func__, reg_addr, cmd_id, swrm->rcmd_id, |
| 784 | dev_addr, *cmd_data); |
| 785 | |
Laxminath Kasam | fbcaf32 | 2018-07-18 00:38:14 +0530 | [diff] [blame] | 786 | dev_err_ratelimited(swrm->dev, |
| 787 | "%s: failed to read fifo\n", __func__); |
| 788 | } |
| 789 | } |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 790 | mutex_unlock(&swrm->iolock); |
| 791 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 792 | return 0; |
| 793 | } |
| 794 | |
| 795 | static int swrm_cmd_fifo_wr_cmd(struct swr_mstr_ctrl *swrm, u8 cmd_data, |
| 796 | u8 dev_addr, u8 cmd_id, u16 reg_addr) |
| 797 | { |
| 798 | u32 val; |
| 799 | int ret = 0; |
| 800 | |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 801 | mutex_lock(&swrm->iolock); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 802 | if (!cmd_id) |
| 803 | val = swrm_get_packed_reg_val(&swrm->wcmd_id, cmd_data, |
| 804 | dev_addr, reg_addr); |
| 805 | else |
| 806 | val = swrm_get_packed_reg_val(&cmd_id, cmd_data, |
| 807 | dev_addr, reg_addr); |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 808 | dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x,wcmd_id: 0x%x, \ |
| 809 | dev_num: 0x%x, cmd_data: 0x%x\n", __func__, |
| 810 | reg_addr, cmd_id, swrm->wcmd_id,dev_addr, cmd_data); |
Ramprasad Katkam | b4c7c68 | 2018-12-19 18:58:36 +0530 | [diff] [blame] | 811 | swr_master_write(swrm, SWRM_CMD_FIFO_WR_CMD, val); |
Ramprasad Katkam | 1e90620 | 2019-01-30 14:16:34 +0530 | [diff] [blame] | 812 | /* |
| 813 | * wait for FIFO WR command to complete to avoid overflow |
| 814 | * skip delay if write is handled in platform driver. |
| 815 | */ |
| 816 | if(!swrm->write) |
Karthikeyan Mani | 5d52dd8 | 2019-08-15 16:58:08 -0700 | [diff] [blame] | 817 | usleep_range(150, 155); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 818 | if (cmd_id == 0xF) { |
| 819 | /* |
| 820 | * sleep for 10ms for MSM soundwire variant to allow broadcast |
| 821 | * command to complete. |
| 822 | */ |
| 823 | if (swrm_is_msm_variant(swrm->version)) |
| 824 | usleep_range(10000, 10100); |
| 825 | else |
| 826 | wait_for_completion_timeout(&swrm->broadcast, |
| 827 | (2 * HZ/10)); |
| 828 | } |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 829 | mutex_unlock(&swrm->iolock); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 830 | return ret; |
| 831 | } |
| 832 | |
| 833 | static int swrm_read(struct swr_master *master, u8 dev_num, u16 reg_addr, |
| 834 | void *buf, u32 len) |
| 835 | { |
| 836 | struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master); |
| 837 | int ret = 0; |
| 838 | int val; |
| 839 | u8 *reg_val = (u8 *)buf; |
| 840 | |
| 841 | if (!swrm) { |
| 842 | dev_err(&master->dev, "%s: swrm is NULL\n", __func__); |
| 843 | return -EINVAL; |
| 844 | } |
Ramprasad Katkam | 0db4801 | 2018-11-09 11:01:23 +0530 | [diff] [blame] | 845 | if (!dev_num) { |
| 846 | dev_err(&master->dev, "%s: invalid slave dev num\n", __func__); |
| 847 | return -EINVAL; |
| 848 | } |
Laxminath Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 849 | mutex_lock(&swrm->devlock); |
| 850 | if (!swrm->dev_up) { |
| 851 | mutex_unlock(&swrm->devlock); |
| 852 | return 0; |
| 853 | } |
| 854 | mutex_unlock(&swrm->devlock); |
| 855 | |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 856 | pm_runtime_get_sync(swrm->dev); |
Vatsal Bucha | bd68a7c | 2020-06-08 10:41:56 +0530 | [diff] [blame] | 857 | if (swrm->req_clk_switch) |
| 858 | swrm_runtime_resume(swrm->dev); |
Ramprasad Katkam | 0db4801 | 2018-11-09 11:01:23 +0530 | [diff] [blame] | 859 | 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] | 860 | |
| 861 | if (!ret) |
| 862 | *reg_val = (u8)val; |
| 863 | |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 864 | pm_runtime_put_autosuspend(swrm->dev); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 865 | pm_runtime_mark_last_busy(swrm->dev); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 866 | return ret; |
| 867 | } |
| 868 | |
| 869 | static int swrm_write(struct swr_master *master, u8 dev_num, u16 reg_addr, |
| 870 | const void *buf) |
| 871 | { |
| 872 | struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master); |
| 873 | int ret = 0; |
| 874 | u8 reg_val = *(u8 *)buf; |
| 875 | |
| 876 | if (!swrm) { |
| 877 | dev_err(&master->dev, "%s: swrm is NULL\n", __func__); |
| 878 | return -EINVAL; |
| 879 | } |
Ramprasad Katkam | 0db4801 | 2018-11-09 11:01:23 +0530 | [diff] [blame] | 880 | if (!dev_num) { |
| 881 | dev_err(&master->dev, "%s: invalid slave dev num\n", __func__); |
| 882 | return -EINVAL; |
| 883 | } |
Laxminath Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 884 | mutex_lock(&swrm->devlock); |
| 885 | if (!swrm->dev_up) { |
| 886 | mutex_unlock(&swrm->devlock); |
| 887 | return 0; |
| 888 | } |
| 889 | mutex_unlock(&swrm->devlock); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 890 | |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 891 | pm_runtime_get_sync(swrm->dev); |
Vatsal Bucha | bd68a7c | 2020-06-08 10:41:56 +0530 | [diff] [blame] | 892 | if (swrm->req_clk_switch) |
| 893 | swrm_runtime_resume(swrm->dev); |
Ramprasad Katkam | 0db4801 | 2018-11-09 11:01:23 +0530 | [diff] [blame] | 894 | 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] | 895 | |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 896 | pm_runtime_put_autosuspend(swrm->dev); |
| 897 | pm_runtime_mark_last_busy(swrm->dev); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 898 | return ret; |
| 899 | } |
| 900 | |
| 901 | static int swrm_bulk_write(struct swr_master *master, u8 dev_num, void *reg, |
| 902 | const void *buf, size_t len) |
| 903 | { |
| 904 | struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master); |
| 905 | int ret = 0; |
| 906 | int i; |
| 907 | u32 *val; |
| 908 | u32 *swr_fifo_reg; |
| 909 | |
| 910 | if (!swrm || !swrm->handle) { |
| 911 | dev_err(&master->dev, "%s: swrm is NULL\n", __func__); |
| 912 | return -EINVAL; |
| 913 | } |
| 914 | if (len <= 0) |
| 915 | return -EINVAL; |
Laxminath Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 916 | mutex_lock(&swrm->devlock); |
| 917 | if (!swrm->dev_up) { |
| 918 | mutex_unlock(&swrm->devlock); |
| 919 | return 0; |
| 920 | } |
| 921 | mutex_unlock(&swrm->devlock); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 922 | |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 923 | pm_runtime_get_sync(swrm->dev); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 924 | if (dev_num) { |
| 925 | swr_fifo_reg = kcalloc(len, sizeof(u32), GFP_KERNEL); |
| 926 | if (!swr_fifo_reg) { |
| 927 | ret = -ENOMEM; |
| 928 | goto err; |
| 929 | } |
| 930 | val = kcalloc(len, sizeof(u32), GFP_KERNEL); |
| 931 | if (!val) { |
| 932 | ret = -ENOMEM; |
| 933 | goto mem_fail; |
| 934 | } |
| 935 | |
| 936 | for (i = 0; i < len; i++) { |
| 937 | val[i] = swrm_get_packed_reg_val(&swrm->wcmd_id, |
| 938 | ((u8 *)buf)[i], |
| 939 | dev_num, |
| 940 | ((u16 *)reg)[i]); |
| 941 | swr_fifo_reg[i] = SWRM_CMD_FIFO_WR_CMD; |
| 942 | } |
| 943 | ret = swr_master_bulk_write(swrm, swr_fifo_reg, val, len); |
| 944 | if (ret) { |
| 945 | dev_err(&master->dev, "%s: bulk write failed\n", |
| 946 | __func__); |
| 947 | ret = -EINVAL; |
| 948 | } |
| 949 | } else { |
| 950 | dev_err(&master->dev, |
| 951 | "%s: No support of Bulk write for master regs\n", |
| 952 | __func__); |
| 953 | ret = -EINVAL; |
| 954 | goto err; |
| 955 | } |
| 956 | kfree(val); |
| 957 | mem_fail: |
| 958 | kfree(swr_fifo_reg); |
| 959 | err: |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 960 | pm_runtime_put_autosuspend(swrm->dev); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 961 | pm_runtime_mark_last_busy(swrm->dev); |
| 962 | return ret; |
| 963 | } |
| 964 | |
| 965 | static u8 get_inactive_bank_num(struct swr_mstr_ctrl *swrm) |
| 966 | { |
| 967 | return (swr_master_read(swrm, SWRM_MCP_STATUS) & |
| 968 | SWRM_MCP_STATUS_BANK_NUM_MASK) ? 0 : 1; |
| 969 | } |
| 970 | |
| 971 | static void enable_bank_switch(struct swr_mstr_ctrl *swrm, u8 bank, |
| 972 | u8 row, u8 col) |
| 973 | { |
| 974 | swrm_cmd_fifo_wr_cmd(swrm, ((row << 3) | col), 0xF, 0xF, |
| 975 | SWRS_SCP_FRAME_CTRL_BANK(bank)); |
| 976 | } |
| 977 | |
Sudheer Papothi | 8a8b12b | 2019-11-15 23:06:41 +0530 | [diff] [blame] | 978 | static void swrm_switch_frame_shape(struct swr_mstr_ctrl *swrm, int mclk_freq) |
| 979 | { |
| 980 | u8 bank; |
| 981 | u32 n_row, n_col; |
| 982 | u32 value = 0; |
| 983 | u32 row = 0, col = 0; |
| 984 | u8 ssp_period = 0; |
| 985 | int frame_sync = SWRM_FRAME_SYNC_SEL; |
| 986 | |
| 987 | if (mclk_freq == MCLK_FREQ_NATIVE) { |
| 988 | n_col = SWR_MAX_COL; |
| 989 | col = SWRM_COL_16; |
| 990 | n_row = SWR_ROW_64; |
| 991 | row = SWRM_ROW_64; |
| 992 | frame_sync = SWRM_FRAME_SYNC_SEL_NATIVE; |
| 993 | } else { |
| 994 | n_col = SWR_MIN_COL; |
| 995 | col = SWRM_COL_02; |
| 996 | n_row = SWR_ROW_50; |
| 997 | row = SWRM_ROW_50; |
| 998 | frame_sync = SWRM_FRAME_SYNC_SEL; |
| 999 | } |
| 1000 | |
| 1001 | bank = get_inactive_bank_num(swrm); |
| 1002 | ssp_period = swrm_get_ssp_period(swrm, row, col, frame_sync); |
| 1003 | dev_dbg(swrm->dev, "%s: ssp_period: %d\n", __func__, ssp_period); |
| 1004 | value = ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) | |
| 1005 | (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) | |
| 1006 | ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT)); |
| 1007 | swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value); |
| 1008 | enable_bank_switch(swrm, bank, n_row, n_col); |
| 1009 | } |
| 1010 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1011 | static struct swr_port_info *swrm_get_port_req(struct swrm_mports *mport, |
| 1012 | u8 slv_port, u8 dev_num) |
| 1013 | { |
| 1014 | struct swr_port_info *port_req = NULL; |
| 1015 | |
| 1016 | list_for_each_entry(port_req, &mport->port_req_list, list) { |
| 1017 | /* Store dev_id instead of dev_num if enumeration is changed run_time */ |
| 1018 | if ((port_req->slave_port_id == slv_port) |
| 1019 | && (port_req->dev_num == dev_num)) |
| 1020 | return port_req; |
| 1021 | } |
| 1022 | return NULL; |
| 1023 | } |
| 1024 | |
| 1025 | static bool swrm_remove_from_group(struct swr_master *master) |
| 1026 | { |
| 1027 | struct swr_device *swr_dev; |
| 1028 | struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master); |
| 1029 | bool is_removed = false; |
| 1030 | |
| 1031 | if (!swrm) |
| 1032 | goto end; |
| 1033 | |
| 1034 | mutex_lock(&swrm->mlock); |
| 1035 | if ((swrm->num_rx_chs > 1) && |
| 1036 | (swrm->num_rx_chs == swrm->num_cfg_devs)) { |
| 1037 | list_for_each_entry(swr_dev, &master->devices, |
| 1038 | dev_list) { |
| 1039 | swr_dev->group_id = SWR_GROUP_NONE; |
| 1040 | master->gr_sid = 0; |
| 1041 | } |
| 1042 | is_removed = true; |
| 1043 | } |
| 1044 | mutex_unlock(&swrm->mlock); |
| 1045 | |
| 1046 | end: |
| 1047 | return is_removed; |
| 1048 | } |
| 1049 | |
Sudheer Papothi | ae5c363 | 2019-11-27 06:52:06 +0530 | [diff] [blame] | 1050 | int swrm_get_clk_div_rate(int mclk_freq, int bus_clk_freq) |
| 1051 | { |
| 1052 | if (!bus_clk_freq) |
| 1053 | return mclk_freq; |
| 1054 | |
| 1055 | if (mclk_freq == SWR_CLK_RATE_9P6MHZ) { |
| 1056 | if (bus_clk_freq <= SWR_CLK_RATE_0P6MHZ) |
| 1057 | bus_clk_freq = SWR_CLK_RATE_0P6MHZ; |
| 1058 | else if (bus_clk_freq <= SWR_CLK_RATE_1P2MHZ) |
| 1059 | bus_clk_freq = SWR_CLK_RATE_1P2MHZ; |
| 1060 | else if (bus_clk_freq <= SWR_CLK_RATE_2P4MHZ) |
| 1061 | bus_clk_freq = SWR_CLK_RATE_2P4MHZ; |
| 1062 | else if(bus_clk_freq <= SWR_CLK_RATE_4P8MHZ) |
| 1063 | bus_clk_freq = SWR_CLK_RATE_4P8MHZ; |
| 1064 | else if(bus_clk_freq <= SWR_CLK_RATE_9P6MHZ) |
| 1065 | bus_clk_freq = SWR_CLK_RATE_9P6MHZ; |
| 1066 | } else if (mclk_freq == SWR_CLK_RATE_11P2896MHZ) |
| 1067 | bus_clk_freq = SWR_CLK_RATE_11P2896MHZ; |
| 1068 | |
| 1069 | return bus_clk_freq; |
| 1070 | } |
| 1071 | |
| 1072 | static int swrm_update_bus_clk(struct swr_mstr_ctrl *swrm) |
| 1073 | { |
| 1074 | int ret = 0; |
| 1075 | int agg_clk = 0; |
| 1076 | int i; |
| 1077 | |
| 1078 | for (i = 0; i < SWR_MSTR_PORT_LEN; i++) |
| 1079 | agg_clk += swrm->mport_cfg[i].ch_rate; |
| 1080 | |
| 1081 | if (agg_clk) |
| 1082 | swrm->bus_clk = swrm_get_clk_div_rate(swrm->mclk_freq, |
| 1083 | agg_clk); |
| 1084 | else |
| 1085 | swrm->bus_clk = swrm->mclk_freq; |
| 1086 | |
| 1087 | dev_dbg(swrm->dev, "%s: all_port_clk: %d, bus_clk: %d\n", |
| 1088 | __func__, agg_clk, swrm->bus_clk); |
| 1089 | |
| 1090 | return ret; |
| 1091 | } |
| 1092 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1093 | static void swrm_disable_ports(struct swr_master *master, |
| 1094 | u8 bank) |
| 1095 | { |
| 1096 | u32 value; |
| 1097 | struct swr_port_info *port_req; |
| 1098 | int i; |
| 1099 | struct swrm_mports *mport; |
| 1100 | struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master); |
| 1101 | |
| 1102 | if (!swrm) { |
| 1103 | pr_err("%s: swrm is null\n", __func__); |
| 1104 | return; |
| 1105 | } |
| 1106 | |
| 1107 | dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__, |
| 1108 | master->num_port); |
| 1109 | |
| 1110 | |
| 1111 | for (i = 0; i < SWR_MSTR_PORT_LEN ; i++) { |
| 1112 | |
| 1113 | mport = &(swrm->mport_cfg[i]); |
| 1114 | if (!mport->port_en) |
| 1115 | continue; |
| 1116 | |
| 1117 | list_for_each_entry(port_req, &mport->port_req_list, list) { |
| 1118 | /* skip ports with no change req's*/ |
| 1119 | if (port_req->req_ch == port_req->ch_en) |
| 1120 | continue; |
| 1121 | |
| 1122 | swrm_cmd_fifo_wr_cmd(swrm, port_req->req_ch, |
| 1123 | port_req->dev_num, 0x00, |
| 1124 | SWRS_DP_CHANNEL_ENABLE_BANK(port_req->slave_port_id, |
| 1125 | bank)); |
| 1126 | dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x\n", |
| 1127 | __func__, i, |
| 1128 | (SWRM_DP_PORT_CTRL_BANK(i + 1, bank))); |
| 1129 | } |
| 1130 | value = ((mport->req_ch) |
| 1131 | << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT); |
| 1132 | value |= ((mport->offset2) |
| 1133 | << SWRM_DP_PORT_CTRL_OFFSET2_SHFT); |
| 1134 | value |= ((mport->offset1) |
| 1135 | << SWRM_DP_PORT_CTRL_OFFSET1_SHFT); |
| 1136 | value |= mport->sinterval; |
| 1137 | |
| 1138 | swr_master_write(swrm, |
| 1139 | SWRM_DP_PORT_CTRL_BANK(i+1, bank), |
| 1140 | value); |
| 1141 | dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n", |
| 1142 | __func__, i, |
| 1143 | (SWRM_DP_PORT_CTRL_BANK(i+1, bank)), value); |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | static void swrm_cleanup_disabled_port_reqs(struct swr_master *master) |
| 1148 | { |
| 1149 | struct swr_port_info *port_req, *next; |
| 1150 | int i; |
| 1151 | struct swrm_mports *mport; |
| 1152 | struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master); |
| 1153 | |
| 1154 | if (!swrm) { |
| 1155 | pr_err("%s: swrm is null\n", __func__); |
| 1156 | return; |
| 1157 | } |
| 1158 | dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__, |
| 1159 | master->num_port); |
| 1160 | |
| 1161 | for (i = 0; i < SWR_MSTR_PORT_LEN; i++) { |
| 1162 | mport = &(swrm->mport_cfg[i]); |
| 1163 | list_for_each_entry_safe(port_req, next, |
| 1164 | &mport->port_req_list, list) { |
| 1165 | /* skip ports without new ch req */ |
| 1166 | if (port_req->ch_en == port_req->req_ch) |
| 1167 | continue; |
| 1168 | |
| 1169 | /* remove new ch req's*/ |
Ramprasad Katkam | c8d52a1 | 2018-08-31 02:30:00 +0530 | [diff] [blame] | 1170 | port_req->ch_en = port_req->req_ch; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1171 | |
| 1172 | /* If no streams enabled on port, remove the port req */ |
| 1173 | if (port_req->ch_en == 0) { |
| 1174 | list_del(&port_req->list); |
| 1175 | kfree(port_req); |
| 1176 | } |
| 1177 | } |
| 1178 | /* remove new ch req's on mport*/ |
Ramprasad Katkam | c8d52a1 | 2018-08-31 02:30:00 +0530 | [diff] [blame] | 1179 | mport->ch_en = mport->req_ch; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1180 | |
| 1181 | if (!(mport->ch_en)) { |
| 1182 | mport->port_en = false; |
| 1183 | master->port_en_mask &= ~i; |
| 1184 | } |
| 1185 | } |
| 1186 | } |
| 1187 | static void swrm_copy_data_port_config(struct swr_master *master, u8 bank) |
| 1188 | { |
| 1189 | u32 value, slv_id; |
| 1190 | struct swr_port_info *port_req; |
| 1191 | int i; |
| 1192 | struct swrm_mports *mport; |
| 1193 | u32 reg[SWRM_MAX_PORT_REG]; |
| 1194 | u32 val[SWRM_MAX_PORT_REG]; |
| 1195 | int len = 0; |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 1196 | u8 hparams; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1197 | struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master); |
| 1198 | |
| 1199 | if (!swrm) { |
| 1200 | pr_err("%s: swrm is null\n", __func__); |
| 1201 | return; |
| 1202 | } |
| 1203 | |
| 1204 | dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__, |
| 1205 | master->num_port); |
| 1206 | |
| 1207 | for (i = 0; i < SWR_MSTR_PORT_LEN; i++) { |
| 1208 | mport = &(swrm->mport_cfg[i]); |
| 1209 | if (!mport->port_en) |
| 1210 | continue; |
| 1211 | |
| 1212 | list_for_each_entry(port_req, &mport->port_req_list, list) { |
| 1213 | slv_id = port_req->slave_port_id; |
| 1214 | reg[len] = SWRM_CMD_FIFO_WR_CMD; |
| 1215 | val[len++] = SWR_REG_VAL_PACK(port_req->req_ch, |
| 1216 | port_req->dev_num, 0x00, |
| 1217 | SWRS_DP_CHANNEL_ENABLE_BANK(slv_id, |
| 1218 | bank)); |
| 1219 | |
| 1220 | reg[len] = SWRM_CMD_FIFO_WR_CMD; |
| 1221 | val[len++] = SWR_REG_VAL_PACK(mport->sinterval, |
| 1222 | port_req->dev_num, 0x00, |
| 1223 | SWRS_DP_SAMPLE_CONTROL_1_BANK(slv_id, |
| 1224 | bank)); |
| 1225 | |
| 1226 | reg[len] = SWRM_CMD_FIFO_WR_CMD; |
| 1227 | val[len++] = SWR_REG_VAL_PACK(mport->offset1, |
| 1228 | port_req->dev_num, 0x00, |
| 1229 | SWRS_DP_OFFSET_CONTROL_1_BANK(slv_id, |
| 1230 | bank)); |
| 1231 | |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 1232 | if (mport->offset2 != SWR_INVALID_PARAM) { |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1233 | reg[len] = SWRM_CMD_FIFO_WR_CMD; |
| 1234 | val[len++] = SWR_REG_VAL_PACK(mport->offset2, |
| 1235 | port_req->dev_num, 0x00, |
| 1236 | SWRS_DP_OFFSET_CONTROL_2_BANK( |
| 1237 | slv_id, bank)); |
| 1238 | } |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 1239 | if (mport->hstart != SWR_INVALID_PARAM |
| 1240 | && mport->hstop != SWR_INVALID_PARAM) { |
| 1241 | hparams = (mport->hstart << 4) | mport->hstop; |
| 1242 | |
| 1243 | reg[len] = SWRM_CMD_FIFO_WR_CMD; |
| 1244 | val[len++] = SWR_REG_VAL_PACK(hparams, |
| 1245 | port_req->dev_num, 0x00, |
| 1246 | SWRS_DP_HCONTROL_BANK(slv_id, |
| 1247 | bank)); |
| 1248 | } |
| 1249 | if (mport->word_length != SWR_INVALID_PARAM) { |
| 1250 | reg[len] = SWRM_CMD_FIFO_WR_CMD; |
| 1251 | val[len++] = |
| 1252 | SWR_REG_VAL_PACK(mport->word_length, |
| 1253 | port_req->dev_num, 0x00, |
| 1254 | SWRS_DP_BLOCK_CONTROL_1(slv_id)); |
| 1255 | } |
Ramprasad Katkam | 2a0996b | 2018-09-25 20:13:30 +0530 | [diff] [blame] | 1256 | if (mport->blk_pack_mode != SWR_INVALID_PARAM |
| 1257 | && swrm->master_id != MASTER_ID_WSA) { |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 1258 | reg[len] = SWRM_CMD_FIFO_WR_CMD; |
| 1259 | val[len++] = |
| 1260 | SWR_REG_VAL_PACK(mport->blk_pack_mode, |
| 1261 | port_req->dev_num, 0x00, |
| 1262 | SWRS_DP_BLOCK_CONTROL_3_BANK(slv_id, |
| 1263 | bank)); |
| 1264 | } |
| 1265 | if (mport->blk_grp_count != SWR_INVALID_PARAM) { |
| 1266 | reg[len] = SWRM_CMD_FIFO_WR_CMD; |
| 1267 | val[len++] = |
| 1268 | SWR_REG_VAL_PACK(mport->blk_grp_count, |
| 1269 | port_req->dev_num, 0x00, |
| 1270 | SWRS_DP_BLOCK_CONTROL_2_BANK(slv_id, |
| 1271 | bank)); |
| 1272 | } |
| 1273 | if (mport->lane_ctrl != SWR_INVALID_PARAM) { |
| 1274 | reg[len] = SWRM_CMD_FIFO_WR_CMD; |
| 1275 | val[len++] = |
| 1276 | SWR_REG_VAL_PACK(mport->lane_ctrl, |
| 1277 | port_req->dev_num, 0x00, |
| 1278 | SWRS_DP_LANE_CONTROL_BANK(slv_id, |
| 1279 | bank)); |
| 1280 | } |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1281 | port_req->ch_en = port_req->req_ch; |
| 1282 | } |
| 1283 | value = ((mport->req_ch) |
| 1284 | << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT); |
Ramprasad Katkam | 2a0996b | 2018-09-25 20:13:30 +0530 | [diff] [blame] | 1285 | |
| 1286 | if (mport->offset2 != SWR_INVALID_PARAM) |
| 1287 | value |= ((mport->offset2) |
| 1288 | << SWRM_DP_PORT_CTRL_OFFSET2_SHFT); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1289 | value |= ((mport->offset1) |
| 1290 | << SWRM_DP_PORT_CTRL_OFFSET1_SHFT); |
| 1291 | value |= mport->sinterval; |
| 1292 | |
| 1293 | |
| 1294 | reg[len] = SWRM_DP_PORT_CTRL_BANK(i + 1, bank); |
| 1295 | val[len++] = value; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1296 | dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n", |
| 1297 | __func__, i, |
| 1298 | (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)), value); |
| 1299 | |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 1300 | if (mport->lane_ctrl != SWR_INVALID_PARAM) { |
| 1301 | reg[len] = SWRM_DP_PORT_CTRL_2_BANK(i + 1, bank); |
| 1302 | val[len++] = mport->lane_ctrl; |
| 1303 | } |
| 1304 | if (mport->word_length != SWR_INVALID_PARAM) { |
| 1305 | reg[len] = SWRM_DP_BLOCK_CTRL_1(i + 1); |
| 1306 | val[len++] = mport->word_length; |
| 1307 | } |
| 1308 | |
| 1309 | if (mport->blk_grp_count != SWR_INVALID_PARAM) { |
| 1310 | reg[len] = SWRM_DP_BLOCK_CTRL2_BANK(i + 1, bank); |
| 1311 | val[len++] = mport->blk_grp_count; |
| 1312 | } |
| 1313 | if (mport->hstart != SWR_INVALID_PARAM |
| 1314 | && mport->hstop != SWR_INVALID_PARAM) { |
| 1315 | reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank); |
Laxminath Kasam | e30eef7 | 2018-11-05 17:40:09 +0530 | [diff] [blame] | 1316 | hparams = (mport->hstop << 4) | mport->hstart; |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 1317 | val[len++] = hparams; |
Laxminath Kasam | 990c70b | 2018-11-09 23:15:09 +0530 | [diff] [blame] | 1318 | } else { |
| 1319 | reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank); |
| 1320 | hparams = (SWR_HSTOP_MAX_VAL << 4) | SWR_HSTART_MIN_VAL; |
| 1321 | val[len++] = hparams; |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 1322 | } |
| 1323 | if (mport->blk_pack_mode != SWR_INVALID_PARAM) { |
| 1324 | reg[len] = SWRM_DP_BLOCK_CTRL3_BANK(i + 1, bank); |
| 1325 | val[len++] = mport->blk_pack_mode; |
| 1326 | } |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1327 | mport->ch_en = mport->req_ch; |
| 1328 | |
| 1329 | } |
Sudheer Papothi | 0016db1 | 2019-06-11 04:42:38 +0530 | [diff] [blame] | 1330 | swrm_reg_dump(swrm, reg, val, len, __func__); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1331 | swr_master_bulk_write(swrm, reg, val, len); |
| 1332 | } |
| 1333 | |
| 1334 | static void swrm_apply_port_config(struct swr_master *master) |
| 1335 | { |
| 1336 | u8 bank; |
| 1337 | struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master); |
| 1338 | |
| 1339 | if (!swrm) { |
| 1340 | pr_err("%s: Invalid handle to swr controller\n", |
| 1341 | __func__); |
| 1342 | return; |
| 1343 | } |
| 1344 | |
| 1345 | bank = get_inactive_bank_num(swrm); |
| 1346 | dev_dbg(swrm->dev, "%s: enter bank: %d master_ports: %d\n", |
| 1347 | __func__, bank, master->num_port); |
| 1348 | |
Vatsal Bucha | 687f982 | 2020-04-01 18:21:41 +0530 | [diff] [blame] | 1349 | if (!swrm->disable_div2_clk_switch) |
| 1350 | swrm_cmd_fifo_wr_cmd(swrm, 0x01, 0xF, 0x00, |
| 1351 | SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(bank)); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1352 | |
| 1353 | swrm_copy_data_port_config(master, bank); |
| 1354 | } |
| 1355 | |
| 1356 | static int swrm_slvdev_datapath_control(struct swr_master *master, bool enable) |
| 1357 | { |
| 1358 | u8 bank; |
Sudheer Papothi | ae5c363 | 2019-11-27 06:52:06 +0530 | [diff] [blame] | 1359 | u32 value = 0, n_row = 0, n_col = 0; |
Sudheer Papothi | ac0ae1c | 2019-10-17 05:38:40 +0530 | [diff] [blame] | 1360 | u32 row = 0, col = 0; |
Sudheer Papothi | ae5c363 | 2019-11-27 06:52:06 +0530 | [diff] [blame] | 1361 | int bus_clk_div_factor; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1362 | int ret; |
Sudheer Papothi | ac0ae1c | 2019-10-17 05:38:40 +0530 | [diff] [blame] | 1363 | u8 ssp_period = 0; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1364 | struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master); |
| 1365 | int mask = (SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK | |
| 1366 | SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK | |
Sudheer Papothi | ae5c363 | 2019-11-27 06:52:06 +0530 | [diff] [blame] | 1367 | SWRM_MCP_FRAME_CTRL_BANK_CLK_DIV_VALUE_BMSK | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1368 | SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_BMSK); |
| 1369 | u8 inactive_bank; |
Sudheer Papothi | 8a8b12b | 2019-11-15 23:06:41 +0530 | [diff] [blame] | 1370 | int frame_sync = SWRM_FRAME_SYNC_SEL; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1371 | |
| 1372 | if (!swrm) { |
| 1373 | pr_err("%s: swrm is null\n", __func__); |
| 1374 | return -EFAULT; |
| 1375 | } |
Ramprasad Katkam | 7e35478 | 2018-11-21 15:52:54 +0530 | [diff] [blame] | 1376 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1377 | mutex_lock(&swrm->mlock); |
| 1378 | |
Ramprasad Katkam | 979b7c9 | 2019-05-17 15:31:21 +0530 | [diff] [blame] | 1379 | /* |
| 1380 | * During disable if master is already down, which implies an ssr/pdr |
| 1381 | * scenario, just mark ports as disabled and exit |
| 1382 | */ |
| 1383 | if (swrm->state == SWR_MSTR_SSR && !enable) { |
| 1384 | if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) { |
| 1385 | dev_dbg(swrm->dev, "%s:No pending disconn port req\n", |
| 1386 | __func__); |
| 1387 | goto exit; |
| 1388 | } |
| 1389 | clear_bit(DISABLE_PENDING, &swrm->port_req_pending); |
| 1390 | swrm_cleanup_disabled_port_reqs(master); |
| 1391 | if (!swrm_is_port_en(master)) { |
| 1392 | dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n", |
| 1393 | __func__); |
| 1394 | pm_runtime_mark_last_busy(swrm->dev); |
| 1395 | pm_runtime_put_autosuspend(swrm->dev); |
| 1396 | } |
| 1397 | goto exit; |
| 1398 | } |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1399 | bank = get_inactive_bank_num(swrm); |
| 1400 | |
| 1401 | if (enable) { |
Ramprasad Katkam | cab8d72 | 2018-09-28 15:54:06 +0530 | [diff] [blame] | 1402 | if (!test_bit(ENABLE_PENDING, &swrm->port_req_pending)) { |
| 1403 | dev_dbg(swrm->dev, "%s:No pending connect port req\n", |
| 1404 | __func__); |
| 1405 | goto exit; |
| 1406 | } |
| 1407 | clear_bit(ENABLE_PENDING, &swrm->port_req_pending); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1408 | ret = swrm_get_port_config(swrm); |
| 1409 | if (ret) { |
| 1410 | /* cannot accommodate ports */ |
| 1411 | swrm_cleanup_disabled_port_reqs(master); |
| 1412 | mutex_unlock(&swrm->mlock); |
| 1413 | return -EINVAL; |
| 1414 | } |
Ramprasad Katkam | 18bc8e2 | 2018-10-25 15:04:24 +0530 | [diff] [blame] | 1415 | swr_master_write(swrm, SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, |
Ramprasad Katkam | 7e35478 | 2018-11-21 15:52:54 +0530 | [diff] [blame] | 1416 | SWRM_INTERRUPT_STATUS_MASK); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1417 | /* apply the new port config*/ |
| 1418 | swrm_apply_port_config(master); |
| 1419 | } else { |
Ramprasad Katkam | cab8d72 | 2018-09-28 15:54:06 +0530 | [diff] [blame] | 1420 | if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) { |
| 1421 | dev_dbg(swrm->dev, "%s:No pending disconn port req\n", |
| 1422 | __func__); |
| 1423 | goto exit; |
| 1424 | } |
| 1425 | clear_bit(DISABLE_PENDING, &swrm->port_req_pending); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1426 | swrm_disable_ports(master, bank); |
| 1427 | } |
Sudheer Papothi | ae5c363 | 2019-11-27 06:52:06 +0530 | [diff] [blame] | 1428 | dev_dbg(swrm->dev, "%s: enable: %d, cfg_devs: %d freq %d\n", |
| 1429 | __func__, enable, swrm->num_cfg_devs, swrm->mclk_freq); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1430 | |
| 1431 | if (enable) { |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 1432 | /* set col = 16 */ |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1433 | n_col = SWR_MAX_COL; |
Sudheer Papothi | ac0ae1c | 2019-10-17 05:38:40 +0530 | [diff] [blame] | 1434 | col = SWRM_COL_16; |
Sudheer Papothi | ae5c363 | 2019-11-27 06:52:06 +0530 | [diff] [blame] | 1435 | if (swrm->bus_clk == MCLK_FREQ_LP) { |
| 1436 | n_col = SWR_MIN_COL; |
| 1437 | col = SWRM_COL_02; |
| 1438 | } |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1439 | } else { |
| 1440 | /* |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 1441 | * Do not change to col = 2 if there are still active ports |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1442 | */ |
Sudheer Papothi | ac0ae1c | 2019-10-17 05:38:40 +0530 | [diff] [blame] | 1443 | if (!master->num_port) { |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1444 | n_col = SWR_MIN_COL; |
Sudheer Papothi | ac0ae1c | 2019-10-17 05:38:40 +0530 | [diff] [blame] | 1445 | col = SWRM_COL_02; |
| 1446 | } else { |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1447 | n_col = SWR_MAX_COL; |
Sudheer Papothi | ac0ae1c | 2019-10-17 05:38:40 +0530 | [diff] [blame] | 1448 | col = SWRM_COL_16; |
| 1449 | } |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1450 | } |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 1451 | /* Use default 50 * x, frame shape. Change based on mclk */ |
Laxminath Kasam | b0f27cd | 2018-09-06 12:17:11 +0530 | [diff] [blame] | 1452 | if (swrm->mclk_freq == MCLK_FREQ_NATIVE) { |
Sudheer Papothi | ae5c363 | 2019-11-27 06:52:06 +0530 | [diff] [blame] | 1453 | dev_dbg(swrm->dev, "setting 64 x %d frameshape\n", col); |
Laxminath Kasam | b0f27cd | 2018-09-06 12:17:11 +0530 | [diff] [blame] | 1454 | n_row = SWR_ROW_64; |
Sudheer Papothi | ac0ae1c | 2019-10-17 05:38:40 +0530 | [diff] [blame] | 1455 | row = SWRM_ROW_64; |
Sudheer Papothi | 8a8b12b | 2019-11-15 23:06:41 +0530 | [diff] [blame] | 1456 | frame_sync = SWRM_FRAME_SYNC_SEL_NATIVE; |
Laxminath Kasam | b0f27cd | 2018-09-06 12:17:11 +0530 | [diff] [blame] | 1457 | } else { |
Sudheer Papothi | ae5c363 | 2019-11-27 06:52:06 +0530 | [diff] [blame] | 1458 | dev_dbg(swrm->dev, "setting 50 x %d frameshape\n", col); |
Laxminath Kasam | b0f27cd | 2018-09-06 12:17:11 +0530 | [diff] [blame] | 1459 | n_row = SWR_ROW_50; |
Sudheer Papothi | ac0ae1c | 2019-10-17 05:38:40 +0530 | [diff] [blame] | 1460 | row = SWRM_ROW_50; |
Sudheer Papothi | 8a8b12b | 2019-11-15 23:06:41 +0530 | [diff] [blame] | 1461 | frame_sync = SWRM_FRAME_SYNC_SEL; |
Laxminath Kasam | b0f27cd | 2018-09-06 12:17:11 +0530 | [diff] [blame] | 1462 | } |
Sudheer Papothi | 8a8b12b | 2019-11-15 23:06:41 +0530 | [diff] [blame] | 1463 | ssp_period = swrm_get_ssp_period(swrm, row, col, frame_sync); |
Sudheer Papothi | ae5c363 | 2019-11-27 06:52:06 +0530 | [diff] [blame] | 1464 | bus_clk_div_factor = swrm_get_clk_div(swrm->mclk_freq, swrm->bus_clk); |
| 1465 | dev_dbg(swrm->dev, "%s: ssp_period: %d, bus_clk_div:%d \n", __func__, |
| 1466 | ssp_period, bus_clk_div_factor); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1467 | value = swr_master_read(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank)); |
| 1468 | value &= (~mask); |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 1469 | value |= ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1470 | (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) | |
Sudheer Papothi | ae5c363 | 2019-11-27 06:52:06 +0530 | [diff] [blame] | 1471 | (bus_clk_div_factor << |
| 1472 | SWRM_MCP_FRAME_CTRL_BANK_CLK_DIV_VALUE_SHFT) | |
Sudheer Papothi | ac0ae1c | 2019-10-17 05:38:40 +0530 | [diff] [blame] | 1473 | ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT)); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1474 | swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value); |
| 1475 | |
| 1476 | dev_dbg(swrm->dev, "%s: regaddr: 0x%x, value: 0x%x\n", __func__, |
| 1477 | SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value); |
| 1478 | |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 1479 | enable_bank_switch(swrm, bank, n_row, n_col); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1480 | inactive_bank = bank ? 0 : 1; |
| 1481 | |
| 1482 | if (enable) |
| 1483 | swrm_copy_data_port_config(master, inactive_bank); |
| 1484 | else { |
| 1485 | swrm_disable_ports(master, inactive_bank); |
| 1486 | swrm_cleanup_disabled_port_reqs(master); |
Ramprasad Katkam | 7cb4ff6 | 2018-09-12 04:00:26 +0530 | [diff] [blame] | 1487 | } |
| 1488 | if (!swrm_is_port_en(master)) { |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1489 | dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n", |
| 1490 | __func__); |
| 1491 | pm_runtime_mark_last_busy(swrm->dev); |
| 1492 | pm_runtime_put_autosuspend(swrm->dev); |
| 1493 | } |
Ramprasad Katkam | cab8d72 | 2018-09-28 15:54:06 +0530 | [diff] [blame] | 1494 | exit: |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1495 | mutex_unlock(&swrm->mlock); |
| 1496 | return 0; |
| 1497 | } |
| 1498 | |
| 1499 | static int swrm_connect_port(struct swr_master *master, |
| 1500 | struct swr_params *portinfo) |
| 1501 | { |
| 1502 | int i; |
| 1503 | struct swr_port_info *port_req; |
| 1504 | int ret = 0; |
| 1505 | struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master); |
| 1506 | struct swrm_mports *mport; |
| 1507 | u8 mstr_port_id, mstr_ch_msk; |
| 1508 | |
| 1509 | dev_dbg(&master->dev, "%s: enter\n", __func__); |
| 1510 | if (!portinfo) |
| 1511 | return -EINVAL; |
| 1512 | |
| 1513 | if (!swrm) { |
| 1514 | dev_err(&master->dev, |
| 1515 | "%s: Invalid handle to swr controller\n", |
| 1516 | __func__); |
| 1517 | return -EINVAL; |
| 1518 | } |
| 1519 | |
| 1520 | mutex_lock(&swrm->mlock); |
Ramprasad Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 1521 | mutex_lock(&swrm->devlock); |
| 1522 | if (!swrm->dev_up) { |
| 1523 | mutex_unlock(&swrm->devlock); |
| 1524 | mutex_unlock(&swrm->mlock); |
| 1525 | return -EINVAL; |
| 1526 | } |
| 1527 | mutex_unlock(&swrm->devlock); |
Ramprasad Katkam | 7cb4ff6 | 2018-09-12 04:00:26 +0530 | [diff] [blame] | 1528 | if (!swrm_is_port_en(master)) |
| 1529 | pm_runtime_get_sync(swrm->dev); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1530 | |
| 1531 | for (i = 0; i < portinfo->num_port; i++) { |
| 1532 | ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_msk, |
| 1533 | portinfo->port_type[i], |
| 1534 | portinfo->port_id[i]); |
| 1535 | if (ret) { |
| 1536 | dev_err(&master->dev, |
| 1537 | "%s: mstr portid for slv port %d not found\n", |
| 1538 | __func__, portinfo->port_id[i]); |
| 1539 | goto port_fail; |
| 1540 | } |
| 1541 | |
| 1542 | mport = &(swrm->mport_cfg[mstr_port_id]); |
| 1543 | /* get port req */ |
| 1544 | port_req = swrm_get_port_req(mport, portinfo->port_id[i], |
| 1545 | portinfo->dev_num); |
| 1546 | if (!port_req) { |
| 1547 | dev_dbg(&master->dev, "%s: new req:port id %d dev %d\n", |
| 1548 | __func__, portinfo->port_id[i], |
| 1549 | portinfo->dev_num); |
| 1550 | port_req = kzalloc(sizeof(struct swr_port_info), |
| 1551 | GFP_KERNEL); |
| 1552 | if (!port_req) { |
| 1553 | ret = -ENOMEM; |
| 1554 | goto mem_fail; |
| 1555 | } |
| 1556 | port_req->dev_num = portinfo->dev_num; |
| 1557 | port_req->slave_port_id = portinfo->port_id[i]; |
| 1558 | port_req->num_ch = portinfo->num_ch[i]; |
| 1559 | port_req->ch_rate = portinfo->ch_rate[i]; |
| 1560 | port_req->ch_en = 0; |
| 1561 | port_req->master_port_id = mstr_port_id; |
| 1562 | list_add(&port_req->list, &mport->port_req_list); |
| 1563 | } |
| 1564 | port_req->req_ch |= portinfo->ch_en[i]; |
| 1565 | |
| 1566 | dev_dbg(&master->dev, |
| 1567 | "%s: mstr port %d, slv port %d ch_rate %d num_ch %d\n", |
| 1568 | __func__, port_req->master_port_id, |
| 1569 | port_req->slave_port_id, port_req->ch_rate, |
| 1570 | port_req->num_ch); |
| 1571 | /* Put the port req on master port */ |
| 1572 | mport = &(swrm->mport_cfg[mstr_port_id]); |
| 1573 | mport->port_en = true; |
| 1574 | mport->req_ch |= mstr_ch_msk; |
| 1575 | master->port_en_mask |= (1 << mstr_port_id); |
Sudheer Papothi | ae5c363 | 2019-11-27 06:52:06 +0530 | [diff] [blame] | 1576 | if (swrm->clk_stop_mode0_supp && |
| 1577 | (mport->ch_rate < portinfo->ch_rate[i])) { |
| 1578 | mport->ch_rate = portinfo->ch_rate[i]; |
| 1579 | swrm_update_bus_clk(swrm); |
| 1580 | } |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1581 | } |
| 1582 | master->num_port += portinfo->num_port; |
Ramprasad Katkam | cab8d72 | 2018-09-28 15:54:06 +0530 | [diff] [blame] | 1583 | set_bit(ENABLE_PENDING, &swrm->port_req_pending); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1584 | swr_port_response(master, portinfo->tid); |
| 1585 | |
| 1586 | mutex_unlock(&swrm->mlock); |
| 1587 | return 0; |
| 1588 | |
| 1589 | port_fail: |
| 1590 | mem_fail: |
| 1591 | /* cleanup port reqs in error condition */ |
| 1592 | swrm_cleanup_disabled_port_reqs(master); |
| 1593 | mutex_unlock(&swrm->mlock); |
| 1594 | return ret; |
| 1595 | } |
| 1596 | |
| 1597 | static int swrm_disconnect_port(struct swr_master *master, |
| 1598 | struct swr_params *portinfo) |
| 1599 | { |
| 1600 | int i, ret = 0; |
| 1601 | struct swr_port_info *port_req; |
| 1602 | struct swrm_mports *mport; |
| 1603 | struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master); |
| 1604 | u8 mstr_port_id, mstr_ch_mask; |
| 1605 | |
| 1606 | if (!swrm) { |
| 1607 | dev_err(&master->dev, |
| 1608 | "%s: Invalid handle to swr controller\n", |
| 1609 | __func__); |
| 1610 | return -EINVAL; |
| 1611 | } |
| 1612 | |
| 1613 | if (!portinfo) { |
| 1614 | dev_err(&master->dev, "%s: portinfo is NULL\n", __func__); |
| 1615 | return -EINVAL; |
| 1616 | } |
| 1617 | mutex_lock(&swrm->mlock); |
| 1618 | |
| 1619 | for (i = 0; i < portinfo->num_port; i++) { |
| 1620 | |
| 1621 | ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_mask, |
| 1622 | portinfo->port_type[i], portinfo->port_id[i]); |
| 1623 | if (ret) { |
| 1624 | dev_err(&master->dev, |
| 1625 | "%s: mstr portid for slv port %d not found\n", |
| 1626 | __func__, portinfo->port_id[i]); |
| 1627 | mutex_unlock(&swrm->mlock); |
| 1628 | return -EINVAL; |
| 1629 | } |
| 1630 | mport = &(swrm->mport_cfg[mstr_port_id]); |
| 1631 | /* get port req */ |
| 1632 | port_req = swrm_get_port_req(mport, portinfo->port_id[i], |
| 1633 | portinfo->dev_num); |
| 1634 | |
| 1635 | if (!port_req) { |
| 1636 | dev_err(&master->dev, "%s:port not enabled : port %d\n", |
| 1637 | __func__, portinfo->port_id[i]); |
Ramprasad Katkam | 86c45e0 | 2018-10-16 19:31:51 +0530 | [diff] [blame] | 1638 | mutex_unlock(&swrm->mlock); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1639 | return -EINVAL; |
| 1640 | } |
| 1641 | port_req->req_ch &= ~portinfo->ch_en[i]; |
| 1642 | mport->req_ch &= ~mstr_ch_mask; |
Sudheer Papothi | ae5c363 | 2019-11-27 06:52:06 +0530 | [diff] [blame] | 1643 | if (swrm->clk_stop_mode0_supp && !mport->req_ch) { |
| 1644 | mport->ch_rate = 0; |
| 1645 | swrm_update_bus_clk(swrm); |
| 1646 | } |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1647 | } |
| 1648 | master->num_port -= portinfo->num_port; |
Ramprasad Katkam | cab8d72 | 2018-09-28 15:54:06 +0530 | [diff] [blame] | 1649 | set_bit(DISABLE_PENDING, &swrm->port_req_pending); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1650 | swr_port_response(master, portinfo->tid); |
| 1651 | mutex_unlock(&swrm->mlock); |
| 1652 | |
| 1653 | return 0; |
| 1654 | } |
| 1655 | |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 1656 | static int swrm_find_alert_slave(struct swr_mstr_ctrl *swrm, |
| 1657 | int status, u8 *devnum) |
| 1658 | { |
| 1659 | int i; |
| 1660 | bool found = false; |
| 1661 | |
| 1662 | for (i = 0; i < (swrm->master.num_dev + 1); i++) { |
| 1663 | if ((status & SWRM_MCP_SLV_STATUS_MASK) == SWR_ALERT) { |
| 1664 | *devnum = i; |
| 1665 | found = true; |
| 1666 | break; |
| 1667 | } |
| 1668 | status >>= 2; |
| 1669 | } |
| 1670 | if (found) |
| 1671 | return 0; |
| 1672 | else |
| 1673 | return -EINVAL; |
| 1674 | } |
| 1675 | |
Sudheer Papothi | 07d5afc | 2019-07-17 06:25:45 +0530 | [diff] [blame] | 1676 | static void swrm_enable_slave_irq(struct swr_mstr_ctrl *swrm) |
| 1677 | { |
| 1678 | int i; |
| 1679 | int status = 0; |
| 1680 | |
| 1681 | status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS); |
| 1682 | if (!status) { |
| 1683 | dev_dbg_ratelimited(swrm->dev, "%s: slaves status is 0x%x\n", |
| 1684 | __func__, status); |
| 1685 | return; |
| 1686 | } |
| 1687 | dev_dbg(swrm->dev, "%s: slave status: 0x%x\n", __func__, status); |
| 1688 | for (i = 0; i < (swrm->master.num_dev + 1); i++) { |
Vatsal Bucha | 21ba077 | 2020-02-28 20:36:46 +0530 | [diff] [blame] | 1689 | if (status & SWRM_MCP_SLV_STATUS_MASK) { |
| 1690 | swrm_cmd_fifo_wr_cmd(swrm, 0xFF, i, 0x0, |
| 1691 | SWRS_SCP_INT_STATUS_CLEAR_1); |
Sudheer Papothi | 07d5afc | 2019-07-17 06:25:45 +0530 | [diff] [blame] | 1692 | swrm_cmd_fifo_wr_cmd(swrm, 0x4, i, 0x0, |
Vatsal Bucha | 21ba077 | 2020-02-28 20:36:46 +0530 | [diff] [blame] | 1693 | SWRS_SCP_INT_STATUS_MASK_1); |
| 1694 | } |
Sudheer Papothi | 07d5afc | 2019-07-17 06:25:45 +0530 | [diff] [blame] | 1695 | status >>= 2; |
| 1696 | } |
| 1697 | } |
| 1698 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1699 | static int swrm_check_slave_change_status(struct swr_mstr_ctrl *swrm, |
| 1700 | int status, u8 *devnum) |
| 1701 | { |
| 1702 | int i; |
| 1703 | int new_sts = status; |
| 1704 | int ret = SWR_NOT_PRESENT; |
| 1705 | |
| 1706 | if (status != swrm->slave_status) { |
| 1707 | for (i = 0; i < (swrm->master.num_dev + 1); i++) { |
| 1708 | if ((status & SWRM_MCP_SLV_STATUS_MASK) != |
| 1709 | (swrm->slave_status & SWRM_MCP_SLV_STATUS_MASK)) { |
| 1710 | ret = (status & SWRM_MCP_SLV_STATUS_MASK); |
| 1711 | *devnum = i; |
| 1712 | break; |
| 1713 | } |
| 1714 | status >>= 2; |
| 1715 | swrm->slave_status >>= 2; |
| 1716 | } |
| 1717 | swrm->slave_status = new_sts; |
| 1718 | } |
| 1719 | return ret; |
| 1720 | } |
| 1721 | |
| 1722 | static irqreturn_t swr_mstr_interrupt(int irq, void *dev) |
| 1723 | { |
| 1724 | struct swr_mstr_ctrl *swrm = dev; |
Ramprasad Katkam | 7e35478 | 2018-11-21 15:52:54 +0530 | [diff] [blame] | 1725 | u32 value, intr_sts, intr_sts_masked; |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 1726 | u32 temp = 0; |
| 1727 | u32 status, chg_sts, i; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1728 | u8 devnum = 0; |
| 1729 | int ret = IRQ_HANDLED; |
| 1730 | struct swr_device *swr_dev; |
| 1731 | struct swr_master *mstr = &swrm->master; |
| 1732 | |
Aditya Bavanari | f500a1d | 2019-09-16 18:27:51 -0700 | [diff] [blame] | 1733 | trace_printk("%s enter\n", __func__); |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 1734 | if (unlikely(swrm_lock_sleep(swrm) == false)) { |
| 1735 | dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__); |
| 1736 | return IRQ_NONE; |
| 1737 | } |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1738 | |
| 1739 | mutex_lock(&swrm->reslock); |
Aditya Bavanari | f4a471d | 2019-02-19 17:57:12 +0530 | [diff] [blame] | 1740 | if (swrm_clk_request(swrm, true)) { |
Ramprasad Katkam | 14efed6 | 2019-03-07 13:16:50 +0530 | [diff] [blame] | 1741 | dev_err_ratelimited(swrm->dev, "%s:clk request failed\n", |
| 1742 | __func__); |
Aditya Bavanari | f4a471d | 2019-02-19 17:57:12 +0530 | [diff] [blame] | 1743 | mutex_unlock(&swrm->reslock); |
| 1744 | goto exit; |
| 1745 | } |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1746 | mutex_unlock(&swrm->reslock); |
| 1747 | |
| 1748 | intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS); |
Ramprasad Katkam | 7e35478 | 2018-11-21 15:52:54 +0530 | [diff] [blame] | 1749 | intr_sts_masked = intr_sts & swrm->intr_mask; |
Aditya Bavanari | f500a1d | 2019-09-16 18:27:51 -0700 | [diff] [blame] | 1750 | |
| 1751 | trace_printk("%s: status: 0x%x \n", __func__, intr_sts_masked); |
Ramprasad Katkam | 8330351 | 2018-10-11 17:34:22 +0530 | [diff] [blame] | 1752 | handle_irq: |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1753 | for (i = 0; i < SWRM_INTERRUPT_MAX; i++) { |
Ramprasad Katkam | 7e35478 | 2018-11-21 15:52:54 +0530 | [diff] [blame] | 1754 | value = intr_sts_masked & (1 << i); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1755 | if (!value) |
| 1756 | continue; |
| 1757 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1758 | switch (value) { |
| 1759 | case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ: |
| 1760 | dev_dbg(swrm->dev, "Trigger irq to slave device\n"); |
| 1761 | status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS); |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 1762 | ret = swrm_find_alert_slave(swrm, status, &devnum); |
| 1763 | if (ret) { |
Ramprasad Katkam | 18bc8e2 | 2018-10-25 15:04:24 +0530 | [diff] [blame] | 1764 | dev_err_ratelimited(swrm->dev, |
| 1765 | "no slave alert found.spurious interrupt\n"); |
Ramprasad Katkam | 48b49b2 | 2018-10-01 20:12:46 +0530 | [diff] [blame] | 1766 | break; |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 1767 | } |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 1768 | swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0, |
| 1769 | SWRS_SCP_INT_STATUS_CLEAR_1, 1); |
| 1770 | swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0, |
| 1771 | SWRS_SCP_INT_STATUS_CLEAR_1); |
| 1772 | swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0, |
| 1773 | SWRS_SCP_INT_STATUS_CLEAR_1); |
Ramprasad Katkam | 62d6d76 | 2018-09-20 17:50:28 +0530 | [diff] [blame] | 1774 | |
| 1775 | |
| 1776 | list_for_each_entry(swr_dev, &mstr->devices, dev_list) { |
| 1777 | if (swr_dev->dev_num != devnum) |
| 1778 | continue; |
| 1779 | if (swr_dev->slave_irq) { |
| 1780 | do { |
Ramprasad Katkam | 2586a4b | 2019-03-18 16:53:39 +0530 | [diff] [blame] | 1781 | swr_dev->slave_irq_pending = 0; |
Ramprasad Katkam | 62d6d76 | 2018-09-20 17:50:28 +0530 | [diff] [blame] | 1782 | handle_nested_irq( |
| 1783 | irq_find_mapping( |
| 1784 | swr_dev->slave_irq, 0)); |
| 1785 | } while (swr_dev->slave_irq_pending); |
| 1786 | } |
| 1787 | |
| 1788 | } |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1789 | break; |
| 1790 | case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED: |
| 1791 | dev_dbg(swrm->dev, "SWR new slave attached\n"); |
| 1792 | break; |
| 1793 | case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS: |
| 1794 | status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS); |
| 1795 | if (status == swrm->slave_status) { |
| 1796 | dev_dbg(swrm->dev, |
| 1797 | "%s: No change in slave status: %d\n", |
| 1798 | __func__, status); |
| 1799 | break; |
| 1800 | } |
| 1801 | chg_sts = swrm_check_slave_change_status(swrm, status, |
| 1802 | &devnum); |
| 1803 | switch (chg_sts) { |
| 1804 | case SWR_NOT_PRESENT: |
| 1805 | dev_dbg(swrm->dev, "device %d got detached\n", |
| 1806 | devnum); |
| 1807 | break; |
| 1808 | case SWR_ATTACHED_OK: |
| 1809 | dev_dbg(swrm->dev, "device %d got attached\n", |
| 1810 | devnum); |
Ramprasad Katkam | debe893 | 2018-09-25 18:08:18 +0530 | [diff] [blame] | 1811 | /* enable host irq from slave device*/ |
| 1812 | swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0, |
| 1813 | SWRS_SCP_INT_STATUS_CLEAR_1); |
| 1814 | swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0, |
| 1815 | SWRS_SCP_INT_STATUS_MASK_1); |
| 1816 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1817 | break; |
| 1818 | case SWR_ALERT: |
| 1819 | dev_dbg(swrm->dev, |
| 1820 | "device %d has pending interrupt\n", |
| 1821 | devnum); |
| 1822 | break; |
| 1823 | } |
| 1824 | break; |
| 1825 | case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET: |
| 1826 | dev_err_ratelimited(swrm->dev, |
| 1827 | "SWR bus clsh detected\n"); |
| 1828 | break; |
| 1829 | case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW: |
| 1830 | dev_dbg(swrm->dev, "SWR read FIFO overflow\n"); |
| 1831 | break; |
| 1832 | case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW: |
| 1833 | dev_dbg(swrm->dev, "SWR read FIFO underflow\n"); |
| 1834 | break; |
| 1835 | case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW: |
| 1836 | dev_dbg(swrm->dev, "SWR write FIFO overflow\n"); |
| 1837 | break; |
| 1838 | case SWRM_INTERRUPT_STATUS_CMD_ERROR: |
| 1839 | value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS); |
| 1840 | dev_err_ratelimited(swrm->dev, |
| 1841 | "SWR CMD error, fifo status 0x%x, flushing fifo\n", |
| 1842 | value); |
| 1843 | swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1); |
| 1844 | break; |
| 1845 | case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION: |
Ramprasad Katkam | 18bc8e2 | 2018-10-25 15:04:24 +0530 | [diff] [blame] | 1846 | dev_err_ratelimited(swrm->dev, "SWR Port collision detected\n"); |
Ramprasad Katkam | 7e35478 | 2018-11-21 15:52:54 +0530 | [diff] [blame] | 1847 | swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION; |
Ramprasad Katkam | 18bc8e2 | 2018-10-25 15:04:24 +0530 | [diff] [blame] | 1848 | swr_master_write(swrm, |
Ramprasad Katkam | 7e35478 | 2018-11-21 15:52:54 +0530 | [diff] [blame] | 1849 | SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1850 | break; |
| 1851 | case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH: |
| 1852 | dev_dbg(swrm->dev, "SWR read enable valid mismatch\n"); |
Ramprasad Katkam | 7e35478 | 2018-11-21 15:52:54 +0530 | [diff] [blame] | 1853 | swrm->intr_mask &= |
Ramprasad Katkam | 18bc8e2 | 2018-10-25 15:04:24 +0530 | [diff] [blame] | 1854 | ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH; |
| 1855 | swr_master_write(swrm, |
Ramprasad Katkam | 7e35478 | 2018-11-21 15:52:54 +0530 | [diff] [blame] | 1856 | SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1857 | break; |
| 1858 | case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED: |
| 1859 | complete(&swrm->broadcast); |
| 1860 | dev_dbg(swrm->dev, "SWR cmd id finished\n"); |
| 1861 | break; |
| 1862 | case SWRM_INTERRUPT_STATUS_NEW_SLAVE_AUTO_ENUM_FINISHED: |
| 1863 | break; |
| 1864 | case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED: |
| 1865 | break; |
| 1866 | case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL: |
| 1867 | break; |
| 1868 | case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED: |
| 1869 | complete(&swrm->reset); |
| 1870 | break; |
| 1871 | case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED: |
| 1872 | break; |
| 1873 | default: |
| 1874 | dev_err_ratelimited(swrm->dev, |
| 1875 | "SWR unknown interrupt\n"); |
| 1876 | ret = IRQ_NONE; |
| 1877 | break; |
| 1878 | } |
| 1879 | } |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 1880 | swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts); |
| 1881 | swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0); |
Ramprasad Katkam | 8330351 | 2018-10-11 17:34:22 +0530 | [diff] [blame] | 1882 | |
| 1883 | intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS); |
Ramprasad Katkam | 7e35478 | 2018-11-21 15:52:54 +0530 | [diff] [blame] | 1884 | intr_sts_masked = intr_sts & swrm->intr_mask; |
Ramprasad Katkam | 8330351 | 2018-10-11 17:34:22 +0530 | [diff] [blame] | 1885 | |
Ramprasad Katkam | 7e35478 | 2018-11-21 15:52:54 +0530 | [diff] [blame] | 1886 | if (intr_sts_masked) { |
Ramprasad Katkam | 8330351 | 2018-10-11 17:34:22 +0530 | [diff] [blame] | 1887 | dev_dbg(swrm->dev, "%s: new interrupt received\n", __func__); |
| 1888 | goto handle_irq; |
| 1889 | } |
| 1890 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1891 | mutex_lock(&swrm->reslock); |
| 1892 | swrm_clk_request(swrm, false); |
| 1893 | mutex_unlock(&swrm->reslock); |
Aditya Bavanari | f4a471d | 2019-02-19 17:57:12 +0530 | [diff] [blame] | 1894 | exit: |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 1895 | swrm_unlock_sleep(swrm); |
Aditya Bavanari | f500a1d | 2019-09-16 18:27:51 -0700 | [diff] [blame] | 1896 | trace_printk("%s exit\n", __func__); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 1897 | return ret; |
| 1898 | } |
| 1899 | |
Sudheer Papothi | d19d0c5 | 2019-02-23 05:41:39 +0530 | [diff] [blame] | 1900 | static irqreturn_t swr_mstr_interrupt_v2(int irq, void *dev) |
| 1901 | { |
| 1902 | struct swr_mstr_ctrl *swrm = dev; |
| 1903 | u32 value, intr_sts, intr_sts_masked; |
| 1904 | u32 temp = 0; |
| 1905 | u32 status, chg_sts, i; |
| 1906 | u8 devnum = 0; |
| 1907 | int ret = IRQ_HANDLED; |
| 1908 | struct swr_device *swr_dev; |
| 1909 | struct swr_master *mstr = &swrm->master; |
| 1910 | |
Aditya Bavanari | f500a1d | 2019-09-16 18:27:51 -0700 | [diff] [blame] | 1911 | trace_printk("%s enter\n", __func__); |
Sudheer Papothi | d19d0c5 | 2019-02-23 05:41:39 +0530 | [diff] [blame] | 1912 | if (unlikely(swrm_lock_sleep(swrm) == false)) { |
| 1913 | dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__); |
| 1914 | return IRQ_NONE; |
| 1915 | } |
| 1916 | |
| 1917 | mutex_lock(&swrm->reslock); |
Sudheer Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 1918 | if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) { |
| 1919 | ret = IRQ_NONE; |
| 1920 | goto exit; |
| 1921 | } |
| 1922 | if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) { |
| 1923 | ret = IRQ_NONE; |
Sudheer Papothi | 06f4341 | 2019-07-09 03:32:54 +0530 | [diff] [blame] | 1924 | goto err_audio_hw_vote; |
Karthikeyan Mani | 035c50b | 2019-05-02 13:35:01 -0700 | [diff] [blame] | 1925 | } |
Karthikeyan Mani | 4bee1db | 2019-09-18 17:58:41 -0700 | [diff] [blame] | 1926 | ret = swrm_clk_request(swrm, true); |
| 1927 | if (ret) { |
| 1928 | dev_err(dev, "%s: swrm clk failed\n", __func__); |
| 1929 | ret = IRQ_NONE; |
| 1930 | goto err_audio_core_vote; |
| 1931 | } |
Sudheer Papothi | d19d0c5 | 2019-02-23 05:41:39 +0530 | [diff] [blame] | 1932 | mutex_unlock(&swrm->reslock); |
| 1933 | |
| 1934 | intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS); |
| 1935 | intr_sts_masked = intr_sts & swrm->intr_mask; |
Sudheer Papothi | 06f4341 | 2019-07-09 03:32:54 +0530 | [diff] [blame] | 1936 | |
| 1937 | dev_dbg(swrm->dev, "%s: status: 0x%x \n", __func__, intr_sts_masked); |
Aditya Bavanari | f500a1d | 2019-09-16 18:27:51 -0700 | [diff] [blame] | 1938 | trace_printk("%s: status: 0x%x \n", __func__, intr_sts_masked); |
Sudheer Papothi | d19d0c5 | 2019-02-23 05:41:39 +0530 | [diff] [blame] | 1939 | handle_irq: |
| 1940 | for (i = 0; i < SWRM_INTERRUPT_MAX; i++) { |
| 1941 | value = intr_sts_masked & (1 << i); |
| 1942 | if (!value) |
| 1943 | continue; |
| 1944 | |
| 1945 | switch (value) { |
| 1946 | case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ: |
| 1947 | dev_dbg(swrm->dev, "%s: Trigger irq to slave device\n", |
| 1948 | __func__); |
| 1949 | status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS); |
| 1950 | ret = swrm_find_alert_slave(swrm, status, &devnum); |
| 1951 | if (ret) { |
| 1952 | dev_err_ratelimited(swrm->dev, |
| 1953 | "%s: no slave alert found.spurious interrupt\n", |
| 1954 | __func__); |
| 1955 | break; |
| 1956 | } |
| 1957 | swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0, |
| 1958 | SWRS_SCP_INT_STATUS_CLEAR_1, 1); |
| 1959 | swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0, |
| 1960 | SWRS_SCP_INT_STATUS_CLEAR_1); |
| 1961 | swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0, |
| 1962 | SWRS_SCP_INT_STATUS_CLEAR_1); |
| 1963 | |
| 1964 | |
| 1965 | list_for_each_entry(swr_dev, &mstr->devices, dev_list) { |
| 1966 | if (swr_dev->dev_num != devnum) |
| 1967 | continue; |
| 1968 | if (swr_dev->slave_irq) { |
| 1969 | do { |
Meng Wang | 31a7ef1 | 2019-12-18 10:36:29 +0800 | [diff] [blame] | 1970 | swr_dev->slave_irq_pending = 0; |
Sudheer Papothi | d19d0c5 | 2019-02-23 05:41:39 +0530 | [diff] [blame] | 1971 | handle_nested_irq( |
| 1972 | irq_find_mapping( |
| 1973 | swr_dev->slave_irq, 0)); |
| 1974 | } while (swr_dev->slave_irq_pending); |
| 1975 | } |
| 1976 | |
| 1977 | } |
| 1978 | break; |
| 1979 | case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED: |
| 1980 | dev_dbg(swrm->dev, "%s: SWR new slave attached\n", |
| 1981 | __func__); |
| 1982 | break; |
| 1983 | case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS: |
| 1984 | status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS); |
Laxminath Kasam | 44cedb8 | 2019-11-20 17:37:23 +0530 | [diff] [blame] | 1985 | swrm_enable_slave_irq(swrm); |
Sudheer Papothi | d19d0c5 | 2019-02-23 05:41:39 +0530 | [diff] [blame] | 1986 | if (status == swrm->slave_status) { |
| 1987 | dev_dbg(swrm->dev, |
| 1988 | "%s: No change in slave status: %d\n", |
| 1989 | __func__, status); |
| 1990 | break; |
| 1991 | } |
| 1992 | chg_sts = swrm_check_slave_change_status(swrm, status, |
| 1993 | &devnum); |
| 1994 | switch (chg_sts) { |
| 1995 | case SWR_NOT_PRESENT: |
| 1996 | dev_dbg(swrm->dev, |
| 1997 | "%s: device %d got detached\n", |
| 1998 | __func__, devnum); |
Vatsal Bucha | 21ba077 | 2020-02-28 20:36:46 +0530 | [diff] [blame] | 1999 | if (devnum == 0) { |
| 2000 | /* |
| 2001 | * enable host irq if device 0 detached |
| 2002 | * as hw will mask host_irq at slave |
| 2003 | * but will not unmask it afterwards. |
| 2004 | */ |
| 2005 | swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0, |
| 2006 | SWRS_SCP_INT_STATUS_CLEAR_1); |
| 2007 | swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0, |
| 2008 | SWRS_SCP_INT_STATUS_MASK_1); |
| 2009 | } |
Sudheer Papothi | d19d0c5 | 2019-02-23 05:41:39 +0530 | [diff] [blame] | 2010 | break; |
| 2011 | case SWR_ATTACHED_OK: |
| 2012 | dev_dbg(swrm->dev, |
| 2013 | "%s: device %d got attached\n", |
| 2014 | __func__, devnum); |
| 2015 | /* enable host irq from slave device*/ |
| 2016 | swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0, |
| 2017 | SWRS_SCP_INT_STATUS_CLEAR_1); |
| 2018 | swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0, |
| 2019 | SWRS_SCP_INT_STATUS_MASK_1); |
| 2020 | |
| 2021 | break; |
| 2022 | case SWR_ALERT: |
| 2023 | dev_dbg(swrm->dev, |
| 2024 | "%s: device %d has pending interrupt\n", |
| 2025 | __func__, devnum); |
| 2026 | break; |
| 2027 | } |
| 2028 | break; |
| 2029 | case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET: |
| 2030 | dev_err_ratelimited(swrm->dev, |
| 2031 | "%s: SWR bus clsh detected\n", |
| 2032 | __func__); |
Aditya Bavanari | f0106d9 | 2020-01-31 17:01:21 +0530 | [diff] [blame] | 2033 | swrm->intr_mask &= |
| 2034 | ~SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET; |
| 2035 | swr_master_write(swrm, |
| 2036 | SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, |
| 2037 | swrm->intr_mask); |
Sudheer Papothi | d19d0c5 | 2019-02-23 05:41:39 +0530 | [diff] [blame] | 2038 | break; |
| 2039 | case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW: |
Vatsal Bucha | bd68a7c | 2020-06-08 10:41:56 +0530 | [diff] [blame] | 2040 | value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS); |
| 2041 | dev_err(swrm->dev, |
| 2042 | "%s: SWR read FIFO overflow fifo status 0x%x\n", |
| 2043 | __func__, value); |
Sudheer Papothi | d19d0c5 | 2019-02-23 05:41:39 +0530 | [diff] [blame] | 2044 | break; |
| 2045 | case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW: |
Vatsal Bucha | bd68a7c | 2020-06-08 10:41:56 +0530 | [diff] [blame] | 2046 | value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS); |
| 2047 | dev_err(swrm->dev, |
| 2048 | "%s: SWR read FIFO underflow fifo status 0x%x\n", |
| 2049 | __func__, value); |
Sudheer Papothi | d19d0c5 | 2019-02-23 05:41:39 +0530 | [diff] [blame] | 2050 | break; |
| 2051 | case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW: |
Vatsal Bucha | bd68a7c | 2020-06-08 10:41:56 +0530 | [diff] [blame] | 2052 | value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS); |
| 2053 | dev_err(swrm->dev, |
| 2054 | "%s: SWR write FIFO overflow fifo status %x\n", |
| 2055 | __func__, value); |
Sudheer Papothi | ac0ae1c | 2019-10-17 05:38:40 +0530 | [diff] [blame] | 2056 | swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1); |
Sudheer Papothi | d19d0c5 | 2019-02-23 05:41:39 +0530 | [diff] [blame] | 2057 | break; |
| 2058 | case SWRM_INTERRUPT_STATUS_CMD_ERROR: |
| 2059 | value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS); |
| 2060 | dev_err_ratelimited(swrm->dev, |
| 2061 | "%s: SWR CMD error, fifo status 0x%x, flushing fifo\n", |
| 2062 | __func__, value); |
| 2063 | swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1); |
| 2064 | break; |
| 2065 | case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION: |
| 2066 | dev_err_ratelimited(swrm->dev, |
| 2067 | "%s: SWR Port collision detected\n", |
| 2068 | __func__); |
| 2069 | swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION; |
| 2070 | swr_master_write(swrm, |
| 2071 | SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask); |
| 2072 | break; |
| 2073 | case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH: |
| 2074 | dev_dbg(swrm->dev, |
| 2075 | "%s: SWR read enable valid mismatch\n", |
| 2076 | __func__); |
| 2077 | swrm->intr_mask &= |
| 2078 | ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH; |
| 2079 | swr_master_write(swrm, |
| 2080 | SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask); |
| 2081 | break; |
| 2082 | case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED: |
| 2083 | complete(&swrm->broadcast); |
| 2084 | dev_dbg(swrm->dev, "%s: SWR cmd id finished\n", |
| 2085 | __func__); |
| 2086 | break; |
| 2087 | case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED_V2: |
| 2088 | break; |
| 2089 | case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL_V2: |
| 2090 | break; |
| 2091 | case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED_V2: |
Laxminath Kasam | e229197 | 2019-11-08 14:51:59 +0530 | [diff] [blame] | 2092 | swrm_check_link_status(swrm, 0x1); |
Sudheer Papothi | d19d0c5 | 2019-02-23 05:41:39 +0530 | [diff] [blame] | 2093 | break; |
| 2094 | case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED_V2: |
| 2095 | break; |
| 2096 | case SWRM_INTERRUPT_STATUS_EXT_CLK_STOP_WAKEUP: |
| 2097 | if (swrm->state == SWR_MSTR_UP) |
| 2098 | dev_dbg(swrm->dev, |
| 2099 | "%s:SWR Master is already up\n", |
| 2100 | __func__); |
| 2101 | else |
| 2102 | dev_err_ratelimited(swrm->dev, |
| 2103 | "%s: SWR wokeup during clock stop\n", |
| 2104 | __func__); |
Sudheer Papothi | 07d5afc | 2019-07-17 06:25:45 +0530 | [diff] [blame] | 2105 | /* It might be possible the slave device gets reset |
| 2106 | * and slave interrupt gets missed. So re-enable |
| 2107 | * Host IRQ and process slave pending |
| 2108 | * interrupts, if any. |
| 2109 | */ |
| 2110 | swrm_enable_slave_irq(swrm); |
Sudheer Papothi | d19d0c5 | 2019-02-23 05:41:39 +0530 | [diff] [blame] | 2111 | break; |
| 2112 | default: |
| 2113 | dev_err_ratelimited(swrm->dev, |
| 2114 | "%s: SWR unknown interrupt value: %d\n", |
| 2115 | __func__, value); |
| 2116 | ret = IRQ_NONE; |
| 2117 | break; |
| 2118 | } |
| 2119 | } |
| 2120 | swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts); |
| 2121 | swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0); |
| 2122 | |
| 2123 | intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS); |
| 2124 | intr_sts_masked = intr_sts & swrm->intr_mask; |
| 2125 | |
| 2126 | if (intr_sts_masked) { |
Sudheer Papothi | 07d5afc | 2019-07-17 06:25:45 +0530 | [diff] [blame] | 2127 | dev_dbg(swrm->dev, "%s: new interrupt received 0x%x\n", |
| 2128 | __func__, intr_sts_masked); |
Sudheer Papothi | d19d0c5 | 2019-02-23 05:41:39 +0530 | [diff] [blame] | 2129 | goto handle_irq; |
| 2130 | } |
| 2131 | |
| 2132 | mutex_lock(&swrm->reslock); |
| 2133 | swrm_clk_request(swrm, false); |
Karthikeyan Mani | 4bee1db | 2019-09-18 17:58:41 -0700 | [diff] [blame] | 2134 | err_audio_core_vote: |
Sudheer Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 2135 | swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false); |
Sudheer Papothi | 06f4341 | 2019-07-09 03:32:54 +0530 | [diff] [blame] | 2136 | |
| 2137 | err_audio_hw_vote: |
Sudheer Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 2138 | swrm_request_hw_vote(swrm, LPASS_HW_CORE, false); |
Karthikeyan Mani | 035c50b | 2019-05-02 13:35:01 -0700 | [diff] [blame] | 2139 | exit: |
Sudheer Papothi | d19d0c5 | 2019-02-23 05:41:39 +0530 | [diff] [blame] | 2140 | mutex_unlock(&swrm->reslock); |
| 2141 | swrm_unlock_sleep(swrm); |
Aditya Bavanari | f500a1d | 2019-09-16 18:27:51 -0700 | [diff] [blame] | 2142 | trace_printk("%s exit\n", __func__); |
Sudheer Papothi | d19d0c5 | 2019-02-23 05:41:39 +0530 | [diff] [blame] | 2143 | return ret; |
| 2144 | } |
| 2145 | |
Aditya Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 2146 | static irqreturn_t swrm_wakeup_interrupt(int irq, void *dev) |
| 2147 | { |
| 2148 | struct swr_mstr_ctrl *swrm = dev; |
| 2149 | int ret = IRQ_HANDLED; |
| 2150 | |
| 2151 | if (!swrm || !(swrm->dev)) { |
| 2152 | pr_err("%s: swrm or dev is null\n", __func__); |
| 2153 | return IRQ_NONE; |
| 2154 | } |
Vatsal Bucha | 8bcaeab | 2019-10-31 11:45:36 +0530 | [diff] [blame] | 2155 | |
Aditya Bavanari | f500a1d | 2019-09-16 18:27:51 -0700 | [diff] [blame] | 2156 | trace_printk("%s enter\n", __func__); |
Aditya Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 2157 | mutex_lock(&swrm->devlock); |
| 2158 | if (!swrm->dev_up) { |
Vatsal Bucha | 8bcaeab | 2019-10-31 11:45:36 +0530 | [diff] [blame] | 2159 | if (swrm->wake_irq > 0) { |
| 2160 | if (unlikely(!irq_get_irq_data(swrm->wake_irq))) { |
| 2161 | pr_err("%s: irq data is NULL\n", __func__); |
| 2162 | mutex_unlock(&swrm->devlock); |
| 2163 | return IRQ_NONE; |
| 2164 | } |
| 2165 | mutex_lock(&swrm->irq_lock); |
| 2166 | if (!irqd_irq_disabled( |
| 2167 | irq_get_irq_data(swrm->wake_irq))) |
| 2168 | disable_irq_nosync(swrm->wake_irq); |
| 2169 | mutex_unlock(&swrm->irq_lock); |
| 2170 | } |
Aditya Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 2171 | mutex_unlock(&swrm->devlock); |
| 2172 | return ret; |
| 2173 | } |
| 2174 | mutex_unlock(&swrm->devlock); |
Ramprasad Katkam | 44b7a96 | 2018-12-20 15:08:44 +0530 | [diff] [blame] | 2175 | if (unlikely(swrm_lock_sleep(swrm) == false)) { |
| 2176 | dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__); |
| 2177 | goto exit; |
| 2178 | } |
Vatsal Bucha | 8bcaeab | 2019-10-31 11:45:36 +0530 | [diff] [blame] | 2179 | if (swrm->wake_irq > 0) { |
| 2180 | if (unlikely(!irq_get_irq_data(swrm->wake_irq))) { |
| 2181 | pr_err("%s: irq data is NULL\n", __func__); |
| 2182 | return IRQ_NONE; |
| 2183 | } |
| 2184 | mutex_lock(&swrm->irq_lock); |
| 2185 | if (!irqd_irq_disabled( |
| 2186 | irq_get_irq_data(swrm->wake_irq))) |
| 2187 | disable_irq_nosync(swrm->wake_irq); |
| 2188 | mutex_unlock(&swrm->irq_lock); |
| 2189 | } |
Aditya Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 2190 | pm_runtime_get_sync(swrm->dev); |
| 2191 | pm_runtime_mark_last_busy(swrm->dev); |
| 2192 | pm_runtime_put_autosuspend(swrm->dev); |
Ramprasad Katkam | 44b7a96 | 2018-12-20 15:08:44 +0530 | [diff] [blame] | 2193 | swrm_unlock_sleep(swrm); |
| 2194 | exit: |
Aditya Bavanari | f500a1d | 2019-09-16 18:27:51 -0700 | [diff] [blame] | 2195 | trace_printk("%s exit\n", __func__); |
Aditya Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 2196 | return ret; |
| 2197 | } |
| 2198 | |
Laxminath Kasam | f0128ef | 2018-08-31 15:15:09 +0530 | [diff] [blame] | 2199 | static void swrm_wakeup_work(struct work_struct *work) |
| 2200 | { |
| 2201 | struct swr_mstr_ctrl *swrm; |
| 2202 | |
| 2203 | swrm = container_of(work, struct swr_mstr_ctrl, |
| 2204 | wakeup_work); |
| 2205 | if (!swrm || !(swrm->dev)) { |
| 2206 | pr_err("%s: swrm or dev is null\n", __func__); |
| 2207 | return; |
| 2208 | } |
Ramprasad Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 2209 | |
Aditya Bavanari | f500a1d | 2019-09-16 18:27:51 -0700 | [diff] [blame] | 2210 | trace_printk("%s enter\n", __func__); |
Ramprasad Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 2211 | mutex_lock(&swrm->devlock); |
| 2212 | if (!swrm->dev_up) { |
| 2213 | mutex_unlock(&swrm->devlock); |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 2214 | goto exit; |
Ramprasad Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 2215 | } |
| 2216 | mutex_unlock(&swrm->devlock); |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 2217 | if (unlikely(swrm_lock_sleep(swrm) == false)) { |
| 2218 | dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__); |
| 2219 | goto exit; |
| 2220 | } |
Laxminath Kasam | f0128ef | 2018-08-31 15:15:09 +0530 | [diff] [blame] | 2221 | pm_runtime_get_sync(swrm->dev); |
Laxminath Kasam | f0128ef | 2018-08-31 15:15:09 +0530 | [diff] [blame] | 2222 | pm_runtime_mark_last_busy(swrm->dev); |
| 2223 | pm_runtime_put_autosuspend(swrm->dev); |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 2224 | swrm_unlock_sleep(swrm); |
| 2225 | exit: |
Aditya Bavanari | f500a1d | 2019-09-16 18:27:51 -0700 | [diff] [blame] | 2226 | trace_printk("%s exit\n", __func__); |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 2227 | pm_relax(swrm->dev); |
Laxminath Kasam | f0128ef | 2018-08-31 15:15:09 +0530 | [diff] [blame] | 2228 | } |
| 2229 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2230 | static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum) |
| 2231 | { |
| 2232 | u32 val; |
| 2233 | |
| 2234 | swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS); |
| 2235 | val = (swrm->slave_status >> (devnum * 2)); |
| 2236 | val &= SWRM_MCP_SLV_STATUS_MASK; |
| 2237 | return val; |
| 2238 | } |
| 2239 | |
| 2240 | static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id, |
| 2241 | u8 *dev_num) |
| 2242 | { |
| 2243 | int i; |
| 2244 | u64 id = 0; |
| 2245 | int ret = -EINVAL; |
| 2246 | struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr); |
| 2247 | struct swr_device *swr_dev; |
| 2248 | u32 num_dev = 0; |
| 2249 | |
| 2250 | if (!swrm) { |
| 2251 | pr_err("%s: Invalid handle to swr controller\n", |
| 2252 | __func__); |
| 2253 | return ret; |
| 2254 | } |
| 2255 | if (swrm->num_dev) |
| 2256 | num_dev = swrm->num_dev; |
| 2257 | else |
| 2258 | num_dev = mstr->num_dev; |
Ramprasad Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 2259 | |
| 2260 | mutex_lock(&swrm->devlock); |
| 2261 | if (!swrm->dev_up) { |
| 2262 | mutex_unlock(&swrm->devlock); |
| 2263 | return ret; |
| 2264 | } |
| 2265 | mutex_unlock(&swrm->devlock); |
| 2266 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2267 | pm_runtime_get_sync(swrm->dev); |
| 2268 | for (i = 1; i < (num_dev + 1); i++) { |
| 2269 | id = ((u64)(swr_master_read(swrm, |
| 2270 | SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32); |
| 2271 | id |= swr_master_read(swrm, |
| 2272 | SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i)); |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 2273 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2274 | /* |
| 2275 | * As pm_runtime_get_sync() brings all slaves out of reset |
| 2276 | * update logical device number for all slaves. |
| 2277 | */ |
| 2278 | list_for_each_entry(swr_dev, &mstr->devices, dev_list) { |
| 2279 | if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) { |
| 2280 | u32 status = swrm_get_device_status(swrm, i); |
| 2281 | |
| 2282 | if ((status == 0x01) || (status == 0x02)) { |
| 2283 | swr_dev->dev_num = i; |
| 2284 | if ((id & SWR_DEV_ID_MASK) == dev_id) { |
| 2285 | *dev_num = i; |
| 2286 | ret = 0; |
| 2287 | } |
| 2288 | dev_dbg(swrm->dev, |
Xiao Li | d8bb93c | 2020-01-07 12:59:05 +0800 | [diff] [blame] | 2289 | "%s: devnum %d is assigned for dev addr %llx\n", |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2290 | __func__, i, swr_dev->addr); |
| 2291 | } |
| 2292 | } |
| 2293 | } |
| 2294 | } |
| 2295 | if (ret) |
| 2296 | dev_err(swrm->dev, "%s: device 0x%llx is not ready\n", |
| 2297 | __func__, dev_id); |
| 2298 | |
| 2299 | pm_runtime_mark_last_busy(swrm->dev); |
| 2300 | pm_runtime_put_autosuspend(swrm->dev); |
| 2301 | return ret; |
| 2302 | } |
Sudheer Papothi | 6abd2de | 2018-09-05 05:57:04 +0530 | [diff] [blame] | 2303 | |
| 2304 | static void swrm_device_wakeup_vote(struct swr_master *mstr) |
| 2305 | { |
| 2306 | struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr); |
| 2307 | |
| 2308 | if (!swrm) { |
| 2309 | pr_err("%s: Invalid handle to swr controller\n", |
| 2310 | __func__); |
| 2311 | return; |
| 2312 | } |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 2313 | if (unlikely(swrm_lock_sleep(swrm) == false)) { |
| 2314 | dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__); |
| 2315 | return; |
| 2316 | } |
Aditya Bavanari | eb04461 | 2019-12-22 17:14:15 +0530 | [diff] [blame] | 2317 | mutex_lock(&swrm->reslock); |
| 2318 | if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) |
| 2319 | dev_err(swrm->dev, "%s:lpass core hw enable failed\n", |
| 2320 | __func__); |
| 2321 | if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) |
| 2322 | dev_err(swrm->dev, "%s:lpass audio hw enable failed\n", |
| 2323 | __func__); |
| 2324 | mutex_unlock(&swrm->reslock); |
| 2325 | |
Sudheer Papothi | 6abd2de | 2018-09-05 05:57:04 +0530 | [diff] [blame] | 2326 | pm_runtime_get_sync(swrm->dev); |
| 2327 | } |
| 2328 | |
| 2329 | static void swrm_device_wakeup_unvote(struct swr_master *mstr) |
| 2330 | { |
| 2331 | struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr); |
| 2332 | |
| 2333 | if (!swrm) { |
| 2334 | pr_err("%s: Invalid handle to swr controller\n", |
| 2335 | __func__); |
| 2336 | return; |
| 2337 | } |
| 2338 | pm_runtime_mark_last_busy(swrm->dev); |
| 2339 | pm_runtime_put_autosuspend(swrm->dev); |
Sudheer Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 2340 | |
Aditya Bavanari | eb04461 | 2019-12-22 17:14:15 +0530 | [diff] [blame] | 2341 | mutex_lock(&swrm->reslock); |
| 2342 | swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false); |
| 2343 | swrm_request_hw_vote(swrm, LPASS_HW_CORE, false); |
| 2344 | mutex_unlock(&swrm->reslock); |
Sudheer Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 2345 | |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 2346 | swrm_unlock_sleep(swrm); |
Sudheer Papothi | 6abd2de | 2018-09-05 05:57:04 +0530 | [diff] [blame] | 2347 | } |
| 2348 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2349 | static int swrm_master_init(struct swr_mstr_ctrl *swrm) |
| 2350 | { |
Laxminath Kasam | 3695642 | 2020-06-01 19:23:48 +0530 | [diff] [blame] | 2351 | int ret = 0, i = 0; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2352 | u32 val; |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 2353 | u8 row_ctrl = SWR_ROW_50; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2354 | u8 col_ctrl = SWR_MIN_COL; |
| 2355 | u8 ssp_period = 1; |
| 2356 | u8 retry_cmd_num = 3; |
| 2357 | u32 reg[SWRM_MAX_INIT_REG]; |
| 2358 | u32 value[SWRM_MAX_INIT_REG]; |
Laxminath Kasam | c7bfab9 | 2019-08-27 16:19:14 +0530 | [diff] [blame] | 2359 | u32 temp = 0; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2360 | int len = 0; |
| 2361 | |
Laxminath Kasam | 3695642 | 2020-06-01 19:23:48 +0530 | [diff] [blame] | 2362 | /* SW workaround to gate hw_ctl for SWR version >=1.6 */ |
| 2363 | if (swrm->version >= SWRM_VERSION_1_6) { |
| 2364 | if (swrm->swrm_hctl_reg) { |
| 2365 | temp = ioread32(swrm->swrm_hctl_reg); |
| 2366 | temp &= 0xFFFFFFFD; |
| 2367 | iowrite32(temp, swrm->swrm_hctl_reg); |
| 2368 | usleep_range(500, 505); |
| 2369 | temp = ioread32(swrm->swrm_hctl_reg); |
| 2370 | dev_dbg(swrm->dev, "%s: hctl_reg val: 0x%x\n", |
| 2371 | __func__, temp); |
| 2372 | } |
| 2373 | } |
Sudheer Papothi | ac0ae1c | 2019-10-17 05:38:40 +0530 | [diff] [blame] | 2374 | ssp_period = swrm_get_ssp_period(swrm, SWRM_ROW_50, |
| 2375 | SWRM_COL_02, SWRM_FRAME_SYNC_SEL); |
| 2376 | dev_dbg(swrm->dev, "%s: ssp_period: %d\n", __func__, ssp_period); |
| 2377 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2378 | /* Clear Rows and Cols */ |
| 2379 | val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) | |
| 2380 | (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) | |
Sudheer Papothi | ac0ae1c | 2019-10-17 05:38:40 +0530 | [diff] [blame] | 2381 | ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT)); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2382 | |
| 2383 | reg[len] = SWRM_MCP_FRAME_CTRL_BANK_ADDR(0); |
| 2384 | value[len++] = val; |
| 2385 | |
| 2386 | /* Set Auto enumeration flag */ |
| 2387 | reg[len] = SWRM_ENUMERATOR_CFG_ADDR; |
| 2388 | value[len++] = 1; |
| 2389 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2390 | /* Configure No pings */ |
| 2391 | val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR); |
| 2392 | val &= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK; |
| 2393 | val |= (0x1f << SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT); |
| 2394 | reg[len] = SWRM_MCP_CFG_ADDR; |
| 2395 | value[len++] = val; |
| 2396 | |
| 2397 | /* Configure number of retries of a read/write cmd */ |
| 2398 | val = (retry_cmd_num << SWRM_CMD_FIFO_CFG_NUM_OF_CMD_RETRY_SHFT); |
| 2399 | reg[len] = SWRM_CMD_FIFO_CFG_ADDR; |
| 2400 | value[len++] = val; |
| 2401 | |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 2402 | reg[len] = SWRM_MCP_BUS_CTRL_ADDR; |
| 2403 | value[len++] = 0x2; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2404 | |
Ramprasad Katkam | 8330351 | 2018-10-11 17:34:22 +0530 | [diff] [blame] | 2405 | /* Set IRQ to PULSE */ |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2406 | reg[len] = SWRM_COMP_CFG_ADDR; |
Ramprasad Katkam | 8330351 | 2018-10-11 17:34:22 +0530 | [diff] [blame] | 2407 | value[len++] = 0x02; |
| 2408 | |
| 2409 | reg[len] = SWRM_COMP_CFG_ADDR; |
| 2410 | value[len++] = 0x03; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2411 | |
| 2412 | reg[len] = SWRM_INTERRUPT_CLEAR; |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 2413 | value[len++] = 0xFFFFFFFF; |
| 2414 | |
Ramprasad Katkam | 7e35478 | 2018-11-21 15:52:54 +0530 | [diff] [blame] | 2415 | swrm->intr_mask = SWRM_INTERRUPT_STATUS_MASK; |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 2416 | /* Mask soundwire interrupts */ |
| 2417 | reg[len] = SWRM_INTERRUPT_MASK_ADDR; |
Ramprasad Katkam | 7e35478 | 2018-11-21 15:52:54 +0530 | [diff] [blame] | 2418 | value[len++] = swrm->intr_mask; |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 2419 | |
| 2420 | reg[len] = SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN; |
Ramprasad Katkam | 7e35478 | 2018-11-21 15:52:54 +0530 | [diff] [blame] | 2421 | value[len++] = swrm->intr_mask; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2422 | |
| 2423 | swr_master_bulk_write(swrm, reg, value, len); |
| 2424 | |
Laxminath Kasam | cafe073 | 2019-11-20 17:31:58 +0530 | [diff] [blame] | 2425 | if (!swrm_check_link_status(swrm, 0x1)) { |
| 2426 | dev_err(swrm->dev, |
| 2427 | "%s: swr link failed to connect\n", |
| 2428 | __func__); |
Laxminath Kasam | 3695642 | 2020-06-01 19:23:48 +0530 | [diff] [blame] | 2429 | for (i = 0; i < len; i++) { |
| 2430 | usleep_range(50, 55); |
| 2431 | dev_err(swrm->dev, |
| 2432 | "%s:reg:0x%x val:0x%x\n", |
| 2433 | __func__, |
| 2434 | reg[i], swr_master_read(swrm, reg[i])); |
| 2435 | } |
Laxminath Kasam | cafe073 | 2019-11-20 17:31:58 +0530 | [diff] [blame] | 2436 | return -EINVAL; |
| 2437 | } |
Sudheer Papothi | 63f4815 | 2018-11-15 01:08:03 +0530 | [diff] [blame] | 2438 | /* |
| 2439 | * For SWR master version 1.5.1, continue |
| 2440 | * execute on command ignore. |
| 2441 | */ |
Laxminath Kasam | c7bfab9 | 2019-08-27 16:19:14 +0530 | [diff] [blame] | 2442 | /* Execute it for versions >= 1.5.1 */ |
| 2443 | if (swrm->version >= SWRM_VERSION_1_5_1) |
Sudheer Papothi | 63f4815 | 2018-11-15 01:08:03 +0530 | [diff] [blame] | 2444 | swr_master_write(swrm, SWRM_CMD_FIFO_CFG_ADDR, |
| 2445 | (swr_master_read(swrm, |
| 2446 | SWRM_CMD_FIFO_CFG_ADDR) | 0x80000000)); |
| 2447 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2448 | return ret; |
| 2449 | } |
| 2450 | |
Ramprasad Katkam | 68765ab | 2018-08-30 11:46:32 +0530 | [diff] [blame] | 2451 | static int swrm_event_notify(struct notifier_block *self, |
Laxminath Kasam | f0128ef | 2018-08-31 15:15:09 +0530 | [diff] [blame] | 2452 | unsigned long action, void *data) |
Ramprasad Katkam | 68765ab | 2018-08-30 11:46:32 +0530 | [diff] [blame] | 2453 | { |
| 2454 | struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl, |
Laxminath Kasam | f0128ef | 2018-08-31 15:15:09 +0530 | [diff] [blame] | 2455 | event_notifier); |
| 2456 | |
| 2457 | if (!swrm || !(swrm->dev)) { |
| 2458 | pr_err("%s: swrm or dev is NULL\n", __func__); |
Ramprasad Katkam | 68765ab | 2018-08-30 11:46:32 +0530 | [diff] [blame] | 2459 | return -EINVAL; |
| 2460 | } |
Laxminath Kasam | f0128ef | 2018-08-31 15:15:09 +0530 | [diff] [blame] | 2461 | switch (action) { |
| 2462 | case MSM_AUD_DC_EVENT: |
| 2463 | schedule_work(&(swrm->dc_presence_work)); |
| 2464 | break; |
| 2465 | case SWR_WAKE_IRQ_EVENT: |
Aditya Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 2466 | if (swrm->ipc_wakeup && !swrm->ipc_wakeup_triggered) { |
| 2467 | swrm->ipc_wakeup_triggered = true; |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 2468 | pm_stay_awake(swrm->dev); |
Laxminath Kasam | f0128ef | 2018-08-31 15:15:09 +0530 | [diff] [blame] | 2469 | schedule_work(&swrm->wakeup_work); |
Ramprasad Katkam | cd61c6e | 2018-09-18 13:22:58 +0530 | [diff] [blame] | 2470 | } |
Laxminath Kasam | f0128ef | 2018-08-31 15:15:09 +0530 | [diff] [blame] | 2471 | break; |
| 2472 | default: |
| 2473 | dev_err(swrm->dev, "%s: invalid event type: %lu\n", |
Ramprasad Katkam | 68765ab | 2018-08-30 11:46:32 +0530 | [diff] [blame] | 2474 | __func__, action); |
| 2475 | return -EINVAL; |
| 2476 | } |
| 2477 | |
Ramprasad Katkam | 68765ab | 2018-08-30 11:46:32 +0530 | [diff] [blame] | 2478 | return 0; |
| 2479 | } |
| 2480 | |
| 2481 | static void swrm_notify_work_fn(struct work_struct *work) |
| 2482 | { |
| 2483 | struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl, |
Laxminath Kasam | f0128ef | 2018-08-31 15:15:09 +0530 | [diff] [blame] | 2484 | dc_presence_work); |
| 2485 | |
| 2486 | if (!swrm || !swrm->pdev) { |
| 2487 | pr_err("%s: swrm or pdev is NULL\n", __func__); |
| 2488 | return; |
| 2489 | } |
Ramprasad Katkam | 68765ab | 2018-08-30 11:46:32 +0530 | [diff] [blame] | 2490 | swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL); |
| 2491 | } |
| 2492 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2493 | static int swrm_probe(struct platform_device *pdev) |
| 2494 | { |
| 2495 | struct swr_mstr_ctrl *swrm; |
| 2496 | struct swr_ctrl_platform_data *pdata; |
Laxminath Kasam | c7bfab9 | 2019-08-27 16:19:14 +0530 | [diff] [blame] | 2497 | u32 i, num_ports, port_num, port_type, ch_mask, swrm_hctl_reg = 0; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2498 | u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0; |
| 2499 | int ret = 0; |
Sudheer Papothi | 66d6fd1 | 2019-03-27 17:34:48 +0530 | [diff] [blame] | 2500 | struct clk *lpass_core_hw_vote = NULL; |
Sudheer Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 2501 | struct clk *lpass_core_audio = NULL; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2502 | |
| 2503 | /* Allocate soundwire master driver structure */ |
| 2504 | swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl), |
| 2505 | GFP_KERNEL); |
| 2506 | if (!swrm) { |
| 2507 | ret = -ENOMEM; |
| 2508 | goto err_memory_fail; |
| 2509 | } |
Laxminath Kasam | f0128ef | 2018-08-31 15:15:09 +0530 | [diff] [blame] | 2510 | swrm->pdev = pdev; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2511 | swrm->dev = &pdev->dev; |
| 2512 | platform_set_drvdata(pdev, swrm); |
| 2513 | swr_set_ctrl_data(&swrm->master, swrm); |
| 2514 | pdata = dev_get_platdata(&pdev->dev); |
| 2515 | if (!pdata) { |
| 2516 | dev_err(&pdev->dev, "%s: pdata from parent is NULL\n", |
| 2517 | __func__); |
| 2518 | ret = -EINVAL; |
| 2519 | goto err_pdata_fail; |
| 2520 | } |
| 2521 | swrm->handle = (void *)pdata->handle; |
| 2522 | if (!swrm->handle) { |
| 2523 | dev_err(&pdev->dev, "%s: swrm->handle is NULL\n", |
| 2524 | __func__); |
| 2525 | ret = -EINVAL; |
| 2526 | goto err_pdata_fail; |
| 2527 | } |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 2528 | ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr_master_id", |
| 2529 | &swrm->master_id); |
| 2530 | if (ret) { |
| 2531 | dev_err(&pdev->dev, "%s: failed to get master id\n", __func__); |
| 2532 | goto err_pdata_fail; |
| 2533 | } |
Laxminath Kasam | fbcaf32 | 2018-07-18 00:38:14 +0530 | [diff] [blame] | 2534 | if (!(of_property_read_u32(pdev->dev.of_node, |
| 2535 | "swrm-io-base", &swrm->swrm_base_reg))) |
| 2536 | ret = of_property_read_u32(pdev->dev.of_node, |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2537 | "swrm-io-base", &swrm->swrm_base_reg); |
| 2538 | if (!swrm->swrm_base_reg) { |
| 2539 | swrm->read = pdata->read; |
| 2540 | if (!swrm->read) { |
| 2541 | dev_err(&pdev->dev, "%s: swrm->read is NULL\n", |
| 2542 | __func__); |
| 2543 | ret = -EINVAL; |
| 2544 | goto err_pdata_fail; |
| 2545 | } |
| 2546 | swrm->write = pdata->write; |
| 2547 | if (!swrm->write) { |
| 2548 | dev_err(&pdev->dev, "%s: swrm->write is NULL\n", |
| 2549 | __func__); |
| 2550 | ret = -EINVAL; |
| 2551 | goto err_pdata_fail; |
| 2552 | } |
| 2553 | swrm->bulk_write = pdata->bulk_write; |
| 2554 | if (!swrm->bulk_write) { |
| 2555 | dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n", |
| 2556 | __func__); |
| 2557 | ret = -EINVAL; |
| 2558 | goto err_pdata_fail; |
| 2559 | } |
| 2560 | } else { |
| 2561 | swrm->swrm_dig_base = devm_ioremap(&pdev->dev, |
| 2562 | swrm->swrm_base_reg, SWRM_MAX_REGISTER); |
| 2563 | } |
| 2564 | |
Karthikeyan Mani | 1d750fe | 2019-09-06 14:36:09 -0700 | [diff] [blame] | 2565 | swrm->core_vote = pdata->core_vote; |
Laxminath Kasam | c7bfab9 | 2019-08-27 16:19:14 +0530 | [diff] [blame] | 2566 | if (!(of_property_read_u32(pdev->dev.of_node, |
| 2567 | "qcom,swrm-hctl-reg", &swrm_hctl_reg))) |
| 2568 | swrm->swrm_hctl_reg = devm_ioremap(&pdev->dev, |
| 2569 | swrm_hctl_reg, 0x4); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2570 | swrm->clk = pdata->clk; |
| 2571 | if (!swrm->clk) { |
| 2572 | dev_err(&pdev->dev, "%s: swrm->clk is NULL\n", |
| 2573 | __func__); |
| 2574 | ret = -EINVAL; |
| 2575 | goto err_pdata_fail; |
| 2576 | } |
Laxminath Kasam | ea6cbee | 2020-04-28 00:02:32 +0530 | [diff] [blame] | 2577 | swrm->pinctrl_setup = pdata->pinctrl_setup; |
| 2578 | |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 2579 | if (of_property_read_u32(pdev->dev.of_node, |
| 2580 | "qcom,swr-clock-stop-mode0", |
| 2581 | &swrm->clk_stop_mode0_supp)) { |
| 2582 | swrm->clk_stop_mode0_supp = FALSE; |
| 2583 | } |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 2584 | |
| 2585 | ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev", |
| 2586 | &swrm->num_dev); |
| 2587 | if (ret) { |
| 2588 | dev_dbg(&pdev->dev, "%s: Looking up %s property failed\n", |
| 2589 | __func__, "qcom,swr-num-dev"); |
| 2590 | } else { |
Sudheer Papothi | ae5c363 | 2019-11-27 06:52:06 +0530 | [diff] [blame] | 2591 | if (swrm->num_dev > SWRM_NUM_AUTO_ENUM_SLAVES) { |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 2592 | dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n", |
Sudheer Papothi | ae5c363 | 2019-11-27 06:52:06 +0530 | [diff] [blame] | 2593 | __func__, swrm->num_dev, |
| 2594 | SWRM_NUM_AUTO_ENUM_SLAVES); |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 2595 | ret = -EINVAL; |
| 2596 | goto err_pdata_fail; |
| 2597 | } |
| 2598 | } |
| 2599 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2600 | /* Parse soundwire port mapping */ |
| 2601 | ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports", |
| 2602 | &num_ports); |
| 2603 | if (ret) { |
| 2604 | dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__); |
| 2605 | goto err_pdata_fail; |
| 2606 | } |
| 2607 | swrm->num_ports = num_ports; |
| 2608 | |
| 2609 | if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping", |
| 2610 | &map_size)) { |
| 2611 | dev_err(swrm->dev, "missing port mapping\n"); |
| 2612 | goto err_pdata_fail; |
| 2613 | } |
| 2614 | |
| 2615 | map_length = map_size / (3 * sizeof(u32)); |
| 2616 | if (num_ports > SWR_MSTR_PORT_LEN) { |
| 2617 | dev_err(&pdev->dev, "%s:invalid number of swr ports\n", |
| 2618 | __func__); |
| 2619 | ret = -EINVAL; |
| 2620 | goto err_pdata_fail; |
| 2621 | } |
| 2622 | temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL); |
| 2623 | |
| 2624 | if (!temp) { |
| 2625 | ret = -ENOMEM; |
| 2626 | goto err_pdata_fail; |
| 2627 | } |
| 2628 | ret = of_property_read_u32_array(pdev->dev.of_node, |
| 2629 | "qcom,swr-port-mapping", temp, 3 * map_length); |
| 2630 | if (ret) { |
| 2631 | dev_err(swrm->dev, "%s: Failed to read port mapping\n", |
| 2632 | __func__); |
| 2633 | goto err_pdata_fail; |
| 2634 | } |
| 2635 | |
| 2636 | for (i = 0; i < map_length; i++) { |
| 2637 | port_num = temp[3 * i]; |
| 2638 | port_type = temp[3 * i + 1]; |
| 2639 | ch_mask = temp[3 * i + 2]; |
| 2640 | |
| 2641 | if (port_num != old_port_num) |
| 2642 | ch_iter = 0; |
| 2643 | swrm->port_mapping[port_num][ch_iter].port_type = port_type; |
| 2644 | swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask; |
| 2645 | old_port_num = port_num; |
| 2646 | } |
| 2647 | devm_kfree(&pdev->dev, temp); |
| 2648 | |
| 2649 | swrm->reg_irq = pdata->reg_irq; |
| 2650 | swrm->master.read = swrm_read; |
| 2651 | swrm->master.write = swrm_write; |
| 2652 | swrm->master.bulk_write = swrm_bulk_write; |
| 2653 | swrm->master.get_logical_dev_num = swrm_get_logical_dev_num; |
| 2654 | swrm->master.connect_port = swrm_connect_port; |
| 2655 | swrm->master.disconnect_port = swrm_disconnect_port; |
| 2656 | swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control; |
| 2657 | swrm->master.remove_from_group = swrm_remove_from_group; |
Sudheer Papothi | 6abd2de | 2018-09-05 05:57:04 +0530 | [diff] [blame] | 2658 | swrm->master.device_wakeup_vote = swrm_device_wakeup_vote; |
| 2659 | swrm->master.device_wakeup_unvote = swrm_device_wakeup_unvote; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2660 | swrm->master.dev.parent = &pdev->dev; |
| 2661 | swrm->master.dev.of_node = pdev->dev.of_node; |
| 2662 | swrm->master.num_port = 0; |
| 2663 | swrm->rcmd_id = 0; |
| 2664 | swrm->wcmd_id = 0; |
| 2665 | swrm->slave_status = 0; |
| 2666 | swrm->num_rx_chs = 0; |
| 2667 | swrm->clk_ref_count = 0; |
Vatsal Bucha | df38c3e | 2019-03-11 17:10:23 +0530 | [diff] [blame] | 2668 | swrm->swr_irq_wakeup_capable = 0; |
Laxminath Kasam | b0f27cd | 2018-09-06 12:17:11 +0530 | [diff] [blame] | 2669 | swrm->mclk_freq = MCLK_FREQ; |
Sudheer Papothi | ac0ae1c | 2019-10-17 05:38:40 +0530 | [diff] [blame] | 2670 | swrm->bus_clk = MCLK_FREQ; |
Laxminath Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 2671 | swrm->dev_up = true; |
Ramprasad Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 2672 | swrm->state = SWR_MSTR_UP; |
Aditya Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 2673 | swrm->ipc_wakeup = false; |
| 2674 | swrm->ipc_wakeup_triggered = false; |
Vatsal Bucha | 687f982 | 2020-04-01 18:21:41 +0530 | [diff] [blame] | 2675 | swrm->disable_div2_clk_switch = FALSE; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2676 | init_completion(&swrm->reset); |
| 2677 | init_completion(&swrm->broadcast); |
Ramprasad Katkam | 6bce2e7 | 2018-10-10 19:20:13 +0530 | [diff] [blame] | 2678 | init_completion(&swrm->clk_off_complete); |
Vatsal Bucha | 8bcaeab | 2019-10-31 11:45:36 +0530 | [diff] [blame] | 2679 | mutex_init(&swrm->irq_lock); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2680 | mutex_init(&swrm->mlock); |
| 2681 | mutex_init(&swrm->reslock); |
| 2682 | mutex_init(&swrm->force_down_lock); |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 2683 | mutex_init(&swrm->iolock); |
Ramprasad Katkam | 6bce2e7 | 2018-10-10 19:20:13 +0530 | [diff] [blame] | 2684 | mutex_init(&swrm->clklock); |
Laxminath Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 2685 | mutex_init(&swrm->devlock); |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 2686 | mutex_init(&swrm->pm_lock); |
| 2687 | swrm->wlock_holders = 0; |
| 2688 | swrm->pm_state = SWRM_PM_SLEEPABLE; |
| 2689 | init_waitqueue_head(&swrm->pm_wq); |
| 2690 | pm_qos_add_request(&swrm->pm_qos_req, |
| 2691 | PM_QOS_CPU_DMA_LATENCY, |
| 2692 | PM_QOS_DEFAULT_VALUE); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2693 | |
| 2694 | for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++) |
| 2695 | INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list); |
| 2696 | |
Vatsal Bucha | 687f982 | 2020-04-01 18:21:41 +0530 | [diff] [blame] | 2697 | if (of_property_read_u32(pdev->dev.of_node, |
| 2698 | "qcom,disable-div2-clk-switch", |
| 2699 | &swrm->disable_div2_clk_switch)) { |
| 2700 | swrm->disable_div2_clk_switch = FALSE; |
| 2701 | } |
| 2702 | |
Sudheer Papothi | 06f4341 | 2019-07-09 03:32:54 +0530 | [diff] [blame] | 2703 | /* Register LPASS core hw vote */ |
| 2704 | lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote"); |
| 2705 | if (IS_ERR(lpass_core_hw_vote)) { |
| 2706 | ret = PTR_ERR(lpass_core_hw_vote); |
| 2707 | dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n", |
| 2708 | __func__, "lpass_core_hw_vote", ret); |
| 2709 | lpass_core_hw_vote = NULL; |
| 2710 | ret = 0; |
| 2711 | } |
| 2712 | swrm->lpass_core_hw_vote = lpass_core_hw_vote; |
| 2713 | |
| 2714 | /* Register LPASS audio core vote */ |
| 2715 | lpass_core_audio = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote"); |
| 2716 | if (IS_ERR(lpass_core_audio)) { |
| 2717 | ret = PTR_ERR(lpass_core_audio); |
| 2718 | dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n", |
| 2719 | __func__, "lpass_core_audio", ret); |
| 2720 | lpass_core_audio = NULL; |
| 2721 | ret = 0; |
| 2722 | } |
| 2723 | swrm->lpass_core_audio = lpass_core_audio; |
| 2724 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2725 | if (swrm->reg_irq) { |
| 2726 | ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm, |
| 2727 | SWR_IRQ_REGISTER); |
| 2728 | if (ret) { |
| 2729 | dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n", |
| 2730 | __func__, ret); |
| 2731 | goto err_irq_fail; |
| 2732 | } |
| 2733 | } else { |
| 2734 | swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq"); |
| 2735 | if (swrm->irq < 0) { |
| 2736 | dev_err(swrm->dev, "%s() error getting irq hdle: %d\n", |
| 2737 | __func__, swrm->irq); |
Laxminath Kasam | fbcaf32 | 2018-07-18 00:38:14 +0530 | [diff] [blame] | 2738 | goto err_irq_fail; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2739 | } |
| 2740 | |
| 2741 | ret = request_threaded_irq(swrm->irq, NULL, |
Sudheer Papothi | d19d0c5 | 2019-02-23 05:41:39 +0530 | [diff] [blame] | 2742 | swr_mstr_interrupt_v2, |
Ramprasad Katkam | 8330351 | 2018-10-11 17:34:22 +0530 | [diff] [blame] | 2743 | IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2744 | "swr_master_irq", swrm); |
| 2745 | if (ret) { |
| 2746 | dev_err(swrm->dev, "%s: Failed to request irq %d\n", |
| 2747 | __func__, ret); |
| 2748 | goto err_irq_fail; |
| 2749 | } |
| 2750 | |
| 2751 | } |
Vatsal Bucha | df38c3e | 2019-03-11 17:10:23 +0530 | [diff] [blame] | 2752 | /* Make inband tx interrupts as wakeup capable for slave irq */ |
| 2753 | ret = of_property_read_u32(pdev->dev.of_node, |
| 2754 | "qcom,swr-mstr-irq-wakeup-capable", |
| 2755 | &swrm->swr_irq_wakeup_capable); |
| 2756 | if (ret) |
| 2757 | dev_dbg(swrm->dev, "%s: swrm irq wakeup capable not defined\n", |
| 2758 | __func__); |
| 2759 | if (swrm->swr_irq_wakeup_capable) |
| 2760 | irq_set_irq_wake(swrm->irq, 1); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2761 | ret = swr_register_master(&swrm->master); |
| 2762 | if (ret) { |
| 2763 | dev_err(&pdev->dev, "%s: error adding swr master\n", __func__); |
| 2764 | goto err_mstr_fail; |
| 2765 | } |
| 2766 | |
| 2767 | /* Add devices registered with board-info as the |
| 2768 | * controller will be up now |
| 2769 | */ |
| 2770 | swr_master_add_boarddevices(&swrm->master); |
| 2771 | mutex_lock(&swrm->mlock); |
| 2772 | swrm_clk_request(swrm, true); |
Laxminath Kasam | 4696fff | 2019-11-26 16:07:11 +0530 | [diff] [blame] | 2773 | swrm->version = swr_master_read(swrm, SWRM_COMP_HW_VERSION); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2774 | ret = swrm_master_init(swrm); |
| 2775 | if (ret < 0) { |
| 2776 | dev_err(&pdev->dev, |
| 2777 | "%s: Error in master Initialization , err %d\n", |
| 2778 | __func__, ret); |
| 2779 | mutex_unlock(&swrm->mlock); |
Laxminath Kasam | 3695642 | 2020-06-01 19:23:48 +0530 | [diff] [blame] | 2780 | ret = -EPROBE_DEFER; |
Laxminath Kasam | ec8c909 | 2019-12-17 13:12:58 +0530 | [diff] [blame] | 2781 | goto err_mstr_init_fail; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2782 | } |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2783 | |
| 2784 | mutex_unlock(&swrm->mlock); |
Laxminath Kasam | f0128ef | 2018-08-31 15:15:09 +0530 | [diff] [blame] | 2785 | INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2786 | |
| 2787 | if (pdev->dev.of_node) |
| 2788 | of_register_swr_devices(&swrm->master); |
| 2789 | |
Sudheer Papothi | 96c842a | 2019-08-29 12:11:21 +0530 | [diff] [blame] | 2790 | #ifdef CONFIG_DEBUG_FS |
| 2791 | swrm->debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0); |
| 2792 | if (!IS_ERR(swrm->debugfs_swrm_dent)) { |
| 2793 | swrm->debugfs_peek = debugfs_create_file("swrm_peek", |
| 2794 | S_IFREG | 0444, swrm->debugfs_swrm_dent, |
| 2795 | (void *) swrm, &swrm_debug_read_ops); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2796 | |
Sudheer Papothi | 96c842a | 2019-08-29 12:11:21 +0530 | [diff] [blame] | 2797 | swrm->debugfs_poke = debugfs_create_file("swrm_poke", |
| 2798 | S_IFREG | 0444, swrm->debugfs_swrm_dent, |
| 2799 | (void *) swrm, &swrm_debug_write_ops); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2800 | |
Sudheer Papothi | 96c842a | 2019-08-29 12:11:21 +0530 | [diff] [blame] | 2801 | swrm->debugfs_reg_dump = debugfs_create_file("swrm_reg_dump", |
| 2802 | S_IFREG | 0444, swrm->debugfs_swrm_dent, |
| 2803 | (void *) swrm, |
| 2804 | &swrm_debug_dump_ops); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2805 | } |
Sudheer Papothi | 96c842a | 2019-08-29 12:11:21 +0530 | [diff] [blame] | 2806 | #endif |
Vatsal Bucha | df38c3e | 2019-03-11 17:10:23 +0530 | [diff] [blame] | 2807 | ret = device_init_wakeup(swrm->dev, true); |
| 2808 | if (ret) { |
| 2809 | dev_err(swrm->dev, "Device wakeup init failed: %d\n", ret); |
| 2810 | goto err_irq_wakeup_fail; |
| 2811 | } |
| 2812 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2813 | pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer); |
| 2814 | pm_runtime_use_autosuspend(&pdev->dev); |
| 2815 | pm_runtime_set_active(&pdev->dev); |
| 2816 | pm_runtime_enable(&pdev->dev); |
| 2817 | pm_runtime_mark_last_busy(&pdev->dev); |
| 2818 | |
Ramprasad Katkam | 68765ab | 2018-08-30 11:46:32 +0530 | [diff] [blame] | 2819 | INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn); |
| 2820 | swrm->event_notifier.notifier_call = swrm_event_notify; |
| 2821 | msm_aud_evt_register_client(&swrm->event_notifier); |
| 2822 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2823 | return 0; |
Vatsal Bucha | df38c3e | 2019-03-11 17:10:23 +0530 | [diff] [blame] | 2824 | err_irq_wakeup_fail: |
| 2825 | device_init_wakeup(swrm->dev, false); |
Laxminath Kasam | ec8c909 | 2019-12-17 13:12:58 +0530 | [diff] [blame] | 2826 | err_mstr_init_fail: |
| 2827 | swr_unregister_master(&swrm->master); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2828 | err_mstr_fail: |
Laxminath Kasam | 3695642 | 2020-06-01 19:23:48 +0530 | [diff] [blame] | 2829 | if (swrm->reg_irq) { |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2830 | swrm->reg_irq(swrm->handle, swr_mstr_interrupt, |
| 2831 | swrm, SWR_IRQ_FREE); |
Laxminath Kasam | 3695642 | 2020-06-01 19:23:48 +0530 | [diff] [blame] | 2832 | } else if (swrm->irq) { |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2833 | free_irq(swrm->irq, swrm); |
Laxminath Kasam | 3695642 | 2020-06-01 19:23:48 +0530 | [diff] [blame] | 2834 | irqd_set_trigger_type( |
| 2835 | irq_get_irq_data(swrm->irq), |
| 2836 | IRQ_TYPE_NONE); |
| 2837 | } |
| 2838 | if (swrm->swr_irq_wakeup_capable) |
| 2839 | irq_set_irq_wake(swrm->irq, 0); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2840 | err_irq_fail: |
Vatsal Bucha | 8bcaeab | 2019-10-31 11:45:36 +0530 | [diff] [blame] | 2841 | mutex_destroy(&swrm->irq_lock); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2842 | mutex_destroy(&swrm->mlock); |
| 2843 | mutex_destroy(&swrm->reslock); |
| 2844 | mutex_destroy(&swrm->force_down_lock); |
Ramprasad Katkam | 1f22126 | 2018-08-23 15:01:22 +0530 | [diff] [blame] | 2845 | mutex_destroy(&swrm->iolock); |
Ramprasad Katkam | 6bce2e7 | 2018-10-10 19:20:13 +0530 | [diff] [blame] | 2846 | mutex_destroy(&swrm->clklock); |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 2847 | mutex_destroy(&swrm->pm_lock); |
| 2848 | pm_qos_remove_request(&swrm->pm_qos_req); |
| 2849 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2850 | err_pdata_fail: |
| 2851 | err_memory_fail: |
| 2852 | return ret; |
| 2853 | } |
| 2854 | |
| 2855 | static int swrm_remove(struct platform_device *pdev) |
| 2856 | { |
| 2857 | struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev); |
| 2858 | |
Laxminath Kasam | 3695642 | 2020-06-01 19:23:48 +0530 | [diff] [blame] | 2859 | if (swrm->reg_irq) { |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2860 | swrm->reg_irq(swrm->handle, swr_mstr_interrupt, |
| 2861 | swrm, SWR_IRQ_FREE); |
Laxminath Kasam | 3695642 | 2020-06-01 19:23:48 +0530 | [diff] [blame] | 2862 | } else if (swrm->irq) { |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2863 | free_irq(swrm->irq, swrm); |
Laxminath Kasam | 3695642 | 2020-06-01 19:23:48 +0530 | [diff] [blame] | 2864 | irqd_set_trigger_type( |
| 2865 | irq_get_irq_data(swrm->irq), |
| 2866 | IRQ_TYPE_NONE); |
| 2867 | } else if (swrm->wake_irq > 0) { |
Aditya Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 2868 | free_irq(swrm->wake_irq, swrm); |
Laxminath Kasam | 3695642 | 2020-06-01 19:23:48 +0530 | [diff] [blame] | 2869 | } |
Vatsal Bucha | df38c3e | 2019-03-11 17:10:23 +0530 | [diff] [blame] | 2870 | if (swrm->swr_irq_wakeup_capable) |
| 2871 | irq_set_irq_wake(swrm->irq, 0); |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 2872 | cancel_work_sync(&swrm->wakeup_work); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2873 | pm_runtime_disable(&pdev->dev); |
| 2874 | pm_runtime_set_suspended(&pdev->dev); |
| 2875 | swr_unregister_master(&swrm->master); |
Ramprasad Katkam | 68765ab | 2018-08-30 11:46:32 +0530 | [diff] [blame] | 2876 | msm_aud_evt_unregister_client(&swrm->event_notifier); |
Vatsal Bucha | df38c3e | 2019-03-11 17:10:23 +0530 | [diff] [blame] | 2877 | device_init_wakeup(swrm->dev, false); |
Vatsal Bucha | 8bcaeab | 2019-10-31 11:45:36 +0530 | [diff] [blame] | 2878 | mutex_destroy(&swrm->irq_lock); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2879 | mutex_destroy(&swrm->mlock); |
| 2880 | mutex_destroy(&swrm->reslock); |
Ramprasad Katkam | 6bce2e7 | 2018-10-10 19:20:13 +0530 | [diff] [blame] | 2881 | mutex_destroy(&swrm->iolock); |
| 2882 | mutex_destroy(&swrm->clklock); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2883 | mutex_destroy(&swrm->force_down_lock); |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 2884 | mutex_destroy(&swrm->pm_lock); |
| 2885 | pm_qos_remove_request(&swrm->pm_qos_req); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2886 | devm_kfree(&pdev->dev, swrm); |
| 2887 | return 0; |
| 2888 | } |
| 2889 | |
| 2890 | static int swrm_clk_pause(struct swr_mstr_ctrl *swrm) |
| 2891 | { |
| 2892 | u32 val; |
| 2893 | |
| 2894 | dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state); |
| 2895 | swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD); |
| 2896 | val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR); |
| 2897 | val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK; |
| 2898 | swr_master_write(swrm, SWRM_MCP_CFG_ADDR, val); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2899 | |
| 2900 | return 0; |
| 2901 | } |
| 2902 | |
| 2903 | #ifdef CONFIG_PM |
| 2904 | static int swrm_runtime_resume(struct device *dev) |
| 2905 | { |
| 2906 | struct platform_device *pdev = to_platform_device(dev); |
| 2907 | struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev); |
| 2908 | int ret = 0; |
Vatsal Bucha | e50b500 | 2019-09-19 14:32:20 +0530 | [diff] [blame] | 2909 | bool swrm_clk_req_err = false; |
Sudheer Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 2910 | bool hw_core_err = false; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2911 | struct swr_master *mstr = &swrm->master; |
| 2912 | struct swr_device *swr_dev; |
| 2913 | |
| 2914 | dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n", |
| 2915 | __func__, swrm->state); |
Aditya Bavanari | f500a1d | 2019-09-16 18:27:51 -0700 | [diff] [blame] | 2916 | trace_printk("%s: pm_runtime: resume, state:%d\n", |
| 2917 | __func__, swrm->state); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2918 | mutex_lock(&swrm->reslock); |
Ramprasad Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 2919 | |
Sudheer Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 2920 | if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) { |
| 2921 | dev_err(dev, "%s:lpass core hw enable failed\n", |
| 2922 | __func__); |
| 2923 | hw_core_err = true; |
| 2924 | } |
| 2925 | if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) { |
| 2926 | dev_err(dev, "%s:lpass audio hw enable failed\n", |
| 2927 | __func__); |
Laxminath Kasam | ea6cbee | 2020-04-28 00:02:32 +0530 | [diff] [blame] | 2928 | swrm->aud_core_err = true; |
Karthikeyan Mani | f682190 | 2019-05-21 17:31:24 -0700 | [diff] [blame] | 2929 | } |
Sudheer Papothi | 66d6fd1 | 2019-03-27 17:34:48 +0530 | [diff] [blame] | 2930 | |
Ramprasad Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 2931 | if ((swrm->state == SWR_MSTR_DOWN) || |
| 2932 | (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) { |
Aditya Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 2933 | if (swrm->clk_stop_mode0_supp) { |
Vatsal Bucha | 8bcaeab | 2019-10-31 11:45:36 +0530 | [diff] [blame] | 2934 | if (swrm->wake_irq > 0) { |
| 2935 | if (unlikely(!irq_get_irq_data |
| 2936 | (swrm->wake_irq))) { |
| 2937 | pr_err("%s: irq data is NULL\n", |
| 2938 | __func__); |
| 2939 | mutex_unlock(&swrm->reslock); |
| 2940 | return IRQ_NONE; |
| 2941 | } |
| 2942 | mutex_lock(&swrm->irq_lock); |
| 2943 | if (!irqd_irq_disabled( |
| 2944 | irq_get_irq_data(swrm->wake_irq))) |
| 2945 | disable_irq_nosync(swrm->wake_irq); |
| 2946 | mutex_unlock(&swrm->irq_lock); |
Laxminath Kasam | ea6cbee | 2020-04-28 00:02:32 +0530 | [diff] [blame] | 2947 | if (swrm->dmic_sva && swrm->pinctrl_setup) |
| 2948 | swrm->pinctrl_setup(swrm->handle, |
| 2949 | false); |
Vatsal Bucha | 8bcaeab | 2019-10-31 11:45:36 +0530 | [diff] [blame] | 2950 | } |
Aditya Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 2951 | if (swrm->ipc_wakeup) |
| 2952 | msm_aud_evt_blocking_notifier_call_chain( |
| 2953 | SWR_WAKE_IRQ_DEREGISTER, (void *)swrm); |
Laxminath Kasam | f0128ef | 2018-08-31 15:15:09 +0530 | [diff] [blame] | 2954 | } |
| 2955 | |
Vatsal Bucha | 63b193f | 2019-08-12 11:56:55 +0530 | [diff] [blame] | 2956 | if (swrm_clk_request(swrm, true)) { |
| 2957 | /* |
| 2958 | * Set autosuspend timer to 1 for |
| 2959 | * master to enter into suspend. |
| 2960 | */ |
Vatsal Bucha | e50b500 | 2019-09-19 14:32:20 +0530 | [diff] [blame] | 2961 | swrm_clk_req_err = true; |
Ramprasad Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 2962 | goto exit; |
Vatsal Bucha | 63b193f | 2019-08-12 11:56:55 +0530 | [diff] [blame] | 2963 | } |
Ramprasad Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 2964 | if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) { |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 2965 | list_for_each_entry(swr_dev, &mstr->devices, dev_list) { |
| 2966 | ret = swr_device_up(swr_dev); |
Sudheer Papothi | 79c9075 | 2019-04-23 06:09:52 +0530 | [diff] [blame] | 2967 | if (ret == -ENODEV) { |
| 2968 | dev_dbg(dev, |
| 2969 | "%s slave device up not implemented\n", |
| 2970 | __func__); |
Aditya Bavanari | f500a1d | 2019-09-16 18:27:51 -0700 | [diff] [blame] | 2971 | trace_printk( |
| 2972 | "%s slave device up not implemented\n", |
| 2973 | __func__); |
Sudheer Papothi | 79c9075 | 2019-04-23 06:09:52 +0530 | [diff] [blame] | 2974 | ret = 0; |
| 2975 | } else if (ret) { |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 2976 | dev_err(dev, |
| 2977 | "%s: failed to wakeup swr dev %d\n", |
| 2978 | __func__, swr_dev->dev_num); |
| 2979 | swrm_clk_request(swrm, false); |
| 2980 | goto exit; |
| 2981 | } |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 2982 | } |
Ramprasad Katkam | 48b49b2 | 2018-10-01 20:12:46 +0530 | [diff] [blame] | 2983 | swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01); |
| 2984 | swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01); |
| 2985 | swrm_master_init(swrm); |
Ramprasad Katkam | 2e85a54 | 2019-04-26 18:28:31 +0530 | [diff] [blame] | 2986 | /* wait for hw enumeration to complete */ |
| 2987 | usleep_range(100, 105); |
Laxminath Kasam | e229197 | 2019-11-08 14:51:59 +0530 | [diff] [blame] | 2988 | if (!swrm_check_link_status(swrm, 0x1)) |
Laxminath Kasam | 696b14b | 2019-12-03 22:07:34 +0530 | [diff] [blame] | 2989 | dev_dbg(dev, "%s:failed in connecting, ssr?\n", |
| 2990 | __func__); |
Ramprasad Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 2991 | swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, 0x0, |
| 2992 | SWRS_SCP_INT_STATUS_MASK_1); |
Karthikeyan Mani | f682190 | 2019-05-21 17:31:24 -0700 | [diff] [blame] | 2993 | if (swrm->state == SWR_MSTR_SSR) { |
| 2994 | mutex_unlock(&swrm->reslock); |
| 2995 | enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL); |
| 2996 | mutex_lock(&swrm->reslock); |
| 2997 | } |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 2998 | } else { |
| 2999 | /*wake up from clock stop*/ |
| 3000 | swr_master_write(swrm, SWRM_MCP_BUS_CTRL_ADDR, 0x2); |
Sudheer Papothi | 55fa597 | 2019-09-28 02:50:27 +0530 | [diff] [blame] | 3001 | /* clear and enable bus clash interrupt */ |
| 3002 | swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x08); |
| 3003 | swrm->intr_mask |= 0x08; |
| 3004 | swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, |
| 3005 | swrm->intr_mask); |
| 3006 | swr_master_write(swrm, |
| 3007 | SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, |
| 3008 | swrm->intr_mask); |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 3009 | usleep_range(100, 105); |
Laxminath Kasam | e229197 | 2019-11-08 14:51:59 +0530 | [diff] [blame] | 3010 | if (!swrm_check_link_status(swrm, 0x1)) |
Laxminath Kasam | 696b14b | 2019-12-03 22:07:34 +0530 | [diff] [blame] | 3011 | dev_dbg(dev, "%s:failed in connecting, ssr?\n", |
| 3012 | __func__); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3013 | } |
Ramprasad Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 3014 | swrm->state = SWR_MSTR_UP; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3015 | } |
| 3016 | exit: |
Laxminath Kasam | ea6cbee | 2020-04-28 00:02:32 +0530 | [diff] [blame] | 3017 | if (ret && !swrm->aud_core_err) |
Sudheer Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 3018 | swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false); |
| 3019 | if (!hw_core_err) |
| 3020 | swrm_request_hw_vote(swrm, LPASS_HW_CORE, false); |
Vatsal Bucha | e50b500 | 2019-09-19 14:32:20 +0530 | [diff] [blame] | 3021 | if (swrm_clk_req_err) |
| 3022 | pm_runtime_set_autosuspend_delay(&pdev->dev, |
| 3023 | ERR_AUTO_SUSPEND_TIMER_VAL); |
| 3024 | else |
| 3025 | pm_runtime_set_autosuspend_delay(&pdev->dev, |
| 3026 | auto_suspend_timer); |
Vatsal Bucha | bd68a7c | 2020-06-08 10:41:56 +0530 | [diff] [blame] | 3027 | if (swrm->req_clk_switch) |
| 3028 | swrm->req_clk_switch = false; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3029 | mutex_unlock(&swrm->reslock); |
Sudheer Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 3030 | |
Aditya Bavanari | f500a1d | 2019-09-16 18:27:51 -0700 | [diff] [blame] | 3031 | trace_printk("%s: pm_runtime: resume done, state:%d\n", |
| 3032 | __func__, swrm->state); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3033 | return ret; |
| 3034 | } |
| 3035 | |
| 3036 | static int swrm_runtime_suspend(struct device *dev) |
| 3037 | { |
| 3038 | struct platform_device *pdev = to_platform_device(dev); |
| 3039 | struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev); |
| 3040 | int ret = 0; |
Sudheer Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 3041 | bool hw_core_err = false; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3042 | struct swr_master *mstr = &swrm->master; |
| 3043 | struct swr_device *swr_dev; |
| 3044 | int current_state = 0; |
| 3045 | |
Aditya Bavanari | f500a1d | 2019-09-16 18:27:51 -0700 | [diff] [blame] | 3046 | trace_printk("%s: pm_runtime: suspend state: %d\n", |
| 3047 | __func__, swrm->state); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3048 | dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n", |
| 3049 | __func__, swrm->state); |
| 3050 | mutex_lock(&swrm->reslock); |
| 3051 | mutex_lock(&swrm->force_down_lock); |
| 3052 | current_state = swrm->state; |
| 3053 | mutex_unlock(&swrm->force_down_lock); |
Sudheer Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 3054 | |
| 3055 | if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) { |
| 3056 | dev_err(dev, "%s:lpass core hw enable failed\n", |
| 3057 | __func__); |
| 3058 | hw_core_err = true; |
| 3059 | } |
Sudheer Papothi | 66d6fd1 | 2019-03-27 17:34:48 +0530 | [diff] [blame] | 3060 | |
Ramprasad Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 3061 | if ((current_state == SWR_MSTR_UP) || |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3062 | (current_state == SWR_MSTR_SSR)) { |
| 3063 | |
| 3064 | if ((current_state != SWR_MSTR_SSR) && |
| 3065 | swrm_is_port_en(&swrm->master)) { |
| 3066 | dev_dbg(dev, "%s ports are enabled\n", __func__); |
Aditya Bavanari | f500a1d | 2019-09-16 18:27:51 -0700 | [diff] [blame] | 3067 | trace_printk("%s ports are enabled\n", __func__); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3068 | ret = -EBUSY; |
| 3069 | goto exit; |
| 3070 | } |
Ramprasad Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 3071 | if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) { |
Aditya Bavanari | f500a1d | 2019-09-16 18:27:51 -0700 | [diff] [blame] | 3072 | dev_err(dev, "%s: clk stop mode not supported or SSR entry\n", |
| 3073 | __func__); |
Sudheer Papothi | 06f4341 | 2019-07-09 03:32:54 +0530 | [diff] [blame] | 3074 | mutex_unlock(&swrm->reslock); |
Ramprasad Katkam | b4c7c68 | 2018-12-19 18:58:36 +0530 | [diff] [blame] | 3075 | enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL); |
Sudheer Papothi | 06f4341 | 2019-07-09 03:32:54 +0530 | [diff] [blame] | 3076 | mutex_lock(&swrm->reslock); |
Sudheer Papothi | 1f2565b | 2020-01-30 05:22:47 +0530 | [diff] [blame] | 3077 | if (!swrm->clk_stop_mode0_supp) { |
| 3078 | swrm_clk_pause(swrm); |
| 3079 | } else { |
| 3080 | /* Mask bus clash interrupt */ |
| 3081 | swrm->intr_mask &= ~((u32)0x08); |
| 3082 | swr_master_write(swrm, |
| 3083 | SWRM_INTERRUPT_MASK_ADDR, |
| 3084 | swrm->intr_mask); |
| 3085 | swr_master_write(swrm, |
| 3086 | SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, |
| 3087 | swrm->intr_mask); |
| 3088 | mutex_unlock(&swrm->reslock); |
| 3089 | /* clock stop sequence */ |
| 3090 | swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF, |
| 3091 | SWRS_SCP_CONTROL); |
| 3092 | mutex_lock(&swrm->reslock); |
| 3093 | } |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 3094 | swr_master_write(swrm, SWRM_COMP_CFG_ADDR, 0x00); |
| 3095 | list_for_each_entry(swr_dev, &mstr->devices, dev_list) { |
| 3096 | ret = swr_device_down(swr_dev); |
Sudheer Papothi | 79c9075 | 2019-04-23 06:09:52 +0530 | [diff] [blame] | 3097 | if (ret == -ENODEV) { |
| 3098 | dev_dbg_ratelimited(dev, |
| 3099 | "%s slave device down not implemented\n", |
Aditya Bavanari | f500a1d | 2019-09-16 18:27:51 -0700 | [diff] [blame] | 3100 | __func__); |
| 3101 | trace_printk( |
| 3102 | "%s slave device down not implemented\n", |
| 3103 | __func__); |
Sudheer Papothi | 79c9075 | 2019-04-23 06:09:52 +0530 | [diff] [blame] | 3104 | ret = 0; |
| 3105 | } else if (ret) { |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 3106 | dev_err(dev, |
| 3107 | "%s: failed to shutdown swr dev %d\n", |
| 3108 | __func__, swr_dev->dev_num); |
Aditya Bavanari | f500a1d | 2019-09-16 18:27:51 -0700 | [diff] [blame] | 3109 | trace_printk( |
| 3110 | "%s: failed to shutdown swr dev %d\n", |
| 3111 | __func__, swr_dev->dev_num); |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 3112 | goto exit; |
| 3113 | } |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3114 | } |
Aditya Bavanari | f500a1d | 2019-09-16 18:27:51 -0700 | [diff] [blame] | 3115 | trace_printk("%s: clk stop mode not supported or SSR exit\n", |
| 3116 | __func__); |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 3117 | } else { |
Sudheer Papothi | 55fa597 | 2019-09-28 02:50:27 +0530 | [diff] [blame] | 3118 | /* Mask bus clash interrupt */ |
| 3119 | swrm->intr_mask &= ~((u32)0x08); |
| 3120 | swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, |
| 3121 | swrm->intr_mask); |
| 3122 | swr_master_write(swrm, |
| 3123 | SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, |
| 3124 | swrm->intr_mask); |
Sudheer Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 3125 | mutex_unlock(&swrm->reslock); |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 3126 | /* clock stop sequence */ |
| 3127 | swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF, |
| 3128 | SWRS_SCP_CONTROL); |
Sudheer Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 3129 | mutex_lock(&swrm->reslock); |
Ramprasad Katkam | 14f47cc | 2018-07-25 17:20:18 +0530 | [diff] [blame] | 3130 | usleep_range(100, 105); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3131 | } |
Laxminath Kasam | cafe073 | 2019-11-20 17:31:58 +0530 | [diff] [blame] | 3132 | if (!swrm_check_link_status(swrm, 0x0)) |
Laxminath Kasam | 696b14b | 2019-12-03 22:07:34 +0530 | [diff] [blame] | 3133 | dev_dbg(dev, "%s:failed in disconnecting, ssr?\n", |
| 3134 | __func__); |
Karthikeyan Mani | 1d750fe | 2019-09-06 14:36:09 -0700 | [diff] [blame] | 3135 | ret = swrm_clk_request(swrm, false); |
| 3136 | if (ret) { |
| 3137 | dev_err(dev, "%s: swrmn clk failed\n", __func__); |
| 3138 | ret = 0; |
| 3139 | goto exit; |
| 3140 | } |
Ramprasad Katkam | 6a3050d | 2018-10-10 02:08:00 +0530 | [diff] [blame] | 3141 | |
Aditya Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 3142 | if (swrm->clk_stop_mode0_supp) { |
| 3143 | if (swrm->wake_irq > 0) { |
Laxminath Kasam | ea6cbee | 2020-04-28 00:02:32 +0530 | [diff] [blame] | 3144 | if (swrm->dmic_sva && swrm->pinctrl_setup) |
| 3145 | swrm->pinctrl_setup(swrm->handle, true); |
Aditya Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 3146 | enable_irq(swrm->wake_irq); |
| 3147 | } else if (swrm->ipc_wakeup) { |
| 3148 | msm_aud_evt_blocking_notifier_call_chain( |
| 3149 | SWR_WAKE_IRQ_REGISTER, (void *)swrm); |
| 3150 | swrm->ipc_wakeup_triggered = false; |
| 3151 | } |
Ramprasad Katkam | 6a3050d | 2018-10-10 02:08:00 +0530 | [diff] [blame] | 3152 | } |
| 3153 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3154 | } |
Ramprasad Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 3155 | /* Retain SSR state until resume */ |
| 3156 | if (current_state != SWR_MSTR_SSR) |
| 3157 | swrm->state = SWR_MSTR_DOWN; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3158 | exit: |
Laxminath Kasam | ea6cbee | 2020-04-28 00:02:32 +0530 | [diff] [blame] | 3159 | if (!swrm->aud_core_err) |
Sudheer Papothi | 384addd | 2019-06-14 02:26:52 +0530 | [diff] [blame] | 3160 | swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false); |
| 3161 | if (!hw_core_err) |
| 3162 | swrm_request_hw_vote(swrm, LPASS_HW_CORE, false); |
Laxminath Kasam | ea6cbee | 2020-04-28 00:02:32 +0530 | [diff] [blame] | 3163 | swrm->aud_core_err = false; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3164 | mutex_unlock(&swrm->reslock); |
Aditya Bavanari | f500a1d | 2019-09-16 18:27:51 -0700 | [diff] [blame] | 3165 | trace_printk("%s: pm_runtime: suspend done state: %d\n", |
| 3166 | __func__, swrm->state); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3167 | return ret; |
| 3168 | } |
| 3169 | #endif /* CONFIG_PM */ |
| 3170 | |
Sudheer Papothi | 06f4341 | 2019-07-09 03:32:54 +0530 | [diff] [blame] | 3171 | static int swrm_device_suspend(struct device *dev) |
| 3172 | { |
| 3173 | struct platform_device *pdev = to_platform_device(dev); |
| 3174 | struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev); |
| 3175 | int ret = 0; |
| 3176 | |
| 3177 | dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state); |
Aditya Bavanari | f500a1d | 2019-09-16 18:27:51 -0700 | [diff] [blame] | 3178 | trace_printk("%s: swrm state: %d\n", __func__, swrm->state); |
Sudheer Papothi | 06f4341 | 2019-07-09 03:32:54 +0530 | [diff] [blame] | 3179 | if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) { |
| 3180 | ret = swrm_runtime_suspend(dev); |
| 3181 | if (!ret) { |
| 3182 | pm_runtime_disable(dev); |
| 3183 | pm_runtime_set_suspended(dev); |
| 3184 | pm_runtime_enable(dev); |
| 3185 | } |
| 3186 | } |
| 3187 | |
| 3188 | return 0; |
| 3189 | } |
| 3190 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3191 | static int swrm_device_down(struct device *dev) |
| 3192 | { |
| 3193 | struct platform_device *pdev = to_platform_device(dev); |
| 3194 | struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3195 | |
| 3196 | dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state); |
Aditya Bavanari | f500a1d | 2019-09-16 18:27:51 -0700 | [diff] [blame] | 3197 | trace_printk("%s: swrm state: %d\n", __func__, swrm->state); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3198 | |
| 3199 | mutex_lock(&swrm->force_down_lock); |
| 3200 | swrm->state = SWR_MSTR_SSR; |
| 3201 | mutex_unlock(&swrm->force_down_lock); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3202 | |
Ramprasad Katkam | 2e85a54 | 2019-04-26 18:28:31 +0530 | [diff] [blame] | 3203 | swrm_device_suspend(dev); |
Laxminath Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 3204 | return 0; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3205 | } |
| 3206 | |
Aditya Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 3207 | int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm) |
| 3208 | { |
| 3209 | int ret = 0; |
Laxminath Kasam | a60239e | 2019-01-10 14:43:03 +0530 | [diff] [blame] | 3210 | int irq, dir_apps_irq; |
Aditya Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 3211 | |
| 3212 | if (!swrm->ipc_wakeup) { |
Laxminath Kasam | a60239e | 2019-01-10 14:43:03 +0530 | [diff] [blame] | 3213 | irq = of_get_named_gpio(swrm->dev->of_node, |
| 3214 | "qcom,swr-wakeup-irq", 0); |
| 3215 | if (gpio_is_valid(irq)) { |
| 3216 | swrm->wake_irq = gpio_to_irq(irq); |
| 3217 | if (swrm->wake_irq < 0) { |
| 3218 | dev_err(swrm->dev, |
| 3219 | "Unable to configure irq\n"); |
| 3220 | return swrm->wake_irq; |
| 3221 | } |
| 3222 | } else { |
| 3223 | dir_apps_irq = platform_get_irq_byname(swrm->pdev, |
| 3224 | "swr_wake_irq"); |
| 3225 | if (dir_apps_irq < 0) { |
| 3226 | dev_err(swrm->dev, |
| 3227 | "TLMM connect gpio not found\n"); |
| 3228 | return -EINVAL; |
| 3229 | } |
| 3230 | swrm->wake_irq = dir_apps_irq; |
Aditya Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 3231 | } |
Aditya Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 3232 | ret = request_threaded_irq(swrm->wake_irq, NULL, |
| 3233 | swrm_wakeup_interrupt, |
| 3234 | IRQF_TRIGGER_HIGH | IRQF_ONESHOT, |
| 3235 | "swr_wake_irq", swrm); |
| 3236 | if (ret) { |
| 3237 | dev_err(swrm->dev, "%s: Failed to request irq %d\n", |
| 3238 | __func__, ret); |
| 3239 | return -EINVAL; |
| 3240 | } |
Aditya Bavanari | 3517b11 | 2018-12-03 13:26:59 +0530 | [diff] [blame] | 3241 | irq_set_irq_wake(swrm->wake_irq, 1); |
Aditya Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 3242 | } |
| 3243 | return ret; |
| 3244 | } |
| 3245 | |
Sudheer Papothi | 3d1596e | 2018-10-27 06:19:18 +0530 | [diff] [blame] | 3246 | static int swrm_alloc_port_mem(struct device *dev, struct swr_mstr_ctrl *swrm, |
| 3247 | u32 uc, u32 size) |
| 3248 | { |
| 3249 | if (!swrm->port_param) { |
| 3250 | swrm->port_param = devm_kzalloc(dev, |
| 3251 | sizeof(swrm->port_param) * SWR_UC_MAX, |
| 3252 | GFP_KERNEL); |
| 3253 | if (!swrm->port_param) |
| 3254 | return -ENOMEM; |
| 3255 | } |
| 3256 | if (!swrm->port_param[uc]) { |
| 3257 | swrm->port_param[uc] = devm_kcalloc(dev, size, |
| 3258 | sizeof(struct port_params), |
| 3259 | GFP_KERNEL); |
| 3260 | if (!swrm->port_param[uc]) |
| 3261 | return -ENOMEM; |
| 3262 | } else { |
| 3263 | dev_err_ratelimited(swrm->dev, "%s: called more than once\n", |
| 3264 | __func__); |
| 3265 | } |
| 3266 | |
| 3267 | return 0; |
| 3268 | } |
| 3269 | |
| 3270 | static int swrm_copy_port_config(struct swr_mstr_ctrl *swrm, |
| 3271 | struct swrm_port_config *port_cfg, |
| 3272 | u32 size) |
| 3273 | { |
| 3274 | int idx; |
| 3275 | struct port_params *params; |
| 3276 | int uc = port_cfg->uc; |
| 3277 | int ret = 0; |
| 3278 | |
| 3279 | for (idx = 0; idx < size; idx++) { |
| 3280 | params = &((struct port_params *)port_cfg->params)[idx]; |
| 3281 | if (!params) { |
| 3282 | dev_err(swrm->dev, "%s: Invalid params\n", __func__); |
| 3283 | ret = -EINVAL; |
| 3284 | break; |
| 3285 | } |
| 3286 | memcpy(&swrm->port_param[uc][idx], params, |
| 3287 | sizeof(struct port_params)); |
| 3288 | } |
| 3289 | |
| 3290 | return ret; |
| 3291 | } |
| 3292 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3293 | /** |
| 3294 | * swrm_wcd_notify - parent device can notify to soundwire master through |
| 3295 | * this function |
| 3296 | * @pdev: pointer to platform device structure |
| 3297 | * @id: command id from parent to the soundwire master |
| 3298 | * @data: data from parent device to soundwire master |
| 3299 | */ |
| 3300 | int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data) |
| 3301 | { |
| 3302 | struct swr_mstr_ctrl *swrm; |
| 3303 | int ret = 0; |
| 3304 | struct swr_master *mstr; |
| 3305 | struct swr_device *swr_dev; |
Sudheer Papothi | 3d1596e | 2018-10-27 06:19:18 +0530 | [diff] [blame] | 3306 | struct swrm_port_config *port_cfg; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3307 | |
| 3308 | if (!pdev) { |
| 3309 | pr_err("%s: pdev is NULL\n", __func__); |
| 3310 | return -EINVAL; |
| 3311 | } |
| 3312 | swrm = platform_get_drvdata(pdev); |
| 3313 | if (!swrm) { |
| 3314 | dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__); |
| 3315 | return -EINVAL; |
| 3316 | } |
| 3317 | mstr = &swrm->master; |
| 3318 | |
| 3319 | switch (id) { |
Sudheer Papothi | 06f4341 | 2019-07-09 03:32:54 +0530 | [diff] [blame] | 3320 | case SWR_REQ_CLK_SWITCH: |
| 3321 | /* This will put soundwire in clock stop mode and disable the |
| 3322 | * clocks, if there is no active usecase running, so that the |
| 3323 | * next activity on soundwire will request clock from new clock |
| 3324 | * source. |
| 3325 | */ |
Sudheer Papothi | 8c50f2f | 2019-12-05 01:14:47 +0530 | [diff] [blame] | 3326 | if (!data) { |
| 3327 | dev_err(swrm->dev, "%s: data is NULL for id:%d\n", |
| 3328 | __func__, id); |
| 3329 | ret = -EINVAL; |
| 3330 | break; |
| 3331 | } |
Sudheer Papothi | 06f4341 | 2019-07-09 03:32:54 +0530 | [diff] [blame] | 3332 | mutex_lock(&swrm->mlock); |
Sudheer Papothi | 8c50f2f | 2019-12-05 01:14:47 +0530 | [diff] [blame] | 3333 | if (swrm->clk_src != *(int *)data) { |
Vatsal Bucha | bd68a7c | 2020-06-08 10:41:56 +0530 | [diff] [blame] | 3334 | if (swrm->state == SWR_MSTR_UP) { |
| 3335 | swrm->req_clk_switch = true; |
Sudheer Papothi | 8c50f2f | 2019-12-05 01:14:47 +0530 | [diff] [blame] | 3336 | swrm_device_suspend(&pdev->dev); |
Vatsal Bucha | bd68a7c | 2020-06-08 10:41:56 +0530 | [diff] [blame] | 3337 | if (swrm->state == SWR_MSTR_UP) |
| 3338 | swrm->req_clk_switch = false; |
| 3339 | } |
Sudheer Papothi | 8c50f2f | 2019-12-05 01:14:47 +0530 | [diff] [blame] | 3340 | swrm->clk_src = *(int *)data; |
| 3341 | } |
Sudheer Papothi | 06f4341 | 2019-07-09 03:32:54 +0530 | [diff] [blame] | 3342 | mutex_unlock(&swrm->mlock); |
| 3343 | break; |
Laxminath Kasam | b0f27cd | 2018-09-06 12:17:11 +0530 | [diff] [blame] | 3344 | case SWR_CLK_FREQ: |
| 3345 | if (!data) { |
| 3346 | dev_err(swrm->dev, "%s: data is NULL\n", __func__); |
| 3347 | ret = -EINVAL; |
| 3348 | } else { |
| 3349 | mutex_lock(&swrm->mlock); |
Ramprasad Katkam | 2e85a54 | 2019-04-26 18:28:31 +0530 | [diff] [blame] | 3350 | if (swrm->mclk_freq != *(int *)data) { |
| 3351 | dev_dbg(swrm->dev, "%s: freq change: force mstr down\n", __func__); |
| 3352 | if (swrm->state == SWR_MSTR_DOWN) |
| 3353 | dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n", |
| 3354 | __func__, swrm->state); |
Sudheer Papothi | 8a8b12b | 2019-11-15 23:06:41 +0530 | [diff] [blame] | 3355 | else { |
| 3356 | swrm->mclk_freq = *(int *)data; |
| 3357 | swrm->bus_clk = swrm->mclk_freq; |
| 3358 | swrm_switch_frame_shape(swrm, |
| 3359 | swrm->bus_clk); |
Ramprasad Katkam | 2e85a54 | 2019-04-26 18:28:31 +0530 | [diff] [blame] | 3360 | swrm_device_suspend(&pdev->dev); |
Sudheer Papothi | 8a8b12b | 2019-11-15 23:06:41 +0530 | [diff] [blame] | 3361 | } |
Prasad Kumpatla | 386df4e | 2019-10-11 18:23:16 +0530 | [diff] [blame] | 3362 | /* |
| 3363 | * add delay to ensure clk release happen |
| 3364 | * if interrupt triggered for clk stop, |
| 3365 | * wait for it to exit |
| 3366 | */ |
| 3367 | usleep_range(10000, 10500); |
Ramprasad Katkam | 2e85a54 | 2019-04-26 18:28:31 +0530 | [diff] [blame] | 3368 | } |
Laxminath Kasam | b0f27cd | 2018-09-06 12:17:11 +0530 | [diff] [blame] | 3369 | swrm->mclk_freq = *(int *)data; |
Sudheer Papothi | ac0ae1c | 2019-10-17 05:38:40 +0530 | [diff] [blame] | 3370 | swrm->bus_clk = swrm->mclk_freq; |
Laxminath Kasam | b0f27cd | 2018-09-06 12:17:11 +0530 | [diff] [blame] | 3371 | mutex_unlock(&swrm->mlock); |
| 3372 | } |
| 3373 | break; |
Laxminath Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 3374 | case SWR_DEVICE_SSR_DOWN: |
Aditya Bavanari | f500a1d | 2019-09-16 18:27:51 -0700 | [diff] [blame] | 3375 | trace_printk("%s: swr device down called\n", __func__); |
Prasad Kumpatla | 71fef46 | 2020-01-31 21:38:58 +0530 | [diff] [blame] | 3376 | mutex_lock(&swrm->mlock); |
| 3377 | if (swrm->state == SWR_MSTR_DOWN) |
| 3378 | dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n", |
| 3379 | __func__, swrm->state); |
| 3380 | else |
| 3381 | swrm_device_down(&pdev->dev); |
Laxminath Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 3382 | mutex_lock(&swrm->devlock); |
| 3383 | swrm->dev_up = false; |
Aditya Bavanari | bb981b7 | 2020-03-19 18:30:10 +0530 | [diff] [blame] | 3384 | swrm->hw_core_clk_en = 0; |
| 3385 | swrm->aud_core_clk_en = 0; |
Laxminath Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 3386 | mutex_unlock(&swrm->devlock); |
Ramprasad Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 3387 | mutex_lock(&swrm->reslock); |
| 3388 | swrm->state = SWR_MSTR_SSR; |
| 3389 | mutex_unlock(&swrm->reslock); |
Prasad Kumpatla | 71fef46 | 2020-01-31 21:38:58 +0530 | [diff] [blame] | 3390 | mutex_unlock(&swrm->mlock); |
Laxminath Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 3391 | break; |
| 3392 | case SWR_DEVICE_SSR_UP: |
Ramprasad Katkam | 6bce2e7 | 2018-10-10 19:20:13 +0530 | [diff] [blame] | 3393 | /* wait for clk voting to be zero */ |
Aditya Bavanari | f500a1d | 2019-09-16 18:27:51 -0700 | [diff] [blame] | 3394 | trace_printk("%s: swr device up called\n", __func__); |
Ramprasad Katkam | 7f6462e | 2018-11-06 11:51:22 +0530 | [diff] [blame] | 3395 | reinit_completion(&swrm->clk_off_complete); |
Ramprasad Katkam | 6bce2e7 | 2018-10-10 19:20:13 +0530 | [diff] [blame] | 3396 | if (swrm->clk_ref_count && |
| 3397 | !wait_for_completion_timeout(&swrm->clk_off_complete, |
Ramprasad Katkam | c87efeb | 2018-12-12 19:26:19 +0530 | [diff] [blame] | 3398 | msecs_to_jiffies(500))) |
Ramprasad Katkam | 6bce2e7 | 2018-10-10 19:20:13 +0530 | [diff] [blame] | 3399 | dev_err(swrm->dev, "%s: clock voting not zero\n", |
| 3400 | __func__); |
| 3401 | |
Laxminath Kasam | 1df09a8 | 2018-09-20 18:57:49 +0530 | [diff] [blame] | 3402 | mutex_lock(&swrm->devlock); |
| 3403 | swrm->dev_up = true; |
| 3404 | mutex_unlock(&swrm->devlock); |
| 3405 | break; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3406 | case SWR_DEVICE_DOWN: |
| 3407 | dev_dbg(swrm->dev, "%s: swr master down called\n", __func__); |
Aditya Bavanari | f500a1d | 2019-09-16 18:27:51 -0700 | [diff] [blame] | 3408 | trace_printk("%s: swr master down called\n", __func__); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3409 | mutex_lock(&swrm->mlock); |
Ramprasad Katkam | 2a799b4 | 2018-10-04 20:23:28 +0530 | [diff] [blame] | 3410 | if (swrm->state == SWR_MSTR_DOWN) |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3411 | dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n", |
| 3412 | __func__, swrm->state); |
| 3413 | else |
| 3414 | swrm_device_down(&pdev->dev); |
| 3415 | mutex_unlock(&swrm->mlock); |
| 3416 | break; |
| 3417 | case SWR_DEVICE_UP: |
| 3418 | dev_dbg(swrm->dev, "%s: swr master up called\n", __func__); |
Aditya Bavanari | f500a1d | 2019-09-16 18:27:51 -0700 | [diff] [blame] | 3419 | trace_printk("%s: swr master up called\n", __func__); |
Ramprasad Katkam | 0fed92f | 2018-11-08 14:22:22 +0530 | [diff] [blame] | 3420 | mutex_lock(&swrm->devlock); |
| 3421 | if (!swrm->dev_up) { |
| 3422 | dev_dbg(swrm->dev, "SSR not complete yet\n"); |
| 3423 | mutex_unlock(&swrm->devlock); |
| 3424 | return -EBUSY; |
| 3425 | } |
| 3426 | mutex_unlock(&swrm->devlock); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3427 | mutex_lock(&swrm->mlock); |
Ramprasad Katkam | 86c45e0 | 2018-10-16 19:31:51 +0530 | [diff] [blame] | 3428 | pm_runtime_mark_last_busy(&pdev->dev); |
| 3429 | pm_runtime_get_sync(&pdev->dev); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3430 | mutex_lock(&swrm->reslock); |
Ramprasad Katkam | 86c45e0 | 2018-10-16 19:31:51 +0530 | [diff] [blame] | 3431 | list_for_each_entry(swr_dev, &mstr->devices, dev_list) { |
| 3432 | ret = swr_reset_device(swr_dev); |
| 3433 | if (ret) { |
| 3434 | dev_err(swrm->dev, |
| 3435 | "%s: failed to reset swr device %d\n", |
| 3436 | __func__, swr_dev->dev_num); |
| 3437 | swrm_clk_request(swrm, false); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3438 | } |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3439 | } |
Ramprasad Katkam | 86c45e0 | 2018-10-16 19:31:51 +0530 | [diff] [blame] | 3440 | pm_runtime_mark_last_busy(&pdev->dev); |
| 3441 | pm_runtime_put_autosuspend(&pdev->dev); |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3442 | mutex_unlock(&swrm->reslock); |
| 3443 | mutex_unlock(&swrm->mlock); |
| 3444 | break; |
| 3445 | case SWR_SET_NUM_RX_CH: |
| 3446 | if (!data) { |
| 3447 | dev_err(swrm->dev, "%s: data is NULL\n", __func__); |
| 3448 | ret = -EINVAL; |
| 3449 | } else { |
| 3450 | mutex_lock(&swrm->mlock); |
| 3451 | swrm->num_rx_chs = *(int *)data; |
| 3452 | if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) { |
| 3453 | list_for_each_entry(swr_dev, &mstr->devices, |
| 3454 | dev_list) { |
| 3455 | ret = swr_set_device_group(swr_dev, |
| 3456 | SWR_BROADCAST); |
| 3457 | if (ret) |
| 3458 | dev_err(swrm->dev, |
| 3459 | "%s: set num ch failed\n", |
| 3460 | __func__); |
| 3461 | } |
| 3462 | } else { |
| 3463 | list_for_each_entry(swr_dev, &mstr->devices, |
| 3464 | dev_list) { |
| 3465 | ret = swr_set_device_group(swr_dev, |
| 3466 | SWR_GROUP_NONE); |
| 3467 | if (ret) |
| 3468 | dev_err(swrm->dev, |
| 3469 | "%s: set num ch failed\n", |
| 3470 | __func__); |
| 3471 | } |
| 3472 | } |
| 3473 | mutex_unlock(&swrm->mlock); |
| 3474 | } |
| 3475 | break; |
Aditya Bavanari | c034fad | 2018-11-12 22:55:11 +0530 | [diff] [blame] | 3476 | case SWR_REGISTER_WAKE_IRQ: |
| 3477 | if (!data) { |
| 3478 | dev_err(swrm->dev, "%s: reg wake irq data is NULL\n", |
| 3479 | __func__); |
| 3480 | ret = -EINVAL; |
| 3481 | } else { |
| 3482 | mutex_lock(&swrm->mlock); |
| 3483 | swrm->ipc_wakeup = *(u32 *)data; |
| 3484 | ret = swrm_register_wake_irq(swrm); |
| 3485 | if (ret) |
| 3486 | dev_err(swrm->dev, "%s: register wake_irq failed\n", |
| 3487 | __func__); |
| 3488 | mutex_unlock(&swrm->mlock); |
| 3489 | } |
| 3490 | break; |
Sudheer Papothi | 72ee264 | 2019-08-08 05:15:17 +0530 | [diff] [blame] | 3491 | case SWR_REGISTER_WAKEUP: |
| 3492 | msm_aud_evt_blocking_notifier_call_chain( |
| 3493 | SWR_WAKE_IRQ_REGISTER, (void *)swrm); |
Laxminath Kasam | ea6cbee | 2020-04-28 00:02:32 +0530 | [diff] [blame] | 3494 | swrm->dmic_sva = *(u32 *)data; |
Sudheer Papothi | 72ee264 | 2019-08-08 05:15:17 +0530 | [diff] [blame] | 3495 | break; |
| 3496 | case SWR_DEREGISTER_WAKEUP: |
| 3497 | msm_aud_evt_blocking_notifier_call_chain( |
| 3498 | SWR_WAKE_IRQ_DEREGISTER, (void *)swrm); |
Laxminath Kasam | ea6cbee | 2020-04-28 00:02:32 +0530 | [diff] [blame] | 3499 | swrm->dmic_sva = 0; |
Sudheer Papothi | 72ee264 | 2019-08-08 05:15:17 +0530 | [diff] [blame] | 3500 | break; |
Sudheer Papothi | 3d1596e | 2018-10-27 06:19:18 +0530 | [diff] [blame] | 3501 | case SWR_SET_PORT_MAP: |
| 3502 | if (!data) { |
| 3503 | dev_err(swrm->dev, "%s: data is NULL for id=%d\n", |
| 3504 | __func__, id); |
| 3505 | ret = -EINVAL; |
| 3506 | } else { |
| 3507 | mutex_lock(&swrm->mlock); |
| 3508 | port_cfg = (struct swrm_port_config *)data; |
| 3509 | if (!port_cfg->size) { |
| 3510 | ret = -EINVAL; |
| 3511 | goto done; |
| 3512 | } |
| 3513 | ret = swrm_alloc_port_mem(&pdev->dev, swrm, |
| 3514 | port_cfg->uc, port_cfg->size); |
| 3515 | if (!ret) |
| 3516 | swrm_copy_port_config(swrm, port_cfg, |
| 3517 | port_cfg->size); |
| 3518 | done: |
| 3519 | mutex_unlock(&swrm->mlock); |
| 3520 | } |
| 3521 | break; |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3522 | default: |
| 3523 | dev_err(swrm->dev, "%s: swr master unknown id %d\n", |
| 3524 | __func__, id); |
| 3525 | break; |
| 3526 | } |
| 3527 | return ret; |
| 3528 | } |
| 3529 | EXPORT_SYMBOL(swrm_wcd_notify); |
| 3530 | |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 3531 | /* |
| 3532 | * swrm_pm_cmpxchg: |
| 3533 | * Check old state and exchange with pm new state |
| 3534 | * if old state matches with current state |
| 3535 | * |
| 3536 | * @swrm: pointer to wcd core resource |
| 3537 | * @o: pm old state |
| 3538 | * @n: pm new state |
| 3539 | * |
| 3540 | * Returns old state |
| 3541 | */ |
| 3542 | static enum swrm_pm_state swrm_pm_cmpxchg( |
| 3543 | struct swr_mstr_ctrl *swrm, |
| 3544 | enum swrm_pm_state o, |
| 3545 | enum swrm_pm_state n) |
| 3546 | { |
| 3547 | enum swrm_pm_state old; |
| 3548 | |
| 3549 | if (!swrm) |
| 3550 | return o; |
| 3551 | |
| 3552 | mutex_lock(&swrm->pm_lock); |
| 3553 | old = swrm->pm_state; |
| 3554 | if (old == o) |
| 3555 | swrm->pm_state = n; |
| 3556 | mutex_unlock(&swrm->pm_lock); |
| 3557 | |
| 3558 | return old; |
| 3559 | } |
| 3560 | |
| 3561 | static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm) |
| 3562 | { |
| 3563 | enum swrm_pm_state os; |
| 3564 | |
| 3565 | /* |
| 3566 | * swrm_{lock/unlock}_sleep will be called by swr irq handler |
| 3567 | * and slave wake up requests.. |
| 3568 | * |
| 3569 | * If system didn't resume, we can simply return false so |
| 3570 | * IRQ handler can return without handling IRQ. |
| 3571 | */ |
| 3572 | mutex_lock(&swrm->pm_lock); |
| 3573 | if (swrm->wlock_holders++ == 0) { |
| 3574 | dev_dbg(swrm->dev, "%s: holding wake lock\n", __func__); |
| 3575 | pm_qos_update_request(&swrm->pm_qos_req, |
| 3576 | msm_cpuidle_get_deep_idle_latency()); |
| 3577 | pm_stay_awake(swrm->dev); |
| 3578 | } |
| 3579 | mutex_unlock(&swrm->pm_lock); |
| 3580 | |
| 3581 | if (!wait_event_timeout(swrm->pm_wq, |
| 3582 | ((os = swrm_pm_cmpxchg(swrm, |
| 3583 | SWRM_PM_SLEEPABLE, |
| 3584 | SWRM_PM_AWAKE)) == |
| 3585 | SWRM_PM_SLEEPABLE || |
| 3586 | (os == SWRM_PM_AWAKE)), |
| 3587 | msecs_to_jiffies( |
| 3588 | SWRM_SYSTEM_RESUME_TIMEOUT_MS))) { |
| 3589 | dev_err(swrm->dev, "%s: system didn't resume within %dms, s %d, w %d\n", |
| 3590 | __func__, SWRM_SYSTEM_RESUME_TIMEOUT_MS, swrm->pm_state, |
| 3591 | swrm->wlock_holders); |
| 3592 | swrm_unlock_sleep(swrm); |
| 3593 | return false; |
| 3594 | } |
| 3595 | wake_up_all(&swrm->pm_wq); |
| 3596 | return true; |
| 3597 | } |
| 3598 | |
| 3599 | static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm) |
| 3600 | { |
| 3601 | mutex_lock(&swrm->pm_lock); |
| 3602 | if (--swrm->wlock_holders == 0) { |
| 3603 | dev_dbg(swrm->dev, "%s: releasing wake lock pm_state %d -> %d\n", |
| 3604 | __func__, swrm->pm_state, SWRM_PM_SLEEPABLE); |
| 3605 | /* |
| 3606 | * if swrm_lock_sleep failed, pm_state would be still |
| 3607 | * swrm_PM_ASLEEP, don't overwrite |
| 3608 | */ |
| 3609 | if (likely(swrm->pm_state == SWRM_PM_AWAKE)) |
| 3610 | swrm->pm_state = SWRM_PM_SLEEPABLE; |
| 3611 | pm_qos_update_request(&swrm->pm_qos_req, |
| 3612 | PM_QOS_DEFAULT_VALUE); |
| 3613 | pm_relax(swrm->dev); |
| 3614 | } |
| 3615 | mutex_unlock(&swrm->pm_lock); |
| 3616 | wake_up_all(&swrm->pm_wq); |
| 3617 | } |
| 3618 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3619 | #ifdef CONFIG_PM_SLEEP |
| 3620 | static int swrm_suspend(struct device *dev) |
| 3621 | { |
| 3622 | int ret = -EBUSY; |
| 3623 | struct platform_device *pdev = to_platform_device(dev); |
| 3624 | struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev); |
| 3625 | |
| 3626 | dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state); |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 3627 | |
| 3628 | mutex_lock(&swrm->pm_lock); |
| 3629 | |
| 3630 | if (swrm->pm_state == SWRM_PM_SLEEPABLE) { |
| 3631 | dev_dbg(swrm->dev, "%s: suspending system, state %d, wlock %d\n", |
| 3632 | __func__, swrm->pm_state, |
| 3633 | swrm->wlock_holders); |
| 3634 | swrm->pm_state = SWRM_PM_ASLEEP; |
| 3635 | } else if (swrm->pm_state == SWRM_PM_AWAKE) { |
| 3636 | /* |
| 3637 | * unlock to wait for pm_state == SWRM_PM_SLEEPABLE |
| 3638 | * then set to SWRM_PM_ASLEEP |
| 3639 | */ |
| 3640 | dev_dbg(swrm->dev, "%s: waiting to suspend system, state %d, wlock %d\n", |
| 3641 | __func__, swrm->pm_state, |
| 3642 | swrm->wlock_holders); |
| 3643 | mutex_unlock(&swrm->pm_lock); |
| 3644 | if (!(wait_event_timeout(swrm->pm_wq, swrm_pm_cmpxchg( |
| 3645 | swrm, SWRM_PM_SLEEPABLE, |
| 3646 | SWRM_PM_ASLEEP) == |
| 3647 | SWRM_PM_SLEEPABLE, |
| 3648 | msecs_to_jiffies( |
| 3649 | SWRM_SYS_SUSPEND_WAIT)))) { |
| 3650 | dev_dbg(swrm->dev, "%s: suspend failed state %d, wlock %d\n", |
| 3651 | __func__, swrm->pm_state, |
| 3652 | swrm->wlock_holders); |
| 3653 | return -EBUSY; |
| 3654 | } else { |
| 3655 | dev_dbg(swrm->dev, |
| 3656 | "%s: done, state %d, wlock %d\n", |
| 3657 | __func__, swrm->pm_state, |
| 3658 | swrm->wlock_holders); |
| 3659 | } |
| 3660 | mutex_lock(&swrm->pm_lock); |
| 3661 | } else if (swrm->pm_state == SWRM_PM_ASLEEP) { |
| 3662 | dev_dbg(swrm->dev, "%s: system is already suspended, state %d, wlock %d\n", |
| 3663 | __func__, swrm->pm_state, |
| 3664 | swrm->wlock_holders); |
| 3665 | } |
| 3666 | |
| 3667 | mutex_unlock(&swrm->pm_lock); |
| 3668 | |
| 3669 | if ((!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev))) { |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3670 | ret = swrm_runtime_suspend(dev); |
| 3671 | if (!ret) { |
| 3672 | /* |
| 3673 | * Synchronize runtime-pm and system-pm states: |
| 3674 | * At this point, we are already suspended. If |
| 3675 | * runtime-pm still thinks its active, then |
| 3676 | * make sure its status is in sync with HW |
| 3677 | * status. The three below calls let the |
| 3678 | * runtime-pm know that we are suspended |
| 3679 | * already without re-invoking the suspend |
| 3680 | * callback |
| 3681 | */ |
| 3682 | pm_runtime_disable(dev); |
| 3683 | pm_runtime_set_suspended(dev); |
| 3684 | pm_runtime_enable(dev); |
| 3685 | } |
| 3686 | } |
| 3687 | if (ret == -EBUSY) { |
| 3688 | /* |
| 3689 | * There is a possibility that some audio stream is active |
| 3690 | * during suspend. We dont want to return suspend failure in |
| 3691 | * that case so that display and relevant components can still |
| 3692 | * go to suspend. |
| 3693 | * If there is some other error, then it should be passed-on |
| 3694 | * to system level suspend |
| 3695 | */ |
| 3696 | ret = 0; |
| 3697 | } |
| 3698 | return ret; |
| 3699 | } |
| 3700 | |
| 3701 | static int swrm_resume(struct device *dev) |
| 3702 | { |
| 3703 | int ret = 0; |
| 3704 | struct platform_device *pdev = to_platform_device(dev); |
| 3705 | struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev); |
| 3706 | |
| 3707 | dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state); |
| 3708 | if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) { |
| 3709 | ret = swrm_runtime_resume(dev); |
| 3710 | if (!ret) { |
| 3711 | pm_runtime_mark_last_busy(dev); |
| 3712 | pm_request_autosuspend(dev); |
| 3713 | } |
| 3714 | } |
Ramprasad Katkam | 5734987 | 2018-11-11 18:34:57 +0530 | [diff] [blame] | 3715 | mutex_lock(&swrm->pm_lock); |
| 3716 | if (swrm->pm_state == SWRM_PM_ASLEEP) { |
| 3717 | dev_dbg(swrm->dev, |
| 3718 | "%s: resuming system, state %d, wlock %d\n", |
| 3719 | __func__, swrm->pm_state, |
| 3720 | swrm->wlock_holders); |
| 3721 | swrm->pm_state = SWRM_PM_SLEEPABLE; |
| 3722 | } else { |
| 3723 | dev_dbg(swrm->dev, "%s: system is already awake, state %d wlock %d\n", |
| 3724 | __func__, swrm->pm_state, |
| 3725 | swrm->wlock_holders); |
| 3726 | } |
| 3727 | mutex_unlock(&swrm->pm_lock); |
| 3728 | wake_up_all(&swrm->pm_wq); |
| 3729 | |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3730 | return ret; |
| 3731 | } |
| 3732 | #endif /* CONFIG_PM_SLEEP */ |
| 3733 | |
| 3734 | static const struct dev_pm_ops swrm_dev_pm_ops = { |
| 3735 | SET_SYSTEM_SLEEP_PM_OPS( |
| 3736 | swrm_suspend, |
| 3737 | swrm_resume |
| 3738 | ) |
| 3739 | SET_RUNTIME_PM_OPS( |
| 3740 | swrm_runtime_suspend, |
| 3741 | swrm_runtime_resume, |
| 3742 | NULL |
| 3743 | ) |
| 3744 | }; |
| 3745 | |
| 3746 | static const struct of_device_id swrm_dt_match[] = { |
| 3747 | { |
| 3748 | .compatible = "qcom,swr-mstr", |
| 3749 | }, |
| 3750 | {} |
| 3751 | }; |
| 3752 | |
| 3753 | static struct platform_driver swr_mstr_driver = { |
| 3754 | .probe = swrm_probe, |
| 3755 | .remove = swrm_remove, |
| 3756 | .driver = { |
| 3757 | .name = SWR_WCD_NAME, |
| 3758 | .owner = THIS_MODULE, |
| 3759 | .pm = &swrm_dev_pm_ops, |
| 3760 | .of_match_table = swrm_dt_match, |
Xiaojun Sang | 53cd13a | 2018-06-29 15:14:37 +0800 | [diff] [blame] | 3761 | .suppress_bind_attrs = true, |
Ramprasad Katkam | 9f040f3 | 2018-05-16 10:19:25 +0530 | [diff] [blame] | 3762 | }, |
| 3763 | }; |
| 3764 | |
| 3765 | static int __init swrm_init(void) |
| 3766 | { |
| 3767 | return platform_driver_register(&swr_mstr_driver); |
| 3768 | } |
| 3769 | module_init(swrm_init); |
| 3770 | |
| 3771 | static void __exit swrm_exit(void) |
| 3772 | { |
| 3773 | platform_driver_unregister(&swr_mstr_driver); |
| 3774 | } |
| 3775 | module_exit(swrm_exit); |
| 3776 | |
| 3777 | MODULE_LICENSE("GPL v2"); |
| 3778 | MODULE_DESCRIPTION("SoundWire Master Controller"); |
| 3779 | MODULE_ALIAS("platform:swr-mstr"); |