blob: 3eec56038a91377353487b4857c479819aaccf33 [file] [log] [blame]
Pratik Patel17f3b822011-11-21 12:41:47 -08001/* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
Pratik Patel7831c082011-06-08 21:44:37 -07002 *
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
13#include <linux/kernel.h>
14#include <linux/module.h>
Pratik Patelcf418622011-09-22 11:15:11 -070015#include <linux/init.h>
16#include <linux/types.h>
17#include <linux/device.h>
Pratik Patel7831c082011-06-08 21:44:37 -070018#include <linux/platform_device.h>
19#include <linux/io.h>
20#include <linux/err.h>
21
22#include "qdss.h"
23
24#define funnel_writel(funnel, id, val, off) \
25 __raw_writel((val), funnel.base + (SZ_4K * id) + off)
26#define funnel_readl(funnel, id, off) \
27 __raw_readl(funnel.base + (SZ_4K * id) + off)
28
Pratik Patel61de7302012-03-07 12:06:10 -080029#define FUNNEL_FUNCTL (0x000)
30#define FUNNEL_PRICTL (0x004)
31#define FUNNEL_ITATBDATA0 (0xEEC)
32#define FUNNEL_ITATBCTR2 (0xEF0)
33#define FUNNEL_ITATBCTR1 (0xEF4)
34#define FUNNEL_ITATBCTR0 (0xEF8)
Pratik Patel7831c082011-06-08 21:44:37 -070035
36
37#define FUNNEL_LOCK(id) \
38do { \
39 mb(); \
Pratik Patel17f3b822011-11-21 12:41:47 -080040 funnel_writel(funnel, id, 0x0, CS_LAR); \
Pratik Patel7831c082011-06-08 21:44:37 -070041} while (0)
42#define FUNNEL_UNLOCK(id) \
43do { \
Pratik Patel17f3b822011-11-21 12:41:47 -080044 funnel_writel(funnel, id, CS_UNLOCK_MAGIC, CS_LAR); \
Pratik Patel7831c082011-06-08 21:44:37 -070045 mb(); \
46} while (0)
47
Pratik Patel61de7302012-03-07 12:06:10 -080048#define FUNNEL_HOLDTIME_MASK (0xF00)
49#define FUNNEL_HOLDTIME_SHFT (0x8)
50#define FUNNEL_HOLDTIME (0x7 << FUNNEL_HOLDTIME_SHFT)
Pratik Patel7831c082011-06-08 21:44:37 -070051
52struct funnel_ctx {
53 void __iomem *base;
54 bool enabled;
55 struct device *dev;
Pratik Patel6630ebe2012-03-06 16:44:22 -080056 struct kobject *kobj;
57 uint32_t priority;
Pratik Patel7831c082011-06-08 21:44:37 -070058};
59
Pratik Patel6630ebe2012-03-06 16:44:22 -080060static struct funnel_ctx funnel = {
61 .priority = 0xFAC680,
62};
Pratik Patel7831c082011-06-08 21:44:37 -070063
64static void __funnel_enable(uint8_t id, uint32_t port_mask)
65{
66 uint32_t functl;
67
68 FUNNEL_UNLOCK(id);
69
Pratik Patel61de7302012-03-07 12:06:10 -080070 functl = funnel_readl(funnel, id, FUNNEL_FUNCTL);
71 functl &= ~FUNNEL_HOLDTIME_MASK;
72 functl |= FUNNEL_HOLDTIME;
Pratik Patel7831c082011-06-08 21:44:37 -070073 functl |= port_mask;
Pratik Patel61de7302012-03-07 12:06:10 -080074 funnel_writel(funnel, id, functl, FUNNEL_FUNCTL);
Pratik Patel6630ebe2012-03-06 16:44:22 -080075 funnel_writel(funnel, id, funnel.priority, FUNNEL_PRICTL);
Pratik Patel7831c082011-06-08 21:44:37 -070076
77 FUNNEL_LOCK(id);
78}
79
80void funnel_enable(uint8_t id, uint32_t port_mask)
81{
82 __funnel_enable(id, port_mask);
83 funnel.enabled = true;
Pratik Patel61de7302012-03-07 12:06:10 -080084 dev_info(funnel.dev, "FUNNEL port mask 0x%lx enabled\n",
Pratik Patel7831c082011-06-08 21:44:37 -070085 (unsigned long) port_mask);
86}
87
88static void __funnel_disable(uint8_t id, uint32_t port_mask)
89{
90 uint32_t functl;
91
92 FUNNEL_UNLOCK(id);
93
Pratik Patel61de7302012-03-07 12:06:10 -080094 functl = funnel_readl(funnel, id, FUNNEL_FUNCTL);
Pratik Patel7831c082011-06-08 21:44:37 -070095 functl &= ~port_mask;
Pratik Patel61de7302012-03-07 12:06:10 -080096 funnel_writel(funnel, id, functl, FUNNEL_FUNCTL);
Pratik Patel7831c082011-06-08 21:44:37 -070097
98 FUNNEL_LOCK(id);
99}
100
101void funnel_disable(uint8_t id, uint32_t port_mask)
102{
103 __funnel_disable(id, port_mask);
104 funnel.enabled = false;
Pratik Patel61de7302012-03-07 12:06:10 -0800105 dev_info(funnel.dev, "FUNNEL port mask 0x%lx disabled\n",
Pratik Patel7831c082011-06-08 21:44:37 -0700106 (unsigned long) port_mask);
107}
108
Pratik Patel6630ebe2012-03-06 16:44:22 -0800109#define FUNNEL_ATTR(__name) \
110static struct kobj_attribute __name##_attr = \
111 __ATTR(__name, S_IRUGO | S_IWUSR, __name##_show, __name##_store)
112
113static ssize_t priority_store(struct kobject *kobj,
114 struct kobj_attribute *attr,
115 const char *buf, size_t n)
116{
117 unsigned long val;
118
119 if (sscanf(buf, "%lx", &val) != 1)
120 return -EINVAL;
121
122 funnel.priority = val;
123 return n;
124}
125static ssize_t priority_show(struct kobject *kobj,
126 struct kobj_attribute *attr,
127 char *buf)
128{
129 unsigned long val = funnel.priority;
130 return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
131}
132FUNNEL_ATTR(priority);
133
134static int __init funnel_sysfs_init(void)
135{
136 int ret;
137
138 funnel.kobj = kobject_create_and_add("funnel", qdss_get_modulekobj());
139 if (!funnel.kobj) {
140 dev_err(funnel.dev, "failed to create FUNNEL sysfs kobject\n");
141 ret = -ENOMEM;
142 goto err_create;
143 }
144
145 ret = sysfs_create_file(funnel.kobj, &priority_attr.attr);
146 if (ret) {
147 dev_err(funnel.dev, "failed to create FUNNEL sysfs priority"
148 " attribute\n");
149 goto err_file;
150 }
151
152 return 0;
153err_file:
154 kobject_put(funnel.kobj);
155err_create:
156 return ret;
157}
158
159static void funnel_sysfs_exit(void)
160{
161 sysfs_remove_file(funnel.kobj, &priority_attr.attr);
162 kobject_put(funnel.kobj);
163}
164
Pratik Patel7831c082011-06-08 21:44:37 -0700165static int __devinit funnel_probe(struct platform_device *pdev)
166{
167 int ret;
168 struct resource *res;
169
170 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
171 if (!res) {
172 ret = -EINVAL;
173 goto err_res;
174 }
175
176 funnel.base = ioremap_nocache(res->start, resource_size(res));
177 if (!funnel.base) {
178 ret = -EINVAL;
179 goto err_ioremap;
180 }
181
182 funnel.dev = &pdev->dev;
183
Pratik Patel6630ebe2012-03-06 16:44:22 -0800184 funnel_sysfs_init();
185
Pratik Patel61de7302012-03-07 12:06:10 -0800186 dev_info(funnel.dev, "FUNNEL initialized\n");
Pratik Patel7831c082011-06-08 21:44:37 -0700187 return 0;
188
189err_ioremap:
190err_res:
Pratik Patel61de7302012-03-07 12:06:10 -0800191 dev_err(funnel.dev, "FUNNEL init failed\n");
Pratik Patel7831c082011-06-08 21:44:37 -0700192 return ret;
193}
194
Pratik Patel59e29942011-12-27 10:31:33 -0800195static int funnel_remove(struct platform_device *pdev)
Pratik Patel7831c082011-06-08 21:44:37 -0700196{
197 if (funnel.enabled)
198 funnel_disable(0x0, 0xFF);
Pratik Patel6630ebe2012-03-06 16:44:22 -0800199 funnel_sysfs_exit();
Pratik Patel7831c082011-06-08 21:44:37 -0700200 iounmap(funnel.base);
201
202 return 0;
203}
204
205static struct platform_driver funnel_driver = {
206 .probe = funnel_probe,
Pratik Patel59e29942011-12-27 10:31:33 -0800207 .remove = funnel_remove,
Pratik Patel7831c082011-06-08 21:44:37 -0700208 .driver = {
209 .name = "msm_funnel",
210 },
211};
212
Pratik Patel59e29942011-12-27 10:31:33 -0800213int __init funnel_init(void)
Pratik Patel7831c082011-06-08 21:44:37 -0700214{
215 return platform_driver_register(&funnel_driver);
216}
Pratik Patel7831c082011-06-08 21:44:37 -0700217
Pratik Patel59e29942011-12-27 10:31:33 -0800218void funnel_exit(void)
Pratik Patel7831c082011-06-08 21:44:37 -0700219{
220 platform_driver_unregister(&funnel_driver);
221}