Siddartha Mohanadoss | 41b4cd9 | 2017-02-21 14:34:23 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2017, The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
Siddartha Mohanadoss | cd8aa0b | 2017-04-06 15:17:46 -0700 | [diff] [blame] | 4 | * it under the terms of the GNU General Public License version 2 and |
Siddartha Mohanadoss | 41b4cd9 | 2017-02-21 14:34:23 -0800 | [diff] [blame] | 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 | |
| 14 | #include <asm/arch_timer.h> |
Ashok Jammigumpula | a1b7ec1 | 2017-11-23 15:37:44 +0530 | [diff] [blame] | 15 | #include <linux/platform_device.h> |
Siddartha Mohanadoss | 41b4cd9 | 2017-02-21 14:34:23 -0800 | [diff] [blame] | 16 | #include "tsens.h" |
Ashok Jammigumpula | a1b7ec1 | 2017-11-23 15:37:44 +0530 | [diff] [blame] | 17 | #include "tsens-mtc.h" |
Siddartha Mohanadoss | 41b4cd9 | 2017-02-21 14:34:23 -0800 | [diff] [blame] | 18 | |
| 19 | /* debug defines */ |
| 20 | #define TSENS_DBG_BUS_ID_0 0 |
| 21 | #define TSENS_DBG_BUS_ID_1 1 |
| 22 | #define TSENS_DBG_BUS_ID_2 2 |
| 23 | #define TSENS_DBG_BUS_ID_15 15 |
| 24 | #define TSENS_DEBUG_LOOP_COUNT_ID_0 2 |
| 25 | #define TSENS_DEBUG_LOOP_COUNT 5 |
| 26 | #define TSENS_DEBUG_STATUS_REG_START 10 |
| 27 | #define TSENS_DEBUG_OFFSET_RANGE 16 |
| 28 | #define TSENS_DEBUG_OFFSET_WORD1 0x4 |
| 29 | #define TSENS_DEBUG_OFFSET_WORD2 0x8 |
| 30 | #define TSENS_DEBUG_OFFSET_WORD3 0xc |
| 31 | #define TSENS_DEBUG_OFFSET_ROW 0x10 |
| 32 | #define TSENS_DEBUG_DECIDEGC -950 |
| 33 | #define TSENS_DEBUG_CYCLE_MS 64 |
| 34 | #define TSENS_DEBUG_POLL_MS 200 |
| 35 | #define TSENS_DEBUG_BUS_ID2_MIN_CYCLE 50 |
| 36 | #define TSENS_DEBUG_BUS_ID2_MAX_CYCLE 51 |
| 37 | #define TSENS_DEBUG_ID_MASK_1_4 0xffffffe1 |
| 38 | #define DEBUG_SIZE 10 |
| 39 | |
Siddartha Mohanadoss | 64a5da3 | 2017-05-09 11:09:51 -0700 | [diff] [blame] | 40 | #define TSENS_DEBUG_CONTROL(n) ((n) + 0x130) |
| 41 | #define TSENS_DEBUG_DATA(n) ((n) + 0x134) |
Siddartha Mohanadoss | 41b4cd9 | 2017-02-21 14:34:23 -0800 | [diff] [blame] | 42 | |
| 43 | struct tsens_dbg_func { |
| 44 | int (*dbg_func)(struct tsens_device *, u32, u32, int *); |
| 45 | }; |
| 46 | |
Ashok Jammigumpula | a1b7ec1 | 2017-11-23 15:37:44 +0530 | [diff] [blame] | 47 | static ssize_t |
| 48 | zonemask_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 49 | { |
| 50 | struct tsens_device *tmdev = NULL; |
| 51 | |
| 52 | tmdev = tsens_controller_is_present(); |
| 53 | if (!tmdev) { |
| 54 | pr_err("No TSENS controller present\n"); |
| 55 | return -EPROBE_DEFER; |
| 56 | } |
| 57 | |
| 58 | return snprintf(buf, PAGE_SIZE, |
| 59 | "Zone =%d th1=%d th2=%d\n", tmdev->mtcsys.zone_mtc, |
| 60 | tmdev->mtcsys.th1, tmdev->mtcsys.th2); |
| 61 | } |
| 62 | |
| 63 | static ssize_t |
| 64 | zonemask_store(struct device *dev, struct device_attribute *attr, |
| 65 | const char *buf, size_t count) |
| 66 | { |
| 67 | int ret; |
| 68 | struct tsens_device *tmdev = NULL; |
| 69 | |
| 70 | tmdev = tsens_controller_is_present(); |
| 71 | if (!tmdev) { |
| 72 | pr_err("No TSENS controller present\n"); |
| 73 | return -EPROBE_DEFER; |
| 74 | } |
| 75 | |
| 76 | ret = sscanf(buf, "%d %d %d", &tmdev->mtcsys.zone_mtc, |
| 77 | &tmdev->mtcsys.th1, &tmdev->mtcsys.th2); |
| 78 | |
| 79 | if (ret != TSENS_ZONEMASK_PARAMS) { |
| 80 | pr_err("Invalid command line arguments\n"); |
| 81 | count = -EINVAL; |
| 82 | } else { |
| 83 | pr_debug("store zone_mtc=%d th1=%d th2=%d\n", |
| 84 | tmdev->mtcsys.zone_mtc, |
| 85 | tmdev->mtcsys.th1, tmdev->mtcsys.th2); |
| 86 | ret = tsens_set_mtc_zone_sw_mask(tmdev->mtcsys.zone_mtc, |
| 87 | tmdev->mtcsys.th1, tmdev->mtcsys.th2); |
| 88 | if (ret < 0) { |
| 89 | pr_err("Invalid command line arguments\n"); |
| 90 | count = -EINVAL; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return count; |
| 95 | } |
| 96 | |
| 97 | static ssize_t |
| 98 | zonelog_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 99 | { |
| 100 | int ret, zlog[TSENS_MTC_ZONE_LOG_SIZE]; |
| 101 | struct tsens_device *tmdev = NULL; |
| 102 | |
| 103 | tmdev = tsens_controller_is_present(); |
| 104 | if (!tmdev) { |
| 105 | pr_err("No TSENS controller present\n"); |
| 106 | return -EPROBE_DEFER; |
| 107 | } |
| 108 | |
| 109 | ret = tsens_get_mtc_zone_log(tmdev->mtcsys.zone_log, zlog); |
| 110 | if (ret < 0) { |
| 111 | pr_err("Invalid command line arguments\n"); |
| 112 | return -EINVAL; |
| 113 | } |
| 114 | |
| 115 | return snprintf(buf, PAGE_SIZE, |
| 116 | "Log[0]=%d\nLog[1]=%d\nLog[2]=%d\nLog[3]=%d\nLog[4]=%d\nLog[5]=%d\n", |
| 117 | zlog[0], zlog[1], zlog[2], zlog[3], zlog[4], zlog[5]); |
| 118 | } |
| 119 | |
| 120 | static ssize_t |
| 121 | zonelog_store(struct device *dev, struct device_attribute *attr, |
| 122 | const char *buf, size_t count) |
| 123 | { |
| 124 | int ret; |
| 125 | struct tsens_device *tmdev = NULL; |
| 126 | |
| 127 | tmdev = tsens_controller_is_present(); |
| 128 | if (!tmdev) { |
| 129 | pr_err("No TSENS controller present\n"); |
| 130 | return -EPROBE_DEFER; |
| 131 | } |
| 132 | |
| 133 | ret = kstrtou32(buf, 0, &tmdev->mtcsys.zone_log); |
| 134 | if (ret < 0) { |
| 135 | pr_err("Invalid command line arguments\n"); |
| 136 | return -EINVAL; |
| 137 | } |
| 138 | |
| 139 | return count; |
| 140 | } |
| 141 | |
| 142 | static ssize_t |
| 143 | zonehist_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 144 | { |
| 145 | int ret, zhist[TSENS_MTC_ZONE_HISTORY_SIZE]; |
| 146 | struct tsens_device *tmdev = NULL; |
| 147 | |
| 148 | tmdev = tsens_controller_is_present(); |
| 149 | if (!tmdev) { |
| 150 | pr_err("No TSENS controller present\n"); |
| 151 | return -EPROBE_DEFER; |
| 152 | } |
| 153 | |
| 154 | ret = tsens_get_mtc_zone_history(tmdev->mtcsys.zone_hist, zhist); |
| 155 | if (ret < 0) { |
| 156 | pr_err("Invalid command line arguments\n"); |
| 157 | return -EINVAL; |
| 158 | } |
| 159 | |
| 160 | return snprintf(buf, PAGE_SIZE, |
| 161 | "Cool = %d\nYellow = %d\nRed = %d\n", |
| 162 | zhist[0], zhist[1], zhist[2]); |
| 163 | } |
| 164 | |
| 165 | static ssize_t |
| 166 | zonehist_store(struct device *dev, struct device_attribute *attr, |
| 167 | const char *buf, size_t count) |
| 168 | { |
| 169 | int ret; |
| 170 | struct tsens_device *tmdev = NULL; |
| 171 | |
| 172 | tmdev = tsens_controller_is_present(); |
| 173 | if (!tmdev) { |
| 174 | pr_err("No TSENS controller present\n"); |
| 175 | return -EPROBE_DEFER; |
| 176 | } |
| 177 | |
| 178 | ret = kstrtou32(buf, 0, &tmdev->mtcsys.zone_hist); |
| 179 | if (ret < 0) { |
| 180 | pr_err("Invalid command line arguments\n"); |
| 181 | return -EINVAL; |
| 182 | } |
| 183 | |
| 184 | return count; |
| 185 | } |
| 186 | |
| 187 | static struct device_attribute tsens_mtc_dev_attr[] = { |
| 188 | __ATTR(zonemask, 0644, zonemask_show, zonemask_store), |
| 189 | __ATTR(zonelog, 0644, zonelog_show, zonelog_store), |
| 190 | __ATTR(zonehist, 0644, zonehist_show, zonehist_store), |
| 191 | }; |
| 192 | |
| 193 | static int tsens_dbg_mtc_data(struct tsens_device *data, |
| 194 | u32 id, u32 dbg_type, int *val) |
| 195 | { |
| 196 | int result = 0, i; |
| 197 | struct tsens_device *tmdev = NULL; |
| 198 | struct device_attribute *attr_ptr = NULL; |
| 199 | |
| 200 | attr_ptr = tsens_mtc_dev_attr; |
| 201 | tmdev = data; |
| 202 | |
| 203 | for (i = 0; i < ARRAY_SIZE(tsens_mtc_dev_attr); i++) { |
| 204 | result = device_create_file(&tmdev->pdev->dev, &attr_ptr[i]); |
| 205 | if (result < 0) |
| 206 | goto error; |
| 207 | } |
| 208 | |
| 209 | return result; |
| 210 | |
| 211 | error: |
| 212 | for (i--; i >= 0; i--) |
| 213 | device_remove_file(&tmdev->pdev->dev, &attr_ptr[i]); |
| 214 | |
| 215 | return result; |
| 216 | } |
| 217 | |
Siddartha Mohanadoss | 41b4cd9 | 2017-02-21 14:34:23 -0800 | [diff] [blame] | 218 | static int tsens_dbg_log_temp_reads(struct tsens_device *data, u32 id, |
| 219 | u32 dbg_type, int *temp) |
| 220 | { |
| 221 | struct tsens_sensor *sensor; |
| 222 | struct tsens_device *tmdev = NULL; |
| 223 | u32 idx = 0; |
| 224 | |
| 225 | if (!data) |
| 226 | return -EINVAL; |
| 227 | |
| 228 | pr_debug("%d %d\n", id, dbg_type); |
| 229 | tmdev = data; |
| 230 | sensor = &tmdev->sensor[id]; |
| 231 | idx = tmdev->tsens_dbg.sensor_dbg_info[sensor->hw_id].idx; |
| 232 | tmdev->tsens_dbg.sensor_dbg_info[sensor->hw_id].temp[idx%10] = *temp; |
| 233 | tmdev->tsens_dbg.sensor_dbg_info[sensor->hw_id].time_stmp[idx%10] = |
| 234 | sched_clock(); |
| 235 | idx++; |
| 236 | tmdev->tsens_dbg.sensor_dbg_info[sensor->hw_id].idx = idx; |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | static int tsens_dbg_log_interrupt_timestamp(struct tsens_device *data, |
| 242 | u32 id, u32 dbg_type, int *val) |
| 243 | { |
| 244 | struct tsens_device *tmdev = NULL; |
| 245 | u32 idx = 0; |
| 246 | |
| 247 | if (!data) |
| 248 | return -EINVAL; |
| 249 | |
| 250 | pr_debug("%d %d\n", id, dbg_type); |
| 251 | tmdev = data; |
| 252 | /* debug */ |
Rama Krishna Phani A | 6ad6633 | 2017-10-25 21:20:04 +0530 | [diff] [blame] | 253 | idx = tmdev->tsens_dbg.irq_idx; |
| 254 | tmdev->tsens_dbg.irq_time_stmp[idx%10] = |
Siddartha Mohanadoss | 41b4cd9 | 2017-02-21 14:34:23 -0800 | [diff] [blame] | 255 | sched_clock(); |
Rama Krishna Phani A | 6ad6633 | 2017-10-25 21:20:04 +0530 | [diff] [blame] | 256 | tmdev->tsens_dbg.irq_idx++; |
Siddartha Mohanadoss | 41b4cd9 | 2017-02-21 14:34:23 -0800 | [diff] [blame] | 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
Siddartha Mohanadoss | 64a5da3 | 2017-05-09 11:09:51 -0700 | [diff] [blame] | 261 | static int tsens_dbg_log_bus_id_data(struct tsens_device *data, |
| 262 | u32 id, u32 dbg_type, int *val) |
| 263 | { |
| 264 | struct tsens_device *tmdev = NULL; |
| 265 | u32 loop = 0, i = 0; |
| 266 | uint32_t r1, r2, r3, r4, offset = 0; |
| 267 | unsigned int debug_dump; |
| 268 | unsigned int debug_id = 0, cntrl_id = 0; |
| 269 | void __iomem *srot_addr; |
| 270 | void __iomem *controller_id_addr; |
| 271 | void __iomem *debug_id_addr; |
| 272 | void __iomem *debug_data_addr; |
| 273 | |
| 274 | if (!data) |
| 275 | return -EINVAL; |
| 276 | |
| 277 | pr_debug("%d %d\n", id, dbg_type); |
| 278 | tmdev = data; |
| 279 | controller_id_addr = TSENS_CONTROLLER_ID(tmdev->tsens_tm_addr); |
| 280 | debug_id_addr = TSENS_DEBUG_CONTROL(tmdev->tsens_tm_addr); |
| 281 | debug_data_addr = TSENS_DEBUG_DATA(tmdev->tsens_tm_addr); |
| 282 | srot_addr = TSENS_CTRL_ADDR(tmdev->tsens_srot_addr); |
| 283 | |
| 284 | cntrl_id = readl_relaxed(controller_id_addr); |
| 285 | pr_err("Controller_id: 0x%x\n", cntrl_id); |
| 286 | |
| 287 | loop = 0; |
| 288 | i = 0; |
| 289 | debug_id = readl_relaxed(debug_id_addr); |
| 290 | writel_relaxed((debug_id | (i << 1) | 1), |
| 291 | TSENS_DEBUG_CONTROL(tmdev->tsens_tm_addr)); |
| 292 | while (loop < TSENS_DEBUG_LOOP_COUNT_ID_0) { |
| 293 | debug_dump = readl_relaxed(debug_data_addr); |
| 294 | r1 = readl_relaxed(debug_data_addr); |
| 295 | r2 = readl_relaxed(debug_data_addr); |
| 296 | r3 = readl_relaxed(debug_data_addr); |
| 297 | r4 = readl_relaxed(debug_data_addr); |
| 298 | pr_err("cntrl:%d, bus-id:%d value:0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", |
| 299 | cntrl_id, i, debug_dump, r1, r2, r3, r4); |
| 300 | loop++; |
| 301 | } |
| 302 | |
| 303 | for (i = TSENS_DBG_BUS_ID_1; i <= TSENS_DBG_BUS_ID_15; i++) { |
| 304 | loop = 0; |
| 305 | debug_id = readl_relaxed(debug_id_addr); |
| 306 | debug_id = debug_id & TSENS_DEBUG_ID_MASK_1_4; |
| 307 | writel_relaxed((debug_id | (i << 1) | 1), |
| 308 | TSENS_DEBUG_CONTROL(tmdev->tsens_tm_addr)); |
| 309 | while (loop < TSENS_DEBUG_LOOP_COUNT) { |
| 310 | debug_dump = readl_relaxed(debug_data_addr); |
| 311 | pr_err("cntrl:%d, bus-id:%d with value: 0x%x\n", |
| 312 | cntrl_id, i, debug_dump); |
| 313 | if (i == TSENS_DBG_BUS_ID_2) |
| 314 | usleep_range( |
| 315 | TSENS_DEBUG_BUS_ID2_MIN_CYCLE, |
| 316 | TSENS_DEBUG_BUS_ID2_MAX_CYCLE); |
| 317 | loop++; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | pr_err("Start of TSENS TM dump\n"); |
| 322 | for (i = 0; i < TSENS_DEBUG_OFFSET_RANGE; i++) { |
| 323 | r1 = readl_relaxed(controller_id_addr + offset); |
| 324 | r2 = readl_relaxed(controller_id_addr + (offset + |
| 325 | TSENS_DEBUG_OFFSET_WORD1)); |
| 326 | r3 = readl_relaxed(controller_id_addr + (offset + |
| 327 | TSENS_DEBUG_OFFSET_WORD2)); |
| 328 | r4 = readl_relaxed(controller_id_addr + (offset + |
| 329 | TSENS_DEBUG_OFFSET_WORD3)); |
| 330 | |
| 331 | pr_err("ctrl:%d:0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n", |
| 332 | cntrl_id, offset, r1, r2, r3, r4); |
| 333 | offset += TSENS_DEBUG_OFFSET_ROW; |
| 334 | } |
| 335 | |
| 336 | offset = 0; |
| 337 | pr_err("Start of TSENS SROT dump\n"); |
| 338 | for (i = 0; i < TSENS_DEBUG_OFFSET_RANGE; i++) { |
| 339 | r1 = readl_relaxed(srot_addr + offset); |
| 340 | r2 = readl_relaxed(srot_addr + (offset + |
| 341 | TSENS_DEBUG_OFFSET_WORD1)); |
| 342 | r3 = readl_relaxed(srot_addr + (offset + |
| 343 | TSENS_DEBUG_OFFSET_WORD2)); |
| 344 | r4 = readl_relaxed(srot_addr + (offset + |
| 345 | TSENS_DEBUG_OFFSET_WORD3)); |
| 346 | |
| 347 | pr_err("ctrl:%d:0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n", |
| 348 | cntrl_id, offset, r1, r2, r3, r4); |
| 349 | offset += TSENS_DEBUG_OFFSET_ROW; |
| 350 | } |
| 351 | |
| 352 | loop = 0; |
| 353 | while (loop < TSENS_DEBUG_LOOP_COUNT) { |
| 354 | offset = TSENS_DEBUG_OFFSET_ROW * |
| 355 | TSENS_DEBUG_STATUS_REG_START; |
| 356 | pr_err("Start of TSENS TM dump %d\n", loop); |
| 357 | /* Limited dump of the registers for the temperature */ |
| 358 | for (i = 0; i < TSENS_DEBUG_LOOP_COUNT; i++) { |
| 359 | r1 = readl_relaxed(controller_id_addr + offset); |
| 360 | r2 = readl_relaxed(controller_id_addr + |
| 361 | (offset + TSENS_DEBUG_OFFSET_WORD1)); |
| 362 | r3 = readl_relaxed(controller_id_addr + |
| 363 | (offset + TSENS_DEBUG_OFFSET_WORD2)); |
| 364 | r4 = readl_relaxed(controller_id_addr + |
| 365 | (offset + TSENS_DEBUG_OFFSET_WORD3)); |
| 366 | |
| 367 | pr_err("ctrl:%d:0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n", |
| 368 | cntrl_id, offset, r1, r2, r3, r4); |
| 369 | offset += TSENS_DEBUG_OFFSET_ROW; |
| 370 | } |
| 371 | loop++; |
| 372 | } |
| 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | |
Siddartha Mohanadoss | 41b4cd9 | 2017-02-21 14:34:23 -0800 | [diff] [blame] | 377 | static struct tsens_dbg_func dbg_arr[] = { |
| 378 | [TSENS_DBG_LOG_TEMP_READS] = {tsens_dbg_log_temp_reads}, |
| 379 | [TSENS_DBG_LOG_INTERRUPT_TIMESTAMP] = { |
| 380 | tsens_dbg_log_interrupt_timestamp}, |
Siddartha Mohanadoss | 64a5da3 | 2017-05-09 11:09:51 -0700 | [diff] [blame] | 381 | [TSENS_DBG_LOG_BUS_ID_DATA] = {tsens_dbg_log_bus_id_data}, |
Ashok Jammigumpula | a1b7ec1 | 2017-11-23 15:37:44 +0530 | [diff] [blame] | 382 | [TSENS_DBG_MTC_DATA] = {tsens_dbg_mtc_data}, |
Siddartha Mohanadoss | 41b4cd9 | 2017-02-21 14:34:23 -0800 | [diff] [blame] | 383 | }; |
| 384 | |
| 385 | int tsens2xxx_dbg(struct tsens_device *data, u32 id, u32 dbg_type, int *val) |
| 386 | { |
| 387 | if (dbg_type >= TSENS_DBG_LOG_MAX) |
| 388 | return -EINVAL; |
| 389 | |
| 390 | dbg_arr[dbg_type].dbg_func(data, id, dbg_type, val); |
| 391 | |
| 392 | return 0; |
| 393 | } |
| 394 | EXPORT_SYMBOL(tsens2xxx_dbg); |