blob: 79a954f79732bad60a8fa0fa4e31cc692113f93c [file] [log] [blame]
Mattias Wallin5814fc32010-09-13 16:05:04 +02001/*
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 etienne0fbce762011-04-08 16:26:36 +02007/*
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 Wallin5814fc32010-09-13 16:05:04 +020073
74#include <linux/seq_file.h>
75#include <linux/uaccess.h>
76#include <linux/fs.h>
Paul Gortmaker4e36dd32011-07-03 15:13:27 -040077#include <linux/module.h>
Mattias Wallin5814fc32010-09-13 16:05:04 +020078#include <linux/debugfs.h>
79#include <linux/platform_device.h>
Lee Jones4b8ac082013-01-14 16:10:36 +000080#include <linux/interrupt.h>
81#include <linux/kobject.h>
82#include <linux/slab.h>
Mattias Wallin5814fc32010-09-13 16:05:04 +020083
84#include <linux/mfd/abx500.h>
Linus Walleijee66e652011-12-02 14:16:33 +010085#include <linux/mfd/abx500/ab8500.h>
Mattias Wallin5814fc32010-09-13 16:05:04 +020086
carriere etienne0fbce762011-04-08 16:26:36 +020087#ifdef CONFIG_DEBUG_FS
88#include <linux/string.h>
89#include <linux/ctype.h>
90#endif
91
Mattias Wallin5814fc32010-09-13 16:05:04 +020092static u32 debug_bank;
93static u32 debug_address;
94
Lee Jones4b8ac082013-01-14 16:10:36 +000095static int irq_first;
96static int irq_last;
Mattias Wallin0b337e72010-11-19 17:55:11 +010097static u32 irq_count[AB8500_NR_IRQS];
98
99static struct device_attribute *dev_attr[AB8500_NR_IRQS];
100static char *event_name[AB8500_NR_IRQS];
Lee Jones4b8ac082013-01-14 16:10:36 +0000101
Mattias Wallin5814fc32010-09-13 16:05:04 +0200102/**
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 */
108struct ab8500_reg_range {
Mattias Wallind7b9f322010-11-26 13:06:39 +0100109 u8 first;
110 u8 last;
111 u8 perm;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200112};
113
114/**
Lee Jones822672a2012-06-20 13:56:38 +0100115 * struct ab8500_prcmu_ranges
Mattias Wallin5814fc32010-09-13 16:05:04 +0200116 * @num_ranges: the number of ranges in the list
117 * @bankid: bank identifier
118 * @range: the list of register ranges
119 */
Lee Jones822672a2012-06-20 13:56:38 +0100120struct ab8500_prcmu_ranges {
Mattias Wallind7b9f322010-11-26 13:06:39 +0100121 u8 num_ranges;
122 u8 bankid;
123 const struct ab8500_reg_range *range;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200124};
125
carriere etienne0fbce762011-04-08 16:26:36 +0200126/* hwreg- "mask" and "shift" entries ressources */
127struct 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
138static 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 Wallin5814fc32010-09-13 16:05:04 +0200145#define AB8500_NAME_STRING "ab8500"
146#define AB8500_NUM_BANKS 22
147
148#define AB8500_REV_REG 0x80
149
Lee Jones822672a2012-06-20 13:56:38 +0100150static struct ab8500_prcmu_ranges debug_ranges[AB8500_NUM_BANKS] = {
Mattias Wallind7b9f322010-11-26 13:06:39 +0100151 [0x0] = {
152 .num_ranges = 0,
Lee Jonesfad55a82013-01-14 17:17:34 +0000153 .range = NULL,
Mattias Wallind7b9f322010-11-26 13:06:39 +0100154 },
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 Brown87fff232010-12-13 14:06:47 +0000293 .range = NULL,
Mattias Wallind7b9f322010-11-26 13:06:39 +0100294 },
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 Brown87fff232010-12-13 14:06:47 +0000306 .range = NULL,
Mattias Wallind7b9f322010-11-26 13:06:39 +0100307 },
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 Brown87fff232010-12-13 14:06:47 +0000382 .range = NULL,
Mattias Wallind7b9f322010-11-26 13:06:39 +0100383 },
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 Brown87fff232010-12-13 14:06:47 +0000432 .range = NULL,
Mattias Wallind7b9f322010-11-26 13:06:39 +0100433 },
434 [0x12] = {
435 .num_ranges = 0,
Mark Brown87fff232010-12-13 14:06:47 +0000436 .range = NULL,
Mattias Wallind7b9f322010-11-26 13:06:39 +0100437 },
438 [0x13] = {
439 .num_ranges = 0,
Mark Brown87fff232010-12-13 14:06:47 +0000440 .range = NULL,
Mattias Wallind7b9f322010-11-26 13:06:39 +0100441 },
442 [0x14] = {
443 .num_ranges = 0,
Mark Brown87fff232010-12-13 14:06:47 +0000444 .range = NULL,
Mattias Wallind7b9f322010-11-26 13:06:39 +0100445 },
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 Wallin5814fc32010-09-13 16:05:04 +0200455};
456
Lee Jones4b8ac082013-01-14 16:10:36 +0000457static irqreturn_t ab8500_debug_handler(int irq, void *data)
458{
459 char buf[16];
460 struct kobject *kobj = (struct kobject *)data;
Mattias Wallin0b337e72010-11-19 17:55:11 +0100461 unsigned int irq_abb = irq - irq_first;
Lee Jones4b8ac082013-01-14 16:10:36 +0000462
Mattias Wallin0b337e72010-11-19 17:55:11 +0100463 if (irq_abb < AB8500_NR_IRQS)
464 irq_count[irq_abb]++;
Lee Jones4b8ac082013-01-14 16:10:36 +0000465 /*
466 * This makes it possible to use poll for events (POLLPRI | POLLERR)
Mattias Wallin0b337e72010-11-19 17:55:11 +0100467 * from userspace on sysfs file named <irq-nr>
Lee Jones4b8ac082013-01-14 16:10:36 +0000468 */
Mattias Wallin0b337e72010-11-19 17:55:11 +0100469 sprintf(buf, "%d", irq);
Lee Jones4b8ac082013-01-14 16:10:36 +0000470 sysfs_notify(kobj, NULL, buf);
471
472 return IRQ_HANDLED;
473}
474
Mattias Wallin5814fc32010-09-13 16:05:04 +0200475static int ab8500_registers_print(struct seq_file *s, void *p)
476{
Mattias Wallind7b9f322010-11-26 13:06:39 +0100477 struct device *dev = s->private;
478 unsigned int i;
479 u32 bank = debug_bank;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200480
Mattias Wallind7b9f322010-11-26 13:06:39 +0100481 seq_printf(s, AB8500_NAME_STRING " register values:\n");
Mattias Wallin5814fc32010-09-13 16:05:04 +0200482
Mattias Wallind7b9f322010-11-26 13:06:39 +0100483 seq_printf(s, " bank %u:\n", bank);
484 for (i = 0; i < debug_ranges[bank].num_ranges; i++) {
485 u32 reg;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200486
Mattias Wallind7b9f322010-11-26 13:06:39 +0100487 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 Wallin5814fc32010-09-13 16:05:04 +0200492
Mattias Wallind7b9f322010-11-26 13:06:39 +0100493 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 Wallin5814fc32010-09-13 16:05:04 +0200499
Mattias Wallind7b9f322010-11-26 13:06:39 +0100500 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 Wallin5814fc32010-09-13 16:05:04 +0200511}
512
513static int ab8500_registers_open(struct inode *inode, struct file *file)
514{
Mattias Wallind7b9f322010-11-26 13:06:39 +0100515 return single_open(file, ab8500_registers_print, inode->i_private);
Mattias Wallin5814fc32010-09-13 16:05:04 +0200516}
517
518static const struct file_operations ab8500_registers_fops = {
Mattias Wallind7b9f322010-11-26 13:06:39 +0100519 .open = ab8500_registers_open,
520 .read = seq_read,
521 .llseek = seq_lseek,
522 .release = single_release,
523 .owner = THIS_MODULE,
Mattias Wallin5814fc32010-09-13 16:05:04 +0200524};
525
526static int ab8500_bank_print(struct seq_file *s, void *p)
527{
Mattias Wallind7b9f322010-11-26 13:06:39 +0100528 return seq_printf(s, "%d\n", debug_bank);
Mattias Wallin5814fc32010-09-13 16:05:04 +0200529}
530
531static int ab8500_bank_open(struct inode *inode, struct file *file)
532{
Mattias Wallind7b9f322010-11-26 13:06:39 +0100533 return single_open(file, ab8500_bank_print, inode->i_private);
Mattias Wallin5814fc32010-09-13 16:05:04 +0200534}
535
536static ssize_t ab8500_bank_write(struct file *file,
Mattias Wallind7b9f322010-11-26 13:06:39 +0100537 const char __user *user_buf,
538 size_t count, loff_t *ppos)
Mattias Wallin5814fc32010-09-13 16:05:04 +0200539{
Mattias Wallind7b9f322010-11-26 13:06:39 +0100540 struct device *dev = ((struct seq_file *)(file->private_data))->private;
Mattias Wallind7b9f322010-11-26 13:06:39 +0100541 unsigned long user_bank;
542 int err;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200543
Mattias Wallind7b9f322010-11-26 13:06:39 +0100544 /* Get userspace string and assure termination */
Peter Huewe8504d632011-06-06 22:43:32 +0200545 err = kstrtoul_from_user(user_buf, count, 0, &user_bank);
Mattias Wallind7b9f322010-11-26 13:06:39 +0100546 if (err)
Peter Huewe8504d632011-06-06 22:43:32 +0200547 return err;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200548
Mattias Wallind7b9f322010-11-26 13:06:39 +0100549 if (user_bank >= AB8500_NUM_BANKS) {
550 dev_err(dev, "debugfs error input > number of banks\n");
551 return -EINVAL;
552 }
Mattias Wallin5814fc32010-09-13 16:05:04 +0200553
Mattias Wallind7b9f322010-11-26 13:06:39 +0100554 debug_bank = user_bank;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200555
Peter Huewe8504d632011-06-06 22:43:32 +0200556 return count;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200557}
558
559static int ab8500_address_print(struct seq_file *s, void *p)
560{
Mattias Wallind7b9f322010-11-26 13:06:39 +0100561 return seq_printf(s, "0x%02X\n", debug_address);
Mattias Wallin5814fc32010-09-13 16:05:04 +0200562}
563
564static int ab8500_address_open(struct inode *inode, struct file *file)
565{
Mattias Wallind7b9f322010-11-26 13:06:39 +0100566 return single_open(file, ab8500_address_print, inode->i_private);
Mattias Wallin5814fc32010-09-13 16:05:04 +0200567}
568
569static ssize_t ab8500_address_write(struct file *file,
Mattias Wallind7b9f322010-11-26 13:06:39 +0100570 const char __user *user_buf,
571 size_t count, loff_t *ppos)
Mattias Wallin5814fc32010-09-13 16:05:04 +0200572{
Mattias Wallind7b9f322010-11-26 13:06:39 +0100573 struct device *dev = ((struct seq_file *)(file->private_data))->private;
Mattias Wallind7b9f322010-11-26 13:06:39 +0100574 unsigned long user_address;
575 int err;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200576
Mattias Wallind7b9f322010-11-26 13:06:39 +0100577 /* Get userspace string and assure termination */
Peter Huewe8504d632011-06-06 22:43:32 +0200578 err = kstrtoul_from_user(user_buf, count, 0, &user_address);
Mattias Wallind7b9f322010-11-26 13:06:39 +0100579 if (err)
Peter Huewe8504d632011-06-06 22:43:32 +0200580 return err;
581
Mattias Wallind7b9f322010-11-26 13:06:39 +0100582 if (user_address > 0xff) {
583 dev_err(dev, "debugfs error input > 0xff\n");
584 return -EINVAL;
585 }
586 debug_address = user_address;
Peter Huewe8504d632011-06-06 22:43:32 +0200587 return count;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200588}
589
590static int ab8500_val_print(struct seq_file *s, void *p)
591{
Mattias Wallind7b9f322010-11-26 13:06:39 +0100592 struct device *dev = s->private;
593 int ret;
594 u8 regvalue;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200595
Mattias Wallind7b9f322010-11-26 13:06:39 +0100596 ret = abx500_get_register_interruptible(dev,
597 (u8)debug_bank, (u8)debug_address, &regvalue);
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 Wallin5814fc32010-09-13 16:05:04 +0200604
Mattias Wallind7b9f322010-11-26 13:06:39 +0100605 return 0;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200606}
607
608static int ab8500_val_open(struct inode *inode, struct file *file)
609{
Mattias Wallind7b9f322010-11-26 13:06:39 +0100610 return single_open(file, ab8500_val_print, inode->i_private);
Mattias Wallin5814fc32010-09-13 16:05:04 +0200611}
612
613static ssize_t ab8500_val_write(struct file *file,
Mattias Wallind7b9f322010-11-26 13:06:39 +0100614 const char __user *user_buf,
615 size_t count, loff_t *ppos)
Mattias Wallin5814fc32010-09-13 16:05:04 +0200616{
Mattias Wallind7b9f322010-11-26 13:06:39 +0100617 struct device *dev = ((struct seq_file *)(file->private_data))->private;
Mattias Wallind7b9f322010-11-26 13:06:39 +0100618 unsigned long user_val;
619 int err;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200620
Mattias Wallind7b9f322010-11-26 13:06:39 +0100621 /* Get userspace string and assure termination */
Peter Huewe8504d632011-06-06 22:43:32 +0200622 err = kstrtoul_from_user(user_buf, count, 0, &user_val);
Mattias Wallind7b9f322010-11-26 13:06:39 +0100623 if (err)
Peter Huewe8504d632011-06-06 22:43:32 +0200624 return err;
625
Mattias Wallind7b9f322010-11-26 13:06:39 +0100626 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 Wallin5814fc32010-09-13 16:05:04 +0200636
Peter Huewe8504d632011-06-06 22:43:32 +0200637 return count;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200638}
639
carriere etienne0fbce762011-04-08 16:26:36 +0200640/*
641 * - HWREG DB8500 formated routines
642 */
643static 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, &regvalue);
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
670static 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 */
682static 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 */
706static 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, &regvalue);
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
818static 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 Jones4b8ac082013-01-14 16:10:36 +0000839static 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
846static 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 Wallin0b337e72010-11-19 17:55:11 +0100854 * Userspace should use poll() on this file. When an event occur
Lee Jones4b8ac082013-01-14 16:10:36 +0000855 * the blocking poll will be released.
856 */
857static ssize_t show_irq(struct device *dev,
858 struct device_attribute *attr, char *buf)
859{
Mattias Wallin0b337e72010-11-19 17:55:11 +0100860 unsigned long name;
861 unsigned int irq_index;
862 int err;
Lee Jones4b8ac082013-01-14 16:10:36 +0000863
Mattias Wallin0b337e72010-11-19 17:55:11 +0100864 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 Jones4b8ac082013-01-14 16:10:36 +0000874
875static 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 Wallin0b337e72010-11-19 17:55:11 +0100884 unsigned int irq_index;
Lee Jones4b8ac082013-01-14 16:10:36 +0000885
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 Wallin0b337e72010-11-19 17:55:11 +0100904 irq_index = user_val - irq_first;
905 if (irq_index >= AB8500_NR_IRQS)
906 return -EINVAL;
907
Lee Jones4b8ac082013-01-14 16:10:36 +0000908 /*
Mattias Wallin0b337e72010-11-19 17:55:11 +0100909 * This will create a sysfs file named <irq-nr> which userspace can
Lee Jones4b8ac082013-01-14 16:10:36 +0000910 * use to select or poll and get the AB8500 events
911 */
Mattias Wallin0b337e72010-11-19 17:55:11 +0100912 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 Jones4b8ac082013-01-14 16:10:36 +0000921 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 Wallin0b337e72010-11-19 17:55:11 +0100932 sysfs_remove_file(&dev->kobj, &dev_attr[irq_index]->attr);
Lee Jones4b8ac082013-01-14 16:10:36 +0000933 return err;
934 }
935
936 return buf_size;
937}
938
939static 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 Wallin0b337e72010-11-19 17:55:11 +0100948 unsigned int irq_index;
Lee Jones4b8ac082013-01-14 16:10:36 +0000949
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 Wallin0b337e72010-11-19 17:55:11 +0100968 irq_index = user_val - irq_first;
969 if (irq_index >= AB8500_NR_IRQS)
970 return -EINVAL;
Lee Jones4b8ac082013-01-14 16:10:36 +0000971
Mattias Wallin0b337e72010-11-19 17:55:11 +0100972 /* 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 Jones4b8ac082013-01-14 16:10:36 +0000982
983 return buf_size;
984}
985
carriere etienne0fbce762011-04-08 16:26:36 +0200986/*
987 * - several deubgfs nodes fops
988 */
989
Mattias Wallin5814fc32010-09-13 16:05:04 +0200990static const struct file_operations ab8500_bank_fops = {
Mattias Wallind7b9f322010-11-26 13:06:39 +0100991 .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 Wallin5814fc32010-09-13 16:05:04 +0200997};
998
999static const struct file_operations ab8500_address_fops = {
Mattias Wallind7b9f322010-11-26 13:06:39 +01001000 .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 Wallin5814fc32010-09-13 16:05:04 +02001006};
1007
1008static const struct file_operations ab8500_val_fops = {
Mattias Wallind7b9f322010-11-26 13:06:39 +01001009 .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 Wallin5814fc32010-09-13 16:05:04 +02001015};
1016
Lee Jones4b8ac082013-01-14 16:10:36 +00001017static 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
1026static 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 etienne0fbce762011-04-08 16:26:36 +02001035static 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 Wallin5814fc32010-09-13 16:05:04 +02001044static struct dentry *ab8500_dir;
Mattias Wallin5814fc32010-09-13 16:05:04 +02001045
Bill Pembertonf791be42012-11-19 13:23:04 -05001046static int ab8500_debug_probe(struct platform_device *plf)
Mattias Wallin5814fc32010-09-13 16:05:04 +02001047{
carriere etienne0fbce762011-04-08 16:26:36 +02001048 struct dentry *file;
Mattias Wallind7b9f322010-11-26 13:06:39 +01001049 debug_bank = AB8500_MISC;
1050 debug_address = AB8500_REV_REG & 0x00FF;
Mattias Wallin5814fc32010-09-13 16:05:04 +02001051
Lee Jones4b8ac082013-01-14 16:10:36 +00001052 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 Wallind7b9f322010-11-26 13:06:39 +01001066 ab8500_dir = debugfs_create_dir(AB8500_NAME_STRING, NULL);
1067 if (!ab8500_dir)
carriere etienne0fbce762011-04-08 16:26:36 +02001068 goto err;
Mattias Wallin5814fc32010-09-13 16:05:04 +02001069
carriere etienne0fbce762011-04-08 16:26:36 +02001070 file = debugfs_create_file("all-bank-registers",
Mattias Wallind7b9f322010-11-26 13:06:39 +01001071 S_IRUGO, ab8500_dir, &plf->dev, &ab8500_registers_fops);
carriere etienne0fbce762011-04-08 16:26:36 +02001072 if (!file)
1073 goto err;
Mattias Wallin5814fc32010-09-13 16:05:04 +02001074
carriere etienne0fbce762011-04-08 16:26:36 +02001075 file = debugfs_create_file("register-bank",
Vasiliy Kulikov44bdcb52011-02-04 15:23:43 +03001076 (S_IRUGO | S_IWUSR), ab8500_dir, &plf->dev, &ab8500_bank_fops);
carriere etienne0fbce762011-04-08 16:26:36 +02001077 if (!file)
1078 goto err;
Mattias Wallin5814fc32010-09-13 16:05:04 +02001079
carriere etienne0fbce762011-04-08 16:26:36 +02001080 file = debugfs_create_file("register-address",
Vasiliy Kulikov44bdcb52011-02-04 15:23:43 +03001081 (S_IRUGO | S_IWUSR), ab8500_dir, &plf->dev,
Mattias Wallind7b9f322010-11-26 13:06:39 +01001082 &ab8500_address_fops);
carriere etienne0fbce762011-04-08 16:26:36 +02001083 if (!file)
1084 goto err;
Mattias Wallin5814fc32010-09-13 16:05:04 +02001085
carriere etienne0fbce762011-04-08 16:26:36 +02001086 file = debugfs_create_file("register-value",
Vasiliy Kulikov44bdcb52011-02-04 15:23:43 +03001087 (S_IRUGO | S_IWUSR), ab8500_dir, &plf->dev, &ab8500_val_fops);
carriere etienne0fbce762011-04-08 16:26:36 +02001088 if (!file)
1089 goto err;
Mattias Wallin5814fc32010-09-13 16:05:04 +02001090
carriere etienne0fbce762011-04-08 16:26:36 +02001091 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 Jones4b8ac082013-01-14 16:10:36 +00001096
carriere etienne0fbce762011-04-08 16:26:36 +02001097 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 Jones4b8ac082013-01-14 16:10:36 +00001108
Mattias Wallind7b9f322010-11-26 13:06:39 +01001109 return 0;
Mattias Wallin5814fc32010-09-13 16:05:04 +02001110
carriere etienne0fbce762011-04-08 16:26:36 +02001111err:
1112 if (ab8500_dir)
1113 debugfs_remove_recursive(ab8500_dir);
Mattias Wallind7b9f322010-11-26 13:06:39 +01001114 dev_err(&plf->dev, "failed to create debugfs entries.\n");
1115 return -ENOMEM;
Mattias Wallin5814fc32010-09-13 16:05:04 +02001116}
1117
Bill Pemberton4740f732012-11-19 13:26:01 -05001118static int ab8500_debug_remove(struct platform_device *plf)
Mattias Wallin5814fc32010-09-13 16:05:04 +02001119{
carriere etienne0fbce762011-04-08 16:26:36 +02001120 debugfs_remove_recursive(ab8500_dir);
Mattias Wallind7b9f322010-11-26 13:06:39 +01001121 return 0;
Mattias Wallin5814fc32010-09-13 16:05:04 +02001122}
1123
1124static struct platform_driver ab8500_debug_driver = {
Mattias Wallind7b9f322010-11-26 13:06:39 +01001125 .driver = {
1126 .name = "ab8500-debug",
1127 .owner = THIS_MODULE,
1128 },
1129 .probe = ab8500_debug_probe,
Bill Pemberton84449212012-11-19 13:20:24 -05001130 .remove = ab8500_debug_remove
Mattias Wallin5814fc32010-09-13 16:05:04 +02001131};
1132
1133static int __init ab8500_debug_init(void)
1134{
Mattias Wallind7b9f322010-11-26 13:06:39 +01001135 return platform_driver_register(&ab8500_debug_driver);
Mattias Wallin5814fc32010-09-13 16:05:04 +02001136}
1137
1138static void __exit ab8500_debug_exit(void)
1139{
Mattias Wallind7b9f322010-11-26 13:06:39 +01001140 platform_driver_unregister(&ab8500_debug_driver);
Mattias Wallin5814fc32010-09-13 16:05:04 +02001141}
1142subsys_initcall(ab8500_debug_init);
1143module_exit(ab8500_debug_exit);
1144
1145MODULE_AUTHOR("Mattias WALLIN <mattias.wallin@stericsson.com");
1146MODULE_DESCRIPTION("AB8500 DEBUG");
1147MODULE_LICENSE("GPL v2");