blob: 5fb85783b044eb5893c72f8a61e69b7d4c734844 [file] [log] [blame]
Mark Brown5bef44f2011-06-13 17:49:55 +01001/*
2 * soc-io.c -- ASoC register I/O helpers
3 *
4 * Copyright 2009-2011 Wolfson Microelectronics PLC.
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
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.
12 */
13
14#include <linux/i2c.h>
15#include <linux/spi/spi.h>
Mark Brownbe3ea3b2011-06-13 19:35:29 +010016#include <linux/regmap.h>
Paul Gortmakerd81a6d72011-09-22 09:34:58 -040017#include <linux/export.h>
Mark Brown5bef44f2011-06-13 17:49:55 +010018#include <sound/soc.h>
19
20#include <trace/events/asoc.h>
21
Lars-Peter Clausen96241c832014-03-18 09:02:07 +010022unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
23{
24 unsigned int ret;
25
26 ret = codec->read(codec, reg);
27 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
28 trace_snd_soc_reg_read(codec, reg, ret);
29
30 return ret;
31}
32EXPORT_SYMBOL_GPL(snd_soc_read);
33
Lars-Peter Clausenab2874a2014-04-19 10:43:57 +020034int snd_soc_write(struct snd_soc_codec *codec, unsigned int reg,
35 unsigned int val)
Lars-Peter Clausen96241c832014-03-18 09:02:07 +010036{
37 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
38 trace_snd_soc_reg_write(codec, reg, val);
39 return codec->write(codec, reg, val);
40}
41EXPORT_SYMBOL_GPL(snd_soc_write);
42
43/**
44 * snd_soc_update_bits - update codec register bits
45 * @codec: audio codec
46 * @reg: codec register
47 * @mask: register mask
48 * @value: new value
49 *
50 * Writes new register value.
51 *
52 * Returns 1 for change, 0 for no change, or negative error code.
53 */
Mark Brownaa0258a2014-04-14 17:42:28 +010054int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned int reg,
Lars-Peter Clausen96241c832014-03-18 09:02:07 +010055 unsigned int mask, unsigned int value)
56{
57 bool change;
58 unsigned int old, new;
59 int ret;
60
61 if (codec->using_regmap) {
62 ret = regmap_update_bits_check(codec->control_data, reg,
63 mask, value, &change);
64 } else {
65 ret = snd_soc_read(codec, reg);
66 if (ret < 0)
67 return ret;
68
69 old = ret;
70 new = (old & ~mask) | (value & mask);
71 change = old != new;
72 if (change)
73 ret = snd_soc_write(codec, reg, new);
74 }
75
76 if (ret < 0)
77 return ret;
78
79 return change;
80}
81EXPORT_SYMBOL_GPL(snd_soc_update_bits);
82
83/**
84 * snd_soc_update_bits_locked - update codec register bits
85 * @codec: audio codec
86 * @reg: codec register
87 * @mask: register mask
88 * @value: new value
89 *
90 * Writes new register value, and takes the codec mutex.
91 *
92 * Returns 1 for change else 0.
93 */
94int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
Mark Brownaa0258a2014-04-14 17:42:28 +010095 unsigned int reg, unsigned int mask,
Lars-Peter Clausen96241c832014-03-18 09:02:07 +010096 unsigned int value)
97{
98 int change;
99
100 mutex_lock(&codec->mutex);
101 change = snd_soc_update_bits(codec, reg, mask, value);
102 mutex_unlock(&codec->mutex);
103
104 return change;
105}
106EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
107
108/**
109 * snd_soc_test_bits - test register for change
110 * @codec: audio codec
111 * @reg: codec register
112 * @mask: register mask
113 * @value: new value
114 *
115 * Tests a register with a new value and checks if the new value is
116 * different from the old value.
117 *
118 * Returns 1 for change else 0.
119 */
Mark Brownaa0258a2014-04-14 17:42:28 +0100120int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned int reg,
Lars-Peter Clausen96241c832014-03-18 09:02:07 +0100121 unsigned int mask, unsigned int value)
122{
123 int change;
124 unsigned int old, new;
125
126 old = snd_soc_read(codec, reg);
127 new = (old & ~mask) | value;
128 change = old != new;
129
130 return change;
131}
132EXPORT_SYMBOL_GPL(snd_soc_test_bits);
133
134int snd_soc_platform_read(struct snd_soc_platform *platform,
135 unsigned int reg)
136{
137 unsigned int ret;
138
139 if (!platform->driver->read) {
140 dev_err(platform->dev, "ASoC: platform has no read back\n");
141 return -1;
142 }
143
144 ret = platform->driver->read(platform, reg);
145 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
146 trace_snd_soc_preg_read(platform, reg, ret);
147
148 return ret;
149}
150EXPORT_SYMBOL_GPL(snd_soc_platform_read);
151
152int snd_soc_platform_write(struct snd_soc_platform *platform,
153 unsigned int reg, unsigned int val)
154{
155 if (!platform->driver->write) {
156 dev_err(platform->dev, "ASoC: platform has no write back\n");
157 return -1;
158 }
159
160 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
161 trace_snd_soc_preg_write(platform, reg, val);
162 return platform->driver->write(platform, reg, val);
163}
164EXPORT_SYMBOL_GPL(snd_soc_platform_write);
165
Mark Brown4835ff92011-08-13 11:50:48 +0900166#ifdef CONFIG_REGMAP
Mark Brownbe3ea3b2011-06-13 19:35:29 +0100167static int hw_write(struct snd_soc_codec *codec, unsigned int reg,
168 unsigned int value)
Mark Brown5bef44f2011-06-13 17:49:55 +0100169{
Mark Brownbe3ea3b2011-06-13 19:35:29 +0100170 return regmap_write(codec->control_data, reg, value);
Mark Brown5bef44f2011-06-13 17:49:55 +0100171}
172
173static unsigned int hw_read(struct snd_soc_codec *codec, unsigned int reg)
174{
175 int ret;
176 unsigned int val;
177
Mark Brown65725472014-02-20 09:08:43 +0900178 ret = regmap_read(codec->control_data, reg, &val);
179 if (ret == 0)
180 return val;
181 else
Mark Brown5bef44f2011-06-13 17:49:55 +0100182 return -1;
Mark Brown5bef44f2011-06-13 17:49:55 +0100183}
184
Mark Brown5bef44f2011-06-13 17:49:55 +0100185/**
186 * snd_soc_codec_set_cache_io: Set up standard I/O functions.
187 *
188 * @codec: CODEC to configure.
Xiubo Li092eba92014-03-11 12:43:21 +0800189 * @map: Register map to write to
Mark Brown5bef44f2011-06-13 17:49:55 +0100190 *
191 * Register formats are frequently shared between many I2C and SPI
192 * devices. In order to promote code reuse the ASoC core provides
193 * some standard implementations of CODEC read and write operations
194 * which can be set up using this function.
195 *
196 * The caller is responsible for allocating and initialising the
197 * actual cache.
198 *
199 * Note that at present this code cannot be used by CODECs with
200 * volatile registers.
201 */
202int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
Xiubo Li092eba92014-03-11 12:43:21 +0800203 struct regmap *regmap)
Mark Brown5bef44f2011-06-13 17:49:55 +0100204{
Mark Brown2b4bdee2012-02-17 14:33:29 -0800205 int ret;
Mark Brown5bef44f2011-06-13 17:49:55 +0100206
Xiubo Li092eba92014-03-11 12:43:21 +0800207 /* Device has made its own regmap arrangements */
208 if (!regmap)
209 codec->control_data = dev_get_regmap(codec->dev, NULL);
210 else
211 codec->control_data = regmap;
212
213 if (IS_ERR(codec->control_data))
214 return PTR_ERR(codec->control_data);
215
Mark Brownbe3ea3b2011-06-13 19:35:29 +0100216 codec->write = hw_write;
Mark Brown5bef44f2011-06-13 17:49:55 +0100217 codec->read = hw_read;
Mark Brown5bef44f2011-06-13 17:49:55 +0100218
Xiubo Li092eba92014-03-11 12:43:21 +0800219 ret = regmap_get_val_bytes(codec->control_data);
220 /* Errors are legitimate for non-integer byte
221 * multiples */
222 if (ret > 0)
223 codec->val_bytes = ret;
Mark Brown2b4bdee2012-02-17 14:33:29 -0800224
Xiubo Li092eba92014-03-11 12:43:21 +0800225 codec->using_regmap = true;
Mark Brown0671da12011-07-24 12:23:37 +0100226
Xiubo Li092eba92014-03-11 12:43:21 +0800227 return 0;
Mark Brown5bef44f2011-06-13 17:49:55 +0100228}
229EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io);
Mark Brown4835ff92011-08-13 11:50:48 +0900230#else
231int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
Xiubo Li092eba92014-03-11 12:43:21 +0800232 struct regmap *regmap)
Mark Brown4835ff92011-08-13 11:50:48 +0900233{
234 return -ENOTSUPP;
235}
236EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io);
237#endif