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