The discovered bit in PGCCSR register indicates if the device has been
[linux-2.6/next.git] / drivers / mfd / ab3550-core.c
blob91e4d5b7fd0f36f549a9b50c4217f3310e949c1b
1 /*
2 * Copyright (C) 2007-2010 ST-Ericsson
3 * License terms: GNU General Public License (GPL) version 2
4 * Low-level core for exclusive access to the AB3550 IC on the I2C bus
5 * and some basic chip-configuration.
6 * Author: Bengt Jonsson <bengt.g.jonsson@stericsson.com>
7 * Author: Mattias Nilsson <mattias.i.nilsson@stericsson.com>
8 * Author: Mattias Wallin <mattias.wallin@stericsson.com>
9 * Author: Rickard Andersson <rickard.andersson@stericsson.com>
12 #include <linux/i2c.h>
13 #include <linux/mutex.h>
14 #include <linux/err.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
17 #include <linux/device.h>
18 #include <linux/module.h>
19 #include <linux/irq.h>
20 #include <linux/interrupt.h>
21 #include <linux/random.h>
22 #include <linux/workqueue.h>
23 #include <linux/debugfs.h>
24 #include <linux/seq_file.h>
25 #include <linux/uaccess.h>
26 #include <linux/mfd/abx500.h>
27 #include <linux/list.h>
28 #include <linux/bitops.h>
29 #include <linux/spinlock.h>
30 #include <linux/mfd/core.h>
32 #define AB3550_NAME_STRING "ab3550"
33 #define AB3550_ID_FORMAT_STRING "AB3550 %s"
34 #define AB3550_NUM_BANKS 2
35 #define AB3550_NUM_EVENT_REG 5
37 /* These are the only registers inside AB3550 used in this main file */
39 /* Chip ID register */
40 #define AB3550_CID_REG 0x20
42 /* Interrupt event registers */
43 #define AB3550_EVENT_BANK 0
44 #define AB3550_EVENT_REG 0x22
46 /* Read/write operation values. */
47 #define AB3550_PERM_RD (0x01)
48 #define AB3550_PERM_WR (0x02)
50 /* Read/write permissions. */
51 #define AB3550_PERM_RO (AB3550_PERM_RD)
52 #define AB3550_PERM_RW (AB3550_PERM_RD | AB3550_PERM_WR)
54 /**
55 * struct ab3550
56 * @access_mutex: lock out concurrent accesses to the AB registers
57 * @i2c_client: I2C client for this chip
58 * @chip_name: name of this chip variant
59 * @chip_id: 8 bit chip ID for this chip variant
60 * @mask_work: a worker for writing to mask registers
61 * @event_lock: a lock to protect the event_mask
62 * @event_mask: a local copy of the mask event registers
63 * @startup_events: a copy of the first reading of the event registers
64 * @startup_events_read: whether the first events have been read
66 struct ab3550 {
67 struct mutex access_mutex;
68 struct i2c_client *i2c_client[AB3550_NUM_BANKS];
69 char chip_name[32];
70 u8 chip_id;
71 struct work_struct mask_work;
72 spinlock_t event_lock;
73 u8 event_mask[AB3550_NUM_EVENT_REG];
74 u8 startup_events[AB3550_NUM_EVENT_REG];
75 bool startup_events_read;
76 #ifdef CONFIG_DEBUG_FS
77 unsigned int debug_bank;
78 unsigned int debug_address;
79 #endif
82 /**
83 * struct ab3550_reg_range
84 * @first: the first address of the range
85 * @last: the last address of the range
86 * @perm: access permissions for the range
88 struct ab3550_reg_range {
89 u8 first;
90 u8 last;
91 u8 perm;
94 /**
95 * struct ab3550_reg_ranges
96 * @count: the number of ranges in the list
97 * @range: the list of register ranges
99 struct ab3550_reg_ranges {
100 u8 count;
101 const struct ab3550_reg_range *range;
105 * Permissible register ranges for reading and writing per device and bank.
107 * The ranges must be listed in increasing address order, and no overlaps are
108 * allowed. It is assumed that write permission implies read permission
109 * (i.e. only RO and RW permissions should be used). Ranges with write
110 * permission must not be split up.
113 #define NO_RANGE {.count = 0, .range = NULL,}
115 static struct
116 ab3550_reg_ranges ab3550_reg_ranges[AB3550_NUM_DEVICES][AB3550_NUM_BANKS] = {
117 [AB3550_DEVID_DAC] = {
118 NO_RANGE,
120 .count = 2,
121 .range = (struct ab3550_reg_range[]) {
123 .first = 0xb0,
124 .last = 0xba,
125 .perm = AB3550_PERM_RW,
128 .first = 0xbc,
129 .last = 0xc3,
130 .perm = AB3550_PERM_RW,
135 [AB3550_DEVID_LEDS] = {
136 NO_RANGE,
138 .count = 2,
139 .range = (struct ab3550_reg_range[]) {
141 .first = 0x5a,
142 .last = 0x88,
143 .perm = AB3550_PERM_RW,
146 .first = 0x8a,
147 .last = 0xad,
148 .perm = AB3550_PERM_RW,
153 [AB3550_DEVID_POWER] = {
155 .count = 1,
156 .range = (struct ab3550_reg_range[]) {
158 .first = 0x21,
159 .last = 0x21,
160 .perm = AB3550_PERM_RO,
164 NO_RANGE,
166 [AB3550_DEVID_REGULATORS] = {
168 .count = 1,
169 .range = (struct ab3550_reg_range[]) {
171 .first = 0x69,
172 .last = 0xa3,
173 .perm = AB3550_PERM_RW,
178 .count = 1,
179 .range = (struct ab3550_reg_range[]) {
181 .first = 0x14,
182 .last = 0x16,
183 .perm = AB3550_PERM_RW,
188 [AB3550_DEVID_SIM] = {
190 .count = 1,
191 .range = (struct ab3550_reg_range[]) {
193 .first = 0x21,
194 .last = 0x21,
195 .perm = AB3550_PERM_RO,
200 .count = 1,
201 .range = (struct ab3550_reg_range[]) {
203 .first = 0x14,
204 .last = 0x17,
205 .perm = AB3550_PERM_RW,
211 [AB3550_DEVID_UART] = {
212 NO_RANGE,
213 NO_RANGE,
215 [AB3550_DEVID_RTC] = {
217 .count = 1,
218 .range = (struct ab3550_reg_range[]) {
220 .first = 0x00,
221 .last = 0x0c,
222 .perm = AB3550_PERM_RW,
226 NO_RANGE,
228 [AB3550_DEVID_CHARGER] = {
230 .count = 2,
231 .range = (struct ab3550_reg_range[]) {
233 .first = 0x10,
234 .last = 0x1a,
235 .perm = AB3550_PERM_RW,
238 .first = 0x21,
239 .last = 0x21,
240 .perm = AB3550_PERM_RO,
244 NO_RANGE,
246 [AB3550_DEVID_ADC] = {
247 NO_RANGE,
249 .count = 1,
250 .range = (struct ab3550_reg_range[]) {
252 .first = 0x20,
253 .last = 0x56,
254 .perm = AB3550_PERM_RW,
260 [AB3550_DEVID_FUELGAUGE] = {
262 .count = 1,
263 .range = (struct ab3550_reg_range[]) {
265 .first = 0x21,
266 .last = 0x21,
267 .perm = AB3550_PERM_RO,
272 .count = 1,
273 .range = (struct ab3550_reg_range[]) {
275 .first = 0x00,
276 .last = 0x0e,
277 .perm = AB3550_PERM_RW,
282 [AB3550_DEVID_VIBRATOR] = {
283 NO_RANGE,
285 .count = 1,
286 .range = (struct ab3550_reg_range[]) {
288 .first = 0x10,
289 .last = 0x13,
290 .perm = AB3550_PERM_RW,
296 [AB3550_DEVID_CODEC] = {
298 .count = 2,
299 .range = (struct ab3550_reg_range[]) {
301 .first = 0x31,
302 .last = 0x63,
303 .perm = AB3550_PERM_RW,
306 .first = 0x65,
307 .last = 0x68,
308 .perm = AB3550_PERM_RW,
312 NO_RANGE,
316 static struct mfd_cell ab3550_devs[AB3550_NUM_DEVICES] = {
317 [AB3550_DEVID_DAC] = {
318 .name = "ab3550-dac",
319 .id = AB3550_DEVID_DAC,
320 .num_resources = 0,
322 [AB3550_DEVID_LEDS] = {
323 .name = "ab3550-leds",
324 .id = AB3550_DEVID_LEDS,
326 [AB3550_DEVID_POWER] = {
327 .name = "ab3550-power",
328 .id = AB3550_DEVID_POWER,
330 [AB3550_DEVID_REGULATORS] = {
331 .name = "ab3550-regulators",
332 .id = AB3550_DEVID_REGULATORS,
334 [AB3550_DEVID_SIM] = {
335 .name = "ab3550-sim",
336 .id = AB3550_DEVID_SIM,
338 [AB3550_DEVID_UART] = {
339 .name = "ab3550-uart",
340 .id = AB3550_DEVID_UART,
342 [AB3550_DEVID_RTC] = {
343 .name = "ab3550-rtc",
344 .id = AB3550_DEVID_RTC,
346 [AB3550_DEVID_CHARGER] = {
347 .name = "ab3550-charger",
348 .id = AB3550_DEVID_CHARGER,
350 [AB3550_DEVID_ADC] = {
351 .name = "ab3550-adc",
352 .id = AB3550_DEVID_ADC,
353 .num_resources = 10,
354 .resources = (struct resource[]) {
356 .name = "TRIGGER-0",
357 .flags = IORESOURCE_IRQ,
358 .start = 16,
359 .end = 16,
362 .name = "TRIGGER-1",
363 .flags = IORESOURCE_IRQ,
364 .start = 17,
365 .end = 17,
368 .name = "TRIGGER-2",
369 .flags = IORESOURCE_IRQ,
370 .start = 18,
371 .end = 18,
374 .name = "TRIGGER-3",
375 .flags = IORESOURCE_IRQ,
376 .start = 19,
377 .end = 19,
380 .name = "TRIGGER-4",
381 .flags = IORESOURCE_IRQ,
382 .start = 20,
383 .end = 20,
386 .name = "TRIGGER-5",
387 .flags = IORESOURCE_IRQ,
388 .start = 21,
389 .end = 21,
392 .name = "TRIGGER-6",
393 .flags = IORESOURCE_IRQ,
394 .start = 22,
395 .end = 22,
398 .name = "TRIGGER-7",
399 .flags = IORESOURCE_IRQ,
400 .start = 23,
401 .end = 23,
404 .name = "TRIGGER-VBAT-TXON",
405 .flags = IORESOURCE_IRQ,
406 .start = 13,
407 .end = 13,
410 .name = "TRIGGER-VBAT",
411 .flags = IORESOURCE_IRQ,
412 .start = 12,
413 .end = 12,
417 [AB3550_DEVID_FUELGAUGE] = {
418 .name = "ab3550-fuelgauge",
419 .id = AB3550_DEVID_FUELGAUGE,
421 [AB3550_DEVID_VIBRATOR] = {
422 .name = "ab3550-vibrator",
423 .id = AB3550_DEVID_VIBRATOR,
425 [AB3550_DEVID_CODEC] = {
426 .name = "ab3550-codec",
427 .id = AB3550_DEVID_CODEC,
432 * I2C transactions with error messages.
434 static int ab3550_i2c_master_send(struct ab3550 *ab, u8 bank, u8 *data,
435 u8 count)
437 int err;
439 err = i2c_master_send(ab->i2c_client[bank], data, count);
440 if (err < 0) {
441 dev_err(&ab->i2c_client[0]->dev, "send error: %d\n", err);
442 return err;
444 return 0;
447 static int ab3550_i2c_master_recv(struct ab3550 *ab, u8 bank, u8 *data,
448 u8 count)
450 int err;
452 err = i2c_master_recv(ab->i2c_client[bank], data, count);
453 if (err < 0) {
454 dev_err(&ab->i2c_client[0]->dev, "receive error: %d\n", err);
455 return err;
457 return 0;
461 * Functionality for getting/setting register values.
463 static int get_register_interruptible(struct ab3550 *ab, u8 bank, u8 reg,
464 u8 *value)
466 int err;
468 err = mutex_lock_interruptible(&ab->access_mutex);
469 if (err)
470 return err;
472 err = ab3550_i2c_master_send(ab, bank, &reg, 1);
473 if (!err)
474 err = ab3550_i2c_master_recv(ab, bank, value, 1);
476 mutex_unlock(&ab->access_mutex);
477 return err;
480 static int get_register_page_interruptible(struct ab3550 *ab, u8 bank,
481 u8 first_reg, u8 *regvals, u8 numregs)
483 int err;
485 err = mutex_lock_interruptible(&ab->access_mutex);
486 if (err)
487 return err;
489 err = ab3550_i2c_master_send(ab, bank, &first_reg, 1);
490 if (!err)
491 err = ab3550_i2c_master_recv(ab, bank, regvals, numregs);
493 mutex_unlock(&ab->access_mutex);
494 return err;
497 static int mask_and_set_register_interruptible(struct ab3550 *ab, u8 bank,
498 u8 reg, u8 bitmask, u8 bitvalues)
500 int err = 0;
502 if (likely(bitmask)) {
503 u8 reg_bits[2] = {reg, 0};
505 err = mutex_lock_interruptible(&ab->access_mutex);
506 if (err)
507 return err;
509 if (bitmask == 0xFF) /* No need to read in this case. */
510 reg_bits[1] = bitvalues;
511 else { /* Read and modify the register value. */
512 u8 bits;
514 err = ab3550_i2c_master_send(ab, bank, &reg, 1);
515 if (err)
516 goto unlock_and_return;
517 err = ab3550_i2c_master_recv(ab, bank, &bits, 1);
518 if (err)
519 goto unlock_and_return;
520 reg_bits[1] = ((~bitmask & bits) |
521 (bitmask & bitvalues));
523 /* Write the new value. */
524 err = ab3550_i2c_master_send(ab, bank, reg_bits, 2);
525 unlock_and_return:
526 mutex_unlock(&ab->access_mutex);
528 return err;
532 * Read/write permission checking functions.
534 static bool page_write_allowed(const struct ab3550_reg_ranges *ranges,
535 u8 first_reg, u8 last_reg)
537 u8 i;
539 if (last_reg < first_reg)
540 return false;
542 for (i = 0; i < ranges->count; i++) {
543 if (first_reg < ranges->range[i].first)
544 break;
545 if ((last_reg <= ranges->range[i].last) &&
546 (ranges->range[i].perm & AB3550_PERM_WR))
547 return true;
549 return false;
552 static bool reg_write_allowed(const struct ab3550_reg_ranges *ranges, u8 reg)
554 return page_write_allowed(ranges, reg, reg);
557 static bool page_read_allowed(const struct ab3550_reg_ranges *ranges,
558 u8 first_reg, u8 last_reg)
560 u8 i;
562 if (last_reg < first_reg)
563 return false;
564 /* Find the range (if it exists in the list) that includes first_reg. */
565 for (i = 0; i < ranges->count; i++) {
566 if (first_reg < ranges->range[i].first)
567 return false;
568 if (first_reg <= ranges->range[i].last)
569 break;
571 /* Make sure that the entire range up to and including last_reg is
572 * readable. This may span several of the ranges in the list.
574 while ((i < ranges->count) &&
575 (ranges->range[i].perm & AB3550_PERM_RD)) {
576 if (last_reg <= ranges->range[i].last)
577 return true;
578 if ((++i >= ranges->count) ||
579 (ranges->range[i].first !=
580 (ranges->range[i - 1].last + 1))) {
581 break;
584 return false;
587 static bool reg_read_allowed(const struct ab3550_reg_ranges *ranges, u8 reg)
589 return page_read_allowed(ranges, reg, reg);
593 * The register access functionality.
595 static int ab3550_get_chip_id(struct device *dev)
597 struct ab3550 *ab = dev_get_drvdata(dev->parent);
598 return (int)ab->chip_id;
601 static int ab3550_mask_and_set_register_interruptible(struct device *dev,
602 u8 bank, u8 reg, u8 bitmask, u8 bitvalues)
604 struct ab3550 *ab;
605 struct platform_device *pdev = to_platform_device(dev);
607 if ((AB3550_NUM_BANKS <= bank) ||
608 !reg_write_allowed(&ab3550_reg_ranges[pdev->id][bank], reg))
609 return -EINVAL;
611 ab = dev_get_drvdata(dev->parent);
612 return mask_and_set_register_interruptible(ab, bank, reg,
613 bitmask, bitvalues);
616 static int ab3550_set_register_interruptible(struct device *dev, u8 bank,
617 u8 reg, u8 value)
619 return ab3550_mask_and_set_register_interruptible(dev, bank, reg, 0xFF,
620 value);
623 static int ab3550_get_register_interruptible(struct device *dev, u8 bank,
624 u8 reg, u8 *value)
626 struct ab3550 *ab;
627 struct platform_device *pdev = to_platform_device(dev);
629 if ((AB3550_NUM_BANKS <= bank) ||
630 !reg_read_allowed(&ab3550_reg_ranges[pdev->id][bank], reg))
631 return -EINVAL;
633 ab = dev_get_drvdata(dev->parent);
634 return get_register_interruptible(ab, bank, reg, value);
637 static int ab3550_get_register_page_interruptible(struct device *dev, u8 bank,
638 u8 first_reg, u8 *regvals, u8 numregs)
640 struct ab3550 *ab;
641 struct platform_device *pdev = to_platform_device(dev);
643 if ((AB3550_NUM_BANKS <= bank) ||
644 !page_read_allowed(&ab3550_reg_ranges[pdev->id][bank],
645 first_reg, (first_reg + numregs - 1)))
646 return -EINVAL;
648 ab = dev_get_drvdata(dev->parent);
649 return get_register_page_interruptible(ab, bank, first_reg, regvals,
650 numregs);
653 static int ab3550_event_registers_startup_state_get(struct device *dev,
654 u8 *event)
656 struct ab3550 *ab;
658 ab = dev_get_drvdata(dev->parent);
659 if (!ab->startup_events_read)
660 return -EAGAIN; /* Try again later */
662 memcpy(event, ab->startup_events, AB3550_NUM_EVENT_REG);
663 return 0;
666 static int ab3550_startup_irq_enabled(struct device *dev, unsigned int irq)
668 struct ab3550 *ab;
669 struct ab3550_platform_data *plf_data;
670 bool val;
672 ab = irq_get_chip_data(irq);
673 plf_data = ab->i2c_client[0]->dev.platform_data;
674 irq -= plf_data->irq.base;
675 val = ((ab->startup_events[irq / 8] & BIT(irq % 8)) != 0);
677 return val;
680 static struct abx500_ops ab3550_ops = {
681 .get_chip_id = ab3550_get_chip_id,
682 .get_register = ab3550_get_register_interruptible,
683 .set_register = ab3550_set_register_interruptible,
684 .get_register_page = ab3550_get_register_page_interruptible,
685 .set_register_page = NULL,
686 .mask_and_set_register = ab3550_mask_and_set_register_interruptible,
687 .event_registers_startup_state_get =
688 ab3550_event_registers_startup_state_get,
689 .startup_irq_enabled = ab3550_startup_irq_enabled,
692 static irqreturn_t ab3550_irq_handler(int irq, void *data)
694 struct ab3550 *ab = data;
695 int err;
696 unsigned int i;
697 u8 e[AB3550_NUM_EVENT_REG];
698 u8 *events;
699 unsigned long flags;
701 events = (ab->startup_events_read ? e : ab->startup_events);
703 err = get_register_page_interruptible(ab, AB3550_EVENT_BANK,
704 AB3550_EVENT_REG, events, AB3550_NUM_EVENT_REG);
705 if (err)
706 goto err_event_rd;
708 if (!ab->startup_events_read) {
709 dev_info(&ab->i2c_client[0]->dev,
710 "startup events 0x%x,0x%x,0x%x,0x%x,0x%x\n",
711 ab->startup_events[0], ab->startup_events[1],
712 ab->startup_events[2], ab->startup_events[3],
713 ab->startup_events[4]);
714 ab->startup_events_read = true;
715 goto out;
718 /* The two highest bits in event[4] are not used. */
719 events[4] &= 0x3f;
721 spin_lock_irqsave(&ab->event_lock, flags);
722 for (i = 0; i < AB3550_NUM_EVENT_REG; i++)
723 events[i] &= ~ab->event_mask[i];
724 spin_unlock_irqrestore(&ab->event_lock, flags);
726 for (i = 0; i < AB3550_NUM_EVENT_REG; i++) {
727 u8 bit;
728 u8 event_reg;
730 dev_dbg(&ab->i2c_client[0]->dev, "IRQ Event[%d]: 0x%2x\n",
731 i, events[i]);
733 event_reg = events[i];
734 for (bit = 0; event_reg; bit++, event_reg /= 2) {
735 if (event_reg % 2) {
736 unsigned int irq;
737 struct ab3550_platform_data *plf_data;
739 plf_data = ab->i2c_client[0]->dev.platform_data;
740 irq = plf_data->irq.base + (i * 8) + bit;
741 handle_nested_irq(irq);
745 out:
746 return IRQ_HANDLED;
748 err_event_rd:
749 dev_dbg(&ab->i2c_client[0]->dev, "error reading event registers\n");
750 return IRQ_HANDLED;
753 #ifdef CONFIG_DEBUG_FS
754 static struct ab3550_reg_ranges debug_ranges[AB3550_NUM_BANKS] = {
756 .count = 6,
757 .range = (struct ab3550_reg_range[]) {
759 .first = 0x00,
760 .last = 0x0e,
763 .first = 0x10,
764 .last = 0x1a,
767 .first = 0x1e,
768 .last = 0x4f,
771 .first = 0x51,
772 .last = 0x63,
775 .first = 0x65,
776 .last = 0xa3,
779 .first = 0xa5,
780 .last = 0xa8,
785 .count = 8,
786 .range = (struct ab3550_reg_range[]) {
788 .first = 0x00,
789 .last = 0x0e,
792 .first = 0x10,
793 .last = 0x17,
796 .first = 0x1a,
797 .last = 0x1c,
800 .first = 0x20,
801 .last = 0x56,
804 .first = 0x5a,
805 .last = 0x88,
808 .first = 0x8a,
809 .last = 0xad,
812 .first = 0xb0,
813 .last = 0xba,
816 .first = 0xbc,
817 .last = 0xc3,
823 static int ab3550_registers_print(struct seq_file *s, void *p)
825 struct ab3550 *ab = s->private;
826 int bank;
828 seq_printf(s, AB3550_NAME_STRING " register values:\n");
830 for (bank = 0; bank < AB3550_NUM_BANKS; bank++) {
831 unsigned int i;
833 seq_printf(s, " bank %d:\n", bank);
834 for (i = 0; i < debug_ranges[bank].count; i++) {
835 u8 reg;
837 for (reg = debug_ranges[bank].range[i].first;
838 reg <= debug_ranges[bank].range[i].last;
839 reg++) {
840 u8 value;
842 get_register_interruptible(ab, bank, reg,
843 &value);
844 seq_printf(s, " [%d/0x%02X]: 0x%02X\n", bank,
845 reg, value);
849 return 0;
852 static int ab3550_registers_open(struct inode *inode, struct file *file)
854 return single_open(file, ab3550_registers_print, inode->i_private);
857 static const struct file_operations ab3550_registers_fops = {
858 .open = ab3550_registers_open,
859 .read = seq_read,
860 .llseek = seq_lseek,
861 .release = single_release,
862 .owner = THIS_MODULE,
865 static int ab3550_bank_print(struct seq_file *s, void *p)
867 struct ab3550 *ab = s->private;
869 seq_printf(s, "%d\n", ab->debug_bank);
870 return 0;
873 static int ab3550_bank_open(struct inode *inode, struct file *file)
875 return single_open(file, ab3550_bank_print, inode->i_private);
878 static ssize_t ab3550_bank_write(struct file *file,
879 const char __user *user_buf,
880 size_t count, loff_t *ppos)
882 struct ab3550 *ab = ((struct seq_file *)(file->private_data))->private;
883 unsigned long user_bank;
884 int err;
886 /* Get userspace string and assure termination */
887 err = kstrtoul_from_user(user_buf, count, 0, &user_bank);
888 if (err)
889 return err;
891 if (user_bank >= AB3550_NUM_BANKS) {
892 dev_err(&ab->i2c_client[0]->dev,
893 "debugfs error input > number of banks\n");
894 return -EINVAL;
897 ab->debug_bank = user_bank;
899 return count;
902 static int ab3550_address_print(struct seq_file *s, void *p)
904 struct ab3550 *ab = s->private;
906 seq_printf(s, "0x%02X\n", ab->debug_address);
907 return 0;
910 static int ab3550_address_open(struct inode *inode, struct file *file)
912 return single_open(file, ab3550_address_print, inode->i_private);
915 static ssize_t ab3550_address_write(struct file *file,
916 const char __user *user_buf,
917 size_t count, loff_t *ppos)
919 struct ab3550 *ab = ((struct seq_file *)(file->private_data))->private;
920 unsigned long user_address;
921 int err;
923 /* Get userspace string and assure termination */
924 err = kstrtoul_from_user(user_buf, count, 0, &user_address);
925 if (err)
926 return err;
928 if (user_address > 0xff) {
929 dev_err(&ab->i2c_client[0]->dev,
930 "debugfs error input > 0xff\n");
931 return -EINVAL;
933 ab->debug_address = user_address;
934 return count;
937 static int ab3550_val_print(struct seq_file *s, void *p)
939 struct ab3550 *ab = s->private;
940 int err;
941 u8 regvalue;
943 err = get_register_interruptible(ab, (u8)ab->debug_bank,
944 (u8)ab->debug_address, &regvalue);
945 if (err)
946 return -EINVAL;
947 seq_printf(s, "0x%02X\n", regvalue);
949 return 0;
952 static int ab3550_val_open(struct inode *inode, struct file *file)
954 return single_open(file, ab3550_val_print, inode->i_private);
957 static ssize_t ab3550_val_write(struct file *file,
958 const char __user *user_buf,
959 size_t count, loff_t *ppos)
961 struct ab3550 *ab = ((struct seq_file *)(file->private_data))->private;
962 unsigned long user_val;
963 int err;
964 u8 regvalue;
966 /* Get userspace string and assure termination */
967 err = kstrtoul_from_user(user_buf, count, 0, &user_val);
968 if (err)
969 return err;
971 if (user_val > 0xff) {
972 dev_err(&ab->i2c_client[0]->dev,
973 "debugfs error input > 0xff\n");
974 return -EINVAL;
976 err = mask_and_set_register_interruptible(
977 ab, (u8)ab->debug_bank,
978 (u8)ab->debug_address, 0xFF, (u8)user_val);
979 if (err)
980 return -EINVAL;
982 get_register_interruptible(ab, (u8)ab->debug_bank,
983 (u8)ab->debug_address, &regvalue);
984 if (err)
985 return -EINVAL;
987 return count;
990 static const struct file_operations ab3550_bank_fops = {
991 .open = ab3550_bank_open,
992 .write = ab3550_bank_write,
993 .read = seq_read,
994 .llseek = seq_lseek,
995 .release = single_release,
996 .owner = THIS_MODULE,
999 static const struct file_operations ab3550_address_fops = {
1000 .open = ab3550_address_open,
1001 .write = ab3550_address_write,
1002 .read = seq_read,
1003 .llseek = seq_lseek,
1004 .release = single_release,
1005 .owner = THIS_MODULE,
1008 static const struct file_operations ab3550_val_fops = {
1009 .open = ab3550_val_open,
1010 .write = ab3550_val_write,
1011 .read = seq_read,
1012 .llseek = seq_lseek,
1013 .release = single_release,
1014 .owner = THIS_MODULE,
1017 static struct dentry *ab3550_dir;
1018 static struct dentry *ab3550_reg_file;
1019 static struct dentry *ab3550_bank_file;
1020 static struct dentry *ab3550_address_file;
1021 static struct dentry *ab3550_val_file;
1023 static inline void ab3550_setup_debugfs(struct ab3550 *ab)
1025 ab->debug_bank = 0;
1026 ab->debug_address = 0x00;
1028 ab3550_dir = debugfs_create_dir(AB3550_NAME_STRING, NULL);
1029 if (!ab3550_dir)
1030 goto exit_no_debugfs;
1032 ab3550_reg_file = debugfs_create_file("all-registers",
1033 S_IRUGO, ab3550_dir, ab, &ab3550_registers_fops);
1034 if (!ab3550_reg_file)
1035 goto exit_destroy_dir;
1037 ab3550_bank_file = debugfs_create_file("register-bank",
1038 (S_IRUGO | S_IWUSR), ab3550_dir, ab, &ab3550_bank_fops);
1039 if (!ab3550_bank_file)
1040 goto exit_destroy_reg;
1042 ab3550_address_file = debugfs_create_file("register-address",
1043 (S_IRUGO | S_IWUSR), ab3550_dir, ab, &ab3550_address_fops);
1044 if (!ab3550_address_file)
1045 goto exit_destroy_bank;
1047 ab3550_val_file = debugfs_create_file("register-value",
1048 (S_IRUGO | S_IWUSR), ab3550_dir, ab, &ab3550_val_fops);
1049 if (!ab3550_val_file)
1050 goto exit_destroy_address;
1052 return;
1054 exit_destroy_address:
1055 debugfs_remove(ab3550_address_file);
1056 exit_destroy_bank:
1057 debugfs_remove(ab3550_bank_file);
1058 exit_destroy_reg:
1059 debugfs_remove(ab3550_reg_file);
1060 exit_destroy_dir:
1061 debugfs_remove(ab3550_dir);
1062 exit_no_debugfs:
1063 dev_err(&ab->i2c_client[0]->dev, "failed to create debugfs entries.\n");
1064 return;
1067 static inline void ab3550_remove_debugfs(void)
1069 debugfs_remove(ab3550_val_file);
1070 debugfs_remove(ab3550_address_file);
1071 debugfs_remove(ab3550_bank_file);
1072 debugfs_remove(ab3550_reg_file);
1073 debugfs_remove(ab3550_dir);
1076 #else /* !CONFIG_DEBUG_FS */
1077 static inline void ab3550_setup_debugfs(struct ab3550 *ab)
1080 static inline void ab3550_remove_debugfs(void)
1083 #endif
1086 * Basic set-up, datastructure creation/destruction and I2C interface.
1087 * This sets up a default config in the AB3550 chip so that it
1088 * will work as expected.
1090 static int __devinit ab3550_setup(struct ab3550 *ab)
1092 int err = 0;
1093 int i;
1094 struct ab3550_platform_data *plf_data;
1095 struct abx500_init_settings *settings;
1097 plf_data = ab->i2c_client[0]->dev.platform_data;
1098 settings = plf_data->init_settings;
1100 for (i = 0; i < plf_data->init_settings_sz; i++) {
1101 err = mask_and_set_register_interruptible(ab,
1102 settings[i].bank,
1103 settings[i].reg,
1104 0xFF, settings[i].setting);
1105 if (err)
1106 goto exit_no_setup;
1108 /* If event mask register update the event mask in ab3550 */
1109 if ((settings[i].bank == 0) &&
1110 (AB3550_IMR1 <= settings[i].reg) &&
1111 (settings[i].reg <= AB3550_IMR5)) {
1112 ab->event_mask[settings[i].reg - AB3550_IMR1] =
1113 settings[i].setting;
1116 exit_no_setup:
1117 return err;
1120 static void ab3550_mask_work(struct work_struct *work)
1122 struct ab3550 *ab = container_of(work, struct ab3550, mask_work);
1123 int i;
1124 unsigned long flags;
1125 u8 mask[AB3550_NUM_EVENT_REG];
1127 spin_lock_irqsave(&ab->event_lock, flags);
1128 for (i = 0; i < AB3550_NUM_EVENT_REG; i++)
1129 mask[i] = ab->event_mask[i];
1130 spin_unlock_irqrestore(&ab->event_lock, flags);
1132 for (i = 0; i < AB3550_NUM_EVENT_REG; i++) {
1133 int err;
1135 err = mask_and_set_register_interruptible(ab, 0,
1136 (AB3550_IMR1 + i), ~0, mask[i]);
1137 if (err)
1138 dev_err(&ab->i2c_client[0]->dev,
1139 "ab3550_mask_work failed 0x%x,0x%x\n",
1140 (AB3550_IMR1 + i), mask[i]);
1144 static void ab3550_mask(struct irq_data *data)
1146 unsigned long flags;
1147 struct ab3550 *ab;
1148 struct ab3550_platform_data *plf_data;
1149 int irq;
1151 ab = irq_data_get_irq_chip_data(data);
1152 plf_data = ab->i2c_client[0]->dev.platform_data;
1153 irq = data->irq - plf_data->irq.base;
1155 spin_lock_irqsave(&ab->event_lock, flags);
1156 ab->event_mask[irq / 8] |= BIT(irq % 8);
1157 spin_unlock_irqrestore(&ab->event_lock, flags);
1159 schedule_work(&ab->mask_work);
1162 static void ab3550_unmask(struct irq_data *data)
1164 unsigned long flags;
1165 struct ab3550 *ab;
1166 struct ab3550_platform_data *plf_data;
1167 int irq;
1169 ab = irq_data_get_irq_chip_data(data);
1170 plf_data = ab->i2c_client[0]->dev.platform_data;
1171 irq = data->irq - plf_data->irq.base;
1173 spin_lock_irqsave(&ab->event_lock, flags);
1174 ab->event_mask[irq / 8] &= ~BIT(irq % 8);
1175 spin_unlock_irqrestore(&ab->event_lock, flags);
1177 schedule_work(&ab->mask_work);
1180 static void noop(struct irq_data *data)
1184 static struct irq_chip ab3550_irq_chip = {
1185 .name = "ab3550-core", /* Keep the same name as the request */
1186 .irq_disable = ab3550_mask, /* No default to mask in chip.c */
1187 .irq_ack = noop,
1188 .irq_mask = ab3550_mask,
1189 .irq_unmask = ab3550_unmask,
1192 struct ab_family_id {
1193 u8 id;
1194 char *name;
1197 static const struct ab_family_id ids[] __devinitconst = {
1198 /* AB3550 */
1200 .id = AB3550_P1A,
1201 .name = "P1A"
1203 /* Terminator */
1205 .id = 0x00,
1209 static int __devinit ab3550_probe(struct i2c_client *client,
1210 const struct i2c_device_id *id)
1212 struct ab3550 *ab;
1213 struct ab3550_platform_data *ab3550_plf_data =
1214 client->dev.platform_data;
1215 int err;
1216 int i;
1217 int num_i2c_clients = 0;
1219 ab = kzalloc(sizeof(struct ab3550), GFP_KERNEL);
1220 if (!ab) {
1221 dev_err(&client->dev,
1222 "could not allocate " AB3550_NAME_STRING " device\n");
1223 return -ENOMEM;
1226 /* Initialize data structure */
1227 mutex_init(&ab->access_mutex);
1228 spin_lock_init(&ab->event_lock);
1229 ab->i2c_client[0] = client;
1231 i2c_set_clientdata(client, ab);
1233 /* Read chip ID register */
1234 err = get_register_interruptible(ab, 0, AB3550_CID_REG, &ab->chip_id);
1235 if (err) {
1236 dev_err(&client->dev, "could not communicate with the analog "
1237 "baseband chip\n");
1238 goto exit_no_detect;
1241 for (i = 0; ids[i].id != 0x0; i++) {
1242 if (ids[i].id == ab->chip_id) {
1243 snprintf(&ab->chip_name[0], sizeof(ab->chip_name) - 1,
1244 AB3550_ID_FORMAT_STRING, ids[i].name);
1245 break;
1249 if (ids[i].id == 0x0) {
1250 dev_err(&client->dev, "unknown analog baseband chip id: 0x%x\n",
1251 ab->chip_id);
1252 dev_err(&client->dev, "driver not started!\n");
1253 goto exit_no_detect;
1256 dev_info(&client->dev, "detected AB chip: %s\n", &ab->chip_name[0]);
1258 /* Attach other dummy I2C clients. */
1259 while (++num_i2c_clients < AB3550_NUM_BANKS) {
1260 ab->i2c_client[num_i2c_clients] =
1261 i2c_new_dummy(client->adapter,
1262 (client->addr + num_i2c_clients));
1263 if (!ab->i2c_client[num_i2c_clients]) {
1264 err = -ENOMEM;
1265 goto exit_no_dummy_client;
1267 strlcpy(ab->i2c_client[num_i2c_clients]->name, id->name,
1268 sizeof(ab->i2c_client[num_i2c_clients]->name));
1271 err = ab3550_setup(ab);
1272 if (err)
1273 goto exit_no_setup;
1275 INIT_WORK(&ab->mask_work, ab3550_mask_work);
1277 for (i = 0; i < ab3550_plf_data->irq.count; i++) {
1278 unsigned int irq;
1280 irq = ab3550_plf_data->irq.base + i;
1281 irq_set_chip_data(irq, ab);
1282 irq_set_chip_and_handler(irq, &ab3550_irq_chip,
1283 handle_simple_irq);
1284 irq_set_nested_thread(irq, 1);
1285 #ifdef CONFIG_ARM
1286 set_irq_flags(irq, IRQF_VALID);
1287 #else
1288 irq_set_noprobe(irq);
1289 #endif
1292 err = request_threaded_irq(client->irq, NULL, ab3550_irq_handler,
1293 IRQF_ONESHOT, "ab3550-core", ab);
1294 /* This real unpredictable IRQ is of course sampled for entropy */
1295 rand_initialize_irq(client->irq);
1297 if (err)
1298 goto exit_no_irq;
1300 err = abx500_register_ops(&client->dev, &ab3550_ops);
1301 if (err)
1302 goto exit_no_ops;
1304 /* Set up and register the platform devices. */
1305 for (i = 0; i < AB3550_NUM_DEVICES; i++) {
1306 ab3550_devs[i].platform_data = ab3550_plf_data->dev_data[i];
1307 ab3550_devs[i].pdata_size = ab3550_plf_data->dev_data_sz[i];
1310 err = mfd_add_devices(&client->dev, 0, ab3550_devs,
1311 ARRAY_SIZE(ab3550_devs), NULL,
1312 ab3550_plf_data->irq.base);
1314 ab3550_setup_debugfs(ab);
1316 return 0;
1318 exit_no_ops:
1319 exit_no_irq:
1320 exit_no_setup:
1321 exit_no_dummy_client:
1322 /* Unregister the dummy i2c clients. */
1323 while (--num_i2c_clients)
1324 i2c_unregister_device(ab->i2c_client[num_i2c_clients]);
1325 exit_no_detect:
1326 kfree(ab);
1327 return err;
1330 static int __devexit ab3550_remove(struct i2c_client *client)
1332 struct ab3550 *ab = i2c_get_clientdata(client);
1333 int num_i2c_clients = AB3550_NUM_BANKS;
1335 mfd_remove_devices(&client->dev);
1336 ab3550_remove_debugfs();
1338 while (--num_i2c_clients)
1339 i2c_unregister_device(ab->i2c_client[num_i2c_clients]);
1342 * At this point, all subscribers should have unregistered
1343 * their notifiers so deactivate IRQ
1345 free_irq(client->irq, ab);
1346 kfree(ab);
1347 return 0;
1350 static const struct i2c_device_id ab3550_id[] = {
1351 {AB3550_NAME_STRING, 0},
1354 MODULE_DEVICE_TABLE(i2c, ab3550_id);
1356 static struct i2c_driver ab3550_driver = {
1357 .driver = {
1358 .name = AB3550_NAME_STRING,
1359 .owner = THIS_MODULE,
1361 .id_table = ab3550_id,
1362 .probe = ab3550_probe,
1363 .remove = __devexit_p(ab3550_remove),
1366 static int __init ab3550_i2c_init(void)
1368 return i2c_add_driver(&ab3550_driver);
1371 static void __exit ab3550_i2c_exit(void)
1373 i2c_del_driver(&ab3550_driver);
1376 subsys_initcall(ab3550_i2c_init);
1377 module_exit(ab3550_i2c_exit);
1379 MODULE_AUTHOR("Mattias Wallin <mattias.wallin@stericsson.com>");
1380 MODULE_DESCRIPTION("AB3550 core driver");
1381 MODULE_LICENSE("GPL");