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