Linux Audio

Check our new training course

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