Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /** |
| 2 | * TSIF driver |
| 3 | * |
| 4 | * Kernel API |
| 5 | * |
Hamad Kadmany | 68bd27a | 2013-01-31 14:53:32 +0200 | [diff] [blame] | 6 | * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 and |
| 10 | * only version 2 as published by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | */ |
| 18 | #ifndef _TSIF_API_H_ |
| 19 | #define _TSIF_API_H_ |
| 20 | /** |
| 21 | * Theory of operation |
| 22 | * |
| 23 | * TSIF driver maintains internal cyclic data buffer where |
| 24 | * received TSIF packets are stored. Size of buffer, in packets, |
| 25 | * and its address, may be obtained by tsif_get_info(). |
| 26 | * |
| 27 | * TSIF stream delivered to the client that should register with |
| 28 | * TSIF driver using tsif_attach() |
| 29 | * |
| 30 | * Producer-consumer pattern used. TSIF driver act as producer, |
| 31 | * writing data to the buffer; clientis consumer. |
| 32 | * 2 indexes maintained by the TSIF driver: |
| 33 | * - wi (write index) points to the next item to be written by |
| 34 | * TSIF |
| 35 | * - ri (read index) points to the next item available for read |
| 36 | * by the client. |
| 37 | * Write index advanced by the TSIF driver when new data |
| 38 | * received; |
| 39 | * Read index advanced only when client tell so to the TSIF |
| 40 | * driver by tsif_reclaim_packets() |
| 41 | * |
| 42 | * Consumer may directly access data TSIF buffer between ri and |
| 43 | * wi. When ri==wi, buffer is empty. |
| 44 | * |
| 45 | * TSIF driver notifies client about any change by calling |
| 46 | * notify function. Client should use tsif_get_state() to query |
| 47 | * new state. |
| 48 | */ |
| 49 | |
| 50 | /* bytes in TSIF packet. not customizable */ |
| 51 | #define TSIF_PKT_SIZE (192) |
| 52 | |
| 53 | /** |
| 54 | * tsif_pkt_status - get TSIF packet status |
| 55 | * |
| 56 | * @pkt: TSIF packet location |
| 57 | * |
| 58 | * Return last DWORD of packet, containing status. |
| 59 | * Status dword consists of: |
| 60 | * - 3 low bytes TTS |
| 61 | * - 1 byte (last byte of packet) with status bits |
| 62 | */ |
| 63 | static inline u32 tsif_pkt_status(void *pkt) |
| 64 | { |
| 65 | u32 *x = pkt; |
| 66 | return x[TSIF_PKT_SIZE / sizeof(u32) - 1]; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Status dword parts for status returned by @tsif_pkt_status |
| 71 | */ |
| 72 | #define TSIF_STATUS_TTS(x) ((x) & 0xffffff) |
| 73 | #define TSIF_STATUS_VALID(x) ((x) & (1<<24)) |
| 74 | #define TSIF_STATUS_FIRST(x) ((x) & (1<<25)) |
| 75 | #define TSIF_STATUS_OVFLW(x) ((x) & (1<<26)) |
| 76 | #define TSIF_STATUS_ERROR(x) ((x) & (1<<27)) |
| 77 | #define TSIF_STATUS_NULL(x) ((x) & (1<<28)) |
| 78 | #define TSIF_STATUS_TIMEO(x) ((x) & (1<<30)) |
| 79 | |
| 80 | /** |
| 81 | * enum tsif_state - TSIF device state |
| 82 | * @tsif_state_stopped: Idle state, data acquisition not running |
| 83 | * @tsif_state_running: Data acquisition in progress |
| 84 | * @tsif_state_flushing: Device is flushing |
| 85 | * |
| 86 | * State transition diagram: |
| 87 | * |
| 88 | * init -> tsif_state_stopped |
| 89 | * |
| 90 | * tsif_state_stopped: |
| 91 | * - open -> tsif_state_running |
| 92 | * |
| 93 | * tsif_state_running: |
| 94 | * - close -> tsif_state_flushing |
| 95 | * |
| 96 | * tsif_state_flushing: |
| 97 | * - flushed -> tsif_state_stopped |
| 98 | */ |
| 99 | enum tsif_state { |
| 100 | tsif_state_stopped = 0, |
| 101 | tsif_state_running = 1, |
| 102 | tsif_state_flushing = 2, |
| 103 | tsif_state_error = 3, |
| 104 | }; |
| 105 | |
| 106 | /** |
Joel Nider | 5578bdb | 2011-08-12 09:37:11 +0300 | [diff] [blame] | 107 | * tsif_get_active - return active tsif hardware instance |
| 108 | * |
| 109 | * Return TSIF instance to use (selected by CONFIG_MSM_USE_TSIF1) |
| 110 | */ |
| 111 | int tsif_get_active(void); |
| 112 | |
| 113 | /** |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 114 | * tsif_attach - Attach to the device. |
| 115 | * @id: TSIF device ID, used to identify TSIF instance. |
| 116 | * @notify: client callback, called when |
| 117 | * any client visible TSIF state changed. |
| 118 | * This includes new data available and device state change |
| 119 | * @data: client data, will be passed to @notify |
| 120 | * |
| 121 | * Return TSIF cookie or error code |
| 122 | * |
| 123 | * Should be called prior to any other tsif_XXX function. |
| 124 | */ |
| 125 | void *tsif_attach(int id, void (*notify)(void *client_data), void *client_data); |
Hamad Kadmany | 509b766 | 2012-10-18 14:00:39 +0200 | [diff] [blame] | 126 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 127 | /** |
| 128 | * tsif_detach - detach from device |
| 129 | * @cookie: TSIF cookie previously obtained with tsif_attach() |
| 130 | */ |
| 131 | void tsif_detach(void *cookie); |
Hamad Kadmany | 509b766 | 2012-10-18 14:00:39 +0200 | [diff] [blame] | 132 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 133 | /** |
| 134 | * tsif_get_info - get data buffer info |
| 135 | * @cookie: TSIF cookie previously obtained with tsif_attach() |
| 136 | * @pdata: if not NULL, TSIF data buffer will be stored there |
| 137 | * @psize: if not NULL, TSIF data buffer size, in packets, |
| 138 | * will be stored there |
| 139 | * |
| 140 | * Data buffer information should be queried after each tsif_start() before |
| 141 | * using data; since data buffer will be re-allocated on tsif_start() |
| 142 | */ |
| 143 | void tsif_get_info(void *cookie, void **pdata, int *psize); |
Hamad Kadmany | 509b766 | 2012-10-18 14:00:39 +0200 | [diff] [blame] | 144 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 145 | /** |
| 146 | * tsif_set_mode - set TSIF mode |
| 147 | * @cookie: TSIF cookie previously obtained with tsif_attach() |
| 148 | * @mode: desired mode of operation |
| 149 | * |
| 150 | * Return error code |
| 151 | * |
| 152 | * Mode may be changed only when TSIF device is stopped. |
| 153 | */ |
| 154 | int tsif_set_mode(void *cookie, int mode); |
Hamad Kadmany | 509b766 | 2012-10-18 14:00:39 +0200 | [diff] [blame] | 155 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 156 | /** |
| 157 | * tsif_set_time_limit - set TSIF time limit |
| 158 | * @cookie: TSIF cookie previously obtained with tsif_attach() |
| 159 | * @value: desired time limit, 0 to disable |
| 160 | * |
| 161 | * Return error code |
| 162 | * |
| 163 | * Time limit may be changed only when TSIF device is stopped. |
| 164 | */ |
| 165 | int tsif_set_time_limit(void *cookie, u32 value); |
Hamad Kadmany | 509b766 | 2012-10-18 14:00:39 +0200 | [diff] [blame] | 166 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 167 | /** |
| 168 | * tsif_set_buf_config - configure data buffer |
| 169 | * |
| 170 | * @cookie: TSIF cookie previously obtained with tsif_attach() |
| 171 | * @pkts_in_chunk: requested number of packets per chunk |
| 172 | * @chunks_in_buf: requested number of chunks in buffer |
| 173 | * |
| 174 | * Return error code |
| 175 | * |
| 176 | * Parameter selection criteria: |
| 177 | * |
| 178 | * - @pkts_in_chunk defines size of DMA transfer and, in turn, time between |
| 179 | * consecutive DMA transfers. Increase @pkts_in_chunk reduces chance for |
| 180 | * hardware overflow. If TSIF stats reports overflows, increase it. |
| 181 | * |
| 182 | * - @chunks_in_buf * @pkts_in_chunk defines total buffer size. Increase this |
| 183 | * parameter if client latency is large and TSIF reports "soft drop" in its |
| 184 | * stats |
| 185 | */ |
| 186 | int tsif_set_buf_config(void *cookie, u32 pkts_in_chunk, u32 chunks_in_buf); |
Hamad Kadmany | 509b766 | 2012-10-18 14:00:39 +0200 | [diff] [blame] | 187 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 188 | /** |
| 189 | * tsif_get_state - query current data buffer information |
| 190 | * @cookie: TSIF cookie previously obtained with tsif_attach() |
| 191 | * @ri: if not NULL, read index will be stored here |
| 192 | * @wi: if not NULL, write index will be stored here |
| 193 | * @state: if not NULL, state will be stored here |
| 194 | */ |
| 195 | void tsif_get_state(void *cookie, int *ri, int *wi, enum tsif_state *state); |
Hamad Kadmany | 509b766 | 2012-10-18 14:00:39 +0200 | [diff] [blame] | 196 | |
| 197 | /** |
| 198 | * tsif_set_clk_inverse - set whether to inverse the clock signal. |
| 199 | * @cookie: TSIF cookie previously obtained with tsif_attach() |
| 200 | * @inverse: 1 to inverse the clock, 0 otherwise. Default is 0. |
| 201 | * |
| 202 | * Return error code |
| 203 | * |
| 204 | * Setting may be changed only when TSIF device is stopped. |
| 205 | */ |
| 206 | int tsif_set_clk_inverse(void *cookie, int inverse); |
| 207 | |
| 208 | /** |
| 209 | * tsif_set_data_inverse - set whether to inverse the data signal. |
| 210 | * @cookie: TSIF cookie previously obtained with tsif_attach() |
| 211 | * @inverse: 1 to inverse the clock, 0 otherwise. Default is 0. |
| 212 | * |
| 213 | * Return error code |
| 214 | * |
| 215 | * Setting may be changed only when TSIF device is stopped. |
| 216 | */ |
| 217 | int tsif_set_data_inverse(void *cookie, int inverse); |
| 218 | |
| 219 | /** |
| 220 | * tsif_set_sync_inverse - set whether to inverse the sync signal. |
| 221 | * @cookie: TSIF cookie previously obtained with tsif_attach() |
| 222 | * @inverse: 1 to inverse the clock, 0 otherwise. Default is 0. |
| 223 | * |
| 224 | * Return error code |
| 225 | * |
| 226 | * Setting may be changed only when TSIF device is stopped. |
| 227 | */ |
| 228 | int tsif_set_sync_inverse(void *cookie, int inverse); |
| 229 | |
| 230 | /** |
| 231 | * tsif_set_enable_inverse - set whether to inverse the enable signal. |
| 232 | * @cookie: TSIF cookie previously obtained with tsif_attach() |
| 233 | * @inverse: 1 to inverse the clock, 0 otherwise. Default is 0. |
| 234 | * |
| 235 | * Return error code |
| 236 | * |
| 237 | * Setting may be changed only when TSIF device is stopped. |
| 238 | */ |
| 239 | int tsif_set_enable_inverse(void *cookie, int inverse); |
| 240 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 241 | /** |
| 242 | * tsif_start - start data acquisition |
| 243 | * @cookie: TSIF cookie previously obtained with tsif_attach() |
| 244 | * |
| 245 | * Return error code |
| 246 | */ |
| 247 | int tsif_start(void *cookie); |
Hamad Kadmany | 509b766 | 2012-10-18 14:00:39 +0200 | [diff] [blame] | 248 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 249 | /** |
| 250 | * tsif_stop - stop data acquisition |
| 251 | * @cookie: TSIF cookie previously obtained with tsif_attach() |
| 252 | * |
| 253 | * Data buffer allocated during this function call; thus client should |
| 254 | * query data buffer info using tsif_get_info() and reset its data pointers. |
| 255 | */ |
| 256 | void tsif_stop(void *cookie); |
Hamad Kadmany | 509b766 | 2012-10-18 14:00:39 +0200 | [diff] [blame] | 257 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 258 | /** |
Hamad Kadmany | 68bd27a | 2013-01-31 14:53:32 +0200 | [diff] [blame] | 259 | * tsif_get_ref_clk_counter - return the TSIF clock reference (TCR) counter. |
| 260 | * @cookie: TSIF cookie previously obtained with tsif_attach() |
| 261 | * @tcr_counter: the value of TCR counter |
| 262 | * |
| 263 | * Return error code |
| 264 | * |
| 265 | * TCR increments at a rate equal to 27 MHz/256 = 105.47 kHz. |
| 266 | */ |
| 267 | int tsif_get_ref_clk_counter(void *cookie, u32 *tcr_counter); |
| 268 | |
| 269 | /** |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 270 | * tsif_reclaim_packets - inform that buffer space may be reclaimed |
| 271 | * @cookie: TSIF cookie previously obtained with tsif_attach() |
| 272 | * @ri: new value for read index |
| 273 | */ |
| 274 | void tsif_reclaim_packets(void *cookie, int ri); |
| 275 | |
| 276 | #endif /* _TSIF_API_H_ */ |
| 277 | |