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