Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) ST-Ericsson SA 2010 |
| 3 | * |
| 4 | * Author: Mattias Wallin <mattias.wallin@stericsson.com> for ST-Ericsson. |
| 5 | * License Terms: GNU General Public License v2 |
| 6 | */ |
carriere etienne | 0fbce76 | 2011-04-08 16:26:36 +0200 | [diff] [blame^] | 7 | /* |
| 8 | * AB8500 register access |
| 9 | * ====================== |
| 10 | * |
| 11 | * read: |
| 12 | * # echo BANK > <debugfs>/ab8500/register-bank |
| 13 | * # echo ADDR > <debugfs>/ab8500/register-address |
| 14 | * # cat <debugfs>/ab8500/register-value |
| 15 | * |
| 16 | * write: |
| 17 | * # echo BANK > <debugfs>/ab8500/register-bank |
| 18 | * # echo ADDR > <debugfs>/ab8500/register-address |
| 19 | * # echo VALUE > <debugfs>/ab8500/register-value |
| 20 | * |
| 21 | * read all registers from a bank: |
| 22 | * # echo BANK > <debugfs>/ab8500/register-bank |
| 23 | * # cat <debugfs>/ab8500/all-bank-register |
| 24 | * |
| 25 | * BANK target AB8500 register bank |
| 26 | * ADDR target AB8500 register address |
| 27 | * VALUE decimal or 0x-prefixed hexadecimal |
| 28 | * |
| 29 | * |
| 30 | * User Space notification on AB8500 IRQ |
| 31 | * ===================================== |
| 32 | * |
| 33 | * Allows user space entity to be notified when target AB8500 IRQ occurs. |
| 34 | * When subscribed, a sysfs entry is created in ab8500.i2c platform device. |
| 35 | * One can pool this file to get target IRQ occurence information. |
| 36 | * |
| 37 | * subscribe to an AB8500 IRQ: |
| 38 | * # echo IRQ > <debugfs>/ab8500/irq-subscribe |
| 39 | * |
| 40 | * unsubscribe from an AB8500 IRQ: |
| 41 | * # echo IRQ > <debugfs>/ab8500/irq-unsubscribe |
| 42 | * |
| 43 | * |
| 44 | * AB8500 register formated read/write access |
| 45 | * ========================================== |
| 46 | * |
| 47 | * Read: read data, data>>SHIFT, data&=MASK, output data |
| 48 | * [0xABCDEF98] shift=12 mask=0xFFF => 0x00000CDE |
| 49 | * Write: read data, data &= ~(MASK<<SHIFT), data |= (VALUE<<SHIFT), write data |
| 50 | * [0xABCDEF98] shift=12 mask=0xFFF value=0x123 => [0xAB123F98] |
| 51 | * |
| 52 | * Usage: |
| 53 | * # echo "CMD [OPTIONS] BANK ADRESS [VALUE]" > $debugfs/ab8500/hwreg |
| 54 | * |
| 55 | * CMD read read access |
| 56 | * write write access |
| 57 | * |
| 58 | * BANK target reg bank |
| 59 | * ADDRESS target reg address |
| 60 | * VALUE (write) value to be updated |
| 61 | * |
| 62 | * OPTIONS |
| 63 | * -d|-dec (read) output in decimal |
| 64 | * -h|-hexa (read) output in 0x-hexa (default) |
| 65 | * -l|-w|-b 32bit (default), 16bit or 8bit reg access |
| 66 | * -m|-mask MASK 0x-hexa mask (default 0xFFFFFFFF) |
| 67 | * -s|-shift SHIFT bit shift value (read:left, write:right) |
| 68 | * -o|-offset OFFSET address offset to add to ADDRESS value |
| 69 | * |
| 70 | * Warning: bit shift operation is applied to bit-mask. |
| 71 | * Warning: bit shift direction depends on read or right command. |
| 72 | */ |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 73 | |
| 74 | #include <linux/seq_file.h> |
| 75 | #include <linux/uaccess.h> |
| 76 | #include <linux/fs.h> |
Paul Gortmaker | 4e36dd3 | 2011-07-03 15:13:27 -0400 | [diff] [blame] | 77 | #include <linux/module.h> |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 78 | #include <linux/debugfs.h> |
| 79 | #include <linux/platform_device.h> |
Lee Jones | 4b8ac08 | 2013-01-14 16:10:36 +0000 | [diff] [blame] | 80 | #include <linux/interrupt.h> |
| 81 | #include <linux/kobject.h> |
| 82 | #include <linux/slab.h> |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 83 | |
| 84 | #include <linux/mfd/abx500.h> |
Linus Walleij | ee66e65 | 2011-12-02 14:16:33 +0100 | [diff] [blame] | 85 | #include <linux/mfd/abx500/ab8500.h> |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 86 | |
carriere etienne | 0fbce76 | 2011-04-08 16:26:36 +0200 | [diff] [blame^] | 87 | #ifdef CONFIG_DEBUG_FS |
| 88 | #include <linux/string.h> |
| 89 | #include <linux/ctype.h> |
| 90 | #endif |
| 91 | |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 92 | static u32 debug_bank; |
| 93 | static u32 debug_address; |
| 94 | |
Lee Jones | 4b8ac08 | 2013-01-14 16:10:36 +0000 | [diff] [blame] | 95 | static int irq_first; |
| 96 | static int irq_last; |
Mattias Wallin | 0b337e7 | 2010-11-19 17:55:11 +0100 | [diff] [blame] | 97 | static u32 irq_count[AB8500_NR_IRQS]; |
| 98 | |
| 99 | static struct device_attribute *dev_attr[AB8500_NR_IRQS]; |
| 100 | static char *event_name[AB8500_NR_IRQS]; |
Lee Jones | 4b8ac08 | 2013-01-14 16:10:36 +0000 | [diff] [blame] | 101 | |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 102 | /** |
| 103 | * struct ab8500_reg_range |
| 104 | * @first: the first address of the range |
| 105 | * @last: the last address of the range |
| 106 | * @perm: access permissions for the range |
| 107 | */ |
| 108 | struct ab8500_reg_range { |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 109 | u8 first; |
| 110 | u8 last; |
| 111 | u8 perm; |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | /** |
Lee Jones | 822672a | 2012-06-20 13:56:38 +0100 | [diff] [blame] | 115 | * struct ab8500_prcmu_ranges |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 116 | * @num_ranges: the number of ranges in the list |
| 117 | * @bankid: bank identifier |
| 118 | * @range: the list of register ranges |
| 119 | */ |
Lee Jones | 822672a | 2012-06-20 13:56:38 +0100 | [diff] [blame] | 120 | struct ab8500_prcmu_ranges { |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 121 | u8 num_ranges; |
| 122 | u8 bankid; |
| 123 | const struct ab8500_reg_range *range; |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 124 | }; |
| 125 | |
carriere etienne | 0fbce76 | 2011-04-08 16:26:36 +0200 | [diff] [blame^] | 126 | /* hwreg- "mask" and "shift" entries ressources */ |
| 127 | struct hwreg_cfg { |
| 128 | u32 bank; /* target bank */ |
| 129 | u32 addr; /* target address */ |
| 130 | uint fmt; /* format */ |
| 131 | uint mask; /* read/write mask, applied before any bit shift */ |
| 132 | int shift; /* bit shift (read:right shift, write:left shift */ |
| 133 | }; |
| 134 | /* fmt bit #0: 0=hexa, 1=dec */ |
| 135 | #define REG_FMT_DEC(c) ((c)->fmt & 0x1) |
| 136 | #define REG_FMT_HEX(c) (!REG_FMT_DEC(c)) |
| 137 | |
| 138 | static struct hwreg_cfg hwreg_cfg = { |
| 139 | .addr = 0, /* default: invalid phys addr */ |
| 140 | .fmt = 0, /* default: 32bit access, hex output */ |
| 141 | .mask = 0xFFFFFFFF, /* default: no mask */ |
| 142 | .shift = 0, /* default: no bit shift */ |
| 143 | }; |
| 144 | |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 145 | #define AB8500_NAME_STRING "ab8500" |
| 146 | #define AB8500_NUM_BANKS 22 |
| 147 | |
| 148 | #define AB8500_REV_REG 0x80 |
| 149 | |
Lee Jones | 822672a | 2012-06-20 13:56:38 +0100 | [diff] [blame] | 150 | static struct ab8500_prcmu_ranges debug_ranges[AB8500_NUM_BANKS] = { |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 151 | [0x0] = { |
| 152 | .num_ranges = 0, |
Lee Jones | fad55a8 | 2013-01-14 17:17:34 +0000 | [diff] [blame] | 153 | .range = NULL, |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 154 | }, |
| 155 | [AB8500_SYS_CTRL1_BLOCK] = { |
| 156 | .num_ranges = 3, |
| 157 | .range = (struct ab8500_reg_range[]) { |
| 158 | { |
| 159 | .first = 0x00, |
| 160 | .last = 0x02, |
| 161 | }, |
| 162 | { |
| 163 | .first = 0x42, |
| 164 | .last = 0x42, |
| 165 | }, |
| 166 | { |
| 167 | .first = 0x80, |
| 168 | .last = 0x81, |
| 169 | }, |
| 170 | }, |
| 171 | }, |
| 172 | [AB8500_SYS_CTRL2_BLOCK] = { |
| 173 | .num_ranges = 4, |
| 174 | .range = (struct ab8500_reg_range[]) { |
| 175 | { |
| 176 | .first = 0x00, |
| 177 | .last = 0x0D, |
| 178 | }, |
| 179 | { |
| 180 | .first = 0x0F, |
| 181 | .last = 0x17, |
| 182 | }, |
| 183 | { |
| 184 | .first = 0x30, |
| 185 | .last = 0x30, |
| 186 | }, |
| 187 | { |
| 188 | .first = 0x32, |
| 189 | .last = 0x33, |
| 190 | }, |
| 191 | }, |
| 192 | }, |
| 193 | [AB8500_REGU_CTRL1] = { |
| 194 | .num_ranges = 3, |
| 195 | .range = (struct ab8500_reg_range[]) { |
| 196 | { |
| 197 | .first = 0x00, |
| 198 | .last = 0x00, |
| 199 | }, |
| 200 | { |
| 201 | .first = 0x03, |
| 202 | .last = 0x10, |
| 203 | }, |
| 204 | { |
| 205 | .first = 0x80, |
| 206 | .last = 0x84, |
| 207 | }, |
| 208 | }, |
| 209 | }, |
| 210 | [AB8500_REGU_CTRL2] = { |
| 211 | .num_ranges = 5, |
| 212 | .range = (struct ab8500_reg_range[]) { |
| 213 | { |
| 214 | .first = 0x00, |
| 215 | .last = 0x15, |
| 216 | }, |
| 217 | { |
| 218 | .first = 0x17, |
| 219 | .last = 0x19, |
| 220 | }, |
| 221 | { |
| 222 | .first = 0x1B, |
| 223 | .last = 0x1D, |
| 224 | }, |
| 225 | { |
| 226 | .first = 0x1F, |
| 227 | .last = 0x22, |
| 228 | }, |
| 229 | { |
| 230 | .first = 0x40, |
| 231 | .last = 0x44, |
| 232 | }, |
| 233 | /* 0x80-0x8B is SIM registers and should |
| 234 | * not be accessed from here */ |
| 235 | }, |
| 236 | }, |
| 237 | [AB8500_USB] = { |
| 238 | .num_ranges = 2, |
| 239 | .range = (struct ab8500_reg_range[]) { |
| 240 | { |
| 241 | .first = 0x80, |
| 242 | .last = 0x83, |
| 243 | }, |
| 244 | { |
| 245 | .first = 0x87, |
| 246 | .last = 0x8A, |
| 247 | }, |
| 248 | }, |
| 249 | }, |
| 250 | [AB8500_TVOUT] = { |
| 251 | .num_ranges = 9, |
| 252 | .range = (struct ab8500_reg_range[]) { |
| 253 | { |
| 254 | .first = 0x00, |
| 255 | .last = 0x12, |
| 256 | }, |
| 257 | { |
| 258 | .first = 0x15, |
| 259 | .last = 0x17, |
| 260 | }, |
| 261 | { |
| 262 | .first = 0x19, |
| 263 | .last = 0x21, |
| 264 | }, |
| 265 | { |
| 266 | .first = 0x27, |
| 267 | .last = 0x2C, |
| 268 | }, |
| 269 | { |
| 270 | .first = 0x41, |
| 271 | .last = 0x41, |
| 272 | }, |
| 273 | { |
| 274 | .first = 0x45, |
| 275 | .last = 0x5B, |
| 276 | }, |
| 277 | { |
| 278 | .first = 0x5D, |
| 279 | .last = 0x5D, |
| 280 | }, |
| 281 | { |
| 282 | .first = 0x69, |
| 283 | .last = 0x69, |
| 284 | }, |
| 285 | { |
| 286 | .first = 0x80, |
| 287 | .last = 0x81, |
| 288 | }, |
| 289 | }, |
| 290 | }, |
| 291 | [AB8500_DBI] = { |
| 292 | .num_ranges = 0, |
Mark Brown | 87fff23 | 2010-12-13 14:06:47 +0000 | [diff] [blame] | 293 | .range = NULL, |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 294 | }, |
| 295 | [AB8500_ECI_AV_ACC] = { |
| 296 | .num_ranges = 1, |
| 297 | .range = (struct ab8500_reg_range[]) { |
| 298 | { |
| 299 | .first = 0x80, |
| 300 | .last = 0x82, |
| 301 | }, |
| 302 | }, |
| 303 | }, |
| 304 | [0x9] = { |
| 305 | .num_ranges = 0, |
Mark Brown | 87fff23 | 2010-12-13 14:06:47 +0000 | [diff] [blame] | 306 | .range = NULL, |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 307 | }, |
| 308 | [AB8500_GPADC] = { |
| 309 | .num_ranges = 1, |
| 310 | .range = (struct ab8500_reg_range[]) { |
| 311 | { |
| 312 | .first = 0x00, |
| 313 | .last = 0x08, |
| 314 | }, |
| 315 | }, |
| 316 | }, |
| 317 | [AB8500_CHARGER] = { |
| 318 | .num_ranges = 8, |
| 319 | .range = (struct ab8500_reg_range[]) { |
| 320 | { |
| 321 | .first = 0x00, |
| 322 | .last = 0x03, |
| 323 | }, |
| 324 | { |
| 325 | .first = 0x05, |
| 326 | .last = 0x05, |
| 327 | }, |
| 328 | { |
| 329 | .first = 0x40, |
| 330 | .last = 0x40, |
| 331 | }, |
| 332 | { |
| 333 | .first = 0x42, |
| 334 | .last = 0x42, |
| 335 | }, |
| 336 | { |
| 337 | .first = 0x44, |
| 338 | .last = 0x44, |
| 339 | }, |
| 340 | { |
| 341 | .first = 0x50, |
| 342 | .last = 0x55, |
| 343 | }, |
| 344 | { |
| 345 | .first = 0x80, |
| 346 | .last = 0x82, |
| 347 | }, |
| 348 | { |
| 349 | .first = 0xC0, |
| 350 | .last = 0xC2, |
| 351 | }, |
| 352 | }, |
| 353 | }, |
| 354 | [AB8500_GAS_GAUGE] = { |
| 355 | .num_ranges = 3, |
| 356 | .range = (struct ab8500_reg_range[]) { |
| 357 | { |
| 358 | .first = 0x00, |
| 359 | .last = 0x00, |
| 360 | }, |
| 361 | { |
| 362 | .first = 0x07, |
| 363 | .last = 0x0A, |
| 364 | }, |
| 365 | { |
| 366 | .first = 0x10, |
| 367 | .last = 0x14, |
| 368 | }, |
| 369 | }, |
| 370 | }, |
| 371 | [AB8500_AUDIO] = { |
| 372 | .num_ranges = 1, |
| 373 | .range = (struct ab8500_reg_range[]) { |
| 374 | { |
| 375 | .first = 0x00, |
| 376 | .last = 0x6F, |
| 377 | }, |
| 378 | }, |
| 379 | }, |
| 380 | [AB8500_INTERRUPT] = { |
| 381 | .num_ranges = 0, |
Mark Brown | 87fff23 | 2010-12-13 14:06:47 +0000 | [diff] [blame] | 382 | .range = NULL, |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 383 | }, |
| 384 | [AB8500_RTC] = { |
| 385 | .num_ranges = 1, |
| 386 | .range = (struct ab8500_reg_range[]) { |
| 387 | { |
| 388 | .first = 0x00, |
| 389 | .last = 0x0F, |
| 390 | }, |
| 391 | }, |
| 392 | }, |
| 393 | [AB8500_MISC] = { |
| 394 | .num_ranges = 8, |
| 395 | .range = (struct ab8500_reg_range[]) { |
| 396 | { |
| 397 | .first = 0x00, |
| 398 | .last = 0x05, |
| 399 | }, |
| 400 | { |
| 401 | .first = 0x10, |
| 402 | .last = 0x15, |
| 403 | }, |
| 404 | { |
| 405 | .first = 0x20, |
| 406 | .last = 0x25, |
| 407 | }, |
| 408 | { |
| 409 | .first = 0x30, |
| 410 | .last = 0x35, |
| 411 | }, |
| 412 | { |
| 413 | .first = 0x40, |
| 414 | .last = 0x45, |
| 415 | }, |
| 416 | { |
| 417 | .first = 0x50, |
| 418 | .last = 0x50, |
| 419 | }, |
| 420 | { |
| 421 | .first = 0x60, |
| 422 | .last = 0x67, |
| 423 | }, |
| 424 | { |
| 425 | .first = 0x80, |
| 426 | .last = 0x80, |
| 427 | }, |
| 428 | }, |
| 429 | }, |
| 430 | [0x11] = { |
| 431 | .num_ranges = 0, |
Mark Brown | 87fff23 | 2010-12-13 14:06:47 +0000 | [diff] [blame] | 432 | .range = NULL, |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 433 | }, |
| 434 | [0x12] = { |
| 435 | .num_ranges = 0, |
Mark Brown | 87fff23 | 2010-12-13 14:06:47 +0000 | [diff] [blame] | 436 | .range = NULL, |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 437 | }, |
| 438 | [0x13] = { |
| 439 | .num_ranges = 0, |
Mark Brown | 87fff23 | 2010-12-13 14:06:47 +0000 | [diff] [blame] | 440 | .range = NULL, |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 441 | }, |
| 442 | [0x14] = { |
| 443 | .num_ranges = 0, |
Mark Brown | 87fff23 | 2010-12-13 14:06:47 +0000 | [diff] [blame] | 444 | .range = NULL, |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 445 | }, |
| 446 | [AB8500_OTP_EMUL] = { |
| 447 | .num_ranges = 1, |
| 448 | .range = (struct ab8500_reg_range[]) { |
| 449 | { |
| 450 | .first = 0x01, |
| 451 | .last = 0x0F, |
| 452 | }, |
| 453 | }, |
| 454 | }, |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 455 | }; |
| 456 | |
Lee Jones | 4b8ac08 | 2013-01-14 16:10:36 +0000 | [diff] [blame] | 457 | static irqreturn_t ab8500_debug_handler(int irq, void *data) |
| 458 | { |
| 459 | char buf[16]; |
| 460 | struct kobject *kobj = (struct kobject *)data; |
Mattias Wallin | 0b337e7 | 2010-11-19 17:55:11 +0100 | [diff] [blame] | 461 | unsigned int irq_abb = irq - irq_first; |
Lee Jones | 4b8ac08 | 2013-01-14 16:10:36 +0000 | [diff] [blame] | 462 | |
Mattias Wallin | 0b337e7 | 2010-11-19 17:55:11 +0100 | [diff] [blame] | 463 | if (irq_abb < AB8500_NR_IRQS) |
| 464 | irq_count[irq_abb]++; |
Lee Jones | 4b8ac08 | 2013-01-14 16:10:36 +0000 | [diff] [blame] | 465 | /* |
| 466 | * This makes it possible to use poll for events (POLLPRI | POLLERR) |
Mattias Wallin | 0b337e7 | 2010-11-19 17:55:11 +0100 | [diff] [blame] | 467 | * from userspace on sysfs file named <irq-nr> |
Lee Jones | 4b8ac08 | 2013-01-14 16:10:36 +0000 | [diff] [blame] | 468 | */ |
Mattias Wallin | 0b337e7 | 2010-11-19 17:55:11 +0100 | [diff] [blame] | 469 | sprintf(buf, "%d", irq); |
Lee Jones | 4b8ac08 | 2013-01-14 16:10:36 +0000 | [diff] [blame] | 470 | sysfs_notify(kobj, NULL, buf); |
| 471 | |
| 472 | return IRQ_HANDLED; |
| 473 | } |
| 474 | |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 475 | static int ab8500_registers_print(struct seq_file *s, void *p) |
| 476 | { |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 477 | struct device *dev = s->private; |
| 478 | unsigned int i; |
| 479 | u32 bank = debug_bank; |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 480 | |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 481 | seq_printf(s, AB8500_NAME_STRING " register values:\n"); |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 482 | |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 483 | seq_printf(s, " bank %u:\n", bank); |
| 484 | for (i = 0; i < debug_ranges[bank].num_ranges; i++) { |
| 485 | u32 reg; |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 486 | |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 487 | for (reg = debug_ranges[bank].range[i].first; |
| 488 | reg <= debug_ranges[bank].range[i].last; |
| 489 | reg++) { |
| 490 | u8 value; |
| 491 | int err; |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 492 | |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 493 | err = abx500_get_register_interruptible(dev, |
| 494 | (u8)bank, (u8)reg, &value); |
| 495 | if (err < 0) { |
| 496 | dev_err(dev, "ab->read fail %d\n", err); |
| 497 | return err; |
| 498 | } |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 499 | |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 500 | err = seq_printf(s, " [%u/0x%02X]: 0x%02X\n", bank, |
| 501 | reg, value); |
| 502 | if (err < 0) { |
| 503 | dev_err(dev, "seq_printf overflow\n"); |
| 504 | /* Error is not returned here since |
| 505 | * the output is wanted in any case */ |
| 506 | return 0; |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | return 0; |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | static int ab8500_registers_open(struct inode *inode, struct file *file) |
| 514 | { |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 515 | return single_open(file, ab8500_registers_print, inode->i_private); |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | static const struct file_operations ab8500_registers_fops = { |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 519 | .open = ab8500_registers_open, |
| 520 | .read = seq_read, |
| 521 | .llseek = seq_lseek, |
| 522 | .release = single_release, |
| 523 | .owner = THIS_MODULE, |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 524 | }; |
| 525 | |
| 526 | static int ab8500_bank_print(struct seq_file *s, void *p) |
| 527 | { |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 528 | return seq_printf(s, "%d\n", debug_bank); |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | static int ab8500_bank_open(struct inode *inode, struct file *file) |
| 532 | { |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 533 | return single_open(file, ab8500_bank_print, inode->i_private); |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | static ssize_t ab8500_bank_write(struct file *file, |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 537 | const char __user *user_buf, |
| 538 | size_t count, loff_t *ppos) |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 539 | { |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 540 | struct device *dev = ((struct seq_file *)(file->private_data))->private; |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 541 | unsigned long user_bank; |
| 542 | int err; |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 543 | |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 544 | /* Get userspace string and assure termination */ |
Peter Huewe | 8504d63 | 2011-06-06 22:43:32 +0200 | [diff] [blame] | 545 | err = kstrtoul_from_user(user_buf, count, 0, &user_bank); |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 546 | if (err) |
Peter Huewe | 8504d63 | 2011-06-06 22:43:32 +0200 | [diff] [blame] | 547 | return err; |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 548 | |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 549 | if (user_bank >= AB8500_NUM_BANKS) { |
| 550 | dev_err(dev, "debugfs error input > number of banks\n"); |
| 551 | return -EINVAL; |
| 552 | } |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 553 | |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 554 | debug_bank = user_bank; |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 555 | |
Peter Huewe | 8504d63 | 2011-06-06 22:43:32 +0200 | [diff] [blame] | 556 | return count; |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | static int ab8500_address_print(struct seq_file *s, void *p) |
| 560 | { |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 561 | return seq_printf(s, "0x%02X\n", debug_address); |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | static int ab8500_address_open(struct inode *inode, struct file *file) |
| 565 | { |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 566 | return single_open(file, ab8500_address_print, inode->i_private); |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | static ssize_t ab8500_address_write(struct file *file, |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 570 | const char __user *user_buf, |
| 571 | size_t count, loff_t *ppos) |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 572 | { |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 573 | struct device *dev = ((struct seq_file *)(file->private_data))->private; |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 574 | unsigned long user_address; |
| 575 | int err; |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 576 | |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 577 | /* Get userspace string and assure termination */ |
Peter Huewe | 8504d63 | 2011-06-06 22:43:32 +0200 | [diff] [blame] | 578 | err = kstrtoul_from_user(user_buf, count, 0, &user_address); |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 579 | if (err) |
Peter Huewe | 8504d63 | 2011-06-06 22:43:32 +0200 | [diff] [blame] | 580 | return err; |
| 581 | |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 582 | if (user_address > 0xff) { |
| 583 | dev_err(dev, "debugfs error input > 0xff\n"); |
| 584 | return -EINVAL; |
| 585 | } |
| 586 | debug_address = user_address; |
Peter Huewe | 8504d63 | 2011-06-06 22:43:32 +0200 | [diff] [blame] | 587 | return count; |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | static int ab8500_val_print(struct seq_file *s, void *p) |
| 591 | { |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 592 | struct device *dev = s->private; |
| 593 | int ret; |
| 594 | u8 regvalue; |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 595 | |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 596 | ret = abx500_get_register_interruptible(dev, |
| 597 | (u8)debug_bank, (u8)debug_address, ®value); |
| 598 | if (ret < 0) { |
| 599 | dev_err(dev, "abx500_get_reg fail %d, %d\n", |
| 600 | ret, __LINE__); |
| 601 | return -EINVAL; |
| 602 | } |
| 603 | seq_printf(s, "0x%02X\n", regvalue); |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 604 | |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 605 | return 0; |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | static int ab8500_val_open(struct inode *inode, struct file *file) |
| 609 | { |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 610 | return single_open(file, ab8500_val_print, inode->i_private); |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | static ssize_t ab8500_val_write(struct file *file, |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 614 | const char __user *user_buf, |
| 615 | size_t count, loff_t *ppos) |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 616 | { |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 617 | struct device *dev = ((struct seq_file *)(file->private_data))->private; |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 618 | unsigned long user_val; |
| 619 | int err; |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 620 | |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 621 | /* Get userspace string and assure termination */ |
Peter Huewe | 8504d63 | 2011-06-06 22:43:32 +0200 | [diff] [blame] | 622 | err = kstrtoul_from_user(user_buf, count, 0, &user_val); |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 623 | if (err) |
Peter Huewe | 8504d63 | 2011-06-06 22:43:32 +0200 | [diff] [blame] | 624 | return err; |
| 625 | |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 626 | if (user_val > 0xff) { |
| 627 | dev_err(dev, "debugfs error input > 0xff\n"); |
| 628 | return -EINVAL; |
| 629 | } |
| 630 | err = abx500_set_register_interruptible(dev, |
| 631 | (u8)debug_bank, debug_address, (u8)user_val); |
| 632 | if (err < 0) { |
| 633 | printk(KERN_ERR "abx500_set_reg failed %d, %d", err, __LINE__); |
| 634 | return -EINVAL; |
| 635 | } |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 636 | |
Peter Huewe | 8504d63 | 2011-06-06 22:43:32 +0200 | [diff] [blame] | 637 | return count; |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 638 | } |
| 639 | |
carriere etienne | 0fbce76 | 2011-04-08 16:26:36 +0200 | [diff] [blame^] | 640 | /* |
| 641 | * - HWREG DB8500 formated routines |
| 642 | */ |
| 643 | static int ab8500_hwreg_print(struct seq_file *s, void *d) |
| 644 | { |
| 645 | struct device *dev = s->private; |
| 646 | int ret; |
| 647 | u8 regvalue; |
| 648 | |
| 649 | ret = abx500_get_register_interruptible(dev, |
| 650 | (u8)hwreg_cfg.bank, (u8)hwreg_cfg.addr, ®value); |
| 651 | if (ret < 0) { |
| 652 | dev_err(dev, "abx500_get_reg fail %d, %d\n", |
| 653 | ret, __LINE__); |
| 654 | return -EINVAL; |
| 655 | } |
| 656 | |
| 657 | if (hwreg_cfg.shift >= 0) |
| 658 | regvalue >>= hwreg_cfg.shift; |
| 659 | else |
| 660 | regvalue <<= -hwreg_cfg.shift; |
| 661 | regvalue &= hwreg_cfg.mask; |
| 662 | |
| 663 | if (REG_FMT_DEC(&hwreg_cfg)) |
| 664 | seq_printf(s, "%d\n", regvalue); |
| 665 | else |
| 666 | seq_printf(s, "0x%02X\n", regvalue); |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | static int ab8500_hwreg_open(struct inode *inode, struct file *file) |
| 671 | { |
| 672 | return single_open(file, ab8500_hwreg_print, inode->i_private); |
| 673 | } |
| 674 | |
| 675 | /* |
| 676 | * return length of an ASCII numerical value, 0 is string is not a |
| 677 | * numerical value. |
| 678 | * string shall start at value 1st char. |
| 679 | * string can be tailed with \0 or space or newline chars only. |
| 680 | * value can be decimal or hexadecimal (prefixed 0x or 0X). |
| 681 | */ |
| 682 | static int strval_len(char *b) |
| 683 | { |
| 684 | char *s = b; |
| 685 | if ((*s == '0') && ((*(s+1) == 'x') || (*(s+1) == 'X'))) { |
| 686 | s += 2; |
| 687 | for (; *s && (*s != ' ') && (*s != '\n'); s++) { |
| 688 | if (!isxdigit(*s)) |
| 689 | return 0; |
| 690 | } |
| 691 | } else { |
| 692 | if (*s == '-') |
| 693 | s++; |
| 694 | for (; *s && (*s != ' ') && (*s != '\n'); s++) { |
| 695 | if (!isdigit(*s)) |
| 696 | return 0; |
| 697 | } |
| 698 | } |
| 699 | return (int) (s-b); |
| 700 | } |
| 701 | |
| 702 | /* |
| 703 | * parse hwreg input data. |
| 704 | * update global hwreg_cfg only if input data syntax is ok. |
| 705 | */ |
| 706 | static ssize_t hwreg_common_write(char *b, struct hwreg_cfg *cfg, |
| 707 | struct device *dev) |
| 708 | { |
| 709 | uint write, val = 0; |
| 710 | u8 regvalue; |
| 711 | int ret; |
| 712 | struct hwreg_cfg loc = { |
| 713 | .bank = 0, /* default: invalid phys addr */ |
| 714 | .addr = 0, /* default: invalid phys addr */ |
| 715 | .fmt = 0, /* default: 32bit access, hex output */ |
| 716 | .mask = 0xFFFFFFFF, /* default: no mask */ |
| 717 | .shift = 0, /* default: no bit shift */ |
| 718 | }; |
| 719 | |
| 720 | /* read or write ? */ |
| 721 | if (!strncmp(b, "read ", 5)) { |
| 722 | write = 0; |
| 723 | b += 5; |
| 724 | } else if (!strncmp(b, "write ", 6)) { |
| 725 | write = 1; |
| 726 | b += 6; |
| 727 | } else |
| 728 | return -EINVAL; |
| 729 | |
| 730 | /* OPTIONS -l|-w|-b -s -m -o */ |
| 731 | while ((*b == ' ') || (*b == '-')) { |
| 732 | if (*(b-1) != ' ') { |
| 733 | b++; |
| 734 | continue; |
| 735 | } |
| 736 | if ((!strncmp(b, "-d ", 3)) || |
| 737 | (!strncmp(b, "-dec ", 5))) { |
| 738 | b += (*(b+2) == ' ') ? 3 : 5; |
| 739 | loc.fmt |= (1<<0); |
| 740 | } else if ((!strncmp(b, "-h ", 3)) || |
| 741 | (!strncmp(b, "-hex ", 5))) { |
| 742 | b += (*(b+2) == ' ') ? 3 : 5; |
| 743 | loc.fmt &= ~(1<<0); |
| 744 | } else if ((!strncmp(b, "-m ", 3)) || |
| 745 | (!strncmp(b, "-mask ", 6))) { |
| 746 | b += (*(b+2) == ' ') ? 3 : 6; |
| 747 | if (strval_len(b) == 0) |
| 748 | return -EINVAL; |
| 749 | loc.mask = simple_strtoul(b, &b, 0); |
| 750 | } else if ((!strncmp(b, "-s ", 3)) || |
| 751 | (!strncmp(b, "-shift ", 7))) { |
| 752 | b += (*(b+2) == ' ') ? 3 : 7; |
| 753 | if (strval_len(b) == 0) |
| 754 | return -EINVAL; |
| 755 | loc.shift = simple_strtol(b, &b, 0); |
| 756 | } else { |
| 757 | return -EINVAL; |
| 758 | } |
| 759 | } |
| 760 | /* get arg BANK and ADDRESS */ |
| 761 | if (strval_len(b) == 0) |
| 762 | return -EINVAL; |
| 763 | loc.bank = simple_strtoul(b, &b, 0); |
| 764 | while (*b == ' ') |
| 765 | b++; |
| 766 | if (strval_len(b) == 0) |
| 767 | return -EINVAL; |
| 768 | loc.addr = simple_strtoul(b, &b, 0); |
| 769 | |
| 770 | if (write) { |
| 771 | while (*b == ' ') |
| 772 | b++; |
| 773 | if (strval_len(b) == 0) |
| 774 | return -EINVAL; |
| 775 | val = simple_strtoul(b, &b, 0); |
| 776 | } |
| 777 | |
| 778 | /* args are ok, update target cfg (mainly for read) */ |
| 779 | *cfg = loc; |
| 780 | |
| 781 | #ifdef ABB_HWREG_DEBUG |
| 782 | pr_warn("HWREG request: %s, %s, addr=0x%08X, mask=0x%X, shift=%d" |
| 783 | "value=0x%X\n", (write) ? "write" : "read", |
| 784 | REG_FMT_DEC(cfg) ? "decimal" : "hexa", |
| 785 | cfg->addr, cfg->mask, cfg->shift, val); |
| 786 | #endif |
| 787 | |
| 788 | if (!write) |
| 789 | return 0; |
| 790 | |
| 791 | ret = abx500_get_register_interruptible(dev, |
| 792 | (u8)cfg->bank, (u8)cfg->addr, ®value); |
| 793 | if (ret < 0) { |
| 794 | dev_err(dev, "abx500_get_reg fail %d, %d\n", |
| 795 | ret, __LINE__); |
| 796 | return -EINVAL; |
| 797 | } |
| 798 | |
| 799 | if (cfg->shift >= 0) { |
| 800 | regvalue &= ~(cfg->mask << (cfg->shift)); |
| 801 | val = (val & cfg->mask) << (cfg->shift); |
| 802 | } else { |
| 803 | regvalue &= ~(cfg->mask >> (-cfg->shift)); |
| 804 | val = (val & cfg->mask) >> (-cfg->shift); |
| 805 | } |
| 806 | val = val | regvalue; |
| 807 | |
| 808 | ret = abx500_set_register_interruptible(dev, |
| 809 | (u8)cfg->bank, (u8)cfg->addr, (u8)val); |
| 810 | if (ret < 0) { |
| 811 | pr_err("abx500_set_reg failed %d, %d", ret, __LINE__); |
| 812 | return -EINVAL; |
| 813 | } |
| 814 | |
| 815 | return 0; |
| 816 | } |
| 817 | |
| 818 | static ssize_t ab8500_hwreg_write(struct file *file, |
| 819 | const char __user *user_buf, size_t count, loff_t *ppos) |
| 820 | { |
| 821 | struct device *dev = ((struct seq_file *)(file->private_data))->private; |
| 822 | char buf[128]; |
| 823 | int buf_size, ret; |
| 824 | |
| 825 | /* Get userspace string and assure termination */ |
| 826 | buf_size = min(count, (sizeof(buf)-1)); |
| 827 | if (copy_from_user(buf, user_buf, buf_size)) |
| 828 | return -EFAULT; |
| 829 | buf[buf_size] = 0; |
| 830 | |
| 831 | /* get args and process */ |
| 832 | ret = hwreg_common_write(buf, &hwreg_cfg, dev); |
| 833 | return (ret) ? ret : buf_size; |
| 834 | } |
| 835 | |
| 836 | /* |
| 837 | * - irq subscribe/unsubscribe stuff |
| 838 | */ |
Lee Jones | 4b8ac08 | 2013-01-14 16:10:36 +0000 | [diff] [blame] | 839 | static int ab8500_subscribe_unsubscribe_print(struct seq_file *s, void *p) |
| 840 | { |
| 841 | seq_printf(s, "%d\n", irq_first); |
| 842 | |
| 843 | return 0; |
| 844 | } |
| 845 | |
| 846 | static int ab8500_subscribe_unsubscribe_open(struct inode *inode, |
| 847 | struct file *file) |
| 848 | { |
| 849 | return single_open(file, ab8500_subscribe_unsubscribe_print, |
| 850 | inode->i_private); |
| 851 | } |
| 852 | |
| 853 | /* |
Mattias Wallin | 0b337e7 | 2010-11-19 17:55:11 +0100 | [diff] [blame] | 854 | * Userspace should use poll() on this file. When an event occur |
Lee Jones | 4b8ac08 | 2013-01-14 16:10:36 +0000 | [diff] [blame] | 855 | * the blocking poll will be released. |
| 856 | */ |
| 857 | static ssize_t show_irq(struct device *dev, |
| 858 | struct device_attribute *attr, char *buf) |
| 859 | { |
Mattias Wallin | 0b337e7 | 2010-11-19 17:55:11 +0100 | [diff] [blame] | 860 | unsigned long name; |
| 861 | unsigned int irq_index; |
| 862 | int err; |
Lee Jones | 4b8ac08 | 2013-01-14 16:10:36 +0000 | [diff] [blame] | 863 | |
Mattias Wallin | 0b337e7 | 2010-11-19 17:55:11 +0100 | [diff] [blame] | 864 | err = strict_strtoul(attr->attr.name, 0, &name); |
| 865 | if (err) |
| 866 | return err; |
| 867 | |
| 868 | irq_index = name - irq_first; |
| 869 | if (irq_index >= AB8500_NR_IRQS) |
| 870 | return -EINVAL; |
| 871 | else |
| 872 | return sprintf(buf, "%u\n", irq_count[irq_index]); |
| 873 | } |
Lee Jones | 4b8ac08 | 2013-01-14 16:10:36 +0000 | [diff] [blame] | 874 | |
| 875 | static ssize_t ab8500_subscribe_write(struct file *file, |
| 876 | const char __user *user_buf, |
| 877 | size_t count, loff_t *ppos) |
| 878 | { |
| 879 | struct device *dev = ((struct seq_file *)(file->private_data))->private; |
| 880 | char buf[32]; |
| 881 | int buf_size; |
| 882 | unsigned long user_val; |
| 883 | int err; |
Mattias Wallin | 0b337e7 | 2010-11-19 17:55:11 +0100 | [diff] [blame] | 884 | unsigned int irq_index; |
Lee Jones | 4b8ac08 | 2013-01-14 16:10:36 +0000 | [diff] [blame] | 885 | |
| 886 | /* Get userspace string and assure termination */ |
| 887 | buf_size = min(count, (sizeof(buf)-1)); |
| 888 | if (copy_from_user(buf, user_buf, buf_size)) |
| 889 | return -EFAULT; |
| 890 | buf[buf_size] = 0; |
| 891 | |
| 892 | err = strict_strtoul(buf, 0, &user_val); |
| 893 | if (err) |
| 894 | return -EINVAL; |
| 895 | if (user_val < irq_first) { |
| 896 | dev_err(dev, "debugfs error input < %d\n", irq_first); |
| 897 | return -EINVAL; |
| 898 | } |
| 899 | if (user_val > irq_last) { |
| 900 | dev_err(dev, "debugfs error input > %d\n", irq_last); |
| 901 | return -EINVAL; |
| 902 | } |
| 903 | |
Mattias Wallin | 0b337e7 | 2010-11-19 17:55:11 +0100 | [diff] [blame] | 904 | irq_index = user_val - irq_first; |
| 905 | if (irq_index >= AB8500_NR_IRQS) |
| 906 | return -EINVAL; |
| 907 | |
Lee Jones | 4b8ac08 | 2013-01-14 16:10:36 +0000 | [diff] [blame] | 908 | /* |
Mattias Wallin | 0b337e7 | 2010-11-19 17:55:11 +0100 | [diff] [blame] | 909 | * This will create a sysfs file named <irq-nr> which userspace can |
Lee Jones | 4b8ac08 | 2013-01-14 16:10:36 +0000 | [diff] [blame] | 910 | * use to select or poll and get the AB8500 events |
| 911 | */ |
Mattias Wallin | 0b337e7 | 2010-11-19 17:55:11 +0100 | [diff] [blame] | 912 | dev_attr[irq_index] = kmalloc(sizeof(struct device_attribute), |
| 913 | GFP_KERNEL); |
| 914 | event_name[irq_index] = kmalloc(buf_size, GFP_KERNEL); |
| 915 | sprintf(event_name[irq_index], "%lu", user_val); |
| 916 | dev_attr[irq_index]->show = show_irq; |
| 917 | dev_attr[irq_index]->store = NULL; |
| 918 | dev_attr[irq_index]->attr.name = event_name[irq_index]; |
| 919 | dev_attr[irq_index]->attr.mode = S_IRUGO; |
| 920 | err = sysfs_create_file(&dev->kobj, &dev_attr[irq_index]->attr); |
Lee Jones | 4b8ac08 | 2013-01-14 16:10:36 +0000 | [diff] [blame] | 921 | if (err < 0) { |
| 922 | printk(KERN_ERR "sysfs_create_file failed %d\n", err); |
| 923 | return err; |
| 924 | } |
| 925 | |
| 926 | err = request_threaded_irq(user_val, NULL, ab8500_debug_handler, |
| 927 | IRQF_SHARED | IRQF_NO_SUSPEND, |
| 928 | "ab8500-debug", &dev->kobj); |
| 929 | if (err < 0) { |
| 930 | printk(KERN_ERR "request_threaded_irq failed %d, %lu\n", |
| 931 | err, user_val); |
Mattias Wallin | 0b337e7 | 2010-11-19 17:55:11 +0100 | [diff] [blame] | 932 | sysfs_remove_file(&dev->kobj, &dev_attr[irq_index]->attr); |
Lee Jones | 4b8ac08 | 2013-01-14 16:10:36 +0000 | [diff] [blame] | 933 | return err; |
| 934 | } |
| 935 | |
| 936 | return buf_size; |
| 937 | } |
| 938 | |
| 939 | static ssize_t ab8500_unsubscribe_write(struct file *file, |
| 940 | const char __user *user_buf, |
| 941 | size_t count, loff_t *ppos) |
| 942 | { |
| 943 | struct device *dev = ((struct seq_file *)(file->private_data))->private; |
| 944 | char buf[32]; |
| 945 | int buf_size; |
| 946 | unsigned long user_val; |
| 947 | int err; |
Mattias Wallin | 0b337e7 | 2010-11-19 17:55:11 +0100 | [diff] [blame] | 948 | unsigned int irq_index; |
Lee Jones | 4b8ac08 | 2013-01-14 16:10:36 +0000 | [diff] [blame] | 949 | |
| 950 | /* Get userspace string and assure termination */ |
| 951 | buf_size = min(count, (sizeof(buf)-1)); |
| 952 | if (copy_from_user(buf, user_buf, buf_size)) |
| 953 | return -EFAULT; |
| 954 | buf[buf_size] = 0; |
| 955 | |
| 956 | err = strict_strtoul(buf, 0, &user_val); |
| 957 | if (err) |
| 958 | return -EINVAL; |
| 959 | if (user_val < irq_first) { |
| 960 | dev_err(dev, "debugfs error input < %d\n", irq_first); |
| 961 | return -EINVAL; |
| 962 | } |
| 963 | if (user_val > irq_last) { |
| 964 | dev_err(dev, "debugfs error input > %d\n", irq_last); |
| 965 | return -EINVAL; |
| 966 | } |
| 967 | |
Mattias Wallin | 0b337e7 | 2010-11-19 17:55:11 +0100 | [diff] [blame] | 968 | irq_index = user_val - irq_first; |
| 969 | if (irq_index >= AB8500_NR_IRQS) |
| 970 | return -EINVAL; |
Lee Jones | 4b8ac08 | 2013-01-14 16:10:36 +0000 | [diff] [blame] | 971 | |
Mattias Wallin | 0b337e7 | 2010-11-19 17:55:11 +0100 | [diff] [blame] | 972 | /* Set irq count to 0 when unsubscribe */ |
| 973 | irq_count[irq_index] = 0; |
| 974 | |
| 975 | if (dev_attr[irq_index]) |
| 976 | sysfs_remove_file(&dev->kobj, &dev_attr[irq_index]->attr); |
| 977 | |
| 978 | |
| 979 | free_irq(user_val, &dev->kobj); |
| 980 | kfree(event_name[irq_index]); |
| 981 | kfree(dev_attr[irq_index]); |
Lee Jones | 4b8ac08 | 2013-01-14 16:10:36 +0000 | [diff] [blame] | 982 | |
| 983 | return buf_size; |
| 984 | } |
| 985 | |
carriere etienne | 0fbce76 | 2011-04-08 16:26:36 +0200 | [diff] [blame^] | 986 | /* |
| 987 | * - several deubgfs nodes fops |
| 988 | */ |
| 989 | |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 990 | static const struct file_operations ab8500_bank_fops = { |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 991 | .open = ab8500_bank_open, |
| 992 | .write = ab8500_bank_write, |
| 993 | .read = seq_read, |
| 994 | .llseek = seq_lseek, |
| 995 | .release = single_release, |
| 996 | .owner = THIS_MODULE, |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 997 | }; |
| 998 | |
| 999 | static const struct file_operations ab8500_address_fops = { |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 1000 | .open = ab8500_address_open, |
| 1001 | .write = ab8500_address_write, |
| 1002 | .read = seq_read, |
| 1003 | .llseek = seq_lseek, |
| 1004 | .release = single_release, |
| 1005 | .owner = THIS_MODULE, |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 1006 | }; |
| 1007 | |
| 1008 | static const struct file_operations ab8500_val_fops = { |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 1009 | .open = ab8500_val_open, |
| 1010 | .write = ab8500_val_write, |
| 1011 | .read = seq_read, |
| 1012 | .llseek = seq_lseek, |
| 1013 | .release = single_release, |
| 1014 | .owner = THIS_MODULE, |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 1015 | }; |
| 1016 | |
Lee Jones | 4b8ac08 | 2013-01-14 16:10:36 +0000 | [diff] [blame] | 1017 | static const struct file_operations ab8500_subscribe_fops = { |
| 1018 | .open = ab8500_subscribe_unsubscribe_open, |
| 1019 | .write = ab8500_subscribe_write, |
| 1020 | .read = seq_read, |
| 1021 | .llseek = seq_lseek, |
| 1022 | .release = single_release, |
| 1023 | .owner = THIS_MODULE, |
| 1024 | }; |
| 1025 | |
| 1026 | static const struct file_operations ab8500_unsubscribe_fops = { |
| 1027 | .open = ab8500_subscribe_unsubscribe_open, |
| 1028 | .write = ab8500_unsubscribe_write, |
| 1029 | .read = seq_read, |
| 1030 | .llseek = seq_lseek, |
| 1031 | .release = single_release, |
| 1032 | .owner = THIS_MODULE, |
| 1033 | }; |
| 1034 | |
carriere etienne | 0fbce76 | 2011-04-08 16:26:36 +0200 | [diff] [blame^] | 1035 | static const struct file_operations ab8500_hwreg_fops = { |
| 1036 | .open = ab8500_hwreg_open, |
| 1037 | .write = ab8500_hwreg_write, |
| 1038 | .read = seq_read, |
| 1039 | .llseek = seq_lseek, |
| 1040 | .release = single_release, |
| 1041 | .owner = THIS_MODULE, |
| 1042 | }; |
| 1043 | |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 1044 | static struct dentry *ab8500_dir; |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 1045 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 1046 | static int ab8500_debug_probe(struct platform_device *plf) |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 1047 | { |
carriere etienne | 0fbce76 | 2011-04-08 16:26:36 +0200 | [diff] [blame^] | 1048 | struct dentry *file; |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 1049 | debug_bank = AB8500_MISC; |
| 1050 | debug_address = AB8500_REV_REG & 0x00FF; |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 1051 | |
Lee Jones | 4b8ac08 | 2013-01-14 16:10:36 +0000 | [diff] [blame] | 1052 | irq_first = platform_get_irq_byname(plf, "IRQ_FIRST"); |
| 1053 | if (irq_first < 0) { |
| 1054 | dev_err(&plf->dev, "First irq not found, err %d\n", |
| 1055 | irq_first); |
| 1056 | return irq_first; |
| 1057 | } |
| 1058 | |
| 1059 | irq_last = platform_get_irq_byname(plf, "IRQ_LAST"); |
| 1060 | if (irq_last < 0) { |
| 1061 | dev_err(&plf->dev, "Last irq not found, err %d\n", |
| 1062 | irq_last); |
| 1063 | return irq_last; |
| 1064 | } |
| 1065 | |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 1066 | ab8500_dir = debugfs_create_dir(AB8500_NAME_STRING, NULL); |
| 1067 | if (!ab8500_dir) |
carriere etienne | 0fbce76 | 2011-04-08 16:26:36 +0200 | [diff] [blame^] | 1068 | goto err; |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 1069 | |
carriere etienne | 0fbce76 | 2011-04-08 16:26:36 +0200 | [diff] [blame^] | 1070 | file = debugfs_create_file("all-bank-registers", |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 1071 | S_IRUGO, ab8500_dir, &plf->dev, &ab8500_registers_fops); |
carriere etienne | 0fbce76 | 2011-04-08 16:26:36 +0200 | [diff] [blame^] | 1072 | if (!file) |
| 1073 | goto err; |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 1074 | |
carriere etienne | 0fbce76 | 2011-04-08 16:26:36 +0200 | [diff] [blame^] | 1075 | file = debugfs_create_file("register-bank", |
Vasiliy Kulikov | 44bdcb5 | 2011-02-04 15:23:43 +0300 | [diff] [blame] | 1076 | (S_IRUGO | S_IWUSR), ab8500_dir, &plf->dev, &ab8500_bank_fops); |
carriere etienne | 0fbce76 | 2011-04-08 16:26:36 +0200 | [diff] [blame^] | 1077 | if (!file) |
| 1078 | goto err; |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 1079 | |
carriere etienne | 0fbce76 | 2011-04-08 16:26:36 +0200 | [diff] [blame^] | 1080 | file = debugfs_create_file("register-address", |
Vasiliy Kulikov | 44bdcb5 | 2011-02-04 15:23:43 +0300 | [diff] [blame] | 1081 | (S_IRUGO | S_IWUSR), ab8500_dir, &plf->dev, |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 1082 | &ab8500_address_fops); |
carriere etienne | 0fbce76 | 2011-04-08 16:26:36 +0200 | [diff] [blame^] | 1083 | if (!file) |
| 1084 | goto err; |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 1085 | |
carriere etienne | 0fbce76 | 2011-04-08 16:26:36 +0200 | [diff] [blame^] | 1086 | file = debugfs_create_file("register-value", |
Vasiliy Kulikov | 44bdcb5 | 2011-02-04 15:23:43 +0300 | [diff] [blame] | 1087 | (S_IRUGO | S_IWUSR), ab8500_dir, &plf->dev, &ab8500_val_fops); |
carriere etienne | 0fbce76 | 2011-04-08 16:26:36 +0200 | [diff] [blame^] | 1088 | if (!file) |
| 1089 | goto err; |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 1090 | |
carriere etienne | 0fbce76 | 2011-04-08 16:26:36 +0200 | [diff] [blame^] | 1091 | file = debugfs_create_file("irq-subscribe", |
| 1092 | (S_IRUGO | S_IWUSR), ab8500_dir, &plf->dev, |
| 1093 | &ab8500_subscribe_fops); |
| 1094 | if (!file) |
| 1095 | goto err; |
Lee Jones | 4b8ac08 | 2013-01-14 16:10:36 +0000 | [diff] [blame] | 1096 | |
carriere etienne | 0fbce76 | 2011-04-08 16:26:36 +0200 | [diff] [blame^] | 1097 | file = debugfs_create_file("irq-unsubscribe", |
| 1098 | (S_IRUGO | S_IWUSR), ab8500_dir, &plf->dev, |
| 1099 | &ab8500_unsubscribe_fops); |
| 1100 | if (!file) |
| 1101 | goto err; |
| 1102 | |
| 1103 | file = debugfs_create_file("hwreg", |
| 1104 | (S_IRUGO | S_IWUSR), ab8500_dir, &plf->dev, |
| 1105 | &ab8500_hwreg_fops); |
| 1106 | if (!file) |
| 1107 | goto err; |
Lee Jones | 4b8ac08 | 2013-01-14 16:10:36 +0000 | [diff] [blame] | 1108 | |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 1109 | return 0; |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 1110 | |
carriere etienne | 0fbce76 | 2011-04-08 16:26:36 +0200 | [diff] [blame^] | 1111 | err: |
| 1112 | if (ab8500_dir) |
| 1113 | debugfs_remove_recursive(ab8500_dir); |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 1114 | dev_err(&plf->dev, "failed to create debugfs entries.\n"); |
| 1115 | return -ENOMEM; |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 1116 | } |
| 1117 | |
Bill Pemberton | 4740f73 | 2012-11-19 13:26:01 -0500 | [diff] [blame] | 1118 | static int ab8500_debug_remove(struct platform_device *plf) |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 1119 | { |
carriere etienne | 0fbce76 | 2011-04-08 16:26:36 +0200 | [diff] [blame^] | 1120 | debugfs_remove_recursive(ab8500_dir); |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 1121 | return 0; |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | static struct platform_driver ab8500_debug_driver = { |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 1125 | .driver = { |
| 1126 | .name = "ab8500-debug", |
| 1127 | .owner = THIS_MODULE, |
| 1128 | }, |
| 1129 | .probe = ab8500_debug_probe, |
Bill Pemberton | 8444921 | 2012-11-19 13:20:24 -0500 | [diff] [blame] | 1130 | .remove = ab8500_debug_remove |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 1131 | }; |
| 1132 | |
| 1133 | static int __init ab8500_debug_init(void) |
| 1134 | { |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 1135 | return platform_driver_register(&ab8500_debug_driver); |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | static void __exit ab8500_debug_exit(void) |
| 1139 | { |
Mattias Wallin | d7b9f32 | 2010-11-26 13:06:39 +0100 | [diff] [blame] | 1140 | platform_driver_unregister(&ab8500_debug_driver); |
Mattias Wallin | 5814fc3 | 2010-09-13 16:05:04 +0200 | [diff] [blame] | 1141 | } |
| 1142 | subsys_initcall(ab8500_debug_init); |
| 1143 | module_exit(ab8500_debug_exit); |
| 1144 | |
| 1145 | MODULE_AUTHOR("Mattias WALLIN <mattias.wallin@stericsson.com"); |
| 1146 | MODULE_DESCRIPTION("AB8500 DEBUG"); |
| 1147 | MODULE_LICENSE("GPL v2"); |