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