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/ab3100.h>
25 #include <linux/mfd/abx500.h>
27 /* These are the only registers inside AB3100 used in this main file */
29 /* Interrupt event registers */
30 #define AB3100_EVENTA1 0x21
31 #define AB3100_EVENTA2 0x22
32 #define AB3100_EVENTA3 0x23
34 /* AB3100 DAC converter registers */
35 #define AB3100_DIS 0x00
36 #define AB3100_D0C 0x01
37 #define AB3100_D1C 0x02
38 #define AB3100_D2C 0x03
39 #define AB3100_D3C 0x04
41 /* Chip ID register */
42 #define AB3100_CID 0x20
44 /* AB3100 interrupt registers */
45 #define AB3100_IMRA1 0x24
46 #define AB3100_IMRA2 0x25
47 #define AB3100_IMRA3 0x26
48 #define AB3100_IMRB1 0x2B
49 #define AB3100_IMRB2 0x2C
50 #define AB3100_IMRB3 0x2D
52 /* System Power Monitoring and control registers */
53 #define AB3100_MCA 0x2E
54 #define AB3100_MCB 0x2F
57 #define AB3100_SUP 0x50
62 * The AB3100 is usually assigned address 0x48 (7-bit)
63 * The chip is defined in the platform i2c_board_data section.
65 static int ab3100_get_chip_id(struct device
*dev
)
67 struct ab3100
*ab3100
= dev_get_drvdata(dev
->parent
);
69 return (int)ab3100
->chip_id
;
72 static int ab3100_set_register_interruptible(struct ab3100
*ab3100
,
75 u8 regandval
[2] = {reg
, regval
};
78 err
= mutex_lock_interruptible(&ab3100
->access_mutex
);
83 * A two-byte write message with the first byte containing the register
84 * number and the second byte containing the value to be written
85 * effectively sets a register in the AB3100.
87 err
= i2c_master_send(ab3100
->i2c_client
, regandval
, 2);
90 "write error (write register): %d\n",
92 } else if (err
!= 2) {
94 "write error (write register)\n"
95 " %d bytes transferred (expected 2)\n",
102 mutex_unlock(&ab3100
->access_mutex
);
106 static int set_register_interruptible(struct device
*dev
,
107 u8 bank
, u8 reg
, u8 value
)
109 struct ab3100
*ab3100
= dev_get_drvdata(dev
->parent
);
111 return ab3100_set_register_interruptible(ab3100
, reg
, value
);
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)\n"
139 " %d bytes transferred (expected 2)\n",
146 mutex_unlock(&ab3100
->access_mutex
);
151 static int ab3100_get_register_interruptible(struct ab3100
*ab3100
,
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)\n"
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)\n"
193 " %d bytes transferred (expected 1)\n",
196 goto get_reg_out_unlock
;
203 mutex_unlock(&ab3100
->access_mutex
);
207 static int get_register_interruptible(struct device
*dev
, u8 bank
, u8 reg
,
210 struct ab3100
*ab3100
= dev_get_drvdata(dev
->parent
);
212 return ab3100_get_register_interruptible(ab3100
, reg
, value
);
215 static int ab3100_get_register_page_interruptible(struct ab3100
*ab3100
,
216 u8 first_reg
, u8
*regvals
, u8 numregs
)
220 if (ab3100
->chip_id
== 0xa0 ||
221 ab3100
->chip_id
== 0xa1)
222 /* These don't support paged reads */
225 err
= mutex_lock_interruptible(&ab3100
->access_mutex
);
230 * Paged read also require an I2C "stop" command.
232 err
= i2c_master_send(ab3100
->i2c_client
, &first_reg
, 1);
235 "write error (send first register address): %d\n",
237 goto get_reg_page_out_unlock
;
238 } else if (err
!= 1) {
240 "write error (send first register address)\n"
241 " %d bytes transferred (expected 1)\n",
244 goto get_reg_page_out_unlock
;
247 err
= i2c_master_recv(ab3100
->i2c_client
, regvals
, numregs
);
250 "write error (read register page): %d\n",
252 goto get_reg_page_out_unlock
;
253 } else if (err
!= numregs
) {
255 "write error (read register page)\n"
256 " %d bytes transferred (expected %d)\n",
259 goto get_reg_page_out_unlock
;
265 get_reg_page_out_unlock
:
266 mutex_unlock(&ab3100
->access_mutex
);
270 static int get_register_page_interruptible(struct device
*dev
, u8 bank
,
271 u8 first_reg
, u8
*regvals
, u8 numregs
)
273 struct ab3100
*ab3100
= dev_get_drvdata(dev
->parent
);
275 return ab3100_get_register_page_interruptible(ab3100
,
276 first_reg
, regvals
, numregs
);
279 static int ab3100_mask_and_set_register_interruptible(struct ab3100
*ab3100
,
280 u8 reg
, u8 andmask
, u8 ormask
)
282 u8 regandval
[2] = {reg
, 0};
285 err
= mutex_lock_interruptible(&ab3100
->access_mutex
);
289 /* First read out the target register */
290 err
= i2c_master_send(ab3100
->i2c_client
, ®
, 1);
293 "write error (maskset send address): %d\n",
295 goto get_maskset_unlock
;
296 } else if (err
!= 1) {
298 "write error (maskset send address)\n"
299 " %d bytes transferred (expected 1)\n",
302 goto get_maskset_unlock
;
305 err
= i2c_master_recv(ab3100
->i2c_client
, ®andval
[1], 1);
308 "write error (maskset read register): %d\n",
310 goto get_maskset_unlock
;
311 } else if (err
!= 1) {
313 "write error (maskset read register)\n"
314 " %d bytes transferred (expected 1)\n",
317 goto get_maskset_unlock
;
320 /* Modify the register */
321 regandval
[1] &= andmask
;
322 regandval
[1] |= ormask
;
324 /* Write the register */
325 err
= i2c_master_send(ab3100
->i2c_client
, regandval
, 2);
328 "write error (write register): %d\n",
330 goto get_maskset_unlock
;
331 } else if (err
!= 2) {
333 "write error (write register)\n"
334 " %d bytes transferred (expected 2)\n",
337 goto get_maskset_unlock
;
344 mutex_unlock(&ab3100
->access_mutex
);
348 static int mask_and_set_register_interruptible(struct device
*dev
, u8 bank
,
349 u8 reg
, u8 bitmask
, u8 bitvalues
)
351 struct ab3100
*ab3100
= dev_get_drvdata(dev
->parent
);
353 return ab3100_mask_and_set_register_interruptible(ab3100
,
354 reg
, bitmask
, (bitmask
& bitvalues
));
358 * Register a simple callback for handling any AB3100 events.
360 int ab3100_event_register(struct ab3100
*ab3100
,
361 struct notifier_block
*nb
)
363 return blocking_notifier_chain_register(&ab3100
->event_subscribers
,
366 EXPORT_SYMBOL(ab3100_event_register
);
369 * Remove a previously registered callback.
371 int ab3100_event_unregister(struct ab3100
*ab3100
,
372 struct notifier_block
*nb
)
374 return blocking_notifier_chain_unregister(&ab3100
->event_subscribers
,
377 EXPORT_SYMBOL(ab3100_event_unregister
);
380 static int ab3100_event_registers_startup_state_get(struct device
*dev
,
383 struct ab3100
*ab3100
= dev_get_drvdata(dev
->parent
);
384 if (!ab3100
->startup_events_read
)
385 return -EAGAIN
; /* Try again later */
386 memcpy(event
, ab3100
->startup_events
, 3);
390 static struct abx500_ops ab3100_ops
= {
391 .get_chip_id
= ab3100_get_chip_id
,
392 .set_register
= set_register_interruptible
,
393 .get_register
= get_register_interruptible
,
394 .get_register_page
= get_register_page_interruptible
,
395 .set_register_page
= NULL
,
396 .mask_and_set_register
= mask_and_set_register_interruptible
,
397 .event_registers_startup_state_get
=
398 ab3100_event_registers_startup_state_get
,
399 .startup_irq_enabled
= NULL
,
403 * This is a threaded interrupt handler so we can make some
406 static irqreturn_t
ab3100_irq_handler(int irq
, void *data
)
408 struct ab3100
*ab3100
= data
;
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_puts(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 ssize_t
ab3100_get_set_reg(struct file
*file
,
486 const char __user
*user_buf
,
487 size_t count
, loff_t
*ppos
)
489 struct ab3100_get_set_reg_priv
*priv
= file
->private_data
;
490 struct ab3100
*ab3100
= priv
->ab3100
;
498 /* Get userspace string and assure termination */
499 buf_size
= min(count
, (sizeof(buf
)-1));
500 if (copy_from_user(buf
, user_buf
, buf_size
))
505 * The idea is here to parse a string which is either
506 * "0xnn" for reading a register, or "0xaa 0xbb" for
507 * writing 0xbb to the register 0xaa. First move past
508 * whitespace and then begin to parse the register.
510 while ((i
< buf_size
) && (buf
[i
] == ' '))
515 * Advance pointer to end of string then terminate
516 * the register string. This is needed to satisfy
517 * the kstrtou8() function.
519 while ((i
< buf_size
) && (buf
[i
] != ' '))
523 err
= kstrtou8(&buf
[regp
], 16, &user_reg
);
527 /* Either we read or we write a register here */
532 ab3100_get_register_interruptible(ab3100
, user_reg
, ®value
);
534 dev_info(ab3100
->dev
,
535 "debug read AB3100 reg[0x%02x]: 0x%02x\n",
543 * Writing, we need some value to write to
544 * the register so keep parsing the string
548 while ((i
< buf_size
) && (buf
[i
] == ' '))
551 while ((i
< buf_size
) && (buf
[i
] != ' '))
555 err
= kstrtou8(&buf
[valp
], 16, &user_value
);
559 ab3100_set_register_interruptible(ab3100
, user_reg
, user_value
);
560 ab3100_get_register_interruptible(ab3100
, user_reg
, ®value
);
562 dev_info(ab3100
->dev
,
563 "debug write reg[0x%02x]\n"
564 " with 0x%02x, after readback: 0x%02x\n",
565 user_reg
, user_value
, regvalue
);
570 static const struct file_operations ab3100_get_set_reg_fops
= {
572 .write
= ab3100_get_set_reg
,
573 .llseek
= noop_llseek
,
576 static struct dentry
*ab3100_dir
;
577 static struct dentry
*ab3100_reg_file
;
578 static struct ab3100_get_set_reg_priv ab3100_get_priv
;
579 static struct dentry
*ab3100_get_reg_file
;
580 static struct ab3100_get_set_reg_priv ab3100_set_priv
;
581 static struct dentry
*ab3100_set_reg_file
;
583 static void ab3100_setup_debugfs(struct ab3100
*ab3100
)
587 ab3100_dir
= debugfs_create_dir("ab3100", NULL
);
589 goto exit_no_debugfs
;
591 ab3100_reg_file
= debugfs_create_file("registers",
592 S_IRUGO
, ab3100_dir
, ab3100
,
593 &ab3100_registers_fops
);
594 if (!ab3100_reg_file
) {
596 goto exit_destroy_dir
;
599 ab3100_get_priv
.ab3100
= ab3100
;
600 ab3100_get_priv
.mode
= false;
601 ab3100_get_reg_file
= debugfs_create_file("get_reg",
602 S_IWUSR
, ab3100_dir
, &ab3100_get_priv
,
603 &ab3100_get_set_reg_fops
);
604 if (!ab3100_get_reg_file
) {
606 goto exit_destroy_reg
;
609 ab3100_set_priv
.ab3100
= ab3100
;
610 ab3100_set_priv
.mode
= true;
611 ab3100_set_reg_file
= debugfs_create_file("set_reg",
612 S_IWUSR
, ab3100_dir
, &ab3100_set_priv
,
613 &ab3100_get_set_reg_fops
);
614 if (!ab3100_set_reg_file
) {
616 goto exit_destroy_get_reg
;
620 exit_destroy_get_reg
:
621 debugfs_remove(ab3100_get_reg_file
);
623 debugfs_remove(ab3100_reg_file
);
625 debugfs_remove(ab3100_dir
);
629 static inline void ab3100_remove_debugfs(void)
631 debugfs_remove(ab3100_set_reg_file
);
632 debugfs_remove(ab3100_get_reg_file
);
633 debugfs_remove(ab3100_reg_file
);
634 debugfs_remove(ab3100_dir
);
637 static inline void ab3100_setup_debugfs(struct ab3100
*ab3100
)
640 static inline void ab3100_remove_debugfs(void)
646 * Basic set-up, datastructure creation/destruction and I2C interface.
647 * This sets up a default config in the AB3100 chip so that it
648 * will work as expected.
651 struct ab3100_init_setting
{
656 static const struct ab3100_init_setting ab3100_init_settings
[] = {
664 .abreg
= AB3100_IMRA1
,
667 .abreg
= AB3100_IMRA2
,
670 .abreg
= AB3100_IMRA3
,
673 .abreg
= AB3100_IMRB1
,
676 .abreg
= AB3100_IMRB2
,
679 .abreg
= AB3100_IMRB3
,
702 static int ab3100_setup(struct ab3100
*ab3100
)
707 for (i
= 0; i
< ARRAY_SIZE(ab3100_init_settings
); i
++) {
708 err
= ab3100_set_register_interruptible(ab3100
,
709 ab3100_init_settings
[i
].abreg
,
710 ab3100_init_settings
[i
].setting
);
716 * Special trick to make the AB3100 use the 32kHz clock (RTC)
717 * bit 3 in test register 0x02 is a special, undocumented test
718 * register bit that only exist in AB3100 P1E
720 if (ab3100
->chip_id
== 0xc4) {
721 dev_warn(ab3100
->dev
,
722 "AB3100 P1E variant detected forcing chip to 32KHz\n");
723 err
= ab3100_set_test_register_interruptible(ab3100
,
731 /* The subdevices of the AB3100 */
732 static struct mfd_cell ab3100_devs
[] = {
734 .name
= "ab3100-dac",
738 .name
= "ab3100-leds",
742 .name
= "ab3100-power",
746 .name
= "ab3100-regulators",
747 .of_compatible
= "stericsson,ab3100-regulators",
751 .name
= "ab3100-sim",
755 .name
= "ab3100-uart",
759 .name
= "ab3100-rtc",
763 .name
= "ab3100-charger",
767 .name
= "ab3100-boost",
771 .name
= "ab3100-adc",
775 .name
= "ab3100-fuelgauge",
779 .name
= "ab3100-vibrator",
783 .name
= "ab3100-otp",
787 .name
= "ab3100-codec",
792 struct ab_family_id
{
797 static const struct ab_family_id ids
[] = {
827 /* AB3000 variants, not supported */
851 static int ab3100_probe(struct i2c_client
*client
,
852 const struct i2c_device_id
*id
)
854 struct ab3100
*ab3100
;
855 struct ab3100_platform_data
*ab3100_plf_data
=
856 dev_get_platdata(&client
->dev
);
860 ab3100
= devm_kzalloc(&client
->dev
, sizeof(struct ab3100
), GFP_KERNEL
);
862 dev_err(&client
->dev
, "could not allocate AB3100 device\n");
866 /* Initialize data structure */
867 mutex_init(&ab3100
->access_mutex
);
868 BLOCKING_INIT_NOTIFIER_HEAD(&ab3100
->event_subscribers
);
870 ab3100
->i2c_client
= client
;
871 ab3100
->dev
= &ab3100
->i2c_client
->dev
;
873 i2c_set_clientdata(client
, ab3100
);
875 /* Read chip ID register */
876 err
= ab3100_get_register_interruptible(ab3100
, AB3100_CID
,
879 dev_err(&client
->dev
,
880 "failed to communicate with AB3100 chip\n");
884 for (i
= 0; ids
[i
].id
!= 0x0; i
++) {
885 if (ids
[i
].id
== ab3100
->chip_id
) {
886 if (ids
[i
].name
!= NULL
) {
887 snprintf(&ab3100
->chip_name
[0],
888 sizeof(ab3100
->chip_name
) - 1,
893 dev_err(&client
->dev
,
894 "AB3000 is not supported\n");
900 if (ids
[i
].id
== 0x0) {
901 dev_err(&client
->dev
, "unknown analog baseband chip id: 0x%x\n",
903 dev_err(&client
->dev
,
904 "accepting it anyway. Please update the driver.\n");
908 dev_info(&client
->dev
, "Detected chip: %s\n",
909 &ab3100
->chip_name
[0]);
911 /* Attach a second dummy i2c_client to the test register address */
912 ab3100
->testreg_client
= i2c_new_dummy(client
->adapter
,
914 if (!ab3100
->testreg_client
) {
916 goto exit_no_testreg_client
;
919 err
= ab3100_setup(ab3100
);
923 err
= devm_request_threaded_irq(&client
->dev
,
924 client
->irq
, NULL
, ab3100_irq_handler
,
925 IRQF_ONESHOT
, "ab3100-core", ab3100
);
929 err
= abx500_register_ops(&client
->dev
, &ab3100_ops
);
933 /* Set up and register the platform devices. */
934 for (i
= 0; i
< ARRAY_SIZE(ab3100_devs
); i
++) {
935 ab3100_devs
[i
].platform_data
= ab3100_plf_data
;
936 ab3100_devs
[i
].pdata_size
= sizeof(struct ab3100_platform_data
);
939 err
= mfd_add_devices(&client
->dev
, 0, ab3100_devs
,
940 ARRAY_SIZE(ab3100_devs
), NULL
, 0, NULL
);
942 ab3100_setup_debugfs(ab3100
);
949 i2c_unregister_device(ab3100
->testreg_client
);
950 exit_no_testreg_client
:
955 static int ab3100_remove(struct i2c_client
*client
)
957 struct ab3100
*ab3100
= i2c_get_clientdata(client
);
959 /* Unregister subdevices */
960 mfd_remove_devices(&client
->dev
);
961 ab3100_remove_debugfs();
962 i2c_unregister_device(ab3100
->testreg_client
);
966 static const struct i2c_device_id ab3100_id
[] = {
970 MODULE_DEVICE_TABLE(i2c
, ab3100_id
);
972 static struct i2c_driver ab3100_driver
= {
976 .id_table
= ab3100_id
,
977 .probe
= ab3100_probe
,
978 .remove
= ab3100_remove
,
981 static int __init
ab3100_i2c_init(void)
983 return i2c_add_driver(&ab3100_driver
);
986 static void __exit
ab3100_i2c_exit(void)
988 i2c_del_driver(&ab3100_driver
);
991 subsys_initcall(ab3100_i2c_init
);
992 module_exit(ab3100_i2c_exit
);
994 MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
995 MODULE_DESCRIPTION("AB3100 core driver");
996 MODULE_LICENSE("GPL");