blob: 36bdf45da3e68598704b301e9a86f8a90db235a1 [file] [log] [blame]
Hamad Kadmany090709b2013-01-06 12:08:13 +02001/* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
Joel Nider5556a852011-10-16 10:52:13 +02002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
Joel Nider435ad8e2011-12-14 16:53:30 +020013#include <linux/module.h> /* Just for modules */
14#include <linux/kernel.h> /* Only for KERN_INFO */
15#include <linux/err.h> /* Error macros */
16#include <linux/list.h> /* Linked list */
Joel Nider5556a852011-10-16 10:52:13 +020017#include <linux/cdev.h>
Joel Nider435ad8e2011-12-14 16:53:30 +020018#include <linux/init.h> /* Needed for the macros */
19#include <linux/io.h> /* IO macros */
20#include <linux/device.h> /* Device drivers need this */
21#include <linux/sched.h> /* Externally defined globals */
22#include <linux/pm_runtime.h> /* Runtime power management */
Joel Nider5556a852011-10-16 10:52:13 +020023#include <linux/fs.h>
Joel Nider435ad8e2011-12-14 16:53:30 +020024#include <linux/uaccess.h> /* copy_to_user */
Joel Nider5556a852011-10-16 10:52:13 +020025#include <linux/slab.h> /* kfree, kzalloc */
Joel Nider435ad8e2011-12-14 16:53:30 +020026#include <linux/ioport.h> /* XXX_ mem_region */
27#include <linux/dma-mapping.h> /* dma_XXX */
Hamad Kadmany090709b2013-01-06 12:08:13 +020028#include <linux/dmapool.h> /* DMA pools */
Joel Nider435ad8e2011-12-14 16:53:30 +020029#include <linux/delay.h> /* msleep */
30#include <linux/platform_device.h>
Joel Nider5556a852011-10-16 10:52:13 +020031#include <linux/clk.h>
Joel Nider435ad8e2011-12-14 16:53:30 +020032#include <linux/poll.h> /* poll() file op */
33#include <linux/wait.h> /* wait() macros, sleeping */
34#include <linux/tspp.h> /* tspp functions */
Joel Nider5556a852011-10-16 10:52:13 +020035#include <linux/bitops.h> /* BIT() macro */
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -070036#include <linux/regulator/consumer.h>
Joel Nider435ad8e2011-12-14 16:53:30 +020037#include <mach/sps.h> /* BAM stuff */
Joel Nider5556a852011-10-16 10:52:13 +020038#include <mach/gpio.h>
Joel Nider435ad8e2011-12-14 16:53:30 +020039#include <linux/wakelock.h> /* Locking functions */
Hamad Kadmany81cee052012-11-29 14:15:57 +020040#include <linux/timer.h> /* Timer services */
41#include <linux/jiffies.h> /* Jiffies counter */
Joel Nider5556a852011-10-16 10:52:13 +020042#include <mach/dma.h>
43#include <mach/msm_tspp.h>
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -070044#include <mach/rpm-regulator-smd.h>
Joel Nider5556a852011-10-16 10:52:13 +020045#include <linux/debugfs.h>
Liron Kuch59339922013-01-01 18:29:47 +020046#include <linux/of.h>
47#include <linux/of_gpio.h>
Liron Kuch275c0b32013-02-10 15:19:32 +020048#include <linux/string.h>
Hamad Kadmanyef5ce992013-06-03 09:30:50 +030049#include <mach/msm_bus.h>
Joel Nider5556a852011-10-16 10:52:13 +020050
51/*
52 * General defines
53 */
Joel Nider5556a852011-10-16 10:52:13 +020054#define TSPP_TSIF_INSTANCES 2
Liron Kuch275c0b32013-02-10 15:19:32 +020055#define TSPP_GPIOS_PER_TSIF 4
Joel Nider5556a852011-10-16 10:52:13 +020056#define TSPP_FILTER_TABLES 3
Joel Nider435ad8e2011-12-14 16:53:30 +020057#define TSPP_MAX_DEVICES 1
Joel Nider5556a852011-10-16 10:52:13 +020058#define TSPP_NUM_CHANNELS 16
59#define TSPP_NUM_PRIORITIES 16
60#define TSPP_NUM_KEYS 8
61#define INVALID_CHANNEL 0xFFFFFFFF
Hamad Kadmany81cee052012-11-29 14:15:57 +020062
63/*
64 * BAM descriptor FIFO size (in number of descriptors).
65 * Max number of descriptors allowed by SPS which is 8K-1.
Hamad Kadmany81cee052012-11-29 14:15:57 +020066 */
Hamad Kadmany24020352013-05-22 12:54:18 +030067#define TSPP_SPS_DESCRIPTOR_COUNT (8 * 1024 - 1)
Joel Nider5556a852011-10-16 10:52:13 +020068#define TSPP_PACKET_LENGTH 188
69#define TSPP_MIN_BUFFER_SIZE (TSPP_PACKET_LENGTH)
Hamad Kadmany81cee052012-11-29 14:15:57 +020070
71/* Max descriptor buffer size allowed by SPS */
72#define TSPP_MAX_BUFFER_SIZE (32 * 1024 - 1)
73
74/*
Hamad Kadmany090709b2013-01-06 12:08:13 +020075 * Returns whether to use DMA pool for TSPP output buffers.
76 * For buffers smaller than page size, using DMA pool
77 * provides better memory utilization as dma_alloc_coherent
78 * allocates minimum of page size.
79 */
80#define TSPP_USE_DMA_POOL(buff_size) ((buff_size) < PAGE_SIZE)
81
82/*
Hamad Kadmany81cee052012-11-29 14:15:57 +020083 * Max allowed TSPP buffers/descriptors.
84 * If SPS desc FIFO holds X descriptors, we can queue up to X-1 descriptors.
85 */
86#define TSPP_NUM_BUFFERS (TSPP_SPS_DESCRIPTOR_COUNT - 1)
Joel Nider5556a852011-10-16 10:52:13 +020087#define TSPP_TSIF_DEFAULT_TIME_LIMIT 60
88#define SPS_DESCRIPTOR_SIZE 8
89#define MIN_ACCEPTABLE_BUFFER_COUNT 2
Joel Nider435ad8e2011-12-14 16:53:30 +020090#define TSPP_DEBUG(msg...)
Joel Nider5556a852011-10-16 10:52:13 +020091
92/*
93 * TSIF register offsets
94 */
95#define TSIF_STS_CTL_OFF (0x0)
96#define TSIF_TIME_LIMIT_OFF (0x4)
97#define TSIF_CLK_REF_OFF (0x8)
98#define TSIF_LPBK_FLAGS_OFF (0xc)
99#define TSIF_LPBK_DATA_OFF (0x10)
100#define TSIF_TEST_CTL_OFF (0x14)
101#define TSIF_TEST_MODE_OFF (0x18)
102#define TSIF_TEST_RESET_OFF (0x1c)
103#define TSIF_TEST_EXPORT_OFF (0x20)
104#define TSIF_TEST_CURRENT_OFF (0x24)
105
106#define TSIF_DATA_PORT_OFF (0x100)
107
108/* bits for TSIF_STS_CTL register */
109#define TSIF_STS_CTL_EN_IRQ BIT(28)
110#define TSIF_STS_CTL_PACK_AVAIL BIT(27)
111#define TSIF_STS_CTL_1ST_PACKET BIT(26)
112#define TSIF_STS_CTL_OVERFLOW BIT(25)
113#define TSIF_STS_CTL_LOST_SYNC BIT(24)
114#define TSIF_STS_CTL_TIMEOUT BIT(23)
115#define TSIF_STS_CTL_INV_SYNC BIT(21)
116#define TSIF_STS_CTL_INV_NULL BIT(20)
117#define TSIF_STS_CTL_INV_ERROR BIT(19)
118#define TSIF_STS_CTL_INV_ENABLE BIT(18)
119#define TSIF_STS_CTL_INV_DATA BIT(17)
120#define TSIF_STS_CTL_INV_CLOCK BIT(16)
121#define TSIF_STS_CTL_SPARE BIT(15)
122#define TSIF_STS_CTL_EN_NULL BIT(11)
123#define TSIF_STS_CTL_EN_ERROR BIT(10)
124#define TSIF_STS_CTL_LAST_BIT BIT(9)
125#define TSIF_STS_CTL_EN_TIME_LIM BIT(8)
126#define TSIF_STS_CTL_EN_TCR BIT(7)
127#define TSIF_STS_CTL_TEST_MODE BIT(6)
Joel Nider435ad8e2011-12-14 16:53:30 +0200128#define TSIF_STS_CTL_MODE_2 BIT(5)
Joel Nider5556a852011-10-16 10:52:13 +0200129#define TSIF_STS_CTL_EN_DM BIT(4)
130#define TSIF_STS_CTL_STOP BIT(3)
131#define TSIF_STS_CTL_START BIT(0)
132
133/*
134 * TSPP register offsets
135 */
Liron Kuch72b78552012-10-30 17:47:50 +0200136#define TSPP_RST 0x00
Joel Nider5556a852011-10-16 10:52:13 +0200137#define TSPP_CLK_CONTROL 0x04
Liron Kuch72b78552012-10-30 17:47:50 +0200138#define TSPP_CONFIG 0x08
139#define TSPP_CONTROL 0x0C
Joel Nider5556a852011-10-16 10:52:13 +0200140#define TSPP_PS_DISABLE 0x10
Liron Kuch72b78552012-10-30 17:47:50 +0200141#define TSPP_MSG_IRQ_STATUS 0x14
Joel Nider5556a852011-10-16 10:52:13 +0200142#define TSPP_MSG_IRQ_MASK 0x18
143#define TSPP_IRQ_STATUS 0x1C
144#define TSPP_IRQ_MASK 0x20
145#define TSPP_IRQ_CLEAR 0x24
146#define TSPP_PIPE_ERROR_STATUS(_n) (0x28 + (_n << 2))
Liron Kuch72b78552012-10-30 17:47:50 +0200147#define TSPP_STATUS 0x68
148#define TSPP_CURR_TSP_HEADER 0x6C
149#define TSPP_CURR_PID_FILTER 0x70
150#define TSPP_SYSTEM_KEY(_n) (0x74 + (_n << 2))
151#define TSPP_CBC_INIT_VAL(_n) (0x94 + (_n << 2))
152#define TSPP_DATA_KEY_RESET 0x9C
Joel Nider5556a852011-10-16 10:52:13 +0200153#define TSPP_KEY_VALID 0xA0
154#define TSPP_KEY_ERROR 0xA4
155#define TSPP_TEST_CTRL 0xA8
Liron Kuch72b78552012-10-30 17:47:50 +0200156#define TSPP_VERSION 0xAC
Joel Nider5556a852011-10-16 10:52:13 +0200157#define TSPP_GENERICS 0xB0
Liron Kuch72b78552012-10-30 17:47:50 +0200158#define TSPP_NOP 0xB4
Joel Nider5556a852011-10-16 10:52:13 +0200159
160/*
161 * Register bit definitions
162 */
163/* TSPP_RST */
164#define TSPP_RST_RESET BIT(0)
165
166/* TSPP_CLK_CONTROL */
167#define TSPP_CLK_CONTROL_FORCE_CRYPTO BIT(9)
168#define TSPP_CLK_CONTROL_FORCE_PES_PL BIT(8)
169#define TSPP_CLK_CONTROL_FORCE_PES_AF BIT(7)
170#define TSPP_CLK_CONTROL_FORCE_RAW_CTRL BIT(6)
171#define TSPP_CLK_CONTROL_FORCE_PERF_CNT BIT(5)
172#define TSPP_CLK_CONTROL_FORCE_CTX_SEARCH BIT(4)
173#define TSPP_CLK_CONTROL_FORCE_TSP_PROC BIT(3)
174#define TSPP_CLK_CONTROL_FORCE_CONS_AHB2MEM BIT(2)
175#define TSPP_CLK_CONTROL_FORCE_TS_AHB2MEM BIT(1)
176#define TSPP_CLK_CONTROL_SET_CLKON BIT(0)
177
178/* TSPP_CONFIG */
179#define TSPP_CONFIG_SET_PACKET_LENGTH(_a, _b) (_a = (_a & 0xF0) | \
180((_b & 0xF) << 8))
181#define TSPP_CONFIG_GET_PACKET_LENGTH(_a) ((_a >> 8) & 0xF)
182#define TSPP_CONFIG_DUP_WITH_DISC_EN BIT(7)
183#define TSPP_CONFIG_PES_SYNC_ERROR_MASK BIT(6)
184#define TSPP_CONFIG_PS_LEN_ERR_MASK BIT(5)
185#define TSPP_CONFIG_PS_CONT_ERR_UNSP_MASK BIT(4)
186#define TSPP_CONFIG_PS_CONT_ERR_MASK BIT(3)
187#define TSPP_CONFIG_PS_DUP_TSP_MASK BIT(2)
188#define TSPP_CONFIG_TSP_ERR_IND_MASK BIT(1)
189#define TSPP_CONFIG_TSP_SYNC_ERR_MASK BIT(0)
190
191/* TSPP_CONTROL */
192#define TSPP_CONTROL_PID_FILTER_LOCK BIT(5)
193#define TSPP_CONTROL_FORCE_KEY_CALC BIT(4)
194#define TSPP_CONTROL_TSP_CONS_SRC_DIS BIT(3)
195#define TSPP_CONTROL_TSP_TSIF1_SRC_DIS BIT(2)
196#define TSPP_CONTROL_TSP_TSIF0_SRC_DIS BIT(1)
197#define TSPP_CONTROL_PERF_COUNT_INIT BIT(0)
198
199/* TSPP_MSG_IRQ_STATUS + TSPP_MSG_IRQ_MASK */
200#define TSPP_MSG_TSPP_IRQ BIT(2)
201#define TSPP_MSG_TSIF_1_IRQ BIT(1)
202#define TSPP_MSG_TSIF_0_IRQ BIT(0)
203
204/* TSPP_IRQ_STATUS + TSPP_IRQ_MASK + TSPP_IRQ_CLEAR */
Liron Kuch72b78552012-10-30 17:47:50 +0200205#define TSPP_IRQ_STATUS_TSP_RD_CMPL BIT(19)
206#define TSPP_IRQ_STATUS_KEY_ERROR BIT(18)
Joel Nider5556a852011-10-16 10:52:13 +0200207#define TSPP_IRQ_STATUS_KEY_SWITCHED_BAD BIT(17)
208#define TSPP_IRQ_STATUS_KEY_SWITCHED BIT(16)
209#define TSPP_IRQ_STATUS_PS_BROKEN(_n) BIT((_n))
210
211/* TSPP_PIPE_ERROR_STATUS */
Liron Kuch72b78552012-10-30 17:47:50 +0200212#define TSPP_PIPE_PES_SYNC_ERROR BIT(3)
213#define TSPP_PIPE_PS_LENGTH_ERROR BIT(2)
Joel Nider5556a852011-10-16 10:52:13 +0200214#define TSPP_PIPE_PS_CONTINUITY_ERROR BIT(1)
Liron Kuch72b78552012-10-30 17:47:50 +0200215#define TSPP_PIP_PS_LOST_START BIT(0)
Joel Nider5556a852011-10-16 10:52:13 +0200216
217/* TSPP_STATUS */
Liron Kuch72b78552012-10-30 17:47:50 +0200218#define TSPP_STATUS_TSP_PKT_AVAIL BIT(10)
219#define TSPP_STATUS_TSIF1_DM_REQ BIT(6)
220#define TSPP_STATUS_TSIF0_DM_REQ BIT(2)
221#define TSPP_CURR_FILTER_TABLE BIT(0)
Joel Nider5556a852011-10-16 10:52:13 +0200222
223/* TSPP_GENERICS */
Liron Kuch72b78552012-10-30 17:47:50 +0200224#define TSPP_GENERICS_CRYPTO_GEN BIT(12)
Joel Nider5556a852011-10-16 10:52:13 +0200225#define TSPP_GENERICS_MAX_CONS_PIPES BIT(7)
Liron Kuch72b78552012-10-30 17:47:50 +0200226#define TSPP_GENERICS_MAX_PIPES BIT(2)
227#define TSPP_GENERICS_TSIF_1_GEN BIT(1)
228#define TSPP_GENERICS_TSIF_0_GEN BIT(0)
Joel Nider5556a852011-10-16 10:52:13 +0200229
230/*
231 * TSPP memory regions
232 */
233#define TSPP_PID_FILTER_TABLE0 0x800
234#define TSPP_PID_FILTER_TABLE1 0x880
235#define TSPP_PID_FILTER_TABLE2 0x900
236#define TSPP_GLOBAL_PERFORMANCE 0x980 /* see tspp_global_performance */
237#define TSPP_PIPE_CONTEXT 0x990 /* see tspp_pipe_context */
238#define TSPP_PIPE_PERFORMANCE 0x998 /* see tspp_pipe_performance */
239#define TSPP_TSP_BUFF_WORD(_n) (0xC10 + (_n << 2))
240#define TSPP_DATA_KEY 0xCD0
241
Joel Nider5556a852011-10-16 10:52:13 +0200242struct debugfs_entry {
243 const char *name;
244 mode_t mode;
245 int offset;
246};
247
248static const struct debugfs_entry debugfs_tsif_regs[] = {
249 {"sts_ctl", S_IRUGO | S_IWUSR, TSIF_STS_CTL_OFF},
250 {"time_limit", S_IRUGO | S_IWUSR, TSIF_TIME_LIMIT_OFF},
251 {"clk_ref", S_IRUGO | S_IWUSR, TSIF_CLK_REF_OFF},
252 {"lpbk_flags", S_IRUGO | S_IWUSR, TSIF_LPBK_FLAGS_OFF},
253 {"lpbk_data", S_IRUGO | S_IWUSR, TSIF_LPBK_DATA_OFF},
254 {"test_ctl", S_IRUGO | S_IWUSR, TSIF_TEST_CTL_OFF},
255 {"test_mode", S_IRUGO | S_IWUSR, TSIF_TEST_MODE_OFF},
256 {"test_reset", S_IWUSR, TSIF_TEST_RESET_OFF},
257 {"test_export", S_IRUGO | S_IWUSR, TSIF_TEST_EXPORT_OFF},
258 {"test_current", S_IRUGO, TSIF_TEST_CURRENT_OFF},
259 {"data_port", S_IRUSR, TSIF_DATA_PORT_OFF},
260};
261
262static const struct debugfs_entry debugfs_tspp_regs[] = {
263 {"rst", S_IRUGO | S_IWUSR, TSPP_RST},
264 {"clk_control", S_IRUGO | S_IWUSR, TSPP_CLK_CONTROL},
265 {"config", S_IRUGO | S_IWUSR, TSPP_CONFIG},
266 {"control", S_IRUGO | S_IWUSR, TSPP_CONTROL},
267 {"ps_disable", S_IRUGO | S_IWUSR, TSPP_PS_DISABLE},
268 {"msg_irq_status", S_IRUGO | S_IWUSR, TSPP_MSG_IRQ_STATUS},
269 {"msg_irq_mask", S_IRUGO | S_IWUSR, TSPP_MSG_IRQ_MASK},
270 {"irq_status", S_IRUGO | S_IWUSR, TSPP_IRQ_STATUS},
271 {"irq_mask", S_IRUGO | S_IWUSR, TSPP_IRQ_MASK},
272 {"irq_clear", S_IRUGO | S_IWUSR, TSPP_IRQ_CLEAR},
273 /* {"pipe_error_status",S_IRUGO | S_IWUSR, TSPP_PIPE_ERROR_STATUS}, */
274 {"status", S_IRUGO | S_IWUSR, TSPP_STATUS},
275 {"curr_tsp_header", S_IRUGO | S_IWUSR, TSPP_CURR_TSP_HEADER},
276 {"curr_pid_filter", S_IRUGO | S_IWUSR, TSPP_CURR_PID_FILTER},
277 /* {"system_key", S_IRUGO | S_IWUSR, TSPP_SYSTEM_KEY}, */
278 /* {"cbc_init_val", S_IRUGO | S_IWUSR, TSPP_CBC_INIT_VAL}, */
279 {"data_key_reset", S_IRUGO | S_IWUSR, TSPP_DATA_KEY_RESET},
280 {"key_valid", S_IRUGO | S_IWUSR, TSPP_KEY_VALID},
281 {"key_error", S_IRUGO | S_IWUSR, TSPP_KEY_ERROR},
282 {"test_ctrl", S_IRUGO | S_IWUSR, TSPP_TEST_CTRL},
283 {"version", S_IRUGO | S_IWUSR, TSPP_VERSION},
284 {"generics", S_IRUGO | S_IWUSR, TSPP_GENERICS},
285 {"pid_filter_table0", S_IRUGO | S_IWUSR, TSPP_PID_FILTER_TABLE0},
286 {"pid_filter_table1", S_IRUGO | S_IWUSR, TSPP_PID_FILTER_TABLE1},
287 {"pid_filter_table2", S_IRUGO | S_IWUSR, TSPP_PID_FILTER_TABLE2},
288 {"global_performance", S_IRUGO | S_IWUSR, TSPP_GLOBAL_PERFORMANCE},
289 {"pipe_context", S_IRUGO | S_IWUSR, TSPP_PIPE_CONTEXT},
290 {"pipe_performance", S_IRUGO | S_IWUSR, TSPP_PIPE_PERFORMANCE},
291 {"data_key", S_IRUGO | S_IWUSR, TSPP_DATA_KEY}
292};
293
Joel Nider5556a852011-10-16 10:52:13 +0200294struct tspp_pid_filter {
295 u32 filter; /* see FILTER_ macros */
296 u32 config; /* see FILTER_ macros */
297};
298
299/* tsp_info */
300#define FILTER_HEADER_ERROR_MASK BIT(7)
301#define FILTER_TRANS_END_DISABLE BIT(6)
302#define FILTER_DEC_ON_ERROR_EN BIT(5)
303#define FILTER_DECRYPT BIT(4)
304#define FILTER_HAS_ENCRYPTION(_p) (_p->config & FILTER_DECRYPT)
305#define FILTER_GET_PIPE_NUMBER0(_p) (_p->config & 0xF)
306#define FILTER_SET_PIPE_NUMBER0(_p, _b) (_p->config = \
307 (_p->config & ~0xF) | (_b & 0xF))
308#define FILTER_GET_PIPE_PROCESS0(_p) ((_p->filter >> 30) & 0x3)
309#define FILTER_SET_PIPE_PROCESS0(_p, _b) (_p->filter = \
310 (_p->filter & ~(0x3<<30)) | ((_b & 0x3) << 30))
311#define FILTER_GET_PIPE_PID(_p) ((_p->filter >> 13) & 0x1FFF)
312#define FILTER_SET_PIPE_PID(_p, _b) (_p->filter = \
313 (_p->filter & ~(0x1FFF<<13)) | ((_b & 0x1FFF) << 13))
314#define FILTER_GET_PID_MASK(_p) (_p->filter & 0x1FFF)
315#define FILTER_SET_PID_MASK(_p, _b) (_p->filter = \
316 (_p->filter & ~0x1FFF) | (_b & 0x1FFF))
317#define FILTER_GET_PIPE_PROCESS1(_p) ((_p->config >> 30) & 0x3)
318#define FILTER_SET_PIPE_PROCESS1(_p, _b) (_p->config = \
319 (_p->config & ~(0x3<<30)) | ((_b & 0x3) << 30))
320#define FILTER_GET_KEY_NUMBER(_p) ((_p->config >> 8) & 0x7)
321#define FILTER_SET_KEY_NUMBER(_p, _b) (_p->config = \
322 (_p->config & ~(0x7<<8)) | ((_b & 0x7) << 8))
323
324struct tspp_global_performance_regs {
325 u32 tsp_total;
326 u32 tsp_ignored;
327 u32 tsp_error;
328 u32 tsp_sync;
329};
330
331struct tspp_pipe_context_regs {
332 u16 pes_bytes_left;
333 u16 count;
334 u32 tsif_suffix;
335} __packed;
336#define CONTEXT_GET_STATE(_a) (_a & 0x3)
337#define CONTEXT_UNSPEC_LENGTH BIT(11)
338#define CONTEXT_GET_CONT_COUNT(_a) ((_a >> 12) & 0xF)
339
Hamad Kadmany81cee052012-11-29 14:15:57 +0200340#define MSEC_TO_JIFFIES(msec) ((msec) * HZ / 1000)
341
Joel Nider5556a852011-10-16 10:52:13 +0200342struct tspp_pipe_performance_regs {
343 u32 tsp_total;
344 u32 ps_duplicate_tsp;
345 u32 tsp_no_payload;
346 u32 tsp_broken_ps;
347 u32 ps_total_num;
348 u32 ps_continuity_error;
349 u32 ps_length_error;
350 u32 pes_sync_error;
351};
352
353struct tspp_tsif_device {
354 void __iomem *base;
355 u32 time_limit;
356 u32 ref_count;
Joel Nider435ad8e2011-12-14 16:53:30 +0200357 enum tspp_tsif_mode mode;
Hamad Kadmany92705b32012-10-23 14:15:41 +0200358 int clock_inverse;
359 int data_inverse;
360 int sync_inverse;
361 int enable_inverse;
Hamad Kadmany44307d32012-11-25 09:49:51 +0200362 u32 tsif_irq;
Joel Nider5556a852011-10-16 10:52:13 +0200363
364 /* debugfs */
Joel Nider5556a852011-10-16 10:52:13 +0200365 struct dentry *dent_tsif;
366 struct dentry *debugfs_tsif_regs[ARRAY_SIZE(debugfs_tsif_regs)];
Hamad Kadmany44307d32012-11-25 09:49:51 +0200367 u32 stat_rx;
368 u32 stat_overflow;
369 u32 stat_lost_sync;
370 u32 stat_timeout;
Joel Nider5556a852011-10-16 10:52:13 +0200371};
372
373enum tspp_buf_state {
374 TSPP_BUF_STATE_EMPTY, /* buffer has been allocated, but not waiting */
375 TSPP_BUF_STATE_WAITING, /* buffer is waiting to be filled */
Joel Nider435ad8e2011-12-14 16:53:30 +0200376 TSPP_BUF_STATE_DATA, /* buffer is not empty and can be read */
377 TSPP_BUF_STATE_LOCKED /* buffer is being read by a client */
Joel Nider5556a852011-10-16 10:52:13 +0200378};
379
380struct tspp_mem_buffer {
Joel Nider435ad8e2011-12-14 16:53:30 +0200381 struct tspp_mem_buffer *next;
382 struct sps_mem_buffer sps;
383 struct tspp_data_descriptor desc; /* buffer descriptor for kernel api */
Joel Nider5556a852011-10-16 10:52:13 +0200384 enum tspp_buf_state state;
385 size_t filled; /* how much data this buffer is holding */
386 int read_index; /* where to start reading data from */
387};
388
389/* this represents each char device 'channel' */
390struct tspp_channel {
391 struct cdev cdev;
392 struct device *dd;
Joel Nider435ad8e2011-12-14 16:53:30 +0200393 struct tspp_device *pdev; /* can use container_of instead? */
Joel Nider5556a852011-10-16 10:52:13 +0200394 struct sps_pipe *pipe;
395 struct sps_connect config;
396 struct sps_register_event event;
Joel Nider435ad8e2011-12-14 16:53:30 +0200397 struct tspp_mem_buffer *data; /* list of buffers */
398 struct tspp_mem_buffer *read; /* first buffer ready to be read */
399 struct tspp_mem_buffer *waiting; /* first outstanding transfer */
400 struct tspp_mem_buffer *locked; /* buffer currently being read */
Joel Nider5556a852011-10-16 10:52:13 +0200401 wait_queue_head_t in_queue; /* set when data is received */
Joel Nider435ad8e2011-12-14 16:53:30 +0200402 u32 id; /* channel id (0-15) */
403 int used; /* is this channel in use? */
404 int key; /* which encryption key index is used */
405 u32 buffer_size; /* size of the sps transfer buffers */
406 u32 max_buffers; /* how many buffers should be allocated */
407 u32 buffer_count; /* how many buffers are actually allocated */
408 u32 filter_count; /* how many filters have been added to this channel */
409 u32 int_freq; /* generate interrupts every x descriptors */
Joel Nider5556a852011-10-16 10:52:13 +0200410 enum tspp_source src;
411 enum tspp_mode mode;
Joel Nider435ad8e2011-12-14 16:53:30 +0200412 tspp_notifier *notifier; /* used only with kernel api */
413 void *notify_data; /* data to be passed with the notifier */
Hamad Kadmany81cee052012-11-29 14:15:57 +0200414 u32 expiration_period_ms; /* notification on partially filled buffers */
415 struct timer_list expiration_timer;
Hamad Kadmany090709b2013-01-06 12:08:13 +0200416 struct dma_pool *dma_pool;
Liron Kuch72b78552012-10-30 17:47:50 +0200417 tspp_memfree *memfree; /* user defined memory free function */
418 void *user_info; /* user cookie passed to memory alloc/free function */
Joel Nider5556a852011-10-16 10:52:13 +0200419};
420
421struct tspp_pid_filter_table {
422 struct tspp_pid_filter filter[TSPP_NUM_PRIORITIES];
423};
424
425struct tspp_key_entry {
426 u32 even_lsb;
427 u32 even_msb;
428 u32 odd_lsb;
429 u32 odd_msb;
430};
431
432struct tspp_key_table {
433 struct tspp_key_entry entry[TSPP_NUM_KEYS];
434};
435
Joel Nider435ad8e2011-12-14 16:53:30 +0200436/* this represents the actual hardware device */
437struct tspp_device {
438 struct list_head devlist; /* list of all devices */
439 struct platform_device *pdev;
440 void __iomem *base;
Hamad Kadmanyef5ce992013-06-03 09:30:50 +0300441 uint32_t tsif_bus_client;
Joel Nider435ad8e2011-12-14 16:53:30 +0200442 unsigned int tspp_irq;
443 unsigned int bam_irq;
444 u32 bam_handle;
445 struct sps_bam_props bam_props;
446 struct wake_lock wake_lock;
447 spinlock_t spinlock;
448 struct tasklet_struct tlet;
449 struct tspp_tsif_device tsif[TSPP_TSIF_INSTANCES];
450 /* clocks */
451 struct clk *tsif_pclk;
452 struct clk *tsif_ref_clk;
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -0700453 /* regulators */
454 struct regulator *tsif_vreg;
Joel Nider435ad8e2011-12-14 16:53:30 +0200455 /* data */
456 struct tspp_pid_filter_table *filters[TSPP_FILTER_TABLES];
457 struct tspp_channel channels[TSPP_NUM_CHANNELS];
458 struct tspp_key_table *tspp_key_table;
459 struct tspp_global_performance_regs *tspp_global_performance;
460 struct tspp_pipe_context_regs *tspp_pipe_context;
461 struct tspp_pipe_performance_regs *tspp_pipe_performance;
462
463 struct dentry *dent;
464 struct dentry *debugfs_regs[ARRAY_SIZE(debugfs_tspp_regs)];
465};
466
467
Joel Nider5556a852011-10-16 10:52:13 +0200468static struct class *tspp_class;
469static int tspp_key_entry;
470static dev_t tspp_minor; /* next minor number to assign */
Joel Nider435ad8e2011-12-14 16:53:30 +0200471
472static LIST_HEAD(tspp_devices);
473
474/* forward declarations */
475static ssize_t tspp_read(struct file *, char __user *, size_t, loff_t *);
476static ssize_t tspp_open(struct inode *inode, struct file *filp);
477static unsigned int tspp_poll(struct file *filp, struct poll_table_struct *p);
478static ssize_t tspp_release(struct inode *inode, struct file *filp);
479static long tspp_ioctl(struct file *, unsigned int, unsigned long);
480
481/* file operations */
482static const struct file_operations tspp_fops = {
483 .owner = THIS_MODULE,
484 .read = tspp_read,
485 .open = tspp_open,
486 .poll = tspp_poll,
487 .release = tspp_release,
488 .unlocked_ioctl = tspp_ioctl,
489};
Joel Nider5556a852011-10-16 10:52:13 +0200490
491/*** IRQ ***/
Joel Nider435ad8e2011-12-14 16:53:30 +0200492static irqreturn_t tspp_isr(int irq, void *dev)
Joel Nider5556a852011-10-16 10:52:13 +0200493{
Joel Nider435ad8e2011-12-14 16:53:30 +0200494 struct tspp_device *device = dev;
Joel Nider5556a852011-10-16 10:52:13 +0200495 u32 status, mask;
496 u32 data;
497
498 status = readl_relaxed(device->base + TSPP_IRQ_STATUS);
499 mask = readl_relaxed(device->base + TSPP_IRQ_MASK);
500 status &= mask;
501
502 if (!status) {
503 dev_warn(&device->pdev->dev, "Spurious interrupt");
504 return IRQ_NONE;
505 }
506
507 /* if (status & TSPP_IRQ_STATUS_TSP_RD_CMPL) */
508
509 if (status & TSPP_IRQ_STATUS_KEY_ERROR) {
510 /* read the key error info */
511 data = readl_relaxed(device->base + TSPP_KEY_ERROR);
512 dev_info(&device->pdev->dev, "key error 0x%x", data);
513 }
514 if (status & TSPP_IRQ_STATUS_KEY_SWITCHED_BAD) {
515 data = readl_relaxed(device->base + TSPP_KEY_VALID);
516 dev_info(&device->pdev->dev, "key invalidated: 0x%x", data);
517 }
518 if (status & TSPP_IRQ_STATUS_KEY_SWITCHED)
519 dev_info(&device->pdev->dev, "key switched");
520
521 if (status & 0xffff)
Joel Nider435ad8e2011-12-14 16:53:30 +0200522 dev_info(&device->pdev->dev, "broken pipe %i", status & 0xffff);
Joel Nider5556a852011-10-16 10:52:13 +0200523
524 writel_relaxed(status, device->base + TSPP_IRQ_CLEAR);
Hamad Kadmany44307d32012-11-25 09:49:51 +0200525
526 /*
527 * Before returning IRQ_HANDLED to the generic interrupt handling
528 * framework need to make sure all operations including clearing of
529 * interrupt status registers in the hardware is performed.
530 * Thus a barrier after clearing the interrupt status register
531 * is required to guarantee that the interrupt status register has
532 * really been cleared by the time we return from this handler.
533 */
534 wmb();
535 return IRQ_HANDLED;
536}
537
538static irqreturn_t tsif_isr(int irq, void *dev)
539{
540 struct tspp_tsif_device *tsif_device = dev;
541 u32 sts_ctl = ioread32(tsif_device->base + TSIF_STS_CTL_OFF);
542
543 if (!(sts_ctl & (TSIF_STS_CTL_PACK_AVAIL |
544 TSIF_STS_CTL_OVERFLOW |
545 TSIF_STS_CTL_LOST_SYNC |
546 TSIF_STS_CTL_TIMEOUT)))
547 return IRQ_NONE;
548
549 if (sts_ctl & TSIF_STS_CTL_OVERFLOW)
550 tsif_device->stat_overflow++;
551
552 if (sts_ctl & TSIF_STS_CTL_LOST_SYNC)
553 tsif_device->stat_lost_sync++;
554
555 if (sts_ctl & TSIF_STS_CTL_TIMEOUT)
556 tsif_device->stat_timeout++;
557
558 iowrite32(sts_ctl, tsif_device->base + TSIF_STS_CTL_OFF);
559
560 /*
561 * Before returning IRQ_HANDLED to the generic interrupt handling
562 * framework need to make sure all operations including clearing of
563 * interrupt status registers in the hardware is performed.
564 * Thus a barrier after clearing the interrupt status register
565 * is required to guarantee that the interrupt status register has
566 * really been cleared by the time we return from this handler.
567 */
Joel Nider5556a852011-10-16 10:52:13 +0200568 wmb();
569 return IRQ_HANDLED;
570}
571
572/*** callbacks ***/
573static void tspp_sps_complete_cb(struct sps_event_notify *notify)
574{
Joel Nider435ad8e2011-12-14 16:53:30 +0200575 struct tspp_device *pdev = notify->user;
576 tasklet_schedule(&pdev->tlet);
Joel Nider5556a852011-10-16 10:52:13 +0200577}
578
Hamad Kadmany81cee052012-11-29 14:15:57 +0200579static void tspp_expiration_timer(unsigned long data)
580{
581 struct tspp_device *pdev = (struct tspp_device *)data;
582
583 if (pdev)
584 tasklet_schedule(&pdev->tlet);
585}
586
Joel Nider5556a852011-10-16 10:52:13 +0200587/*** tasklet ***/
588static void tspp_sps_complete_tlet(unsigned long data)
589{
590 int i;
591 int complete;
592 unsigned long flags;
593 struct sps_iovec iovec;
594 struct tspp_channel *channel;
595 struct tspp_device *device = (struct tspp_device *)data;
Joel Nider5556a852011-10-16 10:52:13 +0200596 spin_lock_irqsave(&device->spinlock, flags);
597
598 for (i = 0; i < TSPP_NUM_CHANNELS; i++) {
599 complete = 0;
Joel Nider435ad8e2011-12-14 16:53:30 +0200600 channel = &device->channels[i];
Hamad Kadmany81cee052012-11-29 14:15:57 +0200601
Joel Nider435ad8e2011-12-14 16:53:30 +0200602 if (!channel->used || !channel->waiting)
603 continue;
Joel Nider5556a852011-10-16 10:52:13 +0200604
Hamad Kadmany81cee052012-11-29 14:15:57 +0200605 /* stop the expiration timer */
606 if (channel->expiration_period_ms)
607 del_timer(&channel->expiration_timer);
608
Joel Nider5556a852011-10-16 10:52:13 +0200609 /* get completions */
Joel Nider435ad8e2011-12-14 16:53:30 +0200610 while (channel->waiting->state == TSPP_BUF_STATE_WAITING) {
Joel Nider5556a852011-10-16 10:52:13 +0200611 if (sps_get_iovec(channel->pipe, &iovec) != 0) {
612 pr_err("tspp: Error in iovec on channel %i",
613 channel->id);
614 break;
615 }
616 if (iovec.size == 0)
617 break;
618
Joel Nider435ad8e2011-12-14 16:53:30 +0200619 if (iovec.addr != channel->waiting->sps.phys_base)
Hamad Kadmanya23457e2013-07-02 16:51:20 +0300620 pr_err("tspp: buffer mismatch %pa",
621 &channel->waiting->sps.phys_base);
Joel Nider5556a852011-10-16 10:52:13 +0200622
623 complete = 1;
Joel Nider435ad8e2011-12-14 16:53:30 +0200624 channel->waiting->state = TSPP_BUF_STATE_DATA;
625 channel->waiting->filled = iovec.size;
626 channel->waiting->read_index = 0;
627
Hamad Kadmany44307d32012-11-25 09:49:51 +0200628 if (channel->src == TSPP_SOURCE_TSIF0)
629 device->tsif[0].stat_rx++;
630 else if (channel->src == TSPP_SOURCE_TSIF1)
631 device->tsif[1].stat_rx++;
632
Joel Nider435ad8e2011-12-14 16:53:30 +0200633 /* update the pointers */
634 channel->waiting = channel->waiting->next;
Joel Nider5556a852011-10-16 10:52:13 +0200635 }
636
Joel Nider435ad8e2011-12-14 16:53:30 +0200637 /* wake any waiting processes */
Joel Nider5556a852011-10-16 10:52:13 +0200638 if (complete) {
Joel Nider5556a852011-10-16 10:52:13 +0200639 wake_up_interruptible(&channel->in_queue);
Joel Nider435ad8e2011-12-14 16:53:30 +0200640
641 /* call notifiers */
642 if (channel->notifier)
643 channel->notifier(channel->id,
644 channel->notify_data);
Joel Nider5556a852011-10-16 10:52:13 +0200645 }
Hamad Kadmany81cee052012-11-29 14:15:57 +0200646
647 /* restart expiration timer */
648 if (channel->expiration_period_ms)
649 mod_timer(&channel->expiration_timer,
650 jiffies +
651 MSEC_TO_JIFFIES(
652 channel->expiration_period_ms));
Joel Nider5556a852011-10-16 10:52:13 +0200653 }
654
655 spin_unlock_irqrestore(&device->spinlock, flags);
656}
657
658/*** GPIO functions ***/
Liron Kuch275c0b32013-02-10 15:19:32 +0200659static int tspp_gpios_disable(const struct tspp_tsif_device *tsif_device,
660 const struct msm_gpio *table,
661 int size)
Joel Nider5556a852011-10-16 10:52:13 +0200662{
663 int rc = 0;
664 int i;
665 const struct msm_gpio *g;
Liron Kuch59339922013-01-01 18:29:47 +0200666
Joel Nider5556a852011-10-16 10:52:13 +0200667 for (i = size-1; i >= 0; i--) {
668 int tmp;
669 g = table + i;
Liron Kuch59339922013-01-01 18:29:47 +0200670
Liron Kuch275c0b32013-02-10 15:19:32 +0200671 /* don't use sync GPIO when not working in mode 2 */
672 if ((tsif_device->mode != TSPP_TSIF_MODE_2) &&
673 (strnstr(g->label, "sync", strlen(g->label)) != NULL))
674 continue;
675
Liron Kuch59339922013-01-01 18:29:47 +0200676 tmp = gpio_tlmm_config(GPIO_CFG(GPIO_PIN(g->gpio_cfg),
677 0, GPIO_CFG_INPUT, GPIO_CFG_PULL_DOWN, GPIO_CFG_2MA),
678 GPIO_CFG_DISABLE);
Joel Nider5556a852011-10-16 10:52:13 +0200679 if (tmp) {
Liron Kuch72b78552012-10-30 17:47:50 +0200680 pr_err("tspp_gpios_disable(0x%08x, GPIO_CFG_DISABLE) <%s> failed: %d\n",
Joel Nider5556a852011-10-16 10:52:13 +0200681 g->gpio_cfg, g->label ?: "?", rc);
682 pr_err("tspp: pin %d func %d dir %d pull %d drvstr %d\n",
683 GPIO_PIN(g->gpio_cfg), GPIO_FUNC(g->gpio_cfg),
684 GPIO_DIR(g->gpio_cfg), GPIO_PULL(g->gpio_cfg),
685 GPIO_DRVSTR(g->gpio_cfg));
686 if (!rc)
687 rc = tmp;
688 }
689 }
690
691 return rc;
692}
693
Liron Kuch275c0b32013-02-10 15:19:32 +0200694static int tspp_gpios_enable(const struct tspp_tsif_device *tsif_device,
695 const struct msm_gpio *table,
696 int size)
Joel Nider5556a852011-10-16 10:52:13 +0200697{
698 int rc;
Liron Kuch275c0b32013-02-10 15:19:32 +0200699 int i;
Joel Nider5556a852011-10-16 10:52:13 +0200700 const struct msm_gpio *g;
Liron Kuch59339922013-01-01 18:29:47 +0200701
Joel Nider5556a852011-10-16 10:52:13 +0200702 for (i = 0; i < size; i++) {
703 g = table + i;
Liron Kuch275c0b32013-02-10 15:19:32 +0200704
705 /* don't use sync GPIO when not working in mode 2 */
706 if ((tsif_device->mode != TSPP_TSIF_MODE_2) &&
707 (strnstr(g->label, "sync", strlen(g->label)) != NULL))
708 continue;
709
Joel Nider5556a852011-10-16 10:52:13 +0200710 rc = gpio_tlmm_config(g->gpio_cfg, GPIO_CFG_ENABLE);
711 if (rc) {
Liron Kuch72b78552012-10-30 17:47:50 +0200712 pr_err("tspp: gpio_tlmm_config(0x%08x, GPIO_CFG_ENABLE) <%s> failed: %d\n",
Joel Nider5556a852011-10-16 10:52:13 +0200713 g->gpio_cfg, g->label ?: "?", rc);
714 pr_err("tspp: pin %d func %d dir %d pull %d drvstr %d\n",
715 GPIO_PIN(g->gpio_cfg), GPIO_FUNC(g->gpio_cfg),
716 GPIO_DIR(g->gpio_cfg), GPIO_PULL(g->gpio_cfg),
717 GPIO_DRVSTR(g->gpio_cfg));
718 goto err;
719 }
720 }
721 return 0;
722err:
Liron Kuch275c0b32013-02-10 15:19:32 +0200723 tspp_gpios_disable(tsif_device, table, i);
Joel Nider5556a852011-10-16 10:52:13 +0200724
Joel Nider5556a852011-10-16 10:52:13 +0200725 return rc;
726}
727
Liron Kuch275c0b32013-02-10 15:19:32 +0200728
729static int tspp_config_gpios(struct tspp_device *device,
730 enum tspp_source source,
731 int enable)
Joel Nider5556a852011-10-16 10:52:13 +0200732{
Liron Kuch275c0b32013-02-10 15:19:32 +0200733 const struct msm_gpio *table;
734 struct msm_tspp_platform_data *pdata = device->pdev->dev.platform_data;
735 int num_gpios = (pdata->num_gpios / TSPP_TSIF_INSTANCES);
736 int i = 0;
Liron Kuch59339922013-01-01 18:29:47 +0200737
Liron Kuch275c0b32013-02-10 15:19:32 +0200738 if (num_gpios != TSPP_GPIOS_PER_TSIF) {
739 pr_err("tspp %s: unexpected number of GPIOs %d, expected %d\n",
740 __func__, num_gpios, TSPP_GPIOS_PER_TSIF);
741 return -EINVAL;
742 }
Joel Nider5556a852011-10-16 10:52:13 +0200743
Liron Kuch275c0b32013-02-10 15:19:32 +0200744 /*
745 * Note: this code assumes that the GPIO definitions in the
746 * pdata->gpios table are according to the TSIF instance number,
747 * i.e., that TSIF0 GPIOs are defined first, then TSIF1 GPIOs etc.
748 */
749 switch (source) {
750 case TSPP_SOURCE_TSIF0:
751 i = 0;
752 break;
753 case TSPP_SOURCE_TSIF1:
754 i = 1;
755 break;
756 default:
757 pr_err("tspp %s: invalid source\n", __func__);
758 return -EINVAL;
759 }
Liron Kuch59339922013-01-01 18:29:47 +0200760
Liron Kuch275c0b32013-02-10 15:19:32 +0200761 table = pdata->gpios + (i * num_gpios);
762 if (enable)
763 return tspp_gpios_enable(&device->tsif[i], table, num_gpios);
764 else
765 return tspp_gpios_disable(&device->tsif[i], table, num_gpios);
Joel Nider5556a852011-10-16 10:52:13 +0200766}
767
Joel Nider435ad8e2011-12-14 16:53:30 +0200768/*** Clock functions ***/
769static int tspp_clock_start(struct tspp_device *device)
770{
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -0700771 int rc;
772
Liron Kuch605cc122013-02-21 14:25:57 +0200773 if (device == NULL) {
774 pr_err("tspp: Can't start clocks, invalid device\n");
775 return -EINVAL;
776 }
777
Hamad Kadmanyef5ce992013-06-03 09:30:50 +0300778 if (device->tsif_bus_client) {
779 rc = msm_bus_scale_client_update_request(
780 device->tsif_bus_client, 1);
781 if (rc) {
782 pr_err("tspp: Can't enable bus\n");
783 return -EBUSY;
784 }
785 }
786
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -0700787 if (device->tsif_vreg) {
788 rc = regulator_set_voltage(device->tsif_vreg,
789 RPM_REGULATOR_CORNER_SUPER_TURBO,
790 RPM_REGULATOR_CORNER_SUPER_TURBO);
791 if (rc) {
792 pr_err("Unable to set CX voltage.\n");
Hamad Kadmanyef5ce992013-06-03 09:30:50 +0300793 if (device->tsif_bus_client)
794 msm_bus_scale_client_update_request(
795 device->tsif_bus_client, 0);
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -0700796 return rc;
797 }
798 }
799
Joel Nider435ad8e2011-12-14 16:53:30 +0200800 if (device->tsif_pclk && clk_prepare_enable(device->tsif_pclk) != 0) {
801 pr_err("tspp: Can't start pclk");
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -0700802
803 if (device->tsif_vreg) {
804 regulator_set_voltage(device->tsif_vreg,
Hamad Kadmanyfde29382013-06-26 08:03:51 +0300805 RPM_REGULATOR_CORNER_NONE,
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -0700806 RPM_REGULATOR_CORNER_SUPER_TURBO);
807 }
Hamad Kadmanyef5ce992013-06-03 09:30:50 +0300808
809 if (device->tsif_bus_client)
810 msm_bus_scale_client_update_request(
811 device->tsif_bus_client, 0);
Joel Nider435ad8e2011-12-14 16:53:30 +0200812 return -EBUSY;
813 }
814
815 if (device->tsif_ref_clk &&
816 clk_prepare_enable(device->tsif_ref_clk) != 0) {
817 pr_err("tspp: Can't start ref clk");
818 clk_disable_unprepare(device->tsif_pclk);
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -0700819 if (device->tsif_vreg) {
820 regulator_set_voltage(device->tsif_vreg,
Hamad Kadmanyfde29382013-06-26 08:03:51 +0300821 RPM_REGULATOR_CORNER_NONE,
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -0700822 RPM_REGULATOR_CORNER_SUPER_TURBO);
823 }
Hamad Kadmanyef5ce992013-06-03 09:30:50 +0300824
825 if (device->tsif_bus_client)
826 msm_bus_scale_client_update_request(
827 device->tsif_bus_client, 0);
Joel Nider435ad8e2011-12-14 16:53:30 +0200828 return -EBUSY;
829 }
830
831 return 0;
832}
833
834static void tspp_clock_stop(struct tspp_device *device)
835{
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -0700836 int rc;
837
Liron Kuch605cc122013-02-21 14:25:57 +0200838 if (device == NULL) {
839 pr_err("tspp: Can't stop clocks, invalid device\n");
840 return;
841 }
842
Joel Nider435ad8e2011-12-14 16:53:30 +0200843 if (device->tsif_pclk)
Liron Kuch605cc122013-02-21 14:25:57 +0200844 clk_disable_unprepare(device->tsif_pclk);
Joel Nider435ad8e2011-12-14 16:53:30 +0200845
846 if (device->tsif_ref_clk)
Liron Kuch605cc122013-02-21 14:25:57 +0200847 clk_disable_unprepare(device->tsif_ref_clk);
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -0700848
849 if (device->tsif_vreg) {
850 rc = regulator_set_voltage(device->tsif_vreg,
Hamad Kadmanyfde29382013-06-26 08:03:51 +0300851 RPM_REGULATOR_CORNER_NONE,
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -0700852 RPM_REGULATOR_CORNER_SUPER_TURBO);
853 if (rc)
854 pr_err("Unable to set CX voltage.\n");
855 }
Hamad Kadmanyef5ce992013-06-03 09:30:50 +0300856
857 if (device->tsif_bus_client) {
858 rc = msm_bus_scale_client_update_request(
859 device->tsif_bus_client, 0);
860 if (rc)
861 pr_err("tspp: Can't disable bus\n");
862 }
Joel Nider435ad8e2011-12-14 16:53:30 +0200863}
864
Joel Nider5556a852011-10-16 10:52:13 +0200865/*** TSIF functions ***/
866static int tspp_start_tsif(struct tspp_tsif_device *tsif_device)
867{
868 int start_hardware = 0;
869 u32 ctl;
870
871 if (tsif_device->ref_count == 0) {
872 start_hardware = 1;
873 } else if (tsif_device->ref_count > 0) {
874 ctl = readl_relaxed(tsif_device->base + TSIF_STS_CTL_OFF);
875 if ((ctl & TSIF_STS_CTL_START) != 1) {
876 /* this hardware should already be running */
877 pr_warn("tspp: tsif hw not started but ref count > 0");
878 start_hardware = 1;
879 }
880 }
881
882 if (start_hardware) {
Joel Nider435ad8e2011-12-14 16:53:30 +0200883 ctl = TSIF_STS_CTL_EN_IRQ |
Joel Nider5556a852011-10-16 10:52:13 +0200884 TSIF_STS_CTL_EN_DM;
Hamad Kadmany92705b32012-10-23 14:15:41 +0200885
886 if (tsif_device->clock_inverse)
887 ctl |= TSIF_STS_CTL_INV_CLOCK;
888
889 if (tsif_device->data_inverse)
890 ctl |= TSIF_STS_CTL_INV_DATA;
891
892 if (tsif_device->sync_inverse)
893 ctl |= TSIF_STS_CTL_INV_SYNC;
894
895 if (tsif_device->enable_inverse)
896 ctl |= TSIF_STS_CTL_INV_ENABLE;
897
Joel Nider435ad8e2011-12-14 16:53:30 +0200898 switch (tsif_device->mode) {
899 case TSPP_TSIF_MODE_LOOPBACK:
900 ctl |= TSIF_STS_CTL_EN_NULL |
901 TSIF_STS_CTL_EN_ERROR |
902 TSIF_STS_CTL_TEST_MODE;
903 break;
904 case TSPP_TSIF_MODE_1:
905 ctl |= TSIF_STS_CTL_EN_TIME_LIM |
906 TSIF_STS_CTL_EN_TCR;
907 break;
908 case TSPP_TSIF_MODE_2:
909 ctl |= TSIF_STS_CTL_EN_TIME_LIM |
910 TSIF_STS_CTL_EN_TCR |
911 TSIF_STS_CTL_MODE_2;
912 break;
913 default:
914 pr_warn("tspp: unknown tsif mode 0x%x",
915 tsif_device->mode);
Joel Nider5556a852011-10-16 10:52:13 +0200916 }
917 writel_relaxed(ctl, tsif_device->base + TSIF_STS_CTL_OFF);
918 writel_relaxed(tsif_device->time_limit,
919 tsif_device->base + TSIF_TIME_LIMIT_OFF);
920 wmb();
921 writel_relaxed(ctl | TSIF_STS_CTL_START,
922 tsif_device->base + TSIF_STS_CTL_OFF);
923 wmb();
Joel Nider5556a852011-10-16 10:52:13 +0200924 }
925
Joel Nider435ad8e2011-12-14 16:53:30 +0200926 ctl = readl_relaxed(tsif_device->base + TSIF_STS_CTL_OFF);
Joel Nider5556a852011-10-16 10:52:13 +0200927 tsif_device->ref_count++;
928
Joel Nider435ad8e2011-12-14 16:53:30 +0200929 return (ctl & TSIF_STS_CTL_START) ? 0 : -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +0200930}
931
932static void tspp_stop_tsif(struct tspp_tsif_device *tsif_device)
933{
934 if (tsif_device->ref_count == 0)
935 return;
936
937 tsif_device->ref_count--;
938
939 if (tsif_device->ref_count == 0) {
940 writel_relaxed(TSIF_STS_CTL_STOP,
941 tsif_device->base + TSIF_STS_CTL_OFF);
942 wmb();
943 }
944}
945
Joel Nider435ad8e2011-12-14 16:53:30 +0200946/*** local TSPP functions ***/
947static int tspp_channels_in_use(struct tspp_device *pdev)
948{
949 int i;
950 int count = 0;
951 for (i = 0; i < TSPP_NUM_CHANNELS; i++)
952 count += (pdev->channels[i].used ? 1 : 0);
953
954 return count;
955}
956
957static struct tspp_device *tspp_find_by_id(int id)
958{
959 struct tspp_device *dev;
960 list_for_each_entry(dev, &tspp_devices, devlist) {
961 if (dev->pdev->id == id)
962 return dev;
963 }
964 return NULL;
965}
966
Joel Nider5556a852011-10-16 10:52:13 +0200967static int tspp_get_key_entry(void)
968{
969 int i;
970 for (i = 0; i < TSPP_NUM_KEYS; i++) {
971 if (!(tspp_key_entry & (1 << i))) {
972 tspp_key_entry |= (1 << i);
973 return i;
974 }
975 }
Joel Nider435ad8e2011-12-14 16:53:30 +0200976 return 1 < TSPP_NUM_KEYS;
Joel Nider5556a852011-10-16 10:52:13 +0200977}
978
979static void tspp_free_key_entry(int entry)
980{
981 if (entry > TSPP_NUM_KEYS) {
982 pr_err("tspp_free_key_entry: index out of bounds");
983 return;
984 }
985
986 tspp_key_entry &= ~(1 << entry);
987}
988
Joel Nider435ad8e2011-12-14 16:53:30 +0200989static int tspp_alloc_buffer(u32 channel_id, struct tspp_data_descriptor *desc,
Hamad Kadmany090709b2013-01-06 12:08:13 +0200990 u32 size, struct dma_pool *dma_pool, tspp_allocator *alloc, void *user)
Joel Nider5556a852011-10-16 10:52:13 +0200991{
Joel Nider435ad8e2011-12-14 16:53:30 +0200992 if (size < TSPP_MIN_BUFFER_SIZE ||
993 size > TSPP_MAX_BUFFER_SIZE) {
994 pr_err("tspp: bad buffer size %i", size);
Joel Nider5556a852011-10-16 10:52:13 +0200995 return -ENOMEM;
996 }
Joel Nider435ad8e2011-12-14 16:53:30 +0200997
998 if (alloc) {
999 TSPP_DEBUG("tspp using alloc function");
1000 desc->virt_base = alloc(channel_id, size,
1001 &desc->phys_base, user);
1002 } else {
Hamad Kadmany090709b2013-01-06 12:08:13 +02001003 if (!dma_pool)
1004 desc->virt_base = dma_alloc_coherent(NULL, size,
1005 &desc->phys_base, GFP_KERNEL);
1006 else
1007 desc->virt_base = dma_pool_alloc(dma_pool, GFP_KERNEL,
1008 &desc->phys_base);
1009
Liron Kuch72b78552012-10-30 17:47:50 +02001010 if (desc->virt_base == 0) {
Hamad Kadmany090709b2013-01-06 12:08:13 +02001011 pr_err("tspp: dma buffer allocation failed %i\n", size);
Liron Kuch72b78552012-10-30 17:47:50 +02001012 return -ENOMEM;
1013 }
Joel Nider435ad8e2011-12-14 16:53:30 +02001014 }
1015
1016 desc->size = size;
1017 return 0;
1018}
1019
1020static int tspp_queue_buffer(struct tspp_channel *channel,
1021 struct tspp_mem_buffer *buffer)
1022{
1023 int rc;
1024 u32 flags = 0;
1025
1026 /* make sure the interrupt frequency is valid */
1027 if (channel->int_freq < 1)
1028 channel->int_freq = 1;
1029
1030 /* generate interrupt according to requested frequency */
1031 if (buffer->desc.id % channel->int_freq == channel->int_freq-1)
Hamad Kadmany81cee052012-11-29 14:15:57 +02001032 flags = SPS_IOVEC_FLAG_INT;
Joel Nider435ad8e2011-12-14 16:53:30 +02001033
1034 /* start the transfer */
1035 rc = sps_transfer_one(channel->pipe,
1036 buffer->sps.phys_base,
1037 buffer->sps.size,
1038 channel->pdev,
1039 flags);
1040 if (rc < 0)
1041 return rc;
1042
1043 buffer->state = TSPP_BUF_STATE_WAITING;
Joel Nider5556a852011-10-16 10:52:13 +02001044
1045 return 0;
1046}
1047
1048static int tspp_global_reset(struct tspp_device *pdev)
1049{
1050 u32 i, val;
1051
1052 /* stop all TSIFs */
1053 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
1054 pdev->tsif[i].ref_count = 1; /* allows stopping hw */
1055 tspp_stop_tsif(&pdev->tsif[i]); /* will reset ref_count to 0 */
1056 pdev->tsif[i].time_limit = TSPP_TSIF_DEFAULT_TIME_LIMIT;
Hamad Kadmany92705b32012-10-23 14:15:41 +02001057 pdev->tsif[i].clock_inverse = 0;
1058 pdev->tsif[i].data_inverse = 0;
1059 pdev->tsif[i].sync_inverse = 0;
1060 pdev->tsif[i].enable_inverse = 0;
Joel Nider5556a852011-10-16 10:52:13 +02001061 }
1062 writel_relaxed(TSPP_RST_RESET, pdev->base + TSPP_RST);
1063 wmb();
1064
1065 /* BAM */
1066 if (sps_device_reset(pdev->bam_handle) != 0) {
1067 pr_err("tspp: error resetting bam");
Joel Nider435ad8e2011-12-14 16:53:30 +02001068 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001069 }
1070
1071 /* TSPP tables */
1072 for (i = 0; i < TSPP_FILTER_TABLES; i++)
Joel Nider435ad8e2011-12-14 16:53:30 +02001073 memset(pdev->filters[i],
Joel Nider5556a852011-10-16 10:52:13 +02001074 0, sizeof(struct tspp_pid_filter_table));
1075
1076 /* disable all filters */
1077 val = (2 << TSPP_NUM_CHANNELS) - 1;
1078 writel_relaxed(val, pdev->base + TSPP_PS_DISABLE);
1079
1080 /* TSPP registers */
1081 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1082 writel_relaxed(val | TSPP_CLK_CONTROL_FORCE_PERF_CNT,
1083 pdev->base + TSPP_CONTROL);
1084 wmb();
Joel Nider435ad8e2011-12-14 16:53:30 +02001085 memset(pdev->tspp_global_performance, 0,
Joel Nider5556a852011-10-16 10:52:13 +02001086 sizeof(struct tspp_global_performance_regs));
Joel Nider435ad8e2011-12-14 16:53:30 +02001087 memset(pdev->tspp_pipe_context, 0,
Joel Nider5556a852011-10-16 10:52:13 +02001088 sizeof(struct tspp_pipe_context_regs));
Joel Nider435ad8e2011-12-14 16:53:30 +02001089 memset(pdev->tspp_pipe_performance, 0,
Joel Nider5556a852011-10-16 10:52:13 +02001090 sizeof(struct tspp_pipe_performance_regs));
1091 wmb();
1092 writel_relaxed(val & ~TSPP_CLK_CONTROL_FORCE_PERF_CNT,
1093 pdev->base + TSPP_CONTROL);
1094 wmb();
1095
1096 val = readl_relaxed(pdev->base + TSPP_CONFIG);
1097 val &= ~(TSPP_CONFIG_PS_LEN_ERR_MASK |
1098 TSPP_CONFIG_PS_CONT_ERR_UNSP_MASK |
1099 TSPP_CONFIG_PS_CONT_ERR_MASK);
1100 TSPP_CONFIG_SET_PACKET_LENGTH(val, TSPP_PACKET_LENGTH);
1101 writel_relaxed(val, pdev->base + TSPP_CONFIG);
Hamad Kadmany6bac7832012-12-20 18:30:40 +02001102 writel_relaxed(0x0007ffff, pdev->base + TSPP_IRQ_MASK);
Joel Nider5556a852011-10-16 10:52:13 +02001103 writel_relaxed(0x000fffff, pdev->base + TSPP_IRQ_CLEAR);
1104 writel_relaxed(0, pdev->base + TSPP_RST);
1105 wmb();
1106
1107 tspp_key_entry = 0;
1108
1109 return 0;
1110}
1111
Joel Nider435ad8e2011-12-14 16:53:30 +02001112static int tspp_select_source(u32 dev, u32 channel_id,
1113 struct tspp_select_source *src)
1114{
1115 /* make sure the requested src id is in bounds */
1116 if (src->source > TSPP_SOURCE_MEM) {
1117 pr_err("tspp source out of bounds");
1118 return -EINVAL;
1119 }
1120
1121 /* open the stream */
Hamad Kadmany92705b32012-10-23 14:15:41 +02001122 tspp_open_stream(dev, channel_id, src);
Joel Nider435ad8e2011-12-14 16:53:30 +02001123
1124 return 0;
1125}
1126
1127static int tspp_set_iv(struct tspp_channel *channel, struct tspp_iv *iv)
1128{
1129 struct tspp_device *pdev = channel->pdev;
1130
1131 writel_relaxed(iv->data[0], pdev->base + TSPP_CBC_INIT_VAL(0));
1132 writel_relaxed(iv->data[1], pdev->base + TSPP_CBC_INIT_VAL(1));
1133 return 0;
1134}
1135
1136static int tspp_set_system_keys(struct tspp_channel *channel,
1137 struct tspp_system_keys *keys)
1138{
1139 int i;
1140 struct tspp_device *pdev = channel->pdev;
1141
1142 for (i = 0; i < TSPP_NUM_SYSTEM_KEYS; i++)
1143 writel_relaxed(keys->data[i], pdev->base + TSPP_SYSTEM_KEY(i));
1144
1145 return 0;
1146}
1147
1148static int tspp_channel_init(struct tspp_channel *channel,
1149 struct tspp_device *pdev)
1150{
1151 channel->cdev.owner = THIS_MODULE;
1152 cdev_init(&channel->cdev, &tspp_fops);
1153 channel->pdev = pdev;
1154 channel->data = NULL;
1155 channel->read = NULL;
1156 channel->waiting = NULL;
1157 channel->locked = NULL;
1158 channel->id = MINOR(tspp_minor);
1159 channel->used = 0;
1160 channel->buffer_size = TSPP_MIN_BUFFER_SIZE;
1161 channel->max_buffers = TSPP_NUM_BUFFERS;
1162 channel->buffer_count = 0;
1163 channel->filter_count = 0;
1164 channel->int_freq = 1;
Liron Kuch72b78552012-10-30 17:47:50 +02001165 channel->src = TSPP_SOURCE_NONE;
1166 channel->mode = TSPP_MODE_DISABLED;
Joel Nider435ad8e2011-12-14 16:53:30 +02001167 channel->notifier = NULL;
1168 channel->notify_data = NULL;
Hamad Kadmany81cee052012-11-29 14:15:57 +02001169 channel->expiration_period_ms = 0;
Liron Kuch72b78552012-10-30 17:47:50 +02001170 channel->memfree = NULL;
1171 channel->user_info = NULL;
Joel Nider435ad8e2011-12-14 16:53:30 +02001172 init_waitqueue_head(&channel->in_queue);
1173
1174 if (cdev_add(&channel->cdev, tspp_minor++, 1) != 0) {
1175 pr_err("tspp: cdev_add failed");
1176 return -EBUSY;
1177 }
1178
1179 channel->dd = device_create(tspp_class, NULL, channel->cdev.dev,
1180 channel, "tspp%02d", channel->id);
1181 if (IS_ERR(channel->dd)) {
1182 pr_err("tspp: device_create failed: %i",
1183 (int)PTR_ERR(channel->dd));
1184 cdev_del(&channel->cdev);
1185 return -EBUSY;
1186 }
1187
1188 return 0;
1189}
1190
1191static int tspp_set_buffer_size(struct tspp_channel *channel,
1192 struct tspp_buffer *buf)
1193{
Liron Kuch72b78552012-10-30 17:47:50 +02001194 if (channel->buffer_count > 0) {
1195 pr_err("tspp: cannot set buffer size - buffers already allocated\n");
1196 return -EPERM;
1197 }
1198
Joel Nider435ad8e2011-12-14 16:53:30 +02001199 if (buf->size < TSPP_MIN_BUFFER_SIZE)
1200 channel->buffer_size = TSPP_MIN_BUFFER_SIZE;
1201 else if (buf->size > TSPP_MAX_BUFFER_SIZE)
1202 channel->buffer_size = TSPP_MAX_BUFFER_SIZE;
1203 else
1204 channel->buffer_size = buf->size;
1205
1206 return 0;
1207}
1208
1209static void tspp_set_tsif_mode(struct tspp_channel *channel,
1210 enum tspp_tsif_mode mode)
1211{
1212 int index;
1213
1214 switch (channel->src) {
1215 case TSPP_SOURCE_TSIF0:
1216 index = 0;
1217 break;
1218 case TSPP_SOURCE_TSIF1:
1219 index = 1;
1220 break;
1221 default:
1222 pr_warn("tspp: can't set mode for non-tsif source %d",
1223 channel->src);
1224 return;
1225 }
1226 channel->pdev->tsif[index].mode = mode;
1227}
1228
Hamad Kadmany92705b32012-10-23 14:15:41 +02001229static void tspp_set_signal_inversion(struct tspp_channel *channel,
Liron Kuch72b78552012-10-30 17:47:50 +02001230 int clock_inverse, int data_inverse,
1231 int sync_inverse, int enable_inverse)
Hamad Kadmany92705b32012-10-23 14:15:41 +02001232{
1233 int index;
1234
1235 switch (channel->src) {
1236 case TSPP_SOURCE_TSIF0:
1237 index = 0;
1238 break;
1239 case TSPP_SOURCE_TSIF1:
1240 index = 1;
1241 break;
1242 default:
1243 return;
1244 }
1245 channel->pdev->tsif[index].clock_inverse = clock_inverse;
1246 channel->pdev->tsif[index].data_inverse = data_inverse;
1247 channel->pdev->tsif[index].sync_inverse = sync_inverse;
1248 channel->pdev->tsif[index].enable_inverse = enable_inverse;
1249}
1250
Liron Kuch72b78552012-10-30 17:47:50 +02001251static int tspp_is_buffer_size_aligned(u32 size, enum tspp_mode mode)
1252{
1253 u32 alignment;
1254
1255 switch (mode) {
1256 case TSPP_MODE_RAW:
1257 /* must be a multiple of 192 */
1258 alignment = (TSPP_PACKET_LENGTH + 4);
1259 if (size % alignment)
1260 return 0;
1261 return 1;
1262
1263 case TSPP_MODE_RAW_NO_SUFFIX:
1264 /* must be a multiple of 188 */
1265 alignment = TSPP_PACKET_LENGTH;
1266 if (size % alignment)
1267 return 0;
1268 return 1;
1269
1270 case TSPP_MODE_DISABLED:
1271 case TSPP_MODE_PES:
1272 default:
1273 /* no alignment requirement */
1274 return 1;
1275 }
1276
1277}
1278
1279static u32 tspp_align_buffer_size_by_mode(u32 size, enum tspp_mode mode)
1280{
1281 u32 new_size;
1282 u32 alignment;
1283
1284 switch (mode) {
1285 case TSPP_MODE_RAW:
1286 /* must be a multiple of 192 */
1287 alignment = (TSPP_PACKET_LENGTH + 4);
1288 break;
1289
1290 case TSPP_MODE_RAW_NO_SUFFIX:
1291 /* must be a multiple of 188 */
1292 alignment = TSPP_PACKET_LENGTH;
1293 break;
1294
1295 case TSPP_MODE_DISABLED:
1296 case TSPP_MODE_PES:
1297 default:
1298 /* no alignment requirement - give the user what he asks for */
1299 alignment = 1;
1300 break;
1301 }
1302 /* align up */
1303 new_size = (((size + alignment - 1) / alignment) * alignment);
1304 return new_size;
1305}
1306
1307static void tspp_destroy_buffers(u32 channel_id, struct tspp_channel *channel)
1308{
1309 int i;
1310 struct tspp_mem_buffer *pbuf, *temp;
1311
1312 pbuf = channel->data;
1313 for (i = 0; i < channel->buffer_count; i++) {
1314 if (pbuf->desc.phys_base) {
1315 if (channel->memfree) {
1316 channel->memfree(channel_id,
1317 pbuf->desc.size,
1318 pbuf->desc.virt_base,
1319 pbuf->desc.phys_base,
1320 channel->user_info);
1321 } else {
Hamad Kadmany090709b2013-01-06 12:08:13 +02001322 if (!channel->dma_pool)
1323 dma_free_coherent(NULL,
1324 pbuf->desc.size,
1325 pbuf->desc.virt_base,
1326 pbuf->desc.phys_base);
1327 else
1328 dma_pool_free(channel->dma_pool,
1329 pbuf->desc.virt_base,
1330 pbuf->desc.phys_base);
Liron Kuch72b78552012-10-30 17:47:50 +02001331 }
1332 pbuf->desc.phys_base = 0;
1333 }
1334 pbuf->desc.virt_base = 0;
1335 pbuf->state = TSPP_BUF_STATE_EMPTY;
1336 temp = pbuf;
1337 pbuf = pbuf->next;
1338 kfree(temp);
1339 }
1340}
1341
Joel Nider435ad8e2011-12-14 16:53:30 +02001342/*** TSPP API functions ***/
Liron Kuch72b78552012-10-30 17:47:50 +02001343
1344/**
1345 * tspp_open_stream - open a TSPP stream for use.
1346 *
1347 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1348 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1349 * @source: stream source parameters.
1350 *
1351 * Return error status
1352 *
1353 */
1354int tspp_open_stream(u32 dev, u32 channel_id,
1355 struct tspp_select_source *source)
Joel Nider5556a852011-10-16 10:52:13 +02001356{
1357 u32 val;
1358 struct tspp_device *pdev;
Joel Nider435ad8e2011-12-14 16:53:30 +02001359 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02001360
Hamad Kadmany92705b32012-10-23 14:15:41 +02001361 TSPP_DEBUG("tspp_open_stream %i %i %i %i",
1362 dev, channel_id, source->source, source->mode);
Liron Kuch72b78552012-10-30 17:47:50 +02001363
Joel Nider435ad8e2011-12-14 16:53:30 +02001364 if (dev >= TSPP_MAX_DEVICES) {
1365 pr_err("tspp: device id out of range");
1366 return -ENODEV;
1367 }
Joel Nider5556a852011-10-16 10:52:13 +02001368
Joel Nider435ad8e2011-12-14 16:53:30 +02001369 if (channel_id >= TSPP_NUM_CHANNELS) {
1370 pr_err("tspp: channel id out of range");
1371 return -ECHRNG;
1372 }
1373
1374 pdev = tspp_find_by_id(dev);
1375 if (!pdev) {
1376 pr_err("tspp_str: can't find device %i", dev);
1377 return -ENODEV;
1378 }
1379 channel = &pdev->channels[channel_id];
Hamad Kadmany92705b32012-10-23 14:15:41 +02001380 channel->src = source->source;
1381 tspp_set_tsif_mode(channel, source->mode);
1382 tspp_set_signal_inversion(channel, source->clk_inverse,
Liron Kuch72b78552012-10-30 17:47:50 +02001383 source->data_inverse, source->sync_inverse,
1384 source->enable_inverse);
Joel Nider5556a852011-10-16 10:52:13 +02001385
Hamad Kadmany92705b32012-10-23 14:15:41 +02001386 switch (source->source) {
Joel Nider5556a852011-10-16 10:52:13 +02001387 case TSPP_SOURCE_TSIF0:
Liron Kuch275c0b32013-02-10 15:19:32 +02001388 if (tspp_config_gpios(pdev, channel->src, 1) != 0) {
1389 pr_err("tspp: error enabling tsif0 GPIOs\n");
1390 return -EBUSY;
1391 }
Joel Nider5556a852011-10-16 10:52:13 +02001392 /* make sure TSIF0 is running & enabled */
1393 if (tspp_start_tsif(&pdev->tsif[0]) != 0) {
1394 pr_err("tspp: error starting tsif0");
Joel Nider435ad8e2011-12-14 16:53:30 +02001395 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001396 }
Liron Kucha7b49ae2013-02-14 16:26:38 +02001397 if (pdev->tsif[0].ref_count == 1) {
1398 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1399 writel_relaxed(val & ~TSPP_CONTROL_TSP_TSIF0_SRC_DIS,
1400 pdev->base + TSPP_CONTROL);
1401 wmb();
1402 }
Joel Nider5556a852011-10-16 10:52:13 +02001403 break;
1404 case TSPP_SOURCE_TSIF1:
Liron Kuch275c0b32013-02-10 15:19:32 +02001405 if (tspp_config_gpios(pdev, channel->src, 1) != 0) {
1406 pr_err("tspp: error enabling tsif1 GPIOs\n");
1407 return -EBUSY;
1408 }
Joel Nider5556a852011-10-16 10:52:13 +02001409 /* make sure TSIF1 is running & enabled */
1410 if (tspp_start_tsif(&pdev->tsif[1]) != 0) {
1411 pr_err("tspp: error starting tsif1");
Joel Nider435ad8e2011-12-14 16:53:30 +02001412 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001413 }
Liron Kucha7b49ae2013-02-14 16:26:38 +02001414 if (pdev->tsif[1].ref_count == 1) {
1415 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1416 writel_relaxed(val & ~TSPP_CONTROL_TSP_TSIF1_SRC_DIS,
1417 pdev->base + TSPP_CONTROL);
1418 wmb();
1419 }
Joel Nider5556a852011-10-16 10:52:13 +02001420 break;
1421 case TSPP_SOURCE_MEM:
1422 break;
1423 default:
Hamad Kadmany92705b32012-10-23 14:15:41 +02001424 pr_err("tspp: channel %i invalid source %i",
1425 channel->id, source->source);
Joel Nider435ad8e2011-12-14 16:53:30 +02001426 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001427 }
1428
Joel Nider5556a852011-10-16 10:52:13 +02001429 return 0;
1430}
1431EXPORT_SYMBOL(tspp_open_stream);
1432
Liron Kuch72b78552012-10-30 17:47:50 +02001433/**
1434 * tspp_close_stream - close a TSPP stream.
1435 *
1436 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1437 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1438 *
1439 * Return error status
1440 *
1441 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001442int tspp_close_stream(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001443{
1444 u32 val;
Liron Kucha7b49ae2013-02-14 16:26:38 +02001445 u32 prev_ref_count;
Joel Nider5556a852011-10-16 10:52:13 +02001446 struct tspp_device *pdev;
Joel Nider435ad8e2011-12-14 16:53:30 +02001447 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02001448
Joel Nider435ad8e2011-12-14 16:53:30 +02001449 if (channel_id >= TSPP_NUM_CHANNELS) {
1450 pr_err("tspp: channel id out of range");
1451 return -ECHRNG;
1452 }
1453 pdev = tspp_find_by_id(dev);
1454 if (!pdev) {
1455 pr_err("tspp_cs: can't find device %i", dev);
1456 return -EBUSY;
1457 }
1458 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02001459
1460 switch (channel->src) {
1461 case TSPP_SOURCE_TSIF0:
Liron Kucha7b49ae2013-02-14 16:26:38 +02001462 prev_ref_count = pdev->tsif[0].ref_count;
Joel Nider5556a852011-10-16 10:52:13 +02001463 tspp_stop_tsif(&pdev->tsif[0]);
Liron Kuch275c0b32013-02-10 15:19:32 +02001464 if (tspp_config_gpios(pdev, channel->src, 0) != 0)
1465 pr_err("tspp: error disabling tsif0 GPIOs\n");
1466
Liron Kucha7b49ae2013-02-14 16:26:38 +02001467 if (prev_ref_count == 1) {
1468 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1469 writel_relaxed(val | TSPP_CONTROL_TSP_TSIF0_SRC_DIS,
1470 pdev->base + TSPP_CONTROL);
1471 wmb();
1472 }
Joel Nider5556a852011-10-16 10:52:13 +02001473 break;
1474 case TSPP_SOURCE_TSIF1:
Liron Kucha7b49ae2013-02-14 16:26:38 +02001475 prev_ref_count = pdev->tsif[1].ref_count;
Joel Nider5556a852011-10-16 10:52:13 +02001476 tspp_stop_tsif(&pdev->tsif[1]);
Liron Kuch275c0b32013-02-10 15:19:32 +02001477 if (tspp_config_gpios(pdev, channel->src, 0) != 0)
1478 pr_err("tspp: error disabling tsif0 GPIOs\n");
1479
Liron Kucha7b49ae2013-02-14 16:26:38 +02001480 if (prev_ref_count == 1) {
1481 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1482 writel_relaxed(val | TSPP_CONTROL_TSP_TSIF1_SRC_DIS,
1483 pdev->base + TSPP_CONTROL);
1484 wmb();
1485 }
Joel Nider5556a852011-10-16 10:52:13 +02001486 break;
1487 case TSPP_SOURCE_MEM:
1488 break;
1489 case TSPP_SOURCE_NONE:
1490 break;
1491 }
1492
Joel Nider435ad8e2011-12-14 16:53:30 +02001493 channel->src = TSPP_SOURCE_NONE;
Joel Nider5556a852011-10-16 10:52:13 +02001494 return 0;
1495}
1496EXPORT_SYMBOL(tspp_close_stream);
1497
Liron Kuch72b78552012-10-30 17:47:50 +02001498/**
1499 * tspp_open_channel - open a TSPP channel.
1500 *
1501 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1502 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1503 *
1504 * Return error status
1505 *
1506 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001507int tspp_open_channel(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001508{
1509 int rc = 0;
Joel Nider435ad8e2011-12-14 16:53:30 +02001510 struct sps_connect *config;
1511 struct sps_register_event *event;
1512 struct tspp_channel *channel;
1513 struct tspp_device *pdev;
1514
1515 if (channel_id >= TSPP_NUM_CHANNELS) {
1516 pr_err("tspp: channel id out of range");
1517 return -ECHRNG;
1518 }
1519 pdev = tspp_find_by_id(dev);
1520 if (!pdev) {
1521 pr_err("tspp_oc: can't find device %i", dev);
1522 return -ENODEV;
1523 }
1524 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02001525
1526 if (channel->used) {
1527 pr_err("tspp channel already in use");
Joel Nider435ad8e2011-12-14 16:53:30 +02001528 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001529 }
1530
Joel Nider435ad8e2011-12-14 16:53:30 +02001531 config = &channel->config;
1532 event = &channel->event;
1533
1534 /* start the clocks if needed */
Liron Kuch59339922013-01-01 18:29:47 +02001535 if (tspp_channels_in_use(pdev) == 0) {
Liron Kuch605cc122013-02-21 14:25:57 +02001536 rc = tspp_clock_start(pdev);
1537 if (rc)
1538 return rc;
1539
Joel Nider435ad8e2011-12-14 16:53:30 +02001540 wake_lock(&pdev->wake_lock);
Liron Kuch59339922013-01-01 18:29:47 +02001541 }
Joel Nider435ad8e2011-12-14 16:53:30 +02001542
Joel Nider5556a852011-10-16 10:52:13 +02001543 /* mark it as used */
1544 channel->used = 1;
1545
1546 /* start the bam */
1547 channel->pipe = sps_alloc_endpoint();
1548 if (channel->pipe == 0) {
1549 pr_err("tspp: error allocating endpoint");
1550 rc = -ENOMEM;
1551 goto err_sps_alloc;
1552 }
1553
1554 /* get default configuration */
1555 sps_get_config(channel->pipe, config);
1556
Joel Nider435ad8e2011-12-14 16:53:30 +02001557 config->source = pdev->bam_handle;
Joel Nider5556a852011-10-16 10:52:13 +02001558 config->destination = SPS_DEV_HANDLE_MEM;
1559 config->mode = SPS_MODE_SRC;
Joel Nider435ad8e2011-12-14 16:53:30 +02001560 config->options =
1561 SPS_O_AUTO_ENABLE | /* connection is auto-enabled */
1562 SPS_O_STREAMING | /* streaming mode */
1563 SPS_O_DESC_DONE | /* interrupt on end of descriptor */
Hamad Kadmany81cee052012-11-29 14:15:57 +02001564 SPS_O_ACK_TRANSFERS | /* must use sps_get_iovec() */
1565 SPS_O_HYBRID; /* Read actual descriptors in sps_get_iovec() */
Joel Nider5556a852011-10-16 10:52:13 +02001566 config->src_pipe_index = channel->id;
1567 config->desc.size =
Hamad Kadmany81cee052012-11-29 14:15:57 +02001568 TSPP_SPS_DESCRIPTOR_COUNT * SPS_DESCRIPTOR_SIZE;
Joel Nider5556a852011-10-16 10:52:13 +02001569 config->desc.base = dma_alloc_coherent(NULL,
1570 config->desc.size,
1571 &config->desc.phys_base,
1572 GFP_KERNEL);
1573 if (config->desc.base == 0) {
1574 pr_err("tspp: error allocating sps descriptors");
1575 rc = -ENOMEM;
1576 goto err_desc_alloc;
1577 }
1578
1579 memset(config->desc.base, 0, config->desc.size);
1580
1581 rc = sps_connect(channel->pipe, config);
1582 if (rc) {
1583 pr_err("tspp: error connecting bam");
1584 goto err_connect;
1585 }
1586
1587 event->mode = SPS_TRIGGER_CALLBACK;
Joel Nider435ad8e2011-12-14 16:53:30 +02001588 event->options = SPS_O_DESC_DONE;
Joel Nider5556a852011-10-16 10:52:13 +02001589 event->callback = tspp_sps_complete_cb;
1590 event->xfer_done = NULL;
Joel Nider435ad8e2011-12-14 16:53:30 +02001591 event->user = pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001592
1593 rc = sps_register_event(channel->pipe, event);
1594 if (rc) {
1595 pr_err("tspp: error registering event");
1596 goto err_event;
1597 }
1598
Hamad Kadmany81cee052012-11-29 14:15:57 +02001599 init_timer(&channel->expiration_timer);
1600 channel->expiration_timer.function = tspp_expiration_timer;
1601 channel->expiration_timer.data = (unsigned long)pdev;
1602 channel->expiration_timer.expires = 0xffffffffL;
1603
Joel Nider435ad8e2011-12-14 16:53:30 +02001604 rc = pm_runtime_get(&pdev->pdev->dev);
Joel Nider5556a852011-10-16 10:52:13 +02001605 if (rc < 0) {
Joel Nider435ad8e2011-12-14 16:53:30 +02001606 dev_err(&pdev->pdev->dev,
Joel Nider5556a852011-10-16 10:52:13 +02001607 "Runtime PM: Unable to wake up tspp device, rc = %d",
1608 rc);
1609 }
Joel Nider5556a852011-10-16 10:52:13 +02001610 return 0;
1611
1612err_event:
1613 sps_disconnect(channel->pipe);
1614err_connect:
1615 dma_free_coherent(NULL, config->desc.size, config->desc.base,
1616 config->desc.phys_base);
1617err_desc_alloc:
1618 sps_free_endpoint(channel->pipe);
1619err_sps_alloc:
1620 return rc;
1621}
1622EXPORT_SYMBOL(tspp_open_channel);
1623
Liron Kuch72b78552012-10-30 17:47:50 +02001624/**
1625 * tspp_close_channel - close a TSPP channel.
1626 *
1627 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1628 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1629 *
1630 * Return error status
1631 *
1632 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001633int tspp_close_channel(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001634{
1635 int i;
1636 int id;
Liron Kuch4ed3bf62013-03-28 09:44:42 +02001637 int table_idx;
Joel Nider5556a852011-10-16 10:52:13 +02001638 u32 val;
Liron Kuche0acb412013-04-21 13:16:45 +03001639 unsigned long flags;
Joel Nider5556a852011-10-16 10:52:13 +02001640
Joel Nider435ad8e2011-12-14 16:53:30 +02001641 struct sps_connect *config;
1642 struct tspp_device *pdev;
1643 struct tspp_channel *channel;
Joel Nider435ad8e2011-12-14 16:53:30 +02001644
1645 if (channel_id >= TSPP_NUM_CHANNELS) {
1646 pr_err("tspp: channel id out of range");
1647 return -ECHRNG;
1648 }
1649 pdev = tspp_find_by_id(dev);
1650 if (!pdev) {
1651 pr_err("tspp_close: can't find device %i", dev);
1652 return -ENODEV;
1653 }
1654 channel = &pdev->channels[channel_id];
1655
1656 /* if the channel is not used, we are done */
1657 if (!channel->used)
1658 return 0;
1659
Liron Kuche0acb412013-04-21 13:16:45 +03001660 /*
1661 * Need to protect access to used and waiting fields, as they are
1662 * used by the tasklet which is invoked from interrupt context
1663 */
1664 spin_lock_irqsave(&pdev->spinlock, flags);
1665 channel->used = 0;
1666 channel->waiting = NULL;
1667 spin_unlock_irqrestore(&pdev->spinlock, flags);
1668
Hamad Kadmany81cee052012-11-29 14:15:57 +02001669 if (channel->expiration_period_ms)
1670 del_timer(&channel->expiration_timer);
1671
Joel Nider435ad8e2011-12-14 16:53:30 +02001672 channel->notifier = NULL;
1673 channel->notify_data = NULL;
Hamad Kadmany81cee052012-11-29 14:15:57 +02001674 channel->expiration_period_ms = 0;
Joel Nider435ad8e2011-12-14 16:53:30 +02001675
1676 config = &channel->config;
1677 pdev = channel->pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001678
1679 /* disable pipe (channel) */
1680 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1681 writel_relaxed(val | channel->id, pdev->base + TSPP_PS_DISABLE);
1682 wmb();
1683
1684 /* unregister all filters for this channel */
Liron Kuch4ed3bf62013-03-28 09:44:42 +02001685 for (table_idx = 0; table_idx < TSPP_FILTER_TABLES; table_idx++) {
1686 for (i = 0; i < TSPP_NUM_PRIORITIES; i++) {
1687 struct tspp_pid_filter *filter =
1688 &pdev->filters[table_idx]->filter[i];
1689 id = FILTER_GET_PIPE_NUMBER0(filter);
1690 if (id == channel->id) {
1691 if (FILTER_HAS_ENCRYPTION(filter))
1692 tspp_free_key_entry(
1693 FILTER_GET_KEY_NUMBER(filter));
1694 filter->config = 0;
1695 filter->filter = 0;
1696 }
Joel Nider5556a852011-10-16 10:52:13 +02001697 }
1698 }
1699 channel->filter_count = 0;
1700
Joel Nider5556a852011-10-16 10:52:13 +02001701 /* disconnect the bam */
1702 if (sps_disconnect(channel->pipe) != 0)
1703 pr_warn("tspp: Error freeing sps endpoint (%i)", channel->id);
1704
1705 /* destroy the buffers */
1706 dma_free_coherent(NULL, config->desc.size, config->desc.base,
1707 config->desc.phys_base);
1708
Liron Kuch72b78552012-10-30 17:47:50 +02001709 tspp_destroy_buffers(channel_id, channel);
Hamad Kadmany090709b2013-01-06 12:08:13 +02001710 if (channel->dma_pool) {
1711 dma_pool_destroy(channel->dma_pool);
1712 channel->dma_pool = NULL;
1713 }
Liron Kuch72b78552012-10-30 17:47:50 +02001714
1715 channel->src = TSPP_SOURCE_NONE;
1716 channel->mode = TSPP_MODE_DISABLED;
1717 channel->memfree = NULL;
1718 channel->user_info = NULL;
Joel Nider5556a852011-10-16 10:52:13 +02001719 channel->buffer_count = 0;
Joel Nider435ad8e2011-12-14 16:53:30 +02001720 channel->data = NULL;
1721 channel->read = NULL;
Joel Nider435ad8e2011-12-14 16:53:30 +02001722 channel->locked = NULL;
Joel Nider5556a852011-10-16 10:52:13 +02001723
Liron Kuch59339922013-01-01 18:29:47 +02001724 if (tspp_channels_in_use(pdev) == 0) {
Joel Nider435ad8e2011-12-14 16:53:30 +02001725 wake_unlock(&pdev->wake_lock);
Liron Kuch59339922013-01-01 18:29:47 +02001726 tspp_clock_stop(pdev);
1727 }
Joel Nider435ad8e2011-12-14 16:53:30 +02001728
Liron Kuch605cc122013-02-21 14:25:57 +02001729 pm_runtime_put(&pdev->pdev->dev);
1730
Joel Nider5556a852011-10-16 10:52:13 +02001731 return 0;
1732}
1733EXPORT_SYMBOL(tspp_close_channel);
1734
Liron Kuch72b78552012-10-30 17:47:50 +02001735/**
Hamad Kadmany6d2a9c72013-01-31 14:49:20 +02001736 * tspp_get_ref_clk_counter - return the TSIF clock reference (TCR) counter.
1737 *
1738 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1739 * @source: The TSIF source from which the counter should be read
1740 * @tcr_counter: the value of TCR counter
1741 *
1742 * Return error status
1743 *
1744 * TCR increments at a rate equal to 27 MHz/256 = 105.47 kHz.
1745 * If source is neither TSIF 0 or TSIF1 0 is returned.
1746 */
1747int tspp_get_ref_clk_counter(u32 dev, enum tspp_source source, u32 *tcr_counter)
1748{
1749 struct tspp_device *pdev;
1750 struct tspp_tsif_device *tsif_device;
1751
1752 if (!tcr_counter)
1753 return -EINVAL;
1754
1755 pdev = tspp_find_by_id(dev);
1756 if (!pdev) {
1757 pr_err("tspp_get_ref_clk_counter: can't find device %i\n", dev);
1758 return -ENODEV;
1759 }
1760
1761 switch (source) {
1762 case TSPP_SOURCE_TSIF0:
1763 tsif_device = &pdev->tsif[0];
1764 break;
1765
1766 case TSPP_SOURCE_TSIF1:
1767 tsif_device = &pdev->tsif[1];
1768 break;
1769
1770 default:
1771 tsif_device = NULL;
1772 break;
1773 }
1774
1775 if (tsif_device && tsif_device->ref_count)
1776 *tcr_counter = ioread32(tsif_device->base + TSIF_CLK_REF_OFF);
1777 else
1778 *tcr_counter = 0;
1779
1780 return 0;
1781}
1782EXPORT_SYMBOL(tspp_get_ref_clk_counter);
1783
1784/**
Liron Kuch72b78552012-10-30 17:47:50 +02001785 * tspp_add_filter - add a TSPP filter to a channel.
1786 *
1787 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1788 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1789 * @filter: TSPP filter parameters
1790 *
1791 * Return error status
1792 *
1793 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001794int tspp_add_filter(u32 dev, u32 channel_id,
Joel Nider5556a852011-10-16 10:52:13 +02001795 struct tspp_filter *filter)
1796{
Liron Kuch72b78552012-10-30 17:47:50 +02001797 int i, rc;
Joel Nider5556a852011-10-16 10:52:13 +02001798 int other_channel;
1799 int entry;
1800 u32 val, pid, enabled;
Joel Nider435ad8e2011-12-14 16:53:30 +02001801 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001802 struct tspp_pid_filter p;
Joel Nider435ad8e2011-12-14 16:53:30 +02001803 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02001804
Joel Nider435ad8e2011-12-14 16:53:30 +02001805 TSPP_DEBUG("tspp: add filter");
1806 if (channel_id >= TSPP_NUM_CHANNELS) {
1807 pr_err("tspp: channel id out of range");
1808 return -ECHRNG;
1809 }
1810 pdev = tspp_find_by_id(dev);
1811 if (!pdev) {
1812 pr_err("tspp_add: can't find device %i", dev);
1813 return -ENODEV;
1814 }
1815
1816 channel = &pdev->channels[channel_id];
1817
Joel Nider5556a852011-10-16 10:52:13 +02001818 if (filter->source > TSPP_SOURCE_MEM) {
1819 pr_err("tspp invalid source");
Joel Nider435ad8e2011-12-14 16:53:30 +02001820 return -ENOSR;
Joel Nider5556a852011-10-16 10:52:13 +02001821 }
1822
1823 if (filter->priority >= TSPP_NUM_PRIORITIES) {
1824 pr_err("tspp invalid source");
Joel Nider435ad8e2011-12-14 16:53:30 +02001825 return -ENOSR;
Joel Nider5556a852011-10-16 10:52:13 +02001826 }
1827
Liron Kuch72b78552012-10-30 17:47:50 +02001828 channel->mode = filter->mode;
1829 /*
1830 * if buffers are already allocated, verify they fulfil
1831 * the alignment requirements.
1832 */
1833 if ((channel->buffer_count > 0) &&
1834 (!tspp_is_buffer_size_aligned(channel->buffer_size, channel->mode)))
1835 pr_warn("tspp: buffers allocated with incorrect alignment\n");
Joel Nider5556a852011-10-16 10:52:13 +02001836
1837 if (filter->mode == TSPP_MODE_PES) {
1838 for (i = 0; i < TSPP_NUM_PRIORITIES; i++) {
1839 struct tspp_pid_filter *tspp_filter =
Joel Nider435ad8e2011-12-14 16:53:30 +02001840 &pdev->filters[channel->src]->filter[i];
Joel Nider5556a852011-10-16 10:52:13 +02001841 pid = FILTER_GET_PIPE_PID((tspp_filter));
1842 enabled = FILTER_GET_PIPE_PROCESS0(tspp_filter);
1843 if (enabled && (pid == filter->pid)) {
1844 other_channel =
1845 FILTER_GET_PIPE_NUMBER0(tspp_filter);
1846 pr_err("tspp: pid 0x%x already in use by channel %i",
1847 filter->pid, other_channel);
Joel Nider435ad8e2011-12-14 16:53:30 +02001848 return -EBADSLT;
Joel Nider5556a852011-10-16 10:52:13 +02001849 }
1850 }
1851 }
1852
1853 /* make sure this priority is not already in use */
1854 enabled = FILTER_GET_PIPE_PROCESS0(
Joel Nider435ad8e2011-12-14 16:53:30 +02001855 (&(pdev->filters[channel->src]->filter[filter->priority])));
Joel Nider5556a852011-10-16 10:52:13 +02001856 if (enabled) {
1857 pr_err("tspp: filter priority %i source %i is already enabled\n",
1858 filter->priority, channel->src);
Joel Nider435ad8e2011-12-14 16:53:30 +02001859 return -ENOSR;
Joel Nider5556a852011-10-16 10:52:13 +02001860 }
1861
1862 if (channel->mode == TSPP_MODE_PES) {
1863 /* if we are already processing in PES mode, disable pipe
1864 (channel) and filter to be updated */
1865 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1866 writel_relaxed(val | (1 << channel->id),
1867 pdev->base + TSPP_PS_DISABLE);
1868 wmb();
1869 }
1870
1871 /* update entry */
1872 p.filter = 0;
Joel Nider435ad8e2011-12-14 16:53:30 +02001873 p.config = FILTER_TRANS_END_DISABLE;
Joel Nider5556a852011-10-16 10:52:13 +02001874 FILTER_SET_PIPE_PROCESS0((&p), filter->mode);
1875 FILTER_SET_PIPE_PID((&p), filter->pid);
1876 FILTER_SET_PID_MASK((&p), filter->mask);
1877 FILTER_SET_PIPE_NUMBER0((&p), channel->id);
1878 FILTER_SET_PIPE_PROCESS1((&p), TSPP_MODE_DISABLED);
1879 if (filter->decrypt) {
1880 entry = tspp_get_key_entry();
1881 if (entry == -1) {
1882 pr_err("tspp: no more keys available!");
1883 } else {
1884 p.config |= FILTER_DECRYPT;
1885 FILTER_SET_KEY_NUMBER((&p), entry);
1886 }
1887 }
Joel Nider5556a852011-10-16 10:52:13 +02001888
Joel Nider435ad8e2011-12-14 16:53:30 +02001889 pdev->filters[channel->src]->
Joel Nider5556a852011-10-16 10:52:13 +02001890 filter[filter->priority].config = p.config;
Joel Nider435ad8e2011-12-14 16:53:30 +02001891 pdev->filters[channel->src]->
Joel Nider5556a852011-10-16 10:52:13 +02001892 filter[filter->priority].filter = p.filter;
1893
Liron Kuch72b78552012-10-30 17:47:50 +02001894 /*
1895 * allocate buffers if needed (i.e. if user did has not already called
1896 * tspp_allocate_buffers() explicitly).
1897 */
1898 if (channel->buffer_count == 0) {
1899 channel->buffer_size =
Hamad Kadmany090709b2013-01-06 12:08:13 +02001900 tspp_align_buffer_size_by_mode(channel->buffer_size,
Liron Kuch72b78552012-10-30 17:47:50 +02001901 channel->mode);
1902 rc = tspp_allocate_buffers(dev, channel->id,
1903 channel->max_buffers,
1904 channel->buffer_size,
1905 channel->int_freq, NULL, NULL, NULL);
1906 if (rc != 0) {
1907 pr_err("tspp: tspp_allocate_buffers failed\n");
1908 return rc;
1909 }
Joel Nider435ad8e2011-12-14 16:53:30 +02001910 }
1911
Joel Nider5556a852011-10-16 10:52:13 +02001912 /* reenable pipe */
1913 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1914 writel_relaxed(val & ~(1 << channel->id), pdev->base + TSPP_PS_DISABLE);
1915 wmb();
1916 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1917
Joel Nider5556a852011-10-16 10:52:13 +02001918 channel->filter_count++;
1919
1920 return 0;
1921}
1922EXPORT_SYMBOL(tspp_add_filter);
1923
Liron Kuch72b78552012-10-30 17:47:50 +02001924/**
1925 * tspp_remove_filter - remove a TSPP filter from a channel.
1926 *
1927 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1928 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1929 * @filter: TSPP filter parameters
1930 *
1931 * Return error status
1932 *
1933 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001934int tspp_remove_filter(u32 dev, u32 channel_id,
Joel Nider5556a852011-10-16 10:52:13 +02001935 struct tspp_filter *filter)
1936{
1937 int entry;
1938 u32 val;
Joel Nider435ad8e2011-12-14 16:53:30 +02001939 struct tspp_device *pdev;
1940 int src;
1941 struct tspp_pid_filter *tspp_filter;
1942 struct tspp_channel *channel;
1943
1944 if (channel_id >= TSPP_NUM_CHANNELS) {
1945 pr_err("tspp: channel id out of range");
1946 return -ECHRNG;
1947 }
1948 pdev = tspp_find_by_id(dev);
1949 if (!pdev) {
1950 pr_err("tspp_remove: can't find device %i", dev);
1951 return -ENODEV;
1952 }
1953 channel = &pdev->channels[channel_id];
1954
1955 src = channel->src;
1956 tspp_filter = &(pdev->filters[src]->filter[filter->priority]);
Joel Nider5556a852011-10-16 10:52:13 +02001957
1958 /* disable pipe (channel) */
1959 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1960 writel_relaxed(val | channel->id, pdev->base + TSPP_PS_DISABLE);
1961 wmb();
1962
1963 /* update data keys */
1964 if (tspp_filter->config & FILTER_DECRYPT) {
1965 entry = FILTER_GET_KEY_NUMBER(tspp_filter);
1966 tspp_free_key_entry(entry);
1967 }
1968
1969 /* update pid table */
1970 tspp_filter->config = 0;
1971 tspp_filter->filter = 0;
1972
1973 channel->filter_count--;
1974
1975 /* reenable pipe */
1976 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1977 writel_relaxed(val & ~(1 << channel->id),
1978 pdev->base + TSPP_PS_DISABLE);
1979 wmb();
1980 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1981
1982 return 0;
1983}
1984EXPORT_SYMBOL(tspp_remove_filter);
1985
Liron Kuch72b78552012-10-30 17:47:50 +02001986/**
1987 * tspp_set_key - set TSPP key in key table.
1988 *
1989 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1990 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1991 * @key: TSPP key parameters
1992 *
1993 * Return error status
1994 *
1995 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001996int tspp_set_key(u32 dev, u32 channel_id, struct tspp_key *key)
Joel Nider5556a852011-10-16 10:52:13 +02001997{
1998 int i;
1999 int id;
2000 int key_index;
2001 int data;
Joel Nider435ad8e2011-12-14 16:53:30 +02002002 struct tspp_channel *channel;
2003 struct tspp_device *pdev;
2004
2005 if (channel_id >= TSPP_NUM_CHANNELS) {
2006 pr_err("tspp: channel id out of range");
2007 return -ECHRNG;
2008 }
2009 pdev = tspp_find_by_id(dev);
2010 if (!pdev) {
2011 pr_err("tspp_set: can't find device %i", dev);
2012 return -ENODEV;
2013 }
2014 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02002015
2016 /* read the key index used by this channel */
2017 for (i = 0; i < TSPP_NUM_PRIORITIES; i++) {
2018 struct tspp_pid_filter *tspp_filter =
Joel Nider435ad8e2011-12-14 16:53:30 +02002019 &(pdev->filters[channel->src]->filter[i]);
Joel Nider5556a852011-10-16 10:52:13 +02002020 id = FILTER_GET_PIPE_NUMBER0(tspp_filter);
2021 if (id == channel->id) {
2022 if (FILTER_HAS_ENCRYPTION(tspp_filter)) {
2023 key_index = FILTER_GET_KEY_NUMBER(tspp_filter);
2024 break;
2025 }
2026 }
2027 }
2028 if (i == TSPP_NUM_PRIORITIES) {
2029 pr_err("tspp: no encryption on this channel");
Joel Nider435ad8e2011-12-14 16:53:30 +02002030 return -ENOKEY;
Joel Nider5556a852011-10-16 10:52:13 +02002031 }
2032
2033 if (key->parity == TSPP_KEY_PARITY_EVEN) {
Joel Nider435ad8e2011-12-14 16:53:30 +02002034 pdev->tspp_key_table->entry[key_index].even_lsb = key->lsb;
2035 pdev->tspp_key_table->entry[key_index].even_msb = key->msb;
Joel Nider5556a852011-10-16 10:52:13 +02002036 } else {
Joel Nider435ad8e2011-12-14 16:53:30 +02002037 pdev->tspp_key_table->entry[key_index].odd_lsb = key->lsb;
2038 pdev->tspp_key_table->entry[key_index].odd_msb = key->msb;
Joel Nider5556a852011-10-16 10:52:13 +02002039 }
2040 data = readl_relaxed(channel->pdev->base + TSPP_KEY_VALID);
2041
2042 return 0;
2043}
2044EXPORT_SYMBOL(tspp_set_key);
2045
Liron Kuch72b78552012-10-30 17:47:50 +02002046/**
2047 * tspp_register_notification - register TSPP channel notification function.
2048 *
2049 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2050 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2051 * @pNotify: notification function
2052 * @userdata: user data to pass to notification function
2053 * @timer_ms: notification for partially filled buffers
2054 *
2055 * Return error status
2056 *
2057 */
Joel Nider435ad8e2011-12-14 16:53:30 +02002058int tspp_register_notification(u32 dev, u32 channel_id,
2059 tspp_notifier *pNotify, void *userdata, u32 timer_ms)
Joel Nider5556a852011-10-16 10:52:13 +02002060{
Joel Nider435ad8e2011-12-14 16:53:30 +02002061 struct tspp_channel *channel;
2062 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02002063
Joel Nider435ad8e2011-12-14 16:53:30 +02002064 if (channel_id >= TSPP_NUM_CHANNELS) {
2065 pr_err("tspp: channel id out of range");
2066 return -ECHRNG;
2067 }
2068 pdev = tspp_find_by_id(dev);
2069 if (!pdev) {
2070 pr_err("tspp_reg: can't find device %i", dev);
2071 return -ENODEV;
2072 }
2073 channel = &pdev->channels[channel_id];
2074 channel->notifier = pNotify;
2075 channel->notify_data = userdata;
Hamad Kadmany81cee052012-11-29 14:15:57 +02002076 channel->expiration_period_ms = timer_ms;
2077
Joel Nider5556a852011-10-16 10:52:13 +02002078 return 0;
2079}
Joel Nider435ad8e2011-12-14 16:53:30 +02002080EXPORT_SYMBOL(tspp_register_notification);
Joel Nider5556a852011-10-16 10:52:13 +02002081
Liron Kuch72b78552012-10-30 17:47:50 +02002082/**
2083 * tspp_unregister_notification - unregister TSPP channel notification function.
2084 *
2085 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2086 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2087 *
2088 * Return error status
2089 *
2090 */
Joel Nider435ad8e2011-12-14 16:53:30 +02002091int tspp_unregister_notification(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02002092{
Joel Nider435ad8e2011-12-14 16:53:30 +02002093 struct tspp_channel *channel;
2094 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02002095
Joel Nider435ad8e2011-12-14 16:53:30 +02002096 if (channel_id >= TSPP_NUM_CHANNELS) {
2097 pr_err("tspp: channel id out of range");
2098 return -ECHRNG;
2099 }
2100 pdev = tspp_find_by_id(dev);
2101 if (!pdev) {
2102 pr_err("tspp_unreg: can't find device %i", dev);
2103 return -ENODEV;
2104 }
2105 channel = &pdev->channels[channel_id];
2106 channel->notifier = NULL;
2107 channel->notify_data = 0;
Joel Nider5556a852011-10-16 10:52:13 +02002108 return 0;
2109}
Joel Nider435ad8e2011-12-14 16:53:30 +02002110EXPORT_SYMBOL(tspp_unregister_notification);
Joel Nider5556a852011-10-16 10:52:13 +02002111
Liron Kuch72b78552012-10-30 17:47:50 +02002112/**
2113 * tspp_get_buffer - get TSPP data buffer.
2114 *
2115 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2116 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2117 *
2118 * Return error status
2119 *
2120 */
Joel Nider435ad8e2011-12-14 16:53:30 +02002121const struct tspp_data_descriptor *tspp_get_buffer(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02002122{
Joel Nider435ad8e2011-12-14 16:53:30 +02002123 struct tspp_mem_buffer *buffer;
2124 struct tspp_channel *channel;
2125 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02002126
Joel Nider435ad8e2011-12-14 16:53:30 +02002127 if (channel_id >= TSPP_NUM_CHANNELS) {
2128 pr_err("tspp: channel id out of range");
2129 return NULL;
2130 }
2131 pdev = tspp_find_by_id(dev);
2132 if (!pdev) {
2133 pr_err("tspp_get: can't find device %i", dev);
2134 return NULL;
2135 }
2136 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02002137
Joel Nider435ad8e2011-12-14 16:53:30 +02002138 if (!channel->read) {
2139 pr_warn("tspp: no buffer to get on channel %i!",
2140 channel->id);
2141 return NULL;
2142 }
2143
2144 buffer = channel->read;
2145 /* see if we have any buffers ready to read */
2146 if (buffer->state != TSPP_BUF_STATE_DATA)
2147 return 0;
2148
2149 if (buffer->state == TSPP_BUF_STATE_DATA) {
2150 /* mark the buffer as busy */
2151 buffer->state = TSPP_BUF_STATE_LOCKED;
2152
2153 /* increment the pointer along the list */
2154 channel->read = channel->read->next;
2155 }
2156
2157 return &buffer->desc;
2158}
2159EXPORT_SYMBOL(tspp_get_buffer);
2160
Liron Kuch72b78552012-10-30 17:47:50 +02002161/**
2162 * tspp_release_buffer - release TSPP data buffer back to TSPP.
2163 *
2164 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2165 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2166 * @descriptor_id: buffer descriptor ID
2167 *
2168 * Return error status
2169 *
2170 */
Joel Nider435ad8e2011-12-14 16:53:30 +02002171int tspp_release_buffer(u32 dev, u32 channel_id, u32 descriptor_id)
2172{
2173 int i, found = 0;
2174 struct tspp_mem_buffer *buffer;
2175 struct tspp_channel *channel;
2176 struct tspp_device *pdev;
2177
2178 if (channel_id >= TSPP_NUM_CHANNELS) {
2179 pr_err("tspp: channel id out of range");
2180 return -ECHRNG;
2181 }
2182 pdev = tspp_find_by_id(dev);
2183 if (!pdev) {
2184 pr_err("tspp: can't find device %i", dev);
2185 return -ENODEV;
2186 }
2187 channel = &pdev->channels[channel_id];
2188
2189 if (descriptor_id > channel->buffer_count)
2190 pr_warn("tspp: desc id looks weird 0x%08x", descriptor_id);
2191
2192 /* find the correct descriptor */
2193 buffer = channel->locked;
2194 for (i = 0; i < channel->buffer_count; i++) {
2195 if (buffer->desc.id == descriptor_id) {
2196 found = 1;
2197 break;
2198 }
2199 buffer = buffer->next;
2200 }
2201 channel->locked = channel->locked->next;
2202
2203 if (!found) {
2204 pr_err("tspp: cant find desc %i", descriptor_id);
2205 return -EINVAL;
2206 }
2207
2208 /* make sure the buffer is in the expected state */
2209 if (buffer->state != TSPP_BUF_STATE_LOCKED) {
2210 pr_err("tspp: buffer %i not locked", descriptor_id);
2211 return -EINVAL;
2212 }
2213 /* unlock the buffer and requeue it */
2214 buffer->state = TSPP_BUF_STATE_WAITING;
2215
2216 if (tspp_queue_buffer(channel, buffer))
2217 pr_warn("tspp: can't requeue buffer");
Joel Nider5556a852011-10-16 10:52:13 +02002218 return 0;
2219}
Joel Nider435ad8e2011-12-14 16:53:30 +02002220EXPORT_SYMBOL(tspp_release_buffer);
2221
Liron Kuch72b78552012-10-30 17:47:50 +02002222/**
2223 * tspp_allocate_buffers - allocate TSPP data buffers.
2224 *
2225 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2226 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2227 * @count: number of buffers to allocate
2228 * @size: size of each buffer to allocate
2229 * @int_freq: interrupt frequency
2230 * @alloc: user defined memory allocator function. Pass NULL for default.
2231 * @memfree: user defined memory free function. Pass NULL for default.
2232 * @user: user data to pass to the memory allocator/free function
2233 *
2234 * Return error status
2235 *
2236 * The user can optionally call this function explicitly to allocate the TSPP
2237 * data buffers. Alternatively, if the user did not call this function, it
2238 * is called implicitly by tspp_add_filter().
2239 */
2240int tspp_allocate_buffers(u32 dev, u32 channel_id, u32 count, u32 size,
2241 u32 int_freq, tspp_allocator *alloc,
2242 tspp_memfree *memfree, void *user)
Joel Nider435ad8e2011-12-14 16:53:30 +02002243{
2244 struct tspp_channel *channel;
2245 struct tspp_device *pdev;
2246 struct tspp_mem_buffer *last = NULL;
2247
2248 TSPP_DEBUG("tspp_allocate_buffers");
2249
2250 if (channel_id >= TSPP_NUM_CHANNELS) {
Liron Kuch72b78552012-10-30 17:47:50 +02002251 pr_err("%s: channel id out of range", __func__);
Joel Nider435ad8e2011-12-14 16:53:30 +02002252 return -ECHRNG;
2253 }
Liron Kuch72b78552012-10-30 17:47:50 +02002254
Joel Nider435ad8e2011-12-14 16:53:30 +02002255 pdev = tspp_find_by_id(dev);
2256 if (!pdev) {
Liron Kuch72b78552012-10-30 17:47:50 +02002257 pr_err("%s: can't find device %i", __func__, dev);
Joel Nider435ad8e2011-12-14 16:53:30 +02002258 return -ENODEV;
2259 }
Liron Kuch72b78552012-10-30 17:47:50 +02002260
2261 if (count < MIN_ACCEPTABLE_BUFFER_COUNT) {
2262 pr_err("%s: tspp requires a minimum of %i buffers\n",
2263 __func__, MIN_ACCEPTABLE_BUFFER_COUNT);
2264 return -EINVAL;
2265 }
2266
Hamad Kadmany24020352013-05-22 12:54:18 +03002267 if (count > TSPP_NUM_BUFFERS) {
2268 pr_err("%s: tspp requires a maximum of %i buffers\n",
2269 __func__, TSPP_NUM_BUFFERS);
2270 return -EINVAL;
2271 }
2272
Joel Nider435ad8e2011-12-14 16:53:30 +02002273 channel = &pdev->channels[channel_id];
Hamad Kadmany090709b2013-01-06 12:08:13 +02002274
Liron Kuch72b78552012-10-30 17:47:50 +02002275 /* allow buffer allocation only if there was no previous buffer
2276 * allocation for this channel.
2277 */
2278 if (channel->buffer_count > 0) {
2279 pr_err("%s: buffers already allocated for channel %u",
2280 __func__, channel_id);
2281 return -EINVAL;
2282 }
Joel Nider435ad8e2011-12-14 16:53:30 +02002283
2284 channel->max_buffers = count;
2285
2286 /* set up interrupt frequency */
Liron Kuch72b78552012-10-30 17:47:50 +02002287 if (int_freq > channel->max_buffers) {
Joel Nider435ad8e2011-12-14 16:53:30 +02002288 int_freq = channel->max_buffers;
Liron Kuch72b78552012-10-30 17:47:50 +02002289 pr_warn("%s: setting interrupt frequency to %u\n",
2290 __func__, int_freq);
Joel Nider435ad8e2011-12-14 16:53:30 +02002291 }
Liron Kuch72b78552012-10-30 17:47:50 +02002292 channel->int_freq = int_freq;
2293 /*
2294 * it is the responsibility of the caller to tspp_allocate_buffers(),
2295 * whether it's the user or the driver, to make sure the size parameter
2296 * is compatible to the channel mode.
2297 */
2298 channel->buffer_size = size;
Joel Nider435ad8e2011-12-14 16:53:30 +02002299
Liron Kuch72b78552012-10-30 17:47:50 +02002300 /* save user defined memory free function for later use */
2301 channel->memfree = memfree;
2302 channel->user_info = user;
2303
Hamad Kadmany090709b2013-01-06 12:08:13 +02002304 /*
2305 * For small buffers, create a DMA pool so that memory
2306 * is not wasted through dma_alloc_coherent.
2307 */
2308 if (TSPP_USE_DMA_POOL(channel->buffer_size)) {
2309 channel->dma_pool = dma_pool_create("tspp",
2310 NULL, channel->buffer_size, 0, 0);
2311 if (!channel->dma_pool) {
2312 pr_err("%s: Can't allocate memory pool\n", __func__);
2313 return -ENOMEM;
2314 }
2315 } else {
2316 channel->dma_pool = NULL;
2317 }
2318
2319
Liron Kuch72b78552012-10-30 17:47:50 +02002320 for (channel->buffer_count = 0;
2321 channel->buffer_count < channel->max_buffers;
Joel Nider435ad8e2011-12-14 16:53:30 +02002322 channel->buffer_count++) {
2323
2324 /* allocate the descriptor */
2325 struct tspp_mem_buffer *desc = (struct tspp_mem_buffer *)
2326 kmalloc(sizeof(struct tspp_mem_buffer), GFP_KERNEL);
2327 if (!desc) {
Liron Kuch72b78552012-10-30 17:47:50 +02002328 pr_warn("%s: Can't allocate desc %i",
2329 __func__, channel->buffer_count);
Joel Nider435ad8e2011-12-14 16:53:30 +02002330 break;
2331 }
2332
2333 desc->desc.id = channel->buffer_count;
2334 /* allocate the buffer */
2335 if (tspp_alloc_buffer(channel_id, &desc->desc,
Hamad Kadmany090709b2013-01-06 12:08:13 +02002336 channel->buffer_size, channel->dma_pool,
2337 alloc, user) != 0) {
Joel Nider435ad8e2011-12-14 16:53:30 +02002338 kfree(desc);
Liron Kuch72b78552012-10-30 17:47:50 +02002339 pr_warn("%s: Can't allocate buffer %i",
2340 __func__, channel->buffer_count);
Joel Nider435ad8e2011-12-14 16:53:30 +02002341 break;
2342 }
2343
2344 /* add the descriptor to the list */
2345 desc->filled = 0;
2346 desc->read_index = 0;
2347 if (!channel->data) {
2348 channel->data = desc;
2349 desc->next = channel->data;
2350 } else {
2351 last->next = desc;
2352 }
2353 last = desc;
2354 desc->next = channel->data;
2355
2356 /* prepare the sps descriptor */
2357 desc->sps.phys_base = desc->desc.phys_base;
2358 desc->sps.base = desc->desc.virt_base;
2359 desc->sps.size = desc->desc.size;
2360
2361 /* start the transfer */
2362 if (tspp_queue_buffer(channel, desc))
Liron Kuch72b78552012-10-30 17:47:50 +02002363 pr_err("%s: can't queue buffer %i",
2364 __func__, desc->desc.id);
2365 }
2366
2367 if (channel->buffer_count < channel->max_buffers) {
2368 /*
2369 * we failed to allocate the requested number of buffers.
2370 * we don't allow a partial success, so need to clean up here.
2371 */
2372 tspp_destroy_buffers(channel_id, channel);
2373 channel->buffer_count = 0;
Hamad Kadmany090709b2013-01-06 12:08:13 +02002374
2375 if (channel->dma_pool) {
2376 dma_pool_destroy(channel->dma_pool);
2377 channel->dma_pool = NULL;
2378 }
Liron Kuch72b78552012-10-30 17:47:50 +02002379 return -ENOMEM;
Joel Nider435ad8e2011-12-14 16:53:30 +02002380 }
2381
2382 channel->waiting = channel->data;
2383 channel->read = channel->data;
2384 channel->locked = channel->data;
Liron Kuch72b78552012-10-30 17:47:50 +02002385
Hamad Kadmany81cee052012-11-29 14:15:57 +02002386 /* Now that buffers are scheduled to HW, kick data expiration timer */
2387 if (channel->expiration_period_ms)
2388 mod_timer(&channel->expiration_timer,
2389 jiffies +
2390 MSEC_TO_JIFFIES(
2391 channel->expiration_period_ms));
2392
Joel Nider435ad8e2011-12-14 16:53:30 +02002393 return 0;
2394}
2395EXPORT_SYMBOL(tspp_allocate_buffers);
Joel Nider5556a852011-10-16 10:52:13 +02002396
2397/*** File Operations ***/
2398static ssize_t tspp_open(struct inode *inode, struct file *filp)
2399{
Joel Nider435ad8e2011-12-14 16:53:30 +02002400 u32 dev;
Joel Nider5556a852011-10-16 10:52:13 +02002401 struct tspp_channel *channel;
Joel Nider435ad8e2011-12-14 16:53:30 +02002402
2403 TSPP_DEBUG("tspp_open");
Joel Nider5556a852011-10-16 10:52:13 +02002404 channel = container_of(inode->i_cdev, struct tspp_channel, cdev);
2405 filp->private_data = channel;
Joel Nider435ad8e2011-12-14 16:53:30 +02002406 dev = channel->pdev->pdev->id;
Joel Nider5556a852011-10-16 10:52:13 +02002407
2408 /* if this channel is already in use, quit */
2409 if (channel->used) {
2410 pr_err("tspp channel %i already in use",
2411 MINOR(channel->cdev.dev));
2412 return -EACCES;
2413 }
2414
Joel Nider435ad8e2011-12-14 16:53:30 +02002415 if (tspp_open_channel(dev, channel->id) != 0) {
Joel Nider5556a852011-10-16 10:52:13 +02002416 pr_err("tspp: error opening channel");
2417 return -EACCES;
2418 }
2419
2420 return 0;
2421}
2422
2423static unsigned int tspp_poll(struct file *filp, struct poll_table_struct *p)
2424{
2425 unsigned long flags;
2426 unsigned int mask = 0;
2427 struct tspp_channel *channel;
2428 channel = filp->private_data;
2429
2430 /* register the wait queue for this channel */
2431 poll_wait(filp, &channel->in_queue, p);
2432
2433 spin_lock_irqsave(&channel->pdev->spinlock, flags);
Joel Nider435ad8e2011-12-14 16:53:30 +02002434 if (channel->read &&
2435 channel->read->state == TSPP_BUF_STATE_DATA)
Joel Nider5556a852011-10-16 10:52:13 +02002436 mask = POLLIN | POLLRDNORM;
2437
2438 spin_unlock_irqrestore(&channel->pdev->spinlock, flags);
2439
2440 return mask;
2441}
2442
2443static ssize_t tspp_release(struct inode *inode, struct file *filp)
2444{
Joel Nider435ad8e2011-12-14 16:53:30 +02002445 struct tspp_channel *channel = filp->private_data;
2446 u32 dev = channel->pdev->pdev->id;
2447 TSPP_DEBUG("tspp_release");
Joel Nider5556a852011-10-16 10:52:13 +02002448
Joel Nider435ad8e2011-12-14 16:53:30 +02002449 tspp_close_channel(dev, channel->id);
Joel Nider5556a852011-10-16 10:52:13 +02002450
2451 return 0;
2452}
2453
2454static ssize_t tspp_read(struct file *filp, char __user *buf, size_t count,
2455 loff_t *f_pos)
2456{
2457 size_t size = 0;
2458 size_t transferred = 0;
2459 struct tspp_channel *channel;
2460 struct tspp_mem_buffer *buffer;
2461 channel = filp->private_data;
2462
2463 TSPP_DEBUG("tspp_read");
Joel Nider435ad8e2011-12-14 16:53:30 +02002464
2465 while (!channel->read) {
2466 if (filp->f_flags & O_NONBLOCK) {
2467 pr_warn("tspp: no buffer on channel %i!",
2468 channel->id);
2469 return -EAGAIN;
2470 }
2471 /* go to sleep if there is nothing to read */
2472 if (wait_event_interruptible(channel->in_queue,
2473 (channel->read != NULL))) {
2474 pr_err("tspp: rude awakening\n");
2475 return -ERESTARTSYS;
2476 }
2477 }
2478
2479 buffer = channel->read;
2480
Joel Nider5556a852011-10-16 10:52:13 +02002481 /* see if we have any buffers ready to read */
2482 while (buffer->state != TSPP_BUF_STATE_DATA) {
2483 if (filp->f_flags & O_NONBLOCK) {
2484 pr_warn("tspp: nothing to read on channel %i!",
2485 channel->id);
2486 return -EAGAIN;
2487 }
2488 /* go to sleep if there is nothing to read */
Joel Nider5556a852011-10-16 10:52:13 +02002489 if (wait_event_interruptible(channel->in_queue,
2490 (buffer->state == TSPP_BUF_STATE_DATA))) {
2491 pr_err("tspp: rude awakening\n");
2492 return -ERESTARTSYS;
2493 }
2494 }
2495
2496 while (buffer->state == TSPP_BUF_STATE_DATA) {
2497 size = min(count, buffer->filled);
Joel Nider5556a852011-10-16 10:52:13 +02002498 if (size == 0)
2499 break;
2500
Joel Nider435ad8e2011-12-14 16:53:30 +02002501 if (copy_to_user(buf, buffer->desc.virt_base +
Joel Nider5556a852011-10-16 10:52:13 +02002502 buffer->read_index, size)) {
2503 pr_err("tspp: error copying to user buffer");
Joel Nider435ad8e2011-12-14 16:53:30 +02002504 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02002505 }
2506 buf += size;
2507 count -= size;
2508 transferred += size;
2509 buffer->read_index += size;
2510
Liron Kuch72b78552012-10-30 17:47:50 +02002511 /*
2512 * after reading the end of the buffer, requeue it,
2513 * and set up for reading the next one
2514 */
Joel Nider435ad8e2011-12-14 16:53:30 +02002515 if (buffer->read_index == buffer->filled) {
Joel Nider5556a852011-10-16 10:52:13 +02002516 buffer->state = TSPP_BUF_STATE_WAITING;
Hamad Kadmany090709b2013-01-06 12:08:13 +02002517
Joel Nider435ad8e2011-12-14 16:53:30 +02002518 if (tspp_queue_buffer(channel, buffer))
2519 pr_err("tspp: can't submit transfer");
Hamad Kadmany090709b2013-01-06 12:08:13 +02002520
Joel Nider435ad8e2011-12-14 16:53:30 +02002521 channel->locked = channel->read;
2522 channel->read = channel->read->next;
Joel Nider5556a852011-10-16 10:52:13 +02002523 }
2524 }
2525
2526 return transferred;
2527}
2528
2529static long tspp_ioctl(struct file *filp,
2530 unsigned int param0, unsigned long param1)
2531{
Joel Nider435ad8e2011-12-14 16:53:30 +02002532 u32 dev;
Joel Nider5556a852011-10-16 10:52:13 +02002533 int rc = -1;
2534 struct tspp_channel *channel;
Joel Nider435ad8e2011-12-14 16:53:30 +02002535 struct tspp_select_source ss;
2536 struct tspp_filter f;
2537 struct tspp_key k;
2538 struct tspp_iv iv;
2539 struct tspp_system_keys sk;
2540 struct tspp_buffer b;
Joel Nider5556a852011-10-16 10:52:13 +02002541 channel = filp->private_data;
Joel Nider435ad8e2011-12-14 16:53:30 +02002542 dev = channel->pdev->pdev->id;
Joel Nider5556a852011-10-16 10:52:13 +02002543
Liron Kucha7b49ae2013-02-14 16:26:38 +02002544 if ((param0 != TSPP_IOCTL_CLOSE_STREAM) && !param1)
Joel Nider5556a852011-10-16 10:52:13 +02002545 return -EINVAL;
2546
2547 switch (param0) {
2548 case TSPP_IOCTL_SELECT_SOURCE:
Joel Nider435ad8e2011-12-14 16:53:30 +02002549 if (!access_ok(VERIFY_READ, param1,
2550 sizeof(struct tspp_select_source))) {
2551 return -EBUSY;
2552 }
2553 if (__copy_from_user(&ss, (void *)param1,
2554 sizeof(struct tspp_select_source)) == 0)
2555 rc = tspp_select_source(dev, channel->id, &ss);
Joel Nider5556a852011-10-16 10:52:13 +02002556 break;
2557 case TSPP_IOCTL_ADD_FILTER:
Joel Nider435ad8e2011-12-14 16:53:30 +02002558 if (!access_ok(VERIFY_READ, param1,
2559 sizeof(struct tspp_filter))) {
2560 return -ENOSR;
2561 }
2562 if (__copy_from_user(&f, (void *)param1,
2563 sizeof(struct tspp_filter)) == 0)
2564 rc = tspp_add_filter(dev, channel->id, &f);
Joel Nider5556a852011-10-16 10:52:13 +02002565 break;
2566 case TSPP_IOCTL_REMOVE_FILTER:
Joel Nider435ad8e2011-12-14 16:53:30 +02002567 if (!access_ok(VERIFY_READ, param1,
2568 sizeof(struct tspp_filter))) {
2569 return -EBUSY;
2570 }
2571 if (__copy_from_user(&f, (void *)param1,
2572 sizeof(struct tspp_filter)) == 0)
2573 rc = tspp_remove_filter(dev, channel->id, &f);
Joel Nider5556a852011-10-16 10:52:13 +02002574 break;
2575 case TSPP_IOCTL_SET_KEY:
Joel Nider435ad8e2011-12-14 16:53:30 +02002576 if (!access_ok(VERIFY_READ, param1,
2577 sizeof(struct tspp_key))) {
2578 return -EBUSY;
2579 }
2580 if (__copy_from_user(&k, (void *)param1,
2581 sizeof(struct tspp_key)) == 0)
2582 rc = tspp_set_key(dev, channel->id, &k);
Joel Nider5556a852011-10-16 10:52:13 +02002583 break;
2584 case TSPP_IOCTL_SET_IV:
Joel Nider435ad8e2011-12-14 16:53:30 +02002585 if (!access_ok(VERIFY_READ, param1,
2586 sizeof(struct tspp_iv))) {
2587 return -EBUSY;
2588 }
2589 if (__copy_from_user(&iv, (void *)param1,
2590 sizeof(struct tspp_iv)) == 0)
2591 rc = tspp_set_iv(channel, &iv);
Joel Nider5556a852011-10-16 10:52:13 +02002592 break;
2593 case TSPP_IOCTL_SET_SYSTEM_KEYS:
Joel Nider435ad8e2011-12-14 16:53:30 +02002594 if (!access_ok(VERIFY_READ, param1,
2595 sizeof(struct tspp_system_keys))) {
2596 return -EINVAL;
2597 }
2598 if (__copy_from_user(&sk, (void *)param1,
2599 sizeof(struct tspp_system_keys)) == 0)
2600 rc = tspp_set_system_keys(channel, &sk);
Joel Nider5556a852011-10-16 10:52:13 +02002601 break;
2602 case TSPP_IOCTL_BUFFER_SIZE:
Joel Nider435ad8e2011-12-14 16:53:30 +02002603 if (!access_ok(VERIFY_READ, param1,
2604 sizeof(struct tspp_buffer))) {
2605 rc = -EINVAL;
2606 }
2607 if (__copy_from_user(&b, (void *)param1,
2608 sizeof(struct tspp_buffer)) == 0)
2609 rc = tspp_set_buffer_size(channel, &b);
Joel Nider5556a852011-10-16 10:52:13 +02002610 break;
Liron Kucha7b49ae2013-02-14 16:26:38 +02002611 case TSPP_IOCTL_CLOSE_STREAM:
2612 rc = tspp_close_stream(dev, channel->id);
2613 break;
Joel Nider5556a852011-10-16 10:52:13 +02002614 default:
2615 pr_err("tspp: Unknown ioctl %i", param0);
2616 }
2617
Liron Kuch72b78552012-10-30 17:47:50 +02002618 /*
2619 * normalize the return code in case one of the subfunctions does
2620 * something weird
2621 */
Joel Nider5556a852011-10-16 10:52:13 +02002622 if (rc != 0)
Joel Nider435ad8e2011-12-14 16:53:30 +02002623 rc = -ENOIOCTLCMD;
Joel Nider5556a852011-10-16 10:52:13 +02002624
2625 return rc;
2626}
2627
2628/*** debugfs ***/
Joel Nider5556a852011-10-16 10:52:13 +02002629static int debugfs_iomem_x32_set(void *data, u64 val)
2630{
2631 writel_relaxed(val, data);
2632 wmb();
2633 return 0;
2634}
2635
2636static int debugfs_iomem_x32_get(void *data, u64 *val)
2637{
2638 *val = readl_relaxed(data);
2639 return 0;
2640}
2641
2642DEFINE_SIMPLE_ATTRIBUTE(fops_iomem_x32, debugfs_iomem_x32_get,
2643 debugfs_iomem_x32_set, "0x%08llx");
2644
2645static void tsif_debugfs_init(struct tspp_tsif_device *tsif_device,
2646 int instance)
2647{
2648 char name[10];
2649 snprintf(name, 10, "tsif%i", instance);
2650 tsif_device->dent_tsif = debugfs_create_dir(
2651 name, NULL);
2652 if (tsif_device->dent_tsif) {
2653 int i;
2654 void __iomem *base = tsif_device->base;
2655 for (i = 0; i < ARRAY_SIZE(debugfs_tsif_regs); i++) {
2656 tsif_device->debugfs_tsif_regs[i] =
2657 debugfs_create_file(
2658 debugfs_tsif_regs[i].name,
2659 debugfs_tsif_regs[i].mode,
2660 tsif_device->dent_tsif,
2661 base + debugfs_tsif_regs[i].offset,
2662 &fops_iomem_x32);
2663 }
Hamad Kadmany44307d32012-11-25 09:49:51 +02002664
2665 debugfs_create_u32(
2666 "stat_rx_chunks",
Hamad Kadmanya1dde822013-03-28 08:23:26 +02002667 S_IRUGO | S_IWUSR | S_IWGRP,
Hamad Kadmany44307d32012-11-25 09:49:51 +02002668 tsif_device->dent_tsif,
2669 &tsif_device->stat_rx);
2670
2671 debugfs_create_u32(
2672 "stat_overflow",
Hamad Kadmanya1dde822013-03-28 08:23:26 +02002673 S_IRUGO | S_IWUSR | S_IWGRP,
Hamad Kadmany44307d32012-11-25 09:49:51 +02002674 tsif_device->dent_tsif,
2675 &tsif_device->stat_overflow);
2676
2677 debugfs_create_u32(
2678 "stat_lost_sync",
Hamad Kadmanya1dde822013-03-28 08:23:26 +02002679 S_IRUGO | S_IWUSR | S_IWGRP,
Hamad Kadmany44307d32012-11-25 09:49:51 +02002680 tsif_device->dent_tsif,
2681 &tsif_device->stat_lost_sync);
2682
2683 debugfs_create_u32(
2684 "stat_timeout",
Hamad Kadmanya1dde822013-03-28 08:23:26 +02002685 S_IRUGO | S_IWUSR | S_IWGRP,
Hamad Kadmany44307d32012-11-25 09:49:51 +02002686 tsif_device->dent_tsif,
2687 &tsif_device->stat_timeout);
Joel Nider5556a852011-10-16 10:52:13 +02002688 }
2689}
2690
2691static void tsif_debugfs_exit(struct tspp_tsif_device *tsif_device)
2692{
2693 if (tsif_device->dent_tsif) {
2694 int i;
2695 debugfs_remove_recursive(tsif_device->dent_tsif);
2696 tsif_device->dent_tsif = NULL;
2697 for (i = 0; i < ARRAY_SIZE(debugfs_tsif_regs); i++)
2698 tsif_device->debugfs_tsif_regs[i] = NULL;
2699 }
2700}
2701
2702static void tspp_debugfs_init(struct tspp_device *device, int instance)
2703{
2704 char name[10];
2705 snprintf(name, 10, "tspp%i", instance);
2706 device->dent = debugfs_create_dir(
2707 name, NULL);
2708 if (device->dent) {
2709 int i;
2710 void __iomem *base = device->base;
2711 for (i = 0; i < ARRAY_SIZE(debugfs_tspp_regs); i++) {
2712 device->debugfs_regs[i] =
2713 debugfs_create_file(
2714 debugfs_tspp_regs[i].name,
2715 debugfs_tspp_regs[i].mode,
2716 device->dent,
2717 base + debugfs_tspp_regs[i].offset,
2718 &fops_iomem_x32);
2719 }
2720 }
2721}
2722
2723static void tspp_debugfs_exit(struct tspp_device *device)
2724{
2725 if (device->dent) {
2726 int i;
2727 debugfs_remove_recursive(device->dent);
2728 device->dent = NULL;
2729 for (i = 0; i < ARRAY_SIZE(debugfs_tspp_regs); i++)
2730 device->debugfs_regs[i] = NULL;
2731 }
2732}
Joel Nider5556a852011-10-16 10:52:13 +02002733
Liron Kuch59339922013-01-01 18:29:47 +02002734/* copy device-tree data to platfrom data struct */
2735static __devinit struct msm_tspp_platform_data *
2736msm_tspp_dt_to_pdata(struct platform_device *pdev)
2737{
2738 struct device_node *node = pdev->dev.of_node;
2739 struct msm_tspp_platform_data *data;
2740 struct msm_gpio *gpios;
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -07002741 struct property *prop;
Liron Kuch59339922013-01-01 18:29:47 +02002742 int i, rc;
2743 int gpio;
2744 u32 gpio_func;
2745
2746 /* Note: memory allocated by devm_kzalloc is freed automatically */
2747 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
2748 if (!data) {
2749 pr_err("tspp: Unable to allocate platform data\n");
2750 return NULL;
2751 }
2752 rc = of_property_read_string(node, "qcom,tsif-pclk", &data->tsif_pclk);
2753 if (rc) {
2754 pr_err("tspp: Could not find tsif-pclk property, err = %d\n",
2755 rc);
2756 return NULL;
2757 }
2758 rc = of_property_read_string(node, "qcom,tsif-ref-clk",
2759 &data->tsif_ref_clk);
2760 if (rc) {
2761 pr_err("tspp: Could not find tsif-ref-clk property, err = %d\n",
2762 rc);
2763 return NULL;
2764 }
2765
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -07002766 data->tsif_vreg_present = 0;
2767 prop = of_find_property(node, "vdd_cx-supply", NULL);
2768 if (prop)
2769 data->tsif_vreg_present = 1;
2770
Liron Kuch59339922013-01-01 18:29:47 +02002771 data->num_gpios = of_gpio_count(node);
2772 if (data->num_gpios == 0) {
2773 pr_err("tspp: Could not find GPIO definitions\n");
2774 return NULL;
2775 }
2776 gpios = devm_kzalloc(&pdev->dev,
2777 (data->num_gpios * sizeof(struct msm_gpio)),
2778 GFP_KERNEL);
2779 if (!gpios) {
2780 pr_err("tspp: Unable to allocate memory for GPIOs table\n");
2781 return NULL;
2782 }
2783 /* Assuming GPIO FUNC property is the same for all GPIOs */
2784 if (of_property_read_u32(node, "qcom,gpios-func", &gpio_func)) {
2785 pr_err("tspp: Could not find gpios-func property\n");
2786 return NULL;
2787 }
2788 for (i = 0; i < data->num_gpios; i++) {
2789 gpio = of_get_gpio(node, i);
2790 gpios[i].gpio_cfg = GPIO_CFG(gpio, gpio_func,
2791 GPIO_CFG_INPUT,
2792 GPIO_CFG_PULL_DOWN,
2793 GPIO_CFG_2MA);
2794 rc = of_property_read_string_index(node, "qcom,gpio-names",
2795 i, &gpios[i].label);
2796 if (rc)
2797 pr_warn("tspp: Could not find gpio-names property\n");
2798 }
2799
2800 data->gpios = gpios;
2801
2802 return data;
2803}
2804
2805static int msm_tspp_map_irqs(struct platform_device *pdev,
2806 struct tspp_device *device)
2807{
2808 int rc;
2809 int i;
2810
2811 /* get IRQ numbers from platform information */
2812
2813 /* map TSPP IRQ */
2814 rc = platform_get_irq_byname(pdev, "TSIF_TSPP_IRQ");
2815 if (rc > 0) {
2816 device->tspp_irq = rc;
2817 rc = request_irq(device->tspp_irq, tspp_isr, IRQF_SHARED,
2818 dev_name(&pdev->dev), device);
2819 if (rc) {
2820 dev_err(&pdev->dev,
2821 "failed to request TSPP IRQ %d : %d",
2822 device->tspp_irq, rc);
2823 device->tspp_irq = 0;
2824 return -EINVAL;
2825 }
2826 } else {
2827 dev_err(&pdev->dev, "failed to get TSPP IRQ");
2828 return -EINVAL;
2829 }
2830
2831 /* map TSIF IRQs */
2832 rc = platform_get_irq_byname(pdev, "TSIF0_IRQ");
2833 if (rc > 0) {
2834 device->tsif[0].tsif_irq = rc;
2835 } else {
2836 dev_err(&pdev->dev, "failed to get TSIF0 IRQ");
2837 return -EINVAL;
2838 }
2839
2840 rc = platform_get_irq_byname(pdev, "TSIF1_IRQ");
2841 if (rc > 0) {
2842 device->tsif[1].tsif_irq = rc;
2843 } else {
2844 dev_err(&pdev->dev, "failed to get TSIF1 IRQ");
2845 return -EINVAL;
2846 }
2847
2848 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
2849 rc = request_irq(device->tsif[i].tsif_irq,
2850 tsif_isr, IRQF_SHARED,
2851 dev_name(&pdev->dev), &device->tsif[i]);
2852 if (rc) {
2853 dev_warn(&pdev->dev, "failed to request TSIF%d IRQ: %d",
2854 i, rc);
2855 device->tsif[i].tsif_irq = 0;
2856 }
2857 }
2858
2859 /* map BAM IRQ */
2860 rc = platform_get_irq_byname(pdev, "TSIF_BAM_IRQ");
2861 if (rc > 0) {
2862 device->bam_irq = rc;
2863 } else {
2864 dev_err(&pdev->dev, "failed to get TSPP BAM IRQ");
2865 return -EINVAL;
2866 }
2867
2868 return 0;
2869}
2870
Joel Nider5556a852011-10-16 10:52:13 +02002871static int __devinit msm_tspp_probe(struct platform_device *pdev)
2872{
2873 int rc = -ENODEV;
2874 u32 version;
Liron Kuch72b78552012-10-30 17:47:50 +02002875 u32 i, j;
Joel Nider5556a852011-10-16 10:52:13 +02002876 struct msm_tspp_platform_data *data;
2877 struct tspp_device *device;
2878 struct resource *mem_tsif0;
2879 struct resource *mem_tsif1;
2880 struct resource *mem_tspp;
2881 struct resource *mem_bam;
Liron Kuch72b78552012-10-30 17:47:50 +02002882 struct tspp_channel *channel;
Hamad Kadmanyef5ce992013-06-03 09:30:50 +03002883 struct msm_bus_scale_pdata *tspp_bus_pdata = NULL;
Joel Nider5556a852011-10-16 10:52:13 +02002884
Liron Kuch59339922013-01-01 18:29:47 +02002885 if (pdev->dev.of_node) {
2886 /* get information from device tree */
2887 data = msm_tspp_dt_to_pdata(pdev);
2888 /* get device ID */
2889 rc = of_property_read_u32(pdev->dev.of_node,
2890 "cell-index", &pdev->id);
2891 if (rc)
2892 pdev->id = -1;
2893
2894 pdev->dev.platform_data = data;
Hamad Kadmanyef5ce992013-06-03 09:30:50 +03002895
2896 tspp_bus_pdata = msm_bus_cl_get_pdata(pdev);
Liron Kuch59339922013-01-01 18:29:47 +02002897 } else {
2898 /* must have platform data */
2899 data = pdev->dev.platform_data;
Hamad Kadmanyef5ce992013-06-03 09:30:50 +03002900 tspp_bus_pdata = NULL;
Liron Kuch59339922013-01-01 18:29:47 +02002901 }
Joel Nider5556a852011-10-16 10:52:13 +02002902 if (!data) {
2903 pr_err("tspp: Platform data not available");
2904 rc = -EINVAL;
2905 goto out;
2906 }
2907
2908 /* check for valid device id */
Joel Nider435ad8e2011-12-14 16:53:30 +02002909 if ((pdev->id < 0) || (pdev->id >= TSPP_MAX_DEVICES)) {
Joel Nider5556a852011-10-16 10:52:13 +02002910 pr_err("tspp: Invalid device ID %d", pdev->id);
2911 rc = -EINVAL;
2912 goto out;
2913 }
2914
2915 /* OK, we will use this device */
2916 device = kzalloc(sizeof(struct tspp_device), GFP_KERNEL);
2917 if (!device) {
2918 pr_err("tspp: Failed to allocate memory for device");
2919 rc = -ENOMEM;
2920 goto out;
2921 }
2922
2923 /* set up references */
2924 device->pdev = pdev;
2925 platform_set_drvdata(pdev, device);
2926
Hamad Kadmanyef5ce992013-06-03 09:30:50 +03002927 /* register bus client */
2928 if (tspp_bus_pdata) {
2929 device->tsif_bus_client =
2930 msm_bus_scale_register_client(tspp_bus_pdata);
2931 if (!device->tsif_bus_client)
2932 pr_err("tspp: Unable to register bus client\n");
2933 } else {
2934 device->tsif_bus_client = 0;
2935 }
2936
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -07002937 /* map regulators */
2938 if (data->tsif_vreg_present) {
2939 device->tsif_vreg = devm_regulator_get(&pdev->dev, "vdd_cx");
2940 if (IS_ERR(device->tsif_vreg)) {
2941 rc = PTR_ERR(device->tsif_vreg);
2942 device->tsif_vreg = NULL;
2943 goto err_regultaor;
2944 }
2945
2946 /* Set an initial voltage and enable the regulator */
2947 rc = regulator_set_voltage(device->tsif_vreg,
Hamad Kadmanyfde29382013-06-26 08:03:51 +03002948 RPM_REGULATOR_CORNER_NONE,
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -07002949 RPM_REGULATOR_CORNER_SUPER_TURBO);
2950 if (rc) {
2951 dev_err(&pdev->dev, "Unable to set CX voltage.\n");
2952 goto err_regultaor;
2953 }
2954
2955 rc = regulator_enable(device->tsif_vreg);
2956 if (rc) {
2957 dev_err(&pdev->dev, "Unable to enable CX regulator.\n");
2958 goto err_regultaor;
2959 }
2960 }
2961
Joel Nider5556a852011-10-16 10:52:13 +02002962 /* map clocks */
2963 if (data->tsif_pclk) {
Joel Niderb9662ca2012-06-10 14:21:11 +03002964 device->tsif_pclk = clk_get(&pdev->dev, data->tsif_pclk);
Joel Nider5556a852011-10-16 10:52:13 +02002965 if (IS_ERR(device->tsif_pclk)) {
Joel Nider5556a852011-10-16 10:52:13 +02002966 rc = PTR_ERR(device->tsif_pclk);
2967 device->tsif_pclk = NULL;
2968 goto err_pclock;
2969 }
2970 }
2971 if (data->tsif_ref_clk) {
Joel Niderb9662ca2012-06-10 14:21:11 +03002972 device->tsif_ref_clk = clk_get(&pdev->dev, data->tsif_ref_clk);
Joel Nider5556a852011-10-16 10:52:13 +02002973 if (IS_ERR(device->tsif_ref_clk)) {
Joel Nider5556a852011-10-16 10:52:13 +02002974 rc = PTR_ERR(device->tsif_ref_clk);
2975 device->tsif_ref_clk = NULL;
2976 goto err_refclock;
2977 }
2978 }
2979
2980 /* map I/O memory */
Liron Kuch59339922013-01-01 18:29:47 +02002981 mem_tsif0 = platform_get_resource_byname(pdev,
2982 IORESOURCE_MEM, "MSM_TSIF0_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02002983 if (!mem_tsif0) {
2984 pr_err("tspp: Missing tsif0 MEM resource");
2985 rc = -ENXIO;
2986 goto err_res_tsif0;
2987 }
2988 device->tsif[0].base = ioremap(mem_tsif0->start,
2989 resource_size(mem_tsif0));
2990 if (!device->tsif[0].base) {
2991 pr_err("tspp: ioremap failed");
2992 goto err_map_tsif0;
2993 }
2994
Liron Kuch59339922013-01-01 18:29:47 +02002995 mem_tsif1 = platform_get_resource_byname(pdev,
2996 IORESOURCE_MEM, "MSM_TSIF1_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02002997 if (!mem_tsif1) {
2998 dev_err(&pdev->dev, "Missing tsif1 MEM resource");
2999 rc = -ENXIO;
3000 goto err_res_tsif1;
3001 }
3002 device->tsif[1].base = ioremap(mem_tsif1->start,
3003 resource_size(mem_tsif1));
3004 if (!device->tsif[1].base) {
3005 dev_err(&pdev->dev, "ioremap failed");
3006 goto err_map_tsif1;
3007 }
3008
Liron Kuch59339922013-01-01 18:29:47 +02003009 mem_tspp = platform_get_resource_byname(pdev,
3010 IORESOURCE_MEM, "MSM_TSPP_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02003011 if (!mem_tspp) {
3012 dev_err(&pdev->dev, "Missing MEM resource");
3013 rc = -ENXIO;
3014 goto err_res_dev;
3015 }
3016 device->base = ioremap(mem_tspp->start, resource_size(mem_tspp));
3017 if (!device->base) {
3018 dev_err(&pdev->dev, "ioremap failed");
3019 goto err_map_dev;
3020 }
3021
Liron Kuch59339922013-01-01 18:29:47 +02003022 mem_bam = platform_get_resource_byname(pdev,
3023 IORESOURCE_MEM, "MSM_TSPP_BAM_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02003024 if (!mem_bam) {
3025 pr_err("tspp: Missing bam MEM resource");
3026 rc = -ENXIO;
3027 goto err_res_bam;
3028 }
3029 memset(&device->bam_props, 0, sizeof(device->bam_props));
3030 device->bam_props.phys_addr = mem_bam->start;
3031 device->bam_props.virt_addr = ioremap(mem_bam->start,
3032 resource_size(mem_bam));
3033 if (!device->bam_props.virt_addr) {
3034 dev_err(&pdev->dev, "ioremap failed");
3035 goto err_map_bam;
3036 }
3037
Liron Kuch59339922013-01-01 18:29:47 +02003038 if (msm_tspp_map_irqs(pdev, device))
Joel Nider5556a852011-10-16 10:52:13 +02003039 goto err_irq;
Joel Nider5556a852011-10-16 10:52:13 +02003040
Joel Nider5556a852011-10-16 10:52:13 +02003041 /* power management */
3042 pm_runtime_set_active(&pdev->dev);
3043 pm_runtime_enable(&pdev->dev);
3044
Joel Nider5556a852011-10-16 10:52:13 +02003045 tspp_debugfs_init(device, 0);
3046
3047 for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
3048 tsif_debugfs_init(&device->tsif[i], i);
Joel Nider5556a852011-10-16 10:52:13 +02003049
3050 wake_lock_init(&device->wake_lock, WAKE_LOCK_SUSPEND,
3051 dev_name(&pdev->dev));
3052
3053 /* set up pointers to ram-based 'registers' */
Joel Nider435ad8e2011-12-14 16:53:30 +02003054 device->filters[0] = device->base + TSPP_PID_FILTER_TABLE0;
3055 device->filters[1] = device->base + TSPP_PID_FILTER_TABLE1;
3056 device->filters[2] = device->base + TSPP_PID_FILTER_TABLE2;
3057 device->tspp_key_table = device->base + TSPP_DATA_KEY;
3058 device->tspp_global_performance =
3059 device->base + TSPP_GLOBAL_PERFORMANCE;
3060 device->tspp_pipe_context =
3061 device->base + TSPP_PIPE_CONTEXT;
3062 device->tspp_pipe_performance =
3063 device->base + TSPP_PIPE_PERFORMANCE;
Joel Nider5556a852011-10-16 10:52:13 +02003064
3065 device->bam_props.summing_threshold = 0x10;
3066 device->bam_props.irq = device->bam_irq;
3067 device->bam_props.manage = SPS_BAM_MGR_LOCAL;
3068
Liron Kuch59339922013-01-01 18:29:47 +02003069 if (tspp_clock_start(device) != 0) {
3070 dev_err(&pdev->dev, "Can't start clocks");
3071 goto err_clock;
3072 }
3073
Joel Nider5556a852011-10-16 10:52:13 +02003074 if (sps_register_bam_device(&device->bam_props,
3075 &device->bam_handle) != 0) {
3076 pr_err("tspp: failed to register bam");
3077 goto err_bam;
3078 }
3079
Joel Nider5556a852011-10-16 10:52:13 +02003080 spin_lock_init(&device->spinlock);
3081 tasklet_init(&device->tlet, tspp_sps_complete_tlet,
3082 (unsigned long)device);
3083
3084 /* initialize everything to a known state */
3085 tspp_global_reset(device);
3086
3087 version = readl_relaxed(device->base + TSPP_VERSION);
Liron Kuch59339922013-01-01 18:29:47 +02003088 /*
3089 * TSPP version can be bits [7:0] or alternatively,
3090 * TSPP major version is bits [31:28].
3091 */
3092 if ((version != 0x1) && (((version >> 28) & 0xF) != 0x1))
Joel Nider5556a852011-10-16 10:52:13 +02003093 pr_warn("tspp: unrecognized hw version=%i", version);
3094
Joel Nider435ad8e2011-12-14 16:53:30 +02003095 /* initialize the channels */
Joel Nider5556a852011-10-16 10:52:13 +02003096 for (i = 0; i < TSPP_NUM_CHANNELS; i++) {
Joel Nider435ad8e2011-12-14 16:53:30 +02003097 if (tspp_channel_init(&(device->channels[i]), device) != 0) {
3098 pr_err("tspp_channel_init failed");
3099 goto err_channel;
3100 }
Joel Nider5556a852011-10-16 10:52:13 +02003101 }
3102
Joel Nider435ad8e2011-12-14 16:53:30 +02003103 /* stop the clocks for power savings */
3104 tspp_clock_stop(device);
3105
3106 /* everything is ok, so add the device to the list */
3107 list_add_tail(&(device->devlist), &tspp_devices);
3108
Joel Nider5556a852011-10-16 10:52:13 +02003109 return 0;
3110
Joel Nider435ad8e2011-12-14 16:53:30 +02003111err_channel:
Liron Kuch59339922013-01-01 18:29:47 +02003112 /* un-initialize channels */
Liron Kuch72b78552012-10-30 17:47:50 +02003113 for (j = 0; j < i; j++) {
3114 channel = &(device->channels[i]);
3115 device_destroy(tspp_class, channel->cdev.dev);
3116 cdev_del(&channel->cdev);
3117 }
Liron Kuch59339922013-01-01 18:29:47 +02003118
Joel Nider5556a852011-10-16 10:52:13 +02003119 sps_deregister_bam_device(device->bam_handle);
Liron Kuch59339922013-01-01 18:29:47 +02003120err_clock:
Joel Nider5556a852011-10-16 10:52:13 +02003121err_bam:
Joel Nider5556a852011-10-16 10:52:13 +02003122 tspp_debugfs_exit(device);
3123 for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
3124 tsif_debugfs_exit(&device->tsif[i]);
Joel Nider5556a852011-10-16 10:52:13 +02003125err_irq:
Liron Kuch59339922013-01-01 18:29:47 +02003126 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
3127 if (device->tsif[i].tsif_irq)
3128 free_irq(device->tsif[i].tsif_irq, &device->tsif[i]);
3129 }
3130 if (device->tspp_irq)
3131 free_irq(device->tspp_irq, device);
3132
Joel Nider5556a852011-10-16 10:52:13 +02003133 iounmap(device->bam_props.virt_addr);
3134err_map_bam:
3135err_res_bam:
3136 iounmap(device->base);
3137err_map_dev:
3138err_res_dev:
3139 iounmap(device->tsif[1].base);
3140err_map_tsif1:
3141err_res_tsif1:
3142 iounmap(device->tsif[0].base);
3143err_map_tsif0:
3144err_res_tsif0:
3145 if (device->tsif_ref_clk)
3146 clk_put(device->tsif_ref_clk);
3147err_refclock:
3148 if (device->tsif_pclk)
3149 clk_put(device->tsif_pclk);
3150err_pclock:
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -07003151 if (device->tsif_vreg)
3152 regulator_disable(device->tsif_vreg);
3153err_regultaor:
Hamad Kadmanyef5ce992013-06-03 09:30:50 +03003154 if (device->tsif_bus_client)
3155 msm_bus_scale_unregister_client(device->tsif_bus_client);
Joel Nider5556a852011-10-16 10:52:13 +02003156 kfree(device);
3157
3158out:
3159 return rc;
3160}
3161
3162static int __devexit msm_tspp_remove(struct platform_device *pdev)
3163{
Joel Nider435ad8e2011-12-14 16:53:30 +02003164 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02003165 u32 i;
Liron Kuch605cc122013-02-21 14:25:57 +02003166 int rc;
Joel Nider5556a852011-10-16 10:52:13 +02003167
3168 struct tspp_device *device = platform_get_drvdata(pdev);
3169
Joel Nider435ad8e2011-12-14 16:53:30 +02003170 /* free the buffers, and delete the channels */
3171 for (i = 0; i < TSPP_NUM_CHANNELS; i++) {
3172 channel = &device->channels[i];
3173 tspp_close_channel(device->pdev->id, i);
3174 device_destroy(tspp_class, channel->cdev.dev);
3175 cdev_del(&channel->cdev);
3176 }
3177
Liron Kuch59339922013-01-01 18:29:47 +02003178 /* de-registering BAM device requires clocks */
Liron Kuch605cc122013-02-21 14:25:57 +02003179 rc = tspp_clock_start(device);
3180 if (rc == 0) {
3181 sps_deregister_bam_device(device->bam_handle);
3182 tspp_clock_stop(device);
3183 }
Joel Nider5556a852011-10-16 10:52:13 +02003184
Hamad Kadmany44307d32012-11-25 09:49:51 +02003185 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
Joel Nider5556a852011-10-16 10:52:13 +02003186 tsif_debugfs_exit(&device->tsif[i]);
Hamad Kadmany44307d32012-11-25 09:49:51 +02003187 if (device->tsif[i].tsif_irq)
3188 free_irq(device->tsif[i].tsif_irq, &device->tsif[i]);
3189 }
Joel Nider5556a852011-10-16 10:52:13 +02003190
Hamad Kadmanyef5ce992013-06-03 09:30:50 +03003191 if (device->tsif_bus_client)
3192 msm_bus_scale_unregister_client(device->tsif_bus_client);
3193
Joel Nider5556a852011-10-16 10:52:13 +02003194 wake_lock_destroy(&device->wake_lock);
3195 free_irq(device->tspp_irq, device);
Joel Nider5556a852011-10-16 10:52:13 +02003196
3197 iounmap(device->bam_props.virt_addr);
3198 iounmap(device->base);
3199 for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
3200 iounmap(device->tsif[i].base);
3201
3202 if (device->tsif_ref_clk)
3203 clk_put(device->tsif_ref_clk);
3204
3205 if (device->tsif_pclk)
3206 clk_put(device->tsif_pclk);
3207
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -07003208 if (device->tsif_vreg)
3209 regulator_disable(device->tsif_vreg);
3210
Joel Nider5556a852011-10-16 10:52:13 +02003211 pm_runtime_disable(&pdev->dev);
Liron Kuch605cc122013-02-21 14:25:57 +02003212
Joel Nider5556a852011-10-16 10:52:13 +02003213 kfree(device);
3214
3215 return 0;
3216}
3217
3218/*** power management ***/
3219
3220static int tspp_runtime_suspend(struct device *dev)
3221{
3222 dev_dbg(dev, "pm_runtime: suspending...");
3223 return 0;
3224}
3225
3226static int tspp_runtime_resume(struct device *dev)
3227{
3228 dev_dbg(dev, "pm_runtime: resuming...");
3229 return 0;
3230}
3231
3232static const struct dev_pm_ops tspp_dev_pm_ops = {
3233 .runtime_suspend = tspp_runtime_suspend,
3234 .runtime_resume = tspp_runtime_resume,
3235};
3236
Liron Kuch59339922013-01-01 18:29:47 +02003237static struct of_device_id msm_match_table[] = {
3238 {.compatible = "qcom,msm_tspp"},
3239 {}
3240};
3241
Joel Nider5556a852011-10-16 10:52:13 +02003242static struct platform_driver msm_tspp_driver = {
3243 .probe = msm_tspp_probe,
3244 .remove = __exit_p(msm_tspp_remove),
3245 .driver = {
3246 .name = "msm_tspp",
3247 .pm = &tspp_dev_pm_ops,
Liron Kuch59339922013-01-01 18:29:47 +02003248 .of_match_table = msm_match_table,
Joel Nider5556a852011-10-16 10:52:13 +02003249 },
3250};
3251
3252
3253static int __init mod_init(void)
3254{
Joel Nider5556a852011-10-16 10:52:13 +02003255 int rc;
3256
Joel Nider435ad8e2011-12-14 16:53:30 +02003257 /* make the char devs (channels) */
Joel Nider5556a852011-10-16 10:52:13 +02003258 rc = alloc_chrdev_region(&tspp_minor, 0, TSPP_NUM_CHANNELS, "tspp");
3259 if (rc) {
3260 pr_err("tspp: alloc_chrdev_region failed: %d", rc);
3261 goto err_devrgn;
3262 }
3263
3264 tspp_class = class_create(THIS_MODULE, "tspp");
3265 if (IS_ERR(tspp_class)) {
3266 rc = PTR_ERR(tspp_class);
3267 pr_err("tspp: Error creating class: %d", rc);
3268 goto err_class;
3269 }
3270
Joel Nider435ad8e2011-12-14 16:53:30 +02003271 /* register the driver, and check hardware */
3272 rc = platform_driver_register(&msm_tspp_driver);
3273 if (rc) {
3274 pr_err("tspp: platform_driver_register failed: %d", rc);
3275 goto err_register;
Joel Nider5556a852011-10-16 10:52:13 +02003276 }
3277
3278 return 0;
3279
Joel Nider435ad8e2011-12-14 16:53:30 +02003280err_register:
3281 class_destroy(tspp_class);
Joel Nider5556a852011-10-16 10:52:13 +02003282err_class:
3283 unregister_chrdev_region(0, TSPP_NUM_CHANNELS);
3284err_devrgn:
Joel Nider5556a852011-10-16 10:52:13 +02003285 return rc;
3286}
3287
3288static void __exit mod_exit(void)
3289{
Joel Nider435ad8e2011-12-14 16:53:30 +02003290 /* delete low level driver */
3291 platform_driver_unregister(&msm_tspp_driver);
Joel Nider5556a852011-10-16 10:52:13 +02003292
Joel Nider435ad8e2011-12-14 16:53:30 +02003293 /* delete upper layer interface */
Joel Nider5556a852011-10-16 10:52:13 +02003294 class_destroy(tspp_class);
3295 unregister_chrdev_region(0, TSPP_NUM_CHANNELS);
Joel Nider5556a852011-10-16 10:52:13 +02003296}
3297
3298module_init(mod_init);
3299module_exit(mod_exit);
3300
Joel Nider435ad8e2011-12-14 16:53:30 +02003301MODULE_DESCRIPTION("TSPP platform device and char dev");
Joel Nider5556a852011-10-16 10:52:13 +02003302MODULE_LICENSE("GPL v2");