2 * Regmap support for HD-audio verbs
4 * A virtual register is translated to one or more hda verbs for write,
8 * - Provided for not all verbs but only subset standard non-volatile verbs.
9 * - For reading, only AC_VERB_GET_* variants can be used.
10 * - For writing, mapped to the *corresponding* AC_VERB_SET_* variants,
11 * so can't handle asymmetric verbs for read and write
14 #include <linux/slab.h>
15 #include <linux/device.h>
16 #include <linux/regmap.h>
17 #include <linux/export.h>
19 #include <linux/pm_runtime.h>
20 #include <sound/core.h>
21 #include <sound/hdaudio.h>
22 #include <sound/hda_regmap.h>
25 #define codec_is_running(codec) \
26 (atomic_read(&(codec)->in_pm) || \
27 !pm_runtime_suspended(&(codec)->dev))
29 #define codec_is_running(codec) true
32 #define get_verb(reg) (((reg) >> 8) & 0xfff)
34 static bool hda_volatile_reg(struct device *dev, unsigned int reg)
36 struct hdac_device *codec = dev_to_hdac_dev(dev);
37 unsigned int verb = get_verb(reg);
40 case AC_VERB_GET_PROC_COEF:
41 return !codec->cache_coef;
42 case AC_VERB_GET_COEF_INDEX:
43 case AC_VERB_GET_PROC_STATE:
44 case AC_VERB_GET_POWER_STATE:
45 case AC_VERB_GET_PIN_SENSE:
46 case AC_VERB_GET_HDMI_DIP_SIZE:
47 case AC_VERB_GET_HDMI_ELDD:
48 case AC_VERB_GET_HDMI_DIP_INDEX:
49 case AC_VERB_GET_HDMI_DIP_DATA:
50 case AC_VERB_GET_HDMI_DIP_XMIT:
51 case AC_VERB_GET_HDMI_CP_CTRL:
52 case AC_VERB_GET_HDMI_CHAN_SLOT:
53 case AC_VERB_GET_DEVICE_SEL:
54 case AC_VERB_GET_DEVICE_LIST: /* read-only volatile */
61 static bool hda_writeable_reg(struct device *dev, unsigned int reg)
63 struct hdac_device *codec = dev_to_hdac_dev(dev);
64 unsigned int verb = get_verb(reg);
67 for (i = 0; i < codec->vendor_verbs.used; i++) {
68 unsigned int *v = snd_array_elem(&codec->vendor_verbs, i);
73 if (codec->caps_overwriting)
76 switch (verb & 0xf00) {
77 case AC_VERB_GET_STREAM_FORMAT:
78 case AC_VERB_GET_AMP_GAIN_MUTE:
80 case AC_VERB_GET_PROC_COEF:
81 return codec->cache_coef;
89 case AC_VERB_GET_CONNECT_SEL:
90 case AC_VERB_GET_SDI_SELECT:
91 case AC_VERB_GET_CONV:
92 case AC_VERB_GET_PIN_WIDGET_CONTROL:
93 case AC_VERB_GET_UNSOLICITED_RESPONSE: /* only as SET_UNSOLICITED_ENABLE */
94 case AC_VERB_GET_BEEP_CONTROL:
95 case AC_VERB_GET_EAPD_BTLENABLE:
96 case AC_VERB_GET_DIGI_CONVERT_1:
97 case AC_VERB_GET_DIGI_CONVERT_2: /* only for beep control */
98 case AC_VERB_GET_VOLUME_KNOB_CONTROL:
99 case AC_VERB_GET_CONFIG_DEFAULT:
100 case AC_VERB_GET_GPIO_MASK:
101 case AC_VERB_GET_GPIO_DIRECTION:
102 case AC_VERB_GET_GPIO_DATA: /* not for volatile read */
103 case AC_VERB_GET_GPIO_WAKE_MASK:
104 case AC_VERB_GET_GPIO_UNSOLICITED_RSP_MASK:
105 case AC_VERB_GET_GPIO_STICKY_MASK:
106 case AC_VERB_GET_CVT_CHAN_COUNT:
113 static bool hda_readable_reg(struct device *dev, unsigned int reg)
115 struct hdac_device *codec = dev_to_hdac_dev(dev);
116 unsigned int verb = get_verb(reg);
118 if (codec->caps_overwriting)
122 case AC_VERB_PARAMETERS:
123 case AC_VERB_GET_CONNECT_LIST:
124 case AC_VERB_GET_SUBSYSTEM_ID:
128 return hda_writeable_reg(dev, reg);
132 * Stereo amp pseudo register:
133 * for making easier to handle the stereo volume control, we provide a
134 * fake register to deal both left and right channels by a single
135 * (pseudo) register access. A verb consisting of SET_AMP_GAIN with
136 * *both* SET_LEFT and SET_RIGHT bits takes a 16bit value, the lower 8bit
137 * for the left and the upper 8bit for the right channel.
139 static bool is_stereo_amp_verb(unsigned int reg)
141 if (((reg >> 8) & 0x700) != AC_VERB_SET_AMP_GAIN_MUTE)
143 return (reg & (AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT)) ==
144 (AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT);
147 /* read a pseudo stereo amp register (16bit left+right) */
148 static int hda_reg_read_stereo_amp(struct hdac_device *codec,
149 unsigned int reg, unsigned int *val)
151 unsigned int left, right;
154 reg &= ~(AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT);
155 err = snd_hdac_exec_verb(codec, reg | AC_AMP_GET_LEFT, 0, &left);
158 err = snd_hdac_exec_verb(codec, reg | AC_AMP_GET_RIGHT, 0, &right);
161 *val = left | (right << 8);
165 /* write a pseudo stereo amp register (16bit left+right) */
166 static int hda_reg_write_stereo_amp(struct hdac_device *codec,
167 unsigned int reg, unsigned int val)
170 unsigned int verb, left, right;
172 verb = AC_VERB_SET_AMP_GAIN_MUTE << 8;
173 if (reg & AC_AMP_GET_OUTPUT)
174 verb |= AC_AMP_SET_OUTPUT;
176 verb |= AC_AMP_SET_INPUT | ((reg & 0xf) << 8);
177 reg = (reg & ~0xfffff) | verb;
180 right = (val >> 8) & 0xff;
182 reg |= AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT;
183 return snd_hdac_exec_verb(codec, reg | left, 0, NULL);
186 err = snd_hdac_exec_verb(codec, reg | AC_AMP_SET_LEFT | left, 0, NULL);
189 err = snd_hdac_exec_verb(codec, reg | AC_AMP_SET_RIGHT | right, 0, NULL);
195 /* read a pseudo coef register (16bit) */
196 static int hda_reg_read_coef(struct hdac_device *codec, unsigned int reg,
202 if (!codec->cache_coef)
204 /* LSB 8bit = coef index */
205 verb = (reg & ~0xfff00) | (AC_VERB_SET_COEF_INDEX << 8);
206 err = snd_hdac_exec_verb(codec, verb, 0, NULL);
209 verb = (reg & ~0xfffff) | (AC_VERB_GET_COEF_INDEX << 8);
210 return snd_hdac_exec_verb(codec, verb, 0, val);
213 /* write a pseudo coef register (16bit) */
214 static int hda_reg_write_coef(struct hdac_device *codec, unsigned int reg,
220 if (!codec->cache_coef)
222 /* LSB 8bit = coef index */
223 verb = (reg & ~0xfff00) | (AC_VERB_SET_COEF_INDEX << 8);
224 err = snd_hdac_exec_verb(codec, verb, 0, NULL);
227 verb = (reg & ~0xfffff) | (AC_VERB_GET_COEF_INDEX << 8) |
229 return snd_hdac_exec_verb(codec, verb, 0, NULL);
232 static int hda_reg_read(void *context, unsigned int reg, unsigned int *val)
234 struct hdac_device *codec = context;
235 int verb = get_verb(reg);
238 if (!codec_is_running(codec))
240 reg |= (codec->addr << 28);
241 if (is_stereo_amp_verb(reg))
242 return hda_reg_read_stereo_amp(codec, reg, val);
243 if (verb == AC_VERB_GET_PROC_COEF)
244 return hda_reg_read_coef(codec, reg, val);
245 err = snd_hdac_exec_verb(codec, reg, 0, val);
248 /* special handling for asymmetric reads */
249 if (verb == AC_VERB_GET_POWER_STATE) {
250 if (*val & AC_PWRST_ERROR)
252 else /* take only the actual state */
253 *val = (*val >> 4) & 0x0f;
258 static int hda_reg_write(void *context, unsigned int reg, unsigned int val)
260 struct hdac_device *codec = context;
264 if (!codec_is_running(codec))
265 return codec->lazy_cache ? 0 : -EAGAIN;
267 reg &= ~0x00080000U; /* drop GET bit */
268 reg |= (codec->addr << 28);
270 if (is_stereo_amp_verb(reg))
271 return hda_reg_write_stereo_amp(codec, reg, val);
273 verb = get_verb(reg);
274 if (verb == AC_VERB_SET_PROC_COEF)
275 return hda_reg_write_coef(codec, reg, val);
277 switch (verb & 0xf00) {
278 case AC_VERB_SET_AMP_GAIN_MUTE:
279 verb = AC_VERB_SET_AMP_GAIN_MUTE;
280 if (reg & AC_AMP_GET_LEFT)
281 verb |= AC_AMP_SET_LEFT >> 8;
283 verb |= AC_AMP_SET_RIGHT >> 8;
284 if (reg & AC_AMP_GET_OUTPUT) {
285 verb |= AC_AMP_SET_OUTPUT >> 8;
287 verb |= AC_AMP_SET_INPUT >> 8;
294 case AC_VERB_SET_DIGI_CONVERT_1:
297 case AC_VERB_SET_CONFIG_DEFAULT_BYTES_0:
305 for (i = 0; i < bytes; i++) {
307 reg |= (verb + i) << 8 | ((val >> (8 * i)) & 0xff);
308 err = snd_hdac_exec_verb(codec, reg, 0, NULL);
316 static const struct regmap_config hda_regmap_cfg = {
320 .max_register = 0xfffffff,
321 .writeable_reg = hda_writeable_reg,
322 .readable_reg = hda_readable_reg,
323 .volatile_reg = hda_volatile_reg,
324 .cache_type = REGCACHE_RBTREE,
325 .reg_read = hda_reg_read,
326 .reg_write = hda_reg_write,
329 int snd_hdac_regmap_init(struct hdac_device *codec)
331 struct regmap *regmap;
333 regmap = regmap_init(&codec->dev, NULL, codec, &hda_regmap_cfg);
335 return PTR_ERR(regmap);
336 codec->regmap = regmap;
337 snd_array_init(&codec->vendor_verbs, sizeof(unsigned int), 8);
340 EXPORT_SYMBOL_GPL(snd_hdac_regmap_init);
342 void snd_hdac_regmap_exit(struct hdac_device *codec)
345 regmap_exit(codec->regmap);
346 codec->regmap = NULL;
347 snd_array_free(&codec->vendor_verbs);
350 EXPORT_SYMBOL_GPL(snd_hdac_regmap_exit);
353 * snd_hdac_regmap_add_vendor_verb - add a vendor-specific verb to regmap
354 * @codec: the codec object
355 * @verb: verb to allow accessing via regmap
357 * Returns zero for success or a negative error code.
359 int snd_hdac_regmap_add_vendor_verb(struct hdac_device *codec,
362 unsigned int *p = snd_array_new(&codec->vendor_verbs);
369 EXPORT_SYMBOL_GPL(snd_hdac_regmap_add_vendor_verb);
375 /* write a pseudo-register value (w/o power sequence) */
376 static int reg_raw_write(struct hdac_device *codec, unsigned int reg,
380 return hda_reg_write(codec, reg, val);
382 return regmap_write(codec->regmap, reg, val);
386 * snd_hdac_regmap_write_raw - write a pseudo register with power mgmt
387 * @codec: the codec object
388 * @reg: pseudo register
389 * @val: value to write
391 * Returns zero if successful or a negative error code.
393 int snd_hdac_regmap_write_raw(struct hdac_device *codec, unsigned int reg,
398 err = reg_raw_write(codec, reg, val);
399 if (err == -EAGAIN) {
400 snd_hdac_power_up(codec);
401 err = reg_raw_write(codec, reg, val);
402 snd_hdac_power_down(codec);
406 EXPORT_SYMBOL_GPL(snd_hdac_regmap_write_raw);
408 static int reg_raw_read(struct hdac_device *codec, unsigned int reg,
412 return hda_reg_read(codec, reg, val);
414 return regmap_read(codec->regmap, reg, val);
418 * snd_hdac_regmap_read_raw - read a pseudo register with power mgmt
419 * @codec: the codec object
420 * @reg: pseudo register
421 * @val: pointer to store the read value
423 * Returns zero if successful or a negative error code.
425 int snd_hdac_regmap_read_raw(struct hdac_device *codec, unsigned int reg,
430 err = reg_raw_read(codec, reg, val);
431 if (err == -EAGAIN) {
432 snd_hdac_power_up(codec);
433 err = reg_raw_read(codec, reg, val);
434 snd_hdac_power_down(codec);
438 EXPORT_SYMBOL_GPL(snd_hdac_regmap_read_raw);
441 * snd_hdac_regmap_update_raw - update a pseudo register with power mgmt
442 * @codec: the codec object
443 * @reg: pseudo register
444 * @mask: bit mask to udpate
445 * @val: value to update
447 * Returns zero if successful or a negative error code.
449 int snd_hdac_regmap_update_raw(struct hdac_device *codec, unsigned int reg,
450 unsigned int mask, unsigned int val)
456 err = snd_hdac_regmap_read_raw(codec, reg, &orig);
462 err = snd_hdac_regmap_write_raw(codec, reg, val);
467 EXPORT_SYMBOL_GPL(snd_hdac_regmap_update_raw);