Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1 | /* |
| 2 | * wm_adsp.c -- Wolfson ADSP support |
| 3 | * |
| 4 | * Copyright 2012 Wolfson Microelectronics plc |
| 5 | * |
| 6 | * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/moduleparam.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/firmware.h> |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 18 | #include <linux/list.h> |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 19 | #include <linux/pm.h> |
| 20 | #include <linux/pm_runtime.h> |
| 21 | #include <linux/regmap.h> |
Mark Brown | 973838a | 2012-11-28 17:20:32 +0000 | [diff] [blame] | 22 | #include <linux/regulator/consumer.h> |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Charles Keepax | cdcd7f7 | 2014-11-14 15:40:45 +0000 | [diff] [blame] | 24 | #include <linux/vmalloc.h> |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 25 | #include <linux/workqueue.h> |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 26 | #include <linux/debugfs.h> |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 27 | #include <sound/core.h> |
| 28 | #include <sound/pcm.h> |
| 29 | #include <sound/pcm_params.h> |
| 30 | #include <sound/soc.h> |
| 31 | #include <sound/jack.h> |
| 32 | #include <sound/initval.h> |
| 33 | #include <sound/tlv.h> |
| 34 | |
| 35 | #include <linux/mfd/arizona/registers.h> |
| 36 | |
Mark Brown | dc91428 | 2013-02-18 19:09:23 +0000 | [diff] [blame] | 37 | #include "arizona.h" |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 38 | #include "wm_adsp.h" |
| 39 | |
| 40 | #define adsp_crit(_dsp, fmt, ...) \ |
| 41 | dev_crit(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__) |
| 42 | #define adsp_err(_dsp, fmt, ...) \ |
| 43 | dev_err(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__) |
| 44 | #define adsp_warn(_dsp, fmt, ...) \ |
| 45 | dev_warn(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__) |
| 46 | #define adsp_info(_dsp, fmt, ...) \ |
| 47 | dev_info(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__) |
| 48 | #define adsp_dbg(_dsp, fmt, ...) \ |
| 49 | dev_dbg(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__) |
| 50 | |
| 51 | #define ADSP1_CONTROL_1 0x00 |
| 52 | #define ADSP1_CONTROL_2 0x02 |
| 53 | #define ADSP1_CONTROL_3 0x03 |
| 54 | #define ADSP1_CONTROL_4 0x04 |
| 55 | #define ADSP1_CONTROL_5 0x06 |
| 56 | #define ADSP1_CONTROL_6 0x07 |
| 57 | #define ADSP1_CONTROL_7 0x08 |
| 58 | #define ADSP1_CONTROL_8 0x09 |
| 59 | #define ADSP1_CONTROL_9 0x0A |
| 60 | #define ADSP1_CONTROL_10 0x0B |
| 61 | #define ADSP1_CONTROL_11 0x0C |
| 62 | #define ADSP1_CONTROL_12 0x0D |
| 63 | #define ADSP1_CONTROL_13 0x0F |
| 64 | #define ADSP1_CONTROL_14 0x10 |
| 65 | #define ADSP1_CONTROL_15 0x11 |
| 66 | #define ADSP1_CONTROL_16 0x12 |
| 67 | #define ADSP1_CONTROL_17 0x13 |
| 68 | #define ADSP1_CONTROL_18 0x14 |
| 69 | #define ADSP1_CONTROL_19 0x16 |
| 70 | #define ADSP1_CONTROL_20 0x17 |
| 71 | #define ADSP1_CONTROL_21 0x18 |
| 72 | #define ADSP1_CONTROL_22 0x1A |
| 73 | #define ADSP1_CONTROL_23 0x1B |
| 74 | #define ADSP1_CONTROL_24 0x1C |
| 75 | #define ADSP1_CONTROL_25 0x1E |
| 76 | #define ADSP1_CONTROL_26 0x20 |
| 77 | #define ADSP1_CONTROL_27 0x21 |
| 78 | #define ADSP1_CONTROL_28 0x22 |
| 79 | #define ADSP1_CONTROL_29 0x23 |
| 80 | #define ADSP1_CONTROL_30 0x24 |
| 81 | #define ADSP1_CONTROL_31 0x26 |
| 82 | |
| 83 | /* |
| 84 | * ADSP1 Control 19 |
| 85 | */ |
| 86 | #define ADSP1_WDMA_BUFFER_LENGTH_MASK 0x00FF /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */ |
| 87 | #define ADSP1_WDMA_BUFFER_LENGTH_SHIFT 0 /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */ |
| 88 | #define ADSP1_WDMA_BUFFER_LENGTH_WIDTH 8 /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */ |
| 89 | |
| 90 | |
| 91 | /* |
| 92 | * ADSP1 Control 30 |
| 93 | */ |
| 94 | #define ADSP1_DBG_CLK_ENA 0x0008 /* DSP1_DBG_CLK_ENA */ |
| 95 | #define ADSP1_DBG_CLK_ENA_MASK 0x0008 /* DSP1_DBG_CLK_ENA */ |
| 96 | #define ADSP1_DBG_CLK_ENA_SHIFT 3 /* DSP1_DBG_CLK_ENA */ |
| 97 | #define ADSP1_DBG_CLK_ENA_WIDTH 1 /* DSP1_DBG_CLK_ENA */ |
| 98 | #define ADSP1_SYS_ENA 0x0004 /* DSP1_SYS_ENA */ |
| 99 | #define ADSP1_SYS_ENA_MASK 0x0004 /* DSP1_SYS_ENA */ |
| 100 | #define ADSP1_SYS_ENA_SHIFT 2 /* DSP1_SYS_ENA */ |
| 101 | #define ADSP1_SYS_ENA_WIDTH 1 /* DSP1_SYS_ENA */ |
| 102 | #define ADSP1_CORE_ENA 0x0002 /* DSP1_CORE_ENA */ |
| 103 | #define ADSP1_CORE_ENA_MASK 0x0002 /* DSP1_CORE_ENA */ |
| 104 | #define ADSP1_CORE_ENA_SHIFT 1 /* DSP1_CORE_ENA */ |
| 105 | #define ADSP1_CORE_ENA_WIDTH 1 /* DSP1_CORE_ENA */ |
| 106 | #define ADSP1_START 0x0001 /* DSP1_START */ |
| 107 | #define ADSP1_START_MASK 0x0001 /* DSP1_START */ |
| 108 | #define ADSP1_START_SHIFT 0 /* DSP1_START */ |
| 109 | #define ADSP1_START_WIDTH 1 /* DSP1_START */ |
| 110 | |
Chris Rattray | 94e205b | 2013-01-18 08:43:09 +0000 | [diff] [blame] | 111 | /* |
| 112 | * ADSP1 Control 31 |
| 113 | */ |
| 114 | #define ADSP1_CLK_SEL_MASK 0x0007 /* CLK_SEL_ENA */ |
| 115 | #define ADSP1_CLK_SEL_SHIFT 0 /* CLK_SEL_ENA */ |
| 116 | #define ADSP1_CLK_SEL_WIDTH 3 /* CLK_SEL_ENA */ |
| 117 | |
Mark Brown | 2d30b57 | 2013-01-28 20:18:17 +0800 | [diff] [blame] | 118 | #define ADSP2_CONTROL 0x0 |
| 119 | #define ADSP2_CLOCKING 0x1 |
| 120 | #define ADSP2_STATUS1 0x4 |
| 121 | #define ADSP2_WDMA_CONFIG_1 0x30 |
| 122 | #define ADSP2_WDMA_CONFIG_2 0x31 |
| 123 | #define ADSP2_RDMA_CONFIG_1 0x34 |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 124 | |
Richard Fitzgerald | 10337b0 | 2015-05-29 10:23:07 +0100 | [diff] [blame] | 125 | #define ADSP2_SCRATCH0 0x40 |
| 126 | #define ADSP2_SCRATCH1 0x41 |
| 127 | #define ADSP2_SCRATCH2 0x42 |
| 128 | #define ADSP2_SCRATCH3 0x43 |
| 129 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 130 | /* |
| 131 | * ADSP2 Control |
| 132 | */ |
| 133 | |
| 134 | #define ADSP2_MEM_ENA 0x0010 /* DSP1_MEM_ENA */ |
| 135 | #define ADSP2_MEM_ENA_MASK 0x0010 /* DSP1_MEM_ENA */ |
| 136 | #define ADSP2_MEM_ENA_SHIFT 4 /* DSP1_MEM_ENA */ |
| 137 | #define ADSP2_MEM_ENA_WIDTH 1 /* DSP1_MEM_ENA */ |
| 138 | #define ADSP2_SYS_ENA 0x0004 /* DSP1_SYS_ENA */ |
| 139 | #define ADSP2_SYS_ENA_MASK 0x0004 /* DSP1_SYS_ENA */ |
| 140 | #define ADSP2_SYS_ENA_SHIFT 2 /* DSP1_SYS_ENA */ |
| 141 | #define ADSP2_SYS_ENA_WIDTH 1 /* DSP1_SYS_ENA */ |
| 142 | #define ADSP2_CORE_ENA 0x0002 /* DSP1_CORE_ENA */ |
| 143 | #define ADSP2_CORE_ENA_MASK 0x0002 /* DSP1_CORE_ENA */ |
| 144 | #define ADSP2_CORE_ENA_SHIFT 1 /* DSP1_CORE_ENA */ |
| 145 | #define ADSP2_CORE_ENA_WIDTH 1 /* DSP1_CORE_ENA */ |
| 146 | #define ADSP2_START 0x0001 /* DSP1_START */ |
| 147 | #define ADSP2_START_MASK 0x0001 /* DSP1_START */ |
| 148 | #define ADSP2_START_SHIFT 0 /* DSP1_START */ |
| 149 | #define ADSP2_START_WIDTH 1 /* DSP1_START */ |
| 150 | |
| 151 | /* |
Mark Brown | 973838a | 2012-11-28 17:20:32 +0000 | [diff] [blame] | 152 | * ADSP2 clocking |
| 153 | */ |
| 154 | #define ADSP2_CLK_SEL_MASK 0x0007 /* CLK_SEL_ENA */ |
| 155 | #define ADSP2_CLK_SEL_SHIFT 0 /* CLK_SEL_ENA */ |
| 156 | #define ADSP2_CLK_SEL_WIDTH 3 /* CLK_SEL_ENA */ |
| 157 | |
| 158 | /* |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 159 | * ADSP2 Status 1 |
| 160 | */ |
| 161 | #define ADSP2_RAM_RDY 0x0001 |
| 162 | #define ADSP2_RAM_RDY_MASK 0x0001 |
| 163 | #define ADSP2_RAM_RDY_SHIFT 0 |
| 164 | #define ADSP2_RAM_RDY_WIDTH 1 |
| 165 | |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 166 | struct wm_adsp_buf { |
| 167 | struct list_head list; |
| 168 | void *buf; |
| 169 | }; |
| 170 | |
| 171 | static struct wm_adsp_buf *wm_adsp_buf_alloc(const void *src, size_t len, |
| 172 | struct list_head *list) |
| 173 | { |
| 174 | struct wm_adsp_buf *buf = kzalloc(sizeof(*buf), GFP_KERNEL); |
| 175 | |
| 176 | if (buf == NULL) |
| 177 | return NULL; |
| 178 | |
Charles Keepax | cdcd7f7 | 2014-11-14 15:40:45 +0000 | [diff] [blame] | 179 | buf->buf = vmalloc(len); |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 180 | if (!buf->buf) { |
Charles Keepax | cdcd7f7 | 2014-11-14 15:40:45 +0000 | [diff] [blame] | 181 | vfree(buf); |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 182 | return NULL; |
| 183 | } |
Charles Keepax | cdcd7f7 | 2014-11-14 15:40:45 +0000 | [diff] [blame] | 184 | memcpy(buf->buf, src, len); |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 185 | |
| 186 | if (list) |
| 187 | list_add_tail(&buf->list, list); |
| 188 | |
| 189 | return buf; |
| 190 | } |
| 191 | |
| 192 | static void wm_adsp_buf_free(struct list_head *list) |
| 193 | { |
| 194 | while (!list_empty(list)) { |
| 195 | struct wm_adsp_buf *buf = list_first_entry(list, |
| 196 | struct wm_adsp_buf, |
| 197 | list); |
| 198 | list_del(&buf->list); |
Charles Keepax | cdcd7f7 | 2014-11-14 15:40:45 +0000 | [diff] [blame] | 199 | vfree(buf->buf); |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 200 | kfree(buf); |
| 201 | } |
| 202 | } |
| 203 | |
Charles Keepax | 04d1300 | 2015-11-26 14:01:52 +0000 | [diff] [blame] | 204 | #define WM_ADSP_FW_MBC_VSS 0 |
| 205 | #define WM_ADSP_FW_HIFI 1 |
| 206 | #define WM_ADSP_FW_TX 2 |
| 207 | #define WM_ADSP_FW_TX_SPK 3 |
| 208 | #define WM_ADSP_FW_RX 4 |
| 209 | #define WM_ADSP_FW_RX_ANC 5 |
| 210 | #define WM_ADSP_FW_CTRL 6 |
| 211 | #define WM_ADSP_FW_ASR 7 |
| 212 | #define WM_ADSP_FW_TRACE 8 |
| 213 | #define WM_ADSP_FW_SPK_PROT 9 |
| 214 | #define WM_ADSP_FW_MISC 10 |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 215 | |
Charles Keepax | 04d1300 | 2015-11-26 14:01:52 +0000 | [diff] [blame] | 216 | #define WM_ADSP_NUM_FW 11 |
Mark Brown | dd84f92 | 2013-03-08 15:25:58 +0800 | [diff] [blame] | 217 | |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 218 | static const char *wm_adsp_fw_text[WM_ADSP_NUM_FW] = { |
Charles Keepax | 04d1300 | 2015-11-26 14:01:52 +0000 | [diff] [blame] | 219 | [WM_ADSP_FW_MBC_VSS] = "MBC/VSS", |
| 220 | [WM_ADSP_FW_HIFI] = "MasterHiFi", |
| 221 | [WM_ADSP_FW_TX] = "Tx", |
| 222 | [WM_ADSP_FW_TX_SPK] = "Tx Speaker", |
| 223 | [WM_ADSP_FW_RX] = "Rx", |
| 224 | [WM_ADSP_FW_RX_ANC] = "Rx ANC", |
| 225 | [WM_ADSP_FW_CTRL] = "Voice Ctrl", |
| 226 | [WM_ADSP_FW_ASR] = "ASR Assist", |
| 227 | [WM_ADSP_FW_TRACE] = "Dbg Trace", |
| 228 | [WM_ADSP_FW_SPK_PROT] = "Protection", |
| 229 | [WM_ADSP_FW_MISC] = "Misc", |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 230 | }; |
| 231 | |
Charles Keepax | 406abc9 | 2015-12-15 11:29:45 +0000 | [diff] [blame^] | 232 | struct wm_adsp_compr { |
| 233 | struct wm_adsp *dsp; |
| 234 | |
| 235 | struct snd_compr_stream *stream; |
| 236 | struct snd_compressed_buffer size; |
| 237 | }; |
| 238 | |
| 239 | #define WM_ADSP_DATA_WORD_SIZE 3 |
| 240 | |
| 241 | #define WM_ADSP_MIN_FRAGMENTS 1 |
| 242 | #define WM_ADSP_MAX_FRAGMENTS 256 |
| 243 | #define WM_ADSP_MIN_FRAGMENT_SIZE (64 * WM_ADSP_DATA_WORD_SIZE) |
| 244 | #define WM_ADSP_MAX_FRAGMENT_SIZE (4096 * WM_ADSP_DATA_WORD_SIZE) |
| 245 | |
| 246 | struct wm_adsp_fw_caps { |
| 247 | u32 id; |
| 248 | struct snd_codec_desc desc; |
| 249 | }; |
| 250 | |
| 251 | static const struct wm_adsp_fw_caps ez2control_caps[] = { |
| 252 | { |
| 253 | .id = SND_AUDIOCODEC_BESPOKE, |
| 254 | .desc = { |
| 255 | .max_ch = 1, |
| 256 | .sample_rates = { 16000 }, |
| 257 | .num_sample_rates = 1, |
| 258 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 259 | }, |
| 260 | }, |
| 261 | }; |
| 262 | |
| 263 | static const struct { |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 264 | const char *file; |
Charles Keepax | 406abc9 | 2015-12-15 11:29:45 +0000 | [diff] [blame^] | 265 | int compr_direction; |
| 266 | int num_caps; |
| 267 | const struct wm_adsp_fw_caps *caps; |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 268 | } wm_adsp_fw[WM_ADSP_NUM_FW] = { |
Charles Keepax | 04d1300 | 2015-11-26 14:01:52 +0000 | [diff] [blame] | 269 | [WM_ADSP_FW_MBC_VSS] = { .file = "mbc-vss" }, |
| 270 | [WM_ADSP_FW_HIFI] = { .file = "hifi" }, |
| 271 | [WM_ADSP_FW_TX] = { .file = "tx" }, |
| 272 | [WM_ADSP_FW_TX_SPK] = { .file = "tx-spk" }, |
| 273 | [WM_ADSP_FW_RX] = { .file = "rx" }, |
| 274 | [WM_ADSP_FW_RX_ANC] = { .file = "rx-anc" }, |
Charles Keepax | 406abc9 | 2015-12-15 11:29:45 +0000 | [diff] [blame^] | 275 | [WM_ADSP_FW_CTRL] = { |
| 276 | .file = "ctrl", |
| 277 | .compr_direction = SND_COMPRESS_CAPTURE, |
| 278 | .num_caps = ARRAY_SIZE(ez2control_caps), |
| 279 | .caps = ez2control_caps, |
| 280 | }, |
Charles Keepax | 04d1300 | 2015-11-26 14:01:52 +0000 | [diff] [blame] | 281 | [WM_ADSP_FW_ASR] = { .file = "asr" }, |
| 282 | [WM_ADSP_FW_TRACE] = { .file = "trace" }, |
| 283 | [WM_ADSP_FW_SPK_PROT] = { .file = "spk-prot" }, |
| 284 | [WM_ADSP_FW_MISC] = { .file = "misc" }, |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 285 | }; |
| 286 | |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 287 | struct wm_coeff_ctl_ops { |
| 288 | int (*xget)(struct snd_kcontrol *kcontrol, |
| 289 | struct snd_ctl_elem_value *ucontrol); |
| 290 | int (*xput)(struct snd_kcontrol *kcontrol, |
| 291 | struct snd_ctl_elem_value *ucontrol); |
| 292 | int (*xinfo)(struct snd_kcontrol *kcontrol, |
| 293 | struct snd_ctl_elem_info *uinfo); |
| 294 | }; |
| 295 | |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 296 | struct wm_coeff_ctl { |
| 297 | const char *name; |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 298 | const char *fw_name; |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 299 | struct wm_adsp_alg_region alg_region; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 300 | struct wm_coeff_ctl_ops ops; |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 301 | struct wm_adsp *dsp; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 302 | unsigned int enabled:1; |
| 303 | struct list_head list; |
| 304 | void *cache; |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 305 | unsigned int offset; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 306 | size_t len; |
Dimitris Papastamos | 0c2e3f3 | 2013-05-28 12:01:50 +0100 | [diff] [blame] | 307 | unsigned int set:1; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 308 | struct snd_kcontrol *kcontrol; |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 309 | unsigned int flags; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 310 | }; |
| 311 | |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 312 | #ifdef CONFIG_DEBUG_FS |
| 313 | static void wm_adsp_debugfs_save_wmfwname(struct wm_adsp *dsp, const char *s) |
| 314 | { |
| 315 | char *tmp = kasprintf(GFP_KERNEL, "%s\n", s); |
| 316 | |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 317 | kfree(dsp->wmfw_file_name); |
| 318 | dsp->wmfw_file_name = tmp; |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | static void wm_adsp_debugfs_save_binname(struct wm_adsp *dsp, const char *s) |
| 322 | { |
| 323 | char *tmp = kasprintf(GFP_KERNEL, "%s\n", s); |
| 324 | |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 325 | kfree(dsp->bin_file_name); |
| 326 | dsp->bin_file_name = tmp; |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | static void wm_adsp_debugfs_clear(struct wm_adsp *dsp) |
| 330 | { |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 331 | kfree(dsp->wmfw_file_name); |
| 332 | kfree(dsp->bin_file_name); |
| 333 | dsp->wmfw_file_name = NULL; |
| 334 | dsp->bin_file_name = NULL; |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | static ssize_t wm_adsp_debugfs_wmfw_read(struct file *file, |
| 338 | char __user *user_buf, |
| 339 | size_t count, loff_t *ppos) |
| 340 | { |
| 341 | struct wm_adsp *dsp = file->private_data; |
| 342 | ssize_t ret; |
| 343 | |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 344 | mutex_lock(&dsp->pwr_lock); |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 345 | |
| 346 | if (!dsp->wmfw_file_name || !dsp->running) |
| 347 | ret = 0; |
| 348 | else |
| 349 | ret = simple_read_from_buffer(user_buf, count, ppos, |
| 350 | dsp->wmfw_file_name, |
| 351 | strlen(dsp->wmfw_file_name)); |
| 352 | |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 353 | mutex_unlock(&dsp->pwr_lock); |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 354 | return ret; |
| 355 | } |
| 356 | |
| 357 | static ssize_t wm_adsp_debugfs_bin_read(struct file *file, |
| 358 | char __user *user_buf, |
| 359 | size_t count, loff_t *ppos) |
| 360 | { |
| 361 | struct wm_adsp *dsp = file->private_data; |
| 362 | ssize_t ret; |
| 363 | |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 364 | mutex_lock(&dsp->pwr_lock); |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 365 | |
| 366 | if (!dsp->bin_file_name || !dsp->running) |
| 367 | ret = 0; |
| 368 | else |
| 369 | ret = simple_read_from_buffer(user_buf, count, ppos, |
| 370 | dsp->bin_file_name, |
| 371 | strlen(dsp->bin_file_name)); |
| 372 | |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 373 | mutex_unlock(&dsp->pwr_lock); |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 374 | return ret; |
| 375 | } |
| 376 | |
| 377 | static const struct { |
| 378 | const char *name; |
| 379 | const struct file_operations fops; |
| 380 | } wm_adsp_debugfs_fops[] = { |
| 381 | { |
| 382 | .name = "wmfw_file_name", |
| 383 | .fops = { |
| 384 | .open = simple_open, |
| 385 | .read = wm_adsp_debugfs_wmfw_read, |
| 386 | }, |
| 387 | }, |
| 388 | { |
| 389 | .name = "bin_file_name", |
| 390 | .fops = { |
| 391 | .open = simple_open, |
| 392 | .read = wm_adsp_debugfs_bin_read, |
| 393 | }, |
| 394 | }, |
| 395 | }; |
| 396 | |
| 397 | static void wm_adsp2_init_debugfs(struct wm_adsp *dsp, |
| 398 | struct snd_soc_codec *codec) |
| 399 | { |
| 400 | struct dentry *root = NULL; |
| 401 | char *root_name; |
| 402 | int i; |
| 403 | |
| 404 | if (!codec->component.debugfs_root) { |
| 405 | adsp_err(dsp, "No codec debugfs root\n"); |
| 406 | goto err; |
| 407 | } |
| 408 | |
| 409 | root_name = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 410 | if (!root_name) |
| 411 | goto err; |
| 412 | |
| 413 | snprintf(root_name, PAGE_SIZE, "dsp%d", dsp->num); |
| 414 | root = debugfs_create_dir(root_name, codec->component.debugfs_root); |
| 415 | kfree(root_name); |
| 416 | |
| 417 | if (!root) |
| 418 | goto err; |
| 419 | |
| 420 | if (!debugfs_create_bool("running", S_IRUGO, root, &dsp->running)) |
| 421 | goto err; |
| 422 | |
| 423 | if (!debugfs_create_x32("fw_id", S_IRUGO, root, &dsp->fw_id)) |
| 424 | goto err; |
| 425 | |
| 426 | if (!debugfs_create_x32("fw_version", S_IRUGO, root, |
| 427 | &dsp->fw_id_version)) |
| 428 | goto err; |
| 429 | |
| 430 | for (i = 0; i < ARRAY_SIZE(wm_adsp_debugfs_fops); ++i) { |
| 431 | if (!debugfs_create_file(wm_adsp_debugfs_fops[i].name, |
| 432 | S_IRUGO, root, dsp, |
| 433 | &wm_adsp_debugfs_fops[i].fops)) |
| 434 | goto err; |
| 435 | } |
| 436 | |
| 437 | dsp->debugfs_root = root; |
| 438 | return; |
| 439 | |
| 440 | err: |
| 441 | debugfs_remove_recursive(root); |
| 442 | adsp_err(dsp, "Failed to create debugfs\n"); |
| 443 | } |
| 444 | |
| 445 | static void wm_adsp2_cleanup_debugfs(struct wm_adsp *dsp) |
| 446 | { |
| 447 | wm_adsp_debugfs_clear(dsp); |
| 448 | debugfs_remove_recursive(dsp->debugfs_root); |
| 449 | } |
| 450 | #else |
| 451 | static inline void wm_adsp2_init_debugfs(struct wm_adsp *dsp, |
| 452 | struct snd_soc_codec *codec) |
| 453 | { |
| 454 | } |
| 455 | |
| 456 | static inline void wm_adsp2_cleanup_debugfs(struct wm_adsp *dsp) |
| 457 | { |
| 458 | } |
| 459 | |
| 460 | static inline void wm_adsp_debugfs_save_wmfwname(struct wm_adsp *dsp, |
| 461 | const char *s) |
| 462 | { |
| 463 | } |
| 464 | |
| 465 | static inline void wm_adsp_debugfs_save_binname(struct wm_adsp *dsp, |
| 466 | const char *s) |
| 467 | { |
| 468 | } |
| 469 | |
| 470 | static inline void wm_adsp_debugfs_clear(struct wm_adsp *dsp) |
| 471 | { |
| 472 | } |
| 473 | #endif |
| 474 | |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 475 | static int wm_adsp_fw_get(struct snd_kcontrol *kcontrol, |
| 476 | struct snd_ctl_elem_value *ucontrol) |
| 477 | { |
Lars-Peter Clausen | ea53bf7 | 2014-03-18 09:02:04 +0100 | [diff] [blame] | 478 | struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol); |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 479 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 480 | struct wm_adsp *dsp = snd_soc_codec_get_drvdata(codec); |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 481 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 482 | ucontrol->value.integer.value[0] = dsp[e->shift_l].fw; |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 483 | |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | static int wm_adsp_fw_put(struct snd_kcontrol *kcontrol, |
| 488 | struct snd_ctl_elem_value *ucontrol) |
| 489 | { |
Lars-Peter Clausen | ea53bf7 | 2014-03-18 09:02:04 +0100 | [diff] [blame] | 490 | struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol); |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 491 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 492 | struct wm_adsp *dsp = snd_soc_codec_get_drvdata(codec); |
Charles Keepax | d27c5e1 | 2015-12-08 16:08:28 +0000 | [diff] [blame] | 493 | int ret = 0; |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 494 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 495 | if (ucontrol->value.integer.value[0] == dsp[e->shift_l].fw) |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 496 | return 0; |
| 497 | |
| 498 | if (ucontrol->value.integer.value[0] >= WM_ADSP_NUM_FW) |
| 499 | return -EINVAL; |
| 500 | |
Charles Keepax | d27c5e1 | 2015-12-08 16:08:28 +0000 | [diff] [blame] | 501 | mutex_lock(&dsp[e->shift_l].pwr_lock); |
| 502 | |
Charles Keepax | 406abc9 | 2015-12-15 11:29:45 +0000 | [diff] [blame^] | 503 | if (dsp[e->shift_l].running || dsp[e->shift_l].compr) |
Charles Keepax | d27c5e1 | 2015-12-08 16:08:28 +0000 | [diff] [blame] | 504 | ret = -EBUSY; |
| 505 | else |
| 506 | dsp[e->shift_l].fw = ucontrol->value.integer.value[0]; |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 507 | |
Charles Keepax | d27c5e1 | 2015-12-08 16:08:28 +0000 | [diff] [blame] | 508 | mutex_unlock(&dsp[e->shift_l].pwr_lock); |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 509 | |
Charles Keepax | d27c5e1 | 2015-12-08 16:08:28 +0000 | [diff] [blame] | 510 | return ret; |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | static const struct soc_enum wm_adsp_fw_enum[] = { |
| 514 | SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text), |
| 515 | SOC_ENUM_SINGLE(0, 1, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text), |
| 516 | SOC_ENUM_SINGLE(0, 2, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text), |
| 517 | SOC_ENUM_SINGLE(0, 3, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text), |
| 518 | }; |
| 519 | |
Richard Fitzgerald | 336d044 | 2015-06-18 13:43:19 +0100 | [diff] [blame] | 520 | const struct snd_kcontrol_new wm_adsp_fw_controls[] = { |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 521 | SOC_ENUM_EXT("DSP1 Firmware", wm_adsp_fw_enum[0], |
| 522 | wm_adsp_fw_get, wm_adsp_fw_put), |
| 523 | SOC_ENUM_EXT("DSP2 Firmware", wm_adsp_fw_enum[1], |
| 524 | wm_adsp_fw_get, wm_adsp_fw_put), |
| 525 | SOC_ENUM_EXT("DSP3 Firmware", wm_adsp_fw_enum[2], |
| 526 | wm_adsp_fw_get, wm_adsp_fw_put), |
Richard Fitzgerald | 336d044 | 2015-06-18 13:43:19 +0100 | [diff] [blame] | 527 | SOC_ENUM_EXT("DSP4 Firmware", wm_adsp_fw_enum[3], |
| 528 | wm_adsp_fw_get, wm_adsp_fw_put), |
Mark Brown | b6ed61cf | 2013-03-29 18:00:24 +0000 | [diff] [blame] | 529 | }; |
Richard Fitzgerald | 336d044 | 2015-06-18 13:43:19 +0100 | [diff] [blame] | 530 | EXPORT_SYMBOL_GPL(wm_adsp_fw_controls); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 531 | |
| 532 | static struct wm_adsp_region const *wm_adsp_find_region(struct wm_adsp *dsp, |
| 533 | int type) |
| 534 | { |
| 535 | int i; |
| 536 | |
| 537 | for (i = 0; i < dsp->num_mems; i++) |
| 538 | if (dsp->mem[i].type == type) |
| 539 | return &dsp->mem[i]; |
| 540 | |
| 541 | return NULL; |
| 542 | } |
| 543 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 544 | static unsigned int wm_adsp_region_to_reg(struct wm_adsp_region const *mem, |
Mark Brown | 45b9ee7 | 2013-01-08 16:02:06 +0000 | [diff] [blame] | 545 | unsigned int offset) |
| 546 | { |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 547 | if (WARN_ON(!mem)) |
Takashi Iwai | 6c452bd | 2013-11-05 18:40:00 +0100 | [diff] [blame] | 548 | return offset; |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 549 | switch (mem->type) { |
Mark Brown | 45b9ee7 | 2013-01-08 16:02:06 +0000 | [diff] [blame] | 550 | case WMFW_ADSP1_PM: |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 551 | return mem->base + (offset * 3); |
Mark Brown | 45b9ee7 | 2013-01-08 16:02:06 +0000 | [diff] [blame] | 552 | case WMFW_ADSP1_DM: |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 553 | return mem->base + (offset * 2); |
Mark Brown | 45b9ee7 | 2013-01-08 16:02:06 +0000 | [diff] [blame] | 554 | case WMFW_ADSP2_XM: |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 555 | return mem->base + (offset * 2); |
Mark Brown | 45b9ee7 | 2013-01-08 16:02:06 +0000 | [diff] [blame] | 556 | case WMFW_ADSP2_YM: |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 557 | return mem->base + (offset * 2); |
Mark Brown | 45b9ee7 | 2013-01-08 16:02:06 +0000 | [diff] [blame] | 558 | case WMFW_ADSP1_ZM: |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 559 | return mem->base + (offset * 2); |
Mark Brown | 45b9ee7 | 2013-01-08 16:02:06 +0000 | [diff] [blame] | 560 | default: |
Takashi Iwai | 6c452bd | 2013-11-05 18:40:00 +0100 | [diff] [blame] | 561 | WARN(1, "Unknown memory region type"); |
Mark Brown | 45b9ee7 | 2013-01-08 16:02:06 +0000 | [diff] [blame] | 562 | return offset; |
| 563 | } |
| 564 | } |
| 565 | |
Richard Fitzgerald | 10337b0 | 2015-05-29 10:23:07 +0100 | [diff] [blame] | 566 | static void wm_adsp2_show_fw_status(struct wm_adsp *dsp) |
| 567 | { |
| 568 | u16 scratch[4]; |
| 569 | int ret; |
| 570 | |
| 571 | ret = regmap_raw_read(dsp->regmap, dsp->base + ADSP2_SCRATCH0, |
| 572 | scratch, sizeof(scratch)); |
| 573 | if (ret) { |
| 574 | adsp_err(dsp, "Failed to read SCRATCH regs: %d\n", ret); |
| 575 | return; |
| 576 | } |
| 577 | |
| 578 | adsp_dbg(dsp, "FW SCRATCH 0:0x%x 1:0x%x 2:0x%x 3:0x%x\n", |
| 579 | be16_to_cpu(scratch[0]), |
| 580 | be16_to_cpu(scratch[1]), |
| 581 | be16_to_cpu(scratch[2]), |
| 582 | be16_to_cpu(scratch[3])); |
| 583 | } |
| 584 | |
Charles Keepax | 7585a5b | 2015-12-08 16:08:25 +0000 | [diff] [blame] | 585 | static int wm_coeff_info(struct snd_kcontrol *kctl, |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 586 | struct snd_ctl_elem_info *uinfo) |
| 587 | { |
Charles Keepax | 7585a5b | 2015-12-08 16:08:25 +0000 | [diff] [blame] | 588 | struct wm_coeff_ctl *ctl = (struct wm_coeff_ctl *)kctl->private_value; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 589 | |
| 590 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES; |
| 591 | uinfo->count = ctl->len; |
| 592 | return 0; |
| 593 | } |
| 594 | |
Charles Keepax | c9f8dd7 | 2015-04-13 13:27:58 +0100 | [diff] [blame] | 595 | static int wm_coeff_write_control(struct wm_coeff_ctl *ctl, |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 596 | const void *buf, size_t len) |
| 597 | { |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 598 | struct wm_adsp_alg_region *alg_region = &ctl->alg_region; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 599 | const struct wm_adsp_region *mem; |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 600 | struct wm_adsp *dsp = ctl->dsp; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 601 | void *scratch; |
| 602 | int ret; |
| 603 | unsigned int reg; |
| 604 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 605 | mem = wm_adsp_find_region(dsp, alg_region->type); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 606 | if (!mem) { |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 607 | adsp_err(dsp, "No base for region %x\n", |
| 608 | alg_region->type); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 609 | return -EINVAL; |
| 610 | } |
| 611 | |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 612 | reg = ctl->alg_region.base + ctl->offset; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 613 | reg = wm_adsp_region_to_reg(mem, reg); |
| 614 | |
| 615 | scratch = kmemdup(buf, ctl->len, GFP_KERNEL | GFP_DMA); |
| 616 | if (!scratch) |
| 617 | return -ENOMEM; |
| 618 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 619 | ret = regmap_raw_write(dsp->regmap, reg, scratch, |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 620 | ctl->len); |
| 621 | if (ret) { |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 622 | adsp_err(dsp, "Failed to write %zu bytes to %x: %d\n", |
Dimitris Papastamos | 43bc3bf | 2013-11-01 15:56:52 +0000 | [diff] [blame] | 623 | ctl->len, reg, ret); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 624 | kfree(scratch); |
| 625 | return ret; |
| 626 | } |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 627 | adsp_dbg(dsp, "Wrote %zu bytes to %x\n", ctl->len, reg); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 628 | |
| 629 | kfree(scratch); |
| 630 | |
| 631 | return 0; |
| 632 | } |
| 633 | |
Charles Keepax | 7585a5b | 2015-12-08 16:08:25 +0000 | [diff] [blame] | 634 | static int wm_coeff_put(struct snd_kcontrol *kctl, |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 635 | struct snd_ctl_elem_value *ucontrol) |
| 636 | { |
Charles Keepax | 7585a5b | 2015-12-08 16:08:25 +0000 | [diff] [blame] | 637 | struct wm_coeff_ctl *ctl = (struct wm_coeff_ctl *)kctl->private_value; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 638 | char *p = ucontrol->value.bytes.data; |
Charles Keepax | 168d10e | 2015-12-08 16:08:27 +0000 | [diff] [blame] | 639 | int ret = 0; |
| 640 | |
| 641 | mutex_lock(&ctl->dsp->pwr_lock); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 642 | |
| 643 | memcpy(ctl->cache, p, ctl->len); |
| 644 | |
Nikesh Oswal | 65d17a9 | 2015-02-16 15:25:48 +0000 | [diff] [blame] | 645 | ctl->set = 1; |
Charles Keepax | 168d10e | 2015-12-08 16:08:27 +0000 | [diff] [blame] | 646 | if (ctl->enabled) |
| 647 | ret = wm_coeff_write_control(ctl, p, ctl->len); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 648 | |
Charles Keepax | 168d10e | 2015-12-08 16:08:27 +0000 | [diff] [blame] | 649 | mutex_unlock(&ctl->dsp->pwr_lock); |
| 650 | |
| 651 | return ret; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 652 | } |
| 653 | |
Charles Keepax | c9f8dd7 | 2015-04-13 13:27:58 +0100 | [diff] [blame] | 654 | static int wm_coeff_read_control(struct wm_coeff_ctl *ctl, |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 655 | void *buf, size_t len) |
| 656 | { |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 657 | struct wm_adsp_alg_region *alg_region = &ctl->alg_region; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 658 | const struct wm_adsp_region *mem; |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 659 | struct wm_adsp *dsp = ctl->dsp; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 660 | void *scratch; |
| 661 | int ret; |
| 662 | unsigned int reg; |
| 663 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 664 | mem = wm_adsp_find_region(dsp, alg_region->type); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 665 | if (!mem) { |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 666 | adsp_err(dsp, "No base for region %x\n", |
| 667 | alg_region->type); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 668 | return -EINVAL; |
| 669 | } |
| 670 | |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 671 | reg = ctl->alg_region.base + ctl->offset; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 672 | reg = wm_adsp_region_to_reg(mem, reg); |
| 673 | |
| 674 | scratch = kmalloc(ctl->len, GFP_KERNEL | GFP_DMA); |
| 675 | if (!scratch) |
| 676 | return -ENOMEM; |
| 677 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 678 | ret = regmap_raw_read(dsp->regmap, reg, scratch, ctl->len); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 679 | if (ret) { |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 680 | adsp_err(dsp, "Failed to read %zu bytes from %x: %d\n", |
Dimitris Papastamos | 43bc3bf | 2013-11-01 15:56:52 +0000 | [diff] [blame] | 681 | ctl->len, reg, ret); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 682 | kfree(scratch); |
| 683 | return ret; |
| 684 | } |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 685 | adsp_dbg(dsp, "Read %zu bytes from %x\n", ctl->len, reg); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 686 | |
| 687 | memcpy(buf, scratch, ctl->len); |
| 688 | kfree(scratch); |
| 689 | |
| 690 | return 0; |
| 691 | } |
| 692 | |
Charles Keepax | 7585a5b | 2015-12-08 16:08:25 +0000 | [diff] [blame] | 693 | static int wm_coeff_get(struct snd_kcontrol *kctl, |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 694 | struct snd_ctl_elem_value *ucontrol) |
| 695 | { |
Charles Keepax | 7585a5b | 2015-12-08 16:08:25 +0000 | [diff] [blame] | 696 | struct wm_coeff_ctl *ctl = (struct wm_coeff_ctl *)kctl->private_value; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 697 | char *p = ucontrol->value.bytes.data; |
Charles Keepax | 168d10e | 2015-12-08 16:08:27 +0000 | [diff] [blame] | 698 | int ret = 0; |
| 699 | |
| 700 | mutex_lock(&ctl->dsp->pwr_lock); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 701 | |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 702 | if (ctl->flags & WMFW_CTL_FLAG_VOLATILE) { |
| 703 | if (ctl->enabled) |
Charles Keepax | 168d10e | 2015-12-08 16:08:27 +0000 | [diff] [blame] | 704 | ret = wm_coeff_read_control(ctl, p, ctl->len); |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 705 | else |
Charles Keepax | 168d10e | 2015-12-08 16:08:27 +0000 | [diff] [blame] | 706 | ret = -EPERM; |
| 707 | } else { |
Charles Keepax | bc1765d | 2015-12-17 10:05:59 +0000 | [diff] [blame] | 708 | if (!ctl->flags && ctl->enabled) |
| 709 | ret = wm_coeff_read_control(ctl, ctl->cache, ctl->len); |
| 710 | |
Charles Keepax | 168d10e | 2015-12-08 16:08:27 +0000 | [diff] [blame] | 711 | memcpy(p, ctl->cache, ctl->len); |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 712 | } |
| 713 | |
Charles Keepax | 168d10e | 2015-12-08 16:08:27 +0000 | [diff] [blame] | 714 | mutex_unlock(&ctl->dsp->pwr_lock); |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 715 | |
Charles Keepax | 168d10e | 2015-12-08 16:08:27 +0000 | [diff] [blame] | 716 | return ret; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 717 | } |
| 718 | |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 719 | struct wmfw_ctl_work { |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 720 | struct wm_adsp *dsp; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 721 | struct wm_coeff_ctl *ctl; |
| 722 | struct work_struct work; |
| 723 | }; |
| 724 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 725 | static int wmfw_add_ctl(struct wm_adsp *dsp, struct wm_coeff_ctl *ctl) |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 726 | { |
| 727 | struct snd_kcontrol_new *kcontrol; |
| 728 | int ret; |
| 729 | |
Dimitris Papastamos | 92bb4c3 | 2013-08-01 11:11:28 +0100 | [diff] [blame] | 730 | if (!ctl || !ctl->name) |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 731 | return -EINVAL; |
| 732 | |
| 733 | kcontrol = kzalloc(sizeof(*kcontrol), GFP_KERNEL); |
| 734 | if (!kcontrol) |
| 735 | return -ENOMEM; |
| 736 | kcontrol->iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
| 737 | |
| 738 | kcontrol->name = ctl->name; |
| 739 | kcontrol->info = wm_coeff_info; |
| 740 | kcontrol->get = wm_coeff_get; |
| 741 | kcontrol->put = wm_coeff_put; |
| 742 | kcontrol->private_value = (unsigned long)ctl; |
| 743 | |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 744 | if (ctl->flags) { |
| 745 | if (ctl->flags & WMFW_CTL_FLAG_WRITEABLE) |
| 746 | kcontrol->access |= SNDRV_CTL_ELEM_ACCESS_WRITE; |
| 747 | if (ctl->flags & WMFW_CTL_FLAG_READABLE) |
| 748 | kcontrol->access |= SNDRV_CTL_ELEM_ACCESS_READ; |
| 749 | if (ctl->flags & WMFW_CTL_FLAG_VOLATILE) |
| 750 | kcontrol->access |= SNDRV_CTL_ELEM_ACCESS_VOLATILE; |
| 751 | } |
| 752 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 753 | ret = snd_soc_add_card_controls(dsp->card, |
Dimitris Papastamos | 81ad93e | 2013-07-29 13:51:59 +0100 | [diff] [blame] | 754 | kcontrol, 1); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 755 | if (ret < 0) |
| 756 | goto err_kcontrol; |
| 757 | |
| 758 | kfree(kcontrol); |
| 759 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 760 | ctl->kcontrol = snd_soc_card_get_kcontrol(dsp->card, |
Dimitris Papastamos | 81ad93e | 2013-07-29 13:51:59 +0100 | [diff] [blame] | 761 | ctl->name); |
| 762 | |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 763 | return 0; |
| 764 | |
| 765 | err_kcontrol: |
| 766 | kfree(kcontrol); |
| 767 | return ret; |
| 768 | } |
| 769 | |
Charles Keepax | b21acc1 | 2015-04-13 13:28:01 +0100 | [diff] [blame] | 770 | static int wm_coeff_init_control_caches(struct wm_adsp *dsp) |
| 771 | { |
| 772 | struct wm_coeff_ctl *ctl; |
| 773 | int ret; |
| 774 | |
| 775 | list_for_each_entry(ctl, &dsp->ctl_list, list) { |
| 776 | if (!ctl->enabled || ctl->set) |
| 777 | continue; |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 778 | if (ctl->flags & WMFW_CTL_FLAG_VOLATILE) |
| 779 | continue; |
| 780 | |
Charles Keepax | b21acc1 | 2015-04-13 13:28:01 +0100 | [diff] [blame] | 781 | ret = wm_coeff_read_control(ctl, |
| 782 | ctl->cache, |
| 783 | ctl->len); |
| 784 | if (ret < 0) |
| 785 | return ret; |
| 786 | } |
| 787 | |
| 788 | return 0; |
| 789 | } |
| 790 | |
| 791 | static int wm_coeff_sync_controls(struct wm_adsp *dsp) |
| 792 | { |
| 793 | struct wm_coeff_ctl *ctl; |
| 794 | int ret; |
| 795 | |
| 796 | list_for_each_entry(ctl, &dsp->ctl_list, list) { |
| 797 | if (!ctl->enabled) |
| 798 | continue; |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 799 | if (ctl->set && !(ctl->flags & WMFW_CTL_FLAG_VOLATILE)) { |
Charles Keepax | b21acc1 | 2015-04-13 13:28:01 +0100 | [diff] [blame] | 800 | ret = wm_coeff_write_control(ctl, |
| 801 | ctl->cache, |
| 802 | ctl->len); |
| 803 | if (ret < 0) |
| 804 | return ret; |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | return 0; |
| 809 | } |
| 810 | |
| 811 | static void wm_adsp_ctl_work(struct work_struct *work) |
| 812 | { |
| 813 | struct wmfw_ctl_work *ctl_work = container_of(work, |
| 814 | struct wmfw_ctl_work, |
| 815 | work); |
| 816 | |
| 817 | wmfw_add_ctl(ctl_work->dsp, ctl_work->ctl); |
| 818 | kfree(ctl_work); |
| 819 | } |
| 820 | |
| 821 | static int wm_adsp_create_control(struct wm_adsp *dsp, |
| 822 | const struct wm_adsp_alg_region *alg_region, |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 823 | unsigned int offset, unsigned int len, |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 824 | const char *subname, unsigned int subname_len, |
| 825 | unsigned int flags) |
Charles Keepax | b21acc1 | 2015-04-13 13:28:01 +0100 | [diff] [blame] | 826 | { |
| 827 | struct wm_coeff_ctl *ctl; |
| 828 | struct wmfw_ctl_work *ctl_work; |
| 829 | char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; |
| 830 | char *region_name; |
| 831 | int ret; |
| 832 | |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 833 | if (flags & WMFW_CTL_FLAG_SYS) |
| 834 | return 0; |
| 835 | |
Charles Keepax | b21acc1 | 2015-04-13 13:28:01 +0100 | [diff] [blame] | 836 | switch (alg_region->type) { |
| 837 | case WMFW_ADSP1_PM: |
| 838 | region_name = "PM"; |
| 839 | break; |
| 840 | case WMFW_ADSP1_DM: |
| 841 | region_name = "DM"; |
| 842 | break; |
| 843 | case WMFW_ADSP2_XM: |
| 844 | region_name = "XM"; |
| 845 | break; |
| 846 | case WMFW_ADSP2_YM: |
| 847 | region_name = "YM"; |
| 848 | break; |
| 849 | case WMFW_ADSP1_ZM: |
| 850 | region_name = "ZM"; |
| 851 | break; |
| 852 | default: |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 853 | adsp_err(dsp, "Unknown region type: %d\n", alg_region->type); |
Charles Keepax | b21acc1 | 2015-04-13 13:28:01 +0100 | [diff] [blame] | 854 | return -EINVAL; |
| 855 | } |
| 856 | |
Charles Keepax | cb5b57a | 2015-04-13 13:28:04 +0100 | [diff] [blame] | 857 | switch (dsp->fw_ver) { |
| 858 | case 0: |
| 859 | case 1: |
| 860 | snprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "DSP%d %s %x", |
| 861 | dsp->num, region_name, alg_region->alg); |
| 862 | break; |
| 863 | default: |
| 864 | ret = snprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, |
| 865 | "DSP%d%c %.12s %x", dsp->num, *region_name, |
| 866 | wm_adsp_fw_text[dsp->fw], alg_region->alg); |
| 867 | |
| 868 | /* Truncate the subname from the start if it is too long */ |
| 869 | if (subname) { |
| 870 | int avail = SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret - 2; |
| 871 | int skip = 0; |
| 872 | |
| 873 | if (subname_len > avail) |
| 874 | skip = subname_len - avail; |
| 875 | |
| 876 | snprintf(name + ret, |
| 877 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret, " %.*s", |
| 878 | subname_len - skip, subname + skip); |
| 879 | } |
| 880 | break; |
| 881 | } |
Charles Keepax | b21acc1 | 2015-04-13 13:28:01 +0100 | [diff] [blame] | 882 | |
Charles Keepax | 7585a5b | 2015-12-08 16:08:25 +0000 | [diff] [blame] | 883 | list_for_each_entry(ctl, &dsp->ctl_list, list) { |
Charles Keepax | b21acc1 | 2015-04-13 13:28:01 +0100 | [diff] [blame] | 884 | if (!strcmp(ctl->name, name)) { |
| 885 | if (!ctl->enabled) |
| 886 | ctl->enabled = 1; |
| 887 | return 0; |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | ctl = kzalloc(sizeof(*ctl), GFP_KERNEL); |
| 892 | if (!ctl) |
| 893 | return -ENOMEM; |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 894 | ctl->fw_name = wm_adsp_fw_text[dsp->fw]; |
Charles Keepax | b21acc1 | 2015-04-13 13:28:01 +0100 | [diff] [blame] | 895 | ctl->alg_region = *alg_region; |
| 896 | ctl->name = kmemdup(name, strlen(name) + 1, GFP_KERNEL); |
| 897 | if (!ctl->name) { |
| 898 | ret = -ENOMEM; |
| 899 | goto err_ctl; |
| 900 | } |
| 901 | ctl->enabled = 1; |
| 902 | ctl->set = 0; |
| 903 | ctl->ops.xget = wm_coeff_get; |
| 904 | ctl->ops.xput = wm_coeff_put; |
| 905 | ctl->dsp = dsp; |
| 906 | |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 907 | ctl->flags = flags; |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 908 | ctl->offset = offset; |
Charles Keepax | b21acc1 | 2015-04-13 13:28:01 +0100 | [diff] [blame] | 909 | if (len > 512) { |
| 910 | adsp_warn(dsp, "Truncating control %s from %d\n", |
| 911 | ctl->name, len); |
| 912 | len = 512; |
| 913 | } |
| 914 | ctl->len = len; |
| 915 | ctl->cache = kzalloc(ctl->len, GFP_KERNEL); |
| 916 | if (!ctl->cache) { |
| 917 | ret = -ENOMEM; |
| 918 | goto err_ctl_name; |
| 919 | } |
| 920 | |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 921 | list_add(&ctl->list, &dsp->ctl_list); |
| 922 | |
Charles Keepax | b21acc1 | 2015-04-13 13:28:01 +0100 | [diff] [blame] | 923 | ctl_work = kzalloc(sizeof(*ctl_work), GFP_KERNEL); |
| 924 | if (!ctl_work) { |
| 925 | ret = -ENOMEM; |
| 926 | goto err_ctl_cache; |
| 927 | } |
| 928 | |
| 929 | ctl_work->dsp = dsp; |
| 930 | ctl_work->ctl = ctl; |
| 931 | INIT_WORK(&ctl_work->work, wm_adsp_ctl_work); |
| 932 | schedule_work(&ctl_work->work); |
| 933 | |
| 934 | return 0; |
| 935 | |
| 936 | err_ctl_cache: |
| 937 | kfree(ctl->cache); |
| 938 | err_ctl_name: |
| 939 | kfree(ctl->name); |
| 940 | err_ctl: |
| 941 | kfree(ctl); |
| 942 | |
| 943 | return ret; |
| 944 | } |
| 945 | |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 946 | struct wm_coeff_parsed_alg { |
| 947 | int id; |
| 948 | const u8 *name; |
| 949 | int name_len; |
| 950 | int ncoeff; |
| 951 | }; |
| 952 | |
| 953 | struct wm_coeff_parsed_coeff { |
| 954 | int offset; |
| 955 | int mem_type; |
| 956 | const u8 *name; |
| 957 | int name_len; |
| 958 | int ctl_type; |
| 959 | int flags; |
| 960 | int len; |
| 961 | }; |
| 962 | |
Charles Keepax | cb5b57a | 2015-04-13 13:28:04 +0100 | [diff] [blame] | 963 | static int wm_coeff_parse_string(int bytes, const u8 **pos, const u8 **str) |
| 964 | { |
| 965 | int length; |
| 966 | |
| 967 | switch (bytes) { |
| 968 | case 1: |
| 969 | length = **pos; |
| 970 | break; |
| 971 | case 2: |
Charles Keepax | 8299ee8 | 2015-04-20 13:52:44 +0100 | [diff] [blame] | 972 | length = le16_to_cpu(*((__le16 *)*pos)); |
Charles Keepax | cb5b57a | 2015-04-13 13:28:04 +0100 | [diff] [blame] | 973 | break; |
| 974 | default: |
| 975 | return 0; |
| 976 | } |
| 977 | |
| 978 | if (str) |
| 979 | *str = *pos + bytes; |
| 980 | |
| 981 | *pos += ((length + bytes) + 3) & ~0x03; |
| 982 | |
| 983 | return length; |
| 984 | } |
| 985 | |
| 986 | static int wm_coeff_parse_int(int bytes, const u8 **pos) |
| 987 | { |
| 988 | int val = 0; |
| 989 | |
| 990 | switch (bytes) { |
| 991 | case 2: |
Charles Keepax | 8299ee8 | 2015-04-20 13:52:44 +0100 | [diff] [blame] | 992 | val = le16_to_cpu(*((__le16 *)*pos)); |
Charles Keepax | cb5b57a | 2015-04-13 13:28:04 +0100 | [diff] [blame] | 993 | break; |
| 994 | case 4: |
Charles Keepax | 8299ee8 | 2015-04-20 13:52:44 +0100 | [diff] [blame] | 995 | val = le32_to_cpu(*((__le32 *)*pos)); |
Charles Keepax | cb5b57a | 2015-04-13 13:28:04 +0100 | [diff] [blame] | 996 | break; |
| 997 | default: |
| 998 | break; |
| 999 | } |
| 1000 | |
| 1001 | *pos += bytes; |
| 1002 | |
| 1003 | return val; |
| 1004 | } |
| 1005 | |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1006 | static inline void wm_coeff_parse_alg(struct wm_adsp *dsp, const u8 **data, |
| 1007 | struct wm_coeff_parsed_alg *blk) |
| 1008 | { |
| 1009 | const struct wmfw_adsp_alg_data *raw; |
| 1010 | |
Charles Keepax | cb5b57a | 2015-04-13 13:28:04 +0100 | [diff] [blame] | 1011 | switch (dsp->fw_ver) { |
| 1012 | case 0: |
| 1013 | case 1: |
| 1014 | raw = (const struct wmfw_adsp_alg_data *)*data; |
| 1015 | *data = raw->data; |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1016 | |
Charles Keepax | cb5b57a | 2015-04-13 13:28:04 +0100 | [diff] [blame] | 1017 | blk->id = le32_to_cpu(raw->id); |
| 1018 | blk->name = raw->name; |
| 1019 | blk->name_len = strlen(raw->name); |
| 1020 | blk->ncoeff = le32_to_cpu(raw->ncoeff); |
| 1021 | break; |
| 1022 | default: |
| 1023 | blk->id = wm_coeff_parse_int(sizeof(raw->id), data); |
| 1024 | blk->name_len = wm_coeff_parse_string(sizeof(u8), data, |
| 1025 | &blk->name); |
| 1026 | wm_coeff_parse_string(sizeof(u16), data, NULL); |
| 1027 | blk->ncoeff = wm_coeff_parse_int(sizeof(raw->ncoeff), data); |
| 1028 | break; |
| 1029 | } |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1030 | |
| 1031 | adsp_dbg(dsp, "Algorithm ID: %#x\n", blk->id); |
| 1032 | adsp_dbg(dsp, "Algorithm name: %.*s\n", blk->name_len, blk->name); |
| 1033 | adsp_dbg(dsp, "# of coefficient descriptors: %#x\n", blk->ncoeff); |
| 1034 | } |
| 1035 | |
| 1036 | static inline void wm_coeff_parse_coeff(struct wm_adsp *dsp, const u8 **data, |
| 1037 | struct wm_coeff_parsed_coeff *blk) |
| 1038 | { |
| 1039 | const struct wmfw_adsp_coeff_data *raw; |
Charles Keepax | cb5b57a | 2015-04-13 13:28:04 +0100 | [diff] [blame] | 1040 | const u8 *tmp; |
| 1041 | int length; |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1042 | |
Charles Keepax | cb5b57a | 2015-04-13 13:28:04 +0100 | [diff] [blame] | 1043 | switch (dsp->fw_ver) { |
| 1044 | case 0: |
| 1045 | case 1: |
| 1046 | raw = (const struct wmfw_adsp_coeff_data *)*data; |
| 1047 | *data = *data + sizeof(raw->hdr) + le32_to_cpu(raw->hdr.size); |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1048 | |
Charles Keepax | cb5b57a | 2015-04-13 13:28:04 +0100 | [diff] [blame] | 1049 | blk->offset = le16_to_cpu(raw->hdr.offset); |
| 1050 | blk->mem_type = le16_to_cpu(raw->hdr.type); |
| 1051 | blk->name = raw->name; |
| 1052 | blk->name_len = strlen(raw->name); |
| 1053 | blk->ctl_type = le16_to_cpu(raw->ctl_type); |
| 1054 | blk->flags = le16_to_cpu(raw->flags); |
| 1055 | blk->len = le32_to_cpu(raw->len); |
| 1056 | break; |
| 1057 | default: |
| 1058 | tmp = *data; |
| 1059 | blk->offset = wm_coeff_parse_int(sizeof(raw->hdr.offset), &tmp); |
| 1060 | blk->mem_type = wm_coeff_parse_int(sizeof(raw->hdr.type), &tmp); |
| 1061 | length = wm_coeff_parse_int(sizeof(raw->hdr.size), &tmp); |
| 1062 | blk->name_len = wm_coeff_parse_string(sizeof(u8), &tmp, |
| 1063 | &blk->name); |
| 1064 | wm_coeff_parse_string(sizeof(u8), &tmp, NULL); |
| 1065 | wm_coeff_parse_string(sizeof(u16), &tmp, NULL); |
| 1066 | blk->ctl_type = wm_coeff_parse_int(sizeof(raw->ctl_type), &tmp); |
| 1067 | blk->flags = wm_coeff_parse_int(sizeof(raw->flags), &tmp); |
| 1068 | blk->len = wm_coeff_parse_int(sizeof(raw->len), &tmp); |
| 1069 | |
| 1070 | *data = *data + sizeof(raw->hdr) + length; |
| 1071 | break; |
| 1072 | } |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1073 | |
| 1074 | adsp_dbg(dsp, "\tCoefficient type: %#x\n", blk->mem_type); |
| 1075 | adsp_dbg(dsp, "\tCoefficient offset: %#x\n", blk->offset); |
| 1076 | adsp_dbg(dsp, "\tCoefficient name: %.*s\n", blk->name_len, blk->name); |
| 1077 | adsp_dbg(dsp, "\tCoefficient flags: %#x\n", blk->flags); |
| 1078 | adsp_dbg(dsp, "\tALSA control type: %#x\n", blk->ctl_type); |
| 1079 | adsp_dbg(dsp, "\tALSA control len: %#x\n", blk->len); |
| 1080 | } |
| 1081 | |
| 1082 | static int wm_adsp_parse_coeff(struct wm_adsp *dsp, |
| 1083 | const struct wmfw_region *region) |
| 1084 | { |
| 1085 | struct wm_adsp_alg_region alg_region = {}; |
| 1086 | struct wm_coeff_parsed_alg alg_blk; |
| 1087 | struct wm_coeff_parsed_coeff coeff_blk; |
| 1088 | const u8 *data = region->data; |
| 1089 | int i, ret; |
| 1090 | |
| 1091 | wm_coeff_parse_alg(dsp, &data, &alg_blk); |
| 1092 | for (i = 0; i < alg_blk.ncoeff; i++) { |
| 1093 | wm_coeff_parse_coeff(dsp, &data, &coeff_blk); |
| 1094 | |
| 1095 | switch (coeff_blk.ctl_type) { |
| 1096 | case SNDRV_CTL_ELEM_TYPE_BYTES: |
| 1097 | break; |
| 1098 | default: |
| 1099 | adsp_err(dsp, "Unknown control type: %d\n", |
| 1100 | coeff_blk.ctl_type); |
| 1101 | return -EINVAL; |
| 1102 | } |
| 1103 | |
| 1104 | alg_region.type = coeff_blk.mem_type; |
| 1105 | alg_region.alg = alg_blk.id; |
| 1106 | |
| 1107 | ret = wm_adsp_create_control(dsp, &alg_region, |
| 1108 | coeff_blk.offset, |
| 1109 | coeff_blk.len, |
| 1110 | coeff_blk.name, |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 1111 | coeff_blk.name_len, |
| 1112 | coeff_blk.flags); |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1113 | if (ret < 0) |
| 1114 | adsp_err(dsp, "Failed to create control: %.*s, %d\n", |
| 1115 | coeff_blk.name_len, coeff_blk.name, ret); |
| 1116 | } |
| 1117 | |
| 1118 | return 0; |
| 1119 | } |
| 1120 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1121 | static int wm_adsp_load(struct wm_adsp *dsp) |
| 1122 | { |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 1123 | LIST_HEAD(buf_list); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1124 | const struct firmware *firmware; |
| 1125 | struct regmap *regmap = dsp->regmap; |
| 1126 | unsigned int pos = 0; |
| 1127 | const struct wmfw_header *header; |
| 1128 | const struct wmfw_adsp1_sizes *adsp1_sizes; |
| 1129 | const struct wmfw_adsp2_sizes *adsp2_sizes; |
| 1130 | const struct wmfw_footer *footer; |
| 1131 | const struct wmfw_region *region; |
| 1132 | const struct wm_adsp_region *mem; |
| 1133 | const char *region_name; |
| 1134 | char *file, *text; |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 1135 | struct wm_adsp_buf *buf; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1136 | unsigned int reg; |
| 1137 | int regions = 0; |
| 1138 | int ret, offset, type, sizes; |
| 1139 | |
| 1140 | file = kzalloc(PAGE_SIZE, GFP_KERNEL); |
| 1141 | if (file == NULL) |
| 1142 | return -ENOMEM; |
| 1143 | |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 1144 | snprintf(file, PAGE_SIZE, "%s-dsp%d-%s.wmfw", dsp->part, dsp->num, |
| 1145 | wm_adsp_fw[dsp->fw].file); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1146 | file[PAGE_SIZE - 1] = '\0'; |
| 1147 | |
| 1148 | ret = request_firmware(&firmware, file, dsp->dev); |
| 1149 | if (ret != 0) { |
| 1150 | adsp_err(dsp, "Failed to request '%s'\n", file); |
| 1151 | goto out; |
| 1152 | } |
| 1153 | ret = -EINVAL; |
| 1154 | |
| 1155 | pos = sizeof(*header) + sizeof(*adsp1_sizes) + sizeof(*footer); |
| 1156 | if (pos >= firmware->size) { |
| 1157 | adsp_err(dsp, "%s: file too short, %zu bytes\n", |
| 1158 | file, firmware->size); |
| 1159 | goto out_fw; |
| 1160 | } |
| 1161 | |
Charles Keepax | 7585a5b | 2015-12-08 16:08:25 +0000 | [diff] [blame] | 1162 | header = (void *)&firmware->data[0]; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1163 | |
| 1164 | if (memcmp(&header->magic[0], "WMFW", 4) != 0) { |
| 1165 | adsp_err(dsp, "%s: invalid magic\n", file); |
| 1166 | goto out_fw; |
| 1167 | } |
| 1168 | |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1169 | switch (header->ver) { |
| 1170 | case 0: |
Charles Keepax | c61e59f | 2015-04-13 13:28:05 +0100 | [diff] [blame] | 1171 | adsp_warn(dsp, "%s: Depreciated file format %d\n", |
| 1172 | file, header->ver); |
| 1173 | break; |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1174 | case 1: |
Charles Keepax | cb5b57a | 2015-04-13 13:28:04 +0100 | [diff] [blame] | 1175 | case 2: |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1176 | break; |
| 1177 | default: |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1178 | adsp_err(dsp, "%s: unknown file format %d\n", |
| 1179 | file, header->ver); |
| 1180 | goto out_fw; |
| 1181 | } |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1182 | |
Dimitris Papastamos | 3626992 | 2013-11-01 15:56:57 +0000 | [diff] [blame] | 1183 | adsp_info(dsp, "Firmware version: %d\n", header->ver); |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1184 | dsp->fw_ver = header->ver; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1185 | |
| 1186 | if (header->core != dsp->type) { |
| 1187 | adsp_err(dsp, "%s: invalid core %d != %d\n", |
| 1188 | file, header->core, dsp->type); |
| 1189 | goto out_fw; |
| 1190 | } |
| 1191 | |
| 1192 | switch (dsp->type) { |
| 1193 | case WMFW_ADSP1: |
| 1194 | pos = sizeof(*header) + sizeof(*adsp1_sizes) + sizeof(*footer); |
| 1195 | adsp1_sizes = (void *)&(header[1]); |
| 1196 | footer = (void *)&(adsp1_sizes[1]); |
| 1197 | sizes = sizeof(*adsp1_sizes); |
| 1198 | |
| 1199 | adsp_dbg(dsp, "%s: %d DM, %d PM, %d ZM\n", |
| 1200 | file, le32_to_cpu(adsp1_sizes->dm), |
| 1201 | le32_to_cpu(adsp1_sizes->pm), |
| 1202 | le32_to_cpu(adsp1_sizes->zm)); |
| 1203 | break; |
| 1204 | |
| 1205 | case WMFW_ADSP2: |
| 1206 | pos = sizeof(*header) + sizeof(*adsp2_sizes) + sizeof(*footer); |
| 1207 | adsp2_sizes = (void *)&(header[1]); |
| 1208 | footer = (void *)&(adsp2_sizes[1]); |
| 1209 | sizes = sizeof(*adsp2_sizes); |
| 1210 | |
| 1211 | adsp_dbg(dsp, "%s: %d XM, %d YM %d PM, %d ZM\n", |
| 1212 | file, le32_to_cpu(adsp2_sizes->xm), |
| 1213 | le32_to_cpu(adsp2_sizes->ym), |
| 1214 | le32_to_cpu(adsp2_sizes->pm), |
| 1215 | le32_to_cpu(adsp2_sizes->zm)); |
| 1216 | break; |
| 1217 | |
| 1218 | default: |
Takashi Iwai | 6c452bd | 2013-11-05 18:40:00 +0100 | [diff] [blame] | 1219 | WARN(1, "Unknown DSP type"); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1220 | goto out_fw; |
| 1221 | } |
| 1222 | |
| 1223 | if (le32_to_cpu(header->len) != sizeof(*header) + |
| 1224 | sizes + sizeof(*footer)) { |
| 1225 | adsp_err(dsp, "%s: unexpected header length %d\n", |
| 1226 | file, le32_to_cpu(header->len)); |
| 1227 | goto out_fw; |
| 1228 | } |
| 1229 | |
| 1230 | adsp_dbg(dsp, "%s: timestamp %llu\n", file, |
| 1231 | le64_to_cpu(footer->timestamp)); |
| 1232 | |
| 1233 | while (pos < firmware->size && |
| 1234 | pos - firmware->size > sizeof(*region)) { |
| 1235 | region = (void *)&(firmware->data[pos]); |
| 1236 | region_name = "Unknown"; |
| 1237 | reg = 0; |
| 1238 | text = NULL; |
| 1239 | offset = le32_to_cpu(region->offset) & 0xffffff; |
| 1240 | type = be32_to_cpu(region->type) & 0xff; |
| 1241 | mem = wm_adsp_find_region(dsp, type); |
Charles Keepax | 7585a5b | 2015-12-08 16:08:25 +0000 | [diff] [blame] | 1242 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1243 | switch (type) { |
| 1244 | case WMFW_NAME_TEXT: |
| 1245 | region_name = "Firmware name"; |
| 1246 | text = kzalloc(le32_to_cpu(region->len) + 1, |
| 1247 | GFP_KERNEL); |
| 1248 | break; |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1249 | case WMFW_ALGORITHM_DATA: |
| 1250 | region_name = "Algorithm"; |
| 1251 | ret = wm_adsp_parse_coeff(dsp, region); |
| 1252 | if (ret != 0) |
| 1253 | goto out_fw; |
| 1254 | break; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1255 | case WMFW_INFO_TEXT: |
| 1256 | region_name = "Information"; |
| 1257 | text = kzalloc(le32_to_cpu(region->len) + 1, |
| 1258 | GFP_KERNEL); |
| 1259 | break; |
| 1260 | case WMFW_ABSOLUTE: |
| 1261 | region_name = "Absolute"; |
| 1262 | reg = offset; |
| 1263 | break; |
| 1264 | case WMFW_ADSP1_PM: |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1265 | region_name = "PM"; |
Mark Brown | 45b9ee7 | 2013-01-08 16:02:06 +0000 | [diff] [blame] | 1266 | reg = wm_adsp_region_to_reg(mem, offset); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1267 | break; |
| 1268 | case WMFW_ADSP1_DM: |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1269 | region_name = "DM"; |
Mark Brown | 45b9ee7 | 2013-01-08 16:02:06 +0000 | [diff] [blame] | 1270 | reg = wm_adsp_region_to_reg(mem, offset); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1271 | break; |
| 1272 | case WMFW_ADSP2_XM: |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1273 | region_name = "XM"; |
Mark Brown | 45b9ee7 | 2013-01-08 16:02:06 +0000 | [diff] [blame] | 1274 | reg = wm_adsp_region_to_reg(mem, offset); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1275 | break; |
| 1276 | case WMFW_ADSP2_YM: |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1277 | region_name = "YM"; |
Mark Brown | 45b9ee7 | 2013-01-08 16:02:06 +0000 | [diff] [blame] | 1278 | reg = wm_adsp_region_to_reg(mem, offset); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1279 | break; |
| 1280 | case WMFW_ADSP1_ZM: |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1281 | region_name = "ZM"; |
Mark Brown | 45b9ee7 | 2013-01-08 16:02:06 +0000 | [diff] [blame] | 1282 | reg = wm_adsp_region_to_reg(mem, offset); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1283 | break; |
| 1284 | default: |
| 1285 | adsp_warn(dsp, |
| 1286 | "%s.%d: Unknown region type %x at %d(%x)\n", |
| 1287 | file, regions, type, pos, pos); |
| 1288 | break; |
| 1289 | } |
| 1290 | |
| 1291 | adsp_dbg(dsp, "%s.%d: %d bytes at %d in %s\n", file, |
| 1292 | regions, le32_to_cpu(region->len), offset, |
| 1293 | region_name); |
| 1294 | |
| 1295 | if (text) { |
| 1296 | memcpy(text, region->data, le32_to_cpu(region->len)); |
| 1297 | adsp_info(dsp, "%s: %s\n", file, text); |
| 1298 | kfree(text); |
| 1299 | } |
| 1300 | |
| 1301 | if (reg) { |
Charles Keepax | cdcd7f7 | 2014-11-14 15:40:45 +0000 | [diff] [blame] | 1302 | buf = wm_adsp_buf_alloc(region->data, |
| 1303 | le32_to_cpu(region->len), |
| 1304 | &buf_list); |
| 1305 | if (!buf) { |
| 1306 | adsp_err(dsp, "Out of memory\n"); |
| 1307 | ret = -ENOMEM; |
| 1308 | goto out_fw; |
| 1309 | } |
Mark Brown | a76fefa | 2013-01-07 19:03:17 +0000 | [diff] [blame] | 1310 | |
Charles Keepax | cdcd7f7 | 2014-11-14 15:40:45 +0000 | [diff] [blame] | 1311 | ret = regmap_raw_write_async(regmap, reg, buf->buf, |
| 1312 | le32_to_cpu(region->len)); |
| 1313 | if (ret != 0) { |
| 1314 | adsp_err(dsp, |
| 1315 | "%s.%d: Failed to write %d bytes at %d in %s: %d\n", |
| 1316 | file, regions, |
| 1317 | le32_to_cpu(region->len), offset, |
| 1318 | region_name, ret); |
| 1319 | goto out_fw; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | pos += le32_to_cpu(region->len) + sizeof(*region); |
| 1324 | regions++; |
| 1325 | } |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 1326 | |
| 1327 | ret = regmap_async_complete(regmap); |
| 1328 | if (ret != 0) { |
| 1329 | adsp_err(dsp, "Failed to complete async write: %d\n", ret); |
| 1330 | goto out_fw; |
| 1331 | } |
| 1332 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1333 | if (pos > firmware->size) |
| 1334 | adsp_warn(dsp, "%s.%d: %zu bytes at end of file\n", |
| 1335 | file, regions, pos - firmware->size); |
| 1336 | |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 1337 | wm_adsp_debugfs_save_wmfwname(dsp, file); |
| 1338 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1339 | out_fw: |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 1340 | regmap_async_complete(regmap); |
| 1341 | wm_adsp_buf_free(&buf_list); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1342 | release_firmware(firmware); |
| 1343 | out: |
| 1344 | kfree(file); |
| 1345 | |
| 1346 | return ret; |
| 1347 | } |
| 1348 | |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1349 | static void wm_adsp_ctl_fixup_base(struct wm_adsp *dsp, |
| 1350 | const struct wm_adsp_alg_region *alg_region) |
| 1351 | { |
| 1352 | struct wm_coeff_ctl *ctl; |
| 1353 | |
| 1354 | list_for_each_entry(ctl, &dsp->ctl_list, list) { |
| 1355 | if (ctl->fw_name == wm_adsp_fw_text[dsp->fw] && |
| 1356 | alg_region->alg == ctl->alg_region.alg && |
| 1357 | alg_region->type == ctl->alg_region.type) { |
| 1358 | ctl->alg_region.base = alg_region->base; |
| 1359 | } |
| 1360 | } |
| 1361 | } |
| 1362 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1363 | static void *wm_adsp_read_algs(struct wm_adsp *dsp, size_t n_algs, |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1364 | unsigned int pos, unsigned int len) |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1365 | { |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1366 | void *alg; |
| 1367 | int ret; |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1368 | __be32 val; |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1369 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1370 | if (n_algs == 0) { |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1371 | adsp_err(dsp, "No algorithms\n"); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1372 | return ERR_PTR(-EINVAL); |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1373 | } |
| 1374 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1375 | if (n_algs > 1024) { |
| 1376 | adsp_err(dsp, "Algorithm count %zx excessive\n", n_algs); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1377 | return ERR_PTR(-EINVAL); |
Mark Brown | d62f4bc | 2012-12-19 14:00:30 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1380 | /* Read the terminator first to validate the length */ |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1381 | ret = regmap_raw_read(dsp->regmap, pos + len, &val, sizeof(val)); |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1382 | if (ret != 0) { |
| 1383 | adsp_err(dsp, "Failed to read algorithm list end: %d\n", |
| 1384 | ret); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1385 | return ERR_PTR(ret); |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | if (be32_to_cpu(val) != 0xbedead) |
| 1389 | adsp_warn(dsp, "Algorithm list end %x 0x%x != 0xbeadead\n", |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1390 | pos + len, be32_to_cpu(val)); |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1391 | |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1392 | alg = kzalloc(len * 2, GFP_KERNEL | GFP_DMA); |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1393 | if (!alg) |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1394 | return ERR_PTR(-ENOMEM); |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1395 | |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1396 | ret = regmap_raw_read(dsp->regmap, pos, alg, len * 2); |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1397 | if (ret != 0) { |
| 1398 | adsp_err(dsp, "Failed to read algorithm list: %d\n", |
| 1399 | ret); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1400 | kfree(alg); |
| 1401 | return ERR_PTR(ret); |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1402 | } |
| 1403 | |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1404 | return alg; |
| 1405 | } |
| 1406 | |
Charles Keepax | 1419709 | 2015-12-15 11:29:43 +0000 | [diff] [blame] | 1407 | static struct wm_adsp_alg_region * |
| 1408 | wm_adsp_find_alg_region(struct wm_adsp *dsp, int type, unsigned int id) |
| 1409 | { |
| 1410 | struct wm_adsp_alg_region *alg_region; |
| 1411 | |
| 1412 | list_for_each_entry(alg_region, &dsp->alg_regions, list) { |
| 1413 | if (id == alg_region->alg && type == alg_region->type) |
| 1414 | return alg_region; |
| 1415 | } |
| 1416 | |
| 1417 | return NULL; |
| 1418 | } |
| 1419 | |
Charles Keepax | d9d20e1 | 2015-04-13 13:27:59 +0100 | [diff] [blame] | 1420 | static struct wm_adsp_alg_region *wm_adsp_create_region(struct wm_adsp *dsp, |
| 1421 | int type, __be32 id, |
| 1422 | __be32 base) |
| 1423 | { |
| 1424 | struct wm_adsp_alg_region *alg_region; |
| 1425 | |
| 1426 | alg_region = kzalloc(sizeof(*alg_region), GFP_KERNEL); |
| 1427 | if (!alg_region) |
| 1428 | return ERR_PTR(-ENOMEM); |
| 1429 | |
| 1430 | alg_region->type = type; |
| 1431 | alg_region->alg = be32_to_cpu(id); |
| 1432 | alg_region->base = be32_to_cpu(base); |
| 1433 | |
| 1434 | list_add_tail(&alg_region->list, &dsp->alg_regions); |
| 1435 | |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1436 | if (dsp->fw_ver > 0) |
| 1437 | wm_adsp_ctl_fixup_base(dsp, alg_region); |
| 1438 | |
Charles Keepax | d9d20e1 | 2015-04-13 13:27:59 +0100 | [diff] [blame] | 1439 | return alg_region; |
| 1440 | } |
| 1441 | |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1442 | static int wm_adsp1_setup_algs(struct wm_adsp *dsp) |
| 1443 | { |
| 1444 | struct wmfw_adsp1_id_hdr adsp1_id; |
| 1445 | struct wmfw_adsp1_alg_hdr *adsp1_alg; |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1446 | struct wm_adsp_alg_region *alg_region; |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1447 | const struct wm_adsp_region *mem; |
| 1448 | unsigned int pos, len; |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1449 | size_t n_algs; |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1450 | int i, ret; |
| 1451 | |
| 1452 | mem = wm_adsp_find_region(dsp, WMFW_ADSP1_DM); |
| 1453 | if (WARN_ON(!mem)) |
| 1454 | return -EINVAL; |
| 1455 | |
| 1456 | ret = regmap_raw_read(dsp->regmap, mem->base, &adsp1_id, |
| 1457 | sizeof(adsp1_id)); |
| 1458 | if (ret != 0) { |
| 1459 | adsp_err(dsp, "Failed to read algorithm info: %d\n", |
| 1460 | ret); |
| 1461 | return ret; |
| 1462 | } |
| 1463 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1464 | n_algs = be32_to_cpu(adsp1_id.n_algs); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1465 | dsp->fw_id = be32_to_cpu(adsp1_id.fw.id); |
| 1466 | adsp_info(dsp, "Firmware: %x v%d.%d.%d, %zu algorithms\n", |
| 1467 | dsp->fw_id, |
| 1468 | (be32_to_cpu(adsp1_id.fw.ver) & 0xff0000) >> 16, |
| 1469 | (be32_to_cpu(adsp1_id.fw.ver) & 0xff00) >> 8, |
| 1470 | be32_to_cpu(adsp1_id.fw.ver) & 0xff, |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1471 | n_algs); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1472 | |
Charles Keepax | d9d20e1 | 2015-04-13 13:27:59 +0100 | [diff] [blame] | 1473 | alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_ZM, |
| 1474 | adsp1_id.fw.id, adsp1_id.zm); |
| 1475 | if (IS_ERR(alg_region)) |
| 1476 | return PTR_ERR(alg_region); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1477 | |
Charles Keepax | d9d20e1 | 2015-04-13 13:27:59 +0100 | [diff] [blame] | 1478 | alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_DM, |
| 1479 | adsp1_id.fw.id, adsp1_id.dm); |
| 1480 | if (IS_ERR(alg_region)) |
| 1481 | return PTR_ERR(alg_region); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1482 | |
| 1483 | pos = sizeof(adsp1_id) / 2; |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1484 | len = (sizeof(*adsp1_alg) * n_algs) / 2; |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1485 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1486 | adsp1_alg = wm_adsp_read_algs(dsp, n_algs, mem->base + pos, len); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1487 | if (IS_ERR(adsp1_alg)) |
| 1488 | return PTR_ERR(adsp1_alg); |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1489 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1490 | for (i = 0; i < n_algs; i++) { |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1491 | adsp_info(dsp, "%d: ID %x v%d.%d.%d DM@%x ZM@%x\n", |
| 1492 | i, be32_to_cpu(adsp1_alg[i].alg.id), |
| 1493 | (be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff0000) >> 16, |
| 1494 | (be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff00) >> 8, |
| 1495 | be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff, |
| 1496 | be32_to_cpu(adsp1_alg[i].dm), |
| 1497 | be32_to_cpu(adsp1_alg[i].zm)); |
Mark Brown | 471f488 | 2013-01-08 16:09:31 +0000 | [diff] [blame] | 1498 | |
Charles Keepax | d9d20e1 | 2015-04-13 13:27:59 +0100 | [diff] [blame] | 1499 | alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_DM, |
| 1500 | adsp1_alg[i].alg.id, |
| 1501 | adsp1_alg[i].dm); |
| 1502 | if (IS_ERR(alg_region)) { |
| 1503 | ret = PTR_ERR(alg_region); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1504 | goto out; |
| 1505 | } |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1506 | if (dsp->fw_ver == 0) { |
| 1507 | if (i + 1 < n_algs) { |
| 1508 | len = be32_to_cpu(adsp1_alg[i + 1].dm); |
| 1509 | len -= be32_to_cpu(adsp1_alg[i].dm); |
| 1510 | len *= 4; |
| 1511 | wm_adsp_create_control(dsp, alg_region, 0, |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 1512 | len, NULL, 0, 0); |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1513 | } else { |
| 1514 | adsp_warn(dsp, "Missing length info for region DM with ID %x\n", |
| 1515 | be32_to_cpu(adsp1_alg[i].alg.id)); |
| 1516 | } |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1517 | } |
Mark Brown | 471f488 | 2013-01-08 16:09:31 +0000 | [diff] [blame] | 1518 | |
Charles Keepax | d9d20e1 | 2015-04-13 13:27:59 +0100 | [diff] [blame] | 1519 | alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_ZM, |
| 1520 | adsp1_alg[i].alg.id, |
| 1521 | adsp1_alg[i].zm); |
| 1522 | if (IS_ERR(alg_region)) { |
| 1523 | ret = PTR_ERR(alg_region); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1524 | goto out; |
| 1525 | } |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1526 | if (dsp->fw_ver == 0) { |
| 1527 | if (i + 1 < n_algs) { |
| 1528 | len = be32_to_cpu(adsp1_alg[i + 1].zm); |
| 1529 | len -= be32_to_cpu(adsp1_alg[i].zm); |
| 1530 | len *= 4; |
| 1531 | wm_adsp_create_control(dsp, alg_region, 0, |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 1532 | len, NULL, 0, 0); |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1533 | } else { |
| 1534 | adsp_warn(dsp, "Missing length info for region ZM with ID %x\n", |
| 1535 | be32_to_cpu(adsp1_alg[i].alg.id)); |
| 1536 | } |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1537 | } |
| 1538 | } |
| 1539 | |
| 1540 | out: |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1541 | kfree(adsp1_alg); |
| 1542 | return ret; |
| 1543 | } |
| 1544 | |
| 1545 | static int wm_adsp2_setup_algs(struct wm_adsp *dsp) |
| 1546 | { |
| 1547 | struct wmfw_adsp2_id_hdr adsp2_id; |
| 1548 | struct wmfw_adsp2_alg_hdr *adsp2_alg; |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1549 | struct wm_adsp_alg_region *alg_region; |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1550 | const struct wm_adsp_region *mem; |
| 1551 | unsigned int pos, len; |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1552 | size_t n_algs; |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1553 | int i, ret; |
| 1554 | |
| 1555 | mem = wm_adsp_find_region(dsp, WMFW_ADSP2_XM); |
| 1556 | if (WARN_ON(!mem)) |
| 1557 | return -EINVAL; |
| 1558 | |
| 1559 | ret = regmap_raw_read(dsp->regmap, mem->base, &adsp2_id, |
| 1560 | sizeof(adsp2_id)); |
| 1561 | if (ret != 0) { |
| 1562 | adsp_err(dsp, "Failed to read algorithm info: %d\n", |
| 1563 | ret); |
| 1564 | return ret; |
| 1565 | } |
| 1566 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1567 | n_algs = be32_to_cpu(adsp2_id.n_algs); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1568 | dsp->fw_id = be32_to_cpu(adsp2_id.fw.id); |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 1569 | dsp->fw_id_version = be32_to_cpu(adsp2_id.fw.ver); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1570 | adsp_info(dsp, "Firmware: %x v%d.%d.%d, %zu algorithms\n", |
| 1571 | dsp->fw_id, |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 1572 | (dsp->fw_id_version & 0xff0000) >> 16, |
| 1573 | (dsp->fw_id_version & 0xff00) >> 8, |
| 1574 | dsp->fw_id_version & 0xff, |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1575 | n_algs); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1576 | |
Charles Keepax | d9d20e1 | 2015-04-13 13:27:59 +0100 | [diff] [blame] | 1577 | alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_XM, |
| 1578 | adsp2_id.fw.id, adsp2_id.xm); |
| 1579 | if (IS_ERR(alg_region)) |
| 1580 | return PTR_ERR(alg_region); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1581 | |
Charles Keepax | d9d20e1 | 2015-04-13 13:27:59 +0100 | [diff] [blame] | 1582 | alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_YM, |
| 1583 | adsp2_id.fw.id, adsp2_id.ym); |
| 1584 | if (IS_ERR(alg_region)) |
| 1585 | return PTR_ERR(alg_region); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1586 | |
Charles Keepax | d9d20e1 | 2015-04-13 13:27:59 +0100 | [diff] [blame] | 1587 | alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_ZM, |
| 1588 | adsp2_id.fw.id, adsp2_id.zm); |
| 1589 | if (IS_ERR(alg_region)) |
| 1590 | return PTR_ERR(alg_region); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1591 | |
| 1592 | pos = sizeof(adsp2_id) / 2; |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1593 | len = (sizeof(*adsp2_alg) * n_algs) / 2; |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1594 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1595 | adsp2_alg = wm_adsp_read_algs(dsp, n_algs, mem->base + pos, len); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1596 | if (IS_ERR(adsp2_alg)) |
| 1597 | return PTR_ERR(adsp2_alg); |
| 1598 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1599 | for (i = 0; i < n_algs; i++) { |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1600 | adsp_info(dsp, |
| 1601 | "%d: ID %x v%d.%d.%d XM@%x YM@%x ZM@%x\n", |
| 1602 | i, be32_to_cpu(adsp2_alg[i].alg.id), |
| 1603 | (be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff0000) >> 16, |
| 1604 | (be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff00) >> 8, |
| 1605 | be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff, |
| 1606 | be32_to_cpu(adsp2_alg[i].xm), |
| 1607 | be32_to_cpu(adsp2_alg[i].ym), |
| 1608 | be32_to_cpu(adsp2_alg[i].zm)); |
| 1609 | |
Charles Keepax | d9d20e1 | 2015-04-13 13:27:59 +0100 | [diff] [blame] | 1610 | alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_XM, |
| 1611 | adsp2_alg[i].alg.id, |
| 1612 | adsp2_alg[i].xm); |
| 1613 | if (IS_ERR(alg_region)) { |
| 1614 | ret = PTR_ERR(alg_region); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1615 | goto out; |
| 1616 | } |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1617 | if (dsp->fw_ver == 0) { |
| 1618 | if (i + 1 < n_algs) { |
| 1619 | len = be32_to_cpu(adsp2_alg[i + 1].xm); |
| 1620 | len -= be32_to_cpu(adsp2_alg[i].xm); |
| 1621 | len *= 4; |
| 1622 | wm_adsp_create_control(dsp, alg_region, 0, |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 1623 | len, NULL, 0, 0); |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1624 | } else { |
| 1625 | adsp_warn(dsp, "Missing length info for region XM with ID %x\n", |
| 1626 | be32_to_cpu(adsp2_alg[i].alg.id)); |
| 1627 | } |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1628 | } |
| 1629 | |
Charles Keepax | d9d20e1 | 2015-04-13 13:27:59 +0100 | [diff] [blame] | 1630 | alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_YM, |
| 1631 | adsp2_alg[i].alg.id, |
| 1632 | adsp2_alg[i].ym); |
| 1633 | if (IS_ERR(alg_region)) { |
| 1634 | ret = PTR_ERR(alg_region); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1635 | goto out; |
| 1636 | } |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1637 | if (dsp->fw_ver == 0) { |
| 1638 | if (i + 1 < n_algs) { |
| 1639 | len = be32_to_cpu(adsp2_alg[i + 1].ym); |
| 1640 | len -= be32_to_cpu(adsp2_alg[i].ym); |
| 1641 | len *= 4; |
| 1642 | wm_adsp_create_control(dsp, alg_region, 0, |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 1643 | len, NULL, 0, 0); |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1644 | } else { |
| 1645 | adsp_warn(dsp, "Missing length info for region YM with ID %x\n", |
| 1646 | be32_to_cpu(adsp2_alg[i].alg.id)); |
| 1647 | } |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1648 | } |
| 1649 | |
Charles Keepax | d9d20e1 | 2015-04-13 13:27:59 +0100 | [diff] [blame] | 1650 | alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_ZM, |
| 1651 | adsp2_alg[i].alg.id, |
| 1652 | adsp2_alg[i].zm); |
| 1653 | if (IS_ERR(alg_region)) { |
| 1654 | ret = PTR_ERR(alg_region); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1655 | goto out; |
| 1656 | } |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1657 | if (dsp->fw_ver == 0) { |
| 1658 | if (i + 1 < n_algs) { |
| 1659 | len = be32_to_cpu(adsp2_alg[i + 1].zm); |
| 1660 | len -= be32_to_cpu(adsp2_alg[i].zm); |
| 1661 | len *= 4; |
| 1662 | wm_adsp_create_control(dsp, alg_region, 0, |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 1663 | len, NULL, 0, 0); |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1664 | } else { |
| 1665 | adsp_warn(dsp, "Missing length info for region ZM with ID %x\n", |
| 1666 | be32_to_cpu(adsp2_alg[i].alg.id)); |
| 1667 | } |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1668 | } |
| 1669 | } |
| 1670 | |
| 1671 | out: |
| 1672 | kfree(adsp2_alg); |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1673 | return ret; |
| 1674 | } |
| 1675 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1676 | static int wm_adsp_load_coeff(struct wm_adsp *dsp) |
| 1677 | { |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 1678 | LIST_HEAD(buf_list); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1679 | struct regmap *regmap = dsp->regmap; |
| 1680 | struct wmfw_coeff_hdr *hdr; |
| 1681 | struct wmfw_coeff_item *blk; |
| 1682 | const struct firmware *firmware; |
Mark Brown | 471f488 | 2013-01-08 16:09:31 +0000 | [diff] [blame] | 1683 | const struct wm_adsp_region *mem; |
| 1684 | struct wm_adsp_alg_region *alg_region; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1685 | const char *region_name; |
| 1686 | int ret, pos, blocks, type, offset, reg; |
| 1687 | char *file; |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 1688 | struct wm_adsp_buf *buf; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1689 | |
| 1690 | file = kzalloc(PAGE_SIZE, GFP_KERNEL); |
| 1691 | if (file == NULL) |
| 1692 | return -ENOMEM; |
| 1693 | |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 1694 | snprintf(file, PAGE_SIZE, "%s-dsp%d-%s.bin", dsp->part, dsp->num, |
| 1695 | wm_adsp_fw[dsp->fw].file); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1696 | file[PAGE_SIZE - 1] = '\0'; |
| 1697 | |
| 1698 | ret = request_firmware(&firmware, file, dsp->dev); |
| 1699 | if (ret != 0) { |
| 1700 | adsp_warn(dsp, "Failed to request '%s'\n", file); |
| 1701 | ret = 0; |
| 1702 | goto out; |
| 1703 | } |
| 1704 | ret = -EINVAL; |
| 1705 | |
| 1706 | if (sizeof(*hdr) >= firmware->size) { |
| 1707 | adsp_err(dsp, "%s: file too short, %zu bytes\n", |
| 1708 | file, firmware->size); |
| 1709 | goto out_fw; |
| 1710 | } |
| 1711 | |
Charles Keepax | 7585a5b | 2015-12-08 16:08:25 +0000 | [diff] [blame] | 1712 | hdr = (void *)&firmware->data[0]; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1713 | if (memcmp(hdr->magic, "WMDR", 4) != 0) { |
| 1714 | adsp_err(dsp, "%s: invalid magic\n", file); |
Charles Keepax | a4cdbec | 2013-01-21 09:02:31 +0000 | [diff] [blame] | 1715 | goto out_fw; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1716 | } |
| 1717 | |
Mark Brown | c712326 | 2013-01-16 16:59:04 +0900 | [diff] [blame] | 1718 | switch (be32_to_cpu(hdr->rev) & 0xff) { |
| 1719 | case 1: |
| 1720 | break; |
| 1721 | default: |
| 1722 | adsp_err(dsp, "%s: Unsupported coefficient file format %d\n", |
| 1723 | file, be32_to_cpu(hdr->rev) & 0xff); |
| 1724 | ret = -EINVAL; |
| 1725 | goto out_fw; |
| 1726 | } |
| 1727 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1728 | adsp_dbg(dsp, "%s: v%d.%d.%d\n", file, |
| 1729 | (le32_to_cpu(hdr->ver) >> 16) & 0xff, |
| 1730 | (le32_to_cpu(hdr->ver) >> 8) & 0xff, |
| 1731 | le32_to_cpu(hdr->ver) & 0xff); |
| 1732 | |
| 1733 | pos = le32_to_cpu(hdr->len); |
| 1734 | |
| 1735 | blocks = 0; |
| 1736 | while (pos < firmware->size && |
| 1737 | pos - firmware->size > sizeof(*blk)) { |
Charles Keepax | 7585a5b | 2015-12-08 16:08:25 +0000 | [diff] [blame] | 1738 | blk = (void *)(&firmware->data[pos]); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1739 | |
Mark Brown | c712326 | 2013-01-16 16:59:04 +0900 | [diff] [blame] | 1740 | type = le16_to_cpu(blk->type); |
| 1741 | offset = le16_to_cpu(blk->offset); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1742 | |
| 1743 | adsp_dbg(dsp, "%s.%d: %x v%d.%d.%d\n", |
| 1744 | file, blocks, le32_to_cpu(blk->id), |
| 1745 | (le32_to_cpu(blk->ver) >> 16) & 0xff, |
| 1746 | (le32_to_cpu(blk->ver) >> 8) & 0xff, |
| 1747 | le32_to_cpu(blk->ver) & 0xff); |
| 1748 | adsp_dbg(dsp, "%s.%d: %d bytes at 0x%x in %x\n", |
| 1749 | file, blocks, le32_to_cpu(blk->len), offset, type); |
| 1750 | |
| 1751 | reg = 0; |
| 1752 | region_name = "Unknown"; |
| 1753 | switch (type) { |
Mark Brown | c712326 | 2013-01-16 16:59:04 +0900 | [diff] [blame] | 1754 | case (WMFW_NAME_TEXT << 8): |
| 1755 | case (WMFW_INFO_TEXT << 8): |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1756 | break; |
Mark Brown | c712326 | 2013-01-16 16:59:04 +0900 | [diff] [blame] | 1757 | case (WMFW_ABSOLUTE << 8): |
Mark Brown | f395a21 | 2013-03-05 22:39:54 +0800 | [diff] [blame] | 1758 | /* |
| 1759 | * Old files may use this for global |
| 1760 | * coefficients. |
| 1761 | */ |
| 1762 | if (le32_to_cpu(blk->id) == dsp->fw_id && |
| 1763 | offset == 0) { |
| 1764 | region_name = "global coefficients"; |
| 1765 | mem = wm_adsp_find_region(dsp, type); |
| 1766 | if (!mem) { |
| 1767 | adsp_err(dsp, "No ZM\n"); |
| 1768 | break; |
| 1769 | } |
| 1770 | reg = wm_adsp_region_to_reg(mem, 0); |
| 1771 | |
| 1772 | } else { |
| 1773 | region_name = "register"; |
| 1774 | reg = offset; |
| 1775 | } |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1776 | break; |
Mark Brown | 471f488 | 2013-01-08 16:09:31 +0000 | [diff] [blame] | 1777 | |
| 1778 | case WMFW_ADSP1_DM: |
| 1779 | case WMFW_ADSP1_ZM: |
| 1780 | case WMFW_ADSP2_XM: |
| 1781 | case WMFW_ADSP2_YM: |
| 1782 | adsp_dbg(dsp, "%s.%d: %d bytes in %x for %x\n", |
| 1783 | file, blocks, le32_to_cpu(blk->len), |
| 1784 | type, le32_to_cpu(blk->id)); |
| 1785 | |
| 1786 | mem = wm_adsp_find_region(dsp, type); |
| 1787 | if (!mem) { |
| 1788 | adsp_err(dsp, "No base for region %x\n", type); |
| 1789 | break; |
| 1790 | } |
| 1791 | |
Charles Keepax | 1419709 | 2015-12-15 11:29:43 +0000 | [diff] [blame] | 1792 | alg_region = wm_adsp_find_alg_region(dsp, type, |
| 1793 | le32_to_cpu(blk->id)); |
| 1794 | if (alg_region) { |
| 1795 | reg = alg_region->base; |
| 1796 | reg = wm_adsp_region_to_reg(mem, reg); |
| 1797 | reg += offset; |
| 1798 | } else { |
Mark Brown | 471f488 | 2013-01-08 16:09:31 +0000 | [diff] [blame] | 1799 | adsp_err(dsp, "No %x for algorithm %x\n", |
| 1800 | type, le32_to_cpu(blk->id)); |
Charles Keepax | 1419709 | 2015-12-15 11:29:43 +0000 | [diff] [blame] | 1801 | } |
Mark Brown | 471f488 | 2013-01-08 16:09:31 +0000 | [diff] [blame] | 1802 | break; |
| 1803 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1804 | default: |
Mark Brown | 25c62f7e | 2013-01-20 19:02:19 +0900 | [diff] [blame] | 1805 | adsp_err(dsp, "%s.%d: Unknown region type %x at %d\n", |
| 1806 | file, blocks, type, pos); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1807 | break; |
| 1808 | } |
| 1809 | |
| 1810 | if (reg) { |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 1811 | buf = wm_adsp_buf_alloc(blk->data, |
| 1812 | le32_to_cpu(blk->len), |
| 1813 | &buf_list); |
Mark Brown | a76fefa | 2013-01-07 19:03:17 +0000 | [diff] [blame] | 1814 | if (!buf) { |
| 1815 | adsp_err(dsp, "Out of memory\n"); |
Wei Yongjun | f4b8281 | 2013-03-12 00:23:15 +0800 | [diff] [blame] | 1816 | ret = -ENOMEM; |
| 1817 | goto out_fw; |
Mark Brown | a76fefa | 2013-01-07 19:03:17 +0000 | [diff] [blame] | 1818 | } |
| 1819 | |
Mark Brown | 20da6d5 | 2013-01-12 19:58:17 +0000 | [diff] [blame] | 1820 | adsp_dbg(dsp, "%s.%d: Writing %d bytes at %x\n", |
| 1821 | file, blocks, le32_to_cpu(blk->len), |
| 1822 | reg); |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 1823 | ret = regmap_raw_write_async(regmap, reg, buf->buf, |
| 1824 | le32_to_cpu(blk->len)); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1825 | if (ret != 0) { |
| 1826 | adsp_err(dsp, |
Dimitris Papastamos | 43bc3bf | 2013-11-01 15:56:52 +0000 | [diff] [blame] | 1827 | "%s.%d: Failed to write to %x in %s: %d\n", |
| 1828 | file, blocks, reg, region_name, ret); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1829 | } |
| 1830 | } |
| 1831 | |
Charles Keepax | be95101 | 2015-02-16 15:25:49 +0000 | [diff] [blame] | 1832 | pos += (le32_to_cpu(blk->len) + sizeof(*blk) + 3) & ~0x03; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1833 | blocks++; |
| 1834 | } |
| 1835 | |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 1836 | ret = regmap_async_complete(regmap); |
| 1837 | if (ret != 0) |
| 1838 | adsp_err(dsp, "Failed to complete async write: %d\n", ret); |
| 1839 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1840 | if (pos > firmware->size) |
| 1841 | adsp_warn(dsp, "%s.%d: %zu bytes at end of file\n", |
| 1842 | file, blocks, pos - firmware->size); |
| 1843 | |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 1844 | wm_adsp_debugfs_save_binname(dsp, file); |
| 1845 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1846 | out_fw: |
Charles Keepax | 9da7a5a | 2014-11-17 10:48:21 +0000 | [diff] [blame] | 1847 | regmap_async_complete(regmap); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1848 | release_firmware(firmware); |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 1849 | wm_adsp_buf_free(&buf_list); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1850 | out: |
| 1851 | kfree(file); |
Wei Yongjun | f4b8281 | 2013-03-12 00:23:15 +0800 | [diff] [blame] | 1852 | return ret; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1853 | } |
| 1854 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1855 | int wm_adsp1_init(struct wm_adsp *dsp) |
Mark Brown | 5e7a7a2 | 2013-01-16 10:03:56 +0900 | [diff] [blame] | 1856 | { |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1857 | INIT_LIST_HEAD(&dsp->alg_regions); |
Mark Brown | 5e7a7a2 | 2013-01-16 10:03:56 +0900 | [diff] [blame] | 1858 | |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 1859 | mutex_init(&dsp->pwr_lock); |
| 1860 | |
Mark Brown | 5e7a7a2 | 2013-01-16 10:03:56 +0900 | [diff] [blame] | 1861 | return 0; |
| 1862 | } |
| 1863 | EXPORT_SYMBOL_GPL(wm_adsp1_init); |
| 1864 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1865 | int wm_adsp1_event(struct snd_soc_dapm_widget *w, |
| 1866 | struct snd_kcontrol *kcontrol, |
| 1867 | int event) |
| 1868 | { |
Lars-Peter Clausen | 7271851 | 2015-01-13 10:27:34 +0100 | [diff] [blame] | 1869 | struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1870 | struct wm_adsp *dsps = snd_soc_codec_get_drvdata(codec); |
| 1871 | struct wm_adsp *dsp = &dsps[w->shift]; |
Dimitris Papastamos | b0101b4 | 2013-11-01 15:56:56 +0000 | [diff] [blame] | 1872 | struct wm_adsp_alg_region *alg_region; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 1873 | struct wm_coeff_ctl *ctl; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1874 | int ret; |
Charles Keepax | 7585a5b | 2015-12-08 16:08:25 +0000 | [diff] [blame] | 1875 | unsigned int val; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1876 | |
Lars-Peter Clausen | 0020010 | 2014-07-17 22:01:07 +0200 | [diff] [blame] | 1877 | dsp->card = codec->component.card; |
Dimitris Papastamos | 92bb4c3 | 2013-08-01 11:11:28 +0100 | [diff] [blame] | 1878 | |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 1879 | mutex_lock(&dsp->pwr_lock); |
| 1880 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1881 | switch (event) { |
| 1882 | case SND_SOC_DAPM_POST_PMU: |
| 1883 | regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30, |
| 1884 | ADSP1_SYS_ENA, ADSP1_SYS_ENA); |
| 1885 | |
Chris Rattray | 94e205b | 2013-01-18 08:43:09 +0000 | [diff] [blame] | 1886 | /* |
| 1887 | * For simplicity set the DSP clock rate to be the |
| 1888 | * SYSCLK rate rather than making it configurable. |
| 1889 | */ |
Charles Keepax | 7585a5b | 2015-12-08 16:08:25 +0000 | [diff] [blame] | 1890 | if (dsp->sysclk_reg) { |
Chris Rattray | 94e205b | 2013-01-18 08:43:09 +0000 | [diff] [blame] | 1891 | ret = regmap_read(dsp->regmap, dsp->sysclk_reg, &val); |
| 1892 | if (ret != 0) { |
| 1893 | adsp_err(dsp, "Failed to read SYSCLK state: %d\n", |
| 1894 | ret); |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 1895 | goto err_mutex; |
Chris Rattray | 94e205b | 2013-01-18 08:43:09 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
| 1898 | val = (val & dsp->sysclk_mask) |
| 1899 | >> dsp->sysclk_shift; |
| 1900 | |
| 1901 | ret = regmap_update_bits(dsp->regmap, |
| 1902 | dsp->base + ADSP1_CONTROL_31, |
| 1903 | ADSP1_CLK_SEL_MASK, val); |
| 1904 | if (ret != 0) { |
| 1905 | adsp_err(dsp, "Failed to set clock rate: %d\n", |
| 1906 | ret); |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 1907 | goto err_mutex; |
Chris Rattray | 94e205b | 2013-01-18 08:43:09 +0000 | [diff] [blame] | 1908 | } |
| 1909 | } |
| 1910 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1911 | ret = wm_adsp_load(dsp); |
| 1912 | if (ret != 0) |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 1913 | goto err_ena; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1914 | |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1915 | ret = wm_adsp1_setup_algs(dsp); |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1916 | if (ret != 0) |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 1917 | goto err_ena; |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1918 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1919 | ret = wm_adsp_load_coeff(dsp); |
| 1920 | if (ret != 0) |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 1921 | goto err_ena; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1922 | |
Dimitris Papastamos | 0c2e3f3 | 2013-05-28 12:01:50 +0100 | [diff] [blame] | 1923 | /* Initialize caches for enabled and unset controls */ |
Dimitris Papastamos | 81ad93e | 2013-07-29 13:51:59 +0100 | [diff] [blame] | 1924 | ret = wm_coeff_init_control_caches(dsp); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 1925 | if (ret != 0) |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 1926 | goto err_ena; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 1927 | |
Dimitris Papastamos | 0c2e3f3 | 2013-05-28 12:01:50 +0100 | [diff] [blame] | 1928 | /* Sync set controls */ |
Dimitris Papastamos | 81ad93e | 2013-07-29 13:51:59 +0100 | [diff] [blame] | 1929 | ret = wm_coeff_sync_controls(dsp); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 1930 | if (ret != 0) |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 1931 | goto err_ena; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 1932 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1933 | /* Start the core running */ |
| 1934 | regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30, |
| 1935 | ADSP1_CORE_ENA | ADSP1_START, |
| 1936 | ADSP1_CORE_ENA | ADSP1_START); |
| 1937 | break; |
| 1938 | |
| 1939 | case SND_SOC_DAPM_PRE_PMD: |
| 1940 | /* Halt the core */ |
| 1941 | regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30, |
| 1942 | ADSP1_CORE_ENA | ADSP1_START, 0); |
| 1943 | |
| 1944 | regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_19, |
| 1945 | ADSP1_WDMA_BUFFER_LENGTH_MASK, 0); |
| 1946 | |
| 1947 | regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30, |
| 1948 | ADSP1_SYS_ENA, 0); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 1949 | |
Dimitris Papastamos | 81ad93e | 2013-07-29 13:51:59 +0100 | [diff] [blame] | 1950 | list_for_each_entry(ctl, &dsp->ctl_list, list) |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 1951 | ctl->enabled = 0; |
Dimitris Papastamos | b0101b4 | 2013-11-01 15:56:56 +0000 | [diff] [blame] | 1952 | |
| 1953 | while (!list_empty(&dsp->alg_regions)) { |
| 1954 | alg_region = list_first_entry(&dsp->alg_regions, |
| 1955 | struct wm_adsp_alg_region, |
| 1956 | list); |
| 1957 | list_del(&alg_region->list); |
| 1958 | kfree(alg_region); |
| 1959 | } |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1960 | break; |
| 1961 | |
| 1962 | default: |
| 1963 | break; |
| 1964 | } |
| 1965 | |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 1966 | mutex_unlock(&dsp->pwr_lock); |
| 1967 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1968 | return 0; |
| 1969 | |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 1970 | err_ena: |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1971 | regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30, |
| 1972 | ADSP1_SYS_ENA, 0); |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 1973 | err_mutex: |
| 1974 | mutex_unlock(&dsp->pwr_lock); |
| 1975 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1976 | return ret; |
| 1977 | } |
| 1978 | EXPORT_SYMBOL_GPL(wm_adsp1_event); |
| 1979 | |
| 1980 | static int wm_adsp2_ena(struct wm_adsp *dsp) |
| 1981 | { |
| 1982 | unsigned int val; |
| 1983 | int ret, count; |
| 1984 | |
Mark Brown | 1552c32 | 2013-11-28 18:11:38 +0000 | [diff] [blame] | 1985 | ret = regmap_update_bits_async(dsp->regmap, dsp->base + ADSP2_CONTROL, |
| 1986 | ADSP2_SYS_ENA, ADSP2_SYS_ENA); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1987 | if (ret != 0) |
| 1988 | return ret; |
| 1989 | |
| 1990 | /* Wait for the RAM to start, should be near instantaneous */ |
Charles Keepax | 939fd1e | 2013-12-18 09:25:49 +0000 | [diff] [blame] | 1991 | for (count = 0; count < 10; ++count) { |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1992 | ret = regmap_read(dsp->regmap, dsp->base + ADSP2_STATUS1, |
| 1993 | &val); |
| 1994 | if (ret != 0) |
| 1995 | return ret; |
Charles Keepax | 939fd1e | 2013-12-18 09:25:49 +0000 | [diff] [blame] | 1996 | |
| 1997 | if (val & ADSP2_RAM_RDY) |
| 1998 | break; |
| 1999 | |
| 2000 | msleep(1); |
| 2001 | } |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 2002 | |
| 2003 | if (!(val & ADSP2_RAM_RDY)) { |
| 2004 | adsp_err(dsp, "Failed to start DSP RAM\n"); |
| 2005 | return -EBUSY; |
| 2006 | } |
| 2007 | |
| 2008 | adsp_dbg(dsp, "RAM ready after %d polls\n", count); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 2009 | |
| 2010 | return 0; |
| 2011 | } |
| 2012 | |
Charles Keepax | 18b1a90 | 2014-01-09 09:06:54 +0000 | [diff] [blame] | 2013 | static void wm_adsp2_boot_work(struct work_struct *work) |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2014 | { |
| 2015 | struct wm_adsp *dsp = container_of(work, |
| 2016 | struct wm_adsp, |
| 2017 | boot_work); |
| 2018 | int ret; |
| 2019 | unsigned int val; |
| 2020 | |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2021 | mutex_lock(&dsp->pwr_lock); |
| 2022 | |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2023 | /* |
| 2024 | * For simplicity set the DSP clock rate to be the |
| 2025 | * SYSCLK rate rather than making it configurable. |
| 2026 | */ |
| 2027 | ret = regmap_read(dsp->regmap, ARIZONA_SYSTEM_CLOCK_1, &val); |
| 2028 | if (ret != 0) { |
| 2029 | adsp_err(dsp, "Failed to read SYSCLK state: %d\n", ret); |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2030 | goto err_mutex; |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2031 | } |
| 2032 | val = (val & ARIZONA_SYSCLK_FREQ_MASK) |
| 2033 | >> ARIZONA_SYSCLK_FREQ_SHIFT; |
| 2034 | |
| 2035 | ret = regmap_update_bits_async(dsp->regmap, |
| 2036 | dsp->base + ADSP2_CLOCKING, |
| 2037 | ADSP2_CLK_SEL_MASK, val); |
| 2038 | if (ret != 0) { |
| 2039 | adsp_err(dsp, "Failed to set clock rate: %d\n", ret); |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2040 | goto err_mutex; |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2041 | } |
| 2042 | |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2043 | ret = wm_adsp2_ena(dsp); |
| 2044 | if (ret != 0) |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2045 | goto err_mutex; |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2046 | |
| 2047 | ret = wm_adsp_load(dsp); |
| 2048 | if (ret != 0) |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2049 | goto err_ena; |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2050 | |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 2051 | ret = wm_adsp2_setup_algs(dsp); |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2052 | if (ret != 0) |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2053 | goto err_ena; |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2054 | |
| 2055 | ret = wm_adsp_load_coeff(dsp); |
| 2056 | if (ret != 0) |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2057 | goto err_ena; |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2058 | |
| 2059 | /* Initialize caches for enabled and unset controls */ |
| 2060 | ret = wm_coeff_init_control_caches(dsp); |
| 2061 | if (ret != 0) |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2062 | goto err_ena; |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2063 | |
| 2064 | /* Sync set controls */ |
| 2065 | ret = wm_coeff_sync_controls(dsp); |
| 2066 | if (ret != 0) |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2067 | goto err_ena; |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2068 | |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2069 | dsp->running = true; |
| 2070 | |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2071 | mutex_unlock(&dsp->pwr_lock); |
| 2072 | |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2073 | return; |
| 2074 | |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2075 | err_ena: |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2076 | regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL, |
| 2077 | ADSP2_SYS_ENA | ADSP2_CORE_ENA | ADSP2_START, 0); |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2078 | err_mutex: |
| 2079 | mutex_unlock(&dsp->pwr_lock); |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2080 | } |
| 2081 | |
Charles Keepax | 12db5ed | 2014-01-08 17:42:19 +0000 | [diff] [blame] | 2082 | int wm_adsp2_early_event(struct snd_soc_dapm_widget *w, |
| 2083 | struct snd_kcontrol *kcontrol, int event) |
| 2084 | { |
Lars-Peter Clausen | 7271851 | 2015-01-13 10:27:34 +0100 | [diff] [blame] | 2085 | struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm); |
Charles Keepax | 12db5ed | 2014-01-08 17:42:19 +0000 | [diff] [blame] | 2086 | struct wm_adsp *dsps = snd_soc_codec_get_drvdata(codec); |
| 2087 | struct wm_adsp *dsp = &dsps[w->shift]; |
| 2088 | |
Lars-Peter Clausen | 0020010 | 2014-07-17 22:01:07 +0200 | [diff] [blame] | 2089 | dsp->card = codec->component.card; |
Charles Keepax | 12db5ed | 2014-01-08 17:42:19 +0000 | [diff] [blame] | 2090 | |
| 2091 | switch (event) { |
| 2092 | case SND_SOC_DAPM_PRE_PMU: |
| 2093 | queue_work(system_unbound_wq, &dsp->boot_work); |
| 2094 | break; |
| 2095 | default: |
| 2096 | break; |
Charles Keepax | cab2725 | 2014-04-17 13:42:54 +0100 | [diff] [blame] | 2097 | } |
Charles Keepax | 12db5ed | 2014-01-08 17:42:19 +0000 | [diff] [blame] | 2098 | |
| 2099 | return 0; |
| 2100 | } |
| 2101 | EXPORT_SYMBOL_GPL(wm_adsp2_early_event); |
| 2102 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 2103 | int wm_adsp2_event(struct snd_soc_dapm_widget *w, |
| 2104 | struct snd_kcontrol *kcontrol, int event) |
| 2105 | { |
Lars-Peter Clausen | 7271851 | 2015-01-13 10:27:34 +0100 | [diff] [blame] | 2106 | struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 2107 | struct wm_adsp *dsps = snd_soc_codec_get_drvdata(codec); |
| 2108 | struct wm_adsp *dsp = &dsps[w->shift]; |
Mark Brown | 471f488 | 2013-01-08 16:09:31 +0000 | [diff] [blame] | 2109 | struct wm_adsp_alg_region *alg_region; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 2110 | struct wm_coeff_ctl *ctl; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 2111 | int ret; |
| 2112 | |
| 2113 | switch (event) { |
| 2114 | case SND_SOC_DAPM_POST_PMU: |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2115 | flush_work(&dsp->boot_work); |
Mark Brown | dd49e2c | 2012-12-02 21:50:46 +0900 | [diff] [blame] | 2116 | |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2117 | if (!dsp->running) |
| 2118 | return -EIO; |
Mark Brown | dd49e2c | 2012-12-02 21:50:46 +0900 | [diff] [blame] | 2119 | |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2120 | ret = regmap_update_bits(dsp->regmap, |
| 2121 | dsp->base + ADSP2_CONTROL, |
Charles Keepax | 00e4c3b | 2014-11-18 16:25:27 +0000 | [diff] [blame] | 2122 | ADSP2_CORE_ENA | ADSP2_START, |
| 2123 | ADSP2_CORE_ENA | ADSP2_START); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 2124 | if (ret != 0) |
| 2125 | goto err; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 2126 | break; |
| 2127 | |
| 2128 | case SND_SOC_DAPM_PRE_PMD: |
Richard Fitzgerald | 10337b0 | 2015-05-29 10:23:07 +0100 | [diff] [blame] | 2129 | /* Log firmware state, it can be useful for analysis */ |
| 2130 | wm_adsp2_show_fw_status(dsp); |
| 2131 | |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2132 | mutex_lock(&dsp->pwr_lock); |
| 2133 | |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 2134 | wm_adsp_debugfs_clear(dsp); |
| 2135 | |
| 2136 | dsp->fw_id = 0; |
| 2137 | dsp->fw_id_version = 0; |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 2138 | dsp->running = false; |
| 2139 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 2140 | regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL, |
Mark Brown | a7f9be7 | 2012-11-28 19:53:59 +0000 | [diff] [blame] | 2141 | ADSP2_SYS_ENA | ADSP2_CORE_ENA | |
| 2142 | ADSP2_START, 0); |
Mark Brown | 973838a | 2012-11-28 17:20:32 +0000 | [diff] [blame] | 2143 | |
Mark Brown | 2d30b57 | 2013-01-28 20:18:17 +0800 | [diff] [blame] | 2144 | /* Make sure DMAs are quiesced */ |
| 2145 | regmap_write(dsp->regmap, dsp->base + ADSP2_WDMA_CONFIG_1, 0); |
| 2146 | regmap_write(dsp->regmap, dsp->base + ADSP2_WDMA_CONFIG_2, 0); |
| 2147 | regmap_write(dsp->regmap, dsp->base + ADSP2_RDMA_CONFIG_1, 0); |
| 2148 | |
Dimitris Papastamos | 81ad93e | 2013-07-29 13:51:59 +0100 | [diff] [blame] | 2149 | list_for_each_entry(ctl, &dsp->ctl_list, list) |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 2150 | ctl->enabled = 0; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 2151 | |
Mark Brown | 471f488 | 2013-01-08 16:09:31 +0000 | [diff] [blame] | 2152 | while (!list_empty(&dsp->alg_regions)) { |
| 2153 | alg_region = list_first_entry(&dsp->alg_regions, |
| 2154 | struct wm_adsp_alg_region, |
| 2155 | list); |
| 2156 | list_del(&alg_region->list); |
| 2157 | kfree(alg_region); |
| 2158 | } |
Charles Keepax | ddbc5ef | 2014-01-22 10:09:11 +0000 | [diff] [blame] | 2159 | |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2160 | mutex_unlock(&dsp->pwr_lock); |
| 2161 | |
Charles Keepax | ddbc5ef | 2014-01-22 10:09:11 +0000 | [diff] [blame] | 2162 | adsp_dbg(dsp, "Shutdown complete\n"); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 2163 | break; |
| 2164 | |
| 2165 | default: |
| 2166 | break; |
| 2167 | } |
| 2168 | |
| 2169 | return 0; |
| 2170 | err: |
| 2171 | regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL, |
Mark Brown | a7f9be7 | 2012-11-28 19:53:59 +0000 | [diff] [blame] | 2172 | ADSP2_SYS_ENA | ADSP2_CORE_ENA | ADSP2_START, 0); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 2173 | return ret; |
| 2174 | } |
| 2175 | EXPORT_SYMBOL_GPL(wm_adsp2_event); |
Mark Brown | 973838a | 2012-11-28 17:20:32 +0000 | [diff] [blame] | 2176 | |
Richard Fitzgerald | f5e2ce9 | 2015-06-11 11:32:30 +0100 | [diff] [blame] | 2177 | int wm_adsp2_codec_probe(struct wm_adsp *dsp, struct snd_soc_codec *codec) |
| 2178 | { |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 2179 | wm_adsp2_init_debugfs(dsp, codec); |
| 2180 | |
Richard Fitzgerald | 218e508 | 2015-06-11 11:32:31 +0100 | [diff] [blame] | 2181 | return snd_soc_add_codec_controls(codec, |
Richard Fitzgerald | 336d044 | 2015-06-18 13:43:19 +0100 | [diff] [blame] | 2182 | &wm_adsp_fw_controls[dsp->num - 1], |
| 2183 | 1); |
Richard Fitzgerald | f5e2ce9 | 2015-06-11 11:32:30 +0100 | [diff] [blame] | 2184 | } |
| 2185 | EXPORT_SYMBOL_GPL(wm_adsp2_codec_probe); |
| 2186 | |
| 2187 | int wm_adsp2_codec_remove(struct wm_adsp *dsp, struct snd_soc_codec *codec) |
| 2188 | { |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 2189 | wm_adsp2_cleanup_debugfs(dsp); |
| 2190 | |
Richard Fitzgerald | f5e2ce9 | 2015-06-11 11:32:30 +0100 | [diff] [blame] | 2191 | return 0; |
| 2192 | } |
| 2193 | EXPORT_SYMBOL_GPL(wm_adsp2_codec_remove); |
| 2194 | |
Richard Fitzgerald | 81ac58b | 2015-06-02 11:53:34 +0100 | [diff] [blame] | 2195 | int wm_adsp2_init(struct wm_adsp *dsp) |
Mark Brown | 973838a | 2012-11-28 17:20:32 +0000 | [diff] [blame] | 2196 | { |
| 2197 | int ret; |
| 2198 | |
Mark Brown | 10a2b66 | 2012-12-02 21:37:00 +0900 | [diff] [blame] | 2199 | /* |
| 2200 | * Disable the DSP memory by default when in reset for a small |
| 2201 | * power saving. |
| 2202 | */ |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 2203 | ret = regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL, |
Mark Brown | 10a2b66 | 2012-12-02 21:37:00 +0900 | [diff] [blame] | 2204 | ADSP2_MEM_ENA, 0); |
| 2205 | if (ret != 0) { |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 2206 | adsp_err(dsp, "Failed to clear memory retention: %d\n", ret); |
Mark Brown | 10a2b66 | 2012-12-02 21:37:00 +0900 | [diff] [blame] | 2207 | return ret; |
| 2208 | } |
| 2209 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 2210 | INIT_LIST_HEAD(&dsp->alg_regions); |
| 2211 | INIT_LIST_HEAD(&dsp->ctl_list); |
| 2212 | INIT_WORK(&dsp->boot_work, wm_adsp2_boot_work); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 2213 | |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2214 | mutex_init(&dsp->pwr_lock); |
| 2215 | |
Mark Brown | 973838a | 2012-11-28 17:20:32 +0000 | [diff] [blame] | 2216 | return 0; |
| 2217 | } |
| 2218 | EXPORT_SYMBOL_GPL(wm_adsp2_init); |
Praveen Diwakar | 0a37c6e | 2014-07-04 11:17:41 +0530 | [diff] [blame] | 2219 | |
Charles Keepax | 406abc9 | 2015-12-15 11:29:45 +0000 | [diff] [blame^] | 2220 | int wm_adsp_compr_open(struct wm_adsp *dsp, struct snd_compr_stream *stream) |
| 2221 | { |
| 2222 | struct wm_adsp_compr *compr; |
| 2223 | int ret = 0; |
| 2224 | |
| 2225 | mutex_lock(&dsp->pwr_lock); |
| 2226 | |
| 2227 | if (wm_adsp_fw[dsp->fw].num_caps == 0) { |
| 2228 | adsp_err(dsp, "Firmware does not support compressed API\n"); |
| 2229 | ret = -ENXIO; |
| 2230 | goto out; |
| 2231 | } |
| 2232 | |
| 2233 | if (wm_adsp_fw[dsp->fw].compr_direction != stream->direction) { |
| 2234 | adsp_err(dsp, "Firmware does not support stream direction\n"); |
| 2235 | ret = -EINVAL; |
| 2236 | goto out; |
| 2237 | } |
| 2238 | |
| 2239 | compr = kzalloc(sizeof(*compr), GFP_KERNEL); |
| 2240 | if (!compr) { |
| 2241 | ret = -ENOMEM; |
| 2242 | goto out; |
| 2243 | } |
| 2244 | |
| 2245 | compr->dsp = dsp; |
| 2246 | compr->stream = stream; |
| 2247 | |
| 2248 | dsp->compr = compr; |
| 2249 | |
| 2250 | stream->runtime->private_data = compr; |
| 2251 | |
| 2252 | out: |
| 2253 | mutex_unlock(&dsp->pwr_lock); |
| 2254 | |
| 2255 | return ret; |
| 2256 | } |
| 2257 | EXPORT_SYMBOL_GPL(wm_adsp_compr_open); |
| 2258 | |
| 2259 | int wm_adsp_compr_free(struct snd_compr_stream *stream) |
| 2260 | { |
| 2261 | struct wm_adsp_compr *compr = stream->runtime->private_data; |
| 2262 | struct wm_adsp *dsp = compr->dsp; |
| 2263 | |
| 2264 | mutex_lock(&dsp->pwr_lock); |
| 2265 | |
| 2266 | dsp->compr = NULL; |
| 2267 | |
| 2268 | kfree(compr); |
| 2269 | |
| 2270 | mutex_unlock(&dsp->pwr_lock); |
| 2271 | |
| 2272 | return 0; |
| 2273 | } |
| 2274 | EXPORT_SYMBOL_GPL(wm_adsp_compr_free); |
| 2275 | |
| 2276 | static int wm_adsp_compr_check_params(struct snd_compr_stream *stream, |
| 2277 | struct snd_compr_params *params) |
| 2278 | { |
| 2279 | struct wm_adsp_compr *compr = stream->runtime->private_data; |
| 2280 | struct wm_adsp *dsp = compr->dsp; |
| 2281 | const struct wm_adsp_fw_caps *caps; |
| 2282 | const struct snd_codec_desc *desc; |
| 2283 | int i, j; |
| 2284 | |
| 2285 | if (params->buffer.fragment_size < WM_ADSP_MIN_FRAGMENT_SIZE || |
| 2286 | params->buffer.fragment_size > WM_ADSP_MAX_FRAGMENT_SIZE || |
| 2287 | params->buffer.fragments < WM_ADSP_MIN_FRAGMENTS || |
| 2288 | params->buffer.fragments > WM_ADSP_MAX_FRAGMENTS || |
| 2289 | params->buffer.fragment_size % WM_ADSP_DATA_WORD_SIZE) { |
| 2290 | adsp_err(dsp, "Invalid buffer fragsize=%d fragments=%d\n", |
| 2291 | params->buffer.fragment_size, |
| 2292 | params->buffer.fragments); |
| 2293 | |
| 2294 | return -EINVAL; |
| 2295 | } |
| 2296 | |
| 2297 | for (i = 0; i < wm_adsp_fw[dsp->fw].num_caps; i++) { |
| 2298 | caps = &wm_adsp_fw[dsp->fw].caps[i]; |
| 2299 | desc = &caps->desc; |
| 2300 | |
| 2301 | if (caps->id != params->codec.id) |
| 2302 | continue; |
| 2303 | |
| 2304 | if (stream->direction == SND_COMPRESS_PLAYBACK) { |
| 2305 | if (desc->max_ch < params->codec.ch_out) |
| 2306 | continue; |
| 2307 | } else { |
| 2308 | if (desc->max_ch < params->codec.ch_in) |
| 2309 | continue; |
| 2310 | } |
| 2311 | |
| 2312 | if (!(desc->formats & (1 << params->codec.format))) |
| 2313 | continue; |
| 2314 | |
| 2315 | for (j = 0; j < desc->num_sample_rates; ++j) |
| 2316 | if (desc->sample_rates[j] == params->codec.sample_rate) |
| 2317 | return 0; |
| 2318 | } |
| 2319 | |
| 2320 | adsp_err(dsp, "Invalid params id=%u ch=%u,%u rate=%u fmt=%u\n", |
| 2321 | params->codec.id, params->codec.ch_in, params->codec.ch_out, |
| 2322 | params->codec.sample_rate, params->codec.format); |
| 2323 | return -EINVAL; |
| 2324 | } |
| 2325 | |
| 2326 | int wm_adsp_compr_set_params(struct snd_compr_stream *stream, |
| 2327 | struct snd_compr_params *params) |
| 2328 | { |
| 2329 | struct wm_adsp_compr *compr = stream->runtime->private_data; |
| 2330 | int ret; |
| 2331 | |
| 2332 | ret = wm_adsp_compr_check_params(stream, params); |
| 2333 | if (ret) |
| 2334 | return ret; |
| 2335 | |
| 2336 | compr->size = params->buffer; |
| 2337 | |
| 2338 | adsp_dbg(compr->dsp, "fragment_size=%d fragments=%d\n", |
| 2339 | compr->size.fragment_size, compr->size.fragments); |
| 2340 | |
| 2341 | return 0; |
| 2342 | } |
| 2343 | EXPORT_SYMBOL_GPL(wm_adsp_compr_set_params); |
| 2344 | |
| 2345 | int wm_adsp_compr_get_caps(struct snd_compr_stream *stream, |
| 2346 | struct snd_compr_caps *caps) |
| 2347 | { |
| 2348 | struct wm_adsp_compr *compr = stream->runtime->private_data; |
| 2349 | int fw = compr->dsp->fw; |
| 2350 | int i; |
| 2351 | |
| 2352 | if (wm_adsp_fw[fw].caps) { |
| 2353 | for (i = 0; i < wm_adsp_fw[fw].num_caps; i++) |
| 2354 | caps->codecs[i] = wm_adsp_fw[fw].caps[i].id; |
| 2355 | |
| 2356 | caps->num_codecs = i; |
| 2357 | caps->direction = wm_adsp_fw[fw].compr_direction; |
| 2358 | |
| 2359 | caps->min_fragment_size = WM_ADSP_MIN_FRAGMENT_SIZE; |
| 2360 | caps->max_fragment_size = WM_ADSP_MAX_FRAGMENT_SIZE; |
| 2361 | caps->min_fragments = WM_ADSP_MIN_FRAGMENTS; |
| 2362 | caps->max_fragments = WM_ADSP_MAX_FRAGMENTS; |
| 2363 | } |
| 2364 | |
| 2365 | return 0; |
| 2366 | } |
| 2367 | EXPORT_SYMBOL_GPL(wm_adsp_compr_get_caps); |
| 2368 | |
Praveen Diwakar | 0a37c6e | 2014-07-04 11:17:41 +0530 | [diff] [blame] | 2369 | MODULE_LICENSE("GPL v2"); |