Linux 3.12-rc5
[linux-2.6-block.git] / sound / soc / codecs / pcm1792a.c
CommitLineData
13b02fa0
MT
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_device.h>
32
33#include "pcm1792a.h"
34
35#define PCM1792A_DAC_VOL_LEFT 0x10
36#define PCM1792A_DAC_VOL_RIGHT 0x11
37#define PCM1792A_FMT_CONTROL 0x12
38#define PCM1792A_SOFT_MUTE PCM1792A_FMT_CONTROL
39
40#define PCM1792A_FMT_MASK 0x70
41#define PCM1792A_FMT_SHIFT 4
42#define PCM1792A_MUTE_MASK 0x01
43#define PCM1792A_MUTE_SHIFT 0
44#define PCM1792A_ATLD_ENABLE (1 << 7)
45
46static const struct reg_default pcm1792a_reg_defaults[] = {
47 { 0x10, 0xff },
48 { 0x11, 0xff },
49 { 0x12, 0x50 },
50 { 0x13, 0x00 },
51 { 0x14, 0x00 },
52 { 0x15, 0x01 },
53 { 0x16, 0x00 },
54 { 0x17, 0x00 },
55};
56
57static bool pcm1792a_accessible_reg(struct device *dev, unsigned int reg)
58{
59 return reg >= 0x10 && reg <= 0x17;
60}
61
62static bool pcm1792a_writeable_reg(struct device *dev, unsigned register reg)
63{
64 bool accessible;
65
66 accessible = pcm1792a_accessible_reg(dev, reg);
67
68 return accessible && reg != 0x16 && reg != 0x17;
69}
70
71struct pcm1792a_private {
72 struct regmap *regmap;
73 unsigned int format;
74 unsigned int rate;
75};
76
77static int pcm1792a_set_dai_fmt(struct snd_soc_dai *codec_dai,
78 unsigned int format)
79{
80 struct snd_soc_codec *codec = codec_dai->codec;
81 struct pcm1792a_private *priv = snd_soc_codec_get_drvdata(codec);
82
83 priv->format = format;
84
85 return 0;
86}
87
88static int pcm1792a_digital_mute(struct snd_soc_dai *dai, int mute)
89{
90 struct snd_soc_codec *codec = dai->codec;
91 struct pcm1792a_private *priv = snd_soc_codec_get_drvdata(codec);
92 int ret;
93
94 ret = regmap_update_bits(priv->regmap, PCM1792A_SOFT_MUTE,
95 PCM1792A_MUTE_MASK, !!mute);
96 if (ret < 0)
97 return ret;
98
99 return 0;
100}
101
102static int pcm1792a_hw_params(struct snd_pcm_substream *substream,
103 struct snd_pcm_hw_params *params,
104 struct snd_soc_dai *dai)
105{
106 struct snd_soc_codec *codec = dai->codec;
107 struct pcm1792a_private *priv = snd_soc_codec_get_drvdata(codec);
108 int val = 0, ret;
109 int pcm_format = params_format(params);
110
111 priv->rate = params_rate(params);
112
113 switch (priv->format & SND_SOC_DAIFMT_FORMAT_MASK) {
114 case SND_SOC_DAIFMT_RIGHT_J:
115 if (pcm_format == SNDRV_PCM_FORMAT_S24_LE ||
116 pcm_format == SNDRV_PCM_FORMAT_S32_LE)
117 val = 0x02;
118 else if (pcm_format == SNDRV_PCM_FORMAT_S16_LE)
119 val = 0x00;
120 break;
121 case SND_SOC_DAIFMT_I2S:
122 if (pcm_format == SNDRV_PCM_FORMAT_S24_LE ||
123 pcm_format == SNDRV_PCM_FORMAT_S32_LE)
124 val = 0x05;
125 else if (pcm_format == SNDRV_PCM_FORMAT_S16_LE)
126 val = 0x04;
127 break;
128 default:
129 dev_err(codec->dev, "Invalid DAI format\n");
130 return -EINVAL;
131 }
132
133 val = val << PCM1792A_FMT_SHIFT | PCM1792A_ATLD_ENABLE;
134
135 ret = regmap_update_bits(priv->regmap, PCM1792A_FMT_CONTROL,
136 PCM1792A_FMT_MASK | PCM1792A_ATLD_ENABLE, val);
137 if (ret < 0)
138 return ret;
139
140 return 0;
141}
142
143static const struct snd_soc_dai_ops pcm1792a_dai_ops = {
144 .set_fmt = pcm1792a_set_dai_fmt,
145 .hw_params = pcm1792a_hw_params,
146 .digital_mute = pcm1792a_digital_mute,
147};
148
149static const DECLARE_TLV_DB_SCALE(pcm1792a_dac_tlv, -12000, 50, 1);
150
151static const struct snd_kcontrol_new pcm1792a_controls[] = {
152 SOC_DOUBLE_R_RANGE_TLV("DAC Playback Volume", PCM1792A_DAC_VOL_LEFT,
153 PCM1792A_DAC_VOL_RIGHT, 0, 0xf, 0xff, 0,
154 pcm1792a_dac_tlv),
155};
156
e7a5cb42
MB
157static const struct snd_soc_dapm_widget pcm1792a_dapm_widgets[] = {
158SND_SOC_DAPM_OUTPUT("IOUTL+"),
159SND_SOC_DAPM_OUTPUT("IOUTL-"),
160SND_SOC_DAPM_OUTPUT("IOUTR+"),
161SND_SOC_DAPM_OUTPUT("IOUTR-"),
162};
163
164static const struct snd_soc_dapm_route pcm1792a_dapm_routes[] = {
165 { "IOUTL+", NULL, "Playback" },
166 { "IOUTL-", NULL, "Playback" },
167 { "IOUTR+", NULL, "Playback" },
168 { "IOUTR-", NULL, "Playback" },
169};
170
13b02fa0
MT
171static struct snd_soc_dai_driver pcm1792a_dai = {
172 .name = "pcm1792a-hifi",
173 .playback = {
174 .stream_name = "Playback",
175 .channels_min = 2,
176 .channels_max = 2,
177 .rates = PCM1792A_RATES,
178 .formats = PCM1792A_FORMATS, },
13b02fa0
MT
179 .ops = &pcm1792a_dai_ops,
180};
181
13b02fa0
MT
182static const struct of_device_id pcm1792a_of_match[] = {
183 { .compatible = "ti,pcm1792a", },
184 { }
185};
186MODULE_DEVICE_TABLE(of, pcm1792a_of_match);
13b02fa0
MT
187
188static const struct regmap_config pcm1792a_regmap = {
189 .reg_bits = 8,
190 .val_bits = 8,
191 .max_register = 24,
192 .reg_defaults = pcm1792a_reg_defaults,
193 .num_reg_defaults = ARRAY_SIZE(pcm1792a_reg_defaults),
194 .writeable_reg = pcm1792a_writeable_reg,
195 .readable_reg = pcm1792a_accessible_reg,
196};
197
198static struct snd_soc_codec_driver soc_codec_dev_pcm1792a = {
199 .controls = pcm1792a_controls,
200 .num_controls = ARRAY_SIZE(pcm1792a_controls),
e7a5cb42
MB
201 .dapm_widgets = pcm1792a_dapm_widgets,
202 .num_dapm_widgets = ARRAY_SIZE(pcm1792a_dapm_widgets),
203 .dapm_routes = pcm1792a_dapm_routes,
204 .num_dapm_routes = ARRAY_SIZE(pcm1792a_dapm_routes),
13b02fa0
MT
205};
206
207static int pcm1792a_spi_probe(struct spi_device *spi)
208{
209 struct pcm1792a_private *pcm1792a;
210 int ret;
211
212 pcm1792a = devm_kzalloc(&spi->dev, sizeof(struct pcm1792a_private),
213 GFP_KERNEL);
214 if (!pcm1792a)
215 return -ENOMEM;
216
217 spi_set_drvdata(spi, pcm1792a);
218
219 pcm1792a->regmap = devm_regmap_init_spi(spi, &pcm1792a_regmap);
220 if (IS_ERR(pcm1792a->regmap)) {
221 ret = PTR_ERR(pcm1792a->regmap);
222 dev_err(&spi->dev, "Failed to register regmap: %d\n", ret);
223 return ret;
224 }
225
226 return snd_soc_register_codec(&spi->dev,
227 &soc_codec_dev_pcm1792a, &pcm1792a_dai, 1);
228}
229
230static int pcm1792a_spi_remove(struct spi_device *spi)
231{
232 snd_soc_unregister_codec(&spi->dev);
233 return 0;
234}
235
236static const struct spi_device_id pcm1792a_spi_ids[] = {
237 { "pcm1792a", 0 },
238 { },
239};
240MODULE_DEVICE_TABLE(spi, pcm1792a_spi_ids);
241
242static struct spi_driver pcm1792a_codec_driver = {
243 .driver = {
244 .name = "pcm1792a",
245 .owner = THIS_MODULE,
55af2d23 246 .of_match_table = of_match_ptr(pcm1792a_of_match),
13b02fa0
MT
247 },
248 .id_table = pcm1792a_spi_ids,
249 .probe = pcm1792a_spi_probe,
250 .remove = pcm1792a_spi_remove,
251};
252
253module_spi_driver(pcm1792a_codec_driver);
254
255MODULE_DESCRIPTION("ASoC PCM1792A driver");
256MODULE_AUTHOR("Michael Trimarchi <michael@amarulasolutions.com>");
257MODULE_LICENSE("GPL");