Pratik Patel | 7492943 | 2011-12-26 12:03:41 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2012, Code Aurora Forum. All rights reserved. |
| 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> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/io.h> |
| 20 | #include <linux/err.h> |
Pratik Patel | bf3e7744 | 2012-03-18 18:30:43 -0700 | [diff] [blame] | 21 | #include <linux/export.h> |
Pratik Patel | 7492943 | 2011-12-26 12:03:41 -0800 | [diff] [blame] | 22 | #include <mach/rpm.h> |
| 23 | |
| 24 | #include "rpm_resources.h" |
Pratik Patel | b27a935 | 2012-03-17 22:37:21 -0700 | [diff] [blame] | 25 | #include "qdss-priv.h" |
Pratik Patel | 7492943 | 2011-12-26 12:03:41 -0800 | [diff] [blame] | 26 | |
| 27 | enum { |
| 28 | QDSS_CLK_OFF, |
| 29 | QDSS_CLK_ON_DBG, |
| 30 | QDSS_CLK_ON_HSDBG, |
| 31 | }; |
| 32 | |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 33 | struct qdss_ctx { |
| 34 | struct kobject *modulekobj; |
| 35 | uint8_t max_clk; |
Pratik Patel | bf3e7744 | 2012-03-18 18:30:43 -0700 | [diff] [blame] | 36 | uint8_t clk_count; |
| 37 | struct mutex clk_mutex; |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | static struct qdss_ctx qdss; |
| 41 | |
| 42 | |
| 43 | struct kobject *qdss_get_modulekobj(void) |
| 44 | { |
| 45 | return qdss.modulekobj; |
| 46 | } |
Pratik Patel | 7492943 | 2011-12-26 12:03:41 -0800 | [diff] [blame] | 47 | |
Pratik Patel | bf3e7744 | 2012-03-18 18:30:43 -0700 | [diff] [blame] | 48 | /** |
| 49 | * qdss_clk_enable - enable qdss clocks |
| 50 | * |
| 51 | * Enables qdss clocks via RPM if they aren't already enabled, otherwise |
| 52 | * increments the reference count. |
| 53 | * |
| 54 | * CONTEXT: |
| 55 | * Might sleep. Uses a mutex lock. Should be called from a non-atomic context. |
| 56 | * |
| 57 | * RETURNS: |
| 58 | * 0 on success, non-zero on failure |
| 59 | */ |
Pratik Patel | 7492943 | 2011-12-26 12:03:41 -0800 | [diff] [blame] | 60 | int qdss_clk_enable(void) |
| 61 | { |
| 62 | int ret; |
Pratik Patel | 7492943 | 2011-12-26 12:03:41 -0800 | [diff] [blame] | 63 | struct msm_rpm_iv_pair iv; |
Pratik Patel | 7492943 | 2011-12-26 12:03:41 -0800 | [diff] [blame] | 64 | |
Pratik Patel | bf3e7744 | 2012-03-18 18:30:43 -0700 | [diff] [blame] | 65 | mutex_lock(&qdss.clk_mutex); |
| 66 | if (qdss.clk_count == 0) { |
| 67 | iv.id = MSM_RPM_ID_QDSS_CLK; |
| 68 | if (qdss.max_clk) |
| 69 | iv.value = QDSS_CLK_ON_HSDBG; |
| 70 | else |
| 71 | iv.value = QDSS_CLK_ON_DBG; |
| 72 | ret = msm_rpmrs_set(MSM_RPM_CTX_SET_0, &iv, 1); |
| 73 | if (WARN(ret, "qdss clks not enabled (%d)\n", ret)) |
| 74 | goto err_clk; |
| 75 | } |
| 76 | qdss.clk_count++; |
| 77 | mutex_unlock(&qdss.clk_mutex); |
Pratik Patel | 7492943 | 2011-12-26 12:03:41 -0800 | [diff] [blame] | 78 | return 0; |
Pratik Patel | 7492943 | 2011-12-26 12:03:41 -0800 | [diff] [blame] | 79 | err_clk: |
Pratik Patel | bf3e7744 | 2012-03-18 18:30:43 -0700 | [diff] [blame] | 80 | mutex_unlock(&qdss.clk_mutex); |
Pratik Patel | 7492943 | 2011-12-26 12:03:41 -0800 | [diff] [blame] | 81 | return ret; |
| 82 | } |
Pratik Patel | bf3e7744 | 2012-03-18 18:30:43 -0700 | [diff] [blame] | 83 | EXPORT_SYMBOL(qdss_clk_enable); |
Pratik Patel | 7492943 | 2011-12-26 12:03:41 -0800 | [diff] [blame] | 84 | |
Pratik Patel | bf3e7744 | 2012-03-18 18:30:43 -0700 | [diff] [blame] | 85 | /** |
| 86 | * qdss_clk_disable - disable qdss clocks |
| 87 | * |
| 88 | * Disables qdss clocks via RPM if the reference count is one, otherwise |
| 89 | * decrements the reference count. |
| 90 | * |
| 91 | * CONTEXT: |
| 92 | * Might sleep. Uses a mutex lock. Should be called from a non-atomic context. |
| 93 | */ |
Pratik Patel | 7492943 | 2011-12-26 12:03:41 -0800 | [diff] [blame] | 94 | void qdss_clk_disable(void) |
| 95 | { |
| 96 | int ret; |
| 97 | struct msm_rpm_iv_pair iv; |
| 98 | |
Pratik Patel | bf3e7744 | 2012-03-18 18:30:43 -0700 | [diff] [blame] | 99 | mutex_lock(&qdss.clk_mutex); |
| 100 | if (WARN(qdss.clk_count == 0, "qdss clks are unbalanced\n")) |
| 101 | goto out; |
| 102 | if (qdss.clk_count == 1) { |
| 103 | iv.id = MSM_RPM_ID_QDSS_CLK; |
| 104 | iv.value = QDSS_CLK_OFF; |
| 105 | ret = msm_rpmrs_set(MSM_RPM_CTX_SET_0, &iv, 1); |
| 106 | WARN(ret, "qdss clks not disabled (%d)\n", ret); |
| 107 | } |
| 108 | qdss.clk_count--; |
| 109 | out: |
| 110 | mutex_unlock(&qdss.clk_mutex); |
Pratik Patel | 7492943 | 2011-12-26 12:03:41 -0800 | [diff] [blame] | 111 | } |
Pratik Patel | bf3e7744 | 2012-03-18 18:30:43 -0700 | [diff] [blame] | 112 | EXPORT_SYMBOL(qdss_clk_disable); |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 113 | |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 114 | #define QDSS_ATTR(name) \ |
| 115 | static struct kobj_attribute name##_attr = \ |
| 116 | __ATTR(name, S_IRUGO | S_IWUSR, name##_show, name##_store) |
| 117 | |
| 118 | static ssize_t max_clk_store(struct kobject *kobj, |
| 119 | struct kobj_attribute *attr, |
| 120 | const char *buf, size_t n) |
| 121 | { |
| 122 | unsigned long val; |
| 123 | |
| 124 | if (sscanf(buf, "%lx", &val) != 1) |
| 125 | return -EINVAL; |
| 126 | |
| 127 | qdss.max_clk = val; |
| 128 | return n; |
| 129 | } |
| 130 | static ssize_t max_clk_show(struct kobject *kobj, |
| 131 | struct kobj_attribute *attr, |
| 132 | char *buf) |
| 133 | { |
| 134 | unsigned long val = qdss.max_clk; |
| 135 | return scnprintf(buf, PAGE_SIZE, "%#lx\n", val); |
| 136 | } |
| 137 | QDSS_ATTR(max_clk); |
| 138 | |
| 139 | static int __init qdss_sysfs_init(void) |
| 140 | { |
| 141 | int ret; |
| 142 | |
| 143 | qdss.modulekobj = kset_find_obj(module_kset, KBUILD_MODNAME); |
| 144 | if (!qdss.modulekobj) { |
| 145 | pr_err("failed to find QDSS sysfs module kobject\n"); |
| 146 | ret = -ENOENT; |
| 147 | goto err; |
| 148 | } |
| 149 | |
| 150 | ret = sysfs_create_file(qdss.modulekobj, &max_clk_attr.attr); |
| 151 | if (ret) { |
| 152 | pr_err("failed to create QDSS sysfs max_clk attribute\n"); |
| 153 | goto err; |
| 154 | } |
| 155 | |
| 156 | return 0; |
| 157 | err: |
| 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | static void qdss_sysfs_exit(void) |
| 162 | { |
| 163 | sysfs_remove_file(qdss.modulekobj, &max_clk_attr.attr); |
| 164 | } |
| 165 | |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 166 | static int __init qdss_init(void) |
| 167 | { |
| 168 | int ret; |
| 169 | |
Pratik Patel | bf3e7744 | 2012-03-18 18:30:43 -0700 | [diff] [blame] | 170 | mutex_init(&qdss.clk_mutex); |
| 171 | |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 172 | ret = qdss_sysfs_init(); |
| 173 | if (ret) |
| 174 | goto err_sysfs; |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 175 | ret = etb_init(); |
| 176 | if (ret) |
| 177 | goto err_etb; |
| 178 | ret = tpiu_init(); |
| 179 | if (ret) |
| 180 | goto err_tpiu; |
| 181 | ret = funnel_init(); |
| 182 | if (ret) |
| 183 | goto err_funnel; |
Pratik Patel | 492b301 | 2012-03-06 14:22:30 -0800 | [diff] [blame] | 184 | ret = etm_init(); |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 185 | if (ret) |
Pratik Patel | 492b301 | 2012-03-06 14:22:30 -0800 | [diff] [blame] | 186 | goto err_etm; |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 187 | |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 188 | pr_info("QDSS initialized\n"); |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 189 | return 0; |
Pratik Patel | 492b301 | 2012-03-06 14:22:30 -0800 | [diff] [blame] | 190 | err_etm: |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 191 | funnel_exit(); |
| 192 | err_funnel: |
| 193 | tpiu_exit(); |
| 194 | err_tpiu: |
| 195 | etb_exit(); |
| 196 | err_etb: |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 197 | qdss_sysfs_exit(); |
| 198 | err_sysfs: |
Pratik Patel | bf3e7744 | 2012-03-18 18:30:43 -0700 | [diff] [blame] | 199 | mutex_destroy(&qdss.clk_mutex); |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 200 | pr_err("QDSS init failed\n"); |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 201 | return ret; |
| 202 | } |
| 203 | module_init(qdss_init); |
| 204 | |
| 205 | static void __exit qdss_exit(void) |
| 206 | { |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 207 | qdss_sysfs_exit(); |
Pratik Patel | 492b301 | 2012-03-06 14:22:30 -0800 | [diff] [blame] | 208 | etm_exit(); |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 209 | funnel_exit(); |
| 210 | tpiu_exit(); |
| 211 | etb_exit(); |
Pratik Patel | bf3e7744 | 2012-03-18 18:30:43 -0700 | [diff] [blame] | 212 | mutex_destroy(&qdss.clk_mutex); |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 213 | } |
| 214 | module_exit(qdss_exit); |
| 215 | |
| 216 | MODULE_LICENSE("GPL v2"); |
| 217 | MODULE_DESCRIPTION("Qualcomm Debug SubSystem Driver"); |