Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.git] / drivers / mfd / ab3100-core.c
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  */
8
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/abx500.h>
25
26 /* These are the only registers inside AB3100 used in this main file */
27
28 /* Interrupt event registers */
29 #define AB3100_EVENTA1          0x21
30 #define AB3100_EVENTA2          0x22
31 #define AB3100_EVENTA3          0x23
32
33 /* AB3100 DAC converter registers */
34 #define AB3100_DIS              0x00
35 #define AB3100_D0C              0x01
36 #define AB3100_D1C              0x02
37 #define AB3100_D2C              0x03
38 #define AB3100_D3C              0x04
39
40 /* Chip ID register */
41 #define AB3100_CID              0x20
42
43 /* AB3100 interrupt registers */
44 #define AB3100_IMRA1            0x24
45 #define AB3100_IMRA2            0x25
46 #define AB3100_IMRA3            0x26
47 #define AB3100_IMRB1            0x2B
48 #define AB3100_IMRB2            0x2C
49 #define AB3100_IMRB3            0x2D
50
51 /* System Power Monitoring and control registers */
52 #define AB3100_MCA              0x2E
53 #define AB3100_MCB              0x2F
54
55 /* SIM power up */
56 #define AB3100_SUP              0x50
57
58 /*
59  * I2C communication
60  *
61  * The AB3100 is usually assigned address 0x48 (7-bit)
62  * The chip is defined in the platform i2c_board_data section.
63  */
64 static int ab3100_get_chip_id(struct device *dev)
65 {
66         struct ab3100 *ab3100 = dev_get_drvdata(dev->parent);
67
68         return (int)ab3100->chip_id;
69 }
70
71 static int ab3100_set_register_interruptible(struct ab3100 *ab3100,
72         u8 reg, u8 regval)
73 {
74         u8 regandval[2] = {reg, regval};
75         int err;
76
77         err = mutex_lock_interruptible(&ab3100->access_mutex);
78         if (err)
79                 return err;
80
81         /*
82          * A two-byte write message with the first byte containing the register
83          * number and the second byte containing the value to be written
84          * effectively sets a register in the AB3100.
85          */
86         err = i2c_master_send(ab3100->i2c_client, regandval, 2);
87         if (err < 0) {
88                 dev_err(ab3100->dev,
89                         "write error (write register): %d\n",
90                         err);
91         } else if (err != 2) {
92                 dev_err(ab3100->dev,
93                         "write error (write register) "
94                         "%d bytes transferred (expected 2)\n",
95                         err);
96                 err = -EIO;
97         } else {
98                 /* All is well */
99                 err = 0;
100         }
101         mutex_unlock(&ab3100->access_mutex);
102         return err;
103 }
104
105 static int set_register_interruptible(struct device *dev,
106         u8 bank, u8 reg, u8 value)
107 {
108         struct ab3100 *ab3100 = dev_get_drvdata(dev->parent);
109
110         return ab3100_set_register_interruptible(ab3100, reg, value);
111 }
112
113 /*
114  * The test registers exist at an I2C bus address up one
115  * from the ordinary base. They are not supposed to be used
116  * in production code, but sometimes you have to do that
117  * anyway. It's currently only used from this file so declare
118  * it static and do not export.
119  */
120 static int ab3100_set_test_register_interruptible(struct ab3100 *ab3100,
121                                     u8 reg, u8 regval)
122 {
123         u8 regandval[2] = {reg, regval};
124         int err;
125
126         err = mutex_lock_interruptible(&ab3100->access_mutex);
127         if (err)
128                 return err;
129
130         err = i2c_master_send(ab3100->testreg_client, regandval, 2);
131         if (err < 0) {
132                 dev_err(ab3100->dev,
133                         "write error (write test register): %d\n",
134                         err);
135         } else if (err != 2) {
136                 dev_err(ab3100->dev,
137                         "write error (write test register) "
138                         "%d bytes transferred (expected 2)\n",
139                         err);
140                 err = -EIO;
141         } else {
142                 /* All is well */
143                 err = 0;
144         }
145         mutex_unlock(&ab3100->access_mutex);
146
147         return err;
148 }
149
150 static int ab3100_get_register_interruptible(struct ab3100 *ab3100,
151                                              u8 reg, u8 *regval)
152 {
153         int err;
154
155         err = mutex_lock_interruptible(&ab3100->access_mutex);
156         if (err)
157                 return err;
158
159         /*
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
163          * separately.
164          */
165         err = i2c_master_send(ab3100->i2c_client, &reg, 1);
166         if (err < 0) {
167                 dev_err(ab3100->dev,
168                         "write error (send register address): %d\n",
169                         err);
170                 goto get_reg_out_unlock;
171         } else if (err != 1) {
172                 dev_err(ab3100->dev,
173                         "write error (send register address) "
174                         "%d bytes transferred (expected 1)\n",
175                         err);
176                 err = -EIO;
177                 goto get_reg_out_unlock;
178         } else {
179                 /* All is well */
180                 err = 0;
181         }
182
183         err = i2c_master_recv(ab3100->i2c_client, regval, 1);
184         if (err < 0) {
185                 dev_err(ab3100->dev,
186                         "write error (read register): %d\n",
187                         err);
188                 goto get_reg_out_unlock;
189         } else if (err != 1) {
190                 dev_err(ab3100->dev,
191                         "write error (read register) "
192                         "%d bytes transferred (expected 1)\n",
193                         err);
194                 err = -EIO;
195                 goto get_reg_out_unlock;
196         } else {
197                 /* All is well */
198                 err = 0;
199         }
200
201  get_reg_out_unlock:
202         mutex_unlock(&ab3100->access_mutex);
203         return err;
204 }
205
206 static int get_register_interruptible(struct device *dev, u8 bank, u8 reg,
207                                       u8 *value)
208 {
209         struct ab3100 *ab3100 = dev_get_drvdata(dev->parent);
210
211         return ab3100_get_register_interruptible(ab3100, reg, value);
212 }
213
214 static int ab3100_get_register_page_interruptible(struct ab3100 *ab3100,
215                              u8 first_reg, u8 *regvals, u8 numregs)
216 {
217         int err;
218
219         if (ab3100->chip_id == 0xa0 ||
220             ab3100->chip_id == 0xa1)
221                 /* These don't support paged reads */
222                 return -EIO;
223
224         err = mutex_lock_interruptible(&ab3100->access_mutex);
225         if (err)
226                 return err;
227
228         /*
229          * Paged read also require an I2C "stop" command.
230          */
231         err = i2c_master_send(ab3100->i2c_client, &first_reg, 1);
232         if (err < 0) {
233                 dev_err(ab3100->dev,
234                         "write error (send first register address): %d\n",
235                         err);
236                 goto get_reg_page_out_unlock;
237         } else if (err != 1) {
238                 dev_err(ab3100->dev,
239                         "write error (send first register address) "
240                         "%d bytes transferred (expected 1)\n",
241                         err);
242                 err = -EIO;
243                 goto get_reg_page_out_unlock;
244         }
245
246         err = i2c_master_recv(ab3100->i2c_client, regvals, numregs);
247         if (err < 0) {
248                 dev_err(ab3100->dev,
249                         "write error (read register page): %d\n",
250                         err);
251                 goto get_reg_page_out_unlock;
252         } else if (err != numregs) {
253                 dev_err(ab3100->dev,
254                         "write error (read register page) "
255                         "%d bytes transferred (expected %d)\n",
256                         err, numregs);
257                 err = -EIO;
258                 goto get_reg_page_out_unlock;
259         }
260
261         /* All is well */
262         err = 0;
263
264  get_reg_page_out_unlock:
265         mutex_unlock(&ab3100->access_mutex);
266         return err;
267 }
268
269 static int get_register_page_interruptible(struct device *dev, u8 bank,
270         u8 first_reg, u8 *regvals, u8 numregs)
271 {
272         struct ab3100 *ab3100 = dev_get_drvdata(dev->parent);
273
274         return ab3100_get_register_page_interruptible(ab3100,
275                         first_reg, regvals, numregs);
276 }
277
278 static int ab3100_mask_and_set_register_interruptible(struct ab3100 *ab3100,
279                                  u8 reg, u8 andmask, u8 ormask)
280 {
281         u8 regandval[2] = {reg, 0};
282         int err;
283
284         err = mutex_lock_interruptible(&ab3100->access_mutex);
285         if (err)
286                 return err;
287
288         /* First read out the target register */
289         err = i2c_master_send(ab3100->i2c_client, &reg, 1);
290         if (err < 0) {
291                 dev_err(ab3100->dev,
292                         "write error (maskset send address): %d\n",
293                         err);
294                 goto get_maskset_unlock;
295         } else if (err != 1) {
296                 dev_err(ab3100->dev,
297                         "write error (maskset send address) "
298                         "%d bytes transferred (expected 1)\n",
299                         err);
300                 err = -EIO;
301                 goto get_maskset_unlock;
302         }
303
304         err = i2c_master_recv(ab3100->i2c_client, &regandval[1], 1);
305         if (err < 0) {
306                 dev_err(ab3100->dev,
307                         "write error (maskset read register): %d\n",
308                         err);
309                 goto get_maskset_unlock;
310         } else if (err != 1) {
311                 dev_err(ab3100->dev,
312                         "write error (maskset read register) "
313                         "%d bytes transferred (expected 1)\n",
314                         err);
315                 err = -EIO;
316                 goto get_maskset_unlock;
317         }
318
319         /* Modify the register */
320         regandval[1] &= andmask;
321         regandval[1] |= ormask;
322
323         /* Write the register */
324         err = i2c_master_send(ab3100->i2c_client, regandval, 2);
325         if (err < 0) {
326                 dev_err(ab3100->dev,
327                         "write error (write register): %d\n",
328                         err);
329                 goto get_maskset_unlock;
330         } else if (err != 2) {
331                 dev_err(ab3100->dev,
332                         "write error (write register) "
333                         "%d bytes transferred (expected 2)\n",
334                         err);
335                 err = -EIO;
336                 goto get_maskset_unlock;
337         }
338
339         /* All is well */
340         err = 0;
341
342  get_maskset_unlock:
343         mutex_unlock(&ab3100->access_mutex);
344         return err;
345 }
346
347 static int mask_and_set_register_interruptible(struct device *dev, u8 bank,
348         u8 reg, u8 bitmask, u8 bitvalues)
349 {
350         struct ab3100 *ab3100 = dev_get_drvdata(dev->parent);
351
352         return ab3100_mask_and_set_register_interruptible(ab3100,
353                         reg, bitmask, (bitmask & bitvalues));
354 }
355
356 /*
357  * Register a simple callback for handling any AB3100 events.
358  */
359 int ab3100_event_register(struct ab3100 *ab3100,
360                           struct notifier_block *nb)
361 {
362         return blocking_notifier_chain_register(&ab3100->event_subscribers,
363                                                nb);
364 }
365 EXPORT_SYMBOL(ab3100_event_register);
366
367 /*
368  * Remove a previously registered callback.
369  */
370 int ab3100_event_unregister(struct ab3100 *ab3100,
371                             struct notifier_block *nb)
372 {
373   return blocking_notifier_chain_unregister(&ab3100->event_subscribers,
374                                             nb);
375 }
376 EXPORT_SYMBOL(ab3100_event_unregister);
377
378
379 static int ab3100_event_registers_startup_state_get(struct device *dev,
380                                              u8 *event)
381 {
382         struct ab3100 *ab3100 = dev_get_drvdata(dev->parent);
383         if (!ab3100->startup_events_read)
384                 return -EAGAIN; /* Try again later */
385         memcpy(event, ab3100->startup_events, 3);
386         return 0;
387 }
388
389 static struct abx500_ops ab3100_ops = {
390         .get_chip_id = ab3100_get_chip_id,
391         .set_register = set_register_interruptible,
392         .get_register = get_register_interruptible,
393         .get_register_page = get_register_page_interruptible,
394         .set_register_page = NULL,
395         .mask_and_set_register = mask_and_set_register_interruptible,
396         .event_registers_startup_state_get =
397                 ab3100_event_registers_startup_state_get,
398         .startup_irq_enabled = NULL,
399 };
400
401 /*
402  * This is a threaded interrupt handler so we can make some
403  * I2C calls etc.
404  */
405 static irqreturn_t ab3100_irq_handler(int irq, void *data)
406 {
407         struct ab3100 *ab3100 = data;
408         u8 event_regs[3];
409         u32 fatevent;
410         int err;
411
412         err = ab3100_get_register_page_interruptible(ab3100, AB3100_EVENTA1,
413                                        event_regs, 3);
414         if (err)
415                 goto err_event;
416
417         fatevent = (event_regs[0] << 16) |
418                 (event_regs[1] << 8) |
419                 event_regs[2];
420
421         if (!ab3100->startup_events_read) {
422                 ab3100->startup_events[0] = event_regs[0];
423                 ab3100->startup_events[1] = event_regs[1];
424                 ab3100->startup_events[2] = event_regs[2];
425                 ab3100->startup_events_read = true;
426         }
427         /*
428          * The notified parties will have to mask out the events
429          * they're interested in and react to them. They will be
430          * notified on all events, then they use the fatevent value
431          * to determine if they're interested.
432          */
433         blocking_notifier_call_chain(&ab3100->event_subscribers,
434                                      fatevent, NULL);
435
436         dev_dbg(ab3100->dev,
437                 "IRQ Event: 0x%08x\n", fatevent);
438
439         return IRQ_HANDLED;
440
441  err_event:
442         dev_dbg(ab3100->dev,
443                 "error reading event status\n");
444         return IRQ_HANDLED;
445 }
446
447 #ifdef CONFIG_DEBUG_FS
448 /*
449  * Some debugfs entries only exposed if we're using debug
450  */
451 static int ab3100_registers_print(struct seq_file *s, void *p)
452 {
453         struct ab3100 *ab3100 = s->private;
454         u8 value;
455         u8 reg;
456
457         seq_printf(s, "AB3100 registers:\n");
458
459         for (reg = 0; reg < 0xff; reg++) {
460                 ab3100_get_register_interruptible(ab3100, reg, &value);
461                 seq_printf(s, "[0x%x]:  0x%x\n", reg, value);
462         }
463         return 0;
464 }
465
466 static int ab3100_registers_open(struct inode *inode, struct file *file)
467 {
468         return single_open(file, ab3100_registers_print, inode->i_private);
469 }
470
471 static const struct file_operations ab3100_registers_fops = {
472         .open = ab3100_registers_open,
473         .read = seq_read,
474         .llseek = seq_lseek,
475         .release = single_release,
476         .owner = THIS_MODULE,
477 };
478
479 struct ab3100_get_set_reg_priv {
480         struct ab3100 *ab3100;
481         bool mode;
482 };
483
484 static int ab3100_get_set_reg_open_file(struct inode *inode, struct file *file)
485 {
486         file->private_data = inode->i_private;
487         return 0;
488 }
489
490 static ssize_t ab3100_get_set_reg(struct file *file,
491                                   const char __user *user_buf,
492                                   size_t count, loff_t *ppos)
493 {
494         struct ab3100_get_set_reg_priv *priv = file->private_data;
495         struct ab3100 *ab3100 = priv->ab3100;
496         char buf[32];
497         ssize_t buf_size;
498         int regp;
499         unsigned long user_reg;
500         int err;
501         int i = 0;
502
503         /* Get userspace string and assure termination */
504         buf_size = min(count, (sizeof(buf)-1));
505         if (copy_from_user(buf, user_buf, buf_size))
506                 return -EFAULT;
507         buf[buf_size] = 0;
508
509         /*
510          * The idea is here to parse a string which is either
511          * "0xnn" for reading a register, or "0xaa 0xbb" for
512          * writing 0xbb to the register 0xaa. First move past
513          * whitespace and then begin to parse the register.
514          */
515         while ((i < buf_size) && (buf[i] == ' '))
516                 i++;
517         regp = i;
518
519         /*
520          * Advance pointer to end of string then terminate
521          * the register string. This is needed to satisfy
522          * the strict_strtoul() function.
523          */
524         while ((i < buf_size) && (buf[i] != ' '))
525                 i++;
526         buf[i] = '\0';
527
528         err = strict_strtoul(&buf[regp], 16, &user_reg);
529         if (err)
530                 return err;
531         if (user_reg > 0xff)
532                 return -EINVAL;
533
534         /* Either we read or we write a register here */
535         if (!priv->mode) {
536                 /* Reading */
537                 u8 reg = (u8) user_reg;
538                 u8 regvalue;
539
540                 ab3100_get_register_interruptible(ab3100, reg, &regvalue);
541
542                 dev_info(ab3100->dev,
543                          "debug read AB3100 reg[0x%02x]: 0x%02x\n",
544                          reg, regvalue);
545         } else {
546                 int valp;
547                 unsigned long user_value;
548                 u8 reg = (u8) user_reg;
549                 u8 value;
550                 u8 regvalue;
551
552                 /*
553                  * Writing, we need some value to write to
554                  * the register so keep parsing the string
555                  * from userspace.
556                  */
557                 i++;
558                 while ((i < buf_size) && (buf[i] == ' '))
559                         i++;
560                 valp = i;
561                 while ((i < buf_size) && (buf[i] != ' '))
562                         i++;
563                 buf[i] = '\0';
564
565                 err = strict_strtoul(&buf[valp], 16, &user_value);
566                 if (err)
567                         return err;
568                 if (user_reg > 0xff)
569                         return -EINVAL;
570
571                 value = (u8) user_value;
572                 ab3100_set_register_interruptible(ab3100, reg, value);
573                 ab3100_get_register_interruptible(ab3100, reg, &regvalue);
574
575                 dev_info(ab3100->dev,
576                          "debug write reg[0x%02x] with 0x%02x, "
577                          "after readback: 0x%02x\n",
578                          reg, value, regvalue);
579         }
580         return buf_size;
581 }
582
583 static const struct file_operations ab3100_get_set_reg_fops = {
584         .open = ab3100_get_set_reg_open_file,
585         .write = ab3100_get_set_reg,
586         .llseek = noop_llseek,
587 };
588
589 static struct dentry *ab3100_dir;
590 static struct dentry *ab3100_reg_file;
591 static struct ab3100_get_set_reg_priv ab3100_get_priv;
592 static struct dentry *ab3100_get_reg_file;
593 static struct ab3100_get_set_reg_priv ab3100_set_priv;
594 static struct dentry *ab3100_set_reg_file;
595
596 static void ab3100_setup_debugfs(struct ab3100 *ab3100)
597 {
598         int err;
599
600         ab3100_dir = debugfs_create_dir("ab3100", NULL);
601         if (!ab3100_dir)
602                 goto exit_no_debugfs;
603
604         ab3100_reg_file = debugfs_create_file("registers",
605                                 S_IRUGO, ab3100_dir, ab3100,
606                                 &ab3100_registers_fops);
607         if (!ab3100_reg_file) {
608                 err = -ENOMEM;
609                 goto exit_destroy_dir;
610         }
611
612         ab3100_get_priv.ab3100 = ab3100;
613         ab3100_get_priv.mode = false;
614         ab3100_get_reg_file = debugfs_create_file("get_reg",
615                                 S_IWUSR, ab3100_dir, &ab3100_get_priv,
616                                 &ab3100_get_set_reg_fops);
617         if (!ab3100_get_reg_file) {
618                 err = -ENOMEM;
619                 goto exit_destroy_reg;
620         }
621
622         ab3100_set_priv.ab3100 = ab3100;
623         ab3100_set_priv.mode = true;
624         ab3100_set_reg_file = debugfs_create_file("set_reg",
625                                 S_IWUSR, ab3100_dir, &ab3100_set_priv,
626                                 &ab3100_get_set_reg_fops);
627         if (!ab3100_set_reg_file) {
628                 err = -ENOMEM;
629                 goto exit_destroy_get_reg;
630         }
631         return;
632
633  exit_destroy_get_reg:
634         debugfs_remove(ab3100_get_reg_file);
635  exit_destroy_reg:
636         debugfs_remove(ab3100_reg_file);
637  exit_destroy_dir:
638         debugfs_remove(ab3100_dir);
639  exit_no_debugfs:
640         return;
641 }
642 static inline void ab3100_remove_debugfs(void)
643 {
644         debugfs_remove(ab3100_set_reg_file);
645         debugfs_remove(ab3100_get_reg_file);
646         debugfs_remove(ab3100_reg_file);
647         debugfs_remove(ab3100_dir);
648 }
649 #else
650 static inline void ab3100_setup_debugfs(struct ab3100 *ab3100)
651 {
652 }
653 static inline void ab3100_remove_debugfs(void)
654 {
655 }
656 #endif
657
658 /*
659  * Basic set-up, datastructure creation/destruction and I2C interface.
660  * This sets up a default config in the AB3100 chip so that it
661  * will work as expected.
662  */
663
664 struct ab3100_init_setting {
665         u8 abreg;
666         u8 setting;
667 };
668
669 static const struct ab3100_init_setting __devinitconst
670 ab3100_init_settings[] = {
671         {
672                 .abreg = AB3100_MCA,
673                 .setting = 0x01
674         }, {
675                 .abreg = AB3100_MCB,
676                 .setting = 0x30
677         }, {
678                 .abreg = AB3100_IMRA1,
679                 .setting = 0x00
680         }, {
681                 .abreg = AB3100_IMRA2,
682                 .setting = 0xFF
683         }, {
684                 .abreg = AB3100_IMRA3,
685                 .setting = 0x01
686         }, {
687                 .abreg = AB3100_IMRB1,
688                 .setting = 0xBF
689         }, {
690                 .abreg = AB3100_IMRB2,
691                 .setting = 0xFF
692         }, {
693                 .abreg = AB3100_IMRB3,
694                 .setting = 0xFF
695         }, {
696                 .abreg = AB3100_SUP,
697                 .setting = 0x00
698         }, {
699                 .abreg = AB3100_DIS,
700                 .setting = 0xF0
701         }, {
702                 .abreg = AB3100_D0C,
703                 .setting = 0x00
704         }, {
705                 .abreg = AB3100_D1C,
706                 .setting = 0x00
707         }, {
708                 .abreg = AB3100_D2C,
709                 .setting = 0x00
710         }, {
711                 .abreg = AB3100_D3C,
712                 .setting = 0x00
713         },
714 };
715
716 static int __devinit ab3100_setup(struct ab3100 *ab3100)
717 {
718         int err = 0;
719         int i;
720
721         for (i = 0; i < ARRAY_SIZE(ab3100_init_settings); i++) {
722                 err = ab3100_set_register_interruptible(ab3100,
723                                           ab3100_init_settings[i].abreg,
724                                           ab3100_init_settings[i].setting);
725                 if (err)
726                         goto exit_no_setup;
727         }
728
729         /*
730          * Special trick to make the AB3100 use the 32kHz clock (RTC)
731          * bit 3 in test register 0x02 is a special, undocumented test
732          * register bit that only exist in AB3100 P1E
733          */
734         if (ab3100->chip_id == 0xc4) {
735                 dev_warn(ab3100->dev,
736                          "AB3100 P1E variant detected, "
737                          "forcing chip to 32KHz\n");
738                 err = ab3100_set_test_register_interruptible(ab3100,
739                         0x02, 0x08);
740         }
741
742  exit_no_setup:
743         return err;
744 }
745
746 /* The subdevices of the AB3100 */
747 static struct mfd_cell ab3100_devs[] = {
748         {
749                 .name = "ab3100-dac",
750                 .id = -1,
751         },
752         {
753                 .name = "ab3100-leds",
754                 .id = -1,
755         },
756         {
757                 .name = "ab3100-power",
758                 .id = -1,
759         },
760         {
761                 .name = "ab3100-regulators",
762                 .id = -1,
763         },
764         {
765                 .name = "ab3100-sim",
766                 .id = -1,
767         },
768         {
769                 .name = "ab3100-uart",
770                 .id = -1,
771         },
772         {
773                 .name = "ab3100-rtc",
774                 .id = -1,
775         },
776         {
777                 .name = "ab3100-charger",
778                 .id = -1,
779         },
780         {
781                 .name = "ab3100-boost",
782                 .id = -1,
783         },
784         {
785                 .name = "ab3100-adc",
786                 .id = -1,
787         },
788         {
789                 .name = "ab3100-fuelgauge",
790                 .id = -1,
791         },
792         {
793                 .name = "ab3100-vibrator",
794                 .id = -1,
795         },
796         {
797                 .name = "ab3100-otp",
798                 .id = -1,
799         },
800         {
801                 .name = "ab3100-codec",
802                 .id = -1,
803         },
804 };
805
806 struct ab_family_id {
807         u8      id;
808         char    *name;
809 };
810
811 static const struct ab_family_id ids[] __devinitconst = {
812         /* AB3100 */
813         {
814                 .id = 0xc0,
815                 .name = "P1A"
816         }, {
817                 .id = 0xc1,
818                 .name = "P1B"
819         }, {
820                 .id = 0xc2,
821                 .name = "P1C"
822         }, {
823                 .id = 0xc3,
824                 .name = "P1D"
825         }, {
826                 .id = 0xc4,
827                 .name = "P1E"
828         }, {
829                 .id = 0xc5,
830                 .name = "P1F/R1A"
831         }, {
832                 .id = 0xc6,
833                 .name = "P1G/R1A"
834         }, {
835                 .id = 0xc7,
836                 .name = "P2A/R2A"
837         }, {
838                 .id = 0xc8,
839                 .name = "P2B/R2B"
840         },
841         /* AB3000 variants, not supported */
842         {
843                 .id = 0xa0
844         }, {
845                 .id = 0xa1
846         }, {
847                 .id = 0xa2
848         }, {
849                 .id = 0xa3
850         }, {
851                 .id = 0xa4
852         }, {
853                 .id = 0xa5
854         }, {
855                 .id = 0xa6
856         }, {
857                 .id = 0xa7
858         },
859         /* Terminator */
860         {
861                 .id = 0x00,
862         },
863 };
864
865 static int __devinit ab3100_probe(struct i2c_client *client,
866                                   const struct i2c_device_id *id)
867 {
868         struct ab3100 *ab3100;
869         struct ab3100_platform_data *ab3100_plf_data =
870                 client->dev.platform_data;
871         int err;
872         int i;
873
874         ab3100 = kzalloc(sizeof(struct ab3100), GFP_KERNEL);
875         if (!ab3100) {
876                 dev_err(&client->dev, "could not allocate AB3100 device\n");
877                 return -ENOMEM;
878         }
879
880         /* Initialize data structure */
881         mutex_init(&ab3100->access_mutex);
882         BLOCKING_INIT_NOTIFIER_HEAD(&ab3100->event_subscribers);
883
884         ab3100->i2c_client = client;
885         ab3100->dev = &ab3100->i2c_client->dev;
886
887         i2c_set_clientdata(client, ab3100);
888
889         /* Read chip ID register */
890         err = ab3100_get_register_interruptible(ab3100, AB3100_CID,
891                                                 &ab3100->chip_id);
892         if (err) {
893                 dev_err(&client->dev,
894                         "could not communicate with the AB3100 analog "
895                         "baseband chip\n");
896                 goto exit_no_detect;
897         }
898
899         for (i = 0; ids[i].id != 0x0; i++) {
900                 if (ids[i].id == ab3100->chip_id) {
901                         if (ids[i].name != NULL) {
902                                 snprintf(&ab3100->chip_name[0],
903                                          sizeof(ab3100->chip_name) - 1,
904                                          "AB3100 %s",
905                                          ids[i].name);
906                                 break;
907                         } else {
908                                 dev_err(&client->dev,
909                                         "AB3000 is not supported\n");
910                                 goto exit_no_detect;
911                         }
912                 }
913         }
914
915         if (ids[i].id == 0x0) {
916                 dev_err(&client->dev, "unknown analog baseband chip id: 0x%x\n",
917                         ab3100->chip_id);
918                 dev_err(&client->dev, "accepting it anyway. Please update "
919                         "the driver.\n");
920                 goto exit_no_detect;
921         }
922
923         dev_info(&client->dev, "Detected chip: %s\n",
924                  &ab3100->chip_name[0]);
925
926         /* Attach a second dummy i2c_client to the test register address */
927         ab3100->testreg_client = i2c_new_dummy(client->adapter,
928                                                      client->addr + 1);
929         if (!ab3100->testreg_client) {
930                 err = -ENOMEM;
931                 goto exit_no_testreg_client;
932         }
933
934         err = ab3100_setup(ab3100);
935         if (err)
936                 goto exit_no_setup;
937
938         err = request_threaded_irq(client->irq, NULL, ab3100_irq_handler,
939                                 IRQF_ONESHOT, "ab3100-core", ab3100);
940         /* This real unpredictable IRQ is of course sampled for entropy */
941         rand_initialize_irq(client->irq);
942
943         if (err)
944                 goto exit_no_irq;
945
946         err = abx500_register_ops(&client->dev, &ab3100_ops);
947         if (err)
948                 goto exit_no_ops;
949
950         /* Set up and register the platform devices. */
951         for (i = 0; i < ARRAY_SIZE(ab3100_devs); i++) {
952                 ab3100_devs[i].platform_data = ab3100_plf_data;
953                 ab3100_devs[i].pdata_size = sizeof(struct ab3100_platform_data);
954         }
955
956         err = mfd_add_devices(&client->dev, 0, ab3100_devs,
957                 ARRAY_SIZE(ab3100_devs), NULL, 0);
958
959         ab3100_setup_debugfs(ab3100);
960
961         return 0;
962
963  exit_no_ops:
964  exit_no_irq:
965  exit_no_setup:
966         i2c_unregister_device(ab3100->testreg_client);
967  exit_no_testreg_client:
968  exit_no_detect:
969         kfree(ab3100);
970         return err;
971 }
972
973 static int __devexit ab3100_remove(struct i2c_client *client)
974 {
975         struct ab3100 *ab3100 = i2c_get_clientdata(client);
976
977         /* Unregister subdevices */
978         mfd_remove_devices(&client->dev);
979
980         ab3100_remove_debugfs();
981         i2c_unregister_device(ab3100->testreg_client);
982
983         /*
984          * At this point, all subscribers should have unregistered
985          * their notifiers so deactivate IRQ
986          */
987         free_irq(client->irq, ab3100);
988         kfree(ab3100);
989         return 0;
990 }
991
992 static const struct i2c_device_id ab3100_id[] = {
993         { "ab3100", 0 },
994         { }
995 };
996 MODULE_DEVICE_TABLE(i2c, ab3100_id);
997
998 static struct i2c_driver ab3100_driver = {
999         .driver = {
1000                 .name   = "ab3100",
1001                 .owner  = THIS_MODULE,
1002         },
1003         .id_table       = ab3100_id,
1004         .probe          = ab3100_probe,
1005         .remove         = __devexit_p(ab3100_remove),
1006 };
1007
1008 static int __init ab3100_i2c_init(void)
1009 {
1010         return i2c_add_driver(&ab3100_driver);
1011 }
1012
1013 static void __exit ab3100_i2c_exit(void)
1014 {
1015         i2c_del_driver(&ab3100_driver);
1016 }
1017
1018 subsys_initcall(ab3100_i2c_init);
1019 module_exit(ab3100_i2c_exit);
1020
1021 MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
1022 MODULE_DESCRIPTION("AB3100 core driver");
1023 MODULE_LICENSE("GPL");