blob: 4699fff322a06262dacc85f3fcc33fc9264891c9 [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 */
7
8#include <linux/seq_file.h>
9#include <linux/uaccess.h>
10#include <linux/fs.h>
Paul Gortmaker4e36dd32011-07-03 15:13:27 -040011#include <linux/module.h>
Mattias Wallin5814fc32010-09-13 16:05:04 +020012#include <linux/debugfs.h>
13#include <linux/platform_device.h>
Lee Jones4b8ac082013-01-14 16:10:36 +000014#include <linux/interrupt.h>
15#include <linux/kobject.h>
16#include <linux/slab.h>
Mattias Wallin5814fc32010-09-13 16:05:04 +020017
18#include <linux/mfd/abx500.h>
Linus Walleijee66e652011-12-02 14:16:33 +010019#include <linux/mfd/abx500/ab8500.h>
Mattias Wallin5814fc32010-09-13 16:05:04 +020020
21static u32 debug_bank;
22static u32 debug_address;
23
Lee Jones4b8ac082013-01-14 16:10:36 +000024static int irq_first;
25static int irq_last;
Mattias Wallin0b337e72010-11-19 17:55:11 +010026static u32 irq_count[AB8500_NR_IRQS];
27
28static struct device_attribute *dev_attr[AB8500_NR_IRQS];
29static char *event_name[AB8500_NR_IRQS];
Lee Jones4b8ac082013-01-14 16:10:36 +000030
Mattias Wallin5814fc32010-09-13 16:05:04 +020031/**
32 * struct ab8500_reg_range
33 * @first: the first address of the range
34 * @last: the last address of the range
35 * @perm: access permissions for the range
36 */
37struct ab8500_reg_range {
Mattias Wallind7b9f322010-11-26 13:06:39 +010038 u8 first;
39 u8 last;
40 u8 perm;
Mattias Wallin5814fc32010-09-13 16:05:04 +020041};
42
43/**
Lee Jones822672a2012-06-20 13:56:38 +010044 * struct ab8500_prcmu_ranges
Mattias Wallin5814fc32010-09-13 16:05:04 +020045 * @num_ranges: the number of ranges in the list
46 * @bankid: bank identifier
47 * @range: the list of register ranges
48 */
Lee Jones822672a2012-06-20 13:56:38 +010049struct ab8500_prcmu_ranges {
Mattias Wallind7b9f322010-11-26 13:06:39 +010050 u8 num_ranges;
51 u8 bankid;
52 const struct ab8500_reg_range *range;
Mattias Wallin5814fc32010-09-13 16:05:04 +020053};
54
55#define AB8500_NAME_STRING "ab8500"
56#define AB8500_NUM_BANKS 22
57
58#define AB8500_REV_REG 0x80
59
Lee Jones822672a2012-06-20 13:56:38 +010060static struct ab8500_prcmu_ranges debug_ranges[AB8500_NUM_BANKS] = {
Mattias Wallind7b9f322010-11-26 13:06:39 +010061 [0x0] = {
62 .num_ranges = 0,
63 .range = 0,
64 },
65 [AB8500_SYS_CTRL1_BLOCK] = {
66 .num_ranges = 3,
67 .range = (struct ab8500_reg_range[]) {
68 {
69 .first = 0x00,
70 .last = 0x02,
71 },
72 {
73 .first = 0x42,
74 .last = 0x42,
75 },
76 {
77 .first = 0x80,
78 .last = 0x81,
79 },
80 },
81 },
82 [AB8500_SYS_CTRL2_BLOCK] = {
83 .num_ranges = 4,
84 .range = (struct ab8500_reg_range[]) {
85 {
86 .first = 0x00,
87 .last = 0x0D,
88 },
89 {
90 .first = 0x0F,
91 .last = 0x17,
92 },
93 {
94 .first = 0x30,
95 .last = 0x30,
96 },
97 {
98 .first = 0x32,
99 .last = 0x33,
100 },
101 },
102 },
103 [AB8500_REGU_CTRL1] = {
104 .num_ranges = 3,
105 .range = (struct ab8500_reg_range[]) {
106 {
107 .first = 0x00,
108 .last = 0x00,
109 },
110 {
111 .first = 0x03,
112 .last = 0x10,
113 },
114 {
115 .first = 0x80,
116 .last = 0x84,
117 },
118 },
119 },
120 [AB8500_REGU_CTRL2] = {
121 .num_ranges = 5,
122 .range = (struct ab8500_reg_range[]) {
123 {
124 .first = 0x00,
125 .last = 0x15,
126 },
127 {
128 .first = 0x17,
129 .last = 0x19,
130 },
131 {
132 .first = 0x1B,
133 .last = 0x1D,
134 },
135 {
136 .first = 0x1F,
137 .last = 0x22,
138 },
139 {
140 .first = 0x40,
141 .last = 0x44,
142 },
143 /* 0x80-0x8B is SIM registers and should
144 * not be accessed from here */
145 },
146 },
147 [AB8500_USB] = {
148 .num_ranges = 2,
149 .range = (struct ab8500_reg_range[]) {
150 {
151 .first = 0x80,
152 .last = 0x83,
153 },
154 {
155 .first = 0x87,
156 .last = 0x8A,
157 },
158 },
159 },
160 [AB8500_TVOUT] = {
161 .num_ranges = 9,
162 .range = (struct ab8500_reg_range[]) {
163 {
164 .first = 0x00,
165 .last = 0x12,
166 },
167 {
168 .first = 0x15,
169 .last = 0x17,
170 },
171 {
172 .first = 0x19,
173 .last = 0x21,
174 },
175 {
176 .first = 0x27,
177 .last = 0x2C,
178 },
179 {
180 .first = 0x41,
181 .last = 0x41,
182 },
183 {
184 .first = 0x45,
185 .last = 0x5B,
186 },
187 {
188 .first = 0x5D,
189 .last = 0x5D,
190 },
191 {
192 .first = 0x69,
193 .last = 0x69,
194 },
195 {
196 .first = 0x80,
197 .last = 0x81,
198 },
199 },
200 },
201 [AB8500_DBI] = {
202 .num_ranges = 0,
Mark Brown87fff232010-12-13 14:06:47 +0000203 .range = NULL,
Mattias Wallind7b9f322010-11-26 13:06:39 +0100204 },
205 [AB8500_ECI_AV_ACC] = {
206 .num_ranges = 1,
207 .range = (struct ab8500_reg_range[]) {
208 {
209 .first = 0x80,
210 .last = 0x82,
211 },
212 },
213 },
214 [0x9] = {
215 .num_ranges = 0,
Mark Brown87fff232010-12-13 14:06:47 +0000216 .range = NULL,
Mattias Wallind7b9f322010-11-26 13:06:39 +0100217 },
218 [AB8500_GPADC] = {
219 .num_ranges = 1,
220 .range = (struct ab8500_reg_range[]) {
221 {
222 .first = 0x00,
223 .last = 0x08,
224 },
225 },
226 },
227 [AB8500_CHARGER] = {
228 .num_ranges = 8,
229 .range = (struct ab8500_reg_range[]) {
230 {
231 .first = 0x00,
232 .last = 0x03,
233 },
234 {
235 .first = 0x05,
236 .last = 0x05,
237 },
238 {
239 .first = 0x40,
240 .last = 0x40,
241 },
242 {
243 .first = 0x42,
244 .last = 0x42,
245 },
246 {
247 .first = 0x44,
248 .last = 0x44,
249 },
250 {
251 .first = 0x50,
252 .last = 0x55,
253 },
254 {
255 .first = 0x80,
256 .last = 0x82,
257 },
258 {
259 .first = 0xC0,
260 .last = 0xC2,
261 },
262 },
263 },
264 [AB8500_GAS_GAUGE] = {
265 .num_ranges = 3,
266 .range = (struct ab8500_reg_range[]) {
267 {
268 .first = 0x00,
269 .last = 0x00,
270 },
271 {
272 .first = 0x07,
273 .last = 0x0A,
274 },
275 {
276 .first = 0x10,
277 .last = 0x14,
278 },
279 },
280 },
281 [AB8500_AUDIO] = {
282 .num_ranges = 1,
283 .range = (struct ab8500_reg_range[]) {
284 {
285 .first = 0x00,
286 .last = 0x6F,
287 },
288 },
289 },
290 [AB8500_INTERRUPT] = {
291 .num_ranges = 0,
Mark Brown87fff232010-12-13 14:06:47 +0000292 .range = NULL,
Mattias Wallind7b9f322010-11-26 13:06:39 +0100293 },
294 [AB8500_RTC] = {
295 .num_ranges = 1,
296 .range = (struct ab8500_reg_range[]) {
297 {
298 .first = 0x00,
299 .last = 0x0F,
300 },
301 },
302 },
303 [AB8500_MISC] = {
304 .num_ranges = 8,
305 .range = (struct ab8500_reg_range[]) {
306 {
307 .first = 0x00,
308 .last = 0x05,
309 },
310 {
311 .first = 0x10,
312 .last = 0x15,
313 },
314 {
315 .first = 0x20,
316 .last = 0x25,
317 },
318 {
319 .first = 0x30,
320 .last = 0x35,
321 },
322 {
323 .first = 0x40,
324 .last = 0x45,
325 },
326 {
327 .first = 0x50,
328 .last = 0x50,
329 },
330 {
331 .first = 0x60,
332 .last = 0x67,
333 },
334 {
335 .first = 0x80,
336 .last = 0x80,
337 },
338 },
339 },
340 [0x11] = {
341 .num_ranges = 0,
Mark Brown87fff232010-12-13 14:06:47 +0000342 .range = NULL,
Mattias Wallind7b9f322010-11-26 13:06:39 +0100343 },
344 [0x12] = {
345 .num_ranges = 0,
Mark Brown87fff232010-12-13 14:06:47 +0000346 .range = NULL,
Mattias Wallind7b9f322010-11-26 13:06:39 +0100347 },
348 [0x13] = {
349 .num_ranges = 0,
Mark Brown87fff232010-12-13 14:06:47 +0000350 .range = NULL,
Mattias Wallind7b9f322010-11-26 13:06:39 +0100351 },
352 [0x14] = {
353 .num_ranges = 0,
Mark Brown87fff232010-12-13 14:06:47 +0000354 .range = NULL,
Mattias Wallind7b9f322010-11-26 13:06:39 +0100355 },
356 [AB8500_OTP_EMUL] = {
357 .num_ranges = 1,
358 .range = (struct ab8500_reg_range[]) {
359 {
360 .first = 0x01,
361 .last = 0x0F,
362 },
363 },
364 },
Mattias Wallin5814fc32010-09-13 16:05:04 +0200365};
366
Lee Jones4b8ac082013-01-14 16:10:36 +0000367static irqreturn_t ab8500_debug_handler(int irq, void *data)
368{
369 char buf[16];
370 struct kobject *kobj = (struct kobject *)data;
Mattias Wallin0b337e72010-11-19 17:55:11 +0100371 unsigned int irq_abb = irq - irq_first;
Lee Jones4b8ac082013-01-14 16:10:36 +0000372
Mattias Wallin0b337e72010-11-19 17:55:11 +0100373 if (irq_abb < AB8500_NR_IRQS)
374 irq_count[irq_abb]++;
Lee Jones4b8ac082013-01-14 16:10:36 +0000375 /*
376 * This makes it possible to use poll for events (POLLPRI | POLLERR)
Mattias Wallin0b337e72010-11-19 17:55:11 +0100377 * from userspace on sysfs file named <irq-nr>
Lee Jones4b8ac082013-01-14 16:10:36 +0000378 */
Mattias Wallin0b337e72010-11-19 17:55:11 +0100379 sprintf(buf, "%d", irq);
Lee Jones4b8ac082013-01-14 16:10:36 +0000380 sysfs_notify(kobj, NULL, buf);
381
382 return IRQ_HANDLED;
383}
384
Mattias Wallin5814fc32010-09-13 16:05:04 +0200385static int ab8500_registers_print(struct seq_file *s, void *p)
386{
Mattias Wallind7b9f322010-11-26 13:06:39 +0100387 struct device *dev = s->private;
388 unsigned int i;
389 u32 bank = debug_bank;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200390
Mattias Wallind7b9f322010-11-26 13:06:39 +0100391 seq_printf(s, AB8500_NAME_STRING " register values:\n");
Mattias Wallin5814fc32010-09-13 16:05:04 +0200392
Mattias Wallind7b9f322010-11-26 13:06:39 +0100393 seq_printf(s, " bank %u:\n", bank);
394 for (i = 0; i < debug_ranges[bank].num_ranges; i++) {
395 u32 reg;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200396
Mattias Wallind7b9f322010-11-26 13:06:39 +0100397 for (reg = debug_ranges[bank].range[i].first;
398 reg <= debug_ranges[bank].range[i].last;
399 reg++) {
400 u8 value;
401 int err;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200402
Mattias Wallind7b9f322010-11-26 13:06:39 +0100403 err = abx500_get_register_interruptible(dev,
404 (u8)bank, (u8)reg, &value);
405 if (err < 0) {
406 dev_err(dev, "ab->read fail %d\n", err);
407 return err;
408 }
Mattias Wallin5814fc32010-09-13 16:05:04 +0200409
Mattias Wallind7b9f322010-11-26 13:06:39 +0100410 err = seq_printf(s, " [%u/0x%02X]: 0x%02X\n", bank,
411 reg, value);
412 if (err < 0) {
413 dev_err(dev, "seq_printf overflow\n");
414 /* Error is not returned here since
415 * the output is wanted in any case */
416 return 0;
417 }
418 }
419 }
420 return 0;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200421}
422
423static int ab8500_registers_open(struct inode *inode, struct file *file)
424{
Mattias Wallind7b9f322010-11-26 13:06:39 +0100425 return single_open(file, ab8500_registers_print, inode->i_private);
Mattias Wallin5814fc32010-09-13 16:05:04 +0200426}
427
428static const struct file_operations ab8500_registers_fops = {
Mattias Wallind7b9f322010-11-26 13:06:39 +0100429 .open = ab8500_registers_open,
430 .read = seq_read,
431 .llseek = seq_lseek,
432 .release = single_release,
433 .owner = THIS_MODULE,
Mattias Wallin5814fc32010-09-13 16:05:04 +0200434};
435
436static int ab8500_bank_print(struct seq_file *s, void *p)
437{
Mattias Wallind7b9f322010-11-26 13:06:39 +0100438 return seq_printf(s, "%d\n", debug_bank);
Mattias Wallin5814fc32010-09-13 16:05:04 +0200439}
440
441static int ab8500_bank_open(struct inode *inode, struct file *file)
442{
Mattias Wallind7b9f322010-11-26 13:06:39 +0100443 return single_open(file, ab8500_bank_print, inode->i_private);
Mattias Wallin5814fc32010-09-13 16:05:04 +0200444}
445
446static ssize_t ab8500_bank_write(struct file *file,
Mattias Wallind7b9f322010-11-26 13:06:39 +0100447 const char __user *user_buf,
448 size_t count, loff_t *ppos)
Mattias Wallin5814fc32010-09-13 16:05:04 +0200449{
Mattias Wallind7b9f322010-11-26 13:06:39 +0100450 struct device *dev = ((struct seq_file *)(file->private_data))->private;
Mattias Wallind7b9f322010-11-26 13:06:39 +0100451 unsigned long user_bank;
452 int err;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200453
Mattias Wallind7b9f322010-11-26 13:06:39 +0100454 /* Get userspace string and assure termination */
Peter Huewe8504d632011-06-06 22:43:32 +0200455 err = kstrtoul_from_user(user_buf, count, 0, &user_bank);
Mattias Wallind7b9f322010-11-26 13:06:39 +0100456 if (err)
Peter Huewe8504d632011-06-06 22:43:32 +0200457 return err;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200458
Mattias Wallind7b9f322010-11-26 13:06:39 +0100459 if (user_bank >= AB8500_NUM_BANKS) {
460 dev_err(dev, "debugfs error input > number of banks\n");
461 return -EINVAL;
462 }
Mattias Wallin5814fc32010-09-13 16:05:04 +0200463
Mattias Wallind7b9f322010-11-26 13:06:39 +0100464 debug_bank = user_bank;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200465
Peter Huewe8504d632011-06-06 22:43:32 +0200466 return count;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200467}
468
469static int ab8500_address_print(struct seq_file *s, void *p)
470{
Mattias Wallind7b9f322010-11-26 13:06:39 +0100471 return seq_printf(s, "0x%02X\n", debug_address);
Mattias Wallin5814fc32010-09-13 16:05:04 +0200472}
473
474static int ab8500_address_open(struct inode *inode, struct file *file)
475{
Mattias Wallind7b9f322010-11-26 13:06:39 +0100476 return single_open(file, ab8500_address_print, inode->i_private);
Mattias Wallin5814fc32010-09-13 16:05:04 +0200477}
478
479static ssize_t ab8500_address_write(struct file *file,
Mattias Wallind7b9f322010-11-26 13:06:39 +0100480 const char __user *user_buf,
481 size_t count, loff_t *ppos)
Mattias Wallin5814fc32010-09-13 16:05:04 +0200482{
Mattias Wallind7b9f322010-11-26 13:06:39 +0100483 struct device *dev = ((struct seq_file *)(file->private_data))->private;
Mattias Wallind7b9f322010-11-26 13:06:39 +0100484 unsigned long user_address;
485 int err;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200486
Mattias Wallind7b9f322010-11-26 13:06:39 +0100487 /* Get userspace string and assure termination */
Peter Huewe8504d632011-06-06 22:43:32 +0200488 err = kstrtoul_from_user(user_buf, count, 0, &user_address);
Mattias Wallind7b9f322010-11-26 13:06:39 +0100489 if (err)
Peter Huewe8504d632011-06-06 22:43:32 +0200490 return err;
491
Mattias Wallind7b9f322010-11-26 13:06:39 +0100492 if (user_address > 0xff) {
493 dev_err(dev, "debugfs error input > 0xff\n");
494 return -EINVAL;
495 }
496 debug_address = user_address;
Peter Huewe8504d632011-06-06 22:43:32 +0200497 return count;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200498}
499
500static int ab8500_val_print(struct seq_file *s, void *p)
501{
Mattias Wallind7b9f322010-11-26 13:06:39 +0100502 struct device *dev = s->private;
503 int ret;
504 u8 regvalue;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200505
Mattias Wallind7b9f322010-11-26 13:06:39 +0100506 ret = abx500_get_register_interruptible(dev,
507 (u8)debug_bank, (u8)debug_address, &regvalue);
508 if (ret < 0) {
509 dev_err(dev, "abx500_get_reg fail %d, %d\n",
510 ret, __LINE__);
511 return -EINVAL;
512 }
513 seq_printf(s, "0x%02X\n", regvalue);
Mattias Wallin5814fc32010-09-13 16:05:04 +0200514
Mattias Wallind7b9f322010-11-26 13:06:39 +0100515 return 0;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200516}
517
518static int ab8500_val_open(struct inode *inode, struct file *file)
519{
Mattias Wallind7b9f322010-11-26 13:06:39 +0100520 return single_open(file, ab8500_val_print, inode->i_private);
Mattias Wallin5814fc32010-09-13 16:05:04 +0200521}
522
523static ssize_t ab8500_val_write(struct file *file,
Mattias Wallind7b9f322010-11-26 13:06:39 +0100524 const char __user *user_buf,
525 size_t count, loff_t *ppos)
Mattias Wallin5814fc32010-09-13 16:05:04 +0200526{
Mattias Wallind7b9f322010-11-26 13:06:39 +0100527 struct device *dev = ((struct seq_file *)(file->private_data))->private;
Mattias Wallind7b9f322010-11-26 13:06:39 +0100528 unsigned long user_val;
529 int err;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200530
Mattias Wallind7b9f322010-11-26 13:06:39 +0100531 /* Get userspace string and assure termination */
Peter Huewe8504d632011-06-06 22:43:32 +0200532 err = kstrtoul_from_user(user_buf, count, 0, &user_val);
Mattias Wallind7b9f322010-11-26 13:06:39 +0100533 if (err)
Peter Huewe8504d632011-06-06 22:43:32 +0200534 return err;
535
Mattias Wallind7b9f322010-11-26 13:06:39 +0100536 if (user_val > 0xff) {
537 dev_err(dev, "debugfs error input > 0xff\n");
538 return -EINVAL;
539 }
540 err = abx500_set_register_interruptible(dev,
541 (u8)debug_bank, debug_address, (u8)user_val);
542 if (err < 0) {
543 printk(KERN_ERR "abx500_set_reg failed %d, %d", err, __LINE__);
544 return -EINVAL;
545 }
Mattias Wallin5814fc32010-09-13 16:05:04 +0200546
Peter Huewe8504d632011-06-06 22:43:32 +0200547 return count;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200548}
549
Lee Jones4b8ac082013-01-14 16:10:36 +0000550static int ab8500_subscribe_unsubscribe_print(struct seq_file *s, void *p)
551{
552 seq_printf(s, "%d\n", irq_first);
553
554 return 0;
555}
556
557static int ab8500_subscribe_unsubscribe_open(struct inode *inode,
558 struct file *file)
559{
560 return single_open(file, ab8500_subscribe_unsubscribe_print,
561 inode->i_private);
562}
563
564/*
Mattias Wallin0b337e72010-11-19 17:55:11 +0100565 * Userspace should use poll() on this file. When an event occur
Lee Jones4b8ac082013-01-14 16:10:36 +0000566 * the blocking poll will be released.
567 */
568static ssize_t show_irq(struct device *dev,
569 struct device_attribute *attr, char *buf)
570{
Mattias Wallin0b337e72010-11-19 17:55:11 +0100571 unsigned long name;
572 unsigned int irq_index;
573 int err;
Lee Jones4b8ac082013-01-14 16:10:36 +0000574
Mattias Wallin0b337e72010-11-19 17:55:11 +0100575 err = strict_strtoul(attr->attr.name, 0, &name);
576 if (err)
577 return err;
578
579 irq_index = name - irq_first;
580 if (irq_index >= AB8500_NR_IRQS)
581 return -EINVAL;
582 else
583 return sprintf(buf, "%u\n", irq_count[irq_index]);
584}
Lee Jones4b8ac082013-01-14 16:10:36 +0000585
586static ssize_t ab8500_subscribe_write(struct file *file,
587 const char __user *user_buf,
588 size_t count, loff_t *ppos)
589{
590 struct device *dev = ((struct seq_file *)(file->private_data))->private;
591 char buf[32];
592 int buf_size;
593 unsigned long user_val;
594 int err;
Mattias Wallin0b337e72010-11-19 17:55:11 +0100595 unsigned int irq_index;
Lee Jones4b8ac082013-01-14 16:10:36 +0000596
597 /* Get userspace string and assure termination */
598 buf_size = min(count, (sizeof(buf)-1));
599 if (copy_from_user(buf, user_buf, buf_size))
600 return -EFAULT;
601 buf[buf_size] = 0;
602
603 err = strict_strtoul(buf, 0, &user_val);
604 if (err)
605 return -EINVAL;
606 if (user_val < irq_first) {
607 dev_err(dev, "debugfs error input < %d\n", irq_first);
608 return -EINVAL;
609 }
610 if (user_val > irq_last) {
611 dev_err(dev, "debugfs error input > %d\n", irq_last);
612 return -EINVAL;
613 }
614
Mattias Wallin0b337e72010-11-19 17:55:11 +0100615 irq_index = user_val - irq_first;
616 if (irq_index >= AB8500_NR_IRQS)
617 return -EINVAL;
618
Lee Jones4b8ac082013-01-14 16:10:36 +0000619 /*
Mattias Wallin0b337e72010-11-19 17:55:11 +0100620 * This will create a sysfs file named <irq-nr> which userspace can
Lee Jones4b8ac082013-01-14 16:10:36 +0000621 * use to select or poll and get the AB8500 events
622 */
Mattias Wallin0b337e72010-11-19 17:55:11 +0100623 dev_attr[irq_index] = kmalloc(sizeof(struct device_attribute),
624 GFP_KERNEL);
625 event_name[irq_index] = kmalloc(buf_size, GFP_KERNEL);
626 sprintf(event_name[irq_index], "%lu", user_val);
627 dev_attr[irq_index]->show = show_irq;
628 dev_attr[irq_index]->store = NULL;
629 dev_attr[irq_index]->attr.name = event_name[irq_index];
630 dev_attr[irq_index]->attr.mode = S_IRUGO;
631 err = sysfs_create_file(&dev->kobj, &dev_attr[irq_index]->attr);
Lee Jones4b8ac082013-01-14 16:10:36 +0000632 if (err < 0) {
633 printk(KERN_ERR "sysfs_create_file failed %d\n", err);
634 return err;
635 }
636
637 err = request_threaded_irq(user_val, NULL, ab8500_debug_handler,
638 IRQF_SHARED | IRQF_NO_SUSPEND,
639 "ab8500-debug", &dev->kobj);
640 if (err < 0) {
641 printk(KERN_ERR "request_threaded_irq failed %d, %lu\n",
642 err, user_val);
Mattias Wallin0b337e72010-11-19 17:55:11 +0100643 sysfs_remove_file(&dev->kobj, &dev_attr[irq_index]->attr);
Lee Jones4b8ac082013-01-14 16:10:36 +0000644 return err;
645 }
646
647 return buf_size;
648}
649
650static ssize_t ab8500_unsubscribe_write(struct file *file,
651 const char __user *user_buf,
652 size_t count, loff_t *ppos)
653{
654 struct device *dev = ((struct seq_file *)(file->private_data))->private;
655 char buf[32];
656 int buf_size;
657 unsigned long user_val;
658 int err;
Mattias Wallin0b337e72010-11-19 17:55:11 +0100659 unsigned int irq_index;
Lee Jones4b8ac082013-01-14 16:10:36 +0000660
661 /* Get userspace string and assure termination */
662 buf_size = min(count, (sizeof(buf)-1));
663 if (copy_from_user(buf, user_buf, buf_size))
664 return -EFAULT;
665 buf[buf_size] = 0;
666
667 err = strict_strtoul(buf, 0, &user_val);
668 if (err)
669 return -EINVAL;
670 if (user_val < irq_first) {
671 dev_err(dev, "debugfs error input < %d\n", irq_first);
672 return -EINVAL;
673 }
674 if (user_val > irq_last) {
675 dev_err(dev, "debugfs error input > %d\n", irq_last);
676 return -EINVAL;
677 }
678
Mattias Wallin0b337e72010-11-19 17:55:11 +0100679 irq_index = user_val - irq_first;
680 if (irq_index >= AB8500_NR_IRQS)
681 return -EINVAL;
Lee Jones4b8ac082013-01-14 16:10:36 +0000682
Mattias Wallin0b337e72010-11-19 17:55:11 +0100683 /* Set irq count to 0 when unsubscribe */
684 irq_count[irq_index] = 0;
685
686 if (dev_attr[irq_index])
687 sysfs_remove_file(&dev->kobj, &dev_attr[irq_index]->attr);
688
689
690 free_irq(user_val, &dev->kobj);
691 kfree(event_name[irq_index]);
692 kfree(dev_attr[irq_index]);
Lee Jones4b8ac082013-01-14 16:10:36 +0000693
694 return buf_size;
695}
696
Mattias Wallin5814fc32010-09-13 16:05:04 +0200697static const struct file_operations ab8500_bank_fops = {
Mattias Wallind7b9f322010-11-26 13:06:39 +0100698 .open = ab8500_bank_open,
699 .write = ab8500_bank_write,
700 .read = seq_read,
701 .llseek = seq_lseek,
702 .release = single_release,
703 .owner = THIS_MODULE,
Mattias Wallin5814fc32010-09-13 16:05:04 +0200704};
705
706static const struct file_operations ab8500_address_fops = {
Mattias Wallind7b9f322010-11-26 13:06:39 +0100707 .open = ab8500_address_open,
708 .write = ab8500_address_write,
709 .read = seq_read,
710 .llseek = seq_lseek,
711 .release = single_release,
712 .owner = THIS_MODULE,
Mattias Wallin5814fc32010-09-13 16:05:04 +0200713};
714
715static const struct file_operations ab8500_val_fops = {
Mattias Wallind7b9f322010-11-26 13:06:39 +0100716 .open = ab8500_val_open,
717 .write = ab8500_val_write,
718 .read = seq_read,
719 .llseek = seq_lseek,
720 .release = single_release,
721 .owner = THIS_MODULE,
Mattias Wallin5814fc32010-09-13 16:05:04 +0200722};
723
Lee Jones4b8ac082013-01-14 16:10:36 +0000724static const struct file_operations ab8500_subscribe_fops = {
725 .open = ab8500_subscribe_unsubscribe_open,
726 .write = ab8500_subscribe_write,
727 .read = seq_read,
728 .llseek = seq_lseek,
729 .release = single_release,
730 .owner = THIS_MODULE,
731};
732
733static const struct file_operations ab8500_unsubscribe_fops = {
734 .open = ab8500_subscribe_unsubscribe_open,
735 .write = ab8500_unsubscribe_write,
736 .read = seq_read,
737 .llseek = seq_lseek,
738 .release = single_release,
739 .owner = THIS_MODULE,
740};
741
Mattias Wallin5814fc32010-09-13 16:05:04 +0200742static struct dentry *ab8500_dir;
743static struct dentry *ab8500_reg_file;
744static struct dentry *ab8500_bank_file;
745static struct dentry *ab8500_address_file;
746static struct dentry *ab8500_val_file;
Lee Jones4b8ac082013-01-14 16:10:36 +0000747static struct dentry *ab8500_subscribe_file;
748static struct dentry *ab8500_unsubscribe_file;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200749
Bill Pembertonf791be42012-11-19 13:23:04 -0500750static int ab8500_debug_probe(struct platform_device *plf)
Mattias Wallin5814fc32010-09-13 16:05:04 +0200751{
Mattias Wallind7b9f322010-11-26 13:06:39 +0100752 debug_bank = AB8500_MISC;
753 debug_address = AB8500_REV_REG & 0x00FF;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200754
Lee Jones4b8ac082013-01-14 16:10:36 +0000755 irq_first = platform_get_irq_byname(plf, "IRQ_FIRST");
756 if (irq_first < 0) {
757 dev_err(&plf->dev, "First irq not found, err %d\n",
758 irq_first);
759 return irq_first;
760 }
761
762 irq_last = platform_get_irq_byname(plf, "IRQ_LAST");
763 if (irq_last < 0) {
764 dev_err(&plf->dev, "Last irq not found, err %d\n",
765 irq_last);
766 return irq_last;
767 }
768
Mattias Wallind7b9f322010-11-26 13:06:39 +0100769 ab8500_dir = debugfs_create_dir(AB8500_NAME_STRING, NULL);
770 if (!ab8500_dir)
771 goto exit_no_debugfs;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200772
Mattias Wallind7b9f322010-11-26 13:06:39 +0100773 ab8500_reg_file = debugfs_create_file("all-bank-registers",
774 S_IRUGO, ab8500_dir, &plf->dev, &ab8500_registers_fops);
775 if (!ab8500_reg_file)
776 goto exit_destroy_dir;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200777
Mattias Wallind7b9f322010-11-26 13:06:39 +0100778 ab8500_bank_file = debugfs_create_file("register-bank",
Vasiliy Kulikov44bdcb52011-02-04 15:23:43 +0300779 (S_IRUGO | S_IWUSR), ab8500_dir, &plf->dev, &ab8500_bank_fops);
Mattias Wallind7b9f322010-11-26 13:06:39 +0100780 if (!ab8500_bank_file)
781 goto exit_destroy_reg;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200782
Mattias Wallind7b9f322010-11-26 13:06:39 +0100783 ab8500_address_file = debugfs_create_file("register-address",
Vasiliy Kulikov44bdcb52011-02-04 15:23:43 +0300784 (S_IRUGO | S_IWUSR), ab8500_dir, &plf->dev,
Mattias Wallind7b9f322010-11-26 13:06:39 +0100785 &ab8500_address_fops);
786 if (!ab8500_address_file)
787 goto exit_destroy_bank;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200788
Mattias Wallind7b9f322010-11-26 13:06:39 +0100789 ab8500_val_file = debugfs_create_file("register-value",
Vasiliy Kulikov44bdcb52011-02-04 15:23:43 +0300790 (S_IRUGO | S_IWUSR), ab8500_dir, &plf->dev, &ab8500_val_fops);
Mattias Wallind7b9f322010-11-26 13:06:39 +0100791 if (!ab8500_val_file)
792 goto exit_destroy_address;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200793
Lee Jones4b8ac082013-01-14 16:10:36 +0000794 ab8500_subscribe_file =
795 debugfs_create_file("irq-subscribe",
796 (S_IRUGO | S_IWUGO), ab8500_dir, &plf->dev,
797 &ab8500_subscribe_fops);
798 if (!ab8500_subscribe_file)
799 goto exit_destroy_val;
800
801 ab8500_unsubscribe_file =
802 debugfs_create_file("irq-unsubscribe",
803 (S_IRUGO | S_IWUGO), ab8500_dir, &plf->dev,
804 &ab8500_unsubscribe_fops);
805 if (!ab8500_unsubscribe_file)
806 goto exit_destroy_subscribe;
807
Mattias Wallind7b9f322010-11-26 13:06:39 +0100808 return 0;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200809
Lee Jones4b8ac082013-01-14 16:10:36 +0000810exit_destroy_subscribe:
811 debugfs_remove(ab8500_subscribe_file);
812exit_destroy_val:
813 debugfs_remove(ab8500_val_file);
Mattias Wallin5814fc32010-09-13 16:05:04 +0200814exit_destroy_address:
Mattias Wallind7b9f322010-11-26 13:06:39 +0100815 debugfs_remove(ab8500_address_file);
Mattias Wallin5814fc32010-09-13 16:05:04 +0200816exit_destroy_bank:
Mattias Wallind7b9f322010-11-26 13:06:39 +0100817 debugfs_remove(ab8500_bank_file);
Mattias Wallin5814fc32010-09-13 16:05:04 +0200818exit_destroy_reg:
Mattias Wallind7b9f322010-11-26 13:06:39 +0100819 debugfs_remove(ab8500_reg_file);
Mattias Wallin5814fc32010-09-13 16:05:04 +0200820exit_destroy_dir:
Mattias Wallind7b9f322010-11-26 13:06:39 +0100821 debugfs_remove(ab8500_dir);
Mattias Wallin5814fc32010-09-13 16:05:04 +0200822exit_no_debugfs:
Mattias Wallind7b9f322010-11-26 13:06:39 +0100823 dev_err(&plf->dev, "failed to create debugfs entries.\n");
824 return -ENOMEM;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200825}
826
Bill Pemberton4740f732012-11-19 13:26:01 -0500827static int ab8500_debug_remove(struct platform_device *plf)
Mattias Wallin5814fc32010-09-13 16:05:04 +0200828{
Mattias Wallind7b9f322010-11-26 13:06:39 +0100829 debugfs_remove(ab8500_val_file);
830 debugfs_remove(ab8500_address_file);
831 debugfs_remove(ab8500_bank_file);
832 debugfs_remove(ab8500_reg_file);
833 debugfs_remove(ab8500_dir);
Mattias Wallin5814fc32010-09-13 16:05:04 +0200834
Mattias Wallind7b9f322010-11-26 13:06:39 +0100835 return 0;
Mattias Wallin5814fc32010-09-13 16:05:04 +0200836}
837
838static struct platform_driver ab8500_debug_driver = {
Mattias Wallind7b9f322010-11-26 13:06:39 +0100839 .driver = {
840 .name = "ab8500-debug",
841 .owner = THIS_MODULE,
842 },
843 .probe = ab8500_debug_probe,
Bill Pemberton84449212012-11-19 13:20:24 -0500844 .remove = ab8500_debug_remove
Mattias Wallin5814fc32010-09-13 16:05:04 +0200845};
846
847static int __init ab8500_debug_init(void)
848{
Mattias Wallind7b9f322010-11-26 13:06:39 +0100849 return platform_driver_register(&ab8500_debug_driver);
Mattias Wallin5814fc32010-09-13 16:05:04 +0200850}
851
852static void __exit ab8500_debug_exit(void)
853{
Mattias Wallind7b9f322010-11-26 13:06:39 +0100854 platform_driver_unregister(&ab8500_debug_driver);
Mattias Wallin5814fc32010-09-13 16:05:04 +0200855}
856subsys_initcall(ab8500_debug_init);
857module_exit(ab8500_debug_exit);
858
859MODULE_AUTHOR("Mattias WALLIN <mattias.wallin@stericsson.com");
860MODULE_DESCRIPTION("AB8500 DEBUG");
861MODULE_LICENSE("GPL v2");