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