blob: 162a0fd68c7b1df28c9b37d667e060e83f9a78ef [file] [log] [blame]
Damien.Horsley39503622015-12-10 14:40:12 +00001/*
2 * Pistachio internal dac driver
3 *
4 * Copyright (C) 2015 Imagination Technologies Ltd.
5 *
6 * Author: Damien Horsley <Damien.Horsley@imgtec.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 */
12
13#include <linux/clk.h>
14#include <linux/delay.h>
15#include <linux/mfd/syscon.h>
16#include <linux/module.h>
17#include <linux/pm_runtime.h>
18#include <linux/regmap.h>
19#include <linux/regulator/consumer.h>
20
21#include <sound/pcm_params.h>
22#include <sound/soc.h>
23
24#define PISTACHIO_INTERNAL_DAC_CTRL 0x40
25#define PISTACHIO_INTERNAL_DAC_CTRL_PWR_SEL_MASK 0x2
26#define PISTACHIO_INTERNAL_DAC_CTRL_PWRDN_MASK 0x1
27
28#define PISTACHIO_INTERNAL_DAC_SRST 0x44
29#define PISTACHIO_INTERNAL_DAC_SRST_MASK 0x1
30
31#define PISTACHIO_INTERNAL_DAC_GTI_CTRL 0x48
32#define PISTACHIO_INTERNAL_DAC_GTI_CTRL_ADDR_SHIFT 0
33#define PISTACHIO_INTERNAL_DAC_GTI_CTRL_ADDR_MASK 0xFFF
34#define PISTACHIO_INTERNAL_DAC_GTI_CTRL_WE_MASK 0x1000
35#define PISTACHIO_INTERNAL_DAC_GTI_CTRL_WDATA_SHIFT 13
36#define PISTACHIO_INTERNAL_DAC_GTI_CTRL_WDATA_MASK 0x1FE000
37
38#define PISTACHIO_INTERNAL_DAC_PWR 0x1
39#define PISTACHIO_INTERNAL_DAC_PWR_MASK 0x1
40
41#define PISTACHIO_INTERNAL_DAC_FORMATS (SNDRV_PCM_FMTBIT_S24_LE | \
42 SNDRV_PCM_FMTBIT_S32_LE)
43
44/* codec private data */
45struct pistachio_internal_dac {
46 struct regmap *regmap;
47 struct regulator *supply;
48 bool mute;
49};
50
51static const struct snd_kcontrol_new pistachio_internal_dac_snd_controls[] = {
52 SOC_SINGLE("Playback Switch", PISTACHIO_INTERNAL_DAC_CTRL, 2, 1, 1)
53};
54
55static const struct snd_soc_dapm_widget pistachio_internal_dac_widgets[] = {
56 SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
57 SND_SOC_DAPM_OUTPUT("AOUTL"),
58 SND_SOC_DAPM_OUTPUT("AOUTR"),
59};
60
61static const struct snd_soc_dapm_route pistachio_internal_dac_routes[] = {
62 { "AOUTL", NULL, "DAC" },
63 { "AOUTR", NULL, "DAC" },
64};
65
66static void pistachio_internal_dac_reg_writel(struct regmap *top_regs,
67 u32 val, u32 reg)
68{
69 regmap_update_bits(top_regs, PISTACHIO_INTERNAL_DAC_GTI_CTRL,
70 PISTACHIO_INTERNAL_DAC_GTI_CTRL_ADDR_MASK,
71 reg << PISTACHIO_INTERNAL_DAC_GTI_CTRL_ADDR_SHIFT);
72
73 regmap_update_bits(top_regs, PISTACHIO_INTERNAL_DAC_GTI_CTRL,
74 PISTACHIO_INTERNAL_DAC_GTI_CTRL_WDATA_MASK,
75 val << PISTACHIO_INTERNAL_DAC_GTI_CTRL_WDATA_SHIFT);
76
77 regmap_update_bits(top_regs, PISTACHIO_INTERNAL_DAC_GTI_CTRL,
78 PISTACHIO_INTERNAL_DAC_GTI_CTRL_WE_MASK,
79 PISTACHIO_INTERNAL_DAC_GTI_CTRL_WE_MASK);
80
81 regmap_update_bits(top_regs, PISTACHIO_INTERNAL_DAC_GTI_CTRL,
82 PISTACHIO_INTERNAL_DAC_GTI_CTRL_WE_MASK, 0);
83}
84
85static void pistachio_internal_dac_pwr_off(struct pistachio_internal_dac *dac)
86{
87 regmap_update_bits(dac->regmap, PISTACHIO_INTERNAL_DAC_CTRL,
88 PISTACHIO_INTERNAL_DAC_CTRL_PWRDN_MASK,
89 PISTACHIO_INTERNAL_DAC_CTRL_PWRDN_MASK);
90
91 pistachio_internal_dac_reg_writel(dac->regmap, 0,
92 PISTACHIO_INTERNAL_DAC_PWR);
93}
94
95static void pistachio_internal_dac_pwr_on(struct pistachio_internal_dac *dac)
96{
97 regmap_update_bits(dac->regmap, PISTACHIO_INTERNAL_DAC_SRST,
98 PISTACHIO_INTERNAL_DAC_SRST_MASK,
99 PISTACHIO_INTERNAL_DAC_SRST_MASK);
100
101 regmap_update_bits(dac->regmap, PISTACHIO_INTERNAL_DAC_SRST,
102 PISTACHIO_INTERNAL_DAC_SRST_MASK, 0);
103
104 pistachio_internal_dac_reg_writel(dac->regmap,
105 PISTACHIO_INTERNAL_DAC_PWR_MASK,
106 PISTACHIO_INTERNAL_DAC_PWR);
107
108 regmap_update_bits(dac->regmap, PISTACHIO_INTERNAL_DAC_CTRL,
109 PISTACHIO_INTERNAL_DAC_CTRL_PWRDN_MASK, 0);
110}
111
112static struct snd_soc_dai_driver pistachio_internal_dac_dais[] = {
113 {
114 .name = "pistachio_internal_dac",
115 .playback = {
116 .stream_name = "Playback",
117 .channels_min = 2,
118 .channels_max = 2,
119 .rates = SNDRV_PCM_RATE_8000_48000,
120 .formats = PISTACHIO_INTERNAL_DAC_FORMATS,
121 }
122 },
123};
124
125static int pistachio_internal_dac_codec_probe(struct snd_soc_codec *codec)
126{
127 struct pistachio_internal_dac *dac = snd_soc_codec_get_drvdata(codec);
128
129 snd_soc_codec_init_regmap(codec, dac->regmap);
130
131 return 0;
132}
133
134static const struct snd_soc_codec_driver pistachio_internal_dac_driver = {
135 .probe = pistachio_internal_dac_codec_probe,
136 .idle_bias_off = true,
137 .controls = pistachio_internal_dac_snd_controls,
138 .num_controls = ARRAY_SIZE(pistachio_internal_dac_snd_controls),
139 .dapm_widgets = pistachio_internal_dac_widgets,
140 .num_dapm_widgets = ARRAY_SIZE(pistachio_internal_dac_widgets),
141 .dapm_routes = pistachio_internal_dac_routes,
142 .num_dapm_routes = ARRAY_SIZE(pistachio_internal_dac_routes),
143};
144
145static int pistachio_internal_dac_probe(struct platform_device *pdev)
146{
147 struct pistachio_internal_dac *dac;
148 int ret, voltage;
149 struct device *dev = &pdev->dev;
150 u32 reg;
151
152 dac = devm_kzalloc(dev, sizeof(*dac), GFP_KERNEL);
153
154 if (!dac)
155 return -ENOMEM;
156
157 platform_set_drvdata(pdev, dac);
158
159 dac->regmap = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
160 "img,cr-top");
161 if (IS_ERR(dac->regmap))
162 return PTR_ERR(dac->regmap);
163
164 dac->supply = devm_regulator_get(dev, "VDD");
165 if (IS_ERR(dac->supply)) {
166 ret = PTR_ERR(dac->supply);
167 if (ret != -EPROBE_DEFER)
168 dev_err(dev, "failed to acquire supply 'VDD-supply': %d\n", ret);
169 return ret;
170 }
171
172 ret = regulator_enable(dac->supply);
173 if (ret) {
174 dev_err(dev, "failed to enable supply: %d\n", ret);
175 return ret;
176 }
177
178 voltage = regulator_get_voltage(dac->supply);
179
180 switch (voltage) {
181 case 1800000:
182 reg = 0;
183 break;
184 case 3300000:
185 reg = PISTACHIO_INTERNAL_DAC_CTRL_PWR_SEL_MASK;
186 break;
187 default:
188 dev_err(dev, "invalid voltage: %d\n", voltage);
189 ret = -EINVAL;
190 goto err_regulator;
191 }
192
193 regmap_update_bits(dac->regmap, PISTACHIO_INTERNAL_DAC_CTRL,
194 PISTACHIO_INTERNAL_DAC_CTRL_PWR_SEL_MASK, reg);
195
196 pistachio_internal_dac_pwr_off(dac);
197 pistachio_internal_dac_pwr_on(dac);
198
199 pm_runtime_set_active(dev);
200 pm_runtime_enable(dev);
201 pm_runtime_idle(dev);
202
203 ret = snd_soc_register_codec(dev, &pistachio_internal_dac_driver,
204 pistachio_internal_dac_dais,
205 ARRAY_SIZE(pistachio_internal_dac_dais));
206 if (ret) {
207 dev_err(dev, "failed to register codec: %d\n", ret);
208 goto err_pwr;
209 }
210
211 return 0;
212
213err_pwr:
214 pm_runtime_disable(&pdev->dev);
215 pistachio_internal_dac_pwr_off(dac);
216err_regulator:
217 regulator_disable(dac->supply);
218
219 return ret;
220}
221
222static int pistachio_internal_dac_remove(struct platform_device *pdev)
223{
224 struct pistachio_internal_dac *dac = dev_get_drvdata(&pdev->dev);
225
226 snd_soc_unregister_codec(&pdev->dev);
227 pm_runtime_disable(&pdev->dev);
228 pistachio_internal_dac_pwr_off(dac);
229 regulator_disable(dac->supply);
230
231 return 0;
232}
233
234#ifdef CONFIG_PM
235static int pistachio_internal_dac_rt_resume(struct device *dev)
236{
237 struct pistachio_internal_dac *dac = dev_get_drvdata(dev);
238 int ret;
239
240 ret = regulator_enable(dac->supply);
241 if (ret) {
242 dev_err(dev, "failed to enable supply: %d\n", ret);
243 return ret;
244 }
245
246 pistachio_internal_dac_pwr_on(dac);
247
248 return 0;
249}
250
251static int pistachio_internal_dac_rt_suspend(struct device *dev)
252{
253 struct pistachio_internal_dac *dac = dev_get_drvdata(dev);
254
255 pistachio_internal_dac_pwr_off(dac);
256
257 regulator_disable(dac->supply);
258
259 return 0;
260}
261#endif
262
263static const struct dev_pm_ops pistachio_internal_dac_pm_ops = {
264 SET_RUNTIME_PM_OPS(pistachio_internal_dac_rt_suspend,
265 pistachio_internal_dac_rt_resume, NULL)
266};
267
268static const struct of_device_id pistachio_internal_dac_of_match[] = {
269 { .compatible = "img,pistachio-internal-dac" },
270 {}
271};
272MODULE_DEVICE_TABLE(of, pistachio_internal_dac_of_match);
273
274static struct platform_driver pistachio_internal_dac_plat_driver = {
275 .driver = {
276 .name = "img-pistachio-internal-dac",
277 .of_match_table = pistachio_internal_dac_of_match,
278 .pm = &pistachio_internal_dac_pm_ops
279 },
280 .probe = pistachio_internal_dac_probe,
281 .remove = pistachio_internal_dac_remove
282};
283module_platform_driver(pistachio_internal_dac_plat_driver);
284
285MODULE_DESCRIPTION("Pistachio Internal DAC driver");
286MODULE_AUTHOR("Damien Horsley <Damien.Horsley@imgtec.com>");
287MODULE_LICENSE("GPL v2");