blob: 405c626aff19e7dff7e021e9928adbe696d08a4a [file] [log] [blame]
Gilad Broner6049ea32014-04-10 09:48:44 +03001/* Copyright (c) 2011-2014, 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;
Gilad Bronerf0f326c2014-06-23 17:56:17 +0300462 bool req_irqs;
Joel Nider435ad8e2011-12-14 16:53:30 +0200463
464 struct dentry *dent;
465 struct dentry *debugfs_regs[ARRAY_SIZE(debugfs_tspp_regs)];
466};
467
468
Joel Nider5556a852011-10-16 10:52:13 +0200469static struct class *tspp_class;
470static int tspp_key_entry;
471static dev_t tspp_minor; /* next minor number to assign */
Joel Nider435ad8e2011-12-14 16:53:30 +0200472
473static LIST_HEAD(tspp_devices);
474
475/* forward declarations */
476static ssize_t tspp_read(struct file *, char __user *, size_t, loff_t *);
477static ssize_t tspp_open(struct inode *inode, struct file *filp);
478static unsigned int tspp_poll(struct file *filp, struct poll_table_struct *p);
479static ssize_t tspp_release(struct inode *inode, struct file *filp);
480static long tspp_ioctl(struct file *, unsigned int, unsigned long);
481
482/* file operations */
483static const struct file_operations tspp_fops = {
484 .owner = THIS_MODULE,
485 .read = tspp_read,
486 .open = tspp_open,
487 .poll = tspp_poll,
488 .release = tspp_release,
489 .unlocked_ioctl = tspp_ioctl,
490};
Joel Nider5556a852011-10-16 10:52:13 +0200491
492/*** IRQ ***/
Joel Nider435ad8e2011-12-14 16:53:30 +0200493static irqreturn_t tspp_isr(int irq, void *dev)
Joel Nider5556a852011-10-16 10:52:13 +0200494{
Joel Nider435ad8e2011-12-14 16:53:30 +0200495 struct tspp_device *device = dev;
Joel Nider5556a852011-10-16 10:52:13 +0200496 u32 status, mask;
497 u32 data;
498
499 status = readl_relaxed(device->base + TSPP_IRQ_STATUS);
500 mask = readl_relaxed(device->base + TSPP_IRQ_MASK);
501 status &= mask;
502
503 if (!status) {
504 dev_warn(&device->pdev->dev, "Spurious interrupt");
505 return IRQ_NONE;
506 }
507
508 /* if (status & TSPP_IRQ_STATUS_TSP_RD_CMPL) */
509
510 if (status & TSPP_IRQ_STATUS_KEY_ERROR) {
511 /* read the key error info */
512 data = readl_relaxed(device->base + TSPP_KEY_ERROR);
513 dev_info(&device->pdev->dev, "key error 0x%x", data);
514 }
515 if (status & TSPP_IRQ_STATUS_KEY_SWITCHED_BAD) {
516 data = readl_relaxed(device->base + TSPP_KEY_VALID);
517 dev_info(&device->pdev->dev, "key invalidated: 0x%x", data);
518 }
519 if (status & TSPP_IRQ_STATUS_KEY_SWITCHED)
520 dev_info(&device->pdev->dev, "key switched");
521
522 if (status & 0xffff)
Joel Nider435ad8e2011-12-14 16:53:30 +0200523 dev_info(&device->pdev->dev, "broken pipe %i", status & 0xffff);
Joel Nider5556a852011-10-16 10:52:13 +0200524
525 writel_relaxed(status, device->base + TSPP_IRQ_CLEAR);
Hamad Kadmany44307d32012-11-25 09:49:51 +0200526
527 /*
528 * Before returning IRQ_HANDLED to the generic interrupt handling
529 * framework need to make sure all operations including clearing of
530 * interrupt status registers in the hardware is performed.
531 * Thus a barrier after clearing the interrupt status register
532 * is required to guarantee that the interrupt status register has
533 * really been cleared by the time we return from this handler.
534 */
535 wmb();
536 return IRQ_HANDLED;
537}
538
539static irqreturn_t tsif_isr(int irq, void *dev)
540{
541 struct tspp_tsif_device *tsif_device = dev;
542 u32 sts_ctl = ioread32(tsif_device->base + TSIF_STS_CTL_OFF);
543
544 if (!(sts_ctl & (TSIF_STS_CTL_PACK_AVAIL |
545 TSIF_STS_CTL_OVERFLOW |
546 TSIF_STS_CTL_LOST_SYNC |
547 TSIF_STS_CTL_TIMEOUT)))
548 return IRQ_NONE;
549
550 if (sts_ctl & TSIF_STS_CTL_OVERFLOW)
551 tsif_device->stat_overflow++;
552
553 if (sts_ctl & TSIF_STS_CTL_LOST_SYNC)
554 tsif_device->stat_lost_sync++;
555
556 if (sts_ctl & TSIF_STS_CTL_TIMEOUT)
557 tsif_device->stat_timeout++;
558
559 iowrite32(sts_ctl, tsif_device->base + TSIF_STS_CTL_OFF);
560
561 /*
562 * Before returning IRQ_HANDLED to the generic interrupt handling
563 * framework need to make sure all operations including clearing of
564 * interrupt status registers in the hardware is performed.
565 * Thus a barrier after clearing the interrupt status register
566 * is required to guarantee that the interrupt status register has
567 * really been cleared by the time we return from this handler.
568 */
Joel Nider5556a852011-10-16 10:52:13 +0200569 wmb();
570 return IRQ_HANDLED;
571}
572
573/*** callbacks ***/
574static void tspp_sps_complete_cb(struct sps_event_notify *notify)
575{
Joel Nider435ad8e2011-12-14 16:53:30 +0200576 struct tspp_device *pdev = notify->user;
577 tasklet_schedule(&pdev->tlet);
Joel Nider5556a852011-10-16 10:52:13 +0200578}
579
Hamad Kadmany81cee052012-11-29 14:15:57 +0200580static void tspp_expiration_timer(unsigned long data)
581{
582 struct tspp_device *pdev = (struct tspp_device *)data;
583
584 if (pdev)
585 tasklet_schedule(&pdev->tlet);
586}
587
Joel Nider5556a852011-10-16 10:52:13 +0200588/*** tasklet ***/
589static void tspp_sps_complete_tlet(unsigned long data)
590{
591 int i;
592 int complete;
593 unsigned long flags;
594 struct sps_iovec iovec;
595 struct tspp_channel *channel;
596 struct tspp_device *device = (struct tspp_device *)data;
Joel Nider5556a852011-10-16 10:52:13 +0200597 spin_lock_irqsave(&device->spinlock, flags);
598
599 for (i = 0; i < TSPP_NUM_CHANNELS; i++) {
600 complete = 0;
Joel Nider435ad8e2011-12-14 16:53:30 +0200601 channel = &device->channels[i];
Hamad Kadmany81cee052012-11-29 14:15:57 +0200602
Joel Nider435ad8e2011-12-14 16:53:30 +0200603 if (!channel->used || !channel->waiting)
604 continue;
Joel Nider5556a852011-10-16 10:52:13 +0200605
Hamad Kadmany81cee052012-11-29 14:15:57 +0200606 /* stop the expiration timer */
607 if (channel->expiration_period_ms)
608 del_timer(&channel->expiration_timer);
609
Joel Nider5556a852011-10-16 10:52:13 +0200610 /* get completions */
Joel Nider435ad8e2011-12-14 16:53:30 +0200611 while (channel->waiting->state == TSPP_BUF_STATE_WAITING) {
Joel Nider5556a852011-10-16 10:52:13 +0200612 if (sps_get_iovec(channel->pipe, &iovec) != 0) {
613 pr_err("tspp: Error in iovec on channel %i",
614 channel->id);
615 break;
616 }
617 if (iovec.size == 0)
618 break;
619
Joel Nider435ad8e2011-12-14 16:53:30 +0200620 if (iovec.addr != channel->waiting->sps.phys_base)
Hamad Kadmanya23457e2013-07-02 16:51:20 +0300621 pr_err("tspp: buffer mismatch %pa",
622 &channel->waiting->sps.phys_base);
Joel Nider5556a852011-10-16 10:52:13 +0200623
624 complete = 1;
Joel Nider435ad8e2011-12-14 16:53:30 +0200625 channel->waiting->state = TSPP_BUF_STATE_DATA;
626 channel->waiting->filled = iovec.size;
627 channel->waiting->read_index = 0;
628
Hamad Kadmany44307d32012-11-25 09:49:51 +0200629 if (channel->src == TSPP_SOURCE_TSIF0)
630 device->tsif[0].stat_rx++;
631 else if (channel->src == TSPP_SOURCE_TSIF1)
632 device->tsif[1].stat_rx++;
633
Joel Nider435ad8e2011-12-14 16:53:30 +0200634 /* update the pointers */
635 channel->waiting = channel->waiting->next;
Joel Nider5556a852011-10-16 10:52:13 +0200636 }
637
Joel Nider435ad8e2011-12-14 16:53:30 +0200638 /* wake any waiting processes */
Joel Nider5556a852011-10-16 10:52:13 +0200639 if (complete) {
Joel Nider5556a852011-10-16 10:52:13 +0200640 wake_up_interruptible(&channel->in_queue);
Joel Nider435ad8e2011-12-14 16:53:30 +0200641
642 /* call notifiers */
643 if (channel->notifier)
644 channel->notifier(channel->id,
645 channel->notify_data);
Joel Nider5556a852011-10-16 10:52:13 +0200646 }
Hamad Kadmany81cee052012-11-29 14:15:57 +0200647
648 /* restart expiration timer */
649 if (channel->expiration_period_ms)
650 mod_timer(&channel->expiration_timer,
651 jiffies +
652 MSEC_TO_JIFFIES(
653 channel->expiration_period_ms));
Joel Nider5556a852011-10-16 10:52:13 +0200654 }
655
656 spin_unlock_irqrestore(&device->spinlock, flags);
657}
658
659/*** GPIO functions ***/
Liron Kuch275c0b32013-02-10 15:19:32 +0200660static int tspp_gpios_disable(const struct tspp_tsif_device *tsif_device,
661 const struct msm_gpio *table,
662 int size)
Joel Nider5556a852011-10-16 10:52:13 +0200663{
664 int rc = 0;
665 int i;
666 const struct msm_gpio *g;
Liron Kuch59339922013-01-01 18:29:47 +0200667
Joel Nider5556a852011-10-16 10:52:13 +0200668 for (i = size-1; i >= 0; i--) {
669 int tmp;
670 g = table + i;
Liron Kuch59339922013-01-01 18:29:47 +0200671
Liron Kuch275c0b32013-02-10 15:19:32 +0200672 /* don't use sync GPIO when not working in mode 2 */
673 if ((tsif_device->mode != TSPP_TSIF_MODE_2) &&
674 (strnstr(g->label, "sync", strlen(g->label)) != NULL))
675 continue;
676
Liron Kuch59339922013-01-01 18:29:47 +0200677 tmp = gpio_tlmm_config(GPIO_CFG(GPIO_PIN(g->gpio_cfg),
678 0, GPIO_CFG_INPUT, GPIO_CFG_PULL_DOWN, GPIO_CFG_2MA),
679 GPIO_CFG_DISABLE);
Joel Nider5556a852011-10-16 10:52:13 +0200680 if (tmp) {
Liron Kuch72b78552012-10-30 17:47:50 +0200681 pr_err("tspp_gpios_disable(0x%08x, GPIO_CFG_DISABLE) <%s> failed: %d\n",
Joel Nider5556a852011-10-16 10:52:13 +0200682 g->gpio_cfg, g->label ?: "?", rc);
683 pr_err("tspp: pin %d func %d dir %d pull %d drvstr %d\n",
684 GPIO_PIN(g->gpio_cfg), GPIO_FUNC(g->gpio_cfg),
685 GPIO_DIR(g->gpio_cfg), GPIO_PULL(g->gpio_cfg),
686 GPIO_DRVSTR(g->gpio_cfg));
687 if (!rc)
688 rc = tmp;
689 }
690 }
691
692 return rc;
693}
694
Liron Kuch275c0b32013-02-10 15:19:32 +0200695static int tspp_gpios_enable(const struct tspp_tsif_device *tsif_device,
696 const struct msm_gpio *table,
697 int size)
Joel Nider5556a852011-10-16 10:52:13 +0200698{
699 int rc;
Liron Kuch275c0b32013-02-10 15:19:32 +0200700 int i;
Joel Nider5556a852011-10-16 10:52:13 +0200701 const struct msm_gpio *g;
Liron Kuch59339922013-01-01 18:29:47 +0200702
Joel Nider5556a852011-10-16 10:52:13 +0200703 for (i = 0; i < size; i++) {
704 g = table + i;
Liron Kuch275c0b32013-02-10 15:19:32 +0200705
706 /* don't use sync GPIO when not working in mode 2 */
707 if ((tsif_device->mode != TSPP_TSIF_MODE_2) &&
708 (strnstr(g->label, "sync", strlen(g->label)) != NULL))
709 continue;
710
Joel Nider5556a852011-10-16 10:52:13 +0200711 rc = gpio_tlmm_config(g->gpio_cfg, GPIO_CFG_ENABLE);
712 if (rc) {
Liron Kuch72b78552012-10-30 17:47:50 +0200713 pr_err("tspp: gpio_tlmm_config(0x%08x, GPIO_CFG_ENABLE) <%s> failed: %d\n",
Joel Nider5556a852011-10-16 10:52:13 +0200714 g->gpio_cfg, g->label ?: "?", rc);
715 pr_err("tspp: pin %d func %d dir %d pull %d drvstr %d\n",
716 GPIO_PIN(g->gpio_cfg), GPIO_FUNC(g->gpio_cfg),
717 GPIO_DIR(g->gpio_cfg), GPIO_PULL(g->gpio_cfg),
718 GPIO_DRVSTR(g->gpio_cfg));
719 goto err;
720 }
721 }
722 return 0;
723err:
Liron Kuch275c0b32013-02-10 15:19:32 +0200724 tspp_gpios_disable(tsif_device, table, i);
Joel Nider5556a852011-10-16 10:52:13 +0200725
Joel Nider5556a852011-10-16 10:52:13 +0200726 return rc;
727}
728
Liron Kuch275c0b32013-02-10 15:19:32 +0200729
730static int tspp_config_gpios(struct tspp_device *device,
731 enum tspp_source source,
732 int enable)
Joel Nider5556a852011-10-16 10:52:13 +0200733{
Liron Kuch275c0b32013-02-10 15:19:32 +0200734 const struct msm_gpio *table;
735 struct msm_tspp_platform_data *pdata = device->pdev->dev.platform_data;
736 int num_gpios = (pdata->num_gpios / TSPP_TSIF_INSTANCES);
737 int i = 0;
Liron Kuch59339922013-01-01 18:29:47 +0200738
Liron Kuch275c0b32013-02-10 15:19:32 +0200739 if (num_gpios != TSPP_GPIOS_PER_TSIF) {
740 pr_err("tspp %s: unexpected number of GPIOs %d, expected %d\n",
741 __func__, num_gpios, TSPP_GPIOS_PER_TSIF);
742 return -EINVAL;
743 }
Joel Nider5556a852011-10-16 10:52:13 +0200744
Liron Kuch275c0b32013-02-10 15:19:32 +0200745 /*
746 * Note: this code assumes that the GPIO definitions in the
747 * pdata->gpios table are according to the TSIF instance number,
748 * i.e., that TSIF0 GPIOs are defined first, then TSIF1 GPIOs etc.
749 */
750 switch (source) {
751 case TSPP_SOURCE_TSIF0:
752 i = 0;
753 break;
754 case TSPP_SOURCE_TSIF1:
755 i = 1;
756 break;
757 default:
758 pr_err("tspp %s: invalid source\n", __func__);
759 return -EINVAL;
760 }
Liron Kuch59339922013-01-01 18:29:47 +0200761
Liron Kuch275c0b32013-02-10 15:19:32 +0200762 table = pdata->gpios + (i * num_gpios);
763 if (enable)
764 return tspp_gpios_enable(&device->tsif[i], table, num_gpios);
765 else
766 return tspp_gpios_disable(&device->tsif[i], table, num_gpios);
Joel Nider5556a852011-10-16 10:52:13 +0200767}
768
Joel Nider435ad8e2011-12-14 16:53:30 +0200769/*** Clock functions ***/
770static int tspp_clock_start(struct tspp_device *device)
771{
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -0700772 int rc;
773
Liron Kuch605cc122013-02-21 14:25:57 +0200774 if (device == NULL) {
775 pr_err("tspp: Can't start clocks, invalid device\n");
776 return -EINVAL;
777 }
778
Hamad Kadmanyef5ce992013-06-03 09:30:50 +0300779 if (device->tsif_bus_client) {
780 rc = msm_bus_scale_client_update_request(
781 device->tsif_bus_client, 1);
782 if (rc) {
783 pr_err("tspp: Can't enable bus\n");
784 return -EBUSY;
785 }
786 }
787
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -0700788 if (device->tsif_vreg) {
789 rc = regulator_set_voltage(device->tsif_vreg,
790 RPM_REGULATOR_CORNER_SUPER_TURBO,
791 RPM_REGULATOR_CORNER_SUPER_TURBO);
792 if (rc) {
793 pr_err("Unable to set CX voltage.\n");
Hamad Kadmanyef5ce992013-06-03 09:30:50 +0300794 if (device->tsif_bus_client)
795 msm_bus_scale_client_update_request(
796 device->tsif_bus_client, 0);
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -0700797 return rc;
798 }
799 }
800
Joel Nider435ad8e2011-12-14 16:53:30 +0200801 if (device->tsif_pclk && clk_prepare_enable(device->tsif_pclk) != 0) {
802 pr_err("tspp: Can't start pclk");
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -0700803
804 if (device->tsif_vreg) {
805 regulator_set_voltage(device->tsif_vreg,
Hamad Kadmanyfde29382013-06-26 08:03:51 +0300806 RPM_REGULATOR_CORNER_NONE,
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -0700807 RPM_REGULATOR_CORNER_SUPER_TURBO);
808 }
Hamad Kadmanyef5ce992013-06-03 09:30:50 +0300809
810 if (device->tsif_bus_client)
811 msm_bus_scale_client_update_request(
812 device->tsif_bus_client, 0);
Joel Nider435ad8e2011-12-14 16:53:30 +0200813 return -EBUSY;
814 }
815
816 if (device->tsif_ref_clk &&
817 clk_prepare_enable(device->tsif_ref_clk) != 0) {
818 pr_err("tspp: Can't start ref clk");
819 clk_disable_unprepare(device->tsif_pclk);
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -0700820 if (device->tsif_vreg) {
821 regulator_set_voltage(device->tsif_vreg,
Hamad Kadmanyfde29382013-06-26 08:03:51 +0300822 RPM_REGULATOR_CORNER_NONE,
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -0700823 RPM_REGULATOR_CORNER_SUPER_TURBO);
824 }
Hamad Kadmanyef5ce992013-06-03 09:30:50 +0300825
826 if (device->tsif_bus_client)
827 msm_bus_scale_client_update_request(
828 device->tsif_bus_client, 0);
Joel Nider435ad8e2011-12-14 16:53:30 +0200829 return -EBUSY;
830 }
831
832 return 0;
833}
834
835static void tspp_clock_stop(struct tspp_device *device)
836{
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -0700837 int rc;
838
Liron Kuch605cc122013-02-21 14:25:57 +0200839 if (device == NULL) {
840 pr_err("tspp: Can't stop clocks, invalid device\n");
841 return;
842 }
843
Joel Nider435ad8e2011-12-14 16:53:30 +0200844 if (device->tsif_pclk)
Liron Kuch605cc122013-02-21 14:25:57 +0200845 clk_disable_unprepare(device->tsif_pclk);
Joel Nider435ad8e2011-12-14 16:53:30 +0200846
847 if (device->tsif_ref_clk)
Liron Kuch605cc122013-02-21 14:25:57 +0200848 clk_disable_unprepare(device->tsif_ref_clk);
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -0700849
850 if (device->tsif_vreg) {
851 rc = regulator_set_voltage(device->tsif_vreg,
Hamad Kadmanyfde29382013-06-26 08:03:51 +0300852 RPM_REGULATOR_CORNER_NONE,
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -0700853 RPM_REGULATOR_CORNER_SUPER_TURBO);
854 if (rc)
855 pr_err("Unable to set CX voltage.\n");
856 }
Hamad Kadmanyef5ce992013-06-03 09:30:50 +0300857
858 if (device->tsif_bus_client) {
859 rc = msm_bus_scale_client_update_request(
860 device->tsif_bus_client, 0);
861 if (rc)
862 pr_err("tspp: Can't disable bus\n");
863 }
Joel Nider435ad8e2011-12-14 16:53:30 +0200864}
865
Joel Nider5556a852011-10-16 10:52:13 +0200866/*** TSIF functions ***/
867static int tspp_start_tsif(struct tspp_tsif_device *tsif_device)
868{
869 int start_hardware = 0;
870 u32 ctl;
871
872 if (tsif_device->ref_count == 0) {
873 start_hardware = 1;
874 } else if (tsif_device->ref_count > 0) {
875 ctl = readl_relaxed(tsif_device->base + TSIF_STS_CTL_OFF);
876 if ((ctl & TSIF_STS_CTL_START) != 1) {
877 /* this hardware should already be running */
878 pr_warn("tspp: tsif hw not started but ref count > 0");
879 start_hardware = 1;
880 }
881 }
882
883 if (start_hardware) {
Joel Nider435ad8e2011-12-14 16:53:30 +0200884 ctl = TSIF_STS_CTL_EN_IRQ |
Joel Nider5556a852011-10-16 10:52:13 +0200885 TSIF_STS_CTL_EN_DM;
Hamad Kadmany92705b32012-10-23 14:15:41 +0200886
887 if (tsif_device->clock_inverse)
888 ctl |= TSIF_STS_CTL_INV_CLOCK;
889
890 if (tsif_device->data_inverse)
891 ctl |= TSIF_STS_CTL_INV_DATA;
892
893 if (tsif_device->sync_inverse)
894 ctl |= TSIF_STS_CTL_INV_SYNC;
895
896 if (tsif_device->enable_inverse)
897 ctl |= TSIF_STS_CTL_INV_ENABLE;
898
Joel Nider435ad8e2011-12-14 16:53:30 +0200899 switch (tsif_device->mode) {
900 case TSPP_TSIF_MODE_LOOPBACK:
901 ctl |= TSIF_STS_CTL_EN_NULL |
902 TSIF_STS_CTL_EN_ERROR |
903 TSIF_STS_CTL_TEST_MODE;
904 break;
905 case TSPP_TSIF_MODE_1:
906 ctl |= TSIF_STS_CTL_EN_TIME_LIM |
907 TSIF_STS_CTL_EN_TCR;
908 break;
909 case TSPP_TSIF_MODE_2:
910 ctl |= TSIF_STS_CTL_EN_TIME_LIM |
911 TSIF_STS_CTL_EN_TCR |
912 TSIF_STS_CTL_MODE_2;
913 break;
914 default:
915 pr_warn("tspp: unknown tsif mode 0x%x",
916 tsif_device->mode);
Joel Nider5556a852011-10-16 10:52:13 +0200917 }
918 writel_relaxed(ctl, tsif_device->base + TSIF_STS_CTL_OFF);
919 writel_relaxed(tsif_device->time_limit,
920 tsif_device->base + TSIF_TIME_LIMIT_OFF);
921 wmb();
922 writel_relaxed(ctl | TSIF_STS_CTL_START,
923 tsif_device->base + TSIF_STS_CTL_OFF);
924 wmb();
Joel Nider5556a852011-10-16 10:52:13 +0200925 }
926
Joel Nider435ad8e2011-12-14 16:53:30 +0200927 ctl = readl_relaxed(tsif_device->base + TSIF_STS_CTL_OFF);
Gilad Broner6049ea32014-04-10 09:48:44 +0300928 if (!(ctl & TSIF_STS_CTL_START))
929 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +0200930
Gilad Broner6049ea32014-04-10 09:48:44 +0300931 tsif_device->ref_count++;
932 return 0;
Joel Nider5556a852011-10-16 10:52:13 +0200933}
934
935static void tspp_stop_tsif(struct tspp_tsif_device *tsif_device)
936{
937 if (tsif_device->ref_count == 0)
938 return;
939
940 tsif_device->ref_count--;
941
942 if (tsif_device->ref_count == 0) {
943 writel_relaxed(TSIF_STS_CTL_STOP,
944 tsif_device->base + TSIF_STS_CTL_OFF);
945 wmb();
946 }
947}
948
Joel Nider435ad8e2011-12-14 16:53:30 +0200949/*** local TSPP functions ***/
950static int tspp_channels_in_use(struct tspp_device *pdev)
951{
952 int i;
953 int count = 0;
954 for (i = 0; i < TSPP_NUM_CHANNELS; i++)
955 count += (pdev->channels[i].used ? 1 : 0);
956
957 return count;
958}
959
960static struct tspp_device *tspp_find_by_id(int id)
961{
962 struct tspp_device *dev;
963 list_for_each_entry(dev, &tspp_devices, devlist) {
964 if (dev->pdev->id == id)
965 return dev;
966 }
967 return NULL;
968}
969
Joel Nider5556a852011-10-16 10:52:13 +0200970static int tspp_get_key_entry(void)
971{
972 int i;
973 for (i = 0; i < TSPP_NUM_KEYS; i++) {
974 if (!(tspp_key_entry & (1 << i))) {
975 tspp_key_entry |= (1 << i);
976 return i;
977 }
978 }
Joel Nider435ad8e2011-12-14 16:53:30 +0200979 return 1 < TSPP_NUM_KEYS;
Joel Nider5556a852011-10-16 10:52:13 +0200980}
981
982static void tspp_free_key_entry(int entry)
983{
984 if (entry > TSPP_NUM_KEYS) {
985 pr_err("tspp_free_key_entry: index out of bounds");
986 return;
987 }
988
989 tspp_key_entry &= ~(1 << entry);
990}
991
Joel Nider435ad8e2011-12-14 16:53:30 +0200992static int tspp_alloc_buffer(u32 channel_id, struct tspp_data_descriptor *desc,
Hamad Kadmany090709b2013-01-06 12:08:13 +0200993 u32 size, struct dma_pool *dma_pool, tspp_allocator *alloc, void *user)
Joel Nider5556a852011-10-16 10:52:13 +0200994{
Joel Nider435ad8e2011-12-14 16:53:30 +0200995 if (size < TSPP_MIN_BUFFER_SIZE ||
996 size > TSPP_MAX_BUFFER_SIZE) {
997 pr_err("tspp: bad buffer size %i", size);
Joel Nider5556a852011-10-16 10:52:13 +0200998 return -ENOMEM;
999 }
Joel Nider435ad8e2011-12-14 16:53:30 +02001000
1001 if (alloc) {
1002 TSPP_DEBUG("tspp using alloc function");
1003 desc->virt_base = alloc(channel_id, size,
1004 &desc->phys_base, user);
1005 } else {
Hamad Kadmany090709b2013-01-06 12:08:13 +02001006 if (!dma_pool)
1007 desc->virt_base = dma_alloc_coherent(NULL, size,
1008 &desc->phys_base, GFP_KERNEL);
1009 else
1010 desc->virt_base = dma_pool_alloc(dma_pool, GFP_KERNEL,
1011 &desc->phys_base);
1012
Liron Kuch72b78552012-10-30 17:47:50 +02001013 if (desc->virt_base == 0) {
Hamad Kadmany090709b2013-01-06 12:08:13 +02001014 pr_err("tspp: dma buffer allocation failed %i\n", size);
Liron Kuch72b78552012-10-30 17:47:50 +02001015 return -ENOMEM;
1016 }
Joel Nider435ad8e2011-12-14 16:53:30 +02001017 }
1018
1019 desc->size = size;
1020 return 0;
1021}
1022
1023static int tspp_queue_buffer(struct tspp_channel *channel,
1024 struct tspp_mem_buffer *buffer)
1025{
1026 int rc;
1027 u32 flags = 0;
1028
1029 /* make sure the interrupt frequency is valid */
1030 if (channel->int_freq < 1)
1031 channel->int_freq = 1;
1032
1033 /* generate interrupt according to requested frequency */
1034 if (buffer->desc.id % channel->int_freq == channel->int_freq-1)
Hamad Kadmany81cee052012-11-29 14:15:57 +02001035 flags = SPS_IOVEC_FLAG_INT;
Joel Nider435ad8e2011-12-14 16:53:30 +02001036
1037 /* start the transfer */
1038 rc = sps_transfer_one(channel->pipe,
1039 buffer->sps.phys_base,
1040 buffer->sps.size,
1041 channel->pdev,
1042 flags);
1043 if (rc < 0)
1044 return rc;
1045
1046 buffer->state = TSPP_BUF_STATE_WAITING;
Joel Nider5556a852011-10-16 10:52:13 +02001047
1048 return 0;
1049}
1050
1051static int tspp_global_reset(struct tspp_device *pdev)
1052{
1053 u32 i, val;
1054
1055 /* stop all TSIFs */
1056 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
1057 pdev->tsif[i].ref_count = 1; /* allows stopping hw */
1058 tspp_stop_tsif(&pdev->tsif[i]); /* will reset ref_count to 0 */
1059 pdev->tsif[i].time_limit = TSPP_TSIF_DEFAULT_TIME_LIMIT;
Hamad Kadmany92705b32012-10-23 14:15:41 +02001060 pdev->tsif[i].clock_inverse = 0;
1061 pdev->tsif[i].data_inverse = 0;
1062 pdev->tsif[i].sync_inverse = 0;
1063 pdev->tsif[i].enable_inverse = 0;
Joel Nider5556a852011-10-16 10:52:13 +02001064 }
1065 writel_relaxed(TSPP_RST_RESET, pdev->base + TSPP_RST);
1066 wmb();
1067
Joel Nider5556a852011-10-16 10:52:13 +02001068 /* TSPP tables */
1069 for (i = 0; i < TSPP_FILTER_TABLES; i++)
Joel Nider435ad8e2011-12-14 16:53:30 +02001070 memset(pdev->filters[i],
Joel Nider5556a852011-10-16 10:52:13 +02001071 0, sizeof(struct tspp_pid_filter_table));
1072
1073 /* disable all filters */
1074 val = (2 << TSPP_NUM_CHANNELS) - 1;
1075 writel_relaxed(val, pdev->base + TSPP_PS_DISABLE);
1076
1077 /* TSPP registers */
1078 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1079 writel_relaxed(val | TSPP_CLK_CONTROL_FORCE_PERF_CNT,
1080 pdev->base + TSPP_CONTROL);
1081 wmb();
Joel Nider435ad8e2011-12-14 16:53:30 +02001082 memset(pdev->tspp_global_performance, 0,
Joel Nider5556a852011-10-16 10:52:13 +02001083 sizeof(struct tspp_global_performance_regs));
Joel Nider435ad8e2011-12-14 16:53:30 +02001084 memset(pdev->tspp_pipe_context, 0,
Joel Nider5556a852011-10-16 10:52:13 +02001085 sizeof(struct tspp_pipe_context_regs));
Joel Nider435ad8e2011-12-14 16:53:30 +02001086 memset(pdev->tspp_pipe_performance, 0,
Joel Nider5556a852011-10-16 10:52:13 +02001087 sizeof(struct tspp_pipe_performance_regs));
1088 wmb();
1089 writel_relaxed(val & ~TSPP_CLK_CONTROL_FORCE_PERF_CNT,
1090 pdev->base + TSPP_CONTROL);
1091 wmb();
1092
1093 val = readl_relaxed(pdev->base + TSPP_CONFIG);
1094 val &= ~(TSPP_CONFIG_PS_LEN_ERR_MASK |
1095 TSPP_CONFIG_PS_CONT_ERR_UNSP_MASK |
1096 TSPP_CONFIG_PS_CONT_ERR_MASK);
1097 TSPP_CONFIG_SET_PACKET_LENGTH(val, TSPP_PACKET_LENGTH);
1098 writel_relaxed(val, pdev->base + TSPP_CONFIG);
Hamad Kadmany6bac7832012-12-20 18:30:40 +02001099 writel_relaxed(0x0007ffff, pdev->base + TSPP_IRQ_MASK);
Joel Nider5556a852011-10-16 10:52:13 +02001100 writel_relaxed(0x000fffff, pdev->base + TSPP_IRQ_CLEAR);
1101 writel_relaxed(0, pdev->base + TSPP_RST);
1102 wmb();
1103
1104 tspp_key_entry = 0;
1105
1106 return 0;
1107}
1108
Joel Nider435ad8e2011-12-14 16:53:30 +02001109static int tspp_select_source(u32 dev, u32 channel_id,
1110 struct tspp_select_source *src)
1111{
1112 /* make sure the requested src id is in bounds */
1113 if (src->source > TSPP_SOURCE_MEM) {
1114 pr_err("tspp source out of bounds");
1115 return -EINVAL;
1116 }
1117
1118 /* open the stream */
Hamad Kadmany92705b32012-10-23 14:15:41 +02001119 tspp_open_stream(dev, channel_id, src);
Joel Nider435ad8e2011-12-14 16:53:30 +02001120
1121 return 0;
1122}
1123
1124static int tspp_set_iv(struct tspp_channel *channel, struct tspp_iv *iv)
1125{
1126 struct tspp_device *pdev = channel->pdev;
1127
1128 writel_relaxed(iv->data[0], pdev->base + TSPP_CBC_INIT_VAL(0));
1129 writel_relaxed(iv->data[1], pdev->base + TSPP_CBC_INIT_VAL(1));
1130 return 0;
1131}
1132
1133static int tspp_set_system_keys(struct tspp_channel *channel,
1134 struct tspp_system_keys *keys)
1135{
1136 int i;
1137 struct tspp_device *pdev = channel->pdev;
1138
1139 for (i = 0; i < TSPP_NUM_SYSTEM_KEYS; i++)
1140 writel_relaxed(keys->data[i], pdev->base + TSPP_SYSTEM_KEY(i));
1141
1142 return 0;
1143}
1144
1145static int tspp_channel_init(struct tspp_channel *channel,
1146 struct tspp_device *pdev)
1147{
1148 channel->cdev.owner = THIS_MODULE;
1149 cdev_init(&channel->cdev, &tspp_fops);
1150 channel->pdev = pdev;
1151 channel->data = NULL;
1152 channel->read = NULL;
1153 channel->waiting = NULL;
1154 channel->locked = NULL;
1155 channel->id = MINOR(tspp_minor);
1156 channel->used = 0;
1157 channel->buffer_size = TSPP_MIN_BUFFER_SIZE;
1158 channel->max_buffers = TSPP_NUM_BUFFERS;
1159 channel->buffer_count = 0;
1160 channel->filter_count = 0;
1161 channel->int_freq = 1;
Liron Kuch72b78552012-10-30 17:47:50 +02001162 channel->src = TSPP_SOURCE_NONE;
1163 channel->mode = TSPP_MODE_DISABLED;
Joel Nider435ad8e2011-12-14 16:53:30 +02001164 channel->notifier = NULL;
1165 channel->notify_data = NULL;
Hamad Kadmany81cee052012-11-29 14:15:57 +02001166 channel->expiration_period_ms = 0;
Liron Kuch72b78552012-10-30 17:47:50 +02001167 channel->memfree = NULL;
1168 channel->user_info = NULL;
Joel Nider435ad8e2011-12-14 16:53:30 +02001169 init_waitqueue_head(&channel->in_queue);
1170
1171 if (cdev_add(&channel->cdev, tspp_minor++, 1) != 0) {
1172 pr_err("tspp: cdev_add failed");
1173 return -EBUSY;
1174 }
1175
1176 channel->dd = device_create(tspp_class, NULL, channel->cdev.dev,
1177 channel, "tspp%02d", channel->id);
1178 if (IS_ERR(channel->dd)) {
1179 pr_err("tspp: device_create failed: %i",
1180 (int)PTR_ERR(channel->dd));
1181 cdev_del(&channel->cdev);
1182 return -EBUSY;
1183 }
1184
1185 return 0;
1186}
1187
1188static int tspp_set_buffer_size(struct tspp_channel *channel,
1189 struct tspp_buffer *buf)
1190{
Liron Kuch72b78552012-10-30 17:47:50 +02001191 if (channel->buffer_count > 0) {
1192 pr_err("tspp: cannot set buffer size - buffers already allocated\n");
1193 return -EPERM;
1194 }
1195
Joel Nider435ad8e2011-12-14 16:53:30 +02001196 if (buf->size < TSPP_MIN_BUFFER_SIZE)
1197 channel->buffer_size = TSPP_MIN_BUFFER_SIZE;
1198 else if (buf->size > TSPP_MAX_BUFFER_SIZE)
1199 channel->buffer_size = TSPP_MAX_BUFFER_SIZE;
1200 else
1201 channel->buffer_size = buf->size;
1202
1203 return 0;
1204}
1205
1206static void tspp_set_tsif_mode(struct tspp_channel *channel,
1207 enum tspp_tsif_mode mode)
1208{
1209 int index;
1210
1211 switch (channel->src) {
1212 case TSPP_SOURCE_TSIF0:
1213 index = 0;
1214 break;
1215 case TSPP_SOURCE_TSIF1:
1216 index = 1;
1217 break;
1218 default:
1219 pr_warn("tspp: can't set mode for non-tsif source %d",
1220 channel->src);
1221 return;
1222 }
1223 channel->pdev->tsif[index].mode = mode;
1224}
1225
Hamad Kadmany92705b32012-10-23 14:15:41 +02001226static void tspp_set_signal_inversion(struct tspp_channel *channel,
Liron Kuch72b78552012-10-30 17:47:50 +02001227 int clock_inverse, int data_inverse,
1228 int sync_inverse, int enable_inverse)
Hamad Kadmany92705b32012-10-23 14:15:41 +02001229{
1230 int index;
1231
1232 switch (channel->src) {
1233 case TSPP_SOURCE_TSIF0:
1234 index = 0;
1235 break;
1236 case TSPP_SOURCE_TSIF1:
1237 index = 1;
1238 break;
1239 default:
1240 return;
1241 }
1242 channel->pdev->tsif[index].clock_inverse = clock_inverse;
1243 channel->pdev->tsif[index].data_inverse = data_inverse;
1244 channel->pdev->tsif[index].sync_inverse = sync_inverse;
1245 channel->pdev->tsif[index].enable_inverse = enable_inverse;
1246}
1247
Liron Kuch72b78552012-10-30 17:47:50 +02001248static int tspp_is_buffer_size_aligned(u32 size, enum tspp_mode mode)
1249{
1250 u32 alignment;
1251
1252 switch (mode) {
1253 case TSPP_MODE_RAW:
1254 /* must be a multiple of 192 */
1255 alignment = (TSPP_PACKET_LENGTH + 4);
1256 if (size % alignment)
1257 return 0;
1258 return 1;
1259
1260 case TSPP_MODE_RAW_NO_SUFFIX:
1261 /* must be a multiple of 188 */
1262 alignment = TSPP_PACKET_LENGTH;
1263 if (size % alignment)
1264 return 0;
1265 return 1;
1266
1267 case TSPP_MODE_DISABLED:
1268 case TSPP_MODE_PES:
1269 default:
1270 /* no alignment requirement */
1271 return 1;
1272 }
1273
1274}
1275
1276static u32 tspp_align_buffer_size_by_mode(u32 size, enum tspp_mode mode)
1277{
1278 u32 new_size;
1279 u32 alignment;
1280
1281 switch (mode) {
1282 case TSPP_MODE_RAW:
1283 /* must be a multiple of 192 */
1284 alignment = (TSPP_PACKET_LENGTH + 4);
1285 break;
1286
1287 case TSPP_MODE_RAW_NO_SUFFIX:
1288 /* must be a multiple of 188 */
1289 alignment = TSPP_PACKET_LENGTH;
1290 break;
1291
1292 case TSPP_MODE_DISABLED:
1293 case TSPP_MODE_PES:
1294 default:
1295 /* no alignment requirement - give the user what he asks for */
1296 alignment = 1;
1297 break;
1298 }
1299 /* align up */
1300 new_size = (((size + alignment - 1) / alignment) * alignment);
1301 return new_size;
1302}
1303
1304static void tspp_destroy_buffers(u32 channel_id, struct tspp_channel *channel)
1305{
1306 int i;
1307 struct tspp_mem_buffer *pbuf, *temp;
1308
1309 pbuf = channel->data;
1310 for (i = 0; i < channel->buffer_count; i++) {
1311 if (pbuf->desc.phys_base) {
1312 if (channel->memfree) {
1313 channel->memfree(channel_id,
1314 pbuf->desc.size,
1315 pbuf->desc.virt_base,
1316 pbuf->desc.phys_base,
1317 channel->user_info);
1318 } else {
Hamad Kadmany090709b2013-01-06 12:08:13 +02001319 if (!channel->dma_pool)
1320 dma_free_coherent(NULL,
1321 pbuf->desc.size,
1322 pbuf->desc.virt_base,
1323 pbuf->desc.phys_base);
1324 else
1325 dma_pool_free(channel->dma_pool,
1326 pbuf->desc.virt_base,
1327 pbuf->desc.phys_base);
Liron Kuch72b78552012-10-30 17:47:50 +02001328 }
1329 pbuf->desc.phys_base = 0;
1330 }
1331 pbuf->desc.virt_base = 0;
1332 pbuf->state = TSPP_BUF_STATE_EMPTY;
1333 temp = pbuf;
1334 pbuf = pbuf->next;
1335 kfree(temp);
1336 }
1337}
1338
Gilad Broner6049ea32014-04-10 09:48:44 +03001339static int msm_tspp_req_irqs(struct tspp_device *device)
1340{
1341 int rc;
1342 int i;
1343 int j;
1344
1345 rc = request_irq(device->tspp_irq, tspp_isr, IRQF_SHARED,
1346 dev_name(&device->pdev->dev), device);
1347 if (rc) {
1348 dev_err(&device->pdev->dev,
1349 "failed to request TSPP IRQ %d : %d",
1350 device->tspp_irq, rc);
1351 return rc;
1352 }
1353
1354 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
1355 rc = request_irq(device->tsif[i].tsif_irq,
1356 tsif_isr, IRQF_SHARED, dev_name(&device->pdev->dev),
1357 &device->tsif[i]);
1358 if (rc) {
1359 dev_err(&device->pdev->dev,
1360 "failed to request TSIF%d IRQ: %d",
1361 i, rc);
1362 goto failed;
1363 }
1364 }
Gilad Bronerf0f326c2014-06-23 17:56:17 +03001365 device->req_irqs = true;
Gilad Broner6049ea32014-04-10 09:48:44 +03001366 return 0;
1367
1368failed:
1369 free_irq(device->tspp_irq, device);
1370 for (j = 0; j < i; j++)
1371 free_irq(device->tsif[j].tsif_irq, device);
1372
1373 return rc;
1374}
1375
1376static inline void msm_tspp_free_irqs(struct tspp_device *device)
1377{
1378 int i;
1379
1380 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
1381 if (device->tsif[i].tsif_irq)
1382 free_irq(device->tsif[i].tsif_irq, &device->tsif[i]);
1383 }
1384
1385 if (device->tspp_irq)
1386 free_irq(device->tspp_irq, device);
Gilad Bronerf0f326c2014-06-23 17:56:17 +03001387 device->req_irqs = false;
Gilad Broner6049ea32014-04-10 09:48:44 +03001388}
1389
Joel Nider435ad8e2011-12-14 16:53:30 +02001390/*** TSPP API functions ***/
Liron Kuch72b78552012-10-30 17:47:50 +02001391
1392/**
1393 * tspp_open_stream - open a TSPP stream for use.
1394 *
1395 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1396 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1397 * @source: stream source parameters.
1398 *
1399 * Return error status
1400 *
1401 */
1402int tspp_open_stream(u32 dev, u32 channel_id,
1403 struct tspp_select_source *source)
Joel Nider5556a852011-10-16 10:52:13 +02001404{
1405 u32 val;
Gilad Broner6049ea32014-04-10 09:48:44 +03001406 int rc;
Joel Nider5556a852011-10-16 10:52:13 +02001407 struct tspp_device *pdev;
Joel Nider435ad8e2011-12-14 16:53:30 +02001408 struct tspp_channel *channel;
Gilad Bronerf0f326c2014-06-23 17:56:17 +03001409 bool req_irqs = false;
Joel Nider5556a852011-10-16 10:52:13 +02001410
Hamad Kadmany92705b32012-10-23 14:15:41 +02001411 TSPP_DEBUG("tspp_open_stream %i %i %i %i",
1412 dev, channel_id, source->source, source->mode);
Liron Kuch72b78552012-10-30 17:47:50 +02001413
Joel Nider435ad8e2011-12-14 16:53:30 +02001414 if (dev >= TSPP_MAX_DEVICES) {
1415 pr_err("tspp: device id out of range");
1416 return -ENODEV;
1417 }
Joel Nider5556a852011-10-16 10:52:13 +02001418
Joel Nider435ad8e2011-12-14 16:53:30 +02001419 if (channel_id >= TSPP_NUM_CHANNELS) {
1420 pr_err("tspp: channel id out of range");
1421 return -ECHRNG;
1422 }
1423
1424 pdev = tspp_find_by_id(dev);
1425 if (!pdev) {
1426 pr_err("tspp_str: can't find device %i", dev);
1427 return -ENODEV;
1428 }
1429 channel = &pdev->channels[channel_id];
Hamad Kadmany92705b32012-10-23 14:15:41 +02001430 channel->src = source->source;
1431 tspp_set_tsif_mode(channel, source->mode);
1432 tspp_set_signal_inversion(channel, source->clk_inverse,
Liron Kuch72b78552012-10-30 17:47:50 +02001433 source->data_inverse, source->sync_inverse,
1434 source->enable_inverse);
Joel Nider5556a852011-10-16 10:52:13 +02001435
Gilad Broner6049ea32014-04-10 09:48:44 +03001436 /* Request IRQ resources on first open */
Gilad Bronerf0f326c2014-06-23 17:56:17 +03001437 if (!pdev->req_irqs && (source->source == TSPP_SOURCE_TSIF0 ||
1438 source->source == TSPP_SOURCE_TSIF1)) {
Gilad Broner6049ea32014-04-10 09:48:44 +03001439 rc = msm_tspp_req_irqs(pdev);
1440 if (rc) {
1441 pr_err("tspp: error requesting irqs\n");
1442 return rc;
1443 }
Gilad Bronerf0f326c2014-06-23 17:56:17 +03001444 req_irqs = true;
Gilad Broner6049ea32014-04-10 09:48:44 +03001445 }
1446
Hamad Kadmany92705b32012-10-23 14:15:41 +02001447 switch (source->source) {
Joel Nider5556a852011-10-16 10:52:13 +02001448 case TSPP_SOURCE_TSIF0:
Liron Kuch275c0b32013-02-10 15:19:32 +02001449 if (tspp_config_gpios(pdev, channel->src, 1) != 0) {
Gilad Broner6049ea32014-04-10 09:48:44 +03001450 rc = -EBUSY;
Liron Kuch275c0b32013-02-10 15:19:32 +02001451 pr_err("tspp: error enabling tsif0 GPIOs\n");
Gilad Broner6049ea32014-04-10 09:48:44 +03001452 goto free_irq;
Liron Kuch275c0b32013-02-10 15:19:32 +02001453 }
Joel Nider5556a852011-10-16 10:52:13 +02001454 /* make sure TSIF0 is running & enabled */
1455 if (tspp_start_tsif(&pdev->tsif[0]) != 0) {
Gilad Broner6049ea32014-04-10 09:48:44 +03001456 rc = -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001457 pr_err("tspp: error starting tsif0");
Gilad Broner6049ea32014-04-10 09:48:44 +03001458 goto free_irq;
Joel Nider5556a852011-10-16 10:52:13 +02001459 }
Liron Kucha7b49ae2013-02-14 16:26:38 +02001460 if (pdev->tsif[0].ref_count == 1) {
1461 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1462 writel_relaxed(val & ~TSPP_CONTROL_TSP_TSIF0_SRC_DIS,
1463 pdev->base + TSPP_CONTROL);
1464 wmb();
1465 }
Joel Nider5556a852011-10-16 10:52:13 +02001466 break;
1467 case TSPP_SOURCE_TSIF1:
Liron Kuch275c0b32013-02-10 15:19:32 +02001468 if (tspp_config_gpios(pdev, channel->src, 1) != 0) {
Gilad Broner6049ea32014-04-10 09:48:44 +03001469 rc = -EBUSY;
Liron Kuch275c0b32013-02-10 15:19:32 +02001470 pr_err("tspp: error enabling tsif1 GPIOs\n");
Gilad Broner6049ea32014-04-10 09:48:44 +03001471 goto free_irq;
Liron Kuch275c0b32013-02-10 15:19:32 +02001472 }
Joel Nider5556a852011-10-16 10:52:13 +02001473 /* make sure TSIF1 is running & enabled */
1474 if (tspp_start_tsif(&pdev->tsif[1]) != 0) {
Gilad Broner6049ea32014-04-10 09:48:44 +03001475 rc = -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001476 pr_err("tspp: error starting tsif1");
Gilad Broner6049ea32014-04-10 09:48:44 +03001477 goto free_irq;
Joel Nider5556a852011-10-16 10:52:13 +02001478 }
Liron Kucha7b49ae2013-02-14 16:26:38 +02001479 if (pdev->tsif[1].ref_count == 1) {
1480 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1481 writel_relaxed(val & ~TSPP_CONTROL_TSP_TSIF1_SRC_DIS,
1482 pdev->base + TSPP_CONTROL);
1483 wmb();
1484 }
Joel Nider5556a852011-10-16 10:52:13 +02001485 break;
1486 case TSPP_SOURCE_MEM:
1487 break;
1488 default:
Hamad Kadmany92705b32012-10-23 14:15:41 +02001489 pr_err("tspp: channel %i invalid source %i",
1490 channel->id, source->source);
Joel Nider435ad8e2011-12-14 16:53:30 +02001491 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001492 }
1493
Joel Nider5556a852011-10-16 10:52:13 +02001494 return 0;
Gilad Broner6049ea32014-04-10 09:48:44 +03001495
1496free_irq:
Gilad Bronerf0f326c2014-06-23 17:56:17 +03001497 /* Free irqs only if were requested during opening of this stream */
1498 if (req_irqs)
Gilad Broner6049ea32014-04-10 09:48:44 +03001499 msm_tspp_free_irqs(pdev);
1500 return rc;
Joel Nider5556a852011-10-16 10:52:13 +02001501}
1502EXPORT_SYMBOL(tspp_open_stream);
1503
Liron Kuch72b78552012-10-30 17:47:50 +02001504/**
1505 * tspp_close_stream - close a TSPP stream.
1506 *
1507 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1508 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1509 *
1510 * Return error status
1511 *
1512 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001513int tspp_close_stream(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001514{
1515 u32 val;
Gilad Broner6049ea32014-04-10 09:48:44 +03001516 u32 prev_ref_count = 0;
Joel Nider5556a852011-10-16 10:52:13 +02001517 struct tspp_device *pdev;
Joel Nider435ad8e2011-12-14 16:53:30 +02001518 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02001519
Joel Nider435ad8e2011-12-14 16:53:30 +02001520 if (channel_id >= TSPP_NUM_CHANNELS) {
1521 pr_err("tspp: channel id out of range");
1522 return -ECHRNG;
1523 }
1524 pdev = tspp_find_by_id(dev);
1525 if (!pdev) {
1526 pr_err("tspp_cs: can't find device %i", dev);
1527 return -EBUSY;
1528 }
1529 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02001530
1531 switch (channel->src) {
1532 case TSPP_SOURCE_TSIF0:
Liron Kucha7b49ae2013-02-14 16:26:38 +02001533 prev_ref_count = pdev->tsif[0].ref_count;
Joel Nider5556a852011-10-16 10:52:13 +02001534 tspp_stop_tsif(&pdev->tsif[0]);
Liron Kuch275c0b32013-02-10 15:19:32 +02001535 if (tspp_config_gpios(pdev, channel->src, 0) != 0)
1536 pr_err("tspp: error disabling tsif0 GPIOs\n");
1537
Liron Kucha7b49ae2013-02-14 16:26:38 +02001538 if (prev_ref_count == 1) {
1539 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1540 writel_relaxed(val | TSPP_CONTROL_TSP_TSIF0_SRC_DIS,
1541 pdev->base + TSPP_CONTROL);
1542 wmb();
1543 }
Joel Nider5556a852011-10-16 10:52:13 +02001544 break;
1545 case TSPP_SOURCE_TSIF1:
Liron Kucha7b49ae2013-02-14 16:26:38 +02001546 prev_ref_count = pdev->tsif[1].ref_count;
Joel Nider5556a852011-10-16 10:52:13 +02001547 tspp_stop_tsif(&pdev->tsif[1]);
Liron Kuch275c0b32013-02-10 15:19:32 +02001548 if (tspp_config_gpios(pdev, channel->src, 0) != 0)
1549 pr_err("tspp: error disabling tsif0 GPIOs\n");
1550
Liron Kucha7b49ae2013-02-14 16:26:38 +02001551 if (prev_ref_count == 1) {
1552 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1553 writel_relaxed(val | TSPP_CONTROL_TSP_TSIF1_SRC_DIS,
1554 pdev->base + TSPP_CONTROL);
1555 wmb();
1556 }
Joel Nider5556a852011-10-16 10:52:13 +02001557 break;
1558 case TSPP_SOURCE_MEM:
1559 break;
1560 case TSPP_SOURCE_NONE:
1561 break;
1562 }
1563
Joel Nider435ad8e2011-12-14 16:53:30 +02001564 channel->src = TSPP_SOURCE_NONE;
Gilad Broner6049ea32014-04-10 09:48:44 +03001565
1566 /* Free requested interrupts to save power */
1567 if ((pdev->tsif[0].ref_count + pdev->tsif[1].ref_count) == 0 &&
1568 prev_ref_count)
1569 msm_tspp_free_irqs(pdev);
1570
Joel Nider5556a852011-10-16 10:52:13 +02001571 return 0;
1572}
1573EXPORT_SYMBOL(tspp_close_stream);
1574
Gilad Bronerf0f326c2014-06-23 17:56:17 +03001575static int tspp_init_sps_device(struct tspp_device *dev)
1576{
1577 int ret;
1578
1579 ret = sps_register_bam_device(&dev->bam_props, &dev->bam_handle);
1580 if (ret) {
1581 pr_err("tspp: failed to register bam device, err-%d\n", ret);
1582 return ret;
1583 }
1584
1585 ret = sps_device_reset(dev->bam_handle);
1586 if (ret) {
1587 sps_deregister_bam_device(dev->bam_handle);
1588 pr_err("tspp: error resetting bam device, err=%d\n", ret);
1589 return ret;
1590 }
1591
1592 return 0;
1593}
1594
Liron Kuch72b78552012-10-30 17:47:50 +02001595/**
1596 * tspp_open_channel - open a TSPP channel.
1597 *
1598 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1599 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1600 *
1601 * Return error status
1602 *
1603 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001604int tspp_open_channel(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001605{
1606 int rc = 0;
Joel Nider435ad8e2011-12-14 16:53:30 +02001607 struct sps_connect *config;
1608 struct sps_register_event *event;
1609 struct tspp_channel *channel;
1610 struct tspp_device *pdev;
1611
1612 if (channel_id >= TSPP_NUM_CHANNELS) {
1613 pr_err("tspp: channel id out of range");
1614 return -ECHRNG;
1615 }
1616 pdev = tspp_find_by_id(dev);
1617 if (!pdev) {
1618 pr_err("tspp_oc: can't find device %i", dev);
1619 return -ENODEV;
1620 }
1621 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02001622
1623 if (channel->used) {
1624 pr_err("tspp channel already in use");
Joel Nider435ad8e2011-12-14 16:53:30 +02001625 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001626 }
1627
Joel Nider435ad8e2011-12-14 16:53:30 +02001628 config = &channel->config;
1629 event = &channel->event;
1630
1631 /* start the clocks if needed */
Liron Kuch59339922013-01-01 18:29:47 +02001632 if (tspp_channels_in_use(pdev) == 0) {
Liron Kuch605cc122013-02-21 14:25:57 +02001633 rc = tspp_clock_start(pdev);
1634 if (rc)
1635 return rc;
1636
Gilad Bronerf0f326c2014-06-23 17:56:17 +03001637 if (pdev->bam_handle == SPS_DEV_HANDLE_INVALID) {
1638 rc = tspp_init_sps_device(pdev);
1639 if (rc) {
1640 pr_err("tspp: failed to init sps device, err=%d\n",
1641 rc);
1642 tspp_clock_stop(pdev);
1643 return rc;
1644 }
1645 }
1646
Joel Nider435ad8e2011-12-14 16:53:30 +02001647 wake_lock(&pdev->wake_lock);
Liron Kuch59339922013-01-01 18:29:47 +02001648 }
Joel Nider435ad8e2011-12-14 16:53:30 +02001649
Joel Nider5556a852011-10-16 10:52:13 +02001650 /* mark it as used */
1651 channel->used = 1;
1652
1653 /* start the bam */
1654 channel->pipe = sps_alloc_endpoint();
1655 if (channel->pipe == 0) {
1656 pr_err("tspp: error allocating endpoint");
1657 rc = -ENOMEM;
1658 goto err_sps_alloc;
1659 }
1660
1661 /* get default configuration */
1662 sps_get_config(channel->pipe, config);
1663
Joel Nider435ad8e2011-12-14 16:53:30 +02001664 config->source = pdev->bam_handle;
Joel Nider5556a852011-10-16 10:52:13 +02001665 config->destination = SPS_DEV_HANDLE_MEM;
1666 config->mode = SPS_MODE_SRC;
Joel Nider435ad8e2011-12-14 16:53:30 +02001667 config->options =
1668 SPS_O_AUTO_ENABLE | /* connection is auto-enabled */
1669 SPS_O_STREAMING | /* streaming mode */
1670 SPS_O_DESC_DONE | /* interrupt on end of descriptor */
Hamad Kadmany81cee052012-11-29 14:15:57 +02001671 SPS_O_ACK_TRANSFERS | /* must use sps_get_iovec() */
1672 SPS_O_HYBRID; /* Read actual descriptors in sps_get_iovec() */
Joel Nider5556a852011-10-16 10:52:13 +02001673 config->src_pipe_index = channel->id;
1674 config->desc.size =
Hamad Kadmany81cee052012-11-29 14:15:57 +02001675 TSPP_SPS_DESCRIPTOR_COUNT * SPS_DESCRIPTOR_SIZE;
Joel Nider5556a852011-10-16 10:52:13 +02001676 config->desc.base = dma_alloc_coherent(NULL,
1677 config->desc.size,
1678 &config->desc.phys_base,
1679 GFP_KERNEL);
1680 if (config->desc.base == 0) {
1681 pr_err("tspp: error allocating sps descriptors");
1682 rc = -ENOMEM;
1683 goto err_desc_alloc;
1684 }
1685
1686 memset(config->desc.base, 0, config->desc.size);
1687
1688 rc = sps_connect(channel->pipe, config);
1689 if (rc) {
1690 pr_err("tspp: error connecting bam");
1691 goto err_connect;
1692 }
1693
1694 event->mode = SPS_TRIGGER_CALLBACK;
Joel Nider435ad8e2011-12-14 16:53:30 +02001695 event->options = SPS_O_DESC_DONE;
Joel Nider5556a852011-10-16 10:52:13 +02001696 event->callback = tspp_sps_complete_cb;
1697 event->xfer_done = NULL;
Joel Nider435ad8e2011-12-14 16:53:30 +02001698 event->user = pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001699
1700 rc = sps_register_event(channel->pipe, event);
1701 if (rc) {
1702 pr_err("tspp: error registering event");
1703 goto err_event;
1704 }
1705
Hamad Kadmany81cee052012-11-29 14:15:57 +02001706 init_timer(&channel->expiration_timer);
1707 channel->expiration_timer.function = tspp_expiration_timer;
1708 channel->expiration_timer.data = (unsigned long)pdev;
1709 channel->expiration_timer.expires = 0xffffffffL;
1710
Joel Nider435ad8e2011-12-14 16:53:30 +02001711 rc = pm_runtime_get(&pdev->pdev->dev);
Joel Nider5556a852011-10-16 10:52:13 +02001712 if (rc < 0) {
Joel Nider435ad8e2011-12-14 16:53:30 +02001713 dev_err(&pdev->pdev->dev,
Joel Nider5556a852011-10-16 10:52:13 +02001714 "Runtime PM: Unable to wake up tspp device, rc = %d",
1715 rc);
1716 }
Joel Nider5556a852011-10-16 10:52:13 +02001717 return 0;
1718
1719err_event:
1720 sps_disconnect(channel->pipe);
1721err_connect:
1722 dma_free_coherent(NULL, config->desc.size, config->desc.base,
1723 config->desc.phys_base);
1724err_desc_alloc:
1725 sps_free_endpoint(channel->pipe);
1726err_sps_alloc:
Gilad Bronerf0f326c2014-06-23 17:56:17 +03001727 channel->used = 0;
Joel Nider5556a852011-10-16 10:52:13 +02001728 return rc;
1729}
1730EXPORT_SYMBOL(tspp_open_channel);
1731
Liron Kuch72b78552012-10-30 17:47:50 +02001732/**
1733 * tspp_close_channel - close a TSPP channel.
1734 *
1735 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1736 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1737 *
1738 * Return error status
1739 *
1740 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001741int tspp_close_channel(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001742{
1743 int i;
1744 int id;
Liron Kuch4ed3bf62013-03-28 09:44:42 +02001745 int table_idx;
Joel Nider5556a852011-10-16 10:52:13 +02001746 u32 val;
Liron Kuche0acb412013-04-21 13:16:45 +03001747 unsigned long flags;
Joel Nider5556a852011-10-16 10:52:13 +02001748
Joel Nider435ad8e2011-12-14 16:53:30 +02001749 struct sps_connect *config;
1750 struct tspp_device *pdev;
1751 struct tspp_channel *channel;
Joel Nider435ad8e2011-12-14 16:53:30 +02001752
1753 if (channel_id >= TSPP_NUM_CHANNELS) {
1754 pr_err("tspp: channel id out of range");
1755 return -ECHRNG;
1756 }
1757 pdev = tspp_find_by_id(dev);
1758 if (!pdev) {
1759 pr_err("tspp_close: can't find device %i", dev);
1760 return -ENODEV;
1761 }
1762 channel = &pdev->channels[channel_id];
1763
1764 /* if the channel is not used, we are done */
1765 if (!channel->used)
1766 return 0;
1767
Liron Kuche0acb412013-04-21 13:16:45 +03001768 /*
1769 * Need to protect access to used and waiting fields, as they are
1770 * used by the tasklet which is invoked from interrupt context
1771 */
1772 spin_lock_irqsave(&pdev->spinlock, flags);
1773 channel->used = 0;
1774 channel->waiting = NULL;
1775 spin_unlock_irqrestore(&pdev->spinlock, flags);
1776
Hamad Kadmany81cee052012-11-29 14:15:57 +02001777 if (channel->expiration_period_ms)
1778 del_timer(&channel->expiration_timer);
1779
Joel Nider435ad8e2011-12-14 16:53:30 +02001780 channel->notifier = NULL;
1781 channel->notify_data = NULL;
Hamad Kadmany81cee052012-11-29 14:15:57 +02001782 channel->expiration_period_ms = 0;
Joel Nider435ad8e2011-12-14 16:53:30 +02001783
1784 config = &channel->config;
1785 pdev = channel->pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001786
1787 /* disable pipe (channel) */
1788 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1789 writel_relaxed(val | channel->id, pdev->base + TSPP_PS_DISABLE);
1790 wmb();
1791
1792 /* unregister all filters for this channel */
Liron Kuch4ed3bf62013-03-28 09:44:42 +02001793 for (table_idx = 0; table_idx < TSPP_FILTER_TABLES; table_idx++) {
1794 for (i = 0; i < TSPP_NUM_PRIORITIES; i++) {
1795 struct tspp_pid_filter *filter =
1796 &pdev->filters[table_idx]->filter[i];
1797 id = FILTER_GET_PIPE_NUMBER0(filter);
1798 if (id == channel->id) {
1799 if (FILTER_HAS_ENCRYPTION(filter))
1800 tspp_free_key_entry(
1801 FILTER_GET_KEY_NUMBER(filter));
1802 filter->config = 0;
1803 filter->filter = 0;
1804 }
Joel Nider5556a852011-10-16 10:52:13 +02001805 }
1806 }
1807 channel->filter_count = 0;
1808
Joel Nider5556a852011-10-16 10:52:13 +02001809 /* disconnect the bam */
1810 if (sps_disconnect(channel->pipe) != 0)
1811 pr_warn("tspp: Error freeing sps endpoint (%i)", channel->id);
1812
1813 /* destroy the buffers */
1814 dma_free_coherent(NULL, config->desc.size, config->desc.base,
1815 config->desc.phys_base);
1816
Hamad Kadmany06ccf822013-12-12 14:10:20 +02001817 sps_free_endpoint(channel->pipe);
1818
Liron Kuch72b78552012-10-30 17:47:50 +02001819 tspp_destroy_buffers(channel_id, channel);
Hamad Kadmany090709b2013-01-06 12:08:13 +02001820 if (channel->dma_pool) {
1821 dma_pool_destroy(channel->dma_pool);
1822 channel->dma_pool = NULL;
1823 }
Liron Kuch72b78552012-10-30 17:47:50 +02001824
1825 channel->src = TSPP_SOURCE_NONE;
1826 channel->mode = TSPP_MODE_DISABLED;
1827 channel->memfree = NULL;
1828 channel->user_info = NULL;
Joel Nider5556a852011-10-16 10:52:13 +02001829 channel->buffer_count = 0;
Joel Nider435ad8e2011-12-14 16:53:30 +02001830 channel->data = NULL;
1831 channel->read = NULL;
Joel Nider435ad8e2011-12-14 16:53:30 +02001832 channel->locked = NULL;
Joel Nider5556a852011-10-16 10:52:13 +02001833
Liron Kuch59339922013-01-01 18:29:47 +02001834 if (tspp_channels_in_use(pdev) == 0) {
Gilad Bronerf0f326c2014-06-23 17:56:17 +03001835 sps_deregister_bam_device(pdev->bam_handle);
1836 pdev->bam_handle = SPS_DEV_HANDLE_INVALID;
1837
Joel Nider435ad8e2011-12-14 16:53:30 +02001838 wake_unlock(&pdev->wake_lock);
Liron Kuch59339922013-01-01 18:29:47 +02001839 tspp_clock_stop(pdev);
1840 }
Joel Nider435ad8e2011-12-14 16:53:30 +02001841
Liron Kuch605cc122013-02-21 14:25:57 +02001842 pm_runtime_put(&pdev->pdev->dev);
1843
Joel Nider5556a852011-10-16 10:52:13 +02001844 return 0;
1845}
1846EXPORT_SYMBOL(tspp_close_channel);
1847
Liron Kuch72b78552012-10-30 17:47:50 +02001848/**
Hamad Kadmany6d2a9c72013-01-31 14:49:20 +02001849 * tspp_get_ref_clk_counter - return the TSIF clock reference (TCR) counter.
1850 *
1851 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1852 * @source: The TSIF source from which the counter should be read
1853 * @tcr_counter: the value of TCR counter
1854 *
1855 * Return error status
1856 *
1857 * TCR increments at a rate equal to 27 MHz/256 = 105.47 kHz.
1858 * If source is neither TSIF 0 or TSIF1 0 is returned.
1859 */
1860int tspp_get_ref_clk_counter(u32 dev, enum tspp_source source, u32 *tcr_counter)
1861{
1862 struct tspp_device *pdev;
1863 struct tspp_tsif_device *tsif_device;
1864
1865 if (!tcr_counter)
1866 return -EINVAL;
1867
1868 pdev = tspp_find_by_id(dev);
1869 if (!pdev) {
1870 pr_err("tspp_get_ref_clk_counter: can't find device %i\n", dev);
1871 return -ENODEV;
1872 }
1873
1874 switch (source) {
1875 case TSPP_SOURCE_TSIF0:
1876 tsif_device = &pdev->tsif[0];
1877 break;
1878
1879 case TSPP_SOURCE_TSIF1:
1880 tsif_device = &pdev->tsif[1];
1881 break;
1882
1883 default:
1884 tsif_device = NULL;
1885 break;
1886 }
1887
1888 if (tsif_device && tsif_device->ref_count)
1889 *tcr_counter = ioread32(tsif_device->base + TSIF_CLK_REF_OFF);
1890 else
1891 *tcr_counter = 0;
1892
1893 return 0;
1894}
1895EXPORT_SYMBOL(tspp_get_ref_clk_counter);
1896
1897/**
Liron Kuch72b78552012-10-30 17:47:50 +02001898 * tspp_add_filter - add a TSPP filter to a channel.
1899 *
1900 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1901 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1902 * @filter: TSPP filter parameters
1903 *
1904 * Return error status
1905 *
1906 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001907int tspp_add_filter(u32 dev, u32 channel_id,
Joel Nider5556a852011-10-16 10:52:13 +02001908 struct tspp_filter *filter)
1909{
Liron Kuch72b78552012-10-30 17:47:50 +02001910 int i, rc;
Joel Nider5556a852011-10-16 10:52:13 +02001911 int other_channel;
1912 int entry;
1913 u32 val, pid, enabled;
Joel Nider435ad8e2011-12-14 16:53:30 +02001914 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001915 struct tspp_pid_filter p;
Joel Nider435ad8e2011-12-14 16:53:30 +02001916 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02001917
Joel Nider435ad8e2011-12-14 16:53:30 +02001918 TSPP_DEBUG("tspp: add filter");
1919 if (channel_id >= TSPP_NUM_CHANNELS) {
1920 pr_err("tspp: channel id out of range");
1921 return -ECHRNG;
1922 }
1923 pdev = tspp_find_by_id(dev);
1924 if (!pdev) {
1925 pr_err("tspp_add: can't find device %i", dev);
1926 return -ENODEV;
1927 }
1928
1929 channel = &pdev->channels[channel_id];
1930
Joel Nider5556a852011-10-16 10:52:13 +02001931 if (filter->source > TSPP_SOURCE_MEM) {
1932 pr_err("tspp invalid source");
Joel Nider435ad8e2011-12-14 16:53:30 +02001933 return -ENOSR;
Joel Nider5556a852011-10-16 10:52:13 +02001934 }
1935
1936 if (filter->priority >= TSPP_NUM_PRIORITIES) {
Hamad Kadmany7d8a6f82013-11-17 11:31:01 +02001937 pr_err("tspp invalid filter priority");
Joel Nider435ad8e2011-12-14 16:53:30 +02001938 return -ENOSR;
Joel Nider5556a852011-10-16 10:52:13 +02001939 }
1940
Liron Kuch72b78552012-10-30 17:47:50 +02001941 channel->mode = filter->mode;
1942 /*
1943 * if buffers are already allocated, verify they fulfil
1944 * the alignment requirements.
1945 */
1946 if ((channel->buffer_count > 0) &&
1947 (!tspp_is_buffer_size_aligned(channel->buffer_size, channel->mode)))
1948 pr_warn("tspp: buffers allocated with incorrect alignment\n");
Joel Nider5556a852011-10-16 10:52:13 +02001949
1950 if (filter->mode == TSPP_MODE_PES) {
1951 for (i = 0; i < TSPP_NUM_PRIORITIES; i++) {
1952 struct tspp_pid_filter *tspp_filter =
Joel Nider435ad8e2011-12-14 16:53:30 +02001953 &pdev->filters[channel->src]->filter[i];
Joel Nider5556a852011-10-16 10:52:13 +02001954 pid = FILTER_GET_PIPE_PID((tspp_filter));
1955 enabled = FILTER_GET_PIPE_PROCESS0(tspp_filter);
1956 if (enabled && (pid == filter->pid)) {
1957 other_channel =
1958 FILTER_GET_PIPE_NUMBER0(tspp_filter);
1959 pr_err("tspp: pid 0x%x already in use by channel %i",
1960 filter->pid, other_channel);
Joel Nider435ad8e2011-12-14 16:53:30 +02001961 return -EBADSLT;
Joel Nider5556a852011-10-16 10:52:13 +02001962 }
1963 }
1964 }
1965
1966 /* make sure this priority is not already in use */
1967 enabled = FILTER_GET_PIPE_PROCESS0(
Joel Nider435ad8e2011-12-14 16:53:30 +02001968 (&(pdev->filters[channel->src]->filter[filter->priority])));
Joel Nider5556a852011-10-16 10:52:13 +02001969 if (enabled) {
1970 pr_err("tspp: filter priority %i source %i is already enabled\n",
1971 filter->priority, channel->src);
Joel Nider435ad8e2011-12-14 16:53:30 +02001972 return -ENOSR;
Joel Nider5556a852011-10-16 10:52:13 +02001973 }
1974
1975 if (channel->mode == TSPP_MODE_PES) {
1976 /* if we are already processing in PES mode, disable pipe
1977 (channel) and filter to be updated */
1978 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1979 writel_relaxed(val | (1 << channel->id),
1980 pdev->base + TSPP_PS_DISABLE);
1981 wmb();
1982 }
1983
1984 /* update entry */
1985 p.filter = 0;
Joel Nider435ad8e2011-12-14 16:53:30 +02001986 p.config = FILTER_TRANS_END_DISABLE;
Joel Nider5556a852011-10-16 10:52:13 +02001987 FILTER_SET_PIPE_PROCESS0((&p), filter->mode);
1988 FILTER_SET_PIPE_PID((&p), filter->pid);
1989 FILTER_SET_PID_MASK((&p), filter->mask);
1990 FILTER_SET_PIPE_NUMBER0((&p), channel->id);
1991 FILTER_SET_PIPE_PROCESS1((&p), TSPP_MODE_DISABLED);
1992 if (filter->decrypt) {
1993 entry = tspp_get_key_entry();
1994 if (entry == -1) {
1995 pr_err("tspp: no more keys available!");
1996 } else {
1997 p.config |= FILTER_DECRYPT;
1998 FILTER_SET_KEY_NUMBER((&p), entry);
1999 }
2000 }
Joel Nider5556a852011-10-16 10:52:13 +02002001
Joel Nider435ad8e2011-12-14 16:53:30 +02002002 pdev->filters[channel->src]->
Joel Nider5556a852011-10-16 10:52:13 +02002003 filter[filter->priority].config = p.config;
Joel Nider435ad8e2011-12-14 16:53:30 +02002004 pdev->filters[channel->src]->
Joel Nider5556a852011-10-16 10:52:13 +02002005 filter[filter->priority].filter = p.filter;
2006
Liron Kuch72b78552012-10-30 17:47:50 +02002007 /*
2008 * allocate buffers if needed (i.e. if user did has not already called
2009 * tspp_allocate_buffers() explicitly).
2010 */
2011 if (channel->buffer_count == 0) {
2012 channel->buffer_size =
Hamad Kadmany090709b2013-01-06 12:08:13 +02002013 tspp_align_buffer_size_by_mode(channel->buffer_size,
Liron Kuch72b78552012-10-30 17:47:50 +02002014 channel->mode);
2015 rc = tspp_allocate_buffers(dev, channel->id,
2016 channel->max_buffers,
2017 channel->buffer_size,
2018 channel->int_freq, NULL, NULL, NULL);
2019 if (rc != 0) {
2020 pr_err("tspp: tspp_allocate_buffers failed\n");
2021 return rc;
2022 }
Joel Nider435ad8e2011-12-14 16:53:30 +02002023 }
2024
Joel Nider5556a852011-10-16 10:52:13 +02002025 /* reenable pipe */
2026 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
2027 writel_relaxed(val & ~(1 << channel->id), pdev->base + TSPP_PS_DISABLE);
2028 wmb();
2029 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
2030
Joel Nider5556a852011-10-16 10:52:13 +02002031 channel->filter_count++;
2032
2033 return 0;
2034}
2035EXPORT_SYMBOL(tspp_add_filter);
2036
Liron Kuch72b78552012-10-30 17:47:50 +02002037/**
2038 * tspp_remove_filter - remove a TSPP filter from a channel.
2039 *
2040 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2041 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2042 * @filter: TSPP filter parameters
2043 *
2044 * Return error status
2045 *
2046 */
Joel Nider435ad8e2011-12-14 16:53:30 +02002047int tspp_remove_filter(u32 dev, u32 channel_id,
Joel Nider5556a852011-10-16 10:52:13 +02002048 struct tspp_filter *filter)
2049{
2050 int entry;
2051 u32 val;
Joel Nider435ad8e2011-12-14 16:53:30 +02002052 struct tspp_device *pdev;
2053 int src;
2054 struct tspp_pid_filter *tspp_filter;
2055 struct tspp_channel *channel;
2056
2057 if (channel_id >= TSPP_NUM_CHANNELS) {
2058 pr_err("tspp: channel id out of range");
2059 return -ECHRNG;
2060 }
2061 pdev = tspp_find_by_id(dev);
2062 if (!pdev) {
2063 pr_err("tspp_remove: can't find device %i", dev);
2064 return -ENODEV;
2065 }
Hamad Kadmany7d8a6f82013-11-17 11:31:01 +02002066 if (filter->priority >= TSPP_NUM_PRIORITIES) {
2067 pr_err("tspp invalid filter priority");
2068 return -ENOSR;
2069 }
Joel Nider435ad8e2011-12-14 16:53:30 +02002070 channel = &pdev->channels[channel_id];
2071
2072 src = channel->src;
2073 tspp_filter = &(pdev->filters[src]->filter[filter->priority]);
Joel Nider5556a852011-10-16 10:52:13 +02002074
2075 /* disable pipe (channel) */
2076 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
2077 writel_relaxed(val | channel->id, pdev->base + TSPP_PS_DISABLE);
2078 wmb();
2079
2080 /* update data keys */
2081 if (tspp_filter->config & FILTER_DECRYPT) {
2082 entry = FILTER_GET_KEY_NUMBER(tspp_filter);
2083 tspp_free_key_entry(entry);
2084 }
2085
2086 /* update pid table */
2087 tspp_filter->config = 0;
2088 tspp_filter->filter = 0;
2089
2090 channel->filter_count--;
2091
2092 /* reenable pipe */
2093 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
2094 writel_relaxed(val & ~(1 << channel->id),
2095 pdev->base + TSPP_PS_DISABLE);
2096 wmb();
2097 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
2098
2099 return 0;
2100}
2101EXPORT_SYMBOL(tspp_remove_filter);
2102
Liron Kuch72b78552012-10-30 17:47:50 +02002103/**
2104 * tspp_set_key - set TSPP key in key table.
2105 *
2106 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2107 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2108 * @key: TSPP key parameters
2109 *
2110 * Return error status
2111 *
2112 */
Joel Nider435ad8e2011-12-14 16:53:30 +02002113int tspp_set_key(u32 dev, u32 channel_id, struct tspp_key *key)
Joel Nider5556a852011-10-16 10:52:13 +02002114{
2115 int i;
2116 int id;
2117 int key_index;
2118 int data;
Joel Nider435ad8e2011-12-14 16:53:30 +02002119 struct tspp_channel *channel;
2120 struct tspp_device *pdev;
2121
2122 if (channel_id >= TSPP_NUM_CHANNELS) {
2123 pr_err("tspp: channel id out of range");
2124 return -ECHRNG;
2125 }
2126 pdev = tspp_find_by_id(dev);
2127 if (!pdev) {
2128 pr_err("tspp_set: can't find device %i", dev);
2129 return -ENODEV;
2130 }
2131 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02002132
2133 /* read the key index used by this channel */
2134 for (i = 0; i < TSPP_NUM_PRIORITIES; i++) {
2135 struct tspp_pid_filter *tspp_filter =
Joel Nider435ad8e2011-12-14 16:53:30 +02002136 &(pdev->filters[channel->src]->filter[i]);
Joel Nider5556a852011-10-16 10:52:13 +02002137 id = FILTER_GET_PIPE_NUMBER0(tspp_filter);
2138 if (id == channel->id) {
2139 if (FILTER_HAS_ENCRYPTION(tspp_filter)) {
2140 key_index = FILTER_GET_KEY_NUMBER(tspp_filter);
2141 break;
2142 }
2143 }
2144 }
2145 if (i == TSPP_NUM_PRIORITIES) {
2146 pr_err("tspp: no encryption on this channel");
Joel Nider435ad8e2011-12-14 16:53:30 +02002147 return -ENOKEY;
Joel Nider5556a852011-10-16 10:52:13 +02002148 }
2149
2150 if (key->parity == TSPP_KEY_PARITY_EVEN) {
Joel Nider435ad8e2011-12-14 16:53:30 +02002151 pdev->tspp_key_table->entry[key_index].even_lsb = key->lsb;
2152 pdev->tspp_key_table->entry[key_index].even_msb = key->msb;
Joel Nider5556a852011-10-16 10:52:13 +02002153 } else {
Joel Nider435ad8e2011-12-14 16:53:30 +02002154 pdev->tspp_key_table->entry[key_index].odd_lsb = key->lsb;
2155 pdev->tspp_key_table->entry[key_index].odd_msb = key->msb;
Joel Nider5556a852011-10-16 10:52:13 +02002156 }
2157 data = readl_relaxed(channel->pdev->base + TSPP_KEY_VALID);
2158
2159 return 0;
2160}
2161EXPORT_SYMBOL(tspp_set_key);
2162
Liron Kuch72b78552012-10-30 17:47:50 +02002163/**
2164 * tspp_register_notification - register TSPP channel notification function.
2165 *
2166 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2167 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2168 * @pNotify: notification function
2169 * @userdata: user data to pass to notification function
2170 * @timer_ms: notification for partially filled buffers
2171 *
2172 * Return error status
2173 *
2174 */
Joel Nider435ad8e2011-12-14 16:53:30 +02002175int tspp_register_notification(u32 dev, u32 channel_id,
2176 tspp_notifier *pNotify, void *userdata, u32 timer_ms)
Joel Nider5556a852011-10-16 10:52:13 +02002177{
Joel Nider435ad8e2011-12-14 16:53:30 +02002178 struct tspp_channel *channel;
2179 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02002180
Joel Nider435ad8e2011-12-14 16:53:30 +02002181 if (channel_id >= TSPP_NUM_CHANNELS) {
2182 pr_err("tspp: channel id out of range");
2183 return -ECHRNG;
2184 }
2185 pdev = tspp_find_by_id(dev);
2186 if (!pdev) {
2187 pr_err("tspp_reg: can't find device %i", dev);
2188 return -ENODEV;
2189 }
2190 channel = &pdev->channels[channel_id];
2191 channel->notifier = pNotify;
2192 channel->notify_data = userdata;
Hamad Kadmany81cee052012-11-29 14:15:57 +02002193 channel->expiration_period_ms = timer_ms;
2194
Joel Nider5556a852011-10-16 10:52:13 +02002195 return 0;
2196}
Joel Nider435ad8e2011-12-14 16:53:30 +02002197EXPORT_SYMBOL(tspp_register_notification);
Joel Nider5556a852011-10-16 10:52:13 +02002198
Liron Kuch72b78552012-10-30 17:47:50 +02002199/**
2200 * tspp_unregister_notification - unregister TSPP channel notification function.
2201 *
2202 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2203 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2204 *
2205 * Return error status
2206 *
2207 */
Joel Nider435ad8e2011-12-14 16:53:30 +02002208int tspp_unregister_notification(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02002209{
Joel Nider435ad8e2011-12-14 16:53:30 +02002210 struct tspp_channel *channel;
2211 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02002212
Joel Nider435ad8e2011-12-14 16:53:30 +02002213 if (channel_id >= TSPP_NUM_CHANNELS) {
2214 pr_err("tspp: channel id out of range");
2215 return -ECHRNG;
2216 }
2217 pdev = tspp_find_by_id(dev);
2218 if (!pdev) {
2219 pr_err("tspp_unreg: can't find device %i", dev);
2220 return -ENODEV;
2221 }
2222 channel = &pdev->channels[channel_id];
2223 channel->notifier = NULL;
2224 channel->notify_data = 0;
Joel Nider5556a852011-10-16 10:52:13 +02002225 return 0;
2226}
Joel Nider435ad8e2011-12-14 16:53:30 +02002227EXPORT_SYMBOL(tspp_unregister_notification);
Joel Nider5556a852011-10-16 10:52:13 +02002228
Liron Kuch72b78552012-10-30 17:47:50 +02002229/**
2230 * tspp_get_buffer - get TSPP data buffer.
2231 *
2232 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2233 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2234 *
2235 * Return error status
2236 *
2237 */
Joel Nider435ad8e2011-12-14 16:53:30 +02002238const struct tspp_data_descriptor *tspp_get_buffer(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02002239{
Joel Nider435ad8e2011-12-14 16:53:30 +02002240 struct tspp_mem_buffer *buffer;
2241 struct tspp_channel *channel;
2242 struct tspp_device *pdev;
Gilad Bronerfe77c222014-06-16 11:03:51 +03002243 unsigned long flags;
Joel Nider5556a852011-10-16 10:52:13 +02002244
Joel Nider435ad8e2011-12-14 16:53:30 +02002245 if (channel_id >= TSPP_NUM_CHANNELS) {
2246 pr_err("tspp: channel id out of range");
2247 return NULL;
2248 }
2249 pdev = tspp_find_by_id(dev);
2250 if (!pdev) {
2251 pr_err("tspp_get: can't find device %i", dev);
2252 return NULL;
2253 }
Gilad Bronerfe77c222014-06-16 11:03:51 +03002254
2255 spin_lock_irqsave(&pdev->spinlock, flags);
2256
Joel Nider435ad8e2011-12-14 16:53:30 +02002257 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02002258
Joel Nider435ad8e2011-12-14 16:53:30 +02002259 if (!channel->read) {
Gilad Bronerfe77c222014-06-16 11:03:51 +03002260 spin_unlock_irqrestore(&pdev->spinlock, flags);
Joel Nider435ad8e2011-12-14 16:53:30 +02002261 pr_warn("tspp: no buffer to get on channel %i!",
2262 channel->id);
2263 return NULL;
2264 }
2265
2266 buffer = channel->read;
2267 /* see if we have any buffers ready to read */
Gilad Bronerfe77c222014-06-16 11:03:51 +03002268 if (buffer->state != TSPP_BUF_STATE_DATA) {
2269 spin_unlock_irqrestore(&pdev->spinlock, flags);
2270 return NULL;
2271 }
Joel Nider435ad8e2011-12-14 16:53:30 +02002272
2273 if (buffer->state == TSPP_BUF_STATE_DATA) {
2274 /* mark the buffer as busy */
2275 buffer->state = TSPP_BUF_STATE_LOCKED;
2276
2277 /* increment the pointer along the list */
2278 channel->read = channel->read->next;
2279 }
2280
Gilad Bronerfe77c222014-06-16 11:03:51 +03002281 spin_unlock_irqrestore(&pdev->spinlock, flags);
2282
Joel Nider435ad8e2011-12-14 16:53:30 +02002283 return &buffer->desc;
2284}
2285EXPORT_SYMBOL(tspp_get_buffer);
2286
Liron Kuch72b78552012-10-30 17:47:50 +02002287/**
2288 * tspp_release_buffer - release TSPP data buffer back to TSPP.
2289 *
2290 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2291 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2292 * @descriptor_id: buffer descriptor ID
2293 *
2294 * Return error status
2295 *
2296 */
Joel Nider435ad8e2011-12-14 16:53:30 +02002297int tspp_release_buffer(u32 dev, u32 channel_id, u32 descriptor_id)
2298{
2299 int i, found = 0;
2300 struct tspp_mem_buffer *buffer;
2301 struct tspp_channel *channel;
2302 struct tspp_device *pdev;
Gilad Bronerfe77c222014-06-16 11:03:51 +03002303 unsigned long flags;
Joel Nider435ad8e2011-12-14 16:53:30 +02002304
2305 if (channel_id >= TSPP_NUM_CHANNELS) {
2306 pr_err("tspp: channel id out of range");
2307 return -ECHRNG;
2308 }
2309 pdev = tspp_find_by_id(dev);
2310 if (!pdev) {
2311 pr_err("tspp: can't find device %i", dev);
2312 return -ENODEV;
2313 }
Gilad Bronerfe77c222014-06-16 11:03:51 +03002314
2315 spin_lock_irqsave(&pdev->spinlock, flags);
2316
Joel Nider435ad8e2011-12-14 16:53:30 +02002317 channel = &pdev->channels[channel_id];
2318
2319 if (descriptor_id > channel->buffer_count)
2320 pr_warn("tspp: desc id looks weird 0x%08x", descriptor_id);
2321
2322 /* find the correct descriptor */
2323 buffer = channel->locked;
2324 for (i = 0; i < channel->buffer_count; i++) {
2325 if (buffer->desc.id == descriptor_id) {
2326 found = 1;
2327 break;
2328 }
2329 buffer = buffer->next;
2330 }
2331 channel->locked = channel->locked->next;
2332
2333 if (!found) {
Gilad Bronerfe77c222014-06-16 11:03:51 +03002334 spin_unlock_irqrestore(&pdev->spinlock, flags);
Joel Nider435ad8e2011-12-14 16:53:30 +02002335 pr_err("tspp: cant find desc %i", descriptor_id);
2336 return -EINVAL;
2337 }
2338
2339 /* make sure the buffer is in the expected state */
2340 if (buffer->state != TSPP_BUF_STATE_LOCKED) {
Gilad Bronerfe77c222014-06-16 11:03:51 +03002341 spin_unlock_irqrestore(&pdev->spinlock, flags);
Joel Nider435ad8e2011-12-14 16:53:30 +02002342 pr_err("tspp: buffer %i not locked", descriptor_id);
2343 return -EINVAL;
2344 }
2345 /* unlock the buffer and requeue it */
2346 buffer->state = TSPP_BUF_STATE_WAITING;
2347
2348 if (tspp_queue_buffer(channel, buffer))
2349 pr_warn("tspp: can't requeue buffer");
Gilad Bronerfe77c222014-06-16 11:03:51 +03002350
2351 spin_unlock_irqrestore(&pdev->spinlock, flags);
2352
Joel Nider5556a852011-10-16 10:52:13 +02002353 return 0;
2354}
Joel Nider435ad8e2011-12-14 16:53:30 +02002355EXPORT_SYMBOL(tspp_release_buffer);
2356
Liron Kuch72b78552012-10-30 17:47:50 +02002357/**
2358 * tspp_allocate_buffers - allocate TSPP data buffers.
2359 *
2360 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2361 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2362 * @count: number of buffers to allocate
2363 * @size: size of each buffer to allocate
2364 * @int_freq: interrupt frequency
2365 * @alloc: user defined memory allocator function. Pass NULL for default.
2366 * @memfree: user defined memory free function. Pass NULL for default.
2367 * @user: user data to pass to the memory allocator/free function
2368 *
2369 * Return error status
2370 *
2371 * The user can optionally call this function explicitly to allocate the TSPP
2372 * data buffers. Alternatively, if the user did not call this function, it
2373 * is called implicitly by tspp_add_filter().
2374 */
2375int tspp_allocate_buffers(u32 dev, u32 channel_id, u32 count, u32 size,
2376 u32 int_freq, tspp_allocator *alloc,
2377 tspp_memfree *memfree, void *user)
Joel Nider435ad8e2011-12-14 16:53:30 +02002378{
2379 struct tspp_channel *channel;
2380 struct tspp_device *pdev;
2381 struct tspp_mem_buffer *last = NULL;
2382
2383 TSPP_DEBUG("tspp_allocate_buffers");
2384
2385 if (channel_id >= TSPP_NUM_CHANNELS) {
Liron Kuch72b78552012-10-30 17:47:50 +02002386 pr_err("%s: channel id out of range", __func__);
Joel Nider435ad8e2011-12-14 16:53:30 +02002387 return -ECHRNG;
2388 }
Liron Kuch72b78552012-10-30 17:47:50 +02002389
Joel Nider435ad8e2011-12-14 16:53:30 +02002390 pdev = tspp_find_by_id(dev);
2391 if (!pdev) {
Liron Kuch72b78552012-10-30 17:47:50 +02002392 pr_err("%s: can't find device %i", __func__, dev);
Joel Nider435ad8e2011-12-14 16:53:30 +02002393 return -ENODEV;
2394 }
Liron Kuch72b78552012-10-30 17:47:50 +02002395
2396 if (count < MIN_ACCEPTABLE_BUFFER_COUNT) {
2397 pr_err("%s: tspp requires a minimum of %i buffers\n",
2398 __func__, MIN_ACCEPTABLE_BUFFER_COUNT);
2399 return -EINVAL;
2400 }
2401
Hamad Kadmany24020352013-05-22 12:54:18 +03002402 if (count > TSPP_NUM_BUFFERS) {
2403 pr_err("%s: tspp requires a maximum of %i buffers\n",
2404 __func__, TSPP_NUM_BUFFERS);
2405 return -EINVAL;
2406 }
2407
Joel Nider435ad8e2011-12-14 16:53:30 +02002408 channel = &pdev->channels[channel_id];
Hamad Kadmany090709b2013-01-06 12:08:13 +02002409
Liron Kuch72b78552012-10-30 17:47:50 +02002410 /* allow buffer allocation only if there was no previous buffer
2411 * allocation for this channel.
2412 */
2413 if (channel->buffer_count > 0) {
2414 pr_err("%s: buffers already allocated for channel %u",
2415 __func__, channel_id);
2416 return -EINVAL;
2417 }
Joel Nider435ad8e2011-12-14 16:53:30 +02002418
2419 channel->max_buffers = count;
2420
2421 /* set up interrupt frequency */
Liron Kuch72b78552012-10-30 17:47:50 +02002422 if (int_freq > channel->max_buffers) {
Joel Nider435ad8e2011-12-14 16:53:30 +02002423 int_freq = channel->max_buffers;
Liron Kuch72b78552012-10-30 17:47:50 +02002424 pr_warn("%s: setting interrupt frequency to %u\n",
2425 __func__, int_freq);
Joel Nider435ad8e2011-12-14 16:53:30 +02002426 }
Liron Kuch72b78552012-10-30 17:47:50 +02002427 channel->int_freq = int_freq;
2428 /*
2429 * it is the responsibility of the caller to tspp_allocate_buffers(),
2430 * whether it's the user or the driver, to make sure the size parameter
2431 * is compatible to the channel mode.
2432 */
2433 channel->buffer_size = size;
Joel Nider435ad8e2011-12-14 16:53:30 +02002434
Liron Kuch72b78552012-10-30 17:47:50 +02002435 /* save user defined memory free function for later use */
2436 channel->memfree = memfree;
2437 channel->user_info = user;
2438
Hamad Kadmany090709b2013-01-06 12:08:13 +02002439 /*
2440 * For small buffers, create a DMA pool so that memory
2441 * is not wasted through dma_alloc_coherent.
2442 */
2443 if (TSPP_USE_DMA_POOL(channel->buffer_size)) {
2444 channel->dma_pool = dma_pool_create("tspp",
2445 NULL, channel->buffer_size, 0, 0);
2446 if (!channel->dma_pool) {
2447 pr_err("%s: Can't allocate memory pool\n", __func__);
2448 return -ENOMEM;
2449 }
2450 } else {
2451 channel->dma_pool = NULL;
2452 }
2453
2454
Liron Kuch72b78552012-10-30 17:47:50 +02002455 for (channel->buffer_count = 0;
2456 channel->buffer_count < channel->max_buffers;
Joel Nider435ad8e2011-12-14 16:53:30 +02002457 channel->buffer_count++) {
2458
2459 /* allocate the descriptor */
2460 struct tspp_mem_buffer *desc = (struct tspp_mem_buffer *)
2461 kmalloc(sizeof(struct tspp_mem_buffer), GFP_KERNEL);
2462 if (!desc) {
Liron Kuch72b78552012-10-30 17:47:50 +02002463 pr_warn("%s: Can't allocate desc %i",
2464 __func__, channel->buffer_count);
Joel Nider435ad8e2011-12-14 16:53:30 +02002465 break;
2466 }
2467
2468 desc->desc.id = channel->buffer_count;
2469 /* allocate the buffer */
2470 if (tspp_alloc_buffer(channel_id, &desc->desc,
Hamad Kadmany090709b2013-01-06 12:08:13 +02002471 channel->buffer_size, channel->dma_pool,
2472 alloc, user) != 0) {
Joel Nider435ad8e2011-12-14 16:53:30 +02002473 kfree(desc);
Liron Kuch72b78552012-10-30 17:47:50 +02002474 pr_warn("%s: Can't allocate buffer %i",
2475 __func__, channel->buffer_count);
Joel Nider435ad8e2011-12-14 16:53:30 +02002476 break;
2477 }
2478
2479 /* add the descriptor to the list */
2480 desc->filled = 0;
2481 desc->read_index = 0;
2482 if (!channel->data) {
2483 channel->data = desc;
2484 desc->next = channel->data;
2485 } else {
2486 last->next = desc;
2487 }
2488 last = desc;
2489 desc->next = channel->data;
2490
2491 /* prepare the sps descriptor */
2492 desc->sps.phys_base = desc->desc.phys_base;
2493 desc->sps.base = desc->desc.virt_base;
2494 desc->sps.size = desc->desc.size;
2495
2496 /* start the transfer */
2497 if (tspp_queue_buffer(channel, desc))
Liron Kuch72b78552012-10-30 17:47:50 +02002498 pr_err("%s: can't queue buffer %i",
2499 __func__, desc->desc.id);
2500 }
2501
2502 if (channel->buffer_count < channel->max_buffers) {
2503 /*
2504 * we failed to allocate the requested number of buffers.
2505 * we don't allow a partial success, so need to clean up here.
2506 */
2507 tspp_destroy_buffers(channel_id, channel);
2508 channel->buffer_count = 0;
Hamad Kadmany090709b2013-01-06 12:08:13 +02002509
2510 if (channel->dma_pool) {
2511 dma_pool_destroy(channel->dma_pool);
2512 channel->dma_pool = NULL;
2513 }
Liron Kuch72b78552012-10-30 17:47:50 +02002514 return -ENOMEM;
Joel Nider435ad8e2011-12-14 16:53:30 +02002515 }
2516
2517 channel->waiting = channel->data;
2518 channel->read = channel->data;
2519 channel->locked = channel->data;
Liron Kuch72b78552012-10-30 17:47:50 +02002520
Hamad Kadmany81cee052012-11-29 14:15:57 +02002521 /* Now that buffers are scheduled to HW, kick data expiration timer */
2522 if (channel->expiration_period_ms)
2523 mod_timer(&channel->expiration_timer,
2524 jiffies +
2525 MSEC_TO_JIFFIES(
2526 channel->expiration_period_ms));
2527
Joel Nider435ad8e2011-12-14 16:53:30 +02002528 return 0;
2529}
2530EXPORT_SYMBOL(tspp_allocate_buffers);
Joel Nider5556a852011-10-16 10:52:13 +02002531
2532/*** File Operations ***/
2533static ssize_t tspp_open(struct inode *inode, struct file *filp)
2534{
Joel Nider435ad8e2011-12-14 16:53:30 +02002535 u32 dev;
Joel Nider5556a852011-10-16 10:52:13 +02002536 struct tspp_channel *channel;
Joel Nider435ad8e2011-12-14 16:53:30 +02002537
2538 TSPP_DEBUG("tspp_open");
Joel Nider5556a852011-10-16 10:52:13 +02002539 channel = container_of(inode->i_cdev, struct tspp_channel, cdev);
2540 filp->private_data = channel;
Joel Nider435ad8e2011-12-14 16:53:30 +02002541 dev = channel->pdev->pdev->id;
Joel Nider5556a852011-10-16 10:52:13 +02002542
2543 /* if this channel is already in use, quit */
2544 if (channel->used) {
2545 pr_err("tspp channel %i already in use",
2546 MINOR(channel->cdev.dev));
2547 return -EACCES;
2548 }
2549
Joel Nider435ad8e2011-12-14 16:53:30 +02002550 if (tspp_open_channel(dev, channel->id) != 0) {
Joel Nider5556a852011-10-16 10:52:13 +02002551 pr_err("tspp: error opening channel");
2552 return -EACCES;
2553 }
2554
2555 return 0;
2556}
2557
2558static unsigned int tspp_poll(struct file *filp, struct poll_table_struct *p)
2559{
2560 unsigned long flags;
2561 unsigned int mask = 0;
2562 struct tspp_channel *channel;
2563 channel = filp->private_data;
2564
2565 /* register the wait queue for this channel */
2566 poll_wait(filp, &channel->in_queue, p);
2567
2568 spin_lock_irqsave(&channel->pdev->spinlock, flags);
Joel Nider435ad8e2011-12-14 16:53:30 +02002569 if (channel->read &&
2570 channel->read->state == TSPP_BUF_STATE_DATA)
Joel Nider5556a852011-10-16 10:52:13 +02002571 mask = POLLIN | POLLRDNORM;
2572
2573 spin_unlock_irqrestore(&channel->pdev->spinlock, flags);
2574
2575 return mask;
2576}
2577
2578static ssize_t tspp_release(struct inode *inode, struct file *filp)
2579{
Joel Nider435ad8e2011-12-14 16:53:30 +02002580 struct tspp_channel *channel = filp->private_data;
2581 u32 dev = channel->pdev->pdev->id;
2582 TSPP_DEBUG("tspp_release");
Joel Nider5556a852011-10-16 10:52:13 +02002583
Joel Nider435ad8e2011-12-14 16:53:30 +02002584 tspp_close_channel(dev, channel->id);
Joel Nider5556a852011-10-16 10:52:13 +02002585
2586 return 0;
2587}
2588
2589static ssize_t tspp_read(struct file *filp, char __user *buf, size_t count,
2590 loff_t *f_pos)
2591{
2592 size_t size = 0;
2593 size_t transferred = 0;
2594 struct tspp_channel *channel;
2595 struct tspp_mem_buffer *buffer;
2596 channel = filp->private_data;
2597
2598 TSPP_DEBUG("tspp_read");
Joel Nider435ad8e2011-12-14 16:53:30 +02002599
2600 while (!channel->read) {
2601 if (filp->f_flags & O_NONBLOCK) {
2602 pr_warn("tspp: no buffer on channel %i!",
2603 channel->id);
2604 return -EAGAIN;
2605 }
2606 /* go to sleep if there is nothing to read */
2607 if (wait_event_interruptible(channel->in_queue,
2608 (channel->read != NULL))) {
2609 pr_err("tspp: rude awakening\n");
2610 return -ERESTARTSYS;
2611 }
2612 }
2613
2614 buffer = channel->read;
2615
Joel Nider5556a852011-10-16 10:52:13 +02002616 /* see if we have any buffers ready to read */
2617 while (buffer->state != TSPP_BUF_STATE_DATA) {
2618 if (filp->f_flags & O_NONBLOCK) {
2619 pr_warn("tspp: nothing to read on channel %i!",
2620 channel->id);
2621 return -EAGAIN;
2622 }
2623 /* go to sleep if there is nothing to read */
Joel Nider5556a852011-10-16 10:52:13 +02002624 if (wait_event_interruptible(channel->in_queue,
2625 (buffer->state == TSPP_BUF_STATE_DATA))) {
2626 pr_err("tspp: rude awakening\n");
2627 return -ERESTARTSYS;
2628 }
2629 }
2630
2631 while (buffer->state == TSPP_BUF_STATE_DATA) {
2632 size = min(count, buffer->filled);
Joel Nider5556a852011-10-16 10:52:13 +02002633 if (size == 0)
2634 break;
2635
Joel Nider435ad8e2011-12-14 16:53:30 +02002636 if (copy_to_user(buf, buffer->desc.virt_base +
Joel Nider5556a852011-10-16 10:52:13 +02002637 buffer->read_index, size)) {
2638 pr_err("tspp: error copying to user buffer");
Joel Nider435ad8e2011-12-14 16:53:30 +02002639 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02002640 }
2641 buf += size;
2642 count -= size;
2643 transferred += size;
2644 buffer->read_index += size;
2645
Liron Kuch72b78552012-10-30 17:47:50 +02002646 /*
2647 * after reading the end of the buffer, requeue it,
2648 * and set up for reading the next one
2649 */
Joel Nider435ad8e2011-12-14 16:53:30 +02002650 if (buffer->read_index == buffer->filled) {
Joel Nider5556a852011-10-16 10:52:13 +02002651 buffer->state = TSPP_BUF_STATE_WAITING;
Hamad Kadmany090709b2013-01-06 12:08:13 +02002652
Joel Nider435ad8e2011-12-14 16:53:30 +02002653 if (tspp_queue_buffer(channel, buffer))
2654 pr_err("tspp: can't submit transfer");
Hamad Kadmany090709b2013-01-06 12:08:13 +02002655
Joel Nider435ad8e2011-12-14 16:53:30 +02002656 channel->locked = channel->read;
2657 channel->read = channel->read->next;
Joel Nider5556a852011-10-16 10:52:13 +02002658 }
2659 }
2660
2661 return transferred;
2662}
2663
2664static long tspp_ioctl(struct file *filp,
2665 unsigned int param0, unsigned long param1)
2666{
Joel Nider435ad8e2011-12-14 16:53:30 +02002667 u32 dev;
Joel Nider5556a852011-10-16 10:52:13 +02002668 int rc = -1;
2669 struct tspp_channel *channel;
Joel Nider435ad8e2011-12-14 16:53:30 +02002670 struct tspp_select_source ss;
2671 struct tspp_filter f;
2672 struct tspp_key k;
2673 struct tspp_iv iv;
2674 struct tspp_system_keys sk;
2675 struct tspp_buffer b;
Joel Nider5556a852011-10-16 10:52:13 +02002676 channel = filp->private_data;
Joel Nider435ad8e2011-12-14 16:53:30 +02002677 dev = channel->pdev->pdev->id;
Joel Nider5556a852011-10-16 10:52:13 +02002678
Liron Kucha7b49ae2013-02-14 16:26:38 +02002679 if ((param0 != TSPP_IOCTL_CLOSE_STREAM) && !param1)
Joel Nider5556a852011-10-16 10:52:13 +02002680 return -EINVAL;
2681
2682 switch (param0) {
2683 case TSPP_IOCTL_SELECT_SOURCE:
Joel Nider435ad8e2011-12-14 16:53:30 +02002684 if (!access_ok(VERIFY_READ, param1,
2685 sizeof(struct tspp_select_source))) {
2686 return -EBUSY;
2687 }
2688 if (__copy_from_user(&ss, (void *)param1,
2689 sizeof(struct tspp_select_source)) == 0)
2690 rc = tspp_select_source(dev, channel->id, &ss);
Joel Nider5556a852011-10-16 10:52:13 +02002691 break;
2692 case TSPP_IOCTL_ADD_FILTER:
Joel Nider435ad8e2011-12-14 16:53:30 +02002693 if (!access_ok(VERIFY_READ, param1,
2694 sizeof(struct tspp_filter))) {
2695 return -ENOSR;
2696 }
2697 if (__copy_from_user(&f, (void *)param1,
2698 sizeof(struct tspp_filter)) == 0)
2699 rc = tspp_add_filter(dev, channel->id, &f);
Joel Nider5556a852011-10-16 10:52:13 +02002700 break;
2701 case TSPP_IOCTL_REMOVE_FILTER:
Joel Nider435ad8e2011-12-14 16:53:30 +02002702 if (!access_ok(VERIFY_READ, param1,
2703 sizeof(struct tspp_filter))) {
2704 return -EBUSY;
2705 }
2706 if (__copy_from_user(&f, (void *)param1,
2707 sizeof(struct tspp_filter)) == 0)
2708 rc = tspp_remove_filter(dev, channel->id, &f);
Joel Nider5556a852011-10-16 10:52:13 +02002709 break;
2710 case TSPP_IOCTL_SET_KEY:
Joel Nider435ad8e2011-12-14 16:53:30 +02002711 if (!access_ok(VERIFY_READ, param1,
2712 sizeof(struct tspp_key))) {
2713 return -EBUSY;
2714 }
2715 if (__copy_from_user(&k, (void *)param1,
2716 sizeof(struct tspp_key)) == 0)
2717 rc = tspp_set_key(dev, channel->id, &k);
Joel Nider5556a852011-10-16 10:52:13 +02002718 break;
2719 case TSPP_IOCTL_SET_IV:
Joel Nider435ad8e2011-12-14 16:53:30 +02002720 if (!access_ok(VERIFY_READ, param1,
2721 sizeof(struct tspp_iv))) {
2722 return -EBUSY;
2723 }
2724 if (__copy_from_user(&iv, (void *)param1,
2725 sizeof(struct tspp_iv)) == 0)
2726 rc = tspp_set_iv(channel, &iv);
Joel Nider5556a852011-10-16 10:52:13 +02002727 break;
2728 case TSPP_IOCTL_SET_SYSTEM_KEYS:
Joel Nider435ad8e2011-12-14 16:53:30 +02002729 if (!access_ok(VERIFY_READ, param1,
2730 sizeof(struct tspp_system_keys))) {
2731 return -EINVAL;
2732 }
2733 if (__copy_from_user(&sk, (void *)param1,
2734 sizeof(struct tspp_system_keys)) == 0)
2735 rc = tspp_set_system_keys(channel, &sk);
Joel Nider5556a852011-10-16 10:52:13 +02002736 break;
2737 case TSPP_IOCTL_BUFFER_SIZE:
Joel Nider435ad8e2011-12-14 16:53:30 +02002738 if (!access_ok(VERIFY_READ, param1,
2739 sizeof(struct tspp_buffer))) {
2740 rc = -EINVAL;
2741 }
2742 if (__copy_from_user(&b, (void *)param1,
2743 sizeof(struct tspp_buffer)) == 0)
2744 rc = tspp_set_buffer_size(channel, &b);
Joel Nider5556a852011-10-16 10:52:13 +02002745 break;
Liron Kucha7b49ae2013-02-14 16:26:38 +02002746 case TSPP_IOCTL_CLOSE_STREAM:
2747 rc = tspp_close_stream(dev, channel->id);
2748 break;
Joel Nider5556a852011-10-16 10:52:13 +02002749 default:
2750 pr_err("tspp: Unknown ioctl %i", param0);
2751 }
2752
Liron Kuch72b78552012-10-30 17:47:50 +02002753 /*
2754 * normalize the return code in case one of the subfunctions does
2755 * something weird
2756 */
Joel Nider5556a852011-10-16 10:52:13 +02002757 if (rc != 0)
Joel Nider435ad8e2011-12-14 16:53:30 +02002758 rc = -ENOIOCTLCMD;
Joel Nider5556a852011-10-16 10:52:13 +02002759
2760 return rc;
2761}
2762
2763/*** debugfs ***/
Joel Nider5556a852011-10-16 10:52:13 +02002764static int debugfs_iomem_x32_set(void *data, u64 val)
2765{
2766 writel_relaxed(val, data);
2767 wmb();
2768 return 0;
2769}
2770
2771static int debugfs_iomem_x32_get(void *data, u64 *val)
2772{
2773 *val = readl_relaxed(data);
2774 return 0;
2775}
2776
2777DEFINE_SIMPLE_ATTRIBUTE(fops_iomem_x32, debugfs_iomem_x32_get,
2778 debugfs_iomem_x32_set, "0x%08llx");
2779
2780static void tsif_debugfs_init(struct tspp_tsif_device *tsif_device,
2781 int instance)
2782{
2783 char name[10];
2784 snprintf(name, 10, "tsif%i", instance);
2785 tsif_device->dent_tsif = debugfs_create_dir(
2786 name, NULL);
2787 if (tsif_device->dent_tsif) {
2788 int i;
2789 void __iomem *base = tsif_device->base;
2790 for (i = 0; i < ARRAY_SIZE(debugfs_tsif_regs); i++) {
2791 tsif_device->debugfs_tsif_regs[i] =
2792 debugfs_create_file(
2793 debugfs_tsif_regs[i].name,
2794 debugfs_tsif_regs[i].mode,
2795 tsif_device->dent_tsif,
2796 base + debugfs_tsif_regs[i].offset,
2797 &fops_iomem_x32);
2798 }
Hamad Kadmany44307d32012-11-25 09:49:51 +02002799
2800 debugfs_create_u32(
2801 "stat_rx_chunks",
Hamad Kadmanya1dde822013-03-28 08:23:26 +02002802 S_IRUGO | S_IWUSR | S_IWGRP,
Hamad Kadmany44307d32012-11-25 09:49:51 +02002803 tsif_device->dent_tsif,
2804 &tsif_device->stat_rx);
2805
2806 debugfs_create_u32(
2807 "stat_overflow",
Hamad Kadmanya1dde822013-03-28 08:23:26 +02002808 S_IRUGO | S_IWUSR | S_IWGRP,
Hamad Kadmany44307d32012-11-25 09:49:51 +02002809 tsif_device->dent_tsif,
2810 &tsif_device->stat_overflow);
2811
2812 debugfs_create_u32(
2813 "stat_lost_sync",
Hamad Kadmanya1dde822013-03-28 08:23:26 +02002814 S_IRUGO | S_IWUSR | S_IWGRP,
Hamad Kadmany44307d32012-11-25 09:49:51 +02002815 tsif_device->dent_tsif,
2816 &tsif_device->stat_lost_sync);
2817
2818 debugfs_create_u32(
2819 "stat_timeout",
Hamad Kadmanya1dde822013-03-28 08:23:26 +02002820 S_IRUGO | S_IWUSR | S_IWGRP,
Hamad Kadmany44307d32012-11-25 09:49:51 +02002821 tsif_device->dent_tsif,
2822 &tsif_device->stat_timeout);
Joel Nider5556a852011-10-16 10:52:13 +02002823 }
2824}
2825
2826static void tsif_debugfs_exit(struct tspp_tsif_device *tsif_device)
2827{
2828 if (tsif_device->dent_tsif) {
2829 int i;
2830 debugfs_remove_recursive(tsif_device->dent_tsif);
2831 tsif_device->dent_tsif = NULL;
2832 for (i = 0; i < ARRAY_SIZE(debugfs_tsif_regs); i++)
2833 tsif_device->debugfs_tsif_regs[i] = NULL;
2834 }
2835}
2836
2837static void tspp_debugfs_init(struct tspp_device *device, int instance)
2838{
2839 char name[10];
2840 snprintf(name, 10, "tspp%i", instance);
2841 device->dent = debugfs_create_dir(
2842 name, NULL);
2843 if (device->dent) {
2844 int i;
2845 void __iomem *base = device->base;
2846 for (i = 0; i < ARRAY_SIZE(debugfs_tspp_regs); i++) {
2847 device->debugfs_regs[i] =
2848 debugfs_create_file(
2849 debugfs_tspp_regs[i].name,
2850 debugfs_tspp_regs[i].mode,
2851 device->dent,
2852 base + debugfs_tspp_regs[i].offset,
2853 &fops_iomem_x32);
2854 }
2855 }
2856}
2857
2858static void tspp_debugfs_exit(struct tspp_device *device)
2859{
2860 if (device->dent) {
2861 int i;
2862 debugfs_remove_recursive(device->dent);
2863 device->dent = NULL;
2864 for (i = 0; i < ARRAY_SIZE(debugfs_tspp_regs); i++)
2865 device->debugfs_regs[i] = NULL;
2866 }
2867}
Joel Nider5556a852011-10-16 10:52:13 +02002868
Liron Kuch59339922013-01-01 18:29:47 +02002869/* copy device-tree data to platfrom data struct */
2870static __devinit struct msm_tspp_platform_data *
2871msm_tspp_dt_to_pdata(struct platform_device *pdev)
2872{
2873 struct device_node *node = pdev->dev.of_node;
2874 struct msm_tspp_platform_data *data;
2875 struct msm_gpio *gpios;
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -07002876 struct property *prop;
Liron Kuch59339922013-01-01 18:29:47 +02002877 int i, rc;
2878 int gpio;
2879 u32 gpio_func;
2880
2881 /* Note: memory allocated by devm_kzalloc is freed automatically */
2882 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
2883 if (!data) {
2884 pr_err("tspp: Unable to allocate platform data\n");
2885 return NULL;
2886 }
2887 rc = of_property_read_string(node, "qcom,tsif-pclk", &data->tsif_pclk);
2888 if (rc) {
2889 pr_err("tspp: Could not find tsif-pclk property, err = %d\n",
2890 rc);
2891 return NULL;
2892 }
2893 rc = of_property_read_string(node, "qcom,tsif-ref-clk",
2894 &data->tsif_ref_clk);
2895 if (rc) {
2896 pr_err("tspp: Could not find tsif-ref-clk property, err = %d\n",
2897 rc);
2898 return NULL;
2899 }
2900
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -07002901 data->tsif_vreg_present = 0;
2902 prop = of_find_property(node, "vdd_cx-supply", NULL);
2903 if (prop)
2904 data->tsif_vreg_present = 1;
2905
Liron Kuch59339922013-01-01 18:29:47 +02002906 data->num_gpios = of_gpio_count(node);
2907 if (data->num_gpios == 0) {
2908 pr_err("tspp: Could not find GPIO definitions\n");
2909 return NULL;
2910 }
2911 gpios = devm_kzalloc(&pdev->dev,
2912 (data->num_gpios * sizeof(struct msm_gpio)),
2913 GFP_KERNEL);
2914 if (!gpios) {
2915 pr_err("tspp: Unable to allocate memory for GPIOs table\n");
2916 return NULL;
2917 }
2918 /* Assuming GPIO FUNC property is the same for all GPIOs */
2919 if (of_property_read_u32(node, "qcom,gpios-func", &gpio_func)) {
2920 pr_err("tspp: Could not find gpios-func property\n");
2921 return NULL;
2922 }
2923 for (i = 0; i < data->num_gpios; i++) {
2924 gpio = of_get_gpio(node, i);
2925 gpios[i].gpio_cfg = GPIO_CFG(gpio, gpio_func,
2926 GPIO_CFG_INPUT,
2927 GPIO_CFG_PULL_DOWN,
2928 GPIO_CFG_2MA);
2929 rc = of_property_read_string_index(node, "qcom,gpio-names",
2930 i, &gpios[i].label);
2931 if (rc)
2932 pr_warn("tspp: Could not find gpio-names property\n");
2933 }
2934
2935 data->gpios = gpios;
2936
2937 return data;
2938}
2939
2940static int msm_tspp_map_irqs(struct platform_device *pdev,
2941 struct tspp_device *device)
2942{
2943 int rc;
Liron Kuch59339922013-01-01 18:29:47 +02002944
2945 /* get IRQ numbers from platform information */
2946
2947 /* map TSPP IRQ */
2948 rc = platform_get_irq_byname(pdev, "TSIF_TSPP_IRQ");
2949 if (rc > 0) {
2950 device->tspp_irq = rc;
Liron Kuch59339922013-01-01 18:29:47 +02002951 } else {
2952 dev_err(&pdev->dev, "failed to get TSPP IRQ");
2953 return -EINVAL;
2954 }
2955
2956 /* map TSIF IRQs */
2957 rc = platform_get_irq_byname(pdev, "TSIF0_IRQ");
2958 if (rc > 0) {
2959 device->tsif[0].tsif_irq = rc;
2960 } else {
2961 dev_err(&pdev->dev, "failed to get TSIF0 IRQ");
2962 return -EINVAL;
2963 }
2964
2965 rc = platform_get_irq_byname(pdev, "TSIF1_IRQ");
2966 if (rc > 0) {
2967 device->tsif[1].tsif_irq = rc;
2968 } else {
2969 dev_err(&pdev->dev, "failed to get TSIF1 IRQ");
2970 return -EINVAL;
2971 }
2972
Liron Kuch59339922013-01-01 18:29:47 +02002973 /* map BAM IRQ */
2974 rc = platform_get_irq_byname(pdev, "TSIF_BAM_IRQ");
2975 if (rc > 0) {
2976 device->bam_irq = rc;
2977 } else {
2978 dev_err(&pdev->dev, "failed to get TSPP BAM IRQ");
2979 return -EINVAL;
2980 }
2981
2982 return 0;
2983}
2984
Joel Nider5556a852011-10-16 10:52:13 +02002985static int __devinit msm_tspp_probe(struct platform_device *pdev)
2986{
2987 int rc = -ENODEV;
2988 u32 version;
Liron Kuch72b78552012-10-30 17:47:50 +02002989 u32 i, j;
Joel Nider5556a852011-10-16 10:52:13 +02002990 struct msm_tspp_platform_data *data;
2991 struct tspp_device *device;
2992 struct resource *mem_tsif0;
2993 struct resource *mem_tsif1;
2994 struct resource *mem_tspp;
2995 struct resource *mem_bam;
Liron Kuch72b78552012-10-30 17:47:50 +02002996 struct tspp_channel *channel;
Hamad Kadmanyef5ce992013-06-03 09:30:50 +03002997 struct msm_bus_scale_pdata *tspp_bus_pdata = NULL;
Joel Nider5556a852011-10-16 10:52:13 +02002998
Liron Kuch59339922013-01-01 18:29:47 +02002999 if (pdev->dev.of_node) {
3000 /* get information from device tree */
3001 data = msm_tspp_dt_to_pdata(pdev);
3002 /* get device ID */
3003 rc = of_property_read_u32(pdev->dev.of_node,
3004 "cell-index", &pdev->id);
3005 if (rc)
3006 pdev->id = -1;
3007
3008 pdev->dev.platform_data = data;
Hamad Kadmanyef5ce992013-06-03 09:30:50 +03003009
3010 tspp_bus_pdata = msm_bus_cl_get_pdata(pdev);
Liron Kuch59339922013-01-01 18:29:47 +02003011 } else {
3012 /* must have platform data */
3013 data = pdev->dev.platform_data;
Hamad Kadmanyef5ce992013-06-03 09:30:50 +03003014 tspp_bus_pdata = NULL;
Liron Kuch59339922013-01-01 18:29:47 +02003015 }
Joel Nider5556a852011-10-16 10:52:13 +02003016 if (!data) {
3017 pr_err("tspp: Platform data not available");
3018 rc = -EINVAL;
3019 goto out;
3020 }
3021
3022 /* check for valid device id */
Joel Nider435ad8e2011-12-14 16:53:30 +02003023 if ((pdev->id < 0) || (pdev->id >= TSPP_MAX_DEVICES)) {
Joel Nider5556a852011-10-16 10:52:13 +02003024 pr_err("tspp: Invalid device ID %d", pdev->id);
3025 rc = -EINVAL;
3026 goto out;
3027 }
3028
3029 /* OK, we will use this device */
3030 device = kzalloc(sizeof(struct tspp_device), GFP_KERNEL);
3031 if (!device) {
3032 pr_err("tspp: Failed to allocate memory for device");
3033 rc = -ENOMEM;
3034 goto out;
3035 }
3036
3037 /* set up references */
3038 device->pdev = pdev;
3039 platform_set_drvdata(pdev, device);
3040
Hamad Kadmanyef5ce992013-06-03 09:30:50 +03003041 /* register bus client */
3042 if (tspp_bus_pdata) {
3043 device->tsif_bus_client =
3044 msm_bus_scale_register_client(tspp_bus_pdata);
3045 if (!device->tsif_bus_client)
3046 pr_err("tspp: Unable to register bus client\n");
3047 } else {
3048 device->tsif_bus_client = 0;
3049 }
3050
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -07003051 /* map regulators */
3052 if (data->tsif_vreg_present) {
3053 device->tsif_vreg = devm_regulator_get(&pdev->dev, "vdd_cx");
3054 if (IS_ERR(device->tsif_vreg)) {
3055 rc = PTR_ERR(device->tsif_vreg);
3056 device->tsif_vreg = NULL;
3057 goto err_regultaor;
3058 }
3059
3060 /* Set an initial voltage and enable the regulator */
3061 rc = regulator_set_voltage(device->tsif_vreg,
Hamad Kadmanyfde29382013-06-26 08:03:51 +03003062 RPM_REGULATOR_CORNER_NONE,
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -07003063 RPM_REGULATOR_CORNER_SUPER_TURBO);
3064 if (rc) {
3065 dev_err(&pdev->dev, "Unable to set CX voltage.\n");
3066 goto err_regultaor;
3067 }
3068
3069 rc = regulator_enable(device->tsif_vreg);
3070 if (rc) {
3071 dev_err(&pdev->dev, "Unable to enable CX regulator.\n");
3072 goto err_regultaor;
3073 }
3074 }
3075
Joel Nider5556a852011-10-16 10:52:13 +02003076 /* map clocks */
3077 if (data->tsif_pclk) {
Joel Niderb9662ca2012-06-10 14:21:11 +03003078 device->tsif_pclk = clk_get(&pdev->dev, data->tsif_pclk);
Joel Nider5556a852011-10-16 10:52:13 +02003079 if (IS_ERR(device->tsif_pclk)) {
Joel Nider5556a852011-10-16 10:52:13 +02003080 rc = PTR_ERR(device->tsif_pclk);
3081 device->tsif_pclk = NULL;
3082 goto err_pclock;
3083 }
3084 }
3085 if (data->tsif_ref_clk) {
Joel Niderb9662ca2012-06-10 14:21:11 +03003086 device->tsif_ref_clk = clk_get(&pdev->dev, data->tsif_ref_clk);
Joel Nider5556a852011-10-16 10:52:13 +02003087 if (IS_ERR(device->tsif_ref_clk)) {
Joel Nider5556a852011-10-16 10:52:13 +02003088 rc = PTR_ERR(device->tsif_ref_clk);
3089 device->tsif_ref_clk = NULL;
3090 goto err_refclock;
3091 }
3092 }
3093
3094 /* map I/O memory */
Liron Kuch59339922013-01-01 18:29:47 +02003095 mem_tsif0 = platform_get_resource_byname(pdev,
3096 IORESOURCE_MEM, "MSM_TSIF0_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02003097 if (!mem_tsif0) {
3098 pr_err("tspp: Missing tsif0 MEM resource");
3099 rc = -ENXIO;
3100 goto err_res_tsif0;
3101 }
3102 device->tsif[0].base = ioremap(mem_tsif0->start,
3103 resource_size(mem_tsif0));
3104 if (!device->tsif[0].base) {
3105 pr_err("tspp: ioremap failed");
3106 goto err_map_tsif0;
3107 }
3108
Liron Kuch59339922013-01-01 18:29:47 +02003109 mem_tsif1 = platform_get_resource_byname(pdev,
3110 IORESOURCE_MEM, "MSM_TSIF1_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02003111 if (!mem_tsif1) {
3112 dev_err(&pdev->dev, "Missing tsif1 MEM resource");
3113 rc = -ENXIO;
3114 goto err_res_tsif1;
3115 }
3116 device->tsif[1].base = ioremap(mem_tsif1->start,
3117 resource_size(mem_tsif1));
3118 if (!device->tsif[1].base) {
3119 dev_err(&pdev->dev, "ioremap failed");
3120 goto err_map_tsif1;
3121 }
3122
Liron Kuch59339922013-01-01 18:29:47 +02003123 mem_tspp = platform_get_resource_byname(pdev,
3124 IORESOURCE_MEM, "MSM_TSPP_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02003125 if (!mem_tspp) {
3126 dev_err(&pdev->dev, "Missing MEM resource");
3127 rc = -ENXIO;
3128 goto err_res_dev;
3129 }
3130 device->base = ioremap(mem_tspp->start, resource_size(mem_tspp));
3131 if (!device->base) {
3132 dev_err(&pdev->dev, "ioremap failed");
3133 goto err_map_dev;
3134 }
3135
Liron Kuch59339922013-01-01 18:29:47 +02003136 mem_bam = platform_get_resource_byname(pdev,
3137 IORESOURCE_MEM, "MSM_TSPP_BAM_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02003138 if (!mem_bam) {
3139 pr_err("tspp: Missing bam MEM resource");
3140 rc = -ENXIO;
3141 goto err_res_bam;
3142 }
3143 memset(&device->bam_props, 0, sizeof(device->bam_props));
3144 device->bam_props.phys_addr = mem_bam->start;
3145 device->bam_props.virt_addr = ioremap(mem_bam->start,
3146 resource_size(mem_bam));
3147 if (!device->bam_props.virt_addr) {
3148 dev_err(&pdev->dev, "ioremap failed");
3149 goto err_map_bam;
3150 }
3151
Liron Kuch59339922013-01-01 18:29:47 +02003152 if (msm_tspp_map_irqs(pdev, device))
Joel Nider5556a852011-10-16 10:52:13 +02003153 goto err_irq;
Gilad Bronerf0f326c2014-06-23 17:56:17 +03003154 device->req_irqs = false;
Joel Nider5556a852011-10-16 10:52:13 +02003155
Joel Nider5556a852011-10-16 10:52:13 +02003156 /* power management */
3157 pm_runtime_set_active(&pdev->dev);
3158 pm_runtime_enable(&pdev->dev);
3159
Joel Nider5556a852011-10-16 10:52:13 +02003160 tspp_debugfs_init(device, 0);
3161
3162 for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
3163 tsif_debugfs_init(&device->tsif[i], i);
Joel Nider5556a852011-10-16 10:52:13 +02003164
3165 wake_lock_init(&device->wake_lock, WAKE_LOCK_SUSPEND,
3166 dev_name(&pdev->dev));
3167
3168 /* set up pointers to ram-based 'registers' */
Joel Nider435ad8e2011-12-14 16:53:30 +02003169 device->filters[0] = device->base + TSPP_PID_FILTER_TABLE0;
3170 device->filters[1] = device->base + TSPP_PID_FILTER_TABLE1;
3171 device->filters[2] = device->base + TSPP_PID_FILTER_TABLE2;
3172 device->tspp_key_table = device->base + TSPP_DATA_KEY;
3173 device->tspp_global_performance =
3174 device->base + TSPP_GLOBAL_PERFORMANCE;
3175 device->tspp_pipe_context =
3176 device->base + TSPP_PIPE_CONTEXT;
3177 device->tspp_pipe_performance =
3178 device->base + TSPP_PIPE_PERFORMANCE;
Joel Nider5556a852011-10-16 10:52:13 +02003179
3180 device->bam_props.summing_threshold = 0x10;
3181 device->bam_props.irq = device->bam_irq;
3182 device->bam_props.manage = SPS_BAM_MGR_LOCAL;
3183
Liron Kuch59339922013-01-01 18:29:47 +02003184 if (tspp_clock_start(device) != 0) {
3185 dev_err(&pdev->dev, "Can't start clocks");
3186 goto err_clock;
3187 }
3188
Gilad Bronerf0f326c2014-06-23 17:56:17 +03003189 device->bam_handle = SPS_DEV_HANDLE_INVALID;
Joel Nider5556a852011-10-16 10:52:13 +02003190
Joel Nider5556a852011-10-16 10:52:13 +02003191 spin_lock_init(&device->spinlock);
3192 tasklet_init(&device->tlet, tspp_sps_complete_tlet,
3193 (unsigned long)device);
3194
3195 /* initialize everything to a known state */
3196 tspp_global_reset(device);
3197
3198 version = readl_relaxed(device->base + TSPP_VERSION);
Liron Kuch59339922013-01-01 18:29:47 +02003199 /*
3200 * TSPP version can be bits [7:0] or alternatively,
3201 * TSPP major version is bits [31:28].
3202 */
3203 if ((version != 0x1) && (((version >> 28) & 0xF) != 0x1))
Joel Nider5556a852011-10-16 10:52:13 +02003204 pr_warn("tspp: unrecognized hw version=%i", version);
3205
Joel Nider435ad8e2011-12-14 16:53:30 +02003206 /* initialize the channels */
Joel Nider5556a852011-10-16 10:52:13 +02003207 for (i = 0; i < TSPP_NUM_CHANNELS; i++) {
Joel Nider435ad8e2011-12-14 16:53:30 +02003208 if (tspp_channel_init(&(device->channels[i]), device) != 0) {
3209 pr_err("tspp_channel_init failed");
3210 goto err_channel;
3211 }
Joel Nider5556a852011-10-16 10:52:13 +02003212 }
3213
Joel Nider435ad8e2011-12-14 16:53:30 +02003214 /* stop the clocks for power savings */
3215 tspp_clock_stop(device);
3216
3217 /* everything is ok, so add the device to the list */
3218 list_add_tail(&(device->devlist), &tspp_devices);
3219
Joel Nider5556a852011-10-16 10:52:13 +02003220 return 0;
3221
Joel Nider435ad8e2011-12-14 16:53:30 +02003222err_channel:
Liron Kuch59339922013-01-01 18:29:47 +02003223 /* un-initialize channels */
Liron Kuch72b78552012-10-30 17:47:50 +02003224 for (j = 0; j < i; j++) {
3225 channel = &(device->channels[i]);
3226 device_destroy(tspp_class, channel->cdev.dev);
3227 cdev_del(&channel->cdev);
3228 }
Liron Kuch59339922013-01-01 18:29:47 +02003229
Joel Nider5556a852011-10-16 10:52:13 +02003230 sps_deregister_bam_device(device->bam_handle);
Liron Kuch59339922013-01-01 18:29:47 +02003231err_clock:
Joel Nider5556a852011-10-16 10:52:13 +02003232 tspp_debugfs_exit(device);
3233 for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
3234 tsif_debugfs_exit(&device->tsif[i]);
Joel Nider5556a852011-10-16 10:52:13 +02003235err_irq:
Joel Nider5556a852011-10-16 10:52:13 +02003236 iounmap(device->bam_props.virt_addr);
3237err_map_bam:
3238err_res_bam:
3239 iounmap(device->base);
3240err_map_dev:
3241err_res_dev:
3242 iounmap(device->tsif[1].base);
3243err_map_tsif1:
3244err_res_tsif1:
3245 iounmap(device->tsif[0].base);
3246err_map_tsif0:
3247err_res_tsif0:
3248 if (device->tsif_ref_clk)
3249 clk_put(device->tsif_ref_clk);
3250err_refclock:
3251 if (device->tsif_pclk)
3252 clk_put(device->tsif_pclk);
3253err_pclock:
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -07003254 if (device->tsif_vreg)
3255 regulator_disable(device->tsif_vreg);
3256err_regultaor:
Hamad Kadmanyef5ce992013-06-03 09:30:50 +03003257 if (device->tsif_bus_client)
3258 msm_bus_scale_unregister_client(device->tsif_bus_client);
Joel Nider5556a852011-10-16 10:52:13 +02003259 kfree(device);
3260
3261out:
3262 return rc;
3263}
3264
3265static int __devexit msm_tspp_remove(struct platform_device *pdev)
3266{
Joel Nider435ad8e2011-12-14 16:53:30 +02003267 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02003268 u32 i;
Joel Nider5556a852011-10-16 10:52:13 +02003269
3270 struct tspp_device *device = platform_get_drvdata(pdev);
3271
Joel Nider435ad8e2011-12-14 16:53:30 +02003272 /* free the buffers, and delete the channels */
3273 for (i = 0; i < TSPP_NUM_CHANNELS; i++) {
3274 channel = &device->channels[i];
3275 tspp_close_channel(device->pdev->id, i);
3276 device_destroy(tspp_class, channel->cdev.dev);
3277 cdev_del(&channel->cdev);
3278 }
3279
Gilad Bronerf0f326c2014-06-23 17:56:17 +03003280 for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
Joel Nider5556a852011-10-16 10:52:13 +02003281 tsif_debugfs_exit(&device->tsif[i]);
Joel Nider5556a852011-10-16 10:52:13 +02003282
Hamad Kadmanyef5ce992013-06-03 09:30:50 +03003283 if (device->tsif_bus_client)
3284 msm_bus_scale_unregister_client(device->tsif_bus_client);
3285
Joel Nider5556a852011-10-16 10:52:13 +02003286 wake_lock_destroy(&device->wake_lock);
Gilad Bronerf0f326c2014-06-23 17:56:17 +03003287 if (device->req_irqs)
3288 msm_tspp_free_irqs(device);
Joel Nider5556a852011-10-16 10:52:13 +02003289
3290 iounmap(device->bam_props.virt_addr);
3291 iounmap(device->base);
3292 for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
3293 iounmap(device->tsif[i].base);
3294
3295 if (device->tsif_ref_clk)
3296 clk_put(device->tsif_ref_clk);
3297
3298 if (device->tsif_pclk)
3299 clk_put(device->tsif_pclk);
3300
Vikram Mulukutlae9ca54b2013-04-19 11:08:42 -07003301 if (device->tsif_vreg)
3302 regulator_disable(device->tsif_vreg);
3303
Joel Nider5556a852011-10-16 10:52:13 +02003304 pm_runtime_disable(&pdev->dev);
Liron Kuch605cc122013-02-21 14:25:57 +02003305
Joel Nider5556a852011-10-16 10:52:13 +02003306 kfree(device);
3307
3308 return 0;
3309}
3310
3311/*** power management ***/
3312
3313static int tspp_runtime_suspend(struct device *dev)
3314{
3315 dev_dbg(dev, "pm_runtime: suspending...");
3316 return 0;
3317}
3318
3319static int tspp_runtime_resume(struct device *dev)
3320{
3321 dev_dbg(dev, "pm_runtime: resuming...");
3322 return 0;
3323}
3324
3325static const struct dev_pm_ops tspp_dev_pm_ops = {
3326 .runtime_suspend = tspp_runtime_suspend,
3327 .runtime_resume = tspp_runtime_resume,
3328};
3329
Liron Kuch59339922013-01-01 18:29:47 +02003330static struct of_device_id msm_match_table[] = {
3331 {.compatible = "qcom,msm_tspp"},
3332 {}
3333};
3334
Joel Nider5556a852011-10-16 10:52:13 +02003335static struct platform_driver msm_tspp_driver = {
3336 .probe = msm_tspp_probe,
3337 .remove = __exit_p(msm_tspp_remove),
3338 .driver = {
3339 .name = "msm_tspp",
3340 .pm = &tspp_dev_pm_ops,
Liron Kuch59339922013-01-01 18:29:47 +02003341 .of_match_table = msm_match_table,
Joel Nider5556a852011-10-16 10:52:13 +02003342 },
3343};
3344
3345
3346static int __init mod_init(void)
3347{
Joel Nider5556a852011-10-16 10:52:13 +02003348 int rc;
3349
Joel Nider435ad8e2011-12-14 16:53:30 +02003350 /* make the char devs (channels) */
Joel Nider5556a852011-10-16 10:52:13 +02003351 rc = alloc_chrdev_region(&tspp_minor, 0, TSPP_NUM_CHANNELS, "tspp");
3352 if (rc) {
3353 pr_err("tspp: alloc_chrdev_region failed: %d", rc);
3354 goto err_devrgn;
3355 }
3356
3357 tspp_class = class_create(THIS_MODULE, "tspp");
3358 if (IS_ERR(tspp_class)) {
3359 rc = PTR_ERR(tspp_class);
3360 pr_err("tspp: Error creating class: %d", rc);
3361 goto err_class;
3362 }
3363
Joel Nider435ad8e2011-12-14 16:53:30 +02003364 /* register the driver, and check hardware */
3365 rc = platform_driver_register(&msm_tspp_driver);
3366 if (rc) {
3367 pr_err("tspp: platform_driver_register failed: %d", rc);
3368 goto err_register;
Joel Nider5556a852011-10-16 10:52:13 +02003369 }
3370
3371 return 0;
3372
Joel Nider435ad8e2011-12-14 16:53:30 +02003373err_register:
3374 class_destroy(tspp_class);
Joel Nider5556a852011-10-16 10:52:13 +02003375err_class:
3376 unregister_chrdev_region(0, TSPP_NUM_CHANNELS);
3377err_devrgn:
Joel Nider5556a852011-10-16 10:52:13 +02003378 return rc;
3379}
3380
3381static void __exit mod_exit(void)
3382{
Joel Nider435ad8e2011-12-14 16:53:30 +02003383 /* delete low level driver */
3384 platform_driver_unregister(&msm_tspp_driver);
Joel Nider5556a852011-10-16 10:52:13 +02003385
Joel Nider435ad8e2011-12-14 16:53:30 +02003386 /* delete upper layer interface */
Joel Nider5556a852011-10-16 10:52:13 +02003387 class_destroy(tspp_class);
3388 unregister_chrdev_region(0, TSPP_NUM_CHANNELS);
Joel Nider5556a852011-10-16 10:52:13 +02003389}
3390
3391module_init(mod_init);
3392module_exit(mod_exit);
3393
Joel Nider435ad8e2011-12-14 16:53:30 +02003394MODULE_DESCRIPTION("TSPP platform device and char dev");
Joel Nider5556a852011-10-16 10:52:13 +02003395MODULE_LICENSE("GPL v2");