Simon Glass | f65a7d4 | 2018-12-27 20:15:22 -0700 | [diff] [blame] | 1 | // 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_SOUND |
| 8 | |
Simon Glass | f65a7d4 | 2018-12-27 20:15:22 -0700 | [diff] [blame] | 9 | #include <audio_codec.h> |
| 10 | #include <clk.h> |
| 11 | #include <dm.h> |
| 12 | #include <i2s.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | f65a7d4 | 2018-12-27 20:15:22 -0700 | [diff] [blame] | 14 | #include <misc.h> |
| 15 | #include <sound.h> |
Kever Yang | 9fbe17c | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 16 | #include <asm/arch-rockchip/periph.h> |
Simon Glass | f65a7d4 | 2018-12-27 20:15:22 -0700 | [diff] [blame] | 17 | #include <dm/pinctrl.h> |
| 18 | |
| 19 | static int rockchip_sound_setup(struct udevice *dev) |
| 20 | { |
| 21 | struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev); |
| 22 | struct i2s_uc_priv *i2c_priv = dev_get_uclass_priv(uc_priv->i2s); |
| 23 | int ret; |
| 24 | |
| 25 | if (uc_priv->setup_done) |
| 26 | return -EALREADY; |
| 27 | ret = audio_codec_set_params(uc_priv->codec, i2c_priv->id, |
| 28 | i2c_priv->samplingrate, |
| 29 | i2c_priv->samplingrate * i2c_priv->rfs, |
| 30 | i2c_priv->bitspersample, |
| 31 | i2c_priv->channels); |
| 32 | if (ret) |
| 33 | return ret; |
| 34 | uc_priv->setup_done = true; |
| 35 | |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | static int rockchip_sound_play(struct udevice *dev, void *data, uint data_size) |
| 40 | { |
| 41 | struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev); |
| 42 | |
| 43 | return i2s_tx_data(uc_priv->i2s, data, data_size); |
| 44 | } |
| 45 | |
| 46 | static int rockchip_sound_probe(struct udevice *dev) |
| 47 | { |
| 48 | struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev); |
| 49 | struct ofnode_phandle_args args; |
| 50 | struct udevice *pinctrl; |
| 51 | struct clk clk; |
| 52 | ofnode node; |
| 53 | int ret; |
| 54 | |
| 55 | node = ofnode_find_subnode(dev_ofnode(dev), "cpu"); |
| 56 | if (!ofnode_valid(node)) { |
| 57 | log_debug("Failed to find /cpu subnode\n"); |
| 58 | return -EINVAL; |
| 59 | } |
| 60 | ret = ofnode_parse_phandle_with_args(node, "sound-dai", |
| 61 | "#sound-dai-cells", 0, 0, &args); |
| 62 | if (ret) { |
| 63 | log_debug("Cannot find i2s phandle: %d\n", ret); |
| 64 | return ret; |
| 65 | } |
| 66 | ret = uclass_get_device_by_ofnode(UCLASS_I2S, args.node, &uc_priv->i2s); |
| 67 | if (ret) { |
| 68 | log_debug("Cannot find i2s: %d\n", ret); |
| 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | node = ofnode_find_subnode(dev_ofnode(dev), "codec"); |
| 73 | if (!ofnode_valid(node)) { |
| 74 | log_debug("Failed to find /codec subnode\n"); |
| 75 | return -EINVAL; |
| 76 | } |
| 77 | ret = ofnode_parse_phandle_with_args(node, "sound-dai", |
| 78 | "#sound-dai-cells", 0, 0, &args); |
| 79 | if (ret) { |
| 80 | log_debug("Cannot find codec phandle: %d\n", ret); |
| 81 | return ret; |
| 82 | } |
| 83 | ret = uclass_get_device_by_ofnode(UCLASS_AUDIO_CODEC, args.node, |
| 84 | &uc_priv->codec); |
| 85 | if (ret) { |
| 86 | log_debug("Cannot find audio codec: %d\n", ret); |
| 87 | return ret; |
| 88 | } |
| 89 | ret = clk_get_by_index(uc_priv->i2s, 1, &clk); |
| 90 | if (ret) { |
| 91 | log_debug("Cannot find clock: %d\n", ret); |
| 92 | return ret; |
| 93 | } |
| 94 | ret = clk_set_rate(&clk, 12288000); |
| 95 | if (ret < 0) { |
| 96 | log_debug("Cannot find clock: %d\n", ret); |
| 97 | return ret; |
| 98 | } |
| 99 | ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl); |
| 100 | if (ret) { |
| 101 | debug("%s: Cannot find pinctrl device\n", __func__); |
| 102 | return ret; |
| 103 | } |
| 104 | ret = pinctrl_request(pinctrl, PERIPH_ID_I2S, 0); |
| 105 | if (ret) { |
| 106 | debug("%s: Cannot select I2C pinctrl\n", __func__); |
| 107 | return ret; |
| 108 | } |
| 109 | |
| 110 | log_debug("Probed sound '%s' with codec '%s' and i2s '%s'\n", dev->name, |
| 111 | uc_priv->codec->name, uc_priv->i2s->name); |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | static const struct sound_ops rockchip_sound_ops = { |
| 117 | .setup = rockchip_sound_setup, |
| 118 | .play = rockchip_sound_play, |
| 119 | }; |
| 120 | |
| 121 | static const struct udevice_id rockchip_sound_ids[] = { |
| 122 | { .compatible = "rockchip,audio-max98090-jerry" }, |
| 123 | { } |
| 124 | }; |
| 125 | |
| 126 | U_BOOT_DRIVER(rockchip_sound) = { |
| 127 | .name = "rockchip_sound", |
| 128 | .id = UCLASS_SOUND, |
| 129 | .of_match = rockchip_sound_ids, |
| 130 | .probe = rockchip_sound_probe, |
| 131 | .ops = &rockchip_sound_ops, |
| 132 | }; |