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/abx500.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.
62 static int ab3100_get_chip_id(struct device
*dev
)
64 struct ab3100
*ab3100
= dev_get_drvdata(dev
->parent
);
66 return (int)ab3100
->chip_id
;
69 static int ab3100_set_register_interruptible(struct ab3100
*ab3100
,
72 u8 regandval
[2] = {reg
, regval
};
75 err
= mutex_lock_interruptible(&ab3100
->access_mutex
);
80 * A two-byte write message with the first byte containing the register
81 * number and the second byte containing the value to be written
82 * effectively sets a register in the AB3100.
84 err
= i2c_master_send(ab3100
->i2c_client
, regandval
, 2);
87 "write error (write register): %d\n",
89 } else if (err
!= 2) {
91 "write error (write register) "
92 "%d bytes transferred (expected 2)\n",
99 mutex_unlock(&ab3100
->access_mutex
);
103 static int set_register_interruptible(struct device
*dev
,
104 u8 bank
, u8 reg
, u8 value
)
106 struct ab3100
*ab3100
= dev_get_drvdata(dev
->parent
);
108 return ab3100_set_register_interruptible(ab3100
, reg
, value
);
112 * The test registers exist at an I2C bus address up one
113 * from the ordinary base. They are not supposed to be used
114 * in production code, but sometimes you have to do that
115 * anyway. It's currently only used from this file so declare
116 * it static and do not export.
118 static int ab3100_set_test_register_interruptible(struct ab3100
*ab3100
,
121 u8 regandval
[2] = {reg
, regval
};
124 err
= mutex_lock_interruptible(&ab3100
->access_mutex
);
128 err
= i2c_master_send(ab3100
->testreg_client
, regandval
, 2);
131 "write error (write test register): %d\n",
133 } else if (err
!= 2) {
135 "write error (write test register) "
136 "%d bytes transferred (expected 2)\n",
143 mutex_unlock(&ab3100
->access_mutex
);
148 static int ab3100_get_register_interruptible(struct ab3100
*ab3100
,
153 err
= mutex_lock_interruptible(&ab3100
->access_mutex
);
158 * AB3100 require an I2C "stop" command between each message, else
159 * it will not work. The only way of achieveing this with the
160 * message transport layer is to send the read and write messages
163 err
= i2c_master_send(ab3100
->i2c_client
, ®
, 1);
166 "write error (send register address): %d\n",
168 goto get_reg_out_unlock
;
169 } else if (err
!= 1) {
171 "write error (send register address) "
172 "%d bytes transferred (expected 1)\n",
175 goto get_reg_out_unlock
;
181 err
= i2c_master_recv(ab3100
->i2c_client
, regval
, 1);
184 "write error (read register): %d\n",
186 goto get_reg_out_unlock
;
187 } else if (err
!= 1) {
189 "write error (read register) "
190 "%d bytes transferred (expected 1)\n",
193 goto get_reg_out_unlock
;
200 mutex_unlock(&ab3100
->access_mutex
);
204 static int get_register_interruptible(struct device
*dev
, u8 bank
, u8 reg
,
207 struct ab3100
*ab3100
= dev_get_drvdata(dev
->parent
);
209 return ab3100_get_register_interruptible(ab3100
, reg
, value
);
212 static int ab3100_get_register_page_interruptible(struct ab3100
*ab3100
,
213 u8 first_reg
, u8
*regvals
, u8 numregs
)
217 if (ab3100
->chip_id
== 0xa0 ||
218 ab3100
->chip_id
== 0xa1)
219 /* These don't support paged reads */
222 err
= mutex_lock_interruptible(&ab3100
->access_mutex
);
227 * Paged read also require an I2C "stop" command.
229 err
= i2c_master_send(ab3100
->i2c_client
, &first_reg
, 1);
232 "write error (send first register address): %d\n",
234 goto get_reg_page_out_unlock
;
235 } else if (err
!= 1) {
237 "write error (send first register address) "
238 "%d bytes transferred (expected 1)\n",
241 goto get_reg_page_out_unlock
;
244 err
= i2c_master_recv(ab3100
->i2c_client
, regvals
, numregs
);
247 "write error (read register page): %d\n",
249 goto get_reg_page_out_unlock
;
250 } else if (err
!= numregs
) {
252 "write error (read register page) "
253 "%d bytes transferred (expected %d)\n",
256 goto get_reg_page_out_unlock
;
262 get_reg_page_out_unlock
:
263 mutex_unlock(&ab3100
->access_mutex
);
267 static int get_register_page_interruptible(struct device
*dev
, u8 bank
,
268 u8 first_reg
, u8
*regvals
, u8 numregs
)
270 struct ab3100
*ab3100
= dev_get_drvdata(dev
->parent
);
272 return ab3100_get_register_page_interruptible(ab3100
,
273 first_reg
, regvals
, numregs
);
276 static int ab3100_mask_and_set_register_interruptible(struct ab3100
*ab3100
,
277 u8 reg
, u8 andmask
, u8 ormask
)
279 u8 regandval
[2] = {reg
, 0};
282 err
= mutex_lock_interruptible(&ab3100
->access_mutex
);
286 /* First read out the target register */
287 err
= i2c_master_send(ab3100
->i2c_client
, ®
, 1);
290 "write error (maskset send address): %d\n",
292 goto get_maskset_unlock
;
293 } else if (err
!= 1) {
295 "write error (maskset send address) "
296 "%d bytes transferred (expected 1)\n",
299 goto get_maskset_unlock
;
302 err
= i2c_master_recv(ab3100
->i2c_client
, ®andval
[1], 1);
305 "write error (maskset read register): %d\n",
307 goto get_maskset_unlock
;
308 } else if (err
!= 1) {
310 "write error (maskset read register) "
311 "%d bytes transferred (expected 1)\n",
314 goto get_maskset_unlock
;
317 /* Modify the register */
318 regandval
[1] &= andmask
;
319 regandval
[1] |= ormask
;
321 /* Write the register */
322 err
= i2c_master_send(ab3100
->i2c_client
, regandval
, 2);
325 "write error (write register): %d\n",
327 goto get_maskset_unlock
;
328 } else if (err
!= 2) {
330 "write error (write register) "
331 "%d bytes transferred (expected 2)\n",
334 goto get_maskset_unlock
;
341 mutex_unlock(&ab3100
->access_mutex
);
345 static int mask_and_set_register_interruptible(struct device
*dev
, u8 bank
,
346 u8 reg
, u8 bitmask
, u8 bitvalues
)
348 struct ab3100
*ab3100
= dev_get_drvdata(dev
->parent
);
350 return ab3100_mask_and_set_register_interruptible(ab3100
,
351 reg
, bitmask
, (bitmask
& bitvalues
));
355 * Register a simple callback for handling any AB3100 events.
357 int ab3100_event_register(struct ab3100
*ab3100
,
358 struct notifier_block
*nb
)
360 return blocking_notifier_chain_register(&ab3100
->event_subscribers
,
363 EXPORT_SYMBOL(ab3100_event_register
);
366 * Remove a previously registered callback.
368 int ab3100_event_unregister(struct ab3100
*ab3100
,
369 struct notifier_block
*nb
)
371 return blocking_notifier_chain_unregister(&ab3100
->event_subscribers
,
374 EXPORT_SYMBOL(ab3100_event_unregister
);
377 static int ab3100_event_registers_startup_state_get(struct device
*dev
,
380 struct ab3100
*ab3100
= dev_get_drvdata(dev
->parent
);
381 if (!ab3100
->startup_events_read
)
382 return -EAGAIN
; /* Try again later */
383 memcpy(event
, ab3100
->startup_events
, 3);
387 static struct abx500_ops ab3100_ops
= {
388 .get_chip_id
= ab3100_get_chip_id
,
389 .set_register
= set_register_interruptible
,
390 .get_register
= get_register_interruptible
,
391 .get_register_page
= get_register_page_interruptible
,
392 .set_register_page
= NULL
,
393 .mask_and_set_register
= mask_and_set_register_interruptible
,
394 .event_registers_startup_state_get
=
395 ab3100_event_registers_startup_state_get
,
396 .startup_irq_enabled
= NULL
,
400 * This is a threaded interrupt handler so we can make some
403 static irqreturn_t
ab3100_irq_handler(int irq
, void *data
)
405 struct ab3100
*ab3100
= data
;
410 add_interrupt_randomness(irq
);
412 err
= ab3100_get_register_page_interruptible(ab3100
, AB3100_EVENTA1
,
417 fatevent
= (event_regs
[0] << 16) |
418 (event_regs
[1] << 8) |
421 if (!ab3100
->startup_events_read
) {
422 ab3100
->startup_events
[0] = event_regs
[0];
423 ab3100
->startup_events
[1] = event_regs
[1];
424 ab3100
->startup_events
[2] = event_regs
[2];
425 ab3100
->startup_events_read
= true;
428 * The notified parties will have to mask out the events
429 * they're interested in and react to them. They will be
430 * notified on all events, then they use the fatevent value
431 * to determine if they're interested.
433 blocking_notifier_call_chain(&ab3100
->event_subscribers
,
437 "IRQ Event: 0x%08x\n", fatevent
);
443 "error reading event status\n");
447 #ifdef CONFIG_DEBUG_FS
449 * Some debugfs entries only exposed if we're using debug
451 static int ab3100_registers_print(struct seq_file
*s
, void *p
)
453 struct ab3100
*ab3100
= s
->private;
457 seq_printf(s
, "AB3100 registers:\n");
459 for (reg
= 0; reg
< 0xff; reg
++) {
460 ab3100_get_register_interruptible(ab3100
, reg
, &value
);
461 seq_printf(s
, "[0x%x]: 0x%x\n", reg
, value
);
466 static int ab3100_registers_open(struct inode
*inode
, struct file
*file
)
468 return single_open(file
, ab3100_registers_print
, inode
->i_private
);
471 static const struct file_operations ab3100_registers_fops
= {
472 .open
= ab3100_registers_open
,
475 .release
= single_release
,
476 .owner
= THIS_MODULE
,
479 struct ab3100_get_set_reg_priv
{
480 struct ab3100
*ab3100
;
484 static int ab3100_get_set_reg_open_file(struct inode
*inode
, struct file
*file
)
486 file
->private_data
= inode
->i_private
;
490 static ssize_t
ab3100_get_set_reg(struct file
*file
,
491 const char __user
*user_buf
,
492 size_t count
, loff_t
*ppos
)
494 struct ab3100_get_set_reg_priv
*priv
= file
->private_data
;
495 struct ab3100
*ab3100
= priv
->ab3100
;
499 unsigned long user_reg
;
503 /* Get userspace string and assure termination */
504 buf_size
= min(count
, (sizeof(buf
)-1));
505 if (copy_from_user(buf
, user_buf
, buf_size
))
510 * The idea is here to parse a string which is either
511 * "0xnn" for reading a register, or "0xaa 0xbb" for
512 * writing 0xbb to the register 0xaa. First move past
513 * whitespace and then begin to parse the register.
515 while ((i
< buf_size
) && (buf
[i
] == ' '))
520 * Advance pointer to end of string then terminate
521 * the register string. This is needed to satisfy
522 * the strict_strtoul() function.
524 while ((i
< buf_size
) && (buf
[i
] != ' '))
528 err
= strict_strtoul(&buf
[regp
], 16, &user_reg
);
534 /* Either we read or we write a register here */
537 u8 reg
= (u8
) user_reg
;
540 ab3100_get_register_interruptible(ab3100
, reg
, ®value
);
542 dev_info(ab3100
->dev
,
543 "debug read AB3100 reg[0x%02x]: 0x%02x\n",
547 unsigned long user_value
;
548 u8 reg
= (u8
) user_reg
;
553 * Writing, we need some value to write to
554 * the register so keep parsing the string
558 while ((i
< buf_size
) && (buf
[i
] == ' '))
561 while ((i
< buf_size
) && (buf
[i
] != ' '))
565 err
= strict_strtoul(&buf
[valp
], 16, &user_value
);
571 value
= (u8
) user_value
;
572 ab3100_set_register_interruptible(ab3100
, reg
, value
);
573 ab3100_get_register_interruptible(ab3100
, reg
, ®value
);
575 dev_info(ab3100
->dev
,
576 "debug write reg[0x%02x] with 0x%02x, "
577 "after readback: 0x%02x\n",
578 reg
, value
, regvalue
);
583 static const struct file_operations ab3100_get_set_reg_fops
= {
584 .open
= ab3100_get_set_reg_open_file
,
585 .write
= ab3100_get_set_reg
,
588 static struct dentry
*ab3100_dir
;
589 static struct dentry
*ab3100_reg_file
;
590 static struct ab3100_get_set_reg_priv ab3100_get_priv
;
591 static struct dentry
*ab3100_get_reg_file
;
592 static struct ab3100_get_set_reg_priv ab3100_set_priv
;
593 static struct dentry
*ab3100_set_reg_file
;
595 static void ab3100_setup_debugfs(struct ab3100
*ab3100
)
599 ab3100_dir
= debugfs_create_dir("ab3100", NULL
);
601 goto exit_no_debugfs
;
603 ab3100_reg_file
= debugfs_create_file("registers",
604 S_IRUGO
, ab3100_dir
, ab3100
,
605 &ab3100_registers_fops
);
606 if (!ab3100_reg_file
) {
608 goto exit_destroy_dir
;
611 ab3100_get_priv
.ab3100
= ab3100
;
612 ab3100_get_priv
.mode
= false;
613 ab3100_get_reg_file
= debugfs_create_file("get_reg",
614 S_IWUGO
, ab3100_dir
, &ab3100_get_priv
,
615 &ab3100_get_set_reg_fops
);
616 if (!ab3100_get_reg_file
) {
618 goto exit_destroy_reg
;
621 ab3100_set_priv
.ab3100
= ab3100
;
622 ab3100_set_priv
.mode
= true;
623 ab3100_set_reg_file
= debugfs_create_file("set_reg",
624 S_IWUGO
, ab3100_dir
, &ab3100_set_priv
,
625 &ab3100_get_set_reg_fops
);
626 if (!ab3100_set_reg_file
) {
628 goto exit_destroy_get_reg
;
632 exit_destroy_get_reg
:
633 debugfs_remove(ab3100_get_reg_file
);
635 debugfs_remove(ab3100_reg_file
);
637 debugfs_remove(ab3100_dir
);
641 static inline void ab3100_remove_debugfs(void)
643 debugfs_remove(ab3100_set_reg_file
);
644 debugfs_remove(ab3100_get_reg_file
);
645 debugfs_remove(ab3100_reg_file
);
646 debugfs_remove(ab3100_dir
);
649 static inline void ab3100_setup_debugfs(struct ab3100
*ab3100
)
652 static inline void ab3100_remove_debugfs(void)
658 * Basic set-up, datastructure creation/destruction and I2C interface.
659 * This sets up a default config in the AB3100 chip so that it
660 * will work as expected.
663 struct ab3100_init_setting
{
668 static const struct ab3100_init_setting __initconst
669 ab3100_init_settings
[] = {
677 .abreg
= AB3100_IMRA1
,
680 .abreg
= AB3100_IMRA2
,
683 .abreg
= AB3100_IMRA3
,
686 .abreg
= AB3100_IMRB1
,
689 .abreg
= AB3100_IMRB2
,
692 .abreg
= AB3100_IMRB3
,
715 static int __init
ab3100_setup(struct ab3100
*ab3100
)
720 for (i
= 0; i
< ARRAY_SIZE(ab3100_init_settings
); i
++) {
721 err
= ab3100_set_register_interruptible(ab3100
,
722 ab3100_init_settings
[i
].abreg
,
723 ab3100_init_settings
[i
].setting
);
729 * Special trick to make the AB3100 use the 32kHz clock (RTC)
730 * bit 3 in test register 0x02 is a special, undocumented test
731 * register bit that only exist in AB3100 P1E
733 if (ab3100
->chip_id
== 0xc4) {
734 dev_warn(ab3100
->dev
,
735 "AB3100 P1E variant detected, "
736 "forcing chip to 32KHz\n");
737 err
= ab3100_set_test_register_interruptible(ab3100
,
746 * Here we define all the platform devices that appear
747 * as children of the AB3100. These are regular platform
748 * devices with the IORESOURCE_IO .start and .end set
749 * to correspond to the internal AB3100 register range
750 * mapping to the corresponding subdevice.
753 #define AB3100_DEVICE(devname, devid) \
754 static struct platform_device ab3100_##devname##_device = { \
759 /* This lists all the subdevices */
760 AB3100_DEVICE(dac
, "ab3100-dac");
761 AB3100_DEVICE(leds
, "ab3100-leds");
762 AB3100_DEVICE(power
, "ab3100-power");
763 AB3100_DEVICE(regulators
, "ab3100-regulators");
764 AB3100_DEVICE(sim
, "ab3100-sim");
765 AB3100_DEVICE(uart
, "ab3100-uart");
766 AB3100_DEVICE(rtc
, "ab3100-rtc");
767 AB3100_DEVICE(charger
, "ab3100-charger");
768 AB3100_DEVICE(boost
, "ab3100-boost");
769 AB3100_DEVICE(adc
, "ab3100-adc");
770 AB3100_DEVICE(fuelgauge
, "ab3100-fuelgauge");
771 AB3100_DEVICE(vibrator
, "ab3100-vibrator");
772 AB3100_DEVICE(otp
, "ab3100-otp");
773 AB3100_DEVICE(codec
, "ab3100-codec");
775 static struct platform_device
*
776 ab3100_platform_devs
[] = {
779 &ab3100_power_device
,
780 &ab3100_regulators_device
,
784 &ab3100_charger_device
,
785 &ab3100_boost_device
,
787 &ab3100_fuelgauge_device
,
788 &ab3100_vibrator_device
,
790 &ab3100_codec_device
,
793 struct ab_family_id
{
798 static const struct ab_family_id ids
[] __initdata
= {
828 /* AB3000 variants, not supported */
852 static int __init
ab3100_probe(struct i2c_client
*client
,
853 const struct i2c_device_id
*id
)
855 struct ab3100
*ab3100
;
856 struct ab3100_platform_data
*ab3100_plf_data
=
857 client
->dev
.platform_data
;
861 ab3100
= kzalloc(sizeof(struct ab3100
), GFP_KERNEL
);
863 dev_err(&client
->dev
, "could not allocate AB3100 device\n");
867 /* Initialize data structure */
868 mutex_init(&ab3100
->access_mutex
);
869 BLOCKING_INIT_NOTIFIER_HEAD(&ab3100
->event_subscribers
);
871 ab3100
->i2c_client
= client
;
872 ab3100
->dev
= &ab3100
->i2c_client
->dev
;
874 i2c_set_clientdata(client
, ab3100
);
876 /* Read chip ID register */
877 err
= ab3100_get_register_interruptible(ab3100
, AB3100_CID
,
880 dev_err(&client
->dev
,
881 "could not communicate with the AB3100 analog "
886 for (i
= 0; ids
[i
].id
!= 0x0; i
++) {
887 if (ids
[i
].id
== ab3100
->chip_id
) {
888 if (ids
[i
].name
!= NULL
) {
889 snprintf(&ab3100
->chip_name
[0],
890 sizeof(ab3100
->chip_name
) - 1,
895 dev_err(&client
->dev
,
896 "AB3000 is not supported\n");
902 if (ids
[i
].id
== 0x0) {
903 dev_err(&client
->dev
, "unknown analog baseband chip id: 0x%x\n",
905 dev_err(&client
->dev
, "accepting it anyway. Please update "
910 dev_info(&client
->dev
, "Detected chip: %s\n",
911 &ab3100
->chip_name
[0]);
913 /* Attach a second dummy i2c_client to the test register address */
914 ab3100
->testreg_client
= i2c_new_dummy(client
->adapter
,
916 if (!ab3100
->testreg_client
) {
918 goto exit_no_testreg_client
;
921 err
= ab3100_setup(ab3100
);
925 err
= request_threaded_irq(client
->irq
, NULL
, ab3100_irq_handler
,
926 IRQF_ONESHOT
, "ab3100-core", ab3100
);
927 /* This real unpredictable IRQ is of course sampled for entropy */
928 rand_initialize_irq(client
->irq
);
933 err
= abx500_register_ops(&client
->dev
, &ab3100_ops
);
937 /* Set parent and a pointer back to the container in device data */
938 for (i
= 0; i
< ARRAY_SIZE(ab3100_platform_devs
); i
++) {
939 ab3100_platform_devs
[i
]->dev
.parent
=
941 ab3100_platform_devs
[i
]->dev
.platform_data
=
943 platform_set_drvdata(ab3100_platform_devs
[i
], ab3100
);
946 /* Register the platform devices */
947 platform_add_devices(ab3100_platform_devs
,
948 ARRAY_SIZE(ab3100_platform_devs
));
950 ab3100_setup_debugfs(ab3100
);
957 i2c_unregister_device(ab3100
->testreg_client
);
958 exit_no_testreg_client
:
964 static int __exit
ab3100_remove(struct i2c_client
*client
)
966 struct ab3100
*ab3100
= i2c_get_clientdata(client
);
969 /* Unregister subdevices */
970 for (i
= 0; i
< ARRAY_SIZE(ab3100_platform_devs
); i
++)
971 platform_device_unregister(ab3100_platform_devs
[i
]);
973 ab3100_remove_debugfs();
974 i2c_unregister_device(ab3100
->testreg_client
);
977 * At this point, all subscribers should have unregistered
978 * their notifiers so deactivate IRQ
980 free_irq(client
->irq
, ab3100
);
985 static const struct i2c_device_id ab3100_id
[] = {
989 MODULE_DEVICE_TABLE(i2c
, ab3100_id
);
991 static struct i2c_driver ab3100_driver
= {
994 .owner
= THIS_MODULE
,
996 .id_table
= ab3100_id
,
997 .probe
= ab3100_probe
,
998 .remove
= __exit_p(ab3100_remove
),
1001 static int __init
ab3100_i2c_init(void)
1003 return i2c_add_driver(&ab3100_driver
);
1006 static void __exit
ab3100_i2c_exit(void)
1008 i2c_del_driver(&ab3100_driver
);
1011 subsys_initcall(ab3100_i2c_init
);
1012 module_exit(ab3100_i2c_exit
);
1014 MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
1015 MODULE_DESCRIPTION("AB3100 core driver");
1016 MODULE_LICENSE("GPL");