blob: f0afb44271f8b8c4bf7c12e4df0a30152811ef6f [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>
Marcus Cooper0fd00132012-08-10 10:32:35 +020024#include <linux/mfd/ab3100.h>
Linus Walleij812f9e92010-05-01 18:26:07 +020025#include <linux/mfd/abx500.h>
Linus Walleij14fa5692009-05-21 23:17:06 +020026
27/* These are the only registers inside AB3100 used in this main file */
28
29/* Interrupt event registers */
30#define AB3100_EVENTA1 0x21
31#define AB3100_EVENTA2 0x22
32#define AB3100_EVENTA3 0x23
33
34/* AB3100 DAC converter registers */
35#define AB3100_DIS 0x00
36#define AB3100_D0C 0x01
37#define AB3100_D1C 0x02
38#define AB3100_D2C 0x03
39#define AB3100_D3C 0x04
40
41/* Chip ID register */
42#define AB3100_CID 0x20
43
44/* AB3100 interrupt registers */
45#define AB3100_IMRA1 0x24
46#define AB3100_IMRA2 0x25
47#define AB3100_IMRA3 0x26
48#define AB3100_IMRB1 0x2B
49#define AB3100_IMRB2 0x2C
50#define AB3100_IMRB3 0x2D
51
52/* System Power Monitoring and control registers */
53#define AB3100_MCA 0x2E
54#define AB3100_MCB 0x2F
55
56/* SIM power up */
57#define AB3100_SUP 0x50
58
59/*
60 * I2C communication
61 *
62 * The AB3100 is usually assigned address 0x48 (7-bit)
63 * The chip is defined in the platform i2c_board_data section.
64 */
Mattias Wallinfa661252010-05-01 18:26:20 +020065static int ab3100_get_chip_id(struct device *dev)
Linus Walleij14fa5692009-05-21 23:17:06 +020066{
Mattias Wallinfa661252010-05-01 18:26:20 +020067 struct ab3100 *ab3100 = dev_get_drvdata(dev->parent);
Linus Walleij14fa5692009-05-21 23:17:06 +020068
Mattias Wallinfa661252010-05-01 18:26:20 +020069 return (int)ab3100->chip_id;
Linus Walleij14fa5692009-05-21 23:17:06 +020070}
Linus Walleij14fa5692009-05-21 23:17:06 +020071
Mattias Wallinfa661252010-05-01 18:26:20 +020072static int ab3100_set_register_interruptible(struct ab3100 *ab3100,
73 u8 reg, u8 regval)
Linus Walleij14fa5692009-05-21 23:17:06 +020074{
75 u8 regandval[2] = {reg, regval};
76 int err;
77
78 err = mutex_lock_interruptible(&ab3100->access_mutex);
79 if (err)
80 return err;
81
82 /*
83 * A two-byte write message with the first byte containing the register
84 * number and the second byte containing the value to be written
85 * effectively sets a register in the AB3100.
86 */
87 err = i2c_master_send(ab3100->i2c_client, regandval, 2);
88 if (err < 0) {
89 dev_err(ab3100->dev,
90 "write error (write register): %d\n",
91 err);
92 } else if (err != 2) {
93 dev_err(ab3100->dev,
Lee Jonescd63a892014-05-08 13:47:15 +010094 "write error (write register)\n"
95 " %d bytes transferred (expected 2)\n",
Linus Walleij14fa5692009-05-21 23:17:06 +020096 err);
97 err = -EIO;
98 } else {
99 /* All is well */
100 err = 0;
101 }
102 mutex_unlock(&ab3100->access_mutex);
Linus Walleij7cdc2b92009-08-13 11:49:38 +0200103 return err;
Linus Walleij14fa5692009-05-21 23:17:06 +0200104}
Linus Walleij956f25a2009-08-13 11:49:23 +0200105
Mattias Wallinfa661252010-05-01 18:26:20 +0200106static int set_register_interruptible(struct device *dev,
107 u8 bank, u8 reg, u8 value)
108{
109 struct ab3100 *ab3100 = dev_get_drvdata(dev->parent);
110
111 return ab3100_set_register_interruptible(ab3100, reg, value);
112}
Linus Walleij14fa5692009-05-21 23:17:06 +0200113
114/*
115 * The test registers exist at an I2C bus address up one
116 * from the ordinary base. They are not supposed to be used
117 * in production code, but sometimes you have to do that
118 * anyway. It's currently only used from this file so declare
119 * it static and do not export.
120 */
Linus Walleij956f25a2009-08-13 11:49:23 +0200121static int ab3100_set_test_register_interruptible(struct ab3100 *ab3100,
Linus Walleij14fa5692009-05-21 23:17:06 +0200122 u8 reg, u8 regval)
123{
124 u8 regandval[2] = {reg, regval};
125 int err;
126
127 err = mutex_lock_interruptible(&ab3100->access_mutex);
128 if (err)
129 return err;
130
131 err = i2c_master_send(ab3100->testreg_client, regandval, 2);
132 if (err < 0) {
133 dev_err(ab3100->dev,
134 "write error (write test register): %d\n",
135 err);
136 } else if (err != 2) {
137 dev_err(ab3100->dev,
Lee Jonescd63a892014-05-08 13:47:15 +0100138 "write error (write test register)\n"
139 " %d bytes transferred (expected 2)\n",
Linus Walleij14fa5692009-05-21 23:17:06 +0200140 err);
141 err = -EIO;
142 } else {
143 /* All is well */
144 err = 0;
145 }
146 mutex_unlock(&ab3100->access_mutex);
147
148 return err;
149}
150
Mattias Wallinfa661252010-05-01 18:26:20 +0200151static int ab3100_get_register_interruptible(struct ab3100 *ab3100,
Linus Walleij8c96aef2010-08-20 10:27:12 +0200152 u8 reg, u8 *regval)
Linus Walleij14fa5692009-05-21 23:17:06 +0200153{
154 int err;
155
156 err = mutex_lock_interruptible(&ab3100->access_mutex);
157 if (err)
158 return err;
159
160 /*
161 * AB3100 require an I2C "stop" command between each message, else
162 * it will not work. The only way of achieveing this with the
163 * message transport layer is to send the read and write messages
164 * separately.
165 */
166 err = i2c_master_send(ab3100->i2c_client, &reg, 1);
167 if (err < 0) {
168 dev_err(ab3100->dev,
169 "write error (send register address): %d\n",
170 err);
171 goto get_reg_out_unlock;
172 } else if (err != 1) {
173 dev_err(ab3100->dev,
Lee Jonescd63a892014-05-08 13:47:15 +0100174 "write error (send register address)\n"
175 " %d bytes transferred (expected 1)\n",
Linus Walleij14fa5692009-05-21 23:17:06 +0200176 err);
177 err = -EIO;
178 goto get_reg_out_unlock;
179 } else {
180 /* All is well */
181 err = 0;
182 }
183
184 err = i2c_master_recv(ab3100->i2c_client, regval, 1);
185 if (err < 0) {
186 dev_err(ab3100->dev,
187 "write error (read register): %d\n",
188 err);
189 goto get_reg_out_unlock;
190 } else if (err != 1) {
191 dev_err(ab3100->dev,
Lee Jonescd63a892014-05-08 13:47:15 +0100192 "write error (read register)\n"
193 " %d bytes transferred (expected 1)\n",
Linus Walleij14fa5692009-05-21 23:17:06 +0200194 err);
195 err = -EIO;
196 goto get_reg_out_unlock;
197 } else {
198 /* All is well */
199 err = 0;
200 }
201
202 get_reg_out_unlock:
203 mutex_unlock(&ab3100->access_mutex);
204 return err;
205}
Linus Walleij14fa5692009-05-21 23:17:06 +0200206
Mattias Wallinfa661252010-05-01 18:26:20 +0200207static int get_register_interruptible(struct device *dev, u8 bank, u8 reg,
Linus Walleij8c96aef2010-08-20 10:27:12 +0200208 u8 *value)
Mattias Wallinfa661252010-05-01 18:26:20 +0200209{
210 struct ab3100 *ab3100 = dev_get_drvdata(dev->parent);
Linus Walleij956f25a2009-08-13 11:49:23 +0200211
Mattias Wallinfa661252010-05-01 18:26:20 +0200212 return ab3100_get_register_interruptible(ab3100, reg, value);
213}
214
215static int ab3100_get_register_page_interruptible(struct ab3100 *ab3100,
Linus Walleij14fa5692009-05-21 23:17:06 +0200216 u8 first_reg, u8 *regvals, u8 numregs)
217{
218 int err;
219
220 if (ab3100->chip_id == 0xa0 ||
221 ab3100->chip_id == 0xa1)
222 /* These don't support paged reads */
223 return -EIO;
224
225 err = mutex_lock_interruptible(&ab3100->access_mutex);
226 if (err)
227 return err;
228
229 /*
230 * Paged read also require an I2C "stop" command.
231 */
232 err = i2c_master_send(ab3100->i2c_client, &first_reg, 1);
233 if (err < 0) {
234 dev_err(ab3100->dev,
235 "write error (send first register address): %d\n",
236 err);
237 goto get_reg_page_out_unlock;
238 } else if (err != 1) {
239 dev_err(ab3100->dev,
Lee Jonescd63a892014-05-08 13:47:15 +0100240 "write error (send first register address)\n"
241 " %d bytes transferred (expected 1)\n",
Linus Walleij14fa5692009-05-21 23:17:06 +0200242 err);
243 err = -EIO;
244 goto get_reg_page_out_unlock;
245 }
246
247 err = i2c_master_recv(ab3100->i2c_client, regvals, numregs);
248 if (err < 0) {
249 dev_err(ab3100->dev,
250 "write error (read register page): %d\n",
251 err);
252 goto get_reg_page_out_unlock;
253 } else if (err != numregs) {
254 dev_err(ab3100->dev,
Lee Jonescd63a892014-05-08 13:47:15 +0100255 "write error (read register page)\n"
256 " %d bytes transferred (expected %d)\n",
Linus Walleij14fa5692009-05-21 23:17:06 +0200257 err, numregs);
258 err = -EIO;
259 goto get_reg_page_out_unlock;
260 }
261
262 /* All is well */
263 err = 0;
264
265 get_reg_page_out_unlock:
266 mutex_unlock(&ab3100->access_mutex);
267 return err;
268}
Linus Walleij14fa5692009-05-21 23:17:06 +0200269
Mattias Wallinfa661252010-05-01 18:26:20 +0200270static int get_register_page_interruptible(struct device *dev, u8 bank,
271 u8 first_reg, u8 *regvals, u8 numregs)
272{
273 struct ab3100 *ab3100 = dev_get_drvdata(dev->parent);
Linus Walleij956f25a2009-08-13 11:49:23 +0200274
Mattias Wallinfa661252010-05-01 18:26:20 +0200275 return ab3100_get_register_page_interruptible(ab3100,
276 first_reg, regvals, numregs);
277}
278
279static int ab3100_mask_and_set_register_interruptible(struct ab3100 *ab3100,
Linus Walleij14fa5692009-05-21 23:17:06 +0200280 u8 reg, u8 andmask, u8 ormask)
281{
282 u8 regandval[2] = {reg, 0};
283 int err;
284
285 err = mutex_lock_interruptible(&ab3100->access_mutex);
286 if (err)
287 return err;
288
289 /* First read out the target register */
290 err = i2c_master_send(ab3100->i2c_client, &reg, 1);
291 if (err < 0) {
292 dev_err(ab3100->dev,
293 "write error (maskset send address): %d\n",
294 err);
295 goto get_maskset_unlock;
296 } else if (err != 1) {
297 dev_err(ab3100->dev,
Lee Jonescd63a892014-05-08 13:47:15 +0100298 "write error (maskset send address)\n"
299 " %d bytes transferred (expected 1)\n",
Linus Walleij14fa5692009-05-21 23:17:06 +0200300 err);
301 err = -EIO;
302 goto get_maskset_unlock;
303 }
304
305 err = i2c_master_recv(ab3100->i2c_client, &regandval[1], 1);
306 if (err < 0) {
307 dev_err(ab3100->dev,
308 "write error (maskset read register): %d\n",
309 err);
310 goto get_maskset_unlock;
311 } else if (err != 1) {
312 dev_err(ab3100->dev,
Lee Jonescd63a892014-05-08 13:47:15 +0100313 "write error (maskset read register)\n"
314 " %d bytes transferred (expected 1)\n",
Linus Walleij14fa5692009-05-21 23:17:06 +0200315 err);
316 err = -EIO;
317 goto get_maskset_unlock;
318 }
319
320 /* Modify the register */
321 regandval[1] &= andmask;
322 regandval[1] |= ormask;
323
324 /* Write the register */
325 err = i2c_master_send(ab3100->i2c_client, regandval, 2);
326 if (err < 0) {
327 dev_err(ab3100->dev,
328 "write error (write register): %d\n",
329 err);
330 goto get_maskset_unlock;
331 } else if (err != 2) {
332 dev_err(ab3100->dev,
Lee Jonescd63a892014-05-08 13:47:15 +0100333 "write error (write register)\n"
334 " %d bytes transferred (expected 2)\n",
Linus Walleij14fa5692009-05-21 23:17:06 +0200335 err);
336 err = -EIO;
337 goto get_maskset_unlock;
338 }
339
340 /* All is well */
341 err = 0;
342
343 get_maskset_unlock:
344 mutex_unlock(&ab3100->access_mutex);
345 return err;
346}
Linus Walleij956f25a2009-08-13 11:49:23 +0200347
Mattias Wallinfa661252010-05-01 18:26:20 +0200348static int mask_and_set_register_interruptible(struct device *dev, u8 bank,
349 u8 reg, u8 bitmask, u8 bitvalues)
350{
351 struct ab3100 *ab3100 = dev_get_drvdata(dev->parent);
352
353 return ab3100_mask_and_set_register_interruptible(ab3100,
354 reg, bitmask, (bitmask & bitvalues));
355}
Linus Walleij14fa5692009-05-21 23:17:06 +0200356
357/*
358 * Register a simple callback for handling any AB3100 events.
359 */
360int ab3100_event_register(struct ab3100 *ab3100,
361 struct notifier_block *nb)
362{
363 return blocking_notifier_chain_register(&ab3100->event_subscribers,
364 nb);
365}
366EXPORT_SYMBOL(ab3100_event_register);
367
368/*
369 * Remove a previously registered callback.
370 */
371int ab3100_event_unregister(struct ab3100 *ab3100,
372 struct notifier_block *nb)
373{
Lee Jonescd63a892014-05-08 13:47:15 +0100374 return blocking_notifier_chain_unregister(&ab3100->event_subscribers,
Linus Walleij14fa5692009-05-21 23:17:06 +0200375 nb);
376}
377EXPORT_SYMBOL(ab3100_event_unregister);
378
379
Mattias Wallinfa661252010-05-01 18:26:20 +0200380static int ab3100_event_registers_startup_state_get(struct device *dev,
381 u8 *event)
Linus Walleij14fa5692009-05-21 23:17:06 +0200382{
Mattias Wallinfa661252010-05-01 18:26:20 +0200383 struct ab3100 *ab3100 = dev_get_drvdata(dev->parent);
Linus Walleij14fa5692009-05-21 23:17:06 +0200384 if (!ab3100->startup_events_read)
385 return -EAGAIN; /* Try again later */
Mattias Wallinfa661252010-05-01 18:26:20 +0200386 memcpy(event, ab3100->startup_events, 3);
Linus Walleij14fa5692009-05-21 23:17:06 +0200387 return 0;
388}
Mattias Wallinfa661252010-05-01 18:26:20 +0200389
390static struct abx500_ops ab3100_ops = {
391 .get_chip_id = ab3100_get_chip_id,
392 .set_register = set_register_interruptible,
393 .get_register = get_register_interruptible,
394 .get_register_page = get_register_page_interruptible,
395 .set_register_page = NULL,
396 .mask_and_set_register = mask_and_set_register_interruptible,
397 .event_registers_startup_state_get =
398 ab3100_event_registers_startup_state_get,
399 .startup_irq_enabled = NULL,
400};
Linus Walleij14fa5692009-05-21 23:17:06 +0200401
Linus Walleij0df883d2010-01-08 10:44:16 +0100402/*
403 * This is a threaded interrupt handler so we can make some
404 * I2C calls etc.
405 */
406static irqreturn_t ab3100_irq_handler(int irq, void *data)
Linus Walleij14fa5692009-05-21 23:17:06 +0200407{
Linus Walleij0df883d2010-01-08 10:44:16 +0100408 struct ab3100 *ab3100 = data;
Linus Walleij14fa5692009-05-21 23:17:06 +0200409 u8 event_regs[3];
410 u32 fatevent;
411 int err;
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
Lee Jonescd63a892014-05-08 13:47:15 +0100458 seq_puts(s, "AB3100 registers:\n");
Linus Walleij14fa5692009-05-21 23:17:06 +0200459
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
Samuel Ortizdfc3aa72009-06-23 10:48:36 +0200485static ssize_t ab3100_get_set_reg(struct file *file,
486 const char __user *user_buf,
487 size_t count, loff_t *ppos)
Linus Walleij14fa5692009-05-21 23:17:06 +0200488{
489 struct ab3100_get_set_reg_priv *priv = file->private_data;
490 struct ab3100 *ab3100 = priv->ab3100;
491 char buf[32];
Samuel Ortizdfc3aa72009-06-23 10:48:36 +0200492 ssize_t buf_size;
Linus Walleij14fa5692009-05-21 23:17:06 +0200493 int regp;
Jingoo Han8420a242013-06-04 13:11:50 +0900494 u8 user_reg;
Linus Walleij14fa5692009-05-21 23:17:06 +0200495 int err;
496 int i = 0;
497
498 /* Get userspace string and assure termination */
499 buf_size = min(count, (sizeof(buf)-1));
500 if (copy_from_user(buf, user_buf, buf_size))
501 return -EFAULT;
502 buf[buf_size] = 0;
503
504 /*
505 * The idea is here to parse a string which is either
506 * "0xnn" for reading a register, or "0xaa 0xbb" for
507 * writing 0xbb to the register 0xaa. First move past
508 * whitespace and then begin to parse the register.
509 */
510 while ((i < buf_size) && (buf[i] == ' '))
511 i++;
512 regp = i;
513
514 /*
515 * Advance pointer to end of string then terminate
516 * the register string. This is needed to satisfy
Jingoo Han8420a242013-06-04 13:11:50 +0900517 * the kstrtou8() function.
Linus Walleij14fa5692009-05-21 23:17:06 +0200518 */
519 while ((i < buf_size) && (buf[i] != ' '))
520 i++;
521 buf[i] = '\0';
522
Jingoo Han8420a242013-06-04 13:11:50 +0900523 err = kstrtou8(&buf[regp], 16, &user_reg);
Linus Walleij14fa5692009-05-21 23:17:06 +0200524 if (err)
525 return err;
Linus Walleij14fa5692009-05-21 23:17:06 +0200526
527 /* Either we read or we write a register here */
528 if (!priv->mode) {
529 /* Reading */
Linus Walleij14fa5692009-05-21 23:17:06 +0200530 u8 regvalue;
531
Jingoo Han8420a242013-06-04 13:11:50 +0900532 ab3100_get_register_interruptible(ab3100, user_reg, &regvalue);
Linus Walleij14fa5692009-05-21 23:17:06 +0200533
534 dev_info(ab3100->dev,
535 "debug read AB3100 reg[0x%02x]: 0x%02x\n",
Jingoo Han8420a242013-06-04 13:11:50 +0900536 user_reg, regvalue);
Linus Walleij14fa5692009-05-21 23:17:06 +0200537 } else {
538 int valp;
Jingoo Han8420a242013-06-04 13:11:50 +0900539 u8 user_value;
Linus Walleij14fa5692009-05-21 23:17:06 +0200540 u8 regvalue;
541
542 /*
543 * Writing, we need some value to write to
544 * the register so keep parsing the string
545 * from userspace.
546 */
547 i++;
548 while ((i < buf_size) && (buf[i] == ' '))
549 i++;
550 valp = i;
551 while ((i < buf_size) && (buf[i] != ' '))
552 i++;
553 buf[i] = '\0';
554
Jingoo Han8420a242013-06-04 13:11:50 +0900555 err = kstrtou8(&buf[valp], 16, &user_value);
Linus Walleij14fa5692009-05-21 23:17:06 +0200556 if (err)
557 return err;
Linus Walleij14fa5692009-05-21 23:17:06 +0200558
Jingoo Han8420a242013-06-04 13:11:50 +0900559 ab3100_set_register_interruptible(ab3100, user_reg, user_value);
560 ab3100_get_register_interruptible(ab3100, user_reg, &regvalue);
Linus Walleij14fa5692009-05-21 23:17:06 +0200561
562 dev_info(ab3100->dev,
Lee Jonescd63a892014-05-08 13:47:15 +0100563 "debug write reg[0x%02x]\n"
564 " with 0x%02x, after readback: 0x%02x\n",
Jingoo Han8420a242013-06-04 13:11:50 +0900565 user_reg, user_value, regvalue);
Linus Walleij14fa5692009-05-21 23:17:06 +0200566 }
567 return buf_size;
568}
569
570static const struct file_operations ab3100_get_set_reg_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700571 .open = simple_open,
Linus Walleij14fa5692009-05-21 23:17:06 +0200572 .write = ab3100_get_set_reg,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200573 .llseek = noop_llseek,
Linus Walleij14fa5692009-05-21 23:17:06 +0200574};
575
576static struct dentry *ab3100_dir;
577static struct dentry *ab3100_reg_file;
578static struct ab3100_get_set_reg_priv ab3100_get_priv;
579static struct dentry *ab3100_get_reg_file;
580static struct ab3100_get_set_reg_priv ab3100_set_priv;
581static struct dentry *ab3100_set_reg_file;
582
583static void ab3100_setup_debugfs(struct ab3100 *ab3100)
584{
585 int err;
586
587 ab3100_dir = debugfs_create_dir("ab3100", NULL);
588 if (!ab3100_dir)
589 goto exit_no_debugfs;
590
591 ab3100_reg_file = debugfs_create_file("registers",
592 S_IRUGO, ab3100_dir, ab3100,
593 &ab3100_registers_fops);
594 if (!ab3100_reg_file) {
595 err = -ENOMEM;
596 goto exit_destroy_dir;
597 }
598
599 ab3100_get_priv.ab3100 = ab3100;
600 ab3100_get_priv.mode = false;
601 ab3100_get_reg_file = debugfs_create_file("get_reg",
Vasiliy Kulikovf8a06972011-02-04 15:23:36 +0300602 S_IWUSR, ab3100_dir, &ab3100_get_priv,
Linus Walleij14fa5692009-05-21 23:17:06 +0200603 &ab3100_get_set_reg_fops);
604 if (!ab3100_get_reg_file) {
605 err = -ENOMEM;
606 goto exit_destroy_reg;
607 }
608
609 ab3100_set_priv.ab3100 = ab3100;
610 ab3100_set_priv.mode = true;
611 ab3100_set_reg_file = debugfs_create_file("set_reg",
Vasiliy Kulikovf8a06972011-02-04 15:23:36 +0300612 S_IWUSR, ab3100_dir, &ab3100_set_priv,
Linus Walleij14fa5692009-05-21 23:17:06 +0200613 &ab3100_get_set_reg_fops);
614 if (!ab3100_set_reg_file) {
615 err = -ENOMEM;
616 goto exit_destroy_get_reg;
617 }
618 return;
619
620 exit_destroy_get_reg:
621 debugfs_remove(ab3100_get_reg_file);
622 exit_destroy_reg:
623 debugfs_remove(ab3100_reg_file);
624 exit_destroy_dir:
625 debugfs_remove(ab3100_dir);
626 exit_no_debugfs:
627 return;
628}
629static inline void ab3100_remove_debugfs(void)
630{
631 debugfs_remove(ab3100_set_reg_file);
632 debugfs_remove(ab3100_get_reg_file);
633 debugfs_remove(ab3100_reg_file);
634 debugfs_remove(ab3100_dir);
635}
636#else
637static inline void ab3100_setup_debugfs(struct ab3100 *ab3100)
638{
639}
640static inline void ab3100_remove_debugfs(void)
641{
642}
643#endif
644
645/*
646 * Basic set-up, datastructure creation/destruction and I2C interface.
647 * This sets up a default config in the AB3100 chip so that it
648 * will work as expected.
649 */
650
651struct ab3100_init_setting {
652 u8 abreg;
653 u8 setting;
654};
655
Bill Pembertona73e5df2012-11-19 13:25:09 -0500656static const struct ab3100_init_setting ab3100_init_settings[] = {
Linus Walleij14fa5692009-05-21 23:17:06 +0200657 {
658 .abreg = AB3100_MCA,
659 .setting = 0x01
660 }, {
661 .abreg = AB3100_MCB,
662 .setting = 0x30
663 }, {
664 .abreg = AB3100_IMRA1,
665 .setting = 0x00
666 }, {
667 .abreg = AB3100_IMRA2,
668 .setting = 0xFF
669 }, {
670 .abreg = AB3100_IMRA3,
671 .setting = 0x01
672 }, {
673 .abreg = AB3100_IMRB1,
Linus Walleijce290b02009-08-13 11:49:49 +0200674 .setting = 0xBF
Linus Walleij14fa5692009-05-21 23:17:06 +0200675 }, {
676 .abreg = AB3100_IMRB2,
677 .setting = 0xFF
678 }, {
679 .abreg = AB3100_IMRB3,
680 .setting = 0xFF
681 }, {
682 .abreg = AB3100_SUP,
683 .setting = 0x00
684 }, {
685 .abreg = AB3100_DIS,
686 .setting = 0xF0
687 }, {
688 .abreg = AB3100_D0C,
689 .setting = 0x00
690 }, {
691 .abreg = AB3100_D1C,
692 .setting = 0x00
693 }, {
694 .abreg = AB3100_D2C,
695 .setting = 0x00
696 }, {
697 .abreg = AB3100_D3C,
698 .setting = 0x00
699 },
700};
701
Bill Pembertonf791be42012-11-19 13:23:04 -0500702static int ab3100_setup(struct ab3100 *ab3100)
Linus Walleij14fa5692009-05-21 23:17:06 +0200703{
704 int err = 0;
705 int i;
706
707 for (i = 0; i < ARRAY_SIZE(ab3100_init_settings); i++) {
Linus Walleij956f25a2009-08-13 11:49:23 +0200708 err = ab3100_set_register_interruptible(ab3100,
Linus Walleij14fa5692009-05-21 23:17:06 +0200709 ab3100_init_settings[i].abreg,
710 ab3100_init_settings[i].setting);
711 if (err)
712 goto exit_no_setup;
713 }
714
715 /*
716 * Special trick to make the AB3100 use the 32kHz clock (RTC)
Linus Walleij956f25a2009-08-13 11:49:23 +0200717 * bit 3 in test register 0x02 is a special, undocumented test
Linus Walleij14fa5692009-05-21 23:17:06 +0200718 * register bit that only exist in AB3100 P1E
719 */
720 if (ab3100->chip_id == 0xc4) {
721 dev_warn(ab3100->dev,
Lee Jonescd63a892014-05-08 13:47:15 +0100722 "AB3100 P1E variant detected forcing chip to 32KHz\n");
Mattias Wallinfa661252010-05-01 18:26:20 +0200723 err = ab3100_set_test_register_interruptible(ab3100,
724 0x02, 0x08);
Linus Walleij14fa5692009-05-21 23:17:06 +0200725 }
726
727 exit_no_setup:
728 return err;
729}
730
Linus Walleij8c96aef2010-08-20 10:27:12 +0200731/* The subdevices of the AB3100 */
732static struct mfd_cell ab3100_devs[] = {
733 {
734 .name = "ab3100-dac",
735 .id = -1,
736 },
737 {
738 .name = "ab3100-leds",
739 .id = -1,
740 },
741 {
742 .name = "ab3100-power",
743 .id = -1,
744 },
745 {
746 .name = "ab3100-regulators",
Linus Walleij8735bc22013-04-22 11:57:25 +0200747 .of_compatible = "stericsson,ab3100-regulators",
Linus Walleij8c96aef2010-08-20 10:27:12 +0200748 .id = -1,
749 },
750 {
751 .name = "ab3100-sim",
752 .id = -1,
753 },
754 {
755 .name = "ab3100-uart",
756 .id = -1,
757 },
758 {
759 .name = "ab3100-rtc",
760 .id = -1,
761 },
762 {
763 .name = "ab3100-charger",
764 .id = -1,
765 },
766 {
767 .name = "ab3100-boost",
768 .id = -1,
769 },
770 {
771 .name = "ab3100-adc",
772 .id = -1,
773 },
774 {
775 .name = "ab3100-fuelgauge",
776 .id = -1,
777 },
778 {
779 .name = "ab3100-vibrator",
780 .id = -1,
781 },
782 {
783 .name = "ab3100-otp",
784 .id = -1,
785 },
786 {
787 .name = "ab3100-codec",
788 .id = -1,
789 },
Linus Walleij14fa5692009-05-21 23:17:06 +0200790};
791
792struct ab_family_id {
793 u8 id;
794 char *name;
795};
796
Bill Pembertona73e5df2012-11-19 13:25:09 -0500797static const struct ab_family_id ids[] = {
Linus Walleij14fa5692009-05-21 23:17:06 +0200798 /* AB3100 */
799 {
800 .id = 0xc0,
801 .name = "P1A"
802 }, {
803 .id = 0xc1,
804 .name = "P1B"
805 }, {
806 .id = 0xc2,
807 .name = "P1C"
808 }, {
809 .id = 0xc3,
810 .name = "P1D"
811 }, {
812 .id = 0xc4,
813 .name = "P1E"
814 }, {
815 .id = 0xc5,
816 .name = "P1F/R1A"
817 }, {
818 .id = 0xc6,
819 .name = "P1G/R1A"
820 }, {
821 .id = 0xc7,
822 .name = "P2A/R2A"
823 }, {
824 .id = 0xc8,
825 .name = "P2B/R2B"
826 },
827 /* AB3000 variants, not supported */
828 {
829 .id = 0xa0
830 }, {
831 .id = 0xa1
832 }, {
833 .id = 0xa2
834 }, {
835 .id = 0xa3
836 }, {
837 .id = 0xa4
838 }, {
839 .id = 0xa5
840 }, {
841 .id = 0xa6
842 }, {
843 .id = 0xa7
844 },
845 /* Terminator */
846 {
847 .id = 0x00,
848 },
849};
850
Bill Pembertonf791be42012-11-19 13:23:04 -0500851static int ab3100_probe(struct i2c_client *client,
Linus Walleij25244682010-08-20 10:26:56 +0200852 const struct i2c_device_id *id)
Linus Walleij14fa5692009-05-21 23:17:06 +0200853{
854 struct ab3100 *ab3100;
Linus Walleijd619bc12009-09-09 11:31:00 +0200855 struct ab3100_platform_data *ab3100_plf_data =
Jingoo Han334a41c2013-07-30 17:10:05 +0900856 dev_get_platdata(&client->dev);
Linus Walleij14fa5692009-05-21 23:17:06 +0200857 int err;
858 int i;
859
Linus Walleij7f0f07c2012-06-12 20:26:58 +0200860 ab3100 = devm_kzalloc(&client->dev, sizeof(struct ab3100), GFP_KERNEL);
Linus Walleij14fa5692009-05-21 23:17:06 +0200861 if (!ab3100) {
862 dev_err(&client->dev, "could not allocate AB3100 device\n");
863 return -ENOMEM;
864 }
865
866 /* Initialize data structure */
867 mutex_init(&ab3100->access_mutex);
868 BLOCKING_INIT_NOTIFIER_HEAD(&ab3100->event_subscribers);
869
870 ab3100->i2c_client = client;
871 ab3100->dev = &ab3100->i2c_client->dev;
872
873 i2c_set_clientdata(client, ab3100);
874
875 /* Read chip ID register */
Linus Walleij956f25a2009-08-13 11:49:23 +0200876 err = ab3100_get_register_interruptible(ab3100, AB3100_CID,
877 &ab3100->chip_id);
Linus Walleij14fa5692009-05-21 23:17:06 +0200878 if (err) {
879 dev_err(&client->dev,
Lee Jonescd63a892014-05-08 13:47:15 +0100880 "failed to communicate with AB3100 chip\n");
Linus Walleij14fa5692009-05-21 23:17:06 +0200881 goto exit_no_detect;
882 }
883
884 for (i = 0; ids[i].id != 0x0; i++) {
885 if (ids[i].id == ab3100->chip_id) {
886 if (ids[i].name != NULL) {
887 snprintf(&ab3100->chip_name[0],
888 sizeof(ab3100->chip_name) - 1,
889 "AB3100 %s",
890 ids[i].name);
891 break;
892 } else {
893 dev_err(&client->dev,
894 "AB3000 is not supported\n");
895 goto exit_no_detect;
896 }
897 }
898 }
899
900 if (ids[i].id == 0x0) {
901 dev_err(&client->dev, "unknown analog baseband chip id: 0x%x\n",
902 ab3100->chip_id);
Lee Jonescd63a892014-05-08 13:47:15 +0100903 dev_err(&client->dev,
904 "accepting it anyway. Please update the driver.\n");
Linus Walleij14fa5692009-05-21 23:17:06 +0200905 goto exit_no_detect;
906 }
907
908 dev_info(&client->dev, "Detected chip: %s\n",
909 &ab3100->chip_name[0]);
910
911 /* Attach a second dummy i2c_client to the test register address */
912 ab3100->testreg_client = i2c_new_dummy(client->adapter,
Linus Walleij7f0f07c2012-06-12 20:26:58 +0200913 client->addr + 1);
Linus Walleij14fa5692009-05-21 23:17:06 +0200914 if (!ab3100->testreg_client) {
915 err = -ENOMEM;
916 goto exit_no_testreg_client;
917 }
918
Linus Walleij14fa5692009-05-21 23:17:06 +0200919 err = ab3100_setup(ab3100);
920 if (err)
921 goto exit_no_setup;
922
Linus Walleij7f0f07c2012-06-12 20:26:58 +0200923 err = devm_request_threaded_irq(&client->dev,
924 client->irq, NULL, ab3100_irq_handler,
925 IRQF_ONESHOT, "ab3100-core", ab3100);
Linus Walleij14fa5692009-05-21 23:17:06 +0200926 if (err)
927 goto exit_no_irq;
928
Mattias Wallinfa661252010-05-01 18:26:20 +0200929 err = abx500_register_ops(&client->dev, &ab3100_ops);
930 if (err)
931 goto exit_no_ops;
932
Linus Walleij8c96aef2010-08-20 10:27:12 +0200933 /* Set up and register the platform devices. */
Samuel Ortiza771e362011-04-06 00:41:43 +0200934 for (i = 0; i < ARRAY_SIZE(ab3100_devs); i++) {
935 ab3100_devs[i].platform_data = ab3100_plf_data;
936 ab3100_devs[i].pdata_size = sizeof(struct ab3100_platform_data);
937 }
Linus Walleij14fa5692009-05-21 23:17:06 +0200938
Linus Walleij8c96aef2010-08-20 10:27:12 +0200939 err = mfd_add_devices(&client->dev, 0, ab3100_devs,
Mark Brown55692af2012-09-11 15:16:36 +0800940 ARRAY_SIZE(ab3100_devs), NULL, 0, NULL);
Linus Walleij14fa5692009-05-21 23:17:06 +0200941
942 ab3100_setup_debugfs(ab3100);
943
944 return 0;
945
Mattias Wallinfa661252010-05-01 18:26:20 +0200946 exit_no_ops:
Linus Walleij14fa5692009-05-21 23:17:06 +0200947 exit_no_irq:
948 exit_no_setup:
949 i2c_unregister_device(ab3100->testreg_client);
950 exit_no_testreg_client:
951 exit_no_detect:
Linus Walleij14fa5692009-05-21 23:17:06 +0200952 return err;
953}
954
Bill Pemberton4740f732012-11-19 13:26:01 -0500955static int ab3100_remove(struct i2c_client *client)
Linus Walleij14fa5692009-05-21 23:17:06 +0200956{
957 struct ab3100 *ab3100 = i2c_get_clientdata(client);
Linus Walleij14fa5692009-05-21 23:17:06 +0200958
959 /* Unregister subdevices */
Linus Walleij8c96aef2010-08-20 10:27:12 +0200960 mfd_remove_devices(&client->dev);
Linus Walleij14fa5692009-05-21 23:17:06 +0200961 ab3100_remove_debugfs();
962 i2c_unregister_device(ab3100->testreg_client);
Linus Walleij14fa5692009-05-21 23:17:06 +0200963 return 0;
964}
965
966static const struct i2c_device_id ab3100_id[] = {
Jean Delvaredcffa122009-10-04 22:53:44 +0200967 { "ab3100", 0 },
Linus Walleij14fa5692009-05-21 23:17:06 +0200968 { }
969};
970MODULE_DEVICE_TABLE(i2c, ab3100_id);
971
972static struct i2c_driver ab3100_driver = {
973 .driver = {
974 .name = "ab3100",
Linus Walleij14fa5692009-05-21 23:17:06 +0200975 },
976 .id_table = ab3100_id,
977 .probe = ab3100_probe,
Bill Pemberton84449212012-11-19 13:20:24 -0500978 .remove = ab3100_remove,
Linus Walleij14fa5692009-05-21 23:17:06 +0200979};
980
981static int __init ab3100_i2c_init(void)
982{
983 return i2c_add_driver(&ab3100_driver);
984}
985
986static void __exit ab3100_i2c_exit(void)
987{
988 i2c_del_driver(&ab3100_driver);
989}
990
991subsys_initcall(ab3100_i2c_init);
992module_exit(ab3100_i2c_exit);
993
994MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
995MODULE_DESCRIPTION("AB3100 core driver");
996MODULE_LICENSE("GPL");