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 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/slab.h>
14 #include <linux/err.h>
15 #include <linux/platform_device.h>
16 #include <linux/device.h>
17 #include <linux/interrupt.h>
18 #include <linux/random.h>
19 #include <linux/debugfs.h>
20 #include <linux/seq_file.h>
21 #include <linux/uaccess.h>
22 #include <linux/mfd/ab3100.h>
24 /* These are the only registers inside AB3100 used in this main file */
26 /* Interrupt event registers */
27 #define AB3100_EVENTA1 0x21
28 #define AB3100_EVENTA2 0x22
29 #define AB3100_EVENTA3 0x23
31 /* AB3100 DAC converter registers */
32 #define AB3100_DIS 0x00
33 #define AB3100_D0C 0x01
34 #define AB3100_D1C 0x02
35 #define AB3100_D2C 0x03
36 #define AB3100_D3C 0x04
38 /* Chip ID register */
39 #define AB3100_CID 0x20
41 /* AB3100 interrupt registers */
42 #define AB3100_IMRA1 0x24
43 #define AB3100_IMRA2 0x25
44 #define AB3100_IMRA3 0x26
45 #define AB3100_IMRB1 0x2B
46 #define AB3100_IMRB2 0x2C
47 #define AB3100_IMRB3 0x2D
49 /* System Power Monitoring and control registers */
50 #define AB3100_MCA 0x2E
51 #define AB3100_MCB 0x2F
54 #define AB3100_SUP 0x50
59 * The AB3100 is usually assigned address 0x48 (7-bit)
60 * The chip is defined in the platform i2c_board_data section.
63 u8
ab3100_get_chip_type(struct ab3100
*ab3100
)
67 switch (ab3100
->chip_id
& 0xf0) {
77 EXPORT_SYMBOL(ab3100_get_chip_type
);
79 int ab3100_set_register_interruptible(struct ab3100
*ab3100
, u8 reg
, u8 regval
)
81 u8 regandval
[2] = {reg
, regval
};
84 err
= mutex_lock_interruptible(&ab3100
->access_mutex
);
89 * A two-byte write message with the first byte containing the register
90 * number and the second byte containing the value to be written
91 * effectively sets a register in the AB3100.
93 err
= i2c_master_send(ab3100
->i2c_client
, regandval
, 2);
96 "write error (write register): %d\n",
98 } else if (err
!= 2) {
100 "write error (write register) "
101 "%d bytes transferred (expected 2)\n",
108 mutex_unlock(&ab3100
->access_mutex
);
111 EXPORT_SYMBOL(ab3100_set_register_interruptible
);
115 * The test registers exist at an I2C bus address up one
116 * from the ordinary base. They are not supposed to be used
117 * in production code, but sometimes you have to do that
118 * anyway. It's currently only used from this file so declare
119 * it static and do not export.
121 static int ab3100_set_test_register_interruptible(struct ab3100
*ab3100
,
124 u8 regandval
[2] = {reg
, regval
};
127 err
= mutex_lock_interruptible(&ab3100
->access_mutex
);
131 err
= i2c_master_send(ab3100
->testreg_client
, regandval
, 2);
134 "write error (write test register): %d\n",
136 } else if (err
!= 2) {
138 "write error (write test register) "
139 "%d bytes transferred (expected 2)\n",
146 mutex_unlock(&ab3100
->access_mutex
);
152 int ab3100_get_register_interruptible(struct ab3100
*ab3100
, u8 reg
, u8
*regval
)
156 err
= mutex_lock_interruptible(&ab3100
->access_mutex
);
161 * AB3100 require an I2C "stop" command between each message, else
162 * it will not work. The only way of achieveing this with the
163 * message transport layer is to send the read and write messages
166 err
= i2c_master_send(ab3100
->i2c_client
, ®
, 1);
169 "write error (send register address): %d\n",
171 goto get_reg_out_unlock
;
172 } else if (err
!= 1) {
174 "write error (send register address) "
175 "%d bytes transferred (expected 1)\n",
178 goto get_reg_out_unlock
;
184 err
= i2c_master_recv(ab3100
->i2c_client
, regval
, 1);
187 "write error (read register): %d\n",
189 goto get_reg_out_unlock
;
190 } else if (err
!= 1) {
192 "write error (read register) "
193 "%d bytes transferred (expected 1)\n",
196 goto get_reg_out_unlock
;
203 mutex_unlock(&ab3100
->access_mutex
);
206 EXPORT_SYMBOL(ab3100_get_register_interruptible
);
209 int ab3100_get_register_page_interruptible(struct ab3100
*ab3100
,
210 u8 first_reg
, u8
*regvals
, u8 numregs
)
214 if (ab3100
->chip_id
== 0xa0 ||
215 ab3100
->chip_id
== 0xa1)
216 /* These don't support paged reads */
219 err
= mutex_lock_interruptible(&ab3100
->access_mutex
);
224 * Paged read also require an I2C "stop" command.
226 err
= i2c_master_send(ab3100
->i2c_client
, &first_reg
, 1);
229 "write error (send first register address): %d\n",
231 goto get_reg_page_out_unlock
;
232 } else if (err
!= 1) {
234 "write error (send first register address) "
235 "%d bytes transferred (expected 1)\n",
238 goto get_reg_page_out_unlock
;
241 err
= i2c_master_recv(ab3100
->i2c_client
, regvals
, numregs
);
244 "write error (read register page): %d\n",
246 goto get_reg_page_out_unlock
;
247 } else if (err
!= numregs
) {
249 "write error (read register page) "
250 "%d bytes transferred (expected %d)\n",
253 goto get_reg_page_out_unlock
;
259 get_reg_page_out_unlock
:
260 mutex_unlock(&ab3100
->access_mutex
);
263 EXPORT_SYMBOL(ab3100_get_register_page_interruptible
);
266 int ab3100_mask_and_set_register_interruptible(struct ab3100
*ab3100
,
267 u8 reg
, u8 andmask
, u8 ormask
)
269 u8 regandval
[2] = {reg
, 0};
272 err
= mutex_lock_interruptible(&ab3100
->access_mutex
);
276 /* First read out the target register */
277 err
= i2c_master_send(ab3100
->i2c_client
, ®
, 1);
280 "write error (maskset send address): %d\n",
282 goto get_maskset_unlock
;
283 } else if (err
!= 1) {
285 "write error (maskset send address) "
286 "%d bytes transferred (expected 1)\n",
289 goto get_maskset_unlock
;
292 err
= i2c_master_recv(ab3100
->i2c_client
, ®andval
[1], 1);
295 "write error (maskset read register): %d\n",
297 goto get_maskset_unlock
;
298 } else if (err
!= 1) {
300 "write error (maskset read register) "
301 "%d bytes transferred (expected 1)\n",
304 goto get_maskset_unlock
;
307 /* Modify the register */
308 regandval
[1] &= andmask
;
309 regandval
[1] |= ormask
;
311 /* Write the register */
312 err
= i2c_master_send(ab3100
->i2c_client
, regandval
, 2);
315 "write error (write register): %d\n",
317 goto get_maskset_unlock
;
318 } else if (err
!= 2) {
320 "write error (write register) "
321 "%d bytes transferred (expected 2)\n",
324 goto get_maskset_unlock
;
331 mutex_unlock(&ab3100
->access_mutex
);
334 EXPORT_SYMBOL(ab3100_mask_and_set_register_interruptible
);
338 * Register a simple callback for handling any AB3100 events.
340 int ab3100_event_register(struct ab3100
*ab3100
,
341 struct notifier_block
*nb
)
343 return blocking_notifier_chain_register(&ab3100
->event_subscribers
,
346 EXPORT_SYMBOL(ab3100_event_register
);
349 * Remove a previously registered callback.
351 int ab3100_event_unregister(struct ab3100
*ab3100
,
352 struct notifier_block
*nb
)
354 return blocking_notifier_chain_unregister(&ab3100
->event_subscribers
,
357 EXPORT_SYMBOL(ab3100_event_unregister
);
360 int ab3100_event_registers_startup_state_get(struct ab3100
*ab3100
,
363 if (!ab3100
->startup_events_read
)
364 return -EAGAIN
; /* Try again later */
365 *fatevent
= ab3100
->startup_events
;
368 EXPORT_SYMBOL(ab3100_event_registers_startup_state_get
);
371 * This is a threaded interrupt handler so we can make some
374 static irqreturn_t
ab3100_irq_handler(int irq
, void *data
)
376 struct ab3100
*ab3100
= data
;
381 add_interrupt_randomness(irq
);
383 err
= ab3100_get_register_page_interruptible(ab3100
, AB3100_EVENTA1
,
388 fatevent
= (event_regs
[0] << 16) |
389 (event_regs
[1] << 8) |
392 if (!ab3100
->startup_events_read
) {
393 ab3100
->startup_events
= fatevent
;
394 ab3100
->startup_events_read
= true;
397 * The notified parties will have to mask out the events
398 * they're interested in and react to them. They will be
399 * notified on all events, then they use the fatevent value
400 * to determine if they're interested.
402 blocking_notifier_call_chain(&ab3100
->event_subscribers
,
406 "IRQ Event: 0x%08x\n", fatevent
);
412 "error reading event status\n");
416 #ifdef CONFIG_DEBUG_FS
418 * Some debugfs entries only exposed if we're using debug
420 static int ab3100_registers_print(struct seq_file
*s
, void *p
)
422 struct ab3100
*ab3100
= s
->private;
426 seq_printf(s
, "AB3100 registers:\n");
428 for (reg
= 0; reg
< 0xff; reg
++) {
429 ab3100_get_register_interruptible(ab3100
, reg
, &value
);
430 seq_printf(s
, "[0x%x]: 0x%x\n", reg
, value
);
435 static int ab3100_registers_open(struct inode
*inode
, struct file
*file
)
437 return single_open(file
, ab3100_registers_print
, inode
->i_private
);
440 static const struct file_operations ab3100_registers_fops
= {
441 .open
= ab3100_registers_open
,
444 .release
= single_release
,
445 .owner
= THIS_MODULE
,
448 struct ab3100_get_set_reg_priv
{
449 struct ab3100
*ab3100
;
453 static int ab3100_get_set_reg_open_file(struct inode
*inode
, struct file
*file
)
455 file
->private_data
= inode
->i_private
;
459 static ssize_t
ab3100_get_set_reg(struct file
*file
,
460 const char __user
*user_buf
,
461 size_t count
, loff_t
*ppos
)
463 struct ab3100_get_set_reg_priv
*priv
= file
->private_data
;
464 struct ab3100
*ab3100
= priv
->ab3100
;
468 unsigned long user_reg
;
472 /* Get userspace string and assure termination */
473 buf_size
= min(count
, (sizeof(buf
)-1));
474 if (copy_from_user(buf
, user_buf
, buf_size
))
479 * The idea is here to parse a string which is either
480 * "0xnn" for reading a register, or "0xaa 0xbb" for
481 * writing 0xbb to the register 0xaa. First move past
482 * whitespace and then begin to parse the register.
484 while ((i
< buf_size
) && (buf
[i
] == ' '))
489 * Advance pointer to end of string then terminate
490 * the register string. This is needed to satisfy
491 * the strict_strtoul() function.
493 while ((i
< buf_size
) && (buf
[i
] != ' '))
497 err
= strict_strtoul(&buf
[regp
], 16, &user_reg
);
503 /* Either we read or we write a register here */
506 u8 reg
= (u8
) user_reg
;
509 ab3100_get_register_interruptible(ab3100
, reg
, ®value
);
511 dev_info(ab3100
->dev
,
512 "debug read AB3100 reg[0x%02x]: 0x%02x\n",
516 unsigned long user_value
;
517 u8 reg
= (u8
) user_reg
;
522 * Writing, we need some value to write to
523 * the register so keep parsing the string
527 while ((i
< buf_size
) && (buf
[i
] == ' '))
530 while ((i
< buf_size
) && (buf
[i
] != ' '))
534 err
= strict_strtoul(&buf
[valp
], 16, &user_value
);
540 value
= (u8
) user_value
;
541 ab3100_set_register_interruptible(ab3100
, reg
, value
);
542 ab3100_get_register_interruptible(ab3100
, reg
, ®value
);
544 dev_info(ab3100
->dev
,
545 "debug write reg[0x%02x] with 0x%02x, "
546 "after readback: 0x%02x\n",
547 reg
, value
, regvalue
);
552 static const struct file_operations ab3100_get_set_reg_fops
= {
553 .open
= ab3100_get_set_reg_open_file
,
554 .write
= ab3100_get_set_reg
,
557 static struct dentry
*ab3100_dir
;
558 static struct dentry
*ab3100_reg_file
;
559 static struct ab3100_get_set_reg_priv ab3100_get_priv
;
560 static struct dentry
*ab3100_get_reg_file
;
561 static struct ab3100_get_set_reg_priv ab3100_set_priv
;
562 static struct dentry
*ab3100_set_reg_file
;
564 static void ab3100_setup_debugfs(struct ab3100
*ab3100
)
568 ab3100_dir
= debugfs_create_dir("ab3100", NULL
);
570 goto exit_no_debugfs
;
572 ab3100_reg_file
= debugfs_create_file("registers",
573 S_IRUGO
, ab3100_dir
, ab3100
,
574 &ab3100_registers_fops
);
575 if (!ab3100_reg_file
) {
577 goto exit_destroy_dir
;
580 ab3100_get_priv
.ab3100
= ab3100
;
581 ab3100_get_priv
.mode
= false;
582 ab3100_get_reg_file
= debugfs_create_file("get_reg",
583 S_IWUGO
, ab3100_dir
, &ab3100_get_priv
,
584 &ab3100_get_set_reg_fops
);
585 if (!ab3100_get_reg_file
) {
587 goto exit_destroy_reg
;
590 ab3100_set_priv
.ab3100
= ab3100
;
591 ab3100_set_priv
.mode
= true;
592 ab3100_set_reg_file
= debugfs_create_file("set_reg",
593 S_IWUGO
, ab3100_dir
, &ab3100_set_priv
,
594 &ab3100_get_set_reg_fops
);
595 if (!ab3100_set_reg_file
) {
597 goto exit_destroy_get_reg
;
601 exit_destroy_get_reg
:
602 debugfs_remove(ab3100_get_reg_file
);
604 debugfs_remove(ab3100_reg_file
);
606 debugfs_remove(ab3100_dir
);
610 static inline void ab3100_remove_debugfs(void)
612 debugfs_remove(ab3100_set_reg_file
);
613 debugfs_remove(ab3100_get_reg_file
);
614 debugfs_remove(ab3100_reg_file
);
615 debugfs_remove(ab3100_dir
);
618 static inline void ab3100_setup_debugfs(struct ab3100
*ab3100
)
621 static inline void ab3100_remove_debugfs(void)
627 * Basic set-up, datastructure creation/destruction and I2C interface.
628 * This sets up a default config in the AB3100 chip so that it
629 * will work as expected.
632 struct ab3100_init_setting
{
637 static const struct ab3100_init_setting __initconst
638 ab3100_init_settings
[] = {
646 .abreg
= AB3100_IMRA1
,
649 .abreg
= AB3100_IMRA2
,
652 .abreg
= AB3100_IMRA3
,
655 .abreg
= AB3100_IMRB1
,
658 .abreg
= AB3100_IMRB2
,
661 .abreg
= AB3100_IMRB3
,
684 static int __init
ab3100_setup(struct ab3100
*ab3100
)
689 for (i
= 0; i
< ARRAY_SIZE(ab3100_init_settings
); i
++) {
690 err
= ab3100_set_register_interruptible(ab3100
,
691 ab3100_init_settings
[i
].abreg
,
692 ab3100_init_settings
[i
].setting
);
698 * Special trick to make the AB3100 use the 32kHz clock (RTC)
699 * bit 3 in test register 0x02 is a special, undocumented test
700 * register bit that only exist in AB3100 P1E
702 if (ab3100
->chip_id
== 0xc4) {
703 dev_warn(ab3100
->dev
,
704 "AB3100 P1E variant detected, "
705 "forcing chip to 32KHz\n");
706 err
= ab3100_set_test_register_interruptible(ab3100
, 0x02, 0x08);
714 * Here we define all the platform devices that appear
715 * as children of the AB3100. These are regular platform
716 * devices with the IORESOURCE_IO .start and .end set
717 * to correspond to the internal AB3100 register range
718 * mapping to the corresponding subdevice.
721 #define AB3100_DEVICE(devname, devid) \
722 static struct platform_device ab3100_##devname##_device = { \
727 /* This lists all the subdevices */
728 AB3100_DEVICE(dac
, "ab3100-dac");
729 AB3100_DEVICE(leds
, "ab3100-leds");
730 AB3100_DEVICE(power
, "ab3100-power");
731 AB3100_DEVICE(regulators
, "ab3100-regulators");
732 AB3100_DEVICE(sim
, "ab3100-sim");
733 AB3100_DEVICE(uart
, "ab3100-uart");
734 AB3100_DEVICE(rtc
, "ab3100-rtc");
735 AB3100_DEVICE(charger
, "ab3100-charger");
736 AB3100_DEVICE(boost
, "ab3100-boost");
737 AB3100_DEVICE(adc
, "ab3100-adc");
738 AB3100_DEVICE(fuelgauge
, "ab3100-fuelgauge");
739 AB3100_DEVICE(vibrator
, "ab3100-vibrator");
740 AB3100_DEVICE(otp
, "ab3100-otp");
741 AB3100_DEVICE(codec
, "ab3100-codec");
743 static struct platform_device
*
744 ab3100_platform_devs
[] = {
747 &ab3100_power_device
,
748 &ab3100_regulators_device
,
752 &ab3100_charger_device
,
753 &ab3100_boost_device
,
755 &ab3100_fuelgauge_device
,
756 &ab3100_vibrator_device
,
758 &ab3100_codec_device
,
761 struct ab_family_id
{
766 static const struct ab_family_id ids
[] __initdata
= {
796 /* AB3000 variants, not supported */
820 static int __init
ab3100_probe(struct i2c_client
*client
,
821 const struct i2c_device_id
*id
)
823 struct ab3100
*ab3100
;
824 struct ab3100_platform_data
*ab3100_plf_data
=
825 client
->dev
.platform_data
;
829 ab3100
= kzalloc(sizeof(struct ab3100
), GFP_KERNEL
);
831 dev_err(&client
->dev
, "could not allocate AB3100 device\n");
835 /* Initialize data structure */
836 mutex_init(&ab3100
->access_mutex
);
837 BLOCKING_INIT_NOTIFIER_HEAD(&ab3100
->event_subscribers
);
839 ab3100
->i2c_client
= client
;
840 ab3100
->dev
= &ab3100
->i2c_client
->dev
;
842 i2c_set_clientdata(client
, ab3100
);
844 /* Read chip ID register */
845 err
= ab3100_get_register_interruptible(ab3100
, AB3100_CID
,
848 dev_err(&client
->dev
,
849 "could not communicate with the AB3100 analog "
854 for (i
= 0; ids
[i
].id
!= 0x0; i
++) {
855 if (ids
[i
].id
== ab3100
->chip_id
) {
856 if (ids
[i
].name
!= NULL
) {
857 snprintf(&ab3100
->chip_name
[0],
858 sizeof(ab3100
->chip_name
) - 1,
863 dev_err(&client
->dev
,
864 "AB3000 is not supported\n");
870 if (ids
[i
].id
== 0x0) {
871 dev_err(&client
->dev
, "unknown analog baseband chip id: 0x%x\n",
873 dev_err(&client
->dev
, "accepting it anyway. Please update "
878 dev_info(&client
->dev
, "Detected chip: %s\n",
879 &ab3100
->chip_name
[0]);
881 /* Attach a second dummy i2c_client to the test register address */
882 ab3100
->testreg_client
= i2c_new_dummy(client
->adapter
,
884 if (!ab3100
->testreg_client
) {
886 goto exit_no_testreg_client
;
889 err
= ab3100_setup(ab3100
);
893 err
= request_threaded_irq(client
->irq
, NULL
, ab3100_irq_handler
,
894 IRQF_ONESHOT
, "ab3100-core", ab3100
);
895 /* This real unpredictable IRQ is of course sampled for entropy */
896 rand_initialize_irq(client
->irq
);
901 /* Set parent and a pointer back to the container in device data */
902 for (i
= 0; i
< ARRAY_SIZE(ab3100_platform_devs
); i
++) {
903 ab3100_platform_devs
[i
]->dev
.parent
=
905 ab3100_platform_devs
[i
]->dev
.platform_data
=
907 platform_set_drvdata(ab3100_platform_devs
[i
], ab3100
);
910 /* Register the platform devices */
911 platform_add_devices(ab3100_platform_devs
,
912 ARRAY_SIZE(ab3100_platform_devs
));
914 ab3100_setup_debugfs(ab3100
);
920 i2c_unregister_device(ab3100
->testreg_client
);
921 exit_no_testreg_client
:
927 static int __exit
ab3100_remove(struct i2c_client
*client
)
929 struct ab3100
*ab3100
= i2c_get_clientdata(client
);
932 /* Unregister subdevices */
933 for (i
= 0; i
< ARRAY_SIZE(ab3100_platform_devs
); i
++)
934 platform_device_unregister(ab3100_platform_devs
[i
]);
936 ab3100_remove_debugfs();
937 i2c_unregister_device(ab3100
->testreg_client
);
940 * At this point, all subscribers should have unregistered
941 * their notifiers so deactivate IRQ
943 free_irq(client
->irq
, ab3100
);
948 static const struct i2c_device_id ab3100_id
[] = {
952 MODULE_DEVICE_TABLE(i2c
, ab3100_id
);
954 static struct i2c_driver ab3100_driver
= {
957 .owner
= THIS_MODULE
,
959 .id_table
= ab3100_id
,
960 .probe
= ab3100_probe
,
961 .remove
= __exit_p(ab3100_remove
),
964 static int __init
ab3100_i2c_init(void)
966 return i2c_add_driver(&ab3100_driver
);
969 static void __exit
ab3100_i2c_exit(void)
971 i2c_del_driver(&ab3100_driver
);
974 subsys_initcall(ab3100_i2c_init
);
975 module_exit(ab3100_i2c_exit
);
977 MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
978 MODULE_DESCRIPTION("AB3100 core driver");
979 MODULE_LICENSE("GPL");