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/core.h>
23 #include <linux/mfd/abx500.h>
25 /* These are the only registers inside AB3100 used in this main file */
27 /* Interrupt event registers */
28 #define AB3100_EVENTA1 0x21
29 #define AB3100_EVENTA2 0x22
30 #define AB3100_EVENTA3 0x23
32 /* AB3100 DAC converter registers */
33 #define AB3100_DIS 0x00
34 #define AB3100_D0C 0x01
35 #define AB3100_D1C 0x02
36 #define AB3100_D2C 0x03
37 #define AB3100_D3C 0x04
39 /* Chip ID register */
40 #define AB3100_CID 0x20
42 /* AB3100 interrupt registers */
43 #define AB3100_IMRA1 0x24
44 #define AB3100_IMRA2 0x25
45 #define AB3100_IMRA3 0x26
46 #define AB3100_IMRB1 0x2B
47 #define AB3100_IMRB2 0x2C
48 #define AB3100_IMRB3 0x2D
50 /* System Power Monitoring and control registers */
51 #define AB3100_MCA 0x2E
52 #define AB3100_MCB 0x2F
55 #define AB3100_SUP 0x50
60 * The AB3100 is usually assigned address 0x48 (7-bit)
61 * The chip is defined in the platform i2c_board_data section.
63 static int ab3100_get_chip_id(struct device
*dev
)
65 struct ab3100
*ab3100
= dev_get_drvdata(dev
->parent
);
67 return (int)ab3100
->chip_id
;
70 static int ab3100_set_register_interruptible(struct ab3100
*ab3100
,
73 u8 regandval
[2] = {reg
, regval
};
76 err
= mutex_lock_interruptible(&ab3100
->access_mutex
);
81 * A two-byte write message with the first byte containing the register
82 * number and the second byte containing the value to be written
83 * effectively sets a register in the AB3100.
85 err
= i2c_master_send(ab3100
->i2c_client
, regandval
, 2);
88 "write error (write register): %d\n",
90 } else if (err
!= 2) {
92 "write error (write register) "
93 "%d bytes transferred (expected 2)\n",
100 mutex_unlock(&ab3100
->access_mutex
);
104 static int set_register_interruptible(struct device
*dev
,
105 u8 bank
, u8 reg
, u8 value
)
107 struct ab3100
*ab3100
= dev_get_drvdata(dev
->parent
);
109 return ab3100_set_register_interruptible(ab3100
, reg
, value
);
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
);
149 static int ab3100_get_register_interruptible(struct ab3100
*ab3100
,
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
);
205 static int get_register_interruptible(struct device
*dev
, u8 bank
, u8 reg
,
208 struct ab3100
*ab3100
= dev_get_drvdata(dev
->parent
);
210 return ab3100_get_register_interruptible(ab3100
, reg
, value
);
213 static int ab3100_get_register_page_interruptible(struct ab3100
*ab3100
,
214 u8 first_reg
, u8
*regvals
, u8 numregs
)
218 if (ab3100
->chip_id
== 0xa0 ||
219 ab3100
->chip_id
== 0xa1)
220 /* These don't support paged reads */
223 err
= mutex_lock_interruptible(&ab3100
->access_mutex
);
228 * Paged read also require an I2C "stop" command.
230 err
= i2c_master_send(ab3100
->i2c_client
, &first_reg
, 1);
233 "write error (send first register address): %d\n",
235 goto get_reg_page_out_unlock
;
236 } else if (err
!= 1) {
238 "write error (send first register address) "
239 "%d bytes transferred (expected 1)\n",
242 goto get_reg_page_out_unlock
;
245 err
= i2c_master_recv(ab3100
->i2c_client
, regvals
, numregs
);
248 "write error (read register page): %d\n",
250 goto get_reg_page_out_unlock
;
251 } else if (err
!= numregs
) {
253 "write error (read register page) "
254 "%d bytes transferred (expected %d)\n",
257 goto get_reg_page_out_unlock
;
263 get_reg_page_out_unlock
:
264 mutex_unlock(&ab3100
->access_mutex
);
268 static int get_register_page_interruptible(struct device
*dev
, u8 bank
,
269 u8 first_reg
, u8
*regvals
, u8 numregs
)
271 struct ab3100
*ab3100
= dev_get_drvdata(dev
->parent
);
273 return ab3100_get_register_page_interruptible(ab3100
,
274 first_reg
, regvals
, numregs
);
277 static int ab3100_mask_and_set_register_interruptible(struct ab3100
*ab3100
,
278 u8 reg
, u8 andmask
, u8 ormask
)
280 u8 regandval
[2] = {reg
, 0};
283 err
= mutex_lock_interruptible(&ab3100
->access_mutex
);
287 /* First read out the target register */
288 err
= i2c_master_send(ab3100
->i2c_client
, ®
, 1);
291 "write error (maskset send address): %d\n",
293 goto get_maskset_unlock
;
294 } else if (err
!= 1) {
296 "write error (maskset send address) "
297 "%d bytes transferred (expected 1)\n",
300 goto get_maskset_unlock
;
303 err
= i2c_master_recv(ab3100
->i2c_client
, ®andval
[1], 1);
306 "write error (maskset read register): %d\n",
308 goto get_maskset_unlock
;
309 } else if (err
!= 1) {
311 "write error (maskset read register) "
312 "%d bytes transferred (expected 1)\n",
315 goto get_maskset_unlock
;
318 /* Modify the register */
319 regandval
[1] &= andmask
;
320 regandval
[1] |= ormask
;
322 /* Write the register */
323 err
= i2c_master_send(ab3100
->i2c_client
, regandval
, 2);
326 "write error (write register): %d\n",
328 goto get_maskset_unlock
;
329 } else if (err
!= 2) {
331 "write error (write register) "
332 "%d bytes transferred (expected 2)\n",
335 goto get_maskset_unlock
;
342 mutex_unlock(&ab3100
->access_mutex
);
346 static int mask_and_set_register_interruptible(struct device
*dev
, u8 bank
,
347 u8 reg
, u8 bitmask
, u8 bitvalues
)
349 struct ab3100
*ab3100
= dev_get_drvdata(dev
->parent
);
351 return ab3100_mask_and_set_register_interruptible(ab3100
,
352 reg
, bitmask
, (bitmask
& bitvalues
));
356 * Register a simple callback for handling any AB3100 events.
358 int ab3100_event_register(struct ab3100
*ab3100
,
359 struct notifier_block
*nb
)
361 return blocking_notifier_chain_register(&ab3100
->event_subscribers
,
364 EXPORT_SYMBOL(ab3100_event_register
);
367 * Remove a previously registered callback.
369 int ab3100_event_unregister(struct ab3100
*ab3100
,
370 struct notifier_block
*nb
)
372 return blocking_notifier_chain_unregister(&ab3100
->event_subscribers
,
375 EXPORT_SYMBOL(ab3100_event_unregister
);
378 static int ab3100_event_registers_startup_state_get(struct device
*dev
,
381 struct ab3100
*ab3100
= dev_get_drvdata(dev
->parent
);
382 if (!ab3100
->startup_events_read
)
383 return -EAGAIN
; /* Try again later */
384 memcpy(event
, ab3100
->startup_events
, 3);
388 static struct abx500_ops ab3100_ops
= {
389 .get_chip_id
= ab3100_get_chip_id
,
390 .set_register
= set_register_interruptible
,
391 .get_register
= get_register_interruptible
,
392 .get_register_page
= get_register_page_interruptible
,
393 .set_register_page
= NULL
,
394 .mask_and_set_register
= mask_and_set_register_interruptible
,
395 .event_registers_startup_state_get
=
396 ab3100_event_registers_startup_state_get
,
397 .startup_irq_enabled
= NULL
,
401 * This is a threaded interrupt handler so we can make some
404 static irqreturn_t
ab3100_irq_handler(int irq
, void *data
)
406 struct ab3100
*ab3100
= data
;
411 add_interrupt_randomness(irq
);
413 err
= ab3100_get_register_page_interruptible(ab3100
, AB3100_EVENTA1
,
418 fatevent
= (event_regs
[0] << 16) |
419 (event_regs
[1] << 8) |
422 if (!ab3100
->startup_events_read
) {
423 ab3100
->startup_events
[0] = event_regs
[0];
424 ab3100
->startup_events
[1] = event_regs
[1];
425 ab3100
->startup_events
[2] = event_regs
[2];
426 ab3100
->startup_events_read
= true;
429 * The notified parties will have to mask out the events
430 * they're interested in and react to them. They will be
431 * notified on all events, then they use the fatevent value
432 * to determine if they're interested.
434 blocking_notifier_call_chain(&ab3100
->event_subscribers
,
438 "IRQ Event: 0x%08x\n", fatevent
);
444 "error reading event status\n");
448 #ifdef CONFIG_DEBUG_FS
450 * Some debugfs entries only exposed if we're using debug
452 static int ab3100_registers_print(struct seq_file
*s
, void *p
)
454 struct ab3100
*ab3100
= s
->private;
458 seq_printf(s
, "AB3100 registers:\n");
460 for (reg
= 0; reg
< 0xff; reg
++) {
461 ab3100_get_register_interruptible(ab3100
, reg
, &value
);
462 seq_printf(s
, "[0x%x]: 0x%x\n", reg
, value
);
467 static int ab3100_registers_open(struct inode
*inode
, struct file
*file
)
469 return single_open(file
, ab3100_registers_print
, inode
->i_private
);
472 static const struct file_operations ab3100_registers_fops
= {
473 .open
= ab3100_registers_open
,
476 .release
= single_release
,
477 .owner
= THIS_MODULE
,
480 struct ab3100_get_set_reg_priv
{
481 struct ab3100
*ab3100
;
485 static int ab3100_get_set_reg_open_file(struct inode
*inode
, struct file
*file
)
487 file
->private_data
= inode
->i_private
;
491 static ssize_t
ab3100_get_set_reg(struct file
*file
,
492 const char __user
*user_buf
,
493 size_t count
, loff_t
*ppos
)
495 struct ab3100_get_set_reg_priv
*priv
= file
->private_data
;
496 struct ab3100
*ab3100
= priv
->ab3100
;
500 unsigned long user_reg
;
504 /* Get userspace string and assure termination */
505 buf_size
= min(count
, (sizeof(buf
)-1));
506 if (copy_from_user(buf
, user_buf
, buf_size
))
511 * The idea is here to parse a string which is either
512 * "0xnn" for reading a register, or "0xaa 0xbb" for
513 * writing 0xbb to the register 0xaa. First move past
514 * whitespace and then begin to parse the register.
516 while ((i
< buf_size
) && (buf
[i
] == ' '))
521 * Advance pointer to end of string then terminate
522 * the register string. This is needed to satisfy
523 * the strict_strtoul() function.
525 while ((i
< buf_size
) && (buf
[i
] != ' '))
529 err
= strict_strtoul(&buf
[regp
], 16, &user_reg
);
535 /* Either we read or we write a register here */
538 u8 reg
= (u8
) user_reg
;
541 ab3100_get_register_interruptible(ab3100
, reg
, ®value
);
543 dev_info(ab3100
->dev
,
544 "debug read AB3100 reg[0x%02x]: 0x%02x\n",
548 unsigned long user_value
;
549 u8 reg
= (u8
) user_reg
;
554 * Writing, we need some value to write to
555 * the register so keep parsing the string
559 while ((i
< buf_size
) && (buf
[i
] == ' '))
562 while ((i
< buf_size
) && (buf
[i
] != ' '))
566 err
= strict_strtoul(&buf
[valp
], 16, &user_value
);
572 value
= (u8
) user_value
;
573 ab3100_set_register_interruptible(ab3100
, reg
, value
);
574 ab3100_get_register_interruptible(ab3100
, reg
, ®value
);
576 dev_info(ab3100
->dev
,
577 "debug write reg[0x%02x] with 0x%02x, "
578 "after readback: 0x%02x\n",
579 reg
, value
, regvalue
);
584 static const struct file_operations ab3100_get_set_reg_fops
= {
585 .open
= ab3100_get_set_reg_open_file
,
586 .write
= ab3100_get_set_reg
,
587 .llseek
= noop_llseek
,
590 static struct dentry
*ab3100_dir
;
591 static struct dentry
*ab3100_reg_file
;
592 static struct ab3100_get_set_reg_priv ab3100_get_priv
;
593 static struct dentry
*ab3100_get_reg_file
;
594 static struct ab3100_get_set_reg_priv ab3100_set_priv
;
595 static struct dentry
*ab3100_set_reg_file
;
597 static void ab3100_setup_debugfs(struct ab3100
*ab3100
)
601 ab3100_dir
= debugfs_create_dir("ab3100", NULL
);
603 goto exit_no_debugfs
;
605 ab3100_reg_file
= debugfs_create_file("registers",
606 S_IRUGO
, ab3100_dir
, ab3100
,
607 &ab3100_registers_fops
);
608 if (!ab3100_reg_file
) {
610 goto exit_destroy_dir
;
613 ab3100_get_priv
.ab3100
= ab3100
;
614 ab3100_get_priv
.mode
= false;
615 ab3100_get_reg_file
= debugfs_create_file("get_reg",
616 S_IWUGO
, ab3100_dir
, &ab3100_get_priv
,
617 &ab3100_get_set_reg_fops
);
618 if (!ab3100_get_reg_file
) {
620 goto exit_destroy_reg
;
623 ab3100_set_priv
.ab3100
= ab3100
;
624 ab3100_set_priv
.mode
= true;
625 ab3100_set_reg_file
= debugfs_create_file("set_reg",
626 S_IWUGO
, ab3100_dir
, &ab3100_set_priv
,
627 &ab3100_get_set_reg_fops
);
628 if (!ab3100_set_reg_file
) {
630 goto exit_destroy_get_reg
;
634 exit_destroy_get_reg
:
635 debugfs_remove(ab3100_get_reg_file
);
637 debugfs_remove(ab3100_reg_file
);
639 debugfs_remove(ab3100_dir
);
643 static inline void ab3100_remove_debugfs(void)
645 debugfs_remove(ab3100_set_reg_file
);
646 debugfs_remove(ab3100_get_reg_file
);
647 debugfs_remove(ab3100_reg_file
);
648 debugfs_remove(ab3100_dir
);
651 static inline void ab3100_setup_debugfs(struct ab3100
*ab3100
)
654 static inline void ab3100_remove_debugfs(void)
660 * Basic set-up, datastructure creation/destruction and I2C interface.
661 * This sets up a default config in the AB3100 chip so that it
662 * will work as expected.
665 struct ab3100_init_setting
{
670 static const struct ab3100_init_setting __devinitconst
671 ab3100_init_settings
[] = {
679 .abreg
= AB3100_IMRA1
,
682 .abreg
= AB3100_IMRA2
,
685 .abreg
= AB3100_IMRA3
,
688 .abreg
= AB3100_IMRB1
,
691 .abreg
= AB3100_IMRB2
,
694 .abreg
= AB3100_IMRB3
,
717 static int __devinit
ab3100_setup(struct ab3100
*ab3100
)
722 for (i
= 0; i
< ARRAY_SIZE(ab3100_init_settings
); i
++) {
723 err
= ab3100_set_register_interruptible(ab3100
,
724 ab3100_init_settings
[i
].abreg
,
725 ab3100_init_settings
[i
].setting
);
731 * Special trick to make the AB3100 use the 32kHz clock (RTC)
732 * bit 3 in test register 0x02 is a special, undocumented test
733 * register bit that only exist in AB3100 P1E
735 if (ab3100
->chip_id
== 0xc4) {
736 dev_warn(ab3100
->dev
,
737 "AB3100 P1E variant detected, "
738 "forcing chip to 32KHz\n");
739 err
= ab3100_set_test_register_interruptible(ab3100
,
747 /* The subdevices of the AB3100 */
748 static struct mfd_cell ab3100_devs
[] = {
750 .name
= "ab3100-dac",
754 .name
= "ab3100-leds",
758 .name
= "ab3100-power",
762 .name
= "ab3100-regulators",
766 .name
= "ab3100-sim",
770 .name
= "ab3100-uart",
774 .name
= "ab3100-rtc",
778 .name
= "ab3100-charger",
782 .name
= "ab3100-boost",
786 .name
= "ab3100-adc",
790 .name
= "ab3100-fuelgauge",
794 .name
= "ab3100-vibrator",
798 .name
= "ab3100-otp",
802 .name
= "ab3100-codec",
807 struct ab_family_id
{
812 static const struct ab_family_id ids
[] __devinitdata
= {
842 /* AB3000 variants, not supported */
866 static int __devinit
ab3100_probe(struct i2c_client
*client
,
867 const struct i2c_device_id
*id
)
869 struct ab3100
*ab3100
;
870 struct ab3100_platform_data
*ab3100_plf_data
=
871 client
->dev
.platform_data
;
875 ab3100
= kzalloc(sizeof(struct ab3100
), GFP_KERNEL
);
877 dev_err(&client
->dev
, "could not allocate AB3100 device\n");
881 /* Initialize data structure */
882 mutex_init(&ab3100
->access_mutex
);
883 BLOCKING_INIT_NOTIFIER_HEAD(&ab3100
->event_subscribers
);
885 ab3100
->i2c_client
= client
;
886 ab3100
->dev
= &ab3100
->i2c_client
->dev
;
888 i2c_set_clientdata(client
, ab3100
);
890 /* Read chip ID register */
891 err
= ab3100_get_register_interruptible(ab3100
, AB3100_CID
,
894 dev_err(&client
->dev
,
895 "could not communicate with the AB3100 analog "
900 for (i
= 0; ids
[i
].id
!= 0x0; i
++) {
901 if (ids
[i
].id
== ab3100
->chip_id
) {
902 if (ids
[i
].name
!= NULL
) {
903 snprintf(&ab3100
->chip_name
[0],
904 sizeof(ab3100
->chip_name
) - 1,
909 dev_err(&client
->dev
,
910 "AB3000 is not supported\n");
916 if (ids
[i
].id
== 0x0) {
917 dev_err(&client
->dev
, "unknown analog baseband chip id: 0x%x\n",
919 dev_err(&client
->dev
, "accepting it anyway. Please update "
924 dev_info(&client
->dev
, "Detected chip: %s\n",
925 &ab3100
->chip_name
[0]);
927 /* Attach a second dummy i2c_client to the test register address */
928 ab3100
->testreg_client
= i2c_new_dummy(client
->adapter
,
930 if (!ab3100
->testreg_client
) {
932 goto exit_no_testreg_client
;
935 err
= ab3100_setup(ab3100
);
939 err
= request_threaded_irq(client
->irq
, NULL
, ab3100_irq_handler
,
940 IRQF_ONESHOT
, "ab3100-core", ab3100
);
941 /* This real unpredictable IRQ is of course sampled for entropy */
942 rand_initialize_irq(client
->irq
);
947 err
= abx500_register_ops(&client
->dev
, &ab3100_ops
);
951 /* Set up and register the platform devices. */
952 for (i
= 0; i
< ARRAY_SIZE(ab3100_devs
); i
++) {
953 ab3100_devs
[i
].platform_data
= ab3100_plf_data
;
954 ab3100_devs
[i
].data_size
= sizeof(struct ab3100_platform_data
);
957 err
= mfd_add_devices(&client
->dev
, 0, ab3100_devs
,
958 ARRAY_SIZE(ab3100_devs
), NULL
, 0);
960 ab3100_setup_debugfs(ab3100
);
967 i2c_unregister_device(ab3100
->testreg_client
);
968 exit_no_testreg_client
:
974 static int __devexit
ab3100_remove(struct i2c_client
*client
)
976 struct ab3100
*ab3100
= i2c_get_clientdata(client
);
978 /* Unregister subdevices */
979 mfd_remove_devices(&client
->dev
);
981 ab3100_remove_debugfs();
982 i2c_unregister_device(ab3100
->testreg_client
);
985 * At this point, all subscribers should have unregistered
986 * their notifiers so deactivate IRQ
988 free_irq(client
->irq
, ab3100
);
993 static const struct i2c_device_id ab3100_id
[] = {
997 MODULE_DEVICE_TABLE(i2c
, ab3100_id
);
999 static struct i2c_driver ab3100_driver
= {
1002 .owner
= THIS_MODULE
,
1004 .id_table
= ab3100_id
,
1005 .probe
= ab3100_probe
,
1006 .remove
= __devexit_p(ab3100_remove
),
1009 static int __init
ab3100_i2c_init(void)
1011 return i2c_add_driver(&ab3100_driver
);
1014 static void __exit
ab3100_i2c_exit(void)
1016 i2c_del_driver(&ab3100_driver
);
1019 subsys_initcall(ab3100_i2c_init
);
1020 module_exit(ab3100_i2c_exit
);
1022 MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
1023 MODULE_DESCRIPTION("AB3100 core driver");
1024 MODULE_LICENSE("GPL");