blob: 8aa08699686676f86a5732a25e3af467c874378d [file] [log] [blame]
Mark Brown5bef44f2011-06-13 17:49:55 +01001/*
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 Brownbe3ea3b2011-06-13 19:35:29 +010016#include <linux/regmap.h>
Paul Gortmakerd81a6d72011-09-22 09:34:58 -040017#include <linux/export.h>
Mark Brown5bef44f2011-06-13 17:49:55 +010018#include <sound/soc.h>
19
20#include <trace/events/asoc.h>
21
Mark Brown4835ff92011-08-13 11:50:48 +090022#ifdef CONFIG_REGMAP
Mark Brownbe3ea3b2011-06-13 19:35:29 +010023static int hw_write(struct snd_soc_codec *codec, unsigned int reg,
24 unsigned int value)
Mark Brown5bef44f2011-06-13 17:49:55 +010025{
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 Brownbe3ea3b2011-06-13 19:35:29 +010041 return regmap_write(codec->control_data, reg, value);
Mark Brown5bef44f2011-06-13 17:49:55 +010042}
43
44static 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 Brownbe3ea3b2011-06-13 19:35:29 +010055 ret = regmap_read(codec->control_data, reg, &val);
56 if (ret == 0)
57 return val;
58 else
Mark Brown3ebb5c92011-10-09 14:06:13 +010059 return -1;
Mark Brown5bef44f2011-06-13 17:49:55 +010060 }
61
62 ret = snd_soc_cache_read(codec, reg, &val);
63 if (ret < 0)
64 return -1;
65 return val;
66}
67
Mark Brown5bef44f2011-06-13 17:49:55 +010068/**
69 * snd_soc_codec_set_cache_io: Set up standard I/O functions.
70 *
71 * @codec: CODEC to configure.
Xiubo Li092eba92014-03-11 12:43:21 +080072 * @map: Register map to write to
Mark Brown5bef44f2011-06-13 17:49:55 +010073 *
74 * Register formats are frequently shared between many I2C and SPI
75 * devices. In order to promote code reuse the ASoC core provides
76 * some standard implementations of CODEC read and write operations
77 * which can be set up using this function.
78 *
79 * The caller is responsible for allocating and initialising the
80 * actual cache.
81 *
82 * Note that at present this code cannot be used by CODECs with
83 * volatile registers.
84 */
85int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
Xiubo Li092eba92014-03-11 12:43:21 +080086 struct regmap *regmap)
Mark Brown5bef44f2011-06-13 17:49:55 +010087{
Mark Brown2b4bdee2012-02-17 14:33:29 -080088 int ret;
Mark Brown5bef44f2011-06-13 17:49:55 +010089
Xiubo Li092eba92014-03-11 12:43:21 +080090 /* Device has made its own regmap arrangements */
91 if (!regmap)
92 codec->control_data = dev_get_regmap(codec->dev, NULL);
93 else
94 codec->control_data = regmap;
95
96 if (IS_ERR(codec->control_data))
97 return PTR_ERR(codec->control_data);
98
Mark Brownbe3ea3b2011-06-13 19:35:29 +010099 codec->write = hw_write;
Mark Brown5bef44f2011-06-13 17:49:55 +0100100 codec->read = hw_read;
Mark Brown5bef44f2011-06-13 17:49:55 +0100101
Xiubo Li092eba92014-03-11 12:43:21 +0800102 ret = regmap_get_val_bytes(codec->control_data);
103 /* Errors are legitimate for non-integer byte
104 * multiples */
105 if (ret > 0)
106 codec->val_bytes = ret;
Mark Brown2b4bdee2012-02-17 14:33:29 -0800107
Xiubo Li092eba92014-03-11 12:43:21 +0800108 codec->using_regmap = true;
Mark Brown0671da12011-07-24 12:23:37 +0100109
Xiubo Li092eba92014-03-11 12:43:21 +0800110 return 0;
Mark Brown5bef44f2011-06-13 17:49:55 +0100111}
112EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io);
Mark Brown4835ff92011-08-13 11:50:48 +0900113#else
114int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
Xiubo Li092eba92014-03-11 12:43:21 +0800115 struct regmap *regmap)
Mark Brown4835ff92011-08-13 11:50:48 +0900116{
117 return -ENOTSUPP;
118}
119EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io);
120#endif