Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/cooloney...
[deliverable/linux.git] / sound / soc / codecs / pcm1792a.c
1 /*
2 * PCM1792A ASoC codec driver
3 *
4 * Copyright (c) Amarula Solutions B.V. 2013
5 *
6 * Michael Trimarchi <michael@amarulasolutions.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 */
18
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/kernel.h>
22 #include <linux/device.h>
23 #include <linux/spi/spi.h>
24
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
28 #include <sound/initval.h>
29 #include <sound/soc.h>
30 #include <sound/tlv.h>
31 #include <linux/of.h>
32 #include <linux/of_device.h>
33
34 #include "pcm1792a.h"
35
36 #define PCM1792A_DAC_VOL_LEFT 0x10
37 #define PCM1792A_DAC_VOL_RIGHT 0x11
38 #define PCM1792A_FMT_CONTROL 0x12
39 #define PCM1792A_SOFT_MUTE PCM1792A_FMT_CONTROL
40
41 #define PCM1792A_FMT_MASK 0x70
42 #define PCM1792A_FMT_SHIFT 4
43 #define PCM1792A_MUTE_MASK 0x01
44 #define PCM1792A_MUTE_SHIFT 0
45 #define PCM1792A_ATLD_ENABLE (1 << 7)
46
47 static const struct reg_default pcm1792a_reg_defaults[] = {
48 { 0x10, 0xff },
49 { 0x11, 0xff },
50 { 0x12, 0x50 },
51 { 0x13, 0x00 },
52 { 0x14, 0x00 },
53 { 0x15, 0x01 },
54 { 0x16, 0x00 },
55 { 0x17, 0x00 },
56 };
57
58 static bool pcm1792a_accessible_reg(struct device *dev, unsigned int reg)
59 {
60 return reg >= 0x10 && reg <= 0x17;
61 }
62
63 static bool pcm1792a_writeable_reg(struct device *dev, unsigned register reg)
64 {
65 bool accessible;
66
67 accessible = pcm1792a_accessible_reg(dev, reg);
68
69 return accessible && reg != 0x16 && reg != 0x17;
70 }
71
72 struct pcm1792a_private {
73 struct regmap *regmap;
74 unsigned int format;
75 unsigned int rate;
76 };
77
78 static int pcm1792a_set_dai_fmt(struct snd_soc_dai *codec_dai,
79 unsigned int format)
80 {
81 struct snd_soc_codec *codec = codec_dai->codec;
82 struct pcm1792a_private *priv = snd_soc_codec_get_drvdata(codec);
83
84 priv->format = format;
85
86 return 0;
87 }
88
89 static int pcm1792a_digital_mute(struct snd_soc_dai *dai, int mute)
90 {
91 struct snd_soc_codec *codec = dai->codec;
92 struct pcm1792a_private *priv = snd_soc_codec_get_drvdata(codec);
93 int ret;
94
95 ret = regmap_update_bits(priv->regmap, PCM1792A_SOFT_MUTE,
96 PCM1792A_MUTE_MASK, !!mute);
97 if (ret < 0)
98 return ret;
99
100 return 0;
101 }
102
103 static int pcm1792a_hw_params(struct snd_pcm_substream *substream,
104 struct snd_pcm_hw_params *params,
105 struct snd_soc_dai *dai)
106 {
107 struct snd_soc_codec *codec = dai->codec;
108 struct pcm1792a_private *priv = snd_soc_codec_get_drvdata(codec);
109 int val = 0, ret;
110 int pcm_format = params_format(params);
111
112 priv->rate = params_rate(params);
113
114 switch (priv->format & SND_SOC_DAIFMT_FORMAT_MASK) {
115 case SND_SOC_DAIFMT_RIGHT_J:
116 if (pcm_format == SNDRV_PCM_FORMAT_S24_LE ||
117 pcm_format == SNDRV_PCM_FORMAT_S32_LE)
118 val = 0x02;
119 else if (pcm_format == SNDRV_PCM_FORMAT_S16_LE)
120 val = 0x00;
121 break;
122 case SND_SOC_DAIFMT_I2S:
123 if (pcm_format == SNDRV_PCM_FORMAT_S24_LE ||
124 pcm_format == SNDRV_PCM_FORMAT_S32_LE)
125 val = 0x05;
126 else if (pcm_format == SNDRV_PCM_FORMAT_S16_LE)
127 val = 0x04;
128 break;
129 default:
130 dev_err(codec->dev, "Invalid DAI format\n");
131 return -EINVAL;
132 }
133
134 val = val << PCM1792A_FMT_SHIFT | PCM1792A_ATLD_ENABLE;
135
136 ret = regmap_update_bits(priv->regmap, PCM1792A_FMT_CONTROL,
137 PCM1792A_FMT_MASK | PCM1792A_ATLD_ENABLE, val);
138 if (ret < 0)
139 return ret;
140
141 return 0;
142 }
143
144 static const struct snd_soc_dai_ops pcm1792a_dai_ops = {
145 .set_fmt = pcm1792a_set_dai_fmt,
146 .hw_params = pcm1792a_hw_params,
147 .digital_mute = pcm1792a_digital_mute,
148 };
149
150 static const DECLARE_TLV_DB_SCALE(pcm1792a_dac_tlv, -12000, 50, 1);
151
152 static const struct snd_kcontrol_new pcm1792a_controls[] = {
153 SOC_DOUBLE_R_RANGE_TLV("DAC Playback Volume", PCM1792A_DAC_VOL_LEFT,
154 PCM1792A_DAC_VOL_RIGHT, 0, 0xf, 0xff, 0,
155 pcm1792a_dac_tlv),
156 };
157
158 static const struct snd_soc_dapm_widget pcm1792a_dapm_widgets[] = {
159 SND_SOC_DAPM_OUTPUT("IOUTL+"),
160 SND_SOC_DAPM_OUTPUT("IOUTL-"),
161 SND_SOC_DAPM_OUTPUT("IOUTR+"),
162 SND_SOC_DAPM_OUTPUT("IOUTR-"),
163 };
164
165 static const struct snd_soc_dapm_route pcm1792a_dapm_routes[] = {
166 { "IOUTL+", NULL, "Playback" },
167 { "IOUTL-", NULL, "Playback" },
168 { "IOUTR+", NULL, "Playback" },
169 { "IOUTR-", NULL, "Playback" },
170 };
171
172 static struct snd_soc_dai_driver pcm1792a_dai = {
173 .name = "pcm1792a-hifi",
174 .playback = {
175 .stream_name = "Playback",
176 .channels_min = 2,
177 .channels_max = 2,
178 .rates = PCM1792A_RATES,
179 .formats = PCM1792A_FORMATS, },
180 .ops = &pcm1792a_dai_ops,
181 };
182
183 static const struct of_device_id pcm1792a_of_match[] = {
184 { .compatible = "ti,pcm1792a", },
185 { }
186 };
187 MODULE_DEVICE_TABLE(of, pcm1792a_of_match);
188
189 static const struct regmap_config pcm1792a_regmap = {
190 .reg_bits = 8,
191 .val_bits = 8,
192 .max_register = 23,
193 .reg_defaults = pcm1792a_reg_defaults,
194 .num_reg_defaults = ARRAY_SIZE(pcm1792a_reg_defaults),
195 .writeable_reg = pcm1792a_writeable_reg,
196 .readable_reg = pcm1792a_accessible_reg,
197 };
198
199 static struct snd_soc_codec_driver soc_codec_dev_pcm1792a = {
200 .controls = pcm1792a_controls,
201 .num_controls = ARRAY_SIZE(pcm1792a_controls),
202 .dapm_widgets = pcm1792a_dapm_widgets,
203 .num_dapm_widgets = ARRAY_SIZE(pcm1792a_dapm_widgets),
204 .dapm_routes = pcm1792a_dapm_routes,
205 .num_dapm_routes = ARRAY_SIZE(pcm1792a_dapm_routes),
206 };
207
208 static int pcm1792a_spi_probe(struct spi_device *spi)
209 {
210 struct pcm1792a_private *pcm1792a;
211 int ret;
212
213 pcm1792a = devm_kzalloc(&spi->dev, sizeof(struct pcm1792a_private),
214 GFP_KERNEL);
215 if (!pcm1792a)
216 return -ENOMEM;
217
218 spi_set_drvdata(spi, pcm1792a);
219
220 pcm1792a->regmap = devm_regmap_init_spi(spi, &pcm1792a_regmap);
221 if (IS_ERR(pcm1792a->regmap)) {
222 ret = PTR_ERR(pcm1792a->regmap);
223 dev_err(&spi->dev, "Failed to register regmap: %d\n", ret);
224 return ret;
225 }
226
227 return snd_soc_register_codec(&spi->dev,
228 &soc_codec_dev_pcm1792a, &pcm1792a_dai, 1);
229 }
230
231 static int pcm1792a_spi_remove(struct spi_device *spi)
232 {
233 snd_soc_unregister_codec(&spi->dev);
234 return 0;
235 }
236
237 static const struct spi_device_id pcm1792a_spi_ids[] = {
238 { "pcm1792a", 0 },
239 { },
240 };
241 MODULE_DEVICE_TABLE(spi, pcm1792a_spi_ids);
242
243 static struct spi_driver pcm1792a_codec_driver = {
244 .driver = {
245 .name = "pcm1792a",
246 .owner = THIS_MODULE,
247 .of_match_table = of_match_ptr(pcm1792a_of_match),
248 },
249 .id_table = pcm1792a_spi_ids,
250 .probe = pcm1792a_spi_probe,
251 .remove = pcm1792a_spi_remove,
252 };
253
254 module_spi_driver(pcm1792a_codec_driver);
255
256 MODULE_DESCRIPTION("ASoC PCM1792A driver");
257 MODULE_AUTHOR("Michael Trimarchi <michael@amarulasolutions.com>");
258 MODULE_LICENSE("GPL");
This page took 0.038616 seconds and 5 git commands to generate.