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_interruptible(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_interruptible);
116 * The test registers exist at an I2C bus address up one
117 * from the ordinary base. They are not supposed to be used
118 * in production code, but sometimes you have to do that
119 * anyway. It's currently only used from this file so declare
120 * it static and do not export.
122 static int ab3100_set_test_register_interruptible(struct ab3100 *ab3100,
125 u8 regandval[2] = {reg, regval};
128 err = mutex_lock_interruptible(&ab3100->access_mutex);
132 err = i2c_master_send(ab3100->testreg_client, regandval, 2);
135 "write error (write test register): %d\n",
137 } else if (err != 2) {
139 "write error (write test register) "
140 "%d bytes transferred (expected 2)\n",
147 mutex_unlock(&ab3100->access_mutex);
153 int ab3100_get_register_interruptible(struct ab3100 *ab3100, u8 reg, u8 *regval)
157 err = mutex_lock_interruptible(&ab3100->access_mutex);
162 * AB3100 require an I2C "stop" command between each message, else
163 * it will not work. The only way of achieveing this with the
164 * message transport layer is to send the read and write messages
167 err = i2c_master_send(ab3100->i2c_client, ®, 1);
170 "write error (send register address): %d\n",
172 goto get_reg_out_unlock;
173 } else if (err != 1) {
175 "write error (send register address) "
176 "%d bytes transferred (expected 1)\n",
179 goto get_reg_out_unlock;
185 err = i2c_master_recv(ab3100->i2c_client, regval, 1);
188 "write error (read register): %d\n",
190 goto get_reg_out_unlock;
191 } else if (err != 1) {
193 "write error (read register) "
194 "%d bytes transferred (expected 1)\n",
197 goto get_reg_out_unlock;
204 mutex_unlock(&ab3100->access_mutex);
207 EXPORT_SYMBOL(ab3100_get_register_interruptible);
210 int ab3100_get_register_page_interruptible(struct ab3100 *ab3100,
211 u8 first_reg, u8 *regvals, u8 numregs)
215 if (ab3100->chip_id == 0xa0 ||
216 ab3100->chip_id == 0xa1)
217 /* These don't support paged reads */
220 err = mutex_lock_interruptible(&ab3100->access_mutex);
225 * Paged read also require an I2C "stop" command.
227 err = i2c_master_send(ab3100->i2c_client, &first_reg, 1);
230 "write error (send first register address): %d\n",
232 goto get_reg_page_out_unlock;
233 } else if (err != 1) {
235 "write error (send first register address) "
236 "%d bytes transferred (expected 1)\n",
239 goto get_reg_page_out_unlock;
242 err = i2c_master_recv(ab3100->i2c_client, regvals, numregs);
245 "write error (read register page): %d\n",
247 goto get_reg_page_out_unlock;
248 } else if (err != numregs) {
250 "write error (read register page) "
251 "%d bytes transferred (expected %d)\n",
254 goto get_reg_page_out_unlock;
260 get_reg_page_out_unlock:
261 mutex_unlock(&ab3100->access_mutex);
264 EXPORT_SYMBOL(ab3100_get_register_page_interruptible);
267 int ab3100_mask_and_set_register_interruptible(struct ab3100 *ab3100,
268 u8 reg, u8 andmask, u8 ormask)
270 u8 regandval[2] = {reg, 0};
273 err = mutex_lock_interruptible(&ab3100->access_mutex);
277 /* First read out the target register */
278 err = i2c_master_send(ab3100->i2c_client, ®, 1);
281 "write error (maskset send address): %d\n",
283 goto get_maskset_unlock;
284 } else if (err != 1) {
286 "write error (maskset send address) "
287 "%d bytes transferred (expected 1)\n",
290 goto get_maskset_unlock;
293 err = i2c_master_recv(ab3100->i2c_client, ®andval[1], 1);
296 "write error (maskset read register): %d\n",
298 goto get_maskset_unlock;
299 } else if (err != 1) {
301 "write error (maskset read register) "
302 "%d bytes transferred (expected 1)\n",
305 goto get_maskset_unlock;
308 /* Modify the register */
309 regandval[1] &= andmask;
310 regandval[1] |= ormask;
312 /* Write the register */
313 err = i2c_master_send(ab3100->i2c_client, regandval, 2);
316 "write error (write register): %d\n",
318 goto get_maskset_unlock;
319 } else if (err != 2) {
321 "write error (write register) "
322 "%d bytes transferred (expected 2)\n",
325 goto get_maskset_unlock;
332 mutex_unlock(&ab3100->access_mutex);
335 EXPORT_SYMBOL(ab3100_mask_and_set_register_interruptible);
339 * Register a simple callback for handling any AB3100 events.
341 int ab3100_event_register(struct ab3100 *ab3100,
342 struct notifier_block *nb)
344 return blocking_notifier_chain_register(&ab3100->event_subscribers,
347 EXPORT_SYMBOL(ab3100_event_register);
350 * Remove a previously registered callback.
352 int ab3100_event_unregister(struct ab3100 *ab3100,
353 struct notifier_block *nb)
355 return blocking_notifier_chain_unregister(&ab3100->event_subscribers,
358 EXPORT_SYMBOL(ab3100_event_unregister);
361 int ab3100_event_registers_startup_state_get(struct ab3100 *ab3100,
364 if (!ab3100->startup_events_read)
365 return -EAGAIN; /* Try again later */
366 *fatevent = ab3100->startup_events;
369 EXPORT_SYMBOL(ab3100_event_registers_startup_state_get);
371 /* Interrupt handling worker */
372 static void ab3100_work(struct work_struct *work)
374 struct ab3100 *ab3100 = container_of(work, struct ab3100, work);
379 err = ab3100_get_register_page_interruptible(ab3100, AB3100_EVENTA1,
384 fatevent = (event_regs[0] << 16) |
385 (event_regs[1] << 8) |
388 if (!ab3100->startup_events_read) {
389 ab3100->startup_events = fatevent;
390 ab3100->startup_events_read = true;
393 * The notified parties will have to mask out the events
394 * they're interested in and react to them. They will be
395 * notified on all events, then they use the fatevent value
396 * to determine if they're interested.
398 blocking_notifier_call_chain(&ab3100->event_subscribers,
402 "IRQ Event: 0x%08x\n", fatevent);
404 /* By now the IRQ should be acked and deasserted so enable it again */
405 enable_irq(ab3100->i2c_client->irq);
410 "error in event workqueue\n");
411 /* Enable the IRQ anyway, what choice do we have? */
412 enable_irq(ab3100->i2c_client->irq);
416 static irqreturn_t ab3100_irq_handler(int irq, void *data)
418 struct ab3100 *ab3100 = data;
420 * Disable the IRQ and dispatch a worker to handle the
421 * event. Since the chip resides on I2C this is slow
422 * stuff and we will re-enable the interrupts once th
423 * worker has finished.
425 disable_irq(ab3100->i2c_client->irq);
426 schedule_work(&ab3100->work);
430 #ifdef CONFIG_DEBUG_FS
432 * Some debugfs entries only exposed if we're using debug
434 static int ab3100_registers_print(struct seq_file *s, void *p)
436 struct ab3100 *ab3100 = s->private;
440 seq_printf(s, "AB3100 registers:\n");
442 for (reg = 0; reg < 0xff; reg++) {
443 ab3100_get_register_interruptible(ab3100, reg, &value);
444 seq_printf(s, "[0x%x]: 0x%x\n", reg, value);
449 static int ab3100_registers_open(struct inode *inode, struct file *file)
451 return single_open(file, ab3100_registers_print, inode->i_private);
454 static const struct file_operations ab3100_registers_fops = {
455 .open = ab3100_registers_open,
458 .release = single_release,
459 .owner = THIS_MODULE,
462 struct ab3100_get_set_reg_priv {
463 struct ab3100 *ab3100;
467 static int ab3100_get_set_reg_open_file(struct inode *inode, struct file *file)
469 file->private_data = inode->i_private;
473 static ssize_t ab3100_get_set_reg(struct file *file,
474 const char __user *user_buf,
475 size_t count, loff_t *ppos)
477 struct ab3100_get_set_reg_priv *priv = file->private_data;
478 struct ab3100 *ab3100 = priv->ab3100;
482 unsigned long user_reg;
486 /* Get userspace string and assure termination */
487 buf_size = min(count, (sizeof(buf)-1));
488 if (copy_from_user(buf, user_buf, buf_size))
493 * The idea is here to parse a string which is either
494 * "0xnn" for reading a register, or "0xaa 0xbb" for
495 * writing 0xbb to the register 0xaa. First move past
496 * whitespace and then begin to parse the register.
498 while ((i < buf_size) && (buf[i] == ' '))
503 * Advance pointer to end of string then terminate
504 * the register string. This is needed to satisfy
505 * the strict_strtoul() function.
507 while ((i < buf_size) && (buf[i] != ' '))
511 err = strict_strtoul(&buf[regp], 16, &user_reg);
517 /* Either we read or we write a register here */
520 u8 reg = (u8) user_reg;
523 ab3100_get_register_interruptible(ab3100, reg, ®value);
525 dev_info(ab3100->dev,
526 "debug read AB3100 reg[0x%02x]: 0x%02x\n",
530 unsigned long user_value;
531 u8 reg = (u8) user_reg;
536 * Writing, we need some value to write to
537 * the register so keep parsing the string
541 while ((i < buf_size) && (buf[i] == ' '))
544 while ((i < buf_size) && (buf[i] != ' '))
548 err = strict_strtoul(&buf[valp], 16, &user_value);
554 value = (u8) user_value;
555 ab3100_set_register_interruptible(ab3100, reg, value);
556 ab3100_get_register_interruptible(ab3100, reg, ®value);
558 dev_info(ab3100->dev,
559 "debug write reg[0x%02x] with 0x%02x, "
560 "after readback: 0x%02x\n",
561 reg, value, regvalue);
566 static const struct file_operations ab3100_get_set_reg_fops = {
567 .open = ab3100_get_set_reg_open_file,
568 .write = ab3100_get_set_reg,
571 static struct dentry *ab3100_dir;
572 static struct dentry *ab3100_reg_file;
573 static struct ab3100_get_set_reg_priv ab3100_get_priv;
574 static struct dentry *ab3100_get_reg_file;
575 static struct ab3100_get_set_reg_priv ab3100_set_priv;
576 static struct dentry *ab3100_set_reg_file;
578 static void ab3100_setup_debugfs(struct ab3100 *ab3100)
582 ab3100_dir = debugfs_create_dir("ab3100", NULL);
584 goto exit_no_debugfs;
586 ab3100_reg_file = debugfs_create_file("registers",
587 S_IRUGO, ab3100_dir, ab3100,
588 &ab3100_registers_fops);
589 if (!ab3100_reg_file) {
591 goto exit_destroy_dir;
594 ab3100_get_priv.ab3100 = ab3100;
595 ab3100_get_priv.mode = false;
596 ab3100_get_reg_file = debugfs_create_file("get_reg",
597 S_IWUGO, ab3100_dir, &ab3100_get_priv,
598 &ab3100_get_set_reg_fops);
599 if (!ab3100_get_reg_file) {
601 goto exit_destroy_reg;
604 ab3100_set_priv.ab3100 = ab3100;
605 ab3100_set_priv.mode = true;
606 ab3100_set_reg_file = debugfs_create_file("set_reg",
607 S_IWUGO, ab3100_dir, &ab3100_set_priv,
608 &ab3100_get_set_reg_fops);
609 if (!ab3100_set_reg_file) {
611 goto exit_destroy_get_reg;
615 exit_destroy_get_reg:
616 debugfs_remove(ab3100_get_reg_file);
618 debugfs_remove(ab3100_reg_file);
620 debugfs_remove(ab3100_dir);
624 static inline void ab3100_remove_debugfs(void)
626 debugfs_remove(ab3100_set_reg_file);
627 debugfs_remove(ab3100_get_reg_file);
628 debugfs_remove(ab3100_reg_file);
629 debugfs_remove(ab3100_dir);
632 static inline void ab3100_setup_debugfs(struct ab3100 *ab3100)
635 static inline void ab3100_remove_debugfs(void)
641 * Basic set-up, datastructure creation/destruction and I2C interface.
642 * This sets up a default config in the AB3100 chip so that it
643 * will work as expected.
646 struct ab3100_init_setting {
651 static const struct ab3100_init_setting __initdata
652 ab3100_init_settings[] = {
660 .abreg = AB3100_IMRA1,
663 .abreg = AB3100_IMRA2,
666 .abreg = AB3100_IMRA3,
669 .abreg = AB3100_IMRB1,
672 .abreg = AB3100_IMRB2,
675 .abreg = AB3100_IMRB3,
698 static int __init ab3100_setup(struct ab3100 *ab3100)
703 for (i = 0; i < ARRAY_SIZE(ab3100_init_settings); i++) {
704 err = ab3100_set_register_interruptible(ab3100,
705 ab3100_init_settings[i].abreg,
706 ab3100_init_settings[i].setting);
712 * Special trick to make the AB3100 use the 32kHz clock (RTC)
713 * bit 3 in test register 0x02 is a special, undocumented test
714 * register bit that only exist in AB3100 P1E
716 if (ab3100->chip_id == 0xc4) {
717 dev_warn(ab3100->dev,
718 "AB3100 P1E variant detected, "
719 "forcing chip to 32KHz\n");
720 err = ab3100_set_test_register_interruptible(ab3100, 0x02, 0x08);
728 * Here we define all the platform devices that appear
729 * as children of the AB3100. These are regular platform
730 * devices with the IORESOURCE_IO .start and .end set
731 * to correspond to the internal AB3100 register range
732 * mapping to the corresponding subdevice.
735 #define AB3100_DEVICE(devname, devid) \
736 static struct platform_device ab3100_##devname##_device = { \
742 * This lists all the subdevices and corresponding register
745 AB3100_DEVICE(dac, "ab3100-dac");
746 AB3100_DEVICE(leds, "ab3100-leds");
747 AB3100_DEVICE(power, "ab3100-power");
748 AB3100_DEVICE(regulators, "ab3100-regulators");
749 AB3100_DEVICE(sim, "ab3100-sim");
750 AB3100_DEVICE(uart, "ab3100-uart");
751 AB3100_DEVICE(rtc, "ab3100-rtc");
752 AB3100_DEVICE(charger, "ab3100-charger");
753 AB3100_DEVICE(boost, "ab3100-boost");
754 AB3100_DEVICE(adc, "ab3100-adc");
755 AB3100_DEVICE(fuelgauge, "ab3100-fuelgauge");
756 AB3100_DEVICE(vibrator, "ab3100-vibrator");
757 AB3100_DEVICE(otp, "ab3100-otp");
758 AB3100_DEVICE(codec, "ab3100-codec");
760 static struct platform_device *
761 ab3100_platform_devs[] = {
764 &ab3100_power_device,
765 &ab3100_regulators_device,
769 &ab3100_charger_device,
770 &ab3100_boost_device,
772 &ab3100_fuelgauge_device,
773 &ab3100_vibrator_device,
775 &ab3100_codec_device,
778 struct ab_family_id {
783 static const struct ab_family_id ids[] __initdata = {
813 /* AB3000 variants, not supported */
837 static int __init ab3100_probe(struct i2c_client *client,
838 const struct i2c_device_id *id)
840 struct ab3100 *ab3100;
844 ab3100 = kzalloc(sizeof(struct ab3100), GFP_KERNEL);
846 dev_err(&client->dev, "could not allocate AB3100 device\n");
850 /* Initialize data structure */
851 mutex_init(&ab3100->access_mutex);
852 BLOCKING_INIT_NOTIFIER_HEAD(&ab3100->event_subscribers);
854 ab3100->i2c_client = client;
855 ab3100->dev = &ab3100->i2c_client->dev;
857 i2c_set_clientdata(client, ab3100);
859 /* Read chip ID register */
860 err = ab3100_get_register_interruptible(ab3100, AB3100_CID,
863 dev_err(&client->dev,
864 "could not communicate with the AB3100 analog "
869 for (i = 0; ids[i].id != 0x0; i++) {
870 if (ids[i].id == ab3100->chip_id) {
871 if (ids[i].name != NULL) {
872 snprintf(&ab3100->chip_name[0],
873 sizeof(ab3100->chip_name) - 1,
878 dev_err(&client->dev,
879 "AB3000 is not supported\n");
885 if (ids[i].id == 0x0) {
886 dev_err(&client->dev, "unknown analog baseband chip id: 0x%x\n",
888 dev_err(&client->dev, "accepting it anyway. Please update "
893 dev_info(&client->dev, "Detected chip: %s\n",
894 &ab3100->chip_name[0]);
896 /* Attach a second dummy i2c_client to the test register address */
897 ab3100->testreg_client = i2c_new_dummy(client->adapter,
899 if (!ab3100->testreg_client) {
901 goto exit_no_testreg_client;
904 strlcpy(ab3100->testreg_client->name, id->name,
905 sizeof(ab3100->testreg_client->name));
907 err = ab3100_setup(ab3100);
911 INIT_WORK(&ab3100->work, ab3100_work);
913 /* This real unpredictable IRQ is of course sampled for entropy */
914 err = request_irq(client->irq, ab3100_irq_handler,
915 IRQF_DISABLED | IRQF_SAMPLE_RANDOM,
916 "AB3100 IRQ", ab3100);
920 /* Set parent and a pointer back to the container in device data */
921 for (i = 0; i < ARRAY_SIZE(ab3100_platform_devs); i++) {
922 ab3100_platform_devs[i]->dev.parent =
924 platform_set_drvdata(ab3100_platform_devs[i], ab3100);
927 /* Register the platform devices */
928 platform_add_devices(ab3100_platform_devs,
929 ARRAY_SIZE(ab3100_platform_devs));
931 ab3100_setup_debugfs(ab3100);
937 i2c_unregister_device(ab3100->testreg_client);
938 exit_no_testreg_client:
944 static int __exit ab3100_remove(struct i2c_client *client)
946 struct ab3100 *ab3100 = i2c_get_clientdata(client);
949 /* Unregister subdevices */
950 for (i = 0; i < ARRAY_SIZE(ab3100_platform_devs); i++)
951 platform_device_unregister(ab3100_platform_devs[i]);
953 ab3100_remove_debugfs();
954 i2c_unregister_device(ab3100->testreg_client);
957 * At this point, all subscribers should have unregistered
958 * their notifiers so deactivate IRQ
960 free_irq(client->irq, ab3100);
965 static const struct i2c_device_id ab3100_id[] = {
966 { "ab3100", ab3100 },
969 MODULE_DEVICE_TABLE(i2c, ab3100_id);
971 static struct i2c_driver ab3100_driver = {
974 .owner = THIS_MODULE,
976 .id_table = ab3100_id,
977 .probe = ab3100_probe,
978 .remove = __exit_p(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");