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