Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | a77bf70 | 2014-02-27 13:26:20 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2012 Samsung Electronics |
| 4 | * R. Chandrasekar <rcsekar@samsung.com> |
Simon Glass | a77bf70 | 2014-02-27 13:26:20 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <malloc.h> |
| 8 | #include <common.h> |
| 9 | #include <asm/io.h> |
Masahiro Yamada | b08c8c4 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 10 | #include <linux/libfdt.h> |
Simon Glass | a77bf70 | 2014-02-27 13:26:20 -0700 | [diff] [blame] | 11 | #include <fdtdec.h> |
| 12 | #include <i2c.h> |
| 13 | #include <i2s.h> |
| 14 | #include <sound.h> |
| 15 | #include <asm/arch/sound.h> |
| 16 | #include "wm8994.h" |
| 17 | #include "max98095.h" |
| 18 | |
| 19 | /* defines */ |
| 20 | #define SOUND_400_HZ 400 |
| 21 | #define SOUND_BITS_IN_BYTE 8 |
| 22 | |
Simon Glass | 45863db | 2018-12-10 10:37:32 -0700 | [diff] [blame] | 23 | static struct i2s_uc_priv g_i2stx_pri; |
Simon Glass | a77bf70 | 2014-02-27 13:26:20 -0700 | [diff] [blame] | 24 | |
| 25 | /* |
| 26 | * get_sound_i2s_values gets values for i2s parameters |
| 27 | * |
Simon Glass | 45863db | 2018-12-10 10:37:32 -0700 | [diff] [blame] | 28 | * @param i2s_uc_priv i2s transmitter transfer param structure |
Simon Glass | a77bf70 | 2014-02-27 13:26:20 -0700 | [diff] [blame] | 29 | * @param blob FDT blob if enabled else NULL |
| 30 | */ |
Simon Glass | 45863db | 2018-12-10 10:37:32 -0700 | [diff] [blame] | 31 | static int get_sound_i2s_values(struct i2s_uc_priv *i2s, const void *blob) |
Simon Glass | a77bf70 | 2014-02-27 13:26:20 -0700 | [diff] [blame] | 32 | { |
| 33 | int node; |
| 34 | int error = 0; |
| 35 | int base; |
| 36 | |
| 37 | node = fdt_path_offset(blob, "i2s"); |
| 38 | if (node <= 0) { |
| 39 | debug("EXYNOS_SOUND: No node for sound in device tree\n"); |
| 40 | return -1; |
| 41 | } |
| 42 | |
| 43 | /* |
| 44 | * Get the pre-defined sound specific values from FDT. |
| 45 | * All of these are expected to be correct otherwise |
| 46 | * wrong register values in i2s setup parameters |
| 47 | * may result in no sound play. |
| 48 | */ |
| 49 | base = fdtdec_get_addr(blob, node, "reg"); |
| 50 | if (base == FDT_ADDR_T_NONE) { |
| 51 | debug("%s: Missing i2s base\n", __func__); |
| 52 | return -1; |
| 53 | } |
| 54 | i2s->base_address = base; |
| 55 | |
| 56 | i2s->audio_pll_clk = fdtdec_get_int(blob, |
| 57 | node, "samsung,i2s-epll-clock-frequency", -1); |
| 58 | error |= i2s->audio_pll_clk; |
| 59 | debug("audio_pll_clk = %d\n", i2s->audio_pll_clk); |
| 60 | i2s->samplingrate = fdtdec_get_int(blob, |
| 61 | node, "samsung,i2s-sampling-rate", -1); |
| 62 | error |= i2s->samplingrate; |
| 63 | debug("samplingrate = %d\n", i2s->samplingrate); |
| 64 | i2s->bitspersample = fdtdec_get_int(blob, |
| 65 | node, "samsung,i2s-bits-per-sample", -1); |
| 66 | error |= i2s->bitspersample; |
| 67 | debug("bitspersample = %d\n", i2s->bitspersample); |
| 68 | i2s->channels = fdtdec_get_int(blob, |
| 69 | node, "samsung,i2s-channels", -1); |
| 70 | error |= i2s->channels; |
| 71 | debug("channels = %d\n", i2s->channels); |
| 72 | i2s->rfs = fdtdec_get_int(blob, |
| 73 | node, "samsung,i2s-lr-clk-framesize", -1); |
| 74 | error |= i2s->rfs; |
| 75 | debug("rfs = %d\n", i2s->rfs); |
| 76 | i2s->bfs = fdtdec_get_int(blob, |
| 77 | node, "samsung,i2s-bit-clk-framesize", -1); |
| 78 | error |= i2s->bfs; |
| 79 | debug("bfs = %d\n", i2s->bfs); |
| 80 | |
| 81 | i2s->id = fdtdec_get_int(blob, node, "samsung,i2s-id", -1); |
| 82 | error |= i2s->id; |
| 83 | debug("id = %d\n", i2s->id); |
| 84 | |
| 85 | if (error == -1) { |
| 86 | debug("fail to get sound i2s node properties\n"); |
| 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * Init codec |
| 95 | * |
| 96 | * @param blob FDT blob |
| 97 | * @param pi2s_tx i2s parameters required by codec |
| 98 | * @return int value, 0 for success |
| 99 | */ |
Simon Glass | 45863db | 2018-12-10 10:37:32 -0700 | [diff] [blame] | 100 | static int codec_init(const void *blob, struct i2s_uc_priv *pi2s_tx) |
Simon Glass | a77bf70 | 2014-02-27 13:26:20 -0700 | [diff] [blame] | 101 | { |
| 102 | int ret; |
| 103 | const char *codectype; |
| 104 | int node; |
| 105 | |
| 106 | /* Get the node from FDT for sound */ |
| 107 | node = fdt_path_offset(blob, "i2s"); |
| 108 | if (node <= 0) { |
| 109 | debug("EXYNOS_SOUND: No node for sound in device tree\n"); |
| 110 | debug("node = %d\n", node); |
| 111 | return -1; |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * Get the pre-defined sound codec specific values from FDT. |
| 116 | * All of these are expected to be correct otherwise sound |
| 117 | * can not be played |
| 118 | */ |
| 119 | codectype = fdt_getprop(blob, node, "samsung,codec-type", NULL); |
| 120 | debug("device = %s\n", codectype); |
| 121 | if (!strcmp(codectype, "wm8994")) { |
| 122 | /* Check the codec type and initialise the same */ |
Simon Glass | 6c986cf | 2018-12-10 10:37:38 -0700 | [diff] [blame] | 123 | ret = wm8994_init(blob, pi2s_tx->id, pi2s_tx->samplingrate, |
Simon Glass | a77bf70 | 2014-02-27 13:26:20 -0700 | [diff] [blame] | 124 | (pi2s_tx->samplingrate * (pi2s_tx->rfs)), |
| 125 | pi2s_tx->bitspersample, pi2s_tx->channels); |
| 126 | } else if (!strcmp(codectype, "max98095")) { |
Simon Glass | 6c986cf | 2018-12-10 10:37:38 -0700 | [diff] [blame] | 127 | ret = max98095_init(blob, pi2s_tx->id, pi2s_tx->samplingrate, |
Simon Glass | a77bf70 | 2014-02-27 13:26:20 -0700 | [diff] [blame] | 128 | (pi2s_tx->samplingrate * (pi2s_tx->rfs)), |
| 129 | pi2s_tx->bitspersample); |
| 130 | } else { |
| 131 | debug("%s: Unknown codec type %s\n", __func__, codectype); |
| 132 | return -1; |
| 133 | } |
| 134 | |
| 135 | if (ret) { |
| 136 | debug("%s: Codec init failed\n", __func__); |
| 137 | return -1; |
| 138 | } |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | int sound_init(const void *blob) |
| 144 | { |
| 145 | int ret; |
Simon Glass | 45863db | 2018-12-10 10:37:32 -0700 | [diff] [blame] | 146 | struct i2s_uc_priv *pi2s_tx = &g_i2stx_pri; |
Simon Glass | a77bf70 | 2014-02-27 13:26:20 -0700 | [diff] [blame] | 147 | |
| 148 | /* Get the I2S Values */ |
| 149 | if (get_sound_i2s_values(pi2s_tx, blob) < 0) { |
| 150 | debug(" FDT I2S values failed\n"); |
| 151 | return -1; |
| 152 | } |
| 153 | |
| 154 | if (codec_init(blob, pi2s_tx) < 0) { |
| 155 | debug(" Codec init failed\n"); |
| 156 | return -1; |
| 157 | } |
| 158 | |
| 159 | ret = i2s_tx_init(pi2s_tx); |
| 160 | if (ret) { |
| 161 | debug("%s: Failed to init i2c transmit: ret=%d\n", __func__, |
| 162 | ret); |
| 163 | return ret; |
| 164 | } |
| 165 | |
| 166 | |
| 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | int sound_play(uint32_t msec, uint32_t frequency) |
| 171 | { |
| 172 | unsigned int *data; |
| 173 | unsigned long data_size; |
| 174 | unsigned int ret = 0; |
| 175 | |
| 176 | /*Buffer length computation */ |
| 177 | data_size = g_i2stx_pri.samplingrate * g_i2stx_pri.channels; |
| 178 | data_size *= (g_i2stx_pri.bitspersample / SOUND_BITS_IN_BYTE); |
| 179 | data = malloc(data_size); |
| 180 | |
| 181 | if (data == NULL) { |
| 182 | debug("%s: malloc failed\n", __func__); |
| 183 | return -1; |
| 184 | } |
| 185 | |
Simon Glass | 7d92b06 | 2018-11-15 19:56:13 -0700 | [diff] [blame] | 186 | sound_create_square_wave(g_i2stx_pri.samplingrate, |
| 187 | (unsigned short *)data, |
Simon Glass | a77bf70 | 2014-02-27 13:26:20 -0700 | [diff] [blame] | 188 | data_size / sizeof(unsigned short), |
| 189 | frequency); |
| 190 | |
| 191 | while (msec >= 1000) { |
| 192 | ret = i2s_transfer_tx_data(&g_i2stx_pri, data, |
| 193 | (data_size / sizeof(int))); |
| 194 | msec -= 1000; |
| 195 | } |
| 196 | if (msec) { |
| 197 | unsigned long size = |
| 198 | (data_size * msec) / (sizeof(int) * 1000); |
| 199 | |
| 200 | ret = i2s_transfer_tx_data(&g_i2stx_pri, data, size); |
| 201 | } |
| 202 | |
| 203 | free(data); |
| 204 | |
| 205 | return ret; |
| 206 | } |