Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 1 | /* |
| 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 Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 14 | #include <linux/i2c.h> |
Mark Brown | 27ded04 | 2009-07-10 23:28:16 +0100 | [diff] [blame] | 15 | #include <linux/spi/spi.h> |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 16 | #include <sound/soc.h> |
| 17 | |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 18 | static unsigned int snd_soc_4_12_read(struct snd_soc_codec *codec, |
| 19 | unsigned int reg) |
| 20 | { |
| 21 | u16 *cache = codec->reg_cache; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 22 | if (reg >= codec->driver->reg_cache_size) |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 23 | return -1; |
| 24 | return cache[reg]; |
| 25 | } |
| 26 | |
| 27 | static 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 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 34 | BUG_ON(codec->driver->volatile_register); |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 35 | |
| 36 | data[0] = (reg << 4) | ((value >> 8) & 0x000f); |
| 37 | data[1] = value & 0x00ff; |
| 38 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 39 | if (reg < codec->driver->reg_cache_size) |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 40 | cache[reg] = value; |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 41 | |
Mark Brown | a3032b4 | 2010-02-01 18:48:03 +0000 | [diff] [blame] | 42 | if (codec->cache_only) { |
| 43 | codec->cache_sync = 1; |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 44 | return 0; |
Mark Brown | a3032b4 | 2010-02-01 18:48:03 +0000 | [diff] [blame] | 45 | } |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 46 | |
Mark Brown | 985d8c4 | 2010-05-03 16:25:52 +0100 | [diff] [blame] | 47 | dev_dbg(codec->dev, "0x%x = 0x%x\n", reg, value); |
| 48 | |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 49 | ret = codec->hw_write(codec->control_data, data, 2); |
| 50 | if (ret == 2) |
| 51 | return 0; |
| 52 | if (ret < 0) |
| 53 | return ret; |
| 54 | else |
| 55 | return -EIO; |
| 56 | } |
| 57 | |
| 58 | #if defined(CONFIG_SPI_MASTER) |
| 59 | static int snd_soc_4_12_spi_write(void *control_data, const char *data, |
| 60 | int len) |
| 61 | { |
| 62 | struct spi_device *spi = control_data; |
| 63 | struct spi_transfer t; |
| 64 | struct spi_message m; |
| 65 | u8 msg[2]; |
| 66 | |
| 67 | if (len <= 0) |
| 68 | return 0; |
| 69 | |
| 70 | msg[0] = data[1]; |
| 71 | msg[1] = data[0]; |
| 72 | |
| 73 | spi_message_init(&m); |
| 74 | memset(&t, 0, (sizeof t)); |
| 75 | |
| 76 | t.tx_buf = &msg[0]; |
| 77 | t.len = len; |
| 78 | |
| 79 | spi_message_add_tail(&t, &m); |
| 80 | spi_sync(spi, &m); |
| 81 | |
| 82 | return len; |
| 83 | } |
| 84 | #else |
| 85 | #define snd_soc_4_12_spi_write NULL |
| 86 | #endif |
| 87 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 88 | static unsigned int snd_soc_7_9_read(struct snd_soc_codec *codec, |
| 89 | unsigned int reg) |
| 90 | { |
| 91 | u16 *cache = codec->reg_cache; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 92 | if (reg >= codec->driver->reg_cache_size) |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 93 | return -1; |
| 94 | return cache[reg]; |
| 95 | } |
| 96 | |
| 97 | static int snd_soc_7_9_write(struct snd_soc_codec *codec, unsigned int reg, |
| 98 | unsigned int value) |
| 99 | { |
| 100 | u16 *cache = codec->reg_cache; |
| 101 | u8 data[2]; |
| 102 | int ret; |
| 103 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 104 | BUG_ON(codec->driver->volatile_register); |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 105 | |
| 106 | data[0] = (reg << 1) | ((value >> 8) & 0x0001); |
| 107 | data[1] = value & 0x00ff; |
| 108 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 109 | if (reg < codec->driver->reg_cache_size) |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 110 | cache[reg] = value; |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 111 | |
Mark Brown | a3032b4 | 2010-02-01 18:48:03 +0000 | [diff] [blame] | 112 | if (codec->cache_only) { |
| 113 | codec->cache_sync = 1; |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 114 | return 0; |
Mark Brown | a3032b4 | 2010-02-01 18:48:03 +0000 | [diff] [blame] | 115 | } |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 116 | |
Mark Brown | 985d8c4 | 2010-05-03 16:25:52 +0100 | [diff] [blame] | 117 | dev_dbg(codec->dev, "0x%x = 0x%x\n", reg, value); |
| 118 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 119 | ret = codec->hw_write(codec->control_data, data, 2); |
| 120 | if (ret == 2) |
| 121 | return 0; |
| 122 | if (ret < 0) |
| 123 | return ret; |
| 124 | else |
| 125 | return -EIO; |
| 126 | } |
| 127 | |
Mark Brown | 27ded04 | 2009-07-10 23:28:16 +0100 | [diff] [blame] | 128 | #if defined(CONFIG_SPI_MASTER) |
| 129 | static int snd_soc_7_9_spi_write(void *control_data, const char *data, |
| 130 | int len) |
| 131 | { |
| 132 | struct spi_device *spi = control_data; |
| 133 | struct spi_transfer t; |
| 134 | struct spi_message m; |
| 135 | u8 msg[2]; |
| 136 | |
| 137 | if (len <= 0) |
| 138 | return 0; |
| 139 | |
| 140 | msg[0] = data[0]; |
| 141 | msg[1] = data[1]; |
| 142 | |
| 143 | spi_message_init(&m); |
| 144 | memset(&t, 0, (sizeof t)); |
| 145 | |
| 146 | t.tx_buf = &msg[0]; |
| 147 | t.len = len; |
| 148 | |
| 149 | spi_message_add_tail(&t, &m); |
| 150 | spi_sync(spi, &m); |
| 151 | |
| 152 | return len; |
| 153 | } |
| 154 | #else |
| 155 | #define snd_soc_7_9_spi_write NULL |
| 156 | #endif |
| 157 | |
Joonyoung Shim | 341c9b8 | 2009-09-07 12:04:37 +0900 | [diff] [blame] | 158 | static int snd_soc_8_8_write(struct snd_soc_codec *codec, unsigned int reg, |
| 159 | unsigned int value) |
| 160 | { |
| 161 | u8 *cache = codec->reg_cache; |
| 162 | u8 data[2]; |
| 163 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 164 | BUG_ON(codec->driver->volatile_register); |
Joonyoung Shim | 341c9b8 | 2009-09-07 12:04:37 +0900 | [diff] [blame] | 165 | |
Barry Song | f4bee1b | 2010-03-18 16:17:01 +0800 | [diff] [blame] | 166 | reg &= 0xff; |
| 167 | data[0] = reg; |
Joonyoung Shim | 341c9b8 | 2009-09-07 12:04:37 +0900 | [diff] [blame] | 168 | data[1] = value & 0xff; |
| 169 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 170 | if (reg < codec->driver->reg_cache_size) |
Joonyoung Shim | 341c9b8 | 2009-09-07 12:04:37 +0900 | [diff] [blame] | 171 | cache[reg] = value; |
| 172 | |
Mark Brown | a3032b4 | 2010-02-01 18:48:03 +0000 | [diff] [blame] | 173 | if (codec->cache_only) { |
| 174 | codec->cache_sync = 1; |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 175 | return 0; |
Mark Brown | a3032b4 | 2010-02-01 18:48:03 +0000 | [diff] [blame] | 176 | } |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 177 | |
Mark Brown | 985d8c4 | 2010-05-03 16:25:52 +0100 | [diff] [blame] | 178 | dev_dbg(codec->dev, "0x%x = 0x%x\n", reg, value); |
| 179 | |
Joonyoung Shim | 341c9b8 | 2009-09-07 12:04:37 +0900 | [diff] [blame] | 180 | if (codec->hw_write(codec->control_data, data, 2) == 2) |
| 181 | return 0; |
| 182 | else |
| 183 | return -EIO; |
| 184 | } |
| 185 | |
| 186 | static unsigned int snd_soc_8_8_read(struct snd_soc_codec *codec, |
| 187 | unsigned int reg) |
| 188 | { |
| 189 | u8 *cache = codec->reg_cache; |
Barry Song | f4bee1b | 2010-03-18 16:17:01 +0800 | [diff] [blame] | 190 | reg &= 0xff; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 191 | if (reg >= codec->driver->reg_cache_size) |
Joonyoung Shim | 341c9b8 | 2009-09-07 12:04:37 +0900 | [diff] [blame] | 192 | return -1; |
| 193 | return cache[reg]; |
| 194 | } |
| 195 | |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 196 | static int snd_soc_8_16_write(struct snd_soc_codec *codec, unsigned int reg, |
| 197 | unsigned int value) |
| 198 | { |
| 199 | u16 *reg_cache = codec->reg_cache; |
| 200 | u8 data[3]; |
| 201 | |
| 202 | data[0] = reg; |
| 203 | data[1] = (value >> 8) & 0xff; |
| 204 | data[2] = value & 0xff; |
| 205 | |
Takashi Iwai | 3e13f65 | 2010-09-23 07:40:16 +0200 | [diff] [blame^] | 206 | if (!snd_soc_codec_volatile_register(codec, reg) && |
| 207 | reg < codec->driver->reg_cache_size) |
| 208 | reg_cache[reg] = value; |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 209 | |
Mark Brown | a3032b4 | 2010-02-01 18:48:03 +0000 | [diff] [blame] | 210 | if (codec->cache_only) { |
| 211 | codec->cache_sync = 1; |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 212 | return 0; |
Mark Brown | a3032b4 | 2010-02-01 18:48:03 +0000 | [diff] [blame] | 213 | } |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 214 | |
Mark Brown | 985d8c4 | 2010-05-03 16:25:52 +0100 | [diff] [blame] | 215 | dev_dbg(codec->dev, "0x%x = 0x%x\n", reg, value); |
| 216 | |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 217 | if (codec->hw_write(codec->control_data, data, 3) == 3) |
| 218 | return 0; |
| 219 | else |
| 220 | return -EIO; |
| 221 | } |
| 222 | |
| 223 | static unsigned int snd_soc_8_16_read(struct snd_soc_codec *codec, |
| 224 | unsigned int reg) |
| 225 | { |
| 226 | u16 *cache = codec->reg_cache; |
| 227 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 228 | if (reg >= codec->driver->reg_cache_size || |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 229 | snd_soc_codec_volatile_register(codec, reg)) { |
| 230 | if (codec->cache_only) |
Dimitris Papastamos | 391d8a0 | 2010-09-21 17:04:07 +0100 | [diff] [blame] | 231 | return -1; |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 232 | |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 233 | return codec->hw_read(codec, reg); |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 234 | } else { |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 235 | return cache[reg]; |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 236 | } |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 237 | } |
| 238 | |
Randy Dunlap | 17244c2 | 2009-08-10 16:04:39 -0700 | [diff] [blame] | 239 | #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE)) |
Cliff Cai | 85dfcdf | 2010-03-18 16:17:00 +0800 | [diff] [blame] | 240 | static unsigned int snd_soc_8_8_read_i2c(struct snd_soc_codec *codec, |
| 241 | unsigned int r) |
| 242 | { |
| 243 | struct i2c_msg xfer[2]; |
| 244 | u8 reg = r; |
| 245 | u8 data; |
| 246 | int ret; |
| 247 | struct i2c_client *client = codec->control_data; |
| 248 | |
| 249 | /* Write register */ |
| 250 | xfer[0].addr = client->addr; |
| 251 | xfer[0].flags = 0; |
| 252 | xfer[0].len = 1; |
| 253 | xfer[0].buf = ® |
| 254 | |
| 255 | /* Read data */ |
| 256 | xfer[1].addr = client->addr; |
| 257 | xfer[1].flags = I2C_M_RD; |
| 258 | xfer[1].len = 1; |
| 259 | xfer[1].buf = &data; |
| 260 | |
| 261 | ret = i2c_transfer(client->adapter, xfer, 2); |
| 262 | if (ret != 2) { |
| 263 | dev_err(&client->dev, "i2c_transfer() returned %d\n", ret); |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | return data; |
| 268 | } |
| 269 | #else |
| 270 | #define snd_soc_8_8_read_i2c NULL |
| 271 | #endif |
| 272 | |
| 273 | #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE)) |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 274 | static unsigned int snd_soc_8_16_read_i2c(struct snd_soc_codec *codec, |
| 275 | unsigned int r) |
| 276 | { |
| 277 | struct i2c_msg xfer[2]; |
| 278 | u8 reg = r; |
| 279 | u16 data; |
| 280 | int ret; |
| 281 | struct i2c_client *client = codec->control_data; |
| 282 | |
| 283 | /* Write register */ |
| 284 | xfer[0].addr = client->addr; |
| 285 | xfer[0].flags = 0; |
| 286 | xfer[0].len = 1; |
| 287 | xfer[0].buf = ® |
| 288 | |
| 289 | /* Read data */ |
| 290 | xfer[1].addr = client->addr; |
| 291 | xfer[1].flags = I2C_M_RD; |
| 292 | xfer[1].len = 2; |
| 293 | xfer[1].buf = (u8 *)&data; |
| 294 | |
| 295 | ret = i2c_transfer(client->adapter, xfer, 2); |
| 296 | if (ret != 2) { |
| 297 | dev_err(&client->dev, "i2c_transfer() returned %d\n", ret); |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | return (data >> 8) | ((data & 0xff) << 8); |
| 302 | } |
| 303 | #else |
| 304 | #define snd_soc_8_16_read_i2c NULL |
| 305 | #endif |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 306 | |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 307 | #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE)) |
| 308 | static unsigned int snd_soc_16_8_read_i2c(struct snd_soc_codec *codec, |
| 309 | unsigned int r) |
| 310 | { |
| 311 | struct i2c_msg xfer[2]; |
| 312 | u16 reg = r; |
| 313 | u8 data; |
| 314 | int ret; |
| 315 | struct i2c_client *client = codec->control_data; |
| 316 | |
| 317 | /* Write register */ |
| 318 | xfer[0].addr = client->addr; |
| 319 | xfer[0].flags = 0; |
| 320 | xfer[0].len = 2; |
| 321 | xfer[0].buf = (u8 *)® |
| 322 | |
| 323 | /* Read data */ |
| 324 | xfer[1].addr = client->addr; |
| 325 | xfer[1].flags = I2C_M_RD; |
| 326 | xfer[1].len = 1; |
| 327 | xfer[1].buf = &data; |
| 328 | |
| 329 | ret = i2c_transfer(client->adapter, xfer, 2); |
| 330 | if (ret != 2) { |
| 331 | dev_err(&client->dev, "i2c_transfer() returned %d\n", ret); |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | return data; |
| 336 | } |
| 337 | #else |
| 338 | #define snd_soc_16_8_read_i2c NULL |
| 339 | #endif |
| 340 | |
| 341 | static unsigned int snd_soc_16_8_read(struct snd_soc_codec *codec, |
| 342 | unsigned int reg) |
| 343 | { |
Cliff Cai | ac77026 | 2010-08-07 11:16:27 -0400 | [diff] [blame] | 344 | u8 *cache = codec->reg_cache; |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 345 | |
| 346 | reg &= 0xff; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 347 | if (reg >= codec->driver->reg_cache_size) |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 348 | return -1; |
| 349 | return cache[reg]; |
| 350 | } |
| 351 | |
| 352 | static int snd_soc_16_8_write(struct snd_soc_codec *codec, unsigned int reg, |
| 353 | unsigned int value) |
| 354 | { |
Cliff Cai | ac77026 | 2010-08-07 11:16:27 -0400 | [diff] [blame] | 355 | u8 *cache = codec->reg_cache; |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 356 | u8 data[3]; |
| 357 | int ret; |
| 358 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 359 | BUG_ON(codec->driver->volatile_register); |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 360 | |
| 361 | data[0] = (reg >> 8) & 0xff; |
| 362 | data[1] = reg & 0xff; |
| 363 | data[2] = value; |
| 364 | |
| 365 | reg &= 0xff; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 366 | if (reg < codec->driver->reg_cache_size) |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 367 | cache[reg] = value; |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 368 | |
Mark Brown | a3032b4 | 2010-02-01 18:48:03 +0000 | [diff] [blame] | 369 | if (codec->cache_only) { |
| 370 | codec->cache_sync = 1; |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 371 | return 0; |
Mark Brown | a3032b4 | 2010-02-01 18:48:03 +0000 | [diff] [blame] | 372 | } |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 373 | |
Mark Brown | 985d8c4 | 2010-05-03 16:25:52 +0100 | [diff] [blame] | 374 | dev_dbg(codec->dev, "0x%x = 0x%x\n", reg, value); |
| 375 | |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 376 | ret = codec->hw_write(codec->control_data, data, 3); |
| 377 | if (ret == 3) |
| 378 | return 0; |
| 379 | if (ret < 0) |
| 380 | return ret; |
| 381 | else |
| 382 | return -EIO; |
| 383 | } |
| 384 | |
| 385 | #if defined(CONFIG_SPI_MASTER) |
| 386 | static int snd_soc_16_8_spi_write(void *control_data, const char *data, |
| 387 | int len) |
| 388 | { |
| 389 | struct spi_device *spi = control_data; |
| 390 | struct spi_transfer t; |
| 391 | struct spi_message m; |
| 392 | u8 msg[3]; |
| 393 | |
| 394 | if (len <= 0) |
| 395 | return 0; |
| 396 | |
| 397 | msg[0] = data[0]; |
| 398 | msg[1] = data[1]; |
| 399 | msg[2] = data[2]; |
| 400 | |
| 401 | spi_message_init(&m); |
| 402 | memset(&t, 0, (sizeof t)); |
| 403 | |
| 404 | t.tx_buf = &msg[0]; |
| 405 | t.len = len; |
| 406 | |
| 407 | spi_message_add_tail(&t, &m); |
| 408 | spi_sync(spi, &m); |
| 409 | |
| 410 | return len; |
| 411 | } |
| 412 | #else |
| 413 | #define snd_soc_16_8_spi_write NULL |
| 414 | #endif |
| 415 | |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 416 | #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE)) |
| 417 | static unsigned int snd_soc_16_16_read_i2c(struct snd_soc_codec *codec, |
| 418 | unsigned int r) |
| 419 | { |
| 420 | struct i2c_msg xfer[2]; |
| 421 | u16 reg = cpu_to_be16(r); |
| 422 | u16 data; |
| 423 | int ret; |
| 424 | struct i2c_client *client = codec->control_data; |
| 425 | |
| 426 | /* Write register */ |
| 427 | xfer[0].addr = client->addr; |
| 428 | xfer[0].flags = 0; |
| 429 | xfer[0].len = 2; |
| 430 | xfer[0].buf = (u8 *)® |
| 431 | |
| 432 | /* Read data */ |
| 433 | xfer[1].addr = client->addr; |
| 434 | xfer[1].flags = I2C_M_RD; |
| 435 | xfer[1].len = 2; |
| 436 | xfer[1].buf = (u8 *)&data; |
| 437 | |
| 438 | ret = i2c_transfer(client->adapter, xfer, 2); |
| 439 | if (ret != 2) { |
| 440 | dev_err(&client->dev, "i2c_transfer() returned %d\n", ret); |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | return be16_to_cpu(data); |
| 445 | } |
| 446 | #else |
| 447 | #define snd_soc_16_16_read_i2c NULL |
| 448 | #endif |
| 449 | |
| 450 | static unsigned int snd_soc_16_16_read(struct snd_soc_codec *codec, |
| 451 | unsigned int reg) |
| 452 | { |
| 453 | u16 *cache = codec->reg_cache; |
| 454 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 455 | if (reg >= codec->driver->reg_cache_size || |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 456 | snd_soc_codec_volatile_register(codec, reg)) { |
| 457 | if (codec->cache_only) |
Dimitris Papastamos | 391d8a0 | 2010-09-21 17:04:07 +0100 | [diff] [blame] | 458 | return -1; |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 459 | |
| 460 | return codec->hw_read(codec, reg); |
| 461 | } |
| 462 | |
| 463 | return cache[reg]; |
| 464 | } |
| 465 | |
| 466 | static int snd_soc_16_16_write(struct snd_soc_codec *codec, unsigned int reg, |
| 467 | unsigned int value) |
| 468 | { |
| 469 | u16 *cache = codec->reg_cache; |
| 470 | u8 data[4]; |
| 471 | int ret; |
| 472 | |
| 473 | data[0] = (reg >> 8) & 0xff; |
| 474 | data[1] = reg & 0xff; |
| 475 | data[2] = (value >> 8) & 0xff; |
| 476 | data[3] = value & 0xff; |
| 477 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 478 | if (reg < codec->driver->reg_cache_size) |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 479 | cache[reg] = value; |
| 480 | |
| 481 | if (codec->cache_only) { |
| 482 | codec->cache_sync = 1; |
| 483 | return 0; |
| 484 | } |
| 485 | |
Mark Brown | 985d8c4 | 2010-05-03 16:25:52 +0100 | [diff] [blame] | 486 | dev_dbg(codec->dev, "0x%x = 0x%x\n", reg, value); |
| 487 | |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 488 | ret = codec->hw_write(codec->control_data, data, 4); |
| 489 | if (ret == 4) |
| 490 | return 0; |
| 491 | if (ret < 0) |
| 492 | return ret; |
| 493 | else |
| 494 | return -EIO; |
| 495 | } |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 496 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 497 | static struct { |
| 498 | int addr_bits; |
| 499 | int data_bits; |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 500 | int (*write)(struct snd_soc_codec *codec, unsigned int, unsigned int); |
Mark Brown | 27ded04 | 2009-07-10 23:28:16 +0100 | [diff] [blame] | 501 | int (*spi_write)(void *, const char *, int); |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 502 | unsigned int (*read)(struct snd_soc_codec *, unsigned int); |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 503 | unsigned int (*i2c_read)(struct snd_soc_codec *, unsigned int); |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 504 | } io_types[] = { |
Mark Brown | d62ab35 | 2009-09-21 04:21:47 -0700 | [diff] [blame] | 505 | { |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 506 | .addr_bits = 4, .data_bits = 12, |
| 507 | .write = snd_soc_4_12_write, .read = snd_soc_4_12_read, |
| 508 | .spi_write = snd_soc_4_12_spi_write, |
| 509 | }, |
| 510 | { |
Mark Brown | d62ab35 | 2009-09-21 04:21:47 -0700 | [diff] [blame] | 511 | .addr_bits = 7, .data_bits = 9, |
| 512 | .write = snd_soc_7_9_write, .read = snd_soc_7_9_read, |
Barry Song | 8998c89 | 2009-12-31 10:30:34 +0800 | [diff] [blame] | 513 | .spi_write = snd_soc_7_9_spi_write, |
Mark Brown | d62ab35 | 2009-09-21 04:21:47 -0700 | [diff] [blame] | 514 | }, |
| 515 | { |
| 516 | .addr_bits = 8, .data_bits = 8, |
| 517 | .write = snd_soc_8_8_write, .read = snd_soc_8_8_read, |
Cliff Cai | 85dfcdf | 2010-03-18 16:17:00 +0800 | [diff] [blame] | 518 | .i2c_read = snd_soc_8_8_read_i2c, |
Mark Brown | d62ab35 | 2009-09-21 04:21:47 -0700 | [diff] [blame] | 519 | }, |
| 520 | { |
| 521 | .addr_bits = 8, .data_bits = 16, |
| 522 | .write = snd_soc_8_16_write, .read = snd_soc_8_16_read, |
| 523 | .i2c_read = snd_soc_8_16_read_i2c, |
| 524 | }, |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 525 | { |
| 526 | .addr_bits = 16, .data_bits = 8, |
| 527 | .write = snd_soc_16_8_write, .read = snd_soc_16_8_read, |
| 528 | .i2c_read = snd_soc_16_8_read_i2c, |
| 529 | .spi_write = snd_soc_16_8_spi_write, |
| 530 | }, |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 531 | { |
| 532 | .addr_bits = 16, .data_bits = 16, |
| 533 | .write = snd_soc_16_16_write, .read = snd_soc_16_16_read, |
| 534 | .i2c_read = snd_soc_16_16_read_i2c, |
| 535 | }, |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 536 | }; |
| 537 | |
| 538 | /** |
| 539 | * snd_soc_codec_set_cache_io: Set up standard I/O functions. |
| 540 | * |
| 541 | * @codec: CODEC to configure. |
| 542 | * @type: Type of cache. |
| 543 | * @addr_bits: Number of bits of register address data. |
| 544 | * @data_bits: Number of bits of data per register. |
Mark Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 545 | * @control: Control bus used. |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 546 | * |
| 547 | * Register formats are frequently shared between many I2C and SPI |
| 548 | * devices. In order to promote code reuse the ASoC core provides |
| 549 | * some standard implementations of CODEC read and write operations |
| 550 | * which can be set up using this function. |
| 551 | * |
| 552 | * The caller is responsible for allocating and initialising the |
| 553 | * actual cache. |
| 554 | * |
| 555 | * Note that at present this code cannot be used by CODECs with |
| 556 | * volatile registers. |
| 557 | */ |
| 558 | int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec, |
Mark Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 559 | int addr_bits, int data_bits, |
| 560 | enum snd_soc_control_type control) |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 561 | { |
| 562 | int i; |
| 563 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 564 | for (i = 0; i < ARRAY_SIZE(io_types); i++) |
| 565 | if (io_types[i].addr_bits == addr_bits && |
| 566 | io_types[i].data_bits == data_bits) |
| 567 | break; |
| 568 | if (i == ARRAY_SIZE(io_types)) { |
| 569 | printk(KERN_ERR |
| 570 | "No I/O functions for %d bit address %d bit data\n", |
| 571 | addr_bits, data_bits); |
| 572 | return -EINVAL; |
| 573 | } |
| 574 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 575 | codec->driver->write = io_types[i].write; |
| 576 | codec->driver->read = io_types[i].read; |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 577 | |
Mark Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 578 | switch (control) { |
| 579 | case SND_SOC_CUSTOM: |
| 580 | break; |
| 581 | |
| 582 | case SND_SOC_I2C: |
Randy Dunlap | 17244c2 | 2009-08-10 16:04:39 -0700 | [diff] [blame] | 583 | #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE)) |
Mark Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 584 | codec->hw_write = (hw_write_t)i2c_master_send; |
| 585 | #endif |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 586 | if (io_types[i].i2c_read) |
| 587 | codec->hw_read = io_types[i].i2c_read; |
Mark Brown | a6d1434 | 2010-08-12 10:59:15 +0100 | [diff] [blame] | 588 | |
| 589 | codec->control_data = container_of(codec->dev, |
| 590 | struct i2c_client, |
| 591 | dev); |
Mark Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 592 | break; |
| 593 | |
| 594 | case SND_SOC_SPI: |
Mark Brown | 27ded04 | 2009-07-10 23:28:16 +0100 | [diff] [blame] | 595 | if (io_types[i].spi_write) |
| 596 | codec->hw_write = io_types[i].spi_write; |
Mark Brown | a6d1434 | 2010-08-12 10:59:15 +0100 | [diff] [blame] | 597 | |
| 598 | codec->control_data = container_of(codec->dev, |
| 599 | struct spi_device, |
| 600 | dev); |
Mark Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 601 | break; |
| 602 | } |
| 603 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 604 | return 0; |
| 605 | } |
| 606 | EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io); |