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 | 96241c83 | 2014-03-18 09:02:07 +0100 | [diff] [blame^] | 22 | unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg) |
| 23 | { |
| 24 | unsigned int ret; |
| 25 | |
| 26 | ret = codec->read(codec, reg); |
| 27 | dev_dbg(codec->dev, "read %x => %x\n", reg, ret); |
| 28 | trace_snd_soc_reg_read(codec, reg, ret); |
| 29 | |
| 30 | return ret; |
| 31 | } |
| 32 | EXPORT_SYMBOL_GPL(snd_soc_read); |
| 33 | |
| 34 | unsigned int snd_soc_write(struct snd_soc_codec *codec, |
| 35 | unsigned int reg, unsigned int val) |
| 36 | { |
| 37 | dev_dbg(codec->dev, "write %x = %x\n", reg, val); |
| 38 | trace_snd_soc_reg_write(codec, reg, val); |
| 39 | return codec->write(codec, reg, val); |
| 40 | } |
| 41 | EXPORT_SYMBOL_GPL(snd_soc_write); |
| 42 | |
| 43 | /** |
| 44 | * snd_soc_update_bits - update codec register bits |
| 45 | * @codec: audio codec |
| 46 | * @reg: codec register |
| 47 | * @mask: register mask |
| 48 | * @value: new value |
| 49 | * |
| 50 | * Writes new register value. |
| 51 | * |
| 52 | * Returns 1 for change, 0 for no change, or negative error code. |
| 53 | */ |
| 54 | int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg, |
| 55 | unsigned int mask, unsigned int value) |
| 56 | { |
| 57 | bool change; |
| 58 | unsigned int old, new; |
| 59 | int ret; |
| 60 | |
| 61 | if (codec->using_regmap) { |
| 62 | ret = regmap_update_bits_check(codec->control_data, reg, |
| 63 | mask, value, &change); |
| 64 | } else { |
| 65 | ret = snd_soc_read(codec, reg); |
| 66 | if (ret < 0) |
| 67 | return ret; |
| 68 | |
| 69 | old = ret; |
| 70 | new = (old & ~mask) | (value & mask); |
| 71 | change = old != new; |
| 72 | if (change) |
| 73 | ret = snd_soc_write(codec, reg, new); |
| 74 | } |
| 75 | |
| 76 | if (ret < 0) |
| 77 | return ret; |
| 78 | |
| 79 | return change; |
| 80 | } |
| 81 | EXPORT_SYMBOL_GPL(snd_soc_update_bits); |
| 82 | |
| 83 | /** |
| 84 | * snd_soc_update_bits_locked - update codec register bits |
| 85 | * @codec: audio codec |
| 86 | * @reg: codec register |
| 87 | * @mask: register mask |
| 88 | * @value: new value |
| 89 | * |
| 90 | * Writes new register value, and takes the codec mutex. |
| 91 | * |
| 92 | * Returns 1 for change else 0. |
| 93 | */ |
| 94 | int snd_soc_update_bits_locked(struct snd_soc_codec *codec, |
| 95 | unsigned short reg, unsigned int mask, |
| 96 | unsigned int value) |
| 97 | { |
| 98 | int change; |
| 99 | |
| 100 | mutex_lock(&codec->mutex); |
| 101 | change = snd_soc_update_bits(codec, reg, mask, value); |
| 102 | mutex_unlock(&codec->mutex); |
| 103 | |
| 104 | return change; |
| 105 | } |
| 106 | EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked); |
| 107 | |
| 108 | /** |
| 109 | * snd_soc_test_bits - test register for change |
| 110 | * @codec: audio codec |
| 111 | * @reg: codec register |
| 112 | * @mask: register mask |
| 113 | * @value: new value |
| 114 | * |
| 115 | * Tests a register with a new value and checks if the new value is |
| 116 | * different from the old value. |
| 117 | * |
| 118 | * Returns 1 for change else 0. |
| 119 | */ |
| 120 | int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg, |
| 121 | unsigned int mask, unsigned int value) |
| 122 | { |
| 123 | int change; |
| 124 | unsigned int old, new; |
| 125 | |
| 126 | old = snd_soc_read(codec, reg); |
| 127 | new = (old & ~mask) | value; |
| 128 | change = old != new; |
| 129 | |
| 130 | return change; |
| 131 | } |
| 132 | EXPORT_SYMBOL_GPL(snd_soc_test_bits); |
| 133 | |
| 134 | int snd_soc_platform_read(struct snd_soc_platform *platform, |
| 135 | unsigned int reg) |
| 136 | { |
| 137 | unsigned int ret; |
| 138 | |
| 139 | if (!platform->driver->read) { |
| 140 | dev_err(platform->dev, "ASoC: platform has no read back\n"); |
| 141 | return -1; |
| 142 | } |
| 143 | |
| 144 | ret = platform->driver->read(platform, reg); |
| 145 | dev_dbg(platform->dev, "read %x => %x\n", reg, ret); |
| 146 | trace_snd_soc_preg_read(platform, reg, ret); |
| 147 | |
| 148 | return ret; |
| 149 | } |
| 150 | EXPORT_SYMBOL_GPL(snd_soc_platform_read); |
| 151 | |
| 152 | int snd_soc_platform_write(struct snd_soc_platform *platform, |
| 153 | unsigned int reg, unsigned int val) |
| 154 | { |
| 155 | if (!platform->driver->write) { |
| 156 | dev_err(platform->dev, "ASoC: platform has no write back\n"); |
| 157 | return -1; |
| 158 | } |
| 159 | |
| 160 | dev_dbg(platform->dev, "write %x = %x\n", reg, val); |
| 161 | trace_snd_soc_preg_write(platform, reg, val); |
| 162 | return platform->driver->write(platform, reg, val); |
| 163 | } |
| 164 | EXPORT_SYMBOL_GPL(snd_soc_platform_write); |
| 165 | |
Mark Brown | 4835ff9 | 2011-08-13 11:50:48 +0900 | [diff] [blame] | 166 | #ifdef CONFIG_REGMAP |
Mark Brown | be3ea3b | 2011-06-13 19:35:29 +0100 | [diff] [blame] | 167 | static int hw_write(struct snd_soc_codec *codec, unsigned int reg, |
| 168 | unsigned int value) |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 169 | { |
Mark Brown | be3ea3b | 2011-06-13 19:35:29 +0100 | [diff] [blame] | 170 | return regmap_write(codec->control_data, reg, value); |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | static unsigned int hw_read(struct snd_soc_codec *codec, unsigned int reg) |
| 174 | { |
| 175 | int ret; |
| 176 | unsigned int val; |
| 177 | |
Mark Brown | 6572547 | 2014-02-20 09:08:43 +0900 | [diff] [blame] | 178 | ret = regmap_read(codec->control_data, reg, &val); |
| 179 | if (ret == 0) |
| 180 | return val; |
| 181 | else |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 182 | return -1; |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 183 | } |
| 184 | |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 185 | /** |
| 186 | * snd_soc_codec_set_cache_io: Set up standard I/O functions. |
| 187 | * |
| 188 | * @codec: CODEC to configure. |
Xiubo Li | 092eba9 | 2014-03-11 12:43:21 +0800 | [diff] [blame] | 189 | * @map: Register map to write to |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 190 | * |
| 191 | * Register formats are frequently shared between many I2C and SPI |
| 192 | * devices. In order to promote code reuse the ASoC core provides |
| 193 | * some standard implementations of CODEC read and write operations |
| 194 | * which can be set up using this function. |
| 195 | * |
| 196 | * The caller is responsible for allocating and initialising the |
| 197 | * actual cache. |
| 198 | * |
| 199 | * Note that at present this code cannot be used by CODECs with |
| 200 | * volatile registers. |
| 201 | */ |
| 202 | int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec, |
Xiubo Li | 092eba9 | 2014-03-11 12:43:21 +0800 | [diff] [blame] | 203 | struct regmap *regmap) |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 204 | { |
Mark Brown | 2b4bdee | 2012-02-17 14:33:29 -0800 | [diff] [blame] | 205 | int ret; |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 206 | |
Xiubo Li | 092eba9 | 2014-03-11 12:43:21 +0800 | [diff] [blame] | 207 | /* Device has made its own regmap arrangements */ |
| 208 | if (!regmap) |
| 209 | codec->control_data = dev_get_regmap(codec->dev, NULL); |
| 210 | else |
| 211 | codec->control_data = regmap; |
| 212 | |
| 213 | if (IS_ERR(codec->control_data)) |
| 214 | return PTR_ERR(codec->control_data); |
| 215 | |
Mark Brown | be3ea3b | 2011-06-13 19:35:29 +0100 | [diff] [blame] | 216 | codec->write = hw_write; |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 217 | codec->read = hw_read; |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 218 | |
Xiubo Li | 092eba9 | 2014-03-11 12:43:21 +0800 | [diff] [blame] | 219 | ret = regmap_get_val_bytes(codec->control_data); |
| 220 | /* Errors are legitimate for non-integer byte |
| 221 | * multiples */ |
| 222 | if (ret > 0) |
| 223 | codec->val_bytes = ret; |
Mark Brown | 2b4bdee | 2012-02-17 14:33:29 -0800 | [diff] [blame] | 224 | |
Xiubo Li | 092eba9 | 2014-03-11 12:43:21 +0800 | [diff] [blame] | 225 | codec->using_regmap = true; |
Mark Brown | 0671da1 | 2011-07-24 12:23:37 +0100 | [diff] [blame] | 226 | |
Xiubo Li | 092eba9 | 2014-03-11 12:43:21 +0800 | [diff] [blame] | 227 | return 0; |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 228 | } |
| 229 | EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io); |
Mark Brown | 4835ff9 | 2011-08-13 11:50:48 +0900 | [diff] [blame] | 230 | #else |
| 231 | int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec, |
Xiubo Li | 092eba9 | 2014-03-11 12:43:21 +0800 | [diff] [blame] | 232 | struct regmap *regmap) |
Mark Brown | 4835ff9 | 2011-08-13 11:50:48 +0900 | [diff] [blame] | 233 | { |
| 234 | return -ENOTSUPP; |
| 235 | } |
| 236 | EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io); |
| 237 | #endif |