blob: 7c2ed53f5a7fc771b7e4cad9303c89e6d2c375ed [file] [log] [blame]
Simon Glass80bf7372019-04-01 13:38:41 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2018 Google, LLC
4 * Written by Simon Glass <sjg@chromium.org>
5 */
6
7#define LOG_CATEGORY UCLASS_I2S
8
9#include <common.h>
10#include <audio_codec.h>
11#include <dm.h>
12#include <i2s.h>
13#include <misc.h>
14#include <sound.h>
15#include <asm/gpio.h>
16#include "tegra_i2s_priv.h"
17
18static int tegra_sound_setup(struct udevice *dev)
19{
20 struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev);
21 struct i2s_uc_priv *i2c_priv = dev_get_uclass_priv(uc_priv->i2s);
22 int ret;
23
24 if (uc_priv->setup_done)
25 return -EALREADY;
26 ret = audio_codec_set_params(uc_priv->codec, i2c_priv->id,
27 i2c_priv->samplingrate,
28 i2c_priv->samplingrate * i2c_priv->rfs,
29 i2c_priv->bitspersample,
30 i2c_priv->channels);
31 if (ret)
32 return ret;
33 uc_priv->setup_done = true;
34
35 return 0;
36}
37
38static int tegra_sound_play(struct udevice *dev, void *data, uint data_size)
39{
40 struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev);
41
42 return i2s_tx_data(uc_priv->i2s, data, data_size);
43}
44
45static int tegra_sound_probe(struct udevice *dev)
46{
47 struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev);
48 struct gpio_desc en_gpio;
49 struct udevice *ahub;
50 int ret;
51
52 ret = gpio_request_by_name(dev, "codec-enable-gpio", 0, &en_gpio,
53 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
54
55 ret = uclass_get_device_by_phandle(UCLASS_AUDIO_CODEC, dev,
56 "nvidia,audio-codec",
57 &uc_priv->codec);
58 if (ret) {
59 log_debug("Failed to probe audio codec\n");
60 return ret;
61 }
62 ret = uclass_get_device_by_phandle(UCLASS_I2S, dev,
63 "nvidia,i2s-controller",
64 &uc_priv->i2s);
65 if (ret) {
66 log_debug("Cannot find i2s: %d\n", ret);
67 return ret;
68 }
69
70 /* Set up the audio hub, telling it the currect i2s to use */
71 ahub = dev_get_parent(uc_priv->i2s);
72 ret = misc_ioctl(ahub, AHUB_MISCOP_SET_I2S, &uc_priv->i2s);
73 if (ret) {
74 log_debug("Cannot set i2c: %d\n", ret);
75 return ret;
76 }
77
78 log_debug("Probed sound '%s' with codec '%s' and i2s '%s'\n", dev->name,
79 uc_priv->codec->name, uc_priv->i2s->name);
80
81 return 0;
82}
83
84static const struct sound_ops tegra_sound_ops = {
85 .setup = tegra_sound_setup,
86 .play = tegra_sound_play,
87};
88
89static const struct udevice_id tegra_sound_ids[] = {
90 { .compatible = "nvidia,tegra-audio-max98090-nyan-big" },
91 { }
92};
93
94U_BOOT_DRIVER(tegra_sound) = {
95 .name = "tegra_sound",
96 .id = UCLASS_SOUND,
97 .of_match = tegra_sound_ids,
98 .probe = tegra_sound_probe,
99 .ops = &tegra_sound_ops,
100};