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 
14 static int samsung_sound_setup(struct udevice *dev)
15 {
16 	struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev);
17 	struct i2s_uc_priv *i2c_priv = dev_get_uclass_priv(uc_priv->i2s);
18 	int ret;
19 
20 	if (uc_priv->setup_done)
21 		return -EALREADY;
22 	ret = audio_codec_set_params(uc_priv->codec, i2c_priv->id,
23 				     i2c_priv->samplingrate,
24 				     i2c_priv->samplingrate * i2c_priv->rfs,
25 				     i2c_priv->bitspersample,
26 				     i2c_priv->channels);
27 	if (ret)
28 		return ret;
29 	uc_priv->setup_done = true;
30 
31 	return 0;
32 }
33 
34 static int samsung_sound_play(struct udevice *dev, void *data, uint data_size)
35 {
36 	struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev);
37 
38 	return i2s_tx_data(uc_priv->i2s, data, data_size);
39 }
40 
41 static int samsung_sound_probe(struct udevice *dev)
42 {
43 	struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev);
44 	struct ofnode_phandle_args args;
45 	struct gpio_desc en_gpio;
46 	ofnode node;
47 	int ret;
48 
49 	ret = gpio_request_by_name(dev, "codec-enable-gpio", 0, &en_gpio,
50 				   GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
51 
52 	/* Turn on the GPIO which connects to the codec's "enable" line. */
53 	if (!ret)
54 		gpio_set_pull(gpio_get_number(&en_gpio), S5P_GPIO_PULL_NONE);
55 
56 	ret = uclass_get_device_by_phandle(UCLASS_AUDIO_CODEC, dev,
57 					   "samsung,audio-codec",
58 					   &uc_priv->codec);
59 	if (ret) {
60 		debug("Failed to probe audio codec\n");
61 		return ret;
62 	}
63 	node = ofnode_find_subnode(dev_ofnode(dev), "cpu");
64 	if (!ofnode_valid(node)) {
65 		debug("Failed to find /cpu subnode\n");
66 		return -EINVAL;
67 	}
68 	ret = ofnode_parse_phandle_with_args(node, "sound-dai",
69 					     "#sound-dai-cells", 0, 0, &args);
70 	if (ret) {
71 		debug("Cannot find phandle: %d\n", ret);
72 		return ret;
73 	}
74 	ret = uclass_get_device_by_ofnode(UCLASS_I2S, args.node, &uc_priv->i2s);
75 	if (ret) {
76 		debug("Cannot find i2s: %d\n", ret);
77 		return ret;
78 	}
79 	debug("Probed sound '%s' with codec '%s' and i2s '%s'\n", dev->name,
80 	      uc_priv->codec->name, uc_priv->i2s->name);
81 
82 	return 0;
83 }
84 
85 static const struct sound_ops samsung_sound_ops = {
86 	.setup	= samsung_sound_setup,
87 	.play	= samsung_sound_play,
88 };
89 
90 static const struct udevice_id samsung_sound_ids[] = {
91 	{ .compatible = "google,snow-audio-max98095" },
92 	{ .compatible = "google,spring-audio-max98095" },
93 	{ .compatible = "samsung,smdk5420-audio-wm8994" },
94 	{ .compatible = "google,peach-audio-max98090" },
95 	{ }
96 };
97 
98 U_BOOT_DRIVER(samsung_sound) = {
99 	.name		= "samsung_sound",
100 	.id		= UCLASS_SOUND,
101 	.of_match	= samsung_sound_ids,
102 	.probe		= samsung_sound_probe,
103 	.ops		= &samsung_sound_ops,
104 };
105