aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Brown <broonie@opensource.wolfsonmicro.com>2009-07-10 18:11:24 -0400
committerMark Brown <broonie@opensource.wolfsonmicro.com>2009-08-03 11:59:15 -0400
commitafa2f1066e7288a9e4f8e3fda277da245219dffc (patch)
tree3f19f2e84e797e6fc45bb5f81e0dc6dae393da76
parent7084a42b965d972079201414d19a399e65b26099 (diff)
ASoC: Factor out I2C 8 bit address 16 bit data I/O
As part of this refactoring the type of the CODEC hw_read operation is changed to match the regular read operation. Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com>
-rw-r--r--include/sound/soc.h2
-rw-r--r--sound/soc/soc-cache.c79
2 files changed, 72 insertions, 9 deletions
diff --git a/include/sound/soc.h b/include/sound/soc.h
index d0b29a509bdd..4a5846e72473 100644
--- a/include/sound/soc.h
+++ b/include/sound/soc.h
@@ -373,7 +373,7 @@ struct snd_soc_codec {
373 size_t, unsigned int); 373 size_t, unsigned int);
374 int (*volatile_register)(unsigned int); 374 int (*volatile_register)(unsigned int);
375 hw_write_t hw_write; 375 hw_write_t hw_write;
376 hw_read_t hw_read; 376 unsigned int (*hw_read)(struct snd_soc_codec *, unsigned int);
377 void *reg_cache; 377 void *reg_cache;
378 short reg_cache_size; 378 short reg_cache_size;
379 short reg_cache_step; 379 short reg_cache_step;
diff --git a/sound/soc/soc-cache.c b/sound/soc/soc-cache.c
index 8b126682c843..ab058b811633 100644
--- a/sound/soc/soc-cache.c
+++ b/sound/soc/soc-cache.c
@@ -46,14 +46,82 @@ static int snd_soc_7_9_write(struct snd_soc_codec *codec, unsigned int reg,
46 return -EIO; 46 return -EIO;
47} 47}
48 48
49static int snd_soc_8_16_write(struct snd_soc_codec *codec, unsigned int reg,
50 unsigned int value)
51{
52 u16 *reg_cache = codec->reg_cache;
53 u8 data[3];
54
55 data[0] = reg;
56 data[1] = (value >> 8) & 0xff;
57 data[2] = value & 0xff;
58
59 if (!snd_soc_codec_volatile_register(codec, reg))
60 reg_cache[reg] = value;
61
62 if (codec->hw_write(codec->control_data, data, 3) == 3)
63 return 0;
64 else
65 return -EIO;
66}
67
68static unsigned int snd_soc_8_16_read(struct snd_soc_codec *codec,
69 unsigned int reg)
70{
71 u16 *cache = codec->reg_cache;
72
73 if (reg >= codec->reg_cache_size ||
74 snd_soc_codec_volatile_register(codec, reg))
75 return codec->hw_read(codec, reg);
76 else
77 return cache[reg];
78}
79
80#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
81static unsigned int snd_soc_8_16_read_i2c(struct snd_soc_codec *codec,
82 unsigned int r)
83{
84 struct i2c_msg xfer[2];
85 u8 reg = r;
86 u16 data;
87 int ret;
88 struct i2c_client *client = codec->control_data;
89
90 /* Write register */
91 xfer[0].addr = client->addr;
92 xfer[0].flags = 0;
93 xfer[0].len = 1;
94 xfer[0].buf = &reg;
95
96 /* Read data */
97 xfer[1].addr = client->addr;
98 xfer[1].flags = I2C_M_RD;
99 xfer[1].len = 2;
100 xfer[1].buf = (u8 *)&data;
101
102 ret = i2c_transfer(client->adapter, xfer, 2);
103 if (ret != 2) {
104 dev_err(&client->dev, "i2c_transfer() returned %d\n", ret);
105 return 0;
106 }
107
108 return (data >> 8) | ((data & 0xff) << 8);
109}
110#else
111#define snd_soc_8_16_read_i2c NULL
112#endif
49 113
50static struct { 114static struct {
51 int addr_bits; 115 int addr_bits;
52 int data_bits; 116 int data_bits;
53 int (*write)(struct snd_soc_codec *, unsigned int, unsigned int); 117 int (*write)(struct snd_soc_codec *codec, unsigned int, unsigned int);
54 unsigned int (*read)(struct snd_soc_codec *, unsigned int); 118 unsigned int (*read)(struct snd_soc_codec *, unsigned int);
119 unsigned int (*i2c_read)(struct snd_soc_codec *, unsigned int);
55} io_types[] = { 120} io_types[] = {
56 { 7, 9, snd_soc_7_9_write, snd_soc_7_9_read }, 121 { 7, 9, snd_soc_7_9_write, snd_soc_7_9_read },
122 { 8, 16,
123 snd_soc_8_16_write, snd_soc_8_16_read,
124 snd_soc_8_16_read_i2c },
57}; 125};
58 126
59/** 127/**
@@ -82,13 +150,6 @@ int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
82{ 150{
83 int i; 151 int i;
84 152
85 /* We don't support volatile registers yet - refactoring of
86 * the hw_read operation will be required to do so. */
87 if (codec->volatile_register) {
88 printk(KERN_ERR "Volatile registers not yet supported\n");
89 return -EINVAL;
90 }
91
92 for (i = 0; i < ARRAY_SIZE(io_types); i++) 153 for (i = 0; i < ARRAY_SIZE(io_types); i++)
93 if (io_types[i].addr_bits == addr_bits && 154 if (io_types[i].addr_bits == addr_bits &&
94 io_types[i].data_bits == data_bits) 155 io_types[i].data_bits == data_bits)
@@ -111,6 +172,8 @@ int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
111#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 172#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
112 codec->hw_write = (hw_write_t)i2c_master_send; 173 codec->hw_write = (hw_write_t)i2c_master_send;
113#endif 174#endif
175 if (io_types[i].i2c_read)
176 codec->hw_read = io_types[i].i2c_read;
114 break; 177 break;
115 178
116 case SND_SOC_SPI: 179 case SND_SOC_SPI: