jme: Fix device PM wakeup API usage
[linux/fpc-iii.git] / sound / soc / codecs / pcm1792a.c
blob3a80ba4452dfb944aab405b32af83854c349081f
1 /*
2 * PCM1792A ASoC codec driver
4 * Copyright (c) Amarula Solutions B.V. 2013
6 * Michael Trimarchi <michael@amarulasolutions.com>
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.
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.
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>
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>
34 #include "pcm1792a.h"
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
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)
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 },
58 static bool pcm1792a_accessible_reg(struct device *dev, unsigned int reg)
60 return reg >= 0x10 && reg <= 0x17;
63 static bool pcm1792a_writeable_reg(struct device *dev, unsigned register reg)
65 bool accessible;
67 accessible = pcm1792a_accessible_reg(dev, reg);
69 return accessible && reg != 0x16 && reg != 0x17;
72 struct pcm1792a_private {
73 struct regmap *regmap;
74 unsigned int format;
75 unsigned int rate;
78 static int pcm1792a_set_dai_fmt(struct snd_soc_dai *codec_dai,
79 unsigned int format)
81 struct snd_soc_codec *codec = codec_dai->codec;
82 struct pcm1792a_private *priv = snd_soc_codec_get_drvdata(codec);
84 priv->format = format;
86 return 0;
89 static int pcm1792a_digital_mute(struct snd_soc_dai *dai, int mute)
91 struct snd_soc_codec *codec = dai->codec;
92 struct pcm1792a_private *priv = snd_soc_codec_get_drvdata(codec);
93 int ret;
95 ret = regmap_update_bits(priv->regmap, PCM1792A_SOFT_MUTE,
96 PCM1792A_MUTE_MASK, !!mute);
97 if (ret < 0)
98 return ret;
100 return 0;
103 static int pcm1792a_hw_params(struct snd_pcm_substream *substream,
104 struct snd_pcm_hw_params *params,
105 struct snd_soc_dai *dai)
107 struct snd_soc_codec *codec = dai->codec;
108 struct pcm1792a_private *priv = snd_soc_codec_get_drvdata(codec);
109 int val = 0, ret;
111 priv->rate = params_rate(params);
113 switch (priv->format & SND_SOC_DAIFMT_FORMAT_MASK) {
114 case SND_SOC_DAIFMT_RIGHT_J:
115 switch (params_width(params)) {
116 case 24:
117 case 32:
118 val = 2;
119 break;
120 case 16:
121 val = 0;
122 break;
123 default:
124 return -EINVAL;
126 break;
127 case SND_SOC_DAIFMT_I2S:
128 switch (params_width(params)) {
129 case 24:
130 case 32:
131 val = 5;
132 break;
133 case 16:
134 val = 4;
135 break;
136 default:
137 return -EINVAL;
139 break;
140 default:
141 dev_err(codec->dev, "Invalid DAI format\n");
142 return -EINVAL;
145 val = val << PCM1792A_FMT_SHIFT | PCM1792A_ATLD_ENABLE;
147 ret = regmap_update_bits(priv->regmap, PCM1792A_FMT_CONTROL,
148 PCM1792A_FMT_MASK | PCM1792A_ATLD_ENABLE, val);
149 if (ret < 0)
150 return ret;
152 return 0;
155 static const struct snd_soc_dai_ops pcm1792a_dai_ops = {
156 .set_fmt = pcm1792a_set_dai_fmt,
157 .hw_params = pcm1792a_hw_params,
158 .digital_mute = pcm1792a_digital_mute,
161 static const DECLARE_TLV_DB_SCALE(pcm1792a_dac_tlv, -12000, 50, 1);
163 static const struct snd_kcontrol_new pcm1792a_controls[] = {
164 SOC_DOUBLE_R_RANGE_TLV("DAC Playback Volume", PCM1792A_DAC_VOL_LEFT,
165 PCM1792A_DAC_VOL_RIGHT, 0, 0xf, 0xff, 0,
166 pcm1792a_dac_tlv),
169 static const struct snd_soc_dapm_widget pcm1792a_dapm_widgets[] = {
170 SND_SOC_DAPM_OUTPUT("IOUTL+"),
171 SND_SOC_DAPM_OUTPUT("IOUTL-"),
172 SND_SOC_DAPM_OUTPUT("IOUTR+"),
173 SND_SOC_DAPM_OUTPUT("IOUTR-"),
176 static const struct snd_soc_dapm_route pcm1792a_dapm_routes[] = {
177 { "IOUTL+", NULL, "Playback" },
178 { "IOUTL-", NULL, "Playback" },
179 { "IOUTR+", NULL, "Playback" },
180 { "IOUTR-", NULL, "Playback" },
183 static struct snd_soc_dai_driver pcm1792a_dai = {
184 .name = "pcm1792a-hifi",
185 .playback = {
186 .stream_name = "Playback",
187 .channels_min = 2,
188 .channels_max = 2,
189 .rates = PCM1792A_RATES,
190 .formats = PCM1792A_FORMATS, },
191 .ops = &pcm1792a_dai_ops,
194 static const struct of_device_id pcm1792a_of_match[] = {
195 { .compatible = "ti,pcm1792a", },
198 MODULE_DEVICE_TABLE(of, pcm1792a_of_match);
200 static const struct regmap_config pcm1792a_regmap = {
201 .reg_bits = 8,
202 .val_bits = 8,
203 .max_register = 23,
204 .reg_defaults = pcm1792a_reg_defaults,
205 .num_reg_defaults = ARRAY_SIZE(pcm1792a_reg_defaults),
206 .writeable_reg = pcm1792a_writeable_reg,
207 .readable_reg = pcm1792a_accessible_reg,
210 static struct snd_soc_codec_driver soc_codec_dev_pcm1792a = {
211 .controls = pcm1792a_controls,
212 .num_controls = ARRAY_SIZE(pcm1792a_controls),
213 .dapm_widgets = pcm1792a_dapm_widgets,
214 .num_dapm_widgets = ARRAY_SIZE(pcm1792a_dapm_widgets),
215 .dapm_routes = pcm1792a_dapm_routes,
216 .num_dapm_routes = ARRAY_SIZE(pcm1792a_dapm_routes),
219 static int pcm1792a_spi_probe(struct spi_device *spi)
221 struct pcm1792a_private *pcm1792a;
222 int ret;
224 pcm1792a = devm_kzalloc(&spi->dev, sizeof(struct pcm1792a_private),
225 GFP_KERNEL);
226 if (!pcm1792a)
227 return -ENOMEM;
229 spi_set_drvdata(spi, pcm1792a);
231 pcm1792a->regmap = devm_regmap_init_spi(spi, &pcm1792a_regmap);
232 if (IS_ERR(pcm1792a->regmap)) {
233 ret = PTR_ERR(pcm1792a->regmap);
234 dev_err(&spi->dev, "Failed to register regmap: %d\n", ret);
235 return ret;
238 return snd_soc_register_codec(&spi->dev,
239 &soc_codec_dev_pcm1792a, &pcm1792a_dai, 1);
242 static int pcm1792a_spi_remove(struct spi_device *spi)
244 snd_soc_unregister_codec(&spi->dev);
245 return 0;
248 static const struct spi_device_id pcm1792a_spi_ids[] = {
249 { "pcm1792a", 0 },
250 { },
252 MODULE_DEVICE_TABLE(spi, pcm1792a_spi_ids);
254 static struct spi_driver pcm1792a_codec_driver = {
255 .driver = {
256 .name = "pcm1792a",
257 .owner = THIS_MODULE,
258 .of_match_table = of_match_ptr(pcm1792a_of_match),
260 .id_table = pcm1792a_spi_ids,
261 .probe = pcm1792a_spi_probe,
262 .remove = pcm1792a_spi_remove,
265 module_spi_driver(pcm1792a_codec_driver);
267 MODULE_DESCRIPTION("ASoC PCM1792A driver");
268 MODULE_AUTHOR("Michael Trimarchi <michael@amarulasolutions.com>");
269 MODULE_LICENSE("GPL");