linux/audit.h: move ptrace.h include to kernel header
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / mfd / ab3100-core.c
blob2ec7725f4a083fe80dc329f1fa5a8a336f80b10a
1 /*
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>
7 */
9 #include <linux/i2c.h>
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
56 /* SIM power up */
57 #define AB3100_SUP 0x50
60 * I2C communication
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,
73 u8 reg, u8 regval)
75 u8 regandval[2] = {reg, regval};
76 int err;
78 err = mutex_lock_interruptible(&ab3100->access_mutex);
79 if (err)
80 return err;
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);
88 if (err < 0) {
89 dev_err(ab3100->dev,
90 "write error (write register): %d\n",
91 err);
92 } else if (err != 2) {
93 dev_err(ab3100->dev,
94 "write error (write register) "
95 "%d bytes transferred (expected 2)\n",
96 err);
97 err = -EIO;
98 } else {
99 /* All is well */
100 err = 0;
102 mutex_unlock(&ab3100->access_mutex);
103 return err;
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,
122 u8 reg, u8 regval)
124 u8 regandval[2] = {reg, regval};
125 int err;
127 err = mutex_lock_interruptible(&ab3100->access_mutex);
128 if (err)
129 return err;
131 err = i2c_master_send(ab3100->testreg_client, regandval, 2);
132 if (err < 0) {
133 dev_err(ab3100->dev,
134 "write error (write test register): %d\n",
135 err);
136 } else if (err != 2) {
137 dev_err(ab3100->dev,
138 "write error (write test register) "
139 "%d bytes transferred (expected 2)\n",
140 err);
141 err = -EIO;
142 } else {
143 /* All is well */
144 err = 0;
146 mutex_unlock(&ab3100->access_mutex);
148 return err;
151 static int ab3100_get_register_interruptible(struct ab3100 *ab3100,
152 u8 reg, u8 *regval)
154 int err;
156 err = mutex_lock_interruptible(&ab3100->access_mutex);
157 if (err)
158 return err;
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
164 * separately.
166 err = i2c_master_send(ab3100->i2c_client, &reg, 1);
167 if (err < 0) {
168 dev_err(ab3100->dev,
169 "write error (send register address): %d\n",
170 err);
171 goto get_reg_out_unlock;
172 } else if (err != 1) {
173 dev_err(ab3100->dev,
174 "write error (send register address) "
175 "%d bytes transferred (expected 1)\n",
176 err);
177 err = -EIO;
178 goto get_reg_out_unlock;
179 } else {
180 /* All is well */
181 err = 0;
184 err = i2c_master_recv(ab3100->i2c_client, regval, 1);
185 if (err < 0) {
186 dev_err(ab3100->dev,
187 "write error (read register): %d\n",
188 err);
189 goto get_reg_out_unlock;
190 } else if (err != 1) {
191 dev_err(ab3100->dev,
192 "write error (read register) "
193 "%d bytes transferred (expected 1)\n",
194 err);
195 err = -EIO;
196 goto get_reg_out_unlock;
197 } else {
198 /* All is well */
199 err = 0;
202 get_reg_out_unlock:
203 mutex_unlock(&ab3100->access_mutex);
204 return err;
207 static int get_register_interruptible(struct device *dev, u8 bank, u8 reg,
208 u8 *value)
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)
218 int err;
220 if (ab3100->chip_id == 0xa0 ||
221 ab3100->chip_id == 0xa1)
222 /* These don't support paged reads */
223 return -EIO;
225 err = mutex_lock_interruptible(&ab3100->access_mutex);
226 if (err)
227 return err;
230 * Paged read also require an I2C "stop" command.
232 err = i2c_master_send(ab3100->i2c_client, &first_reg, 1);
233 if (err < 0) {
234 dev_err(ab3100->dev,
235 "write error (send first register address): %d\n",
236 err);
237 goto get_reg_page_out_unlock;
238 } else if (err != 1) {
239 dev_err(ab3100->dev,
240 "write error (send first register address) "
241 "%d bytes transferred (expected 1)\n",
242 err);
243 err = -EIO;
244 goto get_reg_page_out_unlock;
247 err = i2c_master_recv(ab3100->i2c_client, regvals, numregs);
248 if (err < 0) {
249 dev_err(ab3100->dev,
250 "write error (read register page): %d\n",
251 err);
252 goto get_reg_page_out_unlock;
253 } else if (err != numregs) {
254 dev_err(ab3100->dev,
255 "write error (read register page) "
256 "%d bytes transferred (expected %d)\n",
257 err, numregs);
258 err = -EIO;
259 goto get_reg_page_out_unlock;
262 /* All is well */
263 err = 0;
265 get_reg_page_out_unlock:
266 mutex_unlock(&ab3100->access_mutex);
267 return err;
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};
283 int err;
285 err = mutex_lock_interruptible(&ab3100->access_mutex);
286 if (err)
287 return err;
289 /* First read out the target register */
290 err = i2c_master_send(ab3100->i2c_client, &reg, 1);
291 if (err < 0) {
292 dev_err(ab3100->dev,
293 "write error (maskset send address): %d\n",
294 err);
295 goto get_maskset_unlock;
296 } else if (err != 1) {
297 dev_err(ab3100->dev,
298 "write error (maskset send address) "
299 "%d bytes transferred (expected 1)\n",
300 err);
301 err = -EIO;
302 goto get_maskset_unlock;
305 err = i2c_master_recv(ab3100->i2c_client, &regandval[1], 1);
306 if (err < 0) {
307 dev_err(ab3100->dev,
308 "write error (maskset read register): %d\n",
309 err);
310 goto get_maskset_unlock;
311 } else if (err != 1) {
312 dev_err(ab3100->dev,
313 "write error (maskset read register) "
314 "%d bytes transferred (expected 1)\n",
315 err);
316 err = -EIO;
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);
326 if (err < 0) {
327 dev_err(ab3100->dev,
328 "write error (write register): %d\n",
329 err);
330 goto get_maskset_unlock;
331 } else if (err != 2) {
332 dev_err(ab3100->dev,
333 "write error (write register) "
334 "%d bytes transferred (expected 2)\n",
335 err);
336 err = -EIO;
337 goto get_maskset_unlock;
340 /* All is well */
341 err = 0;
343 get_maskset_unlock:
344 mutex_unlock(&ab3100->access_mutex);
345 return err;
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,
364 nb);
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,
375 nb);
377 EXPORT_SYMBOL(ab3100_event_unregister);
380 static int ab3100_event_registers_startup_state_get(struct device *dev,
381 u8 *event)
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);
387 return 0;
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
404 * I2C calls etc.
406 static irqreturn_t ab3100_irq_handler(int irq, void *data)
408 struct ab3100 *ab3100 = data;
409 u8 event_regs[3];
410 u32 fatevent;
411 int err;
413 err = ab3100_get_register_page_interruptible(ab3100, AB3100_EVENTA1,
414 event_regs, 3);
415 if (err)
416 goto err_event;
418 fatevent = (event_regs[0] << 16) |
419 (event_regs[1] << 8) |
420 event_regs[2];
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,
435 fatevent, NULL);
437 dev_dbg(ab3100->dev,
438 "IRQ Event: 0x%08x\n", fatevent);
440 return IRQ_HANDLED;
442 err_event:
443 dev_dbg(ab3100->dev,
444 "error reading event status\n");
445 return IRQ_HANDLED;
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;
455 u8 value;
456 u8 reg;
458 seq_printf(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);
464 return 0;
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,
474 .read = seq_read,
475 .llseek = seq_lseek,
476 .release = single_release,
477 .owner = THIS_MODULE,
480 struct ab3100_get_set_reg_priv {
481 struct ab3100 *ab3100;
482 bool mode;
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;
491 char buf[32];
492 ssize_t buf_size;
493 int regp;
494 unsigned long user_reg;
495 int err;
496 int i = 0;
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))
501 return -EFAULT;
502 buf[buf_size] = 0;
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] == ' '))
511 i++;
512 regp = i;
515 * Advance pointer to end of string then terminate
516 * the register string. This is needed to satisfy
517 * the strict_strtoul() function.
519 while ((i < buf_size) && (buf[i] != ' '))
520 i++;
521 buf[i] = '\0';
523 err = strict_strtoul(&buf[regp], 16, &user_reg);
524 if (err)
525 return err;
526 if (user_reg > 0xff)
527 return -EINVAL;
529 /* Either we read or we write a register here */
530 if (!priv->mode) {
531 /* Reading */
532 u8 reg = (u8) user_reg;
533 u8 regvalue;
535 ab3100_get_register_interruptible(ab3100, reg, &regvalue);
537 dev_info(ab3100->dev,
538 "debug read AB3100 reg[0x%02x]: 0x%02x\n",
539 reg, regvalue);
540 } else {
541 int valp;
542 unsigned long user_value;
543 u8 reg = (u8) user_reg;
544 u8 value;
545 u8 regvalue;
548 * Writing, we need some value to write to
549 * the register so keep parsing the string
550 * from userspace.
552 i++;
553 while ((i < buf_size) && (buf[i] == ' '))
554 i++;
555 valp = i;
556 while ((i < buf_size) && (buf[i] != ' '))
557 i++;
558 buf[i] = '\0';
560 err = strict_strtoul(&buf[valp], 16, &user_value);
561 if (err)
562 return err;
563 if (user_reg > 0xff)
564 return -EINVAL;
566 value = (u8) user_value;
567 ab3100_set_register_interruptible(ab3100, reg, value);
568 ab3100_get_register_interruptible(ab3100, reg, &regvalue);
570 dev_info(ab3100->dev,
571 "debug write reg[0x%02x] with 0x%02x, "
572 "after readback: 0x%02x\n",
573 reg, value, regvalue);
575 return buf_size;
578 static const struct file_operations ab3100_get_set_reg_fops = {
579 .open = simple_open,
580 .write = ab3100_get_set_reg,
581 .llseek = noop_llseek,
584 static struct dentry *ab3100_dir;
585 static struct dentry *ab3100_reg_file;
586 static struct ab3100_get_set_reg_priv ab3100_get_priv;
587 static struct dentry *ab3100_get_reg_file;
588 static struct ab3100_get_set_reg_priv ab3100_set_priv;
589 static struct dentry *ab3100_set_reg_file;
591 static void ab3100_setup_debugfs(struct ab3100 *ab3100)
593 int err;
595 ab3100_dir = debugfs_create_dir("ab3100", NULL);
596 if (!ab3100_dir)
597 goto exit_no_debugfs;
599 ab3100_reg_file = debugfs_create_file("registers",
600 S_IRUGO, ab3100_dir, ab3100,
601 &ab3100_registers_fops);
602 if (!ab3100_reg_file) {
603 err = -ENOMEM;
604 goto exit_destroy_dir;
607 ab3100_get_priv.ab3100 = ab3100;
608 ab3100_get_priv.mode = false;
609 ab3100_get_reg_file = debugfs_create_file("get_reg",
610 S_IWUSR, ab3100_dir, &ab3100_get_priv,
611 &ab3100_get_set_reg_fops);
612 if (!ab3100_get_reg_file) {
613 err = -ENOMEM;
614 goto exit_destroy_reg;
617 ab3100_set_priv.ab3100 = ab3100;
618 ab3100_set_priv.mode = true;
619 ab3100_set_reg_file = debugfs_create_file("set_reg",
620 S_IWUSR, ab3100_dir, &ab3100_set_priv,
621 &ab3100_get_set_reg_fops);
622 if (!ab3100_set_reg_file) {
623 err = -ENOMEM;
624 goto exit_destroy_get_reg;
626 return;
628 exit_destroy_get_reg:
629 debugfs_remove(ab3100_get_reg_file);
630 exit_destroy_reg:
631 debugfs_remove(ab3100_reg_file);
632 exit_destroy_dir:
633 debugfs_remove(ab3100_dir);
634 exit_no_debugfs:
635 return;
637 static inline void ab3100_remove_debugfs(void)
639 debugfs_remove(ab3100_set_reg_file);
640 debugfs_remove(ab3100_get_reg_file);
641 debugfs_remove(ab3100_reg_file);
642 debugfs_remove(ab3100_dir);
644 #else
645 static inline void ab3100_setup_debugfs(struct ab3100 *ab3100)
648 static inline void ab3100_remove_debugfs(void)
651 #endif
654 * Basic set-up, datastructure creation/destruction and I2C interface.
655 * This sets up a default config in the AB3100 chip so that it
656 * will work as expected.
659 struct ab3100_init_setting {
660 u8 abreg;
661 u8 setting;
664 static const struct ab3100_init_setting ab3100_init_settings[] = {
666 .abreg = AB3100_MCA,
667 .setting = 0x01
668 }, {
669 .abreg = AB3100_MCB,
670 .setting = 0x30
671 }, {
672 .abreg = AB3100_IMRA1,
673 .setting = 0x00
674 }, {
675 .abreg = AB3100_IMRA2,
676 .setting = 0xFF
677 }, {
678 .abreg = AB3100_IMRA3,
679 .setting = 0x01
680 }, {
681 .abreg = AB3100_IMRB1,
682 .setting = 0xBF
683 }, {
684 .abreg = AB3100_IMRB2,
685 .setting = 0xFF
686 }, {
687 .abreg = AB3100_IMRB3,
688 .setting = 0xFF
689 }, {
690 .abreg = AB3100_SUP,
691 .setting = 0x00
692 }, {
693 .abreg = AB3100_DIS,
694 .setting = 0xF0
695 }, {
696 .abreg = AB3100_D0C,
697 .setting = 0x00
698 }, {
699 .abreg = AB3100_D1C,
700 .setting = 0x00
701 }, {
702 .abreg = AB3100_D2C,
703 .setting = 0x00
704 }, {
705 .abreg = AB3100_D3C,
706 .setting = 0x00
710 static int ab3100_setup(struct ab3100 *ab3100)
712 int err = 0;
713 int i;
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);
719 if (err)
720 goto exit_no_setup;
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,
733 0x02, 0x08);
736 exit_no_setup:
737 return err;
740 /* The subdevices of the AB3100 */
741 static struct mfd_cell ab3100_devs[] = {
743 .name = "ab3100-dac",
744 .id = -1,
747 .name = "ab3100-leds",
748 .id = -1,
751 .name = "ab3100-power",
752 .id = -1,
755 .name = "ab3100-regulators",
756 .id = -1,
759 .name = "ab3100-sim",
760 .id = -1,
763 .name = "ab3100-uart",
764 .id = -1,
767 .name = "ab3100-rtc",
768 .id = -1,
771 .name = "ab3100-charger",
772 .id = -1,
775 .name = "ab3100-boost",
776 .id = -1,
779 .name = "ab3100-adc",
780 .id = -1,
783 .name = "ab3100-fuelgauge",
784 .id = -1,
787 .name = "ab3100-vibrator",
788 .id = -1,
791 .name = "ab3100-otp",
792 .id = -1,
795 .name = "ab3100-codec",
796 .id = -1,
800 struct ab_family_id {
801 u8 id;
802 char *name;
805 static const struct ab_family_id ids[] = {
806 /* AB3100 */
808 .id = 0xc0,
809 .name = "P1A"
810 }, {
811 .id = 0xc1,
812 .name = "P1B"
813 }, {
814 .id = 0xc2,
815 .name = "P1C"
816 }, {
817 .id = 0xc3,
818 .name = "P1D"
819 }, {
820 .id = 0xc4,
821 .name = "P1E"
822 }, {
823 .id = 0xc5,
824 .name = "P1F/R1A"
825 }, {
826 .id = 0xc6,
827 .name = "P1G/R1A"
828 }, {
829 .id = 0xc7,
830 .name = "P2A/R2A"
831 }, {
832 .id = 0xc8,
833 .name = "P2B/R2B"
835 /* AB3000 variants, not supported */
837 .id = 0xa0
838 }, {
839 .id = 0xa1
840 }, {
841 .id = 0xa2
842 }, {
843 .id = 0xa3
844 }, {
845 .id = 0xa4
846 }, {
847 .id = 0xa5
848 }, {
849 .id = 0xa6
850 }, {
851 .id = 0xa7
853 /* Terminator */
855 .id = 0x00,
859 static int 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;
865 int err;
866 int i;
868 ab3100 = devm_kzalloc(&client->dev, sizeof(struct ab3100), GFP_KERNEL);
869 if (!ab3100) {
870 dev_err(&client->dev, "could not allocate AB3100 device\n");
871 return -ENOMEM;
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,
885 &ab3100->chip_id);
886 if (err) {
887 dev_err(&client->dev,
888 "could not communicate with the AB3100 analog "
889 "baseband chip\n");
890 goto exit_no_detect;
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,
898 "AB3100 %s",
899 ids[i].name);
900 break;
901 } else {
902 dev_err(&client->dev,
903 "AB3000 is not supported\n");
904 goto exit_no_detect;
909 if (ids[i].id == 0x0) {
910 dev_err(&client->dev, "unknown analog baseband chip id: 0x%x\n",
911 ab3100->chip_id);
912 dev_err(&client->dev, "accepting it anyway. Please update "
913 "the driver.\n");
914 goto exit_no_detect;
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,
922 client->addr + 1);
923 if (!ab3100->testreg_client) {
924 err = -ENOMEM;
925 goto exit_no_testreg_client;
928 err = ab3100_setup(ab3100);
929 if (err)
930 goto exit_no_setup;
932 err = devm_request_threaded_irq(&client->dev,
933 client->irq, NULL, ab3100_irq_handler,
934 IRQF_ONESHOT, "ab3100-core", ab3100);
935 if (err)
936 goto exit_no_irq;
938 err = abx500_register_ops(&client->dev, &ab3100_ops);
939 if (err)
940 goto exit_no_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);
953 return 0;
955 exit_no_ops:
956 exit_no_irq:
957 exit_no_setup:
958 i2c_unregister_device(ab3100->testreg_client);
959 exit_no_testreg_client:
960 exit_no_detect:
961 return err;
964 static int 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);
972 return 0;
975 static const struct i2c_device_id ab3100_id[] = {
976 { "ab3100", 0 },
979 MODULE_DEVICE_TABLE(i2c, ab3100_id);
981 static struct i2c_driver ab3100_driver = {
982 .driver = {
983 .name = "ab3100",
984 .owner = THIS_MODULE,
986 .id_table = ab3100_id,
987 .probe = ab3100_probe,
988 .remove = 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");