Version:  2.0.40 2.2.26 2.4.37 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9

Linux/sound/soc/codecs/ak4104.c

  1 /*
  2  * AK4104 ALSA SoC (ASoC) driver
  3  *
  4  * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
  5  *
  6  *  This program is free software; you can redistribute  it and/or modify it
  7  *  under the terms of  the GNU General  Public License as published by the
  8  *  Free Software Foundation;  either version 2 of the  License, or (at your
  9  *  option) any later version.
 10  */
 11 
 12 #include <linux/module.h>
 13 #include <linux/slab.h>
 14 #include <linux/spi/spi.h>
 15 #include <linux/of_device.h>
 16 #include <linux/of_gpio.h>
 17 #include <linux/regulator/consumer.h>
 18 #include <sound/asoundef.h>
 19 #include <sound/core.h>
 20 #include <sound/soc.h>
 21 #include <sound/initval.h>
 22 
 23 /* AK4104 registers addresses */
 24 #define AK4104_REG_CONTROL1             0x00
 25 #define AK4104_REG_RESERVED             0x01
 26 #define AK4104_REG_CONTROL2             0x02
 27 #define AK4104_REG_TX                   0x03
 28 #define AK4104_REG_CHN_STATUS(x)        ((x) + 0x04)
 29 #define AK4104_NUM_REGS                 10
 30 
 31 #define AK4104_REG_MASK                 0x1f
 32 #define AK4104_READ                     0xc0
 33 #define AK4104_WRITE                    0xe0
 34 #define AK4104_RESERVED_VAL             0x5b
 35 
 36 /* Bit masks for AK4104 registers */
 37 #define AK4104_CONTROL1_RSTN            (1 << 0)
 38 #define AK4104_CONTROL1_PW              (1 << 1)
 39 #define AK4104_CONTROL1_DIF0            (1 << 2)
 40 #define AK4104_CONTROL1_DIF1            (1 << 3)
 41 
 42 #define AK4104_CONTROL2_SEL0            (1 << 0)
 43 #define AK4104_CONTROL2_SEL1            (1 << 1)
 44 #define AK4104_CONTROL2_MODE            (1 << 2)
 45 
 46 #define AK4104_TX_TXE                   (1 << 0)
 47 #define AK4104_TX_V                     (1 << 1)
 48 
 49 struct ak4104_private {
 50         struct regmap *regmap;
 51         struct regulator *regulator;
 52 };
 53 
 54 static const struct snd_soc_dapm_widget ak4104_dapm_widgets[] = {
 55 SND_SOC_DAPM_PGA("TXE", AK4104_REG_TX, AK4104_TX_TXE, 0, NULL, 0),
 56 
 57 SND_SOC_DAPM_OUTPUT("TX"),
 58 };
 59 
 60 static const struct snd_soc_dapm_route ak4104_dapm_routes[] = {
 61         { "TXE", NULL, "Playback" },
 62         { "TX", NULL, "TXE" },
 63 };
 64 
 65 static int ak4104_set_dai_fmt(struct snd_soc_dai *codec_dai,
 66                               unsigned int format)
 67 {
 68         struct snd_soc_codec *codec = codec_dai->codec;
 69         struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
 70         int val = 0;
 71         int ret;
 72 
 73         /* set DAI format */
 74         switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
 75         case SND_SOC_DAIFMT_RIGHT_J:
 76                 break;
 77         case SND_SOC_DAIFMT_LEFT_J:
 78                 val |= AK4104_CONTROL1_DIF0;
 79                 break;
 80         case SND_SOC_DAIFMT_I2S:
 81                 val |= AK4104_CONTROL1_DIF0 | AK4104_CONTROL1_DIF1;
 82                 break;
 83         default:
 84                 dev_err(codec->dev, "invalid dai format\n");
 85                 return -EINVAL;
 86         }
 87 
 88         /* This device can only be slave */
 89         if ((format & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS)
 90                 return -EINVAL;
 91 
 92         ret = regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1,
 93                                  AK4104_CONTROL1_DIF0 | AK4104_CONTROL1_DIF1,
 94                                  val);
 95         if (ret < 0)
 96                 return ret;
 97 
 98         return 0;
 99 }
