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