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