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