| 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> |
| 16 | #include <linux/regmap.h> |
| 17 | #include <linux/export.h> |
| 18 | #include <sound/soc.h> |
| 19 | |
| 20 | #include <trace/events/asoc.h> |
| 21 | |
| 22 | #ifdef CONFIG_REGMAP |
| 23 | static int hw_write(struct snd_soc_codec *codec, unsigned int reg, |
| 24 | unsigned int value) |
| 25 | { |
| 26 | return regmap_write(codec->control_data, reg, value); |
| 27 | } |
| 28 | |
| 29 | static unsigned int hw_read(struct snd_soc_codec *codec, unsigned int reg) |
| 30 | { |
| 31 | int ret; |
| 32 | unsigned int val; |
| 33 | |
| 34 | ret = regmap_read(codec->control_data, reg, &val); |
| 35 | if (ret == 0) |
| 36 | return val; |
| 37 | else |
| 38 | return -1; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * snd_soc_codec_set_cache_io: Set up standard I/O functions. |
| 43 | * |
| 44 | * @codec: CODEC to configure. |
| 45 | * @map: Register map to write to |
| 46 | * |
| 47 | * Register formats are frequently shared between many I2C and SPI |
| 48 | * devices. In order to promote code reuse the ASoC core provides |
| 49 | * some standard implementations of CODEC read and write operations |
| 50 | * which can be set up using this function. |
| 51 | * |
| 52 | * The caller is responsible for allocating and initialising the |
| 53 | * actual cache. |
| 54 | * |
| 55 | * Note that at present this code cannot be used by CODECs with |
| 56 | * volatile registers. |
| 57 | */ |
| 58 | int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec, |
| 59 | struct regmap *regmap) |
| 60 | { |
| 61 | int ret; |
| 62 | |
| 63 | /* Device has made its own regmap arrangements */ |
| 64 | if (!regmap) |
| 65 | codec->control_data = dev_get_regmap(codec->dev, NULL); |
| 66 | else |
| 67 | codec->control_data = regmap; |
| 68 | |
| 69 | if (IS_ERR(codec->control_data)) |
| 70 | return PTR_ERR(codec->control_data); |
| 71 | |
| 72 | codec->write = hw_write; |
| 73 | codec->read = hw_read; |
| 74 | |
| 75 | ret = regmap_get_val_bytes(codec->control_data); |
| 76 | /* Errors are legitimate for non-integer byte |
| 77 | * multiples */ |
| 78 | if (ret > 0) |
| 79 | codec->val_bytes = ret; |
| 80 | |
| 81 | codec->using_regmap = true; |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io); |
| 86 | #else |
| 87 | int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec, |
| 88 | struct regmap *regmap) |
| 89 | { |
| 90 | return -ENOTSUPP; |
| 91 | } |
| 92 | EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io); |
| 93 | #endif |