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/debugfs.h>
18 #include <linux/seq_file.h>
19 #include <linux/uaccess.h>
20 #include <linux/mfd/ab3100.h>
22 /* These are the only registers inside AB3100 used in this main file */
24 /* Interrupt event registers */
25 #define AB3100_EVENTA1 0x21
26 #define AB3100_EVENTA2 0x22
27 #define AB3100_EVENTA3 0x23
29 /* AB3100 DAC converter registers */
30 #define AB3100_DIS 0x00
31 #define AB3100_D0C 0x01
32 #define AB3100_D1C 0x02
33 #define AB3100_D2C 0x03
34 #define AB3100_D3C 0x04
36 /* Chip ID register */
37 #define AB3100_CID 0x20
39 /* AB3100 interrupt registers */
40 #define AB3100_IMRA1 0x24
41 #define AB3100_IMRA2 0x25
42 #define AB3100_IMRA3 0x26
43 #define AB3100_IMRB1 0x2B
44 #define AB3100_IMRB2 0x2C
45 #define AB3100_IMRB3 0x2D
47 /* System Power Monitoring and control registers */
48 #define AB3100_MCA 0x2E
49 #define AB3100_MCB 0x2F
52 #define AB3100_SUP 0x50
57 * The AB3100 is usually assigned address 0x48 (7-bit)
58 * The chip is defined in the platform i2c_board_data section.
60 static unsigned short normal_i2c[] = { 0x48, I2C_CLIENT_END };
61 I2C_CLIENT_INSMOD_1(ab3100);
63 u8 ab3100_get_chip_type(struct ab3100 *ab3100)
67 switch (ab3100->chip_id & 0xf0) {
77 EXPORT_SYMBOL(ab3100_get_chip_type);
79 int ab3100_set_register_interruptible(struct ab3100 *ab3100, u8 reg, u8 regval)
81 u8 regandval[2] = {reg, regval};
84 err = mutex_lock_interruptible(&ab3100->access_mutex);
89 * A two-byte write message with the first byte containing the register
90 * number and the second byte containing the value to be written
91 * effectively sets a register in the AB3100.
93 err = i2c_master_send(ab3100->i2c_client, regandval, 2);
96 "write error (write register): %d\n",
98 } else if (err != 2) {
100 "write error (write register) "
101 "%d bytes transferred (expected 2)\n",
108 mutex_unlock(&ab3100->access_mutex);
111 EXPORT_SYMBOL(ab3100_set_register_interruptible);
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) "
139 "%d bytes transferred (expected 2)\n",
146 mutex_unlock(&ab3100->access_mutex);
152 int ab3100_get_register_interruptible(struct ab3100 *ab3100, u8 reg, u8 *regval)
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) "
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) "
193 "%d bytes transferred (expected 1)\n",
196 goto get_reg_out_unlock;
203 mutex_unlock(&ab3100->access_mutex);
206 EXPORT_SYMBOL(ab3100_get_register_interruptible);
209 int ab3100_get_register_page_interruptible(struct ab3100 *ab3100,
210 u8 first_reg, u8 *regvals, u8 numregs)
214 if (ab3100->chip_id == 0xa0 ||
215 ab3100->chip_id == 0xa1)
216 /* These don't support paged reads */
219 err = mutex_lock_interruptible(&ab3100->access_mutex);
224 * Paged read also require an I2C "stop" command.
226 err = i2c_master_send(ab3100->i2c_client, &first_reg, 1);
229 "write error (send first register address): %d\n",
231 goto get_reg_page_out_unlock;
232 } else if (err != 1) {
234 "write error (send first register address) "
235 "%d bytes transferred (expected 1)\n",
238 goto get_reg_page_out_unlock;
241 err = i2c_master_recv(ab3100->i2c_client, regvals, numregs);
244 "write error (read register page): %d\n",
246 goto get_reg_page_out_unlock;
247 } else if (err != numregs) {
249 "write error (read register page) "
250 "%d bytes transferred (expected %d)\n",
253 goto get_reg_page_out_unlock;
259 get_reg_page_out_unlock:
260 mutex_unlock(&ab3100->access_mutex);
263 EXPORT_SYMBOL(ab3100_get_register_page_interruptible);
266 int ab3100_mask_and_set_register_interruptible(struct ab3100 *ab3100,
267 u8 reg, u8 andmask, u8 ormask)
269 u8 regandval[2] = {reg, 0};
272 err = mutex_lock_interruptible(&ab3100->access_mutex);
276 /* First read out the target register */
277 err = i2c_master_send(ab3100->i2c_client, ®, 1);
280 "write error (maskset send address): %d\n",
282 goto get_maskset_unlock;
283 } else if (err != 1) {
285 "write error (maskset send address) "
286 "%d bytes transferred (expected 1)\n",
289 goto get_maskset_unlock;
292 err = i2c_master_recv(ab3100->i2c_client, ®andval[1], 1);
295 "write error (maskset read register): %d\n",
297 goto get_maskset_unlock;
298 } else if (err != 1) {
300 "write error (maskset read register) "
301 "%d bytes transferred (expected 1)\n",
304 goto get_maskset_unlock;
307 /* Modify the register */
308 regandval[1] &= andmask;
309 regandval[1] |= ormask;
311 /* Write the register */
312 err = i2c_master_send(ab3100->i2c_client, regandval, 2);
315 "write error (write register): %d\n",
317 goto get_maskset_unlock;
318 } else if (err != 2) {
320 "write error (write register) "
321 "%d bytes transferred (expected 2)\n",
324 goto get_maskset_unlock;
331 mutex_unlock(&ab3100->access_mutex);
334 EXPORT_SYMBOL(ab3100_mask_and_set_register_interruptible);
338 * Register a simple callback for handling any AB3100 events.
340 int ab3100_event_register(struct ab3100 *ab3100,
341 struct notifier_block *nb)
343 return blocking_notifier_chain_register(&ab3100->event_subscribers,
346 EXPORT_SYMBOL(ab3100_event_register);
349 * Remove a previously registered callback.
351 int ab3100_event_unregister(struct ab3100 *ab3100,
352 struct notifier_block *nb)
354 return blocking_notifier_chain_unregister(&ab3100->event_subscribers,
357 EXPORT_SYMBOL(ab3100_event_unregister);
360 int ab3100_event_registers_startup_state_get(struct ab3100 *ab3100,
363 if (!ab3100->startup_events_read)
364 return -EAGAIN; /* Try again later */
365 *fatevent = ab3100->startup_events;
368 EXPORT_SYMBOL(ab3100_event_registers_startup_state_get);
370 /* Interrupt handling worker */
371 static void ab3100_work(struct work_struct *work)
373 struct ab3100 *ab3100 = container_of(work, struct ab3100, work);
378 err = ab3100_get_register_page_interruptible(ab3100, AB3100_EVENTA1,
383 fatevent = (event_regs[0] << 16) |
384 (event_regs[1] << 8) |
387 if (!ab3100->startup_events_read) {
388 ab3100->startup_events = fatevent;
389 ab3100->startup_events_read = true;
392 * The notified parties will have to mask out the events
393 * they're interested in and react to them. They will be
394 * notified on all events, then they use the fatevent value
395 * to determine if they're interested.
397 blocking_notifier_call_chain(&ab3100->event_subscribers,
401 "IRQ Event: 0x%08x\n", fatevent);
403 /* By now the IRQ should be acked and deasserted so enable it again */
404 enable_irq(ab3100->i2c_client->irq);
409 "error in event workqueue\n");
410 /* Enable the IRQ anyway, what choice do we have? */
411 enable_irq(ab3100->i2c_client->irq);
415 static irqreturn_t ab3100_irq_handler(int irq, void *data)
417 struct ab3100 *ab3100 = data;
419 * Disable the IRQ and dispatch a worker to handle the
420 * event. Since the chip resides on I2C this is slow
421 * stuff and we will re-enable the interrupts once th
422 * worker has finished.
424 disable_irq_nosync(irq);
425 schedule_work(&ab3100->work);
429 #ifdef CONFIG_DEBUG_FS
431 * Some debugfs entries only exposed if we're using debug
433 static int ab3100_registers_print(struct seq_file *s, void *p)
435 struct ab3100 *ab3100 = s->private;
439 seq_printf(s, "AB3100 registers:\n");
441 for (reg = 0; reg < 0xff; reg++) {
442 ab3100_get_register_interruptible(ab3100, reg, &value);
443 seq_printf(s, "[0x%x]: 0x%x\n", reg, value);
448 static int ab3100_registers_open(struct inode *inode, struct file *file)
450 return single_open(file, ab3100_registers_print, inode->i_private);
453 static const struct file_operations ab3100_registers_fops = {
454 .open = ab3100_registers_open,
457 .release = single_release,
458 .owner = THIS_MODULE,
461 struct ab3100_get_set_reg_priv {
462 struct ab3100 *ab3100;
466 static int ab3100_get_set_reg_open_file(struct inode *inode, struct file *file)
468 file->private_data = inode->i_private;
472 static ssize_t ab3100_get_set_reg(struct file *file,
473 const char __user *user_buf,
474 size_t count, loff_t *ppos)
476 struct ab3100_get_set_reg_priv *priv = file->private_data;
477 struct ab3100 *ab3100 = priv->ab3100;
481 unsigned long user_reg;
485 /* Get userspace string and assure termination */
486 buf_size = min(count, (sizeof(buf)-1));
487 if (copy_from_user(buf, user_buf, buf_size))
492 * The idea is here to parse a string which is either
493 * "0xnn" for reading a register, or "0xaa 0xbb" for
494 * writing 0xbb to the register 0xaa. First move past
495 * whitespace and then begin to parse the register.
497 while ((i < buf_size) && (buf[i] == ' '))
502 * Advance pointer to end of string then terminate
503 * the register string. This is needed to satisfy
504 * the strict_strtoul() function.
506 while ((i < buf_size) && (buf[i] != ' '))
510 err = strict_strtoul(&buf[regp], 16, &user_reg);
516 /* Either we read or we write a register here */
519 u8 reg = (u8) user_reg;
522 ab3100_get_register_interruptible(ab3100, reg, ®value);
524 dev_info(ab3100->dev,
525 "debug read AB3100 reg[0x%02x]: 0x%02x\n",
529 unsigned long user_value;
530 u8 reg = (u8) user_reg;
535 * Writing, we need some value to write to
536 * the register so keep parsing the string
540 while ((i < buf_size) && (buf[i] == ' '))
543 while ((i < buf_size) && (buf[i] != ' '))
547 err = strict_strtoul(&buf[valp], 16, &user_value);
553 value = (u8) user_value;
554 ab3100_set_register_interruptible(ab3100, reg, value);
555 ab3100_get_register_interruptible(ab3100, reg, ®value);
557 dev_info(ab3100->dev,
558 "debug write reg[0x%02x] with 0x%02x, "
559 "after readback: 0x%02x\n",
560 reg, value, regvalue);
565 static const struct file_operations ab3100_get_set_reg_fops = {
566 .open = ab3100_get_set_reg_open_file,
567 .write = ab3100_get_set_reg,
570 static struct dentry *ab3100_dir;
571 static struct dentry *ab3100_reg_file;
572 static struct ab3100_get_set_reg_priv ab3100_get_priv;
573 static struct dentry *ab3100_get_reg_file;
574 static struct ab3100_get_set_reg_priv ab3100_set_priv;
575 static struct dentry *ab3100_set_reg_file;
577 static void ab3100_setup_debugfs(struct ab3100 *ab3100)
581 ab3100_dir = debugfs_create_dir("ab3100", NULL);
583 goto exit_no_debugfs;
585 ab3100_reg_file = debugfs_create_file("registers",
586 S_IRUGO, ab3100_dir, ab3100,
587 &ab3100_registers_fops);
588 if (!ab3100_reg_file) {
590 goto exit_destroy_dir;
593 ab3100_get_priv.ab3100 = ab3100;
594 ab3100_get_priv.mode = false;
595 ab3100_get_reg_file = debugfs_create_file("get_reg",
596 S_IWUGO, ab3100_dir, &ab3100_get_priv,
597 &ab3100_get_set_reg_fops);
598 if (!ab3100_get_reg_file) {
600 goto exit_destroy_reg;
603 ab3100_set_priv.ab3100 = ab3100;
604 ab3100_set_priv.mode = true;
605 ab3100_set_reg_file = debugfs_create_file("set_reg",
606 S_IWUGO, ab3100_dir, &ab3100_set_priv,
607 &ab3100_get_set_reg_fops);
608 if (!ab3100_set_reg_file) {
610 goto exit_destroy_get_reg;
614 exit_destroy_get_reg:
615 debugfs_remove(ab3100_get_reg_file);
617 debugfs_remove(ab3100_reg_file);
619 debugfs_remove(ab3100_dir);
623 static inline void ab3100_remove_debugfs(void)
625 debugfs_remove(ab3100_set_reg_file);
626 debugfs_remove(ab3100_get_reg_file);
627 debugfs_remove(ab3100_reg_file);
628 debugfs_remove(ab3100_dir);
631 static inline void ab3100_setup_debugfs(struct ab3100 *ab3100)
634 static inline void ab3100_remove_debugfs(void)
640 * Basic set-up, datastructure creation/destruction and I2C interface.
641 * This sets up a default config in the AB3100 chip so that it
642 * will work as expected.
645 struct ab3100_init_setting {
650 static const struct ab3100_init_setting __initdata
651 ab3100_init_settings[] = {
659 .abreg = AB3100_IMRA1,
662 .abreg = AB3100_IMRA2,
665 .abreg = AB3100_IMRA3,
668 .abreg = AB3100_IMRB1,
671 .abreg = AB3100_IMRB2,
674 .abreg = AB3100_IMRB3,
697 static int __init ab3100_setup(struct ab3100 *ab3100)
702 for (i = 0; i < ARRAY_SIZE(ab3100_init_settings); i++) {
703 err = ab3100_set_register_interruptible(ab3100,
704 ab3100_init_settings[i].abreg,
705 ab3100_init_settings[i].setting);
711 * Special trick to make the AB3100 use the 32kHz clock (RTC)
712 * bit 3 in test register 0x02 is a special, undocumented test
713 * register bit that only exist in AB3100 P1E
715 if (ab3100->chip_id == 0xc4) {
716 dev_warn(ab3100->dev,
717 "AB3100 P1E variant detected, "
718 "forcing chip to 32KHz\n");
719 err = ab3100_set_test_register_interruptible(ab3100, 0x02, 0x08);
727 * Here we define all the platform devices that appear
728 * as children of the AB3100. These are regular platform
729 * devices with the IORESOURCE_IO .start and .end set
730 * to correspond to the internal AB3100 register range
731 * mapping to the corresponding subdevice.
734 #define AB3100_DEVICE(devname, devid) \
735 static struct platform_device ab3100_##devname##_device = { \
741 * This lists all the subdevices and corresponding register
744 AB3100_DEVICE(dac, "ab3100-dac");
745 AB3100_DEVICE(leds, "ab3100-leds");
746 AB3100_DEVICE(power, "ab3100-power");
747 AB3100_DEVICE(regulators, "ab3100-regulators");
748 AB3100_DEVICE(sim, "ab3100-sim");
749 AB3100_DEVICE(uart, "ab3100-uart");
750 AB3100_DEVICE(rtc, "ab3100-rtc");
751 AB3100_DEVICE(charger, "ab3100-charger");
752 AB3100_DEVICE(boost, "ab3100-boost");
753 AB3100_DEVICE(adc, "ab3100-adc");
754 AB3100_DEVICE(fuelgauge, "ab3100-fuelgauge");
755 AB3100_DEVICE(vibrator, "ab3100-vibrator");
756 AB3100_DEVICE(otp, "ab3100-otp");
757 AB3100_DEVICE(codec, "ab3100-codec");
759 static struct platform_device *
760 ab3100_platform_devs[] = {
763 &ab3100_power_device,
764 &ab3100_regulators_device,
768 &ab3100_charger_device,
769 &ab3100_boost_device,
771 &ab3100_fuelgauge_device,
772 &ab3100_vibrator_device,
774 &ab3100_codec_device,
777 struct ab_family_id {
782 static const struct ab_family_id ids[] __initdata = {
812 /* AB3000 variants, not supported */
836 static int __init ab3100_probe(struct i2c_client *client,
837 const struct i2c_device_id *id)
839 struct ab3100 *ab3100;
840 struct ab3100_platform_data *ab3100_plf_data =
841 client->dev.platform_data;
845 ab3100 = kzalloc(sizeof(struct ab3100), GFP_KERNEL);
847 dev_err(&client->dev, "could not allocate AB3100 device\n");
851 /* Initialize data structure */
852 mutex_init(&ab3100->access_mutex);
853 BLOCKING_INIT_NOTIFIER_HEAD(&ab3100->event_subscribers);
855 ab3100->i2c_client = client;
856 ab3100->dev = &ab3100->i2c_client->dev;
858 i2c_set_clientdata(client, ab3100);
860 /* Read chip ID register */
861 err = ab3100_get_register_interruptible(ab3100, AB3100_CID,
864 dev_err(&client->dev,
865 "could not communicate with the AB3100 analog "
870 for (i = 0; ids[i].id != 0x0; i++) {
871 if (ids[i].id == ab3100->chip_id) {
872 if (ids[i].name != NULL) {
873 snprintf(&ab3100->chip_name[0],
874 sizeof(ab3100->chip_name) - 1,
879 dev_err(&client->dev,
880 "AB3000 is not supported\n");
886 if (ids[i].id == 0x0) {
887 dev_err(&client->dev, "unknown analog baseband chip id: 0x%x\n",
889 dev_err(&client->dev, "accepting it anyway. Please update "
894 dev_info(&client->dev, "Detected chip: %s\n",
895 &ab3100->chip_name[0]);
897 /* Attach a second dummy i2c_client to the test register address */
898 ab3100->testreg_client = i2c_new_dummy(client->adapter,
900 if (!ab3100->testreg_client) {
902 goto exit_no_testreg_client;
905 strlcpy(ab3100->testreg_client->name, id->name,
906 sizeof(ab3100->testreg_client->name));
908 err = ab3100_setup(ab3100);
912 INIT_WORK(&ab3100->work, ab3100_work);
914 /* This real unpredictable IRQ is of course sampled for entropy */
915 err = request_irq(client->irq, ab3100_irq_handler,
916 IRQF_DISABLED | IRQF_SAMPLE_RANDOM,
917 "AB3100 IRQ", ab3100);
921 /* Set parent and a pointer back to the container in device data */
922 for (i = 0; i < ARRAY_SIZE(ab3100_platform_devs); i++) {
923 ab3100_platform_devs[i]->dev.parent =
925 ab3100_platform_devs[i]->dev.platform_data =
927 platform_set_drvdata(ab3100_platform_devs[i], ab3100);
930 /* Register the platform devices */
931 platform_add_devices(ab3100_platform_devs,
932 ARRAY_SIZE(ab3100_platform_devs));
934 ab3100_setup_debugfs(ab3100);
940 i2c_unregister_device(ab3100->testreg_client);
941 exit_no_testreg_client:
947 static int __exit ab3100_remove(struct i2c_client *client)
949 struct ab3100 *ab3100 = i2c_get_clientdata(client);
952 /* Unregister subdevices */
953 for (i = 0; i < ARRAY_SIZE(ab3100_platform_devs); i++)
954 platform_device_unregister(ab3100_platform_devs[i]);
956 ab3100_remove_debugfs();
957 i2c_unregister_device(ab3100->testreg_client);
960 * At this point, all subscribers should have unregistered
961 * their notifiers so deactivate IRQ
963 free_irq(client->irq, ab3100);
968 static const struct i2c_device_id ab3100_id[] = {
969 { "ab3100", ab3100 },
972 MODULE_DEVICE_TABLE(i2c, ab3100_id);
974 static struct i2c_driver ab3100_driver = {
977 .owner = THIS_MODULE,
979 .id_table = ab3100_id,
980 .probe = ab3100_probe,
981 .remove = __exit_p(ab3100_remove),
984 static int __init ab3100_i2c_init(void)
986 return i2c_add_driver(&ab3100_driver);
989 static void __exit ab3100_i2c_exit(void)
991 i2c_del_driver(&ab3100_driver);
994 subsys_initcall(ab3100_i2c_init);
995 module_exit(ab3100_i2c_exit);
997 MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
998 MODULE_DESCRIPTION("AB3100 core driver");
999 MODULE_LICENSE("GPL");