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 | { |
| 26 | int ret; |
| 27 | |
| 28 | if (!snd_soc_codec_volatile_register(codec, reg) && |
| 29 | reg < codec->driver->reg_cache_size && |
| 30 | !codec->cache_bypass) { |
| 31 | ret = snd_soc_cache_write(codec, reg, value); |
| 32 | if (ret < 0) |
| 33 | return -1; |
| 34 | } |
| 35 | |
| 36 | if (codec->cache_only) { |
| 37 | codec->cache_sync = 1; |
| 38 | return 0; |
| 39 | } |
| 40 | |
Mark Brown | be3ea3b | 2011-06-13 19:35:29 +0100 | [diff] [blame] | 41 | return regmap_write(codec->control_data, reg, value); |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | static unsigned int hw_read(struct snd_soc_codec *codec, unsigned int reg) |
| 45 | { |
| 46 | int ret; |
| 47 | unsigned int val; |
| 48 | |
| 49 | if (reg >= codec->driver->reg_cache_size || |
| 50 | snd_soc_codec_volatile_register(codec, reg) || |
| 51 | codec->cache_bypass) { |
| 52 | if (codec->cache_only) |
| 53 | return -1; |
| 54 | |
Mark Brown | be3ea3b | 2011-06-13 19:35:29 +0100 | [diff] [blame] | 55 | ret = regmap_read(codec->control_data, reg, &val); |
| 56 | if (ret == 0) |
| 57 | return val; |
| 58 | else |
Mark Brown | 3ebb5c9 | 2011-10-09 14:06:13 +0100 | [diff] [blame] | 59 | return -1; |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | ret = snd_soc_cache_read(codec, reg, &val); |
| 63 | if (ret < 0) |
| 64 | return -1; |
| 65 | return val; |
| 66 | } |
| 67 | |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 68 | /** |
| 69 | * snd_soc_codec_set_cache_io: Set up standard I/O functions. |
| 70 | * |
| 71 | * @codec: CODEC to configure. |
| 72 | * @addr_bits: Number of bits of register address data. |
| 73 | * @data_bits: Number of bits of data per register. |
| 74 | * @control: Control bus used. |
| 75 | * |
| 76 | * Register formats are frequently shared between many I2C and SPI |
| 77 | * devices. In order to promote code reuse the ASoC core provides |
| 78 | * some standard implementations of CODEC read and write operations |
| 79 | * which can be set up using this function. |
| 80 | * |
| 81 | * The caller is responsible for allocating and initialising the |
| 82 | * actual cache. |
| 83 | * |
| 84 | * Note that at present this code cannot be used by CODECs with |
| 85 | * volatile registers. |
| 86 | */ |
| 87 | int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec, |
| 88 | int addr_bits, int data_bits, |
| 89 | enum snd_soc_control_type control) |
| 90 | { |
Mark Brown | be3ea3b | 2011-06-13 19:35:29 +0100 | [diff] [blame] | 91 | struct regmap_config config; |
Mark Brown | 2b4bdee | 2012-02-17 14:33:29 -0800 | [diff] [blame] | 92 | int ret; |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 93 | |
Mark Brown | be3ea3b | 2011-06-13 19:35:29 +0100 | [diff] [blame] | 94 | memset(&config, 0, sizeof(config)); |
| 95 | codec->write = hw_write; |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 96 | codec->read = hw_read; |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 97 | |
Mark Brown | be3ea3b | 2011-06-13 19:35:29 +0100 | [diff] [blame] | 98 | config.reg_bits = addr_bits; |
| 99 | config.val_bits = data_bits; |
| 100 | |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 101 | switch (control) { |
Fabio Estevam | 4d9127f | 2013-11-20 15:37:42 -0200 | [diff] [blame] | 102 | #if IS_ENABLED(CONFIG_REGMAP_I2C) |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 103 | case SND_SOC_I2C: |
Mark Brown | be3ea3b | 2011-06-13 19:35:29 +0100 | [diff] [blame] | 104 | codec->control_data = regmap_init_i2c(to_i2c_client(codec->dev), |
| 105 | &config); |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 106 | break; |
Axel Lin | f024d9a | 2011-08-10 16:24:12 +0800 | [diff] [blame] | 107 | #endif |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 108 | |
Fabio Estevam | 4d9127f | 2013-11-20 15:37:42 -0200 | [diff] [blame] | 109 | #if IS_ENABLED(CONFIG_REGMAP_SPI) |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 110 | case SND_SOC_SPI: |
Mark Brown | be3ea3b | 2011-06-13 19:35:29 +0100 | [diff] [blame] | 111 | codec->control_data = regmap_init_spi(to_spi_device(codec->dev), |
| 112 | &config); |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 113 | break; |
Axel Lin | f024d9a | 2011-08-10 16:24:12 +0800 | [diff] [blame] | 114 | #endif |
Mark Brown | be3ea3b | 2011-06-13 19:35:29 +0100 | [diff] [blame] | 115 | |
Mark Brown | 0671da1 | 2011-07-24 12:23:37 +0100 | [diff] [blame] | 116 | case SND_SOC_REGMAP: |
| 117 | /* Device has made its own regmap arrangements */ |
Mark Brown | 8a713da | 2011-12-03 12:33:55 +0000 | [diff] [blame] | 118 | codec->using_regmap = true; |
Mark Brown | 210cb67 | 2012-05-08 17:46:36 +0100 | [diff] [blame] | 119 | if (!codec->control_data) |
| 120 | codec->control_data = dev_get_regmap(codec->dev, NULL); |
Mark Brown | 2b4bdee | 2012-02-17 14:33:29 -0800 | [diff] [blame] | 121 | |
Mark Brown | 9dfdd5a | 2012-06-22 12:40:52 +0100 | [diff] [blame] | 122 | if (codec->control_data) { |
| 123 | ret = regmap_get_val_bytes(codec->control_data); |
| 124 | /* Errors are legitimate for non-integer byte |
| 125 | * multiples */ |
| 126 | if (ret > 0) |
| 127 | codec->val_bytes = ret; |
| 128 | } |
Mark Brown | 0671da1 | 2011-07-24 12:23:37 +0100 | [diff] [blame] | 129 | break; |
| 130 | |
Mark Brown | be3ea3b | 2011-06-13 19:35:29 +0100 | [diff] [blame] | 131 | default: |
| 132 | return -EINVAL; |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 133 | } |
| 134 | |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 135 | return PTR_ERR_OR_ZERO(codec->control_data); |
Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 136 | } |
| 137 | EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io); |
Mark Brown | 4835ff9 | 2011-08-13 11:50:48 +0900 | [diff] [blame] | 138 | #else |
| 139 | int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec, |
| 140 | int addr_bits, int data_bits, |
| 141 | enum snd_soc_control_type control) |
| 142 | { |
| 143 | return -ENOTSUPP; |
| 144 | } |
| 145 | EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io); |
| 146 | #endif |