| 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 | /* Primitive bulk write support for soc-cache.  The data pointed to by | 
| Mark Brown | be3ea3b | 2011-06-13 19:35:29 +0100 | [diff] [blame] | 69 |  * `data' needs to already be in the form the hardware expects.  Any | 
 | 70 |  * data written through this function will not go through the cache as | 
 | 71 |  * it only handles writing to volatile or out of bounds registers. | 
 | 72 |  * | 
 | 73 |  * This is currently only supported for devices using the regmap API | 
 | 74 |  * wrappers. | 
| Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 75 |  */ | 
| Mark Brown | be3ea3b | 2011-06-13 19:35:29 +0100 | [diff] [blame] | 76 | static int snd_soc_hw_bulk_write_raw(struct snd_soc_codec *codec, | 
 | 77 | 				     unsigned int reg, | 
| Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 78 | 				     const void *data, size_t len) | 
 | 79 | { | 
| Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 80 | 	/* To ensure that we don't get out of sync with the cache, check | 
 | 81 | 	 * whether the base register is volatile or if we've directly asked | 
 | 82 | 	 * to bypass the cache.  Out of bounds registers are considered | 
 | 83 | 	 * volatile. | 
 | 84 | 	 */ | 
 | 85 | 	if (!codec->cache_bypass | 
 | 86 | 	    && !snd_soc_codec_volatile_register(codec, reg) | 
 | 87 | 	    && reg < codec->driver->reg_cache_size) | 
 | 88 | 		return -EINVAL; | 
 | 89 |  | 
| Mark Brown | be3ea3b | 2011-06-13 19:35:29 +0100 | [diff] [blame] | 90 | 	return regmap_raw_write(codec->control_data, reg, data, len); | 
| Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 91 | } | 
 | 92 |  | 
| Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 93 | /** | 
 | 94 |  * snd_soc_codec_set_cache_io: Set up standard I/O functions. | 
 | 95 |  * | 
 | 96 |  * @codec: CODEC to configure. | 
 | 97 |  * @addr_bits: Number of bits of register address data. | 
 | 98 |  * @data_bits: Number of bits of data per register. | 
 | 99 |  * @control: Control bus used. | 
 | 100 |  * | 
 | 101 |  * Register formats are frequently shared between many I2C and SPI | 
 | 102 |  * devices.  In order to promote code reuse the ASoC core provides | 
 | 103 |  * some standard implementations of CODEC read and write operations | 
 | 104 |  * which can be set up using this function. | 
 | 105 |  * | 
 | 106 |  * The caller is responsible for allocating and initialising the | 
 | 107 |  * actual cache. | 
 | 108 |  * | 
 | 109 |  * Note that at present this code cannot be used by CODECs with | 
 | 110 |  * volatile registers. | 
 | 111 |  */ | 
 | 112 | int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec, | 
 | 113 | 			       int addr_bits, int data_bits, | 
 | 114 | 			       enum snd_soc_control_type control) | 
 | 115 | { | 
| Mark Brown | be3ea3b | 2011-06-13 19:35:29 +0100 | [diff] [blame] | 116 | 	struct regmap_config config; | 
| Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 117 |  | 
| Mark Brown | be3ea3b | 2011-06-13 19:35:29 +0100 | [diff] [blame] | 118 | 	memset(&config, 0, sizeof(config)); | 
 | 119 | 	codec->write = hw_write; | 
| Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 120 | 	codec->read = hw_read; | 
 | 121 | 	codec->bulk_write_raw = snd_soc_hw_bulk_write_raw; | 
 | 122 |  | 
| Mark Brown | be3ea3b | 2011-06-13 19:35:29 +0100 | [diff] [blame] | 123 | 	config.reg_bits = addr_bits; | 
 | 124 | 	config.val_bits = data_bits; | 
 | 125 |  | 
| Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 126 | 	switch (control) { | 
| Stephen Warren | 81bca76 | 2011-08-11 11:59:11 -0600 | [diff] [blame] | 127 | #if defined(CONFIG_REGMAP_I2C) || defined(CONFIG_REGMAP_I2C_MODULE) | 
| Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 128 | 	case SND_SOC_I2C: | 
| Mark Brown | be3ea3b | 2011-06-13 19:35:29 +0100 | [diff] [blame] | 129 | 		codec->control_data = regmap_init_i2c(to_i2c_client(codec->dev), | 
 | 130 | 						      &config); | 
| Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 131 | 		break; | 
| Axel Lin | f024d9a | 2011-08-10 16:24:12 +0800 | [diff] [blame] | 132 | #endif | 
| Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 133 |  | 
| Stephen Warren | 81bca76 | 2011-08-11 11:59:11 -0600 | [diff] [blame] | 134 | #if defined(CONFIG_REGMAP_SPI) || defined(CONFIG_REGMAP_SPI_MODULE) | 
| Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 135 | 	case SND_SOC_SPI: | 
| Mark Brown | be3ea3b | 2011-06-13 19:35:29 +0100 | [diff] [blame] | 136 | 		codec->control_data = regmap_init_spi(to_spi_device(codec->dev), | 
 | 137 | 						      &config); | 
| Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 138 | 		break; | 
| Axel Lin | f024d9a | 2011-08-10 16:24:12 +0800 | [diff] [blame] | 139 | #endif | 
| Mark Brown | be3ea3b | 2011-06-13 19:35:29 +0100 | [diff] [blame] | 140 |  | 
| Mark Brown | 0671da1 | 2011-07-24 12:23:37 +0100 | [diff] [blame] | 141 | 	case SND_SOC_REGMAP: | 
 | 142 | 		/* Device has made its own regmap arrangements */ | 
 | 143 | 		break; | 
 | 144 |  | 
| Mark Brown | be3ea3b | 2011-06-13 19:35:29 +0100 | [diff] [blame] | 145 | 	default: | 
 | 146 | 		return -EINVAL; | 
| Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 147 | 	} | 
 | 148 |  | 
| Mark Brown | be3ea3b | 2011-06-13 19:35:29 +0100 | [diff] [blame] | 149 | 	if (IS_ERR(codec->control_data)) | 
 | 150 | 		return PTR_ERR(codec->control_data); | 
 | 151 |  | 
| Mark Brown | 5bef44f | 2011-06-13 17:49:55 +0100 | [diff] [blame] | 152 | 	return 0; | 
 | 153 | } | 
 | 154 | EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io); | 
| Mark Brown | 4835ff9 | 2011-08-13 11:50:48 +0900 | [diff] [blame] | 155 | #else | 
 | 156 | int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec, | 
 | 157 | 			       int addr_bits, int data_bits, | 
 | 158 | 			       enum snd_soc_control_type control) | 
 | 159 | { | 
 | 160 | 	return -ENOTSUPP; | 
 | 161 | } | 
 | 162 | EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io); | 
 | 163 | #endif |