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_PIN_WIDGET_CONTROL
:
92 case AC_VERB_GET_UNSOLICITED_RESPONSE
: /* only as SET_UNSOLICITED_ENABLE */
93 case AC_VERB_GET_BEEP_CONTROL
:
94 case AC_VERB_GET_EAPD_BTLENABLE
:
95 case AC_VERB_GET_DIGI_CONVERT_1
:
96 case AC_VERB_GET_DIGI_CONVERT_2
: /* only for beep control */
97 case AC_VERB_GET_VOLUME_KNOB_CONTROL
:
98 case AC_VERB_GET_GPIO_MASK
:
99 case AC_VERB_GET_GPIO_DIRECTION
:
100 case AC_VERB_GET_GPIO_DATA
: /* not for volatile read */
101 case AC_VERB_GET_GPIO_WAKE_MASK
:
102 case AC_VERB_GET_GPIO_UNSOLICITED_RSP_MASK
:
103 case AC_VERB_GET_GPIO_STICKY_MASK
:
110 static bool hda_readable_reg(struct device
*dev
, unsigned int reg
)
112 struct hdac_device
*codec
= dev_to_hdac_dev(dev
);
113 unsigned int verb
= get_verb(reg
);
115 if (codec
->caps_overwriting
)
119 case AC_VERB_PARAMETERS
:
120 case AC_VERB_GET_CONNECT_LIST
:
121 case AC_VERB_GET_SUBSYSTEM_ID
:
123 /* below are basically writable, but disabled for reducing unnecessary
126 case AC_VERB_GET_CONFIG_DEFAULT
: /* usually just read */
127 case AC_VERB_GET_CONV
: /* managed in PCM code */
128 case AC_VERB_GET_CVT_CHAN_COUNT
: /* managed in HDMI CA code */
132 return hda_writeable_reg(dev
, reg
);
136 * Stereo amp pseudo register:
137 * for making easier to handle the stereo volume control, we provide a
138 * fake register to deal both left and right channels by a single
139 * (pseudo) register access. A verb consisting of SET_AMP_GAIN with
140 * *both* SET_LEFT and SET_RIGHT bits takes a 16bit value, the lower 8bit
141 * for the left and the upper 8bit for the right channel.
143 static bool is_stereo_amp_verb(unsigned int reg
)
145 if (((reg
>> 8) & 0x700) != AC_VERB_SET_AMP_GAIN_MUTE
)
147 return (reg
& (AC_AMP_SET_LEFT
| AC_AMP_SET_RIGHT
)) ==
148 (AC_AMP_SET_LEFT
| AC_AMP_SET_RIGHT
);
151 /* read a pseudo stereo amp register (16bit left+right) */
152 static int hda_reg_read_stereo_amp(struct hdac_device
*codec
,
153 unsigned int reg
, unsigned int *val
)
155 unsigned int left
, right
;
158 reg
&= ~(AC_AMP_SET_LEFT
| AC_AMP_SET_RIGHT
);
159 err
= snd_hdac_exec_verb(codec
, reg
| AC_AMP_GET_LEFT
, 0, &left
);
162 err
= snd_hdac_exec_verb(codec
, reg
| AC_AMP_GET_RIGHT
, 0, &right
);
165 *val
= left
| (right
<< 8);
169 /* write a pseudo stereo amp register (16bit left+right) */
170 static int hda_reg_write_stereo_amp(struct hdac_device
*codec
,
171 unsigned int reg
, unsigned int val
)
174 unsigned int verb
, left
, right
;
176 verb
= AC_VERB_SET_AMP_GAIN_MUTE
<< 8;
177 if (reg
& AC_AMP_GET_OUTPUT
)
178 verb
|= AC_AMP_SET_OUTPUT
;
180 verb
|= AC_AMP_SET_INPUT
| ((reg
& 0xf) << 8);
181 reg
= (reg
& ~0xfffff) | verb
;
184 right
= (val
>> 8) & 0xff;
186 reg
|= AC_AMP_SET_LEFT
| AC_AMP_SET_RIGHT
;
187 return snd_hdac_exec_verb(codec
, reg
| left
, 0, NULL
);
190 err
= snd_hdac_exec_verb(codec
, reg
| AC_AMP_SET_LEFT
| left
, 0, NULL
);
193 err
= snd_hdac_exec_verb(codec
, reg
| AC_AMP_SET_RIGHT
| right
, 0, NULL
);
199 /* read a pseudo coef register (16bit) */
200 static int hda_reg_read_coef(struct hdac_device
*codec
, unsigned int reg
,
206 if (!codec
->cache_coef
)
208 /* LSB 8bit = coef index */
209 verb
= (reg
& ~0xfff00) | (AC_VERB_SET_COEF_INDEX
<< 8);
210 err
= snd_hdac_exec_verb(codec
, verb
, 0, NULL
);
213 verb
= (reg
& ~0xfffff) | (AC_VERB_GET_COEF_INDEX
<< 8);
214 return snd_hdac_exec_verb(codec
, verb
, 0, val
);
217 /* write a pseudo coef register (16bit) */
218 static int hda_reg_write_coef(struct hdac_device
*codec
, unsigned int reg
,
224 if (!codec
->cache_coef
)
226 /* LSB 8bit = coef index */
227 verb
= (reg
& ~0xfff00) | (AC_VERB_SET_COEF_INDEX
<< 8);
228 err
= snd_hdac_exec_verb(codec
, verb
, 0, NULL
);
231 verb
= (reg
& ~0xfffff) | (AC_VERB_GET_COEF_INDEX
<< 8) |
233 return snd_hdac_exec_verb(codec
, verb
, 0, NULL
);
236 static int hda_reg_read(void *context
, unsigned int reg
, unsigned int *val
)
238 struct hdac_device
*codec
= context
;
239 int verb
= get_verb(reg
);
242 if (!codec_is_running(codec
) && verb
!= AC_VERB_GET_POWER_STATE
)
244 reg
|= (codec
->addr
<< 28);
245 if (is_stereo_amp_verb(reg
))
246 return hda_reg_read_stereo_amp(codec
, reg
, val
);
247 if (verb
== AC_VERB_GET_PROC_COEF
)
248 return hda_reg_read_coef(codec
, reg
, val
);
249 if ((verb
& 0x700) == AC_VERB_SET_AMP_GAIN_MUTE
)
250 reg
&= ~AC_AMP_FAKE_MUTE
;
252 err
= snd_hdac_exec_verb(codec
, reg
, 0, val
);
255 /* special handling for asymmetric reads */
256 if (verb
== AC_VERB_GET_POWER_STATE
) {
257 if (*val
& AC_PWRST_ERROR
)
259 else /* take only the actual state */
260 *val
= (*val
>> 4) & 0x0f;
265 static int hda_reg_write(void *context
, unsigned int reg
, unsigned int val
)
267 struct hdac_device
*codec
= context
;
271 if (codec
->caps_overwriting
)
274 reg
&= ~0x00080000U
; /* drop GET bit */
275 reg
|= (codec
->addr
<< 28);
276 verb
= get_verb(reg
);
278 if (!codec_is_running(codec
) && verb
!= AC_VERB_SET_POWER_STATE
)
279 return codec
->lazy_cache
? 0 : -EAGAIN
;
281 if (is_stereo_amp_verb(reg
))
282 return hda_reg_write_stereo_amp(codec
, reg
, val
);
284 if (verb
== AC_VERB_SET_PROC_COEF
)
285 return hda_reg_write_coef(codec
, reg
, val
);
287 switch (verb
& 0xf00) {
288 case AC_VERB_SET_AMP_GAIN_MUTE
:
289 if ((reg
& AC_AMP_FAKE_MUTE
) && (val
& AC_AMP_MUTE
))
291 verb
= AC_VERB_SET_AMP_GAIN_MUTE
;
292 if (reg
& AC_AMP_GET_LEFT
)
293 verb
|= AC_AMP_SET_LEFT
>> 8;
295 verb
|= AC_AMP_SET_RIGHT
>> 8;
296 if (reg
& AC_AMP_GET_OUTPUT
) {
297 verb
|= AC_AMP_SET_OUTPUT
>> 8;
299 verb
|= AC_AMP_SET_INPUT
>> 8;
306 case AC_VERB_SET_DIGI_CONVERT_1
:
309 case AC_VERB_SET_CONFIG_DEFAULT_BYTES_0
:
317 for (i
= 0; i
< bytes
; i
++) {
319 reg
|= (verb
+ i
) << 8 | ((val
>> (8 * i
)) & 0xff);
320 err
= snd_hdac_exec_verb(codec
, reg
, 0, NULL
);
328 static const struct regmap_config hda_regmap_cfg
= {
332 .max_register
= 0xfffffff,
333 .writeable_reg
= hda_writeable_reg
,
334 .readable_reg
= hda_readable_reg
,
335 .volatile_reg
= hda_volatile_reg
,
336 .cache_type
= REGCACHE_RBTREE
,
337 .reg_read
= hda_reg_read
,
338 .reg_write
= hda_reg_write
,
339 .use_single_rw
= true,
343 * snd_hdac_regmap_init - Initialize regmap for HDA register accesses
344 * @codec: the codec object
346 * Returns zero for success or a negative error code.
348 int snd_hdac_regmap_init(struct hdac_device
*codec
)
350 struct regmap
*regmap
;
352 regmap
= regmap_init(&codec
->dev
, NULL
, codec
, &hda_regmap_cfg
);
354 return PTR_ERR(regmap
);
355 codec
->regmap
= regmap
;
356 snd_array_init(&codec
->vendor_verbs
, sizeof(unsigned int), 8);
359 EXPORT_SYMBOL_GPL(snd_hdac_regmap_init
);
362 * snd_hdac_regmap_init - Release the regmap from HDA codec
363 * @codec: the codec object
365 void snd_hdac_regmap_exit(struct hdac_device
*codec
)
368 regmap_exit(codec
->regmap
);
369 codec
->regmap
= NULL
;
370 snd_array_free(&codec
->vendor_verbs
);
373 EXPORT_SYMBOL_GPL(snd_hdac_regmap_exit
);
376 * snd_hdac_regmap_add_vendor_verb - add a vendor-specific verb to regmap
377 * @codec: the codec object
378 * @verb: verb to allow accessing via regmap
380 * Returns zero for success or a negative error code.
382 int snd_hdac_regmap_add_vendor_verb(struct hdac_device
*codec
,
385 unsigned int *p
= snd_array_new(&codec
->vendor_verbs
);
389 *p
= verb
| 0x800; /* set GET bit */
392 EXPORT_SYMBOL_GPL(snd_hdac_regmap_add_vendor_verb
);
398 /* write a pseudo-register value (w/o power sequence) */
399 static int reg_raw_write(struct hdac_device
*codec
, unsigned int reg
,
403 return hda_reg_write(codec
, reg
, val
);
405 return regmap_write(codec
->regmap
, reg
, val
);
409 * snd_hdac_regmap_write_raw - write a pseudo register with power mgmt
410 * @codec: the codec object
411 * @reg: pseudo register
412 * @val: value to write
414 * Returns zero if successful or a negative error code.
416 int snd_hdac_regmap_write_raw(struct hdac_device
*codec
, unsigned int reg
,
421 err
= reg_raw_write(codec
, reg
, val
);
422 if (err
== -EAGAIN
) {
423 err
= snd_hdac_power_up_pm(codec
);
425 err
= reg_raw_write(codec
, reg
, val
);
426 snd_hdac_power_down_pm(codec
);
430 EXPORT_SYMBOL_GPL(snd_hdac_regmap_write_raw
);
432 static int reg_raw_read(struct hdac_device
*codec
, unsigned int reg
,
436 return hda_reg_read(codec
, reg
, val
);
438 return regmap_read(codec
->regmap
, reg
, val
);
442 * snd_hdac_regmap_read_raw - read a pseudo register with power mgmt
443 * @codec: the codec object
444 * @reg: pseudo register
445 * @val: pointer to store the read value
447 * Returns zero if successful or a negative error code.
449 int snd_hdac_regmap_read_raw(struct hdac_device
*codec
, unsigned int reg
,
454 err
= reg_raw_read(codec
, reg
, val
);
455 if (err
== -EAGAIN
) {
456 err
= snd_hdac_power_up_pm(codec
);
458 err
= reg_raw_read(codec
, reg
, val
);
459 snd_hdac_power_down_pm(codec
);
463 EXPORT_SYMBOL_GPL(snd_hdac_regmap_read_raw
);
466 * snd_hdac_regmap_update_raw - update a pseudo register with power mgmt
467 * @codec: the codec object
468 * @reg: pseudo register
469 * @mask: bit mask to udpate
470 * @val: value to update
472 * Returns zero if successful or a negative error code.
474 int snd_hdac_regmap_update_raw(struct hdac_device
*codec
, unsigned int reg
,
475 unsigned int mask
, unsigned int val
)
481 err
= snd_hdac_regmap_read_raw(codec
, reg
, &orig
);
487 err
= snd_hdac_regmap_write_raw(codec
, reg
, val
);
492 EXPORT_SYMBOL_GPL(snd_hdac_regmap_update_raw
);