Banajit Goswami | de8271c | 2017-01-18 00:28:59 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2015-2017, The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/irq.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/io.h> |
| 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/soundwire/soundwire.h> |
| 21 | #include <linux/soundwire/swr-wcd.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/kthread.h> |
| 24 | #include <linux/clk.h> |
| 25 | #include <linux/pm_runtime.h> |
| 26 | #include <linux/of.h> |
| 27 | #include <linux/debugfs.h> |
| 28 | #include <linux/uaccess.h> |
| 29 | #include "swrm_registers.h" |
| 30 | #include "swr-wcd-ctrl.h" |
| 31 | |
| 32 | #define SWR_BROADCAST_CMD_ID 0x0F |
| 33 | #define SWR_AUTO_SUSPEND_DELAY 3 /* delay in sec */ |
| 34 | #define SWR_DEV_ID_MASK 0xFFFFFFFF |
| 35 | #define SWR_REG_VAL_PACK(data, dev, id, reg) \ |
| 36 | ((reg) | ((id) << 16) | ((dev) << 20) | ((data) << 24)) |
| 37 | |
| 38 | /* pm runtime auto suspend timer in msecs */ |
| 39 | static int auto_suspend_timer = SWR_AUTO_SUSPEND_DELAY * 1000; |
| 40 | module_param(auto_suspend_timer, int, 0664); |
| 41 | MODULE_PARM_DESC(auto_suspend_timer, "timer for auto suspend"); |
| 42 | |
| 43 | static u8 mstr_ports[] = {100, 101, 102, 103, 104, 105, 106, 107}; |
| 44 | static u8 mstr_port_type[] = {SWR_DAC_PORT, SWR_COMP_PORT, SWR_BOOST_PORT, |
| 45 | SWR_DAC_PORT, SWR_COMP_PORT, SWR_BOOST_PORT, |
| 46 | SWR_VISENSE_PORT, SWR_VISENSE_PORT}; |
| 47 | |
| 48 | struct usecase uc[] = { |
| 49 | {0, 0, 0}, /* UC0: no ports */ |
| 50 | {1, 1, 2400}, /* UC1: Spkr */ |
| 51 | {1, 4, 600}, /* UC2: Compander */ |
| 52 | {1, 2, 300}, /* UC3: Smart Boost */ |
| 53 | {1, 2, 1200}, /* UC4: VI Sense */ |
| 54 | {4, 9, 4500}, /* UC5: Spkr + Comp + SB + VI */ |
| 55 | {8, 18, 9000}, /* UC6: 2*(Spkr + Comp + SB + VI) */ |
| 56 | {2, 2, 4800}, /* UC7: 2*Spkr */ |
| 57 | {2, 5, 3000}, /* UC8: Spkr + Comp */ |
| 58 | {4, 10, 6000}, /* UC9: 2*(Spkr + Comp) */ |
| 59 | {3, 7, 3300}, /* UC10: Spkr + Comp + SB */ |
| 60 | {6, 14, 6600}, /* UC11: 2*(Spkr + Comp + SB) */ |
| 61 | {2, 3, 2700}, /* UC12: Spkr + SB */ |
| 62 | {4, 6, 5400}, /* UC13: 2*(Spkr + SB) */ |
| 63 | {3, 5, 3900}, /* UC14: Spkr + SB + VI */ |
| 64 | {6, 10, 7800}, /* UC15: 2*(Spkr + SB + VI) */ |
| 65 | {2, 3, 3600}, /* UC16: Spkr + VI */ |
| 66 | {4, 6, 7200}, /* UC17: 2*(Spkr + VI) */ |
| 67 | }; |
| 68 | #define MAX_USECASE ARRAY_SIZE(uc) |
| 69 | |
| 70 | struct port_params pp[MAX_USECASE][SWR_MSTR_PORT_LEN] = { |
| 71 | /* UC 0 */ |
| 72 | { |
| 73 | {0, 0, 0}, |
| 74 | }, |
| 75 | /* UC 1 */ |
| 76 | { |
| 77 | {7, 1, 0}, |
| 78 | }, |
| 79 | /* UC 2 */ |
| 80 | { |
| 81 | {31, 2, 0}, |
| 82 | }, |
| 83 | /* UC 3 */ |
| 84 | { |
| 85 | {63, 12, 31}, |
| 86 | }, |
| 87 | /* UC 4 */ |
| 88 | { |
| 89 | {15, 7, 0}, |
| 90 | }, |
| 91 | /* UC 5 */ |
| 92 | { |
| 93 | {7, 1, 0}, |
| 94 | {31, 2, 0}, |
| 95 | {63, 12, 31}, |
| 96 | {15, 7, 0}, |
| 97 | }, |
| 98 | /* UC 6 */ |
| 99 | { |
| 100 | {7, 1, 0}, |
| 101 | {31, 2, 0}, |
| 102 | {63, 12, 31}, |
| 103 | {15, 7, 0}, |
| 104 | {7, 6, 0}, |
| 105 | {31, 18, 0}, |
| 106 | {63, 13, 31}, |
| 107 | {15, 10, 0}, |
| 108 | }, |
| 109 | /* UC 7 */ |
| 110 | { |
| 111 | {7, 1, 0}, |
| 112 | {7, 6, 0}, |
| 113 | |
| 114 | }, |
| 115 | /* UC 8 */ |
| 116 | { |
| 117 | {7, 1, 0}, |
| 118 | {31, 2, 0}, |
| 119 | }, |
| 120 | /* UC 9 */ |
| 121 | { |
| 122 | {7, 1, 0}, |
| 123 | {31, 2, 0}, |
| 124 | {7, 6, 0}, |
| 125 | {31, 18, 0}, |
| 126 | }, |
| 127 | /* UC 10 */ |
| 128 | { |
| 129 | {7, 1, 0}, |
| 130 | {31, 2, 0}, |
| 131 | {63, 12, 31}, |
| 132 | }, |
| 133 | /* UC 11 */ |
| 134 | { |
| 135 | {7, 1, 0}, |
| 136 | {31, 2, 0}, |
| 137 | {63, 12, 31}, |
| 138 | {7, 6, 0}, |
| 139 | {31, 18, 0}, |
| 140 | {63, 13, 31}, |
| 141 | }, |
| 142 | /* UC 12 */ |
| 143 | { |
| 144 | {7, 1, 0}, |
| 145 | {63, 12, 31}, |
| 146 | }, |
| 147 | /* UC 13 */ |
| 148 | { |
| 149 | {7, 1, 0}, |
| 150 | {63, 12, 31}, |
| 151 | {7, 6, 0}, |
| 152 | {63, 13, 31}, |
| 153 | }, |
| 154 | /* UC 14 */ |
| 155 | { |
| 156 | {7, 1, 0}, |
| 157 | {63, 12, 31}, |
| 158 | {15, 7, 0}, |
| 159 | }, |
| 160 | /* UC 15 */ |
| 161 | { |
| 162 | {7, 1, 0}, |
| 163 | {63, 12, 31}, |
| 164 | {15, 7, 0}, |
| 165 | {7, 6, 0}, |
| 166 | {63, 13, 31}, |
| 167 | {15, 10, 0}, |
| 168 | }, |
| 169 | /* UC 16 */ |
| 170 | { |
| 171 | {7, 1, 0}, |
| 172 | {15, 7, 0}, |
| 173 | }, |
| 174 | /* UC 17 */ |
| 175 | { |
| 176 | {7, 1, 0}, |
| 177 | {15, 7, 0}, |
| 178 | {7, 6, 0}, |
| 179 | {15, 10, 0}, |
| 180 | }, |
| 181 | }; |
| 182 | |
| 183 | enum { |
| 184 | SWR_NOT_PRESENT, /* Device is detached/not present on the bus */ |
| 185 | SWR_ATTACHED_OK, /* Device is attached */ |
| 186 | SWR_ALERT, /* Device alters master for any interrupts */ |
| 187 | SWR_RESERVED, /* Reserved */ |
| 188 | }; |
| 189 | |
| 190 | #define SWRM_MAX_PORT_REG 40 |
| 191 | #define SWRM_MAX_INIT_REG 8 |
| 192 | |
| 193 | #define SWR_MSTR_MAX_REG_ADDR 0x1740 |
| 194 | #define SWR_MSTR_START_REG_ADDR 0x00 |
| 195 | #define SWR_MSTR_MAX_BUF_LEN 32 |
| 196 | #define BYTES_PER_LINE 12 |
| 197 | #define SWR_MSTR_RD_BUF_LEN 8 |
| 198 | #define SWR_MSTR_WR_BUF_LEN 32 |
| 199 | |
| 200 | static void swrm_copy_data_port_config(struct swr_master *master, |
| 201 | u8 inactive_bank); |
| 202 | static struct swr_mstr_ctrl *dbgswrm; |
| 203 | static struct dentry *debugfs_swrm_dent; |
| 204 | static struct dentry *debugfs_peek; |
| 205 | static struct dentry *debugfs_poke; |
| 206 | static struct dentry *debugfs_reg_dump; |
| 207 | static unsigned int read_data; |
| 208 | |
| 209 | static int swrm_debug_open(struct inode *inode, struct file *file) |
| 210 | { |
| 211 | file->private_data = inode->i_private; |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | static int get_parameters(char *buf, u32 *param1, int num_of_par) |
| 216 | { |
| 217 | char *token; |
| 218 | int base, cnt; |
| 219 | |
| 220 | token = strsep(&buf, " "); |
| 221 | for (cnt = 0; cnt < num_of_par; cnt++) { |
| 222 | if (token) { |
| 223 | if ((token[1] == 'x') || (token[1] == 'X')) |
| 224 | base = 16; |
| 225 | else |
| 226 | base = 10; |
| 227 | |
| 228 | if (kstrtou32(token, base, ¶m1[cnt]) != 0) |
| 229 | return -EINVAL; |
| 230 | |
| 231 | token = strsep(&buf, " "); |
| 232 | } else |
| 233 | return -EINVAL; |
| 234 | } |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | static ssize_t swrm_reg_show(char __user *ubuf, size_t count, |
| 239 | loff_t *ppos) |
| 240 | { |
| 241 | int i, reg_val, len; |
| 242 | ssize_t total = 0; |
| 243 | char tmp_buf[SWR_MSTR_MAX_BUF_LEN]; |
| 244 | |
| 245 | if (!ubuf || !ppos) |
| 246 | return 0; |
| 247 | |
| 248 | for (i = (((int) *ppos / BYTES_PER_LINE) + SWR_MSTR_START_REG_ADDR); |
| 249 | i <= SWR_MSTR_MAX_REG_ADDR; i += 4) { |
| 250 | reg_val = dbgswrm->read(dbgswrm->handle, i); |
| 251 | len = snprintf(tmp_buf, 25, "0x%.3x: 0x%.2x\n", i, reg_val); |
| 252 | if ((total + len) >= count - 1) |
| 253 | break; |
| 254 | if (copy_to_user((ubuf + total), tmp_buf, len)) { |
| 255 | pr_err("%s: fail to copy reg dump\n", __func__); |
| 256 | total = -EFAULT; |
| 257 | goto copy_err; |
| 258 | } |
| 259 | *ppos += len; |
| 260 | total += len; |
| 261 | } |
| 262 | |
| 263 | copy_err: |
| 264 | return total; |
| 265 | } |
| 266 | |
| 267 | static ssize_t swrm_debug_read(struct file *file, char __user *ubuf, |
| 268 | size_t count, loff_t *ppos) |
| 269 | { |
| 270 | char lbuf[SWR_MSTR_RD_BUF_LEN]; |
| 271 | char *access_str; |
| 272 | ssize_t ret_cnt; |
| 273 | |
| 274 | if (!count || !file || !ppos || !ubuf) |
| 275 | return -EINVAL; |
| 276 | |
| 277 | access_str = file->private_data; |
| 278 | if (*ppos < 0) |
| 279 | return -EINVAL; |
| 280 | |
| 281 | if (!strcmp(access_str, "swrm_peek")) { |
| 282 | snprintf(lbuf, sizeof(lbuf), "0x%x\n", read_data); |
| 283 | ret_cnt = simple_read_from_buffer(ubuf, count, ppos, lbuf, |
| 284 | strnlen(lbuf, 7)); |
| 285 | } else if (!strcmp(access_str, "swrm_reg_dump")) { |
| 286 | ret_cnt = swrm_reg_show(ubuf, count, ppos); |
| 287 | } else { |
| 288 | pr_err("%s: %s not permitted to read\n", __func__, access_str); |
| 289 | ret_cnt = -EPERM; |
| 290 | } |
| 291 | return ret_cnt; |
| 292 | } |
| 293 | |
| 294 | static ssize_t swrm_debug_write(struct file *filp, |
| 295 | const char __user *ubuf, size_t cnt, loff_t *ppos) |
| 296 | { |
| 297 | char lbuf[SWR_MSTR_WR_BUF_LEN]; |
| 298 | int rc; |
| 299 | u32 param[5]; |
| 300 | char *access_str; |
| 301 | |
| 302 | if (!filp || !ppos || !ubuf) |
| 303 | return -EINVAL; |
| 304 | |
| 305 | access_str = filp->private_data; |
| 306 | if (cnt > sizeof(lbuf) - 1) |
| 307 | return -EINVAL; |
| 308 | |
| 309 | rc = copy_from_user(lbuf, ubuf, cnt); |
| 310 | if (rc) |
| 311 | return -EFAULT; |
| 312 | |
| 313 | lbuf[cnt] = '\0'; |
| 314 | if (!strcmp(access_str, "swrm_poke")) { |
| 315 | /* write */ |
| 316 | rc = get_parameters(lbuf, param, 2); |
| 317 | if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) && |
| 318 | (param[1] <= 0xFFFFFFFF) && |
| 319 | (rc == 0)) |
| 320 | rc = dbgswrm->write(dbgswrm->handle, param[0], |
| 321 | param[1]); |
| 322 | else |
| 323 | rc = -EINVAL; |
| 324 | } else if (!strcmp(access_str, "swrm_peek")) { |
| 325 | /* read */ |
| 326 | rc = get_parameters(lbuf, param, 1); |
| 327 | if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) && (rc == 0)) |
| 328 | read_data = dbgswrm->read(dbgswrm->handle, param[0]); |
| 329 | else |
| 330 | rc = -EINVAL; |
| 331 | } |
| 332 | if (rc == 0) |
| 333 | rc = cnt; |
| 334 | else |
| 335 | pr_err("%s: rc = %d\n", __func__, rc); |
| 336 | |
| 337 | return rc; |
| 338 | } |
| 339 | |
| 340 | static const struct file_operations swrm_debug_ops = { |
| 341 | .open = swrm_debug_open, |
| 342 | .write = swrm_debug_write, |
| 343 | .read = swrm_debug_read, |
| 344 | }; |
| 345 | |
| 346 | static int swrm_set_ch_map(struct swr_mstr_ctrl *swrm, void *data) |
| 347 | { |
| 348 | struct swr_mstr_port *pinfo = (struct swr_mstr_port *)data; |
| 349 | |
| 350 | swrm->mstr_port = kzalloc(sizeof(struct swr_mstr_port), GFP_KERNEL); |
| 351 | if (swrm->mstr_port == NULL) |
| 352 | return -ENOMEM; |
| 353 | swrm->mstr_port->num_port = pinfo->num_port; |
| 354 | swrm->mstr_port->port = kzalloc((pinfo->num_port * sizeof(u8)), |
| 355 | GFP_KERNEL); |
| 356 | if (!swrm->mstr_port->port) { |
| 357 | kfree(swrm->mstr_port); |
| 358 | swrm->mstr_port = NULL; |
| 359 | return -ENOMEM; |
| 360 | } |
| 361 | memcpy(swrm->mstr_port->port, pinfo->port, pinfo->num_port); |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | static bool swrm_is_port_en(struct swr_master *mstr) |
| 366 | { |
| 367 | return !!(mstr->num_port); |
| 368 | } |
| 369 | |
| 370 | static int swrm_clk_request(struct swr_mstr_ctrl *swrm, bool enable) |
| 371 | { |
| 372 | if (!swrm->clk || !swrm->handle) |
| 373 | return -EINVAL; |
| 374 | |
| 375 | if (enable) { |
| 376 | swrm->clk(swrm->handle, true); |
| 377 | swrm->state = SWR_MSTR_UP; |
| 378 | } else { |
| 379 | swrm->clk(swrm->handle, false); |
| 380 | swrm->state = SWR_MSTR_DOWN; |
| 381 | } |
| 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | static int swrm_get_port_config(struct swr_master *master) |
| 386 | { |
| 387 | u32 ch_rate = 0; |
| 388 | u32 num_ch = 0; |
| 389 | int i, uc_idx; |
| 390 | u32 portcount = 0; |
| 391 | |
| 392 | for (i = 0; i < SWR_MSTR_PORT_LEN; i++) { |
| 393 | if (master->port[i].port_en) { |
| 394 | ch_rate += master->port[i].ch_rate; |
| 395 | num_ch += master->port[i].num_ch; |
| 396 | portcount++; |
| 397 | } |
| 398 | } |
| 399 | for (i = 0; i < ARRAY_SIZE(uc); i++) { |
| 400 | if ((uc[i].num_port == portcount) && |
| 401 | (uc[i].num_ch == num_ch) && |
| 402 | (uc[i].chrate == ch_rate)) { |
| 403 | uc_idx = i; |
| 404 | break; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | if (i >= ARRAY_SIZE(uc)) { |
| 409 | dev_err(&master->dev, |
| 410 | "%s: usecase port:%d, num_ch:%d, chrate:%d not found\n", |
| 411 | __func__, master->num_port, num_ch, ch_rate); |
| 412 | return -EINVAL; |
| 413 | } |
| 414 | for (i = 0; i < SWR_MSTR_PORT_LEN; i++) { |
| 415 | if (master->port[i].port_en) { |
| 416 | master->port[i].sinterval = pp[uc_idx][i].si; |
| 417 | master->port[i].offset1 = pp[uc_idx][i].off1; |
| 418 | master->port[i].offset2 = pp[uc_idx][i].off2; |
| 419 | } |
| 420 | } |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | static int swrm_get_master_port(u8 *mstr_port_id, u8 slv_port_id) |
| 425 | { |
| 426 | int i; |
| 427 | |
| 428 | for (i = 0; i < SWR_MSTR_PORT_LEN; i++) { |
| 429 | if (mstr_ports[i] == slv_port_id) { |
| 430 | *mstr_port_id = i; |
| 431 | return 0; |
| 432 | } |
| 433 | } |
| 434 | return -EINVAL; |
| 435 | } |
| 436 | |
| 437 | static u32 swrm_get_packed_reg_val(u8 *cmd_id, u8 cmd_data, |
| 438 | u8 dev_addr, u16 reg_addr) |
| 439 | { |
| 440 | u32 val; |
| 441 | u8 id = *cmd_id; |
| 442 | |
| 443 | if (id != SWR_BROADCAST_CMD_ID) { |
| 444 | if (id < 14) |
| 445 | id += 1; |
| 446 | else |
| 447 | id = 0; |
| 448 | *cmd_id = id; |
| 449 | } |
| 450 | val = SWR_REG_VAL_PACK(cmd_data, dev_addr, id, reg_addr); |
| 451 | |
| 452 | return val; |
| 453 | } |
| 454 | |
| 455 | static int swrm_cmd_fifo_rd_cmd(struct swr_mstr_ctrl *swrm, int *cmd_data, |
| 456 | u8 dev_addr, u8 cmd_id, u16 reg_addr, |
| 457 | u32 len) |
| 458 | { |
| 459 | u32 val; |
| 460 | int ret = 0; |
| 461 | |
| 462 | val = swrm_get_packed_reg_val(&swrm->rcmd_id, len, dev_addr, reg_addr); |
| 463 | ret = swrm->write(swrm->handle, SWRM_CMD_FIFO_RD_CMD, val); |
| 464 | if (ret < 0) { |
| 465 | dev_err(swrm->dev, "%s: reg 0x%x write failed, err:%d\n", |
| 466 | __func__, val, ret); |
| 467 | goto err; |
| 468 | } |
| 469 | *cmd_data = swrm->read(swrm->handle, SWRM_CMD_FIFO_RD_FIFO_ADDR); |
| 470 | dev_dbg(swrm->dev, |
| 471 | "%s: reg: 0x%x, cmd_id: 0x%x, dev_id: 0x%x, cmd_data: 0x%x\n", |
| 472 | __func__, reg_addr, cmd_id, dev_addr, *cmd_data); |
| 473 | err: |
| 474 | return ret; |
| 475 | } |
| 476 | |
| 477 | static int swrm_cmd_fifo_wr_cmd(struct swr_mstr_ctrl *swrm, u8 cmd_data, |
| 478 | u8 dev_addr, u8 cmd_id, u16 reg_addr) |
| 479 | { |
| 480 | u32 val; |
| 481 | int ret = 0; |
| 482 | |
| 483 | if (!cmd_id) |
| 484 | val = swrm_get_packed_reg_val(&swrm->wcmd_id, cmd_data, |
| 485 | dev_addr, reg_addr); |
| 486 | else |
| 487 | val = swrm_get_packed_reg_val(&cmd_id, cmd_data, |
| 488 | dev_addr, reg_addr); |
| 489 | |
| 490 | dev_dbg(swrm->dev, |
| 491 | "%s: reg: 0x%x, cmd_id: 0x%x, dev_id: 0x%x, cmd_data: 0x%x\n", |
| 492 | __func__, reg_addr, cmd_id, dev_addr, cmd_data); |
| 493 | ret = swrm->write(swrm->handle, SWRM_CMD_FIFO_WR_CMD, val); |
| 494 | if (ret < 0) { |
| 495 | dev_err(swrm->dev, "%s: reg 0x%x write failed, err:%d\n", |
| 496 | __func__, val, ret); |
| 497 | goto err; |
| 498 | } |
| 499 | if (cmd_id == 0xF) |
| 500 | wait_for_completion_timeout(&swrm->broadcast, (2 * HZ/10)); |
| 501 | err: |
| 502 | return ret; |
| 503 | } |
| 504 | |
| 505 | static int swrm_read(struct swr_master *master, u8 dev_num, u16 reg_addr, |
| 506 | void *buf, u32 len) |
| 507 | { |
| 508 | struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master); |
| 509 | int ret = 0; |
Channagoud Kadabi | 075db3b | 2017-03-16 14:26:17 -0700 | [diff] [blame] | 510 | int val = 0; |
Banajit Goswami | de8271c | 2017-01-18 00:28:59 -0800 | [diff] [blame] | 511 | u8 *reg_val = (u8 *)buf; |
| 512 | |
| 513 | if (!swrm) { |
| 514 | dev_err(&master->dev, "%s: swrm is NULL\n", __func__); |
| 515 | return -EINVAL; |
| 516 | } |
| 517 | |
| 518 | if (dev_num) |
| 519 | ret = swrm_cmd_fifo_rd_cmd(swrm, &val, dev_num, 0, reg_addr, |
| 520 | len); |
| 521 | else |
| 522 | val = swrm->read(swrm->handle, reg_addr); |
| 523 | |
| 524 | *reg_val = (u8)val; |
| 525 | pm_runtime_mark_last_busy(&swrm->pdev->dev); |
| 526 | |
| 527 | return ret; |
| 528 | } |
| 529 | |
| 530 | static int swrm_write(struct swr_master *master, u8 dev_num, u16 reg_addr, |
| 531 | const void *buf) |
| 532 | { |
| 533 | struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master); |
| 534 | int ret = 0; |
| 535 | u8 reg_val = *(u8 *)buf; |
| 536 | |
| 537 | if (!swrm) { |
| 538 | dev_err(&master->dev, "%s: swrm is NULL\n", __func__); |
| 539 | return -EINVAL; |
| 540 | } |
| 541 | |
| 542 | if (dev_num) |
| 543 | ret = swrm_cmd_fifo_wr_cmd(swrm, reg_val, dev_num, 0, reg_addr); |
| 544 | else |
| 545 | ret = swrm->write(swrm->handle, reg_addr, reg_val); |
| 546 | |
| 547 | pm_runtime_mark_last_busy(&swrm->pdev->dev); |
| 548 | |
| 549 | return ret; |
| 550 | } |
| 551 | |
| 552 | static int swrm_bulk_write(struct swr_master *master, u8 dev_num, void *reg, |
| 553 | const void *buf, size_t len) |
| 554 | { |
| 555 | struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master); |
| 556 | int ret = 0; |
| 557 | int i; |
| 558 | u32 *val; |
| 559 | u32 *swr_fifo_reg; |
| 560 | |
| 561 | if (!swrm || !swrm->handle) { |
| 562 | dev_err(&master->dev, "%s: swrm is NULL\n", __func__); |
| 563 | return -EINVAL; |
| 564 | } |
| 565 | if (len <= 0) |
| 566 | return -EINVAL; |
| 567 | |
| 568 | if (dev_num) { |
| 569 | swr_fifo_reg = kcalloc(len, sizeof(u32), GFP_KERNEL); |
| 570 | if (!swr_fifo_reg) { |
| 571 | ret = -ENOMEM; |
| 572 | goto err; |
| 573 | } |
| 574 | val = kcalloc(len, sizeof(u32), GFP_KERNEL); |
| 575 | if (!val) { |
| 576 | ret = -ENOMEM; |
| 577 | goto mem_fail; |
| 578 | } |
| 579 | |
| 580 | for (i = 0; i < len; i++) { |
| 581 | val[i] = swrm_get_packed_reg_val(&swrm->wcmd_id, |
| 582 | ((u8 *)buf)[i], |
| 583 | dev_num, |
| 584 | ((u16 *)reg)[i]); |
| 585 | swr_fifo_reg[i] = SWRM_CMD_FIFO_WR_CMD; |
| 586 | } |
| 587 | ret = swrm->bulk_write(swrm->handle, swr_fifo_reg, val, len); |
| 588 | if (ret) { |
| 589 | dev_err(&master->dev, "%s: bulk write failed\n", |
| 590 | __func__); |
| 591 | ret = -EINVAL; |
| 592 | } |
| 593 | } else { |
| 594 | dev_err(&master->dev, |
| 595 | "%s: No support of Bulk write for master regs\n", |
| 596 | __func__); |
| 597 | ret = -EINVAL; |
| 598 | goto err; |
| 599 | } |
| 600 | kfree(val); |
| 601 | mem_fail: |
| 602 | kfree(swr_fifo_reg); |
| 603 | err: |
| 604 | pm_runtime_mark_last_busy(&swrm->pdev->dev); |
| 605 | return ret; |
| 606 | } |
| 607 | |
| 608 | static u8 get_inactive_bank_num(struct swr_mstr_ctrl *swrm) |
| 609 | { |
| 610 | return (swrm->read(swrm->handle, SWRM_MCP_STATUS) & |
| 611 | SWRM_MCP_STATUS_BANK_NUM_MASK) ? 0 : 1; |
| 612 | } |
| 613 | |
| 614 | static void enable_bank_switch(struct swr_mstr_ctrl *swrm, u8 bank, |
| 615 | u8 row, u8 col) |
| 616 | { |
| 617 | swrm_cmd_fifo_wr_cmd(swrm, ((row << 3) | col), 0xF, 0xF, |
| 618 | SWRS_SCP_FRAME_CTRL_BANK(bank)); |
| 619 | } |
| 620 | |
| 621 | static struct swr_port_info *swrm_get_port(struct swr_master *master, |
| 622 | u8 port_id) |
| 623 | { |
| 624 | int i; |
| 625 | struct swr_port_info *port = NULL; |
| 626 | |
| 627 | for (i = 0; i < SWR_MSTR_PORT_LEN; i++) { |
| 628 | port = &master->port[i]; |
| 629 | if (port->port_id == port_id) { |
| 630 | dev_dbg(&master->dev, "%s: port_id: %d, index: %d\n", |
| 631 | __func__, port_id, i); |
| 632 | return port; |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | return NULL; |
| 637 | } |
| 638 | |
| 639 | static struct swr_port_info *swrm_get_avail_port(struct swr_master *master) |
| 640 | { |
| 641 | int i; |
| 642 | struct swr_port_info *port = NULL; |
| 643 | |
| 644 | for (i = 0; i < SWR_MSTR_PORT_LEN; i++) { |
| 645 | port = &master->port[i]; |
| 646 | if (port->port_en) |
| 647 | continue; |
| 648 | |
| 649 | dev_dbg(&master->dev, "%s: port_id: %d, index: %d\n", |
| 650 | __func__, port->port_id, i); |
| 651 | return port; |
| 652 | } |
| 653 | |
| 654 | return NULL; |
| 655 | } |
| 656 | |
| 657 | static struct swr_port_info *swrm_get_enabled_port(struct swr_master *master, |
| 658 | u8 port_id) |
| 659 | { |
| 660 | int i; |
| 661 | struct swr_port_info *port = NULL; |
| 662 | |
| 663 | for (i = 0; i < SWR_MSTR_PORT_LEN; i++) { |
| 664 | port = &master->port[i]; |
| 665 | if ((port->port_id == port_id) && (port->port_en == true)) |
| 666 | break; |
| 667 | } |
| 668 | if (i == SWR_MSTR_PORT_LEN) |
| 669 | port = NULL; |
| 670 | return port; |
| 671 | } |
| 672 | |
| 673 | static bool swrm_remove_from_group(struct swr_master *master) |
| 674 | { |
| 675 | struct swr_device *swr_dev; |
| 676 | struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master); |
| 677 | bool is_removed = false; |
| 678 | |
| 679 | if (!swrm) |
| 680 | goto end; |
| 681 | |
| 682 | mutex_lock(&swrm->mlock); |
| 683 | if ((swrm->num_rx_chs > 1) && |
| 684 | (swrm->num_rx_chs == swrm->num_cfg_devs)) { |
| 685 | list_for_each_entry(swr_dev, &master->devices, |
| 686 | dev_list) { |
| 687 | swr_dev->group_id = SWR_GROUP_NONE; |
| 688 | master->gr_sid = 0; |
| 689 | } |
| 690 | is_removed = true; |
| 691 | } |
| 692 | mutex_unlock(&swrm->mlock); |
| 693 | |
| 694 | end: |
| 695 | return is_removed; |
| 696 | } |
| 697 | |
| 698 | static void swrm_cleanup_disabled_data_ports(struct swr_master *master, |
| 699 | u8 bank) |
| 700 | { |
| 701 | u32 value; |
| 702 | struct swr_port_info *port; |
| 703 | int i; |
| 704 | int port_type; |
| 705 | struct swrm_mports *mport, *mport_next = NULL; |
| 706 | int port_disable_cnt = 0; |
| 707 | struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master); |
| 708 | |
| 709 | if (!swrm) { |
| 710 | pr_err("%s: swrm is null\n", __func__); |
| 711 | return; |
| 712 | } |
| 713 | |
| 714 | dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__, |
| 715 | master->num_port); |
| 716 | |
| 717 | mport = list_first_entry_or_null(&swrm->mport_list, |
| 718 | struct swrm_mports, |
| 719 | list); |
| 720 | if (!mport) { |
| 721 | dev_err(swrm->dev, "%s: list is empty\n", __func__); |
| 722 | return; |
| 723 | } |
| 724 | |
| 725 | for (i = 0; i < master->num_port; i++) { |
| 726 | port = swrm_get_port(master, mstr_ports[mport->id]); |
| 727 | if (!port || port->ch_en) |
| 728 | goto inc_loop; |
| 729 | |
| 730 | port_disable_cnt++; |
| 731 | port_type = mstr_port_type[mport->id]; |
| 732 | value = ((port->ch_en) |
| 733 | << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT); |
| 734 | value |= ((port->offset2) |
| 735 | << SWRM_DP_PORT_CTRL_OFFSET2_SHFT); |
| 736 | value |= ((port->offset1) |
| 737 | << SWRM_DP_PORT_CTRL_OFFSET1_SHFT); |
| 738 | value |= port->sinterval; |
| 739 | |
| 740 | swrm->write(swrm->handle, |
| 741 | SWRM_DP_PORT_CTRL_BANK((mport->id+1), bank), |
| 742 | value); |
| 743 | swrm_cmd_fifo_wr_cmd(swrm, 0x00, port->dev_id, 0x00, |
| 744 | SWRS_DP_CHANNEL_ENABLE_BANK(port_type, bank)); |
| 745 | |
| 746 | dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n", |
| 747 | __func__, mport->id, |
| 748 | (SWRM_DP_PORT_CTRL_BANK((mport->id+1), bank)), value); |
| 749 | |
| 750 | inc_loop: |
| 751 | mport_next = list_next_entry(mport, list); |
| 752 | if (port && !port->ch_en) { |
| 753 | list_del(&mport->list); |
| 754 | kfree(mport); |
| 755 | } |
| 756 | if (!mport_next) { |
| 757 | dev_err(swrm->dev, "%s: end of list\n", __func__); |
| 758 | break; |
| 759 | } |
| 760 | mport = mport_next; |
| 761 | } |
| 762 | master->num_port -= port_disable_cnt; |
| 763 | |
| 764 | dev_dbg(swrm->dev, "%s:disable ports: %d, active ports (rem): %d\n", |
| 765 | __func__, port_disable_cnt, master->num_port); |
| 766 | } |
| 767 | |
| 768 | static void swrm_slvdev_datapath_control(struct swr_master *master, |
| 769 | bool enable) |
| 770 | { |
| 771 | u8 bank; |
| 772 | u32 value, n_col; |
| 773 | struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master); |
| 774 | int mask = (SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK | |
| 775 | SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK | |
| 776 | SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_BMSK); |
| 777 | u8 inactive_bank; |
| 778 | |
| 779 | if (!swrm) { |
| 780 | pr_err("%s: swrm is null\n", __func__); |
| 781 | return; |
| 782 | } |
| 783 | |
| 784 | bank = get_inactive_bank_num(swrm); |
| 785 | |
| 786 | dev_dbg(swrm->dev, "%s: enable: %d, cfg_devs: %d\n", |
| 787 | __func__, enable, swrm->num_cfg_devs); |
| 788 | |
| 789 | if (enable) { |
| 790 | /* set Row = 48 and col = 16 */ |
| 791 | n_col = SWR_MAX_COL; |
| 792 | } else { |
| 793 | /* |
| 794 | * Do not change to 48x2 if number of channels configured |
| 795 | * as stereo and if disable datapath is called for the |
| 796 | * first slave device |
| 797 | */ |
| 798 | if (swrm->num_cfg_devs > 0) |
| 799 | n_col = SWR_MAX_COL; |
| 800 | else |
| 801 | n_col = SWR_MIN_COL; |
| 802 | |
| 803 | /* |
| 804 | * All ports are already disabled, no need to perform |
| 805 | * bank-switch and copy operation. This case can arise |
| 806 | * when speaker channels are enabled in stereo mode with |
| 807 | * BROADCAST and disabled in GROUP_NONE |
| 808 | */ |
| 809 | if (master->num_port == 0) |
| 810 | return; |
| 811 | } |
| 812 | |
| 813 | value = swrm->read(swrm->handle, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank)); |
| 814 | value &= (~mask); |
| 815 | value |= ((0 << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) | |
| 816 | (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) | |
| 817 | (0 << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT)); |
| 818 | swrm->write(swrm->handle, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value); |
| 819 | |
| 820 | dev_dbg(swrm->dev, "%s: regaddr: 0x%x, value: 0x%x\n", __func__, |
| 821 | SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value); |
| 822 | |
| 823 | enable_bank_switch(swrm, bank, SWR_MAX_ROW, n_col); |
| 824 | |
| 825 | inactive_bank = bank ? 0 : 1; |
| 826 | if (enable) |
| 827 | swrm_copy_data_port_config(master, inactive_bank); |
| 828 | else |
| 829 | swrm_cleanup_disabled_data_ports(master, inactive_bank); |
| 830 | |
| 831 | if (!swrm_is_port_en(master)) { |
| 832 | dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n", |
| 833 | __func__); |
| 834 | pm_runtime_mark_last_busy(&swrm->pdev->dev); |
| 835 | pm_runtime_put_autosuspend(&swrm->pdev->dev); |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | static void swrm_apply_port_config(struct swr_master *master) |
| 840 | { |
| 841 | u8 bank; |
| 842 | struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master); |
| 843 | |
| 844 | if (!swrm) { |
| 845 | pr_err("%s: Invalid handle to swr controller\n", |
| 846 | __func__); |
| 847 | return; |
| 848 | } |
| 849 | |
| 850 | bank = get_inactive_bank_num(swrm); |
| 851 | dev_dbg(swrm->dev, "%s: enter bank: %d master_ports: %d\n", |
| 852 | __func__, bank, master->num_port); |
| 853 | |
| 854 | |
| 855 | swrm_cmd_fifo_wr_cmd(swrm, 0x01, 0xF, 0x00, |
| 856 | SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(bank)); |
| 857 | |
| 858 | swrm_copy_data_port_config(master, bank); |
| 859 | } |
| 860 | |
| 861 | static void swrm_copy_data_port_config(struct swr_master *master, u8 bank) |
| 862 | { |
| 863 | u32 value; |
| 864 | struct swr_port_info *port; |
| 865 | int i; |
| 866 | int port_type; |
| 867 | struct swrm_mports *mport; |
| 868 | u32 reg[SWRM_MAX_PORT_REG]; |
| 869 | u32 val[SWRM_MAX_PORT_REG]; |
| 870 | int len = 0; |
| 871 | struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master); |
| 872 | |
| 873 | if (!swrm) { |
| 874 | pr_err("%s: swrm is null\n", __func__); |
| 875 | return; |
| 876 | } |
| 877 | |
| 878 | dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__, |
| 879 | master->num_port); |
| 880 | |
| 881 | mport = list_first_entry_or_null(&swrm->mport_list, |
| 882 | struct swrm_mports, |
| 883 | list); |
| 884 | if (!mport) { |
| 885 | dev_err(swrm->dev, "%s: list is empty\n", __func__); |
| 886 | return; |
| 887 | } |
| 888 | for (i = 0; i < master->num_port; i++) { |
| 889 | |
| 890 | port = swrm_get_enabled_port(master, mstr_ports[mport->id]); |
| 891 | if (!port) |
| 892 | continue; |
| 893 | port_type = mstr_port_type[mport->id]; |
| 894 | if (!port->dev_id || (port->dev_id > master->num_dev)) { |
| 895 | dev_dbg(swrm->dev, "%s: invalid device id = %d\n", |
| 896 | __func__, port->dev_id); |
| 897 | continue; |
| 898 | } |
| 899 | value = ((port->ch_en) |
| 900 | << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT); |
| 901 | value |= ((port->offset2) |
| 902 | << SWRM_DP_PORT_CTRL_OFFSET2_SHFT); |
| 903 | value |= ((port->offset1) |
| 904 | << SWRM_DP_PORT_CTRL_OFFSET1_SHFT); |
| 905 | value |= port->sinterval; |
| 906 | |
| 907 | reg[len] = SWRM_DP_PORT_CTRL_BANK((mport->id+1), bank); |
| 908 | val[len++] = value; |
| 909 | |
| 910 | dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n", |
| 911 | __func__, mport->id, |
| 912 | (SWRM_DP_PORT_CTRL_BANK((mport->id+1), bank)), value); |
| 913 | |
| 914 | reg[len] = SWRM_CMD_FIFO_WR_CMD; |
| 915 | val[len++] = SWR_REG_VAL_PACK(port->ch_en, port->dev_id, 0x00, |
| 916 | SWRS_DP_CHANNEL_ENABLE_BANK(port_type, bank)); |
| 917 | |
| 918 | reg[len] = SWRM_CMD_FIFO_WR_CMD; |
| 919 | val[len++] = SWR_REG_VAL_PACK(port->sinterval, |
| 920 | port->dev_id, 0x00, |
| 921 | SWRS_DP_SAMPLE_CONTROL_1_BANK(port_type, bank)); |
| 922 | |
| 923 | reg[len] = SWRM_CMD_FIFO_WR_CMD; |
| 924 | val[len++] = SWR_REG_VAL_PACK(port->offset1, |
| 925 | port->dev_id, 0x00, |
| 926 | SWRS_DP_OFFSET_CONTROL_1_BANK(port_type, bank)); |
| 927 | |
| 928 | if (port_type != 0) { |
| 929 | reg[len] = SWRM_CMD_FIFO_WR_CMD; |
| 930 | val[len++] = SWR_REG_VAL_PACK(port->offset2, |
| 931 | port->dev_id, 0x00, |
| 932 | SWRS_DP_OFFSET_CONTROL_2_BANK(port_type, |
| 933 | bank)); |
| 934 | } |
| 935 | mport = list_next_entry(mport, list); |
| 936 | if (!mport) { |
| 937 | dev_err(swrm->dev, "%s: end of list\n", __func__); |
| 938 | break; |
| 939 | } |
| 940 | } |
| 941 | swrm->bulk_write(swrm->handle, reg, val, len); |
| 942 | } |
| 943 | |
| 944 | static int swrm_connect_port(struct swr_master *master, |
| 945 | struct swr_params *portinfo) |
| 946 | { |
| 947 | int i; |
| 948 | struct swr_port_info *port; |
| 949 | int ret = 0; |
| 950 | struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master); |
| 951 | struct swrm_mports *mport; |
| 952 | struct list_head *ptr, *next; |
| 953 | |
| 954 | dev_dbg(&master->dev, "%s: enter\n", __func__); |
| 955 | if (!portinfo) |
| 956 | return -EINVAL; |
| 957 | |
| 958 | if (!swrm) { |
| 959 | dev_err(&master->dev, |
| 960 | "%s: Invalid handle to swr controller\n", |
| 961 | __func__); |
| 962 | return -EINVAL; |
| 963 | } |
| 964 | |
| 965 | mutex_lock(&swrm->mlock); |
| 966 | if (!swrm_is_port_en(master)) |
| 967 | pm_runtime_get_sync(&swrm->pdev->dev); |
| 968 | |
| 969 | for (i = 0; i < portinfo->num_port; i++) { |
| 970 | mport = kzalloc(sizeof(struct swrm_mports), GFP_KERNEL); |
| 971 | if (!mport) { |
| 972 | ret = -ENOMEM; |
| 973 | goto mem_fail; |
| 974 | } |
| 975 | ret = swrm_get_master_port(&mport->id, |
| 976 | portinfo->port_id[i]); |
| 977 | if (ret < 0) { |
| 978 | dev_err(&master->dev, |
| 979 | "%s: mstr portid for slv port %d not found\n", |
| 980 | __func__, portinfo->port_id[i]); |
| 981 | goto port_fail; |
| 982 | } |
| 983 | port = swrm_get_avail_port(master); |
| 984 | if (!port) { |
| 985 | dev_err(&master->dev, |
| 986 | "%s: avail ports not found!\n", __func__); |
| 987 | goto port_fail; |
| 988 | } |
| 989 | list_add(&mport->list, &swrm->mport_list); |
| 990 | port->dev_id = portinfo->dev_id; |
| 991 | port->port_id = portinfo->port_id[i]; |
| 992 | port->num_ch = portinfo->num_ch[i]; |
| 993 | port->ch_rate = portinfo->ch_rate[i]; |
| 994 | port->ch_en = portinfo->ch_en[i]; |
| 995 | port->port_en = true; |
| 996 | dev_dbg(&master->dev, |
| 997 | "%s: mstr port %d, slv port %d ch_rate %d num_ch %d\n", |
| 998 | __func__, mport->id, port->port_id, port->ch_rate, |
| 999 | port->num_ch); |
| 1000 | } |
| 1001 | master->num_port += portinfo->num_port; |
| 1002 | if (master->num_port >= SWR_MSTR_PORT_LEN) |
| 1003 | master->num_port = SWR_MSTR_PORT_LEN; |
| 1004 | |
| 1005 | swrm_get_port_config(master); |
| 1006 | swr_port_response(master, portinfo->tid); |
| 1007 | swrm->num_cfg_devs += 1; |
| 1008 | dev_dbg(&master->dev, "%s: cfg_devs: %d, rx_chs: %d\n", |
| 1009 | __func__, swrm->num_cfg_devs, swrm->num_rx_chs); |
| 1010 | if (swrm->num_rx_chs > 1) { |
| 1011 | if (swrm->num_rx_chs == swrm->num_cfg_devs) |
| 1012 | swrm_apply_port_config(master); |
| 1013 | } else { |
| 1014 | swrm_apply_port_config(master); |
| 1015 | } |
| 1016 | mutex_unlock(&swrm->mlock); |
| 1017 | return 0; |
| 1018 | |
| 1019 | port_fail: |
| 1020 | kfree(mport); |
| 1021 | mem_fail: |
| 1022 | list_for_each_safe(ptr, next, &swrm->mport_list) { |
| 1023 | mport = list_entry(ptr, struct swrm_mports, list); |
| 1024 | for (i = 0; i < portinfo->num_port; i++) { |
| 1025 | if (portinfo->port_id[i] == mstr_ports[mport->id]) { |
| 1026 | port = swrm_get_port(master, |
| 1027 | portinfo->port_id[i]); |
| 1028 | if (port) |
| 1029 | port->ch_en = false; |
| 1030 | list_del(&mport->list); |
| 1031 | kfree(mport); |
| 1032 | break; |
| 1033 | } |
| 1034 | } |
| 1035 | } |
| 1036 | mutex_unlock(&swrm->mlock); |
| 1037 | return ret; |
| 1038 | } |
| 1039 | |
| 1040 | static int swrm_disconnect_port(struct swr_master *master, |
| 1041 | struct swr_params *portinfo) |
| 1042 | { |
| 1043 | int i; |
| 1044 | struct swr_port_info *port; |
| 1045 | u8 bank; |
| 1046 | u32 value; |
| 1047 | int ret = 0; |
| 1048 | u8 mport_id = 0; |
| 1049 | int port_type = 0; |
| 1050 | struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master); |
| 1051 | |
| 1052 | if (!swrm) { |
| 1053 | dev_err(&master->dev, |
| 1054 | "%s: Invalid handle to swr controller\n", |
| 1055 | __func__); |
| 1056 | return -EINVAL; |
| 1057 | } |
| 1058 | |
| 1059 | if (!portinfo) { |
| 1060 | dev_err(&master->dev, "%s: portinfo is NULL\n", __func__); |
| 1061 | return -EINVAL; |
| 1062 | } |
| 1063 | mutex_lock(&swrm->mlock); |
| 1064 | bank = get_inactive_bank_num(swrm); |
| 1065 | for (i = 0; i < portinfo->num_port; i++) { |
| 1066 | ret = swrm_get_master_port(&mport_id, |
| 1067 | portinfo->port_id[i]); |
| 1068 | if (ret < 0) { |
| 1069 | dev_err(&master->dev, |
| 1070 | "%s: mstr portid for slv port %d not found\n", |
| 1071 | __func__, portinfo->port_id[i]); |
| 1072 | mutex_unlock(&swrm->mlock); |
| 1073 | return -EINVAL; |
| 1074 | } |
| 1075 | port = swrm_get_enabled_port(master, portinfo->port_id[i]); |
| 1076 | if (!port) { |
| 1077 | dev_dbg(&master->dev, "%s: port %d already disabled\n", |
| 1078 | __func__, portinfo->port_id[i]); |
| 1079 | continue; |
| 1080 | } |
| 1081 | port_type = mstr_port_type[mport_id]; |
| 1082 | port->dev_id = portinfo->dev_id; |
| 1083 | port->port_en = false; |
| 1084 | port->ch_en = 0; |
| 1085 | value = port->ch_en << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT; |
| 1086 | value |= (port->offset2 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT); |
| 1087 | value |= (port->offset1 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT); |
| 1088 | value |= port->sinterval; |
| 1089 | |
| 1090 | |
| 1091 | swrm->write(swrm->handle, |
| 1092 | SWRM_DP_PORT_CTRL_BANK((mport_id+1), bank), |
| 1093 | value); |
| 1094 | swrm_cmd_fifo_wr_cmd(swrm, 0x00, port->dev_id, 0x00, |
| 1095 | SWRS_DP_CHANNEL_ENABLE_BANK(port_type, bank)); |
| 1096 | } |
| 1097 | |
| 1098 | swr_port_response(master, portinfo->tid); |
| 1099 | swrm->num_cfg_devs -= 1; |
| 1100 | dev_dbg(&master->dev, "%s: cfg_devs: %d, rx_chs: %d, active ports: %d\n", |
| 1101 | __func__, swrm->num_cfg_devs, swrm->num_rx_chs, |
| 1102 | master->num_port); |
| 1103 | mutex_unlock(&swrm->mlock); |
| 1104 | |
| 1105 | return 0; |
| 1106 | } |
| 1107 | |
| 1108 | static int swrm_check_slave_change_status(struct swr_mstr_ctrl *swrm, |
| 1109 | int status, u8 *devnum) |
| 1110 | { |
| 1111 | int i; |
| 1112 | int new_sts = status; |
| 1113 | int ret = SWR_NOT_PRESENT; |
| 1114 | |
| 1115 | if (status != swrm->slave_status) { |
| 1116 | for (i = 0; i < (swrm->master.num_dev + 1); i++) { |
| 1117 | if ((status & SWRM_MCP_SLV_STATUS_MASK) != |
| 1118 | (swrm->slave_status & SWRM_MCP_SLV_STATUS_MASK)) { |
| 1119 | ret = (status & SWRM_MCP_SLV_STATUS_MASK); |
| 1120 | *devnum = i; |
| 1121 | break; |
| 1122 | } |
| 1123 | status >>= 2; |
| 1124 | swrm->slave_status >>= 2; |
| 1125 | } |
| 1126 | swrm->slave_status = new_sts; |
| 1127 | } |
| 1128 | return ret; |
| 1129 | } |
| 1130 | |
| 1131 | static irqreturn_t swr_mstr_interrupt(int irq, void *dev) |
| 1132 | { |
| 1133 | struct swr_mstr_ctrl *swrm = dev; |
| 1134 | u32 value, intr_sts; |
| 1135 | int status, chg_sts, i; |
| 1136 | u8 devnum = 0; |
| 1137 | int ret = IRQ_HANDLED; |
| 1138 | |
| 1139 | pm_runtime_get_sync(&swrm->pdev->dev); |
| 1140 | intr_sts = swrm->read(swrm->handle, SWRM_INTERRUPT_STATUS); |
| 1141 | intr_sts &= SWRM_INTERRUPT_STATUS_RMSK; |
| 1142 | for (i = 0; i < SWRM_INTERRUPT_MAX; i++) { |
| 1143 | value = intr_sts & (1 << i); |
| 1144 | if (!value) |
| 1145 | continue; |
| 1146 | |
| 1147 | swrm->write(swrm->handle, SWRM_INTERRUPT_CLEAR, value); |
| 1148 | switch (value) { |
| 1149 | case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ: |
| 1150 | dev_dbg(swrm->dev, "SWR slave pend irq\n"); |
| 1151 | break; |
| 1152 | case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED: |
| 1153 | dev_dbg(swrm->dev, "SWR new slave attached\n"); |
| 1154 | break; |
| 1155 | case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS: |
| 1156 | status = swrm->read(swrm->handle, SWRM_MCP_SLV_STATUS); |
| 1157 | if (status == swrm->slave_status) { |
| 1158 | dev_dbg(swrm->dev, |
| 1159 | "%s: No change in slave status: %d\n", |
| 1160 | __func__, status); |
| 1161 | break; |
| 1162 | } |
| 1163 | chg_sts = swrm_check_slave_change_status(swrm, status, |
| 1164 | &devnum); |
| 1165 | switch (chg_sts) { |
| 1166 | case SWR_NOT_PRESENT: |
| 1167 | dev_dbg(swrm->dev, "device %d got detached\n", |
| 1168 | devnum); |
| 1169 | break; |
| 1170 | case SWR_ATTACHED_OK: |
| 1171 | dev_dbg(swrm->dev, "device %d got attached\n", |
| 1172 | devnum); |
| 1173 | break; |
| 1174 | case SWR_ALERT: |
| 1175 | dev_dbg(swrm->dev, |
| 1176 | "device %d has pending interrupt\n", |
| 1177 | devnum); |
| 1178 | break; |
| 1179 | } |
| 1180 | break; |
| 1181 | case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET: |
| 1182 | dev_err_ratelimited(swrm->dev, "SWR bus clash detected\n"); |
| 1183 | break; |
| 1184 | case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW: |
| 1185 | dev_dbg(swrm->dev, "SWR read FIFO overflow\n"); |
| 1186 | break; |
| 1187 | case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW: |
| 1188 | dev_dbg(swrm->dev, "SWR read FIFO underflow\n"); |
| 1189 | break; |
| 1190 | case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW: |
| 1191 | dev_dbg(swrm->dev, "SWR write FIFO overflow\n"); |
| 1192 | break; |
| 1193 | case SWRM_INTERRUPT_STATUS_CMD_ERROR: |
| 1194 | value = swrm->read(swrm->handle, SWRM_CMD_FIFO_STATUS); |
| 1195 | dev_err_ratelimited(swrm->dev, |
| 1196 | "SWR CMD error, fifo status 0x%x, flushing fifo\n", |
| 1197 | value); |
| 1198 | swrm->write(swrm->handle, SWRM_CMD_FIFO_CMD, 0x1); |
| 1199 | break; |
| 1200 | case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION: |
| 1201 | dev_dbg(swrm->dev, "SWR Port collision detected\n"); |
| 1202 | break; |
| 1203 | case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH: |
| 1204 | dev_dbg(swrm->dev, "SWR read enable valid mismatch\n"); |
| 1205 | break; |
| 1206 | case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED: |
| 1207 | complete(&swrm->broadcast); |
| 1208 | dev_dbg(swrm->dev, "SWR cmd id finished\n"); |
| 1209 | break; |
| 1210 | case SWRM_INTERRUPT_STATUS_NEW_SLAVE_AUTO_ENUM_FINISHED: |
| 1211 | break; |
| 1212 | case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED: |
| 1213 | break; |
| 1214 | case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL: |
| 1215 | break; |
| 1216 | case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED: |
| 1217 | complete(&swrm->reset); |
| 1218 | break; |
| 1219 | case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED: |
| 1220 | break; |
| 1221 | default: |
| 1222 | dev_err_ratelimited(swrm->dev, "SWR unknown interrupt\n"); |
| 1223 | ret = IRQ_NONE; |
| 1224 | break; |
| 1225 | } |
| 1226 | } |
| 1227 | pm_runtime_mark_last_busy(&swrm->pdev->dev); |
| 1228 | pm_runtime_put_autosuspend(&swrm->pdev->dev); |
| 1229 | return ret; |
| 1230 | } |
| 1231 | |
| 1232 | static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum) |
| 1233 | { |
| 1234 | u32 val; |
| 1235 | |
| 1236 | swrm->slave_status = swrm->read(swrm->handle, SWRM_MCP_SLV_STATUS); |
| 1237 | val = (swrm->slave_status >> (devnum * 2)); |
| 1238 | val &= SWRM_MCP_SLV_STATUS_MASK; |
| 1239 | return val; |
| 1240 | } |
| 1241 | |
| 1242 | static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id, |
| 1243 | u8 *dev_num) |
| 1244 | { |
| 1245 | int i; |
| 1246 | u64 id = 0; |
| 1247 | int ret = -EINVAL; |
| 1248 | struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr); |
| 1249 | |
| 1250 | if (!swrm) { |
| 1251 | pr_err("%s: Invalid handle to swr controller\n", |
| 1252 | __func__); |
| 1253 | return ret; |
| 1254 | } |
| 1255 | |
| 1256 | pm_runtime_get_sync(&swrm->pdev->dev); |
| 1257 | for (i = 1; i < (mstr->num_dev + 1); i++) { |
| 1258 | id = ((u64)(swrm->read(swrm->handle, |
| 1259 | SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32); |
| 1260 | id |= swrm->read(swrm->handle, |
| 1261 | SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i)); |
| 1262 | if ((id & SWR_DEV_ID_MASK) == dev_id) { |
| 1263 | if (swrm_get_device_status(swrm, i) == 0x01) { |
| 1264 | *dev_num = i; |
| 1265 | ret = 0; |
| 1266 | } else { |
| 1267 | dev_err(swrm->dev, "%s: device is not ready\n", |
| 1268 | __func__); |
| 1269 | } |
| 1270 | goto found; |
| 1271 | } |
| 1272 | } |
| 1273 | dev_err(swrm->dev, "%s: device id 0x%llx does not match with 0x%llx\n", |
| 1274 | __func__, id, dev_id); |
| 1275 | found: |
| 1276 | pm_runtime_mark_last_busy(&swrm->pdev->dev); |
| 1277 | pm_runtime_put_autosuspend(&swrm->pdev->dev); |
| 1278 | return ret; |
| 1279 | } |
| 1280 | static int swrm_master_init(struct swr_mstr_ctrl *swrm) |
| 1281 | { |
| 1282 | int ret = 0; |
| 1283 | u32 val; |
| 1284 | u8 row_ctrl = SWR_MAX_ROW; |
| 1285 | u8 col_ctrl = SWR_MIN_COL; |
| 1286 | u8 ssp_period = 1; |
| 1287 | u8 retry_cmd_num = 3; |
| 1288 | u32 reg[SWRM_MAX_INIT_REG]; |
| 1289 | u32 value[SWRM_MAX_INIT_REG]; |
| 1290 | int len = 0; |
| 1291 | |
| 1292 | /* Clear Rows and Cols */ |
| 1293 | val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) | |
| 1294 | (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) | |
| 1295 | (ssp_period << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT)); |
| 1296 | |
| 1297 | reg[len] = SWRM_MCP_FRAME_CTRL_BANK_ADDR(0); |
| 1298 | value[len++] = val; |
| 1299 | |
| 1300 | /* Set Auto enumeration flag */ |
| 1301 | reg[len] = SWRM_ENUMERATOR_CFG_ADDR; |
| 1302 | value[len++] = 1; |
| 1303 | |
| 1304 | /* Mask soundwire interrupts */ |
| 1305 | reg[len] = SWRM_INTERRUPT_MASK_ADDR; |
| 1306 | value[len++] = 0x1FFFD; |
| 1307 | |
| 1308 | /* Configure No pings */ |
| 1309 | val = swrm->read(swrm->handle, SWRM_MCP_CFG_ADDR); |
| 1310 | val &= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK; |
| 1311 | val |= (0x1f << SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT); |
| 1312 | reg[len] = SWRM_MCP_CFG_ADDR; |
| 1313 | value[len++] = val; |
| 1314 | |
| 1315 | /* Configure number of retries of a read/write cmd */ |
| 1316 | val = (retry_cmd_num << SWRM_CMD_FIFO_CFG_NUM_OF_CMD_RETRY_SHFT); |
| 1317 | reg[len] = SWRM_CMD_FIFO_CFG_ADDR; |
| 1318 | value[len++] = val; |
| 1319 | |
| 1320 | /* Set IRQ to PULSE */ |
| 1321 | reg[len] = SWRM_COMP_CFG_ADDR; |
| 1322 | value[len++] = 0x02; |
| 1323 | |
| 1324 | reg[len] = SWRM_COMP_CFG_ADDR; |
| 1325 | value[len++] = 0x03; |
| 1326 | |
| 1327 | reg[len] = SWRM_INTERRUPT_CLEAR; |
| 1328 | value[len++] = 0x08; |
| 1329 | |
| 1330 | swrm->bulk_write(swrm->handle, reg, value, len); |
| 1331 | |
| 1332 | return ret; |
| 1333 | } |
| 1334 | |
| 1335 | static int swrm_probe(struct platform_device *pdev) |
| 1336 | { |
| 1337 | struct swr_mstr_ctrl *swrm; |
| 1338 | struct swr_ctrl_platform_data *pdata; |
| 1339 | struct swr_device *swr_dev, *safe; |
| 1340 | int ret; |
| 1341 | |
| 1342 | /* Allocate soundwire master driver structure */ |
| 1343 | swrm = kzalloc(sizeof(struct swr_mstr_ctrl), GFP_KERNEL); |
| 1344 | if (!swrm) { |
| 1345 | ret = -ENOMEM; |
| 1346 | goto err_memory_fail; |
| 1347 | } |
| 1348 | swrm->dev = &pdev->dev; |
| 1349 | swrm->pdev = pdev; |
| 1350 | platform_set_drvdata(pdev, swrm); |
| 1351 | swr_set_ctrl_data(&swrm->master, swrm); |
| 1352 | pdata = dev_get_platdata(&pdev->dev); |
| 1353 | if (!pdata) { |
| 1354 | dev_err(&pdev->dev, "%s: pdata from parent is NULL\n", |
| 1355 | __func__); |
| 1356 | ret = -EINVAL; |
| 1357 | goto err_pdata_fail; |
| 1358 | } |
| 1359 | swrm->handle = (void *)pdata->handle; |
| 1360 | if (!swrm->handle) { |
| 1361 | dev_err(&pdev->dev, "%s: swrm->handle is NULL\n", |
| 1362 | __func__); |
| 1363 | ret = -EINVAL; |
| 1364 | goto err_pdata_fail; |
| 1365 | } |
| 1366 | swrm->read = pdata->read; |
| 1367 | if (!swrm->read) { |
| 1368 | dev_err(&pdev->dev, "%s: swrm->read is NULL\n", |
| 1369 | __func__); |
| 1370 | ret = -EINVAL; |
| 1371 | goto err_pdata_fail; |
| 1372 | } |
| 1373 | swrm->write = pdata->write; |
| 1374 | if (!swrm->write) { |
| 1375 | dev_err(&pdev->dev, "%s: swrm->write is NULL\n", |
| 1376 | __func__); |
| 1377 | ret = -EINVAL; |
| 1378 | goto err_pdata_fail; |
| 1379 | } |
| 1380 | swrm->bulk_write = pdata->bulk_write; |
| 1381 | if (!swrm->bulk_write) { |
| 1382 | dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n", |
| 1383 | __func__); |
| 1384 | ret = -EINVAL; |
| 1385 | goto err_pdata_fail; |
| 1386 | } |
| 1387 | swrm->clk = pdata->clk; |
| 1388 | if (!swrm->clk) { |
| 1389 | dev_err(&pdev->dev, "%s: swrm->clk is NULL\n", |
| 1390 | __func__); |
| 1391 | ret = -EINVAL; |
| 1392 | goto err_pdata_fail; |
| 1393 | } |
| 1394 | swrm->reg_irq = pdata->reg_irq; |
| 1395 | if (!swrm->reg_irq) { |
| 1396 | dev_err(&pdev->dev, "%s: swrm->reg_irq is NULL\n", |
| 1397 | __func__); |
| 1398 | ret = -EINVAL; |
| 1399 | goto err_pdata_fail; |
| 1400 | } |
| 1401 | swrm->master.read = swrm_read; |
| 1402 | swrm->master.write = swrm_write; |
| 1403 | swrm->master.bulk_write = swrm_bulk_write; |
| 1404 | swrm->master.get_logical_dev_num = swrm_get_logical_dev_num; |
| 1405 | swrm->master.connect_port = swrm_connect_port; |
| 1406 | swrm->master.disconnect_port = swrm_disconnect_port; |
| 1407 | swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control; |
| 1408 | swrm->master.remove_from_group = swrm_remove_from_group; |
| 1409 | swrm->master.dev.parent = &pdev->dev; |
| 1410 | swrm->master.dev.of_node = pdev->dev.of_node; |
| 1411 | swrm->master.num_port = 0; |
| 1412 | swrm->num_enum_slaves = 0; |
| 1413 | swrm->rcmd_id = 0; |
| 1414 | swrm->wcmd_id = 0; |
| 1415 | swrm->slave_status = 0; |
| 1416 | swrm->num_rx_chs = 0; |
| 1417 | swrm->state = SWR_MSTR_RESUME; |
| 1418 | init_completion(&swrm->reset); |
| 1419 | init_completion(&swrm->broadcast); |
| 1420 | mutex_init(&swrm->mlock); |
| 1421 | INIT_LIST_HEAD(&swrm->mport_list); |
| 1422 | mutex_init(&swrm->reslock); |
| 1423 | |
| 1424 | ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm, |
| 1425 | SWR_IRQ_REGISTER); |
| 1426 | if (ret) { |
| 1427 | dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n", |
| 1428 | __func__, ret); |
| 1429 | goto err_irq_fail; |
| 1430 | } |
| 1431 | |
| 1432 | ret = swr_register_master(&swrm->master); |
| 1433 | if (ret) { |
| 1434 | dev_err(&pdev->dev, "%s: error adding swr master\n", __func__); |
| 1435 | goto err_mstr_fail; |
| 1436 | } |
| 1437 | |
| 1438 | if (pdev->dev.of_node) |
| 1439 | of_register_swr_devices(&swrm->master); |
| 1440 | |
| 1441 | /* Add devices registered with board-info as the |
| 1442 | * controller will be up now |
| 1443 | */ |
| 1444 | swr_master_add_boarddevices(&swrm->master); |
| 1445 | mutex_lock(&swrm->mlock); |
| 1446 | swrm_clk_request(swrm, true); |
| 1447 | ret = swrm_master_init(swrm); |
| 1448 | if (ret < 0) { |
| 1449 | dev_err(&pdev->dev, |
| 1450 | "%s: Error in master Initializaiton, err %d\n", |
| 1451 | __func__, ret); |
| 1452 | mutex_unlock(&swrm->mlock); |
| 1453 | goto err_mstr_fail; |
| 1454 | } |
| 1455 | |
| 1456 | /* Enumerate slave devices */ |
| 1457 | list_for_each_entry_safe(swr_dev, safe, &swrm->master.devices, |
| 1458 | dev_list) { |
| 1459 | ret = swr_startup_devices(swr_dev); |
| 1460 | if (ret) |
| 1461 | list_del(&swr_dev->dev_list); |
| 1462 | } |
| 1463 | mutex_unlock(&swrm->mlock); |
| 1464 | |
| 1465 | dbgswrm = swrm; |
| 1466 | debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0); |
| 1467 | if (!IS_ERR(debugfs_swrm_dent)) { |
| 1468 | debugfs_peek = debugfs_create_file("swrm_peek", |
| 1469 | S_IFREG | 0444, debugfs_swrm_dent, |
| 1470 | (void *) "swrm_peek", &swrm_debug_ops); |
| 1471 | |
| 1472 | debugfs_poke = debugfs_create_file("swrm_poke", |
| 1473 | S_IFREG | 0444, debugfs_swrm_dent, |
| 1474 | (void *) "swrm_poke", &swrm_debug_ops); |
| 1475 | |
| 1476 | debugfs_reg_dump = debugfs_create_file("swrm_reg_dump", |
| 1477 | S_IFREG | 0444, debugfs_swrm_dent, |
| 1478 | (void *) "swrm_reg_dump", |
| 1479 | &swrm_debug_ops); |
| 1480 | } |
| 1481 | pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer); |
| 1482 | pm_runtime_use_autosuspend(&pdev->dev); |
| 1483 | pm_runtime_set_active(&pdev->dev); |
| 1484 | pm_runtime_enable(&pdev->dev); |
| 1485 | pm_runtime_mark_last_busy(&pdev->dev); |
| 1486 | |
| 1487 | return 0; |
| 1488 | err_mstr_fail: |
| 1489 | swrm->reg_irq(swrm->handle, swr_mstr_interrupt, |
| 1490 | swrm, SWR_IRQ_FREE); |
| 1491 | err_irq_fail: |
| 1492 | err_pdata_fail: |
| 1493 | kfree(swrm); |
| 1494 | err_memory_fail: |
| 1495 | return ret; |
| 1496 | } |
| 1497 | |
| 1498 | static int swrm_remove(struct platform_device *pdev) |
| 1499 | { |
| 1500 | struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev); |
| 1501 | |
| 1502 | swrm->reg_irq(swrm->handle, swr_mstr_interrupt, |
| 1503 | swrm, SWR_IRQ_FREE); |
| 1504 | if (swrm->mstr_port) { |
| 1505 | kfree(swrm->mstr_port->port); |
| 1506 | swrm->mstr_port->port = NULL; |
| 1507 | kfree(swrm->mstr_port); |
| 1508 | swrm->mstr_port = NULL; |
| 1509 | } |
| 1510 | pm_runtime_disable(&pdev->dev); |
| 1511 | pm_runtime_set_suspended(&pdev->dev); |
| 1512 | swr_unregister_master(&swrm->master); |
| 1513 | mutex_destroy(&swrm->mlock); |
| 1514 | mutex_destroy(&swrm->reslock); |
| 1515 | kfree(swrm); |
| 1516 | return 0; |
| 1517 | } |
| 1518 | |
| 1519 | static int swrm_clk_pause(struct swr_mstr_ctrl *swrm) |
| 1520 | { |
| 1521 | u32 val; |
| 1522 | |
| 1523 | dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state); |
| 1524 | swrm->write(swrm->handle, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD); |
| 1525 | val = swrm->read(swrm->handle, SWRM_MCP_CFG_ADDR); |
| 1526 | val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK; |
| 1527 | swrm->write(swrm->handle, SWRM_MCP_CFG_ADDR, val); |
| 1528 | swrm->state = SWR_MSTR_PAUSE; |
| 1529 | |
| 1530 | return 0; |
| 1531 | } |
| 1532 | |
| 1533 | #ifdef CONFIG_PM |
| 1534 | static int swrm_runtime_resume(struct device *dev) |
| 1535 | { |
| 1536 | struct platform_device *pdev = to_platform_device(dev); |
| 1537 | struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev); |
| 1538 | int ret = 0; |
| 1539 | struct swr_master *mstr = &swrm->master; |
| 1540 | struct swr_device *swr_dev; |
| 1541 | |
| 1542 | dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n", |
| 1543 | __func__, swrm->state); |
| 1544 | mutex_lock(&swrm->reslock); |
| 1545 | if ((swrm->state == SWR_MSTR_PAUSE) || |
| 1546 | (swrm->state == SWR_MSTR_DOWN)) { |
| 1547 | if (swrm->state == SWR_MSTR_DOWN) { |
| 1548 | if (swrm_clk_request(swrm, true)) |
| 1549 | goto exit; |
| 1550 | } |
| 1551 | list_for_each_entry(swr_dev, &mstr->devices, dev_list) { |
| 1552 | ret = swr_device_up(swr_dev); |
| 1553 | if (ret) { |
| 1554 | dev_err(dev, |
| 1555 | "%s: failed to wakeup swr dev %d\n", |
| 1556 | __func__, swr_dev->dev_num); |
| 1557 | swrm_clk_request(swrm, false); |
| 1558 | goto exit; |
| 1559 | } |
| 1560 | } |
| 1561 | swrm->write(swrm->handle, SWRM_COMP_SW_RESET, 0x01); |
| 1562 | swrm->write(swrm->handle, SWRM_COMP_SW_RESET, 0x01); |
| 1563 | swrm_master_init(swrm); |
| 1564 | } |
| 1565 | exit: |
| 1566 | pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer); |
| 1567 | mutex_unlock(&swrm->reslock); |
| 1568 | return ret; |
| 1569 | } |
| 1570 | |
| 1571 | static int swrm_runtime_suspend(struct device *dev) |
| 1572 | { |
| 1573 | struct platform_device *pdev = to_platform_device(dev); |
| 1574 | struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev); |
| 1575 | int ret = 0; |
| 1576 | struct swr_master *mstr = &swrm->master; |
| 1577 | struct swr_device *swr_dev; |
| 1578 | |
| 1579 | dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n", |
| 1580 | __func__, swrm->state); |
| 1581 | mutex_lock(&swrm->reslock); |
| 1582 | if ((swrm->state == SWR_MSTR_RESUME) || |
| 1583 | (swrm->state == SWR_MSTR_UP)) { |
| 1584 | if (swrm_is_port_en(&swrm->master)) { |
| 1585 | dev_dbg(dev, "%s ports are enabled\n", __func__); |
| 1586 | ret = -EBUSY; |
| 1587 | goto exit; |
| 1588 | } |
| 1589 | swrm_clk_pause(swrm); |
| 1590 | swrm->write(swrm->handle, SWRM_COMP_CFG_ADDR, 0x00); |
| 1591 | list_for_each_entry(swr_dev, &mstr->devices, dev_list) { |
| 1592 | ret = swr_device_down(swr_dev); |
| 1593 | if (ret) { |
| 1594 | dev_err(dev, |
| 1595 | "%s: failed to shutdown swr dev %d\n", |
| 1596 | __func__, swr_dev->dev_num); |
| 1597 | goto exit; |
| 1598 | } |
| 1599 | } |
| 1600 | swrm_clk_request(swrm, false); |
| 1601 | } |
| 1602 | exit: |
| 1603 | mutex_unlock(&swrm->reslock); |
| 1604 | return ret; |
| 1605 | } |
| 1606 | #endif /* CONFIG_PM */ |
| 1607 | |
| 1608 | static int swrm_device_down(struct device *dev) |
| 1609 | { |
| 1610 | struct platform_device *pdev = to_platform_device(dev); |
| 1611 | struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev); |
| 1612 | int ret = 0; |
| 1613 | struct swr_master *mstr = &swrm->master; |
| 1614 | struct swr_device *swr_dev; |
| 1615 | |
| 1616 | dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state); |
| 1617 | mutex_lock(&swrm->reslock); |
| 1618 | if ((swrm->state == SWR_MSTR_RESUME) || |
| 1619 | (swrm->state == SWR_MSTR_UP)) { |
| 1620 | list_for_each_entry(swr_dev, &mstr->devices, dev_list) { |
| 1621 | ret = swr_device_down(swr_dev); |
| 1622 | if (ret) |
| 1623 | dev_err(dev, |
| 1624 | "%s: failed to shutdown swr dev %d\n", |
| 1625 | __func__, swr_dev->dev_num); |
| 1626 | } |
| 1627 | dev_dbg(dev, "%s: Shutting down SWRM\n", __func__); |
| 1628 | pm_runtime_disable(dev); |
| 1629 | pm_runtime_set_suspended(dev); |
| 1630 | pm_runtime_enable(dev); |
| 1631 | swrm_clk_request(swrm, false); |
| 1632 | } |
| 1633 | mutex_unlock(&swrm->reslock); |
| 1634 | return ret; |
| 1635 | } |
| 1636 | |
| 1637 | /** |
| 1638 | * swrm_wcd_notify - parent device can notify to soundwire master through |
| 1639 | * this function |
| 1640 | * @pdev: pointer to platform device structure |
| 1641 | * @id: command id from parent to the soundwire master |
| 1642 | * @data: data from parent device to soundwire master |
| 1643 | */ |
| 1644 | int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data) |
| 1645 | { |
| 1646 | struct swr_mstr_ctrl *swrm; |
| 1647 | int ret = 0; |
| 1648 | struct swr_master *mstr; |
| 1649 | struct swr_device *swr_dev; |
| 1650 | |
| 1651 | if (!pdev) { |
| 1652 | pr_err("%s: pdev is NULL\n", __func__); |
| 1653 | return -EINVAL; |
| 1654 | } |
| 1655 | swrm = platform_get_drvdata(pdev); |
| 1656 | if (!swrm) { |
| 1657 | dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__); |
| 1658 | return -EINVAL; |
| 1659 | } |
| 1660 | mstr = &swrm->master; |
| 1661 | |
| 1662 | switch (id) { |
| 1663 | case SWR_CH_MAP: |
| 1664 | if (!data) { |
| 1665 | dev_err(swrm->dev, "%s: data is NULL\n", __func__); |
| 1666 | ret = -EINVAL; |
| 1667 | } else { |
| 1668 | ret = swrm_set_ch_map(swrm, data); |
| 1669 | } |
| 1670 | break; |
| 1671 | case SWR_DEVICE_DOWN: |
| 1672 | dev_dbg(swrm->dev, "%s: swr master down called\n", __func__); |
| 1673 | mutex_lock(&swrm->mlock); |
| 1674 | if ((swrm->state == SWR_MSTR_PAUSE) || |
| 1675 | (swrm->state == SWR_MSTR_DOWN)) |
| 1676 | dev_dbg(swrm->dev, "%s: SWR master is already Down: %d\n", |
| 1677 | __func__, swrm->state); |
| 1678 | else |
| 1679 | swrm_device_down(&pdev->dev); |
| 1680 | mutex_unlock(&swrm->mlock); |
| 1681 | break; |
| 1682 | case SWR_DEVICE_UP: |
| 1683 | dev_dbg(swrm->dev, "%s: swr master up called\n", __func__); |
| 1684 | mutex_lock(&swrm->mlock); |
| 1685 | mutex_lock(&swrm->reslock); |
| 1686 | if ((swrm->state == SWR_MSTR_RESUME) || |
| 1687 | (swrm->state == SWR_MSTR_UP)) { |
| 1688 | dev_dbg(swrm->dev, "%s: SWR master is already UP: %d\n", |
| 1689 | __func__, swrm->state); |
| 1690 | } else { |
| 1691 | pm_runtime_mark_last_busy(&pdev->dev); |
| 1692 | mutex_unlock(&swrm->reslock); |
| 1693 | pm_runtime_get_sync(&pdev->dev); |
| 1694 | mutex_lock(&swrm->reslock); |
| 1695 | list_for_each_entry(swr_dev, &mstr->devices, dev_list) { |
| 1696 | ret = swr_reset_device(swr_dev); |
| 1697 | if (ret) { |
| 1698 | dev_err(swrm->dev, |
| 1699 | "%s: failed to reset swr device %d\n", |
| 1700 | __func__, swr_dev->dev_num); |
| 1701 | swrm_clk_request(swrm, false); |
| 1702 | } |
| 1703 | } |
| 1704 | pm_runtime_mark_last_busy(&pdev->dev); |
| 1705 | pm_runtime_put_autosuspend(&pdev->dev); |
| 1706 | } |
| 1707 | mutex_unlock(&swrm->reslock); |
| 1708 | mutex_unlock(&swrm->mlock); |
| 1709 | break; |
| 1710 | case SWR_SET_NUM_RX_CH: |
| 1711 | if (!data) { |
| 1712 | dev_err(swrm->dev, "%s: data is NULL\n", __func__); |
| 1713 | ret = -EINVAL; |
| 1714 | } else { |
| 1715 | mutex_lock(&swrm->mlock); |
| 1716 | swrm->num_rx_chs = *(int *)data; |
| 1717 | if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) { |
| 1718 | list_for_each_entry(swr_dev, &mstr->devices, |
| 1719 | dev_list) { |
| 1720 | ret = swr_set_device_group(swr_dev, |
| 1721 | SWR_BROADCAST); |
| 1722 | if (ret) |
| 1723 | dev_err(swrm->dev, |
| 1724 | "%s: set num ch failed\n", |
| 1725 | __func__); |
| 1726 | } |
| 1727 | } else { |
| 1728 | list_for_each_entry(swr_dev, &mstr->devices, |
| 1729 | dev_list) { |
| 1730 | ret = swr_set_device_group(swr_dev, |
| 1731 | SWR_GROUP_NONE); |
| 1732 | if (ret) |
| 1733 | dev_err(swrm->dev, |
| 1734 | "%s: set num ch failed\n", |
| 1735 | __func__); |
| 1736 | } |
| 1737 | } |
| 1738 | mutex_unlock(&swrm->mlock); |
| 1739 | } |
| 1740 | break; |
| 1741 | default: |
| 1742 | dev_err(swrm->dev, "%s: swr master unknown id %d\n", |
| 1743 | __func__, id); |
| 1744 | break; |
| 1745 | } |
| 1746 | return ret; |
| 1747 | } |
| 1748 | EXPORT_SYMBOL(swrm_wcd_notify); |
| 1749 | |
| 1750 | #ifdef CONFIG_PM_SLEEP |
| 1751 | static int swrm_suspend(struct device *dev) |
| 1752 | { |
| 1753 | int ret = -EBUSY; |
| 1754 | struct platform_device *pdev = to_platform_device(dev); |
| 1755 | struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev); |
| 1756 | |
| 1757 | dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state); |
| 1758 | if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) { |
| 1759 | ret = swrm_runtime_suspend(dev); |
| 1760 | if (!ret) { |
| 1761 | /* |
| 1762 | * Synchronize runtime-pm and system-pm states: |
| 1763 | * At this point, we are already suspended. If |
| 1764 | * runtime-pm still thinks its active, then |
| 1765 | * make sure its status is in sync with HW |
| 1766 | * status. The three below calls let the |
| 1767 | * runtime-pm know that we are suspended |
| 1768 | * already without re-invoking the suspend |
| 1769 | * callback |
| 1770 | */ |
| 1771 | pm_runtime_disable(dev); |
| 1772 | pm_runtime_set_suspended(dev); |
| 1773 | pm_runtime_enable(dev); |
| 1774 | } |
| 1775 | } |
| 1776 | if (ret == -EBUSY) { |
| 1777 | /* |
| 1778 | * There is a possibility that some audio stream is active |
| 1779 | * during suspend. We dont want to return suspend failure in |
| 1780 | * that case so that display and relevant components can still |
| 1781 | * go to suspend. |
| 1782 | * If there is some other error, then it should be passed-on |
| 1783 | * to system level suspend |
| 1784 | */ |
| 1785 | ret = 0; |
| 1786 | } |
| 1787 | return ret; |
| 1788 | } |
| 1789 | |
| 1790 | static int swrm_resume(struct device *dev) |
| 1791 | { |
| 1792 | int ret = 0; |
| 1793 | struct platform_device *pdev = to_platform_device(dev); |
| 1794 | struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev); |
| 1795 | |
| 1796 | dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state); |
| 1797 | if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) { |
| 1798 | ret = swrm_runtime_resume(dev); |
| 1799 | if (!ret) { |
| 1800 | pm_runtime_mark_last_busy(dev); |
| 1801 | pm_request_autosuspend(dev); |
| 1802 | } |
| 1803 | } |
| 1804 | return ret; |
| 1805 | } |
| 1806 | #endif /* CONFIG_PM_SLEEP */ |
| 1807 | |
| 1808 | static const struct dev_pm_ops swrm_dev_pm_ops = { |
| 1809 | SET_SYSTEM_SLEEP_PM_OPS( |
| 1810 | swrm_suspend, |
| 1811 | swrm_resume |
| 1812 | ) |
| 1813 | SET_RUNTIME_PM_OPS( |
| 1814 | swrm_runtime_suspend, |
| 1815 | swrm_runtime_resume, |
| 1816 | NULL |
| 1817 | ) |
| 1818 | }; |
| 1819 | |
| 1820 | static const struct of_device_id swrm_dt_match[] = { |
| 1821 | { |
| 1822 | .compatible = "qcom,swr-wcd", |
| 1823 | }, |
| 1824 | {} |
| 1825 | }; |
| 1826 | |
| 1827 | static struct platform_driver swr_mstr_driver = { |
| 1828 | .probe = swrm_probe, |
| 1829 | .remove = swrm_remove, |
| 1830 | .driver = { |
| 1831 | .name = SWR_WCD_NAME, |
| 1832 | .owner = THIS_MODULE, |
| 1833 | .pm = &swrm_dev_pm_ops, |
| 1834 | .of_match_table = swrm_dt_match, |
| 1835 | }, |
| 1836 | }; |
| 1837 | |
| 1838 | static int __init swrm_init(void) |
| 1839 | { |
| 1840 | return platform_driver_register(&swr_mstr_driver); |
| 1841 | } |
| 1842 | subsys_initcall(swrm_init); |
| 1843 | |
| 1844 | static void __exit swrm_exit(void) |
| 1845 | { |
| 1846 | platform_driver_unregister(&swr_mstr_driver); |
| 1847 | } |
| 1848 | module_exit(swrm_exit); |
| 1849 | |
| 1850 | |
| 1851 | MODULE_LICENSE("GPL v2"); |
| 1852 | MODULE_DESCRIPTION("WCD SoundWire Controller"); |
| 1853 | MODULE_ALIAS("platform:swr-wcd"); |