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