blob: 9150ad4a63ba1892f7b30ffaacd5f1d7175e681a [file] [log] [blame]
Simon Glass4070ba62018-12-10 10:37:39 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2018 Google, LLC
4 * Written by Simon Glass <sjg@chromium.org>
5 */
6
Simon Glass4070ba62018-12-10 10:37:39 -07007#include <audio_codec.h>
8#include <dm.h>
9#include <i2s.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glass4070ba62018-12-10 10:37:39 -070011#include <sound.h>
12#include <asm/gpio.h>
Simon Glassd789d082018-12-27 13:24:39 -070013#include <asm/arch/power.h>
Simon Glass4070ba62018-12-10 10:37:39 -070014
15static int samsung_sound_setup(struct udevice *dev)
16{
17 struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev);
18 struct i2s_uc_priv *i2c_priv = dev_get_uclass_priv(uc_priv->i2s);
19 int ret;
20
21 if (uc_priv->setup_done)
22 return -EALREADY;
23 ret = audio_codec_set_params(uc_priv->codec, i2c_priv->id,
24 i2c_priv->samplingrate,
25 i2c_priv->samplingrate * i2c_priv->rfs,
26 i2c_priv->bitspersample,
27 i2c_priv->channels);
28 if (ret)
29 return ret;
30 uc_priv->setup_done = true;
31
32 return 0;
33}
34
35static int samsung_sound_play(struct udevice *dev, void *data, uint data_size)
36{
37 struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev);
38
39 return i2s_tx_data(uc_priv->i2s, data, data_size);
40}
41
42static int samsung_sound_probe(struct udevice *dev)
43{
44 struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev);
45 struct ofnode_phandle_args args;
46 struct gpio_desc en_gpio;
47 ofnode node;
48 int ret;
49
50 ret = gpio_request_by_name(dev, "codec-enable-gpio", 0, &en_gpio,
51 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
52
53 /* Turn on the GPIO which connects to the codec's "enable" line. */
54 if (!ret)
55 gpio_set_pull(gpio_get_number(&en_gpio), S5P_GPIO_PULL_NONE);
56
57 ret = uclass_get_device_by_phandle(UCLASS_AUDIO_CODEC, dev,
58 "samsung,audio-codec",
59 &uc_priv->codec);
60 if (ret) {
61 debug("Failed to probe audio codec\n");
62 return ret;
63 }
64 node = ofnode_find_subnode(dev_ofnode(dev), "cpu");
65 if (!ofnode_valid(node)) {
66 debug("Failed to find /cpu subnode\n");
67 return -EINVAL;
68 }
69 ret = ofnode_parse_phandle_with_args(node, "sound-dai",
70 "#sound-dai-cells", 0, 0, &args);
71 if (ret) {
72 debug("Cannot find phandle: %d\n", ret);
73 return ret;
74 }
75 ret = uclass_get_device_by_ofnode(UCLASS_I2S, args.node, &uc_priv->i2s);
76 if (ret) {
77 debug("Cannot find i2s: %d\n", ret);
78 return ret;
79 }
80 debug("Probed sound '%s' with codec '%s' and i2s '%s'\n", dev->name,
81 uc_priv->codec->name, uc_priv->i2s->name);
82
Simon Glassd789d082018-12-27 13:24:39 -070083 /* Enable codec clock */
84 set_xclkout();
85
Simon Glass4070ba62018-12-10 10:37:39 -070086 return 0;
87}
88
89static const struct sound_ops samsung_sound_ops = {
90 .setup = samsung_sound_setup,
91 .play = samsung_sound_play,
92};
93
94static const struct udevice_id samsung_sound_ids[] = {
95 { .compatible = "google,snow-audio-max98095" },
Simon Glass355ec122019-01-11 18:37:09 -070096 { .compatible = "google,spring-audio-max98088" },
Simon Glass38d0bb32018-12-10 10:37:44 -070097 { .compatible = "samsung,smdk5420-audio-wm8994" },
98 { .compatible = "google,peach-audio-max98090" },
Simon Glass4070ba62018-12-10 10:37:39 -070099 { }
100};
101
102U_BOOT_DRIVER(samsung_sound) = {
103 .name = "samsung_sound",
104 .id = UCLASS_SOUND,
105 .of_match = samsung_sound_ids,
106 .probe = samsung_sound_probe,
107 .ops = &samsung_sound_ops,
108};