| 1 | // SPDX-License-Identifier: GPL-2.0-only | 
|---|
| 2 | /* | 
|---|
| 3 | * Regmap support for HD-audio verbs | 
|---|
| 4 | * | 
|---|
| 5 | * A virtual register is translated to one or more hda verbs for write, | 
|---|
| 6 | * vice versa for read. | 
|---|
| 7 | * | 
|---|
| 8 | * A few limitations: | 
|---|
| 9 | * - Provided for not all verbs but only subset standard non-volatile verbs. | 
|---|
| 10 | * - For reading, only AC_VERB_GET_* variants can be used. | 
|---|
| 11 | * - For writing, mapped to the *corresponding* AC_VERB_SET_* variants, | 
|---|
| 12 | *   so can't handle asymmetric verbs for read and write | 
|---|
| 13 | */ | 
|---|
| 14 |  | 
|---|
| 15 | #include <linux/slab.h> | 
|---|
| 16 | #include <linux/device.h> | 
|---|
| 17 | #include <linux/regmap.h> | 
|---|
| 18 | #include <linux/export.h> | 
|---|
| 19 | #include <linux/pm.h> | 
|---|
| 20 | #include <sound/core.h> | 
|---|
| 21 | #include <sound/hdaudio.h> | 
|---|
| 22 | #include <sound/hda_regmap.h> | 
|---|
| 23 | #include "local.h" | 
|---|
| 24 |  | 
|---|
| 25 | static int codec_pm_lock(struct hdac_device *codec) | 
|---|
| 26 | { | 
|---|
| 27 | return snd_hdac_keep_power_up(codec); | 
|---|
| 28 | } | 
|---|
| 29 |  | 
|---|
| 30 | static void codec_pm_unlock(struct hdac_device *codec, int lock) | 
|---|
| 31 | { | 
|---|
| 32 | if (lock == 1) | 
|---|
| 33 | snd_hdac_power_down_pm(codec); | 
|---|
| 34 | } | 
|---|
| 35 |  | 
|---|
| 36 | #define get_verb(reg)	(((reg) >> 8) & 0xfff) | 
|---|
| 37 |  | 
|---|
| 38 | static bool hda_volatile_reg(struct device *dev, unsigned int reg) | 
|---|
| 39 | { | 
|---|
| 40 | struct hdac_device *codec = dev_to_hdac_dev(dev); | 
|---|
| 41 | unsigned int verb = get_verb(reg); | 
|---|
| 42 |  | 
|---|
| 43 | switch (verb) { | 
|---|
| 44 | case AC_VERB_GET_PROC_COEF: | 
|---|
| 45 | return !codec->cache_coef; | 
|---|
| 46 | case AC_VERB_GET_COEF_INDEX: | 
|---|
| 47 | case AC_VERB_GET_PROC_STATE: | 
|---|
| 48 | case AC_VERB_GET_POWER_STATE: | 
|---|
| 49 | case AC_VERB_GET_PIN_SENSE: | 
|---|
| 50 | case AC_VERB_GET_HDMI_DIP_SIZE: | 
|---|
| 51 | case AC_VERB_GET_HDMI_ELDD: | 
|---|
| 52 | case AC_VERB_GET_HDMI_DIP_INDEX: | 
|---|
| 53 | case AC_VERB_GET_HDMI_DIP_DATA: | 
|---|
| 54 | case AC_VERB_GET_HDMI_DIP_XMIT: | 
|---|
| 55 | case AC_VERB_GET_HDMI_CP_CTRL: | 
|---|
| 56 | case AC_VERB_GET_HDMI_CHAN_SLOT: | 
|---|
| 57 | case AC_VERB_GET_DEVICE_SEL: | 
|---|
| 58 | case AC_VERB_GET_DEVICE_LIST:	/* read-only volatile */ | 
|---|
| 59 | return true; | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | return false; | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | static bool hda_writeable_reg(struct device *dev, unsigned int reg) | 
|---|
| 66 | { | 
|---|
| 67 | struct hdac_device *codec = dev_to_hdac_dev(dev); | 
|---|
| 68 | unsigned int verb = get_verb(reg); | 
|---|
| 69 | const unsigned int *v; | 
|---|
| 70 | int i; | 
|---|
| 71 |  | 
|---|
| 72 | snd_array_for_each(&codec->vendor_verbs, i, v) { | 
|---|
| 73 | if (verb == *v) | 
|---|
| 74 | return true; | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | if (codec->caps_overwriting) | 
|---|
| 78 | return true; | 
|---|
| 79 |  | 
|---|
| 80 | switch (verb & 0xf00) { | 
|---|
| 81 | case AC_VERB_GET_STREAM_FORMAT: | 
|---|
| 82 | case AC_VERB_GET_AMP_GAIN_MUTE: | 
|---|
| 83 | return true; | 
|---|
| 84 | case AC_VERB_GET_PROC_COEF: | 
|---|
| 85 | return codec->cache_coef; | 
|---|
| 86 | case 0xf00: | 
|---|
| 87 | break; | 
|---|
| 88 | default: | 
|---|
| 89 | return false; | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | switch (verb) { | 
|---|
| 93 | case AC_VERB_GET_CONNECT_SEL: | 
|---|
| 94 | case AC_VERB_GET_SDI_SELECT: | 
|---|
| 95 | case AC_VERB_GET_PIN_WIDGET_CONTROL: | 
|---|
| 96 | case AC_VERB_GET_UNSOLICITED_RESPONSE: /* only as SET_UNSOLICITED_ENABLE */ | 
|---|
| 97 | case AC_VERB_GET_BEEP_CONTROL: | 
|---|
| 98 | case AC_VERB_GET_EAPD_BTLENABLE: | 
|---|
| 99 | case AC_VERB_GET_DIGI_CONVERT_1: | 
|---|
| 100 | case AC_VERB_GET_DIGI_CONVERT_2: /* only for beep control */ | 
|---|
| 101 | case AC_VERB_GET_VOLUME_KNOB_CONTROL: | 
|---|
| 102 | case AC_VERB_GET_GPIO_MASK: | 
|---|
| 103 | case AC_VERB_GET_GPIO_DIRECTION: | 
|---|
| 104 | case AC_VERB_GET_GPIO_DATA: /* not for volatile read */ | 
|---|
| 105 | case AC_VERB_GET_GPIO_WAKE_MASK: | 
|---|
| 106 | case AC_VERB_GET_GPIO_UNSOLICITED_RSP_MASK: | 
|---|
| 107 | case AC_VERB_GET_GPIO_STICKY_MASK: | 
|---|
| 108 | return true; | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | return false; | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | static bool hda_readable_reg(struct device *dev, unsigned int reg) | 
|---|
| 115 | { | 
|---|
| 116 | struct hdac_device *codec = dev_to_hdac_dev(dev); | 
|---|
| 117 | unsigned int verb = get_verb(reg); | 
|---|
| 118 |  | 
|---|
| 119 | if (codec->caps_overwriting) | 
|---|
| 120 | return true; | 
|---|
| 121 |  | 
|---|
| 122 | switch (verb) { | 
|---|
| 123 | case AC_VERB_PARAMETERS: | 
|---|
| 124 | case AC_VERB_GET_CONNECT_LIST: | 
|---|
| 125 | case AC_VERB_GET_SUBSYSTEM_ID: | 
|---|
| 126 | return true; | 
|---|
| 127 | /* below are basically writable, but disabled for reducing unnecessary | 
|---|
| 128 | * writes at sync | 
|---|
| 129 | */ | 
|---|
| 130 | case AC_VERB_GET_CONFIG_DEFAULT: /* usually just read */ | 
|---|
| 131 | case AC_VERB_GET_CONV: /* managed in PCM code */ | 
|---|
| 132 | case AC_VERB_GET_CVT_CHAN_COUNT: /* managed in HDMI CA code */ | 
|---|
| 133 | return true; | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | return hda_writeable_reg(dev, reg); | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | /* | 
|---|
| 140 | * Stereo amp pseudo register: | 
|---|
| 141 | * for making easier to handle the stereo volume control, we provide a | 
|---|
| 142 | * fake register to deal both left and right channels by a single | 
|---|
| 143 | * (pseudo) register access.  A verb consisting of SET_AMP_GAIN with | 
|---|
| 144 | * *both* SET_LEFT and SET_RIGHT bits takes a 16bit value, the lower 8bit | 
|---|
| 145 | * for the left and the upper 8bit for the right channel. | 
|---|
| 146 | */ | 
|---|
| 147 | static bool is_stereo_amp_verb(unsigned int reg) | 
|---|
| 148 | { | 
|---|
| 149 | if (((reg >> 8) & 0x700) != AC_VERB_SET_AMP_GAIN_MUTE) | 
|---|
| 150 | return false; | 
|---|
| 151 | return (reg & (AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT)) == | 
|---|
| 152 | (AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT); | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | /* read a pseudo stereo amp register (16bit left+right) */ | 
|---|
| 156 | static int hda_reg_read_stereo_amp(struct hdac_device *codec, | 
|---|
| 157 | unsigned int reg, unsigned int *val) | 
|---|
| 158 | { | 
|---|
| 159 | unsigned int left, right; | 
|---|
| 160 | int err; | 
|---|
| 161 |  | 
|---|
| 162 | reg &= ~(AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT); | 
|---|
| 163 | err = snd_hdac_exec_verb(codec, cmd: reg | AC_AMP_GET_LEFT, flags: 0, res: &left); | 
|---|
| 164 | if (err < 0) | 
|---|
| 165 | return err; | 
|---|
| 166 | err = snd_hdac_exec_verb(codec, cmd: reg | AC_AMP_GET_RIGHT, flags: 0, res: &right); | 
|---|
| 167 | if (err < 0) | 
|---|
| 168 | return err; | 
|---|
| 169 | *val = left | (right << 8); | 
|---|
| 170 | return 0; | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | /* write a pseudo stereo amp register (16bit left+right) */ | 
|---|
| 174 | static int hda_reg_write_stereo_amp(struct hdac_device *codec, | 
|---|
| 175 | unsigned int reg, unsigned int val) | 
|---|
| 176 | { | 
|---|
| 177 | int err; | 
|---|
| 178 | unsigned int verb, left, right; | 
|---|
| 179 |  | 
|---|
| 180 | verb = AC_VERB_SET_AMP_GAIN_MUTE << 8; | 
|---|
| 181 | if (reg & AC_AMP_GET_OUTPUT) | 
|---|
| 182 | verb |= AC_AMP_SET_OUTPUT; | 
|---|
| 183 | else | 
|---|
| 184 | verb |= AC_AMP_SET_INPUT | ((reg & 0xf) << 8); | 
|---|
| 185 | reg = (reg & ~0xfffff) | verb; | 
|---|
| 186 |  | 
|---|
| 187 | left = val & 0xff; | 
|---|
| 188 | right = (val >> 8) & 0xff; | 
|---|
| 189 | if (left == right) { | 
|---|
| 190 | reg |= AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT; | 
|---|
| 191 | return snd_hdac_exec_verb(codec, cmd: reg | left, flags: 0, NULL); | 
|---|
| 192 | } | 
|---|
| 193 |  | 
|---|
| 194 | err = snd_hdac_exec_verb(codec, cmd: reg | AC_AMP_SET_LEFT | left, flags: 0, NULL); | 
|---|
| 195 | if (err < 0) | 
|---|
| 196 | return err; | 
|---|
| 197 | err = snd_hdac_exec_verb(codec, cmd: reg | AC_AMP_SET_RIGHT | right, flags: 0, NULL); | 
|---|
| 198 | if (err < 0) | 
|---|
| 199 | return err; | 
|---|
| 200 | return 0; | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 | /* read a pseudo coef register (16bit) */ | 
|---|
| 204 | static int hda_reg_read_coef(struct hdac_device *codec, unsigned int reg, | 
|---|
| 205 | unsigned int *val) | 
|---|
| 206 | { | 
|---|
| 207 | unsigned int verb; | 
|---|
| 208 | int err; | 
|---|
| 209 |  | 
|---|
| 210 | if (!codec->cache_coef) | 
|---|
| 211 | return -EINVAL; | 
|---|
| 212 | /* LSB 8bit = coef index */ | 
|---|
| 213 | verb = (reg & ~0xfff00) | (AC_VERB_SET_COEF_INDEX << 8); | 
|---|
| 214 | err = snd_hdac_exec_verb(codec, cmd: verb, flags: 0, NULL); | 
|---|
| 215 | if (err < 0) | 
|---|
| 216 | return err; | 
|---|
| 217 | verb = (reg & ~0xfffff) | (AC_VERB_GET_COEF_INDEX << 8); | 
|---|
| 218 | return snd_hdac_exec_verb(codec, cmd: verb, flags: 0, res: val); | 
|---|
| 219 | } | 
|---|
| 220 |  | 
|---|
| 221 | /* write a pseudo coef register (16bit) */ | 
|---|
| 222 | static int hda_reg_write_coef(struct hdac_device *codec, unsigned int reg, | 
|---|
| 223 | unsigned int val) | 
|---|
| 224 | { | 
|---|
| 225 | unsigned int verb; | 
|---|
| 226 | int err; | 
|---|
| 227 |  | 
|---|
| 228 | if (!codec->cache_coef) | 
|---|
| 229 | return -EINVAL; | 
|---|
| 230 | /* LSB 8bit = coef index */ | 
|---|
| 231 | verb = (reg & ~0xfff00) | (AC_VERB_SET_COEF_INDEX << 8); | 
|---|
| 232 | err = snd_hdac_exec_verb(codec, cmd: verb, flags: 0, NULL); | 
|---|
| 233 | if (err < 0) | 
|---|
| 234 | return err; | 
|---|
| 235 | verb = (reg & ~0xfffff) | (AC_VERB_GET_COEF_INDEX << 8) | | 
|---|
| 236 | (val & 0xffff); | 
|---|
| 237 | return snd_hdac_exec_verb(codec, cmd: verb, flags: 0, NULL); | 
|---|
| 238 | } | 
|---|
| 239 |  | 
|---|
| 240 | static int hda_reg_read(void *context, unsigned int reg, unsigned int *val) | 
|---|
| 241 | { | 
|---|
| 242 | struct hdac_device *codec = context; | 
|---|
| 243 | int verb = get_verb(reg); | 
|---|
| 244 | int err; | 
|---|
| 245 | int pm_lock = 0; | 
|---|
| 246 |  | 
|---|
| 247 | if (verb != AC_VERB_GET_POWER_STATE) { | 
|---|
| 248 | pm_lock = codec_pm_lock(codec); | 
|---|
| 249 | if (pm_lock < 0) | 
|---|
| 250 | return -EAGAIN; | 
|---|
| 251 | } | 
|---|
| 252 | reg |= (codec->addr << 28); | 
|---|
| 253 | if (is_stereo_amp_verb(reg)) { | 
|---|
| 254 | err = hda_reg_read_stereo_amp(codec, reg, val); | 
|---|
| 255 | goto out; | 
|---|
| 256 | } | 
|---|
| 257 | if (verb == AC_VERB_GET_PROC_COEF) { | 
|---|
| 258 | err = hda_reg_read_coef(codec, reg, val); | 
|---|
| 259 | goto out; | 
|---|
| 260 | } | 
|---|
| 261 | if ((verb & 0x700) == AC_VERB_SET_AMP_GAIN_MUTE) | 
|---|
| 262 | reg &= ~AC_AMP_FAKE_MUTE; | 
|---|
| 263 |  | 
|---|
| 264 | err = snd_hdac_exec_verb(codec, cmd: reg, flags: 0, res: val); | 
|---|
| 265 | if (err < 0) | 
|---|
| 266 | goto out; | 
|---|
| 267 | /* special handling for asymmetric reads */ | 
|---|
| 268 | if (verb == AC_VERB_GET_POWER_STATE) { | 
|---|
| 269 | if (*val & AC_PWRST_ERROR) | 
|---|
| 270 | *val = -1; | 
|---|
| 271 | else /* take only the actual state */ | 
|---|
| 272 | *val = (*val >> 4) & 0x0f; | 
|---|
| 273 | } | 
|---|
| 274 | out: | 
|---|
| 275 | codec_pm_unlock(codec, lock: pm_lock); | 
|---|
| 276 | return err; | 
|---|
| 277 | } | 
|---|
| 278 |  | 
|---|
| 279 | static int hda_reg_write(void *context, unsigned int reg, unsigned int val) | 
|---|
| 280 | { | 
|---|
| 281 | struct hdac_device *codec = context; | 
|---|
| 282 | unsigned int verb; | 
|---|
| 283 | int i, bytes, err; | 
|---|
| 284 | int pm_lock = 0; | 
|---|
| 285 |  | 
|---|
| 286 | if (codec->caps_overwriting) | 
|---|
| 287 | return 0; | 
|---|
| 288 |  | 
|---|
| 289 | reg &= ~0x00080000U; /* drop GET bit */ | 
|---|
| 290 | reg |= (codec->addr << 28); | 
|---|
| 291 | verb = get_verb(reg); | 
|---|
| 292 |  | 
|---|
| 293 | if (verb != AC_VERB_SET_POWER_STATE) { | 
|---|
| 294 | pm_lock = codec_pm_lock(codec); | 
|---|
| 295 | if (pm_lock < 0) | 
|---|
| 296 | return codec->lazy_cache ? 0 : -EAGAIN; | 
|---|
| 297 | } | 
|---|
| 298 |  | 
|---|
| 299 | if (is_stereo_amp_verb(reg)) { | 
|---|
| 300 | err = hda_reg_write_stereo_amp(codec, reg, val); | 
|---|
| 301 | goto out; | 
|---|
| 302 | } | 
|---|
| 303 |  | 
|---|
| 304 | if (verb == AC_VERB_SET_PROC_COEF) { | 
|---|
| 305 | err = hda_reg_write_coef(codec, reg, val); | 
|---|
| 306 | goto out; | 
|---|
| 307 | } | 
|---|
| 308 |  | 
|---|
| 309 | switch (verb & 0xf00) { | 
|---|
| 310 | case AC_VERB_SET_AMP_GAIN_MUTE: | 
|---|
| 311 | if ((reg & AC_AMP_FAKE_MUTE) && (val & AC_AMP_MUTE)) | 
|---|
| 312 | val = 0; | 
|---|
| 313 | verb = AC_VERB_SET_AMP_GAIN_MUTE; | 
|---|
| 314 | if (reg & AC_AMP_GET_LEFT) | 
|---|
| 315 | verb |= AC_AMP_SET_LEFT >> 8; | 
|---|
| 316 | else | 
|---|
| 317 | verb |= AC_AMP_SET_RIGHT >> 8; | 
|---|
| 318 | if (reg & AC_AMP_GET_OUTPUT) { | 
|---|
| 319 | verb |= AC_AMP_SET_OUTPUT >> 8; | 
|---|
| 320 | } else { | 
|---|
| 321 | verb |= AC_AMP_SET_INPUT >> 8; | 
|---|
| 322 | verb |= reg & 0xf; | 
|---|
| 323 | } | 
|---|
| 324 | break; | 
|---|
| 325 | } | 
|---|
| 326 |  | 
|---|
| 327 | switch (verb) { | 
|---|
| 328 | case AC_VERB_SET_DIGI_CONVERT_1: | 
|---|
| 329 | bytes = 2; | 
|---|
| 330 | break; | 
|---|
| 331 | case AC_VERB_SET_CONFIG_DEFAULT_BYTES_0: | 
|---|
| 332 | bytes = 4; | 
|---|
| 333 | break; | 
|---|
| 334 | default: | 
|---|
| 335 | bytes = 1; | 
|---|
| 336 | break; | 
|---|
| 337 | } | 
|---|
| 338 |  | 
|---|
| 339 | for (i = 0; i < bytes; i++) { | 
|---|
| 340 | reg &= ~0xfffff; | 
|---|
| 341 | reg |= (verb + i) << 8 | ((val >> (8 * i)) & 0xff); | 
|---|
| 342 | err = snd_hdac_exec_verb(codec, cmd: reg, flags: 0, NULL); | 
|---|
| 343 | if (err < 0) | 
|---|
| 344 | goto out; | 
|---|
| 345 | } | 
|---|
| 346 |  | 
|---|
| 347 | out: | 
|---|
| 348 | codec_pm_unlock(codec, lock: pm_lock); | 
|---|
| 349 | return err; | 
|---|
| 350 | } | 
|---|
| 351 |  | 
|---|
| 352 | static const struct regmap_config hda_regmap_cfg = { | 
|---|
| 353 | .name = "hdaudio", | 
|---|
| 354 | .reg_bits = 32, | 
|---|
| 355 | .val_bits = 32, | 
|---|
| 356 | .max_register = 0xfffffff, | 
|---|
| 357 | .writeable_reg = hda_writeable_reg, | 
|---|
| 358 | .readable_reg = hda_readable_reg, | 
|---|
| 359 | .volatile_reg = hda_volatile_reg, | 
|---|
| 360 | .cache_type = REGCACHE_MAPLE, | 
|---|
| 361 | .reg_read = hda_reg_read, | 
|---|
| 362 | .reg_write = hda_reg_write, | 
|---|
| 363 | .use_single_read = true, | 
|---|
| 364 | .use_single_write = true, | 
|---|
| 365 | .disable_locking = true, | 
|---|
| 366 | }; | 
|---|
| 367 |  | 
|---|
| 368 | /** | 
|---|
| 369 | * snd_hdac_regmap_init - Initialize regmap for HDA register accesses | 
|---|
| 370 | * @codec: the codec object | 
|---|
| 371 | * | 
|---|
| 372 | * Returns zero for success or a negative error code. | 
|---|
| 373 | */ | 
|---|
| 374 | int snd_hdac_regmap_init(struct hdac_device *codec) | 
|---|
| 375 | { | 
|---|
| 376 | struct regmap *regmap; | 
|---|
| 377 |  | 
|---|
| 378 | regmap = regmap_init(&codec->dev, NULL, codec, &hda_regmap_cfg); | 
|---|
| 379 | if (IS_ERR(ptr: regmap)) | 
|---|
| 380 | return PTR_ERR(ptr: regmap); | 
|---|
| 381 | codec->regmap = regmap; | 
|---|
| 382 | snd_array_init(array: &codec->vendor_verbs, size: sizeof(unsigned int), align: 8); | 
|---|
| 383 | return 0; | 
|---|
| 384 | } | 
|---|
| 385 | EXPORT_SYMBOL_GPL(snd_hdac_regmap_init); | 
|---|
| 386 |  | 
|---|
| 387 | /** | 
|---|
| 388 | * snd_hdac_regmap_exit - Release the regmap from HDA codec | 
|---|
| 389 | * @codec: the codec object | 
|---|
| 390 | */ | 
|---|
| 391 | void snd_hdac_regmap_exit(struct hdac_device *codec) | 
|---|
| 392 | { | 
|---|
| 393 | if (codec->regmap) { | 
|---|
| 394 | regmap_exit(map: codec->regmap); | 
|---|
| 395 | codec->regmap = NULL; | 
|---|
| 396 | snd_array_free(array: &codec->vendor_verbs); | 
|---|
| 397 | } | 
|---|
| 398 | } | 
|---|
| 399 | EXPORT_SYMBOL_GPL(snd_hdac_regmap_exit); | 
|---|
| 400 |  | 
|---|
| 401 | /** | 
|---|
| 402 | * snd_hdac_regmap_add_vendor_verb - add a vendor-specific verb to regmap | 
|---|
| 403 | * @codec: the codec object | 
|---|
| 404 | * @verb: verb to allow accessing via regmap | 
|---|
| 405 | * | 
|---|
| 406 | * Returns zero for success or a negative error code. | 
|---|
| 407 | */ | 
|---|
| 408 | int snd_hdac_regmap_add_vendor_verb(struct hdac_device *codec, | 
|---|
| 409 | unsigned int verb) | 
|---|
| 410 | { | 
|---|
| 411 | unsigned int *p = snd_array_new(array: &codec->vendor_verbs); | 
|---|
| 412 |  | 
|---|
| 413 | if (!p) | 
|---|
| 414 | return -ENOMEM; | 
|---|
| 415 | *p = verb | 0x800; /* set GET bit */ | 
|---|
| 416 | return 0; | 
|---|
| 417 | } | 
|---|
| 418 | EXPORT_SYMBOL_GPL(snd_hdac_regmap_add_vendor_verb); | 
|---|
| 419 |  | 
|---|
| 420 | /* | 
|---|
| 421 | * helper functions | 
|---|
| 422 | */ | 
|---|
| 423 |  | 
|---|
| 424 | /* write a pseudo-register value (w/o power sequence) */ | 
|---|
| 425 | static int reg_raw_write(struct hdac_device *codec, unsigned int reg, | 
|---|
| 426 | unsigned int val) | 
|---|
| 427 | { | 
|---|
| 428 | guard(mutex)(T: &codec->regmap_lock); | 
|---|
| 429 | if (!codec->regmap) | 
|---|
| 430 | return hda_reg_write(context: codec, reg, val); | 
|---|
| 431 | else | 
|---|
| 432 | return regmap_write(map: codec->regmap, reg, val); | 
|---|
| 433 | } | 
|---|
| 434 |  | 
|---|
| 435 | /* a helper macro to call @func_call; retry with power-up if failed */ | 
|---|
| 436 | #define CALL_RAW_FUNC(codec, func_call)				\ | 
|---|
| 437 | ({							\ | 
|---|
| 438 | int _err = func_call;				\ | 
|---|
| 439 | if (_err == -EAGAIN) {				\ | 
|---|
| 440 | _err = snd_hdac_power_up_pm(codec);	\ | 
|---|
| 441 | if (_err >= 0)				\ | 
|---|
| 442 | _err = func_call;		\ | 
|---|
| 443 | snd_hdac_power_down_pm(codec);		\ | 
|---|
| 444 | }						\ | 
|---|
| 445 | _err;}) | 
|---|
| 446 |  | 
|---|
| 447 | /** | 
|---|
| 448 | * snd_hdac_regmap_write_raw - write a pseudo register with power mgmt | 
|---|
| 449 | * @codec: the codec object | 
|---|
| 450 | * @reg: pseudo register | 
|---|
| 451 | * @val: value to write | 
|---|
| 452 | * | 
|---|
| 453 | * Returns zero if successful or a negative error code. | 
|---|
| 454 | */ | 
|---|
| 455 | int snd_hdac_regmap_write_raw(struct hdac_device *codec, unsigned int reg, | 
|---|
| 456 | unsigned int val) | 
|---|
| 457 | { | 
|---|
| 458 | return CALL_RAW_FUNC(codec, reg_raw_write(codec, reg, val)); | 
|---|
| 459 | } | 
|---|
| 460 | EXPORT_SYMBOL_GPL(snd_hdac_regmap_write_raw); | 
|---|
| 461 |  | 
|---|
| 462 | static int reg_raw_read(struct hdac_device *codec, unsigned int reg, | 
|---|
| 463 | unsigned int *val, bool uncached) | 
|---|
| 464 | { | 
|---|
| 465 | guard(mutex)(T: &codec->regmap_lock); | 
|---|
| 466 | if (uncached || !codec->regmap) | 
|---|
| 467 | return hda_reg_read(context: codec, reg, val); | 
|---|
| 468 | else | 
|---|
| 469 | return regmap_read(map: codec->regmap, reg, val); | 
|---|
| 470 | } | 
|---|
| 471 |  | 
|---|
| 472 | static int __snd_hdac_regmap_read_raw(struct hdac_device *codec, | 
|---|
| 473 | unsigned int reg, unsigned int *val, | 
|---|
| 474 | bool uncached) | 
|---|
| 475 | { | 
|---|
| 476 | return CALL_RAW_FUNC(codec, reg_raw_read(codec, reg, val, uncached)); | 
|---|
| 477 | } | 
|---|
| 478 |  | 
|---|
| 479 | /** | 
|---|
| 480 | * snd_hdac_regmap_read_raw - read a pseudo register with power mgmt | 
|---|
| 481 | * @codec: the codec object | 
|---|
| 482 | * @reg: pseudo register | 
|---|
| 483 | * @val: pointer to store the read value | 
|---|
| 484 | * | 
|---|
| 485 | * Returns zero if successful or a negative error code. | 
|---|
| 486 | */ | 
|---|
| 487 | int snd_hdac_regmap_read_raw(struct hdac_device *codec, unsigned int reg, | 
|---|
| 488 | unsigned int *val) | 
|---|
| 489 | { | 
|---|
| 490 | return __snd_hdac_regmap_read_raw(codec, reg, val, uncached: false); | 
|---|
| 491 | } | 
|---|
| 492 | EXPORT_SYMBOL_GPL(snd_hdac_regmap_read_raw); | 
|---|
| 493 |  | 
|---|
| 494 | /* Works like snd_hdac_regmap_read_raw(), but this doesn't read from the | 
|---|
| 495 | * cache but always via hda verbs. | 
|---|
| 496 | */ | 
|---|
| 497 | int snd_hdac_regmap_read_raw_uncached(struct hdac_device *codec, | 
|---|
| 498 | unsigned int reg, unsigned int *val) | 
|---|
| 499 | { | 
|---|
| 500 | return __snd_hdac_regmap_read_raw(codec, reg, val, uncached: true); | 
|---|
| 501 | } | 
|---|
| 502 |  | 
|---|
| 503 | static int reg_raw_update(struct hdac_device *codec, unsigned int reg, | 
|---|
| 504 | unsigned int mask, unsigned int val) | 
|---|
| 505 | { | 
|---|
| 506 | unsigned int orig; | 
|---|
| 507 | bool change; | 
|---|
| 508 | int err; | 
|---|
| 509 |  | 
|---|
| 510 | guard(mutex)(T: &codec->regmap_lock); | 
|---|
| 511 | if (codec->regmap) { | 
|---|
| 512 | err = regmap_update_bits_check(map: codec->regmap, reg, mask, val, | 
|---|
| 513 | change: &change); | 
|---|
| 514 | if (!err) | 
|---|
| 515 | err = change ? 1 : 0; | 
|---|
| 516 | } else { | 
|---|
| 517 | err = hda_reg_read(context: codec, reg, val: &orig); | 
|---|
| 518 | if (!err) { | 
|---|
| 519 | val &= mask; | 
|---|
| 520 | val |= orig & ~mask; | 
|---|
| 521 | if (val != orig) { | 
|---|
| 522 | err = hda_reg_write(context: codec, reg, val); | 
|---|
| 523 | if (!err) | 
|---|
| 524 | err = 1; | 
|---|
| 525 | } | 
|---|
| 526 | } | 
|---|
| 527 | } | 
|---|
| 528 | return err; | 
|---|
| 529 | } | 
|---|
| 530 |  | 
|---|
| 531 | /** | 
|---|
| 532 | * snd_hdac_regmap_update_raw - update a pseudo register with power mgmt | 
|---|
| 533 | * @codec: the codec object | 
|---|
| 534 | * @reg: pseudo register | 
|---|
| 535 | * @mask: bit mask to update | 
|---|
| 536 | * @val: value to update | 
|---|
| 537 | * | 
|---|
| 538 | * Returns zero if successful or a negative error code. | 
|---|
| 539 | */ | 
|---|
| 540 | int snd_hdac_regmap_update_raw(struct hdac_device *codec, unsigned int reg, | 
|---|
| 541 | unsigned int mask, unsigned int val) | 
|---|
| 542 | { | 
|---|
| 543 | return CALL_RAW_FUNC(codec, reg_raw_update(codec, reg, mask, val)); | 
|---|
| 544 | } | 
|---|
| 545 | EXPORT_SYMBOL_GPL(snd_hdac_regmap_update_raw); | 
|---|
| 546 |  | 
|---|
| 547 | static int reg_raw_update_once(struct hdac_device *codec, unsigned int reg, | 
|---|
| 548 | unsigned int mask, unsigned int val) | 
|---|
| 549 | { | 
|---|
| 550 | if (!codec->regmap) | 
|---|
| 551 | return reg_raw_update(codec, reg, mask, val); | 
|---|
| 552 |  | 
|---|
| 553 | guard(mutex)(T: &codec->regmap_lock); | 
|---|
| 554 | /* Discard any updates to already initialised registers. */ | 
|---|
| 555 | if (!regcache_reg_cached(map: codec->regmap, reg)) | 
|---|
| 556 | return regmap_update_bits(map: codec->regmap, reg, mask, val); | 
|---|
| 557 | return 0; | 
|---|
| 558 | } | 
|---|
| 559 |  | 
|---|
| 560 | /** | 
|---|
| 561 | * snd_hdac_regmap_update_raw_once - initialize the register value only once | 
|---|
| 562 | * @codec: the codec object | 
|---|
| 563 | * @reg: pseudo register | 
|---|
| 564 | * @mask: bit mask to update | 
|---|
| 565 | * @val: value to update | 
|---|
| 566 | * | 
|---|
| 567 | * Performs the update of the register bits only once when the register | 
|---|
| 568 | * hasn't been initialized yet.  Used in HD-audio legacy driver. | 
|---|
| 569 | * Returns zero if successful or a negative error code | 
|---|
| 570 | */ | 
|---|
| 571 | int snd_hdac_regmap_update_raw_once(struct hdac_device *codec, unsigned int reg, | 
|---|
| 572 | unsigned int mask, unsigned int val) | 
|---|
| 573 | { | 
|---|
| 574 | return CALL_RAW_FUNC(codec, reg_raw_update_once(codec, reg, mask, val)); | 
|---|
| 575 | } | 
|---|
| 576 | EXPORT_SYMBOL_GPL(snd_hdac_regmap_update_raw_once); | 
|---|
| 577 |  | 
|---|
| 578 | /** | 
|---|
| 579 | * snd_hdac_regmap_sync - sync out the cached values for PM resume | 
|---|
| 580 | * @codec: the codec object | 
|---|
| 581 | */ | 
|---|
| 582 | void snd_hdac_regmap_sync(struct hdac_device *codec) | 
|---|
| 583 | { | 
|---|
| 584 | guard(mutex)(T: &codec->regmap_lock); | 
|---|
| 585 | if (codec->regmap) | 
|---|
| 586 | regcache_sync(map: codec->regmap); | 
|---|
| 587 | } | 
|---|
| 588 | EXPORT_SYMBOL_GPL(snd_hdac_regmap_sync); | 
|---|
| 589 |  | 
|---|