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