Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Asihpi soundcard |
| 3 | * Copyright (c) by AudioScience Inc <alsa@audioscience.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of version 2 of the GNU General Public License as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * |
| 19 | * The following is not a condition of use, merely a request: |
| 20 | * If you modify this program, particularly if you fix errors, AudioScience Inc |
| 21 | * would appreciate it if you grant us the right to use those modifications |
| 22 | * for any purpose including commercial applications. |
| 23 | */ |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 24 | |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 25 | #include "hpi_internal.h" |
| 26 | #include "hpimsginit.h" |
| 27 | #include "hpioctl.h" |
| 28 | |
| 29 | #include <linux/pci.h> |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 30 | #include <linux/version.h> |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 31 | #include <linux/init.h> |
| 32 | #include <linux/jiffies.h> |
| 33 | #include <linux/slab.h> |
| 34 | #include <linux/time.h> |
| 35 | #include <linux/wait.h> |
| 36 | #include <sound/core.h> |
| 37 | #include <sound/control.h> |
| 38 | #include <sound/pcm.h> |
| 39 | #include <sound/pcm_params.h> |
| 40 | #include <sound/info.h> |
| 41 | #include <sound/initval.h> |
| 42 | #include <sound/tlv.h> |
| 43 | #include <sound/hwdep.h> |
| 44 | |
Eliot Blennerhassett | f3d145a | 2011-04-05 20:55:44 +1200 | [diff] [blame] | 45 | |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 46 | MODULE_LICENSE("GPL"); |
| 47 | MODULE_AUTHOR("AudioScience inc. <support@audioscience.com>"); |
| 48 | MODULE_DESCRIPTION("AudioScience ALSA ASI5000 ASI6000 ASI87xx ASI89xx"); |
| 49 | |
Eliot Blennerhassett | b0096a6 | 2011-04-05 20:55:46 +1200 | [diff] [blame] | 50 | #if defined CONFIG_SND_DEBUG |
Eliot Blennerhassett | a647713 | 2011-04-05 20:55:42 +1200 | [diff] [blame] | 51 | /* copied from pcm_lib.c, hope later patch will make that version public |
| 52 | and this copy can be removed */ |
Eliot Blennerhassett | 0a17e99 | 2011-07-22 15:52:44 +1200 | [diff] [blame] | 53 | static inline void |
| 54 | snd_pcm_debug_name(struct snd_pcm_substream *substream, char *buf, size_t size) |
Eliot Blennerhassett | a647713 | 2011-04-05 20:55:42 +1200 | [diff] [blame] | 55 | { |
Eliot Blennerhassett | 0a17e99 | 2011-07-22 15:52:44 +1200 | [diff] [blame] | 56 | snprintf(buf, size, "pcmC%dD%d%c:%d", |
Eliot Blennerhassett | a647713 | 2011-04-05 20:55:42 +1200 | [diff] [blame] | 57 | substream->pcm->card->number, |
| 58 | substream->pcm->device, |
| 59 | substream->stream ? 'c' : 'p', |
| 60 | substream->number); |
| 61 | } |
Eliot Blennerhassett | b2e65c8 | 2011-03-25 15:25:48 +1300 | [diff] [blame] | 62 | #else |
Eliot Blennerhassett | 0a17e99 | 2011-07-22 15:52:44 +1200 | [diff] [blame] | 63 | static inline void |
| 64 | snd_pcm_debug_name(struct snd_pcm_substream *substream, char *buf, size_t size) |
| 65 | { |
| 66 | *buf = 0; |
| 67 | } |
Eliot Blennerhassett | b0096a6 | 2011-04-05 20:55:46 +1200 | [diff] [blame] | 68 | #endif |
Eliot Blennerhassett | a647713 | 2011-04-05 20:55:42 +1200 | [diff] [blame] | 69 | |
Eliot Blennerhassett | b2e65c8 | 2011-03-25 15:25:48 +1300 | [diff] [blame] | 70 | #if defined CONFIG_SND_DEBUG_VERBOSE |
| 71 | /** |
| 72 | * snd_printddd - very verbose debug printk |
| 73 | * @format: format string |
| 74 | * |
| 75 | * Works like snd_printk() for debugging purposes. |
| 76 | * Ignored when CONFIG_SND_DEBUG_VERBOSE is not set. |
| 77 | * Must set snd module debug parameter to 3 to enable at runtime. |
| 78 | */ |
| 79 | #define snd_printddd(format, args...) \ |
| 80 | __snd_printk(3, __FILE__, __LINE__, format, ##args) |
| 81 | #else |
Eliot Blennerhassett | b0096a6 | 2011-04-05 20:55:46 +1200 | [diff] [blame] | 82 | #define snd_printddd(format, args...) do { } while (0) |
Eliot Blennerhassett | b2e65c8 | 2011-03-25 15:25:48 +1300 | [diff] [blame] | 83 | #endif |
| 84 | |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 85 | static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* index 0-MAX */ |
| 86 | static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ |
| 87 | static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; |
| 88 | static int enable_hpi_hwdep = 1; |
| 89 | |
| 90 | module_param_array(index, int, NULL, S_IRUGO); |
| 91 | MODULE_PARM_DESC(index, "ALSA index value for AudioScience soundcard."); |
| 92 | |
| 93 | module_param_array(id, charp, NULL, S_IRUGO); |
| 94 | MODULE_PARM_DESC(id, "ALSA ID string for AudioScience soundcard."); |
| 95 | |
| 96 | module_param_array(enable, bool, NULL, S_IRUGO); |
| 97 | MODULE_PARM_DESC(enable, "ALSA enable AudioScience soundcard."); |
| 98 | |
| 99 | module_param(enable_hpi_hwdep, bool, S_IRUGO|S_IWUSR); |
| 100 | MODULE_PARM_DESC(enable_hpi_hwdep, |
| 101 | "ALSA enable HPI hwdep for AudioScience soundcard "); |
| 102 | |
| 103 | /* identify driver */ |
| 104 | #ifdef KERNEL_ALSA_BUILD |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 105 | static char *build_info = "Built using headers from kernel source"; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 106 | module_param(build_info, charp, S_IRUGO); |
| 107 | MODULE_PARM_DESC(build_info, "built using headers from kernel source"); |
| 108 | #else |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 109 | static char *build_info = "Built within ALSA source"; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 110 | module_param(build_info, charp, S_IRUGO); |
| 111 | MODULE_PARM_DESC(build_info, "built within ALSA source"); |
| 112 | #endif |
| 113 | |
| 114 | /* set to 1 to dump every control from adapter to log */ |
| 115 | static const int mixer_dump; |
| 116 | |
| 117 | #define DEFAULT_SAMPLERATE 44100 |
| 118 | static int adapter_fs = DEFAULT_SAMPLERATE; |
| 119 | |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 120 | /* defaults */ |
| 121 | #define PERIODS_MIN 2 |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 122 | #define PERIOD_BYTES_MIN 2048 |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 123 | #define BUFFER_BYTES_MAX (512 * 1024) |
| 124 | |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 125 | #define MAX_CLOCKSOURCES (HPI_SAMPLECLOCK_SOURCE_LAST + 1 + 7) |
| 126 | |
| 127 | struct clk_source { |
| 128 | int source; |
| 129 | int index; |
| 130 | char *name; |
| 131 | }; |
| 132 | |
| 133 | struct clk_cache { |
| 134 | int count; |
| 135 | int has_local; |
| 136 | struct clk_source s[MAX_CLOCKSOURCES]; |
| 137 | }; |
| 138 | |
| 139 | /* Per card data */ |
| 140 | struct snd_card_asihpi { |
| 141 | struct snd_card *card; |
| 142 | struct pci_dev *pci; |
| 143 | u16 adapter_index; |
| 144 | u32 serial_number; |
| 145 | u16 type; |
| 146 | u16 version; |
| 147 | u16 num_outstreams; |
| 148 | u16 num_instreams; |
| 149 | |
| 150 | u32 h_mixer; |
| 151 | struct clk_cache cc; |
| 152 | |
Eliot Blennerhassett | f3d145a | 2011-04-05 20:55:44 +1200 | [diff] [blame] | 153 | u16 can_dma; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 154 | u16 support_grouping; |
| 155 | u16 support_mrx; |
| 156 | u16 update_interval_frames; |
| 157 | u16 in_max_chans; |
| 158 | u16 out_max_chans; |
| 159 | }; |
| 160 | |
| 161 | /* Per stream data */ |
| 162 | struct snd_card_asihpi_pcm { |
| 163 | struct timer_list timer; |
| 164 | unsigned int respawn_timer; |
| 165 | unsigned int hpi_buffer_attached; |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 166 | unsigned int buffer_bytes; |
| 167 | unsigned int period_bytes; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 168 | unsigned int bytes_per_sec; |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 169 | unsigned int pcm_buf_host_rw_ofs; /* Host R/W pos */ |
| 170 | unsigned int pcm_buf_dma_ofs; /* DMA R/W offset in buffer */ |
| 171 | unsigned int pcm_buf_elapsed_dma_ofs; /* DMA R/W offset in buffer */ |
Eliot Blennerhassett | 0b7ce9e | 2011-04-05 20:55:43 +1200 | [diff] [blame] | 172 | unsigned int drained_count; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 173 | struct snd_pcm_substream *substream; |
| 174 | u32 h_stream; |
| 175 | struct hpi_format format; |
| 176 | }; |
| 177 | |
| 178 | /* universal stream verbs work with out or in stream handles */ |
| 179 | |
| 180 | /* Functions to allow driver to give a buffer to HPI for busmastering */ |
| 181 | |
| 182 | static u16 hpi_stream_host_buffer_attach( |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 183 | u32 h_stream, /* handle to outstream. */ |
| 184 | u32 size_in_bytes, /* size in bytes of bus mastering buffer */ |
| 185 | u32 pci_address |
| 186 | ) |
| 187 | { |
| 188 | struct hpi_message hm; |
| 189 | struct hpi_response hr; |
| 190 | unsigned int obj = hpi_handle_object(h_stream); |
| 191 | |
| 192 | if (!h_stream) |
| 193 | return HPI_ERROR_INVALID_OBJ; |
| 194 | hpi_init_message_response(&hm, &hr, obj, |
| 195 | obj == HPI_OBJ_OSTREAM ? |
| 196 | HPI_OSTREAM_HOSTBUFFER_ALLOC : |
| 197 | HPI_ISTREAM_HOSTBUFFER_ALLOC); |
| 198 | |
| 199 | hpi_handle_to_indexes(h_stream, &hm.adapter_index, |
| 200 | &hm.obj_index); |
| 201 | |
| 202 | hm.u.d.u.buffer.buffer_size = size_in_bytes; |
| 203 | hm.u.d.u.buffer.pci_address = pci_address; |
| 204 | hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER; |
| 205 | hpi_send_recv(&hm, &hr); |
| 206 | return hr.error; |
| 207 | } |
| 208 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 209 | static u16 hpi_stream_host_buffer_detach(u32 h_stream) |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 210 | { |
| 211 | struct hpi_message hm; |
| 212 | struct hpi_response hr; |
| 213 | unsigned int obj = hpi_handle_object(h_stream); |
| 214 | |
| 215 | if (!h_stream) |
| 216 | return HPI_ERROR_INVALID_OBJ; |
| 217 | |
| 218 | hpi_init_message_response(&hm, &hr, obj, |
| 219 | obj == HPI_OBJ_OSTREAM ? |
| 220 | HPI_OSTREAM_HOSTBUFFER_FREE : |
| 221 | HPI_ISTREAM_HOSTBUFFER_FREE); |
| 222 | |
| 223 | hpi_handle_to_indexes(h_stream, &hm.adapter_index, |
| 224 | &hm.obj_index); |
| 225 | hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER; |
| 226 | hpi_send_recv(&hm, &hr); |
| 227 | return hr.error; |
| 228 | } |
| 229 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 230 | static inline u16 hpi_stream_start(u32 h_stream) |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 231 | { |
| 232 | if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM) |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 233 | return hpi_outstream_start(h_stream); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 234 | else |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 235 | return hpi_instream_start(h_stream); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 236 | } |
| 237 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 238 | static inline u16 hpi_stream_stop(u32 h_stream) |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 239 | { |
| 240 | if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM) |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 241 | return hpi_outstream_stop(h_stream); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 242 | else |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 243 | return hpi_instream_stop(h_stream); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | static inline u16 hpi_stream_get_info_ex( |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 247 | u32 h_stream, |
| 248 | u16 *pw_state, |
| 249 | u32 *pbuffer_size, |
| 250 | u32 *pdata_in_buffer, |
| 251 | u32 *psample_count, |
| 252 | u32 *pauxiliary_data |
| 253 | ) |
| 254 | { |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 255 | u16 e; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 256 | if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM) |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 257 | e = hpi_outstream_get_info_ex(h_stream, pw_state, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 258 | pbuffer_size, pdata_in_buffer, |
| 259 | psample_count, pauxiliary_data); |
| 260 | else |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 261 | e = hpi_instream_get_info_ex(h_stream, pw_state, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 262 | pbuffer_size, pdata_in_buffer, |
| 263 | psample_count, pauxiliary_data); |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 264 | return e; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 265 | } |
| 266 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 267 | static inline u16 hpi_stream_group_add( |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 268 | u32 h_master, |
| 269 | u32 h_stream) |
| 270 | { |
| 271 | if (hpi_handle_object(h_master) == HPI_OBJ_OSTREAM) |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 272 | return hpi_outstream_group_add(h_master, h_stream); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 273 | else |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 274 | return hpi_instream_group_add(h_master, h_stream); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 275 | } |
| 276 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 277 | static inline u16 hpi_stream_group_reset(u32 h_stream) |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 278 | { |
| 279 | if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM) |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 280 | return hpi_outstream_group_reset(h_stream); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 281 | else |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 282 | return hpi_instream_group_reset(h_stream); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 283 | } |
| 284 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 285 | static inline u16 hpi_stream_group_get_map( |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 286 | u32 h_stream, u32 *mo, u32 *mi) |
| 287 | { |
| 288 | if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM) |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 289 | return hpi_outstream_group_get_map(h_stream, mo, mi); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 290 | else |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 291 | return hpi_instream_group_get_map(h_stream, mo, mi); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | static u16 handle_error(u16 err, int line, char *filename) |
| 295 | { |
| 296 | if (err) |
| 297 | printk(KERN_WARNING |
| 298 | "in file %s, line %d: HPI error %d\n", |
| 299 | filename, line, err); |
| 300 | return err; |
| 301 | } |
| 302 | |
| 303 | #define hpi_handle_error(x) handle_error(x, __LINE__, __FILE__) |
| 304 | |
| 305 | /***************************** GENERAL PCM ****************/ |
Eliot Blennerhassett | a647713 | 2011-04-05 20:55:42 +1200 | [diff] [blame] | 306 | |
| 307 | static void print_hwparams(struct snd_pcm_substream *substream, |
| 308 | struct snd_pcm_hw_params *p) |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 309 | { |
Eliot Blennerhassett | 0a17e99 | 2011-07-22 15:52:44 +1200 | [diff] [blame] | 310 | char name[16]; |
| 311 | snd_pcm_debug_name(substream, name, sizeof(name)); |
Eliot Blennerhassett | a647713 | 2011-04-05 20:55:42 +1200 | [diff] [blame] | 312 | snd_printd("%s HWPARAMS\n", name); |
| 313 | snd_printd(" samplerate %d Hz\n", params_rate(p)); |
| 314 | snd_printd(" channels %d\n", params_channels(p)); |
| 315 | snd_printd(" format %d\n", params_format(p)); |
| 316 | snd_printd(" subformat %d\n", params_subformat(p)); |
| 317 | snd_printd(" buffer %d B\n", params_buffer_bytes(p)); |
| 318 | snd_printd(" period %d B\n", params_period_bytes(p)); |
| 319 | snd_printd(" access %d\n", params_access(p)); |
| 320 | snd_printd(" period_size %d\n", params_period_size(p)); |
| 321 | snd_printd(" periods %d\n", params_periods(p)); |
| 322 | snd_printd(" buffer_size %d\n", params_buffer_size(p)); |
| 323 | snd_printd(" %d B/s\n", params_rate(p) * |
| 324 | params_channels(p) * |
| 325 | snd_pcm_format_width(params_format(p)) / 8); |
| 326 | |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 327 | } |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 328 | |
| 329 | static snd_pcm_format_t hpi_to_alsa_formats[] = { |
| 330 | -1, /* INVALID */ |
| 331 | SNDRV_PCM_FORMAT_U8, /* HPI_FORMAT_PCM8_UNSIGNED 1 */ |
| 332 | SNDRV_PCM_FORMAT_S16, /* HPI_FORMAT_PCM16_SIGNED 2 */ |
| 333 | -1, /* HPI_FORMAT_MPEG_L1 3 */ |
| 334 | SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L2 4 */ |
| 335 | SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L3 5 */ |
| 336 | -1, /* HPI_FORMAT_DOLBY_AC2 6 */ |
| 337 | -1, /* HPI_FORMAT_DOLBY_AC3 7 */ |
| 338 | SNDRV_PCM_FORMAT_S16_BE,/* HPI_FORMAT_PCM16_BIGENDIAN 8 */ |
| 339 | -1, /* HPI_FORMAT_AA_TAGIT1_HITS 9 */ |
| 340 | -1, /* HPI_FORMAT_AA_TAGIT1_INSERTS 10 */ |
| 341 | SNDRV_PCM_FORMAT_S32, /* HPI_FORMAT_PCM32_SIGNED 11 */ |
| 342 | -1, /* HPI_FORMAT_RAW_BITSTREAM 12 */ |
| 343 | -1, /* HPI_FORMAT_AA_TAGIT1_HITS_EX1 13 */ |
| 344 | SNDRV_PCM_FORMAT_FLOAT, /* HPI_FORMAT_PCM32_FLOAT 14 */ |
| 345 | #if 1 |
| 346 | /* ALSA can't handle 3 byte sample size together with power-of-2 |
| 347 | * constraint on buffer_bytes, so disable this format |
| 348 | */ |
| 349 | -1 |
| 350 | #else |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 351 | /* SNDRV_PCM_FORMAT_S24_3LE */ /* HPI_FORMAT_PCM24_SIGNED 15 */ |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 352 | #endif |
| 353 | }; |
| 354 | |
| 355 | |
| 356 | static int snd_card_asihpi_format_alsa2hpi(snd_pcm_format_t alsa_format, |
| 357 | u16 *hpi_format) |
| 358 | { |
| 359 | u16 format; |
| 360 | |
| 361 | for (format = HPI_FORMAT_PCM8_UNSIGNED; |
| 362 | format <= HPI_FORMAT_PCM24_SIGNED; format++) { |
| 363 | if (hpi_to_alsa_formats[format] == alsa_format) { |
| 364 | *hpi_format = format; |
| 365 | return 0; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | snd_printd(KERN_WARNING "failed match for alsa format %d\n", |
| 370 | alsa_format); |
| 371 | *hpi_format = 0; |
| 372 | return -EINVAL; |
| 373 | } |
| 374 | |
| 375 | static void snd_card_asihpi_pcm_samplerates(struct snd_card_asihpi *asihpi, |
| 376 | struct snd_pcm_hardware *pcmhw) |
| 377 | { |
| 378 | u16 err; |
| 379 | u32 h_control; |
| 380 | u32 sample_rate; |
| 381 | int idx; |
| 382 | unsigned int rate_min = 200000; |
| 383 | unsigned int rate_max = 0; |
| 384 | unsigned int rates = 0; |
| 385 | |
| 386 | if (asihpi->support_mrx) { |
| 387 | rates |= SNDRV_PCM_RATE_CONTINUOUS; |
| 388 | rates |= SNDRV_PCM_RATE_8000_96000; |
| 389 | rate_min = 8000; |
| 390 | rate_max = 100000; |
| 391 | } else { |
| 392 | /* on cards without SRC, |
| 393 | valid rates are determined by sampleclock */ |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 394 | err = hpi_mixer_get_control(asihpi->h_mixer, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 395 | HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0, |
| 396 | HPI_CONTROL_SAMPLECLOCK, &h_control); |
| 397 | if (err) { |
| 398 | snd_printk(KERN_ERR |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 399 | "No local sampleclock, err %d\n", err); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 400 | } |
| 401 | |
Eliot Blennerhassett | 7bf76c3 | 2011-03-25 15:25:46 +1300 | [diff] [blame] | 402 | for (idx = -1; idx < 100; idx++) { |
| 403 | if (idx == -1) { |
| 404 | if (hpi_sample_clock_get_sample_rate(h_control, |
| 405 | &sample_rate)) |
| 406 | continue; |
| 407 | } else if (hpi_sample_clock_query_local_rate(h_control, |
| 408 | idx, &sample_rate)) { |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 409 | break; |
| 410 | } |
| 411 | |
| 412 | rate_min = min(rate_min, sample_rate); |
| 413 | rate_max = max(rate_max, sample_rate); |
| 414 | |
| 415 | switch (sample_rate) { |
| 416 | case 5512: |
| 417 | rates |= SNDRV_PCM_RATE_5512; |
| 418 | break; |
| 419 | case 8000: |
| 420 | rates |= SNDRV_PCM_RATE_8000; |
| 421 | break; |
| 422 | case 11025: |
| 423 | rates |= SNDRV_PCM_RATE_11025; |
| 424 | break; |
| 425 | case 16000: |
| 426 | rates |= SNDRV_PCM_RATE_16000; |
| 427 | break; |
| 428 | case 22050: |
| 429 | rates |= SNDRV_PCM_RATE_22050; |
| 430 | break; |
| 431 | case 32000: |
| 432 | rates |= SNDRV_PCM_RATE_32000; |
| 433 | break; |
| 434 | case 44100: |
| 435 | rates |= SNDRV_PCM_RATE_44100; |
| 436 | break; |
| 437 | case 48000: |
| 438 | rates |= SNDRV_PCM_RATE_48000; |
| 439 | break; |
| 440 | case 64000: |
| 441 | rates |= SNDRV_PCM_RATE_64000; |
| 442 | break; |
| 443 | case 88200: |
| 444 | rates |= SNDRV_PCM_RATE_88200; |
| 445 | break; |
| 446 | case 96000: |
| 447 | rates |= SNDRV_PCM_RATE_96000; |
| 448 | break; |
| 449 | case 176400: |
| 450 | rates |= SNDRV_PCM_RATE_176400; |
| 451 | break; |
| 452 | case 192000: |
| 453 | rates |= SNDRV_PCM_RATE_192000; |
| 454 | break; |
| 455 | default: /* some other rate */ |
| 456 | rates |= SNDRV_PCM_RATE_KNOT; |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 461 | pcmhw->rates = rates; |
| 462 | pcmhw->rate_min = rate_min; |
| 463 | pcmhw->rate_max = rate_max; |
| 464 | } |
| 465 | |
| 466 | static int snd_card_asihpi_pcm_hw_params(struct snd_pcm_substream *substream, |
| 467 | struct snd_pcm_hw_params *params) |
| 468 | { |
| 469 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 470 | struct snd_card_asihpi_pcm *dpcm = runtime->private_data; |
| 471 | struct snd_card_asihpi *card = snd_pcm_substream_chip(substream); |
| 472 | int err; |
| 473 | u16 format; |
Kulikov Vasiliy | 315e8f7 | 2010-07-15 22:48:19 +0400 | [diff] [blame] | 474 | int width; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 475 | unsigned int bytes_per_sec; |
| 476 | |
Eliot Blennerhassett | a647713 | 2011-04-05 20:55:42 +1200 | [diff] [blame] | 477 | print_hwparams(substream, params); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 478 | err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params)); |
| 479 | if (err < 0) |
| 480 | return err; |
| 481 | err = snd_card_asihpi_format_alsa2hpi(params_format(params), &format); |
| 482 | if (err) |
| 483 | return err; |
| 484 | |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 485 | hpi_handle_error(hpi_format_create(&dpcm->format, |
| 486 | params_channels(params), |
| 487 | format, params_rate(params), 0, 0)); |
| 488 | |
| 489 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 490 | if (hpi_instream_reset(dpcm->h_stream) != 0) |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 491 | return -EINVAL; |
| 492 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 493 | if (hpi_instream_set_format( |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 494 | dpcm->h_stream, &dpcm->format) != 0) |
| 495 | return -EINVAL; |
| 496 | } |
| 497 | |
| 498 | dpcm->hpi_buffer_attached = 0; |
Eliot Blennerhassett | f3d145a | 2011-04-05 20:55:44 +1200 | [diff] [blame] | 499 | if (card->can_dma) { |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 500 | err = hpi_stream_host_buffer_attach(dpcm->h_stream, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 501 | params_buffer_bytes(params), runtime->dma_addr); |
| 502 | if (err == 0) { |
Eliot Blennerhassett | b2e65c8 | 2011-03-25 15:25:48 +1300 | [diff] [blame] | 503 | snd_printdd( |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 504 | "stream_host_buffer_attach succeeded %u %lu\n", |
| 505 | params_buffer_bytes(params), |
| 506 | (unsigned long)runtime->dma_addr); |
| 507 | } else { |
Eliot Blennerhassett | b2e65c8 | 2011-03-25 15:25:48 +1300 | [diff] [blame] | 508 | snd_printd("stream_host_buffer_attach error %d\n", |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 509 | err); |
| 510 | return -ENOMEM; |
| 511 | } |
| 512 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 513 | err = hpi_stream_get_info_ex(dpcm->h_stream, NULL, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 514 | &dpcm->hpi_buffer_attached, |
| 515 | NULL, NULL, NULL); |
| 516 | |
Eliot Blennerhassett | b2e65c8 | 2011-03-25 15:25:48 +1300 | [diff] [blame] | 517 | snd_printdd("stream_host_buffer_attach status 0x%x\n", |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 518 | dpcm->hpi_buffer_attached); |
| 519 | } |
| 520 | bytes_per_sec = params_rate(params) * params_channels(params); |
Kulikov Vasiliy | 315e8f7 | 2010-07-15 22:48:19 +0400 | [diff] [blame] | 521 | width = snd_pcm_format_width(params_format(params)); |
| 522 | bytes_per_sec *= width; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 523 | bytes_per_sec /= 8; |
Kulikov Vasiliy | 315e8f7 | 2010-07-15 22:48:19 +0400 | [diff] [blame] | 524 | if (width < 0 || bytes_per_sec == 0) |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 525 | return -EINVAL; |
| 526 | |
| 527 | dpcm->bytes_per_sec = bytes_per_sec; |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 528 | dpcm->buffer_bytes = params_buffer_bytes(params); |
| 529 | dpcm->period_bytes = params_period_bytes(params); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 530 | |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 531 | return 0; |
| 532 | } |
| 533 | |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 534 | static int |
| 535 | snd_card_asihpi_hw_free(struct snd_pcm_substream *substream) |
| 536 | { |
| 537 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 538 | struct snd_card_asihpi_pcm *dpcm = runtime->private_data; |
| 539 | if (dpcm->hpi_buffer_attached) |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 540 | hpi_stream_host_buffer_detach(dpcm->h_stream); |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 541 | |
| 542 | snd_pcm_lib_free_pages(substream); |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | static void snd_card_asihpi_runtime_free(struct snd_pcm_runtime *runtime) |
| 547 | { |
| 548 | struct snd_card_asihpi_pcm *dpcm = runtime->private_data; |
| 549 | kfree(dpcm); |
| 550 | } |
| 551 | |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 552 | static void snd_card_asihpi_pcm_timer_start(struct snd_pcm_substream * |
| 553 | substream) |
| 554 | { |
| 555 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 556 | struct snd_card_asihpi_pcm *dpcm = runtime->private_data; |
| 557 | int expiry; |
| 558 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 559 | expiry = HZ / 200; |
| 560 | /*? (dpcm->period_bytes * HZ / dpcm->bytes_per_sec); */ |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 561 | expiry = max(expiry, 1); /* don't let it be zero! */ |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 562 | dpcm->timer.expires = jiffies + expiry; |
| 563 | dpcm->respawn_timer = 1; |
| 564 | add_timer(&dpcm->timer); |
| 565 | } |
| 566 | |
| 567 | static void snd_card_asihpi_pcm_timer_stop(struct snd_pcm_substream *substream) |
| 568 | { |
| 569 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 570 | struct snd_card_asihpi_pcm *dpcm = runtime->private_data; |
| 571 | |
| 572 | dpcm->respawn_timer = 0; |
| 573 | del_timer(&dpcm->timer); |
| 574 | } |
| 575 | |
| 576 | static int snd_card_asihpi_trigger(struct snd_pcm_substream *substream, |
| 577 | int cmd) |
| 578 | { |
| 579 | struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data; |
| 580 | struct snd_card_asihpi *card = snd_pcm_substream_chip(substream); |
| 581 | struct snd_pcm_substream *s; |
| 582 | u16 e; |
Eliot Blennerhassett | 0a17e99 | 2011-07-22 15:52:44 +1200 | [diff] [blame] | 583 | char name[16]; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 584 | |
Eliot Blennerhassett | 0a17e99 | 2011-07-22 15:52:44 +1200 | [diff] [blame] | 585 | snd_pcm_debug_name(substream, name, sizeof(name)); |
Eliot Blennerhassett | a647713 | 2011-04-05 20:55:42 +1200 | [diff] [blame] | 586 | snd_printdd("%s trigger\n", name); |
| 587 | |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 588 | switch (cmd) { |
| 589 | case SNDRV_PCM_TRIGGER_START: |
| 590 | snd_pcm_group_for_each_entry(s, substream) { |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 591 | struct snd_pcm_runtime *runtime = s->runtime; |
| 592 | struct snd_card_asihpi_pcm *ds = runtime->private_data; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 593 | |
| 594 | if (snd_pcm_substream_chip(s) != card) |
| 595 | continue; |
| 596 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 597 | /* don't link Cap and Play */ |
| 598 | if (substream->stream != s->stream) |
| 599 | continue; |
| 600 | |
Eliot Blennerhassett | 0b7ce9e | 2011-04-05 20:55:43 +1200 | [diff] [blame] | 601 | ds->drained_count = 0; |
Eliot Blennerhassett | f3d145a | 2011-04-05 20:55:44 +1200 | [diff] [blame] | 602 | if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 603 | /* How do I know how much valid data is present |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 604 | * in buffer? Must be at least one period! |
| 605 | * Guessing 2 periods, but if |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 606 | * buffer is bigger it may contain even more |
| 607 | * data?? |
| 608 | */ |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 609 | unsigned int preload = ds->period_bytes * 1; |
Eliot Blennerhassett | b2e65c8 | 2011-03-25 15:25:48 +1300 | [diff] [blame] | 610 | snd_printddd("%d preload x%x\n", s->number, preload); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 611 | hpi_handle_error(hpi_outstream_write_buf( |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 612 | ds->h_stream, |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 613 | &runtime->dma_area[0], |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 614 | preload, |
| 615 | &ds->format)); |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 616 | ds->pcm_buf_host_rw_ofs = preload; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | if (card->support_grouping) { |
Eliot Blennerhassett | a647713 | 2011-04-05 20:55:42 +1200 | [diff] [blame] | 620 | snd_printdd("%d group\n", s->number); |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 621 | e = hpi_stream_group_add( |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 622 | dpcm->h_stream, |
| 623 | ds->h_stream); |
| 624 | if (!e) { |
| 625 | snd_pcm_trigger_done(s, substream); |
| 626 | } else { |
| 627 | hpi_handle_error(e); |
| 628 | break; |
| 629 | } |
| 630 | } else |
| 631 | break; |
| 632 | } |
Eliot Blennerhassett | b2e65c8 | 2011-03-25 15:25:48 +1300 | [diff] [blame] | 633 | snd_printdd("start\n"); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 634 | /* start the master stream */ |
| 635 | snd_card_asihpi_pcm_timer_start(substream); |
Eliot Blennerhassett | c4ed97d | 2011-02-10 17:26:20 +1300 | [diff] [blame] | 636 | if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE) || |
Eliot Blennerhassett | f3d145a | 2011-04-05 20:55:44 +1200 | [diff] [blame] | 637 | !card->can_dma) |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 638 | hpi_handle_error(hpi_stream_start(dpcm->h_stream)); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 639 | break; |
| 640 | |
| 641 | case SNDRV_PCM_TRIGGER_STOP: |
| 642 | snd_card_asihpi_pcm_timer_stop(substream); |
| 643 | snd_pcm_group_for_each_entry(s, substream) { |
| 644 | if (snd_pcm_substream_chip(s) != card) |
| 645 | continue; |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 646 | /* don't link Cap and Play */ |
| 647 | if (substream->stream != s->stream) |
| 648 | continue; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 649 | |
| 650 | /*? workaround linked streams don't |
| 651 | transition to SETUP 20070706*/ |
| 652 | s->runtime->status->state = SNDRV_PCM_STATE_SETUP; |
| 653 | |
| 654 | if (card->support_grouping) { |
Eliot Blennerhassett | a647713 | 2011-04-05 20:55:42 +1200 | [diff] [blame] | 655 | snd_printdd("%d group\n", s->number); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 656 | snd_pcm_trigger_done(s, substream); |
| 657 | } else |
| 658 | break; |
| 659 | } |
Eliot Blennerhassett | b2e65c8 | 2011-03-25 15:25:48 +1300 | [diff] [blame] | 660 | snd_printdd("stop\n"); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 661 | |
| 662 | /* _prepare and _hwparams reset the stream */ |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 663 | hpi_handle_error(hpi_stream_stop(dpcm->h_stream)); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 664 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 665 | hpi_handle_error( |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 666 | hpi_outstream_reset(dpcm->h_stream)); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 667 | |
| 668 | if (card->support_grouping) |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 669 | hpi_handle_error(hpi_stream_group_reset(dpcm->h_stream)); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 670 | break; |
| 671 | |
| 672 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
Eliot Blennerhassett | b2e65c8 | 2011-03-25 15:25:48 +1300 | [diff] [blame] | 673 | snd_printdd("pause release\n"); |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 674 | hpi_handle_error(hpi_stream_start(dpcm->h_stream)); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 675 | snd_card_asihpi_pcm_timer_start(substream); |
| 676 | break; |
| 677 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
Eliot Blennerhassett | b2e65c8 | 2011-03-25 15:25:48 +1300 | [diff] [blame] | 678 | snd_printdd("pause\n"); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 679 | snd_card_asihpi_pcm_timer_stop(substream); |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 680 | hpi_handle_error(hpi_stream_stop(dpcm->h_stream)); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 681 | break; |
| 682 | default: |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 683 | snd_printd(KERN_ERR "\tINVALID\n"); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 684 | return -EINVAL; |
| 685 | } |
| 686 | |
| 687 | return 0; |
| 688 | } |
| 689 | |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 690 | /*algorithm outline |
| 691 | Without linking degenerates to getting single stream pos etc |
| 692 | Without mmap 2nd loop degenerates to snd_pcm_period_elapsed |
| 693 | */ |
| 694 | /* |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 695 | pcm_buf_dma_ofs=get_buf_pos(s); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 696 | for_each_linked_stream(s) { |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 697 | pcm_buf_dma_ofs=get_buf_pos(s); |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 698 | min_buf_pos = modulo_min(min_buf_pos, pcm_buf_dma_ofs, buffer_bytes) |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 699 | new_data = min(new_data, calc_new_data(pcm_buf_dma_ofs,irq_pos) |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 700 | } |
| 701 | timer.expires = jiffies + predict_next_period_ready(min_buf_pos); |
| 702 | for_each_linked_stream(s) { |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 703 | s->pcm_buf_dma_ofs = min_buf_pos; |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 704 | if (new_data > period_bytes) { |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 705 | if (mmap) { |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 706 | irq_pos = (irq_pos + period_bytes) % buffer_bytes; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 707 | if (playback) { |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 708 | write(period_bytes); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 709 | } else { |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 710 | read(period_bytes); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 711 | } |
| 712 | } |
| 713 | snd_pcm_period_elapsed(s); |
| 714 | } |
| 715 | } |
| 716 | */ |
| 717 | |
| 718 | /** Minimum of 2 modulo values. Works correctly when the difference between |
| 719 | * the values is less than half the modulus |
| 720 | */ |
| 721 | static inline unsigned int modulo_min(unsigned int a, unsigned int b, |
| 722 | unsigned long int modulus) |
| 723 | { |
| 724 | unsigned int result; |
| 725 | if (((a-b) % modulus) < (modulus/2)) |
| 726 | result = b; |
| 727 | else |
| 728 | result = a; |
| 729 | |
| 730 | return result; |
| 731 | } |
| 732 | |
| 733 | /** Timer function, equivalent to interrupt service routine for cards |
| 734 | */ |
| 735 | static void snd_card_asihpi_timer_function(unsigned long data) |
| 736 | { |
| 737 | struct snd_card_asihpi_pcm *dpcm = (struct snd_card_asihpi_pcm *)data; |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 738 | struct snd_pcm_substream *substream = dpcm->substream; |
| 739 | struct snd_card_asihpi *card = snd_pcm_substream_chip(substream); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 740 | struct snd_pcm_runtime *runtime; |
| 741 | struct snd_pcm_substream *s; |
| 742 | unsigned int newdata = 0; |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 743 | unsigned int pcm_buf_dma_ofs, min_buf_pos = 0; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 744 | unsigned int remdata, xfercount, next_jiffies; |
| 745 | int first = 1; |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 746 | int loops = 0; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 747 | u16 state; |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 748 | u32 buffer_size, bytes_avail, samples_played, on_card_bytes; |
Eliot Blennerhassett | 0a17e99 | 2011-07-22 15:52:44 +1200 | [diff] [blame] | 749 | char name[16]; |
| 750 | |
| 751 | snd_pcm_debug_name(substream, name, sizeof(name)); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 752 | |
Eliot Blennerhassett | a647713 | 2011-04-05 20:55:42 +1200 | [diff] [blame] | 753 | snd_printdd("%s snd_card_asihpi_timer_function\n", name); |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 754 | |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 755 | /* find minimum newdata and buffer pos in group */ |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 756 | snd_pcm_group_for_each_entry(s, substream) { |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 757 | struct snd_card_asihpi_pcm *ds = s->runtime->private_data; |
| 758 | runtime = s->runtime; |
| 759 | |
| 760 | if (snd_pcm_substream_chip(s) != card) |
| 761 | continue; |
| 762 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 763 | /* don't link Cap and Play */ |
| 764 | if (substream->stream != s->stream) |
| 765 | continue; |
| 766 | |
| 767 | hpi_handle_error(hpi_stream_get_info_ex( |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 768 | ds->h_stream, &state, |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 769 | &buffer_size, &bytes_avail, |
| 770 | &samples_played, &on_card_bytes)); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 771 | |
| 772 | /* number of bytes in on-card buffer */ |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 773 | runtime->delay = on_card_bytes; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 774 | |
Eliot Blennerhassett | f3d145a | 2011-04-05 20:55:44 +1200 | [diff] [blame] | 775 | if (!card->can_dma) |
| 776 | on_card_bytes = bytes_avail; |
| 777 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 778 | if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 779 | pcm_buf_dma_ofs = ds->pcm_buf_host_rw_ofs - bytes_avail; |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 780 | if (state == HPI_STATE_STOPPED) { |
| 781 | if ((bytes_avail == 0) && |
| 782 | (on_card_bytes < ds->pcm_buf_host_rw_ofs)) { |
| 783 | hpi_handle_error(hpi_stream_start(ds->h_stream)); |
Eliot Blennerhassett | b2e65c8 | 2011-03-25 15:25:48 +1300 | [diff] [blame] | 784 | snd_printdd("P%d start\n", s->number); |
Eliot Blennerhassett | 0b7ce9e | 2011-04-05 20:55:43 +1200 | [diff] [blame] | 785 | ds->drained_count = 0; |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 786 | } |
| 787 | } else if (state == HPI_STATE_DRAINED) { |
Eliot Blennerhassett | b2e65c8 | 2011-03-25 15:25:48 +1300 | [diff] [blame] | 788 | snd_printd(KERN_WARNING "P%d drained\n", |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 789 | s->number); |
Eliot Blennerhassett | 0b7ce9e | 2011-04-05 20:55:43 +1200 | [diff] [blame] | 790 | ds->drained_count++; |
| 791 | if (ds->drained_count > 2) { |
| 792 | snd_pcm_stop(s, SNDRV_PCM_STATE_XRUN); |
| 793 | continue; |
| 794 | } |
| 795 | } else { |
| 796 | ds->drained_count = 0; |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 797 | } |
| 798 | } else |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 799 | pcm_buf_dma_ofs = bytes_avail + ds->pcm_buf_host_rw_ofs; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 800 | |
| 801 | if (first) { |
| 802 | /* can't statically init min when wrap is involved */ |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 803 | min_buf_pos = pcm_buf_dma_ofs; |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 804 | newdata = (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 805 | first = 0; |
| 806 | } else { |
| 807 | min_buf_pos = |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 808 | modulo_min(min_buf_pos, pcm_buf_dma_ofs, UINT_MAX+1L); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 809 | newdata = min( |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 810 | (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 811 | newdata); |
| 812 | } |
| 813 | |
Eliot Blennerhassett | a647713 | 2011-04-05 20:55:42 +1200 | [diff] [blame] | 814 | snd_printdd("hw_ptr 0x%04lX, appl_ptr 0x%04lX\n", |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 815 | (unsigned long)frames_to_bytes(runtime, |
| 816 | runtime->status->hw_ptr), |
| 817 | (unsigned long)frames_to_bytes(runtime, |
| 818 | runtime->control->appl_ptr)); |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 819 | |
Eliot Blennerhassett | a647713 | 2011-04-05 20:55:42 +1200 | [diff] [blame] | 820 | snd_printdd("%d S=%d, " |
| 821 | "rw=0x%04X, dma=0x%04X, left=0x%04X, " |
| 822 | "aux=0x%04X space=0x%04X\n", |
| 823 | s->number, state, |
| 824 | ds->pcm_buf_host_rw_ofs, pcm_buf_dma_ofs, |
| 825 | (int)bytes_avail, |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 826 | (int)on_card_bytes, buffer_size-bytes_avail); |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 827 | loops++; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 828 | } |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 829 | pcm_buf_dma_ofs = min_buf_pos; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 830 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 831 | remdata = newdata % dpcm->period_bytes; |
| 832 | xfercount = newdata - remdata; /* a multiple of period_bytes */ |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 833 | /* come back when on_card_bytes has decreased enough to allow |
| 834 | write to happen, or when data has been consumed to make another |
| 835 | period |
| 836 | */ |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 837 | if (xfercount && (on_card_bytes > dpcm->period_bytes)) |
| 838 | next_jiffies = ((on_card_bytes - dpcm->period_bytes) * HZ / dpcm->bytes_per_sec); |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 839 | else |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 840 | next_jiffies = ((dpcm->period_bytes - remdata) * HZ / dpcm->bytes_per_sec); |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 841 | |
| 842 | next_jiffies = max(next_jiffies, 1U); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 843 | dpcm->timer.expires = jiffies + next_jiffies; |
Eliot Blennerhassett | a647713 | 2011-04-05 20:55:42 +1200 | [diff] [blame] | 844 | snd_printdd("jif %d buf pos 0x%04X newdata 0x%04X xfer 0x%04X\n", |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 845 | next_jiffies, pcm_buf_dma_ofs, newdata, xfercount); |
| 846 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 847 | snd_pcm_group_for_each_entry(s, substream) { |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 848 | struct snd_card_asihpi_pcm *ds = s->runtime->private_data; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 849 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 850 | /* don't link Cap and Play */ |
| 851 | if (substream->stream != s->stream) |
| 852 | continue; |
| 853 | |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 854 | ds->pcm_buf_dma_ofs = pcm_buf_dma_ofs; |
| 855 | |
Eliot Blennerhassett | f3d145a | 2011-04-05 20:55:44 +1200 | [diff] [blame] | 856 | if (xfercount && |
| 857 | /* Limit use of on card fifo for playback */ |
| 858 | ((on_card_bytes <= ds->period_bytes) || |
| 859 | (s->stream == SNDRV_PCM_STREAM_CAPTURE))) |
| 860 | |
| 861 | { |
| 862 | |
| 863 | unsigned int buf_ofs = ds->pcm_buf_host_rw_ofs % ds->buffer_bytes; |
| 864 | unsigned int xfer1, xfer2; |
| 865 | char *pd = &s->runtime->dma_area[buf_ofs]; |
| 866 | |
Eliot Blennerhassett | b0096a6 | 2011-04-05 20:55:46 +1200 | [diff] [blame] | 867 | if (card->can_dma) { /* buffer wrap is handled at lower level */ |
Eliot Blennerhassett | f3d145a | 2011-04-05 20:55:44 +1200 | [diff] [blame] | 868 | xfer1 = xfercount; |
| 869 | xfer2 = 0; |
| 870 | } else { |
| 871 | xfer1 = min(xfercount, ds->buffer_bytes - buf_ofs); |
| 872 | xfer2 = xfercount - xfer1; |
| 873 | } |
| 874 | |
| 875 | if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 876 | snd_printddd("P%d write1 0x%04X 0x%04X\n", |
| 877 | s->number, xfer1, buf_ofs); |
| 878 | hpi_handle_error( |
| 879 | hpi_outstream_write_buf( |
| 880 | ds->h_stream, pd, xfer1, |
| 881 | &ds->format)); |
| 882 | |
| 883 | if (xfer2) { |
| 884 | pd = s->runtime->dma_area; |
| 885 | |
| 886 | snd_printddd("P%d write2 0x%04X 0x%04X\n", |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 887 | s->number, |
Eliot Blennerhassett | f3d145a | 2011-04-05 20:55:44 +1200 | [diff] [blame] | 888 | xfercount - xfer1, buf_ofs); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 889 | hpi_handle_error( |
| 890 | hpi_outstream_write_buf( |
Eliot Blennerhassett | f3d145a | 2011-04-05 20:55:44 +1200 | [diff] [blame] | 891 | ds->h_stream, pd, |
| 892 | xfercount - xfer1, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 893 | &ds->format)); |
Eliot Blennerhassett | f3d145a | 2011-04-05 20:55:44 +1200 | [diff] [blame] | 894 | } |
| 895 | } else { |
| 896 | snd_printddd("C%d read1 0x%04x\n", |
| 897 | s->number, xfer1); |
| 898 | hpi_handle_error( |
| 899 | hpi_instream_read_buf( |
| 900 | ds->h_stream, |
| 901 | pd, xfer1)); |
| 902 | if (xfer2) { |
| 903 | pd = s->runtime->dma_area; |
| 904 | snd_printddd("C%d read2 0x%04x\n", |
| 905 | s->number, xfer2); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 906 | hpi_handle_error( |
| 907 | hpi_instream_read_buf( |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 908 | ds->h_stream, |
Eliot Blennerhassett | f3d145a | 2011-04-05 20:55:44 +1200 | [diff] [blame] | 909 | pd, xfer2)); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 910 | } |
Eliot Blennerhassett | f3d145a | 2011-04-05 20:55:44 +1200 | [diff] [blame] | 911 | } |
| 912 | ds->pcm_buf_host_rw_ofs = ds->pcm_buf_host_rw_ofs + xfercount; |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 913 | ds->pcm_buf_elapsed_dma_ofs = pcm_buf_dma_ofs; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 914 | snd_pcm_period_elapsed(s); |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | if (dpcm->respawn_timer) |
| 919 | add_timer(&dpcm->timer); |
| 920 | } |
| 921 | |
| 922 | /***************************** PLAYBACK OPS ****************/ |
| 923 | static int snd_card_asihpi_playback_ioctl(struct snd_pcm_substream *substream, |
| 924 | unsigned int cmd, void *arg) |
| 925 | { |
Eliot Blennerhassett | a647713 | 2011-04-05 20:55:42 +1200 | [diff] [blame] | 926 | snd_printddd(KERN_INFO "P%d ioctl %d\n", substream->number, cmd); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 927 | return snd_pcm_lib_ioctl(substream, cmd, arg); |
| 928 | } |
| 929 | |
| 930 | static int snd_card_asihpi_playback_prepare(struct snd_pcm_substream * |
| 931 | substream) |
| 932 | { |
| 933 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 934 | struct snd_card_asihpi_pcm *dpcm = runtime->private_data; |
| 935 | |
Eliot Blennerhassett | a647713 | 2011-04-05 20:55:42 +1200 | [diff] [blame] | 936 | snd_printdd("P%d prepare\n", substream->number); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 937 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 938 | hpi_handle_error(hpi_outstream_reset(dpcm->h_stream)); |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 939 | dpcm->pcm_buf_host_rw_ofs = 0; |
| 940 | dpcm->pcm_buf_dma_ofs = 0; |
| 941 | dpcm->pcm_buf_elapsed_dma_ofs = 0; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 942 | return 0; |
| 943 | } |
| 944 | |
| 945 | static snd_pcm_uframes_t |
| 946 | snd_card_asihpi_playback_pointer(struct snd_pcm_substream *substream) |
| 947 | { |
| 948 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 949 | struct snd_card_asihpi_pcm *dpcm = runtime->private_data; |
| 950 | snd_pcm_uframes_t ptr; |
| 951 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 952 | ptr = bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes); |
Eliot Blennerhassett | a647713 | 2011-04-05 20:55:42 +1200 | [diff] [blame] | 953 | snd_printddd("P%d pointer = 0x%04lx\n", substream->number, (unsigned long)ptr); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 954 | return ptr; |
| 955 | } |
| 956 | |
| 957 | static void snd_card_asihpi_playback_format(struct snd_card_asihpi *asihpi, |
| 958 | u32 h_stream, |
| 959 | struct snd_pcm_hardware *pcmhw) |
| 960 | { |
| 961 | struct hpi_format hpi_format; |
| 962 | u16 format; |
| 963 | u16 err; |
| 964 | u32 h_control; |
| 965 | u32 sample_rate = 48000; |
| 966 | |
| 967 | /* on cards without SRC, must query at valid rate, |
| 968 | * maybe set by external sync |
| 969 | */ |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 970 | err = hpi_mixer_get_control(asihpi->h_mixer, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 971 | HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0, |
| 972 | HPI_CONTROL_SAMPLECLOCK, &h_control); |
| 973 | |
| 974 | if (!err) |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 975 | err = hpi_sample_clock_get_sample_rate(h_control, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 976 | &sample_rate); |
| 977 | |
| 978 | for (format = HPI_FORMAT_PCM8_UNSIGNED; |
| 979 | format <= HPI_FORMAT_PCM24_SIGNED; format++) { |
| 980 | err = hpi_format_create(&hpi_format, |
| 981 | 2, format, sample_rate, 128000, 0); |
| 982 | if (!err) |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 983 | err = hpi_outstream_query_format(h_stream, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 984 | &hpi_format); |
| 985 | if (!err && (hpi_to_alsa_formats[format] != -1)) |
| 986 | pcmhw->formats |= |
| 987 | (1ULL << hpi_to_alsa_formats[format]); |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | static struct snd_pcm_hardware snd_card_asihpi_playback = { |
| 992 | .channels_min = 1, |
| 993 | .channels_max = 2, |
| 994 | .buffer_bytes_max = BUFFER_BYTES_MAX, |
| 995 | .period_bytes_min = PERIOD_BYTES_MIN, |
| 996 | .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN, |
| 997 | .periods_min = PERIODS_MIN, |
| 998 | .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN, |
| 999 | .fifo_size = 0, |
| 1000 | }; |
| 1001 | |
| 1002 | static int snd_card_asihpi_playback_open(struct snd_pcm_substream *substream) |
| 1003 | { |
| 1004 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 1005 | struct snd_card_asihpi_pcm *dpcm; |
| 1006 | struct snd_card_asihpi *card = snd_pcm_substream_chip(substream); |
| 1007 | int err; |
| 1008 | |
| 1009 | dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL); |
| 1010 | if (dpcm == NULL) |
| 1011 | return -ENOMEM; |
| 1012 | |
| 1013 | err = |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1014 | hpi_outstream_open(card->adapter_index, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1015 | substream->number, &dpcm->h_stream); |
| 1016 | hpi_handle_error(err); |
| 1017 | if (err) |
| 1018 | kfree(dpcm); |
| 1019 | if (err == HPI_ERROR_OBJ_ALREADY_OPEN) |
| 1020 | return -EBUSY; |
| 1021 | if (err) |
| 1022 | return -EIO; |
| 1023 | |
| 1024 | /*? also check ASI5000 samplerate source |
| 1025 | If external, only support external rate. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1026 | If internal and other stream playing, can't switch |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1027 | */ |
| 1028 | |
| 1029 | init_timer(&dpcm->timer); |
| 1030 | dpcm->timer.data = (unsigned long) dpcm; |
| 1031 | dpcm->timer.function = snd_card_asihpi_timer_function; |
| 1032 | dpcm->substream = substream; |
| 1033 | runtime->private_data = dpcm; |
| 1034 | runtime->private_free = snd_card_asihpi_runtime_free; |
| 1035 | |
| 1036 | snd_card_asihpi_playback.channels_max = card->out_max_chans; |
| 1037 | /*?snd_card_asihpi_playback.period_bytes_min = |
| 1038 | card->out_max_chans * 4096; */ |
| 1039 | |
| 1040 | snd_card_asihpi_playback_format(card, dpcm->h_stream, |
| 1041 | &snd_card_asihpi_playback); |
| 1042 | |
| 1043 | snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_playback); |
| 1044 | |
| 1045 | snd_card_asihpi_playback.info = SNDRV_PCM_INFO_INTERLEAVED | |
| 1046 | SNDRV_PCM_INFO_DOUBLE | |
| 1047 | SNDRV_PCM_INFO_BATCH | |
| 1048 | SNDRV_PCM_INFO_BLOCK_TRANSFER | |
Eliot Blennerhassett | f3d145a | 2011-04-05 20:55:44 +1200 | [diff] [blame] | 1049 | SNDRV_PCM_INFO_PAUSE | |
| 1050 | SNDRV_PCM_INFO_MMAP | |
| 1051 | SNDRV_PCM_INFO_MMAP_VALID; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1052 | |
| 1053 | if (card->support_grouping) |
| 1054 | snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_SYNC_START; |
| 1055 | |
| 1056 | /* struct is copied, so can create initializer dynamically */ |
| 1057 | runtime->hw = snd_card_asihpi_playback; |
| 1058 | |
Eliot Blennerhassett | f3d145a | 2011-04-05 20:55:44 +1200 | [diff] [blame] | 1059 | if (card->can_dma) |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1060 | err = snd_pcm_hw_constraint_pow2(runtime, 0, |
| 1061 | SNDRV_PCM_HW_PARAM_BUFFER_BYTES); |
| 1062 | if (err < 0) |
| 1063 | return err; |
| 1064 | |
| 1065 | snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, |
| 1066 | card->update_interval_frames); |
Eliot Blennerhassett | 26aebef | 2011-03-25 15:25:47 +1300 | [diff] [blame] | 1067 | |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1068 | snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 1069 | card->update_interval_frames * 2, UINT_MAX); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1070 | |
| 1071 | snd_pcm_set_sync(substream); |
| 1072 | |
Eliot Blennerhassett | b2e65c8 | 2011-03-25 15:25:48 +1300 | [diff] [blame] | 1073 | snd_printdd("playback open\n"); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1074 | |
| 1075 | return 0; |
| 1076 | } |
| 1077 | |
| 1078 | static int snd_card_asihpi_playback_close(struct snd_pcm_substream *substream) |
| 1079 | { |
| 1080 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 1081 | struct snd_card_asihpi_pcm *dpcm = runtime->private_data; |
| 1082 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1083 | hpi_handle_error(hpi_outstream_close(dpcm->h_stream)); |
Eliot Blennerhassett | b2e65c8 | 2011-03-25 15:25:48 +1300 | [diff] [blame] | 1084 | snd_printdd("playback close\n"); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1085 | |
| 1086 | return 0; |
| 1087 | } |
| 1088 | |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1089 | static struct snd_pcm_ops snd_card_asihpi_playback_mmap_ops = { |
| 1090 | .open = snd_card_asihpi_playback_open, |
| 1091 | .close = snd_card_asihpi_playback_close, |
| 1092 | .ioctl = snd_card_asihpi_playback_ioctl, |
| 1093 | .hw_params = snd_card_asihpi_pcm_hw_params, |
| 1094 | .hw_free = snd_card_asihpi_hw_free, |
| 1095 | .prepare = snd_card_asihpi_playback_prepare, |
| 1096 | .trigger = snd_card_asihpi_trigger, |
| 1097 | .pointer = snd_card_asihpi_playback_pointer, |
| 1098 | }; |
| 1099 | |
| 1100 | /***************************** CAPTURE OPS ****************/ |
| 1101 | static snd_pcm_uframes_t |
| 1102 | snd_card_asihpi_capture_pointer(struct snd_pcm_substream *substream) |
| 1103 | { |
| 1104 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 1105 | struct snd_card_asihpi_pcm *dpcm = runtime->private_data; |
| 1106 | |
Eliot Blennerhassett | b2e65c8 | 2011-03-25 15:25:48 +1300 | [diff] [blame] | 1107 | snd_printddd("capture pointer %d=%d\n", |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 1108 | substream->number, dpcm->pcm_buf_dma_ofs); |
| 1109 | /* NOTE Unlike playback can't use actual samples_played |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1110 | for the capture position, because those samples aren't yet in |
| 1111 | the local buffer available for reading. |
| 1112 | */ |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1113 | return bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | static int snd_card_asihpi_capture_ioctl(struct snd_pcm_substream *substream, |
| 1117 | unsigned int cmd, void *arg) |
| 1118 | { |
| 1119 | return snd_pcm_lib_ioctl(substream, cmd, arg); |
| 1120 | } |
| 1121 | |
| 1122 | static int snd_card_asihpi_capture_prepare(struct snd_pcm_substream *substream) |
| 1123 | { |
| 1124 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 1125 | struct snd_card_asihpi_pcm *dpcm = runtime->private_data; |
| 1126 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1127 | hpi_handle_error(hpi_instream_reset(dpcm->h_stream)); |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 1128 | dpcm->pcm_buf_host_rw_ofs = 0; |
| 1129 | dpcm->pcm_buf_dma_ofs = 0; |
| 1130 | dpcm->pcm_buf_elapsed_dma_ofs = 0; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1131 | |
Eliot Blennerhassett | b2e65c8 | 2011-03-25 15:25:48 +1300 | [diff] [blame] | 1132 | snd_printdd("Capture Prepare %d\n", substream->number); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1133 | return 0; |
| 1134 | } |
| 1135 | |
| 1136 | |
| 1137 | |
| 1138 | static void snd_card_asihpi_capture_format(struct snd_card_asihpi *asihpi, |
| 1139 | u32 h_stream, |
| 1140 | struct snd_pcm_hardware *pcmhw) |
| 1141 | { |
| 1142 | struct hpi_format hpi_format; |
| 1143 | u16 format; |
| 1144 | u16 err; |
| 1145 | u32 h_control; |
| 1146 | u32 sample_rate = 48000; |
| 1147 | |
| 1148 | /* on cards without SRC, must query at valid rate, |
| 1149 | maybe set by external sync */ |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1150 | err = hpi_mixer_get_control(asihpi->h_mixer, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1151 | HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0, |
| 1152 | HPI_CONTROL_SAMPLECLOCK, &h_control); |
| 1153 | |
| 1154 | if (!err) |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1155 | err = hpi_sample_clock_get_sample_rate(h_control, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1156 | &sample_rate); |
| 1157 | |
| 1158 | for (format = HPI_FORMAT_PCM8_UNSIGNED; |
| 1159 | format <= HPI_FORMAT_PCM24_SIGNED; format++) { |
| 1160 | |
| 1161 | err = hpi_format_create(&hpi_format, 2, format, |
| 1162 | sample_rate, 128000, 0); |
| 1163 | if (!err) |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1164 | err = hpi_instream_query_format(h_stream, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1165 | &hpi_format); |
| 1166 | if (!err) |
| 1167 | pcmhw->formats |= |
| 1168 | (1ULL << hpi_to_alsa_formats[format]); |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | |
| 1173 | static struct snd_pcm_hardware snd_card_asihpi_capture = { |
| 1174 | .channels_min = 1, |
| 1175 | .channels_max = 2, |
| 1176 | .buffer_bytes_max = BUFFER_BYTES_MAX, |
| 1177 | .period_bytes_min = PERIOD_BYTES_MIN, |
| 1178 | .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN, |
| 1179 | .periods_min = PERIODS_MIN, |
| 1180 | .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN, |
| 1181 | .fifo_size = 0, |
| 1182 | }; |
| 1183 | |
| 1184 | static int snd_card_asihpi_capture_open(struct snd_pcm_substream *substream) |
| 1185 | { |
| 1186 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 1187 | struct snd_card_asihpi *card = snd_pcm_substream_chip(substream); |
| 1188 | struct snd_card_asihpi_pcm *dpcm; |
| 1189 | int err; |
| 1190 | |
| 1191 | dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL); |
| 1192 | if (dpcm == NULL) |
| 1193 | return -ENOMEM; |
| 1194 | |
Eliot Blennerhassett | b2e65c8 | 2011-03-25 15:25:48 +1300 | [diff] [blame] | 1195 | snd_printdd("capture open adapter %d stream %d\n", |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1196 | card->adapter_index, substream->number); |
| 1197 | |
| 1198 | err = hpi_handle_error( |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1199 | hpi_instream_open(card->adapter_index, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1200 | substream->number, &dpcm->h_stream)); |
| 1201 | if (err) |
| 1202 | kfree(dpcm); |
| 1203 | if (err == HPI_ERROR_OBJ_ALREADY_OPEN) |
| 1204 | return -EBUSY; |
| 1205 | if (err) |
| 1206 | return -EIO; |
| 1207 | |
| 1208 | |
| 1209 | init_timer(&dpcm->timer); |
| 1210 | dpcm->timer.data = (unsigned long) dpcm; |
| 1211 | dpcm->timer.function = snd_card_asihpi_timer_function; |
| 1212 | dpcm->substream = substream; |
| 1213 | runtime->private_data = dpcm; |
| 1214 | runtime->private_free = snd_card_asihpi_runtime_free; |
| 1215 | |
| 1216 | snd_card_asihpi_capture.channels_max = card->in_max_chans; |
| 1217 | snd_card_asihpi_capture_format(card, dpcm->h_stream, |
| 1218 | &snd_card_asihpi_capture); |
| 1219 | snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_capture); |
Eliot Blennerhassett | f3d145a | 2011-04-05 20:55:44 +1200 | [diff] [blame] | 1220 | snd_card_asihpi_capture.info = SNDRV_PCM_INFO_INTERLEAVED | |
| 1221 | SNDRV_PCM_INFO_MMAP | |
| 1222 | SNDRV_PCM_INFO_MMAP_VALID; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1223 | |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 1224 | if (card->support_grouping) |
| 1225 | snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_SYNC_START; |
| 1226 | |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1227 | runtime->hw = snd_card_asihpi_capture; |
| 1228 | |
Eliot Blennerhassett | f3d145a | 2011-04-05 20:55:44 +1200 | [diff] [blame] | 1229 | if (card->can_dma) |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1230 | err = snd_pcm_hw_constraint_pow2(runtime, 0, |
| 1231 | SNDRV_PCM_HW_PARAM_BUFFER_BYTES); |
| 1232 | if (err < 0) |
| 1233 | return err; |
| 1234 | |
| 1235 | snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, |
| 1236 | card->update_interval_frames); |
| 1237 | snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, |
| 1238 | card->update_interval_frames * 2, UINT_MAX); |
| 1239 | |
| 1240 | snd_pcm_set_sync(substream); |
| 1241 | |
| 1242 | return 0; |
| 1243 | } |
| 1244 | |
| 1245 | static int snd_card_asihpi_capture_close(struct snd_pcm_substream *substream) |
| 1246 | { |
| 1247 | struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data; |
| 1248 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1249 | hpi_handle_error(hpi_instream_close(dpcm->h_stream)); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1250 | return 0; |
| 1251 | } |
| 1252 | |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1253 | static struct snd_pcm_ops snd_card_asihpi_capture_mmap_ops = { |
| 1254 | .open = snd_card_asihpi_capture_open, |
| 1255 | .close = snd_card_asihpi_capture_close, |
| 1256 | .ioctl = snd_card_asihpi_capture_ioctl, |
| 1257 | .hw_params = snd_card_asihpi_pcm_hw_params, |
| 1258 | .hw_free = snd_card_asihpi_hw_free, |
| 1259 | .prepare = snd_card_asihpi_capture_prepare, |
| 1260 | .trigger = snd_card_asihpi_trigger, |
| 1261 | .pointer = snd_card_asihpi_capture_pointer, |
| 1262 | }; |
| 1263 | |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1264 | static int __devinit snd_card_asihpi_pcm_new(struct snd_card_asihpi *asihpi, |
| 1265 | int device, int substreams) |
| 1266 | { |
| 1267 | struct snd_pcm *pcm; |
| 1268 | int err; |
| 1269 | |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 1270 | err = snd_pcm_new(asihpi->card, "Asihpi PCM", device, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1271 | asihpi->num_outstreams, asihpi->num_instreams, |
| 1272 | &pcm); |
| 1273 | if (err < 0) |
| 1274 | return err; |
| 1275 | /* pointer to ops struct is stored, dont change ops afterwards! */ |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1276 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, |
| 1277 | &snd_card_asihpi_playback_mmap_ops); |
| 1278 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, |
| 1279 | &snd_card_asihpi_capture_mmap_ops); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1280 | |
| 1281 | pcm->private_data = asihpi; |
| 1282 | pcm->info_flags = 0; |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 1283 | strcpy(pcm->name, "Asihpi PCM"); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1284 | |
| 1285 | /*? do we want to emulate MMAP for non-BBM cards? |
| 1286 | Jack doesn't work with ALSAs MMAP emulation - WHY NOT? */ |
| 1287 | snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, |
| 1288 | snd_dma_pci_data(asihpi->pci), |
| 1289 | 64*1024, BUFFER_BYTES_MAX); |
| 1290 | |
| 1291 | return 0; |
| 1292 | } |
| 1293 | |
| 1294 | /***************************** MIXER CONTROLS ****************/ |
| 1295 | struct hpi_control { |
| 1296 | u32 h_control; |
| 1297 | u16 control_type; |
| 1298 | u16 src_node_type; |
| 1299 | u16 src_node_index; |
| 1300 | u16 dst_node_type; |
| 1301 | u16 dst_node_index; |
| 1302 | u16 band; |
| 1303 | char name[44]; /* copied to snd_ctl_elem_id.name[44]; */ |
| 1304 | }; |
| 1305 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1306 | static const char * const asihpi_tuner_band_names[] = { |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1307 | "invalid", |
| 1308 | "AM", |
| 1309 | "FM mono", |
| 1310 | "TV NTSC-M", |
| 1311 | "FM stereo", |
| 1312 | "AUX", |
| 1313 | "TV PAL BG", |
| 1314 | "TV PAL I", |
| 1315 | "TV PAL DK", |
| 1316 | "TV SECAM", |
| 1317 | }; |
| 1318 | |
| 1319 | compile_time_assert( |
| 1320 | (ARRAY_SIZE(asihpi_tuner_band_names) == |
| 1321 | (HPI_TUNER_BAND_LAST+1)), |
| 1322 | assert_tuner_band_names_size); |
| 1323 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1324 | static const char * const asihpi_src_names[] = { |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1325 | "no source", |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 1326 | "PCM", |
| 1327 | "Line", |
| 1328 | "Digital", |
| 1329 | "Tuner", |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1330 | "RF", |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 1331 | "Clock", |
| 1332 | "Bitstream", |
Eliot Blennerhassett | c830613 | 2011-07-22 15:53:00 +1200 | [diff] [blame] | 1333 | "Mic", |
| 1334 | "Net", |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 1335 | "Analog", |
| 1336 | "Adapter", |
Eliot Blennerhassett | c830613 | 2011-07-22 15:53:00 +1200 | [diff] [blame] | 1337 | "RTP", |
| 1338 | "GPI", |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1339 | }; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1340 | |
| 1341 | compile_time_assert( |
| 1342 | (ARRAY_SIZE(asihpi_src_names) == |
Eliot Blennerhassett | 168f1b0 | 2010-07-06 08:37:06 +1200 | [diff] [blame] | 1343 | (HPI_SOURCENODE_LAST_INDEX-HPI_SOURCENODE_NONE+1)), |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1344 | assert_src_names_size); |
| 1345 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1346 | static const char * const asihpi_dst_names[] = { |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1347 | "no destination", |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 1348 | "PCM", |
| 1349 | "Line", |
| 1350 | "Digital", |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1351 | "RF", |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 1352 | "Speaker", |
Eliot Blennerhassett | c830613 | 2011-07-22 15:53:00 +1200 | [diff] [blame] | 1353 | "Net", |
| 1354 | "Analog", |
| 1355 | "RTP", |
| 1356 | "GPO", |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1357 | }; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1358 | |
| 1359 | compile_time_assert( |
| 1360 | (ARRAY_SIZE(asihpi_dst_names) == |
Eliot Blennerhassett | 168f1b0 | 2010-07-06 08:37:06 +1200 | [diff] [blame] | 1361 | (HPI_DESTNODE_LAST_INDEX-HPI_DESTNODE_NONE+1)), |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1362 | assert_dst_names_size); |
| 1363 | |
| 1364 | static inline int ctl_add(struct snd_card *card, struct snd_kcontrol_new *ctl, |
| 1365 | struct snd_card_asihpi *asihpi) |
| 1366 | { |
| 1367 | int err; |
| 1368 | |
| 1369 | err = snd_ctl_add(card, snd_ctl_new1(ctl, asihpi)); |
| 1370 | if (err < 0) |
| 1371 | return err; |
| 1372 | else if (mixer_dump) |
| 1373 | snd_printk(KERN_INFO "added %s(%d)\n", ctl->name, ctl->index); |
| 1374 | |
| 1375 | return 0; |
| 1376 | } |
| 1377 | |
| 1378 | /* Convert HPI control name and location into ALSA control name */ |
| 1379 | static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control, |
| 1380 | struct hpi_control *hpi_ctl, |
| 1381 | char *name) |
| 1382 | { |
Eliot Blennerhassett | 550ac6b | 2011-04-05 20:55:41 +1200 | [diff] [blame] | 1383 | char *dir; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1384 | memset(snd_control, 0, sizeof(*snd_control)); |
| 1385 | snd_control->name = hpi_ctl->name; |
| 1386 | snd_control->private_value = hpi_ctl->h_control; |
| 1387 | snd_control->iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
| 1388 | snd_control->index = 0; |
| 1389 | |
Eliot Blennerhassett | 550ac6b | 2011-04-05 20:55:41 +1200 | [diff] [blame] | 1390 | if (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE == HPI_SOURCENODE_CLOCK_SOURCE) |
| 1391 | dir = ""; /* clock is neither capture nor playback */ |
| 1392 | else if (hpi_ctl->dst_node_type + HPI_DESTNODE_NONE == HPI_DESTNODE_ISTREAM) |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 1393 | dir = "Capture "; /* On or towards a PCM capture destination*/ |
| 1394 | else if ((hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) && |
| 1395 | (!hpi_ctl->dst_node_type)) |
| 1396 | dir = "Capture "; /* On a source node that is not PCM playback */ |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1397 | else if (hpi_ctl->src_node_type && |
| 1398 | (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) && |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 1399 | (hpi_ctl->dst_node_type)) |
| 1400 | dir = "Monitor Playback "; /* Between an input and an output */ |
| 1401 | else |
| 1402 | dir = "Playback "; /* PCM Playback source, or output node */ |
| 1403 | |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1404 | if (hpi_ctl->src_node_type && hpi_ctl->dst_node_type) |
Eliot Blennerhassett | 550ac6b | 2011-04-05 20:55:41 +1200 | [diff] [blame] | 1405 | sprintf(hpi_ctl->name, "%s %d %s %d %s%s", |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1406 | asihpi_src_names[hpi_ctl->src_node_type], |
| 1407 | hpi_ctl->src_node_index, |
| 1408 | asihpi_dst_names[hpi_ctl->dst_node_type], |
| 1409 | hpi_ctl->dst_node_index, |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 1410 | dir, name); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1411 | else if (hpi_ctl->dst_node_type) { |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 1412 | sprintf(hpi_ctl->name, "%s %d %s%s", |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1413 | asihpi_dst_names[hpi_ctl->dst_node_type], |
| 1414 | hpi_ctl->dst_node_index, |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 1415 | dir, name); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1416 | } else { |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 1417 | sprintf(hpi_ctl->name, "%s %d %s%s", |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1418 | asihpi_src_names[hpi_ctl->src_node_type], |
| 1419 | hpi_ctl->src_node_index, |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 1420 | dir, name); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1421 | } |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1422 | /* printk(KERN_INFO "Adding %s %d to %d ", hpi_ctl->name, |
| 1423 | hpi_ctl->wSrcNodeType, hpi_ctl->wDstNodeType); */ |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1424 | } |
| 1425 | |
| 1426 | /*------------------------------------------------------------ |
| 1427 | Volume controls |
| 1428 | ------------------------------------------------------------*/ |
| 1429 | #define VOL_STEP_mB 1 |
| 1430 | static int snd_asihpi_volume_info(struct snd_kcontrol *kcontrol, |
| 1431 | struct snd_ctl_elem_info *uinfo) |
| 1432 | { |
| 1433 | u32 h_control = kcontrol->private_value; |
| 1434 | u16 err; |
| 1435 | /* native gains are in millibels */ |
| 1436 | short min_gain_mB; |
| 1437 | short max_gain_mB; |
| 1438 | short step_gain_mB; |
| 1439 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1440 | err = hpi_volume_query_range(h_control, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1441 | &min_gain_mB, &max_gain_mB, &step_gain_mB); |
| 1442 | if (err) { |
| 1443 | max_gain_mB = 0; |
| 1444 | min_gain_mB = -10000; |
| 1445 | step_gain_mB = VOL_STEP_mB; |
| 1446 | } |
| 1447 | |
| 1448 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 1449 | uinfo->count = 2; |
| 1450 | uinfo->value.integer.min = min_gain_mB / VOL_STEP_mB; |
| 1451 | uinfo->value.integer.max = max_gain_mB / VOL_STEP_mB; |
| 1452 | uinfo->value.integer.step = step_gain_mB / VOL_STEP_mB; |
| 1453 | return 0; |
| 1454 | } |
| 1455 | |
| 1456 | static int snd_asihpi_volume_get(struct snd_kcontrol *kcontrol, |
| 1457 | struct snd_ctl_elem_value *ucontrol) |
| 1458 | { |
| 1459 | u32 h_control = kcontrol->private_value; |
| 1460 | short an_gain_mB[HPI_MAX_CHANNELS]; |
| 1461 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1462 | hpi_handle_error(hpi_volume_get_gain(h_control, an_gain_mB)); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1463 | ucontrol->value.integer.value[0] = an_gain_mB[0] / VOL_STEP_mB; |
| 1464 | ucontrol->value.integer.value[1] = an_gain_mB[1] / VOL_STEP_mB; |
| 1465 | |
| 1466 | return 0; |
| 1467 | } |
| 1468 | |
| 1469 | static int snd_asihpi_volume_put(struct snd_kcontrol *kcontrol, |
| 1470 | struct snd_ctl_elem_value *ucontrol) |
| 1471 | { |
| 1472 | int change; |
| 1473 | u32 h_control = kcontrol->private_value; |
| 1474 | short an_gain_mB[HPI_MAX_CHANNELS]; |
| 1475 | |
| 1476 | an_gain_mB[0] = |
| 1477 | (ucontrol->value.integer.value[0]) * VOL_STEP_mB; |
| 1478 | an_gain_mB[1] = |
| 1479 | (ucontrol->value.integer.value[1]) * VOL_STEP_mB; |
| 1480 | /* change = asihpi->mixer_volume[addr][0] != left || |
| 1481 | asihpi->mixer_volume[addr][1] != right; |
| 1482 | */ |
| 1483 | change = 1; |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1484 | hpi_handle_error(hpi_volume_set_gain(h_control, an_gain_mB)); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1485 | return change; |
| 1486 | } |
| 1487 | |
| 1488 | static const DECLARE_TLV_DB_SCALE(db_scale_100, -10000, VOL_STEP_mB, 0); |
| 1489 | |
Eliot Blennerhassett | fe0aa88e | 2011-07-22 15:53:03 +1200 | [diff] [blame^] | 1490 | static int snd_asihpi_volume_mute_info(struct snd_kcontrol *kcontrol, |
| 1491 | struct snd_ctl_elem_info *uinfo) |
| 1492 | { |
| 1493 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 1494 | uinfo->count = 1; |
| 1495 | uinfo->value.integer.min = 0; |
| 1496 | uinfo->value.integer.max = 1; |
| 1497 | return 0; |
| 1498 | } |
| 1499 | |
| 1500 | static int snd_asihpi_volume_mute_get(struct snd_kcontrol *kcontrol, |
| 1501 | struct snd_ctl_elem_value *ucontrol) |
| 1502 | { |
| 1503 | u32 h_control = kcontrol->private_value; |
| 1504 | u32 mute; |
| 1505 | |
| 1506 | hpi_handle_error(hpi_volume_get_mute(h_control, &mute)); |
| 1507 | ucontrol->value.integer.value[0] = mute ? 0 : 1; |
| 1508 | |
| 1509 | return 0; |
| 1510 | } |
| 1511 | |
| 1512 | static int snd_asihpi_volume_mute_put(struct snd_kcontrol *kcontrol, |
| 1513 | struct snd_ctl_elem_value *ucontrol) |
| 1514 | { |
| 1515 | u32 h_control = kcontrol->private_value; |
| 1516 | int change = 1; |
| 1517 | /* HPI currently only supports all or none muting of multichannel volume |
| 1518 | ALSA Switch element has opposite sense to HPI mute: on==unmuted, off=muted |
| 1519 | */ |
| 1520 | int mute = ucontrol->value.integer.value[0] ? 0 : HPI_BITMASK_ALL_CHANNELS; |
| 1521 | hpi_handle_error(hpi_volume_set_mute(h_control, mute)); |
| 1522 | return change; |
| 1523 | } |
| 1524 | |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1525 | static int __devinit snd_asihpi_volume_add(struct snd_card_asihpi *asihpi, |
| 1526 | struct hpi_control *hpi_ctl) |
| 1527 | { |
| 1528 | struct snd_card *card = asihpi->card; |
| 1529 | struct snd_kcontrol_new snd_control; |
Eliot Blennerhassett | fe0aa88e | 2011-07-22 15:53:03 +1200 | [diff] [blame^] | 1530 | int err; |
| 1531 | u32 mute; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1532 | |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 1533 | asihpi_ctl_init(&snd_control, hpi_ctl, "Volume"); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1534 | snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | |
| 1535 | SNDRV_CTL_ELEM_ACCESS_TLV_READ; |
| 1536 | snd_control.info = snd_asihpi_volume_info; |
| 1537 | snd_control.get = snd_asihpi_volume_get; |
| 1538 | snd_control.put = snd_asihpi_volume_put; |
| 1539 | snd_control.tlv.p = db_scale_100; |
| 1540 | |
Eliot Blennerhassett | fe0aa88e | 2011-07-22 15:53:03 +1200 | [diff] [blame^] | 1541 | err = ctl_add(card, &snd_control, asihpi); |
| 1542 | if (err) |
| 1543 | return err; |
| 1544 | |
| 1545 | if (hpi_volume_get_mute(hpi_ctl->h_control, &mute) == 0) { |
| 1546 | asihpi_ctl_init(&snd_control, hpi_ctl, "Switch"); |
| 1547 | snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE; |
| 1548 | snd_control.info = snd_asihpi_volume_mute_info; |
| 1549 | snd_control.get = snd_asihpi_volume_mute_get; |
| 1550 | snd_control.put = snd_asihpi_volume_mute_put; |
| 1551 | err = ctl_add(card, &snd_control, asihpi); |
| 1552 | } |
| 1553 | return err; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | /*------------------------------------------------------------ |
| 1557 | Level controls |
| 1558 | ------------------------------------------------------------*/ |
| 1559 | static int snd_asihpi_level_info(struct snd_kcontrol *kcontrol, |
| 1560 | struct snd_ctl_elem_info *uinfo) |
| 1561 | { |
| 1562 | u32 h_control = kcontrol->private_value; |
| 1563 | u16 err; |
| 1564 | short min_gain_mB; |
| 1565 | short max_gain_mB; |
| 1566 | short step_gain_mB; |
| 1567 | |
| 1568 | err = |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1569 | hpi_level_query_range(h_control, &min_gain_mB, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1570 | &max_gain_mB, &step_gain_mB); |
| 1571 | if (err) { |
| 1572 | max_gain_mB = 2400; |
| 1573 | min_gain_mB = -1000; |
| 1574 | step_gain_mB = 100; |
| 1575 | } |
| 1576 | |
| 1577 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 1578 | uinfo->count = 2; |
| 1579 | uinfo->value.integer.min = min_gain_mB / HPI_UNITS_PER_dB; |
| 1580 | uinfo->value.integer.max = max_gain_mB / HPI_UNITS_PER_dB; |
| 1581 | uinfo->value.integer.step = step_gain_mB / HPI_UNITS_PER_dB; |
| 1582 | return 0; |
| 1583 | } |
| 1584 | |
| 1585 | static int snd_asihpi_level_get(struct snd_kcontrol *kcontrol, |
| 1586 | struct snd_ctl_elem_value *ucontrol) |
| 1587 | { |
| 1588 | u32 h_control = kcontrol->private_value; |
| 1589 | short an_gain_mB[HPI_MAX_CHANNELS]; |
| 1590 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1591 | hpi_handle_error(hpi_level_get_gain(h_control, an_gain_mB)); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1592 | ucontrol->value.integer.value[0] = |
| 1593 | an_gain_mB[0] / HPI_UNITS_PER_dB; |
| 1594 | ucontrol->value.integer.value[1] = |
| 1595 | an_gain_mB[1] / HPI_UNITS_PER_dB; |
| 1596 | |
| 1597 | return 0; |
| 1598 | } |
| 1599 | |
| 1600 | static int snd_asihpi_level_put(struct snd_kcontrol *kcontrol, |
| 1601 | struct snd_ctl_elem_value *ucontrol) |
| 1602 | { |
| 1603 | int change; |
| 1604 | u32 h_control = kcontrol->private_value; |
| 1605 | short an_gain_mB[HPI_MAX_CHANNELS]; |
| 1606 | |
| 1607 | an_gain_mB[0] = |
| 1608 | (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB; |
| 1609 | an_gain_mB[1] = |
| 1610 | (ucontrol->value.integer.value[1]) * HPI_UNITS_PER_dB; |
| 1611 | /* change = asihpi->mixer_level[addr][0] != left || |
| 1612 | asihpi->mixer_level[addr][1] != right; |
| 1613 | */ |
| 1614 | change = 1; |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1615 | hpi_handle_error(hpi_level_set_gain(h_control, an_gain_mB)); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1616 | return change; |
| 1617 | } |
| 1618 | |
| 1619 | static const DECLARE_TLV_DB_SCALE(db_scale_level, -1000, 100, 0); |
| 1620 | |
| 1621 | static int __devinit snd_asihpi_level_add(struct snd_card_asihpi *asihpi, |
| 1622 | struct hpi_control *hpi_ctl) |
| 1623 | { |
| 1624 | struct snd_card *card = asihpi->card; |
| 1625 | struct snd_kcontrol_new snd_control; |
| 1626 | |
| 1627 | /* can't use 'volume' cos some nodes have volume as well */ |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 1628 | asihpi_ctl_init(&snd_control, hpi_ctl, "Level"); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1629 | snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | |
| 1630 | SNDRV_CTL_ELEM_ACCESS_TLV_READ; |
| 1631 | snd_control.info = snd_asihpi_level_info; |
| 1632 | snd_control.get = snd_asihpi_level_get; |
| 1633 | snd_control.put = snd_asihpi_level_put; |
| 1634 | snd_control.tlv.p = db_scale_level; |
| 1635 | |
| 1636 | return ctl_add(card, &snd_control, asihpi); |
| 1637 | } |
| 1638 | |
| 1639 | /*------------------------------------------------------------ |
| 1640 | AESEBU controls |
| 1641 | ------------------------------------------------------------*/ |
| 1642 | |
| 1643 | /* AESEBU format */ |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1644 | static const char * const asihpi_aesebu_format_names[] = { |
| 1645 | "N/A", "S/PDIF", "AES/EBU" }; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1646 | |
| 1647 | static int snd_asihpi_aesebu_format_info(struct snd_kcontrol *kcontrol, |
| 1648 | struct snd_ctl_elem_info *uinfo) |
| 1649 | { |
| 1650 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 1651 | uinfo->count = 1; |
| 1652 | uinfo->value.enumerated.items = 3; |
| 1653 | |
| 1654 | if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items) |
| 1655 | uinfo->value.enumerated.item = |
| 1656 | uinfo->value.enumerated.items - 1; |
| 1657 | |
| 1658 | strcpy(uinfo->value.enumerated.name, |
| 1659 | asihpi_aesebu_format_names[uinfo->value.enumerated.item]); |
| 1660 | |
| 1661 | return 0; |
| 1662 | } |
| 1663 | |
| 1664 | static int snd_asihpi_aesebu_format_get(struct snd_kcontrol *kcontrol, |
| 1665 | struct snd_ctl_elem_value *ucontrol, |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1666 | u16 (*func)(u32, u16 *)) |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1667 | { |
| 1668 | u32 h_control = kcontrol->private_value; |
| 1669 | u16 source, err; |
| 1670 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1671 | err = func(h_control, &source); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1672 | |
| 1673 | /* default to N/A */ |
| 1674 | ucontrol->value.enumerated.item[0] = 0; |
| 1675 | /* return success but set the control to N/A */ |
| 1676 | if (err) |
| 1677 | return 0; |
| 1678 | if (source == HPI_AESEBU_FORMAT_SPDIF) |
| 1679 | ucontrol->value.enumerated.item[0] = 1; |
| 1680 | if (source == HPI_AESEBU_FORMAT_AESEBU) |
| 1681 | ucontrol->value.enumerated.item[0] = 2; |
| 1682 | |
| 1683 | return 0; |
| 1684 | } |
| 1685 | |
| 1686 | static int snd_asihpi_aesebu_format_put(struct snd_kcontrol *kcontrol, |
| 1687 | struct snd_ctl_elem_value *ucontrol, |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1688 | u16 (*func)(u32, u16)) |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1689 | { |
| 1690 | u32 h_control = kcontrol->private_value; |
| 1691 | |
| 1692 | /* default to S/PDIF */ |
| 1693 | u16 source = HPI_AESEBU_FORMAT_SPDIF; |
| 1694 | |
| 1695 | if (ucontrol->value.enumerated.item[0] == 1) |
| 1696 | source = HPI_AESEBU_FORMAT_SPDIF; |
| 1697 | if (ucontrol->value.enumerated.item[0] == 2) |
| 1698 | source = HPI_AESEBU_FORMAT_AESEBU; |
| 1699 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1700 | if (func(h_control, source) != 0) |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1701 | return -EINVAL; |
| 1702 | |
| 1703 | return 1; |
| 1704 | } |
| 1705 | |
| 1706 | static int snd_asihpi_aesebu_rx_format_get(struct snd_kcontrol *kcontrol, |
| 1707 | struct snd_ctl_elem_value *ucontrol) { |
| 1708 | return snd_asihpi_aesebu_format_get(kcontrol, ucontrol, |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1709 | hpi_aesebu_receiver_get_format); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | static int snd_asihpi_aesebu_rx_format_put(struct snd_kcontrol *kcontrol, |
| 1713 | struct snd_ctl_elem_value *ucontrol) { |
| 1714 | return snd_asihpi_aesebu_format_put(kcontrol, ucontrol, |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1715 | hpi_aesebu_receiver_set_format); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | static int snd_asihpi_aesebu_rxstatus_info(struct snd_kcontrol *kcontrol, |
| 1719 | struct snd_ctl_elem_info *uinfo) |
| 1720 | { |
| 1721 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 1722 | uinfo->count = 1; |
| 1723 | |
| 1724 | uinfo->value.integer.min = 0; |
| 1725 | uinfo->value.integer.max = 0X1F; |
| 1726 | uinfo->value.integer.step = 1; |
| 1727 | |
| 1728 | return 0; |
| 1729 | } |
| 1730 | |
| 1731 | static int snd_asihpi_aesebu_rxstatus_get(struct snd_kcontrol *kcontrol, |
| 1732 | struct snd_ctl_elem_value *ucontrol) { |
| 1733 | |
| 1734 | u32 h_control = kcontrol->private_value; |
| 1735 | u16 status; |
| 1736 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1737 | hpi_handle_error(hpi_aesebu_receiver_get_error_status( |
| 1738 | h_control, &status)); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1739 | ucontrol->value.integer.value[0] = status; |
| 1740 | return 0; |
| 1741 | } |
| 1742 | |
| 1743 | static int __devinit snd_asihpi_aesebu_rx_add(struct snd_card_asihpi *asihpi, |
| 1744 | struct hpi_control *hpi_ctl) |
| 1745 | { |
| 1746 | struct snd_card *card = asihpi->card; |
| 1747 | struct snd_kcontrol_new snd_control; |
| 1748 | |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 1749 | asihpi_ctl_init(&snd_control, hpi_ctl, "Format"); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1750 | snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE; |
| 1751 | snd_control.info = snd_asihpi_aesebu_format_info; |
| 1752 | snd_control.get = snd_asihpi_aesebu_rx_format_get; |
| 1753 | snd_control.put = snd_asihpi_aesebu_rx_format_put; |
| 1754 | |
| 1755 | |
| 1756 | if (ctl_add(card, &snd_control, asihpi) < 0) |
| 1757 | return -EINVAL; |
| 1758 | |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 1759 | asihpi_ctl_init(&snd_control, hpi_ctl, "Status"); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1760 | snd_control.access = |
| 1761 | SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ; |
| 1762 | snd_control.info = snd_asihpi_aesebu_rxstatus_info; |
| 1763 | snd_control.get = snd_asihpi_aesebu_rxstatus_get; |
| 1764 | |
| 1765 | return ctl_add(card, &snd_control, asihpi); |
| 1766 | } |
| 1767 | |
| 1768 | static int snd_asihpi_aesebu_tx_format_get(struct snd_kcontrol *kcontrol, |
| 1769 | struct snd_ctl_elem_value *ucontrol) { |
| 1770 | return snd_asihpi_aesebu_format_get(kcontrol, ucontrol, |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1771 | hpi_aesebu_transmitter_get_format); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1772 | } |
| 1773 | |
| 1774 | static int snd_asihpi_aesebu_tx_format_put(struct snd_kcontrol *kcontrol, |
| 1775 | struct snd_ctl_elem_value *ucontrol) { |
| 1776 | return snd_asihpi_aesebu_format_put(kcontrol, ucontrol, |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1777 | hpi_aesebu_transmitter_set_format); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1778 | } |
| 1779 | |
| 1780 | |
| 1781 | static int __devinit snd_asihpi_aesebu_tx_add(struct snd_card_asihpi *asihpi, |
| 1782 | struct hpi_control *hpi_ctl) |
| 1783 | { |
| 1784 | struct snd_card *card = asihpi->card; |
| 1785 | struct snd_kcontrol_new snd_control; |
| 1786 | |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 1787 | asihpi_ctl_init(&snd_control, hpi_ctl, "Format"); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1788 | snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE; |
| 1789 | snd_control.info = snd_asihpi_aesebu_format_info; |
| 1790 | snd_control.get = snd_asihpi_aesebu_tx_format_get; |
| 1791 | snd_control.put = snd_asihpi_aesebu_tx_format_put; |
| 1792 | |
| 1793 | return ctl_add(card, &snd_control, asihpi); |
| 1794 | } |
| 1795 | |
| 1796 | /*------------------------------------------------------------ |
| 1797 | Tuner controls |
| 1798 | ------------------------------------------------------------*/ |
| 1799 | |
| 1800 | /* Gain */ |
| 1801 | |
| 1802 | static int snd_asihpi_tuner_gain_info(struct snd_kcontrol *kcontrol, |
| 1803 | struct snd_ctl_elem_info *uinfo) |
| 1804 | { |
| 1805 | u32 h_control = kcontrol->private_value; |
| 1806 | u16 err; |
| 1807 | short idx; |
| 1808 | u16 gain_range[3]; |
| 1809 | |
| 1810 | for (idx = 0; idx < 3; idx++) { |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1811 | err = hpi_tuner_query_gain(h_control, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1812 | idx, &gain_range[idx]); |
| 1813 | if (err != 0) |
| 1814 | return err; |
| 1815 | } |
| 1816 | |
| 1817 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 1818 | uinfo->count = 1; |
| 1819 | uinfo->value.integer.min = ((int)gain_range[0]) / HPI_UNITS_PER_dB; |
| 1820 | uinfo->value.integer.max = ((int)gain_range[1]) / HPI_UNITS_PER_dB; |
| 1821 | uinfo->value.integer.step = ((int) gain_range[2]) / HPI_UNITS_PER_dB; |
| 1822 | return 0; |
| 1823 | } |
| 1824 | |
| 1825 | static int snd_asihpi_tuner_gain_get(struct snd_kcontrol *kcontrol, |
| 1826 | struct snd_ctl_elem_value *ucontrol) |
| 1827 | { |
| 1828 | /* |
| 1829 | struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol); |
| 1830 | */ |
| 1831 | u32 h_control = kcontrol->private_value; |
| 1832 | short gain; |
| 1833 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1834 | hpi_handle_error(hpi_tuner_get_gain(h_control, &gain)); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1835 | ucontrol->value.integer.value[0] = gain / HPI_UNITS_PER_dB; |
| 1836 | |
| 1837 | return 0; |
| 1838 | } |
| 1839 | |
| 1840 | static int snd_asihpi_tuner_gain_put(struct snd_kcontrol *kcontrol, |
| 1841 | struct snd_ctl_elem_value *ucontrol) |
| 1842 | { |
| 1843 | /* |
| 1844 | struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol); |
| 1845 | */ |
| 1846 | u32 h_control = kcontrol->private_value; |
| 1847 | short gain; |
| 1848 | |
| 1849 | gain = (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB; |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1850 | hpi_handle_error(hpi_tuner_set_gain(h_control, gain)); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1851 | |
| 1852 | return 1; |
| 1853 | } |
| 1854 | |
| 1855 | /* Band */ |
| 1856 | |
| 1857 | static int asihpi_tuner_band_query(struct snd_kcontrol *kcontrol, |
| 1858 | u16 *band_list, u32 len) { |
| 1859 | u32 h_control = kcontrol->private_value; |
| 1860 | u16 err = 0; |
| 1861 | u32 i; |
| 1862 | |
| 1863 | for (i = 0; i < len; i++) { |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1864 | err = hpi_tuner_query_band( |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1865 | h_control, i, &band_list[i]); |
| 1866 | if (err != 0) |
| 1867 | break; |
| 1868 | } |
| 1869 | |
| 1870 | if (err && (err != HPI_ERROR_INVALID_OBJ_INDEX)) |
| 1871 | return -EIO; |
| 1872 | |
| 1873 | return i; |
| 1874 | } |
| 1875 | |
| 1876 | static int snd_asihpi_tuner_band_info(struct snd_kcontrol *kcontrol, |
| 1877 | struct snd_ctl_elem_info *uinfo) |
| 1878 | { |
| 1879 | u16 tuner_bands[HPI_TUNER_BAND_LAST]; |
| 1880 | int num_bands = 0; |
| 1881 | |
| 1882 | num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands, |
| 1883 | HPI_TUNER_BAND_LAST); |
| 1884 | |
| 1885 | if (num_bands < 0) |
| 1886 | return num_bands; |
| 1887 | |
| 1888 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 1889 | uinfo->count = 1; |
| 1890 | uinfo->value.enumerated.items = num_bands; |
| 1891 | |
| 1892 | if (num_bands > 0) { |
| 1893 | if (uinfo->value.enumerated.item >= |
| 1894 | uinfo->value.enumerated.items) |
| 1895 | uinfo->value.enumerated.item = |
| 1896 | uinfo->value.enumerated.items - 1; |
| 1897 | |
| 1898 | strcpy(uinfo->value.enumerated.name, |
| 1899 | asihpi_tuner_band_names[ |
| 1900 | tuner_bands[uinfo->value.enumerated.item]]); |
| 1901 | |
| 1902 | } |
| 1903 | return 0; |
| 1904 | } |
| 1905 | |
| 1906 | static int snd_asihpi_tuner_band_get(struct snd_kcontrol *kcontrol, |
| 1907 | struct snd_ctl_elem_value *ucontrol) |
| 1908 | { |
| 1909 | u32 h_control = kcontrol->private_value; |
| 1910 | /* |
| 1911 | struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol); |
| 1912 | */ |
| 1913 | u16 band, idx; |
| 1914 | u16 tuner_bands[HPI_TUNER_BAND_LAST]; |
| 1915 | u32 num_bands = 0; |
| 1916 | |
| 1917 | num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands, |
| 1918 | HPI_TUNER_BAND_LAST); |
| 1919 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1920 | hpi_handle_error(hpi_tuner_get_band(h_control, &band)); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1921 | |
| 1922 | ucontrol->value.enumerated.item[0] = -1; |
| 1923 | for (idx = 0; idx < HPI_TUNER_BAND_LAST; idx++) |
| 1924 | if (tuner_bands[idx] == band) { |
| 1925 | ucontrol->value.enumerated.item[0] = idx; |
| 1926 | break; |
| 1927 | } |
| 1928 | |
| 1929 | return 0; |
| 1930 | } |
| 1931 | |
| 1932 | static int snd_asihpi_tuner_band_put(struct snd_kcontrol *kcontrol, |
| 1933 | struct snd_ctl_elem_value *ucontrol) |
| 1934 | { |
| 1935 | /* |
| 1936 | struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol); |
| 1937 | */ |
| 1938 | u32 h_control = kcontrol->private_value; |
| 1939 | u16 band; |
| 1940 | u16 tuner_bands[HPI_TUNER_BAND_LAST]; |
| 1941 | u32 num_bands = 0; |
| 1942 | |
| 1943 | num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands, |
| 1944 | HPI_TUNER_BAND_LAST); |
| 1945 | |
| 1946 | band = tuner_bands[ucontrol->value.enumerated.item[0]]; |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1947 | hpi_handle_error(hpi_tuner_set_band(h_control, band)); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1948 | |
| 1949 | return 1; |
| 1950 | } |
| 1951 | |
| 1952 | /* Freq */ |
| 1953 | |
| 1954 | static int snd_asihpi_tuner_freq_info(struct snd_kcontrol *kcontrol, |
| 1955 | struct snd_ctl_elem_info *uinfo) |
| 1956 | { |
| 1957 | u32 h_control = kcontrol->private_value; |
| 1958 | u16 err; |
| 1959 | u16 tuner_bands[HPI_TUNER_BAND_LAST]; |
| 1960 | u16 num_bands = 0, band_iter, idx; |
| 1961 | u32 freq_range[3], temp_freq_range[3]; |
| 1962 | |
| 1963 | num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands, |
| 1964 | HPI_TUNER_BAND_LAST); |
| 1965 | |
| 1966 | freq_range[0] = INT_MAX; |
| 1967 | freq_range[1] = 0; |
| 1968 | freq_range[2] = INT_MAX; |
| 1969 | |
| 1970 | for (band_iter = 0; band_iter < num_bands; band_iter++) { |
| 1971 | for (idx = 0; idx < 3; idx++) { |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 1972 | err = hpi_tuner_query_frequency(h_control, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 1973 | idx, tuner_bands[band_iter], |
| 1974 | &temp_freq_range[idx]); |
| 1975 | if (err != 0) |
| 1976 | return err; |
| 1977 | } |
| 1978 | |
| 1979 | /* skip band with bogus stepping */ |
| 1980 | if (temp_freq_range[2] <= 0) |
| 1981 | continue; |
| 1982 | |
| 1983 | if (temp_freq_range[0] < freq_range[0]) |
| 1984 | freq_range[0] = temp_freq_range[0]; |
| 1985 | if (temp_freq_range[1] > freq_range[1]) |
| 1986 | freq_range[1] = temp_freq_range[1]; |
| 1987 | if (temp_freq_range[2] < freq_range[2]) |
| 1988 | freq_range[2] = temp_freq_range[2]; |
| 1989 | } |
| 1990 | |
| 1991 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 1992 | uinfo->count = 1; |
| 1993 | uinfo->value.integer.min = ((int)freq_range[0]); |
| 1994 | uinfo->value.integer.max = ((int)freq_range[1]); |
| 1995 | uinfo->value.integer.step = ((int)freq_range[2]); |
| 1996 | return 0; |
| 1997 | } |
| 1998 | |
| 1999 | static int snd_asihpi_tuner_freq_get(struct snd_kcontrol *kcontrol, |
| 2000 | struct snd_ctl_elem_value *ucontrol) |
| 2001 | { |
| 2002 | u32 h_control = kcontrol->private_value; |
| 2003 | u32 freq; |
| 2004 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2005 | hpi_handle_error(hpi_tuner_get_frequency(h_control, &freq)); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2006 | ucontrol->value.integer.value[0] = freq; |
| 2007 | |
| 2008 | return 0; |
| 2009 | } |
| 2010 | |
| 2011 | static int snd_asihpi_tuner_freq_put(struct snd_kcontrol *kcontrol, |
| 2012 | struct snd_ctl_elem_value *ucontrol) |
| 2013 | { |
| 2014 | u32 h_control = kcontrol->private_value; |
| 2015 | u32 freq; |
| 2016 | |
| 2017 | freq = ucontrol->value.integer.value[0]; |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2018 | hpi_handle_error(hpi_tuner_set_frequency(h_control, freq)); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2019 | |
| 2020 | return 1; |
| 2021 | } |
| 2022 | |
| 2023 | /* Tuner control group initializer */ |
| 2024 | static int __devinit snd_asihpi_tuner_add(struct snd_card_asihpi *asihpi, |
| 2025 | struct hpi_control *hpi_ctl) |
| 2026 | { |
| 2027 | struct snd_card *card = asihpi->card; |
| 2028 | struct snd_kcontrol_new snd_control; |
| 2029 | |
| 2030 | snd_control.private_value = hpi_ctl->h_control; |
| 2031 | snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE; |
| 2032 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2033 | if (!hpi_tuner_get_gain(hpi_ctl->h_control, NULL)) { |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 2034 | asihpi_ctl_init(&snd_control, hpi_ctl, "Gain"); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2035 | snd_control.info = snd_asihpi_tuner_gain_info; |
| 2036 | snd_control.get = snd_asihpi_tuner_gain_get; |
| 2037 | snd_control.put = snd_asihpi_tuner_gain_put; |
| 2038 | |
| 2039 | if (ctl_add(card, &snd_control, asihpi) < 0) |
| 2040 | return -EINVAL; |
| 2041 | } |
| 2042 | |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 2043 | asihpi_ctl_init(&snd_control, hpi_ctl, "Band"); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2044 | snd_control.info = snd_asihpi_tuner_band_info; |
| 2045 | snd_control.get = snd_asihpi_tuner_band_get; |
| 2046 | snd_control.put = snd_asihpi_tuner_band_put; |
| 2047 | |
| 2048 | if (ctl_add(card, &snd_control, asihpi) < 0) |
| 2049 | return -EINVAL; |
| 2050 | |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 2051 | asihpi_ctl_init(&snd_control, hpi_ctl, "Freq"); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2052 | snd_control.info = snd_asihpi_tuner_freq_info; |
| 2053 | snd_control.get = snd_asihpi_tuner_freq_get; |
| 2054 | snd_control.put = snd_asihpi_tuner_freq_put; |
| 2055 | |
| 2056 | return ctl_add(card, &snd_control, asihpi); |
| 2057 | } |
| 2058 | |
| 2059 | /*------------------------------------------------------------ |
| 2060 | Meter controls |
| 2061 | ------------------------------------------------------------*/ |
| 2062 | static int snd_asihpi_meter_info(struct snd_kcontrol *kcontrol, |
| 2063 | struct snd_ctl_elem_info *uinfo) |
| 2064 | { |
| 2065 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 2066 | uinfo->count = HPI_MAX_CHANNELS; |
| 2067 | uinfo->value.integer.min = 0; |
| 2068 | uinfo->value.integer.max = 0x7FFFFFFF; |
| 2069 | return 0; |
| 2070 | } |
| 2071 | |
| 2072 | /* linear values for 10dB steps */ |
| 2073 | static int log2lin[] = { |
| 2074 | 0x7FFFFFFF, /* 0dB */ |
| 2075 | 679093956, |
| 2076 | 214748365, |
| 2077 | 67909396, |
| 2078 | 21474837, |
| 2079 | 6790940, |
| 2080 | 2147484, /* -60dB */ |
| 2081 | 679094, |
| 2082 | 214748, /* -80 */ |
| 2083 | 67909, |
| 2084 | 21475, /* -100 */ |
| 2085 | 6791, |
| 2086 | 2147, |
| 2087 | 679, |
| 2088 | 214, |
| 2089 | 68, |
| 2090 | 21, |
| 2091 | 7, |
| 2092 | 2 |
| 2093 | }; |
| 2094 | |
| 2095 | static int snd_asihpi_meter_get(struct snd_kcontrol *kcontrol, |
| 2096 | struct snd_ctl_elem_value *ucontrol) |
| 2097 | { |
| 2098 | u32 h_control = kcontrol->private_value; |
| 2099 | short an_gain_mB[HPI_MAX_CHANNELS], i; |
| 2100 | u16 err; |
| 2101 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2102 | err = hpi_meter_get_peak(h_control, an_gain_mB); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2103 | |
| 2104 | for (i = 0; i < HPI_MAX_CHANNELS; i++) { |
| 2105 | if (err) { |
| 2106 | ucontrol->value.integer.value[i] = 0; |
| 2107 | } else if (an_gain_mB[i] >= 0) { |
| 2108 | ucontrol->value.integer.value[i] = |
| 2109 | an_gain_mB[i] << 16; |
| 2110 | } else { |
| 2111 | /* -ve is log value in millibels < -60dB, |
| 2112 | * convert to (roughly!) linear, |
| 2113 | */ |
| 2114 | ucontrol->value.integer.value[i] = |
| 2115 | log2lin[an_gain_mB[i] / -1000]; |
| 2116 | } |
| 2117 | } |
| 2118 | return 0; |
| 2119 | } |
| 2120 | |
| 2121 | static int __devinit snd_asihpi_meter_add(struct snd_card_asihpi *asihpi, |
| 2122 | struct hpi_control *hpi_ctl, int subidx) |
| 2123 | { |
| 2124 | struct snd_card *card = asihpi->card; |
| 2125 | struct snd_kcontrol_new snd_control; |
| 2126 | |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 2127 | asihpi_ctl_init(&snd_control, hpi_ctl, "Meter"); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2128 | snd_control.access = |
| 2129 | SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ; |
| 2130 | snd_control.info = snd_asihpi_meter_info; |
| 2131 | snd_control.get = snd_asihpi_meter_get; |
| 2132 | |
| 2133 | snd_control.index = subidx; |
| 2134 | |
| 2135 | return ctl_add(card, &snd_control, asihpi); |
| 2136 | } |
| 2137 | |
| 2138 | /*------------------------------------------------------------ |
| 2139 | Multiplexer controls |
| 2140 | ------------------------------------------------------------*/ |
| 2141 | static int snd_card_asihpi_mux_count_sources(struct snd_kcontrol *snd_control) |
| 2142 | { |
| 2143 | u32 h_control = snd_control->private_value; |
| 2144 | struct hpi_control hpi_ctl; |
| 2145 | int s, err; |
| 2146 | for (s = 0; s < 32; s++) { |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2147 | err = hpi_multiplexer_query_source(h_control, s, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2148 | &hpi_ctl. |
| 2149 | src_node_type, |
| 2150 | &hpi_ctl. |
| 2151 | src_node_index); |
| 2152 | if (err) |
| 2153 | break; |
| 2154 | } |
| 2155 | return s; |
| 2156 | } |
| 2157 | |
| 2158 | static int snd_asihpi_mux_info(struct snd_kcontrol *kcontrol, |
| 2159 | struct snd_ctl_elem_info *uinfo) |
| 2160 | { |
| 2161 | int err; |
| 2162 | u16 src_node_type, src_node_index; |
| 2163 | u32 h_control = kcontrol->private_value; |
| 2164 | |
| 2165 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 2166 | uinfo->count = 1; |
| 2167 | uinfo->value.enumerated.items = |
| 2168 | snd_card_asihpi_mux_count_sources(kcontrol); |
| 2169 | |
| 2170 | if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items) |
| 2171 | uinfo->value.enumerated.item = |
| 2172 | uinfo->value.enumerated.items - 1; |
| 2173 | |
| 2174 | err = |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2175 | hpi_multiplexer_query_source(h_control, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2176 | uinfo->value.enumerated.item, |
| 2177 | &src_node_type, &src_node_index); |
| 2178 | |
| 2179 | sprintf(uinfo->value.enumerated.name, "%s %d", |
Eliot Blennerhassett | 168f1b0 | 2010-07-06 08:37:06 +1200 | [diff] [blame] | 2180 | asihpi_src_names[src_node_type - HPI_SOURCENODE_NONE], |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2181 | src_node_index); |
| 2182 | return 0; |
| 2183 | } |
| 2184 | |
| 2185 | static int snd_asihpi_mux_get(struct snd_kcontrol *kcontrol, |
| 2186 | struct snd_ctl_elem_value *ucontrol) |
| 2187 | { |
| 2188 | u32 h_control = kcontrol->private_value; |
| 2189 | u16 source_type, source_index; |
| 2190 | u16 src_node_type, src_node_index; |
| 2191 | int s; |
| 2192 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2193 | hpi_handle_error(hpi_multiplexer_get_source(h_control, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2194 | &source_type, &source_index)); |
| 2195 | /* Should cache this search result! */ |
| 2196 | for (s = 0; s < 256; s++) { |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2197 | if (hpi_multiplexer_query_source(h_control, s, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2198 | &src_node_type, &src_node_index)) |
| 2199 | break; |
| 2200 | |
| 2201 | if ((source_type == src_node_type) |
| 2202 | && (source_index == src_node_index)) { |
| 2203 | ucontrol->value.enumerated.item[0] = s; |
| 2204 | return 0; |
| 2205 | } |
| 2206 | } |
| 2207 | snd_printd(KERN_WARNING |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 2208 | "Control %x failed to match mux source %hu %hu\n", |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2209 | h_control, source_type, source_index); |
| 2210 | ucontrol->value.enumerated.item[0] = 0; |
| 2211 | return 0; |
| 2212 | } |
| 2213 | |
| 2214 | static int snd_asihpi_mux_put(struct snd_kcontrol *kcontrol, |
| 2215 | struct snd_ctl_elem_value *ucontrol) |
| 2216 | { |
| 2217 | int change; |
| 2218 | u32 h_control = kcontrol->private_value; |
| 2219 | u16 source_type, source_index; |
| 2220 | u16 e; |
| 2221 | |
| 2222 | change = 1; |
| 2223 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2224 | e = hpi_multiplexer_query_source(h_control, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2225 | ucontrol->value.enumerated.item[0], |
| 2226 | &source_type, &source_index); |
| 2227 | if (!e) |
| 2228 | hpi_handle_error( |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2229 | hpi_multiplexer_set_source(h_control, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2230 | source_type, source_index)); |
| 2231 | return change; |
| 2232 | } |
| 2233 | |
| 2234 | |
| 2235 | static int __devinit snd_asihpi_mux_add(struct snd_card_asihpi *asihpi, |
| 2236 | struct hpi_control *hpi_ctl) |
| 2237 | { |
| 2238 | struct snd_card *card = asihpi->card; |
| 2239 | struct snd_kcontrol_new snd_control; |
| 2240 | |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 2241 | asihpi_ctl_init(&snd_control, hpi_ctl, "Route"); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2242 | snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE; |
| 2243 | snd_control.info = snd_asihpi_mux_info; |
| 2244 | snd_control.get = snd_asihpi_mux_get; |
| 2245 | snd_control.put = snd_asihpi_mux_put; |
| 2246 | |
| 2247 | return ctl_add(card, &snd_control, asihpi); |
| 2248 | |
| 2249 | } |
| 2250 | |
| 2251 | /*------------------------------------------------------------ |
| 2252 | Channel mode controls |
| 2253 | ------------------------------------------------------------*/ |
| 2254 | static int snd_asihpi_cmode_info(struct snd_kcontrol *kcontrol, |
| 2255 | struct snd_ctl_elem_info *uinfo) |
| 2256 | { |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 2257 | static const char * const mode_names[HPI_CHANNEL_MODE_LAST + 1] = { |
| 2258 | "invalid", |
| 2259 | "Normal", "Swap", |
| 2260 | "From Left", "From Right", |
| 2261 | "To Left", "To Right" |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2262 | }; |
| 2263 | |
| 2264 | u32 h_control = kcontrol->private_value; |
| 2265 | u16 mode; |
| 2266 | int i; |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 2267 | u16 mode_map[6]; |
| 2268 | int valid_modes = 0; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2269 | |
| 2270 | /* HPI channel mode values can be from 1 to 6 |
| 2271 | Some adapters only support a contiguous subset |
| 2272 | */ |
| 2273 | for (i = 0; i < HPI_CHANNEL_MODE_LAST; i++) |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 2274 | if (!hpi_channel_mode_query_mode( |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2275 | h_control, i, &mode)) { |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 2276 | mode_map[valid_modes] = mode; |
| 2277 | valid_modes++; |
| 2278 | } |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2279 | |
| 2280 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 2281 | uinfo->count = 1; |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 2282 | uinfo->value.enumerated.items = valid_modes; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2283 | |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 2284 | if (uinfo->value.enumerated.item >= valid_modes) |
| 2285 | uinfo->value.enumerated.item = valid_modes - 1; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2286 | |
| 2287 | strcpy(uinfo->value.enumerated.name, |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 2288 | mode_names[mode_map[uinfo->value.enumerated.item]]); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2289 | |
| 2290 | return 0; |
| 2291 | } |
| 2292 | |
| 2293 | static int snd_asihpi_cmode_get(struct snd_kcontrol *kcontrol, |
| 2294 | struct snd_ctl_elem_value *ucontrol) |
| 2295 | { |
| 2296 | u32 h_control = kcontrol->private_value; |
| 2297 | u16 mode; |
| 2298 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2299 | if (hpi_channel_mode_get(h_control, &mode)) |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2300 | mode = 1; |
| 2301 | |
| 2302 | ucontrol->value.enumerated.item[0] = mode - 1; |
| 2303 | |
| 2304 | return 0; |
| 2305 | } |
| 2306 | |
| 2307 | static int snd_asihpi_cmode_put(struct snd_kcontrol *kcontrol, |
| 2308 | struct snd_ctl_elem_value *ucontrol) |
| 2309 | { |
| 2310 | int change; |
| 2311 | u32 h_control = kcontrol->private_value; |
| 2312 | |
| 2313 | change = 1; |
| 2314 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2315 | hpi_handle_error(hpi_channel_mode_set(h_control, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2316 | ucontrol->value.enumerated.item[0] + 1)); |
| 2317 | return change; |
| 2318 | } |
| 2319 | |
| 2320 | |
| 2321 | static int __devinit snd_asihpi_cmode_add(struct snd_card_asihpi *asihpi, |
| 2322 | struct hpi_control *hpi_ctl) |
| 2323 | { |
| 2324 | struct snd_card *card = asihpi->card; |
| 2325 | struct snd_kcontrol_new snd_control; |
| 2326 | |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 2327 | asihpi_ctl_init(&snd_control, hpi_ctl, "Mode"); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2328 | snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE; |
| 2329 | snd_control.info = snd_asihpi_cmode_info; |
| 2330 | snd_control.get = snd_asihpi_cmode_get; |
| 2331 | snd_control.put = snd_asihpi_cmode_put; |
| 2332 | |
| 2333 | return ctl_add(card, &snd_control, asihpi); |
| 2334 | } |
| 2335 | |
| 2336 | /*------------------------------------------------------------ |
| 2337 | Sampleclock source controls |
| 2338 | ------------------------------------------------------------*/ |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2339 | static char *sampleclock_sources[MAX_CLOCKSOURCES] = { |
| 2340 | "N/A", "Local PLL", "Digital Sync", "Word External", "Word Header", |
| 2341 | "SMPTE", "Digital1", "Auto", "Network", "Invalid", |
| 2342 | "Prev Module", |
| 2343 | "Digital2", "Digital3", "Digital4", "Digital5", |
| 2344 | "Digital6", "Digital7", "Digital8"}; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2345 | |
| 2346 | static int snd_asihpi_clksrc_info(struct snd_kcontrol *kcontrol, |
| 2347 | struct snd_ctl_elem_info *uinfo) |
| 2348 | { |
| 2349 | struct snd_card_asihpi *asihpi = |
| 2350 | (struct snd_card_asihpi *)(kcontrol->private_data); |
| 2351 | struct clk_cache *clkcache = &asihpi->cc; |
| 2352 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 2353 | uinfo->count = 1; |
| 2354 | uinfo->value.enumerated.items = clkcache->count; |
| 2355 | |
| 2356 | if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items) |
| 2357 | uinfo->value.enumerated.item = |
| 2358 | uinfo->value.enumerated.items - 1; |
| 2359 | |
| 2360 | strcpy(uinfo->value.enumerated.name, |
| 2361 | clkcache->s[uinfo->value.enumerated.item].name); |
| 2362 | return 0; |
| 2363 | } |
| 2364 | |
| 2365 | static int snd_asihpi_clksrc_get(struct snd_kcontrol *kcontrol, |
| 2366 | struct snd_ctl_elem_value *ucontrol) |
| 2367 | { |
| 2368 | struct snd_card_asihpi *asihpi = |
| 2369 | (struct snd_card_asihpi *)(kcontrol->private_data); |
| 2370 | struct clk_cache *clkcache = &asihpi->cc; |
| 2371 | u32 h_control = kcontrol->private_value; |
| 2372 | u16 source, srcindex = 0; |
| 2373 | int i; |
| 2374 | |
| 2375 | ucontrol->value.enumerated.item[0] = 0; |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2376 | if (hpi_sample_clock_get_source(h_control, &source)) |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2377 | source = 0; |
| 2378 | |
| 2379 | if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT) |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2380 | if (hpi_sample_clock_get_source_index(h_control, &srcindex)) |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2381 | srcindex = 0; |
| 2382 | |
| 2383 | for (i = 0; i < clkcache->count; i++) |
| 2384 | if ((clkcache->s[i].source == source) && |
| 2385 | (clkcache->s[i].index == srcindex)) |
| 2386 | break; |
| 2387 | |
| 2388 | ucontrol->value.enumerated.item[0] = i; |
| 2389 | |
| 2390 | return 0; |
| 2391 | } |
| 2392 | |
| 2393 | static int snd_asihpi_clksrc_put(struct snd_kcontrol *kcontrol, |
| 2394 | struct snd_ctl_elem_value *ucontrol) |
| 2395 | { |
| 2396 | struct snd_card_asihpi *asihpi = |
| 2397 | (struct snd_card_asihpi *)(kcontrol->private_data); |
| 2398 | struct clk_cache *clkcache = &asihpi->cc; |
| 2399 | int change, item; |
| 2400 | u32 h_control = kcontrol->private_value; |
| 2401 | |
| 2402 | change = 1; |
| 2403 | item = ucontrol->value.enumerated.item[0]; |
| 2404 | if (item >= clkcache->count) |
| 2405 | item = clkcache->count-1; |
| 2406 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2407 | hpi_handle_error(hpi_sample_clock_set_source( |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2408 | h_control, clkcache->s[item].source)); |
| 2409 | |
| 2410 | if (clkcache->s[item].source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT) |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2411 | hpi_handle_error(hpi_sample_clock_set_source_index( |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2412 | h_control, clkcache->s[item].index)); |
| 2413 | return change; |
| 2414 | } |
| 2415 | |
| 2416 | /*------------------------------------------------------------ |
| 2417 | Clkrate controls |
| 2418 | ------------------------------------------------------------*/ |
| 2419 | /* Need to change this to enumerated control with list of rates */ |
| 2420 | static int snd_asihpi_clklocal_info(struct snd_kcontrol *kcontrol, |
| 2421 | struct snd_ctl_elem_info *uinfo) |
| 2422 | { |
| 2423 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 2424 | uinfo->count = 1; |
| 2425 | uinfo->value.integer.min = 8000; |
| 2426 | uinfo->value.integer.max = 192000; |
| 2427 | uinfo->value.integer.step = 100; |
| 2428 | |
| 2429 | return 0; |
| 2430 | } |
| 2431 | |
| 2432 | static int snd_asihpi_clklocal_get(struct snd_kcontrol *kcontrol, |
| 2433 | struct snd_ctl_elem_value *ucontrol) |
| 2434 | { |
| 2435 | u32 h_control = kcontrol->private_value; |
| 2436 | u32 rate; |
| 2437 | u16 e; |
| 2438 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2439 | e = hpi_sample_clock_get_local_rate(h_control, &rate); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2440 | if (!e) |
| 2441 | ucontrol->value.integer.value[0] = rate; |
| 2442 | else |
| 2443 | ucontrol->value.integer.value[0] = 0; |
| 2444 | return 0; |
| 2445 | } |
| 2446 | |
| 2447 | static int snd_asihpi_clklocal_put(struct snd_kcontrol *kcontrol, |
| 2448 | struct snd_ctl_elem_value *ucontrol) |
| 2449 | { |
| 2450 | int change; |
| 2451 | u32 h_control = kcontrol->private_value; |
| 2452 | |
| 2453 | /* change = asihpi->mixer_clkrate[addr][0] != left || |
| 2454 | asihpi->mixer_clkrate[addr][1] != right; |
| 2455 | */ |
| 2456 | change = 1; |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2457 | hpi_handle_error(hpi_sample_clock_set_local_rate(h_control, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2458 | ucontrol->value.integer.value[0])); |
| 2459 | return change; |
| 2460 | } |
| 2461 | |
| 2462 | static int snd_asihpi_clkrate_info(struct snd_kcontrol *kcontrol, |
| 2463 | struct snd_ctl_elem_info *uinfo) |
| 2464 | { |
| 2465 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 2466 | uinfo->count = 1; |
| 2467 | uinfo->value.integer.min = 8000; |
| 2468 | uinfo->value.integer.max = 192000; |
| 2469 | uinfo->value.integer.step = 100; |
| 2470 | |
| 2471 | return 0; |
| 2472 | } |
| 2473 | |
| 2474 | static int snd_asihpi_clkrate_get(struct snd_kcontrol *kcontrol, |
| 2475 | struct snd_ctl_elem_value *ucontrol) |
| 2476 | { |
| 2477 | u32 h_control = kcontrol->private_value; |
| 2478 | u32 rate; |
| 2479 | u16 e; |
| 2480 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2481 | e = hpi_sample_clock_get_sample_rate(h_control, &rate); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2482 | if (!e) |
| 2483 | ucontrol->value.integer.value[0] = rate; |
| 2484 | else |
| 2485 | ucontrol->value.integer.value[0] = 0; |
| 2486 | return 0; |
| 2487 | } |
| 2488 | |
| 2489 | static int __devinit snd_asihpi_sampleclock_add(struct snd_card_asihpi *asihpi, |
| 2490 | struct hpi_control *hpi_ctl) |
| 2491 | { |
| 2492 | struct snd_card *card = asihpi->card; |
| 2493 | struct snd_kcontrol_new snd_control; |
| 2494 | |
| 2495 | struct clk_cache *clkcache = &asihpi->cc; |
| 2496 | u32 hSC = hpi_ctl->h_control; |
| 2497 | int has_aes_in = 0; |
| 2498 | int i, j; |
| 2499 | u16 source; |
| 2500 | |
| 2501 | snd_control.private_value = hpi_ctl->h_control; |
| 2502 | |
| 2503 | clkcache->has_local = 0; |
| 2504 | |
| 2505 | for (i = 0; i <= HPI_SAMPLECLOCK_SOURCE_LAST; i++) { |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2506 | if (hpi_sample_clock_query_source(hSC, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2507 | i, &source)) |
| 2508 | break; |
| 2509 | clkcache->s[i].source = source; |
| 2510 | clkcache->s[i].index = 0; |
| 2511 | clkcache->s[i].name = sampleclock_sources[source]; |
| 2512 | if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT) |
| 2513 | has_aes_in = 1; |
| 2514 | if (source == HPI_SAMPLECLOCK_SOURCE_LOCAL) |
| 2515 | clkcache->has_local = 1; |
| 2516 | } |
| 2517 | if (has_aes_in) |
| 2518 | /* already will have picked up index 0 above */ |
| 2519 | for (j = 1; j < 8; j++) { |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2520 | if (hpi_sample_clock_query_source_index(hSC, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2521 | j, HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT, |
| 2522 | &source)) |
| 2523 | break; |
| 2524 | clkcache->s[i].source = |
| 2525 | HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT; |
| 2526 | clkcache->s[i].index = j; |
| 2527 | clkcache->s[i].name = sampleclock_sources[ |
| 2528 | j+HPI_SAMPLECLOCK_SOURCE_LAST]; |
| 2529 | i++; |
| 2530 | } |
| 2531 | clkcache->count = i; |
| 2532 | |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 2533 | asihpi_ctl_init(&snd_control, hpi_ctl, "Source"); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2534 | snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ; |
| 2535 | snd_control.info = snd_asihpi_clksrc_info; |
| 2536 | snd_control.get = snd_asihpi_clksrc_get; |
| 2537 | snd_control.put = snd_asihpi_clksrc_put; |
| 2538 | if (ctl_add(card, &snd_control, asihpi) < 0) |
| 2539 | return -EINVAL; |
| 2540 | |
| 2541 | |
| 2542 | if (clkcache->has_local) { |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 2543 | asihpi_ctl_init(&snd_control, hpi_ctl, "Localrate"); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2544 | snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ; |
| 2545 | snd_control.info = snd_asihpi_clklocal_info; |
| 2546 | snd_control.get = snd_asihpi_clklocal_get; |
| 2547 | snd_control.put = snd_asihpi_clklocal_put; |
| 2548 | |
| 2549 | |
| 2550 | if (ctl_add(card, &snd_control, asihpi) < 0) |
| 2551 | return -EINVAL; |
| 2552 | } |
| 2553 | |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 2554 | asihpi_ctl_init(&snd_control, hpi_ctl, "Rate"); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2555 | snd_control.access = |
| 2556 | SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ; |
| 2557 | snd_control.info = snd_asihpi_clkrate_info; |
| 2558 | snd_control.get = snd_asihpi_clkrate_get; |
| 2559 | |
| 2560 | return ctl_add(card, &snd_control, asihpi); |
| 2561 | } |
| 2562 | /*------------------------------------------------------------ |
| 2563 | Mixer |
| 2564 | ------------------------------------------------------------*/ |
| 2565 | |
| 2566 | static int __devinit snd_card_asihpi_mixer_new(struct snd_card_asihpi *asihpi) |
| 2567 | { |
| 2568 | struct snd_card *card = asihpi->card; |
| 2569 | unsigned int idx = 0; |
| 2570 | unsigned int subindex = 0; |
| 2571 | int err; |
| 2572 | struct hpi_control hpi_ctl, prev_ctl; |
| 2573 | |
| 2574 | if (snd_BUG_ON(!asihpi)) |
| 2575 | return -EINVAL; |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 2576 | strcpy(card->mixername, "Asihpi Mixer"); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2577 | |
| 2578 | err = |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2579 | hpi_mixer_open(asihpi->adapter_index, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2580 | &asihpi->h_mixer); |
| 2581 | hpi_handle_error(err); |
| 2582 | if (err) |
| 2583 | return -err; |
| 2584 | |
Takashi Iwai | 21896bc | 2010-06-02 12:08:37 +0200 | [diff] [blame] | 2585 | memset(&prev_ctl, 0, sizeof(prev_ctl)); |
| 2586 | prev_ctl.control_type = -1; |
| 2587 | |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2588 | for (idx = 0; idx < 2000; idx++) { |
| 2589 | err = hpi_mixer_get_control_by_index( |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2590 | asihpi->h_mixer, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2591 | idx, |
| 2592 | &hpi_ctl.src_node_type, |
| 2593 | &hpi_ctl.src_node_index, |
| 2594 | &hpi_ctl.dst_node_type, |
| 2595 | &hpi_ctl.dst_node_index, |
| 2596 | &hpi_ctl.control_type, |
| 2597 | &hpi_ctl.h_control); |
| 2598 | if (err) { |
| 2599 | if (err == HPI_ERROR_CONTROL_DISABLED) { |
| 2600 | if (mixer_dump) |
| 2601 | snd_printk(KERN_INFO |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 2602 | "Disabled HPI Control(%d)\n", |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2603 | idx); |
| 2604 | continue; |
| 2605 | } else |
| 2606 | break; |
| 2607 | |
| 2608 | } |
| 2609 | |
Eliot Blennerhassett | 168f1b0 | 2010-07-06 08:37:06 +1200 | [diff] [blame] | 2610 | hpi_ctl.src_node_type -= HPI_SOURCENODE_NONE; |
| 2611 | hpi_ctl.dst_node_type -= HPI_DESTNODE_NONE; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2612 | |
| 2613 | /* ASI50xx in SSX mode has multiple meters on the same node. |
| 2614 | Use subindex to create distinct ALSA controls |
| 2615 | for any duplicated controls. |
| 2616 | */ |
| 2617 | if ((hpi_ctl.control_type == prev_ctl.control_type) && |
| 2618 | (hpi_ctl.src_node_type == prev_ctl.src_node_type) && |
| 2619 | (hpi_ctl.src_node_index == prev_ctl.src_node_index) && |
| 2620 | (hpi_ctl.dst_node_type == prev_ctl.dst_node_type) && |
| 2621 | (hpi_ctl.dst_node_index == prev_ctl.dst_node_index)) |
| 2622 | subindex++; |
| 2623 | else |
| 2624 | subindex = 0; |
| 2625 | |
| 2626 | prev_ctl = hpi_ctl; |
| 2627 | |
| 2628 | switch (hpi_ctl.control_type) { |
| 2629 | case HPI_CONTROL_VOLUME: |
| 2630 | err = snd_asihpi_volume_add(asihpi, &hpi_ctl); |
| 2631 | break; |
| 2632 | case HPI_CONTROL_LEVEL: |
| 2633 | err = snd_asihpi_level_add(asihpi, &hpi_ctl); |
| 2634 | break; |
| 2635 | case HPI_CONTROL_MULTIPLEXER: |
| 2636 | err = snd_asihpi_mux_add(asihpi, &hpi_ctl); |
| 2637 | break; |
| 2638 | case HPI_CONTROL_CHANNEL_MODE: |
| 2639 | err = snd_asihpi_cmode_add(asihpi, &hpi_ctl); |
| 2640 | break; |
| 2641 | case HPI_CONTROL_METER: |
| 2642 | err = snd_asihpi_meter_add(asihpi, &hpi_ctl, subindex); |
| 2643 | break; |
| 2644 | case HPI_CONTROL_SAMPLECLOCK: |
| 2645 | err = snd_asihpi_sampleclock_add( |
| 2646 | asihpi, &hpi_ctl); |
| 2647 | break; |
| 2648 | case HPI_CONTROL_CONNECTION: /* ignore these */ |
| 2649 | continue; |
| 2650 | case HPI_CONTROL_TUNER: |
| 2651 | err = snd_asihpi_tuner_add(asihpi, &hpi_ctl); |
| 2652 | break; |
| 2653 | case HPI_CONTROL_AESEBU_TRANSMITTER: |
| 2654 | err = snd_asihpi_aesebu_tx_add(asihpi, &hpi_ctl); |
| 2655 | break; |
| 2656 | case HPI_CONTROL_AESEBU_RECEIVER: |
| 2657 | err = snd_asihpi_aesebu_rx_add(asihpi, &hpi_ctl); |
| 2658 | break; |
| 2659 | case HPI_CONTROL_VOX: |
| 2660 | case HPI_CONTROL_BITSTREAM: |
| 2661 | case HPI_CONTROL_MICROPHONE: |
| 2662 | case HPI_CONTROL_PARAMETRIC_EQ: |
| 2663 | case HPI_CONTROL_COMPANDER: |
| 2664 | default: |
| 2665 | if (mixer_dump) |
| 2666 | snd_printk(KERN_INFO |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 2667 | "Untranslated HPI Control" |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2668 | "(%d) %d %d %d %d %d\n", |
| 2669 | idx, |
| 2670 | hpi_ctl.control_type, |
| 2671 | hpi_ctl.src_node_type, |
| 2672 | hpi_ctl.src_node_index, |
| 2673 | hpi_ctl.dst_node_type, |
| 2674 | hpi_ctl.dst_node_index); |
| 2675 | continue; |
| 2676 | }; |
| 2677 | if (err < 0) |
| 2678 | return err; |
| 2679 | } |
| 2680 | if (HPI_ERROR_INVALID_OBJ_INDEX != err) |
| 2681 | hpi_handle_error(err); |
| 2682 | |
| 2683 | snd_printk(KERN_INFO "%d mixer controls found\n", idx); |
| 2684 | |
| 2685 | return 0; |
| 2686 | } |
| 2687 | |
| 2688 | /*------------------------------------------------------------ |
| 2689 | /proc interface |
| 2690 | ------------------------------------------------------------*/ |
| 2691 | |
| 2692 | static void |
| 2693 | snd_asihpi_proc_read(struct snd_info_entry *entry, |
| 2694 | struct snd_info_buffer *buffer) |
| 2695 | { |
| 2696 | struct snd_card_asihpi *asihpi = entry->private_data; |
| 2697 | u16 version; |
| 2698 | u32 h_control; |
| 2699 | u32 rate = 0; |
| 2700 | u16 source = 0; |
| 2701 | int err; |
| 2702 | |
| 2703 | snd_iprintf(buffer, "ASIHPI driver proc file\n"); |
| 2704 | snd_iprintf(buffer, |
| 2705 | "adapter ID=%4X\n_index=%d\n" |
| 2706 | "num_outstreams=%d\n_num_instreams=%d\n", |
| 2707 | asihpi->type, asihpi->adapter_index, |
| 2708 | asihpi->num_outstreams, asihpi->num_instreams); |
| 2709 | |
| 2710 | version = asihpi->version; |
| 2711 | snd_iprintf(buffer, |
| 2712 | "serial#=%d\n_hw version %c%d\nDSP code version %03d\n", |
| 2713 | asihpi->serial_number, ((version >> 3) & 0xf) + 'A', |
| 2714 | version & 0x7, |
| 2715 | ((version >> 13) * 100) + ((version >> 7) & 0x3f)); |
| 2716 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2717 | err = hpi_mixer_get_control(asihpi->h_mixer, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2718 | HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0, |
| 2719 | HPI_CONTROL_SAMPLECLOCK, &h_control); |
| 2720 | |
| 2721 | if (!err) { |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2722 | err = hpi_sample_clock_get_sample_rate( |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2723 | h_control, &rate); |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2724 | err += hpi_sample_clock_get_source(h_control, &source); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2725 | |
| 2726 | if (!err) |
| 2727 | snd_iprintf(buffer, "sample_clock=%d_hz, source %s\n", |
| 2728 | rate, sampleclock_sources[source]); |
| 2729 | } |
| 2730 | |
| 2731 | } |
| 2732 | |
| 2733 | |
| 2734 | static void __devinit snd_asihpi_proc_init(struct snd_card_asihpi *asihpi) |
| 2735 | { |
| 2736 | struct snd_info_entry *entry; |
| 2737 | |
| 2738 | if (!snd_card_proc_new(asihpi->card, "info", &entry)) |
| 2739 | snd_info_set_text_ops(entry, asihpi, snd_asihpi_proc_read); |
| 2740 | } |
| 2741 | |
| 2742 | /*------------------------------------------------------------ |
| 2743 | HWDEP |
| 2744 | ------------------------------------------------------------*/ |
| 2745 | |
| 2746 | static int snd_asihpi_hpi_open(struct snd_hwdep *hw, struct file *file) |
| 2747 | { |
| 2748 | if (enable_hpi_hwdep) |
| 2749 | return 0; |
| 2750 | else |
| 2751 | return -ENODEV; |
| 2752 | |
| 2753 | } |
| 2754 | |
| 2755 | static int snd_asihpi_hpi_release(struct snd_hwdep *hw, struct file *file) |
| 2756 | { |
| 2757 | if (enable_hpi_hwdep) |
| 2758 | return asihpi_hpi_release(file); |
| 2759 | else |
| 2760 | return -ENODEV; |
| 2761 | } |
| 2762 | |
| 2763 | static int snd_asihpi_hpi_ioctl(struct snd_hwdep *hw, struct file *file, |
| 2764 | unsigned int cmd, unsigned long arg) |
| 2765 | { |
| 2766 | if (enable_hpi_hwdep) |
| 2767 | return asihpi_hpi_ioctl(file, cmd, arg); |
| 2768 | else |
| 2769 | return -ENODEV; |
| 2770 | } |
| 2771 | |
| 2772 | |
| 2773 | /* results in /dev/snd/hwC#D0 file for each card with index # |
| 2774 | also /proc/asound/hwdep will contain '#-00: asihpi (HPI) for each card' |
| 2775 | */ |
| 2776 | static int __devinit snd_asihpi_hpi_new(struct snd_card_asihpi *asihpi, |
| 2777 | int device, struct snd_hwdep **rhwdep) |
| 2778 | { |
| 2779 | struct snd_hwdep *hw; |
| 2780 | int err; |
| 2781 | |
| 2782 | if (rhwdep) |
| 2783 | *rhwdep = NULL; |
| 2784 | err = snd_hwdep_new(asihpi->card, "HPI", device, &hw); |
| 2785 | if (err < 0) |
| 2786 | return err; |
| 2787 | strcpy(hw->name, "asihpi (HPI)"); |
| 2788 | hw->iface = SNDRV_HWDEP_IFACE_LAST; |
| 2789 | hw->ops.open = snd_asihpi_hpi_open; |
| 2790 | hw->ops.ioctl = snd_asihpi_hpi_ioctl; |
| 2791 | hw->ops.release = snd_asihpi_hpi_release; |
| 2792 | hw->private_data = asihpi; |
| 2793 | if (rhwdep) |
| 2794 | *rhwdep = hw; |
| 2795 | return 0; |
| 2796 | } |
| 2797 | |
| 2798 | /*------------------------------------------------------------ |
| 2799 | CARD |
| 2800 | ------------------------------------------------------------*/ |
| 2801 | static int __devinit snd_asihpi_probe(struct pci_dev *pci_dev, |
| 2802 | const struct pci_device_id *pci_id) |
| 2803 | { |
| 2804 | int err; |
| 2805 | |
| 2806 | u16 version; |
| 2807 | int pcm_substreams; |
| 2808 | |
| 2809 | struct hpi_adapter *hpi_card; |
| 2810 | struct snd_card *card; |
| 2811 | struct snd_card_asihpi *asihpi; |
| 2812 | |
| 2813 | u32 h_control; |
| 2814 | u32 h_stream; |
| 2815 | |
| 2816 | static int dev; |
| 2817 | if (dev >= SNDRV_CARDS) |
| 2818 | return -ENODEV; |
| 2819 | |
| 2820 | /* Should this be enable[hpi_card->index] ? */ |
| 2821 | if (!enable[dev]) { |
| 2822 | dev++; |
| 2823 | return -ENOENT; |
| 2824 | } |
| 2825 | |
| 2826 | err = asihpi_adapter_probe(pci_dev, pci_id); |
| 2827 | if (err < 0) |
| 2828 | return err; |
| 2829 | |
| 2830 | hpi_card = pci_get_drvdata(pci_dev); |
| 2831 | /* first try to give the card the same index as its hardware index */ |
| 2832 | err = snd_card_create(hpi_card->index, |
| 2833 | id[hpi_card->index], THIS_MODULE, |
| 2834 | sizeof(struct snd_card_asihpi), |
| 2835 | &card); |
| 2836 | if (err < 0) { |
| 2837 | /* if that fails, try the default index==next available */ |
| 2838 | err = |
| 2839 | snd_card_create(index[dev], id[dev], |
| 2840 | THIS_MODULE, |
| 2841 | sizeof(struct snd_card_asihpi), |
| 2842 | &card); |
| 2843 | if (err < 0) |
| 2844 | return err; |
| 2845 | snd_printk(KERN_WARNING |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 2846 | "**** WARNING **** Adapter index %d->ALSA index %d\n", |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2847 | hpi_card->index, card->number); |
| 2848 | } |
| 2849 | |
Eliot Blennerhassett | 1225367 | 2011-02-10 17:26:10 +1300 | [diff] [blame] | 2850 | snd_card_set_dev(card, &pci_dev->dev); |
| 2851 | |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2852 | asihpi = (struct snd_card_asihpi *) card->private_data; |
| 2853 | asihpi->card = card; |
Eliot Blennerhassett | 1225367 | 2011-02-10 17:26:10 +1300 | [diff] [blame] | 2854 | asihpi->pci = pci_dev; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2855 | asihpi->adapter_index = hpi_card->index; |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2856 | hpi_handle_error(hpi_adapter_get_info( |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2857 | asihpi->adapter_index, |
| 2858 | &asihpi->num_outstreams, |
| 2859 | &asihpi->num_instreams, |
| 2860 | &asihpi->version, |
| 2861 | &asihpi->serial_number, &asihpi->type)); |
| 2862 | |
| 2863 | version = asihpi->version; |
| 2864 | snd_printk(KERN_INFO "adapter ID=%4X index=%d num_outstreams=%d " |
| 2865 | "num_instreams=%d S/N=%d\n" |
Eliot Blennerhassett | e64b1a2 | 2011-02-10 17:25:59 +1300 | [diff] [blame] | 2866 | "Hw Version %c%d DSP code version %03d\n", |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2867 | asihpi->type, asihpi->adapter_index, |
| 2868 | asihpi->num_outstreams, |
| 2869 | asihpi->num_instreams, asihpi->serial_number, |
| 2870 | ((version >> 3) & 0xf) + 'A', |
| 2871 | version & 0x7, |
| 2872 | ((version >> 13) * 100) + ((version >> 7) & 0x3f)); |
| 2873 | |
| 2874 | pcm_substreams = asihpi->num_outstreams; |
| 2875 | if (pcm_substreams < asihpi->num_instreams) |
| 2876 | pcm_substreams = asihpi->num_instreams; |
| 2877 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2878 | err = hpi_adapter_get_property(asihpi->adapter_index, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2879 | HPI_ADAPTER_PROPERTY_CAPS1, |
| 2880 | NULL, &asihpi->support_grouping); |
| 2881 | if (err) |
| 2882 | asihpi->support_grouping = 0; |
| 2883 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2884 | err = hpi_adapter_get_property(asihpi->adapter_index, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2885 | HPI_ADAPTER_PROPERTY_CAPS2, |
| 2886 | &asihpi->support_mrx, NULL); |
| 2887 | if (err) |
| 2888 | asihpi->support_mrx = 0; |
| 2889 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2890 | err = hpi_adapter_get_property(asihpi->adapter_index, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2891 | HPI_ADAPTER_PROPERTY_INTERVAL, |
| 2892 | NULL, &asihpi->update_interval_frames); |
| 2893 | if (err) |
| 2894 | asihpi->update_interval_frames = 512; |
| 2895 | |
Eliot Blennerhassett | f3d145a | 2011-04-05 20:55:44 +1200 | [diff] [blame] | 2896 | if (!asihpi->can_dma) |
Eliot Blennerhassett | 26aebef | 2011-03-25 15:25:47 +1300 | [diff] [blame] | 2897 | asihpi->update_interval_frames *= 2; |
| 2898 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2899 | hpi_handle_error(hpi_instream_open(asihpi->adapter_index, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2900 | 0, &h_stream)); |
| 2901 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2902 | err = hpi_instream_host_buffer_free(h_stream); |
Eliot Blennerhassett | f3d145a | 2011-04-05 20:55:44 +1200 | [diff] [blame] | 2903 | asihpi->can_dma = (!err); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2904 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2905 | hpi_handle_error(hpi_instream_close(h_stream)); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2906 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2907 | err = hpi_adapter_get_property(asihpi->adapter_index, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2908 | HPI_ADAPTER_PROPERTY_CURCHANNELS, |
| 2909 | &asihpi->in_max_chans, &asihpi->out_max_chans); |
| 2910 | if (err) { |
| 2911 | asihpi->in_max_chans = 2; |
| 2912 | asihpi->out_max_chans = 2; |
| 2913 | } |
| 2914 | |
Eliot Blennerhassett | f3d145a | 2011-04-05 20:55:44 +1200 | [diff] [blame] | 2915 | snd_printk(KERN_INFO "has dma:%d, grouping:%d, mrx:%d\n", |
| 2916 | asihpi->can_dma, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2917 | asihpi->support_grouping, |
| 2918 | asihpi->support_mrx |
| 2919 | ); |
| 2920 | |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2921 | err = snd_card_asihpi_pcm_new(asihpi, 0, pcm_substreams); |
| 2922 | if (err < 0) { |
| 2923 | snd_printk(KERN_ERR "pcm_new failed\n"); |
| 2924 | goto __nodev; |
| 2925 | } |
| 2926 | err = snd_card_asihpi_mixer_new(asihpi); |
| 2927 | if (err < 0) { |
| 2928 | snd_printk(KERN_ERR "mixer_new failed\n"); |
| 2929 | goto __nodev; |
| 2930 | } |
| 2931 | |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2932 | err = hpi_mixer_get_control(asihpi->h_mixer, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2933 | HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0, |
| 2934 | HPI_CONTROL_SAMPLECLOCK, &h_control); |
| 2935 | |
| 2936 | if (!err) |
| 2937 | err = hpi_sample_clock_set_local_rate( |
Eliot Blennerhassett | ba94455 | 2011-02-10 17:26:04 +1300 | [diff] [blame] | 2938 | h_control, adapter_fs); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2939 | |
| 2940 | snd_asihpi_proc_init(asihpi); |
| 2941 | |
| 2942 | /* always create, can be enabled or disabled dynamically |
| 2943 | by enable_hwdep module param*/ |
| 2944 | snd_asihpi_hpi_new(asihpi, 0, NULL); |
| 2945 | |
Eliot Blennerhassett | f3d145a | 2011-04-05 20:55:44 +1200 | [diff] [blame] | 2946 | strcpy(card->driver, "ASIHPI"); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2947 | |
| 2948 | sprintf(card->shortname, "AudioScience ASI%4X", asihpi->type); |
| 2949 | sprintf(card->longname, "%s %i", |
| 2950 | card->shortname, asihpi->adapter_index); |
| 2951 | err = snd_card_register(card); |
Eliot Blennerhassett | b2e65c8 | 2011-03-25 15:25:48 +1300 | [diff] [blame] | 2952 | |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2953 | if (!err) { |
| 2954 | hpi_card->snd_card_asihpi = card; |
| 2955 | dev++; |
| 2956 | return 0; |
| 2957 | } |
| 2958 | __nodev: |
| 2959 | snd_card_free(card); |
| 2960 | snd_printk(KERN_ERR "snd_asihpi_probe error %d\n", err); |
| 2961 | return err; |
| 2962 | |
| 2963 | } |
| 2964 | |
| 2965 | static void __devexit snd_asihpi_remove(struct pci_dev *pci_dev) |
| 2966 | { |
| 2967 | struct hpi_adapter *hpi_card = pci_get_drvdata(pci_dev); |
| 2968 | |
| 2969 | snd_card_free(hpi_card->snd_card_asihpi); |
| 2970 | hpi_card->snd_card_asihpi = NULL; |
| 2971 | asihpi_adapter_remove(pci_dev); |
| 2972 | } |
| 2973 | |
| 2974 | static DEFINE_PCI_DEVICE_TABLE(asihpi_pci_tbl) = { |
| 2975 | {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_DSP6205, |
| 2976 | HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0, |
| 2977 | (kernel_ulong_t)HPI_6205}, |
| 2978 | {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_PCI2040, |
| 2979 | HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0, |
| 2980 | (kernel_ulong_t)HPI_6000}, |
| 2981 | {0,} |
| 2982 | }; |
| 2983 | MODULE_DEVICE_TABLE(pci, asihpi_pci_tbl); |
| 2984 | |
| 2985 | static struct pci_driver driver = { |
Takashi Iwai | 3733e42 | 2011-06-10 16:20:20 +0200 | [diff] [blame] | 2986 | .name = KBUILD_MODNAME, |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2987 | .id_table = asihpi_pci_tbl, |
| 2988 | .probe = snd_asihpi_probe, |
| 2989 | .remove = __devexit_p(snd_asihpi_remove), |
| 2990 | #ifdef CONFIG_PM |
| 2991 | /* .suspend = snd_asihpi_suspend, |
| 2992 | .resume = snd_asihpi_resume, */ |
| 2993 | #endif |
| 2994 | }; |
| 2995 | |
| 2996 | static int __init snd_asihpi_init(void) |
| 2997 | { |
| 2998 | asihpi_init(); |
| 2999 | return pci_register_driver(&driver); |
| 3000 | } |
| 3001 | |
| 3002 | static void __exit snd_asihpi_exit(void) |
| 3003 | { |
| 3004 | |
| 3005 | pci_unregister_driver(&driver); |
| 3006 | asihpi_exit(); |
| 3007 | } |
| 3008 | |
| 3009 | module_init(snd_asihpi_init) |
| 3010 | module_exit(snd_asihpi_exit) |
| 3011 | |