soc-io.c 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * soc-io.c  --  ASoC register I/O helpers
 *
 * Copyright 2009-2011 Wolfson Microelectronics PLC.
 *
 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
 *
 *  This program is free software; you can redistribute  it and/or modify it
 *  under  the terms of  the GNU General  Public License as published by the
 *  Free Software Foundation;  either version 2 of the  License, or (at your
 *  option) any later version.
 */

#include <linux/i2c.h>
#include <linux/spi/spi.h>
16
#include <linux/regmap.h>
17
#include <linux/export.h>
18 19 20 21
#include <sound/soc.h>

#include <trace/events/asoc.h>

M
Mark Brown 已提交
22
#ifdef CONFIG_REGMAP
23 24
static int hw_write(struct snd_soc_codec *codec, unsigned int reg,
		    unsigned int value)
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
{
	int ret;

	if (!snd_soc_codec_volatile_register(codec, reg) &&
	    reg < codec->driver->reg_cache_size &&
	    !codec->cache_bypass) {
		ret = snd_soc_cache_write(codec, reg, value);
		if (ret < 0)
			return -1;
	}

	if (codec->cache_only) {
		codec->cache_sync = 1;
		return 0;
	}

41
	return regmap_write(codec->control_data, reg, value);
42 43 44 45 46 47 48 49 50 51 52 53 54
}

static unsigned int hw_read(struct snd_soc_codec *codec, unsigned int reg)
{
	int ret;
	unsigned int val;

	if (reg >= codec->driver->reg_cache_size ||
	    snd_soc_codec_volatile_register(codec, reg) ||
	    codec->cache_bypass) {
		if (codec->cache_only)
			return -1;

55 56 57 58
		ret = regmap_read(codec->control_data, reg, &val);
		if (ret == 0)
			return val;
		else
59
			return -1;
60 61 62 63 64 65 66 67 68 69 70 71
	}

	ret = snd_soc_cache_read(codec, reg, &val);
	if (ret < 0)
		return -1;
	return val;
}

/**
 * snd_soc_codec_set_cache_io: Set up standard I/O functions.
 *
 * @codec: CODEC to configure.
72
 * @map: Register map to write to
73 74 75 76 77 78 79 80 81 82 83 84 85
 *
 * Register formats are frequently shared between many I2C and SPI
 * devices.  In order to promote code reuse the ASoC core provides
 * some standard implementations of CODEC read and write operations
 * which can be set up using this function.
 *
 * The caller is responsible for allocating and initialising the
 * actual cache.
 *
 * Note that at present this code cannot be used by CODECs with
 * volatile registers.
 */
int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
86
			       struct regmap *regmap)
87
{
88
	int ret;
89

90 91 92 93 94 95 96 97 98
	/* Device has made its own regmap arrangements */
	if (!regmap)
		codec->control_data = dev_get_regmap(codec->dev, NULL);
	else
		codec->control_data = regmap;

	if (IS_ERR(codec->control_data))
		return PTR_ERR(codec->control_data);

99
	codec->write = hw_write;
100 101
	codec->read = hw_read;

102 103 104 105 106 107 108
	ret = regmap_get_val_bytes(codec->control_data);
	/* Errors are legitimate for non-integer byte
	 * multiples */
	if (ret > 0)
		codec->val_bytes = ret;

	codec->using_regmap = true;
109

110
	return 0;
111 112
}
EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io);
M
Mark Brown 已提交
113 114
#else
int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
115
			       struct regmap *regmap)
M
Mark Brown 已提交
116 117 118 119 120
{
	return -ENOTSUPP;
}
EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io);
#endif