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