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 unsigned int verb
= get_verb(reg
);
63 switch (verb
& 0xf00) {
64 case AC_VERB_GET_STREAM_FORMAT
:
65 case AC_VERB_GET_AMP_GAIN_MUTE
:
74 case AC_VERB_GET_CONNECT_SEL
:
75 case AC_VERB_GET_SDI_SELECT
:
76 case AC_VERB_GET_CONV
:
77 case AC_VERB_GET_PIN_WIDGET_CONTROL
:
78 case AC_VERB_GET_UNSOLICITED_RESPONSE
: /* only as SET_UNSOLICITED_ENABLE */
79 case AC_VERB_GET_BEEP_CONTROL
:
80 case AC_VERB_GET_EAPD_BTLENABLE
:
81 case AC_VERB_GET_DIGI_CONVERT_1
:
82 case AC_VERB_GET_DIGI_CONVERT_2
: /* only for beep control */
83 case AC_VERB_GET_VOLUME_KNOB_CONTROL
:
84 case AC_VERB_GET_CONFIG_DEFAULT
:
85 case AC_VERB_GET_GPIO_MASK
:
86 case AC_VERB_GET_GPIO_DIRECTION
:
87 case AC_VERB_GET_GPIO_DATA
: /* not for volatile read */
88 case AC_VERB_GET_GPIO_WAKE_MASK
:
89 case AC_VERB_GET_GPIO_UNSOLICITED_RSP_MASK
:
90 case AC_VERB_GET_GPIO_STICKY_MASK
:
91 case AC_VERB_GET_CVT_CHAN_COUNT
:
98 static bool hda_readable_reg(struct device
*dev
, unsigned int reg
)
100 unsigned int verb
= get_verb(reg
);
103 case AC_VERB_PARAMETERS
:
104 case AC_VERB_GET_CONNECT_LIST
:
105 case AC_VERB_GET_SUBSYSTEM_ID
:
109 return hda_writeable_reg(dev
, reg
);
112 static int hda_reg_read(void *context
, unsigned int reg
, unsigned int *val
)
114 struct hdac_device
*codec
= context
;
116 if (!codec_is_running(codec
))
118 reg
|= (codec
->addr
<< 28);
119 return snd_hdac_exec_verb(codec
, reg
, 0, val
);
122 static int hda_reg_write(void *context
, unsigned int reg
, unsigned int val
)
124 struct hdac_device
*codec
= context
;
128 if (!codec_is_running(codec
))
129 return codec
->lazy_cache
? 0 : -EAGAIN
;
131 reg
&= ~0x00080000U
; /* drop GET bit */
132 reg
|= (codec
->addr
<< 28);
133 verb
= get_verb(reg
);
135 switch (verb
& 0xf00) {
136 case AC_VERB_SET_AMP_GAIN_MUTE
:
137 verb
= AC_VERB_SET_AMP_GAIN_MUTE
;
138 if (reg
& AC_AMP_GET_LEFT
)
139 verb
|= AC_AMP_SET_LEFT
>> 8;
141 verb
|= AC_AMP_SET_RIGHT
>> 8;
142 if (reg
& AC_AMP_GET_OUTPUT
) {
143 verb
|= AC_AMP_SET_OUTPUT
>> 8;
145 verb
|= AC_AMP_SET_INPUT
>> 8;
152 case AC_VERB_SET_DIGI_CONVERT_1
:
155 case AC_VERB_SET_CONFIG_DEFAULT_BYTES_0
:
163 for (i
= 0; i
< bytes
; i
++) {
165 reg
|= (verb
+ i
) << 8 | ((val
>> (8 * i
)) & 0xff);
166 err
= snd_hdac_exec_verb(codec
, reg
, 0, NULL
);
174 static const struct regmap_config hda_regmap_cfg
= {
178 .max_register
= 0xfffffff,
179 .writeable_reg
= hda_writeable_reg
,
180 .readable_reg
= hda_readable_reg
,
181 .volatile_reg
= hda_volatile_reg
,
182 .cache_type
= REGCACHE_RBTREE
,
183 .reg_read
= hda_reg_read
,
184 .reg_write
= hda_reg_write
,
187 int snd_hdac_regmap_init(struct hdac_device
*codec
)
189 struct regmap
*regmap
;
191 regmap
= regmap_init(&codec
->dev
, NULL
, codec
, &hda_regmap_cfg
);
193 return PTR_ERR(regmap
);
194 codec
->regmap
= regmap
;
197 EXPORT_SYMBOL_GPL(snd_hdac_regmap_init
);
199 void snd_hdac_regmap_exit(struct hdac_device
*codec
)
202 regmap_exit(codec
->regmap
);
203 codec
->regmap
= NULL
;
206 EXPORT_SYMBOL_GPL(snd_hdac_regmap_exit
);
212 /* write a pseudo-register value (w/o power sequence) */
213 static int reg_raw_write(struct hdac_device
*codec
, unsigned int reg
,
217 return hda_reg_write(codec
, reg
, val
);
219 return regmap_write(codec
->regmap
, reg
, val
);
223 * snd_hdac_regmap_write_raw - write a pseudo register with power mgmt
224 * @codec: the codec object
225 * @reg: pseudo register
226 * @val: value to write
228 * Returns zero if successful or a negative error code.
230 int snd_hdac_regmap_write_raw(struct hdac_device
*codec
, unsigned int reg
,
235 err
= reg_raw_write(codec
, reg
, val
);
236 if (err
== -EAGAIN
) {
237 snd_hdac_power_up(codec
);
238 err
= reg_raw_write(codec
, reg
, val
);
239 snd_hdac_power_down(codec
);
243 EXPORT_SYMBOL_GPL(snd_hdac_regmap_write_raw
);
245 static int reg_raw_read(struct hdac_device
*codec
, unsigned int reg
,
249 return hda_reg_read(codec
, reg
, val
);
251 return regmap_read(codec
->regmap
, reg
, val
);
255 * snd_hdac_regmap_read_raw - read a pseudo register with power mgmt
256 * @codec: the codec object
257 * @reg: pseudo register
258 * @val: pointer to store the read value
260 * Returns zero if successful or a negative error code.
262 int snd_hdac_regmap_read_raw(struct hdac_device
*codec
, unsigned int reg
,
267 err
= reg_raw_read(codec
, reg
, val
);
268 if (err
== -EAGAIN
) {
269 snd_hdac_power_up(codec
);
270 err
= reg_raw_read(codec
, reg
, val
);
271 snd_hdac_power_down(codec
);
275 EXPORT_SYMBOL_GPL(snd_hdac_regmap_read_raw
);
278 * snd_hdac_regmap_update_raw - update a pseudo register with power mgmt
279 * @codec: the codec object
280 * @reg: pseudo register
281 * @mask: bit mask to udpate
282 * @val: value to update
284 * Returns zero if successful or a negative error code.
286 int snd_hdac_regmap_update_raw(struct hdac_device
*codec
, unsigned int reg
,
287 unsigned int mask
, unsigned int val
)
293 err
= snd_hdac_regmap_read_raw(codec
, reg
, &orig
);
299 err
= snd_hdac_regmap_write_raw(codec
, reg
, val
);
304 EXPORT_SYMBOL_GPL(snd_hdac_regmap_update_raw
);