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