speakup: lower default software speech rate
[pandora-kernel.git] / drivers / staging / intel_sst / intel_sst_common.h
1 #ifndef __INTEL_SST_COMMON_H__
2 #define __INTEL_SST_COMMON_H__
3 /*
4  *  intel_sst_common.h - Intel SST Driver for audio engine
5  *
6  *  Copyright (C) 2008-10 Intel Corporation
7  *  Authors:    Vinod Koul <vinod.koul@intel.com>
8  *              Harsha Priya <priya.harsha@intel.com>
9  *              Dharageswari R <dharageswari.r@intel.com>
10  *              KP Jeeja <jeeja.kp@intel.com>
11  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12  *
13  *  This program is free software; you can redistribute it and/or modify
14  *  it under the terms of the GNU General Public License as published by
15  *  the Free Software Foundation; version 2 of the License.
16  *
17  *  This program is distributed in the hope that it will be useful, but
18  *  WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  *  General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License along
23  *  with this program; if not, write to the Free Software Foundation, Inc.,
24  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25  *
26  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27  *
28  *  Common private declarations for SST
29  */
30
31 #define SST_DRIVER_VERSION "1.2.17"
32 #define SST_VERSION_NUM 0x1217
33
34 /* driver names */
35 #define SST_DRV_NAME "intel_sst_driver"
36 #define SST_MRST_PCI_ID 0x080A
37 #define SST_MFLD_PCI_ID 0x082F
38 #define PCI_ID_LENGTH 4
39 #define SST_SUSPEND_DELAY 2000
40 #define FW_CONTEXT_MEM (64*1024)
41
42 enum sst_states {
43         SST_FW_LOADED = 1,
44         SST_FW_RUNNING,
45         SST_UN_INIT,
46         SST_ERROR,
47         SST_SUSPENDED
48 };
49
50 #define MAX_ACTIVE_STREAM       3
51 #define MAX_ENC_STREAM          1
52 #define MAX_AM_HANDLES          1
53 #define ALLOC_TIMEOUT           5000
54 /* SST numbers */
55 #define SST_BLOCK_TIMEOUT       5000
56 #define TARGET_DEV_BLOCK_TIMEOUT        5000
57
58 #define BLOCK_UNINIT            -1
59 #define RX_TIMESLOT_UNINIT      -1
60
61 /* SST register map */
62 #define SST_CSR                 0x00
63 #define SST_PISR                0x08
64 #define SST_PIMR                0x10
65 #define SST_ISRX                0x18
66 #define SST_IMRX                0x28
67 #define SST_IPCX                0x38 /* IPC IA-SST */
68 #define SST_IPCD                0x40 /* IPC SST-IA */
69 #define SST_ISRD                0x20 /* dummy register for shim workaround */
70 #define SST_SHIM_SIZE           0X44
71
72 #define SPI_MODE_ENABLE_BASE_ADDR 0xffae4000
73 #define FW_SIGNATURE_SIZE       4
74
75 /* PMIC and SST hardware states */
76 enum sst_mad_states {
77         SND_MAD_UN_INIT = 0,
78         SND_MAD_INIT_DONE,
79 };
80
81 /* stream states */
82 enum sst_stream_states {
83         STREAM_UN_INIT  = 0,    /* Freed/Not used stream */
84         STREAM_RUNNING  = 1,    /* Running */
85         STREAM_PAUSED   = 2,    /* Paused stream */
86         STREAM_DECODE   = 3,    /* stream is in decoding only state */
87         STREAM_INIT     = 4,    /* stream init, waiting for data */
88 };
89
90
91 enum sst_ram_type {
92         SST_IRAM        = 1,
93         SST_DRAM        = 2,
94 };
95 /* SST shim registers to structure mapping  */
96 union config_status_reg {
97         struct {
98                 u32 mfld_strb:1;
99                 u32 sst_reset:1;
100                 u32 hw_rsvd:3;
101                 u32 sst_clk:2;
102                 u32 bypass:3;
103                 u32 run_stall:1;
104                 u32 rsvd1:2;
105                 u32 strb_cntr_rst:1;
106                 u32 rsvd:18;
107         } part;
108         u32 full;
109 };
110
111 union interrupt_reg {
112         struct {
113                 u32 done_interrupt:1;
114                 u32 busy_interrupt:1;
115                 u32 rsvd:30;
116         } part;
117         u32 full;
118 };
119
120 union sst_pisr_reg {
121         struct {
122                 u32 pssp0:1;
123                 u32 pssp1:1;
124                 u32 rsvd0:3;
125                 u32 dmac:1;
126                 u32 rsvd1:26;
127         } part;
128         u32 full;
129 };
130
131 union sst_pimr_reg {
132         struct {
133                 u32 ssp0:1;
134                 u32 ssp1:1;
135                 u32 rsvd0:3;
136                 u32 dmac:1;
137                 u32 rsvd1:10;
138                 u32 ssp0_sc:1;
139                 u32 ssp1_sc:1;
140                 u32 rsvd2:3;
141                 u32 dmac_sc:1;
142                 u32 rsvd3:10;
143         } part;
144         u32 full;
145 };
146
147
148 struct sst_stream_bufs {
149         struct list_head        node;
150         u32                     size;
151         const char              *addr;
152         u32                     data_copied;
153         bool                    in_use;
154         u32                     offset;
155 };
156
157 struct snd_sst_user_cap_list {
158         unsigned int iov_index; /* index of iov */
159         unsigned long iov_offset; /* offset in iov */
160         unsigned long offset; /* offset in kmem */
161         unsigned long size; /* size copied */
162         struct list_head node;
163 };
164 /*
165 This structure is used to block a user/fw data call to another
166 fw/user call
167 */
168 struct sst_block {
169         bool    condition; /* condition for blocking check */
170         int     ret_code; /* ret code when block is released */
171         void    *data; /* data to be appsed for block if any */
172         bool    on;
173 };
174
175 enum snd_sst_buf_type {
176         SST_BUF_USER_STATIC = 1,
177         SST_BUF_USER_DYNAMIC,
178         SST_BUF_MMAP_STATIC,
179         SST_BUF_MMAP_DYNAMIC,
180 };
181
182 enum snd_src {
183         SST_DRV = 1,
184         MAD_DRV = 2
185 };
186
187 /**
188  * struct stream_info - structure that holds the stream information
189  *
190  * @status : stream current state
191  * @prev : stream prev state
192  * @codec : stream codec
193  * @sst_id : stream id
194  * @ops : stream operation pb/cp/drm...
195  * @bufs: stream buffer list
196  * @lock : stream mutex for protecting state
197  * @pcm_lock : spinlock for pcm path only
198  * @mmapped : is stream mmapped
199  * @sg_index : current stream user buffer index
200  * @cur_ptr : stream user buffer pointer
201  * @buf_entry : current user buffer
202  * @data_blk : stream block for data operations
203  * @ctrl_blk : stream block for ctrl operations
204  * @buf_type : stream user buffer type
205  * @pcm_substream : PCM substream
206  * @period_elapsed : PCM period elapsed callback
207  * @sfreq : stream sampling freq
208  * @decode_ibuf : Decoded i/p buffers pointer
209  * @decode_obuf : Decoded o/p buffers pointer
210  * @decode_isize : Decoded i/p buffers size
211  * @decode_osize : Decoded o/p buffers size
212  * @decode_ibuf_type : Decoded i/p buffer type
213  * @decode_obuf_type : Decoded o/p buffer type
214  * @idecode_alloc : Decode alloc index
215  * @need_draining : stream set for drain
216  * @str_type : stream type
217  * @curr_bytes : current bytes decoded
218  * @cumm_bytes : cummulative bytes decoded
219  * @str_type : stream type
220  * @src : stream source
221  * @device : output device type (medfield only)
222  * @pcm_slot : pcm slot value
223  */
224 struct stream_info {
225         unsigned int            status;
226         unsigned int            prev;
227         u8                      codec;
228         unsigned int            sst_id;
229         unsigned int            ops;
230         struct list_head        bufs;
231         struct mutex            lock; /* mutex */
232         spinlock_t          pcm_lock;
233         bool                    mmapped;
234         unsigned int            sg_index; /*  current buf Index  */
235         unsigned char __user    *cur_ptr; /*  Current static bufs  */
236         struct snd_sst_buf_entry __user *buf_entry;
237         struct sst_block        data_blk; /* stream ops block */
238         struct sst_block        ctrl_blk; /* stream control cmd block */
239         enum snd_sst_buf_type   buf_type;
240         void                    *pcm_substream;
241         void (*period_elapsed) (void *pcm_substream);
242         unsigned int            sfreq;
243         void                    *decode_ibuf, *decode_obuf;
244         unsigned int            decode_isize, decode_osize;
245         u8 decode_ibuf_type, decode_obuf_type;
246         unsigned int            idecode_alloc;
247         unsigned int            need_draining;
248         unsigned int            str_type;
249         u32                     curr_bytes;
250         u32                     cumm_bytes;
251         u32                     src;
252         enum snd_sst_audio_device_type device;
253         u8                      pcm_slot;
254 };
255
256 /*
257  * struct stream_alloc_bloc - this structure is used for blocking the user's
258  * alloc calls to fw's response to alloc calls
259  *
260  * @sst_id : session id of blocked stream
261  * @ops_block : ops block struture
262  */
263 struct stream_alloc_block {
264         int                     sst_id; /* session id of blocked stream */
265         struct sst_block        ops_block; /* ops block struture */
266 };
267
268 #define SST_FW_SIGN "$SST"
269 #define SST_FW_LIB_SIGN "$LIB"
270
271 /*
272  * struct fw_header - FW file headers
273  *
274  * @signature : FW signature
275  * @modules : # of modules
276  * @file_format : version of header format
277  * @reserved : reserved fields
278  */
279 struct fw_header {
280         unsigned char signature[FW_SIGNATURE_SIZE]; /* FW signature */
281         u32 file_size; /* size of fw minus this header */
282         u32 modules; /*  # of modules */
283         u32 file_format; /* version of header format */
284         u32 reserved[4];
285 };
286
287 struct fw_module_header {
288         unsigned char signature[FW_SIGNATURE_SIZE]; /* module signature */
289         u32 mod_size; /* size of module */
290         u32 blocks; /* # of blocks */
291         u32 type; /* codec type, pp lib */
292         u32 entry_point;
293 };
294
295 struct dma_block_info {
296         enum sst_ram_type       type;   /* IRAM/DRAM */
297         u32                     size;   /* Bytes */
298         u32                     ram_offset; /* Offset in I/DRAM */
299         u32                     rsvd;   /* Reserved field */
300 };
301
302 struct ioctl_pvt_data {
303         int                     str_id;
304         int                     pvt_id;
305 };
306
307 struct sst_ipc_msg_wq {
308         union ipc_header        header;
309         char mailbox[SST_MAILBOX_SIZE];
310         struct work_struct      wq;
311 };
312
313 struct mad_ops_wq {
314         int stream_id;
315         enum sst_controls control_op;
316         struct work_struct      wq;
317
318 };
319
320 #define SST_MMAP_PAGES  (640*1024 / PAGE_SIZE)
321 #define SST_MMAP_STEP   (40*1024 / PAGE_SIZE)
322
323 /***
324  * struct intel_sst_drv - driver ops
325  *
326  * @pmic_state : pmic state
327  * @pmic_vendor : pmic vendor detected
328  * @sst_state : current sst device state
329  * @pci_id : PCI device id loaded
330  * @shim : SST shim pointer
331  * @mailbox : SST mailbox pointer
332  * @iram : SST IRAM pointer
333  * @dram : SST DRAM pointer
334  * @shim_phy_add : SST shim phy addr
335  * @ipc_dispatch_list : ipc messages dispatched
336  * @ipc_post_msg_wq : wq to post IPC messages context
337  * @ipc_process_msg : wq to process msgs from FW context
338  * @ipc_process_reply : wq to process reply from FW context
339  * @ipc_post_msg : wq to post reply from FW context
340  * @mad_ops : MAD driver operations registered
341  * @mad_wq : MAD driver wq
342  * @post_msg_wq : wq to post IPC messages
343  * @process_msg_wq : wq to process msgs from FW
344  * @process_reply_wq : wq to process reply from FW
345  * @streams : sst stream contexts
346  * @alloc_block : block structure for alloc
347  * @tgt_dev_blk : block structure for target device
348  * @fw_info_blk : block structure for fw info block
349  * @vol_info_blk : block structure for vol info block
350  * @mute_info_blk : block structure for mute info block
351  * @hs_info_blk : block structure for hs info block
352  * @list_lock : sst driver list lock (deprecated)
353  * @list_spin_lock : sst driver spin lock block
354  * @scard_ops : sst card ops
355  * @pci : sst pci device struture
356  * @active_streams : sst active streams
357  * @sst_lock : sst device lock
358  * @stream_lock : sst stream lock
359  * @unique_id : sst unique id
360  * @stream_cnt : total sst active stream count
361  * @pb_streams : total active pb streams
362  * @cp_streams : total active cp streams
363  * @lpe_stalled : lpe stall status
364  * @pmic_port_instance : active pmic port instance
365  * @rx_time_slot_status : active rx slot
366  * @lpaudio_start : lpaudio status
367  * @audio_start : audio status
368  * @devt_d : pointer to /dev/lpe node
369  * @devt_c : pointer to /dev/lpe_ctrl node
370  * @max_streams : max streams allowed
371  */
372 struct intel_sst_drv {
373         bool                    pmic_state;
374         int                     pmic_vendor;
375         int                     sst_state;
376         unsigned int            pci_id;
377         void __iomem            *shim;
378         void __iomem            *mailbox;
379         void __iomem            *iram;
380         void __iomem            *dram;
381         unsigned int            shim_phy_add;
382         struct list_head        ipc_dispatch_list;
383         struct work_struct      ipc_post_msg_wq;
384         struct sst_ipc_msg_wq   ipc_process_msg;
385         struct sst_ipc_msg_wq   ipc_process_reply;
386         struct sst_ipc_msg_wq   ipc_post_msg;
387         struct mad_ops_wq       mad_ops;
388         wait_queue_head_t       wait_queue;
389         struct workqueue_struct *mad_wq;
390         struct workqueue_struct *post_msg_wq;
391         struct workqueue_struct *process_msg_wq;
392         struct workqueue_struct *process_reply_wq;
393
394         struct stream_info      streams[MAX_NUM_STREAMS];
395         struct stream_alloc_block alloc_block[MAX_ACTIVE_STREAM];
396         struct sst_block        tgt_dev_blk, fw_info_blk, ppp_params_blk,
397                                 vol_info_blk, mute_info_blk, hs_info_blk;
398         struct mutex            list_lock;/* mutex for IPC list locking */
399         spinlock_t      list_spin_lock; /* mutex for IPC list locking */
400         struct snd_pmic_ops     *scard_ops;
401         struct pci_dev          *pci;
402         int active_streams[MAX_NUM_STREAMS];
403         void                    *mmap_mem;
404         struct mutex            sst_lock;
405         struct mutex            stream_lock;
406         unsigned int            mmap_len;
407         unsigned int            unique_id;
408         unsigned int            stream_cnt;     /* total streams */
409         unsigned int            encoded_cnt;    /* enocded streams only */
410         unsigned int            am_cnt;
411         unsigned int            pb_streams;     /* pb streams active */
412         unsigned int            cp_streams;     /* cp streams active */
413         unsigned int            lpe_stalled; /* LPE is stalled or not */
414         unsigned int            pmic_port_instance; /*pmic port instance*/
415         int                     rx_time_slot_status;
416         unsigned int            lpaudio_start;
417                 /* 1 - LPA stream(MP3 pb) in progress*/
418         unsigned int            audio_start;
419         dev_t                   devt_d, devt_c;
420         unsigned int            max_streams;
421         unsigned int            *fw_cntx;
422         unsigned int            fw_cntx_size;
423
424         unsigned int            fw_downloaded;
425 };
426
427 extern struct intel_sst_drv *sst_drv_ctx;
428
429 #define CHIP_REV_REG 0xff108000
430 #define CHIP_REV_ADDR 0x78
431
432 /* misc definitions */
433 #define FW_DWNL_ID 0xFF
434 #define LOOP1 0x11111111
435 #define LOOP2 0x22222222
436 #define LOOP3 0x33333333
437 #define LOOP4 0x44444444
438
439 #define SST_DEFAULT_PMIC_PORT 1 /*audio port*/
440 /* NOTE: status will have +ve for good cases and -ve for error ones */
441 #define MAX_STREAM_FIELD 255
442
443 int sst_alloc_stream(char *params, unsigned int stream_ops, u8 codec,
444                                                 unsigned int session_id);
445 int sst_alloc_stream_response(unsigned int str_id,
446                                 struct snd_sst_alloc_response *response);
447 int sst_stalled(void);
448 int sst_pause_stream(int id);
449 int sst_resume_stream(int id);
450 int sst_enable_rx_timeslot(int status);
451 int sst_drop_stream(int id);
452 int sst_free_stream(int id);
453 int sst_start_stream(int streamID);
454 int sst_play_frame(int streamID);
455 int sst_pcm_play_frame(int str_id, struct sst_stream_bufs *sst_buf);
456 int sst_capture_frame(int streamID);
457 int sst_set_stream_param(int streamID, struct snd_sst_params *str_param);
458 int sst_target_device_select(struct snd_sst_target_device *target_device);
459 int sst_decode(int str_id, struct snd_sst_dbufs *dbufs);
460 int sst_get_decoded_bytes(int str_id, unsigned long long *bytes);
461 int sst_get_fw_info(struct snd_sst_fw_info *info);
462 int sst_get_stream_params(int str_id,
463                 struct snd_sst_get_stream_params *get_params);
464 int sst_get_stream(struct snd_sst_params *str_param);
465 int sst_get_stream_allocated(struct snd_sst_params *str_param,
466                                 struct snd_sst_lib_download **lib_dnld);
467 int sst_drain_stream(int str_id);
468 int sst_get_vol(struct snd_sst_vol *set_vol);
469 int sst_set_vol(struct snd_sst_vol *set_vol);
470 int sst_set_mute(struct snd_sst_mute *set_mute);
471
472
473 void sst_post_message(struct work_struct *work);
474 void sst_process_message(struct work_struct *work);
475 void sst_process_reply(struct work_struct *work);
476 void sst_process_mad_ops(struct work_struct *work);
477 void sst_process_mad_jack_detection(struct work_struct *work);
478
479 long intel_sst_ioctl(struct file *file_ptr, unsigned int cmd,
480                         unsigned long arg);
481 int intel_sst_open(struct inode *i_node, struct file *file_ptr);
482 int intel_sst_open_cntrl(struct inode *i_node, struct file *file_ptr);
483 int intel_sst_release(struct inode *i_node, struct file *file_ptr);
484 int intel_sst_release_cntrl(struct inode *i_node, struct file *file_ptr);
485 int intel_sst_read(struct file *file_ptr, char __user *buf,
486                         size_t count, loff_t *ppos);
487 int intel_sst_write(struct file *file_ptr, const char __user *buf,
488                         size_t count, loff_t *ppos);
489 int intel_sst_mmap(struct file *fp, struct vm_area_struct *vma);
490 ssize_t intel_sst_aio_write(struct kiocb *kiocb, const struct iovec *iov,
491                         unsigned long nr_segs, loff_t  offset);
492 ssize_t intel_sst_aio_read(struct kiocb *kiocb, const struct iovec *iov,
493                         unsigned long nr_segs, loff_t offset);
494
495 int sst_load_fw(const struct firmware *fw, void *context);
496 int sst_load_library(struct snd_sst_lib_download *lib, u8 ops);
497 int sst_spi_mode_enable(void);
498 int sst_get_block_stream(struct intel_sst_drv *sst_drv_ctx);
499
500 int sst_wait_interruptible(struct intel_sst_drv *sst_drv_ctx,
501                                 struct sst_block *block);
502 int sst_wait_interruptible_timeout(struct intel_sst_drv *sst_drv_ctx,
503                 struct sst_block *block, int timeout);
504 int sst_wait_timeout(struct intel_sst_drv *sst_drv_ctx,
505                 struct stream_alloc_block *block);
506 int sst_create_large_msg(struct ipc_post **arg);
507 int sst_create_short_msg(struct ipc_post **arg);
508 void sst_wake_up_alloc_block(struct intel_sst_drv *sst_drv_ctx,
509                 u8 sst_id, int status, void *data);
510 void sst_clear_interrupt(void);
511 int intel_sst_resume(struct pci_dev *pci);
512 int sst_download_fw(void);
513 void free_stream_context(unsigned int str_id);
514 void sst_clean_stream(struct stream_info *stream);
515
516 /*
517  * sst_fill_header - inline to fill sst header
518  *
519  * @header : ipc header
520  * @msg : IPC message to be sent
521  * @large : is ipc large msg
522  * @str_id : stream id
523  *
524  * this function is an inline function that sets the headers before
525  * sending a message
526  */
527 static inline void sst_fill_header(union ipc_header *header,
528                                 int msg, int large, int str_id)
529 {
530         header->part.msg_id = msg;
531         header->part.str_id = str_id;
532         header->part.large = large;
533         header->part.done = 0;
534         header->part.busy = 1;
535         header->part.data = 0;
536 }
537
538 /*
539  * sst_assign_pvt_id - assign a pvt id for stream
540  *
541  * @sst_drv_ctx : driver context
542  *
543  * this inline function assigns a private id for calls that dont have stream
544  * context yet, should be called with lock held
545  */
546 static inline unsigned int sst_assign_pvt_id(struct intel_sst_drv *sst_drv_ctx)
547 {
548         sst_drv_ctx->unique_id++;
549         if (sst_drv_ctx->unique_id >= MAX_NUM_STREAMS)
550                 sst_drv_ctx->unique_id = 1;
551         return sst_drv_ctx->unique_id;
552 }
553
554 /*
555  * sst_init_stream - this function initialzes stream context
556  *
557  * @stream : stream struture
558  * @codec : codec for stream
559  * @sst_id : stream id
560  * @ops : stream operation
561  * @slot : stream pcm slot
562  * @device : device type
563  *
564  * this inline function initialzes stream context for allocated stream
565  */
566 static inline void sst_init_stream(struct stream_info *stream,
567                 int codec, int sst_id, int ops, u8 slot,
568                 enum snd_sst_audio_device_type device)
569 {
570         stream->status = STREAM_INIT;
571         stream->prev = STREAM_UN_INIT;
572         stream->codec = codec;
573         stream->sst_id = sst_id;
574         stream->str_type = 0;
575         stream->ops = ops;
576         stream->data_blk.on = false;
577         stream->data_blk.condition = false;
578         stream->data_blk.ret_code = 0;
579         stream->data_blk.data = NULL;
580         stream->ctrl_blk.on = false;
581         stream->ctrl_blk.condition = false;
582         stream->ctrl_blk.ret_code = 0;
583         stream->ctrl_blk.data = NULL;
584         stream->need_draining = false;
585         stream->decode_ibuf = NULL;
586         stream->decode_isize = 0;
587         stream->mmapped = false;
588         stream->pcm_slot = slot;
589         stream->device = device;
590 }
591
592
593 /*
594  * sst_validate_strid - this function validates the stream id
595  *
596  * @str_id : stream id to be validated
597  *
598  * returns 0 if valid stream
599  */
600 static inline int sst_validate_strid(int str_id)
601 {
602         if (str_id <= 0 || str_id > sst_drv_ctx->max_streams) {
603                 pr_err("SST ERR: invalid stream id : %d MAX_STREAMS:%d\n",
604                                         str_id, sst_drv_ctx->max_streams);
605                 return -EINVAL;
606         } else
607                 return 0;
608 }
609
610 static inline int sst_shim_write(void __iomem *addr, int offset, int value)
611 {
612
613         if (sst_drv_ctx->pci_id == SST_MRST_PCI_ID)
614                 writel(value, addr + SST_ISRD); /*dummy*/
615         writel(value, addr + offset);
616         return 0;
617 }
618
619 static inline int sst_shim_read(void __iomem *addr, int offset)
620 {
621         return readl(addr + offset);
622 }
623 #endif /* __INTEL_SST_COMMON_H__ */