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