| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * Copyright (C) 2008, Creative Technology Ltd. All Rights Reserved. |
| 4 | * |
| 5 | * @File ctdaio.c |
| 6 | * |
| 7 | * @Brief |
| 8 | * This file contains the implementation of Digital Audio Input Output |
| 9 | * resource management object. |
| 10 | * |
| 11 | * @Author Liu Chun |
| 12 | * @Date May 23 2008 |
| 13 | */ |
| 14 | |
| 15 | #include "ctdaio.h" |
| 16 | #include "cthardware.h" |
| 17 | #include "ctimap.h" |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/kernel.h> |
| 20 | |
| 21 | struct daio_usage { |
| 22 | unsigned short data; |
| 23 | }; |
| 24 | |
| 25 | struct daio_rsc_idx { |
| 26 | unsigned short left; |
| 27 | unsigned short right; |
| 28 | }; |
| 29 | |
| 30 | static const struct daio_rsc_idx idx_20k1[NUM_DAIOTYP] = { |
| 31 | [LINEO1] = {.left = 0x00, .right = 0x01}, |
| 32 | [LINEO2] = {.left = 0x18, .right = 0x19}, |
| 33 | [LINEO3] = {.left = 0x08, .right = 0x09}, |
| 34 | [LINEO4] = {.left = 0x10, .right = 0x11}, |
| 35 | [LINEIM] = {.left = 0x1b5, .right = 0x1bd}, |
| 36 | [SPDIFOO] = {.left = 0x20, .right = 0x21}, |
| 37 | [SPDIFIO] = {.left = 0x15, .right = 0x1d}, |
| 38 | [SPDIFI1] = {.left = 0x95, .right = 0x9d}, |
| 39 | }; |
| 40 | |
| 41 | static const struct daio_rsc_idx idx_20k2[NUM_DAIOTYP] = { |
| 42 | [LINEO1] = {.left = 0x40, .right = 0x41}, |
| 43 | [LINEO2] = {.left = 0x60, .right = 0x61}, |
| 44 | [LINEO3] = {.left = 0x50, .right = 0x51}, |
| 45 | [LINEO4] = {.left = 0x70, .right = 0x71}, |
| 46 | [LINEIM] = {.left = 0x45, .right = 0xc5}, |
| 47 | [MIC] = {.left = 0x55, .right = 0xd5}, |
| 48 | [RCA] = {.left = 0x30, .right = 0x31}, |
| 49 | [SPDIFOO] = {.left = 0x00, .right = 0x01}, |
| 50 | [SPDIFIO] = {.left = 0x05, .right = 0x85}, |
| 51 | }; |
| 52 | |
| 53 | static void daio_master(struct rsc *rsc) |
| 54 | { |
| 55 | /* Actually, this is not the resource index of DAIO. |
| 56 | * For DAO, it is the input mapper index. And, for DAI, |
| 57 | * it is the output time-slot index. */ |
| 58 | rsc->conj = rsc->idx; |
| 59 | } |
| 60 | |
| 61 | static int daio_index(const struct rsc *rsc) |
| 62 | { |
| 63 | return rsc->conj; |
| 64 | } |
| 65 | |
| 66 | static void daio_out_next_conj(struct rsc *rsc) |
| 67 | { |
| 68 | rsc->conj += 2; |
| 69 | } |
| 70 | |
| 71 | static void daio_in_next_conj_20k1(struct rsc *rsc) |
| 72 | { |
| 73 | rsc->conj += 0x200; |
| 74 | } |
| 75 | |
| 76 | static void daio_in_next_conj_20k2(struct rsc *rsc) |
| 77 | { |
| 78 | rsc->conj += 0x100; |
| 79 | } |
| 80 | |
| 81 | static const struct rsc_ops daio_out_rsc_ops = { |
| 82 | .master = daio_master, |
| 83 | .next_conj = daio_out_next_conj, |
| 84 | .index = daio_index, |
| 85 | .output_slot = NULL, |
| 86 | }; |
| 87 | |
| 88 | static const struct rsc_ops daio_in_rsc_ops_20k1 = { |
| 89 | .master = daio_master, |
| 90 | .next_conj = daio_in_next_conj_20k1, |
| 91 | .index = NULL, |
| 92 | .output_slot = daio_index, |
| 93 | }; |
| 94 | |
| 95 | static const struct rsc_ops daio_in_rsc_ops_20k2 = { |
| 96 | .master = daio_master, |
| 97 | .next_conj = daio_in_next_conj_20k2, |
| 98 | .index = NULL, |
| 99 | .output_slot = daio_index, |
| 100 | }; |
| 101 | |
| 102 | static unsigned int daio_device_index(enum DAIOTYP type, struct hw *hw) |
| 103 | { |
| 104 | switch (hw->chip_type) { |
| 105 | case ATC20K1: |
| 106 | switch (type) { |
| 107 | case SPDIFOO: return 0; |
| 108 | case SPDIFIO: return 0; |
| 109 | case SPDIFI1: return 1; |
| 110 | case LINEO1: return 4; |
| 111 | case LINEO2: return 7; |
| 112 | case LINEO3: return 5; |
| 113 | case LINEO4: return 6; |
| 114 | case LINEIM: return 7; |
| 115 | default: return -EINVAL; |
| 116 | } |
| 117 | case ATC20K2: |
| 118 | switch (type) { |
| 119 | case SPDIFOO: return 0; |
| 120 | case SPDIFIO: return 0; |
| 121 | case LINEO1: return 4; |
| 122 | case LINEO2: return 7; |
| 123 | case LINEO3: return 5; |
| 124 | case LINEO4: return 6; |
| 125 | case LINEIM: return 4; |
| 126 | case MIC: return 5; |
| 127 | case RCA: return 3; |
| 128 | default: return -EINVAL; |
| 129 | } |
| 130 | default: |
| 131 | return -EINVAL; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | static int dao_rsc_reinit(struct dao *dao, const struct dao_desc *desc); |
| 136 | |
| 137 | static int dao_spdif_get_spos(struct dao *dao, unsigned int *spos) |
| 138 | { |
| 139 | dao->hw->dao_get_spos(dao->ctrl_blk, spos); |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static int dao_spdif_set_spos(struct dao *dao, unsigned int spos) |
| 144 | { |
| 145 | dao->hw->dao_set_spos(dao->ctrl_blk, spos); |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | static int dao_commit_write(struct dao *dao) |
| 150 | { |
| 151 | dao->hw->dao_commit_write(dao->hw, |
| 152 | daio_device_index(type: dao->daio.type, hw: dao->hw), dao->ctrl_blk); |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | static int dao_set_left_input(struct dao *dao, struct rsc *input) |
| 157 | { |
| 158 | struct imapper *entry; |
| 159 | struct daio *daio = &dao->daio; |
| 160 | int i; |
| 161 | |
| 162 | entry = kcalloc(daio->rscl.msr, sizeof(*entry), GFP_KERNEL); |
| 163 | if (!entry) |
| 164 | return -ENOMEM; |
| 165 | |
| 166 | dao->ops->clear_left_input(dao); |
| 167 | /* Program master and conjugate resources */ |
| 168 | input->ops->master(input); |
| 169 | daio->rscl.ops->master(&daio->rscl); |
| 170 | for (i = 0; i < daio->rscl.msr; i++, entry++) { |
| 171 | entry->slot = input->ops->output_slot(input); |
| 172 | entry->user = entry->addr = daio->rscl.ops->index(&daio->rscl); |
| 173 | dao->mgr->imap_add(dao->mgr, entry); |
| 174 | dao->imappers[i] = entry; |
| 175 | |
| 176 | input->ops->next_conj(input); |
| 177 | daio->rscl.ops->next_conj(&daio->rscl); |
| 178 | } |
| 179 | input->ops->master(input); |
| 180 | daio->rscl.ops->master(&daio->rscl); |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static int dao_set_right_input(struct dao *dao, struct rsc *input) |
| 186 | { |
| 187 | struct imapper *entry; |
| 188 | struct daio *daio = &dao->daio; |
| 189 | int i; |
| 190 | |
| 191 | entry = kcalloc(daio->rscr.msr, sizeof(*entry), GFP_KERNEL); |
| 192 | if (!entry) |
| 193 | return -ENOMEM; |
| 194 | |
| 195 | dao->ops->clear_right_input(dao); |
| 196 | /* Program master and conjugate resources */ |
| 197 | input->ops->master(input); |
| 198 | daio->rscr.ops->master(&daio->rscr); |
| 199 | for (i = 0; i < daio->rscr.msr; i++, entry++) { |
| 200 | entry->slot = input->ops->output_slot(input); |
| 201 | entry->user = entry->addr = daio->rscr.ops->index(&daio->rscr); |
| 202 | dao->mgr->imap_add(dao->mgr, entry); |
| 203 | dao->imappers[daio->rscl.msr + i] = entry; |
| 204 | |
| 205 | input->ops->next_conj(input); |
| 206 | daio->rscr.ops->next_conj(&daio->rscr); |
| 207 | } |
| 208 | input->ops->master(input); |
| 209 | daio->rscr.ops->master(&daio->rscr); |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | static int dao_clear_input(struct dao *dao, unsigned int start, unsigned int end) |
| 215 | { |
| 216 | unsigned int i; |
| 217 | |
| 218 | if (!dao->imappers[start]) |
| 219 | return 0; |
| 220 | for (i = start; i < end; i++) { |
| 221 | dao->mgr->imap_delete(dao->mgr, dao->imappers[i]); |
| 222 | dao->imappers[i] = NULL; |
| 223 | } |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | |
| 229 | static int dao_clear_left_input(struct dao *dao) |
| 230 | { |
| 231 | return dao_clear_input(dao, start: 0, end: dao->daio.rscl.msr); |
| 232 | } |
| 233 | |
| 234 | static int dao_clear_right_input(struct dao *dao) |
| 235 | { |
| 236 | return dao_clear_input(dao, start: dao->daio.rscl.msr, |
| 237 | end: dao->daio.rscl.msr + dao->daio.rscr.msr); |
| 238 | } |
| 239 | |
| 240 | static const struct dao_rsc_ops dao_ops = { |
| 241 | .set_spos = dao_spdif_set_spos, |
| 242 | .commit_write = dao_commit_write, |
| 243 | .get_spos = dao_spdif_get_spos, |
| 244 | .reinit = dao_rsc_reinit, |
| 245 | .set_left_input = dao_set_left_input, |
| 246 | .set_right_input = dao_set_right_input, |
| 247 | .clear_left_input = dao_clear_left_input, |
| 248 | .clear_right_input = dao_clear_right_input, |
| 249 | }; |
| 250 | |
| 251 | static int dai_set_srt_srcl(struct dai *dai, struct rsc *src) |
| 252 | { |
| 253 | src->ops->master(src); |
| 254 | dai->hw->dai_srt_set_srcm(dai->ctrl_blk, src->ops->index(src)); |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | static int dai_set_srt_srcr(struct dai *dai, struct rsc *src) |
| 259 | { |
| 260 | src->ops->master(src); |
| 261 | dai->hw->dai_srt_set_srco(dai->ctrl_blk, src->ops->index(src)); |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | static int dai_set_srt_msr(struct dai *dai, unsigned int msr) |
| 266 | { |
| 267 | unsigned int rsr; |
| 268 | |
| 269 | for (rsr = 0; msr > 1; msr >>= 1) |
| 270 | rsr++; |
| 271 | |
| 272 | dai->hw->dai_srt_set_rsr(dai->ctrl_blk, rsr); |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | static int dai_set_enb_src(struct dai *dai, unsigned int enb) |
| 277 | { |
| 278 | dai->hw->dai_srt_set_ec(dai->ctrl_blk, enb); |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | static int dai_set_enb_srt(struct dai *dai, unsigned int enb) |
| 283 | { |
| 284 | dai->hw->dai_srt_set_et(dai->ctrl_blk, enb); |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | static int dai_commit_write(struct dai *dai) |
| 289 | { |
| 290 | dai->hw->dai_commit_write(dai->hw, |
| 291 | daio_device_index(type: dai->daio.type, hw: dai->hw), dai->ctrl_blk); |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | static const struct dai_rsc_ops dai_ops = { |
| 296 | .set_srt_srcl = dai_set_srt_srcl, |
| 297 | .set_srt_srcr = dai_set_srt_srcr, |
| 298 | .set_srt_msr = dai_set_srt_msr, |
| 299 | .set_enb_src = dai_set_enb_src, |
| 300 | .set_enb_srt = dai_set_enb_srt, |
| 301 | .commit_write = dai_commit_write, |
| 302 | }; |
| 303 | |
| 304 | static int daio_rsc_init(struct daio *daio, |
| 305 | const struct daio_desc *desc, |
| 306 | struct hw *hw) |
| 307 | { |
| 308 | int err; |
| 309 | unsigned int idx_l, idx_r; |
| 310 | |
| 311 | switch (hw->chip_type) { |
| 312 | case ATC20K1: |
| 313 | idx_l = idx_20k1[desc->type].left; |
| 314 | idx_r = idx_20k1[desc->type].right; |
| 315 | break; |
| 316 | case ATC20K2: |
| 317 | idx_l = idx_20k2[desc->type].left; |
| 318 | idx_r = idx_20k2[desc->type].right; |
| 319 | break; |
| 320 | default: |
| 321 | return -EINVAL; |
| 322 | } |
| 323 | err = rsc_init(rsc: &daio->rscl, idx: idx_l, type: DAIO, msr: desc->msr, hw); |
| 324 | if (err) |
| 325 | return err; |
| 326 | |
| 327 | err = rsc_init(rsc: &daio->rscr, idx: idx_r, type: DAIO, msr: desc->msr, hw); |
| 328 | if (err) |
| 329 | goto error1; |
| 330 | |
| 331 | /* Set daio->rscl/r->ops to daio specific ones */ |
| 332 | if (desc->output) { |
| 333 | daio->rscl.ops = daio->rscr.ops = &daio_out_rsc_ops; |
| 334 | } else { |
| 335 | switch (hw->chip_type) { |
| 336 | case ATC20K1: |
| 337 | daio->rscl.ops = daio->rscr.ops = &daio_in_rsc_ops_20k1; |
| 338 | break; |
| 339 | case ATC20K2: |
| 340 | daio->rscl.ops = daio->rscr.ops = &daio_in_rsc_ops_20k2; |
| 341 | break; |
| 342 | default: |
| 343 | break; |
| 344 | } |
| 345 | } |
| 346 | daio->type = desc->type; |
| 347 | daio->output = desc->output; |
| 348 | |
| 349 | return 0; |
| 350 | |
| 351 | error1: |
| 352 | rsc_uninit(rsc: &daio->rscl); |
| 353 | return err; |
| 354 | } |
| 355 | |
| 356 | static int daio_rsc_uninit(struct daio *daio) |
| 357 | { |
| 358 | rsc_uninit(rsc: &daio->rscl); |
| 359 | rsc_uninit(rsc: &daio->rscr); |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | static int dao_rsc_init(struct dao *dao, |
| 365 | const struct daio_desc *desc, |
| 366 | struct daio_mgr *mgr) |
| 367 | { |
| 368 | struct hw *hw = mgr->mgr.hw; |
| 369 | unsigned int conf; |
| 370 | int err; |
| 371 | |
| 372 | err = daio_rsc_init(daio: &dao->daio, desc, hw: mgr->mgr.hw); |
| 373 | if (err) |
| 374 | return err; |
| 375 | |
| 376 | dao->imappers = kzalloc(array3_size(sizeof(void *), desc->msr, 2), |
| 377 | GFP_KERNEL); |
| 378 | if (!dao->imappers) { |
| 379 | err = -ENOMEM; |
| 380 | goto error1; |
| 381 | } |
| 382 | dao->ops = &dao_ops; |
| 383 | dao->mgr = mgr; |
| 384 | dao->hw = hw; |
| 385 | err = hw->dao_get_ctrl_blk(&dao->ctrl_blk); |
| 386 | if (err) |
| 387 | goto error2; |
| 388 | |
| 389 | hw->daio_mgr_dsb_dao(mgr->mgr.ctrl_blk, |
| 390 | daio_device_index(type: dao->daio.type, hw)); |
| 391 | hw->daio_mgr_commit_write(hw, mgr->mgr.ctrl_blk); |
| 392 | |
| 393 | conf = (desc->msr & 0x7) | (desc->passthru << 3); |
| 394 | hw->daio_mgr_dao_init(hw, mgr->mgr.ctrl_blk, |
| 395 | daio_device_index(type: dao->daio.type, hw), conf); |
| 396 | hw->daio_mgr_enb_dao(mgr->mgr.ctrl_blk, |
| 397 | daio_device_index(type: dao->daio.type, hw)); |
| 398 | hw->daio_mgr_commit_write(hw, mgr->mgr.ctrl_blk); |
| 399 | |
| 400 | return 0; |
| 401 | |
| 402 | error2: |
| 403 | kfree(objp: dao->imappers); |
| 404 | dao->imappers = NULL; |
| 405 | error1: |
| 406 | daio_rsc_uninit(daio: &dao->daio); |
| 407 | return err; |
| 408 | } |
| 409 | |
| 410 | static int dao_rsc_uninit(struct dao *dao) |
| 411 | { |
| 412 | if (dao->imappers) { |
| 413 | if (dao->imappers[0]) |
| 414 | dao_clear_left_input(dao); |
| 415 | |
| 416 | if (dao->imappers[dao->daio.rscl.msr]) |
| 417 | dao_clear_right_input(dao); |
| 418 | |
| 419 | kfree(objp: dao->imappers); |
| 420 | dao->imappers = NULL; |
| 421 | } |
| 422 | dao->hw->dao_put_ctrl_blk(dao->ctrl_blk); |
| 423 | dao->hw = dao->ctrl_blk = NULL; |
| 424 | daio_rsc_uninit(daio: &dao->daio); |
| 425 | |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | static int dao_rsc_reinit(struct dao *dao, const struct dao_desc *desc) |
| 430 | { |
| 431 | struct daio_mgr *mgr = dao->mgr; |
| 432 | struct daio_desc dsc = {0}; |
| 433 | |
| 434 | dsc.type = dao->daio.type; |
| 435 | dsc.msr = desc->msr; |
| 436 | dsc.passthru = desc->passthru; |
| 437 | dsc.output = dao->daio.output; |
| 438 | dao_rsc_uninit(dao); |
| 439 | return dao_rsc_init(dao, desc: &dsc, mgr); |
| 440 | } |
| 441 | |
| 442 | static int dai_rsc_init(struct dai *dai, |
| 443 | const struct daio_desc *desc, |
| 444 | struct daio_mgr *mgr) |
| 445 | { |
| 446 | int err; |
| 447 | struct hw *hw = mgr->mgr.hw; |
| 448 | unsigned int rsr, msr; |
| 449 | |
| 450 | err = daio_rsc_init(daio: &dai->daio, desc, hw: mgr->mgr.hw); |
| 451 | if (err) |
| 452 | return err; |
| 453 | |
| 454 | dai->ops = &dai_ops; |
| 455 | dai->hw = mgr->mgr.hw; |
| 456 | err = hw->dai_get_ctrl_blk(&dai->ctrl_blk); |
| 457 | if (err) |
| 458 | goto error1; |
| 459 | |
| 460 | for (rsr = 0, msr = desc->msr; msr > 1; msr >>= 1) |
| 461 | rsr++; |
| 462 | |
| 463 | hw->dai_srt_set_rsr(dai->ctrl_blk, rsr); |
| 464 | hw->dai_srt_set_drat(dai->ctrl_blk, 0); |
| 465 | /* default to disabling control of a SRC */ |
| 466 | hw->dai_srt_set_ec(dai->ctrl_blk, 0); |
| 467 | hw->dai_srt_set_et(dai->ctrl_blk, 0); /* default to disabling SRT */ |
| 468 | hw->dai_commit_write(hw, |
| 469 | daio_device_index(type: dai->daio.type, hw: dai->hw), dai->ctrl_blk); |
| 470 | |
| 471 | return 0; |
| 472 | |
| 473 | error1: |
| 474 | daio_rsc_uninit(daio: &dai->daio); |
| 475 | return err; |
| 476 | } |
| 477 | |
| 478 | static int dai_rsc_uninit(struct dai *dai) |
| 479 | { |
| 480 | dai->hw->dai_put_ctrl_blk(dai->ctrl_blk); |
| 481 | dai->hw = dai->ctrl_blk = NULL; |
| 482 | daio_rsc_uninit(daio: &dai->daio); |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | static int daio_mgr_get_rsc(struct rsc_mgr *mgr, enum DAIOTYP type) |
| 487 | { |
| 488 | if (((struct daio_usage *)mgr->rscs)->data & (0x1 << type)) |
| 489 | return -ENOENT; |
| 490 | |
| 491 | ((struct daio_usage *)mgr->rscs)->data |= (0x1 << type); |
| 492 | |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | static int daio_mgr_put_rsc(struct rsc_mgr *mgr, enum DAIOTYP type) |
| 497 | { |
| 498 | ((struct daio_usage *)mgr->rscs)->data &= ~(0x1 << type); |
| 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | static int get_daio_rsc(struct daio_mgr *mgr, |
| 504 | const struct daio_desc *desc, |
| 505 | struct daio **rdaio) |
| 506 | { |
| 507 | int err; |
| 508 | |
| 509 | *rdaio = NULL; |
| 510 | |
| 511 | /* Check whether there are sufficient daio resources to meet request. */ |
| 512 | scoped_guard(spinlock_irqsave, &mgr->mgr_lock) { |
| 513 | err = daio_mgr_get_rsc(mgr: &mgr->mgr, type: desc->type); |
| 514 | } |
| 515 | if (err) { |
| 516 | dev_err(mgr->card->dev, |
| 517 | "Can't meet DAIO resource request!\n" ); |
| 518 | return err; |
| 519 | } |
| 520 | |
| 521 | err = -ENOMEM; |
| 522 | /* Allocate mem for daio resource */ |
| 523 | if (desc->output) { |
| 524 | struct dao *dao = kzalloc(sizeof(*dao), GFP_KERNEL); |
| 525 | if (!dao) |
| 526 | goto error; |
| 527 | |
| 528 | err = dao_rsc_init(dao, desc, mgr); |
| 529 | if (err) { |
| 530 | kfree(objp: dao); |
| 531 | goto error; |
| 532 | } |
| 533 | |
| 534 | *rdaio = &dao->daio; |
| 535 | } else { |
| 536 | struct dai *dai = kzalloc(sizeof(*dai), GFP_KERNEL); |
| 537 | if (!dai) |
| 538 | goto error; |
| 539 | |
| 540 | err = dai_rsc_init(dai, desc, mgr); |
| 541 | if (err) { |
| 542 | kfree(objp: dai); |
| 543 | goto error; |
| 544 | } |
| 545 | |
| 546 | *rdaio = &dai->daio; |
| 547 | } |
| 548 | |
| 549 | mgr->daio_enable(mgr, *rdaio); |
| 550 | mgr->commit_write(mgr); |
| 551 | |
| 552 | return 0; |
| 553 | |
| 554 | error: |
| 555 | scoped_guard(spinlock_irqsave, &mgr->mgr_lock) { |
| 556 | daio_mgr_put_rsc(mgr: &mgr->mgr, type: desc->type); |
| 557 | } |
| 558 | return err; |
| 559 | } |
| 560 | |
| 561 | static int put_daio_rsc(struct daio_mgr *mgr, struct daio *daio) |
| 562 | { |
| 563 | mgr->daio_disable(mgr, daio); |
| 564 | mgr->commit_write(mgr); |
| 565 | |
| 566 | scoped_guard(spinlock_irqsave, &mgr->mgr_lock) { |
| 567 | daio_mgr_put_rsc(mgr: &mgr->mgr, type: daio->type); |
| 568 | } |
| 569 | |
| 570 | if (daio->output) { |
| 571 | dao_rsc_uninit(container_of(daio, struct dao, daio)); |
| 572 | kfree(container_of(daio, struct dao, daio)); |
| 573 | } else { |
| 574 | dai_rsc_uninit(container_of(daio, struct dai, daio)); |
| 575 | kfree(container_of(daio, struct dai, daio)); |
| 576 | } |
| 577 | |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | static int daio_mgr_enb_daio(struct daio_mgr *mgr, struct daio *daio) |
| 582 | { |
| 583 | struct hw *hw = mgr->mgr.hw; |
| 584 | |
| 585 | if (daio->output) { |
| 586 | hw->daio_mgr_enb_dao(mgr->mgr.ctrl_blk, |
| 587 | daio_device_index(type: daio->type, hw)); |
| 588 | } else { |
| 589 | hw->daio_mgr_enb_dai(mgr->mgr.ctrl_blk, |
| 590 | daio_device_index(type: daio->type, hw)); |
| 591 | } |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | static int daio_mgr_dsb_daio(struct daio_mgr *mgr, struct daio *daio) |
| 596 | { |
| 597 | struct hw *hw = mgr->mgr.hw; |
| 598 | |
| 599 | if (daio->output) { |
| 600 | hw->daio_mgr_dsb_dao(mgr->mgr.ctrl_blk, |
| 601 | daio_device_index(type: daio->type, hw)); |
| 602 | } else { |
| 603 | hw->daio_mgr_dsb_dai(mgr->mgr.ctrl_blk, |
| 604 | daio_device_index(type: daio->type, hw)); |
| 605 | } |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | static int daio_map_op(void *data, struct imapper *entry) |
| 610 | { |
| 611 | struct rsc_mgr *mgr = &((struct daio_mgr *)data)->mgr; |
| 612 | struct hw *hw = mgr->hw; |
| 613 | |
| 614 | hw->daio_mgr_set_imaparc(mgr->ctrl_blk, entry->slot); |
| 615 | hw->daio_mgr_set_imapnxt(mgr->ctrl_blk, entry->next); |
| 616 | hw->daio_mgr_set_imapaddr(mgr->ctrl_blk, entry->addr); |
| 617 | hw->daio_mgr_commit_write(mgr->hw, mgr->ctrl_blk); |
| 618 | |
| 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | static int daio_imap_add(struct daio_mgr *mgr, struct imapper *entry) |
| 623 | { |
| 624 | guard(spinlock_irqsave)(l: &mgr->imap_lock); |
| 625 | if (!entry->addr && mgr->init_imap_added) { |
| 626 | input_mapper_delete(mappers: &mgr->imappers, entry: mgr->init_imap, |
| 627 | map_op: daio_map_op, data: mgr); |
| 628 | mgr->init_imap_added = 0; |
| 629 | } |
| 630 | return input_mapper_add(mappers: &mgr->imappers, entry, map_op: daio_map_op, data: mgr); |
| 631 | } |
| 632 | |
| 633 | static int daio_imap_delete(struct daio_mgr *mgr, struct imapper *entry) |
| 634 | { |
| 635 | int err; |
| 636 | |
| 637 | guard(spinlock_irqsave)(l: &mgr->imap_lock); |
| 638 | err = input_mapper_delete(mappers: &mgr->imappers, entry, map_op: daio_map_op, data: mgr); |
| 639 | if (list_empty(head: &mgr->imappers)) { |
| 640 | input_mapper_add(mappers: &mgr->imappers, entry: mgr->init_imap, |
| 641 | map_op: daio_map_op, data: mgr); |
| 642 | mgr->init_imap_added = 1; |
| 643 | } |
| 644 | |
| 645 | return err; |
| 646 | } |
| 647 | |
| 648 | static int daio_mgr_commit_write(struct daio_mgr *mgr) |
| 649 | { |
| 650 | struct hw *hw = mgr->mgr.hw; |
| 651 | |
| 652 | hw->daio_mgr_commit_write(hw, mgr->mgr.ctrl_blk); |
| 653 | return 0; |
| 654 | } |
| 655 | |
| 656 | int daio_mgr_create(struct hw *hw, void **rdaio_mgr) |
| 657 | { |
| 658 | int err, i; |
| 659 | struct daio_mgr *daio_mgr; |
| 660 | struct imapper *entry; |
| 661 | |
| 662 | *rdaio_mgr = NULL; |
| 663 | daio_mgr = kzalloc(sizeof(*daio_mgr), GFP_KERNEL); |
| 664 | if (!daio_mgr) |
| 665 | return -ENOMEM; |
| 666 | |
| 667 | err = rsc_mgr_init(mgr: &daio_mgr->mgr, type: DAIO, amount: NUM_DAIOTYP, hw); |
| 668 | if (err) |
| 669 | goto error1; |
| 670 | |
| 671 | spin_lock_init(&daio_mgr->mgr_lock); |
| 672 | spin_lock_init(&daio_mgr->imap_lock); |
| 673 | INIT_LIST_HEAD(list: &daio_mgr->imappers); |
| 674 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
| 675 | if (!entry) { |
| 676 | err = -ENOMEM; |
| 677 | goto error2; |
| 678 | } |
| 679 | entry->slot = entry->addr = entry->next = entry->user = 0; |
| 680 | list_add(new: &entry->list, head: &daio_mgr->imappers); |
| 681 | daio_mgr->init_imap = entry; |
| 682 | daio_mgr->init_imap_added = 1; |
| 683 | |
| 684 | daio_mgr->get_daio = get_daio_rsc; |
| 685 | daio_mgr->put_daio = put_daio_rsc; |
| 686 | daio_mgr->daio_enable = daio_mgr_enb_daio; |
| 687 | daio_mgr->daio_disable = daio_mgr_dsb_daio; |
| 688 | daio_mgr->imap_add = daio_imap_add; |
| 689 | daio_mgr->imap_delete = daio_imap_delete; |
| 690 | daio_mgr->commit_write = daio_mgr_commit_write; |
| 691 | daio_mgr->card = hw->card; |
| 692 | |
| 693 | for (i = 0; i < 8; i++) { |
| 694 | hw->daio_mgr_dsb_dao(daio_mgr->mgr.ctrl_blk, i); |
| 695 | hw->daio_mgr_dsb_dai(daio_mgr->mgr.ctrl_blk, i); |
| 696 | } |
| 697 | hw->daio_mgr_commit_write(hw, daio_mgr->mgr.ctrl_blk); |
| 698 | |
| 699 | *rdaio_mgr = daio_mgr; |
| 700 | |
| 701 | return 0; |
| 702 | |
| 703 | error2: |
| 704 | rsc_mgr_uninit(mgr: &daio_mgr->mgr); |
| 705 | error1: |
| 706 | kfree(objp: daio_mgr); |
| 707 | return err; |
| 708 | } |
| 709 | |
| 710 | int daio_mgr_destroy(void *ptr) |
| 711 | { |
| 712 | struct daio_mgr *daio_mgr = ptr; |
| 713 | |
| 714 | /* free daio input mapper list */ |
| 715 | scoped_guard(spinlock_irqsave, &daio_mgr->imap_lock) { |
| 716 | free_input_mapper_list(mappers: &daio_mgr->imappers); |
| 717 | } |
| 718 | |
| 719 | rsc_mgr_uninit(mgr: &daio_mgr->mgr); |
| 720 | kfree(objp: daio_mgr); |
| 721 | |
| 722 | return 0; |
| 723 | } |
| 724 | |
| 725 | |