blob: 260efc8466fc352cf70d08663799c7fde0758a2e [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{
Mark Brownbe3ea3b2011-06-13 19:35:29 +010026 return regmap_write(codec->control_data, reg, value);
Mark Brown5bef44f2011-06-13 17:49:55 +010027}
28
29static unsigned int hw_read(struct snd_soc_codec *codec, unsigned int reg)
30{
31 int ret;
32 unsigned int val;
33
Mark Brown65725472014-02-20 09:08:43 +090034 ret = regmap_read(codec->control_data, reg, &val);
35 if (ret == 0)
36 return val;
37 else
Mark Brown5bef44f2011-06-13 17:49:55 +010038 return -1;
Mark Brown5bef44f2011-06-13 17:49:55 +010039}
40
Mark Brown5bef44f2011-06-13 17:49:55 +010041/**
42 * snd_soc_codec_set_cache_io: Set up standard I/O functions.
43 *
44 * @codec: CODEC to configure.
Xiubo Li092eba92014-03-11 12:43:21 +080045 * @map: Register map to write to
Mark Brown5bef44f2011-06-13 17:49:55 +010046 *
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 */
58int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
Xiubo Li092eba92014-03-11 12:43:21 +080059 struct regmap *regmap)
Mark Brown5bef44f2011-06-13 17:49:55 +010060{
Mark Brown2b4bdee2012-02-17 14:33:29 -080061 int ret;
Mark Brown5bef44f2011-06-13 17:49:55 +010062
Xiubo Li092eba92014-03-11 12:43:21 +080063 /* 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 Brownbe3ea3b2011-06-13 19:35:29 +010072 codec->write = hw_write;
Mark Brown5bef44f2011-06-13 17:49:55 +010073 codec->read = hw_read;
Mark Brown5bef44f2011-06-13 17:49:55 +010074
Xiubo Li092eba92014-03-11 12:43:21 +080075 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 Brown2b4bdee2012-02-17 14:33:29 -080080
Xiubo Li092eba92014-03-11 12:43:21 +080081 codec->using_regmap = true;
Mark Brown0671da12011-07-24 12:23:37 +010082
Xiubo Li092eba92014-03-11 12:43:21 +080083 return 0;
Mark Brown5bef44f2011-06-13 17:49:55 +010084}
85EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io);
Mark Brown4835ff92011-08-13 11:50:48 +090086#else
87int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
Xiubo Li092eba92014-03-11 12:43:21 +080088 struct regmap *regmap)
Mark Brown4835ff92011-08-13 11:50:48 +090089{
90 return -ENOTSUPP;
91}
92EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io);
93#endif