aboutsummaryrefslogtreecommitdiffstats
path: root/sound/soc/codecs/ak4104.c
diff options
context:
space:
mode:
Diffstat (limited to 'sound/soc/codecs/ak4104.c')
-rw-r--r--sound/soc/codecs/ak4104.c174
1 files changed, 62 insertions, 112 deletions
diff --git a/sound/soc/codecs/ak4104.c b/sound/soc/codecs/ak4104.c
index d27b5e4cce99..ceb96ecf5588 100644
--- a/sound/soc/codecs/ak4104.c
+++ b/sound/soc/codecs/ak4104.c
@@ -46,75 +46,15 @@
46#define DRV_NAME "ak4104-codec" 46#define DRV_NAME "ak4104-codec"
47 47
48struct ak4104_private { 48struct ak4104_private {
49 enum snd_soc_control_type control_type; 49 struct regmap *regmap;
50 void *control_data;
51}; 50};
52 51
53static int ak4104_fill_cache(struct snd_soc_codec *codec)
54{
55 int i;
56 u8 *reg_cache = codec->reg_cache;
57 struct spi_device *spi = codec->control_data;
58
59 for (i = 0; i < codec->driver->reg_cache_size; i++) {
60 int ret = spi_w8r8(spi, i | AK4104_READ);
61 if (ret < 0) {
62 dev_err(&spi->dev, "SPI write failure\n");
63 return ret;
64 }
65
66 reg_cache[i] = ret;
67 }
68
69 return 0;
70}
71
72static unsigned int ak4104_read_reg_cache(struct snd_soc_codec *codec,
73 unsigned int reg)
74{
75 u8 *reg_cache = codec->reg_cache;
76
77 if (reg >= codec->driver->reg_cache_size)
78 return -EINVAL;
79
80 return reg_cache[reg];
81}
82
83static int ak4104_spi_write(struct snd_soc_codec *codec, unsigned int reg,
84 unsigned int value)
85{
86 u8 *cache = codec->reg_cache;
87 struct spi_device *spi = codec->control_data;
88
89 if (reg >= codec->driver->reg_cache_size)
90 return -EINVAL;
91
92 /* only write to the hardware if value has changed */
93 if (cache[reg] != value) {
94 u8 tmp[2] = { (reg & AK4104_REG_MASK) | AK4104_WRITE, value };
95
96 if (spi_write(spi, tmp, sizeof(tmp))) {
97 dev_err(&spi->dev, "SPI write failed\n");
98 return -EIO;
99 }
100
101 cache[reg] = value;
102 }
103
104 return 0;
105}
106
107static int ak4104_set_dai_fmt(struct snd_soc_dai *codec_dai, 52static int ak4104_set_dai_fmt(struct snd_soc_dai *codec_dai,
108 unsigned int format) 53 unsigned int format)
109{ 54{
110 struct snd_soc_codec *codec = codec_dai->codec; 55 struct snd_soc_codec *codec = codec_dai->codec;
111 int val = 0; 56 int val = 0;
112 57 int ret;
113 val = ak4104_read_reg_cache(codec, AK4104_REG_CONTROL1);
114 if (val < 0)
115 return val;
116
117 val &= ~(AK4104_CONTROL1_DIF0 | AK4104_CONTROL1_DIF1);
118 58
119 /* set DAI format */ 59 /* set DAI format */
120 switch (format & SND_SOC_DAIFMT_FORMAT_MASK) { 60 switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
@@ -135,7 +75,13 @@ static int ak4104_set_dai_fmt(struct snd_soc_dai *codec_dai,
135 if ((format & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS) 75 if ((format & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS)
136 return -EINVAL; 76 return -EINVAL;
137 77
138 return ak4104_spi_write(codec, AK4104_REG_CONTROL1, val); 78 ret = snd_soc_update_bits(codec, AK4104_REG_CONTROL1,
79 AK4104_CONTROL1_DIF0 | AK4104_CONTROL1_DIF1,
80 val);
81 if (ret < 0)
82 return ret;
83
84 return 0;
139} 85}
140 86
141static int ak4104_hw_params(struct snd_pcm_substream *substream, 87static int ak4104_hw_params(struct snd_pcm_substream *substream,
@@ -148,7 +94,7 @@ static int ak4104_hw_params(struct snd_pcm_substream *substream,
148 94
149 /* set the IEC958 bits: consumer mode, no copyright bit */ 95 /* set the IEC958 bits: consumer mode, no copyright bit */
150 val |= IEC958_AES0_CON_NOT_COPYRIGHT; 96 val |= IEC958_AES0_CON_NOT_COPYRIGHT;
151 ak4104_spi_write(codec, AK4104_REG_CHN_STATUS(0), val); 97 snd_soc_write(codec, AK4104_REG_CHN_STATUS(0), val);
152 98
153 val = 0; 99 val = 0;
154 100
@@ -167,7 +113,7 @@ static int ak4104_hw_params(struct snd_pcm_substream *substream,
167 return -EINVAL; 113 return -EINVAL;
168 } 114 }
169 115
170 return ak4104_spi_write(codec, AK4104_REG_CHN_STATUS(3), val); 116 return snd_soc_write(codec, AK4104_REG_CHN_STATUS(3), val);
171} 117}
172 118
173static const struct snd_soc_dai_ops ak4101_dai_ops = { 119static const struct snd_soc_dai_ops ak4101_dai_ops = {
@@ -192,67 +138,57 @@ static struct snd_soc_dai_driver ak4104_dai = {
192static int ak4104_probe(struct snd_soc_codec *codec) 138static int ak4104_probe(struct snd_soc_codec *codec)
193{ 139{
194 struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec); 140 struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
195 int ret, val; 141 int ret;
196
197 codec->control_data = ak4104->control_data;
198 142
199 /* read all regs and fill the cache */ 143 codec->control_data = ak4104->regmap;
200 ret = ak4104_fill_cache(codec); 144 ret = snd_soc_codec_set_cache_io(codec, 8, 8, SND_SOC_REGMAP);
201 if (ret < 0) { 145 if (ret != 0)
202 dev_err(codec->dev, "failed to fill register cache\n");
203 return ret; 146 return ret;
204 }
205
206 /* read the 'reserved' register - according to the datasheet, it
207 * should contain 0x5b. Not a good way to verify the presence of
208 * the device, but there is no hardware ID register. */
209 if (ak4104_read_reg_cache(codec, AK4104_REG_RESERVED) !=
210 AK4104_RESERVED_VAL)
211 return -ENODEV;
212 147
213 /* set power-up and non-reset bits */ 148 /* set power-up and non-reset bits */
214 val = ak4104_read_reg_cache(codec, AK4104_REG_CONTROL1); 149 ret = snd_soc_update_bits(codec, AK4104_REG_CONTROL1,
215 val |= AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN; 150 AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN,
216 ret = ak4104_spi_write(codec, AK4104_REG_CONTROL1, val); 151 AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN);
217 if (ret < 0) 152 if (ret < 0)
218 return ret; 153 return ret;
219 154
220 /* enable transmitter */ 155 /* enable transmitter */
221 val = ak4104_read_reg_cache(codec, AK4104_REG_TX); 156 ret = snd_soc_update_bits(codec, AK4104_REG_TX,
222 val |= AK4104_TX_TXE; 157 AK4104_TX_TXE, AK4104_TX_TXE);
223 ret = ak4104_spi_write(codec, AK4104_REG_TX, val);
224 if (ret < 0) 158 if (ret < 0)
225 return ret; 159 return ret;
226 160
227 dev_info(codec->dev, "SPI device initialized\n");
228 return 0; 161 return 0;
229} 162}
230 163
231static int ak4104_remove(struct snd_soc_codec *codec) 164static int ak4104_remove(struct snd_soc_codec *codec)
232{ 165{
233 int val, ret; 166 snd_soc_update_bits(codec, AK4104_REG_CONTROL1,
234 167 AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN, 0);
235 val = ak4104_read_reg_cache(codec, AK4104_REG_CONTROL1);
236 if (val < 0)
237 return val;
238
239 /* clear power-up and non-reset bits */
240 val &= ~(AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN);
241 ret = ak4104_spi_write(codec, AK4104_REG_CONTROL1, val);
242 168
243 return ret; 169 return 0;
244} 170}
245 171
246static struct snd_soc_codec_driver soc_codec_device_ak4104 = { 172static struct snd_soc_codec_driver soc_codec_device_ak4104 = {
247 .probe = ak4104_probe, 173 .probe = ak4104_probe,
248 .remove = ak4104_remove, 174 .remove = ak4104_remove,
249 .reg_cache_size = AK4104_NUM_REGS, 175};
250 .reg_word_size = sizeof(u8), 176
177static const struct regmap_config ak4104_regmap = {
178 .reg_bits = 8,
179 .val_bits = 8,
180
181 .max_register = AK4104_NUM_REGS - 1,
182 .read_flag_mask = AK4104_READ,
183 .write_flag_mask = AK4104_WRITE,
184
185 .cache_type = REGCACHE_RBTREE,
251}; 186};
252 187
253static int ak4104_spi_probe(struct spi_device *spi) 188static int ak4104_spi_probe(struct spi_device *spi)
254{ 189{
255 struct ak4104_private *ak4104; 190 struct ak4104_private *ak4104;
191 unsigned int val;
256 int ret; 192 int ret;
257 193
258 spi->bits_per_word = 8; 194 spi->bits_per_word = 8;
@@ -266,17 +202,41 @@ static int ak4104_spi_probe(struct spi_device *spi)
266 if (ak4104 == NULL) 202 if (ak4104 == NULL)
267 return -ENOMEM; 203 return -ENOMEM;
268 204
269 ak4104->control_data = spi; 205 ak4104->regmap = regmap_init_spi(spi, &ak4104_regmap);
270 ak4104->control_type = SND_SOC_SPI; 206 if (IS_ERR(ak4104->regmap)) {
207 ret = PTR_ERR(ak4104->regmap);
208 return ret;
209 }
210
211 /* read the 'reserved' register - according to the datasheet, it
212 * should contain 0x5b. Not a good way to verify the presence of
213 * the device, but there is no hardware ID register. */
214 ret = regmap_read(ak4104->regmap, AK4104_REG_RESERVED, &val);
215 if (ret != 0)
216 goto err;
217 if (val != AK4104_RESERVED_VAL) {
218 ret = -ENODEV;
219 goto err;
220 }
221
271 spi_set_drvdata(spi, ak4104); 222 spi_set_drvdata(spi, ak4104);
272 223
273 ret = snd_soc_register_codec(&spi->dev, 224 ret = snd_soc_register_codec(&spi->dev,
274 &soc_codec_device_ak4104, &ak4104_dai, 1); 225 &soc_codec_device_ak4104, &ak4104_dai, 1);
226 if (ret != 0)
227 goto err;
228
229 return 0;
230
231err:
232 regmap_exit(ak4104->regmap);
275 return ret; 233 return ret;
276} 234}
277 235
278static int __devexit ak4104_spi_remove(struct spi_device *spi) 236static int __devexit ak4104_spi_remove(struct spi_device *spi)
279{ 237{
238 struct ak4104_private *ak4101 = spi_get_drvdata(spi);
239 regmap_exit(ak4101->regmap);
280 snd_soc_unregister_codec(&spi->dev); 240 snd_soc_unregister_codec(&spi->dev);
281 return 0; 241 return 0;
282} 242}
@@ -290,17 +250,7 @@ static struct spi_driver ak4104_spi_driver = {
290 .remove = __devexit_p(ak4104_spi_remove), 250 .remove = __devexit_p(ak4104_spi_remove),
291}; 251};
292 252
293static int __init ak4104_init(void) 253module_spi_driver(ak4104_spi_driver);
294{
295 return spi_register_driver(&ak4104_spi_driver);
296}
297module_init(ak4104_init);
298
299static void __exit ak4104_exit(void)
300{
301 spi_unregister_driver(&ak4104_spi_driver);
302}
303module_exit(ak4104_exit);
304 254
305MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>"); 255MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
306MODULE_DESCRIPTION("Asahi Kasei AK4104 ALSA SoC driver"); 256MODULE_DESCRIPTION("Asahi Kasei AK4104 ALSA SoC driver");