2 * soc-io.c -- ASoC register I/O helpers
4 * Copyright 2009-2011 Wolfson Microelectronics PLC.
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
14 #include <linux/i2c.h>
15 #include <linux/spi/spi.h>
16 #include <linux/regmap.h>
17 #include <linux/export.h>
18 #include <sound/soc.h>
20 #include <trace/events/asoc.h>
23 static int hw_write(struct snd_soc_codec *codec, unsigned int reg,
28 if (!snd_soc_codec_volatile_register(codec, reg) &&
29 reg < codec->driver->reg_cache_size &&
30 !codec->cache_bypass) {
31 ret = snd_soc_cache_write(codec, reg, value);
36 if (codec->cache_only) {
37 codec->cache_sync = 1;
41 return regmap_write(codec->control_data, reg, value);
44 static unsigned int hw_read(struct snd_soc_codec *codec, unsigned int reg)
49 if (reg >= codec->driver->reg_cache_size ||
50 snd_soc_codec_volatile_register(codec, reg) ||
51 codec->cache_bypass) {
52 if (codec->cache_only)
55 ret = regmap_read(codec->control_data, reg, &val);
62 ret = snd_soc_cache_read(codec, reg, &val);
69 * snd_soc_codec_set_cache_io: Set up standard I/O functions.
71 * @codec: CODEC to configure.
72 * @addr_bits: Number of bits of register address data.
73 * @data_bits: Number of bits of data per register.
74 * @control: Control bus used.
76 * Register formats are frequently shared between many I2C and SPI
77 * devices. In order to promote code reuse the ASoC core provides
78 * some standard implementations of CODEC read and write operations
79 * which can be set up using this function.
81 * The caller is responsible for allocating and initialising the
84 * Note that at present this code cannot be used by CODECs with
87 int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
88 int addr_bits, int data_bits,
89 enum snd_soc_control_type control)
91 struct regmap_config config;
94 memset(&config, 0, sizeof(config));
95 codec->write = hw_write;
96 codec->read = hw_read;
98 config.reg_bits = addr_bits;
99 config.val_bits = data_bits;
102 #if IS_ENABLED(CONFIG_REGMAP_I2C)
104 codec->control_data = regmap_init_i2c(to_i2c_client(codec->dev),
109 #if IS_ENABLED(CONFIG_REGMAP_SPI)
111 codec->control_data = regmap_init_spi(to_spi_device(codec->dev),
117 /* Device has made its own regmap arrangements */
118 codec->using_regmap = true;
119 if (!codec->control_data)
120 codec->control_data = dev_get_regmap(codec->dev, NULL);
122 if (codec->control_data) {
123 ret = regmap_get_val_bytes(codec->control_data);
124 /* Errors are legitimate for non-integer byte
127 codec->val_bytes = ret;
135 return PTR_ERR_OR_ZERO(codec->control_data);
137 EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io);
139 int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
140 int addr_bits, int data_bits,
141 enum snd_soc_control_type control)
145 EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io);