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