Commit | Line | Data |
---|---|---|
14fa5691 | 1 | /* |
2071db48 | 2 | * Copyright (C) 2007-2010 ST-Ericsson |
14fa5691 LW |
3 | * License terms: GNU General Public License (GPL) version 2 |
4 | * Low-level core for exclusive access to the AB3100 IC on the I2C bus | |
5 | * and some basic chip-configuration. | |
6 | * Author: Linus Walleij <linus.walleij@stericsson.com> | |
7 | */ | |
8 | ||
9 | #include <linux/i2c.h> | |
10 | #include <linux/mutex.h> | |
11 | #include <linux/list.h> | |
12 | #include <linux/notifier.h> | |
5a0e3ad6 | 13 | #include <linux/slab.h> |
14fa5691 LW |
14 | #include <linux/err.h> |
15 | #include <linux/platform_device.h> | |
16 | #include <linux/device.h> | |
17 | #include <linux/interrupt.h> | |
2071db48 | 18 | #include <linux/random.h> |
14fa5691 LW |
19 | #include <linux/debugfs.h> |
20 | #include <linux/seq_file.h> | |
21 | #include <linux/uaccess.h> | |
22 | #include <linux/mfd/ab3100.h> | |
23 | ||
24 | /* These are the only registers inside AB3100 used in this main file */ | |
25 | ||
26 | /* Interrupt event registers */ | |
27 | #define AB3100_EVENTA1 0x21 | |
28 | #define AB3100_EVENTA2 0x22 | |
29 | #define AB3100_EVENTA3 0x23 | |
30 | ||
31 | /* AB3100 DAC converter registers */ | |
32 | #define AB3100_DIS 0x00 | |
33 | #define AB3100_D0C 0x01 | |
34 | #define AB3100_D1C 0x02 | |
35 | #define AB3100_D2C 0x03 | |
36 | #define AB3100_D3C 0x04 | |
37 | ||
38 | /* Chip ID register */ | |
39 | #define AB3100_CID 0x20 | |
40 | ||
41 | /* AB3100 interrupt registers */ | |
42 | #define AB3100_IMRA1 0x24 | |
43 | #define AB3100_IMRA2 0x25 | |
44 | #define AB3100_IMRA3 0x26 | |
45 | #define AB3100_IMRB1 0x2B | |
46 | #define AB3100_IMRB2 0x2C | |
47 | #define AB3100_IMRB3 0x2D | |
48 | ||
49 | /* System Power Monitoring and control registers */ | |
50 | #define AB3100_MCA 0x2E | |
51 | #define AB3100_MCB 0x2F | |
52 | ||
53 | /* SIM power up */ | |
54 | #define AB3100_SUP 0x50 | |
55 | ||
56 | /* | |
57 | * I2C communication | |
58 | * | |
59 | * The AB3100 is usually assigned address 0x48 (7-bit) | |
60 | * The chip is defined in the platform i2c_board_data section. | |
61 | */ | |
14fa5691 LW |
62 | |
63 | u8 ab3100_get_chip_type(struct ab3100 *ab3100) | |
64 | { | |
65 | u8 chip = ABUNKNOWN; | |
66 | ||
67 | switch (ab3100->chip_id & 0xf0) { | |
68 | case 0xa0: | |
69 | chip = AB3000; | |
70 | break; | |
71 | case 0xc0: | |
72 | chip = AB3100; | |
73 | break; | |
74 | } | |
75 | return chip; | |
76 | } | |
77 | EXPORT_SYMBOL(ab3100_get_chip_type); | |
78 | ||
956f25a6 | 79 | int ab3100_set_register_interruptible(struct ab3100 *ab3100, u8 reg, u8 regval) |
14fa5691 LW |
80 | { |
81 | u8 regandval[2] = {reg, regval}; | |
82 | int err; | |
83 | ||
84 | err = mutex_lock_interruptible(&ab3100->access_mutex); | |
85 | if (err) | |
86 | return err; | |
87 | ||
88 | /* | |
89 | * A two-byte write message with the first byte containing the register | |
90 | * number and the second byte containing the value to be written | |
91 | * effectively sets a register in the AB3100. | |
92 | */ | |
93 | err = i2c_master_send(ab3100->i2c_client, regandval, 2); | |
94 | if (err < 0) { | |
95 | dev_err(ab3100->dev, | |
96 | "write error (write register): %d\n", | |
97 | err); | |
98 | } else if (err != 2) { | |
99 | dev_err(ab3100->dev, | |
100 | "write error (write register) " | |
101 | "%d bytes transferred (expected 2)\n", | |
102 | err); | |
103 | err = -EIO; | |
104 | } else { | |
105 | /* All is well */ | |
106 | err = 0; | |
107 | } | |
108 | mutex_unlock(&ab3100->access_mutex); | |
7cdc2b98 | 109 | return err; |
14fa5691 | 110 | } |
956f25a6 LW |
111 | EXPORT_SYMBOL(ab3100_set_register_interruptible); |
112 | ||
14fa5691 LW |
113 | |
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 | */ | |
956f25a6 | 121 | static int ab3100_set_test_register_interruptible(struct ab3100 *ab3100, |
14fa5691 LW |
122 | 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, | |
138 | "write error (write test register) " | |
139 | "%d bytes transferred (expected 2)\n", | |
140 | 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 | ||
956f25a6 LW |
151 | |
152 | int ab3100_get_register_interruptible(struct ab3100 *ab3100, u8 reg, u8 *regval) | |
14fa5691 LW |
153 | { |
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, ®, 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, | |
174 | "write error (send register address) " | |
175 | "%d bytes transferred (expected 1)\n", | |
176 | 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, | |
192 | "write error (read register) " | |
193 | "%d bytes transferred (expected 1)\n", | |
194 | 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 | } | |
956f25a6 LW |
206 | EXPORT_SYMBOL(ab3100_get_register_interruptible); |
207 | ||
14fa5691 | 208 | |
956f25a6 | 209 | int ab3100_get_register_page_interruptible(struct ab3100 *ab3100, |
14fa5691 LW |
210 | u8 first_reg, u8 *regvals, u8 numregs) |
211 | { | |
212 | int err; | |
213 | ||
214 | if (ab3100->chip_id == 0xa0 || | |
215 | ab3100->chip_id == 0xa1) | |
216 | /* These don't support paged reads */ | |
217 | return -EIO; | |
218 | ||
219 | err = mutex_lock_interruptible(&ab3100->access_mutex); | |
220 | if (err) | |
221 | return err; | |
222 | ||
223 | /* | |
224 | * Paged read also require an I2C "stop" command. | |
225 | */ | |
226 | err = i2c_master_send(ab3100->i2c_client, &first_reg, 1); | |
227 | if (err < 0) { | |
228 | dev_err(ab3100->dev, | |
229 | "write error (send first register address): %d\n", | |
230 | err); | |
231 | goto get_reg_page_out_unlock; | |
232 | } else if (err != 1) { | |
233 | dev_err(ab3100->dev, | |
234 | "write error (send first register address) " | |
235 | "%d bytes transferred (expected 1)\n", | |
236 | err); | |
237 | err = -EIO; | |
238 | goto get_reg_page_out_unlock; | |
239 | } | |
240 | ||
241 | err = i2c_master_recv(ab3100->i2c_client, regvals, numregs); | |
242 | if (err < 0) { | |
243 | dev_err(ab3100->dev, | |
244 | "write error (read register page): %d\n", | |
245 | err); | |
246 | goto get_reg_page_out_unlock; | |
247 | } else if (err != numregs) { | |
248 | dev_err(ab3100->dev, | |
249 | "write error (read register page) " | |
250 | "%d bytes transferred (expected %d)\n", | |
251 | err, numregs); | |
252 | err = -EIO; | |
253 | goto get_reg_page_out_unlock; | |
254 | } | |
255 | ||
256 | /* All is well */ | |
257 | err = 0; | |
258 | ||
259 | get_reg_page_out_unlock: | |
260 | mutex_unlock(&ab3100->access_mutex); | |
261 | return err; | |
262 | } | |
956f25a6 | 263 | EXPORT_SYMBOL(ab3100_get_register_page_interruptible); |
14fa5691 | 264 | |
956f25a6 LW |
265 | |
266 | int ab3100_mask_and_set_register_interruptible(struct ab3100 *ab3100, | |
14fa5691 LW |
267 | u8 reg, u8 andmask, u8 ormask) |
268 | { | |
269 | u8 regandval[2] = {reg, 0}; | |
270 | int err; | |
271 | ||
272 | err = mutex_lock_interruptible(&ab3100->access_mutex); | |
273 | if (err) | |
274 | return err; | |
275 | ||
276 | /* First read out the target register */ | |
277 | err = i2c_master_send(ab3100->i2c_client, ®, 1); | |
278 | if (err < 0) { | |
279 | dev_err(ab3100->dev, | |
280 | "write error (maskset send address): %d\n", | |
281 | err); | |
282 | goto get_maskset_unlock; | |
283 | } else if (err != 1) { | |
284 | dev_err(ab3100->dev, | |
285 | "write error (maskset send address) " | |
286 | "%d bytes transferred (expected 1)\n", | |
287 | err); | |
288 | err = -EIO; | |
289 | goto get_maskset_unlock; | |
290 | } | |
291 | ||
292 | err = i2c_master_recv(ab3100->i2c_client, ®andval[1], 1); | |
293 | if (err < 0) { | |
294 | dev_err(ab3100->dev, | |
295 | "write error (maskset read register): %d\n", | |
296 | err); | |
297 | goto get_maskset_unlock; | |
298 | } else if (err != 1) { | |
299 | dev_err(ab3100->dev, | |
300 | "write error (maskset read register) " | |
301 | "%d bytes transferred (expected 1)\n", | |
302 | err); | |
303 | err = -EIO; | |
304 | goto get_maskset_unlock; | |
305 | } | |
306 | ||
307 | /* Modify the register */ | |
308 | regandval[1] &= andmask; | |
309 | regandval[1] |= ormask; | |
310 | ||
311 | /* Write the register */ | |
312 | err = i2c_master_send(ab3100->i2c_client, regandval, 2); | |
313 | if (err < 0) { | |
314 | dev_err(ab3100->dev, | |
315 | "write error (write register): %d\n", | |
316 | err); | |
317 | goto get_maskset_unlock; | |
318 | } else if (err != 2) { | |
319 | dev_err(ab3100->dev, | |
320 | "write error (write register) " | |
321 | "%d bytes transferred (expected 2)\n", | |
322 | err); | |
323 | err = -EIO; | |
324 | goto get_maskset_unlock; | |
325 | } | |
326 | ||
327 | /* All is well */ | |
328 | err = 0; | |
329 | ||
330 | get_maskset_unlock: | |
331 | mutex_unlock(&ab3100->access_mutex); | |
332 | return err; | |
333 | } | |
956f25a6 LW |
334 | EXPORT_SYMBOL(ab3100_mask_and_set_register_interruptible); |
335 | ||
14fa5691 LW |
336 | |
337 | /* | |
338 | * Register a simple callback for handling any AB3100 events. | |
339 | */ | |
340 | int ab3100_event_register(struct ab3100 *ab3100, | |
341 | struct notifier_block *nb) | |
342 | { | |
343 | return blocking_notifier_chain_register(&ab3100->event_subscribers, | |
344 | nb); | |
345 | } | |
346 | EXPORT_SYMBOL(ab3100_event_register); | |
347 | ||
348 | /* | |
349 | * Remove a previously registered callback. | |
350 | */ | |
351 | int ab3100_event_unregister(struct ab3100 *ab3100, | |
352 | struct notifier_block *nb) | |
353 | { | |
354 | return blocking_notifier_chain_unregister(&ab3100->event_subscribers, | |
355 | nb); | |
356 | } | |
357 | EXPORT_SYMBOL(ab3100_event_unregister); | |
358 | ||
359 | ||
360 | int ab3100_event_registers_startup_state_get(struct ab3100 *ab3100, | |
361 | u32 *fatevent) | |
362 | { | |
363 | if (!ab3100->startup_events_read) | |
364 | return -EAGAIN; /* Try again later */ | |
365 | *fatevent = ab3100->startup_events; | |
366 | return 0; | |
367 | } | |
368 | EXPORT_SYMBOL(ab3100_event_registers_startup_state_get); | |
369 | ||
0df883df LW |
370 | /* |
371 | * This is a threaded interrupt handler so we can make some | |
372 | * I2C calls etc. | |
373 | */ | |
374 | static irqreturn_t ab3100_irq_handler(int irq, void *data) | |
14fa5691 | 375 | { |
0df883df | 376 | struct ab3100 *ab3100 = data; |
14fa5691 LW |
377 | u8 event_regs[3]; |
378 | u32 fatevent; | |
379 | int err; | |
380 | ||
2071db48 LW |
381 | add_interrupt_randomness(irq); |
382 | ||
956f25a6 | 383 | err = ab3100_get_register_page_interruptible(ab3100, AB3100_EVENTA1, |
14fa5691 LW |
384 | event_regs, 3); |
385 | if (err) | |
0df883df | 386 | goto err_event; |
14fa5691 LW |
387 | |
388 | fatevent = (event_regs[0] << 16) | | |
389 | (event_regs[1] << 8) | | |
390 | event_regs[2]; | |
391 | ||
392 | if (!ab3100->startup_events_read) { | |
393 | ab3100->startup_events = fatevent; | |
394 | ab3100->startup_events_read = true; | |
395 | } | |
396 | /* | |
397 | * The notified parties will have to mask out the events | |
398 | * they're interested in and react to them. They will be | |
399 | * notified on all events, then they use the fatevent value | |
400 | * to determine if they're interested. | |
401 | */ | |
402 | blocking_notifier_call_chain(&ab3100->event_subscribers, | |
403 | fatevent, NULL); | |
404 | ||
405 | dev_dbg(ab3100->dev, | |
406 | "IRQ Event: 0x%08x\n", fatevent); | |
407 | ||
0df883df | 408 | return IRQ_HANDLED; |
14fa5691 | 409 | |
0df883df | 410 | err_event: |
14fa5691 | 411 | dev_dbg(ab3100->dev, |
0df883df | 412 | "error reading event status\n"); |
14fa5691 LW |
413 | return IRQ_HANDLED; |
414 | } | |
415 | ||
416 | #ifdef CONFIG_DEBUG_FS | |
417 | /* | |
418 | * Some debugfs entries only exposed if we're using debug | |
419 | */ | |
420 | static int ab3100_registers_print(struct seq_file *s, void *p) | |
421 | { | |
422 | struct ab3100 *ab3100 = s->private; | |
423 | u8 value; | |
424 | u8 reg; | |
425 | ||
426 | seq_printf(s, "AB3100 registers:\n"); | |
427 | ||
428 | for (reg = 0; reg < 0xff; reg++) { | |
956f25a6 | 429 | ab3100_get_register_interruptible(ab3100, reg, &value); |
14fa5691 LW |
430 | seq_printf(s, "[0x%x]: 0x%x\n", reg, value); |
431 | } | |
432 | return 0; | |
433 | } | |
434 | ||
435 | static int ab3100_registers_open(struct inode *inode, struct file *file) | |
436 | { | |
437 | return single_open(file, ab3100_registers_print, inode->i_private); | |
438 | } | |
439 | ||
440 | static const struct file_operations ab3100_registers_fops = { | |
441 | .open = ab3100_registers_open, | |
442 | .read = seq_read, | |
443 | .llseek = seq_lseek, | |
444 | .release = single_release, | |
445 | .owner = THIS_MODULE, | |
446 | }; | |
447 | ||
448 | struct ab3100_get_set_reg_priv { | |
449 | struct ab3100 *ab3100; | |
450 | bool mode; | |
451 | }; | |
452 | ||
453 | static int ab3100_get_set_reg_open_file(struct inode *inode, struct file *file) | |
454 | { | |
455 | file->private_data = inode->i_private; | |
456 | return 0; | |
457 | } | |
458 | ||
dfc3aa72 SO |
459 | static ssize_t ab3100_get_set_reg(struct file *file, |
460 | const char __user *user_buf, | |
461 | size_t count, loff_t *ppos) | |
14fa5691 LW |
462 | { |
463 | struct ab3100_get_set_reg_priv *priv = file->private_data; | |
464 | struct ab3100 *ab3100 = priv->ab3100; | |
465 | char buf[32]; | |
dfc3aa72 | 466 | ssize_t buf_size; |
14fa5691 LW |
467 | int regp; |
468 | unsigned long user_reg; | |
469 | int err; | |
470 | int i = 0; | |
471 | ||
472 | /* Get userspace string and assure termination */ | |
473 | buf_size = min(count, (sizeof(buf)-1)); | |
474 | if (copy_from_user(buf, user_buf, buf_size)) | |
475 | return -EFAULT; | |
476 | buf[buf_size] = 0; | |
477 | ||
478 | /* | |
479 | * The idea is here to parse a string which is either | |
480 | * "0xnn" for reading a register, or "0xaa 0xbb" for | |
481 | * writing 0xbb to the register 0xaa. First move past | |
482 | * whitespace and then begin to parse the register. | |
483 | */ | |
484 | while ((i < buf_size) && (buf[i] == ' ')) | |
485 | i++; | |
486 | regp = i; | |
487 | ||
488 | /* | |
489 | * Advance pointer to end of string then terminate | |
490 | * the register string. This is needed to satisfy | |
491 | * the strict_strtoul() function. | |
492 | */ | |
493 | while ((i < buf_size) && (buf[i] != ' ')) | |
494 | i++; | |
495 | buf[i] = '\0'; | |
496 | ||
497 | err = strict_strtoul(&buf[regp], 16, &user_reg); | |
498 | if (err) | |
499 | return err; | |
500 | if (user_reg > 0xff) | |
501 | return -EINVAL; | |
502 | ||
503 | /* Either we read or we write a register here */ | |
504 | if (!priv->mode) { | |
505 | /* Reading */ | |
506 | u8 reg = (u8) user_reg; | |
507 | u8 regvalue; | |
508 | ||
956f25a6 | 509 | ab3100_get_register_interruptible(ab3100, reg, ®value); |
14fa5691 LW |
510 | |
511 | dev_info(ab3100->dev, | |
512 | "debug read AB3100 reg[0x%02x]: 0x%02x\n", | |
513 | reg, regvalue); | |
514 | } else { | |
515 | int valp; | |
516 | unsigned long user_value; | |
517 | u8 reg = (u8) user_reg; | |
518 | u8 value; | |
519 | u8 regvalue; | |
520 | ||
521 | /* | |
522 | * Writing, we need some value to write to | |
523 | * the register so keep parsing the string | |
524 | * from userspace. | |
525 | */ | |
526 | i++; | |
527 | while ((i < buf_size) && (buf[i] == ' ')) | |
528 | i++; | |
529 | valp = i; | |
530 | while ((i < buf_size) && (buf[i] != ' ')) | |
531 | i++; | |
532 | buf[i] = '\0'; | |
533 | ||
534 | err = strict_strtoul(&buf[valp], 16, &user_value); | |
535 | if (err) | |
536 | return err; | |
537 | if (user_reg > 0xff) | |
538 | return -EINVAL; | |
539 | ||
540 | value = (u8) user_value; | |
956f25a6 LW |
541 | ab3100_set_register_interruptible(ab3100, reg, value); |
542 | ab3100_get_register_interruptible(ab3100, reg, ®value); | |
14fa5691 LW |
543 | |
544 | dev_info(ab3100->dev, | |
545 | "debug write reg[0x%02x] with 0x%02x, " | |
546 | "after readback: 0x%02x\n", | |
547 | reg, value, regvalue); | |
548 | } | |
549 | return buf_size; | |
550 | } | |
551 | ||
552 | static const struct file_operations ab3100_get_set_reg_fops = { | |
553 | .open = ab3100_get_set_reg_open_file, | |
554 | .write = ab3100_get_set_reg, | |
555 | }; | |
556 | ||
557 | static struct dentry *ab3100_dir; | |
558 | static struct dentry *ab3100_reg_file; | |
559 | static struct ab3100_get_set_reg_priv ab3100_get_priv; | |
560 | static struct dentry *ab3100_get_reg_file; | |
561 | static struct ab3100_get_set_reg_priv ab3100_set_priv; | |
562 | static struct dentry *ab3100_set_reg_file; | |
563 | ||
564 | static void ab3100_setup_debugfs(struct ab3100 *ab3100) | |
565 | { | |
566 | int err; | |
567 | ||
568 | ab3100_dir = debugfs_create_dir("ab3100", NULL); | |
569 | if (!ab3100_dir) | |
570 | goto exit_no_debugfs; | |
571 | ||
572 | ab3100_reg_file = debugfs_create_file("registers", | |
573 | S_IRUGO, ab3100_dir, ab3100, | |
574 | &ab3100_registers_fops); | |
575 | if (!ab3100_reg_file) { | |
576 | err = -ENOMEM; | |
577 | goto exit_destroy_dir; | |
578 | } | |
579 | ||
580 | ab3100_get_priv.ab3100 = ab3100; | |
581 | ab3100_get_priv.mode = false; | |
582 | ab3100_get_reg_file = debugfs_create_file("get_reg", | |
583 | S_IWUGO, ab3100_dir, &ab3100_get_priv, | |
584 | &ab3100_get_set_reg_fops); | |
585 | if (!ab3100_get_reg_file) { | |
586 | err = -ENOMEM; | |
587 | goto exit_destroy_reg; | |
588 | } | |
589 | ||
590 | ab3100_set_priv.ab3100 = ab3100; | |
591 | ab3100_set_priv.mode = true; | |
592 | ab3100_set_reg_file = debugfs_create_file("set_reg", | |
593 | S_IWUGO, ab3100_dir, &ab3100_set_priv, | |
594 | &ab3100_get_set_reg_fops); | |
595 | if (!ab3100_set_reg_file) { | |
596 | err = -ENOMEM; | |
597 | goto exit_destroy_get_reg; | |
598 | } | |
599 | return; | |
600 | ||
601 | exit_destroy_get_reg: | |
602 | debugfs_remove(ab3100_get_reg_file); | |
603 | exit_destroy_reg: | |
604 | debugfs_remove(ab3100_reg_file); | |
605 | exit_destroy_dir: | |
606 | debugfs_remove(ab3100_dir); | |
607 | exit_no_debugfs: | |
608 | return; | |
609 | } | |
610 | static inline void ab3100_remove_debugfs(void) | |
611 | { | |
612 | debugfs_remove(ab3100_set_reg_file); | |
613 | debugfs_remove(ab3100_get_reg_file); | |
614 | debugfs_remove(ab3100_reg_file); | |
615 | debugfs_remove(ab3100_dir); | |
616 | } | |
617 | #else | |
618 | static inline void ab3100_setup_debugfs(struct ab3100 *ab3100) | |
619 | { | |
620 | } | |
621 | static inline void ab3100_remove_debugfs(void) | |
622 | { | |
623 | } | |
624 | #endif | |
625 | ||
626 | /* | |
627 | * Basic set-up, datastructure creation/destruction and I2C interface. | |
628 | * This sets up a default config in the AB3100 chip so that it | |
629 | * will work as expected. | |
630 | */ | |
631 | ||
632 | struct ab3100_init_setting { | |
633 | u8 abreg; | |
634 | u8 setting; | |
635 | }; | |
636 | ||
00d3803b | 637 | static const struct ab3100_init_setting __initconst |
14fa5691 LW |
638 | ab3100_init_settings[] = { |
639 | { | |
640 | .abreg = AB3100_MCA, | |
641 | .setting = 0x01 | |
642 | }, { | |
643 | .abreg = AB3100_MCB, | |
644 | .setting = 0x30 | |
645 | }, { | |
646 | .abreg = AB3100_IMRA1, | |
647 | .setting = 0x00 | |
648 | }, { | |
649 | .abreg = AB3100_IMRA2, | |
650 | .setting = 0xFF | |
651 | }, { | |
652 | .abreg = AB3100_IMRA3, | |
653 | .setting = 0x01 | |
654 | }, { | |
655 | .abreg = AB3100_IMRB1, | |
ce290b0e | 656 | .setting = 0xBF |
14fa5691 LW |
657 | }, { |
658 | .abreg = AB3100_IMRB2, | |
659 | .setting = 0xFF | |
660 | }, { | |
661 | .abreg = AB3100_IMRB3, | |
662 | .setting = 0xFF | |
663 | }, { | |
664 | .abreg = AB3100_SUP, | |
665 | .setting = 0x00 | |
666 | }, { | |
667 | .abreg = AB3100_DIS, | |
668 | .setting = 0xF0 | |
669 | }, { | |
670 | .abreg = AB3100_D0C, | |
671 | .setting = 0x00 | |
672 | }, { | |
673 | .abreg = AB3100_D1C, | |
674 | .setting = 0x00 | |
675 | }, { | |
676 | .abreg = AB3100_D2C, | |
677 | .setting = 0x00 | |
678 | }, { | |
679 | .abreg = AB3100_D3C, | |
680 | .setting = 0x00 | |
681 | }, | |
682 | }; | |
683 | ||
684 | static int __init ab3100_setup(struct ab3100 *ab3100) | |
685 | { | |
686 | int err = 0; | |
687 | int i; | |
688 | ||
689 | for (i = 0; i < ARRAY_SIZE(ab3100_init_settings); i++) { | |
956f25a6 | 690 | err = ab3100_set_register_interruptible(ab3100, |
14fa5691 LW |
691 | ab3100_init_settings[i].abreg, |
692 | ab3100_init_settings[i].setting); | |
693 | if (err) | |
694 | goto exit_no_setup; | |
695 | } | |
696 | ||
697 | /* | |
698 | * Special trick to make the AB3100 use the 32kHz clock (RTC) | |
956f25a6 | 699 | * bit 3 in test register 0x02 is a special, undocumented test |
14fa5691 LW |
700 | * register bit that only exist in AB3100 P1E |
701 | */ | |
702 | if (ab3100->chip_id == 0xc4) { | |
703 | dev_warn(ab3100->dev, | |
704 | "AB3100 P1E variant detected, " | |
705 | "forcing chip to 32KHz\n"); | |
956f25a6 | 706 | err = ab3100_set_test_register_interruptible(ab3100, 0x02, 0x08); |
14fa5691 LW |
707 | } |
708 | ||
709 | exit_no_setup: | |
710 | return err; | |
711 | } | |
712 | ||
713 | /* | |
714 | * Here we define all the platform devices that appear | |
715 | * as children of the AB3100. These are regular platform | |
716 | * devices with the IORESOURCE_IO .start and .end set | |
717 | * to correspond to the internal AB3100 register range | |
718 | * mapping to the corresponding subdevice. | |
719 | */ | |
720 | ||
721 | #define AB3100_DEVICE(devname, devid) \ | |
722 | static struct platform_device ab3100_##devname##_device = { \ | |
723 | .name = devid, \ | |
724 | .id = -1, \ | |
725 | } | |
726 | ||
2071db48 | 727 | /* This lists all the subdevices */ |
14fa5691 LW |
728 | AB3100_DEVICE(dac, "ab3100-dac"); |
729 | AB3100_DEVICE(leds, "ab3100-leds"); | |
730 | AB3100_DEVICE(power, "ab3100-power"); | |
731 | AB3100_DEVICE(regulators, "ab3100-regulators"); | |
732 | AB3100_DEVICE(sim, "ab3100-sim"); | |
733 | AB3100_DEVICE(uart, "ab3100-uart"); | |
734 | AB3100_DEVICE(rtc, "ab3100-rtc"); | |
735 | AB3100_DEVICE(charger, "ab3100-charger"); | |
736 | AB3100_DEVICE(boost, "ab3100-boost"); | |
737 | AB3100_DEVICE(adc, "ab3100-adc"); | |
738 | AB3100_DEVICE(fuelgauge, "ab3100-fuelgauge"); | |
739 | AB3100_DEVICE(vibrator, "ab3100-vibrator"); | |
740 | AB3100_DEVICE(otp, "ab3100-otp"); | |
741 | AB3100_DEVICE(codec, "ab3100-codec"); | |
742 | ||
743 | static struct platform_device * | |
744 | ab3100_platform_devs[] = { | |
745 | &ab3100_dac_device, | |
746 | &ab3100_leds_device, | |
747 | &ab3100_power_device, | |
748 | &ab3100_regulators_device, | |
749 | &ab3100_sim_device, | |
750 | &ab3100_uart_device, | |
751 | &ab3100_rtc_device, | |
752 | &ab3100_charger_device, | |
753 | &ab3100_boost_device, | |
754 | &ab3100_adc_device, | |
755 | &ab3100_fuelgauge_device, | |
756 | &ab3100_vibrator_device, | |
757 | &ab3100_otp_device, | |
758 | &ab3100_codec_device, | |
759 | }; | |
760 | ||
761 | struct ab_family_id { | |
762 | u8 id; | |
763 | char *name; | |
764 | }; | |
765 | ||
766 | static const struct ab_family_id ids[] __initdata = { | |
767 | /* AB3100 */ | |
768 | { | |
769 | .id = 0xc0, | |
770 | .name = "P1A" | |
771 | }, { | |
772 | .id = 0xc1, | |
773 | .name = "P1B" | |
774 | }, { | |
775 | .id = 0xc2, | |
776 | .name = "P1C" | |
777 | }, { | |
778 | .id = 0xc3, | |
779 | .name = "P1D" | |
780 | }, { | |
781 | .id = 0xc4, | |
782 | .name = "P1E" | |
783 | }, { | |
784 | .id = 0xc5, | |
785 | .name = "P1F/R1A" | |
786 | }, { | |
787 | .id = 0xc6, | |
788 | .name = "P1G/R1A" | |
789 | }, { | |
790 | .id = 0xc7, | |
791 | .name = "P2A/R2A" | |
792 | }, { | |
793 | .id = 0xc8, | |
794 | .name = "P2B/R2B" | |
795 | }, | |
796 | /* AB3000 variants, not supported */ | |
797 | { | |
798 | .id = 0xa0 | |
799 | }, { | |
800 | .id = 0xa1 | |
801 | }, { | |
802 | .id = 0xa2 | |
803 | }, { | |
804 | .id = 0xa3 | |
805 | }, { | |
806 | .id = 0xa4 | |
807 | }, { | |
808 | .id = 0xa5 | |
809 | }, { | |
810 | .id = 0xa6 | |
811 | }, { | |
812 | .id = 0xa7 | |
813 | }, | |
814 | /* Terminator */ | |
815 | { | |
816 | .id = 0x00, | |
817 | }, | |
818 | }; | |
819 | ||
820 | static int __init ab3100_probe(struct i2c_client *client, | |
821 | const struct i2c_device_id *id) | |
822 | { | |
823 | struct ab3100 *ab3100; | |
d619bc14 LW |
824 | struct ab3100_platform_data *ab3100_plf_data = |
825 | client->dev.platform_data; | |
14fa5691 LW |
826 | int err; |
827 | int i; | |
828 | ||
829 | ab3100 = kzalloc(sizeof(struct ab3100), GFP_KERNEL); | |
830 | if (!ab3100) { | |
831 | dev_err(&client->dev, "could not allocate AB3100 device\n"); | |
832 | return -ENOMEM; | |
833 | } | |
834 | ||
835 | /* Initialize data structure */ | |
836 | mutex_init(&ab3100->access_mutex); | |
837 | BLOCKING_INIT_NOTIFIER_HEAD(&ab3100->event_subscribers); | |
838 | ||
839 | ab3100->i2c_client = client; | |
840 | ab3100->dev = &ab3100->i2c_client->dev; | |
841 | ||
842 | i2c_set_clientdata(client, ab3100); | |
843 | ||
844 | /* Read chip ID register */ | |
956f25a6 LW |
845 | err = ab3100_get_register_interruptible(ab3100, AB3100_CID, |
846 | &ab3100->chip_id); | |
14fa5691 LW |
847 | if (err) { |
848 | dev_err(&client->dev, | |
849 | "could not communicate with the AB3100 analog " | |
850 | "baseband chip\n"); | |
851 | goto exit_no_detect; | |
852 | } | |
853 | ||
854 | for (i = 0; ids[i].id != 0x0; i++) { | |
855 | if (ids[i].id == ab3100->chip_id) { | |
856 | if (ids[i].name != NULL) { | |
857 | snprintf(&ab3100->chip_name[0], | |
858 | sizeof(ab3100->chip_name) - 1, | |
859 | "AB3100 %s", | |
860 | ids[i].name); | |
861 | break; | |
862 | } else { | |
863 | dev_err(&client->dev, | |
864 | "AB3000 is not supported\n"); | |
865 | goto exit_no_detect; | |
866 | } | |
867 | } | |
868 | } | |
869 | ||
870 | if (ids[i].id == 0x0) { | |
871 | dev_err(&client->dev, "unknown analog baseband chip id: 0x%x\n", | |
872 | ab3100->chip_id); | |
873 | dev_err(&client->dev, "accepting it anyway. Please update " | |
874 | "the driver.\n"); | |
875 | goto exit_no_detect; | |
876 | } | |
877 | ||
878 | dev_info(&client->dev, "Detected chip: %s\n", | |
879 | &ab3100->chip_name[0]); | |
880 | ||
881 | /* Attach a second dummy i2c_client to the test register address */ | |
882 | ab3100->testreg_client = i2c_new_dummy(client->adapter, | |
883 | client->addr + 1); | |
884 | if (!ab3100->testreg_client) { | |
885 | err = -ENOMEM; | |
886 | goto exit_no_testreg_client; | |
887 | } | |
888 | ||
14fa5691 LW |
889 | err = ab3100_setup(ab3100); |
890 | if (err) | |
891 | goto exit_no_setup; | |
892 | ||
0df883df | 893 | err = request_threaded_irq(client->irq, NULL, ab3100_irq_handler, |
2071db48 LW |
894 | IRQF_ONESHOT, "ab3100-core", ab3100); |
895 | /* This real unpredictable IRQ is of course sampled for entropy */ | |
896 | rand_initialize_irq(client->irq); | |
897 | ||
14fa5691 LW |
898 | if (err) |
899 | goto exit_no_irq; | |
900 | ||
901 | /* Set parent and a pointer back to the container in device data */ | |
902 | for (i = 0; i < ARRAY_SIZE(ab3100_platform_devs); i++) { | |
903 | ab3100_platform_devs[i]->dev.parent = | |
904 | &client->dev; | |
d619bc14 LW |
905 | ab3100_platform_devs[i]->dev.platform_data = |
906 | ab3100_plf_data; | |
14fa5691 LW |
907 | platform_set_drvdata(ab3100_platform_devs[i], ab3100); |
908 | } | |
909 | ||
910 | /* Register the platform devices */ | |
911 | platform_add_devices(ab3100_platform_devs, | |
912 | ARRAY_SIZE(ab3100_platform_devs)); | |
913 | ||
914 | ab3100_setup_debugfs(ab3100); | |
915 | ||
916 | return 0; | |
917 | ||
918 | exit_no_irq: | |
919 | exit_no_setup: | |
920 | i2c_unregister_device(ab3100->testreg_client); | |
921 | exit_no_testreg_client: | |
922 | exit_no_detect: | |
923 | kfree(ab3100); | |
924 | return err; | |
925 | } | |
926 | ||
927 | static int __exit ab3100_remove(struct i2c_client *client) | |
928 | { | |
929 | struct ab3100 *ab3100 = i2c_get_clientdata(client); | |
930 | int i; | |
931 | ||
932 | /* Unregister subdevices */ | |
933 | for (i = 0; i < ARRAY_SIZE(ab3100_platform_devs); i++) | |
934 | platform_device_unregister(ab3100_platform_devs[i]); | |
935 | ||
936 | ab3100_remove_debugfs(); | |
937 | i2c_unregister_device(ab3100->testreg_client); | |
938 | ||
939 | /* | |
940 | * At this point, all subscribers should have unregistered | |
941 | * their notifiers so deactivate IRQ | |
942 | */ | |
943 | free_irq(client->irq, ab3100); | |
944 | kfree(ab3100); | |
945 | return 0; | |
946 | } | |
947 | ||
948 | static const struct i2c_device_id ab3100_id[] = { | |
dcffa125 | 949 | { "ab3100", 0 }, |
14fa5691 LW |
950 | { } |
951 | }; | |
952 | MODULE_DEVICE_TABLE(i2c, ab3100_id); | |
953 | ||
954 | static struct i2c_driver ab3100_driver = { | |
955 | .driver = { | |
956 | .name = "ab3100", | |
957 | .owner = THIS_MODULE, | |
958 | }, | |
959 | .id_table = ab3100_id, | |
960 | .probe = ab3100_probe, | |
961 | .remove = __exit_p(ab3100_remove), | |
962 | }; | |
963 | ||
964 | static int __init ab3100_i2c_init(void) | |
965 | { | |
966 | return i2c_add_driver(&ab3100_driver); | |
967 | } | |
968 | ||
969 | static void __exit ab3100_i2c_exit(void) | |
970 | { | |
971 | i2c_del_driver(&ab3100_driver); | |
972 | } | |
973 | ||
974 | subsys_initcall(ab3100_i2c_init); | |
975 | module_exit(ab3100_i2c_exit); | |
976 | ||
977 | MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>"); | |
978 | MODULE_DESCRIPTION("AB3100 core driver"); | |
979 | MODULE_LICENSE("GPL"); |