2 * tegra20_ac97.c - Tegra20 AC97 platform driver
4 * Copyright (c) 2012 Lucas Stach <dev@lynxeye.de>
6 * Partly based on code copyright/by:
8 * Copyright (c) 2011,2012 Toradex Inc.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * version 2 as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
21 #include <linux/clk.h>
22 #include <linux/delay.h>
23 #include <linux/device.h>
24 #include <linux/gpio.h>
26 #include <linux/jiffies.h>
27 #include <linux/module.h>
29 #include <linux/of_gpio.h>
30 #include <linux/platform_device.h>
31 #include <linux/pm_runtime.h>
32 #include <linux/regmap.h>
33 #include <linux/slab.h>
34 #include <sound/core.h>
35 #include <sound/pcm.h>
36 #include <sound/pcm_params.h>
37 #include <sound/soc.h>
38 #include <sound/dmaengine_pcm.h>
40 #include "tegra_asoc_utils.h"
41 #include "tegra20_ac97.h"
43 #define DRV_NAME "tegra20-ac97"
45 static struct tegra20_ac97
*workdata
;
47 static void tegra20_ac97_codec_reset(struct snd_ac97
*ac97
)
50 unsigned long timeout
;
52 /* reset line is not driven by DAC pad group, have to toggle GPIO */
53 gpio_set_value(workdata
->reset_gpio
, 0);
56 gpio_set_value(workdata
->reset_gpio
, 1);
59 timeout
= jiffies
+ msecs_to_jiffies(100);
62 regmap_read(workdata
->regmap
, TEGRA20_AC97_STATUS1
, &readback
);
63 if (readback
& TEGRA20_AC97_STATUS1_CODEC1_RDY
)
65 usleep_range(1000, 2000);
66 } while (!time_after(jiffies
, timeout
));
69 static void tegra20_ac97_codec_warm_reset(struct snd_ac97
*ac97
)
72 unsigned long timeout
;
75 * although sync line is driven by the DAC pad group warm reset using
76 * the controller cmd is not working, have to toggle sync line
79 gpio_request(workdata
->sync_gpio
, "codec-sync");
81 gpio_direction_output(workdata
->sync_gpio
, 1);
84 gpio_set_value(workdata
->sync_gpio
, 0);
86 gpio_free(workdata
->sync_gpio
);
88 timeout
= jiffies
+ msecs_to_jiffies(100);
91 regmap_read(workdata
->regmap
, TEGRA20_AC97_STATUS1
, &readback
);
92 if (readback
& TEGRA20_AC97_STATUS1_CODEC1_RDY
)
94 usleep_range(1000, 2000);
95 } while (!time_after(jiffies
, timeout
));
98 static unsigned short tegra20_ac97_codec_read(struct snd_ac97
*ac97_snd
,
102 unsigned long timeout
;
104 regmap_write(workdata
->regmap
, TEGRA20_AC97_CMD
,
105 (((reg
| 0x80) << TEGRA20_AC97_CMD_CMD_ADDR_SHIFT
) &
106 TEGRA20_AC97_CMD_CMD_ADDR_MASK
) |
107 TEGRA20_AC97_CMD_BUSY
);
109 timeout
= jiffies
+ msecs_to_jiffies(100);
112 regmap_read(workdata
->regmap
, TEGRA20_AC97_STATUS1
, &readback
);
113 if (readback
& TEGRA20_AC97_STATUS1_STA_VALID1
)
115 usleep_range(1000, 2000);
116 } while (!time_after(jiffies
, timeout
));
118 return ((readback
& TEGRA20_AC97_STATUS1_STA_DATA1_MASK
) >>
119 TEGRA20_AC97_STATUS1_STA_DATA1_SHIFT
);
122 static void tegra20_ac97_codec_write(struct snd_ac97
*ac97_snd
,
123 unsigned short reg
, unsigned short val
)
126 unsigned long timeout
;
128 regmap_write(workdata
->regmap
, TEGRA20_AC97_CMD
,
129 ((reg
<< TEGRA20_AC97_CMD_CMD_ADDR_SHIFT
) &
130 TEGRA20_AC97_CMD_CMD_ADDR_MASK
) |
131 ((val
<< TEGRA20_AC97_CMD_CMD_DATA_SHIFT
) &
132 TEGRA20_AC97_CMD_CMD_DATA_MASK
) |
133 TEGRA20_AC97_CMD_BUSY
);
135 timeout
= jiffies
+ msecs_to_jiffies(100);
138 regmap_read(workdata
->regmap
, TEGRA20_AC97_CMD
, &readback
);
139 if (!(readback
& TEGRA20_AC97_CMD_BUSY
))
141 usleep_range(1000, 2000);
142 } while (!time_after(jiffies
, timeout
));
145 static struct snd_ac97_bus_ops tegra20_ac97_ops
= {
146 .read
= tegra20_ac97_codec_read
,
147 .write
= tegra20_ac97_codec_write
,
148 .reset
= tegra20_ac97_codec_reset
,
149 .warm_reset
= tegra20_ac97_codec_warm_reset
,
152 static inline void tegra20_ac97_start_playback(struct tegra20_ac97
*ac97
)
154 regmap_update_bits(ac97
->regmap
, TEGRA20_AC97_FIFO1_SCR
,
155 TEGRA20_AC97_FIFO_SCR_PB_QRT_MT_EN
,
156 TEGRA20_AC97_FIFO_SCR_PB_QRT_MT_EN
);
158 regmap_update_bits(ac97
->regmap
, TEGRA20_AC97_CTRL
,
159 TEGRA20_AC97_CTRL_PCM_DAC_EN
|
160 TEGRA20_AC97_CTRL_STM_EN
,
161 TEGRA20_AC97_CTRL_PCM_DAC_EN
|
162 TEGRA20_AC97_CTRL_STM_EN
);
165 static inline void tegra20_ac97_stop_playback(struct tegra20_ac97
*ac97
)
167 regmap_update_bits(ac97
->regmap
, TEGRA20_AC97_FIFO1_SCR
,
168 TEGRA20_AC97_FIFO_SCR_PB_QRT_MT_EN
, 0);
170 regmap_update_bits(ac97
->regmap
, TEGRA20_AC97_CTRL
,
171 TEGRA20_AC97_CTRL_PCM_DAC_EN
, 0);
174 static inline void tegra20_ac97_start_capture(struct tegra20_ac97
*ac97
)
176 regmap_update_bits(ac97
->regmap
, TEGRA20_AC97_FIFO1_SCR
,
177 TEGRA20_AC97_FIFO_SCR_REC_FULL_EN
,
178 TEGRA20_AC97_FIFO_SCR_REC_FULL_EN
);
181 static inline void tegra20_ac97_stop_capture(struct tegra20_ac97
*ac97
)
183 regmap_update_bits(ac97
->regmap
, TEGRA20_AC97_FIFO1_SCR
,
184 TEGRA20_AC97_FIFO_SCR_REC_FULL_EN
, 0);
187 static int tegra20_ac97_trigger(struct snd_pcm_substream
*substream
, int cmd
,
188 struct snd_soc_dai
*dai
)
190 struct tegra20_ac97
*ac97
= snd_soc_dai_get_drvdata(dai
);
193 case SNDRV_PCM_TRIGGER_START
:
194 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
195 case SNDRV_PCM_TRIGGER_RESUME
:
196 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
197 tegra20_ac97_start_playback(ac97
);
199 tegra20_ac97_start_capture(ac97
);
201 case SNDRV_PCM_TRIGGER_STOP
:
202 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
203 case SNDRV_PCM_TRIGGER_SUSPEND
:
204 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
205 tegra20_ac97_stop_playback(ac97
);
207 tegra20_ac97_stop_capture(ac97
);
216 static const struct snd_soc_dai_ops tegra20_ac97_dai_ops
= {
217 .trigger
= tegra20_ac97_trigger
,
220 static int tegra20_ac97_probe(struct snd_soc_dai
*dai
)
222 struct tegra20_ac97
*ac97
= snd_soc_dai_get_drvdata(dai
);
224 dai
->capture_dma_data
= &ac97
->capture_dma_data
;
225 dai
->playback_dma_data
= &ac97
->playback_dma_data
;
230 static struct snd_soc_dai_driver tegra20_ac97_dai
= {
231 .name
= "tegra-ac97-pcm",
233 .probe
= tegra20_ac97_probe
,
235 .stream_name
= "PCM Playback",
238 .rates
= SNDRV_PCM_RATE_8000_48000
,
239 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
242 .stream_name
= "PCM Capture",
245 .rates
= SNDRV_PCM_RATE_8000_48000
,
246 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
248 .ops
= &tegra20_ac97_dai_ops
,
251 static const struct snd_soc_component_driver tegra20_ac97_component
= {
255 static bool tegra20_ac97_wr_rd_reg(struct device
*dev
, unsigned int reg
)
258 case TEGRA20_AC97_CTRL
:
259 case TEGRA20_AC97_CMD
:
260 case TEGRA20_AC97_STATUS1
:
261 case TEGRA20_AC97_FIFO1_SCR
:
262 case TEGRA20_AC97_FIFO_TX1
:
263 case TEGRA20_AC97_FIFO_RX1
:
272 static bool tegra20_ac97_volatile_reg(struct device
*dev
, unsigned int reg
)
275 case TEGRA20_AC97_STATUS1
:
276 case TEGRA20_AC97_FIFO1_SCR
:
277 case TEGRA20_AC97_FIFO_TX1
:
278 case TEGRA20_AC97_FIFO_RX1
:
287 static bool tegra20_ac97_precious_reg(struct device
*dev
, unsigned int reg
)
290 case TEGRA20_AC97_FIFO_TX1
:
291 case TEGRA20_AC97_FIFO_RX1
:
300 static const struct regmap_config tegra20_ac97_regmap_config
= {
304 .max_register
= TEGRA20_AC97_FIFO_RX1
,
305 .writeable_reg
= tegra20_ac97_wr_rd_reg
,
306 .readable_reg
= tegra20_ac97_wr_rd_reg
,
307 .volatile_reg
= tegra20_ac97_volatile_reg
,
308 .precious_reg
= tegra20_ac97_precious_reg
,
309 .cache_type
= REGCACHE_RBTREE
,
312 static int tegra20_ac97_platform_probe(struct platform_device
*pdev
)
314 struct tegra20_ac97
*ac97
;
315 struct resource
*mem
;
320 ac97
= devm_kzalloc(&pdev
->dev
, sizeof(struct tegra20_ac97
),
323 dev_err(&pdev
->dev
, "Can't allocate tegra20_ac97\n");
327 dev_set_drvdata(&pdev
->dev
, ac97
);
329 ac97
->clk_ac97
= devm_clk_get(&pdev
->dev
, NULL
);
330 if (IS_ERR(ac97
->clk_ac97
)) {
331 dev_err(&pdev
->dev
, "Can't retrieve ac97 clock\n");
332 ret
= PTR_ERR(ac97
->clk_ac97
);
336 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
337 regs
= devm_ioremap_resource(&pdev
->dev
, mem
);
343 ac97
->regmap
= devm_regmap_init_mmio(&pdev
->dev
, regs
,
344 &tegra20_ac97_regmap_config
);
345 if (IS_ERR(ac97
->regmap
)) {
346 dev_err(&pdev
->dev
, "regmap init failed\n");
347 ret
= PTR_ERR(ac97
->regmap
);
351 if (of_property_read_u32_array(pdev
->dev
.of_node
,
352 "nvidia,dma-request-selector",
354 dev_err(&pdev
->dev
, "No DMA resource\n");
359 ac97
->reset_gpio
= of_get_named_gpio(pdev
->dev
.of_node
,
360 "nvidia,codec-reset-gpio", 0);
361 if (gpio_is_valid(ac97
->reset_gpio
)) {
362 ret
= devm_gpio_request_one(&pdev
->dev
, ac97
->reset_gpio
,
363 GPIOF_OUT_INIT_HIGH
, "codec-reset");
365 dev_err(&pdev
->dev
, "could not get codec-reset GPIO\n");
369 dev_err(&pdev
->dev
, "no codec-reset GPIO supplied\n");
373 ac97
->sync_gpio
= of_get_named_gpio(pdev
->dev
.of_node
,
374 "nvidia,codec-sync-gpio", 0);
375 if (!gpio_is_valid(ac97
->sync_gpio
)) {
376 dev_err(&pdev
->dev
, "no codec-sync GPIO supplied\n");
380 ac97
->capture_dma_data
.addr
= mem
->start
+ TEGRA20_AC97_FIFO_RX1
;
381 ac97
->capture_dma_data
.addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
382 ac97
->capture_dma_data
.maxburst
= 4;
383 ac97
->capture_dma_data
.slave_id
= of_dma
[1];
385 ac97
->playback_dma_data
.addr
= mem
->start
+ TEGRA20_AC97_FIFO_TX1
;
386 ac97
->playback_dma_data
.addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
387 ac97
->playback_dma_data
.maxburst
= 4;
388 ac97
->playback_dma_data
.slave_id
= of_dma
[1];
390 ret
= tegra_asoc_utils_init(&ac97
->util_data
, &pdev
->dev
);
394 ret
= tegra_asoc_utils_set_ac97_rate(&ac97
->util_data
);
396 goto err_asoc_utils_fini
;
398 ret
= clk_prepare_enable(ac97
->clk_ac97
);
400 dev_err(&pdev
->dev
, "clk_enable failed: %d\n", ret
);
401 goto err_asoc_utils_fini
;
404 ret
= snd_soc_set_ac97_ops(&tegra20_ac97_ops
);
406 dev_err(&pdev
->dev
, "Failed to set AC'97 ops: %d\n", ret
);
407 goto err_asoc_utils_fini
;
410 ret
= snd_soc_register_component(&pdev
->dev
, &tegra20_ac97_component
,
411 &tegra20_ac97_dai
, 1);
413 dev_err(&pdev
->dev
, "Could not register DAI: %d\n", ret
);
415 goto err_asoc_utils_fini
;
418 ret
= tegra_pcm_platform_register(&pdev
->dev
);
420 dev_err(&pdev
->dev
, "Could not register PCM: %d\n", ret
);
421 goto err_unregister_component
;
424 /* XXX: crufty ASoC AC97 API - only one AC97 codec allowed */
429 err_unregister_component
:
430 snd_soc_unregister_component(&pdev
->dev
);
432 tegra_asoc_utils_fini(&ac97
->util_data
);
435 snd_soc_set_ac97_ops(NULL
);
439 static int tegra20_ac97_platform_remove(struct platform_device
*pdev
)
441 struct tegra20_ac97
*ac97
= dev_get_drvdata(&pdev
->dev
);
443 tegra_pcm_platform_unregister(&pdev
->dev
);
444 snd_soc_unregister_component(&pdev
->dev
);
446 tegra_asoc_utils_fini(&ac97
->util_data
);
448 clk_disable_unprepare(ac97
->clk_ac97
);
450 snd_soc_set_ac97_ops(NULL
);
455 static const struct of_device_id tegra20_ac97_of_match
[] = {
456 { .compatible
= "nvidia,tegra20-ac97", },
460 static struct platform_driver tegra20_ac97_driver
= {
463 .owner
= THIS_MODULE
,
464 .of_match_table
= tegra20_ac97_of_match
,
466 .probe
= tegra20_ac97_platform_probe
,
467 .remove
= tegra20_ac97_platform_remove
,
469 module_platform_driver(tegra20_ac97_driver
);
471 MODULE_AUTHOR("Lucas Stach");
472 MODULE_DESCRIPTION("Tegra20 AC97 ASoC driver");
473 MODULE_LICENSE("GPL v2");
474 MODULE_ALIAS("platform:" DRV_NAME
);
475 MODULE_DEVICE_TABLE(of
, tegra20_ac97_of_match
);