|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 Intel Corporation. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <errno.h> |
| 8 | +#include <stdbool.h> |
| 9 | +#include <stdint.h> |
| 10 | +#include <zephyr/spinlock.h> |
| 11 | +#include <zephyr/devicetree.h> |
| 12 | +#include <zephyr/logging/log.h> |
| 13 | + |
| 14 | +#define DT_DRV_COMPAT intel_alh_dai |
| 15 | +#define LOG_DOMAIN dai_intel_alh |
| 16 | + |
| 17 | +LOG_MODULE_REGISTER(LOG_DOMAIN); |
| 18 | + |
| 19 | +#include "alh.h" |
| 20 | + |
| 21 | +/* Digital Audio interface formatting */ |
| 22 | +static int dai_alh_set_config_tplg(struct dai_intel_alh *dp, const void *spec_config) |
| 23 | +{ |
| 24 | + struct dai_intel_alh_pdata *alh = dai_get_drvdata(dp); |
| 25 | + const struct dai_intel_ipc3_alh_params *config = spec_config; |
| 26 | + |
| 27 | + if (config->channels && config->rate) { |
| 28 | + alh->params.channels = config->channels; |
| 29 | + alh->params.rate = config->rate; |
| 30 | + LOG_INF("%s channels %d rate %d", __func__, config->channels, config->rate); |
| 31 | + } |
| 32 | + |
| 33 | + alh->params.stream_id = config->stream_id; |
| 34 | + |
| 35 | + return 0; |
| 36 | +} |
| 37 | + |
| 38 | +static int dai_alh_set_config_blob(struct dai_intel_alh *dp, const void *spec_config) |
| 39 | +{ |
| 40 | + struct dai_intel_alh_pdata *alh = dai_get_drvdata(dp); |
| 41 | + const struct dai_intel_ipc4_alh_configuration_blob *blob = spec_config; |
| 42 | + const struct ipc4_alh_multi_gtw_cfg *alh_cfg = &blob->alh_cfg; |
| 43 | + |
| 44 | + alh->params.channels = ALH_CHANNELS_DEFAULT; |
| 45 | + alh->params.rate = ALH_RATE_DEFAULT; |
| 46 | + /* the LSB 8bits are for stream id */ |
| 47 | + alh->params.stream_id = alh_cfg->mapping[0].alh_id & 0xff; |
| 48 | + |
| 49 | + return 0; |
| 50 | +} |
| 51 | + |
| 52 | +static int dai_alh_trigger(const struct device *dev, enum dai_dir dir, |
| 53 | + enum dai_trigger_cmd cmd) |
| 54 | +{ |
| 55 | + LOG_DBG("cmd %d", cmd); |
| 56 | + |
| 57 | + return 0; |
| 58 | +} |
| 59 | + |
| 60 | +static void alh_claim_ownership(void) |
| 61 | +{ |
| 62 | +#if CONFIG_DAI_ALH_HAS_OWNERSHIP |
| 63 | + uint32_t ALHASCTL = DT_INST_PROP_BY_IDX(0, reg, 0); |
| 64 | + uint32_t ALHCSCTL = DT_INST_PROP_BY_IDX(0, reg, 1); |
| 65 | + |
| 66 | + sys_write32(sys_read32(ALHASCTL) | ALHASCTL_OSEL(0x3), ALHASCTL); |
| 67 | + sys_write32(sys_read32(ALHCSCTL) | ALHASCTL_OSEL(0x3), ALHCSCTL); |
| 68 | +#endif |
| 69 | +} |
| 70 | + |
| 71 | +static void alh_release_ownership(void) |
| 72 | +{ |
| 73 | +#if CONFIG_DAI_ALH_HAS_OWNERSHIP |
| 74 | + uint32_t ALHASCTL = DT_INST_PROP_BY_IDX(0, reg, 0); |
| 75 | + uint32_t ALHCSCTL = DT_INST_PROP_BY_IDX(0, reg, 1); |
| 76 | + |
| 77 | + sys_write32(sys_read32(ALHASCTL) | ALHASCTL_OSEL(0), ALHASCTL); |
| 78 | + sys_write32(sys_read32(ALHCSCTL) | ALHASCTL_OSEL(0), ALHCSCTL); |
| 79 | +#endif |
| 80 | +} |
| 81 | + |
| 82 | + |
| 83 | +static const struct dai_config *dai_alh_config_get(const struct device *dev, enum dai_dir dir) |
| 84 | +{ |
| 85 | + struct dai_config *params = (struct dai_config *)dev->config; |
| 86 | + struct dai_intel_alh *dp = (struct dai_intel_alh *)dev->data; |
| 87 | + struct dai_intel_alh_pdata *alh = dai_get_drvdata(dp); |
| 88 | + |
| 89 | + params->rate = alh->params.rate; |
| 90 | + params->channels = alh->params.channels; |
| 91 | + params->word_size = ALH_WORD_SIZE_DEFAULT; |
| 92 | + |
| 93 | + return params; |
| 94 | +} |
| 95 | + |
| 96 | +static int dai_alh_config_set(const struct device *dev, const struct dai_config *cfg, |
| 97 | + const void *bespoke_cfg) |
| 98 | +{ |
| 99 | + struct dai_intel_alh *dp = (struct dai_intel_alh *)dev->data; |
| 100 | + |
| 101 | + LOG_DBG("%s", __func__); |
| 102 | + |
| 103 | + if (cfg->type == DAI_INTEL_ALH) { |
| 104 | + return dai_alh_set_config_tplg(dp, bespoke_cfg); |
| 105 | + } else { |
| 106 | + return dai_alh_set_config_blob(dp, bespoke_cfg); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +static const struct dai_properties *dai_alh_get_properties(const struct device *dev, |
| 111 | + enum dai_dir dir, int stream_id) |
| 112 | +{ |
| 113 | + struct dai_intel_alh *dp = (struct dai_intel_alh *)dev->data; |
| 114 | + struct dai_intel_alh_pdata *alh = dai_get_drvdata(dp); |
| 115 | + struct dai_properties *prop = &alh->props; |
| 116 | + uint32_t offset = dir == DAI_DIR_PLAYBACK ? |
| 117 | + ALH_TXDA_OFFSET : ALH_RXDA_OFFSET; |
| 118 | + |
| 119 | + prop->fifo_address = dai_base(dp) + offset + ALH_STREAM_OFFSET * stream_id; |
| 120 | + prop->dma_hs_id = alh_handshake_map[stream_id]; |
| 121 | + prop->stream_id = alh->params.stream_id; |
| 122 | + |
| 123 | + LOG_DBG("dai_index %u", dp->index); |
| 124 | + LOG_DBG("fifo %u", prop->fifo_address); |
| 125 | + LOG_DBG("handshake %u", prop->dma_hs_id); |
| 126 | + |
| 127 | + return prop; |
| 128 | +} |
| 129 | + |
| 130 | +static int dai_alh_probe(const struct device *dev) |
| 131 | +{ |
| 132 | + struct dai_intel_alh *dp = (struct dai_intel_alh *)dev->data; |
| 133 | + k_spinlock_key_t key; |
| 134 | + |
| 135 | + LOG_DBG("%s", __func__); |
| 136 | + |
| 137 | + key = k_spin_lock(&dp->lock); |
| 138 | + |
| 139 | + if (dp->sref == 0) { |
| 140 | + alh_claim_ownership(); |
| 141 | + } |
| 142 | + |
| 143 | + dp->sref++; |
| 144 | + |
| 145 | + k_spin_unlock(&dp->lock, key); |
| 146 | + |
| 147 | + return 0; |
| 148 | +} |
| 149 | + |
| 150 | +static int dai_alh_remove(const struct device *dev) |
| 151 | +{ |
| 152 | + struct dai_intel_alh *dp = (struct dai_intel_alh *)dev->data; |
| 153 | + k_spinlock_key_t key; |
| 154 | + |
| 155 | + LOG_DBG("%s", __func__); |
| 156 | + |
| 157 | + key = k_spin_lock(&dp->lock); |
| 158 | + |
| 159 | + if (--dp->sref == 0) { |
| 160 | + alh_release_ownership(); |
| 161 | + } |
| 162 | + |
| 163 | + k_spin_unlock(&dp->lock, key); |
| 164 | + |
| 165 | + return 0; |
| 166 | +} |
| 167 | + |
| 168 | +static int alh_init(const struct device *dev) |
| 169 | +{ |
| 170 | + return 0; |
| 171 | +} |
| 172 | + |
| 173 | +static const struct dai_driver_api dai_intel_alh_api_funcs = { |
| 174 | + .probe = dai_alh_probe, |
| 175 | + .remove = dai_alh_remove, |
| 176 | + .config_set = dai_alh_config_set, |
| 177 | + .config_get = dai_alh_config_get, |
| 178 | + .trigger = dai_alh_trigger, |
| 179 | + .get_properties = dai_alh_get_properties, |
| 180 | +}; |
| 181 | + |
| 182 | +#define DAI_INTEL_ALH_DEVICE_INIT(n) \ |
| 183 | + static struct dai_config dai_intel_alh_config_##n; \ |
| 184 | + static struct dai_intel_alh dai_intel_alh_data_##n = { \ |
| 185 | + .index = (n / DAI_NUM_ALH_BI_DIR_LINKS_GROUP) << 8 | \ |
| 186 | + (n % DAI_NUM_ALH_BI_DIR_LINKS_GROUP), \ |
| 187 | + .plat_data = { \ |
| 188 | + .base = DT_INST_PROP_BY_IDX(n, reg, 0), \ |
| 189 | + .fifo_depth[DAI_DIR_PLAYBACK] = ALH_GPDMA_BURST_LENGTH, \ |
| 190 | + .fifo_depth[DAI_DIR_CAPTURE] = ALH_GPDMA_BURST_LENGTH, \ |
| 191 | + }, \ |
| 192 | + }; \ |
| 193 | + \ |
| 194 | + DEVICE_DT_INST_DEFINE(n, \ |
| 195 | + alh_init, NULL, \ |
| 196 | + &dai_intel_alh_data_##n, \ |
| 197 | + &dai_intel_alh_config_##n, \ |
| 198 | + POST_KERNEL, 32, \ |
| 199 | + &dai_intel_alh_api_funcs); |
| 200 | + |
| 201 | +DT_INST_FOREACH_STATUS_OKAY(DAI_INTEL_ALH_DEVICE_INIT) |
0 commit comments