2 * Copyright (C) 2007-2009 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/err.h>
14 #include <linux/platform_device.h>
15 #include <linux/device.h>
16 #include <linux/interrupt.h>
17 #include <linux/workqueue.h>
18 #include <linux/debugfs.h>
19 #include <linux/seq_file.h>
20 #include <linux/uaccess.h>
21 #include <linux/mfd/ab3100.h>
23 /* These are the only registers inside AB3100 used in this main file */
25 /* Interrupt event registers */
26 #define AB3100_EVENTA1 0x21
27 #define AB3100_EVENTA2 0x22
28 #define AB3100_EVENTA3 0x23
30 /* AB3100 DAC converter registers */
31 #define AB3100_DIS 0x00
32 #define AB3100_D0C 0x01
33 #define AB3100_D1C 0x02
34 #define AB3100_D2C 0x03
35 #define AB3100_D3C 0x04
37 /* Chip ID register */
38 #define AB3100_CID 0x20
40 /* AB3100 interrupt registers */
41 #define AB3100_IMRA1 0x24
42 #define AB3100_IMRA2 0x25
43 #define AB3100_IMRA3 0x26
44 #define AB3100_IMRB1 0x2B
45 #define AB3100_IMRB2 0x2C
46 #define AB3100_IMRB3 0x2D
48 /* System Power Monitoring and control registers */
49 #define AB3100_MCA 0x2E
50 #define AB3100_MCB 0x2F
53 #define AB3100_SUP 0x50
58 * The AB3100 is usually assigned address 0x48 (7-bit)
59 * The chip is defined in the platform i2c_board_data section.
61 static unsigned short normal_i2c
[] = { 0x48, I2C_CLIENT_END
};
62 I2C_CLIENT_INSMOD_1(ab3100
);
64 u8
ab3100_get_chip_type(struct ab3100
*ab3100
)
68 switch (ab3100
->chip_id
& 0xf0) {
78 EXPORT_SYMBOL(ab3100_get_chip_type
);
80 int ab3100_set_register(struct ab3100
*ab3100
, u8 reg
, u8 regval
)
82 u8 regandval
[2] = {reg
, regval
};
85 err
= mutex_lock_interruptible(&ab3100
->access_mutex
);
90 * A two-byte write message with the first byte containing the register
91 * number and the second byte containing the value to be written
92 * effectively sets a register in the AB3100.
94 err
= i2c_master_send(ab3100
->i2c_client
, regandval
, 2);
97 "write error (write register): %d\n",
99 } else if (err
!= 2) {
101 "write error (write register) "
102 "%d bytes transferred (expected 2)\n",
109 mutex_unlock(&ab3100
->access_mutex
);
112 EXPORT_SYMBOL(ab3100_set_register
);
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(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
);
151 int ab3100_get_register(struct ab3100
*ab3100
, u8 reg
, u8
*regval
)
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
);
205 EXPORT_SYMBOL(ab3100_get_register
);
207 int ab3100_get_register_page(struct ab3100
*ab3100
,
208 u8 first_reg
, u8
*regvals
, u8 numregs
)
212 if (ab3100
->chip_id
== 0xa0 ||
213 ab3100
->chip_id
== 0xa1)
214 /* These don't support paged reads */
217 err
= mutex_lock_interruptible(&ab3100
->access_mutex
);
222 * Paged read also require an I2C "stop" command.
224 err
= i2c_master_send(ab3100
->i2c_client
, &first_reg
, 1);
227 "write error (send first register address): %d\n",
229 goto get_reg_page_out_unlock
;
230 } else if (err
!= 1) {
232 "write error (send first register address) "
233 "%d bytes transferred (expected 1)\n",
236 goto get_reg_page_out_unlock
;
239 err
= i2c_master_recv(ab3100
->i2c_client
, regvals
, numregs
);
242 "write error (read register page): %d\n",
244 goto get_reg_page_out_unlock
;
245 } else if (err
!= numregs
) {
247 "write error (read register page) "
248 "%d bytes transferred (expected %d)\n",
251 goto get_reg_page_out_unlock
;
257 get_reg_page_out_unlock
:
258 mutex_unlock(&ab3100
->access_mutex
);
261 EXPORT_SYMBOL(ab3100_get_register_page
);
263 int ab3100_mask_and_set_register(struct ab3100
*ab3100
,
264 u8 reg
, u8 andmask
, u8 ormask
)
266 u8 regandval
[2] = {reg
, 0};
269 err
= mutex_lock_interruptible(&ab3100
->access_mutex
);
273 /* First read out the target register */
274 err
= i2c_master_send(ab3100
->i2c_client
, ®
, 1);
277 "write error (maskset send address): %d\n",
279 goto get_maskset_unlock
;
280 } else if (err
!= 1) {
282 "write error (maskset send address) "
283 "%d bytes transferred (expected 1)\n",
286 goto get_maskset_unlock
;
289 err
= i2c_master_recv(ab3100
->i2c_client
, ®andval
[1], 1);
292 "write error (maskset read register): %d\n",
294 goto get_maskset_unlock
;
295 } else if (err
!= 1) {
297 "write error (maskset read register) "
298 "%d bytes transferred (expected 1)\n",
301 goto get_maskset_unlock
;
304 /* Modify the register */
305 regandval
[1] &= andmask
;
306 regandval
[1] |= ormask
;
308 /* Write the register */
309 err
= i2c_master_send(ab3100
->i2c_client
, regandval
, 2);
312 "write error (write register): %d\n",
314 goto get_maskset_unlock
;
315 } else if (err
!= 2) {
317 "write error (write register) "
318 "%d bytes transferred (expected 2)\n",
321 goto get_maskset_unlock
;
328 mutex_unlock(&ab3100
->access_mutex
);
331 EXPORT_SYMBOL(ab3100_mask_and_set_register
);
334 * Register a simple callback for handling any AB3100 events.
336 int ab3100_event_register(struct ab3100
*ab3100
,
337 struct notifier_block
*nb
)
339 return blocking_notifier_chain_register(&ab3100
->event_subscribers
,
342 EXPORT_SYMBOL(ab3100_event_register
);
345 * Remove a previously registered callback.
347 int ab3100_event_unregister(struct ab3100
*ab3100
,
348 struct notifier_block
*nb
)
350 return blocking_notifier_chain_unregister(&ab3100
->event_subscribers
,
353 EXPORT_SYMBOL(ab3100_event_unregister
);
356 int ab3100_event_registers_startup_state_get(struct ab3100
*ab3100
,
359 if (!ab3100
->startup_events_read
)
360 return -EAGAIN
; /* Try again later */
361 *fatevent
= ab3100
->startup_events
;
364 EXPORT_SYMBOL(ab3100_event_registers_startup_state_get
);
366 /* Interrupt handling worker */
367 static void ab3100_work(struct work_struct
*work
)
369 struct ab3100
*ab3100
= container_of(work
, struct ab3100
, work
);
374 err
= ab3100_get_register_page(ab3100
, AB3100_EVENTA1
,
379 fatevent
= (event_regs
[0] << 16) |
380 (event_regs
[1] << 8) |
383 if (!ab3100
->startup_events_read
) {
384 ab3100
->startup_events
= fatevent
;
385 ab3100
->startup_events_read
= true;
388 * The notified parties will have to mask out the events
389 * they're interested in and react to them. They will be
390 * notified on all events, then they use the fatevent value
391 * to determine if they're interested.
393 blocking_notifier_call_chain(&ab3100
->event_subscribers
,
397 "IRQ Event: 0x%08x\n", fatevent
);
399 /* By now the IRQ should be acked and deasserted so enable it again */
400 enable_irq(ab3100
->i2c_client
->irq
);
405 "error in event workqueue\n");
406 /* Enable the IRQ anyway, what choice do we have? */
407 enable_irq(ab3100
->i2c_client
->irq
);
411 static irqreturn_t
ab3100_irq_handler(int irq
, void *data
)
413 struct ab3100
*ab3100
= data
;
415 * Disable the IRQ and dispatch a worker to handle the
416 * event. Since the chip resides on I2C this is slow
417 * stuff and we will re-enable the interrupts once th
418 * worker has finished.
420 disable_irq(ab3100
->i2c_client
->irq
);
421 schedule_work(&ab3100
->work
);
425 #ifdef CONFIG_DEBUG_FS
427 * Some debugfs entries only exposed if we're using debug
429 static int ab3100_registers_print(struct seq_file
*s
, void *p
)
431 struct ab3100
*ab3100
= s
->private;
435 seq_printf(s
, "AB3100 registers:\n");
437 for (reg
= 0; reg
< 0xff; reg
++) {
438 ab3100_get_register(ab3100
, reg
, &value
);
439 seq_printf(s
, "[0x%x]: 0x%x\n", reg
, value
);
444 static int ab3100_registers_open(struct inode
*inode
, struct file
*file
)
446 return single_open(file
, ab3100_registers_print
, inode
->i_private
);
449 static const struct file_operations ab3100_registers_fops
= {
450 .open
= ab3100_registers_open
,
453 .release
= single_release
,
454 .owner
= THIS_MODULE
,
457 struct ab3100_get_set_reg_priv
{
458 struct ab3100
*ab3100
;
462 static int ab3100_get_set_reg_open_file(struct inode
*inode
, struct file
*file
)
464 file
->private_data
= inode
->i_private
;
468 static int ab3100_get_set_reg(struct file
*file
,
469 const char __user
*user_buf
,
470 size_t count
, loff_t
*ppos
)
472 struct ab3100_get_set_reg_priv
*priv
= file
->private_data
;
473 struct ab3100
*ab3100
= priv
->ab3100
;
477 unsigned long user_reg
;
481 /* Get userspace string and assure termination */
482 buf_size
= min(count
, (sizeof(buf
)-1));
483 if (copy_from_user(buf
, user_buf
, buf_size
))
488 * The idea is here to parse a string which is either
489 * "0xnn" for reading a register, or "0xaa 0xbb" for
490 * writing 0xbb to the register 0xaa. First move past
491 * whitespace and then begin to parse the register.
493 while ((i
< buf_size
) && (buf
[i
] == ' '))
498 * Advance pointer to end of string then terminate
499 * the register string. This is needed to satisfy
500 * the strict_strtoul() function.
502 while ((i
< buf_size
) && (buf
[i
] != ' '))
506 err
= strict_strtoul(&buf
[regp
], 16, &user_reg
);
512 /* Either we read or we write a register here */
515 u8 reg
= (u8
) user_reg
;
518 ab3100_get_register(ab3100
, reg
, ®value
);
520 dev_info(ab3100
->dev
,
521 "debug read AB3100 reg[0x%02x]: 0x%02x\n",
525 unsigned long user_value
;
526 u8 reg
= (u8
) user_reg
;
531 * Writing, we need some value to write to
532 * the register so keep parsing the string
536 while ((i
< buf_size
) && (buf
[i
] == ' '))
539 while ((i
< buf_size
) && (buf
[i
] != ' '))
543 err
= strict_strtoul(&buf
[valp
], 16, &user_value
);
549 value
= (u8
) user_value
;
550 ab3100_set_register(ab3100
, reg
, value
);
551 ab3100_get_register(ab3100
, reg
, ®value
);
553 dev_info(ab3100
->dev
,
554 "debug write reg[0x%02x] with 0x%02x, "
555 "after readback: 0x%02x\n",
556 reg
, value
, regvalue
);
561 static const struct file_operations ab3100_get_set_reg_fops
= {
562 .open
= ab3100_get_set_reg_open_file
,
563 .write
= ab3100_get_set_reg
,
566 static struct dentry
*ab3100_dir
;
567 static struct dentry
*ab3100_reg_file
;
568 static struct ab3100_get_set_reg_priv ab3100_get_priv
;
569 static struct dentry
*ab3100_get_reg_file
;
570 static struct ab3100_get_set_reg_priv ab3100_set_priv
;
571 static struct dentry
*ab3100_set_reg_file
;
573 static void ab3100_setup_debugfs(struct ab3100
*ab3100
)
577 ab3100_dir
= debugfs_create_dir("ab3100", NULL
);
579 goto exit_no_debugfs
;
581 ab3100_reg_file
= debugfs_create_file("registers",
582 S_IRUGO
, ab3100_dir
, ab3100
,
583 &ab3100_registers_fops
);
584 if (!ab3100_reg_file
) {
586 goto exit_destroy_dir
;
589 ab3100_get_priv
.ab3100
= ab3100
;
590 ab3100_get_priv
.mode
= false;
591 ab3100_get_reg_file
= debugfs_create_file("get_reg",
592 S_IWUGO
, ab3100_dir
, &ab3100_get_priv
,
593 &ab3100_get_set_reg_fops
);
594 if (!ab3100_get_reg_file
) {
596 goto exit_destroy_reg
;
599 ab3100_set_priv
.ab3100
= ab3100
;
600 ab3100_set_priv
.mode
= true;
601 ab3100_set_reg_file
= debugfs_create_file("set_reg",
602 S_IWUGO
, ab3100_dir
, &ab3100_set_priv
,
603 &ab3100_get_set_reg_fops
);
604 if (!ab3100_set_reg_file
) {
606 goto exit_destroy_get_reg
;
610 exit_destroy_get_reg
:
611 debugfs_remove(ab3100_get_reg_file
);
613 debugfs_remove(ab3100_reg_file
);
615 debugfs_remove(ab3100_dir
);
619 static inline void ab3100_remove_debugfs(void)
621 debugfs_remove(ab3100_set_reg_file
);
622 debugfs_remove(ab3100_get_reg_file
);
623 debugfs_remove(ab3100_reg_file
);
624 debugfs_remove(ab3100_dir
);
627 static inline void ab3100_setup_debugfs(struct ab3100
*ab3100
)
630 static inline void ab3100_remove_debugfs(void)
636 * Basic set-up, datastructure creation/destruction and I2C interface.
637 * This sets up a default config in the AB3100 chip so that it
638 * will work as expected.
641 struct ab3100_init_setting
{
646 static const struct ab3100_init_setting __initconst
647 ab3100_init_settings
[] = {
655 .abreg
= AB3100_IMRA1
,
658 .abreg
= AB3100_IMRA2
,
661 .abreg
= AB3100_IMRA3
,
664 .abreg
= AB3100_IMRB1
,
667 .abreg
= AB3100_IMRB2
,
670 .abreg
= AB3100_IMRB3
,
693 static int __init
ab3100_setup(struct ab3100
*ab3100
)
698 for (i
= 0; i
< ARRAY_SIZE(ab3100_init_settings
); i
++) {
699 err
= ab3100_set_register(ab3100
,
700 ab3100_init_settings
[i
].abreg
,
701 ab3100_init_settings
[i
].setting
);
707 * Special trick to make the AB3100 use the 32kHz clock (RTC)
708 * bit 3 in test registe 0x02 is a special, undocumented test
709 * register bit that only exist in AB3100 P1E
711 if (ab3100
->chip_id
== 0xc4) {
712 dev_warn(ab3100
->dev
,
713 "AB3100 P1E variant detected, "
714 "forcing chip to 32KHz\n");
715 err
= ab3100_set_test_register(ab3100
, 0x02, 0x08);
723 * Here we define all the platform devices that appear
724 * as children of the AB3100. These are regular platform
725 * devices with the IORESOURCE_IO .start and .end set
726 * to correspond to the internal AB3100 register range
727 * mapping to the corresponding subdevice.
730 #define AB3100_DEVICE(devname, devid) \
731 static struct platform_device ab3100_##devname##_device = { \
737 * This lists all the subdevices and corresponding register
740 AB3100_DEVICE(dac
, "ab3100-dac");
741 AB3100_DEVICE(leds
, "ab3100-leds");
742 AB3100_DEVICE(power
, "ab3100-power");
743 AB3100_DEVICE(regulators
, "ab3100-regulators");
744 AB3100_DEVICE(sim
, "ab3100-sim");
745 AB3100_DEVICE(uart
, "ab3100-uart");
746 AB3100_DEVICE(rtc
, "ab3100-rtc");
747 AB3100_DEVICE(charger
, "ab3100-charger");
748 AB3100_DEVICE(boost
, "ab3100-boost");
749 AB3100_DEVICE(adc
, "ab3100-adc");
750 AB3100_DEVICE(fuelgauge
, "ab3100-fuelgauge");
751 AB3100_DEVICE(vibrator
, "ab3100-vibrator");
752 AB3100_DEVICE(otp
, "ab3100-otp");
753 AB3100_DEVICE(codec
, "ab3100-codec");
755 static struct platform_device
*
756 ab3100_platform_devs
[] = {
759 &ab3100_power_device
,
760 &ab3100_regulators_device
,
764 &ab3100_charger_device
,
765 &ab3100_boost_device
,
767 &ab3100_fuelgauge_device
,
768 &ab3100_vibrator_device
,
770 &ab3100_codec_device
,
773 struct ab_family_id
{
778 static const struct ab_family_id ids
[] __initdata
= {
808 /* AB3000 variants, not supported */
832 static int __init
ab3100_probe(struct i2c_client
*client
,
833 const struct i2c_device_id
*id
)
835 struct ab3100
*ab3100
;
839 ab3100
= kzalloc(sizeof(struct ab3100
), GFP_KERNEL
);
841 dev_err(&client
->dev
, "could not allocate AB3100 device\n");
845 /* Initialize data structure */
846 mutex_init(&ab3100
->access_mutex
);
847 BLOCKING_INIT_NOTIFIER_HEAD(&ab3100
->event_subscribers
);
849 ab3100
->i2c_client
= client
;
850 ab3100
->dev
= &ab3100
->i2c_client
->dev
;
852 i2c_set_clientdata(client
, ab3100
);
854 /* Read chip ID register */
855 err
= ab3100_get_register(ab3100
, AB3100_CID
,
858 dev_err(&client
->dev
,
859 "could not communicate with the AB3100 analog "
864 for (i
= 0; ids
[i
].id
!= 0x0; i
++) {
865 if (ids
[i
].id
== ab3100
->chip_id
) {
866 if (ids
[i
].name
!= NULL
) {
867 snprintf(&ab3100
->chip_name
[0],
868 sizeof(ab3100
->chip_name
) - 1,
873 dev_err(&client
->dev
,
874 "AB3000 is not supported\n");
880 if (ids
[i
].id
== 0x0) {
881 dev_err(&client
->dev
, "unknown analog baseband chip id: 0x%x\n",
883 dev_err(&client
->dev
, "accepting it anyway. Please update "
888 dev_info(&client
->dev
, "Detected chip: %s\n",
889 &ab3100
->chip_name
[0]);
891 /* Attach a second dummy i2c_client to the test register address */
892 ab3100
->testreg_client
= i2c_new_dummy(client
->adapter
,
894 if (!ab3100
->testreg_client
) {
896 goto exit_no_testreg_client
;
899 strlcpy(ab3100
->testreg_client
->name
, id
->name
,
900 sizeof(ab3100
->testreg_client
->name
));
902 err
= ab3100_setup(ab3100
);
906 INIT_WORK(&ab3100
->work
, ab3100_work
);
908 /* This real unpredictable IRQ is of course sampled for entropy */
909 err
= request_irq(client
->irq
, ab3100_irq_handler
,
910 IRQF_DISABLED
| IRQF_SAMPLE_RANDOM
,
911 "AB3100 IRQ", ab3100
);
915 /* Set parent and a pointer back to the container in device data */
916 for (i
= 0; i
< ARRAY_SIZE(ab3100_platform_devs
); i
++) {
917 ab3100_platform_devs
[i
]->dev
.parent
=
919 platform_set_drvdata(ab3100_platform_devs
[i
], ab3100
);
922 /* Register the platform devices */
923 platform_add_devices(ab3100_platform_devs
,
924 ARRAY_SIZE(ab3100_platform_devs
));
926 ab3100_setup_debugfs(ab3100
);
932 i2c_unregister_device(ab3100
->testreg_client
);
933 exit_no_testreg_client
:
939 static int __exit
ab3100_remove(struct i2c_client
*client
)
941 struct ab3100
*ab3100
= i2c_get_clientdata(client
);
944 /* Unregister subdevices */
945 for (i
= 0; i
< ARRAY_SIZE(ab3100_platform_devs
); i
++)
946 platform_device_unregister(ab3100_platform_devs
[i
]);
948 ab3100_remove_debugfs();
949 i2c_unregister_device(ab3100
->testreg_client
);
952 * At this point, all subscribers should have unregistered
953 * their notifiers so deactivate IRQ
955 free_irq(client
->irq
, ab3100
);
960 static const struct i2c_device_id ab3100_id
[] = {
961 { "ab3100", ab3100
},
964 MODULE_DEVICE_TABLE(i2c
, ab3100_id
);
966 static struct i2c_driver ab3100_driver
= {
969 .owner
= THIS_MODULE
,
971 .id_table
= ab3100_id
,
972 .probe
= ab3100_probe
,
973 .remove
= __exit_p(ab3100_remove
),
976 static int __init
ab3100_i2c_init(void)
978 return i2c_add_driver(&ab3100_driver
);
981 static void __exit
ab3100_i2c_exit(void)
983 i2c_del_driver(&ab3100_driver
);
986 subsys_initcall(ab3100_i2c_init
);
987 module_exit(ab3100_i2c_exit
);
989 MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
990 MODULE_DESCRIPTION("AB3100 core driver");
991 MODULE_LICENSE("GPL");