aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTakashi Iwai <tiwai@suse.de>2015-02-25 08:42:38 -0500
committerTakashi Iwai <tiwai@suse.de>2015-03-23 08:19:36 -0400
commit4d75faa0448a6bb2fd6d56051a3675a3937cbada (patch)
tree3cb2a3fbf131cf7d8eb8eba866490548d9ea6c1a
parent71fc4c7ef5ef2d0ddd22f0545ede4c135b554b84 (diff)
ALSA: hda - Add regmap support
This patch adds an infrastructure to support regmap-based verb accesses. Because o the asymmetric nature of HD-audio verbs, especially the amp verbs, we need to translate the verbs as a sort of pseudo registers to be mapped uniquely in regmap. In this patch, a pseudo register is built from the NID, the AC_VERB_GET_* and 8bit parameters, i.e. almost in the form to be sent to HD-audio bus but without codec address field. OTOH, for writing, the same pseudo register is translated to AC_VERB_SET_* automatically. The AC_VERB_SET_AMP_* verb is re-encoded from the corresponding AC_VERB_GET_AMP_* verb and parameter at writing. Some verbs has a single command for read but multiple for writes. A write for such a verb is split automatically to multiple verbs. The patch provides also a few handy helper functions. They are designed to be accessible even without regmap. When no regmap is set up (e.g. before the codec device instantiation), the direct hardware access is used. Also, it tries to avoid the unnecessary power-up. The power up/down sequence is performed only on demand. The codec driver needs to call snd_hdac_regmap_exit() and snd_hdac_regmap_exit() at probe and remove if it wants the regmap access. There is one flag added to hdac_device. When the flag lazy_cache is set, regmap helper ignores a write for a suspended device and returns as if it was actually written. It reduces the hardware access pretty much, e.g. when adjusting the mixer volume while in idle. This assumes that the driver will sync the cache later at resume properly, so use it carefully. Signed-off-by: Takashi Iwai <tiwai@suse.de>
-rw-r--r--include/sound/hda_regmap.h145
-rw-r--r--include/sound/hdaudio.h4
-rw-r--r--sound/hda/Kconfig1
-rw-r--r--sound/hda/Makefile2
-rw-r--r--sound/hda/hdac_regmap.c304
-rw-r--r--sound/pci/hda/hda_bind.c4
-rw-r--r--sound/pci/hda/hda_codec.c1
-rw-r--r--sound/pci/hda/hda_codec.h1
8 files changed, 461 insertions, 1 deletions
diff --git a/include/sound/hda_regmap.h b/include/sound/hda_regmap.h
new file mode 100644
index 000000000000..95651d26437d
--- /dev/null
+++ b/include/sound/hda_regmap.h
@@ -0,0 +1,145 @@
1/*
2 * HD-audio regmap helpers
3 */
4
5#ifndef __SOUND_HDA_REGMAP_H
6#define __SOUND_HDA_REGMAP_H
7
8#include <linux/regmap.h>
9#include <sound/core.h>
10#include <sound/hdaudio.h>
11
12int snd_hdac_regmap_init(struct hdac_device *codec);
13void snd_hdac_regmap_exit(struct hdac_device *codec);
14
15int snd_hdac_regmap_read_raw(struct hdac_device *codec, unsigned int reg,
16 unsigned int *val);
17int snd_hdac_regmap_write_raw(struct hdac_device *codec, unsigned int reg,
18 unsigned int val);
19int snd_hdac_regmap_update_raw(struct hdac_device *codec, unsigned int reg,
20 unsigned int mask, unsigned int val);
21
22/**
23 * snd_hdac_regmap_encode_verb - encode the verb to a pseudo register
24 * @nid: widget NID
25 * @verb: codec verb
26 *
27 * Returns an encoded pseudo register.
28 */
29#define snd_hdac_regmap_encode_verb(nid, verb) \
30 (((verb) << 8) | 0x80000 | ((unsigned int)(nid) << 20))
31
32/**
33 * snd_hdac_regmap_encode_amp - encode the AMP verb to a pseudo register
34 * @nid: widget NID
35 * @ch: channel (left = 0, right = 1)
36 * @dir: direction (#HDA_INPUT, #HDA_OUTPUT)
37 * @idx: input index value
38 *
39 * Returns an encoded pseudo register.
40 */
41#define snd_hdac_regmap_encode_amp(nid, ch, dir, idx) \
42 (snd_hdac_regmap_encode_verb(nid, AC_VERB_GET_AMP_GAIN_MUTE) | \
43 ((ch) ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT) | \
44 ((dir) == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT) | \
45 (idx))
46
47/**
48 * snd_hdac_regmap_write - Write a verb with caching
49 * @nid: codec NID
50 * @reg: verb to write
51 * @val: value to write
52 *
53 * For writing an amp value, use snd_hda_regmap_amp_update().
54 */
55static inline int
56snd_hdac_regmap_write(struct hdac_device *codec, hda_nid_t nid,
57 unsigned int verb, unsigned int val)
58{
59 unsigned int cmd = snd_hdac_regmap_encode_verb(nid, verb);
60
61 return snd_hdac_regmap_write_raw(codec, cmd, val);
62}
63
64/**
65 * snd_hda_regmap_update - Update a verb value with caching
66 * @nid: codec NID
67 * @verb: verb to update
68 * @mask: bit mask to update
69 * @val: value to update
70 *
71 * For updating an amp value, use snd_hda_regmap_amp_update().
72 */
73static inline int
74snd_hdac_regmap_update(struct hdac_device *codec, hda_nid_t nid,
75 unsigned int verb, unsigned int mask,
76 unsigned int val)
77{
78 unsigned int cmd = snd_hdac_regmap_encode_verb(nid, verb);
79
80 return snd_hdac_regmap_update_raw(codec, cmd, mask, val);
81}
82
83/**
84 * snd_hda_regmap_read - Read a verb with caching
85 * @nid: codec NID
86 * @verb: verb to read
87 * @val: pointer to store the value
88 *
89 * For reading an amp value, use snd_hda_regmap_get_amp().
90 */
91static inline int
92snd_hdac_regmap_read(struct hdac_device *codec, hda_nid_t nid,
93 unsigned int verb, unsigned int *val)
94{
95 unsigned int cmd = snd_hdac_regmap_encode_verb(nid, verb);
96
97 return snd_hdac_regmap_read_raw(codec, cmd, val);
98}
99
100/**
101 * snd_hdac_regmap_get_amp - Read AMP value
102 * @codec: HD-audio codec
103 * @nid: NID to read the AMP value
104 * @ch: channel (left=0 or right=1)
105 * @direction: #HDA_INPUT or #HDA_OUTPUT
106 * @index: the index value (only for input direction)
107 * @val: the pointer to store the value
108 *
109 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
110 * Returns the value or a negative error.
111 */
112static inline int
113snd_hdac_regmap_get_amp(struct hdac_device *codec, hda_nid_t nid,
114 int ch, int dir, int idx)
115{
116 unsigned int cmd = snd_hdac_regmap_encode_amp(nid, ch, dir, idx);
117 int err, val;
118
119 err = snd_hdac_regmap_read_raw(codec, cmd, &val);
120 return err < 0 ? err : val;
121}
122
123/**
124 * snd_hdac_regmap_update_amp - update the AMP value
125 * @codec: HD-audio codec
126 * @nid: NID to read the AMP value
127 * @ch: channel (left=0 or right=1)
128 * @direction: #HDA_INPUT or #HDA_OUTPUT
129 * @idx: the index value (only for input direction)
130 * @mask: bit mask to set
131 * @val: the bits value to set
132 *
133 * Update the AMP value with a bit mask.
134 * Returns 0 if the value is unchanged, 1 if changed, or a negative error.
135 */
136static inline int
137snd_hdac_regmap_update_amp(struct hdac_device *codec, hda_nid_t nid,
138 int ch, int dir, int idx, int mask, int val)
139{
140 unsigned int cmd = snd_hdac_regmap_encode_amp(nid, ch, dir, idx);
141
142 return snd_hdac_regmap_update_raw(codec, cmd, mask, val);
143}
144
145#endif /* __SOUND_HDA_REGMAP_H */
diff --git a/include/sound/hdaudio.h b/include/sound/hdaudio.h
index 3abdd3f16528..47e20b741c51 100644
--- a/include/sound/hdaudio.h
+++ b/include/sound/hdaudio.h
@@ -72,6 +72,10 @@ struct hdac_device {
72 72
73 /* sysfs */ 73 /* sysfs */
74 struct hdac_widget_tree *widgets; 74 struct hdac_widget_tree *widgets;
75
76 /* regmap */
77 struct regmap *regmap;
78 bool lazy_cache:1; /* don't wake up for writes */
75}; 79};
76 80
77/* device/driver type used for matching */ 81/* device/driver type used for matching */
diff --git a/sound/hda/Kconfig b/sound/hda/Kconfig
index 4f428ccf64ad..001c6588a5ff 100644
--- a/sound/hda/Kconfig
+++ b/sound/hda/Kconfig
@@ -1,2 +1,3 @@
1config SND_HDA_CORE 1config SND_HDA_CORE
2 tristate 2 tristate
3 select REGMAP
diff --git a/sound/hda/Makefile b/sound/hda/Makefile
index e508ba1102cb..7a359f5b7e25 100644
--- a/sound/hda/Makefile
+++ b/sound/hda/Makefile
@@ -1,5 +1,5 @@
1snd-hda-core-objs := hda_bus_type.o hdac_bus.o hdac_device.o hdac_sysfs.o \ 1snd-hda-core-objs := hda_bus_type.o hdac_bus.o hdac_device.o hdac_sysfs.o \
2 array.o 2 hdac_regmap.o array.o
3 3
4snd-hda-core-objs += trace.o 4snd-hda-core-objs += trace.o
5CFLAGS_trace.o := -I$(src) 5CFLAGS_trace.o := -I$(src)
diff --git a/sound/hda/hdac_regmap.c b/sound/hda/hdac_regmap.c
new file mode 100644
index 000000000000..db03d60d9c99
--- /dev/null
+++ b/sound/hda/hdac_regmap.c
@@ -0,0 +1,304 @@
1/*
2 * Regmap support for HD-audio verbs
3 *
4 * A virtual register is translated to one or more hda verbs for write,
5 * vice versa for read.
6 *
7 * A few limitations:
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
12 */
13
14#include <linux/slab.h>
15#include <linux/device.h>
16#include <linux/regmap.h>
17#include <linux/export.h>
18#include <linux/pm.h>
19#include <linux/pm_runtime.h>
20#include <sound/core.h>
21#include <sound/hdaudio.h>
22#include <sound/hda_regmap.h>
23
24#ifdef CONFIG_PM
25#define codec_is_running(codec) \
26 (atomic_read(&(codec)->in_pm) || \
27 !pm_runtime_suspended(&(codec)->dev))
28#else
29#define codec_is_running(codec) true
30#endif
31
32#define get_verb(reg) (((reg) >> 8) & 0xfff)
33
34static bool hda_volatile_reg(struct device *dev, unsigned int reg)
35{
36 unsigned int verb = get_verb(reg);
37
38 switch (verb) {
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 */
53 return true;
54 }
55
56 return false;
57}
58
59static bool hda_writeable_reg(struct device *dev, unsigned int reg)
60{
61 unsigned int verb = get_verb(reg);
62
63 switch (verb & 0xf00) {
64 case AC_VERB_GET_STREAM_FORMAT:
65 case AC_VERB_GET_AMP_GAIN_MUTE:
66 return true;
67 case 0xf00:
68 break;
69 default:
70 return false;
71 }
72
73 switch (verb) {
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:
92 return true;
93 }
94
95 return false;
96}
97
98static bool hda_readable_reg(struct device *dev, unsigned int reg)
99{
100 unsigned int verb = get_verb(reg);
101
102 switch (verb) {
103 case AC_VERB_PARAMETERS:
104 case AC_VERB_GET_CONNECT_LIST:
105 case AC_VERB_GET_SUBSYSTEM_ID:
106 return true;
107 }
108
109 return hda_writeable_reg(dev, reg);
110}
111
112static int hda_reg_read(void *context, unsigned int reg, unsigned int *val)
113{
114 struct hdac_device *codec = context;
115
116 if (!codec_is_running(codec))
117 return -EAGAIN;
118 reg |= (codec->addr << 28);
119 return snd_hdac_exec_verb(codec, reg, 0, val);
120}
121
122static int hda_reg_write(void *context, unsigned int reg, unsigned int val)
123{
124 struct hdac_device *codec = context;
125 unsigned int verb;
126 int i, bytes, err;
127
128 if (!codec_is_running(codec))
129 return codec->lazy_cache ? 0 : -EAGAIN;
130
131 reg &= ~0x00080000U; /* drop GET bit */
132 reg |= (codec->addr << 28);
133 verb = get_verb(reg);
134
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;
140 else
141 verb |= AC_AMP_SET_RIGHT >> 8;
142 if (reg & AC_AMP_GET_OUTPUT) {
143 verb |= AC_AMP_SET_OUTPUT >> 8;
144 } else {
145 verb |= AC_AMP_SET_INPUT >> 8;
146 verb |= reg & 0xf;
147 }
148 break;
149 }
150
151 switch (verb) {
152 case AC_VERB_SET_DIGI_CONVERT_1:
153 bytes = 2;
154 break;
155 case AC_VERB_SET_CONFIG_DEFAULT_BYTES_0:
156 bytes = 4;
157 break;
158 default:
159 bytes = 1;
160 break;
161 }
162
163 for (i = 0; i < bytes; i++) {
164 reg &= ~0xfffff;
165 reg |= (verb + i) << 8 | ((val >> (8 * i)) & 0xff);
166 err = snd_hdac_exec_verb(codec, reg, 0, NULL);
167 if (err < 0)
168 return err;
169 }
170
171 return 0;
172}
173
174static const struct regmap_config hda_regmap_cfg = {
175 .name = "hdaudio",
176 .reg_bits = 32,
177 .val_bits = 32,
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,
185};
186
187int snd_hdac_regmap_init(struct hdac_device *codec)
188{
189 struct regmap *regmap;
190
191 regmap = regmap_init(&codec->dev, NULL, codec, &hda_regmap_cfg);
192 if (IS_ERR(regmap))
193 return PTR_ERR(regmap);
194 codec->regmap = regmap;
195 return 0;
196}
197EXPORT_SYMBOL_GPL(snd_hdac_regmap_init);
198
199void snd_hdac_regmap_exit(struct hdac_device *codec)
200{
201 if (codec->regmap) {
202 regmap_exit(codec->regmap);
203 codec->regmap = NULL;
204 }
205}
206EXPORT_SYMBOL_GPL(snd_hdac_regmap_exit);
207
208/*
209 * helper functions
210 */
211
212/* write a pseudo-register value (w/o power sequence) */
213static int reg_raw_write(struct hdac_device *codec, unsigned int reg,
214 unsigned int val)
215{
216 if (!codec->regmap)
217 return hda_reg_write(codec, reg, val);
218 else
219 return regmap_write(codec->regmap, reg, val);
220}
221
222/**
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
227 *
228 * Returns zero if successful or a negative error code.
229 */
230int snd_hdac_regmap_write_raw(struct hdac_device *codec, unsigned int reg,
231 unsigned int val)
232{
233 int err;
234
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);
240 }
241 return err;
242}
243EXPORT_SYMBOL_GPL(snd_hdac_regmap_write_raw);
244
245static int reg_raw_read(struct hdac_device *codec, unsigned int reg,
246 unsigned int *val)
247{
248 if (!codec->regmap)
249 return hda_reg_read(codec, reg, val);
250 else
251 return regmap_read(codec->regmap, reg, val);
252}
253
254/**
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
259 *
260 * Returns zero if successful or a negative error code.
261 */
262int snd_hdac_regmap_read_raw(struct hdac_device *codec, unsigned int reg,
263 unsigned int *val)
264{
265 int err;
266
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);
272 }
273 return err;
274}
275EXPORT_SYMBOL_GPL(snd_hdac_regmap_read_raw);
276
277/**
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
283 *
284 * Returns zero if successful or a negative error code.
285 */
286int snd_hdac_regmap_update_raw(struct hdac_device *codec, unsigned int reg,
287 unsigned int mask, unsigned int val)
288{
289 unsigned int orig;
290 int err;
291
292 val &= mask;
293 err = snd_hdac_regmap_read_raw(codec, reg, &orig);
294 if (err < 0)
295 return err;
296 val |= orig & ~mask;
297 if (val == orig)
298 return 0;
299 err = snd_hdac_regmap_write_raw(codec, reg, val);
300 if (err < 0)
301 return err;
302 return 1;
303}
304EXPORT_SYMBOL_GPL(snd_hdac_regmap_update_raw);
diff --git a/sound/pci/hda/hda_bind.c b/sound/pci/hda/hda_bind.c
index 7b269c3237e3..00aa31c5f08e 100644
--- a/sound/pci/hda/hda_bind.c
+++ b/sound/pci/hda/hda_bind.c
@@ -75,6 +75,9 @@ static int hda_codec_driver_probe(struct device *dev)
75 err = codec_refresh_name(codec, codec->preset->name); 75 err = codec_refresh_name(codec, codec->preset->name);
76 if (err < 0) 76 if (err < 0)
77 goto error; 77 goto error;
78 err = snd_hdac_regmap_init(&codec->core);
79 if (err < 0)
80 goto error;
78 81
79 if (!try_module_get(owner)) { 82 if (!try_module_get(owner)) {
80 err = -EINVAL; 83 err = -EINVAL;
@@ -98,6 +101,7 @@ static int hda_codec_driver_probe(struct device *dev)
98 snd_hda_codec_register(codec); 101 snd_hda_codec_register(codec);
99 } 102 }
100 103
104 codec->core.lazy_cache = true;
101 return 0; 105 return 0;
102 106
103 error_module: 107 error_module:
diff --git a/sound/pci/hda/hda_codec.c b/sound/pci/hda/hda_codec.c
index 10e257ff9084..8eb42f4226ff 100644
--- a/sound/pci/hda/hda_codec.c
+++ b/sound/pci/hda/hda_codec.c
@@ -945,6 +945,7 @@ void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec)
945 snd_array_free(&codec->mixers); 945 snd_array_free(&codec->mixers);
946 snd_array_free(&codec->nids); 946 snd_array_free(&codec->nids);
947 remove_conn_list(codec); 947 remove_conn_list(codec);
948 snd_hdac_regmap_exit(&codec->core);
948} 949}
949 950
950static unsigned int hda_set_power_state(struct hda_codec *codec, 951static unsigned int hda_set_power_state(struct hda_codec *codec,
diff --git a/sound/pci/hda/hda_codec.h b/sound/pci/hda/hda_codec.h
index 3068163b3db2..0f5749ca1600 100644
--- a/sound/pci/hda/hda_codec.h
+++ b/sound/pci/hda/hda_codec.h
@@ -28,6 +28,7 @@
28#include <sound/hwdep.h> 28#include <sound/hwdep.h>
29#include <sound/hdaudio.h> 29#include <sound/hdaudio.h>
30#include <sound/hda_verbs.h> 30#include <sound/hda_verbs.h>
31#include <sound/hda_regmap.h>
31 32
32/* 33/*
33 * Structures 34 * Structures