Pratik Patel | 17f3b82 | 2011-11-21 12:41:47 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved. |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 2 | * |
| 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 Patel | cf41862 | 2011-09-22 11:15:11 -0700 | [diff] [blame] | 15 | #include <linux/init.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/device.h> |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/io.h> |
| 20 | #include <linux/err.h> |
| 21 | |
Pratik Patel | b27a935 | 2012-03-17 22:37:21 -0700 | [diff] [blame] | 22 | #include "qdss-priv.h" |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 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 Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 29 | #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 Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 35 | |
| 36 | |
| 37 | #define FUNNEL_LOCK(id) \ |
| 38 | do { \ |
| 39 | mb(); \ |
Pratik Patel | 17f3b82 | 2011-11-21 12:41:47 -0800 | [diff] [blame] | 40 | funnel_writel(funnel, id, 0x0, CS_LAR); \ |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 41 | } while (0) |
| 42 | #define FUNNEL_UNLOCK(id) \ |
| 43 | do { \ |
Pratik Patel | 17f3b82 | 2011-11-21 12:41:47 -0800 | [diff] [blame] | 44 | funnel_writel(funnel, id, CS_UNLOCK_MAGIC, CS_LAR); \ |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 45 | mb(); \ |
| 46 | } while (0) |
| 47 | |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 48 | #define FUNNEL_HOLDTIME_MASK (0xF00) |
| 49 | #define FUNNEL_HOLDTIME_SHFT (0x8) |
| 50 | #define FUNNEL_HOLDTIME (0x7 << FUNNEL_HOLDTIME_SHFT) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 51 | |
| 52 | struct funnel_ctx { |
| 53 | void __iomem *base; |
| 54 | bool enabled; |
Pratik Patel | 783408a | 2012-03-21 10:28:12 -0700 | [diff] [blame] | 55 | struct mutex mutex; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 56 | struct device *dev; |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 57 | struct kobject *kobj; |
| 58 | uint32_t priority; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
Pratik Patel | 783408a | 2012-03-21 10:28:12 -0700 | [diff] [blame] | 61 | static struct funnel_ctx funnel; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 62 | |
| 63 | static void __funnel_enable(uint8_t id, uint32_t port_mask) |
| 64 | { |
| 65 | uint32_t functl; |
| 66 | |
| 67 | FUNNEL_UNLOCK(id); |
| 68 | |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 69 | functl = funnel_readl(funnel, id, FUNNEL_FUNCTL); |
| 70 | functl &= ~FUNNEL_HOLDTIME_MASK; |
| 71 | functl |= FUNNEL_HOLDTIME; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 72 | functl |= port_mask; |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 73 | funnel_writel(funnel, id, functl, FUNNEL_FUNCTL); |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 74 | funnel_writel(funnel, id, funnel.priority, FUNNEL_PRICTL); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 75 | |
| 76 | FUNNEL_LOCK(id); |
| 77 | } |
| 78 | |
| 79 | void funnel_enable(uint8_t id, uint32_t port_mask) |
| 80 | { |
Pratik Patel | 783408a | 2012-03-21 10:28:12 -0700 | [diff] [blame] | 81 | mutex_lock(&funnel.mutex); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 82 | __funnel_enable(id, port_mask); |
| 83 | funnel.enabled = true; |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 84 | dev_info(funnel.dev, "FUNNEL port mask 0x%lx enabled\n", |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 85 | (unsigned long) port_mask); |
Pratik Patel | 783408a | 2012-03-21 10:28:12 -0700 | [diff] [blame] | 86 | mutex_unlock(&funnel.mutex); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | static void __funnel_disable(uint8_t id, uint32_t port_mask) |
| 90 | { |
| 91 | uint32_t functl; |
| 92 | |
| 93 | FUNNEL_UNLOCK(id); |
| 94 | |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 95 | functl = funnel_readl(funnel, id, FUNNEL_FUNCTL); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 96 | functl &= ~port_mask; |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 97 | funnel_writel(funnel, id, functl, FUNNEL_FUNCTL); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 98 | |
| 99 | FUNNEL_LOCK(id); |
| 100 | } |
| 101 | |
| 102 | void funnel_disable(uint8_t id, uint32_t port_mask) |
| 103 | { |
Pratik Patel | 783408a | 2012-03-21 10:28:12 -0700 | [diff] [blame] | 104 | mutex_lock(&funnel.mutex); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 105 | __funnel_disable(id, port_mask); |
| 106 | funnel.enabled = false; |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 107 | dev_info(funnel.dev, "FUNNEL port mask 0x%lx disabled\n", |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 108 | (unsigned long) port_mask); |
Pratik Patel | 783408a | 2012-03-21 10:28:12 -0700 | [diff] [blame] | 109 | mutex_unlock(&funnel.mutex); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 112 | #define FUNNEL_ATTR(__name) \ |
| 113 | static struct kobj_attribute __name##_attr = \ |
| 114 | __ATTR(__name, S_IRUGO | S_IWUSR, __name##_show, __name##_store) |
| 115 | |
| 116 | static ssize_t priority_store(struct kobject *kobj, |
| 117 | struct kobj_attribute *attr, |
| 118 | const char *buf, size_t n) |
| 119 | { |
| 120 | unsigned long val; |
| 121 | |
| 122 | if (sscanf(buf, "%lx", &val) != 1) |
| 123 | return -EINVAL; |
| 124 | |
| 125 | funnel.priority = val; |
| 126 | return n; |
| 127 | } |
| 128 | static ssize_t priority_show(struct kobject *kobj, |
| 129 | struct kobj_attribute *attr, |
| 130 | char *buf) |
| 131 | { |
| 132 | unsigned long val = funnel.priority; |
| 133 | return scnprintf(buf, PAGE_SIZE, "%#lx\n", val); |
| 134 | } |
| 135 | FUNNEL_ATTR(priority); |
| 136 | |
| 137 | static int __init funnel_sysfs_init(void) |
| 138 | { |
| 139 | int ret; |
| 140 | |
| 141 | funnel.kobj = kobject_create_and_add("funnel", qdss_get_modulekobj()); |
| 142 | if (!funnel.kobj) { |
| 143 | dev_err(funnel.dev, "failed to create FUNNEL sysfs kobject\n"); |
| 144 | ret = -ENOMEM; |
| 145 | goto err_create; |
| 146 | } |
| 147 | |
| 148 | ret = sysfs_create_file(funnel.kobj, &priority_attr.attr); |
| 149 | if (ret) { |
| 150 | dev_err(funnel.dev, "failed to create FUNNEL sysfs priority" |
| 151 | " attribute\n"); |
| 152 | goto err_file; |
| 153 | } |
| 154 | |
| 155 | return 0; |
| 156 | err_file: |
| 157 | kobject_put(funnel.kobj); |
| 158 | err_create: |
| 159 | return ret; |
| 160 | } |
| 161 | |
| 162 | static void funnel_sysfs_exit(void) |
| 163 | { |
| 164 | sysfs_remove_file(funnel.kobj, &priority_attr.attr); |
| 165 | kobject_put(funnel.kobj); |
| 166 | } |
| 167 | |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 168 | static int __devinit funnel_probe(struct platform_device *pdev) |
| 169 | { |
| 170 | int ret; |
| 171 | struct resource *res; |
| 172 | |
| 173 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 174 | if (!res) { |
| 175 | ret = -EINVAL; |
| 176 | goto err_res; |
| 177 | } |
| 178 | |
| 179 | funnel.base = ioremap_nocache(res->start, resource_size(res)); |
| 180 | if (!funnel.base) { |
| 181 | ret = -EINVAL; |
| 182 | goto err_ioremap; |
| 183 | } |
| 184 | |
| 185 | funnel.dev = &pdev->dev; |
| 186 | |
Pratik Patel | 783408a | 2012-03-21 10:28:12 -0700 | [diff] [blame] | 187 | mutex_init(&funnel.mutex); |
| 188 | |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 189 | funnel_sysfs_init(); |
| 190 | |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 191 | dev_info(funnel.dev, "FUNNEL initialized\n"); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 192 | return 0; |
| 193 | |
| 194 | err_ioremap: |
| 195 | err_res: |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 196 | dev_err(funnel.dev, "FUNNEL init failed\n"); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 197 | return ret; |
| 198 | } |
| 199 | |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 200 | static int funnel_remove(struct platform_device *pdev) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 201 | { |
| 202 | if (funnel.enabled) |
| 203 | funnel_disable(0x0, 0xFF); |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 204 | funnel_sysfs_exit(); |
Pratik Patel | 783408a | 2012-03-21 10:28:12 -0700 | [diff] [blame] | 205 | mutex_destroy(&funnel.mutex); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 206 | iounmap(funnel.base); |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | static struct platform_driver funnel_driver = { |
| 212 | .probe = funnel_probe, |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 213 | .remove = funnel_remove, |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 214 | .driver = { |
| 215 | .name = "msm_funnel", |
| 216 | }, |
| 217 | }; |
| 218 | |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 219 | int __init funnel_init(void) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 220 | { |
| 221 | return platform_driver_register(&funnel_driver); |
| 222 | } |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 223 | |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 224 | void funnel_exit(void) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 225 | { |
| 226 | platform_driver_unregister(&funnel_driver); |
| 227 | } |