1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2018 Google, LLC
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6 
7 #include <common.h>
8 #include <audio_codec.h>
9 #include <dm.h>
10 #include <i2s.h>
11 #include <sound.h>
12 #include <asm/gpio.h>
13 #include <asm/arch/power.h>
14 
15 static 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 
35 static 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 
42 static 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 
83 	/* Enable codec clock */
84 	set_xclkout();
85 
86 	return 0;
87 }
88 
89 static const struct sound_ops samsung_sound_ops = {
90 	.setup	= samsung_sound_setup,
91 	.play	= samsung_sound_play,
92 };
93 
94 static const struct udevice_id samsung_sound_ids[] = {
95 	{ .compatible = "google,snow-audio-max98095" },
96 	{ .compatible = "google,spring-audio-max98088" },
97 	{ .compatible = "samsung,smdk5420-audio-wm8994" },
98 	{ .compatible = "google,peach-audio-max98090" },
99 	{ }
100 };
101 
102 U_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 };
109