2 * Copyright (C) 2007-2009 ST-Ericsson
3 * License terms: GNU General Public License (GPL) version 2
4 * Low-level core for exclusive access to the AB3100 IC on the I2C bus
5 * and some basic chip-configuration.
6 * Author: Linus Walleij <linus.walleij@stericsson.com>
10 #include <linux/mutex.h>
11 #include <linux/list.h>
12 #include <linux/notifier.h>
13 #include <linux/err.h>
14 #include <linux/platform_device.h>
15 #include <linux/device.h>
16 #include <linux/interrupt.h>
17 #include <linux/debugfs.h>
18 #include <linux/seq_file.h>
19 #include <linux/uaccess.h>
20 #include <linux/mfd/ab3100.h>
22 /* These are the only registers inside AB3100 used in this main file */
24 /* Interrupt event registers */
25 #define AB3100_EVENTA1 0x21
26 #define AB3100_EVENTA2 0x22
27 #define AB3100_EVENTA3 0x23
29 /* AB3100 DAC converter registers */
30 #define AB3100_DIS 0x00
31 #define AB3100_D0C 0x01
32 #define AB3100_D1C 0x02
33 #define AB3100_D2C 0x03
34 #define AB3100_D3C 0x04
36 /* Chip ID register */
37 #define AB3100_CID 0x20
39 /* AB3100 interrupt registers */
40 #define AB3100_IMRA1 0x24
41 #define AB3100_IMRA2 0x25
42 #define AB3100_IMRA3 0x26
43 #define AB3100_IMRB1 0x2B
44 #define AB3100_IMRB2 0x2C
45 #define AB3100_IMRB3 0x2D
47 /* System Power Monitoring and control registers */
48 #define AB3100_MCA 0x2E
49 #define AB3100_MCB 0x2F
52 #define AB3100_SUP 0x50
57 * The AB3100 is usually assigned address 0x48 (7-bit)
58 * The chip is defined in the platform i2c_board_data section.
61 u8
ab3100_get_chip_type(struct ab3100
*ab3100
)
65 switch (ab3100
->chip_id
& 0xf0) {
75 EXPORT_SYMBOL(ab3100_get_chip_type
);
77 int ab3100_set_register_interruptible(struct ab3100
*ab3100
, u8 reg
, u8 regval
)
79 u8 regandval
[2] = {reg
, regval
};
82 err
= mutex_lock_interruptible(&ab3100
->access_mutex
);
87 * A two-byte write message with the first byte containing the register
88 * number and the second byte containing the value to be written
89 * effectively sets a register in the AB3100.
91 err
= i2c_master_send(ab3100
->i2c_client
, regandval
, 2);
94 "write error (write register): %d\n",
96 } else if (err
!= 2) {
98 "write error (write register) "
99 "%d bytes transferred (expected 2)\n",
106 mutex_unlock(&ab3100
->access_mutex
);
109 EXPORT_SYMBOL(ab3100_set_register_interruptible
);
113 * The test registers exist at an I2C bus address up one
114 * from the ordinary base. They are not supposed to be used
115 * in production code, but sometimes you have to do that
116 * anyway. It's currently only used from this file so declare
117 * it static and do not export.
119 static int ab3100_set_test_register_interruptible(struct ab3100
*ab3100
,
122 u8 regandval
[2] = {reg
, regval
};
125 err
= mutex_lock_interruptible(&ab3100
->access_mutex
);
129 err
= i2c_master_send(ab3100
->testreg_client
, regandval
, 2);
132 "write error (write test register): %d\n",
134 } else if (err
!= 2) {
136 "write error (write test register) "
137 "%d bytes transferred (expected 2)\n",
144 mutex_unlock(&ab3100
->access_mutex
);
150 int ab3100_get_register_interruptible(struct ab3100
*ab3100
, u8 reg
, u8
*regval
)
154 err
= mutex_lock_interruptible(&ab3100
->access_mutex
);
159 * AB3100 require an I2C "stop" command between each message, else
160 * it will not work. The only way of achieveing this with the
161 * message transport layer is to send the read and write messages
164 err
= i2c_master_send(ab3100
->i2c_client
, ®
, 1);
167 "write error (send register address): %d\n",
169 goto get_reg_out_unlock
;
170 } else if (err
!= 1) {
172 "write error (send register address) "
173 "%d bytes transferred (expected 1)\n",
176 goto get_reg_out_unlock
;
182 err
= i2c_master_recv(ab3100
->i2c_client
, regval
, 1);
185 "write error (read register): %d\n",
187 goto get_reg_out_unlock
;
188 } else if (err
!= 1) {
190 "write error (read register) "
191 "%d bytes transferred (expected 1)\n",
194 goto get_reg_out_unlock
;
201 mutex_unlock(&ab3100
->access_mutex
);
204 EXPORT_SYMBOL(ab3100_get_register_interruptible
);
207 int ab3100_get_register_page_interruptible(struct ab3100
*ab3100
,
208 u8 first_reg
, u8
*regvals
, u8 numregs
)
212 if (ab3100
->chip_id
== 0xa0 ||
213 ab3100
->chip_id
== 0xa1)
214 /* These don't support paged reads */
217 err
= mutex_lock_interruptible(&ab3100
->access_mutex
);
222 * Paged read also require an I2C "stop" command.
224 err
= i2c_master_send(ab3100
->i2c_client
, &first_reg
, 1);
227 "write error (send first register address): %d\n",
229 goto get_reg_page_out_unlock
;
230 } else if (err
!= 1) {
232 "write error (send first register address) "
233 "%d bytes transferred (expected 1)\n",
236 goto get_reg_page_out_unlock
;
239 err
= i2c_master_recv(ab3100
->i2c_client
, regvals
, numregs
);
242 "write error (read register page): %d\n",
244 goto get_reg_page_out_unlock
;
245 } else if (err
!= numregs
) {
247 "write error (read register page) "
248 "%d bytes transferred (expected %d)\n",
251 goto get_reg_page_out_unlock
;
257 get_reg_page_out_unlock
:
258 mutex_unlock(&ab3100
->access_mutex
);
261 EXPORT_SYMBOL(ab3100_get_register_page_interruptible
);
264 int ab3100_mask_and_set_register_interruptible(struct ab3100
*ab3100
,
265 u8 reg
, u8 andmask
, u8 ormask
)
267 u8 regandval
[2] = {reg
, 0};
270 err
= mutex_lock_interruptible(&ab3100
->access_mutex
);
274 /* First read out the target register */
275 err
= i2c_master_send(ab3100
->i2c_client
, ®
, 1);
278 "write error (maskset send address): %d\n",
280 goto get_maskset_unlock
;
281 } else if (err
!= 1) {
283 "write error (maskset send address) "
284 "%d bytes transferred (expected 1)\n",
287 goto get_maskset_unlock
;
290 err
= i2c_master_recv(ab3100
->i2c_client
, ®andval
[1], 1);
293 "write error (maskset read register): %d\n",
295 goto get_maskset_unlock
;
296 } else if (err
!= 1) {
298 "write error (maskset read register) "
299 "%d bytes transferred (expected 1)\n",
302 goto get_maskset_unlock
;
305 /* Modify the register */
306 regandval
[1] &= andmask
;
307 regandval
[1] |= ormask
;
309 /* Write the register */
310 err
= i2c_master_send(ab3100
->i2c_client
, regandval
, 2);
313 "write error (write register): %d\n",
315 goto get_maskset_unlock
;
316 } else if (err
!= 2) {
318 "write error (write register) "
319 "%d bytes transferred (expected 2)\n",
322 goto get_maskset_unlock
;
329 mutex_unlock(&ab3100
->access_mutex
);
332 EXPORT_SYMBOL(ab3100_mask_and_set_register_interruptible
);
336 * Register a simple callback for handling any AB3100 events.
338 int ab3100_event_register(struct ab3100
*ab3100
,
339 struct notifier_block
*nb
)
341 return blocking_notifier_chain_register(&ab3100
->event_subscribers
,
344 EXPORT_SYMBOL(ab3100_event_register
);
347 * Remove a previously registered callback.
349 int ab3100_event_unregister(struct ab3100
*ab3100
,
350 struct notifier_block
*nb
)
352 return blocking_notifier_chain_unregister(&ab3100
->event_subscribers
,
355 EXPORT_SYMBOL(ab3100_event_unregister
);
358 int ab3100_event_registers_startup_state_get(struct ab3100
*ab3100
,
361 if (!ab3100
->startup_events_read
)
362 return -EAGAIN
; /* Try again later */
363 *fatevent
= ab3100
->startup_events
;
366 EXPORT_SYMBOL(ab3100_event_registers_startup_state_get
);
368 /* Interrupt handling worker */
369 static void ab3100_work(struct work_struct
*work
)
371 struct ab3100
*ab3100
= container_of(work
, struct ab3100
, work
);
376 err
= ab3100_get_register_page_interruptible(ab3100
, AB3100_EVENTA1
,
381 fatevent
= (event_regs
[0] << 16) |
382 (event_regs
[1] << 8) |
385 if (!ab3100
->startup_events_read
) {
386 ab3100
->startup_events
= fatevent
;
387 ab3100
->startup_events_read
= true;
390 * The notified parties will have to mask out the events
391 * they're interested in and react to them. They will be
392 * notified on all events, then they use the fatevent value
393 * to determine if they're interested.
395 blocking_notifier_call_chain(&ab3100
->event_subscribers
,
399 "IRQ Event: 0x%08x\n", fatevent
);
401 /* By now the IRQ should be acked and deasserted so enable it again */
402 enable_irq(ab3100
->i2c_client
->irq
);
407 "error in event workqueue\n");
408 /* Enable the IRQ anyway, what choice do we have? */
409 enable_irq(ab3100
->i2c_client
->irq
);
413 static irqreturn_t
ab3100_irq_handler(int irq
, void *data
)
415 struct ab3100
*ab3100
= data
;
417 * Disable the IRQ and dispatch a worker to handle the
418 * event. Since the chip resides on I2C this is slow
419 * stuff and we will re-enable the interrupts once th
420 * worker has finished.
422 disable_irq_nosync(irq
);
423 schedule_work(&ab3100
->work
);
427 #ifdef CONFIG_DEBUG_FS
429 * Some debugfs entries only exposed if we're using debug
431 static int ab3100_registers_print(struct seq_file
*s
, void *p
)
433 struct ab3100
*ab3100
= s
->private;
437 seq_printf(s
, "AB3100 registers:\n");
439 for (reg
= 0; reg
< 0xff; reg
++) {
440 ab3100_get_register_interruptible(ab3100
, reg
, &value
);
441 seq_printf(s
, "[0x%x]: 0x%x\n", reg
, value
);
446 static int ab3100_registers_open(struct inode
*inode
, struct file
*file
)
448 return single_open(file
, ab3100_registers_print
, inode
->i_private
);
451 static const struct file_operations ab3100_registers_fops
= {
452 .open
= ab3100_registers_open
,
455 .release
= single_release
,
456 .owner
= THIS_MODULE
,
459 struct ab3100_get_set_reg_priv
{
460 struct ab3100
*ab3100
;
464 static int ab3100_get_set_reg_open_file(struct inode
*inode
, struct file
*file
)
466 file
->private_data
= inode
->i_private
;
470 static ssize_t
ab3100_get_set_reg(struct file
*file
,
471 const char __user
*user_buf
,
472 size_t count
, loff_t
*ppos
)
474 struct ab3100_get_set_reg_priv
*priv
= file
->private_data
;
475 struct ab3100
*ab3100
= priv
->ab3100
;
479 unsigned long user_reg
;
483 /* Get userspace string and assure termination */
484 buf_size
= min(count
, (sizeof(buf
)-1));
485 if (copy_from_user(buf
, user_buf
, buf_size
))
490 * The idea is here to parse a string which is either
491 * "0xnn" for reading a register, or "0xaa 0xbb" for
492 * writing 0xbb to the register 0xaa. First move past
493 * whitespace and then begin to parse the register.
495 while ((i
< buf_size
) && (buf
[i
] == ' '))
500 * Advance pointer to end of string then terminate
501 * the register string. This is needed to satisfy
502 * the strict_strtoul() function.
504 while ((i
< buf_size
) && (buf
[i
] != ' '))
508 err
= strict_strtoul(&buf
[regp
], 16, &user_reg
);
514 /* Either we read or we write a register here */
517 u8 reg
= (u8
) user_reg
;
520 ab3100_get_register_interruptible(ab3100
, reg
, ®value
);
522 dev_info(ab3100
->dev
,
523 "debug read AB3100 reg[0x%02x]: 0x%02x\n",
527 unsigned long user_value
;
528 u8 reg
= (u8
) user_reg
;
533 * Writing, we need some value to write to
534 * the register so keep parsing the string
538 while ((i
< buf_size
) && (buf
[i
] == ' '))
541 while ((i
< buf_size
) && (buf
[i
] != ' '))
545 err
= strict_strtoul(&buf
[valp
], 16, &user_value
);
551 value
= (u8
) user_value
;
552 ab3100_set_register_interruptible(ab3100
, reg
, value
);
553 ab3100_get_register_interruptible(ab3100
, reg
, ®value
);
555 dev_info(ab3100
->dev
,
556 "debug write reg[0x%02x] with 0x%02x, "
557 "after readback: 0x%02x\n",
558 reg
, value
, regvalue
);
563 static const struct file_operations ab3100_get_set_reg_fops
= {
564 .open
= ab3100_get_set_reg_open_file
,
565 .write
= ab3100_get_set_reg
,
568 static struct dentry
*ab3100_dir
;
569 static struct dentry
*ab3100_reg_file
;
570 static struct ab3100_get_set_reg_priv ab3100_get_priv
;
571 static struct dentry
*ab3100_get_reg_file
;
572 static struct ab3100_get_set_reg_priv ab3100_set_priv
;
573 static struct dentry
*ab3100_set_reg_file
;
575 static void ab3100_setup_debugfs(struct ab3100
*ab3100
)
579 ab3100_dir
= debugfs_create_dir("ab3100", NULL
);
581 goto exit_no_debugfs
;
583 ab3100_reg_file
= debugfs_create_file("registers",
584 S_IRUGO
, ab3100_dir
, ab3100
,
585 &ab3100_registers_fops
);
586 if (!ab3100_reg_file
) {
588 goto exit_destroy_dir
;
591 ab3100_get_priv
.ab3100
= ab3100
;
592 ab3100_get_priv
.mode
= false;
593 ab3100_get_reg_file
= debugfs_create_file("get_reg",
594 S_IWUGO
, ab3100_dir
, &ab3100_get_priv
,
595 &ab3100_get_set_reg_fops
);
596 if (!ab3100_get_reg_file
) {
598 goto exit_destroy_reg
;
601 ab3100_set_priv
.ab3100
= ab3100
;
602 ab3100_set_priv
.mode
= true;
603 ab3100_set_reg_file
= debugfs_create_file("set_reg",
604 S_IWUGO
, ab3100_dir
, &ab3100_set_priv
,
605 &ab3100_get_set_reg_fops
);
606 if (!ab3100_set_reg_file
) {
608 goto exit_destroy_get_reg
;
612 exit_destroy_get_reg
:
613 debugfs_remove(ab3100_get_reg_file
);
615 debugfs_remove(ab3100_reg_file
);
617 debugfs_remove(ab3100_dir
);
621 static inline void ab3100_remove_debugfs(void)
623 debugfs_remove(ab3100_set_reg_file
);
624 debugfs_remove(ab3100_get_reg_file
);
625 debugfs_remove(ab3100_reg_file
);
626 debugfs_remove(ab3100_dir
);
629 static inline void ab3100_setup_debugfs(struct ab3100
*ab3100
)
632 static inline void ab3100_remove_debugfs(void)
638 * Basic set-up, datastructure creation/destruction and I2C interface.
639 * This sets up a default config in the AB3100 chip so that it
640 * will work as expected.
643 struct ab3100_init_setting
{
648 static const struct ab3100_init_setting __initconst
649 ab3100_init_settings
[] = {
657 .abreg
= AB3100_IMRA1
,
660 .abreg
= AB3100_IMRA2
,
663 .abreg
= AB3100_IMRA3
,
666 .abreg
= AB3100_IMRB1
,
669 .abreg
= AB3100_IMRB2
,
672 .abreg
= AB3100_IMRB3
,
695 static int __init
ab3100_setup(struct ab3100
*ab3100
)
700 for (i
= 0; i
< ARRAY_SIZE(ab3100_init_settings
); i
++) {
701 err
= ab3100_set_register_interruptible(ab3100
,
702 ab3100_init_settings
[i
].abreg
,
703 ab3100_init_settings
[i
].setting
);
709 * Special trick to make the AB3100 use the 32kHz clock (RTC)
710 * bit 3 in test register 0x02 is a special, undocumented test
711 * register bit that only exist in AB3100 P1E
713 if (ab3100
->chip_id
== 0xc4) {
714 dev_warn(ab3100
->dev
,
715 "AB3100 P1E variant detected, "
716 "forcing chip to 32KHz\n");
717 err
= ab3100_set_test_register_interruptible(ab3100
, 0x02, 0x08);
725 * Here we define all the platform devices that appear
726 * as children of the AB3100. These are regular platform
727 * devices with the IORESOURCE_IO .start and .end set
728 * to correspond to the internal AB3100 register range
729 * mapping to the corresponding subdevice.
732 #define AB3100_DEVICE(devname, devid) \
733 static struct platform_device ab3100_##devname##_device = { \
739 * This lists all the subdevices and corresponding register
742 AB3100_DEVICE(dac
, "ab3100-dac");
743 AB3100_DEVICE(leds
, "ab3100-leds");
744 AB3100_DEVICE(power
, "ab3100-power");
745 AB3100_DEVICE(regulators
, "ab3100-regulators");
746 AB3100_DEVICE(sim
, "ab3100-sim");
747 AB3100_DEVICE(uart
, "ab3100-uart");
748 AB3100_DEVICE(rtc
, "ab3100-rtc");
749 AB3100_DEVICE(charger
, "ab3100-charger");
750 AB3100_DEVICE(boost
, "ab3100-boost");
751 AB3100_DEVICE(adc
, "ab3100-adc");
752 AB3100_DEVICE(fuelgauge
, "ab3100-fuelgauge");
753 AB3100_DEVICE(vibrator
, "ab3100-vibrator");
754 AB3100_DEVICE(otp
, "ab3100-otp");
755 AB3100_DEVICE(codec
, "ab3100-codec");
757 static struct platform_device
*
758 ab3100_platform_devs
[] = {
761 &ab3100_power_device
,
762 &ab3100_regulators_device
,
766 &ab3100_charger_device
,
767 &ab3100_boost_device
,
769 &ab3100_fuelgauge_device
,
770 &ab3100_vibrator_device
,
772 &ab3100_codec_device
,
775 struct ab_family_id
{
780 static const struct ab_family_id ids
[] __initdata
= {
810 /* AB3000 variants, not supported */
834 static int __init
ab3100_probe(struct i2c_client
*client
,
835 const struct i2c_device_id
*id
)
837 struct ab3100
*ab3100
;
838 struct ab3100_platform_data
*ab3100_plf_data
=
839 client
->dev
.platform_data
;
843 ab3100
= kzalloc(sizeof(struct ab3100
), GFP_KERNEL
);
845 dev_err(&client
->dev
, "could not allocate AB3100 device\n");
849 /* Initialize data structure */
850 mutex_init(&ab3100
->access_mutex
);
851 BLOCKING_INIT_NOTIFIER_HEAD(&ab3100
->event_subscribers
);
853 ab3100
->i2c_client
= client
;
854 ab3100
->dev
= &ab3100
->i2c_client
->dev
;
856 i2c_set_clientdata(client
, ab3100
);
858 /* Read chip ID register */
859 err
= ab3100_get_register_interruptible(ab3100
, AB3100_CID
,
862 dev_err(&client
->dev
,
863 "could not communicate with the AB3100 analog "
868 for (i
= 0; ids
[i
].id
!= 0x0; i
++) {
869 if (ids
[i
].id
== ab3100
->chip_id
) {
870 if (ids
[i
].name
!= NULL
) {
871 snprintf(&ab3100
->chip_name
[0],
872 sizeof(ab3100
->chip_name
) - 1,
877 dev_err(&client
->dev
,
878 "AB3000 is not supported\n");
884 if (ids
[i
].id
== 0x0) {
885 dev_err(&client
->dev
, "unknown analog baseband chip id: 0x%x\n",
887 dev_err(&client
->dev
, "accepting it anyway. Please update "
892 dev_info(&client
->dev
, "Detected chip: %s\n",
893 &ab3100
->chip_name
[0]);
895 /* Attach a second dummy i2c_client to the test register address */
896 ab3100
->testreg_client
= i2c_new_dummy(client
->adapter
,
898 if (!ab3100
->testreg_client
) {
900 goto exit_no_testreg_client
;
903 strlcpy(ab3100
->testreg_client
->name
, id
->name
,
904 sizeof(ab3100
->testreg_client
->name
));
906 err
= ab3100_setup(ab3100
);
910 INIT_WORK(&ab3100
->work
, ab3100_work
);
912 /* This real unpredictable IRQ is of course sampled for entropy */
913 err
= request_irq(client
->irq
, ab3100_irq_handler
,
914 IRQF_DISABLED
| IRQF_SAMPLE_RANDOM
,
915 "AB3100 IRQ", ab3100
);
919 /* Set parent and a pointer back to the container in device data */
920 for (i
= 0; i
< ARRAY_SIZE(ab3100_platform_devs
); i
++) {
921 ab3100_platform_devs
[i
]->dev
.parent
=
923 ab3100_platform_devs
[i
]->dev
.platform_data
=
925 platform_set_drvdata(ab3100_platform_devs
[i
], ab3100
);
928 /* Register the platform devices */
929 platform_add_devices(ab3100_platform_devs
,
930 ARRAY_SIZE(ab3100_platform_devs
));
932 ab3100_setup_debugfs(ab3100
);
938 i2c_unregister_device(ab3100
->testreg_client
);
939 exit_no_testreg_client
:
945 static int __exit
ab3100_remove(struct i2c_client
*client
)
947 struct ab3100
*ab3100
= i2c_get_clientdata(client
);
950 /* Unregister subdevices */
951 for (i
= 0; i
< ARRAY_SIZE(ab3100_platform_devs
); i
++)
952 platform_device_unregister(ab3100_platform_devs
[i
]);
954 ab3100_remove_debugfs();
955 i2c_unregister_device(ab3100
->testreg_client
);
958 * At this point, all subscribers should have unregistered
959 * their notifiers so deactivate IRQ
961 free_irq(client
->irq
, ab3100
);
966 static const struct i2c_device_id ab3100_id
[] = {
970 MODULE_DEVICE_TABLE(i2c
, ab3100_id
);
972 static struct i2c_driver ab3100_driver
= {
975 .owner
= THIS_MODULE
,
977 .id_table
= ab3100_id
,
978 .probe
= ab3100_probe
,
979 .remove
= __exit_p(ab3100_remove
),
982 static int __init
ab3100_i2c_init(void)
984 return i2c_add_driver(&ab3100_driver
);
987 static void __exit
ab3100_i2c_exit(void)
989 i2c_del_driver(&ab3100_driver
);
992 subsys_initcall(ab3100_i2c_init
);
993 module_exit(ab3100_i2c_exit
);
995 MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
996 MODULE_DESCRIPTION("AB3100 core driver");
997 MODULE_LICENSE("GPL");