blob: a20e1c41bed2f67c7fcdf65958ef83e62dde98e2 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Linus Walleij14fa5692009-05-21 23:17:06 +020014#include <linux/err.h>
15#include <linux/platform_device.h>
16#include <linux/device.h>
17#include <linux/interrupt.h>
Linus Walleij2071db42010-01-19 11:41:52 +010018#include <linux/random.h>
Linus Walleij14fa5692009-05-21 23:17:06 +020019#include <linux/debugfs.h>
20#include <linux/seq_file.h>
21#include <linux/uaccess.h>
Linus Walleij8c96aef2010-08-20 10:27:12 +020022#include <linux/mfd/core.h>
Linus Walleij812f9e92010-05-01 18:26:07 +020023#include <linux/mfd/abx500.h>
Linus Walleij14fa5692009-05-21 23:17:06 +020024
25/* These are the only registers inside AB3100 used in this main file */
26
27/* Interrupt event registers */
28#define AB3100_EVENTA1 0x21
29#define AB3100_EVENTA2 0x22
30#define AB3100_EVENTA3 0x23
31
32/* AB3100 DAC converter registers */
33#define AB3100_DIS 0x00
34#define AB3100_D0C 0x01
35#define AB3100_D1C 0x02
36#define AB3100_D2C 0x03
37#define AB3100_D3C 0x04
38
39/* Chip ID register */
40#define AB3100_CID 0x20
41
42/* AB3100 interrupt registers */
43#define AB3100_IMRA1 0x24
44#define AB3100_IMRA2 0x25
45#define AB3100_IMRA3 0x26
46#define AB3100_IMRB1 0x2B
47#define AB3100_IMRB2 0x2C
48#define AB3100_IMRB3 0x2D
49
50/* System Power Monitoring and control registers */
51#define AB3100_MCA 0x2E
52#define AB3100_MCB 0x2F
53
54/* SIM power up */
55#define AB3100_SUP 0x50
56
57/*
58 * I2C communication
59 *
60 * The AB3100 is usually assigned address 0x48 (7-bit)
61 * The chip is defined in the platform i2c_board_data section.
62 */
Mattias Wallinfa661252010-05-01 18:26:20 +020063static int ab3100_get_chip_id(struct device *dev)
Linus Walleij14fa5692009-05-21 23:17:06 +020064{
Mattias Wallinfa661252010-05-01 18:26:20 +020065 struct ab3100 *ab3100 = dev_get_drvdata(dev->parent);
Linus Walleij14fa5692009-05-21 23:17:06 +020066
Mattias Wallinfa661252010-05-01 18:26:20 +020067 return (int)ab3100->chip_id;
Linus Walleij14fa5692009-05-21 23:17:06 +020068}
Linus Walleij14fa5692009-05-21 23:17:06 +020069
Mattias Wallinfa661252010-05-01 18:26:20 +020070static int ab3100_set_register_interruptible(struct ab3100 *ab3100,
71 u8 reg, u8 regval)
Linus Walleij14fa5692009-05-21 23:17:06 +020072{
73 u8 regandval[2] = {reg, regval};
74 int err;
75
76 err = mutex_lock_interruptible(&ab3100->access_mutex);
77 if (err)
78 return err;
79
80 /*
81 * A two-byte write message with the first byte containing the register
82 * number and the second byte containing the value to be written
83 * effectively sets a register in the AB3100.
84 */
85 err = i2c_master_send(ab3100->i2c_client, regandval, 2);
86 if (err < 0) {
87 dev_err(ab3100->dev,
88 "write error (write register): %d\n",
89 err);
90 } else if (err != 2) {
91 dev_err(ab3100->dev,
92 "write error (write register) "
93 "%d bytes transferred (expected 2)\n",
94 err);
95 err = -EIO;
96 } else {
97 /* All is well */
98 err = 0;
99 }
100 mutex_unlock(&ab3100->access_mutex);
Linus Walleij7cdc2b92009-08-13 11:49:38 +0200101 return err;
Linus Walleij14fa5692009-05-21 23:17:06 +0200102}
Linus Walleij956f25a2009-08-13 11:49:23 +0200103
Mattias Wallinfa661252010-05-01 18:26:20 +0200104static int set_register_interruptible(struct device *dev,
105 u8 bank, u8 reg, u8 value)
106{
107 struct ab3100 *ab3100 = dev_get_drvdata(dev->parent);
108
109 return ab3100_set_register_interruptible(ab3100, reg, value);
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
Mattias Wallinfa661252010-05-01 18:26:20 +0200149static int ab3100_get_register_interruptible(struct ab3100 *ab3100,
Linus Walleij8c96aef2010-08-20 10:27:12 +0200150 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 Walleij14fa5692009-05-21 23:17:06 +0200204
Mattias Wallinfa661252010-05-01 18:26:20 +0200205static int get_register_interruptible(struct device *dev, u8 bank, u8 reg,
Linus Walleij8c96aef2010-08-20 10:27:12 +0200206 u8 *value)
Mattias Wallinfa661252010-05-01 18:26:20 +0200207{
208 struct ab3100 *ab3100 = dev_get_drvdata(dev->parent);
Linus Walleij956f25a2009-08-13 11:49:23 +0200209
Mattias Wallinfa661252010-05-01 18:26:20 +0200210 return ab3100_get_register_interruptible(ab3100, reg, value);
211}
212
213static int ab3100_get_register_page_interruptible(struct ab3100 *ab3100,
Linus Walleij14fa5692009-05-21 23:17:06 +0200214 u8 first_reg, u8 *regvals, u8 numregs)
215{
216 int err;
217
218 if (ab3100->chip_id == 0xa0 ||
219 ab3100->chip_id == 0xa1)
220 /* These don't support paged reads */
221 return -EIO;
222
223 err = mutex_lock_interruptible(&ab3100->access_mutex);
224 if (err)
225 return err;
226
227 /*
228 * Paged read also require an I2C "stop" command.
229 */
230 err = i2c_master_send(ab3100->i2c_client, &first_reg, 1);
231 if (err < 0) {
232 dev_err(ab3100->dev,
233 "write error (send first register address): %d\n",
234 err);
235 goto get_reg_page_out_unlock;
236 } else if (err != 1) {
237 dev_err(ab3100->dev,
238 "write error (send first register address) "
239 "%d bytes transferred (expected 1)\n",
240 err);
241 err = -EIO;
242 goto get_reg_page_out_unlock;
243 }
244
245 err = i2c_master_recv(ab3100->i2c_client, regvals, numregs);
246 if (err < 0) {
247 dev_err(ab3100->dev,
248 "write error (read register page): %d\n",
249 err);
250 goto get_reg_page_out_unlock;
251 } else if (err != numregs) {
252 dev_err(ab3100->dev,
253 "write error (read register page) "
254 "%d bytes transferred (expected %d)\n",
255 err, numregs);
256 err = -EIO;
257 goto get_reg_page_out_unlock;
258 }
259
260 /* All is well */
261 err = 0;
262
263 get_reg_page_out_unlock:
264 mutex_unlock(&ab3100->access_mutex);
265 return err;
266}
Linus Walleij14fa5692009-05-21 23:17:06 +0200267
Mattias Wallinfa661252010-05-01 18:26:20 +0200268static int get_register_page_interruptible(struct device *dev, u8 bank,
269 u8 first_reg, u8 *regvals, u8 numregs)
270{
271 struct ab3100 *ab3100 = dev_get_drvdata(dev->parent);
Linus Walleij956f25a2009-08-13 11:49:23 +0200272
Mattias Wallinfa661252010-05-01 18:26:20 +0200273 return ab3100_get_register_page_interruptible(ab3100,
274 first_reg, regvals, numregs);
275}
276
277static int ab3100_mask_and_set_register_interruptible(struct ab3100 *ab3100,
Linus Walleij14fa5692009-05-21 23:17:06 +0200278 u8 reg, u8 andmask, u8 ormask)
279{
280 u8 regandval[2] = {reg, 0};
281 int err;
282
283 err = mutex_lock_interruptible(&ab3100->access_mutex);
284 if (err)
285 return err;
286
287 /* First read out the target register */
288 err = i2c_master_send(ab3100->i2c_client, &reg, 1);
289 if (err < 0) {
290 dev_err(ab3100->dev,
291 "write error (maskset send address): %d\n",
292 err);
293 goto get_maskset_unlock;
294 } else if (err != 1) {
295 dev_err(ab3100->dev,
296 "write error (maskset send address) "
297 "%d bytes transferred (expected 1)\n",
298 err);
299 err = -EIO;
300 goto get_maskset_unlock;
301 }
302
303 err = i2c_master_recv(ab3100->i2c_client, &regandval[1], 1);
304 if (err < 0) {
305 dev_err(ab3100->dev,
306 "write error (maskset read register): %d\n",
307 err);
308 goto get_maskset_unlock;
309 } else if (err != 1) {
310 dev_err(ab3100->dev,
311 "write error (maskset read register) "
312 "%d bytes transferred (expected 1)\n",
313 err);
314 err = -EIO;
315 goto get_maskset_unlock;
316 }
317
318 /* Modify the register */
319 regandval[1] &= andmask;
320 regandval[1] |= ormask;
321
322 /* Write the register */
323 err = i2c_master_send(ab3100->i2c_client, regandval, 2);
324 if (err < 0) {
325 dev_err(ab3100->dev,
326 "write error (write register): %d\n",
327 err);
328 goto get_maskset_unlock;
329 } else if (err != 2) {
330 dev_err(ab3100->dev,
331 "write error (write register) "
332 "%d bytes transferred (expected 2)\n",
333 err);
334 err = -EIO;
335 goto get_maskset_unlock;
336 }
337
338 /* All is well */
339 err = 0;
340
341 get_maskset_unlock:
342 mutex_unlock(&ab3100->access_mutex);
343 return err;
344}
Linus Walleij956f25a2009-08-13 11:49:23 +0200345
Mattias Wallinfa661252010-05-01 18:26:20 +0200346static int mask_and_set_register_interruptible(struct device *dev, u8 bank,
347 u8 reg, u8 bitmask, u8 bitvalues)
348{
349 struct ab3100 *ab3100 = dev_get_drvdata(dev->parent);
350
351 return ab3100_mask_and_set_register_interruptible(ab3100,
352 reg, bitmask, (bitmask & bitvalues));
353}
Linus Walleij14fa5692009-05-21 23:17:06 +0200354
355/*
356 * Register a simple callback for handling any AB3100 events.
357 */
358int ab3100_event_register(struct ab3100 *ab3100,
359 struct notifier_block *nb)
360{
361 return blocking_notifier_chain_register(&ab3100->event_subscribers,
362 nb);
363}
364EXPORT_SYMBOL(ab3100_event_register);
365
366/*
367 * Remove a previously registered callback.
368 */
369int ab3100_event_unregister(struct ab3100 *ab3100,
370 struct notifier_block *nb)
371{
372 return blocking_notifier_chain_unregister(&ab3100->event_subscribers,
373 nb);
374}
375EXPORT_SYMBOL(ab3100_event_unregister);
376
377
Mattias Wallinfa661252010-05-01 18:26:20 +0200378static int ab3100_event_registers_startup_state_get(struct device *dev,
379 u8 *event)
Linus Walleij14fa5692009-05-21 23:17:06 +0200380{
Mattias Wallinfa661252010-05-01 18:26:20 +0200381 struct ab3100 *ab3100 = dev_get_drvdata(dev->parent);
Linus Walleij14fa5692009-05-21 23:17:06 +0200382 if (!ab3100->startup_events_read)
383 return -EAGAIN; /* Try again later */
Mattias Wallinfa661252010-05-01 18:26:20 +0200384 memcpy(event, ab3100->startup_events, 3);
Linus Walleij14fa5692009-05-21 23:17:06 +0200385 return 0;
386}
Mattias Wallinfa661252010-05-01 18:26:20 +0200387
388static struct abx500_ops ab3100_ops = {
389 .get_chip_id = ab3100_get_chip_id,
390 .set_register = set_register_interruptible,
391 .get_register = get_register_interruptible,
392 .get_register_page = get_register_page_interruptible,
393 .set_register_page = NULL,
394 .mask_and_set_register = mask_and_set_register_interruptible,
395 .event_registers_startup_state_get =
396 ab3100_event_registers_startup_state_get,
397 .startup_irq_enabled = NULL,
398};
Linus Walleij14fa5692009-05-21 23:17:06 +0200399
Linus Walleij0df883d2010-01-08 10:44:16 +0100400/*
401 * This is a threaded interrupt handler so we can make some
402 * I2C calls etc.
403 */
404static irqreturn_t ab3100_irq_handler(int irq, void *data)
Linus Walleij14fa5692009-05-21 23:17:06 +0200405{
Linus Walleij0df883d2010-01-08 10:44:16 +0100406 struct ab3100 *ab3100 = data;
Linus Walleij14fa5692009-05-21 23:17:06 +0200407 u8 event_regs[3];
408 u32 fatevent;
409 int err;
410
Linus Walleij2071db42010-01-19 11:41:52 +0100411 add_interrupt_randomness(irq);
412
Linus Walleij956f25a2009-08-13 11:49:23 +0200413 err = ab3100_get_register_page_interruptible(ab3100, AB3100_EVENTA1,
Linus Walleij14fa5692009-05-21 23:17:06 +0200414 event_regs, 3);
415 if (err)
Linus Walleij0df883d2010-01-08 10:44:16 +0100416 goto err_event;
Linus Walleij14fa5692009-05-21 23:17:06 +0200417
418 fatevent = (event_regs[0] << 16) |
419 (event_regs[1] << 8) |
420 event_regs[2];
421
422 if (!ab3100->startup_events_read) {
Mattias Wallinfa661252010-05-01 18:26:20 +0200423 ab3100->startup_events[0] = event_regs[0];
424 ab3100->startup_events[1] = event_regs[1];
425 ab3100->startup_events[2] = event_regs[2];
Linus Walleij14fa5692009-05-21 23:17:06 +0200426 ab3100->startup_events_read = true;
427 }
428 /*
429 * The notified parties will have to mask out the events
430 * they're interested in and react to them. They will be
431 * notified on all events, then they use the fatevent value
432 * to determine if they're interested.
433 */
434 blocking_notifier_call_chain(&ab3100->event_subscribers,
435 fatevent, NULL);
436
437 dev_dbg(ab3100->dev,
438 "IRQ Event: 0x%08x\n", fatevent);
439
Linus Walleij0df883d2010-01-08 10:44:16 +0100440 return IRQ_HANDLED;
Linus Walleij14fa5692009-05-21 23:17:06 +0200441
Linus Walleij0df883d2010-01-08 10:44:16 +0100442 err_event:
Linus Walleij14fa5692009-05-21 23:17:06 +0200443 dev_dbg(ab3100->dev,
Linus Walleij0df883d2010-01-08 10:44:16 +0100444 "error reading event status\n");
Linus Walleij14fa5692009-05-21 23:17:06 +0200445 return IRQ_HANDLED;
446}
447
448#ifdef CONFIG_DEBUG_FS
449/*
450 * Some debugfs entries only exposed if we're using debug
451 */
452static int ab3100_registers_print(struct seq_file *s, void *p)
453{
454 struct ab3100 *ab3100 = s->private;
455 u8 value;
456 u8 reg;
457
458 seq_printf(s, "AB3100 registers:\n");
459
460 for (reg = 0; reg < 0xff; reg++) {
Linus Walleij956f25a2009-08-13 11:49:23 +0200461 ab3100_get_register_interruptible(ab3100, reg, &value);
Linus Walleij14fa5692009-05-21 23:17:06 +0200462 seq_printf(s, "[0x%x]: 0x%x\n", reg, value);
463 }
464 return 0;
465}
466
467static int ab3100_registers_open(struct inode *inode, struct file *file)
468{
469 return single_open(file, ab3100_registers_print, inode->i_private);
470}
471
472static const struct file_operations ab3100_registers_fops = {
473 .open = ab3100_registers_open,
474 .read = seq_read,
475 .llseek = seq_lseek,
476 .release = single_release,
477 .owner = THIS_MODULE,
478};
479
480struct ab3100_get_set_reg_priv {
481 struct ab3100 *ab3100;
482 bool mode;
483};
484
485static int ab3100_get_set_reg_open_file(struct inode *inode, struct file *file)
486{
487 file->private_data = inode->i_private;
488 return 0;
489}
490
Samuel Ortizdfc3aa72009-06-23 10:48:36 +0200491static ssize_t ab3100_get_set_reg(struct file *file,
492 const char __user *user_buf,
493 size_t count, loff_t *ppos)
Linus Walleij14fa5692009-05-21 23:17:06 +0200494{
495 struct ab3100_get_set_reg_priv *priv = file->private_data;
496 struct ab3100 *ab3100 = priv->ab3100;
497 char buf[32];
Samuel Ortizdfc3aa72009-06-23 10:48:36 +0200498 ssize_t buf_size;
Linus Walleij14fa5692009-05-21 23:17:06 +0200499 int regp;
500 unsigned long user_reg;
501 int err;
502 int i = 0;
503
504 /* Get userspace string and assure termination */
505 buf_size = min(count, (sizeof(buf)-1));
506 if (copy_from_user(buf, user_buf, buf_size))
507 return -EFAULT;
508 buf[buf_size] = 0;
509
510 /*
511 * The idea is here to parse a string which is either
512 * "0xnn" for reading a register, or "0xaa 0xbb" for
513 * writing 0xbb to the register 0xaa. First move past
514 * whitespace and then begin to parse the register.
515 */
516 while ((i < buf_size) && (buf[i] == ' '))
517 i++;
518 regp = i;
519
520 /*
521 * Advance pointer to end of string then terminate
522 * the register string. This is needed to satisfy
523 * the strict_strtoul() function.
524 */
525 while ((i < buf_size) && (buf[i] != ' '))
526 i++;
527 buf[i] = '\0';
528
529 err = strict_strtoul(&buf[regp], 16, &user_reg);
530 if (err)
531 return err;
532 if (user_reg > 0xff)
533 return -EINVAL;
534
535 /* Either we read or we write a register here */
536 if (!priv->mode) {
537 /* Reading */
538 u8 reg = (u8) user_reg;
539 u8 regvalue;
540
Linus Walleij956f25a2009-08-13 11:49:23 +0200541 ab3100_get_register_interruptible(ab3100, reg, &regvalue);
Linus Walleij14fa5692009-05-21 23:17:06 +0200542
543 dev_info(ab3100->dev,
544 "debug read AB3100 reg[0x%02x]: 0x%02x\n",
545 reg, regvalue);
546 } else {
547 int valp;
548 unsigned long user_value;
549 u8 reg = (u8) user_reg;
550 u8 value;
551 u8 regvalue;
552
553 /*
554 * Writing, we need some value to write to
555 * the register so keep parsing the string
556 * from userspace.
557 */
558 i++;
559 while ((i < buf_size) && (buf[i] == ' '))
560 i++;
561 valp = i;
562 while ((i < buf_size) && (buf[i] != ' '))
563 i++;
564 buf[i] = '\0';
565
566 err = strict_strtoul(&buf[valp], 16, &user_value);
567 if (err)
568 return err;
569 if (user_reg > 0xff)
570 return -EINVAL;
571
572 value = (u8) user_value;
Linus Walleij956f25a2009-08-13 11:49:23 +0200573 ab3100_set_register_interruptible(ab3100, reg, value);
574 ab3100_get_register_interruptible(ab3100, reg, &regvalue);
Linus Walleij14fa5692009-05-21 23:17:06 +0200575
576 dev_info(ab3100->dev,
577 "debug write reg[0x%02x] with 0x%02x, "
578 "after readback: 0x%02x\n",
579 reg, value, regvalue);
580 }
581 return buf_size;
582}
583
584static const struct file_operations ab3100_get_set_reg_fops = {
585 .open = ab3100_get_set_reg_open_file,
586 .write = ab3100_get_set_reg,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200587 .llseek = noop_llseek,
Linus Walleij14fa5692009-05-21 23:17:06 +0200588};
589
590static struct dentry *ab3100_dir;
591static struct dentry *ab3100_reg_file;
592static struct ab3100_get_set_reg_priv ab3100_get_priv;
593static struct dentry *ab3100_get_reg_file;
594static struct ab3100_get_set_reg_priv ab3100_set_priv;
595static struct dentry *ab3100_set_reg_file;
596
597static void ab3100_setup_debugfs(struct ab3100 *ab3100)
598{
599 int err;
600
601 ab3100_dir = debugfs_create_dir("ab3100", NULL);
602 if (!ab3100_dir)
603 goto exit_no_debugfs;
604
605 ab3100_reg_file = debugfs_create_file("registers",
606 S_IRUGO, ab3100_dir, ab3100,
607 &ab3100_registers_fops);
608 if (!ab3100_reg_file) {
609 err = -ENOMEM;
610 goto exit_destroy_dir;
611 }
612
613 ab3100_get_priv.ab3100 = ab3100;
614 ab3100_get_priv.mode = false;
615 ab3100_get_reg_file = debugfs_create_file("get_reg",
Vasiliy Kulikovf8a06972011-02-04 15:23:36 +0300616 S_IWUSR, ab3100_dir, &ab3100_get_priv,
Linus Walleij14fa5692009-05-21 23:17:06 +0200617 &ab3100_get_set_reg_fops);
618 if (!ab3100_get_reg_file) {
619 err = -ENOMEM;
620 goto exit_destroy_reg;
621 }
622
623 ab3100_set_priv.ab3100 = ab3100;
624 ab3100_set_priv.mode = true;
625 ab3100_set_reg_file = debugfs_create_file("set_reg",
Vasiliy Kulikovf8a06972011-02-04 15:23:36 +0300626 S_IWUSR, ab3100_dir, &ab3100_set_priv,
Linus Walleij14fa5692009-05-21 23:17:06 +0200627 &ab3100_get_set_reg_fops);
628 if (!ab3100_set_reg_file) {
629 err = -ENOMEM;
630 goto exit_destroy_get_reg;
631 }
632 return;
633
634 exit_destroy_get_reg:
635 debugfs_remove(ab3100_get_reg_file);
636 exit_destroy_reg:
637 debugfs_remove(ab3100_reg_file);
638 exit_destroy_dir:
639 debugfs_remove(ab3100_dir);
640 exit_no_debugfs:
641 return;
642}
643static inline void ab3100_remove_debugfs(void)
644{
645 debugfs_remove(ab3100_set_reg_file);
646 debugfs_remove(ab3100_get_reg_file);
647 debugfs_remove(ab3100_reg_file);
648 debugfs_remove(ab3100_dir);
649}
650#else
651static inline void ab3100_setup_debugfs(struct ab3100 *ab3100)
652{
653}
654static inline void ab3100_remove_debugfs(void)
655{
656}
657#endif
658
659/*
660 * Basic set-up, datastructure creation/destruction and I2C interface.
661 * This sets up a default config in the AB3100 chip so that it
662 * will work as expected.
663 */
664
665struct ab3100_init_setting {
666 u8 abreg;
667 u8 setting;
668};
669
Linus Walleij25244682010-08-20 10:26:56 +0200670static const struct ab3100_init_setting __devinitconst
Linus Walleij14fa5692009-05-21 23:17:06 +0200671ab3100_init_settings[] = {
672 {
673 .abreg = AB3100_MCA,
674 .setting = 0x01
675 }, {
676 .abreg = AB3100_MCB,
677 .setting = 0x30
678 }, {
679 .abreg = AB3100_IMRA1,
680 .setting = 0x00
681 }, {
682 .abreg = AB3100_IMRA2,
683 .setting = 0xFF
684 }, {
685 .abreg = AB3100_IMRA3,
686 .setting = 0x01
687 }, {
688 .abreg = AB3100_IMRB1,
Linus Walleijce290b02009-08-13 11:49:49 +0200689 .setting = 0xBF
Linus Walleij14fa5692009-05-21 23:17:06 +0200690 }, {
691 .abreg = AB3100_IMRB2,
692 .setting = 0xFF
693 }, {
694 .abreg = AB3100_IMRB3,
695 .setting = 0xFF
696 }, {
697 .abreg = AB3100_SUP,
698 .setting = 0x00
699 }, {
700 .abreg = AB3100_DIS,
701 .setting = 0xF0
702 }, {
703 .abreg = AB3100_D0C,
704 .setting = 0x00
705 }, {
706 .abreg = AB3100_D1C,
707 .setting = 0x00
708 }, {
709 .abreg = AB3100_D2C,
710 .setting = 0x00
711 }, {
712 .abreg = AB3100_D3C,
713 .setting = 0x00
714 },
715};
716
Linus Walleij25244682010-08-20 10:26:56 +0200717static int __devinit ab3100_setup(struct ab3100 *ab3100)
Linus Walleij14fa5692009-05-21 23:17:06 +0200718{
719 int err = 0;
720 int i;
721
722 for (i = 0; i < ARRAY_SIZE(ab3100_init_settings); i++) {
Linus Walleij956f25a2009-08-13 11:49:23 +0200723 err = ab3100_set_register_interruptible(ab3100,
Linus Walleij14fa5692009-05-21 23:17:06 +0200724 ab3100_init_settings[i].abreg,
725 ab3100_init_settings[i].setting);
726 if (err)
727 goto exit_no_setup;
728 }
729
730 /*
731 * Special trick to make the AB3100 use the 32kHz clock (RTC)
Linus Walleij956f25a2009-08-13 11:49:23 +0200732 * bit 3 in test register 0x02 is a special, undocumented test
Linus Walleij14fa5692009-05-21 23:17:06 +0200733 * register bit that only exist in AB3100 P1E
734 */
735 if (ab3100->chip_id == 0xc4) {
736 dev_warn(ab3100->dev,
737 "AB3100 P1E variant detected, "
738 "forcing chip to 32KHz\n");
Mattias Wallinfa661252010-05-01 18:26:20 +0200739 err = ab3100_set_test_register_interruptible(ab3100,
740 0x02, 0x08);
Linus Walleij14fa5692009-05-21 23:17:06 +0200741 }
742
743 exit_no_setup:
744 return err;
745}
746
Linus Walleij8c96aef2010-08-20 10:27:12 +0200747/* The subdevices of the AB3100 */
748static struct mfd_cell ab3100_devs[] = {
749 {
750 .name = "ab3100-dac",
751 .id = -1,
752 },
753 {
754 .name = "ab3100-leds",
755 .id = -1,
756 },
757 {
758 .name = "ab3100-power",
759 .id = -1,
760 },
761 {
762 .name = "ab3100-regulators",
763 .id = -1,
764 },
765 {
766 .name = "ab3100-sim",
767 .id = -1,
768 },
769 {
770 .name = "ab3100-uart",
771 .id = -1,
772 },
773 {
774 .name = "ab3100-rtc",
775 .id = -1,
776 },
777 {
778 .name = "ab3100-charger",
779 .id = -1,
780 },
781 {
782 .name = "ab3100-boost",
783 .id = -1,
784 },
785 {
786 .name = "ab3100-adc",
787 .id = -1,
788 },
789 {
790 .name = "ab3100-fuelgauge",
791 .id = -1,
792 },
793 {
794 .name = "ab3100-vibrator",
795 .id = -1,
796 },
797 {
798 .name = "ab3100-otp",
799 .id = -1,
800 },
801 {
802 .name = "ab3100-codec",
803 .id = -1,
804 },
Linus Walleij14fa5692009-05-21 23:17:06 +0200805};
806
807struct ab_family_id {
808 u8 id;
809 char *name;
810};
811
Linus Walleij25244682010-08-20 10:26:56 +0200812static const struct ab_family_id ids[] __devinitdata = {
Linus Walleij14fa5692009-05-21 23:17:06 +0200813 /* AB3100 */
814 {
815 .id = 0xc0,
816 .name = "P1A"
817 }, {
818 .id = 0xc1,
819 .name = "P1B"
820 }, {
821 .id = 0xc2,
822 .name = "P1C"
823 }, {
824 .id = 0xc3,
825 .name = "P1D"
826 }, {
827 .id = 0xc4,
828 .name = "P1E"
829 }, {
830 .id = 0xc5,
831 .name = "P1F/R1A"
832 }, {
833 .id = 0xc6,
834 .name = "P1G/R1A"
835 }, {
836 .id = 0xc7,
837 .name = "P2A/R2A"
838 }, {
839 .id = 0xc8,
840 .name = "P2B/R2B"
841 },
842 /* AB3000 variants, not supported */
843 {
844 .id = 0xa0
845 }, {
846 .id = 0xa1
847 }, {
848 .id = 0xa2
849 }, {
850 .id = 0xa3
851 }, {
852 .id = 0xa4
853 }, {
854 .id = 0xa5
855 }, {
856 .id = 0xa6
857 }, {
858 .id = 0xa7
859 },
860 /* Terminator */
861 {
862 .id = 0x00,
863 },
864};
865
Linus Walleij25244682010-08-20 10:26:56 +0200866static int __devinit ab3100_probe(struct i2c_client *client,
867 const struct i2c_device_id *id)
Linus Walleij14fa5692009-05-21 23:17:06 +0200868{
869 struct ab3100 *ab3100;
Linus Walleijd619bc12009-09-09 11:31:00 +0200870 struct ab3100_platform_data *ab3100_plf_data =
871 client->dev.platform_data;
Linus Walleij14fa5692009-05-21 23:17:06 +0200872 int err;
873 int i;
874
875 ab3100 = kzalloc(sizeof(struct ab3100), GFP_KERNEL);
876 if (!ab3100) {
877 dev_err(&client->dev, "could not allocate AB3100 device\n");
878 return -ENOMEM;
879 }
880
881 /* Initialize data structure */
882 mutex_init(&ab3100->access_mutex);
883 BLOCKING_INIT_NOTIFIER_HEAD(&ab3100->event_subscribers);
884
885 ab3100->i2c_client = client;
886 ab3100->dev = &ab3100->i2c_client->dev;
887
888 i2c_set_clientdata(client, ab3100);
889
890 /* Read chip ID register */
Linus Walleij956f25a2009-08-13 11:49:23 +0200891 err = ab3100_get_register_interruptible(ab3100, AB3100_CID,
892 &ab3100->chip_id);
Linus Walleij14fa5692009-05-21 23:17:06 +0200893 if (err) {
894 dev_err(&client->dev,
895 "could not communicate with the AB3100 analog "
896 "baseband chip\n");
897 goto exit_no_detect;
898 }
899
900 for (i = 0; ids[i].id != 0x0; i++) {
901 if (ids[i].id == ab3100->chip_id) {
902 if (ids[i].name != NULL) {
903 snprintf(&ab3100->chip_name[0],
904 sizeof(ab3100->chip_name) - 1,
905 "AB3100 %s",
906 ids[i].name);
907 break;
908 } else {
909 dev_err(&client->dev,
910 "AB3000 is not supported\n");
911 goto exit_no_detect;
912 }
913 }
914 }
915
916 if (ids[i].id == 0x0) {
917 dev_err(&client->dev, "unknown analog baseband chip id: 0x%x\n",
918 ab3100->chip_id);
919 dev_err(&client->dev, "accepting it anyway. Please update "
920 "the driver.\n");
921 goto exit_no_detect;
922 }
923
924 dev_info(&client->dev, "Detected chip: %s\n",
925 &ab3100->chip_name[0]);
926
927 /* Attach a second dummy i2c_client to the test register address */
928 ab3100->testreg_client = i2c_new_dummy(client->adapter,
929 client->addr + 1);
930 if (!ab3100->testreg_client) {
931 err = -ENOMEM;
932 goto exit_no_testreg_client;
933 }
934
Linus Walleij14fa5692009-05-21 23:17:06 +0200935 err = ab3100_setup(ab3100);
936 if (err)
937 goto exit_no_setup;
938
Linus Walleij0df883d2010-01-08 10:44:16 +0100939 err = request_threaded_irq(client->irq, NULL, ab3100_irq_handler,
Linus Walleij2071db42010-01-19 11:41:52 +0100940 IRQF_ONESHOT, "ab3100-core", ab3100);
941 /* This real unpredictable IRQ is of course sampled for entropy */
942 rand_initialize_irq(client->irq);
943
Linus Walleij14fa5692009-05-21 23:17:06 +0200944 if (err)
945 goto exit_no_irq;
946
Mattias Wallinfa661252010-05-01 18:26:20 +0200947 err = abx500_register_ops(&client->dev, &ab3100_ops);
948 if (err)
949 goto exit_no_ops;
950
Linus Walleij8c96aef2010-08-20 10:27:12 +0200951 /* Set up and register the platform devices. */
Samuel Ortiza771e362011-04-06 00:41:43 +0200952 for (i = 0; i < ARRAY_SIZE(ab3100_devs); i++) {
953 ab3100_devs[i].platform_data = ab3100_plf_data;
954 ab3100_devs[i].pdata_size = sizeof(struct ab3100_platform_data);
955 }
Linus Walleij14fa5692009-05-21 23:17:06 +0200956
Linus Walleij8c96aef2010-08-20 10:27:12 +0200957 err = mfd_add_devices(&client->dev, 0, ab3100_devs,
958 ARRAY_SIZE(ab3100_devs), NULL, 0);
Linus Walleij14fa5692009-05-21 23:17:06 +0200959
960 ab3100_setup_debugfs(ab3100);
961
962 return 0;
963
Mattias Wallinfa661252010-05-01 18:26:20 +0200964 exit_no_ops:
Linus Walleij14fa5692009-05-21 23:17:06 +0200965 exit_no_irq:
966 exit_no_setup:
967 i2c_unregister_device(ab3100->testreg_client);
968 exit_no_testreg_client:
969 exit_no_detect:
970 kfree(ab3100);
971 return err;
972}
973
Linus Walleij25244682010-08-20 10:26:56 +0200974static int __devexit ab3100_remove(struct i2c_client *client)
Linus Walleij14fa5692009-05-21 23:17:06 +0200975{
976 struct ab3100 *ab3100 = i2c_get_clientdata(client);
Linus Walleij14fa5692009-05-21 23:17:06 +0200977
978 /* Unregister subdevices */
Linus Walleij8c96aef2010-08-20 10:27:12 +0200979 mfd_remove_devices(&client->dev);
Linus Walleij14fa5692009-05-21 23:17:06 +0200980
981 ab3100_remove_debugfs();
982 i2c_unregister_device(ab3100->testreg_client);
983
984 /*
985 * At this point, all subscribers should have unregistered
986 * their notifiers so deactivate IRQ
987 */
988 free_irq(client->irq, ab3100);
989 kfree(ab3100);
990 return 0;
991}
992
993static const struct i2c_device_id ab3100_id[] = {
Jean Delvaredcffa122009-10-04 22:53:44 +0200994 { "ab3100", 0 },
Linus Walleij14fa5692009-05-21 23:17:06 +0200995 { }
996};
997MODULE_DEVICE_TABLE(i2c, ab3100_id);
998
999static struct i2c_driver ab3100_driver = {
1000 .driver = {
1001 .name = "ab3100",
1002 .owner = THIS_MODULE,
1003 },
1004 .id_table = ab3100_id,
1005 .probe = ab3100_probe,
Linus Walleij25244682010-08-20 10:26:56 +02001006 .remove = __devexit_p(ab3100_remove),
Linus Walleij14fa5692009-05-21 23:17:06 +02001007};
1008
1009static int __init ab3100_i2c_init(void)
1010{
1011 return i2c_add_driver(&ab3100_driver);
1012}
1013
1014static void __exit ab3100_i2c_exit(void)
1015{
1016 i2c_del_driver(&ab3100_driver);
1017}
1018
1019subsys_initcall(ab3100_i2c_init);
1020module_exit(ab3100_i2c_exit);
1021
1022MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
1023MODULE_DESCRIPTION("AB3100 core driver");
1024MODULE_LICENSE("GPL");