Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 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 <linux/pm_runtime.h> |
| 21 | #include <sound/core.h> |
| 22 | #include <sound/hdaudio.h> |
| 23 | #include <sound/hda_regmap.h> |
| 24 | |
Takashi Iwai | fc4f000 | 2016-03-04 11:34:18 +0100 | [diff] [blame] | 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 | } |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 35 | |
| 36 | #define get_verb(reg) (((reg) >> 8) & 0xfff) |
| 37 | |
| 38 | static bool hda_volatile_reg(struct device *dev, unsigned int reg) |
| 39 | { |
Takashi Iwai | 40ba66a | 2015-03-13 15:56:25 +0100 | [diff] [blame] | 40 | struct hdac_device *codec = dev_to_hdac_dev(dev); |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 41 | unsigned int verb = get_verb(reg); |
| 42 | |
| 43 | switch (verb) { |
| 44 | case AC_VERB_GET_PROC_COEF: |
Takashi Iwai | 40ba66a | 2015-03-13 15:56:25 +0100 | [diff] [blame] | 45 | return !codec->cache_coef; |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 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 | { |
Takashi Iwai | faa75f8 | 2015-02-26 08:54:56 +0100 | [diff] [blame] | 67 | struct hdac_device *codec = dev_to_hdac_dev(dev); |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 68 | unsigned int verb = get_verb(reg); |
Takashi Iwai | a9c2dfc | 2018-04-23 17:24:56 +0200 | [diff] [blame] | 69 | const unsigned int *v; |
Takashi Iwai | 5e56bce | 2015-02-26 12:29:03 +0100 | [diff] [blame] | 70 | int i; |
| 71 | |
Takashi Iwai | a9c2dfc | 2018-04-23 17:24:56 +0200 | [diff] [blame] | 72 | snd_array_for_each(&codec->vendor_verbs, i, v) { |
Takashi Iwai | 5e56bce | 2015-02-26 12:29:03 +0100 | [diff] [blame] | 73 | if (verb == *v) |
| 74 | return true; |
| 75 | } |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 76 | |
Takashi Iwai | faa75f8 | 2015-02-26 08:54:56 +0100 | [diff] [blame] | 77 | if (codec->caps_overwriting) |
| 78 | return true; |
| 79 | |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 80 | switch (verb & 0xf00) { |
| 81 | case AC_VERB_GET_STREAM_FORMAT: |
| 82 | case AC_VERB_GET_AMP_GAIN_MUTE: |
| 83 | return true; |
Takashi Iwai | 40ba66a | 2015-03-13 15:56:25 +0100 | [diff] [blame] | 84 | case AC_VERB_GET_PROC_COEF: |
| 85 | return codec->cache_coef; |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 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: |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 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: |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 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: |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 108 | return true; |
| 109 | } |
| 110 | |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | static bool hda_readable_reg(struct device *dev, unsigned int reg) |
| 115 | { |
Takashi Iwai | faa75f8 | 2015-02-26 08:54:56 +0100 | [diff] [blame] | 116 | struct hdac_device *codec = dev_to_hdac_dev(dev); |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 117 | unsigned int verb = get_verb(reg); |
| 118 | |
Takashi Iwai | faa75f8 | 2015-02-26 08:54:56 +0100 | [diff] [blame] | 119 | if (codec->caps_overwriting) |
| 120 | return true; |
| 121 | |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 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; |
Takashi Iwai | 8bc174e | 2015-03-26 14:18:34 +0100 | [diff] [blame] | 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; |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | return hda_writeable_reg(dev, reg); |
| 137 | } |
| 138 | |
Takashi Iwai | d313e0a | 2015-03-04 20:43:20 +0100 | [diff] [blame] | 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, reg | AC_AMP_GET_LEFT, 0, &left); |
| 164 | if (err < 0) |
| 165 | return err; |
| 166 | err = snd_hdac_exec_verb(codec, reg | AC_AMP_GET_RIGHT, 0, &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, reg | left, 0, NULL); |
| 192 | } |
| 193 | |
| 194 | err = snd_hdac_exec_verb(codec, reg | AC_AMP_SET_LEFT | left, 0, NULL); |
| 195 | if (err < 0) |
| 196 | return err; |
| 197 | err = snd_hdac_exec_verb(codec, reg | AC_AMP_SET_RIGHT | right, 0, NULL); |
| 198 | if (err < 0) |
| 199 | return err; |
| 200 | return 0; |
| 201 | } |
| 202 | |
Takashi Iwai | 40ba66a | 2015-03-13 15:56:25 +0100 | [diff] [blame] | 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, verb, 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, verb, 0, 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, verb, 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, verb, 0, NULL); |
| 238 | } |
| 239 | |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 240 | static int hda_reg_read(void *context, unsigned int reg, unsigned int *val) |
| 241 | { |
| 242 | struct hdac_device *codec = context; |
Takashi Iwai | 40ba66a | 2015-03-13 15:56:25 +0100 | [diff] [blame] | 243 | int verb = get_verb(reg); |
Takashi Iwai | 33f8194 | 2015-03-09 22:19:47 +0100 | [diff] [blame] | 244 | int err; |
Takashi Iwai | fc4f000 | 2016-03-04 11:34:18 +0100 | [diff] [blame] | 245 | int pm_lock = 0; |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 246 | |
Takashi Iwai | fc4f000 | 2016-03-04 11:34:18 +0100 | [diff] [blame] | 247 | if (verb != AC_VERB_GET_POWER_STATE) { |
| 248 | pm_lock = codec_pm_lock(codec); |
| 249 | if (pm_lock < 0) |
| 250 | return -EAGAIN; |
| 251 | } |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 252 | reg |= (codec->addr << 28); |
Takashi Iwai | fc4f000 | 2016-03-04 11:34:18 +0100 | [diff] [blame] | 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 | } |
Takashi Iwai | a686ec4 | 2015-06-11 10:51:28 +0200 | [diff] [blame] | 261 | if ((verb & 0x700) == AC_VERB_SET_AMP_GAIN_MUTE) |
| 262 | reg &= ~AC_AMP_FAKE_MUTE; |
| 263 | |
Takashi Iwai | 33f8194 | 2015-03-09 22:19:47 +0100 | [diff] [blame] | 264 | err = snd_hdac_exec_verb(codec, reg, 0, val); |
| 265 | if (err < 0) |
Takashi Iwai | fc4f000 | 2016-03-04 11:34:18 +0100 | [diff] [blame] | 266 | goto out; |
Takashi Iwai | 33f8194 | 2015-03-09 22:19:47 +0100 | [diff] [blame] | 267 | /* special handling for asymmetric reads */ |
Takashi Iwai | 40ba66a | 2015-03-13 15:56:25 +0100 | [diff] [blame] | 268 | if (verb == AC_VERB_GET_POWER_STATE) { |
Takashi Iwai | 33f8194 | 2015-03-09 22:19:47 +0100 | [diff] [blame] | 269 | if (*val & AC_PWRST_ERROR) |
| 270 | *val = -1; |
| 271 | else /* take only the actual state */ |
| 272 | *val = (*val >> 4) & 0x0f; |
| 273 | } |
Takashi Iwai | fc4f000 | 2016-03-04 11:34:18 +0100 | [diff] [blame] | 274 | out: |
| 275 | codec_pm_unlock(codec, pm_lock); |
| 276 | return err; |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 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; |
Takashi Iwai | fc4f000 | 2016-03-04 11:34:18 +0100 | [diff] [blame] | 284 | int pm_lock = 0; |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 285 | |
Takashi Iwai | 98a226e | 2015-06-10 10:27:00 +0200 | [diff] [blame] | 286 | if (codec->caps_overwriting) |
| 287 | return 0; |
| 288 | |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 289 | reg &= ~0x00080000U; /* drop GET bit */ |
| 290 | reg |= (codec->addr << 28); |
Takashi Iwai | 9efe273 | 2015-04-09 07:58:46 +0200 | [diff] [blame] | 291 | verb = get_verb(reg); |
| 292 | |
Takashi Iwai | fc4f000 | 2016-03-04 11:34:18 +0100 | [diff] [blame] | 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 | } |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 298 | |
Takashi Iwai | fc4f000 | 2016-03-04 11:34:18 +0100 | [diff] [blame] | 299 | if (is_stereo_amp_verb(reg)) { |
| 300 | err = hda_reg_write_stereo_amp(codec, reg, val); |
| 301 | goto out; |
| 302 | } |
Takashi Iwai | d313e0a | 2015-03-04 20:43:20 +0100 | [diff] [blame] | 303 | |
Takashi Iwai | fc4f000 | 2016-03-04 11:34:18 +0100 | [diff] [blame] | 304 | if (verb == AC_VERB_SET_PROC_COEF) { |
| 305 | err = hda_reg_write_coef(codec, reg, val); |
| 306 | goto out; |
| 307 | } |
Takashi Iwai | 40ba66a | 2015-03-13 15:56:25 +0100 | [diff] [blame] | 308 | |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 309 | switch (verb & 0xf00) { |
| 310 | case AC_VERB_SET_AMP_GAIN_MUTE: |
Takashi Iwai | a686ec4 | 2015-06-11 10:51:28 +0200 | [diff] [blame] | 311 | if ((reg & AC_AMP_FAKE_MUTE) && (val & AC_AMP_MUTE)) |
| 312 | val = 0; |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 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, reg, 0, NULL); |
| 343 | if (err < 0) |
Takashi Iwai | fc4f000 | 2016-03-04 11:34:18 +0100 | [diff] [blame] | 344 | goto out; |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 345 | } |
| 346 | |
Takashi Iwai | fc4f000 | 2016-03-04 11:34:18 +0100 | [diff] [blame] | 347 | out: |
| 348 | codec_pm_unlock(codec, pm_lock); |
| 349 | return err; |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 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_RBTREE, |
| 361 | .reg_read = hda_reg_read, |
| 362 | .reg_write = hda_reg_write, |
David Frey | 1c96a2f | 2018-09-01 09:50:41 -0700 | [diff] [blame] | 363 | .use_single_read = true, |
| 364 | .use_single_write = true, |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 365 | }; |
| 366 | |
Takashi Iwai | 78dd5e2 | 2015-10-28 12:26:48 +0100 | [diff] [blame] | 367 | /** |
| 368 | * snd_hdac_regmap_init - Initialize regmap for HDA register accesses |
| 369 | * @codec: the codec object |
| 370 | * |
| 371 | * Returns zero for success or a negative error code. |
| 372 | */ |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 373 | int snd_hdac_regmap_init(struct hdac_device *codec) |
| 374 | { |
| 375 | struct regmap *regmap; |
| 376 | |
| 377 | regmap = regmap_init(&codec->dev, NULL, codec, &hda_regmap_cfg); |
| 378 | if (IS_ERR(regmap)) |
| 379 | return PTR_ERR(regmap); |
| 380 | codec->regmap = regmap; |
Takashi Iwai | 5e56bce | 2015-02-26 12:29:03 +0100 | [diff] [blame] | 381 | snd_array_init(&codec->vendor_verbs, sizeof(unsigned int), 8); |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 382 | return 0; |
| 383 | } |
| 384 | EXPORT_SYMBOL_GPL(snd_hdac_regmap_init); |
| 385 | |
Takashi Iwai | 78dd5e2 | 2015-10-28 12:26:48 +0100 | [diff] [blame] | 386 | /** |
| 387 | * snd_hdac_regmap_init - Release the regmap from HDA codec |
| 388 | * @codec: the codec object |
| 389 | */ |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 390 | void snd_hdac_regmap_exit(struct hdac_device *codec) |
| 391 | { |
| 392 | if (codec->regmap) { |
| 393 | regmap_exit(codec->regmap); |
| 394 | codec->regmap = NULL; |
Takashi Iwai | 5e56bce | 2015-02-26 12:29:03 +0100 | [diff] [blame] | 395 | snd_array_free(&codec->vendor_verbs); |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | EXPORT_SYMBOL_GPL(snd_hdac_regmap_exit); |
| 399 | |
Takashi Iwai | 5e56bce | 2015-02-26 12:29:03 +0100 | [diff] [blame] | 400 | /** |
| 401 | * snd_hdac_regmap_add_vendor_verb - add a vendor-specific verb to regmap |
| 402 | * @codec: the codec object |
| 403 | * @verb: verb to allow accessing via regmap |
| 404 | * |
| 405 | * Returns zero for success or a negative error code. |
| 406 | */ |
| 407 | int snd_hdac_regmap_add_vendor_verb(struct hdac_device *codec, |
| 408 | unsigned int verb) |
| 409 | { |
| 410 | unsigned int *p = snd_array_new(&codec->vendor_verbs); |
| 411 | |
| 412 | if (!p) |
| 413 | return -ENOMEM; |
Mengdong Lin | d6eb9e3 | 2015-04-14 11:25:36 +0800 | [diff] [blame] | 414 | *p = verb | 0x800; /* set GET bit */ |
Takashi Iwai | 5e56bce | 2015-02-26 12:29:03 +0100 | [diff] [blame] | 415 | return 0; |
| 416 | } |
| 417 | EXPORT_SYMBOL_GPL(snd_hdac_regmap_add_vendor_verb); |
| 418 | |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 419 | /* |
| 420 | * helper functions |
| 421 | */ |
| 422 | |
| 423 | /* write a pseudo-register value (w/o power sequence) */ |
| 424 | static int reg_raw_write(struct hdac_device *codec, unsigned int reg, |
| 425 | unsigned int val) |
| 426 | { |
| 427 | if (!codec->regmap) |
| 428 | return hda_reg_write(codec, reg, val); |
| 429 | else |
| 430 | return regmap_write(codec->regmap, reg, val); |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * snd_hdac_regmap_write_raw - write a pseudo register with power mgmt |
| 435 | * @codec: the codec object |
| 436 | * @reg: pseudo register |
| 437 | * @val: value to write |
| 438 | * |
| 439 | * Returns zero if successful or a negative error code. |
| 440 | */ |
| 441 | int snd_hdac_regmap_write_raw(struct hdac_device *codec, unsigned int reg, |
| 442 | unsigned int val) |
| 443 | { |
| 444 | int err; |
| 445 | |
| 446 | err = reg_raw_write(codec, reg, val); |
| 447 | if (err == -EAGAIN) { |
Takashi Iwai | fbce23a0 | 2015-07-17 16:27:33 +0200 | [diff] [blame] | 448 | err = snd_hdac_power_up_pm(codec); |
Jaroslav Kysela | 8198868 | 2016-06-17 13:35:56 +0200 | [diff] [blame] | 449 | if (err >= 0) |
Takashi Iwai | fbce23a0 | 2015-07-17 16:27:33 +0200 | [diff] [blame] | 450 | err = reg_raw_write(codec, reg, val); |
Takashi Iwai | 664c715 | 2015-04-08 11:43:14 +0200 | [diff] [blame] | 451 | snd_hdac_power_down_pm(codec); |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 452 | } |
| 453 | return err; |
| 454 | } |
| 455 | EXPORT_SYMBOL_GPL(snd_hdac_regmap_write_raw); |
| 456 | |
| 457 | static int reg_raw_read(struct hdac_device *codec, unsigned int reg, |
Takashi Iwai | 3194ed4 | 2016-04-21 17:49:11 +0200 | [diff] [blame] | 458 | unsigned int *val, bool uncached) |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 459 | { |
Takashi Iwai | 3194ed4 | 2016-04-21 17:49:11 +0200 | [diff] [blame] | 460 | if (uncached || !codec->regmap) |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 461 | return hda_reg_read(codec, reg, val); |
| 462 | else |
| 463 | return regmap_read(codec->regmap, reg, val); |
| 464 | } |
| 465 | |
Takashi Iwai | 3194ed4 | 2016-04-21 17:49:11 +0200 | [diff] [blame] | 466 | static int __snd_hdac_regmap_read_raw(struct hdac_device *codec, |
| 467 | unsigned int reg, unsigned int *val, |
| 468 | bool uncached) |
| 469 | { |
| 470 | int err; |
| 471 | |
| 472 | err = reg_raw_read(codec, reg, val, uncached); |
| 473 | if (err == -EAGAIN) { |
| 474 | err = snd_hdac_power_up_pm(codec); |
Jaroslav Kysela | 8198868 | 2016-06-17 13:35:56 +0200 | [diff] [blame] | 475 | if (err >= 0) |
Takashi Iwai | 3194ed4 | 2016-04-21 17:49:11 +0200 | [diff] [blame] | 476 | err = reg_raw_read(codec, reg, val, uncached); |
| 477 | snd_hdac_power_down_pm(codec); |
| 478 | } |
| 479 | return err; |
| 480 | } |
| 481 | |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 482 | /** |
| 483 | * snd_hdac_regmap_read_raw - read a pseudo register with power mgmt |
| 484 | * @codec: the codec object |
| 485 | * @reg: pseudo register |
| 486 | * @val: pointer to store the read value |
| 487 | * |
| 488 | * Returns zero if successful or a negative error code. |
| 489 | */ |
| 490 | int snd_hdac_regmap_read_raw(struct hdac_device *codec, unsigned int reg, |
| 491 | unsigned int *val) |
| 492 | { |
Takashi Iwai | 3194ed4 | 2016-04-21 17:49:11 +0200 | [diff] [blame] | 493 | return __snd_hdac_regmap_read_raw(codec, reg, val, false); |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 494 | } |
| 495 | EXPORT_SYMBOL_GPL(snd_hdac_regmap_read_raw); |
| 496 | |
Takashi Iwai | 3194ed4 | 2016-04-21 17:49:11 +0200 | [diff] [blame] | 497 | /* Works like snd_hdac_regmap_read_raw(), but this doesn't read from the |
| 498 | * cache but always via hda verbs. |
| 499 | */ |
| 500 | int snd_hdac_regmap_read_raw_uncached(struct hdac_device *codec, |
| 501 | unsigned int reg, unsigned int *val) |
| 502 | { |
| 503 | return __snd_hdac_regmap_read_raw(codec, reg, val, true); |
| 504 | } |
| 505 | |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame] | 506 | /** |
| 507 | * snd_hdac_regmap_update_raw - update a pseudo register with power mgmt |
| 508 | * @codec: the codec object |
| 509 | * @reg: pseudo register |
| 510 | * @mask: bit mask to udpate |
| 511 | * @val: value to update |
| 512 | * |
| 513 | * Returns zero if successful or a negative error code. |
| 514 | */ |
| 515 | int snd_hdac_regmap_update_raw(struct hdac_device *codec, unsigned int reg, |
| 516 | unsigned int mask, unsigned int val) |
| 517 | { |
| 518 | unsigned int orig; |
| 519 | int err; |
| 520 | |
| 521 | val &= mask; |
| 522 | err = snd_hdac_regmap_read_raw(codec, reg, &orig); |
| 523 | if (err < 0) |
| 524 | return err; |
| 525 | val |= orig & ~mask; |
| 526 | if (val == orig) |
| 527 | return 0; |
| 528 | err = snd_hdac_regmap_write_raw(codec, reg, val); |
| 529 | if (err < 0) |
| 530 | return err; |
| 531 | return 1; |
| 532 | } |
| 533 | EXPORT_SYMBOL_GPL(snd_hdac_regmap_update_raw); |