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 | |
Mark Brown | 4835ff9 | 2011-08-13 11:50:48 +0900 | [diff] [blame] | 22 | #ifdef CONFIG_REGMAP |
Mark Brown | be3ea3b | 2011-06-13 19:35:29 +0100 | [diff] [blame] | 23 | static int hw_write(struct snd_soc_codec *codec, unsigned int reg, |
| 24 | unsigned int value) |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 25 | { |
Mark Brown | be3ea3b | 2011-06-13 19:35:29 +0100 | [diff] [blame] | 26 | return regmap_write(codec->control_data, reg, value); |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 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 | |
Mark Brown | 6572547 | 2014-02-20 09:08:43 +0900 | [diff] [blame] | 34 | ret = regmap_read(codec->control_data, reg, &val); |
| 35 | if (ret == 0) |
| 36 | return val; |
| 37 | else |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 38 | return -1; |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 39 | } |
| 40 | |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 41 | /** |
| 42 | * snd_soc_codec_set_cache_io: Set up standard I/O functions. |
| 43 | * |
| 44 | * @codec: CODEC to configure. |
Xiubo Li | 092eba9 | 2014-03-11 12:43:21 +0800 | [diff] [blame] | 45 | * @map: Register map to write to |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 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, |
Xiubo Li | 092eba9 | 2014-03-11 12:43:21 +0800 | [diff] [blame] | 59 | struct regmap *regmap) |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 60 | { |
Mark Brown | 2b4bdee | 2012-02-17 14:33:29 -0800 | [diff] [blame] | 61 | int ret; |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 62 | |
Xiubo Li | 092eba9 | 2014-03-11 12:43:21 +0800 | [diff] [blame] | 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 | |
Mark Brown | be3ea3b | 2011-06-13 19:35:29 +0100 | [diff] [blame] | 72 | codec->write = hw_write; |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 73 | codec->read = hw_read; |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 74 | |
Xiubo Li | 092eba9 | 2014-03-11 12:43:21 +0800 | [diff] [blame] | 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; |
Mark Brown | 2b4bdee | 2012-02-17 14:33:29 -0800 | [diff] [blame] | 80 | |
Xiubo Li | 092eba9 | 2014-03-11 12:43:21 +0800 | [diff] [blame] | 81 | codec->using_regmap = true; |
Mark Brown | 0671da1 | 2011-07-24 12:23:37 +0100 | [diff] [blame] | 82 | |
Xiubo Li | 092eba9 | 2014-03-11 12:43:21 +0800 | [diff] [blame] | 83 | return 0; |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 84 | } |
| 85 | EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io); |
Mark Brown | 4835ff9 | 2011-08-13 11:50:48 +0900 | [diff] [blame] | 86 | #else |
| 87 | int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec, |
Xiubo Li | 092eba9 | 2014-03-11 12:43:21 +0800 | [diff] [blame] | 88 | struct regmap *regmap) |
Mark Brown | 4835ff9 | 2011-08-13 11:50:48 +0900 | [diff] [blame] | 89 | { |
| 90 | return -ENOTSUPP; |
| 91 | } |
| 92 | EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io); |
| 93 | #endif |