i2c: qcom-geni: Provide support for ACPI
[linux/fpc-iii.git] / sound / hda / hdac_regmap.c
blobf399a1552e730e2849b2a39e9029fdd08b5205f2
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Regmap support for HD-audio verbs
5 * A virtual register is translated to one or more hda verbs for write,
6 * vice versa for read.
8 * A few limitations:
9 * - Provided for not all verbs but only subset standard non-volatile verbs.
10 * - For reading, only AC_VERB_GET_* variants can be used.
11 * - For writing, mapped to the *corresponding* AC_VERB_SET_* variants,
12 * so can't handle asymmetric verbs for read and write
15 #include <linux/slab.h>
16 #include <linux/device.h>
17 #include <linux/regmap.h>
18 #include <linux/export.h>
19 #include <linux/pm.h>
20 #include <linux/pm_runtime.h>
21 #include <sound/core.h>
22 #include <sound/hdaudio.h>
23 #include <sound/hda_regmap.h>
25 static int codec_pm_lock(struct hdac_device *codec)
27 return snd_hdac_keep_power_up(codec);
30 static void codec_pm_unlock(struct hdac_device *codec, int lock)
32 if (lock == 1)
33 snd_hdac_power_down_pm(codec);
36 #define get_verb(reg) (((reg) >> 8) & 0xfff)
38 static bool hda_volatile_reg(struct device *dev, unsigned int reg)
40 struct hdac_device *codec = dev_to_hdac_dev(dev);
41 unsigned int verb = get_verb(reg);
43 switch (verb) {
44 case AC_VERB_GET_PROC_COEF:
45 return !codec->cache_coef;
46 case AC_VERB_GET_COEF_INDEX:
47 case AC_VERB_GET_PROC_STATE:
48 case AC_VERB_GET_POWER_STATE:
49 case AC_VERB_GET_PIN_SENSE:
50 case AC_VERB_GET_HDMI_DIP_SIZE:
51 case AC_VERB_GET_HDMI_ELDD:
52 case AC_VERB_GET_HDMI_DIP_INDEX:
53 case AC_VERB_GET_HDMI_DIP_DATA:
54 case AC_VERB_GET_HDMI_DIP_XMIT:
55 case AC_VERB_GET_HDMI_CP_CTRL:
56 case AC_VERB_GET_HDMI_CHAN_SLOT:
57 case AC_VERB_GET_DEVICE_SEL:
58 case AC_VERB_GET_DEVICE_LIST: /* read-only volatile */
59 return true;
62 return false;
65 static bool hda_writeable_reg(struct device *dev, unsigned int reg)
67 struct hdac_device *codec = dev_to_hdac_dev(dev);
68 unsigned int verb = get_verb(reg);
69 const unsigned int *v;
70 int i;
72 snd_array_for_each(&codec->vendor_verbs, i, v) {
73 if (verb == *v)
74 return true;
77 if (codec->caps_overwriting)
78 return true;
80 switch (verb & 0xf00) {
81 case AC_VERB_GET_STREAM_FORMAT:
82 case AC_VERB_GET_AMP_GAIN_MUTE:
83 return true;
84 case AC_VERB_GET_PROC_COEF:
85 return codec->cache_coef;
86 case 0xf00:
87 break;
88 default:
89 return false;
92 switch (verb) {
93 case AC_VERB_GET_CONNECT_SEL:
94 case AC_VERB_GET_SDI_SELECT:
95 case AC_VERB_GET_PIN_WIDGET_CONTROL:
96 case AC_VERB_GET_UNSOLICITED_RESPONSE: /* only as SET_UNSOLICITED_ENABLE */
97 case AC_VERB_GET_BEEP_CONTROL:
98 case AC_VERB_GET_EAPD_BTLENABLE:
99 case AC_VERB_GET_DIGI_CONVERT_1:
100 case AC_VERB_GET_DIGI_CONVERT_2: /* only for beep control */
101 case AC_VERB_GET_VOLUME_KNOB_CONTROL:
102 case AC_VERB_GET_GPIO_MASK:
103 case AC_VERB_GET_GPIO_DIRECTION:
104 case AC_VERB_GET_GPIO_DATA: /* not for volatile read */
105 case AC_VERB_GET_GPIO_WAKE_MASK:
106 case AC_VERB_GET_GPIO_UNSOLICITED_RSP_MASK:
107 case AC_VERB_GET_GPIO_STICKY_MASK:
108 return true;
111 return false;
114 static bool hda_readable_reg(struct device *dev, unsigned int reg)
116 struct hdac_device *codec = dev_to_hdac_dev(dev);
117 unsigned int verb = get_verb(reg);
119 if (codec->caps_overwriting)
120 return true;
122 switch (verb) {
123 case AC_VERB_PARAMETERS:
124 case AC_VERB_GET_CONNECT_LIST:
125 case AC_VERB_GET_SUBSYSTEM_ID:
126 return true;
127 /* below are basically writable, but disabled for reducing unnecessary
128 * writes at sync
130 case AC_VERB_GET_CONFIG_DEFAULT: /* usually just read */
131 case AC_VERB_GET_CONV: /* managed in PCM code */
132 case AC_VERB_GET_CVT_CHAN_COUNT: /* managed in HDMI CA code */
133 return true;
136 return hda_writeable_reg(dev, reg);
140 * Stereo amp pseudo register:
141 * for making easier to handle the stereo volume control, we provide a
142 * fake register to deal both left and right channels by a single
143 * (pseudo) register access. A verb consisting of SET_AMP_GAIN with
144 * *both* SET_LEFT and SET_RIGHT bits takes a 16bit value, the lower 8bit
145 * for the left and the upper 8bit for the right channel.
147 static bool is_stereo_amp_verb(unsigned int reg)
149 if (((reg >> 8) & 0x700) != AC_VERB_SET_AMP_GAIN_MUTE)
150 return false;
151 return (reg & (AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT)) ==
152 (AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT);
155 /* read a pseudo stereo amp register (16bit left+right) */
156 static int hda_reg_read_stereo_amp(struct hdac_device *codec,
157 unsigned int reg, unsigned int *val)
159 unsigned int left, right;
160 int err;
162 reg &= ~(AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT);
163 err = snd_hdac_exec_verb(codec, reg | AC_AMP_GET_LEFT, 0, &left);
164 if (err < 0)
165 return err;
166 err = snd_hdac_exec_verb(codec, reg | AC_AMP_GET_RIGHT, 0, &right);
167 if (err < 0)
168 return err;
169 *val = left | (right << 8);
170 return 0;
173 /* write a pseudo stereo amp register (16bit left+right) */
174 static int hda_reg_write_stereo_amp(struct hdac_device *codec,
175 unsigned int reg, unsigned int val)
177 int err;
178 unsigned int verb, left, right;
180 verb = AC_VERB_SET_AMP_GAIN_MUTE << 8;
181 if (reg & AC_AMP_GET_OUTPUT)
182 verb |= AC_AMP_SET_OUTPUT;
183 else
184 verb |= AC_AMP_SET_INPUT | ((reg & 0xf) << 8);
185 reg = (reg & ~0xfffff) | verb;
187 left = val & 0xff;
188 right = (val >> 8) & 0xff;
189 if (left == right) {
190 reg |= AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT;
191 return snd_hdac_exec_verb(codec, reg | left, 0, NULL);
194 err = snd_hdac_exec_verb(codec, reg | AC_AMP_SET_LEFT | left, 0, NULL);
195 if (err < 0)
196 return err;
197 err = snd_hdac_exec_verb(codec, reg | AC_AMP_SET_RIGHT | right, 0, NULL);
198 if (err < 0)
199 return err;
200 return 0;
203 /* read a pseudo coef register (16bit) */
204 static int hda_reg_read_coef(struct hdac_device *codec, unsigned int reg,
205 unsigned int *val)
207 unsigned int verb;
208 int err;
210 if (!codec->cache_coef)
211 return -EINVAL;
212 /* LSB 8bit = coef index */
213 verb = (reg & ~0xfff00) | (AC_VERB_SET_COEF_INDEX << 8);
214 err = snd_hdac_exec_verb(codec, verb, 0, NULL);
215 if (err < 0)
216 return err;
217 verb = (reg & ~0xfffff) | (AC_VERB_GET_COEF_INDEX << 8);
218 return snd_hdac_exec_verb(codec, verb, 0, val);
221 /* write a pseudo coef register (16bit) */
222 static int hda_reg_write_coef(struct hdac_device *codec, unsigned int reg,
223 unsigned int val)
225 unsigned int verb;
226 int err;
228 if (!codec->cache_coef)
229 return -EINVAL;
230 /* LSB 8bit = coef index */
231 verb = (reg & ~0xfff00) | (AC_VERB_SET_COEF_INDEX << 8);
232 err = snd_hdac_exec_verb(codec, verb, 0, NULL);
233 if (err < 0)
234 return err;
235 verb = (reg & ~0xfffff) | (AC_VERB_GET_COEF_INDEX << 8) |
236 (val & 0xffff);
237 return snd_hdac_exec_verb(codec, verb, 0, NULL);
240 static int hda_reg_read(void *context, unsigned int reg, unsigned int *val)
242 struct hdac_device *codec = context;
243 int verb = get_verb(reg);
244 int err;
245 int pm_lock = 0;
247 if (verb != AC_VERB_GET_POWER_STATE) {
248 pm_lock = codec_pm_lock(codec);
249 if (pm_lock < 0)
250 return -EAGAIN;
252 reg |= (codec->addr << 28);
253 if (is_stereo_amp_verb(reg)) {
254 err = hda_reg_read_stereo_amp(codec, reg, val);
255 goto out;
257 if (verb == AC_VERB_GET_PROC_COEF) {
258 err = hda_reg_read_coef(codec, reg, val);
259 goto out;
261 if ((verb & 0x700) == AC_VERB_SET_AMP_GAIN_MUTE)
262 reg &= ~AC_AMP_FAKE_MUTE;
264 err = snd_hdac_exec_verb(codec, reg, 0, val);
265 if (err < 0)
266 goto out;
267 /* special handling for asymmetric reads */
268 if (verb == AC_VERB_GET_POWER_STATE) {
269 if (*val & AC_PWRST_ERROR)
270 *val = -1;
271 else /* take only the actual state */
272 *val = (*val >> 4) & 0x0f;
274 out:
275 codec_pm_unlock(codec, pm_lock);
276 return err;
279 static int hda_reg_write(void *context, unsigned int reg, unsigned int val)
281 struct hdac_device *codec = context;
282 unsigned int verb;
283 int i, bytes, err;
284 int pm_lock = 0;
286 if (codec->caps_overwriting)
287 return 0;
289 reg &= ~0x00080000U; /* drop GET bit */
290 reg |= (codec->addr << 28);
291 verb = get_verb(reg);
293 if (verb != AC_VERB_SET_POWER_STATE) {
294 pm_lock = codec_pm_lock(codec);
295 if (pm_lock < 0)
296 return codec->lazy_cache ? 0 : -EAGAIN;
299 if (is_stereo_amp_verb(reg)) {
300 err = hda_reg_write_stereo_amp(codec, reg, val);
301 goto out;
304 if (verb == AC_VERB_SET_PROC_COEF) {
305 err = hda_reg_write_coef(codec, reg, val);
306 goto out;
309 switch (verb & 0xf00) {
310 case AC_VERB_SET_AMP_GAIN_MUTE:
311 if ((reg & AC_AMP_FAKE_MUTE) && (val & AC_AMP_MUTE))
312 val = 0;
313 verb = AC_VERB_SET_AMP_GAIN_MUTE;
314 if (reg & AC_AMP_GET_LEFT)
315 verb |= AC_AMP_SET_LEFT >> 8;
316 else
317 verb |= AC_AMP_SET_RIGHT >> 8;
318 if (reg & AC_AMP_GET_OUTPUT) {
319 verb |= AC_AMP_SET_OUTPUT >> 8;
320 } else {
321 verb |= AC_AMP_SET_INPUT >> 8;
322 verb |= reg & 0xf;
324 break;
327 switch (verb) {
328 case AC_VERB_SET_DIGI_CONVERT_1:
329 bytes = 2;
330 break;
331 case AC_VERB_SET_CONFIG_DEFAULT_BYTES_0:
332 bytes = 4;
333 break;
334 default:
335 bytes = 1;
336 break;
339 for (i = 0; i < bytes; i++) {
340 reg &= ~0xfffff;
341 reg |= (verb + i) << 8 | ((val >> (8 * i)) & 0xff);
342 err = snd_hdac_exec_verb(codec, reg, 0, NULL);
343 if (err < 0)
344 goto out;
347 out:
348 codec_pm_unlock(codec, pm_lock);
349 return err;
352 static const struct regmap_config hda_regmap_cfg = {
353 .name = "hdaudio",
354 .reg_bits = 32,
355 .val_bits = 32,
356 .max_register = 0xfffffff,
357 .writeable_reg = hda_writeable_reg,
358 .readable_reg = hda_readable_reg,
359 .volatile_reg = hda_volatile_reg,
360 .cache_type = REGCACHE_RBTREE,
361 .reg_read = hda_reg_read,
362 .reg_write = hda_reg_write,
363 .use_single_read = true,
364 .use_single_write = true,
368 * snd_hdac_regmap_init - Initialize regmap for HDA register accesses
369 * @codec: the codec object
371 * Returns zero for success or a negative error code.
373 int snd_hdac_regmap_init(struct hdac_device *codec)
375 struct regmap *regmap;
377 regmap = regmap_init(&codec->dev, NULL, codec, &hda_regmap_cfg);
378 if (IS_ERR(regmap))
379 return PTR_ERR(regmap);
380 codec->regmap = regmap;
381 snd_array_init(&codec->vendor_verbs, sizeof(unsigned int), 8);
382 return 0;
384 EXPORT_SYMBOL_GPL(snd_hdac_regmap_init);
387 * snd_hdac_regmap_init - Release the regmap from HDA codec
388 * @codec: the codec object
390 void snd_hdac_regmap_exit(struct hdac_device *codec)
392 if (codec->regmap) {
393 regmap_exit(codec->regmap);
394 codec->regmap = NULL;
395 snd_array_free(&codec->vendor_verbs);
398 EXPORT_SYMBOL_GPL(snd_hdac_regmap_exit);
401 * snd_hdac_regmap_add_vendor_verb - add a vendor-specific verb to regmap
402 * @codec: the codec object
403 * @verb: verb to allow accessing via regmap
405 * Returns zero for success or a negative error code.
407 int snd_hdac_regmap_add_vendor_verb(struct hdac_device *codec,
408 unsigned int verb)
410 unsigned int *p = snd_array_new(&codec->vendor_verbs);
412 if (!p)
413 return -ENOMEM;
414 *p = verb | 0x800; /* set GET bit */
415 return 0;
417 EXPORT_SYMBOL_GPL(snd_hdac_regmap_add_vendor_verb);
420 * helper functions
423 /* write a pseudo-register value (w/o power sequence) */
424 static int reg_raw_write(struct hdac_device *codec, unsigned int reg,
425 unsigned int val)
427 if (!codec->regmap)
428 return hda_reg_write(codec, reg, val);
429 else
430 return regmap_write(codec->regmap, reg, val);
434 * snd_hdac_regmap_write_raw - write a pseudo register with power mgmt
435 * @codec: the codec object
436 * @reg: pseudo register
437 * @val: value to write
439 * Returns zero if successful or a negative error code.
441 int snd_hdac_regmap_write_raw(struct hdac_device *codec, unsigned int reg,
442 unsigned int val)
444 int err;
446 err = reg_raw_write(codec, reg, val);
447 if (err == -EAGAIN) {
448 err = snd_hdac_power_up_pm(codec);
449 if (err >= 0)
450 err = reg_raw_write(codec, reg, val);
451 snd_hdac_power_down_pm(codec);
453 return err;
455 EXPORT_SYMBOL_GPL(snd_hdac_regmap_write_raw);
457 static int reg_raw_read(struct hdac_device *codec, unsigned int reg,
458 unsigned int *val, bool uncached)
460 if (uncached || !codec->regmap)
461 return hda_reg_read(codec, reg, val);
462 else
463 return regmap_read(codec->regmap, reg, val);
466 static int __snd_hdac_regmap_read_raw(struct hdac_device *codec,
467 unsigned int reg, unsigned int *val,
468 bool uncached)
470 int err;
472 err = reg_raw_read(codec, reg, val, uncached);
473 if (err == -EAGAIN) {
474 err = snd_hdac_power_up_pm(codec);
475 if (err >= 0)
476 err = reg_raw_read(codec, reg, val, uncached);
477 snd_hdac_power_down_pm(codec);
479 return err;
483 * snd_hdac_regmap_read_raw - read a pseudo register with power mgmt
484 * @codec: the codec object
485 * @reg: pseudo register
486 * @val: pointer to store the read value
488 * Returns zero if successful or a negative error code.
490 int snd_hdac_regmap_read_raw(struct hdac_device *codec, unsigned int reg,
491 unsigned int *val)
493 return __snd_hdac_regmap_read_raw(codec, reg, val, false);
495 EXPORT_SYMBOL_GPL(snd_hdac_regmap_read_raw);
497 /* Works like snd_hdac_regmap_read_raw(), but this doesn't read from the
498 * cache but always via hda verbs.
500 int snd_hdac_regmap_read_raw_uncached(struct hdac_device *codec,
501 unsigned int reg, unsigned int *val)
503 return __snd_hdac_regmap_read_raw(codec, reg, val, true);
507 * snd_hdac_regmap_update_raw - update a pseudo register with power mgmt
508 * @codec: the codec object
509 * @reg: pseudo register
510 * @mask: bit mask to udpate
511 * @val: value to update
513 * Returns zero if successful or a negative error code.
515 int snd_hdac_regmap_update_raw(struct hdac_device *codec, unsigned int reg,
516 unsigned int mask, unsigned int val)
518 unsigned int orig;
519 int err;
521 val &= mask;
522 err = snd_hdac_regmap_read_raw(codec, reg, &orig);
523 if (err < 0)
524 return err;
525 val |= orig & ~mask;
526 if (val == orig)
527 return 0;
528 err = snd_hdac_regmap_write_raw(codec, reg, val);
529 if (err < 0)
530 return err;
531 return 1;
533 EXPORT_SYMBOL_GPL(snd_hdac_regmap_update_raw);