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 unsigned int verb
= get_verb(reg
);
39 case AC_VERB_GET_PROC_COEF
:
40 case AC_VERB_GET_COEF_INDEX
:
41 case AC_VERB_GET_PROC_STATE
:
42 case AC_VERB_GET_POWER_STATE
:
43 case AC_VERB_GET_PIN_SENSE
:
44 case AC_VERB_GET_HDMI_DIP_SIZE
:
45 case AC_VERB_GET_HDMI_ELDD
:
46 case AC_VERB_GET_HDMI_DIP_INDEX
:
47 case AC_VERB_GET_HDMI_DIP_DATA
:
48 case AC_VERB_GET_HDMI_DIP_XMIT
:
49 case AC_VERB_GET_HDMI_CP_CTRL
:
50 case AC_VERB_GET_HDMI_CHAN_SLOT
:
51 case AC_VERB_GET_DEVICE_SEL
:
52 case AC_VERB_GET_DEVICE_LIST
: /* read-only volatile */
59 static bool hda_writeable_reg(struct device
*dev
, unsigned int reg
)
61 struct hdac_device
*codec
= dev_to_hdac_dev(dev
);
62 unsigned int verb
= get_verb(reg
);
65 for (i
= 0; i
< codec
->vendor_verbs
.used
; i
++) {
66 unsigned int *v
= snd_array_elem(&codec
->vendor_verbs
, i
);
71 if (codec
->caps_overwriting
)
74 switch (verb
& 0xf00) {
75 case AC_VERB_GET_STREAM_FORMAT
:
76 case AC_VERB_GET_AMP_GAIN_MUTE
:
85 case AC_VERB_GET_CONNECT_SEL
:
86 case AC_VERB_GET_SDI_SELECT
:
87 case AC_VERB_GET_CONV
:
88 case AC_VERB_GET_PIN_WIDGET_CONTROL
:
89 case AC_VERB_GET_UNSOLICITED_RESPONSE
: /* only as SET_UNSOLICITED_ENABLE */
90 case AC_VERB_GET_BEEP_CONTROL
:
91 case AC_VERB_GET_EAPD_BTLENABLE
:
92 case AC_VERB_GET_DIGI_CONVERT_1
:
93 case AC_VERB_GET_DIGI_CONVERT_2
: /* only for beep control */
94 case AC_VERB_GET_VOLUME_KNOB_CONTROL
:
95 case AC_VERB_GET_CONFIG_DEFAULT
:
96 case AC_VERB_GET_GPIO_MASK
:
97 case AC_VERB_GET_GPIO_DIRECTION
:
98 case AC_VERB_GET_GPIO_DATA
: /* not for volatile read */
99 case AC_VERB_GET_GPIO_WAKE_MASK
:
100 case AC_VERB_GET_GPIO_UNSOLICITED_RSP_MASK
:
101 case AC_VERB_GET_GPIO_STICKY_MASK
:
102 case AC_VERB_GET_CVT_CHAN_COUNT
:
109 static bool hda_readable_reg(struct device
*dev
, unsigned int reg
)
111 struct hdac_device
*codec
= dev_to_hdac_dev(dev
);
112 unsigned int verb
= get_verb(reg
);
114 if (codec
->caps_overwriting
)
118 case AC_VERB_PARAMETERS
:
119 case AC_VERB_GET_CONNECT_LIST
:
120 case AC_VERB_GET_SUBSYSTEM_ID
:
124 return hda_writeable_reg(dev
, reg
);
127 static int hda_reg_read(void *context
, unsigned int reg
, unsigned int *val
)
129 struct hdac_device
*codec
= context
;
131 if (!codec_is_running(codec
))
133 reg
|= (codec
->addr
<< 28);
134 return snd_hdac_exec_verb(codec
, reg
, 0, val
);
137 static int hda_reg_write(void *context
, unsigned int reg
, unsigned int val
)
139 struct hdac_device
*codec
= context
;
143 if (!codec_is_running(codec
))
144 return codec
->lazy_cache
? 0 : -EAGAIN
;
146 reg
&= ~0x00080000U
; /* drop GET bit */
147 reg
|= (codec
->addr
<< 28);
148 verb
= get_verb(reg
);
150 switch (verb
& 0xf00) {
151 case AC_VERB_SET_AMP_GAIN_MUTE
:
152 verb
= AC_VERB_SET_AMP_GAIN_MUTE
;
153 if (reg
& AC_AMP_GET_LEFT
)
154 verb
|= AC_AMP_SET_LEFT
>> 8;
156 verb
|= AC_AMP_SET_RIGHT
>> 8;
157 if (reg
& AC_AMP_GET_OUTPUT
) {
158 verb
|= AC_AMP_SET_OUTPUT
>> 8;
160 verb
|= AC_AMP_SET_INPUT
>> 8;
167 case AC_VERB_SET_DIGI_CONVERT_1
:
170 case AC_VERB_SET_CONFIG_DEFAULT_BYTES_0
:
178 for (i
= 0; i
< bytes
; i
++) {
180 reg
|= (verb
+ i
) << 8 | ((val
>> (8 * i
)) & 0xff);
181 err
= snd_hdac_exec_verb(codec
, reg
, 0, NULL
);
189 static const struct regmap_config hda_regmap_cfg
= {
193 .max_register
= 0xfffffff,
194 .writeable_reg
= hda_writeable_reg
,
195 .readable_reg
= hda_readable_reg
,
196 .volatile_reg
= hda_volatile_reg
,
197 .cache_type
= REGCACHE_RBTREE
,
198 .reg_read
= hda_reg_read
,
199 .reg_write
= hda_reg_write
,
202 int snd_hdac_regmap_init(struct hdac_device
*codec
)
204 struct regmap
*regmap
;
206 regmap
= regmap_init(&codec
->dev
, NULL
, codec
, &hda_regmap_cfg
);
208 return PTR_ERR(regmap
);
209 codec
->regmap
= regmap
;
210 snd_array_init(&codec
->vendor_verbs
, sizeof(unsigned int), 8);
213 EXPORT_SYMBOL_GPL(snd_hdac_regmap_init
);
215 void snd_hdac_regmap_exit(struct hdac_device
*codec
)
218 regmap_exit(codec
->regmap
);
219 codec
->regmap
= NULL
;
220 snd_array_free(&codec
->vendor_verbs
);
223 EXPORT_SYMBOL_GPL(snd_hdac_regmap_exit
);
226 * snd_hdac_regmap_add_vendor_verb - add a vendor-specific verb to regmap
227 * @codec: the codec object
228 * @verb: verb to allow accessing via regmap
230 * Returns zero for success or a negative error code.
232 int snd_hdac_regmap_add_vendor_verb(struct hdac_device
*codec
,
235 unsigned int *p
= snd_array_new(&codec
->vendor_verbs
);
242 EXPORT_SYMBOL_GPL(snd_hdac_regmap_add_vendor_verb
);
248 /* write a pseudo-register value (w/o power sequence) */
249 static int reg_raw_write(struct hdac_device
*codec
, unsigned int reg
,
253 return hda_reg_write(codec
, reg
, val
);
255 return regmap_write(codec
->regmap
, reg
, val
);
259 * snd_hdac_regmap_write_raw - write a pseudo register with power mgmt
260 * @codec: the codec object
261 * @reg: pseudo register
262 * @val: value to write
264 * Returns zero if successful or a negative error code.
266 int snd_hdac_regmap_write_raw(struct hdac_device
*codec
, unsigned int reg
,
271 err
= reg_raw_write(codec
, reg
, val
);
272 if (err
== -EAGAIN
) {
273 snd_hdac_power_up(codec
);
274 err
= reg_raw_write(codec
, reg
, val
);
275 snd_hdac_power_down(codec
);
279 EXPORT_SYMBOL_GPL(snd_hdac_regmap_write_raw
);
281 static int reg_raw_read(struct hdac_device
*codec
, unsigned int reg
,
285 return hda_reg_read(codec
, reg
, val
);
287 return regmap_read(codec
->regmap
, reg
, val
);
291 * snd_hdac_regmap_read_raw - read a pseudo register with power mgmt
292 * @codec: the codec object
293 * @reg: pseudo register
294 * @val: pointer to store the read value
296 * Returns zero if successful or a negative error code.
298 int snd_hdac_regmap_read_raw(struct hdac_device
*codec
, unsigned int reg
,
303 err
= reg_raw_read(codec
, reg
, val
);
304 if (err
== -EAGAIN
) {
305 snd_hdac_power_up(codec
);
306 err
= reg_raw_read(codec
, reg
, val
);
307 snd_hdac_power_down(codec
);
311 EXPORT_SYMBOL_GPL(snd_hdac_regmap_read_raw
);
314 * snd_hdac_regmap_update_raw - update a pseudo register with power mgmt
315 * @codec: the codec object
316 * @reg: pseudo register
317 * @mask: bit mask to udpate
318 * @val: value to update
320 * Returns zero if successful or a negative error code.
322 int snd_hdac_regmap_update_raw(struct hdac_device
*codec
, unsigned int reg
,
323 unsigned int mask
, unsigned int val
)
329 err
= snd_hdac_regmap_read_raw(codec
, reg
, &orig
);
335 err
= snd_hdac_regmap_write_raw(codec
, reg
, val
);
340 EXPORT_SYMBOL_GPL(snd_hdac_regmap_update_raw
);