Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 1 | /* |
| 2 | * soc-io.c -- ASoC register I/O helpers |
| 3 | * |
| 4 | * Copyright 2009-2011 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 | |
| 14 | #include <linux/i2c.h> |
| 15 | #include <linux/spi/spi.h> |
Mark Brown | be3ea3b | 2011-06-13 19:35:29 +0100 | [diff] [blame] | 16 | #include <linux/regmap.h> |
Paul Gortmaker | d81a6d7 | 2011-09-22 09:34:58 -0400 | [diff] [blame] | 17 | #include <linux/export.h> |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 18 | #include <sound/soc.h> |
| 19 | |
| 20 | #include <trace/events/asoc.h> |
| 21 | |
Lars-Peter Clausen | e2c330b | 2014-04-22 13:23:13 +0200 | [diff] [blame^] | 22 | /** |
| 23 | * snd_soc_component_read() - Read register value |
| 24 | * @component: Component to read from |
| 25 | * @reg: Register to read |
| 26 | * @val: Pointer to where the read value is stored |
| 27 | * |
| 28 | * Return: 0 on success, a negative error code otherwise. |
| 29 | */ |
| 30 | int snd_soc_component_read(struct snd_soc_component *component, |
| 31 | unsigned int reg, unsigned int *val) |
Lars-Peter Clausen | 96241c83 | 2014-03-18 09:02:07 +0100 | [diff] [blame] | 32 | { |
Lars-Peter Clausen | e2c330b | 2014-04-22 13:23:13 +0200 | [diff] [blame^] | 33 | int ret; |
Lars-Peter Clausen | 96241c83 | 2014-03-18 09:02:07 +0100 | [diff] [blame] | 34 | |
Lars-Peter Clausen | e2c330b | 2014-04-22 13:23:13 +0200 | [diff] [blame^] | 35 | if (component->regmap) |
| 36 | ret = regmap_read(component->regmap, reg, val); |
| 37 | else if (component->read) |
| 38 | ret = component->read(component, reg, val); |
| 39 | else |
| 40 | ret = -EIO; |
| 41 | |
| 42 | dev_dbg(component->dev, "read %x => %x\n", reg, *val); |
Lars-Peter Clausen | 96241c83 | 2014-03-18 09:02:07 +0100 | [diff] [blame] | 43 | |
| 44 | return ret; |
| 45 | } |
Lars-Peter Clausen | e2c330b | 2014-04-22 13:23:13 +0200 | [diff] [blame^] | 46 | EXPORT_SYMBOL_GPL(snd_soc_component_read); |
| 47 | |
| 48 | /** |
| 49 | * snd_soc_component_write() - Write register value |
| 50 | * @component: Component to write to |
| 51 | * @reg: Register to write |
| 52 | * @val: Value to write to the register |
| 53 | * |
| 54 | * Return: 0 on success, a negative error code otherwise. |
| 55 | */ |
| 56 | int snd_soc_component_write(struct snd_soc_component *component, |
| 57 | unsigned int reg, unsigned int val) |
| 58 | { |
| 59 | dev_dbg(component->dev, "write %x = %x\n", reg, val); |
| 60 | |
| 61 | if (component->regmap) |
| 62 | return regmap_write(component->regmap, reg, val); |
| 63 | else if (component->write) |
| 64 | return component->write(component, reg, val); |
| 65 | else |
| 66 | return -EIO; |
| 67 | } |
| 68 | EXPORT_SYMBOL_GPL(snd_soc_component_write); |
| 69 | |
| 70 | static int snd_soc_component_update_bits_legacy( |
| 71 | struct snd_soc_component *component, unsigned int reg, |
| 72 | unsigned int mask, unsigned int val, bool *change) |
| 73 | { |
| 74 | unsigned int old, new; |
| 75 | int ret; |
| 76 | |
| 77 | if (!component->read || !component->write) |
| 78 | return -EIO; |
| 79 | |
| 80 | mutex_lock(&component->io_mutex); |
| 81 | |
| 82 | ret = component->read(component, reg, &old); |
| 83 | if (ret < 0) |
| 84 | goto out_unlock; |
| 85 | |
| 86 | new = (old & ~mask) | (val & mask); |
| 87 | *change = old != new; |
| 88 | if (*change) |
| 89 | ret = component->write(component, reg, new); |
| 90 | out_unlock: |
| 91 | mutex_unlock(&component->io_mutex); |
| 92 | |
| 93 | return ret; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * snd_soc_component_update_bits() - Perform read/modify/write cycle |
| 98 | * @component: Component to update |
| 99 | * @reg: Register to update |
| 100 | * @mask: Mask that specifies which bits to update |
| 101 | * @val: New value for the bits specified by mask |
| 102 | * |
| 103 | * Return: 1 if the operation was successful and the value of the register |
| 104 | * changed, 0 if the operation was successful, but the value did not change. |
| 105 | * Returns a negative error code otherwise. |
| 106 | */ |
| 107 | int snd_soc_component_update_bits(struct snd_soc_component *component, |
| 108 | unsigned int reg, unsigned int mask, unsigned int val) |
| 109 | { |
| 110 | bool change; |
| 111 | int ret; |
| 112 | |
| 113 | if (component->regmap) |
| 114 | ret = regmap_update_bits_check(component->regmap, reg, mask, |
| 115 | val, &change); |
| 116 | else |
| 117 | ret = snd_soc_component_update_bits_legacy(component, reg, |
| 118 | mask, val, &change); |
| 119 | |
| 120 | if (ret < 0) |
| 121 | return ret; |
| 122 | return change; |
| 123 | } |
| 124 | EXPORT_SYMBOL_GPL(snd_soc_component_update_bits); |
| 125 | |
| 126 | /** |
| 127 | * snd_soc_component_update_bits_async() - Perform asynchronous |
| 128 | * read/modify/write cycle |
| 129 | * @component: Component to update |
| 130 | * @reg: Register to update |
| 131 | * @mask: Mask that specifies which bits to update |
| 132 | * @val: New value for the bits specified by mask |
| 133 | * |
| 134 | * This function is similar to snd_soc_component_update_bits(), but the update |
| 135 | * operation is scheduled asynchronously. This means it may not be completed |
| 136 | * when the function returns. To make sure that all scheduled updates have been |
| 137 | * completed snd_soc_component_async_complete() must be called. |
| 138 | * |
| 139 | * Return: 1 if the operation was successful and the value of the register |
| 140 | * changed, 0 if the operation was successful, but the value did not change. |
| 141 | * Returns a negative error code otherwise. |
| 142 | */ |
| 143 | int snd_soc_component_update_bits_async(struct snd_soc_component *component, |
| 144 | unsigned int reg, unsigned int mask, unsigned int val) |
| 145 | { |
| 146 | bool change; |
| 147 | int ret; |
| 148 | |
| 149 | if (component->regmap) |
| 150 | ret = regmap_update_bits_check_async(component->regmap, reg, |
| 151 | mask, val, &change); |
| 152 | else |
| 153 | ret = snd_soc_component_update_bits_legacy(component, reg, |
| 154 | mask, val, &change); |
| 155 | |
| 156 | if (ret < 0) |
| 157 | return ret; |
| 158 | return change; |
| 159 | } |
| 160 | EXPORT_SYMBOL_GPL(snd_soc_component_update_bits_async); |
| 161 | |
| 162 | /** |
| 163 | * snd_soc_component_async_complete() - Ensure asynchronous I/O has completed |
| 164 | * @component: Component for which to wait |
| 165 | * |
| 166 | * This function blocks until all asynchronous I/O which has previously been |
| 167 | * scheduled using snd_soc_component_update_bits_async() has completed. |
| 168 | */ |
| 169 | void snd_soc_component_async_complete(struct snd_soc_component *component) |
| 170 | { |
| 171 | if (component->regmap) |
| 172 | regmap_async_complete(component->regmap); |
| 173 | } |
| 174 | EXPORT_SYMBOL_GPL(snd_soc_component_async_complete); |
| 175 | |
| 176 | /** |
| 177 | * snd_soc_component_test_bits - Test register for change |
| 178 | * @component: component |
| 179 | * @reg: Register to test |
| 180 | * @mask: Mask that specifies which bits to test |
| 181 | * @value: Value to test against |
| 182 | * |
| 183 | * Tests a register with a new value and checks if the new value is |
| 184 | * different from the old value. |
| 185 | * |
| 186 | * Return: 1 for change, otherwise 0. |
| 187 | */ |
| 188 | int snd_soc_component_test_bits(struct snd_soc_component *component, |
| 189 | unsigned int reg, unsigned int mask, unsigned int value) |
| 190 | { |
| 191 | unsigned int old, new; |
| 192 | int ret; |
| 193 | |
| 194 | ret = snd_soc_component_read(component, reg, &old); |
| 195 | if (ret < 0) |
| 196 | return ret; |
| 197 | new = (old & ~mask) | value; |
| 198 | return old != new; |
| 199 | } |
| 200 | EXPORT_SYMBOL_GPL(snd_soc_component_test_bits); |
| 201 | |
| 202 | unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg) |
| 203 | { |
| 204 | unsigned int val; |
| 205 | int ret; |
| 206 | |
| 207 | ret = snd_soc_component_read(&codec->component, reg, &val); |
| 208 | if (ret < 0) |
| 209 | return -1; |
| 210 | trace_snd_soc_reg_read(codec, reg, val); |
| 211 | |
| 212 | return val; |
| 213 | } |
Lars-Peter Clausen | 96241c83 | 2014-03-18 09:02:07 +0100 | [diff] [blame] | 214 | EXPORT_SYMBOL_GPL(snd_soc_read); |
| 215 | |
Lars-Peter Clausen | ab2874a | 2014-04-19 10:43:57 +0200 | [diff] [blame] | 216 | int snd_soc_write(struct snd_soc_codec *codec, unsigned int reg, |
| 217 | unsigned int val) |
Lars-Peter Clausen | 96241c83 | 2014-03-18 09:02:07 +0100 | [diff] [blame] | 218 | { |
Lars-Peter Clausen | 96241c83 | 2014-03-18 09:02:07 +0100 | [diff] [blame] | 219 | trace_snd_soc_reg_write(codec, reg, val); |
Lars-Peter Clausen | e2c330b | 2014-04-22 13:23:13 +0200 | [diff] [blame^] | 220 | return snd_soc_component_write(&codec->component, reg, val); |
Lars-Peter Clausen | 96241c83 | 2014-03-18 09:02:07 +0100 | [diff] [blame] | 221 | } |
| 222 | EXPORT_SYMBOL_GPL(snd_soc_write); |
| 223 | |
| 224 | /** |
| 225 | * snd_soc_update_bits - update codec register bits |
| 226 | * @codec: audio codec |
| 227 | * @reg: codec register |
| 228 | * @mask: register mask |
| 229 | * @value: new value |
| 230 | * |
| 231 | * Writes new register value. |
| 232 | * |
| 233 | * Returns 1 for change, 0 for no change, or negative error code. |
| 234 | */ |
Mark Brown | aa0258a | 2014-04-14 17:42:28 +0100 | [diff] [blame] | 235 | int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned int reg, |
Lars-Peter Clausen | 96241c83 | 2014-03-18 09:02:07 +0100 | [diff] [blame] | 236 | unsigned int mask, unsigned int value) |
| 237 | { |
Lars-Peter Clausen | e2c330b | 2014-04-22 13:23:13 +0200 | [diff] [blame^] | 238 | return snd_soc_component_update_bits(&codec->component, reg, mask, |
| 239 | value); |
Lars-Peter Clausen | 96241c83 | 2014-03-18 09:02:07 +0100 | [diff] [blame] | 240 | } |
| 241 | EXPORT_SYMBOL_GPL(snd_soc_update_bits); |
| 242 | |
| 243 | /** |
| 244 | * snd_soc_update_bits_locked - update codec register bits |
| 245 | * @codec: audio codec |
| 246 | * @reg: codec register |
| 247 | * @mask: register mask |
| 248 | * @value: new value |
| 249 | * |
| 250 | * Writes new register value, and takes the codec mutex. |
| 251 | * |
| 252 | * Returns 1 for change else 0. |
| 253 | */ |
| 254 | int snd_soc_update_bits_locked(struct snd_soc_codec *codec, |
Mark Brown | aa0258a | 2014-04-14 17:42:28 +0100 | [diff] [blame] | 255 | unsigned int reg, unsigned int mask, |
Lars-Peter Clausen | 96241c83 | 2014-03-18 09:02:07 +0100 | [diff] [blame] | 256 | unsigned int value) |
| 257 | { |
Lars-Peter Clausen | e2c330b | 2014-04-22 13:23:13 +0200 | [diff] [blame^] | 258 | return snd_soc_component_update_bits(&codec->component, reg, mask, |
| 259 | value); |
Lars-Peter Clausen | 96241c83 | 2014-03-18 09:02:07 +0100 | [diff] [blame] | 260 | } |
| 261 | EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked); |
| 262 | |
| 263 | /** |
| 264 | * snd_soc_test_bits - test register for change |
| 265 | * @codec: audio codec |
| 266 | * @reg: codec register |
| 267 | * @mask: register mask |
| 268 | * @value: new value |
| 269 | * |
| 270 | * Tests a register with a new value and checks if the new value is |
| 271 | * different from the old value. |
| 272 | * |
| 273 | * Returns 1 for change else 0. |
| 274 | */ |
Mark Brown | aa0258a | 2014-04-14 17:42:28 +0100 | [diff] [blame] | 275 | int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned int reg, |
Lars-Peter Clausen | 96241c83 | 2014-03-18 09:02:07 +0100 | [diff] [blame] | 276 | unsigned int mask, unsigned int value) |
| 277 | { |
Lars-Peter Clausen | e2c330b | 2014-04-22 13:23:13 +0200 | [diff] [blame^] | 278 | return snd_soc_component_test_bits(&codec->component, reg, mask, value); |
Lars-Peter Clausen | 96241c83 | 2014-03-18 09:02:07 +0100 | [diff] [blame] | 279 | } |
| 280 | EXPORT_SYMBOL_GPL(snd_soc_test_bits); |
| 281 | |
| 282 | int snd_soc_platform_read(struct snd_soc_platform *platform, |
| 283 | unsigned int reg) |
| 284 | { |
Lars-Peter Clausen | e2c330b | 2014-04-22 13:23:13 +0200 | [diff] [blame^] | 285 | unsigned int val; |
| 286 | int ret; |
Lars-Peter Clausen | 96241c83 | 2014-03-18 09:02:07 +0100 | [diff] [blame] | 287 | |
Lars-Peter Clausen | e2c330b | 2014-04-22 13:23:13 +0200 | [diff] [blame^] | 288 | ret = snd_soc_component_read(&platform->component, reg, &val); |
| 289 | if (ret < 0) |
Lars-Peter Clausen | 96241c83 | 2014-03-18 09:02:07 +0100 | [diff] [blame] | 290 | return -1; |
Lars-Peter Clausen | 96241c83 | 2014-03-18 09:02:07 +0100 | [diff] [blame] | 291 | |
Lars-Peter Clausen | e2c330b | 2014-04-22 13:23:13 +0200 | [diff] [blame^] | 292 | trace_snd_soc_preg_read(platform, reg, val); |
Lars-Peter Clausen | 96241c83 | 2014-03-18 09:02:07 +0100 | [diff] [blame] | 293 | |
Lars-Peter Clausen | e2c330b | 2014-04-22 13:23:13 +0200 | [diff] [blame^] | 294 | return val; |
Lars-Peter Clausen | 96241c83 | 2014-03-18 09:02:07 +0100 | [diff] [blame] | 295 | } |
| 296 | EXPORT_SYMBOL_GPL(snd_soc_platform_read); |
| 297 | |
| 298 | int snd_soc_platform_write(struct snd_soc_platform *platform, |
| 299 | unsigned int reg, unsigned int val) |
| 300 | { |
Lars-Peter Clausen | 96241c83 | 2014-03-18 09:02:07 +0100 | [diff] [blame] | 301 | trace_snd_soc_preg_write(platform, reg, val); |
Lars-Peter Clausen | e2c330b | 2014-04-22 13:23:13 +0200 | [diff] [blame^] | 302 | return snd_soc_component_write(&platform->component, reg, val); |
Lars-Peter Clausen | 96241c83 | 2014-03-18 09:02:07 +0100 | [diff] [blame] | 303 | } |
| 304 | EXPORT_SYMBOL_GPL(snd_soc_platform_write); |
| 305 | |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 306 | /** |
Lars-Peter Clausen | e2c330b | 2014-04-22 13:23:13 +0200 | [diff] [blame^] | 307 | * snd_soc_component_init_io() - Initialize regmap IO |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 308 | * |
Lars-Peter Clausen | e2c330b | 2014-04-22 13:23:13 +0200 | [diff] [blame^] | 309 | * @component: component to initialize |
| 310 | * @regmap: regmap instance to use for IO operations |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 311 | * |
Lars-Peter Clausen | e2c330b | 2014-04-22 13:23:13 +0200 | [diff] [blame^] | 312 | * Return: 0 on success, a negative error code otherwise |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 313 | */ |
Lars-Peter Clausen | e2c330b | 2014-04-22 13:23:13 +0200 | [diff] [blame^] | 314 | int snd_soc_component_init_io(struct snd_soc_component *component, |
| 315 | struct regmap *regmap) |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 316 | { |
Mark Brown | 2b4bdee | 2012-02-17 14:33:29 -0800 | [diff] [blame] | 317 | int ret; |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 318 | |
Xiubo Li | 092eba9 | 2014-03-11 12:43:21 +0800 | [diff] [blame] | 319 | if (!regmap) |
Xiubo Li | a39f75f | 2014-03-26 13:40:23 +0800 | [diff] [blame] | 320 | return -EINVAL; |
Xiubo Li | 092eba9 | 2014-03-11 12:43:21 +0800 | [diff] [blame] | 321 | |
Lars-Peter Clausen | e2c330b | 2014-04-22 13:23:13 +0200 | [diff] [blame^] | 322 | ret = regmap_get_val_bytes(regmap); |
Xiubo Li | 092eba9 | 2014-03-11 12:43:21 +0800 | [diff] [blame] | 323 | /* Errors are legitimate for non-integer byte |
| 324 | * multiples */ |
| 325 | if (ret > 0) |
Lars-Peter Clausen | e2c330b | 2014-04-22 13:23:13 +0200 | [diff] [blame^] | 326 | component->val_bytes = ret; |
Mark Brown | 2b4bdee | 2012-02-17 14:33:29 -0800 | [diff] [blame] | 327 | |
Lars-Peter Clausen | e2c330b | 2014-04-22 13:23:13 +0200 | [diff] [blame^] | 328 | component->regmap = regmap; |
Mark Brown | 0671da1 | 2011-07-24 12:23:37 +0100 | [diff] [blame] | 329 | |
Xiubo Li | 092eba9 | 2014-03-11 12:43:21 +0800 | [diff] [blame] | 330 | return 0; |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 331 | } |
Lars-Peter Clausen | e2c330b | 2014-04-22 13:23:13 +0200 | [diff] [blame^] | 332 | EXPORT_SYMBOL_GPL(snd_soc_component_init_io); |