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/irq.h>
19 #include <linux/interrupt.h>
20 #include <linux/random.h>
21 #include <linux/workqueue.h>
22 #include <linux/debugfs.h>
23 #include <linux/seq_file.h>
24 #include <linux/uaccess.h>
25 #include <linux/mfd/abx500.h>
26 #include <linux/list.h>
27 #include <linux/bitops.h>
28 #include <linux/spinlock.h>
29 #include <linux/mfd/core.h>
31 #define AB3550_NAME_STRING "ab3550"
32 #define AB3550_ID_FORMAT_STRING "AB3550 %s"
33 #define AB3550_NUM_BANKS 2
34 #define AB3550_NUM_EVENT_REG 5
36 /* These are the only registers inside AB3550 used in this main file */
38 /* Chip ID register */
39 #define AB3550_CID_REG 0x20
41 /* Interrupt event registers */
42 #define AB3550_EVENT_BANK 0
43 #define AB3550_EVENT_REG 0x22
45 /* Read/write operation values. */
46 #define AB3550_PERM_RD (0x01)
47 #define AB3550_PERM_WR (0x02)
49 /* Read/write permissions. */
50 #define AB3550_PERM_RO (AB3550_PERM_RD)
51 #define AB3550_PERM_RW (AB3550_PERM_RD | AB3550_PERM_WR)
55 * @access_mutex: lock out concurrent accesses to the AB registers
56 * @i2c_client: I2C client for this chip
57 * @chip_name: name of this chip variant
58 * @chip_id: 8 bit chip ID for this chip variant
59 * @mask_work: a worker for writing to mask registers
60 * @event_lock: a lock to protect the event_mask
61 * @event_mask: a local copy of the mask event registers
62 * @startup_events: a copy of the first reading of the event registers
63 * @startup_events_read: whether the first events have been read
66 struct mutex access_mutex
;
67 struct i2c_client
*i2c_client
[AB3550_NUM_BANKS
];
70 struct work_struct mask_work
;
71 spinlock_t event_lock
;
72 u8 event_mask
[AB3550_NUM_EVENT_REG
];
73 u8 startup_events
[AB3550_NUM_EVENT_REG
];
74 bool startup_events_read
;
75 #ifdef CONFIG_DEBUG_FS
76 unsigned int debug_bank
;
77 unsigned int debug_address
;
82 * struct ab3550_reg_range
83 * @first: the first address of the range
84 * @last: the last address of the range
85 * @perm: access permissions for the range
87 struct ab3550_reg_range
{
94 * struct ab3550_reg_ranges
95 * @count: the number of ranges in the list
96 * @range: the list of register ranges
98 struct ab3550_reg_ranges
{
100 const struct ab3550_reg_range
*range
;
104 * Permissible register ranges for reading and writing per device and bank.
106 * The ranges must be listed in increasing address order, and no overlaps are
107 * allowed. It is assumed that write permission implies read permission
108 * (i.e. only RO and RW permissions should be used). Ranges with write
109 * permission must not be split up.
112 #define NO_RANGE {.count = 0, .range = NULL,}
115 ab3550_reg_ranges ab3550_reg_ranges
[AB3550_NUM_DEVICES
][AB3550_NUM_BANKS
] = {
116 [AB3550_DEVID_DAC
] = {
120 .range
= (struct ab3550_reg_range
[]) {
124 .perm
= AB3550_PERM_RW
,
129 .perm
= AB3550_PERM_RW
,
134 [AB3550_DEVID_LEDS
] = {
138 .range
= (struct ab3550_reg_range
[]) {
142 .perm
= AB3550_PERM_RW
,
147 .perm
= AB3550_PERM_RW
,
152 [AB3550_DEVID_POWER
] = {
155 .range
= (struct ab3550_reg_range
[]) {
159 .perm
= AB3550_PERM_RO
,
165 [AB3550_DEVID_REGULATORS
] = {
168 .range
= (struct ab3550_reg_range
[]) {
172 .perm
= AB3550_PERM_RW
,
178 .range
= (struct ab3550_reg_range
[]) {
182 .perm
= AB3550_PERM_RW
,
187 [AB3550_DEVID_SIM
] = {
190 .range
= (struct ab3550_reg_range
[]) {
194 .perm
= AB3550_PERM_RO
,
200 .range
= (struct ab3550_reg_range
[]) {
204 .perm
= AB3550_PERM_RW
,
210 [AB3550_DEVID_UART
] = {
214 [AB3550_DEVID_RTC
] = {
217 .range
= (struct ab3550_reg_range
[]) {
221 .perm
= AB3550_PERM_RW
,
227 [AB3550_DEVID_CHARGER
] = {
230 .range
= (struct ab3550_reg_range
[]) {
234 .perm
= AB3550_PERM_RW
,
239 .perm
= AB3550_PERM_RO
,
245 [AB3550_DEVID_ADC
] = {
249 .range
= (struct ab3550_reg_range
[]) {
253 .perm
= AB3550_PERM_RW
,
259 [AB3550_DEVID_FUELGAUGE
] = {
262 .range
= (struct ab3550_reg_range
[]) {
266 .perm
= AB3550_PERM_RO
,
272 .range
= (struct ab3550_reg_range
[]) {
276 .perm
= AB3550_PERM_RW
,
281 [AB3550_DEVID_VIBRATOR
] = {
285 .range
= (struct ab3550_reg_range
[]) {
289 .perm
= AB3550_PERM_RW
,
295 [AB3550_DEVID_CODEC
] = {
298 .range
= (struct ab3550_reg_range
[]) {
302 .perm
= AB3550_PERM_RW
,
307 .perm
= AB3550_PERM_RW
,
315 static struct mfd_cell ab3550_devs
[AB3550_NUM_DEVICES
] = {
316 [AB3550_DEVID_DAC
] = {
317 .name
= "ab3550-dac",
318 .id
= AB3550_DEVID_DAC
,
321 [AB3550_DEVID_LEDS
] = {
322 .name
= "ab3550-leds",
323 .id
= AB3550_DEVID_LEDS
,
325 [AB3550_DEVID_POWER
] = {
326 .name
= "ab3550-power",
327 .id
= AB3550_DEVID_POWER
,
329 [AB3550_DEVID_REGULATORS
] = {
330 .name
= "ab3550-regulators",
331 .id
= AB3550_DEVID_REGULATORS
,
333 [AB3550_DEVID_SIM
] = {
334 .name
= "ab3550-sim",
335 .id
= AB3550_DEVID_SIM
,
337 [AB3550_DEVID_UART
] = {
338 .name
= "ab3550-uart",
339 .id
= AB3550_DEVID_UART
,
341 [AB3550_DEVID_RTC
] = {
342 .name
= "ab3550-rtc",
343 .id
= AB3550_DEVID_RTC
,
345 [AB3550_DEVID_CHARGER
] = {
346 .name
= "ab3550-charger",
347 .id
= AB3550_DEVID_CHARGER
,
349 [AB3550_DEVID_ADC
] = {
350 .name
= "ab3550-adc",
351 .id
= AB3550_DEVID_ADC
,
353 .resources
= (struct resource
[]) {
356 .flags
= IORESOURCE_IRQ
,
362 .flags
= IORESOURCE_IRQ
,
368 .flags
= IORESOURCE_IRQ
,
374 .flags
= IORESOURCE_IRQ
,
380 .flags
= IORESOURCE_IRQ
,
386 .flags
= IORESOURCE_IRQ
,
392 .flags
= IORESOURCE_IRQ
,
398 .flags
= IORESOURCE_IRQ
,
403 .name
= "TRIGGER-VBAT-TXON",
404 .flags
= IORESOURCE_IRQ
,
409 .name
= "TRIGGER-VBAT",
410 .flags
= IORESOURCE_IRQ
,
416 [AB3550_DEVID_FUELGAUGE
] = {
417 .name
= "ab3550-fuelgauge",
418 .id
= AB3550_DEVID_FUELGAUGE
,
420 [AB3550_DEVID_VIBRATOR
] = {
421 .name
= "ab3550-vibrator",
422 .id
= AB3550_DEVID_VIBRATOR
,
424 [AB3550_DEVID_CODEC
] = {
425 .name
= "ab3550-codec",
426 .id
= AB3550_DEVID_CODEC
,
431 * I2C transactions with error messages.
433 static int ab3550_i2c_master_send(struct ab3550
*ab
, u8 bank
, u8
*data
,
438 err
= i2c_master_send(ab
->i2c_client
[bank
], data
, count
);
440 dev_err(&ab
->i2c_client
[0]->dev
, "send error: %d\n", err
);
446 static int ab3550_i2c_master_recv(struct ab3550
*ab
, u8 bank
, u8
*data
,
451 err
= i2c_master_recv(ab
->i2c_client
[bank
], data
, count
);
453 dev_err(&ab
->i2c_client
[0]->dev
, "receive error: %d\n", err
);
460 * Functionality for getting/setting register values.
462 static int get_register_interruptible(struct ab3550
*ab
, u8 bank
, u8 reg
,
467 err
= mutex_lock_interruptible(&ab
->access_mutex
);
471 err
= ab3550_i2c_master_send(ab
, bank
, ®
, 1);
473 err
= ab3550_i2c_master_recv(ab
, bank
, value
, 1);
475 mutex_unlock(&ab
->access_mutex
);
479 static int get_register_page_interruptible(struct ab3550
*ab
, u8 bank
,
480 u8 first_reg
, u8
*regvals
, u8 numregs
)
484 err
= mutex_lock_interruptible(&ab
->access_mutex
);
488 err
= ab3550_i2c_master_send(ab
, bank
, &first_reg
, 1);
490 err
= ab3550_i2c_master_recv(ab
, bank
, regvals
, numregs
);
492 mutex_unlock(&ab
->access_mutex
);
496 static int mask_and_set_register_interruptible(struct ab3550
*ab
, u8 bank
,
497 u8 reg
, u8 bitmask
, u8 bitvalues
)
501 if (likely(bitmask
)) {
502 u8 reg_bits
[2] = {reg
, 0};
504 err
= mutex_lock_interruptible(&ab
->access_mutex
);
508 if (bitmask
== 0xFF) /* No need to read in this case. */
509 reg_bits
[1] = bitvalues
;
510 else { /* Read and modify the register value. */
513 err
= ab3550_i2c_master_send(ab
, bank
, ®
, 1);
515 goto unlock_and_return
;
516 err
= ab3550_i2c_master_recv(ab
, bank
, &bits
, 1);
518 goto unlock_and_return
;
519 reg_bits
[1] = ((~bitmask
& bits
) |
520 (bitmask
& bitvalues
));
522 /* Write the new value. */
523 err
= ab3550_i2c_master_send(ab
, bank
, reg_bits
, 2);
525 mutex_unlock(&ab
->access_mutex
);
531 * Read/write permission checking functions.
533 static bool page_write_allowed(const struct ab3550_reg_ranges
*ranges
,
534 u8 first_reg
, u8 last_reg
)
538 if (last_reg
< first_reg
)
541 for (i
= 0; i
< ranges
->count
; i
++) {
542 if (first_reg
< ranges
->range
[i
].first
)
544 if ((last_reg
<= ranges
->range
[i
].last
) &&
545 (ranges
->range
[i
].perm
& AB3550_PERM_WR
))
551 static bool reg_write_allowed(const struct ab3550_reg_ranges
*ranges
, u8 reg
)
553 return page_write_allowed(ranges
, reg
, reg
);
556 static bool page_read_allowed(const struct ab3550_reg_ranges
*ranges
,
557 u8 first_reg
, u8 last_reg
)
561 if (last_reg
< first_reg
)
563 /* Find the range (if it exists in the list) that includes first_reg. */
564 for (i
= 0; i
< ranges
->count
; i
++) {
565 if (first_reg
< ranges
->range
[i
].first
)
567 if (first_reg
<= ranges
->range
[i
].last
)
570 /* Make sure that the entire range up to and including last_reg is
571 * readable. This may span several of the ranges in the list.
573 while ((i
< ranges
->count
) &&
574 (ranges
->range
[i
].perm
& AB3550_PERM_RD
)) {
575 if (last_reg
<= ranges
->range
[i
].last
)
577 if ((++i
>= ranges
->count
) ||
578 (ranges
->range
[i
].first
!=
579 (ranges
->range
[i
- 1].last
+ 1))) {
586 static bool reg_read_allowed(const struct ab3550_reg_ranges
*ranges
, u8 reg
)
588 return page_read_allowed(ranges
, reg
, reg
);
592 * The register access functionality.
594 static int ab3550_get_chip_id(struct device
*dev
)
596 struct ab3550
*ab
= dev_get_drvdata(dev
->parent
);
597 return (int)ab
->chip_id
;
600 static int ab3550_mask_and_set_register_interruptible(struct device
*dev
,
601 u8 bank
, u8 reg
, u8 bitmask
, u8 bitvalues
)
604 struct platform_device
*pdev
= to_platform_device(dev
);
606 if ((AB3550_NUM_BANKS
<= bank
) ||
607 !reg_write_allowed(&ab3550_reg_ranges
[pdev
->id
][bank
], reg
))
610 ab
= dev_get_drvdata(dev
->parent
);
611 return mask_and_set_register_interruptible(ab
, bank
, reg
,
615 static int ab3550_set_register_interruptible(struct device
*dev
, u8 bank
,
618 return ab3550_mask_and_set_register_interruptible(dev
, bank
, reg
, 0xFF,
622 static int ab3550_get_register_interruptible(struct device
*dev
, u8 bank
,
626 struct platform_device
*pdev
= to_platform_device(dev
);
628 if ((AB3550_NUM_BANKS
<= bank
) ||
629 !reg_read_allowed(&ab3550_reg_ranges
[pdev
->id
][bank
], reg
))
632 ab
= dev_get_drvdata(dev
->parent
);
633 return get_register_interruptible(ab
, bank
, reg
, value
);
636 static int ab3550_get_register_page_interruptible(struct device
*dev
, u8 bank
,
637 u8 first_reg
, u8
*regvals
, u8 numregs
)
640 struct platform_device
*pdev
= to_platform_device(dev
);
642 if ((AB3550_NUM_BANKS
<= bank
) ||
643 !page_read_allowed(&ab3550_reg_ranges
[pdev
->id
][bank
],
644 first_reg
, (first_reg
+ numregs
- 1)))
647 ab
= dev_get_drvdata(dev
->parent
);
648 return get_register_page_interruptible(ab
, bank
, first_reg
, regvals
,
652 static int ab3550_event_registers_startup_state_get(struct device
*dev
,
657 ab
= dev_get_drvdata(dev
->parent
);
658 if (!ab
->startup_events_read
)
659 return -EAGAIN
; /* Try again later */
661 memcpy(event
, ab
->startup_events
, AB3550_NUM_EVENT_REG
);
665 static int ab3550_startup_irq_enabled(struct device
*dev
, unsigned int irq
)
668 struct ab3550_platform_data
*plf_data
;
671 ab
= irq_get_chip_data(irq
);
672 plf_data
= ab
->i2c_client
[0]->dev
.platform_data
;
673 irq
-= plf_data
->irq
.base
;
674 val
= ((ab
->startup_events
[irq
/ 8] & BIT(irq
% 8)) != 0);
679 static struct abx500_ops ab3550_ops
= {
680 .get_chip_id
= ab3550_get_chip_id
,
681 .get_register
= ab3550_get_register_interruptible
,
682 .set_register
= ab3550_set_register_interruptible
,
683 .get_register_page
= ab3550_get_register_page_interruptible
,
684 .set_register_page
= NULL
,
685 .mask_and_set_register
= ab3550_mask_and_set_register_interruptible
,
686 .event_registers_startup_state_get
=
687 ab3550_event_registers_startup_state_get
,
688 .startup_irq_enabled
= ab3550_startup_irq_enabled
,
691 static irqreturn_t
ab3550_irq_handler(int irq
, void *data
)
693 struct ab3550
*ab
= data
;
696 u8 e
[AB3550_NUM_EVENT_REG
];
700 events
= (ab
->startup_events_read
? e
: ab
->startup_events
);
702 err
= get_register_page_interruptible(ab
, AB3550_EVENT_BANK
,
703 AB3550_EVENT_REG
, events
, AB3550_NUM_EVENT_REG
);
707 if (!ab
->startup_events_read
) {
708 dev_info(&ab
->i2c_client
[0]->dev
,
709 "startup events 0x%x,0x%x,0x%x,0x%x,0x%x\n",
710 ab
->startup_events
[0], ab
->startup_events
[1],
711 ab
->startup_events
[2], ab
->startup_events
[3],
712 ab
->startup_events
[4]);
713 ab
->startup_events_read
= true;
717 /* The two highest bits in event[4] are not used. */
720 spin_lock_irqsave(&ab
->event_lock
, flags
);
721 for (i
= 0; i
< AB3550_NUM_EVENT_REG
; i
++)
722 events
[i
] &= ~ab
->event_mask
[i
];
723 spin_unlock_irqrestore(&ab
->event_lock
, flags
);
725 for (i
= 0; i
< AB3550_NUM_EVENT_REG
; i
++) {
729 dev_dbg(&ab
->i2c_client
[0]->dev
, "IRQ Event[%d]: 0x%2x\n",
732 event_reg
= events
[i
];
733 for (bit
= 0; event_reg
; bit
++, event_reg
/= 2) {
736 struct ab3550_platform_data
*plf_data
;
738 plf_data
= ab
->i2c_client
[0]->dev
.platform_data
;
739 irq
= plf_data
->irq
.base
+ (i
* 8) + bit
;
740 handle_nested_irq(irq
);
748 dev_dbg(&ab
->i2c_client
[0]->dev
, "error reading event registers\n");
752 #ifdef CONFIG_DEBUG_FS
753 static struct ab3550_reg_ranges debug_ranges
[AB3550_NUM_BANKS
] = {
756 .range
= (struct ab3550_reg_range
[]) {
785 .range
= (struct ab3550_reg_range
[]) {
822 static int ab3550_registers_print(struct seq_file
*s
, void *p
)
824 struct ab3550
*ab
= s
->private;
827 seq_printf(s
, AB3550_NAME_STRING
" register values:\n");
829 for (bank
= 0; bank
< AB3550_NUM_BANKS
; bank
++) {
832 seq_printf(s
, " bank %d:\n", bank
);
833 for (i
= 0; i
< debug_ranges
[bank
].count
; i
++) {
836 for (reg
= debug_ranges
[bank
].range
[i
].first
;
837 reg
<= debug_ranges
[bank
].range
[i
].last
;
841 get_register_interruptible(ab
, bank
, reg
,
843 seq_printf(s
, " [%d/0x%02X]: 0x%02X\n", bank
,
851 static int ab3550_registers_open(struct inode
*inode
, struct file
*file
)
853 return single_open(file
, ab3550_registers_print
, inode
->i_private
);
856 static const struct file_operations ab3550_registers_fops
= {
857 .open
= ab3550_registers_open
,
860 .release
= single_release
,
861 .owner
= THIS_MODULE
,
864 static int ab3550_bank_print(struct seq_file
*s
, void *p
)
866 struct ab3550
*ab
= s
->private;
868 seq_printf(s
, "%d\n", ab
->debug_bank
);
872 static int ab3550_bank_open(struct inode
*inode
, struct file
*file
)
874 return single_open(file
, ab3550_bank_print
, inode
->i_private
);
877 static ssize_t
ab3550_bank_write(struct file
*file
,
878 const char __user
*user_buf
,
879 size_t count
, loff_t
*ppos
)
881 struct ab3550
*ab
= ((struct seq_file
*)(file
->private_data
))->private;
882 unsigned long user_bank
;
885 /* Get userspace string and assure termination */
886 err
= kstrtoul_from_user(user_buf
, count
, 0, &user_bank
);
890 if (user_bank
>= AB3550_NUM_BANKS
) {
891 dev_err(&ab
->i2c_client
[0]->dev
,
892 "debugfs error input > number of banks\n");
896 ab
->debug_bank
= user_bank
;
901 static int ab3550_address_print(struct seq_file
*s
, void *p
)
903 struct ab3550
*ab
= s
->private;
905 seq_printf(s
, "0x%02X\n", ab
->debug_address
);
909 static int ab3550_address_open(struct inode
*inode
, struct file
*file
)
911 return single_open(file
, ab3550_address_print
, inode
->i_private
);
914 static ssize_t
ab3550_address_write(struct file
*file
,
915 const char __user
*user_buf
,
916 size_t count
, loff_t
*ppos
)
918 struct ab3550
*ab
= ((struct seq_file
*)(file
->private_data
))->private;
919 unsigned long user_address
;
922 /* Get userspace string and assure termination */
923 err
= kstrtoul_from_user(user_buf
, count
, 0, &user_address
);
927 if (user_address
> 0xff) {
928 dev_err(&ab
->i2c_client
[0]->dev
,
929 "debugfs error input > 0xff\n");
932 ab
->debug_address
= user_address
;
936 static int ab3550_val_print(struct seq_file
*s
, void *p
)
938 struct ab3550
*ab
= s
->private;
942 err
= get_register_interruptible(ab
, (u8
)ab
->debug_bank
,
943 (u8
)ab
->debug_address
, ®value
);
946 seq_printf(s
, "0x%02X\n", regvalue
);
951 static int ab3550_val_open(struct inode
*inode
, struct file
*file
)
953 return single_open(file
, ab3550_val_print
, inode
->i_private
);
956 static ssize_t
ab3550_val_write(struct file
*file
,
957 const char __user
*user_buf
,
958 size_t count
, loff_t
*ppos
)
960 struct ab3550
*ab
= ((struct seq_file
*)(file
->private_data
))->private;
961 unsigned long user_val
;
965 /* Get userspace string and assure termination */
966 err
= kstrtoul_from_user(user_buf
, count
, 0, &user_val
);
970 if (user_val
> 0xff) {
971 dev_err(&ab
->i2c_client
[0]->dev
,
972 "debugfs error input > 0xff\n");
975 err
= mask_and_set_register_interruptible(
976 ab
, (u8
)ab
->debug_bank
,
977 (u8
)ab
->debug_address
, 0xFF, (u8
)user_val
);
981 get_register_interruptible(ab
, (u8
)ab
->debug_bank
,
982 (u8
)ab
->debug_address
, ®value
);
989 static const struct file_operations ab3550_bank_fops
= {
990 .open
= ab3550_bank_open
,
991 .write
= ab3550_bank_write
,
994 .release
= single_release
,
995 .owner
= THIS_MODULE
,
998 static const struct file_operations ab3550_address_fops
= {
999 .open
= ab3550_address_open
,
1000 .write
= ab3550_address_write
,
1002 .llseek
= seq_lseek
,
1003 .release
= single_release
,
1004 .owner
= THIS_MODULE
,
1007 static const struct file_operations ab3550_val_fops
= {
1008 .open
= ab3550_val_open
,
1009 .write
= ab3550_val_write
,
1011 .llseek
= seq_lseek
,
1012 .release
= single_release
,
1013 .owner
= THIS_MODULE
,
1016 static struct dentry
*ab3550_dir
;
1017 static struct dentry
*ab3550_reg_file
;
1018 static struct dentry
*ab3550_bank_file
;
1019 static struct dentry
*ab3550_address_file
;
1020 static struct dentry
*ab3550_val_file
;
1022 static inline void ab3550_setup_debugfs(struct ab3550
*ab
)
1025 ab
->debug_address
= 0x00;
1027 ab3550_dir
= debugfs_create_dir(AB3550_NAME_STRING
, NULL
);
1029 goto exit_no_debugfs
;
1031 ab3550_reg_file
= debugfs_create_file("all-registers",
1032 S_IRUGO
, ab3550_dir
, ab
, &ab3550_registers_fops
);
1033 if (!ab3550_reg_file
)
1034 goto exit_destroy_dir
;
1036 ab3550_bank_file
= debugfs_create_file("register-bank",
1037 (S_IRUGO
| S_IWUSR
), ab3550_dir
, ab
, &ab3550_bank_fops
);
1038 if (!ab3550_bank_file
)
1039 goto exit_destroy_reg
;
1041 ab3550_address_file
= debugfs_create_file("register-address",
1042 (S_IRUGO
| S_IWUSR
), ab3550_dir
, ab
, &ab3550_address_fops
);
1043 if (!ab3550_address_file
)
1044 goto exit_destroy_bank
;
1046 ab3550_val_file
= debugfs_create_file("register-value",
1047 (S_IRUGO
| S_IWUSR
), ab3550_dir
, ab
, &ab3550_val_fops
);
1048 if (!ab3550_val_file
)
1049 goto exit_destroy_address
;
1053 exit_destroy_address
:
1054 debugfs_remove(ab3550_address_file
);
1056 debugfs_remove(ab3550_bank_file
);
1058 debugfs_remove(ab3550_reg_file
);
1060 debugfs_remove(ab3550_dir
);
1062 dev_err(&ab
->i2c_client
[0]->dev
, "failed to create debugfs entries.\n");
1066 static inline void ab3550_remove_debugfs(void)
1068 debugfs_remove(ab3550_val_file
);
1069 debugfs_remove(ab3550_address_file
);
1070 debugfs_remove(ab3550_bank_file
);
1071 debugfs_remove(ab3550_reg_file
);
1072 debugfs_remove(ab3550_dir
);
1075 #else /* !CONFIG_DEBUG_FS */
1076 static inline void ab3550_setup_debugfs(struct ab3550
*ab
)
1079 static inline void ab3550_remove_debugfs(void)
1085 * Basic set-up, datastructure creation/destruction and I2C interface.
1086 * This sets up a default config in the AB3550 chip so that it
1087 * will work as expected.
1089 static int __init
ab3550_setup(struct ab3550
*ab
)
1093 struct ab3550_platform_data
*plf_data
;
1094 struct abx500_init_settings
*settings
;
1096 plf_data
= ab
->i2c_client
[0]->dev
.platform_data
;
1097 settings
= plf_data
->init_settings
;
1099 for (i
= 0; i
< plf_data
->init_settings_sz
; i
++) {
1100 err
= mask_and_set_register_interruptible(ab
,
1103 0xFF, settings
[i
].setting
);
1107 /* If event mask register update the event mask in ab3550 */
1108 if ((settings
[i
].bank
== 0) &&
1109 (AB3550_IMR1
<= settings
[i
].reg
) &&
1110 (settings
[i
].reg
<= AB3550_IMR5
)) {
1111 ab
->event_mask
[settings
[i
].reg
- AB3550_IMR1
] =
1112 settings
[i
].setting
;
1119 static void ab3550_mask_work(struct work_struct
*work
)
1121 struct ab3550
*ab
= container_of(work
, struct ab3550
, mask_work
);
1123 unsigned long flags
;
1124 u8 mask
[AB3550_NUM_EVENT_REG
];
1126 spin_lock_irqsave(&ab
->event_lock
, flags
);
1127 for (i
= 0; i
< AB3550_NUM_EVENT_REG
; i
++)
1128 mask
[i
] = ab
->event_mask
[i
];
1129 spin_unlock_irqrestore(&ab
->event_lock
, flags
);
1131 for (i
= 0; i
< AB3550_NUM_EVENT_REG
; i
++) {
1134 err
= mask_and_set_register_interruptible(ab
, 0,
1135 (AB3550_IMR1
+ i
), ~0, mask
[i
]);
1137 dev_err(&ab
->i2c_client
[0]->dev
,
1138 "ab3550_mask_work failed 0x%x,0x%x\n",
1139 (AB3550_IMR1
+ i
), mask
[i
]);
1143 static void ab3550_mask(struct irq_data
*data
)
1145 unsigned long flags
;
1147 struct ab3550_platform_data
*plf_data
;
1150 ab
= irq_data_get_irq_chip_data(data
);
1151 plf_data
= ab
->i2c_client
[0]->dev
.platform_data
;
1152 irq
= data
->irq
- plf_data
->irq
.base
;
1154 spin_lock_irqsave(&ab
->event_lock
, flags
);
1155 ab
->event_mask
[irq
/ 8] |= BIT(irq
% 8);
1156 spin_unlock_irqrestore(&ab
->event_lock
, flags
);
1158 schedule_work(&ab
->mask_work
);
1161 static void ab3550_unmask(struct irq_data
*data
)
1163 unsigned long flags
;
1165 struct ab3550_platform_data
*plf_data
;
1168 ab
= irq_data_get_irq_chip_data(data
);
1169 plf_data
= ab
->i2c_client
[0]->dev
.platform_data
;
1170 irq
= data
->irq
- plf_data
->irq
.base
;
1172 spin_lock_irqsave(&ab
->event_lock
, flags
);
1173 ab
->event_mask
[irq
/ 8] &= ~BIT(irq
% 8);
1174 spin_unlock_irqrestore(&ab
->event_lock
, flags
);
1176 schedule_work(&ab
->mask_work
);
1179 static void noop(struct irq_data
*data
)
1183 static struct irq_chip ab3550_irq_chip
= {
1184 .name
= "ab3550-core", /* Keep the same name as the request */
1185 .irq_disable
= ab3550_mask
, /* No default to mask in chip.c */
1187 .irq_mask
= ab3550_mask
,
1188 .irq_unmask
= ab3550_unmask
,
1191 struct ab_family_id
{
1196 static const struct ab_family_id ids
[] __initdata
= {
1208 static int __init
ab3550_probe(struct i2c_client
*client
,
1209 const struct i2c_device_id
*id
)
1212 struct ab3550_platform_data
*ab3550_plf_data
=
1213 client
->dev
.platform_data
;
1216 int num_i2c_clients
= 0;
1218 ab
= kzalloc(sizeof(struct ab3550
), GFP_KERNEL
);
1220 dev_err(&client
->dev
,
1221 "could not allocate " AB3550_NAME_STRING
" device\n");
1225 /* Initialize data structure */
1226 mutex_init(&ab
->access_mutex
);
1227 spin_lock_init(&ab
->event_lock
);
1228 ab
->i2c_client
[0] = client
;
1230 i2c_set_clientdata(client
, ab
);
1232 /* Read chip ID register */
1233 err
= get_register_interruptible(ab
, 0, AB3550_CID_REG
, &ab
->chip_id
);
1235 dev_err(&client
->dev
, "could not communicate with the analog "
1237 goto exit_no_detect
;
1240 for (i
= 0; ids
[i
].id
!= 0x0; i
++) {
1241 if (ids
[i
].id
== ab
->chip_id
) {
1242 snprintf(&ab
->chip_name
[0], sizeof(ab
->chip_name
) - 1,
1243 AB3550_ID_FORMAT_STRING
, ids
[i
].name
);
1248 if (ids
[i
].id
== 0x0) {
1249 dev_err(&client
->dev
, "unknown analog baseband chip id: 0x%x\n",
1251 dev_err(&client
->dev
, "driver not started!\n");
1252 goto exit_no_detect
;
1255 dev_info(&client
->dev
, "detected AB chip: %s\n", &ab
->chip_name
[0]);
1257 /* Attach other dummy I2C clients. */
1258 while (++num_i2c_clients
< AB3550_NUM_BANKS
) {
1259 ab
->i2c_client
[num_i2c_clients
] =
1260 i2c_new_dummy(client
->adapter
,
1261 (client
->addr
+ num_i2c_clients
));
1262 if (!ab
->i2c_client
[num_i2c_clients
]) {
1264 goto exit_no_dummy_client
;
1266 strlcpy(ab
->i2c_client
[num_i2c_clients
]->name
, id
->name
,
1267 sizeof(ab
->i2c_client
[num_i2c_clients
]->name
));
1270 err
= ab3550_setup(ab
);
1274 INIT_WORK(&ab
->mask_work
, ab3550_mask_work
);
1276 for (i
= 0; i
< ab3550_plf_data
->irq
.count
; i
++) {
1279 irq
= ab3550_plf_data
->irq
.base
+ i
;
1280 irq_set_chip_data(irq
, ab
);
1281 irq_set_chip_and_handler(irq
, &ab3550_irq_chip
,
1283 irq_set_nested_thread(irq
, 1);
1285 set_irq_flags(irq
, IRQF_VALID
);
1287 irq_set_noprobe(irq
);
1291 err
= request_threaded_irq(client
->irq
, NULL
, ab3550_irq_handler
,
1292 IRQF_ONESHOT
, "ab3550-core", ab
);
1293 /* This real unpredictable IRQ is of course sampled for entropy */
1294 rand_initialize_irq(client
->irq
);
1299 err
= abx500_register_ops(&client
->dev
, &ab3550_ops
);
1303 /* Set up and register the platform devices. */
1304 for (i
= 0; i
< AB3550_NUM_DEVICES
; i
++) {
1305 ab3550_devs
[i
].platform_data
= ab3550_plf_data
->dev_data
[i
];
1306 ab3550_devs
[i
].pdata_size
= ab3550_plf_data
->dev_data_sz
[i
];
1309 err
= mfd_add_devices(&client
->dev
, 0, ab3550_devs
,
1310 ARRAY_SIZE(ab3550_devs
), NULL
,
1311 ab3550_plf_data
->irq
.base
);
1313 ab3550_setup_debugfs(ab
);
1320 exit_no_dummy_client
:
1321 /* Unregister the dummy i2c clients. */
1322 while (--num_i2c_clients
)
1323 i2c_unregister_device(ab
->i2c_client
[num_i2c_clients
]);
1329 static int __exit
ab3550_remove(struct i2c_client
*client
)
1331 struct ab3550
*ab
= i2c_get_clientdata(client
);
1332 int num_i2c_clients
= AB3550_NUM_BANKS
;
1334 mfd_remove_devices(&client
->dev
);
1335 ab3550_remove_debugfs();
1337 while (--num_i2c_clients
)
1338 i2c_unregister_device(ab
->i2c_client
[num_i2c_clients
]);
1341 * At this point, all subscribers should have unregistered
1342 * their notifiers so deactivate IRQ
1344 free_irq(client
->irq
, ab
);
1349 static const struct i2c_device_id ab3550_id
[] = {
1350 {AB3550_NAME_STRING
, 0},
1353 MODULE_DEVICE_TABLE(i2c
, ab3550_id
);
1355 static struct i2c_driver ab3550_driver
= {
1357 .name
= AB3550_NAME_STRING
,
1358 .owner
= THIS_MODULE
,
1360 .id_table
= ab3550_id
,
1361 .probe
= ab3550_probe
,
1362 .remove
= __exit_p(ab3550_remove
),
1365 static int __init
ab3550_i2c_init(void)
1367 return i2c_add_driver(&ab3550_driver
);
1370 static void __exit
ab3550_i2c_exit(void)
1372 i2c_del_driver(&ab3550_driver
);
1375 subsys_initcall(ab3550_i2c_init
);
1376 module_exit(ab3550_i2c_exit
);
1378 MODULE_AUTHOR("Mattias Wallin <mattias.wallin@stericsson.com>");
1379 MODULE_DESCRIPTION("AB3550 core driver");
1380 MODULE_LICENSE("GPL");