]>
Commit | Line | Data |
---|---|---|
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 | */ | |
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 | */ | |
73 | ||
74 | #include <linux/seq_file.h> | |
75 | #include <linux/uaccess.h> | |
76 | #include <linux/fs.h> | |
77 | #include <linux/module.h> | |
78 | #include <linux/debugfs.h> | |
79 | #include <linux/platform_device.h> | |
80 | #include <linux/interrupt.h> | |
81 | #include <linux/kobject.h> | |
82 | #include <linux/slab.h> | |
83 | ||
84 | #include <linux/mfd/abx500.h> | |
85 | #include <linux/mfd/abx500/ab8500.h> | |
86 | ||
87 | #ifdef CONFIG_DEBUG_FS | |
88 | #include <linux/string.h> | |
89 | #include <linux/ctype.h> | |
90 | #endif | |
91 | ||
92 | static u32 debug_bank; | |
93 | static u32 debug_address; | |
94 | ||
95 | static int irq_first; | |
96 | static int irq_last; | |
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]; | |
101 | ||
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 { | |
109 | u8 first; | |
110 | u8 last; | |
111 | u8 perm; | |
112 | }; | |
113 | ||
114 | /** | |
115 | * struct ab8500_prcmu_ranges | |
116 | * @num_ranges: the number of ranges in the list | |
117 | * @bankid: bank identifier | |
118 | * @range: the list of register ranges | |
119 | */ | |
120 | struct ab8500_prcmu_ranges { | |
121 | u8 num_ranges; | |
122 | u8 bankid; | |
123 | const struct ab8500_reg_range *range; | |
124 | }; | |
125 | ||
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 | ||
145 | #define AB8500_NAME_STRING "ab8500" | |
146 | #define AB8500_NUM_BANKS 22 | |
147 | ||
148 | #define AB8500_REV_REG 0x80 | |
149 | ||
150 | static struct ab8500_prcmu_ranges debug_ranges[AB8500_NUM_BANKS] = { | |
151 | [0x0] = { | |
152 | .num_ranges = 0, | |
153 | .range = NULL, | |
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, | |
293 | .range = NULL, | |
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, | |
306 | .range = NULL, | |
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, | |
382 | .range = NULL, | |
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, | |
432 | .range = NULL, | |
433 | }, | |
434 | [0x12] = { | |
435 | .num_ranges = 0, | |
436 | .range = NULL, | |
437 | }, | |
438 | [0x13] = { | |
439 | .num_ranges = 0, | |
440 | .range = NULL, | |
441 | }, | |
442 | [0x14] = { | |
443 | .num_ranges = 0, | |
444 | .range = NULL, | |
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 | }, | |
455 | }; | |
456 | ||
457 | static irqreturn_t ab8500_debug_handler(int irq, void *data) | |
458 | { | |
459 | char buf[16]; | |
460 | struct kobject *kobj = (struct kobject *)data; | |
461 | unsigned int irq_abb = irq - irq_first; | |
462 | ||
463 | if (irq_abb < AB8500_NR_IRQS) | |
464 | irq_count[irq_abb]++; | |
465 | /* | |
466 | * This makes it possible to use poll for events (POLLPRI | POLLERR) | |
467 | * from userspace on sysfs file named <irq-nr> | |
468 | */ | |
469 | sprintf(buf, "%d", irq); | |
470 | sysfs_notify(kobj, NULL, buf); | |
471 | ||
472 | return IRQ_HANDLED; | |
473 | } | |
474 | ||
475 | static int ab8500_registers_print(struct seq_file *s, void *p) | |
476 | { | |
477 | struct device *dev = s->private; | |
478 | unsigned int i; | |
479 | u32 bank = debug_bank; | |
480 | ||
481 | seq_printf(s, AB8500_NAME_STRING " register values:\n"); | |
482 | ||
483 | seq_printf(s, " bank %u:\n", bank); | |
484 | for (i = 0; i < debug_ranges[bank].num_ranges; i++) { | |
485 | u32 reg; | |
486 | ||
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; | |
492 | ||
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 | } | |
499 | ||
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; | |
511 | } | |
512 | ||
513 | static int ab8500_registers_open(struct inode *inode, struct file *file) | |
514 | { | |
515 | return single_open(file, ab8500_registers_print, inode->i_private); | |
516 | } | |
517 | ||
518 | static const struct file_operations ab8500_registers_fops = { | |
519 | .open = ab8500_registers_open, | |
520 | .read = seq_read, | |
521 | .llseek = seq_lseek, | |
522 | .release = single_release, | |
523 | .owner = THIS_MODULE, | |
524 | }; | |
525 | ||
526 | static int ab8500_bank_print(struct seq_file *s, void *p) | |
527 | { | |
528 | return seq_printf(s, "%d\n", debug_bank); | |
529 | } | |
530 | ||
531 | static int ab8500_bank_open(struct inode *inode, struct file *file) | |
532 | { | |
533 | return single_open(file, ab8500_bank_print, inode->i_private); | |
534 | } | |
535 | ||
536 | static ssize_t ab8500_bank_write(struct file *file, | |
537 | const char __user *user_buf, | |
538 | size_t count, loff_t *ppos) | |
539 | { | |
540 | struct device *dev = ((struct seq_file *)(file->private_data))->private; | |
541 | unsigned long user_bank; | |
542 | int err; | |
543 | ||
544 | /* Get userspace string and assure termination */ | |
545 | err = kstrtoul_from_user(user_buf, count, 0, &user_bank); | |
546 | if (err) | |
547 | return err; | |
548 | ||
549 | if (user_bank >= AB8500_NUM_BANKS) { | |
550 | dev_err(dev, "debugfs error input > number of banks\n"); | |
551 | return -EINVAL; | |
552 | } | |
553 | ||
554 | debug_bank = user_bank; | |
555 | ||
556 | return count; | |
557 | } | |
558 | ||
559 | static int ab8500_address_print(struct seq_file *s, void *p) | |
560 | { | |
561 | return seq_printf(s, "0x%02X\n", debug_address); | |
562 | } | |
563 | ||
564 | static int ab8500_address_open(struct inode *inode, struct file *file) | |
565 | { | |
566 | return single_open(file, ab8500_address_print, inode->i_private); | |
567 | } | |
568 | ||
569 | static ssize_t ab8500_address_write(struct file *file, | |
570 | const char __user *user_buf, | |
571 | size_t count, loff_t *ppos) | |
572 | { | |
573 | struct device *dev = ((struct seq_file *)(file->private_data))->private; | |
574 | unsigned long user_address; | |
575 | int err; | |
576 | ||
577 | /* Get userspace string and assure termination */ | |
578 | err = kstrtoul_from_user(user_buf, count, 0, &user_address); | |
579 | if (err) | |
580 | return err; | |
581 | ||
582 | if (user_address > 0xff) { | |
583 | dev_err(dev, "debugfs error input > 0xff\n"); | |
584 | return -EINVAL; | |
585 | } | |
586 | debug_address = user_address; | |
587 | return count; | |
588 | } | |
589 | ||
590 | static int ab8500_val_print(struct seq_file *s, void *p) | |
591 | { | |
592 | struct device *dev = s->private; | |
593 | int ret; | |
594 | u8 regvalue; | |
595 | ||
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); | |
604 | ||
605 | return 0; | |
606 | } | |
607 | ||
608 | static int ab8500_val_open(struct inode *inode, struct file *file) | |
609 | { | |
610 | return single_open(file, ab8500_val_print, inode->i_private); | |
611 | } | |
612 | ||
613 | static ssize_t ab8500_val_write(struct file *file, | |
614 | const char __user *user_buf, | |
615 | size_t count, loff_t *ppos) | |
616 | { | |
617 | struct device *dev = ((struct seq_file *)(file->private_data))->private; | |
618 | unsigned long user_val; | |
619 | int err; | |
620 | ||
621 | /* Get userspace string and assure termination */ | |
622 | err = kstrtoul_from_user(user_buf, count, 0, &user_val); | |
623 | if (err) | |
624 | return err; | |
625 | ||
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 | } | |
636 | ||
637 | return count; | |
638 | } | |
639 | ||
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 | */ | |
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 | /* | |
854 | * Userspace should use poll() on this file. When an event occur | |
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 | { | |
860 | unsigned long name; | |
861 | unsigned int irq_index; | |
862 | int err; | |
863 | ||
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 | } | |
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; | |
884 | unsigned int irq_index; | |
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 | ||
904 | irq_index = user_val - irq_first; | |
905 | if (irq_index >= AB8500_NR_IRQS) | |
906 | return -EINVAL; | |
907 | ||
908 | /* | |
909 | * This will create a sysfs file named <irq-nr> which userspace can | |
910 | * use to select or poll and get the AB8500 events | |
911 | */ | |
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); | |
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); | |
932 | sysfs_remove_file(&dev->kobj, &dev_attr[irq_index]->attr); | |
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; | |
948 | unsigned int irq_index; | |
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 | ||
968 | irq_index = user_val - irq_first; | |
969 | if (irq_index >= AB8500_NR_IRQS) | |
970 | return -EINVAL; | |
971 | ||
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]); | |
982 | ||
983 | return buf_size; | |
984 | } | |
985 | ||
986 | /* | |
987 | * - several deubgfs nodes fops | |
988 | */ | |
989 | ||
990 | static const struct file_operations ab8500_bank_fops = { | |
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, | |
997 | }; | |
998 | ||
999 | static const struct file_operations ab8500_address_fops = { | |
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, | |
1006 | }; | |
1007 | ||
1008 | static const struct file_operations ab8500_val_fops = { | |
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, | |
1015 | }; | |
1016 | ||
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 | ||
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 | ||
1044 | static struct dentry *ab8500_dir; | |
1045 | ||
1046 | static int ab8500_debug_probe(struct platform_device *plf) | |
1047 | { | |
1048 | struct dentry *file; | |
1049 | debug_bank = AB8500_MISC; | |
1050 | debug_address = AB8500_REV_REG & 0x00FF; | |
1051 | ||
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 | ||
1066 | ab8500_dir = debugfs_create_dir(AB8500_NAME_STRING, NULL); | |
1067 | if (!ab8500_dir) | |
1068 | goto err; | |
1069 | ||
1070 | file = debugfs_create_file("all-bank-registers", | |
1071 | S_IRUGO, ab8500_dir, &plf->dev, &ab8500_registers_fops); | |
1072 | if (!file) | |
1073 | goto err; | |
1074 | ||
1075 | file = debugfs_create_file("register-bank", | |
1076 | (S_IRUGO | S_IWUSR), ab8500_dir, &plf->dev, &ab8500_bank_fops); | |
1077 | if (!file) | |
1078 | goto err; | |
1079 | ||
1080 | file = debugfs_create_file("register-address", | |
1081 | (S_IRUGO | S_IWUSR), ab8500_dir, &plf->dev, | |
1082 | &ab8500_address_fops); | |
1083 | if (!file) | |
1084 | goto err; | |
1085 | ||
1086 | file = debugfs_create_file("register-value", | |
1087 | (S_IRUGO | S_IWUSR), ab8500_dir, &plf->dev, &ab8500_val_fops); | |
1088 | if (!file) | |
1089 | goto err; | |
1090 | ||
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; | |
1096 | ||
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; | |
1108 | ||
1109 | return 0; | |
1110 | ||
1111 | err: | |
1112 | if (ab8500_dir) | |
1113 | debugfs_remove_recursive(ab8500_dir); | |
1114 | dev_err(&plf->dev, "failed to create debugfs entries.\n"); | |
1115 | return -ENOMEM; | |
1116 | } | |
1117 | ||
1118 | static int ab8500_debug_remove(struct platform_device *plf) | |
1119 | { | |
1120 | debugfs_remove_recursive(ab8500_dir); | |
1121 | return 0; | |
1122 | } | |
1123 | ||
1124 | static struct platform_driver ab8500_debug_driver = { | |
1125 | .driver = { | |
1126 | .name = "ab8500-debug", | |
1127 | .owner = THIS_MODULE, | |
1128 | }, | |
1129 | .probe = ab8500_debug_probe, | |
1130 | .remove = ab8500_debug_remove | |
1131 | }; | |
1132 | ||
1133 | static int __init ab8500_debug_init(void) | |
1134 | { | |
1135 | return platform_driver_register(&ab8500_debug_driver); | |
1136 | } | |
1137 | ||
1138 | static void __exit ab8500_debug_exit(void) | |
1139 | { | |
1140 | platform_driver_unregister(&ab8500_debug_driver); | |
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"); |