blob: cde7b63de113c487cfa102143693e08d9f62855e [file] [log] [blame]
Mark Brown17a52fd2009-07-05 17:24:50 +01001/*
2 * soc-cache.c -- ASoC register cache helpers
3 *
4 * Copyright 2009 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
Mark Brown7084a422009-07-10 22:24:27 +010014#include <linux/i2c.h>
Mark Brown27ded042009-07-10 23:28:16 +010015#include <linux/spi/spi.h>
Mark Brown17a52fd2009-07-05 17:24:50 +010016#include <sound/soc.h>
17
Barry Song63b62ab2010-01-27 11:46:17 +080018static unsigned int snd_soc_4_12_read(struct snd_soc_codec *codec,
19 unsigned int reg)
20{
21 u16 *cache = codec->reg_cache;
22 if (reg >= codec->reg_cache_size)
23 return -1;
24 return cache[reg];
25}
26
27static int snd_soc_4_12_write(struct snd_soc_codec *codec, unsigned int reg,
28 unsigned int value)
29{
30 u16 *cache = codec->reg_cache;
31 u8 data[2];
32 int ret;
33
34 BUG_ON(codec->volatile_register);
35
36 data[0] = (reg << 4) | ((value >> 8) & 0x000f);
37 data[1] = value & 0x00ff;
38
39 if (reg < codec->reg_cache_size)
40 cache[reg] = value;
41 ret = codec->hw_write(codec->control_data, data, 2);
42 if (ret == 2)
43 return 0;
44 if (ret < 0)
45 return ret;
46 else
47 return -EIO;
48}
49
50#if defined(CONFIG_SPI_MASTER)
51static int snd_soc_4_12_spi_write(void *control_data, const char *data,
52 int len)
53{
54 struct spi_device *spi = control_data;
55 struct spi_transfer t;
56 struct spi_message m;
57 u8 msg[2];
58
59 if (len <= 0)
60 return 0;
61
62 msg[0] = data[1];
63 msg[1] = data[0];
64
65 spi_message_init(&m);
66 memset(&t, 0, (sizeof t));
67
68 t.tx_buf = &msg[0];
69 t.len = len;
70
71 spi_message_add_tail(&t, &m);
72 spi_sync(spi, &m);
73
74 return len;
75}
76#else
77#define snd_soc_4_12_spi_write NULL
78#endif
79
Mark Brown17a52fd2009-07-05 17:24:50 +010080static unsigned int snd_soc_7_9_read(struct snd_soc_codec *codec,
81 unsigned int reg)
82{
83 u16 *cache = codec->reg_cache;
84 if (reg >= codec->reg_cache_size)
85 return -1;
86 return cache[reg];
87}
88
89static int snd_soc_7_9_write(struct snd_soc_codec *codec, unsigned int reg,
90 unsigned int value)
91{
92 u16 *cache = codec->reg_cache;
93 u8 data[2];
94 int ret;
95
96 BUG_ON(codec->volatile_register);
97
98 data[0] = (reg << 1) | ((value >> 8) & 0x0001);
99 data[1] = value & 0x00ff;
100
101 if (reg < codec->reg_cache_size)
102 cache[reg] = value;
103 ret = codec->hw_write(codec->control_data, data, 2);
104 if (ret == 2)
105 return 0;
106 if (ret < 0)
107 return ret;
108 else
109 return -EIO;
110}
111
Mark Brown27ded042009-07-10 23:28:16 +0100112#if defined(CONFIG_SPI_MASTER)
113static int snd_soc_7_9_spi_write(void *control_data, const char *data,
114 int len)
115{
116 struct spi_device *spi = control_data;
117 struct spi_transfer t;
118 struct spi_message m;
119 u8 msg[2];
120
121 if (len <= 0)
122 return 0;
123
124 msg[0] = data[0];
125 msg[1] = data[1];
126
127 spi_message_init(&m);
128 memset(&t, 0, (sizeof t));
129
130 t.tx_buf = &msg[0];
131 t.len = len;
132
133 spi_message_add_tail(&t, &m);
134 spi_sync(spi, &m);
135
136 return len;
137}
138#else
139#define snd_soc_7_9_spi_write NULL
140#endif
141
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900142static int snd_soc_8_8_write(struct snd_soc_codec *codec, unsigned int reg,
143 unsigned int value)
144{
145 u8 *cache = codec->reg_cache;
146 u8 data[2];
147
148 BUG_ON(codec->volatile_register);
149
150 data[0] = reg & 0xff;
151 data[1] = value & 0xff;
152
153 if (reg < codec->reg_cache_size)
154 cache[reg] = value;
155
156 if (codec->hw_write(codec->control_data, data, 2) == 2)
157 return 0;
158 else
159 return -EIO;
160}
161
162static unsigned int snd_soc_8_8_read(struct snd_soc_codec *codec,
163 unsigned int reg)
164{
165 u8 *cache = codec->reg_cache;
166 if (reg >= codec->reg_cache_size)
167 return -1;
168 return cache[reg];
169}
170
Mark Brownafa2f102009-07-10 23:11:24 +0100171static int snd_soc_8_16_write(struct snd_soc_codec *codec, unsigned int reg,
172 unsigned int value)
173{
174 u16 *reg_cache = codec->reg_cache;
175 u8 data[3];
176
177 data[0] = reg;
178 data[1] = (value >> 8) & 0xff;
179 data[2] = value & 0xff;
180
181 if (!snd_soc_codec_volatile_register(codec, reg))
182 reg_cache[reg] = value;
183
184 if (codec->hw_write(codec->control_data, data, 3) == 3)
185 return 0;
186 else
187 return -EIO;
188}
189
190static unsigned int snd_soc_8_16_read(struct snd_soc_codec *codec,
191 unsigned int reg)
192{
193 u16 *cache = codec->reg_cache;
194
195 if (reg >= codec->reg_cache_size ||
196 snd_soc_codec_volatile_register(codec, reg))
197 return codec->hw_read(codec, reg);
198 else
199 return cache[reg];
200}
201
Randy Dunlap17244c22009-08-10 16:04:39 -0700202#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
Mark Brownafa2f102009-07-10 23:11:24 +0100203static unsigned int snd_soc_8_16_read_i2c(struct snd_soc_codec *codec,
204 unsigned int r)
205{
206 struct i2c_msg xfer[2];
207 u8 reg = r;
208 u16 data;
209 int ret;
210 struct i2c_client *client = codec->control_data;
211
212 /* Write register */
213 xfer[0].addr = client->addr;
214 xfer[0].flags = 0;
215 xfer[0].len = 1;
216 xfer[0].buf = &reg;
217
218 /* Read data */
219 xfer[1].addr = client->addr;
220 xfer[1].flags = I2C_M_RD;
221 xfer[1].len = 2;
222 xfer[1].buf = (u8 *)&data;
223
224 ret = i2c_transfer(client->adapter, xfer, 2);
225 if (ret != 2) {
226 dev_err(&client->dev, "i2c_transfer() returned %d\n", ret);
227 return 0;
228 }
229
230 return (data >> 8) | ((data & 0xff) << 8);
231}
232#else
233#define snd_soc_8_16_read_i2c NULL
234#endif
Mark Brown17a52fd2009-07-05 17:24:50 +0100235
236static struct {
237 int addr_bits;
238 int data_bits;
Mark Brownafa2f102009-07-10 23:11:24 +0100239 int (*write)(struct snd_soc_codec *codec, unsigned int, unsigned int);
Mark Brown27ded042009-07-10 23:28:16 +0100240 int (*spi_write)(void *, const char *, int);
Mark Brown17a52fd2009-07-05 17:24:50 +0100241 unsigned int (*read)(struct snd_soc_codec *, unsigned int);
Mark Brownafa2f102009-07-10 23:11:24 +0100242 unsigned int (*i2c_read)(struct snd_soc_codec *, unsigned int);
Mark Brown17a52fd2009-07-05 17:24:50 +0100243} io_types[] = {
Mark Brownd62ab352009-09-21 04:21:47 -0700244 {
Barry Song63b62ab2010-01-27 11:46:17 +0800245 .addr_bits = 4, .data_bits = 12,
246 .write = snd_soc_4_12_write, .read = snd_soc_4_12_read,
247 .spi_write = snd_soc_4_12_spi_write,
248 },
249 {
Mark Brownd62ab352009-09-21 04:21:47 -0700250 .addr_bits = 7, .data_bits = 9,
251 .write = snd_soc_7_9_write, .read = snd_soc_7_9_read,
Barry Song8998c892009-12-31 10:30:34 +0800252 .spi_write = snd_soc_7_9_spi_write,
Mark Brownd62ab352009-09-21 04:21:47 -0700253 },
254 {
255 .addr_bits = 8, .data_bits = 8,
256 .write = snd_soc_8_8_write, .read = snd_soc_8_8_read,
257 },
258 {
259 .addr_bits = 8, .data_bits = 16,
260 .write = snd_soc_8_16_write, .read = snd_soc_8_16_read,
261 .i2c_read = snd_soc_8_16_read_i2c,
262 },
Mark Brown17a52fd2009-07-05 17:24:50 +0100263};
264
265/**
266 * snd_soc_codec_set_cache_io: Set up standard I/O functions.
267 *
268 * @codec: CODEC to configure.
269 * @type: Type of cache.
270 * @addr_bits: Number of bits of register address data.
271 * @data_bits: Number of bits of data per register.
Mark Brown7084a422009-07-10 22:24:27 +0100272 * @control: Control bus used.
Mark Brown17a52fd2009-07-05 17:24:50 +0100273 *
274 * Register formats are frequently shared between many I2C and SPI
275 * devices. In order to promote code reuse the ASoC core provides
276 * some standard implementations of CODEC read and write operations
277 * which can be set up using this function.
278 *
279 * The caller is responsible for allocating and initialising the
280 * actual cache.
281 *
282 * Note that at present this code cannot be used by CODECs with
283 * volatile registers.
284 */
285int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
Mark Brown7084a422009-07-10 22:24:27 +0100286 int addr_bits, int data_bits,
287 enum snd_soc_control_type control)
Mark Brown17a52fd2009-07-05 17:24:50 +0100288{
289 int i;
290
Mark Brown17a52fd2009-07-05 17:24:50 +0100291 for (i = 0; i < ARRAY_SIZE(io_types); i++)
292 if (io_types[i].addr_bits == addr_bits &&
293 io_types[i].data_bits == data_bits)
294 break;
295 if (i == ARRAY_SIZE(io_types)) {
296 printk(KERN_ERR
297 "No I/O functions for %d bit address %d bit data\n",
298 addr_bits, data_bits);
299 return -EINVAL;
300 }
301
302 codec->write = io_types[i].write;
303 codec->read = io_types[i].read;
304
Mark Brown7084a422009-07-10 22:24:27 +0100305 switch (control) {
306 case SND_SOC_CUSTOM:
307 break;
308
309 case SND_SOC_I2C:
Randy Dunlap17244c22009-08-10 16:04:39 -0700310#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
Mark Brown7084a422009-07-10 22:24:27 +0100311 codec->hw_write = (hw_write_t)i2c_master_send;
312#endif
Mark Brownafa2f102009-07-10 23:11:24 +0100313 if (io_types[i].i2c_read)
314 codec->hw_read = io_types[i].i2c_read;
Mark Brown7084a422009-07-10 22:24:27 +0100315 break;
316
317 case SND_SOC_SPI:
Mark Brown27ded042009-07-10 23:28:16 +0100318 if (io_types[i].spi_write)
319 codec->hw_write = io_types[i].spi_write;
Mark Brown7084a422009-07-10 22:24:27 +0100320 break;
321 }
322
Mark Brown17a52fd2009-07-05 17:24:50 +0100323 return 0;
324}
325EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io);