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/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/device.h>
18 #include <linux/interrupt.h>
19 #include <linux/random.h>
20 #include <linux/debugfs.h>
21 #include <linux/seq_file.h>
22 #include <linux/uaccess.h>
23 #include <linux/mfd/core.h>
24 #include <linux/mfd/abx500.h>
26 /* These are the only registers inside AB3100 used in this main file */
28 /* Interrupt event registers */
29 #define AB3100_EVENTA1 0x21
30 #define AB3100_EVENTA2 0x22
31 #define AB3100_EVENTA3 0x23
33 /* AB3100 DAC converter registers */
34 #define AB3100_DIS 0x00
35 #define AB3100_D0C 0x01
36 #define AB3100_D1C 0x02
37 #define AB3100_D2C 0x03
38 #define AB3100_D3C 0x04
40 /* Chip ID register */
41 #define AB3100_CID 0x20
43 /* AB3100 interrupt registers */
44 #define AB3100_IMRA1 0x24
45 #define AB3100_IMRA2 0x25
46 #define AB3100_IMRA3 0x26
47 #define AB3100_IMRB1 0x2B
48 #define AB3100_IMRB2 0x2C
49 #define AB3100_IMRB3 0x2D
51 /* System Power Monitoring and control registers */
52 #define AB3100_MCA 0x2E
53 #define AB3100_MCB 0x2F
56 #define AB3100_SUP 0x50
61 * The AB3100 is usually assigned address 0x48 (7-bit)
62 * The chip is defined in the platform i2c_board_data section.
64 static int ab3100_get_chip_id(struct device
*dev
)
66 struct ab3100
*ab3100
= dev_get_drvdata(dev
->parent
);
68 return (int)ab3100
->chip_id
;
71 static int ab3100_set_register_interruptible(struct ab3100
*ab3100
,
74 u8 regandval
[2] = {reg
, regval
};
77 err
= mutex_lock_interruptible(&ab3100
->access_mutex
);
82 * A two-byte write message with the first byte containing the register
83 * number and the second byte containing the value to be written
84 * effectively sets a register in the AB3100.
86 err
= i2c_master_send(ab3100
->i2c_client
, regandval
, 2);
89 "write error (write register): %d\n",
91 } else if (err
!= 2) {
93 "write error (write register) "
94 "%d bytes transferred (expected 2)\n",
101 mutex_unlock(&ab3100
->access_mutex
);
105 static int set_register_interruptible(struct device
*dev
,
106 u8 bank
, u8 reg
, u8 value
)
108 struct ab3100
*ab3100
= dev_get_drvdata(dev
->parent
);
110 return ab3100_set_register_interruptible(ab3100
, reg
, value
);
114 * The test registers exist at an I2C bus address up one
115 * from the ordinary base. They are not supposed to be used
116 * in production code, but sometimes you have to do that
117 * anyway. It's currently only used from this file so declare
118 * it static and do not export.
120 static int ab3100_set_test_register_interruptible(struct ab3100
*ab3100
,
123 u8 regandval
[2] = {reg
, regval
};
126 err
= mutex_lock_interruptible(&ab3100
->access_mutex
);
130 err
= i2c_master_send(ab3100
->testreg_client
, regandval
, 2);
133 "write error (write test register): %d\n",
135 } else if (err
!= 2) {
137 "write error (write test register) "
138 "%d bytes transferred (expected 2)\n",
145 mutex_unlock(&ab3100
->access_mutex
);
150 static int ab3100_get_register_interruptible(struct ab3100
*ab3100
,
155 err
= mutex_lock_interruptible(&ab3100
->access_mutex
);
160 * AB3100 require an I2C "stop" command between each message, else
161 * it will not work. The only way of achieveing this with the
162 * message transport layer is to send the read and write messages
165 err
= i2c_master_send(ab3100
->i2c_client
, ®
, 1);
168 "write error (send register address): %d\n",
170 goto get_reg_out_unlock
;
171 } else if (err
!= 1) {
173 "write error (send register address) "
174 "%d bytes transferred (expected 1)\n",
177 goto get_reg_out_unlock
;
183 err
= i2c_master_recv(ab3100
->i2c_client
, regval
, 1);
186 "write error (read register): %d\n",
188 goto get_reg_out_unlock
;
189 } else if (err
!= 1) {
191 "write error (read register) "
192 "%d bytes transferred (expected 1)\n",
195 goto get_reg_out_unlock
;
202 mutex_unlock(&ab3100
->access_mutex
);
206 static int get_register_interruptible(struct device
*dev
, u8 bank
, u8 reg
,
209 struct ab3100
*ab3100
= dev_get_drvdata(dev
->parent
);
211 return ab3100_get_register_interruptible(ab3100
, reg
, value
);
214 static int ab3100_get_register_page_interruptible(struct ab3100
*ab3100
,
215 u8 first_reg
, u8
*regvals
, u8 numregs
)
219 if (ab3100
->chip_id
== 0xa0 ||
220 ab3100
->chip_id
== 0xa1)
221 /* These don't support paged reads */
224 err
= mutex_lock_interruptible(&ab3100
->access_mutex
);
229 * Paged read also require an I2C "stop" command.
231 err
= i2c_master_send(ab3100
->i2c_client
, &first_reg
, 1);
234 "write error (send first register address): %d\n",
236 goto get_reg_page_out_unlock
;
237 } else if (err
!= 1) {
239 "write error (send first register address) "
240 "%d bytes transferred (expected 1)\n",
243 goto get_reg_page_out_unlock
;
246 err
= i2c_master_recv(ab3100
->i2c_client
, regvals
, numregs
);
249 "write error (read register page): %d\n",
251 goto get_reg_page_out_unlock
;
252 } else if (err
!= numregs
) {
254 "write error (read register page) "
255 "%d bytes transferred (expected %d)\n",
258 goto get_reg_page_out_unlock
;
264 get_reg_page_out_unlock
:
265 mutex_unlock(&ab3100
->access_mutex
);
269 static int get_register_page_interruptible(struct device
*dev
, u8 bank
,
270 u8 first_reg
, u8
*regvals
, u8 numregs
)
272 struct ab3100
*ab3100
= dev_get_drvdata(dev
->parent
);
274 return ab3100_get_register_page_interruptible(ab3100
,
275 first_reg
, regvals
, numregs
);
278 static int ab3100_mask_and_set_register_interruptible(struct ab3100
*ab3100
,
279 u8 reg
, u8 andmask
, u8 ormask
)
281 u8 regandval
[2] = {reg
, 0};
284 err
= mutex_lock_interruptible(&ab3100
->access_mutex
);
288 /* First read out the target register */
289 err
= i2c_master_send(ab3100
->i2c_client
, ®
, 1);
292 "write error (maskset send address): %d\n",
294 goto get_maskset_unlock
;
295 } else if (err
!= 1) {
297 "write error (maskset send address) "
298 "%d bytes transferred (expected 1)\n",
301 goto get_maskset_unlock
;
304 err
= i2c_master_recv(ab3100
->i2c_client
, ®andval
[1], 1);
307 "write error (maskset read register): %d\n",
309 goto get_maskset_unlock
;
310 } else if (err
!= 1) {
312 "write error (maskset read register) "
313 "%d bytes transferred (expected 1)\n",
316 goto get_maskset_unlock
;
319 /* Modify the register */
320 regandval
[1] &= andmask
;
321 regandval
[1] |= ormask
;
323 /* Write the register */
324 err
= i2c_master_send(ab3100
->i2c_client
, regandval
, 2);
327 "write error (write register): %d\n",
329 goto get_maskset_unlock
;
330 } else if (err
!= 2) {
332 "write error (write register) "
333 "%d bytes transferred (expected 2)\n",
336 goto get_maskset_unlock
;
343 mutex_unlock(&ab3100
->access_mutex
);
347 static int mask_and_set_register_interruptible(struct device
*dev
, u8 bank
,
348 u8 reg
, u8 bitmask
, u8 bitvalues
)
350 struct ab3100
*ab3100
= dev_get_drvdata(dev
->parent
);
352 return ab3100_mask_and_set_register_interruptible(ab3100
,
353 reg
, bitmask
, (bitmask
& bitvalues
));
357 * Register a simple callback for handling any AB3100 events.
359 int ab3100_event_register(struct ab3100
*ab3100
,
360 struct notifier_block
*nb
)
362 return blocking_notifier_chain_register(&ab3100
->event_subscribers
,
365 EXPORT_SYMBOL(ab3100_event_register
);
368 * Remove a previously registered callback.
370 int ab3100_event_unregister(struct ab3100
*ab3100
,
371 struct notifier_block
*nb
)
373 return blocking_notifier_chain_unregister(&ab3100
->event_subscribers
,
376 EXPORT_SYMBOL(ab3100_event_unregister
);
379 static int ab3100_event_registers_startup_state_get(struct device
*dev
,
382 struct ab3100
*ab3100
= dev_get_drvdata(dev
->parent
);
383 if (!ab3100
->startup_events_read
)
384 return -EAGAIN
; /* Try again later */
385 memcpy(event
, ab3100
->startup_events
, 3);
389 static struct abx500_ops ab3100_ops
= {
390 .get_chip_id
= ab3100_get_chip_id
,
391 .set_register
= set_register_interruptible
,
392 .get_register
= get_register_interruptible
,
393 .get_register_page
= get_register_page_interruptible
,
394 .set_register_page
= NULL
,
395 .mask_and_set_register
= mask_and_set_register_interruptible
,
396 .event_registers_startup_state_get
=
397 ab3100_event_registers_startup_state_get
,
398 .startup_irq_enabled
= NULL
,
402 * This is a threaded interrupt handler so we can make some
405 static irqreturn_t
ab3100_irq_handler(int irq
, void *data
)
407 struct ab3100
*ab3100
= data
;
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 ssize_t
ab3100_get_set_reg(struct file
*file
,
485 const char __user
*user_buf
,
486 size_t count
, loff_t
*ppos
)
488 struct ab3100_get_set_reg_priv
*priv
= file
->private_data
;
489 struct ab3100
*ab3100
= priv
->ab3100
;
493 unsigned long user_reg
;
497 /* Get userspace string and assure termination */
498 buf_size
= min(count
, (sizeof(buf
)-1));
499 if (copy_from_user(buf
, user_buf
, buf_size
))
504 * The idea is here to parse a string which is either
505 * "0xnn" for reading a register, or "0xaa 0xbb" for
506 * writing 0xbb to the register 0xaa. First move past
507 * whitespace and then begin to parse the register.
509 while ((i
< buf_size
) && (buf
[i
] == ' '))
514 * Advance pointer to end of string then terminate
515 * the register string. This is needed to satisfy
516 * the strict_strtoul() function.
518 while ((i
< buf_size
) && (buf
[i
] != ' '))
522 err
= strict_strtoul(&buf
[regp
], 16, &user_reg
);
528 /* Either we read or we write a register here */
531 u8 reg
= (u8
) user_reg
;
534 ab3100_get_register_interruptible(ab3100
, reg
, ®value
);
536 dev_info(ab3100
->dev
,
537 "debug read AB3100 reg[0x%02x]: 0x%02x\n",
541 unsigned long user_value
;
542 u8 reg
= (u8
) user_reg
;
547 * Writing, we need some value to write to
548 * the register so keep parsing the string
552 while ((i
< buf_size
) && (buf
[i
] == ' '))
555 while ((i
< buf_size
) && (buf
[i
] != ' '))
559 err
= strict_strtoul(&buf
[valp
], 16, &user_value
);
565 value
= (u8
) user_value
;
566 ab3100_set_register_interruptible(ab3100
, reg
, value
);
567 ab3100_get_register_interruptible(ab3100
, reg
, ®value
);
569 dev_info(ab3100
->dev
,
570 "debug write reg[0x%02x] with 0x%02x, "
571 "after readback: 0x%02x\n",
572 reg
, value
, regvalue
);
577 static const struct file_operations ab3100_get_set_reg_fops
= {
579 .write
= ab3100_get_set_reg
,
580 .llseek
= noop_llseek
,
583 static struct dentry
*ab3100_dir
;
584 static struct dentry
*ab3100_reg_file
;
585 static struct ab3100_get_set_reg_priv ab3100_get_priv
;
586 static struct dentry
*ab3100_get_reg_file
;
587 static struct ab3100_get_set_reg_priv ab3100_set_priv
;
588 static struct dentry
*ab3100_set_reg_file
;
590 static void ab3100_setup_debugfs(struct ab3100
*ab3100
)
594 ab3100_dir
= debugfs_create_dir("ab3100", NULL
);
596 goto exit_no_debugfs
;
598 ab3100_reg_file
= debugfs_create_file("registers",
599 S_IRUGO
, ab3100_dir
, ab3100
,
600 &ab3100_registers_fops
);
601 if (!ab3100_reg_file
) {
603 goto exit_destroy_dir
;
606 ab3100_get_priv
.ab3100
= ab3100
;
607 ab3100_get_priv
.mode
= false;
608 ab3100_get_reg_file
= debugfs_create_file("get_reg",
609 S_IWUSR
, ab3100_dir
, &ab3100_get_priv
,
610 &ab3100_get_set_reg_fops
);
611 if (!ab3100_get_reg_file
) {
613 goto exit_destroy_reg
;
616 ab3100_set_priv
.ab3100
= ab3100
;
617 ab3100_set_priv
.mode
= true;
618 ab3100_set_reg_file
= debugfs_create_file("set_reg",
619 S_IWUSR
, ab3100_dir
, &ab3100_set_priv
,
620 &ab3100_get_set_reg_fops
);
621 if (!ab3100_set_reg_file
) {
623 goto exit_destroy_get_reg
;
627 exit_destroy_get_reg
:
628 debugfs_remove(ab3100_get_reg_file
);
630 debugfs_remove(ab3100_reg_file
);
632 debugfs_remove(ab3100_dir
);
636 static inline void ab3100_remove_debugfs(void)
638 debugfs_remove(ab3100_set_reg_file
);
639 debugfs_remove(ab3100_get_reg_file
);
640 debugfs_remove(ab3100_reg_file
);
641 debugfs_remove(ab3100_dir
);
644 static inline void ab3100_setup_debugfs(struct ab3100
*ab3100
)
647 static inline void ab3100_remove_debugfs(void)
653 * Basic set-up, datastructure creation/destruction and I2C interface.
654 * This sets up a default config in the AB3100 chip so that it
655 * will work as expected.
658 struct ab3100_init_setting
{
663 static const struct ab3100_init_setting __devinitconst
664 ab3100_init_settings
[] = {
672 .abreg
= AB3100_IMRA1
,
675 .abreg
= AB3100_IMRA2
,
678 .abreg
= AB3100_IMRA3
,
681 .abreg
= AB3100_IMRB1
,
684 .abreg
= AB3100_IMRB2
,
687 .abreg
= AB3100_IMRB3
,
710 static int __devinit
ab3100_setup(struct ab3100
*ab3100
)
715 for (i
= 0; i
< ARRAY_SIZE(ab3100_init_settings
); i
++) {
716 err
= ab3100_set_register_interruptible(ab3100
,
717 ab3100_init_settings
[i
].abreg
,
718 ab3100_init_settings
[i
].setting
);
724 * Special trick to make the AB3100 use the 32kHz clock (RTC)
725 * bit 3 in test register 0x02 is a special, undocumented test
726 * register bit that only exist in AB3100 P1E
728 if (ab3100
->chip_id
== 0xc4) {
729 dev_warn(ab3100
->dev
,
730 "AB3100 P1E variant detected, "
731 "forcing chip to 32KHz\n");
732 err
= ab3100_set_test_register_interruptible(ab3100
,
740 /* The subdevices of the AB3100 */
741 static struct mfd_cell ab3100_devs
[] = {
743 .name
= "ab3100-dac",
747 .name
= "ab3100-leds",
751 .name
= "ab3100-power",
755 .name
= "ab3100-regulators",
759 .name
= "ab3100-sim",
763 .name
= "ab3100-uart",
767 .name
= "ab3100-rtc",
771 .name
= "ab3100-charger",
775 .name
= "ab3100-boost",
779 .name
= "ab3100-adc",
783 .name
= "ab3100-fuelgauge",
787 .name
= "ab3100-vibrator",
791 .name
= "ab3100-otp",
795 .name
= "ab3100-codec",
800 struct ab_family_id
{
805 static const struct ab_family_id ids
[] __devinitconst
= {
835 /* AB3000 variants, not supported */
859 static int __devinit
ab3100_probe(struct i2c_client
*client
,
860 const struct i2c_device_id
*id
)
862 struct ab3100
*ab3100
;
863 struct ab3100_platform_data
*ab3100_plf_data
=
864 client
->dev
.platform_data
;
868 ab3100
= devm_kzalloc(&client
->dev
, sizeof(struct ab3100
), GFP_KERNEL
);
870 dev_err(&client
->dev
, "could not allocate AB3100 device\n");
874 /* Initialize data structure */
875 mutex_init(&ab3100
->access_mutex
);
876 BLOCKING_INIT_NOTIFIER_HEAD(&ab3100
->event_subscribers
);
878 ab3100
->i2c_client
= client
;
879 ab3100
->dev
= &ab3100
->i2c_client
->dev
;
881 i2c_set_clientdata(client
, ab3100
);
883 /* Read chip ID register */
884 err
= ab3100_get_register_interruptible(ab3100
, AB3100_CID
,
887 dev_err(&client
->dev
,
888 "could not communicate with the AB3100 analog "
893 for (i
= 0; ids
[i
].id
!= 0x0; i
++) {
894 if (ids
[i
].id
== ab3100
->chip_id
) {
895 if (ids
[i
].name
!= NULL
) {
896 snprintf(&ab3100
->chip_name
[0],
897 sizeof(ab3100
->chip_name
) - 1,
902 dev_err(&client
->dev
,
903 "AB3000 is not supported\n");
909 if (ids
[i
].id
== 0x0) {
910 dev_err(&client
->dev
, "unknown analog baseband chip id: 0x%x\n",
912 dev_err(&client
->dev
, "accepting it anyway. Please update "
917 dev_info(&client
->dev
, "Detected chip: %s\n",
918 &ab3100
->chip_name
[0]);
920 /* Attach a second dummy i2c_client to the test register address */
921 ab3100
->testreg_client
= i2c_new_dummy(client
->adapter
,
923 if (!ab3100
->testreg_client
) {
925 goto exit_no_testreg_client
;
928 err
= ab3100_setup(ab3100
);
932 err
= devm_request_threaded_irq(&client
->dev
,
933 client
->irq
, NULL
, ab3100_irq_handler
,
934 IRQF_ONESHOT
, "ab3100-core", ab3100
);
938 err
= abx500_register_ops(&client
->dev
, &ab3100_ops
);
942 /* Set up and register the platform devices. */
943 for (i
= 0; i
< ARRAY_SIZE(ab3100_devs
); i
++) {
944 ab3100_devs
[i
].platform_data
= ab3100_plf_data
;
945 ab3100_devs
[i
].pdata_size
= sizeof(struct ab3100_platform_data
);
948 err
= mfd_add_devices(&client
->dev
, 0, ab3100_devs
,
949 ARRAY_SIZE(ab3100_devs
), NULL
, 0, NULL
);
951 ab3100_setup_debugfs(ab3100
);
958 i2c_unregister_device(ab3100
->testreg_client
);
959 exit_no_testreg_client
:
964 static int __devexit
ab3100_remove(struct i2c_client
*client
)
966 struct ab3100
*ab3100
= i2c_get_clientdata(client
);
968 /* Unregister subdevices */
969 mfd_remove_devices(&client
->dev
);
970 ab3100_remove_debugfs();
971 i2c_unregister_device(ab3100
->testreg_client
);
975 static const struct i2c_device_id ab3100_id
[] = {
979 MODULE_DEVICE_TABLE(i2c
, ab3100_id
);
981 static struct i2c_driver ab3100_driver
= {
984 .owner
= THIS_MODULE
,
986 .id_table
= ab3100_id
,
987 .probe
= ab3100_probe
,
988 .remove
= __devexit_p(ab3100_remove
),
991 static int __init
ab3100_i2c_init(void)
993 return i2c_add_driver(&ab3100_driver
);
996 static void __exit
ab3100_i2c_exit(void)
998 i2c_del_driver(&ab3100_driver
);
1001 subsys_initcall(ab3100_i2c_init
);
1002 module_exit(ab3100_i2c_exit
);
1004 MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
1005 MODULE_DESCRIPTION("AB3100 core driver");
1006 MODULE_LICENSE("GPL");