100 
101 static int ak4104_hw_params(struct snd_pcm_substream *substream,
102                             struct snd_pcm_hw_params *params,
103                             struct snd_soc_dai *dai)
104 {
105         struct snd_soc_codec *codec = dai->codec;
106         struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
107         int ret, val = 0;
108 
109         /* set the IEC958 bits: consumer mode, no copyright bit */
110         val |= IEC958_AES0_CON_NOT_COPYRIGHT;
111         regmap_write(ak4104->regmap, AK4104_REG_CHN_STATUS(0), val);
112 
113         val = 0;
114 
115         switch (params_rate(params)) {
116         case 22050:
117                 val |= IEC958_AES3_CON_FS_22050;
118                 break;
119         case 24000:
120                 val |= IEC958_AES3_CON_FS_24000;
121                 break;
122         case 32000:
123                 val |= IEC958_AES3_CON_FS_32000;
124                 break;
125         case 44100:
126                 val |= IEC958_AES3_CON_FS_44100;
127                 break;
128         case 48000:
129                 val |= IEC958_AES3_CON_FS_48000;
130                 break;
131         case 88200:
132                 val |= IEC958_AES3_CON_FS_88200;
133                 break;
134         case 96000:
135                 val |= IEC958_AES3_CON_FS_96000;
136                 break;
137         case 176400:
138                 val |= IEC958_AES3_CON_FS_176400;
139                 break;
140         case 192000:
141                 val |= IEC958_AES3_CON_FS_192000;
142                 break;
143         default:
144                 dev_err(codec->dev, "unsupported sampling rate\n");
145                 return -EINVAL;
146         }
147 
148         ret = regmap_write(ak4104->regmap, AK4104_REG_CHN_STATUS(3), val);
149         if (ret < 0)
150                 return ret;
151 
152         return 0;
153 }
154 
155 static const struct snd_soc_dai_ops ak4101_dai_ops = {
156         .hw_params = ak4104_hw_params,
157         .set_fmt = ak4104_set_dai_fmt,
158 };
159 
160 static struct snd_soc_dai_driver ak4104_dai = {
161         .name = "ak4104-hifi",
162         .playback = {
163                 .stream_name = "Playback",
164                 .channels_min = 2,
165                 .channels_max = 2,
166                 .rates = SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |
167                          SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
168                          SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
169                          SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000,
170                 .formats = SNDRV_PCM_FMTBIT_S16_LE  |
171                            SNDRV_PCM_FMTBIT_S24_3LE |
172                            SNDRV_PCM_FMTBIT_S24_LE
173         },
174         .ops = &ak4101_dai_ops,
175 };
176 
177 static int ak4104_probe(struct snd_soc_codec *codec)
178 {
179         struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
180         int ret;
181 
182         ret = regulator_enable(ak4104->regulator);
183         if (ret < 0) {
184                 dev_err(codec->dev, "Unable to enable regulator: %d\n", ret);
185                 return ret;
186         }
187 
188         /* set power-up and non-reset bits */
189         ret = regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1,
190                                  AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN,
191                                  AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN);
192         if (ret < 0)
193                 goto exit_disable_regulator;
194 
195         /* enable transmitter */
196         ret = regmap_update_bits(ak4104->regmap, AK4104_REG_TX,
197                                  AK4104_TX_TXE, AK4104_TX_TXE);
198         if (ret < 0)
199                 goto exit_disable_regulator;
200 
201         return 0;
202 
203 exit_disable_regulator:
204         regulator_disable(ak4104->regulator);
205         return ret;
206 }
207 
208 static int ak4104_remove(struct snd_soc_codec *codec)
209 {
210         struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
211 
212         regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1,
213                            AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN, 0);
214         regulator_disable(ak4104->regulator);
215 
216         return 0;
217 }
218 
219 #ifdef CONFIG_PM
220 static int ak4104_soc_suspend(struct snd_soc_codec *codec)
221 {
222         struct ak4104_private *priv = snd_soc_codec_get_drvdata(codec);
223 
224         regulator_disable(priv->regulator);
225 
226         return 0;
227 }
228 
229 static int ak4104_soc_resume(struct snd_soc_codec *codec)
230 {
231         struct ak4104_private *priv = snd_soc_codec_get_drvdata(codec);
232         int ret;
233 
234         ret = regulator_enable(priv->regulator);
235         if (ret < 0)
236                 return ret;
237 
238         return 0;
239 }
240 #else
241 #define ak4104_soc_suspend      NULL
242 #define ak4104_soc_resume       NULL
243 #endif /* CONFIG_PM */
244 
245 static struct snd_soc_codec_driver soc_codec_device_ak4104 = {
246         .probe = ak4104_probe,
247         .remove = ak4104_remove,
248         .suspend = ak4104_soc_suspend,
249         .resume = ak4104_soc_resume,
250 
251         .component_driver = {
252                 .dapm_widgets           = ak4104_dapm_widgets,
253                 .num_dapm_widgets       = ARRAY_SIZE(ak4104_dapm_widgets),
254                 .dapm_routes            = ak4104_dapm_routes,
255                 .num_dapm_routes        = ARRAY_SIZE(ak4104_dapm_routes),
256         }
257 };
258 
259 static const struct regmap_config ak4104_regmap = {
260         .reg_bits = 8,
261         .val_bits = 8,
262 
263         .max_register = AK4104_NUM_REGS - 1,
264         .read_flag_mask = AK4104_READ,
265         .write_flag_mask = AK4104_WRITE,
266 
267         .cache_type = REGCACHE_RBTREE,
268 };
269 
270 static int ak4104_spi_probe(struct spi_device *spi)
271 {
272         struct device_node *np = spi->dev.of_node;
273         struct ak4104_private *ak4104;
274         unsigned int val;
275         int ret;
276 
277         spi->bits_per_word = 8;
278         spi->mode = SPI_MODE_0;
279         ret = spi_setup(spi);
280         if (ret < 0)
281                 return ret;
282 
283         ak4104 = devm_kzalloc(&spi->dev, sizeof(struct ak4104_private),
284                               GFP_KERNEL);
285         if (ak4104 == NULL)
286                 return -ENOMEM;
287 
288         ak4104->regulator = devm_regulator_get(&spi->dev, "vdd");
289         if (IS_ERR(ak4104->regulator)) {
290                 ret = PTR_ERR(ak4104->regulator);
291                 dev_err(&spi->dev, "Unable to get Vdd regulator: %d\n", ret);
292                 return ret;
293         }
294 
295         ak4104->regmap = devm_regmap_init_spi(spi, &ak4104_regmap);
296         if (IS_ERR(ak4104->regmap)) {
297                 ret = PTR_ERR(ak4104->regmap);
298                 return ret;
299         }
300 
301         if (np) {
302                 enum of_gpio_flags flags;
303                 int gpio = of_get_named_gpio_flags(np, "reset-gpio", 0, &flags);
304 
305                 if (gpio_is_valid(gpio)) {
306                         ret = devm_gpio_request_one(&spi->dev, gpio,
307                                      flags & OF_GPIO_ACTIVE_LOW ?
308                                         GPIOF_OUT_INIT_LOW : GPIOF_OUT_INIT_HIGH,
309                                      "ak4104 reset");
310                         if (ret < 0)
311                                 return ret;
312                 }
313         }
314 
315         /* read the 'reserved' register - according to the datasheet, it
316          * should contain 0x5b. Not a good way to verify the presence of
317          * the device, but there is no hardware ID register. */
318         ret = regmap_read(ak4104->regmap, AK4104_REG_RESERVED, &val);
319         if (ret != 0)
320                 return ret;
321         if (val != AK4104_RESERVED_VAL)
322                 return -ENODEV;
323 
324         spi_set_drvdata(spi, ak4104);
325 
326         ret = snd_soc_register_codec(&spi->dev,
327                         &soc_codec_device_ak4104, &ak4104_dai, 1);
328         return ret;
329 }
330 
331 static int ak4104_spi_remove(struct spi_device *spi)
332 {
333         snd_soc_unregister_codec(&spi->dev);
334         return 0;
335 }
336 
337 static const struct of_device_id ak4104_of_match[] = {
338         { .compatible = "asahi-kasei,ak4104", },
339         { }
340 };
341 MODULE_DEVICE_TABLE(of, ak4104_of_match);
342 
343 static const struct spi_device_id ak4104_id_table[] = {
344         { "ak4104", 0 },
345         { }
346 };
347 MODULE_DEVICE_TABLE(spi, ak4104_id_table);
348 
349 static struct spi_driver ak4104_spi_driver = {
350         .driver  = {
351                 .name   = "ak4104",
352                 .of_match_table = ak4104_of_match,
353         },
354         .id_table = ak4104_id_table,
355         .probe  = ak4104_spi_probe,
356         .remove = ak4104_spi_remove,
357 };
358 
359 module_spi_driver(ak4104_spi_driver);
360 
361 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
362 MODULE_DESCRIPTION("Asahi Kasei AK4104 ALSA SoC driver");
363 MODULE_LICENSE("GPL");
364 
365 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us