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)
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
67 struct mutex access_mutex
;
68 struct i2c_client
*i2c_client
[AB3550_NUM_BANKS
];
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
;
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
{
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
{
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,}
116 ab3550_reg_ranges ab3550_reg_ranges
[AB3550_NUM_DEVICES
][AB3550_NUM_BANKS
] = {
117 [AB3550_DEVID_DAC
] = {
121 .range
= (struct ab3550_reg_range
[]) {
125 .perm
= AB3550_PERM_RW
,
130 .perm
= AB3550_PERM_RW
,
135 [AB3550_DEVID_LEDS
] = {
139 .range
= (struct ab3550_reg_range
[]) {
143 .perm
= AB3550_PERM_RW
,
148 .perm
= AB3550_PERM_RW
,
153 [AB3550_DEVID_POWER
] = {
156 .range
= (struct ab3550_reg_range
[]) {
160 .perm
= AB3550_PERM_RO
,
166 [AB3550_DEVID_REGULATORS
] = {
169 .range
= (struct ab3550_reg_range
[]) {
173 .perm
= AB3550_PERM_RW
,
179 .range
= (struct ab3550_reg_range
[]) {
183 .perm
= AB3550_PERM_RW
,
188 [AB3550_DEVID_SIM
] = {
191 .range
= (struct ab3550_reg_range
[]) {
195 .perm
= AB3550_PERM_RO
,
201 .range
= (struct ab3550_reg_range
[]) {
205 .perm
= AB3550_PERM_RW
,
211 [AB3550_DEVID_UART
] = {
215 [AB3550_DEVID_RTC
] = {
218 .range
= (struct ab3550_reg_range
[]) {
222 .perm
= AB3550_PERM_RW
,
228 [AB3550_DEVID_CHARGER
] = {
231 .range
= (struct ab3550_reg_range
[]) {
235 .perm
= AB3550_PERM_RW
,
240 .perm
= AB3550_PERM_RO
,
246 [AB3550_DEVID_ADC
] = {
250 .range
= (struct ab3550_reg_range
[]) {
254 .perm
= AB3550_PERM_RW
,
260 [AB3550_DEVID_FUELGAUGE
] = {
263 .range
= (struct ab3550_reg_range
[]) {
267 .perm
= AB3550_PERM_RO
,
273 .range
= (struct ab3550_reg_range
[]) {
277 .perm
= AB3550_PERM_RW
,
282 [AB3550_DEVID_VIBRATOR
] = {
286 .range
= (struct ab3550_reg_range
[]) {
290 .perm
= AB3550_PERM_RW
,
296 [AB3550_DEVID_CODEC
] = {
299 .range
= (struct ab3550_reg_range
[]) {
303 .perm
= AB3550_PERM_RW
,
308 .perm
= AB3550_PERM_RW
,
316 static struct mfd_cell ab3550_devs
[AB3550_NUM_DEVICES
] = {
317 [AB3550_DEVID_DAC
] = {
318 .name
= "ab3550-dac",
319 .id
= AB3550_DEVID_DAC
,
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
,
354 .resources
= (struct resource
[]) {
357 .flags
= IORESOURCE_IRQ
,
363 .flags
= IORESOURCE_IRQ
,
369 .flags
= IORESOURCE_IRQ
,
375 .flags
= IORESOURCE_IRQ
,
381 .flags
= IORESOURCE_IRQ
,
387 .flags
= IORESOURCE_IRQ
,
393 .flags
= IORESOURCE_IRQ
,
399 .flags
= IORESOURCE_IRQ
,
404 .name
= "TRIGGER-VBAT-TXON",
405 .flags
= IORESOURCE_IRQ
,
410 .name
= "TRIGGER-VBAT",
411 .flags
= IORESOURCE_IRQ
,
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
,
439 err
= i2c_master_send(ab
->i2c_client
[bank
], data
, count
);
441 dev_err(&ab
->i2c_client
[0]->dev
, "send error: %d\n", err
);
447 static int ab3550_i2c_master_recv(struct ab3550
*ab
, u8 bank
, u8
*data
,
452 err
= i2c_master_recv(ab
->i2c_client
[bank
], data
, count
);
454 dev_err(&ab
->i2c_client
[0]->dev
, "receive error: %d\n", err
);
461 * Functionality for getting/setting register values.
463 static int get_register_interruptible(struct ab3550
*ab
, u8 bank
, u8 reg
,
468 err
= mutex_lock_interruptible(&ab
->access_mutex
);
472 err
= ab3550_i2c_master_send(ab
, bank
, ®
, 1);
474 err
= ab3550_i2c_master_recv(ab
, bank
, value
, 1);
476 mutex_unlock(&ab
->access_mutex
);
480 static int get_register_page_interruptible(struct ab3550
*ab
, u8 bank
,
481 u8 first_reg
, u8
*regvals
, u8 numregs
)
485 err
= mutex_lock_interruptible(&ab
->access_mutex
);
489 err
= ab3550_i2c_master_send(ab
, bank
, &first_reg
, 1);
491 err
= ab3550_i2c_master_recv(ab
, bank
, regvals
, numregs
);
493 mutex_unlock(&ab
->access_mutex
);
497 static int mask_and_set_register_interruptible(struct ab3550
*ab
, u8 bank
,
498 u8 reg
, u8 bitmask
, u8 bitvalues
)
502 if (likely(bitmask
)) {
503 u8 reg_bits
[2] = {reg
, 0};
505 err
= mutex_lock_interruptible(&ab
->access_mutex
);
509 if (bitmask
== 0xFF) /* No need to read in this case. */
510 reg_bits
[1] = bitvalues
;
511 else { /* Read and modify the register value. */
514 err
= ab3550_i2c_master_send(ab
, bank
, ®
, 1);
516 goto unlock_and_return
;
517 err
= ab3550_i2c_master_recv(ab
, bank
, &bits
, 1);
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);
526 mutex_unlock(&ab
->access_mutex
);
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
)
539 if (last_reg
< first_reg
)
542 for (i
= 0; i
< ranges
->count
; i
++) {
543 if (first_reg
< ranges
->range
[i
].first
)
545 if ((last_reg
<= ranges
->range
[i
].last
) &&
546 (ranges
->range
[i
].perm
& AB3550_PERM_WR
))
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
)
562 if (last_reg
< first_reg
)
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
)
568 if (first_reg
<= ranges
->range
[i
].last
)
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
)
578 if ((++i
>= ranges
->count
) ||
579 (ranges
->range
[i
].first
!=
580 (ranges
->range
[i
- 1].last
+ 1))) {
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
)
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
))
611 ab
= dev_get_drvdata(dev
->parent
);
612 return mask_and_set_register_interruptible(ab
, bank
, reg
,
616 static int ab3550_set_register_interruptible(struct device
*dev
, u8 bank
,
619 return ab3550_mask_and_set_register_interruptible(dev
, bank
, reg
, 0xFF,
623 static int ab3550_get_register_interruptible(struct device
*dev
, u8 bank
,
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
))
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
)
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)))
648 ab
= dev_get_drvdata(dev
->parent
);
649 return get_register_page_interruptible(ab
, bank
, first_reg
, regvals
,
653 static int ab3550_event_registers_startup_state_get(struct device
*dev
,
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
);
666 static int ab3550_startup_irq_enabled(struct device
*dev
, unsigned int irq
)
669 struct ab3550_platform_data
*plf_data
;
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);
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
;
697 u8 e
[AB3550_NUM_EVENT_REG
];
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
);
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;
718 /* The two highest bits in event[4] are not used. */
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
++) {
730 dev_dbg(&ab
->i2c_client
[0]->dev
, "IRQ Event[%d]: 0x%2x\n",
733 event_reg
= events
[i
];
734 for (bit
= 0; event_reg
; bit
++, event_reg
/= 2) {
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
);
749 dev_dbg(&ab
->i2c_client
[0]->dev
, "error reading event registers\n");
753 #ifdef CONFIG_DEBUG_FS
754 static struct ab3550_reg_ranges debug_ranges
[AB3550_NUM_BANKS
] = {
757 .range
= (struct ab3550_reg_range
[]) {
786 .range
= (struct ab3550_reg_range
[]) {
823 static int ab3550_registers_print(struct seq_file
*s
, void *p
)
825 struct ab3550
*ab
= s
->private;
828 seq_printf(s
, AB3550_NAME_STRING
" register values:\n");
830 for (bank
= 0; bank
< AB3550_NUM_BANKS
; bank
++) {
833 seq_printf(s
, " bank %d:\n", bank
);
834 for (i
= 0; i
< debug_ranges
[bank
].count
; i
++) {
837 for (reg
= debug_ranges
[bank
].range
[i
].first
;
838 reg
<= debug_ranges
[bank
].range
[i
].last
;
842 get_register_interruptible(ab
, bank
, reg
,
844 seq_printf(s
, " [%d/0x%02X]: 0x%02X\n", bank
,
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
,
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
);
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
;
886 /* Get userspace string and assure termination */
887 err
= kstrtoul_from_user(user_buf
, count
, 0, &user_bank
);
891 if (user_bank
>= AB3550_NUM_BANKS
) {
892 dev_err(&ab
->i2c_client
[0]->dev
,
893 "debugfs error input > number of banks\n");
897 ab
->debug_bank
= user_bank
;
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
);
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
;
923 /* Get userspace string and assure termination */
924 err
= kstrtoul_from_user(user_buf
, count
, 0, &user_address
);
928 if (user_address
> 0xff) {
929 dev_err(&ab
->i2c_client
[0]->dev
,
930 "debugfs error input > 0xff\n");
933 ab
->debug_address
= user_address
;
937 static int ab3550_val_print(struct seq_file
*s
, void *p
)
939 struct ab3550
*ab
= s
->private;
943 err
= get_register_interruptible(ab
, (u8
)ab
->debug_bank
,
944 (u8
)ab
->debug_address
, ®value
);
947 seq_printf(s
, "0x%02X\n", regvalue
);
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
;
966 /* Get userspace string and assure termination */
967 err
= kstrtoul_from_user(user_buf
, count
, 0, &user_val
);
971 if (user_val
> 0xff) {
972 dev_err(&ab
->i2c_client
[0]->dev
,
973 "debugfs error input > 0xff\n");
976 err
= mask_and_set_register_interruptible(
977 ab
, (u8
)ab
->debug_bank
,
978 (u8
)ab
->debug_address
, 0xFF, (u8
)user_val
);
982 get_register_interruptible(ab
, (u8
)ab
->debug_bank
,
983 (u8
)ab
->debug_address
, ®value
);
990 static const struct file_operations ab3550_bank_fops
= {
991 .open
= ab3550_bank_open
,
992 .write
= ab3550_bank_write
,
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
,
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
,
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
)
1026 ab
->debug_address
= 0x00;
1028 ab3550_dir
= debugfs_create_dir(AB3550_NAME_STRING
, NULL
);
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
;
1054 exit_destroy_address
:
1055 debugfs_remove(ab3550_address_file
);
1057 debugfs_remove(ab3550_bank_file
);
1059 debugfs_remove(ab3550_reg_file
);
1061 debugfs_remove(ab3550_dir
);
1063 dev_err(&ab
->i2c_client
[0]->dev
, "failed to create debugfs entries.\n");
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)
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
)
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
,
1104 0xFF, settings
[i
].setting
);
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
;
1120 static void ab3550_mask_work(struct work_struct
*work
)
1122 struct ab3550
*ab
= container_of(work
, struct ab3550
, mask_work
);
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
++) {
1135 err
= mask_and_set_register_interruptible(ab
, 0,
1136 (AB3550_IMR1
+ i
), ~0, mask
[i
]);
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
;
1148 struct ab3550_platform_data
*plf_data
;
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
;
1166 struct ab3550_platform_data
*plf_data
;
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 */
1188 .irq_mask
= ab3550_mask
,
1189 .irq_unmask
= ab3550_unmask
,
1192 struct ab_family_id
{
1197 static const struct ab_family_id ids
[] __devinitconst
= {
1209 static int __devinit
ab3550_probe(struct i2c_client
*client
,
1210 const struct i2c_device_id
*id
)
1213 struct ab3550_platform_data
*ab3550_plf_data
=
1214 client
->dev
.platform_data
;
1217 int num_i2c_clients
= 0;
1219 ab
= kzalloc(sizeof(struct ab3550
), GFP_KERNEL
);
1221 dev_err(&client
->dev
,
1222 "could not allocate " AB3550_NAME_STRING
" device\n");
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
);
1236 dev_err(&client
->dev
, "could not communicate with the analog "
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
);
1249 if (ids
[i
].id
== 0x0) {
1250 dev_err(&client
->dev
, "unknown analog baseband chip id: 0x%x\n",
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
]) {
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
);
1275 INIT_WORK(&ab
->mask_work
, ab3550_mask_work
);
1277 for (i
= 0; i
< ab3550_plf_data
->irq
.count
; i
++) {
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
,
1284 irq_set_nested_thread(irq
, 1);
1286 set_irq_flags(irq
, IRQF_VALID
);
1288 irq_set_noprobe(irq
);
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
);
1300 err
= abx500_register_ops(&client
->dev
, &ab3550_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
);
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
]);
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
);
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
= {
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");