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> |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 17 | #include <linux/lzo.h> |
| 18 | #include <linux/bitmap.h> |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 19 | #include <linux/rbtree.h> |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 20 | |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 21 | static unsigned int snd_soc_4_12_read(struct snd_soc_codec *codec, |
| 22 | unsigned int reg) |
| 23 | { |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 24 | int ret; |
| 25 | unsigned int val; |
Dimitris Papastamos | db49c14 | 2010-09-22 13:25:47 +0100 | [diff] [blame] | 26 | |
| 27 | if (reg >= codec->driver->reg_cache_size || |
Dimitris Papastamos | dad8e7a | 2011-01-19 14:53:36 +0000 | [diff] [blame] | 28 | snd_soc_codec_volatile_register(codec, reg) || |
| 29 | codec->cache_bypass) { |
Dimitris Papastamos | db49c14 | 2010-09-22 13:25:47 +0100 | [diff] [blame] | 30 | if (codec->cache_only) |
| 31 | return -1; |
| 32 | |
Dimitris Papastamos | 5aaa062 | 2010-11-08 15:37:07 +0000 | [diff] [blame] | 33 | BUG_ON(!codec->hw_read); |
Dimitris Papastamos | db49c14 | 2010-09-22 13:25:47 +0100 | [diff] [blame] | 34 | return codec->hw_read(codec, reg); |
| 35 | } |
| 36 | |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 37 | ret = snd_soc_cache_read(codec, reg, &val); |
| 38 | if (ret < 0) |
| 39 | return -1; |
| 40 | return val; |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | static int snd_soc_4_12_write(struct snd_soc_codec *codec, unsigned int reg, |
| 44 | unsigned int value) |
| 45 | { |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 46 | u8 data[2]; |
| 47 | int ret; |
| 48 | |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 49 | data[0] = (reg << 4) | ((value >> 8) & 0x000f); |
| 50 | data[1] = value & 0x00ff; |
| 51 | |
Dimitris Papastamos | db49c14 | 2010-09-22 13:25:47 +0100 | [diff] [blame] | 52 | if (!snd_soc_codec_volatile_register(codec, reg) && |
Dimitris Papastamos | dad8e7a | 2011-01-19 14:53:36 +0000 | [diff] [blame] | 53 | reg < codec->driver->reg_cache_size && |
| 54 | !codec->cache_bypass) { |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 55 | ret = snd_soc_cache_write(codec, reg, value); |
| 56 | if (ret < 0) |
| 57 | return -1; |
| 58 | } |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 59 | |
Mark Brown | a3032b4 | 2010-02-01 18:48:03 +0000 | [diff] [blame] | 60 | if (codec->cache_only) { |
| 61 | codec->cache_sync = 1; |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 62 | return 0; |
Mark Brown | a3032b4 | 2010-02-01 18:48:03 +0000 | [diff] [blame] | 63 | } |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 64 | |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 65 | ret = codec->hw_write(codec->control_data, data, 2); |
| 66 | if (ret == 2) |
| 67 | return 0; |
| 68 | if (ret < 0) |
| 69 | return ret; |
| 70 | else |
| 71 | return -EIO; |
| 72 | } |
| 73 | |
| 74 | #if defined(CONFIG_SPI_MASTER) |
| 75 | static int snd_soc_4_12_spi_write(void *control_data, const char *data, |
| 76 | int len) |
| 77 | { |
| 78 | struct spi_device *spi = control_data; |
| 79 | struct spi_transfer t; |
| 80 | struct spi_message m; |
| 81 | u8 msg[2]; |
| 82 | |
| 83 | if (len <= 0) |
| 84 | return 0; |
| 85 | |
| 86 | msg[0] = data[1]; |
| 87 | msg[1] = data[0]; |
| 88 | |
| 89 | spi_message_init(&m); |
Dimitris Papastamos | 465d7fc | 2010-12-14 15:15:36 +0000 | [diff] [blame] | 90 | memset(&t, 0, sizeof t); |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 91 | |
| 92 | t.tx_buf = &msg[0]; |
| 93 | t.len = len; |
| 94 | |
| 95 | spi_message_add_tail(&t, &m); |
| 96 | spi_sync(spi, &m); |
| 97 | |
| 98 | return len; |
| 99 | } |
| 100 | #else |
| 101 | #define snd_soc_4_12_spi_write NULL |
| 102 | #endif |
| 103 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 104 | static unsigned int snd_soc_7_9_read(struct snd_soc_codec *codec, |
| 105 | unsigned int reg) |
| 106 | { |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 107 | int ret; |
| 108 | unsigned int val; |
Dimitris Papastamos | db49c14 | 2010-09-22 13:25:47 +0100 | [diff] [blame] | 109 | |
| 110 | if (reg >= codec->driver->reg_cache_size || |
Dimitris Papastamos | dad8e7a | 2011-01-19 14:53:36 +0000 | [diff] [blame] | 111 | snd_soc_codec_volatile_register(codec, reg) || |
| 112 | codec->cache_bypass) { |
Dimitris Papastamos | db49c14 | 2010-09-22 13:25:47 +0100 | [diff] [blame] | 113 | if (codec->cache_only) |
| 114 | return -1; |
| 115 | |
Dimitris Papastamos | 5aaa062 | 2010-11-08 15:37:07 +0000 | [diff] [blame] | 116 | BUG_ON(!codec->hw_read); |
Dimitris Papastamos | db49c14 | 2010-09-22 13:25:47 +0100 | [diff] [blame] | 117 | return codec->hw_read(codec, reg); |
| 118 | } |
| 119 | |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 120 | ret = snd_soc_cache_read(codec, reg, &val); |
| 121 | if (ret < 0) |
| 122 | return -1; |
| 123 | return val; |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static int snd_soc_7_9_write(struct snd_soc_codec *codec, unsigned int reg, |
| 127 | unsigned int value) |
| 128 | { |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 129 | u8 data[2]; |
| 130 | int ret; |
| 131 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 132 | data[0] = (reg << 1) | ((value >> 8) & 0x0001); |
| 133 | data[1] = value & 0x00ff; |
| 134 | |
Dimitris Papastamos | db49c14 | 2010-09-22 13:25:47 +0100 | [diff] [blame] | 135 | if (!snd_soc_codec_volatile_register(codec, reg) && |
Dimitris Papastamos | dad8e7a | 2011-01-19 14:53:36 +0000 | [diff] [blame] | 136 | reg < codec->driver->reg_cache_size && |
| 137 | !codec->cache_bypass) { |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 138 | ret = snd_soc_cache_write(codec, reg, value); |
| 139 | if (ret < 0) |
| 140 | return -1; |
| 141 | } |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 142 | |
Mark Brown | a3032b4 | 2010-02-01 18:48:03 +0000 | [diff] [blame] | 143 | if (codec->cache_only) { |
| 144 | codec->cache_sync = 1; |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 145 | return 0; |
Mark Brown | a3032b4 | 2010-02-01 18:48:03 +0000 | [diff] [blame] | 146 | } |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 147 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 148 | ret = codec->hw_write(codec->control_data, data, 2); |
| 149 | if (ret == 2) |
| 150 | return 0; |
| 151 | if (ret < 0) |
| 152 | return ret; |
| 153 | else |
| 154 | return -EIO; |
| 155 | } |
| 156 | |
Mark Brown | 27ded04 | 2009-07-10 23:28:16 +0100 | [diff] [blame] | 157 | #if defined(CONFIG_SPI_MASTER) |
| 158 | static int snd_soc_7_9_spi_write(void *control_data, const char *data, |
| 159 | int len) |
| 160 | { |
| 161 | struct spi_device *spi = control_data; |
| 162 | struct spi_transfer t; |
| 163 | struct spi_message m; |
| 164 | u8 msg[2]; |
| 165 | |
| 166 | if (len <= 0) |
| 167 | return 0; |
| 168 | |
| 169 | msg[0] = data[0]; |
| 170 | msg[1] = data[1]; |
| 171 | |
| 172 | spi_message_init(&m); |
Dimitris Papastamos | 465d7fc | 2010-12-14 15:15:36 +0000 | [diff] [blame] | 173 | memset(&t, 0, sizeof t); |
Mark Brown | 27ded04 | 2009-07-10 23:28:16 +0100 | [diff] [blame] | 174 | |
| 175 | t.tx_buf = &msg[0]; |
| 176 | t.len = len; |
| 177 | |
| 178 | spi_message_add_tail(&t, &m); |
| 179 | spi_sync(spi, &m); |
| 180 | |
| 181 | return len; |
| 182 | } |
| 183 | #else |
| 184 | #define snd_soc_7_9_spi_write NULL |
| 185 | #endif |
| 186 | |
Joonyoung Shim | 341c9b8 | 2009-09-07 12:04:37 +0900 | [diff] [blame] | 187 | static int snd_soc_8_8_write(struct snd_soc_codec *codec, unsigned int reg, |
| 188 | unsigned int value) |
| 189 | { |
Joonyoung Shim | 341c9b8 | 2009-09-07 12:04:37 +0900 | [diff] [blame] | 190 | u8 data[2]; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 191 | int ret; |
Joonyoung Shim | 341c9b8 | 2009-09-07 12:04:37 +0900 | [diff] [blame] | 192 | |
Barry Song | f4bee1b | 2010-03-18 16:17:01 +0800 | [diff] [blame] | 193 | reg &= 0xff; |
| 194 | data[0] = reg; |
Joonyoung Shim | 341c9b8 | 2009-09-07 12:04:37 +0900 | [diff] [blame] | 195 | data[1] = value & 0xff; |
| 196 | |
Dimitris Papastamos | 005d65f | 2010-09-22 16:16:06 +0100 | [diff] [blame] | 197 | if (!snd_soc_codec_volatile_register(codec, reg) && |
Dimitris Papastamos | dad8e7a | 2011-01-19 14:53:36 +0000 | [diff] [blame] | 198 | reg < codec->driver->reg_cache_size && |
| 199 | !codec->cache_bypass) { |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 200 | ret = snd_soc_cache_write(codec, reg, value); |
| 201 | if (ret < 0) |
| 202 | return -1; |
| 203 | } |
Joonyoung Shim | 341c9b8 | 2009-09-07 12:04:37 +0900 | [diff] [blame] | 204 | |
Mark Brown | a3032b4 | 2010-02-01 18:48:03 +0000 | [diff] [blame] | 205 | if (codec->cache_only) { |
| 206 | codec->cache_sync = 1; |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 207 | return 0; |
Mark Brown | a3032b4 | 2010-02-01 18:48:03 +0000 | [diff] [blame] | 208 | } |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 209 | |
Joonyoung Shim | 341c9b8 | 2009-09-07 12:04:37 +0900 | [diff] [blame] | 210 | if (codec->hw_write(codec->control_data, data, 2) == 2) |
| 211 | return 0; |
| 212 | else |
| 213 | return -EIO; |
| 214 | } |
| 215 | |
| 216 | static unsigned int snd_soc_8_8_read(struct snd_soc_codec *codec, |
| 217 | unsigned int reg) |
| 218 | { |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 219 | int ret; |
| 220 | unsigned int val; |
Dimitris Papastamos | db49c14 | 2010-09-22 13:25:47 +0100 | [diff] [blame] | 221 | |
Barry Song | f4bee1b | 2010-03-18 16:17:01 +0800 | [diff] [blame] | 222 | reg &= 0xff; |
Dimitris Papastamos | db49c14 | 2010-09-22 13:25:47 +0100 | [diff] [blame] | 223 | if (reg >= codec->driver->reg_cache_size || |
Dimitris Papastamos | dad8e7a | 2011-01-19 14:53:36 +0000 | [diff] [blame] | 224 | snd_soc_codec_volatile_register(codec, reg) || |
| 225 | codec->cache_bypass) { |
Dimitris Papastamos | db49c14 | 2010-09-22 13:25:47 +0100 | [diff] [blame] | 226 | if (codec->cache_only) |
| 227 | return -1; |
| 228 | |
Dimitris Papastamos | 5aaa062 | 2010-11-08 15:37:07 +0000 | [diff] [blame] | 229 | BUG_ON(!codec->hw_read); |
Dimitris Papastamos | db49c14 | 2010-09-22 13:25:47 +0100 | [diff] [blame] | 230 | return codec->hw_read(codec, reg); |
| 231 | } |
| 232 | |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 233 | ret = snd_soc_cache_read(codec, reg, &val); |
| 234 | if (ret < 0) |
| 235 | return -1; |
| 236 | return val; |
Joonyoung Shim | 341c9b8 | 2009-09-07 12:04:37 +0900 | [diff] [blame] | 237 | } |
| 238 | |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 239 | #if defined(CONFIG_SPI_MASTER) |
| 240 | static int snd_soc_8_8_spi_write(void *control_data, const char *data, |
| 241 | int len) |
| 242 | { |
| 243 | struct spi_device *spi = control_data; |
| 244 | struct spi_transfer t; |
| 245 | struct spi_message m; |
| 246 | u8 msg[2]; |
| 247 | |
| 248 | if (len <= 0) |
| 249 | return 0; |
| 250 | |
| 251 | msg[0] = data[0]; |
| 252 | msg[1] = data[1]; |
| 253 | |
| 254 | spi_message_init(&m); |
Dimitris Papastamos | 465d7fc | 2010-12-14 15:15:36 +0000 | [diff] [blame] | 255 | memset(&t, 0, sizeof t); |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 256 | |
| 257 | t.tx_buf = &msg[0]; |
| 258 | t.len = len; |
| 259 | |
| 260 | spi_message_add_tail(&t, &m); |
| 261 | spi_sync(spi, &m); |
| 262 | |
| 263 | return len; |
| 264 | } |
| 265 | #else |
| 266 | #define snd_soc_8_8_spi_write NULL |
| 267 | #endif |
| 268 | |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 269 | static int snd_soc_8_16_write(struct snd_soc_codec *codec, unsigned int reg, |
| 270 | unsigned int value) |
| 271 | { |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 272 | u8 data[3]; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 273 | int ret; |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 274 | |
| 275 | data[0] = reg; |
| 276 | data[1] = (value >> 8) & 0xff; |
| 277 | data[2] = value & 0xff; |
| 278 | |
Takashi Iwai | 3e13f65 | 2010-09-23 07:40:16 +0200 | [diff] [blame] | 279 | if (!snd_soc_codec_volatile_register(codec, reg) && |
Dimitris Papastamos | dad8e7a | 2011-01-19 14:53:36 +0000 | [diff] [blame] | 280 | reg < codec->driver->reg_cache_size && |
| 281 | !codec->cache_bypass) { |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 282 | ret = snd_soc_cache_write(codec, reg, value); |
| 283 | if (ret < 0) |
| 284 | return -1; |
| 285 | } |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 286 | |
Mark Brown | a3032b4 | 2010-02-01 18:48:03 +0000 | [diff] [blame] | 287 | if (codec->cache_only) { |
| 288 | codec->cache_sync = 1; |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 289 | return 0; |
Mark Brown | a3032b4 | 2010-02-01 18:48:03 +0000 | [diff] [blame] | 290 | } |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 291 | |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 292 | if (codec->hw_write(codec->control_data, data, 3) == 3) |
| 293 | return 0; |
| 294 | else |
| 295 | return -EIO; |
| 296 | } |
| 297 | |
| 298 | static unsigned int snd_soc_8_16_read(struct snd_soc_codec *codec, |
| 299 | unsigned int reg) |
| 300 | { |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 301 | int ret; |
| 302 | unsigned int val; |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 303 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 304 | if (reg >= codec->driver->reg_cache_size || |
Dimitris Papastamos | dad8e7a | 2011-01-19 14:53:36 +0000 | [diff] [blame] | 305 | snd_soc_codec_volatile_register(codec, reg) || |
| 306 | codec->cache_bypass) { |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 307 | if (codec->cache_only) |
Dimitris Papastamos | 391d8a0 | 2010-09-21 17:04:07 +0100 | [diff] [blame] | 308 | return -1; |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 309 | |
Dimitris Papastamos | 5aaa062 | 2010-11-08 15:37:07 +0000 | [diff] [blame] | 310 | BUG_ON(!codec->hw_read); |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 311 | return codec->hw_read(codec, reg); |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 312 | } |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 313 | |
| 314 | ret = snd_soc_cache_read(codec, reg, &val); |
| 315 | if (ret < 0) |
| 316 | return -1; |
| 317 | return val; |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 318 | } |
| 319 | |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 320 | #if defined(CONFIG_SPI_MASTER) |
| 321 | static int snd_soc_8_16_spi_write(void *control_data, const char *data, |
| 322 | int len) |
| 323 | { |
| 324 | struct spi_device *spi = control_data; |
| 325 | struct spi_transfer t; |
| 326 | struct spi_message m; |
| 327 | u8 msg[3]; |
| 328 | |
| 329 | if (len <= 0) |
| 330 | return 0; |
| 331 | |
| 332 | msg[0] = data[0]; |
| 333 | msg[1] = data[1]; |
| 334 | msg[2] = data[2]; |
| 335 | |
| 336 | spi_message_init(&m); |
Dimitris Papastamos | 465d7fc | 2010-12-14 15:15:36 +0000 | [diff] [blame] | 337 | memset(&t, 0, sizeof t); |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 338 | |
| 339 | t.tx_buf = &msg[0]; |
| 340 | t.len = len; |
| 341 | |
| 342 | spi_message_add_tail(&t, &m); |
| 343 | spi_sync(spi, &m); |
| 344 | |
| 345 | return len; |
| 346 | } |
| 347 | #else |
| 348 | #define snd_soc_8_16_spi_write NULL |
| 349 | #endif |
| 350 | |
Randy Dunlap | 17244c2 | 2009-08-10 16:04:39 -0700 | [diff] [blame] | 351 | #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE)) |
Cliff Cai | 85dfcdf | 2010-03-18 16:17:00 +0800 | [diff] [blame] | 352 | static unsigned int snd_soc_8_8_read_i2c(struct snd_soc_codec *codec, |
| 353 | unsigned int r) |
| 354 | { |
| 355 | struct i2c_msg xfer[2]; |
| 356 | u8 reg = r; |
| 357 | u8 data; |
| 358 | int ret; |
| 359 | struct i2c_client *client = codec->control_data; |
| 360 | |
| 361 | /* Write register */ |
| 362 | xfer[0].addr = client->addr; |
| 363 | xfer[0].flags = 0; |
| 364 | xfer[0].len = 1; |
| 365 | xfer[0].buf = ® |
| 366 | |
| 367 | /* Read data */ |
| 368 | xfer[1].addr = client->addr; |
| 369 | xfer[1].flags = I2C_M_RD; |
| 370 | xfer[1].len = 1; |
| 371 | xfer[1].buf = &data; |
| 372 | |
| 373 | ret = i2c_transfer(client->adapter, xfer, 2); |
| 374 | if (ret != 2) { |
| 375 | dev_err(&client->dev, "i2c_transfer() returned %d\n", ret); |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | return data; |
| 380 | } |
| 381 | #else |
| 382 | #define snd_soc_8_8_read_i2c NULL |
| 383 | #endif |
| 384 | |
| 385 | #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE)) |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 386 | static unsigned int snd_soc_8_16_read_i2c(struct snd_soc_codec *codec, |
| 387 | unsigned int r) |
| 388 | { |
| 389 | struct i2c_msg xfer[2]; |
| 390 | u8 reg = r; |
| 391 | u16 data; |
| 392 | int ret; |
| 393 | struct i2c_client *client = codec->control_data; |
| 394 | |
| 395 | /* Write register */ |
| 396 | xfer[0].addr = client->addr; |
| 397 | xfer[0].flags = 0; |
| 398 | xfer[0].len = 1; |
| 399 | xfer[0].buf = ® |
| 400 | |
| 401 | /* Read data */ |
| 402 | xfer[1].addr = client->addr; |
| 403 | xfer[1].flags = I2C_M_RD; |
| 404 | xfer[1].len = 2; |
| 405 | xfer[1].buf = (u8 *)&data; |
| 406 | |
| 407 | ret = i2c_transfer(client->adapter, xfer, 2); |
| 408 | if (ret != 2) { |
| 409 | dev_err(&client->dev, "i2c_transfer() returned %d\n", ret); |
| 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | return (data >> 8) | ((data & 0xff) << 8); |
| 414 | } |
| 415 | #else |
| 416 | #define snd_soc_8_16_read_i2c NULL |
| 417 | #endif |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 418 | |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 419 | #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE)) |
| 420 | static unsigned int snd_soc_16_8_read_i2c(struct snd_soc_codec *codec, |
| 421 | unsigned int r) |
| 422 | { |
| 423 | struct i2c_msg xfer[2]; |
| 424 | u16 reg = r; |
| 425 | u8 data; |
| 426 | int ret; |
| 427 | struct i2c_client *client = codec->control_data; |
| 428 | |
| 429 | /* Write register */ |
| 430 | xfer[0].addr = client->addr; |
| 431 | xfer[0].flags = 0; |
| 432 | xfer[0].len = 2; |
| 433 | xfer[0].buf = (u8 *)® |
| 434 | |
| 435 | /* Read data */ |
| 436 | xfer[1].addr = client->addr; |
| 437 | xfer[1].flags = I2C_M_RD; |
| 438 | xfer[1].len = 1; |
| 439 | xfer[1].buf = &data; |
| 440 | |
| 441 | ret = i2c_transfer(client->adapter, xfer, 2); |
| 442 | if (ret != 2) { |
| 443 | dev_err(&client->dev, "i2c_transfer() returned %d\n", ret); |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | return data; |
| 448 | } |
| 449 | #else |
| 450 | #define snd_soc_16_8_read_i2c NULL |
| 451 | #endif |
| 452 | |
| 453 | static unsigned int snd_soc_16_8_read(struct snd_soc_codec *codec, |
| 454 | unsigned int reg) |
| 455 | { |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 456 | int ret; |
| 457 | unsigned int val; |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 458 | |
| 459 | reg &= 0xff; |
Dimitris Papastamos | db49c14 | 2010-09-22 13:25:47 +0100 | [diff] [blame] | 460 | if (reg >= codec->driver->reg_cache_size || |
Dimitris Papastamos | dad8e7a | 2011-01-19 14:53:36 +0000 | [diff] [blame] | 461 | snd_soc_codec_volatile_register(codec, reg) || |
| 462 | codec->cache_bypass) { |
Dimitris Papastamos | db49c14 | 2010-09-22 13:25:47 +0100 | [diff] [blame] | 463 | if (codec->cache_only) |
| 464 | return -1; |
| 465 | |
Dimitris Papastamos | 5aaa062 | 2010-11-08 15:37:07 +0000 | [diff] [blame] | 466 | BUG_ON(!codec->hw_read); |
Dimitris Papastamos | db49c14 | 2010-09-22 13:25:47 +0100 | [diff] [blame] | 467 | return codec->hw_read(codec, reg); |
| 468 | } |
| 469 | |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 470 | ret = snd_soc_cache_read(codec, reg, &val); |
| 471 | if (ret < 0) |
| 472 | return -1; |
| 473 | return val; |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | static int snd_soc_16_8_write(struct snd_soc_codec *codec, unsigned int reg, |
| 477 | unsigned int value) |
| 478 | { |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 479 | u8 data[3]; |
| 480 | int ret; |
| 481 | |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 482 | data[0] = (reg >> 8) & 0xff; |
| 483 | data[1] = reg & 0xff; |
| 484 | data[2] = value; |
| 485 | |
| 486 | reg &= 0xff; |
Dimitris Papastamos | db49c14 | 2010-09-22 13:25:47 +0100 | [diff] [blame] | 487 | if (!snd_soc_codec_volatile_register(codec, reg) && |
Dimitris Papastamos | dad8e7a | 2011-01-19 14:53:36 +0000 | [diff] [blame] | 488 | reg < codec->driver->reg_cache_size && |
| 489 | !codec->cache_bypass) { |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 490 | ret = snd_soc_cache_write(codec, reg, value); |
| 491 | if (ret < 0) |
| 492 | return -1; |
| 493 | } |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 494 | |
Mark Brown | a3032b4 | 2010-02-01 18:48:03 +0000 | [diff] [blame] | 495 | if (codec->cache_only) { |
| 496 | codec->cache_sync = 1; |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 497 | return 0; |
Mark Brown | a3032b4 | 2010-02-01 18:48:03 +0000 | [diff] [blame] | 498 | } |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 499 | |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 500 | ret = codec->hw_write(codec->control_data, data, 3); |
| 501 | if (ret == 3) |
| 502 | return 0; |
| 503 | if (ret < 0) |
| 504 | return ret; |
| 505 | else |
| 506 | return -EIO; |
| 507 | } |
| 508 | |
| 509 | #if defined(CONFIG_SPI_MASTER) |
| 510 | static int snd_soc_16_8_spi_write(void *control_data, const char *data, |
| 511 | int len) |
| 512 | { |
| 513 | struct spi_device *spi = control_data; |
| 514 | struct spi_transfer t; |
| 515 | struct spi_message m; |
| 516 | u8 msg[3]; |
| 517 | |
| 518 | if (len <= 0) |
| 519 | return 0; |
| 520 | |
| 521 | msg[0] = data[0]; |
| 522 | msg[1] = data[1]; |
| 523 | msg[2] = data[2]; |
| 524 | |
| 525 | spi_message_init(&m); |
Dimitris Papastamos | 465d7fc | 2010-12-14 15:15:36 +0000 | [diff] [blame] | 526 | memset(&t, 0, sizeof t); |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 527 | |
| 528 | t.tx_buf = &msg[0]; |
| 529 | t.len = len; |
| 530 | |
| 531 | spi_message_add_tail(&t, &m); |
| 532 | spi_sync(spi, &m); |
| 533 | |
| 534 | return len; |
| 535 | } |
| 536 | #else |
| 537 | #define snd_soc_16_8_spi_write NULL |
| 538 | #endif |
| 539 | |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 540 | #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE)) |
| 541 | static unsigned int snd_soc_16_16_read_i2c(struct snd_soc_codec *codec, |
| 542 | unsigned int r) |
| 543 | { |
| 544 | struct i2c_msg xfer[2]; |
| 545 | u16 reg = cpu_to_be16(r); |
| 546 | u16 data; |
| 547 | int ret; |
| 548 | struct i2c_client *client = codec->control_data; |
| 549 | |
| 550 | /* Write register */ |
| 551 | xfer[0].addr = client->addr; |
| 552 | xfer[0].flags = 0; |
| 553 | xfer[0].len = 2; |
| 554 | xfer[0].buf = (u8 *)® |
| 555 | |
| 556 | /* Read data */ |
| 557 | xfer[1].addr = client->addr; |
| 558 | xfer[1].flags = I2C_M_RD; |
| 559 | xfer[1].len = 2; |
| 560 | xfer[1].buf = (u8 *)&data; |
| 561 | |
| 562 | ret = i2c_transfer(client->adapter, xfer, 2); |
| 563 | if (ret != 2) { |
| 564 | dev_err(&client->dev, "i2c_transfer() returned %d\n", ret); |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | return be16_to_cpu(data); |
| 569 | } |
| 570 | #else |
| 571 | #define snd_soc_16_16_read_i2c NULL |
| 572 | #endif |
| 573 | |
| 574 | static unsigned int snd_soc_16_16_read(struct snd_soc_codec *codec, |
| 575 | unsigned int reg) |
| 576 | { |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 577 | int ret; |
| 578 | unsigned int val; |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 579 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 580 | if (reg >= codec->driver->reg_cache_size || |
Dimitris Papastamos | dad8e7a | 2011-01-19 14:53:36 +0000 | [diff] [blame] | 581 | snd_soc_codec_volatile_register(codec, reg) || |
| 582 | codec->cache_bypass) { |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 583 | if (codec->cache_only) |
Dimitris Papastamos | 391d8a0 | 2010-09-21 17:04:07 +0100 | [diff] [blame] | 584 | return -1; |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 585 | |
Dimitris Papastamos | 5aaa062 | 2010-11-08 15:37:07 +0000 | [diff] [blame] | 586 | BUG_ON(!codec->hw_read); |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 587 | return codec->hw_read(codec, reg); |
| 588 | } |
| 589 | |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 590 | ret = snd_soc_cache_read(codec, reg, &val); |
| 591 | if (ret < 0) |
| 592 | return -1; |
| 593 | |
| 594 | return val; |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | static int snd_soc_16_16_write(struct snd_soc_codec *codec, unsigned int reg, |
| 598 | unsigned int value) |
| 599 | { |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 600 | u8 data[4]; |
| 601 | int ret; |
| 602 | |
| 603 | data[0] = (reg >> 8) & 0xff; |
| 604 | data[1] = reg & 0xff; |
| 605 | data[2] = (value >> 8) & 0xff; |
| 606 | data[3] = value & 0xff; |
| 607 | |
Dimitris Papastamos | db49c14 | 2010-09-22 13:25:47 +0100 | [diff] [blame] | 608 | if (!snd_soc_codec_volatile_register(codec, reg) && |
Dimitris Papastamos | dad8e7a | 2011-01-19 14:53:36 +0000 | [diff] [blame] | 609 | reg < codec->driver->reg_cache_size && |
| 610 | !codec->cache_bypass) { |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 611 | ret = snd_soc_cache_write(codec, reg, value); |
| 612 | if (ret < 0) |
| 613 | return -1; |
| 614 | } |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 615 | |
| 616 | if (codec->cache_only) { |
| 617 | codec->cache_sync = 1; |
| 618 | return 0; |
| 619 | } |
| 620 | |
| 621 | ret = codec->hw_write(codec->control_data, data, 4); |
| 622 | if (ret == 4) |
| 623 | return 0; |
| 624 | if (ret < 0) |
| 625 | return ret; |
| 626 | else |
| 627 | return -EIO; |
| 628 | } |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 629 | |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 630 | #if defined(CONFIG_SPI_MASTER) |
| 631 | static int snd_soc_16_16_spi_write(void *control_data, const char *data, |
| 632 | int len) |
| 633 | { |
| 634 | struct spi_device *spi = control_data; |
| 635 | struct spi_transfer t; |
| 636 | struct spi_message m; |
| 637 | u8 msg[4]; |
| 638 | |
| 639 | if (len <= 0) |
| 640 | return 0; |
| 641 | |
| 642 | msg[0] = data[0]; |
| 643 | msg[1] = data[1]; |
| 644 | msg[2] = data[2]; |
| 645 | msg[3] = data[3]; |
| 646 | |
| 647 | spi_message_init(&m); |
Dimitris Papastamos | 465d7fc | 2010-12-14 15:15:36 +0000 | [diff] [blame] | 648 | memset(&t, 0, sizeof t); |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 649 | |
| 650 | t.tx_buf = &msg[0]; |
| 651 | t.len = len; |
| 652 | |
| 653 | spi_message_add_tail(&t, &m); |
| 654 | spi_sync(spi, &m); |
| 655 | |
| 656 | return len; |
| 657 | } |
| 658 | #else |
| 659 | #define snd_soc_16_16_spi_write NULL |
| 660 | #endif |
| 661 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 662 | static struct { |
| 663 | int addr_bits; |
| 664 | int data_bits; |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 665 | int (*write)(struct snd_soc_codec *codec, unsigned int, unsigned int); |
Mark Brown | 27ded04 | 2009-07-10 23:28:16 +0100 | [diff] [blame] | 666 | int (*spi_write)(void *, const char *, int); |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 667 | unsigned int (*read)(struct snd_soc_codec *, unsigned int); |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 668 | unsigned int (*i2c_read)(struct snd_soc_codec *, unsigned int); |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 669 | } io_types[] = { |
Mark Brown | d62ab35 | 2009-09-21 04:21:47 -0700 | [diff] [blame] | 670 | { |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 671 | .addr_bits = 4, .data_bits = 12, |
| 672 | .write = snd_soc_4_12_write, .read = snd_soc_4_12_read, |
| 673 | .spi_write = snd_soc_4_12_spi_write, |
| 674 | }, |
| 675 | { |
Mark Brown | d62ab35 | 2009-09-21 04:21:47 -0700 | [diff] [blame] | 676 | .addr_bits = 7, .data_bits = 9, |
| 677 | .write = snd_soc_7_9_write, .read = snd_soc_7_9_read, |
Barry Song | 8998c89 | 2009-12-31 10:30:34 +0800 | [diff] [blame] | 678 | .spi_write = snd_soc_7_9_spi_write, |
Mark Brown | d62ab35 | 2009-09-21 04:21:47 -0700 | [diff] [blame] | 679 | }, |
| 680 | { |
| 681 | .addr_bits = 8, .data_bits = 8, |
| 682 | .write = snd_soc_8_8_write, .read = snd_soc_8_8_read, |
Cliff Cai | 85dfcdf | 2010-03-18 16:17:00 +0800 | [diff] [blame] | 683 | .i2c_read = snd_soc_8_8_read_i2c, |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 684 | .spi_write = snd_soc_8_8_spi_write, |
Mark Brown | d62ab35 | 2009-09-21 04:21:47 -0700 | [diff] [blame] | 685 | }, |
| 686 | { |
| 687 | .addr_bits = 8, .data_bits = 16, |
| 688 | .write = snd_soc_8_16_write, .read = snd_soc_8_16_read, |
| 689 | .i2c_read = snd_soc_8_16_read_i2c, |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 690 | .spi_write = snd_soc_8_16_spi_write, |
Mark Brown | d62ab35 | 2009-09-21 04:21:47 -0700 | [diff] [blame] | 691 | }, |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 692 | { |
| 693 | .addr_bits = 16, .data_bits = 8, |
| 694 | .write = snd_soc_16_8_write, .read = snd_soc_16_8_read, |
| 695 | .i2c_read = snd_soc_16_8_read_i2c, |
| 696 | .spi_write = snd_soc_16_8_spi_write, |
| 697 | }, |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 698 | { |
| 699 | .addr_bits = 16, .data_bits = 16, |
| 700 | .write = snd_soc_16_16_write, .read = snd_soc_16_16_read, |
| 701 | .i2c_read = snd_soc_16_16_read_i2c, |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 702 | .spi_write = snd_soc_16_16_spi_write, |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 703 | }, |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 704 | }; |
| 705 | |
| 706 | /** |
| 707 | * snd_soc_codec_set_cache_io: Set up standard I/O functions. |
| 708 | * |
| 709 | * @codec: CODEC to configure. |
| 710 | * @type: Type of cache. |
| 711 | * @addr_bits: Number of bits of register address data. |
| 712 | * @data_bits: Number of bits of data per register. |
Mark Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 713 | * @control: Control bus used. |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 714 | * |
| 715 | * Register formats are frequently shared between many I2C and SPI |
| 716 | * devices. In order to promote code reuse the ASoC core provides |
| 717 | * some standard implementations of CODEC read and write operations |
| 718 | * which can be set up using this function. |
| 719 | * |
| 720 | * The caller is responsible for allocating and initialising the |
| 721 | * actual cache. |
| 722 | * |
| 723 | * Note that at present this code cannot be used by CODECs with |
| 724 | * volatile registers. |
| 725 | */ |
| 726 | int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec, |
Mark Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 727 | int addr_bits, int data_bits, |
| 728 | enum snd_soc_control_type control) |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 729 | { |
| 730 | int i; |
| 731 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 732 | for (i = 0; i < ARRAY_SIZE(io_types); i++) |
| 733 | if (io_types[i].addr_bits == addr_bits && |
| 734 | io_types[i].data_bits == data_bits) |
| 735 | break; |
| 736 | if (i == ARRAY_SIZE(io_types)) { |
| 737 | printk(KERN_ERR |
| 738 | "No I/O functions for %d bit address %d bit data\n", |
| 739 | addr_bits, data_bits); |
| 740 | return -EINVAL; |
| 741 | } |
| 742 | |
Mark Brown | c3acec2 | 2010-12-02 16:15:29 +0000 | [diff] [blame] | 743 | codec->write = io_types[i].write; |
| 744 | codec->read = io_types[i].read; |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 745 | |
Mark Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 746 | switch (control) { |
| 747 | case SND_SOC_CUSTOM: |
| 748 | break; |
| 749 | |
| 750 | case SND_SOC_I2C: |
Randy Dunlap | 17244c2 | 2009-08-10 16:04:39 -0700 | [diff] [blame] | 751 | #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE)) |
Mark Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 752 | codec->hw_write = (hw_write_t)i2c_master_send; |
| 753 | #endif |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 754 | if (io_types[i].i2c_read) |
| 755 | codec->hw_read = io_types[i].i2c_read; |
Mark Brown | a6d1434 | 2010-08-12 10:59:15 +0100 | [diff] [blame] | 756 | |
| 757 | codec->control_data = container_of(codec->dev, |
| 758 | struct i2c_client, |
| 759 | dev); |
Mark Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 760 | break; |
| 761 | |
| 762 | case SND_SOC_SPI: |
Mark Brown | 27ded04 | 2009-07-10 23:28:16 +0100 | [diff] [blame] | 763 | if (io_types[i].spi_write) |
| 764 | codec->hw_write = io_types[i].spi_write; |
Mark Brown | a6d1434 | 2010-08-12 10:59:15 +0100 | [diff] [blame] | 765 | |
| 766 | codec->control_data = container_of(codec->dev, |
| 767 | struct spi_device, |
| 768 | dev); |
Mark Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 769 | break; |
| 770 | } |
| 771 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 772 | return 0; |
| 773 | } |
| 774 | EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io); |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 775 | |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 776 | static bool snd_soc_set_cache_val(void *base, unsigned int idx, |
| 777 | unsigned int val, unsigned int word_size) |
| 778 | { |
| 779 | switch (word_size) { |
| 780 | case 1: { |
| 781 | u8 *cache = base; |
| 782 | if (cache[idx] == val) |
| 783 | return true; |
| 784 | cache[idx] = val; |
| 785 | break; |
| 786 | } |
| 787 | case 2: { |
| 788 | u16 *cache = base; |
| 789 | if (cache[idx] == val) |
| 790 | return true; |
| 791 | cache[idx] = val; |
| 792 | break; |
| 793 | } |
| 794 | default: |
| 795 | BUG(); |
| 796 | } |
| 797 | return false; |
| 798 | } |
| 799 | |
| 800 | static unsigned int snd_soc_get_cache_val(const void *base, unsigned int idx, |
| 801 | unsigned int word_size) |
| 802 | { |
| 803 | switch (word_size) { |
| 804 | case 1: { |
| 805 | const u8 *cache = base; |
| 806 | return cache[idx]; |
| 807 | } |
| 808 | case 2: { |
| 809 | const u16 *cache = base; |
| 810 | return cache[idx]; |
| 811 | } |
| 812 | default: |
| 813 | BUG(); |
| 814 | } |
| 815 | /* unreachable */ |
| 816 | return -1; |
| 817 | } |
| 818 | |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 819 | struct snd_soc_rbtree_node { |
| 820 | struct rb_node node; |
| 821 | unsigned int reg; |
| 822 | unsigned int value; |
| 823 | unsigned int defval; |
| 824 | } __attribute__ ((packed)); |
| 825 | |
| 826 | struct snd_soc_rbtree_ctx { |
| 827 | struct rb_root root; |
| 828 | }; |
| 829 | |
| 830 | static struct snd_soc_rbtree_node *snd_soc_rbtree_lookup( |
| 831 | struct rb_root *root, unsigned int reg) |
| 832 | { |
| 833 | struct rb_node *node; |
| 834 | struct snd_soc_rbtree_node *rbnode; |
| 835 | |
| 836 | node = root->rb_node; |
| 837 | while (node) { |
| 838 | rbnode = container_of(node, struct snd_soc_rbtree_node, node); |
| 839 | if (rbnode->reg < reg) |
| 840 | node = node->rb_left; |
| 841 | else if (rbnode->reg > reg) |
| 842 | node = node->rb_right; |
| 843 | else |
| 844 | return rbnode; |
| 845 | } |
| 846 | |
| 847 | return NULL; |
| 848 | } |
| 849 | |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 850 | static int snd_soc_rbtree_insert(struct rb_root *root, |
| 851 | struct snd_soc_rbtree_node *rbnode) |
| 852 | { |
| 853 | struct rb_node **new, *parent; |
| 854 | struct snd_soc_rbtree_node *rbnode_tmp; |
| 855 | |
| 856 | parent = NULL; |
| 857 | new = &root->rb_node; |
| 858 | while (*new) { |
| 859 | rbnode_tmp = container_of(*new, struct snd_soc_rbtree_node, |
| 860 | node); |
| 861 | parent = *new; |
| 862 | if (rbnode_tmp->reg < rbnode->reg) |
| 863 | new = &((*new)->rb_left); |
| 864 | else if (rbnode_tmp->reg > rbnode->reg) |
| 865 | new = &((*new)->rb_right); |
| 866 | else |
| 867 | return 0; |
| 868 | } |
| 869 | |
| 870 | /* insert the node into the rbtree */ |
| 871 | rb_link_node(&rbnode->node, parent, new); |
| 872 | rb_insert_color(&rbnode->node, root); |
| 873 | |
| 874 | return 1; |
| 875 | } |
| 876 | |
| 877 | static int snd_soc_rbtree_cache_sync(struct snd_soc_codec *codec) |
| 878 | { |
| 879 | struct snd_soc_rbtree_ctx *rbtree_ctx; |
| 880 | struct rb_node *node; |
| 881 | struct snd_soc_rbtree_node *rbnode; |
| 882 | unsigned int val; |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 883 | int ret; |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 884 | |
| 885 | rbtree_ctx = codec->reg_cache; |
| 886 | for (node = rb_first(&rbtree_ctx->root); node; node = rb_next(node)) { |
| 887 | rbnode = rb_entry(node, struct snd_soc_rbtree_node, node); |
| 888 | if (rbnode->value == rbnode->defval) |
| 889 | continue; |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 890 | ret = snd_soc_cache_read(codec, rbnode->reg, &val); |
| 891 | if (ret) |
| 892 | return ret; |
Dimitris Papastamos | 9978007 | 2011-01-19 14:53:37 +0000 | [diff] [blame^] | 893 | codec->cache_bypass = 1; |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 894 | ret = snd_soc_write(codec, rbnode->reg, val); |
Dimitris Papastamos | 9978007 | 2011-01-19 14:53:37 +0000 | [diff] [blame^] | 895 | codec->cache_bypass = 0; |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 896 | if (ret) |
| 897 | return ret; |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 898 | dev_dbg(codec->dev, "Synced register %#x, value = %#x\n", |
| 899 | rbnode->reg, val); |
| 900 | } |
| 901 | |
| 902 | return 0; |
| 903 | } |
| 904 | |
| 905 | static int snd_soc_rbtree_cache_write(struct snd_soc_codec *codec, |
| 906 | unsigned int reg, unsigned int value) |
| 907 | { |
| 908 | struct snd_soc_rbtree_ctx *rbtree_ctx; |
| 909 | struct snd_soc_rbtree_node *rbnode; |
| 910 | |
| 911 | rbtree_ctx = codec->reg_cache; |
| 912 | rbnode = snd_soc_rbtree_lookup(&rbtree_ctx->root, reg); |
| 913 | if (rbnode) { |
| 914 | if (rbnode->value == value) |
| 915 | return 0; |
| 916 | rbnode->value = value; |
| 917 | } else { |
| 918 | /* bail out early, no need to create the rbnode yet */ |
| 919 | if (!value) |
| 920 | return 0; |
| 921 | /* |
| 922 | * for uninitialized registers whose value is changed |
| 923 | * from the default zero, create an rbnode and insert |
| 924 | * it into the tree. |
| 925 | */ |
| 926 | rbnode = kzalloc(sizeof *rbnode, GFP_KERNEL); |
| 927 | if (!rbnode) |
| 928 | return -ENOMEM; |
| 929 | rbnode->reg = reg; |
| 930 | rbnode->value = value; |
| 931 | snd_soc_rbtree_insert(&rbtree_ctx->root, rbnode); |
| 932 | } |
| 933 | |
| 934 | return 0; |
| 935 | } |
| 936 | |
| 937 | static int snd_soc_rbtree_cache_read(struct snd_soc_codec *codec, |
| 938 | unsigned int reg, unsigned int *value) |
| 939 | { |
| 940 | struct snd_soc_rbtree_ctx *rbtree_ctx; |
| 941 | struct snd_soc_rbtree_node *rbnode; |
| 942 | |
| 943 | rbtree_ctx = codec->reg_cache; |
| 944 | rbnode = snd_soc_rbtree_lookup(&rbtree_ctx->root, reg); |
| 945 | if (rbnode) { |
| 946 | *value = rbnode->value; |
| 947 | } else { |
| 948 | /* uninitialized registers default to 0 */ |
| 949 | *value = 0; |
| 950 | } |
| 951 | |
| 952 | return 0; |
| 953 | } |
| 954 | |
| 955 | static int snd_soc_rbtree_cache_exit(struct snd_soc_codec *codec) |
| 956 | { |
| 957 | struct rb_node *next; |
| 958 | struct snd_soc_rbtree_ctx *rbtree_ctx; |
| 959 | struct snd_soc_rbtree_node *rbtree_node; |
| 960 | |
| 961 | /* if we've already been called then just return */ |
| 962 | rbtree_ctx = codec->reg_cache; |
| 963 | if (!rbtree_ctx) |
| 964 | return 0; |
| 965 | |
| 966 | /* free up the rbtree */ |
| 967 | next = rb_first(&rbtree_ctx->root); |
| 968 | while (next) { |
| 969 | rbtree_node = rb_entry(next, struct snd_soc_rbtree_node, node); |
| 970 | next = rb_next(&rbtree_node->node); |
| 971 | rb_erase(&rbtree_node->node, &rbtree_ctx->root); |
| 972 | kfree(rbtree_node); |
| 973 | } |
| 974 | |
| 975 | /* release the resources */ |
| 976 | kfree(codec->reg_cache); |
| 977 | codec->reg_cache = NULL; |
| 978 | |
| 979 | return 0; |
| 980 | } |
| 981 | |
| 982 | static int snd_soc_rbtree_cache_init(struct snd_soc_codec *codec) |
| 983 | { |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 984 | struct snd_soc_rbtree_node *rbtree_node; |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 985 | struct snd_soc_rbtree_ctx *rbtree_ctx; |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 986 | unsigned int val; |
| 987 | unsigned int word_size; |
| 988 | int i; |
| 989 | int ret; |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 990 | |
| 991 | codec->reg_cache = kmalloc(sizeof *rbtree_ctx, GFP_KERNEL); |
| 992 | if (!codec->reg_cache) |
| 993 | return -ENOMEM; |
| 994 | |
| 995 | rbtree_ctx = codec->reg_cache; |
| 996 | rbtree_ctx->root = RB_ROOT; |
| 997 | |
Dimitris Papastamos | 3335ddc | 2010-12-02 16:11:05 +0000 | [diff] [blame] | 998 | if (!codec->reg_def_copy) |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 999 | return 0; |
| 1000 | |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 1001 | /* |
| 1002 | * populate the rbtree with the initialized registers. All other |
| 1003 | * registers will be inserted when they are first modified. |
| 1004 | */ |
| 1005 | word_size = codec->driver->reg_word_size; |
| 1006 | for (i = 0; i < codec->driver->reg_cache_size; ++i) { |
| 1007 | val = snd_soc_get_cache_val(codec->reg_def_copy, i, word_size); |
| 1008 | if (!val) |
| 1009 | continue; |
| 1010 | rbtree_node = kzalloc(sizeof *rbtree_node, GFP_KERNEL); |
| 1011 | if (!rbtree_node) { |
| 1012 | ret = -ENOMEM; |
| 1013 | snd_soc_cache_exit(codec); |
| 1014 | break; |
| 1015 | } |
| 1016 | rbtree_node->reg = i; |
| 1017 | rbtree_node->value = val; |
| 1018 | rbtree_node->defval = val; |
| 1019 | snd_soc_rbtree_insert(&rbtree_ctx->root, rbtree_node); |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | return 0; |
| 1023 | } |
| 1024 | |
Mark Brown | 68d44ee | 2010-12-21 17:19:56 +0000 | [diff] [blame] | 1025 | #ifdef CONFIG_SND_SOC_CACHE_LZO |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1026 | struct snd_soc_lzo_ctx { |
| 1027 | void *wmem; |
| 1028 | void *dst; |
| 1029 | const void *src; |
| 1030 | size_t src_len; |
| 1031 | size_t dst_len; |
| 1032 | size_t decompressed_size; |
| 1033 | unsigned long *sync_bmp; |
| 1034 | int sync_bmp_nbits; |
| 1035 | }; |
| 1036 | |
| 1037 | #define LZO_BLOCK_NUM 8 |
| 1038 | static int snd_soc_lzo_block_count(void) |
| 1039 | { |
| 1040 | return LZO_BLOCK_NUM; |
| 1041 | } |
| 1042 | |
| 1043 | static int snd_soc_lzo_prepare(struct snd_soc_lzo_ctx *lzo_ctx) |
| 1044 | { |
| 1045 | lzo_ctx->wmem = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); |
| 1046 | if (!lzo_ctx->wmem) |
| 1047 | return -ENOMEM; |
| 1048 | return 0; |
| 1049 | } |
| 1050 | |
| 1051 | static int snd_soc_lzo_compress(struct snd_soc_lzo_ctx *lzo_ctx) |
| 1052 | { |
| 1053 | size_t compress_size; |
| 1054 | int ret; |
| 1055 | |
| 1056 | ret = lzo1x_1_compress(lzo_ctx->src, lzo_ctx->src_len, |
| 1057 | lzo_ctx->dst, &compress_size, lzo_ctx->wmem); |
| 1058 | if (ret != LZO_E_OK || compress_size > lzo_ctx->dst_len) |
| 1059 | return -EINVAL; |
| 1060 | lzo_ctx->dst_len = compress_size; |
| 1061 | return 0; |
| 1062 | } |
| 1063 | |
| 1064 | static int snd_soc_lzo_decompress(struct snd_soc_lzo_ctx *lzo_ctx) |
| 1065 | { |
| 1066 | size_t dst_len; |
| 1067 | int ret; |
| 1068 | |
| 1069 | dst_len = lzo_ctx->dst_len; |
| 1070 | ret = lzo1x_decompress_safe(lzo_ctx->src, lzo_ctx->src_len, |
| 1071 | lzo_ctx->dst, &dst_len); |
| 1072 | if (ret != LZO_E_OK || dst_len != lzo_ctx->dst_len) |
| 1073 | return -EINVAL; |
| 1074 | return 0; |
| 1075 | } |
| 1076 | |
| 1077 | static int snd_soc_lzo_compress_cache_block(struct snd_soc_codec *codec, |
| 1078 | struct snd_soc_lzo_ctx *lzo_ctx) |
| 1079 | { |
| 1080 | int ret; |
| 1081 | |
| 1082 | lzo_ctx->dst_len = lzo1x_worst_compress(PAGE_SIZE); |
| 1083 | lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL); |
| 1084 | if (!lzo_ctx->dst) { |
| 1085 | lzo_ctx->dst_len = 0; |
| 1086 | return -ENOMEM; |
| 1087 | } |
| 1088 | |
| 1089 | ret = snd_soc_lzo_compress(lzo_ctx); |
| 1090 | if (ret < 0) |
| 1091 | return ret; |
| 1092 | return 0; |
| 1093 | } |
| 1094 | |
| 1095 | static int snd_soc_lzo_decompress_cache_block(struct snd_soc_codec *codec, |
| 1096 | struct snd_soc_lzo_ctx *lzo_ctx) |
| 1097 | { |
| 1098 | int ret; |
| 1099 | |
| 1100 | lzo_ctx->dst_len = lzo_ctx->decompressed_size; |
| 1101 | lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL); |
| 1102 | if (!lzo_ctx->dst) { |
| 1103 | lzo_ctx->dst_len = 0; |
| 1104 | return -ENOMEM; |
| 1105 | } |
| 1106 | |
| 1107 | ret = snd_soc_lzo_decompress(lzo_ctx); |
| 1108 | if (ret < 0) |
| 1109 | return ret; |
| 1110 | return 0; |
| 1111 | } |
| 1112 | |
| 1113 | static inline int snd_soc_lzo_get_blkindex(struct snd_soc_codec *codec, |
| 1114 | unsigned int reg) |
| 1115 | { |
Mark Brown | 001ae4c | 2010-12-02 16:21:08 +0000 | [diff] [blame] | 1116 | const struct snd_soc_codec_driver *codec_drv; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1117 | |
| 1118 | codec_drv = codec->driver; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1119 | return (reg * codec_drv->reg_word_size) / |
Dimitris Papastamos | aea170a | 2011-01-12 10:38:58 +0000 | [diff] [blame] | 1120 | DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count()); |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | static inline int snd_soc_lzo_get_blkpos(struct snd_soc_codec *codec, |
| 1124 | unsigned int reg) |
| 1125 | { |
Mark Brown | 001ae4c | 2010-12-02 16:21:08 +0000 | [diff] [blame] | 1126 | const struct snd_soc_codec_driver *codec_drv; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1127 | |
| 1128 | codec_drv = codec->driver; |
Dimitris Papastamos | aea170a | 2011-01-12 10:38:58 +0000 | [diff] [blame] | 1129 | return reg % (DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count()) / |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1130 | codec_drv->reg_word_size); |
| 1131 | } |
| 1132 | |
| 1133 | static inline int snd_soc_lzo_get_blksize(struct snd_soc_codec *codec) |
| 1134 | { |
Mark Brown | 001ae4c | 2010-12-02 16:21:08 +0000 | [diff] [blame] | 1135 | const struct snd_soc_codec_driver *codec_drv; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1136 | |
| 1137 | codec_drv = codec->driver; |
Dimitris Papastamos | aea170a | 2011-01-12 10:38:58 +0000 | [diff] [blame] | 1138 | return DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count()); |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | static int snd_soc_lzo_cache_sync(struct snd_soc_codec *codec) |
| 1142 | { |
| 1143 | struct snd_soc_lzo_ctx **lzo_blocks; |
| 1144 | unsigned int val; |
| 1145 | int i; |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 1146 | int ret; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1147 | |
| 1148 | lzo_blocks = codec->reg_cache; |
| 1149 | for_each_set_bit(i, lzo_blocks[0]->sync_bmp, lzo_blocks[0]->sync_bmp_nbits) { |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 1150 | ret = snd_soc_cache_read(codec, i, &val); |
| 1151 | if (ret) |
| 1152 | return ret; |
Dimitris Papastamos | 9978007 | 2011-01-19 14:53:37 +0000 | [diff] [blame^] | 1153 | codec->cache_bypass = 1; |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 1154 | ret = snd_soc_write(codec, i, val); |
Dimitris Papastamos | 9978007 | 2011-01-19 14:53:37 +0000 | [diff] [blame^] | 1155 | codec->cache_bypass = 0; |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 1156 | if (ret) |
| 1157 | return ret; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1158 | dev_dbg(codec->dev, "Synced register %#x, value = %#x\n", |
| 1159 | i, val); |
| 1160 | } |
| 1161 | |
| 1162 | return 0; |
| 1163 | } |
| 1164 | |
| 1165 | static int snd_soc_lzo_cache_write(struct snd_soc_codec *codec, |
| 1166 | unsigned int reg, unsigned int value) |
| 1167 | { |
| 1168 | struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks; |
| 1169 | int ret, blkindex, blkpos; |
| 1170 | size_t blksize, tmp_dst_len; |
| 1171 | void *tmp_dst; |
| 1172 | |
| 1173 | /* index of the compressed lzo block */ |
| 1174 | blkindex = snd_soc_lzo_get_blkindex(codec, reg); |
| 1175 | /* register index within the decompressed block */ |
| 1176 | blkpos = snd_soc_lzo_get_blkpos(codec, reg); |
| 1177 | /* size of the compressed block */ |
| 1178 | blksize = snd_soc_lzo_get_blksize(codec); |
| 1179 | lzo_blocks = codec->reg_cache; |
| 1180 | lzo_block = lzo_blocks[blkindex]; |
| 1181 | |
| 1182 | /* save the pointer and length of the compressed block */ |
| 1183 | tmp_dst = lzo_block->dst; |
| 1184 | tmp_dst_len = lzo_block->dst_len; |
| 1185 | |
| 1186 | /* prepare the source to be the compressed block */ |
| 1187 | lzo_block->src = lzo_block->dst; |
| 1188 | lzo_block->src_len = lzo_block->dst_len; |
| 1189 | |
| 1190 | /* decompress the block */ |
| 1191 | ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block); |
| 1192 | if (ret < 0) { |
| 1193 | kfree(lzo_block->dst); |
| 1194 | goto out; |
| 1195 | } |
| 1196 | |
| 1197 | /* write the new value to the cache */ |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 1198 | if (snd_soc_set_cache_val(lzo_block->dst, blkpos, value, |
| 1199 | codec->driver->reg_word_size)) { |
| 1200 | kfree(lzo_block->dst); |
| 1201 | goto out; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | /* prepare the source to be the decompressed block */ |
| 1205 | lzo_block->src = lzo_block->dst; |
| 1206 | lzo_block->src_len = lzo_block->dst_len; |
| 1207 | |
| 1208 | /* compress the block */ |
| 1209 | ret = snd_soc_lzo_compress_cache_block(codec, lzo_block); |
| 1210 | if (ret < 0) { |
| 1211 | kfree(lzo_block->dst); |
| 1212 | kfree(lzo_block->src); |
| 1213 | goto out; |
| 1214 | } |
| 1215 | |
| 1216 | /* set the bit so we know we have to sync this register */ |
| 1217 | set_bit(reg, lzo_block->sync_bmp); |
| 1218 | kfree(tmp_dst); |
| 1219 | kfree(lzo_block->src); |
| 1220 | return 0; |
| 1221 | out: |
| 1222 | lzo_block->dst = tmp_dst; |
| 1223 | lzo_block->dst_len = tmp_dst_len; |
| 1224 | return ret; |
| 1225 | } |
| 1226 | |
| 1227 | static int snd_soc_lzo_cache_read(struct snd_soc_codec *codec, |
| 1228 | unsigned int reg, unsigned int *value) |
| 1229 | { |
| 1230 | struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks; |
| 1231 | int ret, blkindex, blkpos; |
| 1232 | size_t blksize, tmp_dst_len; |
| 1233 | void *tmp_dst; |
| 1234 | |
| 1235 | *value = 0; |
| 1236 | /* index of the compressed lzo block */ |
| 1237 | blkindex = snd_soc_lzo_get_blkindex(codec, reg); |
| 1238 | /* register index within the decompressed block */ |
| 1239 | blkpos = snd_soc_lzo_get_blkpos(codec, reg); |
| 1240 | /* size of the compressed block */ |
| 1241 | blksize = snd_soc_lzo_get_blksize(codec); |
| 1242 | lzo_blocks = codec->reg_cache; |
| 1243 | lzo_block = lzo_blocks[blkindex]; |
| 1244 | |
| 1245 | /* save the pointer and length of the compressed block */ |
| 1246 | tmp_dst = lzo_block->dst; |
| 1247 | tmp_dst_len = lzo_block->dst_len; |
| 1248 | |
| 1249 | /* prepare the source to be the compressed block */ |
| 1250 | lzo_block->src = lzo_block->dst; |
| 1251 | lzo_block->src_len = lzo_block->dst_len; |
| 1252 | |
| 1253 | /* decompress the block */ |
| 1254 | ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block); |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 1255 | if (ret >= 0) |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1256 | /* fetch the value from the cache */ |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 1257 | *value = snd_soc_get_cache_val(lzo_block->dst, blkpos, |
| 1258 | codec->driver->reg_word_size); |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1259 | |
| 1260 | kfree(lzo_block->dst); |
| 1261 | /* restore the pointer and length of the compressed block */ |
| 1262 | lzo_block->dst = tmp_dst; |
| 1263 | lzo_block->dst_len = tmp_dst_len; |
| 1264 | return 0; |
| 1265 | } |
| 1266 | |
| 1267 | static int snd_soc_lzo_cache_exit(struct snd_soc_codec *codec) |
| 1268 | { |
| 1269 | struct snd_soc_lzo_ctx **lzo_blocks; |
| 1270 | int i, blkcount; |
| 1271 | |
| 1272 | lzo_blocks = codec->reg_cache; |
| 1273 | if (!lzo_blocks) |
| 1274 | return 0; |
| 1275 | |
| 1276 | blkcount = snd_soc_lzo_block_count(); |
| 1277 | /* |
| 1278 | * the pointer to the bitmap used for syncing the cache |
| 1279 | * is shared amongst all lzo_blocks. Ensure it is freed |
| 1280 | * only once. |
| 1281 | */ |
| 1282 | if (lzo_blocks[0]) |
| 1283 | kfree(lzo_blocks[0]->sync_bmp); |
| 1284 | for (i = 0; i < blkcount; ++i) { |
| 1285 | if (lzo_blocks[i]) { |
| 1286 | kfree(lzo_blocks[i]->wmem); |
| 1287 | kfree(lzo_blocks[i]->dst); |
| 1288 | } |
| 1289 | /* each lzo_block is a pointer returned by kmalloc or NULL */ |
| 1290 | kfree(lzo_blocks[i]); |
| 1291 | } |
| 1292 | kfree(lzo_blocks); |
| 1293 | codec->reg_cache = NULL; |
| 1294 | return 0; |
| 1295 | } |
| 1296 | |
| 1297 | static int snd_soc_lzo_cache_init(struct snd_soc_codec *codec) |
| 1298 | { |
| 1299 | struct snd_soc_lzo_ctx **lzo_blocks; |
Dimitris Papastamos | aea170a | 2011-01-12 10:38:58 +0000 | [diff] [blame] | 1300 | size_t bmp_size; |
Mark Brown | 001ae4c | 2010-12-02 16:21:08 +0000 | [diff] [blame] | 1301 | const struct snd_soc_codec_driver *codec_drv; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1302 | int ret, tofree, i, blksize, blkcount; |
| 1303 | const char *p, *end; |
| 1304 | unsigned long *sync_bmp; |
| 1305 | |
| 1306 | ret = 0; |
| 1307 | codec_drv = codec->driver; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1308 | |
| 1309 | /* |
| 1310 | * If we have not been given a default register cache |
| 1311 | * then allocate a dummy zero-ed out region, compress it |
| 1312 | * and remember to free it afterwards. |
| 1313 | */ |
| 1314 | tofree = 0; |
Dimitris Papastamos | 3335ddc | 2010-12-02 16:11:05 +0000 | [diff] [blame] | 1315 | if (!codec->reg_def_copy) |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1316 | tofree = 1; |
| 1317 | |
Dimitris Papastamos | 3335ddc | 2010-12-02 16:11:05 +0000 | [diff] [blame] | 1318 | if (!codec->reg_def_copy) { |
Dimitris Papastamos | aea170a | 2011-01-12 10:38:58 +0000 | [diff] [blame] | 1319 | codec->reg_def_copy = kzalloc(codec->reg_size, GFP_KERNEL); |
Dimitris Papastamos | 3335ddc | 2010-12-02 16:11:05 +0000 | [diff] [blame] | 1320 | if (!codec->reg_def_copy) |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1321 | return -ENOMEM; |
| 1322 | } |
| 1323 | |
| 1324 | blkcount = snd_soc_lzo_block_count(); |
| 1325 | codec->reg_cache = kzalloc(blkcount * sizeof *lzo_blocks, |
| 1326 | GFP_KERNEL); |
| 1327 | if (!codec->reg_cache) { |
| 1328 | ret = -ENOMEM; |
| 1329 | goto err_tofree; |
| 1330 | } |
| 1331 | lzo_blocks = codec->reg_cache; |
| 1332 | |
| 1333 | /* |
| 1334 | * allocate a bitmap to be used when syncing the cache with |
| 1335 | * the hardware. Each time a register is modified, the corresponding |
| 1336 | * bit is set in the bitmap, so we know that we have to sync |
| 1337 | * that register. |
| 1338 | */ |
| 1339 | bmp_size = codec_drv->reg_cache_size; |
Dimitris Papastamos | 465d7fc | 2010-12-14 15:15:36 +0000 | [diff] [blame] | 1340 | sync_bmp = kmalloc(BITS_TO_LONGS(bmp_size) * sizeof(long), |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1341 | GFP_KERNEL); |
| 1342 | if (!sync_bmp) { |
| 1343 | ret = -ENOMEM; |
| 1344 | goto err; |
| 1345 | } |
Dimitris Papastamos | 09c74a9 | 2010-11-29 11:43:33 +0000 | [diff] [blame] | 1346 | bitmap_zero(sync_bmp, bmp_size); |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1347 | |
| 1348 | /* allocate the lzo blocks and initialize them */ |
| 1349 | for (i = 0; i < blkcount; ++i) { |
| 1350 | lzo_blocks[i] = kzalloc(sizeof **lzo_blocks, |
| 1351 | GFP_KERNEL); |
| 1352 | if (!lzo_blocks[i]) { |
| 1353 | kfree(sync_bmp); |
| 1354 | ret = -ENOMEM; |
| 1355 | goto err; |
| 1356 | } |
| 1357 | lzo_blocks[i]->sync_bmp = sync_bmp; |
Dimitris Papastamos | 04f8fd1 | 2011-01-11 11:24:02 +0000 | [diff] [blame] | 1358 | lzo_blocks[i]->sync_bmp_nbits = bmp_size; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1359 | /* alloc the working space for the compressed block */ |
| 1360 | ret = snd_soc_lzo_prepare(lzo_blocks[i]); |
| 1361 | if (ret < 0) |
| 1362 | goto err; |
| 1363 | } |
| 1364 | |
| 1365 | blksize = snd_soc_lzo_get_blksize(codec); |
Dimitris Papastamos | 3335ddc | 2010-12-02 16:11:05 +0000 | [diff] [blame] | 1366 | p = codec->reg_def_copy; |
Dimitris Papastamos | aea170a | 2011-01-12 10:38:58 +0000 | [diff] [blame] | 1367 | end = codec->reg_def_copy + codec->reg_size; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1368 | /* compress the register map and fill the lzo blocks */ |
| 1369 | for (i = 0; i < blkcount; ++i, p += blksize) { |
| 1370 | lzo_blocks[i]->src = p; |
| 1371 | if (p + blksize > end) |
| 1372 | lzo_blocks[i]->src_len = end - p; |
| 1373 | else |
| 1374 | lzo_blocks[i]->src_len = blksize; |
| 1375 | ret = snd_soc_lzo_compress_cache_block(codec, |
| 1376 | lzo_blocks[i]); |
| 1377 | if (ret < 0) |
| 1378 | goto err; |
| 1379 | lzo_blocks[i]->decompressed_size = |
| 1380 | lzo_blocks[i]->src_len; |
| 1381 | } |
| 1382 | |
Dimitris Papastamos | 3335ddc | 2010-12-02 16:11:05 +0000 | [diff] [blame] | 1383 | if (tofree) { |
| 1384 | kfree(codec->reg_def_copy); |
| 1385 | codec->reg_def_copy = NULL; |
| 1386 | } |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1387 | return 0; |
| 1388 | err: |
| 1389 | snd_soc_cache_exit(codec); |
| 1390 | err_tofree: |
Dimitris Papastamos | 3335ddc | 2010-12-02 16:11:05 +0000 | [diff] [blame] | 1391 | if (tofree) { |
| 1392 | kfree(codec->reg_def_copy); |
| 1393 | codec->reg_def_copy = NULL; |
| 1394 | } |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1395 | return ret; |
| 1396 | } |
Mark Brown | 68d44ee | 2010-12-21 17:19:56 +0000 | [diff] [blame] | 1397 | #endif |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1398 | |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1399 | static int snd_soc_flat_cache_sync(struct snd_soc_codec *codec) |
| 1400 | { |
| 1401 | int i; |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 1402 | int ret; |
Mark Brown | 001ae4c | 2010-12-02 16:21:08 +0000 | [diff] [blame] | 1403 | const struct snd_soc_codec_driver *codec_drv; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1404 | unsigned int val; |
| 1405 | |
| 1406 | codec_drv = codec->driver; |
| 1407 | for (i = 0; i < codec_drv->reg_cache_size; ++i) { |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 1408 | ret = snd_soc_cache_read(codec, i, &val); |
| 1409 | if (ret) |
| 1410 | return ret; |
Dimitris Papastamos | d779fce | 2011-01-12 10:22:28 +0000 | [diff] [blame] | 1411 | if (codec->reg_def_copy) |
| 1412 | if (snd_soc_get_cache_val(codec->reg_def_copy, |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 1413 | i, codec_drv->reg_word_size) == val) |
| 1414 | continue; |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 1415 | ret = snd_soc_write(codec, i, val); |
| 1416 | if (ret) |
| 1417 | return ret; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1418 | dev_dbg(codec->dev, "Synced register %#x, value = %#x\n", |
| 1419 | i, val); |
| 1420 | } |
| 1421 | return 0; |
| 1422 | } |
| 1423 | |
| 1424 | static int snd_soc_flat_cache_write(struct snd_soc_codec *codec, |
| 1425 | unsigned int reg, unsigned int value) |
| 1426 | { |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 1427 | snd_soc_set_cache_val(codec->reg_cache, reg, value, |
| 1428 | codec->driver->reg_word_size); |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1429 | return 0; |
| 1430 | } |
| 1431 | |
| 1432 | static int snd_soc_flat_cache_read(struct snd_soc_codec *codec, |
| 1433 | unsigned int reg, unsigned int *value) |
| 1434 | { |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 1435 | *value = snd_soc_get_cache_val(codec->reg_cache, reg, |
| 1436 | codec->driver->reg_word_size); |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1437 | return 0; |
| 1438 | } |
| 1439 | |
| 1440 | static int snd_soc_flat_cache_exit(struct snd_soc_codec *codec) |
| 1441 | { |
| 1442 | if (!codec->reg_cache) |
| 1443 | return 0; |
| 1444 | kfree(codec->reg_cache); |
| 1445 | codec->reg_cache = NULL; |
| 1446 | return 0; |
| 1447 | } |
| 1448 | |
| 1449 | static int snd_soc_flat_cache_init(struct snd_soc_codec *codec) |
| 1450 | { |
Mark Brown | 001ae4c | 2010-12-02 16:21:08 +0000 | [diff] [blame] | 1451 | const struct snd_soc_codec_driver *codec_drv; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1452 | |
| 1453 | codec_drv = codec->driver; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1454 | |
Dimitris Papastamos | d779fce | 2011-01-12 10:22:28 +0000 | [diff] [blame] | 1455 | if (codec->reg_def_copy) |
| 1456 | codec->reg_cache = kmemdup(codec->reg_def_copy, |
Dimitris Papastamos | aea170a | 2011-01-12 10:38:58 +0000 | [diff] [blame] | 1457 | codec->reg_size, GFP_KERNEL); |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1458 | else |
Dimitris Papastamos | aea170a | 2011-01-12 10:38:58 +0000 | [diff] [blame] | 1459 | codec->reg_cache = kzalloc(codec->reg_size, GFP_KERNEL); |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1460 | if (!codec->reg_cache) |
| 1461 | return -ENOMEM; |
| 1462 | |
| 1463 | return 0; |
| 1464 | } |
| 1465 | |
| 1466 | /* an array of all supported compression types */ |
| 1467 | static const struct snd_soc_cache_ops cache_types[] = { |
Mark Brown | be4fcdd | 2010-12-21 17:09:48 +0000 | [diff] [blame] | 1468 | /* Flat *must* be the first entry for fallback */ |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1469 | { |
Dimitris Papastamos | df0701b | 2010-11-29 10:54:28 +0000 | [diff] [blame] | 1470 | .id = SND_SOC_FLAT_COMPRESSION, |
Dimitris Papastamos | 0d735ea | 2010-12-06 09:51:57 +0000 | [diff] [blame] | 1471 | .name = "flat", |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1472 | .init = snd_soc_flat_cache_init, |
| 1473 | .exit = snd_soc_flat_cache_exit, |
| 1474 | .read = snd_soc_flat_cache_read, |
| 1475 | .write = snd_soc_flat_cache_write, |
| 1476 | .sync = snd_soc_flat_cache_sync |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1477 | }, |
Mark Brown | 68d44ee | 2010-12-21 17:19:56 +0000 | [diff] [blame] | 1478 | #ifdef CONFIG_SND_SOC_CACHE_LZO |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1479 | { |
| 1480 | .id = SND_SOC_LZO_COMPRESSION, |
Dimitris Papastamos | 0d735ea | 2010-12-06 09:51:57 +0000 | [diff] [blame] | 1481 | .name = "LZO", |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1482 | .init = snd_soc_lzo_cache_init, |
| 1483 | .exit = snd_soc_lzo_cache_exit, |
| 1484 | .read = snd_soc_lzo_cache_read, |
| 1485 | .write = snd_soc_lzo_cache_write, |
| 1486 | .sync = snd_soc_lzo_cache_sync |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 1487 | }, |
Mark Brown | 68d44ee | 2010-12-21 17:19:56 +0000 | [diff] [blame] | 1488 | #endif |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 1489 | { |
| 1490 | .id = SND_SOC_RBTREE_COMPRESSION, |
Dimitris Papastamos | 0d735ea | 2010-12-06 09:51:57 +0000 | [diff] [blame] | 1491 | .name = "rbtree", |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 1492 | .init = snd_soc_rbtree_cache_init, |
| 1493 | .exit = snd_soc_rbtree_cache_exit, |
| 1494 | .read = snd_soc_rbtree_cache_read, |
| 1495 | .write = snd_soc_rbtree_cache_write, |
| 1496 | .sync = snd_soc_rbtree_cache_sync |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1497 | } |
| 1498 | }; |
| 1499 | |
| 1500 | int snd_soc_cache_init(struct snd_soc_codec *codec) |
| 1501 | { |
| 1502 | int i; |
| 1503 | |
| 1504 | for (i = 0; i < ARRAY_SIZE(cache_types); ++i) |
Dimitris Papastamos | 23bbce3 | 2010-12-02 14:53:01 +0000 | [diff] [blame] | 1505 | if (cache_types[i].id == codec->compress_type) |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1506 | break; |
Mark Brown | be4fcdd | 2010-12-21 17:09:48 +0000 | [diff] [blame] | 1507 | |
| 1508 | /* Fall back to flat compression */ |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1509 | if (i == ARRAY_SIZE(cache_types)) { |
Mark Brown | be4fcdd | 2010-12-21 17:09:48 +0000 | [diff] [blame] | 1510 | dev_warn(codec->dev, "Could not match compress type: %d\n", |
| 1511 | codec->compress_type); |
| 1512 | i = 0; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | mutex_init(&codec->cache_rw_mutex); |
| 1516 | codec->cache_ops = &cache_types[i]; |
| 1517 | |
Dimitris Papastamos | 0d735ea | 2010-12-06 09:51:57 +0000 | [diff] [blame] | 1518 | if (codec->cache_ops->init) { |
| 1519 | if (codec->cache_ops->name) |
| 1520 | dev_dbg(codec->dev, "Initializing %s cache for %s codec\n", |
| 1521 | codec->cache_ops->name, codec->name); |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1522 | return codec->cache_ops->init(codec); |
Dimitris Papastamos | 0d735ea | 2010-12-06 09:51:57 +0000 | [diff] [blame] | 1523 | } |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1524 | return -EINVAL; |
| 1525 | } |
| 1526 | |
| 1527 | /* |
| 1528 | * NOTE: keep in mind that this function might be called |
| 1529 | * multiple times. |
| 1530 | */ |
| 1531 | int snd_soc_cache_exit(struct snd_soc_codec *codec) |
| 1532 | { |
Dimitris Papastamos | 0d735ea | 2010-12-06 09:51:57 +0000 | [diff] [blame] | 1533 | if (codec->cache_ops && codec->cache_ops->exit) { |
| 1534 | if (codec->cache_ops->name) |
| 1535 | dev_dbg(codec->dev, "Destroying %s cache for %s codec\n", |
| 1536 | codec->cache_ops->name, codec->name); |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1537 | return codec->cache_ops->exit(codec); |
Dimitris Papastamos | 0d735ea | 2010-12-06 09:51:57 +0000 | [diff] [blame] | 1538 | } |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1539 | return -EINVAL; |
| 1540 | } |
| 1541 | |
| 1542 | /** |
| 1543 | * snd_soc_cache_read: Fetch the value of a given register from the cache. |
| 1544 | * |
| 1545 | * @codec: CODEC to configure. |
| 1546 | * @reg: The register index. |
| 1547 | * @value: The value to be returned. |
| 1548 | */ |
| 1549 | int snd_soc_cache_read(struct snd_soc_codec *codec, |
| 1550 | unsigned int reg, unsigned int *value) |
| 1551 | { |
| 1552 | int ret; |
| 1553 | |
| 1554 | mutex_lock(&codec->cache_rw_mutex); |
| 1555 | |
| 1556 | if (value && codec->cache_ops && codec->cache_ops->read) { |
| 1557 | ret = codec->cache_ops->read(codec, reg, value); |
| 1558 | mutex_unlock(&codec->cache_rw_mutex); |
| 1559 | return ret; |
| 1560 | } |
| 1561 | |
| 1562 | mutex_unlock(&codec->cache_rw_mutex); |
| 1563 | return -EINVAL; |
| 1564 | } |
| 1565 | EXPORT_SYMBOL_GPL(snd_soc_cache_read); |
| 1566 | |
| 1567 | /** |
| 1568 | * snd_soc_cache_write: Set the value of a given register in the cache. |
| 1569 | * |
| 1570 | * @codec: CODEC to configure. |
| 1571 | * @reg: The register index. |
| 1572 | * @value: The new register value. |
| 1573 | */ |
| 1574 | int snd_soc_cache_write(struct snd_soc_codec *codec, |
| 1575 | unsigned int reg, unsigned int value) |
| 1576 | { |
| 1577 | int ret; |
| 1578 | |
| 1579 | mutex_lock(&codec->cache_rw_mutex); |
| 1580 | |
| 1581 | if (codec->cache_ops && codec->cache_ops->write) { |
| 1582 | ret = codec->cache_ops->write(codec, reg, value); |
| 1583 | mutex_unlock(&codec->cache_rw_mutex); |
| 1584 | return ret; |
| 1585 | } |
| 1586 | |
| 1587 | mutex_unlock(&codec->cache_rw_mutex); |
| 1588 | return -EINVAL; |
| 1589 | } |
| 1590 | EXPORT_SYMBOL_GPL(snd_soc_cache_write); |
| 1591 | |
| 1592 | /** |
| 1593 | * snd_soc_cache_sync: Sync the register cache with the hardware. |
| 1594 | * |
| 1595 | * @codec: CODEC to configure. |
| 1596 | * |
| 1597 | * Any registers that should not be synced should be marked as |
| 1598 | * volatile. In general drivers can choose not to use the provided |
| 1599 | * syncing functionality if they so require. |
| 1600 | */ |
| 1601 | int snd_soc_cache_sync(struct snd_soc_codec *codec) |
| 1602 | { |
| 1603 | int ret; |
| 1604 | |
| 1605 | if (!codec->cache_sync) { |
| 1606 | return 0; |
| 1607 | } |
| 1608 | |
| 1609 | if (codec->cache_ops && codec->cache_ops->sync) { |
Dimitris Papastamos | 0d735ea | 2010-12-06 09:51:57 +0000 | [diff] [blame] | 1610 | if (codec->cache_ops->name) |
| 1611 | dev_dbg(codec->dev, "Syncing %s cache for %s codec\n", |
| 1612 | codec->cache_ops->name, codec->name); |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1613 | ret = codec->cache_ops->sync(codec); |
| 1614 | if (!ret) |
| 1615 | codec->cache_sync = 0; |
| 1616 | return ret; |
| 1617 | } |
| 1618 | |
| 1619 | return -EINVAL; |
| 1620 | } |
| 1621 | EXPORT_SYMBOL_GPL(snd_soc_cache_sync); |
Dimitris Papastamos | 066d16c | 2011-01-13 12:20:36 +0000 | [diff] [blame] | 1622 | |
| 1623 | static int snd_soc_get_reg_access_index(struct snd_soc_codec *codec, |
| 1624 | unsigned int reg) |
| 1625 | { |
| 1626 | const struct snd_soc_codec_driver *codec_drv; |
| 1627 | unsigned int min, max, index; |
| 1628 | |
| 1629 | codec_drv = codec->driver; |
| 1630 | min = 0; |
| 1631 | max = codec_drv->reg_access_size - 1; |
| 1632 | do { |
| 1633 | index = (min + max) / 2; |
| 1634 | if (codec_drv->reg_access_default[index].reg == reg) |
| 1635 | return index; |
| 1636 | if (codec_drv->reg_access_default[index].reg < reg) |
| 1637 | min = index + 1; |
| 1638 | else |
| 1639 | max = index; |
| 1640 | } while (min <= max); |
| 1641 | return -1; |
| 1642 | } |
| 1643 | |
| 1644 | int snd_soc_default_volatile_register(struct snd_soc_codec *codec, |
| 1645 | unsigned int reg) |
| 1646 | { |
| 1647 | int index; |
| 1648 | |
| 1649 | if (reg >= codec->driver->reg_cache_size) |
| 1650 | return 1; |
| 1651 | index = snd_soc_get_reg_access_index(codec, reg); |
| 1652 | if (index < 0) |
| 1653 | return 0; |
| 1654 | return codec->driver->reg_access_default[index].vol; |
| 1655 | } |
| 1656 | EXPORT_SYMBOL_GPL(snd_soc_default_volatile_register); |
| 1657 | |
| 1658 | int snd_soc_default_readable_register(struct snd_soc_codec *codec, |
| 1659 | unsigned int reg) |
| 1660 | { |
| 1661 | int index; |
| 1662 | |
| 1663 | if (reg >= codec->driver->reg_cache_size) |
| 1664 | return 1; |
| 1665 | index = snd_soc_get_reg_access_index(codec, reg); |
| 1666 | if (index < 0) |
| 1667 | return 0; |
| 1668 | return codec->driver->reg_access_default[index].read; |
| 1669 | } |
| 1670 | EXPORT_SYMBOL_GPL(snd_soc_default_readable_register); |